-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextWidget.h
144 lines (110 loc) · 3.38 KB
/
TextWidget.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#pragma once
#include "HUDWidget.h"
class TextWidget :public HUDWidget
{
public:
TextWidget()
{
}
~TextWidget()
{
}
virtual void Tick(float DeltaTime) override
{
HUDWidget::Tick(DeltaTime);
if (!m_visible)return;
auto& transform = m_EntityComponentSystemManager->GetComponent<TransformComponent2D>(m_ID);
//std::cout << m_Brush->GetColor().a;
//std::cout << transform.scale.m_x;
D2D1_POINT_2F origin = D2D1::Point2F(
static_cast<FLOAT>(transform.position.m_x- transform.scale.m_x / 2),
static_cast<FLOAT>(transform.position.m_y- transform.scale.m_y / 2)
);
HUD::GetHUDSystem()->DrawTextOnScreen( origin, m_text_layout, m_Brush);
//std::cout << "ok";
}
virtual void BeginPlay() override
{
HUDWidget::BeginPlay();
}
void SetText(const wchar_t* text )
{
m_text = nullptr;
m_text = text;
//CreateTextFont();
}
void UpdateText(const wchar_t* text)
{
m_text = nullptr;
m_text = text;
CreateTextFont();
}
void UpdateText(int number)
{
wchar_t string[10];
_snwprintf_s(string, sizeof(string), L"%d", number);
m_text = nullptr;
m_text = string;
CreateTextFont();
}
void SetFontSize(float font_size)
{
m_font_size = font_size;
}
void CreateColor(Vector3D brush_color)
{
D2D1_COLOR_F color = D2D1::ColorF(brush_color.m_x, brush_color.m_y, brush_color.m_z, 1);
HUD::GetHUDSystem()->GetRenderTarget()->CreateSolidColorBrush(
color,
&m_Brush
);
}
void UpdateColor(Vector3D brush_color)
{
D2D1_COLOR_F color = D2D1::ColorF(brush_color.m_x, brush_color.m_y, brush_color.m_z, 1);
HUD::GetHUDSystem()->GetRenderTarget()->CreateSolidColorBrush(
color,
&m_Brush
);
CreateTextFont();
}
void CreateTextFont()
{
m_text_format = NULL;
HUD::GetHUDSystem()->GetWriteFactory()->CreateTextFormat(
L"Bauhaus 93", // Font name.
NULL,
DWRITE_FONT_WEIGHT_REGULAR,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
m_font_size, //Font size
L"en-us",
&m_text_format
);
FLOAT dpiX, dpiY;
dpiX = (FLOAT)GetDpiForWindow(GetDesktopWindow());
dpiY = dpiX;
auto& transform = m_EntityComponentSystemManager->GetComponent<TransformComponent2D>(m_ID);
transform.scale.m_x = transform.scale.m_x + int(HUD::GetHUDSystem()->GetScreenRect().right / dpiX);
transform.scale.m_y = transform.scale.m_y + int(HUD::GetHUDSystem()->GetScreenRect().bottom / dpiY);
m_text_format->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
m_text_format->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
UINT32 textLength;
textLength = (UINT32)wcslen(m_text);
if (m_text_format != NULL)
HUD::GetHUDSystem()->GetWriteFactory()->CreateTextLayout(
m_text, // The string to be laid out and formatted.
textLength, // The length of the string.
m_text_format, // The text format to apply to the string
FLOAT(transform.scale.m_x), // The width of the layout box.
FLOAT(transform.scale.m_y), // The height of the layout box.
&m_text_layout // The IDWriteTextLayout interface pointer.
);
}
private:
const wchar_t* m_text = nullptr;
float m_font_size = 0;
ID2D1SolidColorBrush* m_Brush = NULL;
IDWriteTextFormat* m_text_format{};
IDWriteTextLayout* m_text_layout{};
};