tree.cpp 104 KB

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