-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringDrawer.cpp
More file actions
201 lines (170 loc) · 6.84 KB
/
Copy pathStringDrawer.cpp
File metadata and controls
201 lines (170 loc) · 6.84 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "StringDrawer.h"
#ifdef GD_EXTENSION_GODOCTOPUS
#include <godot_cpp/variant/utility_functions.hpp>
#include <godot_cpp/classes/rendering_server.hpp>
#else
#include "servers/rendering_server.h"
#include <scene/theme/theme_db.h>
#include <scene/main/window.h>
#endif
#include <cmath>
namespace godot
{
void StringDrawer::_notification(int p_notification)
{
switch (p_notification) {
case NOTIFICATION_PROCESS: {
_process(get_process_delta_time());
} break;
case NOTIFICATION_DRAW: {
_draw();
} break;
case NOTIFICATION_READY: {
_ready();
set_process(true);
set_physics_process(true);
} break;
}
}
StringDrawer::~StringDrawer() {}
void StringDrawer::_ready()
{
if(!_ref_camera_path.is_empty())
_ref_camera = (Camera2D*)get_node(_ref_camera_path);
}
Size2i get_size(Viewport const *viewport)
{
Window const * wi = dynamic_cast<Window const *>(viewport);
SubViewport const * sv = dynamic_cast<SubViewport const *>(viewport);
if(wi)
{
return wi->get_size();
}
if(sv)
{
return sv->get_size();
}
return Size2i();
}
Vector2 get_cam_top_left(Camera2D * camera)
{
Size2i viewport_size = get_size(camera->get_viewport());
auto cam_size = Vector2(viewport_size.x, viewport_size.y) / camera->get_zoom().x;
return camera->get_position() - cam_size / 2.;
}
Vector2 get_screen_pos(Camera2D * camera, Vector2 const &world_pos)
{
auto viewport_pos = (world_pos - get_cam_top_left(camera)) * camera->get_zoom().x;
auto window_size = DisplayServer::get_singleton()->window_get_size();
Size2i viewport_size = get_size(camera->get_viewport());
auto scale_size = Vector2(float(viewport_size.x) / window_size.x, float(viewport_size.y) / window_size.y);
auto screen_pos = viewport_pos / scale_size;
return screen_pos;
}
void StringDrawer::_draw()
{
std::lock_guard<std::mutex> lock(mutex);
std::vector<size_t> ended_instances;
instances.for_each_const([this, &ended_instances](StringInstance const &instance, size_t idx) {
Vector2 pos = instance.position;
Color color = instance.color;
if(instance.floating)
{
double delta = elapsed_time - instance.spawn_time;
pos += Vector2(std::cos(delta * 3.14)*oscillation_factor, -delta*up_speed);
color.a = 1. - std::max(0., (delta - float_time)/fade_time);
if(color.a < 1e-5)
{
ended_instances.push_back(idx);
}
}
// adjust from camera if available
if(_ref_camera)
{
pos = get_screen_pos(_ref_camera, pos);
}
if(instance.outline)
{
draw_string_outline(
get_window()->get_theme_default_font() ,
pos,
instance.str,
HORIZONTAL_ALIGNMENT_LEFT, -1, Font::DEFAULT_FONT_SIZE * size_ratio, 4 * size_ratio,
Color(0,0,0,color.a)
);
}
draw_string(
get_window()->get_theme_default_font(),
pos,
instance.str,
HORIZONTAL_ALIGNMENT_LEFT, -1, Font::DEFAULT_FONT_SIZE * size_ratio,
color
);
if(instance.icon.is_valid())
{
draw_texture_rect(
instance.icon,
Rect2(pos + Vector2(instance.str.length()*icon_offset_x, icon_offset_y), size_ratio * Vector2(icon_size,icon_size)),
false,
Color(1,1,1,color.a)
);
}
});
for(size_t idx : ended_instances)
{
instances.free_instance(idx);
}
}
void StringDrawer::_process(double delta)
{
elapsed_time += delta;
queue_redraw();
}
int StringDrawer::add_string_instance(StringName const &str, bool outline, bool floating, Vector2 const &pos,
Color const &color, Ref<Texture2D> const &texture)
{
std::lock_guard<std::mutex> lock(mutex);
smart_list_handle<StringInstance> handle = instances.new_instance({str, pos, elapsed_time, texture, color, floating, outline});
return int(handle.handle());
}
void StringDrawer::_bind_methods()
{
ClassDB::bind_method(D_METHOD("add_string_instance", "str", "outline", "floating", "pos", "color", "icon"), &StringDrawer::add_string_instance);
ADD_GROUP("StringDrawer", "StringDrawer_");
ClassDB::bind_method(D_METHOD("get_ref_camera"), &StringDrawer::get_ref_camera);
ClassDB::bind_method(D_METHOD("set_ref_camera", "ref_camera"), &StringDrawer::set_ref_camera);
ClassDB::add_property("StringDrawer", PropertyInfo(Variant::NODE_PATH, "ref_camera", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Camera2D"), "set_ref_camera", "get_ref_camera");
// oscillation_factor
ClassDB::bind_method(D_METHOD("get_oscillation_factor"), &StringDrawer::get_oscillation_factor);
ClassDB::bind_method(D_METHOD("set_oscillation_factor", "oscillation_factor"), &StringDrawer::set_oscillation_factor);
ClassDB::add_property("StringDrawer", PropertyInfo(Variant::FLOAT, "oscillation_factor"), "set_oscillation_factor", "get_oscillation_factor");
// up_speed
ClassDB::bind_method(D_METHOD("get_up_speed"), &StringDrawer::get_up_speed);
ClassDB::bind_method(D_METHOD("set_up_speed", "up_speed"), &StringDrawer::set_up_speed);
ClassDB::add_property("StringDrawer", PropertyInfo(Variant::FLOAT, "up_speed"), "set_up_speed", "get_up_speed");
// float_time
ClassDB::bind_method(D_METHOD("get_float_time"), &StringDrawer::get_float_time);
ClassDB::bind_method(D_METHOD("set_float_time", "float_time"), &StringDrawer::set_float_time);
ClassDB::add_property("StringDrawer", PropertyInfo(Variant::FLOAT, "float_time"), "set_float_time", "get_float_time");
// fade_time
ClassDB::bind_method(D_METHOD("get_fade_time"), &StringDrawer::get_fade_time);
ClassDB::bind_method(D_METHOD("set_fade_time", "fade_time"), &StringDrawer::set_fade_time);
ClassDB::add_property("StringDrawer", PropertyInfo(Variant::FLOAT, "fade_time"), "set_fade_time", "get_fade_time");
// icon_offset_x
ClassDB::bind_method(D_METHOD("get_icon_offset_x"), &StringDrawer::get_icon_offset_x);
ClassDB::bind_method(D_METHOD("set_icon_offset_x", "icon_offset_x"), &StringDrawer::set_icon_offset_x);
ClassDB::add_property("StringDrawer", PropertyInfo(Variant::FLOAT, "icon_offset_x"), "set_icon_offset_x", "get_icon_offset_x");
// icon_offset_y
ClassDB::bind_method(D_METHOD("get_icon_offset_y"), &StringDrawer::get_icon_offset_y);
ClassDB::bind_method(D_METHOD("set_icon_offset_y", "icon_offset_y"), &StringDrawer::set_icon_offset_y);
ClassDB::add_property("StringDrawer", PropertyInfo(Variant::FLOAT, "icon_offset_y"), "set_icon_offset_y", "get_icon_offset_y");
// icon_size
ClassDB::bind_method(D_METHOD("get_icon_size"), &StringDrawer::get_icon_size);
ClassDB::bind_method(D_METHOD("set_icon_size", "icon_size"), &StringDrawer::set_icon_size);
ClassDB::add_property("StringDrawer", PropertyInfo(Variant::FLOAT, "icon_size"), "set_icon_size", "get_icon_size");
// size_ratio
ClassDB::bind_method(D_METHOD("get_size_ratio"), &StringDrawer::get_size_ratio);
ClassDB::bind_method(D_METHOD("set_size_ratio", "size_ratio"), &StringDrawer::set_size_ratio);
ClassDB::add_property("StringDrawer", PropertyInfo(Variant::FLOAT, "size_ratio"), "set_size_ratio", "get_size_ratio");
}
} // godot