mirror of
https://github.com/edubart/otclient.git
synced 2025-10-14 19:44:54 +02:00
lua console and some changes
This commit is contained in:
74
modules/console/console.lua
Normal file
74
modules/console/console.lua
Normal file
@@ -0,0 +1,74 @@
|
||||
Console = { }
|
||||
local console
|
||||
local logLocked = false
|
||||
|
||||
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
|
||||
color = '#55ff55'
|
||||
elseif level == LogWarning then
|
||||
color = '#ffff00'
|
||||
else
|
||||
color = '#ff0000'
|
||||
end
|
||||
|
||||
Console.addLine(message, color)
|
||||
|
||||
logLocked = false
|
||||
end
|
||||
end
|
||||
|
||||
function Console.addLine(text, color)
|
||||
-- create new label
|
||||
local label = UILabel.create()
|
||||
label:setStyle('ConsoleLabel')
|
||||
label:setText(text)
|
||||
label:setForegroundColor(color)
|
||||
|
||||
console:insertChild(label, -1)
|
||||
end
|
||||
|
||||
function Console.create()
|
||||
console = loadUI("/console/console.otui")
|
||||
rootWidget:addChild(console)
|
||||
console:hide()
|
||||
|
||||
Logger.setOnLog(Console.onLog)
|
||||
Logger.fireOldMessages()
|
||||
end
|
||||
|
||||
function Console.destroy()
|
||||
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")
|
||||
local func, err = loadstring("return (" .. command .. ")", "@")
|
||||
if func then
|
||||
local ok, ret = pcall(func)
|
||||
if ok then
|
||||
Logger.log(LogDebug, "=> " .. tostring(ret))
|
||||
else
|
||||
Logger.log(LogError, 'command failed: ' .. ret)
|
||||
end
|
||||
else
|
||||
Logger.log(LogError, 'incorrect lua syntax: ' .. err:sub(5))
|
||||
end
|
||||
end
|
17
modules/console/console.otmod
Normal file
17
modules/console/console.otmod
Normal file
@@ -0,0 +1,17 @@
|
||||
Module
|
||||
name: console
|
||||
description: Console for executing lua functions
|
||||
author: OTClient team
|
||||
website: https://github.com/edubart/otclient
|
||||
version: 0.2
|
||||
autoLoad: true
|
||||
dependencies:
|
||||
- core
|
||||
|
||||
onLoad: |
|
||||
require 'console'
|
||||
Console.create()
|
||||
return true
|
||||
|
||||
onUnload: |
|
||||
Console.destroy()
|
35
modules/console/console.otui
Normal file
35
modules/console/console.otui
Normal file
@@ -0,0 +1,35 @@
|
||||
ConsoleLabel < UILabel
|
||||
font: terminus-14px-bold
|
||||
height: 16
|
||||
anchors.bottom: next.top
|
||||
anchors.left: parent.left
|
||||
anchors.right: parent.right
|
||||
margin.left: 2
|
||||
|
||||
RectPanel
|
||||
id: consolePanel
|
||||
background-color: #000000
|
||||
opacity: 216
|
||||
anchors.fill: parent
|
||||
|
||||
UILabel
|
||||
size: 18 20
|
||||
anchors.bottom: parent.bottom
|
||||
anchors.left: parent.left
|
||||
margin.left: 2
|
||||
font: terminus-14px-bold
|
||||
text: >>
|
||||
|
||||
UILineEdit
|
||||
id: commandBox
|
||||
height: 20
|
||||
anchors.bottom: parent.bottom
|
||||
anchors.left: prev.right
|
||||
anchors.right: parent.right
|
||||
margin.left: 2
|
||||
font: terminus-14px-bold
|
||||
onAction: |
|
||||
function(self)
|
||||
Console.executeCommand(self:getText())
|
||||
self:clearText()
|
||||
end
|
Reference in New Issue
Block a user