m5stack-avatar
Loading...
Searching...
No Matches
Balloon.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 BALLOON_H_
6#define BALLOON_H_
7#define LGFX_USE_V1
8#include <M5Unified.h>
9#include "DrawContext.h"
10#include "Drawable.h"
11
12#ifndef ARDUINO
13#include <string>
14typedef std::string String;
15#endif // ARDUINO
16
17const int16_t TEXT_HEIGHT = 8;
18const int16_t TEXT_SIZE = 2;
19const int16_t MIN_WIDTH = 40;
20const int cx = 240;
21const int cy = 220;
22
23namespace m5avatar {
24class Balloon final : public Drawable {
25 public:
26 // constructor
27 Balloon() = default;
28 ~Balloon() = default;
29 Balloon(const Balloon &other) = default;
30 Balloon &operator=(const Balloon &other) = default;
31 void draw(M5Canvas *spi, BoundingRect rect,
32 DrawContext *drawContext) override {
33 String text = drawContext->getspeechText();
34 const lgfx::IFont *font = drawContext->getSpeechFont();
35 if (text.length() == 0) {
36 return;
37 }
38 ColorPalette* cp = drawContext->getColorPalette();
39 uint16_t primaryColor = cp->get(COLOR_BALLOON_FOREGROUND);
40 uint16_t backgroundColor = cp->get(COLOR_BALLOON_BACKGROUND);
41 M5.Lcd.setTextSize(TEXT_SIZE);
42 M5.Lcd.setTextDatum(MC_DATUM);
43 spi->setTextSize(TEXT_SIZE);
44 spi->setTextColor(primaryColor, backgroundColor);
45 spi->setTextDatum(MC_DATUM);
46 M5.Lcd.setFont(font);
47 int textWidth = M5.Lcd.textWidth(text.c_str());
48 int textHeight = TEXT_HEIGHT * TEXT_SIZE;
49 spi->fillEllipse(cx - 20, cy,textWidth + 2, textHeight * 2 + 2,
50 primaryColor);
51 spi->fillTriangle(cx - 62, cy - 42, cx - 8, cy - 10, cx - 41, cy - 8,
52 primaryColor);
53 spi->fillEllipse(cx - 20, cy, textWidth, textHeight * 2,
54 backgroundColor);
55 spi->fillTriangle(cx - 60, cy - 40, cx - 10, cy - 10, cx - 40, cy - 10,
56 backgroundColor);
57 spi->drawString(text.c_str(), cx - textWidth / 6 - 15, cy, font); // Continue printing from new x position
58 }
59};
60
61} // namespace m5avatar
62
63#endif // BALLOON_H_
Definition: Balloon.h:24
Definition: BoundingRect.h:10
Definition: ColorPalette.h:50
Definition: DrawContext.h:22
Definition: Drawable.h:13