- Joined
- Dec 3, 2011
- Messages
- 366
CHANGELOG
- Change some API.
JASS:
library TriggerManager requires Table
/*
Credit:
Bribe - Table ( [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/[/url] )
Description: A snippet which allows to control triggers easier.
* Pros:
+ Can set a custom data to trigger.
+ Easy to control trigger actions and conditions.
+ Automatic index actions/conditions handle
---------------------------------------------------------------------------------------------------------------------------------
| struct Trigger
| static method create takes integer data returns Trigger
| Create the trigger and give it a data
|
| method destroy takes nothing returns nothing
| Destroy it.
|
| method operator trigger takes nothing returns trigger
| Return trigger that you have created.
|
| method operator data takes nothing returns integer
| Return trigger's data.
|
| static method operator [] takes trigger t returns Trigger
| Takes a trigger which have been created by this struct to return Trigger.
|
| method addAction takes code func returns integer handle
| Add action to trigger and return this action handle. This handle will be used on removeAction.
|
| method replaceAction takes code func, integer handle returns nothing
| Replace old action with a new action.
|
| method removeAction takes integer handle returns nothing
| Remove action.
|
| method addCondition takes code func returns integer handle
| Add condition to trigger and return this condition handle. This handle will be used on removeCondition.
|
| method replaceCondition takes code func, integer handle returns nothing
| Replace old condition with a new condition.
|
| method removeCondition takes integer handle returns nothing
| Remove condition.
|
| method enable takes nothing returns nothing
| Turn on the trigger.
|
| method disable takes nothing returns nothing
| Turn off the trigger.
|
| method clearActions takes nothing returns nothing
| Clear all actions.
|
| method clearConditions takes nothing returns nothing
| Clear all conditions.
|
| method clear takes nothing returns nothing
| Clear all actions and conditions.
---------------------------------------------------------------------------------------------------------------------------------
*/
struct Trigger
private static Table TriggerTable
private static integer ActionCounter = 0
private static integer ConditionCounter = 0
private static integer array ActionRecycler
private static integer array ConditionRecycler
static method [] takes trigger t returns thistype
return TriggerTable.integer[GetHandleId(t)]
endmethod
method operator data takes nothing returns integer
return TriggerTable.integer[this]
endmethod
method operator trigger takes nothing returns trigger
return TriggerTable.trigger[this]
endmethod
method destroy takes nothing returns nothing
call DestroyTrigger(trigger)
call TriggerTable[this].flush()
call deallocate()
endmethod
method clearActions takes nothing returns nothing
call TriggerClearActions(trigger)
endmethod
method clearConditions takes nothing returns nothing
call TriggerClearConditions(trigger)
endmethod
method clear takes nothing returns nothing
call clearActions()
call clearConditions()
endmethod
method replaceAction takes code func, integer i returns nothing
call TriggerRemoveAction(trigger,TriggerTable[this].triggeraction[i])
set TriggerTable[this].triggeraction[i] = null
set TriggerTable[this].triggeraction[i] = TriggerAddAction(trigger,func)
endmethod
method replaceCondition takes code func, integer i returns nothing
call TriggerRemoveCondition(trigger,TriggerTable[this].triggercondition[i])
set TriggerTable[this].triggercondition[i] = null
set TriggerTable[this].triggercondition[i] = TriggerAddCondition(trigger,Filter(func))
endmethod
method removeAction takes integer i returns nothing
set ActionRecycler[0] = ActionRecycler[this]
set ActionRecycler[0] = this
call TriggerRemoveAction(trigger,TriggerTable[this].triggeraction[i])
set TriggerTable[this].triggeraction[i] = null
endmethod
method removeCondition takes integer i returns nothing
set ConditionRecycler[0] = ConditionRecycler[this]
set ConditionRecycler[0] = this
call TriggerRemoveCondition(trigger,TriggerTable[this].triggercondition[i])
set TriggerTable[this].triggercondition[i] = null
endmethod
method addAction takes code func returns integer
local integer result = ConditionRecycler[0]
if result == 0 then
set result = ActionCounter + 1
set ActionCounter = result
else
set ActionRecycler[0] = ActionRecycler[result]
endif
set TriggerTable[this].triggeraction[result] = TriggerAddAction(trigger,func)
return result
endmethod
method addCondition takes code func returns integer
local integer result = ConditionRecycler[0]
if result == 0 then
set result = ConditionCounter + 1
set ConditionCounter = result
else
set ConditionRecycler[0] = ConditionRecycler[result]
endif
set TriggerTable[this].triggercondition[result] = TriggerAddCondition(trigger,Filter(func))
return result
endmethod
method enable takes nothing returns nothing
call EnableTrigger(trigger)
endmethod
method disable takes nothing returns nothing
call DisableTrigger(trigger)
endmethod
static method create takes integer data returns thistype
local thistype this = allocate()
if TriggerTable[this] == null then
set TriggerTable[this] = Table.create()
endif
set TriggerTable.trigger[this] = CreateTrigger()
set TriggerTable.integer[this] = data
set TriggerTable.integer[GetHandleId(trigger)] = this
return this
endmethod
private static method onInit takes nothing returns nothing
set TriggerTable = Table.create()
endmethod
endstruct
endlibrary
Test map:
View attachment Trigger Manager 1.0 .w3m
Attachments
Last edited: