control.cpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888
  1. /*************************************************************************/
  2. /* control.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "control.h"
  30. #include "servers/visual_server.h"
  31. #include "scene/main/viewport.h"
  32. #include "scene/main/canvas_layer.h"
  33. #include "globals.h"
  34. #include "print_string.h"
  35. #include "os/keyboard.h"
  36. #include "os/os.h"
  37. #include "message_queue.h"
  38. #include "scene/scene_string_names.h"
  39. #include "scene/gui/panel.h"
  40. #include "scene/gui/label.h"
  41. #include <stdio.h>
  42. class TooltipPanel : public Panel {
  43. OBJ_TYPE(TooltipPanel,Panel)
  44. public:
  45. TooltipPanel() {};
  46. };
  47. class TooltipLabel : public Label {
  48. OBJ_TYPE(TooltipLabel,Label)
  49. public:
  50. TooltipLabel() {};
  51. };
  52. Control::Window::Window() {
  53. mouse_focus=NULL;
  54. mouse_focus_button=-1;
  55. key_focus=NULL;
  56. mouse_over=NULL;
  57. disable_input=false;
  58. cancelled_input_ID=0;
  59. tooltip=NULL;
  60. tooltip_popup=NULL;
  61. tooltip_label=NULL;
  62. subwindow_order_dirty=false;
  63. }
  64. Variant Control::edit_get_state() const {
  65. return get_rect();
  66. }
  67. void Control::edit_set_state(const Variant& p_state) {
  68. Rect2 state=p_state;
  69. set_pos(state.pos);
  70. set_size(state.size);
  71. }
  72. void Control::set_custom_minimum_size(const Size2& p_custom) {
  73. if (p_custom==data.custom_minimum_size)
  74. return;
  75. data.custom_minimum_size=p_custom;
  76. minimum_size_changed();
  77. }
  78. Size2 Control::get_custom_minimum_size() const{
  79. return data.custom_minimum_size;
  80. }
  81. Size2 Control::get_combined_minimum_size() const {
  82. Size2 minsize = get_minimum_size();
  83. minsize.x = MAX(minsize.x,data.custom_minimum_size.x);
  84. minsize.y = MAX(minsize.y,data.custom_minimum_size.y);
  85. return minsize;
  86. }
  87. Size2 Control::edit_get_minimum_size() const {
  88. return get_combined_minimum_size();
  89. }
  90. void Control::edit_set_rect(const Rect2& p_edit_rect) {
  91. Rect2 new_rect=get_rect();
  92. new_rect.pos+=p_edit_rect.pos.snapped(Vector2(1,1));
  93. new_rect.size=p_edit_rect.size.snapped(Vector2(1,1));
  94. set_pos(new_rect.pos);
  95. set_size(new_rect.size);
  96. }
  97. bool Control::_set(const StringName& p_name, const Variant& p_value) {
  98. String name= p_name;
  99. if (!name.begins_with("custom"))
  100. return false;
  101. if (p_value.get_type()==Variant::NIL) {
  102. if (name.begins_with("custom_icons/")) {
  103. String dname = name.get_slice("/",1);
  104. data.icon_override.erase(dname);
  105. update();
  106. } else if (name.begins_with("custom_styles/")) {
  107. String dname = name.get_slice("/",1);
  108. data.style_override.erase(dname);
  109. update();
  110. } else if (name.begins_with("custom_fonts/")) {
  111. String dname = name.get_slice("/",1);
  112. data.font_override.erase(dname);
  113. update();
  114. } else if (name.begins_with("custom_colors/")) {
  115. String dname = name.get_slice("/",1);
  116. data.color_override.erase(dname);
  117. update();
  118. } else if (name.begins_with("custom_constants/")) {
  119. String dname = name.get_slice("/",1);
  120. data.constant_override.erase(dname);
  121. update();
  122. } else
  123. return false;
  124. } else {
  125. if (name.begins_with("custom_icons/")) {
  126. String dname = name.get_slice("/",1);
  127. add_icon_override(dname,p_value);
  128. } else if (name.begins_with("custom_styles/")) {
  129. String dname = name.get_slice("/",1);
  130. add_style_override(dname,p_value);
  131. } else if (name.begins_with("custom_fonts/")) {
  132. String dname = name.get_slice("/",1);
  133. add_font_override(dname,p_value);
  134. } else if (name.begins_with("custom_colors/")) {
  135. String dname = name.get_slice("/",1);
  136. add_color_override(dname,p_value);
  137. } else if (name.begins_with("custom_constants/")) {
  138. String dname = name.get_slice("/",1);
  139. add_constant_override(dname,p_value);
  140. } else
  141. return false;
  142. }
  143. return true;
  144. }
  145. void Control::_update_minimum_size() {
  146. if (!is_inside_scene())
  147. return;
  148. data.pending_min_size_update=false;
  149. Size2 minsize = get_combined_minimum_size();
  150. if (minsize.x > data.size_cache.x ||
  151. minsize.y > data.size_cache.y
  152. ) {
  153. _size_changed();
  154. }
  155. emit_signal(SceneStringNames::get_singleton()->minimum_size_changed);
  156. }
  157. bool Control::_get(const StringName& p_name,Variant &r_ret) const {
  158. String sname=p_name;
  159. if (!sname.begins_with("custom"))
  160. return false;
  161. if (sname.begins_with("custom_icons/")) {
  162. String name = sname.get_slice("/",1);
  163. r_ret= data.icon_override.has(name)?Variant(data.icon_override[name]):Variant();
  164. } else if (sname.begins_with("custom_styles/")) {
  165. String name = sname.get_slice("/",1);
  166. r_ret= data.style_override.has(name)?Variant(data.style_override[name]):Variant();
  167. } else if (sname.begins_with("custom_fonts/")) {
  168. String name = sname.get_slice("/",1);
  169. r_ret= data.font_override.has(name)?Variant(data.font_override[name]):Variant();
  170. } else if (sname.begins_with("custom_colors/")) {
  171. String name = sname.get_slice("/",1);
  172. r_ret= data.color_override.has(name)?Variant(data.color_override[name]):Variant();
  173. } else if (sname.begins_with("custom_constants/")) {
  174. String name = sname.get_slice("/",1);
  175. r_ret= data.constant_override.has(name)?Variant(data.constant_override[name]):Variant();
  176. } else
  177. return false;
  178. return true;
  179. }
  180. void Control::_get_property_list( List<PropertyInfo> *p_list) const {
  181. Ref<Theme> theme;
  182. if (data.theme.is_valid()) {
  183. theme=data.theme;
  184. } else {
  185. theme=Theme::get_default();
  186. }
  187. {
  188. List<StringName> names;
  189. theme->get_icon_list(get_type_name(),&names);
  190. for(List<StringName>::Element *E=names.front();E;E=E->next()) {
  191. uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE;
  192. if (data.icon_override.has(E->get()))
  193. hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED;
  194. p_list->push_back( PropertyInfo(Variant::OBJECT,"custom_icons/"+E->get(),PROPERTY_HINT_RESOURCE_TYPE, "Texture",hint) );
  195. }
  196. }
  197. {
  198. List<StringName> names;
  199. theme->get_stylebox_list(get_type_name(),&names);
  200. for(List<StringName>::Element *E=names.front();E;E=E->next()) {
  201. uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE;
  202. if (data.style_override.has(E->get()))
  203. hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED;
  204. p_list->push_back( PropertyInfo(Variant::OBJECT,"custom_styles/"+E->get(),PROPERTY_HINT_RESOURCE_TYPE, "StyleBox",hint) );
  205. }
  206. }
  207. {
  208. List<StringName> names;
  209. theme->get_font_list(get_type_name(),&names);
  210. for(List<StringName>::Element *E=names.front();E;E=E->next()) {
  211. uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE;
  212. if (data.font_override.has(E->get()))
  213. hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED;
  214. p_list->push_back( PropertyInfo(Variant::OBJECT,"custom_fonts/"+E->get(),PROPERTY_HINT_RESOURCE_TYPE, "Font",hint) );
  215. }
  216. }
  217. {
  218. List<StringName> names;
  219. theme->get_color_list(get_type_name(),&names);
  220. for(List<StringName>::Element *E=names.front();E;E=E->next()) {
  221. uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE;
  222. if (data.color_override.has(E->get()))
  223. hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED;
  224. p_list->push_back( PropertyInfo(Variant::COLOR,"custom_colors/"+E->get(),PROPERTY_HINT_NONE, "",hint) );
  225. }
  226. }
  227. {
  228. List<StringName> names;
  229. theme->get_constant_list(get_type_name(),&names);
  230. for(List<StringName>::Element *E=names.front();E;E=E->next()) {
  231. uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE;
  232. if (data.constant_override.has(E->get()))
  233. hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED;
  234. p_list->push_back( PropertyInfo(Variant::INT,"custom_constants/"+E->get(),PROPERTY_HINT_RANGE, "-16384,16384",hint) );
  235. }
  236. }
  237. }
  238. Control *Control::get_parent_control() const {
  239. return data.parent;
  240. }
  241. void Control::_input_text(const String& p_text) {
  242. if (!window)
  243. return;
  244. if (window->key_focus)
  245. window->key_focus->call("set_text",p_text);
  246. }
  247. void Control::_gui_input(const InputEvent& p_event) {
  248. _window_input_event(p_event);
  249. }
  250. void Control::_resize(const Size2& p_size) {
  251. _size_changed();
  252. }
  253. void Control::_notification(int p_notification) {
  254. switch(p_notification) {
  255. case NOTIFICATION_ENTER_SCENE: {
  256. if (data.window==this) {
  257. window = memnew( Window );
  258. add_to_group("_vp_gui_input"+itos(get_viewport()->get_instance_ID()));
  259. add_to_group("windows");
  260. window->tooltip_timer = memnew( Timer );
  261. add_child(window->tooltip_timer);
  262. window->tooltip_timer->force_parent_owned();
  263. window->tooltip_timer->set_wait_time( GLOBAL_DEF("display/tooltip_delay",0.7));
  264. window->tooltip_timer->connect("timeout",this,"_window_show_tooltip");
  265. window->tooltip=NULL;
  266. window->tooltip_popup = memnew( TooltipPanel );
  267. add_child(window->tooltip_popup);
  268. window->tooltip_popup->force_parent_owned();
  269. window->tooltip_label = memnew( TooltipLabel );
  270. window->tooltip_popup->add_child(window->tooltip_label);
  271. window->tooltip_popup->set_as_toplevel(true);
  272. window->tooltip_popup->hide();
  273. window->drag_attempted=false;
  274. window->drag_preview=NULL;
  275. if (get_scene()->is_editor_hint()) {
  276. Node *n = this;
  277. while(n) {
  278. if (n->has_meta("_editor_disable_input")) {
  279. window->disable_input=true;
  280. break;
  281. }
  282. n=n->get_parent();
  283. }
  284. }
  285. } else {
  286. window=NULL;
  287. }
  288. _size_changed();
  289. } break;
  290. case NOTIFICATION_EXIT_SCENE: {
  291. if (data.window) {
  292. if (data.window->window->mouse_focus == this)
  293. data.window->window->mouse_focus=NULL;
  294. if (data.window->window->key_focus == this)
  295. data.window->window->key_focus=NULL;
  296. if (data.window->window->mouse_over == this)
  297. data.window->window->mouse_over=NULL;
  298. if (data.window->window->tooltip == this)
  299. data.window->window->tooltip=NULL;
  300. }
  301. if (window) {
  302. remove_from_group("_vp_gui_input"+itos(get_viewport()->get_instance_ID()));
  303. remove_from_group("windows");
  304. if (window->tooltip_timer)
  305. memdelete(window->tooltip_timer);
  306. window->tooltip_timer=NULL;
  307. window->tooltip=NULL;
  308. if (window->tooltip_popup)
  309. memdelete(window->tooltip_popup);
  310. window->tooltip_popup=NULL;
  311. memdelete(window);
  312. window=NULL;
  313. }
  314. } break;
  315. case NOTIFICATION_ENTER_CANVAS: {
  316. data.window=NULL;
  317. data.viewport=NULL;
  318. data.parent=NULL;
  319. Control *_window=this;
  320. bool gap=false;
  321. bool gap_valid=true;
  322. bool window_found=false;
  323. Node *parent=_window->get_parent();
  324. if (parent && parent->cast_to<Control>()) {
  325. data.parent=parent->cast_to<Control>();
  326. }
  327. Viewport *viewport=NULL;
  328. parent=this; //meh
  329. while(parent) {
  330. Control *c=parent->cast_to<Control>();
  331. if (!window_found && c) {
  332. if (!gap && c!=this) {
  333. gap_valid=false;
  334. }
  335. _window = c;
  336. }
  337. CanvasItem *ci =parent->cast_to<CanvasItem>();
  338. if ((ci && ci->is_set_as_toplevel()) || !ci) {
  339. gap=true;
  340. }
  341. if (parent->cast_to<CanvasLayer>()) {
  342. window_found=true; //don't go beyond canvas layer
  343. }
  344. viewport =parent->cast_to<Viewport>();
  345. if (viewport) {
  346. break; //no go beyond viewport either
  347. }
  348. parent=parent->get_parent();
  349. }
  350. data.window=_window;
  351. data.viewport=viewport;
  352. data.parent_canvas_item=get_parent_item();
  353. if (data.parent_canvas_item) {
  354. data.parent_canvas_item->connect("item_rect_changed",this,"_size_changed");
  355. } else if (data.viewport) {
  356. //connect viewport
  357. data.viewport->connect("size_changed",this,"_size_changed");
  358. } else {
  359. }
  360. if (gap && gap_valid && data.window!=this) {
  361. //is a subwindow, conditions to meet subwindow status are quite complex..
  362. data.SI = data.window->window->subwindows.push_back(this);
  363. data.window->window->subwindow_order_dirty=true;
  364. }
  365. } break;
  366. case NOTIFICATION_EXIT_CANVAS: {
  367. if (data.parent_canvas_item) {
  368. data.parent_canvas_item->disconnect("item_rect_changed",this,"_size_changed");
  369. data.parent_canvas_item=NULL;
  370. } else if (data.viewport) {
  371. //disconnect viewport
  372. data.viewport->disconnect("size_changed",this,"_size_changed");
  373. } else {
  374. }
  375. if (data.MI) {
  376. data.window->window->modal_stack.erase(data.MI);
  377. data.MI=NULL;
  378. }
  379. if (data.SI) {
  380. //erase from subwindows
  381. data.window->window->subwindows.erase(data.SI);
  382. data.SI=NULL;
  383. }
  384. data.viewport=NULL;
  385. data.window=NULL;
  386. data.parent=NULL;
  387. } break;
  388. case NOTIFICATION_PARENTED: {
  389. Control * parent = get_parent()->cast_to<Control>();
  390. //make children reference them theme
  391. if (parent && data.theme.is_null() && parent->data.theme_owner)
  392. _propagate_theme_changed(parent->data.theme_owner);
  393. } break;
  394. case NOTIFICATION_UNPARENTED: {
  395. //make children unreference the theme
  396. if (data.theme.is_null() && data.theme_owner)
  397. _propagate_theme_changed(NULL);
  398. } break;
  399. case NOTIFICATION_MOVED_IN_PARENT: {
  400. // some parents need to know the order of the childrens to draw (like TabContainer)
  401. // update if necesary
  402. if (data.parent)
  403. data.parent->update();
  404. update();
  405. if (data.SI && data.window) {
  406. data.window->window->subwindow_order_dirty=true;
  407. }
  408. } break;
  409. case NOTIFICATION_RESIZED: {
  410. emit_signal(SceneStringNames::get_singleton()->resized);
  411. } break;
  412. case NOTIFICATION_DRAW: {
  413. Matrix32 xform;
  414. xform.set_origin(get_pos());
  415. VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(),xform);
  416. VisualServer::get_singleton()->canvas_item_set_custom_rect( get_canvas_item(),true, Rect2(Point2(),get_size()));
  417. //emit_signal(SceneStringNames::get_singleton()->draw);
  418. } break;
  419. case NOTIFICATION_MOUSE_ENTER: {
  420. emit_signal(SceneStringNames::get_singleton()->mouse_enter);
  421. } break;
  422. case NOTIFICATION_MOUSE_EXIT: {
  423. emit_signal(SceneStringNames::get_singleton()->mouse_exit);
  424. } break;
  425. case NOTIFICATION_FOCUS_ENTER: {
  426. emit_signal(SceneStringNames::get_singleton()->focus_enter);
  427. update();
  428. } break;
  429. case NOTIFICATION_FOCUS_EXIT: {
  430. emit_signal(SceneStringNames::get_singleton()->focus_exit);
  431. update();
  432. } break;
  433. case NOTIFICATION_THEME_CHANGED: {
  434. update();
  435. } break;
  436. case NOTIFICATION_VISIBILITY_CHANGED: {
  437. if (!is_visible()) {
  438. if (data.window->window->mouse_focus == this) {
  439. data.window->window->mouse_focus=NULL;
  440. }
  441. if (data.window==this) {
  442. window->drag_data=Variant();
  443. if (window->drag_preview) {
  444. memdelete( window->drag_preview);
  445. window->drag_preview=NULL;
  446. }
  447. }
  448. if (data.window->window->key_focus == this)
  449. data.window->window->key_focus=NULL;
  450. if (data.window->window->mouse_over == this)
  451. data.window->window->mouse_over=NULL;
  452. if (data.window->window->tooltip == this)
  453. data.window->window->tooltip=NULL;
  454. if (data.window->window->tooltip == this)
  455. data.window->window->tooltip=NULL;
  456. _modal_stack_remove();
  457. minimum_size_changed();
  458. //remove key focus
  459. //remove modalness
  460. } else {
  461. _size_changed();
  462. }
  463. } break;
  464. case SceneMainLoop::NOTIFICATION_WM_UNFOCUS_REQUEST: {
  465. if (!window)
  466. return;
  467. if (window->key_focus)
  468. window->key_focus->release_focus();
  469. } break;
  470. }
  471. }
  472. bool Control::clips_input() const {
  473. return false;
  474. }
  475. bool Control::has_point(const Point2& p_point) const {
  476. if (get_script_instance()) {
  477. Variant v=p_point;
  478. const Variant *p=&v;
  479. Variant::CallError ce;
  480. Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->has_point,&p,1,ce);
  481. if (ce.error==Variant::CallError::CALL_OK) {
  482. return ret;
  483. }
  484. }
  485. /*if (has_stylebox("mask")) {
  486. Ref<StyleBox> mask = get_stylebox("mask");
  487. return mask->test_mask(p_point,Rect2(Point2(),get_size()));
  488. }*/
  489. return Rect2( Point2(), get_size() ).has_point(p_point);
  490. }
  491. Variant Control::get_drag_data(const Point2& p_point) {
  492. if (get_script_instance()) {
  493. Variant v=p_point;
  494. const Variant *p=&v;
  495. Variant::CallError ce;
  496. Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->get_drag_data,&p,1,ce);
  497. if (ce.error==Variant::CallError::CALL_OK)
  498. return ret;
  499. }
  500. return Variant();
  501. }
  502. bool Control::can_drop_data(const Point2& p_point,const Variant& p_data) const {
  503. if (get_script_instance()) {
  504. Variant v=p_point;
  505. const Variant *p[2]={&v,&p_data};
  506. Variant::CallError ce;
  507. Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->can_drop_data,p,2,ce);
  508. if (ce.error==Variant::CallError::CALL_OK)
  509. return ret;
  510. }
  511. return Variant();
  512. }
  513. void Control::drop_data(const Point2& p_point,const Variant& p_data){
  514. if (get_script_instance()) {
  515. Variant v=p_point;
  516. const Variant *p[2]={&v,&p_data};
  517. Variant::CallError ce;
  518. Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->drop_data,p,2,ce);
  519. if (ce.error==Variant::CallError::CALL_OK)
  520. return;
  521. }
  522. }
  523. void Control::force_drag(const Variant& p_data,Control *p_control) {
  524. ERR_FAIL_COND(!is_inside_scene());
  525. ERR_FAIL_COND(!data.window);
  526. ERR_FAIL_COND(p_data.get_type()==Variant::NIL);
  527. data.window->window->drag_data=p_data;
  528. data.window->window->mouse_focus=NULL;
  529. if (p_control) {
  530. data.window->set_drag_preview(p_control);
  531. }
  532. }
  533. void Control::set_drag_preview(Control *p_control) {
  534. ERR_FAIL_NULL(p_control);
  535. ERR_FAIL_COND( !((Object*)p_control)->cast_to<Control>());
  536. ERR_FAIL_COND(!is_inside_scene() || !data.window);
  537. ERR_FAIL_COND(p_control->is_inside_scene());
  538. ERR_FAIL_COND(p_control->get_parent()!=NULL);
  539. if (data.window->window->drag_preview) {
  540. memdelete(data.window->window->drag_preview);
  541. }
  542. p_control->set_as_toplevel(true);
  543. p_control->set_pos(data.window->window->last_mouse_pos);
  544. data.window->add_child(p_control);
  545. if (data.window->window->drag_preview) {
  546. memdelete( data.window->window->drag_preview );
  547. }
  548. data.window->window->drag_preview=p_control;
  549. }
  550. Control* Control::_find_next_visible_control_at_pos(Node* p_node,const Point2& p_global,Matrix32& r_xform) const {
  551. return NULL;
  552. }
  553. Control* Control::_find_control_at_pos(CanvasItem* p_node,const Point2& p_global,const Matrix32& p_xform,Matrix32& r_inv_xform) {
  554. if (p_node->cast_to<Viewport>())
  555. return NULL;
  556. Control *c=p_node->cast_to<Control>();
  557. if (c) {
  558. // print_line("at "+String(c->get_path())+" POS "+c->get_pos()+" bt "+p_xform);
  559. }
  560. if (c==data.window) {
  561. //try subwindows first!!
  562. c->_window_sort_subwindows(); // sort them
  563. for (List<Control*>::Element *E=c->window->subwindows.back();E;E=E->prev()) {
  564. Control *sw = E->get();
  565. if (!sw->is_visible())
  566. continue;
  567. Matrix32 xform;
  568. CanvasItem *pci = sw->get_parent_item();
  569. if (pci)
  570. xform=pci->get_global_transform();
  571. Control *ret = _find_control_at_pos(sw,p_global,xform,r_inv_xform);
  572. if (ret)
  573. return ret;
  574. }
  575. }
  576. if (p_node->is_hidden()) {
  577. //return _find_next_visible_control_at_pos(p_node,p_global,r_inv_xform);
  578. return NULL; //canvas item hidden, discard
  579. }
  580. Matrix32 matrix = p_xform * p_node->get_transform();
  581. if (!c || !c->clips_input() || c->has_point(matrix.affine_inverse().xform(p_global))) {
  582. for(int i=p_node->get_child_count()-1;i>=0;i--) {
  583. if (p_node==data.window->window->tooltip_popup)
  584. continue;
  585. CanvasItem *ci = p_node->get_child(i)->cast_to<CanvasItem>();
  586. if (!ci || ci->is_set_as_toplevel())
  587. continue;
  588. Control *ret=_find_control_at_pos(ci,p_global,matrix,r_inv_xform);;
  589. if (ret)
  590. return ret;
  591. }
  592. }
  593. if (!c)
  594. return NULL;
  595. matrix.affine_invert();
  596. //conditions for considering this as a valid control for return
  597. if (!c->data.ignore_mouse && c->has_point(matrix.xform(p_global)) && (!window->drag_preview || (c!=window->drag_preview && !window->drag_preview->is_a_parent_of(c)))) {
  598. r_inv_xform=matrix;
  599. return c;
  600. } else
  601. return NULL;
  602. }
  603. void Control::_window_cancel_input_ID(int p_input) {
  604. window->cancelled_input_ID=(unsigned int)p_input;
  605. }
  606. void Control::_window_remove_focus() {
  607. if (window->key_focus) {
  608. Node *f=window->key_focus;
  609. window->key_focus=NULL;
  610. f->notification( NOTIFICATION_FOCUS_EXIT,true );
  611. }
  612. }
  613. bool Control::window_has_modal_stack() const {
  614. if (!data.window)
  615. return false;
  616. return data.window->window->modal_stack.size();
  617. }
  618. void Control::_window_cancel_tooltip() {
  619. window->tooltip=NULL;
  620. if (window->tooltip_timer)
  621. window->tooltip_timer->stop();
  622. if (window->tooltip_popup)
  623. window->tooltip_popup->hide();
  624. }
  625. void Control::_window_show_tooltip() {
  626. if (!window->tooltip) {
  627. return;
  628. }
  629. String tooltip = window->tooltip->get_tooltip( window->tooltip->get_global_transform().xform_inv(window->tooltip_pos) );
  630. if (tooltip.length()==0)
  631. return; // bye
  632. if (!window->tooltip_label) {
  633. return;
  634. }
  635. Ref<StyleBox> ttp = get_stylebox("panel","TooltipPanel");
  636. window->tooltip_label->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_BEGIN,ttp->get_margin(MARGIN_LEFT));
  637. window->tooltip_label->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,ttp->get_margin(MARGIN_TOP));
  638. window->tooltip_label->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,ttp->get_margin(MARGIN_RIGHT));
  639. window->tooltip_label->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,ttp->get_margin(MARGIN_BOTTOM));
  640. window->tooltip_label->set_text(tooltip);
  641. Rect2 r(window->tooltip_pos+Point2(10,10),window->tooltip_label->get_combined_minimum_size()+ttp->get_minimum_size());
  642. Rect2 vr = get_viewport_rect();
  643. if (r.size.x+r.pos.x>vr.size.x)
  644. r.pos.x=vr.size.x-r.size.x;
  645. else if (r.pos.x<0)
  646. r.pos.x=0;
  647. if (r.size.y+r.pos.y>vr.size.y)
  648. r.pos.y=vr.size.y-r.size.y;
  649. else if (r.pos.y<0)
  650. r.pos.y=0;
  651. window->tooltip_popup->set_pos(r.pos);
  652. window->tooltip_popup->set_size(r.size);
  653. window->tooltip_popup->raise();
  654. window->tooltip_popup->show();
  655. }
  656. void Control::_window_call_input(Control *p_control,const InputEvent& p_input) {
  657. while(p_control) {
  658. p_control->call_multilevel(SceneStringNames::get_singleton()->_input_event,p_input);
  659. if (window->key_event_accepted)
  660. break;
  661. p_control->emit_signal(SceneStringNames::get_singleton()->input_event,p_input);
  662. if (p_control->is_set_as_toplevel()) {
  663. break;
  664. }
  665. if (window->key_event_accepted)
  666. break;
  667. if (p_control->data.stop_mouse && (p_input.type==InputEvent::MOUSE_BUTTON || p_input.type==InputEvent::MOUSE_MOTION))
  668. break;
  669. p_control=p_control->data.parent;
  670. }
  671. }
  672. void Control::_window_input_event(InputEvent p_event) {
  673. if (!window)
  674. return;
  675. if (window->disable_input)
  676. return;
  677. if (p_event.ID==window->cancelled_input_ID) {
  678. return;
  679. }
  680. if (!is_visible()) {
  681. return; //simple and plain
  682. }
  683. switch(p_event.type) {
  684. case InputEvent::MOUSE_BUTTON: {
  685. window->key_event_accepted=false;
  686. Point2 mpos =(get_canvas_transform()).affine_inverse().xform(Point2(p_event.mouse_button.x,p_event.mouse_button.y));
  687. if (p_event.mouse_button.pressed) {
  688. Size2 pos = mpos;
  689. if (window->mouse_focus && p_event.mouse_button.button_index!=window->mouse_focus_button) {
  690. //do not steal mouse focus and stuff
  691. } else {
  692. _window_sort_modal_stack();
  693. while (!window->modal_stack.empty()) {
  694. Control *top = window->modal_stack.back()->get();
  695. if (!top->has_point(top->get_global_transform().affine_inverse().xform(pos))) {
  696. if (top->data.modal_exclusive) {
  697. //cancel event, sorry, modal exclusive EATS UP ALL
  698. get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  699. get_scene()->set_input_as_handled();
  700. return; // no one gets the event if exclusive NO ONE
  701. }
  702. top->notification(NOTIFICATION_MODAL_CLOSE);
  703. top->_modal_stack_remove();
  704. top->hide();
  705. } else {
  706. break;
  707. }
  708. }
  709. Matrix32 parent_xform;
  710. if (data.parent_canvas_item)
  711. parent_xform=data.parent_canvas_item->get_global_transform();
  712. window->mouse_focus = _find_control_at_pos(this,pos,parent_xform,window->focus_inv_xform);
  713. //print_line("has mf "+itos(window->mouse_focus!=NULL));
  714. window->mouse_focus_button=p_event.mouse_button.button_index;
  715. if (!window->mouse_focus) {
  716. break;
  717. }
  718. if (p_event.mouse_button.button_index==BUTTON_LEFT) {
  719. window->drag_accum=Vector2();
  720. window->drag_attempted=false;
  721. window->drag_data=Variant();
  722. }
  723. }
  724. p_event.mouse_button.global_x = pos.x;
  725. p_event.mouse_button.global_y = pos.y;
  726. pos = window->focus_inv_xform.xform(pos);
  727. p_event.mouse_button.x = pos.x;
  728. p_event.mouse_button.y = pos.y;
  729. #ifdef DEBUG_ENABLED
  730. if (ScriptDebugger::get_singleton()) {
  731. Array arr;
  732. arr.push_back(window->mouse_focus->get_path());
  733. arr.push_back(window->mouse_focus->get_type());
  734. ScriptDebugger::get_singleton()->send_message("click_ctrl",arr);
  735. }
  736. /*if (bool(GLOBAL_DEF("debug/print_clicked_control",false))) {
  737. print_line(String(window->mouse_focus->get_path())+" - "+pos);
  738. }*/
  739. #endif
  740. if (window->mouse_focus->get_focus_mode()!=FOCUS_NONE && window->mouse_focus!=window->key_focus && p_event.mouse_button.button_index==BUTTON_LEFT) {
  741. // also get keyboard focus
  742. window->mouse_focus->grab_focus();
  743. }
  744. if (window->mouse_focus->can_process()) {
  745. _window_call_input(window->mouse_focus,p_event);
  746. }
  747. get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  748. get_scene()->set_input_as_handled();
  749. } else {
  750. if (window->drag_preview && p_event.mouse_button.button_index==BUTTON_LEFT) {
  751. memdelete( window->drag_preview );
  752. window->drag_preview=NULL;
  753. }
  754. if (!window->mouse_focus) {
  755. if (window->mouse_over && window->drag_data.get_type()!=Variant::NIL && p_event.mouse_button.button_index==BUTTON_LEFT) {
  756. Size2 pos = mpos;
  757. pos = window->focus_inv_xform.xform(pos);
  758. window->mouse_over->drop_data(pos,window->drag_data);
  759. window->drag_data=Variant();
  760. //change mouse accordingly
  761. }
  762. break;
  763. }
  764. Size2 pos = mpos;
  765. p_event.mouse_button.global_x = pos.x;
  766. p_event.mouse_button.global_y = pos.y;
  767. pos = window->focus_inv_xform.xform(pos);
  768. p_event.mouse_button.x = pos.x;
  769. p_event.mouse_button.y = pos.y;
  770. if (window->mouse_focus->can_process()) {
  771. _window_call_input(window->mouse_focus,p_event);
  772. }
  773. if (p_event.mouse_button.button_index==window->mouse_focus_button) {
  774. window->mouse_focus=NULL;
  775. window->mouse_focus_button=-1;
  776. }
  777. if (window->drag_data.get_type()!=Variant::NIL && p_event.mouse_button.button_index==BUTTON_LEFT) {
  778. window->drag_data=Variant(); //always clear
  779. }
  780. get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  781. get_scene()->set_input_as_handled();
  782. }
  783. } break;
  784. case InputEvent::MOUSE_MOTION: {
  785. window->key_event_accepted=false;
  786. Matrix32 localizer = (get_canvas_transform()).affine_inverse();
  787. Size2 pos = localizer.xform(Size2(p_event.mouse_motion.x,p_event.mouse_motion.y));
  788. Vector2 speed = localizer.basis_xform(Point2(p_event.mouse_motion.speed_x,p_event.mouse_motion.speed_y));
  789. Vector2 rel = localizer.basis_xform(Point2(p_event.mouse_motion.relative_x,p_event.mouse_motion.relative_y));
  790. window->last_mouse_pos=pos;
  791. Control *over = NULL;
  792. Matrix32 parent_xform;
  793. if (data.parent_canvas_item)
  794. parent_xform=data.parent_canvas_item->get_global_transform();
  795. // D&D
  796. if (!window->drag_attempted && window->mouse_focus && p_event.mouse_motion.button_mask&BUTTON_MASK_LEFT) {
  797. window->drag_accum+=rel;
  798. float len = window->drag_accum.length();
  799. if (len>10) {
  800. window->drag_data=window->mouse_focus->get_drag_data(window->focus_inv_xform.xform(pos)-window->drag_accum);
  801. if (window->drag_data.get_type()!=Variant::NIL) {
  802. window->mouse_focus=NULL;
  803. }
  804. window->drag_attempted=true;
  805. }
  806. }
  807. if (window->mouse_focus) {
  808. over=window->mouse_focus;
  809. //recompute focus_inv_xform again here
  810. } else {
  811. over = _find_control_at_pos(this,pos,parent_xform,window->focus_inv_xform);
  812. }
  813. if (window->drag_data.get_type()==Variant::NIL && over && !window->modal_stack.empty()) {
  814. Control *top = window->modal_stack.back()->get();
  815. if (over!=top && !top->is_a_parent_of(over)) {
  816. break; // don't send motion event to anything below modal stack top
  817. }
  818. }
  819. if (over!=window->mouse_over) {
  820. if (window->mouse_over)
  821. window->mouse_over->notification(NOTIFICATION_MOUSE_EXIT);
  822. if (over)
  823. over->notification(NOTIFICATION_MOUSE_ENTER);
  824. }
  825. window->mouse_over=over;
  826. get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,"windows","_cancel_tooltip");
  827. if (window->drag_preview) {
  828. window->drag_preview->set_pos(pos);
  829. }
  830. if (!over) {
  831. OS::get_singleton()->set_cursor_shape(OS::CURSOR_ARROW);
  832. break;
  833. }
  834. p_event.mouse_motion.global_x = pos.x;
  835. p_event.mouse_motion.global_y = pos.y;
  836. p_event.mouse_motion.speed_x=speed.x;
  837. p_event.mouse_motion.speed_y=speed.y;
  838. p_event.mouse_motion.relative_x=rel.x;
  839. p_event.mouse_motion.relative_y=rel.y;
  840. if (p_event.mouse_motion.button_mask==0 && window->tooltip_timer) {
  841. //nothing pressed
  842. bool can_tooltip=true;
  843. if (!window->modal_stack.empty()) {
  844. if (window->modal_stack.back()->get()!=over && !window->modal_stack.back()->get()->is_a_parent_of(over))
  845. can_tooltip=false;
  846. }
  847. if (can_tooltip) {
  848. window->tooltip=over;
  849. window->tooltip_pos=(parent_xform * get_transform()).affine_inverse().xform(pos);
  850. window->tooltip_timer->start();
  851. }
  852. }
  853. pos = window->focus_inv_xform.xform(pos);
  854. p_event.mouse_motion.x = pos.x;
  855. p_event.mouse_motion.y = pos.y;
  856. CursorShape cursor_shape = over->get_cursor_shape(pos);
  857. OS::get_singleton()->set_cursor_shape( (OS::CursorShape)cursor_shape );
  858. if (over->can_process()) {
  859. _window_call_input(over,p_event);
  860. }
  861. get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  862. get_scene()->set_input_as_handled();
  863. if (window->drag_data.get_type()!=Variant::NIL && p_event.mouse_motion.button_mask&BUTTON_MASK_LEFT) {
  864. /*bool can_drop =*/ over->can_drop_data(pos,window->drag_data);
  865. //change mouse accordingly i guess
  866. }
  867. } break;
  868. case InputEvent::ACTION:
  869. case InputEvent::JOYSTICK_BUTTON:
  870. case InputEvent::KEY: {
  871. if (window->key_focus) {
  872. window->key_event_accepted=false;
  873. if (window->key_focus->can_process()) {
  874. window->key_focus->call_multilevel("_input_event",p_event);
  875. if (window->key_focus) //maybe lost it
  876. window->key_focus->emit_signal(SceneStringNames::get_singleton()->input_event,p_event);
  877. }
  878. if (window->key_event_accepted) {
  879. get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  880. break;
  881. }
  882. }
  883. if (p_event.is_pressed() && p_event.is_action("ui_cancel") && !window->modal_stack.empty()) {
  884. _window_sort_modal_stack();
  885. Control *top = window->modal_stack.back()->get();
  886. if (!top->data.modal_exclusive) {
  887. top->notification(NOTIFICATION_MODAL_CLOSE);
  888. top->_modal_stack_remove();
  889. top->hide();
  890. }
  891. }
  892. Control * from = window->key_focus ? window->key_focus : NULL; //hmm
  893. //keyboard focus
  894. //if (from && p_event.key.pressed && !p_event.key.mod.alt && !p_event.key.mod.meta && !p_event.key.mod.command) {
  895. if (from && p_event.is_pressed()) {
  896. Control * next=NULL;
  897. if (p_event.is_action("ui_focus_next")) {
  898. next = from->find_next_valid_focus();
  899. }
  900. if (p_event.is_action("ui_focus_prev")) {
  901. next = from->find_prev_valid_focus();
  902. }
  903. if (p_event.is_action("ui_up")) {
  904. next = from->_get_focus_neighbour(MARGIN_TOP);
  905. }
  906. if (p_event.is_action("ui_left")) {
  907. next = from->_get_focus_neighbour(MARGIN_LEFT);
  908. }
  909. if (p_event.is_action("ui_right")) {
  910. next = from->_get_focus_neighbour(MARGIN_RIGHT);
  911. }
  912. if (p_event.is_action("ui_down")) {
  913. next = from->_get_focus_neighbour(MARGIN_BOTTOM);
  914. }
  915. if (next) {
  916. next->grab_focus();
  917. get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  918. }
  919. }
  920. } break;
  921. }
  922. }
  923. Control *Control::get_window() const {
  924. return data.window;
  925. }
  926. bool Control::is_window() const {
  927. return (is_inside_scene() && window);
  928. }
  929. Size2 Control::get_minimum_size() const {
  930. ScriptInstance *si = const_cast<Control*>(this)->get_script_instance();
  931. if (si) {
  932. Variant::CallError ce;
  933. Variant s = si->call(SceneStringNames::get_singleton()->get_minimum_size,NULL,0,ce);
  934. if (ce.error==Variant::CallError::CALL_OK)
  935. return s;
  936. }
  937. return Size2();
  938. }
  939. Ref<Texture> Control::get_icon(const StringName& p_name,const StringName& p_type) const {
  940. const Ref<Texture>* tex = data.icon_override.getptr(p_name);
  941. if (tex)
  942. return *tex;
  943. StringName type = p_type?p_type:get_type_name();
  944. // try with custom themes
  945. Control *theme_owner = data.theme_owner;
  946. while(theme_owner) {
  947. if (theme_owner->data.theme->has_icon(p_name, type ) )
  948. return data.theme_owner->data.theme->get_icon(p_name, type );
  949. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  950. if (parent)
  951. theme_owner=parent->data.theme_owner;
  952. else
  953. theme_owner=NULL;
  954. }
  955. return Theme::get_default()->get_icon( p_name, type );
  956. }
  957. Ref<StyleBox> Control::get_stylebox(const StringName& p_name,const StringName& p_type) const {
  958. const Ref<StyleBox>* style = data.style_override.getptr(p_name);
  959. if (style)
  960. return *style;
  961. StringName type = p_type?p_type:get_type_name();
  962. // try with custom themes
  963. Control *theme_owner = data.theme_owner;
  964. while(theme_owner) {
  965. if (theme_owner->data.theme->has_stylebox(p_name, type ) )
  966. return data.theme_owner->data.theme->get_stylebox(p_name, type );
  967. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  968. if (parent)
  969. theme_owner=parent->data.theme_owner;
  970. else
  971. theme_owner=NULL;
  972. }
  973. return Theme::get_default()->get_stylebox( p_name, type );
  974. }
  975. Ref<Font> Control::get_font(const StringName& p_name,const StringName& p_type) const {
  976. const Ref<Font>* font = data.font_override.getptr(p_name);
  977. if (font)
  978. return *font;
  979. StringName type = p_type?p_type:get_type_name();
  980. // try with custom themes
  981. Control *theme_owner = data.theme_owner;
  982. while(theme_owner) {
  983. if (theme_owner->data.theme->has_font(p_name, type ) )
  984. return data.theme_owner->data.theme->get_font(p_name, type );
  985. if (theme_owner->data.theme->get_default_theme_font().is_valid())
  986. return theme_owner->data.theme->get_default_theme_font();
  987. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  988. if (parent)
  989. theme_owner=parent->data.theme_owner;
  990. else
  991. theme_owner=NULL;
  992. }
  993. return Theme::get_default()->get_font( p_name, type );
  994. }
  995. Color Control::get_color(const StringName& p_name,const StringName& p_type) const {
  996. const Color* color = data.color_override.getptr(p_name);
  997. if (color)
  998. return *color;
  999. StringName type = p_type?p_type:get_type_name();
  1000. // try with custom themes
  1001. Control *theme_owner = data.theme_owner;
  1002. while(theme_owner) {
  1003. if (theme_owner->data.theme->has_color(p_name, type ) )
  1004. return data.theme_owner->data.theme->get_color(p_name, type );
  1005. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  1006. if (parent)
  1007. theme_owner=parent->data.theme_owner;
  1008. else
  1009. theme_owner=NULL;
  1010. }
  1011. return Theme::get_default()->get_color( p_name, type );
  1012. }
  1013. int Control::get_constant(const StringName& p_name,const StringName& p_type) const {
  1014. const int* constant = data.constant_override.getptr(p_name);
  1015. if (constant)
  1016. return *constant;
  1017. StringName type = p_type?p_type:get_type_name();
  1018. // try with custom themes
  1019. Control *theme_owner = data.theme_owner;
  1020. while(theme_owner) {
  1021. if (theme_owner->data.theme->has_constant(p_name, type ) )
  1022. return data.theme_owner->data.theme->get_constant(p_name, type );
  1023. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  1024. if (parent)
  1025. theme_owner=parent->data.theme_owner;
  1026. else
  1027. theme_owner=NULL;
  1028. }
  1029. return Theme::get_default()->get_constant( p_name, type );
  1030. }
  1031. bool Control::has_icon(const StringName& p_name,const StringName& p_type) const {
  1032. const Ref<Texture>* tex = data.icon_override.getptr(p_name);
  1033. if (tex)
  1034. return true;
  1035. StringName type = p_type?p_type:get_type_name();
  1036. // try with custom themes
  1037. Control *theme_owner = data.theme_owner;
  1038. while(theme_owner) {
  1039. if (theme_owner->data.theme->has_icon(p_name, type ) )
  1040. return true;
  1041. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  1042. if (parent)
  1043. theme_owner=parent->data.theme_owner;
  1044. else
  1045. theme_owner=NULL;
  1046. }
  1047. return Theme::get_default()->has_icon( p_name, type );
  1048. }
  1049. bool Control::has_stylebox(const StringName& p_name,const StringName& p_type) const {
  1050. const Ref<StyleBox>* style = data.style_override.getptr(p_name);
  1051. if (style)
  1052. return true;
  1053. StringName type = p_type?p_type:get_type_name();
  1054. // try with custom themes
  1055. Control *theme_owner = data.theme_owner;
  1056. while(theme_owner) {
  1057. if (theme_owner->data.theme->has_stylebox(p_name, type ) )
  1058. return true;
  1059. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  1060. if (parent)
  1061. theme_owner=parent->data.theme_owner;
  1062. else
  1063. theme_owner=NULL;
  1064. }
  1065. return Theme::get_default()->has_stylebox( p_name, type );
  1066. }
  1067. bool Control::has_font(const StringName& p_name,const StringName& p_type) const {
  1068. const Ref<Font>* font = data.font_override.getptr(p_name);
  1069. if (font)
  1070. return true;
  1071. StringName type = p_type?p_type:get_type_name();
  1072. // try with custom themes
  1073. Control *theme_owner = data.theme_owner;
  1074. while(theme_owner) {
  1075. if (theme_owner->data.theme->has_font(p_name, type ) )
  1076. return true;
  1077. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  1078. if (parent)
  1079. theme_owner=parent->data.theme_owner;
  1080. else
  1081. theme_owner=NULL;
  1082. }
  1083. return Theme::get_default()->has_font( p_name, type );
  1084. }
  1085. bool Control::has_color(const StringName& p_name,const StringName& p_type) const {
  1086. const Color* color = data.color_override.getptr(p_name);
  1087. if (color)
  1088. return true;
  1089. StringName type = p_type?p_type:get_type_name();
  1090. // try with custom themes
  1091. Control *theme_owner = data.theme_owner;
  1092. while(theme_owner) {
  1093. if (theme_owner->data.theme->has_color(p_name, type ) )
  1094. return true;
  1095. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  1096. if (parent)
  1097. theme_owner=parent->data.theme_owner;
  1098. else
  1099. theme_owner=NULL;
  1100. }
  1101. return Theme::get_default()->has_color( p_name, type );
  1102. }
  1103. bool Control::has_constant(const StringName& p_name,const StringName& p_type) const {
  1104. const int* constant = data.constant_override.getptr(p_name);
  1105. if (constant)
  1106. return true;
  1107. StringName type = p_type?p_type:get_type_name();
  1108. // try with custom themes
  1109. Control *theme_owner = data.theme_owner;
  1110. while(theme_owner) {
  1111. if (theme_owner->data.theme->has_constant(p_name, type ) )
  1112. return true;
  1113. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  1114. if (parent)
  1115. theme_owner=parent->data.theme_owner;
  1116. else
  1117. theme_owner=NULL;
  1118. }
  1119. return Theme::get_default()->has_constant( p_name, type );
  1120. }
  1121. Size2 Control::get_parent_area_size() const {
  1122. ERR_FAIL_COND_V(!is_inside_scene(),Size2());
  1123. Size2 parent_size;
  1124. if (data.parent_canvas_item) {
  1125. parent_size=data.parent_canvas_item->get_item_rect().size;
  1126. } else if (data.viewport) {
  1127. parent_size=data.viewport->get_visible_rect().size;
  1128. }
  1129. return parent_size;
  1130. }
  1131. void Control::_size_changed() {
  1132. if (!is_inside_scene())
  1133. return;
  1134. Size2 parent_size = get_parent_area_size();
  1135. float margin_pos[4];
  1136. for(int i=0;i<4;i++) {
  1137. float area = parent_size[i&1];
  1138. switch(data.anchor[i]) {
  1139. case ANCHOR_BEGIN: {
  1140. margin_pos[i]=data.margin[i];
  1141. } break;
  1142. case ANCHOR_END: {
  1143. margin_pos[i]=area-data.margin[i];
  1144. } break;
  1145. case ANCHOR_RATIO: {
  1146. margin_pos[i]=area*data.margin[i];
  1147. } break;
  1148. case ANCHOR_CENTER: {
  1149. margin_pos[i]=(area/2)-data.margin[i];
  1150. } break;
  1151. }
  1152. }
  1153. Point2 new_pos_cache=Point2(margin_pos[0],margin_pos[1]).floor();
  1154. Size2 new_size_cache=Point2(margin_pos[2],margin_pos[3]).floor()-new_pos_cache;
  1155. Size2 minimum_size=get_combined_minimum_size();
  1156. new_size_cache.x = MAX( minimum_size.x, new_size_cache.x );
  1157. new_size_cache.y = MAX( minimum_size.y, new_size_cache.y );
  1158. if (new_pos_cache == data.pos_cache && new_size_cache == data.size_cache)
  1159. return; // did not change, don't emit signal
  1160. data.pos_cache=new_pos_cache;
  1161. data.size_cache=new_size_cache;
  1162. notification(NOTIFICATION_RESIZED);
  1163. item_rect_changed();
  1164. _change_notify_margins();
  1165. _notify_transform();
  1166. }
  1167. float Control::_get_parent_range(int p_idx) const {
  1168. if (!is_inside_scene()) {
  1169. return 1.0;
  1170. } if (data.parent_canvas_item) {
  1171. return data.parent_canvas_item->get_item_rect().size[p_idx&1];
  1172. } else if (data.viewport) {
  1173. return data.viewport->get_visible_rect().size[p_idx&1];
  1174. }
  1175. return 1.0;
  1176. }
  1177. float Control::_get_range(int p_idx) const {
  1178. p_idx&=1;
  1179. float parent_range = _get_parent_range( p_idx );
  1180. float from = _a2s( data.margin[p_idx], data.anchor[p_idx], parent_range );
  1181. float to = _a2s( data.margin[p_idx+2], data.anchor[p_idx+2], parent_range );
  1182. return to-from;
  1183. }
  1184. float Control::_s2a(float p_val, AnchorType p_anchor,float p_range) const {
  1185. switch(p_anchor) {
  1186. case ANCHOR_BEGIN: {
  1187. return p_val;
  1188. } break;
  1189. case ANCHOR_END: {
  1190. return p_range-p_val;
  1191. } break;
  1192. case ANCHOR_RATIO: {
  1193. return p_val/p_range;
  1194. } break;
  1195. case ANCHOR_CENTER: {
  1196. return (p_range/2)-p_val;
  1197. } break;
  1198. }
  1199. return 0;
  1200. }
  1201. float Control::_a2s(float p_val, AnchorType p_anchor,float p_range) const {
  1202. switch(p_anchor) {
  1203. case ANCHOR_BEGIN: {
  1204. return Math::floor(p_val);
  1205. } break;
  1206. case ANCHOR_END: {
  1207. return Math::floor(p_range-p_val);
  1208. } break;
  1209. case ANCHOR_RATIO: {
  1210. return Math::floor(p_range*p_val);
  1211. } break;
  1212. case ANCHOR_CENTER: {
  1213. return Math::floor((p_range/2)-p_val);
  1214. } break;
  1215. }
  1216. return 0;
  1217. }
  1218. void Control::set_anchor(Margin p_margin,AnchorType p_anchor) {
  1219. if (!is_inside_scene()) {
  1220. data.anchor[p_margin]=p_anchor;
  1221. } else {
  1222. float pr = _get_parent_range(p_margin);
  1223. float s = _a2s( data.margin[p_margin], data.anchor[p_margin], pr );
  1224. data.anchor[p_margin]=p_anchor;
  1225. data.margin[p_margin] = _s2a( s, p_anchor, pr );
  1226. }
  1227. _change_notify();
  1228. }
  1229. void Control::set_anchor_and_margin(Margin p_margin,AnchorType p_anchor, float p_pos) {
  1230. set_anchor(p_margin,p_anchor);
  1231. set_margin(p_margin,p_pos);
  1232. }
  1233. Control::AnchorType Control::get_anchor(Margin p_margin) const {
  1234. return data.anchor[p_margin];
  1235. }
  1236. void Control::_change_notify_margins() {
  1237. // this avoids sending the whole object data again on a change
  1238. _change_notify("margin/left");
  1239. _change_notify("margin/top");
  1240. _change_notify("margin/right");
  1241. _change_notify("margin/bottom");
  1242. _change_notify("rect/pos");
  1243. _change_notify("rect/size");
  1244. }
  1245. void Control::set_margin(Margin p_margin,float p_value) {
  1246. data.margin[p_margin]=p_value;
  1247. _size_changed();
  1248. }
  1249. void Control::set_begin(const Size2& p_point) {
  1250. data.margin[0]=p_point.x;
  1251. data.margin[1]=p_point.y;
  1252. _size_changed();
  1253. }
  1254. void Control::set_end(const Size2& p_point) {
  1255. data.margin[2]=p_point.x;
  1256. data.margin[3]=p_point.y;
  1257. _size_changed();
  1258. }
  1259. float Control::get_margin(Margin p_margin) const {
  1260. return data.margin[p_margin];
  1261. }
  1262. Size2 Control::get_begin() const {
  1263. return Size2( data.margin[0], data.margin[1] );
  1264. }
  1265. Size2 Control::get_end() const {
  1266. return Size2( data.margin[2], data.margin[3] );
  1267. }
  1268. Point2 Control::get_global_pos() const {
  1269. return get_global_transform().get_origin();
  1270. }
  1271. void Control::set_global_pos(const Point2& p_point) {
  1272. Matrix32 inv;
  1273. if (data.parent_canvas_item) {
  1274. inv = data.parent_canvas_item->get_global_transform().affine_inverse();
  1275. }
  1276. set_pos(inv.xform(p_point));
  1277. }
  1278. void Control::set_pos(const Size2& p_point) {
  1279. float pw = _get_parent_range(0);
  1280. float ph = _get_parent_range(1);
  1281. float x = _a2s( data.margin[0], data.anchor[0], pw );
  1282. float y = _a2s( data.margin[1], data.anchor[1], ph );
  1283. float x2 = _a2s( data.margin[2], data.anchor[2], pw );
  1284. float y2 = _a2s( data.margin[3], data.anchor[3], ph );
  1285. Size2 ret = Size2(x2-x,y2-y);
  1286. Size2 min = get_combined_minimum_size();
  1287. Size2 size = Size2(MAX( min.width, ret.width),MAX( min.height, ret.height));
  1288. float w=size.x;
  1289. float h=size.y;
  1290. x=p_point.x;
  1291. y=p_point.y;
  1292. data.margin[0] = _s2a( x, data.anchor[0], pw );
  1293. data.margin[1] = _s2a( y, data.anchor[1], ph );
  1294. data.margin[2] = _s2a( x+w, data.anchor[2], pw );
  1295. data.margin[3] = _s2a( y+h, data.anchor[3], ph );
  1296. _size_changed();
  1297. }
  1298. void Control::set_size(const Size2& p_size) {
  1299. Size2 new_size=p_size;
  1300. Size2 min=get_combined_minimum_size();
  1301. if (new_size.x<min.x)
  1302. new_size.x=min.x;
  1303. if (new_size.y<min.y)
  1304. new_size.y=min.y;
  1305. float pw = _get_parent_range(0);
  1306. float ph = _get_parent_range(1);
  1307. float x = _a2s( data.margin[0], data.anchor[0], pw );
  1308. float y = _a2s( data.margin[1], data.anchor[1], ph );
  1309. float w=new_size.width;
  1310. float h=new_size.height;
  1311. data.margin[2] = _s2a( x+w, data.anchor[2], pw );
  1312. data.margin[3] = _s2a( y+h, data.anchor[3], ph );
  1313. _size_changed();
  1314. }
  1315. Size2 Control::get_pos() const {
  1316. return data.pos_cache;
  1317. }
  1318. Size2 Control::get_size() const {
  1319. return data.size_cache;
  1320. }
  1321. Rect2 Control::get_global_rect() const {
  1322. return Rect2( get_global_pos(), get_size() );
  1323. }
  1324. Rect2 Control::get_window_rect() const {
  1325. Rect2 gr = get_global_rect();
  1326. if (data.viewport)
  1327. gr.pos+=data.viewport->get_visible_rect().pos;
  1328. return gr;
  1329. }
  1330. Rect2 Control::get_rect() const {
  1331. return Rect2(get_pos(),get_size());
  1332. }
  1333. Rect2 Control::get_item_rect() const {
  1334. return Rect2(Point2(),get_size());
  1335. }
  1336. void Control::set_area_as_parent_rect(int p_margin) {
  1337. data.anchor[MARGIN_LEFT]=ANCHOR_BEGIN;
  1338. data.anchor[MARGIN_TOP]=ANCHOR_BEGIN;
  1339. data.anchor[MARGIN_RIGHT]=ANCHOR_END;
  1340. data.anchor[MARGIN_BOTTOM]=ANCHOR_END;
  1341. for(int i=0;i<4;i++)
  1342. data.margin[i]=p_margin;
  1343. _size_changed();
  1344. }
  1345. void Control::add_icon_override(const StringName& p_name, const Ref<Texture>& p_icon) {
  1346. ERR_FAIL_COND(p_icon.is_null());
  1347. data.icon_override[p_name]=p_icon;
  1348. notification(NOTIFICATION_THEME_CHANGED);
  1349. update();
  1350. }
  1351. void Control::add_style_override(const StringName& p_name, const Ref<StyleBox>& p_style) {
  1352. ERR_FAIL_COND(p_style.is_null());
  1353. data.style_override[p_name]=p_style;
  1354. notification(NOTIFICATION_THEME_CHANGED);
  1355. update();
  1356. }
  1357. void Control::add_font_override(const StringName& p_name, const Ref<Font>& p_font) {
  1358. ERR_FAIL_COND(p_font.is_null());
  1359. data.font_override[p_name]=p_font;
  1360. notification(NOTIFICATION_THEME_CHANGED);
  1361. update();
  1362. }
  1363. void Control::add_color_override(const StringName& p_name, const Color& p_color) {
  1364. data.color_override[p_name]=p_color;
  1365. notification(NOTIFICATION_THEME_CHANGED);
  1366. update();
  1367. }
  1368. void Control::add_constant_override(const StringName& p_name, int p_constant) {
  1369. data.constant_override[p_name]=p_constant;
  1370. notification(NOTIFICATION_THEME_CHANGED);
  1371. update();
  1372. }
  1373. void Control::set_focus_mode(FocusMode p_focus_mode) {
  1374. if (is_inside_scene() && p_focus_mode == FOCUS_NONE && data.focus_mode!=FOCUS_NONE && has_focus())
  1375. release_focus();
  1376. data.focus_mode=p_focus_mode;
  1377. }
  1378. static Control *_next_control(Control *p_from) {
  1379. if (p_from->is_set_as_toplevel())
  1380. return NULL; // can't go above
  1381. Control *parent = p_from->get_parent()?p_from->get_parent()->cast_to<Control>():NULL;
  1382. if (!parent) {
  1383. return NULL;
  1384. }
  1385. int next = p_from->get_position_in_parent();
  1386. ERR_FAIL_INDEX_V(next,parent->get_child_count(),NULL);
  1387. for(int i=(next+1);i<parent->get_child_count();i++) {
  1388. Control *c = parent->get_child(i)->cast_to<Control>();
  1389. if (!c || !c->is_visible() || c->is_set_as_toplevel())
  1390. continue;
  1391. return c;
  1392. }
  1393. //no next in parent, try the same in parent
  1394. return _next_control(parent);
  1395. }
  1396. Control *Control::find_next_valid_focus() const {
  1397. Control *from = const_cast<Control*>(this);
  1398. while(true) {
  1399. // find next child
  1400. Control *next_child=NULL;
  1401. for(int i=0;i<from->get_child_count();i++) {
  1402. Control *c = from->get_child(i)->cast_to<Control>();
  1403. if (!c || !c->is_visible() || c->is_set_as_toplevel()) {
  1404. continue;
  1405. }
  1406. next_child=c;
  1407. break;
  1408. }
  1409. if (next_child) {
  1410. from = next_child;
  1411. } else {
  1412. next_child=_next_control(from);
  1413. if (!next_child) { //nothing else.. go up and find either window or subwindow
  1414. next_child=const_cast<Control*>(this);
  1415. while(next_child && !next_child->is_set_as_toplevel()) {
  1416. if (next_child->get_parent()) {
  1417. next_child=next_child->get_parent()->cast_to<Control>();
  1418. } else
  1419. next_child=NULL;
  1420. }
  1421. if (!next_child) {
  1422. next_child=get_window();
  1423. }
  1424. }
  1425. }
  1426. if (next_child==this) // no next control->
  1427. return (get_focus_mode()==FOCUS_ALL)?next_child:NULL;
  1428. if (next_child->get_focus_mode()==FOCUS_ALL)
  1429. return next_child;
  1430. from = next_child;
  1431. }
  1432. return NULL;
  1433. }
  1434. static Control *_prev_control(Control *p_from) {
  1435. Control *child=NULL;
  1436. for(int i=p_from->get_child_count()-1;i>=0;i--) {
  1437. Control *c = p_from->get_child(i)->cast_to<Control>();
  1438. if (!c || !c->is_visible() || c->is_set_as_toplevel())
  1439. continue;
  1440. child=c;
  1441. break;
  1442. }
  1443. if (!child)
  1444. return p_from;
  1445. //no prev in parent, try the same in parent
  1446. return _prev_control(child);
  1447. }
  1448. Control *Control::find_prev_valid_focus() const {
  1449. Control *from = const_cast<Control*>(this);
  1450. while(true) {
  1451. // find prev child
  1452. Control *prev_child = NULL;
  1453. if ( from->is_set_as_toplevel() || !from->get_parent() || !from->get_parent()->cast_to<Control>()) {
  1454. //find last of the childs
  1455. prev_child=_prev_control(from);
  1456. } else {
  1457. for(int i=(from->get_position_in_parent()-1);i>=0;i--) {
  1458. Control *c = from->get_parent()->get_child(i)->cast_to<Control>();
  1459. if (!c || !c->is_visible() || c->is_set_as_toplevel()) {
  1460. continue;
  1461. }
  1462. prev_child=c;
  1463. break;
  1464. }
  1465. if (!prev_child) {
  1466. prev_child = from->get_parent()->cast_to<Control>();
  1467. } else {
  1468. prev_child = _prev_control(prev_child);
  1469. }
  1470. }
  1471. if (prev_child==this) // no prev control->
  1472. return (get_focus_mode()==FOCUS_ALL)?prev_child:NULL;
  1473. if (prev_child->get_focus_mode()==FOCUS_ALL)
  1474. return prev_child;
  1475. from = prev_child;
  1476. }
  1477. return NULL;
  1478. return NULL;
  1479. }
  1480. Control::FocusMode Control::get_focus_mode() const {
  1481. return data.focus_mode;
  1482. }
  1483. bool Control::has_focus() const {
  1484. return (data.window && data.window->window->key_focus==this);
  1485. }
  1486. void Control::grab_focus() {
  1487. ERR_FAIL_COND(!is_inside_scene());
  1488. ERR_FAIL_COND(!data.window);
  1489. if (data.focus_mode==FOCUS_NONE)
  1490. return;
  1491. //no need for change
  1492. if (data.window->window->key_focus && data.window->window->key_focus==this)
  1493. return;
  1494. get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,"windows","_window_remove_focus");
  1495. data.window->window->key_focus=this;
  1496. notification(NOTIFICATION_FOCUS_ENTER);
  1497. #ifdef DEBUG_ENABLED
  1498. if (GLOBAL_DEF("debug/print_clicked_control", false)) {
  1499. print_line(String(get_path())+" - focus");
  1500. };
  1501. #endif
  1502. update();
  1503. }
  1504. void Control::release_focus() {
  1505. ERR_FAIL_COND(!is_inside_scene());
  1506. ERR_FAIL_COND(!data.window);
  1507. if (!has_focus())
  1508. return;
  1509. get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,"windows","_window_remove_focus");
  1510. //data.window->window->key_focus=this;
  1511. //notification(NOTIFICATION_FOCUS_ENTER);
  1512. update();
  1513. }
  1514. bool Control::is_toplevel_control() const {
  1515. return is_inside_scene() && (!data.parent_canvas_item && !window && is_set_as_toplevel());
  1516. }
  1517. void Control::show_modal(bool p_exclusive) {
  1518. ERR_FAIL_COND(!is_inside_scene());
  1519. ERR_FAIL_COND(!data.SI && data.window!=this);
  1520. ERR_FAIL_COND(!data.window);
  1521. if (is_visible())
  1522. hide();
  1523. ERR_FAIL_COND( data.MI );
  1524. show();
  1525. raise();
  1526. data.window->window->modal_stack.push_back(this);
  1527. data.MI = data.window->window->modal_stack.back();
  1528. data.modal_exclusive=p_exclusive;
  1529. if (data.window->window->key_focus)
  1530. data.modal_prev_focus_owner = data.window->window->key_focus->get_instance_ID();
  1531. else
  1532. data.modal_prev_focus_owner=0;
  1533. }
  1534. void Control::_window_sort_subwindows() {
  1535. if (!window->subwindow_order_dirty)
  1536. return;
  1537. window->modal_stack.sort_custom<CComparator>();
  1538. window->subwindow_order_dirty=false;
  1539. }
  1540. void Control::_window_sort_modal_stack() {
  1541. window->modal_stack.sort_custom<CComparator>();
  1542. }
  1543. void Control::_modal_stack_remove() {
  1544. List<Control*>::Element *next=NULL; //transfer the focus stack to the next
  1545. if (data.window && data.MI) {
  1546. next = data.MI->next();
  1547. data.window->window->modal_stack.erase(data.MI);
  1548. data.MI=NULL;
  1549. }
  1550. if (data.modal_prev_focus_owner) {
  1551. if (!next) { //top of stack
  1552. Object *pfo = ObjectDB::get_instance(data.modal_prev_focus_owner);
  1553. Control *pfoc = pfo->cast_to<Control>();
  1554. if (!pfoc)
  1555. return;
  1556. if (!pfoc->is_inside_scene() || !pfoc->is_visible())
  1557. return;
  1558. pfoc->grab_focus();
  1559. } else {
  1560. next->get()->data.modal_prev_focus_owner=data.modal_prev_focus_owner;
  1561. }
  1562. data.modal_prev_focus_owner=0;
  1563. }
  1564. }
  1565. void Control::_propagate_theme_changed(Control *p_owner) {
  1566. for(int i=0;i<get_child_count();i++) {
  1567. Control *child = get_child(i)->cast_to<Control>();
  1568. if (child && child->data.theme.is_null()) //has no theme, propagate
  1569. child->_propagate_theme_changed(p_owner);
  1570. }
  1571. data.theme_owner=p_owner;
  1572. _notification(NOTIFICATION_THEME_CHANGED);
  1573. update();
  1574. }
  1575. void Control::set_theme(const Ref<Theme>& p_theme) {
  1576. data.theme=p_theme;
  1577. if (!p_theme.is_null()) {
  1578. _propagate_theme_changed(this);
  1579. } else {
  1580. Control *parent = get_parent()?get_parent()->cast_to<Control>():NULL;
  1581. if (parent && parent->data.theme_owner) {
  1582. _propagate_theme_changed(parent->data.theme_owner);
  1583. } else {
  1584. _propagate_theme_changed(NULL);
  1585. }
  1586. }
  1587. }
  1588. void Control::_window_accept_event() {
  1589. window->key_event_accepted=true;
  1590. if (is_inside_scene())
  1591. get_scene()->set_input_as_handled();
  1592. }
  1593. void Control::accept_event() {
  1594. if (is_inside_scene() && get_window())
  1595. get_window()->_window_accept_event();
  1596. }
  1597. Ref<Theme> Control::get_theme() const {
  1598. return data.theme;
  1599. }
  1600. void Control::set_tooltip(const String& p_tooltip) {
  1601. data.tooltip=p_tooltip;
  1602. }
  1603. String Control::get_tooltip(const Point2& p_pos) const {
  1604. return data.tooltip;
  1605. }
  1606. void Control::set_default_cursor_shape(CursorShape p_shape) {
  1607. data.default_cursor=p_shape;
  1608. }
  1609. Control::CursorShape Control::get_default_cursor_shape() const {
  1610. return data.default_cursor;
  1611. }
  1612. Control::CursorShape Control::get_cursor_shape(const Point2& p_pos) const {
  1613. return data.default_cursor;
  1614. }
  1615. Matrix32 Control::get_transform() const {
  1616. Matrix32 xf;
  1617. xf.set_origin(get_pos());
  1618. return xf;
  1619. }
  1620. String Control::_get_tooltip() const {
  1621. return data.tooltip;
  1622. }
  1623. void Control::set_focus_neighbour(Margin p_margin, const NodePath &p_neighbour) {
  1624. ERR_FAIL_INDEX(p_margin,4);
  1625. data.focus_neighbour[p_margin]=p_neighbour;
  1626. }
  1627. NodePath Control::get_focus_neighbour(Margin p_margin) const {
  1628. ERR_FAIL_INDEX_V(p_margin,4,NodePath());
  1629. return data.focus_neighbour[p_margin];
  1630. }
  1631. #define MAX_NEIGHBOUR_SEARCH_COUNT 512
  1632. Control *Control::_get_focus_neighbour(Margin p_margin,int p_count) {
  1633. if (p_count >= MAX_NEIGHBOUR_SEARCH_COUNT)
  1634. return NULL;
  1635. if (!data.focus_neighbour[p_margin].is_empty()) {
  1636. Control *c=NULL;
  1637. Node * n = get_node(data.focus_neighbour[p_margin]);
  1638. if (n) {
  1639. c=n->cast_to<Control>();
  1640. if (!c) {
  1641. ERR_EXPLAIN("Next focus node is not a control: "+n->get_name());
  1642. ERR_FAIL_V(NULL);
  1643. }
  1644. } else {
  1645. return NULL;
  1646. }
  1647. bool valid=true;
  1648. if (c->is_hidden())
  1649. valid=false;
  1650. if (c->get_focus_mode()==FOCUS_NONE)
  1651. valid=false;
  1652. if (valid)
  1653. return c;
  1654. c=c->_get_focus_neighbour(p_margin,p_count+1);
  1655. return c;
  1656. }
  1657. float dist=1e7;
  1658. Control * result=NULL;
  1659. Point2 points[4];
  1660. Matrix32 xform = get_global_transform();
  1661. Rect2 rect = get_item_rect();
  1662. points[0]=xform.xform(rect.pos);
  1663. points[1]=xform.xform(rect.pos + Point2(rect.size.x, 0));
  1664. points[2]=xform.xform(rect.pos + rect.size);
  1665. points[3]=xform.xform(rect.pos + Point2(0, rect.size.y));
  1666. const Vector2 dir[4]={
  1667. Vector2(-1,0),
  1668. Vector2(0,-1),
  1669. Vector2(1,0),
  1670. Vector2(0,1)
  1671. };
  1672. Vector2 vdir=dir[p_margin];
  1673. float maxd=-1e7;
  1674. for(int i=0;i<4;i++) {
  1675. float d = vdir.dot(points[i]);
  1676. if (d>maxd)
  1677. maxd=d;
  1678. }
  1679. Node *base=this;
  1680. while (base) {
  1681. Control *c = base->cast_to<Control>();
  1682. if (c) {
  1683. if (c->data.SI)
  1684. break;
  1685. if (c==data.window)
  1686. break;
  1687. }
  1688. base=base->get_parent();
  1689. }
  1690. if (!base)
  1691. return NULL;
  1692. _window_find_focus_neighbour(vdir,base,points,maxd,dist,&result);
  1693. return result;
  1694. }
  1695. void Control::_window_find_focus_neighbour(const Vector2& p_dir, Node *p_at,const Point2* p_points,float p_min ,float &r_closest_dist,Control **r_closest) {
  1696. if (p_at->cast_to<Viewport>())
  1697. return; //bye
  1698. Control *c = p_at->cast_to<Control>();
  1699. if (c && c !=this && c->get_focus_mode()==FOCUS_ALL && c->is_visible()) {
  1700. Point2 points[4];
  1701. Matrix32 xform = c->get_global_transform();
  1702. Rect2 rect = c->get_item_rect();
  1703. points[0]=xform.xform(rect.pos);
  1704. points[1]=xform.xform(rect.pos + Point2(rect.size.x, 0));
  1705. points[2]=xform.xform(rect.pos + rect.size);
  1706. points[3]=xform.xform(rect.pos + Point2(0, rect.size.y));
  1707. float min=1e7;
  1708. for(int i=0;i<4;i++) {
  1709. float d = p_dir.dot(points[i]);
  1710. if (d < min)
  1711. min =d;
  1712. }
  1713. if (min>(p_min-CMP_EPSILON)) {
  1714. for(int i=0;i<4;i++) {
  1715. Vector2 la=p_points[i];
  1716. Vector2 lb=p_points[(i+1)%4];
  1717. for(int j=0;j<4;j++) {
  1718. Vector2 fa=points[j];
  1719. Vector2 fb=points[(j+1)%4];
  1720. Vector2 pa,pb;
  1721. float d=Geometry::get_closest_points_between_segments(la,lb,fa,fb,pa,pb);
  1722. //float d = Geometry::get_closest_distance_between_segments(Vector3(la.x,la.y,0),Vector3(lb.x,lb.y,0),Vector3(fa.x,fa.y,0),Vector3(fb.x,fb.y,0));
  1723. if (d<r_closest_dist) {
  1724. r_closest_dist=d;
  1725. *r_closest=c;
  1726. }
  1727. }
  1728. }
  1729. }
  1730. }
  1731. for(int i=0;i<p_at->get_child_count();i++) {
  1732. Node *child=p_at->get_child(i);
  1733. Control *childc = child->cast_to<Control>();
  1734. if (childc && childc->data.SI)
  1735. continue; //subwindow, ignore
  1736. _window_find_focus_neighbour(p_dir,p_at->get_child(i),p_points,p_min,r_closest_dist,r_closest);
  1737. }
  1738. }
  1739. void Control::set_h_size_flags(int p_flags) {
  1740. if (data.h_size_flags==p_flags)
  1741. return;
  1742. data.h_size_flags=p_flags;
  1743. emit_signal(SceneStringNames::get_singleton()->size_flags_changed);
  1744. }
  1745. int Control::get_h_size_flags() const{
  1746. return data.h_size_flags;
  1747. }
  1748. void Control::set_v_size_flags(int p_flags) {
  1749. if (data.v_size_flags==p_flags)
  1750. return;
  1751. data.v_size_flags=p_flags;
  1752. emit_signal(SceneStringNames::get_singleton()->size_flags_changed);
  1753. }
  1754. void Control::set_stretch_ratio(float p_ratio) {
  1755. if (data.expand==p_ratio)
  1756. return;
  1757. data.expand=p_ratio;
  1758. emit_signal(SceneStringNames::get_singleton()->size_flags_changed);
  1759. }
  1760. float Control::get_stretch_ratio() const {
  1761. return data.expand;
  1762. }
  1763. void Control::grab_click_focus() {
  1764. ERR_FAIL_COND(!is_inside_scene());
  1765. if (data.window && data.window->window->mouse_focus) {
  1766. Window *w=data.window->window;
  1767. if (w->mouse_focus==this)
  1768. return;
  1769. InputEvent ie;
  1770. ie.type=InputEvent::MOUSE_BUTTON;
  1771. InputEventMouseButton &mb=ie.mouse_button;
  1772. //send unclic
  1773. Point2 click =w->mouse_focus->get_global_transform().affine_inverse().xform(w->last_mouse_pos);
  1774. mb.x=click.x;
  1775. mb.y=click.y;
  1776. mb.button_index=w->mouse_focus_button;
  1777. mb.pressed=false;
  1778. w->mouse_focus->call_deferred("_input_event",ie);
  1779. w->mouse_focus=this;
  1780. w->focus_inv_xform=w->mouse_focus->get_global_transform().affine_inverse();
  1781. click =w->mouse_focus->get_global_transform().affine_inverse().xform(w->last_mouse_pos);
  1782. mb.x=click.x;
  1783. mb.y=click.y;
  1784. mb.button_index=w->mouse_focus_button;
  1785. mb.pressed=true;
  1786. w->mouse_focus->call_deferred("_input_event",ie);
  1787. }
  1788. }
  1789. void Control::minimum_size_changed() {
  1790. if (!is_inside_scene())
  1791. return;
  1792. if (data.pending_min_size_update)
  1793. return;
  1794. data.pending_min_size_update=true;
  1795. MessageQueue::get_singleton()->push_call(this,"_update_minimum_size");
  1796. if (!is_toplevel_control()) {
  1797. Control *pc = get_parent_control();
  1798. if (pc)
  1799. pc->minimum_size_changed();
  1800. }
  1801. }
  1802. int Control::get_v_size_flags() const{
  1803. return data.v_size_flags;
  1804. }
  1805. void Control::set_ignore_mouse(bool p_ignore) {
  1806. data.ignore_mouse=p_ignore;
  1807. }
  1808. bool Control::is_ignoring_mouse() const {
  1809. return data.ignore_mouse;
  1810. }
  1811. void Control::set_stop_mouse(bool p_stop) {
  1812. data.stop_mouse=p_stop;
  1813. }
  1814. bool Control::is_stopping_mouse() const {
  1815. return data.stop_mouse;
  1816. }
  1817. Control *Control::get_focus_owner() const {
  1818. ERR_FAIL_COND_V(!is_inside_scene(),NULL);
  1819. ERR_FAIL_COND_V(!window,NULL);
  1820. return window->key_focus;
  1821. }
  1822. void Control::_bind_methods() {
  1823. ObjectTypeDB::bind_method(_MD("_window_input_event"),&Control::_window_input_event);
  1824. ObjectTypeDB::bind_method(_MD("_gui_input"),&Control::_gui_input);
  1825. ObjectTypeDB::bind_method(_MD("_input_text"),&Control::_input_text);
  1826. // ObjectTypeDB::bind_method(_MD("_window_resize_event"),&Control::_window_resize_event);
  1827. ObjectTypeDB::bind_method(_MD("_window_remove_focus"),&Control::_window_remove_focus);
  1828. ObjectTypeDB::bind_method(_MD("_cancel_input_ID"),&Control::_window_cancel_input_ID);
  1829. ObjectTypeDB::bind_method(_MD("_cancel_tooltip"),&Control::_window_cancel_tooltip);
  1830. ObjectTypeDB::bind_method(_MD("_window_show_tooltip"),&Control::_window_show_tooltip);
  1831. ObjectTypeDB::bind_method(_MD("_size_changed"),&Control::_size_changed);
  1832. ObjectTypeDB::bind_method(_MD("_update_minimum_size"),&Control::_update_minimum_size);
  1833. ObjectTypeDB::bind_method(_MD("accept_event"),&Control::accept_event);
  1834. ObjectTypeDB::bind_method(_MD("get_minimum_size"),&Control::get_minimum_size);
  1835. ObjectTypeDB::bind_method(_MD("get_combined_minimum_size"),&Control::get_combined_minimum_size);
  1836. ObjectTypeDB::bind_method(_MD("is_window"),&Control::is_window);
  1837. ObjectTypeDB::bind_method(_MD("get_window"),&Control::get_window);
  1838. ObjectTypeDB::bind_method(_MD("set_anchor","margin","anchor_mode"),&Control::set_anchor);
  1839. ObjectTypeDB::bind_method(_MD("get_anchor","margin"),&Control::get_anchor);
  1840. ObjectTypeDB::bind_method(_MD("set_margin","margin","offset"),&Control::set_margin);
  1841. ObjectTypeDB::bind_method(_MD("set_anchor_and_margin","margin","anchor_mode","offset"),&Control::set_anchor_and_margin);
  1842. ObjectTypeDB::bind_method(_MD("set_begin","pos"),&Control::set_begin);
  1843. ObjectTypeDB::bind_method(_MD("set_end","pos"),&Control::set_end);
  1844. ObjectTypeDB::bind_method(_MD("set_pos","pos"),&Control::set_pos);
  1845. ObjectTypeDB::bind_method(_MD("set_size","size"),&Control::set_size);
  1846. ObjectTypeDB::bind_method(_MD("set_custom_minimum_size","size"),&Control::set_custom_minimum_size);
  1847. ObjectTypeDB::bind_method(_MD("set_global_pos","pos"),&Control::set_global_pos);
  1848. ObjectTypeDB::bind_method(_MD("get_margin","margin"),&Control::get_margin);
  1849. ObjectTypeDB::bind_method(_MD("get_begin"),&Control::get_begin);
  1850. ObjectTypeDB::bind_method(_MD("get_end"),&Control::get_end);
  1851. ObjectTypeDB::bind_method(_MD("get_pos"),&Control::get_pos);
  1852. ObjectTypeDB::bind_method(_MD("get_size"),&Control::get_size);
  1853. ObjectTypeDB::bind_method(_MD("get_custom_minimum_size"),&Control::get_custom_minimum_size);
  1854. ObjectTypeDB::bind_method(_MD("get_parent_area_size"),&Control::get_size);
  1855. ObjectTypeDB::bind_method(_MD("get_global_pos"),&Control::get_global_pos);
  1856. ObjectTypeDB::bind_method(_MD("get_rect"),&Control::get_rect);
  1857. ObjectTypeDB::bind_method(_MD("get_global_rect"),&Control::get_global_rect);
  1858. ObjectTypeDB::bind_method(_MD("set_area_as_parent_rect","margin"),&Control::set_area_as_parent_rect,DEFVAL(0));
  1859. ObjectTypeDB::bind_method(_MD("show_modal","exclusive"),&Control::show_modal,DEFVAL(false));
  1860. ObjectTypeDB::bind_method(_MD("set_focus_mode","mode"),&Control::set_focus_mode);
  1861. ObjectTypeDB::bind_method(_MD("has_focus"),&Control::has_focus);
  1862. ObjectTypeDB::bind_method(_MD("grab_focus"),&Control::grab_focus);
  1863. ObjectTypeDB::bind_method(_MD("release_focus"),&Control::release_focus);
  1864. ObjectTypeDB::bind_method(_MD("get_focus_owner:Control"),&Control::get_focus_owner);
  1865. ObjectTypeDB::bind_method(_MD("set_h_size_flags","flags"),&Control::set_h_size_flags);
  1866. ObjectTypeDB::bind_method(_MD("get_h_size_flags"),&Control::get_h_size_flags);
  1867. ObjectTypeDB::bind_method(_MD("set_stretch_ratio","ratio"),&Control::set_stretch_ratio);
  1868. ObjectTypeDB::bind_method(_MD("get_stretch_ratio"),&Control::get_stretch_ratio);
  1869. ObjectTypeDB::bind_method(_MD("set_v_size_flags","flags"),&Control::set_v_size_flags);
  1870. ObjectTypeDB::bind_method(_MD("get_v_size_flags"),&Control::get_v_size_flags);
  1871. ObjectTypeDB::bind_method(_MD("set_theme","theme:Theme"),&Control::set_theme);
  1872. ObjectTypeDB::bind_method(_MD("get_theme:Theme"),&Control::get_theme);
  1873. ObjectTypeDB::bind_method(_MD("add_icon_override","name","texture:Texture"),&Control::add_icon_override);
  1874. ObjectTypeDB::bind_method(_MD("add_style_override","name","stylebox:StyleBox"),&Control::add_style_override);
  1875. ObjectTypeDB::bind_method(_MD("add_font_override","name","font:Font"),&Control::add_font_override);
  1876. ObjectTypeDB::bind_method(_MD("add_color_override","name","color"),&Control::add_color_override);
  1877. ObjectTypeDB::bind_method(_MD("add_constant_override","name","constant"),&Control::add_constant_override);
  1878. ObjectTypeDB::bind_method(_MD("get_icon:Texture","name","type"),&Control::get_icon,DEFVAL(""));
  1879. ObjectTypeDB::bind_method(_MD("get_stylebox:StyleBox","name","type"),&Control::get_stylebox,DEFVAL(""));
  1880. ObjectTypeDB::bind_method(_MD("get_font:Font","name","type"),&Control::get_font,DEFVAL(""));
  1881. ObjectTypeDB::bind_method(_MD("get_color","name","type"),&Control::get_color,DEFVAL(""));
  1882. ObjectTypeDB::bind_method(_MD("get_constant","name","type"),&Control::get_constant,DEFVAL(""));
  1883. ObjectTypeDB::bind_method(_MD("get_parent_control:Control"),&Control::get_parent_control);
  1884. ObjectTypeDB::bind_method(_MD("set_tooltip","tooltip"),&Control::set_tooltip);
  1885. ObjectTypeDB::bind_method(_MD("get_tooltip","atpos"),&Control::get_tooltip,DEFVAL(Point2()));
  1886. ObjectTypeDB::bind_method(_MD("_get_tooltip"),&Control::_get_tooltip);
  1887. ObjectTypeDB::bind_method(_MD("set_default_cursor_shape","shape"),&Control::set_default_cursor_shape);
  1888. ObjectTypeDB::bind_method(_MD("get_default_cursor_shape"),&Control::get_default_cursor_shape);
  1889. ObjectTypeDB::bind_method(_MD("get_cursor_shape","pos"),&Control::get_cursor_shape,DEFVAL(Point2()));
  1890. ObjectTypeDB::bind_method(_MD("set_focus_neighbour","margin","neighbour"),&Control::set_focus_neighbour);
  1891. ObjectTypeDB::bind_method(_MD("get_focus_neighbour","margin"),&Control::get_focus_neighbour);
  1892. ObjectTypeDB::bind_method(_MD("set_ignore_mouse","ignore"),&Control::set_ignore_mouse);
  1893. ObjectTypeDB::bind_method(_MD("is_ignoring_mouse"),&Control::is_ignoring_mouse);
  1894. ObjectTypeDB::bind_method(_MD("force_drag","data","preview"),&Control::force_drag);
  1895. ObjectTypeDB::bind_method(_MD("set_stop_mouse","stop"),&Control::set_stop_mouse);
  1896. ObjectTypeDB::bind_method(_MD("is_stopping_mouse"),&Control::is_stopping_mouse);
  1897. ObjectTypeDB::bind_method(_MD("grab_click_focus"),&Control::grab_click_focus);
  1898. ObjectTypeDB::bind_method(_MD("set_drag_preview","control:Control"),&Control::set_drag_preview);
  1899. BIND_VMETHOD(MethodInfo("_input_event",PropertyInfo(Variant::INPUT_EVENT,"event")));
  1900. BIND_VMETHOD(MethodInfo(Variant::VECTOR2,"get_minimum_size"));
  1901. BIND_VMETHOD(MethodInfo(Variant::OBJECT,"get_drag_data",PropertyInfo(Variant::VECTOR2,"pos")));
  1902. BIND_VMETHOD(MethodInfo(Variant::BOOL,"can_drop_data",PropertyInfo(Variant::VECTOR2,"pos"),PropertyInfo(Variant::NIL,"data")));
  1903. BIND_VMETHOD(MethodInfo("drop_data",PropertyInfo(Variant::VECTOR2,"pos"),PropertyInfo(Variant::NIL,"data")));
  1904. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/left", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("set_anchor"),_SCS("get_anchor"), MARGIN_LEFT );
  1905. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/top", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("set_anchor"),_SCS("get_anchor"), MARGIN_TOP );
  1906. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/right", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("set_anchor"),_SCS("get_anchor"), MARGIN_RIGHT );
  1907. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/bottom", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("set_anchor"),_SCS("get_anchor"), MARGIN_BOTTOM );
  1908. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/left", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_LEFT );
  1909. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/top", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_TOP );
  1910. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/right", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_RIGHT );
  1911. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/bottom", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_BOTTOM );
  1912. ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect/pos", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_EDITOR), _SCS("set_pos"),_SCS("get_pos") );
  1913. ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect/size", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_EDITOR), _SCS("set_size"),_SCS("get_size") );
  1914. ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect/min_size"), _SCS("set_custom_minimum_size"),_SCS("get_custom_minimum_size") );
  1915. ADD_PROPERTYNZ( PropertyInfo(Variant::STRING,"hint/tooltip", PROPERTY_HINT_MULTILINE_TEXT), _SCS("set_tooltip"),_SCS("_get_tooltip") );
  1916. ADD_PROPERTYI( PropertyInfo(Variant::NODE_PATH,"focus_neighbour/left" ), _SCS("set_focus_neighbour"),_SCS("get_focus_neighbour"),MARGIN_LEFT );
  1917. ADD_PROPERTYI( PropertyInfo(Variant::NODE_PATH,"focus_neighbour/top" ), _SCS("set_focus_neighbour"),_SCS("get_focus_neighbour"),MARGIN_TOP );
  1918. ADD_PROPERTYI( PropertyInfo(Variant::NODE_PATH,"focus_neighbour/right" ), _SCS("set_focus_neighbour"),_SCS("get_focus_neighbour"),MARGIN_RIGHT );
  1919. ADD_PROPERTYI( PropertyInfo(Variant::NODE_PATH,"focus_neighbour/bottom" ), _SCS("set_focus_neighbour"),_SCS("get_focus_neighbour"),MARGIN_BOTTOM );
  1920. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"focus/ignore_mouse"), _SCS("set_ignore_mouse"),_SCS("is_ignoring_mouse") );
  1921. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"focus/stop_mouse"), _SCS("set_stop_mouse"),_SCS("is_stopping_mouse") );
  1922. ADD_PROPERTYNZ( PropertyInfo(Variant::INT,"size_flags/horizontal", PROPERTY_HINT_FLAGS, "Expand,Fill"), _SCS("set_h_size_flags"),_SCS("get_h_size_flags") );
  1923. ADD_PROPERTYNZ( PropertyInfo(Variant::INT,"size_flags/vertical", PROPERTY_HINT_FLAGS, "Expand,Fill"), _SCS("set_v_size_flags"),_SCS("get_v_size_flags") );
  1924. ADD_PROPERTY( PropertyInfo(Variant::INT,"size_flags/stretch_ratio", PROPERTY_HINT_RANGE, "1,128,0.01"), _SCS("set_stretch_ratio"),_SCS("get_stretch_ratio") );
  1925. ADD_PROPERTYNZ( PropertyInfo(Variant::OBJECT,"theme/theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), _SCS("set_theme"),_SCS("get_theme") );
  1926. BIND_CONSTANT( ANCHOR_BEGIN );
  1927. BIND_CONSTANT( ANCHOR_END );
  1928. BIND_CONSTANT( ANCHOR_RATIO );
  1929. BIND_CONSTANT( ANCHOR_CENTER );
  1930. BIND_CONSTANT( FOCUS_NONE );
  1931. BIND_CONSTANT( FOCUS_CLICK );
  1932. BIND_CONSTANT( FOCUS_ALL );
  1933. BIND_CONSTANT( NOTIFICATION_RESIZED );
  1934. BIND_CONSTANT( NOTIFICATION_MOUSE_ENTER );
  1935. BIND_CONSTANT( NOTIFICATION_MOUSE_EXIT );
  1936. BIND_CONSTANT( NOTIFICATION_FOCUS_ENTER );
  1937. BIND_CONSTANT( NOTIFICATION_FOCUS_EXIT );
  1938. BIND_CONSTANT( NOTIFICATION_THEME_CHANGED );
  1939. BIND_CONSTANT( NOTIFICATION_MODAL_CLOSE );
  1940. BIND_CONSTANT( CURSOR_ARROW );
  1941. BIND_CONSTANT( CURSOR_IBEAM );
  1942. BIND_CONSTANT( CURSOR_POINTING_HAND );
  1943. BIND_CONSTANT( CURSOR_CROSS );
  1944. BIND_CONSTANT( CURSOR_WAIT );
  1945. BIND_CONSTANT( CURSOR_BUSY );
  1946. BIND_CONSTANT( CURSOR_DRAG );
  1947. BIND_CONSTANT( CURSOR_CAN_DROP );
  1948. BIND_CONSTANT( CURSOR_FORBIDDEN );
  1949. BIND_CONSTANT( CURSOR_VSIZE );
  1950. BIND_CONSTANT( CURSOR_HSIZE );
  1951. BIND_CONSTANT( CURSOR_BDIAGSIZE );
  1952. BIND_CONSTANT( CURSOR_FDIAGSIZE );
  1953. BIND_CONSTANT( CURSOR_MOVE );
  1954. BIND_CONSTANT( CURSOR_VSPLIT );
  1955. BIND_CONSTANT( CURSOR_HSPLIT );
  1956. BIND_CONSTANT( CURSOR_HELP );
  1957. BIND_CONSTANT( SIZE_EXPAND );
  1958. BIND_CONSTANT( SIZE_FILL );
  1959. BIND_CONSTANT( SIZE_EXPAND_FILL );
  1960. ADD_SIGNAL( MethodInfo("resized") );
  1961. ADD_SIGNAL( MethodInfo("input_event") );
  1962. ADD_SIGNAL( MethodInfo("mouse_enter") );
  1963. ADD_SIGNAL( MethodInfo("mouse_exit") );
  1964. ADD_SIGNAL( MethodInfo("focus_enter") );
  1965. ADD_SIGNAL( MethodInfo("focus_exit") );
  1966. ADD_SIGNAL( MethodInfo("size_flags_changed") );
  1967. ADD_SIGNAL( MethodInfo("minimum_size_changed") );
  1968. }
  1969. Control::Control() {
  1970. data.parent=NULL;
  1971. data.window=NULL;
  1972. data.viewport=NULL;
  1973. data.ignore_mouse=false;
  1974. data.stop_mouse=true;
  1975. window=NULL;
  1976. data.SI=NULL;
  1977. data.MI=NULL;
  1978. data.modal=false;
  1979. data.theme_owner=NULL;
  1980. data.modal_exclusive=false;
  1981. data.default_cursor = CURSOR_ARROW;
  1982. data.h_size_flags=SIZE_FILL;
  1983. data.v_size_flags=SIZE_FILL;
  1984. data.expand=1;
  1985. data.pending_min_size_update=false;
  1986. for (int i=0;i<4;i++) {
  1987. data.anchor[i]=ANCHOR_BEGIN;
  1988. data.margin[i]=0;
  1989. }
  1990. data.focus_mode=FOCUS_NONE;
  1991. data.modal_prev_focus_owner=0;
  1992. }
  1993. Control::~Control()
  1994. {
  1995. }