new lua function for creating widgets: createWidget

This commit is contained in:
Eduardo Bart 2012-01-02 22:42:53 -02:00
parent 9fbdf3f5cb
commit 05230f44e4
29 changed files with 137 additions and 120 deletions

View File

@ -1,5 +1,5 @@
function dumpWidgets() function dumpWidgets()
for i=1,UI.root:getChildCount() do for i=1,rootWidget:getChildCount() do
print(UI.root:getChildByIndex(i):getId()) print(rootWidget:getChildByIndex(i):getId())
end end
end end

View File

@ -120,7 +120,7 @@ end
-- public functions -- public functions
function Console.init() function Console.init()
consoleWidget = UI.display('console.otui', { visible = false }) consoleWidget = displayUI('console.otui', { visible = false })
connect(consoleWidget, { onKeyPress = onKeyPress }) connect(consoleWidget, { onKeyPress = onKeyPress })
commandLineEdit = consoleWidget:getChildById('commandLineEdit') commandLineEdit = consoleWidget:getChildById('commandLineEdit')
@ -139,10 +139,8 @@ end
function Console.addLine(text, color) function Console.addLine(text, color)
-- create new line label -- create new line label
local numLines = consoleBuffer:getChildCount() + 1 local numLines = consoleBuffer:getChildCount() + 1
local label = UILabel.create() local label = createWidget('ConsoleLabel', consoleBuffer)
consoleBuffer:addChild(label)
label:setId('consoleLabel' .. numLines) label:setId('consoleLabel' .. numLines)
label:setStyle('ConsoleLabel')
label:setText(text) label:setText(text)
label:setForegroundColor(color) label:setForegroundColor(color)

View File

@ -1,10 +1,9 @@
-- place any code for testing purposes here -- place any code for testing purposes here
function init() function init()
local box = UIComboBox.create() local box = createWidget('ComboBox')
box:setStyle('ComboBox')
box:moveTo({x=100, y=8}) box:moveTo({x=100, y=8})
UI.display(box) displayUI(box)
end end
addEvent(init) addEvent(init)

View File

@ -5,7 +5,7 @@ local aboutWindow
-- public functions -- public functions
function About.create() function About.create()
aboutWindow = UI.display('about.otui', { locked = true }) aboutWindow = displayUI('about.otui', { locked = true })
end end
function About.destroy() function About.destroy()

View File

@ -5,7 +5,7 @@ local background
-- public functions -- public functions
function Background.create() function Background.create()
background = UI.display('background.otui') background = displayUI('background.otui')
end end
function Background.destroy() function Background.destroy()

View File

@ -56,7 +56,7 @@ function CharacterList.create(characters, premDays)
charactersWindow:destroy() charactersWindow:destroy()
end end
charactersWindow = UI.display('characterlist.otui') charactersWindow = displayUI('characterlist.otui')
characterList = charactersWindow:getChildById('characterList') characterList = charactersWindow:getChildById('characterList')
local accountStatusLabel = charactersWindow:getChildById('accountStatusLabel') local accountStatusLabel = charactersWindow:getChildById('accountStatusLabel')
charactersWindow.onKeyPress = onCharactersWindowKeyPress charactersWindow.onKeyPress = onCharactersWindowKeyPress
@ -68,10 +68,8 @@ function CharacterList.create(characters, premDays)
local worldHost = characterInfo[3] local worldHost = characterInfo[3]
local worldIp = characterInfo[4] local worldIp = characterInfo[4]
local label = UILabel.create() local label = createWidget('CharacterListLabel', characterList)
characterList:addChild(label)
label:setText(characterName .. ' (' .. worldName .. ')') label:setText(characterName .. ' (' .. worldName .. ')')
label:setStyle('CharacterListLabel')
label.characterName = characterName label.characterName = characterName
label.worldHost = worldHost label.worldHost = worldHost
label.worldPort = worldIp label.worldPort = worldIp

View File

