-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpluginoptionshandler.lua
287 lines (237 loc) · 8.77 KB
/
pluginoptionshandler.lua
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
local PluginOptionsHandler, super = Class(StateClass, "PluginOptionsHandler")
function PluginOptionsHandler:init(menu)
self.menu = menu
self.font = Assets.getFont("main")
self.scroll_target_y = 0
self.scroll_y = 0
self.selected_option = 1
self.noise_timer = 0
local function yeahnah(val)
return function()
if Kristal.Config["plugins/"..val] then
return "YES"
else
return "NO"
end
end
end
local function toggle(val)
return function()
Kristal.Config["plugins/"..val] = not Kristal.Config["plugins/"..val]
end
end
local NyI = {
value = function()
return "NyI"
end,
callback = function ()
Assets.playSound("bluh")
end
}
local function enum(name, val, options)
return {
name = name,
value = function ()
return Kristal.Config["plugins/"..val]
end,
callback = function()
local index = Utils.getIndex(options, Kristal.Config["plugins/"..val])
if index == nil then index = 0 end
index = ((index) % (#options)+1)
Kristal.Config["plugins/"..val] = options[index]
end
}
end
self.options = {}
for _, mod in pairs(Kristal.Mods.getMods()) do
local id = mod.id
if mod.plugin then
local name = mod.name
if type(mod.plugin) == "table" and mod.plugin.name then
name = mod.plugin.name
end
local option = { name = name }
function option.value() return Kristal.Config["plugins/enabled_plugins"][id] and "ON" or "OFF" end
function option.callback() Kristal.Config["plugins/enabled_plugins"][id] = not Kristal.Config["plugins/enabled_plugins"][id] end
local pluginmenu = mod.plugin.menu
if pluginmenu == nil and love.filesystem.getInfo(mod.path.."/options.lua") then
pluginmenu = true
end
if pluginmenu == true then
pluginmenu = "plugin_"..mod.id
end
if type(mod.plugin) == "table" and pluginmenu then
local oldvalue = option.value
function option.value(x,y)
if self.options[self.selected_option] == option then
if Input.usingGamepad() then
Draw.draw(Input.getTexture("menu"), x + self.font:getWidth(oldvalue() .. " > "),y + 3, 0, 2, 2)
else
return oldvalue() .. " > " .. Input.getText("menu")
end
end
return oldvalue() .. " >"
end
function option.aux() self.menu:setState(pluginmenu) end
end
table.insert(self.options, option)
end
end
end
function PluginOptionsHandler:registerEvents()
self:registerEvent("enter", self.onEnter)
self:registerEvent("keypressed", self.onKeyPressed)
self:registerEvent("update", self.update)
self:registerEvent("draw", self.draw)
end
function PluginOptionsHandler:onEnter(old_state)
if old_state == "MODSELECT" then
self.selected_option = 1
self.scroll_target_y = 0
self.scroll_y = 0
end
end
function PluginOptionsHandler:onLeave()
Kristal.saveConfig()
end
function PluginOptionsHandler:onKeyPressed(key, is_repeat)
if Input.isCancel(key) then
Assets.stopAndPlaySound("ui_move")
Kristal.saveConfig()
if TARGET_MOD then
self.menu:setState("OPTIONS")
else
self.menu:setState("MODSELECT")
end
return
end
local move_noise = false
--[[
local page_dir = "right"
local old_page = self.selected_page
if Input.is("left", key) then
self.selected_page = self.selected_page - 1
page_dir = "left"
end
if Input.is("right", key) then
self.selected_page = self.selected_page + 1
page_dir = "right"
end
self.selected_page = Utils.clamp(self.selected_page, 1, #self.pages)
if self.selected_page ~= old_page then
move_noise = true
self.selected_option = 1
self.scroll_target_y = 0
self.scroll_y = 0
self.page_scroll_direction = page_dir
self.page_scroll_timer = 0.1
end
local page = self.pages[self.selected_page]
]]
local options = self.options
local max_option = #options + 1
local old_option = self.selected_option
if Input.is("up", key) then self.selected_option = self.selected_option - 1 end
if Input.is("down", key) then self.selected_option = self.selected_option + 1 end
if self.selected_option > max_option then self.selected_option = is_repeat and max_option or 1 end
if self.selected_option < 1 then self.selected_option = is_repeat and 1 or max_option end
if old_option ~= self.selected_option then
move_noise = true
end
if move_noise then
Assets.stopAndPlaySound("ui_move")
end
if Input.isConfirm(key) then
Assets.stopAndPlaySound("ui_select")
if self.selected_option == max_option then
-- "Back" button
Kristal.saveConfig()
if TARGET_MOD then
self.menu:setState("OPTIONS")
else
self.menu:setState("MODSELECT")
end
elseif options[self.selected_option].callback then
options[self.selected_option].callback()
end
end
if Input.isMenu(key) then
if self.selected_option == max_option then
-- do nothing
elseif options[self.selected_option].aux then
Assets.stopAndPlaySound("ui_select")
options[self.selected_option].aux()
end
end
end
function PluginOptionsHandler:getHeartPos()
local options = self.options
local max_option = #options + 1
local x, y = 152, 129
if self.selected_option < max_option then
x = 152
y = 129 + (self.selected_option - 1) * 32 + self.scroll_target_y
else
-- "Back" button
x = 320 - 32 - 16 + 1
y = 480 - 16 + 1
end
return x, y
end
function PluginOptionsHandler:update()
local options = self.options
local max_option = #options + 1
if self.selected_option < max_option then
local y_off = (self.selected_option - 1) * 32
if y_off + self.scroll_target_y < 0 then
self.scroll_target_y = self.scroll_target_y + (0 - (y_off + self.scroll_target_y))
end
if y_off + self.scroll_target_y > (9 * 32) then
self.scroll_target_y = self.scroll_target_y + ((9 * 32) - (y_off + self.scroll_target_y))
end
end
if (math.abs((self.scroll_target_y - self.scroll_y)) <= 2) then
self.scroll_y = self.scroll_target_y
end
self.scroll_y = self.scroll_y + ((self.scroll_target_y - self.scroll_y) / 2) * DTMULT
self.menu.heart_target_x, self.menu.heart_target_y = self:getHeartPos()
end
function PluginOptionsHandler:draw()
local menu_font = self.font
local options = self.options
local title = "PLUGINS"
local title_width = menu_font:getWidth(title)
Draw.setColor(1, 1, 1)
Draw.printShadow(title, 0, 48, 2, "center", 640)
local menu_x = 185 - 14
local menu_y = 110
local width = 360
local height = 32 * 10
local total_height = 32 * #options
Draw.pushScissor()
Draw.scissor(menu_x, menu_y, width + 10, height + 10)
menu_y = menu_y + self.scroll_y
for i, option in ipairs(options) do
local y = menu_y + 32 * (i - 1)
Draw.printShadow(option.name, menu_x, y)
local value_x = menu_x + (32 * 8)
local value = option.value and option.value(value_x, y) or nil
if value then
Draw.printShadow(tostring(value), value_x, y)
end
end
-- Draw the scrollbar background if the menu scrolls
if total_height > height then
Draw.setColor({ 0, 0, 0, 0.5 })
love.graphics.rectangle("fill", menu_x + width, 0, 4, menu_y + height - self.scroll_y)
local scrollbar_height = (height / total_height) * height
local scrollbar_y = (-self.scroll_y / (total_height - height)) * (height - scrollbar_height)
Draw.popScissor()
Draw.setColor(1, 1, 1, 1)
love.graphics.rectangle("fill", menu_x + width, menu_y + scrollbar_y - self.scroll_y, 4, scrollbar_height)
else
Draw.popScissor()
end
Draw.printShadow("Back", 0, 454 - 8, 2, "center", 640)
end
return PluginOptionsHandler