editor_properties.cpp 91 KB

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