scene_debugger.cpp 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160
  1. /**************************************************************************/
  2. /* scene_debugger.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "scene_debugger.h"
  31. #include "core/debugger/debugger_marshalls.h"
  32. #include "core/debugger/engine_debugger.h"
  33. #include "core/io/marshalls.h"
  34. #include "core/object/script_language.h"
  35. #include "core/templates/local_vector.h"
  36. #include "scene/2d/physics/collision_object_2d.h"
  37. #include "scene/2d/physics/collision_polygon_2d.h"
  38. #include "scene/2d/physics/collision_shape_2d.h"
  39. #ifndef _3D_DISABLED
  40. #include "scene/3d/label_3d.h"
  41. #include "scene/3d/mesh_instance_3d.h"
  42. #include "scene/3d/physics/collision_object_3d.h"
  43. #include "scene/3d/physics/collision_shape_3d.h"
  44. #include "scene/3d/sprite_3d.h"
  45. #include "scene/resources/surface_tool.h"
  46. #endif // _3D_DISABLED
  47. #include "scene/gui/popup_menu.h"
  48. #include "scene/main/canvas_layer.h"
  49. #include "scene/main/scene_tree.h"
  50. #include "scene/main/window.h"
  51. #include "scene/resources/packed_scene.h"
  52. #include "scene/theme/theme_db.h"
  53. SceneDebugger::SceneDebugger() {
  54. singleton = this;
  55. #ifdef DEBUG_ENABLED
  56. LiveEditor::singleton = memnew(LiveEditor);
  57. RuntimeNodeSelect::singleton = memnew(RuntimeNodeSelect);
  58. EngineDebugger::register_message_capture("scene", EngineDebugger::Capture(nullptr, SceneDebugger::parse_message));
  59. #endif
  60. }
  61. SceneDebugger::~SceneDebugger() {
  62. #ifdef DEBUG_ENABLED
  63. if (LiveEditor::singleton) {
  64. EngineDebugger::unregister_message_capture("scene");
  65. memdelete(LiveEditor::singleton);
  66. LiveEditor::singleton = nullptr;
  67. }
  68. if (RuntimeNodeSelect::singleton) {
  69. memdelete(RuntimeNodeSelect::singleton);
  70. RuntimeNodeSelect::singleton = nullptr;
  71. }
  72. #endif
  73. singleton = nullptr;
  74. }
  75. void SceneDebugger::initialize() {
  76. if (EngineDebugger::is_active()) {
  77. memnew(SceneDebugger);
  78. }
  79. }
  80. void SceneDebugger::deinitialize() {
  81. if (singleton) {
  82. memdelete(singleton);
  83. }
  84. }
  85. #ifdef DEBUG_ENABLED
  86. void SceneDebugger::_handle_input(const Ref<InputEvent> &p_event, const Ref<Shortcut> &p_shortcut) {
  87. Ref<InputEventKey> k = p_event;
  88. if (k.is_valid() && k->is_pressed() && !k->is_echo() && p_shortcut->matches_event(k)) {
  89. EngineDebugger::get_singleton()->send_message("request_quit", Array());
  90. }
  91. }
  92. Error SceneDebugger::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) {
  93. SceneTree *scene_tree = SceneTree::get_singleton();
  94. if (!scene_tree) {
  95. return ERR_UNCONFIGURED;
  96. }
  97. LiveEditor *live_editor = LiveEditor::get_singleton();
  98. if (!live_editor) {
  99. return ERR_UNCONFIGURED;
  100. }
  101. RuntimeNodeSelect *runtime_node_select = RuntimeNodeSelect::get_singleton();
  102. if (!runtime_node_select) {
  103. return ERR_UNCONFIGURED;
  104. }
  105. r_captured = true;
  106. if (p_msg == "setup_scene") {
  107. SceneTree::get_singleton()->get_root()->connect(SceneStringName(window_input), callable_mp_static(SceneDebugger::_handle_input).bind(DebuggerMarshalls::deserialize_key_shortcut(p_args)));
  108. } else if (p_msg == "request_scene_tree") { // Scene tree
  109. live_editor->_send_tree();
  110. } else if (p_msg == "save_node") { // Save node.
  111. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  112. _save_node(p_args[0], p_args[1]);
  113. Array arr;
  114. arr.append(p_args[1]);
  115. EngineDebugger::get_singleton()->send_message("filesystem:update_file", { arr });
  116. } else if (p_msg == "inspect_object") { // Object Inspect
  117. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  118. ObjectID id = p_args[0];
  119. _send_object_id(id);
  120. } else if (p_msg == "suspend_changed") {
  121. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  122. bool suspended = p_args[0];
  123. scene_tree->set_suspend(suspended);
  124. runtime_node_select->_update_input_state();
  125. } else if (p_msg == "next_frame") {
  126. _next_frame();
  127. } else if (p_msg == "override_cameras") { // Camera
  128. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  129. bool enable = p_args[0];
  130. bool from_editor = p_args[1];
  131. scene_tree->get_root()->enable_canvas_transform_override(enable);
  132. #ifndef _3D_DISABLED
  133. scene_tree->get_root()->enable_camera_3d_override(enable);
  134. #endif // _3D_DISABLED
  135. runtime_node_select->_set_camera_override_enabled(enable && !from_editor);
  136. } else if (p_msg == "transform_camera_2d") {
  137. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  138. Transform2D transform = p_args[0];
  139. scene_tree->get_root()->set_canvas_transform_override(transform);
  140. runtime_node_select->_queue_selection_update();
  141. #ifndef _3D_DISABLED
  142. } else if (p_msg == "transform_camera_3d") {
  143. ERR_FAIL_COND_V(p_args.size() < 5, ERR_INVALID_DATA);
  144. Transform3D transform = p_args[0];
  145. bool is_perspective = p_args[1];
  146. float size_or_fov = p_args[2];
  147. float depth_near = p_args[3];
  148. float depth_far = p_args[4];
  149. if (is_perspective) {
  150. scene_tree->get_root()->set_camera_3d_override_perspective(size_or_fov, depth_near, depth_far);
  151. } else {
  152. scene_tree->get_root()->set_camera_3d_override_orthogonal(size_or_fov, depth_near, depth_far);
  153. }
  154. scene_tree->get_root()->set_camera_3d_override_transform(transform);
  155. runtime_node_select->_queue_selection_update();
  156. #endif // _3D_DISABLED
  157. } else if (p_msg == "set_object_property") {
  158. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  159. _set_object_property(p_args[0], p_args[1], p_args[2]);
  160. runtime_node_select->_queue_selection_update();
  161. } else if (p_msg == "reload_cached_files") {
  162. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  163. PackedStringArray files = p_args[0];
  164. reload_cached_files(files);
  165. } else if (p_msg.begins_with("live_")) { /// Live Edit
  166. if (p_msg == "live_set_root") {
  167. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  168. live_editor->_root_func(p_args[0], p_args[1]);
  169. } else if (p_msg == "live_node_path") {
  170. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  171. live_editor->_node_path_func(p_args[0], p_args[1]);
  172. } else if (p_msg == "live_res_path") {
  173. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  174. live_editor->_res_path_func(p_args[0], p_args[1]);
  175. } else if (p_msg == "live_node_prop_res") {
  176. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  177. live_editor->_node_set_res_func(p_args[0], p_args[1], p_args[2]);
  178. } else if (p_msg == "live_node_prop") {
  179. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  180. live_editor->_node_set_func(p_args[0], p_args[1], p_args[2]);
  181. } else if (p_msg == "live_res_prop_res") {
  182. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  183. live_editor->_res_set_res_func(p_args[0], p_args[1], p_args[2]);
  184. } else if (p_msg == "live_res_prop") {
  185. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  186. live_editor->_res_set_func(p_args[0], p_args[1], p_args[2]);
  187. } else if (p_msg == "live_node_call") {
  188. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  189. LocalVector<Variant> args;
  190. LocalVector<Variant *> argptrs;
  191. args.resize(p_args.size() - 2);
  192. argptrs.resize(args.size());
  193. for (uint32_t i = 0; i < args.size(); i++) {
  194. args[i] = p_args[i + 2];
  195. argptrs[i] = &args[i];
  196. }
  197. live_editor->_node_call_func(p_args[0], p_args[1], argptrs.size() ? (const Variant **)argptrs.ptr() : nullptr, argptrs.size());
  198. } else if (p_msg == "live_res_call") {
  199. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  200. LocalVector<Variant> args;
  201. LocalVector<Variant *> argptrs;
  202. args.resize(p_args.size() - 2);
  203. argptrs.resize(args.size());
  204. for (uint32_t i = 0; i < args.size(); i++) {
  205. args[i] = p_args[i + 2];
  206. argptrs[i] = &args[i];
  207. }
  208. live_editor->_res_call_func(p_args[0], p_args[1], argptrs.size() ? (const Variant **)argptrs.ptr() : nullptr, argptrs.size());
  209. } else if (p_msg == "live_create_node") {
  210. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  211. live_editor->_create_node_func(p_args[0], p_args[1], p_args[2]);
  212. } else if (p_msg == "live_instantiate_node") {
  213. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  214. live_editor->_instance_node_func(p_args[0], p_args[1], p_args[2]);
  215. } else if (p_msg == "live_remove_node") {
  216. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  217. live_editor->_remove_node_func(p_args[0]);
  218. if (!runtime_node_select->has_selection) {
  219. runtime_node_select->_clear_selection();
  220. }
  221. } else if (p_msg == "live_remove_and_keep_node") {
  222. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  223. live_editor->_remove_and_keep_node_func(p_args[0], p_args[1]);
  224. if (!runtime_node_select->has_selection) {
  225. runtime_node_select->_clear_selection();
  226. }
  227. } else if (p_msg == "live_restore_node") {
  228. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  229. live_editor->_restore_node_func(p_args[0], p_args[1], p_args[2]);
  230. } else if (p_msg == "live_duplicate_node") {
  231. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  232. live_editor->_duplicate_node_func(p_args[0], p_args[1]);
  233. } else if (p_msg == "live_reparent_node") {
  234. ERR_FAIL_COND_V(p_args.size() < 4, ERR_INVALID_DATA);
  235. live_editor->_reparent_node_func(p_args[0], p_args[1], p_args[2], p_args[3]);
  236. } else {
  237. return ERR_SKIP;
  238. }
  239. } else if (p_msg.begins_with("runtime_node_select_")) { /// Runtime Node Selection
  240. if (p_msg == "runtime_node_select_setup") {
  241. ERR_FAIL_COND_V(p_args.is_empty() || p_args[0].get_type() != Variant::DICTIONARY, ERR_INVALID_DATA);
  242. runtime_node_select->_setup(p_args[0]);
  243. } else if (p_msg == "runtime_node_select_set_type") {
  244. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  245. RuntimeNodeSelect::NodeType type = (RuntimeNodeSelect::NodeType)(int)p_args[0];
  246. runtime_node_select->_node_set_type(type);
  247. } else if (p_msg == "runtime_node_select_set_mode") {
  248. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  249. RuntimeNodeSelect::SelectMode mode = (RuntimeNodeSelect::SelectMode)(int)p_args[0];
  250. runtime_node_select->_select_set_mode(mode);
  251. } else if (p_msg == "runtime_node_select_set_visible") {
  252. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  253. bool visible = p_args[0];
  254. runtime_node_select->_set_selection_visible(visible);
  255. } else if (p_msg == "runtime_node_select_reset_camera_2d") {
  256. runtime_node_select->_reset_camera_2d();
  257. #ifndef _3D_DISABLED
  258. } else if (p_msg == "runtime_node_select_reset_camera_3d") {
  259. runtime_node_select->_reset_camera_3d();
  260. #endif // _3D_DISABLED
  261. } else {
  262. return ERR_SKIP;
  263. }
  264. } else {
  265. r_captured = false;
  266. }
  267. return OK;
  268. }
  269. void SceneDebugger::_save_node(ObjectID id, const String &p_path) {
  270. Node *node = Object::cast_to<Node>(ObjectDB::get_instance(id));
  271. ERR_FAIL_NULL(node);
  272. #ifdef TOOLS_ENABLED
  273. HashMap<const Node *, Node *> duplimap;
  274. Node *copy = node->duplicate_from_editor(duplimap);
  275. #else
  276. Node *copy = node->duplicate();
  277. #endif
  278. // Handle Unique Nodes.
  279. for (int i = 0; i < copy->get_child_count(false); i++) {
  280. _set_node_owner_recursive(copy->get_child(i, false), copy);
  281. }
  282. // Root node cannot ever be unique name in its own Scene!
  283. copy->set_unique_name_in_owner(false);
  284. Ref<PackedScene> ps = memnew(PackedScene);
  285. ps->pack(copy);
  286. ResourceSaver::save(ps, p_path);
  287. memdelete(copy);
  288. }
  289. void SceneDebugger::_set_node_owner_recursive(Node *p_node, Node *p_owner) {
  290. if (!p_node->get_owner()) {
  291. p_node->set_owner(p_owner);
  292. }
  293. for (int i = 0; i < p_node->get_child_count(false); i++) {
  294. _set_node_owner_recursive(p_node->get_child(i, false), p_owner);
  295. }
  296. }
  297. void SceneDebugger::_send_object_id(ObjectID p_id, int p_max_size) {
  298. SceneDebuggerObject obj(p_id);
  299. if (obj.id.is_null()) {
  300. return;
  301. }
  302. Node *node = Object::cast_to<Node>(ObjectDB::get_instance(p_id));
  303. RuntimeNodeSelect::get_singleton()->_select_node(node);
  304. Array arr;
  305. obj.serialize(arr);
  306. EngineDebugger::get_singleton()->send_message("scene:inspect_object", arr);
  307. }
  308. void SceneDebugger::_set_object_property(ObjectID p_id, const String &p_property, const Variant &p_value) {
  309. Object *obj = ObjectDB::get_instance(p_id);
  310. if (!obj) {
  311. return;
  312. }
  313. String prop_name = p_property;
  314. if (p_property.begins_with("Members/")) {
  315. Vector<String> ss = p_property.split("/");
  316. prop_name = ss[ss.size() - 1];
  317. }
  318. obj->set(prop_name, p_value);
  319. }
  320. void SceneDebugger::_next_frame() {
  321. SceneTree *scene_tree = SceneTree::get_singleton();
  322. if (!scene_tree->is_suspended()) {
  323. return;
  324. }
  325. scene_tree->set_suspend(false);
  326. RenderingServer::get_singleton()->connect("frame_post_draw", callable_mp(scene_tree, &SceneTree::set_suspend).bind(true), Object::CONNECT_ONE_SHOT);
  327. }
  328. void SceneDebugger::add_to_cache(const String &p_filename, Node *p_node) {
  329. LiveEditor *debugger = LiveEditor::get_singleton();
  330. if (!debugger) {
  331. return;
  332. }
  333. if (EngineDebugger::get_script_debugger() && !p_filename.is_empty()) {
  334. debugger->live_scene_edit_cache[p_filename].insert(p_node);
  335. }
  336. }
  337. void SceneDebugger::remove_from_cache(const String &p_filename, Node *p_node) {
  338. LiveEditor *debugger = LiveEditor::get_singleton();
  339. if (!debugger) {
  340. return;
  341. }
  342. HashMap<String, HashSet<Node *>> &edit_cache = debugger->live_scene_edit_cache;
  343. HashMap<String, HashSet<Node *>>::Iterator E = edit_cache.find(p_filename);
  344. if (E) {
  345. E->value.erase(p_node);
  346. if (E->value.size() == 0) {
  347. edit_cache.remove(E);
  348. }
  349. }
  350. HashMap<Node *, HashMap<ObjectID, Node *>> &remove_list = debugger->live_edit_remove_list;
  351. HashMap<Node *, HashMap<ObjectID, Node *>>::Iterator F = remove_list.find(p_node);
  352. if (F) {
  353. for (const KeyValue<ObjectID, Node *> &G : F->value) {
  354. memdelete(G.value);
  355. }
  356. remove_list.remove(F);
  357. }
  358. }
  359. void SceneDebugger::reload_cached_files(const PackedStringArray &p_files) {
  360. for (const String &file : p_files) {
  361. Ref<Resource> res = ResourceCache::get_ref(file);
  362. if (res.is_valid()) {
  363. res->reload_from_file();
  364. }
  365. }
  366. }
  367. /// SceneDebuggerObject
  368. SceneDebuggerObject::SceneDebuggerObject(ObjectID p_id) {
  369. id = ObjectID();
  370. Object *obj = ObjectDB::get_instance(p_id);
  371. if (!obj) {
  372. return;
  373. }
  374. id = p_id;
  375. class_name = obj->get_class();
  376. if (ScriptInstance *si = obj->get_script_instance()) {
  377. // Read script instance constants and variables
  378. if (!si->get_script().is_null()) {
  379. Script *s = si->get_script().ptr();
  380. _parse_script_properties(s, si);
  381. }
  382. }
  383. if (Node *node = Object::cast_to<Node>(obj)) {
  384. // For debugging multiplayer.
  385. {
  386. PropertyInfo pi(Variant::INT, String("Node/multiplayer_authority"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY);
  387. properties.push_back(SceneDebuggerProperty(pi, node->get_multiplayer_authority()));
  388. }
  389. // Add specialized NodePath info (if inside tree).
  390. if (node->is_inside_tree()) {
  391. PropertyInfo pi(Variant::NODE_PATH, String("Node/path"));
  392. properties.push_back(SceneDebuggerProperty(pi, node->get_path()));
  393. } else { // Can't ask for path if a node is not in tree.
  394. PropertyInfo pi(Variant::STRING, String("Node/path"));
  395. properties.push_back(SceneDebuggerProperty(pi, "[Orphan]"));
  396. }
  397. } else if (Script *s = Object::cast_to<Script>(obj)) {
  398. // Add script constants (no instance).
  399. _parse_script_properties(s, nullptr);
  400. }
  401. // Add base object properties.
  402. List<PropertyInfo> pinfo;
  403. obj->get_property_list(&pinfo, true);
  404. for (const PropertyInfo &E : pinfo) {
  405. if (E.usage & (PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CATEGORY)) {
  406. properties.push_back(SceneDebuggerProperty(E, obj->get(E.name)));
  407. }
  408. }
  409. }
  410. void SceneDebuggerObject::_parse_script_properties(Script *p_script, ScriptInstance *p_instance) {
  411. typedef HashMap<const Script *, HashSet<StringName>> ScriptMemberMap;
  412. typedef HashMap<const Script *, HashMap<StringName, Variant>> ScriptConstantsMap;
  413. ScriptMemberMap members;
  414. if (p_instance) {
  415. members[p_script] = HashSet<StringName>();
  416. p_script->get_members(&(members[p_script]));
  417. }
  418. ScriptConstantsMap constants;
  419. constants[p_script] = HashMap<StringName, Variant>();
  420. p_script->get_constants(&(constants[p_script]));
  421. Ref<Script> base = p_script->get_base_script();
  422. while (base.is_valid()) {
  423. if (p_instance) {
  424. members[base.ptr()] = HashSet<StringName>();
  425. base->get_members(&(members[base.ptr()]));
  426. }
  427. constants[base.ptr()] = HashMap<StringName, Variant>();
  428. base->get_constants(&(constants[base.ptr()]));
  429. base = base->get_base_script();
  430. }
  431. // Members
  432. for (KeyValue<const Script *, HashSet<StringName>> sm : members) {
  433. for (const StringName &E : sm.value) {
  434. Variant m;
  435. if (p_instance->get(E, m)) {
  436. String script_path = sm.key == p_script ? "" : sm.key->get_path().get_file() + "/";
  437. PropertyInfo pi(m.get_type(), "Members/" + script_path + E);
  438. properties.push_back(SceneDebuggerProperty(pi, m));
  439. }
  440. }
  441. }
  442. // Constants
  443. for (KeyValue<const Script *, HashMap<StringName, Variant>> &sc : constants) {
  444. for (const KeyValue<StringName, Variant> &E : sc.value) {
  445. String script_path = sc.key == p_script ? "" : sc.key->get_path().get_file() + "/";
  446. if (E.value.get_type() == Variant::OBJECT) {
  447. Variant inst_id = ((Object *)E.value)->get_instance_id();
  448. PropertyInfo pi(inst_id.get_type(), "Constants/" + E.key, PROPERTY_HINT_OBJECT_ID, "Object");
  449. properties.push_back(SceneDebuggerProperty(pi, inst_id));
  450. } else {
  451. PropertyInfo pi(E.value.get_type(), "Constants/" + script_path + E.key);
  452. properties.push_back(SceneDebuggerProperty(pi, E.value));
  453. }
  454. }
  455. }
  456. }
  457. void SceneDebuggerObject::serialize(Array &r_arr, int p_max_size) {
  458. Array send_props;
  459. for (SceneDebuggerObject::SceneDebuggerProperty &property : properties) {
  460. const PropertyInfo &pi = property.first;
  461. Variant &var = property.second;
  462. Ref<Resource> res = var;
  463. Array prop;
  464. prop.push_back(pi.name);
  465. prop.push_back(pi.type);
  466. PropertyHint hint = pi.hint;
  467. String hint_string = pi.hint_string;
  468. if (!res.is_null() && !res->get_path().is_empty()) {
  469. var = res->get_path();
  470. } else { //only send information that can be sent..
  471. int len = 0; //test how big is this to encode
  472. encode_variant(var, nullptr, len);
  473. if (len > p_max_size) { //limit to max size
  474. hint = PROPERTY_HINT_OBJECT_TOO_BIG;
  475. hint_string = "";
  476. var = Variant();
  477. }
  478. }
  479. prop.push_back(hint);
  480. prop.push_back(hint_string);
  481. prop.push_back(pi.usage);
  482. prop.push_back(var);
  483. send_props.push_back(prop);
  484. }
  485. r_arr.push_back(uint64_t(id));
  486. r_arr.push_back(class_name);
  487. r_arr.push_back(send_props);
  488. }
  489. void SceneDebuggerObject::deserialize(const Array &p_arr) {
  490. #define CHECK_TYPE(p_what, p_type) ERR_FAIL_COND(p_what.get_type() != Variant::p_type);
  491. ERR_FAIL_COND(p_arr.size() < 3);
  492. CHECK_TYPE(p_arr[0], INT);
  493. CHECK_TYPE(p_arr[1], STRING);
  494. CHECK_TYPE(p_arr[2], ARRAY);
  495. id = uint64_t(p_arr[0]);
  496. class_name = p_arr[1];
  497. Array props = p_arr[2];
  498. for (int i = 0; i < props.size(); i++) {
  499. CHECK_TYPE(props[i], ARRAY);
  500. Array prop = props[i];
  501. ERR_FAIL_COND(prop.size() != 6);
  502. CHECK_TYPE(prop[0], STRING);
  503. CHECK_TYPE(prop[1], INT);
  504. CHECK_TYPE(prop[2], INT);
  505. CHECK_TYPE(prop[3], STRING);
  506. CHECK_TYPE(prop[4], INT);
  507. PropertyInfo pinfo;
  508. pinfo.name = prop[0];
  509. pinfo.type = Variant::Type(int(prop[1]));
  510. pinfo.hint = PropertyHint(int(prop[2]));
  511. pinfo.hint_string = prop[3];
  512. pinfo.usage = PropertyUsageFlags(int(prop[4]));
  513. Variant var = prop[5];
  514. if (pinfo.type == Variant::OBJECT) {
  515. if (var.is_zero()) {
  516. var = Ref<Resource>();
  517. } else if (var.get_type() == Variant::OBJECT) {
  518. if (((Object *)var)->is_class("EncodedObjectAsID")) {
  519. var = Object::cast_to<EncodedObjectAsID>(var)->get_object_id();
  520. pinfo.type = var.get_type();
  521. pinfo.hint = PROPERTY_HINT_OBJECT_ID;
  522. pinfo.hint_string = "Object";
  523. }
  524. }
  525. }
  526. properties.push_back(SceneDebuggerProperty(pinfo, var));
  527. }
  528. }
  529. /// SceneDebuggerTree
  530. SceneDebuggerTree::SceneDebuggerTree(Node *p_root) {
  531. // Flatten tree into list, depth first, use stack to avoid recursion.
  532. List<Node *> stack;
  533. stack.push_back(p_root);
  534. bool is_root = true;
  535. const StringName &is_visible_sn = SNAME("is_visible");
  536. const StringName &is_visible_in_tree_sn = SNAME("is_visible_in_tree");
  537. while (stack.size()) {
  538. Node *n = stack.front()->get();
  539. stack.pop_front();
  540. int count = n->get_child_count();
  541. for (int i = 0; i < count; i++) {
  542. stack.push_front(n->get_child(count - i - 1));
  543. }
  544. int view_flags = 0;
  545. if (is_root) {
  546. // Prevent root window visibility from being changed.
  547. is_root = false;
  548. } else if (n->has_method(is_visible_sn)) {
  549. const Variant visible = n->call(is_visible_sn);
  550. if (visible.get_type() == Variant::BOOL) {
  551. view_flags = RemoteNode::VIEW_HAS_VISIBLE_METHOD;
  552. view_flags |= uint8_t(visible) * RemoteNode::VIEW_VISIBLE;
  553. }
  554. if (n->has_method(is_visible_in_tree_sn)) {
  555. const Variant visible_in_tree = n->call(is_visible_in_tree_sn);
  556. if (visible_in_tree.get_type() == Variant::BOOL) {
  557. view_flags |= uint8_t(visible_in_tree) * RemoteNode::VIEW_VISIBLE_IN_TREE;
  558. }
  559. }
  560. }
  561. String class_name;
  562. ScriptInstance *script_instance = n->get_script_instance();
  563. if (script_instance) {
  564. Ref<Script> script = script_instance->get_script();
  565. if (script.is_valid()) {
  566. class_name = script->get_global_name();
  567. if (class_name.is_empty()) {
  568. // If there is no class_name in this script we just take the script path.
  569. class_name = script->get_path();
  570. }
  571. }
  572. }
  573. nodes.push_back(RemoteNode(count, n->get_name(), class_name.is_empty() ? n->get_class() : class_name, n->get_instance_id(), n->get_scene_file_path(), view_flags));
  574. }
  575. }
  576. void SceneDebuggerTree::serialize(Array &p_arr) {
  577. for (const RemoteNode &n : nodes) {
  578. p_arr.push_back(n.child_count);
  579. p_arr.push_back(n.name);
  580. p_arr.push_back(n.type_name);
  581. p_arr.push_back(n.id);
  582. p_arr.push_back(n.scene_file_path);
  583. p_arr.push_back(n.view_flags);
  584. }
  585. }
  586. void SceneDebuggerTree::deserialize(const Array &p_arr) {
  587. int idx = 0;
  588. while (p_arr.size() > idx) {
  589. ERR_FAIL_COND(p_arr.size() < 6);
  590. CHECK_TYPE(p_arr[idx], INT); // child_count.
  591. CHECK_TYPE(p_arr[idx + 1], STRING); // name.
  592. CHECK_TYPE(p_arr[idx + 2], STRING); // type_name.
  593. CHECK_TYPE(p_arr[idx + 3], INT); // id.
  594. CHECK_TYPE(p_arr[idx + 4], STRING); // scene_file_path.
  595. CHECK_TYPE(p_arr[idx + 5], INT); // view_flags.
  596. nodes.push_back(RemoteNode(p_arr[idx], p_arr[idx + 1], p_arr[idx + 2], p_arr[idx + 3], p_arr[idx + 4], p_arr[idx + 5]));
  597. idx += 6;
  598. }
  599. }
  600. /// LiveEditor
  601. LiveEditor *LiveEditor::get_singleton() {
  602. return singleton;
  603. }
  604. void LiveEditor::_send_tree() {
  605. SceneTree *scene_tree = SceneTree::get_singleton();
  606. if (!scene_tree) {
  607. return;
  608. }
  609. Array arr;
  610. // Encoded as a flat list depth first.
  611. SceneDebuggerTree tree(scene_tree->root);
  612. tree.serialize(arr);
  613. EngineDebugger::get_singleton()->send_message("scene:scene_tree", arr);
  614. }
  615. void LiveEditor::_node_path_func(const NodePath &p_path, int p_id) {
  616. live_edit_node_path_cache[p_id] = p_path;
  617. }
  618. void LiveEditor::_res_path_func(const String &p_path, int p_id) {
  619. live_edit_resource_cache[p_id] = p_path;
  620. }
  621. void LiveEditor::_node_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  622. SceneTree *scene_tree = SceneTree::get_singleton();
  623. if (!scene_tree) {
  624. return;
  625. }
  626. if (!live_edit_node_path_cache.has(p_id)) {
  627. return;
  628. }
  629. NodePath np = live_edit_node_path_cache[p_id];
  630. Node *base = nullptr;
  631. if (scene_tree->root->has_node(live_edit_root)) {
  632. base = scene_tree->root->get_node(live_edit_root);
  633. }
  634. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  635. if (!E) {
  636. return; //scene not editable
  637. }
  638. for (Node *F : E->value) {
  639. Node *n = F;
  640. if (base && !base->is_ancestor_of(n)) {
  641. continue;
  642. }
  643. if (!n->has_node(np)) {
  644. continue;
  645. }
  646. Node *n2 = n->get_node(np);
  647. // Do not change transform of edited scene root, unless it's the scene being played.
  648. // See GH-86659 for additional context.
  649. bool keep_transform = (n2 == n) && (n2->get_parent() != scene_tree->root);
  650. Variant orig_tf;
  651. if (keep_transform) {
  652. if (n2->is_class("Node3D")) {
  653. orig_tf = n2->call("get_transform");
  654. } else if (n2->is_class("CanvasItem")) {
  655. orig_tf = n2->call("_edit_get_state");
  656. }
  657. }
  658. n2->set(p_prop, p_value);
  659. if (keep_transform) {
  660. if (n2->is_class("Node3D")) {
  661. Variant new_tf = n2->call("get_transform");
  662. if (new_tf != orig_tf) {
  663. n2->call("set_transform", orig_tf);
  664. }
  665. } else if (n2->is_class("CanvasItem")) {
  666. Variant new_tf = n2->call("_edit_get_state");
  667. if (new_tf != orig_tf) {
  668. n2->call("_edit_set_state", orig_tf);
  669. }
  670. }
  671. }
  672. }
  673. }
  674. void LiveEditor::_node_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  675. Ref<Resource> r = ResourceLoader::load(p_value);
  676. if (!r.is_valid()) {
  677. return;
  678. }
  679. _node_set_func(p_id, p_prop, r);
  680. }
  681. void LiveEditor::_node_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) {
  682. SceneTree *scene_tree = SceneTree::get_singleton();
  683. if (!scene_tree) {
  684. return;
  685. }
  686. if (!live_edit_node_path_cache.has(p_id)) {
  687. return;
  688. }
  689. NodePath np = live_edit_node_path_cache[p_id];
  690. Node *base = nullptr;
  691. if (scene_tree->root->has_node(live_edit_root)) {
  692. base = scene_tree->root->get_node(live_edit_root);
  693. }
  694. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  695. if (!E) {
  696. return; //scene not editable
  697. }
  698. for (Node *F : E->value) {
  699. Node *n = F;
  700. if (base && !base->is_ancestor_of(n)) {
  701. continue;
  702. }
  703. if (!n->has_node(np)) {
  704. continue;
  705. }
  706. Node *n2 = n->get_node(np);
  707. // Do not change transform of edited scene root, unless it's the scene being played.
  708. // See GH-86659 for additional context.
  709. bool keep_transform = (n2 == n) && (n2->get_parent() != scene_tree->root);
  710. Variant orig_tf;
  711. if (keep_transform) {
  712. if (n2->is_class("Node3D")) {
  713. orig_tf = n2->call("get_transform");
  714. } else if (n2->is_class("CanvasItem")) {
  715. orig_tf = n2->call("_edit_get_state");
  716. }
  717. }
  718. Callable::CallError ce;
  719. n2->callp(p_method, p_args, p_argcount, ce);
  720. if (keep_transform) {
  721. if (n2->is_class("Node3D")) {
  722. Variant new_tf = n2->call("get_transform");
  723. if (new_tf != orig_tf) {
  724. n2->call("set_transform", orig_tf);
  725. }
  726. } else if (n2->is_class("CanvasItem")) {
  727. Variant new_tf = n2->call("_edit_get_state");
  728. if (new_tf != orig_tf) {
  729. n2->call("_edit_set_state", orig_tf);
  730. }
  731. }
  732. }
  733. }
  734. }
  735. void LiveEditor::_res_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  736. if (!live_edit_resource_cache.has(p_id)) {
  737. return;
  738. }
  739. String resp = live_edit_resource_cache[p_id];
  740. if (!ResourceCache::has(resp)) {
  741. return;
  742. }
  743. Ref<Resource> r = ResourceCache::get_ref(resp);
  744. if (!r.is_valid()) {
  745. return;
  746. }
  747. r->set(p_prop, p_value);
  748. }
  749. void LiveEditor::_res_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  750. Ref<Resource> r = ResourceLoader::load(p_value);
  751. if (!r.is_valid()) {
  752. return;
  753. }
  754. _res_set_func(p_id, p_prop, r);
  755. }
  756. void LiveEditor::_res_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) {
  757. if (!live_edit_resource_cache.has(p_id)) {
  758. return;
  759. }
  760. String resp = live_edit_resource_cache[p_id];
  761. if (!ResourceCache::has(resp)) {
  762. return;
  763. }
  764. Ref<Resource> r = ResourceCache::get_ref(resp);
  765. if (!r.is_valid()) {
  766. return;
  767. }
  768. Callable::CallError ce;
  769. r->callp(p_method, p_args, p_argcount, ce);
  770. }
  771. void LiveEditor::_root_func(const NodePath &p_scene_path, const String &p_scene_from) {
  772. live_edit_root = p_scene_path;
  773. live_edit_scene = p_scene_from;
  774. }
  775. void LiveEditor::_create_node_func(const NodePath &p_parent, const String &p_type, const String &p_name) {
  776. SceneTree *scene_tree = SceneTree::get_singleton();
  777. if (!scene_tree) {
  778. return;
  779. }
  780. Node *base = nullptr;
  781. if (scene_tree->root->has_node(live_edit_root)) {
  782. base = scene_tree->root->get_node(live_edit_root);
  783. }
  784. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  785. if (!E) {
  786. return; //scene not editable
  787. }
  788. for (Node *F : E->value) {
  789. Node *n = F;
  790. if (base && !base->is_ancestor_of(n)) {
  791. continue;
  792. }
  793. if (!n->has_node(p_parent)) {
  794. continue;
  795. }
  796. Node *n2 = n->get_node(p_parent);
  797. Node *no = Object::cast_to<Node>(ClassDB::instantiate(p_type));
  798. if (!no) {
  799. continue;
  800. }
  801. no->set_name(p_name);
  802. n2->add_child(no);
  803. }
  804. }
  805. void LiveEditor::_instance_node_func(const NodePath &p_parent, const String &p_path, const String &p_name) {
  806. SceneTree *scene_tree = SceneTree::get_singleton();
  807. if (!scene_tree) {
  808. return;
  809. }
  810. Ref<PackedScene> ps = ResourceLoader::load(p_path);
  811. if (!ps.is_valid()) {
  812. return;
  813. }
  814. Node *base = nullptr;
  815. if (scene_tree->root->has_node(live_edit_root)) {
  816. base = scene_tree->root->get_node(live_edit_root);
  817. }
  818. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  819. if (!E) {
  820. return; //scene not editable
  821. }
  822. for (Node *F : E->value) {
  823. Node *n = F;
  824. if (base && !base->is_ancestor_of(n)) {
  825. continue;
  826. }
  827. if (!n->has_node(p_parent)) {
  828. continue;
  829. }
  830. Node *n2 = n->get_node(p_parent);
  831. Node *no = ps->instantiate();
  832. if (!no) {
  833. continue;
  834. }
  835. no->set_name(p_name);
  836. n2->add_child(no);
  837. }
  838. }
  839. void LiveEditor::_remove_node_func(const NodePath &p_at) {
  840. SceneTree *scene_tree = SceneTree::get_singleton();
  841. if (!scene_tree) {
  842. return;
  843. }
  844. Node *base = nullptr;
  845. if (scene_tree->root->has_node(live_edit_root)) {
  846. base = scene_tree->root->get_node(live_edit_root);
  847. }
  848. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  849. if (!E) {
  850. return; //scene not editable
  851. }
  852. Vector<Node *> to_delete;
  853. for (HashSet<Node *>::Iterator F = E->value.begin(); F; ++F) {
  854. Node *n = *F;
  855. if (base && !base->is_ancestor_of(n)) {
  856. continue;
  857. }
  858. if (!n->has_node(p_at)) {
  859. continue;
  860. }
  861. Node *n2 = n->get_node(p_at);
  862. to_delete.push_back(n2);
  863. }
  864. for (int i = 0; i < to_delete.size(); i++) {
  865. memdelete(to_delete[i]);
  866. }
  867. }
  868. void LiveEditor::_remove_and_keep_node_func(const NodePath &p_at, ObjectID p_keep_id) {
  869. SceneTree *scene_tree = SceneTree::get_singleton();
  870. if (!scene_tree) {
  871. return;
  872. }
  873. Node *base = nullptr;
  874. if (scene_tree->root->has_node(live_edit_root)) {
  875. base = scene_tree->root->get_node(live_edit_root);
  876. }
  877. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  878. if (!E) {
  879. return; //scene not editable
  880. }
  881. Vector<Node *> to_remove;
  882. for (HashSet<Node *>::Iterator F = E->value.begin(); F; ++F) {
  883. Node *n = *F;
  884. if (base && !base->is_ancestor_of(n)) {
  885. continue;
  886. }
  887. if (!n->has_node(p_at)) {
  888. continue;
  889. }
  890. to_remove.push_back(n);
  891. }
  892. for (int i = 0; i < to_remove.size(); i++) {
  893. Node *n = to_remove[i];
  894. Node *n2 = n->get_node(p_at);
  895. n2->get_parent()->remove_child(n2);
  896. live_edit_remove_list[n][p_keep_id] = n2;
  897. }
  898. }
  899. void LiveEditor::_restore_node_func(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  900. SceneTree *scene_tree = SceneTree::get_singleton();
  901. if (!scene_tree) {
  902. return;
  903. }
  904. Node *base = nullptr;
  905. if (scene_tree->root->has_node(live_edit_root)) {
  906. base = scene_tree->root->get_node(live_edit_root);
  907. }
  908. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  909. if (!E) {
  910. return; //scene not editable
  911. }
  912. for (HashSet<Node *>::Iterator F = E->value.begin(); F;) {
  913. HashSet<Node *>::Iterator N = F;
  914. ++N;
  915. Node *n = *F;
  916. if (base && !base->is_ancestor_of(n)) {
  917. continue;
  918. }
  919. if (!n->has_node(p_at)) {
  920. continue;
  921. }
  922. Node *n2 = n->get_node(p_at);
  923. HashMap<Node *, HashMap<ObjectID, Node *>>::Iterator EN = live_edit_remove_list.find(n);
  924. if (!EN) {
  925. continue;
  926. }
  927. HashMap<ObjectID, Node *>::Iterator FN = EN->value.find(p_id);
  928. if (!FN) {
  929. continue;
  930. }
  931. n2->add_child(FN->value);
  932. EN->value.remove(FN);
  933. if (EN->value.size() == 0) {
  934. live_edit_remove_list.remove(EN);
  935. }
  936. F = N;
  937. }
  938. }
  939. void LiveEditor::_duplicate_node_func(const NodePath &p_at, const String &p_new_name) {
  940. SceneTree *scene_tree = SceneTree::get_singleton();
  941. if (!scene_tree) {
  942. return;
  943. }
  944. Node *base = nullptr;
  945. if (scene_tree->root->has_node(live_edit_root)) {
  946. base = scene_tree->root->get_node(live_edit_root);
  947. }
  948. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  949. if (!E) {
  950. return; //scene not editable
  951. }
  952. for (Node *F : E->value) {
  953. Node *n = F;
  954. if (base && !base->is_ancestor_of(n)) {
  955. continue;
  956. }
  957. if (!n->has_node(p_at)) {
  958. continue;
  959. }
  960. Node *n2 = n->get_node(p_at);
  961. Node *dup = n2->duplicate(Node::DUPLICATE_SIGNALS | Node::DUPLICATE_GROUPS | Node::DUPLICATE_SCRIPTS);
  962. if (!dup) {
  963. continue;
  964. }
  965. dup->set_name(p_new_name);
  966. n2->get_parent()->add_child(dup);
  967. }
  968. }
  969. void LiveEditor::_reparent_node_func(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  970. SceneTree *scene_tree = SceneTree::get_singleton();
  971. if (!scene_tree) {
  972. return;
  973. }
  974. Node *base = nullptr;
  975. if (scene_tree->root->has_node(live_edit_root)) {
  976. base = scene_tree->root->get_node(live_edit_root);
  977. }
  978. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  979. if (!E) {
  980. return; //scene not editable
  981. }
  982. for (Node *F : E->value) {
  983. Node *n = F;
  984. if (base && !base->is_ancestor_of(n)) {
  985. continue;
  986. }
  987. if (!n->has_node(p_at)) {
  988. continue;
  989. }
  990. Node *nfrom = n->get_node(p_at);
  991. if (!n->has_node(p_new_place)) {
  992. continue;
  993. }
  994. Node *nto = n->get_node(p_new_place);
  995. nfrom->get_parent()->remove_child(nfrom);
  996. nfrom->set_name(p_new_name);
  997. nto->add_child(nfrom);
  998. if (p_at_pos >= 0) {
  999. nto->move_child(nfrom, p_at_pos);
  1000. }
  1001. }
  1002. }
  1003. /// RuntimeNodeSelect
  1004. RuntimeNodeSelect *RuntimeNodeSelect::get_singleton() {
  1005. return singleton;
  1006. }
  1007. RuntimeNodeSelect::~RuntimeNodeSelect() {
  1008. if (selection_list && !selection_list->is_visible()) {
  1009. memdelete(selection_list);
  1010. }
  1011. if (sbox_2d_canvas.is_valid()) {
  1012. RS::get_singleton()->free(sbox_2d_canvas);
  1013. RS::get_singleton()->free(sbox_2d_ci);
  1014. }
  1015. #ifndef _3D_DISABLED
  1016. if (sbox_3d_instance.is_valid()) {
  1017. RS::get_singleton()->free(sbox_3d_instance);
  1018. RS::get_singleton()->free(sbox_3d_instance_ofs);
  1019. RS::get_singleton()->free(sbox_3d_instance_xray);
  1020. RS::get_singleton()->free(sbox_3d_instance_xray_ofs);
  1021. }
  1022. #endif // _3D_DISABLED
  1023. }
  1024. void RuntimeNodeSelect::_setup(const Dictionary &p_settings) {
  1025. Window *root = SceneTree::get_singleton()->get_root();
  1026. ERR_FAIL_COND(root->is_connected(SceneStringName(window_input), callable_mp(this, &RuntimeNodeSelect::_root_window_input)));
  1027. root->connect(SceneStringName(window_input), callable_mp(this, &RuntimeNodeSelect::_root_window_input));
  1028. root->connect("size_changed", callable_mp(this, &RuntimeNodeSelect::_queue_selection_update), CONNECT_DEFERRED);
  1029. selection_list = memnew(PopupMenu);
  1030. selection_list->set_theme(ThemeDB::get_singleton()->get_default_theme());
  1031. selection_list->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED);
  1032. selection_list->set_force_native(true);
  1033. selection_list->connect("index_pressed", callable_mp(this, &RuntimeNodeSelect::_items_popup_index_pressed).bind(selection_list));
  1034. selection_list->connect("popup_hide", callable_mp(Object::cast_to<Node>(root), &Node::remove_child).bind(selection_list));
  1035. panner.instantiate();
  1036. panner->set_callbacks(callable_mp(this, &RuntimeNodeSelect::_pan_callback), callable_mp(this, &RuntimeNodeSelect::_zoom_callback));
  1037. ViewPanner::ControlScheme panning_scheme = (ViewPanner::ControlScheme)p_settings.get("editors/panning/2d_editor_panning_scheme", 0).operator int();
  1038. bool simple_panning = p_settings.get("editors/panning/simple_panning", false);
  1039. int pan_speed = p_settings.get("editors/panning/2d_editor_pan_speed", 20);
  1040. Array keys = p_settings.get("canvas_item_editor/pan_view", Array()).operator Array();
  1041. panner->setup(panning_scheme, DebuggerMarshalls::deserialize_key_shortcut(keys), simple_panning);
  1042. panner->set_scroll_speed(pan_speed);
  1043. warped_panning = p_settings.get("editors/panning/warped_mouse_panning", false);
  1044. /// 2D Selection Box Generation
  1045. sbox_2d_canvas = RS::get_singleton()->canvas_create();
  1046. sbox_2d_ci = RS::get_singleton()->canvas_item_create();
  1047. RS::get_singleton()->viewport_attach_canvas(root->get_viewport_rid(), sbox_2d_canvas);
  1048. RS::get_singleton()->canvas_item_set_parent(sbox_2d_ci, sbox_2d_canvas);
  1049. #ifndef _3D_DISABLED
  1050. cursor = Cursor();
  1051. /// 3D Selection Box Generation
  1052. // Copied from the Node3DEditor implementation.
  1053. // Use two AABBs to create the illusion of a slightly thicker line.
  1054. AABB aabb(Vector3(), Vector3(1, 1, 1));
  1055. // Create a x-ray (visible through solid surfaces) and standard version of the selection box.
  1056. // Both will be drawn at the same position, but with different opacity.
  1057. // This lets the user see where the selection is while still having a sense of depth.
  1058. Ref<SurfaceTool> st = memnew(SurfaceTool);
  1059. Ref<SurfaceTool> st_xray = memnew(SurfaceTool);
  1060. st->begin(Mesh::PRIMITIVE_LINES);
  1061. st_xray->begin(Mesh::PRIMITIVE_LINES);
  1062. for (int i = 0; i < 12; i++) {
  1063. Vector3 a, b;
  1064. aabb.get_edge(i, a, b);
  1065. st->add_vertex(a);
  1066. st->add_vertex(b);
  1067. st_xray->add_vertex(a);
  1068. st_xray->add_vertex(b);
  1069. }
  1070. Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
  1071. mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1072. mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1073. // In the original Node3DEditor, this value would be fetched from the "editors/3d/selection_box_color" editor property,
  1074. // but since this is not accessible from here, we will just use the default value.
  1075. const Color selection_color_3d = Color(1, 0.5, 0);
  1076. mat->set_albedo(selection_color_3d);
  1077. mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1078. st->set_material(mat);
  1079. sbox_3d_mesh = st->commit();
  1080. Ref<StandardMaterial3D> mat_xray = memnew(StandardMaterial3D);
  1081. mat_xray->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1082. mat_xray->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1083. mat_xray->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  1084. mat_xray->set_albedo(selection_color_3d * Color(1, 1, 1, 0.15));
  1085. mat_xray->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1086. st_xray->set_material(mat_xray);
  1087. sbox_3d_mesh_xray = st_xray->commit();
  1088. #endif // _3D_DISABLED
  1089. SceneTree::get_singleton()->connect("process_frame", callable_mp(this, &RuntimeNodeSelect::_process_frame));
  1090. SceneTree::get_singleton()->connect("physics_frame", callable_mp(this, &RuntimeNodeSelect::_physics_frame));
  1091. // This function will be called before the root enters the tree at first when the Game view is passing its settings to
  1092. // the debugger, so queue the update for after it enters.
  1093. root->connect(SceneStringName(tree_entered), callable_mp(this, &RuntimeNodeSelect::_update_input_state), Object::CONNECT_ONE_SHOT);
  1094. }
  1095. void RuntimeNodeSelect::_node_set_type(NodeType p_type) {
  1096. node_select_type = p_type;
  1097. _update_input_state();
  1098. }
  1099. void RuntimeNodeSelect::_select_set_mode(SelectMode p_mode) {
  1100. node_select_mode = p_mode;
  1101. }
  1102. void RuntimeNodeSelect::_set_camera_override_enabled(bool p_enabled) {
  1103. camera_override = p_enabled;
  1104. if (p_enabled) {
  1105. _update_view_2d();
  1106. }
  1107. #ifndef _3D_DISABLED
  1108. if (camera_first_override) {
  1109. _reset_camera_2d();
  1110. _reset_camera_3d();
  1111. camera_first_override = false;
  1112. } else if (p_enabled) {
  1113. _update_view_2d();
  1114. SceneTree::get_singleton()->get_root()->set_camera_3d_override_transform(_get_cursor_transform());
  1115. SceneTree::get_singleton()->get_root()->set_camera_3d_override_perspective(CAMERA_BASE_FOV * cursor.fov_scale, CAMERA_ZNEAR, CAMERA_ZFAR);
  1116. }
  1117. #endif // _3D_DISABLED
  1118. }
  1119. void RuntimeNodeSelect::_root_window_input(const Ref<InputEvent> &p_event) {
  1120. Window *root = SceneTree::get_singleton()->get_root();
  1121. if (node_select_type == NODE_TYPE_NONE || selection_list->is_visible()) {
  1122. // Workaround for platforms that don't allow subwindows.
  1123. if (selection_list->is_visible() && selection_list->is_embedded()) {
  1124. root->set_disable_input_override(false);
  1125. selection_list->push_input(p_event);
  1126. callable_mp(root->get_viewport(), &Viewport::set_disable_input_override).call_deferred(true);
  1127. }
  1128. return;
  1129. }
  1130. if (camera_override) {
  1131. if (node_select_type == NODE_TYPE_2D) {
  1132. if (panner->gui_input(p_event, warped_panning ? Rect2(Vector2(), root->get_size()) : Rect2())) {
  1133. return;
  1134. }
  1135. } else if (node_select_type == NODE_TYPE_3D) {
  1136. #ifndef _3D_DISABLED
  1137. if (root->get_camera_3d() && _handle_3d_input(p_event)) {
  1138. return;
  1139. }
  1140. #endif // _3D_DISABLED
  1141. }
  1142. }
  1143. Ref<InputEventMouseButton> b = p_event;
  1144. if (!b.is_valid() || !b->is_pressed()) {
  1145. return;
  1146. }
  1147. list_shortcut_pressed = node_select_mode == SELECT_MODE_SINGLE && b->get_button_index() == MouseButton::RIGHT && b->is_alt_pressed();
  1148. if (list_shortcut_pressed || b->get_button_index() == MouseButton::LEFT) {
  1149. selection_position = b->get_position();
  1150. }
  1151. }
  1152. void RuntimeNodeSelect::_items_popup_index_pressed(int p_index, PopupMenu *p_popup) {
  1153. Object *obj = p_popup->get_item_metadata(p_index).get_validated_object();
  1154. if (!obj) {
  1155. return;
  1156. }
  1157. Array message;
  1158. message.append(obj->get_instance_id());
  1159. EngineDebugger::get_singleton()->send_message("remote_node_clicked", message);
  1160. }
  1161. void RuntimeNodeSelect::_update_input_state() {
  1162. SceneTree *scene_tree = SceneTree::get_singleton();
  1163. // This function can be called at the very beginning, when the root hasn't entered the tree yet.
  1164. // So check first to avoid a crash.
  1165. if (!scene_tree->get_root()->is_inside_tree()) {
  1166. return;
  1167. }
  1168. bool disable_input = scene_tree->is_suspended() || node_select_type != RuntimeNodeSelect::NODE_TYPE_NONE;
  1169. Input::get_singleton()->set_disable_input(disable_input);
  1170. Input::get_singleton()->set_mouse_mode_override_enabled(disable_input);
  1171. scene_tree->get_root()->set_disable_input_override(disable_input);
  1172. }
  1173. void RuntimeNodeSelect::_process_frame() {
  1174. #ifndef _3D_DISABLED
  1175. if (camera_freelook) {
  1176. Transform3D transform = _get_cursor_transform();
  1177. Vector3 forward = transform.basis.xform(Vector3(0, 0, -1));
  1178. const Vector3 right = transform.basis.xform(Vector3(1, 0, 0));
  1179. Vector3 up = transform.basis.xform(Vector3(0, 1, 0));
  1180. Vector3 direction;
  1181. Input *input = Input::get_singleton();
  1182. bool was_input_disabled = input->is_input_disabled();
  1183. if (was_input_disabled) {
  1184. input->set_disable_input(false);
  1185. }
  1186. if (input->is_physical_key_pressed(Key::A)) {
  1187. direction -= right;
  1188. }
  1189. if (input->is_physical_key_pressed(Key::D)) {
  1190. direction += right;
  1191. }
  1192. if (input->is_physical_key_pressed(Key::W)) {
  1193. direction += forward;
  1194. }
  1195. if (input->is_physical_key_pressed(Key::S)) {
  1196. direction -= forward;
  1197. }
  1198. if (input->is_physical_key_pressed(Key::E)) {
  1199. direction += up;
  1200. }
  1201. if (input->is_physical_key_pressed(Key::Q)) {
  1202. direction -= up;
  1203. }
  1204. real_t speed = FREELOOK_BASE_SPEED;
  1205. if (input->is_physical_key_pressed(Key::SHIFT)) {
  1206. speed *= 3.0;
  1207. }
  1208. if (input->is_physical_key_pressed(Key::ALT)) {
  1209. speed *= 0.333333;
  1210. }
  1211. if (was_input_disabled) {
  1212. input->set_disable_input(true);
  1213. }
  1214. if (direction != Vector3()) {
  1215. // Calculate the process time manually, as the time scale is frozen.
  1216. const double process_time = (1.0 / Engine::get_singleton()->get_frames_per_second()) * Engine::get_singleton()->get_unfrozen_time_scale();
  1217. const Vector3 motion = direction * speed * process_time;
  1218. cursor.pos += motion;
  1219. cursor.eye_pos += motion;
  1220. SceneTree::get_singleton()->get_root()->set_camera_3d_override_transform(_get_cursor_transform());
  1221. }
  1222. }
  1223. #endif // _3D_DISABLED
  1224. if (selection_update_queued || !SceneTree::get_singleton()->is_suspended()) {
  1225. selection_update_queued = false;
  1226. if (has_selection) {
  1227. _update_selection();
  1228. }
  1229. }
  1230. }
  1231. void RuntimeNodeSelect::_physics_frame() {
  1232. if (!Math::is_inf(selection_position.x) || !Math::is_inf(selection_position.y)) {
  1233. _click_point();
  1234. selection_position = Point2(INFINITY, INFINITY);
  1235. }
  1236. }
  1237. void RuntimeNodeSelect::_click_point() {
  1238. Window *root = SceneTree::get_singleton()->get_root();
  1239. Point2 pos = root->get_screen_transform().affine_inverse().xform(selection_position);
  1240. Vector<SelectResult> items;
  1241. if (node_select_type == NODE_TYPE_2D) {
  1242. for (int i = 0; i < root->get_child_count(); i++) {
  1243. _find_canvas_items_at_pos(pos, root->get_child(i), items);
  1244. }
  1245. // Remove possible duplicates.
  1246. for (int i = 0; i < items.size(); i++) {
  1247. Node *item = items[i].item;
  1248. for (int j = 0; j < i; j++) {
  1249. if (items[j].item == item) {
  1250. items.remove_at(i);
  1251. i--;
  1252. break;
  1253. }
  1254. }
  1255. }
  1256. } else if (node_select_type == NODE_TYPE_3D) {
  1257. #ifndef _3D_DISABLED
  1258. _find_3d_items_at_pos(pos, items);
  1259. #endif // _3D_DISABLED
  1260. }
  1261. if (items.is_empty()) {
  1262. return;
  1263. }
  1264. items.sort();
  1265. if ((!list_shortcut_pressed && node_select_mode == SELECT_MODE_SINGLE) || items.size() == 1) {
  1266. Array message;
  1267. message.append(items[0].item->get_instance_id());
  1268. EngineDebugger::get_singleton()->send_message("remote_node_clicked", message);
  1269. } else if (list_shortcut_pressed || node_select_mode == SELECT_MODE_LIST) {
  1270. if (!selection_list->is_inside_tree()) {
  1271. root->add_child(selection_list);
  1272. }
  1273. selection_list->clear();
  1274. for (const SelectResult &I : items) {
  1275. selection_list->add_item(I.item->get_name());
  1276. selection_list->set_item_metadata(-1, I.item);
  1277. }
  1278. selection_list->set_position(selection_list->is_embedded() ? pos : selection_position + root->get_position());
  1279. selection_list->reset_size();
  1280. selection_list->popup();
  1281. // FIXME: Ugly hack that stops the popup from hiding when the button is released.
  1282. selection_list->call_deferred(SNAME("set_position"), selection_list->get_position() + Point2(1, 0));
  1283. }
  1284. }
  1285. void RuntimeNodeSelect::_select_node(Node *p_node) {
  1286. if (p_node == selected_node) {
  1287. return;
  1288. }
  1289. _clear_selection();
  1290. CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);
  1291. if (ci) {
  1292. selected_node = p_node;
  1293. } else {
  1294. #ifndef _3D_DISABLED
  1295. Node3D *node_3d = Object::cast_to<Node3D>(p_node);
  1296. if (node_3d) {
  1297. if (!node_3d->is_inside_world()) {
  1298. return;
  1299. }
  1300. selected_node = p_node;
  1301. sbox_3d_instance = RS::get_singleton()->instance_create2(sbox_3d_mesh->get_rid(), node_3d->get_world_3d()->get_scenario());
  1302. sbox_3d_instance_ofs = RS::get_singleton()->instance_create2(sbox_3d_mesh->get_rid(), node_3d->get_world_3d()->get_scenario());
  1303. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(sbox_3d_instance, RS::SHADOW_CASTING_SETTING_OFF);
  1304. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(sbox_3d_instance_ofs, RS::SHADOW_CASTING_SETTING_OFF);
  1305. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  1306. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  1307. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance_ofs, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  1308. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance_ofs, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  1309. sbox_3d_instance_xray = RS::get_singleton()->instance_create2(sbox_3d_mesh_xray->get_rid(), node_3d->get_world_3d()->get_scenario());
  1310. sbox_3d_instance_xray_ofs = RS::get_singleton()->instance_create2(sbox_3d_mesh_xray->get_rid(), node_3d->get_world_3d()->get_scenario());
  1311. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(sbox_3d_instance_xray, RS::SHADOW_CASTING_SETTING_OFF);
  1312. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(sbox_3d_instance_xray_ofs, RS::SHADOW_CASTING_SETTING_OFF);
  1313. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance_xray, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  1314. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance_xray, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  1315. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance_xray_ofs, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  1316. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance_xray_ofs, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  1317. }
  1318. #endif // _3D_DISABLED
  1319. }
  1320. has_selection = selected_node;
  1321. _queue_selection_update();
  1322. }
  1323. void RuntimeNodeSelect::_queue_selection_update() {
  1324. if (has_selection && selection_visible) {
  1325. if (SceneTree::get_singleton()->is_suspended()) {
  1326. _update_selection();
  1327. } else {
  1328. selection_update_queued = true;
  1329. }
  1330. }
  1331. }
  1332. void RuntimeNodeSelect::_update_selection() {
  1333. if (has_selection && (!selected_node || !selected_node->is_inside_tree())) {
  1334. _clear_selection();
  1335. return;
  1336. }
  1337. CanvasItem *ci = Object::cast_to<CanvasItem>(selected_node);
  1338. if (ci) {
  1339. Window *root = SceneTree::get_singleton()->get_root();
  1340. Transform2D xform;
  1341. if (root->is_canvas_transform_override_enabled() && !ci->get_canvas_layer_node()) {
  1342. RS::get_singleton()->canvas_item_set_transform(sbox_2d_ci, (root->get_canvas_transform_override()));
  1343. xform = ci->get_global_transform();
  1344. } else {
  1345. RS::get_singleton()->canvas_item_set_transform(sbox_2d_ci, Transform2D());
  1346. xform = ci->get_global_transform_with_canvas();
  1347. }
  1348. // Fallback.
  1349. Rect2 rect = Rect2(Vector2(), Vector2(10, 10));
  1350. if (ci->_edit_use_rect()) {
  1351. rect = ci->_edit_get_rect();
  1352. } else {
  1353. CollisionShape2D *collision_shape = Object::cast_to<CollisionShape2D>(ci);
  1354. if (collision_shape) {
  1355. Ref<Shape2D> shape = collision_shape->get_shape();
  1356. if (shape.is_valid()) {
  1357. rect = shape->get_rect();
  1358. }
  1359. }
  1360. }
  1361. RS::get_singleton()->canvas_item_set_visible(sbox_2d_ci, selection_visible);
  1362. if (xform == sbox_2d_xform && rect == sbox_2d_rect) {
  1363. return; // Nothing changed.
  1364. }
  1365. sbox_2d_xform = xform;
  1366. sbox_2d_rect = rect;
  1367. RS::get_singleton()->canvas_item_clear(sbox_2d_ci);
  1368. const Vector2 endpoints[4] = {
  1369. xform.xform(rect.position),
  1370. xform.xform(rect.position + Vector2(rect.size.x, 0)),
  1371. xform.xform(rect.position + rect.size),
  1372. xform.xform(rect.position + Vector2(0, rect.size.y))
  1373. };
  1374. const Color selection_color_2d = Color(1, 0.6, 0.4, 0.7);
  1375. for (int i = 0; i < 4; i++) {
  1376. RS::get_singleton()->canvas_item_add_line(sbox_2d_ci, endpoints[i], endpoints[(i + 1) % 4], selection_color_2d, Math::round(2.f));
  1377. }
  1378. } else {
  1379. #ifndef _3D_DISABLED
  1380. Node3D *node_3d = Object::cast_to<Node3D>(selected_node);
  1381. // Fallback.
  1382. AABB bounds(Vector3(-0.5, -0.5, -0.5), Vector3(1, 1, 1));
  1383. VisualInstance3D *visual_instance = Object::cast_to<VisualInstance3D>(node_3d);
  1384. if (visual_instance) {
  1385. bounds = visual_instance->get_aabb();
  1386. } else {
  1387. CollisionShape3D *collision_shape = Object::cast_to<CollisionShape3D>(node_3d);
  1388. if (collision_shape) {
  1389. Ref<Shape3D> shape = collision_shape->get_shape();
  1390. if (shape.is_valid()) {
  1391. bounds = shape->get_debug_mesh()->get_aabb();
  1392. }
  1393. }
  1394. }
  1395. RS::get_singleton()->instance_set_visible(sbox_3d_instance, selection_visible);
  1396. RS::get_singleton()->instance_set_visible(sbox_3d_instance_ofs, selection_visible);
  1397. RS::get_singleton()->instance_set_visible(sbox_3d_instance_xray, selection_visible);
  1398. RS::get_singleton()->instance_set_visible(sbox_3d_instance_xray_ofs, selection_visible);
  1399. Transform3D xform_to_top_level_parent_space = node_3d->get_global_transform().affine_inverse() * node_3d->get_global_transform();
  1400. bounds = xform_to_top_level_parent_space.xform(bounds);
  1401. Transform3D t = node_3d->get_global_transform();
  1402. if (t == sbox_3d_xform && bounds == sbox_3d_bounds) {
  1403. return; // Nothing changed.
  1404. }
  1405. sbox_3d_xform = t;
  1406. sbox_3d_bounds = bounds;
  1407. Transform3D t_offset = t;
  1408. // Apply AABB scaling before item's global transform.
  1409. {
  1410. const Vector3 offset(0.005, 0.005, 0.005);
  1411. Basis aabb_s;
  1412. aabb_s.scale(bounds.size + offset);
  1413. t.translate_local(bounds.position - offset / 2);
  1414. t.basis = t.basis * aabb_s;
  1415. }
  1416. {
  1417. const Vector3 offset(0.01, 0.01, 0.01);
  1418. Basis aabb_s;
  1419. aabb_s.scale(bounds.size + offset);
  1420. t_offset.translate_local(bounds.position - offset / 2);
  1421. t_offset.basis = t_offset.basis * aabb_s;
  1422. }
  1423. RS::get_singleton()->instance_set_transform(sbox_3d_instance, t);
  1424. RS::get_singleton()->instance_set_transform(sbox_3d_instance_ofs, t_offset);
  1425. RS::get_singleton()->instance_set_transform(sbox_3d_instance_xray, t);
  1426. RS::get_singleton()->instance_set_transform(sbox_3d_instance_xray_ofs, t_offset);
  1427. #endif // _3D_DISABLED
  1428. }
  1429. }
  1430. void RuntimeNodeSelect::_clear_selection() {
  1431. selected_node = nullptr;
  1432. has_selection = false;
  1433. if (sbox_2d_canvas.is_valid()) {
  1434. RS::get_singleton()->canvas_item_clear(sbox_2d_ci);
  1435. }
  1436. #ifndef _3D_DISABLED
  1437. if (sbox_3d_instance.is_valid()) {
  1438. RS::get_singleton()->free(sbox_3d_instance);
  1439. RS::get_singleton()->free(sbox_3d_instance_ofs);
  1440. RS::get_singleton()->free(sbox_3d_instance_xray);
  1441. RS::get_singleton()->free(sbox_3d_instance_xray_ofs);
  1442. }
  1443. #endif // _3D_DISABLED
  1444. }
  1445. void RuntimeNodeSelect::_set_selection_visible(bool p_visible) {
  1446. selection_visible = p_visible;
  1447. if (has_selection) {
  1448. _update_selection();
  1449. }
  1450. }
  1451. // Copied and trimmed from the CanvasItemEditor implementation.
  1452. void RuntimeNodeSelect::_find_canvas_items_at_pos(const Point2 &p_pos, Node *p_node, Vector<SelectResult> &r_items, const Transform2D &p_parent_xform, const Transform2D &p_canvas_xform) {
  1453. if (!p_node || Object::cast_to<Viewport>(p_node)) {
  1454. return;
  1455. }
  1456. // In the original CanvasItemEditor, this value would be fetched from the "editors/polygon_editor/point_grab_radius" editor property,
  1457. // but since this is not accessible from here, we will just use the default value.
  1458. const real_t grab_distance = 8;
  1459. CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);
  1460. for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
  1461. if (ci) {
  1462. if (!ci->is_set_as_top_level()) {
  1463. _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, p_parent_xform * ci->get_transform(), p_canvas_xform);
  1464. } else {
  1465. _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, ci->get_transform(), p_canvas_xform);
  1466. }
  1467. } else {
  1468. CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node);
  1469. _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, Transform2D(), cl ? cl->get_transform() : p_canvas_xform);
  1470. }
  1471. }
  1472. if (ci && ci->is_visible_in_tree()) {
  1473. Transform2D xform = p_canvas_xform;
  1474. if (!ci->is_set_as_top_level()) {
  1475. xform *= p_parent_xform;
  1476. }
  1477. Vector2 pos;
  1478. // Cameras (overridden or not) don't affect `CanvasLayer`s.
  1479. if (!ci->get_canvas_layer_node()) {
  1480. Window *root = SceneTree::get_singleton()->get_root();
  1481. pos = (root->is_canvas_transform_override_enabled() ? root->get_canvas_transform_override() : root->get_canvas_transform()).affine_inverse().xform(p_pos);
  1482. } else {
  1483. pos = p_pos;
  1484. }
  1485. xform = (xform * ci->get_transform()).affine_inverse();
  1486. const real_t local_grab_distance = xform.basis_xform(Vector2(grab_distance, 0)).length() / view_2d_zoom;
  1487. if (ci->_edit_is_selected_on_click(xform.xform(pos), local_grab_distance)) {
  1488. SelectResult res;
  1489. res.item = ci;
  1490. res.order = ci->get_effective_z_index() + ci->get_canvas_layer();
  1491. r_items.push_back(res);
  1492. // If it's a shape, get the collision object it's from.
  1493. // FIXME: If the collision object has multiple shapes, only the topmost will be above it in the list.
  1494. if (Object::cast_to<CollisionShape2D>(ci) || Object::cast_to<CollisionPolygon2D>(ci)) {
  1495. CollisionObject2D *collision_object = Object::cast_to<CollisionObject2D>(ci->get_parent());
  1496. if (collision_object) {
  1497. SelectResult res_col;
  1498. res_col.item = ci->get_parent();
  1499. res_col.order = collision_object->get_z_index() + ci->get_canvas_layer();
  1500. r_items.push_back(res_col);
  1501. }
  1502. }
  1503. }
  1504. }
  1505. }
  1506. void RuntimeNodeSelect::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
  1507. view_2d_offset.x -= p_scroll_vec.x / view_2d_zoom;
  1508. view_2d_offset.y -= p_scroll_vec.y / view_2d_zoom;
  1509. _update_view_2d();
  1510. }
  1511. // A very shallow copy of the same function inside CanvasItemEditor.
  1512. void RuntimeNodeSelect::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
  1513. real_t prev_zoom = view_2d_zoom;
  1514. view_2d_zoom = CLAMP(view_2d_zoom * p_zoom_factor, VIEW_2D_MIN_ZOOM, VIEW_2D_MAX_ZOOM);
  1515. Vector2 pos = SceneTree::get_singleton()->get_root()->get_screen_transform().affine_inverse().xform(p_origin);
  1516. view_2d_offset += pos / prev_zoom - pos / view_2d_zoom;
  1517. // We want to align in-scene pixels to screen pixels, this prevents blurry rendering
  1518. // of small details (texts, lines).
  1519. // This correction adds a jitter movement when zooming, so we correct only when the
  1520. // zoom factor is an integer. (in the other cases, all pixels won't be aligned anyway)
  1521. const real_t closest_zoom_factor = Math::round(view_2d_zoom);
  1522. if (Math::is_zero_approx(view_2d_zoom - closest_zoom_factor)) {
  1523. // Make sure scene pixel at view_offset is aligned on a screen pixel.
  1524. Vector2 view_offset_int = view_2d_offset.floor();
  1525. Vector2 view_offset_frac = view_2d_offset - view_offset_int;
  1526. view_2d_offset = view_offset_int + (view_offset_frac * closest_zoom_factor).round() / closest_zoom_factor;
  1527. }
  1528. _update_view_2d();
  1529. }
  1530. void RuntimeNodeSelect::_reset_camera_2d() {
  1531. view_2d_offset = -SceneTree::get_singleton()->get_root()->get_canvas_transform().get_origin();
  1532. view_2d_zoom = 1;
  1533. _update_view_2d();
  1534. }
  1535. void RuntimeNodeSelect::_update_view_2d() {
  1536. Transform2D transform = Transform2D();
  1537. transform.scale_basis(Size2(view_2d_zoom, view_2d_zoom));
  1538. transform.columns[2] = -view_2d_offset * view_2d_zoom;
  1539. SceneTree::get_singleton()->get_root()->set_canvas_transform_override(transform);
  1540. _queue_selection_update();
  1541. }
  1542. #ifndef _3D_DISABLED
  1543. void RuntimeNodeSelect::_find_3d_items_at_pos(const Point2 &p_pos, Vector<SelectResult> &r_items) {
  1544. Window *root = SceneTree::get_singleton()->get_root();
  1545. Camera3D *camera = root->get_viewport()->get_camera_3d();
  1546. if (!camera) {
  1547. return;
  1548. }
  1549. Vector3 ray, pos, to;
  1550. if (root->get_viewport()->is_camera_3d_override_enabled()) {
  1551. Viewport *vp = root->get_viewport();
  1552. ray = vp->camera_3d_override_project_ray_normal(p_pos);
  1553. pos = vp->camera_3d_override_project_ray_origin(p_pos);
  1554. to = pos + ray * vp->get_camera_3d_override_properties()["z_far"];
  1555. } else {
  1556. ray = camera->project_ray_normal(p_pos);
  1557. pos = camera->project_ray_origin(p_pos);
  1558. to = pos + ray * camera->get_far();
  1559. }
  1560. // Start with physical objects.
  1561. PhysicsDirectSpaceState3D *ss = root->get_world_3d()->get_direct_space_state();
  1562. PhysicsDirectSpaceState3D::RayResult result;
  1563. HashSet<RID> excluded;
  1564. PhysicsDirectSpaceState3D::RayParameters ray_params;
  1565. ray_params.from = pos;
  1566. ray_params.to = to;
  1567. ray_params.collide_with_areas = true;
  1568. while (true) {
  1569. ray_params.exclude = excluded;
  1570. if (ss->intersect_ray(ray_params, result)) {
  1571. SelectResult res;
  1572. res.item = Object::cast_to<Node>(result.collider);
  1573. res.order = -pos.distance_to(Object::cast_to<Node3D>(res.item)->get_global_transform().xform(result.position));
  1574. // Fetch collision shapes.
  1575. CollisionObject3D *collision = Object::cast_to<CollisionObject3D>(result.collider);
  1576. if (collision) {
  1577. List<uint32_t> owners;
  1578. collision->get_shape_owners(&owners);
  1579. for (const uint32_t &I : owners) {
  1580. SelectResult res_shape;
  1581. res_shape.item = Object::cast_to<Node>(collision->shape_owner_get_owner(I));
  1582. res_shape.order = res.order;
  1583. r_items.push_back(res_shape);
  1584. }
  1585. }
  1586. r_items.push_back(res);
  1587. excluded.insert(result.rid);
  1588. } else {
  1589. break;
  1590. }
  1591. }
  1592. // Then go for the meshes.
  1593. Vector<ObjectID> items = RS::get_singleton()->instances_cull_ray(pos, to, root->get_world_3d()->get_scenario());
  1594. for (int i = 0; i < items.size(); i++) {
  1595. Object *obj = ObjectDB::get_instance(items[i]);
  1596. GeometryInstance3D *geo_instance = nullptr;
  1597. Ref<TriangleMesh> mesh_collision;
  1598. MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(obj);
  1599. if (mesh_instance) {
  1600. if (mesh_instance->get_mesh().is_valid()) {
  1601. geo_instance = mesh_instance;
  1602. mesh_collision = mesh_instance->get_mesh()->generate_triangle_mesh();
  1603. }
  1604. } else {
  1605. Label3D *label = Object::cast_to<Label3D>(obj);
  1606. if (label) {
  1607. geo_instance = label;
  1608. mesh_collision = label->generate_triangle_mesh();
  1609. } else {
  1610. Sprite3D *sprite = Object::cast_to<Sprite3D>(obj);
  1611. if (sprite) {
  1612. geo_instance = sprite;
  1613. mesh_collision = sprite->generate_triangle_mesh();
  1614. }
  1615. }
  1616. }
  1617. if (mesh_collision.is_valid()) {
  1618. Transform3D gt = geo_instance->get_global_transform();
  1619. Transform3D ai = gt.affine_inverse();
  1620. Vector3 point, normal;
  1621. if (mesh_collision->intersect_ray(ai.xform(pos), ai.basis.xform(ray).normalized(), point, normal)) {
  1622. SelectResult res;
  1623. res.item = Object::cast_to<Node>(obj);
  1624. res.order = -pos.distance_to(gt.xform(point));
  1625. r_items.push_back(res);
  1626. continue;
  1627. }
  1628. }
  1629. items.remove_at(i);
  1630. i--;
  1631. }
  1632. }
  1633. bool RuntimeNodeSelect::_handle_3d_input(const Ref<InputEvent> &p_event) {
  1634. Ref<InputEventMouseButton> b = p_event;
  1635. if (b.is_valid()) {
  1636. const real_t zoom_factor = 1.08 * b->get_factor();
  1637. switch (b->get_button_index()) {
  1638. case MouseButton::WHEEL_UP: {
  1639. if (!camera_freelook) {
  1640. _cursor_scale_distance(1.0 / zoom_factor);
  1641. }
  1642. return true;
  1643. } break;
  1644. case MouseButton::WHEEL_DOWN: {
  1645. if (!camera_freelook) {
  1646. _cursor_scale_distance(zoom_factor);
  1647. }
  1648. return true;
  1649. } break;
  1650. case MouseButton::RIGHT: {
  1651. _set_camera_freelook_enabled(b->is_pressed());
  1652. return true;
  1653. } break;
  1654. default: {
  1655. }
  1656. }
  1657. }
  1658. Ref<InputEventMouseMotion> m = p_event;
  1659. if (m.is_valid()) {
  1660. if (camera_freelook) {
  1661. _cursor_look(m);
  1662. } else if (m->get_button_mask().has_flag(MouseButtonMask::MIDDLE)) {
  1663. if (m->is_shift_pressed()) {
  1664. _cursor_pan(m);
  1665. } else {
  1666. _cursor_orbit(m);
  1667. }
  1668. }
  1669. return true;
  1670. }
  1671. Ref<InputEventKey> k = p_event;
  1672. if (k.is_valid()) {
  1673. if (k->get_physical_keycode() == Key::ESCAPE) {
  1674. _set_camera_freelook_enabled(false);
  1675. return true;
  1676. } else if (k->is_ctrl_pressed()) {
  1677. switch (k->get_physical_keycode()) {
  1678. case Key::EQUAL: {
  1679. cursor.fov_scale = CLAMP(cursor.fov_scale - 0.05, CAMERA_MIN_FOV_SCALE, CAMERA_MAX_FOV_SCALE);
  1680. SceneTree::get_singleton()->get_root()->set_camera_3d_override_perspective(CAMERA_BASE_FOV * cursor.fov_scale, CAMERA_ZNEAR, CAMERA_ZFAR);
  1681. return true;
  1682. } break;
  1683. case Key::MINUS: {
  1684. cursor.fov_scale = CLAMP(cursor.fov_scale + 0.05, CAMERA_MIN_FOV_SCALE, CAMERA_MAX_FOV_SCALE);
  1685. SceneTree::get_singleton()->get_root()->set_camera_3d_override_perspective(CAMERA_BASE_FOV * cursor.fov_scale, CAMERA_ZNEAR, CAMERA_ZFAR);
  1686. return true;
  1687. } break;
  1688. case Key::KEY_0: {
  1689. cursor.fov_scale = 1;
  1690. SceneTree::get_singleton()->get_root()->set_camera_3d_override_perspective(CAMERA_BASE_FOV, CAMERA_ZNEAR, CAMERA_ZFAR);
  1691. return true;
  1692. } break;
  1693. default: {
  1694. }
  1695. }
  1696. }
  1697. }
  1698. // TODO: Handle magnify and pan input gestures.
  1699. return false;
  1700. }
  1701. void RuntimeNodeSelect::_set_camera_freelook_enabled(bool p_enabled) {
  1702. camera_freelook = p_enabled;
  1703. if (p_enabled) {
  1704. // Make sure eye_pos is synced, because freelook referential is eye pos rather than orbit pos
  1705. Vector3 forward = _get_cursor_transform().basis.xform(Vector3(0, 0, -1));
  1706. cursor.eye_pos = cursor.pos - cursor.distance * forward;
  1707. previous_mouse_position = SceneTree::get_singleton()->get_root()->get_mouse_position();
  1708. // Hide mouse like in an FPS (warping doesn't work).
  1709. Input::get_singleton()->set_mouse_mode_override(Input::MOUSE_MODE_CAPTURED);
  1710. } else {
  1711. // Restore mouse.
  1712. Input::get_singleton()->set_mouse_mode_override(Input::MOUSE_MODE_VISIBLE);
  1713. // Restore the previous mouse position when leaving freelook mode.
  1714. // This is done because leaving `Input.MOUSE_MODE_CAPTURED` will center the cursor
  1715. // due to OS limitations.
  1716. Input::get_singleton()->warp_mouse(previous_mouse_position);
  1717. }
  1718. }
  1719. void RuntimeNodeSelect::_cursor_scale_distance(real_t p_scale) {
  1720. real_t min_distance = MAX(CAMERA_ZNEAR * 4, VIEW_3D_MIN_ZOOM);
  1721. real_t max_distance = MIN(CAMERA_ZFAR / 4, VIEW_3D_MAX_ZOOM);
  1722. cursor.distance = CLAMP(cursor.distance * p_scale, min_distance, max_distance);
  1723. SceneTree::get_singleton()->get_root()->set_camera_3d_override_transform(_get_cursor_transform());
  1724. }
  1725. void RuntimeNodeSelect::_cursor_look(Ref<InputEventWithModifiers> p_event) {
  1726. Window *root = SceneTree::get_singleton()->get_root();
  1727. const Vector2 relative = Input::get_singleton()->warp_mouse_motion(p_event, Rect2(Vector2(), root->get_size()));
  1728. const Transform3D prev_camera_transform = _get_cursor_transform();
  1729. cursor.x_rot += relative.y * RADS_PER_PIXEL;
  1730. // Clamp the Y rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented.
  1731. cursor.x_rot = CLAMP(cursor.x_rot, -1.57, 1.57);
  1732. cursor.y_rot += relative.x * RADS_PER_PIXEL;
  1733. // Look is like the opposite of Orbit: the focus point rotates around the camera.
  1734. Transform3D camera_transform = _get_cursor_transform();
  1735. Vector3 pos = camera_transform.xform(Vector3(0, 0, 0));
  1736. Vector3 prev_pos = prev_camera_transform.xform(Vector3(0, 0, 0));
  1737. Vector3 diff = prev_pos - pos;
  1738. cursor.pos += diff;
  1739. SceneTree::get_singleton()->get_root()->set_camera_3d_override_transform(_get_cursor_transform());
  1740. }
  1741. void RuntimeNodeSelect::_cursor_pan(Ref<InputEventWithModifiers> p_event) {
  1742. Window *root = SceneTree::get_singleton()->get_root();
  1743. // Reduce all sides of the area by 1, so warping works when windows are maximized/fullscreen.
  1744. const Vector2 relative = Input::get_singleton()->warp_mouse_motion(p_event, Rect2(Vector2(1, 1), root->get_size() - Vector2(2, 2)));
  1745. const real_t pan_speed = 1 / 150.0;
  1746. Transform3D camera_transform;
  1747. camera_transform.translate_local(cursor.pos);
  1748. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  1749. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  1750. Vector3 translation(1 * -relative.x * pan_speed, relative.y * pan_speed, 0);
  1751. translation *= cursor.distance / 4;
  1752. camera_transform.translate_local(translation);
  1753. cursor.pos = camera_transform.origin;
  1754. SceneTree::get_singleton()->get_root()->set_camera_3d_override_transform(_get_cursor_transform());
  1755. }
  1756. void RuntimeNodeSelect::_cursor_orbit(Ref<InputEventWithModifiers> p_event) {
  1757. Window *root = SceneTree::get_singleton()->get_root();
  1758. // Reduce all sides of the area by 1, so warping works when windows are maximized/fullscreen.
  1759. const Vector2 relative = Input::get_singleton()->warp_mouse_motion(p_event, Rect2(Vector2(1, 1), root->get_size() - Vector2(2, 2)));
  1760. cursor.x_rot += relative.y * RADS_PER_PIXEL;
  1761. // Clamp the Y rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented.
  1762. cursor.x_rot = CLAMP(cursor.x_rot, -1.57, 1.57);
  1763. cursor.y_rot += relative.x * RADS_PER_PIXEL;
  1764. SceneTree::get_singleton()->get_root()->set_camera_3d_override_transform(_get_cursor_transform());
  1765. }
  1766. Transform3D RuntimeNodeSelect::_get_cursor_transform() {
  1767. Transform3D camera_transform;
  1768. camera_transform.translate_local(cursor.pos);
  1769. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  1770. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  1771. camera_transform.translate_local(0, 0, cursor.distance);
  1772. return camera_transform;
  1773. }
  1774. void RuntimeNodeSelect::_reset_camera_3d() {
  1775. camera_first_override = true;
  1776. Window *root = SceneTree::get_singleton()->get_root();
  1777. Camera3D *camera = root->get_camera_3d();
  1778. if (!camera) {
  1779. return;
  1780. }
  1781. cursor = Cursor();
  1782. Transform3D transform = camera->get_global_transform();
  1783. transform.translate_local(0, 0, -cursor.distance);
  1784. cursor.pos = transform.origin;
  1785. cursor.x_rot = -camera->get_global_rotation().x;
  1786. cursor.y_rot = -camera->get_global_rotation().y;
  1787. cursor.fov_scale = CLAMP(camera->get_fov() / CAMERA_BASE_FOV, CAMERA_MIN_FOV_SCALE, CAMERA_MAX_FOV_SCALE);
  1788. SceneTree::get_singleton()->get_root()->set_camera_3d_override_transform(_get_cursor_transform());
  1789. SceneTree::get_singleton()->get_root()->set_camera_3d_override_perspective(CAMERA_BASE_FOV * cursor.fov_scale, CAMERA_ZNEAR, CAMERA_ZFAR);
  1790. }
  1791. #endif // _3D_DISABLED
  1792. #endif