CYD-UI
A C++ library for building native graphic user interfaces
Loading...
Searching...
No Matches
configure_anchors.cppm
Go to the documentation of this file.
1// Copyright (c) 2025, Víctor Castillo Agüero.
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4module;
6
7export import std;
8
9export import cydui.components.base;
10export import cydui.components.anchors;
11
12#define TO_STRING(...) #__VA_ARGS__
13#define ANCHOR(PREFIX, NAME) \
14 struct NAME { \
15 static constexpr dimension_parameter_t x{TO_STRING(PREFIX##_##NAME##_x)}; \
16 static constexpr dimension_parameter_t y{TO_STRING(PREFIX##_##NAME##_y)}; \
17 }
18
19export {
22 std::shared_ptr<component_base_t> child, std::optional<std::shared_ptr<component_base_t>> prev
23 ) {
25 dimension_ctx_t& ctx = *child->get_dimensional_context();
26 ctx.set_parameter("self_x", child->get_dimensional_relations().x);
27 ctx.set_parameter("self_y", child->get_dimensional_relations().y);
28 ctx.set_parameter("self_width", child->get_dimensional_relations().width);
29 ctx.set_parameter("self_height", child->get_dimensional_relations().height);
30 ctx.set_parameter("self_screen_x", child->get_internal_relations().cx);
31 ctx.set_parameter("self_screen_y", child->get_internal_relations().cy);
32 ctx.set_parameter("self_content_width", child->get_internal_relations().cw);
33 ctx.set_parameter("self_content_height", child->get_internal_relations().ch);
34
35#define TO_STRING(...) #__VA_ARGS__
36#define DIMENSIONAL_PARAM(PREFIX, NAME, ...) \
37 ctx.set_parameter(TO_STRING(PREFIX##_##NAME), __VA_ARGS__)
38#define PARENT_PARAM(NAME, ...) DIMENSIONAL_PARAM(parent, NAME, __VA_ARGS__ + 0_px)
39#define SELF_PARAM(NAME, ...) DIMENSIONAL_PARAM(self, NAME, __VA_ARGS__ + 0_px)
40
41 {
43 if (child->parent.has_value()) {
44 auto& [cx, cy, cw, ch] = child->parent.value()->get_internal_relations();
45 PARENT_PARAM(x, cx);
46 PARENT_PARAM(y, cy);
47 PARENT_PARAM(width, cw);
48 PARENT_PARAM(height, ch);
49
50 PARENT_PARAM(top_left_x, cx);
51 PARENT_PARAM(top_left_y, cy);
52 PARENT_PARAM(top_center_x, cx + (cw / 2));
53 PARENT_PARAM(top_center_y, cy);
54 PARENT_PARAM(top_right_x, cx + cw);
55 PARENT_PARAM(top_right_y, cy);
56 PARENT_PARAM(middle_left_x, cx);
57 PARENT_PARAM(middle_left_y, cy + (ch / 2));
58 PARENT_PARAM(center_x, cx + (cw / 2));
59 PARENT_PARAM(center_y, cy + (ch / 2));
60 PARENT_PARAM(middle_right_x, cx + cw);
61 PARENT_PARAM(middle_right_y, cy + (ch / 2));
62 PARENT_PARAM(bottom_left_x, cx);
63 PARENT_PARAM(bottom_left_y, cy + ch);
64 PARENT_PARAM(bottom_center_x, cx + (cw / 2));
65 PARENT_PARAM(bottom_center_y, cy + ch);
66 PARENT_PARAM(bottom_right_x, cx + cw);
67 PARENT_PARAM(bottom_right_y, cy + ch);
68 } else {
69 PARENT_PARAM(x, 0_px);
70 PARENT_PARAM(y, 0_px);
71 PARENT_PARAM(width, 0_px);
72 PARENT_PARAM(height, 0_px);
73
74 PARENT_PARAM(top_left_x, 0_px);
75 PARENT_PARAM(top_left_y, 0_px);
76 PARENT_PARAM(top_center_x, 0_px);
77 PARENT_PARAM(top_center_y, 0_px);
78 PARENT_PARAM(top_right_x, 0_px);
79 PARENT_PARAM(top_right_y, 0_px);
80 PARENT_PARAM(middle_left_x, 0_px);
81 PARENT_PARAM(middle_left_y, 0_px);
82 PARENT_PARAM(center_x, 0_px);
83 PARENT_PARAM(center_y, 0_px);
84 PARENT_PARAM(middle_right_x, 0_px);
85 PARENT_PARAM(middle_right_y, 0_px);
86 PARENT_PARAM(bottom_left_x, 0_px);
87 PARENT_PARAM(bottom_left_y, 0_px);
88 PARENT_PARAM(bottom_center_x, 0_px);
89 PARENT_PARAM(bottom_center_y, 0_px);
90 PARENT_PARAM(bottom_right_x, 0_px);
91 PARENT_PARAM(bottom_right_y, 0_px);
92 }
93 }
94
95 {
97#define PREV_PARAM(NAME, ...) DIMENSIONAL_PARAM(prev, NAME, __VA_ARGS__ + 0_px)
98
99 if (prev.has_value()) {
100 auto dims = prev.value()->get_dimensional_relations();
101 auto& x = dims.x;
102 auto& y = dims.y;
103 auto& w = dims.width;
104 auto& h = dims.height;
105
106 PREV_PARAM(x, x);
107 PREV_PARAM(y, y);
108 PREV_PARAM(width, w);
109 PREV_PARAM(height, h);
110
111 PREV_PARAM(top_left_x, x);
112 PREV_PARAM(top_left_y, y);
113 PREV_PARAM(top_center_x, x + (w / 2));
114 PREV_PARAM(top_center_y, y);
115 PREV_PARAM(top_right_x, x + w);
116 PREV_PARAM(top_right_y, y);
117 PREV_PARAM(middle_left_x, x);
118 PREV_PARAM(middle_left_y, y + (h / 2));
119 PREV_PARAM(center_x, x + (w / 2));
120 PREV_PARAM(center_y, y + (h / 2));
121 PREV_PARAM(middle_right_x, x + w);
122 PREV_PARAM(middle_right_y, y + (h / 2));
123 PREV_PARAM(bottom_left_x, x);
124 PREV_PARAM(bottom_left_y, y + h);
125 PREV_PARAM(bottom_center_x, x + (w / 2));
126 PREV_PARAM(bottom_center_y, y + h);
127 PREV_PARAM(bottom_right_x, x + w);
128 PREV_PARAM(bottom_right_y, y + h);
129 } else {
130 PREV_PARAM(x, 0_px);
131 PREV_PARAM(y, 0_px);
132 PREV_PARAM(width, 0_px);
133 PREV_PARAM(height, 0_px);
134
135 PREV_PARAM(top_left_x, 0_px);
136 PREV_PARAM(top_left_y, 0_px);
137 PREV_PARAM(top_center_x, 0_px);
138 PREV_PARAM(top_center_y, 0_px);
139 PREV_PARAM(top_right_x, 0_px);
140 PREV_PARAM(top_right_y, 0_px);
141 PREV_PARAM(middle_left_x, 0_px);
142 PREV_PARAM(middle_left_y, 0_px);
143 PREV_PARAM(center_x, 0_px);
144 PREV_PARAM(center_y, 0_px);
145 PREV_PARAM(middle_right_x, 0_px);
146 PREV_PARAM(middle_right_y, 0_px);
147 PREV_PARAM(bottom_left_x, 0_px);
148 PREV_PARAM(bottom_left_y, 0_px);
149 PREV_PARAM(bottom_center_x, 0_px);
150 PREV_PARAM(bottom_center_y, 0_px);
151 PREV_PARAM(bottom_right_x, 0_px);
152 PREV_PARAM(bottom_right_y, 0_px);
153 }
154 }
155 }
156 } // namespace cydui::components::anchors
157}
void set_parameter(const std::string &name, V &&getter)
Definition context.cppm:20
#define PARENT_PARAM(NAME,...)
#define PREV_PARAM(NAME,...)
void configure_anchors(std::shared_ptr< component_base_t > child, std::optional< std::shared_ptr< component_base_t > > prev)
dimensions::context< dimensions::screen_measure > dimension_ctx_t