Wc3C.net

Wc3C.net (http://www.wc3c.net/forums.php)
-   Scripts (http://www.wc3c.net/forumdisplay.php?f=737)
-   -   KeyAction (http://www.wc3c.net/showthread.php?t=108172)

Earth-Fury 11-10-2009 09:51 AM

KeyAction
 
Zoom (requires log in)
Arrow Key Helpers
Version 0.1.0
Introduction
KeyAction is written in vJass and requires the NewGen editor, or Jass Helper in some other configuration. KeyAction requires the latest version of Jass Helper.

KeyAction is a library that provides a simpler, cleaner API for dealing with arrow key related events. Documentation of the API is available in the library's documentation header.
Credits
The KeyAction Library
Requirements:
  • None

To add KeyAction to your map, copy and paste the below library in to a custom-text trigger in your map.
Expand KeyAction:

Change Log

Change Log

  • 0.1:
    • Initial version


Anachron 11-10-2009 10:54 AM

Yes very nice,... But I guess there is nothing that removes the delay of arrow keys or?

Anyway, I see very much use in this. For an hero arrow selection system, for example.

Still don't get why you use a trigger per arrow and for every arrowstate, instead sharing one and checking the gameevent.

Collapse JASS:
function eventHandler takes nothing returns nothing
    if EVENT_PLAYER_ARROW_DOWN_UP == GetTriggerEventId() then
           //Arrow_up_stuff
    endif
endfunction

Earth-Fury 11-10-2009 11:16 AM

Quote:

Originally Posted by Anachron
Yes very nice,... But I guess there is nothing that removes the delay of arrow keys or?


Using a program like DelayReducer to reduce Battle.net lag will help on battle.net. There is still, however, an intrinsic delay in arrow keys registering events for some unknown reason.

Quote:

Originally Posted by Anachron
Still don't get why you use a trigger per arrow and for every arrowstate, instead sharing one and checking the gameevent.

Collapse JASS:
function eventHandler takes nothing returns nothing
    if EVENT_PLAYER_ARROW_DOWN_UP == GetTriggerEventId() then
           //Arrow_up_stuff
    endif
endfunction

That would add the cost of an 8 branch logical structure to all arrow key events... But more seriously, who gives a flying fuck considering both methods are nigh exactly the same?

Anachron 11-10-2009 11:26 AM

Quote:

That would add the cost of an 8 branch logical structure to all arrow key events... But more seriously, who gives a flying fuck considering both methods are nigh exactly the same?
So what about it? If possible I would like to have the possibility to disable the systems by not adding the event-handler.

But it doesn't matter at all. Its good, and since I have my own bnet I don't have any delay.

Mr.Malte 11-10-2009 01:09 PM

Hey, nice.
Made this a while ago:

Collapse JASS:
library ArrowkeyController initializer Init
    globals
        // A problem with the arrow keys are, that they always have some delay.
        // Normally it's 0.25 (it's a constant)
        private constant real ARROW_KEY_REACTION_TIME = 0.25
        // The bigger the smoother the 'back camera movement' but the less
        // the accurarry
        private constant real CAMERA_SMOOTHING = 1.00
    endglobals
    
    globals
        private trigger onArrowKey = CreateTrigger()
        private trigger releaseArrowKey = CreateTrigger()
        private boolean array Allowed
        private boolean array IsLocked
        private real array CamX
        private real array CamY
    endglobals
    
    private function unlockCamera takes nothing returns nothing
        local integer ID = GetPlayerId(GetTriggerPlayer())
        local real x
        local real y
        if Allowed[ID] then
            if GetLocalPlayer() == GetTriggerPlayer() then
                call PanCameraToTimed(CamX[ID], CamY[ID], 0.5)
            endif
            set IsLocked[ID] = false
        endif
    endfunction
    
    private function lockCamera takes nothing returns nothing
        local integer ID = GetPlayerId(GetTriggerPlayer())
        local real x
        local real y
        if Allowed[ID] then
            set IsLocked[ID] = true
            if GetLocalPlayer() == GetTriggerPlayer() then
                call PanCameraToTimed(CamX[ID], CamY[ID], ARROW_KEY_REACTION_TIME*CAMERA_SMOOTHING)
            endif
        endif
    endfunction
    
    private function SaveCamCoords takes nothing returns nothing
        local integer i = 0
        loop
            exitwhen i == 12
                if IsLocked[i] == false then
                    if GetLocalPlayer() == Player(i) then
                        set CamX[i] = GetCameraTargetPositionX()
                        set CamY[i] = GetCameraTargetPositionY()
                    endif
                endif
            set i = i + 1
        endloop
    endfunction
    
    private function Init takes nothing returns nothing
        local integer i = 0
        loop
            exitwhen i == 12
                set Allowed[i] = true
                if GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(Player(i)) == MAP_CONTROL_USER then
                    call TriggerRegisterPlayerEvent(onArrowKey, Player(i), EVENT_PLAYER_ARROW_UP_DOWN)
                    call TriggerRegisterPlayerEvent(onArrowKey, Player(i), EVENT_PLAYER_ARROW_DOWN_DOWN)
                    call TriggerRegisterPlayerEvent(onArrowKey, Player(i), EVENT_PLAYER_ARROW_RIGHT_DOWN)
                    call TriggerRegisterPlayerEvent(onArrowKey, Player(i), EVENT_PLAYER_ARROW_LEFT_DOWN)
                    
                    call TriggerRegisterPlayerEvent(releaseArrowKey, Player(i), EVENT_PLAYER_ARROW_UP_UP)
                    call TriggerRegisterPlayerEvent(releaseArrowKey, Player(i), EVENT_PLAYER_ARROW_DOWN_UP)
                    call TriggerRegisterPlayerEvent(releaseArrowKey, Player(i), EVENT_PLAYER_ARROW_RIGHT_UP)
                    call TriggerRegisterPlayerEvent(releaseArrowKey, Player(i), EVENT_PLAYER_ARROW_LEFT_UP)
                endif
            set i = i + 1
        endloop
        call TriggerAddAction(onArrowKey,function lockCamera)
        call TriggerAddAction(releaseArrowKey,function unlockCamera)
        call TimerStart(CreateTimer(),ARROW_KEY_REACTION_TIME,true,function SaveCamCoords)
    endfunction
    
    public function Allow takes boolean flag returns nothing
        local integer i = 0
        if flag == true then
            call DisableTrigger(onArrowKey)
            call DisableTrigger(releaseArrowKey)
        else
            loop
                exitwhen i == 12
                call PanCameraToTimed(CamX[i], CamY[i], 0.5)
                set i = i + 1
            endloop
            call EnableTrigger(onArrowKey)
            call EnableTrigger(releaseArrowKey)
        endif
    endfunction
    
    public function AllowFor takes player p, boolean flag returns nothing
        set Allowed[GetPlayerId(p)] = flag
    endfunction
endlibrary

You should add sth. like that to your library. Making movement via arrow keys not possible.
But I'm not sure if that fits into a library like that.

Niceldy done.

grim001 11-10-2009 05:34 PM

Quote:

Originally Posted by Mr.Malte
You should add sth. like that to your library. Making movement via arrow keys not possible. But I'm not sure if that fits into a library like that.


That's the kind of thing that should require a library like this. It would shorten and simplify the code.

Tot 11-10-2009 06:23 PM

of topic:

how did you create this?

Quote:

Originally Posted by EF
Zoom (requires log in)


It's really cool :emote_thumbup:

Rising_Dusk 04-16-2010 03:56 PM

Remind me why I would ever use this over Anitarf's ArrowKeys library in the database, again?

Earth-Fury 04-19-2010 05:43 PM

Quote:

Originally Posted by Rising_Dusk
Remind me why I would ever use this over Anitarf's ArrowKeys library in the database, again?


I like my API better.

Also the ability to register multiple callbacks directly to the library. (Limited utility, but useful on rare occasions.)

I don't feel like putting work in to improving this much any time soon, so do as you see fit. Let it rot, approve it, graveyard it. (I can always resubmit it in the future)

Anitarf 12-07-2010 02:41 PM

I guess I've spent enough time trying to figure out what to do with this. It is certainly coded with more rigour than ArrowKeys and should probably replace it. However, I don't see anything here that would easily replicate the "quickpress" functionality of ArrowKeys, which is a bit ugly but a very functional hack. On top of that, I suppose some people might prefer a more lightweight library so I guess the two of them can coexist in the resource section, even if that means the number of libraries handling arrow keys will approach the number of people who actually have a use for them.

Approved.

Anitarf 09-26-2011 11:57 AM

I found a couple of bugs in the OnKeyAction module.

Expand Original code:

Expand Fixed code:

Edit: there can also be trouble if struct instances are destroyed in the OnKeyAction callback. Not sure what would be the best way to avoid issues here.

Yrth 10-15-2014 11:20 AM

this bug doesnt break anything but definitely a bad bug for how easy the fix is...
Collapse JASS:
private function OnInit takes nothing returns nothing
    local integer i
    
    //! textmacro KeyAction_RegisterEvents takes KEY, DIR
        set i = 0
        loop
            exitwhen i > 11
            
            // The actual event
            call TriggerRegisterPlayerEvent(key_$KEY$_$DIR$, Player(i), EVENT_PLAYER_ARROW_$KEY$_$DIR$)
            
            // For the functions
            call TriggerAddCondition(key_$KEY$_$DIR$, Condition(function HandleKeyEventFunc_$KEY$_$DIR$))
            
            set i = i + 1
        endloop
    //! endtextmacro
    
    //! runtextmacro KeyAction_RegisterEvents("UP",    "DOWN")
    //! runtextmacro KeyAction_RegisterEvents("UP",    "UP")
    //! runtextmacro KeyAction_RegisterEvents("DOWN",  "DOWN")
    //! runtextmacro KeyAction_RegisterEvents("DOWN",  "UP")
    //! runtextmacro KeyAction_RegisterEvents("LEFT",  "DOWN")
    //! runtextmacro KeyAction_RegisterEvents("LEFT",  "UP")
    //! runtextmacro KeyAction_RegisterEvents("RIGHT", "DOWN")
    //! runtextmacro KeyAction_RegisterEvents("RIGHT", "UP")
endfunction

should be

Collapse JASS:
private function OnInit takes nothing returns nothing
    local integer i
    
    //! textmacro KeyAction_RegisterEvents takes KEY, DIR
        set i = 0
        loop
            exitwhen i > 11
            
            // The actual event
            call TriggerRegisterPlayerEvent(key_$KEY$_$DIR$, Player(i), EVENT_PLAYER_ARROW_$KEY$_$DIR$)

            set i = i + 1
        endloop

        // For the functions
        call TriggerAddCondition(key_$KEY$_$DIR$, Condition(function HandleKeyEventFunc_$KEY$_$DIR$))
    //! endtextmacro
    
    //! runtextmacro KeyAction_RegisterEvents("UP",    "DOWN")
    //! runtextmacro KeyAction_RegisterEvents("UP",    "UP")
    //! runtextmacro KeyAction_RegisterEvents("DOWN",  "DOWN")
    //! runtextmacro KeyAction_RegisterEvents("DOWN",  "UP")
    //! runtextmacro KeyAction_RegisterEvents("LEFT",  "DOWN")
    //! runtextmacro KeyAction_RegisterEvents("LEFT",  "UP")
    //! runtextmacro KeyAction_RegisterEvents("RIGHT", "DOWN")
    //! runtextmacro KeyAction_RegisterEvents("RIGHT", "UP")
endfunction
it probably wasnt noticed before because it sorta does the same thing in game. well. it calls execute on the same interface 12 times, each time crashing the one before it until it finally runs fully on the 12th, but hey what's it matter if it runs
oh yeah it also means that the initial event (where it sets the key down and all that internal magic) happens 12 times, each time the exact same things being set lol

also i thought id share my findings after using this. the system works great and allows easy access / general use. id definitely recommend this as a prototyping thing ("hmm... is this actually a good idea?") but if you need to react to quick key presses youll need to either make your own system or butcher out the "general" components of this one. by quickpress, i mean >1 second reaction time AND frequent key presses. you should just use this system barring that


All times are GMT. The time now is 01:56 PM.

Powered by vBulletin (Copyright ©2000 - 2019, Jelsoft Enterprises Ltd).
Hosted by www.OICcam.com
IT Support and Services provided by Executive IT Services