output_strings.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*************************************************************************/
  2. /* output_strings.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "output_strings.h"
  31. void OutputStrings::update_scrollbars() {
  32. Size2 hmin = h_scroll->get_combined_minimum_size();
  33. Size2 vmin = v_scroll->get_combined_minimum_size();
  34. v_scroll->set_anchor(MARGIN_LEFT, ANCHOR_END);
  35. v_scroll->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  36. v_scroll->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  37. v_scroll->set_begin(Point2(vmin.width, 0));
  38. v_scroll->set_end(Point2(0, 0));
  39. h_scroll->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  40. h_scroll->set_anchor(MARGIN_TOP, ANCHOR_END);
  41. h_scroll->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  42. h_scroll->set_begin(Point2(0, hmin.y));
  43. h_scroll->set_end(Point2(vmin.x, 0));
  44. margin.y = hmin.y;
  45. margin.x = vmin.x;
  46. Ref<StyleBox> tree_st = get_stylebox("bg", "Tree");
  47. int page = ((size_height - (int)margin.y - tree_st->get_margin(MARGIN_TOP)) / font_height);
  48. v_scroll->set_page(page);
  49. }
  50. void OutputStrings::_notification(int p_what) {
  51. switch (p_what) {
  52. case NOTIFICATION_DRAW: {
  53. if (following) {
  54. updating = true;
  55. v_scroll->set_val(v_scroll->get_max() - v_scroll->get_page());
  56. updating = false;
  57. }
  58. RID ci = get_canvas_item();
  59. Size2 size = get_size();
  60. Ref<Font> font = get_font("font", "Tree");
  61. Ref<StyleBox> tree_st = get_stylebox("bg", "Tree");
  62. tree_st->draw(ci, Rect2(Point2(), size));
  63. Color color = get_color("font_color", "Tree");
  64. Ref<Texture> icon_error = get_icon("Error", "EditorIcons");
  65. Ref<Texture> icon_warning = get_icon("Warning", "EditorIcons");
  66. // int lines = (size_height-(int)margin.y) / font_height;
  67. Point2 ofs = tree_st->get_offset();
  68. LineMap::Element *E = line_map.find(v_scroll->get_val());
  69. float h_ofs = (int)h_scroll->get_val();
  70. Point2 icon_ofs = Point2(0, (font_height - (int)icon_error->get_height()) / 2);
  71. while (E && ofs.y < (size_height - (int)margin.y)) {
  72. String str = E->get().text;
  73. Point2 line_ofs = ofs;
  74. switch (E->get().type) {
  75. case LINE_WARNING: {
  76. icon_warning->draw(ci, line_ofs + icon_ofs);
  77. } break;
  78. case LINE_ERROR: {
  79. icon_error->draw(ci, line_ofs + icon_ofs);
  80. } break;
  81. case LINE_LINK: {
  82. } break;
  83. default: {
  84. }
  85. }
  86. line_ofs.y += font->get_ascent();
  87. line_ofs.x += icon_error->get_width() + 4;
  88. for (int i = 0; i < str.length(); i++) {
  89. if (line_ofs.x - h_ofs < 0) {
  90. line_ofs.x += font->get_char_size(str[i], str[i + 1]).width;
  91. } else if (line_ofs.x - h_ofs > size.width - margin.width) {
  92. break;
  93. } else {
  94. line_ofs.x += font->draw_char(ci, Point2(line_ofs.x - h_ofs, line_ofs.y), str[i], str[i + 1], color);
  95. }
  96. }
  97. ofs.y += font_height;
  98. E = E->next();
  99. }
  100. } break;
  101. case NOTIFICATION_ENTER_TREE:
  102. case NOTIFICATION_RESIZED: {
  103. font_height = get_font("font", "Tree")->get_height();
  104. size_height = get_size().height;
  105. update_scrollbars();
  106. } break;
  107. }
  108. }
  109. void OutputStrings::_hscroll_changed(float p_value) {
  110. if (updating)
  111. return;
  112. update();
  113. }
  114. void OutputStrings::_vscroll_changed(float p_value) {
  115. if (updating)
  116. return;
  117. //user changed scroll
  118. following = (p_value + v_scroll->get_page()) >= v_scroll->get_max();
  119. update();
  120. }
  121. void OutputStrings::add_line(const String &p_text, const Variant &p_meta, const LineType p_type) {
  122. Vector<String> strings = p_text.split("\n");
  123. for (int i = 0; i < strings.size(); i++) {
  124. if (strings[i].length() == 0)
  125. continue;
  126. int last = line_map.empty() ? 0 : (line_map.back()->key() + 1);
  127. Line l;
  128. l.text = strings[i];
  129. l.meta = p_meta;
  130. l.type = p_type;
  131. line_map.insert(last, l);
  132. updating = true;
  133. v_scroll->set_max(last + 1);
  134. v_scroll->set_min(line_map.front()->key());
  135. updating = false;
  136. }
  137. while (line_map.size() > line_max_count) {
  138. line_map.erase(line_map.front());
  139. }
  140. update();
  141. }
  142. void OutputStrings::_bind_methods() {
  143. ObjectTypeDB::bind_method("_vscroll_changed", &OutputStrings::_vscroll_changed);
  144. ObjectTypeDB::bind_method("_hscroll_changed", &OutputStrings::_hscroll_changed);
  145. }
  146. OutputStrings::OutputStrings() {
  147. following = true;
  148. updating = false;
  149. line_max_count = 4096;
  150. h_scroll = memnew(HScrollBar);
  151. v_scroll = memnew(VScrollBar);
  152. add_child(h_scroll);
  153. add_child(v_scroll);
  154. size_height = 1;
  155. font_height = 1;
  156. update_scrollbars();
  157. h_scroll->connect("value_changed", this, "_hscroll_changed");
  158. v_scroll->connect("value_changed", this, "_vscroll_changed");
  159. }