Debian Codegraph @

source/code/sago/SagoLogicalResize.hpp

raw symbols SCIP
1 /*
2 Copyright (c) 2025 Poul Sander
3
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation files
6 (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge,
8 publish, distribute, sublicense, and/or sell copies of the Software,
9 and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24
25 #pragma once
26 #include <algorithm>
27 #include "SDL.h"
28
29
30 namespace sago {
31
32 class SagoLogicalResize
33 {
34 public:
35 SagoLogicalResize() : logical_width_(1), logical_height_(1), physical_width_(1), physical_height_(1) { SetScaleFactor(); }
36 SagoLogicalResize(int logical_width, int logical_height)
37 : logical_width_(std::max(1, logical_width)), logical_height_(std::max(1, logical_height)), physical_width_(1), physical_height_(1) { SetScaleFactor(); }
38
39 void SetPhysicalSize(int physical_width, int physical_height)
40 {
41 //Physical size must be at least 1. Less than 1 is not drawn anyway and it prevents division by zero.
42 physical_width_ = std::max(1, physical_width);
43 physical_height_ = std::max(1, physical_height);
44 SetScaleFactor();
45 }
46
47 void LogicalToPhysical(int logical_x, int logical_y, int &physical_x, int &physical_y) const
48 {
49 physical_x = logical_x;
50 physical_y = logical_y;
51 LogicalToPhysical(&physical_x, &physical_y);
52 }
53
54 void LogicalToPhysical(int *x, int *y) const
55 {
56 if (x) {
57 *x = *x * scale_factor_ + left_margin_;
58 }
59 if (y) {
60 *y = *y * scale_factor_ + top_margin_;
61 }
62 }
63
64 void LogicalToPhysical(SDL_Rect &inout) const
65 {
66 SDL_Rect input = inout;
67 LogicalToPhysical(&inout.x, &inout.y);
68 LogicalToPhysical(input.x + input.w + 1, input.y + input.h + 1, inout.w, inout.h);
69 inout.w -= inout.x - 1;
70 inout.h -= inout.y - 1;
71 }
72
73 void PhysicalToLogical(int physical_x, int physical_y, int &logical_x, int &logical_y) const
74 {
75 logical_x = (physical_x - left_margin_) / scale_factor_;
76 logical_y = (physical_y - top_margin_) / scale_factor_;
77 }
78
79 int GetTopMargin() const
80 {
81 return top_margin_;
82 }
83
84 int GetLeftMargin() const
85 {
86 return left_margin_;
87 }
88
89 private:
90 void SetScaleFactor()
91 {
92 scale_factor_ = std::min(physical_width_ / logical_width_, physical_height_ / logical_height_);
93 left_margin_ = (physical_width_ - logical_width_ * scale_factor_) / 2;
94 top_margin_ = (physical_height_ - logical_height_ * scale_factor_) / 2;
95 }
96
97 double scale_factor_;
98 int top_margin_;
99 int left_margin_;
100 double logical_width_;
101 double logical_height_;
102 double physical_width_;
103 double physical_height_;
104 };
105
106 } // namespace sago