stackchan-display
Loading...
Searching...
No Matches
Drawable.h
1#pragma once
2#include <M5GFX.h>
3#include <M5Unified.h>
4
5#include "Vector.h"
6
7// reference:
8// https://github.com/m5stack/StackChan/blob/main/firmware/main/stackchan/avatar/avatar/elements/element.h
9
10namespace stackchan::display
11{
12 template <typename T>
13 T clamp(T value, T min, T max)
14 {
15 if (value < min)
16 {
17 return min;
18 }
19 if (value > max)
20 {
21 return max;
22 }
23 return value;
24 }
25
26 template <typename T>
27 T remap(T x, T in_min, T in_max, T out_min, T out_max, bool clamp = false)
28 {
29 if (clamp)
30 {
31 if (x < in_min)
32 {
33 return out_min;
34 }
35 if (in_max < x)
36 {
37 return out_max;
38 }
39 }
40
41 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
42 }
43
45 {
46 protected:
47 m5::Vector2i position_{0, 0}; // in pixels, with positive x to the right and positive y downwards
48 int rotation_ = 0; // in degrees, counter-clockwise
49 m5::Size2i size_{100, 100}; // width and height, in pixels
50 bool visible_ = true;
51 bool ignore_expression_ = false;
52
53 public:
54 Drawable() = default;
55 Drawable(int16_t x, int16_t y, int16_t width, int16_t height) : position_(x, y), size_(width, height) {};
56
57 virtual ~Drawable() = default;
58
64 virtual void setPosition(const m5::Vector2i &position)
65 {
66 position_ = position;
67 }
68 virtual m5::Vector2i &getPosition()
69 {
70 return position_;
71 }
72
78 virtual void setRotation(int rotation)
79 {
80 rotation_ = rotation;
81 }
87 virtual int getRotation()
88 {
89 return rotation_;
90 }
91
92 virtual void setSize(const m5::Size2i &size)
93 {
94 size_ = size;
95 size_.width = clamp(size_.width, 1, 200);
96 size_.height = clamp(size_.height, 1, 200);
97 }
98
99 virtual m5::Size2i &getSize()
100 {
101 return size_;
102 }
103
104 virtual void setVisible(bool visible)
105 {
106 visible_ = visible;
107 }
108 virtual bool getVisible()
109 {
110 return visible_;
111 }
112 virtual bool isVisible()
113 {
114 return visible_;
115 }
116
117 virtual void setIgnoreExpression(bool ignore)
118 {
119 ignore_expression_ = ignore;
120 }
121 virtual bool getIgnoreExpression()
122 {
123 return ignore_expression_;
124 }
125 virtual bool isIgnoreExpression()
126 {
127 return ignore_expression_;
128 }
129
130 virtual void draw(M5Canvas &canvas) {}
131
132 virtual void update() {}
133 };
134
135}
Definition Vector.h:105
Definition Vector.h:9
Definition Drawable.h:45
virtual int getRotation()
Get the Rotation in degrees, counter-clockwise.
Definition Drawable.h:87
virtual void setRotation(int rotation)
Definition Drawable.h:78
virtual void setPosition(const m5::Vector2i &position)
set primary position
Definition Drawable.h:64