From 9312d20a0f988f94c09882476b8cd0edaf2aea36 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Thu, 28 Feb 2013 20:10:36 -0300 Subject: [PATCH] More terminal improvements --- modules/client_terminal/commands.lua | 20 ++-- modules/client_terminal/terminal.lua | 143 ++++++++++++++++---------- modules/client_terminal/terminal.otui | 19 +++- src/framework/ui/uitextedit.cpp | 4 +- 4 files changed, 122 insertions(+), 64 deletions(-) diff --git a/modules/client_terminal/commands.lua b/modules/client_terminal/commands.lua index aae09eb1..1f2506a9 100644 --- a/modules/client_terminal/commands.lua +++ b/modules/client_terminal/commands.lua @@ -3,9 +3,8 @@ local function pcolored(text, color) modules.client_terminal.addLine(text, color) end -function draw_debug_boxes(enable) - if enable == nil then enable = true end - g_ui.setDebugBoxesDrawing(enable) +function draw_debug_boxes() + g_ui.setDebugBoxesDrawing(not g_ui.isDrawingDebugBoxes()) end function hide_map() @@ -24,23 +23,28 @@ function auto_reload_module(name) reloadEvent() end -local function pingBack(ping) print(g_game.getWorldName() .. ' => ' .. ping .. ' ms') end local pinging = false +local function pingBack(ping) + if ping < 300 then color = 'green' + elseif ping < 600 then color = 'yellow' + else color = 'red' end + pcolored(g_game.getWorldName() .. ' => ' .. ping .. ' ms', color) +end function ping() if pinging then - pdebug('Ping stopped.') + pcolored('Ping stopped.') g_game.setPingDelay(1000) disconnect(g_game, 'onPingBack', pingBack) else if not (g_game.getFeature(GameClientPing) or g_game.getFeature(GameExtendedClientPing)) then - perror('this server does not support ping') + pcolored('this server does not support ping', 'red') return elseif not g_game.isOnline() then - perror('ping command is only allowed when online') + pcolored('ping command is only allowed when online', 'red') return end - pdebug('Starting ping...') + pcolored('Starting ping...') g_game.setPingDelay(0) connect(g_game, 'onPingBack', pingBack) end diff --git a/modules/client_terminal/terminal.lua b/modules/client_terminal/terminal.lua index db9b3ed5..2ee2cd24 100644 --- a/modules/client_terminal/terminal.lua +++ b/modules/client_terminal/terminal.lua @@ -20,6 +20,10 @@ local commandTextEdit local terminalBuffer local commandHistory = { } local currentHistoryIndex = 0 +local poped = false +local oldPos +local oldSize +local firstShown = false -- private functions local function navigateCommand(step) @@ -113,22 +117,7 @@ function init() terminalWindow = g_ui.displayUI('terminal') terminalWindow:setVisible(false) - local poped = false - terminalWindow.onDoubleClick = function(self) - if poped then - self:fill('parent') - self:getChildById('bottomResizeBorder'):disable() - self:getChildById('rightResizeBorder'):disable() - poped = false - else - self:breakAnchors() - self:resize(g_window.getWidth()/2, g_window.getHeight()/2) - self:move(g_window.getWidth()/2, g_window.getHeight()/2) - self:getChildById('bottomResizeBorder'):enable() - self:getChildById('rightResizeBorder'):enable() - poped = true - end - end + terminalWindow.onDoubleClick = popWindow terminalButton = modules.client_topmenu.addLeftButton('terminalButton', tr('Terminal') .. ' (Ctrl + T)', '/images/topbuttons/terminal', toggle) g_keyboard.bindKeyDown('Ctrl+T', toggle) @@ -150,6 +139,7 @@ function init() terminalBuffer = terminalWindow:recursiveGetChildById('terminalBuffer') terminalSelectText = terminalWindow:recursiveGetChildById('terminalSelectText') + terminalSelectText.onDoubleClick = popWindow g_logger.setOnLog(onLog) g_logger.fireOldMessages() @@ -157,6 +147,18 @@ end function terminate() g_settings.setList('terminal-history', commandHistory) + + if poped then + oldPos = terminalWindow:getPosition() + oldSize = terminalWindow:getSize() + end + local settings = { + size = oldSize, + pos = oldPos, + poped = poped + } + g_settings.setNode('terminal-window', settings) + g_keyboard.unbindKeyDown('Ctrl+T') g_logger.setOnLog(nil) terminalWindow:destroy() @@ -168,10 +170,50 @@ function hideButton() terminalButton:hide() end +function popWindow() + if poped then + oldPos = terminalWindow:getPosition() + oldSize = terminalWindow:getSize() + terminalWindow:fill('parent') + terminalWindow:setOn(false) + terminalWindow:getChildById('bottomResizeBorder'):disable() + terminalWindow:getChildById('rightResizeBorder'):disable() + terminalWindow:getChildById('titleBar'):hide() + terminalWindow:getChildById('terminalScroll'):setMarginTop(0) + terminalWindow:getChildById('terminalScroll'):setMarginBottom(0) + terminalWindow:getChildById('terminalScroll'):setMarginRight(0) + poped = false + else + terminalWindow:breakAnchors() + terminalWindow:setOn(true) + local size = oldSize or { width = g_window.getWidth()/2, height = g_window.getHeight()/2 } + terminalWindow:setSize(size) + local pos = oldPos or { x = (g_window.getWidth() - terminalWindow:getWidth())/2, y = (g_window.getHeight() - terminalWindow:getHeight())/2 } + terminalWindow:setPosition(pos) + terminalWindow:getChildById('bottomResizeBorder'):enable() + terminalWindow:getChildById('rightResizeBorder'):enable() + terminalWindow:getChildById('titleBar'):show() + terminalWindow:getChildById('terminalScroll'):setMarginTop(18) + terminalWindow:getChildById('terminalScroll'):setMarginBottom(1) + terminalWindow:getChildById('terminalScroll'):setMarginRight(1) + terminalWindow:bindRectToParent() + poped = true + end +end + function toggle() if terminalWindow:isVisible() then hide() else + if not firstShow then + local settings = g_settings.getNode('terminal-window') + if settings then + if settings.size then oldSize = size end + if settings.pos then oldPos = settings.pos end + if settings.poped then popWindow() end + end + firstShown = true + end show() end end @@ -210,19 +252,17 @@ end function executeCommand(command) if command == nil or #command == 0 then return end - logLocked = true - g_logger.log(LogInfo, '> ' .. command) - logLocked = false + -- add command line + addLine("> " .. command, "#ffffff") - local func - local err + -- reset current history index + currentHistoryIndex = 0 - -- detect terminal commands - local command_name = command:match('^([%w_]+)[%s]*.*') - if command_name then - local args = string.split(command:match('^[%w]+[%s]*(.*)'), ' ') - if commandEnv[command_name] and type(commandEnv[command_name]) == 'function' then - func = function() modules.client_terminal.commandEnv[command_name](unpack(args)) end + -- add new command to history + if #commandHistory == 0 or commandHistory[#commandHistory] ~= command then + table.insert(commandHistory, command) + if #commandHistory > MaxHistory then + table.remove(commandHistory, 1) end end @@ -234,44 +274,39 @@ function executeCommand(command) realCommand = command end - -- reset current history index - currentHistoryIndex = 0 + local func, err = loadstring(realCommand, "@") - -- add new command to history - if #commandHistory == 0 or commandHistory[#commandHistory] ~= command then - table.insert(commandHistory, command) - if #commandHistory > MaxHistory then - table.remove(commandHistory, 1) + -- detect terminal commands + if not func then + local command_name = command:match('^([%w_]+)[%s]*.*') + if command_name then + local args = string.split(command:match('^[%w]+[%s]*(.*)'), ' ') + if commandEnv[command_name] and type(commandEnv[command_name]) == 'function' then + func = function() modules.client_terminal.commandEnv[command_name](unpack(args)) end + elseif command_name == command then + addLine('ERROR: command not found', 'red') + return + end end end - -- add command line - --addLine(">> " .. command, "#ffffff") - - -- load command buffer + -- check for syntax errors if not func then - func, err = loadstring(realCommand, "@") - - -- check for syntax errors - if not func then - g_logger.log(LogError, 'incorrect lua syntax: ' .. err:sub(5)) - return - end + addLine('ERROR: incorrect lua syntax: ' .. err:sub(5), 'red') + return end -- setup func env to commandEnv setfenv(func, commandEnv) -- execute the command - addEvent(function() - local ok, ret = pcall(func) - if ok then - -- if the command returned a value, print it - if ret then print(ret) end - else - g_logger.log(LogError, 'command failed: ' .. ret) - end - end) + local ok, ret = pcall(func) + if ok then + -- if the command returned a value, print it + if ret then print(ret) end + else + addLine('ERROR: command failed: ' .. ret, 'red') + end end function clear() diff --git a/modules/client_terminal/terminal.otui b/modules/client_terminal/terminal.otui index ec9c01ae..c9729e92 100644 --- a/modules/client_terminal/terminal.otui +++ b/modules/client_terminal/terminal.otui @@ -23,13 +23,30 @@ UIWindow opacity: 0.85 clipping: true anchors.fill: parent + border: 0 white + $on: + border: 1 black + + Label + id: titleBar + !text: tr('Terminal') + border: 1 black + color: white + anchors.top: parent.top + anchors.left: parent.left + anchors.right: parent.right + background-color: #ffffff11 + text-align: left + text-offset: 4 0 + height: 18 + visible: false ScrollablePanel id: terminalScrollArea focusable: false anchors.left: parent.left anchors.right: terminalScroll.left - anchors.top: parent.top + anchors.top: terminalScroll.top anchors.bottom: commandSymbolLabel.top vertical-scrollbar: terminalScroll inverted-scroll: true diff --git a/src/framework/ui/uitextedit.cpp b/src/framework/ui/uitextedit.cpp index 91b4f09f..f0adefe5 100644 --- a/src/framework/ui/uitextedit.cpp +++ b/src/framework/ui/uitextedit.cpp @@ -814,11 +814,13 @@ bool UITextEdit::onMouseMove(const Point& mousePos, const Point& mouseMoved) bool UITextEdit::onDoubleClick(const Point& mousePos) { + if(UIWidget::onDoubleClick(mousePos)) + return true; if(m_selectable && m_text.length() > 0) { selectAll(); return true; } - return UIWidget::onDoubleClick(mousePos); + return false; } void UITextEdit::onTextAreaUpdate(const Point& offset, const Size& visibleSize, const Size& totalSize)