3ef575ee
Stefan Wichmann
Initial checkin
|
1
2
3
4
5
6
7
8
|
-- calculator.lua
-- ----------------------------------------------------------------
local function get_gui_root(player)
return player.gui.screen
end
-- ----------------------------------------------------------------
|
d7b81613
Stefan Wichmann
Minor fixed - rel...
|
9
10
11
12
13
14
15
16
17
18
19
20
21
|
function show_think(player, enabled)
local root = get_gui_root(player)
if enabled then
root.calcui.calcui_table.calcui_table_col2.calcui_rant.sprite = "sprite_calcui_think"
root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.style.height = 196
else
root.calcui.calcui_table.calcui_table_col2.calcui_rant.sprite = nil
root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.style.height = 252
end
end
-- ----------------------------------------------------------------
|
3ef575ee
Stefan Wichmann
Initial checkin
|
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
function show_rant(player, enabled)
local root = get_gui_root(player)
if enabled then
root.calcui.calcui_table.calcui_table_col2.calcui_rant.sprite = "sprite_calcui_rant"
root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.style.height = 196
else
root.calcui.calcui_table.calcui_table_col2.calcui_rant.sprite = nil
root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.style.height = 252
end
end
-- ----------------------------------------------------------------
local function destroy_calculator(player)
local root = get_gui_root(player)
if root.calcui then
root.calcui.destroy()
end
end
-- ----------------------------------------------------------------
|
ae0df041
Stefan Wichmann
Fixed multiplayer...
|
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
function fix_oob_ui(player)
if global.gui_position[player.index].x < 0 then
global.gui_position[player.index].x = 0
end
if global.gui_position[player.index].y < 0 then
global.gui_position[player.index].y = 0
end
-- TODO fixed box size, because there is no API call for that
local width = 255
local height = 350
if global.gui_position[player.index].x + width > player.display_resolution.width then
global.gui_position[player.index].x = player.display_resolution.width - width
end
if global.gui_position[player.index].y + height > player.display_resolution.height then
global.gui_position[player.index].y = player.display_resolution.height - height
end
--global.gui_position[player.index]
end
-- ----------------------------------------------------------------
|
3ef575ee
Stefan Wichmann
Initial checkin
|
65
66
|
function show_calculator(player)
local root = get_gui_root(player)
|
3ef575ee
Stefan Wichmann
Initial checkin
|
67
|
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
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
|
if not root.calcui then
local calcui = root.add({
type = "frame",
name = "calcui",
style = "dialog_frame",
direction = "vertical"
})
local flow = calcui.add({
type = "flow",
name = "calcui_flow"
})
flow.style.horizontally_stretchable = "on"
flow.add({
type = "label",
caption = {"calculator-ui.title"},
style = "frame_title"
}).drag_target = calcui
local widget = flow.add({
type = "empty-widget",
style = "draggable_space_header",
name = "calcui_drag"
})
widget.drag_target = calcui
widget.style.horizontally_stretchable = "on"
widget.style.minimal_width = 24
widget.style.natural_height = 24
flow.add({
type = "sprite-button",
sprite = "utility/close_white",
style = "frame_action_button",
name = "calcui_close"
})
local table = calcui.add({
type = "table",
name = "calcui_table",
column_count = "2",
vertical_centering = "false"
})
local col1 = table.add({
type = "flow",
name = "calcui_table_col1",
direction = "vertical"
})
local display = col1.add({
type = "textfield",
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
120
121
122
|
caption = "",
name = "calcui_display"
})
|
1eefa090
Stefan Wichmann
visual overhaul i...
|
123
|
display.style.width = 212
|
3ef575ee
Stefan Wichmann
Initial checkin
|
124
|
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
125
|
local row1 = col1.add({type="flow", name="calcui_col1_row1", direction="horizontal"})
|
1eefa090
Stefan Wichmann
visual overhaul i...
|
126
127
128
129
|
row1.add({type="sprite-button", style="calcui_button_style_light", caption="CE", name="calcui_button_CE"}).sprite = "sprite_calcui_light" -- CE = Clear Entry (just this line)
row1.add({type="sprite-button", style="calcui_button_style_light", caption="C", name="calcui_button_C"}).sprite = "sprite_calcui_light" -- C = Clear (all, past results as well)
row1.add({type="sprite-button", style="calcui_button_style_light", caption="BS", name="calcui_button_BS"}).sprite = "sprite_calcui_light"
row1.add({type="sprite-button", style="calcui_button_style_light", caption="/", name="calcui_button_DIV"}).sprite = "sprite_calcui_light"
|
3ef575ee
Stefan Wichmann
Initial checkin
|
130
|
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
131
|
local row2 = col1.add({type="flow", name="calcui_col1_row2", direction="horizontal"})
|
1eefa090
Stefan Wichmann
visual overhaul i...
|
132
133
134
135
|
row2.add({type="sprite-button", style="calcui_button_style_dark", caption="7", name="calcui_button_7"}).sprite = "sprite_calcui_dark"
row2.add({type="sprite-button", style="calcui_button_style_dark", caption="8", name="calcui_button_8"}).sprite = "sprite_calcui_dark"
row2.add({type="sprite-button", style="calcui_button_style_dark", caption="9", name="calcui_button_9"}).sprite = "sprite_calcui_dark"
row2.add({type="sprite-button", style="calcui_button_style_light", caption="*", name="calcui_button_MUL"}).sprite = "sprite_calcui_light"
|
3ef575ee
Stefan Wichmann
Initial checkin
|
136
|
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
137
|
local row3 = col1.add({type="flow", name="calcui_col1_row3", direction="horizontal"})
|
1eefa090
Stefan Wichmann
visual overhaul i...
|
138
139
140
141
|
row3.add({type="sprite-button", style="calcui_button_style_dark", caption="4", name="calcui_button_4"}).sprite = "sprite_calcui_dark"
row3.add({type="sprite-button", style="calcui_button_style_dark", caption="5", name="calcui_button_5"}).sprite = "sprite_calcui_dark"
row3.add({type="sprite-button", style="calcui_button_style_dark", caption="6", name="calcui_button_6"}).sprite = "sprite_calcui_dark"
row3.add({type="sprite-button", style="calcui_button_style_light", caption="-", name="calcui_button_SUB"}).sprite = "sprite_calcui_light"
|
3ef575ee
Stefan Wichmann
Initial checkin
|
142
|
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
143
|
local row4 = col1.add({type="flow", name="calcui_col1_row4", direction="horizontal"})
|
1eefa090
Stefan Wichmann
visual overhaul i...
|
144
145
146
147
|
row4.add({type="sprite-button", style="calcui_button_style_dark", caption="1", name="calcui_button_1"}).sprite = "sprite_calcui_dark"
row4.add({type="sprite-button", style="calcui_button_style_dark", caption="2", name="calcui_button_2"}).sprite = "sprite_calcui_dark"
row4.add({type="sprite-button", style="calcui_button_style_dark", caption="3", name="calcui_button_3"}).sprite = "sprite_calcui_dark"
row4.add({type="sprite-button", style="calcui_button_style_light", caption="+", name="calcui_button_ADD"}).sprite = "sprite_calcui_light"
|
3ef575ee
Stefan Wichmann
Initial checkin
|
148
|
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
149
|
local row5 = col1.add({type="flow", name="calcui_col1_row5", direction="horizontal"})
|
1eefa090
Stefan Wichmann
visual overhaul i...
|
150
151
152
153
|
row5.add({type="sprite-button", style="calcui_button_style_light", caption="%", name="calcui_button_PERC"}).sprite = "sprite_calcui_light"
row5.add({type="sprite-button", style="calcui_button_style_dark", caption="0", name="calcui_button_0"}).sprite = "sprite_calcui_dark"
row5.add({type="sprite-button", style="calcui_button_style_dark", caption=".", name="calcui_button_DOT"}).sprite = "sprite_calcui_dark"
row5.add({type="sprite-button", style="calcui_button_style_red", caption="=", name="calcui_button_EQU"}).sprite = "sprite_calcui_red"
|
3ef575ee
Stefan Wichmann
Initial checkin
|
154
|
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
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
|
local col2 = table.add({
type = "flow",
name = "calcui_table_col2",
direction = "vertical"
})
local result = col2.add({
type = "label",
caption = "= ",
name = "calcui_display_result"
})
result.style.font = "default-large"
local rant = col2.add({
type = "sprite",
name = "calcui_rant"
})
col2.add({
type = "line",
direction = "horizontal"
})
local scroll = col2.add({
type = "scroll-pane",
name = "calcui_scroll_pane"
})
scroll.style.height = 252
local recents = scroll.add({
type = "table",
caption = "",
name = "calcui_result_table",
column_count = "2"
})
recents.style.column_alignments[1] = "right"
|
ae0df041
Stefan Wichmann
Fixed multiplayer...
|
193
194
195
196
197
198
199
200
201
202
203
204
|
-- use last saved location or center the gui
if not global.gui_position then
global.gui_position = {}
end
if global.gui_position[player.index] then
-- fix weird saved positions (out of reach)
fix_oob_ui(player)
calcui.location = global.gui_position[player.index]
else
calcui.force_auto_center()
end
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
205
|
end
|
3ef575ee
Stefan Wichmann
Initial checkin
|
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
end
-- ----------------------------------------------------------------
function hide_calculator(player)
destroy_calculator(player)
end
-- ----------------------------------------------------------------
function toggle_calculator(player)
local root = get_gui_root(player)
if root and root.calcui then
hide_calculator(player)
else
show_calculator(player)
end
end
-- ----------------------------------------------------------------
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
224
225
226
227
228
229
|
function focus_on_input(player)
local root = get_gui_root(player)
root.calcui.calcui_table.calcui_table_col1.calcui_display.focus()
end
-- ----------------------------------------------------------------
|
3ef575ee
Stefan Wichmann
Initial checkin
|
230
231
232
233
234
235
236
237
238
239
|
function clear_equation(player)
local root = get_gui_root(player)
root.calcui.calcui_table.calcui_table_col1.calcui_display.text = ""
end
-- ----------------------------------------------------------------
function process_ce_key(player, button)
local root = get_gui_root(player)
clear_equation(player)
root.calcui.calcui_table.calcui_table_col2.calcui_display_result.caption = "="
|
1eefa090
Stefan Wichmann
visual overhaul i...
|
240
|
root.calcui.calcui_table.calcui_table_col2.calcui_display_result.tooltip = ""
|
3ef575ee
Stefan Wichmann
Initial checkin
|
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
end
-- ----------------------------------------------------------------
function process_c_key(player, button)
local root = get_gui_root(player)
process_ce_key(player, button)
root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.calcui_result_table.clear()
end
-- ----------------------------------------------------------------
function process_backspace_key(player, button)
local root = get_gui_root(player)
root.calcui.calcui_table.calcui_table_col1.calcui_display.text = string.sub(root.calcui.calcui_table.calcui_table_col1.calcui_display.text, 1, -2)
end
-- ----------------------------------------------------------------
|
1eefa090
Stefan Wichmann
visual overhaul i...
|
257
|
function fix_equation(equation, root)
|
3ef575ee
Stefan Wichmann
Initial checkin
|
258
|
local result = equation
|
1eefa090
Stefan Wichmann
visual overhaul i...
|
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
local prev_result = root.calcui.calcui_table.calcui_table_col2.calcui_display_result.tooltip
-- 1. visible part
-- if equation does not start with a number or char, use the previous result (if available) and put it in front
if not (string.match(result:sub(1, 1), "[^%w%(%)]") == nil) and prev_result then
result = prev_result .. result
end
-- remove "math."
result = result:gsub("math.", "")
local new_equation = result
-- 2. invisible part
|
3ef575ee
Stefan Wichmann
Initial checkin
|
274
275
276
277
278
279
|
-- fix math library shortcuts
local math_lib = {
["abs"] = "math.abs",
["acos"] = "math.acos",
["asin"] = "math.asin",
["atan"] = "math.atan",
|
d7b81613
Stefan Wichmann
Minor fixed - rel...
|
280
|
["atan2"] = "math.atan2",
|
3ef575ee
Stefan Wichmann
Initial checkin
|
281
282
283
|
["ceil"] = "math.ceil",
["floor"] = "math.floor",
["cos"] = "math.cos",
|
d7b81613
Stefan Wichmann
Minor fixed - rel...
|
284
|
["cosh"] = "math.cosh",
|
3ef575ee
Stefan Wichmann
Initial checkin
|
285
|
["sin"] = "math.sin",
|
d7b81613
Stefan Wichmann
Minor fixed - rel...
|
286
|
["sinh"] = "math.sinh",
|
3ef575ee
Stefan Wichmann
Initial checkin
|
287
|
["tan"] = "math.tan",
|
d7b81613
Stefan Wichmann
Minor fixed - rel...
|
288
|
["tanh"] = "math.tanh",
|
3ef575ee
Stefan Wichmann
Initial checkin
|
289
290
|
["deg"] = "math.deg",
["rad"] = "math.rad",
|
4078977a
Stefan Wichmann
Prepare for first...
|
291
292
|
["exp"] = "math.exp",
["log"] = "math.log",
|
d7b81613
Stefan Wichmann
Minor fixed - rel...
|
293
|
["log10"] = "math.log10",
|
3ef575ee
Stefan Wichmann
Initial checkin
|
294
295
296
|
["min"] = "math.min",
["max"] = "math.max",
["modf"] = "math.modf",
|
d7b81613
Stefan Wichmann
Minor fixed - rel...
|
297
298
299
|
["fmod"] = "math.fmod",
["frexp"] = "math.frexp",
["ldexp"] = "math.ldexp",
|
3ef575ee
Stefan Wichmann
Initial checkin
|
300
301
|
["sqrt"] = "math.sqrt",
["huge"] = "math.huge",
|
d7b81613
Stefan Wichmann
Minor fixed - rel...
|
302
303
|
["pi"] = "math.pi",
["pow"] = "math.pow"
|
3ef575ee
Stefan Wichmann
Initial checkin
|
304
305
306
307
308
309
310
311
|
}
for key, val in pairs(math_lib) do
result = result:gsub(key, val)
end
-- fix percentage
result = result:gsub("(%%)", "/100")
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
312
313
|
-- fix danish keyboard
result = result:gsub(",", ".")
|
d7b81613
Stefan Wichmann
Minor fixed - rel...
|
314
|
result = result:gsub(";", ",")
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
315
|
|
1eefa090
Stefan Wichmann
visual overhaul i...
|
316
317
|
return result, new_equation
|
3ef575ee
Stefan Wichmann
Initial checkin
|
318
319
320
321
322
|
end
-- ----------------------------------------------------------------
function process_equal_key(player, button)
local root = get_gui_root(player)
|
3ef575ee
Stefan Wichmann
Initial checkin
|
323
324
|
local original_equation = root.calcui.calcui_table.calcui_table_col1.calcui_display.text;
|
1eefa090
Stefan Wichmann
visual overhaul i...
|
325
|
equation, original_equation = fix_equation(original_equation, root)
|
3ef575ee
Stefan Wichmann
Initial checkin
|
326
327
328
329
330
331
332
333
334
|
-- just testing
--root.calcui.calcui_table.calcui_table_col1.calcui_display.text = equation
if not (equation == nil or equation == "") then
local status, retval = pcall(function()
return load("return " .. equation)()
end)
root.calcui.calcui_table.calcui_table_col2.calcui_display_result.tooltip = retval
|
d7b81613
Stefan Wichmann
Minor fixed - rel...
|
335
336
337
338
339
340
341
342
343
344
345
|
if not (retval == math.huge or retval ~= retval) then
status, retval_show = pcall(function()
local result = string.format("%0." .. settings.get_player_settings(player)["calcui-decimal-places"].value .. "f", retval)
if result:len() > tostring(retval):len() then
result = retval
end
return result
end)
else
status = false
end
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
346
|
if retval_show == nil or retval_show == "" then
|
3ef575ee
Stefan Wichmann
Initial checkin
|
347
348
349
|
status = false
end
if not status then
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
350
|
retval_show = "NaN"
|
3ef575ee
Stefan Wichmann
Initial checkin
|
351
352
353
|
show_rant(player, true)
else
if retval <= 0 then
|
d7b81613
Stefan Wichmann
Minor fixed - rel...
|
354
|
show_think(player, true)
|
3ef575ee
Stefan Wichmann
Initial checkin
|
355
356
357
358
|
else
show_rant(player, false)
end
end
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
359
|
root.calcui.calcui_table.calcui_table_col2.calcui_display_result.caption = "= " .. retval_show
|
3ef575ee
Stefan Wichmann
Initial checkin
|
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
-- only write in recent table if actually a result
if status then
-- check last equation and only insert if not the same
local item_size = #root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.calcui_result_table.children
if item_size == 0 or root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.calcui_result_table.children[item_size-1].caption ~= original_equation then
local recent_equation = root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.calcui_result_table.add({
type = "label",
name = "calcui_recent_equation_" .. item_size,
caption = original_equation,
tooltip = {"calculator-ui.recent_tooltip"}
})
local recent_result = root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.calcui_result_table.add({
type = "label",
name = "calcui_recent_result_" .. item_size,
caption = root.calcui.calcui_table.calcui_table_col2.calcui_display_result.caption
})
root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.scroll_to_bottom()
end
end
end
|
4078977a
Stefan Wichmann
Prepare for first...
|
382
383
384
385
|
if settings.get_player_settings(player)["calcui-clear-on-calc"].value then
clear_equation(player)
end
|
3ef575ee
Stefan Wichmann
Initial checkin
|
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
end
-- ----------------------------------------------------------------
function display_addchar(player, char)
local root = get_gui_root(player)
root.calcui.calcui_table.calcui_table_col1.calcui_display.text = root.calcui.calcui_table.calcui_table_col1.calcui_display.text .. char
show_rant(player, false)
end
-- ----------------------------------------------------------------
local button_dispatch = {
["CE"] = process_ce_key,
["C"] = process_c_key,
["BS"] = process_backspace_key,
--
["EQU"] = process_equal_key
}
local button_addchar = {
["DIV"] = "/",
--
["7"] = "7",
["8"] = "8",
["9"] = "9",
["MUL"] = "*",
--
["4"] = "4",
["5"] = "5",
["6"] = "6",
["SUB"] = "-",
--
["1"] = "1",
["2"] = "2",
["3"] = "3",
["ADD"] = "+",
--
["PERC"] = "%",
["0"] = "0",
["DOT"] = "."
}
function handle_calcui_click(event, player)
debug_print("handle_calcui_click()")
local event_name = event.element.name
local button_prefix = "calcui_button_"
local button_prefix_len = string.len(button_prefix)
local recent_prefix = "calcui_recent_"
local recent_prefix_len = string.len(recent_prefix);
-- calculator buttons
if string.sub(event_name, 1, button_prefix_len) == button_prefix then
show_rant(player, false)
button = string.sub(event_name, button_prefix_len + 1 )
debug_print("handle_calcui_click button " .. button)
local dispatch_func = button_dispatch[button]
if dispatch_func then
dispatch_func(player, button)
end
local addchar = button_addchar[button]
if addchar then
display_addchar(player, addchar)
end
-- close button
elseif event_name == "calcui_close" then
hide_calculator(player)
-- recent results
elseif string.sub(event_name, 1, recent_prefix_len) == recent_prefix then
if event.button == defines.mouse_button_type.left and
event.shift == true then
-- copy equation to display
local root = get_gui_root(player)
root.calcui.calcui_table.calcui_table_col1.calcui_display.text = root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.calcui_result_table[event_name].caption
|
c6cb1d22
Stefan Wichmann
Bugfix 0.18.1 - b...
|
460
|
focus_on_input(player)
|
3ef575ee
Stefan Wichmann
Initial checkin
|
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
end
end
end
-- ----------------------------------------------------------------
function calcui_on_gui_text_changed(event)
if event.element.name == "calcui_display" then
local player = game.players[event.player_index]
local root = get_gui_root(player)
if string.find(root.calcui.calcui_table.calcui_table_col1.calcui_display.text, "=") then
root.calcui.calcui_table.calcui_table_col1.calcui_display.text = root.calcui.calcui_table.calcui_table_col1.calcui_display.text:gsub("=", "")
process_equal_key(player)
end
end
end
-- ----------------------------------------------------------------
function calcui_on_gui_location_changed(event)
if event.element.name == "calcui" then
|
ae0df041
Stefan Wichmann
Fixed multiplayer...
|
480
481
482
483
484
|
local root = get_gui_root(game.players[event.player_index])
if not global.gui_position then
global.gui_position = {}
end
global.gui_position[event.player_index] = event.element.location
|
3ef575ee
Stefan Wichmann
Initial checkin
|
485
|
end
|
4078977a
Stefan Wichmann
Prepare for first...
|
486
|
end
|