@ -51,7 +51,7 @@ end
-- public functions -- public functions
function EnterGame.create() function EnterGame.create()
enterGame = UI.display('entergame.otui') enterGame = displayUI('entergame.otui')
local account = g_configs.get('account') local account = g_configs.get('account')
local password = g_configs.get('password') local password = g_configs.get('password')

View File

@ -36,14 +36,14 @@ end
function Options.enableFps(on) function Options.enableFps(on)
fpsEnabled = on fpsEnabled = on
local frameCounter = UI.root:recursiveGetChildById('frameCounter') local frameCounter = rootWidget:recursiveGetChildById('frameCounter')
frameCounter:setVisible(on) frameCounter:setVisible(on)
setConfig('showfps', on) setConfig('showfps', on)
end end
-- public functions -- public functions
function Options.create() function Options.create()
options = UI.display('options.otui', { locked = true }) options = displayUI('options.otui', { locked = true })
local fpsBox = options:getChildById('fpsCheckBox') local fpsBox = options:getChildById('fpsCheckBox')
local vsyncBox = options:getChildById('vsyncCheckBox') local vsyncBox = options:getChildById('vsyncCheckBox')

View File

@ -5,7 +5,7 @@ local topMenu
-- public functions -- public functions
function TopMenu.create() function TopMenu.create()
topMenu = UI.display('topmenu.otui') topMenu = displayUI('topmenu.otui')
end end
function TopMenu.destroy() function TopMenu.destroy()

View File

@ -10,8 +10,8 @@ Module
// NOTE: order does matter // NOTE: order does matter
dependencies: dependencies:
- core_scripts
- core_fonts - core_fonts
- core_styles - core_styles
- core_scripts
- core_widgets - core_widgets

View File

@ -1,17 +1,21 @@
UI = { } -- globals
UI.root = getRootWidget() rootWidget = g_ui.getRootWidget()
-- public functions -- public functions
function UI.display(arg1, options) function importStyle(otui)
g_ui.importStyle(resolveFileFullPath(otui, 2))
end
function displayUI(arg1, options)
local widget local widget
local parent local parent
if options then parent = options.parent end if options then parent = options.parent end
parent = parent or UI.root parent = parent or rootWidget
-- display otui files -- display otui files
if type(arg1) == 'string' then if type(arg1) == 'string' then
local otuiFilePath = resolveFileFullPath(arg1, 2) local otuiFilePath = resolveFileFullPath(arg1, 2)
widget = loadUI(otuiFilePath, parent) widget = g_ui.loadUI(otuiFilePath, parent)
-- display already loaded widgets -- display already loaded widgets
else else
widget = arg1 widget = arg1
@ -40,3 +44,14 @@ function UI.display(arg1, options)
end end
return widget return widget
end end
function createWidget(style, parent)
local className = g_ui.getStyleClass(style)
local class = _G[className]
local widget = class.create()
if parent then
parent:addChild(widget)
end
widget:setStyle(style)
return widget
end

View File

@ -5,16 +5,16 @@ Module
website: https://github.com/edubart/otclient website: https://github.com/edubart/otclient
onLoad: | onLoad: |
importStyles 'styles/buttons.otui' importStyle 'styles/buttons.otui'
importStyles 'styles/labels.otui' importStyle 'styles/labels.otui'
importStyles 'styles/panels.otui' importStyle 'styles/panels.otui'
importStyles 'styles/separators.otui' importStyle 'styles/separators.otui'
importStyles 'styles/lineedits.otui' importStyle 'styles/lineedits.otui'
importStyles 'styles/checkboxes.otui' importStyle 'styles/checkboxes.otui'
importStyles 'styles/windows.otui' importStyle 'styles/windows.otui'
importStyles 'styles/listboxes.otui' importStyle 'styles/listboxes.otui'
importStyles 'styles/items.otui' importStyle 'styles/items.otui'
importStyles 'styles/creatures.otui' importStyle 'styles/creatures.otui'
importStyles 'styles/comboboxes.otui' importStyle 'styles/comboboxes.otui'
importStyles 'styles/popupmenus.otui' importStyle 'styles/popupmenus.otui'
return true return true

