Skip to content

Commit 0de1b3f

Browse files
author
bjn
committed
Tidied up the axis accessing APIs
1 parent 1e045fe commit 0de1b3f

File tree

4 files changed

+99
-87
lines changed

4 files changed

+99
-87
lines changed

lib/include/elements/base_view.hpp

+13-7
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,8 @@ namespace cycfi::elements
7474
float x = 1.0;
7575
float y = 1.0;
7676

77-
constexpr float coord(bool is_x_axis) const {
78-
return is_x_axis ? x : y;
79-
}
80-
81-
constexpr float& coord(bool is_x_axis) {
82-
return is_x_axis ? x : y;
83-
}
77+
constexpr float& operator[](axis a);
78+
constexpr float operator[](axis a) const;
8479
};
8580

8681
////////////////////////////////////////////////////////////////////////////
@@ -398,6 +393,17 @@ namespace cycfi::elements
398393
////////////////////////////////////////////////////////////////////////////
399394
// Scroll direction
400395
point scroll_direction();
396+
397+
////////////////////////////////////////////////////////////////////////////
398+
constexpr float& view_stretch::operator[](axis a)
399+
{
400+
return a==axis::x ? x : y;
401+
}
402+
403+
constexpr float view_stretch::operator[](axis a) const
404+
{
405+
return a==axis::x ? x : y;
406+
}
401407
}
402408

403409
#endif

lib/include/elements/support/point.hpp

+16-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88

