mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-15 00:11:55 +07:00
add resizable window flag and resize window events
This commit is contained in:
@@ -83,16 +83,19 @@ namespace e2d
|
||||
window_parameters& caption(str_view value);
|
||||
window_parameters& size(const v2u& value) noexcept;
|
||||
window_parameters& vsync(bool value) noexcept;
|
||||
window_parameters& resizable(bool value) noexcept;
|
||||
window_parameters& fullscreen(bool value) noexcept;
|
||||
|
||||
const str& caption() const noexcept;
|
||||
const v2u& size() const noexcept;
|
||||
bool vsync() const noexcept;
|
||||
bool resizable() const noexcept;
|
||||
bool fullscreen() const noexcept;
|
||||
private:
|
||||
str caption_{"Enduro2D"};
|
||||
v2u size_{640, 480};
|
||||
bool vsync_{false};
|
||||
bool resizable_{false};
|
||||
bool fullscreen_{false};
|
||||
};
|
||||
|
||||
|
||||
@@ -27,13 +27,15 @@ namespace e2d
|
||||
virtual void on_mouse_scroll(const v2f& delta) 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_size(const v2u& size) noexcept;
|
||||
virtual void on_framebuffer_size(const v2u& size) 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:
|
||||
window(const v2u& size, str_view title, bool vsync, bool fullscreen);
|
||||
window(const v2u& size, str_view title, bool vsync, bool resizable, bool fullscreen);
|
||||
~window() noexcept final;
|
||||
|
||||
void hide() noexcept;
|
||||
@@ -84,6 +86,8 @@ namespace e2d
|
||||
void on_mouse_scroll(const v2f& delta) 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_size(const v2u& size) noexcept final;
|
||||
void on_framebuffer_size(const v2u& size) noexcept final;
|
||||
void on_window_close() noexcept final;
|
||||
void on_window_focus(bool focused) noexcept final;
|
||||
void on_window_minimize(bool minimized) noexcept final;
|
||||
|
||||
@@ -132,6 +132,11 @@ namespace e2d
|
||||
return *this;
|
||||
}
|
||||
|
||||
engine::window_parameters& engine::window_parameters::resizable(bool value) noexcept {
|
||||
resizable_ = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
engine::window_parameters& engine::window_parameters::fullscreen(bool value) noexcept {
|
||||
fullscreen_ = value;
|
||||
return *this;
|
||||
@@ -149,6 +154,10 @@ namespace e2d
|
||||
return vsync_;
|
||||
}
|
||||
|
||||
bool engine::window_parameters::resizable() const noexcept {
|
||||
return resizable_;
|
||||
}
|
||||
|
||||
bool engine::window_parameters::fullscreen() const noexcept {
|
||||
return fullscreen_;
|
||||
}
|
||||
@@ -435,6 +444,7 @@ namespace e2d
|
||||
params.window_params().size(),
|
||||
params.window_params().caption(),
|
||||
params.window_params().vsync(),
|
||||
params.window_params().resizable(),
|
||||
params.window_params().fullscreen());
|
||||
|
||||
the<window>().register_event_listener<window_input_source>(the<input>());
|
||||
|
||||
@@ -34,6 +34,14 @@ namespace e2d
|
||||
E2D_UNUSED(key, scancode, act);
|
||||
}
|
||||
|
||||
void window::event_listener::on_window_size(const v2u& size) noexcept {
|
||||
E2D_UNUSED(size);
|
||||
}
|
||||
|
||||
void window::event_listener::on_framebuffer_size(const v2u& size) noexcept {
|
||||
E2D_UNUSED(size);
|
||||
}
|
||||
|
||||
void window::event_listener::on_window_close() noexcept {
|
||||
}
|
||||
|
||||
@@ -77,6 +85,14 @@ namespace e2d
|
||||
keyboard_key_action_to_cstr(act));
|
||||
}
|
||||
|
||||
void window_trace_event_listener::on_window_size(const v2u& size) noexcept {
|
||||
debug_.trace("WINDOW: on_window_size(size: %0)", size);
|
||||
}
|
||||
|
||||
void window_trace_event_listener::on_framebuffer_size(const v2u& size) noexcept {
|
||||
debug_.trace("WINDOW: on_framebuffer_size(size: %0)", size);
|
||||
}
|
||||
|
||||
void window_trace_event_listener::on_window_close() noexcept {
|
||||
debug_.trace("WINDOW: on_window_close()");
|
||||
}
|
||||
|
||||
@@ -260,10 +260,11 @@ namespace e2d
|
||||
v2u framebuffer_size;
|
||||
str title;
|
||||
bool vsync = false;
|
||||
bool resizable = false;
|
||||
bool fullscreen = false;
|
||||
bool cursor_hidden = false;
|
||||
public:
|
||||
state(const v2u& size, str_view ntitle, bool nvsync, bool nfullscreen)
|
||||
state(const v2u& size, str_view ntitle, bool nvsync, bool nresizable, bool nfullscreen)
|
||||
: shared_state(glfw_state::get_shared_state())
|
||||
, window(nullptr, glfwDestroyWindow)
|
||||
, real_size(size)
|
||||
@@ -271,12 +272,14 @@ namespace e2d
|
||||
, framebuffer_size(size)
|
||||
, title(ntitle)
|
||||
, vsync(nvsync)
|
||||
, resizable(nresizable)
|
||||
, fullscreen(nfullscreen)
|
||||
{
|
||||
window = open_window_(
|
||||
size,
|
||||
make_utf8(ntitle),
|
||||
vsync,
|
||||
resizable,
|
||||
fullscreen);
|
||||
|
||||
if ( !window ) {
|
||||
@@ -295,7 +298,7 @@ namespace e2d
|
||||
glfwSetKeyCallback(window.get(), keyboard_key_callback_);
|
||||
|
||||
glfwSetWindowSizeCallback(window.get(), window_size_callback_);
|
||||
glfwSetFramebufferSizeCallback(window.get(), window_framebuffer_callback_);
|
||||
glfwSetFramebufferSizeCallback(window.get(), framebuffer_size_callback_);
|
||||
|
||||
glfwSetWindowCloseCallback(window.get(), window_close_callback_);
|
||||
glfwSetWindowFocusCallback(window.get(), window_focus_callback_);
|
||||
@@ -345,6 +348,7 @@ namespace e2d
|
||||
const v2u& virtual_size,
|
||||
const str& title,
|
||||
bool vsync,
|
||||
bool resizable,
|
||||
bool fullscreen) noexcept
|
||||
{
|
||||
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
|
||||
@@ -355,11 +359,11 @@ namespace e2d
|
||||
if ( !video_mode ) {
|
||||
return {nullptr, glfwDestroyWindow};
|
||||
}
|
||||
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
|
||||
glfwWindowHint(GLFW_RESIZABLE, resizable ? GLFW_TRUE : GLFW_FALSE);
|
||||
#if defined(E2D_BUILD_MODE) && E2D_BUILD_MODE == E2D_BUILD_MODE_DEBUG
|
||||
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
|
||||
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
|
||||
#endif
|
||||
v2i real_size = fullscreen
|
||||
? make_vec2(video_mode->width, video_mode->height)
|
||||
@@ -432,14 +436,20 @@ namespace e2d
|
||||
state* self = static_cast<state*>(glfwGetWindowUserPointer(window));
|
||||
if ( self ) {
|
||||
self->update_window_size();
|
||||
self->for_all_listeners(
|
||||
&event_listener::on_window_size,
|
||||
make_vec2(w,h).cast_to<u32>());
|
||||
}
|
||||
}
|
||||
|
||||
static void window_framebuffer_callback_(GLFWwindow* window, int w, int h) noexcept {
|
||||
static void framebuffer_size_callback_(GLFWwindow* window, int w, int h) noexcept {
|
||||
E2D_UNUSED(w, h);
|
||||
state* self = static_cast<state*>(glfwGetWindowUserPointer(window));
|
||||
if ( self ) {
|
||||
self->update_framebuffer_size();
|
||||
self->for_all_listeners(
|
||||
&event_listener::on_framebuffer_size,
|
||||
make_vec2(w,h).cast_to<u32>());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -474,8 +484,8 @@ namespace e2d
|
||||
// class window
|
||||
//
|
||||
|
||||
window::window(const v2u& size, str_view title, bool vsync, bool fullscreen)
|
||||
: state_(new state(size, title, vsync, fullscreen)) {}
|
||||
window::window(const v2u& size, str_view title, bool vsync, bool resizable, bool fullscreen)
|
||||
: state_(new state(size, title, vsync, resizable, fullscreen)) {}
|
||||
window::~window() noexcept = default;
|
||||
|
||||
void window::hide() noexcept {
|
||||
|
||||
@@ -19,6 +19,7 @@ namespace e2d
|
||||
v2u virtual_size;
|
||||
str title;
|
||||
bool vsync = false;
|
||||
bool resizable = false;
|
||||
bool fullscreen = false;
|
||||
bool cursor_hidden = false;
|
||||
bool should_close = false;
|
||||
@@ -27,10 +28,11 @@ namespace e2d
|
||||
bool focused = true;
|
||||
bool minimized = false;
|
||||
public:
|
||||
state(const v2u& size, str_view title, bool vsync, bool fullscreen)
|
||||
state(const v2u& size, str_view title, bool vsync, bool resizable, bool fullscreen)
|
||||
: virtual_size(size)
|
||||
, title(make_utf8(title))
|
||||
, vsync(vsync)
|
||||
, resizable(resizable)
|
||||
, fullscreen(fullscreen) {}
|
||||
~state() noexcept = default;
|
||||
|
||||
@@ -45,8 +47,8 @@ namespace e2d
|
||||
}
|
||||
};
|
||||
|
||||
window::window(const v2u& size, str_view title, bool vsync, bool fullscreen)
|
||||
: state_(new state(size, title, vsync, fullscreen)) {}
|
||||
window::window(const v2u& size, str_view title, bool vsync, bool resizable, bool fullscreen)
|
||||
: state_(new state(size, title, vsync, resizable, fullscreen)) {}
|
||||
window::~window() noexcept = default;
|
||||
|
||||
void window::hide() noexcept {
|
||||
|
||||
Reference in New Issue
Block a user