tree.cpp 112 KB

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