Commit 3ef575ee7b1d42fb1a64b0b101fabc3d7ddb38d4

Authored by Stefan Wichmann
0 parents

Initial checkin

calcui-hotkey.lua 0 → 100644
  1 +++ a/calcui-hotkey.lua
  1 +data:extend({
  2 + {
  3 + type = "custom-input",
  4 + name = "calcui_hotkey",
  5 + key_sequence = "CONTROL + N",
  6 + consuming = "none"
  7 + },
  8 + {
  9 + type = "custom-input",
  10 + name = "calcui_toggle",
  11 + key_sequence = "CONTROL + ENTER",
  12 + consuming = "none"
  13 + }
  14 +}
  15 +)
0 \ No newline at end of file 16 \ No newline at end of file
calcui-prototypes.lua 0 → 100644
  1 +++ a/calcui-prototypes.lua
  1 +-- calc-ui-prototypes.lua
  2 +
  3 +data:extend({
  4 + {
  5 + type = "shortcut",
  6 + name = "calcui_4func",
  7 + order = "b[blueprints]-h[calculator-ui]",
  8 + action = "lua",
  9 + toggleable = true,
  10 + icon =
  11 + {
  12 + filename = "__calculator-ui__/graphics/calculator.png",
  13 + priority = "extra-high-no-scale",
  14 + size = 64,
  15 + scale = 1,
  16 + flags = {"icon"}
  17 + }
  18 + }
  19 +})
0 \ No newline at end of file 20 \ No newline at end of file
calcui-styles.lua 0 → 100644
  1 +++ a/calcui-styles.lua
  1 +-- calc-ui-styles.lua
  2 +
  3 +local default_gui = data.raw["gui-style"].default
  4 +
  5 +default_gui.calcui_sprite_obj_style = {
  6 + type = "button_style",
  7 + -- parent="button_style",
  8 + top_padding = 0,
  9 + right_padding = 0,
  10 + bottom_padding = 0,
  11 + left_padding = 0,
  12 + height = 32,
  13 + width = 32,
  14 + scalable = false
  15 +}
  16 +
  17 +default_gui.calcui_button_style = {
  18 + type = "button_style",
  19 + -- parent="button_style",
  20 + top_padding = 0,
  21 + right_padding = 0,
  22 + bottom_padding = 0,
  23 + left_padding = 0,
  24 + height = 50,
  25 + width = 50,
  26 + scalable = false
  27 +}
  28 +
  29 +default_gui.calcui_textfield_style = {
  30 + type = "textbox_style",
  31 + width = 212
  32 +}
  33 +
  34 +default_gui.calcui_table_style = {
  35 + type = "table_style",
  36 + horizontal_spacing = 5,
  37 + vertical_spacing = 1,
  38 + resize_row_to_width = false,
  39 + resize_to_row_height = false,
  40 +}
  41 +
  42 +default_gui.calcui_scroll_pane_style = {
  43 + type = "scroll_pane_style",
  44 + -- parent="scroll_pane_style",
  45 + -- flow_style =
  46 + -- {
  47 + -- type = "flow_style",
  48 + -- parent = "flow_style"
  49 + -- },
  50 + resize_row_to_width = true,
  51 + resize_to_row_height = false,
  52 + minimal_height=128,
  53 + maximal_height=400,
  54 + max_on_row = 1,
  55 +}
  56 +
  57 +data:extend({
  58 + {
  59 + type = "sprite",
  60 + name = "sprite_calcui_rant",
  61 + filename = "__calculator-ui__/graphics/nilausRant.png",
  62 + width = 56,
  63 + height = 56
  64 + },
  65 + {
  66 + type = "sprite",
  67 + name = "sprite_calcui_calculator",
  68 + filename = "__calculator-ui__/graphics/calculator.png",
  69 + width = 64,
  70 + height = 64
  71 + }
  72 +})
