forked from sirrobzeroone/3d_armor_flyswim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.lua
More file actions
351 lines (287 loc) · 11.3 KB
/
Copy pathgui.lua
File metadata and controls
351 lines (287 loc) · 11.3 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
-- 3D Armor Hovering Animations
-- Copyright (C) 2026 Kunshan Wang
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public
-- License along with this library; if not, see <https://www.gnu.org/licenses/>.
local function formspec_builder()
local formspec = {}
local b = {}
function b:add(str)
table.insert(formspec, str)
end
function b:add_many(...)
for i = 1, select("#", ...) do
table.insert(formspec, select(i, ...))
end
end
function b:add_format(...)
local str = string.format(...)
table.insert(formspec, str)
end
-- Add a place holder and return a function.
-- Calling the returned function will set the place holder to a new string.
function b:add_later()
table.insert(formspec, "")
local index = #formspec
return function(text)
formspec[index] = text
end
end
function b:get_formspec()
return table.concat(formspec, "")
end
return b
end
local function xy(w, h)
return string.format("%f,%f", w, h)
end
local function wh(w, h)
return xy(w, h)
end
local function textxy(x, y, w, h)
return string.format("%f,%f", x, y + h / 2)
end
local function xy_wh(x, y, w, h)
return string.format("%f,%f;%f,%f", x, y, w, h)
end
-- A helper that builds rects by cutting parts away from a bigger rect.
local function box_cut_layout(left, top, width, height)
local b = {}
function b:cut_left(size)
size = math.min(size, width)
local x, y, w, h = left, top, size, height
left, width = left + w, width - w
return x, y, w, h
end
function b:cut_top(size)
size = math.min(size, height)
local x, y, w, h = left, top, width, size
top, height = top + h, height - h
return x, y, w, h
end
function b:cut_right(size)
size = math.min(size, width)
local x, y, w, h = left + width - size, top, size, height
width = width - w
return x, y, w, h
end
function b:cut_bottom(size)
size = math.min(size, height)
local x, y, w, h = left, top + height - size, width, size
height = height - h
return x, y, w, h
end
function b:rest()
return left, top, width, height
end
return b
end
local function linear_layout(horizontal, padding, spacing, left, top, width, height)
padding = math.min(width / 2, height / 2, padding)
left = left + padding
top = top + padding
width = width - padding * 2
height = height - padding * 2
local bcl = box_cut_layout(left, top, width, height)
local started = false
local b = {}
local function do_spacing(special_spacing)
if not started then
started = true
else
local this_spacing = special_spacing or spacing
if horizontal then
bcl:cut_left(this_spacing)
else
bcl:cut_top(this_spacing)
end
end
end
function b:add(size, special_spacing)
do_spacing(special_spacing)
if horizontal then
return bcl:cut_left(size)
else
return bcl:cut_top(size)
end
end
function b:rest(special_spacing)
do_spacing(special_spacing)
return bcl:rest()
end
-- End the layout early. Return the padded size (current stuffed content size plus twice the padding).
function b:cut_off()
local cur_left, cur_top, _, _ = bcl:rest()
local start = horizontal and left or top
local cursor = horizontal and cur_left or cur_top
return cursor - start + padding * 2
end
return b
end
local function even_layout(rows, cols, padding, spacing, left, top, width, height)
padding = math.min(width / 2, height / 2, padding)
left = left + padding
top = top + padding
width = width - padding * 2
height = height - padding * 2
local cell_width = (width - spacing * (cols - 1)) / cols
local cell_height = (height - spacing * (rows - 1)) / rows
local b = {}
function b:get(row, col)
local cell_left = left + (spacing + cell_width) * (col - 1)
local cell_top = top + (spacing + cell_height) * (row - 1)
return cell_left, cell_top, cell_width, cell_height
end
return b
end
armor_hover.gui_style = {
window_width = 8,
padding = 0.375,
spacing = 0.25,
title_height = 0.5,
label_height = 0.3,
dropdown_height = 0.5,
info_width = 0.5,
}
local ENABLE_PREVIEW = false
function armor_hover.get_config_formspec(player_name)
local player = core.get_player_by_name(player_name)
local style = armor_hover.gui_style
local b = formspec_builder()
b:add("formspec_version[10]")
-- We'll set the size later.
local set_size = b:add_later()
-- Set an unrealistic large height and we will determine the actual size later.
local vlayout = linear_layout(false, style.padding, style.spacing, 0, 0, style.window_width, 999999)
local function label_widget_buttons(label_text, info, reset_name, callback)
b:add_format("label[%s;%s]",
xy_wh(vlayout:add(style.label_height)),
core.formspec_escape(label_text))
local bcl = box_cut_layout(vlayout:add(style.dropdown_height, 0))
-- Unconditionall reserve space for the info and reset icons.
local info_xywh = xy_wh(bcl:cut_right(style.info_width))
bcl:cut_right(style.spacing)
local reset_xywh = xy_wh(bcl:cut_right(style.info_width))
bcl:cut_right(style.spacing)
if info then
b:add_format("image[%s;settings_info.png]", info_xywh)
b:add_format("tooltip[%s;%s]", info_xywh, core.formspec_escape(info))
end
if reset_name then
b:add_format("image_button[%s;settings_reset.png;%s;]", reset_xywh, reset_name)
b:add_format("tooltip[%s;%s]", reset_xywh, core.formspec_escape("Reset to default"))
end
callback(bcl)
end
b:add_format("label[%s;%s]", xy_wh(vlayout:add(style.title_height)),
core.formspec_escape("3D Armor Hovering Animation Configuration"))
-- Skin switcher (if using bundled skins)
if armor_hover.skin_backend.name == "bundled_skins" then
local label_text = "Skin:"
local info = "Select the player skin."
local resetter = "reset_skin"
local skin_names = {}
for i, skin in ipairs(armor_hover.skin_backend.skins) do
table.insert(skin_names, core.formspec_escape(skin.skin_name))
end
local skin_names_string = table.concat(skin_names, ",")
local chosen_index = armor_hover.skin_backend:get_player_skin_index(player)
label_widget_buttons(label_text, info, resetter, function(bcl)
b:add_format("dropdown[%s;skin;%s;%d;true]",
xy_wh(bcl:rest()),
skin_names_string,
chosen_index)
end)
end
-- Animation configurations
do
local options = armor_hover.configurable_anim_names
local options_string = table.concat(options, ",")
for _, mstate in ipairs(armor_hover.mstate_list) do
local mstate_map = armor_hover.mstates[mstate]
local chosen_anim_name = armor_hover.get_chosen_anim_name(player, mstate)
local chosen_index = armor_hover.list_find(options, chosen_anim_name) or 1
local label_text = string.format("%s animation:", mstate_map.description)
label_widget_buttons(label_text, nil, "reset_selector_" .. mstate, function(bcl)
b:add_format("dropdown[%s;selector_%s;%s;%d;false]",
xy_wh(bcl:rest()),
mstate,
options_string,
chosen_index)
end)
end
end
-- When-stop-fly behavior
do
local options = armor_hover.player_configs.when_stop_fly.possible_values
local options_string = table.concat(options, ",")
local cur_value = armor_hover.player_configs.when_stop_fly:get(player)
local index = armor_hover.list_find(options, cur_value)
local label_text = "When stop moving during flight..."
local info = [[
keep: Keep the flying animation
hover: Switch to hovering animation]]
label_widget_buttons(label_text, info, "reset_when_stop_fly", function(bcl)
b:add_format("dropdown[%s;when_stop_fly;%s;%d;false]",
xy_wh(bcl.rest()),
options_string,
index)
end)
end
-- Eye offset
do
local eye_offset = armor_hover.player_configs.eye_offset:get(player)
local label_text = "3rd person rear view eye offset:"
local info = [[
Add an eye offset in third-person rear view mode to make it more comfortable to play in third-person mode.
The value is a vector of the form "(dx, dy, dz)", representing the offset from the character model's eye position.
Positive dx moves to the right; positive dy moves to the top; and positive dz moves to the front.]]
label_widget_buttons(label_text, info, "reset_eye_offset", function(bcl)
b:add_format("field[%s;eye_offset;;%s]field_close_on_enter[eye_offset;false]",
xy_wh(bcl.rest()),
vector.to_string(eye_offset))
end)
end
-- A bug (fixed in Git version) in Luanti is preventing the `model[]` from animating.
-- We temporarily disable the preview until a stable version is released.
-- See: https://github.com/luanti-org/luanti/commit/619f780c17775601f2b9682b4a84ca64477b4187
if ENABLE_PREVIEW then
local hlayout = even_layout(1, 4, 0, style.spacing, vlayout:rest())
local prop = player:get_properties()
local mesh = prop.mesh
local textures = table.concat(prop.textures, ",")
local function add_preview(index, anim_name)
local anim = armor_hover.animations[anim_name]
armor_hover.debug(anim_name, anim.x, anim.y)
local vlayout2 = linear_layout(false, 0, 0, hlayout:get(1, index))
b:add_format("label[%s;%s]", xy_wh(vlayout2:add(style.label_height)), core.formspec_escape(anim_name))
b:add_format("model[%s;preview_%s;%s;%s;0,180;false;true;%d,%d;30]",
xy_wh(vlayout2:rest()),
core.formspec_escape(anim_name),
mesh,
textures,
1000 or anim.x,
2000 or anim.y,
anim.animation_speed or 30
)
end
add_preview(1, "hover1")
add_preview(2, "hover2")
add_preview(3, "fly_slow")
add_preview(4, "fly_fast")
end
-- Determine the actual size
local window_height = vlayout:cut_off()
set_size(string.format("size[%s]", wh(style.window_width, window_height)))
return b:get_formspec()
end