2
0

tree.cpp 87 KB

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