tree.cpp 97 KB

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