tree.cpp 106 KB

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