View File

@ -35,6 +35,8 @@ PopupMenuSeparator < UIWidget
PopupMenu < UIPopupMenu PopupMenu < UIPopupMenu
width: 100 width: 100
button-style: PopupMenuButton
separator-style: PopupMenuSeparator
border-image: border-image:
source: /core_styles/images/menubox.png source: /core_styles/images/menubox.png
border: 3 border: 3

View File

@ -10,7 +10,7 @@ function MessageBox.create(title, text, flags)
setmetatable(box, MessageBox) setmetatable(box, MessageBox)
-- create messagebox window -- create messagebox window
local window = UI.display('messagebox.otui', { locked = true }) local window = displayUI('messagebox.otui', { locked = true })
window:setTitle(title) window:setTitle(title)
local label = window:getChildById('messageBoxLabel') local label = window:getChildById('messageBoxLabel')

View File

@ -20,7 +20,7 @@ end
function ToolTip.display(text) function ToolTip.display(text)
if text then if text then
ToolTip.hide() ToolTip.hide()
currentToolTip = UI.display('tooltip.otui') currentToolTip = displayUI('tooltip.otui')
currentToolTip.onMouseMove = moveToolTip currentToolTip.onMouseMove = moveToolTip
local label = currentToolTip:getChildById('toolTipText') local label = currentToolTip:getChildById('toolTipText')
label:setText(text) label:setText(text)

View File

@ -7,12 +7,11 @@ function UIPopupMenu.create()
local layout = UIVerticalLayout.create(menu) local layout = UIVerticalLayout.create(menu)
layout:setFitParent(true) layout:setFitParent(true)
menu:setLayout(layout) menu:setLayout(layout)
menu:setStyle('PopupMenu')
return menu return menu
end end
function UIPopupMenu.display(menu, pos) function UIPopupMenu.display(menu, pos)
UI.display(menu, {x = pos.x, y = pos.y}) displayUI(menu, {x = pos.x, y = pos.y})
menu:bindRectToParent() menu:bindRectToParent()
menu:grabMouse() menu:grabMouse()
menu:grabKeyboard() menu:grabKeyboard()
@ -20,21 +19,17 @@ function UIPopupMenu.display(menu, pos)
end end
function UIPopupMenu.addOption(menu, optionName, optionCallback) function UIPopupMenu.addOption(menu, optionName, optionCallback)
local optionWidget = UIButton.create() local optionWidget = createWidget(menu.buttonStyle, menu)
local lastOptionWidget = menu:getLastChild() local lastOptionWidget = menu:getLastChild()
optionWidget.onClick = function() optionWidget.onClick = function()
optionCallback() optionCallback()
menu:destroy() menu:destroy()
end end
optionWidget:setText(optionName) optionWidget:setText(optionName)
optionWidget:setStyle('PopupMenuButton')
menu:addChild(optionWidget)
end end
function UIPopupMenu.addSeparator(menu) function UIPopupMenu.addSeparator(menu)
local separatorWidget = UIWidget.create() local separatorWidget = createWidget(menu.separatorStyle, separator)
separatorWidget:setStyle('PopupMenuSeparator')
menu:addChild(separator)
end end
-- hooked events -- hooked events
@ -54,3 +49,12 @@ function UIPopupMenu.onKeyPress(menu, keyCode, keyText, keyboardModifiers)
end end
return false return false
end end
function UIPopupMenu.onStyleApply(menu, style)
if style['button-style'] then
menu.buttonStyle = style['button-style']
end
if style['separator-style'] then
menu.separatorStyle = style['separator-style']
end
end

View File

