diff --git a/CHANGELOG.md b/CHANGELOG.md index 9317d24..ade8312 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,14 +8,22 @@ and this project adheres to the versioning of Factorio, so 0.18.x will be at lea ## [0.18.4] - 2020-06-07 ### General -Based onyuser feedback updated the mod +Based on user feedback updated the mod + +### Added +- Added the possibility to copy the results (from recent and also the current) to the display ### Changed - Window now remember last location when opened +- Updated the BS button (it's backspace people...) so it now has a nice icon instead of the letters +- Recent results now add to the top, instead of the bottom +- When calculator is opened it focues on the display for entering the equation +- You can click pretty much anywhere now and it focues on the display for entering the equation ### Fixed - On multiplayer session the window would lag behind while dragging, because the location would be saved on server. This isn't going to happen anymore, hopefully (not tested yet). - When window is moved out of boundaries, when reopened, it will correct itself to be fully in sight again. +- Settings had weird default values (were set 2 times) ## [0.18.3] - 2020-06-07 ### General diff --git a/calcui-styles.lua b/calcui-styles.lua index 970889e..d7c1c8d 100644 --- a/calcui-styles.lua +++ b/calcui-styles.lua @@ -53,6 +53,13 @@ data:extend({ }, { type = "sprite", + name = "sprite_calcui_backspace", + filename = "__calculator-ui__/graphics/backspace.png", + width = 50, + height = 50 + }, + { + type = "sprite", name = "sprite_calcui_red", filename = "__calculator-ui__/graphics/red.png", width = 50, diff --git a/calculator.lua b/calculator.lua index aa5f15f..f9c166c 100644 --- a/calculator.lua +++ b/calculator.lua @@ -6,9 +6,9 @@ local function get_gui_root(player) end -- ---------------------------------------------------------------- -function show_think(player, enabled) +local 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 @@ -19,9 +19,9 @@ function show_think(player, enabled) end -- ---------------------------------------------------------------- -function show_rant(player, enabled) +local 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 @@ -40,7 +40,7 @@ local function destroy_calculator(player) end -- ---------------------------------------------------------------- -function fix_oob_ui(player) +local function fix_oob_ui(player) if global.gui_position[player.index].x < 0 then global.gui_position[player.index].x = 0 end @@ -58,33 +58,40 @@ function fix_oob_ui(player) 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 -- ---------------------------------------------------------------- function show_calculator(player) local root = get_gui_root(player) + if not global.recent_results then + global.recent_results = {} + if not global.recent_results[player.index] then + global.recent_results[player.index] = {} + end + end + if not root.calcui then local calcui = root.add({ - type = "frame", - name = "calcui", + type = "frame", + name = "calcui", style = "dialog_frame", direction = "vertical" }) - + local flow = calcui.add({ - type = "flow", + type = "flow", name = "calcui_flow" }) flow.style.horizontally_stretchable = "on" - + flow.add({ - type = "label", - caption = {"calculator-ui.title"}, + type = "label", + name = "calcui_title", + caption = {"calculator-ui.title"}, style = "frame_title" }).drag_target = calcui - + local widget = flow.add({ type = "empty-widget", style = "draggable_space_header", @@ -94,30 +101,30 @@ function show_calculator(player) 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", + 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", + type = "table", + name = "calcui_table", + column_count = "2", vertical_centering = "false" }) - + local col1 = table.add({ - type = "flow", - name = "calcui_table_col1", + type = "flow", + name = "calcui_table_col1", direction = "vertical" }) - + local display = col1.add({ - type = "textfield", - caption = "", + type = "textfield", + caption = "", name = "calcui_display" }) display.style.width = 212 @@ -125,7 +132,7 @@ function show_calculator(player) local row1 = col1.add({type="flow", name="calcui_col1_row1", direction="horizontal"}) 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_BS"}).sprite = "sprite_calcui_backspace" row1.add({type="sprite-button", style="calcui_button_style_light", caption="/", name="calcui_button_DIV"}).sprite = "sprite_calcui_light" local row2 = col1.add({type="flow", name="calcui_col1_row2", direction="horizontal"}) @@ -153,43 +160,54 @@ function show_calculator(player) row5.add({type="sprite-button", style="calcui_button_style_red", caption="=", name="calcui_button_EQU"}).sprite = "sprite_calcui_red" local col2 = table.add({ - type = "flow", - name = "calcui_table_col2", + type = "flow", + name = "calcui_table_col2", direction = "vertical" }) local result = col2.add({ - type = "label", - caption = "= ", - name = "calcui_display_result" + type = "flow", + name = "calcui_result", + direction = "horizontal" }) - result.style.font = "default-large" - - local rant = col2.add({ + + 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({ type = "sprite", name = "calcui_rant" }) - + col2.add({ - type = "line", + type = "line", + name = "calcui_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" - - + + scroll.add({ + type = "table", + caption = "", + name = "calcui_result_table", + column_count = "3" + }).style.column_alignments[1] = "right" + + -- use last saved location or center the gui if not global.gui_position then global.gui_position = {} @@ -197,16 +215,19 @@ function show_calculator(player) 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 + + -- focus on display + display.focus() end end -- ---------------------------------------------------------------- -function hide_calculator(player) +local function hide_calculator(player) destroy_calculator(player) end @@ -227,36 +248,65 @@ function focus_on_input(player) end -- ---------------------------------------------------------------- -function clear_equation(player) +local 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 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 = "=" - root.calcui.calcui_table.calcui_table_col2.calcui_display_result.tooltip = "" + root.calcui.calcui_table.calcui_table_col2.calcui_result.calcui_copy_display_result.caption = "" + root.calcui.calcui_table.calcui_table_col2.calcui_result.calcui_copy_display_result.tooltip = "" end -- ---------------------------------------------------------------- -function process_c_key(player, button) +local 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() + global.recent_results[player.index] = {} end -- ---------------------------------------------------------------- -function process_backspace_key(player, button) +local 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 -- ---------------------------------------------------------------- -function fix_equation(equation, root) +local function draw_recent_table(player) + local root = get_gui_root(player) + + -- drop old table + root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.calcui_result_table.clear() + + for i, result in ipairs(global.recent_results[player.index]) do + root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.calcui_result_table.add({ + type = "label", + name = "calcui_copy_equation_" .. i, + caption = result["equation"], + tooltip = {"calculator-ui.recent_tooltip"} + }) + root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.calcui_result_table.add({ + type = "label", + name = "calcui_sign_" .. i, + caption = "=" + }) + root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.calcui_result_table.add({ + type = "label", + name = "calcui_copy_result_" .. i, + caption = result["result"] + }) + end + root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.scroll_to_top() +end + +-- ---------------------------------------------------------------- +local function fix_equation(equation, root) local result = equation - local prev_result = root.calcui.calcui_table.calcui_table_col2.calcui_display_result.tooltip + local prev_result = root.calcui.calcui_table.calcui_table_col2.calcui_result.calcui_copy_display_result.tooltip -- 1. visible part @@ -308,7 +358,7 @@ function fix_equation(equation, root) -- fix percentage result = result:gsub("(%%)", "/100") - + -- fix danish keyboard result = result:gsub(",", ".") result = result:gsub(";", ",") @@ -321,17 +371,17 @@ end function process_equal_key(player, button) local root = get_gui_root(player) local original_equation = root.calcui.calcui_table.calcui_table_col1.calcui_display.text; - + equation, original_equation = fix_equation(original_equation, root) - + -- 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 + root.calcui.calcui_table.calcui_table_col2.calcui_result.calcui_copy_display_result.tooltip = retval 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) @@ -352,31 +402,22 @@ function process_equal_key(player, button) else if retval <= 0 then show_think(player, true) - else + else show_rant(player, false) end end - root.calcui.calcui_table.calcui_table_col2.calcui_display_result.caption = "= " .. retval_show - + root.calcui.calcui_table.calcui_table_col2.calcui_result.calcui_copy_display_result.caption = retval_show + -- 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 + -- check first equation and only insert if not the same + if #global.recent_results[player.index] == 0 or global.recent_results[player.index][1]["equation"] ~= original_equation then + table.insert(global.recent_results[player.index], 1, { + equation = original_equation, + result = retval_show }) - root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.scroll_to_bottom() end + draw_recent_table(player) end end @@ -386,7 +427,7 @@ function process_equal_key(player, button) end -- ---------------------------------------------------------------- -function display_addchar(player, char) +local 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) @@ -428,10 +469,10 @@ function handle_calcui_click(event, player) 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); - + + local copy_prefix = "calcui_copy_" + local copy_prefix_len = string.len(copy_prefix); + -- calculator buttons if string.sub(event_name, 1, button_prefix_len) == button_prefix then show_rant(player, false) @@ -442,7 +483,7 @@ function handle_calcui_click(event, player) if dispatch_func then dispatch_func(player, button) end - + local addchar = button_addchar[button] if addchar then display_addchar(player, addchar) @@ -450,15 +491,22 @@ function handle_calcui_click(event, player) -- 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 + -- copy results + elseif string.sub(event_name, 1, copy_prefix_len) == copy_prefix then + if event.button == defines.mouse_button_type.left and event.shift == true then - -- copy equation to display + -- copy equation or result 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 + if event_name == "calcui_copy_display_result" then + root.calcui.calcui_table.calcui_table_col1.calcui_display.text = root.calcui.calcui_table.calcui_table_col2.calcui_result.calcui_copy_display_result.caption + else + 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 + end focus_on_input(player) end + -- if else focus on focus on display + else + focus_on_input(player) end end @@ -477,7 +525,6 @@ end -- ---------------------------------------------------------------- function calcui_on_gui_location_changed(event) if event.element.name == "calcui" then - local root = get_gui_root(game.players[event.player_index]) if not global.gui_position then global.gui_position = {} end diff --git a/control.lua b/control.lua index 21d6f92..bbd0af4 100644 --- a/control.lua +++ b/control.lua @@ -27,17 +27,6 @@ function debug_log(f, str) end -- ---------------------------------------------------------------- -local function get_gui_root(player) - return player.gui.screen -end - --- ---------------------------------------------------------------- -function destroy_calcui_gui(player) - local root = get_gui_root(player) - root.calcui_gui_top.destroy() -end - --- ---------------------------------------------------------------- local function shortcut(event) if event.prototype_name == "calcui_4func" then local player = game.players[event.player_index] @@ -52,8 +41,8 @@ local function on_gui_click(event) local event_name = event.element.name debug_print("event_name " .. event_name) local player = game.players[event.player_index] - - local calcui_prefix = "calcui_" + + local calcui_prefix = "calcui" local possible_marcalc_prefix = string.sub( event_name, 1, string.len(calcui_prefix)) if possible_marcalc_prefix == calcui_prefix then handle_calcui_click(event, player) diff --git a/graphics/backspace.png b/graphics/backspace.png new file mode 100644 index 0000000..608d775 --- /dev/null +++ b/graphics/backspace.png diff --git a/settings.lua b/settings.lua index 9380ece..1925895 100644 --- a/settings.lua +++ b/settings.lua @@ -6,13 +6,11 @@ data:extend({ maximum_value = 10, name = "calcui-decimal-places", setting_type = "runtime-per-user", - default_value = true }, { type = "bool-setting", - default_value = 0, + default_value = false, name = "calcui-clear-on-calc", setting_type = "runtime-per-user", - default_value = false } }) \ No newline at end of file