tree.cpp 125 KB

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