tree.cpp 76 KB

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