m5stack-avatar
Loading...
Searching...
No Matches
DogFace.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 FACES_DOGFACE_H_
6#define FACES_DOGFACE_H_
7
8#include <M5Unified.h> // TODO(meganetaaan): include only the Sprite function not a whole library
9
10#include "../BoundingRect.h"
11#include "../DrawContext.h"
12#include "../Drawable.h"
13#include "../Face.h"
14
15namespace m5avatar {
16class DogEye : public Drawable {
17 void draw(M5Canvas *spi, BoundingRect rect, DrawContext *ctx) {
18 uint32_t cx = rect.getCenterX();
19 uint32_t cy = rect.getCenterY();
20 Gaze g = ctx->getLeftGaze();
21 ColorPalette *cp = ctx->getColorPalette();
22 uint16_t primaryColor =
23 ctx->getColorDepth() == 1 ? 1 : cp->get(COLOR_PRIMARY);
24 uint16_t backgroundColor = ctx->getColorDepth() == 1
25 ? ERACER_COLOR
26 : cp->get(COLOR_BACKGROUND);
27 uint32_t offsetX = g.getHorizontal() * 8;
28 uint32_t offsetY = g.getVertical() * 5;
29 float eor = ctx->getLeftEyeOpenRatio();
30
31 if (eor == 0) {
32 // eye closed
33 spi->fillRect(cx - 15, cy - 2, 30, 4, primaryColor);
34 return;
35 }
36 spi->fillEllipse(cx, cy, 30, 25, primaryColor);
37 spi->fillEllipse(cx, cy, 28, 23, backgroundColor);
38
39 spi->fillEllipse(cx + offsetX, cy + offsetY, 18, 18, primaryColor);
40 spi->fillEllipse(cx + offsetX - 3, cy + offsetY - 3, 3, 3,
41 backgroundColor);
42 }
43};
44
45class DogMouth : public Drawable {
46 private:
47 uint16_t minWidth;
48 uint16_t maxWidth;
49 uint16_t minHeight;
50 uint16_t maxHeight;
51
52 public:
53 DogMouth() : DogMouth(50, 90, 4, 60) {}
54 DogMouth(uint16_t minWidth, uint16_t maxWidth, uint16_t minHeight,
55 uint16_t maxHeight)
56 : minWidth{minWidth},
57 maxWidth{maxWidth},
58 minHeight{minHeight},
59 maxHeight{maxHeight} {}
60 void draw(M5Canvas *spi, BoundingRect rect, DrawContext *ctx) {
61 uint16_t primaryColor =
62 ctx->getColorDepth() == 1
63 ? 1
64 : ctx->getColorPalette()->get(COLOR_PRIMARY);
65 uint16_t backgroundColor =
66 ctx->getColorDepth() == 1
67 ? ERACER_COLOR
68 : ctx->getColorPalette()->get(COLOR_BACKGROUND);
69 uint32_t cx = rect.getCenterX();
70 uint32_t cy = rect.getCenterY();
71 float openRatio = ctx->getMouthOpenRatio();
72 uint32_t h = minHeight + (maxHeight - minHeight) * openRatio;
73 uint32_t w = minWidth + (maxWidth - minWidth) * (1 - openRatio);
74 if (h > minHeight) {
75 spi->fillEllipse(cx, cy, w / 2, h / 2, primaryColor);
76 spi->fillEllipse(cx, cy, w / 2 - 4, h / 2 - 4, TFT_RED);
77 spi->fillRect(cx - w / 2, cy - h / 2, w, h / 2, backgroundColor);
78 }
79 spi->fillEllipse(cx, cy - 15, 10, 6, primaryColor);
80 spi->fillEllipse(cx - 28, cy, 30, 15, primaryColor);
81 spi->fillEllipse(cx + 28, cy, 30, 15, primaryColor);
82 spi->fillEllipse(cx - 29, cy - 4, 27, 15, backgroundColor);
83 spi->fillEllipse(cx + 29, cy - 4, 27, 15, backgroundColor);
84 }
85};
86
87class DogFace : public Face {
88 public:
89 DogFace()
90 : Face(new DogMouth(), new BoundingRect(168, 163), new DogEye(),
91 new BoundingRect(103, 80), new DogEye(),
92 new BoundingRect(106, 240), new Eyeblow(15, 2, false),
93 new BoundingRect(67, 96), new Eyeblow(15, 2, true),
94 new BoundingRect(72, 230)) {}
95};
96
97} // namespace m5avatar
98
99#endif // FACES_DOGFACE_H_
Definition: BoundingRect.h:10
Definition: ColorPalette.h:50
Definition: DogFace.h:16
Definition: DogFace.h:87
Definition: DogFace.h:45
Definition: DrawContext.h:22
Definition: Drawable.h:13
Definition: Eyeblow.h:15
Definition: Face.h:18
Definition: Gaze.h:9