99
namespace cycfi { namespace elements
1010
{
11+
////////////////////////////////////////////////////////////////////////////
12+
// Axis
13+
////////////////////////////////////////////////////////////////////////////
14+
enum class axis : bool { x = false, y = true};
15+
16+
inline constexpr axis other(axis a)
17+
{
18+
return axis(not bool(a));
19+
}
20+
1121
////////////////////////////////////////////////////////////////////////////
1222
// Points
1323
////////////////////////////////////////////////////////////////////////////
@@ -24,8 +34,8 @@ namespace cycfi { namespace elements
2434
constexpr point move(float dx, float dy) const;
2535
constexpr point move_to(float x, float y) const;
2636

27-
constexpr float coord(bool is_x_axis) const;
28-
constexpr float& coord(bool is_x_axis);
37+
constexpr float& operator[](axis a);
38+
constexpr float operator[](axis a) const;
2939

3040
float x;
3141
float y;
@@ -83,14 +93,14 @@ namespace cycfi { namespace elements
8393
return r;
8494
}
8595

86-
inline constexpr float point::coord(bool is_x_axis) const
96+
constexpr float& point::operator[](axis a)
8797
{
88-
return is_x_axis ? x : y;
98+
return a==axis::x ? x : y;
8999
}
90100

91-
constexpr float& point::coord(bool is_x_axis)
101+
constexpr float point::operator[](axis a) const
92102
{
93-
return is_x_axis ? x : y;
103+
return a==axis::x ? x : y;
94104
}
95105
}}
96106

lib/include/elements/support/rect.hpp

+32-36
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ namespace cycfi { namespace elements
2727
: rect(origin.x, origin.y, origin.x + size.x, origin.y + size.y)
2828
{}
2929

30-
constexpr rect(bool is_x_axis, float this_min, float other_min, float this_max, float other_max);
31-
3230
rect(rect const &) = default;
3331
constexpr rect& operator=(rect const&) = default;
3432

@@ -55,12 +53,6 @@ namespace cycfi { namespace elements
5553
constexpr rect move_to(float x, float y) const;
5654
constexpr rect inset(float x_inset = 1.0, float y_inset = 1.0) const;
5755

58-
constexpr float axisDelta(bool is_x_axis) const;
59-
constexpr float axisMin(bool is_x_axis ) const;
60-
constexpr float axisMax(bool is_x_axis ) const;
61-
constexpr float& axisMin(bool is_x_axis );
62-
constexpr float& axisMax(bool is_x_axis );
63-
6456
float left;
6557
float top;
6658
float right;
@@ -100,11 +92,6 @@ namespace cycfi { namespace elements
10092
: left(left), top(top), right(right), bottom(bottom)
10193
{}
10294

103-
constexpr rect::rect(bool is_x_axis, float this_min, float other_min, float this_max, float other_max)
104-
: left(is_x_axis ? this_min : other_min), top(not is_x_axis ? this_min : other_min), right(is_x_axis ? this_max : other_max), bottom(not is_x_axis ? this_max : other_max)
105-
{
106-
}
107-
10895
constexpr bool rect::operator==(rect const& other) const
10996
{
11097
return
@@ -221,58 +208,67 @@ namespace cycfi { namespace elements
221208
return r;
222209
}
223210

211+
constexpr bool is_valid(rect r)
212+
{
213+
return (r.left <= r.right) && (r.top <= r.bottom);
214+
}
224215

225-
constexpr float rect::axisDelta(bool is_x_axis) const
216+
constexpr bool is_same_size(rect a, rect b)
226217
{
227-
return is_x_axis ? width() : height();
218+
return (a.width() == b.width()) && (a.height() == b.height());
228219
}
229220

230-
constexpr float rect::axisMin(bool is_x_axis ) const
221+
constexpr point center_point(rect r)
231222
{
232-
return is_x_axis ? left : top;
223+
return {r.left + (r.width() / 2.0f), r.top + (r.height() / 2.0f)};
233224
}
234225

235-
constexpr float rect::axisMax(bool is_x_axis ) const
226+
constexpr float area(rect r)
236227
{
237-
return is_x_axis ? right : bottom;
228+
return r.width() * r.height();
238229
}
239230

240-
constexpr float& rect::axisMin(bool is_x_axis )
231+
constexpr void clear(rect& r)
241232
{
242-
return is_x_axis ? left : top;
233+
r.left = 0.0;
234+
r.top = 0.0;
235+
r.right = 0.0;
236+
r.bottom = 0.0;
243237
}
244238

245-
constexpr float& rect::axisMax(bool is_x_axis )
239+
inline constexpr float axis_delta(rect const &r, axis a)
246240
{
247-
return is_x_axis ? right : bottom;
241+
return a == axis::x ? r.width() : r.height();
248242
}
249243

250-
constexpr bool is_valid(rect r)
244+
inline constexpr float axis_min(rect const &r, axis a)
251245
{
252-
return (r.left <= r.right) && (r.top <= r.bottom);
246+
return a == axis::x ? r.left : r.top;
253247
}
254248

255-
constexpr bool is_same_size(rect a, rect b)
249+
inline constexpr float axis_max(rect const &r, axis a)
256250
{
257-
return (a.width() == b.width()) && (a.height() == b.height());
251+
return a == axis::x ? r.right : r.bottom;
258252
}
259253

260-
constexpr point center_point(rect r)
254+
inline constexpr float& axis_min(rect &r, axis a)
261255
{
262-
return {r.left + (r.width() / 2.0f), r.top + (r.height() / 2.0f)};
256+
return a == axis::x ? r.left : r.top;
263257
}
264258

265-
constexpr float area(rect r)
259+
inline constexpr float& axis_max(rect &r, axis a)
266260
{
267-
return r.width() * r.height();
261+
return a == axis::x ? r.right : r.bottom;
268262
}
269263

270-
constexpr void clear(rect& r)
264+
inline constexpr auto make_rect(axis a, float this_axis_min, float other_axis_min, float this_axis_max, float other_axis_max) -> rect
271265
{
272-
r.left = 0.0;
273-
r.top = 0.0;
274-
r.right = 0.0;
275-
r.bottom = 0.0;
266+
if(a == axis::x) {
267+
return rect(this_axis_min, other_axis_min, this_axis_max, other_axis_max);
268+
}
269+
else {
270+
return rect(other_axis_min, this_axis_min, other_axis_max, this_axis_max);
271+
}
276272
}
277273
}}
278274

lib/src/element/tile.cpp

+38-38
Original file line numberDiff line numberDiff line change
@@ -91,29 +91,29 @@ namespace cycfi::elements
9191
[](layout_info lhs, layout_info rhs){ return lhs.index < rhs.index; });
9292
}
9393

94-
template<bool IS_X_AXIS, class TILE_ELEMENT>
95-
[[gnu::always_inline]] inline auto compute_limits(basic_context const& ctx, const TILE_ELEMENT &tile) -> view_limits
94+
template<axis Axis, class TileElement, axis OtherAxis = other(Axis)>
95+
[[gnu::always_inline]] inline auto compute_limits(basic_context const& ctx, const TileElement &tile) -> view_limits
9696
{
97-
view_limits limits{{0.0, 0.0},
98-
{IS_X_AXIS ? 0.0 : full_extent,
99-
IS_X_AXIS ? full_extent : 0.0}};
100-
for (std::size_t i = 0; i != tile.size(); ++i)
101-
{
102-
auto el = tile.at(i).limits(ctx);
97+
view_limits limits{{0.0, 0.0},
98+
{Axis == axis::x ? 0.0 : full_extent,
99+
Axis == axis::x ? full_extent : 0.0}};
100+
for (std::size_t i = 0; i != tile.size(); ++i)
101+
{
102+
auto el = tile.at(i).limits(ctx);
103103

104-
limits.min.coord(IS_X_AXIS) += el.min.coord(IS_X_AXIS);
105-
limits.max.coord(IS_X_AXIS) += el.max.coord(IS_X_AXIS);
106-
clamp_min(limits.min.coord(! IS_X_AXIS), el.min.coord(! IS_X_AXIS));
107-
clamp_max(limits.max.coord(! IS_X_AXIS), el.max.coord(! IS_X_AXIS));
108-
}
104+
limits.min[Axis] += el.min[Axis];
105+
limits.max[Axis] += el.max[Axis];
106+
clamp_min(limits.min[OtherAxis], el.min[OtherAxis]);
107+
clamp_max(limits.max[OtherAxis], el.max[OtherAxis]);
108+
}
109109

110-
clamp_min(limits.max.coord(! IS_X_AXIS), limits.min.coord(! IS_X_AXIS));
111-
clamp_max(limits.max.coord(IS_X_AXIS), full_extent);
112-
return limits;
110+
clamp_min(limits.max[OtherAxis], limits.min[OtherAxis]);
111+
clamp_max(limits.max[Axis], full_extent);
112+
return limits;
113113
}
114114

115-
template<bool IS_X_AXIS, class TILE_ELEMENT>
116-
[[gnu::always_inline]] inline auto compute_layout(context const& ctx, TILE_ELEMENT &tile, std::vector<float> &tile_offsets) -> void
115+
template<axis Axis, class TileElement, axis OtherAxis = other(Axis)>
116+
[[gnu::always_inline]] inline auto compute_layout(context const& ctx, TileElement &tile, std::vector<float> &tile_offsets) -> void
117117
{
118118
auto const sz = tile.size();
119119

@@ -124,17 +124,17 @@ namespace cycfi::elements
124124
{
125125
auto& elem = tile.at(i);
126126
auto limits = elem.limits(ctx);
127-
info[i].stretch = elem.stretch().coord(IS_X_AXIS);
128-
info[i].min = limits.min.coord(IS_X_AXIS);
129-
info[i].max = limits.max.coord(IS_X_AXIS);
130-
info[i].alloc = limits.min.coord(IS_X_AXIS);
127+
info[i].stretch = elem.stretch()[Axis];
128+
info[i].min = limits.min[Axis];
129+
info[i].max = limits.max[Axis];
130+
info[i].alloc = limits.min[Axis];
131131
info[i].index = i;
132132
}
133133

134-
auto const other_axis_min = ctx.bounds.axisMin(not IS_X_AXIS);
135-
auto const other_axis_max = ctx.bounds.axisMax(not IS_X_AXIS);
136-
auto const my_axis_min = ctx.bounds.axisMin(IS_X_AXIS);
137-
auto const my_axis_delta = ctx.bounds.axisDelta(IS_X_AXIS);
134+
auto const other_axis_min = axis_min(ctx.bounds, OtherAxis);
135+
auto const other_axis_max = axis_max(ctx.bounds, OtherAxis);
136+
auto const my_axis_min = axis_min(ctx.bounds, Axis);
137+
auto const my_axis_delta = axis_delta(ctx.bounds, Axis);
138138
// Compute the best fit for all elements
139139
allocate(my_axis_delta, info);
140140

@@ -149,21 +149,21 @@ namespace cycfi::elements
149149
curr += info[i].alloc;
150150

151151
auto& elem = tile.at(i);
152-
auto ebounds = rect(IS_X_AXIS, prev+my_axis_min, other_axis_min, curr+my_axis_min, other_axis_max);
152+
auto ebounds = make_rect(Axis, prev+my_axis_min, other_axis_min, curr+my_axis_min, other_axis_max);
153153

154154
elem.layout(context{ctx, &elem, ebounds});
155155
}
156156
}
157157

