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

View File

@ -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,29 +252,8 @@ end
function executeCommand(command)
if command == nil or #command == 0 then return end
logLocked = true
g_logger.log(LogInfo, '> ' .. command)
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
-- add command line
addLine("> " .. command, "#ffffff")
-- reset current history index
currentHistoryIndex = 0
@ -245,33 +266,47 @@ function executeCommand(command)
end
end
-- add command line
--addLine(">> " .. command, "#ffffff")
-- 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
-- load command buffer
local func, err = loadstring(realCommand, "@")
-- detect terminal commands
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
if not func then
g_logger.log(LogError, 'incorrect lua syntax: ' .. err:sub(5))
addLine('ERROR: incorrect lua syntax: ' .. err:sub(5), 'red')
return
end
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)
addLine('ERROR: command failed: ' .. ret, 'red')
end
end)
end
function clear()

View File

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

View File

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