scene_debugger.cpp 68 KB

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