change loadUI/UI.display lua API

This commit is contained in:
Eduardo Bart 2011-11-17 18:40:31 -02:00
parent 63cbe11f7e
commit 55136fe866
21 changed files with 88 additions and 45 deletions

View File

@ -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

View File

@ -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()

View File

@ -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

View File

@ -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')

View File

@ -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)

View File

@ -1,22 +1,38 @@
UI = { }
UI.root = getRootWidget()
function UI.loadAndDisplayLocked(otuiFile)
local widget = loadUI(otuiFile, UI.root)
UI.root:lockChild(widget)
-- public functions
function UI.display(arg1, options)
local widget
local parent
if options then parent = options.parent end
parent = parent or UI.root
-- 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
-- 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
function UI.loadAndDisplay(otuiFile)
local widget = loadUI(otuiFile, UI.root)
return widget
end
function UI.display(widget)
UI.root:addChild(widget)
end
function UI.displayLocked(widget)
UI.root:addChild(widget)
UI.root:lockChild(widget)
end

View File

@ -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

View File

@ -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

View File

@ -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')

View File

@ -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')

View File

@ -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)

View File

@ -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)

View File

@ -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')

View File

@ -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')

View File

@ -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

View File

@ -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)

View File

@ -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')

View File

@ -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)

View File

@ -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()

View File

@ -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()

View File

@ -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);