From 5e302c74812e173cafa5e68a929a7773436cf3f5 Mon Sep 17 00:00:00 2001 From: Octav Sandulescu Date: Sat, 1 Aug 2015 18:28:41 +0000 Subject: [PATCH] Add a settings screen, and toggled popup. Closes #6. Closes #5. --- evoGUI.lua | 151 ++++++++++++++++++++++++++----------- script-locale/en.cfg | 4 + settingsGUI.lua | 80 ++++++++++++++++++++ value_sensors/template.lua | 2 +- 4 files changed, 193 insertions(+), 44 deletions(-) create mode 100644 settingsGUI.lua diff --git a/evoGUI.lua b/evoGUI.lua index 5cf98cb..17b6fbd 100644 --- a/evoGUI.lua +++ b/evoGUI.lua @@ -2,40 +2,40 @@ require "defines" require "value_sensors.day_time" require "value_sensors.evolution_factor" require "value_sensors.play_time" +require "settingsGUI" if not evogui then evogui = {} end if not evogui.on_click then evogui.on_click = {} end evogui.update_delay = 60 -- ticks to wait between each GUI update +local EXPECTED_VERSION = "0.3.0" -function evogui.update_gui() - if (game.tick % evogui.update_delay) == 0 then - for i, player in ipairs(game.players) do - evogui.create_or_update(player) - if not player.gui.top.evoGUI then return end +function evogui.create_player_globals(player) + if not global.evogui then global.evogui = {} end + if not global.evogui[player.name] then global.evogui[player.name] = {} end + local player_settings = global.evogui[player.name] - local sensors = player.gui.top.evoGUI.sensors - if not sensors then return end - - if sensors.evolution_factor then - evogui.update_evolution(sensors.evolution_factor) - end - if sensors.play_time then - evogui.update_play_time(sensors.play_time) - end - if sensors.day_time then - evogui.update_day_time(sensors.day_time) - end - end + if not player_settings.version then player_settings.version = "" end + if not player_settings.always_visible then + player_settings.always_visible = { + ["evolution_factor"] = true, + ["play_time"] = true, + } + end + if not player_settings.in_popup then + player_settings.in_popup = { + ["day_time"] = true, + } end + if not player_settings.popup_open then player_settings.popup_open = false end end -function evogui.create_or_update(player) + +function evogui.create_sensor_display(player) local root = player.gui.top.evoGUI local destroyed = false - if root and (not global.evoGUI or not global.evoGUI[player.name] or - not global.evoGUI[player.name].version or global.evoGUI[player.name].version ~= "0.3.0") then + if root and global.evogui[player.name].version ~= EXPECTED_VERSION then player.gui.top.evoGUI.destroy() destroyed = true end @@ -44,37 +44,102 @@ function evogui.create_or_update(player) local root = player.gui.top.add{type="frame", name="evoGUI", direction = "horizontal", style="outer_frame_style"} local action_buttons = root.add{type="flow", name="action_buttons", direction = "vertical", style="description_flow_style"} - action_buttons.add{type="button", name="evoGUI_size", caption="-", style="evoGUI_small_button_style"} + action_buttons.add{type="button", name="evoGUI_popup", caption="+", style="evoGUI_small_button_style"} action_buttons.add{type="button", name="evoGUI_settings", caption="s", style="evoGUI_small_button_style"} - local sensors = root.add{type="flow", name="sensors", direction = "vertical", style="description_flow_style"} - sensors.add{type="label", name="day_time"} - sensors.add{type="label", name="evolution_factor"} - sensors.add{type="label", name="play_time"} + local sensor_flow = root.add{type="flow", name="sensor_flow", direction = "vertical", style="description_flow_style"} + sensor_flow.add{type="flow", name="always_visible", direction = "vertical", style="description_flow_style"} + sensor_flow.add{type="flow", name="in_popup", direction = "vertical", style="description_flow_style"} - if not global.evoGUI then global.evoGUI = {} end - if not global.evoGUI[player.name] then global.evoGUI[player.name] = {} end - global.evoGUI[player.name].version = "0.3.0" + global.evogui[player.name].version = EXPECTED_VERSION end end -function evogui.on_click.evoGUI_size(event) + +function evogui.update_gui() + if (game.tick % evogui.update_delay) == 0 then + for i, player in ipairs(game.players) do + evogui.create_player_globals(player) + evogui.create_sensor_display(player) + + local player_settings = global.evogui[player.name] + + local sensor_flow = player.gui.top.evoGUI.sensor_flow + evogui.update_av(player, sensor_flow.always_visible) + if player_settings.popup_open then + evogui.update_ip(player, sensor_flow.in_popup) + end + end + end +end + + +function evogui.update_av(player, element) + local av_settings = global.evogui[player.name].always_visible + + for _, sensor in ipairs(evogui.value_sensors) do + -- should we show it? + if av_settings[sensor.name] ~= nil then + -- is it not showing yet? + if element[sensor.name] == nil then + element.add{type="label", name=sensor.name} + end + -- and update its display + element[sensor.name].caption = sensor:get_line() + else + -- is it showing? + if element[sensor.name] ~= nil then + element[sensor.name].destroy() + end + end + end +end + + +function evogui.update_ip(player, element) + if not global.evogui[player.name].popup_open then return end + + local ip_settings = global.evogui[player.name].in_popup + + for _, sensor in ipairs(evogui.value_sensors) do + -- should we show it? + if ip_settings[sensor.name] ~= nil then + -- is it not showing yet? + if element[sensor.name] == nil then + element.add{type="label", name=sensor.name} + end + -- and update its display + element[sensor.name].caption = sensor:get_line() + else + -- is it showing? + if element[sensor.name] ~= nil then + element[sensor.name].destroy() + end + end + end +end + + +function evogui.on_click.evoGUI_popup(event) local player = game.get_player(event.player_index) + local player_settings = global.evogui[player.name] - local sensors = player.gui.top.evoGUI.sensors - if sensors.evolution_factor ~= nil then - -- hide the extra bits - sensors.evolution_factor.destroy() - sensors.play_time.destroy() - player.gui.top.evoGUI.action_buttons.evoGUI_size.caption = "+" - else - -- show the extra bits - sensors.add{type="label", name="evolution_factor"} - evogui.update_evolution(sensors.evolution_factor) + local root = player.gui.top.evoGUI + + if player_settings.popup_open then + -- close it + player_settings.popup_open = false - sensors.add{type="label", name="play_time"} - evogui.update_play_time(sensors.play_time) + for _, childname in ipairs(root.sensor_flow.in_popup.children_names) do + root.sensor_flow.in_popup[childname].destroy() + end + + root.action_buttons.evoGUI_popup.caption = "+" + else + -- open it + player_settings.popup_open = true - player.gui.top.evoGUI.action_buttons.evoGUI_size.caption = "-" + evogui.update_ip(player, root.sensor_flow.in_popup) + root.action_buttons.evoGUI_popup.caption = "-" end end diff --git a/script-locale/en.cfg b/script-locale/en.cfg index e94b8c0..19eb140 100644 --- a/script-locale/en.cfg +++ b/script-locale/en.cfg @@ -7,3 +7,7 @@ sensor.evolution_factor.format=Biter evolution: __1__. sensor.play_time.name=Time played sensor.play_time.format=Play time: __1__. +settings_always_visible=Always visible +settings_in_popup=Visible in popup + +settings_close=Close diff --git a/settingsGUI.lua b/settingsGUI.lua new file mode 100644 index 0000000..b43e176 --- /dev/null +++ b/settingsGUI.lua @@ -0,0 +1,80 @@ +require "defines" + +if not evogui then evogui = {} end +if not evogui.on_click then evogui.on_click = {} end + +if not global.evogui then global.evogui = {} end + + +local function toggle_always_visible(event) + local player = game.get_player(event.player_index) + + if event.element.name:sub(1,3) ~= "AV_" then + error(string.format("toggle_always_visible called on the wrong thing: %s", event.element.name)) + return + end + + local always_visible = global.evogui[player.name].always_visible + local sensor_name = event.element.name:sub(4,-1) + if event.element.state then + always_visible[sensor_name] = true + else + always_visible[sensor_name] = nil + end +end + + +local function toggle_in_popup(event) + local player = game.get_player(event.player_index) + + if event.element.name:sub(1,3) ~= "IP_" then + error(string.format("toggle_in_popup called on the wrong thing: %s", event.element.name)) + return + end + + local in_popup = global.evogui[player.name].in_popup + local sensor_name = event.element.name:sub(4,-1) + if event.element.state then + in_popup[sensor_name] = true + else + in_popup[sensor_name] = nil + end +end + + +function evogui.on_click.evoGUI_settings(event) + local player = game.get_player(event.player_index) + if player.gui.center.evoGUI_settingsGUI then return end + + evogui.create_player_globals(player) + local always_visible = global.evogui[player.name].always_visible + local in_popup = global.evogui[player.name].in_popup + + local root = player.gui.center.add{type="frame", direction="vertical", name="evoGUI_settingsGUI"} + local table = root.add{type="table", colspan=3} + + for _, sensor in ipairs(evogui.value_sensors) do + local sensor_always_visible = always_visible[sensor.name] ~= nil + local sensor_in_popup = in_popup[sensor.name] ~= nil + + table.add{type="label", caption=sensor.display_name} + table.add{type="checkbox", name="AV_"..sensor.name, + caption={"settings_always_visible"}, state=sensor_always_visible} + table.add{type="checkbox", name="IP_"..sensor.name, + caption={"settings_in_popup"}, state=sensor_in_popup} + + evogui.on_click["AV_"..sensor.name] = toggle_always_visible + evogui.on_click["IP_"..sensor.name] = toggle_in_popup + end + + local buttons = root.add{type="flow", direction="horizontal"} + buttons.add{type="button", name="evoGUI_settings_close", caption={"settings_close"}} +end + +function evogui.on_click.evoGUI_settings_close(event) + local player = game.get_player(event.player_index) + + if player.gui.center.evoGUI_settingsGUI ~= nil then + player.gui.center.evoGUI_settingsGUI.destroy() + end +end diff --git a/value_sensors/template.lua b/value_sensors/template.lua index a148382..dee7d25 100644 --- a/value_sensors/template.lua +++ b/value_sensors/template.lua @@ -11,7 +11,7 @@ function ValueSensor.new(name, get_line) ["format_key"] = "sensor."..name..".format", ["get_line"] = get_line, } - evogui.value_sensors[name] = sensor + table.insert(evogui.value_sensors, sensor) return sensor end