Skyward boardcore
Loading...
Searching...
No Matches
TextView.h
Go to the documentation of this file.
1/* Copyright (c) 2021 Skyward Experimental Rocketry
2 * Author: Luca Erbetta
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <mxgui/misc_inst.h>
26
27#include "View.h"
28
29namespace Boardcore
30{
31
36class TextView : public View
37{
38public:
46 TextView(std::string text = "", mxgui::Color textColor = mxgui::white,
47 uint8_t padding = 1)
48 : View(), text(text), colText(textColor), padding(padding)
49 {
50 }
51
53 {
54 vertAlign = alignment;
55 invalidate();
56 }
57
59 {
60 horizAlign = alignment;
61 invalidate();
62 }
63
64 void setAlignment(HorizAlignment horizAlign, VertAlignment vertAlign)
65 {
66 setVerticalAlignment(vertAlign);
67 setHorizontalAlignment(horizAlign);
68 }
69
70 void setText(std::string text)
71 {
72 if (text != this->text)
73 {
74 invalidate();
75 this->text = text;
76 }
77 }
78
79 std::string getText() { return text; }
80
81 virtual void setTextColor(mxgui::Color color)
82 {
83 if (color != colText)
84 {
85 this->colText = color;
86 invalidate();
87 }
88 }
89
90 mxgui::Color getTextColor() { return colText; }
91
92 virtual void draw(mxgui::DrawingContext& dc) override
93 {
94 if (isInvalidated())
95 {
96 View::draw(dc);
97
98 Bounds paddedBounds = getBounds();
99 paddedBounds.pos += Position{padding, padding};
100 paddedBounds.size.height -= padding;
101 paddedBounds.size.width -= padding;
102
103 Position topLeft = paddedBounds.topLeft();
104 switch (vertAlign)
105 {
107 topLeft.y =
108 paddedBounds.bottomLeft().y - dc.getFont().getHeight();
109 break;
111 topLeft.y =
112 paddedBounds.pos.y +
113 (paddedBounds.size.height - dc.getFont().getHeight()) /
114 2;
115 break;
117 break;
118 }
119
120 switch (horizAlign)
121 {
123 topLeft.x = paddedBounds.topRight().x - getTextWidth(text);
124 break;
126 topLeft.x =
127 paddedBounds.pos.x +
128 (paddedBounds.size.width - getTextWidth(text)) / 2;
129 break;
131 break;
132 }
133
134 mxgui::Font oldFont = dc.getFont();
135 dc.setFont(font);
136
137 dc.setTextColor(getTextColor(), getBackgroundColor());
138 dc.clippedWrite(topLeft, paddedBounds.topLeft(),
139 paddedBounds.bottomRight(), text);
140
141 dc.setFont(oldFont);
142 }
143 }
144
145 void setFont(mxgui::Font font)
146 {
147 this->font = font;
148 invalidate();
149 }
150
151 mxgui::Font getFont() { return font; }
152
153protected:
154 short int getTextWidth(std::string text)
155 {
156 short int w = 0;
157 for (char c : text)
158 {
159 if (c >= font.getStartChar() && c <= font.getEndChar())
160 {
161 if (font.isFixedWidth())
162 w += font.getWidth();
163 else
164 w += font.getWidths()[c - font.getStartChar()];
165 }
166 }
167 return w;
168 }
169
170private:
171 mxgui::Font font = mxgui::tahoma;
174 std::string text;
175
176 mxgui::Color colText = mxgui::white;
177 uint8_t padding;
178};
179
180} // namespace Boardcore
Simple view to display text on screen.
Definition TextView.h:37
TextView(std::string text="", mxgui::Color textColor=mxgui::white, uint8_t padding=1)
Creates a new TextView.
Definition TextView.h:46
void setHorizontalAlignment(HorizAlignment alignment)
Definition TextView.h:58
virtual void setTextColor(mxgui::Color color)
Definition TextView.h:81
virtual void draw(mxgui::DrawingContext &dc) override
Draw the view in its bounds.
Definition TextView.h:92
std::string getText()
Definition TextView.h:79
void setVerticalAlignment(VertAlignment alignment)
Definition TextView.h:52
mxgui::Color getTextColor()
Definition TextView.h:90
short int getTextWidth(std::string text)
Definition TextView.h:154
mxgui::Font getFont()
Definition TextView.h:151
void setFont(mxgui::Font font)
Definition TextView.h:145
void setAlignment(HorizAlignment horizAlign, VertAlignment vertAlign)
Definition TextView.h:64
void setText(std::string text)
Definition TextView.h:70
Base class for anything that can be drawn on the screen and interacted with.
Definition View.h:126
virtual void draw(mxgui::DrawingContext &dc)
Draw the view in its bounds.
Definition View.h:171
void invalidate()
Signal that what has been previously drawn is now invalid and has to be redrawn.
Definition View.h:148
Bounds getBounds()
Definition View.h:142
bool isInvalidated()
Definition View.h:238
virtual mxgui::Color getBackgroundColor()
Definition View.h:205
This file includes all the types the logdecoder script will decode.
HorizAlignment
Definition View.h:107
VertAlignment
Definition View.h:100
short int x
Definition View.h:44
short int y
Definition View.h:45