tree.cpp 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621
  1. /*************************************************************************/
  2. /* tree.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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 "tree.h"
  30. #include "print_string.h"
  31. #include "os/os.h"
  32. #include "os/keyboard.h"
  33. #include "globals.h"
  34. #include "os/input.h"
  35. void TreeItem::move_to_top() {
  36. if (!parent || parent->childs==this)
  37. return; //already on top
  38. TreeItem *prev = get_prev();
  39. prev->next=next;
  40. next=parent->childs;
  41. parent->childs=this;
  42. }
  43. void TreeItem::move_to_bottom() {
  44. if (!parent || !next)
  45. return;
  46. while(next) {
  47. if (parent->childs==this)
  48. parent->childs=next;
  49. TreeItem *n=next;
  50. next=n->next;
  51. n->next=this;
  52. }
  53. }
  54. Size2 TreeItem::Cell::get_icon_size() const {
  55. if (icon.is_null())
  56. return Size2();
  57. if (icon_region==Rect2i())
  58. return icon->get_size();
  59. else
  60. return icon_region.size;
  61. }
  62. void TreeItem::Cell::draw_icon(const RID& p_where, const Point2& p_pos, const Size2& p_size) const{
  63. if (icon.is_null())
  64. return;
  65. Size2i dsize=(p_size==Size2()) ? icon->get_size() : p_size;
  66. if (icon_region==Rect2i()) {
  67. icon->draw_rect_region(p_where,Rect2(p_pos,dsize),Rect2(Point2(),icon->get_size()));
  68. } else {
  69. icon->draw_rect_region(p_where,Rect2(p_pos,dsize),icon_region);
  70. }
  71. }
  72. void TreeItem::_changed_notify(int p_cell) {
  73. tree->item_changed(p_cell,this);
  74. }
  75. void TreeItem::_changed_notify() {
  76. tree->item_changed(-1,this);
  77. }
  78. void TreeItem::_cell_selected(int p_cell) {
  79. tree->item_selected(p_cell,this);
  80. }
  81. void TreeItem::_cell_deselected(int p_cell) {
  82. tree->item_deselected(p_cell,this);
  83. }
  84. /* cell mode */
  85. void TreeItem::set_cell_mode( int p_column, TreeCellMode p_mode ) {
  86. ERR_FAIL_INDEX( p_column, cells.size() );
  87. Cell&c=cells[p_column];
  88. c.mode=p_mode;
  89. c.min=0;
  90. c.max=100;
  91. c.step=1;
  92. c.val=0;
  93. c.checked=false;
  94. c.icon=Ref<Texture>();
  95. c.text="";
  96. c.icon_max_w=0;
  97. _changed_notify(p_column);
  98. }
  99. TreeItem::TreeCellMode TreeItem::get_cell_mode( int p_column ) const {
  100. ERR_FAIL_INDEX_V( p_column, cells.size(), TreeItem::CELL_MODE_STRING );
  101. return cells[p_column].mode;
  102. }
  103. /* check mode */
  104. void TreeItem::set_checked(int p_column,bool p_checked) {
  105. ERR_FAIL_INDEX( p_column, cells.size() );
  106. cells[p_column].checked=p_checked;
  107. _changed_notify(p_column);
  108. }
  109. bool TreeItem::is_checked(int p_column) const {
  110. ERR_FAIL_INDEX_V( p_column, cells.size(), false );
  111. return cells[p_column].checked;
  112. }
  113. void TreeItem::set_text(int p_column,String p_text) {
  114. ERR_FAIL_INDEX( p_column, cells.size() );
  115. cells[p_column].text=p_text;
  116. if (cells[p_column].mode==TreeItem::CELL_MODE_RANGE || cells[p_column].mode==TreeItem::CELL_MODE_RANGE_EXPRESSION) {
  117. cells[p_column].min=0;
  118. cells[p_column].max=p_text.get_slice_count(",");
  119. cells[p_column].step=0;
  120. }
  121. _changed_notify(p_column);
  122. }
  123. String TreeItem::get_text(int p_column) const {
  124. ERR_FAIL_INDEX_V( p_column, cells.size(), "" );
  125. return cells[p_column].text;
  126. }
  127. void TreeItem::set_icon(int p_column,const Ref<Texture>& p_icon) {
  128. ERR_FAIL_INDEX( p_column, cells.size() );
  129. cells[p_column].icon=p_icon;
  130. _changed_notify(p_column);
  131. }
  132. Ref<Texture> TreeItem::get_icon(int p_column) const {
  133. ERR_FAIL_INDEX_V( p_column, cells.size(), Ref<Texture>() );
  134. return cells[p_column].icon;
  135. }
  136. void TreeItem::set_icon_region(int p_column,const Rect2& p_icon_region) {
  137. ERR_FAIL_INDEX( p_column, cells.size() );
  138. cells[p_column].icon_region=p_icon_region;
  139. _changed_notify(p_column);
  140. }
  141. Rect2 TreeItem::get_icon_region(int p_column) const {
  142. ERR_FAIL_INDEX_V( p_column, cells.size(), Rect2() );
  143. return cells[p_column].icon_region;
  144. }
  145. void TreeItem::set_icon_max_width(int p_column,int p_max) {
  146. ERR_FAIL_INDEX( p_column, cells.size() );
  147. cells[p_column].icon_max_w=p_max;
  148. _changed_notify(p_column);
  149. }
  150. int TreeItem::get_icon_max_width(int p_column) const {
  151. ERR_FAIL_INDEX_V( p_column, cells.size(), 0);
  152. return cells[p_column].icon_max_w;
  153. }
  154. /* range works for mode number or mode combo */
  155. void TreeItem::set_range(int p_column,double p_value) {
  156. ERR_FAIL_INDEX( p_column, cells.size() );
  157. if (cells[p_column].step>0)
  158. p_value=Math::stepify( p_value, cells[p_column].step );
  159. if (p_value<cells[p_column].min)
  160. p_value=cells[p_column].min;
  161. if (p_value>cells[p_column].max)
  162. p_value=cells[p_column].max;
  163. cells[p_column].val=p_value;
  164. _changed_notify(p_column);
  165. }
  166. double TreeItem::get_range(int p_column) const {
  167. ERR_FAIL_INDEX_V( p_column, cells.size(), 0 );
  168. return cells[p_column].val;
  169. }
  170. bool TreeItem::is_range_exponential(int p_column) const {
  171. ERR_FAIL_INDEX_V( p_column, cells.size(), false);
  172. return cells[p_column].expr;
  173. }
  174. void TreeItem::set_range_config(int p_column,double p_min,double p_max,double p_step,bool p_exp) {
  175. ERR_FAIL_INDEX( p_column, cells.size() );
  176. cells[p_column].min=p_min;
  177. cells[p_column].max=p_max;
  178. cells[p_column].step=p_step;
  179. cells[p_column].expr=p_exp;
  180. _changed_notify(p_column);
  181. }
  182. void TreeItem::get_range_config(int p_column,double& r_min,double& r_max,double &r_step) const {
  183. ERR_FAIL_INDEX( p_column, cells.size() );
  184. r_min=cells[p_column].min;
  185. r_max=cells[p_column].max;
  186. r_step=cells[p_column].step;
  187. }
  188. void TreeItem::set_metadata(int p_column,const Variant& p_meta) {
  189. ERR_FAIL_INDEX( p_column, cells.size() );
  190. cells[p_column].meta=p_meta;
  191. }
  192. Variant TreeItem::get_metadata(int p_column) const {
  193. ERR_FAIL_INDEX_V( p_column, cells.size(), Variant() );
  194. return cells[p_column].meta;
  195. }
  196. void TreeItem::set_custom_draw(int p_column,Object *p_object,const StringName& p_callback) {
  197. ERR_FAIL_INDEX( p_column, cells.size() );
  198. ERR_FAIL_NULL(p_object);
  199. cells[p_column].custom_draw_obj=p_object->get_instance_ID();
  200. cells[p_column].custom_draw_callback=p_callback;
  201. }
  202. void TreeItem::set_collapsed(bool p_collapsed) {
  203. if (collapsed==p_collapsed)
  204. return;
  205. collapsed=p_collapsed;
  206. TreeItem *ci = tree->selected_item;
  207. if (ci) {
  208. while (ci && ci!=this) {
  209. ci=ci->parent;
  210. }
  211. if (ci) { // collapsing cursor/selectd, move it!
  212. if (tree->select_mode==Tree::SELECT_MULTI) {
  213. tree->selected_item=this;
  214. emit_signal("cell_selected");
  215. } else {
  216. select(tree->selected_col);
  217. }
  218. tree->update();
  219. }
  220. }
  221. _changed_notify();
  222. if (tree)
  223. tree->emit_signal("item_collapsed",this);
  224. }
  225. bool TreeItem::is_collapsed() {
  226. return collapsed;
  227. }
  228. TreeItem *TreeItem::get_next() {
  229. return next;
  230. }
  231. TreeItem *TreeItem::get_prev() {
  232. if (!parent || parent->childs==this)
  233. return NULL;
  234. TreeItem *prev = parent->childs;
  235. while(prev && prev->next!=this)
  236. prev=prev->next;
  237. return prev;
  238. }
  239. TreeItem *TreeItem::get_parent() {
  240. return parent;
  241. }
  242. TreeItem *TreeItem::get_children() {
  243. return childs;
  244. }
  245. TreeItem *TreeItem::get_prev_visible() {
  246. TreeItem *current=this;
  247. TreeItem *prev = current->get_prev();
  248. if (!prev) {
  249. current=current->parent;
  250. if (!current || (current==tree->root && tree->hide_root))
  251. return NULL;
  252. } else {
  253. current=prev;
  254. while( !current->collapsed && current->childs ) {
  255. //go to the very end
  256. current = current->childs;
  257. while (current->next)
  258. current=current->next;
  259. }
  260. }
  261. return current;
  262. }
  263. TreeItem *TreeItem::get_next_visible() {
  264. TreeItem *current=this;
  265. if (!current->collapsed && current->childs) {
  266. current=current->childs;
  267. } else if (current->next) {
  268. current=current->next;
  269. } else {
  270. while(current && !current->next) {
  271. current=current->parent;
  272. }
  273. if (current==NULL)
  274. return NULL;
  275. else
  276. current=current->next;
  277. }
  278. return current;
  279. }
  280. void TreeItem::remove_child(TreeItem *p_item) {
  281. ERR_FAIL_NULL(p_item);
  282. TreeItem **c=&childs;
  283. while (*c) {
  284. if ( (*c) == p_item ) {
  285. TreeItem *aux = *c;
  286. *c=(*c)->next;
  287. aux->parent = NULL;
  288. return;
  289. }
  290. c=&(*c)->next;
  291. }
  292. ERR_FAIL();
  293. }
  294. void TreeItem::set_selectable(int p_column,bool p_selectable) {
  295. ERR_FAIL_INDEX( p_column, cells.size() );
  296. cells[p_column].selectable=p_selectable;
  297. }
  298. bool TreeItem::is_selectable(int p_column) const {
  299. ERR_FAIL_INDEX_V( p_column, cells.size(), false );
  300. return cells[p_column].selectable;
  301. }
  302. bool TreeItem::is_selected(int p_column) {
  303. ERR_FAIL_INDEX_V( p_column, cells.size(), false );
  304. return cells[p_column].selectable && cells[p_column].selected;
  305. }
  306. void TreeItem::set_as_cursor(int p_column) {
  307. ERR_FAIL_INDEX( p_column, cells.size() );
  308. if (!tree)
  309. return;
  310. if (tree->select_mode!=Tree::SELECT_MULTI)
  311. return;
  312. tree->selected_item=this;
  313. tree->selected_col=p_column;
  314. tree->update();
  315. }
  316. void TreeItem::select(int p_column) {
  317. ERR_FAIL_INDEX( p_column, cells.size() );
  318. _cell_selected(p_column);
  319. }
  320. void TreeItem::deselect(int p_column) {
  321. ERR_FAIL_INDEX( p_column, cells.size() );
  322. _cell_deselected(p_column);
  323. }
  324. void TreeItem::add_button(int p_column, const Ref<Texture>& p_button, int p_id, bool p_disabled) {
  325. ERR_FAIL_INDEX( p_column, cells.size() );
  326. ERR_FAIL_COND(!p_button.is_valid());
  327. TreeItem::Cell::Button button;
  328. button.texture=p_button;
  329. if (p_id<0)
  330. p_id=cells[p_column].buttons.size();
  331. button.id=p_id;
  332. button.disabled=p_disabled;
  333. cells[p_column].buttons.push_back(button);
  334. _changed_notify(p_column);
  335. }
  336. int TreeItem::get_button_count(int p_column) const {
  337. ERR_FAIL_INDEX_V( p_column, cells.size(), -1 );
  338. return cells[p_column].buttons.size();
  339. }
  340. Ref<Texture> TreeItem::get_button(int p_column,int p_idx) const {
  341. ERR_FAIL_INDEX_V( p_column, cells.size(), Ref<Texture>() );
  342. ERR_FAIL_INDEX_V( p_idx, cells[p_column].buttons.size(), Ref<Texture>() );
  343. return cells[p_column].buttons[p_idx].texture;
  344. }
  345. int TreeItem::get_button_id(int p_column,int p_idx) const {
  346. ERR_FAIL_INDEX_V( p_column, cells.size(), -1 );
  347. ERR_FAIL_INDEX_V( p_idx, cells[p_column].buttons.size(), -1 );
  348. return cells[p_column].buttons[p_idx].id;
  349. }
  350. void TreeItem::erase_button(int p_column,int p_idx) {
  351. ERR_FAIL_INDEX( p_column, cells.size() );
  352. ERR_FAIL_INDEX( p_idx, cells[p_column].buttons.size() );
  353. cells[p_column].buttons.remove(p_idx);
  354. _changed_notify(p_column);
  355. }
  356. int TreeItem::get_button_by_id(int p_column,int p_id) const {
  357. ERR_FAIL_INDEX_V( p_column, cells.size(),-1 );
  358. for(int i=0;i<cells[p_column].buttons.size();i++) {
  359. if (cells[p_column].buttons[i].id==p_id)
  360. return i;
  361. }
  362. return -1;
  363. }
  364. bool TreeItem::is_button_disabled(int p_column, int p_idx) const {
  365. ERR_FAIL_INDEX_V( p_column, cells.size(), false );
  366. ERR_FAIL_INDEX_V( p_idx, cells[p_column].buttons.size(), false );
  367. return cells[p_column].buttons[p_idx].disabled;
  368. }
  369. void TreeItem::set_button(int p_column,int p_idx,const Ref<Texture>& p_button){
  370. ERR_FAIL_COND( p_button.is_null() );
  371. ERR_FAIL_INDEX( p_column, cells.size() );
  372. ERR_FAIL_INDEX( p_idx, cells[p_column].buttons.size() );
  373. cells[p_column].buttons[p_idx].texture=p_button;
  374. _changed_notify(p_column);
  375. }
  376. void TreeItem::set_editable(int p_column,bool p_editable) {
  377. ERR_FAIL_INDEX( p_column, cells.size() );
  378. cells[p_column].editable=p_editable;
  379. _changed_notify(p_column);
  380. }
  381. bool TreeItem::is_editable(int p_column) {
  382. ERR_FAIL_INDEX_V( p_column, cells.size(), false );
  383. return cells[p_column].editable;
  384. }
  385. void TreeItem::set_custom_color(int p_column,const Color& p_color) {
  386. ERR_FAIL_INDEX( p_column, cells.size() );
  387. cells[p_column].custom_color=true;
  388. cells[p_column].color=p_color;
  389. _changed_notify(p_column);
  390. }
  391. Color TreeItem::get_custom_color(int p_column) const {
  392. ERR_FAIL_INDEX_V( p_column, cells.size(), Color() );
  393. if (!cells[p_column].custom_color)
  394. return Color();
  395. return cells[p_column].color;
  396. }
  397. void TreeItem::clear_custom_color(int p_column) {
  398. ERR_FAIL_INDEX( p_column, cells.size() );
  399. cells[p_column].custom_color=false;
  400. cells[p_column].color=Color();;
  401. _changed_notify(p_column);
  402. }
  403. void TreeItem::set_tooltip(int p_column, const String& p_tooltip) {
  404. ERR_FAIL_INDEX( p_column, cells.size() );
  405. cells[p_column].tooltip=p_tooltip;
  406. }
  407. String TreeItem::get_tooltip(int p_column) const{
  408. ERR_FAIL_INDEX_V( p_column, cells.size(), "" );
  409. return cells[p_column].tooltip;
  410. }
  411. void TreeItem::set_custom_bg_color(int p_column,const Color& p_color,bool p_bg_outline) {
  412. ERR_FAIL_INDEX( p_column, cells.size() );
  413. cells[p_column].custom_bg_color=true;
  414. cells[p_column].custom_bg_outline=p_bg_outline;
  415. cells[p_column].bg_color=p_color;
  416. _changed_notify(p_column);
  417. }
  418. void TreeItem::clear_custom_bg_color(int p_column) {
  419. ERR_FAIL_INDEX( p_column, cells.size() );
  420. cells[p_column].custom_bg_color=false;
  421. cells[p_column].bg_color=Color();;
  422. _changed_notify(p_column);
  423. }
  424. Color TreeItem::get_custom_bg_color(int p_column) const {
  425. ERR_FAIL_INDEX_V( p_column, cells.size(), Color() );
  426. if (!cells[p_column].custom_bg_color)
  427. return Color();
  428. return cells[p_column].bg_color;
  429. }
  430. void TreeItem::_bind_methods() {
  431. ObjectTypeDB::bind_method(_MD("set_cell_mode","column","mode"),&TreeItem::set_cell_mode);
  432. ObjectTypeDB::bind_method(_MD("get_cell_mode","column"),&TreeItem::get_cell_mode);
  433. ObjectTypeDB::bind_method(_MD("set_checked","column","checked"),&TreeItem::set_checked);
  434. ObjectTypeDB::bind_method(_MD("is_checked","column"),&TreeItem::is_checked);
  435. ObjectTypeDB::bind_method(_MD("set_text","column","text"),&TreeItem::set_text);
  436. ObjectTypeDB::bind_method(_MD("get_text","column"),&TreeItem::get_text);
  437. ObjectTypeDB::bind_method(_MD("set_icon","column","texture:Texture"),&TreeItem::set_icon);
  438. ObjectTypeDB::bind_method(_MD("get_icon:Texture","column"),&TreeItem::get_icon);
  439. ObjectTypeDB::bind_method(_MD("set_icon_region","column","region"),&TreeItem::set_icon_region);
  440. ObjectTypeDB::bind_method(_MD("get_icon_region","column"),&TreeItem::get_icon_region);
  441. ObjectTypeDB::bind_method(_MD("set_icon_max_width","column","width"),&TreeItem::set_icon_max_width);
  442. ObjectTypeDB::bind_method(_MD("get_icon_max_width","column"),&TreeItem::get_icon_max_width);
  443. ObjectTypeDB::bind_method(_MD("set_range","column","value"),&TreeItem::set_range);
  444. ObjectTypeDB::bind_method(_MD("get_range","column"),&TreeItem::get_range);
  445. ObjectTypeDB::bind_method(_MD("set_range_config","column","min","max","step","expr"),&TreeItem::set_range_config,DEFVAL(false));
  446. ObjectTypeDB::bind_method(_MD("get_range_config","column"),&TreeItem::_get_range_config);
  447. ObjectTypeDB::bind_method(_MD("set_metadata","column","meta"),&TreeItem::set_metadata);
  448. ObjectTypeDB::bind_method(_MD("get_metadata","column"),&TreeItem::get_metadata);
  449. ObjectTypeDB::bind_method(_MD("set_custom_draw","column","object","callback"),&TreeItem::set_custom_draw);
  450. ObjectTypeDB::bind_method(_MD("set_collapsed","enable"),&TreeItem::set_collapsed);
  451. ObjectTypeDB::bind_method(_MD("is_collapsed"),&TreeItem::is_collapsed);
  452. ObjectTypeDB::bind_method(_MD("get_next:TreeItem"),&TreeItem::get_next);
  453. ObjectTypeDB::bind_method(_MD("get_prev:TreeItem"),&TreeItem::get_prev);
  454. ObjectTypeDB::bind_method(_MD("get_parent:TreeItem"),&TreeItem::get_parent);
  455. ObjectTypeDB::bind_method(_MD("get_children:TreeItem"),&TreeItem::get_children);
  456. ObjectTypeDB::bind_method(_MD("get_next_visible:TreeItem"),&TreeItem::get_next_visible);
  457. ObjectTypeDB::bind_method(_MD("get_prev_visible:TreeItem"),&TreeItem::get_prev_visible);
  458. ObjectTypeDB::bind_method(_MD("remove_child:TreeItem","child"),&TreeItem::_remove_child);
  459. ObjectTypeDB::bind_method(_MD("set_selectable","column","selectable"),&TreeItem::set_selectable);
  460. ObjectTypeDB::bind_method(_MD("is_selectable","column"),&TreeItem::is_selectable);
  461. ObjectTypeDB::bind_method(_MD("is_selected","column"),&TreeItem::is_selected);
  462. ObjectTypeDB::bind_method(_MD("select","column"),&TreeItem::select);
  463. ObjectTypeDB::bind_method(_MD("deselect","column"),&TreeItem::deselect);
  464. ObjectTypeDB::bind_method(_MD("set_editable","column","enabled"),&TreeItem::set_editable);
  465. ObjectTypeDB::bind_method(_MD("is_editable","column"),&TreeItem::is_editable);
  466. ObjectTypeDB::bind_method(_MD("set_custom_color","column","color"),&TreeItem::set_custom_color);
  467. ObjectTypeDB::bind_method(_MD("clear_custom_color","column"),&TreeItem::clear_custom_color);
  468. ObjectTypeDB::bind_method(_MD("set_custom_bg_color","column","color","just_outline"),&TreeItem::set_custom_bg_color,DEFVAL(false));
  469. ObjectTypeDB::bind_method(_MD("clear_custom_bg_color","column"),&TreeItem::clear_custom_bg_color);
  470. ObjectTypeDB::bind_method(_MD("get_custom_bg_color","column"),&TreeItem::get_custom_bg_color);
  471. ObjectTypeDB::bind_method(_MD("add_button","column","button:Texture","button_idx","disabled"),&TreeItem::add_button,DEFVAL(-1),DEFVAL(false));
  472. ObjectTypeDB::bind_method(_MD("get_button_count","column"),&TreeItem::get_button_count);
  473. ObjectTypeDB::bind_method(_MD("get_button:Texture","column","button_idx"),&TreeItem::get_button);
  474. ObjectTypeDB::bind_method(_MD("erase_button","column","button_idx"),&TreeItem::erase_button);
  475. ObjectTypeDB::bind_method(_MD("is_button_disabled","column","button_idx"),&TreeItem::is_button_disabled);
  476. ObjectTypeDB::bind_method(_MD("set_tooltip","column","tooltip"),&TreeItem::set_tooltip);
  477. ObjectTypeDB::bind_method(_MD("get_tooltip","column"),&TreeItem::get_tooltip);
  478. ObjectTypeDB::bind_method(_MD("move_to_top"),&TreeItem::move_to_top);
  479. ObjectTypeDB::bind_method(_MD("move_to_bottom"),&TreeItem::move_to_bottom);
  480. BIND_CONSTANT( CELL_MODE_STRING );
  481. BIND_CONSTANT( CELL_MODE_CHECK );
  482. BIND_CONSTANT( CELL_MODE_RANGE );
  483. BIND_CONSTANT( CELL_MODE_RANGE_EXPRESSION );
  484. BIND_CONSTANT( CELL_MODE_ICON );
  485. BIND_CONSTANT( CELL_MODE_CUSTOM );
  486. }
  487. void TreeItem::clear_children() {
  488. TreeItem *c=childs;
  489. while (c) {
  490. TreeItem *aux=c;
  491. c=c->get_next();
  492. aux->parent=0; // so it wont try to recursively autoremove from me in here
  493. memdelete( aux );
  494. }
  495. childs = 0;
  496. };
  497. TreeItem::TreeItem(Tree *p_tree) {
  498. tree=p_tree;
  499. collapsed=false;
  500. parent=0; // parent item
  501. next=0; // next in list
  502. childs=0; //child items
  503. }
  504. TreeItem::~TreeItem() {
  505. clear_children();
  506. if (parent)
  507. parent->remove_child(this);
  508. if (tree && tree->root==this) {
  509. tree->root=0;
  510. }
  511. if (tree && tree->popup_edited_item==this) {
  512. tree->popup_edited_item=NULL;
  513. tree->pressing_for_editor=false;
  514. }
  515. if (tree && tree->selected_item==this)
  516. tree->selected_item=NULL;
  517. if (tree && tree->drop_mode_over==this)
  518. tree->drop_mode_over=NULL;
  519. if (tree && tree->single_select_defer==this)
  520. tree->single_select_defer=NULL;
  521. if (tree && tree->edited_item==this) {
  522. tree->edited_item=NULL;
  523. tree->pressing_for_editor=false;
  524. }
  525. }
  526. /**********************************************/
  527. /**********************************************/
  528. /**********************************************/
  529. /**********************************************/
  530. /**********************************************/
  531. /**********************************************/
  532. void Tree::update_cache() {
  533. cache.font = get_font("font");
  534. cache.tb_font = get_font("title_button_font");
  535. cache.bg = get_stylebox("bg");
  536. cache.selected= get_stylebox("selected");
  537. cache.selected_focus= get_stylebox("selected_focus");
  538. cache.cursor = get_stylebox("cursor");
  539. cache.cursor_unfocus = get_stylebox("cursor_unfocused");
  540. cache.button_pressed= get_stylebox("button_pressed");
  541. cache.checked=get_icon("checked");
  542. cache.unchecked=get_icon("unchecked");
  543. cache.arrow_collapsed=get_icon("arrow_collapsed");
  544. cache.arrow =get_icon("arrow");
  545. cache.select_arrow =get_icon("select_arrow");
  546. cache.updown=get_icon("updown");
  547. cache.font_color=get_color("font_color");
  548. cache.font_color_selected=get_color("font_color_selected");
  549. cache.guide_color=get_color("guide_color");
  550. cache.drop_position_color=get_color("drop_position_color");
  551. cache.hseparation=get_constant("hseparation");
  552. cache.vseparation=get_constant("vseparation");
  553. cache.item_margin=get_constant("item_margin");
  554. cache.button_margin=get_constant("button_margin");
  555. cache.guide_width=get_constant("guide_width");
  556. cache.title_button = get_stylebox("title_button_normal");
  557. cache.title_button_pressed = get_stylebox("title_button_pressed");
  558. cache.title_button_hover = get_stylebox("title_button_hover");
  559. cache.title_button_color = get_color("title_button_color");
  560. v_scroll->set_custom_step(cache.font->get_height());
  561. }
  562. int Tree::compute_item_height(TreeItem *p_item) const {
  563. if (p_item==root && hide_root)
  564. return 0;
  565. int height=cache.font->get_height();
  566. for (int i=0;i<columns.size();i++) {
  567. for(int j=0;j<p_item->cells[i].buttons.size();j++) {
  568. Size2i s;// = cache.button_pressed->get_minimum_size();
  569. s+= p_item->cells[i].buttons[j].texture->get_size();
  570. if (s.height>height)
  571. height=s.height;
  572. }
  573. switch(p_item->cells[i].mode) {
  574. case TreeItem::CELL_MODE_CHECK: {
  575. int check_icon_h = cache.checked->get_height();
  576. if (height<check_icon_h)
  577. height=check_icon_h;
  578. }
  579. case TreeItem::CELL_MODE_STRING:
  580. case TreeItem::CELL_MODE_CUSTOM:
  581. case TreeItem::CELL_MODE_ICON: {
  582. Ref<Texture> icon = p_item->cells[i].icon;
  583. if (!icon.is_null()) {
  584. Size2i s = p_item->cells[i].get_icon_size();
  585. if (p_item->cells[i].icon_max_w>0 && s.width > p_item->cells[i].icon_max_w ) {
  586. s.height=s.height * p_item->cells[i].icon_max_w / s.width;
  587. }
  588. if (s.height > height )
  589. height=s.height;
  590. }
  591. } break;
  592. default: {}
  593. }
  594. }
  595. height += cache.vseparation;
  596. return height;
  597. }
  598. int Tree::get_item_height(TreeItem *p_item) const {
  599. int height=compute_item_height(p_item);
  600. height+=cache.vseparation;
  601. if (!p_item->collapsed) { /* if not collapsed, check the childs */
  602. TreeItem *c=p_item->childs;
  603. while (c) {
  604. height += get_item_height( c );
  605. c=c->next;
  606. }
  607. }
  608. return height;
  609. }
  610. void Tree::draw_item_rect(const TreeItem::Cell& p_cell,const Rect2i& p_rect,const Color& p_color) {
  611. Rect2i rect=p_rect;
  612. RID ci = get_canvas_item();
  613. if (!p_cell.icon.is_null()) {
  614. Size2i bmsize = p_cell.get_icon_size();
  615. if (p_cell.icon_max_w>0 && bmsize.width > p_cell.icon_max_w) {
  616. bmsize.height = bmsize.height * p_cell.icon_max_w / bmsize.width;
  617. bmsize.width=p_cell.icon_max_w;
  618. }
  619. p_cell.draw_icon(ci,rect.pos + Size2i(0,Math::floor((rect.size.y-bmsize.y)/2)),bmsize);
  620. rect.pos.x+=bmsize.x+cache.hseparation;
  621. rect.size.x-=bmsize.x+cache.hseparation;
  622. }
  623. // if (p_tool)
  624. // rect.size.x-=Math::floor(rect.size.y/2);
  625. Ref<Font> font = cache.font;
  626. rect.pos.y+=Math::floor((rect.size.y-font->get_height())/2.0) +font->get_ascent();
  627. font->draw(ci,rect.pos,p_cell.text,p_color,rect.size.x);
  628. }
  629. #if 0
  630. void Tree::draw_item_text(String p_text,const Ref<Texture>& p_icon,int p_icon_max_w,bool p_tool,Rect2i p_rect,const Color& p_color) {
  631. RID ci = get_canvas_item();
  632. if (!p_icon.is_null()) {
  633. Size2i bmsize = p_icon->get_size();
  634. if (p_icon_max_w>0 && bmsize.width > p_icon_max_w) {
  635. bmsize.height = bmsize.height * p_icon_max_w / bmsize.width;
  636. bmsize.width=p_icon_max_w;
  637. }
  638. draw_texture_rect(p_icon,Rect2(p_rect.pos + Size2i(0,Math::floor((p_rect.size.y-bmsize.y)/2)),bmsize));
  639. p_rect.pos.x+=bmsize.x+cache.hseparation;
  640. p_rect.size.x-=bmsize.x+cache.hseparation;
  641. }
  642. if (p_tool)
  643. p_rect.size.x-=Math::floor(p_rect.size.y/2);
  644. Ref<Font> font = cache.font;
  645. p_rect.pos.y+=Math::floor((p_rect.size.y-font->get_height())/2.0) +font->get_ascent();
  646. font->draw(ci,p_rect.pos,p_text,p_color,p_rect.size.x);
  647. }
  648. #endif
  649. int Tree::draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& p_draw_size,TreeItem *p_item) {
  650. if (p_pos.y-cache.offset.y > (p_draw_size.height))
  651. return -1; //draw no more!
  652. RID ci = get_canvas_item();
  653. int htotal=0;
  654. int label_h=compute_item_height( p_item );
  655. /* Calculate height of the label part */
  656. label_h+=cache.vseparation;
  657. /* Draw label, if height fits */
  658. Point2i guide_from;
  659. bool skip=(p_item==root && hide_root);
  660. // printf("skip (%p == %p && %i) %i\n",p_item,root,hide_root,skip);
  661. if (!skip && (p_pos.y+label_h-cache.offset.y)>0) {
  662. // printf("entering\n");
  663. int height=label_h;
  664. Point2i guide_space=Point2i( cache.guide_width , height );
  665. if (!hide_folding && p_item->childs) { //has childs, draw the guide box
  666. Ref<Texture> arrow;
  667. if (p_item->collapsed) {
  668. arrow=cache.arrow_collapsed;
  669. } else {
  670. arrow=cache.arrow;
  671. }
  672. arrow->draw( ci , p_pos+p_draw_ofs+Point2i(0,(label_h-arrow->get_height())/2)-cache.offset);
  673. }
  674. //draw separation.
  675. // if (p_item->get_parent()!=root || !hide_root)
  676. Ref<Font> font = cache.font;
  677. int font_ascent=font->get_ascent();
  678. int ofs = p_pos.x + (hide_folding?cache.hseparation:cache.item_margin);
  679. for (int i=0;i<columns.size();i++) {
  680. int w = get_column_width(i);
  681. if (i==0) {
  682. w-=ofs;
  683. if (w<=0) {
  684. ofs=get_column_width(0);
  685. continue;
  686. }
  687. } else {
  688. ofs+=cache.hseparation;
  689. w-=cache.hseparation;
  690. }
  691. int bw=0;
  692. for(int j=p_item->cells[i].buttons.size()-1;j>=0;j--) {
  693. Ref<Texture> b=p_item->cells[i].buttons[j].texture;
  694. Size2 s = b->get_size() + cache.button_pressed->get_minimum_size();
  695. Point2i o = Point2i( ofs+w-s.width, p_pos.y )-cache.offset+p_draw_ofs;
  696. if (cache.click_type==Cache::CLICK_BUTTON && cache.click_item==p_item && cache.click_column==i && !p_item->cells[i].buttons[j].disabled) {
  697. //being pressed
  698. cache.button_pressed->draw(get_canvas_item(),Rect2(o,s));
  699. }
  700. o.y+=(label_h-s.height)/2;
  701. o+=cache.button_pressed->get_offset();
  702. b->draw(ci,o,p_item->cells[i].buttons[j].disabled?Color(1,1,1,0.5):Color(1,1,1,1));
  703. w-=s.width+cache.button_margin;
  704. bw+=s.width+cache.button_margin;
  705. }
  706. Rect2i item_rect = Rect2i( Point2i( ofs, p_pos.y )-cache.offset+p_draw_ofs, Size2i( w, label_h ));
  707. Rect2i cell_rect=item_rect;
  708. if (i!=0) {
  709. cell_rect.pos.x-=cache.hseparation;
  710. cell_rect.size.x+=cache.hseparation;
  711. }
  712. VisualServer::get_singleton()->canvas_item_add_line(ci,Point2i(cell_rect.pos.x,cell_rect.pos.y+cell_rect.size.height),cell_rect.pos+cell_rect.size,cache.guide_color,1);
  713. if (i==0) {
  714. if (p_item->cells[0].selected && select_mode==SELECT_ROW) {
  715. Rect2i row_rect = Rect2i( Point2i( cache.bg->get_margin(MARGIN_LEFT), item_rect.pos.y), Size2i( get_size().width-cache.bg->get_minimum_size().width, item_rect.size.y ));
  716. //Rect2 r = Rect2i(row_rect.pos,row_rect.size);
  717. //r.grow(cache.selected->get_margin(MARGIN_LEFT));
  718. if (has_focus())
  719. cache.selected_focus->draw(ci,row_rect );
  720. else
  721. cache.selected->draw(ci,row_rect );
  722. }
  723. }
  724. if (p_item->cells[i].selected && select_mode!=SELECT_ROW) {
  725. Rect2i r(item_rect.pos,item_rect.size);
  726. //r.grow(cache.selected->get_margin(MARGIN_LEFT));
  727. if (has_focus())
  728. cache.selected_focus->draw(ci,r );
  729. else
  730. cache.selected->draw(ci,r );
  731. }
  732. if (p_item->cells[i].custom_bg_color) {
  733. Rect2 r=cell_rect;
  734. r.pos.x-=cache.hseparation;
  735. r.size.x+=cache.hseparation;
  736. if (p_item->cells[i].custom_bg_outline) {
  737. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x,r.pos.y,r.size.x,1),p_item->cells[i].bg_color);
  738. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x,r.pos.y+r.size.y-1,r.size.x,1),p_item->cells[i].bg_color);
  739. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x,r.pos.y,1,r.size.y),p_item->cells[i].bg_color);
  740. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x+r.size.x-1,r.pos.y,1,r.size.y),p_item->cells[i].bg_color);
  741. } else {
  742. VisualServer::get_singleton()->canvas_item_add_rect(ci,r,p_item->cells[i].bg_color);
  743. }
  744. }
  745. if (drop_mode_flags && drop_mode_over==p_item) {
  746. Rect2 r=cell_rect;
  747. if (drop_mode_section==-1 || drop_mode_section==0) {
  748. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x,r.pos.y,r.size.x,1),cache.drop_position_color);
  749. }
  750. if (drop_mode_section==0) {
  751. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x,r.pos.y,1,r.size.y),cache.drop_position_color);
  752. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x+r.size.x-1,r.pos.y,1,r.size.y),cache.drop_position_color);
  753. }
  754. if (drop_mode_section==1 || drop_mode_section==0) {
  755. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x,r.pos.y+r.size.y,r.size.x,1),cache.drop_position_color);
  756. }
  757. }
  758. Color col=p_item->cells[i].custom_color?p_item->cells[i].color:get_color( p_item->cells[i].selected?"font_color_selected":"font_color");
  759. Point2i text_pos=item_rect.pos;
  760. text_pos.y+=Math::floor((item_rect.size.y-font->get_height())/2) + font_ascent;
  761. switch (p_item->cells[i].mode) {
  762. case TreeItem::CELL_MODE_STRING: {
  763. draw_item_rect(p_item->cells[i],item_rect,col);
  764. } break;
  765. case TreeItem::CELL_MODE_CHECK: {
  766. Ref<Texture> checked = cache.checked;
  767. Ref<Texture> unchecked = cache.unchecked;
  768. Point2i check_ofs=item_rect.pos;
  769. check_ofs.y+=Math::floor((item_rect.size.y-checked->get_height())/2);
  770. if (p_item->cells[i].checked) {
  771. checked->draw( ci, check_ofs );
  772. } else {
  773. unchecked->draw( ci, check_ofs );
  774. }
  775. int check_w = checked->get_width()+cache.hseparation;
  776. text_pos.x+=check_w;
  777. item_rect.size.x-=check_w;
  778. item_rect.pos.x+=check_w;
  779. draw_item_rect(p_item->cells[i],item_rect,col);
  780. //font->draw( ci, text_pos, p_item->cells[i].text, col,item_rect.size.x-check_w );
  781. } break;
  782. case TreeItem::CELL_MODE_RANGE:
  783. case TreeItem::CELL_MODE_RANGE_EXPRESSION: {
  784. if (p_item->cells[i].text!="") {
  785. if (!p_item->cells[i].editable)
  786. break;
  787. int option = (int)p_item->cells[i].val;
  788. String s = p_item->cells[i].text;
  789. s=s.get_slicec(',',option);
  790. Ref<Texture> downarrow = cache.select_arrow;
  791. font->draw(ci, text_pos, s, col,item_rect.size.x-downarrow->get_width() );
  792. //?
  793. Point2i arrow_pos=item_rect.pos;
  794. arrow_pos.x+=item_rect.size.x-downarrow->get_width();
  795. arrow_pos.y+=Math::floor(((item_rect.size.y-downarrow->get_height()))/2.0);
  796. downarrow->draw( ci, arrow_pos );
  797. } else {
  798. Ref<Texture> updown = cache.updown;
  799. String valtext = String::num( p_item->cells[i].val, Math::decimals( p_item->cells[i].step ) );
  800. font->draw( ci, text_pos, valtext, col, item_rect.size.x-updown->get_width());
  801. if (!p_item->cells[i].editable)
  802. break;
  803. Point2i updown_pos=item_rect.pos;
  804. updown_pos.x+=item_rect.size.x-updown->get_width();
  805. updown_pos.y+=Math::floor(((item_rect.size.y-updown->get_height()))/2.0);
  806. updown->draw( ci, updown_pos );
  807. }
  808. } break;
  809. case TreeItem::CELL_MODE_ICON: {
  810. if (p_item->cells[i].icon.is_null())
  811. break;
  812. Size2i icon_size = p_item->cells[i].get_icon_size();
  813. if (p_item->cells[i].icon_max_w>0 && icon_size.width >p_item->cells[i].icon_max_w) {
  814. icon_size.height = icon_size.height * p_item->cells[i].icon_max_w / icon_size.width;
  815. icon_size.width=p_item->cells[i].icon_max_w;
  816. }
  817. Point2i icon_ofs = (item_rect.size-icon_size)/2;
  818. icon_ofs+=item_rect.pos;
  819. draw_texture_rect(p_item->cells[i].icon,Rect2(icon_ofs,icon_size));;
  820. //p_item->cells[i].icon->draw(ci, icon_ofs);
  821. } break;
  822. case TreeItem::CELL_MODE_CUSTOM: {
  823. // int option = (int)p_item->cells[i].val;
  824. if (p_item->cells[i].custom_draw_obj) {
  825. Object* cdo = ObjectDB::get_instance(p_item->cells[i].custom_draw_obj);
  826. if (cdo)
  827. cdo->call(p_item->cells[i].custom_draw_callback,p_item,Rect2(item_rect));
  828. }
  829. if (!p_item->cells[i].editable) {
  830. draw_item_rect(p_item->cells[i],item_rect,col);
  831. break;
  832. }
  833. Ref<Texture> downarrow = cache.select_arrow;
  834. Rect2i ir=item_rect;
  835. ir.size.width-=downarrow->get_width();
  836. draw_item_rect(p_item->cells[i],ir,col);
  837. Point2i arrow_pos=item_rect.pos;
  838. arrow_pos.x+=item_rect.size.x-downarrow->get_width();
  839. arrow_pos.y+=Math::floor(((item_rect.size.y-downarrow->get_height()))/2.0);
  840. downarrow->draw( ci, arrow_pos );
  841. } break;
  842. }
  843. if (i==0) {
  844. ofs=get_column_width(0);
  845. } else {
  846. ofs+=w+bw;
  847. }
  848. if (select_mode==SELECT_MULTI && selected_item==p_item && selected_col==i) {
  849. if (has_focus())
  850. cache.cursor->draw(ci,cell_rect);
  851. else
  852. cache.cursor_unfocus->draw(ci,cell_rect);
  853. }
  854. }
  855. //separator
  856. //get_painter()->draw_fill_rect( Point2i(0,pos.y),Size2i(get_size().width,1),color( COLOR_TREE_GRID) );
  857. //pos=p_pos; //reset pos
  858. }
  859. Point2 children_pos=p_pos;
  860. if (!skip) {
  861. children_pos.x+=cache.item_margin;
  862. htotal+=label_h;
  863. children_pos.y+=htotal;
  864. }
  865. if (!p_item->collapsed) { /* if not collapsed, check the childs */
  866. TreeItem *c=p_item->childs;
  867. while (c) {
  868. int child_h=draw_item(children_pos, p_draw_ofs, p_draw_size, c );
  869. if (child_h<0)
  870. return -1; // break, stop drawing, no need to anymore
  871. htotal+=child_h;
  872. children_pos.y+=child_h;
  873. c=c->next;
  874. }
  875. }
  876. return htotal;
  877. }
  878. int Tree::_count_selected_items(TreeItem* p_from) const {
  879. int count=0;
  880. for(int i=0;i<columns.size();i++) {
  881. if (p_from->is_selected(i))
  882. count++;
  883. }
  884. if (p_from->get_children()) {
  885. count+=_count_selected_items(p_from->get_children());
  886. }
  887. if (p_from->get_next()) {
  888. count+=_count_selected_items(p_from->get_next());
  889. }
  890. return count;
  891. }
  892. void Tree::select_single_item(TreeItem *p_selected,TreeItem *p_current,int p_col,TreeItem *p_prev,bool *r_in_range) {
  893. TreeItem::Cell &selected_cell=p_selected->cells[p_col];
  894. bool switched=false;
  895. if (r_in_range && !*r_in_range && (p_current==p_selected || p_current==p_prev)) {
  896. *r_in_range=true;
  897. switched=true;
  898. }
  899. for (int i=0;i<columns.size();i++) {
  900. TreeItem::Cell &c=p_current->cells[i];
  901. if (!c.selectable)
  902. continue;
  903. if (select_mode==SELECT_ROW) {
  904. if (p_selected==p_current) {
  905. if (!c.selected) {
  906. c.selected=true;
  907. selected_item=p_selected;
  908. selected_col=0;
  909. selected_item=p_selected;
  910. emit_signal("item_selected");
  911. //if (p_col==i)
  912. // p_current->selected_signal.call(p_col);
  913. }
  914. } else {
  915. if (c.selected) {
  916. c.selected=false;
  917. //p_current->deselected_signal.call(p_col);
  918. }
  919. }
  920. } else if (select_mode==SELECT_SINGLE || select_mode==SELECT_MULTI) {
  921. if (!r_in_range && &selected_cell==&c) {
  922. if (!selected_cell.selected) {
  923. selected_cell.selected=true;
  924. selected_item=p_selected;
  925. selected_col=i;
  926. emit_signal("cell_selected");
  927. if (select_mode==SELECT_MULTI)
  928. emit_signal("multi_selected",p_current,i,true);
  929. } else if (select_mode==SELECT_MULTI && (selected_item!=p_selected || selected_col!=i)) {
  930. selected_item=p_selected;
  931. selected_col=i;
  932. emit_signal("cell_selected");
  933. }
  934. } else {
  935. if (r_in_range && *r_in_range) {
  936. if (!c.selected && c.selectable) {
  937. c.selected=true;
  938. emit_signal("multi_selected",p_current,i,true);
  939. }
  940. } else if (!r_in_range){
  941. if (select_mode==SELECT_MULTI && c.selected)
  942. emit_signal("multi_selected",p_current,i,false);
  943. c.selected=false;
  944. }
  945. //p_current->deselected_signal.call(p_col);
  946. }
  947. }
  948. }
  949. if (!switched && r_in_range && *r_in_range && (p_current==p_selected || p_current==p_prev)) {
  950. *r_in_range=false;
  951. }
  952. TreeItem *c=p_current->childs;
  953. while (c) {
  954. select_single_item(p_selected,c,p_col,p_prev,r_in_range);
  955. c=c->next;
  956. }
  957. }
  958. Rect2 Tree::search_item_rect(TreeItem *p_from, TreeItem *p_item) {
  959. return Rect2();
  960. }
  961. void Tree::_range_click_timeout() {
  962. if (range_item_last && !range_drag_enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
  963. Point2 pos = get_local_mouse_pos()-cache.bg->get_offset();
  964. if (show_column_titles) {
  965. pos.y-=_get_title_button_height();
  966. if (pos.y<0) {
  967. range_click_timer->stop();
  968. return;
  969. }
  970. }
  971. click_handled=false;
  972. InputModifierState mod = InputModifierState(); // should be irrelevant..
  973. blocked++;
  974. propagate_mouse_event(pos+cache.offset, 0, 0, false, root, BUTTON_LEFT, mod);
  975. blocked--;
  976. if (range_click_timer->is_one_shot()) {
  977. range_click_timer->set_wait_time(0.05);
  978. range_click_timer->set_one_shot(false);
  979. range_click_timer->start();
  980. }
  981. if (!click_handled)
  982. range_click_timer->stop();
  983. } else {
  984. range_click_timer->stop();
  985. }
  986. }
  987. int Tree::propagate_mouse_event(const Point2i &p_pos,int x_ofs,int y_ofs,bool p_doubleclick,TreeItem *p_item,int p_button,const InputModifierState& p_mod) {
  988. int item_h=compute_item_height( p_item )+cache.vseparation;
  989. bool skip=(p_item==root && hide_root);
  990. if (!skip && p_pos.y<item_h) {
  991. // check event!
  992. if (range_click_timer->get_time_left() > 0 && p_item != range_item_last) {
  993. return -1;
  994. }
  995. if (!hide_folding && (p_pos.x >=x_ofs && p_pos.x < (x_ofs+cache.item_margin) )) {
  996. if (p_item->childs)
  997. p_item->set_collapsed( ! p_item->is_collapsed() );
  998. return -1; //handled!
  999. }
  1000. int x=p_pos.x;
  1001. /* find clicked column */
  1002. int col=-1;
  1003. int col_ofs=0;
  1004. int col_width=0;
  1005. for (int i=0;i<columns.size();i++) {
  1006. col_width=get_column_width(i);
  1007. if (x>col_width) {
  1008. col_ofs+=col_width;
  1009. x-=col_width;
  1010. continue;
  1011. }
  1012. col=i;
  1013. break;
  1014. }
  1015. if (col==-1)
  1016. return -1;
  1017. else if (col==0) {
  1018. int margin=x_ofs+cache.item_margin;//-cache.hseparation;
  1019. //int lm = cache.bg->get_margin(MARGIN_LEFT);
  1020. col_width-=margin;
  1021. col_ofs+=margin;
  1022. x-=margin;
  1023. } else {
  1024. col_width-=cache.hseparation;
  1025. x-=cache.hseparation;
  1026. }
  1027. TreeItem::Cell &c = p_item->cells[col];
  1028. bool already_selected=c.selected;
  1029. bool already_cursor=(p_item==selected_item) && col == selected_col;
  1030. for(int j=c.buttons.size()-1;j>=0;j--) {
  1031. Ref<Texture> b=c.buttons[j].texture;
  1032. int w = b->get_size().width + cache.button_pressed->get_minimum_size().width;
  1033. if (x>col_width-w) {
  1034. if (c.buttons[j].disabled) {
  1035. pressed_button=-1;
  1036. cache.click_type=Cache::CLICK_NONE;
  1037. return -1;
  1038. }
  1039. pressed_button=j;
  1040. cache.click_type=Cache::CLICK_BUTTON;
  1041. cache.click_index=j;
  1042. cache.click_id=c.buttons[j].id;
  1043. cache.click_item=p_item;
  1044. cache.click_column=col;
  1045. update();
  1046. //emit_signal("button_pressed");
  1047. return -1;
  1048. }
  1049. col_width-=w+cache.button_margin;
  1050. }
  1051. if (p_button==BUTTON_LEFT) {
  1052. /* process selection */
  1053. if (p_doubleclick && (!c.editable || c.mode==TreeItem::CELL_MODE_CUSTOM || c.mode==TreeItem::CELL_MODE_ICON || c.mode==TreeItem::CELL_MODE_CHECK)) {
  1054. emit_signal("item_activated");
  1055. return -1;
  1056. }
  1057. if (select_mode==SELECT_MULTI && p_mod.command && c.selectable) {
  1058. if (!c.selected) {
  1059. p_item->select(col);
  1060. emit_signal("multi_selected",p_item,col,true);
  1061. //p_item->selected_signal.call(col);
  1062. } else {
  1063. p_item->deselect(col);
  1064. emit_signal("multi_selected",p_item,col,false);
  1065. //p_item->deselected_signal.call(col);
  1066. }
  1067. } else {
  1068. if (c.selectable) {
  1069. if (select_mode==SELECT_MULTI && p_mod.shift && selected_item && selected_item!=p_item) {
  1070. bool inrange=false;
  1071. select_single_item( p_item, root, col,selected_item,&inrange );
  1072. } else {
  1073. int icount = _count_selected_items(root);
  1074. if (select_mode==SELECT_MULTI && icount>1) {
  1075. single_select_defer=p_item;
  1076. single_select_defer_column=col;
  1077. } else {
  1078. select_single_item( p_item, root, col );
  1079. }
  1080. }
  1081. //if (!c.selected && select_mode==SELECT_MULTI) {
  1082. // emit_signal("multi_selected",p_item,col,true);
  1083. //}
  1084. update();
  1085. }
  1086. }
  1087. }
  1088. if (!c.editable)
  1089. return -1; // if cell is not editable, don't bother
  1090. /* editing */
  1091. bool bring_up_editor=c.selected;// && already_selected;
  1092. bool bring_up_value_editor=false;
  1093. String editor_text=c.text;
  1094. switch (c.mode) {
  1095. case TreeItem::CELL_MODE_STRING: {
  1096. //nothing in particular
  1097. if (select_mode==SELECT_MULTI && (get_tree()->get_last_event_id() == focus_in_id || !already_cursor)) {
  1098. bring_up_editor=false;
  1099. }
  1100. } break;
  1101. case TreeItem::CELL_MODE_CHECK: {
  1102. Ref<Texture> checked = cache.checked;
  1103. bring_up_editor=false; //checkboxes are not edited with editor
  1104. if (x>=0 && x<= checked->get_width()+cache.hseparation ) {
  1105. p_item->set_checked(col,!c.checked);
  1106. item_edited(col,p_item);
  1107. click_handled=true;
  1108. //p_item->edited_signal.call(col);
  1109. }
  1110. } break;
  1111. case TreeItem::CELL_MODE_RANGE:
  1112. case TreeItem::CELL_MODE_RANGE_EXPRESSION: {
  1113. if (c.text!="") {
  1114. //if (x >= (get_column_width(col)-item_h/2)) {
  1115. popup_menu->clear();
  1116. for (int i=0;i<c.text.get_slice_count(",");i++) {
  1117. String s = c.text.get_slicec(',',i);
  1118. popup_menu->add_item(s,i);
  1119. }
  1120. popup_menu->set_size(Size2(col_width,0));
  1121. popup_menu->set_pos( get_global_pos() + Point2i(col_ofs,_get_title_button_height()+y_ofs+item_h)-cache.offset );
  1122. popup_menu->popup();
  1123. popup_edited_item=p_item;
  1124. popup_edited_item_col=col;
  1125. //}
  1126. bring_up_editor=false;
  1127. } else {
  1128. Ref<Texture> updown = cache.updown;
  1129. if (x >= (col_width-item_h/2)) {
  1130. /* touching the combo */
  1131. bool up=p_pos.y < (item_h /2);
  1132. if (p_button==BUTTON_LEFT) {
  1133. if (range_click_timer->get_time_left() == 0) {
  1134. range_item_last=p_item;
  1135. range_up_last=up;
  1136. range_click_timer->set_wait_time(0.6);
  1137. range_click_timer->set_one_shot(true);
  1138. range_click_timer->start();
  1139. } else if (up != range_up_last) {
  1140. return -1; // break. avoid changing direction on mouse held
  1141. }
  1142. p_item->set_range( col, c.val + (up?1.0:-1.0) * c.step );
  1143. item_edited(col,p_item);
  1144. } else if (p_button==BUTTON_RIGHT) {
  1145. p_item->set_range( col, (up?c.max:c.min) );
  1146. item_edited(col,p_item);
  1147. } else if (p_button==BUTTON_WHEEL_UP) {
  1148. p_item->set_range( col, c.val + c.step );
  1149. item_edited(col,p_item);
  1150. } else if (p_button==BUTTON_WHEEL_DOWN) {
  1151. p_item->set_range( col, c.val - c.step );
  1152. item_edited(col,p_item);
  1153. }
  1154. //p_item->edited_signal.call(col);
  1155. bring_up_editor=false;
  1156. } else {
  1157. editor_text=String::num( p_item->cells[col].val, Math::decimals( p_item->cells[col].step ) );
  1158. bring_up_value_editor=false;
  1159. if (select_mode==SELECT_MULTI && get_tree()->get_last_event_id() == focus_in_id)
  1160. bring_up_editor=false;
  1161. }
  1162. }
  1163. click_handled=true;
  1164. } break;
  1165. case TreeItem::CELL_MODE_ICON: {
  1166. bring_up_editor=false;
  1167. } break;
  1168. case TreeItem::CELL_MODE_CUSTOM: {
  1169. edited_item=p_item;
  1170. edited_col=col;
  1171. custom_popup_rect=Rect2i(get_global_pos() + Point2i(col_ofs,_get_title_button_height()+y_ofs+item_h-cache.offset.y), Size2(get_column_width(col),item_h));
  1172. emit_signal("custom_popup_edited",((bool)(x >= (col_width-item_h/2))));
  1173. bring_up_editor=false;
  1174. item_edited(col,p_item);
  1175. click_handled=true;
  1176. return -1;
  1177. } break;
  1178. };
  1179. if (!bring_up_editor || p_button!=BUTTON_LEFT)
  1180. return -1;
  1181. click_handled=true;
  1182. popup_edited_item=p_item;
  1183. popup_edited_item_col=col;
  1184. pressing_item_rect=Rect2(get_global_pos() + Point2i(col_ofs,_get_title_button_height()+y_ofs)-cache.offset,Size2(col_width,item_h));
  1185. pressing_for_editor_text=editor_text;
  1186. pressing_for_editor=true;
  1187. return -1; //select
  1188. } else {
  1189. Point2i new_pos=p_pos;
  1190. if (!skip) {
  1191. x_ofs+=cache.item_margin;
  1192. //new_pos.x-=cache.item_margin;
  1193. y_ofs+=item_h;
  1194. new_pos.y-=item_h;
  1195. }
  1196. if (!p_item->collapsed) { /* if not collapsed, check the childs */
  1197. TreeItem *c=p_item->childs;
  1198. while (c) {
  1199. int child_h=propagate_mouse_event( new_pos,x_ofs,y_ofs,p_doubleclick,c,p_button,p_mod);
  1200. if (child_h<0)
  1201. return -1; // break, stop propagating, no need to anymore
  1202. new_pos.y-=child_h;
  1203. y_ofs+=child_h;
  1204. c=c->next;
  1205. item_h+=child_h;
  1206. }
  1207. }
  1208. }
  1209. return item_h; // nothing found
  1210. }
  1211. void Tree::_text_editor_modal_close() {
  1212. if ( Input::get_singleton()->is_key_pressed(KEY_ESCAPE) ||
  1213. Input::get_singleton()->is_key_pressed(KEY_ENTER) ||
  1214. Input::get_singleton()->is_key_pressed(KEY_RETURN) ) {
  1215. return;
  1216. }
  1217. if (value_editor->has_point(value_editor->get_local_mouse_pos()))
  1218. return;
  1219. text_editor_enter(text_editor->get_text());
  1220. }
  1221. void Tree::text_editor_enter(String p_text) {
  1222. text_editor->hide();
  1223. value_editor->hide();
  1224. if (!popup_edited_item)
  1225. return;
  1226. if (popup_edited_item_col<0 || popup_edited_item_col>columns.size())
  1227. return;
  1228. TreeItem::Cell &c=popup_edited_item->cells[popup_edited_item_col];
  1229. switch( c.mode ) {
  1230. case TreeItem::CELL_MODE_STRING: {
  1231. c.text=p_text;
  1232. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1233. } break;
  1234. case TreeItem::CELL_MODE_RANGE: {
  1235. c.val=p_text.to_double();
  1236. if (c.step>0)
  1237. c.val=Math::stepify(c.val,c.step);
  1238. if (c.val<c.min)
  1239. c.val=c.min;
  1240. else if (c.val>c.max)
  1241. c.val=c.max;
  1242. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1243. } break;
  1244. case TreeItem::CELL_MODE_RANGE_EXPRESSION: {
  1245. if(evaluator)
  1246. c.val=evaluator->eval(p_text);
  1247. else
  1248. c.val=p_text.to_double();
  1249. } break;
  1250. default: { ERR_FAIL(); }
  1251. }
  1252. item_edited(popup_edited_item_col,popup_edited_item);
  1253. update();
  1254. }
  1255. void Tree::value_editor_changed(double p_value) {
  1256. if (updating_value_editor) {
  1257. return;
  1258. }
  1259. if (!popup_edited_item) {
  1260. return;
  1261. }
  1262. TreeItem::Cell &c=popup_edited_item->cells[popup_edited_item_col];
  1263. c.val=p_value;
  1264. item_edited(popup_edited_item_col,popup_edited_item);
  1265. update();
  1266. }
  1267. void Tree::popup_select(int p_option) {
  1268. if (!popup_edited_item)
  1269. return;
  1270. if (popup_edited_item_col<0 || popup_edited_item_col>columns.size())
  1271. return;
  1272. popup_edited_item->cells[popup_edited_item_col].val=p_option;
  1273. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1274. update();
  1275. item_edited(popup_edited_item_col,popup_edited_item);
  1276. }
  1277. void Tree::_input_event(InputEvent p_event) {
  1278. switch (p_event.type) {
  1279. case InputEvent::KEY: {
  1280. if (!p_event.key.pressed)
  1281. break;
  1282. if (p_event.key.mod.alt || p_event.key.mod.command || (p_event.key.mod.shift && p_event.key.unicode==0) || p_event.key.mod.meta)
  1283. break;
  1284. if (!root)
  1285. return;
  1286. if (hide_root && !root->get_next_visible())
  1287. return;
  1288. switch(p_event.key.scancode) {
  1289. #define EXIT_BREAK { if (!cursor_can_exit_tree) accept_event(); break; }
  1290. case KEY_RIGHT: {
  1291. //TreeItem *next = NULL;
  1292. if (!selected_item)
  1293. break;
  1294. if (select_mode==SELECT_ROW)
  1295. EXIT_BREAK;
  1296. if (selected_col>=(columns.size()-1))
  1297. EXIT_BREAK;
  1298. if (select_mode==SELECT_MULTI) {
  1299. selected_col++;
  1300. emit_signal("cell_selected");
  1301. } else {
  1302. selected_item->select(selected_col+1);
  1303. }
  1304. update();
  1305. ensure_cursor_is_visible();
  1306. accept_event();
  1307. } break;
  1308. case KEY_LEFT: {
  1309. // TreeItem *next = NULL;
  1310. if (!selected_item)
  1311. break;
  1312. if (select_mode==SELECT_ROW)
  1313. EXIT_BREAK;
  1314. if (selected_col<=0)
  1315. EXIT_BREAK;
  1316. if (select_mode==SELECT_MULTI) {
  1317. selected_col--;
  1318. emit_signal("cell_selected");
  1319. } else {
  1320. selected_item->select(selected_col-1);
  1321. }
  1322. update();
  1323. accept_event();
  1324. } break;
  1325. case KEY_DOWN: {
  1326. TreeItem *next = NULL;
  1327. if (!selected_item) {
  1328. next=hide_root?root->get_next_visible():root;
  1329. selected_item=0;
  1330. } else {
  1331. next=selected_item->get_next_visible();
  1332. // if (diff < uint64_t(GLOBAL_DEF("gui/incr_search_max_interval_msec",2000))) {
  1333. if (last_keypress!=0) {
  1334. //incr search next
  1335. int col;
  1336. next=_search_item_text(next,incr_search,&col,true);
  1337. if (!next) {
  1338. accept_event();
  1339. return;
  1340. }
  1341. }
  1342. }
  1343. if (select_mode==SELECT_MULTI) {
  1344. if (!next)
  1345. EXIT_BREAK;
  1346. selected_item=next;
  1347. emit_signal("cell_selected");
  1348. update();
  1349. } else {
  1350. int col=selected_col<0?0:selected_col;
  1351. while (next && !next->cells[col].selectable)
  1352. next=next->get_next_visible();
  1353. if (!next)
  1354. EXIT_BREAK; // do nothing..
  1355. next->select(col);
  1356. }
  1357. ensure_cursor_is_visible();
  1358. accept_event();
  1359. } break;
  1360. case KEY_UP: {
  1361. TreeItem *prev = NULL;
  1362. if (!selected_item) {
  1363. prev = get_last_item();
  1364. selected_col=0;
  1365. } else {
  1366. prev=selected_item->get_prev_visible();
  1367. if (last_keypress!=0) {
  1368. //incr search next
  1369. int col;
  1370. prev=_search_item_text(prev,incr_search,&col,true,true);
  1371. if (!prev) {
  1372. accept_event();
  1373. return;
  1374. }
  1375. }
  1376. }
  1377. if (select_mode==SELECT_MULTI) {
  1378. if (!prev)
  1379. break;
  1380. selected_item=prev;
  1381. emit_signal("cell_selected");
  1382. update();
  1383. } else {
  1384. int col=selected_col<0?0:selected_col;
  1385. while (prev && !prev->cells[col].selectable)
  1386. prev=prev->get_prev_visible();
  1387. if (!prev)
  1388. break; // do nothing..
  1389. prev->select(col);
  1390. }
  1391. ensure_cursor_is_visible();
  1392. accept_event();
  1393. } break;
  1394. case KEY_PAGEDOWN: {
  1395. TreeItem *next = NULL;
  1396. if (!selected_item)
  1397. break;
  1398. next=selected_item;
  1399. for(int i=0;i<10;i++) {
  1400. TreeItem *_n = next->get_next_visible();
  1401. if (_n) {
  1402. next=_n;
  1403. } else {
  1404. break;
  1405. }
  1406. }
  1407. if (next==selected_item)
  1408. break;
  1409. if (select_mode==SELECT_MULTI) {
  1410. selected_item=next;
  1411. emit_signal("cell_selected");
  1412. update();
  1413. } else {
  1414. while (next && !next->cells[selected_col].selectable)
  1415. next=next->get_next_visible();
  1416. if (!next)
  1417. EXIT_BREAK; // do nothing..
  1418. next->select(selected_col);
  1419. }
  1420. ensure_cursor_is_visible();
  1421. } break;
  1422. case KEY_PAGEUP: {
  1423. TreeItem *prev = NULL;
  1424. if (!selected_item)
  1425. break;
  1426. prev=selected_item;
  1427. for(int i=0;i<10;i++) {
  1428. TreeItem *_n = prev->get_prev_visible();
  1429. if (_n) {
  1430. prev=_n;
  1431. } else {
  1432. break;
  1433. }
  1434. }
  1435. if (prev==selected_item)
  1436. break;
  1437. if (select_mode==SELECT_MULTI) {
  1438. selected_item=prev;
  1439. emit_signal("cell_selected");
  1440. update();
  1441. } else {
  1442. while (prev && !prev->cells[selected_col].selectable)
  1443. prev=prev->get_prev_visible();
  1444. if (!prev)
  1445. EXIT_BREAK; // do nothing..
  1446. prev->select(selected_col);
  1447. }
  1448. ensure_cursor_is_visible();
  1449. } break;
  1450. case KEY_F2:
  1451. case KEY_RETURN:
  1452. case KEY_ENTER: {
  1453. if (selected_item) {
  1454. //bring up editor if possible
  1455. if (!edit_selected()) {
  1456. emit_signal("item_activated");
  1457. }
  1458. }
  1459. accept_event();
  1460. } break;
  1461. case KEY_SPACE: {
  1462. if (select_mode==SELECT_MULTI) {
  1463. if (!selected_item)
  1464. break;
  1465. if (selected_item->is_selected(selected_col)) {
  1466. selected_item->deselect(selected_col);
  1467. emit_signal("multi_selected",selected_item,selected_col,false);
  1468. } else if (selected_item->is_selectable(selected_col)) {
  1469. selected_item->select(selected_col);
  1470. emit_signal("multi_selected",selected_item,selected_col,true);
  1471. }
  1472. }
  1473. accept_event();
  1474. } break;
  1475. default: {
  1476. if (p_event.key.unicode>0) {
  1477. _do_incr_search(String::chr(p_event.key.unicode));
  1478. accept_event();
  1479. return;
  1480. } else {
  1481. if (p_event.key.scancode!=KEY_SHIFT)
  1482. last_keypress=0;
  1483. }
  1484. } break;
  1485. last_keypress=0;
  1486. }
  1487. } break;
  1488. case InputEvent::MOUSE_MOTION: {
  1489. if (cache.font.is_null()) // avoid a strange case that may fuckup stuff
  1490. update_cache();
  1491. const InputEventMouseMotion& b=p_event.mouse_motion;
  1492. Ref<StyleBox> bg = cache.bg;
  1493. Point2 pos = Point2(b.x,b.y) - bg->get_offset();
  1494. Cache::ClickType old_hover = cache.hover_type;
  1495. int old_index = cache.hover_index;
  1496. cache.hover_type=Cache::CLICK_NONE;
  1497. cache.hover_index=0;
  1498. if (show_column_titles) {
  1499. pos.y-=_get_title_button_height();
  1500. if (pos.y<0) {
  1501. pos.x+=cache.offset.x;
  1502. int len=0;
  1503. for(int i=0;i<columns.size();i++) {
  1504. len+=get_column_width(i);
  1505. if (pos.x<len) {
  1506. cache.hover_type=Cache::CLICK_TITLE;
  1507. cache.hover_index=i;
  1508. update();
  1509. break;
  1510. }
  1511. }
  1512. }
  1513. }
  1514. if (drop_mode_flags && root) {
  1515. Point2 mpos=Point2(b.x,b.y);
  1516. mpos -= cache.bg->get_offset();
  1517. mpos.y-=_get_title_button_height();
  1518. if (mpos.y>=0) {
  1519. if (h_scroll->is_visible())
  1520. mpos.x+=h_scroll->get_val();
  1521. if (v_scroll->is_visible())
  1522. mpos.y+=v_scroll->get_val();
  1523. int col,h,section;
  1524. TreeItem *it = _find_item_at_pos(root,mpos,col,h,section);
  1525. if (it!=drop_mode_over || section!=drop_mode_section) {
  1526. drop_mode_over=it;
  1527. drop_mode_section=section;
  1528. update();
  1529. }
  1530. }
  1531. }
  1532. if (cache.hover_type!=old_hover || cache.hover_index!=old_index) {
  1533. update();
  1534. }
  1535. if (pressing_for_editor && popup_edited_item && popup_edited_item->get_cell_mode(popup_edited_item_col)==TreeItem::CELL_MODE_RANGE) {
  1536. //range drag
  1537. if (!range_drag_enabled) {
  1538. Vector2 cpos = Vector2(b.x,b.y);
  1539. if (cpos.distance_to(pressing_pos)>2) {
  1540. range_drag_enabled=true;
  1541. range_drag_capture_pos=cpos;
  1542. range_drag_base=popup_edited_item->get_range(popup_edited_item_col);
  1543. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  1544. }
  1545. } else {
  1546. TreeItem::Cell &c=popup_edited_item->cells[popup_edited_item_col];
  1547. float diff_y = -b.relative_y;
  1548. diff_y=Math::pow(ABS(diff_y),1.8)*SGN(diff_y);
  1549. diff_y*=0.1;
  1550. range_drag_base=CLAMP(range_drag_base + c.step * diff_y, c.min, c.max);
  1551. popup_edited_item->set_range(popup_edited_item_col,range_drag_base);
  1552. item_edited(popup_edited_item_col,popup_edited_item);
  1553. }
  1554. }
  1555. if (drag_touching && ! drag_touching_deaccel) {
  1556. drag_accum-=b.relative_y;
  1557. v_scroll->set_val(drag_from+drag_accum);
  1558. drag_speed=-b.speed_y;
  1559. }
  1560. } break;
  1561. case InputEvent::MOUSE_BUTTON: {
  1562. if (cache.font.is_null()) // avoid a strange case that may fuckup stuff
  1563. update_cache();
  1564. const InputEventMouseButton& b=p_event.mouse_button;
  1565. if (!b.pressed) {
  1566. if (b.button_index==BUTTON_LEFT) {
  1567. if (single_select_defer) {
  1568. select_single_item( single_select_defer, root, single_select_defer_column );
  1569. single_select_defer=NULL;
  1570. }
  1571. range_click_timer->stop();
  1572. if (pressing_for_editor) {
  1573. if (range_drag_enabled) {
  1574. range_drag_enabled=false;
  1575. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  1576. warp_mouse(range_drag_capture_pos);
  1577. } else
  1578. edit_selected();
  1579. pressing_for_editor=false;
  1580. }
  1581. if (cache.click_type==Cache::CLICK_BUTTON) {
  1582. emit_signal("button_pressed",cache.click_item,cache.click_column,cache.click_id);
  1583. }
  1584. cache.click_type=Cache::CLICK_NONE;
  1585. cache.click_index=-1;
  1586. cache.click_id=-1;
  1587. cache.click_item=NULL;
  1588. cache.click_column=0;
  1589. if (drag_touching) {
  1590. if (drag_speed==0) {
  1591. drag_touching_deaccel=false;
  1592. drag_touching=false;
  1593. set_fixed_process(false);
  1594. } else {
  1595. drag_touching_deaccel=true;
  1596. }
  1597. }
  1598. update();
  1599. }
  1600. break;
  1601. }
  1602. switch(b.button_index) {
  1603. case BUTTON_LEFT: {
  1604. Ref<StyleBox> bg = cache.bg;
  1605. Point2 pos = Point2(b.x,b.y) - bg->get_offset();
  1606. cache.click_type=Cache::CLICK_NONE;
  1607. if (show_column_titles) {
  1608. pos.y-=_get_title_button_height();
  1609. if (pos.y<0) {
  1610. pos.x+=cache.offset.x;
  1611. int len=0;
  1612. for(int i=0;i<columns.size();i++) {
  1613. len+=get_column_width(i);
  1614. if (pos.x<len) {
  1615. cache.click_type=Cache::CLICK_TITLE;
  1616. cache.click_index=i;
  1617. //cache.click_id=;
  1618. update();
  1619. break;
  1620. }
  1621. }
  1622. break;
  1623. }
  1624. }
  1625. if (!root)
  1626. break;
  1627. click_handled=false;
  1628. pressing_for_editor=false;
  1629. blocked++;
  1630. bool handled = propagate_mouse_event(pos+cache.offset,0,0,b.doubleclick,root,b.button_index,b.mod);
  1631. blocked--;
  1632. if (pressing_for_editor) {
  1633. pressing_pos=Point2(b.x,b.y);
  1634. }
  1635. if (drag_touching) {
  1636. set_fixed_process(false);
  1637. drag_touching_deaccel=false;
  1638. drag_touching=false;
  1639. drag_speed=0;
  1640. drag_from=0;
  1641. }
  1642. if (!click_handled) {
  1643. drag_speed=0;
  1644. drag_accum=0;
  1645. // last_drag_accum=0;
  1646. drag_from=v_scroll->get_val();
  1647. drag_touching=OS::get_singleton()->has_touchscreen_ui_hint();
  1648. drag_touching_deaccel=false;
  1649. if (drag_touching) {
  1650. set_fixed_process(true);
  1651. }
  1652. }
  1653. } break;
  1654. case BUTTON_WHEEL_UP: {
  1655. v_scroll->set_val( v_scroll->get_val()-v_scroll->get_page()/8 );
  1656. } break;
  1657. case BUTTON_WHEEL_DOWN: {
  1658. v_scroll->set_val( v_scroll->get_val()+v_scroll->get_page()/8 );
  1659. } break;
  1660. }
  1661. } break;
  1662. }
  1663. }
  1664. bool Tree::edit_selected() {
  1665. TreeItem *s = get_selected();
  1666. ERR_EXPLAIN("No item selected!");
  1667. ERR_FAIL_COND_V(!s,false);
  1668. ensure_cursor_is_visible();
  1669. int col = get_selected_column();
  1670. ERR_EXPLAIN("No item column selected!");
  1671. ERR_FAIL_INDEX_V(col,columns.size(),false);
  1672. if (!s->cells[col].editable)
  1673. return false;
  1674. Rect2 rect;
  1675. rect.pos.y = get_item_offset(s) - get_scroll().y;
  1676. for(int i=0;i<col;i++) {
  1677. rect.pos.x+=get_column_width(i);
  1678. }
  1679. rect.size.width=get_column_width(col);
  1680. rect.size.height=compute_item_height(s)+cache.vseparation;
  1681. popup_edited_item=s;
  1682. popup_edited_item_col=col;
  1683. TreeItem::Cell &c = s->cells[col];
  1684. if (c.mode==TreeItem::CELL_MODE_CHECK) {
  1685. s->set_checked(col, !c.checked);
  1686. item_edited(col,s);
  1687. return true;
  1688. } else if (c.mode==TreeItem::CELL_MODE_CUSTOM) {
  1689. edited_item=s;
  1690. edited_col=col;
  1691. custom_popup_rect=Rect2i( get_global_pos() + rect.pos, rect.size );
  1692. emit_signal("custom_popup_edited",false);
  1693. item_edited(col,s);
  1694. return true;
  1695. } else if ((c.mode==TreeItem::CELL_MODE_RANGE||c.mode==TreeItem::CELL_MODE_RANGE_EXPRESSION) && c.text!="") {
  1696. popup_menu->clear();
  1697. for (int i=0;i<c.text.get_slice_count(",");i++) {
  1698. String s = c.text.get_slicec(',',i);
  1699. popup_menu->add_item(s,i);
  1700. }
  1701. popup_menu->set_size(Size2(rect.size.width,0));
  1702. popup_menu->set_pos( get_global_pos() + rect.pos + Point2i(0,rect.size.height) );
  1703. popup_menu->popup();
  1704. popup_edited_item=s;
  1705. popup_edited_item_col=col;
  1706. return true;
  1707. } else if (c.mode==TreeItem::CELL_MODE_STRING || c.mode==TreeItem::CELL_MODE_RANGE || c.mode==TreeItem::CELL_MODE_RANGE_EXPRESSION ) {
  1708. Point2i textedpos=get_global_pos() + rect.pos;
  1709. text_editor->set_pos( textedpos );
  1710. text_editor->set_size( rect.size);
  1711. text_editor->clear();
  1712. text_editor->set_text( c.mode==TreeItem::CELL_MODE_STRING?c.text:rtos(c.val) );
  1713. text_editor->select_all();
  1714. if (c.mode==TreeItem::CELL_MODE_RANGE || c.mode==TreeItem::CELL_MODE_RANGE_EXPRESSION ) {
  1715. value_editor->set_pos(textedpos + Point2i(0,text_editor->get_size().height) );
  1716. value_editor->set_size( Size2(rect.size.width,1));
  1717. value_editor->show_modal();
  1718. updating_value_editor=true;
  1719. value_editor->set_min( c.min );
  1720. value_editor->set_max( c.max );
  1721. value_editor->set_step( c.step );
  1722. value_editor->set_val( c.val );
  1723. value_editor->set_exp_unit_value( c.expr );
  1724. updating_value_editor=false;
  1725. }
  1726. text_editor->show_modal();
  1727. text_editor->grab_focus();
  1728. return true;
  1729. }
  1730. return false;
  1731. }
  1732. Size2 Tree::get_internal_min_size() const {
  1733. Size2i size=cache.bg->get_offset();
  1734. if (root)
  1735. size.height+=get_item_height(root);
  1736. for (int i=0;i<columns.size();i++) {
  1737. size.width+=columns[i].min_width;
  1738. }
  1739. return size;
  1740. }
  1741. void Tree::update_scrollbars() {
  1742. Size2 size = get_size();
  1743. int tbh;
  1744. if (show_column_titles) {
  1745. tbh=_get_title_button_height();
  1746. } else {
  1747. tbh=0;
  1748. }
  1749. Size2 hmin = h_scroll->get_combined_minimum_size();
  1750. Size2 vmin = v_scroll->get_combined_minimum_size();
  1751. v_scroll->set_begin( Point2(size.width - vmin.width , cache.bg->get_margin(MARGIN_TOP)) );
  1752. v_scroll->set_end( Point2(size.width, size.height-cache.bg->get_margin(MARGIN_TOP)-cache.bg->get_margin(MARGIN_BOTTOM)) );
  1753. h_scroll->set_begin( Point2( 0, size.height - hmin.height) );
  1754. h_scroll->set_end( Point2(size.width-vmin.width, size.height) );
  1755. Size2 min = get_internal_min_size();
  1756. if (min.height < size.height - hmin.height) {
  1757. v_scroll->hide();
  1758. cache.offset.y=0;
  1759. } else {
  1760. v_scroll->show();
  1761. v_scroll->set_max(min.height);
  1762. v_scroll->set_page(size.height - hmin.height - tbh);
  1763. cache.offset.y=v_scroll->get_val();
  1764. }
  1765. if (min.width < size.width - vmin.width) {
  1766. h_scroll->hide();
  1767. cache.offset.x=0;
  1768. } else {
  1769. h_scroll->show();
  1770. h_scroll->set_max(min.width);
  1771. h_scroll->set_page(size.width - vmin.width);
  1772. cache.offset.x=h_scroll->get_val();
  1773. }
  1774. }
  1775. int Tree::_get_title_button_height() const {
  1776. return show_column_titles?cache.font->get_height() + cache.title_button->get_minimum_size().height:0;
  1777. }
  1778. void Tree::_notification(int p_what) {
  1779. if (p_what==NOTIFICATION_FOCUS_ENTER) {
  1780. focus_in_id=get_tree()->get_last_event_id();
  1781. }
  1782. if (p_what==NOTIFICATION_MOUSE_EXIT) {
  1783. if (cache.hover_type!=Cache::CLICK_NONE) {
  1784. cache.hover_type=Cache::CLICK_NONE;
  1785. update();
  1786. }
  1787. }
  1788. if (p_what==NOTIFICATION_VISIBILITY_CHANGED) {
  1789. drag_touching=false;
  1790. }
  1791. if (p_what==NOTIFICATION_ENTER_TREE) {
  1792. update_cache();;
  1793. }
  1794. if (p_what==NOTIFICATION_DRAG_END) {
  1795. drop_mode_flags=0;
  1796. update();
  1797. }
  1798. if (p_what==NOTIFICATION_DRAG_BEGIN) {
  1799. single_select_defer=NULL;
  1800. }
  1801. if (p_what==NOTIFICATION_FIXED_PROCESS) {
  1802. if (drag_touching) {
  1803. if (drag_touching_deaccel) {
  1804. float pos = v_scroll->get_val();
  1805. pos+=drag_speed*get_fixed_process_delta_time();
  1806. bool turnoff=false;
  1807. if (pos<0) {
  1808. pos=0;
  1809. turnoff=true;
  1810. set_fixed_process(false);
  1811. drag_touching=false;
  1812. drag_touching_deaccel=false;
  1813. }
  1814. if (pos > (v_scroll->get_max()-v_scroll->get_page())) {
  1815. pos=v_scroll->get_max()-v_scroll->get_page();
  1816. turnoff=true;
  1817. }
  1818. v_scroll->set_val(pos);
  1819. float sgn = drag_speed<0? -1 : 1;
  1820. float val = Math::abs(drag_speed);
  1821. val-=1000*get_fixed_process_delta_time();
  1822. if (val<0) {
  1823. turnoff=true;
  1824. }
  1825. drag_speed=sgn*val;
  1826. if (turnoff) {
  1827. set_fixed_process(false);
  1828. drag_touching=false;
  1829. drag_touching_deaccel=false;
  1830. }
  1831. } else {
  1832. }
  1833. }
  1834. }
  1835. if (p_what==NOTIFICATION_DRAW) {
  1836. update_cache();
  1837. update_scrollbars();
  1838. RID ci = get_canvas_item();
  1839. VisualServer::get_singleton()->canvas_item_set_clip(ci,true);
  1840. Ref<StyleBox> bg = cache.bg;
  1841. Ref<StyleBox> bg_focus = get_stylebox("bg_focus");
  1842. Point2 draw_ofs;
  1843. draw_ofs+=bg->get_offset();
  1844. Size2 draw_size=get_size()-bg->get_minimum_size();
  1845. bg->draw( ci, Rect2( Point2(), get_size()) );
  1846. if (has_focus()) {
  1847. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci,true);
  1848. bg_focus->draw( ci, Rect2( Point2(), get_size()) );
  1849. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci,false);
  1850. }
  1851. int tbh = _get_title_button_height();
  1852. draw_ofs.y+=tbh;
  1853. draw_size.y-=tbh;
  1854. if (root) {
  1855. draw_item( Point2(),draw_ofs,draw_size,root);
  1856. }
  1857. int ofs=0;
  1858. // int from_y=exposed.pos.y+bg->get_margin(MARGIN_TOP);
  1859. // int size_y=exposed.size.height-bg->get_minimum_size().height;
  1860. for (int i=0;i<(columns.size()-1-1);i++) {
  1861. ofs+=get_column_width(i);
  1862. //get_painter()->draw_fill_rect( Point2(ofs+cache.hseparation/2, from_y), Size2( 1, size_y ),color( COLOR_TREE_GRID) );
  1863. }
  1864. if (show_column_titles) {
  1865. //title butons
  1866. int ofs=cache.bg->get_margin(MARGIN_LEFT);
  1867. for(int i=0;i<columns.size();i++) {
  1868. Ref<StyleBox> sb = (cache.click_type==Cache::CLICK_TITLE && cache.click_index==i)?cache.title_button_pressed:((cache.hover_type==Cache::CLICK_TITLE && cache.hover_index==i)?cache.title_button_hover:cache.title_button);
  1869. Ref<Font> f = cache.tb_font;
  1870. Rect2 tbrect = Rect2(ofs - cache.offset.x,bg->get_margin(MARGIN_TOP),get_column_width(i),tbh);
  1871. sb->draw(ci,tbrect);
  1872. ofs+=tbrect.size.width;
  1873. //text
  1874. int clip_w = tbrect.size.width - sb->get_minimum_size().width;
  1875. f->draw_halign(ci,tbrect.pos+Point2i(sb->get_offset().x,(tbrect.size.height-f->get_height())/2+f->get_ascent()),HALIGN_CENTER,clip_w,columns[i].title,cache.title_button_color);
  1876. }
  1877. }
  1878. }
  1879. if (p_what==NOTIFICATION_THEME_CHANGED) {
  1880. update_cache();
  1881. }
  1882. }
  1883. Size2 Tree::get_minimum_size() const {
  1884. return Size2(1,1);
  1885. }
  1886. TreeItem *Tree::create_item(TreeItem *p_parent) {
  1887. ERR_FAIL_COND_V(blocked>0,NULL);
  1888. TreeItem *ti = memnew( TreeItem(this) );
  1889. ti->cells.resize( columns.size() );
  1890. ERR_FAIL_COND_V(!ti,NULL);
  1891. if (p_parent) {
  1892. /* Always append at the end */
  1893. TreeItem *last=0;
  1894. TreeItem *c=p_parent->childs;
  1895. while(c) {
  1896. last=c;
  1897. c=c->next;
  1898. }
  1899. if (last) {
  1900. last->next=ti;
  1901. } else {
  1902. p_parent->childs=ti;
  1903. }
  1904. ti->parent=p_parent;
  1905. } else {
  1906. if (root)
  1907. ti->childs=root;
  1908. root=ti;
  1909. }
  1910. return ti;
  1911. }
  1912. TreeItem* Tree::get_root() {
  1913. return root;
  1914. }
  1915. TreeItem* Tree::get_last_item() {
  1916. TreeItem *last=root;
  1917. while(last) {
  1918. if (last->next)
  1919. last=last->next;
  1920. else if (last->childs)
  1921. last=last->childs;
  1922. else
  1923. break;
  1924. }
  1925. return last;
  1926. }
  1927. void Tree::item_edited(int p_column,TreeItem *p_item) {
  1928. edited_item=p_item;
  1929. edited_col=p_column;
  1930. emit_signal("item_edited");
  1931. }
  1932. void Tree::item_changed(int p_column,TreeItem *p_item) {
  1933. update();
  1934. }
  1935. void Tree::item_selected(int p_column,TreeItem *p_item) {
  1936. if (select_mode==SELECT_MULTI) {
  1937. if (!p_item->cells[p_column].selectable)
  1938. return;
  1939. p_item->cells[p_column].selected=true;
  1940. //emit_signal("multi_selected",p_item,p_column,true); - NO this is for TreeItem::select
  1941. } else {
  1942. select_single_item(p_item,root,p_column);
  1943. }
  1944. update();
  1945. }
  1946. void Tree::item_deselected(int p_column,TreeItem *p_item) {
  1947. if (select_mode==SELECT_MULTI) {
  1948. p_item->cells[p_column].selected=false;
  1949. }
  1950. update();
  1951. }
  1952. void Tree::set_select_mode(SelectMode p_mode) {
  1953. select_mode=p_mode;
  1954. }
  1955. void Tree::clear() {
  1956. if (blocked>0) {
  1957. ERR_FAIL_COND(blocked>0);
  1958. }
  1959. if (pressing_for_editor) {
  1960. if (range_drag_enabled) {
  1961. range_drag_enabled = false;
  1962. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  1963. warp_mouse(range_drag_capture_pos);
  1964. }
  1965. pressing_for_editor = false;
  1966. }
  1967. if (root) {
  1968. memdelete( root );
  1969. root = NULL;
  1970. };
  1971. selected_item=NULL;
  1972. edited_item=NULL;
  1973. popup_edited_item=NULL;
  1974. selected_item=NULL;
  1975. update();
  1976. };
  1977. void Tree::set_hide_root(bool p_enabled) {
  1978. hide_root=p_enabled;
  1979. update();
  1980. }
  1981. void Tree::set_column_min_width(int p_column,int p_min_width) {
  1982. ERR_FAIL_INDEX(p_column,columns.size());
  1983. if (p_min_width<1)
  1984. return;
  1985. columns[p_column].min_width=p_min_width;
  1986. update();
  1987. }
  1988. void Tree::set_column_expand(int p_column,bool p_expand) {
  1989. ERR_FAIL_INDEX(p_column,columns.size());
  1990. columns[p_column].expand=p_expand;
  1991. update();
  1992. }
  1993. TreeItem *Tree::get_selected() const {
  1994. return selected_item;
  1995. }
  1996. int Tree::get_selected_column() const {
  1997. return selected_col;
  1998. }
  1999. TreeItem *Tree::get_edited() const {
  2000. return edited_item;
  2001. }
  2002. int Tree::get_edited_column() const {
  2003. return edited_col;
  2004. }
  2005. TreeItem* Tree::get_next_selected( TreeItem* p_item) {
  2006. //if (!p_item)
  2007. // return NULL;
  2008. if (!root)
  2009. return NULL;
  2010. while(true) {
  2011. if (!p_item) {
  2012. p_item=root;
  2013. } else {
  2014. if (p_item->childs) {
  2015. p_item=p_item->childs;
  2016. } else if (p_item->next) {
  2017. p_item=p_item->next;
  2018. } else {
  2019. while(!p_item->next) {
  2020. p_item=p_item->parent;
  2021. if (p_item==NULL)
  2022. return NULL;
  2023. }
  2024. p_item=p_item->next;
  2025. }
  2026. }
  2027. for (int i=0;i<columns.size();i++)
  2028. if (p_item->cells[i].selected)
  2029. return p_item;
  2030. }
  2031. return NULL;
  2032. }
  2033. int Tree::get_column_width(int p_column) const {
  2034. ERR_FAIL_INDEX_V(p_column,columns.size(),-1);
  2035. if (!columns[p_column].expand)
  2036. return columns[p_column].min_width;
  2037. Ref<StyleBox> bg = cache.bg;
  2038. int expand_area=get_size().width-(bg->get_margin(MARGIN_LEFT)+bg->get_margin(MARGIN_RIGHT));
  2039. if (v_scroll->is_visible())
  2040. expand_area-=v_scroll->get_combined_minimum_size().width;
  2041. int expanding_columns=0;
  2042. int expanding_total=0;
  2043. for (int i=0;i<columns.size();i++) {
  2044. if (!columns[i].expand) {
  2045. expand_area-=columns[i].min_width;
  2046. } else {
  2047. expanding_total+=columns[i].min_width;
  2048. expanding_columns++;
  2049. }
  2050. }
  2051. if (expand_area<expanding_total)
  2052. return columns[p_column].min_width;
  2053. ERR_FAIL_COND_V(expanding_columns==0,-1); // shouldnt happen
  2054. return expand_area * columns[p_column].min_width / expanding_total;
  2055. }
  2056. void Tree::propagate_set_columns(TreeItem *p_item) {
  2057. p_item->cells.resize( columns.size() );
  2058. TreeItem *c = p_item->get_children();
  2059. while(c) {
  2060. propagate_set_columns(c);
  2061. c=c->get_next();
  2062. }
  2063. }
  2064. void Tree::set_columns(int p_columns) {
  2065. ERR_FAIL_COND(p_columns<1);
  2066. ERR_FAIL_COND(blocked>0);
  2067. columns.resize(p_columns);
  2068. if (root)
  2069. propagate_set_columns(root);
  2070. if (selected_col>=p_columns)
  2071. selected_col=p_columns-1;
  2072. update();
  2073. }
  2074. int Tree::get_columns() const {
  2075. return columns.size();
  2076. }
  2077. void Tree::_scroll_moved(float) {
  2078. update();
  2079. }
  2080. Rect2 Tree::get_custom_popup_rect() const {
  2081. return custom_popup_rect;
  2082. }
  2083. int Tree::get_item_offset(TreeItem *p_item) const {
  2084. TreeItem *it=root;
  2085. int ofs=_get_title_button_height();
  2086. if (!it)
  2087. return 0;
  2088. while(true) {
  2089. if (it==p_item)
  2090. return ofs;
  2091. ofs+=compute_item_height(it)+cache.vseparation;
  2092. if (it->childs && !it->collapsed) {
  2093. it=it->childs;
  2094. } else if (it->next) {
  2095. it=it->next;
  2096. } else {
  2097. while(!it->next) {
  2098. it=it->parent;
  2099. if (it==NULL)
  2100. return 0;
  2101. }
  2102. it=it->next;
  2103. }
  2104. }
  2105. return -1; //not found
  2106. }
  2107. void Tree::ensure_cursor_is_visible() {
  2108. if (!is_inside_tree())
  2109. return;
  2110. TreeItem *selected = get_selected();
  2111. if (!selected)
  2112. return;
  2113. int ofs = get_item_offset(selected);
  2114. if (ofs==-1)
  2115. return;
  2116. int h = compute_item_height(selected)+cache.vseparation;
  2117. int screenh=get_size().height-h_scroll->get_combined_minimum_size().height;
  2118. if (ofs+h>v_scroll->get_val()+screenh)
  2119. v_scroll->set_val(ofs-screenh+h);
  2120. else if (ofs < v_scroll->get_val())
  2121. v_scroll->set_val(ofs);
  2122. }
  2123. int Tree::get_pressed_button() const {
  2124. return pressed_button;
  2125. }
  2126. Rect2 Tree::get_item_rect(TreeItem *p_item,int p_column) const {
  2127. ERR_FAIL_NULL_V(p_item,Rect2());
  2128. ERR_FAIL_COND_V(p_item->tree!=this,Rect2());
  2129. if (p_column!=-1) {
  2130. ERR_FAIL_INDEX_V(p_column,columns.size(),Rect2());
  2131. }
  2132. int ofs = get_item_offset(p_item);
  2133. int height = compute_item_height(p_item);
  2134. Rect2 r;
  2135. r.pos.y=ofs;
  2136. r.size.height=height;
  2137. if (p_column==-1) {
  2138. r.pos.x=0;
  2139. r.size.x=get_size().width;
  2140. } else {
  2141. int accum=0;
  2142. for(int i=0;i<p_column;i++) {
  2143. accum+=get_column_width(i);
  2144. }
  2145. r.pos.x=accum;
  2146. r.size.x=get_column_width(p_column);
  2147. }
  2148. return r;
  2149. }
  2150. void Tree::set_column_titles_visible(bool p_show) {
  2151. show_column_titles=p_show;
  2152. update();
  2153. }
  2154. bool Tree::are_column_titles_visible() const {
  2155. return show_column_titles;
  2156. }
  2157. void Tree::set_column_title(int p_column,const String& p_title) {
  2158. ERR_FAIL_INDEX(p_column,columns.size());
  2159. columns[p_column].title=p_title;
  2160. update();
  2161. }
  2162. String Tree::get_column_title(int p_column) const {
  2163. ERR_FAIL_INDEX_V(p_column,columns.size(),"");
  2164. return columns[p_column].title;
  2165. }
  2166. Point2 Tree::get_scroll() const {
  2167. Point2 ofs;
  2168. if (h_scroll->is_visible())
  2169. ofs.x=h_scroll->get_val();
  2170. if (v_scroll->is_visible())
  2171. ofs.y=v_scroll->get_val();
  2172. return ofs;
  2173. }
  2174. TreeItem* Tree::_search_item_text(TreeItem *p_at, const String& p_find,int *r_col,bool p_selectable,bool p_backwards) {
  2175. while(p_at) {
  2176. for(int i=0;i<columns.size();i++) {
  2177. if (p_at->get_text(i).findn(p_find)==0 && (!p_selectable || p_at->is_selectable(i))) {
  2178. if (r_col)
  2179. *r_col=i;
  2180. return p_at;
  2181. }
  2182. }
  2183. if (p_backwards)
  2184. p_at=p_at->get_prev_visible();
  2185. else
  2186. p_at=p_at->get_next_visible();
  2187. }
  2188. return NULL;
  2189. }
  2190. TreeItem* Tree::search_item_text(const String& p_find,int *r_col,bool p_selectable) {
  2191. if (!root)
  2192. return NULL;
  2193. return _search_item_text(root,p_find,r_col,p_selectable);
  2194. }
  2195. void Tree::_do_incr_search(const String& p_add) {
  2196. uint64_t time = OS::get_singleton()->get_ticks_usec() / 1000; // convert to msec
  2197. uint64_t diff = time - last_keypress;
  2198. if (diff > uint64_t(GLOBAL_DEF("gui/incr_search_max_interval_msec",2000)))
  2199. incr_search=p_add;
  2200. else
  2201. incr_search+=p_add;
  2202. last_keypress=time;
  2203. int col;
  2204. TreeItem *item = search_item_text(incr_search,&col,true);
  2205. if (!item)
  2206. return;
  2207. item->select(col);
  2208. ensure_cursor_is_visible();
  2209. }
  2210. TreeItem* Tree::_find_item_at_pos(TreeItem*p_item, const Point2& p_pos,int& r_column,int &h,int &section) const {
  2211. Point2 pos = p_pos;
  2212. if (root!=p_item || ! hide_root) {
  2213. h = compute_item_height(p_item)+cache.vseparation;;
  2214. if (pos.y<h) {
  2215. if (drop_mode_flags==DROP_MODE_ON_ITEM) {
  2216. section=0;
  2217. } else if (drop_mode_flags==DROP_MODE_INBETWEEN) {
  2218. section=pos.y<h/2?-1:1;
  2219. } else if (pos.y<h/4) {
  2220. section=-1;
  2221. } else if (pos.y>=(h*3/4)) {
  2222. section=1;
  2223. } else {
  2224. section=0;
  2225. }
  2226. for(int i=0;i<columns.size();i++) {
  2227. int w = get_column_width(i);
  2228. if (pos.x < w) {
  2229. r_column=i;
  2230. return p_item;
  2231. }
  2232. pos.x-=w;
  2233. }
  2234. return NULL;
  2235. } else {
  2236. pos.y-=h;
  2237. }
  2238. } else {
  2239. h=0;
  2240. }
  2241. if (p_item->is_collapsed())
  2242. return NULL; // do not try childs, it's collapsed
  2243. TreeItem *n = p_item->get_children();
  2244. while(n) {
  2245. int ch;
  2246. TreeItem *r = _find_item_at_pos(n,pos,r_column,ch,section);
  2247. pos.y-=ch;
  2248. h+=ch;
  2249. if (r)
  2250. return r;
  2251. n=n->get_next();
  2252. }
  2253. return NULL;
  2254. }
  2255. int Tree::get_column_at_pos(const Point2& p_pos) const {
  2256. if (root) {
  2257. Point2 pos=p_pos;
  2258. pos -= cache.bg->get_offset();
  2259. pos.y-=_get_title_button_height();
  2260. if (pos.y<0)
  2261. return -1;
  2262. if (h_scroll->is_visible())
  2263. pos.x+=h_scroll->get_val();
  2264. if (v_scroll->is_visible())
  2265. pos.y+=v_scroll->get_val();
  2266. int col,h,section;
  2267. TreeItem *it = _find_item_at_pos(root,pos,col,h,section);
  2268. if (it) {
  2269. return col;
  2270. }
  2271. }
  2272. return -1;
  2273. }
  2274. int Tree::get_drop_section_at_pos(const Point2& p_pos) const {
  2275. if (root) {
  2276. Point2 pos=p_pos;
  2277. pos -= cache.bg->get_offset();
  2278. pos.y-=_get_title_button_height();
  2279. if (pos.y<0)
  2280. return -100;
  2281. if (h_scroll->is_visible())
  2282. pos.x+=h_scroll->get_val();
  2283. if (v_scroll->is_visible())
  2284. pos.y+=v_scroll->get_val();
  2285. int col,h,section;
  2286. TreeItem *it = _find_item_at_pos(root,pos,col,h,section);
  2287. if (it) {
  2288. return section;
  2289. }
  2290. }
  2291. return -100;
  2292. }
  2293. TreeItem* Tree::get_item_at_pos(const Point2& p_pos) const {
  2294. if (root) {
  2295. Point2 pos=p_pos;
  2296. pos -= cache.bg->get_offset();
  2297. pos.y-=_get_title_button_height();
  2298. if (pos.y<0)
  2299. return NULL;
  2300. if (h_scroll->is_visible())
  2301. pos.x+=h_scroll->get_val();
  2302. if (v_scroll->is_visible())
  2303. pos.y+=v_scroll->get_val();
  2304. int col,h,section;
  2305. TreeItem *it = _find_item_at_pos(root,pos,col,h,section);
  2306. if (it) {
  2307. return it;
  2308. }
  2309. }
  2310. return NULL;
  2311. }
  2312. String Tree::get_tooltip(const Point2& p_pos) const {
  2313. if (root) {
  2314. Point2 pos=p_pos;
  2315. pos -= cache.bg->get_offset();
  2316. pos.y-=_get_title_button_height();
  2317. if (pos.y<0)
  2318. return Control::get_tooltip(p_pos);
  2319. if (h_scroll->is_visible())
  2320. pos.x+=h_scroll->get_val();
  2321. if (v_scroll->is_visible())
  2322. pos.y+=v_scroll->get_val();
  2323. int col,h,section;
  2324. TreeItem *it = _find_item_at_pos(root,pos,col,h,section);
  2325. if (it) {
  2326. String ret;
  2327. if (it->get_tooltip(col)=="")
  2328. ret=it->get_text(col);
  2329. else
  2330. ret=it->get_tooltip(col);
  2331. return ret;
  2332. }
  2333. }
  2334. return Control::get_tooltip(p_pos);
  2335. }
  2336. void Tree::set_cursor_can_exit_tree(bool p_enable) {
  2337. cursor_can_exit_tree=p_enable;
  2338. }
  2339. bool Tree::can_cursor_exit_tree() const {
  2340. return cursor_can_exit_tree;
  2341. }
  2342. void Tree::set_hide_folding(bool p_hide) {
  2343. hide_folding=p_hide;
  2344. update();
  2345. }
  2346. bool Tree::is_folding_hidden() const {
  2347. return hide_folding;
  2348. }
  2349. void Tree::set_value_evaluator(ValueEvaluator *p_evaluator) {
  2350. evaluator = p_evaluator;
  2351. }
  2352. void Tree::set_drop_mode_flags(int p_flags) {
  2353. drop_mode_flags=p_flags;
  2354. if (drop_mode_flags==0) {
  2355. drop_mode_over=NULL;
  2356. }
  2357. update();
  2358. }
  2359. int Tree::get_drop_mode_flags() const {
  2360. return drop_mode_flags;
  2361. }
  2362. void Tree::_bind_methods() {
  2363. ObjectTypeDB::bind_method(_MD("_range_click_timeout"),&Tree::_range_click_timeout);
  2364. ObjectTypeDB::bind_method(_MD("_input_event"),&Tree::_input_event);
  2365. ObjectTypeDB::bind_method(_MD("_popup_select"),&Tree::popup_select);
  2366. ObjectTypeDB::bind_method(_MD("_text_editor_enter"),&Tree::text_editor_enter);
  2367. ObjectTypeDB::bind_method(_MD("_text_editor_modal_close"),&Tree::_text_editor_modal_close);
  2368. ObjectTypeDB::bind_method(_MD("_value_editor_changed"),&Tree::value_editor_changed);
  2369. ObjectTypeDB::bind_method(_MD("_scroll_moved"),&Tree::_scroll_moved);
  2370. ObjectTypeDB::bind_method(_MD("clear"),&Tree::clear);
  2371. ObjectTypeDB::bind_method(_MD("create_item:TreeItem","parent:TreeItem"),&Tree::_create_item,DEFVAL(Variant()));
  2372. ObjectTypeDB::bind_method(_MD("get_root:TreeItem"),&Tree::get_root);
  2373. ObjectTypeDB::bind_method(_MD("set_column_min_width","column","min_width"),&Tree::set_column_min_width);
  2374. ObjectTypeDB::bind_method(_MD("set_column_expand","column","expand"),&Tree::set_column_expand);
  2375. ObjectTypeDB::bind_method(_MD("get_column_width","column"),&Tree::get_column_width);
  2376. ObjectTypeDB::bind_method(_MD("set_hide_root","enable"),&Tree::set_hide_root);
  2377. ObjectTypeDB::bind_method(_MD("get_next_selected:TreeItem","from:TreeItem"),&Tree::_get_next_selected);
  2378. ObjectTypeDB::bind_method(_MD("get_selected:TreeItem"),&Tree::get_selected);
  2379. ObjectTypeDB::bind_method(_MD("get_selected_column"),&Tree::get_selected_column);
  2380. ObjectTypeDB::bind_method(_MD("get_pressed_button"),&Tree::get_pressed_button);
  2381. ObjectTypeDB::bind_method(_MD("set_select_mode","mode"),&Tree::set_select_mode);
  2382. ObjectTypeDB::bind_method(_MD("set_columns","amount"),&Tree::set_columns);
  2383. ObjectTypeDB::bind_method(_MD("get_columns"),&Tree::get_columns);
  2384. ObjectTypeDB::bind_method(_MD("get_edited:TreeItem"),&Tree::get_edited);
  2385. ObjectTypeDB::bind_method(_MD("get_edited_column"),&Tree::get_edited_column);
  2386. ObjectTypeDB::bind_method(_MD("get_custom_popup_rect"),&Tree::get_custom_popup_rect);
  2387. ObjectTypeDB::bind_method(_MD("get_item_area_rect","item:TreeItem","column"),&Tree::_get_item_rect,DEFVAL(-1));
  2388. ObjectTypeDB::bind_method(_MD("get_item_at_pos:TreeItem","pos"),&Tree::get_item_at_pos);
  2389. ObjectTypeDB::bind_method(_MD("get_column_at_pos","pos"),&Tree::get_column_at_pos);
  2390. ObjectTypeDB::bind_method(_MD("ensure_cursor_is_visible"),&Tree::ensure_cursor_is_visible);
  2391. ObjectTypeDB::bind_method(_MD("set_column_titles_visible","visible"),&Tree::set_column_titles_visible);
  2392. ObjectTypeDB::bind_method(_MD("are_column_titles_visible"),&Tree::are_column_titles_visible);
  2393. ObjectTypeDB::bind_method(_MD("set_column_title","column","title"),&Tree::set_column_title);
  2394. ObjectTypeDB::bind_method(_MD("get_column_title","column"),&Tree::get_column_title);
  2395. ObjectTypeDB::bind_method(_MD("get_scroll"),&Tree::get_scroll);
  2396. ObjectTypeDB::bind_method(_MD("set_hide_folding","hide"),&Tree::set_hide_folding);
  2397. ObjectTypeDB::bind_method(_MD("is_folding_hidden"),&Tree::is_folding_hidden);
  2398. ObjectTypeDB::bind_method(_MD("set_drop_mode_flags","flags"),&Tree::set_drop_mode_flags);
  2399. ObjectTypeDB::bind_method(_MD("get_drop_mode_flags"),&Tree::get_drop_mode_flags);
  2400. ADD_SIGNAL( MethodInfo("item_selected"));
  2401. ADD_SIGNAL( MethodInfo("cell_selected"));
  2402. ADD_SIGNAL( MethodInfo("multi_selected",PropertyInfo(Variant::OBJECT,"item"),PropertyInfo(Variant::INT,"column"),PropertyInfo(Variant::BOOL,"selected")) );
  2403. ADD_SIGNAL( MethodInfo("item_edited"));
  2404. ADD_SIGNAL( MethodInfo("item_collapsed",PropertyInfo(Variant::OBJECT,"item")));
  2405. //ADD_SIGNAL( MethodInfo("item_doubleclicked" ) );
  2406. ADD_SIGNAL( MethodInfo("button_pressed",PropertyInfo(Variant::OBJECT,"item"),PropertyInfo(Variant::INT,"column"),PropertyInfo(Variant::INT,"id")));
  2407. ADD_SIGNAL( MethodInfo("custom_popup_edited",PropertyInfo(Variant::BOOL,"arrow_clicked") ) );
  2408. ADD_SIGNAL( MethodInfo("item_activated"));
  2409. BIND_CONSTANT( SELECT_SINGLE );
  2410. BIND_CONSTANT( SELECT_ROW );
  2411. BIND_CONSTANT( SELECT_MULTI );
  2412. BIND_CONSTANT( DROP_MODE_DISABLED );
  2413. BIND_CONSTANT( DROP_MODE_ON_ITEM );
  2414. BIND_CONSTANT( DROP_MODE_INBETWEEN );
  2415. }
  2416. Tree::Tree() {
  2417. selected_col=0;
  2418. columns.resize(1);
  2419. selected_item=NULL;
  2420. edited_item=NULL;
  2421. selected_col=-1;
  2422. edited_col=-1;
  2423. hide_root=false;
  2424. select_mode=SELECT_SINGLE;
  2425. root=0;
  2426. popup_menu=NULL;
  2427. popup_edited_item=NULL;
  2428. text_editor=NULL;
  2429. set_focus_mode(FOCUS_ALL);
  2430. popup_menu = memnew( PopupMenu );
  2431. popup_menu->hide();
  2432. add_child(popup_menu);
  2433. popup_menu->set_as_toplevel(true);
  2434. text_editor = memnew( LineEdit );
  2435. add_child(text_editor);
  2436. text_editor->set_as_toplevel(true);
  2437. text_editor->hide();
  2438. value_editor = memnew( HSlider );
  2439. add_child(value_editor);
  2440. value_editor->set_as_toplevel(true);
  2441. value_editor->hide();
  2442. h_scroll = memnew( HScrollBar );
  2443. v_scroll = memnew( VScrollBar );
  2444. add_child(h_scroll);
  2445. add_child(v_scroll);
  2446. range_click_timer = memnew( Timer );
  2447. range_click_timer->connect("timeout",this,"_range_click_timeout");
  2448. add_child(range_click_timer);
  2449. h_scroll->connect("value_changed", this,"_scroll_moved");
  2450. v_scroll->connect("value_changed", this,"_scroll_moved");
  2451. text_editor->connect("text_entered", this,"_text_editor_enter");
  2452. text_editor->connect("modal_close", this,"_text_editor_modal_close");
  2453. popup_menu->connect("item_pressed", this,"_popup_select");
  2454. value_editor->connect("value_changed", this,"_value_editor_changed");
  2455. value_editor->set_as_toplevel(true);
  2456. text_editor->set_as_toplevel(true);
  2457. updating_value_editor=false;
  2458. pressed_button=-1;
  2459. show_column_titles=false;
  2460. cache.click_type = Cache::CLICK_NONE;
  2461. cache.hover_type = Cache::CLICK_NONE;
  2462. cache.hover_index = -1;
  2463. cache.click_index=-1;
  2464. cache.click_id=-1;
  2465. cache.click_item=NULL;
  2466. cache.click_column=0;
  2467. last_keypress=0;
  2468. focus_in_id=0;
  2469. blocked=0;
  2470. cursor_can_exit_tree=true;
  2471. set_stop_mouse(true);
  2472. drag_speed=0;
  2473. drag_touching=false;
  2474. drag_touching_deaccel=false;
  2475. pressing_for_editor=false;
  2476. range_drag_enabled=false;
  2477. hide_folding=false;
  2478. evaluator=NULL;
  2479. drop_mode_flags=0;
  2480. drop_mode_over=NULL;
  2481. drop_mode_section=0;
  2482. single_select_defer=NULL;
  2483. }
  2484. Tree::~Tree() {
  2485. if (root) {
  2486. memdelete( root );
  2487. }
  2488. }