0 \ No newline at end of file 73 \ No newline at end of file
calculator.lua 0 → 100644
  1 +++ a/calculator.lua
  1 +-- calculator.lua
  2 +
  3 +-- ----------------------------------------------------------------
  4 +local function get_gui_root(player)
  5 + return player.gui.screen
  6 +end
  7 +
  8 +-- ----------------------------------------------------------------
  9 +function show_rant(player, enabled)
  10 + local root = get_gui_root(player)
  11 +
  12 + if enabled then
  13 + root.calcui.calcui_table.calcui_table_col2.calcui_rant.sprite = "sprite_calcui_rant"
  14 + root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.style.height = 196
  15 + else
  16 + root.calcui.calcui_table.calcui_table_col2.calcui_rant.sprite = nil
  17 + root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.style.height = 252
  18 + end
  19 +end
  20 +
  21 +-- ----------------------------------------------------------------
  22 +local function destroy_calculator(player)
  23 + local root = get_gui_root(player)
  24 + if root.calcui then
  25 + root.calcui.destroy()
  26 + end
  27 +end
  28 +
  29 +-- ----------------------------------------------------------------
  30 +function show_calculator(player)
  31 + local root = get_gui_root(player)
  32 + destroy_calculator(player)
  33 +
  34 + local calcui = root.add({
  35 + type = "frame",
  36 + name = "calcui",
  37 + style = "dialog_frame",
  38 + direction = "vertical"
  39 + })
  40 +
  41 + local flow = calcui.add({
  42 + type = "flow",
  43 + name = "calcui_flow"
  44 + })
  45 + flow.style.horizontally_stretchable = "on"
  46 +
  47 + flow.add({
  48 + type = "label",
  49 + caption = {"calculator-ui.title"},
  50 + style = "frame_title"
  51 + }).drag_target = calcui
  52 +
  53 + local widget = flow.add({
  54 + type = "empty-widget",
  55 + style = "draggable_space_header",
  56 + name = "calcui_drag"
  57 + })
  58 + widget.drag_target = calcui
  59 + widget.style.horizontally_stretchable = "on"
  60 + widget.style.minimal_width = 24
  61 + widget.style.natural_height = 24
  62 +
  63 + flow.add({
  64 + type = "sprite-button",
  65 + sprite = "utility/close_white",
  66 + style = "frame_action_button",
  67 + name = "calcui_close"
  68 + })
  69 +
  70 + local table = calcui.add({
  71 + type = "table",
  72 + name = "calcui_table",
  73 + column_count = "2",
  74 + vertical_centering = "false"
  75 + })
  76 +
  77 + local col1 = table.add({
  78 + type = "flow",
  79 + name = "calcui_table_col1",
  80 + direction = "vertical"
  81 + })
  82 +
  83 + local display = col1.add({
  84 + type = "textfield",
  85 + style = "calcui_textfield_style",
  86 + caption = "",
  87 + name = "calcui_display"
  88 + })
  89 +
  90 + local row1 = col1.add({type="flow", name="calcui_col1_row1", direction="horizontal"})
  91 + row1.add({type="button", style="calcui_button_style", caption="CE", name="calcui_button_CE"}) -- CE = Clear Entry (just this line)
  92 + row1.add({type="button", style="calcui_button_style", caption="C", name="calcui_button_C"}) -- C = Clear (all, past results as well)
  93 + row1.add({type="button", style="calcui_button_style", caption="BS", name="calcui_button_BS"})
  94 + row1.add({type="button", style="calcui_button_style", caption="/", name="calcui_button_DIV"})
  95 +
  96 + local row2 = col1.add({type="flow", name="calcui_col1_row2", direction="horizontal"})
  97 + row2.add({type="button", style="calcui_button_style", caption="7", name="calcui_button_7"})
  98 + row2.add({type="button", style="calcui_button_style", caption="8", name="calcui_button_8"})
  99 + row2.add({type="button", style="calcui_button_style", caption="9", name="calcui_button_9"})
  100 + row2.add({type="button", style="calcui_button_style", caption="*", name="calcui_button_MUL"})
  101 +
  102 + local row3 = col1.add({type="flow", name="calcui_col1_row3", direction="horizontal"})
  103 + row3.add({type="button", style="calcui_button_style", caption="4", name="calcui_button_4"})
  104 + row3.add({type="button", style="calcui_button_style", caption="5", name="calcui_button_5"})
  105 + row3.add({type="button", style="calcui_button_style", caption="6", name="calcui_button_6"})
  106 + row3.add({type="button", style="calcui_button_style", caption="-", name="calcui_button_SUB"})
  107 +
  108 + local row4 = col1.add({type="flow", name="calcui_col1_row4", direction="horizontal"})
  109 + row4.add({type="button", style="calcui_button_style", caption="1", name="calcui_button_1"})
  110 + row4.add({type="button", style="calcui_button_style", caption="2", name="calcui_button_2"})
  111 + row4.add({type="button", style="calcui_button_style", caption="3", name="calcui_button_3"})
  112 + row4.add({type="button", style="calcui_button_style", caption="+", name="calcui_button_ADD"})
  113 +
  114 + local row5 = col1.add({type="flow", name="calcui_col1_row5", direction="horizontal"})
  115 + row5.add({type="button", style="calcui_button_style", caption="%", name="calcui_button_PERC"})
  116 + row5.add({type="button", style="calcui_button_style", caption="0", name="calcui_button_0"})
  117 + row5.add({type="button", style="calcui_button_style", caption=".", name="calcui_button_DOT"})
  118 + row5.add({type="button", style="calcui_button_style", caption="=", name="calcui_button_EQU"})
  119 +
  120 +
  121 + local col2 = table.add({
  122 + type = "flow",
  123 + name = "calcui_table_col2",
  124 + direction = "vertical"
  125 + })
  126 +
  127 + local result = col2.add({
  128 + type = "label",
  129 + caption = "= ",
  130 + name = "calcui_display_result"
  131 + })
  132 + result.style.font = "default-large"
  133 +
  134 + local rant = col2.add({
  135 + type = "sprite",
  136 + name = "calcui_rant"
  137 + })
  138 +
  139 + col2.add({
  140 + type = "line",
  141 + direction = "horizontal"
  142 + })
  143 +
  144 + local scroll = col2.add({
  145 + type = "scroll-pane",
  146 + name = "calcui_scroll_pane"
  147 + })
  148 + scroll.style.height = 252
  149 +
  150 + local recents = scroll.add({
  151 + type = "table",
  152 + caption = "",
  153 + name = "calcui_result_table",
  154 + column_count = "2"
  155 + })
  156 + recents.style.column_alignments[1] = "right"
  157 +
  158 +
  159 + -- center the gui
  160 + calcui.force_auto_center()
  161 +end
  162 +
  163 +-- ----------------------------------------------------------------
  164 +function hide_calculator(player)
  165 + destroy_calculator(player)
  166 +end
  167 +
  168 +-- ----------------------------------------------------------------
  169 +function toggle_calculator(player)
  170 + local root = get_gui_root(player)
  171 + if root and root.calcui then
  172 + hide_calculator(player)
  173 + else
  174 + show_calculator(player)
  175 + end
  176 +end
  177 +
  178 +-- ----------------------------------------------------------------
  179 +function clear_equation(player)
  180 + local root = get_gui_root(player)
  181 + root.calcui.calcui_table.calcui_table_col1.calcui_display.text = ""
  182 +end
  183 +
  184 +-- ----------------------------------------------------------------
  185 +function process_ce_key(player, button)
  186 + local root = get_gui_root(player)
  187 + clear_equation(player)
  188 + root.calcui.calcui_table.calcui_table_col2.calcui_display_result.caption = "="
  189 +end
  190 +
  191 +-- ----------------------------------------------------------------
  192 +function process_c_key(player, button)
  193 + local root = get_gui_root(player)
  194 + process_ce_key(player, button)
  195 + root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.calcui_result_table.clear()
  196 +end
  197 +
  198 +-- ----------------------------------------------------------------
  199 +function process_backspace_key(player, button)
  200 + local root = get_gui_root(player)
  201 + root.calcui.calcui_table.calcui_table_col1.calcui_display.text = string.sub(root.calcui.calcui_table.calcui_table_col1.calcui_display.text, 1, -2)
  202 +end
  203 +
  204 +-- ----------------------------------------------------------------
  205 +function fix_equation(equation)
  206 + local result = equation
  207 +
  208 + -- fix math library shortcuts
  209 + local math_lib = {
  210 + ["abs"] = "math.abs",
  211 + ["acos"] = "math.acos",
  212 + ["asin"] = "math.asin",
  213 + ["atan"] = "math.atan",
  214 + ["ceil"] = "math.ceil",
  215 + ["floor"] = "math.floor",
  216 + ["cos"] = "math.cos",
  217 + ["sin"] = "math.sin",
  218 + ["tan"] = "math.tan",
  219 + ["deg"] = "math.deg",
  220 + ["rad"] = "math.rad",
  221 + ["exp"] = "math.log",
  222 + ["exp"] = "math.log",
  223 + ["min"] = "math.min",
  224 + ["max"] = "math.max",
  225 + ["modf"] = "math.modf",
  226 + ["sqrt"] = "math.sqrt",
  227 + ["huge"] = "math.huge",
  228 + ["pi"] = "math.pi"
  229 + }
  230 + for key, val in pairs(math_lib) do
  231 + result = result:gsub(key, val)
  232 + end
  233 +
  234 + -- fix percentage
  235 + result = result:gsub("(%%)", "/100")
  236 +
  237 + return result
  238 +end
  239 +
  240 +-- ----------------------------------------------------------------
  241 +function process_equal_key(player, button)
  242 + local root = get_gui_root(player)
  243 +
  244 + local original_equation = root.calcui.calcui_table.calcui_table_col1.calcui_display.text;
  245 +
  246 + equation = fix_equation(original_equation)
  247 +
  248 + -- just testing
  249 + --root.calcui.calcui_table.calcui_table_col1.calcui_display.text = equation
  250 +
  251 + if not (equation == nil or equation == "") then
  252 + local status, retval = pcall(function()
  253 + return load("return " .. equation)()
  254 + end)
  255 + root.calcui.calcui_table.calcui_table_col2.calcui_display_result.tooltip = retval
  256 + status, retval = pcall(function()
  257 + return tonumber(string.format("%." .. settings.get_player_settings(player)["calcui-decimal-places"].value .. "f", retval))
  258 + end)
  259 + if retval == nil or retval == "" then
  260 + status = false
  261 + end
  262 + if not status then
  263 + retval = "NaN"
  264 + show_rant(player, true)
  265 + else
  266 + if retval <= 0 then
  267 + show_rant(player, true)
  268 + else
  269 + show_rant(player, false)
  270 + end
  271 + end
  272 + root.calcui.calcui_table.calcui_table_col2.calcui_display_result.caption = "= " .. retval
  273 +
  274 + -- only write in recent table if actually a result
  275 + if status then
  276 + -- check last equation and only insert if not the same
  277 + local item_size = #root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.calcui_result_table.children
  278 +
  279 + 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
  280 + local recent_equation = root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.calcui_result_table.add({
  281 + type = "label",
  282 + name = "calcui_recent_equation_" .. item_size,
  283 + caption = original_equation,
  284 + tooltip = {"calculator-ui.recent_tooltip"}
  285 + })
  286 + local recent_result = root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.calcui_result_table.add({
  287 + type = "label",
  288 + name = "calcui_recent_result_" .. item_size,
  289 + caption = root.calcui.calcui_table.calcui_table_col2.calcui_display_result.caption
  290 + })
  291 + root.calcui.calcui_table.calcui_table_col2.calcui_scroll_pane.scroll_to_bottom()
  292 + end
  293 + end
  294 + end
  295 +end
  296 +
  297 +-- ----------------------------------------------------------------
  298 +function display_addchar(player, char)
  299 + local root = get_gui_root(player)
  300 + root.calcui.calcui_table.calcui_table_col1.calcui_display.text = root.calcui.calcui_table.calcui_table_col1.calcui_display.text .. char
  301 + show_rant(player, false)
  302 +end
  303 +
  304 +-- ----------------------------------------------------------------
  305 +local button_dispatch = {
  306 + ["CE"] = process_ce_key,
  307 + ["C"] = process_c_key,
  308 + ["BS"] = process_backspace_key,
  309 + --
  310 + ["EQU"] = process_equal_key
  311 +}
  312 +local button_addchar = {
  313 + ["DIV"] = "/",
  314 + --
  315 + ["7"] = "7",
  316 + ["8"] = "8",
  317 + ["9"] = "9",
  318 + ["MUL"] = "*",
  319 + --
  320 + ["4"] = "4",
  321 + ["5"] = "5",
  322 + ["6"] = "6",
  323 + ["SUB"] = "-",
  324 + --
  325 + ["1"] = "1",
  326 + ["2"] = "2",
  327 + ["3"] = "3",
  328 + ["ADD"] = "+",
  329 + --
  330 + ["PERC"] = "%",
  331 + ["0"] = "0",
  332 + ["DOT"] = "."
  333 +}
  334 +
  335 +function handle_calcui_click(event, player)
  336 + debug_print("handle_calcui_click()")
  337 + local event_name = event.element.name
  338 + local button_prefix = "calcui_button_"
  339 + local button_prefix_len = string.len(button_prefix)
  340 +
  341 + local recent_prefix = "calcui_recent_"
  342 + local recent_prefix_len = string.len(recent_prefix);
  343 +
  344 + -- calculator buttons
  345 + if string.sub(event_name, 1, button_prefix_len) == button_prefix then
  346 + show_rant(player, false)
  347 +
  348 + button = string.sub(event_name, button_prefix_len + 1 )
  349 + debug_print("handle_calcui_click button " .. button)
  350 + local dispatch_func = button_dispatch[button]
  351 + if dispatch_func then
  352 + dispatch_func(player, button)
  353 + end
  354 +
  355 + local addchar = button_addchar[button]
  356 + if addchar then
  357 + display_addchar(player, addchar)
  358 + end
  359 + -- close button
  360 + elseif event_name == "calcui_close" then
  361 + hide_calculator(player)
  362 + -- recent results
  363 + elseif string.sub(event_name, 1, recent_prefix_len) == recent_prefix then
  364 + if event.button == defines.mouse_button_type.left and
  365 + event.shift == true then
  366 + -- copy equation to display
  367 + local root = get_gui_root(player)
  368 + 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
  369 + root.calcui.calcui_table.calcui_table_col1.calcui_display.focus()
  370 + end
  371 + end
  372 +end
  373 +
  374 +-- ----------------------------------------------------------------
  375 +function calcui_on_gui_text_changed(event)
  376 + if event.element.name == "calcui_display" then
  377 + local player = game.players[event.player_index]
  378 + local root = get_gui_root(player)
  379 + if string.find(root.calcui.calcui_table.calcui_table_col1.calcui_display.text, "=") then
  380 + root.calcui.calcui_table.calcui_table_col1.calcui_display.text = root.calcui.calcui_table.calcui_table_col1.calcui_display.text:gsub("=", "")
  381 + process_equal_key(player)
  382 + end
  383 + end
  384 +end
  385 +
  386 +-- ----------------------------------------------------------------
  387 +function calcui_on_gui_location_changed(event)
  388 + if event.element.name == "calcui" then
  389 + local player = game.players[event.player_index]
  390 + local root = get_gui_root(player)
  391 + root.calcui.location = event.element.location
  392 + end
  393 +end
  394 +
  395 +
  396 +
  397 +
  398 +
  399 +
  400 +-- -- ----------------------------------------------------------------
  401 +-- local function process_number_key(player, num)
  402 + -- debug_print("process_number_key(" .. num .. ")")
  403 +
  404 + -- if global.calcui_context[player.index].in_left_of_decimal then
  405 + -- val = num / global.calcui_context[player.index].decimal_place
  406 + -- if global.calcui_context[player.index].in_data_entry then
  407 + -- global.calcui_context[player.index].current_value = global.calcui_context[player.index].current_value + val
  408 + -- else
  409 + -- global.calcui_context[player.index].current_value = val
  410 + -- end
  411 + -- global.calcui_context[player.index].decimal_place = global.calcui_context[player.index].decimal_place * 10
  412 + -- else
  413 + -- if global.calcui_context[player.index].in_data_entry then
  414 + -- global.calcui_context[player.index].current_value = global.calcui_context[player.index].current_value * 10 + num
  415 + -- else
  416 + -- global.calcui_context[player.index].current_value = tonumber(num)
  417 + -- end
  418 + -- end
  419 + -- global.calcui_context[player.index].in_data_entry = true
  420 + -- update_display(player)
  421 +-- end
  422 +
  423 +-- -- ----------------------------------------------------------------
  424 +-- local function process_decimal_key(player, key)
  425 + -- debug_print("process_decimal_key(" .. key .. ")")
  426 + -- if global.calcui_context[player.index].in_left_of_decimal then
  427 + -- return
  428 + -- end
  429 +
  430 + -- global.calcui_context[player.index].in_left_of_decimal = true
  431 + -- global.calcui_context[player.index].decimal_place = 10
  432 + -- update_display(player)
  433 +-- end
  434 +
  435 +-- -- ----------------------------------------------------------------
  436 +-- local function process_change_sign_key(player, key)
  437 + -- global.calcui_context[player.index].current_value = global.calcui_context[player.index].current_value * -1
  438 + -- update_display(player)
  439 +-- end
  440 +
  441 +-- -- ----------------------------------------------------------------
  442 +-- local function process_equal_key(player, key)
  443 + -- debug_print("process_equal_key(" .. key .. ") global.calcui_context[player.index].current_func is " .. global.calcui_context[player.index].current_func)
  444 +
  445 + -- global.calcui_context[player.index].in_data_entry = false
  446 + -- doing_decimal = false
  447 +
  448 + -- if global.calcui_context[player.index].current_func == "ADD" then
  449 + -- global.calcui_context[player.index].current_value = global.calcui_context[player.index].operand + global.calcui_context[player.index].current_value
  450 + -- elseif global.calcui_context[player.index].current_func == "SUB" then
  451 + -- global.calcui_context[player.index].current_value = global.calcui_context[player.index].operand - global.calcui_context[player.index].current_value
  452 + -- elseif global.calcui_context[player.index].current_func == "MUL" then
  453 + -- global.calcui_context[player.index].current_value = global.calcui_context[player.index].operand * global.calcui_context[player.index].current_value
  454 + -- elseif global.calcui_context[player.index].current_func == "DIV" then
  455 + -- debug_print("process_equal_key global.calcui_context[player.index].current_value " .. global.calcui_context[player.index].current_value)
  456 +
  457 + -- if global.calcui_context[player.index].current_value == 0 then
  458 + -- debug_print("process_equal_key DIVIDE BY ZERO")
  459 + -- update_display(player, {"calcui_divide_by_zero"}) -- TODO: localize
  460 + -- return
  461 + -- end
  462 + -- global.calcui_context[player.index].current_value = global.calcui_context[player.index].operand / global.calcui_context[player.index].current_value
  463 + -- end
  464 + -- global.calcui_context[player.index].current_func = ""
  465 + -- global.calcui_context[player.index].in_data_entry = false
  466 + -- update_display(player)
  467 +-- end
  468 +
  469 +-- -- ----------------------------------------------------------------
  470 +-- local function process_func_key(player, key)
  471 + -- debug_print("process_func_key(" .. key .. ")")
  472 +
  473 + -- if global.calcui_context[player.index].current_func ~= "" then
  474 + -- process_equal_key(player, key)
  475 + -- end
  476 +
  477 + -- global.calcui_context[player.index].current_func = key
  478 + -- global.calcui_context[player.index].operand = global.calcui_context[player.index].current_value
  479 + -- global.calcui_context[player.index].in_data_entry = false
  480 + -- global.calcui_context[player.index].in_left_of_decimal = false
  481 +-- end
  482 +
  483 +-- -- ----------------------------------------------------------------
  484 +-- local function process_mem_key(player, key)
  485 + -- debug_print("process_mem_key(" .. key .. ")")
  486 + -- if calculator_memory == nil then
  487 + -- debug_print("process_mem_key(" .. key .. ") calculator_memory was nil")
  488 + -- calculator_memory = 0
  489 + -- end
  490 + -- if key == "MS" then
  491 + -- calculator_memory = global.calcui_context[player.index].current_value
  492 + -- elseif key == "M+" then
  493 + -- calculator_memory = calculator_memory + global.calcui_context[player.index].current_value
  494 + -- elseif key == "M-" then
  495 + -- calculator_memory = calculator_memory - global.calcui_context[player.index].current_value
  496 + -- else
  497 + -- global.calcui_context[player.index].current_value = calculator_memory
  498 + -- end
  499 + -- global.calcui_context[player.index].in_data_entry = false
  500 + -- update_display(player)
  501 +-- end
  502 +
  503 +-- -- ----------------------------------------------------------------
  504 +-- local function process_edit_key(player, key)
  505 + -- debug_print("process_edit_key(" .. key .. ")")
  506 + -- if key == "CE" then
  507 + -- global.calcui_context[player.index].current_value = 0
  508 + -- elseif key == "C" then
  509 + -- init_calculator(player)
  510 + -- end
  511 + -- global.calcui_context[player.index].in_data_entry = false
  512 + -- update_display(player)
  513 +-- end
  514 +
  515 +-- -- ----------------------------------------------------------------
  516 +-- local function set_current_value_to_text(player, text)
  517 + -- local last_char = string.sub(text, -1)
  518 + -- if last_char == "+" then
  519 + -- process_func_key(player, "ADD")
  520 + -- elseif last_char == "-" then
  521 + -- process_func_key(player, "SUB")
  522 + -- elseif last_char == "*" then
  523 + -- process_func_key(player, "MUL")
  524 + -- elseif last_char == "/" then
  525 + -- process_func_key(player, "DIV")
  526 + -- else
  527 + -- -- number
  528 + -- local val = tonumber(text)
  529 + -- if val ~= nil then
  530 + -- global.calcui_context[player.index].current_value = val
  531 + -- debug_print("set_current_value_to_text got value " .. text)
  532 + -- local plain_text = true
  533 + -- local dot = string.find(text, '.', 1, plain_text)
  534 + -- if dot ~= nil then
  535 + -- local number_decimal_places = #text - dot
  536 + -- global.calcui_context[player.index].decimal_place = 10 ^ (number_decimal_places + 1)
  537 + -- global.calcui_context[player.index].in_left_of_decimal = true
  538 + -- debug_print("#text " .. #text .. " number_global.calcui_context[player.index].decimal_places " ..
  539 + -- 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))
  540 + -- else
  541 + -- debug_print("set_current_value_to_text no dot. set lod to false" )
  542 + -- global.calcui_context[player.index].in_left_of_decimal = false
  543 + -- end
  544 + -- else
  545 + -- debug_print("got junk")
  546 + -- end
  547 + -- end
  548 +-- end
  549 +
  550 +-- -- ----------------------------------------------------------------
  551 +-- local function process_backspace_key(player, key)
  552 + -- local root = get_gui_root(player)
  553 + -- debug_print("process_backspace_key(" .. key .. ")")
  554 + -- local old_text = root.calcui.calcui_display.caption
  555 + -- local old_len = #old_text
  556 + -- text = string.sub(old_text, 1, old_len -1)
  557 + -- debug_print("process_backspace_key(" .. key .. ") old_text " .. old_text .. " old_len " .. old_len .. " text " .. text ..
  558 + -- " 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)
  559 + -- if #text == 0 then
  560 + -- global.calcui_context[player.index].current_value = 0
  561 + -- else
  562 + -- set_current_value_to_text(player, text)
  563 + -- end
  564 + -- update_display(player)
  565 +-- end
  566 +
  567 +-- -- ----------------------------------------------------------------
  568 +-- local function calcui_toggle_key(event)
  569 + -- local player = game.players[event.player_index]
  570 + -- toggle_calculator(player)
  571 +-- end
  572 +
  573 +-- -- ----------------------------------------------------------------
  574 +-- function calcui_clickable_value_clicked(player, val) -- a value in the main Max Rate Calculator has been clicked on. Paste into current value
  575 + -- local root = get_gui_root(player)
  576 + -- if root ~=nil and root.calcui ~= nil then
  577 + -- local text = tostring(val)
  578 + -- set_current_value_to_text(player, text)
  579 + -- update_display(player)
  580 + -- end
  581 +-- end
  582 +
  583 +-- script.on_event( "calcui_toggle", calcui_toggle_key )
0 \ No newline at end of file 584 \ No newline at end of file
control.lua 0 → 100644
  1 +++ a/control.lua
  1 +-- control.lua
  2 +
  3 +require("calculator")
  4 +
  5 +-- ----------------------------------------------------------------
  6 +function boolstr(bool)
  7 + if bool then
  8 + return "T"
  9 + else
  10 + return "F"
  11 + end
  12 +end
  13 +
  14 +-- ----------------------------------------------------------------
  15 +function debug_print(str)
  16 + if global.marc_debug then
  17 + game.print(str)
  18 + end
  19 +end
  20 +
  21 +function __FUNC__() return debug.getinfo(2, 'n').name end
  22 +
  23 +function debug_log(f, str)
  24 + if global.marc_debug then
  25 + game.print(f .. ": " .. str)
  26 + end
  27 +end
  28 +
  29 +-- ----------------------------------------------------------------
  30 +local function get_gui_root(player)
  31 + -- return player.gui.left
  32 + return player.gui.screen
  33 +end
  34 +
  35 +-- ----------------------------------------------------------------
  36 +function destroy_calcui_gui(player)
  37 + local root = get_gui_root(player)
  38 + root.calcui_gui_top.destroy()
  39 +end
  40 +
  41 +-- ----------------------------------------------------------------
  42 +local function shortcut(event)
  43 + if event.prototype_name == "calcui_4func" then
  44 + local player = game.players[event.player_index]
  45 + toggle_calculator(player)
  46 + end
  47 +end
  48 +
  49 +-- ----------------------------------------------------------------
  50 +-- user has clicked somewhere. If clicked on any gui item name that starts with "calcui_..."
  51 +-- hide the gui
  52 +local function on_gui_click(event)
  53 + local event_name = event.element.name
  54 + debug_print("event_name " .. event_name)
  55 + -- local calcui_prefix = "calcui_"
  56 + -- local possible_calcui_prefix = string.sub( event_name, 1, string.len(calcui_prefix) )
  57 + local player = game.players[event.player_index]
  58 + -- local root = get_gui_root(player)
  59 +
  60 + local calcui_prefix = "calcui_"
  61 + local possible_marcalc_prefix = string.sub( event_name, 1, string.len(calcui_prefix))
  62 + if possible_marcalc_prefix == calcui_prefix then
  63 + handle_calcui_click(event, player)
  64 + return
  65 + end
  66 +
  67 + -- if possible_calcui_prefix == calcui_prefix then
  68 + -- if event_name == "calcui_calculator_button" then
  69 + -- toggle_calculator(player)
  70 + -- return
  71 + -- end
  72 +
  73 + -- if root.calcui_gui_top then
  74 + -- if event_name == "calcui_close_button" then
  75 + -- destroy_calcui_gui(player)
  76 + -- hide_calculator(player)
  77 + -- end
  78 + -- elseif event_name == "calcui_close_button" then
  79 + -- if player.gui.left.calcui_gui_top then
  80 + -- player.gui.left.calcui_gui_top.destroy()
  81 + -- hide_calculator(player)
  82 + -- end
  83 + -- end
  84 + -- end
  85 +end
  86 +
  87 +-- ----------------------------------------------------------------
  88 +-- user has confirmed the textfield / Called when a LuaGuiElement is confirmed, for example by pressing Enter in a textfield.
  89 +local function on_gui_confirmed(event)
  90 + player = game.players[event.player_index];
  91 + if event.element.name == "calcui_display" then
  92 + process_equal_key(player)
  93 + if settings.get_player_settings(player)["calcui-enter-clear"].value then
  94 + clear_equation(player)
  95 + end
  96 + end
  97 +end
  98 +
  99 +-- ----------------------------------------------------------------
  100 +local function on_calcui_command(event)
  101 + if event.parameter == "debug" then
  102 + global.calcui_debug = true
  103 + debug_print("calcui debugging is on")
  104 + elseif event.parameter == "nodebug" then
  105 + debug_print("calcui debugging is off")
  106 + global.calcui_debug = false
  107 + elseif event.parameter == nil then
  108 + game.players[event.player_index].print("please add a parameter")
  109 + else
  110 + game.players[event.player_index].print("unknown calcui parameter: " .. event.parameter)
  111 + end
  112 +end
  113 +
  114 +-- ----------------------------------------------------------------
  115 +--script.on_event( "calcui_hotkey", on_hotkey_main )
  116 +script.on_event( defines.events.on_lua_shortcut, shortcut )
  117 +script.on_event( defines.events.on_gui_click, on_gui_click)
  118 +script.on_event( defines.events.on_gui_confirmed, on_gui_confirmed)
  119 +script.on_event( defines.events.on_gui_text_changed, calcui_on_gui_text_changed )
  120 +script.on_event( defines.events.on_gui_location_changed, calcui_on_gui_location_changed )
  121 +commands.add_command( "calcui", "Max Rate Calculator [ debug | nodebug ] ", on_calcui_command )
0 \ No newline at end of file 122 \ No newline at end of file
data.lua 0 → 100644
  1 +++ a/data.lua
  1 +--data.lua
  2 +
  3 +require("calcui-prototypes")
  4 +require("calcui-styles")
  5 +require("calcui-hotkey")
0 \ No newline at end of file 6 \ No newline at end of file
graphics/calculator.png 0 → 100644

387 Bytes

graphics/nilausRant.png 0 → 100644

6.86 KB

info.json 0 → 100644
  1 +++ a/info.json
  1 +{
  2 + "name": "calculator-ui",
  3 + "version": "0.18.0",
  4 + "title": "Calculator UI",
  5 + "author": "Wichu",
  6 + "factorio_version": "0.18",
  7 + "dependencies": ["base >= 0.18"],
  8 + "description": "This mod adds a calculator with advanced controls to the UI."
  9 +}
0 \ No newline at end of file 10 \ No newline at end of file
locale/en/config.cfg 0 → 100644
  1 +++ a/locale/en/config.cfg
  1 +[shortcut-name]
  2 +calcui_4func=Calculator
  3 +
  4 +[mod-setting-name]
  5 +calcui-decimal-places=Decimal places
  6 +calcui-enter-clear=Clear equation on enter?
  7 +
  8 +[mod-settings-description]
  9 +calcui-decimal-places=Number of decimal places in the result
  10 +calcui-enter-clear=Should pressing the enter key delete the equation?
  11 +
  12 +[calculator-ui]
  13 +title=Calculator
  14 +recent_tooltip=Press shift+left-click to copy it to current equation
0 \ No newline at end of file 15 \ No newline at end of file
settings.lua 0 → 100644
  1 +++ a/settings.lua
  1 +data:extend({
  2 + {
  3 + type = "int-setting",
  4 + default_value = 2,
  5 + minimum_value = 0,
  6 + maximum_value = 10,
  7 + name = "calcui-decimal-places",
  8 + setting_type = "runtime-per-user",
  9 + default_value = true
  10 + },
  11 + {
  12 + type = "bool-setting",
  13 + default_value = 0,
  14 + name = "calcui-enter-clear",
  15 + setting_type = "runtime-per-user",
  16 + default_value = false
  17 + }
  18 +})
0 \ No newline at end of file 19 \ No newline at end of file