console.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*************************************************************************/
  2. /* console.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 "console.h"
  30. #include "os/os.h"
  31. #include "os/keyboard.h"
  32. #include "editor_icons.h"
  33. #include "scene/gui/label.h"
  34. #include "globals.h"
  35. void Console::_stats_update_timer_callback() {
  36. if (!status->is_visible())
  37. return;
  38. VisualServer *vs = VisualServer::get_singleton();
  39. stats.render_objects_in_frame->set_text(1,String::num(vs->get_render_info( VisualServer::INFO_OBJECTS_IN_FRAME ) ) );
  40. stats.material_changes_in_frame->set_text(1,String::num(vs->get_render_info( VisualServer::INFO_MATERIAL_CHANGES_IN_FRAME ) ) );
  41. int64_t total_vmem = vs->get_render_info( VisualServer::INFO_USAGE_VIDEO_MEM_TOTAL );
  42. if (total_vmem<0)
  43. stats.usage_video_mem_total->set_text(1, "Unknown");
  44. else
  45. stats.usage_video_mem_total->set_text(1,String::humanize_size( total_vmem ) );
  46. stats.usage_video_mem_used->set_text(1,String::humanize_size( vs->get_render_info( VisualServer::INFO_VIDEO_MEM_USED ) ) );
  47. stats.usage_texture_mem_used->set_text(1,String::humanize_size( vs->get_render_info( VisualServer::INFO_TEXTURE_MEM_USED ) ) );
  48. stats.usage_vertex_mem_used->set_text(1,String::humanize_size( vs->get_render_info( VisualServer::INFO_VERTEX_MEM_USED ) ) );
  49. stats.usage_static_memory_total->set_text(1,String::humanize_size( Memory::get_static_mem_available() ) );
  50. stats.usage_static_memory->set_text(1,String::humanize_size( Memory::get_static_mem_usage() ) );
  51. stats.usage_dynamic_memory_total->set_text(1,String::humanize_size( Memory::get_dynamic_mem_available() ) );
  52. stats.usage_dynamic_memory->set_text(1,String::humanize_size( Memory::get_dynamic_mem_usage() ) );
  53. stats.usage_objects_instanced->set_text(1,String::num( ObjectDB::get_object_count()) );
  54. }
  55. void Console::_print_handle(void *p_this,const String& p_string) {
  56. return;
  57. Console *self = (Console*)p_this;
  58. OutputQueue oq;
  59. oq.text=p_string;
  60. oq.type=OutputStrings::LINE_NORMAL;
  61. if (self->output_queue_mutex)
  62. self->output_queue_mutex->lock();
  63. self->output_queue.push_back(oq);
  64. if (self->output_queue_mutex)
  65. self->output_queue_mutex->unlock();
  66. }
  67. void Console::_error_handle(void *p_this,const char*p_function,const char* p_file,int p_line,const char *p_error, const char *p_explanation,ErrorHandlerType p_type) {
  68. Console *self = (Console*)p_this;
  69. OutputQueue oq;
  70. oq.text="ERROR: "+String(p_file)+":"+itos(p_line)+", in function: "+String(p_function);
  71. oq.text+="\n "+String(p_error)+".";
  72. if (p_explanation && p_explanation[0])
  73. oq.text+="\n Reason: "+String(p_explanation);
  74. oq.text+="\n";
  75. oq.type=OutputStrings::LINE_ERROR;
  76. if (self->output_queue_mutex)
  77. self->output_queue_mutex->lock();
  78. self->output_queue.push_back(oq);
  79. if (self->output_queue_mutex)
  80. self->output_queue_mutex->unlock();
  81. }
  82. void Console::_window_input_event(InputEvent p_event) {
  83. Control::_window_input_event(p_event);
  84. if (p_event.type==InputEvent::KEY && p_event.key.pressed) {
  85. if (p_event.key.scancode==KEY_QUOTELEFT && p_event.key.mod.control) {
  86. if (is_visible())
  87. hide();
  88. else {
  89. globals_property_editor->edit( NULL );
  90. globals_property_editor->edit( Globals::get_singleton() );
  91. show();
  92. };
  93. }
  94. if (p_event.key.scancode==KEY_ESCAPE && !window_has_modal_stack() && is_visible()) {
  95. hide();
  96. get_scene()->call_group(0,"windows","_cancel_input_ID",p_event.ID);
  97. }
  98. }
  99. }
  100. void Console::_window_resize_event() {
  101. // Control::_window_resize_event();
  102. _resized();
  103. }
  104. void Console::_resized() {
  105. set_pos( Point2( 0, OS::get_singleton()->get_video_mode().height-height) );
  106. set_size( Size2( OS::get_singleton()->get_video_mode().width, height) );
  107. }
  108. void Console::_notification(int p_what) {
  109. switch(p_what) {
  110. case NOTIFICATION_ENTER_SCENE: {
  111. _resized();
  112. show();
  113. globals_property_editor->edit( Globals::get_singleton() );
  114. } break;
  115. case NOTIFICATION_PROCESS: {
  116. //pop messies
  117. if (output_queue_mutex)
  118. output_queue_mutex->lock();
  119. while(output_queue.size()) {
  120. OutputQueue q = output_queue.front()->get();
  121. if (q.type==OutputStrings::LINE_ERROR || q.type==OutputStrings::LINE_WARNING)
  122. errors->add_line(q.text,q.meta,q.type);
  123. output->add_line(q.text,q.meta,q.type);
  124. output_queue.pop_front();
  125. }
  126. if (output_queue_mutex)
  127. output_queue_mutex->unlock();
  128. } break;
  129. case NOTIFICATION_DRAW: {
  130. RID ci = get_canvas_item();
  131. get_stylebox("panel","Panel")->draw(ci,Rect2(Point2(),get_size()));
  132. } break;
  133. }
  134. }
  135. void Console::_close_pressed() {
  136. hide();
  137. }
  138. void Console::_inspector_node_selected() {
  139. Node *node = inspect_tree_editor->get_selected();
  140. if (!node)
  141. inspect_property_editor->edit(NULL);
  142. else {
  143. inspect_history.add_object(node->get_instance_ID());
  144. inspect_property_editor->edit(node);
  145. }
  146. }
  147. void Console::_bind_methods() {
  148. ObjectTypeDB::bind_method("_stats_update_timer_callback",&Console::_stats_update_timer_callback);
  149. ObjectTypeDB::bind_method("_close_pressed",&Console::_close_pressed);
  150. ObjectTypeDB::bind_method("_inspector_node_selected",&Console::_inspector_node_selected);
  151. }
  152. Console::Console() {
  153. Ref<Theme> theme( memnew( Theme ) );
  154. set_theme( theme );
  155. editor_register_icons(theme);
  156. height=300;
  157. tabs = memnew( TabContainer );
  158. tabs->set_tab_align(TabContainer::ALIGN_LEFT);
  159. add_child(tabs);
  160. tabs->set_area_as_parent_rect();
  161. output = memnew( OutputStrings );
  162. output->set_name("Output");
  163. tabs->add_child(output);
  164. errors = memnew( OutputStrings );
  165. errors->set_name("Errors");
  166. tabs->add_child(errors);
  167. status = memnew( Control );
  168. status->set_name("Stats");
  169. tabs->add_child(status);
  170. inspect = memnew( Control );
  171. inspect->set_name("Inspect");
  172. tabs->add_child(inspect);
  173. globals = memnew( Control );
  174. globals->set_name("Globals");
  175. tabs->add_child(globals);
  176. // stats
  177. stats_tree = memnew( Tree );
  178. stats_tree->set_hide_root(true);
  179. stats_tree->set_columns(2);
  180. status->add_child(stats_tree);
  181. stats_tree->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  182. stats_tree->set_anchor( MARGIN_RIGHT, ANCHOR_RATIO );
  183. stats_tree->set_margin( MARGIN_RIGHT, 0.5 );
  184. stats_tree->set_begin( Point2( 20,25 ) );
  185. stats_tree->set_end( Point2( 0.5,5 ) );
  186. Label *stats_label = memnew( Label );
  187. stats_label->set_text("Engine Statistics:");
  188. stats_label->set_pos( Point2( 5,5 ) );
  189. status->add_child(stats_label);
  190. TreeItem *stats_tree_root = stats_tree->create_item(NULL);
  191. {
  192. //system items
  193. TreeItem *system_item = stats_tree->create_item(stats_tree_root);
  194. system_item->set_text(0,"System");
  195. stats.usage_static_memory_total = stats_tree->create_item(system_item);
  196. stats.usage_static_memory_total->set_text(0,"Total Static Mem");;
  197. stats.usage_static_memory = stats_tree->create_item(system_item);
  198. stats.usage_static_memory->set_text(0,"Static Mem Usage");;
  199. stats.usage_dynamic_memory_total = stats_tree->create_item(system_item);
  200. stats.usage_dynamic_memory_total->set_text(0,"Total Dynamic Mem");;
  201. stats.usage_dynamic_memory = stats_tree->create_item(system_item);
  202. stats.usage_dynamic_memory->set_text(0,"Dynamic Mem Usage");
  203. stats.usage_objects_instanced = stats_tree->create_item(system_item);
  204. stats.usage_objects_instanced->set_text(0,"Instanced Objects");
  205. //render items
  206. TreeItem *render_item = stats_tree->create_item(stats_tree_root);
  207. render_item->set_text(0,"Render");
  208. stats.render_objects_in_frame = stats_tree->create_item(render_item);
  209. stats.render_objects_in_frame->set_text(0,"Visible Objects");
  210. stats.material_changes_in_frame = stats_tree->create_item(render_item);
  211. stats.material_changes_in_frame->set_text(0,"Material Changes");
  212. stats.usage_video_mem_total = stats_tree->create_item(render_item);
  213. stats.usage_video_mem_total->set_text(0,"Total Video Mem");
  214. stats.usage_texture_mem_used = stats_tree->create_item(render_item);
  215. stats.usage_texture_mem_used->set_text(0,"Texture Mem Usage");
  216. stats.usage_vertex_mem_used = stats_tree->create_item(render_item);
  217. stats.usage_vertex_mem_used->set_text(0,"Vertex Mem Usage");
  218. stats.usage_video_mem_used = stats_tree->create_item(render_item);
  219. stats.usage_video_mem_used->set_text(0,"Combined Mem Usage");
  220. }
  221. {
  222. inspect_tree_editor = memnew( SceneTreeEditor );
  223. inspect_tree_editor->set_anchor( MARGIN_RIGHT, ANCHOR_RATIO );
  224. inspect_tree_editor->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  225. inspect_tree_editor->set_begin( Point2( 20, 5 ) );
  226. inspect_tree_editor->set_end( Point2( 0.49, 5 ) );
  227. inspect->add_child(inspect_tree_editor);
  228. inspect_property_editor = memnew( PropertyEditor );
  229. inspect_property_editor->set_anchor( MARGIN_LEFT, ANCHOR_RATIO );
  230. inspect_property_editor->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  231. inspect_property_editor->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  232. inspect_property_editor->set_begin( Point2( 0.51, 5 ) );
  233. inspect_property_editor->set_end( Point2( 5, 5 ) );
  234. inspect->add_child(inspect_property_editor);
  235. }
  236. { //globals
  237. globals_property_editor = memnew( PropertyEditor );
  238. globals_property_editor->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  239. globals_property_editor->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  240. globals_property_editor->set_begin( Point2( 15, 5 ) );
  241. globals_property_editor->set_end( Point2( 5, 5 ) );
  242. globals_property_editor->get_top_label()->set_text("Globals Editor:");
  243. globals->add_child(globals_property_editor);
  244. }
  245. #ifndef NO_THREADS
  246. output_queue_mutex = Mutex::create();
  247. #else
  248. output_queue_mutex = NULL;
  249. #endif
  250. hide();
  251. set_process(true);
  252. close = memnew( Button );
  253. add_child(close);
  254. close->set_anchor( MARGIN_LEFT, ANCHOR_END);
  255. close->set_anchor( MARGIN_RIGHT, ANCHOR_END);
  256. close->set_begin( Point2( 25, 3 ) );
  257. close->set_end( Point2( 5, 3 ) );
  258. close->set_flat(true);
  259. close->connect("pressed", this,"_close_pressed");
  260. close->set_icon( get_icon("close","Icons") );
  261. // force_top_viewport(true);
  262. err_handler.userdata=this;
  263. err_handler.errfunc=_error_handle;
  264. add_error_handler(&err_handler);
  265. print_handler.userdata=this;
  266. print_handler.printfunc=_print_handle;
  267. add_print_handler(&print_handler);
  268. Timer *timer = memnew( Timer );
  269. add_child(timer);
  270. timer->set_wait_time(1);
  271. timer->start();
  272. timer->connect("timeout", this,"_stats_update_timer_callback");
  273. inspect_tree_editor->connect("node_selected", this,"_inspector_node_selected");
  274. }
  275. Console::~Console() {
  276. if (output_queue_mutex)
  277. memdelete(output_queue_mutex);
  278. remove_error_handler(&err_handler);
  279. remove_print_handler(&print_handler);
  280. }