More terminal improvements

This commit is contained in:
Eduardo Bart 2013-02-28 20:10:36 -03:00
parent 425bfd998b
commit 9312d20a0f
4 changed files with 125 additions and 67 deletions

View File

@ -3,9 +3,8 @@ local function pcolored(text, color)
modules.client_terminal.addLine(text, color) modules.client_terminal.addLine(text, color)
end end
function draw_debug_boxes(enable) function draw_debug_boxes()
if enable == nil then enable = true end g_ui.setDebugBoxesDrawing(not g_ui.isDrawingDebugBoxes())
g_ui.setDebugBoxesDrawing(enable)
end end
function hide_map() function hide_map()
@ -24,23 +23,28 @@ function auto_reload_module(name)
reloadEvent() reloadEvent()
end end
local function pingBack(ping) print(g_game.getWorldName() .. ' => ' .. ping .. ' ms') end
local pinging = false 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() function ping()
if pinging then if pinging then
pdebug('Ping stopped.') pcolored('Ping stopped.')
g_game.setPingDelay(1000) g_game.setPingDelay(1000)
disconnect(g_game, 'onPingBack', pingBack) disconnect(g_game, 'onPingBack', pingBack)
else else
if not (g_game.getFeature(GameClientPing) or g_game.getFeature(GameExtendedClientPing)) then 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 return
elseif not g_game.isOnline() then elseif not g_game.isOnline() then
perror('ping command is only allowed when online') pcolored('ping command is only allowed when online', 'red')
return return
end end
pdebug('Starting ping...') pcolored('Starting ping...')
g_game.setPingDelay(0) g_game.setPingDelay(0)
connect(g_game, 'onPingBack', pingBack) connect(g_game, 'onPingBack', pingBack)
end end

View File

@ -20,6 +20,10 @@ local commandTextEdit
local terminalBuffer local terminalBuffer
local commandHistory = { } local commandHistory = { }
local currentHistoryIndex = 0 local currentHistoryIndex = 0
local poped = false
local oldPos
local oldSize
local firstShown = false
-- private functions -- private functions
local function navigateCommand(step) local function navigateCommand(step)
@ -113,22 +117,7 @@ function init()
terminalWindow = g_ui.displayUI('terminal') terminalWindow = g_ui.displayUI('terminal')
terminalWindow:setVisible(false) terminalWindow:setVisible(false)
local poped = false terminalWindow.onDoubleClick = popWindow
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
terminalButton = modules.client_topmenu.addLeftButton('terminalButton', tr('Terminal') .. ' (Ctrl + T)', '/images/topbuttons/terminal', toggle) terminalButton = modules.client_topmenu.addLeftButton('terminalButton', tr('Terminal') .. ' (Ctrl + T)', '/images/topbuttons/terminal', toggle)
g_keyboard.bindKeyDown('Ctrl+T', toggle) g_keyboard.bindKeyDown('Ctrl+T', toggle)
@ -150,6 +139,7 @@ function init()
terminalBuffer = terminalWindow:recursiveGetChildById('terminalBuffer') terminalBuffer = terminalWindow:recursiveGetChildById('terminalBuffer')
terminalSelectText = terminalWindow:recursiveGetChildById('terminalSelectText') terminalSelectText = terminalWindow:recursiveGetChildById('terminalSelectText')
terminalSelectText.onDoubleClick = popWindow
g_logger.setOnLog(onLog) g_logger.setOnLog(onLog)
g_logger.fireOldMessages() g_logger.fireOldMessages()
@ -157,6 +147,18 @@ end
function terminate() function terminate()
g_settings.setList('terminal-history', commandHistory) 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_keyboard.unbindKeyDown('Ctrl+T')
g_logger.setOnLog(nil) g_logger.setOnLog(nil)
terminalWindow:destroy() terminalWindow:destroy()
@ -168,10 +170,50 @@ function hideButton()
terminalButton:hide() terminalButton:hide()
end 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() function toggle()
if terminalWindow:isVisible() then if terminalWindow:isVisible() then
hide() hide()
else 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() show()
end end
end end
@ -210,29 +252,8 @@ end
function executeCommand(command) function executeCommand(command)
if command == nil or #command == 0 then return end if command == nil or #command == 0 then return end
logLocked = true -- add command line
g_logger.log(LogInfo, '> ' .. command) addLine("> " .. command, "#ffffff")
logLocked = false
local func
local err
-- 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
end
end
-- detect and convert commands with simple syntax
local realCommand
if string.sub(command, 1, 1) == '=' then
realCommand = 'print(' .. string.sub(command,2) .. ')'
else
realCommand = command
end
-- reset current history index -- reset current history index
currentHistoryIndex = 0 currentHistoryIndex = 0
@ -245,33 +266,47 @@ function executeCommand(command)
end end
end end
-- add command line -- detect and convert commands with simple syntax
--addLine(">> " .. command, "#ffffff") local realCommand
if string.sub(command, 1, 1) == '=' then
realCommand = 'print(' .. string.sub(command,2) .. ')'
else
realCommand = command
end
-- load command buffer local func, err = loadstring(realCommand, "@")
-- detect terminal commands
if not func then if not func then
func, err = loadstring(realCommand, "@") 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
-- check for syntax errors -- check for syntax errors
if not func then if not func then
g_logger.log(LogError, 'incorrect lua syntax: ' .. err:sub(5)) addLine('ERROR: incorrect lua syntax: ' .. err:sub(5), 'red')
return return
end end
end
-- setup func env to commandEnv -- setup func env to commandEnv
setfenv(func, commandEnv) setfenv(func, commandEnv)
-- execute the command -- execute the command
addEvent(function()
local ok, ret = pcall(func) local ok, ret = pcall(func)
if ok then if ok then
-- if the command returned a value, print it -- if the command returned a value, print it
if ret then print(ret) end if ret then print(ret) end
else else
g_logger.log(LogError, 'command failed: ' .. ret) addLine('ERROR: command failed: ' .. ret, 'red')
end end
end)
end end
function clear() function clear()

View File

@ -23,13 +23,30 @@ UIWindow
opacity: 0.85 opacity: 0.85
clipping: true clipping: true
anchors.fill: parent 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 ScrollablePanel
id: terminalScrollArea id: terminalScrollArea
focusable: false focusable: false
anchors.left: parent.left anchors.left: parent.left
anchors.right: terminalScroll.left anchors.right: terminalScroll.left
anchors.top: parent.top anchors.top: terminalScroll.top
anchors.bottom: commandSymbolLabel.top anchors.bottom: commandSymbolLabel.top
vertical-scrollbar: terminalScroll vertical-scrollbar: terminalScroll
inverted-scroll: true inverted-scroll: true

View File

@ -814,11 +814,13 @@ bool UITextEdit::onMouseMove(const Point& mousePos, const Point& mouseMoved)
bool UITextEdit::onDoubleClick(const Point& mousePos) bool UITextEdit::onDoubleClick(const Point& mousePos)
{ {
if(UIWidget::onDoubleClick(mousePos))
return true;
if(m_selectable && m_text.length() > 0) { if(m_selectable && m_text.length() > 0) {
selectAll(); selectAll();
return true; return true;
} }
return UIWidget::onDoubleClick(mousePos); return false;
} }
void UITextEdit::onTextAreaUpdate(const Point& offset, const Size& visibleSize, const Size& totalSize) void UITextEdit::onTextAreaUpdate(const Point& offset, const Size& visibleSize, const Size& totalSize)