editor_properties.cpp 114 KB

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