keyup sends cancel to command subscribers

This commit is contained in:
Frank DeMarco 2019-05-31 21:53:39 -04:00
parent daa7945c89
commit c71635e5d6
7 changed files with 52 additions and 31 deletions

View File

@ -129,8 +129,8 @@ struct Demo : Game
Mix_PlayMusic(music, -1);
load_gl_context();
delegate.subscribe(&Demo::respond, this);
Input* l = new Input(this);
delete l;
// Input* l = new Input(this);
// delete l;
// input.print_branch();
// mushroom.print_branch();
// Input* i = new Input(this);
@ -424,22 +424,22 @@ struct Demo : Game
roundedBoxColor(renderer, 300, 200, 500, 300, 10, 0x8f8fdfff);
aacircleColor(renderer, 300, 200, 30, 0xffef3fff);
int speed = 2;
if (up_active)
{
// grass.move(0, -speed);
}
if (right_active)
{
// grass.move(speed);
}
if (down_active)
{
// grass.move(0, speed);
}
if (left_active)
{
// grass.move(-speed, 0);
}
// if (up_active)
// {
// grass.move(0, -speed);
// }
// if (right_active)
// {
// grass.move(speed);
// }
// if (down_active)
// {
// grass.move(0, speed);
// }
// if (left_active)
// {
// grass.move(-speed, 0);
// }
// grass.update();
// mushroom.update();
SDL_RenderPresent(renderer);

View File

@ -6,7 +6,7 @@ GLEW_DIR = $(SFW_LIB_DIR)glew/
CC_LINUX = clang-7
CPPC_LINUX = clang++-7
SDLCONFIG = /home/frank/local/sdl/bin/sdl2-config
CFLAGS = -Wall -O2 -c -I$(SFW_LIB_DIR) -I$(SFW_SRC_DIR)
CFLAGS = -Wall -O0 -c -I$(SFW_LIB_DIR) -I$(SFW_SRC_DIR) -g
CPP_FLAGS = $(CFLAGS) --std=c++17
SDL_FLAGS = $(shell $(SDLCONFIG) --cflags)
LFLAGS = $(shell $(SDLCONFIG) --libs) -lpthread

View File

@ -28,11 +28,26 @@ void Delegate::dispatch()
}
}
}
if (event.type == command_event_type)
{
delete static_cast<bool*>(event.user.data2);
}
}
}
bool Delegate::compare(SDL_Event& event, std::string command)
bool Delegate::compare(SDL_Event& event, std::string command, bool neutral, bool cancel)
{
return event.type == command_event_type and
*static_cast<std::string*>(event.user.data1) == command;
(command == "" or command == *static_cast<std::string*>(event.user.data1)) and
(neutral or (cancel == *static_cast<bool*>(event.user.data2)));
}
bool Delegate::compare_cancel(SDL_Event& event, std::string command)
{
return compare(event, command, false, true);
}
bool Delegate::compare_neutral(SDL_Event& event, std::string command)
{
return compare(event, command, true);
}

View File

@ -27,7 +27,9 @@ struct Delegate : Node
Delegate(Node*);
void add_subscriber(Subscriber, int);
void dispatch();
bool compare(SDL_Event&, std::string);
bool compare(SDL_Event&, std::string = "", bool = false, bool = false);
bool compare_cancel(SDL_Event&, std::string = "");
bool compare_neutral(SDL_Event&, std::string = "");
template<typename T>
void subscribe(void(T::*f)(SDL_Event&), T* o, int type = command_event_type)

View File

@ -4,6 +4,7 @@ Input::Input(Node *parent) : Node(parent)
{
load_key_map();
get_delegate().subscribe(&Input::respond, this, SDL_KEYDOWN);
get_delegate().subscribe(&Input::respond, this, SDL_KEYUP);
for (KeyCombination& combination : key_map)
{
print_key_combination(combination);
@ -74,21 +75,23 @@ void Input::add_to_key_map(
void Input::respond(SDL_Event &event)
{
if (event.type == SDL_KEYDOWN)
{
// if (event.type == SDL_KEYDOWN)
// {
SDL_Keymod mod = SDL_GetModState();
for (KeyCombination &combination : key_map)
for (KeyCombination& combination : key_map)
{
if (event.key.keysym.sym == combination.key and
(not combination.ctrl || mod & KMOD_CTRL) and
(not combination.shift || mod & KMOD_SHIFT) and
(not combination.alt || mod & KMOD_ALT))
{
SDL_Event event;
event.type = Delegate::command_event_type;
event.user.data1 = &combination.command;
SDL_PushEvent(&event);
SDL_Event relay;
bool* cancel = new bool(event.type == SDL_KEYDOWN ? false : true);
relay.type = Delegate::command_event_type;
relay.user.data1 = &combination.command;
relay.user.data2 = cancel;
SDL_PushEvent(&relay);
}
}
}
// }
}

View File

@ -44,7 +44,7 @@ Display& Node::get_display()
void Node::print_branch()
{
Node *current = this;
Node* current = this;
while (current != NULL)
{
std::cout << current->get_class_name() << " @ " << current;

View File

@ -1,4 +1,5 @@
#include "Sprite.hpp"
#include "Game.hpp"
Sprite::Sprite(Node *parent) : Node(parent) {}