tree.cpp 90 KB

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