diff --git b/calcui-hotkey.lua a/calcui-hotkey.lua new file mode 100644 index 0000000..ba39f42 --- /dev/null +++ a/calcui-hotkey.lua @@ -0,0 +1,15 @@ +data:extend({ + { + type = "custom-input", + name = "calcui_hotkey", + key_sequence = "CONTROL + N", + consuming = "none" + }, + { + type = "custom-input", + name = "calcui_toggle", + key_sequence = "CONTROL + ENTER", + consuming = "none" + } +} +) \ No newline at end of file diff --git b/calcui-prototypes.lua a/calcui-prototypes.lua new file mode 100644 index 0000000..73f7e0e --- /dev/null +++ a/calcui-prototypes.lua @@ -0,0 +1,19 @@ +-- calc-ui-prototypes.lua + +data:extend({ + { + type = "shortcut", + name = "calcui_4func", + order = "b[blueprints]-h[calculator-ui]", + action = "lua", + toggleable = true, + icon = + { + filename = "__calculator-ui__/graphics/calculator.png", + priority = "extra-high-no-scale", + size = 64, + scale = 1, + flags = {"icon"} + } + } +}) \ No newline at end of file diff --git b/calcui-styles.lua a/calcui-styles.lua new file mode 100644 index 0000000..a19b393 --- /dev/null +++ a/calcui-styles.lua @@ -0,0 +1,72 @@ +-- calc-ui-styles.lua + +local default_gui = data.raw["gui-style"].default + +default_gui.calcui_sprite_obj_style = { + type = "button_style", + -- parent="button_style", + top_padding = 0, + right_padding = 0, + bottom_padding = 0, + left_padding = 0, + height = 32, + width = 32, + scalable = false +} + +default_gui.calcui_button_style = { + type = "button_style", + -- parent="button_style", + top_padding = 0, + right_padding = 0, + bottom_padding = 0, + left_padding = 0, + height = 50, + width = 50, + scalable = false +} + +default_gui.calcui_textfield_style = { + type = "textbox_style", + width = 212 +} + +default_gui.calcui_table_style = { + type = "table_style", + horizontal_spacing = 5, + vertical_spacing = 1, + resize_row_to_width = false, + resize_to_row_height = false, +} + +default_gui.calcui_scroll_pane_style = { + type = "scroll_pane_style", + -- parent="scroll_pane_style", + -- flow_style = + -- { + -- type = "flow_style", + -- parent = "flow_style" + -- }, + resize_row_to_width = true, + resize_to_row_height = false, + minimal_height=128, + maximal_height=400, + max_on_row = 1, +} + +data:extend({ + { + type = "sprite", + name = "sprite_calcui_rant", + filename = "__calculator-ui__/graphics/nilausRant.png", + width = 56, + height = 56 + }, + { + type = "sprite", + name = "sprite_calcui_calculator", + filename = "__calculator-ui__/graphics/calculator.png", + width = 64, + height = 64 + } +}) \ No newline at end of file diff --git b/calculator.lua a/calculator.lua new file mode 100644 index 0000000..639080b --- /dev/null +++ a/calculator.lua @@ -0,0 +1,583 @@ +-- calculator.lua + +-- ---------------------------------------------------------------- +local function get_gui_root(player) + return player.gui.screen +end + +-- ---------------------------------------------------------------- +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 + +-- ---------------------------------------------------------------- +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"}) + + 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 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 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() +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 + +-- ---------------------------------------------------------------- +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 = "=" +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 + +-- ---------------------------------------------------------------- +function fix_equation(equation) + local result = equation + + -- fix math library shortcuts + local math_lib = { + ["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.log", + ["exp"] = "math.log", + ["min"] = "math.min", + ["max"] = "math.max", + ["modf"] = "math.modf", + ["sqrt"] = "math.sqrt", + ["huge"] = "math.huge", + ["pi"] = "math.pi" + } + for key, val in pairs(math_lib) do + result = result:gsub(key, val) + end + + -- fix percentage + result = result:gsub("(%%)", "/100") + + return result +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 = fix_equation(original_equation) + + -- 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 + status, retval = pcall(function() + return tonumber(string.format("%." .. settings.get_player_settings(player)["calcui-decimal-places"].value .. "f", retval)) + end) + if retval == nil or retval == "" then + status = false + end + if not status then + retval = "NaN" + show_rant(player, true) + else + if retval <= 0 then + show_rant(player, true) + else + show_rant(player, false) + end + end + root.calcui.calcui_table.calcui_table_col2.calcui_display_result.caption = "= " .. retval + + -- 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 +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 + root.calcui.calcui_table.calcui_table_col1.calcui_display.focus() + 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 + local player = game.players[event.player_index] + 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 diff --git b/control.lua a/control.lua new file mode 100644 index 0000000..8acb100 --- /dev/null +++ a/control.lua @@ -0,0 +1,121 @@ +-- control.lua + +require("calculator") + +-- ---------------------------------------------------------------- +function boolstr(bool) + if bool then + return "T" + else + return "F" + end +end + +-- ---------------------------------------------------------------- +function debug_print(str) + if global.marc_debug then + game.print(str) + end +end + +function __FUNC__() return debug.getinfo(2, 'n').name end + +function debug_log(f, str) + if global.marc_debug then + game.print(f .. ": " .. str) + end +end + +-- ---------------------------------------------------------------- +local function get_gui_root(player) + -- return player.gui.left + 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] + toggle_calculator(player) + end +end + +-- ---------------------------------------------------------------- +-- user has clicked somewhere. If clicked on any gui item name that starts with "calcui_..." +-- hide the gui +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)) + if possible_marcalc_prefix == calcui_prefix then + 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 + +-- ---------------------------------------------------------------- +-- user has confirmed the textfield / Called when a LuaGuiElement is confirmed, for example by pressing Enter in a textfield. +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 + +-- ---------------------------------------------------------------- +local function on_calcui_command(event) + if event.parameter == "debug" then + global.calcui_debug = true + debug_print("calcui debugging is on") + elseif event.parameter == "nodebug" then + debug_print("calcui debugging is off") + global.calcui_debug = false + elseif event.parameter == nil then + game.players[event.player_index].print("please add a parameter") + else + game.players[event.player_index].print("unknown calcui parameter: " .. event.parameter) + end +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) +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 diff --git b/data.lua a/data.lua new file mode 100644 index 0000000..f52719f --- /dev/null +++ a/data.lua @@ -0,0 +1,5 @@ +--data.lua + +require("calcui-prototypes") +require("calcui-styles") +require("calcui-hotkey") \ No newline at end of file diff --git b/graphics/calculator.png a/graphics/calculator.png new file mode 100644 index 0000000..d95b768 --- /dev/null +++ a/graphics/calculator.png diff --git b/graphics/nilausRant.png a/graphics/nilausRant.png new file mode 100644 index 0000000..668f3ba --- /dev/null +++ a/graphics/nilausRant.png diff --git b/info.json a/info.json new file mode 100644 index 0000000..3ca3eb7 --- /dev/null +++ a/info.json @@ -0,0 +1,9 @@ +{ + "name": "calculator-ui", + "version": "0.18.0", + "title": "Calculator UI", + "author": "Wichu", + "factorio_version": "0.18", + "dependencies": ["base >= 0.18"], + "description": "This mod adds a calculator with advanced controls to the UI." +} \ No newline at end of file diff --git b/locale/en/config.cfg a/locale/en/config.cfg new file mode 100644 index 0000000..c511e69 --- /dev/null +++ a/locale/en/config.cfg @@ -0,0 +1,14 @@ +[shortcut-name] +calcui_4func=Calculator + +[mod-setting-name] +calcui-decimal-places=Decimal places +calcui-enter-clear=Clear equation on enter? + +[mod-settings-description] +calcui-decimal-places=Number of decimal places in the result +calcui-enter-clear=Should pressing the enter key delete the equation? + +[calculator-ui] +title=Calculator +recent_tooltip=Press shift+left-click to copy it to current equation \ No newline at end of file diff --git b/settings.lua a/settings.lua new file mode 100644 index 0000000..84ce78e --- /dev/null +++ a/settings.lua @@ -0,0 +1,18 @@ +data:extend({ + { + type = "int-setting", + default_value = 2, + minimum_value = 0, + maximum_value = 10, + name = "calcui-decimal-places", + setting_type = "runtime-per-user", + default_value = true + }, + { + type = "bool-setting", + default_value = 0, + name = "calcui-enter-clear", + setting_type = "runtime-per-user", + default_value = false + } +}) \ No newline at end of file