Divergent/mods/Western Goods/gamedata/scripts/western_goods_ui_readable.s...

276 lines
10 KiB
Plaintext

---==================================================================================================================---
--- ---
--- Original Author(s) : Tronex ---
--- Edited : NLTP_ASHES ---
--- Date : 20/02/2024 ---
--- License : Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) ---
--- ---
--- Based off item_recipe and item_map_kit by Tronex, script used to manage magazines in the Western Goods addon. ---
--- ---
--- Naming for some files/folders is based on the following contract : ---
--- - texture descriptions must be named like : "ui_<name_of_the_section>_page_<page_number>" ---
--- ---
--- When the Readable UI is displayed, the player will start healing psi-health. ---
--- The value of that healing is driven by CONST_MAGAZINE_PSI_REGEN and the factor defined in MCM. ---
--- ---
---==================================================================================================================---
-- ---------------------------------------------------------------------------------------------------------------------
-- Constants, global variables and imported functions
-- ---------------------------------------------------------------------------------------------------------------------
-- Imported functions
local dbg_printf = western_goods_utils.dbg_printf
-- Constants
local CONST_MAGAZINE_PSI_REGEN = 0.0080
-- Variables
local magazine_textures = {}
-- Singleton
GUI = nil -- instance, don't touch
-- ---------------------------------------------------------------------------------------------------------------------
-- General Functions
-- ---------------------------------------------------------------------------------------------------------------------
--- Function used to start displaying the Readable UI onto the screen.
--- @param sec string
--- @return nil
function display(sec)
if not sec then
printf("![WG] ERROR | UI Readable | No section passed!")
return
end
-- Hide inventory
if get_hud() then
hide_hud_inventory()
end
-- Create instance if needed
if not GUI then
GUI = UIReadable(sec)
end
-- Show magazine
if GUI and not GUI:IsShown() then
GUI:Restart(sec)
GUI:ShowDialog(true)
Register_UI("UIReadable","western_goods_ui_readable")
end
end
--- Function used to get the bonus for reading a magazine.
--- @return number
function get_psy_health_regeneration_magazine()
if not GUI or not GUI:IsShown() then
return 0.0
end
return CONST_MAGAZINE_PSI_REGEN * western_goods_mcm.get_config("magazine_psi_regen")
end
--- Function used to collect the name of the texture descriptions for all the readables.
--- @return nil
function collect_textures()
local file_count, tex_count = 0, 0
ini_sys:section_for_each(function(section)
local is_readable = ini_sys:r_bool_ex(section,"readable", false)
local is_base = string.find(section, "_base")
if is_readable and not is_base then
dbg_printf("[WG] UI Readable | Found a readable item '%s'", section)
magazine_textures[section] = western_goods_utils.collect_texture_descriptions("ui_" .. section .. "_page_")
file_count = file_count + 1
tex_count = tex_count + size_table(magazine_textures[section])
end
end)
dbg_printf("[WG] UI Readable | Collected a total of %s texture descriptions for %s magazines", tex_count, file_count)
end
-- ---------------------------------------------------------------------------------------------------------------------
-- Callbacks registration
-- ---------------------------------------------------------------------------------------------------------------------
--- Function used to register callbacks.
--- @return nil
function on_game_start()
RegisterScriptCallback("on_game_load", collect_textures)
end
-- ---------------------------------------------------------------------------------------------------------------------
-- UI Class
-- ---------------------------------------------------------------------------------------------------------------------
class "UIReadable" (CUIScriptWnd)
function UIReadable:__init(sec) super()
dbg_printf("[WG] UI Readable | Constructing GUI...")
self:Reset(sec)
self:InitControls()
self:InitCallBacks()
end
function UIReadable:__finalize()
GUI = nil
end
function UIReadable:Reset(sec)
self.m_section = sec
self.m_page_current = 1
self.m_page_count = magazine_textures[self.m_section] and size_table(magazine_textures[self.m_section]) or 0
end
function UIReadable:Restart(sec)
self:Reset(sec)
self.m_page:InitTexture(self:GetPageTexturePath(self.m_page_current))
dbg_printf("[WG] UI Readable | Reset GUI - '%s' (%s/%s)", self.m_section, self.m_page_current, self.m_page_count)
self:RefreshNavigationButtons()
end
function UIReadable:InitControls()
self:SetWndRect(Frect():set(0,0,1024,768))
self:SetAutoDelete(true)
self.m_xml = CScriptXmlInit()
self.m_xml:ParseFile("ui_western_goods_readable.xml")
self.m_dialog = self.m_xml:InitStatic("magazine", self)
self.m_dialog:SetStretchTexture(true)
-- Page Setup
self.m_page = self.m_xml:InitStatic("magazine:page",self.m_dialog)
self.m_page:InitTexture(self:GetPageTexturePath(self.m_page_current))
self.m_page:SetStretchTexture(true)
-- Button Ok
self.m_btn_ok_pic = self.m_xml:InitStatic("magazine:btn_ok_pic", self.m_dialog)
self.m_btn_ok_pic:InitTexture("ui_button_ordinary_h")
self.m_btn_ok_pic:SetStretchTexture(true)
self.m_btn_ok = self.m_xml:Init3tButton("magazine:btn_ok", self.m_dialog)
self:Register(self.m_btn_ok,"btn_ok")
-- Button Next
self.m_btn_next_pic = self.m_xml:InitStatic("magazine:btn_next_pic", self.m_dialog)
self.m_btn_next_pic:InitTexture("ui_button_ordinary_h")
self.m_btn_next_pic:SetStretchTexture(true)
self.m_btn_next = self.m_xml:Init3tButton("magazine:btn_next", self.m_dialog)
self:Register(self.m_btn_next,"btn_next")
-- Button Previous
self.m_btn_previous_pic = self.m_xml:InitStatic("magazine:btn_previous_pic", self.m_dialog)
self.m_btn_previous_pic:InitTexture("ui_button_ordinary_h")
self.m_btn_previous_pic:SetStretchTexture(true)
self.m_btn_previous = self.m_xml:Init3tButton("magazine:btn_previous", self.m_dialog)
self:Register(self.m_btn_previous,"btn_previous")
dbg_printf("[WG] UI Readable | Controls initialized...")
end
function UIReadable:InitCallBacks()
self:AddCallback("btn_ok", ui_events.BUTTON_CLICKED, self.OnClose, self)
self:AddCallback("btn_next", ui_events.BUTTON_CLICKED, self.OnNext, self)
self:AddCallback("btn_previous", ui_events.BUTTON_CLICKED, self.OnPrevious, self)
dbg_printf("[WG] UI Readable | CallBacks initialized...")
end
function UIReadable:OnNext()
if (self.m_page_current >= self.m_page_count) then return end
utils_obj.play_sound([[interface\inv_page]])
self.m_page_current = self.m_page_current + 1
self.m_page:InitTexture(self:GetPageTexturePath(self.m_page_current))
dbg_printf("[WG] UI Readable | To next page - '%s' (%s/%s)", self.m_section, self.m_page_current, self.m_page_count)
self:RefreshNavigationButtons()
end
function UIReadable:OnPrevious()
if (self.m_page_current <= 1) then return end
utils_obj.play_sound([[interface\inv_page]])
self.m_page_current = self.m_page_current - 1
self.m_page:InitTexture(self:GetPageTexturePath(self.m_page_current))
dbg_printf("[WG] UI Readable | To previous page - '%s' (%s/%s)", self.m_section, self.m_page_current, self.m_page_count)
self:RefreshNavigationButtons()
end
function UIReadable:OnClose()
utils_obj.play_sound([[interface\inv_close]])
self:HideDialog()
dbg_printf("[WG] UI Readable | Closed...")
Unregister_UI("UIReadable")
end
function UIReadable:OnKeyboard(dik, keyboard_action)
local res = CUIScriptWnd.OnKeyboard(self,dik,keyboard_action)
if res ~= false then
return res
end
if keyboard_action ~= ui_events.WINDOW_KEY_PRESSED then
return res
end
if dik == DIK_keys.DIK_ESCAPE then
self:OnClose()
elseif dik == DIK_keys.DIK_RIGHT then
self:OnNext()
elseif dik == DIK_keys.DIK_LEFT then
self:OnPrevious()
end
end
function UIReadable:RefreshNavigationButtons()
-- Reset all buttons
self.m_btn_previous_pic:Show(true)
self.m_btn_previous:Show(true)
self.m_btn_next_pic:Show(true)
self.m_btn_next:Show(true)
-- Hide previous button if we're on fist page
if (self.m_page_current <= 1) then
self.m_btn_previous_pic:Show(false)
self.m_btn_previous:Show(false)
end
-- Hide next button if we're on last page
if (self.m_page_current >= self.m_page_count) then
self.m_btn_next_pic:Show(false)
self.m_btn_next:Show(false)
end
end
function UIReadable:GetPageTexturePath(page_number)
local expected = "ui_" .. self.m_section .. "_page_" .. tostring(page_number)
for _,texture_id in pairs(magazine_textures[self.m_section]) do
if texture_id == expected then
return expected
end
end
printf("![WG] ERROR | UI Readable | Missing texture for '%s'. Expected texture '%s' !", self.m_section, expected)
return "ui_readable_missing_texture"
end