tree.cpp 87 KB

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