@ -16,8 +16,8 @@ end
function Game.createInterface() function Game.createInterface()
Background.hide() Background.hide()
CharacterList.destroyLoadBox() CharacterList.destroyLoadBox()
Game.gameUi = UI.display('game.otui') Game.gameUi = displayUI('game.otui')
UI.root:moveChildToIndex(Game.gameUi, 1) rootWidget:moveChildToIndex(Game.gameUi, 1)
Game.gameMapPanel = Game.gameUi:getChildById('mapPanel') Game.gameMapPanel = Game.gameUi:getChildById('mapPanel')
Game.gameRightPanel = Game.gameUi:getChildById('rightPanel') Game.gameRightPanel = Game.gameUi:getChildById('rightPanel')
Game.gameBottomPanel = Game.gameUi:getChildById('bottomPanel') Game.gameBottomPanel = Game.gameUi:getChildById('bottomPanel')

View File

@ -12,15 +12,14 @@ local function onCreatureSpeak(name, level, msgtype, message)
style = 'YellowChatLabel' style = 'YellowChatLabel'
end end
local label = UILabel.create() local label = createWidget(style)
label:setStyle(style)
label:setText(message) label:setText(message)
chatBuffer:addChild(label) chatBuffer:addChild(label)
end end
-- public functions -- public functions
function Chat.create() function Chat.create()
chatPanel = UI.display('chat.otui', { parent = Game.gameBottomPanel } ) chatPanel = displayUI('chat.otui', { parent = Game.gameBottomPanel } )
chatBuffer = chatPanel:getChildById('chatBuffer') chatBuffer = chatPanel:getChildById('chatBuffer')
end end

View File

@ -5,28 +5,20 @@ local healthManaPanel = nil
-- public functions -- public functions
function HealthBar.create() function HealthBar.create()
healthManaPanel = UI.display('healthbar.otui', { parent = Game.gameRightPanel }) healthManaPanel = displayUI('healthbar.otui', { parent = Game.gameRightPanel })
local healthBar = UIProgressBar.create() local healthBar = createWidget('HealthBar', healthManaPanel)
healthManaPanel:addChild(healthBar)
healthBar:setId('healthBar') healthBar:setId('healthBar')
healthBar:setStyle('HealthBar')
local healthLabel = UILabel.create() local healthLabel = createWidget('HealthLabel', healthManaPanel)
healthManaPanel:addChild(healthLabel)
healthLabel:setId('healthLabel') healthLabel:setId('healthLabel')
healthLabel:setStyle('HealthLabel')
healthLabel:setText('0 / 0') healthLabel:setText('0 / 0')
local manaBar = UIProgressBar.create() local manaBar = createWidget('ManaBar', healthManaPanel)
healthManaPanel:addChild(manaBar)
manaBar:setId('manaBar') manaBar:setId('manaBar')
manaBar:setStyle('ManaBar')
local manaLabel = UILabel.create() local manaLabel = createWidget('ManaLabel', healthManaPanel)
healthManaPanel:addChild(manaLabel)
manaLabel:setId('manaLabel') manaLabel:setId('manaLabel')
manaLabel:setStyle('ManaLabel')
manaLabel:setText('0 / 0') manaLabel:setText('0 / 0')
healthManaPanel:setHeight(healthBar:getHeight() + manaBar:getHeight() + 4) healthManaPanel:setHeight(healthBar:getHeight() + manaBar:getHeight() + 4)

View File

