tree.cpp 114 KB

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