expose plugin interface

This commit is contained in:
Markus Ast
2021-11-02 12:12:12 +01:00
parent 22dea08045
commit 6a3259c5f5

View File

@@ -39,6 +39,12 @@ local function loadScratchpad()
-- Crosshair resources -- Crosshair resources
local crosshairWindow = nil local crosshairWindow = nil
-- Plugin event handlers
local pluginListeners = {
onOpen = {},
onClose = {},
}
local function log(str) local function log(str)
if not str then if not str then
return return
@@ -310,8 +316,23 @@ local function loadScratchpad()
textarea:setSelectionNew(line, 0, line, 0) textarea:setSelectionNew(line, 0, line, 0)
end end
local function setVisible(b) local function fireOnOpen()
window:setVisible(b) local x, y, w, h = window:getBounds()
for i, listener in ipairs(pluginListeners.onOpen) do
local status, err = pcall(listener, x, y, w, h)
if not status then
net.log("[Scratchpad] Error calling onOpen listener: " .. tostring(err))
end
end
end
local function fireOnClose()
for i, listener in ipairs(pluginListeners.onClose) do
local status, err = pcall(listener)
if not status then
net.log("[Scratchpad] Error calling onClose listener: " .. tostring(err))
end
end
end end
local function handleResize(self) local function handleResize(self)
@@ -331,12 +352,16 @@ local function loadScratchpad()
config.windowSize = {w = w, h = h} config.windowSize = {w = w, h = h}
saveConfiguration() saveConfiguration()
fireOnOpen()
end end
local function handleMove(self) local function handleMove(self)
local x, y = self:getPosition() local x, y = self:getPosition()
config.windowPosition = {x = x, y = y} config.windowPosition = {x = x, y = y}
saveConfiguration() saveConfiguration()
fireOnOpen()
end end
local function updateCoordsMode() local function updateCoordsMode()
@@ -371,6 +396,8 @@ local function loadScratchpad()
updateCoordsMode() updateCoordsMode()
isHidden = false isHidden = false
fireOnOpen()
end end
local function hide() local function hide()
@@ -384,6 +411,8 @@ local function loadScratchpad()
crosshairWindow:setVisible(false) crosshairWindow:setVisible(false)
isHidden = true isHidden = true
fireOnClose()
end end
local function createCrosshairWindow() local function createCrosshairWindow()
@@ -524,6 +553,19 @@ local function loadScratchpad()
end end
DCS.setUserCallbacks(handler) DCS.setUserCallbacks(handler)
-- expose plugin interface
_G.scratchpad = {
onOpen = function(listener)
table.insert(pluginListeners.onOpen, listener)
end,
onClose = function(listener)
table.insert(pluginListeners.onClose, listener)
end,
getTextarea = function()
return textarea
end,
}
net.log("[Scratchpad] Loaded ...") net.log("[Scratchpad] Loaded ...")
end end