diff --git a/Moose Development/Moose/Core/Base.lua b/Moose Development/Moose/Core/Base.lua index 854252cbc..fe186131a 100644 --- a/Moose Development/Moose/Core/Base.lua +++ b/Moose Development/Moose/Core/Base.lua @@ -451,23 +451,23 @@ do -- Event Handling --- Subscribe to a DCS Event. -- @param #BASE self - -- @param Core.Event#EVENTS Event + -- @param Core.Event#EVENTS EventID Event ID. -- @param #function EventFunction (optional) The function to be called when the event occurs for the unit. -- @return #BASE - function BASE:HandleEvent( Event, EventFunction ) + function BASE:HandleEvent( EventID, EventFunction ) - self:EventDispatcher():OnEventGeneric( EventFunction, self, Event ) + self:EventDispatcher():OnEventGeneric( EventFunction, self, EventID ) return self end --- UnSubscribe to a DCS event. -- @param #BASE self - -- @param Core.Event#EVENTS Event + -- @param Core.Event#EVENTS EventID Event ID. -- @return #BASE - function BASE:UnHandleEvent( Event ) + function BASE:UnHandleEvent( EventID ) - self:EventDispatcher():RemoveEvent( self, Event ) + self:EventDispatcher():RemoveEvent( self, EventID ) return self end diff --git a/Moose Development/Moose/Core/Event.lua b/Moose Development/Moose/Core/Event.lua index c19e41d4c..1f0b99b0a 100644 --- a/Moose Development/Moose/Core/Event.lua +++ b/Moose Development/Moose/Core/Event.lua @@ -526,17 +526,21 @@ local _EVENTMETA = { -- @param #EVENT self -- @return #EVENT self function EVENT:New() + + -- Inherit base. local self = BASE:Inherit( self, BASE:New() ) - self:F2() - self.EventHandler = world.addEventHandler( self ) + + -- Add world event handler. + self.EventHandler = world.addEventHandler(self) + return self end ---- Initializes the Events structure for the event +--- Initializes the Events structure for the event. -- @param #EVENT self --- @param DCS#world.event EventID --- @param Core.Base#BASE EventClass +-- @param DCS#world.event EventID Event ID. +-- @param Core.Base#BASE EventClass The class object for which events are handled. -- @return #EVENT.Events function EVENT:Init( EventID, EventClass ) self:F3( { _EVENTMETA[EventID].Text, EventClass } ) @@ -548,6 +552,7 @@ function EVENT:Init( EventID, EventClass ) -- Each event has a subtable of EventClasses, ordered by EventPriority. local EventPriority = EventClass:GetEventPriority() + if not self.Events[EventID][EventPriority] then self.Events[EventID][EventPriority] = setmetatable( {}, { __mode = "k" } ) end @@ -555,40 +560,45 @@ function EVENT:Init( EventID, EventClass ) if not self.Events[EventID][EventPriority][EventClass] then self.Events[EventID][EventPriority][EventClass] = {} end - - + return self.Events[EventID][EventPriority][EventClass] end --- Removes a subscription -- @param #EVENT self -- @param Core.Base#BASE EventClass The self instance of the class for which the event is. --- @param DCS#world.event EventID --- @return #EVENT.Events +-- @param DCS#world.event EventID Event ID. +-- @return #EVENT self function EVENT:RemoveEvent( EventClass, EventID ) + -- Debug info. self:F2( { "Removing subscription for class: ", EventClass:GetClassNameAndID() } ) + -- Get event prio. local EventPriority = EventClass:GetEventPriority() + -- Events. self.Events = self.Events or {} self.Events[EventID] = self.Events[EventID] or {} self.Events[EventID][EventPriority] = self.Events[EventID][EventPriority] or {} + -- Remove self.Events[EventID][EventPriority][EventClass] = nil + return self end ---- Resets subscriptions +--- Resets subscriptions. -- @param #EVENT self -- @param Core.Base#BASE EventClass The self instance of the class for which the event is. --- @param DCS#world.event EventID +-- @param DCS#world.event EventID Event ID. -- @return #EVENT.Events function EVENT:Reset( EventObject ) --R2.1 self:F( { "Resetting subscriptions for class: ", EventObject:GetClassNameAndID() } ) local EventPriority = EventObject:GetEventPriority() + for EventID, EventData in pairs( self.Events ) do if self.EventsDead then if self.EventsDead[EventID] then @@ -603,19 +613,22 @@ function EVENT:Reset( EventObject ) --R2.1 end - - --- Clears all event subscriptions for a @{Core.Base#BASE} derived object. -- @param #EVENT self --- @param Core.Base#BASE EventObject -function EVENT:RemoveAll( EventObject ) - self:F3( { EventObject:GetClassNameAndID() } ) +-- @param Core.Base#BASE EventClass The self class object for which the events are removed. +-- @return #EVENT self +function EVENT:RemoveAll(EventClass) - local EventClass = EventObject:GetClassNameAndID() + local EventClassName = EventClass:GetClassNameAndID() + + -- Get Event prio. local EventPriority = EventClass:GetEventPriority() + for EventID, EventData in pairs( self.Events ) do self.Events[EventID][EventPriority][EventClass] = nil end + + return self end @@ -636,7 +649,7 @@ function EVENT:OnEventForTemplate( EventTemplate, EventFunction, EventClass, Eve return self end ---- Set a new listener for an S_EVENT_X event independent from a unit or a weapon. +--- Set a new listener for an `S_EVENT_X` event independent from a unit or a weapon. -- @param #EVENT self -- @param #function EventFunction The function to be called when the event occurs for the unit. -- @param Core.Base#BASE EventClass The self instance of the class for which the event is captured. When the event happens, the event process will be called in this class provided. @@ -652,13 +665,13 @@ function EVENT:OnEventGeneric( EventFunction, EventClass, EventID ) end ---- Set a new listener for an S_EVENT_X event for a UNIT. +--- Set a new listener for an `S_EVENT_X` event for a UNIT. -- @param #EVENT self -- @param #string UnitName The name of the UNIT. -- @param #function EventFunction The function to be called when the event occurs for the GROUP. -- @param Core.Base#BASE EventClass The self instance of the class for which the event is. -- @param EventID --- @return #EVENT +-- @return #EVENT self function EVENT:OnEventForUnit( UnitName, EventFunction, EventClass, EventID ) self:F2( UnitName ) @@ -724,10 +737,10 @@ do -- OnDead --- Create an OnDead event handler for a group -- @param #EVENT self - -- @param Wrapper.Group#GROUP EventGroup + -- @param Wrapper.Group#GROUP EventGroup The GROUP object. -- @param #function EventFunction The function to be called when the event occurs for the unit. - -- @param EventClass The self instance of the class for which the event is. - -- @return #EVENT + -- @param #table EventClass The self instance of the class for which the event is. + -- @return #EVENT self function EVENT:OnDeadForTemplate( EventTemplate, EventFunction, EventClass ) self:F2( EventTemplate.name ) @@ -740,12 +753,13 @@ end do -- OnLand + --- Create an OnLand event handler for a group -- @param #EVENT self -- @param #table EventTemplate -- @param #function EventFunction The function to be called when the event occurs for the unit. - -- @param EventClass The self instance of the class for which the event is. - -- @return #EVENT + -- @param #table EventClass The self instance of the class for which the event is. + -- @return #EVENT self function EVENT:OnLandForTemplate( EventTemplate, EventFunction, EventClass ) self:F2( EventTemplate.name ) @@ -757,12 +771,13 @@ do -- OnLand end do -- OnTakeOff + --- Create an OnTakeOff event handler for a group -- @param #EVENT self - -- @param #table EventTemplate + -- @param #table EventTemplate Template table. -- @param #function EventFunction The function to be called when the event occurs for the unit. - -- @param EventClass The self instance of the class for which the event is. - -- @return #EVENT + -- @param #table EventClass The self instance of the class for which the event is. + -- @return #EVENT self function EVENT:OnTakeOffForTemplate( EventTemplate, EventFunction, EventClass ) self:F2( EventTemplate.name ) @@ -902,8 +917,9 @@ do -- Event Creation end ---- @param #EVENT self --- @param #EVENTDATA Event +--- Main event function. +-- @param #EVENT self +-- @param #EVENTDATA Event Event data table. function EVENT:onEvent( Event ) local ErrorHandler = function( errmsg ) @@ -1287,7 +1303,7 @@ function EVENT:onEvent( Event ) Event = nil end ---- The EVENTHANDLER structure +--- The EVENTHANDLER structure. -- @type EVENTHANDLER -- @extends Core.Base#BASE EVENTHANDLER = { @@ -1295,7 +1311,7 @@ EVENTHANDLER = { ClassID = 0, } ---- The EVENTHANDLER constructor +--- The EVENTHANDLER constructor. -- @param #EVENTHANDLER self -- @return #EVENTHANDLER self function EVENTHANDLER:New() diff --git a/Moose Development/Moose/Wrapper/Unit.lua b/Moose Development/Moose/Wrapper/Unit.lua index 7f36de4c4..64b656b52 100644 --- a/Moose Development/Moose/Wrapper/Unit.lua +++ b/Moose Development/Moose/Wrapper/Unit.lua @@ -1179,23 +1179,26 @@ do -- Event Handling --- Subscribe to a DCS Event. -- @param #UNIT self - -- @param Core.Event#EVENTS Event - -- @param #function EventFunction (optional) The function to be called when the event occurs for the unit. - -- @return #UNIT - function UNIT:HandleEvent( Event, EventFunction ) + -- @param Core.Event#EVENTS EventID Event ID. + -- @param #function EventFunction (Optional) The function to be called when the event occurs for the unit. + -- @return #UNIT self + function UNIT:HandleEvent(EventID, EventFunction) - self:EventDispatcher():OnEventForUnit( self:GetName(), EventFunction, self, Event ) + self:EventDispatcher():OnEventForUnit(self:GetName(), EventFunction, self, EventID) return self end --- UnSubscribe to a DCS event. -- @param #UNIT self - -- @param Core.Event#EVENTS Event - -- @return #UNIT - function UNIT:UnHandleEvent( Event ) + -- @param Core.Event#EVENTS EventID Event ID. + -- @return #UNIT self + function UNIT:UnHandleEvent(EventID) - self:EventDispatcher():RemoveForUnit( self:GetName(), self, Event ) + --self:EventDispatcher():RemoveForUnit( self:GetName(), self, EventID ) + + -- Fixes issue #1365 https://github.com/FlightControl-Master/MOOSE/issues/1365 + self:EventDispatcher():RemoveEvent(self, EventID) return self end