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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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()
@ -35,7 +35,7 @@ function Inventory.onInventoryItemMousePress(itemWidget, mousePos, mouseButton)
local item = itemWidget:getItem()
if not item then return end
local menu = UIPopupMenu.create()
local menu = createWidget('PopupMenu')
-- Look
local itemId = item:getId()
@ -48,7 +48,6 @@ function Inventory.onInventoryItemMousePress(itemWidget, mousePos, mouseButton)
else
menu:addOption('Use', function() print('use') end)
end
menu:display(mousePos)
end

View File

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

View File

@ -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,7 +129,7 @@ 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()
@ -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)

View File

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

View File

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

View File

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

View File

@ -241,7 +241,13 @@ void Application::registerLuaFunctions()
g_lua.bindClassStaticFunction<Logger>("fireOldMessages", std::bind(&Logger::fireOldMessages, &g_logger));
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
@ -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));
}

View File

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

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 {
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<UIWidgetPtr>(widgetType, "create");

View File

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