@ -5,7 +5,7 @@ local window = nil
-- public functions -- public functions
function Inventory.create() function Inventory.create()
window = UI.display('inventory.otui', { parent = Game.gameRightPanel }) window = displayUI('inventory.otui', { parent = Game.gameRightPanel })
end end
function Inventory.destroy() function Inventory.destroy()
@ -31,24 +31,23 @@ end
function Inventory.onInventoryItemMousePress(itemWidget, mousePos, mouseButton) function Inventory.onInventoryItemMousePress(itemWidget, mousePos, mouseButton)
if mouseButton ~= MouseRightButton then return end if mouseButton ~= MouseRightButton then return end
local item = itemWidget:getItem() local item = itemWidget:getItem()
if not item then return end if not item then return end
local menu = UIPopupMenu.create() local menu = createWidget('PopupMenu')
-- Look -- Look
local itemId = item:getId() local itemId = item:getId()
local slotId = tonumber(itemWidget:getId():sub(5)) local slotId = tonumber(itemWidget:getId():sub(5))
menu:addOption('Look', function() Game.lookAtInventory(itemId, slotId) end) menu:addOption('Look', function() Game.lookAtInventory(itemId, slotId) end)
-- Open or Use, depending if thing is a container -- Open or Use, depending if thing is a container
if item:isContainer() then if item:isContainer() then
menu:addOption('Open', function() print('open') end) menu:addOption('Open', function() print('open') end)
else else
menu:addOption('Use', function() print('use') end) menu:addOption('Use', function() print('use') end)
end end
menu:display(mousePos) menu:display(mousePos)
end end

View File

@ -1,5 +0,0 @@
PopupMenu
PopupMenuFirstButton
text: Look
PopupMenuLastButton
text: Use

View File

@ -40,7 +40,7 @@ local function onColorCheckChange(color)
elseif m_currentClothe:getId() == 'detail' then elseif m_currentClothe:getId() == 'detail' then
m_outfit.feet = m_currentColor.colorId m_outfit.feet = m_currentColor.colorId
end end
m_creature:setOutfit(m_outfit) m_creature:setOutfit(m_outfit)
end end
end end
@ -56,7 +56,7 @@ local function onClotheCheckChange(clothe)
m_currentClothe.onCheckChange = onClotheCheckChange m_currentClothe.onCheckChange = onClotheCheckChange
m_currentClothe = clothe m_currentClothe = clothe
local color = 0 local color = 0
if m_currentClothe:getId() == 'head' then if m_currentClothe:getId() == 'head' then
color = m_outfit.head color = m_outfit.head
@ -119,10 +119,8 @@ end
-- public functions -- public functions
function Outfit.test() function Outfit.test()
local button = UIButton.create() local button = createWidget('Button', rootWidget)
UI.root:addChild(button)
button:setText('Set Outfit') button:setText('Set Outfit')
button:setStyle('Button')
button:moveTo({x = 0, y = 100}) button:moveTo({x = 0, y = 100})
button:setWidth('100') button:setWidth('100')
button:setHeight('30') button:setHeight('30')
@ -131,11 +129,11 @@ end
function Outfit.create(creature, outfitList) function Outfit.create(creature, outfitList)
Outfit.destroy() Outfit.destroy()
window = UI.display('outfit.otui', { parent = UI.root }) window = displayUI('outfit.otui', { parent = rootWidget })
window:lock() window:lock()
m_outfit = creature:getOutfit() m_outfit = creature:getOutfit()
m_currentClothe = window:getChildById('head') m_currentClothe = window:getChildById('head')
window:getChildById('head').onCheckChange = onClotheCheckChange window:getChildById('head').onCheckChange = onClotheCheckChange
window:getChildById('primary').onCheckChange = onClotheCheckChange window:getChildById('primary').onCheckChange = onClotheCheckChange
@ -147,13 +145,10 @@ function Outfit.create(creature, outfitList)
for i=0,18 do for i=0,18 do
for j=0,6 do for j=0,6 do
local color = UICheckBox.create() local color = createWidget('Color', window)
window:addChild(color)
local outfitColor = getOufitColor(j*19 + i) local outfitColor = getOufitColor(j*19 + i)
color:setId('color' .. j*19+i) color:setId('color' .. j*19+i)
color.colorId = j*19 + i color.colorId = j*19 + i
color:setStyle('Color')
color:setBackgroundColor(outfitColor) color:setBackgroundColor(outfitColor)
color:setMarginTop(j * 3 + j * 14) color:setMarginTop(j * 3 + j * 14)
color:setMarginLeft(10 + i * 3 + i * 14) color:setMarginLeft(10 + i * 3 + i * 14)

