2011-08-20 22:30:41 +02:00
|
|
|
Console = { }
|
2011-08-21 03:01:46 +02:00
|
|
|
|
2011-08-20 22:30:41 +02:00
|
|
|
local console
|
|
|
|
local logLocked = false
|
2011-08-21 03:01:46 +02:00
|
|
|
local commandEnv = createEnvironment()
|
2011-08-21 23:49:31 +02:00
|
|
|
local maxLines = 80
|
|
|
|
local numLines = 0
|
2011-08-20 22:30:41 +02:00
|
|
|
|
|
|
|
function Console.onLog(level, message, time)
|
|
|
|
-- avoid logging while reporting logs (would cause a infinite loop)
|
|
|
|
if not logLocked then
|
|
|
|
logLocked = true
|
|
|
|
|
|
|
|
local color
|
|
|
|
if level == LogDebug then
|
|
|
|
color = '#5555ff'
|
|
|
|
elseif level == LogInfo then
|
2011-08-23 03:08:36 +02:00
|
|
|
color = '#5555ff'
|
2011-08-20 22:30:41 +02:00
|
|
|
elseif level == LogWarning then
|
|
|
|
color = '#ffff00'
|
|
|
|
else
|
|
|
|
color = '#ff0000'
|
|
|
|
end
|
|
|
|
|
2011-08-23 03:08:36 +02:00
|
|
|
if level ~= LogDebug then
|
|
|
|
Console.addLine(message, color)
|
|
|
|
end
|
2011-08-20 22:30:41 +02:00
|
|
|
|
|
|
|
logLocked = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Console.addLine(text, color)
|
|
|
|
-- create new label
|
2011-08-21 23:49:31 +02:00
|
|
|
|
2011-08-20 22:30:41 +02:00
|
|
|
local label = UILabel.create()
|
|
|
|
label:setStyle('ConsoleLabel')
|
|
|
|
label:setText(text)
|
|
|
|
label:setForegroundColor(color)
|
2011-08-23 03:08:36 +02:00
|
|
|
console:insertChild(3, label)
|
|
|
|
|
|
|
|
local lastLabel = console:getChildByIndex(4)
|
|
|
|
if lastLabel then
|
|
|
|
lastLabel:addAnchor(AnchorBottom, "prev", AnchorTop)
|
|
|
|
end
|
2011-08-21 23:49:31 +02:00
|
|
|
|
|
|
|
numLines = numLines + 1
|
|
|
|
if numLines > maxLines then
|
2011-08-23 03:08:36 +02:00
|
|
|
local firstLabel = console:getChildByIndex(-1)
|
|
|
|
firstLabel:destroy()
|
2011-08-21 23:49:31 +02:00
|
|
|
end
|
2011-08-20 22:30:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Console.create()
|
|
|
|
console = loadUI("/console/console.otui")
|
|
|
|
rootWidget:addChild(console)
|
|
|
|
console:hide()
|
|
|
|
|
|
|
|
Logger.setOnLog(Console.onLog)
|
|
|
|
Logger.fireOldMessages()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Console.destroy()
|
2011-08-20 23:37:27 +02:00
|
|
|
Logger.setOnLog(nil)
|
2011-08-20 22:30:41 +02:00
|
|
|
console:destroy()
|
|
|
|
console = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function Console.show()
|
|
|
|
console.parent:lockChild(console)
|
|
|
|
console.visible = true
|
|
|
|
end
|
|
|
|
|
|
|
|
function Console.hide()
|
|
|
|
console.parent:unlockChild(console)
|
|
|
|
console.visible = false
|
|
|
|
end
|
|
|
|
|
|
|
|
function Console.executeCommand(command)
|
|
|
|
Console.addLine(">> " .. command, "#ffffff")
|
2011-08-21 03:01:46 +02:00
|
|
|
local func, err = loadstring(command, "@")
|
2011-08-20 22:30:41 +02:00
|
|
|
if func then
|
2011-08-21 03:01:46 +02:00
|
|
|
setfenv(func, commandEnv)
|
2011-08-20 22:30:41 +02:00
|
|
|
local ok, ret = pcall(func)
|
|
|
|
if ok then
|
2011-08-21 03:01:46 +02:00
|
|
|
if ret then
|
|
|
|
print(ret)
|
|
|
|
end
|
2011-08-20 22:30:41 +02:00
|
|
|
else
|
|
|
|
Logger.log(LogError, 'command failed: ' .. ret)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Logger.log(LogError, 'incorrect lua syntax: ' .. err:sub(5))
|
|
|
|
end
|
|
|
|
end
|