tree.cpp 123 KB

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