tree.cpp 93 KB

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