From 0f3062c10868136edb12795b30919a59970d275f Mon Sep 17 00:00:00 2001 From: Jonas Date: Thu, 29 Nov 2018 15:56:01 +0100 Subject: [PATCH] Feature/1 initial tests (#6) closes #2 . Adding entities is possible now closes #3 automatic updates of values fixed crash when removing entity --- changelog.txt | 2 + locale/de/locale.cfg | 68 ++--------- locale/en/locale.cfg | 68 ++--------- src/control.lua | 25 +++-- src/data.lua | 11 +- src/pmon/pmon.lua | 243 ++++++++++++++++++++++++++++++++++++++++ src/pmon/prototypes.lua | 46 ++++++++ src/pmon/style.lua | 38 +++++++ src/settings.lua | 3 +- 9 files changed, 366 insertions(+), 138 deletions(-) create mode 100644 changelog.txt create mode 100644 src/pmon/pmon.lua create mode 100644 src/pmon/prototypes.lua create mode 100644 src/pmon/style.lua diff --git a/changelog.txt b/changelog.txt new file mode 100644 index 0000000..3697e8b --- /dev/null +++ b/changelog.txt @@ -0,0 +1,2 @@ +16.1.0 +------------------ diff --git a/locale/de/locale.cfg b/locale/de/locale.cfg index 497105b..c74a5bb 100644 --- a/locale/de/locale.cfg +++ b/locale/de/locale.cfg @@ -1,62 +1,12 @@ -[todo] -todo_list=Todo List -title_done=Status -title_task=Aufgabe -title_assignee=Zugewiesen an -title_edit=Bearbeiten -title_details=Details - -minimize=Minimieren -add=Neue Aufgabe -add_assignee=Bearbeiter -add_task=Aufgabe -add_title=Neue Aufgabe anlegen -add_task_title=Titel -add_top=Oben hinzufügen - -edit_title=Aufgabe ändern - -persist=Speichern +[pmon] +title=PowerMon +add_monitor=Überwachung hinzufügen +add_monitor_name=Anzeigename cancel=Abbrechen -update=Speichern -delete=Löschen -confirm_deletion=Wirklich löschen - -import=Importieren -export=Exportieren - -unassigned=Nicht zugewiesen -assign_self=Mir zuweisen -show_done=Zeige abgeschlossene Aufgaben -hide_done=Verberge abgeschlossene Aufgaben - -tasks_available= __1__ Aufgaben verfügbar - -created_by=Erstellt von -updated_by=Geändert von -noone=Niemand - -add_subtask=Unteraufgabe erstellen -edit_subtask=Unteraufgabe bearbeiten -delete_subtask=Unteraufgabe löschen - -[controls] -todolist-toggle-ui=Todo Liste minimieren/maximieren - -[mod-setting-name] -todolist-show-button=Zeige "Todo List" Schalter. -todolist-show-log=Zeige debug Nachrichten im Chatfenster an. -todolist-window-height=Fensterhöhe -todolist-auto-assign=Auto-assign tasks -todolist-click-edit-task=Edit task on middle click +add=Speichern -[mod-setting-description] -todolist-show-button=Wenn aktiviert wird immer ein "Todo List" Schalter in der oberen linken Ecke angezeigt.\nWenn deaktiviert kann die Todo Liste nur über den Hotkey (standard shift+t) aufgerufen werden. -todolist-show-log=Wenn aktiviert werden debug Nachrichten des mods im Chatfenster angezeigt.\nAchtung: Es sind sehr viele Nachrichten. -todolist-window-height=Die Höhe des TodoList Fensters. Standard ist 600. -todolist-auto-assign=If there is only one player in the game, auto-assign the task to this player. -todolist-click-edit-task=If enabled the middle click will open the edit popup. Default on right click. +[technology-name] +pmon-power-monitoring=Power Monitoring -[string-mod-setting] -todolist-click-edit-task-right-button=Right button -todolist-click-edit-task-middle-button=Middle button \ No newline at end of file +[item-name] +pmon-power-monitor=Power Monitor \ No newline at end of file diff --git a/locale/en/locale.cfg b/locale/en/locale.cfg index 24b2bd4..d2dda2b 100644 --- a/locale/en/locale.cfg +++ b/locale/en/locale.cfg @@ -1,62 +1,12 @@ -[todo] -todo_list=Todo List -title_done=Done -title_task=Task -title_assignee=Assignee -title_edit=Edit -title_details=Details - -minimize=Minimize -add=Add new -add_assignee=Assignee -add_task=Task -add_title=Add new task -add_task_title=Title -add_top=Add to Top - -edit_title=Edit task - -persist=Save +[pmon] +title=PowerMon +add_monitor=Add monitor +add_monitor_name=Displayname cancel=Cancel -update=Update -delete=Delete -confirm_deletion=Confirm deletion - -import=import -export=export - -unassigned=Unassigned -assign_self=Take -show_done=Show completed Tasks -hide_done=Hide completed Tasks - -tasks_available=__1__ Tasks available - -created_by=Created by -updated_by=Modified by -noone=No One - -add_subtask=Create subtask -edit_subtask=Edit subtask -delete_subtask=Delete subtask - -[controls] -todolist-toggle-ui=Min/maximize todo list - -[mod-setting-name] -todolist-show-button=Show "Todo List" button. -todolist-show-log=Show debug log messages in the chat window. -todolist-window-height=Todo list window height -todolist-auto-assign=Auto-assign tasks -todolist-click-edit-task=Edit task on middle click +add=Save -[mod-setting-description] -todolist-show-button=If enabled will always show a "Todo List" button in the top left.\nIf disabled the todo list can only be accessed by its hotkey (default shift-t). -todolist-show-log=If enabled the mod will print debug messages to the chat window.\nAttention: there are a lot of them :) -todolist-window-height=The height of the todo list window. Default is 600. -todolist-auto-assign=If there is only one player in the game, auto-assign the task to this player. -todolist-click-edit-task=If enabled the middle click will open the edit popup. Default on right click. +[technology-name] +pmon-power-monitoring=Power Monitoring -[string-mod-setting] -todolist-click-edit-task-right-button=Right button -todolist-click-edit-task-middle-button=Middle button \ No newline at end of file +[item-name] +pmon-power-monitor=Power Monitor \ No newline at end of file diff --git a/src/control.lua b/src/control.lua index 21ffaaa..3ae463d 100644 --- a/src/control.lua +++ b/src/control.lua @@ -1,32 +1,37 @@ require "mod-gui" -require "todo/todo" +require "pmon/pmon" -- when creating a new game, initialize data structure -script.on_init(todo.mod_init) +script.on_init(pmon.mod_init) -- When a player is joining, create the UI for them -script.on_event(defines.events.on_player_created, function(event) +--[[script.on_event(defines.events.on_player_created, function(event) local player = game.players[event.player_index] - todo.create_maximize_button(player) -end) + pmon.create_ui(player) +end)]]-- -- if the version of the mod or any other version changed script.on_configuration_changed(function(_) - todo.mod_init() + pmon.mod_init() end) script.on_event(defines.events.on_gui_click, function(event) - todo.on_gui_click(event) + pmon.on_gui_click(event) end) -script.on_event("todolist-toggle-ui", function(event) - local player = game.players[event.player_index] - todo.toggle_main_frame(player) +script.on_event(defines.events.on_tick, function(event) + pmon.on_tick(event) +end) + +script.on_event(defines.events.on_player_selected_area, function(event) + pmon.on_player_selected_area(event) end) +--[[ script.on_event(defines.events.on_runtime_mod_setting_changed, function(event) local player = game.players[event.player_index] local key = event.setting todo.on_runtime_mod_setting_changed(player, key) end) +]]-- diff --git a/src/data.lua b/src/data.lua index b918bd0..d644e23 100644 --- a/src/data.lua +++ b/src/data.lua @@ -1,9 +1,2 @@ -require('todo.style') - -local hotkey = { - type = "custom-input", - name = "todolist-toggle-ui", - key_sequence = "SHIFT + T", - consuming = "none", -} -data:extend({hotkey}) +require('pmon.style') +require('pmon.prototypes') diff --git a/src/pmon/pmon.lua b/src/pmon/pmon.lua new file mode 100644 index 0000000..940ca48 --- /dev/null +++ b/src/pmon/pmon.lua @@ -0,0 +1,243 @@ +pmon = {} + +function pmon.mod_init() + if not global.pmon then + global.pmon = {} + end +end + +function pmon.on_tick(event) + if (event.tick % 10 == 0) then + + pmon.mod_init() + + local count = 0 + + for _, monitor in pairs(global.pmon) do + + if (monitor.entity.valid) then + pmon.update_data(monitor) + else + game.players[1].print("removing monitor" .. monitor.title) + global.pmon[monitor.name] = nil + end + + count = count + 1 + end + + -- update UI + if (count > 0) then + for _, player in pairs(game.players) do + pmon.update_ui(player) + end + end + end +end + +function pmon.update_data(monitor) + if (monitor.type == "solar-panel") then + monitor.value = (0.85 - monitor.entity.surface.darkness) / 0.85 + elseif (monitor.type == "accumulator") then + local max = monitor.entity.electric_buffer_size + local current = monitor.entity.energy + + monitor.value = current / max + end +end + +function pmon.update_ui(player) + local frame = pmon.ensure_main_frame(player) + + for _, monitor in pairs(global.pmon) do + pmon.ensure_static_info(frame, monitor) + local bar = pmon.ensure_progress_bar(frame, monitor) + bar.value = monitor.value + -- TODO: color? + end +end + +function pmon.ensure_main_frame(player) + + local frame_flow = mod_gui.get_frame_flow(player) + if (not frame_flow.pmon_main_frame) then + frame_flow.add({ + type = "frame", + name = "pmon_main_frame", + direction = "vertical" + }) + end + + return frame_flow.pmon_main_frame +end + +function pmon.ensure_static_info(frame, monitor) + local flow = pmon.ensure_horizontal_flow(frame, monitor) + pmon.ensure_monitor_title(flow, monitor) + --pmon.ensure_monitor_delete_button(flow, monitor) +end + +function pmon.ensure_horizontal_flow(frame, monitor) + + local flow_name = "pmon_" .. monitor.name .. "_flow" + + if (frame[flow_name]) then + return frame[flow_name] + end + + return frame.add({ + type = "flow", + name = flow_name, + direction = "horizontal" + }) +end + +function pmon.ensure_monitor_title(flow, monitor) + local title_name = "pmon" .. monitor.name .. "_title" + + if (flow[title_name]) then + return + end + + flow.add({ + type = "label", + style = "pmon_label_default", + name = title_name, + caption = monitor.title + }) +end + +function pmon.ensure_monitor_delete_button(flow, monitor) + +end + +function pmon.ensure_progress_bar(frame, monitor) + local bar_name = "pmon_" .. monitor.name .. "_progress_bar" + + if (frame[bar_name]) then + return frame[bar_name] + end + + local bar = frame.add({ + type = "progressbar", + name = bar_name, + value = monitor.value + }) + bar.style.color = { r = 0, g = 1, b = 0, a = 1 } + bar.style.horizontally_stretchable = true + return bar +end + +function pmon.on_gui_click(event) + local player = game.players[event.player_index] + local element = event.element + + if (element.name == "pmon_add_cancel_button") then + pmon.get_add_dialog(player).destroy() + elseif (element.name == "pmon_add_monitor_button") then + pmon.add_monitor(player) + pmon.get_add_dialog(player).destroy() + end +end + +function pmon.add_monitor(player) + local dialog = pmon.get_add_dialog(player) + local entity = dialog.pmon_add_entity.entity + local title = dialog.pmon_add_dialog_flow.pmon_add_monitor_title.text + + local monitor = {} + monitor.type = entity.prototype.type + monitor.entity = entity + monitor.title = title + monitor.name = string.gsub(title, " ", "_") + monitor.value = 0 + + global.pmon[monitor.name] = monitor +end + +function pmon.create_add_dialog(player, entity) + local gui = player.gui.center + + local old_dialog = pmon.get_add_dialog(player) + if (old_dialog ~= nil) then + old_dialog.destroy() + end + + local dialog = gui.add({ + type = "frame", + name = "pmon_add_dialog", + caption = { "pmon.add_monitor" }, + direction = "vertical" + }) + + local preview = dialog.add({ + type = "entity-preview", + name = "pmon_add_entity" + }) + + preview.entity = entity + + local flow = dialog.add({ + type = "flow", + name = "pmon_add_dialog_flow", + direction = "horizontal" + }) + + flow.add({ + type = "label", + name = "pmon_add_label", + caption = { "pmon.add_monitor_name" } + }) + + flow.add({ + type = "textfield", + style = "pmon_textfield_default", + name = "pmon_add_monitor_title" + }) + + local button_flow = dialog.add({ + type = "flow", + name = "pmon_add_dialog_button_flow", + direction = "horizontal" + }) + + local ok = button_flow.add({ + type = "button", + style = "pmon_button_default", + name = "pmon_add_monitor_button", + caption = { "pmon.add" } + }) + + button_flow.add({ + type = "button", + style = "pmon_button_default", + name = "pmon_add_cancel_button", + caption = { "pmon.cancel" } + }) +end + +function pmon.get_add_dialog(player) + local gui = player.gui.center + if gui.pmon_add_dialog then + return gui.pmon_add_dialog + else + return nil + end +end + +function pmon.on_player_selected_area(event) + player = game.players[event.player_index] + + if player.cursor_stack.name == "pmon-power-monitor" then + local entity = event.entities[1] + + if (entity) then + local type = entity.prototype.type + if (type == "solar-panel" or type == "accumulator") then + player.clean_cursor() + pmon.create_add_dialog(player, entity) + else + player.print("Currently only solar panels and accumulators are supported.") + end + end + end +end \ No newline at end of file diff --git a/src/pmon/prototypes.lua b/src/pmon/prototypes.lua new file mode 100644 index 0000000..0f06c7b --- /dev/null +++ b/src/pmon/prototypes.lua @@ -0,0 +1,46 @@ +local technology = { + type = "technology", + name = "pmon-power-monitoring", + icon = "__core__/graphics/electricity-icon-red.png", + icon_size = 64, + effects = { + { + type = "unlock-recipe", + recipe = "pmon-power-monitor" + } + }, + prerequisites = { "electric-energy-accumulators-1" }, + unit = table.deepcopy(data.raw.technology['electric-energy-accumulators-1'].unit), + order = table.deepcopy(data.raw.technology['electric-energy-accumulators-1'].order), +} +technology.unit.count = technology.unit.count * 3 + +local recipe = { + type = "recipe", + name = "pmon-power-monitor", + enabled = false, --this one may be set to TRUE to make recipe available from the start + ingredients ={ + {'advanced-circuit', 5}, {'red-wire', 50}, {'green-wire', 50}, + }, + result = "pmon-power-monitor" +} + +local item = { + type = "selection-tool", + name = "pmon-power-monitor", + icon = "__core__/graphics/electricity-icon-red.png", + icon_size = 64, + flags = {"goes-to-quickbar"}, + subgroup = "tool", + damage_radius = 5, + order = "zz", + stack_size = 1, + selection_color = { r = 0, g = 1, b = 0 }, + alt_selection_color = { r = 0, g = 1, b = 0 }, + selection_mode = {"blueprint"}, + alt_selection_mode = {"blueprint"}, + selection_cursor_box_type = "copy", + alt_selection_cursor_box_type = "copy" +} + +data:extend({technology, recipe, item}) \ No newline at end of file diff --git a/src/pmon/style.lua b/src/pmon/style.lua new file mode 100644 index 0000000..d39caf8 --- /dev/null +++ b/src/pmon/style.lua @@ -0,0 +1,38 @@ +local default_gui = data.raw["gui-style"].default + +data:extend({ + { + type = "font", + name = "pmon_font_default", + from = "default", + size = 14 + }, +}) + +default_gui["pmon_button_default"] = { + type = "button_style", + font = "pmon_font_default", + align = "center", + vertical_align = "center" +} + +default_gui["pmon_sprite_button_default"] = { + type = "button_style", + parent = "icon_button", + font = "pmon_font_default", + align = "center", + vertical_align = "center", + height = 36 +} + +default_gui["pmon_label_default"] = { + type = "label_style", + font = "pmon_font_default", + minimal_width = 150 +} + +default_gui["pmon_textfield_default"] = { + type = "textfield_style", + font = "pmon_font_default", + minimal_width = 300 +} diff --git a/src/settings.lua b/src/settings.lua index e7a4eb1..c8dd64d 100644 --- a/src/settings.lua +++ b/src/settings.lua @@ -1,4 +1,4 @@ -data:extend({ +--[[data:extend({ { name = "todolist-show-button", setting_type = "runtime-per-user", @@ -36,3 +36,4 @@ data:extend({ per_user = true, } }) +]]--