property_editor.cpp 110 KB

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