Implement floating MessageBox

This commit is contained in:
mrkubax10 2023-10-20 11:28:22 +02:00
parent c7a8b8b46c
commit 49922e4a18
11 changed files with 274 additions and 14 deletions

View File

@ -138,6 +138,7 @@ if(BUILD_CLIENT)
src/game/gui/image_gallery.cpp src/game/gui/image_gallery.cpp
src/game/gui/label.cpp src/game/gui/label.cpp
src/game/gui/layout.cpp src/game/gui/layout.cpp
src/game/gui/message_box.cpp
src/game/gui/style_change_listener.cpp src/game/gui/style_change_listener.cpp
src/game/gui/style.cpp src/game/gui/style.cpp
src/game/gui/text_edit.cpp src/game/gui/text_edit.cpp

View File

@ -44,7 +44,7 @@ namespace polygun::engine {
m_screen_manager(*this), m_screen_manager(*this),
m_window(nullptr), m_window(nullptr),
m_master_renderer(nullptr), m_master_renderer(nullptr),
m_gui(this) m_gui(nullptr)
{} {}
void Engine::init(DirectJoinArguments* direct_join) { void Engine::init(DirectJoinArguments* direct_join) {
@ -52,8 +52,9 @@ namespace polygun::engine {
m_window = window::Window::create_window("PolyGun", g_window_width, g_window_height, GameConfig::get().m_renderer); m_window = window::Window::create_window("PolyGun", g_window_width, g_window_height, GameConfig::get().m_renderer);
m_master_renderer = renderer::MasterRenderer::create_master_renderer(GameConfig::get().m_renderer, m_window); m_master_renderer = renderer::MasterRenderer::create_master_renderer(GameConfig::get().m_renderer, m_window);
m_gui.set_width(new gui::FixedConstraint(get_screen_width())); m_gui = new gui::GUIMaster(this);
m_gui.set_height(new gui::FixedConstraint(get_screen_height())); m_gui->set_width(new gui::FixedConstraint(get_screen_width()));
m_gui->set_height(new gui::FixedConstraint(get_screen_height()));
gui::Style::create(); gui::Style::create();
gui::Style::get().load_from_file("res/gui/styles/blue.xml"); gui::Style::get().load_from_file("res/gui/styles/blue.xml");
@ -74,7 +75,7 @@ namespace polygun::engine {
while (running && m_screen_manager.has_screen()) while (running && m_screen_manager.has_screen())
{ {
m_screen_manager.render(); m_screen_manager.render();
m_gui.render(get_gui_renderer()); m_gui->render(get_gui_renderer());
m_screen_manager.update(); m_screen_manager.update();
m_window->finish_frame(); m_window->finish_frame();
@ -86,11 +87,11 @@ namespace polygun::engine {
case window::EventType::EVENT_TYPE_WINDOW_RESIZE: case window::EventType::EVENT_TYPE_WINDOW_RESIZE:
g_window_width = event.m_data.m_window_event.m_width; g_window_width = event.m_data.m_window_event.m_width;
g_window_height = event.m_data.m_window_event.m_height; g_window_height = event.m_data.m_window_event.m_height;
m_gui.set_width(new gui::FixedConstraint(get_screen_width())); m_gui->set_width(new gui::FixedConstraint(get_screen_width()));
m_gui.set_height(new gui::FixedConstraint(get_screen_height())); m_gui->set_height(new gui::FixedConstraint(get_screen_height()));
get_gui_renderer()->on_viewport_resize(g_window_width, g_window_height); get_gui_renderer()->on_viewport_resize(g_window_width, g_window_height);
default: default:
if(m_gui.update(event)) if(m_gui->update(event))
break; break;
m_screen_manager.event(event); m_screen_manager.event(event);
break; break;
@ -99,6 +100,7 @@ namespace polygun::engine {
} }
m_screen_manager.clear(); m_screen_manager.clear();
delete m_gui;
gui::Style::cleanup(); gui::Style::cleanup();
delete m_master_renderer; delete m_master_renderer;
delete m_window; delete m_window;

View File

@ -46,7 +46,7 @@ namespace polygun::engine {
ScreenManager m_screen_manager; ScreenManager m_screen_manager;
window::Window* m_window; window::Window* m_window;
renderer::MasterRenderer* m_master_renderer; renderer::MasterRenderer* m_master_renderer;
gui::GUIMaster m_gui; gui::GUIMaster* m_gui;
public: public:
Engine(); Engine();
@ -61,7 +61,7 @@ namespace polygun::engine {
renderer::MasterRenderer* get_master_renderer() { return m_master_renderer; } renderer::MasterRenderer* get_master_renderer() { return m_master_renderer; }
renderer::MeshRenderer* get_mesh_renderer() { return m_master_renderer->get_mesh_renderer(); } renderer::MeshRenderer* get_mesh_renderer() { return m_master_renderer->get_mesh_renderer(); }
renderer::GUIRenderer* get_gui_renderer() { return m_master_renderer->get_gui_renderer(); } renderer::GUIRenderer* get_gui_renderer() { return m_master_renderer->get_gui_renderer(); }
gui::GUIMaster& get_gui_master() { return m_gui; } gui::GUIMaster& get_gui_master() { return *m_gui; }
static unsigned get_screen_width(); static unsigned get_screen_width();
static unsigned get_screen_height(); static unsigned get_screen_height();

View File

@ -77,8 +77,9 @@ void Box::set_title(const std::string& title) {
} }
void Box::render(renderer::GUIRenderer* renderer) { void Box::render(renderer::GUIRenderer* renderer) {
renderer->fill_rect(glm::vec2(get_x()-2, get_y()-2), glm::vec2(get_width()+2, get_height()+2), add_to_color(Style::get().m_background_color, glm::vec4ub(100, 100, 100, 0))); const float extrusion = Style::get().m_extrusion_size;
renderer->fill_rect(glm::vec2(get_x()+2, get_y()+2), glm::vec2(get_width(), get_height()), subtract_from_color(Style::get().m_background_color, glm::vec4ub(40, 40, 40, 0))); renderer->fill_rect(glm::vec2(get_x()-extrusion, get_y()-extrusion), glm::vec2(get_width()+extrusion, get_height()+extrusion), add_to_color(Style::get().m_background_color, glm::vec4ub(100, 100, 100, 0)));
renderer->fill_rect(glm::vec2(get_x()+extrusion, get_y()+extrusion), glm::vec2(get_width(), get_height()), subtract_from_color(Style::get().m_background_color, glm::vec4ub(40, 40, 40, 0)));
renderer->fill_rect(glm::vec2(get_x(), get_y()), glm::vec2(get_width(), get_height()), Style::get().m_background_color); renderer->fill_rect(glm::vec2(get_x(), get_y()), glm::vec2(get_width(), get_height()), Style::get().m_background_color);
renderer->render_texture(glm::vec2(get_x()+5, get_y()+5), m_title_texture); renderer->render_texture(glm::vec2(get_x()+5, get_y()+5), m_title_texture);
Container::render(renderer); Container::render(renderer);

View File

@ -68,6 +68,8 @@ Container::Container(engine::Engine* engine, xml::XMLNode* node) :
m_layout = Layout::from_string(val); m_layout = Layout::from_string(val);
m_layout->m_container = this; m_layout->m_container = this;
} }
else
m_layout = new FixedLayout;
} }
Container::Container(engine::Engine* engine) : Container::Container(engine::Engine* engine) :

View File

@ -24,6 +24,10 @@ SOFTWARE.
#include "game/gui/gui_master.hpp" #include "game/gui/gui_master.hpp"
#include <algorithm>
#include "common/xml/parser.hpp"
#include "common/xml/xml_node.hpp"
#include "game/window/event.hpp" #include "game/window/event.hpp"
using namespace polygun::gui; using namespace polygun::gui;
@ -32,7 +36,8 @@ GUIMaster::GUIMaster(engine::Engine* engine) :
Container(engine), Container(engine),
m_highlighted_component(nullptr), m_highlighted_component(nullptr),
m_highlight_locked(false), m_highlight_locked(false),
m_shift(false) m_shift(false),
m_messagebox_stack()
{} {}
void GUIMaster::request_highlight(Component* component) { void GUIMaster::request_highlight(Component* component) {
@ -43,6 +48,27 @@ void GUIMaster::request_highlight(Component* component) {
m_highlighted_component = component; m_highlighted_component = component;
} }
MessageBox* GUIMaster::open_messagebox(const std::string& gui_path) {
xml::XMLNode* node;
if(!xml::parse_file(gui_path, node))
return nullptr;
std::unique_ptr<MessageBox> message_box = std::make_unique<MessageBox>(m_engine, node);
delete node;
MessageBox* const message_box_ptr = message_box.get();
m_messagebox_stack.push_back(std::move(message_box));
return message_box_ptr;
}
void GUIMaster::close_messagebox(const MessageBox* message_box) {
std::vector<std::unique_ptr<MessageBox>>::iterator it = std::find_if(m_messagebox_stack.begin(), m_messagebox_stack.end(), [message_box](const std::unique_ptr<MessageBox>& element) {
return element.get()==message_box;
});
if(it==m_messagebox_stack.end())
return;
m_messagebox_stack.erase(it);
m_highlighted_component = nullptr;
}
void GUIMaster::clear() { void GUIMaster::clear() {
Container::clear(); Container::clear();
m_highlighted_component = nullptr; m_highlighted_component = nullptr;
@ -51,6 +77,8 @@ void GUIMaster::clear() {
void GUIMaster::render(renderer::GUIRenderer* renderer) { void GUIMaster::render(renderer::GUIRenderer* renderer) {
Container::render(renderer); Container::render(renderer);
for(std::unique_ptr<MessageBox>& message_box : m_messagebox_stack)
message_box->render(renderer);
if(m_highlighted_component) if(m_highlighted_component)
m_highlighted_component->render(renderer); m_highlighted_component->render(renderer);
} }
@ -87,6 +115,10 @@ bool GUIMaster::update(const window::Event& event) {
} }
if(m_highlighted_component && m_highlighted_component->update(event)) if(m_highlighted_component && m_highlighted_component->update(event))
return true; return true;
for(ssize_t i = m_messagebox_stack.size()-1; i>=0; i--) {
if(m_messagebox_stack[i]->update(event))
return true;
}
const bool result = Container::update(event); const bool result = Container::update(event);
if(!result && event.m_type==window::EventType::EVENT_TYPE_MOUSE_BUTTON_DOWN) if(!result && event.m_type==window::EventType::EVENT_TYPE_MOUSE_BUTTON_DOWN)
request_highlight(nullptr); request_highlight(nullptr);

View File

@ -25,7 +25,9 @@ SOFTWARE.
#ifndef POLYGUN_GUI_GUI_MASTER_HPP #ifndef POLYGUN_GUI_GUI_MASTER_HPP
#define POLYGUN_GUI_GUI_MASTER_HPP #define POLYGUN_GUI_GUI_MASTER_HPP
#include "game/gui/container.hpp" #include <memory>
#include "game/gui/message_box.hpp"
namespace polygun::gui { namespace polygun::gui {
class GUIMaster final : public Container { class GUIMaster final : public Container {
@ -35,6 +37,8 @@ namespace polygun::gui {
void request_highlight(Component* component); void request_highlight(Component* component);
void lock_highlight() { m_highlight_locked = true; } void lock_highlight() { m_highlight_locked = true; }
void unlock_highlight() { m_highlight_locked = false; } void unlock_highlight() { m_highlight_locked = false; }
MessageBox* open_messagebox(const std::string& gui_path);
void close_messagebox(const MessageBox* message_box);
Component* get_highlighted_component() { return m_highlighted_component; } Component* get_highlighted_component() { return m_highlighted_component; }
@ -46,6 +50,7 @@ namespace polygun::gui {
Component* m_highlighted_component; Component* m_highlighted_component;
bool m_highlight_locked; bool m_highlight_locked;
bool m_shift; bool m_shift;
std::vector<std::unique_ptr<MessageBox>> m_messagebox_stack;
private: private:
static Component* find_first_component(Container* container); static Component* find_first_component(Container* container);

View File

@ -0,0 +1,137 @@
/*
PolyGun
Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "game/gui/message_box.hpp"
#include "common/locale/util.hpp"
#include "common/xml/reader.hpp"
#include "game/engine/engine.hpp"
#include "game/gui/button.hpp"
#include "game/gui/constraint.hpp"
#include "game/gui/style.hpp"
#include "game/renderer/texture.hpp"
#include "game/window/event.hpp"
#include "game/window/window.hpp"
using namespace polygun::gui;
MessageBox::MessageBox(engine::Engine* engine, xml::XMLNode* node) :
Container(engine, node),
StyleChangeListener(),
m_title(),
m_dragged(false),
m_drag_offset(),
m_title_texture(engine->get_master_renderer()->create_texture())
{
xml::XMLReader reader(node);
std::string val;
if(reader.get_attribute("text", m_title))
update_title_text_texture();
else if(reader.get_attribute("tr_text", val))
set_title(_(val));
m_left_padding = 5;
set_x(new FixedConstraint((engine->get_screen_width()-get_width())/2));
set_y(new FixedConstraint((engine->get_screen_height()-get_height())/2));
// TODO: Implement buttons with images
ButtonProp properties = {
"X",
&MessageBox::close,
reinterpret_cast<void*>(this)
};
Button* const close_button = new Button(engine, new RightAlignedConstraint(5), new FixedConstraint(-24), new FixedConstraint(16), new FixedConstraint(16), "", properties);
add_component(close_button);
}
MessageBox::~MessageBox() {
delete m_title_texture;
}
void MessageBox::set_title(const std::string& title) {
m_title = title;
update_title_text_texture();
}
void MessageBox::render(renderer::GUIRenderer* renderer) {
const float extrusion = Style::get().m_extrusion_size;
renderer->fill_rect(glm::vec2(get_x()-extrusion, get_y()-extrusion), glm::vec2(get_width()+extrusion, get_height()+extrusion), add_to_color(Style::get().m_background_color, glm::vec4ub(100, 100, 100, 0)));
renderer->fill_rect(glm::vec2(get_x()+extrusion, get_y()+extrusion), glm::vec2(get_width(), get_height()), subtract_from_color(Style::get().m_background_color, glm::vec4ub(40, 40, 40, 0)));
renderer->fill_rect(glm::vec2(get_x(), get_y()), glm::vec2(get_width(), get_height()), Style::get().m_background_color);
renderer->render_texture(glm::vec2(get_x()+5, get_y()+5), m_title_texture);
Container::render(renderer);
}
bool MessageBox::update(const window::Event& ev) {
const glm::vec2 pos(get_x(), get_y());
switch(ev.m_type) {
case window::EventType::EVENT_TYPE_MOUSE_BUTTON_DOWN:
if(ev.m_data.m_mouse_event.m_button==window::MouseButton::MOUSE_BUTTON_LEFT && m_engine->get_window()->is_mouse_inside_area(pos, glm::vec2(get_width(), 20))) {
m_dragged = true;
m_drag_offset.x = m_engine->get_window()->get_mouse_x()-get_x();
m_drag_offset.y = m_engine->get_window()->get_mouse_y()-get_y();
}
break;
case window::EventType::EVENT_TYPE_MOUSE_BUTTON_UP:
if(ev.m_data.m_mouse_event.m_button==window::MouseButton::MOUSE_BUTTON_LEFT)
m_dragged = false;
break;
case window::EventType::EVENT_TYPE_MOUSE_MOVE:
if(m_dragged) {
set_x(new FixedConstraint(ev.m_data.m_mouse_event.m_x-m_drag_offset.x));
set_y(new FixedConstraint(ev.m_data.m_mouse_event.m_y-m_drag_offset.y));
}
default:
break;
}
if(Container::update(ev))
return true;
const glm::vec2 size(get_width(), get_height());
if((ev.m_type==window::EventType::EVENT_TYPE_MOUSE_BUTTON_DOWN || ev.m_type==window::EventType::EVENT_TYPE_MOUSE_BUTTON_UP) && m_engine->get_window()->is_mouse_inside_area(pos, size))
return true;
return false;
}
void MessageBox::on_style_change() {
update_title_text_texture();
}
void MessageBox::close(void* data) {
MessageBox* const message_box = reinterpret_cast<MessageBox*>(data);
message_box->m_engine->get_gui_master().close_messagebox(message_box);
}
void MessageBox::update_title_text_texture() {
std::unique_ptr<renderer::Surface> surf;
const float width = get_width();
const float height = get_height();
const glm::vec4ub& color = m_inactive?Style::get().m_inactive_text_color:Style::get().m_text_color;
const unsigned title_size = 14;
if(width>0 && height>0)
surf = Style::get().m_font.render_text_in_area(glm::vec2(width, height), m_title, title_size, color);
else
surf = Style::get().m_font.render_text(m_title, title_size, color);
m_title_texture->load_from_surface(*surf);
m_top_padding = m_title_texture->get_height()+15;
}

View File

@ -0,0 +1,59 @@
/*
PolyGun
Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef POLYGUN_GUI_MESSAGEBOX_HPP
#define POLYGUN_GUI_MESSAGEBOX_HPP
#include "game/gui/container.hpp"
#include "game/gui/style_change_listener.hpp"
namespace polygun::renderer {
class Texture;
}
namespace polygun::gui {
class MessageBox final : public Container, public StyleChangeListener {
public:
MessageBox(engine::Engine* engine, xml::XMLNode* node);
virtual ~MessageBox() override;
void set_title(const std::string& title);
virtual void render(renderer::GUIRenderer* renderer) override;
virtual bool update(const window::Event& ev) override;
virtual void on_style_change() override;
private:
std::string m_title;
bool m_dragged;
glm::vec2 m_drag_offset;
renderer::Texture* m_title_texture;
private:
static void close(void* data);
void update_title_text_texture();
};
}
#endif // POLYGUN_GUI_MESSAGEBOX_HPP

View File

@ -42,7 +42,7 @@ Style::Style() :
m_background_color(39, 59, 86, 255), m_background_color(39, 59, 86, 255),
m_lower_background_color(19, 39, 65, 255), m_lower_background_color(19, 39, 65, 255),
m_font(), m_font(),
m_extrusion_size(2), m_extrusion_size(1),
m_change_listeners() m_change_listeners()
{} {}

View File

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang ="pl">
<head>
<meta charset ="utf-8">
</head>
<body>
<h2>Szymon Maczuga</h2>
<hr>
<p>Uczę się i chce uzyskac zawód <u><b>Technik Programista</b></u>.</p>
<div> Dzisiaj pójdzie mi super i dostane ocenę <i><mark> pozytywną </mark></i></div>
<p>Dostanę <s>niedostateczny</s> czy bardzo dobry</p>
<br>
<mark>Trzeba sie uczyć systematycznie</mark>
<pre>Zmotywuje się do
nauki
i osiągne sukces.</pre>
<code>Logarytmy to jest wyzwanie: log<sub>a</sub>b<sup>n</sup>=n*log<sub>a</sub>b</code>
</body>
</html>