Thread: KeyAction
View Single Post
Old 11-10-2009, 01:09 PM   #5
Mr.Malte
User
 
Mr.Malte's Avatar
 
Join Date: Apr 2008
Posts: 286

Submissions (2)

Mr.Malte is on a distinguished road (11)

Default

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.
Mr.Malte is offline   Reply With Quote