property_editor.cpp 106 KB

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