-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.lua
More file actions
166 lines (136 loc) · 4.46 KB
/
menu.lua
File metadata and controls
166 lines (136 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
local Component = require("component")
LANG = 0
USERNAME = 1
TOKEN = 2
return function ()
local showing = false
local hide_callback = function () end
local cursor_pos = 0
local lang = "en"
local menu_index = 0
local username = ""
local token = ""
local time, flash = 0, 0
local inputs = {
{ -- language_select
clear = function ()
lang = "en"
cursor_pos = 0
end,
keypressed = function (key)
if key == "left" or key == "right" then
if lang ~= "en" then
lang = "en"
cursor_pos = 0
else
lang = "jp"
cursor_pos = 100
end
end
end
},
{ -- username
clear = function ()
username = ""
username_cursor_pos = 200
end,
textinput = function (key)
username = username .. key
end
},
{ -- token
clear = function ()
token = ""
token_cursor_pos = 200
end,
textinput = function (key)
token = token .. key
end
}
}
local drawCursor = function (x, y)
local icon = ">"
love.graphics.print(icon, x + cursor_pos, y)
end
local drawUsername = function (x, y)
local icon = ""
if flash == 0 and menu_index == USERNAME then icon = "_" end
love.graphics.print(username .. icon, x, y)
end
local drawToken = function (x, y)
local icon = ""
if flash == 0 and menu_index == TOKEN then icon = "_" end
love.graphics.print(token .. icon, x, y)
end
local localization = Component(0, 0, Component(0, 0, "LANG"), Component(200, 0, drawCursor), Component(230, 0, "EN"), Component(330, 0, "JP"))
local username_part = Component(0, 100, Component(0, 0, "USERNAME"), Component(200, 0, drawUsername))
local token_part = Component(0, 200, Component(0, 0, " TOKEN"), Component(200, 0, drawToken))
local component = Component(100, W_HEIGHT/2 - 200, localization, username_part, token_part)
local draw = function ()
component.draw(0, 0)
end
local update = function (dt)
time = time + 2*dt
flash = math.floor(time)%2
end
local writeProfile = function ()
local hfile = love.filesystem.newFile("profile.lua", "w")
if hfile == nil then return end
hfile:write('return { lang = "' .. lang .. '", username = "' .. username .. '", token = "' .. token .. '" }')--bad argument #1 to 'write' (string expected, got nil)
hfile:close()
end
local findProfile = function ()
return love.filesystem.isFile("profile.lua")
end
local recoverProfile = function ()
local profile = love.filesystem.load("profile.lua")
local status, result = pcall(profile)
if status then return result end
end
local show = function (callback)
hide_callback = callback
showing = not findProfile()
if not showing then
callback()
end
end
local hide = function ()
hide_callback()
showing = false
end
local isShowing = function ()
return showing
end
local keypressed = function (key)
if menu_index == TOKEN and key == "return" then
writeProfile()
hide()
end
if key == "down" or (key == "return" and menu_index < 2) then
menu_index = (menu_index + 1)%3
inputs[menu_index + 1].clear()
end
if key == "up" then
menu_index = (menu_index - 1)%3
inputs[menu_index + 1].clear()
end
if inputs[menu_index + 1].keypressed then
inputs[menu_index + 1].keypressed(key)
end
end
local textinput = function (key)
if inputs[menu_index + 1].textinput then
inputs[menu_index + 1].textinput(key)
end
end
return {
draw = draw,
update = update,
keypressed = keypressed,
textinput = textinput,
show = show,
hide = hide,
recoverProfile = recoverProfile,
isShowing = isShowing
}
end