stackchan-display
Loading...
Searching...
No Matches
SpeechBalloon.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#pragma once
6#include <M5Unified.h>
7
8#include "Drawable.h"
9#include "ColorPalette.h"
10
11#ifndef ARDUINO
12#include <string>
13typedef std::string String;
14#endif // ARDUINO
15
16const int16_t TEXT_HEIGHT = 8;
17const int16_t TEXT_SIZE = 2;
18const int16_t MIN_WIDTH = 40;
19const int cx = 240;
20const int cy = 220;
21
22namespace stackchan::display
23{
24 class SpeechBalloon final : public Drawable
25 {
26 protected:
27 std::string text_;
28 const lgfx::IFont *font_;
29
30 public:
31 // constructor
32 SpeechBalloon() = default;
33 ~SpeechBalloon() = default;
34 SpeechBalloon(const SpeechBalloon &other) = default;
35 SpeechBalloon &operator=(const SpeechBalloon &other) = default;
36
37 void setText(const std::string &text)
38 {
39 text_ = text;
40 }
41 void draw(M5Canvas &canvas) override
42 {
43 // This method is intentionally left empty. The actual drawing logic is implemented in the overloaded draw method that takes a ColorPalette parameter.
44 }
45
46 void draw(M5Canvas &canvas, ColorPalette &cp)
47 {
48 if (text_.length() == 0)
49 {
50 return;
51 }
52
53 uint16_t primaryColor = canvas.getColorDepth() == 1
54 ? 1
55 : cp.get(DrawingLocation::kBalloonForeground);
56 uint16_t backgroundColor = canvas.getColorDepth() == 1
57 ? 0
58 : cp.get(DrawingLocation::kBalloonBackground);
59 M5.Lcd.setTextSize(TEXT_SIZE);
60 M5.Lcd.setTextDatum(MC_DATUM);
61 canvas.setTextSize(TEXT_SIZE);
62 canvas.setTextColor(primaryColor, backgroundColor);
63 canvas.setTextDatum(MC_DATUM);
64 M5.Lcd.setFont(font_);
65 int textWidth = M5.Lcd.textWidth(text_.c_str());
66 int textHeight = TEXT_HEIGHT * TEXT_SIZE;
67 canvas.fillEllipse(cx - 20, cy, textWidth + 2, textHeight * 2 + 2,
68 primaryColor);
69 canvas.fillTriangle(cx - 62, cy - 42, cx - 8, cy - 10, cx - 41, cy - 8,
70 primaryColor);
71 canvas.fillEllipse(cx - 20, cy, textWidth, textHeight * 2,
72 backgroundColor);
73 canvas.fillTriangle(cx - 60, cy - 40, cx - 10, cy - 10, cx - 40, cy - 10,
74 backgroundColor);
75 canvas.drawString(text_.c_str(), cx - textWidth / 6 - 15, cy, font_); // Continue printing from new x position
76 }
77 };
78
79} // namespace stackchan::display
Definition ColorPalette.h:48
Definition Drawable.h:45
Definition SpeechBalloon.h:25