File: output_strings.cpp

package info (click to toggle)
godot 3.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 104,964 kB
  • sloc: ansic: 732,795; cpp: 601,776; xml: 56,216; asm: 17,127; lisp: 12,017; python: 9,048; java: 8,253; cs: 5,803; sh: 557; perl: 275; makefile: 98; objc: 17
file content (206 lines) | stat: -rw-r--r-- 6,564 bytes parent folder | download
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
202
203
204
205
206
/*************************************************************************/
/*  output_strings.cpp                                                   */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md)    */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#include "output_strings.h"

void OutputStrings::update_scrollbars() {

	Size2 hmin = h_scroll->get_combined_minimum_size();
	Size2 vmin = v_scroll->get_combined_minimum_size();

	v_scroll->set_anchor(MARGIN_LEFT, ANCHOR_END);
	v_scroll->set_anchor(MARGIN_RIGHT, ANCHOR_END);
	v_scroll->set_anchor(MARGIN_BOTTOM, ANCHOR_END);

	v_scroll->set_begin(Point2(-vmin.width, 0));
	v_scroll->set_end(Point2(0, 0));

	h_scroll->set_anchor(MARGIN_RIGHT, ANCHOR_END);
	h_scroll->set_anchor(MARGIN_TOP, ANCHOR_END);
	h_scroll->set_anchor(MARGIN_BOTTOM, ANCHOR_END);

	h_scroll->set_begin(Point2(0, -hmin.y));
	h_scroll->set_end(Point2(-vmin.x, 0));

	margin.y = hmin.y;
	margin.x = vmin.x;

	Ref<StyleBox> tree_st = get_stylebox("bg", "Tree");
	int page = ((size_height - (int)margin.y - tree_st->get_margin(MARGIN_TOP)) / font_height);
	v_scroll->set_page(page);
}

void OutputStrings::_notification(int p_what) {

	switch (p_what) {

		case NOTIFICATION_DRAW: {

			if (following) {

				updating = true;
				v_scroll->set_value(v_scroll->get_max() - v_scroll->get_page());
				updating = false;
			}

			RID ci = get_canvas_item();
			Size2 size = get_size();

			Ref<Font> font = get_font("font", "Tree");
			Ref<StyleBox> tree_st = get_stylebox("bg", "Tree");
			tree_st->draw(ci, Rect2(Point2(), size));
			Color color = get_color("font_color", "Tree");
			Ref<Texture> icon_error = get_icon("Error", "EditorIcons");
			Ref<Texture> icon_warning = get_icon("Warning", "EditorIcons");

			//int lines = (size_height-(int)margin.y) / font_height;
			Point2 ofs = tree_st->get_offset();

			LineMap::Element *E = line_map.find(v_scroll->get_value());
			float h_ofs = (int)h_scroll->get_value();
			Point2 icon_ofs = Point2(0, (font_height - (int)icon_error->get_height()) / 2);

			while (E && ofs.y < (size_height - (int)margin.y)) {

				String str = E->get().text;
				Point2 line_ofs = ofs;

				switch (E->get().type) {

					case LINE_WARNING: {
						icon_warning->draw(ci, line_ofs + icon_ofs);

					} break;
					case LINE_ERROR: {
						icon_error->draw(ci, line_ofs + icon_ofs);
					} break;
					case LINE_LINK: {

					} break;
					default: {}
				}

				line_ofs.y += font->get_ascent();
				line_ofs.x += icon_error->get_width() + 4;

				for (int i = 0; i < str.length(); i++) {
					if (line_ofs.x - h_ofs < 0) {
						line_ofs.x += font->get_char_size(str[i], str[i + 1]).width;
					} else if (line_ofs.x - h_ofs > size.width - margin.width) {
						break;
					} else {
						line_ofs.x += font->draw_char(ci, Point2(line_ofs.x - h_ofs, line_ofs.y), str[i], str[i + 1], color);
					}
				}

				ofs.y += font_height;
				E = E->next();
			}

		} break;

		case NOTIFICATION_ENTER_TREE:
		case NOTIFICATION_RESIZED: {

			font_height = get_font("font", "Tree")->get_height();
			size_height = get_size().height;
			update_scrollbars();
		} break;
	}
}

void OutputStrings::_hscroll_changed(float p_value) {

	if (updating)
		return;

	update();
}
void OutputStrings::_vscroll_changed(float p_value) {

	if (updating)
		return;
	//user changed scroll
	following = (p_value + v_scroll->get_page()) >= v_scroll->get_max();
	update();
}

void OutputStrings::add_line(const String &p_text, const Variant &p_meta, const LineType p_type) {

	Vector<String> strings = p_text.split("\n");

	for (int i = 0; i < strings.size(); i++) {

		if (strings[i].length() == 0)
			continue;

		int last = line_map.empty() ? 0 : (line_map.back()->key() + 1);

		Line l;
		l.text = strings[i];
		l.meta = p_meta;
		l.type = p_type;
		line_map.insert(last, l);

		updating = true;
		v_scroll->set_max(last + 1);
		v_scroll->set_min(line_map.front()->key());
		updating = false;
	}

	while (line_map.size() > line_max_count) {

		line_map.erase(line_map.front());
	}

	update();
}

void OutputStrings::_bind_methods() {

	ClassDB::bind_method("_vscroll_changed", &OutputStrings::_vscroll_changed);
	ClassDB::bind_method("_hscroll_changed", &OutputStrings::_hscroll_changed);
}

OutputStrings::OutputStrings() {

	following = true;
	updating = false;
	line_max_count = 4096;
	h_scroll = memnew(HScrollBar);
	v_scroll = memnew(VScrollBar);
	add_child(h_scroll);
	add_child(v_scroll);
	size_height = 1;
	font_height = 1;
	update_scrollbars();
	h_scroll->connect("value_changed", this, "_hscroll_changed");
	v_scroll->connect("value_changed", this, "_vscroll_changed");
}