editor_properties.cpp 96 KB

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