diff options
| -rw-r--r-- | include/cru/platform/GraphicsBase.h | 7 | ||||
| -rw-r--r-- | include/cru/platform/gui/sdl/Window.h | 3 | ||||
| -rw-r--r-- | src/platform/gui/sdl/Window.cpp | 37 |
3 files changed, 39 insertions, 8 deletions
diff --git a/include/cru/platform/GraphicsBase.h b/include/cru/platform/GraphicsBase.h index dccff4e1..81c44cb6 100644 --- a/include/cru/platform/GraphicsBase.h +++ b/include/cru/platform/GraphicsBase.h @@ -157,7 +157,8 @@ struct Rect final { constexpr static Rect FromVertices(const Point& lefttop, const Point& rightbottom) { - return Rect(lefttop.x, lefttop.y, rightbottom.x - lefttop.x, rightbottom.y - lefttop.y); + return Rect(lefttop.x, lefttop.y, rightbottom.x - lefttop.x, + rightbottom.y - lefttop.y); } constexpr static Rect FromCenter(const Point& center, const float width, @@ -215,6 +216,10 @@ struct Rect final { return result; } + constexpr Rect Scale(float scale) const { + return {left * scale, top * scale, width * scale, height * scale}; + } + constexpr bool IsPointInside(const Point& point) const { return point.x >= left && point.x < GetRight() && point.y >= top && point.y < GetBottom(); diff --git a/include/cru/platform/gui/sdl/Window.h b/include/cru/platform/gui/sdl/Window.h index 78fbaa93..98166128 100644 --- a/include/cru/platform/gui/sdl/Window.h +++ b/include/cru/platform/gui/sdl/Window.h @@ -64,10 +64,13 @@ class SdlWindow : public SdlResource, public virtual INativeWindow { public: std::optional<SDL_Window*> GetSdlWindow(); SdlUiApplication* GetSdlUiApplication(); + float GetDisplayScale(); + Thickness GetBorderThickness(); private: SdlUiApplication* application_; std::optional<SDL_Window*> sdl_window_; + Rect client_rect_; SdlWindow* parent_; WindowStyleFlag style_; }; diff --git a/src/platform/gui/sdl/Window.cpp b/src/platform/gui/sdl/Window.cpp index 654ac4cd..f5fceb5f 100644 --- a/src/platform/gui/sdl/Window.cpp +++ b/src/platform/gui/sdl/Window.cpp @@ -4,6 +4,7 @@ #include "cru/platform/graphics/NullPainter.h" #include "cru/platform/graphics/Painter.h" #include "cru/platform/gui/Window.h" +#include "cru/platform/gui/sdl/Base.h" #include "cru/platform/gui/sdl/UiApplication.h" #include <SDL3/SDL_video.h> @@ -16,7 +17,9 @@ namespace cru::platform::gui::sdl { SdlWindow::SdlWindow(SdlUiApplication* application) - : application_(application), parent_(nullptr) { + : application_(application), + client_rect_(100, 100, 400, 200), + parent_(nullptr) { application->RegisterWindow(this); } @@ -63,22 +66,24 @@ void SdlWindow::SetClientSize(const Size& size) { Rect SdlWindow::GetClientRect() { if (!sdl_window_) return {}; - NotImplemented(); + return client_rect_; } void SdlWindow::SetClientRect(const Rect& rect) { - if (!sdl_window_) return; - NotImplemented(); + client_rect_ = rect; + if (sdl_window_) { + CheckSdlReturn(SDL_SetWindowPosition(*sdl_window_, rect.left, rect.top)); + CheckSdlReturn(SDL_SetWindowSize(*sdl_window_, rect.width, rect.height)); + } } Rect SdlWindow::GetWindowRect() { if (!sdl_window_) return {}; - NotImplemented(); + return client_rect_.Expand(GetBorderThickness()); } void SdlWindow::SetWindowRect(const Rect& rect) { - if (!sdl_window_) return; - NotImplemented(); + SetClientRect(rect.Shrink(GetBorderThickness())); } bool SdlWindow::RequestFocus() { @@ -127,4 +132,22 @@ std::optional<SDL_Window*> SdlWindow::GetSdlWindow() { return sdl_window_; } SdlUiApplication* SdlWindow::GetSdlUiApplication() { return application_; } +float SdlWindow::GetDisplayScale() { + if (!sdl_window_) return 1.f; + auto scale = SDL_GetWindowDisplayScale(*sdl_window_); + if (scale == 0.f) { + throw SdlException("Failed to get window display scale."); + } + return scale; +} + +Thickness SdlWindow::GetBorderThickness() { + if (!sdl_window_) return {}; + int top, left, bottom, right; + CheckSdlReturn( + SDL_GetWindowBordersSize(*sdl_window_, &top, &left, &bottom, &right)); + return {static_cast<float>(left), static_cast<float>(top), + static_cast<float>(right), static_cast<float>(bottom)}; +} + } // namespace cru::platform::gui::sdl |
