![]() |
KTech 1.1.0
C++ 2D terminal game engine library
|
Input callbacks creator and manager. More...
#include <callbackgroup.hpp>
Public Member Functions | |
| CallbackGroup (Engine &engine, bool enabled=true) | |
Register a new CallbackGroup at Input. | |
| ~CallbackGroup () | |
Remove CallbackGroup and its callback functions from Input. | |
| void | RegisterCallback (const std::string &stringKey, const std::function< bool()> &callback) |
| Register a function to be called back when an input is received. | |
| void | RegisterRangedCallback (char start, char end, const std::function< bool()> &callback) |
| Register a function to be called back when an input within a characters range is received. | |
| void | Enable () |
| Enable registered callback functions. | |
| void | Disable () |
| Disable registered callback functions. | |
Input callbacks creator and manager.
Through this class you can register functions to get called back when certain inputs are received from the terminal (CallbackGroup::RegisterCallback() and CallbackGroup::RegisterRangedCallback()). You can also temporarily activate and deactivate your callbacks so they happen only when you want (CallbackGroup::Enable() and CallbackGroup::Disable()).
This class is basically a wrapper for a couple of internal classes and functions which are not individually documented. The way this wrapper works assures callback functions don't go into action immediately (otherwise there's unexpected behavior), and aren't removed while being processed somewhere else.
| KTech::Input::CallbackGroup::CallbackGroup | ( | Engine & | engine, |
| bool | enabled = true ) |
Register a new CallbackGroup at Input.
| [in] | engine | Parent engine. |
| [in] | enabled | Will set registered callbacks to this value by default. |
| KTech::Input::CallbackGroup::~CallbackGroup | ( | ) |
Remove CallbackGroup and its callback functions from Input.
Deconstructing a CallbackGroup also means all of its input callback functions will be permanently deregistered. This is good, because you can create a CallbackGroup instance in your class (like a moving character inherited from Object), register member functions as input callbacks, and not worry later about your character deconstructing while still having member functions registered and potentially called (because the CallbackGroup and its callback functions are deconstructed with it).
| void KTech::Input::CallbackGroup::Disable | ( | ) |
Disable registered callback functions.
The callbacks will be disabled only in the next tick. This behavior is explained in CallbackGroup::Enable()'s documentation entry.
CallbackGroup::Enable() | void KTech::Input::CallbackGroup::Enable | ( | ) |
Enable registered callback functions.
The callbacks will be enabled only in next tick. This way, for example, you don't have to worry about a sequence where:
CallbackGroup).Which is clearly not the wanted result. The desired behavior is to let the player see the next button, and have to press twice if they wish to press on both buttons.
This behavior of "update only in the next tick" applies to CallbackGroup::Disable(), CallbackGroup::RegisterCallback() and CallbackGroup::RegisterRangedCallback() as well.
| void KTech::Input::CallbackGroup::RegisterCallback | ( | const std::string & | stringKey, |
| const std::function< bool()> & | callback ) |
Register a function to be called back when an input is received.
Input is received in escape sequences. KTech::Keys includes such values that you can give to the stringKey parameter.
If your function is a callback for different inputs, you can use Input::input from within your function to find out which input called it.
If the callbacks group is currently enabled, the new callback will be enabled as well only in the next tick. This behavior is explained in CallbackGroup::Enable()'s documentation entry.
| [in] | stringKey | Escape sequence that when received calls your callback function. |
| [in] | callback | Your callback function which returns bool. The return value is explained in Output::ShouldRenderThisTick(). |
| void KTech::Input::CallbackGroup::RegisterRangedCallback | ( | char | start, |
| char | end, | ||
| const std::function< bool()> & | callback ) |
Register a function to be called back when an input within a characters range is received.
Any input received within the range will call your callback function. Because it's a range, the inputs are limited in length to 1 character. Anyway, this function is mostly useful when 1-character-long inputs are needed. For example:
You can use Input::input from within your callback function to find out which input called it.
If the callbacks group is currently enabled, the new callback will be enabled as well only in the next tick. This behavior is explained in CallbackGroup::Enable()'s documentation entry.
| [in] | start | Start of (ASCII) characters range. |
| [in] | end | End of (ASCII) characters range. |
| [in] | callback | Your callback function which returns bool. The return value is explained in Output::ShouldRenderThisTick(). |