Prozix
New Member
- Reaction score
- 7
This library makes checking for arrow key presses a lot easier. You should definitely check the demo map out to see what you can accomplish with this.
This system is a time saver with an easy way to accomplish things involving keypresses
An extensive example of how you could use it:
This example is included in the demo map
Have fun!
JASS:
library EasyKeys initializer Init
//EasyKeys v1.1 by Prozix
//pros: - Everything has been done for you
// - Easy to use
// - You can check for: Newpress, Doublepress, Release, Held and Doubleheld in this version!
//cons: - Slow (due to warcraft keypress event limitations)
// - EasyKeys requires you to call an update function in order to make this system work which would become annoying to do properly if you have multiple triggers using this system at once
//how to use:
// Lets say I want to know if player 1 (red) is holding the up key:
// if Pad[0].U.Held() then
// BJDebugMsg("Up key being pressed by player 1")
// endif
// general syntax: Pad[#playerid#].#key#.#state()# with
// playerid = the players id (0-11)
// key = U, D, L or R (up down left right)
// state() = Held(), Doubleheld(), Newpress(), Doublepress() or Released()
// Maximum amount of ticks to register a double press can be adjusted in the globals section below
globals
private constant integer KEY_LOOSE = 0
private constant integer KEY_NEWPRESS = 1
private constant integer KEY_HELD = 2
private constant integer KEY_RELEASED = 3
private constant integer KEY_DOUBLEPRESS = 4
private constant integer KEY_DOUBLEHELD = 5
private constant integer DOUBLEPRESS_TICKLIMIT = 8
endglobals
private struct KEY
integer action
integer ticks
static method create takes nothing returns thistype
local thistype k = thistype.allocate()
set k.action = KEY_LOOSE
set k.ticks = 0
return k
endmethod
method Newpress takes nothing returns boolean
if .action == KEY_NEWPRESS then
return true
endif
return false
endmethod
method Doublepress takes nothing returns boolean
if .action == KEY_DOUBLEPRESS then
return true
endif
return false
endmethod
method Held takes nothing returns boolean
if .action == KEY_NEWPRESS or .action == KEY_DOUBLEPRESS or .action == KEY_HELD or .action == KEY_DOUBLEHELD then
return true
endif
return false
endmethod
method Doubleheld takes nothing returns boolean
if .action == KEY_DOUBLEHELD then
return true
endif
return false
endmethod
method Released takes nothing returns boolean
if .action == KEY_RELEASED then
return true
endif
return false
endmethod
method Update takes nothing returns nothing
if .action == KEY_NEWPRESS then
if .ticks == 0 then
set .ticks = 1
set .action = KEY_HELD
elseif .ticks < DOUBLEPRESS_TICKLIMIT then
set .action = KEY_DOUBLEPRESS
endif
elseif .action == KEY_DOUBLEPRESS then
set .ticks = 0
set .action = KEY_DOUBLEHELD
elseif .action == KEY_RELEASED then
set .action = KEY_LOOSE
endif
if .ticks>0 then
set .ticks = .ticks+1
endif
if .ticks>DOUBLEPRESS_TICKLIMIT then
set .ticks = 0
endif
endmethod
endstruct
struct ARROWS
KEY U
KEY D
KEY L
KEY R
static method create takes nothing returns thistype
local thistype a = thistype.allocate()
set a.U = KEY.create()
set a.D = KEY.create()
set a.L = KEY.create()
set a.R = KEY.create()
return a
endmethod
method Update takes nothing returns nothing
call .U.Update()
call .D.Update()
call .L.Update()
call .R.Update()
endmethod
endstruct
globals
ARROWS array Pad[12]
endglobals
//! textmacro ArrowFunc takes KEY
private function PressArrow$KEY$ takes nothing returns nothing
set Pad[GetPlayerId(GetEventDetectingPlayer())].$KEY$.action = KEY_NEWPRESS
endfunction
private function ReleaseArrow$KEY$ takes nothing returns nothing
set Pad[GetPlayerId(GetEventDetectingPlayer())].$KEY$.action = KEY_RELEASED
endfunction
//! endtextmacro
//! runtextmacro ArrowFunc("U")
//! runtextmacro ArrowFunc("D")
//! runtextmacro ArrowFunc("L")
//! runtextmacro ArrowFunc("R")
private function Init takes nothing returns nothing
local integer i = 0
local trigger PressU = CreateTrigger()
local trigger PressD = CreateTrigger()
local trigger PressL = CreateTrigger()
local trigger PressR = CreateTrigger()
local trigger ReleaseU = CreateTrigger()
local trigger ReleaseD = CreateTrigger()
local trigger ReleaseL = CreateTrigger()
local trigger ReleaseR = CreateTrigger()
set i = 0
loop
call TriggerRegisterPlayerEvent(PressU, Player(i), EVENT_PLAYER_ARROW_UP_DOWN)
call TriggerRegisterPlayerEvent(PressD, Player(i), EVENT_PLAYER_ARROW_DOWN_DOWN)
call TriggerRegisterPlayerEvent(PressL, Player(i), EVENT_PLAYER_ARROW_LEFT_DOWN)
call TriggerRegisterPlayerEvent(PressR, Player(i), EVENT_PLAYER_ARROW_RIGHT_DOWN)
call TriggerRegisterPlayerEvent(ReleaseU, Player(i), EVENT_PLAYER_ARROW_UP_UP)
call TriggerRegisterPlayerEvent(ReleaseD, Player(i), EVENT_PLAYER_ARROW_DOWN_UP)
call TriggerRegisterPlayerEvent(ReleaseL, Player(i), EVENT_PLAYER_ARROW_LEFT_UP)
call TriggerRegisterPlayerEvent(ReleaseR, Player(i), EVENT_PLAYER_ARROW_RIGHT_UP)
set Pad<i> = ARROWS.create()
set i = i+1
exitwhen i == bj_MAX_PLAYERS or i == Pad.size
endloop
call TriggerAddAction(PressU, function PressArrowU)
call TriggerAddAction(PressD, function PressArrowD)
call TriggerAddAction(PressL, function PressArrowL)
call TriggerAddAction(PressR, function PressArrowR)
call TriggerAddAction(ReleaseU, function ReleaseArrowU)
call TriggerAddAction(ReleaseD, function ReleaseArrowD)
call TriggerAddAction(ReleaseL, function ReleaseArrowL)
call TriggerAddAction(ReleaseR, function ReleaseArrowR)
endfunction
endlibrary
</i>
This system is a time saver with an easy way to accomplish things involving keypresses
An extensive example of how you could use it:
JASS:
scope MoveHeroes initializer Init
globals
private unit array heroes[3]
private constant string SPECIAL_EFFECT = "Abilities\\Spells\\Human\\Polymorph\\PolyMorphTarget.mdl"
private constant real MOVESPEED = 8
private constant real TURNSPEED = 8
endglobals
private function MoveHero takes nothing returns boolean
local integer i = 0
local real a
loop
if heroes<i>!=null then
set a = GetUnitFacing(heroes<i>)*bj_DEGTORAD
//Up key
if Pad<i>.U.Doublepress() then
call DestroyEffect(AddSpecialEffect(SPECIAL_EFFECT, GetUnitX(heroes<i>), GetUnitY(heroes<i>)))
call DisplayTextToPlayer(Player(i), 0, 0, "SPRINTING!")
endif
if Pad<i>.U.Doubleheld() then
call SetUnitPosition(heroes<i>, GetUnitX(heroes<i>)+Cos(a)*MOVESPEED*3, GetUnitY(heroes<i>)+Sin(a)*MOVESPEED*3)
elseif Pad<i>.U.Held() then
call SetUnitPosition(heroes<i>, GetUnitX(heroes<i>)+Cos(a)*MOVESPEED, GetUnitY(heroes<i>)+Sin(a)*MOVESPEED)
endif
//Down key
if Pad<i>.D.Newpress() then
call DestroyEffect(AddSpecialEffect(SPECIAL_EFFECT, GetUnitX(heroes<i>), GetUnitY(heroes<i>)))
elseif Pad<i>.D.Held() then
call SetUnitPosition(heroes<i>, GetUnitX(heroes<i>)-Cos(a)*MOVESPEED, GetUnitY(heroes<i>)-Sin(a)*MOVESPEED)
elseif Pad<i>.D.Released() then
call DestroyEffect(AddSpecialEffect(SPECIAL_EFFECT, GetUnitX(heroes<i>), GetUnitY(heroes<i>)))
endif
//Left key
if Pad<i>.L.Doubleheld() then
call SetUnitFacing(heroes<i>, GetUnitFacing(heroes<i>)+TURNSPEED*3)
elseif Pad<i>.L.Held() then
call SetUnitFacing(heroes<i>, GetUnitFacing(heroes<i>)+TURNSPEED)
endif
//Right key
if Pad<i>.R.Doubleheld() then
call SetUnitFacing(heroes<i>, GetUnitFacing(heroes<i>)-TURNSPEED*3)
elseif Pad<i>.R.Held() then
call SetUnitFacing(heroes<i>, GetUnitFacing(heroes<i>)-TURNSPEED)
endif
//instant spin
if Pad<i>.L.Doublepress() or Pad<i>.R.Doublepress() then
call DisplayTextToPlayer(Player(i), 0, 0, "Superturnspeed!")
endif
//Camera
if Player(i) == GetLocalPlayer() then
call SetCameraTargetController(heroes<i>, 0, 0, true)
call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, 1100, 0)
call SetCameraField(CAMERA_FIELD_ZOFFSET, 100, 0)
call SetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK, 325, 0)
call SetCameraField(CAMERA_FIELD_ROTATION, GetUnitFacing(heroes<i>), 0.0625)
call SetCameraField(CAMERA_FIELD_FIELD_OF_VIEW, 65, 0)
endif
endif
call Pad<i>.Update()
set i = i+1
exitwhen i==heroes.size
endloop
return false
endfunction
private function Init takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterTimerEvent(t, 0.03125, true)
call TriggerAddCondition(t, Condition(function MoveHero))
set heroes[0] = gg_unit_Hpal_0001
set heroes[1] = gg_unit_Hpal_0002
set heroes[2] = gg_unit_Hpal_0003
call PauseUnit(heroes[0], true)
call PauseUnit(heroes[1], true)
call PauseUnit(heroes[2], true)
endfunction
endscope
</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>
This example is included in the demo map
Have fun!