updated docs, Util functions and fixed last issues

This commit is contained in:
2026-09-07 14:04:28 +02:00
parent 2a7febfa4d
commit 145eb385bc
6 changed files with 98 additions and 88 deletions
@@ -34,7 +34,7 @@ function AirGroup:New(groupName, groupType, config, logger, spawnManager)
self._config = config self._config = config
self._logger = logger self._logger = logger
self._spawnManager = spawnManager self._spawnManager = spawnManager
self._routeDrawing = nil self._routeDrawings = {}
local group = Group.getByName(self._groupName) local group = Group.getByName(self._groupName)
if group then if group then
@@ -538,14 +538,14 @@ function Stage:OnStageNumberChanged(number, stageLaneIdentifier)
---@return boolean ---@return boolean
local needsPreActivation = function() local needsPreActivation = function()
if self._stageLaneIdentifier == stageLaneIdentifier then if self._stageLaneIdentifier == stageLaneIdentifier then
if self.stageNumber - number < self._stageConfig.AmountPreactivateStage then if self.stageNumber <= number + self._stageConfig.AmountPreactivateStage then
return true return true
end end
elseif self._stageLaneIdentifier == nil then elseif self._stageLaneIdentifier == nil then
--main lane --main lane
--Check if this stage is a chapter stage --Check if this stage is a chapter stage
if self._stageRepository:getStageLane(self._stageLaneIdentifier):IsChapterStart(self.stageNumber) if self._stageRepository:getStageLane(self._stageLaneIdentifier):IsChapterStart(self.stageNumber)
and self.stageNumber - number < self._stageConfig.AmountPreactivateStage and self.stageNumber < number + self._stageConfig.AmountPreactivateStage
then then
return true return true
end end
@@ -568,7 +568,7 @@ function Stage:OnStageNumberChanged(number, stageLaneIdentifier)
---@return boolean ---@return boolean
local needsBlueActivation = function() local needsBlueActivation = function()
if self._stageLaneIdentifier == stageLaneIdentifier and self.stageNumber > number then if self._stageLaneIdentifier == stageLaneIdentifier and self.stageNumber < number then
return true return true
end end
return false return false
@@ -6,7 +6,7 @@ local drawingLogger = Logger.new("CustomDrawing")
---@class CustomDrawing ---@class CustomDrawing
---@field protected _id integer? ---@field protected _IDs Array<integer>?
---@field protected _name string? ---@field protected _name string?
---@field protected _drawingObject DrawingObject ---@field protected _drawingObject DrawingObject
local CustomDrawing = {} local CustomDrawing = {}
@@ -18,7 +18,7 @@ CustomDrawing.__index = CustomDrawing
function CustomDrawing.New(drawingObject) function CustomDrawing.New(drawingObject)
local self = setmetatable({}, CustomDrawing) local self = setmetatable({}, CustomDrawing)
self._drawingObject = drawingObject self._drawingObject = drawingObject
self._id = nil -- initiate ID as nil, when drawn it will be set to the ID returned by DrawingHelper.Draw self._IDs = {} -- initiate IDs as an empty array, when drawn it will be set to the IDs returned by DrawingHelper.Draw
self._name = drawingObject.name self._name = drawingObject.name
return self return self
end end
@@ -110,20 +110,24 @@ function CustomDrawing:GetName()
end end
function CustomDrawing:Draw() function CustomDrawing:Draw()
drawingLogger:debug("Drawing custom drawing with ID " .. tostring(self._id)) drawingLogger:debug("Drawing custom drawing with IDs " .. table.concat(self._IDs, ", "))
if self._id ~= nil then if self._IDs ~= nil then
DrawingHelper.Remove(self._id) for _, id in ipairs(self._IDs) do
DrawingHelper.Remove(id)
end
end end
self._id = DrawingHelper.Draw(self._drawingObject) self._IDs = DrawingHelper.Draw(self._drawingObject)
end end
function CustomDrawing:Remove() function CustomDrawing:Remove()
if self._id ~= nil then if self._IDs ~= nil then
drawingLogger:debug("Removing custom drawing with ID " .. tostring(self._id)) for _, id in ipairs(self._IDs) do
DrawingHelper.Remove(self._id) drawingLogger:debug("Removing custom drawing with ID " .. tostring(id))
self._id = nil DrawingHelper.Remove(id)
end
self._IDs = {}
end end
end end
@@ -1,57 +1,46 @@
local Util = require("classes.util.Util") local Util = require("classes.util.Util")
local DcsUtil = require("classes.util.DcsUtil")
local Logger = require("classes.util.Logger") local Logger = require("classes.util.Logger")
local GlobalConfig = require("classes.configuration.GlobalConfig") local GlobalConfig = require("classes.configuration.GlobalConfig")
---@type LogLevel ---@type LogLevel
local level = "INFO" local level = "INFO"
if GlobalConfig:isDebugEnabled() then if GlobalConfig.New():isDebugEnabled() then
level = "DEBUG" level = "DEBUG"
end end
local drawingLogger = Logger.new("DrawingHelper", level) local logger = Logger.new("DrawingHelper", level)
---@class DrawingHelper ---@class DrawingHelper
local DrawingHelper = {} local DrawingHelper = {}
DrawingHelper.__index = DrawingHelper DrawingHelper.__index = DrawingHelper
local customDrawingIdIncrementer = 4210
---@param object DrawingObject ---@param object DrawingObject
---@param logger Logger? ---@return Array<integer> id
---@return integer? id function DrawingHelper.Draw(object)
function DrawingHelper.Draw(object, logger)
if object == nil then if object == nil then
if logger then if logger then
logger:warn("DrawingHelper.Draw called with nil object") logger:warn("DrawingHelper.Draw called with nil object")
end end
return nil return {}
end
local id = DrawingHelper.GetAndAddId()
if logger then
logger:debug("Drawing object with ID " .. tostring(id) .. " and primitive type: " .. tostring(object.primitiveType))
end end
if(object.primitiveType == "Polygon") then if(object.primitiveType == "Polygon") then
DrawingHelper.DrawPolygon(object--[[@as Polygon]], id, logger) return DrawingHelper.DrawPolygon(object--[[@as Polygon]])
elseif(object.primitiveType == "Line") then elseif(object.primitiveType == "Line") then
DrawingHelper.DrawLine(object--[[@as Line]], id, logger) return DrawingHelper.DrawLine(object--[[@as Line]])
elseif(object.primitiveType == "TextBox") then elseif(object.primitiveType == "TextBox") then
DrawingHelper.DrawTextBox(object--[[@as TextBox]], id) return DrawingHelper.DrawTextBox(object--[[@as TextBox]])
else else
if logger then logger:warn("Unknown primitive type: " .. tostring(object.primitiveType))
logger:warn("Unknown primitive type: " .. tostring(object.primitiveType))
end
end end
return id return {}
end end
function DrawingHelper.GetAndAddId() function DrawingHelper.GetAndAddId()
customDrawingIdIncrementer = customDrawingIdIncrementer + 1 return DcsUtil.GetNextDrawID()
return customDrawingIdIncrementer
end end
---@private ---@private
@@ -62,8 +51,7 @@ end
---@param lineColor table ---@param lineColor table
---@param lineStyle LineType ---@param lineStyle LineType
---@param lineThickness number ---@param lineThickness number
---@param logger Logger? local function MarkupToAll(shapeID, drawID, points, fillColor, lineColor, lineStyle, lineThickness)
local function MarkupToAll(shapeID, drawID, points, fillColor, lineColor, lineStyle, lineThickness, logger)
if lineThickness == nil or lineThickness <= 0 then if lineThickness == nil or lineThickness <= 0 then
lineStyle = 0 lineStyle = 0
@@ -76,23 +64,19 @@ local function MarkupToAll(shapeID, drawID, points, fillColor, lineColor, lineS
end end
functionString = functionString .. "{0,1,0,1}, {0,1,0,1}, " .. lineStyle .. ")" functionString = functionString .. "{0,1,0,1}, {0,1,0,1}, " .. lineStyle .. ")"
if logger then logger:debug("Drawing complex drawing with ID " .. tostring(drawID) .. " and function string: " .. functionString)
logger:debug("Drawing complex drawing with ID " .. tostring(drawID) .. " and function string: " .. functionString)
end
---@diagnostic disable-next-line: deprecated ---@diagnostic disable-next-line: deprecated
local f, err = loadstring(functionString) local f, err = loadstring(functionString)
if f then if f then
f() f()
else else
env.error("Something failed when drawing complex drawing" .. err) logger:error("Something failed when drawing complex drawing" .. err)
end
if logger then
logger:debug("Drawing with fill color: " .. table.concat(fillColor, ",") .. " and line color: " .. table.concat(lineColor, ",") .. " and line style: " .. tostring(lineStyle) .. " and line thickness: " .. tostring(lineThickness))
end end
trigger.action.setMarkupColorFill(drawID, fillColor) if fillColor then
trigger.action.setMarkupColorFill(drawID, fillColor)
end
trigger.action.setMarkupColor(drawID, lineColor) trigger.action.setMarkupColor(drawID, lineColor)
trigger.action.setMarkupTypeLine(drawID, lineStyle) trigger.action.setMarkupTypeLine(drawID, lineStyle)
@@ -100,13 +84,14 @@ end
---@private ---@private
---@param object Polygon ---@param object Polygon
---@param id integer ---@return Array<integer>
---@param logger Logger? function DrawingHelper.DrawPolygon(object)
function DrawingHelper.DrawPolygon(object, id, logger)
if object == nil then if object == nil then
return return {}
end end
local id = DrawingHelper.GetAndAddId()
---@param circle Circle ---@param circle Circle
local function DrawCircle(circle) local function DrawCircle(circle)
local vec3 = { x = circle.mapX, y = 0, z = circle.mapY } local vec3 = { x = circle.mapX, y = 0, z = circle.mapY }
@@ -115,15 +100,15 @@ function DrawingHelper.DrawPolygon(object, id, logger)
local style = DrawingHelper.ToLineStyleInteger(circle.style) local style = DrawingHelper.ToLineStyleInteger(circle.style)
trigger.action.circleToAll(-1, id, vec3, circle.radius, colorString, fillColor, style, true) trigger.action.circleToAll(-1, id, vec3, circle.radius, colorString, fillColor, style, true)
end end
---@param oval Oval ---@param oval Oval
---@param logger Logger? ---@return integer?, Array<CustomDrawing>?
local function DrawOval(oval, logger) local function DrawOval(oval)
---@type Array<Vec3> ---@type Array<Vec3>
local points = {} local points = {}
local pointsNo = 30 local pointsNo = 30
local angleStep = (2 * math.pi) / pointsNo local angleStep = (2 * math.pi) / pointsNo
local fillColor = DrawingHelper.ColorToColorTable(oval.fillColorString) local fillColor = DrawingHelper.ColorToColorTable(oval.fillColorString)
local color = DrawingHelper.ColorToColorTable(oval.colorString) local color = DrawingHelper.ColorToColorTable(oval.colorString)
local lineStyle = DrawingHelper.ToLineStyleInteger(oval.style) local lineStyle = DrawingHelper.ToLineStyleInteger(oval.style)
@@ -134,12 +119,11 @@ function DrawingHelper.DrawPolygon(object, id, logger)
local y = oval.mapY + (oval.r2 * math.sin(angle)) local y = oval.mapY + (oval.r2 * math.sin(angle))
table.insert(points, { x = x, y = 0, z = y } ) table.insert(points, { x = x, y = 0, z = y } )
end end
MarkupToAll(7, id, points, fillColor, color, lineStyle, oval.thickness, logger) MarkupToAll(7, id, points, fillColor, color, lineStyle, oval.thickness)
end end
---@param free Free ---@param free Free
---@param logger Logger? local function DrawFree(free)
local function DrawFree(free, logger)
local fillColor = DrawingHelper.ColorToColorTable(free.fillColorString) local fillColor = DrawingHelper.ColorToColorTable(free.fillColorString)
local color = DrawingHelper.ColorToColorTable(free.colorString) local color = DrawingHelper.ColorToColorTable(free.colorString)
local lineStyle = DrawingHelper.ToLineStyleInteger(free.style) local lineStyle = DrawingHelper.ToLineStyleInteger(free.style)
@@ -163,7 +147,7 @@ function DrawingHelper.DrawPolygon(object, id, logger)
end end
end end
MarkupToAll(7, id, points, fillColor, color, lineStyle, free.thickness, logger) MarkupToAll(7, id, points, fillColor, color, lineStyle, free.thickness)
end end
---@param rect Rect ---@param rect Rect
@@ -175,19 +159,16 @@ function DrawingHelper.DrawPolygon(object, id, logger)
local pointA = { x = rect.mapX, y = 0, z = rect.mapY } local pointA = { x = rect.mapX, y = 0, z = rect.mapY }
local pointB = { x = rect.mapX + rect.width, y = 0, z = rect.mapY + rect.height } local pointB = { x = rect.mapX + rect.width, y = 0, z = rect.mapY + rect.height }
trigger.action.rectToAll(-1, id, pointA, pointB, color, fillColor, lineStyle, true) trigger.action.rectToAll(-1, id, pointA, pointB, color, fillColor, lineStyle, true)
end end
---@param arrow Arrow ---@param arrow Arrow
---@param logger Logger? local function DrawArrow(arrow)
local function DrawArrow(arrow, logger)
local fillColor = DrawingHelper.ColorToColorTable(arrow.fillColorString) local fillColor = DrawingHelper.ColorToColorTable(arrow.fillColorString)
local color = DrawingHelper.ColorToColorTable(arrow.colorString) local color = DrawingHelper.ColorToColorTable(arrow.colorString)
local lineStyle = DrawingHelper.ToLineStyleInteger(arrow.style) local lineStyle = DrawingHelper.ToLineStyleInteger(arrow.style)
if logger then logger:debug("Drawing arrow with start point: " .. tostring(arrow.mapX) .. ", " .. tostring(arrow.mapY) .. " and angle: " .. tostring(arrow.angle) .. " and length: " .. tostring(arrow.length))
logger:debug("Drawing arrow with start point: " .. tostring(arrow.mapX) .. ", " .. tostring(arrow.mapY) .. " and angle: " .. tostring(arrow.angle) .. " and length: " .. tostring(arrow.length))
end
local endPoint = { x = arrow.mapX, y = 0, z = arrow.mapY } local endPoint = { x = arrow.mapX, y = 0, z = arrow.mapY }
local rad = math.rad(arrow.angle or 0) local rad = math.rad(arrow.angle or 0)
@@ -203,44 +184,56 @@ function DrawingHelper.DrawPolygon(object, id, logger)
if object.polygonMode == "circle" then if object.polygonMode == "circle" then
DrawCircle(object--[[@as Circle]]) DrawCircle(object--[[@as Circle]])
elseif object.polygonMode == "oval" then elseif object.polygonMode == "oval" then
DrawOval(object--[[@as Oval]], logger) DrawOval(object--[[@as Oval]])
elseif object.polygonMode == "free" then elseif object.polygonMode == "free" then
DrawFree(object--[[@as Free]], logger) DrawFree(object--[[@as Free]])
elseif object.polygonMode == "rect" then elseif object.polygonMode == "rect" then
DrawRect(object--[[@as Rect]]) DrawRect(object--[[@as Rect]])
elseif object.polygonMode == "arrow" then elseif object.polygonMode == "arrow" then
DrawArrow(object--[[@as Arrow]], logger) DrawArrow(object--[[@as Arrow]])
end end
return {id}
end end
---@private ---@private
---@param object Line ---@param object Line
---@param id integer ---@return Array<integer>
---@param logger Logger? function DrawingHelper.DrawLine(object)
function DrawingHelper.DrawLine(object, id, logger)
---@type Array<Vec3> ---@type Array<Vec3>
local points = {} local points = {}
local ids = {}
for _, point in ipairs(object.points) do for _, point in ipairs(object.points) do
table.insert(points, { x = point.x, y = 0, z = point.y } ) table.insert(points, { x = object.mapX + point.x, y = 0, z = object.mapY + point.y } )
end end
local color = DrawingHelper.ColorToColorTable(object.colorString) local color = DrawingHelper.ColorToColorTable(object.colorString)
local lineStyle = DrawingHelper.ToLineStyleInteger(object.style) local lineStyle = DrawingHelper.ToLineStyleInteger(object.style)
MarkupToAll(1, id, points, color, color, lineStyle, object.thickness, logger)
for i = 1, #points - 1 do
local id = DrawingHelper.GetAndAddId()
trigger.action.lineToAll(-1, id, points[i], points[i + 1], color, lineStyle, true)
table.insert(ids, id)
end
return ids
end end
---@private ---@private
---@param object TextBox ---@param object TextBox
---@param id integer ---@return Array<integer>
function DrawingHelper.DrawTextBox(object, id) function DrawingHelper.DrawTextBox(object)
local id = DrawingHelper.GetAndAddId()
trigger.action.textToAll(-1, id, { x= object.mapX, y = 0, z = object.mapY }, trigger.action.textToAll(-1, id, { x= object.mapX, y = 0, z = object.mapY },
DrawingHelper.ColorToColorTable(object.colorString), DrawingHelper.ColorToColorTable(object.colorString),
DrawingHelper.ColorToColorTable(object.fillColorString), DrawingHelper.ColorToColorTable(object.fillColorString),
object.fontSize or 12, object.fontSize or 12,
true, true,
object.text or "") object.text or "")
return {id}
end end
function DrawingHelper.Remove(id) function DrawingHelper.Remove(id)
@@ -252,7 +245,7 @@ end
function DrawingHelper.ColorToColorTable(hexStr) function DrawingHelper.ColorToColorTable(hexStr)
if hexStr == nil then if hexStr == nil then
drawingLogger:warn("ColorToColorTable called with nil hexStr, returning default color {0, 0, 0, 0}") logger:warn("ColorToColorTable called with nil hexStr, returning default color {0, 0, 0, 0}")
return { 0, 0, 0, 0 } return { 0, 0, 0, 0 }
end end
@@ -270,7 +263,7 @@ end
---@return string ---@return string
function DrawingHelper.ColorTableToColorString(rgba) function DrawingHelper.ColorTableToColorString(rgba)
if rgba == nil or #rgba < 4 or rgba[1] == nil or rgba[2] == nil or rgba[3] == nil or rgba[4] == nil then if rgba == nil or #rgba < 4 or rgba[1] == nil or rgba[2] == nil or rgba[3] == nil or rgba[4] == nil then
drawingLogger:warn("ColorTableToColorString called with invalid rgba table, returning default color string '0x00000000'") logger:warn("ColorTableToColorString called with invalid rgba table, returning default color string '0x00000000'")
return "0x00000000" return "0x00000000"
end end
@@ -293,11 +286,11 @@ function DrawingHelper.ToLineStyleInteger(lineStyle)
return 2 return 2
elseif lineStyle == "dotted" then elseif lineStyle == "dotted" then
return 3 return 3
elseif lineStyle == "dot dash" then elseif lineStyle == "dotdash" then
return 4 return 4
elseif lineStyle == "long dash" then elseif lineStyle == "longdash" then
return 5 return 5
elseif lineStyle == "two dash" then elseif lineStyle == "twodash" then
return 6 return 6
else else
return 0 return 0
@@ -314,11 +307,11 @@ function DrawingHelper.ToLineStyleString(lineStyle)
elseif lineStyle == 3 then elseif lineStyle == 3 then
return "dotted" return "dotted"
elseif lineStyle == 4 then elseif lineStyle == 4 then
return "dot dash" return "dotdash"
elseif lineStyle == 5 then elseif lineStyle == 5 then
return "long dash" return "longdash"
elseif lineStyle == 6 then elseif lineStyle == 6 then
return "two dash" return "twodash"
else else
return "no line" return "no line"
end end
@@ -18,7 +18,7 @@ local RunwayStrikeMission = {}
---@field corners Array<Vec2> ---@field corners Array<Vec2>
---@field kilosHit number ---@field kilosHit number
---@field repairGroups Array<string> ---@field repairGroups Array<string>
---@field drawID number? ---@field drawID Array<number>
---@param runway Runway ---@param runway Runway
@@ -172,8 +172,10 @@ function RunwayStrikeMission:Draw()
runwaySection.drawID = DrawingHelper.Draw(drawObject) runwaySection.drawID = DrawingHelper.Draw(drawObject)
else else
DcsUtil.SetFillColor(runwaySection.drawID, fillColor) for _, drawID in pairs(runwaySection.drawID) do
DcsUtil.SetLineColor(runwaySection.drawID, lineColor) DcsUtil.SetFillColor(drawID, fillColor)
DcsUtil.SetLineColor(drawID, lineColor)
end
end end
end end
@@ -497,6 +499,7 @@ function RunwayStrikeMission:ToSections(runway, numSections)
corners = corners, corners = corners,
kilosHit = 0, kilosHit = 0,
repairGroups = {}, repairGroups = {},
drawID = {},
} }
table.insert(sections, section) table.insert(sections, section)
+10
View File
@@ -632,6 +632,12 @@ do -- INIT DCS_UTIL
---@field b number ---@field b number
---@field a number ---@field a number
local drawID = 4210
function DCS_UTIL.GetNextDrawID()
drawID = drawID + 1
return drawID
end
---@param groupID number ---@param groupID number
---@param text string ---@param text string
@@ -677,6 +683,10 @@ do -- INIT DCS_UTIL
---@param drawID number ---@param drawID number
---@param fillColor DrawColor ---@param fillColor DrawColor
function DCS_UTIL.SetFillColor(drawID, fillColor) function DCS_UTIL.SetFillColor(drawID, fillColor)
if fillColor == nil then
return
end
local lineColorMapped = { local lineColorMapped = {
fillColor.r or 0, fillColor.r or 0,
fillColor.g or 0, fillColor.g or 0,