Divergent/mods/Western Goods/gamedata/scripts/western_goods_ui_rangefinde...

204 lines
7.3 KiB
Plaintext
Raw Normal View History

2024-03-17 20:18:03 -04:00
---==================================================================================================================---
--- ---
--- Original Author(s) : NLTP_ASHES ---
--- Edited : N/A ---
--- Date : 19/06/2023 ---
--- License : Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) ---
--- ---
--- Script used to handle the UI for the Rangefinder. ---
--- ---
---==================================================================================================================---
-- ---------------------------------------------------------------------------------------------------------------------
-- Constants, global variables and imported functions
-- ---------------------------------------------------------------------------------------------------------------------
-- Imported functions
local dbg_printf = western_goods_utils.dbg_printf
-- Singleton
GUI = nil -- instance, don't touch
-- ---------------------------------------------------------------------------------------------------------------------
-- General Functions
-- ---------------------------------------------------------------------------------------------------------------------
--- Function used to get the UI singleton for the UIRangefinder class.
--- @return UIRangefinder
function get_ui()
if (not GUI) then
GUI = UIRangefinder()
end
return GUI
end
-- ---------------------------------------------------------------------------------------------------------------------
-- UI Class
-- ---------------------------------------------------------------------------------------------------------------------
class "UIRangefinder" (CUIScriptWnd)
function UIRangefinder:__init() super()
dbg_printf("[WG] UI Rangefinder | Constructing GUI...")
self:Show(true)
self:Enable(true)
-- Build UI
self.m_xml = CScriptXmlInit()
self.m_xml:ParseFile("ui_western_goods_rangefinder.xml")
self.m_xml:InitWindow("rangefinder", 0, self)
self.m_los1 = self.m_xml:InitStatic("rangefinder:los1", self)
self.m_los2 = self.m_xml:InitStatic("rangefinder:los2", self)
self.m_los3 = self.m_xml:InitStatic("rangefinder:los3", self)
self.m_hcd1 = self.m_xml:InitStatic("rangefinder:hcd1", self)
self.m_hcd2 = self.m_xml:InitStatic("rangefinder:hcd2", self)
self.m_hcd3 = self.m_xml:InitStatic("rangefinder:hcd3", self)
self.m_reticule = self.m_xml:InitStatic("rangefinder:reticule", self)
-- Sound effects
self.m_sfx_btn = sound_object([[western_goods_tech\rangefinder_btn]])
-- Build variables
self.m_hud_fov = nil
self.m_update_timer = 0
self.m_update_freq = 222
self.m_night_vision = false
self.m_valid_sections = { "wg_rangefinder" }
-- Callbacks
RegisterScriptCallback("actor_on_weapon_zoom_in", self)
RegisterScriptCallback("actor_on_weapon_zoom_out", self)
end
function UIRangefinder:__finalize()
GUI = nil
end
function UIRangefinder:Update()
CUIScriptWnd.Update(self)
local tg = time_global()
if (tg < self.m_update_timer) then
return
else
self.m_update_timer = tg + self.m_update_freq
end
-- Line Of Sight Distance
local los1, los2, los3 = self:GetLOSTextures()
-- Horizontal Compensation Distance Distance
local hcd1, hcd2, hcd3 = self:GetHCDTextures()
-- Reticule
local ret = "rf_reticule"
-- Night Vision
local nv = self:GetNightVisionSuffix()
self.m_los1:InitTextureEx(los1..nv, [[hud\p3d]])
self.m_los2:InitTextureEx(los2..nv, [[hud\p3d]])
self.m_los3:InitTextureEx(los3..nv, [[hud\p3d]])
self.m_hcd1:InitTextureEx(hcd1..nv, [[hud\p3d]])
self.m_hcd2:InitTextureEx(hcd2..nv, [[hud\p3d]])
self.m_hcd3:InitTextureEx(hcd3..nv, [[hud\p3d]])
self.m_reticule:InitTextureEx(ret..nv, [[hud\p3d]])
end
function UIRangefinder:GetNightVisionSuffix()
return self.m_night_vision and "_g" or "_r"
end
function UIRangefinder:GetLOSTextures()
local los = math.floor(level.get_target_dist())
-- Get LOS number as a 3 char string (5 -> "005")
local s_los = western_goods_utils.as_string(los, 3)
-- Max range at 999m
if los > 999 then
return "rf_dash", "rf_dash", "rf_dash"
end
-- Line Of Sight format
local los1 = strformat("rf_num_%s", s_los:sub(1, 1))
local los2 = strformat("rf_num_%s", s_los:sub(2, 2))
local los3 = strformat("rf_num_%s", s_los:sub(3, 3))
return los1, los2, los3
end
function UIRangefinder:GetHCDTextures()
local los_pos = vector():mad(device().cam_pos, device().cam_dir, level.get_target_dist())
local hcd_pos = vector():set(los_pos.x, db.actor:position().y , los_pos.z)
local hcd = math.floor(western_goods_utils.get_distance(device().cam_pos, hcd_pos))
-- Get HCD number as a 3 char string (5 -> "005")
local s_hcd = western_goods_utils.as_string(hcd, 3)
-- Max range at 999m
if hcd > 999 then
return "rf_dash", "rf_dash", "rf_dash"
end
-- Horizontal Compensation Distance format
local hcd1 = strformat("rf_num_%s", s_hcd:sub(1, 1))
local hcd2 = strformat("rf_num_%s", s_hcd:sub(2, 2))
local hcd3 = strformat("rf_num_%s", s_hcd:sub(3, 3))
return hcd1, hcd2, hcd3
end
function UIRangefinder:actor_on_weapon_zoom_in(wpn)
if not western_goods_utils.table_contains(self.m_valid_sections, wpn:section()) then
return
end
local old_hud_fov = ui_options.get("video/basic/hud_fov")
local new_hud_fov = ini_sys:r_float_ex(wpn:section(),"scope_hud_fov")
exec_console_cmd("hud_fov ".. new_hud_fov)
self.m_hud_fov = old_hud_fov
RegisterScriptCallback("on_key_press", self)
dbg_printf("[WG] UI Rangefinder | Changed 'hud_fov' from '%s' to '%s'", old_hud_fov, new_hud_fov)
end
function UIRangefinder:actor_on_weapon_zoom_out(wpn)
if not western_goods_utils.table_contains(self.m_valid_sections, wpn:section()) then
return
end
local old_hud_fov = ui_options.get("video/basic/hud_fov")
local new_hud_fov = self.m_hud_fov
exec_console_cmd("hud_fov " .. new_hud_fov)
self.m_hud_fov = nil
UnregisterScriptCallback("on_key_press", self)
dbg_printf("[WG] UI Rangefinder | Restored 'hud_fov' from '%s' to '%s'", old_hud_fov, new_hud_fov)
end
function UIRangefinder:on_key_press(dik)
if dik ~= western_goods_mcm.get_config("rangefinder_nv") then
return
end
local active_item = db.actor:active_item()
if not active_item or not western_goods_utils.table_contains(self.m_valid_sections, active_item:section()) then
return
end
GUI.m_night_vision = not GUI.m_night_vision
GUI.m_sfx_btn:play(db.actor, 0, sound_object.s2d)
end