tree.cpp 97 KB

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