ID:2351995
 

Problem description: My problem is that I don't know how to use this library. I am also opened to other ways to implement macros.

Link to Library: http://www.byond.com/developer/Kaiochao/AnyMacro

Code:
////////////////////////////BUTTON TRACKER - KAIOCHAO/////////////////////////////////////////////
button_tracker
var
/*
Keeps track of what buttons are pressed.
You shouldn't ever need to use this in your own code.
*/

list/is_pressed

proc
/*
Press a button.
Until the button is released, IsButtonPressed returns TRUE.
Calls ButtonPressed if the button wasn't already pressed.
*/

Press(button)
if(IsReleased(button))
if(!is_pressed)
is_pressed = list()
is_pressed[button] = TRUE
Pressed(button)

/*
Release a button.
Until the button is pressed, IsButtonPressed returns FALSE.
Calls ButtonReleased if the button wasn't already released.
*/

Release(button)
if(IsPressed(button))
is_pressed -= button
Released(button)
if(!length(is_pressed))
is_pressed = null

/*
Is the button currently pressed?
Returns TRUE or FALSE.
*/

IsPressed(button)
/*
This should return TRUE or FALSE.
This seemingly overcomplicated expression
guarantees that it results in TRUE or FALSE
and not, like, null, or something.
*/

return is_pressed && is_pressed[button] || FALSE

/*
Is the button currently released (not pressed)?
Returns TRUE or FALSE.
*/

IsReleased(button)
return !IsPressed(button)

/*
Called when a button is pressed.
Does nothing by default, but handy for overriding.
*/

Pressed(button)

/*
Called when abutton is released.
Does nothing by default, but handy for overriding.
*/

Released(button)

////////////////////////////////////ANY MACRO//////////////////////////////////////////////////////////////////////////////////////

/*
This library adds button tracking for clients,
for all macros caught by the Any macro, including:
* All keyboard keys
* All gamepad face buttons, stick buttons, shoulder buttons, and triggers

Buttons are tracked using the Button Tracker from that library.

By default, the Any macro is bound for the client in /client/proc/BindAnyMacro()
using a couple calls to winset() (one for the press, one for the release).
This allows for plug-and-playability, but those calls have overhead that you can avoid
by defining those macros as .dmf or .dms macros.
If you do define the macros elsewhere, include this in your code:
client/bind_any_macro = FALSE

*/






///////////////////////////////////////////////////////////////////////////////////////////////////


button_tracker/echo
var
// The output target receives messages about changes in button states.
output_target

// The output target is received by the constructor.
New(output_target)
src.output_target = output_target

// When a button is pressed, send a message to the output target.
Pressed(button)
if(!(button in usr.hotkeys)){
return
}
//Made my abilities into verbs so I can call them here.
//My gut tells me that this is a poor attempt and needs to be
//burned
call(usr.hotkeys["[button]"])(usr)

// When a button is released, send a message to the output target.
Released(button)
if(!(button in usr.hotkeys)){
return
}
//My attempt to simulate that a player is holding down a button
if(usr.hotkeys["[button]"] == /mob/verb/Basic_Guard){
call(usr.hotkeys["[button]"])(usr)
}


client
var
tmp
/* This is a reference to the Button Tracker that keeps track of macros
caught by the Any macro. */

button_tracker/macros

/* Set this to FALSE in your code to avoid calling winset() to bind the Any macro
if you have the Any macro defined in your interface already.
Alternatively, you could override BindAnyMacro() to do nothing. */

bind_any_macro = TRUE

New()
. = ..()
macros = new
if(bind_any_macro){
BindAnyMacro()
}
macros = new/button_tracker/echo(src)

proc
BindAnyMacro(){
winset(src, "AnyMacro","parent=macro; name=Any; command=\"press-button \\\"\[\[*]]\\\"\"")
winset(src, "AnyUpMacro","parent=macro; name=Any+Up; command=\"release-button \\\"\[\[*]]\\\"\"")
}

verb
/* Call this with any button you want to track.
By default, this is called by all keyboard keys and gamepad buttons. */

press_button(button as text){
set hidden = TRUE
set instant = TRUE
if(macros){
macros.Press(button)
}
}

/* Call this with any button you want to track.
By default, this is called by all keyboard keys and gamepad buttons. */

release_button(button as text){
set hidden = TRUE
set instant = TRUE
if(macros){
macros.Release(button)
}
}


-How I am map buttons to skills
mob/proc/basicHotkeys(){
if(!hotkeys){
hotkeys = new
}
hotkeys["F"] = new/mob/verb/Primary_Attack
hotkeys["G"] = new/mob/verb/Basic_Guard
}



Im not sure i understand, but if you need to add some action like atttack on pressed "A" button. All you have to do is add a if statemaent in Pressed() proc for button_tracker/echo (in demo).

client/proc/procName()

button_tracker/echo
Pressed(button)
if(button=="a") world<<"Atack proc here"
if(button=="s")
world<<"Player client[output_target]"
output_target.procName()


but if you mean about custom macro, you can add some list with actions


//if you want 5 macros for skills like in hotkey

client
var/list/List=list("key1"="Spell","key2"="potion","key3","key4","key5")
proc/addMacro(id, action)
List["key[id]"] = action
proc/removeMacro(id)
List["key[id]"] = null

//and now in

button_tracker/echo
Pressed(button)

//spell
if(button=="1" && output_target.List["key[button]"])
world<<output_target.List["key[button]"]


//potion
if(button=="2" && output_target.List["key[button]"])
world<<output_target.List["key[button]"]

//add skill to 5th slot
if(button=="3")
output_target.addMacro(5, "Kamehameha")
world<<"kamehameha attach to 5th key"

//remove skill from 5th slot
if(button=="4")
output_target.removeMacro(5)
world<<"5th key reseted"

//use 5th slot
if(button=="5" && output_target.List["key[button]"])
world<<output_target.List["key[button]"]


have you any questions, call for more.
Thanks for responding Marekssj. If I am looking to simulate a "While button is pushed", do I just call the function/proc via "Released(button)" proc?
If you mean about check pushed button fe. You want use special attack by use shift + attack key ("a button") Just use "IsPressed("KEY")" proc.
You don't have to add any procs, it's built-in kaio lib.

button_tracker/echo

Pressed(button)

if(button=="A")
if(IsPressed("Shift"))
//Code for special attack
else
//normal attack


Do not forget about the correct form of buttons. Use capital letter. Shift, Enter etc. also