tree.cpp 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690
  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.draw_relationship_lines=get_constant("draw_relationship_lines");
  557. cache.relationship_line_color=get_color("relationship_line_color");
  558. cache.title_button = get_stylebox("title_button_normal");
  559. cache.title_button_pressed = get_stylebox("title_button_pressed");
  560. cache.title_button_hover = get_stylebox("title_button_hover");
  561. cache.title_button_color = get_color("title_button_color");
  562. v_scroll->set_custom_step(cache.font->get_height());
  563. }
  564. int Tree::compute_item_height(TreeItem *p_item) const {
  565. if (p_item==root && hide_root)
  566. return 0;
  567. int height=cache.font->get_height();
  568. for (int i=0;i<columns.size();i++) {
  569. for(int j=0;j<p_item->cells[i].buttons.size();j++) {
  570. Size2i s;// = cache.button_pressed->get_minimum_size();
  571. s+= p_item->cells[i].buttons[j].texture->get_size();
  572. if (s.height>height)
  573. height=s.height;
  574. }
  575. switch(p_item->cells[i].mode) {
  576. case TreeItem::CELL_MODE_CHECK: {
  577. int check_icon_h = cache.checked->get_height();
  578. if (height<check_icon_h)
  579. height=check_icon_h;
  580. }
  581. case TreeItem::CELL_MODE_STRING:
  582. case TreeItem::CELL_MODE_CUSTOM:
  583. case TreeItem::CELL_MODE_ICON: {
  584. Ref<Texture> icon = p_item->cells[i].icon;
  585. if (!icon.is_null()) {
  586. Size2i s = p_item->cells[i].get_icon_size();
  587. if (p_item->cells[i].icon_max_w>0 && s.width > p_item->cells[i].icon_max_w ) {
  588. s.height=s.height * p_item->cells[i].icon_max_w / s.width;
  589. }
  590. if (s.height > height )
  591. height=s.height;
  592. }
  593. } break;
  594. default: {}
  595. }
  596. }
  597. height += cache.vseparation;
  598. return height;
  599. }
  600. int Tree::get_item_height(TreeItem *p_item) const {
  601. int height=compute_item_height(p_item);
  602. height+=cache.vseparation;
  603. if (!p_item->collapsed) { /* if not collapsed, check the childs */
  604. TreeItem *c=p_item->childs;
  605. while (c) {
  606. height += get_item_height( c );
  607. c=c->next;
  608. }
  609. }
  610. return height;
  611. }
  612. void Tree::draw_item_rect(const TreeItem::Cell& p_cell,const Rect2i& p_rect,const Color& p_color) {
  613. Rect2i rect=p_rect;
  614. RID ci = get_canvas_item();
  615. if (!p_cell.icon.is_null()) {
  616. Size2i bmsize = p_cell.get_icon_size();
  617. if (p_cell.icon_max_w>0 && bmsize.width > p_cell.icon_max_w) {
  618. bmsize.height = bmsize.height * p_cell.icon_max_w / bmsize.width;
  619. bmsize.width=p_cell.icon_max_w;
  620. }
  621. p_cell.draw_icon(ci,rect.pos + Size2i(0,Math::floor((rect.size.y-bmsize.y)/2)),bmsize);
  622. rect.pos.x+=bmsize.x+cache.hseparation;
  623. rect.size.x-=bmsize.x+cache.hseparation;
  624. }
  625. // if (p_tool)
  626. // rect.size.x-=Math::floor(rect.size.y/2);
  627. Ref<Font> font = cache.font;
  628. rect.pos.y+=Math::floor((rect.size.y-font->get_height())/2.0) +font->get_ascent();
  629. font->draw(ci,rect.pos,p_cell.text,p_color,rect.size.x);
  630. }
  631. #if 0
  632. 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) {
  633. RID ci = get_canvas_item();
  634. if (!p_icon.is_null()) {
  635. Size2i bmsize = p_icon->get_size();
  636. if (p_icon_max_w>0 && bmsize.width > p_icon_max_w) {
  637. bmsize.height = bmsize.height * p_icon_max_w / bmsize.width;
  638. bmsize.width=p_icon_max_w;
  639. }
  640. draw_texture_rect(p_icon,Rect2(p_rect.pos + Size2i(0,Math::floor((p_rect.size.y-bmsize.y)/2)),bmsize));
  641. p_rect.pos.x+=bmsize.x+cache.hseparation;
  642. p_rect.size.x-=bmsize.x+cache.hseparation;
  643. }
  644. if (p_tool)
  645. p_rect.size.x-=Math::floor(p_rect.size.y/2);
  646. Ref<Font> font = cache.font;
  647. p_rect.pos.y+=Math::floor((p_rect.size.y-font->get_height())/2.0) +font->get_ascent();
  648. font->draw(ci,p_rect.pos,p_text,p_color,p_rect.size.x);
  649. }
  650. #endif
  651. int Tree::draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& p_draw_size,TreeItem *p_item) {
  652. if (p_pos.y-cache.offset.y > (p_draw_size.height))
  653. return -1; //draw no more!
  654. RID ci = get_canvas_item();
  655. int htotal=0;
  656. int label_h=compute_item_height( p_item );
  657. /* Calculate height of the label part */
  658. label_h+=cache.vseparation;
  659. /* Draw label, if height fits */
  660. Point2i guide_from;
  661. bool skip=(p_item==root && hide_root);
  662. // printf("skip (%p == %p && %i) %i\n",p_item,root,hide_root,skip);
  663. if (!skip && (p_pos.y+label_h-cache.offset.y)>0) {
  664. // printf("entering\n");
  665. int height=label_h;
  666. Point2i guide_space=Point2i( cache.guide_width , height );
  667. if (!hide_folding && p_item->childs) { //has childs, draw the guide box
  668. Ref<Texture> arrow;
  669. if (p_item->collapsed) {
  670. arrow=cache.arrow_collapsed;
  671. } else {
  672. arrow=cache.arrow;
  673. }
  674. arrow->draw( ci , p_pos+p_draw_ofs+Point2i(0,(label_h-arrow->get_height())/2)-cache.offset);
  675. }
  676. //draw separation.
  677. // if (p_item->get_parent()!=root || !hide_root)
  678. Ref<Font> font = cache.font;
  679. int font_ascent=font->get_ascent();
  680. int ofs = p_pos.x + (hide_folding?cache.hseparation:cache.item_margin);
  681. for (int i=0;i<columns.size();i++) {
  682. int w = get_column_width(i);
  683. if (i==0) {
  684. w-=ofs;
  685. if (w<=0) {
  686. ofs=get_column_width(0);
  687. continue;
  688. }
  689. } else {
  690. ofs+=cache.hseparation;
  691. w-=cache.hseparation;
  692. }
  693. int bw=0;
  694. for(int j=p_item->cells[i].buttons.size()-1;j>=0;j--) {
  695. Ref<Texture> b=p_item->cells[i].buttons[j].texture;
  696. Size2 s = b->get_size() + cache.button_pressed->get_minimum_size();
  697. Point2i o = Point2i( ofs+w-s.width, p_pos.y )-cache.offset+p_draw_ofs;
  698. if (cache.click_type==Cache::CLICK_BUTTON && cache.click_item==p_item && cache.click_column==i && !p_item->cells[i].buttons[j].disabled) {
  699. //being pressed
  700. cache.button_pressed->draw(get_canvas_item(),Rect2(o,s));
  701. }
  702. o.y+=(label_h-s.height)/2;
  703. o+=cache.button_pressed->get_offset();
  704. b->draw(ci,o,p_item->cells[i].buttons[j].disabled?Color(1,1,1,0.5):Color(1,1,1,1));
  705. w-=s.width+cache.button_margin;
  706. bw+=s.width+cache.button_margin;
  707. }
  708. Rect2i item_rect = Rect2i( Point2i( ofs, p_pos.y )-cache.offset+p_draw_ofs, Size2i( w, label_h ));
  709. Rect2i cell_rect=item_rect;
  710. if (i!=0) {
  711. cell_rect.pos.x-=cache.hseparation;
  712. cell_rect.size.x+=cache.hseparation;
  713. }
  714. 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);
  715. if (i==0) {
  716. if (p_item->cells[0].selected && select_mode==SELECT_ROW) {
  717. 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 ));
  718. //Rect2 r = Rect2i(row_rect.pos,row_rect.size);
  719. //r.grow(cache.selected->get_margin(MARGIN_LEFT));
  720. if (has_focus())
  721. cache.selected_focus->draw(ci,row_rect );
  722. else
  723. cache.selected->draw(ci,row_rect );
  724. }
  725. }
  726. if (p_item->cells[i].selected && select_mode!=SELECT_ROW) {
  727. Rect2i r(item_rect.pos,item_rect.size);
  728. //r.grow(cache.selected->get_margin(MARGIN_LEFT));
  729. if (has_focus())
  730. cache.selected_focus->draw(ci,r );
  731. else
  732. cache.selected->draw(ci,r );
  733. }
  734. if (p_item->cells[i].custom_bg_color) {
  735. Rect2 r=cell_rect;
  736. r.pos.x-=cache.hseparation;
  737. r.size.x+=cache.hseparation;
  738. if (p_item->cells[i].custom_bg_outline) {
  739. 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);
  740. 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);
  741. 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);
  742. 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);
  743. } else {
  744. VisualServer::get_singleton()->canvas_item_add_rect(ci,r,p_item->cells[i].bg_color);
  745. }
  746. }
  747. if (drop_mode_flags && drop_mode_over==p_item) {
  748. Rect2 r=cell_rect;
  749. if (drop_mode_section==-1 || drop_mode_section==0) {
  750. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x,r.pos.y,r.size.x,1),cache.drop_position_color);
  751. }
  752. if (drop_mode_section==0) {
  753. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x,r.pos.y,1,r.size.y),cache.drop_position_color);
  754. 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);
  755. }
  756. if (drop_mode_section==1 || drop_mode_section==0) {
  757. 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);
  758. }
  759. }
  760. 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");
  761. Point2i text_pos=item_rect.pos;
  762. text_pos.y+=Math::floor((item_rect.size.y-font->get_height())/2) + font_ascent;
  763. switch (p_item->cells[i].mode) {
  764. case TreeItem::CELL_MODE_STRING: {
  765. draw_item_rect(p_item->cells[i],item_rect,col);
  766. } break;
  767. case TreeItem::CELL_MODE_CHECK: {
  768. Ref<Texture> checked = cache.checked;
  769. Ref<Texture> unchecked = cache.unchecked;
  770. Point2i check_ofs=item_rect.pos;
  771. check_ofs.y+=Math::floor((item_rect.size.y-checked->get_height())/2);
  772. if (p_item->cells[i].checked) {
  773. checked->draw( ci, check_ofs );
  774. } else {
  775. unchecked->draw( ci, check_ofs );
  776. }
  777. int check_w = checked->get_width()+cache.hseparation;
  778. text_pos.x+=check_w;
  779. item_rect.size.x-=check_w;
  780. item_rect.pos.x+=check_w;
  781. draw_item_rect(p_item->cells[i],item_rect,col);
  782. //font->draw( ci, text_pos, p_item->cells[i].text, col,item_rect.size.x-check_w );
  783. } break;
  784. case TreeItem::CELL_MODE_RANGE:
  785. case TreeItem::CELL_MODE_RANGE_EXPRESSION: {
  786. if (p_item->cells[i].text!="") {
  787. if (!p_item->cells[i].editable)
  788. break;
  789. int option = (int)p_item->cells[i].val;
  790. String s = p_item->cells[i].text;
  791. s=s.get_slicec(',',option);
  792. Ref<Texture> downarrow = cache.select_arrow;
  793. font->draw(ci, text_pos, s, col,item_rect.size.x-downarrow->get_width() );
  794. //?
  795. Point2i arrow_pos=item_rect.pos;
  796. arrow_pos.x+=item_rect.size.x-downarrow->get_width();
  797. arrow_pos.y+=Math::floor(((item_rect.size.y-downarrow->get_height()))/2.0);
  798. downarrow->draw( ci, arrow_pos );
  799. } else {
  800. Ref<Texture> updown = cache.updown;
  801. //String valtext = String::num( p_item->cells[i].val, Math::decimals( p_item->cells[i].step ) );
  802. String valtext = rtos( p_item->cells[i].val );
  803. font->draw( ci, text_pos, valtext, col, item_rect.size.x-updown->get_width());
  804. if (!p_item->cells[i].editable)
  805. break;
  806. Point2i updown_pos=item_rect.pos;
  807. updown_pos.x+=item_rect.size.x-updown->get_width();
  808. updown_pos.y+=Math::floor(((item_rect.size.y-updown->get_height()))/2.0);
  809. updown->draw( ci, updown_pos );
  810. }
  811. } break;
  812. case TreeItem::CELL_MODE_ICON: {
  813. if (p_item->cells[i].icon.is_null())
  814. break;
  815. Size2i icon_size = p_item->cells[i].get_icon_size();
  816. if (p_item->cells[i].icon_max_w>0 && icon_size.width >p_item->cells[i].icon_max_w) {
  817. icon_size.height = icon_size.height * p_item->cells[i].icon_max_w / icon_size.width;
  818. icon_size.width=p_item->cells[i].icon_max_w;
  819. }
  820. Point2i icon_ofs = (item_rect.size-icon_size)/2;
  821. icon_ofs+=item_rect.pos;
  822. draw_texture_rect(p_item->cells[i].icon,Rect2(icon_ofs,icon_size));;
  823. //p_item->cells[i].icon->draw(ci, icon_ofs);
  824. } break;
  825. case TreeItem::CELL_MODE_CUSTOM: {
  826. // int option = (int)p_item->cells[i].val;
  827. if (p_item->cells[i].custom_draw_obj) {
  828. Object* cdo = ObjectDB::get_instance(p_item->cells[i].custom_draw_obj);
  829. if (cdo)
  830. cdo->call(p_item->cells[i].custom_draw_callback,p_item,Rect2(item_rect));
  831. }
  832. if (!p_item->cells[i].editable) {
  833. draw_item_rect(p_item->cells[i],item_rect,col);
  834. break;
  835. }
  836. Ref<Texture> downarrow = cache.select_arrow;
  837. Rect2i ir=item_rect;
  838. ir.size.width-=downarrow->get_width();
  839. draw_item_rect(p_item->cells[i],ir,col);
  840. Point2i arrow_pos=item_rect.pos;
  841. arrow_pos.x+=item_rect.size.x-downarrow->get_width();
  842. arrow_pos.y+=Math::floor(((item_rect.size.y-downarrow->get_height()))/2.0);
  843. downarrow->draw( ci, arrow_pos );
  844. } break;
  845. }
  846. if (i==0) {
  847. ofs=get_column_width(0);
  848. } else {
  849. ofs+=w+bw;
  850. }
  851. if (select_mode==SELECT_MULTI && selected_item==p_item && selected_col==i) {
  852. if (has_focus())
  853. cache.cursor->draw(ci,cell_rect);
  854. else
  855. cache.cursor_unfocus->draw(ci,cell_rect);
  856. }
  857. }
  858. //separator
  859. //get_painter()->draw_fill_rect( Point2i(0,pos.y),Size2i(get_size().width,1),color( COLOR_TREE_GRID) );
  860. //pos=p_pos; //reset pos
  861. }
  862. Point2 children_pos=p_pos;
  863. if (!skip) {
  864. children_pos.x+=cache.item_margin;
  865. htotal+=label_h;
  866. children_pos.y+=htotal;
  867. }
  868. if (!p_item->collapsed) { /* if not collapsed, check the childs */
  869. TreeItem *c=p_item->childs;
  870. while (c) {
  871. if (cache.draw_relationship_lines == 1){
  872. int root_ofs = children_pos.x + (hide_folding?cache.hseparation:cache.item_margin);
  873. int parent_ofs = p_pos.x + (hide_folding?cache.hseparation:cache.item_margin);
  874. Point2i root_pos = Point2i(root_ofs, children_pos.y + label_h/2)-cache.offset+p_draw_ofs;
  875. if (c->get_children() > 0)
  876. root_pos -= Point2i(cache.arrow->get_width(),0);
  877. Point2i parent_pos = Point2i(parent_ofs - cache.arrow->get_width()/2, p_pos.y + label_h/2 + cache.arrow->get_height()/2)-cache.offset+p_draw_ofs;
  878. VisualServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x, root_pos.y), cache.relationship_line_color);
  879. VisualServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y), parent_pos, cache.relationship_line_color);
  880. }
  881. int child_h=draw_item(children_pos, p_draw_ofs, p_draw_size, c );
  882. if (child_h<0 && cache.draw_relationship_lines == 0)
  883. return -1; // break, stop drawing, no need to anymore
  884. htotal+=child_h;
  885. children_pos.y+=child_h;
  886. c=c->next;
  887. }
  888. }
  889. return htotal;
  890. }
  891. int Tree::_count_selected_items(TreeItem* p_from) const {
  892. int count=0;
  893. for(int i=0;i<columns.size();i++) {
  894. if (p_from->is_selected(i))
  895. count++;
  896. }
  897. if (p_from->get_children()) {
  898. count+=_count_selected_items(p_from->get_children());
  899. }
  900. if (p_from->get_next()) {
  901. count+=_count_selected_items(p_from->get_next());
  902. }
  903. return count;
  904. }
  905. void Tree::select_single_item(TreeItem *p_selected,TreeItem *p_current,int p_col,TreeItem *p_prev,bool *r_in_range) {
  906. TreeItem::Cell &selected_cell=p_selected->cells[p_col];
  907. bool switched=false;
  908. if (r_in_range && !*r_in_range && (p_current==p_selected || p_current==p_prev)) {
  909. *r_in_range=true;
  910. switched=true;
  911. }
  912. for (int i=0;i<columns.size();i++) {
  913. TreeItem::Cell &c=p_current->cells[i];
  914. if (!c.selectable)
  915. continue;
  916. if (select_mode==SELECT_ROW) {
  917. if (p_selected==p_current) {
  918. if (!c.selected) {
  919. c.selected=true;
  920. selected_item=p_selected;
  921. selected_col=0;
  922. selected_item=p_selected;
  923. emit_signal("item_selected");
  924. //if (p_col==i)
  925. // p_current->selected_signal.call(p_col);
  926. }
  927. } else {
  928. if (c.selected) {
  929. c.selected=false;
  930. //p_current->deselected_signal.call(p_col);
  931. }
  932. }
  933. } else if (select_mode==SELECT_SINGLE || select_mode==SELECT_MULTI) {
  934. if (!r_in_range && &selected_cell==&c) {
  935. if (!selected_cell.selected) {
  936. selected_cell.selected=true;
  937. selected_item=p_selected;
  938. selected_col=i;
  939. emit_signal("cell_selected");
  940. if (select_mode==SELECT_MULTI)
  941. emit_signal("multi_selected",p_current,i,true);
  942. } else if (select_mode==SELECT_MULTI && (selected_item!=p_selected || selected_col!=i)) {
  943. selected_item=p_selected;
  944. selected_col=i;
  945. emit_signal("cell_selected");
  946. }
  947. } else {
  948. if (r_in_range && *r_in_range) {
  949. if (!c.selected && c.selectable) {
  950. c.selected=true;
  951. emit_signal("multi_selected",p_current,i,true);
  952. }
  953. } else if (!r_in_range){
  954. if (select_mode==SELECT_MULTI && c.selected)
  955. emit_signal("multi_selected",p_current,i,false);
  956. c.selected=false;
  957. }
  958. //p_current->deselected_signal.call(p_col);
  959. }
  960. }
  961. }
  962. if (!switched && r_in_range && *r_in_range && (p_current==p_selected || p_current==p_prev)) {
  963. *r_in_range=false;
  964. }
  965. TreeItem *c=p_current->childs;
  966. while (c) {
  967. select_single_item(p_selected,c,p_col,p_prev,r_in_range);
  968. c=c->next;
  969. }
  970. }
  971. Rect2 Tree::search_item_rect(TreeItem *p_from, TreeItem *p_item) {
  972. return Rect2();
  973. }
  974. void Tree::_range_click_timeout() {
  975. if (range_item_last && !range_drag_enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
  976. Point2 pos = get_local_mouse_pos()-cache.bg->get_offset();
  977. if (show_column_titles) {
  978. pos.y-=_get_title_button_height();
  979. if (pos.y<0) {
  980. range_click_timer->stop();
  981. return;
  982. }
  983. }
  984. click_handled=false;
  985. InputModifierState mod = InputModifierState(); // should be irrelevant..
  986. blocked++;
  987. propagate_mouse_event(pos+cache.offset, 0, 0, false, root, BUTTON_LEFT, mod);
  988. blocked--;
  989. if (range_click_timer->is_one_shot()) {
  990. range_click_timer->set_wait_time(0.05);
  991. range_click_timer->set_one_shot(false);
  992. range_click_timer->start();
  993. }
  994. if (!click_handled)
  995. range_click_timer->stop();
  996. } else {
  997. range_click_timer->stop();
  998. }
  999. }
  1000. 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) {
  1001. int item_h=compute_item_height( p_item )+cache.vseparation;
  1002. bool skip=(p_item==root && hide_root);
  1003. if (!skip && p_pos.y<item_h) {
  1004. // check event!
  1005. if (range_click_timer->get_time_left() > 0 && p_item != range_item_last) {
  1006. return -1;
  1007. }
  1008. if (!hide_folding && (p_pos.x >=x_ofs && p_pos.x < (x_ofs+cache.item_margin) )) {
  1009. if (p_item->childs)
  1010. p_item->set_collapsed( ! p_item->is_collapsed() );
  1011. return -1; //handled!
  1012. }
  1013. int x=p_pos.x;
  1014. /* find clicked column */
  1015. int col=-1;
  1016. int col_ofs=0;
  1017. int col_width=0;
  1018. for (int i=0;i<columns.size();i++) {
  1019. col_width=get_column_width(i);
  1020. if (x>col_width) {
  1021. col_ofs+=col_width;
  1022. x-=col_width;
  1023. continue;
  1024. }
  1025. col=i;
  1026. break;
  1027. }
  1028. if (col==-1)
  1029. return -1;
  1030. else if (col==0) {
  1031. int margin=x_ofs+cache.item_margin;//-cache.hseparation;
  1032. //int lm = cache.bg->get_margin(MARGIN_LEFT);
  1033. col_width-=margin;
  1034. col_ofs+=margin;
  1035. x-=margin;
  1036. } else {
  1037. col_width-=cache.hseparation;
  1038. x-=cache.hseparation;
  1039. }
  1040. TreeItem::Cell &c = p_item->cells[col];
  1041. bool already_selected=c.selected;
  1042. bool already_cursor=(p_item==selected_item) && col == selected_col;
  1043. for(int j=c.buttons.size()-1;j>=0;j--) {
  1044. Ref<Texture> b=c.buttons[j].texture;
  1045. int w = b->get_size().width + cache.button_pressed->get_minimum_size().width;
  1046. if (x>col_width-w) {
  1047. if (c.buttons[j].disabled) {
  1048. pressed_button=-1;
  1049. cache.click_type=Cache::CLICK_NONE;
  1050. return -1;
  1051. }
  1052. pressed_button=j;
  1053. cache.click_type=Cache::CLICK_BUTTON;
  1054. cache.click_index=j;
  1055. cache.click_id=c.buttons[j].id;
  1056. cache.click_item=p_item;
  1057. cache.click_column=col;
  1058. update();
  1059. //emit_signal("button_pressed");
  1060. return -1;
  1061. }
  1062. col_width-=w+cache.button_margin;
  1063. }
  1064. if (p_button==BUTTON_LEFT || (p_button==BUTTON_RIGHT && allow_rmb_select)) {
  1065. /* process selection */
  1066. if (p_doubleclick && (!c.editable || c.mode==TreeItem::CELL_MODE_CUSTOM || c.mode==TreeItem::CELL_MODE_ICON /*|| c.mode==TreeItem::CELL_MODE_CHECK*/)) { //it' s confusing for check
  1067. emit_signal("item_activated");
  1068. return -1;
  1069. }
  1070. if (select_mode==SELECT_MULTI && p_mod.command && c.selectable) {
  1071. if (!c.selected || p_button==BUTTON_RIGHT) {
  1072. p_item->select(col);
  1073. emit_signal("multi_selected",p_item,col,true);
  1074. if (p_button==BUTTON_RIGHT) {
  1075. emit_signal("item_rmb_selected",get_local_mouse_pos());
  1076. }
  1077. //p_item->selected_signal.call(col);
  1078. } else {
  1079. p_item->deselect(col);
  1080. emit_signal("multi_selected",p_item,col,false);
  1081. //p_item->deselected_signal.call(col);
  1082. }
  1083. } else {
  1084. if (c.selectable) {
  1085. if (select_mode==SELECT_MULTI && p_mod.shift && selected_item && selected_item!=p_item) {
  1086. bool inrange=false;
  1087. select_single_item( p_item, root, col,selected_item,&inrange );
  1088. if (p_button==BUTTON_RIGHT) {
  1089. emit_signal("item_rmb_selected",get_local_mouse_pos());
  1090. }
  1091. } else {
  1092. int icount = _count_selected_items(root);
  1093. if (select_mode==SELECT_MULTI && icount>1 && p_button!=BUTTON_RIGHT) {
  1094. single_select_defer=p_item;
  1095. single_select_defer_column=col;
  1096. } else {
  1097. if (p_button!=BUTTON_RIGHT || !c.selected) {
  1098. select_single_item( p_item, root, col );
  1099. }
  1100. if (p_button==BUTTON_RIGHT) {
  1101. emit_signal("item_rmb_selected",get_local_mouse_pos());
  1102. }
  1103. }
  1104. }
  1105. //if (!c.selected && select_mode==SELECT_MULTI) {
  1106. // emit_signal("multi_selected",p_item,col,true);
  1107. //}
  1108. update();
  1109. }
  1110. }
  1111. }
  1112. if (!c.editable)
  1113. return -1; // if cell is not editable, don't bother
  1114. /* editing */
  1115. bool bring_up_editor=force_select_on_already_selected ? (c.selected && already_selected) : c.selected;
  1116. bool bring_up_value_editor=false;
  1117. String editor_text=c.text;
  1118. switch (c.mode) {
  1119. case TreeItem::CELL_MODE_STRING: {
  1120. //nothing in particular
  1121. if (select_mode==SELECT_MULTI && (get_tree()->get_last_event_id() == focus_in_id || !already_cursor)) {
  1122. bring_up_editor=false;
  1123. }
  1124. } break;
  1125. case TreeItem::CELL_MODE_CHECK: {
  1126. Ref<Texture> checked = cache.checked;
  1127. bring_up_editor=false; //checkboxes are not edited with editor
  1128. if (x>=0 && x<= checked->get_width()+cache.hseparation ) {
  1129. p_item->set_checked(col,!c.checked);
  1130. item_edited(col,p_item);
  1131. click_handled=true;
  1132. //p_item->edited_signal.call(col);
  1133. }
  1134. } break;
  1135. case TreeItem::CELL_MODE_RANGE:
  1136. case TreeItem::CELL_MODE_RANGE_EXPRESSION: {
  1137. if (c.text!="") {
  1138. //if (x >= (get_column_width(col)-item_h/2)) {
  1139. popup_menu->clear();
  1140. for (int i=0;i<c.text.get_slice_count(",");i++) {
  1141. String s = c.text.get_slicec(',',i);
  1142. popup_menu->add_item(s,i);
  1143. }
  1144. popup_menu->set_size(Size2(col_width,0));
  1145. popup_menu->set_pos( get_global_pos() + Point2i(col_ofs,_get_title_button_height()+y_ofs+item_h)-cache.offset );
  1146. popup_menu->popup();
  1147. popup_edited_item=p_item;
  1148. popup_edited_item_col=col;
  1149. //}
  1150. bring_up_editor=false;
  1151. } else {
  1152. Ref<Texture> updown = cache.updown;
  1153. if (x >= (col_width-item_h/2)) {
  1154. /* touching the combo */
  1155. bool up=p_pos.y < (item_h /2);
  1156. if (p_button==BUTTON_LEFT) {
  1157. if (range_click_timer->get_time_left() == 0) {
  1158. range_item_last=p_item;
  1159. range_up_last=up;
  1160. range_click_timer->set_wait_time(0.6);
  1161. range_click_timer->set_one_shot(true);
  1162. range_click_timer->start();
  1163. } else if (up != range_up_last) {
  1164. return -1; // break. avoid changing direction on mouse held
  1165. }
  1166. p_item->set_range( col, c.val + (up?1.0:-1.0) * c.step );
  1167. item_edited(col,p_item);
  1168. } else if (p_button==BUTTON_RIGHT) {
  1169. p_item->set_range( col, (up?c.max:c.min) );
  1170. item_edited(col,p_item);
  1171. } else if (p_button==BUTTON_WHEEL_UP) {
  1172. p_item->set_range( col, c.val + c.step );
  1173. item_edited(col,p_item);
  1174. } else if (p_button==BUTTON_WHEEL_DOWN) {
  1175. p_item->set_range( col, c.val - c.step );
  1176. item_edited(col,p_item);
  1177. }
  1178. //p_item->edited_signal.call(col);
  1179. bring_up_editor=false;
  1180. } else {
  1181. editor_text=String::num( p_item->cells[col].val, Math::decimals( p_item->cells[col].step ) );
  1182. bring_up_value_editor=false;
  1183. if (select_mode==SELECT_MULTI && get_tree()->get_last_event_id() == focus_in_id)
  1184. bring_up_editor=false;
  1185. }
  1186. }
  1187. click_handled=true;
  1188. } break;
  1189. case TreeItem::CELL_MODE_ICON: {
  1190. bring_up_editor=false;
  1191. } break;
  1192. case TreeItem::CELL_MODE_CUSTOM: {
  1193. edited_item=p_item;
  1194. edited_col=col;
  1195. 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));
  1196. emit_signal("custom_popup_edited",((bool)(x >= (col_width-item_h/2))));
  1197. bring_up_editor=false;
  1198. item_edited(col,p_item);
  1199. click_handled=true;
  1200. return -1;
  1201. } break;
  1202. };
  1203. if (!bring_up_editor || p_button!=BUTTON_LEFT)
  1204. return -1;
  1205. click_handled=true;
  1206. popup_edited_item=p_item;
  1207. popup_edited_item_col=col;
  1208. pressing_item_rect=Rect2(get_global_pos() + Point2i(col_ofs,_get_title_button_height()+y_ofs)-cache.offset,Size2(col_width,item_h));
  1209. pressing_for_editor_text=editor_text;
  1210. pressing_for_editor=true;
  1211. return -1; //select
  1212. } else {
  1213. Point2i new_pos=p_pos;
  1214. if (!skip) {
  1215. x_ofs+=cache.item_margin;
  1216. //new_pos.x-=cache.item_margin;
  1217. y_ofs+=item_h;
  1218. new_pos.y-=item_h;
  1219. }
  1220. if (!p_item->collapsed) { /* if not collapsed, check the childs */
  1221. TreeItem *c=p_item->childs;
  1222. while (c) {
  1223. int child_h=propagate_mouse_event( new_pos,x_ofs,y_ofs,p_doubleclick,c,p_button,p_mod);
  1224. if (child_h<0)
  1225. return -1; // break, stop propagating, no need to anymore
  1226. new_pos.y-=child_h;
  1227. y_ofs+=child_h;
  1228. c=c->next;
  1229. item_h+=child_h;
  1230. }
  1231. }
  1232. }
  1233. return item_h; // nothing found
  1234. }
  1235. void Tree::_text_editor_modal_close() {
  1236. if ( Input::get_singleton()->is_key_pressed(KEY_ESCAPE) ||
  1237. Input::get_singleton()->is_key_pressed(KEY_ENTER) ||
  1238. Input::get_singleton()->is_key_pressed(KEY_RETURN) ) {
  1239. return;
  1240. }
  1241. if (value_editor->has_point(value_editor->get_local_mouse_pos()))
  1242. return;
  1243. text_editor_enter(text_editor->get_text());
  1244. }
  1245. void Tree::text_editor_enter(String p_text) {
  1246. text_editor->hide();
  1247. value_editor->hide();
  1248. if (!popup_edited_item)
  1249. return;
  1250. if (popup_edited_item_col<0 || popup_edited_item_col>columns.size())
  1251. return;
  1252. TreeItem::Cell &c=popup_edited_item->cells[popup_edited_item_col];
  1253. switch( c.mode ) {
  1254. case TreeItem::CELL_MODE_STRING: {
  1255. c.text=p_text;
  1256. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1257. } break;
  1258. case TreeItem::CELL_MODE_RANGE: {
  1259. c.val=p_text.to_double();
  1260. if (c.step>0)
  1261. c.val=Math::stepify(c.val,c.step);
  1262. if (c.val<c.min)
  1263. c.val=c.min;
  1264. else if (c.val>c.max)
  1265. c.val=c.max;
  1266. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1267. } break;
  1268. case TreeItem::CELL_MODE_RANGE_EXPRESSION: {
  1269. if(evaluator)
  1270. c.val=evaluator->eval(p_text);
  1271. else
  1272. c.val=p_text.to_double();
  1273. } break;
  1274. default: { ERR_FAIL(); }
  1275. }
  1276. item_edited(popup_edited_item_col,popup_edited_item);
  1277. update();
  1278. }
  1279. void Tree::value_editor_changed(double p_value) {
  1280. if (updating_value_editor) {
  1281. return;
  1282. }
  1283. if (!popup_edited_item) {
  1284. return;
  1285. }
  1286. TreeItem::Cell &c=popup_edited_item->cells[popup_edited_item_col];
  1287. c.val=p_value;
  1288. item_edited(popup_edited_item_col,popup_edited_item);
  1289. update();
  1290. }
  1291. void Tree::popup_select(int p_option) {
  1292. if (!popup_edited_item)
  1293. return;
  1294. if (popup_edited_item_col<0 || popup_edited_item_col>columns.size())
  1295. return;
  1296. popup_edited_item->cells[popup_edited_item_col].val=p_option;
  1297. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1298. update();
  1299. item_edited(popup_edited_item_col,popup_edited_item);
  1300. }
  1301. void Tree::_input_event(InputEvent p_event) {
  1302. switch (p_event.type) {
  1303. case InputEvent::KEY: {
  1304. if (!p_event.key.pressed)
  1305. break;
  1306. 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)
  1307. break;
  1308. if (!root)
  1309. return;
  1310. if (hide_root && !root->get_next_visible())
  1311. return;
  1312. switch(p_event.key.scancode) {
  1313. #define EXIT_BREAK { if (!cursor_can_exit_tree) accept_event(); break; }
  1314. case KEY_RIGHT: {
  1315. //TreeItem *next = NULL;
  1316. if (!selected_item)
  1317. break;
  1318. if (select_mode==SELECT_ROW)
  1319. EXIT_BREAK;
  1320. if (selected_col>=(columns.size()-1))
  1321. EXIT_BREAK;
  1322. if (select_mode==SELECT_MULTI) {
  1323. selected_col++;
  1324. emit_signal("cell_selected");
  1325. } else {
  1326. selected_item->select(selected_col+1);
  1327. }
  1328. update();
  1329. ensure_cursor_is_visible();
  1330. accept_event();
  1331. } break;
  1332. case KEY_LEFT: {
  1333. // TreeItem *next = NULL;
  1334. if (!selected_item)
  1335. break;
  1336. if (select_mode==SELECT_ROW)
  1337. EXIT_BREAK;
  1338. if (selected_col<=0)
  1339. EXIT_BREAK;
  1340. if (select_mode==SELECT_MULTI) {
  1341. selected_col--;
  1342. emit_signal("cell_selected");
  1343. } else {
  1344. selected_item->select(selected_col-1);
  1345. }
  1346. update();
  1347. accept_event();
  1348. } break;
  1349. case KEY_DOWN: {
  1350. TreeItem *next = NULL;
  1351. if (!selected_item) {
  1352. next=hide_root?root->get_next_visible():root;
  1353. selected_item=0;
  1354. } else {
  1355. next=selected_item->get_next_visible();
  1356. // if (diff < uint64_t(GLOBAL_DEF("gui/incr_search_max_interval_msec",2000))) {
  1357. if (last_keypress!=0) {
  1358. //incr search next
  1359. int col;
  1360. next=_search_item_text(next,incr_search,&col,true);
  1361. if (!next) {
  1362. accept_event();
  1363. return;
  1364. }
  1365. }
  1366. }
  1367. if (select_mode==SELECT_MULTI) {
  1368. if (!next)
  1369. EXIT_BREAK;
  1370. selected_item=next;
  1371. emit_signal("cell_selected");
  1372. update();
  1373. } else {
  1374. int col=selected_col<0?0:selected_col;
  1375. while (next && !next->cells[col].selectable)
  1376. next=next->get_next_visible();
  1377. if (!next)
  1378. EXIT_BREAK; // do nothing..
  1379. next->select(col);
  1380. }
  1381. ensure_cursor_is_visible();
  1382. accept_event();
  1383. } break;
  1384. case KEY_UP: {
  1385. TreeItem *prev = NULL;
  1386. if (!selected_item) {
  1387. prev = get_last_item();
  1388. selected_col=0;
  1389. } else {
  1390. prev=selected_item->get_prev_visible();
  1391. if (last_keypress!=0) {
  1392. //incr search next
  1393. int col;
  1394. prev=_search_item_text(prev,incr_search,&col,true,true);
  1395. if (!prev) {
  1396. accept_event();
  1397. return;
  1398. }
  1399. }
  1400. }
  1401. if (select_mode==SELECT_MULTI) {
  1402. if (!prev)
  1403. break;
  1404. selected_item=prev;
  1405. emit_signal("cell_selected");
  1406. update();
  1407. } else {
  1408. int col=selected_col<0?0:selected_col;
  1409. while (prev && !prev->cells[col].selectable)
  1410. prev=prev->get_prev_visible();
  1411. if (!prev)
  1412. break; // do nothing..
  1413. prev->select(col);
  1414. }
  1415. ensure_cursor_is_visible();
  1416. accept_event();
  1417. } break;
  1418. case KEY_PAGEDOWN: {
  1419. TreeItem *next = NULL;
  1420. if (!selected_item)
  1421. break;
  1422. next=selected_item;
  1423. for(int i=0;i<10;i++) {
  1424. TreeItem *_n = next->get_next_visible();
  1425. if (_n) {
  1426. next=_n;
  1427. } else {
  1428. break;
  1429. }
  1430. }
  1431. if (next==selected_item)
  1432. break;
  1433. if (select_mode==SELECT_MULTI) {
  1434. selected_item=next;
  1435. emit_signal("cell_selected");
  1436. update();
  1437. } else {
  1438. while (next && !next->cells[selected_col].selectable)
  1439. next=next->get_next_visible();
  1440. if (!next)
  1441. EXIT_BREAK; // do nothing..
  1442. next->select(selected_col);
  1443. }
  1444. ensure_cursor_is_visible();
  1445. } break;
  1446. case KEY_PAGEUP: {
  1447. TreeItem *prev = NULL;
  1448. if (!selected_item)
  1449. break;
  1450. prev=selected_item;
  1451. for(int i=0;i<10;i++) {
  1452. TreeItem *_n = prev->get_prev_visible();
  1453. if (_n) {
  1454. prev=_n;
  1455. } else {
  1456. break;
  1457. }
  1458. }
  1459. if (prev==selected_item)
  1460. break;
  1461. if (select_mode==SELECT_MULTI) {
  1462. selected_item=prev;
  1463. emit_signal("cell_selected");
  1464. update();
  1465. } else {
  1466. while (prev && !prev->cells[selected_col].selectable)
  1467. prev=prev->get_prev_visible();
  1468. if (!prev)
  1469. EXIT_BREAK; // do nothing..
  1470. prev->select(selected_col);
  1471. }
  1472. ensure_cursor_is_visible();
  1473. } break;
  1474. case KEY_F2:
  1475. case KEY_RETURN:
  1476. case KEY_ENTER: {
  1477. if (selected_item) {
  1478. //bring up editor if possible
  1479. if (!edit_selected()) {
  1480. emit_signal("item_activated");
  1481. }
  1482. }
  1483. accept_event();
  1484. } break;
  1485. case KEY_SPACE: {
  1486. if (select_mode==SELECT_MULTI) {
  1487. if (!selected_item)
  1488. break;
  1489. if (selected_item->is_selected(selected_col)) {
  1490. selected_item->deselect(selected_col);
  1491. emit_signal("multi_selected",selected_item,selected_col,false);
  1492. } else if (selected_item->is_selectable(selected_col)) {
  1493. selected_item->select(selected_col);
  1494. emit_signal("multi_selected",selected_item,selected_col,true);
  1495. }
  1496. }
  1497. accept_event();
  1498. } break;
  1499. default: {
  1500. if (p_event.key.unicode>0) {
  1501. _do_incr_search(String::chr(p_event.key.unicode));
  1502. accept_event();
  1503. return;
  1504. } else {
  1505. if (p_event.key.scancode!=KEY_SHIFT)
  1506. last_keypress=0;
  1507. }
  1508. } break;
  1509. last_keypress=0;
  1510. }
  1511. } break;
  1512. case InputEvent::MOUSE_MOTION: {
  1513. if (cache.font.is_null()) // avoid a strange case that may fuckup stuff
  1514. update_cache();
  1515. const InputEventMouseMotion& b=p_event.mouse_motion;
  1516. Ref<StyleBox> bg = cache.bg;
  1517. Point2 pos = Point2(b.x,b.y) - bg->get_offset();
  1518. Cache::ClickType old_hover = cache.hover_type;
  1519. int old_index = cache.hover_index;
  1520. cache.hover_type=Cache::CLICK_NONE;
  1521. cache.hover_index=0;
  1522. if (show_column_titles) {
  1523. pos.y-=_get_title_button_height();
  1524. if (pos.y<0) {
  1525. pos.x+=cache.offset.x;
  1526. int len=0;
  1527. for(int i=0;i<columns.size();i++) {
  1528. len+=get_column_width(i);
  1529. if (pos.x<len) {
  1530. cache.hover_type=Cache::CLICK_TITLE;
  1531. cache.hover_index=i;
  1532. update();
  1533. break;
  1534. }
  1535. }
  1536. }
  1537. }
  1538. if (drop_mode_flags && root) {
  1539. Point2 mpos=Point2(b.x,b.y);
  1540. mpos -= cache.bg->get_offset();
  1541. mpos.y-=_get_title_button_height();
  1542. if (mpos.y>=0) {
  1543. if (h_scroll->is_visible())
  1544. mpos.x+=h_scroll->get_val();
  1545. if (v_scroll->is_visible())
  1546. mpos.y+=v_scroll->get_val();
  1547. int col,h,section;
  1548. TreeItem *it = _find_item_at_pos(root,mpos,col,h,section);
  1549. if (it!=drop_mode_over || section!=drop_mode_section) {
  1550. drop_mode_over=it;
  1551. drop_mode_section=section;
  1552. update();
  1553. }
  1554. }
  1555. }
  1556. if (cache.hover_type!=old_hover || cache.hover_index!=old_index) {
  1557. update();
  1558. }
  1559. if (pressing_for_editor && popup_edited_item && popup_edited_item->get_cell_mode(popup_edited_item_col)==TreeItem::CELL_MODE_RANGE) {
  1560. //range drag
  1561. if (!range_drag_enabled) {
  1562. Vector2 cpos = Vector2(b.x,b.y);
  1563. if (cpos.distance_to(pressing_pos)>2) {
  1564. range_drag_enabled=true;
  1565. range_drag_capture_pos=cpos;
  1566. range_drag_base=popup_edited_item->get_range(popup_edited_item_col);
  1567. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  1568. }
  1569. } else {
  1570. TreeItem::Cell &c=popup_edited_item->cells[popup_edited_item_col];
  1571. float diff_y = -b.relative_y;
  1572. diff_y=Math::pow(ABS(diff_y),1.8)*SGN(diff_y);
  1573. diff_y*=0.1;
  1574. range_drag_base=CLAMP(range_drag_base + c.step * diff_y, c.min, c.max);
  1575. popup_edited_item->set_range(popup_edited_item_col,range_drag_base);
  1576. item_edited(popup_edited_item_col,popup_edited_item);
  1577. }
  1578. }
  1579. if (drag_touching && ! drag_touching_deaccel) {
  1580. drag_accum-=b.relative_y;
  1581. v_scroll->set_val(drag_from+drag_accum);
  1582. drag_speed=-b.speed_y;
  1583. }
  1584. } break;
  1585. case InputEvent::MOUSE_BUTTON: {
  1586. if (cache.font.is_null()) // avoid a strange case that may fuckup stuff
  1587. update_cache();
  1588. const InputEventMouseButton& b=p_event.mouse_button;
  1589. if (!b.pressed) {
  1590. if (b.button_index==BUTTON_LEFT) {
  1591. if (single_select_defer) {
  1592. select_single_item( single_select_defer, root, single_select_defer_column );
  1593. single_select_defer=NULL;
  1594. }
  1595. range_click_timer->stop();
  1596. if (pressing_for_editor) {
  1597. if (range_drag_enabled) {
  1598. range_drag_enabled=false;
  1599. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  1600. warp_mouse(range_drag_capture_pos);
  1601. } else
  1602. edit_selected();
  1603. pressing_for_editor=false;
  1604. }
  1605. if (cache.click_type==Cache::CLICK_BUTTON) {
  1606. emit_signal("button_pressed",cache.click_item,cache.click_column,cache.click_id);
  1607. }
  1608. cache.click_type=Cache::CLICK_NONE;
  1609. cache.click_index=-1;
  1610. cache.click_id=-1;
  1611. cache.click_item=NULL;
  1612. cache.click_column=0;
  1613. if (drag_touching) {
  1614. if (drag_speed==0) {
  1615. drag_touching_deaccel=false;
  1616. drag_touching=false;
  1617. set_fixed_process(false);
  1618. } else {
  1619. drag_touching_deaccel=true;
  1620. }
  1621. }
  1622. update();
  1623. }
  1624. break;
  1625. }
  1626. switch(b.button_index) {
  1627. case BUTTON_RIGHT:
  1628. case BUTTON_LEFT: {
  1629. Ref<StyleBox> bg = cache.bg;
  1630. Point2 pos = Point2(b.x,b.y) - bg->get_offset();
  1631. cache.click_type=Cache::CLICK_NONE;
  1632. if (show_column_titles && b.button_index==BUTTON_LEFT) {
  1633. pos.y-=_get_title_button_height();
  1634. if (pos.y<0) {
  1635. pos.x+=cache.offset.x;
  1636. int len=0;
  1637. for(int i=0;i<columns.size();i++) {
  1638. len+=get_column_width(i);
  1639. if (pos.x<len) {
  1640. cache.click_type=Cache::CLICK_TITLE;
  1641. cache.click_index=i;
  1642. //cache.click_id=;
  1643. update();
  1644. break;
  1645. }
  1646. }
  1647. break;
  1648. }
  1649. }
  1650. if (!root || (!root->get_children() && hide_root)) {
  1651. if (b.button_index==BUTTON_RIGHT && allow_rmb_select) {
  1652. emit_signal("empty_tree_rmb_selected",get_local_mouse_pos());
  1653. }
  1654. break;
  1655. }
  1656. click_handled=false;
  1657. pressing_for_editor=false;
  1658. blocked++;
  1659. bool handled = propagate_mouse_event(pos+cache.offset,0,0,b.doubleclick,root,b.button_index,b.mod);
  1660. blocked--;
  1661. if (pressing_for_editor) {
  1662. pressing_pos=Point2(b.x,b.y);
  1663. }
  1664. if (b.button_index==BUTTON_RIGHT)
  1665. break;
  1666. if (drag_touching) {
  1667. set_fixed_process(false);
  1668. drag_touching_deaccel=false;
  1669. drag_touching=false;
  1670. drag_speed=0;
  1671. drag_from=0;
  1672. }
  1673. if (!click_handled) {
  1674. drag_speed=0;
  1675. drag_accum=0;
  1676. // last_drag_accum=0;
  1677. drag_from=v_scroll->get_val();
  1678. drag_touching=OS::get_singleton()->has_touchscreen_ui_hint();
  1679. drag_touching_deaccel=false;
  1680. if (drag_touching) {
  1681. set_fixed_process(true);
  1682. }
  1683. }
  1684. } break;
  1685. case BUTTON_WHEEL_UP: {
  1686. v_scroll->set_val( v_scroll->get_val()-v_scroll->get_page()/8 );
  1687. } break;
  1688. case BUTTON_WHEEL_DOWN: {
  1689. v_scroll->set_val( v_scroll->get_val()+v_scroll->get_page()/8 );
  1690. } break;
  1691. }
  1692. } break;
  1693. }
  1694. }
  1695. bool Tree::edit_selected() {
  1696. TreeItem *s = get_selected();
  1697. ERR_EXPLAIN("No item selected!");
  1698. ERR_FAIL_COND_V(!s,false);
  1699. ensure_cursor_is_visible();
  1700. int col = get_selected_column();
  1701. ERR_EXPLAIN("No item column selected!");
  1702. ERR_FAIL_INDEX_V(col,columns.size(),false);
  1703. if (!s->cells[col].editable)
  1704. return false;
  1705. Rect2 rect;
  1706. rect.pos.y = get_item_offset(s) - get_scroll().y;
  1707. for(int i=0;i<col;i++) {
  1708. rect.pos.x+=get_column_width(i);
  1709. }
  1710. rect.size.width=get_column_width(col);
  1711. rect.size.height=compute_item_height(s)+cache.vseparation;
  1712. popup_edited_item=s;
  1713. popup_edited_item_col=col;
  1714. TreeItem::Cell &c = s->cells[col];
  1715. if (c.mode==TreeItem::CELL_MODE_CHECK) {
  1716. s->set_checked(col, !c.checked);
  1717. item_edited(col,s);
  1718. return true;
  1719. } else if (c.mode==TreeItem::CELL_MODE_CUSTOM) {
  1720. edited_item=s;
  1721. edited_col=col;
  1722. custom_popup_rect=Rect2i( get_global_pos() + rect.pos, rect.size );
  1723. emit_signal("custom_popup_edited",false);
  1724. item_edited(col,s);
  1725. return true;
  1726. } else if ((c.mode==TreeItem::CELL_MODE_RANGE||c.mode==TreeItem::CELL_MODE_RANGE_EXPRESSION) && c.text!="") {
  1727. popup_menu->clear();
  1728. for (int i=0;i<c.text.get_slice_count(",");i++) {
  1729. String s = c.text.get_slicec(',',i);
  1730. popup_menu->add_item(s,i);
  1731. }
  1732. popup_menu->set_size(Size2(rect.size.width,0));
  1733. popup_menu->set_pos( get_global_pos() + rect.pos + Point2i(0,rect.size.height) );
  1734. popup_menu->popup();
  1735. popup_edited_item=s;
  1736. popup_edited_item_col=col;
  1737. return true;
  1738. } else if (c.mode==TreeItem::CELL_MODE_STRING || c.mode==TreeItem::CELL_MODE_RANGE || c.mode==TreeItem::CELL_MODE_RANGE_EXPRESSION ) {
  1739. Point2i textedpos=get_global_pos() + rect.pos;
  1740. text_editor->set_pos( textedpos );
  1741. text_editor->set_size( rect.size);
  1742. text_editor->clear();
  1743. text_editor->set_text( c.mode==TreeItem::CELL_MODE_STRING?c.text:rtos(c.val) );
  1744. text_editor->select_all();
  1745. if (c.mode==TreeItem::CELL_MODE_RANGE || c.mode==TreeItem::CELL_MODE_RANGE_EXPRESSION ) {
  1746. value_editor->set_pos(textedpos + Point2i(0,text_editor->get_size().height) );
  1747. value_editor->set_size( Size2(rect.size.width,1));
  1748. value_editor->show_modal();
  1749. updating_value_editor=true;
  1750. value_editor->set_min( c.min );
  1751. value_editor->set_max( c.max );
  1752. value_editor->set_step( c.step );
  1753. value_editor->set_val( c.val );
  1754. value_editor->set_exp_unit_value( c.expr );
  1755. updating_value_editor=false;
  1756. }
  1757. text_editor->show_modal();
  1758. text_editor->grab_focus();
  1759. return true;
  1760. }
  1761. return false;
  1762. }
  1763. Size2 Tree::get_internal_min_size() const {
  1764. Size2i size=cache.bg->get_offset();
  1765. if (root)
  1766. size.height+=get_item_height(root);
  1767. for (int i=0;i<columns.size();i++) {
  1768. size.width+=columns[i].min_width;
  1769. }
  1770. return size;
  1771. }
  1772. void Tree::update_scrollbars() {
  1773. Size2 size = get_size();
  1774. int tbh;
  1775. if (show_column_titles) {
  1776. tbh=_get_title_button_height();
  1777. } else {
  1778. tbh=0;
  1779. }
  1780. Size2 hmin = h_scroll->get_combined_minimum_size();
  1781. Size2 vmin = v_scroll->get_combined_minimum_size();
  1782. v_scroll->set_begin( Point2(size.width - vmin.width , cache.bg->get_margin(MARGIN_TOP)) );
  1783. v_scroll->set_end( Point2(size.width, size.height-cache.bg->get_margin(MARGIN_TOP)-cache.bg->get_margin(MARGIN_BOTTOM)) );
  1784. h_scroll->set_begin( Point2( 0, size.height - hmin.height) );
  1785. h_scroll->set_end( Point2(size.width-vmin.width, size.height) );
  1786. Size2 min = get_internal_min_size();
  1787. if (min.height < size.height - hmin.height) {
  1788. v_scroll->hide();
  1789. cache.offset.y=0;
  1790. } else {
  1791. v_scroll->show();
  1792. v_scroll->set_max(min.height);
  1793. v_scroll->set_page(size.height - hmin.height - tbh);
  1794. cache.offset.y=v_scroll->get_val();
  1795. }
  1796. if (min.width < size.width - vmin.width) {
  1797. h_scroll->hide();
  1798. cache.offset.x=0;
  1799. } else {
  1800. h_scroll->show();
  1801. h_scroll->set_max(min.width);
  1802. h_scroll->set_page(size.width - vmin.width);
  1803. cache.offset.x=h_scroll->get_val();
  1804. }
  1805. }
  1806. int Tree::_get_title_button_height() const {
  1807. return show_column_titles?cache.font->get_height() + cache.title_button->get_minimum_size().height:0;
  1808. }
  1809. void Tree::_notification(int p_what) {
  1810. if (p_what==NOTIFICATION_FOCUS_ENTER) {
  1811. focus_in_id=get_tree()->get_last_event_id();
  1812. }
  1813. if (p_what==NOTIFICATION_MOUSE_EXIT) {
  1814. if (cache.hover_type!=Cache::CLICK_NONE) {
  1815. cache.hover_type=Cache::CLICK_NONE;
  1816. update();
  1817. }
  1818. }
  1819. if (p_what==NOTIFICATION_VISIBILITY_CHANGED) {
  1820. drag_touching=false;
  1821. }
  1822. if (p_what==NOTIFICATION_ENTER_TREE) {
  1823. update_cache();;
  1824. }
  1825. if (p_what==NOTIFICATION_DRAG_END) {
  1826. drop_mode_flags=0;
  1827. update();
  1828. }
  1829. if (p_what==NOTIFICATION_DRAG_BEGIN) {
  1830. single_select_defer=NULL;
  1831. }
  1832. if (p_what==NOTIFICATION_FIXED_PROCESS) {
  1833. if (drag_touching) {
  1834. if (drag_touching_deaccel) {
  1835. float pos = v_scroll->get_val();
  1836. pos+=drag_speed*get_fixed_process_delta_time();
  1837. bool turnoff=false;
  1838. if (pos<0) {
  1839. pos=0;
  1840. turnoff=true;
  1841. set_fixed_process(false);
  1842. drag_touching=false;
  1843. drag_touching_deaccel=false;
  1844. }
  1845. if (pos > (v_scroll->get_max()-v_scroll->get_page())) {
  1846. pos=v_scroll->get_max()-v_scroll->get_page();
  1847. turnoff=true;
  1848. }
  1849. v_scroll->set_val(pos);
  1850. float sgn = drag_speed<0? -1 : 1;
  1851. float val = Math::abs(drag_speed);
  1852. val-=1000*get_fixed_process_delta_time();
  1853. if (val<0) {
  1854. turnoff=true;
  1855. }
  1856. drag_speed=sgn*val;
  1857. if (turnoff) {
  1858. set_fixed_process(false);
  1859. drag_touching=false;
  1860. drag_touching_deaccel=false;
  1861. }
  1862. } else {
  1863. }
  1864. }
  1865. }
  1866. if (p_what==NOTIFICATION_DRAW) {
  1867. update_cache();
  1868. update_scrollbars();
  1869. RID ci = get_canvas_item();
  1870. VisualServer::get_singleton()->canvas_item_set_clip(ci,true);
  1871. Ref<StyleBox> bg = cache.bg;
  1872. Ref<StyleBox> bg_focus = get_stylebox("bg_focus");
  1873. Point2 draw_ofs;
  1874. draw_ofs+=bg->get_offset();
  1875. Size2 draw_size=get_size()-bg->get_minimum_size();
  1876. bg->draw( ci, Rect2( Point2(), get_size()) );
  1877. if (has_focus()) {
  1878. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci,true);
  1879. bg_focus->draw( ci, Rect2( Point2(), get_size()) );
  1880. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci,false);
  1881. }
  1882. int tbh = _get_title_button_height();
  1883. draw_ofs.y+=tbh;
  1884. draw_size.y-=tbh;
  1885. if (root) {
  1886. draw_item( Point2(),draw_ofs,draw_size,root);
  1887. }
  1888. int ofs=0;
  1889. // int from_y=exposed.pos.y+bg->get_margin(MARGIN_TOP);
  1890. // int size_y=exposed.size.height-bg->get_minimum_size().height;
  1891. for (int i=0;i<(columns.size()-1-1);i++) {
  1892. ofs+=get_column_width(i);
  1893. //get_painter()->draw_fill_rect( Point2(ofs+cache.hseparation/2, from_y), Size2( 1, size_y ),color( COLOR_TREE_GRID) );
  1894. }
  1895. if (show_column_titles) {
  1896. //title butons
  1897. int ofs=cache.bg->get_margin(MARGIN_LEFT);
  1898. for(int i=0;i<columns.size();i++) {
  1899. 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);
  1900. Ref<Font> f = cache.tb_font;
  1901. Rect2 tbrect = Rect2(ofs - cache.offset.x,bg->get_margin(MARGIN_TOP),get_column_width(i),tbh);
  1902. sb->draw(ci,tbrect);
  1903. ofs+=tbrect.size.width;
  1904. //text
  1905. int clip_w = tbrect.size.width - sb->get_minimum_size().width;
  1906. 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);
  1907. }
  1908. }
  1909. }
  1910. if (p_what==NOTIFICATION_THEME_CHANGED) {
  1911. update_cache();
  1912. }
  1913. }
  1914. Size2 Tree::get_minimum_size() const {
  1915. return Size2(1,1);
  1916. }
  1917. TreeItem *Tree::create_item(TreeItem *p_parent) {
  1918. ERR_FAIL_COND_V(blocked>0,NULL);
  1919. TreeItem *ti = memnew( TreeItem(this) );
  1920. ti->cells.resize( columns.size() );
  1921. ERR_FAIL_COND_V(!ti,NULL);
  1922. if (p_parent) {
  1923. /* Always append at the end */
  1924. TreeItem *last=0;
  1925. TreeItem *c=p_parent->childs;
  1926. while(c) {
  1927. last=c;
  1928. c=c->next;
  1929. }
  1930. if (last) {
  1931. last->next=ti;
  1932. } else {
  1933. p_parent->childs=ti;
  1934. }
  1935. ti->parent=p_parent;
  1936. } else {
  1937. if (root)
  1938. ti->childs=root;
  1939. root=ti;
  1940. }
  1941. return ti;
  1942. }
  1943. TreeItem* Tree::get_root() {
  1944. return root;
  1945. }
  1946. TreeItem* Tree::get_last_item() {
  1947. TreeItem *last=root;
  1948. while(last) {
  1949. if (last->next)
  1950. last=last->next;
  1951. else if (last->childs)
  1952. last=last->childs;
  1953. else
  1954. break;
  1955. }
  1956. return last;
  1957. }
  1958. void Tree::item_edited(int p_column,TreeItem *p_item) {
  1959. edited_item=p_item;
  1960. edited_col=p_column;
  1961. emit_signal("item_edited");
  1962. }
  1963. void Tree::item_changed(int p_column,TreeItem *p_item) {
  1964. update();
  1965. }
  1966. void Tree::item_selected(int p_column,TreeItem *p_item) {
  1967. if (select_mode==SELECT_MULTI) {
  1968. if (!p_item->cells[p_column].selectable)
  1969. return;
  1970. p_item->cells[p_column].selected=true;
  1971. //emit_signal("multi_selected",p_item,p_column,true); - NO this is for TreeItem::select
  1972. } else {
  1973. select_single_item(p_item,root,p_column);
  1974. }
  1975. update();
  1976. }
  1977. void Tree::item_deselected(int p_column,TreeItem *p_item) {
  1978. if (select_mode==SELECT_MULTI) {
  1979. p_item->cells[p_column].selected=false;
  1980. }
  1981. update();
  1982. }
  1983. void Tree::set_select_mode(SelectMode p_mode) {
  1984. select_mode=p_mode;
  1985. }
  1986. void Tree::clear() {
  1987. if (blocked>0) {
  1988. ERR_FAIL_COND(blocked>0);
  1989. }
  1990. if (pressing_for_editor) {
  1991. if (range_drag_enabled) {
  1992. range_drag_enabled = false;
  1993. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  1994. warp_mouse(range_drag_capture_pos);
  1995. }
  1996. pressing_for_editor = false;
  1997. }
  1998. if (root) {
  1999. memdelete( root );
  2000. root = NULL;
  2001. };
  2002. selected_item=NULL;
  2003. edited_item=NULL;
  2004. popup_edited_item=NULL;
  2005. selected_item=NULL;
  2006. update();
  2007. };
  2008. void Tree::set_hide_root(bool p_enabled) {
  2009. hide_root=p_enabled;
  2010. update();
  2011. }
  2012. void Tree::set_column_min_width(int p_column,int p_min_width) {
  2013. ERR_FAIL_INDEX(p_column,columns.size());
  2014. if (p_min_width<1)
  2015. return;
  2016. columns[p_column].min_width=p_min_width;
  2017. update();
  2018. }
  2019. void Tree::set_column_expand(int p_column,bool p_expand) {
  2020. ERR_FAIL_INDEX(p_column,columns.size());
  2021. columns[p_column].expand=p_expand;
  2022. update();
  2023. }
  2024. TreeItem *Tree::get_selected() const {
  2025. return selected_item;
  2026. }
  2027. int Tree::get_selected_column() const {
  2028. return selected_col;
  2029. }
  2030. TreeItem *Tree::get_edited() const {
  2031. return edited_item;
  2032. }
  2033. int Tree::get_edited_column() const {
  2034. return edited_col;
  2035. }
  2036. TreeItem* Tree::get_next_selected( TreeItem* p_item) {
  2037. //if (!p_item)
  2038. // return NULL;
  2039. if (!root)
  2040. return NULL;
  2041. while(true) {
  2042. if (!p_item) {
  2043. p_item=root;
  2044. } else {
  2045. if (p_item->childs) {
  2046. p_item=p_item->childs;
  2047. } else if (p_item->next) {
  2048. p_item=p_item->next;
  2049. } else {
  2050. while(!p_item->next) {
  2051. p_item=p_item->parent;
  2052. if (p_item==NULL)
  2053. return NULL;
  2054. }
  2055. p_item=p_item->next;
  2056. }
  2057. }
  2058. for (int i=0;i<columns.size();i++)
  2059. if (p_item->cells[i].selected)
  2060. return p_item;
  2061. }
  2062. return NULL;
  2063. }
  2064. int Tree::get_column_width(int p_column) const {
  2065. ERR_FAIL_INDEX_V(p_column,columns.size(),-1);
  2066. if (!columns[p_column].expand)
  2067. return columns[p_column].min_width;
  2068. Ref<StyleBox> bg = cache.bg;
  2069. int expand_area=get_size().width-(bg->get_margin(MARGIN_LEFT)+bg->get_margin(MARGIN_RIGHT));
  2070. if (v_scroll->is_visible())
  2071. expand_area-=v_scroll->get_combined_minimum_size().width;
  2072. int expanding_columns=0;
  2073. int expanding_total=0;
  2074. for (int i=0;i<columns.size();i++) {
  2075. if (!columns[i].expand) {
  2076. expand_area-=columns[i].min_width;
  2077. } else {
  2078. expanding_total+=columns[i].min_width;
  2079. expanding_columns++;
  2080. }
  2081. }
  2082. if (expand_area<expanding_total)
  2083. return columns[p_column].min_width;
  2084. ERR_FAIL_COND_V(expanding_columns==0,-1); // shouldnt happen
  2085. return expand_area * columns[p_column].min_width / expanding_total;
  2086. }
  2087. void Tree::propagate_set_columns(TreeItem *p_item) {
  2088. p_item->cells.resize( columns.size() );
  2089. TreeItem *c = p_item->get_children();
  2090. while(c) {
  2091. propagate_set_columns(c);
  2092. c=c->get_next();
  2093. }
  2094. }
  2095. void Tree::set_columns(int p_columns) {
  2096. ERR_FAIL_COND(p_columns<1);
  2097. ERR_FAIL_COND(blocked>0);
  2098. columns.resize(p_columns);
  2099. if (root)
  2100. propagate_set_columns(root);
  2101. if (selected_col>=p_columns)
  2102. selected_col=p_columns-1;
  2103. update();
  2104. }
  2105. int Tree::get_columns() const {
  2106. return columns.size();
  2107. }
  2108. void Tree::_scroll_moved(float) {
  2109. update();
  2110. }
  2111. Rect2 Tree::get_custom_popup_rect() const {
  2112. return custom_popup_rect;
  2113. }
  2114. int Tree::get_item_offset(TreeItem *p_item) const {
  2115. TreeItem *it=root;
  2116. int ofs=_get_title_button_height();
  2117. if (!it)
  2118. return 0;
  2119. while(true) {
  2120. if (it==p_item)
  2121. return ofs;
  2122. ofs+=compute_item_height(it)+cache.vseparation;
  2123. if (it->childs && !it->collapsed) {
  2124. it=it->childs;
  2125. } else if (it->next) {
  2126. it=it->next;
  2127. } else {
  2128. while(!it->next) {
  2129. it=it->parent;
  2130. if (it==NULL)
  2131. return 0;
  2132. }
  2133. it=it->next;
  2134. }
  2135. }
  2136. return -1; //not found
  2137. }
  2138. void Tree::ensure_cursor_is_visible() {
  2139. if (!is_inside_tree())
  2140. return;
  2141. TreeItem *selected = get_selected();
  2142. if (!selected)
  2143. return;
  2144. int ofs = get_item_offset(selected);
  2145. if (ofs==-1)
  2146. return;
  2147. int h = compute_item_height(selected)+cache.vseparation;
  2148. int screenh=get_size().height-h_scroll->get_combined_minimum_size().height;
  2149. if (ofs+h>v_scroll->get_val()+screenh)
  2150. v_scroll->set_val(ofs-screenh+h);
  2151. else if (ofs < v_scroll->get_val())
  2152. v_scroll->set_val(ofs);
  2153. }
  2154. int Tree::get_pressed_button() const {
  2155. return pressed_button;
  2156. }
  2157. Rect2 Tree::get_item_rect(TreeItem *p_item,int p_column) const {
  2158. ERR_FAIL_NULL_V(p_item,Rect2());
  2159. ERR_FAIL_COND_V(p_item->tree!=this,Rect2());
  2160. if (p_column!=-1) {
  2161. ERR_FAIL_INDEX_V(p_column,columns.size(),Rect2());
  2162. }
  2163. int ofs = get_item_offset(p_item);
  2164. int height = compute_item_height(p_item);
  2165. Rect2 r;
  2166. r.pos.y=ofs;
  2167. r.size.height=height;
  2168. if (p_column==-1) {
  2169. r.pos.x=0;
  2170. r.size.x=get_size().width;
  2171. } else {
  2172. int accum=0;
  2173. for(int i=0;i<p_column;i++) {
  2174. accum+=get_column_width(i);
  2175. }
  2176. r.pos.x=accum;
  2177. r.size.x=get_column_width(p_column);
  2178. }
  2179. return r;
  2180. }
  2181. void Tree::set_column_titles_visible(bool p_show) {
  2182. show_column_titles=p_show;
  2183. update();
  2184. }
  2185. bool Tree::are_column_titles_visible() const {
  2186. return show_column_titles;
  2187. }
  2188. void Tree::set_column_title(int p_column,const String& p_title) {
  2189. ERR_FAIL_INDEX(p_column,columns.size());
  2190. columns[p_column].title=p_title;
  2191. update();
  2192. }
  2193. String Tree::get_column_title(int p_column) const {
  2194. ERR_FAIL_INDEX_V(p_column,columns.size(),"");
  2195. return columns[p_column].title;
  2196. }
  2197. Point2 Tree::get_scroll() const {
  2198. Point2 ofs;
  2199. if (h_scroll->is_visible())
  2200. ofs.x=h_scroll->get_val();
  2201. if (v_scroll->is_visible())
  2202. ofs.y=v_scroll->get_val();
  2203. return ofs;
  2204. }
  2205. TreeItem* Tree::_search_item_text(TreeItem *p_at, const String& p_find,int *r_col,bool p_selectable,bool p_backwards) {
  2206. while(p_at) {
  2207. for(int i=0;i<columns.size();i++) {
  2208. if (p_at->get_text(i).findn(p_find)==0 && (!p_selectable || p_at->is_selectable(i))) {
  2209. if (r_col)
  2210. *r_col=i;
  2211. return p_at;
  2212. }
  2213. }
  2214. if (p_backwards)
  2215. p_at=p_at->get_prev_visible();
  2216. else
  2217. p_at=p_at->get_next_visible();
  2218. }
  2219. return NULL;
  2220. }
  2221. TreeItem* Tree::search_item_text(const String& p_find,int *r_col,bool p_selectable) {
  2222. if (!root)
  2223. return NULL;
  2224. return _search_item_text(root,p_find,r_col,p_selectable);
  2225. }
  2226. void Tree::_do_incr_search(const String& p_add) {
  2227. uint64_t time = OS::get_singleton()->get_ticks_usec() / 1000; // convert to msec
  2228. uint64_t diff = time - last_keypress;
  2229. if (diff > uint64_t(GLOBAL_DEF("gui/incr_search_max_interval_msec",2000)))
  2230. incr_search=p_add;
  2231. else
  2232. incr_search+=p_add;
  2233. last_keypress=time;
  2234. int col;
  2235. TreeItem *item = search_item_text(incr_search,&col,true);
  2236. if (!item)
  2237. return;
  2238. item->select(col);
  2239. ensure_cursor_is_visible();
  2240. }
  2241. TreeItem* Tree::_find_item_at_pos(TreeItem*p_item, const Point2& p_pos,int& r_column,int &h,int &section) const {
  2242. Point2 pos = p_pos;
  2243. if (root!=p_item || ! hide_root) {
  2244. h = compute_item_height(p_item)+cache.vseparation;;
  2245. if (pos.y<h) {
  2246. if (drop_mode_flags==DROP_MODE_ON_ITEM) {
  2247. section=0;
  2248. } else if (drop_mode_flags==DROP_MODE_INBETWEEN) {
  2249. section=pos.y<h/2?-1:1;
  2250. } else if (pos.y<h/4) {
  2251. section=-1;
  2252. } else if (pos.y>=(h*3/4)) {
  2253. section=1;
  2254. } else {
  2255. section=0;
  2256. }
  2257. for(int i=0;i<columns.size();i++) {
  2258. int w = get_column_width(i);
  2259. if (pos.x < w) {
  2260. r_column=i;
  2261. return p_item;
  2262. }
  2263. pos.x-=w;
  2264. }
  2265. return NULL;
  2266. } else {
  2267. pos.y-=h;
  2268. }
  2269. } else {
  2270. h=0;
  2271. }
  2272. if (p_item->is_collapsed())
  2273. return NULL; // do not try childs, it's collapsed
  2274. TreeItem *n = p_item->get_children();
  2275. while(n) {
  2276. int ch;
  2277. TreeItem *r = _find_item_at_pos(n,pos,r_column,ch,section);
  2278. pos.y-=ch;
  2279. h+=ch;
  2280. if (r)
  2281. return r;
  2282. n=n->get_next();
  2283. }
  2284. return NULL;
  2285. }
  2286. int Tree::get_column_at_pos(const Point2& p_pos) const {
  2287. if (root) {
  2288. Point2 pos=p_pos;
  2289. pos -= cache.bg->get_offset();
  2290. pos.y-=_get_title_button_height();
  2291. if (pos.y<0)
  2292. return -1;
  2293. if (h_scroll->is_visible())
  2294. pos.x+=h_scroll->get_val();
  2295. if (v_scroll->is_visible())
  2296. pos.y+=v_scroll->get_val();
  2297. int col,h,section;
  2298. TreeItem *it = _find_item_at_pos(root,pos,col,h,section);
  2299. if (it) {
  2300. return col;
  2301. }
  2302. }
  2303. return -1;
  2304. }
  2305. int Tree::get_drop_section_at_pos(const Point2& p_pos) const {
  2306. if (root) {
  2307. Point2 pos=p_pos;
  2308. pos -= cache.bg->get_offset();
  2309. pos.y-=_get_title_button_height();
  2310. if (pos.y<0)
  2311. return -100;
  2312. if (h_scroll->is_visible())
  2313. pos.x+=h_scroll->get_val();
  2314. if (v_scroll->is_visible())
  2315. pos.y+=v_scroll->get_val();
  2316. int col,h,section;
  2317. TreeItem *it = _find_item_at_pos(root,pos,col,h,section);
  2318. if (it) {
  2319. return section;
  2320. }
  2321. }
  2322. return -100;
  2323. }
  2324. TreeItem* Tree::get_item_at_pos(const Point2& p_pos) const {
  2325. if (root) {
  2326. Point2 pos=p_pos;
  2327. pos -= cache.bg->get_offset();
  2328. pos.y-=_get_title_button_height();
  2329. if (pos.y<0)
  2330. return NULL;
  2331. if (h_scroll->is_visible())
  2332. pos.x+=h_scroll->get_val();
  2333. if (v_scroll->is_visible())
  2334. pos.y+=v_scroll->get_val();
  2335. int col,h,section;
  2336. TreeItem *it = _find_item_at_pos(root,pos,col,h,section);
  2337. if (it) {
  2338. return it;
  2339. }
  2340. }
  2341. return NULL;
  2342. }
  2343. String Tree::get_tooltip(const Point2& p_pos) const {
  2344. if (root) {
  2345. Point2 pos=p_pos;
  2346. pos -= cache.bg->get_offset();
  2347. pos.y-=_get_title_button_height();
  2348. if (pos.y<0)
  2349. return Control::get_tooltip(p_pos);
  2350. if (h_scroll->is_visible())
  2351. pos.x+=h_scroll->get_val();
  2352. if (v_scroll->is_visible())
  2353. pos.y+=v_scroll->get_val();
  2354. int col,h,section;
  2355. TreeItem *it = _find_item_at_pos(root,pos,col,h,section);
  2356. if (it) {
  2357. String ret;
  2358. if (it->get_tooltip(col)=="")
  2359. ret=it->get_text(col);
  2360. else
  2361. ret=it->get_tooltip(col);
  2362. return ret;
  2363. }
  2364. }
  2365. return Control::get_tooltip(p_pos);
  2366. }
  2367. void Tree::set_cursor_can_exit_tree(bool p_enable) {
  2368. cursor_can_exit_tree=p_enable;
  2369. }
  2370. bool Tree::can_cursor_exit_tree() const {
  2371. return cursor_can_exit_tree;
  2372. }
  2373. void Tree::set_hide_folding(bool p_hide) {
  2374. hide_folding=p_hide;
  2375. update();
  2376. }
  2377. bool Tree::is_folding_hidden() const {
  2378. return hide_folding;
  2379. }
  2380. void Tree::set_value_evaluator(ValueEvaluator *p_evaluator) {
  2381. evaluator = p_evaluator;
  2382. }
  2383. void Tree::set_drop_mode_flags(int p_flags) {
  2384. if (drop_mode_flags==p_flags)
  2385. return;
  2386. drop_mode_flags=p_flags;
  2387. if (drop_mode_flags==0) {
  2388. drop_mode_over=NULL;
  2389. }
  2390. update();
  2391. }
  2392. int Tree::get_drop_mode_flags() const {
  2393. return drop_mode_flags;
  2394. }
  2395. void Tree::set_single_select_cell_editing_only_when_already_selected(bool p_enable) {
  2396. force_select_on_already_selected=p_enable;
  2397. }
  2398. bool Tree::get_single_select_cell_editing_only_when_already_selected() const {
  2399. return force_select_on_already_selected;
  2400. }
  2401. void Tree::set_allow_rmb_select(bool p_allow) {
  2402. allow_rmb_select=p_allow;
  2403. }
  2404. bool Tree::get_allow_rmb_select() const{
  2405. return allow_rmb_select;
  2406. }
  2407. void Tree::_bind_methods() {
  2408. ObjectTypeDB::bind_method(_MD("_range_click_timeout"),&Tree::_range_click_timeout);
  2409. ObjectTypeDB::bind_method(_MD("_input_event"),&Tree::_input_event);
  2410. ObjectTypeDB::bind_method(_MD("_popup_select"),&Tree::popup_select);
  2411. ObjectTypeDB::bind_method(_MD("_text_editor_enter"),&Tree::text_editor_enter);
  2412. ObjectTypeDB::bind_method(_MD("_text_editor_modal_close"),&Tree::_text_editor_modal_close);
  2413. ObjectTypeDB::bind_method(_MD("_value_editor_changed"),&Tree::value_editor_changed);
  2414. ObjectTypeDB::bind_method(_MD("_scroll_moved"),&Tree::_scroll_moved);
  2415. ObjectTypeDB::bind_method(_MD("clear"),&Tree::clear);
  2416. ObjectTypeDB::bind_method(_MD("create_item:TreeItem","parent:TreeItem"),&Tree::_create_item,DEFVAL(Variant()));
  2417. ObjectTypeDB::bind_method(_MD("get_root:TreeItem"),&Tree::get_root);
  2418. ObjectTypeDB::bind_method(_MD("set_column_min_width","column","min_width"),&Tree::set_column_min_width);
  2419. ObjectTypeDB::bind_method(_MD("set_column_expand","column","expand"),&Tree::set_column_expand);
  2420. ObjectTypeDB::bind_method(_MD("get_column_width","column"),&Tree::get_column_width);
  2421. ObjectTypeDB::bind_method(_MD("set_hide_root","enable"),&Tree::set_hide_root);
  2422. ObjectTypeDB::bind_method(_MD("get_next_selected:TreeItem","from:TreeItem"),&Tree::_get_next_selected);
  2423. ObjectTypeDB::bind_method(_MD("get_selected:TreeItem"),&Tree::get_selected);
  2424. ObjectTypeDB::bind_method(_MD("get_selected_column"),&Tree::get_selected_column);
  2425. ObjectTypeDB::bind_method(_MD("get_pressed_button"),&Tree::get_pressed_button);
  2426. ObjectTypeDB::bind_method(_MD("set_select_mode","mode"),&Tree::set_select_mode);
  2427. ObjectTypeDB::bind_method(_MD("set_columns","amount"),&Tree::set_columns);
  2428. ObjectTypeDB::bind_method(_MD("get_columns"),&Tree::get_columns);
  2429. ObjectTypeDB::bind_method(_MD("get_edited:TreeItem"),&Tree::get_edited);
  2430. ObjectTypeDB::bind_method(_MD("get_edited_column"),&Tree::get_edited_column);
  2431. ObjectTypeDB::bind_method(_MD("get_custom_popup_rect"),&Tree::get_custom_popup_rect);
  2432. ObjectTypeDB::bind_method(_MD("get_item_area_rect","item:TreeItem","column"),&Tree::_get_item_rect,DEFVAL(-1));
  2433. ObjectTypeDB::bind_method(_MD("get_item_at_pos:TreeItem","pos"),&Tree::get_item_at_pos);
  2434. ObjectTypeDB::bind_method(_MD("get_column_at_pos","pos"),&Tree::get_column_at_pos);
  2435. ObjectTypeDB::bind_method(_MD("ensure_cursor_is_visible"),&Tree::ensure_cursor_is_visible);
  2436. ObjectTypeDB::bind_method(_MD("set_column_titles_visible","visible"),&Tree::set_column_titles_visible);
  2437. ObjectTypeDB::bind_method(_MD("are_column_titles_visible"),&Tree::are_column_titles_visible);
  2438. ObjectTypeDB::bind_method(_MD("set_column_title","column","title"),&Tree::set_column_title);
  2439. ObjectTypeDB::bind_method(_MD("get_column_title","column"),&Tree::get_column_title);
  2440. ObjectTypeDB::bind_method(_MD("get_scroll"),&Tree::get_scroll);
  2441. ObjectTypeDB::bind_method(_MD("set_hide_folding","hide"),&Tree::set_hide_folding);
  2442. ObjectTypeDB::bind_method(_MD("is_folding_hidden"),&Tree::is_folding_hidden);
  2443. ObjectTypeDB::bind_method(_MD("set_drop_mode_flags","flags"),&Tree::set_drop_mode_flags);
  2444. ObjectTypeDB::bind_method(_MD("get_drop_mode_flags"),&Tree::get_drop_mode_flags);
  2445. ObjectTypeDB::bind_method(_MD("set_allow_rmb_select","allow"),&Tree::set_allow_rmb_select);
  2446. ObjectTypeDB::bind_method(_MD("get_allow_rmb_select"),&Tree::get_allow_rmb_select);
  2447. ObjectTypeDB::bind_method(_MD("set_single_select_cell_editing_only_when_already_selected","enable"),&Tree::set_single_select_cell_editing_only_when_already_selected);
  2448. ObjectTypeDB::bind_method(_MD("get_single_select_cell_editing_only_when_already_selected"),&Tree::get_single_select_cell_editing_only_when_already_selected);
  2449. ADD_SIGNAL( MethodInfo("item_selected"));
  2450. ADD_SIGNAL( MethodInfo("cell_selected"));
  2451. ADD_SIGNAL( MethodInfo("multi_selected",PropertyInfo(Variant::OBJECT,"item"),PropertyInfo(Variant::INT,"column"),PropertyInfo(Variant::BOOL,"selected")) );
  2452. ADD_SIGNAL( MethodInfo("item_rmb_selected",PropertyInfo(Variant::VECTOR2,"pos")));
  2453. ADD_SIGNAL( MethodInfo("empty_tree_rmb_selected",PropertyInfo(Variant::VECTOR2,"pos")));
  2454. ADD_SIGNAL( MethodInfo("item_edited"));
  2455. ADD_SIGNAL( MethodInfo("item_collapsed",PropertyInfo(Variant::OBJECT,"item")));
  2456. //ADD_SIGNAL( MethodInfo("item_doubleclicked" ) );
  2457. ADD_SIGNAL( MethodInfo("button_pressed",PropertyInfo(Variant::OBJECT,"item"),PropertyInfo(Variant::INT,"column"),PropertyInfo(Variant::INT,"id")));
  2458. ADD_SIGNAL( MethodInfo("custom_popup_edited",PropertyInfo(Variant::BOOL,"arrow_clicked") ) );
  2459. ADD_SIGNAL( MethodInfo("item_activated"));
  2460. BIND_CONSTANT( SELECT_SINGLE );
  2461. BIND_CONSTANT( SELECT_ROW );
  2462. BIND_CONSTANT( SELECT_MULTI );
  2463. BIND_CONSTANT( DROP_MODE_DISABLED );
  2464. BIND_CONSTANT( DROP_MODE_ON_ITEM );
  2465. BIND_CONSTANT( DROP_MODE_INBETWEEN );
  2466. }
  2467. Tree::Tree() {
  2468. selected_col=0;
  2469. columns.resize(1);
  2470. selected_item=NULL;
  2471. edited_item=NULL;
  2472. selected_col=-1;
  2473. edited_col=-1;
  2474. hide_root=false;
  2475. select_mode=SELECT_SINGLE;
  2476. root=0;
  2477. popup_menu=NULL;
  2478. popup_edited_item=NULL;
  2479. text_editor=NULL;
  2480. set_focus_mode(FOCUS_ALL);
  2481. popup_menu = memnew( PopupMenu );
  2482. popup_menu->hide();
  2483. add_child(popup_menu);
  2484. popup_menu->set_as_toplevel(true);
  2485. text_editor = memnew( LineEdit );
  2486. add_child(text_editor);
  2487. text_editor->set_as_toplevel(true);
  2488. text_editor->hide();
  2489. value_editor = memnew( HSlider );
  2490. add_child(value_editor);
  2491. value_editor->set_as_toplevel(true);
  2492. value_editor->hide();
  2493. h_scroll = memnew( HScrollBar );
  2494. v_scroll = memnew( VScrollBar );
  2495. add_child(h_scroll);
  2496. add_child(v_scroll);
  2497. range_click_timer = memnew( Timer );
  2498. range_click_timer->connect("timeout",this,"_range_click_timeout");
  2499. add_child(range_click_timer);
  2500. h_scroll->connect("value_changed", this,"_scroll_moved");
  2501. v_scroll->connect("value_changed", this,"_scroll_moved");
  2502. text_editor->connect("text_entered", this,"_text_editor_enter");
  2503. text_editor->connect("modal_close", this,"_text_editor_modal_close");
  2504. popup_menu->connect("item_pressed", this,"_popup_select");
  2505. value_editor->connect("value_changed", this,"_value_editor_changed");
  2506. value_editor->set_as_toplevel(true);
  2507. text_editor->set_as_toplevel(true);
  2508. updating_value_editor=false;
  2509. pressed_button=-1;
  2510. show_column_titles=false;
  2511. cache.click_type = Cache::CLICK_NONE;
  2512. cache.hover_type = Cache::CLICK_NONE;
  2513. cache.hover_index = -1;
  2514. cache.click_index=-1;
  2515. cache.click_id=-1;
  2516. cache.click_item=NULL;
  2517. cache.click_column=0;
  2518. last_keypress=0;
  2519. focus_in_id=0;
  2520. blocked=0;
  2521. cursor_can_exit_tree=true;
  2522. set_stop_mouse(true);
  2523. drag_speed=0;
  2524. drag_touching=false;
  2525. drag_touching_deaccel=false;
  2526. pressing_for_editor=false;
  2527. range_drag_enabled=false;
  2528. hide_folding=false;
  2529. evaluator=NULL;
  2530. drop_mode_flags=0;
  2531. drop_mode_over=NULL;
  2532. drop_mode_section=0;
  2533. single_select_defer=NULL;
  2534. force_select_on_already_selected=false;
  2535. allow_rmb_select=false;
  2536. }
  2537. Tree::~Tree() {
  2538. if (root) {
  2539. memdelete( root );
  2540. }
  2541. }