editor_run_native.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*************************************************************************/
  2. /* editor_run_native.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_run_native.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_scale.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/export/editor_export_platform.h"
  35. void EditorRunNative::_notification(int p_what) {
  36. switch (p_what) {
  37. case NOTIFICATION_ENTER_TREE: {
  38. for (int i = 0; i < EditorExport::get_singleton()->get_export_platform_count(); i++) {
  39. Ref<EditorExportPlatform> eep = EditorExport::get_singleton()->get_export_platform(i);
  40. if (eep.is_null()) {
  41. continue;
  42. }
  43. Ref<ImageTexture> icon = eep->get_run_icon();
  44. if (!icon.is_null()) {
  45. Ref<Image> im = icon->get_image();
  46. im = im->duplicate();
  47. im->clear_mipmaps();
  48. if (!im->is_empty()) {
  49. im->resize(16 * EDSCALE, 16 * EDSCALE);
  50. Ref<ImageTexture> small_icon = ImageTexture::create_from_image(im);
  51. MenuButton *mb = memnew(MenuButton);
  52. mb->get_popup()->connect("id_pressed", callable_mp(this, &EditorRunNative::run_native).bind(i));
  53. mb->connect("pressed", callable_mp(this, &EditorRunNative::run_native).bind(-1, i));
  54. mb->set_icon(small_icon);
  55. add_child(mb);
  56. menus[i] = mb;
  57. }
  58. }
  59. }
  60. } break;
  61. case NOTIFICATION_PROCESS: {
  62. bool changed = EditorExport::get_singleton()->poll_export_platforms() || first;
  63. if (changed) {
  64. for (KeyValue<int, MenuButton *> &E : menus) {
  65. Ref<EditorExportPlatform> eep = EditorExport::get_singleton()->get_export_platform(E.key);
  66. MenuButton *mb = E.value;
  67. int dc = eep->get_options_count();
  68. if (dc == 0) {
  69. mb->hide();
  70. } else {
  71. mb->get_popup()->clear();
  72. mb->show();
  73. if (dc == 1) {
  74. mb->set_tooltip_text(eep->get_option_tooltip(0));
  75. } else {
  76. mb->set_tooltip_text(eep->get_options_tooltip());
  77. for (int i = 0; i < dc; i++) {
  78. mb->get_popup()->add_icon_item(eep->get_option_icon(i), eep->get_option_label(i));
  79. mb->get_popup()->set_item_tooltip(-1, eep->get_option_tooltip(i));
  80. }
  81. }
  82. }
  83. }
  84. first = false;
  85. }
  86. } break;
  87. }
  88. }
  89. Error EditorRunNative::run_native(int p_idx, int p_platform) {
  90. if (!EditorNode::get_singleton()->ensure_main_scene(true)) {
  91. resume_idx = p_idx;
  92. resume_platform = p_platform;
  93. return OK;
  94. }
  95. Ref<EditorExportPlatform> eep = EditorExport::get_singleton()->get_export_platform(p_platform);
  96. ERR_FAIL_COND_V(eep.is_null(), ERR_UNAVAILABLE);
  97. if (p_idx == -1) {
  98. if (eep->get_options_count() == 1) {
  99. menus[p_platform]->get_popup()->hide();
  100. p_idx = 0;
  101. } else {
  102. return ERR_INVALID_PARAMETER;
  103. }
  104. }
  105. Ref<EditorExportPreset> preset;
  106. for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); i++) {
  107. Ref<EditorExportPreset> ep = EditorExport::get_singleton()->get_export_preset(i);
  108. if (ep->is_runnable() && ep->get_platform() == eep) {
  109. preset = ep;
  110. break;
  111. }
  112. }
  113. if (preset.is_null()) {
  114. EditorNode::get_singleton()->show_warning(TTR("No runnable export preset found for this platform.\nPlease add a runnable preset in the Export menu or define an existing preset as runnable."));
  115. return ERR_UNAVAILABLE;
  116. }
  117. emit_signal(SNAME("native_run"), preset);
  118. int flags = 0;
  119. bool deploy_debug_remote = is_deploy_debug_remote_enabled();
  120. bool deploy_dumb = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_file_server", false);
  121. bool debug_collisions = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_collisons", false);
  122. bool debug_navigation = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_navigation", false);
  123. if (deploy_debug_remote) {
  124. flags |= EditorExportPlatform::DEBUG_FLAG_REMOTE_DEBUG;
  125. }
  126. if (deploy_dumb) {
  127. flags |= EditorExportPlatform::DEBUG_FLAG_DUMB_CLIENT;
  128. }
  129. if (debug_collisions) {
  130. flags |= EditorExportPlatform::DEBUG_FLAG_VIEW_COLLISONS;
  131. }
  132. if (debug_navigation) {
  133. flags |= EditorExportPlatform::DEBUG_FLAG_VIEW_NAVIGATION;
  134. }
  135. eep->clear_messages();
  136. Error err = eep->run(preset, p_idx, flags);
  137. result_dialog_log->clear();
  138. if (eep->fill_log_messages(result_dialog_log, err)) {
  139. result_dialog->popup_centered_ratio(0.5);
  140. }
  141. return err;
  142. }
  143. void EditorRunNative::resume_run_native() {
  144. run_native(resume_idx, resume_platform);
  145. }
  146. void EditorRunNative::_bind_methods() {
  147. ADD_SIGNAL(MethodInfo("native_run", PropertyInfo(Variant::OBJECT, "preset", PROPERTY_HINT_RESOURCE_TYPE, "EditorExportPreset")));
  148. }
  149. bool EditorRunNative::is_deploy_debug_remote_enabled() const {
  150. return EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_deploy_remote_debug", false);
  151. }
  152. EditorRunNative::EditorRunNative() {
  153. result_dialog = memnew(AcceptDialog);
  154. result_dialog->set_title(TTR("Project Run"));
  155. result_dialog_log = memnew(RichTextLabel);
  156. result_dialog_log->set_custom_minimum_size(Size2(300, 80) * EDSCALE);
  157. result_dialog->add_child(result_dialog_log);
  158. add_child(result_dialog);
  159. result_dialog->hide();
  160. set_process(true);
  161. resume_idx = 0;
  162. resume_platform = 0;
  163. }