rework key input handling, add more script events for UI and implement console history

This commit is contained in:
Eduardo Bart
2011-08-28 18:32:24 -03:00
parent 7242dfc9ee
commit 8aadea2a96
12 changed files with 494 additions and 314 deletions

View File

@@ -5,6 +5,8 @@ local logLocked = false
local commandEnv = createEnvironment()
local maxLines = 80
local numLines = 0
local commandHistory = { }
local currentHistoryIndex = 0
function Console.onLog(level, message, time)
-- avoid logging while reporting logs (would cause a infinite loop)
@@ -47,9 +49,41 @@ function Console.addLine(text, color)
end
end
function Console.navigateCommand(step)
local numCommands = #commandHistory
if numCommands > 0 then
currentHistoryIndex = math.min(math.max(currentHistoryIndex + step, 0), numCommands)
local commandLineEdit = console:getChildById('commandLineEdit')
if currentHistoryIndex > 0 then
local command = commandHistory[numCommands - currentHistoryIndex + 1]
commandLineEdit:setText(command)
else
commandLineEdit:clearText()
end
end
end
function Console.create()
console = UI.loadAndDisplay("/console/console.otui")
console:hide()
console.onKeyPress = function(self, keyCode, keyChar, keyboardModifiers)
if keyboardModifiers == KeyboardNoModifier then
if keyCode == KeyReturn or keyCode == keyEnter then
local commandLineEdit = console:getChildById('commandLineEdit')
local command = commandLineEdit:getText()
Console.executeCommand(command)
commandLineEdit:clearText()
return true
elseif keyCode == KeyUp then
Console.navigateCommand(1)
return true
elseif keyCode == KeyDown then
Console.navigateCommand(-1)
return true
end
end
return false
end
Logger.setOnLog(Console.onLog)
Logger.fireOldMessages()
@@ -72,6 +106,8 @@ function Console.hide()
end
function Console.executeCommand(command)
currentHistoryIndex = 0
table.insert(commandHistory, command)
Console.addLine(">> " .. command, "#ffffff")
local func, err = loadstring(command, "@")
if func then

View File

@@ -23,14 +23,9 @@ RectPanel
text: >>
UILineEdit
id: commandBox
id: commandLineEdit
height: 16
anchors.bottom: parent.bottom
anchors.left: commandSymbolLabel.right
anchors.right: parent.right
font: terminus-14px-bold
onAction: |
function(self)
Console.executeCommand(self:getText())
self:clearText()
end