2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* output_strings.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2019-01-03 11:41:35 +01:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "output_strings.h"
|
|
|
|
|
|
|
|
void OutputStrings::update_scrollbars() {
|
|
|
|
|
|
|
|
Size2 hmin = h_scroll->get_combined_minimum_size();
|
|
|
|
Size2 vmin = v_scroll->get_combined_minimum_size();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
v_scroll->set_anchor(MARGIN_LEFT, ANCHOR_END);
|
|
|
|
v_scroll->set_anchor(MARGIN_RIGHT, ANCHOR_END);
|
|
|
|
v_scroll->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-07-06 09:16:27 +02:00
|
|
|
v_scroll->set_begin(Point2(-vmin.width, 0));
|
2017-03-05 16:44:50 +01:00
|
|
|
v_scroll->set_end(Point2(0, 0));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
h_scroll->set_anchor(MARGIN_RIGHT, ANCHOR_END);
|
|
|
|
h_scroll->set_anchor(MARGIN_TOP, ANCHOR_END);
|
|
|
|
h_scroll->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-07-06 09:16:27 +02:00
|
|
|
h_scroll->set_begin(Point2(0, -hmin.y));
|
|
|
|
h_scroll->set_end(Point2(-vmin.x, 0));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
margin.y = hmin.y;
|
|
|
|
margin.x = vmin.x;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<StyleBox> tree_st = get_stylebox("bg", "Tree");
|
|
|
|
int page = ((size_height - (int)margin.y - tree_st->get_margin(MARGIN_TOP)) / font_height);
|
2014-02-10 02:10:30 +01:00
|
|
|
v_scroll->set_page(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputStrings::_notification(int p_what) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_what) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
|
|
|
|
if (following) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
updating = true;
|
|
|
|
v_scroll->set_value(v_scroll->get_max() - v_scroll->get_page());
|
|
|
|
updating = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RID ci = get_canvas_item();
|
|
|
|
Size2 size = get_size();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
//int lines = (size_height-(int)margin.y) / font_height;
|
2017-03-05 16:44:50 +01:00
|
|
|
Point2 ofs = tree_st->get_offset();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-04 05:16:14 +01:00
|
|
|
LineMap::Element *E = line_map.find(v_scroll->get_value());
|
|
|
|
float h_ofs = (int)h_scroll->get_value();
|
2017-03-05 16:44:50 +01:00
|
|
|
Point2 icon_ofs = Point2(0, (font_height - (int)icon_error->get_height()) / 2);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (E && ofs.y < (size_height - (int)margin.y)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
String str = E->get().text;
|
2017-03-05 16:44:50 +01:00
|
|
|
Point2 line_ofs = ofs;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (E->get().type) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
case LINE_WARNING: {
|
2017-03-05 16:44:50 +01:00
|
|
|
icon_warning->draw(ci, line_ofs + icon_ofs);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case LINE_ERROR: {
|
2017-03-05 16:44:50 +01:00
|
|
|
icon_error->draw(ci, line_ofs + icon_ofs);
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case LINE_LINK: {
|
|
|
|
|
|
|
|
} break;
|
|
|
|
default: {}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
line_ofs.y += font->get_ascent();
|
|
|
|
line_ofs.x += icon_error->get_width() + 4;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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) {
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
line_ofs.x += font->draw_char(ci, Point2(line_ofs.x - h_ofs, line_ofs.y), str[i], str[i + 1], color);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ofs.y += font_height;
|
|
|
|
E = E->next();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
2014-11-06 01:20:42 +01:00
|
|
|
case NOTIFICATION_ENTER_TREE:
|
2014-02-10 02:10:30 +01:00
|
|
|
case NOTIFICATION_RESIZED: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
font_height = get_font("font", "Tree")->get_height();
|
2014-02-10 02:10:30 +01:00
|
|
|
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
|
2017-03-05 16:44:50 +01:00
|
|
|
following = (p_value + v_scroll->get_page()) >= v_scroll->get_max();
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void OutputStrings::add_line(const String &p_text, const Variant &p_meta, const LineType p_type) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector<String> strings = p_text.split("\n");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < strings.size(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (strings[i].length() == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
int last = line_map.empty() ? 0 : (line_map.back()->key() + 1);
|
|
|
|
|
|
|
|
Line l;
|
2017-03-05 16:44:50 +01:00
|
|
|
l.text = strings[i];
|
|
|
|
l.meta = p_meta;
|
|
|
|
l.type = p_type;
|
|
|
|
line_map.insert(last, l);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
updating = true;
|
|
|
|
v_scroll->set_max(last + 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
v_scroll->set_min(line_map.front()->key());
|
2017-03-05 16:44:50 +01:00
|
|
|
updating = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while (line_map.size() > line_max_count) {
|
|
|
|
|
|
|
|
line_map.erase(line_map.front());
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputStrings::_bind_methods() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method("_vscroll_changed", &OutputStrings::_vscroll_changed);
|
|
|
|
ClassDB::bind_method("_hscroll_changed", &OutputStrings::_hscroll_changed);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
OutputStrings::OutputStrings() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
following = true;
|
|
|
|
updating = false;
|
|
|
|
line_max_count = 4096;
|
|
|
|
h_scroll = memnew(HScrollBar);
|
|
|
|
v_scroll = memnew(VScrollBar);
|
2014-02-10 02:10:30 +01:00
|
|
|
add_child(h_scroll);
|
|
|
|
add_child(v_scroll);
|
2017-03-05 16:44:50 +01:00
|
|
|
size_height = 1;
|
|
|
|
font_height = 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
update_scrollbars();
|
2017-03-05 16:44:50 +01:00
|
|
|
h_scroll->connect("value_changed", this, "_hscroll_changed");
|
|
|
|
v_scroll->connect("value_changed", this, "_vscroll_changed");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|