KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
KTech::Input::CallbackGroup Class Reference

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.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ CallbackGroup()

KTech::Input::CallbackGroup::CallbackGroup ( Engine & engine,
bool enabled = true )

Register a new CallbackGroup at Input.

Parameters
[in]engineParent engine.
[in]enabledWill set registered callbacks to this value by default.

◆ ~CallbackGroup()

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).

Member Function Documentation

◆ Disable()

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.

See also
CallbackGroup::Enable()

◆ 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:

  1. button1 shows and selects button2 (enableing button2's CallbackGroup).
  2. button2 being immediately pressed as well (because both buttons are pressed using the same key).

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.

◆ RegisterCallback()

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.

Parameters
[in]stringKeyEscape sequence that when received calls your callback function.
[in]callbackYour callback function which returns bool. The return value is explained in Output::ShouldRenderThisTick().
See also
KTech::Keys
Output::ShouldRenderThisTick()

◆ RegisterRangedCallback()

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:

bool LogLetters()
{
Output::Log(Input::input, RGBColors::green);
return true;
}
//...
myCallbackGroup.RegisterRangedCallback('a', 'z', LogLetters); // Notice that the inputs `a`-`z` don't include `A`-`sZ`
myCallbackGroup.RegisterRangedCallback('A', 'Z', LogLetters);
//...
std::string input
Input for the last-called callback function.
Definition input.hpp:48
static void Log(const std::string &text, RGB color)
Print colored log text to the terminal.
Definition output.cpp:43

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.

Parameters
[in]startStart of (ASCII) characters range.
[in]endEnd of (ASCII) characters range.
[in]callbackYour callback function which returns bool. The return value is explained in Output::ShouldRenderThisTick().
See also
CallbackGroup::Enable()
Output::ShouldRenderThisTick()

The documentation for this class was generated from the following files: