TinyMaterialDesign
Loading...
Searching...
No Matches
RadialLayout.h
Go to the documentation of this file.
1#pragma once
2#include <math.h>
3
4#include "TinyMaterialDesign/Core/Bounds.h"
5
6namespace tinymd {
7
8/**
9 * @brief Places item rects evenly spaced around a circle.
10 *
11 * Not a widget - see GridLayout.h for the rationale. Useful for round
12 * displays (watch-face style dials/menus) where LinearLayout/GridLayout's
13 * rectilinear packing doesn't fit the screen shape:
14 *
15 * RadialLayout dial(Bounds(0, 0, 240, 240), 90); // radius from center
16 * for (int i = 0; i < 6; ++i) items[i].bounds = dial.itemRect(i, 6, 36, 36);
17 *
18 * Angle 0 points up (12 o'clock) and items are placed clockwise, matching
19 * how a clock/dial reads; pass a non-zero `startDegrees` to rotate the
20 * whole ring.
21 */
23 public:
24 RadialLayout(Bounds container, int32_t radius, float startDegrees = 0.0f)
25 : container_(container), radius_(radius), startDegrees_(startDegrees) {}
26
27 /// Bounds for the `index`-th of `count` items, each `width` x `height`,
28 /// centered on a point evenly spaced around the circle.
29 Bounds itemRect(int index, int count, int32_t width, int32_t height) const {
30 if (count <= 0) count = 1;
31 const float degrees = startDegrees_ + 360.0f * static_cast<float>(index) / static_cast<float>(count);
32 const float radians = degrees * (3.14159265358979323846f / 180.0f);
33 const int32_t cx = container_.centerX() + static_cast<int32_t>(radius_ * sinf(radians));
34 const int32_t cy = container_.centerY() - static_cast<int32_t>(radius_ * cosf(radians));
35 return Bounds(cx - width / 2, cy - height / 2, width, height);
36 }
37
38 private:
39 Bounds container_;
40 int32_t radius_;
41 float startDegrees_;
42};
43
44} // namespace tinymd
Places item rects evenly spaced around a circle.
Definition: RadialLayout.h:22
Bounds itemRect(int index, int count, int32_t width, int32_t height) const
Definition: RadialLayout.h:29
RadialLayout(Bounds container, int32_t radius, float startDegrees=0.0f)
Definition: RadialLayout.h:24
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
Bounds(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: Bounds.h:28
int32_t centerY() const
Definition: Bounds.h:33
int32_t centerX() const
Definition: Bounds.h:32