Fixes and new tuned terminal

This commit is contained in:
Eduardo Bart
2013-02-28 18:39:27 -03:00
parent 69e762385e
commit b804dd6959
18 changed files with 238 additions and 206 deletions

View File

@@ -1,84 +1,17 @@
function dumpWidgets(widget, level)
widget = widget or rootWidget
level = level or 0
for i=1,widget:getChildCount() do
local child = widget:getChildByIndex(i)
if child:isVisible() then
local name = child:getId()
if name:match('widget%d+') == nil then
print(string.rep(' ', level) .. name)
end
if child:getId() ~= 'terminalBuffer' then
dumpWidgets(child, level+1)
end
end
end
end
function drawDebugBoxes(enable)
function draw_debug_boxes(enable)
if enable == nil then enable = true end
g_ui.setDebugBoxesDrawing(enable)
end
function hideMap()
local map = rootWidget:recursiveGetChildById('gameMapPanel')
if map then map:hide() end
function hide_map()
modules.game_interface.getMapPanel():hide()
end
function showMap()
local map = rootWidget:recursiveGetChildById('gameMapPanel')
if map then map:show() end
function show_map()
modules.game_interface.getMapPanel():show()
end
function debugContainersItems()
function UIItem:onHoverChange(hovered)
if hovered then
local item = self:getItem()
if item then
local text = "id: " ..item:getId() ..
"\n stackable: " ..tostring(item:isStackable()) ..
"\n marketable: " ..tostring(item:isMarketable()) ..
"\n vocation: "..(item:getMarketData() and item:getMarketData().restrictVocation or 'none') ..
"\n cloth slot: " ..item:getClothSlot()
g_tooltip.display(text)
end
else
g_tooltip.hide()
end
end
end
function debugPosition(enable)
if enable == nil then enable = true end
local label = rootWidget:getChildById('debugPositionLabel')
if not label then
label = g_ui.createWidget('GameLabel', rootWidget)
label:setColor('pink')
label:setFont('terminus-14px-bold')
label:setId('debugPositionLabel')
label:setPosition({x= 10, y = 40 })
label:setPhantom(true)
label:setTextAutoResize(true)
end
if enable then
label.event = cycleEvent(function()
local player = g_game.getLocalPlayer()
if player then
local pos = g_game.getLocalPlayer():getPosition()
label:show()
label:setText('x: ' .. pos.x .. '\ny: ' .. pos.y .. '\nz: ' .. pos.z)
else
label:hide()
end
end, 100)
else
removeEvent(label.event)
label.event = nil
label:hide()
end
end
function autoReloadModule(name)
function auto_reload_module(name)
local function reloadEvent()
reloadModule(name)
scheduleEvent(reloadEvent, 1000)
@@ -86,14 +19,53 @@ function autoReloadModule(name)
reloadEvent()
end
function createDebugUIItem(id)
local uiitem = g_ui.createWidget('Item', rootWidget)
uiitem:setPosition({x= 200, y = 200 })
uiitem:setItemId(id)
uiitem:show()
local function pingBack(ping) print(g_game.getWorldName() .. ' => ' .. ping .. ' ms') end
local pinging = false
function ping()
if pinging then
pdebug('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')
return
elseif not g_game.isOnline() then
perror('ping command is only allowed when online')
return
end
pdebug('Starting ping...')
g_game.setPingDelay(0)
connect(g_game, 'onPingBack', pingBack)
end
pinging = not pinging
end
function debugPings()
g_game.setPingDelay(0)
connect(g_game, { onPingBack = function(ping) print(g_game.getWorldName() .. ' => ' .. ping .. ' ms') end })
function clear()
modules.client_terminal.clear()
end
function ls(path)
path = path or '/'
local files = g_resources.listDirectoryFiles(path)
for k,v in pairs(files) do
if g_resources.directoryExists(path .. v) then
modules.client_terminal.addLine(path .. v, 'blue')
else
pinfo(path .. v)
end
end
end
function about_version()
pinfo(g_app.getName() .. ' ' .. g_app.getVersion() .. '\n' ..
'Rev ' .. g_app.getBuildRevision() .. ' ('.. g_app.getBuildCommit() .. ')\n' ..
'Built on ' .. g_app.getBuildDate())
end
function about_graphics()
pinfo('Vendor ' .. g_graphics.getVendor() )
pinfo('Renderer' .. g_graphics.getRenderer())
pinfo('Version' .. g_graphics.getVersion())
end

View File

