tree.cpp 106 KB

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