diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..d0720d7 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "factoriomod", + "request": "launch", + "name": "Factorio Mod Debug", + "modsPath": "C:/Program Files/Factorio/mods", + "configPath": "C:/Program Files/Factorio/config/config.ini", + "factorioPath": "C:/Program Files/Factorio/bin/x64/factorio.exe" + } + ] +} \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..36e2661 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,50 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to the versioning of Factorio, so 0.18.x will be at least compatible / tested with Factorio 0.18.x. + +## [Released] + +## [0.18.0] - 2020-06-06 +### General +- Initial Release of the mod +- Compatible with Factorio >0.18.0; tested on 0.18.30 +- Based on the 4-Function Calculator found in the [Max Rate Calculator](https://mods.factorio.com/mod/MaxRateCalculator) mod by [Theanderblast](https://mods.factorio.com/user/theanderblast) + +### Added +- History of recent calculations +- Possibility to copy recent calculations to the current one (with shift+left-click) +- Substitutes some functions & "constants" of the Lua [math-lib](http://lua-users.org/wiki/MathLibraryTutorial) + - abs() -> math.abs() + - acos() -> math.acos() + - asin() -> math.asin() + - atan() -> math.atan() + - ceil() -> math.ceil() + - floor() -> math.floor() + - cos() -> math.cos() + - sin() -> math.sin() + - tan() -> math.tan() + - deg() -> math.deg() + - rad() -> math.rad() + - exp() -> math.exp() + - log() -> math.log() + - min() -> math.min() + - max() -> math.max() + - modf() -> math.modf() + - sqrt() -> math.sqrt() + - huge -> math.huge + - pi -> math.pi +- Allows the use of "%" (percent sign) in the calculation +- A little easter egg :-) +- Setting for number of decimal places in result (Default: 2). Exact value will be displayed in the tooltip +- Setting if triggering the calculation should clear the current equation (Default: no) + +### Changed +- Made the buttons slightly bigger, so that it's better readable on streams. +- Allows to make "complex" calculations with parentheses +- Allows pretty much all characters in the equation field + - but the "=" (equal sign) will trigger the calculation + +### Removed +- Got rid of the memory functions - but introduced the list of recent calculations instead \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b667143 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# Calculator UI + +Calculator UI is a Factorio mod which adds a calculator to the Factorio game with the goal to have a little bit of usability behind it. + +## Based on +The 4-Function Calculator found in the [Max Rate Calculator](https://mods.factorio.com/mod/MaxRateCalculator) mod by [Theanderblast](https://mods.factorio.com/user/theanderblast). + +## Features +- List of recent calculations +- Use of parentheses in the equation +- rounding of numbers (number of decimal places can be set in the settings) +- copy a recent calculation into the current calculation (by shift+left-click on the equation you want to copy) +- a nifty little easter egg ;-) +- use of some advanced function and constants of the Lua [math-lib](http://lua-users.org/wiki/MathLibraryTutorial) (without math.) -> e.g. "pi" instead of "math.pi". + +## Installation +Well ... ehm ... search for the mod ingame and install it :-) + +## Usage +Press the 3 little dots on the shortcuts UI of Factorio to select the "Calculator". Afterwards you can click on the calculator symbol to open it. + +## Changelog +see separate [changelog](changelog.md) + +## License +[MIT](https://choosealicense.com/licenses/mit/) \ No newline at end of file diff --git a/calculator.lua b/calculator.lua index 639080b..53263dd 100644 --- a/calculator.lua +++ b/calculator.lua @@ -218,8 +218,8 @@ function fix_equation(equation) ["tan"] = "math.tan", ["deg"] = "math.deg", ["rad"] = "math.rad", - ["exp"] = "math.log", - ["exp"] = "math.log", + ["exp"] = "math.exp", + ["log"] = "math.log", ["min"] = "math.min", ["max"] = "math.max", ["modf"] = "math.modf", @@ -292,6 +292,10 @@ function process_equal_key(player, button) end end end + + if settings.get_player_settings(player)["calcui-clear-on-calc"].value then + clear_equation(player) + end end -- ---------------------------------------------------------------- @@ -390,194 +394,4 @@ function calcui_on_gui_location_changed(event) local root = get_gui_root(player) root.calcui.location = event.element.location end -end - - - - - - --- -- ---------------------------------------------------------------- --- local function process_number_key(player, num) - -- debug_print("process_number_key(" .. num .. ")") - - -- if global.calcui_context[player.index].in_left_of_decimal then - -- val = num / global.calcui_context[player.index].decimal_place - -- if global.calcui_context[player.index].in_data_entry then - -- global.calcui_context[player.index].current_value = global.calcui_context[player.index].current_value + val - -- else - -- global.calcui_context[player.index].current_value = val - -- end - -- global.calcui_context[player.index].decimal_place = global.calcui_context[player.index].decimal_place * 10 - -- else - -- if global.calcui_context[player.index].in_data_entry then - -- global.calcui_context[player.index].current_value = global.calcui_context[player.index].current_value * 10 + num - -- else - -- global.calcui_context[player.index].current_value = tonumber(num) - -- end - -- end - -- global.calcui_context[player.index].in_data_entry = true - -- update_display(player) --- end - --- -- ---------------------------------------------------------------- --- local function process_decimal_key(player, key) - -- debug_print("process_decimal_key(" .. key .. ")") - -- if global.calcui_context[player.index].in_left_of_decimal then - -- return - -- end - - -- global.calcui_context[player.index].in_left_of_decimal = true - -- global.calcui_context[player.index].decimal_place = 10 - -- update_display(player) --- end - --- -- ---------------------------------------------------------------- --- local function process_change_sign_key(player, key) - -- global.calcui_context[player.index].current_value = global.calcui_context[player.index].current_value * -1 - -- update_display(player) --- end - --- -- ---------------------------------------------------------------- --- local function process_equal_key(player, key) - -- debug_print("process_equal_key(" .. key .. ") global.calcui_context[player.index].current_func is " .. global.calcui_context[player.index].current_func) - - -- global.calcui_context[player.index].in_data_entry = false - -- doing_decimal = false - - -- if global.calcui_context[player.index].current_func == "ADD" then - -- global.calcui_context[player.index].current_value = global.calcui_context[player.index].operand + global.calcui_context[player.index].current_value - -- elseif global.calcui_context[player.index].current_func == "SUB" then - -- global.calcui_context[player.index].current_value = global.calcui_context[player.index].operand - global.calcui_context[player.index].current_value - -- elseif global.calcui_context[player.index].current_func == "MUL" then - -- global.calcui_context[player.index].current_value = global.calcui_context[player.index].operand * global.calcui_context[player.index].current_value - -- elseif global.calcui_context[player.index].current_func == "DIV" then - -- debug_print("process_equal_key global.calcui_context[player.index].current_value " .. global.calcui_context[player.index].current_value) - - -- if global.calcui_context[player.index].current_value == 0 then - -- debug_print("process_equal_key DIVIDE BY ZERO") - -- update_display(player, {"calcui_divide_by_zero"}) -- TODO: localize - -- return - -- end - -- global.calcui_context[player.index].current_value = global.calcui_context[player.index].operand / global.calcui_context[player.index].current_value - -- end - -- global.calcui_context[player.index].current_func = "" - -- global.calcui_context[player.index].in_data_entry = false - -- update_display(player) --- end - --- -- ---------------------------------------------------------------- --- local function process_func_key(player, key) - -- debug_print("process_func_key(" .. key .. ")") - - -- if global.calcui_context[player.index].current_func ~= "" then - -- process_equal_key(player, key) - -- end - - -- global.calcui_context[player.index].current_func = key - -- global.calcui_context[player.index].operand = global.calcui_context[player.index].current_value - -- global.calcui_context[player.index].in_data_entry = false - -- global.calcui_context[player.index].in_left_of_decimal = false --- end - --- -- ---------------------------------------------------------------- --- local function process_mem_key(player, key) - -- debug_print("process_mem_key(" .. key .. ")") - -- if calculator_memory == nil then - -- debug_print("process_mem_key(" .. key .. ") calculator_memory was nil") - -- calculator_memory = 0 - -- end - -- if key == "MS" then - -- calculator_memory = global.calcui_context[player.index].current_value - -- elseif key == "M+" then - -- calculator_memory = calculator_memory + global.calcui_context[player.index].current_value - -- elseif key == "M-" then - -- calculator_memory = calculator_memory - global.calcui_context[player.index].current_value - -- else - -- global.calcui_context[player.index].current_value = calculator_memory - -- end - -- global.calcui_context[player.index].in_data_entry = false - -- update_display(player) --- end - --- -- ---------------------------------------------------------------- --- local function process_edit_key(player, key) - -- debug_print("process_edit_key(" .. key .. ")") - -- if key == "CE" then - -- global.calcui_context[player.index].current_value = 0 - -- elseif key == "C" then - -- init_calculator(player) - -- end - -- global.calcui_context[player.index].in_data_entry = false - -- update_display(player) --- end - --- -- ---------------------------------------------------------------- --- local function set_current_value_to_text(player, text) - -- local last_char = string.sub(text, -1) - -- if last_char == "+" then - -- process_func_key(player, "ADD") - -- elseif last_char == "-" then - -- process_func_key(player, "SUB") - -- elseif last_char == "*" then - -- process_func_key(player, "MUL") - -- elseif last_char == "/" then - -- process_func_key(player, "DIV") - -- else - -- -- number - -- local val = tonumber(text) - -- if val ~= nil then - -- global.calcui_context[player.index].current_value = val - -- debug_print("set_current_value_to_text got value " .. text) - -- local plain_text = true - -- local dot = string.find(text, '.', 1, plain_text) - -- if dot ~= nil then - -- local number_decimal_places = #text - dot - -- global.calcui_context[player.index].decimal_place = 10 ^ (number_decimal_places + 1) - -- global.calcui_context[player.index].in_left_of_decimal = true - -- debug_print("#text " .. #text .. " number_global.calcui_context[player.index].decimal_places " .. - -- number_decimal_places .. " global.calcui_context[player.index].decimal_place " .. global.calcui_context[player.index].decimal_place .. " dot " .. dot .. " in_lod " .. boolstr(global.calcui_context[player.index].in_left_of_decimal)) - -- else - -- debug_print("set_current_value_to_text no dot. set lod to false" ) - -- global.calcui_context[player.index].in_left_of_decimal = false - -- end - -- else - -- debug_print("got junk") - -- end - -- end --- end - --- -- ---------------------------------------------------------------- --- local function process_backspace_key(player, key) - -- local root = get_gui_root(player) - -- debug_print("process_backspace_key(" .. key .. ")") - -- local old_text = root.calcui.calcui_display.caption - -- local old_len = #old_text - -- text = string.sub(old_text, 1, old_len -1) - -- debug_print("process_backspace_key(" .. key .. ") old_text " .. old_text .. " old_len " .. old_len .. " text " .. text .. - -- " in_decimal " .. boolstr(global.calcui_context[player.index].in_left_of_decimal) .. " global.calcui_context[player.index].decimal_place " .. global.calcui_context[player.index].decimal_place) - -- if #text == 0 then - -- global.calcui_context[player.index].current_value = 0 - -- else - -- set_current_value_to_text(player, text) - -- end - -- update_display(player) --- end - --- -- ---------------------------------------------------------------- --- local function calcui_toggle_key(event) - -- local player = game.players[event.player_index] - -- toggle_calculator(player) --- end - --- -- ---------------------------------------------------------------- --- function calcui_clickable_value_clicked(player, val) -- a value in the main Max Rate Calculator has been clicked on. Paste into current value - -- local root = get_gui_root(player) - -- if root ~=nil and root.calcui ~= nil then - -- local text = tostring(val) - -- set_current_value_to_text(player, text) - -- update_display(player) - -- end --- end - --- script.on_event( "calcui_toggle", calcui_toggle_key ) \ No newline at end of file +end \ No newline at end of file diff --git a/control.lua b/control.lua index 8acb100..ff6c968 100644 --- a/control.lua +++ b/control.lua @@ -28,7 +28,6 @@ end -- ---------------------------------------------------------------- local function get_gui_root(player) - -- return player.gui.left return player.gui.screen end @@ -52,10 +51,7 @@ end local function on_gui_click(event) local event_name = event.element.name debug_print("event_name " .. event_name) - -- local calcui_prefix = "calcui_" - -- local possible_calcui_prefix = string.sub( event_name, 1, string.len(calcui_prefix) ) local player = game.players[event.player_index] - -- local root = get_gui_root(player) local calcui_prefix = "calcui_" local possible_marcalc_prefix = string.sub( event_name, 1, string.len(calcui_prefix)) @@ -63,25 +59,6 @@ local function on_gui_click(event) handle_calcui_click(event, player) return end - - -- if possible_calcui_prefix == calcui_prefix then - -- if event_name == "calcui_calculator_button" then - -- toggle_calculator(player) - -- return - -- end - - -- if root.calcui_gui_top then - -- if event_name == "calcui_close_button" then - -- destroy_calcui_gui(player) - -- hide_calculator(player) - -- end - -- elseif event_name == "calcui_close_button" then - -- if player.gui.left.calcui_gui_top then - -- player.gui.left.calcui_gui_top.destroy() - -- hide_calculator(player) - -- end - -- end - -- end end -- ---------------------------------------------------------------- @@ -90,9 +67,6 @@ local function on_gui_confirmed(event) player = game.players[event.player_index]; if event.element.name == "calcui_display" then process_equal_key(player) - if settings.get_player_settings(player)["calcui-enter-clear"].value then - clear_equation(player) - end end end @@ -118,4 +92,4 @@ script.on_event( defines.events.on_gui_click, on_gui_click) script.on_event( defines.events.on_gui_confirmed, on_gui_confirmed) script.on_event( defines.events.on_gui_text_changed, calcui_on_gui_text_changed ) script.on_event( defines.events.on_gui_location_changed, calcui_on_gui_location_changed ) -commands.add_command( "calcui", "Max Rate Calculator [ debug | nodebug ] ", on_calcui_command ) \ No newline at end of file +commands.add_command( "calcui", "Calculator UI [ debug | nodebug ] ", on_calcui_command ) \ No newline at end of file diff --git a/locale/en/config.cfg b/locale/en/config.cfg index c511e69..8a90808 100644 --- a/locale/en/config.cfg +++ b/locale/en/config.cfg @@ -3,11 +3,11 @@ calcui_4func=Calculator [mod-setting-name] calcui-decimal-places=Decimal places -calcui-enter-clear=Clear equation on enter? +calcui-clear-on-calc=Clear equation on calculation [mod-settings-description] calcui-decimal-places=Number of decimal places in the result -calcui-enter-clear=Should pressing the enter key delete the equation? +calcui-clear-on-calc=Should trigger the calculation delete the equation? [calculator-ui] title=Calculator diff --git a/settings.lua b/settings.lua index 84ce78e..9380ece 100644 --- a/settings.lua +++ b/settings.lua @@ -11,7 +11,7 @@ data:extend({ { type = "bool-setting", default_value = 0, - name = "calcui-enter-clear", + name = "calcui-clear-on-calc", setting_type = "runtime-per-user", default_value = false } diff --git a/thumbnail.png b/thumbnail.png new file mode 100644 index 0000000..cc8449d --- /dev/null +++ b/thumbnail.png