Blame view

calculator.lua 20.7 KB
3ef575ee   Stefan Wichmann   Initial checkin
1
2
3
4
5
6
7
  -- calculator.lua
  
  -- ----------------------------------------------------------------
  local function get_gui_root(player)
  	return player.gui.screen
  end
  
d7393909   Stefan Wichmann   preparing release...
8
9
  local nilaus_think = { "calcui_nilaus_really" }
  local nilaus_rant = { "calcui_nilaus_ugghhhh", "utility/cannot_build" }
423dee08   compilatron   preparing release...
10
11
12
13
  
  -- ----------------------------------------------------------------
  local function play_sfx(player, sfx)
  	if settings.get_player_settings(player)["calcui-sfx"].value then
9d6691a6   Stefan Wichmann   Updated to Factor...
14
  		player.play_sound {
423dee08   compilatron   preparing release...
15
16
17
18
19
20
  			path = sfx,
  			volume_modifier = 1.0
  		}
  	end
  end
  
3ef575ee   Stefan Wichmann   Initial checkin
21
  -- ----------------------------------------------------------------
2de48216   Stefan Wichmann   Version 0.18.4 pu...
22
  local function show_think(player, enabled)
d7b81613   Stefan Wichmann   Minor fixed - rel...
23
  	local root = get_gui_root(player)
423dee08   compilatron   preparing release...
24
  	local col2 = root.calcui.calcui_table.calcui_table_col2
2de48216   Stefan Wichmann   Version 0.18.4 pu...
25
  
d7b81613   Stefan Wichmann   Minor fixed - rel...
26
  	if enabled then
