tree.cpp 105 KB

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