property_editor.cpp 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345
  1. /*************************************************************************/
  2. /* property_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "property_editor.h"
  31. #include "array_property_edit.h"
  32. #include "dictionary_property_edit.h"
  33. #include "editor_file_system.h"
  34. #include "editor_help.h"
  35. #include "editor_import_export.h"
  36. #include "editor_node.h"
  37. #include "editor_settings.h"
  38. #include "globals.h"
  39. #include "io/image_loader.h"
  40. #include "io/resource_loader.h"
  41. #include "multi_node_edit.h"
  42. #include "object_type_db.h"
  43. #include "os/input.h"
  44. #include "os/keyboard.h"
  45. #include "pair.h"
  46. #include "plugins/script_editor_plugin.h"
  47. #include "print_string.h"
  48. #include "scene/gui/label.h"
  49. #include "scene/main/viewport.h"
  50. #include "scene/resources/font.h"
  51. #include "scene/resources/packed_scene.h"
  52. #include "scene/scene_string_names.h"
  53. #include "script_editor_debugger.h"
  54. void CustomPropertyEditor::_notification(int p_what) {
  55. if (p_what == NOTIFICATION_DRAW) {
  56. RID ci = get_canvas_item();
  57. get_stylebox("panel", "PopupMenu")->draw(ci, Rect2(Point2(), get_size()));
  58. /*
  59. if (v.get_type()==Variant::COLOR) {
  60. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2( 10,10,60, get_size().height-20 ), v );
  61. }*/
  62. }
  63. }
  64. void CustomPropertyEditor::_menu_option(int p_which) {
  65. switch (type) {
  66. case Variant::INT: {
  67. if (hint == PROPERTY_HINT_FLAGS) {
  68. int val = v;
  69. if (val & (1 << p_which)) {
  70. val &= ~(1 << p_which);
  71. } else {
  72. val |= (1 << p_which);
  73. }
  74. v = val;
  75. emit_signal("variant_changed");
  76. }
  77. } break;
  78. case Variant::OBJECT: {
  79. switch (p_which) {
  80. case OBJ_MENU_LOAD: {
  81. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  82. String type = (hint == PROPERTY_HINT_RESOURCE_TYPE) ? hint_text : String();
  83. List<String> extensions;
  84. for (int i = 0; i < type.get_slice_count(","); i++) {
  85. ResourceLoader::get_recognized_extensions_for_type(type.get_slice(",", i), &extensions);
  86. }
  87. Set<String> valid_extensions;
  88. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  89. valid_extensions.insert(E->get());
  90. }
  91. file->clear_filters();
  92. for (Set<String>::Element *E = valid_extensions.front(); E; E = E->next()) {
  93. file->add_filter("*." + E->get() + " ; " + E->get().to_upper());
  94. }
  95. file->popup_centered_ratio();
  96. } break;
  97. case OBJ_MENU_EDIT: {
  98. RefPtr RefPtr = v;
  99. if (!RefPtr.is_null()) {
  100. emit_signal("resource_edit_request");
  101. hide();
  102. }
  103. } break;
  104. case OBJ_MENU_CLEAR: {
  105. v = Variant();
  106. emit_signal("variant_changed");
  107. hide();
  108. } break;
  109. case OBJ_MENU_MAKE_UNIQUE: {
  110. RefPtr RefPtr = v;
  111. Ref<Resource> res_orig = RefPtr;
  112. if (res_orig.is_null())
  113. return;
  114. List<PropertyInfo> property_list;
  115. res_orig->get_property_list(&property_list);
  116. List<Pair<String, Variant> > propvalues;
  117. for (List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
  118. Pair<String, Variant> p;
  119. PropertyInfo &pi = E->get();
  120. if (pi.usage & PROPERTY_USAGE_STORAGE) {
  121. p.first = pi.name;
  122. p.second = res_orig->get(pi.name);
  123. }
  124. propvalues.push_back(p);
  125. }
  126. String orig_type = res_orig->get_type();
  127. Object *inst = ObjectTypeDB::instance(orig_type);
  128. Ref<Resource> res = Ref<Resource>(inst->cast_to<Resource>());
  129. ERR_FAIL_COND(res.is_null());
  130. for (List<Pair<String, Variant> >::Element *E = propvalues.front(); E; E = E->next()) {
  131. Pair<String, Variant> &p = E->get();
  132. res->set(p.first, p.second);
  133. }
  134. v = res.get_ref_ptr();
  135. emit_signal("variant_changed");
  136. hide();
  137. } break;
  138. case OBJ_MENU_COPY: {
  139. EditorSettings::get_singleton()->set_resource_clipboard(v);
  140. } break;
  141. case OBJ_MENU_PASTE: {
  142. v = EditorSettings::get_singleton()->get_resource_clipboard();
  143. emit_signal("variant_changed");
  144. } break;
  145. case OBJ_MENU_REIMPORT: {
  146. RES r = v;
  147. if (r.is_valid() && r->get_import_metadata().is_valid()) {
  148. Ref<ResourceImportMetadata> rimd = r->get_import_metadata();
  149. Ref<EditorImportPlugin> eip = EditorImportExport::get_singleton()->get_import_plugin_by_name(rimd->get_editor());
  150. if (eip.is_valid()) {
  151. eip->import_dialog(r->get_path());
  152. }
  153. }
  154. } break;
  155. case OBJ_MENU_NEW_SCRIPT: {
  156. if (owner->cast_to<Node>())
  157. EditorNode::get_singleton()->get_scene_tree_dock()->open_script_dialog(owner->cast_to<Node>());
  158. } break;
  159. case OBJ_MENU_SHOW_IN_FILE_SYSTEM: {
  160. RES r = v;
  161. FileSystemDock *file_system_dock = EditorNode::get_singleton()->get_filesystem_dock();
  162. file_system_dock->navigate_to_path(r->get_path());
  163. // Ensure that the FileSystem dock is visible.
  164. TabContainer *tab_container = (TabContainer *)file_system_dock->get_parent_control();
  165. tab_container->set_current_tab(file_system_dock->get_position_in_parent());
  166. } break;
  167. default: {
  168. ERR_FAIL_COND(inheritors_array.empty());
  169. String intype = inheritors_array[p_which - TYPE_BASE_ID];
  170. Object *obj = ObjectTypeDB::instance(intype);
  171. ERR_BREAK(!obj);
  172. Resource *res = obj->cast_to<Resource>();
  173. ERR_BREAK(!res);
  174. v = Ref<Resource>(res).get_ref_ptr();
  175. emit_signal("variant_changed");
  176. } break;
  177. }
  178. } break;
  179. default: {}
  180. }
  181. }
  182. void CustomPropertyEditor::hide_menu() {
  183. menu->hide();
  184. }
  185. Variant CustomPropertyEditor::get_variant() const {
  186. return v;
  187. }
  188. String CustomPropertyEditor::get_name() const {
  189. return name;
  190. }
  191. bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant::Type p_type, const Variant &p_variant, int p_hint, String p_hint_text) {
  192. owner = p_owner;
  193. updating = true;
  194. name = p_name;
  195. v = p_variant;
  196. field_names.clear();
  197. hint = p_hint;
  198. hint_text = p_hint_text;
  199. type_button->hide();
  200. if (color_picker)
  201. color_picker->hide();
  202. texture_preview->hide();
  203. inheritors_array.clear();
  204. text_edit->hide();
  205. easing_draw->hide();
  206. spinbox->hide();
  207. slider->hide();
  208. for (int i = 0; i < MAX_VALUE_EDITORS; i++) {
  209. value_editor[i]->hide();
  210. value_label[i]->hide();
  211. if (i < 4)
  212. scroll[i]->hide();
  213. }
  214. for (int i = 0; i < MAX_ACTION_BUTTONS; i++) {
  215. action_buttons[i]->hide();
  216. }
  217. for (int i = 0; i < 20; i++)
  218. checks20[i]->hide();
  219. type = (p_variant.get_type() != Variant::NIL && p_variant.get_type() != Variant::_RID && p_type != Variant::OBJECT) ? p_variant.get_type() : p_type;
  220. switch (type) {
  221. case Variant::INT:
  222. case Variant::REAL: {
  223. if (hint == PROPERTY_HINT_RANGE) {
  224. int c = hint_text.get_slice_count(",");
  225. float min = 0, max = 100, step = 1;
  226. if (c >= 1) {
  227. if (!hint_text.get_slice(",", 0).empty())
  228. min = hint_text.get_slice(",", 0).to_double();
  229. }
  230. if (c >= 2) {
  231. if (!hint_text.get_slice(",", 1).empty())
  232. max = hint_text.get_slice(",", 1).to_double();
  233. }
  234. if (c >= 3) {
  235. if (!hint_text.get_slice(",", 2).empty())
  236. step = hint_text.get_slice(",", 2).to_double();
  237. }
  238. if (c >= 4 && hint_text.get_slice(",", 3) == "slider") {
  239. slider->set_min(min);
  240. slider->set_max(max);
  241. slider->set_step(step);
  242. slider->set_val(v);
  243. slider->show();
  244. set_size(Size2(110, 30) * EDSCALE);
  245. } else {
  246. spinbox->set_min(min);
  247. spinbox->set_max(max);
  248. spinbox->set_step(step);
  249. spinbox->set_val(v);
  250. spinbox->show();
  251. set_size(Size2(70, 35) * EDSCALE);
  252. }
  253. } else if (hint == PROPERTY_HINT_ALL_FLAGS) {
  254. uint32_t flgs = v;
  255. for (int i = 0; i < 2; i++) {
  256. Point2 ofs(4, 4);
  257. ofs.y += 22 * i;
  258. for (int j = 0; j < 10; j++) {
  259. CheckBox *c = checks20[i * 10 + j];
  260. Point2 o = ofs;
  261. o.x += j * 22;
  262. if (j >= 5)
  263. o.x += 4;
  264. c->set_pos(o);
  265. c->set_pressed(flgs & (1 << (i * 10 + j)));
  266. c->show();
  267. }
  268. }
  269. set_size(checks20[19]->get_pos() + Size2(20, 25) * EDSCALE);
  270. } else if (hint == PROPERTY_HINT_EXP_EASING) {
  271. easing_draw->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 5);
  272. easing_draw->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 5);
  273. easing_draw->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 5);
  274. easing_draw->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 30);
  275. type_button->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 3);
  276. type_button->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 3);
  277. type_button->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, 25);
  278. type_button->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 7);
  279. type_button->set_text(TTR("Preset.."));
  280. type_button->get_popup()->clear();
  281. type_button->get_popup()->add_item(TTR("Linear"), EASING_LINEAR);
  282. type_button->get_popup()->add_item(TTR("Ease In"), EASING_EASE_IN);
  283. type_button->get_popup()->add_item(TTR("Ease Out"), EASING_EASE_OUT);
  284. if (hint_text != "attenuation") {
  285. type_button->get_popup()->add_item(TTR("Zero"), EASING_ZERO);
  286. type_button->get_popup()->add_item(TTR("Easing In-Out"), EASING_IN_OUT);
  287. type_button->get_popup()->add_item(TTR("Easing Out-In"), EASING_OUT_IN);
  288. }
  289. type_button->show();
  290. easing_draw->show();
  291. set_size(Size2(200, 150) * EDSCALE);
  292. } else if (hint == PROPERTY_HINT_FLAGS) {
  293. menu->clear();
  294. Vector<String> flags = hint_text.split(",");
  295. for (int i = 0; i < flags.size(); i++) {
  296. String flag = flags[i];
  297. if (flag == "")
  298. continue;
  299. menu->add_check_item(flag, i);
  300. int f = v;
  301. if (f & (1 << i))
  302. menu->set_item_checked(menu->get_item_index(i), true);
  303. }
  304. menu->set_pos(get_pos());
  305. menu->popup();
  306. hide();
  307. updating = false;
  308. return false;
  309. } else {
  310. List<String> names;
  311. names.push_back("value:");
  312. config_value_editors(1, 1, 50, names);
  313. value_editor[0]->set_text(String::num(v));
  314. }
  315. } break;
  316. case Variant::STRING: {
  317. if (hint == PROPERTY_HINT_FILE || hint == PROPERTY_HINT_GLOBAL_FILE) {
  318. List<String> names;
  319. names.push_back(TTR("File.."));
  320. names.push_back(TTR("Clear"));
  321. config_action_buttons(names);
  322. } else if (hint == PROPERTY_HINT_DIR || hint == PROPERTY_HINT_GLOBAL_DIR) {
  323. List<String> names;
  324. names.push_back(TTR("Dir.."));
  325. names.push_back(TTR("Clear"));
  326. config_action_buttons(names);
  327. } else if (hint == PROPERTY_HINT_ENUM) {
  328. } else if (hint == PROPERTY_HINT_MULTILINE_TEXT) {
  329. text_edit->show();
  330. text_edit->set_text(v);
  331. //action_buttons[0];
  332. int button_margin = get_constant("button_margin", "Dialogs");
  333. int margin = get_constant("margin", "Dialogs");
  334. action_buttons[0]->set_anchor(MARGIN_LEFT, ANCHOR_END);
  335. action_buttons[0]->set_anchor(MARGIN_TOP, ANCHOR_END);
  336. action_buttons[0]->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  337. action_buttons[0]->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  338. action_buttons[0]->set_begin(Point2(70, button_margin - 5));
  339. action_buttons[0]->set_end(Point2(margin, margin));
  340. action_buttons[0]->set_text(TTR("Close"));
  341. action_buttons[0]->show();
  342. } else {
  343. List<String> names;
  344. names.push_back("string:");
  345. config_value_editors(1, 1, 50, names);
  346. value_editor[0]->set_text(v);
  347. }
  348. } break;
  349. case Variant::VECTOR2: {
  350. field_names.push_back("x");
  351. field_names.push_back("y");
  352. config_value_editors(2, 2, 10, field_names);
  353. Vector2 vec = v;
  354. value_editor[0]->set_text(String::num(vec.x));
  355. value_editor[1]->set_text(String::num(vec.y));
  356. } break;
  357. case Variant::RECT2: {
  358. field_names.push_back("x");
  359. field_names.push_back("y");
  360. field_names.push_back("w");
  361. field_names.push_back("h");
  362. config_value_editors(4, 4, 10, field_names);
  363. Rect2 r = v;
  364. value_editor[0]->set_text(String::num(r.pos.x));
  365. value_editor[1]->set_text(String::num(r.pos.y));
  366. value_editor[2]->set_text(String::num(r.size.x));
  367. value_editor[3]->set_text(String::num(r.size.y));
  368. } break;
  369. case Variant::VECTOR3: {
  370. field_names.push_back("x");
  371. field_names.push_back("y");
  372. field_names.push_back("z");
  373. config_value_editors(3, 3, 10, field_names);
  374. Vector3 vec = v;
  375. value_editor[0]->set_text(String::num(vec.x));
  376. value_editor[1]->set_text(String::num(vec.y));
  377. value_editor[2]->set_text(String::num(vec.z));
  378. } break;
  379. case Variant::PLANE: {
  380. field_names.push_back("x");
  381. field_names.push_back("y");
  382. field_names.push_back("z");
  383. field_names.push_back("d");
  384. config_value_editors(4, 4, 10, field_names);
  385. Plane plane = v;
  386. value_editor[0]->set_text(String::num(plane.normal.x));
  387. value_editor[1]->set_text(String::num(plane.normal.y));
  388. value_editor[2]->set_text(String::num(plane.normal.z));
  389. value_editor[3]->set_text(String::num(plane.d));
  390. } break;
  391. case Variant::QUAT: {
  392. field_names.push_back("x");
  393. field_names.push_back("y");
  394. field_names.push_back("z");
  395. field_names.push_back("w");
  396. config_value_editors(4, 4, 10, field_names);
  397. Quat q = v;
  398. value_editor[0]->set_text(String::num(q.x));
  399. value_editor[1]->set_text(String::num(q.y));
  400. value_editor[2]->set_text(String::num(q.z));
  401. value_editor[3]->set_text(String::num(q.w));
  402. } break;
  403. case Variant::_AABB: {
  404. field_names.push_back("px");
  405. field_names.push_back("py");
  406. field_names.push_back("pz");
  407. field_names.push_back("sx");
  408. field_names.push_back("sy");
  409. field_names.push_back("sz");
  410. config_value_editors(6, 3, 16, field_names);
  411. AABB aabb = v;
  412. value_editor[0]->set_text(String::num(aabb.pos.x));
  413. value_editor[1]->set_text(String::num(aabb.pos.y));
  414. value_editor[2]->set_text(String::num(aabb.pos.z));
  415. value_editor[3]->set_text(String::num(aabb.size.x));
  416. value_editor[4]->set_text(String::num(aabb.size.y));
  417. value_editor[5]->set_text(String::num(aabb.size.z));
  418. } break;
  419. case Variant::MATRIX32: {
  420. field_names.push_back("xx");
  421. field_names.push_back("xy");
  422. field_names.push_back("yx");
  423. field_names.push_back("yy");
  424. field_names.push_back("ox");
  425. field_names.push_back("oy");
  426. config_value_editors(6, 2, 16, field_names);
  427. Matrix32 basis = v;
  428. for (int i = 0; i < 6; i++) {
  429. value_editor[i]->set_text(String::num(basis.elements[i / 2][i % 2]));
  430. }
  431. } break;
  432. case Variant::MATRIX3: {
  433. field_names.push_back("xx");
  434. field_names.push_back("xy");
  435. field_names.push_back("xz");
  436. field_names.push_back("yx");
  437. field_names.push_back("yy");
  438. field_names.push_back("yz");
  439. field_names.push_back("zx");
  440. field_names.push_back("zy");
  441. field_names.push_back("zz");
  442. config_value_editors(9, 3, 16, field_names);
  443. Matrix3 basis = v;
  444. for (int i = 0; i < 9; i++) {
  445. value_editor[i]->set_text(String::num(basis.elements[i / 3][i % 3]));
  446. }
  447. } break;
  448. case Variant::TRANSFORM: {
  449. field_names.push_back("xx");
  450. field_names.push_back("xy");
  451. field_names.push_back("xz");
  452. field_names.push_back("xo");
  453. field_names.push_back("yx");
  454. field_names.push_back("yy");
  455. field_names.push_back("yz");
  456. field_names.push_back("yo");
  457. field_names.push_back("zx");
  458. field_names.push_back("zy");
  459. field_names.push_back("zz");
  460. field_names.push_back("zo");
  461. config_value_editors(12, 4, 16, field_names);
  462. Transform tr = v;
  463. for (int i = 0; i < 9; i++) {
  464. value_editor[(i / 3) * 4 + i % 3]->set_text(String::num(tr.basis.elements[i / 3][i % 3]));
  465. }
  466. value_editor[3]->set_text(String::num(tr.origin.x));
  467. value_editor[7]->set_text(String::num(tr.origin.y));
  468. value_editor[11]->set_text(String::num(tr.origin.z));
  469. } break;
  470. case Variant::COLOR: {
  471. if (!color_picker) {
  472. //late init for performance
  473. color_picker = memnew(ColorPicker);
  474. add_child(color_picker);
  475. color_picker->hide();
  476. color_picker->set_area_as_parent_rect();
  477. for (int i = 0; i < 4; i++)
  478. color_picker->set_margin((Margin)i, 5);
  479. color_picker->connect("color_changed", this, "_color_changed");
  480. }
  481. color_picker->show();
  482. color_picker->set_edit_alpha(hint != PROPERTY_HINT_COLOR_NO_ALPHA);
  483. color_picker->set_color(v);
  484. set_size(Size2(300 * EDSCALE, color_picker->get_combined_minimum_size().height + 10 * EDSCALE));
  485. color_picker->set_focus_on_line_edit();
  486. /*
  487. int ofs=80;
  488. int m=10;
  489. int h=20;
  490. Color c=v;
  491. float values[4]={c.r,c.g,c.b,c.a};
  492. for (int i=0;i<4;i++) {
  493. int y=m+i*h;
  494. value_editor[i]->show();
  495. value_label[i]->show();
  496. value_label[i]->set_pos(Point2(ofs,y));
  497. scroll[i]->set_min(0);
  498. scroll[i]->set_max(1.0);
  499. scroll[i]->set_page(0);
  500. scroll[i]->set_pos(Point2(ofs+15,y+Math::floor((h-scroll[i]->get_minimum_size().height)/2.0)));
  501. scroll[i]->set_val(values[i]);
  502. scroll[i]->set_size(Size2(120,1));
  503. scroll[i]->show();
  504. value_editor[i]->set_pos(Point2(ofs+140,y));
  505. value_editor[i]->set_size(Size2(40,h));
  506. value_editor[i]->set_text( String::num(values[i],2 ));
  507. }
  508. value_label[0]->set_text("R");
  509. value_label[1]->set_text("G");
  510. value_label[2]->set_text("B");
  511. value_label[3]->set_text("A");
  512. Size2 new_size = value_editor[3]->get_pos() + value_editor[3]->get_size() + Point2(10,10);
  513. set_size( new_size );
  514. */
  515. } break;
  516. case Variant::IMAGE: {
  517. List<String> names;
  518. names.push_back(TTR("New"));
  519. names.push_back(TTR("Load"));
  520. names.push_back(TTR("Clear"));
  521. config_action_buttons(names);
  522. } break;
  523. case Variant::NODE_PATH: {
  524. List<String> names;
  525. names.push_back(TTR("Assign"));
  526. names.push_back(TTR("Clear"));
  527. config_action_buttons(names);
  528. } break;
  529. case Variant::OBJECT: {
  530. if (hint != PROPERTY_HINT_RESOURCE_TYPE)
  531. break;
  532. menu->clear();
  533. menu->set_size(Size2(1, 1));
  534. if (p_name == "script/script" && hint_text == "Script" && owner->cast_to<Node>()) {
  535. menu->add_icon_item(get_icon("Script", "EditorIcons"), TTR("New Script"), OBJ_MENU_NEW_SCRIPT);
  536. menu->add_separator();
  537. } else if (hint_text != "") {
  538. int idx = 0;
  539. for (int i = 0; i < hint_text.get_slice_count(","); i++) {
  540. String base = hint_text.get_slice(",", i);
  541. Set<String> valid_inheritors;
  542. valid_inheritors.insert(base);
  543. List<StringName> inheritors;
  544. ObjectTypeDB::get_inheriters_from(base.strip_edges(), &inheritors);
  545. List<StringName>::Element *E = inheritors.front();
  546. while (E) {
  547. valid_inheritors.insert(E->get());
  548. E = E->next();
  549. }
  550. for (Set<String>::Element *E = valid_inheritors.front(); E; E = E->next()) {
  551. String t = E->get();
  552. if (!ObjectTypeDB::can_instance(t))
  553. continue;
  554. inheritors_array.push_back(t);
  555. int id = TYPE_BASE_ID + idx;
  556. if (has_icon(t, "EditorIcons")) {
  557. menu->add_icon_item(get_icon(t, "EditorIcons"), TTR("New") + " " + t, id);
  558. } else {
  559. menu->add_item(TTR("New") + " " + t, id);
  560. }
  561. idx++;
  562. }
  563. }
  564. if (menu->get_item_count())
  565. menu->add_separator();
  566. }
  567. menu->add_icon_item(get_icon("Load", "EditorIcons"), TTR("Load"), OBJ_MENU_LOAD);
  568. if (!RES(v).is_null()) {
  569. menu->add_icon_item(get_icon("EditResource", "EditorIcons"), TTR("Edit"), OBJ_MENU_EDIT);
  570. menu->add_icon_item(get_icon("Del", "EditorIcons"), TTR("Clear"), OBJ_MENU_CLEAR);
  571. menu->add_icon_item(get_icon("Duplicate", "EditorIcons"), TTR("Make Unique"), OBJ_MENU_MAKE_UNIQUE);
  572. RES r = v;
  573. if (r.is_valid() && r->get_path().is_resource_file() && r->get_import_metadata().is_valid()) {
  574. menu->add_separator();
  575. menu->add_icon_item(get_icon("ReloadSmall", "EditorIcons"), TTR("Re-Import"), OBJ_MENU_REIMPORT);
  576. }
  577. if (r.is_valid() && r->get_path().is_resource_file()) {
  578. menu->add_separator();
  579. menu->add_item(TTR("Show in File System"), OBJ_MENU_SHOW_IN_FILE_SYSTEM);
  580. }
  581. /*if (r.is_valid() && r->get_path().is_resource_file()) {
  582. menu->set_item_tooltip(1,r->get_path());
  583. } else if (r.is_valid()) {
  584. menu->set_item_tooltip(1,r->get_name()+" ("+r->get_type()+")");
  585. }*/
  586. } else {
  587. }
  588. RES cb = EditorSettings::get_singleton()->get_resource_clipboard();
  589. bool paste_valid = false;
  590. if (cb.is_valid()) {
  591. if (hint_text == "")
  592. paste_valid = true;
  593. else
  594. for (int i = 0; i < hint_text.get_slice_count(","); i++)
  595. if (ObjectTypeDB::is_type(cb->get_type(), hint_text.get_slice(",", i))) {
  596. paste_valid = true;
  597. break;
  598. }
  599. }
  600. if (!RES(v).is_null() || paste_valid) {
  601. menu->add_separator();
  602. if (!RES(v).is_null()) {
  603. menu->add_item(TTR("Copy"), OBJ_MENU_COPY);
  604. }
  605. if (paste_valid) {
  606. menu->add_item(TTR("Paste"), OBJ_MENU_PASTE);
  607. }
  608. }
  609. menu->set_pos(get_pos());
  610. menu->popup();
  611. hide();
  612. updating = false;
  613. return false;
  614. } break;
  615. case Variant::INPUT_EVENT: {
  616. } break;
  617. case Variant::DICTIONARY: {
  618. } break;
  619. case Variant::RAW_ARRAY: {
  620. } break;
  621. case Variant::INT_ARRAY: {
  622. } break;
  623. case Variant::REAL_ARRAY: {
  624. } break;
  625. case Variant::STRING_ARRAY: {
  626. } break;
  627. case Variant::VECTOR3_ARRAY: {
  628. } break;
  629. case Variant::COLOR_ARRAY: {
  630. } break;
  631. default: {}
  632. }
  633. updating = false;
  634. return true;
  635. }
  636. ////void CustomPropertyEditor::_save_properties_values(List<String> p_names) {
  637. ////
  638. //// field_names=p_names;
  639. //// for (int i=0;i<p_names.size();i++) {
  640. //// field_values.push_back(v.get(p_names[i]));
  641. //// }
  642. ////}
  643. void CustomPropertyEditor::_file_selected(String p_file) {
  644. switch (type) {
  645. case Variant::STRING: {
  646. if (hint == PROPERTY_HINT_FILE || hint == PROPERTY_HINT_DIR) {
  647. v = Globals::get_singleton()->localize_path(p_file);
  648. emit_signal("variant_changed");
  649. hide();
  650. }
  651. if (hint == PROPERTY_HINT_GLOBAL_FILE || hint == PROPERTY_HINT_GLOBAL_DIR) {
  652. v = p_file;
  653. emit_signal("variant_changed");
  654. hide();
  655. }
  656. } break;
  657. case Variant::OBJECT: {
  658. String type = (hint == PROPERTY_HINT_RESOURCE_TYPE) ? hint_text : String();
  659. RES res = ResourceLoader::load(p_file, type);
  660. if (res.is_null()) {
  661. error->set_text(TTR("Error loading file: Not a resource!"));
  662. error->popup_centered_minsize();
  663. break;
  664. }
  665. v = res.get_ref_ptr();
  666. emit_signal("variant_changed");
  667. hide();
  668. } break;
  669. case Variant::IMAGE: {
  670. Image image;
  671. Error err = ImageLoader::load_image(p_file, &image);
  672. ERR_EXPLAIN(TTR("Couldn't load image"));
  673. ERR_FAIL_COND(err);
  674. v = image;
  675. emit_signal("variant_changed");
  676. hide();
  677. } break;
  678. default: {}
  679. }
  680. }
  681. void CustomPropertyEditor::_type_create_selected(int p_idx) {
  682. if (type == Variant::INT || type == Variant::REAL) {
  683. float newval = 0;
  684. switch (p_idx) {
  685. case EASING_LINEAR: {
  686. newval = 1;
  687. } break;
  688. case EASING_EASE_IN: {
  689. newval = 2.0;
  690. } break;
  691. case EASING_EASE_OUT: {
  692. newval = 0.5;
  693. } break;
  694. case EASING_ZERO: {
  695. newval = 0;
  696. } break;
  697. case EASING_IN_OUT: {
  698. newval = -0.5;
  699. } break;
  700. case EASING_OUT_IN: {
  701. newval = -2.0;
  702. } break;
  703. }
  704. v = newval;
  705. emit_signal("variant_changed");
  706. easing_draw->update();
  707. } else if (type == Variant::OBJECT) {
  708. ERR_FAIL_INDEX(p_idx, inheritors_array.size());
  709. //List<String> inheritors;
  710. //ObjectTypeDB::get_inheriters_from(hint_text,&inheritors);
  711. //inheritors.push_front(hint_text);
  712. //ERR_FAIL_INDEX( p_idx, inheritors.size() );
  713. String intype = inheritors_array[p_idx];
  714. Object *obj = ObjectTypeDB::instance(intype);
  715. ERR_FAIL_COND(!obj);
  716. Resource *res = obj->cast_to<Resource>();
  717. ERR_FAIL_COND(!res);
  718. v = Ref<Resource>(res).get_ref_ptr();
  719. emit_signal("variant_changed");
  720. hide();
  721. }
  722. }
  723. void CustomPropertyEditor::_color_changed(const Color &p_color) {
  724. v = p_color;
  725. emit_signal("variant_changed");
  726. }
  727. void CustomPropertyEditor::_node_path_selected(NodePath p_path) {
  728. if (owner) {
  729. Node *node = NULL;
  730. if (owner->is_type("Node"))
  731. node = owner->cast_to<Node>();
  732. else if (owner->is_type("ArrayPropertyEdit"))
  733. node = owner->cast_to<ArrayPropertyEdit>()->get_node();
  734. if (!node) {
  735. v = p_path;
  736. emit_signal("variant_changed");
  737. call_deferred("hide"); //to not mess with dialogs
  738. return;
  739. }
  740. Node *tonode = node->get_node(p_path);
  741. if (tonode) {
  742. p_path = node->get_path_to(tonode);
  743. }
  744. }
  745. v = p_path;
  746. emit_signal("variant_changed");
  747. call_deferred("hide"); //to not mess with dialogs
  748. }
  749. void CustomPropertyEditor::_action_pressed(int p_which) {
  750. if (updating)
  751. return;
  752. switch (type) {
  753. case Variant::INT: {
  754. if (hint == PROPERTY_HINT_ALL_FLAGS) {
  755. uint32_t f = v;
  756. if (checks20[p_which]->is_pressed())
  757. f |= (1 << p_which);
  758. else
  759. f &= ~(1 << p_which);
  760. v = f;
  761. emit_signal("variant_changed");
  762. }
  763. } break;
  764. case Variant::STRING: {
  765. if (hint == PROPERTY_HINT_MULTILINE_TEXT) {
  766. hide();
  767. } else if (hint == PROPERTY_HINT_FILE || hint == PROPERTY_HINT_GLOBAL_FILE) {
  768. if (p_which == 0) {
  769. if (hint == PROPERTY_HINT_FILE)
  770. file->set_access(EditorFileDialog::ACCESS_RESOURCES);
  771. else
  772. file->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  773. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  774. file->clear_filters();
  775. file->clear_filters();
  776. if (hint_text != "") {
  777. Vector<String> extensions = hint_text.split(",");
  778. for (int i = 0; i < extensions.size(); i++) {
  779. String filter = extensions[i];
  780. if (filter.begins_with("."))
  781. filter = "*" + extensions[i];
  782. else if (!filter.begins_with("*"))
  783. filter = "*." + extensions[i];
  784. file->add_filter(filter + " ; " + extensions[i].to_upper());
  785. }
  786. }
  787. file->popup_centered_ratio();
  788. } else {
  789. v = "";
  790. emit_signal("variant_changed");
  791. hide();
  792. }
  793. } else if (hint == PROPERTY_HINT_DIR || hint == PROPERTY_HINT_GLOBAL_DIR) {
  794. if (p_which == 0) {
  795. if (hint == PROPERTY_HINT_DIR)
  796. file->set_access(EditorFileDialog::ACCESS_RESOURCES);
  797. else
  798. file->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  799. file->set_mode(EditorFileDialog::MODE_OPEN_DIR);
  800. file->clear_filters();
  801. file->popup_centered_ratio();
  802. } else {
  803. v = "";
  804. emit_signal("variant_changed");
  805. hide();
  806. }
  807. }
  808. } break;
  809. case Variant::NODE_PATH: {
  810. if (p_which == 0) {
  811. scene_tree->popup_centered_ratio();
  812. } else if (p_which == 1) {
  813. v = NodePath();
  814. emit_signal("variant_changed");
  815. hide();
  816. }
  817. } break;
  818. case Variant::OBJECT: {
  819. if (p_which == 0) {
  820. ERR_FAIL_COND(inheritors_array.empty());
  821. String intype = inheritors_array[0];
  822. if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
  823. Object *obj = ObjectTypeDB::instance(intype);
  824. ERR_BREAK(!obj);
  825. Resource *res = obj->cast_to<Resource>();
  826. ERR_BREAK(!res);
  827. v = Ref<Resource>(res).get_ref_ptr();
  828. emit_signal("variant_changed");
  829. hide();
  830. }
  831. } else if (p_which == 1) {
  832. file->set_access(EditorFileDialog::ACCESS_RESOURCES);
  833. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  834. List<String> extensions;
  835. String type = (hint == PROPERTY_HINT_RESOURCE_TYPE) ? hint_text : String();
  836. ResourceLoader::get_recognized_extensions_for_type(type, &extensions);
  837. file->clear_filters();
  838. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  839. file->add_filter("*." + E->get() + " ; " + E->get().to_upper());
  840. }
  841. file->popup_centered_ratio();
  842. } else if (p_which == 2) {
  843. RefPtr RefPtr = v;
  844. if (!RefPtr.is_null()) {
  845. emit_signal("resource_edit_request");
  846. hide();
  847. }
  848. } else if (p_which == 3) {
  849. v = Variant();
  850. emit_signal("variant_changed");
  851. hide();
  852. } else if (p_which == 4) {
  853. RefPtr RefPtr = v;
  854. Ref<Resource> res_orig = RefPtr;
  855. if (res_orig.is_null())
  856. return;
  857. List<PropertyInfo> property_list;
  858. res_orig->get_property_list(&property_list);
  859. List<Pair<String, Variant> > propvalues;
  860. for (List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
  861. Pair<String, Variant> p;
  862. PropertyInfo &pi = E->get();
  863. if (pi.usage & PROPERTY_USAGE_STORAGE) {
  864. p.first = pi.name;
  865. p.second = res_orig->get(pi.name);
  866. }
  867. propvalues.push_back(p);
  868. }
  869. Ref<Resource> res = Ref<Resource>(ObjectTypeDB::instance(res_orig->get_type()));
  870. ERR_FAIL_COND(res.is_null());
  871. for (List<Pair<String, Variant> >::Element *E = propvalues.front(); E; E = E->next()) {
  872. Pair<String, Variant> &p = E->get();
  873. res->set(p.first, p.second);
  874. }
  875. v = res.get_ref_ptr();
  876. emit_signal("variant_changed");
  877. hide();
  878. }
  879. } break;
  880. case Variant::IMAGE: {
  881. if (p_which == 0) {
  882. //new image too difficult
  883. ERR_PRINT("New Image Unimplemented");
  884. } else if (p_which == 1) {
  885. file->set_access(EditorFileDialog::ACCESS_RESOURCES);
  886. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  887. List<String> extensions;
  888. ImageLoader::get_recognized_extensions(&extensions);
  889. file->clear_filters();
  890. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  891. file->add_filter("*." + E->get() + " ; " + E->get().to_upper());
  892. }
  893. file->popup_centered_ratio();
  894. } else if (p_which == 2) {
  895. v = Image();
  896. emit_signal("variant_changed");
  897. hide();
  898. }
  899. } break;
  900. default: {};
  901. }
  902. }
  903. void CustomPropertyEditor::_scroll_modified(double p_value) {
  904. if (updating)
  905. return;
  906. /*
  907. switch(type) {
  908. case Variant::COLOR: {
  909. for (int i=0;i<4;i++) {
  910. value_editor[i]->set_text( String::num(scroll[i]->get_val(),2) );
  911. }
  912. Color c;
  913. c.r=scroll[0]->get_val();
  914. c.g=scroll[1]->get_val();
  915. c.b=scroll[2]->get_val();
  916. c.a=scroll[3]->get_val();
  917. v=c;
  918. update();
  919. emit_signal("variant_changed");
  920. } break;
  921. default: {}
  922. }
  923. */
  924. }
  925. void CustomPropertyEditor::_drag_easing(const InputEvent &p_ev) {
  926. if (p_ev.type == InputEvent::MOUSE_MOTION && p_ev.mouse_motion.button_mask & BUTTON_MASK_LEFT) {
  927. float rel = p_ev.mouse_motion.relative_x;
  928. if (rel == 0)
  929. return;
  930. bool flip = hint_text == "attenuation";
  931. if (flip)
  932. rel = -rel;
  933. float val = v;
  934. if (val == 0)
  935. return;
  936. bool sg = val < 0;
  937. val = Math::absf(val);
  938. val = Math::log(val) / Math::log(2);
  939. //logspace
  940. val += rel * 0.05;
  941. //
  942. val = Math::pow(2, val);
  943. if (sg)
  944. val = -val;
  945. v = val;
  946. easing_draw->update();
  947. //emit_signal("variant_changed");
  948. emit_signal("variant_changed");
  949. }
  950. if (p_ev.type == InputEvent::MOUSE_BUTTON && p_ev.mouse_button.button_index == BUTTON_LEFT) {
  951. }
  952. }
  953. void CustomPropertyEditor::_draw_easing() {
  954. RID ci = easing_draw->get_canvas_item();
  955. Size2 s = easing_draw->get_size();
  956. Rect2 r(Point2(), s);
  957. r = r.grow(3);
  958. get_stylebox("normal", "LineEdit")->draw(ci, r);
  959. //VisualServer::get_singleton()->canvas_item_add
  960. int points = 48;
  961. float prev = 1.0;
  962. float exp = v;
  963. bool flip = hint_text == "attenuation";
  964. Ref<Font> f = get_font("font", "Label");
  965. Color color = get_color("font_color", "Label");
  966. for (int i = 1; i <= points; i++) {
  967. float ifl = i / float(points);
  968. float iflp = (i - 1) / float(points);
  969. float h = 1.0 - Math::ease(ifl, exp);
  970. if (flip) {
  971. ifl = 1.0 - ifl;
  972. iflp = 1.0 - iflp;
  973. }
  974. VisualServer::get_singleton()->canvas_item_add_line(ci, Point2(iflp * s.width, prev * s.height), Point2(ifl * s.width, h * s.height), color);
  975. prev = h;
  976. }
  977. f->draw(ci, Point2(10, 10 + f->get_ascent()), String::num(exp, 2), color);
  978. }
  979. void CustomPropertyEditor::_text_edit_changed() {
  980. v = text_edit->get_text();
  981. emit_signal("variant_changed");
  982. }
  983. void CustomPropertyEditor::_modified(String p_string) {
  984. if (updating)
  985. return;
  986. updating = true;
  987. switch (type) {
  988. case Variant::REAL: {
  989. if (hint != PROPERTY_HINT_EXP_EASING) {
  990. if (evaluator)
  991. evaluator->eval(value_editor[0]->get_text());
  992. else
  993. v = value_editor[0]->get_text().to_double();
  994. emit_signal("variant_changed");
  995. }
  996. } break;
  997. case Variant::STRING: {
  998. v = value_editor[0]->get_text();
  999. emit_signal("variant_changed");
  1000. } break;
  1001. case Variant::VECTOR2: {
  1002. Vector2 vec;
  1003. if (evaluator) {
  1004. vec.x = evaluator->eval(value_editor[0]->get_text());
  1005. vec.y = evaluator->eval(value_editor[1]->get_text());
  1006. } else {
  1007. vec.x = value_editor[0]->get_text().to_double();
  1008. vec.y = value_editor[1]->get_text().to_double();
  1009. }
  1010. v = vec;
  1011. _emit_changed_whole_or_field();
  1012. } break;
  1013. case Variant::RECT2: {
  1014. Rect2 r2;
  1015. if (evaluator) {
  1016. r2.pos.x = evaluator->eval(value_editor[0]->get_text());
  1017. r2.pos.y = evaluator->eval(value_editor[1]->get_text());
  1018. r2.size.x = evaluator->eval(value_editor[2]->get_text());
  1019. r2.size.y = evaluator->eval(value_editor[3]->get_text());
  1020. } else {
  1021. r2.pos.x = value_editor[0]->get_text().to_double();
  1022. r2.pos.y = value_editor[1]->get_text().to_double();
  1023. r2.size.x = value_editor[2]->get_text().to_double();
  1024. r2.size.y = value_editor[3]->get_text().to_double();
  1025. }
  1026. v = r2;
  1027. _emit_changed_whole_or_field();
  1028. } break;
  1029. case Variant::VECTOR3: {
  1030. Vector3 vec;
  1031. if (evaluator) {
  1032. vec.x = evaluator->eval(value_editor[0]->get_text());
  1033. vec.y = evaluator->eval(value_editor[1]->get_text());
  1034. vec.z = evaluator->eval(value_editor[2]->get_text());
  1035. } else {
  1036. vec.x = value_editor[0]->get_text().to_double();
  1037. vec.y = value_editor[1]->get_text().to_double();
  1038. vec.z = value_editor[2]->get_text().to_double();
  1039. }
  1040. v = vec;
  1041. _emit_changed_whole_or_field();
  1042. } break;
  1043. case Variant::PLANE: {
  1044. Plane pl;
  1045. if (evaluator) {
  1046. pl.normal.x = evaluator->eval(value_editor[0]->get_text());
  1047. pl.normal.y = evaluator->eval(value_editor[1]->get_text());
  1048. pl.normal.z = evaluator->eval(value_editor[2]->get_text());
  1049. pl.d = evaluator->eval(value_editor[3]->get_text());
  1050. } else {
  1051. pl.normal.x = value_editor[0]->get_text().to_double();
  1052. pl.normal.y = value_editor[1]->get_text().to_double();
  1053. pl.normal.z = value_editor[2]->get_text().to_double();
  1054. pl.d = value_editor[3]->get_text().to_double();
  1055. }
  1056. v = pl;
  1057. _emit_changed_whole_or_field();
  1058. } break;
  1059. case Variant::QUAT: {
  1060. Quat q;
  1061. if (evaluator) {
  1062. q.x = evaluator->eval(value_editor[0]->get_text());
  1063. q.y = evaluator->eval(value_editor[1]->get_text());
  1064. q.z = evaluator->eval(value_editor[2]->get_text());
  1065. q.w = evaluator->eval(value_editor[3]->get_text());
  1066. } else {
  1067. q.x = value_editor[0]->get_text().to_double();
  1068. q.y = value_editor[1]->get_text().to_double();
  1069. q.z = value_editor[2]->get_text().to_double();
  1070. q.w = value_editor[3]->get_text().to_double();
  1071. }
  1072. v = q;
  1073. _emit_changed_whole_or_field();
  1074. } break;
  1075. case Variant::_AABB: {
  1076. Vector3 pos;
  1077. Vector3 size;
  1078. if (evaluator) {
  1079. pos.x = evaluator->eval(value_editor[0]->get_text());
  1080. pos.y = evaluator->eval(value_editor[1]->get_text());
  1081. pos.z = evaluator->eval(value_editor[2]->get_text());
  1082. size.x = evaluator->eval(value_editor[3]->get_text());
  1083. size.y = evaluator->eval(value_editor[4]->get_text());
  1084. size.z = evaluator->eval(value_editor[5]->get_text());
  1085. } else {
  1086. pos.x = value_editor[0]->get_text().to_double();
  1087. pos.y = value_editor[1]->get_text().to_double();
  1088. pos.z = value_editor[2]->get_text().to_double();
  1089. size.x = value_editor[3]->get_text().to_double();
  1090. size.y = value_editor[4]->get_text().to_double();
  1091. size.z = value_editor[5]->get_text().to_double();
  1092. }
  1093. v = AABB(pos, size);
  1094. _emit_changed_whole_or_field();
  1095. } break;
  1096. case Variant::MATRIX32: {
  1097. Matrix32 m;
  1098. for (int i = 0; i < 6; i++) {
  1099. if (evaluator) {
  1100. m.elements[i / 2][i % 2] = evaluator->eval(value_editor[i]->get_text());
  1101. } else {
  1102. m.elements[i / 2][i % 2] = value_editor[i]->get_text().to_double();
  1103. }
  1104. }
  1105. v = m;
  1106. _emit_changed_whole_or_field();
  1107. } break;
  1108. case Variant::MATRIX3: {
  1109. Matrix3 m;
  1110. for (int i = 0; i < 9; i++) {
  1111. if (evaluator) {
  1112. m.elements[i / 3][i % 3] = evaluator->eval(value_editor[i]->get_text());
  1113. } else {
  1114. m.elements[i / 3][i % 3] = value_editor[i]->get_text().to_double();
  1115. }
  1116. }
  1117. v = m;
  1118. _emit_changed_whole_or_field();
  1119. } break;
  1120. case Variant::TRANSFORM: {
  1121. Matrix3 basis;
  1122. for (int i = 0; i < 9; i++) {
  1123. if (evaluator) {
  1124. basis.elements[i / 3][i % 3] = evaluator->eval(value_editor[(i / 3) * 4 + i % 3]->get_text());
  1125. } else {
  1126. basis.elements[i / 3][i % 3] = value_editor[(i / 3) * 4 + i % 3]->get_text().to_double();
  1127. }
  1128. }
  1129. Vector3 origin;
  1130. if (evaluator) {
  1131. origin.x = evaluator->eval(value_editor[3]->get_text());
  1132. origin.y = evaluator->eval(value_editor[7]->get_text());
  1133. origin.z = evaluator->eval(value_editor[11]->get_text());
  1134. } else {
  1135. origin.x = value_editor[3]->get_text().to_double();
  1136. origin.y = value_editor[7]->get_text().to_double();
  1137. origin.z = value_editor[11]->get_text().to_double();
  1138. }
  1139. v = Transform(basis, origin);
  1140. _emit_changed_whole_or_field();
  1141. } break;
  1142. case Variant::COLOR: {
  1143. /*
  1144. for (int i=0;i<4;i++) {
  1145. scroll[i]->set_val( value_editor[i]->get_text().to_double() );
  1146. }
  1147. Color c;
  1148. c.r=value_editor[0]->get_text().to_double();
  1149. c.g=value_editor[1]->get_text().to_double();
  1150. c.b=value_editor[2]->get_text().to_double();
  1151. c.a=value_editor[3]->get_text().to_double();
  1152. v=c;
  1153. update();
  1154. emit_signal("variant_changed");
  1155. */
  1156. } break;
  1157. case Variant::IMAGE: {
  1158. } break;
  1159. case Variant::NODE_PATH: {
  1160. } break;
  1161. case Variant::INPUT_EVENT: {
  1162. } break;
  1163. case Variant::DICTIONARY: {
  1164. } break;
  1165. case Variant::RAW_ARRAY: {
  1166. } break;
  1167. case Variant::INT_ARRAY: {
  1168. } break;
  1169. case Variant::REAL_ARRAY: {
  1170. } break;
  1171. case Variant::STRING_ARRAY: {
  1172. } break;
  1173. case Variant::VECTOR3_ARRAY: {
  1174. } break;
  1175. case Variant::COLOR_ARRAY: {
  1176. } break;
  1177. default: {}
  1178. }
  1179. updating = false;
  1180. }
  1181. void CustomPropertyEditor::_emit_changed_whole_or_field() {
  1182. if (!Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1183. emit_signal("variant_changed");
  1184. } else {
  1185. emit_signal("variant_field_changed", field_names[focused_value_editor]);
  1186. }
  1187. }
  1188. void CustomPropertyEditor::_range_modified(double p_value) {
  1189. v = p_value;
  1190. emit_signal("variant_changed");
  1191. }
  1192. void CustomPropertyEditor::_focus_enter() {
  1193. switch (type) {
  1194. case Variant::REAL:
  1195. case Variant::STRING:
  1196. case Variant::VECTOR2:
  1197. case Variant::RECT2:
  1198. case Variant::VECTOR3:
  1199. case Variant::PLANE:
  1200. case Variant::QUAT:
  1201. case Variant::_AABB:
  1202. case Variant::MATRIX32:
  1203. case Variant::MATRIX3:
  1204. case Variant::TRANSFORM: {
  1205. for (int i = 0; i < MAX_VALUE_EDITORS; ++i) {
  1206. if (value_editor[i]->has_focus()) {
  1207. focused_value_editor = i;
  1208. value_editor[i]->select_all();
  1209. break;
  1210. }
  1211. }
  1212. } break;
  1213. default: {}
  1214. }
  1215. }
  1216. void CustomPropertyEditor::_focus_exit() {
  1217. switch (type) {
  1218. case Variant::REAL:
  1219. case Variant::STRING:
  1220. case Variant::VECTOR2:
  1221. case Variant::RECT2:
  1222. case Variant::VECTOR3:
  1223. case Variant::PLANE:
  1224. case Variant::QUAT:
  1225. case Variant::_AABB:
  1226. case Variant::MATRIX32:
  1227. case Variant::MATRIX3:
  1228. case Variant::TRANSFORM: {
  1229. for (int i = 0; i < MAX_VALUE_EDITORS; ++i) {
  1230. value_editor[i]->select(0, 0);
  1231. }
  1232. } break;
  1233. default: {}
  1234. }
  1235. }
  1236. void CustomPropertyEditor::config_action_buttons(const List<String> &p_strings) {
  1237. int w = 100;
  1238. int h = 18;
  1239. int m = 5;
  1240. set_size(Size2(w, m * 2 + (h + m) * p_strings.size()));
  1241. for (int i = 0; i < MAX_ACTION_BUTTONS; i++) {
  1242. if (i < p_strings.size()) {
  1243. action_buttons[i]->show();
  1244. action_buttons[i]->set_text(p_strings[i]);
  1245. action_buttons[i]->set_pos(Point2(m, m + i * (h + m)));
  1246. action_buttons[i]->set_size(Size2(w - m * 2, h));
  1247. action_buttons[i]->set_flat(true);
  1248. } else {
  1249. action_buttons[i]->hide();
  1250. }
  1251. }
  1252. }
  1253. void CustomPropertyEditor::config_value_editors(int p_amount, int p_columns, int p_label_w, const List<String> &p_strings) {
  1254. int w = 80;
  1255. int h = 20;
  1256. int m = 10;
  1257. int rows = ((p_amount - 1) / p_columns) + 1;
  1258. set_size(Size2(m * (1 + p_columns) + (w + p_label_w) * p_columns, m * (1 + rows) + h * rows));
  1259. for (int i = 0; i < MAX_VALUE_EDITORS; i++) {
  1260. int c = i % p_columns;
  1261. int r = i / p_columns;
  1262. if (i < p_amount) {
  1263. value_editor[i]->show();
  1264. value_label[i]->show();
  1265. value_label[i]->set_text(i < p_strings.size() ? p_strings[i] : String(""));
  1266. value_editor[i]->set_pos(Point2(m + p_label_w + c * (w + m + p_label_w), m + r * (h + m)));
  1267. value_editor[i]->set_size(Size2(w, h));
  1268. value_label[i]->set_pos(Point2(m + c * (w + m + p_label_w), m + r * (h + m)));
  1269. value_editor[i]->set_editable(!read_only);
  1270. } else {
  1271. value_editor[i]->hide();
  1272. value_label[i]->hide();
  1273. }
  1274. }
  1275. }
  1276. void CustomPropertyEditor::_bind_methods() {
  1277. ObjectTypeDB::bind_method("_focus_enter", &CustomPropertyEditor::_focus_enter);
  1278. ObjectTypeDB::bind_method("_focus_exit", &CustomPropertyEditor::_focus_exit);
  1279. ObjectTypeDB::bind_method("_modified", &CustomPropertyEditor::_modified);
  1280. ObjectTypeDB::bind_method("_range_modified", &CustomPropertyEditor::_range_modified);
  1281. ObjectTypeDB::bind_method("_scroll_modified", &CustomPropertyEditor::_scroll_modified);
  1282. ObjectTypeDB::bind_method("_action_pressed", &CustomPropertyEditor::_action_pressed);
  1283. ObjectTypeDB::bind_method("_file_selected", &CustomPropertyEditor::_file_selected);
  1284. ObjectTypeDB::bind_method("_type_create_selected", &CustomPropertyEditor::_type_create_selected);
  1285. ObjectTypeDB::bind_method("_node_path_selected", &CustomPropertyEditor::_node_path_selected);
  1286. ObjectTypeDB::bind_method("_color_changed", &CustomPropertyEditor::_color_changed);
  1287. ObjectTypeDB::bind_method("_draw_easing", &CustomPropertyEditor::_draw_easing);
  1288. ObjectTypeDB::bind_method("_drag_easing", &CustomPropertyEditor::_drag_easing);
  1289. ObjectTypeDB::bind_method("_text_edit_changed", &CustomPropertyEditor::_text_edit_changed);
  1290. ObjectTypeDB::bind_method("_menu_option", &CustomPropertyEditor::_menu_option);
  1291. ADD_SIGNAL(MethodInfo("variant_changed"));
  1292. ADD_SIGNAL(MethodInfo("variant_field_changed", PropertyInfo(Variant::STRING, "field")));
  1293. ADD_SIGNAL(MethodInfo("resource_edit_request"));
  1294. }
  1295. CustomPropertyEditor::CustomPropertyEditor() {
  1296. read_only = false;
  1297. updating = false;
  1298. for (int i = 0; i < MAX_VALUE_EDITORS; i++) {
  1299. value_editor[i] = memnew(LineEdit);
  1300. add_child(value_editor[i]);
  1301. value_label[i] = memnew(Label);
  1302. add_child(value_label[i]);
  1303. value_editor[i]->hide();
  1304. value_label[i]->hide();
  1305. value_editor[i]->connect("text_entered", this, "_modified");
  1306. value_editor[i]->connect("focus_enter", this, "_focus_enter");
  1307. value_editor[i]->connect("focus_exit", this, "_focus_exit");
  1308. }
  1309. focused_value_editor = -1;
  1310. for (int i = 0; i < 4; i++) {
  1311. scroll[i] = memnew(HScrollBar);
  1312. scroll[i]->hide();
  1313. scroll[i]->set_min(0);
  1314. scroll[i]->set_max(1.0);
  1315. scroll[i]->set_step(0.01);
  1316. add_child(scroll[i]);
  1317. scroll[i]->connect("value_changed", this, "_scroll_modified");
  1318. }
  1319. for (int i = 0; i < 20; i++) {
  1320. checks20[i] = memnew(CheckBox);
  1321. checks20[i]->set_toggle_mode(true);
  1322. checks20[i]->set_focus_mode(FOCUS_NONE);
  1323. add_child(checks20[i]);
  1324. checks20[i]->hide();
  1325. checks20[i]->connect("pressed", this, "_action_pressed", make_binds(i));
  1326. checks20[i]->set_tooltip(vformat(TTR("Bit %d, val %d."), i, 1 << i));
  1327. }
  1328. text_edit = memnew(TextEdit);
  1329. add_child(text_edit);
  1330. text_edit->set_area_as_parent_rect();
  1331. for (int i = 0; i < 4; i++)
  1332. text_edit->set_margin((Margin)i, 5);
  1333. text_edit->set_margin(MARGIN_BOTTOM, 30);
  1334. text_edit->hide();
  1335. text_edit->connect("text_changed", this, "_text_edit_changed");
  1336. for (int i = 0; i < MAX_ACTION_BUTTONS; i++) {
  1337. action_buttons[i] = memnew(Button);
  1338. action_buttons[i]->hide();
  1339. add_child(action_buttons[i]);
  1340. Vector<Variant> binds;
  1341. binds.push_back(i);
  1342. action_buttons[i]->connect("pressed", this, "_action_pressed", binds);
  1343. }
  1344. color_picker = NULL;
  1345. set_as_toplevel(true);
  1346. file = memnew(EditorFileDialog);
  1347. add_child(file);
  1348. file->hide();
  1349. file->connect("file_selected", this, "_file_selected");
  1350. file->connect("dir_selected", this, "_file_selected");
  1351. error = memnew(ConfirmationDialog);
  1352. error->set_title(TTR("Error!"));
  1353. add_child(error);
  1354. //error->get_cancel()->hide();
  1355. type_button = memnew(MenuButton);
  1356. add_child(type_button);
  1357. type_button->hide();
  1358. type_button->get_popup()->connect("item_pressed", this, "_type_create_selected");
  1359. scene_tree = memnew(SceneTreeDialog);
  1360. add_child(scene_tree);
  1361. scene_tree->connect("selected", this, "_node_path_selected");
  1362. scene_tree->get_scene_tree()->set_show_enabled_subscene(true);
  1363. texture_preview = memnew(TextureFrame);
  1364. add_child(texture_preview);
  1365. texture_preview->hide();
  1366. easing_draw = memnew(Control);
  1367. add_child(easing_draw);
  1368. easing_draw->hide();
  1369. easing_draw->connect("draw", this, "_draw_easing");
  1370. easing_draw->connect("input_event", this, "_drag_easing");
  1371. //easing_draw->emit_signal(SceneStringNames::get_singleton()->input_event,InputEvent());
  1372. easing_draw->set_default_cursor_shape(Control::CURSOR_MOVE);
  1373. menu = memnew(PopupMenu);
  1374. add_child(menu);
  1375. menu->connect("item_pressed", this, "_menu_option");
  1376. evaluator = NULL;
  1377. spinbox = memnew(SpinBox);
  1378. add_child(spinbox);
  1379. spinbox->set_area_as_parent_rect(5);
  1380. spinbox->connect("value_changed", this, "_range_modified");
  1381. slider = memnew(HSlider);
  1382. add_child(slider);
  1383. slider->set_area_as_parent_rect(5);
  1384. slider->connect("value_changed", this, "_range_modified");
  1385. }
  1386. bool PropertyEditor::_might_be_in_instance() {
  1387. if (!obj)
  1388. return false;
  1389. Node *node = obj->cast_to<Node>();
  1390. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  1391. bool might_be = false;
  1392. while (node) {
  1393. if (node->get_scene_instance_state().is_valid()) {
  1394. might_be = true;
  1395. break;
  1396. }
  1397. if (node == edited_scene) {
  1398. if (node->get_scene_inherited_state().is_valid()) {
  1399. might_be = true;
  1400. break;
  1401. }
  1402. might_be = false;
  1403. break;
  1404. }
  1405. node = node->get_owner();
  1406. }
  1407. return might_be;
  1408. }
  1409. bool PropertyEditor::_get_instanced_node_original_property(const StringName &p_prop, Variant &value) {
  1410. Node *node = obj->cast_to<Node>();
  1411. if (!node)
  1412. return false;
  1413. Node *orig = node;
  1414. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  1415. bool found = false;
  1416. // print_line("for prop - "+String(p_prop));
  1417. while (node) {
  1418. Ref<SceneState> ss;
  1419. if (node == edited_scene) {
  1420. ss = node->get_scene_inherited_state();
  1421. } else {
  1422. ss = node->get_scene_instance_state();
  1423. }
  1424. // print_line("at - "+String(edited_scene->get_path_to(node)));
  1425. if (ss.is_valid()) {
  1426. NodePath np = node->get_path_to(orig);
  1427. int node_idx = ss->find_node_by_path(np);
  1428. // print_line("\t valid, nodeidx "+itos(node_idx));
  1429. if (node_idx >= 0) {
  1430. bool lfound = false;
  1431. Variant lvar;
  1432. lvar = ss->get_property_value(node_idx, p_prop, lfound);
  1433. if (lfound) {
  1434. found = true;
  1435. value = lvar;
  1436. // print_line("\t found value "+String(value));
  1437. }
  1438. }
  1439. }
  1440. if (node == edited_scene) {
  1441. //just in case
  1442. break;
  1443. }
  1444. node = node->get_owner();
  1445. }
  1446. return found;
  1447. }
  1448. bool PropertyEditor::_is_property_different(const Variant &p_current, const Variant &p_orig, int p_usage) {
  1449. {
  1450. Node *node = obj->cast_to<Node>();
  1451. if (!node)
  1452. return false;
  1453. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  1454. bool found_state = false;
  1455. // print_line("for prop - "+String(p_prop));
  1456. while (node) {
  1457. Ref<SceneState> ss;
  1458. if (node == edited_scene) {
  1459. ss = node->get_scene_inherited_state();
  1460. } else {
  1461. ss = node->get_scene_instance_state();
  1462. }
  1463. if (ss.is_valid()) {
  1464. found_state = true;
  1465. }
  1466. if (node == edited_scene) {
  1467. //just in case
  1468. break;
  1469. }
  1470. node = node->get_owner();
  1471. }
  1472. if (!found_state)
  1473. return false; //pointless to check if we are not comparing against anything.
  1474. }
  1475. if (p_orig.get_type() == Variant::NIL) {
  1476. //special cases
  1477. if (p_current.is_zero() && p_usage & PROPERTY_USAGE_STORE_IF_NONZERO)
  1478. return false;
  1479. if (p_current.is_one() && p_usage & PROPERTY_USAGE_STORE_IF_NONONE)
  1480. return false;
  1481. }
  1482. if (p_current.get_type() == Variant::REAL && p_orig.get_type() == Variant::REAL) {
  1483. float a = p_current;
  1484. float b = p_orig;
  1485. return Math::abs(a - b) > CMP_EPSILON; //this must be done because, as some scenes save as text, there might be a tiny difference in floats due to numerical error
  1486. }
  1487. return bool(Variant::evaluate(Variant::OP_NOT_EQUAL, p_current, p_orig));
  1488. }
  1489. TreeItem *PropertyEditor::find_item(TreeItem *p_item, const String &p_name) {
  1490. if (!p_item)
  1491. return NULL;
  1492. String name = p_item->get_metadata(1);
  1493. if (name == p_name) {
  1494. return p_item;
  1495. }
  1496. TreeItem *c = p_item->get_children();
  1497. while (c) {
  1498. TreeItem *found = find_item(c, p_name);
  1499. if (found)
  1500. return found;
  1501. c = c->get_next();
  1502. }
  1503. return NULL;
  1504. }
  1505. void PropertyEditor::_changed_callback(Object *p_changed, const char *p_prop) {
  1506. _changed_callbacks(p_changed, p_prop);
  1507. }
  1508. void PropertyEditor::_changed_callbacks(Object *p_changed, const String &p_prop) {
  1509. if (p_changed != obj)
  1510. return;
  1511. if (changing)
  1512. return;
  1513. if (p_prop == String())
  1514. update_tree_pending = true;
  1515. else {
  1516. pending[p_prop] = p_prop;
  1517. }
  1518. }
  1519. void PropertyEditor::update_property(const String &p_prop) {
  1520. if (obj)
  1521. _changed_callbacks(obj, p_prop);
  1522. }
  1523. void PropertyEditor::set_item_text(TreeItem *p_item, int p_type, const String &p_name, int p_hint, const String &p_hint_text) {
  1524. switch (p_type) {
  1525. case Variant::BOOL: {
  1526. p_item->set_checked(1, obj->get(p_name));
  1527. } break;
  1528. case Variant::REAL:
  1529. case Variant::INT: {
  1530. if (p_hint == PROPERTY_HINT_ALL_FLAGS) {
  1531. tree->update();
  1532. break;
  1533. }
  1534. if (p_hint == PROPERTY_HINT_FLAGS) {
  1535. Vector<String> values = p_hint_text.split(",");
  1536. String flags;
  1537. int val = obj->get(p_name);
  1538. for (int i = 0; i < values.size(); i++) {
  1539. String v = values[i];
  1540. if (v == "")
  1541. continue;
  1542. if (!(val & (1 << i)))
  1543. continue;
  1544. if (flags != "")
  1545. flags += ", ";
  1546. flags += v;
  1547. }
  1548. p_item->set_text(1, flags);
  1549. break;
  1550. }
  1551. if (p_hint == PROPERTY_HINT_EXP_EASING) {
  1552. p_item->set_text(1, String::num(obj->get(p_name), 2));
  1553. break;
  1554. }
  1555. //p_item->set_cell_mode( 1, TreeItem::CELL_MODE_RANGE );
  1556. if (p_type == Variant::REAL) {
  1557. p_item->set_range(1, obj->get(p_name));
  1558. } else {
  1559. p_item->set_range(1, obj->get(p_name));
  1560. }
  1561. p_item->set_editable(1, !read_only);
  1562. } break;
  1563. case Variant::STRING:
  1564. if (p_hint == PROPERTY_HINT_ENUM) {
  1565. Vector<String> strings = p_hint_text.split(",");
  1566. String current = obj->get(p_name);
  1567. int idx = 0;
  1568. for (int x = 0; x < strings.size(); x++) {
  1569. if (strings[x] == current) {
  1570. idx = x;
  1571. break;
  1572. }
  1573. }
  1574. p_item->set_text(1, p_hint_text);
  1575. p_item->set_range(1, idx);
  1576. break;
  1577. }
  1578. case Variant::VECTOR3:
  1579. case Variant::QUAT:
  1580. case Variant::VECTOR2:
  1581. case Variant::_AABB:
  1582. case Variant::RECT2:
  1583. case Variant::MATRIX32:
  1584. case Variant::MATRIX3:
  1585. case Variant::TRANSFORM: {
  1586. p_item->set_text(1, obj->get(p_name));
  1587. } break;
  1588. case Variant::COLOR: {
  1589. tree->update();
  1590. //p_item->set_text(1,obj->get(p_name));
  1591. } break;
  1592. case Variant::IMAGE: {
  1593. Image img = obj->get(p_name);
  1594. if (img.empty())
  1595. p_item->set_text(1, "[Image (empty)]");
  1596. else
  1597. p_item->set_text(1, "[Image " + itos(img.get_width()) + "x" + itos(img.get_height()) + "-" + String(Image::get_format_name(img.get_format())) + "]");
  1598. } break;
  1599. case Variant::NODE_PATH: {
  1600. p_item->set_text(1, obj->get(p_name));
  1601. } break;
  1602. case Variant::OBJECT: {
  1603. if (obj->get(p_name).get_type() == Variant::NIL || obj->get(p_name).operator RefPtr().is_null()) {
  1604. p_item->set_text(1, "<null>");
  1605. p_item->set_icon(1, Ref<Texture>());
  1606. Dictionary d = p_item->get_metadata(0);
  1607. int hint = d.has("hint") ? d["hint"].operator int() : -1;
  1608. String hint_text = d.has("hint_text") ? d["hint_text"] : "";
  1609. if (hint == PROPERTY_HINT_RESOURCE_TYPE && hint_text == "Texture") {
  1610. p_item->set_icon(1, NULL);
  1611. }
  1612. } else {
  1613. RES res = obj->get(p_name).operator RefPtr();
  1614. if (res->is_type("Texture")) {
  1615. int tw = EditorSettings::get_singleton()->get("property_editor/texture_preview_width");
  1616. Vector2 size(res->call("get_width"), res->call("get_height"));
  1617. if (size.width < size.height) {
  1618. tw = MAX((size.width / size.height) * tw, 1);
  1619. }
  1620. p_item->set_icon_max_width(1, tw);
  1621. p_item->set_icon(1, res);
  1622. p_item->set_text(1, "");
  1623. } else if (res->get_name() != "") {
  1624. p_item->set_text(1, res->get_name());
  1625. } else if (res->get_path() != "" && !res->get_path().begins_with("local://")) {
  1626. p_item->set_text(1, res->get_path().get_file());
  1627. } else {
  1628. p_item->set_text(1, "<" + res->get_type() + ">");
  1629. };
  1630. if (res.is_valid() && res->get_path().is_resource_file()) {
  1631. p_item->set_tooltip(1, res->get_path());
  1632. } else if (res.is_valid()) {
  1633. p_item->set_tooltip(1, res->get_name() + " (" + res->get_type() + ")");
  1634. }
  1635. if (has_icon(res->get_type(), "EditorIcons")) {
  1636. p_item->set_icon(0, get_icon(res->get_type(), "EditorIcons"));
  1637. } else {
  1638. Dictionary d = p_item->get_metadata(0);
  1639. int hint = d.has("hint") ? d["hint"].operator int() : -1;
  1640. String hint_text = d.has("hint_text") ? d["hint_text"] : "";
  1641. if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
  1642. if (has_icon(hint_text, "EditorIcons")) {
  1643. p_item->set_icon(0, get_icon(hint_text, "EditorIcons"));
  1644. } else {
  1645. p_item->set_icon(0, get_icon("Object", "EditorIcons"));
  1646. }
  1647. }
  1648. }
  1649. if (res->is_type("Script")) {
  1650. p_item->set_text(1, res->get_path().get_file());
  1651. } else if (!res->is_type("Texture")) {
  1652. //texture already previews via itself
  1653. EditorResourcePreview::get_singleton()->queue_edited_resource_preview(res, this, "_resource_preview_done", p_item->get_instance_ID());
  1654. }
  1655. }
  1656. } break;
  1657. default: {};
  1658. }
  1659. }
  1660. void PropertyEditor::_check_reload_status(const String &p_name, TreeItem *item) {
  1661. bool has_reload = false;
  1662. int found = -1;
  1663. bool is_disabled = false;
  1664. for (int i = 0; i < item->get_button_count(1); i++) {
  1665. if (item->get_button_id(1, i) == 3) {
  1666. found = i;
  1667. is_disabled = item->is_button_disabled(1, i);
  1668. break;
  1669. }
  1670. }
  1671. if (_might_be_in_instance()) {
  1672. Variant vorig;
  1673. Dictionary d = item->get_metadata(0);
  1674. int usage = d.has("usage") ? int(int(d["usage"]) & (PROPERTY_USAGE_STORE_IF_NONONE | PROPERTY_USAGE_STORE_IF_NONZERO)) : 0;
  1675. if (_get_instanced_node_original_property(p_name, vorig) || usage) {
  1676. Variant v = obj->get(p_name);
  1677. bool changed = _is_property_different(v, vorig, usage);
  1678. //if ((found!=-1 && !is_disabled)!=changed) {
  1679. if (changed) {
  1680. has_reload = true;
  1681. } else {
  1682. }
  1683. //}
  1684. }
  1685. }
  1686. if (!has_reload && !obj->get_script().is_null()) {
  1687. Ref<Script> scr = obj->get_script();
  1688. Variant orig_value;
  1689. if (scr->get_property_default_value(p_name, orig_value)) {
  1690. if (orig_value != obj->get(p_name)) {
  1691. has_reload = true;
  1692. }
  1693. }
  1694. }
  1695. //print_line("found: "+itos(found)+" has reload: "+itos(has_reload)+" is_disabled "+itos(is_disabled));
  1696. if (found != -1 && !has_reload) {
  1697. if (!is_disabled) {
  1698. item->erase_button(1, found);
  1699. if (item->get_cell_mode(1) == TreeItem::CELL_MODE_RANGE && item->get_text(1) == String()) {
  1700. item->add_button(1, get_icon("ReloadEmpty", "EditorIcons"), 3, true);
  1701. }
  1702. }
  1703. } else if (found == -1 && has_reload) {
  1704. item->add_button(1, get_icon("ReloadSmall", "EditorIcons"), 3);
  1705. } else if (found != -1 && has_reload && is_disabled) {
  1706. item->erase_button(1, found);
  1707. item->add_button(1, get_icon("ReloadSmall", "EditorIcons"), 3);
  1708. }
  1709. }
  1710. bool PropertyEditor::_is_drop_valid(const Dictionary &p_drag_data, const Dictionary &p_item_data) const {
  1711. Dictionary d = p_item_data;
  1712. if (d.has("type")) {
  1713. int type = d["type"];
  1714. if (type == Variant::OBJECT && d.has("hint") && d.has("hint_text") && int(d["hint"]) == PROPERTY_HINT_RESOURCE_TYPE) {
  1715. String allowed_type = d["hint_text"];
  1716. Dictionary drag_data = p_drag_data;
  1717. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  1718. Ref<Resource> res = drag_data["resource"];
  1719. for (int i = 0; i < allowed_type.get_slice_count(","); i++) {
  1720. String at = allowed_type.get_slice(",", i).strip_edges();
  1721. if (res.is_valid() && ObjectTypeDB::is_type(res->get_type(), at)) {
  1722. return true;
  1723. }
  1724. }
  1725. }
  1726. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  1727. Vector<String> files = drag_data["files"];
  1728. if (files.size() == 1) {
  1729. String file = files[0];
  1730. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  1731. if (ftype != "") {
  1732. for (int i = 0; i < allowed_type.get_slice_count(","); i++) {
  1733. String at = allowed_type.get_slice(",", i).strip_edges();
  1734. if (ObjectTypeDB::is_type(ftype, at)) {
  1735. return true;
  1736. }
  1737. }
  1738. }
  1739. }
  1740. }
  1741. }
  1742. }
  1743. return false;
  1744. }
  1745. void PropertyEditor::_mark_drop_fields(TreeItem *p_at) {
  1746. if (_is_drop_valid(get_viewport()->gui_get_drag_data(), p_at->get_metadata(0)))
  1747. p_at->set_custom_bg_color(1, Color(0.7, 0.5, 0.2), true);
  1748. if (p_at->get_children()) {
  1749. _mark_drop_fields(p_at->get_children());
  1750. }
  1751. if (p_at->get_next()) {
  1752. _mark_drop_fields(p_at->get_next());
  1753. }
  1754. }
  1755. Variant PropertyEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1756. TreeItem *item = tree->get_item_at_pos(p_point);
  1757. if (!item)
  1758. return Variant();
  1759. int col = tree->get_column_at_pos(p_point);
  1760. if (col != 1)
  1761. return Variant();
  1762. Dictionary d = item->get_metadata(0);
  1763. if (!d.has("name"))
  1764. return Variant();
  1765. Variant val = obj->get(d["name"]);
  1766. if (val.get_type() == Variant::OBJECT) {
  1767. RES res = val;
  1768. if (res.is_valid()) {
  1769. custom_editor->hide_menu();
  1770. return EditorNode::get_singleton()->drag_resource(res, p_from);
  1771. }
  1772. }
  1773. return Variant();
  1774. }
  1775. bool PropertyEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1776. TreeItem *item = tree->get_item_at_pos(p_point);
  1777. if (!item)
  1778. return false;
  1779. int col = tree->get_column_at_pos(p_point);
  1780. if (col != 1)
  1781. return false;
  1782. return _is_drop_valid(p_data, item->get_metadata(0));
  1783. }
  1784. void PropertyEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1785. TreeItem *item = tree->get_item_at_pos(p_point);
  1786. if (!item)
  1787. return;
  1788. int col = tree->get_column_at_pos(p_point);
  1789. if (col != 1)
  1790. return;
  1791. if (!_is_drop_valid(p_data, item->get_metadata(0)))
  1792. return;
  1793. Dictionary d = item->get_metadata(0);
  1794. if (!d.has("name"))
  1795. return;
  1796. String name = d["name"];
  1797. Dictionary drag_data = p_data;
  1798. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  1799. Ref<Resource> res = drag_data["resource"];
  1800. if (res.is_valid()) {
  1801. _edit_set(name, res);
  1802. return;
  1803. }
  1804. }
  1805. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  1806. Vector<String> files = drag_data["files"];
  1807. if (files.size() == 1) {
  1808. String file = files[0];
  1809. RES res = ResourceLoader::load(file);
  1810. if (res.is_valid()) {
  1811. _edit_set(name, res);
  1812. return;
  1813. }
  1814. }
  1815. }
  1816. }
  1817. void PropertyEditor::_clear_drop_fields(TreeItem *p_at) {
  1818. Dictionary d = p_at->get_metadata(0);
  1819. if (d.has("type")) {
  1820. int type = d["type"];
  1821. if (type == Variant::OBJECT) {
  1822. p_at->clear_custom_bg_color(1);
  1823. }
  1824. }
  1825. if (p_at->get_children()) {
  1826. _clear_drop_fields(p_at->get_children());
  1827. }
  1828. if (p_at->get_next()) {
  1829. _clear_drop_fields(p_at->get_next());
  1830. }
  1831. }
  1832. void PropertyEditor::_notification(int p_what) {
  1833. if (p_what == NOTIFICATION_ENTER_TREE) {
  1834. get_tree()->connect("node_removed", this, "_node_removed");
  1835. }
  1836. if (p_what == NOTIFICATION_EXIT_TREE) {
  1837. get_tree()->disconnect("node_removed", this, "_node_removed");
  1838. edit(NULL);
  1839. }
  1840. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  1841. if (is_visible() && tree->get_root()) {
  1842. _mark_drop_fields(tree->get_root());
  1843. }
  1844. }
  1845. if (p_what == NOTIFICATION_DRAG_END) {
  1846. if (is_visible() && tree->get_root()) {
  1847. _clear_drop_fields(tree->get_root());
  1848. }
  1849. }
  1850. if (p_what == NOTIFICATION_FIXED_PROCESS) {
  1851. if (refresh_countdown > 0) {
  1852. refresh_countdown -= get_fixed_process_delta_time();
  1853. if (refresh_countdown <= 0) {
  1854. TreeItem *root = tree->get_root();
  1855. _refresh_item(root);
  1856. }
  1857. }
  1858. changing = true;
  1859. if (update_tree_pending) {
  1860. update_tree();
  1861. update_tree_pending = false;
  1862. } else {
  1863. const String *k = NULL;
  1864. while ((k = pending.next(k))) {
  1865. TreeItem *item = find_item(tree->get_root(), *k);
  1866. if (!item)
  1867. continue;
  1868. _check_reload_status(*k, item);
  1869. Dictionary d = item->get_metadata(0);
  1870. set_item_text(item, d["type"], d["name"], d["hint"], d["hint_text"]);
  1871. }
  1872. }
  1873. pending.clear();
  1874. changing = false;
  1875. }
  1876. }
  1877. TreeItem *PropertyEditor::get_parent_node(String p_path, HashMap<String, TreeItem *> &item_paths, TreeItem *root) {
  1878. TreeItem *item = NULL;
  1879. if (p_path == "") {
  1880. item = root;
  1881. } else if (item_paths.has(p_path)) {
  1882. item = item_paths.get(p_path);
  1883. } else {
  1884. // printf("path %s parent path %s - item name %s\n",p_path.ascii().get_data(),p_path.left( p_path.find_last("/") ).ascii().get_data(),p_path.right( p_path.find_last("/") ).ascii().get_data() );
  1885. TreeItem *parent = get_parent_node(p_path.left(p_path.find_last("/")), item_paths, root);
  1886. item = tree->create_item(parent);
  1887. String name = (p_path.find("/") != -1) ? p_path.right(p_path.find_last("/") + 1) : p_path;
  1888. item->set_text(0, capitalize_paths ? name.capitalize() : name);
  1889. item->set_tooltip(0, p_path);
  1890. if (item->get_parent() != root) {
  1891. item->set_icon(0, get_icon("Folder", "EditorIcons"));
  1892. }
  1893. item->set_editable(0, false);
  1894. item->set_selectable(0, subsection_selectable);
  1895. item->set_editable(1, false);
  1896. item->set_selectable(1, subsection_selectable);
  1897. if (item->get_parent() == root) {
  1898. item->set_custom_bg_color(0, get_color("prop_subsection", "Editor"));
  1899. item->set_custom_bg_color(1, get_color("prop_subsection", "Editor"));
  1900. }
  1901. item_paths[p_path] = item;
  1902. }
  1903. return item;
  1904. }
  1905. void PropertyEditor::_refresh_item(TreeItem *p_item) {
  1906. if (!p_item)
  1907. return;
  1908. String name = p_item->get_metadata(1);
  1909. if (name != String()) {
  1910. _check_reload_status(name, p_item);
  1911. #if 0
  1912. bool has_reload=false;
  1913. int found=-1;
  1914. for(int i=0;i<p_item->get_button_count(1);i++) {
  1915. if (p_item->get_button_id(1,i)==3) {
  1916. found=i;
  1917. break;
  1918. }
  1919. }
  1920. if (_might_be_in_instance()) {
  1921. Variant vorig;
  1922. Dictionary d=p_item->get_metadata(0);
  1923. int usage = d.has("usage")?int(int(d["usage"])&(PROPERTY_USAGE_STORE_IF_NONONE|PROPERTY_USAGE_STORE_IF_NONZERO)):0;
  1924. if (_get_instanced_node_original_property(name,vorig) || usage) {
  1925. Variant v = obj->get(name);
  1926. bool changed = _is_property_different(v,vorig,usage);
  1927. if ((found!=-1)!=changed) {
  1928. if (changed) {
  1929. has_reload=true;
  1930. } else {
  1931. //p_item->erase_button(1,found);
  1932. }
  1933. }
  1934. }
  1935. }
  1936. if (!has_reload && !obj->get_script().is_null()) {
  1937. Ref<Script> scr = obj->get_script();
  1938. Variant orig_value;
  1939. if (scr->get_property_default_value(name,orig_value)) {
  1940. if (orig_value!=obj->get(name)) {
  1941. has_reload=true;
  1942. }
  1943. }
  1944. }
  1945. if (!has_reload && found!=-1) {
  1946. p_item->erase_button(1,found);
  1947. } else if (has_reload && found==-1) {
  1948. p_item->add_button(1,get_icon("ReloadSmall","EditorIcons"),3);
  1949. }
  1950. #endif
  1951. Dictionary d = p_item->get_metadata(0);
  1952. set_item_text(p_item, d["type"], d["name"], d["hint"], d["hint_text"]);
  1953. }
  1954. TreeItem *c = p_item->get_children();
  1955. while (c) {
  1956. _refresh_item(c);
  1957. c = c->get_next();
  1958. }
  1959. }
  1960. void PropertyEditor::refresh() {
  1961. if (refresh_countdown > 0)
  1962. return;
  1963. refresh_countdown = EditorSettings::get_singleton()->get("property_editor/auto_refresh_interval");
  1964. }
  1965. void PropertyEditor::update_tree() {
  1966. tree->clear();
  1967. if (!obj)
  1968. return;
  1969. HashMap<String, TreeItem *> item_path;
  1970. TreeItem *root = tree->create_item(NULL);
  1971. tree->set_hide_root(true);
  1972. /*
  1973. TreeItem *title = tree->create_item(root);
  1974. title->set_custom_bg_color(0,get_color("prop_section","Editor"));
  1975. title->set_text(0,"Property"); // todo, fetch name if ID exists in database
  1976. title->set_editable(0,false);
  1977. title->set_selectable(0,false);
  1978. title->set_custom_bg_color(1,get_color("prop_section","Editor"));
  1979. title->set_text(1,"Value"); // todo, fetch name if ID exists in database
  1980. title->set_editable(1,false);
  1981. title->set_selectable(1,false);
  1982. */
  1983. /*
  1984. if (obj->cast_to<Node>() || obj->cast_to<Resource>()) {
  1985. TreeItem *type = tree->create_item(root);
  1986. type->set_text(0,"Type"); // todo, fetch name if ID exists in database
  1987. type->set_text(1,obj->get_type()); // todo, fetch name if ID exists in database
  1988. if (has_icon(obj->get_type(),"EditorIcons"))
  1989. type->set_icon(1,get_icon(obj->get_type(),"EditorIcons") );
  1990. else
  1991. type->set_icon(1,get_icon("Object","EditorIcons") );
  1992. type->set_selectable(0,false);
  1993. type->set_selectable(1,false);
  1994. TreeItem *name = tree->create_item(root);
  1995. name->set_text(0,"Name"); // todo, fetch name if ID exists in database
  1996. if (obj->is_type("Resource"))
  1997. name->set_text(1,obj->cast_to<Resource>()->get_name());
  1998. else if (obj->is_type("Node"))
  1999. name->set_text(1,obj->cast_to<Node>()->get_name());
  2000. name->set_selectable(0,false);
  2001. name->set_selectable(1,false);
  2002. }
  2003. */
  2004. List<PropertyInfo> plist;
  2005. obj->get_property_list(&plist, true);
  2006. bool draw_red = false;
  2007. {
  2008. Node *nod = obj->cast_to<Node>();
  2009. Node *es = EditorNode::get_singleton()->get_edited_scene();
  2010. if (nod && es != nod && nod->get_owner() != es) {
  2011. draw_red = true;
  2012. }
  2013. }
  2014. Color sscolor = get_color("prop_subsection", "Editor");
  2015. TreeItem *current_category = NULL;
  2016. String filter = search_box ? search_box->get_text() : "";
  2017. for (List<PropertyInfo>::Element *I = plist.front(); I; I = I->next()) {
  2018. PropertyInfo &p = I->get();
  2019. //make sure the property can be edited
  2020. if (p.usage & PROPERTY_USAGE_CATEGORY) {
  2021. if (!show_categories)
  2022. continue;
  2023. List<PropertyInfo>::Element *N = I->next();
  2024. bool valid = true;
  2025. //if no properties in category, skip
  2026. while (N) {
  2027. if (N->get().usage & PROPERTY_USAGE_EDITOR)
  2028. break;
  2029. if (N->get().usage & PROPERTY_USAGE_CATEGORY) {
  2030. valid = false;
  2031. break;
  2032. }
  2033. N = N->next();
  2034. }
  2035. if (!valid)
  2036. continue; //empty, ignore
  2037. TreeItem *sep = tree->create_item(root);
  2038. current_category = sep;
  2039. String type = p.name;
  2040. /*if (has_icon(type,"EditorIcons"))
  2041. sep->set_icon(0,get_icon(type,"EditorIcons") );
  2042. else
  2043. sep->set_icon(0,get_icon("Object","EditorIcons") );
  2044. print_line("CATEGORY: "+type);
  2045. */
  2046. sep->set_text(0, type);
  2047. sep->set_selectable(0, false);
  2048. sep->set_selectable(1, false);
  2049. sep->set_custom_bg_color(0, get_color("prop_category", "Editor"));
  2050. sep->set_custom_bg_color(1, get_color("prop_category", "Editor"));
  2051. if (use_doc_hints) {
  2052. StringName type = p.name;
  2053. if (!class_descr_cache.has(type)) {
  2054. String descr;
  2055. DocData *dd = EditorHelp::get_doc_data();
  2056. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(type);
  2057. if (E) {
  2058. descr = E->get().brief_description;
  2059. }
  2060. class_descr_cache[type] = descr.word_wrap(80);
  2061. }
  2062. sep->set_tooltip(0, TTR("Class:") + " " + p.name + ":\n\n" + class_descr_cache[type]);
  2063. }
  2064. //sep->set_custom_color(0,Color(1,1,1));
  2065. continue;
  2066. } else if (!(p.usage & PROPERTY_USAGE_EDITOR))
  2067. continue;
  2068. String name = (p.name.find("/") != -1) ? p.name.right(p.name.find_last("/") + 1) : p.name;
  2069. if (capitalize_paths)
  2070. name = name.camelcase_to_underscore().capitalize();
  2071. String path = p.name.left(p.name.find_last("/"));
  2072. if (use_filter && filter != "") {
  2073. String cat = path;
  2074. if (capitalize_paths)
  2075. cat = cat.capitalize();
  2076. if (!filter.is_subsequence_ofi(cat) && !filter.is_subsequence_ofi(name))
  2077. continue;
  2078. }
  2079. //printf("property %s\n",p.name.ascii().get_data());
  2080. TreeItem *parent = get_parent_node(path, item_path, current_category ? current_category : root);
  2081. //if (parent->get_parent()==root)
  2082. // parent=root;
  2083. int level = 0;
  2084. if (parent != root) {
  2085. level++;
  2086. TreeItem *parent_lev = parent;
  2087. while (parent_lev->get_parent() != root) {
  2088. parent_lev = parent_lev->get_parent();
  2089. level++;
  2090. }
  2091. }
  2092. if (level > 4)
  2093. level = 4;
  2094. Color col = sscolor;
  2095. col.a = (level / 4.0) * 0.7;
  2096. TreeItem *item = tree->create_item(parent);
  2097. if (level > 0) {
  2098. item->set_custom_bg_color(0, col);
  2099. //item->set_custom_bg_color(1,col);
  2100. }
  2101. item->set_editable(0, false);
  2102. item->set_selectable(0, false);
  2103. if (p.usage & PROPERTY_USAGE_CHECKABLE) {
  2104. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  2105. item->set_selectable(0, true);
  2106. item->set_editable(0, true);
  2107. item->set_checked(0, p.usage & PROPERTY_USAGE_CHECKED);
  2108. }
  2109. item->set_text(0, name);
  2110. item->set_tooltip(0, p.name);
  2111. if (use_doc_hints) {
  2112. StringName setter;
  2113. StringName type;
  2114. if (ObjectTypeDB::get_setter_and_type_for_property(obj->get_type_name(), p.name, type, setter)) {
  2115. String descr;
  2116. bool found = false;
  2117. Map<StringName, Map<StringName, String> >::Element *E = descr_cache.find(type);
  2118. if (E) {
  2119. Map<StringName, String>::Element *F = E->get().find(setter);
  2120. if (F) {
  2121. found = true;
  2122. descr = F->get();
  2123. }
  2124. }
  2125. if (!found) {
  2126. DocData *dd = EditorHelp::get_doc_data();
  2127. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(type);
  2128. if (E) {
  2129. for (int i = 0; i < E->get().methods.size(); i++) {
  2130. if (E->get().methods[i].name == setter.operator String()) {
  2131. descr = E->get().methods[i].description.strip_edges().word_wrap(80);
  2132. }
  2133. }
  2134. }
  2135. descr_cache[type][setter] = descr;
  2136. }
  2137. item->set_tooltip(0, TTR("Property:") + " " + p.name + "\n\n" + descr);
  2138. }
  2139. }
  2140. //EditorHelp::get_doc_data();
  2141. Dictionary d;
  2142. d["name"] = p.name;
  2143. d["type"] = (int)p.type;
  2144. d["hint"] = (int)p.hint;
  2145. d["hint_text"] = p.hint_string;
  2146. d["usage"] = (int)p.usage;
  2147. item->set_metadata(0, d);
  2148. item->set_metadata(1, p.name);
  2149. if (draw_red)
  2150. item->set_custom_color(0, Color(0.8, 0.4, 0.20));
  2151. if (p.name == selected_property) {
  2152. item->select(1);
  2153. }
  2154. //printf("property %s type %i\n",p.name.ascii().get_data(),p.type);
  2155. switch (p.type) {
  2156. case Variant::BOOL: {
  2157. item->set_cell_mode(1, TreeItem::CELL_MODE_CHECK);
  2158. item->set_text(1, TTR("On"));
  2159. item->set_tooltip(1, obj->get(p.name) ? "True" : "False");
  2160. item->set_checked(1, obj->get(p.name));
  2161. if (show_type_icons)
  2162. item->set_icon(0, get_icon("Bool", "EditorIcons"));
  2163. item->set_editable(1, !read_only);
  2164. } break;
  2165. case Variant::REAL:
  2166. case Variant::INT: {
  2167. if (p.hint == PROPERTY_HINT_EXP_EASING) {
  2168. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2169. item->set_text(1, String::num(obj->get(p.name), 2));
  2170. item->set_editable(1, !read_only);
  2171. if (show_type_icons)
  2172. item->set_icon(0, get_icon("Curve", "EditorIcons"));
  2173. break;
  2174. }
  2175. if (p.hint == PROPERTY_HINT_ALL_FLAGS) {
  2176. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2177. item->set_editable(1, !read_only);
  2178. item->set_custom_draw(1, this, "_draw_flags");
  2179. break;
  2180. }
  2181. if (p.hint == PROPERTY_HINT_FLAGS) {
  2182. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2183. item->set_editable(1, !read_only);
  2184. //item->set_icon( 0, get_icon("Curve","EditorIcons"));
  2185. Vector<String> values = p.hint_string.split(",");
  2186. String flags;
  2187. int val = obj->get(p.name);
  2188. for (int i = 0; i < values.size(); i++) {
  2189. String v = values[i];
  2190. if (v == "")
  2191. continue;
  2192. if (!(val & (1 << i)))
  2193. continue;
  2194. if (flags != "")
  2195. flags += ", ";
  2196. flags += v;
  2197. }
  2198. item->set_text(1, flags);
  2199. break;
  2200. }
  2201. if (p.hint == PROPERTY_HINT_ENUM)
  2202. item->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
  2203. else
  2204. item->set_cell_mode(1, TreeItem::CELL_MODE_RANGE_EXPRESSION);
  2205. if (p.hint == PROPERTY_HINT_SPRITE_FRAME || p.hint == PROPERTY_HINT_RANGE || p.hint == PROPERTY_HINT_EXP_RANGE) {
  2206. int c = p.hint_string.get_slice_count(",");
  2207. float min = 0, max = 100, step = 1;
  2208. if (c >= 1) {
  2209. min = p.hint_string.get_slice(",", 0).to_double();
  2210. }
  2211. if (c >= 2) {
  2212. max = p.hint_string.get_slice(",", 1).to_double();
  2213. }
  2214. if (p.type != PROPERTY_HINT_SPRITE_FRAME && c >= 3) {
  2215. step = p.hint_string.get_slice(",", 2).to_double();
  2216. }
  2217. item->set_range_config(1, min, max, step, p.hint == PROPERTY_HINT_EXP_RANGE);
  2218. } else if (p.hint == PROPERTY_HINT_ENUM) {
  2219. // int c = p.hint_string.get_slice_count(",");
  2220. item->set_text(1, p.hint_string);
  2221. if (show_type_icons)
  2222. item->set_icon(0, get_icon("Enum", "EditorIcons"));
  2223. item->set_range(1, obj->get(p.name));
  2224. item->set_editable(1, !read_only);
  2225. break;
  2226. } else if (p.hint == PROPERTY_HINT_OBJECT_ID) {
  2227. // int c = p.hint_string.get_slice_count(",");
  2228. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2229. String type = p.hint_string;
  2230. if (type == "")
  2231. type = "Object";
  2232. ObjectID id = _get_curent_remote_object_id(p.name);
  2233. if (id != 0) {
  2234. item->set_text(1, type + " ID: " + itos(id));
  2235. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2236. } else {
  2237. item->set_text(1, "[Empty]");
  2238. }
  2239. if (has_icon(p.hint_string, "EditorIcons")) {
  2240. type = p.hint_string;
  2241. } else {
  2242. type = "Object";
  2243. }
  2244. item->set_icon(0, get_icon(type, "EditorIcons"));
  2245. break;
  2246. } else {
  2247. if (p.type == Variant::REAL) {
  2248. item->set_range_config(1, -16777216, 16777216, 0.001);
  2249. } else {
  2250. item->set_range_config(1, -2147483647, 2147483647, 1);
  2251. }
  2252. };
  2253. if (p.type == Variant::REAL) {
  2254. if (show_type_icons)
  2255. item->set_icon(0, get_icon("Real", "EditorIcons"));
  2256. item->set_range(1, obj->get(p.name));
  2257. } else {
  2258. if (show_type_icons)
  2259. item->set_icon(0, get_icon("Integer", "EditorIcons"));
  2260. item->set_range(1, obj->get(p.name));
  2261. }
  2262. item->set_editable(1, !read_only);
  2263. } break;
  2264. case Variant::STRING: {
  2265. switch (p.hint) {
  2266. case PROPERTY_HINT_DIR:
  2267. case PROPERTY_HINT_FILE:
  2268. case PROPERTY_HINT_GLOBAL_DIR:
  2269. case PROPERTY_HINT_GLOBAL_FILE: {
  2270. item->set_cell_mode(1, TreeItem::CELL_MODE_STRING);
  2271. item->set_editable(1, !read_only);
  2272. if (show_type_icons)
  2273. item->set_icon(0, get_icon("File", "EditorIcons"));
  2274. item->set_text(1, obj->get(p.name));
  2275. item->add_button(1, get_icon("Folder", "EditorIcons"));
  2276. } break;
  2277. case PROPERTY_HINT_ENUM: {
  2278. item->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
  2279. Vector<String> strings = p.hint_string.split(",");
  2280. String current = obj->get(p.name);
  2281. int idx = 0;
  2282. for (int x = 0; x < strings.size(); x++) {
  2283. if (strings[x] == current) {
  2284. idx = x;
  2285. break;
  2286. }
  2287. }
  2288. item->set_text(1, p.hint_string);
  2289. item->set_range(1, idx);
  2290. item->set_editable(1, !read_only);
  2291. if (show_type_icons)
  2292. item->set_icon(0, get_icon("Enum", "EditorIcons"));
  2293. } break;
  2294. default: {
  2295. item->set_cell_mode(1, TreeItem::CELL_MODE_STRING);
  2296. item->set_editable(1, !read_only);
  2297. if (show_type_icons)
  2298. item->set_icon(0, get_icon("String", "EditorIcons"));
  2299. item->set_text(1, obj->get(p.name));
  2300. if (p.hint == PROPERTY_HINT_MULTILINE_TEXT)
  2301. item->add_button(1, get_icon("MultiLine", "EditorIcons"));
  2302. } break;
  2303. }
  2304. } break;
  2305. case Variant::ARRAY: {
  2306. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2307. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2308. Variant v = obj->get(p.name);
  2309. if (v.is_array())
  2310. item->set_text(1, "Array[" + itos(v.call("size")) + "]");
  2311. else
  2312. item->set_text(1, "Array[]");
  2313. if (show_type_icons)
  2314. item->set_icon(0, get_icon("ArrayData", "EditorIcons"));
  2315. } break;
  2316. case Variant::DICTIONARY: {
  2317. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2318. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2319. Dictionary d = obj->get(p.name);
  2320. item->set_text(1, "Dictionary{" + itos(d.size()) + "}");
  2321. } break;
  2322. case Variant::INT_ARRAY: {
  2323. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2324. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2325. Variant v = obj->get(p.name);
  2326. if (v.is_array())
  2327. item->set_text(1, "IntArray[" + itos(v.call("size")) + "]");
  2328. else
  2329. item->set_text(1, "IntArray[]");
  2330. if (show_type_icons)
  2331. item->set_icon(0, get_icon("ArrayInt", "EditorIcons"));
  2332. } break;
  2333. case Variant::REAL_ARRAY: {
  2334. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2335. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2336. Variant v = obj->get(p.name);
  2337. if (v.is_array())
  2338. item->set_text(1, "FloatArray[" + itos(v.call("size")) + "]");
  2339. else
  2340. item->set_text(1, "FloatArray[]");
  2341. if (show_type_icons)
  2342. item->set_icon(0, get_icon("ArrayReal", "EditorIcons"));
  2343. } break;
  2344. case Variant::STRING_ARRAY: {
  2345. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2346. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2347. Variant v = obj->get(p.name);
  2348. if (v.is_array())
  2349. item->set_text(1, "String[" + itos(v.call("size")) + "]");
  2350. else
  2351. item->set_text(1, "String[]");
  2352. if (show_type_icons)
  2353. item->set_icon(0, get_icon("ArrayString", "EditorIcons"));
  2354. } break;
  2355. case Variant::RAW_ARRAY: {
  2356. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2357. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2358. Variant v = obj->get(p.name);
  2359. if (v.is_array())
  2360. item->set_text(1, "Byte[" + itos(v.call("size")) + "]");
  2361. else
  2362. item->set_text(1, "Byte[]");
  2363. if (show_type_icons)
  2364. item->set_icon(0, get_icon("ArrayData", "EditorIcons"));
  2365. } break;
  2366. case Variant::VECTOR2_ARRAY: {
  2367. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2368. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2369. Variant v = obj->get(p.name);
  2370. if (v.is_array())
  2371. item->set_text(1, "Vector2[" + itos(v.call("size")) + "]");
  2372. else
  2373. item->set_text(1, "Vector2[]");
  2374. if (show_type_icons)
  2375. item->set_icon(0, get_icon("Vector2", "EditorIcons"));
  2376. } break;
  2377. case Variant::VECTOR3_ARRAY: {
  2378. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2379. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2380. Variant v = obj->get(p.name);
  2381. if (v.is_array())
  2382. item->set_text(1, "Vector3[" + itos(v.call("size")) + "]");
  2383. else
  2384. item->set_text(1, "Vector3[]");
  2385. if (show_type_icons)
  2386. item->set_icon(0, get_icon("Vector", "EditorIcons"));
  2387. } break;
  2388. case Variant::COLOR_ARRAY: {
  2389. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2390. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2391. Variant v = obj->get(p.name);
  2392. if (v.is_array())
  2393. item->set_text(1, "Color[" + itos(v.call("size")) + "]");
  2394. else
  2395. item->set_text(1, "Color[]");
  2396. if (show_type_icons)
  2397. item->set_icon(0, get_icon("Color", "EditorIcons"));
  2398. } break;
  2399. case Variant::VECTOR2: {
  2400. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2401. item->set_editable(1, true);
  2402. item->set_text(1, obj->get(p.name));
  2403. if (show_type_icons)
  2404. item->set_icon(0, get_icon("Vector2", "EditorIcons"));
  2405. } break;
  2406. case Variant::RECT2: {
  2407. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2408. item->set_editable(1, true);
  2409. item->set_text(1, obj->get(p.name));
  2410. if (show_type_icons)
  2411. item->set_icon(0, get_icon("Rect2", "EditorIcons"));
  2412. } break;
  2413. case Variant::VECTOR3: {
  2414. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2415. item->set_editable(1, true);
  2416. item->set_text(1, obj->get(p.name));
  2417. if (show_type_icons)
  2418. item->set_icon(0, get_icon("Vector", "EditorIcons"));
  2419. } break;
  2420. case Variant::MATRIX32:
  2421. case Variant::MATRIX3: {
  2422. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2423. item->set_editable(1, true);
  2424. item->set_text(1, obj->get(p.name));
  2425. } break;
  2426. case Variant::TRANSFORM: {
  2427. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2428. item->set_editable(1, true);
  2429. item->set_text(1, obj->get(p.name));
  2430. if (show_type_icons)
  2431. item->set_icon(0, get_icon("Matrix", "EditorIcons"));
  2432. } break;
  2433. case Variant::PLANE: {
  2434. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2435. item->set_editable(1, true);
  2436. item->set_text(1, obj->get(p.name));
  2437. if (show_type_icons)
  2438. item->set_icon(0, get_icon("Plane", "EditorIcons"));
  2439. } break;
  2440. case Variant::_AABB: {
  2441. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2442. item->set_editable(1, true);
  2443. item->set_text(1, "AABB");
  2444. if (show_type_icons)
  2445. item->set_icon(0, get_icon("Rect3", "EditorIcons"));
  2446. } break;
  2447. case Variant::QUAT: {
  2448. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2449. item->set_editable(1, true);
  2450. item->set_text(1, obj->get(p.name));
  2451. if (show_type_icons)
  2452. item->set_icon(0, get_icon("Quat", "EditorIcons"));
  2453. } break;
  2454. case Variant::COLOR: {
  2455. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2456. item->set_editable(1, !read_only);
  2457. // item->set_text(1,obj->get(p.name));
  2458. item->set_custom_draw(1, this, "_draw_transparency");
  2459. if (show_type_icons)
  2460. item->set_icon(0, get_icon("Color", "EditorIcons"));
  2461. } break;
  2462. case Variant::IMAGE: {
  2463. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2464. item->set_editable(1, !read_only);
  2465. Image img = obj->get(p.name);
  2466. if (img.empty())
  2467. item->set_text(1, "[Image (empty)]");
  2468. else
  2469. item->set_text(1, "[Image " + itos(img.get_width()) + "x" + itos(img.get_height()) + "-" + String(Image::get_format_name(img.get_format())) + "]");
  2470. if (show_type_icons)
  2471. item->set_icon(0, get_icon("Image", "EditorIcons"));
  2472. } break;
  2473. case Variant::NODE_PATH: {
  2474. item->set_cell_mode(1, TreeItem::CELL_MODE_STRING);
  2475. item->set_editable(1, !read_only);
  2476. item->set_text(1, obj->get(p.name));
  2477. item->add_button(1, get_icon("Collapse", "EditorIcons"));
  2478. } break;
  2479. case Variant::OBJECT: {
  2480. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2481. item->set_editable(1, !read_only);
  2482. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2483. String type;
  2484. if (p.hint == PROPERTY_HINT_RESOURCE_TYPE)
  2485. type = p.hint_string;
  2486. if (obj->get(p.name).get_type() == Variant::NIL || obj->get(p.name).operator RefPtr().is_null()) {
  2487. if (Object *_o = obj->get(p.name)) {
  2488. if (_o->is_type("ScriptEditorDebuggerInspectedObject"))
  2489. item->set_text(1, _o->call("get_title"));
  2490. else
  2491. item->set_text(1, String(_o->get_type_name()) + " ID: " + itos(obj->get_instance_ID()));
  2492. } else {
  2493. item->set_text(1, "<null>");
  2494. item->set_icon(1, Ref<Texture>());
  2495. }
  2496. } else {
  2497. RES res = obj->get(p.name).operator RefPtr();
  2498. if (res->is_type("Texture")) {
  2499. int tw = EditorSettings::get_singleton()->get("property_editor/texture_preview_width");
  2500. Vector2 size(res->call("get_width"), res->call("get_height"));
  2501. if (size.width < size.height) {
  2502. tw = MAX((size.width / size.height) * tw, 1);
  2503. }
  2504. item->set_icon_max_width(1, tw);
  2505. item->set_icon(1, res);
  2506. item->set_text(1, "");
  2507. } else if (res->get_name() != "") {
  2508. item->set_text(1, res->get_name());
  2509. } else if (res->get_path() != "" && !res->get_path().begins_with("local://")) {
  2510. item->set_text(1, res->get_path().get_file());
  2511. } else {
  2512. item->set_text(1, "<" + res->get_type() + ">");
  2513. }
  2514. if (has_icon(res->get_type(), "EditorIcons")) {
  2515. type = res->get_type();
  2516. }
  2517. if (res.is_valid() && res->get_path().is_resource_file()) {
  2518. item->set_tooltip(1, res->get_path());
  2519. } else if (res.is_valid()) {
  2520. item->set_tooltip(1, res->get_name() + " (" + res->get_type() + ")");
  2521. }
  2522. if (res->is_type("Script")) {
  2523. item->set_text(1, res->get_path().get_file());
  2524. } else if (!res->is_type("Texture")) {
  2525. //texture already previews via itself
  2526. EditorResourcePreview::get_singleton()->queue_edited_resource_preview(res, this, "_resource_preview_done", item->get_instance_ID());
  2527. }
  2528. }
  2529. if (type != String()) {
  2530. if (type.find(",") != -1)
  2531. type = type.get_slice(",", 0);
  2532. //printf("prop %s , type %s\n",p.name.ascii().get_data(),p.hint_string.ascii().get_data());
  2533. if (has_icon(type, "EditorIcons"))
  2534. item->set_icon(0, get_icon(type, "EditorIcons"));
  2535. else
  2536. item->set_icon(0, get_icon("Object", "EditorIcons"));
  2537. }
  2538. // item->double_click_signal.connect( Method1<int>( Method2<int,String>( this, &Editoritem_obj_edited ), p.name ) );
  2539. } break;
  2540. default: {};
  2541. }
  2542. if (keying) {
  2543. if (p.hint == PROPERTY_HINT_SPRITE_FRAME) {
  2544. item->add_button(1, get_icon("KeyNext", "EditorIcons"), 5);
  2545. } else {
  2546. item->add_button(1, get_icon("Key", "EditorIcons"), 2);
  2547. }
  2548. }
  2549. bool has_reload = false;
  2550. bool mbi = _might_be_in_instance();
  2551. if (mbi) {
  2552. Variant vorig;
  2553. Dictionary d = item->get_metadata(0);
  2554. int usage = d.has("usage") ? int(int(d["usage"]) & (PROPERTY_USAGE_STORE_IF_NONONE | PROPERTY_USAGE_STORE_IF_NONZERO)) : 0;
  2555. if (_get_instanced_node_original_property(p.name, vorig) || usage) {
  2556. Variant v = obj->get(p.name);
  2557. if (_is_property_different(v, vorig, usage)) {
  2558. //print_line("FOR "+String(p.name)+" RELOAD WITH: "+String(v)+"("+Variant::get_type_name(v.get_type())+")=="+String(vorig)+"("+Variant::get_type_name(vorig.get_type())+")");
  2559. item->add_button(1, get_icon("ReloadSmall", "EditorIcons"), 3);
  2560. has_reload = true;
  2561. }
  2562. }
  2563. }
  2564. if (!has_reload && !obj->get_script().is_null()) {
  2565. Ref<Script> scr = obj->get_script();
  2566. Variant orig_value;
  2567. if (scr->get_property_default_value(p.name, orig_value)) {
  2568. if (orig_value != obj->get(p.name)) {
  2569. item->add_button(1, get_icon("ReloadSmall", "EditorIcons"), 3);
  2570. has_reload = true;
  2571. }
  2572. }
  2573. }
  2574. if (mbi && !has_reload && item->get_cell_mode(1) == TreeItem::CELL_MODE_RANGE && item->get_text(1) == String()) {
  2575. item->add_button(1, get_icon("ReloadEmpty", "EditorIcons"), 3, true);
  2576. }
  2577. }
  2578. }
  2579. void PropertyEditor::_draw_transparency(Object *t, const Rect2 &p_rect) {
  2580. TreeItem *ti = t->cast_to<TreeItem>();
  2581. if (!ti)
  2582. return;
  2583. Color color = obj->get(ti->get_metadata(1));
  2584. Ref<Texture> arrow = tree->get_icon("select_arrow");
  2585. // make a little space between consecutive color fields
  2586. Rect2 area = p_rect;
  2587. area.pos.y += 1;
  2588. area.size.height -= 2;
  2589. area.size.width -= arrow->get_size().width + 5;
  2590. tree->draw_texture_rect(get_icon("Transparent", "EditorIcons"), area, true);
  2591. tree->draw_rect(area, color);
  2592. }
  2593. ObjectID PropertyEditor::_get_curent_remote_object_id(const StringName &p_name) {
  2594. ObjectID id = 0;
  2595. if (obj) {
  2596. id = obj->get(p_name);
  2597. if (id == 0) {
  2598. Object *debugObj = NULL;
  2599. if (obj->is_type("ScriptEditorDebuggerVariables")) {
  2600. if (Object *oo = obj->call("get_var_value", p_name)) {
  2601. if (oo->is_type("ScriptEditorDebuggerInspectedObject"))
  2602. debugObj = oo;
  2603. }
  2604. } else if (obj->is_type("ScriptEditorDebuggerInspectedObject")) {
  2605. if (Object *oo = obj->call("get_variant", p_name)) {
  2606. if (oo->is_type("ScriptEditorDebuggerInspectedObject"))
  2607. debugObj = oo;
  2608. }
  2609. }
  2610. if (debugObj) {
  2611. id = debugObj->call("get_remote_object_id");
  2612. }
  2613. }
  2614. }
  2615. return id;
  2616. }
  2617. void PropertyEditor::_item_selected() {
  2618. TreeItem *item = tree->get_selected();
  2619. ERR_FAIL_COND(!item);
  2620. selected_property = item->get_metadata(1);
  2621. }
  2622. void PropertyEditor::_edit_set(const String &p_name, const Variant &p_value, const String &p_changed_field) {
  2623. if (autoclear) {
  2624. TreeItem *item = tree->get_selected();
  2625. if (item && item->get_cell_mode(0) == TreeItem::CELL_MODE_CHECK) {
  2626. item->set_checked(0, true);
  2627. }
  2628. }
  2629. if (!undo_redo || obj->cast_to<ArrayPropertyEdit>()) { //kind of hacky
  2630. obj->set(p_name, p_value);
  2631. _changed_callbacks(obj, p_name);
  2632. emit_signal(_prop_edited, p_name);
  2633. } else if (obj->cast_to<MultiNodeEdit>()) {
  2634. obj->cast_to<MultiNodeEdit>()->set_property_field(p_name, p_value, p_changed_field);
  2635. _changed_callbacks(obj, p_name);
  2636. emit_signal(_prop_edited, p_name);
  2637. } else {
  2638. undo_redo->create_action(TTR("Set") + " " + p_name, UndoRedo::MERGE_ENDS);
  2639. undo_redo->add_do_property(obj, p_name, p_value);
  2640. undo_redo->add_undo_property(obj, p_name, obj->get(p_name));
  2641. undo_redo->add_do_method(this, "_changed_callback", obj, p_name);
  2642. undo_redo->add_undo_method(this, "_changed_callback", obj, p_name);
  2643. undo_redo->add_undo_method(this, "_changed_callback", obj, p_name);
  2644. Resource *r = obj->cast_to<Resource>();
  2645. if (r) {
  2646. if (!r->is_edited() && String(p_name) != "resource/edited") {
  2647. undo_redo->add_do_method(r, "set_edited", true);
  2648. undo_redo->add_undo_method(r, "set_edited", false);
  2649. }
  2650. }
  2651. _prop_edited_name[0] = p_name;
  2652. undo_redo->add_do_method(this, "emit_signal", _prop_edited, _prop_edited_name);
  2653. undo_redo->commit_action();
  2654. }
  2655. }
  2656. void PropertyEditor::_item_edited() {
  2657. TreeItem *item = tree->get_edited();
  2658. if (!item)
  2659. return; //it all happened too fast..
  2660. Dictionary d = item->get_metadata(0);
  2661. String name = d["name"];
  2662. if (tree->get_edited_column() == 0) {
  2663. //property checked
  2664. if (autoclear) {
  2665. if (!item->is_checked(0)) {
  2666. obj->set(name, Variant());
  2667. update_property(name);
  2668. } else {
  2669. Variant::CallError ce;
  2670. obj->set(name, Variant::construct(Variant::Type(int(d["type"])), NULL, 0, ce));
  2671. }
  2672. } else {
  2673. emit_signal("property_toggled", name, item->is_checked(0));
  2674. }
  2675. return;
  2676. }
  2677. if (autoclear && item->get_cell_mode(0) == TreeItem::CELL_MODE_CHECK && item->get_cell_mode(1) != TreeItem::CELL_MODE_CUSTOM) {
  2678. item->set_checked(0, true);
  2679. }
  2680. int type = d["type"];
  2681. int hint = d["hint"];
  2682. String hint_text = d["hint_text"];
  2683. switch (type) {
  2684. case Variant::NIL: {
  2685. } break;
  2686. case Variant::BOOL: {
  2687. item->set_tooltip(1, item->is_checked(1) ? "True" : "False");
  2688. _edit_set(name, item->is_checked(1));
  2689. } break;
  2690. case Variant::INT:
  2691. case Variant::REAL: {
  2692. if (hint == PROPERTY_HINT_ALL_FLAGS)
  2693. break;
  2694. if (hint == PROPERTY_HINT_EXP_EASING)
  2695. break;
  2696. if (hint == PROPERTY_HINT_FLAGS)
  2697. break;
  2698. if (type == Variant::INT)
  2699. _edit_set(name, int(item->get_range(1)));
  2700. else
  2701. _edit_set(name, item->get_range(1));
  2702. } break;
  2703. case Variant::STRING: {
  2704. if (hint == PROPERTY_HINT_ENUM) {
  2705. int idx = item->get_range(1);
  2706. Vector<String> strings = hint_text.split(",");
  2707. String txt;
  2708. if (idx >= 0 && idx < strings.size()) {
  2709. txt = strings[idx];
  2710. }
  2711. _edit_set(name, txt);
  2712. } else {
  2713. _edit_set(name, item->get_text(1));
  2714. }
  2715. } break;
  2716. // math types
  2717. case Variant::VECTOR3: {
  2718. } break;
  2719. case Variant::PLANE: {
  2720. } break;
  2721. case Variant::QUAT: {
  2722. } break;
  2723. case Variant::_AABB: {
  2724. } break;
  2725. case Variant::MATRIX3: {
  2726. } break;
  2727. case Variant::TRANSFORM: {
  2728. } break;
  2729. case Variant::COLOR: {
  2730. //_edit_set(name,item->get_custom_bg_color(0));
  2731. } break;
  2732. case Variant::IMAGE: {
  2733. } break;
  2734. case Variant::NODE_PATH: {
  2735. _edit_set(name, NodePath(item->get_text(1)));
  2736. } break;
  2737. case Variant::INPUT_EVENT: {
  2738. } break;
  2739. case Variant::DICTIONARY: {
  2740. } break;
  2741. // arrays
  2742. case Variant::RAW_ARRAY: {
  2743. } break;
  2744. case Variant::INT_ARRAY: {
  2745. } break;
  2746. case Variant::REAL_ARRAY: {
  2747. } break;
  2748. case Variant::STRING_ARRAY: {
  2749. } break;
  2750. case Variant::VECTOR3_ARRAY: {
  2751. } break;
  2752. case Variant::COLOR_ARRAY: {
  2753. } break;
  2754. };
  2755. }
  2756. void PropertyEditor::_resource_edit_request() {
  2757. RES res = custom_editor->get_variant();
  2758. if (res.is_null())
  2759. return;
  2760. String name = custom_editor->get_name();
  2761. emit_signal("resource_selected", res.get_ref_ptr(), name);
  2762. }
  2763. void PropertyEditor::_custom_editor_edited() {
  2764. if (!obj)
  2765. return;
  2766. _edit_set(custom_editor->get_name(), custom_editor->get_variant());
  2767. }
  2768. void PropertyEditor::_custom_editor_edited_field(const String &p_field_name) {
  2769. ERR_FAIL_COND(p_field_name == "");
  2770. if (!obj)
  2771. return;
  2772. _edit_set(custom_editor->get_name(), custom_editor->get_variant(), p_field_name);
  2773. }
  2774. void PropertyEditor::_custom_editor_request(bool p_arrow) {
  2775. TreeItem *item = tree->get_edited();
  2776. ERR_FAIL_COND(!item);
  2777. Dictionary d = item->get_metadata(0);
  2778. //int type=d["type"];
  2779. String name = d["name"];
  2780. Variant::Type type = Variant::NIL;
  2781. if (d.has("type"))
  2782. type = (Variant::Type)((int)(d["type"]));
  2783. Variant v = obj->get(name);
  2784. int hint = d.has("hint") ? d["hint"].operator int() : -1;
  2785. String hint_text = d.has("hint_text") ? d["hint_text"] : "";
  2786. Rect2 where = tree->get_custom_popup_rect();
  2787. custom_editor->set_pos(where.pos);
  2788. if (custom_editor->edit(obj, name, type, v, hint, hint_text)) {
  2789. custom_editor->popup();
  2790. }
  2791. }
  2792. void PropertyEditor::edit(Object *p_object) {
  2793. if (obj == p_object)
  2794. return;
  2795. obj = p_object;
  2796. if (obj) {
  2797. set_enable_capitalize_paths(true);
  2798. obj->remove_change_receptor(this);
  2799. if (obj->is_type("ScriptEditorDebuggerInspectedObject")) {
  2800. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  2801. if (sed->is_connected()) {
  2802. set_enable_capitalize_paths(false);
  2803. emit_signal("object_id_selected", obj->call("get_remote_object_id"));
  2804. } else {
  2805. obj = NULL;
  2806. p_object = NULL;
  2807. }
  2808. }
  2809. }
  2810. evaluator->edit(p_object);
  2811. update_tree();
  2812. if (obj) {
  2813. obj->add_change_receptor(this);
  2814. }
  2815. }
  2816. void PropertyEditor::_set_range_def(Object *p_item, String prop, float p_frame) {
  2817. TreeItem *ti = p_item->cast_to<TreeItem>();
  2818. ERR_FAIL_COND(!ti);
  2819. ti->call_deferred("set_range", 1, p_frame);
  2820. obj->call_deferred("set", prop, p_frame);
  2821. }
  2822. void PropertyEditor::_edit_button(Object *p_item, int p_column, int p_button) {
  2823. TreeItem *ti = p_item->cast_to<TreeItem>();
  2824. ERR_FAIL_COND(!ti);
  2825. Dictionary d = ti->get_metadata(0);
  2826. if (p_button == 2) {
  2827. if (!d.has("name"))
  2828. return;
  2829. String prop = d["name"];
  2830. emit_signal("property_keyed", prop, obj->get(prop), false);
  2831. } else if (p_button == 5) {
  2832. print_line("PB5");
  2833. if (!d.has("name"))
  2834. return;
  2835. String prop = d["name"];
  2836. emit_signal("property_keyed", prop, obj->get(prop), true);
  2837. //set_range(p_column, ti->get_range(p_column)+1.0 );
  2838. call_deferred("_set_range_def", ti, prop, ti->get_range(p_column) + 1.0);
  2839. } else if (p_button == 3) {
  2840. if (!d.has("name"))
  2841. return;
  2842. String prop = d["name"];
  2843. Variant vorig;
  2844. if (_might_be_in_instance() && _get_instanced_node_original_property(prop, vorig)) {
  2845. _edit_set(prop, vorig);
  2846. return;
  2847. }
  2848. if (!obj->get_script().is_null()) {
  2849. Ref<Script> scr = obj->get_script();
  2850. Variant orig_value;
  2851. if (scr->get_property_default_value(prop, orig_value)) {
  2852. _edit_set(prop, orig_value);
  2853. }
  2854. }
  2855. } else {
  2856. Dictionary d = ti->get_metadata(0);
  2857. if (!d.has("type"))
  2858. return;
  2859. if (!d.has("hint"))
  2860. return;
  2861. if (!d.has("name"))
  2862. return;
  2863. if (!d.has("hint_text"))
  2864. return;
  2865. int t = d["type"];
  2866. int h = d["hint"];
  2867. String n = d["name"];
  2868. String ht = d["hint_text"];
  2869. if (t == Variant::NODE_PATH) {
  2870. Variant v = obj->get(n);
  2871. custom_editor->edit(obj, n, (Variant::Type)t, v, h, ht);
  2872. Rect2 where = tree->get_item_rect(ti, 1);
  2873. where.pos -= tree->get_scroll();
  2874. where.pos += tree->get_global_pos();
  2875. custom_editor->set_pos(where.pos);
  2876. custom_editor->popup();
  2877. } else if (t == Variant::STRING) {
  2878. Variant v = obj->get(n);
  2879. custom_editor->edit(obj, n, (Variant::Type)t, v, h, ht);
  2880. //Rect2 where=tree->get_custom_popup_rect();
  2881. if (h == PROPERTY_HINT_FILE || h == PROPERTY_HINT_DIR || h == PROPERTY_HINT_GLOBAL_DIR || h == PROPERTY_HINT_GLOBAL_FILE) {
  2882. Rect2 where = tree->get_item_rect(ti, 1);
  2883. where.pos -= tree->get_scroll();
  2884. where.pos += tree->get_global_pos();
  2885. custom_editor->set_pos(where.pos);
  2886. custom_editor->popup();
  2887. } else {
  2888. custom_editor->popup_centered_ratio();
  2889. }
  2890. } else if (t == Variant::OBJECT) {
  2891. Variant var = obj->get(n);
  2892. RES r = var;
  2893. if (r.is_valid()) {
  2894. emit_signal("resource_selected", r, n);
  2895. } else if (Object *o = var) {
  2896. // Remote object clicked form property editor cell
  2897. if (o->is_type("ScriptEditorDebuggerInspectedObject")) {
  2898. ObjectID id = o->call("get_remote_object_id");
  2899. emit_signal("object_id_selected", id);
  2900. print_line(String("OBJ ID SELECTED: ") + itos(id));
  2901. }
  2902. }
  2903. } else if (t == Variant::INT && h == PROPERTY_HINT_OBJECT_ID) {
  2904. ObjectID id = _get_curent_remote_object_id(n);
  2905. emit_signal("object_id_selected", id);
  2906. print_line(String("OBJ ID SELECTED: ") + itos(id));
  2907. } else if (t == Variant::ARRAY || t == Variant::INT_ARRAY || t == Variant::REAL_ARRAY || t == Variant::STRING_ARRAY || t == Variant::VECTOR2_ARRAY || t == Variant::VECTOR3_ARRAY || t == Variant::COLOR_ARRAY || t == Variant::RAW_ARRAY) {
  2908. Variant v = obj->get(n);
  2909. if (v.get_type() != t) {
  2910. Variant::CallError ce;
  2911. v = Variant::construct(Variant::Type(t), NULL, 0, ce);
  2912. }
  2913. Ref<ArrayPropertyEdit> ape = memnew(ArrayPropertyEdit);
  2914. ape->edit(obj, n, Variant::Type(t));
  2915. EditorNode::get_singleton()->push_item(ape.ptr());
  2916. } else if (t == Variant::DICTIONARY) {
  2917. Ref<DictionaryPropertyEdit> dpe = memnew(DictionaryPropertyEdit);
  2918. dpe->edit(obj, n);
  2919. EditorNode::get_singleton()->push_item(dpe.ptr());
  2920. }
  2921. }
  2922. }
  2923. void PropertyEditor::_node_removed(Node *p_node) {
  2924. if (p_node == obj) {
  2925. edit(NULL);
  2926. }
  2927. }
  2928. void PropertyEditor::set_keying(bool p_active) {
  2929. if (keying == p_active)
  2930. return;
  2931. keying = p_active;
  2932. update_tree();
  2933. }
  2934. void PropertyEditor::_draw_flags(Object *t, const Rect2 &p_rect) {
  2935. TreeItem *ti = t->cast_to<TreeItem>();
  2936. if (!ti)
  2937. return;
  2938. Dictionary d = ti->get_metadata(0);
  2939. if (!d.has("name"))
  2940. return;
  2941. uint32_t f = obj->get(d["name"]);
  2942. int bsize = (p_rect.size.height * 80 / 100) / 2;
  2943. int h = bsize * 2 + 1;
  2944. int vofs = (p_rect.size.height - h) / 2;
  2945. for (int i = 0; i < 2; i++) {
  2946. Point2 ofs(4, vofs);
  2947. if (i == 1)
  2948. ofs.y += bsize + 1;
  2949. ofs += p_rect.pos;
  2950. for (int j = 0; j < 10; j++) {
  2951. Point2 o = ofs + Point2(j * (bsize + 1), 0);
  2952. if (j >= 5)
  2953. o.x += 1;
  2954. uint32_t idx = i * 10 + j;
  2955. bool on = f & (1 << idx);
  2956. tree->draw_rect(Rect2(o, Size2(bsize, bsize)), Color(0, 0, 0, on ? 0.8 : 0.3));
  2957. }
  2958. }
  2959. }
  2960. void PropertyEditor::_filter_changed(const String &p_text) {
  2961. update_tree();
  2962. }
  2963. void PropertyEditor::_resource_preview_done(const String &p_path, const Ref<Texture> &p_preview, Variant p_ud) {
  2964. if (p_preview.is_null())
  2965. return; //don't bother with empty preview
  2966. ObjectID id = p_ud;
  2967. Object *obj = ObjectDB::get_instance(id);
  2968. if (!obj)
  2969. return;
  2970. TreeItem *ti = obj->cast_to<TreeItem>();
  2971. ERR_FAIL_COND(!ti);
  2972. int tw = EditorSettings::get_singleton()->get("property_editor/texture_preview_width");
  2973. ti->set_icon(1, p_preview); //should be scaled I think?
  2974. ti->set_icon_max_width(1, tw);
  2975. ti->set_text(1, "");
  2976. }
  2977. void PropertyEditor::_bind_methods() {
  2978. ObjectTypeDB::bind_method("_item_edited", &PropertyEditor::_item_edited);
  2979. ObjectTypeDB::bind_method("_item_selected", &PropertyEditor::_item_selected);
  2980. ObjectTypeDB::bind_method("_custom_editor_request", &PropertyEditor::_custom_editor_request);
  2981. ObjectTypeDB::bind_method("_custom_editor_edited", &PropertyEditor::_custom_editor_edited);
  2982. ObjectTypeDB::bind_method("_custom_editor_edited_field", &PropertyEditor::_custom_editor_edited_field, DEFVAL(""));
  2983. ObjectTypeDB::bind_method("_resource_edit_request", &PropertyEditor::_resource_edit_request);
  2984. ObjectTypeDB::bind_method("_node_removed", &PropertyEditor::_node_removed);
  2985. ObjectTypeDB::bind_method("_edit_button", &PropertyEditor::_edit_button);
  2986. ObjectTypeDB::bind_method("_changed_callback", &PropertyEditor::_changed_callbacks);
  2987. ObjectTypeDB::bind_method("_draw_flags", &PropertyEditor::_draw_flags);
  2988. ObjectTypeDB::bind_method("_set_range_def", &PropertyEditor::_set_range_def);
  2989. ObjectTypeDB::bind_method("_filter_changed", &PropertyEditor::_filter_changed);
  2990. ObjectTypeDB::bind_method("update_tree", &PropertyEditor::update_tree);
  2991. ObjectTypeDB::bind_method("_resource_preview_done", &PropertyEditor::_resource_preview_done);
  2992. ObjectTypeDB::bind_method("refresh", &PropertyEditor::refresh);
  2993. ObjectTypeDB::bind_method("_draw_transparency", &PropertyEditor::_draw_transparency);
  2994. ObjectTypeDB::bind_method(_MD("get_drag_data_fw"), &PropertyEditor::get_drag_data_fw);
  2995. ObjectTypeDB::bind_method(_MD("can_drop_data_fw"), &PropertyEditor::can_drop_data_fw);
  2996. ObjectTypeDB::bind_method(_MD("drop_data_fw"), &PropertyEditor::drop_data_fw);
  2997. ADD_SIGNAL(MethodInfo("property_toggled", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::BOOL, "value")));
  2998. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::OBJECT, "res"), PropertyInfo(Variant::STRING, "prop")));
  2999. ADD_SIGNAL(MethodInfo("object_id_selected", PropertyInfo(Variant::INT, "id")));
  3000. ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING, "property")));
  3001. ADD_SIGNAL(MethodInfo("property_edited", PropertyInfo(Variant::STRING, "property")));
  3002. }
  3003. Tree *PropertyEditor::get_scene_tree() {
  3004. return tree;
  3005. }
  3006. Label *PropertyEditor::get_top_label() {
  3007. return top_label;
  3008. }
  3009. void PropertyEditor::hide_top_label() {
  3010. top_label->hide();
  3011. tree->set_begin(Point2(0, 0));
  3012. }
  3013. String PropertyEditor::get_selected_path() const {
  3014. TreeItem *ti = tree->get_selected();
  3015. if (!ti)
  3016. return "";
  3017. Dictionary d = ti->get_metadata(0);
  3018. if (d.has("name"))
  3019. return d["name"];
  3020. else
  3021. return "";
  3022. }
  3023. bool PropertyEditor::is_capitalize_paths_enabled() const {
  3024. return capitalize_paths;
  3025. }
  3026. void PropertyEditor::set_enable_capitalize_paths(bool p_capitalize) {
  3027. capitalize_paths = p_capitalize;
  3028. update_tree_pending = true;
  3029. }
  3030. void PropertyEditor::set_autoclear(bool p_enable) {
  3031. autoclear = p_enable;
  3032. }
  3033. void PropertyEditor::set_show_categories(bool p_show) {
  3034. show_categories = p_show;
  3035. update_tree();
  3036. }
  3037. void PropertyEditor::set_use_filter(bool p_use) {
  3038. if (p_use == use_filter)
  3039. return;
  3040. use_filter = p_use;
  3041. update_tree();
  3042. }
  3043. void PropertyEditor::register_text_enter(Node *p_line_edit) {
  3044. ERR_FAIL_NULL(p_line_edit);
  3045. search_box = p_line_edit->cast_to<LineEdit>();
  3046. if (search_box)
  3047. search_box->connect("text_changed", this, "_filter_changed");
  3048. }
  3049. void PropertyEditor::set_subsection_selectable(bool p_selectable) {
  3050. if (p_selectable == subsection_selectable)
  3051. return;
  3052. subsection_selectable = p_selectable;
  3053. update_tree();
  3054. }
  3055. PropertyEditor::PropertyEditor() {
  3056. _prop_edited = "property_edited";
  3057. _prop_edited_name.push_back(String());
  3058. undo_redo = NULL;
  3059. obj = NULL;
  3060. search_box = NULL;
  3061. changing = false;
  3062. update_tree_pending = false;
  3063. top_label = memnew(Label);
  3064. top_label->set_text(TTR("Properties:"));
  3065. top_label->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  3066. top_label->set_begin(Point2(10, 0));
  3067. top_label->set_end(Point2(0, 12));
  3068. add_child(top_label);
  3069. tree = memnew(Tree);
  3070. tree->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  3071. tree->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  3072. tree->set_begin(Point2(0, 19));
  3073. tree->set_end(Point2(0, 0));
  3074. tree->set_columns(2);
  3075. tree->set_column_expand(0, true);
  3076. tree->set_column_min_width(0, 30);
  3077. tree->set_column_expand(1, true);
  3078. tree->set_column_min_width(1, 18);
  3079. //tree->set_hide_root(true);
  3080. add_child(tree);
  3081. tree->connect("item_edited", this, "_item_edited", varray(), CONNECT_DEFERRED);
  3082. tree->connect("cell_selected", this, "_item_selected");
  3083. tree->set_drag_forwarding(this);
  3084. set_fixed_process(true);
  3085. custom_editor = memnew(CustomPropertyEditor);
  3086. add_child(custom_editor);
  3087. tree->connect("custom_popup_edited", this, "_custom_editor_request");
  3088. tree->connect("button_pressed", this, "_edit_button");
  3089. custom_editor->connect("variant_changed", this, "_custom_editor_edited");
  3090. custom_editor->connect("variant_field_changed", this, "_custom_editor_edited_field");
  3091. custom_editor->connect("resource_edit_request", this, "_resource_edit_request", make_binds(), CONNECT_DEFERRED);
  3092. tree->set_hide_folding(true);
  3093. evaluator = memnew(PropertyValueEvaluator);
  3094. tree->set_value_evaluator(evaluator);
  3095. custom_editor->set_value_evaluator(evaluator);
  3096. capitalize_paths = true;
  3097. autoclear = false;
  3098. tree->set_column_titles_visible(false);
  3099. tree->add_constant_override("button_margin", 0);
  3100. keying = false;
  3101. read_only = false;
  3102. show_categories = false;
  3103. refresh_countdown = 0;
  3104. use_doc_hints = false;
  3105. use_filter = false;
  3106. subsection_selectable = false;
  3107. show_type_icons = EDITOR_DEF("inspector/show_type_icons", false);
  3108. }
  3109. PropertyEditor::~PropertyEditor() {
  3110. memdelete(evaluator);
  3111. }
  3112. /////////////////////////////
  3113. class SectionedPropertyEditorFilter : public Object {
  3114. OBJ_TYPE(SectionedPropertyEditorFilter, Object);
  3115. Object *edited;
  3116. String section;
  3117. bool _set(const StringName &p_name, const Variant &p_value) {
  3118. if (!edited)
  3119. return false;
  3120. String name = p_name;
  3121. if (section != "") {
  3122. name = section + "/" + name;
  3123. }
  3124. bool valid;
  3125. edited->set(name, p_value, &valid);
  3126. return valid;
  3127. }
  3128. bool _get(const StringName &p_name, Variant &r_ret) const {
  3129. if (!edited)
  3130. return false;
  3131. String name = p_name;
  3132. if (section != "") {
  3133. name = section + "/" + name;
  3134. }
  3135. bool valid = false;
  3136. r_ret = edited->get(name, &valid);
  3137. return valid;
  3138. }
  3139. void _get_property_list(List<PropertyInfo> *p_list) const {
  3140. if (!edited)
  3141. return;
  3142. List<PropertyInfo> pinfo;
  3143. edited->get_property_list(&pinfo);
  3144. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  3145. PropertyInfo pi = E->get();
  3146. int sp = pi.name.find("/");
  3147. if (sp != -1) {
  3148. String ss = pi.name.substr(0, sp);
  3149. if (ss == section) {
  3150. pi.name = pi.name.substr(sp + 1, pi.name.length());
  3151. p_list->push_back(pi);
  3152. }
  3153. } else {
  3154. if (section == "")
  3155. p_list->push_back(pi);
  3156. }
  3157. }
  3158. }
  3159. public:
  3160. void set_section(const String &p_section) {
  3161. section = p_section;
  3162. _change_notify();
  3163. }
  3164. void set_edited(Object *p_edited) {
  3165. edited = p_edited;
  3166. _change_notify();
  3167. }
  3168. SectionedPropertyEditorFilter() {
  3169. edited = NULL;
  3170. }
  3171. };
  3172. void SectionedPropertyEditor::_bind_methods() {
  3173. ObjectTypeDB::bind_method("_section_selected", &SectionedPropertyEditor::_section_selected);
  3174. ObjectTypeDB::bind_method("update_category_list", &SectionedPropertyEditor::update_category_list);
  3175. }
  3176. void SectionedPropertyEditor::_section_selected(int p_which) {
  3177. filter->set_section(sections->get_item_metadata(p_which));
  3178. }
  3179. void SectionedPropertyEditor::set_current_section(const String &p_section) {
  3180. int section_idx = sections->find_metadata(p_section);
  3181. if (section_idx == sections->get_current())
  3182. return;
  3183. if (section_idx != -1) {
  3184. sections->select(section_idx);
  3185. _section_selected(section_idx);
  3186. } else if (sections->get_item_count()) {
  3187. sections->select(0);
  3188. _section_selected(0);
  3189. }
  3190. }
  3191. String SectionedPropertyEditor::get_current_section() const {
  3192. if (sections->get_current() != -1)
  3193. return sections->get_item_metadata(sections->get_current());
  3194. else
  3195. return "";
  3196. }
  3197. String SectionedPropertyEditor::get_full_item_path(const String &p_item) {
  3198. String base = get_current_section();
  3199. if (base != "")
  3200. return base + "/" + p_item;
  3201. else
  3202. return p_item;
  3203. }
  3204. void SectionedPropertyEditor::edit(Object *p_object) {
  3205. if (!p_object) {
  3206. obj = -1;
  3207. sections->clear();
  3208. filter->set_edited(NULL);
  3209. editor->edit(NULL);
  3210. return;
  3211. }
  3212. ObjectID id = p_object->get_instance_ID();
  3213. if (obj != id) {
  3214. obj = id;
  3215. update_category_list();
  3216. filter->set_edited(p_object);
  3217. editor->edit(filter);
  3218. sections->select(0);
  3219. _section_selected(0);
  3220. } else {
  3221. update_category_list();
  3222. }
  3223. }
  3224. void SectionedPropertyEditor::update_category_list() {
  3225. String selected_category = get_current_section();
  3226. sections->clear();
  3227. Object *o = ObjectDB::get_instance(obj);
  3228. if (!o)
  3229. return;
  3230. List<PropertyInfo> pinfo;
  3231. o->get_property_list(&pinfo);
  3232. Set<String> existing_sections;
  3233. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  3234. PropertyInfo pi = E->get();
  3235. if (pi.usage & PROPERTY_USAGE_CATEGORY)
  3236. continue;
  3237. else if (!(pi.usage & PROPERTY_USAGE_EDITOR))
  3238. continue;
  3239. if (pi.name.find(":") != -1 || pi.name == "script/script" || pi.name.begins_with("resource/"))
  3240. continue;
  3241. int sp = pi.name.find("/");
  3242. if (sp != -1) {
  3243. String sname = pi.name.substr(0, sp);
  3244. if (!existing_sections.has(sname)) {
  3245. existing_sections.insert(sname);
  3246. sections->add_item(sname.capitalize());
  3247. sections->set_item_metadata(sections->get_item_count() - 1, sname);
  3248. }
  3249. } else {
  3250. if (!existing_sections.has("")) {
  3251. existing_sections.insert("");
  3252. sections->add_item(TTR("Global"));
  3253. sections->set_item_metadata(sections->get_item_count() - 1, "");
  3254. }
  3255. }
  3256. }
  3257. set_current_section(selected_category);
  3258. }
  3259. PropertyEditor *SectionedPropertyEditor::get_property_editor() {
  3260. return editor;
  3261. }
  3262. SectionedPropertyEditor::SectionedPropertyEditor() {
  3263. obj = -1;
  3264. VBoxContainer *left_vb = memnew(VBoxContainer);
  3265. left_vb->set_custom_minimum_size(Size2(160, 0) * EDSCALE);
  3266. add_child(left_vb);
  3267. sections = memnew(ItemList);
  3268. sections->set_v_size_flags(SIZE_EXPAND_FILL);
  3269. left_vb->add_margin_child(TTR("Sections:"), sections, true);
  3270. VBoxContainer *right_vb = memnew(VBoxContainer);
  3271. right_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  3272. add_child(right_vb);
  3273. filter = memnew(SectionedPropertyEditorFilter);
  3274. editor = memnew(PropertyEditor);
  3275. editor->set_v_size_flags(SIZE_EXPAND_FILL);
  3276. right_vb->add_margin_child(TTR("Properties:"), editor, true);
  3277. editor->get_scene_tree()->set_column_titles_visible(false);
  3278. editor->hide_top_label();
  3279. sections->connect("item_selected", this, "_section_selected");
  3280. }
  3281. SectionedPropertyEditor::~SectionedPropertyEditor() {
  3282. memdelete(filter);
  3283. }
  3284. double PropertyValueEvaluator::eval(const String &p_text) {
  3285. if (!obj)
  3286. return _default_eval(p_text);
  3287. Ref<Script> script = Ref<Script>(script_language->create_script());
  3288. script->set_source_code(_build_script(p_text));
  3289. Error err = script->reload();
  3290. if (err) {
  3291. print_line("[PropertyValueEvaluator] Error loading script for expression: " + p_text);
  3292. return _default_eval(p_text);
  3293. }
  3294. ScriptInstance *script_instance = script->instance_create(this);
  3295. Variant::CallError call_err;
  3296. script_instance->call("set_this", obj);
  3297. double result = script_instance->call("e", NULL, 0, call_err);
  3298. if (call_err.error == Variant::CallError::CALL_OK) {
  3299. return result;
  3300. }
  3301. print_line("[PropertyValueEvaluator]: Error eval! Error code: " + itos(call_err.error));
  3302. memdelete(script_instance);
  3303. return _default_eval(p_text);
  3304. }
  3305. void PropertyValueEvaluator::edit(Object *p_obj) {
  3306. obj = p_obj;
  3307. }
  3308. String PropertyValueEvaluator::_build_script(const String &p_text) {
  3309. String script_text = "tool\nvar this\nfunc set_this(p_this):\n\tthis=p_this\nfunc e():\n\treturn ";
  3310. script_text += p_text.strip_edges();
  3311. script_text += "\n";
  3312. return script_text;
  3313. }
  3314. PropertyValueEvaluator::PropertyValueEvaluator() {
  3315. script_language = ScriptServer::get_language(0); // todo: get script language from editor setting
  3316. }
  3317. PropertyValueEvaluator::~PropertyValueEvaluator() {
  3318. }