From 05230f44e41115f47c1082b8b0137712feebfdb1 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Mon, 2 Jan 2012 22:42:53 -0200 Subject: [PATCH] new lua function for creating widgets: createWidget --- modules/addon_console/commands.lua | 4 +-- modules/addon_console/console.lua | 6 ++-- modules/addon_playground/playground.lua | 5 ++- modules/client_about/about.lua | 2 +- modules/client_background/background.lua | 2 +- modules/client_entergame/characterlist.lua | 6 ++-- modules/client_entergame/entergame.lua | 2 +- modules/client_options/options.lua | 4 +-- modules/client_topmenu/topmenu.lua | 2 +- modules/core/core.otmod | 2 +- modules/core_scripts/ui.lua | 25 +++++++++++--- modules/core_styles/core_styles.otmod | 24 +++++++------- modules/core_styles/styles/popupmenus.otui | 2 ++ .../core_widgets/messagebox/messagebox.lua | 2 +- modules/core_widgets/tooltip/tooltip.lua | 2 +- modules/core_widgets/uipopupmenu.lua | 20 ++++++----- modules/game/game.lua | 4 +-- modules/game_chat/chat.lua | 5 ++- modules/game_healthbar/healthbar.lua | 18 +++------- modules/game_inventory/inventory.lua | 13 ++++---- modules/game_inventory/itempopupmenu.otui | 5 --- modules/game_outfit/outfit.lua | 17 ++++------ modules/game_skills/skills.lua | 2 +- modules/game_textmessage/textmessage.lua | 33 +++++++++---------- modules/game_viplist/viplist.lua | 6 ++-- src/framework/luafunctions.cpp | 11 ++++--- src/framework/luascript/luavaluecasts.cpp | 16 ++++++++- src/framework/ui/uimanager.cpp | 14 ++++++-- src/framework/ui/uimanager.h | 3 +- 29 files changed, 137 insertions(+), 120 deletions(-) delete mode 100644 modules/game_inventory/itempopupmenu.otui diff --git a/modules/addon_console/commands.lua b/modules/addon_console/commands.lua index 03ddf63d..e2c0c8be 100644 --- a/modules/addon_console/commands.lua +++ b/modules/addon_console/commands.lua @@ -1,5 +1,5 @@ function dumpWidgets() - for i=1,UI.root:getChildCount() do - print(UI.root:getChildByIndex(i):getId()) + for i=1,rootWidget:getChildCount() do + print(rootWidget:getChildByIndex(i):getId()) end end \ No newline at end of file diff --git a/modules/addon_console/console.lua b/modules/addon_console/console.lua index 41a8e222..d6c17a03 100644 --- a/modules/addon_console/console.lua +++ b/modules/addon_console/console.lua @@ -120,7 +120,7 @@ end -- public functions function Console.init() - consoleWidget = UI.display('console.otui', { visible = false }) + consoleWidget = displayUI('console.otui', { visible = false }) connect(consoleWidget, { onKeyPress = onKeyPress }) commandLineEdit = consoleWidget:getChildById('commandLineEdit') @@ -139,10 +139,8 @@ end function Console.addLine(text, color) -- create new line label local numLines = consoleBuffer:getChildCount() + 1 - local label = UILabel.create() - consoleBuffer:addChild(label) + local label = createWidget('ConsoleLabel', consoleBuffer) label:setId('consoleLabel' .. numLines) - label:setStyle('ConsoleLabel') label:setText(text) label:setForegroundColor(color) diff --git a/modules/addon_playground/playground.lua b/modules/addon_playground/playground.lua index 02ec1df1..c6c5dac0 100644 --- a/modules/addon_playground/playground.lua +++ b/modules/addon_playground/playground.lua @@ -1,10 +1,9 @@ -- place any code for testing purposes here function init() - local box = UIComboBox.create() - box:setStyle('ComboBox') + local box = createWidget('ComboBox') box:moveTo({x=100, y=8}) - UI.display(box) + displayUI(box) end addEvent(init) \ No newline at end of file diff --git a/modules/client_about/about.lua b/modules/client_about/about.lua index 64b92909..7bee0540 100644 --- a/modules/client_about/about.lua +++ b/modules/client_about/about.lua @@ -5,7 +5,7 @@ local aboutWindow -- public functions function About.create() - aboutWindow = UI.display('about.otui', { locked = true }) + aboutWindow = displayUI('about.otui', { locked = true }) end function About.destroy() diff --git a/modules/client_background/background.lua b/modules/client_background/background.lua index 59235a49..baaeb35e 100644 --- a/modules/client_background/background.lua +++ b/modules/client_background/background.lua @@ -5,7 +5,7 @@ local background -- public functions function Background.create() - background = UI.display('background.otui') + background = displayUI('background.otui') end function Background.destroy() diff --git a/modules/client_entergame/characterlist.lua b/modules/client_entergame/characterlist.lua index e8b378f9..83ba161a 100644 --- a/modules/client_entergame/characterlist.lua +++ b/modules/client_entergame/characterlist.lua @@ -56,7 +56,7 @@ function CharacterList.create(characters, premDays) charactersWindow:destroy() end - charactersWindow = UI.display('characterlist.otui') + charactersWindow = displayUI('characterlist.otui') characterList = charactersWindow:getChildById('characterList') local accountStatusLabel = charactersWindow:getChildById('accountStatusLabel') charactersWindow.onKeyPress = onCharactersWindowKeyPress @@ -68,10 +68,8 @@ function CharacterList.create(characters, premDays) local worldHost = characterInfo[3] local worldIp = characterInfo[4] - local label = UILabel.create() - characterList:addChild(label) + local label = createWidget('CharacterListLabel', characterList) label:setText(characterName .. ' (' .. worldName .. ')') - label:setStyle('CharacterListLabel') label.characterName = characterName label.worldHost = worldHost label.worldPort = worldIp diff --git a/modules/client_entergame/entergame.lua b/modules/client_entergame/entergame.lua index ba38bb3c..45a33292 100644 --- a/modules/client_entergame/entergame.lua +++ b/modules/client_entergame/entergame.lua @@ -51,7 +51,7 @@ end -- public functions function EnterGame.create() - enterGame = UI.display('entergame.otui') + enterGame = displayUI('entergame.otui') local account = g_configs.get('account') local password = g_configs.get('password') diff --git a/modules/client_options/options.lua b/modules/client_options/options.lua index 68f6060a..42f11087 100644 --- a/modules/client_options/options.lua +++ b/modules/client_options/options.lua @@ -36,14 +36,14 @@ end function Options.enableFps(on) fpsEnabled = on - local frameCounter = UI.root:recursiveGetChildById('frameCounter') + local frameCounter = rootWidget:recursiveGetChildById('frameCounter') frameCounter:setVisible(on) setConfig('showfps', on) end -- public functions function Options.create() - options = UI.display('options.otui', { locked = true }) + options = displayUI('options.otui', { locked = true }) local fpsBox = options:getChildById('fpsCheckBox') local vsyncBox = options:getChildById('vsyncCheckBox') diff --git a/modules/client_topmenu/topmenu.lua b/modules/client_topmenu/topmenu.lua index fb1662c1..984e466e 100644 --- a/modules/client_topmenu/topmenu.lua +++ b/modules/client_topmenu/topmenu.lua @@ -5,7 +5,7 @@ local topMenu -- public functions function TopMenu.create() - topMenu = UI.display('topmenu.otui') + topMenu = displayUI('topmenu.otui') end function TopMenu.destroy() diff --git a/modules/core/core.otmod b/modules/core/core.otmod index 88849485..31460779 100644 --- a/modules/core/core.otmod +++ b/modules/core/core.otmod @@ -10,8 +10,8 @@ Module // NOTE: order does matter dependencies: + - core_scripts - core_fonts - core_styles - - core_scripts - core_widgets diff --git a/modules/core_scripts/ui.lua b/modules/core_scripts/ui.lua index 7c292e89..d0aad1c9 100644 --- a/modules/core_scripts/ui.lua +++ b/modules/core_scripts/ui.lua @@ -1,17 +1,21 @@ -UI = { } -UI.root = getRootWidget() +-- globals +rootWidget = g_ui.getRootWidget() -- public functions -function UI.display(arg1, options) +function importStyle(otui) + g_ui.importStyle(resolveFileFullPath(otui, 2)) +end + +function displayUI(arg1, options) local widget local parent if options then parent = options.parent end - parent = parent or UI.root + parent = parent or rootWidget -- display otui files if type(arg1) == 'string' then local otuiFilePath = resolveFileFullPath(arg1, 2) - widget = loadUI(otuiFilePath, parent) + widget = g_ui.loadUI(otuiFilePath, parent) -- display already loaded widgets else widget = arg1 @@ -40,3 +44,14 @@ function UI.display(arg1, options) end return widget 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 diff --git a/modules/core_styles/core_styles.otmod b/modules/core_styles/core_styles.otmod index a8ef6bfa..05928328 100644 --- a/modules/core_styles/core_styles.otmod +++ b/modules/core_styles/core_styles.otmod @@ -5,16 +5,16 @@ Module website: https://github.com/edubart/otclient onLoad: | - importStyles 'styles/buttons.otui' - importStyles 'styles/labels.otui' - importStyles 'styles/panels.otui' - importStyles 'styles/separators.otui' - importStyles 'styles/lineedits.otui' - importStyles 'styles/checkboxes.otui' - importStyles 'styles/windows.otui' - importStyles 'styles/listboxes.otui' - importStyles 'styles/items.otui' - importStyles 'styles/creatures.otui' - importStyles 'styles/comboboxes.otui' - importStyles 'styles/popupmenus.otui' + importStyle 'styles/buttons.otui' + importStyle 'styles/labels.otui' + importStyle 'styles/panels.otui' + importStyle 'styles/separators.otui' + importStyle 'styles/lineedits.otui' + importStyle 'styles/checkboxes.otui' + importStyle 'styles/windows.otui' + importStyle 'styles/listboxes.otui' + importStyle 'styles/items.otui' + importStyle 'styles/creatures.otui' + importStyle 'styles/comboboxes.otui' + importStyle 'styles/popupmenus.otui' return true diff --git a/modules/core_styles/styles/popupmenus.otui b/modules/core_styles/styles/popupmenus.otui index a8006386..5d1f991c 100644 --- a/modules/core_styles/styles/popupmenus.otui +++ b/modules/core_styles/styles/popupmenus.otui @@ -35,6 +35,8 @@ PopupMenuSeparator < UIWidget PopupMenu < UIPopupMenu width: 100 + button-style: PopupMenuButton + separator-style: PopupMenuSeparator border-image: source: /core_styles/images/menubox.png border: 3 \ No newline at end of file diff --git a/modules/core_widgets/messagebox/messagebox.lua b/modules/core_widgets/messagebox/messagebox.lua index cd4704ba..4eecbae6 100644 --- a/modules/core_widgets/messagebox/messagebox.lua +++ b/modules/core_widgets/messagebox/messagebox.lua @@ -10,7 +10,7 @@ function MessageBox.create(title, text, flags) setmetatable(box, MessageBox) -- create messagebox window - local window = UI.display('messagebox.otui', { locked = true }) + local window = displayUI('messagebox.otui', { locked = true }) window:setTitle(title) local label = window:getChildById('messageBoxLabel') diff --git a/modules/core_widgets/tooltip/tooltip.lua b/modules/core_widgets/tooltip/tooltip.lua index 20630da0..118ed624 100644 --- a/modules/core_widgets/tooltip/tooltip.lua +++ b/modules/core_widgets/tooltip/tooltip.lua @@ -20,7 +20,7 @@ end function ToolTip.display(text) if text then ToolTip.hide() - currentToolTip = UI.display('tooltip.otui') + currentToolTip = displayUI('tooltip.otui') currentToolTip.onMouseMove = moveToolTip local label = currentToolTip:getChildById('toolTipText') label:setText(text) diff --git a/modules/core_widgets/uipopupmenu.lua b/modules/core_widgets/uipopupmenu.lua index ad49efd3..e6bb25bc 100644 --- a/modules/core_widgets/uipopupmenu.lua +++ b/modules/core_widgets/uipopupmenu.lua @@ -7,12 +7,11 @@ function UIPopupMenu.create() local layout = UIVerticalLayout.create(menu) layout:setFitParent(true) menu:setLayout(layout) - menu:setStyle('PopupMenu') return menu end 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:grabMouse() menu:grabKeyboard() @@ -20,21 +19,17 @@ function UIPopupMenu.display(menu, pos) end function UIPopupMenu.addOption(menu, optionName, optionCallback) - local optionWidget = UIButton.create() + local optionWidget = createWidget(menu.buttonStyle, menu) local lastOptionWidget = menu:getLastChild() optionWidget.onClick = function() optionCallback() menu:destroy() end optionWidget:setText(optionName) - optionWidget:setStyle('PopupMenuButton') - menu:addChild(optionWidget) end function UIPopupMenu.addSeparator(menu) - local separatorWidget = UIWidget.create() - separatorWidget:setStyle('PopupMenuSeparator') - menu:addChild(separator) + local separatorWidget = createWidget(menu.separatorStyle, separator) end -- hooked events @@ -54,3 +49,12 @@ function UIPopupMenu.onKeyPress(menu, keyCode, keyText, keyboardModifiers) end return false 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 \ No newline at end of file diff --git a/modules/game/game.lua b/modules/game/game.lua index 81b8d6ab..6b07c236 100644 --- a/modules/game/game.lua +++ b/modules/game/game.lua @@ -16,8 +16,8 @@ end function Game.createInterface() Background.hide() CharacterList.destroyLoadBox() - Game.gameUi = UI.display('game.otui') - UI.root:moveChildToIndex(Game.gameUi, 1) + Game.gameUi = displayUI('game.otui') + rootWidget:moveChildToIndex(Game.gameUi, 1) Game.gameMapPanel = Game.gameUi:getChildById('mapPanel') Game.gameRightPanel = Game.gameUi:getChildById('rightPanel') Game.gameBottomPanel = Game.gameUi:getChildById('bottomPanel') diff --git a/modules/game_chat/chat.lua b/modules/game_chat/chat.lua index 7a77fa3e..2207293e 100644 --- a/modules/game_chat/chat.lua +++ b/modules/game_chat/chat.lua @@ -12,15 +12,14 @@ local function onCreatureSpeak(name, level, msgtype, message) style = 'YellowChatLabel' end - local label = UILabel.create() - label:setStyle(style) + local label = createWidget(style) label:setText(message) chatBuffer:addChild(label) end -- public functions function Chat.create() - chatPanel = UI.display('chat.otui', { parent = Game.gameBottomPanel } ) + chatPanel = displayUI('chat.otui', { parent = Game.gameBottomPanel } ) chatBuffer = chatPanel:getChildById('chatBuffer') end diff --git a/modules/game_healthbar/healthbar.lua b/modules/game_healthbar/healthbar.lua index ebc5202c..440b7ea9 100644 --- a/modules/game_healthbar/healthbar.lua +++ b/modules/game_healthbar/healthbar.lua @@ -5,28 +5,20 @@ local healthManaPanel = nil -- public functions function HealthBar.create() - healthManaPanel = UI.display('healthbar.otui', { parent = Game.gameRightPanel }) + healthManaPanel = displayUI('healthbar.otui', { parent = Game.gameRightPanel }) - local healthBar = UIProgressBar.create() - healthManaPanel:addChild(healthBar) + local healthBar = createWidget('HealthBar', healthManaPanel) healthBar:setId('healthBar') - healthBar:setStyle('HealthBar') - local healthLabel = UILabel.create() - healthManaPanel:addChild(healthLabel) + local healthLabel = createWidget('HealthLabel', healthManaPanel) healthLabel:setId('healthLabel') - healthLabel:setStyle('HealthLabel') healthLabel:setText('0 / 0') - local manaBar = UIProgressBar.create() - healthManaPanel:addChild(manaBar) + local manaBar = createWidget('ManaBar', healthManaPanel) manaBar:setId('manaBar') - manaBar:setStyle('ManaBar') - local manaLabel = UILabel.create() - healthManaPanel:addChild(manaLabel) + local manaLabel = createWidget('ManaLabel', healthManaPanel) manaLabel:setId('manaLabel') - manaLabel:setStyle('ManaLabel') manaLabel:setText('0 / 0') healthManaPanel:setHeight(healthBar:getHeight() + manaBar:getHeight() + 4) diff --git a/modules/game_inventory/inventory.lua b/modules/game_inventory/inventory.lua index becdfffe..99b09934 100644 --- a/modules/game_inventory/inventory.lua +++ b/modules/game_inventory/inventory.lua @@ -5,7 +5,7 @@ local window = nil -- public functions function Inventory.create() - window = UI.display('inventory.otui', { parent = Game.gameRightPanel }) + window = displayUI('inventory.otui', { parent = Game.gameRightPanel }) end function Inventory.destroy() @@ -31,24 +31,23 @@ end function Inventory.onInventoryItemMousePress(itemWidget, mousePos, mouseButton) if mouseButton ~= MouseRightButton then return end - + local item = itemWidget:getItem() if not item then return end - - local menu = UIPopupMenu.create() - + + local menu = createWidget('PopupMenu') + -- Look local itemId = item:getId() local slotId = tonumber(itemWidget:getId():sub(5)) menu:addOption('Look', function() Game.lookAtInventory(itemId, slotId) end) - + -- Open or Use, depending if thing is a container if item:isContainer() then menu:addOption('Open', function() print('open') end) else menu:addOption('Use', function() print('use') end) end - menu:display(mousePos) end diff --git a/modules/game_inventory/itempopupmenu.otui b/modules/game_inventory/itempopupmenu.otui deleted file mode 100644 index 0de6cb7e..00000000 --- a/modules/game_inventory/itempopupmenu.otui +++ /dev/null @@ -1,5 +0,0 @@ -PopupMenu - PopupMenuFirstButton - text: Look - PopupMenuLastButton - text: Use \ No newline at end of file diff --git a/modules/game_outfit/outfit.lua b/modules/game_outfit/outfit.lua index dd808735..7bd3313c 100644 --- a/modules/game_outfit/outfit.lua +++ b/modules/game_outfit/outfit.lua @@ -40,7 +40,7 @@ local function onColorCheckChange(color) elseif m_currentClothe:getId() == 'detail' then m_outfit.feet = m_currentColor.colorId end - + m_creature:setOutfit(m_outfit) end end @@ -56,7 +56,7 @@ local function onClotheCheckChange(clothe) m_currentClothe.onCheckChange = onClotheCheckChange m_currentClothe = clothe - + local color = 0 if m_currentClothe:getId() == 'head' then color = m_outfit.head @@ -119,10 +119,8 @@ end -- public functions function Outfit.test() - local button = UIButton.create() - UI.root:addChild(button) + local button = createWidget('Button', rootWidget) button:setText('Set Outfit') - button:setStyle('Button') button:moveTo({x = 0, y = 100}) button:setWidth('100') button:setHeight('30') @@ -131,11 +129,11 @@ end function Outfit.create(creature, outfitList) Outfit.destroy() - window = UI.display('outfit.otui', { parent = UI.root }) + window = displayUI('outfit.otui', { parent = rootWidget }) window:lock() m_outfit = creature:getOutfit() - + m_currentClothe = window:getChildById('head') window:getChildById('head').onCheckChange = onClotheCheckChange window:getChildById('primary').onCheckChange = onClotheCheckChange @@ -147,13 +145,10 @@ function Outfit.create(creature, outfitList) for i=0,18 do for j=0,6 do - local color = UICheckBox.create() - window:addChild(color) - + local color = createWidget('Color', window) local outfitColor = getOufitColor(j*19 + i) color:setId('color' .. j*19+i) color.colorId = j*19 + i - color:setStyle('Color') color:setBackgroundColor(outfitColor) color:setMarginTop(j * 3 + j * 14) color:setMarginLeft(10 + i * 3 + i * 14) diff --git a/modules/game_skills/skills.lua b/modules/game_skills/skills.lua index b3d70519..b2db37b1 100644 --- a/modules/game_skills/skills.lua +++ b/modules/game_skills/skills.lua @@ -41,7 +41,7 @@ end -- public functions function Skills.create() - skillWindow = UI.display('skills.otui', { parent = Game.gameRightPanel }) + skillWindow = displayUI('skills.otui', { parent = Game.gameRightPanel }) end function Skills.destroy() diff --git a/modules/game_textmessage/textmessage.lua b/modules/game_textmessage/textmessage.lua index f56b1156..26d786e6 100644 --- a/modules/game_textmessage/textmessage.lua +++ b/modules/game_textmessage/textmessage.lua @@ -1,37 +1,34 @@ TextMessage = {} -- require styles -importStyles 'textmessage.otui' +importStyle 'textmessage.otui' -- private variables local bottomLabelWidget, centerLabelWidget local messageTypes = { first = 12, - { type = 'MessageOrange', color = '#C87832', showOnConsole = true, showOnWindow = false }, - { type = 'MessageOrange', color = '#C87832', showOnConsole = true, showOnWindow = false }, - { type = 'MessageRed', color = '#C83200', showOnConsole = true, showOnWindow = true, windowLocation = 'CenterLabel' }, - { type = 'MessageWhite', color = '#FFFFFF', showOnConsole = true, showOnWindow = true, windowLocation = 'CenterLabel' }, - { type = 'MessageWhite', color = '#FFFFFF', showOnConsole = true, showOnWindow = true, windowLocation = 'BottomLabel' }, - { type = 'MessageWhite', color = '#FFFFFF', showOnConsole = true, showOnWindow = true, windowLocation = 'BottomLabel' }, - { type = 'MessageGreen', color = '#3FBE32', showOnConsole = true, showOnWindow = true, windowLocation = 'CenterLabel' }, - { type = 'MessageWhite', color = '#FFFFFF', showOnConsole = false, showOnWindow = true, windowLocation = 'BottomLabel' }, - { type = 'MessageBlue', color = '#3264C8', showOnConsole = true, showOnWindow = false }, - { type = 'MessageRed', color = '#C83200', showOnConsole = true, showOnWindow = false } + { msgtype = 'MessageOrange', color = '#C87832', showOnConsole = true, showOnWindow = false }, + { msgtype = 'MessageOrange', color = '#C87832', showOnConsole = true, showOnWindow = false }, + { msgtype = 'MessageRed', color = '#C83200', showOnConsole = true, showOnWindow = true, windowLocation = 'CenterLabel' }, + { msgtype = 'MessageWhite', color = '#FFFFFF', showOnConsole = true, showOnWindow = true, windowLocation = 'CenterLabel' }, + { msgtype = 'MessageWhite', color = '#FFFFFF', showOnConsole = true, showOnWindow = true, windowLocation = 'BottomLabel' }, + { msgtype = 'MessageWhite', color = '#FFFFFF', showOnConsole = true, showOnWindow = true, windowLocation = 'BottomLabel' }, + { msgtype = 'MessageGreen', color = '#3FBE32', showOnConsole = true, showOnWindow = true, windowLocation = 'CenterLabel' }, + { msgtype = 'MessageWhite', color = '#FFFFFF', showOnConsole = false, showOnWindow = true, windowLocation = 'BottomLabel' }, + { msgtype = 'MessageBlue', color = '#3264C8', showOnConsole = true, showOnWindow = false }, + { msgtype = 'MessageRed', color = '#C83200', showOnConsole = true, showOnWindow = false } } local hideEvent -- public functions function TextMessage.create() - bottomLabelWidget = UILabel.create() - Game.gameMapPanel:addChild(bottomLabelWidget) - - centerLabelWidget = UILabel.create() - Game.gameMapPanel:addChild(centerLabelWidget) + bottomLabelWidget = createWidget('UILabel', Game.gameMapPanel) + centerLabelWidget = createWidget('UILabel', Game.gameMapPanel) end -- hooked events -function TextMessage.onTextMessage(type, message) - local messageType = messageTypes[type - messageTypes.first] +function TextMessage.onTextMessage(msgtype, message) + local messageType = messageTypes[msgtype - messageTypes.first] if messageType.showOnConsole then -- TODO diff --git a/modules/game_viplist/viplist.lua b/modules/game_viplist/viplist.lua index ed68da0a..8f346bd0 100644 --- a/modules/game_viplist/viplist.lua +++ b/modules/game_viplist/viplist.lua @@ -5,7 +5,7 @@ local vipWindow = nil -- public functions function VipList.create() - vipWindow = UI.display('viplist.otui', { parent = Game.gameRightPanel }) + vipWindow = displayUI('viplist.otui', { parent = Game.gameRightPanel }) end function VipList.destroy() @@ -17,11 +17,9 @@ end function Game.onAddVip(id, name, online) local vipList = vipWindow:getChildById('vipList') - local label = UILabel.create() - vipList:addChild(label) + local label = createWidget('VipListLabel', vipList) label:setId('vip' .. id) label:setText(name) - label:setStyle('VipListLabel') if online then label:setForegroundColor('#00ff00') diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index 3a8a5f37..53909f22 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -241,7 +241,13 @@ void Application::registerLuaFunctions() g_lua.bindClassStaticFunction("fireOldMessages", std::bind(&Logger::fireOldMessages, &g_logger)); g_lua.bindClassStaticFunction("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 @@ -260,8 +266,5 @@ void Application::registerLuaFunctions() // global functions 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("loadUI", std::bind(&UIManager::loadUI, &g_ui, _1, _2)); - g_lua.bindGlobalFunction("getRootWidget", std::bind(&UIManager::getRootWidget, &g_ui)); } diff --git a/src/framework/luascript/luavaluecasts.cpp b/src/framework/luascript/luavaluecasts.cpp index fb0f2e48..c969b310 100644 --- a/src/framework/luascript/luavaluecasts.cpp +++ b/src/framework/luascript/luavaluecasts.cpp @@ -226,7 +226,7 @@ bool luavalue_cast(int index, Size& size) } // otml nodes -void push_luavalue(const OTMLNodePtr& node) +void push_otml_subnode_luavalue(const OTMLNodePtr& node) { if(node->hasValue()) { g_lua.pushString(node->value()); @@ -251,6 +251,20 @@ void push_luavalue(const OTMLNodePtr& node) 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) { node = OTMLNode::create(); diff --git a/src/framework/ui/uimanager.cpp b/src/framework/ui/uimanager.cpp index 79fd1265..b5f6dc35 100644 --- a/src/framework/ui/uimanager.cpp +++ b/src/framework/ui/uimanager.cpp @@ -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 { OTMLDocumentPtr doc = OTMLDocument::parse(file); @@ -135,13 +135,21 @@ OTMLNodePtr UIManager::getStyle(const std::string& styleName) // styles starting with UI are automatically defined if(boost::starts_with(styleName, "UI")) { OTMLNodePtr node = OTMLNode::create(); - node->writeAt("__widgetType", styleName); + node->writeAt("__class", styleName); return node; } 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) { try { @@ -176,7 +184,7 @@ UIWidgetPtr UIManager::loadWidgetFromOTML(const OTMLNodePtr& widgetNode, const U OTMLNodePtr styleNode = originalStyleNode->clone(); styleNode->merge(widgetNode); - std::string widgetType = styleNode->valueAt("__widgetType"); + std::string widgetType = styleNode->valueAt("__class"); // call widget creation from lua UIWidgetPtr widget = g_lua.callGlobalField(widgetType, "create"); diff --git a/src/framework/ui/uimanager.h b/src/framework/ui/uimanager.h index 3051e98e..5d351a4b 100644 --- a/src/framework/ui/uimanager.h +++ b/src/framework/ui/uimanager.h @@ -37,9 +37,10 @@ public: void resize(const Size& size); void inputEvent(const InputEvent& event); - bool importStyles(const std::string& file); + bool importStyle(const std::string& file); void importStyleFromOTML(const OTMLNodePtr& styleNode); OTMLNodePtr getStyle(const std::string& styleName); + std::string getStyleClass(const std::string& styleName); UIWidgetPtr loadUI(const std::string& file, const UIWidgetPtr& parent = nullptr); UIWidgetPtr loadWidgetFromOTML(const OTMLNodePtr& widgetNode, const UIWidgetPtr& parent);