editor_properties.cpp 107 KB

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