158-
template<bool IS_X_AXIS>
158+
template<axis Axis, axis OtherAxis = other(Axis)>
159159
[[gnu::always_inline]] inline auto compute_bounds_of(rect const& bounds, std::size_t index, const std::vector<float> &tile_offsets) -> rect
160160
{
161161
if (index >= tile_offsets.size())
162162
return {};
163-
auto const other_axis_min = bounds.axisMin(not IS_X_AXIS);
164-
auto const other_axis_max = bounds.axisMax(not IS_X_AXIS);
165-
auto const my_axis_min = bounds.axisMin(IS_X_AXIS);
166-
return rect{IS_X_AXIS, (index? tile_offsets[index-1] : 0)+my_axis_min, other_axis_min, tile_offsets[index]+my_axis_min, other_axis_max};
163+
auto const other_axis_min = axis_min(bounds, OtherAxis);
164+
auto const other_axis_max = axis_max(bounds, OtherAxis);
165+
auto const my_axis_min = axis_min(bounds, Axis);
166+
return make_rect(Axis, (index? tile_offsets[index-1] : 0)+my_axis_min, other_axis_min, tile_offsets[index]+my_axis_min, other_axis_max);
167167
}
168168
}
169169

@@ -172,12 +172,12 @@ namespace cycfi::elements
172172
////////////////////////////////////////////////////////////////////////////
173173
view_limits vtile_element::limits(basic_context const& ctx) const
174174
{
175-
return compute_limits<false>(ctx, *this);
175+
return compute_limits<axis::y>(ctx, *this);
176176
}
177177

