Debug Draw 3D (and 2D) 1.4.5
Draw 3D debug graphics and 2D overlays with this add-on.
Loading...
Searching...
No Matches
graphs.h
1#pragma once
2
3#include "common/circular_buffer.h"
4#include "common/colors.h"
5#include "utils/compiler.h"
6#include "utils/profiler.h"
7
8#include <mutex>
9#include <vector>
10
11GODOT_WARNING_DISABLE()
12#include <godot_cpp/classes/canvas_item.hpp>
13#include <godot_cpp/classes/font.hpp>
14#include <godot_cpp/classes/ref_counted.hpp>
15GODOT_WARNING_RESTORE()
16using namespace godot;
17
18class DataGraphManager;
19
26class DebugDraw2DGraph : public RefCounted {
27 GDCLASS(DebugDraw2DGraph, RefCounted);
28
29public:
30 /*enum GraphLinePosition : int {
31 LINE_TOP = 0,
32 LINE_CENTER = 1,
33 LINE_BOTTOM = 2,
34 };*/
35
39 enum GraphPosition : int {
40 POSITION_LEFT_TOP = 0,
41 POSITION_RIGHT_TOP = 1,
42 POSITION_LEFT_BOTTOM = 2,
43 POSITION_RIGHT_BOTTOM = 3,
44 POSITION_MAX
45 };
46
50 enum GraphSide : int {
51 SIDE_LEFT = 0,
52 SIDE_TOP = 1,
53 SIDE_RIGHT = 2,
54 SIDE_BOTTOM = 3,
55 SIDE_MAX
56 };
57
61 enum TextFlags : int64_t {
62 TEXT_CURRENT = 1 << 0,
63 TEXT_AVG = 1 << 1,
64 TEXT_MAX = 1 << 2,
65 TEXT_MIN = 1 << 3,
66 TEXT_ALL = TEXT_CURRENT | TEXT_AVG | TEXT_MAX | TEXT_MIN,
67 };
68
70 enum GraphType {
71 GRAPH_NORMAL,
72 GRAPH_FPS,
73 };
74
76 struct graph_interpolated_values_range {
77 double shrink_weight_max, shrink_weight_min;
78 double upd_timer_max, upd_timer_min, max_timer_delay;
79 double min, max, avg;
80 uint32_t buffer_size;
81
82 void update(const double &_min, const double &_max, const double &_avg, const double &_delta);
83 void reset(uint32_t _buffer_size, double _upd_timer_delay = 2.0);
84 };
85
86private:
88 bool enabled = true;
90 bool upside_down = true;
92 bool show_title = false;
93 /*
95 GraphLinePosition line_position = GraphLinePosition::LINE_CENTER;
96 */
98 BitField<TextFlags> show_text_flags = TextFlags::TEXT_ALL;
100 Vector2i size = Vector2i(256, 64);
102 int buffer_size = 256;
104 Vector2i offset = Vector2i(8, 8);
106 GraphPosition corner = GraphPosition::POSITION_RIGHT_TOP;
108 real_t line_width = 1.0f;
110 Color line_color = Colors::orange_red;
112 Color background_color = Colors::gray_graph_bg;
114 Color border_color = Colors::black;
115
117 String text_suffix = "";
119 Ref<Font> custom_font = Ref<Font>();
121 int title_size = 14;
123 int text_size = 12;
125 Color title_color = Colors::white_smoke;
127 Color text_color = Colors::white_smoke;
129 int text_precision = 2;
131 StringName parent_graph;
133 GraphSide parent_graph_side = GraphSide::SIDE_BOTTOM;
135 Callable data_getter;
136
137private:
138 mutable graph_interpolated_values_range graph_range = {};
139 StringName title;
140
141protected:
143 DataGraphManager *owner = nullptr;
145 mutable ProfiledMutex(std::recursive_mutex, datalock, "Graphs draw lock");
147 std::unique_ptr<CircularBuffer<double> > buffer_data;
148
150 static void _bind_methods();
151
153 virtual void _update_received(double _value);
155 void _init(DataGraphManager *_owner, StringName _title);
156
157public:
160 DebugDraw2DGraph(DataGraphManager *_owner, StringName _title);
162
164 virtual GraphType get_type() { return GraphType::GRAPH_NORMAL; };
165
169 StringName get_title() const;
170
174 void set_enabled(const bool _state);
175 bool is_enabled() const;
179 void set_upside_down(const bool _state);
180 bool is_upside_down() const;
184 void set_show_title(const bool _state);
185 bool is_show_title() const;
186 /*void set_line_position(const GraphLinePosition _position);
187 GraphLinePosition get_line_position() const;*/
191 void set_show_text_flags(const BitField<TextFlags> _flags);
192 BitField<TextFlags> get_show_text_flags() const;
196 void set_size(const Vector2i &_size);
197 Vector2i get_size() const;
201 void set_buffer_size(const int _buf_size);
202 int get_buffer_size() const;
206 void set_offset(const Vector2i &_offset);
207 Vector2i get_offset() const;
212 void set_corner(const GraphPosition _position);
213 GraphPosition get_corner() const;
217 void set_line_width(const real_t _width);
218 real_t get_line_width() const;
222 void set_line_color(const Color &_new_color);
223 Color get_line_color() const;
227 void set_background_color(const Color &_new_color);
228 Color get_background_color() const;
232 void set_border_color(const Color &_new_color);
233 Color get_border_color() const;
237 void set_text_suffix(const String &_suffix);
238 String get_text_suffix() const;
242 void set_custom_font(const Ref<Font> _custom_font);
243 Ref<Font> get_custom_font() const;
247 void set_title_size(const int _size);
248 int get_title_size() const;
252 void set_text_size(const int _size);
253 int get_text_size() const;
257 void set_title_color(const Color &_new_color);
258 Color get_title_color() const;
262 void set_text_color(const Color &_new_color);
263 Color get_text_color() const;
267 void set_text_precision(const int _precision);
268 int get_text_precision() const;
272 void set_parent_graph(const StringName &_graph);
273 StringName get_parent_graph() const;
279 GraphSide get_parent_graph_side() const;
283 virtual void set_data_getter(const Callable &_callable);
284 Callable get_data_getter() const;
285
289 void set_parent(const StringName &_name, const GraphSide _side = GraphSide::SIDE_BOTTOM);
290
291public:
293 void update(double value);
294
296 struct graph_rects {
297 Rect2i full;
298 Rect2i base;
299 };
300
302 Vector2i _get_graph_position(const bool &_is_root, const DebugDraw2DGraph::GraphPosition &_corner, const DebugDraw2DGraph::graph_rects &_rects) const;
304 graph_rects draw(CanvasItem *_ci, const Ref<Font> &_font, const graph_rects &_prev_rects, const GraphPosition &_corner, const bool &_is_root, const double &_delta) const;
305};
306
307VARIANT_ENUM_CAST(DebugDraw2DGraph::GraphPosition);
308// VARIANT_ENUM_CAST(DebugDraw2DGraph::GraphLinePosition);
309VARIANT_ENUM_CAST(DebugDraw2DGraph::GraphSide);
310VARIANT_BITFIELD_CAST(DebugDraw2DGraph::TextFlags);
311
320
321private:
322 bool frametime_mode = true;
323 bool is_ms = false;
324
325protected:
327 static void _bind_methods();
328
329#ifndef DISABLE_DEBUG_RENDERING
331 virtual void _update_received(double _value) override;
332
333public:
336 DebugDraw2DFPSGraph(DataGraphManager *_owner, StringName _title);
337
339 virtual GraphType get_type() override { return GraphType::GRAPH_FPS; };
343 virtual void set_data_getter(const Callable &_callable) override;
344#endif
345
349 void set_frame_time_mode(const bool _state);
350 bool is_frame_time_mode() const;
351};
352
353#ifndef DISABLE_DEBUG_RENDERING
354
356class DataGraphManager {
357 std::vector<Ref<DebugDraw2DGraph> > graphs;
358 mutable ProfiledMutex(std::recursive_mutex, datalock, "Graphs manager lock");
359 class DebugDraw2D *owner = nullptr;
360
361public:
362 DataGraphManager(class DebugDraw2D *root);
363 ~DataGraphManager();
364
365 void mark_canvas_dirty();
366 void draw(CanvasItem *_ci, Ref<Font> _font, Vector2 _vp_size, double _delta) const;
367 Ref<DebugDraw2DGraph> create_graph(const StringName &_title);
368 Ref<DebugDraw2DGraph> create_fps_graph(const StringName &_title);
369 void auto_update_graphs(double delta);
370 void graph_update_data(const StringName &_title, const double &_data);
371 void remove_graph(const StringName &_title);
372 void clear_graphs();
373 Ref<DebugDraw2DGraph> get_graph(const StringName &_title) const;
374 PackedStringArray get_graph_names() const;
375 size_t get_graphs_enabled() const;
376 size_t get_graphs_total() const;
377};
378
379#endif
This version of the graphs is automatically updated and displays FPS or Frametime.
Definition graphs.h:318
virtual void set_data_getter(const Callable &_callable) override
void set_frame_time_mode(const bool _state)
Base class for drawing graphs.
Definition graphs.h:26
void set_buffer_size(const int _buf_size)
void set_parent_graph(const StringName &_graph)
virtual void set_data_getter(const Callable &_callable)
void set_border_color(const Color &_new_color)
StringName get_title() const
void set_parent_graph_side(const GraphSide _side)
GraphPosition
Definition graphs.h:39
void set_title_size(const int _size)
void set_line_color(const Color &_new_color)
void set_text_size(const int _size)
void set_show_title(const bool _state)
void set_custom_font(const Ref< Font > _custom_font)
void set_text_suffix(const String &_suffix)
void set_offset(const Vector2i &_offset)
void set_show_text_flags(const BitField< TextFlags > _flags)
void set_text_precision(const int _precision)
TextFlags
Definition graphs.h:61
void set_enabled(const bool _state)
void set_corner(const GraphPosition _position)
GraphSide
Definition graphs.h:50
void set_upside_down(const bool _state)
void set_background_color(const Color &_new_color)
void set_title_color(const Color &_new_color)
void set_size(const Vector2i &_size)
void set_parent(const StringName &_name, const GraphSide _side=GraphSide::SIDE_BOTTOM)
void set_line_width(const real_t _width)
void set_text_color(const Color &_new_color)
Singleton class for calling debugging 2D methods.
Definition debug_draw_2d.h:27