383 lines
11 KiB
Plaintext
383 lines
11 KiB
Plaintext
|
-- try with barter ui lol
|
||
|
gc = game.translate_string
|
||
|
print_dbg = barter_core.print_dbg
|
||
|
|
||
|
-- imports
|
||
|
get_barter_list = barter_core.get_barter_list
|
||
|
barter_get_data = barter_core.barter_get_data
|
||
|
build_string = barter_core.build_string
|
||
|
do_barter = barter_core.do_barter
|
||
|
get_desc = barter_core.get_desc
|
||
|
npc_has_barter = barter_core.npc_has_barter
|
||
|
-- temp way to start trade window
|
||
|
|
||
|
----------------------------------------------------------------------
|
||
|
GUI = nil -- instance, don't touch
|
||
|
function start()
|
||
|
|
||
|
local hud = get_hud()
|
||
|
if (hud) then
|
||
|
hide_hud_inventory()
|
||
|
end
|
||
|
if (not GUI) then
|
||
|
GUI = UIBarter()
|
||
|
end
|
||
|
|
||
|
if (GUI) and (not GUI:IsShown()) then
|
||
|
GUI:Reset()
|
||
|
GUI:ShowDialog(true)
|
||
|
Register_UI("UIBarter","barter_ui")
|
||
|
end
|
||
|
end
|
||
|
|
||
|
function ui_inventory.UIInventory:InitBarterButton()
|
||
|
self.npc_barter_btn = self.xml:Init3tButton("npc:trade_delimiter:trade_buy_button", self.npc_trade)
|
||
|
local weight = self.npc_trade_weight:GetWndPos()
|
||
|
-- printf("trade button at %s %s, delimter at %s %s", pos.x, pos.y, rcoord.x, rcoord.y)
|
||
|
local pos = self.npc_barter_btn:GetWndPos()
|
||
|
|
||
|
self.npc_barter_btn:SetWndPos(vector2():set(
|
||
|
weight.x + 20 , -- TODO: Add sliders in MCM to adjust button position
|
||
|
pos.y
|
||
|
))
|
||
|
|
||
|
self.npc_barter_btn:TextControl():SetText(game.translate_string("st_ui_mm_barter"))
|
||
|
|
||
|
self:Register(self.npc_barter_btn, "barter")
|
||
|
|
||
|
self:InitBarterButtonCallback()
|
||
|
|
||
|
local npc = mob_trade.GetTalkingNpc()
|
||
|
local enable = npc_has_barter(npc)
|
||
|
self.npc_barter_btn:Enable(enable)
|
||
|
|
||
|
end
|
||
|
|
||
|
function ui_inventory.UIInventory:InitBarterButtonCallback()
|
||
|
self:AddCallback("barter", ui_events.BUTTON_CLICKED, start, self)
|
||
|
end
|
||
|
|
||
|
function GUI_on_show(name, path)
|
||
|
local instance = ui_inventory.GUI
|
||
|
if name ~= "UIInventory" or (not instance ) then return end
|
||
|
|
||
|
if (not instance.npc_barter_btn) then
|
||
|
instance:InitBarterButton()
|
||
|
end
|
||
|
local partner = instance:GetPartner()
|
||
|
instance.npc_barter_btn:Show(partner and npc_has_barter(partner))
|
||
|
end
|
||
|
|
||
|
function on_game_start()
|
||
|
RegisterScriptCallback("GUI_on_show", GUI_on_show)
|
||
|
end
|
||
|
|
||
|
----------------------------------------------------------------------
|
||
|
-- UI
|
||
|
----------------------------------------------------------------------
|
||
|
class "UIBarter" (CUIScriptWnd)
|
||
|
|
||
|
function UIBarter:__init(obj,section) super()
|
||
|
self.section = section
|
||
|
self.obj = obj
|
||
|
|
||
|
-- Reload settings
|
||
|
self.clr_b = utils_xml.get_color("pda_blue")
|
||
|
|
||
|
self:InitControls()
|
||
|
self:InitCallBacks()
|
||
|
end
|
||
|
|
||
|
function UIBarter:__finalize()
|
||
|
end
|
||
|
|
||
|
function UIBarter:InitControls()
|
||
|
self:SetWndRect (Frect():set(0,0,1024,768))
|
||
|
self:SetAutoDelete(true)
|
||
|
|
||
|
self.xml = CScriptXmlInit()
|
||
|
local xml = self.xml
|
||
|
xml:ParseFile ("ui_barter.xml")
|
||
|
|
||
|
self.dialog = xml:InitStatic("barter", self)
|
||
|
xml:InitStatic("barter:background", self.dialog)
|
||
|
self.cap_menu = xml:InitTextWnd("barter:cap_tab" , self.dialog)
|
||
|
|
||
|
self.cap = {}
|
||
|
self.b_inv = {}
|
||
|
self.b_item = {}
|
||
|
self.list = {} -- List of items
|
||
|
self.cell_item = {} -- infobox (rename)
|
||
|
self.cell_scroll = {} -- infobox scroll (rename)
|
||
|
self.text_item = {} -- Item to be traded for
|
||
|
self.CC = {}
|
||
|
for i=1,2 do
|
||
|
self.cap[i] = xml:InitStatic("barter:cap_barter_" .. tostring(i),self.dialog)
|
||
|
self.b_inv[i] = xml:InitStatic("barter:back_inv_" .. tostring(i) , self.dialog)
|
||
|
self.b_item[i] = xml:InitStatic("barter:back_item_" .. tostring(i) , self.dialog)
|
||
|
self.text_item[i] = xml:InitTextWnd("barter:text_item_" .. tostring(i) , self.dialog)
|
||
|
self.cell_item[i] = xml:InitTextWnd("barter:box_item_" .. tostring(i) , nil)
|
||
|
self.cell_scroll[i] = xml:InitScrollView("barter:scroll_desc_" .. tostring(i) , self.dialog)
|
||
|
self.cell_scroll[i]:AddWindow(self.cell_item[i], true)
|
||
|
self.cell_item[i]:SetAutoDelete(false)
|
||
|
|
||
|
self.CC[i] = barter_cellcont.UIBarterCellContainer(i, self, nil, "barter:cont_inv_" .. tostring(i), self.dialog)
|
||
|
self.CC[i].can_select = true
|
||
|
self.CC[i].disable_drag = true
|
||
|
self.CC[i].showcase = true
|
||
|
self.CC[i]:SetGridSpecs(35, 2)
|
||
|
end
|
||
|
self.CC[1].sort_method = "none"
|
||
|
self.CC[2].sort_method = "none"
|
||
|
self.item_info = utils_ui.UIInfoItem(self, 1000)
|
||
|
|
||
|
-- Main icon - change to npc face
|
||
|
self.box_item_main = xml:InitStatic("barter:pic_item_main" , self.dialog)
|
||
|
self.box_item_main_temp = xml:InitStatic("barter:pic_item_main" , self.dialog)
|
||
|
|
||
|
-- Main Buttons
|
||
|
self.btn_barter = xml:Init3tButton("barter:btn_barter", self.dialog)
|
||
|
self:Register(self.btn_barter,"btn_barter")
|
||
|
|
||
|
self.btn_cancel = xml:Init3tButton("barter:btn_back", self.dialog)
|
||
|
self:Register(self.btn_cancel,"btn_cancel")
|
||
|
end
|
||
|
|
||
|
function UIBarter:InitCallBacks()
|
||
|
self:AddCallback("btn_barter", ui_events.BUTTON_CLICKED, self.OnBarter, self)
|
||
|
self:AddCallback("btn_cancel", ui_events.BUTTON_CLICKED, self.OnCancel, self)
|
||
|
end
|
||
|
|
||
|
function UIBarter:Reset()
|
||
|
local npc = mob_trade.GetTalkingNpc()
|
||
|
self.section = npc:section()
|
||
|
|
||
|
-- Settings
|
||
|
self.barter_frame = "ui_itm_repair_1"
|
||
|
self.barters = get_barter_list(npc) -- barters for this npc
|
||
|
self.index_barter = {} --
|
||
|
self.selected = "" -- currently selected barter
|
||
|
|
||
|
-- Elements
|
||
|
self.CC[2]:Reset()
|
||
|
self.text_item[1]:SetText("")
|
||
|
self.text_item[2]:SetText("")
|
||
|
self.cell_item[1]:SetText("")
|
||
|
self.cell_item[2]:SetText("")
|
||
|
self.btn_barter:Enable(false)
|
||
|
-- string.format(gc("st_ui_barter_rules"), get_config("cond"))
|
||
|
local to_show = true --self.use_parts and true or false
|
||
|
self.cap[2]:Show(to_show)
|
||
|
self.b_inv[2]:Show(to_show)
|
||
|
self.b_item[2]:Show(to_show)
|
||
|
self.text_item[2]:Show(to_show)
|
||
|
self.CC[2]:Show(to_show)
|
||
|
|
||
|
-- Set npc face
|
||
|
if npc:section() == "m_trader" then
|
||
|
self.box_item_main:InitTexture( "ui_saff" )
|
||
|
else
|
||
|
self.box_item_main:InitTexture( npc:character_icon() )
|
||
|
end
|
||
|
-- Set npc trader name
|
||
|
self.cap_menu:SetText(gc(npc:character_name()))
|
||
|
|
||
|
-- Show barters
|
||
|
self:InitInventory(1)
|
||
|
|
||
|
-- Hide active item
|
||
|
actor_effects.toggle_active_slot(0)
|
||
|
end
|
||
|
|
||
|
local clr_g = utils_xml.get_color("d_green", true)
|
||
|
local clr_r = utils_xml.get_color("d_red", true)
|
||
|
function UIBarter:InitInventory(n)
|
||
|
local inv = {}
|
||
|
local inf = {}
|
||
|
|
||
|
-- get all the barters for this npc
|
||
|
-- gray out items and make unclickable if not valid
|
||
|
if n == 1 then
|
||
|
local prev_rank = nil
|
||
|
for k,v in pairs(self.barters) do
|
||
|
-- custom cell container supports adding 'newlines' which will force subsequent items to start occupying the next line
|
||
|
-- default behavior segments based on sort rank
|
||
|
if not prev_rank then prev_rep = v.sort_rank end
|
||
|
if prev_rank ~= v.sort_rank then
|
||
|
inv[#inv+1] = "newline"
|
||
|
inf[#inf+1] = ""
|
||
|
prev_rank = v.sort_rank
|
||
|
end
|
||
|
inv[#inv+1] = v.give.sec
|
||
|
inf[#inf+1] = v.amt and v.amt .. "/" .. v.limit or ""
|
||
|
end
|
||
|
self.CC[1]:Reinit(inv, inf)
|
||
|
|
||
|
-- colorize what can be traded for
|
||
|
for i,ci in pairs(self.CC[1].cell) do
|
||
|
if ci:IsShown() then
|
||
|
local b = self.barters[ci.indx]
|
||
|
if b.allowed then
|
||
|
ci:Colorize("def")
|
||
|
else
|
||
|
ci:Colorize("hide")
|
||
|
end
|
||
|
if ci.flags.info ~= "" then
|
||
|
local clr = b.amt < b.limit and clr_g or clr_r
|
||
|
ci:Add_CustomText(ci.flags.info, nil, nil, clr, fnt)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
-- get all the required items to exchange, grayed out if requirements not met
|
||
|
elseif n == 2 then
|
||
|
-- assume self.selected has the selected barter
|
||
|
if not self.selected then return end
|
||
|
local barter_data = self.barters[self.selected]
|
||
|
local take = barter_data.take_items
|
||
|
for i=1,#take do
|
||
|
if take[i].sec == "money" then
|
||
|
inv[#inv+1] = "money_1000_5000"
|
||
|
inf[#inf+1] = take[i].limit
|
||
|
else
|
||
|
inv[#inv+1] = take[i].sec
|
||
|
inf[#inf+1] = take[i].amt .. "/" .. take[i].limit
|
||
|
end
|
||
|
end
|
||
|
|
||
|
-- second arg to reinit paints the numbered cell with the appropriate text
|
||
|
self.CC[2]:Reinit(inv, inf)
|
||
|
local fnt = GetFontSmall()
|
||
|
for i,ci in pairs(self.CC[2].cell) do
|
||
|
if ci:IsShown() then
|
||
|
local data = take[ci.indx]
|
||
|
local clr = clr_r
|
||
|
if data.amt < data.limit then
|
||
|
ci:Colorize("hide")
|
||
|
else
|
||
|
ci:Colorize("def")
|
||
|
clr = clr_g
|
||
|
end
|
||
|
ci:Add_CustomText(ci.flags.info, nil, nil, clr, fnt)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
end
|
||
|
end
|
||
|
|
||
|
function UIBarter:Update()
|
||
|
CUIScriptWnd.Update(self)
|
||
|
|
||
|
-- Warning messages timer
|
||
|
if (self.msg_wnd_timer and time_global() > self.msg_wnd_timer) then
|
||
|
self.msg_wnd_timer = nil
|
||
|
self.msg_wnd:Show(false)
|
||
|
end
|
||
|
|
||
|
-- Updating item info box and item cell containers
|
||
|
local found_cell = false
|
||
|
for name,cc in pairs(self.CC) do
|
||
|
found_cell = cc:Update(self.item_info) or found_cell
|
||
|
end
|
||
|
if (not found_cell) then
|
||
|
self.item_info:Update()
|
||
|
end
|
||
|
end
|
||
|
|
||
|
-- Callbacks
|
||
|
function UIBarter:On_CC_Mouse1(cont, idx)
|
||
|
-- idx is the cell clicked, check index_barter to determine name
|
||
|
local ci = self.CC[cont].cell[idx]
|
||
|
if ci then
|
||
|
self:OnItemSelect(cont)
|
||
|
if cont == 1 then
|
||
|
self:InitInventory(2)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
-- set the text box to be scrolly n shit
|
||
|
function UIBarter:SetWindowText(n, str)
|
||
|
|
||
|
self.cell_item[n]:SetText(str)
|
||
|
self.cell_item[n]:AdjustHeightToText()
|
||
|
self.cell_item[n]:SetWndSize(vector2():set(self.cell_item[n]:GetWidth(),self.cell_item[n]:GetHeight()+10))
|
||
|
self.cell_scroll[n]:Clear()
|
||
|
self.cell_scroll[n]:AddWindow(self.cell_item[n], true)
|
||
|
self.cell_item[n]:SetAutoDelete(false)
|
||
|
|
||
|
end
|
||
|
|
||
|
function UIBarter:OnItemSelect(n)
|
||
|
local obj_1 = self.CC[1]:GetCell_Selected()
|
||
|
local idx = obj_1.indx
|
||
|
|
||
|
if n == 1 then
|
||
|
self.selected = idx
|
||
|
-- -- Text (Name)
|
||
|
local give = self.barters[self.selected].give
|
||
|
local name = barter_core.get_give_str(give)
|
||
|
-- set name of barter give item
|
||
|
self.text_item[n]:SetText(name)
|
||
|
self:SetWindowText(1, gc(get_desc(self.barters[self.selected].name)))
|
||
|
self:SetWindowText(2, build_string(self.barters[self.selected].name))
|
||
|
|
||
|
if self.barters[self.selected].type == "allOf" then
|
||
|
self.btn_barter:Enable(self.barters[self.selected].allowed)
|
||
|
else
|
||
|
self.btn_barter:Enable(false)
|
||
|
end
|
||
|
else
|
||
|
-- for anyof barters, if the selected item is sufficient use that
|
||
|
local barter_data = self.barters[self.selected]
|
||
|
if barter_data.type ~= "anyOf" then return end
|
||
|
-- check if the selected cell is full
|
||
|
local obj_n = self.CC[n]:GetCell_Selected()
|
||
|
local n_indx = obj_n.indx
|
||
|
local take = barter_data.take_items[n_indx]
|
||
|
print_dbg("Any of check index %s, selected item %s", n_indx, take.sec)
|
||
|
local name = ui_item.get_sec_name(take.sec)
|
||
|
-- set name of barter give item
|
||
|
self.text_item[n]:SetText(name)
|
||
|
self.btn_barter:Enable(take.amt >= take.limit and barter_data.allowed)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
function UIBarter:OnBarter()
|
||
|
local obj_1 = self.CC[1]:GetCell_Selected()
|
||
|
local obj_2 = self.CC[2]:GetCell_Selected()
|
||
|
|
||
|
do_barter(self.barters[self.selected].name, self.barters[self.selected].give, self.barters[self.selected].take_items, obj_2 and obj_2.indx)
|
||
|
-- consider resetting ui?
|
||
|
self.CC[1]:Reset()
|
||
|
CreateTimeEvent("barter", math.random(69), 0, function()
|
||
|
self:Reset()
|
||
|
return true
|
||
|
end)
|
||
|
end
|
||
|
|
||
|
function UIBarter:OnKeyboard(dik, keyboard_action)
|
||
|
local res = CUIScriptWnd.OnKeyboard(self,dik,keyboard_action)
|
||
|
if (res == false) then
|
||
|
for i=1,#self.CC do
|
||
|
if self.CC[i]:IsShown() then
|
||
|
self.CC[i]:OnKeyboard(dik, keyboard_action)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
local bind = dik_to_bind(dik)
|
||
|
if keyboard_action == ui_events.WINDOW_KEY_PRESSED then
|
||
|
if dik == DIK_keys.DIK_ESCAPE then
|
||
|
self:OnCancel()
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
return res
|
||
|
end
|
||
|
|
||
|
function UIBarter:OnCancel()
|
||
|
utils_obj.play_sound("interface\\inv_close")
|
||
|
|
||
|
self:HideDialog()
|
||
|
|
||
|
Unregister_UI("UIBarter")
|
||
|
end
|