Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Expose the ability to create sensors from the remote scripting interface
Browse files Browse the repository at this point in the history
Expose the ability to update remotely-created sensors from the remote scripting interface
Add method to get / set the font-color of sensor text
  • Loading branch information
Afforess committed Nov 1, 2015
1 parent 08f9b77 commit 8681c8c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions evoGUI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
65 changes: 65 additions & 0 deletions remote.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
25 changes: 25 additions & 0 deletions value_sensors/remote_sensor.lua
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions value_sensors/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 8681c8c

Please sign in to comment.