From 55136fe866071404424ec567a5913f463baf2c1d Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Thu, 17 Nov 2011 18:40:31 -0200 Subject: [PATCH] change loadUI/UI.display lua API --- modules/about/about.lua | 2 +- modules/background/background.lua | 2 +- modules/chat/chat.lua | 2 +- modules/console/console.lua | 3 +- modules/core/ext/table.lua | 2 +- modules/core/ui.lua | 48 +++++++++++++++++++---------- modules/core/util.lua | 24 +++++++++++++++ modules/entergame/characterlist.lua | 2 +- modules/entergame/entergame.lua | 2 +- modules/game/game.lua | 2 +- modules/health_mana/health_mana.lua | 2 +- modules/inventory/inventory.lua | 2 +- modules/messagebox/messagebox.lua | 2 +- modules/options/options.lua | 2 +- modules/outfit/outfit.lua | 16 +++++----- modules/playground/playground.lua | 2 +- modules/skills/skills.lua | 2 +- modules/tooltip/tooltip.lua | 2 +- modules/topmenu/topmenu.lua | 2 +- modules/viplist/viplist.lua | 2 +- src/framework/ui/uiwidget.cpp | 8 +++-- 21 files changed, 87 insertions(+), 44 deletions(-) diff --git a/modules/about/about.lua b/modules/about/about.lua index e25fa8af..98ce5ff0 100644 --- a/modules/about/about.lua +++ b/modules/about/about.lua @@ -5,7 +5,7 @@ local about -- public functions function About.create() - about = UI.loadAndDisplay("/about/about.otui") + about = UI.display('about.otui') UI.root:lockChild(about) end diff --git a/modules/background/background.lua b/modules/background/background.lua index 9df08f07..59235a49 100644 --- a/modules/background/background.lua +++ b/modules/background/background.lua @@ -5,7 +5,7 @@ local background -- public functions function Background.create() - background = UI.loadAndDisplay('/background/background.otui') + background = UI.display('background.otui') end function Background.destroy() diff --git a/modules/chat/chat.lua b/modules/chat/chat.lua index 55da45af..7a77fa3e 100644 --- a/modules/chat/chat.lua +++ b/modules/chat/chat.lua @@ -20,7 +20,7 @@ end -- public functions function Chat.create() - chatPanel = loadUI("/chat/chat.otui", Game.gameBottomPanel) + chatPanel = UI.display('chat.otui', { parent = Game.gameBottomPanel } ) chatBuffer = chatPanel:getChildById('chatBuffer') end diff --git a/modules/console/console.lua b/modules/console/console.lua index 99db32e8..01250d76 100644 --- a/modules/console/console.lua +++ b/modules/console/console.lua @@ -120,8 +120,7 @@ end -- public functions function Console.init() - consoleWidget = UI.loadAndDisplay("/console/console.otui") - consoleWidget:hide() + consoleWidget = UI.display('console.otui', { visible = false }) connect(consoleWidget, { onKeyPress = onKeyPress }) commandLineEdit = consoleWidget:getChildById('commandLineEdit') diff --git a/modules/core/ext/table.lua b/modules/core/ext/table.lua index 14f607ea..8627cc5f 100644 --- a/modules/core/ext/table.lua +++ b/modules/core/ext/table.lua @@ -3,7 +3,7 @@ function table.dump(t, depth) for k,v in pairs(t) do str = string.rep(' ', depth * 2) .. k .. ': ' if type(v) ~= "table" then - print(str .. v) + print(str .. tostring(v)) else print(str) table.dump(v, depth+1) diff --git a/modules/core/ui.lua b/modules/core/ui.lua index 535a1e7e..0bf1c10d 100644 --- a/modules/core/ui.lua +++ b/modules/core/ui.lua @@ -1,22 +1,38 @@ UI = { } UI.root = getRootWidget() -function UI.loadAndDisplayLocked(otuiFile) - local widget = loadUI(otuiFile, UI.root) - UI.root:lockChild(widget) - return widget -end +-- public functions +function UI.display(arg1, options) + local widget + local parent + if options then parent = options.parent end + parent = parent or UI.root -function UI.loadAndDisplay(otuiFile) - local widget = loadUI(otuiFile, UI.root) - return widget -end + -- display otui files + if type(arg1) == 'string' then + local otuiFilePath = resolveFileFullPath(arg1, 2) + widget = loadUI(otuiFilePath, parent) + -- display already loaded widgets + else + widget = arg1 + if parent:hasChild(widget) then + widget:focus() + widget:show() + else + parent:addChild(widget) + widget:show() + end + end -function UI.display(widget) - UI.root:addChild(widget) -end - -function UI.displayLocked(widget) - UI.root:addChild(widget) - UI.root:lockChild(widget) + -- apply display options + if widget and options then + for option,value in pairs(options) do + if option == 'locked' and value then + widget:lock() + elseif option == 'visible' then + widget:setVisible(value) + end + end + end + return widget end diff --git a/modules/core/util.lua b/modules/core/util.lua index e1ea9157..ac26a4ae 100644 --- a/modules/core/util.lua +++ b/modules/core/util.lua @@ -29,3 +29,27 @@ function dumpWidgets() print(UI.root:getChildByIndex(i):getId()) end end + +function getCallingScriptSourcePath(depth) + depth = depth or 2 + local info = debug.getinfo(1+depth, "Sn") + local path + if info.short_src then + path = info.short_src:match("(.*)/.*") + end + if not path then + path = '/' + elseif path:sub(0, 1) ~= '/' then + path = '/' .. path + end + return path +end + +function resolveFileFullPath(filePath, depth) + depth = depth or 1 + if filePath:sub(0, 1) ~= '/' then + return getCallingScriptSourcePath(depth+1) .. '/' .. filePath + else + return filePath + end +end diff --git a/modules/entergame/characterlist.lua b/modules/entergame/characterlist.lua index 8ee7f4fc..c9dc7388 100644 --- a/modules/entergame/characterlist.lua +++ b/modules/entergame/characterlist.lua @@ -56,7 +56,7 @@ function CharacterList.create(characters, premDays) charactersWindow:destroy() end - charactersWindow = UI.loadAndDisplay('/entergame/characterlist.otui') + charactersWindow = UI.display('characterlist.otui') characterList = charactersWindow:getChildById('characterList') local accountStatusLabel = charactersWindow:getChildById('accountStatusLabel') charactersWindow.onKeyPress = onCharactersWindowKeyPress diff --git a/modules/entergame/entergame.lua b/modules/entergame/entergame.lua index 6a4f395d..8336940b 100644 --- a/modules/entergame/entergame.lua +++ b/modules/entergame/entergame.lua @@ -51,7 +51,7 @@ end -- public functions function EnterGame.create() - enterGame = UI.loadAndDisplay('/entergame/entergame.otui') + enterGame = UI.display('entergame.otui') local account = Configs.get('account') local password = Configs.get('password') diff --git a/modules/game/game.lua b/modules/game/game.lua index e40ec8c2..e4234c38 100644 --- a/modules/game/game.lua +++ b/modules/game/game.lua @@ -16,7 +16,7 @@ end function Game.createInterface() Background.hide() CharacterList.destroyLoadBox() - Game.gameUi = UI.loadAndDisplay('/game/game.otui') + Game.gameUi = UI.display('/game/game.otui') UI.root:moveChildToIndex(Game.gameUi, 1) Game.gameMapPanel = Game.gameUi:getChildById('mapPanel') Game.gameRightPanel = Game.gameUi:getChildById('rightPanel') diff --git a/modules/health_mana/health_mana.lua b/modules/health_mana/health_mana.lua index a67073f7..e8fe8f1a 100644 --- a/modules/health_mana/health_mana.lua +++ b/modules/health_mana/health_mana.lua @@ -5,7 +5,7 @@ local healthManaPanel = nil -- public functions function HealthMana.create() - healthManaPanel = loadUI("/health_mana/health_mana.otui", Game.gameRightPanel) + healthManaPanel = UI.display('health_mana.otui', { parent = Game.gameRightPanel }) local healthBar = UIProgressBar.create() healthManaPanel:addChild(healthBar) diff --git a/modules/inventory/inventory.lua b/modules/inventory/inventory.lua index 5420a50b..d4a60dc6 100644 --- a/modules/inventory/inventory.lua +++ b/modules/inventory/inventory.lua @@ -16,7 +16,7 @@ local InventorySlotAmmo = 10 -- public functions function Inventory.create() - window = loadUI("/inventory/inventory.otui", Game.gameRightPanel) + window = UI.display('inventory.otui', { parent = Game.gameRightPanel }) local itemWidget = window:getChildById('feet') window:setHeight(itemWidget:getPosition().y + itemWidget:getHeight() - window:getPosition().y) diff --git a/modules/messagebox/messagebox.lua b/modules/messagebox/messagebox.lua index 74b34612..6486371a 100644 --- a/modules/messagebox/messagebox.lua +++ b/modules/messagebox/messagebox.lua @@ -10,7 +10,7 @@ function MessageBox.create(title, text, flags) setmetatable(box, MessageBox) -- create messagebox window - local window = UI.loadAndDisplayLocked('/messagebox/messagebox.otui') + local window = UI.display('messagebox.otui', { locked = true }) window:setTitle(title) local label = window:getChildById('messageBoxLabel') diff --git a/modules/options/options.lua b/modules/options/options.lua index 035d775f..b1517666 100644 --- a/modules/options/options.lua +++ b/modules/options/options.lua @@ -43,7 +43,7 @@ end -- public functions function Options.create() - options = UI.loadAndDisplayLocked("/options/options.otui") + options = UI.display('options.otui', { locked = true }) local fpsBox = options:getChildById('fpsCheckBox') local vsyncBox = options:getChildById('vsyncCheckBox') diff --git a/modules/outfit/outfit.lua b/modules/outfit/outfit.lua index 016d7bbb..219b5756 100644 --- a/modules/outfit/outfit.lua +++ b/modules/outfit/outfit.lua @@ -61,7 +61,7 @@ local function update() m_outfit.type = m_outfits[currentOutfit][1] m_outfit.addons = 0 m_creature:setOutfit(m_outfit) - + end local function onColorCheckChange(color) @@ -74,9 +74,9 @@ local function onColorCheckChange(color) m_currentColor:setChecked(false) local color2 = m_currentColor m_currentColor.onCheckChange = function() onColorCheckChange(color2) end - + m_currentColor = color - + m_outfit.head = m_currentColor.colorId m_creature:setOutfit(m_outfit) end @@ -96,9 +96,9 @@ end function Outfit.create(creature, outfitList) Outfit.destroy() - window = loadUI("/outfit/outfit.otui", UI.root) + window = UI.display('outfit.otui', { parent = UI.root }) window:lock() - + m_outfit = creature:getOutfit() local creatureWidget = window:getChildById('creature') @@ -115,19 +115,19 @@ function Outfit.create(creature, outfitList) color:setBackgroundColor(outfitColor) color:setMarginTop(j * 3 + j * 14) color:setMarginLeft(10 + i * 3 + i * 14) - + if j*19 + i == m_outfit.head then m_currentColor = color color:setChecked(true) end - + color.onCheckChange = function() onColorCheckChange(color) end end end m_creature = creature m_outfits = outfitList - + currentOutfit = 1 for i=1,#outfitList do if outfitList[i][1] == m_outfit.type then diff --git a/modules/playground/playground.lua b/modules/playground/playground.lua index a593de0a..2f3d65e1 100644 --- a/modules/playground/playground.lua +++ b/modules/playground/playground.lua @@ -11,7 +11,7 @@ function UIItem.onMouseRelease(self, mousePos, mouseButton) local menuFile = self:getStyle()['popup menu'] if not menuFile then return end - local popupMenu = UI.loadAndDisplay(menuFile) + local popupMenu = UI.display(menuFile) if not popupMenu then return end popupMenu:moveTo(mousePos) diff --git a/modules/skills/skills.lua b/modules/skills/skills.lua index ceb7f481..bf018cc1 100644 --- a/modules/skills/skills.lua +++ b/modules/skills/skills.lua @@ -6,7 +6,7 @@ local skills = {"Fist Fighting", "Club Fighting", "Sword Fighting", "Axe Fightin -- public functions function Skills.create() - skillWindow = loadUI("/skills/skills.otui", Game.gameRightPanel) + skillWindow = UI.display('skills.otui', { parent = Game.gameRightPanel }) local skillPanel = skillWindow:getChildById('skillPanel') diff --git a/modules/tooltip/tooltip.lua b/modules/tooltip/tooltip.lua index 2ba13d59..3c68fb4f 100644 --- a/modules/tooltip/tooltip.lua +++ b/modules/tooltip/tooltip.lua @@ -20,7 +20,7 @@ end function ToolTip.display(text) if text then ToolTip.hide() - currentToolTip = UI.loadAndDisplay('/tooltip/tooltip.otui', UI.root) + currentToolTip = UI.display('tooltip.otui') currentToolTip.onMouseMove = moveToolTip local label = currentToolTip:getChildById('toolTipText') label:setText(text) diff --git a/modules/topmenu/topmenu.lua b/modules/topmenu/topmenu.lua index b0bd928a..28874b2a 100644 --- a/modules/topmenu/topmenu.lua +++ b/modules/topmenu/topmenu.lua @@ -5,7 +5,7 @@ local topMenu -- public functions function TopMenu.create() - topMenu = UI.loadAndDisplay("/topmenu/topmenu.otui") + topMenu = UI.display('topmenu.otui') end function TopMenu.destroy() diff --git a/modules/viplist/viplist.lua b/modules/viplist/viplist.lua index 9df157fd..ed68da0a 100644 --- a/modules/viplist/viplist.lua +++ b/modules/viplist/viplist.lua @@ -5,7 +5,7 @@ local vipWindow = nil -- public functions function VipList.create() - vipWindow = loadUI("/viplist/viplist.otui", Game.gameRightPanel) + vipWindow = UI.display('viplist.otui', { parent = Game.gameRightPanel }) end function VipList.destroy() diff --git a/src/framework/ui/uiwidget.cpp b/src/framework/ui/uiwidget.cpp index b398f029..97c05fa1 100644 --- a/src/framework/ui/uiwidget.cpp +++ b/src/framework/ui/uiwidget.cpp @@ -156,10 +156,14 @@ void UIWidget::setStyleFromNode(const OTMLNodePtr& styleNode) void UIWidget::setParent(const UIWidgetPtr& parent) { - UIWidgetPtr self = asUIWidget(); - // remove from old parent UIWidgetPtr oldParent = getParent(); + + // the parent is already the same + if(oldParent == parent) + return; + + UIWidgetPtr self = asUIWidget(); if(oldParent && oldParent->hasChild(self)) oldParent->removeChild(self);