output_strings.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*************************************************************************/
  2. /* output_strings.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "output_strings.h"
  30. void OutputStrings::update_scrollbars() {
  31. Size2 size = get_size();
  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. line_ofs.y+=font->get_ascent();
  86. line_ofs.x+=icon_error->get_width()+4;
  87. for(int i=0;i<str.length();i++) {
  88. if (line_ofs.x-h_ofs < 0 ) {
  89. line_ofs.x+=font->get_char_size(str[i],str[i+1]).width;
  90. } else if (line_ofs.x-h_ofs > size.width - margin.width) {
  91. break;
  92. } else {
  93. line_ofs.x+=font->draw_char(ci,Point2(line_ofs.x-h_ofs,line_ofs.y),str[i],str[i+1],color);
  94. }
  95. }
  96. ofs.y+=font_height;
  97. E=E->next();
  98. }
  99. } break;
  100. case NOTIFICATION_ENTER_SCENE:
  101. case NOTIFICATION_RESIZED: {
  102. font_height = get_font("font","Tree")->get_height();
  103. size_height = get_size().height;
  104. update_scrollbars();
  105. } break;
  106. }
  107. }
  108. void OutputStrings::_hscroll_changed(float p_value) {
  109. if (updating)
  110. return;
  111. update();
  112. }
  113. void OutputStrings::_vscroll_changed(float p_value) {
  114. if (updating)
  115. return;
  116. //user changed scroll
  117. following=(p_value+v_scroll->get_page())>=v_scroll->get_max();
  118. update();
  119. }
  120. void OutputStrings::add_line(const String& p_text, const Variant& p_meta, const LineType p_type) {
  121. Vector<String> strings=p_text.split("\n");
  122. for(int i=0;i<strings.size();i++) {
  123. if (strings[i].length()==0)
  124. continue;
  125. int last = line_map.empty() ? 0 : (line_map.back()->key() + 1);
  126. Line l;
  127. l.text=strings[i];
  128. l.meta=p_meta;
  129. l.type=p_type;
  130. line_map.insert(last,l);
  131. updating=true;
  132. v_scroll->set_max(last+1);
  133. v_scroll->set_min(line_map.front()->key());
  134. updating=false;
  135. }
  136. while (line_map.size() > line_max_count) {
  137. line_map.erase(line_map.front());
  138. }
  139. update();
  140. }
  141. void OutputStrings::_bind_methods() {
  142. ObjectTypeDB::bind_method("_vscroll_changed",&OutputStrings::_vscroll_changed);
  143. ObjectTypeDB::bind_method("_hscroll_changed",&OutputStrings::_hscroll_changed);
  144. }
  145. OutputStrings::OutputStrings() {
  146. following=true;
  147. updating=false;
  148. line_max_count=4096;
  149. h_scroll = memnew( HScrollBar );
  150. v_scroll = memnew( VScrollBar );
  151. add_child(h_scroll);
  152. add_child(v_scroll);
  153. size_height=1;
  154. font_height=1;
  155. update_scrollbars();
  156. h_scroll->connect("value_changed", this,"_hscroll_changed");
  157. v_scroll->connect("value_changed", this,"_vscroll_changed");
  158. }