From 3d439e56e0b509b9df99634a48266845f02e145a Mon Sep 17 00:00:00 2001 From: Frank DeMarco Date: Wed, 22 Jul 2020 00:53:40 -0400 Subject: [PATCH] suppress input temporarily --- demo/Demo.cpp | 2 +- src/Configuration.cpp | 6 +++-- src/Delegate.cpp | 4 ++-- src/Delegate.hpp | 4 ++-- src/Game.cpp | 9 +++----- src/Game.hpp | 2 -- src/Input.cpp | 51 +++++++++++++++++++++++++++---------------- src/Input.hpp | 5 +++++ src/Node.cpp | 10 +++++++++ src/Node.hpp | 1 + src/Timer.cpp | 7 +++++- src/Timer.hpp | 1 + 12 files changed, 67 insertions(+), 35 deletions(-) diff --git a/demo/Demo.cpp b/demo/Demo.cpp index c6dda1d..4153e5c 100644 --- a/demo/Demo.cpp +++ b/demo/Demo.cpp @@ -10,7 +10,7 @@ config parameters on command line, effects chain, asset dict with metadata, move added sprite locations by offset when location is changed, gradients, level select code input, logging, variable screen resolution, debug display, - loading wheel animation, shadowed sprite + loading wheel animation, shadowed sprite, separate update and draw :) SWEATY HANDS :) OILY SNACKS :) AND BAD HYGIENE :) diff --git a/src/Configuration.cpp b/src/Configuration.cpp index 6df8f73..2c466c7 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -23,13 +23,15 @@ void Configuration::set_defaults() {"left", "left"}, {"pause", "enter"}, {"fullscreen", {"ALT", "enter"}}, - {"toggle-framerate", {"CTRL", "f"}} + {"toggle-framerate", {"CTRL", "f"}}, + {"reset", {"CTRL", "r"}} }; sys_config["input"] = { {"suppress-any-key-on-mods", true}, {"system-any-key-ignore-commands", {"fullscreen", "screenshot", "toggle-framerate", "record", "quit"}}, - {"any-key-ignore-commands", {}} + {"any-key-ignore-commands", {}}, + {"default-unsuppress-delay", 700} }; sys_config["path"] = { {"screenshots", "."}, diff --git a/src/Delegate.cpp b/src/Delegate.cpp index cd2f9ed..86111d7 100644 --- a/src/Delegate.cpp +++ b/src/Delegate.cpp @@ -38,7 +38,7 @@ void Delegate::dispatch() } } -bool Delegate::compare(SDL_Event& event, const std::vector& commands, const bool& neutral, const bool& cancel) +bool Delegate::compare(SDL_Event& event, const std::vector& commands, bool neutral, bool cancel) { for (const std::string& command : commands) { @@ -50,7 +50,7 @@ bool Delegate::compare(SDL_Event& event, const std::vector& command return false; } -bool Delegate::compare(SDL_Event& event, const std::string& command, const bool& neutral, const bool& cancel) +bool Delegate::compare(SDL_Event& event, const std::string& command, bool neutral, bool cancel) { return event.type == command_event_type && (command == "" || command == *static_cast(event.user.data1)) && diff --git a/src/Delegate.hpp b/src/Delegate.hpp index 669da66..e7920d9 100644 --- a/src/Delegate.hpp +++ b/src/Delegate.hpp @@ -27,8 +27,8 @@ struct Delegate : Node Delegate(Node*); void add_subscriber(Subscriber, int); void dispatch(); - bool compare(SDL_Event&, const std::vector&, const bool& = false, const bool& = false); - bool compare(SDL_Event&, const std::string& = "", const bool& = false, const bool& = false); + bool compare(SDL_Event&, const std::vector&, bool = false, bool = false); + 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& = ""); diff --git a/src/Game.cpp b/src/Game.cpp index b3a091d..30fbbef 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -456,23 +456,19 @@ SDL_Renderer* Game::get_renderer() void Game::run() { - + SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT); + suppress_input_temporarily(); #if defined(__EMSCRIPTEN__) - SDL_Log("using emscripten main loop"); emscripten_set_main_loop_arg(&loop, this, -1, true); - #else - SDL_Log("using standard main loop"); while (not done) { frame(SDL_GetTicks()); SDL_Delay(8); } - #endif - } void Game::frame(float ticks) @@ -492,6 +488,7 @@ void Game::frame(float ticks) // std::cout << ", last_frame_length: " << last_frame_length << " [rendering frame]"; recorder.update(); delegate.dispatch(); + get_root()->input.unsuppress_animation.update(); update(); framerate_indicator.update(); if (!is_gl_context) diff --git a/src/Game.hpp b/src/Game.hpp index 7749be7..abbc1a3 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -52,8 +52,6 @@ struct Game : Node SDL_Window* window; SDL_Renderer* renderer = NULL; SDL_GLContext glcontext = NULL; - // 768, 432 - // 864, 486 int frame_count_this_second = 0, framerate, ticks, last_frame_length; float frame_length = 1000.0 / 60.0, frame_time_overflow = 0, last_frame_timestamp, last_frame_count_timestamp, emscripten_previous_time; diff --git a/src/Input.cpp b/src/Input.cpp index 6592097..86c650a 100644 --- a/src/Input.cpp +++ b/src/Input.cpp @@ -77,30 +77,33 @@ void Input::add_to_key_map( void Input::respond(SDL_Event &event) { - SDL_Keymod mod = SDL_GetModState(); - SDL_Keycode sym = event.key.keysym.sym; - bool found_command = false, cancel = event.type != SDL_KEYDOWN, ctrl = mod & KMOD_CTRL, - shift = mod & KMOD_SHIFT, alt = mod & KMOD_ALT, - suppress_any_key = get_configuration()["input"]["suppress-any-key-on-mods"] && (ctrl || alt); - const std::vector& system_any_key_ignore = get_configuration()["input"]["system-any-key-ignore-commands"]; - const std::vector& any_key_ignore = get_configuration()["input"]["any-key-ignore-commands"]; - for (KeyCombination& combination : key_map) + if (!is_suppressed) { - if (sym == combination.key && (!combination.ctrl || ctrl) && (!combination.shift || shift) && - (!combination.alt || alt)) + SDL_Keymod mod = SDL_GetModState(); + SDL_Keycode sym = event.key.keysym.sym; + bool found_command = false, cancel = event.type != SDL_KEYDOWN, ctrl = mod & KMOD_CTRL, + shift = mod & KMOD_SHIFT, alt = mod & KMOD_ALT, + suppress_any_key = get_configuration()["input"]["suppress-any-key-on-mods"] && (ctrl || alt); + const std::vector& system_any_key_ignore = get_configuration()["input"]["system-any-key-ignore-commands"]; + const std::vector& any_key_ignore = get_configuration()["input"]["any-key-ignore-commands"]; + for (KeyCombination& combination : key_map) { - post_command(combination.command, cancel); - if (!sfw::is_in_container(system_any_key_ignore, combination.command) && !found_command && - !sfw::is_in_container(any_key_ignore, combination.command) && !suppress_any_key) + if (sym == combination.key && (!combination.ctrl || ctrl) && (!combination.shift || shift) && + (!combination.alt || alt)) { - post_command(any, cancel); + post_command(combination.command, cancel); + if (!sfw::is_in_container(system_any_key_ignore, combination.command) && !found_command && + !sfw::is_in_container(any_key_ignore, combination.command) && !suppress_any_key) + { + post_command(any, cancel); + } + found_command = true; } - found_command = true; } - } - if (!found_command && !suppress_any_key) - { - post_command(any, cancel); + if (!found_command && !suppress_any_key) + { + post_command(any, cancel); + } } } @@ -113,3 +116,13 @@ void Input::post_command(std::string& name, const bool& cancel) const relay.user.data2 = cancel_pointer; SDL_PushEvent(&relay); } + +void Input::suppress() +{ + is_suppressed = true; +} + +void Input::unsuppress() +{ + is_suppressed = false; +} diff --git a/src/Input.hpp b/src/Input.hpp index b12eab7..592aeb4 100644 --- a/src/Input.hpp +++ b/src/Input.hpp @@ -10,6 +10,7 @@ #include "SDL.h" #include "Node.hpp" +#include "Animation.hpp" struct KeyCombination { @@ -46,6 +47,8 @@ struct Input : Node }; std::vector key_map; static std::string any; + bool is_suppressed = false; + Animation unsuppress_animation = Animation(&Input::unsuppress, this); Input(Node*); void respond(SDL_Event&); @@ -55,6 +58,8 @@ struct Input : Node std::string, SDL_Keycode, bool = false, bool = false, bool = false); void print_key_combination(const KeyCombination&) const; void post_command(std::string&, const bool&) const; + void suppress(); + void unsuppress(); std::string get_class_name() { return "Input"; } }; diff --git a/src/Node.cpp b/src/Node.cpp index 139a136..4bc7d23 100644 --- a/src/Node.cpp +++ b/src/Node.cpp @@ -54,6 +54,16 @@ Display& Node::get_display() return get_root()->display; } +void Node::suppress_input_temporarily(int length) +{ + get_root()->input.suppress(); + if (length == 0) + { + length = get_configuration()["input"]["default-unsuppress-delay"]; + } + get_root()->input.unsuppress_animation.play_once(length); +} + void Node::print_branch() { Node* current = this; diff --git a/src/Node.hpp b/src/Node.hpp index f584fc2..c0eb98b 100644 --- a/src/Node.hpp +++ b/src/Node.hpp @@ -28,6 +28,7 @@ struct Node nlohmann::json& get_configuration(); Delegate& get_delegate(); Display& get_display(); + void suppress_input_temporarily(int = 0); void print_branch(); virtual std::string get_class_name() { return "Node"; }; virtual ~Node(); diff --git a/src/Timer.cpp b/src/Timer.cpp index 09b56ed..a1f44fe 100644 --- a/src/Timer.cpp +++ b/src/Timer.cpp @@ -8,7 +8,7 @@ Timer::Timer() void Timer::toggle() { - is_timing = !is_timing; + toggle(!is_timing); } void Timer::toggle(bool state) @@ -21,6 +21,11 @@ void Timer::reset() elapsed = 0; } +float Timer::get_seconds_elapsed() +{ + return elapsed / 1000; +} + void Timer::update() { ticks = SDL_GetTicks(); diff --git a/src/Timer.hpp b/src/Timer.hpp index 6f741f1..b16ec51 100644 --- a/src/Timer.hpp +++ b/src/Timer.hpp @@ -14,6 +14,7 @@ struct Timer void toggle(); void toggle(bool); void reset(); + float get_seconds_elapsed(); void update(); };