423dee08   compilatron   preparing release...
27
28
29
30
31
32
33
  		if settings.get_player_settings(player)["calcui-nilaus-mode"].value then
  			col2.calcui_rant.sprite = "sprite_calcui_think"
  			col2.calcui_scroll_pane.style.height = 196
  			play_sfx(player, nilaus_think[math.random(1, #nilaus_think)])
  		else
  			play_sfx(player, "calcui_home_improvement")
  		end
d7b81613   Stefan Wichmann   Minor fixed - rel...
34
  	else
423dee08   compilatron   preparing release...
35
36
  		col2.calcui_rant.sprite = nil
  		col2.calcui_scroll_pane.style.height = 252
d7b81613   Stefan Wichmann   Minor fixed - rel...
37
38
39
40
  	end
  end
  
  -- ----------------------------------------------------------------
2de48216   Stefan Wichmann   Version 0.18.4 pu...
41
  local function show_rant(player, enabled)
3ef575ee   Stefan Wichmann   Initial checkin
42
  	local root = get_gui_root(player)
423dee08   compilatron   preparing release...
43
  	local col2 = root.calcui.calcui_table.calcui_table_col2
2de48216   Stefan Wichmann   Version 0.18.4 pu...
44
  
3ef575ee   Stefan Wichmann   Initial checkin
45
  	if enabled then
423dee08   compilatron   preparing release...
46
47
48
49
50
51
52
  		if settings.get_player_settings(player)["calcui-nilaus-mode"].value then
  			col2.calcui_rant.sprite = "sprite_calcui_rant"
  			col2.calcui_scroll_pane.style.height = 196
  			play_sfx(player, nilaus_rant[math.random(1, #nilaus_rant)])
  		else
  			play_sfx(player, "utility/cannot_build")
  		end
3ef575ee   Stefan Wichmann   Initial checkin
53
  	else
423dee08   compilatron   preparing release...
54
55
  		col2.calcui_rant.sprite = nil
  		col2.calcui_scroll_pane.style.height = 252
3ef575ee   Stefan Wichmann   Initial checkin
56
57
58
59
60
61
62
63
  	end
  end
  
  -- ----------------------------------------------------------------
  local function destroy_calculator(player)
  	local root = get_gui_root(player)
  	if root.calcui then
  		root.calcui.destroy()
9d6691a6   Stefan Wichmann   Updated to Factor...
64
  		storage.recent_results[player.index] = {}
3ef575ee   Stefan Wichmann   Initial checkin
65
66
67
68
  	end
  end
  
  -- ----------------------------------------------------------------
2de48216   Stefan Wichmann   Version 0.18.4 pu...
69
  local function fix_oob_ui(player)
9d6691a6   Stefan Wichmann   Updated to Factor...
70
71
  	if storage.gui_position[player.index].x < 0 then
  		storage.gui_position[player.index].x = 0
ae0df041   Stefan Wichmann   Fixed multiplayer...
72
  	end
9d6691a6   Stefan Wichmann   Updated to Factor...
73
74
  	if storage.gui_position[player.index].y < 0 then
  		storage.gui_position[player.index].y = 0
ae0df041   Stefan Wichmann   Fixed multiplayer...
75
76
77
78
79
80
  	end
  
  	-- TODO fixed box size, because there is no API call for that
  	local width = 255
  	local height = 350
  
9d6691a6   Stefan Wichmann   Updated to Factor...
81
82
  	if storage.gui_position[player.index].x + width > player.display_resolution.width then
  		storage.gui_position[player.index].x = player.display_resolution.width - width
ae0df041   Stefan Wichmann   Fixed multiplayer...
83
  	end
9d6691a6   Stefan Wichmann   Updated to Factor...
84
85
  	if storage.gui_position[player.index].y + height > player.display_resolution.height then
  		storage.gui_position[player.index].y = player.display_resolution.height - height
ae0df041   Stefan Wichmann   Fixed multiplayer...
86
  	end
ae0df041   Stefan Wichmann   Fixed multiplayer...
87
88
89
  end
  
  -- ----------------------------------------------------------------
3ef575ee   Stefan Wichmann   Initial checkin
90
91
  function show_calculator(player)
  	local root = get_gui_root(player)
3ef575ee   Stefan Wichmann   Initial checkin
92
  
9d6691a6   Stefan Wichmann   Updated to Factor...
93
94
  	if not storage.recent_results then
  		storage.recent_results = {}
423dee08   compilatron   preparing release...
95
  	end
9d6691a6   Stefan Wichmann   Updated to Factor...
96
97
  	if not storage.recent_results[player.index] then
  		storage.recent_results[player.index] = {}
2de48216   Stefan Wichmann   Version 0.18.4 pu...
98
99
  	end
  
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
100
101
  	if not root.calcui then
  		local calcui = root.add({
2de48216   Stefan Wichmann   Version 0.18.4 pu...
102
103
  			type = "frame",
  			name = "calcui",
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
104
105
  			direction = "vertical"
  		})
2de48216   Stefan Wichmann   Version 0.18.4 pu...
106
  
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
107
  		local flow = calcui.add({
2de48216   Stefan Wichmann   Version 0.18.4 pu...
108
  			type = "flow",
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
109
110
  			name = "calcui_flow"
  		})
9d6691a6   Stefan Wichmann   Updated to Factor...
111
  		flow.style.horizontally_stretchable = true
2de48216   Stefan Wichmann   Version 0.18.4 pu...
112
  
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
113
  		flow.add({
2de48216   Stefan Wichmann   Version 0.18.4 pu...
114
115
  			type = "label",
  			name = "calcui_title",
9d6691a6   Stefan Wichmann   Updated to Factor...
116
  			caption = { "calculator-ui.title" },
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
117
118
  			style = "frame_title"
  		}).drag_target = calcui
2de48216   Stefan Wichmann   Version 0.18.4 pu...
119
  
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
120
121
122
123
124
125
  		local widget = flow.add({
  			type = "empty-widget",
  			style = "draggable_space_header",
  			name = "calcui_drag"
  		})
  		widget.drag_target = calcui
9d6691a6   Stefan Wichmann   Updated to Factor...
126
  		widget.style.horizontally_stretchable = true
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
127
128
  		widget.style.minimal_width = 24
  		widget.style.natural_height = 24
2de48216   Stefan Wichmann   Version 0.18.4 pu...
129
  
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
130
  		flow.add({
2de48216   Stefan Wichmann   Version 0.18.4 pu...
131
  			type = "sprite-button",
9d6691a6   Stefan Wichmann   Updated to Factor...
132
  			sprite = "utility/close",
2de48216   Stefan Wichmann   Version 0.18.4 pu...
133
  			style = "frame_action_button",
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
134
135
  			name = "calcui_close"
  		})
2de48216   Stefan Wichmann   Version 0.18.4 pu...
136
  
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
137
  		local table = calcui.add({
2de48216   Stefan Wichmann   Version 0.18.4 pu...
138
139
140
  			type = "table",
  			name = "calcui_table",
  			column_count = "2",
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
141
142
  			vertical_centering = "false"
  		})
2de48216   Stefan Wichmann   Version 0.18.4 pu...
143
  
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
144
  		local col1 = table.add({
2de48216   Stefan Wichmann   Version 0.18.4 pu...
145
146
  			type = "flow",
  			name = "calcui_table_col1",
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
147
148
  			direction = "vertical"
  		})
2de48216   Stefan Wichmann   Version 0.18.4 pu...
149
  
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
150
  		local display = col1.add({
2de48216   Stefan Wichmann   Version 0.18.4 pu...
151
152
  			type = "textfield",
  			caption = "",
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
153
154
  			name = "calcui_display"
  		})
1eefa090   Stefan Wichmann   visual overhaul i...
155
  		display.style.width = 212
3ef575ee   Stefan Wichmann   Initial checkin
156
  
9d6691a6   Stefan Wichmann   Updated to Factor...
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
  		local row1 = col1.add({ type = "flow", name = "calcui_col1_row1", direction = "horizontal" })
  		row1.add({
  			type = "sprite-button",
  			style = "calcui_button_style_light",
  			caption = "CE",
  			tooltip = { "calculator-ui.button_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", tooltip = { "calculator-ui.button_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 = "", tooltip = { "calculator-ui.button_BS" }, name = "calcui_button_BS" }).sprite =
  		"sprite_calcui_backspace"
  		row1.add({ type = "sprite-button", style = "calcui_button_style_light", caption = "/", tooltip = { "calculator-ui.button_DIV" }, name = "calcui_button_DIV" }).sprite =
  		"sprite_calcui_light"
  
  		local row2 = col1.add({ type = "flow", name = "calcui_col1_row2", direction = "horizontal" })
  		row2.add({ type = "sprite-button", style = "calcui_button_style_dark", caption = "7", tooltip = { "calculator-ui.button_7" }, name = "calcui_button_7" }).sprite =
  		"sprite_calcui_dark"
  		row2.add({ type = "sprite-button", style = "calcui_button_style_dark", caption = "8", tooltip = { "calculator-ui.button_8" }, name = "calcui_button_8" }).sprite =
  		"sprite_calcui_dark"
  		row2.add({ type = "sprite-button", style = "calcui_button_style_dark", caption = "9", tooltip = { "calculator-ui.button_9" }, name = "calcui_button_9" }).sprite =
  		"sprite_calcui_dark"
  		row2.add({ type = "sprite-button", style = "calcui_button_style_light", caption = "*", tooltip = { "calculator-ui.button_MUL" }, name = "calcui_button_MUL" }).sprite =
  		"sprite_calcui_light"
  
  		local row3 = col1.add({ type = "flow", name = "calcui_col1_row3", direction = "horizontal" })
  		row3.add({ type = "sprite-button", style = "calcui_button_style_dark", caption = "4", tooltip = { "calculator-ui.button_4" }, name = "calcui_button_4" }).sprite =
  		"sprite_calcui_dark"
  		row3.add({ type = "sprite-button", style = "calcui_button_style_dark", caption = "5", tooltip = { "calculator-ui.button_5" }, name = "calcui_button_5" }).sprite =
  		"sprite_calcui_dark"
  		row3.add({ type = "sprite-button", style = "calcui_button_style_dark", caption = "6", tooltip = { "calculator-ui.button_6" }, name = "calcui_button_6" }).sprite =
  		"sprite_calcui_dark"
  		row3.add({ type = "sprite-button", style = "calcui_button_style_light", caption = "-", tooltip = { "calculator-ui.button_SUB" }, name = "calcui_button_SUB" }).sprite =
  		"sprite_calcui_light"
  
  		local row4 = col1.add({ type = "flow", name = "calcui_col1_row4", direction = "horizontal" })
  		row4.add({ type = "sprite-button", style = "calcui_button_style_dark", caption = "1", tooltip = { "calculator-ui.button_1" }, name = "calcui_button_1" }).sprite =
  		"sprite_calcui_dark"
  		row4.add({ type = "sprite-button", style = "calcui_button_style_dark", caption = "2", tooltip = { "calculator-ui.button_2" }, name = "calcui_button_2" }).sprite =
  		"sprite_calcui_dark"
  		row4.add({ type = "sprite-button", style = "calcui_button_style_dark", caption = "3", tooltip = { "calculator-ui.button_3" }, name = "calcui_button_3" }).sprite =
  		"sprite_calcui_dark"
  		row4.add({ type = "sprite-button", style = "calcui_button_style_light", caption = "+", tooltip = { "calculator-ui.button_ADD" }, name = "calcui_button_ADD" }).sprite =
  		"sprite_calcui_light"
  
  		local row5 = col1.add({ type = "flow", name = "calcui_col1_row5", direction = "horizontal" })
  		row5.add({ type = "sprite-button", style = "calcui_button_style_light", caption = "%", tooltip = { "calculator-ui.button_PERC" }, name = "calcui_button_PERC" }).sprite =
  		"sprite_calcui_light"
  		row5.add({ type = "sprite-button", style = "calcui_button_style_dark", caption = "0", tooltip = { "calculator-ui.button_0" }, name = "calcui_button_0" }).sprite =
  		"sprite_calcui_dark"
  		row5.add({ type = "sprite-button", style = "calcui_button_style_dark", caption = ".", tooltip = { "calculator-ui.button_DOT" }, name = "calcui_button_DOT" }).sprite =
  		"sprite_calcui_dark"
  		row5.add({ type = "sprite-button", style = "calcui_button_style_red", caption = "=", tooltip = { "calculator-ui.button_EQU" }, name = "calcui_button_EQU" }).sprite =
  		"sprite_calcui_red"
3ef575ee   Stefan Wichmann   Initial checkin
212
  
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
213
  		local col2 = table.add({
2de48216   Stefan Wichmann   Version 0.18.4 pu...
214
215
  			type = "flow",
  			name = "calcui_table_col2",
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
216
217
218
219
  			direction = "vertical"
  		})
  
  		local result = col2.add({
2de48216   Stefan Wichmann   Version 0.18.4 pu...
220
221
222
  			type = "flow",
  			name = "calcui_result",
  			direction = "horizontal"
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
223
  		})
2de48216   Stefan Wichmann   Version 0.18.4 pu...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  
  		result.add({
  			type = "label",
  			caption = "=",
  			name = "calcui_display_sign"
  		}).style.font = "default-large"
  
  		result.add({
  			type = "label",
  			caption = "",
  			name = "calcui_copy_display_result"
  		}).style.font = "default-large"
  
  		col2.add({
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
238
239
240
  			type = "sprite",
  			name = "calcui_rant"
  		})
2de48216   Stefan Wichmann   Version 0.18.4 pu...
241
  
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
242
  		col2.add({
2de48216   Stefan Wichmann   Version 0.18.4 pu...
243
244
  			type = "line",
  			name = "calcui_line",
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
245
246
  			direction = "horizontal"
  		})
2de48216   Stefan Wichmann   Version 0.18.4 pu...
247
  
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
248
249
250
251
252
  		local scroll = col2.add({
  			type = "scroll-pane",
  			name = "calcui_scroll_pane"
  		})
  		scroll.style.height = 252
2de48216   Stefan Wichmann   Version 0.18.4 pu...
253
254
255
256
257
258
259
260
261
  
  		scroll.add({
  			type = "table",
  			caption = "",
  			name = "calcui_result_table",
  			column_count = "3"
  		}).style.column_alignments[1] = "right"
  
  
ae0df041   Stefan Wichmann   Fixed multiplayer...
262
  		-- use last saved location or center the gui
9d6691a6   Stefan Wichmann   Updated to Factor...
263
264
  		if not storage.gui_position then
  			storage.gui_position = {}
ae0df041   Stefan Wichmann   Fixed multiplayer...
265
  		end
9d6691a6   Stefan Wichmann   Updated to Factor...
266
  		if storage.gui_position[player.index] then
ae0df041   Stefan Wichmann   Fixed multiplayer...
267
268
  			-- fix weird saved positions (out of reach)
  			fix_oob_ui(player)
2de48216   Stefan Wichmann   Version 0.18.4 pu...
269
  
9d6691a6   Stefan Wichmann   Updated to Factor...
270
  			calcui.location = storage.gui_position[player.index]
ae0df041   Stefan Wichmann   Fixed multiplayer...
271
272
273
  		else
  			calcui.force_auto_center()
  		end
2de48216   Stefan Wichmann   Version 0.18.4 pu...
274
275
276
  
  		-- focus on display
  		display.focus()
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
277
  	end
3ef575ee   Stefan Wichmann   Initial checkin
278
279
280
  end
  
  -- ----------------------------------------------------------------
2de48216   Stefan Wichmann   Version 0.18.4 pu...
281
  local function hide_calculator(player)
3ef575ee   Stefan Wichmann   Initial checkin
282
283
284
285
  	destroy_calculator(player)
  end
  
  -- ----------------------------------------------------------------
3371d413   Stefan Wichmann   New setting for c...
286
  function focus_on_input(player)
3ef575ee   Stefan Wichmann   Initial checkin
287
  	local root = get_gui_root(player)
3371d413   Stefan Wichmann   New setting for c...
288
  	root.calcui.calcui_table.calcui_table_col1.calcui_display.focus()
3ef575ee   Stefan Wichmann   Initial checkin
289
290
291
  end
  
  -- ----------------------------------------------------------------
3371d413   Stefan Wichmann   New setting for c...
292
293
294
  function toggle_calculator(player, extended_mode)
  	extended_mode = extended_mode or false
  
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
295
  	local root = get_gui_root(player)
3371d413   Stefan Wichmann   New setting for c...
296
297
298
299
300
301
302
303
304
305
  	if root and root.calcui then
  		-- root.calcui.calcui_table.calcui_table_col1.calcui_display.in_focus()? FIXME not possible with current api
  		if extended_mode and not settings.get_player_settings(player)["calcui-shortcut-close"].value then
  			focus_on_input(player)
  		else
  			hide_calculator(player)
  		end
  	else
  		show_calculator(player)
  	end
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
306
307
308
  end
  
  -- ----------------------------------------------------------------
2de48216   Stefan Wichmann   Version 0.18.4 pu...
309
  local function clear_equation(player)
3ef575ee   Stefan Wichmann   Initial checkin
310
311
312
313
314
  	local root = get_gui_root(player)
  	root.calcui.calcui_table.calcui_table_col1.calcui_display.text = ""
  end
  
  -- ----------------------------------------------------------------
2de48216   Stefan Wichmann   Version 0.18.4 pu...
315
  local function process_ce_key(player, button)
3ef575ee   Stefan Wichmann   Initial checkin
316
317
  	local root = get_gui_root(player)
  	clear_equation(player)
423dee08   compilatron   preparing release...
318
319
320
  	local result = root.calcui.calcui_table.calcui_table_col2.calcui_result.calcui_copy_display_result
  	result.caption = ""
  	result.tooltip = ""
3ef575ee   Stefan Wichmann   Initial checkin
321
322
323
  end
  
  -- ----------------------------------------------------------------
2de48216   Stefan Wichmann   Version 0.18.4 pu...
324
  local function process_c_key(player, button)
3ef575ee   Stefan Wichmann   Initial checkin
325
326
327
  	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()
9d6691a6   Stefan Wichmann   Updated to Factor...
328
  	storage.recent_results[player.index] = {}
3ef575ee   Stefan Wichmann   Initial checkin
329
330
331
  end
  
  -- ----------------------------------------------------------------
2de48216   Stefan Wichmann   Version 0.18.4 pu...
332
  local function process_backspace_key(player, button)
3ef575ee   Stefan Wichmann   Initial checkin
333
  	local root = get_gui_root(player)
423dee08   compilatron   preparing release...
334
335
  	local display = root.calcui.calcui_table.calcui_table_col1.calcui_display
  	display.text = string.sub(display.text, 1, -2)
3ef575ee   Stefan Wichmann   Initial checkin
336
337
338
  end
  
  -- ----------------------------------------------------------------
2de48216   Stefan Wichmann   Version 0.18.4 pu...
339
340
341
  local function draw_recent_table(player)
  	local root = get_gui_root(player)
  
423dee08   compilatron   preparing release...
342
343
  	local recent = root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane
  
2de48216   Stefan Wichmann   Version 0.18.4 pu...
344
  	-- drop old table
423dee08   compilatron   preparing release...
345
  	recent.calcui_result_table.clear()
2de48216   Stefan Wichmann   Version 0.18.4 pu...
346
  
9d6691a6   Stefan Wichmann   Updated to Factor...
347
  	for i, result in ipairs(storage.recent_results[player.index]) do
423dee08   compilatron   preparing release...
348
  		recent.calcui_result_table.add({
2de48216   Stefan Wichmann   Version 0.18.4 pu...
349
350
351
  			type = "label",
  			name = "calcui_copy_equation_" .. i,
  			caption = result["equation"],
9d6691a6   Stefan Wichmann   Updated to Factor...
352
  			tooltip = { "calculator-ui.recent_tooltip" }
2de48216   Stefan Wichmann   Version 0.18.4 pu...
353
  		})
423dee08   compilatron   preparing release...
354
  		recent.calcui_result_table.add({
2de48216   Stefan Wichmann   Version 0.18.4 pu...
355
356
357
358
  			type = "label",
  			name = "calcui_sign_" .. i,
  			caption = "="
  		})
423dee08   compilatron   preparing release...
359
  		recent.calcui_result_table.add({
2de48216   Stefan Wichmann   Version 0.18.4 pu...
360
361
362
363
364
  			type = "label",
  			name = "calcui_copy_result_" .. i,
  			caption = result["result"]
  		})
  	end
423dee08   compilatron   preparing release...
365
  	recent.scroll_to_top()
2de48216   Stefan Wichmann   Version 0.18.4 pu...
366
367
368
  end
  
  -- ----------------------------------------------------------------
423dee08   compilatron   preparing release...
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
  local math_lib = {
  	["abs"]   = "math.abs",
  	["acos"]  = "math.acos",
  	["asin"]  = "math.asin",
  	["atan"]  = "math.atan",
  	["atan2"] = "math.atan2",
  	["ceil"]  = "math.ceil",
  	["floor"] = "math.floor",
  	["cos"]   = "math.cos",
  	["cosh"]  = "math.cosh",
  	["sin"]   = "math.sin",
  	["sinh"]  = "math.sinh",
  	["tan"]   = "math.tan",
  	["tanh"]  = "math.tanh",
  	["deg"]   = "math.deg",
  	["rad"]   = "math.rad",
  	["exp"]   = "math.exp",
  	["log"]   = "math.log",
  	["log10"] = "math.log10",
  	["min"]   = "math.min",
  	["max"]   = "math.max",
  	["modf"]  = "math.modf",
  	["fmod"]  = "math.fmod",
  	["frexp"] = "math.frexp",
  	["ldexp"] = "math.ldexp",
  	["sqrt"]  = "math.sqrt",
  	["huge"]  = "math.huge",
  	["pi"]    = "math.pi",
  	["pow"]   = "math.pow"
  }
2de48216   Stefan Wichmann   Version 0.18.4 pu...
399
  local function fix_equation(equation, root)
3ef575ee   Stefan Wichmann   Initial checkin
400
  	local result = equation
2de48216   Stefan Wichmann   Version 0.18.4 pu...
401
  	local prev_result = root.calcui.calcui_table.calcui_table_col2.calcui_result.calcui_copy_display_result.tooltip
1eefa090   Stefan Wichmann   visual overhaul i...
402
403
404
405
406
407
408
409
410
411
412
413
414
415
  
  
  	-- 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
416
  	-- fix math library shortcuts
3ef575ee   Stefan Wichmann   Initial checkin
417
418
419
420
421
  	for key, val in pairs(math_lib) do
  		result = result:gsub(key, val)
  	end
  
  	-- fix percentage
d7393909   Stefan Wichmann   preparing release...
422
  	-- complex equations like "20+10%" = 22, before it was 20.1 -- big thanks to GWulf
9d6691a6   Stefan Wichmann   Updated to Factor...
423
  	result = result:gsub("(%d+)(.)(%d+)%%", function(base, sign, perc)
d7393909   Stefan Wichmann   preparing release...
424
425
426
  		if sign == "+" then
  			return base .. "*1." .. perc
  		elseif sign == "-" then
9d6691a6   Stefan Wichmann   Updated to Factor...
427
  			return base .. "*(1-0." .. perc .. ")"
d7393909   Stefan Wichmann   preparing release...
428
429
430
431
432
433
434
  		elseif sign == "*" then
  			return "(" .. base .. "/100)*" .. perc
  		elseif sign == "/" then
  			return "(" .. base .. "*100)/" .. perc
  		end
  	end)
  	-- still the simple equation needs some work: 1% = 0.01
3ef575ee   Stefan Wichmann   Initial checkin
435
  	result = result:gsub("(%%)", "/100")
2de48216   Stefan Wichmann   Version 0.18.4 pu...
436
  
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
437
438
  	-- fix danish keyboard
  	result = result:gsub(",", ".")
d7b81613   Stefan Wichmann   Minor fixed - rel...
439
  	result = result:gsub(";", ",")
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
440
  
9d6691a6   Stefan Wichmann   Updated to Factor...
441
442
  	-- remove thousand separators
  	result = result:gsub("'", "");
1eefa090   Stefan Wichmann   visual overhaul i...
443
444
  
  	return result, new_equation
3ef575ee   Stefan Wichmann   Initial checkin
445
446
447
  end
  
  -- ----------------------------------------------------------------
9d6691a6   Stefan Wichmann   Updated to Factor...
448
449
450
451
452
453
454
455
456
457
458
459
460
  function add_thousand_separator(result)
  	local left, num, right = string.match(result, '^([^%d]*%d)(%d*)(.-)$')
  	num = num:reverse():gsub("(%d%d%d)", "%1'"):reverse()
  	return left .. num .. right
  end
  
  -- ----------------------------------------------------------------
  function is_scientific_notation(result)
  	-- Check if the number is in scientific notation
  	return string.match(result, "^[-+]?%d*%.?%d+e[-+]?%d+$")
  end
  
  -- ----------------------------------------------------------------
3ef575ee   Stefan Wichmann   Initial checkin
461
462
  function process_equal_key(player, button)
  	local root = get_gui_root(player)
3ef575ee   Stefan Wichmann   Initial checkin
463
  	local original_equation = root.calcui.calcui_table.calcui_table_col1.calcui_display.text;
2de48216   Stefan Wichmann   Version 0.18.4 pu...
464
  
1eefa090   Stefan Wichmann   visual overhaul i...
465
  	equation, original_equation = fix_equation(original_equation, root)
2de48216   Stefan Wichmann   Version 0.18.4 pu...
466
  
3ef575ee   Stefan Wichmann   Initial checkin
467
468
  	-- just testing
  	--root.calcui.calcui_table.calcui_table_col1.calcui_display.text = equation
2de48216   Stefan Wichmann   Version 0.18.4 pu...
469
  
3ef575ee   Stefan Wichmann   Initial checkin
470
471
472
473
  	if not (equation == nil or equation == "") then
  		local status, retval = pcall(function()
  			return load("return " .. equation)()
  		end)
2de48216   Stefan Wichmann   Version 0.18.4 pu...
474
  		root.calcui.calcui_table.calcui_table_col2.calcui_result.calcui_copy_display_result.tooltip = retval
9d6691a6   Stefan Wichmann   Updated to Factor...
475
  
d7b81613   Stefan Wichmann   Minor fixed - rel...
476
477
  		if not (retval == math.huge or retval ~= retval) then
  			status, retval_show = pcall(function()
9d6691a6   Stefan Wichmann   Updated to Factor...
478
479
480
  				-- Check if the result is in scientific notation
  				if is_scientific_notation(retval) then
  					return retval
d7b81613   Stefan Wichmann   Minor fixed - rel...
481
  				end
9d6691a6   Stefan Wichmann   Updated to Factor...
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
  
  				local result = string.format(
  					"%0." .. settings.get_player_settings(player)["calcui-decimal-places"].value .. "f", retval)
  
  
  				-- Add thousand separators
  				result = add_thousand_separator(result)
  
  				-- Check if extra decimal places need to be removed
  				local trimmed_result = tostring(retval)
  				if string.find(trimmed_result, '%.') then
  					local int, frac = trimmed_result:match("(%d+)%.(%d+)")
  					if frac then
  						local truncated_result = int ..
  							"." .. frac:sub(1, settings.get_player_settings(player)["calcui-decimal-places"].value)
  						result = add_thousand_separator(truncated_result)
  					end
  				else
  					result = add_thousand_separator(trimmed_result)
  				end
  
d7b81613   Stefan Wichmann   Minor fixed - rel...
503
504
505
506
507
  				return result
  			end)
  		else
  			status = false
  		end
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
508
  		if retval_show == nil or retval_show == "" then
3ef575ee   Stefan Wichmann   Initial checkin
509
510
511
  			status = false
  		end
  		if not status then
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
512
  			retval_show = "NaN"
3ef575ee   Stefan Wichmann   Initial checkin
513
514
515
  			show_rant(player, true)
  		else
  			if retval <= 0 then
d7b81613   Stefan Wichmann   Minor fixed - rel...
516
  				show_think(player, true)
2de48216   Stefan Wichmann   Version 0.18.4 pu...
517
  			else
3ef575ee   Stefan Wichmann   Initial checkin
518
519
520
  				show_rant(player, false)
  			end
  		end
2de48216   Stefan Wichmann   Version 0.18.4 pu...
521
522
  		root.calcui.calcui_table.calcui_table_col2.calcui_result.calcui_copy_display_result.caption = retval_show
  
3ef575ee   Stefan Wichmann   Initial checkin
523
524
  		-- only write in recent table if actually a result
  		if status then
2de48216   Stefan Wichmann   Version 0.18.4 pu...
525
  			-- check first equation and only insert if not the same
9d6691a6   Stefan Wichmann   Updated to Factor...
526
527
  			if #storage.recent_results[player.index] == 0 or storage.recent_results[player.index][1]["equation"] ~= original_equation then
  				table.insert(storage.recent_results[player.index], 1, {
2de48216   Stefan Wichmann   Version 0.18.4 pu...
528
529
  					equation = original_equation,
  					result = retval_show
3ef575ee   Stefan Wichmann   Initial checkin
530
  				})
3ef575ee   Stefan Wichmann   Initial checkin
531
  			end
2de48216   Stefan Wichmann   Version 0.18.4 pu...
532
  			draw_recent_table(player)
3ef575ee   Stefan Wichmann   Initial checkin
533
534
  		end
  	end
4078977a   Stefan Wichmann   Prepare for first...
535
536
537
538
  
  	if settings.get_player_settings(player)["calcui-clear-on-calc"].value then
  		clear_equation(player)
  	end
3ef575ee   Stefan Wichmann   Initial checkin
539
540
541
  end
  
  -- ----------------------------------------------------------------
2de48216   Stefan Wichmann   Version 0.18.4 pu...
542
  local function display_addchar(player, char)
3ef575ee   Stefan Wichmann   Initial checkin
543
  	local root = get_gui_root(player)
423dee08   compilatron   preparing release...
544
545
  	local display = root.calcui.calcui_table.calcui_table_col1.calcui_display
  	display.text = display.text .. char
3ef575ee   Stefan Wichmann   Initial checkin
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
  	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)
2de48216   Stefan Wichmann   Version 0.18.4 pu...
585
586
587
588
  
  	local copy_prefix = "calcui_copy_"
  	local copy_prefix_len = string.len(copy_prefix);
  
3ef575ee   Stefan Wichmann   Initial checkin
589
590
591
592
  	-- calculator buttons
  	if string.sub(event_name, 1, button_prefix_len) == button_prefix then
  		show_rant(player, false)
  
9d6691a6   Stefan Wichmann   Updated to Factor...
593
  		button = string.sub(event_name, button_prefix_len + 1)
3ef575ee   Stefan Wichmann   Initial checkin
594
595
596
597
598
  		debug_print("handle_calcui_click button " .. button)
  		local dispatch_func = button_dispatch[button]
  		if dispatch_func then
  			dispatch_func(player, button)
  		end
2de48216   Stefan Wichmann   Version 0.18.4 pu...
599
  
3ef575ee   Stefan Wichmann   Initial checkin
600
601
602
603
  		local addchar = button_addchar[button]
  		if addchar then
  			display_addchar(player, addchar)
  		end
9d6691a6   Stefan Wichmann   Updated to Factor...
604
  		-- close button
3ef575ee   Stefan Wichmann   Initial checkin
605
606
  	elseif event_name == "calcui_close" then
  		hide_calculator(player)
9d6691a6   Stefan Wichmann   Updated to Factor...
607
  		-- copy results
2de48216   Stefan Wichmann   Version 0.18.4 pu...
608
609
  	elseif string.sub(event_name, 1, copy_prefix_len) == copy_prefix then
  		if event.button == defines.mouse_button_type.left and
9d6691a6   Stefan Wichmann   Updated to Factor...
610
  			event.shift == true then
2de48216   Stefan Wichmann   Version 0.18.4 pu...
611
  			-- copy equation or result to display
3ef575ee   Stefan Wichmann   Initial checkin
612
  			local root = get_gui_root(player)
2de48216   Stefan Wichmann   Version 0.18.4 pu...
613
  			if event_name == "calcui_copy_display_result" then
9d6691a6   Stefan Wichmann   Updated to Factor...
614
615
  				root.calcui.calcui_table.calcui_table_col1.calcui_display.text = root.calcui.calcui_table
  					.calcui_table_col2.calcui_result.calcui_copy_display_result.caption
2de48216   Stefan Wichmann   Version 0.18.4 pu...
616
  			else
9d6691a6   Stefan Wichmann   Updated to Factor...
617
618
  				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
2de48216   Stefan Wichmann   Version 0.18.4 pu...
619
  			end
3ef575ee   Stefan Wichmann   Initial checkin
620
  		end
423dee08   compilatron   preparing release...
621
  		focus_on_input(player)
9d6691a6   Stefan Wichmann   Updated to Factor...
622
  		-- if else focus on focus on display
2de48216   Stefan Wichmann   Version 0.18.4 pu...
623
624
  	else
  		focus_on_input(player)
3ef575ee   Stefan Wichmann   Initial checkin
625
626
627
628
629
630
631
632
633
  	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
9d6691a6   Stefan Wichmann   Updated to Factor...
634
635
  			root.calcui.calcui_table.calcui_table_col1.calcui_display.text = root.calcui.calcui_table.calcui_table_col1
  				.calcui_display.text:gsub("=", "")
3ef575ee   Stefan Wichmann   Initial checkin
636
637
638
639
640
641
642
643
  			process_equal_key(player)
  		end
  	end
  end
  
  -- ----------------------------------------------------------------
  function calcui_on_gui_location_changed(event)
  	if event.element.name == "calcui" then
9d6691a6   Stefan Wichmann   Updated to Factor...
644
645
  		if not storage.gui_position then
  			storage.gui_position = {}
ae0df041   Stefan Wichmann   Fixed multiplayer...
646
  		end
9d6691a6   Stefan Wichmann   Updated to Factor...
647
  		storage.gui_position[event.player_index] = event.element.location
3ef575ee   Stefan Wichmann   Initial checkin
648
  	end
9d6691a6   Stefan Wichmann   Updated to Factor...
649
  end