method to cancel event propagation
This commit is contained in:
parent
bc2a4a39ad
commit
16e1cc19d0
|
@ -22,11 +22,16 @@ void Delegate::dispatch()
|
|||
{
|
||||
if (event.type == iter->first)
|
||||
{
|
||||
cancelling_propagation = false;
|
||||
for (Subscriber s : iter->second)
|
||||
{
|
||||
if (s.o->is_active())
|
||||
{
|
||||
s.f(event);
|
||||
if (cancelling_propagation)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -66,3 +71,8 @@ bool Delegate::compare_neutral(SDL_Event& event, const std::string& command)
|
|||
{
|
||||
return compare(event, command, true);
|
||||
}
|
||||
|
||||
void Delegate::cancel_propagation()
|
||||
{
|
||||
cancelling_propagation = true;
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ struct Delegate : Node
|
|||
|
||||
std::map<int, std::vector<Subscriber>> subscribers;
|
||||
static int command_event_type;
|
||||
bool cancelling_propagation = false;
|
||||
|
||||
Delegate(Node*);
|
||||
void add_subscriber(Subscriber, int);
|
||||
|
@ -31,6 +32,7 @@ struct Delegate : Node
|
|||
bool compare(SDL_Event&, const std::string& = "", bool = false, bool = false);
|
||||
bool compare_cancel(SDL_Event&, const std::string& = "");
|
||||
bool compare_neutral(SDL_Event&, const std::string& = "");
|
||||
void cancel_propagation();
|
||||
|
||||
template<typename T>
|
||||
void subscribe(void(T::*f)(SDL_Event&), T* o, int type = command_event_type)
|
||||
|
|
|
@ -44,7 +44,7 @@ void FramerateIndicator::refresh()
|
|||
}
|
||||
}
|
||||
|
||||
Game::Game()
|
||||
Game::Game() : Node()
|
||||
{
|
||||
frame_length_history.reserve(5000);
|
||||
set_framerate(get_configuration()["display"]["framerate"]);
|
||||
|
@ -178,7 +178,7 @@ void Game::load_sdl_context()
|
|||
glcontext = NULL;
|
||||
}
|
||||
if ((renderer = SDL_CreateRenderer(
|
||||
window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE)) == NULL)
|
||||
window, -1, SDL_RENDERER_TARGETTEXTURE)) == NULL)
|
||||
{
|
||||
print_sdl_error("Could not create renderer");
|
||||
flag_to_end();
|
||||
|
|
18
src/Node.cpp
18
src/Node.cpp
|
@ -34,15 +34,15 @@ nlohmann::json& Node::get_configuration()
|
|||
return get_root()->configuration.config;
|
||||
}
|
||||
|
||||
Game* Node::get_root()
|
||||
{
|
||||
Node* current = this;
|
||||
while (current->parent != NULL)
|
||||
{
|
||||
current = current->parent;
|
||||
}
|
||||
return static_cast<Game*>(current);
|
||||
}
|
||||
// Game* Node::get_root()
|
||||
// {
|
||||
// Node* current = this;
|
||||
// while (current->parent != NULL)
|
||||
// {
|
||||
// current = current->parent;
|
||||
// }
|
||||
// return static_cast<Game*>(current);
|
||||
// }
|
||||
|
||||
Delegate& Node::get_delegate()
|
||||
{
|
||||
|
|
14
src/Node.hpp
14
src/Node.hpp
|
@ -24,7 +24,6 @@ struct Node
|
|||
void activate();
|
||||
void deactivate();
|
||||
bool is_active() const;
|
||||
Game* get_root();
|
||||
nlohmann::json& get_configuration();
|
||||
Delegate& get_delegate();
|
||||
Display& get_display();
|
||||
|
@ -33,6 +32,19 @@ struct Node
|
|||
virtual std::string get_class_name() { return "Node"; };
|
||||
virtual ~Node();
|
||||
|
||||
template<typename T = Game>
|
||||
T* get_root()
|
||||
{
|
||||
if (parent == NULL)
|
||||
{
|
||||
return static_cast<T*>(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
return parent->get_root<T>();
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#include "Configuration.hpp"
|
||||
|
|
|
@ -15,11 +15,6 @@ Sprite::Sprite(Node* parent, std::string path) : Sprite(parent)
|
|||
associate(path);
|
||||
}
|
||||
|
||||
void Sprite::set_frame_length(float length)
|
||||
{
|
||||
frame_animation.set_frame_length(length);
|
||||
}
|
||||
|
||||
void Sprite::associate(std::string path)
|
||||
{
|
||||
if (fs::is_regular_file(path))
|
||||
|
@ -97,11 +92,12 @@ Frameset& Sprite::add_frameset(std::string name)
|
|||
return framesets[name];
|
||||
}
|
||||
|
||||
void Sprite::set_frameset(std::string name)
|
||||
Frameset& Sprite::set_frameset(std::string name)
|
||||
{
|
||||
current_frameset_name = name;
|
||||
frame_animation.set_frame_length(get_current_frameset().get_frame_length());
|
||||
update_size();
|
||||
return get_current_frameset();
|
||||
}
|
||||
|
||||
Frameset& Sprite::get_current_frameset()
|
||||
|
|
|
@ -38,14 +38,13 @@ struct Sprite : Node
|
|||
Sprite();
|
||||
Sprite(Node*);
|
||||
Sprite(Node*, std::string);
|
||||
void set_frame_length(float);
|
||||
void associate(std::string);
|
||||
void load();
|
||||
void load_file(fs::path);
|
||||
void add_frame(SDL_Texture*);
|
||||
Frameset& get_all_frames_frameset();
|
||||
Frameset& add_frameset(std::string);
|
||||
void set_frameset(std::string);
|
||||
Frameset& set_frameset(std::string);
|
||||
Frameset& get_current_frameset();
|
||||
Box& get_box(int = 0);
|
||||
void add_box(glm::vec2, bool = false);
|
||||
|
|
|
@ -136,7 +136,6 @@ SDL_Texture* sfw::get_remapped_texture(
|
|||
}
|
||||
|
||||
#include "superxbr.cpp"
|
||||
#include "hq2x.c"
|
||||
|
||||
/*
|
||||
Base texture must be set to SDL_TEXTUREACCESS_TARGET
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
#include "glm/gtx/vector_angle.hpp"
|
||||
|
||||
#include "Box.hpp"
|
||||
#include "Color.hpp"
|
||||
#include "filesystem.hpp"
|
||||
|
||||
namespace sfw
|
||||
|
|
Loading…
Reference in New Issue