editor_properties.cpp 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810
  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/window.h"
  37. #include "scene/resources/font.h"
  38. ///////////////////// NULL /////////////////////////
  39. void EditorPropertyNil::update_property() {
  40. }
  41. EditorPropertyNil::EditorPropertyNil() {
  42. Label *label = memnew(Label);
  43. label->set_text("[null]");
  44. add_child(label);
  45. }
  46. ///////////////////// TEXT /////////////////////////
  47. void EditorPropertyText::_text_entered(const String &p_string) {
  48. if (updating) {
  49. return;
  50. }
  51. if (text->has_focus()) {
  52. text->release_focus();
  53. _text_changed(p_string);
  54. }
  55. }
  56. void EditorPropertyText::_text_changed(const String &p_string) {
  57. if (updating) {
  58. return;
  59. }
  60. if (string_name) {
  61. emit_changed(get_edited_property(), StringName(p_string), "", true);
  62. } else {
  63. emit_changed(get_edited_property(), p_string, "", true);
  64. }
  65. }
  66. void EditorPropertyText::update_property() {
  67. String s = get_edited_object()->get(get_edited_property());
  68. updating = true;
  69. if (text->get_text() != s) {
  70. text->set_text(s);
  71. }
  72. text->set_editable(!is_read_only());
  73. updating = false;
  74. }
  75. void EditorPropertyText::set_string_name(bool p_enabled) {
  76. string_name = p_enabled;
  77. }
  78. void EditorPropertyText::set_placeholder(const String &p_string) {
  79. text->set_placeholder(p_string);
  80. }
  81. void EditorPropertyText::_bind_methods() {
  82. }
  83. EditorPropertyText::EditorPropertyText() {
  84. text = memnew(LineEdit);
  85. add_child(text);
  86. add_focusable(text);
  87. text->connect("text_changed", callable_mp(this, &EditorPropertyText::_text_changed));
  88. text->connect("text_entered", callable_mp(this, &EditorPropertyText::_text_entered));
  89. string_name = false;
  90. updating = false;
  91. }
  92. ///////////////////// MULTILINE TEXT /////////////////////////
  93. void EditorPropertyMultilineText::_big_text_changed() {
  94. text->set_text(big_text->get_text());
  95. emit_changed(get_edited_property(), big_text->get_text(), "", true);
  96. }
  97. void EditorPropertyMultilineText::_text_changed() {
  98. emit_changed(get_edited_property(), text->get_text(), "", true);
  99. }
  100. void EditorPropertyMultilineText::_open_big_text() {
  101. if (!big_text_dialog) {
  102. big_text = memnew(TextEdit);
  103. big_text->connect("text_changed", callable_mp(this, &EditorPropertyMultilineText::_big_text_changed));
  104. big_text->set_wrap_enabled(true);
  105. big_text_dialog = memnew(AcceptDialog);
  106. big_text_dialog->add_child(big_text);
  107. big_text_dialog->set_title(TTR("Edit Text:"));
  108. add_child(big_text_dialog);
  109. }
  110. big_text_dialog->popup_centered_clamped(Size2(1000, 900) * EDSCALE, 0.8);
  111. big_text->set_text(text->get_text());
  112. big_text->grab_focus();
  113. }
  114. void EditorPropertyMultilineText::update_property() {
  115. String t = get_edited_object()->get(get_edited_property());
  116. if (text->get_text() != t) {
  117. text->set_text(t);
  118. if (big_text && big_text->is_visible_in_tree()) {
  119. big_text->set_text(t);
  120. }
  121. }
  122. }
  123. void EditorPropertyMultilineText::_notification(int p_what) {
  124. switch (p_what) {
  125. case NOTIFICATION_THEME_CHANGED:
  126. case NOTIFICATION_ENTER_TREE: {
  127. Ref<Texture2D> df = get_theme_icon("DistractionFree", "EditorIcons");
  128. open_big_text->set_icon(df);
  129. Ref<Font> font = get_theme_font("font", "Label");
  130. int font_size = get_theme_font_size("font_size", "Label");
  131. text->set_custom_minimum_size(Vector2(0, font->get_height(font_size) * 6));
  132. } break;
  133. }
  134. }
  135. void EditorPropertyMultilineText::_bind_methods() {
  136. }
  137. EditorPropertyMultilineText::EditorPropertyMultilineText() {
  138. HBoxContainer *hb = memnew(HBoxContainer);
  139. add_child(hb);
  140. set_bottom_editor(hb);
  141. text = memnew(TextEdit);
  142. text->connect("text_changed", callable_mp(this, &EditorPropertyMultilineText::_text_changed));
  143. text->set_wrap_enabled(true);
  144. add_focusable(text);
  145. hb->add_child(text);
  146. text->set_h_size_flags(SIZE_EXPAND_FILL);
  147. open_big_text = memnew(Button);
  148. open_big_text->set_flat(true);
  149. open_big_text->connect("pressed", callable_mp(this, &EditorPropertyMultilineText::_open_big_text));
  150. hb->add_child(open_big_text);
  151. big_text_dialog = nullptr;
  152. big_text = nullptr;
  153. }
  154. ///////////////////// TEXT ENUM /////////////////////////
  155. void EditorPropertyTextEnum::_option_selected(int p_which) {
  156. if (string_name) {
  157. emit_changed(get_edited_property(), StringName(options->get_item_text(p_which)));
  158. } else {
  159. emit_changed(get_edited_property(), options->get_item_text(p_which));
  160. }
  161. }
  162. void EditorPropertyTextEnum::update_property() {
  163. String which = get_edited_object()->get(get_edited_property());
  164. for (int i = 0; i < options->get_item_count(); i++) {
  165. String t = options->get_item_text(i);
  166. if (t == which) {
  167. options->select(i);
  168. return;
  169. }
  170. }
  171. }
  172. void EditorPropertyTextEnum::setup(const Vector<String> &p_options, bool p_string_name) {
  173. for (int i = 0; i < p_options.size(); i++) {
  174. options->add_item(p_options[i], i);
  175. }
  176. string_name = p_string_name;
  177. }
  178. void EditorPropertyTextEnum::_bind_methods() {
  179. }
  180. EditorPropertyTextEnum::EditorPropertyTextEnum() {
  181. options = memnew(OptionButton);
  182. options->set_clip_text(true);
  183. options->set_flat(true);
  184. string_name = false;
  185. add_child(options);
  186. add_focusable(options);
  187. options->connect("item_selected", callable_mp(this, &EditorPropertyTextEnum::_option_selected));
  188. }
  189. ///////////////////// PATH /////////////////////////
  190. void EditorPropertyPath::_path_selected(const String &p_path) {
  191. emit_changed(get_edited_property(), p_path);
  192. update_property();
  193. }
  194. void EditorPropertyPath::_path_pressed() {
  195. if (!dialog) {
  196. dialog = memnew(EditorFileDialog);
  197. dialog->connect("file_selected", callable_mp(this, &EditorPropertyPath::_path_selected));
  198. dialog->connect("dir_selected", callable_mp(this, &EditorPropertyPath::_path_selected));
  199. add_child(dialog);
  200. }
  201. String full_path = get_edited_object()->get(get_edited_property());
  202. dialog->clear_filters();
  203. if (global) {
  204. dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  205. } else {
  206. dialog->set_access(EditorFileDialog::ACCESS_RESOURCES);
  207. }
  208. if (folder) {
  209. dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_DIR);
  210. dialog->set_current_dir(full_path);
  211. } else {
  212. dialog->set_file_mode(save_mode ? EditorFileDialog::FILE_MODE_SAVE_FILE : EditorFileDialog::FILE_MODE_OPEN_FILE);
  213. for (int i = 0; i < extensions.size(); i++) {
  214. String e = extensions[i].strip_edges();
  215. if (e != String()) {
  216. dialog->add_filter(extensions[i].strip_edges());
  217. }
  218. }
  219. dialog->set_current_path(full_path);
  220. }
  221. dialog->popup_file_dialog();
  222. }
  223. void EditorPropertyPath::update_property() {
  224. String full_path = get_edited_object()->get(get_edited_property());
  225. path->set_text(full_path);
  226. path->set_tooltip(full_path);
  227. }
  228. void EditorPropertyPath::setup(const Vector<String> &p_extensions, bool p_folder, bool p_global) {
  229. extensions = p_extensions;
  230. folder = p_folder;
  231. global = p_global;
  232. }
  233. void EditorPropertyPath::set_save_mode() {
  234. save_mode = true;
  235. }
  236. void EditorPropertyPath::_notification(int p_what) {
  237. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  238. path_edit->set_icon(get_theme_icon("Folder", "EditorIcons"));
  239. }
  240. }
  241. void EditorPropertyPath::_path_focus_exited() {
  242. _path_selected(path->get_text());
  243. }
  244. void EditorPropertyPath::_bind_methods() {
  245. }
  246. EditorPropertyPath::EditorPropertyPath() {
  247. HBoxContainer *path_hb = memnew(HBoxContainer);
  248. add_child(path_hb);
  249. path = memnew(LineEdit);
  250. path->set_structured_text_bidi_override(Control::STRUCTURED_TEXT_FILE);
  251. path_hb->add_child(path);
  252. path->connect("text_entered", callable_mp(this, &EditorPropertyPath::_path_selected));
  253. path->connect("focus_exited", callable_mp(this, &EditorPropertyPath::_path_focus_exited));
  254. path->set_h_size_flags(SIZE_EXPAND_FILL);
  255. path_edit = memnew(Button);
  256. path_edit->set_clip_text(true);
  257. path_hb->add_child(path_edit);
  258. add_focusable(path);
  259. dialog = nullptr;
  260. path_edit->connect("pressed", callable_mp(this, &EditorPropertyPath::_path_pressed));
  261. folder = false;
  262. global = false;
  263. save_mode = false;
  264. }
  265. ///////////////////// CLASS NAME /////////////////////////
  266. void EditorPropertyClassName::setup(const String &p_base_type, const String &p_selected_type) {
  267. base_type = p_base_type;
  268. dialog->set_base_type(base_type);
  269. selected_type = p_selected_type;
  270. property->set_text(selected_type);
  271. }
  272. void EditorPropertyClassName::update_property() {
  273. String s = get_edited_object()->get(get_edited_property());
  274. property->set_text(s);
  275. selected_type = s;
  276. }
  277. void EditorPropertyClassName::_property_selected() {
  278. dialog->popup_create(true);
  279. }
  280. void EditorPropertyClassName::_dialog_created() {
  281. selected_type = dialog->get_selected_type();
  282. emit_changed(get_edited_property(), selected_type);
  283. update_property();
  284. }
  285. void EditorPropertyClassName::_bind_methods() {
  286. }
  287. EditorPropertyClassName::EditorPropertyClassName() {
  288. property = memnew(Button);
  289. property->set_clip_text(true);
  290. add_child(property);
  291. add_focusable(property);
  292. property->set_text(selected_type);
  293. property->connect("pressed", callable_mp(this, &EditorPropertyClassName::_property_selected));
  294. dialog = memnew(CreateDialog);
  295. dialog->set_base_type(base_type);
  296. dialog->connect("create", callable_mp(this, &EditorPropertyClassName::_dialog_created));
  297. add_child(dialog);
  298. }
  299. ///////////////////// MEMBER /////////////////////////
  300. void EditorPropertyMember::_property_selected(const String &p_selected) {
  301. emit_changed(get_edited_property(), p_selected);
  302. update_property();
  303. }
  304. void EditorPropertyMember::_property_select() {
  305. if (!selector) {
  306. selector = memnew(PropertySelector);
  307. selector->connect("selected", callable_mp(this, &EditorPropertyMember::_property_selected));
  308. add_child(selector);
  309. }
  310. String current = get_edited_object()->get(get_edited_property());
  311. if (hint == MEMBER_METHOD_OF_VARIANT_TYPE) {
  312. Variant::Type type = Variant::NIL;
  313. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  314. if (hint_text == Variant::get_type_name(Variant::Type(i))) {
  315. type = Variant::Type(i);
  316. }
  317. }
  318. if (type != Variant::NIL) {
  319. selector->select_method_from_basic_type(type, current);
  320. }
  321. } else if (hint == MEMBER_METHOD_OF_BASE_TYPE) {
  322. selector->select_method_from_base_type(hint_text, current);
  323. } else if (hint == MEMBER_METHOD_OF_INSTANCE) {
  324. Object *instance = ObjectDB::get_instance(ObjectID(hint_text.to_int()));
  325. if (instance) {
  326. selector->select_method_from_instance(instance, current);
  327. }
  328. } else if (hint == MEMBER_METHOD_OF_SCRIPT) {
  329. Object *obj = ObjectDB::get_instance(ObjectID(hint_text.to_int()));
  330. if (Object::cast_to<Script>(obj)) {
  331. selector->select_method_from_script(Object::cast_to<Script>(obj), current);
  332. }
  333. } else if (hint == MEMBER_PROPERTY_OF_VARIANT_TYPE) {
  334. Variant::Type type = Variant::NIL;
  335. String tname = hint_text;
  336. if (tname.find(".") != -1) {
  337. tname = tname.get_slice(".", 0);
  338. }
  339. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  340. if (tname == Variant::get_type_name(Variant::Type(i))) {
  341. type = Variant::Type(Variant::Type(i));
  342. }
  343. }
  344. if (type != Variant::NIL) {
  345. selector->select_property_from_basic_type(type, current);
  346. }
  347. } else if (hint == MEMBER_PROPERTY_OF_BASE_TYPE) {
  348. selector->select_property_from_base_type(hint_text, current);
  349. } else if (hint == MEMBER_PROPERTY_OF_INSTANCE) {
  350. Object *instance = ObjectDB::get_instance(ObjectID(hint_text.to_int()));
  351. if (instance) {
  352. selector->select_property_from_instance(instance, current);
  353. }
  354. } else if (hint == MEMBER_PROPERTY_OF_SCRIPT) {
  355. Object *obj = ObjectDB::get_instance(ObjectID(hint_text.to_int()));
  356. if (Object::cast_to<Script>(obj)) {
  357. selector->select_property_from_script(Object::cast_to<Script>(obj), current);
  358. }
  359. }
  360. }
  361. void EditorPropertyMember::setup(Type p_hint, const String &p_hint_text) {
  362. hint = p_hint;
  363. hint_text = p_hint_text;
  364. }
  365. void EditorPropertyMember::update_property() {
  366. String full_path = get_edited_object()->get(get_edited_property());
  367. property->set_text(full_path);
  368. }
  369. void EditorPropertyMember::_bind_methods() {
  370. }
  371. EditorPropertyMember::EditorPropertyMember() {
  372. selector = nullptr;
  373. property = memnew(Button);
  374. property->set_clip_text(true);
  375. add_child(property);
  376. add_focusable(property);
  377. property->connect("pressed", callable_mp(this, &EditorPropertyMember::_property_select));
  378. }
  379. ///////////////////// CHECK /////////////////////////
  380. void EditorPropertyCheck::_checkbox_pressed() {
  381. emit_changed(get_edited_property(), checkbox->is_pressed());
  382. }
  383. void EditorPropertyCheck::update_property() {
  384. bool c = get_edited_object()->get(get_edited_property());
  385. checkbox->set_pressed(c);
  386. checkbox->set_disabled(is_read_only());
  387. }
  388. void EditorPropertyCheck::_bind_methods() {
  389. }
  390. EditorPropertyCheck::EditorPropertyCheck() {
  391. checkbox = memnew(CheckBox);
  392. checkbox->set_text(TTR("On"));
  393. add_child(checkbox);
  394. add_focusable(checkbox);
  395. checkbox->connect("pressed", callable_mp(this, &EditorPropertyCheck::_checkbox_pressed));
  396. }
  397. ///////////////////// ENUM /////////////////////////
  398. void EditorPropertyEnum::_option_selected(int p_which) {
  399. int64_t val = options->get_item_metadata(p_which);
  400. emit_changed(get_edited_property(), val);
  401. }
  402. void EditorPropertyEnum::update_property() {
  403. int64_t which = get_edited_object()->get(get_edited_property());
  404. for (int i = 0; i < options->get_item_count(); i++) {
  405. if (which == (int64_t)options->get_item_metadata(i)) {
  406. options->select(i);
  407. return;
  408. }
  409. }
  410. }
  411. void EditorPropertyEnum::setup(const Vector<String> &p_options) {
  412. int64_t current_val = 0;
  413. for (int i = 0; i < p_options.size(); i++) {
  414. Vector<String> text_split = p_options[i].split(":");
  415. if (text_split.size() != 1) {
  416. current_val = text_split[1].to_int();
  417. }
  418. options->add_item(text_split[0]);
  419. options->set_item_metadata(i, current_val);
  420. current_val += 1;
  421. }
  422. }
  423. void EditorPropertyEnum::set_option_button_clip(bool p_enable) {
  424. options->set_clip_text(p_enable);
  425. }
  426. void EditorPropertyEnum::_bind_methods() {
  427. }
  428. EditorPropertyEnum::EditorPropertyEnum() {
  429. options = memnew(OptionButton);
  430. options->set_clip_text(true);
  431. options->set_flat(true);
  432. add_child(options);
  433. add_focusable(options);
  434. options->connect("item_selected", callable_mp(this, &EditorPropertyEnum::_option_selected));
  435. }
  436. ///////////////////// FLAGS /////////////////////////
  437. void EditorPropertyFlags::_flag_toggled() {
  438. uint32_t value = 0;
  439. for (int i = 0; i < flags.size(); i++) {
  440. if (flags[i]->is_pressed()) {
  441. uint32_t val = 1;
  442. val <<= flag_indices[i];
  443. value |= val;
  444. }
  445. }
  446. emit_changed(get_edited_property(), value);
  447. }
  448. void EditorPropertyFlags::update_property() {
  449. uint32_t value = get_edited_object()->get(get_edited_property());
  450. for (int i = 0; i < flags.size(); i++) {
  451. uint32_t val = 1;
  452. val <<= flag_indices[i];
  453. if (value & val) {
  454. flags[i]->set_pressed(true);
  455. } else {
  456. flags[i]->set_pressed(false);
  457. }
  458. }
  459. }
  460. void EditorPropertyFlags::setup(const Vector<String> &p_options) {
  461. ERR_FAIL_COND(flags.size());
  462. bool first = true;
  463. for (int i = 0; i < p_options.size(); i++) {
  464. String option = p_options[i].strip_edges();
  465. if (option != "") {
  466. CheckBox *cb = memnew(CheckBox);
  467. cb->set_text(option);
  468. cb->set_clip_text(true);
  469. cb->connect("pressed", callable_mp(this, &EditorPropertyFlags::_flag_toggled));
  470. add_focusable(cb);
  471. vbox->add_child(cb);
  472. flags.push_back(cb);
  473. flag_indices.push_back(i);
  474. if (first) {
  475. set_label_reference(cb);
  476. first = false;
  477. }
  478. }
  479. }
  480. }
  481. void EditorPropertyFlags::_bind_methods() {
  482. }
  483. EditorPropertyFlags::EditorPropertyFlags() {
  484. vbox = memnew(VBoxContainer);
  485. add_child(vbox);
  486. }
  487. ///////////////////// LAYERS /////////////////////////
  488. class EditorPropertyLayersGrid : public Control {
  489. GDCLASS(EditorPropertyLayersGrid, Control);
  490. public:
  491. uint32_t value;
  492. Vector<Rect2> flag_rects;
  493. Vector<String> names;
  494. Vector<String> tooltips;
  495. int hovered_index;
  496. virtual Size2 get_minimum_size() const override {
  497. Ref<Font> font = get_theme_font("font", "Label");
  498. int font_size = get_theme_font_size("font_size", "Label");
  499. return Vector2(0, font->get_height(font_size) * 2);
  500. }
  501. virtual String get_tooltip(const Point2 &p_pos) const override {
  502. for (int i = 0; i < flag_rects.size(); i++) {
  503. if (i < tooltips.size() && flag_rects[i].has_point(p_pos)) {
  504. return tooltips[i];
  505. }
  506. }
  507. return String();
  508. }
  509. void _gui_input(const Ref<InputEvent> &p_ev) {
  510. const Ref<InputEventMouseMotion> mm = p_ev;
  511. if (mm.is_valid()) {
  512. for (int i = 0; i < flag_rects.size(); i++) {
  513. if (flag_rects[i].has_point(mm->get_position())) {
  514. // Used to highlight the hovered flag in the layers grid.
  515. hovered_index = i;
  516. update();
  517. break;
  518. }
  519. }
  520. }
  521. const Ref<InputEventMouseButton> mb = p_ev;
  522. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && mb->is_pressed() && hovered_index >= 0) {
  523. // Toggle the flag.
  524. // We base our choice on the hovered flag, so that it always matches the hovered flag.
  525. if (value & (1 << hovered_index)) {
  526. value &= ~(1 << hovered_index);
  527. } else {
  528. value |= (1 << hovered_index);
  529. }
  530. emit_signal("flag_changed", value);
  531. update();
  532. }
  533. }
  534. void _notification(int p_what) {
  535. switch (p_what) {
  536. case NOTIFICATION_DRAW: {
  537. Rect2 rect;
  538. rect.size = get_size();
  539. flag_rects.clear();
  540. const int bsize = (rect.size.height * 80 / 100) / 2;
  541. const int h = bsize * 2 + 1;
  542. const int vofs = (rect.size.height - h) / 2;
  543. Color color = get_theme_color("highlight_color", "Editor");
  544. for (int i = 0; i < 2; i++) {
  545. Point2 ofs(4, vofs);
  546. if (i == 1)
  547. ofs.y += bsize + 1;
  548. ofs += rect.position;
  549. for (int j = 0; j < 10; j++) {
  550. Point2 o = ofs + Point2(j * (bsize + 1), 0);
  551. if (j >= 5)
  552. o.x += 1;
  553. const int idx = i * 10 + j;
  554. const bool on = value & (1 << idx);
  555. Rect2 rect2 = Rect2(o, Size2(bsize, bsize));
  556. color.a = on ? 0.6 : 0.2;
  557. if (idx == hovered_index) {
  558. // Add visual feedback when hovering a flag.
  559. color.a += 0.15;
  560. }
  561. draw_rect(rect2, color);
  562. flag_rects.push_back(rect2);
  563. }
  564. }
  565. } break;
  566. case NOTIFICATION_MOUSE_EXIT: {
  567. hovered_index = -1;
  568. update();
  569. } break;
  570. default:
  571. break;
  572. }
  573. }
  574. void set_flag(uint32_t p_flag) {
  575. value = p_flag;
  576. update();
  577. }
  578. static void _bind_methods() {
  579. ClassDB::bind_method(D_METHOD("_gui_input"), &EditorPropertyLayersGrid::_gui_input);
  580. ADD_SIGNAL(MethodInfo("flag_changed", PropertyInfo(Variant::INT, "flag")));
  581. }
  582. EditorPropertyLayersGrid() {
  583. value = 0;
  584. hovered_index = -1; // Nothing is hovered.
  585. }
  586. };
  587. void EditorPropertyLayers::_grid_changed(uint32_t p_grid) {
  588. emit_changed(get_edited_property(), p_grid);
  589. }
  590. void EditorPropertyLayers::update_property() {
  591. uint32_t value = get_edited_object()->get(get_edited_property());
  592. grid->set_flag(value);
  593. }
  594. void EditorPropertyLayers::setup(LayerType p_layer_type) {
  595. String basename;
  596. switch (p_layer_type) {
  597. case LAYER_RENDER_2D:
  598. basename = "layer_names/2d_render";
  599. break;
  600. case LAYER_PHYSICS_2D:
  601. basename = "layer_names/2d_physics";
  602. break;
  603. case LAYER_RENDER_3D:
  604. basename = "layer_names/3d_render";
  605. break;
  606. case LAYER_PHYSICS_3D:
  607. basename = "layer_names/3d_physics";
  608. break;
  609. }
  610. Vector<String> names;
  611. Vector<String> tooltips;
  612. for (int i = 0; i < 20; i++) {
  613. String name;
  614. if (ProjectSettings::get_singleton()->has_setting(basename + vformat("/layer_%d", i))) {
  615. name = ProjectSettings::get_singleton()->get(basename + vformat("/layer_%d", i));
  616. }
  617. if (name == "") {
  618. name = vformat(TTR("Layer %d"), i);
  619. }
  620. names.push_back(name);
  621. tooltips.push_back(name + "\n" + vformat(TTR("Bit %d, value %d"), i, 1 << i));
  622. }
  623. grid->names = names;
  624. grid->tooltips = tooltips;
  625. }
  626. void EditorPropertyLayers::_button_pressed() {
  627. layers->clear();
  628. for (int i = 0; i < 20; i++) {
  629. if (i == 5 || i == 10 || i == 15) {
  630. layers->add_separator();
  631. }
  632. layers->add_check_item(grid->names[i], i);
  633. int idx = layers->get_item_index(i);
  634. layers->set_item_checked(idx, grid->value & (1 << i));
  635. }
  636. Rect2 gp = button->get_screen_rect();
  637. layers->set_as_minsize();
  638. Vector2 popup_pos = gp.position - Vector2(layers->get_contents_minimum_size().x, 0);
  639. layers->set_position(popup_pos);
  640. layers->popup();
  641. }
  642. void EditorPropertyLayers::_menu_pressed(int p_menu) {
  643. if (grid->value & (1 << p_menu)) {
  644. grid->value &= ~(1 << p_menu);
  645. } else {
  646. grid->value |= (1 << p_menu);
  647. }
  648. grid->update();
  649. layers->set_item_checked(layers->get_item_index(p_menu), grid->value & (1 << p_menu));
  650. _grid_changed(grid->value);
  651. }
  652. void EditorPropertyLayers::_bind_methods() {
  653. }
  654. EditorPropertyLayers::EditorPropertyLayers() {
  655. HBoxContainer *hb = memnew(HBoxContainer);
  656. add_child(hb);
  657. grid = memnew(EditorPropertyLayersGrid);
  658. grid->connect("flag_changed", callable_mp(this, &EditorPropertyLayers::_grid_changed));
  659. grid->set_h_size_flags(SIZE_EXPAND_FILL);
  660. hb->add_child(grid);
  661. button = memnew(Button);
  662. button->set_toggle_mode(true);
  663. button->set_text("...");
  664. button->connect("pressed", callable_mp(this, &EditorPropertyLayers::_button_pressed));
  665. hb->add_child(button);
  666. set_bottom_editor(hb);
  667. layers = memnew(PopupMenu);
  668. add_child(layers);
  669. layers->set_hide_on_checkable_item_selection(false);
  670. layers->connect("id_pressed", callable_mp(this, &EditorPropertyLayers::_menu_pressed));
  671. layers->connect("popup_hide", callable_mp((BaseButton *)button, &BaseButton::set_pressed), varray(false));
  672. }
  673. ///////////////////// INT /////////////////////////
  674. void EditorPropertyInteger::_value_changed(int64_t val) {
  675. if (setting) {
  676. return;
  677. }
  678. emit_changed(get_edited_property(), val);
  679. }
  680. void EditorPropertyInteger::update_property() {
  681. int64_t val = get_edited_object()->get(get_edited_property());
  682. setting = true;
  683. spin->set_value(val);
  684. setting = false;
  685. #ifdef DEBUG_ENABLED
  686. // If spin (currently EditorSplinSlider : Range) is changed so that it can use int64_t, then the below warning wouldn't be a problem.
  687. if (val != (int64_t)(double)(val)) {
  688. WARN_PRINT("Cannot reliably represent '" + itos(val) + "' in the inspector, value is too large.");
  689. }
  690. #endif
  691. }
  692. void EditorPropertyInteger::_bind_methods() {
  693. }
  694. void EditorPropertyInteger::setup(int64_t p_min, int64_t p_max, int64_t p_step, bool p_allow_greater, bool p_allow_lesser) {
  695. spin->set_min(p_min);
  696. spin->set_max(p_max);
  697. spin->set_step(p_step);
  698. spin->set_allow_greater(p_allow_greater);
  699. spin->set_allow_lesser(p_allow_lesser);
  700. }
  701. EditorPropertyInteger::EditorPropertyInteger() {
  702. spin = memnew(EditorSpinSlider);
  703. spin->set_flat(true);
  704. add_child(spin);
  705. add_focusable(spin);
  706. spin->connect("value_changed", callable_mp(this, &EditorPropertyInteger::_value_changed));
  707. setting = false;
  708. }
  709. ///////////////////// OBJECT ID /////////////////////////
  710. void EditorPropertyObjectID::_edit_pressed() {
  711. emit_signal("object_id_selected", get_edited_property(), get_edited_object()->get(get_edited_property()));
  712. }
  713. void EditorPropertyObjectID::update_property() {
  714. String type = base_type;
  715. if (type == "") {
  716. type = "Object";
  717. }
  718. ObjectID id = get_edited_object()->get(get_edited_property());
  719. if (id.is_valid()) {
  720. edit->set_text(type + " ID: " + itos(id));
  721. edit->set_disabled(false);
  722. edit->set_icon(EditorNode::get_singleton()->get_class_icon(type));
  723. } else {
  724. edit->set_text(TTR("[Empty]"));
  725. edit->set_disabled(true);
  726. edit->set_icon(Ref<Texture2D>());
  727. }
  728. }
  729. void EditorPropertyObjectID::setup(const String &p_base_type) {
  730. base_type = p_base_type;
  731. }
  732. void EditorPropertyObjectID::_bind_methods() {
  733. }
  734. EditorPropertyObjectID::EditorPropertyObjectID() {
  735. edit = memnew(Button);
  736. add_child(edit);
  737. add_focusable(edit);
  738. edit->connect("pressed", callable_mp(this, &EditorPropertyObjectID::_edit_pressed));
  739. }
  740. ///////////////////// FLOAT /////////////////////////
  741. void EditorPropertyFloat::_value_changed(double val) {
  742. if (setting) {
  743. return;
  744. }
  745. emit_changed(get_edited_property(), val);
  746. }
  747. void EditorPropertyFloat::update_property() {
  748. double val = get_edited_object()->get(get_edited_property());
  749. setting = true;
  750. spin->set_value(val);
  751. setting = false;
  752. }
  753. void EditorPropertyFloat::_bind_methods() {
  754. }
  755. 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) {
  756. spin->set_min(p_min);
  757. spin->set_max(p_max);
  758. spin->set_step(p_step);
  759. spin->set_hide_slider(p_no_slider);
  760. spin->set_exp_ratio(p_exp_range);
  761. spin->set_allow_greater(p_greater);
  762. spin->set_allow_lesser(p_lesser);
  763. }
  764. EditorPropertyFloat::EditorPropertyFloat() {
  765. spin = memnew(EditorSpinSlider);
  766. spin->set_flat(true);
  767. add_child(spin);
  768. add_focusable(spin);
  769. spin->connect("value_changed", callable_mp(this, &EditorPropertyFloat::_value_changed));
  770. setting = false;
  771. }
  772. ///////////////////// EASING /////////////////////////
  773. void EditorPropertyEasing::_drag_easing(const Ref<InputEvent> &p_ev) {
  774. const Ref<InputEventMouseButton> mb = p_ev;
  775. if (mb.is_valid()) {
  776. if (mb->is_doubleclick() && mb->get_button_index() == BUTTON_LEFT) {
  777. _setup_spin();
  778. }
  779. if (mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  780. preset->set_position(easing_draw->get_screen_transform().xform(mb->get_position()));
  781. preset->popup();
  782. // Ensure the easing doesn't appear as being dragged
  783. dragging = false;
  784. easing_draw->update();
  785. }
  786. if (mb->get_button_index() == BUTTON_LEFT) {
  787. dragging = mb->is_pressed();
  788. // Update to display the correct dragging color
  789. easing_draw->update();
  790. }
  791. }
  792. const Ref<InputEventMouseMotion> mm = p_ev;
  793. if (mm.is_valid() && mm->get_button_mask() & BUTTON_MASK_LEFT) {
  794. float rel = mm->get_relative().x;
  795. if (rel == 0) {
  796. return;
  797. }
  798. if (flip) {
  799. rel = -rel;
  800. }
  801. float val = get_edited_object()->get(get_edited_property());
  802. bool sg = val < 0;
  803. val = Math::absf(val);
  804. val = Math::log(val) / Math::log((float)2.0);
  805. // Logarithmic space.
  806. val += rel * 0.05;
  807. val = Math::pow(2.0f, val);
  808. if (sg) {
  809. val = -val;
  810. }
  811. // 0 is a singularity, but both positive and negative values
  812. // are otherwise allowed. Enforce 0+ as workaround.
  813. if (Math::is_zero_approx(val)) {
  814. val = 0.00001;
  815. }
  816. // Limit to a reasonable value to prevent the curve going into infinity,
  817. // which can cause crashes and other issues.
  818. val = CLAMP(val, -1'000'000, 1'000'000);
  819. emit_changed(get_edited_property(), val);
  820. easing_draw->update();
  821. }
  822. }
  823. void EditorPropertyEasing::_draw_easing() {
  824. RID ci = easing_draw->get_canvas_item();
  825. Size2 s = easing_draw->get_size();
  826. const int points = 48;
  827. float prev = 1.0;
  828. const float exp = get_edited_object()->get(get_edited_property());
  829. const Ref<Font> f = get_theme_font("font", "Label");
  830. int font_size = get_theme_font_size("font_size", "Label");
  831. const Color font_color = get_theme_color("font_color", "Label");
  832. Color line_color;
  833. if (dragging) {
  834. line_color = get_theme_color("accent_color", "Editor");
  835. } else {
  836. line_color = get_theme_color("font_color", "Label") * Color(1, 1, 1, 0.9);
  837. }
  838. Vector<Point2> lines;
  839. for (int i = 1; i <= points; i++) {
  840. float ifl = i / float(points);
  841. float iflp = (i - 1) / float(points);
  842. const float h = 1.0 - Math::ease(ifl, exp);
  843. if (flip) {
  844. ifl = 1.0 - ifl;
  845. iflp = 1.0 - iflp;
  846. }
  847. lines.push_back(Point2(ifl * s.width, h * s.height));
  848. lines.push_back(Point2(iflp * s.width, prev * s.height));
  849. prev = h;
  850. }
  851. easing_draw->draw_multiline(lines, line_color, 1.0);
  852. // Draw more decimals for small numbers since higher precision is usually required for fine adjustments.
  853. int decimals;
  854. if (Math::abs(exp) < 0.1 - CMP_EPSILON) {
  855. decimals = 4;
  856. } else if (Math::abs(exp) < 1 - CMP_EPSILON) {
  857. decimals = 3;
  858. } else if (Math::abs(exp) < 10 - CMP_EPSILON) {
  859. decimals = 2;
  860. } else {
  861. decimals = 1;
  862. }
  863. f->draw_string(ci, Point2(10, 10 + f->get_ascent(font_size)), TS->format_number(rtos(exp).pad_decimals(decimals)), HALIGN_LEFT, -1, font_size, font_color);
  864. }
  865. void EditorPropertyEasing::update_property() {
  866. easing_draw->update();
  867. }
  868. void EditorPropertyEasing::_set_preset(int p_preset) {
  869. static const float preset_value[EASING_MAX] = { 0.0, 1.0, 2.0, 0.5, -2.0, -0.5 };
  870. emit_changed(get_edited_property(), preset_value[p_preset]);
  871. easing_draw->update();
  872. }
  873. void EditorPropertyEasing::_setup_spin() {
  874. setting = true;
  875. spin->setup_and_show();
  876. spin->get_line_edit()->set_text(TS->format_number(rtos(get_edited_object()->get(get_edited_property()))));
  877. setting = false;
  878. spin->show();
  879. }
  880. void EditorPropertyEasing::_spin_value_changed(double p_value) {
  881. if (setting) {
  882. return;
  883. }
  884. // 0 is a singularity, but both positive and negative values
  885. // are otherwise allowed. Enforce 0+ as workaround.
  886. if (Math::is_zero_approx(p_value)) {
  887. p_value = 0.00001;
  888. }
  889. // Limit to a reasonable value to prevent the curve going into infinity,
  890. // which can cause crashes and other issues.
  891. p_value = CLAMP(p_value, -1'000'000, 1'000'000);
  892. emit_changed(get_edited_property(), p_value);
  893. _spin_focus_exited();
  894. }
  895. void EditorPropertyEasing::_spin_focus_exited() {
  896. spin->hide();
  897. // Ensure the easing doesn't appear as being dragged
  898. dragging = false;
  899. easing_draw->update();
  900. }
  901. void EditorPropertyEasing::setup(bool p_full, bool p_flip) {
  902. flip = p_flip;
  903. full = p_full;
  904. }
  905. void EditorPropertyEasing::_notification(int p_what) {
  906. switch (p_what) {
  907. case NOTIFICATION_THEME_CHANGED:
  908. case NOTIFICATION_ENTER_TREE: {
  909. preset->clear();
  910. preset->add_icon_item(get_theme_icon("CurveConstant", "EditorIcons"), "Zero", EASING_ZERO);
  911. preset->add_icon_item(get_theme_icon("CurveLinear", "EditorIcons"), "Linear", EASING_LINEAR);
  912. preset->add_icon_item(get_theme_icon("CurveIn", "EditorIcons"), "In", EASING_IN);
  913. preset->add_icon_item(get_theme_icon("CurveOut", "EditorIcons"), "Out", EASING_OUT);
  914. if (full) {
  915. preset->add_icon_item(get_theme_icon("CurveInOut", "EditorIcons"), "In-Out", EASING_IN_OUT);
  916. preset->add_icon_item(get_theme_icon("CurveOutIn", "EditorIcons"), "Out-In", EASING_OUT_IN);
  917. }
  918. easing_draw->set_custom_minimum_size(Size2(0, get_theme_font("font", "Label")->get_height(get_theme_font_size("font_size", "Label")) * 2));
  919. } break;
  920. }
  921. }
  922. void EditorPropertyEasing::_bind_methods() {
  923. }
  924. EditorPropertyEasing::EditorPropertyEasing() {
  925. easing_draw = memnew(Control);
  926. easing_draw->connect("draw", callable_mp(this, &EditorPropertyEasing::_draw_easing));
  927. easing_draw->connect("gui_input", callable_mp(this, &EditorPropertyEasing::_drag_easing));
  928. easing_draw->set_default_cursor_shape(Control::CURSOR_MOVE);
  929. add_child(easing_draw);
  930. preset = memnew(PopupMenu);
  931. add_child(preset);
  932. preset->connect("id_pressed", callable_mp(this, &EditorPropertyEasing::_set_preset));
  933. spin = memnew(EditorSpinSlider);
  934. spin->set_flat(true);
  935. spin->set_min(-100);
  936. spin->set_max(100);
  937. spin->set_step(0);
  938. spin->set_hide_slider(true);
  939. spin->set_allow_lesser(true);
  940. spin->set_allow_greater(true);
  941. spin->connect("value_changed", callable_mp(this, &EditorPropertyEasing::_spin_value_changed));
  942. spin->get_line_edit()->connect("focus_exited", callable_mp(this, &EditorPropertyEasing::_spin_focus_exited));
  943. spin->hide();
  944. add_child(spin);
  945. dragging = false;
  946. flip = false;
  947. full = false;
  948. }
  949. ///////////////////// VECTOR2 /////////////////////////
  950. void EditorPropertyVector2::_value_changed(double val, const String &p_name) {
  951. if (setting) {
  952. return;
  953. }
  954. Vector2 v2;
  955. v2.x = spin[0]->get_value();
  956. v2.y = spin[1]->get_value();
  957. emit_changed(get_edited_property(), v2, p_name);
  958. }
  959. void EditorPropertyVector2::update_property() {
  960. Vector2 val = get_edited_object()->get(get_edited_property());
  961. setting = true;
  962. spin[0]->set_value(val.x);
  963. spin[1]->set_value(val.y);
  964. setting = false;
  965. }
  966. void EditorPropertyVector2::_notification(int p_what) {
  967. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  968. Color base = get_theme_color("accent_color", "Editor");
  969. for (int i = 0; i < 2; i++) {
  970. Color c = base;
  971. c.set_hsv(float(i) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  972. spin[i]->set_custom_label_color(true, c);
  973. }
  974. }
  975. }
  976. void EditorPropertyVector2::_bind_methods() {
  977. }
  978. void EditorPropertyVector2::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  979. for (int i = 0; i < 2; i++) {
  980. spin[i]->set_min(p_min);
  981. spin[i]->set_max(p_max);
  982. spin[i]->set_step(p_step);
  983. spin[i]->set_hide_slider(p_no_slider);
  984. spin[i]->set_allow_greater(true);
  985. spin[i]->set_allow_lesser(true);
  986. }
  987. }
  988. EditorPropertyVector2::EditorPropertyVector2(bool p_force_wide) {
  989. bool horizontal = p_force_wide || bool(EDITOR_GET("interface/inspector/horizontal_vector2_editing"));
  990. BoxContainer *bc;
  991. if (p_force_wide) {
  992. bc = memnew(HBoxContainer);
  993. add_child(bc);
  994. } else if (horizontal) {
  995. bc = memnew(HBoxContainer);
  996. add_child(bc);
  997. set_bottom_editor(bc);
  998. } else {
  999. bc = memnew(VBoxContainer);
  1000. add_child(bc);
  1001. }
  1002. static const char *desc[2] = { "x", "y" };
  1003. for (int i = 0; i < 2; i++) {
  1004. spin[i] = memnew(EditorSpinSlider);
  1005. spin[i]->set_flat(true);
  1006. spin[i]->set_label(desc[i]);
  1007. bc->add_child(spin[i]);
  1008. add_focusable(spin[i]);
  1009. spin[i]->connect("value_changed", callable_mp(this, &EditorPropertyVector2::_value_changed), varray(desc[i]));
  1010. if (horizontal) {
  1011. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1012. }
  1013. }
  1014. if (!horizontal) {
  1015. set_label_reference(spin[0]); //show text and buttons around this
  1016. }
  1017. setting = false;
  1018. }
  1019. ///////////////////// RECT2 /////////////////////////
  1020. void EditorPropertyRect2::_value_changed(double val, const String &p_name) {
  1021. if (setting) {
  1022. return;
  1023. }
  1024. Rect2 r2;
  1025. r2.position.x = spin[0]->get_value();
  1026. r2.position.y = spin[1]->get_value();
  1027. r2.size.x = spin[2]->get_value();
  1028. r2.size.y = spin[3]->get_value();
  1029. emit_changed(get_edited_property(), r2, p_name);
  1030. }
  1031. void EditorPropertyRect2::update_property() {
  1032. Rect2 val = get_edited_object()->get(get_edited_property());
  1033. setting = true;
  1034. spin[0]->set_value(val.position.x);
  1035. spin[1]->set_value(val.position.y);
  1036. spin[2]->set_value(val.size.x);
  1037. spin[3]->set_value(val.size.y);
  1038. setting = false;
  1039. }
  1040. void EditorPropertyRect2::_notification(int p_what) {
  1041. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1042. Color base = get_theme_color("accent_color", "Editor");
  1043. for (int i = 0; i < 4; i++) {
  1044. Color c = base;
  1045. c.set_hsv(float(i % 2) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1046. spin[i]->set_custom_label_color(true, c);
  1047. }
  1048. }
  1049. }
  1050. void EditorPropertyRect2::_bind_methods() {
  1051. }
  1052. void EditorPropertyRect2::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1053. for (int i = 0; i < 4; i++) {
  1054. spin[i]->set_min(p_min);
  1055. spin[i]->set_max(p_max);
  1056. spin[i]->set_step(p_step);
  1057. spin[i]->set_hide_slider(p_no_slider);
  1058. spin[i]->set_allow_greater(true);
  1059. spin[i]->set_allow_lesser(true);
  1060. }
  1061. }
  1062. EditorPropertyRect2::EditorPropertyRect2(bool p_force_wide) {
  1063. bool horizontal = p_force_wide || bool(EDITOR_GET("interface/inspector/horizontal_vector_types_editing"));
  1064. bool grid = false;
  1065. BoxContainer *bc;
  1066. if (p_force_wide) {
  1067. bc = memnew(HBoxContainer);
  1068. add_child(bc);
  1069. } else if (horizontal) {
  1070. bc = memnew(VBoxContainer);
  1071. add_child(bc);
  1072. set_bottom_editor(bc);
  1073. bc->add_child(memnew(HBoxContainer));
  1074. bc->add_child(memnew(HBoxContainer));
  1075. grid = true;
  1076. } else {
  1077. bc = memnew(VBoxContainer);
  1078. add_child(bc);
  1079. }
  1080. static const char *desc[4] = { "x", "y", "w", "h" };
  1081. for (int i = 0; i < 4; i++) {
  1082. spin[i] = memnew(EditorSpinSlider);
  1083. spin[i]->set_label(desc[i]);
  1084. spin[i]->set_flat(true);
  1085. if (grid) {
  1086. bc->get_child(i / 2)->add_child(spin[i]);
  1087. } else {
  1088. bc->add_child(spin[i]);
  1089. }
  1090. add_focusable(spin[i]);
  1091. spin[i]->connect("value_changed", callable_mp(this, &EditorPropertyRect2::_value_changed), varray(desc[i]));
  1092. if (horizontal) {
  1093. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1094. }
  1095. }
  1096. if (!horizontal) {
  1097. set_label_reference(spin[0]); //show text and buttons around this
  1098. }
  1099. setting = false;
  1100. }
  1101. ///////////////////// VECTOR3 /////////////////////////
  1102. void EditorPropertyVector3::_value_changed(double val, const String &p_name) {
  1103. if (setting) {
  1104. return;
  1105. }
  1106. Vector3 v3;
  1107. v3.x = spin[0]->get_value();
  1108. v3.y = spin[1]->get_value();
  1109. v3.z = spin[2]->get_value();
  1110. emit_changed(get_edited_property(), v3, p_name);
  1111. }
  1112. void EditorPropertyVector3::update_property() {
  1113. update_using_vector(get_edited_object()->get(get_edited_property()));
  1114. }
  1115. void EditorPropertyVector3::update_using_vector(Vector3 p_vector) {
  1116. setting = true;
  1117. spin[0]->set_value(p_vector.x);
  1118. spin[1]->set_value(p_vector.y);
  1119. spin[2]->set_value(p_vector.z);
  1120. setting = false;
  1121. }
  1122. Vector3 EditorPropertyVector3::get_vector() {
  1123. Vector3 v3;
  1124. v3.x = spin[0]->get_value();
  1125. v3.y = spin[1]->get_value();
  1126. v3.z = spin[2]->get_value();
  1127. return v3;
  1128. }
  1129. void EditorPropertyVector3::_notification(int p_what) {
  1130. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1131. Color base = get_theme_color("accent_color", "Editor");
  1132. for (int i = 0; i < 3; i++) {
  1133. Color c = base;
  1134. c.set_hsv(float(i) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1135. spin[i]->set_custom_label_color(true, c);
  1136. }
  1137. }
  1138. }
  1139. void EditorPropertyVector3::_bind_methods() {
  1140. }
  1141. void EditorPropertyVector3::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1142. for (int i = 0; i < 3; i++) {
  1143. spin[i]->set_min(p_min);
  1144. spin[i]->set_max(p_max);
  1145. spin[i]->set_step(p_step);
  1146. spin[i]->set_hide_slider(p_no_slider);
  1147. spin[i]->set_allow_greater(true);
  1148. spin[i]->set_allow_lesser(true);
  1149. }
  1150. }
  1151. EditorPropertyVector3::EditorPropertyVector3(bool p_force_wide) {
  1152. bool horizontal = p_force_wide || bool(EDITOR_GET("interface/inspector/horizontal_vector_types_editing"));
  1153. BoxContainer *bc;
  1154. if (p_force_wide) {
  1155. bc = memnew(HBoxContainer);
  1156. add_child(bc);
  1157. } else if (horizontal) {
  1158. bc = memnew(HBoxContainer);
  1159. add_child(bc);
  1160. set_bottom_editor(bc);
  1161. } else {
  1162. bc = memnew(VBoxContainer);
  1163. add_child(bc);
  1164. }
  1165. static const char *desc[3] = { "x", "y", "z" };
  1166. for (int i = 0; i < 3; i++) {
  1167. spin[i] = memnew(EditorSpinSlider);
  1168. spin[i]->set_flat(true);
  1169. spin[i]->set_label(desc[i]);
  1170. bc->add_child(spin[i]);
  1171. add_focusable(spin[i]);
  1172. spin[i]->connect("value_changed", callable_mp(this, &EditorPropertyVector3::_value_changed), varray(desc[i]));
  1173. if (horizontal) {
  1174. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1175. }
  1176. }
  1177. if (!horizontal) {
  1178. set_label_reference(spin[0]); //show text and buttons around this
  1179. }
  1180. setting = false;
  1181. }
  1182. ///////////////////// VECTOR2i /////////////////////////
  1183. void EditorPropertyVector2i::_value_changed(double val, const String &p_name) {
  1184. if (setting) {
  1185. return;
  1186. }
  1187. Vector2i v2;
  1188. v2.x = spin[0]->get_value();
  1189. v2.y = spin[1]->get_value();
  1190. emit_changed(get_edited_property(), v2, p_name);
  1191. }
  1192. void EditorPropertyVector2i::update_property() {
  1193. Vector2i val = get_edited_object()->get(get_edited_property());
  1194. setting = true;
  1195. spin[0]->set_value(val.x);
  1196. spin[1]->set_value(val.y);
  1197. setting = false;
  1198. }
  1199. void EditorPropertyVector2i::_notification(int p_what) {
  1200. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1201. Color base = get_theme_color("accent_color", "Editor");
  1202. for (int i = 0; i < 2; i++) {
  1203. Color c = base;
  1204. c.set_hsv(float(i) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1205. spin[i]->set_custom_label_color(true, c);
  1206. }
  1207. }
  1208. }
  1209. void EditorPropertyVector2i::_bind_methods() {
  1210. }
  1211. void EditorPropertyVector2i::setup(int p_min, int p_max, bool p_no_slider) {
  1212. for (int i = 0; i < 2; i++) {
  1213. spin[i]->set_min(p_min);
  1214. spin[i]->set_max(p_max);
  1215. spin[i]->set_step(1);
  1216. spin[i]->set_hide_slider(p_no_slider);
  1217. spin[i]->set_allow_greater(true);
  1218. spin[i]->set_allow_lesser(true);
  1219. }
  1220. }
  1221. EditorPropertyVector2i::EditorPropertyVector2i(bool p_force_wide) {
  1222. bool horizontal = p_force_wide || bool(EDITOR_GET("interface/inspector/horizontal_vector2_editing"));
  1223. BoxContainer *bc;
  1224. if (p_force_wide) {
  1225. bc = memnew(HBoxContainer);
  1226. add_child(bc);
  1227. } else if (horizontal) {
  1228. bc = memnew(HBoxContainer);
  1229. add_child(bc);
  1230. set_bottom_editor(bc);
  1231. } else {
  1232. bc = memnew(VBoxContainer);
  1233. add_child(bc);
  1234. }
  1235. static const char *desc[2] = { "x", "y" };
  1236. for (int i = 0; i < 2; i++) {
  1237. spin[i] = memnew(EditorSpinSlider);
  1238. spin[i]->set_flat(true);
  1239. spin[i]->set_label(desc[i]);
  1240. bc->add_child(spin[i]);
  1241. add_focusable(spin[i]);
  1242. spin[i]->connect("value_changed", callable_mp(this, &EditorPropertyVector2i::_value_changed), varray(desc[i]));
  1243. if (horizontal) {
  1244. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1245. }
  1246. }
  1247. if (!horizontal) {
  1248. set_label_reference(spin[0]); //show text and buttons around this
  1249. }
  1250. setting = false;
  1251. }
  1252. ///////////////////// RECT2i /////////////////////////
  1253. void EditorPropertyRect2i::_value_changed(double val, const String &p_name) {
  1254. if (setting) {
  1255. return;
  1256. }
  1257. Rect2i r2;
  1258. r2.position.x = spin[0]->get_value();
  1259. r2.position.y = spin[1]->get_value();
  1260. r2.size.x = spin[2]->get_value();
  1261. r2.size.y = spin[3]->get_value();
  1262. emit_changed(get_edited_property(), r2, p_name);
  1263. }
  1264. void EditorPropertyRect2i::update_property() {
  1265. Rect2i val = get_edited_object()->get(get_edited_property());
  1266. setting = true;
  1267. spin[0]->set_value(val.position.x);
  1268. spin[1]->set_value(val.position.y);
  1269. spin[2]->set_value(val.size.x);
  1270. spin[3]->set_value(val.size.y);
  1271. setting = false;
  1272. }
  1273. void EditorPropertyRect2i::_notification(int p_what) {
  1274. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1275. Color base = get_theme_color("accent_color", "Editor");
  1276. for (int i = 0; i < 4; i++) {
  1277. Color c = base;
  1278. c.set_hsv(float(i % 2) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1279. spin[i]->set_custom_label_color(true, c);
  1280. }
  1281. }
  1282. }
  1283. void EditorPropertyRect2i::_bind_methods() {
  1284. }
  1285. void EditorPropertyRect2i::setup(int p_min, int p_max, bool p_no_slider) {
  1286. for (int i = 0; i < 4; i++) {
  1287. spin[i]->set_min(p_min);
  1288. spin[i]->set_max(p_max);
  1289. spin[i]->set_step(1);
  1290. spin[i]->set_hide_slider(p_no_slider);
  1291. spin[i]->set_allow_greater(true);
  1292. spin[i]->set_allow_lesser(true);
  1293. }
  1294. }
  1295. EditorPropertyRect2i::EditorPropertyRect2i(bool p_force_wide) {
  1296. bool horizontal = p_force_wide || bool(EDITOR_GET("interface/inspector/horizontal_vector_types_editing"));
  1297. bool grid = false;
  1298. BoxContainer *bc;
  1299. if (p_force_wide) {
  1300. bc = memnew(HBoxContainer);
  1301. add_child(bc);
  1302. } else if (horizontal) {
  1303. bc = memnew(VBoxContainer);
  1304. add_child(bc);
  1305. set_bottom_editor(bc);
  1306. bc->add_child(memnew(HBoxContainer));
  1307. bc->add_child(memnew(HBoxContainer));
  1308. grid = true;
  1309. } else {
  1310. bc = memnew(VBoxContainer);
  1311. add_child(bc);
  1312. }
  1313. static const char *desc[4] = { "x", "y", "w", "h" };
  1314. for (int i = 0; i < 4; i++) {
  1315. spin[i] = memnew(EditorSpinSlider);
  1316. spin[i]->set_label(desc[i]);
  1317. spin[i]->set_flat(true);
  1318. if (grid) {
  1319. bc->get_child(i / 2)->add_child(spin[i]);
  1320. } else {
  1321. bc->add_child(spin[i]);
  1322. }
  1323. add_focusable(spin[i]);
  1324. spin[i]->connect("value_changed", callable_mp(this, &EditorPropertyRect2i::_value_changed), varray(desc[i]));
  1325. if (horizontal) {
  1326. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1327. }
  1328. }
  1329. if (!horizontal) {
  1330. set_label_reference(spin[0]); //show text and buttons around this
  1331. }
  1332. setting = false;
  1333. }
  1334. ///////////////////// VECTOR3i /////////////////////////
  1335. void EditorPropertyVector3i::_value_changed(double val, const String &p_name) {
  1336. if (setting) {
  1337. return;
  1338. }
  1339. Vector3i v3;
  1340. v3.x = spin[0]->get_value();
  1341. v3.y = spin[1]->get_value();
  1342. v3.z = spin[2]->get_value();
  1343. emit_changed(get_edited_property(), v3, p_name);
  1344. }
  1345. void EditorPropertyVector3i::update_property() {
  1346. Vector3i val = get_edited_object()->get(get_edited_property());
  1347. setting = true;
  1348. spin[0]->set_value(val.x);
  1349. spin[1]->set_value(val.y);
  1350. spin[2]->set_value(val.z);
  1351. setting = false;
  1352. }
  1353. void EditorPropertyVector3i::_notification(int p_what) {
  1354. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1355. Color base = get_theme_color("accent_color", "Editor");
  1356. for (int i = 0; i < 3; i++) {
  1357. Color c = base;
  1358. c.set_hsv(float(i) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1359. spin[i]->set_custom_label_color(true, c);
  1360. }
  1361. }
  1362. }
  1363. void EditorPropertyVector3i::_bind_methods() {
  1364. }
  1365. void EditorPropertyVector3i::setup(int p_min, int p_max, bool p_no_slider) {
  1366. for (int i = 0; i < 3; i++) {
  1367. spin[i]->set_min(p_min);
  1368. spin[i]->set_max(p_max);
  1369. spin[i]->set_step(1);
  1370. spin[i]->set_hide_slider(p_no_slider);
  1371. spin[i]->set_allow_greater(true);
  1372. spin[i]->set_allow_lesser(true);
  1373. }
  1374. }
  1375. EditorPropertyVector3i::EditorPropertyVector3i(bool p_force_wide) {
  1376. bool horizontal = p_force_wide || bool(EDITOR_GET("interface/inspector/horizontal_vector_types_editing"));
  1377. BoxContainer *bc;
  1378. if (p_force_wide) {
  1379. bc = memnew(HBoxContainer);
  1380. add_child(bc);
  1381. } else if (horizontal) {
  1382. bc = memnew(HBoxContainer);
  1383. add_child(bc);
  1384. set_bottom_editor(bc);
  1385. } else {
  1386. bc = memnew(VBoxContainer);
  1387. add_child(bc);
  1388. }
  1389. static const char *desc[3] = { "x", "y", "z" };
  1390. for (int i = 0; i < 3; i++) {
  1391. spin[i] = memnew(EditorSpinSlider);
  1392. spin[i]->set_flat(true);
  1393. spin[i]->set_label(desc[i]);
  1394. bc->add_child(spin[i]);
  1395. add_focusable(spin[i]);
  1396. spin[i]->connect("value_changed", callable_mp(this, &EditorPropertyVector3i::_value_changed), varray(desc[i]));
  1397. if (horizontal) {
  1398. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1399. }
  1400. }
  1401. if (!horizontal) {
  1402. set_label_reference(spin[0]); //show text and buttons around this
  1403. }
  1404. setting = false;
  1405. }
  1406. ///////////////////// PLANE /////////////////////////
  1407. void EditorPropertyPlane::_value_changed(double val, const String &p_name) {
  1408. if (setting) {
  1409. return;
  1410. }
  1411. Plane p;
  1412. p.normal.x = spin[0]->get_value();
  1413. p.normal.y = spin[1]->get_value();
  1414. p.normal.z = spin[2]->get_value();
  1415. p.d = spin[3]->get_value();
  1416. emit_changed(get_edited_property(), p, p_name);
  1417. }
  1418. void EditorPropertyPlane::update_property() {
  1419. Plane val = get_edited_object()->get(get_edited_property());
  1420. setting = true;
  1421. spin[0]->set_value(val.normal.x);
  1422. spin[1]->set_value(val.normal.y);
  1423. spin[2]->set_value(val.normal.z);
  1424. spin[3]->set_value(val.d);
  1425. setting = false;
  1426. }
  1427. void EditorPropertyPlane::_notification(int p_what) {
  1428. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1429. Color base = get_theme_color("accent_color", "Editor");
  1430. for (int i = 0; i < 3; i++) {
  1431. Color c = base;
  1432. c.set_hsv(float(i) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1433. spin[i]->set_custom_label_color(true, c);
  1434. }
  1435. }
  1436. }
  1437. void EditorPropertyPlane::_bind_methods() {
  1438. }
  1439. void EditorPropertyPlane::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1440. for (int i = 0; i < 4; i++) {
  1441. spin[i]->set_min(p_min);
  1442. spin[i]->set_max(p_max);
  1443. spin[i]->set_step(p_step);
  1444. spin[i]->set_hide_slider(p_no_slider);
  1445. spin[i]->set_allow_greater(true);
  1446. spin[i]->set_allow_lesser(true);
  1447. }
  1448. }
  1449. EditorPropertyPlane::EditorPropertyPlane(bool p_force_wide) {
  1450. bool horizontal = p_force_wide || bool(EDITOR_GET("interface/inspector/horizontal_vector_types_editing"));
  1451. BoxContainer *bc;
  1452. if (p_force_wide) {
  1453. bc = memnew(HBoxContainer);
  1454. add_child(bc);
  1455. } else if (horizontal) {
  1456. bc = memnew(HBoxContainer);
  1457. add_child(bc);
  1458. set_bottom_editor(bc);
  1459. } else {
  1460. bc = memnew(VBoxContainer);
  1461. add_child(bc);
  1462. }
  1463. static const char *desc[4] = { "x", "y", "z", "d" };
  1464. for (int i = 0; i < 4; i++) {
  1465. spin[i] = memnew(EditorSpinSlider);
  1466. spin[i]->set_flat(true);
  1467. spin[i]->set_label(desc[i]);
  1468. bc->add_child(spin[i]);
  1469. add_focusable(spin[i]);
  1470. spin[i]->connect("value_changed", callable_mp(this, &EditorPropertyPlane::_value_changed), varray(desc[i]));
  1471. if (horizontal) {
  1472. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1473. }
  1474. }
  1475. if (!horizontal) {
  1476. set_label_reference(spin[0]); //show text and buttons around this
  1477. }
  1478. setting = false;
  1479. }
  1480. ///////////////////// QUAT /////////////////////////
  1481. void EditorPropertyQuat::_value_changed(double val, const String &p_name) {
  1482. if (setting) {
  1483. return;
  1484. }
  1485. Quat p;
  1486. p.x = spin[0]->get_value();
  1487. p.y = spin[1]->get_value();
  1488. p.z = spin[2]->get_value();
  1489. p.w = spin[3]->get_value();
  1490. emit_changed(get_edited_property(), p, p_name);
  1491. }
  1492. void EditorPropertyQuat::update_property() {
  1493. Quat val = get_edited_object()->get(get_edited_property());
  1494. setting = true;
  1495. spin[0]->set_value(val.x);
  1496. spin[1]->set_value(val.y);
  1497. spin[2]->set_value(val.z);
  1498. spin[3]->set_value(val.w);
  1499. setting = false;
  1500. }
  1501. void EditorPropertyQuat::_notification(int p_what) {
  1502. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1503. Color base = get_theme_color("accent_color", "Editor");
  1504. for (int i = 0; i < 3; i++) {
  1505. Color c = base;
  1506. c.set_hsv(float(i) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1507. spin[i]->set_custom_label_color(true, c);
  1508. }
  1509. }
  1510. }
  1511. void EditorPropertyQuat::_bind_methods() {
  1512. }
  1513. void EditorPropertyQuat::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1514. for (int i = 0; i < 4; i++) {
  1515. spin[i]->set_min(p_min);
  1516. spin[i]->set_max(p_max);
  1517. spin[i]->set_step(p_step);
  1518. spin[i]->set_hide_slider(p_no_slider);
  1519. spin[i]->set_allow_greater(true);
  1520. spin[i]->set_allow_lesser(true);
  1521. }
  1522. }
  1523. EditorPropertyQuat::EditorPropertyQuat() {
  1524. bool horizontal = EDITOR_GET("interface/inspector/horizontal_vector_types_editing");
  1525. BoxContainer *bc;
  1526. if (horizontal) {
  1527. bc = memnew(HBoxContainer);
  1528. add_child(bc);
  1529. set_bottom_editor(bc);
  1530. } else {
  1531. bc = memnew(VBoxContainer);
  1532. add_child(bc);
  1533. }
  1534. static const char *desc[4] = { "x", "y", "z", "w" };
  1535. for (int i = 0; i < 4; i++) {
  1536. spin[i] = memnew(EditorSpinSlider);
  1537. spin[i]->set_flat(true);
  1538. spin[i]->set_label(desc[i]);
  1539. bc->add_child(spin[i]);
  1540. add_focusable(spin[i]);
  1541. spin[i]->connect("value_changed", callable_mp(this, &EditorPropertyQuat::_value_changed), varray(desc[i]));
  1542. if (horizontal) {
  1543. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1544. }
  1545. }
  1546. if (!horizontal) {
  1547. set_label_reference(spin[0]); //show text and buttons around this
  1548. }
  1549. setting = false;
  1550. }
  1551. ///////////////////// AABB /////////////////////////
  1552. void EditorPropertyAABB::_value_changed(double val, const String &p_name) {
  1553. if (setting) {
  1554. return;
  1555. }
  1556. AABB p;
  1557. p.position.x = spin[0]->get_value();
  1558. p.position.y = spin[1]->get_value();
  1559. p.position.z = spin[2]->get_value();
  1560. p.size.x = spin[3]->get_value();
  1561. p.size.y = spin[4]->get_value();
  1562. p.size.z = spin[5]->get_value();
  1563. emit_changed(get_edited_property(), p, p_name);
  1564. }
  1565. void EditorPropertyAABB::update_property() {
  1566. AABB val = get_edited_object()->get(get_edited_property());
  1567. setting = true;
  1568. spin[0]->set_value(val.position.x);
  1569. spin[1]->set_value(val.position.y);
  1570. spin[2]->set_value(val.position.z);
  1571. spin[3]->set_value(val.size.x);
  1572. spin[4]->set_value(val.size.y);
  1573. spin[5]->set_value(val.size.z);
  1574. setting = false;
  1575. }
  1576. void EditorPropertyAABB::_notification(int p_what) {
  1577. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1578. Color base = get_theme_color("accent_color", "Editor");
  1579. for (int i = 0; i < 6; i++) {
  1580. Color c = base;
  1581. c.set_hsv(float(i % 3) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1582. spin[i]->set_custom_label_color(true, c);
  1583. }
  1584. }
  1585. }
  1586. void EditorPropertyAABB::_bind_methods() {
  1587. }
  1588. void EditorPropertyAABB::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1589. for (int i = 0; i < 6; i++) {
  1590. spin[i]->set_min(p_min);
  1591. spin[i]->set_max(p_max);
  1592. spin[i]->set_step(p_step);
  1593. spin[i]->set_hide_slider(p_no_slider);
  1594. spin[i]->set_allow_greater(true);
  1595. spin[i]->set_allow_lesser(true);
  1596. }
  1597. }
  1598. EditorPropertyAABB::EditorPropertyAABB() {
  1599. GridContainer *g = memnew(GridContainer);
  1600. g->set_columns(3);
  1601. add_child(g);
  1602. static const char *desc[6] = { "x", "y", "z", "w", "h", "d" };
  1603. for (int i = 0; i < 6; i++) {
  1604. spin[i] = memnew(EditorSpinSlider);
  1605. spin[i]->set_label(desc[i]);
  1606. spin[i]->set_flat(true);
  1607. g->add_child(spin[i]);
  1608. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1609. add_focusable(spin[i]);
  1610. spin[i]->connect("value_changed", callable_mp(this, &EditorPropertyAABB::_value_changed), varray(desc[i]));
  1611. }
  1612. set_bottom_editor(g);
  1613. setting = false;
  1614. }
  1615. ///////////////////// TRANSFORM2D /////////////////////////
  1616. void EditorPropertyTransform2D::_value_changed(double val, const String &p_name) {
  1617. if (setting) {
  1618. return;
  1619. }
  1620. Transform2D p;
  1621. p[0][0] = spin[0]->get_value();
  1622. p[0][1] = spin[1]->get_value();
  1623. p[1][0] = spin[2]->get_value();
  1624. p[1][1] = spin[3]->get_value();
  1625. p[2][0] = spin[4]->get_value();
  1626. p[2][1] = spin[5]->get_value();
  1627. emit_changed(get_edited_property(), p, p_name);
  1628. }
  1629. void EditorPropertyTransform2D::update_property() {
  1630. Transform2D val = get_edited_object()->get(get_edited_property());
  1631. setting = true;
  1632. spin[0]->set_value(val[0][0]);
  1633. spin[1]->set_value(val[0][1]);
  1634. spin[2]->set_value(val[1][0]);
  1635. spin[3]->set_value(val[1][1]);
  1636. spin[4]->set_value(val[2][0]);
  1637. spin[5]->set_value(val[2][1]);
  1638. setting = false;
  1639. }
  1640. void EditorPropertyTransform2D::_notification(int p_what) {
  1641. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1642. Color base = get_theme_color("accent_color", "Editor");
  1643. for (int i = 0; i < 6; i++) {
  1644. Color c = base;
  1645. c.set_hsv(float(i % 2) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1646. spin[i]->set_custom_label_color(true, c);
  1647. }
  1648. }
  1649. }
  1650. void EditorPropertyTransform2D::_bind_methods() {
  1651. }
  1652. void EditorPropertyTransform2D::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1653. for (int i = 0; i < 6; i++) {
  1654. spin[i]->set_min(p_min);
  1655. spin[i]->set_max(p_max);
  1656. spin[i]->set_step(p_step);
  1657. spin[i]->set_hide_slider(p_no_slider);
  1658. spin[i]->set_allow_greater(true);
  1659. spin[i]->set_allow_lesser(true);
  1660. }
  1661. }
  1662. EditorPropertyTransform2D::EditorPropertyTransform2D() {
  1663. GridContainer *g = memnew(GridContainer);
  1664. g->set_columns(2);
  1665. add_child(g);
  1666. static const char *desc[6] = { "x", "y", "x", "y", "x", "y" };
  1667. for (int i = 0; i < 6; i++) {
  1668. spin[i] = memnew(EditorSpinSlider);
  1669. spin[i]->set_label(desc[i]);
  1670. spin[i]->set_flat(true);
  1671. g->add_child(spin[i]);
  1672. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1673. add_focusable(spin[i]);
  1674. spin[i]->connect("value_changed", callable_mp(this, &EditorPropertyTransform2D::_value_changed), varray(desc[i]));
  1675. }
  1676. set_bottom_editor(g);
  1677. setting = false;
  1678. }
  1679. ///////////////////// BASIS /////////////////////////
  1680. void EditorPropertyBasis::_value_changed(double val, const String &p_name) {
  1681. if (setting) {
  1682. return;
  1683. }
  1684. Basis p;
  1685. p[0][0] = spin[0]->get_value();
  1686. p[1][0] = spin[1]->get_value();
  1687. p[2][0] = spin[2]->get_value();
  1688. p[0][1] = spin[3]->get_value();
  1689. p[1][1] = spin[4]->get_value();
  1690. p[2][1] = spin[5]->get_value();
  1691. p[0][2] = spin[6]->get_value();
  1692. p[1][2] = spin[7]->get_value();
  1693. p[2][2] = spin[8]->get_value();
  1694. emit_changed(get_edited_property(), p, p_name);
  1695. }
  1696. void EditorPropertyBasis::update_property() {
  1697. Basis val = get_edited_object()->get(get_edited_property());
  1698. setting = true;
  1699. spin[0]->set_value(val[0][0]);
  1700. spin[1]->set_value(val[1][0]);
  1701. spin[2]->set_value(val[2][0]);
  1702. spin[3]->set_value(val[0][1]);
  1703. spin[4]->set_value(val[1][1]);
  1704. spin[5]->set_value(val[2][1]);
  1705. spin[6]->set_value(val[0][2]);
  1706. spin[7]->set_value(val[1][2]);
  1707. spin[8]->set_value(val[2][2]);
  1708. setting = false;
  1709. }
  1710. void EditorPropertyBasis::_notification(int p_what) {
  1711. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1712. Color base = get_theme_color("accent_color", "Editor");
  1713. for (int i = 0; i < 9; i++) {
  1714. Color c = base;
  1715. c.set_hsv(float(i % 3) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1716. spin[i]->set_custom_label_color(true, c);
  1717. }
  1718. }
  1719. }
  1720. void EditorPropertyBasis::_bind_methods() {
  1721. }
  1722. void EditorPropertyBasis::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1723. for (int i = 0; i < 9; i++) {
  1724. spin[i]->set_min(p_min);
  1725. spin[i]->set_max(p_max);
  1726. spin[i]->set_step(p_step);
  1727. spin[i]->set_hide_slider(p_no_slider);
  1728. spin[i]->set_allow_greater(true);
  1729. spin[i]->set_allow_lesser(true);
  1730. }
  1731. }
  1732. EditorPropertyBasis::EditorPropertyBasis() {
  1733. GridContainer *g = memnew(GridContainer);
  1734. g->set_columns(3);
  1735. add_child(g);
  1736. static const char *desc[9] = { "x", "y", "z", "x", "y", "z", "x", "y", "z" };
  1737. for (int i = 0; i < 9; i++) {
  1738. spin[i] = memnew(EditorSpinSlider);
  1739. spin[i]->set_label(desc[i]);
  1740. spin[i]->set_flat(true);
  1741. g->add_child(spin[i]);
  1742. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1743. add_focusable(spin[i]);
  1744. spin[i]->connect("value_changed", callable_mp(this, &EditorPropertyBasis::_value_changed), varray(desc[i]));
  1745. }
  1746. set_bottom_editor(g);
  1747. setting = false;
  1748. }
  1749. ///////////////////// TRANSFORM /////////////////////////
  1750. void EditorPropertyTransform::_value_changed(double val, const String &p_name) {
  1751. if (setting) {
  1752. return;
  1753. }
  1754. Transform p;
  1755. p.basis[0][0] = spin[0]->get_value();
  1756. p.basis[1][0] = spin[1]->get_value();
  1757. p.basis[2][0] = spin[2]->get_value();
  1758. p.basis[0][1] = spin[3]->get_value();
  1759. p.basis[1][1] = spin[4]->get_value();
  1760. p.basis[2][1] = spin[5]->get_value();
  1761. p.basis[0][2] = spin[6]->get_value();
  1762. p.basis[1][2] = spin[7]->get_value();
  1763. p.basis[2][2] = spin[8]->get_value();
  1764. p.origin[0] = spin[9]->get_value();
  1765. p.origin[1] = spin[10]->get_value();
  1766. p.origin[2] = spin[11]->get_value();
  1767. emit_changed(get_edited_property(), p, p_name);
  1768. }
  1769. void EditorPropertyTransform::update_property() {
  1770. update_using_transform(get_edited_object()->get(get_edited_property()));
  1771. }
  1772. void EditorPropertyTransform::update_using_transform(Transform p_transform) {
  1773. setting = true;
  1774. spin[0]->set_value(p_transform.basis[0][0]);
  1775. spin[1]->set_value(p_transform.basis[1][0]);
  1776. spin[2]->set_value(p_transform.basis[2][0]);
  1777. spin[3]->set_value(p_transform.basis[0][1]);
  1778. spin[4]->set_value(p_transform.basis[1][1]);
  1779. spin[5]->set_value(p_transform.basis[2][1]);
  1780. spin[6]->set_value(p_transform.basis[0][2]);
  1781. spin[7]->set_value(p_transform.basis[1][2]);
  1782. spin[8]->set_value(p_transform.basis[2][2]);
  1783. spin[9]->set_value(p_transform.origin[0]);
  1784. spin[10]->set_value(p_transform.origin[1]);
  1785. spin[11]->set_value(p_transform.origin[2]);
  1786. setting = false;
  1787. }
  1788. void EditorPropertyTransform::_notification(int p_what) {
  1789. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1790. Color base = get_theme_color("accent_color", "Editor");
  1791. for (int i = 0; i < 12; i++) {
  1792. Color c = base;
  1793. c.set_hsv(float(i % 3) / 3.0 + 0.05, c.get_s() * 0.75, c.get_v());
  1794. spin[i]->set_custom_label_color(true, c);
  1795. }
  1796. }
  1797. }
  1798. void EditorPropertyTransform::_bind_methods() {
  1799. }
  1800. void EditorPropertyTransform::setup(double p_min, double p_max, double p_step, bool p_no_slider) {
  1801. for (int i = 0; i < 12; i++) {
  1802. spin[i]->set_min(p_min);
  1803. spin[i]->set_max(p_max);
  1804. spin[i]->set_step(p_step);
  1805. spin[i]->set_hide_slider(p_no_slider);
  1806. spin[i]->set_allow_greater(true);
  1807. spin[i]->set_allow_lesser(true);
  1808. }
  1809. }
  1810. EditorPropertyTransform::EditorPropertyTransform() {
  1811. GridContainer *g = memnew(GridContainer);
  1812. g->set_columns(3);
  1813. add_child(g);
  1814. static const char *desc[12] = { "x", "y", "z", "x", "y", "z", "x", "y", "z", "x", "y", "z" };
  1815. for (int i = 0; i < 12; i++) {
  1816. spin[i] = memnew(EditorSpinSlider);
  1817. spin[i]->set_label(desc[i]);
  1818. spin[i]->set_flat(true);
  1819. g->add_child(spin[i]);
  1820. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1821. add_focusable(spin[i]);
  1822. spin[i]->connect("value_changed", callable_mp(this, &EditorPropertyTransform::_value_changed), varray(desc[i]));
  1823. }
  1824. set_bottom_editor(g);
  1825. setting = false;
  1826. }
  1827. ////////////// COLOR PICKER //////////////////////
  1828. void EditorPropertyColor::_color_changed(const Color &p_color) {
  1829. // Cancel the color change if the current color is identical to the new one.
  1830. if (get_edited_object()->get(get_edited_property()) == p_color) {
  1831. return;
  1832. }
  1833. emit_changed(get_edited_property(), p_color, "", true);
  1834. }
  1835. void EditorPropertyColor::_popup_closed() {
  1836. if (picker->get_pick_color() != last_color) {
  1837. emit_changed(get_edited_property(), picker->get_pick_color(), "", false);
  1838. }
  1839. }
  1840. void EditorPropertyColor::_picker_created() {
  1841. // get default color picker mode from editor settings
  1842. int default_color_mode = EDITOR_GET("interface/inspector/default_color_picker_mode");
  1843. if (default_color_mode == 1) {
  1844. picker->get_picker()->set_hsv_mode(true);
  1845. } else if (default_color_mode == 2) {
  1846. picker->get_picker()->set_raw_mode(true);
  1847. }
  1848. }
  1849. void EditorPropertyColor::_picker_opening() {
  1850. last_color = picker->get_pick_color();
  1851. }
  1852. void EditorPropertyColor::_bind_methods() {
  1853. }
  1854. void EditorPropertyColor::update_property() {
  1855. picker->set_pick_color(get_edited_object()->get(get_edited_property()));
  1856. const Color color = picker->get_pick_color();
  1857. // Add a tooltip to display each channel's values without having to click the ColorPickerButton
  1858. if (picker->is_editing_alpha()) {
  1859. picker->set_tooltip(vformat(
  1860. "R: %s\nG: %s\nB: %s\nA: %s",
  1861. rtos(color.r).pad_decimals(2),
  1862. rtos(color.g).pad_decimals(2),
  1863. rtos(color.b).pad_decimals(2),
  1864. rtos(color.a).pad_decimals(2)));
  1865. } else {
  1866. picker->set_tooltip(vformat(
  1867. "R: %s\nG: %s\nB: %s",
  1868. rtos(color.r).pad_decimals(2),
  1869. rtos(color.g).pad_decimals(2),
  1870. rtos(color.b).pad_decimals(2)));
  1871. }
  1872. }
  1873. void EditorPropertyColor::setup(bool p_show_alpha) {
  1874. picker->set_edit_alpha(p_show_alpha);
  1875. }
  1876. EditorPropertyColor::EditorPropertyColor() {
  1877. picker = memnew(ColorPickerButton);
  1878. add_child(picker);
  1879. picker->set_flat(true);
  1880. picker->connect("color_changed", callable_mp(this, &EditorPropertyColor::_color_changed));
  1881. picker->connect("popup_closed", callable_mp(this, &EditorPropertyColor::_popup_closed));
  1882. picker->connect("picker_created", callable_mp(this, &EditorPropertyColor::_picker_created));
  1883. picker->get_popup()->connect("about_to_popup", callable_mp(this, &EditorPropertyColor::_picker_opening));
  1884. }
  1885. ////////////// NODE PATH //////////////////////
  1886. void EditorPropertyNodePath::_node_selected(const NodePath &p_path) {
  1887. NodePath path = p_path;
  1888. Node *base_node = nullptr;
  1889. if (!use_path_from_scene_root) {
  1890. base_node = Object::cast_to<Node>(get_edited_object());
  1891. if (!base_node) {
  1892. //try a base node within history
  1893. if (EditorNode::get_singleton()->get_editor_history()->get_path_size() > 0) {
  1894. Object *base = ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_history()->get_path_object(0));
  1895. if (base) {
  1896. base_node = Object::cast_to<Node>(base);
  1897. }
  1898. }
  1899. }
  1900. }
  1901. if (!base_node && get_edited_object()->has_method("get_root_path")) {
  1902. base_node = get_edited_object()->call("get_root_path");
  1903. }
  1904. if (!base_node && Object::cast_to<Reference>(get_edited_object())) {
  1905. Node *to_node = get_node(p_path);
  1906. ERR_FAIL_COND(!to_node);
  1907. path = get_tree()->get_edited_scene_root()->get_path_to(to_node);
  1908. }
  1909. if (base_node) { // for AnimationTrackKeyEdit
  1910. path = base_node->get_path().rel_path_to(p_path);
  1911. }
  1912. emit_changed(get_edited_property(), path);
  1913. update_property();
  1914. }
  1915. void EditorPropertyNodePath::_node_assign() {
  1916. if (!scene_tree) {
  1917. scene_tree = memnew(SceneTreeDialog);
  1918. scene_tree->get_scene_tree()->set_show_enabled_subscene(true);
  1919. scene_tree->get_scene_tree()->set_valid_types(valid_types);
  1920. add_child(scene_tree);
  1921. scene_tree->connect("selected", callable_mp(this, &EditorPropertyNodePath::_node_selected));
  1922. }
  1923. scene_tree->popup_scenetree_dialog();
  1924. }
  1925. void EditorPropertyNodePath::_node_clear() {
  1926. emit_changed(get_edited_property(), NodePath());
  1927. update_property();
  1928. }
  1929. void EditorPropertyNodePath::update_property() {
  1930. NodePath p = get_edited_object()->get(get_edited_property());
  1931. assign->set_tooltip(p);
  1932. if (p == NodePath()) {
  1933. assign->set_icon(Ref<Texture2D>());
  1934. assign->set_text(TTR("Assign..."));
  1935. assign->set_flat(false);
  1936. return;
  1937. }
  1938. assign->set_flat(true);
  1939. Node *base_node = nullptr;
  1940. if (base_hint != NodePath()) {
  1941. if (get_tree()->get_root()->has_node(base_hint)) {
  1942. base_node = get_tree()->get_root()->get_node(base_hint);
  1943. }
  1944. } else {
  1945. base_node = Object::cast_to<Node>(get_edited_object());
  1946. }
  1947. if (!base_node || !base_node->has_node(p)) {
  1948. assign->set_icon(Ref<Texture2D>());
  1949. assign->set_text(p);
  1950. return;
  1951. }
  1952. Node *target_node = base_node->get_node(p);
  1953. ERR_FAIL_COND(!target_node);
  1954. if (String(target_node->get_name()).find("@") != -1) {
  1955. assign->set_icon(Ref<Texture2D>());
  1956. assign->set_text(p);
  1957. return;
  1958. }
  1959. assign->set_text(target_node->get_name());
  1960. assign->set_icon(EditorNode::get_singleton()->get_object_icon(target_node, "Node"));
  1961. }
  1962. void EditorPropertyNodePath::setup(const NodePath &p_base_hint, Vector<StringName> p_valid_types, bool p_use_path_from_scene_root) {
  1963. base_hint = p_base_hint;
  1964. valid_types = p_valid_types;
  1965. use_path_from_scene_root = p_use_path_from_scene_root;
  1966. }
  1967. void EditorPropertyNodePath::_notification(int p_what) {
  1968. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1969. Ref<Texture2D> t = get_theme_icon("Clear", "EditorIcons");
  1970. clear->set_icon(t);
  1971. }
  1972. }
  1973. void EditorPropertyNodePath::_bind_methods() {
  1974. }
  1975. EditorPropertyNodePath::EditorPropertyNodePath() {
  1976. HBoxContainer *hbc = memnew(HBoxContainer);
  1977. add_child(hbc);
  1978. assign = memnew(Button);
  1979. assign->set_flat(true);
  1980. assign->set_h_size_flags(SIZE_EXPAND_FILL);
  1981. assign->set_clip_text(true);
  1982. assign->connect("pressed", callable_mp(this, &EditorPropertyNodePath::_node_assign));
  1983. hbc->add_child(assign);
  1984. clear = memnew(Button);
  1985. clear->set_flat(true);
  1986. clear->connect("pressed", callable_mp(this, &EditorPropertyNodePath::_node_clear));
  1987. hbc->add_child(clear);
  1988. use_path_from_scene_root = false;
  1989. scene_tree = nullptr; //do not allocate unnecessarily
  1990. }
  1991. ///////////////////// RID /////////////////////////
  1992. void EditorPropertyRID::update_property() {
  1993. RID rid = get_edited_object()->get(get_edited_property());
  1994. if (rid.is_valid()) {
  1995. int id = rid.get_id();
  1996. label->set_text("RID: " + itos(id));
  1997. } else {
  1998. label->set_text(TTR("Invalid RID"));
  1999. }
  2000. }
  2001. EditorPropertyRID::EditorPropertyRID() {
  2002. label = memnew(Label);
  2003. add_child(label);
  2004. }
  2005. ////////////// RESOURCE //////////////////////
  2006. void EditorPropertyResource::_file_selected(const String &p_path) {
  2007. RES res = ResourceLoader::load(p_path);
  2008. ERR_FAIL_COND_MSG(res.is_null(), "Cannot load resource from path '" + p_path + "'.");
  2009. List<PropertyInfo> prop_list;
  2010. get_edited_object()->get_property_list(&prop_list);
  2011. String property_types;
  2012. for (List<PropertyInfo>::Element *E = prop_list.front(); E; E = E->next()) {
  2013. if (E->get().name == get_edited_property() && (E->get().hint & PROPERTY_HINT_RESOURCE_TYPE)) {
  2014. property_types = E->get().hint_string;
  2015. }
  2016. }
  2017. if (!property_types.is_empty()) {
  2018. bool any_type_matches = false;
  2019. const Vector<String> split_property_types = property_types.split(",");
  2020. for (int i = 0; i < split_property_types.size(); ++i) {
  2021. if (res->is_class(split_property_types[i])) {
  2022. any_type_matches = true;
  2023. break;
  2024. }
  2025. }
  2026. if (!any_type_matches) {
  2027. EditorNode::get_singleton()->show_warning(vformat(TTR("The selected resource (%s) does not match any type expected for this property (%s)."), res->get_class(), property_types));
  2028. }
  2029. }
  2030. emit_changed(get_edited_property(), res);
  2031. update_property();
  2032. }
  2033. void EditorPropertyResource::_menu_option(int p_which) {
  2034. //scene_tree->popup_scenetree_dialog();
  2035. switch (p_which) {
  2036. case OBJ_MENU_LOAD: {
  2037. if (!file) {
  2038. file = memnew(EditorFileDialog);
  2039. file->connect("file_selected", callable_mp(this, &EditorPropertyResource::_file_selected));
  2040. add_child(file);
  2041. }
  2042. file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  2043. String type = base_type;
  2044. List<String> extensions;
  2045. for (int i = 0; i < type.get_slice_count(","); i++) {
  2046. ResourceLoader::get_recognized_extensions_for_type(type.get_slice(",", i), &extensions);
  2047. }
  2048. Set<String> valid_extensions;
  2049. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  2050. valid_extensions.insert(E->get());
  2051. }
  2052. file->clear_filters();
  2053. for (Set<String>::Element *E = valid_extensions.front(); E; E = E->next()) {
  2054. file->add_filter("*." + E->get() + " ; " + E->get().to_upper());
  2055. }
  2056. file->popup_file_dialog();
  2057. } break;
  2058. case OBJ_MENU_EDIT: {
  2059. RES res = get_edited_object()->get(get_edited_property());
  2060. if (!res.is_null()) {
  2061. emit_signal("resource_selected", get_edited_property(), res);
  2062. }
  2063. } break;
  2064. case OBJ_MENU_CLEAR: {
  2065. emit_changed(get_edited_property(), RES());
  2066. update_property();
  2067. } break;
  2068. case OBJ_MENU_MAKE_UNIQUE: {
  2069. RES res_orig = get_edited_object()->get(get_edited_property());
  2070. if (res_orig.is_null()) {
  2071. return;
  2072. }
  2073. List<PropertyInfo> property_list;
  2074. res_orig->get_property_list(&property_list);
  2075. List<Pair<String, Variant>> propvalues;
  2076. for (List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
  2077. Pair<String, Variant> p;
  2078. PropertyInfo &pi = E->get();
  2079. if (pi.usage & PROPERTY_USAGE_STORAGE) {
  2080. p.first = pi.name;
  2081. p.second = res_orig->get(pi.name);
  2082. }
  2083. propvalues.push_back(p);
  2084. }
  2085. String orig_type = res_orig->get_class();
  2086. Object *inst = ClassDB::instance(orig_type);
  2087. Ref<Resource> res = Ref<Resource>(Object::cast_to<Resource>(inst));
  2088. ERR_FAIL_COND(res.is_null());
  2089. for (List<Pair<String, Variant>>::Element *E = propvalues.front(); E; E = E->next()) {
  2090. Pair<String, Variant> &p = E->get();
  2091. res->set(p.first, p.second);
  2092. }
  2093. emit_changed(get_edited_property(), res);
  2094. update_property();
  2095. } break;
  2096. case OBJ_MENU_SAVE: {
  2097. RES res = get_edited_object()->get(get_edited_property());
  2098. if (res.is_null()) {
  2099. return;
  2100. }
  2101. EditorNode::get_singleton()->save_resource(res);
  2102. } break;
  2103. case OBJ_MENU_COPY: {
  2104. RES res = get_edited_object()->get(get_edited_property());
  2105. EditorSettings::get_singleton()->set_resource_clipboard(res);
  2106. } break;
  2107. case OBJ_MENU_PASTE: {
  2108. RES res = EditorSettings::get_singleton()->get_resource_clipboard();
  2109. emit_changed(get_edited_property(), res);
  2110. update_property();
  2111. } break;
  2112. case OBJ_MENU_NEW_SCRIPT: {
  2113. if (Object::cast_to<Node>(get_edited_object())) {
  2114. EditorNode::get_singleton()->get_scene_tree_dock()->open_script_dialog(Object::cast_to<Node>(get_edited_object()), false);
  2115. }
  2116. } break;
  2117. case OBJ_MENU_EXTEND_SCRIPT: {
  2118. if (Object::cast_to<Node>(get_edited_object())) {
  2119. EditorNode::get_singleton()->get_scene_tree_dock()->open_script_dialog(Object::cast_to<Node>(get_edited_object()), true);
  2120. }
  2121. } break;
  2122. case OBJ_MENU_SHOW_IN_FILE_SYSTEM: {
  2123. RES res = get_edited_object()->get(get_edited_property());
  2124. FileSystemDock *file_system_dock = EditorNode::get_singleton()->get_filesystem_dock();
  2125. file_system_dock->navigate_to_path(res->get_path());
  2126. // Ensure that the FileSystem dock is visible.
  2127. TabContainer *tab_container = (TabContainer *)file_system_dock->get_parent_control();
  2128. tab_container->set_current_tab(file_system_dock->get_index());
  2129. } break;
  2130. default: {
  2131. RES res = get_edited_object()->get(get_edited_property());
  2132. if (p_which >= CONVERT_BASE_ID) {
  2133. int to_type = p_which - CONVERT_BASE_ID;
  2134. Vector<Ref<EditorResourceConversionPlugin>> conversions = EditorNode::get_singleton()->find_resource_conversion_plugin(res);
  2135. ERR_FAIL_INDEX(to_type, conversions.size());
  2136. Ref<Resource> new_res = conversions[to_type]->convert(res);
  2137. emit_changed(get_edited_property(), new_res);
  2138. update_property();
  2139. break;
  2140. }
  2141. ERR_FAIL_COND(inheritors_array.is_empty());
  2142. String intype = inheritors_array[p_which - TYPE_BASE_ID];
  2143. if (intype == "ViewportTexture") {
  2144. Resource *r = Object::cast_to<Resource>(get_edited_object());
  2145. if (r && r->get_path().is_resource_file()) {
  2146. EditorNode::get_singleton()->show_warning(TTR("Can't create a ViewportTexture on resources saved as a file.\nResource needs to belong to a scene."));
  2147. return;
  2148. }
  2149. if (r && !r->is_local_to_scene()) {
  2150. 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)."));
  2151. return;
  2152. }
  2153. if (!scene_tree) {
  2154. scene_tree = memnew(SceneTreeDialog);
  2155. Vector<StringName> valid_types;
  2156. valid_types.push_back("Viewport");
  2157. scene_tree->get_scene_tree()->set_valid_types(valid_types);
  2158. scene_tree->get_scene_tree()->set_show_enabled_subscene(true);
  2159. add_child(scene_tree);
  2160. scene_tree->connect("selected", callable_mp(this, &EditorPropertyResource::_viewport_selected));
  2161. scene_tree->set_title(TTR("Pick a Viewport"));
  2162. }
  2163. scene_tree->popup_scenetree_dialog();
  2164. return;
  2165. }
  2166. Variant obj;
  2167. if (ScriptServer::is_global_class(intype)) {
  2168. obj = ClassDB::instance(ScriptServer::get_global_class_native_base(intype));
  2169. if (obj) {
  2170. Ref<Script> script = ResourceLoader::load(ScriptServer::get_global_class_path(intype));
  2171. if (script.is_valid()) {
  2172. ((Object *)obj)->set_script(script);
  2173. }
  2174. }
  2175. } else {
  2176. obj = ClassDB::instance(intype);
  2177. }
  2178. if (!obj) {
  2179. obj = EditorNode::get_editor_data().instance_custom_type(intype, "Resource");
  2180. }
  2181. Resource *resp = Object::cast_to<Resource>(obj);
  2182. ERR_BREAK(!resp);
  2183. if (get_edited_object() && base_type != String() && base_type == "Script") {
  2184. //make visual script the right type
  2185. resp->call("set_instance_base_type", get_edited_object()->get_class());
  2186. }
  2187. res = RES(resp);
  2188. emit_changed(get_edited_property(), res);
  2189. update_property();
  2190. } break;
  2191. }
  2192. }
  2193. void EditorPropertyResource::_resource_preview(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, ObjectID p_obj) {
  2194. RES p = get_edited_object()->get(get_edited_property());
  2195. if (p.is_valid() && p->get_instance_id() == p_obj) {
  2196. String type = p->get_class_name();
  2197. if (ClassDB::is_parent_class(type, "Script")) {
  2198. assign->set_text(p->get_path().get_file());
  2199. return;
  2200. }
  2201. if (p_preview.is_valid()) {
  2202. preview->set_offset(SIDE_LEFT, assign->get_icon()->get_width() + assign->get_theme_stylebox("normal")->get_default_margin(SIDE_LEFT) + get_theme_constant("hseparation", "Button"));
  2203. if (type == "GradientTexture") {
  2204. preview->set_stretch_mode(TextureRect::STRETCH_SCALE);
  2205. assign->set_custom_minimum_size(Size2(1, 1));
  2206. } else {
  2207. preview->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  2208. int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size");
  2209. thumbnail_size *= EDSCALE;
  2210. assign->set_custom_minimum_size(Size2(1, thumbnail_size));
  2211. }
  2212. preview->set_texture(p_preview);
  2213. assign->set_text("");
  2214. }
  2215. }
  2216. }
  2217. void EditorPropertyResource::_update_menu_items() {
  2218. //////////////////// UPDATE MENU //////////////////////////
  2219. RES res = get_edited_object()->get(get_edited_property());
  2220. menu->clear();
  2221. if (get_edited_property() == "script" && base_type == "Script" && Object::cast_to<Node>(get_edited_object())) {
  2222. menu->add_icon_item(get_theme_icon("ScriptCreate", "EditorIcons"), TTR("New Script"), OBJ_MENU_NEW_SCRIPT);
  2223. menu->add_icon_item(get_theme_icon("ScriptExtend", "EditorIcons"), TTR("Extend Script"), OBJ_MENU_EXTEND_SCRIPT);
  2224. menu->add_separator();
  2225. } else if (base_type != "") {
  2226. int idx = 0;
  2227. Vector<EditorData::CustomType> custom_resources;
  2228. if (EditorNode::get_editor_data().get_custom_types().has("Resource")) {
  2229. custom_resources = EditorNode::get_editor_data().get_custom_types()["Resource"];
  2230. }
  2231. for (int i = 0; i < base_type.get_slice_count(","); i++) {
  2232. String base = base_type.get_slice(",", i);
  2233. Set<String> valid_inheritors;
  2234. valid_inheritors.insert(base);
  2235. List<StringName> inheritors;
  2236. ClassDB::get_inheriters_from_class(base.strip_edges(), &inheritors);
  2237. for (int j = 0; j < custom_resources.size(); j++) {
  2238. inheritors.push_back(custom_resources[j].name);
  2239. }
  2240. List<StringName>::Element *E = inheritors.front();
  2241. while (E) {
  2242. valid_inheritors.insert(E->get());
  2243. E = E->next();
  2244. }
  2245. List<StringName> global_classes;
  2246. ScriptServer::get_global_class_list(&global_classes);
  2247. E = global_classes.front();
  2248. while (E) {
  2249. if (EditorNode::get_editor_data().script_class_is_parent(E->get(), base_type)) {
  2250. valid_inheritors.insert(E->get());
  2251. }
  2252. E = E->next();
  2253. }
  2254. for (Set<String>::Element *F = valid_inheritors.front(); F; F = F->next()) {
  2255. const String &t = F->get();
  2256. bool is_custom_resource = false;
  2257. Ref<Texture2D> icon;
  2258. if (!custom_resources.is_empty()) {
  2259. for (int j = 0; j < custom_resources.size(); j++) {
  2260. if (custom_resources[j].name == t) {
  2261. is_custom_resource = true;
  2262. if (custom_resources[j].icon.is_valid()) {
  2263. icon = custom_resources[j].icon;
  2264. }
  2265. break;
  2266. }
  2267. }
  2268. }
  2269. if (!is_custom_resource && !(ScriptServer::is_global_class(t) || ClassDB::can_instance(t))) {
  2270. continue;
  2271. }
  2272. inheritors_array.push_back(t);
  2273. if (!icon.is_valid()) {
  2274. icon = get_theme_icon(has_theme_icon(t, "EditorIcons") ? t : "Object", "EditorIcons");
  2275. }
  2276. int id = TYPE_BASE_ID + idx;
  2277. menu->add_icon_item(icon, vformat(TTR("New %s"), t), id);
  2278. idx++;
  2279. }
  2280. }
  2281. if (menu->get_item_count()) {
  2282. menu->add_separator();
  2283. }
  2284. }
  2285. menu->add_icon_item(get_theme_icon("Load", "EditorIcons"), TTR("Load"), OBJ_MENU_LOAD);
  2286. if (!res.is_null()) {
  2287. menu->add_icon_item(get_theme_icon("Edit", "EditorIcons"), TTR("Edit"), OBJ_MENU_EDIT);
  2288. menu->add_icon_item(get_theme_icon("Clear", "EditorIcons"), TTR("Clear"), OBJ_MENU_CLEAR);
  2289. menu->add_icon_item(get_theme_icon("Duplicate", "EditorIcons"), TTR("Make Unique"), OBJ_MENU_MAKE_UNIQUE);
  2290. menu->add_icon_item(get_theme_icon("Save", "EditorIcons"), TTR("Save"), OBJ_MENU_SAVE);
  2291. RES r = res;
  2292. if (r.is_valid() && r->get_path().is_resource_file()) {
  2293. menu->add_separator();
  2294. menu->add_item(TTR("Show in FileSystem"), OBJ_MENU_SHOW_IN_FILE_SYSTEM);
  2295. }
  2296. }
  2297. RES cb = EditorSettings::get_singleton()->get_resource_clipboard();
  2298. bool paste_valid = false;
  2299. if (cb.is_valid()) {
  2300. if (base_type == "") {
  2301. paste_valid = true;
  2302. } else {
  2303. for (int i = 0; i < base_type.get_slice_count(","); i++) {
  2304. if (ClassDB::is_parent_class(cb->get_class(), base_type.get_slice(",", i))) {
  2305. paste_valid = true;
  2306. break;
  2307. }
  2308. }
  2309. }
  2310. }
  2311. if (!res.is_null() || paste_valid) {
  2312. menu->add_separator();
  2313. if (!res.is_null()) {
  2314. menu->add_item(TTR("Copy"), OBJ_MENU_COPY);
  2315. }
  2316. if (paste_valid) {
  2317. menu->add_item(TTR("Paste"), OBJ_MENU_PASTE);
  2318. }
  2319. }
  2320. if (!res.is_null()) {
  2321. Vector<Ref<EditorResourceConversionPlugin>> conversions = EditorNode::get_singleton()->find_resource_conversion_plugin(res);
  2322. if (conversions.size()) {
  2323. menu->add_separator();
  2324. }
  2325. for (int i = 0; i < conversions.size(); i++) {
  2326. String what = conversions[i]->converts_to();
  2327. Ref<Texture2D> icon;
  2328. if (has_theme_icon(what, "EditorIcons")) {
  2329. icon = get_theme_icon(what, "EditorIcons");
  2330. } else {
  2331. icon = get_theme_icon(what, "Resource");
  2332. }
  2333. menu->add_icon_item(icon, vformat(TTR("Convert To %s"), what), CONVERT_BASE_ID + i);
  2334. }
  2335. }
  2336. }
  2337. void EditorPropertyResource::_update_menu() {
  2338. _update_menu_items();
  2339. Rect2 gt = edit->get_screen_rect();
  2340. menu->set_as_minsize();
  2341. int ms = menu->get_contents_minimum_size().width;
  2342. Vector2 popup_pos = gt.position + gt.size - Vector2(ms, 0);
  2343. menu->set_position(popup_pos);
  2344. menu->popup();
  2345. }
  2346. void EditorPropertyResource::_sub_inspector_property_keyed(const String &p_property, const Variant &p_value, bool) {
  2347. emit_signal("property_keyed_with_value", String(get_edited_property()) + ":" + p_property, p_value, false);
  2348. }
  2349. void EditorPropertyResource::_sub_inspector_resource_selected(const RES &p_resource, const String &p_property) {
  2350. emit_signal("resource_selected", String(get_edited_property()) + ":" + p_property, p_resource);
  2351. }
  2352. void EditorPropertyResource::_sub_inspector_object_id_selected(int p_id) {
  2353. emit_signal("object_id_selected", get_edited_property(), p_id);
  2354. }
  2355. void EditorPropertyResource::_button_input(const Ref<InputEvent> &p_event) {
  2356. Ref<InputEventMouseButton> mb = p_event;
  2357. if (mb.is_valid()) {
  2358. if (mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  2359. _update_menu_items();
  2360. Vector2 pos = get_screen_position() + mb->get_position();
  2361. //pos = assign->get_global_transform().xform(pos);
  2362. menu->set_as_minsize();
  2363. menu->set_position(pos);
  2364. menu->popup();
  2365. }
  2366. }
  2367. }
  2368. void EditorPropertyResource::_open_editor_pressed() {
  2369. RES res = get_edited_object()->get(get_edited_property());
  2370. if (res.is_valid()) {
  2371. EditorNode::get_singleton()->call_deferred("edit_item_resource", res); //may clear the editor so do it deferred
  2372. }
  2373. }
  2374. void EditorPropertyResource::_fold_other_editors(Object *p_self) {
  2375. if (this == p_self) {
  2376. return;
  2377. }
  2378. RES res = get_edited_object()->get(get_edited_property());
  2379. if (!res.is_valid()) {
  2380. return;
  2381. }
  2382. bool use_editor = false;
  2383. for (int i = 0; i < EditorNode::get_editor_data().get_editor_plugin_count(); i++) {
  2384. EditorPlugin *ep = EditorNode::get_editor_data().get_editor_plugin(i);
  2385. if (ep->handles(res.ptr())) {
  2386. use_editor = true;
  2387. }
  2388. }
  2389. if (!use_editor) {
  2390. return;
  2391. }
  2392. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  2393. opened_editor = false;
  2394. if (unfolded) {
  2395. //refold
  2396. assign->set_pressed(false);
  2397. get_edited_object()->editor_set_section_unfold(get_edited_property(), false);
  2398. update_property();
  2399. }
  2400. }
  2401. void EditorPropertyResource::_update_property_bg() {
  2402. if (!is_inside_tree()) {
  2403. return;
  2404. }
  2405. updating_theme = true;
  2406. if (sub_inspector != nullptr) {
  2407. int count_subinspectors = 0;
  2408. Node *n = get_parent();
  2409. while (n) {
  2410. EditorInspector *ei = Object::cast_to<EditorInspector>(n);
  2411. if (ei && ei->is_sub_inspector()) {
  2412. count_subinspectors++;
  2413. }
  2414. n = n->get_parent();
  2415. }
  2416. count_subinspectors = MIN(15, count_subinspectors);
  2417. add_theme_color_override("property_color", get_theme_color("sub_inspector_property_color", "Editor"));
  2418. add_theme_style_override("bg_selected", get_theme_stylebox("sub_inspector_property_bg_selected" + itos(count_subinspectors), "Editor"));
  2419. add_theme_style_override("bg", get_theme_stylebox("sub_inspector_property_bg" + itos(count_subinspectors), "Editor"));
  2420. add_theme_constant_override("font_offset", get_theme_constant("sub_inspector_font_offset", "Editor"));
  2421. add_theme_constant_override("vseparation", 0);
  2422. } else {
  2423. add_theme_color_override("property_color", get_theme_color("property_color", "EditorProperty"));
  2424. add_theme_style_override("bg_selected", get_theme_stylebox("bg_selected", "EditorProperty"));
  2425. add_theme_style_override("bg", get_theme_stylebox("bg", "EditorProperty"));
  2426. add_theme_constant_override("vseparation", get_theme_constant("vseparation", "EditorProperty"));
  2427. add_theme_constant_override("font_offset", get_theme_constant("font_offset", "EditorProperty"));
  2428. }
  2429. updating_theme = false;
  2430. update();
  2431. }
  2432. void EditorPropertyResource::update_property() {
  2433. RES res = get_edited_object()->get(get_edited_property());
  2434. if (use_sub_inspector) {
  2435. if (res.is_valid() != assign->is_toggle_mode()) {
  2436. assign->set_toggle_mode(res.is_valid());
  2437. }
  2438. if (res.is_valid() && get_edited_object()->editor_is_section_unfolded(get_edited_property())) {
  2439. if (!sub_inspector) {
  2440. sub_inspector = memnew(EditorInspector);
  2441. sub_inspector->set_enable_v_scroll(false);
  2442. sub_inspector->set_use_doc_hints(true);
  2443. sub_inspector->set_sub_inspector(true);
  2444. sub_inspector->set_enable_capitalize_paths(true);
  2445. sub_inspector->connect("property_keyed", callable_mp(this, &EditorPropertyResource::_sub_inspector_property_keyed));
  2446. sub_inspector->connect("resource_selected", callable_mp(this, &EditorPropertyResource::_sub_inspector_resource_selected));
  2447. sub_inspector->connect("object_id_selected", callable_mp(this, &EditorPropertyResource::_sub_inspector_object_id_selected));
  2448. sub_inspector->set_keying(is_keying());
  2449. sub_inspector->set_read_only(is_read_only());
  2450. sub_inspector->set_use_folding(is_using_folding());
  2451. sub_inspector->set_undo_redo(EditorNode::get_undo_redo());
  2452. sub_inspector_vbox = memnew(VBoxContainer);
  2453. add_child(sub_inspector_vbox);
  2454. set_bottom_editor(sub_inspector_vbox);
  2455. sub_inspector_vbox->add_child(sub_inspector);
  2456. assign->set_pressed(true);
  2457. bool use_editor = false;
  2458. for (int i = 0; i < EditorNode::get_editor_data().get_editor_plugin_count(); i++) {
  2459. EditorPlugin *ep = EditorNode::get_editor_data().get_editor_plugin(i);
  2460. if (ep->handles(res.ptr())) {
  2461. use_editor = true;
  2462. }
  2463. }
  2464. if (use_editor) {
  2465. //open editor directly and hide other open of these
  2466. _open_editor_pressed();
  2467. if (is_inside_tree()) {
  2468. get_tree()->call_deferred("call_group", "_editor_resource_properties", "_fold_other_editors", this);
  2469. }
  2470. opened_editor = true;
  2471. }
  2472. _update_property_bg();
  2473. }
  2474. if (res.ptr() != sub_inspector->get_edited_object()) {
  2475. sub_inspector->edit(res.ptr());
  2476. }
  2477. } else {
  2478. if (sub_inspector) {
  2479. set_bottom_editor(nullptr);
  2480. memdelete(sub_inspector_vbox);
  2481. sub_inspector = nullptr;
  2482. sub_inspector_vbox = nullptr;
  2483. if (opened_editor) {
  2484. EditorNode::get_singleton()->hide_top_editors();
  2485. opened_editor = false;
  2486. }
  2487. _update_property_bg();
  2488. }
  2489. }
  2490. }
  2491. preview->set_texture(Ref<Texture2D>());
  2492. if (res == RES()) {
  2493. assign->set_icon(Ref<Texture2D>());
  2494. assign->set_text(TTR("[empty]"));
  2495. } else {
  2496. assign->set_icon(EditorNode::get_singleton()->get_object_icon(res.operator->(), "Object"));
  2497. if (res->get_name() != String()) {
  2498. assign->set_text(res->get_name());
  2499. } else if (res->get_path().is_resource_file()) {
  2500. assign->set_text(res->get_path().get_file());
  2501. assign->set_tooltip(res->get_path());
  2502. } else {
  2503. assign->set_text(res->get_class());
  2504. }
  2505. if (res->get_path().is_resource_file()) {
  2506. assign->set_tooltip(res->get_path());
  2507. }
  2508. //preview will override the above, so called at the end
  2509. EditorResourcePreview::get_singleton()->queue_edited_resource_preview(res, this, "_resource_preview", res->get_instance_id());
  2510. }
  2511. }
  2512. void EditorPropertyResource::_resource_selected() {
  2513. RES res = get_edited_object()->get(get_edited_property());
  2514. if (res.is_null()) {
  2515. edit->set_pressed(true);
  2516. _update_menu();
  2517. return;
  2518. }
  2519. if (use_sub_inspector) {
  2520. bool unfold = !get_edited_object()->editor_is_section_unfolded(get_edited_property());
  2521. get_edited_object()->editor_set_section_unfold(get_edited_property(), unfold);
  2522. update_property();
  2523. } else {
  2524. emit_signal("resource_selected", get_edited_property(), res);
  2525. }
  2526. }
  2527. void EditorPropertyResource::setup(const String &p_base_type) {
  2528. base_type = p_base_type;
  2529. }
  2530. void EditorPropertyResource::_notification(int p_what) {
  2531. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  2532. if (updating_theme) {
  2533. return;
  2534. }
  2535. Ref<Texture2D> t = get_theme_icon("select_arrow", "Tree");
  2536. edit->set_icon(t);
  2537. _update_property_bg();
  2538. }
  2539. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  2540. if (_is_drop_valid(get_viewport()->gui_get_drag_data())) {
  2541. dropping = true;
  2542. assign->update();
  2543. }
  2544. }
  2545. if (p_what == NOTIFICATION_DRAG_END) {
  2546. if (dropping) {
  2547. dropping = false;
  2548. assign->update();
  2549. }
  2550. }
  2551. }
  2552. void EditorPropertyResource::_viewport_selected(const NodePath &p_path) {
  2553. Node *to_node = get_node(p_path);
  2554. if (!Object::cast_to<Viewport>(to_node)) {
  2555. EditorNode::get_singleton()->show_warning(TTR("Selected node is not a Viewport!"));
  2556. return;
  2557. }
  2558. Ref<ViewportTexture> vt;
  2559. vt.instance();
  2560. vt->set_viewport_path_in_scene(get_tree()->get_edited_scene_root()->get_path_to(to_node));
  2561. vt->setup_local_to_scene();
  2562. emit_changed(get_edited_property(), vt);
  2563. update_property();
  2564. }
  2565. void EditorPropertyResource::collapse_all_folding() {
  2566. if (sub_inspector) {
  2567. sub_inspector->collapse_all_folding();
  2568. }
  2569. }
  2570. void EditorPropertyResource::expand_all_folding() {
  2571. if (sub_inspector) {
  2572. sub_inspector->expand_all_folding();
  2573. }
  2574. }
  2575. void EditorPropertyResource::_button_draw() {
  2576. if (dropping) {
  2577. Color color = get_theme_color("accent_color", "Editor");
  2578. assign->draw_rect(Rect2(Point2(), assign->get_size()), color, false);
  2579. }
  2580. }
  2581. Variant EditorPropertyResource::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  2582. RES res = get_edited_object()->get(get_edited_property());
  2583. if (res.is_valid()) {
  2584. return EditorNode::get_singleton()->drag_resource(res, p_from);
  2585. }
  2586. return Variant();
  2587. }
  2588. bool EditorPropertyResource::_is_drop_valid(const Dictionary &p_drag_data) const {
  2589. Vector<String> allowed_types = base_type.split(",");
  2590. int size = allowed_types.size();
  2591. for (int i = 0; i < size; i++) {
  2592. String at = allowed_types[i].strip_edges();
  2593. if (at == "StandardMaterial3D") {
  2594. allowed_types.append("Texture2D");
  2595. } else if (at == "ShaderMaterial") {
  2596. allowed_types.append("Shader");
  2597. } else if (at == "Font") {
  2598. allowed_types.append("FontData");
  2599. }
  2600. }
  2601. Dictionary drag_data = p_drag_data;
  2602. Ref<Resource> res;
  2603. if (drag_data.has("type") && String(drag_data["type"]) == "script_list_element") {
  2604. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(drag_data["script_list_element"]);
  2605. res = se->get_edited_resource();
  2606. } else if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  2607. res = drag_data["resource"];
  2608. }
  2609. if (res.is_valid()) {
  2610. for (int i = 0; i < allowed_types.size(); i++) {
  2611. String at = allowed_types[i].strip_edges();
  2612. if (ClassDB::is_parent_class(res->get_class(), at)) {
  2613. return true;
  2614. }
  2615. }
  2616. }
  2617. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  2618. Vector<String> files = drag_data["files"];
  2619. if (files.size() == 1) {
  2620. String file = files[0];
  2621. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  2622. if (ftype != "") {
  2623. for (int i = 0; i < allowed_types.size(); i++) {
  2624. String at = allowed_types[i].strip_edges();
  2625. if (ClassDB::is_parent_class(ftype, at)) {
  2626. return true;
  2627. }
  2628. }
  2629. }
  2630. }
  2631. }
  2632. return false;
  2633. }
  2634. bool EditorPropertyResource::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  2635. return _is_drop_valid(p_data);
  2636. }
  2637. void EditorPropertyResource::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  2638. ERR_FAIL_COND(!_is_drop_valid(p_data));
  2639. Dictionary drag_data = p_data;
  2640. Ref<Resource> res;
  2641. if (drag_data.has("type") && String(drag_data["type"]) == "script_list_element") {
  2642. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(drag_data["script_list_element"]);
  2643. res = se->get_edited_resource();
  2644. } else if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  2645. res = drag_data["resource"];
  2646. }
  2647. if (!res.is_valid() && drag_data.has("type") && String(drag_data["type"]) == "files") {
  2648. Vector<String> files = drag_data["files"];
  2649. if (files.size() == 1) {
  2650. String file = files[0];
  2651. res = ResourceLoader::load(file);
  2652. }
  2653. }
  2654. if (res.is_valid()) {
  2655. bool need_convert = true;
  2656. Vector<String> allowed_types = base_type.split(",");
  2657. for (int i = 0; i < allowed_types.size(); i++) {
  2658. String at = allowed_types[i].strip_edges();
  2659. if (ClassDB::is_parent_class(res->get_class(), at)) {
  2660. need_convert = false;
  2661. break;
  2662. }
  2663. }
  2664. if (need_convert) {
  2665. for (int i = 0; i < allowed_types.size(); i++) {
  2666. String at = allowed_types[i].strip_edges();
  2667. if (at == "StandardMaterial3D" && ClassDB::is_parent_class(res->get_class(), "Texture2D")) {
  2668. Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
  2669. mat->set_texture(StandardMaterial3D::TextureParam::TEXTURE_ALBEDO, res);
  2670. res = mat;
  2671. break;
  2672. }
  2673. if (at == "ShaderMaterial" && ClassDB::is_parent_class(res->get_class(), "Shader")) {
  2674. Ref<ShaderMaterial> mat = memnew(ShaderMaterial);
  2675. mat->set_shader(res);
  2676. res = mat;
  2677. break;
  2678. }
  2679. if (at == "Font" && ClassDB::is_parent_class(res->get_class(), "FontData")) {
  2680. Ref<Font> font = memnew(Font);
  2681. font->add_data(res);
  2682. res = font;
  2683. break;
  2684. }
  2685. }
  2686. }
  2687. emit_changed(get_edited_property(), res);
  2688. update_property();
  2689. return;
  2690. }
  2691. }
  2692. void EditorPropertyResource::set_use_sub_inspector(bool p_enable) {
  2693. use_sub_inspector = p_enable;
  2694. }
  2695. void EditorPropertyResource::_bind_methods() {
  2696. ClassDB::bind_method(D_METHOD("_resource_preview"), &EditorPropertyResource::_resource_preview);
  2697. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &EditorPropertyResource::get_drag_data_fw);
  2698. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &EditorPropertyResource::can_drop_data_fw);
  2699. ClassDB::bind_method(D_METHOD("drop_data_fw"), &EditorPropertyResource::drop_data_fw);
  2700. ClassDB::bind_method(D_METHOD("_open_editor_pressed"), &EditorPropertyResource::_open_editor_pressed);
  2701. ClassDB::bind_method(D_METHOD("_fold_other_editors"), &EditorPropertyResource::_fold_other_editors);
  2702. }
  2703. EditorPropertyResource::EditorPropertyResource() {
  2704. opened_editor = false;
  2705. sub_inspector = nullptr;
  2706. sub_inspector_vbox = nullptr;
  2707. use_sub_inspector = bool(EDITOR_GET("interface/inspector/open_resources_in_current_inspector"));
  2708. HBoxContainer *hbc = memnew(HBoxContainer);
  2709. add_child(hbc);
  2710. assign = memnew(Button);
  2711. assign->set_flat(true);
  2712. assign->set_h_size_flags(SIZE_EXPAND_FILL);
  2713. assign->set_clip_text(true);
  2714. assign->connect("pressed", callable_mp(this, &EditorPropertyResource::_resource_selected));
  2715. assign->set_drag_forwarding(this);
  2716. assign->connect("draw", callable_mp(this, &EditorPropertyResource::_button_draw));
  2717. hbc->add_child(assign);
  2718. add_focusable(assign);
  2719. preview = memnew(TextureRect);
  2720. preview->set_expand(true);
  2721. preview->set_anchors_and_offsets_preset(PRESET_WIDE);
  2722. preview->set_offset(SIDE_TOP, 1);
  2723. preview->set_offset(SIDE_BOTTOM, -1);
  2724. preview->set_offset(SIDE_RIGHT, -1);
  2725. assign->add_child(preview);
  2726. assign->connect("gui_input", callable_mp(this, &EditorPropertyResource::_button_input));
  2727. menu = memnew(PopupMenu);
  2728. add_child(menu);
  2729. edit = memnew(Button);
  2730. edit->set_flat(true);
  2731. edit->set_toggle_mode(true);
  2732. menu->connect("id_pressed", callable_mp(this, &EditorPropertyResource::_menu_option));
  2733. menu->connect("popup_hide", callable_mp((BaseButton *)edit, &BaseButton::set_pressed), varray(false));
  2734. edit->connect("pressed", callable_mp(this, &EditorPropertyResource::_update_menu));
  2735. hbc->add_child(edit);
  2736. edit->connect("gui_input", callable_mp(this, &EditorPropertyResource::_button_input));
  2737. add_focusable(edit);
  2738. file = nullptr;
  2739. scene_tree = nullptr;
  2740. dropping = false;
  2741. add_to_group("_editor_resource_properties");
  2742. }
  2743. ////////////// DEFAULT PLUGIN //////////////////////
  2744. bool EditorInspectorDefaultPlugin::can_handle(Object *p_object) {
  2745. return true; //can handle everything
  2746. }
  2747. void EditorInspectorDefaultPlugin::parse_begin(Object *p_object) {
  2748. //do none
  2749. }
  2750. 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, bool p_wide) {
  2751. float default_float_step = EDITOR_GET("interface/inspector/default_float_step");
  2752. switch (p_type) {
  2753. // atomic types
  2754. case Variant::NIL: {
  2755. EditorPropertyNil *editor = memnew(EditorPropertyNil);
  2756. add_property_editor(p_path, editor);
  2757. } break;
  2758. case Variant::BOOL: {
  2759. EditorPropertyCheck *editor = memnew(EditorPropertyCheck);
  2760. add_property_editor(p_path, editor);
  2761. } break;
  2762. case Variant::INT: {
  2763. if (p_hint == PROPERTY_HINT_ENUM) {
  2764. EditorPropertyEnum *editor = memnew(EditorPropertyEnum);
  2765. Vector<String> options = p_hint_text.split(",");
  2766. editor->setup(options);
  2767. add_property_editor(p_path, editor);
  2768. } else if (p_hint == PROPERTY_HINT_FLAGS) {
  2769. EditorPropertyFlags *editor = memnew(EditorPropertyFlags);
  2770. Vector<String> options = p_hint_text.split(",");
  2771. editor->setup(options);
  2772. add_property_editor(p_path, editor);
  2773. } 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) {
  2774. EditorPropertyLayers::LayerType lt = EditorPropertyLayers::LAYER_RENDER_2D;
  2775. switch (p_hint) {
  2776. case PROPERTY_HINT_LAYERS_2D_RENDER:
  2777. lt = EditorPropertyLayers::LAYER_RENDER_2D;
  2778. break;
  2779. case PROPERTY_HINT_LAYERS_2D_PHYSICS:
  2780. lt = EditorPropertyLayers::LAYER_PHYSICS_2D;
  2781. break;
  2782. case PROPERTY_HINT_LAYERS_3D_RENDER:
  2783. lt = EditorPropertyLayers::LAYER_RENDER_3D;
  2784. break;
  2785. case PROPERTY_HINT_LAYERS_3D_PHYSICS:
  2786. lt = EditorPropertyLayers::LAYER_PHYSICS_3D;
  2787. break;
  2788. default: {
  2789. } //compiler could be smarter here and realize this can't happen
  2790. }
  2791. EditorPropertyLayers *editor = memnew(EditorPropertyLayers);
  2792. editor->setup(lt);
  2793. add_property_editor(p_path, editor);
  2794. } else if (p_hint == PROPERTY_HINT_OBJECT_ID) {
  2795. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  2796. editor->setup(p_hint_text);
  2797. add_property_editor(p_path, editor);
  2798. } else {
  2799. EditorPropertyInteger *editor = memnew(EditorPropertyInteger);
  2800. int min = 0, max = 65535, step = 1;
  2801. bool greater = true, lesser = true;
  2802. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2803. greater = false; //if using ranged, assume false by default
  2804. lesser = false;
  2805. min = p_hint_text.get_slice(",", 0).to_int();
  2806. max = p_hint_text.get_slice(",", 1).to_int();
  2807. if (p_hint_text.get_slice_count(",") >= 3) {
  2808. step = p_hint_text.get_slice(",", 2).to_int();
  2809. }
  2810. for (int i = 2; i < p_hint_text.get_slice_count(","); i++) {
  2811. String slice = p_hint_text.get_slice(",", i).strip_edges();
  2812. if (slice == "or_greater") {
  2813. greater = true;
  2814. }
  2815. if (slice == "or_lesser") {
  2816. lesser = true;
  2817. }
  2818. }
  2819. }
  2820. editor->setup(min, max, step, greater, lesser);
  2821. add_property_editor(p_path, editor);
  2822. }
  2823. } break;
  2824. case Variant::FLOAT: {
  2825. if (p_hint == PROPERTY_HINT_EXP_EASING) {
  2826. EditorPropertyEasing *editor = memnew(EditorPropertyEasing);
  2827. bool full = true;
  2828. bool flip = false;
  2829. Vector<String> hints = p_hint_text.split(",");
  2830. for (int i = 0; i < hints.size(); i++) {
  2831. String h = hints[i].strip_edges();
  2832. if (h == "attenuation") {
  2833. flip = true;
  2834. }
  2835. if (h == "inout") {
  2836. full = true;
  2837. }
  2838. }
  2839. editor->setup(full, flip);
  2840. add_property_editor(p_path, editor);
  2841. } else {
  2842. EditorPropertyFloat *editor = memnew(EditorPropertyFloat);
  2843. double min = -65535, max = 65535, step = default_float_step;
  2844. bool hide_slider = true;
  2845. bool exp_range = false;
  2846. bool greater = true, lesser = true;
  2847. if ((p_hint == PROPERTY_HINT_RANGE || p_hint == PROPERTY_HINT_EXP_RANGE) && p_hint_text.get_slice_count(",") >= 2) {
  2848. greater = false; //if using ranged, assume false by default
  2849. lesser = false;
  2850. min = p_hint_text.get_slice(",", 0).to_float();
  2851. max = p_hint_text.get_slice(",", 1).to_float();
  2852. if (p_hint_text.get_slice_count(",") >= 3) {
  2853. step = p_hint_text.get_slice(",", 2).to_float();
  2854. }
  2855. hide_slider = false;
  2856. exp_range = p_hint == PROPERTY_HINT_EXP_RANGE;
  2857. for (int i = 2; i < p_hint_text.get_slice_count(","); i++) {
  2858. String slice = p_hint_text.get_slice(",", i).strip_edges();
  2859. if (slice == "or_greater") {
  2860. greater = true;
  2861. }
  2862. if (slice == "or_lesser") {
  2863. lesser = true;
  2864. }
  2865. }
  2866. }
  2867. editor->setup(min, max, step, hide_slider, exp_range, greater, lesser);
  2868. add_property_editor(p_path, editor);
  2869. }
  2870. } break;
  2871. case Variant::STRING: {
  2872. if (p_hint == PROPERTY_HINT_ENUM) {
  2873. EditorPropertyTextEnum *editor = memnew(EditorPropertyTextEnum);
  2874. Vector<String> options = p_hint_text.split(",");
  2875. editor->setup(options);
  2876. add_property_editor(p_path, editor);
  2877. } else if (p_hint == PROPERTY_HINT_MULTILINE_TEXT) {
  2878. EditorPropertyMultilineText *editor = memnew(EditorPropertyMultilineText);
  2879. add_property_editor(p_path, editor);
  2880. } else if (p_hint == PROPERTY_HINT_TYPE_STRING) {
  2881. EditorPropertyClassName *editor = memnew(EditorPropertyClassName);
  2882. editor->setup("Object", p_hint_text);
  2883. add_property_editor(p_path, editor);
  2884. } 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) {
  2885. Vector<String> extensions = p_hint_text.split(",");
  2886. bool global = p_hint == PROPERTY_HINT_GLOBAL_DIR || p_hint == PROPERTY_HINT_GLOBAL_FILE;
  2887. bool folder = p_hint == PROPERTY_HINT_DIR || p_hint == PROPERTY_HINT_GLOBAL_DIR;
  2888. bool save = p_hint == PROPERTY_HINT_SAVE_FILE;
  2889. EditorPropertyPath *editor = memnew(EditorPropertyPath);
  2890. editor->setup(extensions, folder, global);
  2891. if (save) {
  2892. editor->set_save_mode();
  2893. }
  2894. add_property_editor(p_path, editor);
  2895. } else if (p_hint == PROPERTY_HINT_METHOD_OF_VARIANT_TYPE ||
  2896. p_hint == PROPERTY_HINT_METHOD_OF_BASE_TYPE ||
  2897. p_hint == PROPERTY_HINT_METHOD_OF_INSTANCE ||
  2898. p_hint == PROPERTY_HINT_METHOD_OF_SCRIPT ||
  2899. p_hint == PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE ||
  2900. p_hint == PROPERTY_HINT_PROPERTY_OF_BASE_TYPE ||
  2901. p_hint == PROPERTY_HINT_PROPERTY_OF_INSTANCE ||
  2902. p_hint == PROPERTY_HINT_PROPERTY_OF_SCRIPT) {
  2903. EditorPropertyMember *editor = memnew(EditorPropertyMember);
  2904. EditorPropertyMember::Type type = EditorPropertyMember::MEMBER_METHOD_OF_BASE_TYPE;
  2905. switch (p_hint) {
  2906. case PROPERTY_HINT_METHOD_OF_BASE_TYPE:
  2907. type = EditorPropertyMember::MEMBER_METHOD_OF_BASE_TYPE;
  2908. break;
  2909. case PROPERTY_HINT_METHOD_OF_INSTANCE:
  2910. type = EditorPropertyMember::MEMBER_METHOD_OF_INSTANCE;
  2911. break;
  2912. case PROPERTY_HINT_METHOD_OF_SCRIPT:
  2913. type = EditorPropertyMember::MEMBER_METHOD_OF_SCRIPT;
  2914. break;
  2915. case PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE:
  2916. type = EditorPropertyMember::MEMBER_PROPERTY_OF_VARIANT_TYPE;
  2917. break;
  2918. case PROPERTY_HINT_PROPERTY_OF_BASE_TYPE:
  2919. type = EditorPropertyMember::MEMBER_PROPERTY_OF_BASE_TYPE;
  2920. break;
  2921. case PROPERTY_HINT_PROPERTY_OF_INSTANCE:
  2922. type = EditorPropertyMember::MEMBER_PROPERTY_OF_INSTANCE;
  2923. break;
  2924. case PROPERTY_HINT_PROPERTY_OF_SCRIPT:
  2925. type = EditorPropertyMember::MEMBER_PROPERTY_OF_SCRIPT;
  2926. break;
  2927. default: {
  2928. }
  2929. }
  2930. editor->setup(type, p_hint_text);
  2931. add_property_editor(p_path, editor);
  2932. } else {
  2933. EditorPropertyText *editor = memnew(EditorPropertyText);
  2934. if (p_hint == PROPERTY_HINT_PLACEHOLDER_TEXT) {
  2935. editor->set_placeholder(p_hint_text);
  2936. }
  2937. add_property_editor(p_path, editor);
  2938. }
  2939. } break;
  2940. // math types
  2941. case Variant::VECTOR2: {
  2942. EditorPropertyVector2 *editor = memnew(EditorPropertyVector2(p_wide));
  2943. double min = -65535, max = 65535, step = default_float_step;
  2944. bool hide_slider = true;
  2945. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2946. min = p_hint_text.get_slice(",", 0).to_float();
  2947. max = p_hint_text.get_slice(",", 1).to_float();
  2948. if (p_hint_text.get_slice_count(",") >= 3) {
  2949. step = p_hint_text.get_slice(",", 2).to_float();
  2950. }
  2951. hide_slider = false;
  2952. }
  2953. editor->setup(min, max, step, hide_slider);
  2954. add_property_editor(p_path, editor);
  2955. } break;
  2956. case Variant::VECTOR2I: {
  2957. EditorPropertyVector2i *editor = memnew(EditorPropertyVector2i(p_wide));
  2958. int min = -65535, max = 65535;
  2959. bool hide_slider = true;
  2960. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2961. min = p_hint_text.get_slice(",", 0).to_float();
  2962. max = p_hint_text.get_slice(",", 1).to_float();
  2963. hide_slider = false;
  2964. }
  2965. editor->setup(min, max, hide_slider);
  2966. add_property_editor(p_path, editor);
  2967. } break;
  2968. case Variant::RECT2: {
  2969. EditorPropertyRect2 *editor = memnew(EditorPropertyRect2(p_wide));
  2970. double min = -65535, max = 65535, step = default_float_step;
  2971. bool hide_slider = true;
  2972. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2973. min = p_hint_text.get_slice(",", 0).to_float();
  2974. max = p_hint_text.get_slice(",", 1).to_float();
  2975. if (p_hint_text.get_slice_count(",") >= 3) {
  2976. step = p_hint_text.get_slice(",", 2).to_float();
  2977. }
  2978. hide_slider = false;
  2979. }
  2980. editor->setup(min, max, step, hide_slider);
  2981. add_property_editor(p_path, editor);
  2982. } break;
  2983. case Variant::RECT2I: {
  2984. EditorPropertyRect2i *editor = memnew(EditorPropertyRect2i(p_wide));
  2985. int min = -65535, max = 65535;
  2986. bool hide_slider = true;
  2987. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2988. min = p_hint_text.get_slice(",", 0).to_float();
  2989. max = p_hint_text.get_slice(",", 1).to_float();
  2990. hide_slider = false;
  2991. }
  2992. editor->setup(min, max, hide_slider);
  2993. add_property_editor(p_path, editor);
  2994. } break;
  2995. case Variant::VECTOR3: {
  2996. EditorPropertyVector3 *editor = memnew(EditorPropertyVector3(p_wide));
  2997. double min = -65535, max = 65535, step = default_float_step;
  2998. bool hide_slider = true;
  2999. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  3000. min = p_hint_text.get_slice(",", 0).to_float();
  3001. max = p_hint_text.get_slice(",", 1).to_float();
  3002. if (p_hint_text.get_slice_count(",") >= 3) {
  3003. step = p_hint_text.get_slice(",", 2).to_float();
  3004. }
  3005. hide_slider = false;
  3006. }
  3007. editor->setup(min, max, step, hide_slider);
  3008. add_property_editor(p_path, editor);
  3009. } break;
  3010. case Variant::VECTOR3I: {
  3011. EditorPropertyVector3i *editor = memnew(EditorPropertyVector3i(p_wide));
  3012. int min = -65535, max = 65535;
  3013. bool hide_slider = true;
  3014. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  3015. min = p_hint_text.get_slice(",", 0).to_float();
  3016. max = p_hint_text.get_slice(",", 1).to_float();
  3017. hide_slider = false;
  3018. }
  3019. editor->setup(min, max, hide_slider);
  3020. add_property_editor(p_path, editor);
  3021. } break;
  3022. case Variant::TRANSFORM2D: {
  3023. EditorPropertyTransform2D *editor = memnew(EditorPropertyTransform2D);
  3024. double min = -65535, max = 65535, step = default_float_step;
  3025. bool hide_slider = true;
  3026. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  3027. min = p_hint_text.get_slice(",", 0).to_float();
  3028. max = p_hint_text.get_slice(",", 1).to_float();
  3029. if (p_hint_text.get_slice_count(",") >= 3) {
  3030. step = p_hint_text.get_slice(",", 2).to_float();
  3031. }
  3032. hide_slider = false;
  3033. }
  3034. editor->setup(min, max, step, hide_slider);
  3035. add_property_editor(p_path, editor);
  3036. } break;
  3037. case Variant::PLANE: {
  3038. EditorPropertyPlane *editor = memnew(EditorPropertyPlane(p_wide));
  3039. double min = -65535, max = 65535, step = default_float_step;
  3040. bool hide_slider = true;
  3041. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  3042. min = p_hint_text.get_slice(",", 0).to_float();
  3043. max = p_hint_text.get_slice(",", 1).to_float();
  3044. if (p_hint_text.get_slice_count(",") >= 3) {
  3045. step = p_hint_text.get_slice(",", 2).to_float();
  3046. }
  3047. hide_slider = false;
  3048. }
  3049. editor->setup(min, max, step, hide_slider);
  3050. add_property_editor(p_path, editor);
  3051. } break;
  3052. case Variant::QUAT: {
  3053. EditorPropertyQuat *editor = memnew(EditorPropertyQuat);
  3054. double min = -65535, max = 65535, step = default_float_step;
  3055. bool hide_slider = true;
  3056. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  3057. min = p_hint_text.get_slice(",", 0).to_float();
  3058. max = p_hint_text.get_slice(",", 1).to_float();
  3059. if (p_hint_text.get_slice_count(",") >= 3) {
  3060. step = p_hint_text.get_slice(",", 2).to_float();
  3061. }
  3062. hide_slider = false;
  3063. }
  3064. editor->setup(min, max, step, hide_slider);
  3065. add_property_editor(p_path, editor);
  3066. } break;
  3067. case Variant::AABB: {
  3068. EditorPropertyAABB *editor = memnew(EditorPropertyAABB);
  3069. double min = -65535, max = 65535, step = default_float_step;
  3070. bool hide_slider = true;
  3071. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  3072. min = p_hint_text.get_slice(",", 0).to_float();
  3073. max = p_hint_text.get_slice(",", 1).to_float();
  3074. if (p_hint_text.get_slice_count(",") >= 3) {
  3075. step = p_hint_text.get_slice(",", 2).to_float();
  3076. }
  3077. hide_slider = false;
  3078. }
  3079. editor->setup(min, max, step, hide_slider);
  3080. add_property_editor(p_path, editor);
  3081. } break;
  3082. case Variant::BASIS: {
  3083. EditorPropertyBasis *editor = memnew(EditorPropertyBasis);
  3084. double min = -65535, max = 65535, step = default_float_step;
  3085. bool hide_slider = true;
  3086. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  3087. min = p_hint_text.get_slice(",", 0).to_float();
  3088. max = p_hint_text.get_slice(",", 1).to_float();
  3089. if (p_hint_text.get_slice_count(",") >= 3) {
  3090. step = p_hint_text.get_slice(",", 2).to_float();
  3091. }
  3092. hide_slider = false;
  3093. }
  3094. editor->setup(min, max, step, hide_slider);
  3095. add_property_editor(p_path, editor);
  3096. } break;
  3097. case Variant::TRANSFORM: {
  3098. EditorPropertyTransform *editor = memnew(EditorPropertyTransform);
  3099. double min = -65535, max = 65535, step = default_float_step;
  3100. bool hide_slider = true;
  3101. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  3102. min = p_hint_text.get_slice(",", 0).to_float();
  3103. max = p_hint_text.get_slice(",", 1).to_float();
  3104. if (p_hint_text.get_slice_count(",") >= 3) {
  3105. step = p_hint_text.get_slice(",", 2).to_float();
  3106. }
  3107. hide_slider = false;
  3108. }
  3109. editor->setup(min, max, step, hide_slider);
  3110. add_property_editor(p_path, editor);
  3111. } break;
  3112. // misc types
  3113. case Variant::COLOR: {
  3114. EditorPropertyColor *editor = memnew(EditorPropertyColor);
  3115. editor->setup(p_hint != PROPERTY_HINT_COLOR_NO_ALPHA);
  3116. add_property_editor(p_path, editor);
  3117. } break;
  3118. case Variant::STRING_NAME: {
  3119. if (p_hint == PROPERTY_HINT_ENUM) {
  3120. EditorPropertyTextEnum *editor = memnew(EditorPropertyTextEnum);
  3121. Vector<String> options = p_hint_text.split(",");
  3122. editor->setup(options, true);
  3123. add_property_editor(p_path, editor);
  3124. } else {
  3125. EditorPropertyText *editor = memnew(EditorPropertyText);
  3126. if (p_hint == PROPERTY_HINT_PLACEHOLDER_TEXT) {
  3127. editor->set_placeholder(p_hint_text);
  3128. }
  3129. editor->set_string_name(true);
  3130. add_property_editor(p_path, editor);
  3131. }
  3132. } break;
  3133. case Variant::NODE_PATH: {
  3134. EditorPropertyNodePath *editor = memnew(EditorPropertyNodePath);
  3135. if (p_hint == PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE && p_hint_text != String()) {
  3136. editor->setup(p_hint_text, Vector<StringName>(), (p_usage & PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT));
  3137. }
  3138. if (p_hint == PROPERTY_HINT_NODE_PATH_VALID_TYPES && p_hint_text != String()) {
  3139. Vector<String> types = p_hint_text.split(",", false);
  3140. Vector<StringName> sn = Variant(types); //convert via variant
  3141. editor->setup(NodePath(), sn, (p_usage & PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT));
  3142. }
  3143. add_property_editor(p_path, editor);
  3144. } break;
  3145. case Variant::RID: {
  3146. EditorPropertyRID *editor = memnew(EditorPropertyRID);
  3147. add_property_editor(p_path, editor);
  3148. } break;
  3149. case Variant::OBJECT: {
  3150. EditorPropertyResource *editor = memnew(EditorPropertyResource);
  3151. editor->setup(p_hint == PROPERTY_HINT_RESOURCE_TYPE ? p_hint_text : "Resource");
  3152. if (p_hint == PROPERTY_HINT_RESOURCE_TYPE) {
  3153. String open_in_new = EDITOR_GET("interface/inspector/resources_to_open_in_new_inspector");
  3154. for (int i = 0; i < open_in_new.get_slice_count(","); i++) {
  3155. String type = open_in_new.get_slicec(',', i).strip_edges();
  3156. for (int j = 0; j < p_hint_text.get_slice_count(","); j++) {
  3157. String inherits = p_hint_text.get_slicec(',', j);
  3158. if (ClassDB::is_parent_class(inherits, type)) {
  3159. editor->set_use_sub_inspector(false);
  3160. }
  3161. }
  3162. }
  3163. }
  3164. add_property_editor(p_path, editor);
  3165. } break;
  3166. case Variant::DICTIONARY: {
  3167. EditorPropertyDictionary *editor = memnew(EditorPropertyDictionary);
  3168. add_property_editor(p_path, editor);
  3169. } break;
  3170. case Variant::ARRAY: {
  3171. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  3172. editor->setup(Variant::ARRAY, p_hint_text);
  3173. add_property_editor(p_path, editor);
  3174. } break;
  3175. case Variant::PACKED_BYTE_ARRAY: {
  3176. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  3177. editor->setup(Variant::PACKED_BYTE_ARRAY);
  3178. add_property_editor(p_path, editor);
  3179. } break;
  3180. case Variant::PACKED_INT32_ARRAY: {
  3181. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  3182. editor->setup(Variant::PACKED_INT32_ARRAY);
  3183. add_property_editor(p_path, editor);
  3184. } break;
  3185. case Variant::PACKED_INT64_ARRAY: {
  3186. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  3187. editor->setup(Variant::PACKED_INT64_ARRAY);
  3188. add_property_editor(p_path, editor);
  3189. } break;
  3190. case Variant::PACKED_FLOAT32_ARRAY: {
  3191. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  3192. editor->setup(Variant::PACKED_FLOAT32_ARRAY);
  3193. add_property_editor(p_path, editor);
  3194. } break;
  3195. case Variant::PACKED_FLOAT64_ARRAY: {
  3196. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  3197. editor->setup(Variant::PACKED_FLOAT64_ARRAY);
  3198. add_property_editor(p_path, editor);
  3199. } break;
  3200. case Variant::PACKED_STRING_ARRAY: {
  3201. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  3202. editor->setup(Variant::PACKED_STRING_ARRAY);
  3203. add_property_editor(p_path, editor);
  3204. } break;
  3205. case Variant::PACKED_VECTOR2_ARRAY: {
  3206. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  3207. editor->setup(Variant::PACKED_VECTOR2_ARRAY);
  3208. add_property_editor(p_path, editor);
  3209. } break;
  3210. case Variant::PACKED_VECTOR3_ARRAY: {
  3211. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  3212. editor->setup(Variant::PACKED_VECTOR3_ARRAY);
  3213. add_property_editor(p_path, editor);
  3214. } break;
  3215. case Variant::PACKED_COLOR_ARRAY: {
  3216. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  3217. editor->setup(Variant::PACKED_COLOR_ARRAY);
  3218. add_property_editor(p_path, editor);
  3219. } break;
  3220. default: {
  3221. }
  3222. }
  3223. return false; //can be overridden, although it will most likely be last anyway
  3224. }
  3225. void EditorInspectorDefaultPlugin::parse_end() {
  3226. //do none
  3227. }