editor_properties.cpp 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025
  1. /*************************************************************************/
  2. /* editor_properties.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "editor_properties.h"
  31. #include "editor/editor_resource_preview.h"
  32. #include "editor/filesystem_dock.h"
  33. #include "editor_node.h"
  34. #include "editor_properties_array_dict.h"
  35. #include "editor_scale.h"
  36. #include "scene/main/viewport.h"
  37. ///////////////////// NULL /////////////////////////
  38. void EditorPropertyNil::update_property() {
  39. }
  40. EditorPropertyNil::EditorPropertyNil() {
  41. Label *label = memnew(Label);
  42. label->set_text("[null]");
  43. add_child(label);
  44. }
  45. ///////////////////// TEXT /////////////////////////
  46. void EditorPropertyText::_text_entered(const String &p_string) {
  47. if (updating) {
  48. return;
  49. }
  50. if (text->has_focus()) {
  51. text->release_focus();
  52. _text_changed(p_string);
  53. }
  54. }
  55. void EditorPropertyText::_text_changed(const String &p_string) {
  56. if (updating) {
  57. return;
  58. }
  59. emit_changed(get_edited_property(), p_string, "", true);
  60. }
  61. void EditorPropertyText::update_property() {
  62. String s = get_edited_object()->get(get_edited_property());
  63. updating = true;
  64. if (text->get_text() != s) {
  65. text->set_text(s);
  66. }
  67. text->set_editable(!is_read_only());
  68. updating = false;
  69. }
  70. void EditorPropertyText::set_placeholder(const String &p_string) {
  71. text->set_placeholder(p_string);
  72. }
  73. void EditorPropertyText::_bind_methods() {
  74. ClassDB::bind_method(D_METHOD("_text_changed", "txt"), &EditorPropertyText::_text_changed);
  75. ClassDB::bind_method(D_METHOD("_text_entered", "txt"), &EditorPropertyText::_text_entered);
  76. }
  77. EditorPropertyText::EditorPropertyText() {
  78. text = memnew(LineEdit);
  79. add_child(text);
  80. add_focusable(text);
  81. text->connect("text_changed", this, "_text_changed");
  82. text->connect("text_entered", this, "_text_entered");
  83. updating = false;
  84. }
  85. ///////////////////// MULTILINE TEXT /////////////////////////
  86. void EditorPropertyMultilineText::_big_text_changed() {
  87. text->set_text(big_text->get_text());
  88. emit_changed(get_edited_property(), big_text->get_text(), "", true);
  89. }
  90. void EditorPropertyMultilineText::_text_changed() {
  91. emit_changed(get_edited_property(), text->get_text(), "", true);
  92. }
  93. void EditorPropertyMultilineText::_open_big_text() {
  94. if (!big_text_dialog) {
  95. big_text = memnew(TextEdit);
  96. big_text->connect("text_changed", this, "_big_text_changed");
  97. big_text->set_wrap_enabled(true);
  98. big_text_dialog = memnew(AcceptDialog);
  99. big_text_dialog->add_child(big_text);
  100. big_text_dialog->set_title(TTR("Edit Text:"));
  101. add_child(big_text_dialog);
  102. }
  103. big_text_dialog->popup_centered_clamped(Size2(1000, 900) * EDSCALE, 0.8);
  104. big_text->set_text(text->get_text());
  105. big_text->grab_focus();
  106. }
  107. void EditorPropertyMultilineText::update_property() {
  108. String t = get_edited_object()->get(get_edited_property());
  109. if (text->get_text() != t) {
  110. text->set_text(t);
  111. if (big_text && big_text->is_visible_in_tree()) {
  112. big_text->set_text(t);
  113. }
  114. }
  115. }
  116. void EditorPropertyMultilineText::_notification(int p_what) {
  117. switch (p_what) {
  118. case NOTIFICATION_THEME_CHANGED:
  119. case NOTIFICATION_ENTER_TREE: {
  120. Ref<Texture> df = get_icon("DistractionFree", "EditorIcons");
  121. open_big_text->set_icon(df);
  122. Ref<Font> font = get_font("font", "Label");
  123. text->set_custom_minimum_size(Vector2(0, font->get_height() * 6));
  124. } break;
  125. }
  126. }
  127. void EditorPropertyMultilineText::_bind_methods() {
  128. ClassDB::bind_method(D_METHOD("_text_changed"), &EditorPropertyMultilineText::_text_changed);
  129. ClassDB::bind_method(D_METHOD("_big_text_changed"), &EditorPropertyMultilineText::_big_text_changed);
  130. ClassDB::bind_method(D_METHOD("_open_big_text"), &EditorPropertyMultilineText::_open_big_text);
  131. }
  132. EditorPropertyMultilineText::EditorPropertyMultilineText() {
  133. HBoxContainer *hb = memnew(HBoxContainer);
  134. add_child(hb);
  135. set_bottom_editor(hb);
  136. text = memnew(TextEdit);
  137. text->connect("text_changed", this, "_text_changed");
  138. text->set_wrap_enabled(true);
  139. add_focusable(text);
  140. hb->add_child(text);
  141. text->set_h_size_flags(SIZE_EXPAND_FILL);
  142. open_big_text = memnew(ToolButton);
  143. open_big_text->connect("pressed", this, "_open_big_text");
  144. hb->add_child(open_big_text);
  145. big_text_dialog = nullptr;
  146. big_text = nullptr;
  147. }
  148. ///////////////////// TEXT ENUM /////////////////////////
  149. void EditorPropertyTextEnum::_option_selected(int p_which) {
  150. emit_changed(get_edited_property(), options->get_item_text(p_which));
  151. }
  152. void EditorPropertyTextEnum::update_property() {
  153. String which = get_edited_object()->get(get_edited_property());
  154. for (int i = 0; i < options->get_item_count(); i++) {
  155. String t = options->get_item_text(i);
  156. if (t == which) {
  157. options->select(i);
  158. return;
  159. }
  160. }
  161. }
  162. void EditorPropertyTextEnum::setup(const Vector<String> &p_options) {
  163. for (int i = 0; i < p_options.size(); i++) {
  164. options->add_item(p_options[i], i);
  165. }
  166. }
  167. void EditorPropertyTextEnum::_bind_methods() {
  168. ClassDB::bind_method(D_METHOD("_option_selected"), &EditorPropertyTextEnum::_option_selected);
  169. }
  170. EditorPropertyTextEnum::EditorPropertyTextEnum() {
  171. options = memnew(OptionButton);
  172. options->set_clip_text(true);
  173. options->set_flat(true);
  174. add_child(options);
  175. add_focusable(options);
  176. options->connect("item_selected", this, "_option_selected");
  177. }
  178. ///////////////////// PATH /////////////////////////
  179. void EditorPropertyPath::_path_selected(const String &p_path) {
  180. emit_changed(get_edited_property(), p_path);
  181. update_property();
  182. }
  183. void EditorPropertyPath::_path_pressed() {
  184. if (!dialog) {
  185. dialog = memnew(EditorFileDialog);
  186. dialog->connect("file_selected", this, "_path_selected");
  187. dialog->connect("dir_selected", this, "_path_selected");
  188. add_child(dialog);
  189. }
  190. String full_path = get_edited_object()->get(get_edited_property());
  191. dialog->clear_filters();
  192. if (global) {
  193. dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  194. } else {
  195. dialog->set_access(EditorFileDialog::ACCESS_RESOURCES);
  196. }
  197. if (folder) {
  198. dialog->set_mode(EditorFileDialog::MODE_OPEN_DIR);
  199. dialog->set_current_dir(full_path);
  200. } else {
  201. dialog->set_mode(save_mode ? EditorFileDialog::MODE_SAVE_FILE : EditorFileDialog::MODE_OPEN_FILE);
  202. for (int i = 0; i < extensions.size(); i++) {
  203. String e = extensions[i].strip_edges();
  204. if (e != String()) {
  205. dialog->add_filter(extensions[i].strip_edges());
  206. }
  207. }
  208. dialog->set_current_path(full_path);
  209. }
  210. dialog->popup_centered_ratio();
  211. }
  212. void EditorPropertyPath::update_property() {
  213. String full_path = get_edited_object()->get(get_edited_property());
  214. path->set_text(full_path);
  215. path->set_tooltip(full_path);
  216. }
  217. void EditorPropertyPath::setup(const Vector<String> &p_extensions, bool p_folder, bool p_global) {
  218. extensions = p_extensions;
  219. folder = p_folder;
  220. global = p_global;
  221. }
  222. void EditorPropertyPath::set_save_mode() {
  223. save_mode = true;
  224. }
  225. void EditorPropertyPath::_notification(int p_what) {
  226. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  227. path_edit->set_icon(get_icon("Folder", "EditorIcons"));
  228. }
  229. }
  230. void EditorPropertyPath::_path_focus_exited() {
  231. _path_selected(path->get_text());
  232. }
  233. void EditorPropertyPath::_bind_methods() {
  234. ClassDB::bind_method(D_METHOD("_path_pressed"), &EditorPropertyPath::_path_pressed);
  235. ClassDB::bind_method(D_METHOD("_path_selected"), &EditorPropertyPath::_path_selected);
  236. ClassDB::bind_method(D_METHOD("_path_focus_exited"), &EditorPropertyPath::_path_focus_exited);
  237. }
  238. EditorPropertyPath::EditorPropertyPath() {
  239. HBoxContainer *path_hb = memnew(HBoxContainer);
  240. add_child(path_hb);
  241. path = memnew(LineEdit);
  242. path_hb->add_child(path);
  243. path->connect("text_entered", this, "_path_selected");
  244. path->connect("focus_exited", this, "_path_focus_exited");
  245. path->set_h_size_flags(SIZE_EXPAND_FILL);
  246. path_edit = memnew(Button);
  247. path_edit->set_clip_text(true);
  248. path_hb->add_child(path_edit);
  249. add_focusable(path);
  250. dialog = nullptr;
  251. path_edit->connect("pressed", this, "_path_pressed");
  252. folder = false;
  253. global = false;
  254. save_mode = false;
  255. }
  256. ///////////////////// CLASS NAME /////////////////////////
  257. void EditorPropertyClassName::setup(const String &p_base_type, const String &p_selected_type) {
  258. base_type = p_base_type;
  259. dialog->set_base_type(base_type);
  260. selected_type = p_selected_type;
  261. property->set_text(selected_type);
  262. }
  263. void EditorPropertyClassName::update_property() {
  264. String s = get_edited_object()->get(get_edited_property());
  265. property->set_text(s);
  266. selected_type = s;
  267. }
  268. void EditorPropertyClassName::_property_selected() {
  269. dialog->popup_create(true);
  270. }
  271. void EditorPropertyClassName::_dialog_created() {
  272. selected_type = dialog->get_selected_type();
  273. emit_changed(get_edited_property(), selected_type);
  274. update_property();
  275. }
  276. void EditorPropertyClassName::_bind_methods() {
  277. ClassDB::bind_method(D_METHOD("_dialog_created"), &EditorPropertyClassName::_dialog_created);
  278. ClassDB::bind_method(D_METHOD("_property_selected"), &EditorPropertyClassName::_property_selected);
  279. }
  280. EditorPropertyClassName::EditorPropertyClassName() {
  281. property = memnew(Button);
  282. property->set_clip_text(true);
  283. add_child(property);
  284. add_focusable(property);
  285. property->set_text(selected_type);
  286. property->connect("pressed", this, "_property_selected");
  287. dialog = memnew(CreateDialog);
  288. dialog->set_base_type(base_type);
  289. dialog->connect("create", this, "_dialog_created");
  290. add_child(dialog);
  291. }
  292. ///////////////////// MEMBER /////////////////////////
  293. void EditorPropertyMember::_property_selected(const String &p_selected) {
  294. emit_changed(get_edited_property(), p_selected);
  295. update_property();
  296. }
  297. void EditorPropertyMember::_property_select() {
  298. if (!selector) {
  299. selector = memnew(PropertySelector);
  300. selector->connect("selected", this, "_property_selected");
  301. add_child(selector);
  302. }
  303. String current = get_edited_object()->get(get_edited_property());
  304. if (hint == MEMBER_METHOD_OF_VARIANT_TYPE) {
  305. Variant::Type type = Variant::NIL;
  306. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  307. if (hint_text == Variant::get_type_name(Variant::Type(i))) {
  308. type = Variant::Type(i);
  309. }
  310. }
  311. if (type != Variant::NIL) {
  312. selector->select_method_from_basic_type(type, current);
  313. }
  314. } else if (hint == MEMBER_METHOD_OF_BASE_TYPE) {
  315. selector->select_method_from_base_type(hint_text, current);
  316. } else if (hint == MEMBER_METHOD_OF_INSTANCE) {
  317. Object *instance = ObjectDB::get_instance(hint_text.to_int64());
  318. if (instance) {
  319. selector->select_method_from_instance(instance, current);
  320. }
  321. } else if (hint == MEMBER_METHOD_OF_SCRIPT) {
  322. Object *obj = ObjectDB::get_instance(hint_text.to_int64());
  323. if (Object::cast_to<Script>(obj)) {
  324. selector->select_method_from_script(Object::cast_to<Script>(obj), current);
  325. }
  326. } else if (hint == MEMBER_PROPERTY_OF_VARIANT_TYPE) {
  327. Variant::Type type = Variant::NIL;
  328. String tname = hint_text;
  329. if (tname.find(".") != -1) {
  330. tname = tname.get_slice(".", 0);
  331. }
  332. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  333. if (tname == Variant::get_type_name(Variant::Type(i))) {
  334. type = Variant::Type(Variant::Type(i));
  335. }
  336. }
  337. if (type != Variant::NIL) {
  338. selector->select_property_from_basic_type(type, current);
  339. }
  340. } else if (hint == MEMBER_PROPERTY_OF_BASE_TYPE) {
  341. selector->select_property_from_base_type(hint_text, current);
  342. } else if (hint == MEMBER_PROPERTY_OF_INSTANCE) {
  343. Object *instance = ObjectDB::get_instance(hint_text.to_int64());
  344. if (instance) {
  345. selector->select_property_from_instance(instance, current);
  346. }
  347. } else if (hint == MEMBER_PROPERTY_OF_SCRIPT) {
  348. Object *obj = ObjectDB::get_instance(hint_text.to_int64());
  349. if (Object::cast_to<Script>(obj)) {
  350. selector->select_property_from_script(Object::cast_to<Script>(obj), current);
  351. }
  352. }
  353. }
  354. void EditorPropertyMember::setup(Type p_hint, const String &p_hint_text) {
  355. hint = p_hint;
  356. hint_text = p_hint_text;
  357. }
  358. void EditorPropertyMember::update_property() {
  359. String full_path = get_edited_object()->get(get_edited_property());
  360. property->set_text(full_path);
  361. }
  362. void EditorPropertyMember::_bind_methods() {
  363. ClassDB::bind_method(D_METHOD("_property_selected"), &EditorPropertyMember::_property_selected);
  364. ClassDB::bind_method(D_METHOD("_property_select"), &EditorPropertyMember::_property_select);
  365. }
  366. EditorPropertyMember::EditorPropertyMember() {
  367. selector = nullptr;
  368. property = memnew(Button);
  369. property->set_clip_text(true);
  370. add_child(property);
  371. add_focusable(property);
  372. property->connect("pressed", this, "_property_select");
  373. }
  374. ///////////////////// CHECK /////////////////////////
  375. void EditorPropertyCheck::_checkbox_pressed() {
  376. emit_changed(get_edited_property(), checkbox->is_pressed());
  377. }
  378. void EditorPropertyCheck::update_property() {
  379. bool c = get_edited_object()->get(get_edited_property());
  380. checkbox->set_pressed(c);
  381. checkbox->set_disabled(is_read_only());
  382. }
  383. void EditorPropertyCheck::_bind_methods() {
  384. ClassDB::bind_method(D_METHOD("_checkbox_pressed"), &EditorPropertyCheck::_checkbox_pressed);
  385. }
  386. EditorPropertyCheck::EditorPropertyCheck() {
  387. checkbox = memnew(CheckBox);
  388. checkbox->set_text(TTR("On"));
  389. add_child(checkbox);
  390. add_focusable(checkbox);
  391. checkbox->connect("pressed", this, "_checkbox_pressed");
  392. }
  393. ///////////////////// ENUM /////////////////////////
  394. void EditorPropertyEnum::_option_selected(int p_which) {
  395. int64_t val = options->get_item_metadata(p_which);
  396. emit_changed(get_edited_property(), val);
  397. }
  398. void EditorPropertyEnum::update_property() {
  399. int64_t which = get_edited_object()->get(get_edited_property());
  400. for (int i = 0; i < options->get_item_count(); i++) {
  401. if (which == (int64_t)options->get_item_metadata(i)) {
  402. options->select(i);
  403. return;
  404. }
  405. }
  406. }
  407. void EditorPropertyEnum::setup(const Vector<String> &p_options) {
  408. int64_t current_val = 0;
  409. for (int i = 0; i < p_options.size(); i++) {
  410. Vector<String> text_split = p_options[i].split(":");
  411. if (text_split.size() != 1) {
  412. current_val = text_split[1].to_int64();
  413. }
  414. options->add_item(text_split[0]);
  415. options->set_item_metadata(i, current_val);
  416. current_val += 1;
  417. }
  418. }
  419. void EditorPropertyEnum::set_option_button_clip(bool p_enable) {
  420. options->set_clip_text(p_enable);
  421. }
  422. void EditorPropertyEnum::_bind_methods() {
  423. ClassDB::bind_method(D_METHOD("_option_selected"), &EditorPropertyEnum::_option_selected);
  424. }
  425. EditorPropertyEnum::EditorPropertyEnum() {
  426. options = memnew(OptionButton);
  427. options->set_clip_text(true);
  428. options->set_flat(true);
  429. add_child(options);
  430. add_focusable(options);
  431. options->connect("item_selected", this, "_option_selected");
  432. }
  433. ///////////////////// FLAGS /////////////////////////
  434. void EditorPropertyFlags::_flag_toggled() {
  435. uint32_t value = 0;
  436. for (int i = 0; i < flags.size(); i++) {
  437. if (flags[i]->is_pressed()) {
  438. uint32_t val = 1;
  439. val <<= flag_indices[i];
  440. value |= val;
  441. }
  442. }
  443. emit_changed(get_edited_property(), value);
  444. }
  445. void EditorPropertyFlags::update_property() {
  446. uint32_t value = get_edited_object()->get(get_edited_property());
  447. for (int i = 0; i < flags.size(); i++) {
  448. uint32_t val = 1;
  449. val <<= flag_indices[i];
  450. if (value & val) {
  451. flags[i]->set_pressed(true);
  452. } else {
  453. flags[i]->set_pressed(false);
  454. }
  455. }
  456. }
  457. void EditorPropertyFlags::setup(const Vector<String> &p_options) {
  458. ERR_FAIL_COND(flags.size());
  459. bool first = true;
  460. for (int i = 0; i < p_options.size(); i++) {
  461. String option = p_options[i].strip_edges();
  462. if (option != "") {
  463. CheckBox *cb = memnew(CheckBox);
  464. cb->set_text(option);
  465. cb->set_clip_text(true);
  466. cb->connect("pressed", this, "_flag_toggled");
  467. add_focusable(cb);
  468. vbox->add_child(cb);
  469. flags.push_back(cb);
  470. flag_indices.push_back(i);
  471. if (first) {
  472. set_label_reference(cb);
  473. first = false;
  474. }
  475. }
  476. }
  477. }
  478. void EditorPropertyFlags::_bind_methods() {
  479. ClassDB::bind_method(D_METHOD("_flag_toggled"), &EditorPropertyFlags::_flag_toggled);
  480. }
  481. EditorPropertyFlags::EditorPropertyFlags() {
  482. vbox = memnew(VBoxContainer);
  483. add_child(vbox);
  484. }
  485. ///////////////////// LAYERS /////////////////////////
  486. class EditorPropertyLayersGrid : public Control {
  487. GDCLASS(EditorPropertyLayersGrid, Control);
  488. private:
  489. Vector<Rect2> flag_rects;
  490. Rect2 expand_rect;
  491. bool expand_hovered = false;
  492. bool expanded = false;
  493. int expansion_rows = 0;
  494. int hovered_index = -1;
  495. Size2 get_grid_size() const {
  496. Ref<Font> font = get_font("font", "Label");
  497. return Size2(0, font->get_height() * 3);
  498. }
  499. public:
  500. uint32_t value = 0;
  501. int layer_group_size = 0;
  502. int layer_count = 0;
  503. Vector<String> names;
  504. Vector<String> tooltips;
  505. virtual Size2 get_minimum_size() const {
  506. Size2 min_size = get_grid_size();
  507. // Add extra rows when expanded.
  508. if (expanded) {
  509. const int bsize = (min_size.height * 80 / 100) / 2;
  510. for (int i = 0; i < expansion_rows; ++i) {
  511. min_size.y += 2 * (bsize + 1) + 3;
  512. }
  513. }
  514. return min_size;
  515. }
  516. virtual String get_tooltip(const Point2 &p_pos) const {
  517. for (int i = 0; i < flag_rects.size(); i++) {
  518. if (i < tooltips.size() && flag_rects[i].has_point(p_pos)) {
  519. return tooltips[i];
  520. }
  521. }
  522. return String();
  523. }
  524. void _gui_input(const Ref<InputEvent> &p_ev) {
  525. const Ref<InputEventMouseMotion> mm = p_ev;
  526. if (mm.is_valid()) {
  527. bool expand_was_hovered = expand_hovered;
  528. expand_hovered = expand_rect.has_point(mm->get_position());
  529. if (expand_hovered != expand_was_hovered) {
  530. update();
  531. }
  532. if (!expand_hovered) {
  533. for (int i = 0; i < flag_rects.size(); i++) {
  534. if (flag_rects[i].has_point(mm->get_position())) {
  535. // Used to highlight the hovered flag in the layers grid.
  536. hovered_index = i;
  537. update();
  538. return;
  539. }
  540. }
  541. }
  542. // Remove highlight when no square is hovered.
  543. if (hovered_index != -1) {
  544. hovered_index = -1;
  545. update();
  546. }
  547. return;
  548. }
  549. const Ref<InputEventMouseButton> mb = p_ev;
  550. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
  551. if (hovered_index >= 0) {
  552. // Toggle the flag.
  553. // We base our choice on the hovered flag, so that it always matches the hovered flag.
  554. if (value & (1 << hovered_index)) {
  555. value &= ~(1 << hovered_index);
  556. } else {
  557. value |= (1 << hovered_index);
  558. }
  559. emit_signal("flag_changed", value);
  560. update();
  561. } else if (expand_hovered) {
  562. expanded = !expanded;
  563. minimum_size_changed();
  564. update();
  565. }
  566. }
  567. }
  568. void _notification(int p_what) {
  569. switch (p_what) {
  570. case NOTIFICATION_DRAW: {
  571. Size2 grid_size = get_grid_size();
  572. grid_size.x = get_size().x;
  573. flag_rects.clear();
  574. int prev_expansion_rows = expansion_rows;
  575. expansion_rows = 0;
  576. const int bsize = (grid_size.height * 80 / 100) / 2;
  577. const int h = bsize * 2 + 1;
  578. Color color = get_color("highlight_color", "Editor");
  579. Color text_color = get_color("font_color", "Editor");
  580. text_color.a *= 0.5;
  581. Color text_color_on = get_color("dark_color_3", "Editor");
  582. text_color_on.a *= 0.8;
  583. const int vofs = (grid_size.height - h) / 2;
  584. int layer_index = 0;
  585. int block_index = 0;
  586. Point2 arrow_pos;
  587. Point2 block_ofs(4, vofs);
  588. while (true) {
  589. Point2 ofs = block_ofs;
  590. for (int i = 0; i < 2; i++) {
  591. for (int j = 0; j < layer_group_size; j++) {
  592. const bool on = value & (1 << layer_index);
  593. Rect2 rect2 = Rect2(ofs, Size2(bsize, bsize));
  594. color.a = on ? 0.6 : 0.2;
  595. if (layer_index == hovered_index) {
  596. // Add visual feedback when hovering a flag.
  597. color.a += 0.15;
  598. }
  599. draw_rect(rect2, color);
  600. flag_rects.push_back(rect2);
  601. Ref<Font> font = get_font("font", "Label");
  602. Vector2 offset;
  603. if (layer_index + 1 > 9) {
  604. // Offset for double digit numbers.
  605. offset.x = rect2.size.x * 0.1;
  606. } else {
  607. offset.x = rect2.size.x * 0.3;
  608. }
  609. offset.y = rect2.size.y * 0.75;
  610. draw_string(font, rect2.position + offset, itos(layer_index + 1), on ? text_color_on : text_color);
  611. ofs.x += bsize + 1;
  612. ++layer_index;
  613. }
  614. ofs.x = block_ofs.x;
  615. ofs.y += bsize + 1;
  616. }
  617. if (layer_index >= layer_count) {
  618. if (!flag_rects.empty() && (expansion_rows == 0)) {
  619. const Rect2 &last_rect = flag_rects[flag_rects.size() - 1];
  620. arrow_pos = last_rect.position + last_rect.size;
  621. }
  622. break;
  623. }
  624. int block_size_x = layer_group_size * (bsize + 1);
  625. block_ofs.x += block_size_x + 3;
  626. if (block_ofs.x + block_size_x + 12 > grid_size.width) {
  627. // Keep last valid cell position for the expansion icon.
  628. if (!flag_rects.empty() && (expansion_rows == 0)) {
  629. const Rect2 &last_rect = flag_rects[flag_rects.size() - 1];
  630. arrow_pos = last_rect.position + last_rect.size;
  631. }
  632. ++expansion_rows;
  633. if (expanded) {
  634. // Expand grid to next line.
  635. block_ofs.x = 4;
  636. block_ofs.y += 2 * (bsize + 1) + 3;
  637. } else {
  638. // Skip remaining blocks.
  639. break;
  640. }
  641. }
  642. ++block_index;
  643. }
  644. if ((expansion_rows != prev_expansion_rows) && expanded) {
  645. minimum_size_changed();
  646. }
  647. if ((expansion_rows == 0) && (layer_index == layer_count)) {
  648. // Whole grid was drawn, no need for expansion icon.
  649. break;
  650. }
  651. Ref<Texture> arrow = get_icon("arrow", "Tree");
  652. ERR_FAIL_COND(arrow.is_null());
  653. Color arrow_color = get_color("highlight_color", "Editor");
  654. arrow_color.a = expand_hovered ? 1.0 : 0.6;
  655. arrow_pos.x += 2.0;
  656. arrow_pos.y -= arrow->get_height();
  657. Rect2 arrow_draw_rect(arrow_pos, arrow->get_size());
  658. expand_rect = arrow_draw_rect;
  659. if (expanded) {
  660. arrow_draw_rect.size.y *= -1.0; // Flip arrow vertically when expanded.
  661. }
  662. RID ci = get_canvas_item();
  663. arrow->draw_rect(ci, arrow_draw_rect, false, arrow_color);
  664. } break;
  665. case NOTIFICATION_MOUSE_EXIT: {
  666. if (expand_hovered) {
  667. expand_hovered = false;
  668. update();
  669. }
  670. if (hovered_index != -1) {
  671. hovered_index = -1;
  672. update();
  673. }
  674. } break;
  675. default:
  676. break;
  677. }
  678. }
  679. void set_flag(uint32_t p_flag) {
  680. value = p_flag;
  681. update();
  682. }
  683. static void _bind_methods() {
  684. ClassDB::bind_method(D_METHOD("_gui_input"), &EditorPropertyLayersGrid::_gui_input);
  685. ADD_SIGNAL(MethodInfo("flag_changed", PropertyInfo(Variant::INT, "flag")));
  686. }
  687. };
  688. void EditorPropertyLayers::_grid_changed(uint32_t p_grid) {
  689. emit_changed(get_edited_property(), p_grid);
  690. }
  691. void EditorPropertyLayers::update_property() {
  692. uint32_t value = get_edited_object()->get(get_edited_property());
  693. grid->set_flag(value);
  694. }
  695. void EditorPropertyLayers::setup(LayerType p_layer_type) {
  696. String basename;
  697. int layer_group_size = 0;
  698. int layer_count = 0;
  699. switch (p_layer_type) {
  700. case LAYER_RENDER_2D: {
  701. basename = "layer_names/2d_render";
  702. layer_group_size = 5;
  703. layer_count = 20;
  704. } break;
  705. case LAYER_PHYSICS_2D: {
  706. basename = "layer_names/2d_physics";
  707. layer_group_size = 4;
  708. layer_count = 32;
  709. } break;
  710. case LAYER_RENDER_3D: {
  711. basename = "layer_names/3d_render";
  712. layer_group_size = 5;
  713. layer_count = 20;
  714. } break;
  715. case LAYER_PHYSICS_3D: {
  716. basename = "layer_names/3d_physics";
  717. layer_group_size = 4;
  718. layer_count = 32;
  719. } break;
  720. }
  721. Vector<String> names;
  722. Vector<String> tooltips;
  723. for (int i = 0; i < layer_count; i++) {
  724. String name;
  725. if (ProjectSettings::get_singleton()->has_setting(basename + "/layer_" + itos(i + 1))) {
  726. name = ProjectSettings::get_singleton()->get(basename + "/layer_" + itos(i + 1));
  727. }
  728. if (name == "") {
  729. name = TTR("Layer") + " " + itos(i + 1);
  730. }
  731. names.push_back(name);
  732. tooltips.push_back(name + "\n" + vformat(TTR("Bit %d, value %d"), i, 1 << i));
  733. }
  734. grid->names = names;
  735. grid->tooltips = tooltips;
  736. grid->layer_group_size = layer_group_size;
  737. grid->layer_count = layer_count;
  738. }
  739. void EditorPropertyLayers::_button_pressed() {
  740. int layer_count = grid->layer_count;
  741. int layer_group_size = grid->layer_group_size;
  742. layers->clear();
  743. for (int i = 0; i < layer_count; i++) {
  744. if ((i != 0) && ((i % layer_group_size) == 0)) {
  745. layers->add_separator();
  746. }
  747. layers->add_check_item(grid->names[i], i);
  748. int idx = layers->get_item_index(i);
  749. layers->set_item_checked(idx, grid->value & (1 << i));
  750. }
  751. Rect2 gp = button->get_global_rect();
  752. layers->set_as_minsize();
  753. Vector2 popup_pos = gp.position - Vector2(layers->get_combined_minimum_size().x, 0);
  754. layers->set_global_position(popup_pos);
  755. layers->popup();
  756. }
  757. void EditorPropertyLayers::_menu_pressed(int p_menu) {
  758. if (grid->value & (1 << p_menu)) {
  759. grid->value &= ~(1 << p_menu);
  760. } else {
  761. grid->value |= (1 << p_menu);
  762. }
  763. grid->update();
  764. layers->set_item_checked(layers->get_item_index(p_menu), grid->value & (1 << p_menu));
  765. _grid_changed(grid->value);
  766. }
  767. void EditorPropertyLayers::_bind_methods() {
  768. ClassDB::bind_method(D_METHOD("_grid_changed"), &EditorPropertyLayers::_grid_changed);
  769. ClassDB::bind_method(D_METHOD("_button_pressed"), &EditorPropertyLayers::_button_pressed);
  770. ClassDB::bind_method(D_METHOD("_menu_pressed"), &EditorPropertyLayers::_menu_pressed);
  771. }
  772. EditorPropertyLayers::EditorPropertyLayers() {
  773. HBoxContainer *hb = memnew(HBoxContainer);
  774. hb->set_clip_contents(true);
  775. add_child(hb);
  776. grid = memnew(EditorPropertyLayersGrid);
  777. grid->connect("flag_changed", this, "_grid_changed");
  778. grid->set_h_size_flags(SIZE_EXPAND_FILL);
  779. hb->add_child(grid);
  780. button = memnew(Button);
  781. button->set_toggle_mode(true);
  782. button->set_text("...");
  783. button->connect("pressed", this, "_button_pressed");
  784. hb->add_child(button);
  785. set_bottom_editor(hb);
  786. layers = memnew(PopupMenu);
  787. add_child(layers);
  788. layers->set_hide_on_checkable_item_selection(false);
  789. layers->connect("id_pressed", this, "_menu_pressed");
  790. layers->connect("popup_hide", button, "set_pressed", varray(false));
  791. }
  792. ///////////////////// INT /////////////////////////
  793. void EditorPropertyInteger::_value_changed(int64_t val) {
  794. if (setting) {
  795. return;
  796. }
  797. emit_changed(get_edited_property(), val);
  798. }
  799. void EditorPropertyInteger::update_property() {
  800. int64_t val = get_edited_object()->get(get_edited_property());
  801. setting = true;
  802. spin->set_value(val);
  803. setting = false;
  804. #ifdef DEBUG_ENABLED
  805. // If spin (currently EditorSplinSlider : Range) is changed so that it can use int64_t, then the below warning wouldn't be a problem.
  806. if (val != (int64_t)(double)(val)) {
  807. WARN_PRINT("Cannot reliably represent '" + itos(val) + "' in the inspector, value is too large.");
  808. }
  809. #endif
  810. }
  811. void EditorPropertyInteger::_bind_methods() {
  812. ClassDB::bind_method(D_METHOD("_value_changed"), &EditorPropertyInteger::_value_changed);
  813. }
  814. void EditorPropertyInteger::setup(int64_t p_min, int64_t p_max, int64_t p_step, bool p_allow_greater, bool p_allow_lesser) {
  815. spin->set_min(p_min);
  816. spin->set_max(p_max);
  817. spin->set_step(p_step);
  818. spin->set_allow_greater(p_allow_greater);
  819. spin->set_allow_lesser(p_allow_lesser);
  820. }
  821. EditorPropertyInteger::EditorPropertyInteger() {
  822. spin = memnew(EditorSpinSlider);
  823. spin->set_flat(true);
  824. add_child(spin);
  825. add_focusable(spin);
  826. spin->connect("value_changed", this, "_value_changed");
  827. setting = false;
  828. }
  829. ///////////////////// OBJECT ID /////////////////////////
  830. void EditorPropertyObjectID::_edit_pressed() {
  831. emit_signal("object_id_selected", get_edited_property(), get_edited_object()->get(get_edited_property()));
  832. }
  833. void EditorPropertyObjectID::update_property() {
  834. String type = base_type;
  835. if (type == "") {
  836. type = "Object";
  837. }
  838. ObjectID id = get_edited_object()->get(get_edited_property());
  839. if (id != 0) {
  840. edit->set_text(type + " ID: " + itos(id));
  841. edit->set_disabled(false);
  842. edit->set_icon(EditorNode::get_singleton()->get_class_icon(type));
  843. } else {
  844. edit->set_text(TTR("[Empty]"));
  845. edit->set_disabled(true);
  846. edit->set_icon(Ref<Texture>());
  847. }
  848. }
  849. void EditorPropertyObjectID::setup(const String &p_base_type) {
  850. base_type = p_base_type;
  851. }
  852. void EditorPropertyObjectID::_bind_methods() {
  853. ClassDB::bind_method(D_METHOD("_edit_pressed"), &EditorPropertyObjectID::_edit_pressed);
  854. }
  855. EditorPropertyObjectID::EditorPropertyObjectID() {
  856. edit = memnew(Button);
  857. add_child(edit);
  858. add_focusable(edit);
  859. edit->connect("pressed", this, "_edit_pressed");
  860. }
  861. ///////////////////// FLOAT /////////////////////////
  862. void EditorPropertyFloat::_value_changed(double val) {
  863. if (setting) {
  864. return;
  865. }
  866. emit_changed(get_edited_property(), val);
  867. }
  868. void EditorPropertyFloat::update_property() {
  869. double val = get_edited_object()->get(get_edited_property());
  870. setting = true;
  871. spin->set_value(val);
  872. setting = false;
  873. }
  874. void EditorPropertyFloat::_bind_methods() {
  875. ClassDB::bind_method(D_METHOD("_value_changed"), &EditorPropertyFloat::_value_changed);
  876. }
  877. void EditorPropertyFloat::setup(double p_min, double p_max, double p_step, bool p_no_slider, bool p_exp_range, bool p_greater, bool p_lesser) {
  878. spin->set_min(p_min);
  879. spin->set_max(p_max);
  880. spin->set_step(p_step);
  881. spin->set_hide_slider(p_no_slider);
  882. spin->set_exp_ratio(p_exp_range);
  883. spin->set_allow_greater(p_greater);
  884. spin->set_allow_lesser(p_lesser);
  885. }
  886. EditorPropertyFloat::EditorPropertyFloat() {
  887. spin = memnew(EditorSpinSlider);
  888. spin->set_flat(true);
  889. add_child(spin);
  890. add_focusable(spin);
  891. spin->connect("value_changed", this, "_value_changed");
  892. setting = false;
  893. }
  894. ///////////////////// EASING /////////////////////////
  895. void EditorPropertyEasing::_drag_easing(const Ref<InputEvent> &p_ev) {
  896. const Ref<InputEventMouseButton> mb = p_ev;
  897. if (mb.is_valid()) {
  898. if (mb->is_doubleclick() && mb->get_button_index() == BUTTON_LEFT) {
  899. _setup_spin();
  900. }
  901. if (mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  902. preset->set_global_position(easing_draw->get_global_transform().xform(mb->get_position()));
  903. preset->popup();
  904. // Ensure the easing doesn't appear as being dragged
  905. dragging = false;
  906. easing_draw->update();
  907. }
  908. if (mb->get_button_index() == BUTTON_LEFT) {
  909. dragging = mb->is_pressed();
  910. // Update to display the correct dragging color
  911. easing_draw->update();
  912. }
  913. }
  914. const Ref<InputEventMouseMotion> mm = p_ev;
  915. if (dragging && mm.is_valid() && mm->get_button_mask() & BUTTON_MASK_LEFT) {
  916. float rel = mm->get_relative().x;
  917. if (rel == 0) {
  918. return;
  919. }
  920. if (flip) {
  921. rel = -rel;
  922. }
  923. float val = get_edited_object()->get(get_edited_property());
  924. bool sg = val < 0;
  925. val = Math::absf(val);
  926. val = Math::log(val) / Math::log((float)2.0);
  927. // Logarithmic space.
  928. val += rel * 0.05;
  929. val = Math::pow(2.0f, val);
  930. if (sg) {
  931. val = -val;
  932. }
  933. // 0 is a singularity, but both positive and negative values
  934. // are otherwise allowed. Enforce 0+ as workaround.
  935. if (Math::is_zero_approx(val)) {
  936. val = 0.00001;
  937. }
  938. // Limit to a reasonable value to prevent the curve going into infinity,
  939. // which can cause crashes and other issues.
  940. val = CLAMP(val, -1000000, 1000000);
  941. emit_changed(get_edited_property(), val);
  942. easing_draw->update();
  943. }
  944. }
  945. void EditorPropertyEasing::_draw_easing() {
  946. RID ci = easing_draw->get_canvas_item();
  947. Size2 s = easing_draw->get_size();
  948. const int point_count = 48;
  949. const float exp = get_edited_object()->get(get_edited_property());
  950. const Ref<Font> f = get_font("font", "Label");
  951. const Color font_color = get_color("font_color", "Label");
  952. Color line_color;
  953. if (dragging) {
  954. line_color = get_color("accent_color", "Editor");
  955. } else {
  956. line_color = get_color("font_color", "Label") * Color(1, 1, 1, 0.9);
  957. }
  958. Vector<Point2> points;
  959. for (int i = 0; i <= point_count; i++) {
  960. float ifl = i / float(point_count);
  961. const float h = 1.0 - Math::ease(ifl, exp);
  962. if (flip) {
  963. ifl = 1.0 - ifl;
  964. }
  965. points.push_back(Point2(ifl * s.width, h * s.height));
  966. }
  967. easing_draw->draw_polyline(points, line_color, 1.0, true);
  968. // Draw more decimals for small numbers since higher precision is usually required for fine adjustments.
  969. int decimals;
  970. if (Math::abs(exp) < 0.1 - CMP_EPSILON) {
  971. decimals = 4;
  972. } else if (Math::abs(exp) < 1 - CMP_EPSILON) {
  973. decimals = 3;
  974. } else if (Math::abs(exp) < 10 - CMP_EPSILON) {
  975. decimals = 2;
  976. } else {
  977. decimals = 1;
  978. }
  979. f->draw(ci, Point2(10, 10 + f->get_ascent()), rtos(exp).pad_decimals(decimals), font_color);
  980. }
  981. void EditorPropertyEasing::update_property() {
  982. easing_draw->update();
  983. }
  984. void EditorPropertyEasing::_set_preset(int p_preset) {
  985. static const float preset_value[EASING_MAX] = { 0.0, 1.0, 2.0, 0.5, -2.0, -0.5 };
  986. emit_changed(get_edited_property(), preset_value[p_preset]);
  987. easing_draw->update();
  988. }
  989. void EditorPropertyEasing::_setup_spin() {
  990. setting = true;
  991. spin->setup_and_show();
  992. spin->get_line_edit()->set_text(rtos(get_edited_object()->get(get_edited_property())));
  993. setting = false;
  994. spin->show();
  995. }
  996. void EditorPropertyEasing::_spin_value_changed(double p_value) {
  997. if (setting) {
  998. return;
  999. }
  1000. // 0 is a singularity, but both positive and negative values
  1001. // are otherwise allowed. Enforce 0+ as workaround.
  1002. if (Math::is_zero_approx(p_value)) {
  1003. p_value = 0.00001;
  1004. }
  1005. // Limit to a reasonable value to prevent the curve going into infinity,
  1006. // which can cause crashes and other issues.
  1007. p_value = CLAMP(p_value, -1000000, 1000000);
  1008. emit_changed(get_edited_property(), p_value);
  1009. _spin_focus_exited();
  1010. }
  1011. void EditorPropertyEasing::_spin_focus_exited() {
  1012. spin->hide();
  1013. // Ensure the easing doesn't appear as being dragged
  1014. dragging = false;
  1015. easing_draw->update();
  1016. }
  1017. void EditorPropertyEasing::setup(bool p_full, bool p_flip) {
  1018. flip = p_flip;
  1019. full = p_full;
  1020. }
  1021. void EditorPropertyEasing::_notification(int p_what) {
  1022. switch (p_what) {
  1023. case NOTIFICATION_THEME_CHANGED:
  1024. case NOTIFICATION_ENTER_TREE: {
  1025. preset->clear();
  1026. preset->add_icon_item(get_icon("CurveConstant", "EditorIcons"), "Zero", EASING_ZERO);
  1027. preset->add_icon_item(get_icon("CurveLinear", "EditorIcons"), "Linear", EASING_LINEAR);
  1028. preset->add_icon_item(get_icon("CurveIn", "EditorIcons"), "In", EASING_IN);
  1029. preset->add_icon_item(get_icon("CurveOut", "EditorIcons"), "Out", EASING_OUT);
  1030. if (full) {
  1031. preset->add_icon_item(get_icon("CurveInOut", "EditorIcons"), "In-Out", EASING_IN_OUT);
  1032. preset->add_icon_item(get_icon("CurveOutIn", "EditorIcons"), "Out-In", EASING_OUT_IN);
  1033. }
  1034. easing_draw->set_custom_minimum_size(Size2(0, get_font("font", "Label")->get_height() * 2));
  1035. } break;
  1036. }
  1037. }
  1038. void EditorPropertyEasing::_bind_methods() {
  1039. ClassDB::bind_method("_draw_easing", &EditorPropertyEasing::_draw_easing);
  1040. ClassDB::bind_method("_drag_easing", &EditorPropertyEasing::_drag_easing);
  1041. ClassDB::bind_method("_set_preset", &EditorPropertyEasing::_set_preset);
  1042. ClassDB::bind_method("_spin_value_changed", &EditorPropertyEasing::_spin_value_changed);
  1043. ClassDB::bind_method("_spin_focus_exited", &EditorPropertyEasing::_spin_focus_exited);
  1044. }
  1045. EditorPropertyEasing::EditorPropertyEasing() {
  1046. easing_draw = memnew(Control);
  1047. easing_draw->connect("draw", this, "_draw_easing");
  1048. easing_draw->connect("gui_input", this, "_drag_easing");
  1049. easing_draw->set_default_cursor_shape(Control::CURSOR_MOVE);
  1050. add_child(easing_draw);
  1051. preset = memnew(PopupMenu);
  1052. add_child(preset);
  1053. preset->connect("id_pressed", this, "_set_preset");
  1054. spin = memnew(EditorSpinSlider);
  1055. spin->set_flat(true);
  1056. spin->set_min(-100);
  1057. spin->set_max(100);
  1058. spin->set_step(0);
  1059. spin->set_hide_slider(true);
  1060. spin->set_allow_lesser(true);
  1061. spin->set_allow_greater(true);
  1062. spin->connect("value_changed", this, "_spin_value_changed");
  1063. spin->get_line_edit()->connect("focus_exited", this, "_spin_focus_exited");
  1064. spin->hide();
  1065. add_child(spin);
  1066. dragging = false;
  1067. flip = false;
  1068. full = false;
  1069. }
  1070. ///////////////////// VECTOR2 /////////////////////////
  1071. void EditorPropertyVector2::_value_changed(double val, const String &p_name) {
  1072. if (setting) {
  1073. return;
  1074. }
  1075. Vector2 v2;
  1076. v2.x = spin[0]->get_value();
  1077. v2.y = spin[1]->get_value();
  1078. emit_changed(get_edited_property(), v2, p_name);
  1079. }
  1080. void EditorPropertyVector2::update_property() {
  1081. Vector2 val = get_edited_object()->get(get_edited_property());
  1082. setting = true;
  1083. spin[0]->set_value(val.x);
  1084. spin[1]->set_value(val.y);
  1085. setting = false;
  1086. }
  1087. void EditorPropertyVector2::_notification(int p_what) {
  1088. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1089. Color base = get_color("accent_color", "Editor");
  1090. for (int i = 0; i < 2; i++) {
  1091. Color c = base;
  1092. c.set_hsv(float(i) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1093. spin[i]->set_custom_label_color(true, c);
  1094. }
  1095. }
  1096. }
  1097. void EditorPropertyVector2::_bind_methods() {
  1098. ClassDB::bind_method(D_METHOD("_value_changed"), &EditorPropertyVector2::_value_changed);
  1099. }
  1100. void EditorPropertyVector2::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1101. for (int i = 0; i < 2; i++) {
  1102. spin[i]->set_min(p_min);
  1103. spin[i]->set_max(p_max);
  1104. spin[i]->set_step(p_step);
  1105. spin[i]->set_hide_slider(p_no_slider);
  1106. spin[i]->set_allow_greater(true);
  1107. spin[i]->set_allow_lesser(true);
  1108. }
  1109. }
  1110. EditorPropertyVector2::EditorPropertyVector2() {
  1111. bool horizontal = EDITOR_GET("interface/inspector/horizontal_vector2_editing");
  1112. BoxContainer *bc;
  1113. if (horizontal) {
  1114. bc = memnew(HBoxContainer);
  1115. add_child(bc);
  1116. set_bottom_editor(bc);
  1117. } else {
  1118. bc = memnew(VBoxContainer);
  1119. add_child(bc);
  1120. }
  1121. static const char *desc[2] = { "x", "y" };
  1122. for (int i = 0; i < 2; i++) {
  1123. spin[i] = memnew(EditorSpinSlider);
  1124. spin[i]->set_flat(true);
  1125. spin[i]->set_label(desc[i]);
  1126. bc->add_child(spin[i]);
  1127. add_focusable(spin[i]);
  1128. spin[i]->connect("value_changed", this, "_value_changed", varray(desc[i]));
  1129. if (horizontal) {
  1130. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1131. }
  1132. }
  1133. if (!horizontal) {
  1134. set_label_reference(spin[0]); //show text and buttons around this
  1135. }
  1136. setting = false;
  1137. }
  1138. ///////////////////// RECT2 /////////////////////////
  1139. void EditorPropertyRect2::_value_changed(double val, const String &p_name) {
  1140. if (setting) {
  1141. return;
  1142. }
  1143. Rect2 r2;
  1144. r2.position.x = spin[0]->get_value();
  1145. r2.position.y = spin[1]->get_value();
  1146. r2.size.x = spin[2]->get_value();
  1147. r2.size.y = spin[3]->get_value();
  1148. emit_changed(get_edited_property(), r2, p_name);
  1149. }
  1150. void EditorPropertyRect2::update_property() {
  1151. Rect2 val = get_edited_object()->get(get_edited_property());
  1152. setting = true;
  1153. spin[0]->set_value(val.position.x);
  1154. spin[1]->set_value(val.position.y);
  1155. spin[2]->set_value(val.size.x);
  1156. spin[3]->set_value(val.size.y);
  1157. setting = false;
  1158. }
  1159. void EditorPropertyRect2::_notification(int p_what) {
  1160. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1161. Color base = get_color("accent_color", "Editor");
  1162. for (int i = 0; i < 4; i++) {
  1163. Color c = base;
  1164. c.set_hsv(float(i % 2) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1165. spin[i]->set_custom_label_color(true, c);
  1166. }
  1167. }
  1168. }
  1169. void EditorPropertyRect2::_bind_methods() {
  1170. ClassDB::bind_method(D_METHOD("_value_changed"), &EditorPropertyRect2::_value_changed);
  1171. }
  1172. void EditorPropertyRect2::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1173. for (int i = 0; i < 4; i++) {
  1174. spin[i]->set_min(p_min);
  1175. spin[i]->set_max(p_max);
  1176. spin[i]->set_step(p_step);
  1177. spin[i]->set_hide_slider(p_no_slider);
  1178. spin[i]->set_allow_greater(true);
  1179. spin[i]->set_allow_lesser(true);
  1180. }
  1181. }
  1182. EditorPropertyRect2::EditorPropertyRect2() {
  1183. bool horizontal = EDITOR_GET("interface/inspector/horizontal_vector_types_editing");
  1184. BoxContainer *bc;
  1185. if (horizontal) {
  1186. bc = memnew(HBoxContainer);
  1187. add_child(bc);
  1188. set_bottom_editor(bc);
  1189. } else {
  1190. bc = memnew(VBoxContainer);
  1191. add_child(bc);
  1192. }
  1193. static const char *desc[4] = { "x", "y", "w", "h" };
  1194. for (int i = 0; i < 4; i++) {
  1195. spin[i] = memnew(EditorSpinSlider);
  1196. spin[i]->set_label(desc[i]);
  1197. spin[i]->set_flat(true);
  1198. bc->add_child(spin[i]);
  1199. add_focusable(spin[i]);
  1200. spin[i]->connect("value_changed", this, "_value_changed", varray(desc[i]));
  1201. if (horizontal) {
  1202. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1203. }
  1204. }
  1205. if (!horizontal) {
  1206. set_label_reference(spin[0]); //show text and buttons around this
  1207. }
  1208. setting = false;
  1209. }
  1210. ///////////////////// VECTOR3 /////////////////////////
  1211. void EditorPropertyVector3::_value_changed(double val, const String &p_name) {
  1212. if (setting) {
  1213. return;
  1214. }
  1215. Vector3 v3;
  1216. v3.x = spin[0]->get_value();
  1217. v3.y = spin[1]->get_value();
  1218. v3.z = spin[2]->get_value();
  1219. emit_changed(get_edited_property(), v3, p_name);
  1220. }
  1221. void EditorPropertyVector3::update_property() {
  1222. Vector3 val = get_edited_object()->get(get_edited_property());
  1223. setting = true;
  1224. spin[0]->set_value(val.x);
  1225. spin[1]->set_value(val.y);
  1226. spin[2]->set_value(val.z);
  1227. setting = false;
  1228. }
  1229. void EditorPropertyVector3::_notification(int p_what) {
  1230. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1231. Color base = get_color("accent_color", "Editor");
  1232. for (int i = 0; i < 3; i++) {
  1233. Color c = base;
  1234. c.set_hsv(float(i) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1235. spin[i]->set_custom_label_color(true, c);
  1236. }
  1237. }
  1238. }
  1239. void EditorPropertyVector3::_bind_methods() {
  1240. ClassDB::bind_method(D_METHOD("_value_changed"), &EditorPropertyVector3::_value_changed);
  1241. }
  1242. void EditorPropertyVector3::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1243. for (int i = 0; i < 3; i++) {
  1244. spin[i]->set_min(p_min);
  1245. spin[i]->set_max(p_max);
  1246. spin[i]->set_step(p_step);
  1247. spin[i]->set_hide_slider(p_no_slider);
  1248. spin[i]->set_allow_greater(true);
  1249. spin[i]->set_allow_lesser(true);
  1250. }
  1251. }
  1252. EditorPropertyVector3::EditorPropertyVector3() {
  1253. bool horizontal = EDITOR_GET("interface/inspector/horizontal_vector_types_editing");
  1254. BoxContainer *bc;
  1255. if (horizontal) {
  1256. bc = memnew(HBoxContainer);
  1257. add_child(bc);
  1258. set_bottom_editor(bc);
  1259. } else {
  1260. bc = memnew(VBoxContainer);
  1261. add_child(bc);
  1262. }
  1263. static const char *desc[3] = { "x", "y", "z" };
  1264. for (int i = 0; i < 3; i++) {
  1265. spin[i] = memnew(EditorSpinSlider);
  1266. spin[i]->set_flat(true);
  1267. spin[i]->set_label(desc[i]);
  1268. bc->add_child(spin[i]);
  1269. add_focusable(spin[i]);
  1270. spin[i]->connect("value_changed", this, "_value_changed", varray(desc[i]));
  1271. if (horizontal) {
  1272. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1273. }
  1274. }
  1275. if (!horizontal) {
  1276. set_label_reference(spin[0]); //show text and buttons around this
  1277. }
  1278. setting = false;
  1279. }
  1280. ///////////////////// PLANE /////////////////////////
  1281. void EditorPropertyPlane::_value_changed(double val, const String &p_name) {
  1282. if (setting) {
  1283. return;
  1284. }
  1285. Plane p;
  1286. p.normal.x = spin[0]->get_value();
  1287. p.normal.y = spin[1]->get_value();
  1288. p.normal.z = spin[2]->get_value();
  1289. p.d = spin[3]->get_value();
  1290. emit_changed(get_edited_property(), p, p_name);
  1291. }
  1292. void EditorPropertyPlane::update_property() {
  1293. Plane val = get_edited_object()->get(get_edited_property());
  1294. setting = true;
  1295. spin[0]->set_value(val.normal.x);
  1296. spin[1]->set_value(val.normal.y);
  1297. spin[2]->set_value(val.normal.z);
  1298. spin[3]->set_value(val.d);
  1299. setting = false;
  1300. }
  1301. void EditorPropertyPlane::_notification(int p_what) {
  1302. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1303. Color base = get_color("accent_color", "Editor");
  1304. for (int i = 0; i < 3; i++) {
  1305. Color c = base;
  1306. c.set_hsv(float(i) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1307. spin[i]->set_custom_label_color(true, c);
  1308. }
  1309. }
  1310. }
  1311. void EditorPropertyPlane::_bind_methods() {
  1312. ClassDB::bind_method(D_METHOD("_value_changed"), &EditorPropertyPlane::_value_changed);
  1313. }
  1314. void EditorPropertyPlane::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1315. for (int i = 0; i < 4; i++) {
  1316. spin[i]->set_min(p_min);
  1317. spin[i]->set_max(p_max);
  1318. spin[i]->set_step(p_step);
  1319. spin[i]->set_hide_slider(p_no_slider);
  1320. spin[i]->set_allow_greater(true);
  1321. spin[i]->set_allow_lesser(true);
  1322. }
  1323. }
  1324. EditorPropertyPlane::EditorPropertyPlane() {
  1325. bool horizontal = EDITOR_GET("interface/inspector/horizontal_vector_types_editing");
  1326. BoxContainer *bc;
  1327. if (horizontal) {
  1328. bc = memnew(HBoxContainer);
  1329. add_child(bc);
  1330. set_bottom_editor(bc);
  1331. } else {
  1332. bc = memnew(VBoxContainer);
  1333. add_child(bc);
  1334. }
  1335. static const char *desc[4] = { "x", "y", "z", "d" };
  1336. for (int i = 0; i < 4; i++) {
  1337. spin[i] = memnew(EditorSpinSlider);
  1338. spin[i]->set_flat(true);
  1339. spin[i]->set_label(desc[i]);
  1340. bc->add_child(spin[i]);
  1341. add_focusable(spin[i]);
  1342. spin[i]->connect("value_changed", this, "_value_changed", varray(desc[i]));
  1343. if (horizontal) {
  1344. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1345. }
  1346. }
  1347. if (!horizontal) {
  1348. set_label_reference(spin[0]); //show text and buttons around this
  1349. }
  1350. setting = false;
  1351. }
  1352. ///////////////////// QUAT /////////////////////////
  1353. void EditorPropertyQuat::_value_changed(double val, const String &p_name) {
  1354. if (setting) {
  1355. return;
  1356. }
  1357. Quat p;
  1358. p.x = spin[0]->get_value();
  1359. p.y = spin[1]->get_value();
  1360. p.z = spin[2]->get_value();
  1361. p.w = spin[3]->get_value();
  1362. emit_changed(get_edited_property(), p, p_name);
  1363. }
  1364. void EditorPropertyQuat::update_property() {
  1365. Quat val = get_edited_object()->get(get_edited_property());
  1366. setting = true;
  1367. spin[0]->set_value(val.x);
  1368. spin[1]->set_value(val.y);
  1369. spin[2]->set_value(val.z);
  1370. spin[3]->set_value(val.w);
  1371. setting = false;
  1372. }
  1373. void EditorPropertyQuat::_notification(int p_what) {
  1374. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1375. Color base = get_color("accent_color", "Editor");
  1376. for (int i = 0; i < 3; i++) {
  1377. Color c = base;
  1378. c.set_hsv(float(i) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1379. spin[i]->set_custom_label_color(true, c);
  1380. }
  1381. }
  1382. }
  1383. void EditorPropertyQuat::_bind_methods() {
  1384. ClassDB::bind_method(D_METHOD("_value_changed"), &EditorPropertyQuat::_value_changed);
  1385. }
  1386. void EditorPropertyQuat::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1387. for (int i = 0; i < 4; i++) {
  1388. spin[i]->set_min(p_min);
  1389. spin[i]->set_max(p_max);
  1390. spin[i]->set_step(p_step);
  1391. spin[i]->set_hide_slider(p_no_slider);
  1392. spin[i]->set_allow_greater(true);
  1393. spin[i]->set_allow_lesser(true);
  1394. }
  1395. }
  1396. EditorPropertyQuat::EditorPropertyQuat() {
  1397. bool horizontal = EDITOR_GET("interface/inspector/horizontal_vector_types_editing");
  1398. BoxContainer *bc;
  1399. if (horizontal) {
  1400. bc = memnew(HBoxContainer);
  1401. add_child(bc);
  1402. set_bottom_editor(bc);
  1403. } else {
  1404. bc = memnew(VBoxContainer);
  1405. add_child(bc);
  1406. }
  1407. static const char *desc[4] = { "x", "y", "z", "w" };
  1408. for (int i = 0; i < 4; i++) {
  1409. spin[i] = memnew(EditorSpinSlider);
  1410. spin[i]->set_flat(true);
  1411. spin[i]->set_label(desc[i]);
  1412. bc->add_child(spin[i]);
  1413. add_focusable(spin[i]);
  1414. spin[i]->connect("value_changed", this, "_value_changed", varray(desc[i]));
  1415. if (horizontal) {
  1416. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1417. }
  1418. }
  1419. if (!horizontal) {
  1420. set_label_reference(spin[0]); //show text and buttons around this
  1421. }
  1422. setting = false;
  1423. }
  1424. ///////////////////// AABB /////////////////////////
  1425. void EditorPropertyAABB::_value_changed(double val, const String &p_name) {
  1426. if (setting) {
  1427. return;
  1428. }
  1429. AABB p;
  1430. p.position.x = spin[0]->get_value();
  1431. p.position.y = spin[1]->get_value();
  1432. p.position.z = spin[2]->get_value();
  1433. p.size.x = spin[3]->get_value();
  1434. p.size.y = spin[4]->get_value();
  1435. p.size.z = spin[5]->get_value();
  1436. emit_changed(get_edited_property(), p, p_name);
  1437. }
  1438. void EditorPropertyAABB::update_property() {
  1439. AABB val = get_edited_object()->get(get_edited_property());
  1440. setting = true;
  1441. spin[0]->set_value(val.position.x);
  1442. spin[1]->set_value(val.position.y);
  1443. spin[2]->set_value(val.position.z);
  1444. spin[3]->set_value(val.size.x);
  1445. spin[4]->set_value(val.size.y);
  1446. spin[5]->set_value(val.size.z);
  1447. setting = false;
  1448. }
  1449. void EditorPropertyAABB::_notification(int p_what) {
  1450. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1451. Color base = get_color("accent_color", "Editor");
  1452. for (int i = 0; i < 6; i++) {
  1453. Color c = base;
  1454. c.set_hsv(float(i % 3) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1455. spin[i]->set_custom_label_color(true, c);
  1456. }
  1457. }
  1458. }
  1459. void EditorPropertyAABB::_bind_methods() {
  1460. ClassDB::bind_method(D_METHOD("_value_changed"), &EditorPropertyAABB::_value_changed);
  1461. }
  1462. void EditorPropertyAABB::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1463. for (int i = 0; i < 6; i++) {
  1464. spin[i]->set_min(p_min);
  1465. spin[i]->set_max(p_max);
  1466. spin[i]->set_step(p_step);
  1467. spin[i]->set_hide_slider(p_no_slider);
  1468. spin[i]->set_allow_greater(true);
  1469. spin[i]->set_allow_lesser(true);
  1470. }
  1471. }
  1472. EditorPropertyAABB::EditorPropertyAABB() {
  1473. GridContainer *g = memnew(GridContainer);
  1474. g->set_columns(3);
  1475. add_child(g);
  1476. static const char *desc[6] = { "x", "y", "z", "w", "h", "d" };
  1477. for (int i = 0; i < 6; i++) {
  1478. spin[i] = memnew(EditorSpinSlider);
  1479. spin[i]->set_label(desc[i]);
  1480. spin[i]->set_flat(true);
  1481. g->add_child(spin[i]);
  1482. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1483. add_focusable(spin[i]);
  1484. spin[i]->connect("value_changed", this, "_value_changed", varray(desc[i]));
  1485. }
  1486. set_bottom_editor(g);
  1487. setting = false;
  1488. }
  1489. ///////////////////// TRANSFORM2D /////////////////////////
  1490. void EditorPropertyTransform2D::_value_changed(double val, const String &p_name) {
  1491. if (setting) {
  1492. return;
  1493. }
  1494. Transform2D p;
  1495. p[0][0] = spin[0]->get_value();
  1496. p[0][1] = spin[1]->get_value();
  1497. p[1][0] = spin[2]->get_value();
  1498. p[1][1] = spin[3]->get_value();
  1499. p[2][0] = spin[4]->get_value();
  1500. p[2][1] = spin[5]->get_value();
  1501. emit_changed(get_edited_property(), p, p_name);
  1502. }
  1503. void EditorPropertyTransform2D::update_property() {
  1504. Transform2D val = get_edited_object()->get(get_edited_property());
  1505. setting = true;
  1506. spin[0]->set_value(val[0][0]);
  1507. spin[1]->set_value(val[0][1]);
  1508. spin[2]->set_value(val[1][0]);
  1509. spin[3]->set_value(val[1][1]);
  1510. spin[4]->set_value(val[2][0]);
  1511. spin[5]->set_value(val[2][1]);
  1512. setting = false;
  1513. }
  1514. void EditorPropertyTransform2D::_notification(int p_what) {
  1515. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1516. Color base = get_color("accent_color", "Editor");
  1517. for (int i = 0; i < 6; i++) {
  1518. Color c = base;
  1519. c.set_hsv(float(i % 2) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1520. spin[i]->set_custom_label_color(true, c);
  1521. }
  1522. }
  1523. }
  1524. void EditorPropertyTransform2D::_bind_methods() {
  1525. ClassDB::bind_method(D_METHOD("_value_changed"), &EditorPropertyTransform2D::_value_changed);
  1526. }
  1527. void EditorPropertyTransform2D::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1528. for (int i = 0; i < 6; i++) {
  1529. spin[i]->set_min(p_min);
  1530. spin[i]->set_max(p_max);
  1531. spin[i]->set_step(p_step);
  1532. spin[i]->set_hide_slider(p_no_slider);
  1533. spin[i]->set_allow_greater(true);
  1534. spin[i]->set_allow_lesser(true);
  1535. }
  1536. }
  1537. EditorPropertyTransform2D::EditorPropertyTransform2D() {
  1538. GridContainer *g = memnew(GridContainer);
  1539. g->set_columns(2);
  1540. add_child(g);
  1541. static const char *desc[6] = { "x", "y", "x", "y", "x", "y" };
  1542. for (int i = 0; i < 6; i++) {
  1543. spin[i] = memnew(EditorSpinSlider);
  1544. spin[i]->set_label(desc[i]);
  1545. spin[i]->set_flat(true);
  1546. g->add_child(spin[i]);
  1547. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1548. add_focusable(spin[i]);
  1549. spin[i]->connect("value_changed", this, "_value_changed", varray(desc[i]));
  1550. }
  1551. set_bottom_editor(g);
  1552. setting = false;
  1553. }
  1554. ///////////////////// BASIS /////////////////////////
  1555. void EditorPropertyBasis::_value_changed(double val, const String &p_name) {
  1556. if (setting) {
  1557. return;
  1558. }
  1559. Basis p;
  1560. p[0][0] = spin[0]->get_value();
  1561. p[1][0] = spin[1]->get_value();
  1562. p[2][0] = spin[2]->get_value();
  1563. p[0][1] = spin[3]->get_value();
  1564. p[1][1] = spin[4]->get_value();
  1565. p[2][1] = spin[5]->get_value();
  1566. p[0][2] = spin[6]->get_value();
  1567. p[1][2] = spin[7]->get_value();
  1568. p[2][2] = spin[8]->get_value();
  1569. emit_changed(get_edited_property(), p, p_name);
  1570. }
  1571. void EditorPropertyBasis::update_property() {
  1572. Basis val = get_edited_object()->get(get_edited_property());
  1573. setting = true;
  1574. spin[0]->set_value(val[0][0]);
  1575. spin[1]->set_value(val[1][0]);
  1576. spin[2]->set_value(val[2][0]);
  1577. spin[3]->set_value(val[0][1]);
  1578. spin[4]->set_value(val[1][1]);
  1579. spin[5]->set_value(val[2][1]);
  1580. spin[6]->set_value(val[0][2]);
  1581. spin[7]->set_value(val[1][2]);
  1582. spin[8]->set_value(val[2][2]);
  1583. setting = false;
  1584. }
  1585. void EditorPropertyBasis::_notification(int p_what) {
  1586. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1587. Color base = get_color("accent_color", "Editor");
  1588. for (int i = 0; i < 9; i++) {
  1589. Color c = base;
  1590. c.set_hsv(float(i % 3) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1591. spin[i]->set_custom_label_color(true, c);
  1592. }
  1593. }
  1594. }
  1595. void EditorPropertyBasis::_bind_methods() {
  1596. ClassDB::bind_method(D_METHOD("_value_changed"), &EditorPropertyBasis::_value_changed);
  1597. }
  1598. void EditorPropertyBasis::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1599. for (int i = 0; i < 9; i++) {
  1600. spin[i]->set_min(p_min);
  1601. spin[i]->set_max(p_max);
  1602. spin[i]->set_step(p_step);
  1603. spin[i]->set_hide_slider(p_no_slider);
  1604. spin[i]->set_allow_greater(true);
  1605. spin[i]->set_allow_lesser(true);
  1606. }
  1607. }
  1608. EditorPropertyBasis::EditorPropertyBasis() {
  1609. GridContainer *g = memnew(GridContainer);
  1610. g->set_columns(3);
  1611. add_child(g);
  1612. static const char *desc[9] = { "x", "y", "z", "x", "y", "z", "x", "y", "z" };
  1613. for (int i = 0; i < 9; i++) {
  1614. spin[i] = memnew(EditorSpinSlider);
  1615. spin[i]->set_label(desc[i]);
  1616. spin[i]->set_flat(true);
  1617. g->add_child(spin[i]);
  1618. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1619. add_focusable(spin[i]);
  1620. spin[i]->connect("value_changed", this, "_value_changed", varray(desc[i]));
  1621. }
  1622. set_bottom_editor(g);
  1623. setting = false;
  1624. }
  1625. ///////////////////// TRANSFORM /////////////////////////
  1626. void EditorPropertyTransform::_value_changed(double val, const String &p_name) {
  1627. if (setting) {
  1628. return;
  1629. }
  1630. Transform p;
  1631. p.basis[0][0] = spin[0]->get_value();
  1632. p.basis[1][0] = spin[1]->get_value();
  1633. p.basis[2][0] = spin[2]->get_value();
  1634. p.basis[0][1] = spin[3]->get_value();
  1635. p.basis[1][1] = spin[4]->get_value();
  1636. p.basis[2][1] = spin[5]->get_value();
  1637. p.basis[0][2] = spin[6]->get_value();
  1638. p.basis[1][2] = spin[7]->get_value();
  1639. p.basis[2][2] = spin[8]->get_value();
  1640. p.origin[0] = spin[9]->get_value();
  1641. p.origin[1] = spin[10]->get_value();
  1642. p.origin[2] = spin[11]->get_value();
  1643. emit_changed(get_edited_property(), p, p_name);
  1644. }
  1645. void EditorPropertyTransform::update_property() {
  1646. Transform val = get_edited_object()->get(get_edited_property());
  1647. setting = true;
  1648. spin[0]->set_value(val.basis[0][0]);
  1649. spin[1]->set_value(val.basis[1][0]);
  1650. spin[2]->set_value(val.basis[2][0]);
  1651. spin[3]->set_value(val.basis[0][1]);
  1652. spin[4]->set_value(val.basis[1][1]);
  1653. spin[5]->set_value(val.basis[2][1]);
  1654. spin[6]->set_value(val.basis[0][2]);
  1655. spin[7]->set_value(val.basis[1][2]);
  1656. spin[8]->set_value(val.basis[2][2]);
  1657. spin[9]->set_value(val.origin[0]);
  1658. spin[10]->set_value(val.origin[1]);
  1659. spin[11]->set_value(val.origin[2]);
  1660. setting = false;
  1661. }
  1662. void EditorPropertyTransform::_notification(int p_what) {
  1663. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1664. Color base = get_color("accent_color", "Editor");
  1665. for (int i = 0; i < 12; i++) {
  1666. Color c = base;
  1667. c.set_hsv(float(i % 3) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1668. spin[i]->set_custom_label_color(true, c);
  1669. }
  1670. }
  1671. }
  1672. void EditorPropertyTransform::_bind_methods() {
  1673. ClassDB::bind_method(D_METHOD("_value_changed"), &EditorPropertyTransform::_value_changed);
  1674. }
  1675. void EditorPropertyTransform::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1676. for (int i = 0; i < 12; i++) {
  1677. spin[i]->set_min(p_min);
  1678. spin[i]->set_max(p_max);
  1679. spin[i]->set_step(p_step);
  1680. spin[i]->set_hide_slider(p_no_slider);
  1681. spin[i]->set_allow_greater(true);
  1682. spin[i]->set_allow_lesser(true);
  1683. }
  1684. }
  1685. EditorPropertyTransform::EditorPropertyTransform() {
  1686. GridContainer *g = memnew(GridContainer);
  1687. g->set_columns(3);
  1688. add_child(g);
  1689. static const char *desc[12] = { "x", "y", "z", "x", "y", "z", "x", "y", "z", "x", "y", "z" };
  1690. for (int i = 0; i < 12; i++) {
  1691. spin[i] = memnew(EditorSpinSlider);
  1692. spin[i]->set_label(desc[i]);
  1693. spin[i]->set_flat(true);
  1694. g->add_child(spin[i]);
  1695. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1696. add_focusable(spin[i]);
  1697. spin[i]->connect("value_changed", this, "_value_changed", varray(desc[i]));
  1698. }
  1699. set_bottom_editor(g);
  1700. setting = false;
  1701. }
  1702. ////////////// COLOR PICKER //////////////////////
  1703. void EditorPropertyColor::_color_changed(const Color &p_color) {
  1704. // Cancel the color change if the current color is identical to the new one.
  1705. if (get_edited_object()->get(get_edited_property()) == p_color) {
  1706. return;
  1707. }
  1708. emit_changed(get_edited_property(), p_color, "", true);
  1709. }
  1710. void EditorPropertyColor::_popup_closed() {
  1711. if (picker->get_pick_color() != last_color) {
  1712. emit_changed(get_edited_property(), picker->get_pick_color(), "", false);
  1713. }
  1714. }
  1715. void EditorPropertyColor::_picker_created() {
  1716. // get default color picker mode from editor settings
  1717. int default_color_mode = EDITOR_GET("interface/inspector/default_color_picker_mode");
  1718. if (default_color_mode == 1) {
  1719. picker->get_picker()->set_hsv_mode(true);
  1720. } else if (default_color_mode == 2) {
  1721. picker->get_picker()->set_raw_mode(true);
  1722. }
  1723. }
  1724. void EditorPropertyColor::_picker_opening() {
  1725. last_color = picker->get_pick_color();
  1726. }
  1727. void EditorPropertyColor::_bind_methods() {
  1728. ClassDB::bind_method(D_METHOD("_color_changed"), &EditorPropertyColor::_color_changed);
  1729. ClassDB::bind_method(D_METHOD("_popup_closed"), &EditorPropertyColor::_popup_closed);
  1730. ClassDB::bind_method(D_METHOD("_picker_created"), &EditorPropertyColor::_picker_created);
  1731. ClassDB::bind_method(D_METHOD("_picker_opening"), &EditorPropertyColor::_picker_opening);
  1732. }
  1733. void EditorPropertyColor::update_property() {
  1734. picker->set_pick_color(get_edited_object()->get(get_edited_property()));
  1735. const Color color = picker->get_pick_color();
  1736. // Add a tooltip to display each channel's values without having to click the ColorPickerButton
  1737. if (picker->is_editing_alpha()) {
  1738. picker->set_tooltip(vformat(
  1739. "R: %s\nG: %s\nB: %s\nA: %s",
  1740. rtos(color.r).pad_decimals(2),
  1741. rtos(color.g).pad_decimals(2),
  1742. rtos(color.b).pad_decimals(2),
  1743. rtos(color.a).pad_decimals(2)));
  1744. } else {
  1745. picker->set_tooltip(vformat(
  1746. "R: %s\nG: %s\nB: %s",
  1747. rtos(color.r).pad_decimals(2),
  1748. rtos(color.g).pad_decimals(2),
  1749. rtos(color.b).pad_decimals(2)));
  1750. }
  1751. }
  1752. void EditorPropertyColor::setup(bool p_show_alpha) {
  1753. picker->set_edit_alpha(p_show_alpha);
  1754. }
  1755. EditorPropertyColor::EditorPropertyColor() {
  1756. picker = memnew(ColorPickerButton);
  1757. add_child(picker);
  1758. picker->set_flat(true);
  1759. picker->connect("color_changed", this, "_color_changed");
  1760. picker->connect("popup_closed", this, "_popup_closed");
  1761. picker->connect("picker_created", this, "_picker_created");
  1762. picker->get_popup()->connect("about_to_show", this, "_picker_opening");
  1763. }
  1764. ////////////// NODE PATH //////////////////////
  1765. void EditorPropertyNodePath::_node_selected(const NodePath &p_path) {
  1766. NodePath path = p_path;
  1767. Node *base_node = nullptr;
  1768. if (!use_path_from_scene_root) {
  1769. base_node = Object::cast_to<Node>(get_edited_object());
  1770. if (!base_node) {
  1771. //try a base node within history
  1772. if (EditorNode::get_singleton()->get_editor_history()->get_path_size() > 0) {
  1773. Object *base = ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_history()->get_path_object(0));
  1774. if (base) {
  1775. base_node = Object::cast_to<Node>(base);
  1776. }
  1777. }
  1778. }
  1779. }
  1780. if (!base_node && get_edited_object()->has_method("get_root_path")) {
  1781. base_node = get_edited_object()->call("get_root_path");
  1782. }
  1783. if (!base_node && Object::cast_to<Reference>(get_edited_object())) {
  1784. Node *to_node = get_node(p_path);
  1785. ERR_FAIL_COND(!to_node);
  1786. path = get_tree()->get_edited_scene_root()->get_path_to(to_node);
  1787. }
  1788. if (base_node) { // for AnimationTrackKeyEdit
  1789. path = base_node->get_path().rel_path_to(p_path);
  1790. }
  1791. emit_changed(get_edited_property(), path);
  1792. update_property();
  1793. }
  1794. void EditorPropertyNodePath::_node_assign() {
  1795. if (!scene_tree) {
  1796. scene_tree = memnew(SceneTreeDialog);
  1797. scene_tree->get_scene_tree()->set_show_enabled_subscene(true);
  1798. scene_tree->get_scene_tree()->set_valid_types(valid_types);
  1799. add_child(scene_tree);
  1800. scene_tree->connect("selected", this, "_node_selected");
  1801. }
  1802. scene_tree->popup_centered_ratio();
  1803. }
  1804. void EditorPropertyNodePath::_node_clear() {
  1805. emit_changed(get_edited_property(), NodePath());
  1806. update_property();
  1807. }
  1808. void EditorPropertyNodePath::update_property() {
  1809. NodePath p = get_edited_object()->get(get_edited_property());
  1810. assign->set_tooltip(p);
  1811. if (p == NodePath()) {
  1812. assign->set_icon(Ref<Texture>());
  1813. assign->set_text(TTR("Assign..."));
  1814. assign->set_flat(false);
  1815. return;
  1816. }
  1817. assign->set_flat(true);
  1818. Node *base_node = nullptr;
  1819. if (base_hint != NodePath()) {
  1820. if (get_tree()->get_root()->has_node(base_hint)) {
  1821. base_node = get_tree()->get_root()->get_node(base_hint);
  1822. }
  1823. } else {
  1824. base_node = Object::cast_to<Node>(get_edited_object());
  1825. }
  1826. if (!base_node || !base_node->has_node(p)) {
  1827. assign->set_icon(Ref<Texture>());
  1828. assign->set_text(p);
  1829. return;
  1830. }
  1831. Node *target_node = base_node->get_node(p);
  1832. ERR_FAIL_COND(!target_node);
  1833. if (String(target_node->get_name()).find("@") != -1) {
  1834. assign->set_icon(Ref<Texture>());
  1835. assign->set_text(p);
  1836. return;
  1837. }
  1838. assign->set_text(target_node->get_name());
  1839. assign->set_icon(EditorNode::get_singleton()->get_object_icon(target_node, "Node"));
  1840. }
  1841. void EditorPropertyNodePath::setup(const NodePath &p_base_hint, Vector<StringName> p_valid_types, bool p_use_path_from_scene_root) {
  1842. base_hint = p_base_hint;
  1843. valid_types = p_valid_types;
  1844. use_path_from_scene_root = p_use_path_from_scene_root;
  1845. }
  1846. void EditorPropertyNodePath::_notification(int p_what) {
  1847. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1848. Ref<Texture> t = get_icon("Clear", "EditorIcons");
  1849. clear->set_icon(t);
  1850. }
  1851. }
  1852. void EditorPropertyNodePath::_bind_methods() {
  1853. ClassDB::bind_method(D_METHOD("_node_selected"), &EditorPropertyNodePath::_node_selected);
  1854. ClassDB::bind_method(D_METHOD("_node_assign"), &EditorPropertyNodePath::_node_assign);
  1855. ClassDB::bind_method(D_METHOD("_node_clear"), &EditorPropertyNodePath::_node_clear);
  1856. }
  1857. EditorPropertyNodePath::EditorPropertyNodePath() {
  1858. HBoxContainer *hbc = memnew(HBoxContainer);
  1859. add_child(hbc);
  1860. assign = memnew(Button);
  1861. assign->set_flat(true);
  1862. assign->set_h_size_flags(SIZE_EXPAND_FILL);
  1863. assign->set_clip_text(true);
  1864. assign->connect("pressed", this, "_node_assign");
  1865. hbc->add_child(assign);
  1866. clear = memnew(Button);
  1867. clear->set_flat(true);
  1868. clear->connect("pressed", this, "_node_clear");
  1869. hbc->add_child(clear);
  1870. use_path_from_scene_root = false;
  1871. scene_tree = nullptr; //do not allocate unnecessarily
  1872. }
  1873. ///////////////////// RID /////////////////////////
  1874. void EditorPropertyRID::update_property() {
  1875. RID rid = get_edited_object()->get(get_edited_property());
  1876. if (rid.is_valid()) {
  1877. int id = rid.get_id();
  1878. label->set_text("RID: " + itos(id));
  1879. } else {
  1880. label->set_text(TTR("Invalid RID"));
  1881. }
  1882. }
  1883. EditorPropertyRID::EditorPropertyRID() {
  1884. label = memnew(Label);
  1885. add_child(label);
  1886. }
  1887. ////////////// RESOURCE //////////////////////
  1888. void EditorPropertyResource::_resource_selected(const RES &p_resource, bool p_edit) {
  1889. if (!p_edit && _can_use_sub_inspector(p_resource)) {
  1890. bool unfold = !get_edited_object()->editor_is_section_unfolded(get_edited_property());
  1891. get_edited_object()->editor_set_section_unfold(get_edited_property(), unfold);
  1892. update_property();
  1893. } else {
  1894. emit_signal("resource_selected", get_edited_property(), p_resource);
  1895. }
  1896. }
  1897. void EditorPropertyResource::_resource_changed(const RES &p_resource) {
  1898. // Make visual script the correct type.
  1899. Ref<Script> s = p_resource;
  1900. if (get_edited_object() && s.is_valid()) {
  1901. s->call("set_instance_base_type", get_edited_object()->get_class());
  1902. }
  1903. // Prevent the creation of invalid ViewportTextures when possible.
  1904. Ref<ViewportTexture> vpt = p_resource;
  1905. if (vpt.is_valid()) {
  1906. Resource *r = Object::cast_to<Resource>(get_edited_object());
  1907. if (r && r->get_path().is_resource_file()) {
  1908. EditorNode::get_singleton()->show_warning(TTR("Can't create a ViewportTexture on resources saved as a file.\nResource needs to belong to a scene."));
  1909. emit_changed(get_edited_property(), RES());
  1910. update_property();
  1911. return;
  1912. }
  1913. if (r && !r->is_local_to_scene()) {
  1914. EditorNode::get_singleton()->show_warning(TTR("Can't create a ViewportTexture on this resource because it's not set as local to scene.\nPlease switch on the 'local to scene' property on it (and all resources containing it up to a node)."));
  1915. emit_changed(get_edited_property(), RES());
  1916. update_property();
  1917. return;
  1918. }
  1919. }
  1920. emit_changed(get_edited_property(), p_resource);
  1921. update_property();
  1922. // Automatically suggest setting up the path for a ViewportTexture.
  1923. if (vpt.is_valid() && vpt->get_viewport_path_in_scene().is_empty()) {
  1924. if (!scene_tree) {
  1925. scene_tree = memnew(SceneTreeDialog);
  1926. scene_tree->set_title(TTR("Pick a Viewport"));
  1927. Vector<StringName> valid_types;
  1928. valid_types.push_back("Viewport");
  1929. scene_tree->get_scene_tree()->set_valid_types(valid_types);
  1930. scene_tree->get_scene_tree()->set_show_enabled_subscene(true);
  1931. add_child(scene_tree);
  1932. scene_tree->connect("selected", this, "_viewport_selected");
  1933. }
  1934. scene_tree->popup_centered_ratio();
  1935. }
  1936. }
  1937. void EditorPropertyResource::_sub_inspector_property_keyed(const String &p_property, const Variant &p_value, bool) {
  1938. emit_signal("property_keyed_with_value", String(get_edited_property()) + ":" + p_property, p_value, false);
  1939. }
  1940. void EditorPropertyResource::_sub_inspector_resource_selected(const RES &p_resource, const String &p_property) {
  1941. emit_signal("resource_selected", String(get_edited_property()) + ":" + p_property, p_resource);
  1942. }
  1943. void EditorPropertyResource::_sub_inspector_object_id_selected(int p_id) {
  1944. emit_signal("object_id_selected", get_edited_property(), p_id);
  1945. }
  1946. void EditorPropertyResource::_open_editor_pressed() {
  1947. RES res = get_edited_object()->get(get_edited_property());
  1948. if (res.is_valid()) {
  1949. // May clear the editor so do it deferred.
  1950. EditorNode::get_singleton()->call_deferred("edit_item_resource", res);
  1951. }
  1952. }
  1953. void EditorPropertyResource::_fold_other_editors(Object *p_self) {
  1954. if (this == p_self) {
  1955. return;
  1956. }
  1957. RES res = get_edited_object()->get(get_edited_property());
  1958. if (!res.is_valid()) {
  1959. return;
  1960. }
  1961. bool use_editor = false;
  1962. for (int i = 0; i < EditorNode::get_editor_data().get_editor_plugin_count(); i++) {
  1963. EditorPlugin *ep = EditorNode::get_editor_data().get_editor_plugin(i);
  1964. if (ep->handles(res.ptr())) {
  1965. use_editor = true;
  1966. }
  1967. }
  1968. if (!use_editor) {
  1969. return;
  1970. }
  1971. opened_editor = false;
  1972. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  1973. if (unfolded) {
  1974. // Refold.
  1975. resource_picker->set_toggle_pressed(false);
  1976. get_edited_object()->editor_set_section_unfold(get_edited_property(), false);
  1977. update_property();
  1978. }
  1979. }
  1980. void EditorPropertyResource::_update_property_bg() {
  1981. if (!is_inside_tree()) {
  1982. return;
  1983. }
  1984. updating_theme = true;
  1985. if (sub_inspector != nullptr) {
  1986. int count_subinspectors = 0;
  1987. Node *n = get_parent();
  1988. while (n) {
  1989. EditorInspector *ei = Object::cast_to<EditorInspector>(n);
  1990. if (ei && ei->is_sub_inspector()) {
  1991. count_subinspectors++;
  1992. }
  1993. n = n->get_parent();
  1994. }
  1995. count_subinspectors = MIN(15, count_subinspectors);
  1996. add_color_override("property_color", get_color("sub_inspector_property_color", "Editor"));
  1997. add_style_override("bg_selected", get_stylebox("sub_inspector_property_bg_selected" + itos(count_subinspectors), "Editor"));
  1998. add_style_override("bg", get_stylebox("sub_inspector_property_bg" + itos(count_subinspectors), "Editor"));
  1999. add_constant_override("font_offset", get_constant("sub_inspector_font_offset", "Editor"));
  2000. add_constant_override("vseparation", 0);
  2001. } else {
  2002. add_color_override("property_color", get_color("property_color", "EditorProperty"));
  2003. add_style_override("bg_selected", get_stylebox("bg_selected", "EditorProperty"));
  2004. add_style_override("bg", get_stylebox("bg", "EditorProperty"));
  2005. add_constant_override("vseparation", get_constant("vseparation", "EditorProperty"));
  2006. add_constant_override("font_offset", get_constant("font_offset", "EditorProperty"));
  2007. }
  2008. updating_theme = false;
  2009. update();
  2010. }
  2011. void EditorPropertyResource::_viewport_selected(const NodePath &p_path) {
  2012. Node *to_node = get_node(p_path);
  2013. if (!Object::cast_to<Viewport>(to_node)) {
  2014. EditorNode::get_singleton()->show_warning(TTR("Selected node is not a Viewport!"));
  2015. return;
  2016. }
  2017. Ref<ViewportTexture> vt;
  2018. vt.instance();
  2019. vt->set_viewport_path_in_scene(get_tree()->get_edited_scene_root()->get_path_to(to_node));
  2020. vt->setup_local_to_scene();
  2021. emit_changed(get_edited_property(), vt);
  2022. update_property();
  2023. }
  2024. bool EditorPropertyResource::_can_use_sub_inspector(const RES &p_resource) {
  2025. bool use_editor = false;
  2026. if (p_resource.is_valid()) {
  2027. for (int i = 0; i < EditorNode::get_editor_data().get_editor_plugin_count(); i++) {
  2028. EditorPlugin *ep = EditorNode::get_editor_data().get_editor_plugin(i);
  2029. if (ep->handles((Resource *)p_resource.ptr())) {
  2030. use_editor = true;
  2031. }
  2032. }
  2033. }
  2034. return !use_editor && use_sub_inspector;
  2035. }
  2036. void EditorPropertyResource::setup(Object *p_object, const String &p_path, const String &p_base_type) {
  2037. if (resource_picker) {
  2038. resource_picker->disconnect("resource_selected", this, "_resource_selected");
  2039. resource_picker->disconnect("resource_changed", this, "_resource_changed");
  2040. memdelete(resource_picker);
  2041. }
  2042. if (p_path == "script" && p_base_type == "Script" && Object::cast_to<Node>(p_object)) {
  2043. EditorScriptPicker *script_picker = memnew(EditorScriptPicker);
  2044. script_picker->set_script_owner(Object::cast_to<Node>(p_object));
  2045. resource_picker = script_picker;
  2046. } else {
  2047. resource_picker = memnew(EditorResourcePicker);
  2048. }
  2049. resource_picker->set_base_type(p_base_type);
  2050. resource_picker->set_editable(true);
  2051. resource_picker->set_h_size_flags(SIZE_EXPAND_FILL);
  2052. add_child(resource_picker);
  2053. resource_picker->connect("resource_selected", this, "_resource_selected");
  2054. resource_picker->connect("resource_changed", this, "_resource_changed");
  2055. for (int i = 0; i < resource_picker->get_child_count(); i++) {
  2056. Button *b = Object::cast_to<Button>(resource_picker->get_child(i));
  2057. if (b) {
  2058. add_focusable(b);
  2059. }
  2060. }
  2061. }
  2062. void EditorPropertyResource::update_property() {
  2063. RES res = get_edited_object()->get(get_edited_property());
  2064. if (_can_use_sub_inspector(res)) {
  2065. if (res.is_valid() != resource_picker->is_toggle_mode()) {
  2066. resource_picker->set_toggle_mode(res.is_valid());
  2067. }
  2068. if (res.is_valid() && get_edited_object()->editor_is_section_unfolded(get_edited_property())) {
  2069. if (!sub_inspector) {
  2070. sub_inspector = memnew(EditorInspector);
  2071. sub_inspector->set_enable_v_scroll(false);
  2072. sub_inspector->set_use_doc_hints(true);
  2073. sub_inspector->set_sub_inspector(true);
  2074. sub_inspector->set_enable_capitalize_paths(bool(EDITOR_GET("interface/inspector/capitalize_properties")));
  2075. sub_inspector->connect("property_keyed", this, "_sub_inspector_property_keyed");
  2076. sub_inspector->connect("resource_selected", this, "_sub_inspector_resource_selected");
  2077. sub_inspector->connect("object_id_selected", this, "_sub_inspector_object_id_selected");
  2078. sub_inspector->set_keying(is_keying());
  2079. sub_inspector->set_read_only(is_read_only());
  2080. sub_inspector->set_use_folding(is_using_folding());
  2081. sub_inspector->set_undo_redo(EditorNode::get_undo_redo());
  2082. sub_inspector_vbox = memnew(VBoxContainer);
  2083. add_child(sub_inspector_vbox);
  2084. set_bottom_editor(sub_inspector_vbox);
  2085. sub_inspector_vbox->add_child(sub_inspector);
  2086. resource_picker->set_toggle_pressed(true);
  2087. _update_property_bg();
  2088. }
  2089. if (res.ptr() != sub_inspector->get_edited_object()) {
  2090. sub_inspector->edit(res.ptr());
  2091. }
  2092. sub_inspector->refresh();
  2093. } else {
  2094. if (sub_inspector) {
  2095. set_bottom_editor(nullptr);
  2096. memdelete(sub_inspector_vbox);
  2097. sub_inspector = nullptr;
  2098. sub_inspector_vbox = nullptr;
  2099. if (opened_editor) {
  2100. EditorNode::get_singleton()->hide_top_editors();
  2101. opened_editor = false;
  2102. }
  2103. _update_property_bg();
  2104. }
  2105. }
  2106. }
  2107. resource_picker->set_edited_resource(res);
  2108. }
  2109. void EditorPropertyResource::collapse_all_folding() {
  2110. if (sub_inspector) {
  2111. sub_inspector->collapse_all_folding();
  2112. }
  2113. }
  2114. void EditorPropertyResource::expand_all_folding() {
  2115. if (sub_inspector) {
  2116. sub_inspector->expand_all_folding();
  2117. }
  2118. }
  2119. void EditorPropertyResource::set_use_sub_inspector(bool p_enable) {
  2120. use_sub_inspector = p_enable;
  2121. }
  2122. void EditorPropertyResource::_notification(int p_what) {
  2123. switch (p_what) {
  2124. case NOTIFICATION_ENTER_TREE:
  2125. case NOTIFICATION_THEME_CHANGED: {
  2126. if (!updating_theme) {
  2127. _update_property_bg();
  2128. }
  2129. } break;
  2130. }
  2131. }
  2132. void EditorPropertyResource::_bind_methods() {
  2133. ClassDB::bind_method(D_METHOD("_resource_selected"), &EditorPropertyResource::_resource_selected);
  2134. ClassDB::bind_method(D_METHOD("_resource_changed"), &EditorPropertyResource::_resource_changed);
  2135. ClassDB::bind_method(D_METHOD("_viewport_selected"), &EditorPropertyResource::_viewport_selected);
  2136. ClassDB::bind_method(D_METHOD("_sub_inspector_property_keyed"), &EditorPropertyResource::_sub_inspector_property_keyed);
  2137. ClassDB::bind_method(D_METHOD("_sub_inspector_resource_selected"), &EditorPropertyResource::_sub_inspector_resource_selected);
  2138. ClassDB::bind_method(D_METHOD("_sub_inspector_object_id_selected"), &EditorPropertyResource::_sub_inspector_object_id_selected);
  2139. ClassDB::bind_method(D_METHOD("_open_editor_pressed"), &EditorPropertyResource::_open_editor_pressed);
  2140. ClassDB::bind_method(D_METHOD("_fold_other_editors"), &EditorPropertyResource::_fold_other_editors);
  2141. }
  2142. EditorPropertyResource::EditorPropertyResource() {
  2143. use_sub_inspector = bool(EDITOR_GET("interface/inspector/open_resources_in_current_inspector"));
  2144. add_to_group("_editor_resource_properties");
  2145. }
  2146. ////////////// DEFAULT PLUGIN //////////////////////
  2147. bool EditorInspectorDefaultPlugin::can_handle(Object *p_object) {
  2148. return true; //can handle everything
  2149. }
  2150. void EditorInspectorDefaultPlugin::parse_begin(Object *p_object) {
  2151. //do none
  2152. }
  2153. bool EditorInspectorDefaultPlugin::parse_property(Object *p_object, Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, int p_usage) {
  2154. double default_float_step = EDITOR_GET("interface/inspector/default_float_step");
  2155. switch (p_type) {
  2156. // atomic types
  2157. case Variant::NIL: {
  2158. EditorPropertyNil *editor = memnew(EditorPropertyNil);
  2159. add_property_editor(p_path, editor);
  2160. } break;
  2161. case Variant::BOOL: {
  2162. EditorPropertyCheck *editor = memnew(EditorPropertyCheck);
  2163. add_property_editor(p_path, editor);
  2164. } break;
  2165. case Variant::INT: {
  2166. if (p_hint == PROPERTY_HINT_ENUM) {
  2167. EditorPropertyEnum *editor = memnew(EditorPropertyEnum);
  2168. Vector<String> options = p_hint_text.split(",");
  2169. editor->setup(options);
  2170. add_property_editor(p_path, editor);
  2171. } else if (p_hint == PROPERTY_HINT_FLAGS) {
  2172. EditorPropertyFlags *editor = memnew(EditorPropertyFlags);
  2173. Vector<String> options = p_hint_text.split(",");
  2174. editor->setup(options);
  2175. add_property_editor(p_path, editor);
  2176. } else if (p_hint == PROPERTY_HINT_LAYERS_2D_PHYSICS || p_hint == PROPERTY_HINT_LAYERS_2D_RENDER || p_hint == PROPERTY_HINT_LAYERS_3D_PHYSICS || p_hint == PROPERTY_HINT_LAYERS_3D_RENDER) {
  2177. EditorPropertyLayers::LayerType lt = EditorPropertyLayers::LAYER_RENDER_2D;
  2178. switch (p_hint) {
  2179. case PROPERTY_HINT_LAYERS_2D_RENDER:
  2180. lt = EditorPropertyLayers::LAYER_RENDER_2D;
  2181. break;
  2182. case PROPERTY_HINT_LAYERS_2D_PHYSICS:
  2183. lt = EditorPropertyLayers::LAYER_PHYSICS_2D;
  2184. break;
  2185. case PROPERTY_HINT_LAYERS_3D_RENDER:
  2186. lt = EditorPropertyLayers::LAYER_RENDER_3D;
  2187. break;
  2188. case PROPERTY_HINT_LAYERS_3D_PHYSICS:
  2189. lt = EditorPropertyLayers::LAYER_PHYSICS_3D;
  2190. break;
  2191. default: {
  2192. } //compiler could be smarter here and realize this can't happen
  2193. }
  2194. EditorPropertyLayers *editor = memnew(EditorPropertyLayers);
  2195. editor->setup(lt);
  2196. add_property_editor(p_path, editor);
  2197. } else if (p_hint == PROPERTY_HINT_OBJECT_ID) {
  2198. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  2199. editor->setup(p_hint_text);
  2200. add_property_editor(p_path, editor);
  2201. } else {
  2202. EditorPropertyInteger *editor = memnew(EditorPropertyInteger);
  2203. int min = 0, max = 65535, step = 1;
  2204. bool greater = true, lesser = true;
  2205. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2206. greater = false; //if using ranged, assume false by default
  2207. lesser = false;
  2208. min = p_hint_text.get_slice(",", 0).to_int();
  2209. max = p_hint_text.get_slice(",", 1).to_int();
  2210. if (p_hint_text.get_slice_count(",") >= 3) {
  2211. step = p_hint_text.get_slice(",", 2).to_int();
  2212. }
  2213. for (int i = 2; i < p_hint_text.get_slice_count(","); i++) {
  2214. String slice = p_hint_text.get_slice(",", i).strip_edges();
  2215. if (slice == "or_greater") {
  2216. greater = true;
  2217. }
  2218. if (slice == "or_lesser") {
  2219. lesser = true;
  2220. }
  2221. }
  2222. }
  2223. editor->setup(min, max, step, greater, lesser);
  2224. add_property_editor(p_path, editor);
  2225. }
  2226. } break;
  2227. case Variant::REAL: {
  2228. if (p_hint == PROPERTY_HINT_EXP_EASING) {
  2229. EditorPropertyEasing *editor = memnew(EditorPropertyEasing);
  2230. bool full = true;
  2231. bool flip = false;
  2232. Vector<String> hints = p_hint_text.split(",");
  2233. for (int i = 0; i < hints.size(); i++) {
  2234. String h = hints[i].strip_edges();
  2235. if (h == "attenuation") {
  2236. flip = true;
  2237. }
  2238. if (h == "inout") {
  2239. full = true;
  2240. }
  2241. }
  2242. editor->setup(full, flip);
  2243. add_property_editor(p_path, editor);
  2244. } else {
  2245. EditorPropertyFloat *editor = memnew(EditorPropertyFloat);
  2246. double min = -65535, max = 65535, step = default_float_step;
  2247. bool hide_slider = true;
  2248. bool exp_range = false;
  2249. bool greater = true, lesser = true;
  2250. if ((p_hint == PROPERTY_HINT_RANGE || p_hint == PROPERTY_HINT_EXP_RANGE) && p_hint_text.get_slice_count(",") >= 2) {
  2251. greater = false; //if using ranged, assume false by default
  2252. lesser = false;
  2253. min = p_hint_text.get_slice(",", 0).to_double();
  2254. max = p_hint_text.get_slice(",", 1).to_double();
  2255. if (p_hint_text.get_slice_count(",") >= 3) {
  2256. step = p_hint_text.get_slice(",", 2).to_double();
  2257. }
  2258. hide_slider = false;
  2259. exp_range = p_hint == PROPERTY_HINT_EXP_RANGE;
  2260. for (int i = 2; i < p_hint_text.get_slice_count(","); i++) {
  2261. String slice = p_hint_text.get_slice(",", i).strip_edges();
  2262. if (slice == "or_greater") {
  2263. greater = true;
  2264. }
  2265. if (slice == "or_lesser") {
  2266. lesser = true;
  2267. }
  2268. }
  2269. }
  2270. editor->setup(min, max, step, hide_slider, exp_range, greater, lesser);
  2271. add_property_editor(p_path, editor);
  2272. }
  2273. } break;
  2274. case Variant::STRING: {
  2275. if (p_hint == PROPERTY_HINT_ENUM) {
  2276. EditorPropertyTextEnum *editor = memnew(EditorPropertyTextEnum);
  2277. Vector<String> options = p_hint_text.split(",");
  2278. editor->setup(options);
  2279. add_property_editor(p_path, editor);
  2280. } else if (p_hint == PROPERTY_HINT_MULTILINE_TEXT) {
  2281. EditorPropertyMultilineText *editor = memnew(EditorPropertyMultilineText);
  2282. add_property_editor(p_path, editor);
  2283. } else if (p_hint == PROPERTY_HINT_TYPE_STRING) {
  2284. EditorPropertyClassName *editor = memnew(EditorPropertyClassName);
  2285. editor->setup("Object", p_hint_text);
  2286. add_property_editor(p_path, editor);
  2287. } else if (p_hint == PROPERTY_HINT_DIR || p_hint == PROPERTY_HINT_FILE || p_hint == PROPERTY_HINT_SAVE_FILE || p_hint == PROPERTY_HINT_GLOBAL_DIR || p_hint == PROPERTY_HINT_GLOBAL_FILE) {
  2288. Vector<String> extensions = p_hint_text.split(",");
  2289. bool global = p_hint == PROPERTY_HINT_GLOBAL_DIR || p_hint == PROPERTY_HINT_GLOBAL_FILE;
  2290. bool folder = p_hint == PROPERTY_HINT_DIR || p_hint == PROPERTY_HINT_GLOBAL_DIR;
  2291. bool save = p_hint == PROPERTY_HINT_SAVE_FILE;
  2292. EditorPropertyPath *editor = memnew(EditorPropertyPath);
  2293. editor->setup(extensions, folder, global);
  2294. if (save) {
  2295. editor->set_save_mode();
  2296. }
  2297. add_property_editor(p_path, editor);
  2298. } else if (p_hint == PROPERTY_HINT_METHOD_OF_VARIANT_TYPE ||
  2299. p_hint == PROPERTY_HINT_METHOD_OF_BASE_TYPE ||
  2300. p_hint == PROPERTY_HINT_METHOD_OF_INSTANCE ||
  2301. p_hint == PROPERTY_HINT_METHOD_OF_SCRIPT ||
  2302. p_hint == PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE ||
  2303. p_hint == PROPERTY_HINT_PROPERTY_OF_BASE_TYPE ||
  2304. p_hint == PROPERTY_HINT_PROPERTY_OF_INSTANCE ||
  2305. p_hint == PROPERTY_HINT_PROPERTY_OF_SCRIPT) {
  2306. EditorPropertyMember *editor = memnew(EditorPropertyMember);
  2307. EditorPropertyMember::Type type = EditorPropertyMember::MEMBER_METHOD_OF_BASE_TYPE;
  2308. switch (p_hint) {
  2309. case PROPERTY_HINT_METHOD_OF_BASE_TYPE:
  2310. type = EditorPropertyMember::MEMBER_METHOD_OF_BASE_TYPE;
  2311. break;
  2312. case PROPERTY_HINT_METHOD_OF_INSTANCE:
  2313. type = EditorPropertyMember::MEMBER_METHOD_OF_INSTANCE;
  2314. break;
  2315. case PROPERTY_HINT_METHOD_OF_SCRIPT:
  2316. type = EditorPropertyMember::MEMBER_METHOD_OF_SCRIPT;
  2317. break;
  2318. case PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE:
  2319. type = EditorPropertyMember::MEMBER_PROPERTY_OF_VARIANT_TYPE;
  2320. break;
  2321. case PROPERTY_HINT_PROPERTY_OF_BASE_TYPE:
  2322. type = EditorPropertyMember::MEMBER_PROPERTY_OF_BASE_TYPE;
  2323. break;
  2324. case PROPERTY_HINT_PROPERTY_OF_INSTANCE:
  2325. type = EditorPropertyMember::MEMBER_PROPERTY_OF_INSTANCE;
  2326. break;
  2327. case PROPERTY_HINT_PROPERTY_OF_SCRIPT:
  2328. type = EditorPropertyMember::MEMBER_PROPERTY_OF_SCRIPT;
  2329. break;
  2330. default: {
  2331. }
  2332. }
  2333. editor->setup(type, p_hint_text);
  2334. add_property_editor(p_path, editor);
  2335. } else {
  2336. EditorPropertyText *editor = memnew(EditorPropertyText);
  2337. if (p_hint == PROPERTY_HINT_PLACEHOLDER_TEXT) {
  2338. editor->set_placeholder(p_hint_text);
  2339. }
  2340. add_property_editor(p_path, editor);
  2341. }
  2342. } break;
  2343. // math types
  2344. case Variant::VECTOR2: {
  2345. EditorPropertyVector2 *editor = memnew(EditorPropertyVector2);
  2346. double min = -65535, max = 65535, step = default_float_step;
  2347. bool hide_slider = true;
  2348. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2349. min = p_hint_text.get_slice(",", 0).to_double();
  2350. max = p_hint_text.get_slice(",", 1).to_double();
  2351. if (p_hint_text.get_slice_count(",") >= 3) {
  2352. step = p_hint_text.get_slice(",", 2).to_double();
  2353. }
  2354. hide_slider = false;
  2355. }
  2356. editor->setup(min, max, step, hide_slider);
  2357. add_property_editor(p_path, editor);
  2358. } break; // 5
  2359. case Variant::RECT2: {
  2360. EditorPropertyRect2 *editor = memnew(EditorPropertyRect2);
  2361. double min = -65535, max = 65535, step = default_float_step;
  2362. bool hide_slider = true;
  2363. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2364. min = p_hint_text.get_slice(",", 0).to_double();
  2365. max = p_hint_text.get_slice(",", 1).to_double();
  2366. if (p_hint_text.get_slice_count(",") >= 3) {
  2367. step = p_hint_text.get_slice(",", 2).to_double();
  2368. }
  2369. hide_slider = false;
  2370. }
  2371. editor->setup(min, max, step, hide_slider);
  2372. add_property_editor(p_path, editor);
  2373. } break;
  2374. case Variant::VECTOR3: {
  2375. EditorPropertyVector3 *editor = memnew(EditorPropertyVector3);
  2376. double min = -65535, max = 65535, step = default_float_step;
  2377. bool hide_slider = true;
  2378. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2379. min = p_hint_text.get_slice(",", 0).to_double();
  2380. max = p_hint_text.get_slice(",", 1).to_double();
  2381. if (p_hint_text.get_slice_count(",") >= 3) {
  2382. step = p_hint_text.get_slice(",", 2).to_double();
  2383. }
  2384. hide_slider = false;
  2385. }
  2386. editor->setup(min, max, step, hide_slider);
  2387. add_property_editor(p_path, editor);
  2388. } break;
  2389. case Variant::TRANSFORM2D: {
  2390. EditorPropertyTransform2D *editor = memnew(EditorPropertyTransform2D);
  2391. double min = -65535, max = 65535, step = default_float_step;
  2392. bool hide_slider = true;
  2393. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2394. min = p_hint_text.get_slice(",", 0).to_double();
  2395. max = p_hint_text.get_slice(",", 1).to_double();
  2396. if (p_hint_text.get_slice_count(",") >= 3) {
  2397. step = p_hint_text.get_slice(",", 2).to_double();
  2398. }
  2399. hide_slider = false;
  2400. }
  2401. editor->setup(min, max, step, hide_slider);
  2402. add_property_editor(p_path, editor);
  2403. } break;
  2404. case Variant::PLANE: {
  2405. EditorPropertyPlane *editor = memnew(EditorPropertyPlane);
  2406. double min = -65535, max = 65535, step = default_float_step;
  2407. bool hide_slider = true;
  2408. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2409. min = p_hint_text.get_slice(",", 0).to_double();
  2410. max = p_hint_text.get_slice(",", 1).to_double();
  2411. if (p_hint_text.get_slice_count(",") >= 3) {
  2412. step = p_hint_text.get_slice(",", 2).to_double();
  2413. }
  2414. hide_slider = false;
  2415. }
  2416. editor->setup(min, max, step, hide_slider);
  2417. add_property_editor(p_path, editor);
  2418. } break;
  2419. case Variant::QUAT: {
  2420. EditorPropertyQuat *editor = memnew(EditorPropertyQuat);
  2421. double min = -65535, max = 65535, step = default_float_step;
  2422. bool hide_slider = true;
  2423. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2424. min = p_hint_text.get_slice(",", 0).to_double();
  2425. max = p_hint_text.get_slice(",", 1).to_double();
  2426. if (p_hint_text.get_slice_count(",") >= 3) {
  2427. step = p_hint_text.get_slice(",", 2).to_double();
  2428. }
  2429. hide_slider = false;
  2430. }
  2431. editor->setup(min, max, step, hide_slider);
  2432. add_property_editor(p_path, editor);
  2433. } break; // 10
  2434. case Variant::AABB: {
  2435. EditorPropertyAABB *editor = memnew(EditorPropertyAABB);
  2436. double min = -65535, max = 65535, step = default_float_step;
  2437. bool hide_slider = true;
  2438. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2439. min = p_hint_text.get_slice(",", 0).to_double();
  2440. max = p_hint_text.get_slice(",", 1).to_double();
  2441. if (p_hint_text.get_slice_count(",") >= 3) {
  2442. step = p_hint_text.get_slice(",", 2).to_double();
  2443. }
  2444. hide_slider = false;
  2445. }
  2446. editor->setup(min, max, step, hide_slider);
  2447. add_property_editor(p_path, editor);
  2448. } break;
  2449. case Variant::BASIS: {
  2450. EditorPropertyBasis *editor = memnew(EditorPropertyBasis);
  2451. double min = -65535, max = 65535, step = default_float_step;
  2452. bool hide_slider = true;
  2453. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2454. min = p_hint_text.get_slice(",", 0).to_double();
  2455. max = p_hint_text.get_slice(",", 1).to_double();
  2456. if (p_hint_text.get_slice_count(",") >= 3) {
  2457. step = p_hint_text.get_slice(",", 2).to_double();
  2458. }
  2459. hide_slider = false;
  2460. }
  2461. editor->setup(min, max, step, hide_slider);
  2462. add_property_editor(p_path, editor);
  2463. } break;
  2464. case Variant::TRANSFORM: {
  2465. EditorPropertyTransform *editor = memnew(EditorPropertyTransform);
  2466. double min = -65535, max = 65535, step = default_float_step;
  2467. bool hide_slider = true;
  2468. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2469. min = p_hint_text.get_slice(",", 0).to_double();
  2470. max = p_hint_text.get_slice(",", 1).to_double();
  2471. if (p_hint_text.get_slice_count(",") >= 3) {
  2472. step = p_hint_text.get_slice(",", 2).to_double();
  2473. }
  2474. hide_slider = false;
  2475. }
  2476. editor->setup(min, max, step, hide_slider);
  2477. add_property_editor(p_path, editor);
  2478. } break;
  2479. // misc types
  2480. case Variant::COLOR: {
  2481. EditorPropertyColor *editor = memnew(EditorPropertyColor);
  2482. editor->setup(p_hint != PROPERTY_HINT_COLOR_NO_ALPHA);
  2483. add_property_editor(p_path, editor);
  2484. } break;
  2485. case Variant::NODE_PATH: {
  2486. EditorPropertyNodePath *editor = memnew(EditorPropertyNodePath);
  2487. if (p_hint == PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE && p_hint_text != String()) {
  2488. editor->setup(p_hint_text, Vector<StringName>(), (p_usage & PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT));
  2489. }
  2490. if (p_hint == PROPERTY_HINT_NODE_PATH_VALID_TYPES && p_hint_text != String()) {
  2491. Vector<String> types = p_hint_text.split(",", false);
  2492. Vector<StringName> sn = Variant(types); //convert via variant
  2493. editor->setup(NodePath(), sn, (p_usage & PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT));
  2494. }
  2495. add_property_editor(p_path, editor);
  2496. } break; // 15
  2497. case Variant::_RID: {
  2498. EditorPropertyRID *editor = memnew(EditorPropertyRID);
  2499. add_property_editor(p_path, editor);
  2500. } break;
  2501. case Variant::OBJECT: {
  2502. EditorPropertyResource *editor = memnew(EditorPropertyResource);
  2503. editor->setup(p_object, p_path, p_hint == PROPERTY_HINT_RESOURCE_TYPE ? p_hint_text : "Resource");
  2504. if (p_hint == PROPERTY_HINT_RESOURCE_TYPE) {
  2505. String open_in_new = EDITOR_GET("interface/inspector/resources_to_open_in_new_inspector");
  2506. for (int i = 0; i < open_in_new.get_slice_count(","); i++) {
  2507. String type = open_in_new.get_slicec(',', i).strip_edges();
  2508. for (int j = 0; j < p_hint_text.get_slice_count(","); j++) {
  2509. String inherits = p_hint_text.get_slicec(',', j);
  2510. if (ClassDB::is_parent_class(inherits, type)) {
  2511. editor->set_use_sub_inspector(false);
  2512. }
  2513. }
  2514. }
  2515. }
  2516. add_property_editor(p_path, editor);
  2517. } break;
  2518. case Variant::DICTIONARY: {
  2519. EditorPropertyDictionary *editor = memnew(EditorPropertyDictionary);
  2520. add_property_editor(p_path, editor);
  2521. } break;
  2522. case Variant::ARRAY: {
  2523. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2524. editor->setup(Variant::ARRAY, p_hint_text);
  2525. add_property_editor(p_path, editor);
  2526. } break;
  2527. case Variant::POOL_BYTE_ARRAY: {
  2528. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2529. editor->setup(Variant::POOL_BYTE_ARRAY);
  2530. add_property_editor(p_path, editor);
  2531. } break; // 20
  2532. case Variant::POOL_INT_ARRAY: {
  2533. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2534. editor->setup(Variant::POOL_INT_ARRAY);
  2535. add_property_editor(p_path, editor);
  2536. } break;
  2537. case Variant::POOL_REAL_ARRAY: {
  2538. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2539. editor->setup(Variant::POOL_REAL_ARRAY);
  2540. add_property_editor(p_path, editor);
  2541. } break;
  2542. case Variant::POOL_STRING_ARRAY: {
  2543. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2544. editor->setup(Variant::POOL_STRING_ARRAY);
  2545. add_property_editor(p_path, editor);
  2546. } break;
  2547. case Variant::POOL_VECTOR2_ARRAY: {
  2548. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2549. editor->setup(Variant::POOL_VECTOR2_ARRAY);
  2550. add_property_editor(p_path, editor);
  2551. } break;
  2552. case Variant::POOL_VECTOR3_ARRAY: {
  2553. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2554. editor->setup(Variant::POOL_VECTOR3_ARRAY);
  2555. add_property_editor(p_path, editor);
  2556. } break; // 25
  2557. case Variant::POOL_COLOR_ARRAY: {
  2558. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2559. editor->setup(Variant::POOL_COLOR_ARRAY);
  2560. add_property_editor(p_path, editor);
  2561. } break;
  2562. default: {
  2563. }
  2564. }
  2565. return false; //can be overridden, although it will most likely be last anyway
  2566. }
  2567. void EditorInspectorDefaultPlugin::parse_end() {
  2568. //do none
  2569. }