View File

@ -41,7 +41,7 @@ end
-- public functions -- public functions
function Skills.create() function Skills.create()
skillWindow = UI.display('skills.otui', { parent = Game.gameRightPanel }) skillWindow = displayUI('skills.otui', { parent = Game.gameRightPanel })
end end
function Skills.destroy() function Skills.destroy()

View File

@ -1,37 +1,34 @@
TextMessage = {} TextMessage = {}
-- require styles -- require styles
importStyles 'textmessage.otui' importStyle 'textmessage.otui'
-- private variables -- private variables
local bottomLabelWidget, centerLabelWidget local bottomLabelWidget, centerLabelWidget
local messageTypes = { local messageTypes = {
first = 12, first = 12,
{ type = 'MessageOrange', color = '#C87832', showOnConsole = true, showOnWindow = false }, { msgtype = 'MessageOrange', color = '#C87832', showOnConsole = true, showOnWindow = false },
{ type = 'MessageOrange', color = '#C87832', showOnConsole = true, showOnWindow = false }, { msgtype = 'MessageOrange', color = '#C87832', showOnConsole = true, showOnWindow = false },
{ type = 'MessageRed', color = '#C83200', showOnConsole = true, showOnWindow = true, windowLocation = 'CenterLabel' }, { msgtype = 'MessageRed', color = '#C83200', showOnConsole = true, showOnWindow = true, windowLocation = 'CenterLabel' },
{ type = 'MessageWhite', color = '#FFFFFF', showOnConsole = true, showOnWindow = true, windowLocation = 'CenterLabel' }, { msgtype = 'MessageWhite', color = '#FFFFFF', showOnConsole = true, showOnWindow = true, windowLocation = 'CenterLabel' },
{ type = 'MessageWhite', color = '#FFFFFF', showOnConsole = true, showOnWindow = true, windowLocation = 'BottomLabel' }, { msgtype = 'MessageWhite', color = '#FFFFFF', showOnConsole = true, showOnWindow = true, windowLocation = 'BottomLabel' },
{ type = 'MessageWhite', color = '#FFFFFF', showOnConsole = true, showOnWindow = true, windowLocation = 'BottomLabel' }, { msgtype = 'MessageWhite', color = '#FFFFFF', showOnConsole = true, showOnWindow = true, windowLocation = 'BottomLabel' },
{ type = 'MessageGreen', color = '#3FBE32', showOnConsole = true, showOnWindow = true, windowLocation = 'CenterLabel' }, { msgtype = 'MessageGreen', color = '#3FBE32', showOnConsole = true, showOnWindow = true, windowLocation = 'CenterLabel' },
{ type = 'MessageWhite', color = '#FFFFFF', showOnConsole = false, showOnWindow = true, windowLocation = 'BottomLabel' }, { msgtype = 'MessageWhite', color = '#FFFFFF', showOnConsole = false, showOnWindow = true, windowLocation = 'BottomLabel' },
{ type = 'MessageBlue', color = '#3264C8', showOnConsole = true, showOnWindow = false }, { msgtype = 'MessageBlue', color = '#3264C8', showOnConsole = true, showOnWindow = false },
{ type = 'MessageRed', color = '#C83200', showOnConsole = true, showOnWindow = false } { msgtype = 'MessageRed', color = '#C83200', showOnConsole = true, showOnWindow = false }
} }
local hideEvent local hideEvent
-- public functions -- public functions
function TextMessage.create() function TextMessage.create()
bottomLabelWidget = UILabel.create() bottomLabelWidget = createWidget('UILabel', Game.gameMapPanel)
Game.gameMapPanel:addChild(bottomLabelWidget) centerLabelWidget = createWidget('UILabel', Game.gameMapPanel)
centerLabelWidget = UILabel.create()
Game.gameMapPanel:addChild(centerLabelWidget)
end end
-- hooked events -- hooked events
function TextMessage.onTextMessage(type, message) function TextMessage.onTextMessage(msgtype, message)
local messageType = messageTypes[type - messageTypes.first] local messageType = messageTypes[msgtype - messageTypes.first]
if messageType.showOnConsole then if messageType.showOnConsole then
-- TODO -- TODO

