tree.cpp 86 KB

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