Divergent/mods/Hideout Furniture/gamedata/scripts/hf_placement_hud.script

104 lines
2.5 KiB
Plaintext
Raw Permalink Normal View History

2024-03-17 20:18:03 -04:00
-- Display onscreen text for placement controls
local function get_placement_binds()
return {
["key_interact"] = ui_mcm.dispaly_key(bind_to_dik(key_bindings.kUSE)),
["key_collision"] = ui_mcm.dispaly_key(ui_mcm.get("aol_hf/controls/bind_collision")),
["key_align"] = ui_mcm.dispaly_key(ui_mcm.get("aol_hf/controls/bind_alignment")),
["key_mode"] = ui_mcm.dispaly_key(ui_mcm.get("aol_hf/controls/bind_place_mode")),
}
end
local function generate_msg(string_id)
local msg = game.translate_string(string_id)
return utils_data.parse_string_keys(msg, get_placement_binds())
end
local function generate_holding_msg()
return generate_msg("st_hf_holding_msg")
end
local function generate_advanced_msg()
return generate_msg("st_hf_advanced_msg")
end
local function generate_preview_msg()
return generate_msg("st_hf_preview_msg")
end
local state_to_msg_func = {
[placeable_furniture.states.HOLDING] = generate_holding_msg,
[placeable_furniture.states.ADV_CTRL] = generate_advanced_msg,
[placeable_furniture.states.PREVIEW] = generate_preview_msg
}
---@type hf_placement_hud.UIHFPlacementHUD|nil
HUD = nil
class "UIHFPlacementHUD" (CUIScriptWnd)
function UIHFPlacementHUD:__init() super()
self:Initialise()
end
function UIHFPlacementHUD:__finalize()
end
function UIHFPlacementHUD:Initialise()
self:SetWndRect(Frect():set(0,0,1024,768))
self:SetAutoDelete(true)
local xml = CScriptXmlInit()
xml:ParseFile("ui_hf_placement_hud.xml")
self.bg = xml:InitFrame("background", self)
self.text = xml:InitTextWnd("background:text", self.bg)
end
function UIHFPlacementHUD:Refresh(state)
local msg_func = state_to_msg_func[state]
if not msg_func then self:Clear() end
self.text:SetText(msg_func())
self.text:AdjustHeightToText()
self.bg:SetHeight(self.text:GetHeight() + 24)
end
function UIHFPlacementHUD:Update()
CUIScriptWnd.Update(self)
end
function UIHFPlacementHUD:Clear()
self.text:SetText("")
end
-------------
function update_hud(state)
if HUD == nil then
HUD = UIHFPlacementHUD()
get_hud():AddDialogToRender(HUD)
end
HUD:Refresh(state)
end
function remove_hud()
if HUD ~= nil then
get_hud():RemoveDialogToRender(HUD)
HUD = nil
end
end
local hf_set_state = placeable_furniture.set_state
function placeable_furniture.set_state(new_state)
hf_set_state(new_state)
if placeable_furniture.in_placing_state() then
update_hud(new_state)
else
remove_hud()
end
end