Power System Platform  2026w23a-beta
Loading...
Searching...
No Matches
GCText.cpp
1/*
2 * Copyright (C) 2024 Thales Lima Oliveira <thales@ufu.br>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#include "GCText.h"
19#include <wx/dcscreen.h>
20#include <wx/dcmemory.h>
21#include <wx/dcgraph.h>
22#include <wx/dcclient.h>
23#include <wx/graphics.h>
24
25GCText::GCText() {}
26GCText::GCText(wxString text)
27{
28 SetText(text);
29}
30
31GCText::~GCText()
32{
33}
34
35void GCText::Draw(wxPoint2DDouble position, wxGraphicsContext* gc, double angle, wxColour colour) const
36{
37 //gc->SetFont(wxFont(m_fontSize, m_fontFamily, m_fontStyle, m_fontWeight), colour);
38
39 gc->SetFont(m_font, colour);
40 gc->DrawText(m_text, position.m_x, position.m_y, angle);
41}
42
43void GCText::Draw(wxPoint2DDouble position, double width, double height, wxDC& dc, double angle, wxColour colour) const
44{
45 dc.SetFont(m_font);
46 dc.SetPen(wxPen(colour));
47 dc.SetTextForeground(colour);
48
49 if(angle == 0.0) {
50 dc.DrawText(m_text, position.m_x - width / 2.0, position.m_y - height / 2.0);
51 return;
52 }
53
54 double rad = wxDegToRad(angle);
55
56 double dx = -width / 2.0;
57 double dy = -height / 2.0;
58
59 double rx = dx * cos(rad) - dy * sin(rad);
60 double ry = dx * sin(rad) + dy * cos(rad);
61
62 double drawX = position.m_x + rx;
63 double drawY = position.m_y + ry;
64
65 dc.DrawRotatedText(m_text, drawX, drawY, -angle);
66}
67
68void GCText::SetText(wxString text)
69{
70 m_text = text;
71 if (!m_customFont) {
72 m_font = wxFont(m_fontSize,
73 wxFONTFAMILY_DEFAULT,
74 wxFONTSTYLE_NORMAL,
75 wxFONTWEIGHT_NORMAL,
76 false,
77 wxT("Arial"));
78 }
79
80
81 wxScreenDC dc;
82 dc.SetFont(m_font);
83 m_size = dc.GetTextExtent(m_text);
84}
85
86void GCText::SetText(wxString text, wxSize size)
87{
88 m_text = text;
89 m_size = size;
90}
91
92void GCText::SetFont(wxFont font)
93{
94 m_font = font;
95 m_customFont = true;
96 SetText(m_text); // Recalculate the size with the new font
97}
98
100{
101 GCText* copy = new GCText();
102 *copy = *this;
103 return copy;
104}
Class to draw text on Graphics Context using wxWidgets.
Definition GCText.h:32
virtual GCText * GetCopy()
Get a deep text copy.
Definition GCText.cpp:99
virtual void Draw(wxPoint2DDouble position, wxGraphicsContext *gc, double angle=0.0, wxColour colour= *wxBLACK) const
Draw the text in wxGraphicsContext.
Definition GCText.cpp:35
virtual void SetText(wxString text)
Set correctly a new text string.
Definition GCText.cpp:68