suppress input temporarily

This commit is contained in:
Frank DeMarco 2020-07-22 00:53:40 -04:00
parent 5df099354b
commit 3d439e56e0
12 changed files with 67 additions and 35 deletions

View File

@ -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 :)

View File

@ -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", "."},

View File

@ -38,7 +38,7 @@ void Delegate::dispatch()
}
}
bool Delegate::compare(SDL_Event& event, const std::vector<std::string>& commands, const bool& neutral, const bool& cancel)
bool Delegate::compare(SDL_Event& event, const std::vector<std::string>& commands, bool neutral, bool cancel)
{
for (const std::string& command : commands)
{
@ -50,7 +50,7 @@ bool Delegate::compare(SDL_Event& event, const std::vector<std::string>& 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<std::string*>(event.user.data1)) &&

View File

@ -27,8 +27,8 @@ struct Delegate : Node
Delegate(Node*);
void add_subscriber(Subscriber, int);
void dispatch();
bool compare(SDL_Event&, const std::vector<std::string>&, 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<std::string>&, 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& = "");

View File

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

View File

@ -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;

View File

@ -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<std::string>& system_any_key_ignore = get_configuration()["input"]["system-any-key-ignore-commands"];
const std::vector<std::string>& 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<std::string>& system_any_key_ignore = get_configuration()["input"]["system-any-key-ignore-commands"];
const std::vector<std::string>& 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;
}

View File

@ -10,6 +10,7 @@
#include "SDL.h"
#include "Node.hpp"
#include "Animation.hpp"
struct KeyCombination
{
@ -46,6 +47,8 @@ struct Input : Node
};
std::vector<KeyCombination> 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"; }
};

View File

@ -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;

View File

@ -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();

View File

@ -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();

View File

@ -14,6 +14,7 @@ struct Timer
void toggle();
void toggle(bool);
void reset();
float get_seconds_elapsed();
void update();
};