use vertical layout for console (much faster)

master
Eduardo Bart 13 years ago
parent 66703e4b07
commit 028ae664be

@ -1,17 +1,18 @@
Console = createEnvironment() Console = { }
setfenv(1, Console)
-- public variables -- configs
LogColors = { [LogInfo] = 'white', local LogColors = { [LogInfo] = 'white',
[LogWarning] = 'yellow', [LogWarning] = 'yellow',
[LogError] = 'red' } [LogError] = 'red' }
MaxLogLines = 80 local MaxLogLines = 80
local LabelHeight = 16
-- private variables -- private variables
local consoleWidget local consoleWidget
local logLocked = false local logLocked = false
local commandEnv = createEnvironment() local commandEnv = createEnvironment()
local commandLineEdit local commandLineEdit
local consoleBuffer
local commandHistory = { } local commandHistory = { }
local currentHistoryIndex = 0 local currentHistoryIndex = 0
@ -34,7 +35,7 @@ local function onKeyPress(widget, keyCode, keyChar, keyboardModifiers)
-- execute current command -- execute current command
if keyCode == KeyReturn or keyCode == keyEnter then if keyCode == KeyReturn or keyCode == keyEnter then
local currentCommand = commandLineEdit:getText() local currentCommand = commandLineEdit:getText()
executeCommand(currentCommand) Console.executeCommand(currentCommand)
commandLineEdit:clearText() commandLineEdit:clearText()
return true return true
-- navigate history up -- navigate history up
@ -58,33 +59,34 @@ local function onLog(level, message, time)
if logLocked then return end if logLocked then return end
logLocked = true logLocked = true
addLine(message, LogColors[level]) Console.addLine(message, LogColors[level])
logLocked = false logLocked = false
end end
-- public functions -- public functions
function init() function Console.init()
consoleWidget = UI.loadAndDisplay("/console/console.otui") consoleWidget = UI.loadAndDisplay("/console/console.otui")
consoleWidget:hide() consoleWidget:hide()
consoleWidget.onKeyPress = onKeyPress consoleWidget.onKeyPress = onKeyPress
commandLineEdit = consoleWidget:getChildById('commandLineEdit') commandLineEdit = consoleWidget:getChildById('commandLineEdit')
consoleBuffer = consoleWidget:getChildById('consoleBuffer')
Logger.setOnLog(onLog) Logger.setOnLog(onLog)
Logger.fireOldMessages() Logger.fireOldMessages()
end end
function terminate() function Console.terminate()
Logger.setOnLog(nil) Logger.setOnLog(nil)
consoleWidget:destroy() consoleWidget:destroy()
commandLineEdit = nil commandLineEdit = nil
consoleWidget = nil consoleWidget = nil
end end
function addLine(text, color) function Console.addLine(text, color)
-- create new line label -- create new line label
local numLines = consoleWidget:getChildCount() - 2 local numLines = consoleBuffer:getChildCount() + 1
local label = UILabel.create() local label = UILabel.create()
consoleWidget:insertChild(-2, label) consoleBuffer:addChild(label)
label:setId('consoleLabel' .. numLines) label:setId('consoleLabel' .. numLines)
label:setStyle('ConsoleLabel') label:setStyle('ConsoleLabel')
label:setText(text) label:setText(text)
@ -92,11 +94,14 @@ function addLine(text, color)
-- delete old lines if needed -- delete old lines if needed
if numLines > MaxLogLines then if numLines > MaxLogLines then
consoleWidget:getChildByIndex(1):destroy() consoleBuffer:getChildByIndex(1):destroy()
else
consoleBuffer:setHeight(consoleBuffer:getHeight() + LabelHeight)
consoleBuffer:updateParentLayout()
end end
end end
function executeCommand(command) function Console.executeCommand(command)
-- reset current history index -- reset current history index
currentHistoryIndex = 0 currentHistoryIndex = 0
@ -104,7 +109,7 @@ function executeCommand(command)
table.insert(commandHistory, command) table.insert(commandHistory, command)
-- add command line -- add command line
addLine(">> " .. command, "#ffffff") Console.addLine(">> " .. command, "#ffffff")
-- load command buffer -- load command buffer
local func, err = loadstring(command, "@") local func, err = loadstring(command, "@")

@ -1,10 +1,6 @@
ConsoleLabel < UILabel ConsoleLabel < UILabel
font: terminus-14px-bold font: terminus-14px-bold
height: 16 height: 16
anchors.bottom: next.top
anchors.left: parent.left
anchors.right: parent.right
margin.left: 2
RectPanel RectPanel
id: consolePanel id: consolePanel
@ -12,6 +8,14 @@ RectPanel
opacity: 216 opacity: 216
anchors.fill: parent anchors.fill: parent
UIWidget
id: consoleBuffer
layout: verticalBox
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: commandSymbolLabel.top
margin.left: 2
UILabel UILabel
id: commandSymbolLabel id: commandSymbolLabel
size: 20 16 size: 20 16

Loading…
Cancel
Save