editor_properties.cpp 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302
  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: {
  2307. } //compiler could be smarter here and realize this can't happen
  2308. }
  2309. EditorPropertyLayers *editor = memnew(EditorPropertyLayers);
  2310. editor->setup(lt);
  2311. add_property_editor(p_path, editor);
  2312. } else if (p_hint == PROPERTY_HINT_OBJECT_ID) {
  2313. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  2314. editor->setup(p_hint_text);
  2315. add_property_editor(p_path, editor);
  2316. } else {
  2317. EditorPropertyInteger *editor = memnew(EditorPropertyInteger);
  2318. int min = 0, max = 65535, step = 1;
  2319. bool greater = true, lesser = true;
  2320. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2321. greater = false; //if using ranged, assume false by default
  2322. lesser = false;
  2323. min = p_hint_text.get_slice(",", 0).to_int();
  2324. max = p_hint_text.get_slice(",", 1).to_int();
  2325. if (p_hint_text.get_slice_count(",") >= 3) {
  2326. step = p_hint_text.get_slice(",", 2).to_int();
  2327. }
  2328. for (int i = 2; i < p_hint_text.get_slice_count(","); i++) {
  2329. String slice = p_hint_text.get_slice(",", i).strip_edges();
  2330. if (slice == "or_greater") {
  2331. greater = true;
  2332. }
  2333. if (slice == "or_lesser") {
  2334. lesser = true;
  2335. }
  2336. }
  2337. }
  2338. editor->setup(min, max, step, greater, lesser);
  2339. add_property_editor(p_path, editor);
  2340. }
  2341. } break;
  2342. case Variant::REAL: {
  2343. if (p_hint == PROPERTY_HINT_EXP_EASING) {
  2344. EditorPropertyEasing *editor = memnew(EditorPropertyEasing);
  2345. bool full = true;
  2346. bool flip = false;
  2347. Vector<String> hints = p_hint_text.split(",");
  2348. for (int i = 0; i < hints.size(); i++) {
  2349. String h = hints[i].strip_edges();
  2350. if (h == "attenuation") {
  2351. flip = true;
  2352. }
  2353. if (h == "inout") {
  2354. full = true;
  2355. }
  2356. }
  2357. editor->setup(full, flip);
  2358. add_property_editor(p_path, editor);
  2359. } else {
  2360. EditorPropertyFloat *editor = memnew(EditorPropertyFloat);
  2361. double min = -65535, max = 65535, step = 0.001;
  2362. bool hide_slider = true;
  2363. bool exp_range = false;
  2364. bool greater = true, lesser = true;
  2365. if ((p_hint == PROPERTY_HINT_RANGE || p_hint == PROPERTY_HINT_EXP_RANGE) && p_hint_text.get_slice_count(",") >= 2) {
  2366. greater = false; //if using ranged, assume false by default
  2367. lesser = false;
  2368. min = p_hint_text.get_slice(",", 0).to_double();
  2369. max = p_hint_text.get_slice(",", 1).to_double();
  2370. if (p_hint_text.get_slice_count(",") >= 3) {
  2371. step = p_hint_text.get_slice(",", 2).to_double();
  2372. }
  2373. hide_slider = false;
  2374. exp_range = p_hint == PROPERTY_HINT_EXP_RANGE;
  2375. for (int i = 2; i < p_hint_text.get_slice_count(","); i++) {
  2376. String slice = p_hint_text.get_slice(",", i).strip_edges();
  2377. if (slice == "or_greater") {
  2378. greater = true;
  2379. }
  2380. if (slice == "or_lesser") {
  2381. lesser = true;
  2382. }
  2383. }
  2384. }
  2385. editor->setup(min, max, step, hide_slider, exp_range, greater, lesser);
  2386. add_property_editor(p_path, editor);
  2387. }
  2388. } break;
  2389. case Variant::STRING: {
  2390. if (p_hint == PROPERTY_HINT_ENUM) {
  2391. EditorPropertyTextEnum *editor = memnew(EditorPropertyTextEnum);
  2392. Vector<String> options = p_hint_text.split(",");
  2393. editor->setup(options);
  2394. add_property_editor(p_path, editor);
  2395. } else if (p_hint == PROPERTY_HINT_MULTILINE_TEXT) {
  2396. EditorPropertyMultilineText *editor = memnew(EditorPropertyMultilineText);
  2397. add_property_editor(p_path, editor);
  2398. } else if (p_hint == PROPERTY_HINT_TYPE_STRING) {
  2399. EditorPropertyClassName *editor = memnew(EditorPropertyClassName);
  2400. editor->setup("Object", p_hint_text);
  2401. add_property_editor(p_path, editor);
  2402. } else if (p_hint == PROPERTY_HINT_DIR || p_hint == PROPERTY_HINT_FILE || p_hint == PROPERTY_HINT_SAVE_FILE || p_hint == PROPERTY_HINT_GLOBAL_DIR || p_hint == PROPERTY_HINT_GLOBAL_FILE) {
  2403. Vector<String> extensions = p_hint_text.split(",");
  2404. bool global = p_hint == PROPERTY_HINT_GLOBAL_DIR || p_hint == PROPERTY_HINT_GLOBAL_FILE;
  2405. bool folder = p_hint == PROPERTY_HINT_DIR || p_hint == PROPERTY_HINT_GLOBAL_DIR;
  2406. bool save = p_hint == PROPERTY_HINT_SAVE_FILE;
  2407. EditorPropertyPath *editor = memnew(EditorPropertyPath);
  2408. editor->setup(extensions, folder, global);
  2409. if (save) {
  2410. editor->set_save_mode();
  2411. }
  2412. add_property_editor(p_path, editor);
  2413. } else if (p_hint == PROPERTY_HINT_METHOD_OF_VARIANT_TYPE ||
  2414. p_hint == PROPERTY_HINT_METHOD_OF_BASE_TYPE ||
  2415. p_hint == PROPERTY_HINT_METHOD_OF_INSTANCE ||
  2416. p_hint == PROPERTY_HINT_METHOD_OF_SCRIPT ||
  2417. p_hint == PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE ||
  2418. p_hint == PROPERTY_HINT_PROPERTY_OF_BASE_TYPE ||
  2419. p_hint == PROPERTY_HINT_PROPERTY_OF_INSTANCE ||
  2420. p_hint == PROPERTY_HINT_PROPERTY_OF_SCRIPT) {
  2421. EditorPropertyMember *editor = memnew(EditorPropertyMember);
  2422. EditorPropertyMember::Type type = EditorPropertyMember::MEMBER_METHOD_OF_BASE_TYPE;
  2423. switch (p_hint) {
  2424. case PROPERTY_HINT_METHOD_OF_BASE_TYPE: type = EditorPropertyMember::MEMBER_METHOD_OF_BASE_TYPE; break;
  2425. case PROPERTY_HINT_METHOD_OF_INSTANCE: type = EditorPropertyMember::MEMBER_METHOD_OF_INSTANCE; break;
  2426. case PROPERTY_HINT_METHOD_OF_SCRIPT: type = EditorPropertyMember::MEMBER_METHOD_OF_SCRIPT; break;
  2427. case PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE: type = EditorPropertyMember::MEMBER_PROPERTY_OF_VARIANT_TYPE; break;
  2428. case PROPERTY_HINT_PROPERTY_OF_BASE_TYPE: type = EditorPropertyMember::MEMBER_PROPERTY_OF_BASE_TYPE; break;
  2429. case PROPERTY_HINT_PROPERTY_OF_INSTANCE: type = EditorPropertyMember::MEMBER_PROPERTY_OF_INSTANCE; break;
  2430. case PROPERTY_HINT_PROPERTY_OF_SCRIPT: type = EditorPropertyMember::MEMBER_PROPERTY_OF_SCRIPT; break;
  2431. default: {
  2432. }
  2433. }
  2434. editor->setup(type, p_hint_text);
  2435. add_property_editor(p_path, editor);
  2436. } else {
  2437. EditorPropertyText *editor = memnew(EditorPropertyText);
  2438. if (p_hint == PROPERTY_HINT_PLACEHOLDER_TEXT) {
  2439. editor->set_placeholder(p_hint_text);
  2440. }
  2441. add_property_editor(p_path, editor);
  2442. }
  2443. } break;
  2444. // math types
  2445. case Variant::VECTOR2: {
  2446. EditorPropertyVector2 *editor = memnew(EditorPropertyVector2);
  2447. double min = -65535, max = 65535, step = 0.001;
  2448. bool hide_slider = true;
  2449. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2450. min = p_hint_text.get_slice(",", 0).to_double();
  2451. max = p_hint_text.get_slice(",", 1).to_double();
  2452. if (p_hint_text.get_slice_count(",") >= 3) {
  2453. step = p_hint_text.get_slice(",", 2).to_double();
  2454. }
  2455. hide_slider = false;
  2456. }
  2457. editor->setup(min, max, step, hide_slider);
  2458. add_property_editor(p_path, editor);
  2459. } break; // 5
  2460. case Variant::RECT2: {
  2461. EditorPropertyRect2 *editor = memnew(EditorPropertyRect2);
  2462. double min = -65535, max = 65535, step = 0.001;
  2463. bool hide_slider = true;
  2464. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2465. min = p_hint_text.get_slice(",", 0).to_double();
  2466. max = p_hint_text.get_slice(",", 1).to_double();
  2467. if (p_hint_text.get_slice_count(",") >= 3) {
  2468. step = p_hint_text.get_slice(",", 2).to_double();
  2469. }
  2470. hide_slider = false;
  2471. }
  2472. editor->setup(min, max, step, hide_slider);
  2473. add_property_editor(p_path, editor);
  2474. } break;
  2475. case Variant::VECTOR3: {
  2476. EditorPropertyVector3 *editor = memnew(EditorPropertyVector3);
  2477. double min = -65535, max = 65535, step = 0.001;
  2478. bool hide_slider = true;
  2479. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2480. min = p_hint_text.get_slice(",", 0).to_double();
  2481. max = p_hint_text.get_slice(",", 1).to_double();
  2482. if (p_hint_text.get_slice_count(",") >= 3) {
  2483. step = p_hint_text.get_slice(",", 2).to_double();
  2484. }
  2485. hide_slider = false;
  2486. }
  2487. editor->setup(min, max, step, hide_slider);
  2488. add_property_editor(p_path, editor);
  2489. } break;
  2490. case Variant::TRANSFORM2D: {
  2491. EditorPropertyTransform2D *editor = memnew(EditorPropertyTransform2D);
  2492. double min = -65535, max = 65535, step = 0.001;
  2493. bool hide_slider = true;
  2494. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2495. min = p_hint_text.get_slice(",", 0).to_double();
  2496. max = p_hint_text.get_slice(",", 1).to_double();
  2497. if (p_hint_text.get_slice_count(",") >= 3) {
  2498. step = p_hint_text.get_slice(",", 2).to_double();
  2499. }
  2500. hide_slider = false;
  2501. }
  2502. editor->setup(min, max, step, hide_slider);
  2503. add_property_editor(p_path, editor);
  2504. } break;
  2505. case Variant::PLANE: {
  2506. EditorPropertyPlane *editor = memnew(EditorPropertyPlane);
  2507. double min = -65535, max = 65535, step = 0.001;
  2508. bool hide_slider = true;
  2509. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2510. min = p_hint_text.get_slice(",", 0).to_double();
  2511. max = p_hint_text.get_slice(",", 1).to_double();
  2512. if (p_hint_text.get_slice_count(",") >= 3) {
  2513. step = p_hint_text.get_slice(",", 2).to_double();
  2514. }
  2515. hide_slider = false;
  2516. }
  2517. editor->setup(min, max, step, hide_slider);
  2518. add_property_editor(p_path, editor);
  2519. } break;
  2520. case Variant::QUAT: {
  2521. EditorPropertyQuat *editor = memnew(EditorPropertyQuat);
  2522. double min = -65535, max = 65535, step = 0.001;
  2523. bool hide_slider = true;
  2524. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2525. min = p_hint_text.get_slice(",", 0).to_double();
  2526. max = p_hint_text.get_slice(",", 1).to_double();
  2527. if (p_hint_text.get_slice_count(",") >= 3) {
  2528. step = p_hint_text.get_slice(",", 2).to_double();
  2529. }
  2530. hide_slider = false;
  2531. }
  2532. editor->setup(min, max, step, hide_slider);
  2533. add_property_editor(p_path, editor);
  2534. } break; // 10
  2535. case Variant::AABB: {
  2536. EditorPropertyAABB *editor = memnew(EditorPropertyAABB);
  2537. double min = -65535, max = 65535, step = 0.001;
  2538. bool hide_slider = true;
  2539. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2540. min = p_hint_text.get_slice(",", 0).to_double();
  2541. max = p_hint_text.get_slice(",", 1).to_double();
  2542. if (p_hint_text.get_slice_count(",") >= 3) {
  2543. step = p_hint_text.get_slice(",", 2).to_double();
  2544. }
  2545. hide_slider = false;
  2546. }
  2547. editor->setup(min, max, step, hide_slider);
  2548. add_property_editor(p_path, editor);
  2549. } break;
  2550. case Variant::BASIS: {
  2551. EditorPropertyBasis *editor = memnew(EditorPropertyBasis);
  2552. double min = -65535, max = 65535, step = 0.001;
  2553. bool hide_slider = true;
  2554. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2555. min = p_hint_text.get_slice(",", 0).to_double();
  2556. max = p_hint_text.get_slice(",", 1).to_double();
  2557. if (p_hint_text.get_slice_count(",") >= 3) {
  2558. step = p_hint_text.get_slice(",", 2).to_double();
  2559. }
  2560. hide_slider = false;
  2561. }
  2562. editor->setup(min, max, step, hide_slider);
  2563. add_property_editor(p_path, editor);
  2564. } break;
  2565. case Variant::TRANSFORM: {
  2566. EditorPropertyTransform *editor = memnew(EditorPropertyTransform);
  2567. double min = -65535, max = 65535, step = 0.001;
  2568. bool hide_slider = true;
  2569. if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
  2570. min = p_hint_text.get_slice(",", 0).to_double();
  2571. max = p_hint_text.get_slice(",", 1).to_double();
  2572. if (p_hint_text.get_slice_count(",") >= 3) {
  2573. step = p_hint_text.get_slice(",", 2).to_double();
  2574. }
  2575. hide_slider = false;
  2576. }
  2577. editor->setup(min, max, step, hide_slider);
  2578. add_property_editor(p_path, editor);
  2579. } break;
  2580. // misc types
  2581. case Variant::COLOR: {
  2582. EditorPropertyColor *editor = memnew(EditorPropertyColor);
  2583. editor->setup(p_hint != PROPERTY_HINT_COLOR_NO_ALPHA);
  2584. add_property_editor(p_path, editor);
  2585. } break;
  2586. case Variant::NODE_PATH: {
  2587. EditorPropertyNodePath *editor = memnew(EditorPropertyNodePath);
  2588. if (p_hint == PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE && p_hint_text != String()) {
  2589. editor->setup(p_hint_text, Vector<StringName>(), (p_usage & PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT));
  2590. }
  2591. if (p_hint == PROPERTY_HINT_NODE_PATH_VALID_TYPES && p_hint_text != String()) {
  2592. Vector<String> types = p_hint_text.split(",", false);
  2593. Vector<StringName> sn = Variant(types); //convert via variant
  2594. editor->setup(NodePath(), sn, (p_usage & PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT));
  2595. }
  2596. add_property_editor(p_path, editor);
  2597. } break; // 15
  2598. case Variant::_RID: {
  2599. EditorPropertyRID *editor = memnew(EditorPropertyRID);
  2600. add_property_editor(p_path, editor);
  2601. } break;
  2602. case Variant::OBJECT: {
  2603. EditorPropertyResource *editor = memnew(EditorPropertyResource);
  2604. editor->setup(p_hint == PROPERTY_HINT_RESOURCE_TYPE ? p_hint_text : "Resource");
  2605. if (p_hint == PROPERTY_HINT_RESOURCE_TYPE) {
  2606. String open_in_new = EDITOR_GET("interface/inspector/resources_to_open_in_new_inspector");
  2607. for (int i = 0; i < open_in_new.get_slice_count(","); i++) {
  2608. String type = open_in_new.get_slicec(',', i).strip_edges();
  2609. for (int j = 0; j < p_hint_text.get_slice_count(","); j++) {
  2610. String inherits = p_hint_text.get_slicec(',', j);
  2611. if (ClassDB::is_parent_class(inherits, type)) {
  2612. editor->set_use_sub_inspector(false);
  2613. }
  2614. }
  2615. }
  2616. }
  2617. add_property_editor(p_path, editor);
  2618. } break;
  2619. case Variant::DICTIONARY: {
  2620. EditorPropertyDictionary *editor = memnew(EditorPropertyDictionary);
  2621. add_property_editor(p_path, editor);
  2622. } break;
  2623. case Variant::ARRAY: {
  2624. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2625. editor->setup(Variant::ARRAY, p_hint_text);
  2626. add_property_editor(p_path, editor);
  2627. } break;
  2628. case Variant::POOL_BYTE_ARRAY: {
  2629. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2630. editor->setup(Variant::POOL_BYTE_ARRAY);
  2631. add_property_editor(p_path, editor);
  2632. } break; // 20
  2633. case Variant::POOL_INT_ARRAY: {
  2634. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2635. editor->setup(Variant::POOL_INT_ARRAY);
  2636. add_property_editor(p_path, editor);
  2637. } break;
  2638. case Variant::POOL_REAL_ARRAY: {
  2639. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2640. editor->setup(Variant::POOL_REAL_ARRAY);
  2641. add_property_editor(p_path, editor);
  2642. } break;
  2643. case Variant::POOL_STRING_ARRAY: {
  2644. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2645. editor->setup(Variant::POOL_STRING_ARRAY);
  2646. add_property_editor(p_path, editor);
  2647. } break;
  2648. case Variant::POOL_VECTOR2_ARRAY: {
  2649. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2650. editor->setup(Variant::POOL_VECTOR2_ARRAY);
  2651. add_property_editor(p_path, editor);
  2652. } break;
  2653. case Variant::POOL_VECTOR3_ARRAY: {
  2654. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2655. editor->setup(Variant::POOL_VECTOR3_ARRAY);
  2656. add_property_editor(p_path, editor);
  2657. } break; // 25
  2658. case Variant::POOL_COLOR_ARRAY: {
  2659. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  2660. editor->setup(Variant::POOL_COLOR_ARRAY);
  2661. add_property_editor(p_path, editor);
  2662. } break;
  2663. default: {
  2664. }
  2665. }
  2666. return false; //can be overridden, although it will most likely be last anyway
  2667. }
  2668. void EditorInspectorDefaultPlugin::parse_end() {
  2669. //do none
  2670. }