editor_log.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*************************************************************************/
  2. /* editor_log.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 "editor_log.h"
  31. #include "editor_node.h"
  32. #include "scene/gui/center_container.h"
  33. #include "version.h"
  34. void EditorLog::_error_handler(void *p_self, const char *p_func, const char *p_file, int p_line, const char *p_error, const char *p_errorexp, ErrorHandlerType p_type) {
  35. EditorLog *self = (EditorLog *)p_self;
  36. if (self->current != Thread::get_caller_ID())
  37. return;
  38. String err_str;
  39. if (p_errorexp && p_errorexp[0]) {
  40. err_str = p_errorexp;
  41. } else {
  42. err_str = String(p_file) + ":" + itos(p_line) + " - " + String(p_error);
  43. }
  44. // if (!self->is_visible())
  45. // self->emit_signal("show_request");
  46. err_str = " " + err_str;
  47. self->log->add_newline();
  48. Ref<Texture> icon;
  49. switch (p_type) {
  50. case ERR_HANDLER_ERROR: {
  51. icon = self->get_icon("Error", "EditorIcons");
  52. return; // these are confusing
  53. } break;
  54. case ERR_HANDLER_WARNING: {
  55. icon = self->get_icon("Error", "EditorIcons");
  56. } break;
  57. case ERR_HANDLER_SCRIPT: {
  58. icon = self->get_icon("ScriptError", "EditorIcons");
  59. } break;
  60. }
  61. self->add_message(err_str, true);
  62. }
  63. void EditorLog::_notification(int p_what) {
  64. if (p_what == NOTIFICATION_ENTER_TREE) {
  65. log->add_color_override("default_color", get_color("font_color", "Tree"));
  66. //button->set_icon(get_icon("Console","EditorIcons"));
  67. }
  68. /*if (p_what==NOTIFICATION_DRAW) {
  69. RID ci = get_canvas_item();
  70. get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  71. int top_ofs = 20;
  72. int border_ofs=4;
  73. Ref<StyleBox> style = get_stylebox("normal","TextEdit");
  74. style->draw(ci,Rect2( Point2(border_ofs,top_ofs),get_size()-Size2(border_ofs*2,top_ofs+border_ofs)));
  75. }*/
  76. }
  77. void EditorLog::_clear_request() {
  78. log->clear();
  79. }
  80. void EditorLog::clear() {
  81. _clear_request();
  82. }
  83. void EditorLog::add_message(const String &p_msg, bool p_error) {
  84. if (p_error) {
  85. Ref<Texture> icon = get_icon("Error", "EditorIcons");
  86. log->add_image(icon);
  87. //button->set_icon(icon);
  88. log->push_color(get_color("fg_error", "Editor"));
  89. } else {
  90. //button->set_icon(Ref<Texture>());
  91. }
  92. log->add_newline();
  93. log->add_text(p_msg);
  94. // button->set_text(p_msg);
  95. if (p_error)
  96. log->pop();
  97. }
  98. /*
  99. void EditorLog::_dragged(const Point2& p_ofs) {
  100. int ofs = ec->get_minsize().height;
  101. ofs = ofs-p_ofs.y;
  102. if (ofs<50)
  103. ofs=50;
  104. if (ofs>300)
  105. ofs=300;
  106. ec->set_minsize(Size2(ec->get_minsize().width,ofs));
  107. minimum_size_changed();
  108. }
  109. */
  110. void EditorLog::_undo_redo_cbk(void *p_self, const String &p_name) {
  111. EditorLog *self = (EditorLog *)p_self;
  112. self->add_message(p_name);
  113. }
  114. void EditorLog::_bind_methods() {
  115. ObjectTypeDB::bind_method(_MD("_clear_request"), &EditorLog::_clear_request);
  116. //ObjectTypeDB::bind_method(_MD("_dragged"),&EditorLog::_dragged );
  117. ADD_SIGNAL(MethodInfo("clear_request"));
  118. }
  119. EditorLog::EditorLog() {
  120. VBoxContainer *vb = this;
  121. add_constant_override("separation", get_constant("separation", "VBoxContainer"));
  122. HBoxContainer *hb = memnew(HBoxContainer);
  123. vb->add_child(hb);
  124. title = memnew(Label);
  125. title->set_text(TTR(" Output:"));
  126. title->set_h_size_flags(SIZE_EXPAND_FILL);
  127. hb->add_child(title);
  128. //pd = memnew( PaneDrag );
  129. //hb->add_child(pd);
  130. //pd->connect("dragged",this,"_dragged");
  131. //pd->set_default_cursor_shape(Control::CURSOR_MOVE);
  132. clearbutton = memnew(Button);
  133. hb->add_child(clearbutton);
  134. clearbutton->set_text(TTR("Clear"));
  135. clearbutton->connect("pressed", this, "_clear_request");
  136. ec = memnew(Control);
  137. vb->add_child(ec);
  138. ec->set_custom_minimum_size(Size2(0, 180));
  139. ec->set_v_size_flags(SIZE_EXPAND_FILL);
  140. PanelContainer *pc = memnew(PanelContainer);
  141. pc->add_style_override("panel", get_stylebox("normal", "TextEdit"));
  142. ec->add_child(pc);
  143. pc->set_area_as_parent_rect();
  144. log = memnew(RichTextLabel);
  145. log->set_scroll_follow(true);
  146. log->set_selection_enabled(true);
  147. log->set_focus_mode(FOCUS_CLICK);
  148. pc->add_child(log);
  149. add_message(VERSION_FULL_NAME " (c) 2007-2020 Juan Linietsky, Ariel Manzur.");
  150. //log->add_text("Initialization Complete.\n"); //because it looks cool.
  151. eh.errfunc = _error_handler;
  152. eh.userdata = this;
  153. add_error_handler(&eh);
  154. current = Thread::get_caller_ID();
  155. EditorNode::get_undo_redo()->set_commit_notify_callback(_undo_redo_cbk, this);
  156. }
  157. void EditorLog::deinit() {
  158. remove_error_handler(&eh);
  159. }
  160. EditorLog::~EditorLog() {
  161. }