178178
void vtile_element::layout(context const& ctx)
179179
{
180-
compute_layout<false>(ctx, *this, _tiles);
180+
compute_layout<axis::y>(ctx, *this, _tiles);
181181
}
182182

183183
void vtile_element::draw(context const& ctx)
@@ -189,20 +189,20 @@ namespace cycfi::elements
189189

190190
rect vtile_element::bounds_of(context const& ctx, std::size_t index) const
191191
{
192-
return compute_bounds_of<false>(ctx.bounds, index, _tiles);
192+
return compute_bounds_of<axis::y>(ctx.bounds, index, _tiles);
193193
}
194194

195195
////////////////////////////////////////////////////////////////////////////
196196
// Horizontal Tiles
197197
////////////////////////////////////////////////////////////////////////////
198198
view_limits htile_element::limits(basic_context const& ctx) const
199199
{
200-
return compute_limits<true>(ctx, *this);
200+
return compute_limits<axis::x>(ctx, *this);
201201
}
202202

203203
void htile_element::layout(context const& ctx)
204204
{
205-
compute_layout<true>(ctx, *this, _tiles);
205+
compute_layout<axis::x>(ctx, *this, _tiles);
206206
}
207207

208208
void htile_element::draw(context const& ctx)
@@ -214,6 +214,6 @@ namespace cycfi::elements
214214

215215
rect htile_element::bounds_of(context const& ctx, std::size_t index) const
216216
{
217-
return compute_bounds_of<true>(ctx.bounds, index, _tiles);
217+
return compute_bounds_of<axis::x>(ctx.bounds, index, _tiles);
218218
}
219219
}

0 commit comments

Comments
 (0)