From 8681c8c738f653f419a28c321ee8ba272f356c7b Mon Sep 17 00:00:00 2001 From: Afforess Date: Sun, 1 Nov 2015 14:39:24 -0500 Subject: [PATCH] Expose the ability to create sensors from the remote scripting interface Expose the ability to update remotely-created sensors from the remote scripting interface Add method to get / set the font-color of sensor text --- evoGUI.lua | 1 + remote.lua | 65 +++++++++++++++++++++++++++++++++ value_sensors/remote_sensor.lua | 25 +++++++++++++ value_sensors/template.lua | 8 ++++ 4 files changed, 99 insertions(+) create mode 100644 value_sensors/remote_sensor.lua diff --git a/evoGUI.lua b/evoGUI.lua index 6b6e349..7df027f 100644 --- a/evoGUI.lua +++ b/evoGUI.lua @@ -5,6 +5,7 @@ require "value_sensors.kill_count" require "value_sensors.play_time" require "value_sensors.player_locations" require "value_sensors.pollution_around_player" +require "value_sensors.remote_sensor" require "settingsGUI" require "remote" diff --git a/remote.lua b/remote.lua index fa704b9..2e08528 100644 --- a/remote.lua +++ b/remote.lua @@ -31,11 +31,76 @@ local function remote_rebuild(player_name) global.evogui[player.name].version = "" end +-- +-- Creates a sensor managed by a remote interface (another mod or script) +-- sensor_name: internal name of the sensor. Should be unique. +-- sensor_text: Text to display in the active gui +-- sensor_caption: Sensor setting name in the EvoGUI settings panel +-- sensor_color: Font color of the text to display in the active gui, optional, may be nil +-- example: remote.call("EvoGUI", "create_remote_sensor", "mymod_my_sensor_name", "Text: Lorem Ipsum", "[My Mod] Lorem Ipsum Text") +local function create_remote_sensor(sensor_name, sensor_text, sensor_caption, sensor_color) + if not sensor_name then + evogui.log({"err_nosensorname"}) + return + end + + if not sensor_text then + evogui.log({"err_nosensortext"}) + return + end + + if not sensor_caption then + evogui.log({"err_nosensorcaption"}) + return + end + + RemoteSensor.new(sensor_name, sensor_text, sensor_caption, sensor_color) +end + +-- +-- Updates a sensor managed by a remote interface +-- sensor_name: internal name of the sensor. The sensor should have been previously created. +-- sensor_text: Text to display in the active gui +-- sensor_color: Font color of the text to display in the active gui, optional, may be nil +-- example: remote.call("EvoGUI", "update_remote_sensor", "mymod_my_sensor_name", "Text: Lorem Ipsum") +local function update_remote_sensor(sensor_name, sensor_text, sensor_color) + if not sensor_name then + evogui.log({"err_nosensorname"}) + return + end + + if not sensor_text then + evogui.log({"err_nosensortext"}) + return + end + + local sensor = RemoteSensor.get_by_name(sensor_name) + if not sensor then + evogui.log({"err_nosensorfound"}) + return + end + + sensor["line"] = sensor_text + if sensor_color then + sensor["color"] = sensor_color + end +end + interface = { rebuild = function(player_name) local status, err = pcall(remote_rebuild, player_name) if err then evogui.log({"err_generic", "interface.rebuild", err}) end + end, + + create_remote_sensor = function(sensor_name, sensor_text, sensor_caption, sensor_color) + local status, err = pcall(create_remote_sensor, sensor_name, sensor_text, sensor_caption, sensor_color) + if err then evogui.log({"err_generic", "remote.sensor", err}) end + end, + + update_remote_sensor = function(sensor_name, sensor_text, sensor_color) + local status, err = pcall(update_remote_sensor, sensor_name, sensor_text, sensor_color) + if err then evogui.log({"err_generic", "remote.sensor", err}) end end } diff --git a/value_sensors/remote_sensor.lua b/value_sensors/remote_sensor.lua new file mode 100644 index 0000000..1dc57f9 --- /dev/null +++ b/value_sensors/remote_sensor.lua @@ -0,0 +1,25 @@ +require "template" + +RemoteSensor = {} +function RemoteSensor.new(name, line, caption, style) + local sensor = ValueSensor.new("remote_sensor_" .. name) + sensor["line"] = line + sensor["display_name"] = caption + sensor["style"] = style + + function sensor:set_line(text) + self.line = text + end + + function sensor:get_line() + return self.line + end + + ValueSensor.register(sensor) +end + +function RemoteSensor.get_by_name(name) + return ValueSensor.get_by_name("remote_sensor_" .. name) +end + +return RemoteSensor diff --git a/value_sensors/template.lua b/value_sensors/template.lua index 47ab6d6..6cdf3a0 100644 --- a/value_sensors/template.lua +++ b/value_sensors/template.lua @@ -9,8 +9,13 @@ function ValueSensor.new(name) ["name"] = name, ["display_name"] = { "sensor."..name..".name" }, ["format_key"] = "sensor."..name..".format", + ["color"] = nil, } + function sensor:get_font_color() + return self.color + end + function sensor:get_line() return self.display_name end @@ -28,6 +33,9 @@ function ValueSensor.new(name) self.settings = sensor_settings owner[self.name].caption = self:get_line() + if self:get_font_color() ~= nil then + owner[self.name].style.font_color = self:get_font_color() + end end function sensor:delete_ui(owner)