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

Commit

Permalink
Merge PR #39: remote sensor create/update
Browse files Browse the repository at this point in the history
Attn: #23 -- new locale.
  • Loading branch information
narc0tiq committed Nov 3, 2015
2 parents 08f9b77 + 4af1d1d commit 5690e13
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.6
0.4.7
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
5 changes: 5 additions & 0 deletions locale/en/locale.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,8 @@ err_settings_whatsettings=__1__ called for settings-less sensor!
err_needplayername=[EvoGUI] Need a player name!
err_nosuchplayer=[EvoGUI] No such player: __1__!
err_noplayerdata=[EvoGUI] No player data for __1__!
err_nosensorname=[EvoGUI] Missing a sensor name!
err_nosensortext=[EvoGUI] Missing sensor text for __1__!
err_nosensorcaption=[EvoGUI] Missing caption text for __1__!
err_nosensorfound=[EvoGUI] No sensor was previously created matching the name __1__! Create a sensor first.
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", sensor_name})
return
end

if not sensor_caption then
evogui.log({"err_nosensorcaption", sensor_name})
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", sensor_name})
return
end

local sensor = RemoteSensor.get_by_name(sensor_name)
if not sensor then
evogui.log({"err_nosensorfound", sensor_name})
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.create_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.update_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, color)
local sensor = ValueSensor.new("remote_sensor_" .. name)
sensor["line"] = line
sensor["display_name"] = caption
sensor["color"] = color

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
2 changes: 2 additions & 0 deletions value_sensors/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function ValueSensor.new(name)
["name"] = name,
["display_name"] = { "sensor."..name..".name" },
["format_key"] = "sensor."..name..".format",
["color"] = { r = 255, g = 255, b = 255 },
}

function sensor:get_line()
Expand All @@ -28,6 +29,7 @@ function ValueSensor.new(name)
self.settings = sensor_settings

owner[self.name].caption = self:get_line()
owner[self.name].style.font_color = self.color
end

function sensor:delete_ui(owner)
Expand Down

0 comments on commit 5690e13

Please sign in to comment.