Skip to content

Commit

Permalink
added Callback Functions Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
benstyle11 committed Dec 20, 2021
1 parent c099bf2 commit b277036
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
13 changes: 10 additions & 3 deletions include/entity/strategies/controllable.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#define CONTROLLABLE_H
#include "strategy.h"
#include <SFML/Window.hpp>
#include <functional>
#include <string>
#include <vector>
#include <map>

#define NB_KEYS 256

Expand All @@ -10,14 +14,18 @@
class Controllable : public virtual Strategy
{
private:

std::vector<std::pair<std::vector<unsigned short>,std::vector<std::function<void()>> > > callBacks;

public:
Controllable(/* args */);
~Controllable();

void flush(unsigned short[NB_KEYS] inputs); //flush ? events ?, receive an array of boolean (flags) representing if some keys are pressed,
void flush(std::vector<unsigned short> &inputs); //flush ? events ?, receive an array of boolean (flags) representing if some keys are pressed,
// WARNIGN needs a convention (between conductor.ControllableHandler and this class) on that boolean array

void addCallBack(std::vector<unsigned short> inputs, std::function<void()> callback); // These take effects immediatly ( independant of the MAE )


};

Controllable::Controllable(/* args */)
Expand All @@ -33,5 +41,4 @@ Controllable::~Controllable()




#endif
40 changes: 40 additions & 0 deletions src/entity/strategies/controllable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "controllable.h"




void Controllable::addCallBack(std::vector<unsigned short> inputs, std::function<void()> callback){

for (int i = 0; i < callBacks.size(); i++)
{
if(callBacks[i].first == inputs){ /// adds the callback to the callBacks Vector
callBacks[i].second.emplace_back(callback);
return;
}
}


std::pair<std::vector<unsigned short>, std::function<void()> > a =
std::pair<std::vector<unsigned short>, std::function<void()>>(inputs,callback);

callBacks.emplace_back(a);

}

void Controllable::flush(std::vector<unsigned short> &inputs){

// Calls all callback functions ( without MAE)
for (int i = 0; i < callBacks.size(); i++)
{
if(callBacks[i].first == inputs){
for (int j = 0; j < callBacks[i].second.size(); i++)
{
callBacks[i].second[j](); // flushes Every Callbacks
}
break;
}
}



}

0 comments on commit b277036

Please sign in to comment.