Skip to content

Commit 1e045fe

Browse files
author
bjn
committed
Added an axis aware constructor to rect.
Refactored the 'bounds_of' method on vtile and htile to use a common code patht that exploits the axis aware constructor of rect.
1 parent 1441f38 commit 1e045fe

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

lib/include/elements/support/rect.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ 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+
3032
rect(rect const &) = default;
3133
constexpr rect& operator=(rect const&) = default;
3234

@@ -98,6 +100,11 @@ namespace cycfi { namespace elements
98100
: left(left), top(top), right(right), bottom(bottom)
99101
{}
100102

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+
101108
constexpr bool rect::operator==(rect const& other) const
102109
{
103110
return

lib/src/element/tile.cpp

+27-27
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ namespace cycfi::elements
113113
}
114114

115115
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> &tilePos) -> void
116+
[[gnu::always_inline]] inline auto compute_layout(context const& ctx, TILE_ELEMENT &tile, std::vector<float> &tile_offsets) -> void
117117
{
118118
auto const sz = tile.size();
119119

@@ -131,43 +131,53 @@ namespace cycfi::elements
131131
info[i].index = i;
132132
}
133133

134-
auto const otherMin = ctx.bounds.axisMin(not IS_X_AXIS);
135-
auto const otherMax = ctx.bounds.axisMax(not IS_X_AXIS);
136-
auto const myMin = ctx.bounds.axisMin(IS_X_AXIS);
137-
auto const myDelta = ctx.bounds.axisDelta(IS_X_AXIS);
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);
138138
// Compute the best fit for all elements
139-
allocate(myDelta, info);
139+
allocate(my_axis_delta, info);
140140

141141
// Now we have the final layout. We can now layout the individual
142142
// elements.
143-
tilePos.resize(sz);
143+
tile_offsets.resize(sz);
144144
auto curr = 0.0f;
145145
for (std::size_t i = 0; i != sz; ++i)
146146
{
147-
tilePos[i] = curr + info[i].alloc;
147+
tile_offsets[i] = curr + info[i].alloc;
148148
auto const prev = curr;
149149
curr += info[i].alloc;
150150

151151
auto& elem = tile.at(i);
152-
auto ebounds = IS_X_AXIS
153-
? rect{prev+myMin, otherMin, curr+myMin, otherMax}
154-
: rect{otherMin, prev+myMin, otherMax, curr+myMin};
152+
auto ebounds = rect(IS_X_AXIS, prev+my_axis_min, other_axis_min, curr+my_axis_min, other_axis_max);
153+
155154
elem.layout(context{ctx, &elem, ebounds});
156155
}
157156
}
157+
158+
template<bool IS_X_AXIS>
159+
[[gnu::always_inline]] inline auto compute_bounds_of(rect const& bounds, std::size_t index, const std::vector<float> &tile_offsets) -> rect
160+
{
161+
if (index >= tile_offsets.size())
162+
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};
167+
}
158168
}
159169

160170
////////////////////////////////////////////////////////////////////////////
161171
// Vertical Tiles
162172
////////////////////////////////////////////////////////////////////////////
163173
view_limits vtile_element::limits(basic_context const& ctx) const
164174
{
165-
return compute_limits<false>(ctx, *this);
175+
return compute_limits<false>(ctx, *this);
166176
}
167177

168178
void vtile_element::layout(context const& ctx)
169179
{
170-
compute_layout<false>(ctx, *this, _tiles);
180+
compute_layout<false>(ctx, *this, _tiles);
171181
}
172182

173183
void vtile_element::draw(context const& ctx)
@@ -179,25 +189,20 @@ namespace cycfi::elements
179189

180190
rect vtile_element::bounds_of(context const& ctx, std::size_t index) const
181191
{
182-
if (index >= _tiles.size())
183-
return {};
184-
auto const left = ctx.bounds.left;
185-
auto const right = ctx.bounds.right;
186-
auto const top = ctx.bounds.top;
187-
return rect{left, (index? _tiles[index-1] : 0)+top, right, _tiles[index]+top};
192+
return compute_bounds_of<false>(ctx.bounds, index, _tiles);
188193
}
189194

190195
////////////////////////////////////////////////////////////////////////////
191196
// Horizontal Tiles
192197
////////////////////////////////////////////////////////////////////////////
193198
view_limits htile_element::limits(basic_context const& ctx) const
194199
{
195-
return compute_limits<true>(ctx, *this);
200+
return compute_limits<true>(ctx, *this);
196201
}
197202

198203
void htile_element::layout(context const& ctx)
199204
{
200-
compute_layout<true>(ctx, *this, _tiles);
205+
compute_layout<true>(ctx, *this, _tiles);
201206
}
202207

203208
void htile_element::draw(context const& ctx)
@@ -209,11 +214,6 @@ namespace cycfi::elements
209214

210215
rect htile_element::bounds_of(context const& ctx, std::size_t index) const
211216
{
212-
if (index >= _tiles.size())
213-
return {};
214-
auto const top = ctx.bounds.top;
215-
auto const bottom = ctx.bounds.bottom;
216-
auto const left = ctx.bounds.left;
217-
return rect{(index? _tiles[index-1] : 0)+left, top, _tiles[index]+left, bottom};
217+
return compute_bounds_of<true>(ctx.bounds, index, _tiles);
218218
}
219219
}

0 commit comments

Comments
 (0)