mirror of
https://github.com/edubart/otclient.git
synced 2025-11-30 07:26:49 +01:00
a lot of changes in modules
This commit is contained in:
55
modules/client_terminal/commands.lua
Normal file
55
modules/client_terminal/commands.lua
Normal file
@@ -0,0 +1,55 @@
|
||||
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)
|
||||
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
|
||||
end
|
||||
|
||||
function showMap()
|
||||
local map = rootWidget:recursiveGetChildById('gameMapPanel')
|
||||
if map then map:show() end
|
||||
end
|
||||
|
||||
function debugContainersItems()
|
||||
function UIItem:onHoverChange(hovered)
|
||||
if hovered then
|
||||
local item = self:getItem()
|
||||
if item then ToolTip.display(item:getId()) end
|
||||
else
|
||||
ToolTip.hide()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function quit()
|
||||
exit()
|
||||
end
|
||||
|
||||
function autoReloadModule(name)
|
||||
local function reloadEvent()
|
||||
reloadModule(name)
|
||||
scheduleEvent(reloadEvent, 1000)
|
||||
end
|
||||
reloadEvent()
|
||||
end
|
||||
|
||||
227
modules/client_terminal/terminal.lua
Normal file
227
modules/client_terminal/terminal.lua
Normal file
@@ -0,0 +1,227 @@
|
||||
Terminal = { }
|
||||
|
||||
-- configs
|
||||
local LogColors = { [LogInfo] = 'white',
|
||||
[LogWarning] = 'yellow',
|
||||
[LogError] = 'red' }
|
||||
local MaxLogLines = 80
|
||||
local LabelHeight = 16
|
||||
local MaxHistory = 1000
|
||||
|
||||
-- private variables
|
||||
local terminalWidget
|
||||
local terminalButton
|
||||
local logLocked = false
|
||||
local commandEnv = newenv()
|
||||
local commandLineEdit
|
||||
local terminalBuffer
|
||||
local commandHistory = { }
|
||||
local currentHistoryIndex = 0
|
||||
|
||||
-- private functions
|
||||
local function navigateCommand(step)
|
||||
local numCommands = #commandHistory
|
||||
if numCommands > 0 then
|
||||
currentHistoryIndex = math.min(math.max(currentHistoryIndex + step, 0), numCommands)
|
||||
if currentHistoryIndex > 0 then
|
||||
local command = commandHistory[numCommands - currentHistoryIndex + 1]
|
||||
commandLineEdit:setText(command)
|
||||
else
|
||||
commandLineEdit:clearText()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function completeCommand()
|
||||
local cursorPos = commandLineEdit:getCursorPos()
|
||||
if cursorPos == 0 then return end
|
||||
|
||||
local commandBegin = commandLineEdit:getText():sub(1, cursorPos)
|
||||
local possibleCommands = {}
|
||||
|
||||
-- create a list containing all globals
|
||||
local allVars = table.copy(_G)
|
||||
table.merge(allVars, commandEnv)
|
||||
|
||||
-- match commands
|
||||
for k,v in pairs(allVars) do
|
||||
if k:sub(1, cursorPos) == commandBegin then
|
||||
table.insert(possibleCommands, k)
|
||||
end
|
||||
end
|
||||
|
||||
-- complete command with one match
|
||||
if #possibleCommands == 1 then
|
||||
commandLineEdit:setText(possibleCommands[1])
|
||||
-- show command matches
|
||||
elseif #possibleCommands > 0 then
|
||||
print('>> ' .. commandBegin)
|
||||
|
||||
-- expand command
|
||||
local expandedComplete = commandBegin
|
||||
local done = false
|
||||
while not done do
|
||||
cursorPos = #commandBegin+1
|
||||
if #possibleCommands[1] < cursorPos then
|
||||
break
|
||||
end
|
||||
expandedComplete = commandBegin .. possibleCommands[1]:sub(cursorPos, cursorPos)
|
||||
for i,v in ipairs(possibleCommands) do
|
||||
if v:sub(1, #expandedComplete) ~= expandedComplete then
|
||||
done = true
|
||||
end
|
||||
end
|
||||
if not done then
|
||||
commandBegin = expandedComplete
|
||||
end
|
||||
end
|
||||
commandLineEdit:setText(commandBegin)
|
||||
|
||||
for i,v in ipairs(possibleCommands) do
|
||||
print(v)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function doCommand()
|
||||
local currentCommand = commandLineEdit:getText()
|
||||
Terminal.executeCommand(currentCommand)
|
||||
|
||||
if commandLineEdit then
|
||||
commandLineEdit:clearText()
|
||||
end
|
||||
return true
|
||||
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
|
||||
|
||||
logLocked = true
|
||||
Terminal.addLine(message, LogColors[level])
|
||||
logLocked = false
|
||||
end
|
||||
|
||||
-- public functions
|
||||
function Terminal.init()
|
||||
terminalWidget = displayUI('terminal.otui')
|
||||
terminalWidget:setVisible(false)
|
||||
|
||||
terminalButton = TopMenu.addButton('terminalButton', 'Terminal (Ctrl + T)', 'terminal.png', Terminal.toggle)
|
||||
Keyboard.bindKeyDown('Ctrl+T', Terminal.toggle)
|
||||
|
||||
commandHistory = Settings.getList('terminal-history')
|
||||
|
||||
commandLineEdit = terminalWidget:getChildById('commandLineEdit')
|
||||
Keyboard.bindKeyDown('Up', function() navigateCommand(1) end, commandLineEdit)
|
||||
Keyboard.bindKeyDown('Down', function() navigateCommand(-1) end, commandLineEdit)
|
||||
Keyboard.bindKeyDown('Tab', completeCommand, commandLineEdit)
|
||||
Keyboard.bindKeyDown('Enter', doCommand, commandLineEdit)
|
||||
|
||||
terminalBuffer = terminalWidget:getChildById('terminalBuffer')
|
||||
Logger.setOnLog(onLog)
|
||||
Logger.fireOldMessages()
|
||||
end
|
||||
|
||||
function Terminal.terminate()
|
||||
Settings.setList('terminal-history', commandHistory)
|
||||
Keyboard.unbindKeyDown('Ctrl+T')
|
||||
Logger.setOnLog(nil)
|
||||
terminalButton:destroy()
|
||||
terminalButton = nil
|
||||
commandLineEdit = nil
|
||||
terminalBuffer = nil
|
||||
terminalWidget:destroy()
|
||||
terminalWidget = nil
|
||||
commandEnv = nil
|
||||
end
|
||||
|
||||
function Terminal.toggle()
|
||||
if terminalWidget:isVisible() then
|
||||
Terminal.hide()
|
||||
else
|
||||
Terminal.show()
|
||||
end
|
||||
end
|
||||
|
||||
function Terminal.show()
|
||||
terminalWidget:show()
|
||||
terminalWidget:lock()
|
||||
end
|
||||
|
||||
function Terminal.hide()
|
||||
terminalWidget:unlock()
|
||||
terminalWidget:hide()
|
||||
end
|
||||
|
||||
function Terminal.addLine(text, color)
|
||||
-- create new line label
|
||||
local numLines = terminalBuffer:getChildCount() + 1
|
||||
local label = createWidget('TerminalLabel', terminalBuffer)
|
||||
label:setId('terminalLabel' .. numLines)
|
||||
label:setText(text)
|
||||
label:setColor(color)
|
||||
|
||||
-- delete old lines if needed
|
||||
if numLines > MaxLogLines then
|
||||
terminalBuffer:getChildByIndex(1):destroy()
|
||||
else
|
||||
terminalBuffer:setHeight(terminalBuffer:getHeight() + LabelHeight)
|
||||
end
|
||||
end
|
||||
|
||||
function Terminal.executeCommand(command)
|
||||
if command == nil or #command == 0 then return end
|
||||
|
||||
logLocked = true
|
||||
Logger.log(LogInfo, '>> ' .. command)
|
||||
logLocked = false
|
||||
|
||||
-- detect and convert commands with simple syntax
|
||||
local realCommand
|
||||
if commandEnv[command] then
|
||||
if type(commandEnv[command]) == "function" then
|
||||
realCommand = command .. '()'
|
||||
else
|
||||
realCommand = 'print(' .. command .. ')'
|
||||
end
|
||||
else
|
||||
realCommand = command
|
||||
end
|
||||
|
||||
-- reset current history index
|
||||
currentHistoryIndex = 0
|
||||
|
||||
-- add new command to history
|
||||
table.insert(commandHistory, command)
|
||||
if #commandHistory > MaxHistory then
|
||||
table.remove(commandHistory, 1)
|
||||
end
|
||||
|
||||
-- add command line
|
||||
Terminal.addLine(">> " .. command, "#ffffff")
|
||||
|
||||
-- load command buffer
|
||||
local func, err = loadstring(realCommand, "@")
|
||||
|
||||
-- check for syntax errors
|
||||
if not func then
|
||||
Logger.log(LogError, 'incorrect lua syntax: ' .. err:sub(5))
|
||||
return
|
||||
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
|
||||
Logger.log(LogError, 'command failed: ' .. ret)
|
||||
end
|
||||
end
|
||||
15
modules/client_terminal/terminal.otmod
Normal file
15
modules/client_terminal/terminal.otmod
Normal file
@@ -0,0 +1,15 @@
|
||||
Module
|
||||
name: client_terminal
|
||||
description: Terminal for executing lua functions
|
||||
author: OTClient team
|
||||
website: https://github.com/edubart/otclient
|
||||
autoload: true
|
||||
autoload-antecedence: 160
|
||||
|
||||
onLoad: |
|
||||
dofile 'terminal'
|
||||
dofile 'commands'
|
||||
Terminal.init()
|
||||
|
||||
onUnload: |
|
||||
Terminal.terminate()
|
||||
38
modules/client_terminal/terminal.otui
Normal file
38
modules/client_terminal/terminal.otui
Normal file
@@ -0,0 +1,38 @@
|
||||
TerminalLabel < UILabel
|
||||
font: terminus-14px-bold
|
||||
height: 16
|
||||
|
||||
Panel
|
||||
id: terminalPanel
|
||||
background-color: #000000
|
||||
opacity: 0.85
|
||||
anchors.fill: parent
|
||||
@onEscape: Terminal.hide()
|
||||
|
||||
Panel
|
||||
id: terminalBuffer
|
||||
layout: verticalBox
|
||||
focusable: false
|
||||
anchors.left: parent.left
|
||||
anchors.right: parent.right
|
||||
anchors.bottom: commandSymbolLabel.top
|
||||
margin-left: 2
|
||||
|
||||
UILabel
|
||||
id: commandSymbolLabel
|
||||
size: 20 16
|
||||
fixed-size: true
|
||||
anchors.bottom: parent.bottom
|
||||
anchors.left: parent.left
|
||||
margin-left: 2
|
||||
font: terminus-14px-bold
|
||||
text: >>
|
||||
|
||||
UILineEdit
|
||||
id: commandLineEdit
|
||||
height: 16
|
||||
anchors.bottom: parent.bottom
|
||||
anchors.left: commandSymbolLabel.right
|
||||
anchors.right: parent.right
|
||||
margin-left: 5
|
||||
font: terminus-14px-bold
|
||||
BIN
modules/client_terminal/terminal.png
Normal file
BIN
modules/client_terminal/terminal.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 459 B |
Reference in New Issue
Block a user