input name refactoring

This commit is contained in:
2018-09-26 22:32:16 +07:00
parent 20c14603ff
commit aabd9bc618
9 changed files with 398 additions and 465 deletions

View File

@@ -28,143 +28,7 @@ namespace e2d
namespace e2d
{
enum class key : u16 {
_0,
_1,
_2,
_3,
_4,
_5,
_6,
_7,
_8,
_9,
a,
b,
c,
d,
e,
f,
g,
h,
i,
j,
k,
l,
m,
n,
o,
p,
q,
r,
s,
t,
u,
v,
w,
x,
y,
z,
f1,
f2,
f3,
f4,
f5,
f6,
f7,
f8,
f9,
f10,
f11,
f12,
f13,
f14,
f15,
f16,
f17,
f18,
f19,
f20,
f21,
f22,
f23,
f24,
f25,
minus,
equal,
backspace,
section_sign,
grave_accent,
lbracket,
rbracket,
semicolon,
apostrophe,
backslash,
comma,
period,
slash,
escape,
tab,
caps_lock,
space,
enter,
lshift,
rshift,
lcontrol,
rcontrol,
lalt,
ralt,
lsuper,
rsuper,
menu,
print_screen,
scroll_lock,
pause,
insert,
del,
home,
end,
page_up,
page_down,
left,
up,
right,
down,
kp_0,
kp_1,
kp_2,
kp_3,
kp_4,
kp_5,
kp_6,
kp_7,
kp_8,
kp_9,
kp_num_lock,
kp_divide,
kp_multiply,
kp_subtract,
kp_add,
kp_enter,
kp_equal,
kp_decimal,
unknown
};
enum class mouse : u8 {
enum class mouse_button : u8 {
left,
right,
middle,
@@ -173,21 +37,59 @@ namespace e2d
unknown
};
enum class key_action : u8 {
enum class keyboard_key : u16 {
_0, _1, _2, _3, _4, _5, _6, _7, _8, _9,
a, b, c, d, e, f, g, h, i, j, k, l, m,
n, o, p, q, r, s, t, u, v, w, x, y, z,
f1, f2, f3, f4, f5,
f6, f7, f8, f9, f10,
f11, f12, f13, f14, f15,
f16, f17, f18, f19, f20,
f21, f22, f23, f24, f25,
minus, equal, backspace, section_sign, grave_accent,
lbracket, rbracket, semicolon, apostrophe, backslash,
comma, period, slash,
escape, tab, caps_lock, space, enter,
lshift, rshift, lcontrol, rcontrol,
lalt, ralt, lsuper, rsuper, menu,
print_screen, scroll_lock, pause,
insert, del, home, end, page_up, page_down,
left, up, right, down,
kp_0, kp_1, kp_2, kp_3, kp_4,
kp_5, kp_6, kp_7, kp_8, kp_9,
kp_num_lock, kp_divide, kp_multiply, kp_subtract,
kp_add, kp_enter, kp_equal, kp_decimal,
unknown
};
enum class mouse_button_action : u8 {
press,
release,
unknown
};
enum class keyboard_key_action : u8 {
press,
repeat,
release,
unknown
};
enum class mouse_action : u8 {
press,
release,
unknown
};
const char* key_to_cstr(key key) noexcept;
const char* mouse_to_cstr(mouse mouse) noexcept;
const char* key_action_to_cstr(key_action action) noexcept;
const char* mouse_action_to_cstr(mouse_action action) noexcept;
const char* mouse_button_to_cstr(mouse_button btn) noexcept;
const char* keyboard_key_to_cstr(keyboard_key key) noexcept;
const char* mouse_button_action_to_cstr(mouse_button_action action) noexcept;
const char* keyboard_key_action_to_cstr(keyboard_key_action action) noexcept;
}

View File

@@ -23,29 +23,33 @@ namespace e2d
input();
~input() noexcept;
bool any_key_up() const noexcept;
bool any_key_down() const noexcept;
bool any_key_downed() const noexcept;
bool is_any_mouse_button_pressed() const noexcept;
bool is_any_mouse_button_just_pressed() const noexcept;
bool is_any_mouse_button_just_released() const noexcept;
bool any_mouse_up() const noexcept;
bool any_mouse_down() const noexcept;
bool any_mouse_downed() const noexcept;
bool is_any_keyboard_key_pressed() const noexcept;
bool is_any_keyboard_key_just_pressed() const noexcept;
bool is_any_keyboard_key_just_released() const noexcept;
bool key_up(key key) const noexcept;
bool key_down(key key) const noexcept;
bool key_downed(key key) const noexcept;
bool is_mouse_button_pressed(mouse_button btn) const noexcept;
bool is_mouse_button_just_pressed(mouse_button btn) const noexcept;
bool is_mouse_button_just_released(mouse_button btn) const noexcept;
bool mouse_up(mouse mouse) const noexcept;
bool mouse_down(mouse mouse) const noexcept;
bool mouse_downed(mouse mouse) const noexcept;
bool is_keyboard_key_pressed(keyboard_key key) const noexcept;
bool is_keyboard_key_just_pressed(keyboard_key key) const noexcept;
bool is_keyboard_key_just_released(keyboard_key key) const noexcept;
void extract_up_keys(std::vector<key>& result) const;
void extract_down_keys(std::vector<key>& result) const;
void extract_downed_keys(std::vector<key>& result) const;
void extract_pressed_mouse_buttons(std::vector<mouse_button>& dst) const;
void extract_just_pressed_mouse_buttons(std::vector<mouse_button>& dst) const;
void extract_just_released_mouse_buttons(std::vector<mouse_button>& dst) const;
void extract_pressed_keyboard_keys(std::vector<keyboard_key>& dst) const;
void extract_just_pressed_keyboard_keys(std::vector<keyboard_key>& dst) const;
void extract_just_released_keyboard_keys(std::vector<keyboard_key>& dst) const;
void frame_tick() noexcept;
void post_event(key key, key_action action) noexcept;
void post_event(mouse mouse, mouse_action action) noexcept;
void post_event(keyboard_key key, keyboard_key_action act) noexcept;
void post_event(mouse_button btn, mouse_button_action act) noexcept;
private:
class state;
std::unique_ptr<state> state_;
@@ -54,8 +58,8 @@ namespace e2d
class window_input_source : public window::event_listener {
public:
window_input_source(input& input) noexcept;
void on_key(key key, u32 scancode, key_action action) noexcept final;
void on_mouse(mouse mouse, mouse_action action) noexcept final;
void on_mouse_button(mouse_button btn, mouse_button_action act) noexcept final;
void on_keyboard_key(keyboard_key key, u32 scancode, keyboard_key_action act) noexcept final;
private:
input& input_;
};

View File

@@ -22,14 +22,14 @@ namespace e2d
class event_listener : private e2d::noncopyable {
public:
virtual ~event_listener() noexcept = default;
virtual void on_key(key key, u32 scancode, key_action action) noexcept;
virtual void on_uchar(char32_t uchar) noexcept;
virtual void on_scroll(const v2f& delta) noexcept;
virtual void on_cursor(const v2f& position) noexcept;
virtual void on_mouse(mouse mouse, mouse_action action) noexcept;
virtual void on_close() noexcept;
virtual void on_focus(bool focused) noexcept;
virtual void on_minimize(bool minimized) noexcept;
virtual void on_move_cursor(const v2f& pos) noexcept;
virtual void on_input_char(char32_t uchar) noexcept;
virtual void on_mouse_button(mouse_button btn, mouse_button_action act) noexcept;
virtual void on_keyboard_key(keyboard_key key, u32 scancode, keyboard_key_action act) noexcept;
virtual void on_window_close() noexcept;
virtual void on_window_focus(bool focused) noexcept;
virtual void on_window_minimize(bool minimized) noexcept;
};
using event_listener_uptr = std::unique_ptr<event_listener>;
public:
@@ -76,14 +76,14 @@ namespace e2d
class window_trace_event_listener final : public window::event_listener {
public:
window_trace_event_listener(debug& debug) noexcept;
void on_key(key key, u32 scancode, key_action action) noexcept final;
void on_uchar(char32_t uchar) noexcept final;
void on_scroll(const v2f& delta) noexcept final;
void on_cursor(const v2f& position) noexcept final;
void on_mouse(mouse mouse, mouse_action action) noexcept final;
void on_close() noexcept final;
void on_focus(bool focused) noexcept final;
void on_minimize(bool minimized) noexcept final;
void on_move_cursor(const v2f& pos) noexcept final;
void on_input_char(char32_t uchar) noexcept final;
void on_mouse_button(mouse_button btn, mouse_button_action act) noexcept final;
void on_keyboard_key(keyboard_key key, u32 scancode, keyboard_key_action act) noexcept final;
void on_window_close() noexcept final;
void on_window_focus(bool focused) noexcept final;
void on_window_minimize(bool minimized) noexcept final;
private:
debug& debug_;
};

View File

@@ -11,18 +11,17 @@ int main() {
input& i = modules::initialize<input>();
debug& d = modules::initialize<debug>();
window& w = modules::initialize<window>(
v2u{640, 480}, "Enduro2D", false, false);
v2u{640, 480}, "Enduro2D", true, false);
d.add_sink<debug_console_sink>();
w.register_event_listener<window_input_source>(i);
w.register_event_listener<window_trace_event_listener>(d);
the<debug>()
.trace("SAMPLE: window real size: %0", w.real_size())
d.trace("SAMPLE: window real size: %0", w.real_size())
.trace("SAMPLE: window virtual size: %0", w.virtual_size())
.trace("SAMPLE: window framebuffer size: %0", w.framebuffer_size());
while ( !w.should_close() && !i.key_up(key::escape) ) {
while ( !w.should_close() && !i.is_keyboard_key_just_released(keyboard_key::escape) ) {
i.frame_tick();
w.swap_buffers();
window::frame_tick();

View File

@@ -11,18 +11,17 @@ int main() {
input& i = modules::initialize<input>();
debug& d = modules::initialize<debug>();
window& w = modules::initialize<window>(
v2u{640, 480}, "Enduro2D", false, false);
v2u{640, 480}, "Enduro2D", true, false);
d.add_sink<debug_console_sink>();
w.register_event_listener<window_input_source>(i);
w.register_event_listener<window_trace_event_listener>(d);
the<debug>()
.trace("SAMPLE: window real size: %0", w.real_size())
d.trace("SAMPLE: window real size: %0", w.real_size())
.trace("SAMPLE: window virtual size: %0", w.virtual_size())
.trace("SAMPLE: window framebuffer size: %0", w.framebuffer_size());
while ( !w.should_close() && !i.key_up(key::escape) ) {
while ( !w.should_close() && !i.is_keyboard_key_just_released(keyboard_key::escape) ) {
i.frame_tick();
w.swap_buffers();
window::frame_tick();

View File

@@ -8,9 +8,25 @@
namespace e2d
{
const char* key_to_cstr(key k) noexcept {
#define DEFINE_CASE(x) case key::x: return #x
switch ( k ) {
const char* mouse_button_to_cstr(mouse_button btn) noexcept {
#define DEFINE_CASE(x) case mouse_button::x: return #x
switch ( btn ) {
DEFINE_CASE(left);
DEFINE_CASE(right);
DEFINE_CASE(middle);
DEFINE_CASE(x1);
DEFINE_CASE(x2);
DEFINE_CASE(unknown);
default:
E2D_ASSERT_MSG(false, "unexpected mouse button");
return "";
}
#undef DEFINE_CASE
}
const char* keyboard_key_to_cstr(keyboard_key key) noexcept {
#define DEFINE_CASE(x) case keyboard_key::x: return #x
switch ( key ) {
DEFINE_CASE(_0);
DEFINE_CASE(_1);
DEFINE_CASE(_2);
@@ -145,50 +161,34 @@ namespace e2d
DEFINE_CASE(unknown);
default:
E2D_ASSERT_MSG(false, "unexpected key");
E2D_ASSERT_MSG(false, "unexpected keyboard key");
return "";
}
#undef DEFINE_CASE
}
const char* mouse_to_cstr(mouse m) noexcept {
#define DEFINE_CASE(x) case mouse::x: return #x
switch ( m ) {
DEFINE_CASE(left);
DEFINE_CASE(right);
DEFINE_CASE(middle);
DEFINE_CASE(x1);
DEFINE_CASE(x2);
const char* mouse_button_action_to_cstr(mouse_button_action action) noexcept {
#define DEFINE_CASE(x) case mouse_button_action::x: return #x
switch ( action ) {
DEFINE_CASE(press);
DEFINE_CASE(release);
DEFINE_CASE(unknown);
default:
E2D_ASSERT_MSG(false, "unexpected mouse");
E2D_ASSERT_MSG(false, "unexpected mouse button action");
return "";
}
#undef DEFINE_CASE
}
const char* key_action_to_cstr(key_action ka) noexcept {
#define DEFINE_CASE(x) case key_action::x: return #x
switch ( ka ) {
const char* keyboard_key_action_to_cstr(keyboard_key_action action) noexcept {
#define DEFINE_CASE(x) case keyboard_key_action::x: return #x
switch ( action ) {
DEFINE_CASE(press);
DEFINE_CASE(repeat);
DEFINE_CASE(release);
DEFINE_CASE(unknown);
default:
E2D_ASSERT_MSG(false, "unexpected key action");
return "";
}
#undef DEFINE_CASE
}
const char* mouse_action_to_cstr(mouse_action ka) noexcept {
#define DEFINE_CASE(x) case mouse_action::x: return #x
switch ( ka ) {
DEFINE_CASE(press);
DEFINE_CASE(release);
DEFINE_CASE(unknown);
default:
E2D_ASSERT_MSG(false, "unexpected mouse action");
E2D_ASSERT_MSG(false, "unexpected keyboard key action");
return "";
}
#undef DEFINE_CASE

View File

@@ -23,44 +23,44 @@ namespace e2d
class input::state final : private e2d::noncopyable {
public:
enum class button_state : u8 {
none,
press,
pressed,
release
released,
just_pressed,
just_released
};
mutable std::mutex mutex;
std::array<button_state, enum_to_number(key::unknown) + 1> key_button_states;
std::array<button_state, enum_to_number(mouse::unknown) + 1> mouse_button_states;
std::array<button_state, enum_to_number(keyboard_key::unknown) + 1> key_button_states;
std::array<button_state, enum_to_number(mouse_button::unknown) + 1> mouse_button_states;
char _pad[2];
public:
state() noexcept {
std::fill(
key_button_states.begin(),
key_button_states.end(),
button_state::none);
button_state::released);
std::fill(
mouse_button_states.begin(),
mouse_button_states.end(),
button_state::none);
button_state::released);
}
std::size_t key_button_index(key key) const noexcept {
std::size_t mouse_button_index(mouse_button btn) const noexcept {
const auto index = enum_to_number(btn);
E2D_ASSERT(index < mouse_button_states.size());
return math::numeric_cast<std::size_t>(index);
}
std::size_t keyboard_key_index(keyboard_key key) const noexcept {
const auto index = enum_to_number(key);
E2D_ASSERT(index < key_button_states.size());
return math::numeric_cast<std::size_t>(index);
}
std::size_t mouse_button_index(mouse mouse) const noexcept {
const auto index = enum_to_number(mouse);
E2D_ASSERT(index < mouse_button_states.size());
return math::numeric_cast<std::size_t>(index);
}
static void update_button_state(enum button_state& state) noexcept {
if ( state == button_state::press ) {
if ( state == button_state::just_pressed ) {
state = button_state::pressed;
} else if ( state == button_state::release ) {
state = button_state::none;
} else if ( state == button_state::just_released ) {
state = button_state::released;
}
}
};
@@ -73,135 +73,168 @@ namespace e2d
: state_(new state()) {}
input::~input() noexcept = default;
bool input::any_key_up() const noexcept {
bool input::is_any_mouse_button_pressed() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return std::any_of(
state_->key_button_states.cbegin(),
state_->key_button_states.cend(),
state_->mouse_button_states.cbegin(),
state_->mouse_button_states.cend(),
[](state::button_state s) noexcept {
return s == state::button_state::release;
});
}
bool input::any_key_down() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return std::any_of(
state_->key_button_states.cbegin(),
state_->key_button_states.cend(),
[](state::button_state s) noexcept {
return s == state::button_state::press;
});
}
bool input::any_key_downed() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return std::any_of(
state_->key_button_states.cbegin(),
state_->key_button_states.cend(),
[](state::button_state s) noexcept {
return s == state::button_state::press
return s == state::button_state::just_pressed
|| s == state::button_state::pressed;
});
}
bool input::any_mouse_up() const noexcept {
bool input::is_any_mouse_button_just_pressed() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return std::any_of(
state_->mouse_button_states.cbegin(),
state_->mouse_button_states.cend(),
[](state::button_state s) noexcept {
return s == state::button_state::release;
return s == state::button_state::just_pressed;
});
}
bool input::any_mouse_down() const noexcept {
bool input::is_any_mouse_button_just_released() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return std::any_of(
state_->mouse_button_states.cbegin(),
state_->mouse_button_states.cend(),
[](state::button_state s) noexcept {
return s == state::button_state::press;
return s == state::button_state::just_released;
});
}
bool input::any_mouse_downed() const noexcept {
bool input::is_any_keyboard_key_pressed() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return std::any_of(
state_->mouse_button_states.cbegin(),
state_->mouse_button_states.cend(),
state_->key_button_states.cbegin(),
state_->key_button_states.cend(),
[](state::button_state s) noexcept {
return s == state::button_state::press
return s == state::button_state::just_pressed
|| s == state::button_state::pressed;
});
}
bool input::key_up(key key) const noexcept {
bool input::is_any_keyboard_key_just_pressed() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
const std::size_t index = state_->key_button_index(key);
const enum state::button_state ks = state_->key_button_states[index];
return ks == state::button_state::release;
return std::any_of(
state_->key_button_states.cbegin(),
state_->key_button_states.cend(),
[](state::button_state s) noexcept {
return s == state::button_state::just_pressed;
});
}
bool input::key_down(key key) const noexcept {
bool input::is_any_keyboard_key_just_released() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
const std::size_t index = state_->key_button_index(key);
const enum state::button_state ks = state_->key_button_states[index];
return ks == state::button_state::press;
return std::any_of(
state_->key_button_states.cbegin(),
state_->key_button_states.cend(),
[](state::button_state s) noexcept {
return s == state::button_state::just_released;
});
}
bool input::key_downed(key key) const noexcept {
bool input::is_mouse_button_pressed(mouse_button btn) const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
const std::size_t index = state_->key_button_index(key);
const enum state::button_state ks = state_->key_button_states[index];
return ks == state::button_state::press
|| ks == state::button_state::pressed;
}
bool input::mouse_up(mouse mouse) const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
const std::size_t index = state_->mouse_button_index(mouse);
const std::size_t index = state_->mouse_button_index(btn);
const enum state::button_state ms = state_->mouse_button_states[index];
return ms == state::button_state::release;
}
bool input::mouse_down(mouse mouse) const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
const std::size_t index = state_->mouse_button_index(mouse);
const enum state::button_state ms = state_->mouse_button_states[index];
return ms == state::button_state::press;
}
bool input::mouse_downed(mouse mouse) const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
const std::size_t index = state_->mouse_button_index(mouse);
const enum state::button_state ms = state_->mouse_button_states[index];
return ms == state::button_state::press
return ms == state::button_state::just_pressed
|| ms == state::button_state::pressed;
}
void input::extract_up_keys(std::vector<key>& result) const {
for ( std::size_t i = 0; i < state_->key_button_states.size(); ++i ) {
state::button_state ks = state_->key_button_states[i];
if ( ks == state::button_state::release ) {
result.push_back(static_cast<key>(i));
bool input::is_mouse_button_just_pressed(mouse_button btn) const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
const std::size_t index = state_->mouse_button_index(btn);
const enum state::button_state ms = state_->mouse_button_states[index];
return ms == state::button_state::just_pressed;
}
bool input::is_mouse_button_just_released(mouse_button btn) const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
const std::size_t index = state_->mouse_button_index(btn);
const enum state::button_state ms = state_->mouse_button_states[index];
return ms == state::button_state::just_released;
}
bool input::is_keyboard_key_pressed(keyboard_key key) const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
const std::size_t index = state_->keyboard_key_index(key);
const enum state::button_state ks = state_->key_button_states[index];
return ks == state::button_state::just_pressed
|| ks == state::button_state::pressed;
}
bool input::is_keyboard_key_just_pressed(keyboard_key key) const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
const std::size_t index = state_->keyboard_key_index(key);
const enum state::button_state ks = state_->key_button_states[index];
return ks == state::button_state::just_pressed;
}
bool input::is_keyboard_key_just_released(keyboard_key key) const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
const std::size_t index = state_->keyboard_key_index(key);
const enum state::button_state ks = state_->key_button_states[index];
return ks == state::button_state::just_released;
}
void input::extract_pressed_mouse_buttons(std::vector<mouse_button>& dst) const {
std::lock_guard<std::mutex> guard(state_->mutex);
for ( std::size_t i = 0; i < state_->mouse_button_states.size(); ++i ) {
state::button_state ks = state_->mouse_button_states[i];
if ( ks == state::button_state::just_pressed || ks == state::button_state::pressed ) {
dst.push_back(static_cast<mouse_button>(i));
}
}
}
void input::extract_down_keys(std::vector<key>& result) const {
for ( std::size_t i = 0; i < state_->key_button_states.size(); ++i ) {
state::button_state ks = state_->key_button_states[i];
if ( ks == state::button_state::press ) {
result.push_back(static_cast<key>(i));
void input::extract_just_pressed_mouse_buttons(std::vector<mouse_button>& dst) const {
std::lock_guard<std::mutex> guard(state_->mutex);
for ( std::size_t i = 0; i < state_->mouse_button_states.size(); ++i ) {
state::button_state ks = state_->mouse_button_states[i];
if ( ks == state::button_state::just_pressed ) {
dst.push_back(static_cast<mouse_button>(i));
}
}
}
void input::extract_downed_keys(std::vector<key>& result) const {
void input::extract_just_released_mouse_buttons(std::vector<mouse_button>& dst) const {
std::lock_guard<std::mutex> guard(state_->mutex);
for ( std::size_t i = 0; i < state_->mouse_button_states.size(); ++i ) {
state::button_state ks = state_->mouse_button_states[i];
if ( ks == state::button_state::just_released ) {
dst.push_back(static_cast<mouse_button>(i));
}
}
}
void input::extract_pressed_keyboard_keys(std::vector<keyboard_key>& dst) const {
std::lock_guard<std::mutex> guard(state_->mutex);
for ( std::size_t i = 0; i < state_->key_button_states.size(); ++i ) {
state::button_state ks = state_->key_button_states[i];
if ( ks == state::button_state::press || ks == state::button_state::pressed ) {
result.push_back(static_cast<key>(i));
if ( ks == state::button_state::just_pressed || ks == state::button_state::pressed ) {
dst.push_back(static_cast<keyboard_key>(i));
}
}
}
void input::extract_just_pressed_keyboard_keys(std::vector<keyboard_key>& dst) const {
std::lock_guard<std::mutex> guard(state_->mutex);
for ( std::size_t i = 0; i < state_->key_button_states.size(); ++i ) {
state::button_state ks = state_->key_button_states[i];
if ( ks == state::button_state::just_pressed ) {
dst.push_back(static_cast<keyboard_key>(i));
}
}
}
void input::extract_just_released_keyboard_keys(std::vector<keyboard_key>& dst) const {
std::lock_guard<std::mutex> guard(state_->mutex);
for ( std::size_t i = 0; i < state_->key_button_states.size(); ++i ) {
state::button_state ks = state_->key_button_states[i];
if ( ks == state::button_state::just_released ) {
dst.push_back(static_cast<keyboard_key>(i));
}
}
}
@@ -218,23 +251,23 @@ namespace e2d
&state::update_button_state);
}
void input::post_event(key key, key_action action) noexcept {
void input::post_event(keyboard_key key, keyboard_key_action action) noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
const std::size_t index = state_->key_button_index(key);
const std::size_t index = state_->keyboard_key_index(key);
const enum state::button_state ks = state_->key_button_states[index];
switch ( action ) {
case key_action::press:
case key_action::repeat:
if ( ks == state::button_state::none || ks == state::button_state::release ) {
state_->key_button_states[index] = state::button_state::press;
case keyboard_key_action::press:
case keyboard_key_action::repeat:
if ( ks == state::button_state::released || ks == state::button_state::just_released ) {
state_->key_button_states[index] = state::button_state::just_pressed;
}
break;
case key_action::release:
if ( ks == state::button_state::press || ks == state::button_state::pressed ) {
state_->key_button_states[index] = state::button_state::release;
case keyboard_key_action::release:
if ( ks == state::button_state::pressed || ks == state::button_state::just_pressed ) {
state_->key_button_states[index] = state::button_state::just_released;
}
break;
case key_action::unknown:
case keyboard_key_action::unknown:
break;
default:
E2D_ASSERT_MSG(false, "unexpected key action");
@@ -242,22 +275,22 @@ namespace e2d
}
}
void input::post_event(mouse mouse, mouse_action action) noexcept {
void input::post_event(mouse_button mouse, mouse_button_action action) noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
const std::size_t index = state_->mouse_button_index(mouse);
const enum state::button_state ms = state_->mouse_button_states[index];
switch ( action ) {
case mouse_action::press:
if ( ms == state::button_state::none || ms == state::button_state::release ) {
state_->mouse_button_states[index] = state::button_state::press;
case mouse_button_action::press:
if ( ms == state::button_state::released || ms == state::button_state::just_released ) {
state_->mouse_button_states[index] = state::button_state::just_pressed;
}
break;
case mouse_action::release:
if ( ms == state::button_state::press || ms == state::button_state::pressed ) {
state_->mouse_button_states[index] = state::button_state::release;
case mouse_button_action::release:
if ( ms == state::button_state::just_pressed || ms == state::button_state::pressed ) {
state_->mouse_button_states[index] = state::button_state::just_released;
}
break;
case mouse_action::unknown:
case mouse_button_action::unknown:
break;
default:
E2D_ASSERT_MSG(false, "unexpected mouse action");
@@ -272,12 +305,12 @@ namespace e2d
window_input_source::window_input_source(input& input) noexcept
: input_(input) {}
void window_input_source::on_key(key key, u32 scancode, key_action action) noexcept {
void window_input_source::on_keyboard_key(keyboard_key key, u32 scancode, keyboard_key_action act) noexcept {
E2D_UNUSED(scancode);
input_.post_event(key, action);
input_.post_event(key, act);
}
void window_input_source::on_mouse(mouse mouse, mouse_action action) noexcept {
input_.post_event(mouse, action);
void window_input_source::on_mouse_button(mouse_button btn, mouse_button_action act) noexcept {
input_.post_event(btn, act);
}
}

View File

@@ -13,34 +13,34 @@ namespace e2d
// class window::event_listener
//
void window::event_listener::on_key(key key, u32 scancode, key_action action) noexcept {
E2D_UNUSED(key, scancode, action);
}
void window::event_listener::on_uchar(char32_t uchar) noexcept {
E2D_UNUSED(uchar);
}
void window::event_listener::on_scroll(const v2f& delta) noexcept {
E2D_UNUSED(delta);
}
void window::event_listener::on_cursor(const v2f& position) noexcept {
E2D_UNUSED(position);
void window::event_listener::on_move_cursor(const v2f& pos) noexcept {
E2D_UNUSED(pos);
}
void window::event_listener::on_mouse(mouse mouse, mouse_action action) noexcept {
E2D_UNUSED(mouse, action);
void window::event_listener::on_input_char(char32_t uchar) noexcept {
E2D_UNUSED(uchar);
}
void window::event_listener::on_close() noexcept {
void window::event_listener::on_mouse_button(mouse_button btn, mouse_button_action act) noexcept {
E2D_UNUSED(btn, act);
}
void window::event_listener::on_focus(bool focused) noexcept {
void window::event_listener::on_keyboard_key(keyboard_key key, u32 scancode, keyboard_key_action act) noexcept {
E2D_UNUSED(key, scancode, act);
}
void window::event_listener::on_window_close() noexcept {
}
void window::event_listener::on_window_focus(bool focused) noexcept {
E2D_UNUSED(focused);
}
void window::event_listener::on_minimize(bool minimized) noexcept {
void window::event_listener::on_window_minimize(bool minimized) noexcept {
E2D_UNUSED(minimized);
}
@@ -51,40 +51,40 @@ namespace e2d
window_trace_event_listener::window_trace_event_listener(debug& debug) noexcept
: debug_(debug) {}
void window_trace_event_listener::on_key(key key, u32 scancode, key_action action) noexcept {
debug_.trace("WINDOW: on_key(key: %0 scancode: %1 action: %2)",
key_to_cstr(key),
scancode,
key_action_to_cstr(action));
}
void window_trace_event_listener::on_uchar(char32_t uchar) noexcept {
debug_.trace("WINDOW: on_uchar(uchar: %0)", str32_view(&uchar, 1));
}
void window_trace_event_listener::on_scroll(const v2f& delta) noexcept {
debug_.trace("WINDOW: on_scroll(delta: %0)", delta);
}
void window_trace_event_listener::on_cursor(const v2f& position) noexcept {
debug_.trace("WINDOW: on_cursor(position: %0)", position);
void window_trace_event_listener::on_move_cursor(const v2f& position) noexcept {
debug_.trace("WINDOW: on_move_cursor(position: %0)", position);
}
void window_trace_event_listener::on_mouse(mouse mouse, mouse_action action) noexcept {
debug_.trace("WINDOW: on_mouse(mouse: %0 action: %1)",
mouse_to_cstr(mouse),
mouse_action_to_cstr(action));
void window_trace_event_listener::on_input_char(char32_t uchar) noexcept {
debug_.trace("WINDOW: on_input_char(uchar: %0)", str32_view(&uchar, 1));
}
void window_trace_event_listener::on_close() noexcept {
debug_.trace("WINDOW: on_close()");
void window_trace_event_listener::on_mouse_button(mouse_button btn, mouse_button_action act) noexcept {
debug_.trace("WINDOW: on_mouse_button(btn: %0 act: %1)",
mouse_button_to_cstr(btn),
mouse_button_action_to_cstr(act));
}
void window_trace_event_listener::on_focus(bool focused) noexcept {
debug_.trace("WINDOW: on_focus(focused: %0)", focused);
void window_trace_event_listener::on_keyboard_key(keyboard_key key, u32 scancode, keyboard_key_action act) noexcept {
debug_.trace("WINDOW: on_keyboard_key(key: %0 scancode: %1 act: %2)",
keyboard_key_to_cstr(key),
scancode,
keyboard_key_action_to_cstr(act));
}
void window_trace_event_listener::on_minimize(bool minimized) noexcept {
debug_.trace("WINDOW: on_minimize(minimized: %0)", minimized);
void window_trace_event_listener::on_window_close() noexcept {
debug_.trace("WINDOW: on_window_close()");
}
void window_trace_event_listener::on_window_focus(bool focused) noexcept {
debug_.trace("WINDOW: on_window_focus(focused: %0)", focused);
}
void window_trace_event_listener::on_window_minimize(bool minimized) noexcept {
debug_.trace("WINDOW: on_window_minimize(minimized: %0)", minimized);
}
}

View File

@@ -65,8 +65,21 @@ namespace
std::mutex glfw_state::mutex_;
std::shared_ptr<glfw_state> glfw_state::shared_state_;
key convert_glfw_key(int k) noexcept {
#define DEFINE_CASE(x,y) case GLFW_KEY_##x: return key::y
mouse_button convert_glfw_mouse_button(int m) noexcept {
#define DEFINE_CASE(x,y) case GLFW_MOUSE_BUTTON_##x: return mouse_button::y
switch ( m ) {
DEFINE_CASE(LEFT, left);
DEFINE_CASE(RIGHT, right);
DEFINE_CASE(MIDDLE, middle);
DEFINE_CASE(4, x1);
DEFINE_CASE(5, x2);
default: return mouse_button::unknown;
}
#undef DEFINE_CASE
}
keyboard_key convert_glfw_keyboard_key(int k) noexcept {
#define DEFINE_CASE(x,y) case GLFW_KEY_##x: return keyboard_key::y
switch ( k ) {
DEFINE_CASE(0, _0);
DEFINE_CASE(1, _1);
@@ -200,49 +213,32 @@ namespace
DEFINE_CASE(KP_EQUAL, kp_equal);
DEFINE_CASE(KP_DECIMAL, kp_decimal);
default: return key::unknown;
default: return keyboard_key::unknown;
}
#undef DEFINE_CASE
}
mouse convert_glfw_mouse(int m) noexcept {
switch ( m ) {
case GLFW_MOUSE_BUTTON_LEFT:
return mouse::left;
case GLFW_MOUSE_BUTTON_RIGHT:
return mouse::right;
case GLFW_MOUSE_BUTTON_MIDDLE:
return mouse::middle;
case GLFW_MOUSE_BUTTON_4:
return mouse::x1;
case GLFW_MOUSE_BUTTON_5:
return mouse::x2;
default:
return mouse::unknown;
}
}
key_action convert_glfw_key_action(int ka) noexcept {
switch ( ka ) {
case GLFW_PRESS:
return key_action::press;
case GLFW_REPEAT:
return key_action::repeat;
case GLFW_RELEASE:
return key_action::release;
default:
return key_action::unknown;
}
}
mouse_action convert_glfw_mouse_action(int ma) noexcept {
mouse_button_action convert_glfw_mouse_button_action(int ma) noexcept {
switch ( ma ) {
case GLFW_PRESS:
return mouse_action::press;
return mouse_button_action::press;
case GLFW_RELEASE:
return mouse_action::release;
return mouse_button_action::release;
default:
return mouse_action::unknown;
return mouse_button_action::unknown;
}
}
keyboard_key_action convert_glfw_keyboard_key_action(int ka) noexcept {
switch ( ka ) {
case GLFW_PRESS:
return keyboard_key_action::press;
case GLFW_REPEAT:
return keyboard_key_action::repeat;
case GLFW_RELEASE:
return keyboard_key_action::release;
default:
return keyboard_key_action::unknown;
}
}
}
@@ -277,14 +273,14 @@ namespace e2d
throw bad_window_operation();
}
glfwSetWindowUserPointer(window.get(), this);
glfwSetKeyCallback(window.get(), key_callback_);
glfwSetCharCallback(window.get(), uchar_callback_);
glfwSetScrollCallback(window.get(), scroll_callback_);
glfwSetCursorPosCallback(window.get(), cursor_callback_);
glfwSetMouseButtonCallback(window.get(), mouse_callback_);
glfwSetWindowCloseCallback(window.get(), close_callback_);
glfwSetWindowFocusCallback(window.get(), focus_callback_);
glfwSetWindowIconifyCallback(window.get(), minimize_callback_);
glfwSetCursorPosCallback(window.get(), move_cursor_callback_);
glfwSetCharCallback(window.get(), input_char_callback_);
glfwSetMouseButtonCallback(window.get(), mouse_button_callback_);
glfwSetKeyCallback(window.get(), keyboard_key_callback_);
glfwSetWindowCloseCallback(window.get(), window_close_callback_);
glfwSetWindowFocusCallback(window.get(), window_focus_callback_);
glfwSetWindowIconifyCallback(window.get(), window_minimize_callback_);
}
~state() noexcept {
@@ -334,78 +330,78 @@ namespace e2d
return {w, glfwDestroyWindow};
}
static void key_callback_(GLFWwindow* window, int key, int scancode, int action, int mods) noexcept {
E2D_UNUSED(mods);
state* self = static_cast<state*>(glfwGetWindowUserPointer(window));
if ( self ) {
self->for_all_listeners(
&event_listener::on_key,
convert_glfw_key(key),
math::numeric_cast<u32>(scancode),
convert_glfw_key_action(action));
}
}
static void uchar_callback_(GLFWwindow* window, u32 uchar) noexcept {
state* self = static_cast<state*>(glfwGetWindowUserPointer(window));
if ( self ) {
self->for_all_listeners(
&event_listener::on_uchar,
static_cast<char32_t>(uchar));
}
}
static void scroll_callback_(GLFWwindow* window, double xoffset, double yoffset) noexcept {
static void scroll_callback_(GLFWwindow* window, double delta_x, double delta_y) noexcept {
state* self = static_cast<state*>(glfwGetWindowUserPointer(window));
if ( self ) {
self->for_all_listeners(
&event_listener::on_scroll,
make_vec2(xoffset, yoffset).cast_to<f32>());
make_vec2(delta_x, delta_y).cast_to<f32>());
}
}
static void cursor_callback_(GLFWwindow* window, double x, double y) noexcept {
static void move_cursor_callback_(GLFWwindow* window, double pos_x, double pos_y) noexcept {
state* self = static_cast<state*>(glfwGetWindowUserPointer(window));
if ( self ) {
self->for_all_listeners(
&event_listener::on_cursor,
make_vec2(x, y).cast_to<f32>());
&event_listener::on_move_cursor,
make_vec2(pos_x, pos_y).cast_to<f32>());
}
}
static void mouse_callback_(GLFWwindow* window, int button, int action, int mods) noexcept {
static void input_char_callback_(GLFWwindow* window, u32 uchar) noexcept {
state* self = static_cast<state*>(glfwGetWindowUserPointer(window));
if ( self ) {
self->for_all_listeners(
&event_listener::on_input_char,
static_cast<char32_t>(uchar));
}
}
static void mouse_button_callback_(GLFWwindow* window, int button, int action, int mods) noexcept {
E2D_UNUSED(mods);
state* self = static_cast<state*>(glfwGetWindowUserPointer(window));
if ( self ) {
self->for_all_listeners(
&event_listener::on_mouse,
convert_glfw_mouse(button),
convert_glfw_mouse_action(action));
&event_listener::on_mouse_button,
convert_glfw_mouse_button(button),
convert_glfw_mouse_button_action(action));
}
}
static void close_callback_(GLFWwindow* window) noexcept {
static void keyboard_key_callback_(GLFWwindow* window, int key, int scancode, int action, int mods) noexcept {
E2D_UNUSED(mods);
state* self = static_cast<state*>(glfwGetWindowUserPointer(window));
if ( self ) {
self->for_all_listeners(
&event_listener::on_close);
&event_listener::on_keyboard_key,
convert_glfw_keyboard_key(key),
math::numeric_cast<u32>(scancode),
convert_glfw_keyboard_key_action(action));
}
}
static void focus_callback_(GLFWwindow* window, int focused) noexcept {
static void window_close_callback_(GLFWwindow* window) noexcept {
state* self = static_cast<state*>(glfwGetWindowUserPointer(window));
if ( self ) {
self->for_all_listeners(
&event_listener::on_focus,
&event_listener::on_window_close);
}
}
static void window_focus_callback_(GLFWwindow* window, int focused) noexcept {
state* self = static_cast<state*>(glfwGetWindowUserPointer(window));
if ( self ) {
self->for_all_listeners(
&event_listener::on_window_focus,
focused);
}
}
static void minimize_callback_(GLFWwindow* window, int minimized) noexcept {
static void window_minimize_callback_(GLFWwindow* window, int minimized) noexcept {
state* self = static_cast<state*>(glfwGetWindowUserPointer(window));
if ( self ) {
self->for_all_listeners(
&event_listener::on_minimize,
&event_listener::on_window_minimize,
minimized);
}
}
@@ -473,6 +469,7 @@ namespace e2d
bool window::toggle_vsync(bool yesno) noexcept {
std::lock_guard<std::recursive_mutex> guard(state_->rmutex);
E2D_ASSERT(state_->window);
glfwMakeContextCurrent(state_->window.get());
glfwSwapInterval(yesno ? 1 : 0);
state_->vsync = yesno;
@@ -495,6 +492,7 @@ namespace e2d
v2i real_size = yesno
? make_vec2(video_mode->width, video_mode->height)
: state_->virtual_size.cast_to<i32>();
E2D_ASSERT(state_->window);
glfwSetWindowMonitor(
state_->window.get(),
yesno ? monitor : nullptr,
@@ -509,8 +507,8 @@ namespace e2d
v2u window::real_size() const noexcept {
std::lock_guard<std::recursive_mutex> guard(state_->rmutex);
E2D_ASSERT(state_->window);
int w = 0, h = 0;
E2D_ASSERT(state_->window);
glfwGetWindowSize(state_->window.get(), &w, &h);
return make_vec2(w,h).cast_to<u32>();
}
@@ -522,8 +520,8 @@ namespace e2d
v2u window::framebuffer_size() const noexcept {
std::lock_guard<std::recursive_mutex> guard(state_->rmutex);
E2D_ASSERT(state_->window);
int w = 0, h = 0;
E2D_ASSERT(state_->window);
glfwGetFramebufferSize(state_->window.get(), &w, &h);
return make_vec2(w,h).cast_to<u32>();
}
@@ -535,10 +533,9 @@ namespace e2d
void window::set_title(str_view title) {
std::lock_guard<std::recursive_mutex> guard(state_->rmutex);
E2D_ASSERT(state_->window);
state_->title = make_utf8(title);
glfwSetWindowTitle(
state_->window.get(), state_->title.c_str());
E2D_ASSERT(state_->window);
glfwSetWindowTitle(state_->window.get(), state_->title.c_str());
}
bool window::should_close() const noexcept {
@@ -550,8 +547,7 @@ namespace e2d
void window::set_should_close(bool yesno) noexcept {
std::lock_guard<std::recursive_mutex> guard(state_->rmutex);
E2D_ASSERT(state_->window);
glfwSetWindowShouldClose(
state_->window.get(), yesno ? GLFW_TRUE : GLFW_FALSE);
glfwSetWindowShouldClose(state_->window.get(), yesno ? GLFW_TRUE : GLFW_FALSE);
}
void window::swap_buffers() noexcept {