From c6cb1d225c2d8359daa02931a226d972b9c4fe49 Mon Sep 17 00:00:00 2001 From: Wichu Date: Sat, 6 Jun 2020 21:32:11 +0200 Subject: [PATCH] Bugfix 0.18.1 - based on first user feedback --- .gitignore | 1 + .vscode/launch.json | 2 ++ CHANGELOG.md | 9 +++++++++ calcui-hotkey.lua | 11 ++--------- calculator.lua | 268 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------- control.lua | 9 ++++++++- info.json | 2 +- locale/en/config.cfg | 3 +++ 8 files changed, 167 insertions(+), 138 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5769974 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +calculator-ui_*.zip diff --git a/.vscode/launch.json b/.vscode/launch.json index d0720d7..1cbe187 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,6 +11,8 @@ "modsPath": "C:/Program Files/Factorio/mods", "configPath": "C:/Program Files/Factorio/config/config.ini", "factorioPath": "C:/Program Files/Factorio/bin/x64/factorio.exe" + // "hookSettings": true, + // "hookData": true } ] } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 36e2661..0847a17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,15 @@ and this project adheres to the versioning of Factorio, so 0.18.x will be at lea ## [Released] +## [0.18.1] - 2020-06-06 +Based on user feedback updated the mod +### Added +- Shortcut for opening the calculator and automatically focus on the input (Default: Ctrl+Shift+C) + +### Fixed +- Rounding removed 0 at the end, will now stand as it should. So 2.0000001 -> 2.00 +- Interpreting "," as "." in the equation, because of some European countries do have a weird keyboard layout + ## [0.18.0] - 2020-06-06 ### General - Initial Release of the mod diff --git a/calcui-hotkey.lua b/calcui-hotkey.lua index ba39f42..610d4df 100644 --- a/calcui-hotkey.lua +++ b/calcui-hotkey.lua @@ -2,14 +2,7 @@ data:extend({ { type = "custom-input", name = "calcui_hotkey", - key_sequence = "CONTROL + N", - consuming = "none" - }, - { - type = "custom-input", - name = "calcui_toggle", - key_sequence = "CONTROL + ENTER", + key_sequence = "CONTROL + SHIFT + C", consuming = "none" } -} -) \ No newline at end of file +}) \ No newline at end of file diff --git a/calculator.lua b/calculator.lua index 53263dd..1d3af5a 100644 --- a/calculator.lua +++ b/calculator.lua @@ -29,135 +29,136 @@ end -- ---------------------------------------------------------------- function show_calculator(player) local root = get_gui_root(player) - destroy_calculator(player) - - 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", - style = "calcui_textfield_style", - caption = "", - name = "calcui_display" - }) - local row1 = col1.add({type="flow", name="calcui_col1_row1", direction="horizontal"}) - row1.add({type="button", style="calcui_button_style", caption="CE", name="calcui_button_CE"}) -- CE = Clear Entry (just this line) - row1.add({type="button", style="calcui_button_style", caption="C", name="calcui_button_C"}) -- C = Clear (all, past results as well) - row1.add({type="button", style="calcui_button_style", caption="BS", name="calcui_button_BS"}) - row1.add({type="button", style="calcui_button_style", caption="/", name="calcui_button_DIV"}) + 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", + style = "calcui_textfield_style", + caption = "", + name = "calcui_display" + }) - local row2 = col1.add({type="flow", name="calcui_col1_row2", direction="horizontal"}) - row2.add({type="button", style="calcui_button_style", caption="7", name="calcui_button_7"}) - row2.add({type="button", style="calcui_button_style", caption="8", name="calcui_button_8"}) - row2.add({type="button", style="calcui_button_style", caption="9", name="calcui_button_9"}) - row2.add({type="button", style="calcui_button_style", caption="*", name="calcui_button_MUL"}) + local row1 = col1.add({type="flow", name="calcui_col1_row1", direction="horizontal"}) + row1.add({type="button", style="calcui_button_style", caption="CE", name="calcui_button_CE"}) -- CE = Clear Entry (just this line) + row1.add({type="button", style="calcui_button_style", caption="C", name="calcui_button_C"}) -- C = Clear (all, past results as well) + row1.add({type="button", style="calcui_button_style", caption="BS", name="calcui_button_BS"}) + row1.add({type="button", style="calcui_button_style", caption="/", name="calcui_button_DIV"}) - local row3 = col1.add({type="flow", name="calcui_col1_row3", direction="horizontal"}) - row3.add({type="button", style="calcui_button_style", caption="4", name="calcui_button_4"}) - row3.add({type="button", style="calcui_button_style", caption="5", name="calcui_button_5"}) - row3.add({type="button", style="calcui_button_style", caption="6", name="calcui_button_6"}) - row3.add({type="button", style="calcui_button_style", caption="-", name="calcui_button_SUB"}) + local row2 = col1.add({type="flow", name="calcui_col1_row2", direction="horizontal"}) + row2.add({type="button", style="calcui_button_style", caption="7", name="calcui_button_7"}) + row2.add({type="button", style="calcui_button_style", caption="8", name="calcui_button_8"}) + row2.add({type="button", style="calcui_button_style", caption="9", name="calcui_button_9"}) + row2.add({type="button", style="calcui_button_style", caption="*", name="calcui_button_MUL"}) - local row4 = col1.add({type="flow", name="calcui_col1_row4", direction="horizontal"}) - row4.add({type="button", style="calcui_button_style", caption="1", name="calcui_button_1"}) - row4.add({type="button", style="calcui_button_style", caption="2", name="calcui_button_2"}) - row4.add({type="button", style="calcui_button_style", caption="3", name="calcui_button_3"}) - row4.add({type="button", style="calcui_button_style", caption="+", name="calcui_button_ADD"}) + local row3 = col1.add({type="flow", name="calcui_col1_row3", direction="horizontal"}) + row3.add({type="button", style="calcui_button_style", caption="4", name="calcui_button_4"}) + row3.add({type="button", style="calcui_button_style", caption="5", name="calcui_button_5"}) + row3.add({type="button", style="calcui_button_style", caption="6", name="calcui_button_6"}) + row3.add({type="button", style="calcui_button_style", caption="-", name="calcui_button_SUB"}) - local row5 = col1.add({type="flow", name="calcui_col1_row5", direction="horizontal"}) - row5.add({type="button", style="calcui_button_style", caption="%", name="calcui_button_PERC"}) - row5.add({type="button", style="calcui_button_style", caption="0", name="calcui_button_0"}) - row5.add({type="button", style="calcui_button_style", caption=".", name="calcui_button_DOT"}) - row5.add({type="button", style="calcui_button_style", caption="=", name="calcui_button_EQU"}) + local row4 = col1.add({type="flow", name="calcui_col1_row4", direction="horizontal"}) + row4.add({type="button", style="calcui_button_style", caption="1", name="calcui_button_1"}) + row4.add({type="button", style="calcui_button_style", caption="2", name="calcui_button_2"}) + row4.add({type="button", style="calcui_button_style", caption="3", name="calcui_button_3"}) + row4.add({type="button", style="calcui_button_style", caption="+", name="calcui_button_ADD"}) + local row5 = col1.add({type="flow", name="calcui_col1_row5", direction="horizontal"}) + row5.add({type="button", style="calcui_button_style", caption="%", name="calcui_button_PERC"}) + row5.add({type="button", style="calcui_button_style", caption="0", name="calcui_button_0"}) + row5.add({type="button", style="calcui_button_style", caption=".", name="calcui_button_DOT"}) + row5.add({type="button", style="calcui_button_style", caption="=", name="calcui_button_EQU"}) - 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" - - - -- center the gui - calcui.force_auto_center() + 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" + + + -- center the gui + calcui.force_auto_center() + end end -- ---------------------------------------------------------------- @@ -176,6 +177,12 @@ function toggle_calculator(player) end -- ---------------------------------------------------------------- +function focus_on_input(player) + local root = get_gui_root(player) + root.calcui.calcui_table.calcui_table_col1.calcui_display.focus() +end + +-- ---------------------------------------------------------------- function clear_equation(player) local root = get_gui_root(player) root.calcui.calcui_table.calcui_table_col1.calcui_display.text = "" @@ -234,6 +241,9 @@ function fix_equation(equation) -- fix percentage result = result:gsub("(%%)", "/100") + -- fix danish keyboard + result = result:gsub(",", ".") + return result end @@ -253,14 +263,18 @@ function process_equal_key(player, button) return load("return " .. equation)() end) root.calcui.calcui_table.calcui_table_col2.calcui_display_result.tooltip = retval - status, retval = pcall(function() - return tonumber(string.format("%." .. settings.get_player_settings(player)["calcui-decimal-places"].value .. "f", retval)) + 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) - if retval == nil or retval == "" then + if retval_show == nil or retval_show == "" then status = false end if not status then - retval = "NaN" + retval_show = "NaN" show_rant(player, true) else if retval <= 0 then @@ -269,7 +283,7 @@ function process_equal_key(player, button) show_rant(player, false) end end - root.calcui.calcui_table.calcui_table_col2.calcui_display_result.caption = "= " .. retval + root.calcui.calcui_table.calcui_table_col2.calcui_display_result.caption = "= " .. retval_show -- only write in recent table if actually a result if status then @@ -370,7 +384,7 @@ function handle_calcui_click(event, player) -- 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 - root.calcui.calcui_table.calcui_table_col1.calcui_display.focus() + focus_on_input(player) end end end diff --git a/control.lua b/control.lua index ff6c968..21d6f92 100644 --- a/control.lua +++ b/control.lua @@ -86,7 +86,14 @@ local function on_calcui_command(event) end -- ---------------------------------------------------------------- ---script.on_event( "calcui_hotkey", on_hotkey_main ) +local function on_hotkey_main(event) + local player = game.players[event.player_index] + show_calculator(player) + focus_on_input(player) +end + +-- ---------------------------------------------------------------- +script.on_event( "calcui_hotkey", on_hotkey_main ) script.on_event( defines.events.on_lua_shortcut, shortcut ) script.on_event( defines.events.on_gui_click, on_gui_click) script.on_event( defines.events.on_gui_confirmed, on_gui_confirmed) diff --git a/info.json b/info.json index 3ca3eb7..cc51c76 100644 --- a/info.json +++ b/info.json @@ -1,6 +1,6 @@ { "name": "calculator-ui", - "version": "0.18.0", + "version": "0.18.1", "title": "Calculator UI", "author": "Wichu", "factorio_version": "0.18", diff --git a/locale/en/config.cfg b/locale/en/config.cfg index 8a90808..8f96250 100644 --- a/locale/en/config.cfg +++ b/locale/en/config.cfg @@ -1,6 +1,9 @@ [shortcut-name] calcui_4func=Calculator +[controls] +calcui_hotkey=Focus on input + [mod-setting-name] calcui-decimal-places=Decimal places calcui-clear-on-calc=Clear equation on calculation -- libgit2 0.22.2