View File

@ -5,7 +5,7 @@ local vipWindow = nil
-- public functions -- public functions
function VipList.create() function VipList.create()
vipWindow = UI.display('viplist.otui', { parent = Game.gameRightPanel }) vipWindow = displayUI('viplist.otui', { parent = Game.gameRightPanel })
end end
function VipList.destroy() function VipList.destroy()
@ -17,11 +17,9 @@ end
function Game.onAddVip(id, name, online) function Game.onAddVip(id, name, online)
local vipList = vipWindow:getChildById('vipList') local vipList = vipWindow:getChildById('vipList')
local label = UILabel.create() local label = createWidget('VipListLabel', vipList)
vipList:addChild(label)
label:setId('vip' .. id) label:setId('vip' .. id)
label:setText(name) label:setText(name)
label:setStyle('VipListLabel')
if online then if online then
label:setForegroundColor('#00ff00') label:setForegroundColor('#00ff00')

View File

@ -241,7 +241,13 @@ void Application::registerLuaFunctions()
g_lua.bindClassStaticFunction<Logger>("fireOldMessages", std::bind(&Logger::fireOldMessages, &g_logger)); g_lua.bindClassStaticFunction<Logger>("fireOldMessages", std::bind(&Logger::fireOldMessages, &g_logger));
g_lua.bindClassStaticFunction<Logger>("setOnLog", std::bind(&Logger::setOnLog, &g_logger, _1)); g_lua.bindClassStaticFunction<Logger>("setOnLog", std::bind(&Logger::setOnLog, &g_logger, _1));
// Font // UI
g_lua.registerStaticClass("g_ui");
g_lua.bindClassStaticFunction("g_ui", "importStyle", std::bind(&UIManager::importStyle, &g_ui, _1));
g_lua.bindClassStaticFunction("g_ui", "getStyle", std::bind(&UIManager::getStyle, &g_ui, _1));
g_lua.bindClassStaticFunction("g_ui", "getStyleClass", std::bind(&UIManager::getStyleClass, &g_ui, _1));
g_lua.bindClassStaticFunction("g_ui", "loadUI", std::bind(&UIManager::loadUI, &g_ui, _1, _2));
g_lua.bindClassStaticFunction("g_ui", "getRootWidget", std::bind(&UIManager::getRootWidget, &g_ui));
/* /*
// FontManager // FontManager
@ -260,8 +266,5 @@ void Application::registerLuaFunctions()
// global functions // global functions
g_lua.bindGlobalFunction("importFont", std::bind(&FontManager::importFont, &g_fonts, _1)); g_lua.bindGlobalFunction("importFont", std::bind(&FontManager::importFont, &g_fonts, _1));
g_lua.bindGlobalFunction("importStyles", std::bind(&UIManager::importStyles, &g_ui, _1));
g_lua.bindGlobalFunction("setDefaultFont", std::bind(&FontManager::setDefaultFont, &g_fonts, _1)); g_lua.bindGlobalFunction("setDefaultFont", std::bind(&FontManager::setDefaultFont, &g_fonts, _1));
g_lua.bindGlobalFunction("loadUI", std::bind(&UIManager::loadUI, &g_ui, _1, _2));
g_lua.bindGlobalFunction("getRootWidget", std::bind(&UIManager::getRootWidget, &g_ui));
} }

View File

@ -226,7 +226,7 @@ bool luavalue_cast(int index, Size& size)
} }
// otml nodes // otml nodes
void push_luavalue(const OTMLNodePtr& node) void push_otml_subnode_luavalue(const OTMLNodePtr& node)
{ {
if(node->hasValue()) { if(node->hasValue()) {
g_lua.pushString(node->value()); g_lua.pushString(node->value());
@ -251,6 +251,20 @@ void push_luavalue(const OTMLNodePtr& node)
g_lua.pushNil(); g_lua.pushNil();
} }
void push_luavalue(const OTMLNodePtr& node)
{
g_lua.newTable();
for(const OTMLNodePtr& cnode : node->children()) {
if(cnode->isUnique()) {
push_otml_subnode_luavalue(cnode);
if(!g_lua.isNil()) {
g_lua.setField(cnode->tag());
} else
g_lua.pop();
}
}
}
bool luavalue_cast(int index, OTMLNodePtr& node) bool luavalue_cast(int index, OTMLNodePtr& node)
{ {
node = OTMLNode::create(); node = OTMLNode::create();

View File

@ -81,7 +81,7 @@ void UIManager::inputEvent(const InputEvent& event)
}; };
} }
bool UIManager::importStyles(const std::string& file) bool UIManager::importStyle(const std::string& file)
{ {
try { try {
OTMLDocumentPtr doc = OTMLDocument::parse(file); OTMLDocumentPtr doc = OTMLDocument::parse(file);
@ -135,13 +135,21 @@ OTMLNodePtr UIManager::getStyle(const std::string& styleName)
// styles starting with UI are automatically defined // styles starting with UI are automatically defined
if(boost::starts_with(styleName, "UI")) { if(boost::starts_with(styleName, "UI")) {
OTMLNodePtr node = OTMLNode::create(); OTMLNodePtr node = OTMLNode::create();
node->writeAt("__widgetType", styleName); node->writeAt("__class", styleName);
return node; return node;
} }
return nullptr; return nullptr;
} }
std::string UIManager::getStyleClass(const std::string& styleName)
{
OTMLNodePtr style = getStyle(styleName);
if(style && style->get("__class"))
return style->valueAt("__class");
return "";
}
UIWidgetPtr UIManager::loadUI(const std::string& file, const UIWidgetPtr& parent) UIWidgetPtr UIManager::loadUI(const std::string& file, const UIWidgetPtr& parent)
{ {
try { try {
@ -176,7 +184,7 @@ UIWidgetPtr UIManager::loadWidgetFromOTML(const OTMLNodePtr& widgetNode, const U
OTMLNodePtr styleNode = originalStyleNode->clone(); OTMLNodePtr styleNode = originalStyleNode->clone();
styleNode->merge(widgetNode); styleNode->merge(widgetNode);
std::string widgetType = styleNode->valueAt("__widgetType"); std::string widgetType = styleNode->valueAt("__class");
// call widget creation from lua // call widget creation from lua
UIWidgetPtr widget = g_lua.callGlobalField<UIWidgetPtr>(widgetType, "create"); UIWidgetPtr widget = g_lua.callGlobalField<UIWidgetPtr>(widgetType, "create");

View File

@ -37,9 +37,10 @@ public:
void resize(const Size& size); void resize(const Size& size);
void inputEvent(const InputEvent& event); void inputEvent(const InputEvent& event);
bool importStyles(const std::string& file); bool importStyle(const std::string& file);
void importStyleFromOTML(const OTMLNodePtr& styleNode); void importStyleFromOTML(const OTMLNodePtr& styleNode);
OTMLNodePtr getStyle(const std::string& styleName); OTMLNodePtr getStyle(const std::string& styleName);
std::string getStyleClass(const std::string& styleName);
UIWidgetPtr loadUI(const std::string& file, const UIWidgetPtr& parent = nullptr); UIWidgetPtr loadUI(const std::string& file, const UIWidgetPtr& parent = nullptr);
UIWidgetPtr loadWidgetFromOTML(const OTMLNodePtr& widgetNode, const UIWidgetPtr& parent); UIWidgetPtr loadWidgetFromOTML(const OTMLNodePtr& widgetNode, const UIWidgetPtr& parent);