tree.cpp 72 KB

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