FlowArithmeticCalculator 1.0.0
Flow Arithmetic Calculator
Loading...
Searching...
No Matches
WindowBase.h
Go to the documentation of this file.
1#ifndef GUI_WINDOW_WINDOW_BASE_H
2#define GUI_WINDOW_WINDOW_BASE_H
3
4#include <algorithm>
5#include <memory>
6#include <utility>
7
13#include "spdlog/spdlog.h"
14
18namespace gui::window {
26 template <typename Canvas>
28 public:
29 WindowBase(int width, int height, std::shared_ptr<spdlog::logger> logger)
30 : blocksManager(std::make_shared<gui::elements::SkiaBlocksManagerRenderer>(this)),
31 logger(std::move(logger)),
32 winSize({.width = width, .height = height}),
33 framebufferSize({.width = width, .height = height}) {}
34
35 ~WindowBase() override = default;
36
37 // delete copy semantics
38 WindowBase(const WindowBase&) = delete;
39 WindowBase& operator=(const WindowBase&) = delete;
40
41 // enable move semantics
42 WindowBase(WindowBase&&) noexcept = default;
43 WindowBase& operator=(WindowBase&&) noexcept = default;
44
48 virtual void run() = 0;
49
54 virtual bool shouldClose() const = 0;
55
61
66 void handleMouseUp() { blocksManager->handleMouseUp(); }
67
72 void handleNumericKeyPress(int number) { blocksManager->handleNumericKeyPress(number); }
73
78 void handleEscapeKeyPress() { blocksManager->handleEscapeKeyPress(); }
79
85 auto maybeHoveredBlock = blocksManager->getBlockAtMousePos();
86
87 if (maybeHoveredBlock.has_value()) {
88 blocksManager->handleRightClickOnBlock(maybeHoveredBlock.value());
89 } else {
90 // if there is no hovered block, open the add block popup
91 if (blocksManager->hasActiveChoicesInput()) {
92 logger->info(
93 "Right clicked on empty space, but input is already active, ignoring "
94 "event");
95 } else {
96 logger->info("Right clicked on empty space, opening add block popup");
97
100 choices = {};
101
102 std::transform(
103 magic_enum::enum_values<business_logic::elements::blocks::BlockType>()
104 .begin(),
105 magic_enum::enum_values<business_logic::elements::blocks::BlockType>()
106 .end(),
107 std::back_inserter(choices),
108 [](const auto& choice) {
111 .displayName = std::string(magic_enum::enum_name(choice)),
112 .value = choice,
113 };
114 });
115
116 this->blocksManager->setActiveChoicesInput({
117 .choices = choices,
118 .callback =
119 [this](const business_logic::elements::blocks::BlockType& blockType) {
120 this->blocksManager->onNewBlockChoice(blockType);
121 },
122 });
123 }
124 }
125 }
126
131 void handleMouseMove(int x, int y) { blocksManager->handleMouseMove(x, y); }
132
137
142
143 protected:
147 std::shared_ptr<business_logic::BlocksManager> blocksManager;
148
152 std::shared_ptr<spdlog::logger> logger;
153
158
163 };
164} // namespace gui::window
165
166#endif // GUI_WINDOW_WINDOW_BASE_H
std::string displayName
The display name of the input choice.
Definition InputChoice.h:16
The abstract base class for implementing a window.
Definition WindowBase.h:27
void handleNumericKeyPress(int number)
Handles the numeric key press event; should be called internally by the window implementation.
Definition WindowBase.h:72
void handleRightClick()
Handles the right click event; should be called internally by the window implementation.
Definition WindowBase.h:84
business_logic::geometry::Size2D getFramebufferSize() override
Get the framebuffer size.
Definition WindowBase.h:141
std::shared_ptr< spdlog::logger > logger
Definition WindowBase.h:152
WindowBase(const WindowBase &)=delete
business_logic::geometry::Size2D winSize
Definition WindowBase.h:157
business_logic::geometry::Size2D getWindowSize() override
Get the window size.
Definition WindowBase.h:136
~WindowBase() override=default
virtual bool shouldClose() const =0
Whether the window should close.
WindowBase & operator=(const WindowBase &)=delete
virtual void run()=0
Runs the main window loop.
business_logic::geometry::Size2D framebufferSize
Definition WindowBase.h:162
void handleMouseUp()
Handles the mouse up event; should be called internally by the window implementation.
Definition WindowBase.h:66
std::shared_ptr< business_logic::BlocksManager > blocksManager
Definition WindowBase.h:147
void handleMouseDown()
Handles the mouse down event; should be called internally by the window implementation.
Definition WindowBase.h:60
WindowBase(int width, int height, std::shared_ptr< spdlog::logger > logger)
Definition WindowBase.h:29
WindowBase(WindowBase &&) noexcept=default
void handleMouseMove(int x, int y)
Handles the mouse move event; should be called internally by the window implementation.
Definition WindowBase.h:131
void handleEscapeKeyPress()
Handles the ESC key press event; should be called internally by the window implementation.
Definition WindowBase.h:78
BlockType
The available block types.
Definition BlockType.h:10
The GUI window abstraction & implementation.
The actual GUI / renderer implementation counterpart.
Definition constants.h:7
STL namespace.
A basic struct representing a 2D size.
Definition Size2D.h:8