2022-10-19 14:04:16 -06:00
|
|
|
local _ENV = mkmodule('plugins.overlay')
|
|
|
|
|
2022-11-02 13:33:06 -06:00
|
|
|
local gui = require('gui')
|
2022-10-31 14:32:45 -06:00
|
|
|
local json = require('json')
|
2022-12-07 14:26:35 -07:00
|
|
|
local scriptmanager = require('script-manager')
|
2022-10-31 14:32:45 -06:00
|
|
|
local utils = require('utils')
|
2022-10-31 13:22:00 -06:00
|
|
|
local widgets = require('gui.widgets')
|
|
|
|
|
2022-11-04 13:57:05 -06:00
|
|
|
local OVERLAY_CONFIG_FILE = 'dfhack-config/overlay.json'
|
|
|
|
local OVERLAY_WIDGETS_VAR = 'OVERLAY_WIDGETS'
|
2022-10-31 14:32:45 -06:00
|
|
|
|
2022-11-04 13:57:05 -06:00
|
|
|
local DEFAULT_X_POS, DEFAULT_Y_POS = -2, -2
|
|
|
|
|
|
|
|
-- ---------------- --
|
|
|
|
-- state and config --
|
|
|
|
-- ---------------- --
|
|
|
|
|
2022-11-28 16:17:23 -07:00
|
|
|
local trigger_lock_holder_description = nil
|
|
|
|
local trigger_lock_holder_screen = nil -- if non-nil, no triggering allowed
|
2022-11-04 13:57:05 -06:00
|
|
|
local widget_db = {} -- map of widget name to ephermeral state
|
|
|
|
local widget_index = {} -- ordered list of widget names
|
|
|
|
local overlay_config = {} -- map of widget name to persisted state
|
2022-10-31 13:22:00 -06:00
|
|
|
local active_hotspot_widgets = {} -- map of widget names to the db entry
|
|
|
|
local active_viewscreen_widgets = {} -- map of vs_name to map of w.names -> db
|
|
|
|
|
2022-11-28 16:17:23 -07:00
|
|
|
function get_state()
|
|
|
|
return {index=widget_index, config=overlay_config, db=widget_db}
|
|
|
|
end
|
|
|
|
|
|
|
|
function register_trigger_lock_screen(scr, desc)
|
|
|
|
if trigger_lock_holder_screen then
|
|
|
|
if not trigger_lock_holder_screen:isActive() then
|
|
|
|
trigger_lock_holder_screen:dismiss()
|
|
|
|
end
|
|
|
|
trigger_lock_holder_description = nil
|
|
|
|
end
|
|
|
|
trigger_lock_holder_screen = scr
|
|
|
|
if trigger_lock_holder_screen then
|
|
|
|
trigger_lock_holder_description = desc
|
|
|
|
return true
|
2022-11-04 13:57:05 -06:00
|
|
|
end
|
2022-11-28 16:17:23 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function triggered_screen_has_lock()
|
|
|
|
if not trigger_lock_holder_screen then return false end
|
2023-01-13 13:16:49 -07:00
|
|
|
if trigger_lock_holder_screen:isActive() then
|
|
|
|
if trigger_lock_holder_screen.raise then
|
|
|
|
trigger_lock_holder_screen:raise()
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2022-11-28 16:17:23 -07:00
|
|
|
return register_trigger_lock_screen(nil, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function reset()
|
|
|
|
register_trigger_lock_screen(nil, nil)
|
2022-11-04 13:57:05 -06:00
|
|
|
|
|
|
|
widget_db = {}
|
|
|
|
widget_index = {}
|
|
|
|
|
|
|
|
local ok, config = pcall(json.decode_file, OVERLAY_CONFIG_FILE)
|
|
|
|
overlay_config = ok and config or {}
|
|
|
|
|
|
|
|
active_hotspot_widgets = {}
|
|
|
|
active_viewscreen_widgets = {}
|
2022-11-02 13:33:06 -06:00
|
|
|
end
|
|
|
|
|
2022-11-04 13:57:05 -06:00
|
|
|
local function save_config()
|
|
|
|
if not safecall(json.encode_file, overlay_config, OVERLAY_CONFIG_FILE) then
|
2022-11-02 13:33:06 -06:00
|
|
|
dfhack.printerr(('failed to save overlay config file: "%s"')
|
|
|
|
:format(path))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-04 13:57:05 -06:00
|
|
|
-- ----------- --
|
|
|
|
-- utility fns --
|
|
|
|
-- ----------- --
|
|
|
|
|
2022-11-28 16:17:23 -07:00
|
|
|
function normalize_list(element_or_list)
|
2022-11-02 13:33:06 -06:00
|
|
|
if type(element_or_list) == 'table' then return element_or_list end
|
|
|
|
return {element_or_list}
|
|
|
|
end
|
|
|
|
|
2022-11-11 11:36:58 -07:00
|
|
|
-- normalize "short form" viewscreen names to "long form"
|
2022-11-02 13:33:06 -06:00
|
|
|
local function normalize_viewscreen_name(vs_name)
|
2023-01-01 18:20:06 -07:00
|
|
|
if vs_name == 'all' or vs_name:match('viewscreen_.*st') then
|
|
|
|
return vs_name
|
|
|
|
end
|
2022-11-02 13:33:06 -06:00
|
|
|
return 'viewscreen_' .. vs_name .. 'st'
|
|
|
|
end
|
|
|
|
|
2022-11-11 11:36:58 -07:00
|
|
|
-- reduce "long form" viewscreen names to "short form"
|
2022-11-28 16:17:23 -07:00
|
|
|
function simplify_viewscreen_name(vs_name)
|
2022-11-11 11:36:58 -07:00
|
|
|
_,_,short_name = vs_name:find('^viewscreen_(.*)st$')
|
|
|
|
if short_name then return short_name end
|
|
|
|
return vs_name
|
|
|
|
end
|
|
|
|
|
2022-11-02 13:33:06 -06:00
|
|
|
local function is_empty(tbl)
|
|
|
|
for _ in pairs(tbl) do
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2022-11-04 13:57:05 -06:00
|
|
|
local function sanitize_pos(pos)
|
|
|
|
local x = math.floor(tonumber(pos.x) or DEFAULT_X_POS)
|
|
|
|
local y = math.floor(tonumber(pos.y) or DEFAULT_Y_POS)
|
2022-11-11 11:36:58 -07:00
|
|
|
-- if someone accidentally uses 0-based instead of 1-based indexing, fix it
|
2022-11-04 13:57:05 -06:00
|
|
|
if x == 0 then x = 1 end
|
|
|
|
if y == 0 then y = 1 end
|
|
|
|
return {x=x, y=y}
|
|
|
|
end
|
|
|
|
|
|
|
|
local function make_frame(pos, old_frame)
|
|
|
|
old_frame = old_frame or {}
|
|
|
|
local frame = {w=old_frame.w, h=old_frame.h}
|
|
|
|
if pos.x < 0 then frame.r = math.abs(pos.x) - 1 else frame.l = pos.x - 1 end
|
|
|
|
if pos.y < 0 then frame.b = math.abs(pos.y) - 1 else frame.t = pos.y - 1 end
|
|
|
|
return frame
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_screen_rect()
|
|
|
|
local w, h = dfhack.screen.getWindowSize()
|
|
|
|
return gui.ViewRect{rect=gui.mkdims_wh(0, 0, w, h)}
|
|
|
|
end
|
|
|
|
|
|
|
|
-- ------------- --
|
|
|
|
-- CLI functions --
|
|
|
|
-- ------------- --
|
|
|
|
|
2022-11-02 13:33:06 -06:00
|
|
|
local function get_name(name_or_number)
|
|
|
|
local num = tonumber(name_or_number)
|
|
|
|
if num and widget_index[num] then
|
|
|
|
return widget_index[num]
|
|
|
|
end
|
|
|
|
return tostring(name_or_number)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function do_by_names_or_numbers(args, fn)
|
2022-11-09 15:08:38 -07:00
|
|
|
local arglist = normalize_list(args)
|
|
|
|
if #arglist == 0 then
|
|
|
|
dfhack.printerr('please specify a widget name or list number')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
for _,name_or_number in ipairs(arglist) do
|
2022-11-02 13:33:06 -06:00
|
|
|
local name = get_name(name_or_number)
|
|
|
|
local db_entry = widget_db[name]
|
|
|
|
if not db_entry then
|
|
|
|
dfhack.printerr(('widget not found: "%s"'):format(name))
|
|
|
|
else
|
|
|
|
fn(name, db_entry)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-04 13:57:05 -06:00
|
|
|
local function do_enable(args, quiet, skip_save)
|
2022-11-09 15:08:38 -07:00
|
|
|
local enable_fn = function(name, db_entry)
|
2022-11-04 13:57:05 -06:00
|
|
|
overlay_config[name].enabled = true
|
|
|
|
if db_entry.widget.hotspot then
|
2022-11-02 13:33:06 -06:00
|
|
|
active_hotspot_widgets[name] = db_entry
|
|
|
|
end
|
2022-11-04 13:57:05 -06:00
|
|
|
for _,vs_name in ipairs(normalize_list(db_entry.widget.viewscreens)) do
|
2022-11-02 13:33:06 -06:00
|
|
|
vs_name = normalize_viewscreen_name(vs_name)
|
|
|
|
ensure_key(active_viewscreen_widgets, vs_name)[name] = db_entry
|
|
|
|
end
|
2022-11-04 13:57:05 -06:00
|
|
|
if not quiet then
|
|
|
|
print(('enabled widget %s'):format(name))
|
|
|
|
end
|
2022-11-09 15:08:38 -07:00
|
|
|
end
|
|
|
|
if args[1] == 'all' then
|
|
|
|
for name,db_entry in pairs(widget_db) do
|
|
|
|
if not overlay_config[name].enabled then
|
|
|
|
enable_fn(name, db_entry)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
do_by_names_or_numbers(args, enable_fn)
|
|
|
|
end
|
2022-11-04 13:57:05 -06:00
|
|
|
if not skip_save then
|
|
|
|
save_config()
|
|
|
|
end
|
2022-11-02 13:33:06 -06:00
|
|
|
end
|
|
|
|
|
2022-11-28 16:17:23 -07:00
|
|
|
local function do_disable(args, quiet)
|
2022-11-09 15:08:38 -07:00
|
|
|
local disable_fn = function(name, db_entry)
|
2022-11-04 13:57:05 -06:00
|
|
|
overlay_config[name].enabled = false
|
|
|
|
if db_entry.widget.hotspot then
|
2022-11-02 13:33:06 -06:00
|
|
|
active_hotspot_widgets[name] = nil
|
|
|
|
end
|
2022-11-04 13:57:05 -06:00
|
|
|
for _,vs_name in ipairs(normalize_list(db_entry.widget.viewscreens)) do
|
2022-11-02 13:33:06 -06:00
|
|
|
vs_name = normalize_viewscreen_name(vs_name)
|
|
|
|
ensure_key(active_viewscreen_widgets, vs_name)[name] = nil
|
|
|
|
if is_empty(active_viewscreen_widgets[vs_name]) then
|
|
|
|
active_viewscreen_widgets[vs_name] = nil
|
|
|
|
end
|
|
|
|
end
|
2022-11-28 16:17:23 -07:00
|
|
|
if not quiet then
|
|
|
|
print(('disabled widget %s'):format(name))
|
|
|
|
end
|
2022-11-09 15:08:38 -07:00
|
|
|
end
|
|
|
|
if args[1] == 'all' then
|
|
|
|
for name,db_entry in pairs(widget_db) do
|
|
|
|
if overlay_config[name].enabled then
|
|
|
|
disable_fn(name, db_entry)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
do_by_names_or_numbers(args, disable_fn)
|
|
|
|
end
|
2022-11-04 13:57:05 -06:00
|
|
|
save_config()
|
2022-11-02 13:33:06 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
local function do_list(args)
|
|
|
|
local filter = args and #args > 0
|
2022-11-04 13:57:05 -06:00
|
|
|
local num_filtered = 0
|
2022-11-02 13:33:06 -06:00
|
|
|
for i,name in ipairs(widget_index) do
|
|
|
|
if filter then
|
|
|
|
local passes = false
|
|
|
|
for _,str in ipairs(args) do
|
|
|
|
if name:find(str) then
|
|
|
|
passes = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2022-11-04 13:57:05 -06:00
|
|
|
if not passes then
|
|
|
|
num_filtered = num_filtered + 1
|
|
|
|
goto continue
|
|
|
|
end
|
2022-11-02 13:33:06 -06:00
|
|
|
end
|
2022-11-04 13:57:05 -06:00
|
|
|
local db_entry = widget_db[name]
|
|
|
|
local enabled = overlay_config[name].enabled
|
2022-11-09 15:08:38 -07:00
|
|
|
dfhack.color(enabled and COLOR_LIGHTGREEN or COLOR_YELLOW)
|
2022-11-02 13:33:06 -06:00
|
|
|
dfhack.print(enabled and '[enabled] ' or '[disabled]')
|
|
|
|
dfhack.color()
|
2022-11-09 15:08:38 -07:00
|
|
|
print((' %d) %s'):format(i, name))
|
2022-11-02 13:33:06 -06:00
|
|
|
::continue::
|
|
|
|
end
|
2022-11-04 13:57:05 -06:00
|
|
|
if num_filtered > 0 then
|
|
|
|
print(('(%d widgets filtered out)'):format(num_filtered))
|
|
|
|
end
|
2022-11-02 13:33:06 -06:00
|
|
|
end
|
|
|
|
|
2022-11-04 13:57:05 -06:00
|
|
|
local function load_widget(name, widget_class)
|
|
|
|
local widget = widget_class{name=name}
|
|
|
|
widget_db[name] = {
|
|
|
|
widget=widget,
|
|
|
|
next_update_ms=widget.overlay_onupdate and 0 or math.huge,
|
|
|
|
}
|
|
|
|
if not overlay_config[name] then overlay_config[name] = {} end
|
|
|
|
local config = overlay_config[name]
|
2022-11-11 11:36:58 -07:00
|
|
|
config.pos = sanitize_pos(config.pos or widget.default_pos)
|
2022-11-04 13:57:05 -06:00
|
|
|
widget.frame = make_frame(config.pos, widget.frame)
|
2023-01-22 02:07:30 -07:00
|
|
|
if config.enabled then
|
2022-11-04 13:57:05 -06:00
|
|
|
do_enable(name, true, true)
|
|
|
|
else
|
|
|
|
config.enabled = false
|
|
|
|
end
|
2022-11-02 13:33:06 -06:00
|
|
|
end
|
|
|
|
|
2022-12-05 18:09:08 -07:00
|
|
|
local function load_widgets(env_name, env)
|
|
|
|
local overlay_widgets = env[OVERLAY_WIDGETS_VAR]
|
|
|
|
if not overlay_widgets then return end
|
2022-11-04 13:57:05 -06:00
|
|
|
if type(overlay_widgets) ~= 'table' then
|
|
|
|
dfhack.printerr(
|
|
|
|
('error loading overlay widgets from "%s": %s map is malformed')
|
|
|
|
:format(env_name, OVERLAY_WIDGETS_VAR))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
for widget_name,widget_class in pairs(overlay_widgets) do
|
2022-12-05 18:09:08 -07:00
|
|
|
local name = env_name .. '.' .. widget_name
|
2022-11-04 13:57:05 -06:00
|
|
|
if not safecall(load_widget, name, widget_class) then
|
|
|
|
dfhack.printerr(('error loading overlay widget "%s"'):format(name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-11 11:36:58 -07:00
|
|
|
-- called directly from cpp on plugin enable
|
2022-11-04 13:57:05 -06:00
|
|
|
function reload()
|
|
|
|
reset()
|
|
|
|
|
|
|
|
for _,plugin in ipairs(dfhack.internal.listPlugins()) do
|
2022-12-05 18:09:08 -07:00
|
|
|
local env_name = 'plugins.' .. plugin
|
|
|
|
local ok, plugin_env = pcall(require, env_name)
|
|
|
|
if ok then
|
|
|
|
load_widgets(plugin, plugin_env)
|
2022-11-04 13:57:05 -06:00
|
|
|
end
|
|
|
|
end
|
2022-12-05 18:09:08 -07:00
|
|
|
scriptmanager.foreach_module_script(load_widgets)
|
2022-11-04 13:57:05 -06:00
|
|
|
|
|
|
|
for name in pairs(widget_db) do
|
|
|
|
table.insert(widget_index, name)
|
|
|
|
end
|
|
|
|
table.sort(widget_index)
|
|
|
|
|
|
|
|
reposition_widgets()
|
|
|
|
end
|
|
|
|
|
2022-11-09 15:08:38 -07:00
|
|
|
local function dump_widget_config(name, widget)
|
|
|
|
local pos = overlay_config[name].pos
|
|
|
|
print(('widget %s is positioned at x=%d, y=%d'):format(name, pos.x, pos.y))
|
2022-11-11 11:36:58 -07:00
|
|
|
local viewscreens = normalize_list(widget.viewscreens)
|
|
|
|
if #viewscreens > 0 then
|
2022-11-09 15:08:38 -07:00
|
|
|
print(' it will be attached to the following viewscreens:')
|
2022-11-11 11:36:58 -07:00
|
|
|
for _,vs in ipairs(viewscreens) do
|
|
|
|
print((' %s'):format(simplify_viewscreen_name(vs)))
|
2022-11-09 15:08:38 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if widget.hotspot then
|
2022-11-11 11:36:58 -07:00
|
|
|
print(' it will act as a hotspot on all screens')
|
2022-11-09 15:08:38 -07:00
|
|
|
end
|
2022-11-02 13:33:06 -06:00
|
|
|
end
|
|
|
|
|
2022-11-28 16:17:23 -07:00
|
|
|
local function do_position(args, quiet)
|
2022-11-02 13:33:06 -06:00
|
|
|
local name_or_number, x, y = table.unpack(args)
|
|
|
|
local name = get_name(name_or_number)
|
2022-11-09 15:08:38 -07:00
|
|
|
if not widget_db[name] then
|
|
|
|
if not name_or_number then
|
|
|
|
dfhack.printerr('please specify a widget name or list number')
|
|
|
|
else
|
|
|
|
dfhack.printerr(('widget not found: "%s"'):format(name))
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
2022-11-04 13:57:05 -06:00
|
|
|
local widget = widget_db[name].widget
|
2022-11-09 15:08:38 -07:00
|
|
|
local pos
|
|
|
|
if x == 'default' then
|
|
|
|
pos = sanitize_pos(widget.default_pos)
|
|
|
|
else
|
|
|
|
x, y = tonumber(x), tonumber(y)
|
|
|
|
if not x or not y then
|
|
|
|
dump_widget_config(name, widget)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
pos = sanitize_pos{x=x, y=y}
|
|
|
|
end
|
|
|
|
overlay_config[name].pos = pos
|
2022-11-04 13:57:05 -06:00
|
|
|
widget.frame = make_frame(pos, widget.frame)
|
|
|
|
widget:updateLayout(get_screen_rect())
|
|
|
|
save_config()
|
2022-11-28 16:17:23 -07:00
|
|
|
if not quiet then
|
|
|
|
print(('repositioned widget %s to x=%d, y=%d'):format(name, pos.x, pos.y))
|
|
|
|
end
|
2022-11-02 13:33:06 -06:00
|
|
|
end
|
|
|
|
|
2022-11-04 13:57:05 -06:00
|
|
|
-- note that the widget does not have to be enabled to be triggered
|
2022-11-28 16:17:23 -07:00
|
|
|
local function do_trigger(args, quiet)
|
2022-11-04 13:57:05 -06:00
|
|
|
if triggered_screen_has_lock() then
|
2022-11-09 15:08:38 -07:00
|
|
|
dfhack.printerr(('cannot trigger widget; widget "%s" is already active')
|
2023-01-18 16:44:47 -07:00
|
|
|
:format(trigger_lock_holder_description))
|
2022-11-04 13:57:05 -06:00
|
|
|
return
|
|
|
|
end
|
2022-11-11 11:36:58 -07:00
|
|
|
do_by_names_or_numbers(args[1], function(name, db_entry)
|
2022-11-04 13:57:05 -06:00
|
|
|
local widget = db_entry.widget
|
|
|
|
if widget.overlay_trigger then
|
2022-11-28 16:17:23 -07:00
|
|
|
register_trigger_lock_screen(widget:overlay_trigger(), name)
|
|
|
|
if not quiet then
|
|
|
|
print(('triggered widget %s'):format(name))
|
2022-11-09 15:08:38 -07:00
|
|
|
end
|
2022-11-02 13:33:06 -06:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
local command_fns = {
|
|
|
|
enable=do_enable,
|
|
|
|
disable=do_disable,
|
|
|
|
list=do_list,
|
2022-11-09 15:08:38 -07:00
|
|
|
position=do_position,
|
2022-11-02 13:33:06 -06:00
|
|
|
trigger=do_trigger,
|
|
|
|
}
|
|
|
|
|
|
|
|
local HELP_ARGS = utils.invert{'help', '--help', '-h'}
|
|
|
|
|
2022-11-28 16:17:23 -07:00
|
|
|
function overlay_command(args, quiet)
|
2022-11-02 13:33:06 -06:00
|
|
|
local command = table.remove(args, 1) or 'help'
|
|
|
|
if HELP_ARGS[command] or not command_fns[command] then return false end
|
2022-11-28 16:17:23 -07:00
|
|
|
command_fns[command](args, quiet)
|
2022-11-02 13:33:06 -06:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2022-11-04 13:57:05 -06:00
|
|
|
-- ---------------- --
|
|
|
|
-- event management --
|
|
|
|
-- ---------------- --
|
2022-11-02 13:33:06 -06:00
|
|
|
|
|
|
|
local function detect_frame_change(widget, fn)
|
|
|
|
local frame = widget.frame
|
|
|
|
local w, h = frame.w, frame.h
|
|
|
|
local ret = fn()
|
|
|
|
if w ~= frame.w or h ~= frame.h then
|
|
|
|
widget:updateLayout()
|
|
|
|
end
|
|
|
|
return ret
|
2022-10-31 13:22:00 -06:00
|
|
|
end
|
|
|
|
|
2022-11-11 11:36:58 -07:00
|
|
|
local function get_next_onupdate_timestamp(now_ms, widget)
|
|
|
|
local freq_s = widget.overlay_onupdate_max_freq_seconds
|
|
|
|
if freq_s == 0 then
|
|
|
|
return now_ms
|
|
|
|
end
|
|
|
|
local freq_ms = math.floor(freq_s * 1000)
|
|
|
|
local jitter = math.random(0, freq_ms // 8) -- up to ~12% jitter
|
|
|
|
return now_ms + freq_ms - jitter
|
|
|
|
end
|
|
|
|
|
2022-10-31 13:22:00 -06:00
|
|
|
-- reduces the next call by a small random amount to introduce jitter into the
|
|
|
|
-- widget processing timings
|
2022-11-09 15:08:38 -07:00
|
|
|
local function do_update(name, db_entry, now_ms, vs)
|
2022-10-31 13:22:00 -06:00
|
|
|
if db_entry.next_update_ms > now_ms then return end
|
|
|
|
local w = db_entry.widget
|
2022-11-11 11:36:58 -07:00
|
|
|
db_entry.next_update_ms = get_next_onupdate_timestamp(now_ms, w)
|
|
|
|
if detect_frame_change(w, function() return w:overlay_onupdate(vs) end) then
|
2022-11-28 16:17:23 -07:00
|
|
|
if register_trigger_lock_screen(w:overlay_trigger(), name) then
|
2022-11-09 15:08:38 -07:00
|
|
|
return true
|
|
|
|
end
|
2022-10-31 13:22:00 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function update_hotspot_widgets()
|
2022-11-04 13:57:05 -06:00
|
|
|
if triggered_screen_has_lock() then return end
|
2022-10-31 13:22:00 -06:00
|
|
|
local now_ms = dfhack.getTickCount()
|
2022-11-09 15:08:38 -07:00
|
|
|
for name,db_entry in pairs(active_hotspot_widgets) do
|
|
|
|
if do_update(name, db_entry, now_ms) then return end
|
2022-10-31 13:22:00 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-01 18:20:06 -07:00
|
|
|
local function _update_viewscreen_widgets(vs_name, vs, now_ms)
|
2022-10-31 13:22:00 -06:00
|
|
|
local vs_widgets = active_viewscreen_widgets[vs_name]
|
|
|
|
if not vs_widgets then return end
|
2023-01-01 18:20:06 -07:00
|
|
|
now_ms = now_ms or dfhack.getTickCount()
|
2022-11-09 15:08:38 -07:00
|
|
|
for name,db_entry in pairs(vs_widgets) do
|
|
|
|
if do_update(name, db_entry, now_ms, vs) then return end
|
2022-10-31 13:22:00 -06:00
|
|
|
end
|
2023-01-01 18:20:06 -07:00
|
|
|
return now_ms
|
2022-10-31 13:22:00 -06:00
|
|
|
end
|
|
|
|
|
2023-01-01 18:20:06 -07:00
|
|
|
function update_viewscreen_widgets(vs_name, vs)
|
2023-01-13 13:16:49 -07:00
|
|
|
if triggered_screen_has_lock() then return end
|
2023-01-01 18:20:06 -07:00
|
|
|
local now_ms = _update_viewscreen_widgets(vs_name, vs, nil)
|
|
|
|
_update_viewscreen_widgets('all', vs, now_ms)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function _feed_viewscreen_widgets(vs_name, keys)
|
2022-10-31 13:22:00 -06:00
|
|
|
local vs_widgets = active_viewscreen_widgets[vs_name]
|
|
|
|
if not vs_widgets then return false end
|
|
|
|
for _,db_entry in pairs(vs_widgets) do
|
2022-11-11 11:36:58 -07:00
|
|
|
local w = db_entry.widget
|
|
|
|
if detect_frame_change(w, function() return w:onInput(keys) end) then
|
2022-11-02 13:33:06 -06:00
|
|
|
return true
|
|
|
|
end
|
2022-10-31 13:22:00 -06:00
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-01-01 18:20:06 -07:00
|
|
|
function feed_viewscreen_widgets(vs_name, keys)
|
|
|
|
return _feed_viewscreen_widgets(vs_name, keys) or
|
|
|
|
_feed_viewscreen_widgets('all', keys)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function _render_viewscreen_widgets(vs_name, dc)
|
2022-10-31 13:22:00 -06:00
|
|
|
local vs_widgets = active_viewscreen_widgets[vs_name]
|
|
|
|
if not vs_widgets then return false end
|
2023-01-01 18:20:06 -07:00
|
|
|
dc = dc or gui.Painter.new()
|
2022-10-31 13:22:00 -06:00
|
|
|
for _,db_entry in pairs(vs_widgets) do
|
2022-11-11 11:36:58 -07:00
|
|
|
local w = db_entry.widget
|
|
|
|
detect_frame_change(w, function() w:render(dc) end)
|
2022-10-31 13:22:00 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-01 18:20:06 -07:00
|
|
|
function render_viewscreen_widgets(vs_name)
|
|
|
|
local dc = _render_viewscreen_widgets(vs_name, nil)
|
|
|
|
_render_viewscreen_widgets('all', dc)
|
|
|
|
end
|
|
|
|
|
2022-10-31 13:22:00 -06:00
|
|
|
-- called when the DF window is resized
|
|
|
|
function reposition_widgets()
|
2022-11-02 13:33:06 -06:00
|
|
|
local sr = get_screen_rect()
|
2022-10-31 13:22:00 -06:00
|
|
|
for _,db_entry in pairs(widget_db) do
|
2022-11-02 13:33:06 -06:00
|
|
|
db_entry.widget:updateLayout(sr)
|
2022-10-31 13:22:00 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-04 13:57:05 -06:00
|
|
|
-- ------------------------------------------------- --
|
|
|
|
-- OverlayWidget (base class of all overlay widgets) --
|
|
|
|
-- ------------------------------------------------- --
|
|
|
|
|
2022-12-31 22:02:00 -07:00
|
|
|
OverlayWidget = defclass(OverlayWidget, widgets.Panel)
|
2022-10-31 13:22:00 -06:00
|
|
|
OverlayWidget.ATTRS{
|
2022-11-04 13:57:05 -06:00
|
|
|
name=DEFAULT_NIL, -- this is set by the framework to the widget name
|
2022-11-11 11:36:58 -07:00
|
|
|
default_pos={x=DEFAULT_X_POS, y=DEFAULT_Y_POS}, -- 1-based widget screen pos
|
2022-11-28 16:17:23 -07:00
|
|
|
overlay_only=false, -- true if there is no widget to reposition
|
2022-11-11 11:36:58 -07:00
|
|
|
hotspot=false, -- whether to call overlay_onupdate on all screens
|
|
|
|
viewscreens={}, -- override with associated viewscreen or list of viewscrens
|
2022-11-04 13:57:05 -06:00
|
|
|
overlay_onupdate_max_freq_seconds=5, -- throttle calls to overlay_onupdate
|
2022-10-31 13:22:00 -06:00
|
|
|
}
|
|
|
|
|
2022-11-04 13:57:05 -06:00
|
|
|
function OverlayWidget:init()
|
2022-11-09 16:48:24 -07:00
|
|
|
if self.overlay_onupdate_max_freq_seconds < 0 then
|
|
|
|
error(('overlay_onupdate_max_freq_seconds must be >= 0: %s')
|
|
|
|
:format(tostring(self.overlay_onupdate_max_freq_seconds)))
|
|
|
|
end
|
|
|
|
|
|
|
|
-- set defaults for frame. the widget is expected to keep these up to date
|
2022-11-11 11:36:58 -07:00
|
|
|
-- when display contents change so the widget position can shift if the
|
|
|
|
-- frame is relative to the right or bottom edges.
|
2022-11-04 13:57:05 -06:00
|
|
|
self.frame = self.frame or {}
|
|
|
|
self.frame.w = self.frame.w or 5
|
|
|
|
self.frame.h = self.frame.h or 1
|
2022-11-02 13:33:06 -06:00
|
|
|
end
|
|
|
|
|
2022-10-19 14:04:16 -06:00
|
|
|
return _ENV
|