@@ -1,17 +1,21 @@
-- configs
local LogColors = { [LogInfo] = 'white',
local LogColors = { [LogDebug] = 'pink',
[LogInfo] = 'white',
[LogWarning] = 'yellow',
[LogError] = 'red' }
local MaxLogLines = 80
local MaxLogLines = 512
local LabelHeight = 16
local MaxHistory = 1000
local oldenv = getfenv(0)
setfenv(0, _G)
commandEnv = runinsandbox('commands')
setfenv(0, oldenv)
-- private variables
local terminalWindow
local terminalButton
local logLocked = false
local commandEnv = {}
setmetatable(commandEnv, { __index = getfenv() } )
local commandTextEdit
local terminalBuffer
local commandHistory = { }
@@ -96,9 +100,6 @@ local function doCommand()
end
local function onLog(level, message, time)
-- debug messages are ignored
if level == LogDebug then return end
-- avoid logging while reporting logs (would cause a infinite loop)
if logLocked then return end
@@ -137,11 +138,19 @@ function init()
commandTextEdit = terminalWindow:getChildById('commandTextEdit')
g_keyboard.bindKeyPress('Up', function() navigateCommand(1) end, commandTextEdit)
g_keyboard.bindKeyPress('Down', function() navigateCommand(-1) end, commandTextEdit)
g_keyboard.bindKeyPress('Ctrl+C',
function()
if commandTextEdit:hasSelection() or not terminalSelectText:hasSelection() then return false end
g_window.setClipboardText(terminalSelectText:getSelection())
return true
end, commandTextEdit)
g_keyboard.bindKeyDown('Tab', completeCommand, commandTextEdit)
g_keyboard.bindKeyDown('Enter', doCommand, commandTextEdit)
g_keyboard.bindKeyDown('Escape', hide, terminalWindow)
terminalBuffer = terminalWindow:getChildById('terminalBuffer')
terminalBuffer = terminalWindow:recursiveGetChildById('terminalBuffer')
terminalSelectText = terminalWindow:recursiveGetChildById('terminalSelectText')
g_logger.setOnLog(onLog)
g_logger.fireOldMessages()
end
@@ -194,6 +203,8 @@ function addLine(text, color)
label:setId('terminalLabel' .. numLines)
label:setText(text)
label:setColor(color)
terminalSelectText:setText(terminalSelectText:getText() .. '\n' .. text)
end
function executeCommand(command)
@@ -203,6 +214,18 @@ function executeCommand(command)
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
@@ -226,23 +249,32 @@ function executeCommand(command)
--addLine(">> " .. command, "#ffffff")
-- load command buffer
local func, err = loadstring(realCommand, "@")
-- check for syntax errors
if not func then
g_logger.log(LogError, 'incorrect lua syntax: ' .. err:sub(5))
return
func, err = loadstring(realCommand, "@")
-- check for syntax errors
if not func then
g_logger.log(LogError, 'incorrect lua syntax: ' .. err:sub(5))
return
end
end
-- setup func env to commandEnv
setfenv(func, commandEnv)
-- execute the command
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
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)
end
function clear()
terminalBuffer:destroyChildren()
terminalSelectText:setText('')
end

View File

@@ -3,7 +3,7 @@ Module
description: Terminal for executing lua functions
author: edubart
website: www.otclient.info
scripts: [ terminal, commands ]
scripts: [ terminal ]
sandboxed: true
@onLoad: init()
@onUnload: terminate()

View File

@@ -1,7 +1,21 @@
TerminalLabel < UILabel
font: terminus-14px-bold
font: terminus-10px
text-wrap: true
text-auto-resize: true
phantom: true
TerminalSelectText < UITextEdit
font: terminus-10px
text-wrap: true
text-align: bottomLeft
editable: false
change-cursor-image: false
cursor-visible: false
selection-color: black
selection-background-color: white
color: alpha
focusable: false
auto-scroll: false
UIWindow
id: terminalWindow
@@ -10,35 +24,57 @@ UIWindow
clipping: true
anchors.fill: parent
Panel
id: terminalBuffer
layout:
type: verticalBox
fit-children: true
ScrollablePanel
id: terminalScrollArea
focusable: false
anchors.left: parent.left
anchors.right: parent.right
anchors.right: terminalScroll.left
anchors.top: parent.top
anchors.bottom: commandSymbolLabel.top
vertical-scrollbar: terminalScroll
inverted-scroll: true
margin-left: 2
Panel
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.right: parent.right
id: terminalBuffer
layout:
type: verticalBox
fit-children: true
focusable: false
TerminalSelectText
id: terminalSelectText
anchors.fill: terminalBuffer
VerticalScrollBar
id: terminalScroll
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.right: parent.right
step: 48
pixels-scroll: true
UILabel
id: commandSymbolLabel
size: 12 16
size: 12 12
fixed-size: true
anchors.bottom: parent.bottom
anchors.left: parent.left
margin-left: 2
font: terminus-14px-bold
font: terminus-10px
text: >
UITextEdit
id: commandTextEdit
height: 16
height: 12
anchors.bottom: parent.bottom
anchors.left: commandSymbolLabel.right
anchors.right: parent.right
margin-left: 5
font: terminus-14px-bold
margin-left: 1
font: terminus-10px
selection-color: black
selection-background-color: white