From c4b2dd18d6908187c09de07c25f5f8637e042ad5 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Sat, 7 Jan 2012 15:36:58 -0200 Subject: [PATCH] restore terminal, rework console --- TODO | 2 + modules/addon_mapeffects/mapeffects.lua | 4 +- .../commands.lua | 0 .../terminal.lua} | 68 +++++++----- .../terminal.otmod} | 11 +- .../terminal.otui} | 6 +- modules/client_about/about.lua | 14 ++- modules/client_about/about.otmod | 4 + modules/client_about/about.otui | 2 +- modules/client_background/background.lua | 4 +- modules/client_background/background.otmod | 4 +- modules/client_entergame/entergame.lua | 27 ++++- modules/client_entergame/entergame.otmod | 4 +- modules/client_options/options.lua | 11 ++ modules/client_options/options.otmod | 4 +- modules/client_topmenu/topmenu.lua | 63 ++++++++++- modules/client_topmenu/topmenu.otmod | 4 +- modules/client_topmenu/topmenu.otui | 99 +++++++++--------- modules/core_styles/icons/terminal.png | Bin 0 -> 459 bytes modules/core_styles/styles/buttons.otui | 25 ----- src/framework/luascript/luaobject.h | 2 +- src/framework/otml/otmlnode.cpp | 19 ++++ src/framework/otml/otmlnode.h | 1 + src/framework/ui/uilineedit.cpp | 6 +- src/framework/ui/uiwidget.cpp | 12 ++- 25 files changed, 259 insertions(+), 137 deletions(-) rename modules/{addon_console => addon_terminal}/commands.lua (100%) rename modules/{addon_console/console.lua => addon_terminal/terminal.lua} (73%) rename modules/{addon_console/console.otmod => addon_terminal/terminal.otmod} (50%) rename modules/{addon_console/console.otui => addon_terminal/terminal.otui} (91%) create mode 100644 modules/core_styles/icons/terminal.png diff --git a/TODO b/TODO index 37959c09..20c7889f 100644 --- a/TODO +++ b/TODO @@ -44,6 +44,8 @@ [bart] padding [bart] review and make more error prone with more warnings [bart] a real working border and background property in otui +[bart] merge states declared in inherited styles +[bart] reapply anchor styles when adding new childs == Client modules [bart] modules managment interface diff --git a/modules/addon_mapeffects/mapeffects.lua b/modules/addon_mapeffects/mapeffects.lua index e6c9fced..b6afd12c 100644 --- a/modules/addon_mapeffects/mapeffects.lua +++ b/modules/addon_mapeffects/mapeffects.lua @@ -1,12 +1,14 @@ MapEffects = {} function MapEffects.init() +--[[ local box = createWidget('ComboBox') box:moveTo({x=100, y=8}) box:addOption('Normal') box:addOption('Bloom') box:addOption('TV') - --displayUI(box) + displayUI(box) + ]]-- end function MapEffects.terminate() diff --git a/modules/addon_console/commands.lua b/modules/addon_terminal/commands.lua similarity index 100% rename from modules/addon_console/commands.lua rename to modules/addon_terminal/commands.lua diff --git a/modules/addon_console/console.lua b/modules/addon_terminal/terminal.lua similarity index 73% rename from modules/addon_console/console.lua rename to modules/addon_terminal/terminal.lua index bac13f7f..8eed8ad6 100644 --- a/modules/addon_console/console.lua +++ b/modules/addon_terminal/terminal.lua @@ -1,4 +1,4 @@ -Console = { } +Terminal = { } -- configs local LogColors = { [LogInfo] = 'white', @@ -8,11 +8,12 @@ local MaxLogLines = 80 local LabelHeight = 16 -- private variables -local consoleWidget +local terminalWidget +local terminalButton local logLocked = false local commandEnv = newenv() local commandLineEdit -local consoleBuffer +local terminalBuffer local commandHistory = { } local currentHistoryIndex = 0 @@ -81,12 +82,12 @@ local function completeCommand() end end -local function onKeyPress(widget, keyCode, keyText, keyboardModifiers) +local function onCommandLineKeyPress(widget, keyCode, keyText, keyboardModifiers) if keyboardModifiers == KeyboardNoModifier then -- execute current command - if keyCode == KeyReturn or keyCode == keyEnter then + if keyCode == KeyReturn or keyCode == KeyEnter then local currentCommand = commandLineEdit:getText() - Console.executeCommand(currentCommand) + Terminal.executeCommand(currentCommand) commandLineEdit:clearText() return true -- navigate history up @@ -114,46 +115,64 @@ local function onLog(level, message, time) if logLocked then return end logLocked = true - Console.addLine(message, LogColors[level]) + Terminal.addLine(message, LogColors[level]) logLocked = false end -- public functions -function Console.init() - consoleWidget = displayUI('console.otui', { visible = false }) - connect(consoleWidget, { onKeyPress = onKeyPress }) +function Terminal.init() + terminalWidget = displayUI('terminal.otui') + terminalWidget:setVisible(false) - commandLineEdit = consoleWidget:getChildById('commandLineEdit') - consoleBuffer = consoleWidget:getChildById('consoleBuffer') + terminalButton = TopMenu.addButton('terminalButton', 'Terminal', '/core_styles/icons/terminal.png', Terminal.show) + + commandLineEdit = terminalWidget:getChildById('commandLineEdit') + connect(commandLineEdit, { onKeyPress = onCommandLineKeyPress }) + + terminalBuffer = terminalWidget:getChildById('terminalBuffer') Logger.setOnLog(onLog) Logger.fireOldMessages() end -function Console.terminate() +function Terminal.terminate() Logger.setOnLog(nil) - consoleWidget:destroy() + terminalButton:destroy() + terminalButton = nil commandLineEdit = nil - consoleWidget = nil + terminalBuffer = nil + terminalWidget:destroy() + terminalWidget = nil + commandEnv = nil +end + +function Terminal.show() + terminalWidget:show() + terminalWidget:lock() end -function Console.addLine(text, color) +function Terminal.hide() + terminalWidget:unlock() + terminalWidget:hide() +end + +function Terminal.addLine(text, color) -- create new line label - local numLines = consoleBuffer:getChildCount() + 1 - local label = createWidget('ConsoleLabel', consoleBuffer) - label:setId('consoleLabel' .. numLines) + local numLines = terminalBuffer:getChildCount() + 1 + local label = createWidget('TerminalLabel', terminalBuffer) + label:setId('terminalLabel' .. numLines) label:setText(text) label:setForegroundColor(color) -- delete old lines if needed if numLines > MaxLogLines then - consoleBuffer:getChildByIndex(1):destroy() + terminalBuffer:getChildByIndex(1):destroy() else - consoleBuffer:setHeight(consoleBuffer:getHeight() + LabelHeight) - consoleBuffer:updateParentLayout() + terminalBuffer:setHeight(terminalBuffer:getHeight() + LabelHeight) + terminalBuffer:updateParentLayout() end end -function Console.executeCommand(command) +function Terminal.executeCommand(command) -- detect and convert commands with simple syntax local realCommand if commandEnv[command] then @@ -173,7 +192,7 @@ function Console.executeCommand(command) table.insert(commandHistory, command) -- add command line - Console.addLine(">> " .. command, "#ffffff") + Terminal.addLine(">> " .. command, "#ffffff") -- load command buffer local func, err = loadstring(realCommand, "@") @@ -196,4 +215,3 @@ function Console.executeCommand(command) Logger.log(LogError, 'command failed: ' .. ret) end end - diff --git a/modules/addon_console/console.otmod b/modules/addon_terminal/terminal.otmod similarity index 50% rename from modules/addon_console/console.otmod rename to modules/addon_terminal/terminal.otmod index fdecb18b..a8a9a710 100644 --- a/modules/addon_console/console.otmod +++ b/modules/addon_terminal/terminal.otmod @@ -1,17 +1,16 @@ Module - name: console - description: Console for executing lua functions + name: terminal + description: Terminal for executing lua functions author: OTClient team website: https://github.com/edubart/otclient - // console can be loaded after core autoLoad: true autoLoadPriority: 1000 onLoad: | - require 'console' + require 'terminal' require 'commands' - Console.init() + Terminal.init() onUnload: | - Console.terminate() + Terminal.terminate() diff --git a/modules/addon_console/console.otui b/modules/addon_terminal/terminal.otui similarity index 91% rename from modules/addon_console/console.otui rename to modules/addon_terminal/terminal.otui index de1900d8..fc9a3793 100644 --- a/modules/addon_console/console.otui +++ b/modules/addon_terminal/terminal.otui @@ -1,15 +1,15 @@ -ConsoleLabel < UILabel +TerminalLabel < UILabel font: terminus-14px-bold height: 16 RectPanel - id: consolePanel + id: terminalPanel background-color: #000000 opacity: 216 anchors.fill: parent Panel - id: consoleBuffer + id: terminalBuffer layout: verticalBox focusable: false anchors.left: parent.left diff --git a/modules/client_about/about.lua b/modules/client_about/about.lua index 7bee0540..c1d33df3 100644 --- a/modules/client_about/about.lua +++ b/modules/client_about/about.lua @@ -1,16 +1,20 @@ About = {} -- private variables -local aboutWindow +local aboutButton -- public functions -function About.create() +function About.init() + aboutButton = TopMenu.addRightButton('aboutButton', 'About', '/core_styles/icons/about.png', About.display) +end + +function About.display() aboutWindow = displayUI('about.otui', { locked = true }) end -function About.destroy() - aboutWindow:destroy() - aboutWindow = nil +function About.terminate() + aboutButton:destroy() + aboutButton = nil end function About.openWebpage() diff --git a/modules/client_about/about.otmod b/modules/client_about/about.otmod index d6bc8b49..d3c1f63a 100644 --- a/modules/client_about/about.otmod +++ b/modules/client_about/about.otmod @@ -6,3 +6,7 @@ Module onLoad: | require 'about' + About.init() + + onUnload: + About.terminate() \ No newline at end of file diff --git a/modules/client_about/about.otui b/modules/client_about/about.otui index 9ca6f7a8..c25643c5 100644 --- a/modules/client_about/about.otui +++ b/modules/client_about/about.otui @@ -58,4 +58,4 @@ MainWindow anchors.top: parent.top margin-top: 191 margin-left: 188 - @onClick: About.destroy() + @onClick: self:getParent():destroy() diff --git a/modules/client_background/background.lua b/modules/client_background/background.lua index baaeb35e..6972898f 100644 --- a/modules/client_background/background.lua +++ b/modules/client_background/background.lua @@ -4,11 +4,11 @@ Background = { } local background -- public functions -function Background.create() +function Background.init() background = displayUI('background.otui') end -function Background.destroy() +function Background.terminate() background:destroy() background = nil end diff --git a/modules/client_background/background.otmod b/modules/client_background/background.otmod index e9de49b3..bb19c7ba 100644 --- a/modules/client_background/background.otmod +++ b/modules/client_background/background.otmod @@ -6,8 +6,8 @@ Module onLoad: | require 'background' - Background.create() + Background.init() onUnload: - Background.destroy() + Background.terminate() diff --git a/modules/client_entergame/entergame.lua b/modules/client_entergame/entergame.lua index e42ca1aa..58dba10e 100644 --- a/modules/client_entergame/entergame.lua +++ b/modules/client_entergame/entergame.lua @@ -5,6 +5,9 @@ local loadBox local enterGame local motdNumber local motdMessage +local motdButton +local enterGameButton + -- private functions local function clearAccountFields() @@ -25,7 +28,7 @@ end local function onMotd(protocol, motd) motdNumber = tonumber(motd:sub(0, motd:find("\n"))) motdMessage = motd:sub(motd:find("\n") + 1, #motd) - TopMenu.getButton('motdButton'):show() + motdButton:show() end local function onCharacterList(protocol, characters, premDays) @@ -50,7 +53,10 @@ local function onCharacterList(protocol, characters, premDays) end -- public functions -function EnterGame.create() +function EnterGame.init() + enterGameButton = TopMenu.addButton('enterGameButton', 'Login', '/core_styles/icons/login.png', EnterGame.openWindow) + motdButton = TopMenu.addButton('motdButton', 'Message of the day', '/core_styles/icons/motd.png', EnterGame.displayMotd) + motdButton:hide() enterGame = displayUI('entergame.otui') local account = Settings.get('account') @@ -72,9 +78,13 @@ function EnterGame.create() end end -function EnterGame.destroy() +function EnterGame.terminate() enterGame:destroy() enterGame = nil + enterGameButton:destroy() + enterGameButton = nil + motdButton:destroy() + motdButton = nil end function EnterGame.show() @@ -86,6 +96,14 @@ function EnterGame.hide() enterGame:hide() end +function EnterGame.openWindow() + if Game.isOnline() then + CharacterList.show() + elseif not CharacterList.isVisible() then + EnterGame.show() + end +end + function EnterGame.doLogin() EnterGame.account = enterGame:getChildById('accountNameLineEdit'):getText() EnterGame.password = enterGame:getChildById('accountPasswordLineEdit'):getText() @@ -111,5 +129,6 @@ function EnterGame.doLogin() end function EnterGame.displayMotd() - displayInfoBox("Message of the day", motdMessage) + displayInfoBox('Message of the day', motdMessage) end + diff --git a/modules/client_entergame/entergame.otmod b/modules/client_entergame/entergame.otmod index 8da4d8a2..081ae34d 100644 --- a/modules/client_entergame/entergame.otmod +++ b/modules/client_entergame/entergame.otmod @@ -7,9 +7,9 @@ Module onLoad: | require 'entergame' require 'characterlist' - EnterGame.create() + EnterGame.init() onUnload: - EnterGame.destroy() + EnterGame.terminate() diff --git a/modules/client_options/options.lua b/modules/client_options/options.lua index 02201109..eb5d7d2b 100644 --- a/modules/client_options/options.lua +++ b/modules/client_options/options.lua @@ -1,5 +1,16 @@ Options = {} +local optionsButton + +function Options.init() + optionsButton = TopMenu.addButton('settingsButton', 'Options', '/core_styles/icons/settings.png', Options.show) + Options.load() +end + +function Options.terminate() + TopMenu.removeButton(optionsButton) +end + function Options.load() local booleanOptions = { vsync = true, showfps = true, diff --git a/modules/client_options/options.otmod b/modules/client_options/options.otmod index 542df62d..1a38eb45 100644 --- a/modules/client_options/options.otmod +++ b/modules/client_options/options.otmod @@ -6,5 +6,7 @@ Module onLoad: | require 'options' - Options.load() + Options.init() + onUnload: + Options.terminate() \ No newline at end of file diff --git a/modules/client_topmenu/topmenu.lua b/modules/client_topmenu/topmenu.lua index 984e466e..32d93260 100644 --- a/modules/client_topmenu/topmenu.lua +++ b/modules/client_topmenu/topmenu.lua @@ -2,17 +2,74 @@ TopMenu = {} -- private variables local topMenu +local leftButtonsPanel +local rightButtonsPanel -- public functions -function TopMenu.create() +function TopMenu.init() topMenu = displayUI('topmenu.otui') + leftButtonsPanel = topMenu:getChildById('leftButtonsPanel') + rightButtonsPanel = topMenu:getChildById('rightButtonsPanel') + + TopMenu.addRightButton('logoutButton', 'Logout', '/core_styles/icons/logout.png', + function() + if Game.isOnline() then + Game.logout(false) + else + exit() + end + end + ) end -function TopMenu.destroy() +function TopMenu.terminate() + leftButtonsPanel = nil + rightButtonsPanel = nil topMenu:destroy() topMenu = nil end +function TopMenu.addButton(id, description, icon, callback, right) + local panel + local class + if right then + panel = rightButtonsPanel + class = 'TopRightButton' + else + panel = leftButtonsPanel + class = 'TopLeftButton' + end + + local button = createWidget(class, panel) + button:setId(id) + button:setTooltip(description) + button:setIcon(resolvepath(icon, 2)) + button.onClick = callback + return button +end + +function TopMenu.addLeftButton(id, description, icon, callback) + return TopMenu.addButton(id, description, icon, callback, false) +end + +function TopMenu.addRightButton(id, description, icon, callback) + return TopMenu.addButton(id, description, icon, callback, true) +end + +function TopMenu.removeButton(param) + local button + if type(param) == 'string' then + button = TopMenu.getButton(param) + else + button = param + end + button:destroy() +end + function TopMenu.getButton(id) - return topMenu:getChildById(id) + return topMenu:recursiveGetChildById(id) +end + +function TopMenu:getLogoutButton(id) + return TopMenu.getButton('logoutButton') end diff --git a/modules/client_topmenu/topmenu.otmod b/modules/client_topmenu/topmenu.otmod index 3a902a7f..c2ad99e6 100644 --- a/modules/client_topmenu/topmenu.otmod +++ b/modules/client_topmenu/topmenu.otmod @@ -6,7 +6,7 @@ Module onLoad: | require 'topmenu' - TopMenu.create() + TopMenu.init() onUnload: | - TopMenu.destroy() + TopMenu.terminate() diff --git a/modules/client_topmenu/topmenu.otui b/modules/client_topmenu/topmenu.otui index b2f023c8..07f7704a 100644 --- a/modules/client_topmenu/topmenu.otui +++ b/modules/client_topmenu/topmenu.otui @@ -1,67 +1,72 @@ -TopPanel - id: topMenu - anchors.top: parent.top - anchors.left: parent.left - anchors.right: parent.right - focusable: false +TopButton < UIButton + background-color: white + size: 26 26 + text-translate: 0 0 + border-image: + source: /core_styles/images/top_button.png + clip: 0 0 26 26 + border: 3 + + $hover: + border-image: + source: /core_styles/images/top_button.png + clip: 26 0 26 26 + border: 3 + + $pressed: + text-translate: 1 1 + border-image: + source: /core_styles/images/top_button.png + clip: 52 0 26 26 + border: 3 - TopButton - id: settingsButton + $disabled: + background-color: #ffffff66 + +TopLeftButton < TopButton + $first: anchors.top: parent.top anchors.left: parent.left margin-top: 4 margin-left: 6 - tooltip: Options - icon: /core_styles/icons/settings.png - @onClick: Options.show() - - TopButton - id: enterGameButton + $!first: anchors.top: prev.top anchors.left: prev.right margin-left: 6 - tooltip: Enter game with a character - icon: /core_styles/icons/login.png - @onClick: | - if Game.isOnline() then - CharacterList.show() - elseif not CharacterList.isVisible() then - EnterGame.show() - end - TopButton - id: motdButton - anchors.top: prev.top - anchors.left: prev.right - margin-left: 6 - tooltip: Message of the day - icon: /core_styles/icons/motd.png - visible: false - @onClick: EnterGame.displayMotd() - - TopButton +TopRightButton < TopButton + $first: anchors.top: parent.top anchors.right: parent.right margin-top: 4 margin-right: 6 - tooltip: Logout - icon: /core_styles/icons/logout.png - @onClick: | - if Game.isOnline() then - Game.logout(false) - else - exit() - end - TopButton - anchors.top: parent.top + $!first: + anchors.top: prev.top anchors.right: prev.left - margin-top: 4 margin-right: 6 - tooltip: About OTClient - icon: /core_styles/icons/about.png - @onClick: About.create() + +TopPanel + id: topMenu + anchors.top: parent.top + anchors.left: parent.left + anchors.right: parent.right + focusable: false + + Panel + id: leftButtonsPanel + anchors.top: parent.top + anchors.bottom: parent.bottom + anchors.left: parent.left + anchors.right: next.left + + Panel + id: rightButtonsPanel + anchors.top: parent.top + anchors.bottom: parent.bottom + anchors.right: parent.right + width: 60 FrameCounter id: frameCounter diff --git a/modules/core_styles/icons/terminal.png b/modules/core_styles/icons/terminal.png new file mode 100644 index 0000000000000000000000000000000000000000..768c2e9ece17c676f783f123cdc2c8a30d038e59 GIT binary patch literal 459 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|*pj^6T^Rm@ z;DWu&Cj&(|3p^r=85p>QL70(Y)*K0-AbW|YuPgf7$c2W-&PmwC#`O^GU9E zO%e`jUENYWvAX3)TG%qPxq{Kzjt0J6%*_GnCsh vf6JB{+|8SAb6!yJOzY0Ptx*!8e}$X%R~b$8(Ys-J2^5T;u6{1-oD!Mtag() == newChild->tag() && (node->isUnique() || newChild->isUnique())) { newChild->setUnique(true); + + if(node->hasChildren() && newChild->hasChildren()) { + OTMLNodePtr tmpNode = node->clone(); + tmpNode->merge(newChild); + newChild->copy(tmpNode); + } + replaceChild(node, newChild); // remove any other child with the same tag @@ -140,6 +147,18 @@ bool OTMLNode::replaceChild(const OTMLNodePtr& oldChild, const OTMLNodePtr& newC return false; } +void OTMLNode::copy(const OTMLNodePtr& node) +{ + setTag(node->tag()); + setValue(node->value()); + setUnique(node->isUnique()); + setNull(node->isNull()); + setSource(node->source()); + clear(); + for(const OTMLNodePtr& child : node->m_children) + addChild(child->clone()); +} + void OTMLNode::merge(const OTMLNodePtr& node) { for(const OTMLNodePtr& child : node->m_children) diff --git a/src/framework/otml/otmlnode.h b/src/framework/otml/otmlnode.h index 1520baba..1b1e6e74 100644 --- a/src/framework/otml/otmlnode.h +++ b/src/framework/otml/otmlnode.h @@ -63,6 +63,7 @@ public: void addChild(const OTMLNodePtr& newChild); bool removeChild(const OTMLNodePtr& oldChild); bool replaceChild(const OTMLNodePtr& oldChild, const OTMLNodePtr& newChild); + void copy(const OTMLNodePtr& node); void merge(const OTMLNodePtr& node); void clear(); diff --git a/src/framework/ui/uilineedit.cpp b/src/framework/ui/uilineedit.cpp index 0d889dab..7720b48b 100644 --- a/src/framework/ui/uilineedit.cpp +++ b/src/framework/ui/uilineedit.cpp @@ -411,6 +411,9 @@ void UILineEdit::onFocusChange(bool focused, Fw::FocusReason reason) bool UILineEdit::onKeyPress(uchar keyCode, std::string keyText, int keyboardModifiers) { + if(UIWidget::onKeyPress(keyCode, keyText, keyboardModifiers)) + return true; + if(keyCode == Fw::KeyDelete) // erase right character removeCharacter(true); else if(keyCode == Fw::KeyBackspace) // erase left character { @@ -433,8 +436,7 @@ bool UILineEdit::onKeyPress(uchar keyCode, std::string keyText, int keyboardModi } else if(!keyText.empty() && (keyboardModifiers == Fw::KeyboardNoModifier || keyboardModifiers == Fw::KeyboardShiftModifier)) appendText(keyText); else - return UIWidget::onKeyPress(keyCode, keyText, keyboardModifiers); - + return false; return true; } diff --git a/src/framework/ui/uiwidget.cpp b/src/framework/ui/uiwidget.cpp index 4003e9a2..9377a0b4 100644 --- a/src/framework/ui/uiwidget.cpp +++ b/src/framework/ui/uiwidget.cpp @@ -1116,8 +1116,12 @@ void UIWidget::onStyleApply(const std::string& styleName, const OTMLNodePtr& sty // anchors else if(boost::starts_with(node->tag(), "anchors.")) { UIWidgetPtr parent = getParent(); - if(!parent) - throw OTMLException(node, "cannot create anchor, there is no parent widget!"); + if(!parent) { + if(m_firstOnStyle) + throw OTMLException(node, "cannot create anchor, there is no parent widget!"); + else + continue; + } UIAnchorLayoutPtr anchorLayout = parent->getLayout()->asUIAnchorLayout(); if(!anchorLayout) @@ -1331,9 +1335,7 @@ void UIWidget::propagateOnMouseRelease(const Point& mousePos, Fw::MouseButton bu child->setPressed(false); } - // fire release events only when pressed - if(isPressed()) - onMouseRelease(mousePos, button); + onMouseRelease(mousePos, button); } bool UIWidget::propagateOnMouseMove(const Point& mousePos, const Point& mouseMoved)