-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.cpp
228 lines (189 loc) · 6.84 KB
/
window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// Created by piotr on 05.02.25.
//
/*------- include files:
-------------------------------------------------------------------*/
#include "window.h"
#include "application.h"
#include "thema.h"
#include "types.h"
#include "toolbox/all.h"
namespace Witcher {
using namespace bee;
Window::Window(Application& app, std::string const& title, int width, int height) :
Widget(ObjectType::Window, this)
{
constexpr auto flags =
SDL_WINDOW_RESIZABLE
| SDL_WINDOW_HIDDEN
| SDL_WINDOW_HIGH_PIXEL_DENSITY
| SDL_WINDOW_INPUT_FOCUS
| SDL_WINDOW_MOUSE_FOCUS;
SDL_Renderer* renderer;
if (!SDL_CreateWindowAndRenderer(title.c_str(), width, height, flags, &window_, &renderer)) {
box::print_error("Failed to create window and renderer: {}\n", SDL_GetError());
exit(1);
}
set_renderer(renderer);
display_id_ = SDL_GetDisplayForWindow(window_);
SDL_HideWindow(window_);
set_focusable(true);
// Register the window in application.
app.window_ = this;
box::println("Window::Window");
}
Window::~Window() {
if (menu_bar_)
delete menu_bar_;
SDL_DestroyRenderer(renderer());
SDL_DestroyWindow(window_);
}
bool Window::user_quit_request() noexcept {
if (can_close()) {
auto close_window_request = SDL_Event{.type = SDL_EVENT_WINDOW_CLOSE_REQUESTED};
SDL_PushEvent(&close_window_request);
auto quit_request = SDL_Event{.type = SDL_EVENT_QUIT};
SDL_PushEvent(&quit_request);
return true;
}
return false;
/*
SDL_Event event;
SDL_zero(event);
event.type = SDL_EVENT_QUIT;
// event.user.code = UserEvent::QuitRequest;
// event.user.data1 = nullptr;
// event.user.data2 = nullptr;
// event.type = SDL_EVENT_USER;
// event.user.code = UserEvent::QuitRequest;
// event.user.data1 = nullptr;
// event.user.data2 = nullptr;
SDL_PushEvent(&event);
*/
}
bool Window::set_content(Object* const content) noexcept {
if (children().empty()) {
add_child(content);
return true;
}
box::println_error("Failed to set content. The window already has content.");
return {};
}
/****************************************************************
* *
* p r e p a r e *
* *
****************************************************************/
void Window::prepare() noexcept {
update_frame();
if (menu_bar_)
menu_bar_->prepare();
for (const auto it : children()) {
it->set_parent(this);
it->prepare();
}
}
void Window::show() noexcept {
SDL_ShowWindow(window_);
set_visible(true);
move(frame().pos.x, frame().pos.y);
update_geometry();
}
void Window::move(int const x, int const y) noexcept {
if (SDL_SetWindowPosition(window_, x, y)) {
update_frame();
return;
}
box::print_error("Failed to move window to position: {}\n", SDL_GetError());
}
void Window::update_geometry() noexcept {
update_frame();
// box::println("Window::update_geometry {}", frame().size);
if (menu_bar_)
menu_bar_->update_geometry();
for (auto const child : children())
child->update_geometry();
}
void Window::move_center(int display) noexcept {
int display_count{};
SDL_DisplayID const* const screen = SDL_GetDisplays(&display_count);
if (display > (display_count - 1))
display = display_count - 1;
SDL_Rect display_rect{};
if (SDL_GetDisplayBounds(screen[display], &display_rect)) {
if (auto size = window_size()) {
auto const [w, h] = size.value();
auto const name = std::string(SDL_GetDisplayName(screen[display])).c_str();
// box::println("Selected display: {}, {}\n", name, display_rect);
auto const x = display_rect.x + (display_rect.w - w) / 2;
auto const y = display_rect.y + (display_rect.h - h) / 2;
move(x, y);
return;
}
}
box::print_error("Failed to move window to center: {}\n", SDL_GetError());
}
void Window::resize(int const width, int const height) noexcept {
if (SDL_SetWindowSize(window_, width, height)) {
// update_frame();
update_geometry();
return;
}
box::print_error("Failed to resize window: {}\n", SDL_GetError());
}
std::optional<Size> Window::window_size() const noexcept {
Size size{};
if (SDL_GetWindowSize(window_, &size.w, &size.h))
return size;
box::print_error("Failed to get size of window: {}\n", SDL_GetError());
return {};
}
Object* Window::contains_point(f32 const x, f32 const y) noexcept {
if (focusable()) {
if (menu_bar_) {
if (auto const obj = menu_bar_->contains_point(x, y))
return obj;
}
for (auto const child : children())
if (child->contains_point(x, y))
return child;
return this;
}
return {};
}
std::optional<Point> Window::window_pos() const noexcept {
Point pos{};
if (SDL_GetWindowPosition(window_, &pos.x, &pos.y))
return pos;
box::print_error("Failed to get pos of window: {}\n", SDL_GetError());
return {};
}
void Window::update_frame() noexcept {
if (SDL_SyncWindow(window_)) {
if (auto const pos = window_pos()) {
if (auto const size = window_size()) {
frame().pos = *pos;
frame().size = *size;
// box::println("Window::update_frame, window frame: {}", frame());
return;
}
}
}
box::print_error("Failed to update window's frame: {}\n", SDL_GetError());
exit(1);
}
void Window::update() noexcept {
for (const auto child: children()) {
child->update();
}
}
void Window::draw() noexcept {
auto const [r, g, b, a] = thema::DEFAULT_WINDOW_BACKGROUND;
SDL_SetRenderDrawColor(renderer(), r, g, b, a);
SDL_RenderClear(renderer());
if (menu_bar_) menu_bar_->draw();
for (const auto child: children())
child->draw();
SDL_RenderPresent(renderer());
}
}