m5stack-avatar
Loading...
Searching...
No Matches
Effect.h
1// Copyright (c) Shinya Ishikawa. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full
3// license information.
4
5#ifndef EFFECT_H_
6#define EFFECT_H_
7#define LGFX_USE_V1
8#include <M5GFX.h>
9
10#include "DrawContext.h"
11#include "Drawable.h"
12
13namespace m5avatar {
14
15class Effect final : public Drawable {
16 private:
17 void drawBubbleMark(M5Canvas *spi, uint32_t x, uint32_t y, uint32_t r,
18 uint16_t color) {
19 drawBubbleMark(spi, x, y, r, color, 0);
20 }
21
22 void drawBubbleMark(M5Canvas *spi, uint32_t x, uint32_t y, uint32_t r,
23 uint16_t color, float offset) {
24 r = r + floor(r * 0.2 * offset);
25 spi->drawCircle(x, y, r, color);
26 spi->drawCircle(x - (r / 4), y - (r / 4), r / 4, color);
27 }
28
29 void drawSweatMark(M5Canvas *spi, uint32_t x, uint32_t y, uint32_t r,
30 uint16_t color) {
31 drawSweatMark(spi, x, y, r, color, 0);
32 }
33
34 void drawSweatMark(M5Canvas *spi, uint32_t x, uint32_t y, uint32_t r,
35 uint16_t color, float offset) {
36 y = y + floor(5 * offset);
37 r = r + floor(r * 0.2 * offset);
38 spi->fillCircle(x, y, r, color);
39 uint32_t a = (sqrt(3) * r) / 2;
40 spi->fillTriangle(x, y - r * 2, x - a, y - r * 0.5, x + a, y - r * 0.5,
41 color);
42 }
43
44 void drawChillMark(M5Canvas *spi, uint32_t x, uint32_t y, uint32_t r,
45 uint16_t color) {
46 drawChillMark(spi, x, y, r, color, 0);
47 }
48
49 void drawChillMark(M5Canvas *spi, uint32_t x, uint32_t y, uint32_t r,
50 uint16_t color, float offset) {
51 uint32_t h = r + abs(r * 0.2 * offset);
52 spi->fillRect(x - (r / 2), y, 3, h / 2, color);
53 spi->fillRect(x, y, 3, h * 3 / 4, color);
54 spi->fillRect(x + (r / 2), y, 3, h, color);
55 }
56
57 void drawAngerMark(M5Canvas *spi, uint32_t x, uint32_t y, uint32_t r,
58 uint16_t color, uint32_t bColor) {
59 drawAngerMark(spi, x, y, r, color, bColor, 0);
60 }
61
62 void drawAngerMark(M5Canvas *spi, uint32_t x, uint32_t y, uint32_t r,
63 uint16_t color, uint16_t bColor, float offset) {
64 r = r + abs(r * 0.4 * offset);
65 spi->fillRect(x - (r / 3), y - r, (r * 2) / 3, r * 2, color);
66 spi->fillRect(x - r, y - (r / 3), r * 2, (r * 2) / 3, color);
67 spi->fillRect(x - (r / 3) + 2, y - r, ((r * 2) / 3) - 4, r * 2, bColor);
68 spi->fillRect(x - r, y - (r / 3) + 2, r * 2, ((r * 2) / 3) - 4, bColor);
69 }
70
71 void drawHeartMark(M5Canvas *spi, uint32_t x, uint32_t y, uint32_t r,
72 uint16_t color) {
73 drawHeartMark(spi, x, y, r, color, 0);
74 }
75
76 void drawHeartMark(M5Canvas *spi, uint32_t x, uint32_t y, uint32_t r,
77 uint16_t color, float offset) {
78 r = r + floor(r * 0.4 * offset);
79 spi->fillCircle(x - r / 2, y, r / 2, color);
80 spi->fillCircle(x + r / 2, y, r / 2, color);
81 float a = (sqrt(2) * r) / 4.0;
82 spi->fillTriangle(x, y, x - r / 2 - a, y + a, x + r / 2 + a, y + a, color);
83 spi->fillTriangle(x, y + (r / 2) + 2 * a, x - r / 2 - a, y + a,
84 x + r / 2 + a, y + a, color);
85 }
86
87 public:
88 // constructor
89 Effect() = default;
90 ~Effect() = default;
91 Effect(const Effect &other) = default;
92 Effect &operator=(const Effect &other) = default;
93 void draw(M5Canvas *spi, BoundingRect rect, DrawContext *ctx) override {
94 uint16_t primaryColor = ctx->getColorDepth() == 1
95 ? 1
96 : ctx->getColorPalette()->get(COLOR_PRIMARY);
97 uint16_t bgColor = ctx->getColorDepth() == 1
98 ? ERACER_COLOR
99 : ctx->getColorPalette()->get(COLOR_BACKGROUND);
100 float offset = ctx->getBreath();
101 Expression exp = ctx->getExpression();
102 switch (exp) {
103 case Expression::kDoubt:
104 drawSweatMark(spi, 290, 110, 7, primaryColor, -offset);
105 break;
106 case Expression::kAngry:
107 drawAngerMark(spi, 280, 50, 12, primaryColor, bgColor, offset);
108 break;
109 case Expression::kHappy:
110 drawHeartMark(spi, 280, 50, 12, primaryColor, offset);
111 break;
112 case Expression::kSad:
113 drawChillMark(spi, 270, 0, 30, primaryColor, offset);
114 break;
115 case Expression::kSleepy:
116 drawBubbleMark(spi, 290, 40, 10, primaryColor, offset);
117 drawBubbleMark(spi, 270, 52, 6, primaryColor, -offset);
118 break;
119 default:
120 // noop
121 break;
122 }
123 }
124};
125
126} // namespace m5avatar
127
128#endif // EFFECT_H_
Definition: BoundingRect.h:10
Definition: DrawContext.h:22
Definition: Drawable.h:13
Definition: Effect.h:15