Skip to content

Commit

Permalink
refactor: the big field polygon refactoring
Browse files Browse the repository at this point in the history
Callbacks to frame on field detection finished
  • Loading branch information
Peter Vaiko committed Jan 24, 2025
1 parent 24cb049 commit d630262
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
4 changes: 4 additions & 0 deletions config/MasterTranslations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
<Text language="de"><![CDATA[Felderkennung läuft noch.]]></Text>
<Text language="en"><![CDATA[Field detection still running.]]></Text>
</Translation>
<Translation name="CP_error_field_detection_failed">
<Text language="de"><![CDATA[Felderkennung fehlgeschlagen.]]></Text>
<Text language="en"><![CDATA[Field detection failed.]]></Text>
</Translation>
<Translation name="CP_error_not_on_field">
<Text language="de"><![CDATA[Ziel ist nicht auf einem Feld.]]></Text>
<Text language="en"><![CDATA[Target is not on a field.]]></Text>
Expand Down
17 changes: 17 additions & 0 deletions scripts/ai/jobs/CpAIJob.lua
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@ function CpAIJob:onFieldBoundaryDetectionFinished(vehicle, fieldPolygon, islandP
-- override in the derived classes to handle the detected field boundary
end

--- If registered, call the field boundary detection finished callback. This is to notify the frame
--- at the end of the async field detection.
function CpAIJob:callFieldBoundaryDetectionFinishedCallback(isValid, errorTextName)
local c = self.onFieldBoundaryDetectionFinishedCallback
if c and c.object and c.func then
c.func(c.object, isValid, errorTextName and g_i18n:getText(errorTextName) or '')
end
end

--- Register a callback for the field boundary detection finished event.
--- @param object table object to call the function on
--- @param func function function to call func(boolean isValid, string|nil errorTextName), errorTextName is the
--- name of the text in MasterTranslations.xml
function CpAIJob:registerFieldBoundaryDetectionCallback(object, func)
self.onFieldBoundaryDetectionFinishedCallback = {object = object, func = func}
end

function CpAIJob:getIsStartable(connection)

local vehicle = self.vehicleParameter:getVehicle()
Expand Down
5 changes: 2 additions & 3 deletions scripts/ai/jobs/CpAIJobBaleFinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ function CpAIJobBaleFinder:onFieldBoundaryDetectionFinished(vehicle, fieldPolygo
if fieldPolygon then
self.selectedFieldPlot:setWaypoints(fieldPolygon)
self.selectedFieldPlot:setVisible(true)
-- TODO: here we need to tell somehow the frame about the detection success/failure
self:callFieldBoundaryDetectionFinishedCallback(true)
else
self.selectedFieldPlot:setVisible(false)
-- TODO: here we need to tell somehow the frame about the detection success/failure
return false, g_i18n:getText("CP_error_not_on_field")
self:callFieldBoundaryDetectionFinishedCallback(false, 'CP_error_field_detection_failed')
end
end

Expand Down
15 changes: 6 additions & 9 deletions scripts/ai/jobs/CpAIJobCombineUnloader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,12 @@ function CpAIJobCombineUnloader:onFieldBoundaryDetectionFinished(vehicle, fieldP
isValid = CpMathUtil.isPointInPolygon(fieldPolygon, x, z) or
CpMathUtil.getClosestDistanceToPolygonEdge(fieldPolygon, x, z) < self.minStartDistanceToField
if not isValid then
-- TODO notify the frame? But what about the direct start? The same is check in the strategy, but has no consequence
return false, g_i18n:getText("CP_error_start_position_to_far_away_from_field")
self:callFieldBoundaryDetectionFinishedCallback(false, "CP_error_start_position_to_far_away_from_field")
end
end
if not isValid then
-- TODO notify the frame? But what about the direct start? The bale finder does this check in the strategy
return false, g_i18n:getText("CP_error_unloader_to_far_away_from_field")
self:callFieldBoundaryDetectionFinishedCallback(false, "CP_error_unloader_to_far_away_from_field")
return
end
end
------------------------------------
Expand All @@ -197,8 +196,8 @@ function CpAIJobCombineUnloader:onFieldBoundaryDetectionFinished(vehicle, fieldP
isValid = CpMathUtil.isPointInPolygon(fieldPolygon, x, z) or
CpMathUtil.getClosestDistanceToPolygonEdge(fieldPolygon, x, z) < self.minFieldUnloadDistanceToField
if not isValid then
-- TODO notify the frame? But what about the direct start?
return false, g_i18n:getText("CP_error_fieldUnloadPosition_too_far_away_from_field")
self:callFieldBoundaryDetectionFinishedCallback(false, 'CP_error_fieldUnloadPosition_too_far_away_from_field')
return
end
--- Draws the silo
local angle = self.cpJobParameters.fieldUnloadPosition:getAngle()
Expand All @@ -210,9 +209,7 @@ function CpAIJobCombineUnloader:onFieldBoundaryDetectionFinished(vehicle, fieldP
self.heapPlot:setVisible(true)
end
end

-- TODO notify the frame? But what about the direct start?
return isValid, errorMessage
self:callFieldBoundaryDetectionFinishedCallback(true)
end

function CpAIJobCombineUnloader:draw(map, isOverviewMap)
Expand Down
5 changes: 2 additions & 3 deletions scripts/ai/jobs/CpAIJobFieldWork.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,9 @@ function CpAIJobFieldWork:onFieldBoundaryDetectionFinished(vehicle, fieldPolygon
self.selectedFieldPlot:setVisible(true)
else
self.selectedFieldPlot:setVisible(false)
-- TODO: here we need to tell somehow the frame about the detection success/failure
return false, g_i18n:getText("CP_error_not_on_field")
self:callFieldBoundaryDetectionFinishedCallback(false, 'CP_error_field_detection_failed')
end
-- TODO: here we need to tell somehow the frame about the detection success/failure
self:callFieldBoundaryDetectionFinishedCallback(true)
end

function CpAIJobFieldWork:setValues()
Expand Down
10 changes: 9 additions & 1 deletion scripts/gui/pages/CpCourseGeneratorFrame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -866,14 +866,22 @@ function CpCourseGeneratorFrame:validateParameters()
local errorText = ""
if self.currentJob ~= nil then
self.currentJob:setValues()
errorText = self.currentJob:validate()
self.currentJob:registerFieldBoundaryDetectionCallback(self, self.onFieldBoundaryDetectionFinished)
isValid, errorText = self.currentJob:validate()
self:updateWarnings()
self:updateContextActions()
end
self.errorMessage:setText(errorText)
self.errorMessage:setVisible(not isValid)
end

function CpCourseGenerator:onFieldBoundaryDetectionFinished(isValid, errorText)
self:updateWarnings()
self:updateContextActions()
self.errorMessage:setText(errorText)
self.errorMessage:setVisible(not isValid)
end

function CpCourseGeneratorFrame:updateWarnings()
for _, element in ipairs(self.currentJobElements) do
local parameter = element.aiParameter
Expand Down

0 comments on commit d630262

Please sign in to comment.