tree.cpp 85 KB

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