• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

DoubleClick

Level 8
Joined
Oct 3, 2008
Messages
367
It detects when you click the same unit twice within the provided time (a second by default).

Requires a vJass preprocessor.

A single function is available to you.
JASS:
function TriggerRegisterDoubleClickEvent takes trigger whichTrigger returns nothing

DestroyTrigger friendly, leakless, uses no H2I or any attachment except for what is done at initialization.

Requires no outside systems.

Yours, Azlier.

JASS:
library DoubleClick initializer Init


globals
    private constant real CLICK_THRESHHOLD = 1.
    //How much time between clicks it takes to register a double click.
endglobals

//** System Code **

private function SetTriggerData takes trigger t, integer i returns nothing
    loop
        exitwhen i == 0
        call TriggerExecute(t)
        set i = i - 1
    endloop
endfunction

private struct TrigChain
    
    TrigChain next = 0
    TrigChain prev = 0
    trigger trig = null
    
    static TrigChain temp
    static integer exec
    
    method addNode takes trigger trig returns nothing
        local TrigChain new = TrigChain.create()
        set new.trig = trig
        set new.next = .next
        set .next.prev = new
        set new.prev = this
        set .next = new
    endmethod
    
    method fire takes nothing returns nothing
        set this = .next
        loop
            exitwhen this == 0
            if IsTriggerEnabled(.trig) then
                if TriggerEvaluate(.trig) then
                    call TriggerExecute(.trig)
                endif
            else
                call EnableTrigger(.trig)
                if not IsTriggerEnabled(.trig) then
                    set .next.prev = .prev
                    set .prev.next = .next
                    call this.destroy()
                else
                    call DisableTrigger(.trig)
                endif
            endif
            set this = .next
        endloop
    endmethod
    
endstruct

globals
    private boolean array Clicking
    private unit array Clicked
    private timer array ClickTimer
    private trigger array ClickTrig
    private TrigChain Registered
endglobals

function TriggerRegisterDoubleClickEvent takes trigger whichTrigger returns nothing
    call Registered.addNode(whichTrigger) //Inline friendly? This is madness.
endfunction

private function Callback takes nothing returns boolean
    set Clicking[GetTriggerExecCount(GetTriggeringTrigger())] = false
    return false
endfunction

private function Actions takes nothing returns boolean
    
    globals
        private integer Id
        private unit Cu
    endglobals
    
    set Id = GetPlayerId(GetTriggerPlayer())
    set Cu = GetTriggerUnit()
    if Clicking[Id] then
        call PauseTimer(ClickTimer[Id])
        if Clicked[Id] == Cu then
            call Registered.fire()
            set Clicking[Id] = false
        else
            set Clicked[Id] = Cu
            call TimerStart(ClickTimer[Id], CLICK_THRESHHOLD, false, null)
        endif
    else
        set Clicking[Id] = true
        set Clicked[Id] = Cu
        call TimerStart(ClickTimer[Id], CLICK_THRESHHOLD, false, null)
    endif
    return false
endfunction

private function Init takes nothing returns nothing
    local trigger t = CreateTrigger()
    local integer i = 15
    loop
        exitwhen i < 0
        call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_SELECTED, null)
        set ClickTimer[i] = CreateTimer()
        set ClickTrig[i] = CreateTrigger()
        call SetTriggerData.execute(ClickTrig[i], i)
        call TriggerRegisterTimerExpireEvent(ClickTrig[i], ClickTimer[i])
        call TriggerAddCondition(ClickTrig[i], Condition(function Callback))
        set i = i - 1
    endloop
    call TriggerAddCondition(t, Condition(function Actions))
    set Registered = TrigChain.create()
endfunction

endlibrary
 

Attachments

  • DoubleClick.w3x
    24.1 KB · Views: 36
Top