control.cpp 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919
  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_tree())
  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_TREE: {
  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_tree()->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_TREE: {
  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 SceneTree::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_tree());
  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_tree() || !data.window);
  537. ERR_FAIL_COND(p_control->is_inside_tree());
  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_tree()->call_group(SceneTree::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  699. get_tree()->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_tree()->call_group(SceneTree::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  748. get_tree()->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_tree()->call_group(SceneTree::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  781. get_tree()->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_tree()->call_group(SceneTree::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_tree()->call_group(SceneTree::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  862. get_tree()->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_tree()->call_group(SceneTree::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_tree()->call_group(SceneTree::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_tree() && 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. if (p_type==StringName()) {
  941. const Ref<Texture>* tex = data.icon_override.getptr(p_name);
  942. if (tex)
  943. return *tex;
  944. }
  945. StringName type = p_type?p_type:get_type_name();
  946. // try with custom themes
  947. Control *theme_owner = data.theme_owner;
  948. while(theme_owner) {
  949. if (theme_owner->data.theme->has_icon(p_name, type ) )
  950. return data.theme_owner->data.theme->get_icon(p_name, type );
  951. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  952. if (parent)
  953. theme_owner=parent->data.theme_owner;
  954. else
  955. theme_owner=NULL;
  956. }
  957. return Theme::get_default()->get_icon( p_name, type );
  958. }
  959. Ref<StyleBox> Control::get_stylebox(const StringName& p_name,const StringName& p_type) const {
  960. if (p_type==StringName()) {
  961. const Ref<StyleBox>* style = data.style_override.getptr(p_name);
  962. if (style)
  963. return *style;
  964. }
  965. StringName type = p_type?p_type:get_type_name();
  966. // try with custom themes
  967. Control *theme_owner = data.theme_owner;
  968. while(theme_owner) {
  969. if (theme_owner->data.theme->has_stylebox(p_name, type ) )
  970. return data.theme_owner->data.theme->get_stylebox(p_name, type );
  971. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  972. if (parent)
  973. theme_owner=parent->data.theme_owner;
  974. else
  975. theme_owner=NULL;
  976. }
  977. return Theme::get_default()->get_stylebox( p_name, type );
  978. }
  979. Ref<Font> Control::get_font(const StringName& p_name,const StringName& p_type) const {
  980. if (p_type==StringName()) {
  981. const Ref<Font>* font = data.font_override.getptr(p_name);
  982. if (font)
  983. return *font;
  984. }
  985. StringName type = p_type?p_type:get_type_name();
  986. // try with custom themes
  987. Control *theme_owner = data.theme_owner;
  988. while(theme_owner) {
  989. if (theme_owner->data.theme->has_font(p_name, type ) )
  990. return data.theme_owner->data.theme->get_font(p_name, type );
  991. if (theme_owner->data.theme->get_default_theme_font().is_valid())
  992. return theme_owner->data.theme->get_default_theme_font();
  993. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  994. if (parent)
  995. theme_owner=parent->data.theme_owner;
  996. else
  997. theme_owner=NULL;
  998. }
  999. return Theme::get_default()->get_font( p_name, type );
  1000. }
  1001. Color Control::get_color(const StringName& p_name,const StringName& p_type) const {
  1002. if (p_type==StringName()) {
  1003. const Color* color = data.color_override.getptr(p_name);
  1004. if (color)
  1005. return *color;
  1006. }
  1007. StringName type = p_type?p_type:get_type_name();
  1008. // try with custom themes
  1009. Control *theme_owner = data.theme_owner;
  1010. while(theme_owner) {
  1011. if (theme_owner->data.theme->has_color(p_name, type ) )
  1012. return data.theme_owner->data.theme->get_color(p_name, type );
  1013. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  1014. if (parent)
  1015. theme_owner=parent->data.theme_owner;
  1016. else
  1017. theme_owner=NULL;
  1018. }
  1019. return Theme::get_default()->get_color( p_name, type );
  1020. }
  1021. int Control::get_constant(const StringName& p_name,const StringName& p_type) const {
  1022. if (p_type==StringName()) {
  1023. const int* constant = data.constant_override.getptr(p_name);
  1024. if (constant)
  1025. return *constant;
  1026. }
  1027. StringName type = p_type?p_type:get_type_name();
  1028. // try with custom themes
  1029. Control *theme_owner = data.theme_owner;
  1030. while(theme_owner) {
  1031. if (theme_owner->data.theme->has_constant(p_name, type ) )
  1032. return data.theme_owner->data.theme->get_constant(p_name, type );
  1033. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  1034. if (parent)
  1035. theme_owner=parent->data.theme_owner;
  1036. else
  1037. theme_owner=NULL;
  1038. }
  1039. return Theme::get_default()->get_constant( p_name, type );
  1040. }
  1041. bool Control::has_icon(const StringName& p_name,const StringName& p_type) const {
  1042. if (p_type==StringName()) {
  1043. const Ref<Texture>* tex = data.icon_override.getptr(p_name);
  1044. if (tex)
  1045. return true;
  1046. }
  1047. StringName type = p_type?p_type:get_type_name();
  1048. // try with custom themes
  1049. Control *theme_owner = data.theme_owner;
  1050. while(theme_owner) {
  1051. if (theme_owner->data.theme->has_icon(p_name, type ) )
  1052. return true;
  1053. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  1054. if (parent)
  1055. theme_owner=parent->data.theme_owner;
  1056. else
  1057. theme_owner=NULL;
  1058. }
  1059. return Theme::get_default()->has_icon( p_name, type );
  1060. }
  1061. bool Control::has_stylebox(const StringName& p_name,const StringName& p_type) const {
  1062. if (p_type==StringName()) {
  1063. const Ref<StyleBox>* style = data.style_override.getptr(p_name);
  1064. if (style)
  1065. return true;
  1066. }
  1067. StringName type = p_type?p_type:get_type_name();
  1068. // try with custom themes
  1069. Control *theme_owner = data.theme_owner;
  1070. while(theme_owner) {
  1071. if (theme_owner->data.theme->has_stylebox(p_name, type ) )
  1072. return true;
  1073. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  1074. if (parent)
  1075. theme_owner=parent->data.theme_owner;
  1076. else
  1077. theme_owner=NULL;
  1078. }
  1079. return Theme::get_default()->has_stylebox( p_name, type );
  1080. }
  1081. bool Control::has_font(const StringName& p_name,const StringName& p_type) const {
  1082. if (p_type==StringName()) {
  1083. const Ref<Font>* font = data.font_override.getptr(p_name);
  1084. if (font)
  1085. return true;
  1086. }
  1087. StringName type = p_type?p_type:get_type_name();
  1088. // try with custom themes
  1089. Control *theme_owner = data.theme_owner;
  1090. while(theme_owner) {
  1091. if (theme_owner->data.theme->has_font(p_name, type ) )
  1092. return true;
  1093. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  1094. if (parent)
  1095. theme_owner=parent->data.theme_owner;
  1096. else
  1097. theme_owner=NULL;
  1098. }
  1099. return Theme::get_default()->has_font( p_name, type );
  1100. }
  1101. bool Control::has_color(const StringName& p_name,const StringName& p_type) const {
  1102. if (p_type==StringName()) {
  1103. const Color* color = data.color_override.getptr(p_name);
  1104. if (color)
  1105. return true;
  1106. }
  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_color(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_color( p_name, type );
  1120. }
  1121. bool Control::has_constant(const StringName& p_name,const StringName& p_type) const {
  1122. if (p_type==StringName()) {
  1123. const int* constant = data.constant_override.getptr(p_name);
  1124. if (constant)
  1125. return true;
  1126. }
  1127. StringName type = p_type?p_type:get_type_name();
  1128. // try with custom themes
  1129. Control *theme_owner = data.theme_owner;
  1130. while(theme_owner) {
  1131. if (theme_owner->data.theme->has_constant(p_name, type ) )
  1132. return true;
  1133. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  1134. if (parent)
  1135. theme_owner=parent->data.theme_owner;
  1136. else
  1137. theme_owner=NULL;
  1138. }
  1139. return Theme::get_default()->has_constant( p_name, type );
  1140. }
  1141. Size2 Control::get_parent_area_size() const {
  1142. ERR_FAIL_COND_V(!is_inside_tree(),Size2());
  1143. Size2 parent_size;
  1144. if (data.parent_canvas_item) {
  1145. parent_size=data.parent_canvas_item->get_item_rect().size;
  1146. } else if (data.viewport) {
  1147. parent_size=data.viewport->get_visible_rect().size;
  1148. }
  1149. return parent_size;
  1150. }
  1151. void Control::_size_changed() {
  1152. if (!is_inside_tree())
  1153. return;
  1154. Size2 parent_size = get_parent_area_size();
  1155. float margin_pos[4];
  1156. for(int i=0;i<4;i++) {
  1157. float area = parent_size[i&1];
  1158. switch(data.anchor[i]) {
  1159. case ANCHOR_BEGIN: {
  1160. margin_pos[i]=data.margin[i];
  1161. } break;
  1162. case ANCHOR_END: {
  1163. margin_pos[i]=area-data.margin[i];
  1164. } break;
  1165. case ANCHOR_RATIO: {
  1166. margin_pos[i]=area*data.margin[i];
  1167. } break;
  1168. case ANCHOR_CENTER: {
  1169. margin_pos[i]=(area/2)-data.margin[i];
  1170. } break;
  1171. }
  1172. }
  1173. Point2 new_pos_cache=Point2(margin_pos[0],margin_pos[1]).floor();
  1174. Size2 new_size_cache=Point2(margin_pos[2],margin_pos[3]).floor()-new_pos_cache;
  1175. Size2 minimum_size=get_combined_minimum_size();
  1176. new_size_cache.x = MAX( minimum_size.x, new_size_cache.x );
  1177. new_size_cache.y = MAX( minimum_size.y, new_size_cache.y );
  1178. if (new_pos_cache == data.pos_cache && new_size_cache == data.size_cache)
  1179. return; // did not change, don't emit signal
  1180. data.pos_cache=new_pos_cache;
  1181. data.size_cache=new_size_cache;
  1182. notification(NOTIFICATION_RESIZED);
  1183. item_rect_changed();
  1184. _change_notify_margins();
  1185. _notify_transform();
  1186. }
  1187. float Control::_get_parent_range(int p_idx) const {
  1188. if (!is_inside_tree()) {
  1189. return 1.0;
  1190. } if (data.parent_canvas_item) {
  1191. return data.parent_canvas_item->get_item_rect().size[p_idx&1];
  1192. } else if (data.viewport) {
  1193. return data.viewport->get_visible_rect().size[p_idx&1];
  1194. }
  1195. return 1.0;
  1196. }
  1197. float Control::_get_range(int p_idx) const {
  1198. p_idx&=1;
  1199. float parent_range = _get_parent_range( p_idx );
  1200. float from = _a2s( data.margin[p_idx], data.anchor[p_idx], parent_range );
  1201. float to = _a2s( data.margin[p_idx+2], data.anchor[p_idx+2], parent_range );
  1202. return to-from;
  1203. }
  1204. float Control::_s2a(float p_val, AnchorType p_anchor,float p_range) const {
  1205. switch(p_anchor) {
  1206. case ANCHOR_BEGIN: {
  1207. return p_val;
  1208. } break;
  1209. case ANCHOR_END: {
  1210. return p_range-p_val;
  1211. } break;
  1212. case ANCHOR_RATIO: {
  1213. return p_val/p_range;
  1214. } break;
  1215. case ANCHOR_CENTER: {
  1216. return (p_range/2)-p_val;
  1217. } break;
  1218. }
  1219. return 0;
  1220. }
  1221. float Control::_a2s(float p_val, AnchorType p_anchor,float p_range) const {
  1222. switch(p_anchor) {
  1223. case ANCHOR_BEGIN: {
  1224. return Math::floor(p_val);
  1225. } break;
  1226. case ANCHOR_END: {
  1227. return Math::floor(p_range-p_val);
  1228. } break;
  1229. case ANCHOR_RATIO: {
  1230. return Math::floor(p_range*p_val);
  1231. } break;
  1232. case ANCHOR_CENTER: {
  1233. return Math::floor((p_range/2)-p_val);
  1234. } break;
  1235. }
  1236. return 0;
  1237. }
  1238. void Control::set_anchor(Margin p_margin,AnchorType p_anchor) {
  1239. if (!is_inside_tree()) {
  1240. data.anchor[p_margin]=p_anchor;
  1241. } else {
  1242. float pr = _get_parent_range(p_margin);
  1243. float s = _a2s( data.margin[p_margin], data.anchor[p_margin], pr );
  1244. data.anchor[p_margin]=p_anchor;
  1245. data.margin[p_margin] = _s2a( s, p_anchor, pr );
  1246. }
  1247. _change_notify();
  1248. }
  1249. void Control::set_anchor_and_margin(Margin p_margin,AnchorType p_anchor, float p_pos) {
  1250. set_anchor(p_margin,p_anchor);
  1251. set_margin(p_margin,p_pos);
  1252. }
  1253. Control::AnchorType Control::get_anchor(Margin p_margin) const {
  1254. return data.anchor[p_margin];
  1255. }
  1256. void Control::_change_notify_margins() {
  1257. // this avoids sending the whole object data again on a change
  1258. _change_notify("margin/left");
  1259. _change_notify("margin/top");
  1260. _change_notify("margin/right");
  1261. _change_notify("margin/bottom");
  1262. _change_notify("rect/pos");
  1263. _change_notify("rect/size");
  1264. }
  1265. void Control::set_margin(Margin p_margin,float p_value) {
  1266. data.margin[p_margin]=p_value;
  1267. _size_changed();
  1268. }
  1269. void Control::set_begin(const Size2& p_point) {
  1270. data.margin[0]=p_point.x;
  1271. data.margin[1]=p_point.y;
  1272. _size_changed();
  1273. }
  1274. void Control::set_end(const Size2& p_point) {
  1275. data.margin[2]=p_point.x;
  1276. data.margin[3]=p_point.y;
  1277. _size_changed();
  1278. }
  1279. float Control::get_margin(Margin p_margin) const {
  1280. return data.margin[p_margin];
  1281. }
  1282. Size2 Control::get_begin() const {
  1283. return Size2( data.margin[0], data.margin[1] );
  1284. }
  1285. Size2 Control::get_end() const {
  1286. return Size2( data.margin[2], data.margin[3] );
  1287. }
  1288. Point2 Control::get_global_pos() const {
  1289. return get_global_transform().get_origin();
  1290. }
  1291. void Control::set_global_pos(const Point2& p_point) {
  1292. Matrix32 inv;
  1293. if (data.parent_canvas_item) {
  1294. inv = data.parent_canvas_item->get_global_transform().affine_inverse();
  1295. }
  1296. set_pos(inv.xform(p_point));
  1297. }
  1298. void Control::set_pos(const Size2& p_point) {
  1299. float pw = _get_parent_range(0);
  1300. float ph = _get_parent_range(1);
  1301. float x = _a2s( data.margin[0], data.anchor[0], pw );
  1302. float y = _a2s( data.margin[1], data.anchor[1], ph );
  1303. float x2 = _a2s( data.margin[2], data.anchor[2], pw );
  1304. float y2 = _a2s( data.margin[3], data.anchor[3], ph );
  1305. Size2 ret = Size2(x2-x,y2-y);
  1306. Size2 min = get_combined_minimum_size();
  1307. Size2 size = Size2(MAX( min.width, ret.width),MAX( min.height, ret.height));
  1308. float w=size.x;
  1309. float h=size.y;
  1310. x=p_point.x;
  1311. y=p_point.y;
  1312. data.margin[0] = _s2a( x, data.anchor[0], pw );
  1313. data.margin[1] = _s2a( y, data.anchor[1], ph );
  1314. data.margin[2] = _s2a( x+w, data.anchor[2], pw );
  1315. data.margin[3] = _s2a( y+h, data.anchor[3], ph );
  1316. _size_changed();
  1317. }
  1318. void Control::set_size(const Size2& p_size) {
  1319. Size2 new_size=p_size;
  1320. Size2 min=get_combined_minimum_size();
  1321. if (new_size.x<min.x)
  1322. new_size.x=min.x;
  1323. if (new_size.y<min.y)
  1324. new_size.y=min.y;
  1325. float pw = _get_parent_range(0);
  1326. float ph = _get_parent_range(1);
  1327. float x = _a2s( data.margin[0], data.anchor[0], pw );
  1328. float y = _a2s( data.margin[1], data.anchor[1], ph );
  1329. float w=new_size.width;
  1330. float h=new_size.height;
  1331. data.margin[2] = _s2a( x+w, data.anchor[2], pw );
  1332. data.margin[3] = _s2a( y+h, data.anchor[3], ph );
  1333. _size_changed();
  1334. }
  1335. Size2 Control::get_pos() const {
  1336. return data.pos_cache;
  1337. }
  1338. Size2 Control::get_size() const {
  1339. return data.size_cache;
  1340. }
  1341. Rect2 Control::get_global_rect() const {
  1342. return Rect2( get_global_pos(), get_size() );
  1343. }
  1344. Rect2 Control::get_window_rect() const {
  1345. Rect2 gr = get_global_rect();
  1346. if (data.viewport)
  1347. gr.pos+=data.viewport->get_visible_rect().pos;
  1348. return gr;
  1349. }
  1350. Rect2 Control::get_rect() const {
  1351. return Rect2(get_pos(),get_size());
  1352. }
  1353. Rect2 Control::get_item_rect() const {
  1354. return Rect2(Point2(),get_size());
  1355. }
  1356. void Control::set_area_as_parent_rect(int p_margin) {
  1357. data.anchor[MARGIN_LEFT]=ANCHOR_BEGIN;
  1358. data.anchor[MARGIN_TOP]=ANCHOR_BEGIN;
  1359. data.anchor[MARGIN_RIGHT]=ANCHOR_END;
  1360. data.anchor[MARGIN_BOTTOM]=ANCHOR_END;
  1361. for(int i=0;i<4;i++)
  1362. data.margin[i]=p_margin;
  1363. _size_changed();
  1364. }
  1365. void Control::add_icon_override(const StringName& p_name, const Ref<Texture>& p_icon) {
  1366. ERR_FAIL_COND(p_icon.is_null());
  1367. data.icon_override[p_name]=p_icon;
  1368. notification(NOTIFICATION_THEME_CHANGED);
  1369. update();
  1370. }
  1371. void Control::add_style_override(const StringName& p_name, const Ref<StyleBox>& p_style) {
  1372. ERR_FAIL_COND(p_style.is_null());
  1373. data.style_override[p_name]=p_style;
  1374. notification(NOTIFICATION_THEME_CHANGED);
  1375. update();
  1376. }
  1377. void Control::add_font_override(const StringName& p_name, const Ref<Font>& p_font) {
  1378. ERR_FAIL_COND(p_font.is_null());
  1379. data.font_override[p_name]=p_font;
  1380. notification(NOTIFICATION_THEME_CHANGED);
  1381. update();
  1382. }
  1383. void Control::add_color_override(const StringName& p_name, const Color& p_color) {
  1384. data.color_override[p_name]=p_color;
  1385. notification(NOTIFICATION_THEME_CHANGED);
  1386. update();
  1387. }
  1388. void Control::add_constant_override(const StringName& p_name, int p_constant) {
  1389. data.constant_override[p_name]=p_constant;
  1390. notification(NOTIFICATION_THEME_CHANGED);
  1391. update();
  1392. }
  1393. void Control::set_focus_mode(FocusMode p_focus_mode) {
  1394. if (is_inside_tree() && p_focus_mode == FOCUS_NONE && data.focus_mode!=FOCUS_NONE && has_focus())
  1395. release_focus();
  1396. data.focus_mode=p_focus_mode;
  1397. }
  1398. static Control *_next_control(Control *p_from) {
  1399. if (p_from->is_set_as_toplevel())
  1400. return NULL; // can't go above
  1401. Control *parent = p_from->get_parent()?p_from->get_parent()->cast_to<Control>():NULL;
  1402. if (!parent) {
  1403. return NULL;
  1404. }
  1405. int next = p_from->get_position_in_parent();
  1406. ERR_FAIL_INDEX_V(next,parent->get_child_count(),NULL);
  1407. for(int i=(next+1);i<parent->get_child_count();i++) {
  1408. Control *c = parent->get_child(i)->cast_to<Control>();
  1409. if (!c || !c->is_visible() || c->is_set_as_toplevel())
  1410. continue;
  1411. return c;
  1412. }
  1413. //no next in parent, try the same in parent
  1414. return _next_control(parent);
  1415. }
  1416. Control *Control::find_next_valid_focus() const {
  1417. Control *from = const_cast<Control*>(this);
  1418. while(true) {
  1419. // find next child
  1420. Control *next_child=NULL;
  1421. for(int i=0;i<from->get_child_count();i++) {
  1422. Control *c = from->get_child(i)->cast_to<Control>();
  1423. if (!c || !c->is_visible() || c->is_set_as_toplevel()) {
  1424. continue;
  1425. }
  1426. next_child=c;
  1427. break;
  1428. }
  1429. if (next_child) {
  1430. from = next_child;
  1431. } else {
  1432. next_child=_next_control(from);
  1433. if (!next_child) { //nothing else.. go up and find either window or subwindow
  1434. next_child=const_cast<Control*>(this);
  1435. while(next_child && !next_child->is_set_as_toplevel()) {
  1436. if (next_child->get_parent()) {
  1437. next_child=next_child->get_parent()->cast_to<Control>();
  1438. } else
  1439. next_child=NULL;
  1440. }
  1441. if (!next_child) {
  1442. next_child=get_window();
  1443. }
  1444. }
  1445. }
  1446. if (next_child==this) // no next control->
  1447. return (get_focus_mode()==FOCUS_ALL)?next_child:NULL;
  1448. if (next_child->get_focus_mode()==FOCUS_ALL)
  1449. return next_child;
  1450. from = next_child;
  1451. }
  1452. return NULL;
  1453. }
  1454. static Control *_prev_control(Control *p_from) {
  1455. Control *child=NULL;
  1456. for(int i=p_from->get_child_count()-1;i>=0;i--) {
  1457. Control *c = p_from->get_child(i)->cast_to<Control>();
  1458. if (!c || !c->is_visible() || c->is_set_as_toplevel())
  1459. continue;
  1460. child=c;
  1461. break;
  1462. }
  1463. if (!child)
  1464. return p_from;
  1465. //no prev in parent, try the same in parent
  1466. return _prev_control(child);
  1467. }
  1468. Control *Control::find_prev_valid_focus() const {
  1469. Control *from = const_cast<Control*>(this);
  1470. while(true) {
  1471. // find prev child
  1472. Control *prev_child = NULL;
  1473. if ( from->is_set_as_toplevel() || !from->get_parent() || !from->get_parent()->cast_to<Control>()) {
  1474. //find last of the childs
  1475. prev_child=_prev_control(from);
  1476. } else {
  1477. for(int i=(from->get_position_in_parent()-1);i>=0;i--) {
  1478. Control *c = from->get_parent()->get_child(i)->cast_to<Control>();
  1479. if (!c || !c->is_visible() || c->is_set_as_toplevel()) {
  1480. continue;
  1481. }
  1482. prev_child=c;
  1483. break;
  1484. }
  1485. if (!prev_child) {
  1486. prev_child = from->get_parent()->cast_to<Control>();
  1487. } else {
  1488. prev_child = _prev_control(prev_child);
  1489. }
  1490. }
  1491. if (prev_child==this) // no prev control->
  1492. return (get_focus_mode()==FOCUS_ALL)?prev_child:NULL;
  1493. if (prev_child->get_focus_mode()==FOCUS_ALL)
  1494. return prev_child;
  1495. from = prev_child;
  1496. }
  1497. return NULL;
  1498. return NULL;
  1499. }
  1500. Control::FocusMode Control::get_focus_mode() const {
  1501. return data.focus_mode;
  1502. }
  1503. bool Control::has_focus() const {
  1504. return (data.window && data.window->window->key_focus==this);
  1505. }
  1506. void Control::grab_focus() {
  1507. ERR_FAIL_COND(!is_inside_tree());
  1508. ERR_FAIL_COND(!data.window);
  1509. if (data.focus_mode==FOCUS_NONE)
  1510. return;
  1511. //no need for change
  1512. if (data.window->window->key_focus && data.window->window->key_focus==this)
  1513. return;
  1514. get_tree()->call_group(SceneTree::GROUP_CALL_REALTIME,"windows","_window_remove_focus");
  1515. data.window->window->key_focus=this;
  1516. notification(NOTIFICATION_FOCUS_ENTER);
  1517. #ifdef DEBUG_ENABLED
  1518. if (GLOBAL_DEF("debug/print_clicked_control", false)) {
  1519. print_line(String(get_path())+" - focus");
  1520. };
  1521. #endif
  1522. update();
  1523. }
  1524. void Control::release_focus() {
  1525. ERR_FAIL_COND(!is_inside_tree());
  1526. ERR_FAIL_COND(!data.window);
  1527. if (!has_focus())
  1528. return;
  1529. get_tree()->call_group(SceneTree::GROUP_CALL_REALTIME,"windows","_window_remove_focus");
  1530. //data.window->window->key_focus=this;
  1531. //notification(NOTIFICATION_FOCUS_ENTER);
  1532. update();
  1533. }
  1534. bool Control::is_toplevel_control() const {
  1535. return is_inside_tree() && (!data.parent_canvas_item && !window && is_set_as_toplevel());
  1536. }
  1537. void Control::show_modal(bool p_exclusive) {
  1538. ERR_FAIL_COND(!is_inside_tree());
  1539. ERR_FAIL_COND(!data.SI && data.window!=this);
  1540. ERR_FAIL_COND(!data.window);
  1541. if (is_visible())
  1542. hide();
  1543. ERR_FAIL_COND( data.MI );
  1544. show();
  1545. raise();
  1546. data.window->window->modal_stack.push_back(this);
  1547. data.MI = data.window->window->modal_stack.back();
  1548. data.modal_exclusive=p_exclusive;
  1549. if (data.window->window->key_focus)
  1550. data.modal_prev_focus_owner = data.window->window->key_focus->get_instance_ID();
  1551. else
  1552. data.modal_prev_focus_owner=0;
  1553. }
  1554. void Control::_window_sort_subwindows() {
  1555. if (!window->subwindow_order_dirty)
  1556. return;
  1557. window->modal_stack.sort_custom<CComparator>();
  1558. window->subwindows.sort_custom<CComparator>();
  1559. window->subwindow_order_dirty=false;
  1560. }
  1561. void Control::_window_sort_modal_stack() {
  1562. window->modal_stack.sort_custom<CComparator>();
  1563. }
  1564. void Control::_modal_stack_remove() {
  1565. List<Control*>::Element *next=NULL; //transfer the focus stack to the next
  1566. if (data.window && data.MI) {
  1567. next = data.MI->next();
  1568. data.window->window->modal_stack.erase(data.MI);
  1569. data.MI=NULL;
  1570. }
  1571. if (data.modal_prev_focus_owner) {
  1572. if (!next) { //top of stack
  1573. Object *pfo = ObjectDB::get_instance(data.modal_prev_focus_owner);
  1574. Control *pfoc = pfo->cast_to<Control>();
  1575. if (!pfoc)
  1576. return;
  1577. if (!pfoc->is_inside_tree() || !pfoc->is_visible())
  1578. return;
  1579. pfoc->grab_focus();
  1580. } else {
  1581. next->get()->data.modal_prev_focus_owner=data.modal_prev_focus_owner;
  1582. }
  1583. data.modal_prev_focus_owner=0;
  1584. }
  1585. }
  1586. void Control::_propagate_theme_changed(Control *p_owner) {
  1587. for(int i=0;i<get_child_count();i++) {
  1588. Control *child = get_child(i)->cast_to<Control>();
  1589. if (child && child->data.theme.is_null()) //has no theme, propagate
  1590. child->_propagate_theme_changed(p_owner);
  1591. }
  1592. data.theme_owner=p_owner;
  1593. _notification(NOTIFICATION_THEME_CHANGED);
  1594. update();
  1595. }
  1596. void Control::set_theme(const Ref<Theme>& p_theme) {
  1597. data.theme=p_theme;
  1598. if (!p_theme.is_null()) {
  1599. _propagate_theme_changed(this);
  1600. } else {
  1601. Control *parent = get_parent()?get_parent()->cast_to<Control>():NULL;
  1602. if (parent && parent->data.theme_owner) {
  1603. _propagate_theme_changed(parent->data.theme_owner);
  1604. } else {
  1605. _propagate_theme_changed(NULL);
  1606. }
  1607. }
  1608. }
  1609. void Control::_window_accept_event() {
  1610. window->key_event_accepted=true;
  1611. if (is_inside_tree())
  1612. get_tree()->set_input_as_handled();
  1613. }
  1614. void Control::accept_event() {
  1615. if (is_inside_tree() && get_window())
  1616. get_window()->_window_accept_event();
  1617. }
  1618. Ref<Theme> Control::get_theme() const {
  1619. return data.theme;
  1620. }
  1621. void Control::set_tooltip(const String& p_tooltip) {
  1622. data.tooltip=p_tooltip;
  1623. }
  1624. String Control::get_tooltip(const Point2& p_pos) const {
  1625. return data.tooltip;
  1626. }
  1627. void Control::set_default_cursor_shape(CursorShape p_shape) {
  1628. data.default_cursor=p_shape;
  1629. }
  1630. Control::CursorShape Control::get_default_cursor_shape() const {
  1631. return data.default_cursor;
  1632. }
  1633. Control::CursorShape Control::get_cursor_shape(const Point2& p_pos) const {
  1634. return data.default_cursor;
  1635. }
  1636. Matrix32 Control::get_transform() const {
  1637. Matrix32 xf;
  1638. xf.set_origin(get_pos());
  1639. return xf;
  1640. }
  1641. String Control::_get_tooltip() const {
  1642. return data.tooltip;
  1643. }
  1644. void Control::set_focus_neighbour(Margin p_margin, const NodePath &p_neighbour) {
  1645. ERR_FAIL_INDEX(p_margin,4);
  1646. data.focus_neighbour[p_margin]=p_neighbour;
  1647. }
  1648. NodePath Control::get_focus_neighbour(Margin p_margin) const {
  1649. ERR_FAIL_INDEX_V(p_margin,4,NodePath());
  1650. return data.focus_neighbour[p_margin];
  1651. }
  1652. #define MAX_NEIGHBOUR_SEARCH_COUNT 512
  1653. Control *Control::_get_focus_neighbour(Margin p_margin,int p_count) {
  1654. if (p_count >= MAX_NEIGHBOUR_SEARCH_COUNT)
  1655. return NULL;
  1656. if (!data.focus_neighbour[p_margin].is_empty()) {
  1657. Control *c=NULL;
  1658. Node * n = get_node(data.focus_neighbour[p_margin]);
  1659. if (n) {
  1660. c=n->cast_to<Control>();
  1661. if (!c) {
  1662. ERR_EXPLAIN("Next focus node is not a control: "+n->get_name());
  1663. ERR_FAIL_V(NULL);
  1664. }
  1665. } else {
  1666. return NULL;
  1667. }
  1668. bool valid=true;
  1669. if (c->is_hidden())
  1670. valid=false;
  1671. if (c->get_focus_mode()==FOCUS_NONE)
  1672. valid=false;
  1673. if (valid)
  1674. return c;
  1675. c=c->_get_focus_neighbour(p_margin,p_count+1);
  1676. return c;
  1677. }
  1678. float dist=1e7;
  1679. Control * result=NULL;
  1680. Point2 points[4];
  1681. Matrix32 xform = get_global_transform();
  1682. Rect2 rect = get_item_rect();
  1683. points[0]=xform.xform(rect.pos);
  1684. points[1]=xform.xform(rect.pos + Point2(rect.size.x, 0));
  1685. points[2]=xform.xform(rect.pos + rect.size);
  1686. points[3]=xform.xform(rect.pos + Point2(0, rect.size.y));
  1687. const Vector2 dir[4]={
  1688. Vector2(-1,0),
  1689. Vector2(0,-1),
  1690. Vector2(1,0),
  1691. Vector2(0,1)
  1692. };
  1693. Vector2 vdir=dir[p_margin];
  1694. float maxd=-1e7;
  1695. for(int i=0;i<4;i++) {
  1696. float d = vdir.dot(points[i]);
  1697. if (d>maxd)
  1698. maxd=d;
  1699. }
  1700. Node *base=this;
  1701. while (base) {
  1702. Control *c = base->cast_to<Control>();
  1703. if (c) {
  1704. if (c->data.SI)
  1705. break;
  1706. if (c==data.window)
  1707. break;
  1708. }
  1709. base=base->get_parent();
  1710. }
  1711. if (!base)
  1712. return NULL;
  1713. _window_find_focus_neighbour(vdir,base,points,maxd,dist,&result);
  1714. return result;
  1715. }
  1716. 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) {
  1717. if (p_at->cast_to<Viewport>())
  1718. return; //bye
  1719. Control *c = p_at->cast_to<Control>();
  1720. if (c && c !=this && c->get_focus_mode()==FOCUS_ALL && c->is_visible()) {
  1721. Point2 points[4];
  1722. Matrix32 xform = c->get_global_transform();
  1723. Rect2 rect = c->get_item_rect();
  1724. points[0]=xform.xform(rect.pos);
  1725. points[1]=xform.xform(rect.pos + Point2(rect.size.x, 0));
  1726. points[2]=xform.xform(rect.pos + rect.size);
  1727. points[3]=xform.xform(rect.pos + Point2(0, rect.size.y));
  1728. float min=1e7;
  1729. for(int i=0;i<4;i++) {
  1730. float d = p_dir.dot(points[i]);
  1731. if (d < min)
  1732. min =d;
  1733. }
  1734. if (min>(p_min-CMP_EPSILON)) {
  1735. for(int i=0;i<4;i++) {
  1736. Vector2 la=p_points[i];
  1737. Vector2 lb=p_points[(i+1)%4];
  1738. for(int j=0;j<4;j++) {
  1739. Vector2 fa=points[j];
  1740. Vector2 fb=points[(j+1)%4];
  1741. Vector2 pa,pb;
  1742. float d=Geometry::get_closest_points_between_segments(la,lb,fa,fb,pa,pb);
  1743. //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));
  1744. if (d<r_closest_dist) {
  1745. r_closest_dist=d;
  1746. *r_closest=c;
  1747. }
  1748. }
  1749. }
  1750. }
  1751. }
  1752. for(int i=0;i<p_at->get_child_count();i++) {
  1753. Node *child=p_at->get_child(i);
  1754. Control *childc = child->cast_to<Control>();
  1755. if (childc && childc->data.SI)
  1756. continue; //subwindow, ignore
  1757. _window_find_focus_neighbour(p_dir,p_at->get_child(i),p_points,p_min,r_closest_dist,r_closest);
  1758. }
  1759. }
  1760. void Control::set_h_size_flags(int p_flags) {
  1761. if (data.h_size_flags==p_flags)
  1762. return;
  1763. data.h_size_flags=p_flags;
  1764. emit_signal(SceneStringNames::get_singleton()->size_flags_changed);
  1765. }
  1766. int Control::get_h_size_flags() const{
  1767. return data.h_size_flags;
  1768. }
  1769. void Control::set_v_size_flags(int p_flags) {
  1770. if (data.v_size_flags==p_flags)
  1771. return;
  1772. data.v_size_flags=p_flags;
  1773. emit_signal(SceneStringNames::get_singleton()->size_flags_changed);
  1774. }
  1775. void Control::set_stretch_ratio(float p_ratio) {
  1776. if (data.expand==p_ratio)
  1777. return;
  1778. data.expand=p_ratio;
  1779. emit_signal(SceneStringNames::get_singleton()->size_flags_changed);
  1780. }
  1781. float Control::get_stretch_ratio() const {
  1782. return data.expand;
  1783. }
  1784. void Control::grab_click_focus() {
  1785. ERR_FAIL_COND(!is_inside_tree());
  1786. if (data.window && data.window->window->mouse_focus) {
  1787. Window *w=data.window->window;
  1788. if (w->mouse_focus==this)
  1789. return;
  1790. InputEvent ie;
  1791. ie.type=InputEvent::MOUSE_BUTTON;
  1792. InputEventMouseButton &mb=ie.mouse_button;
  1793. //send unclic
  1794. Point2 click =w->mouse_focus->get_global_transform().affine_inverse().xform(w->last_mouse_pos);
  1795. mb.x=click.x;
  1796. mb.y=click.y;
  1797. mb.button_index=w->mouse_focus_button;
  1798. mb.pressed=false;
  1799. w->mouse_focus->call_deferred("_input_event",ie);
  1800. w->mouse_focus=this;
  1801. w->focus_inv_xform=w->mouse_focus->get_global_transform().affine_inverse();
  1802. click =w->mouse_focus->get_global_transform().affine_inverse().xform(w->last_mouse_pos);
  1803. mb.x=click.x;
  1804. mb.y=click.y;
  1805. mb.button_index=w->mouse_focus_button;
  1806. mb.pressed=true;
  1807. w->mouse_focus->call_deferred("_input_event",ie);
  1808. }
  1809. }
  1810. void Control::minimum_size_changed() {
  1811. if (!is_inside_tree())
  1812. return;
  1813. if (data.pending_min_size_update)
  1814. return;
  1815. data.pending_min_size_update=true;
  1816. MessageQueue::get_singleton()->push_call(this,"_update_minimum_size");
  1817. if (!is_toplevel_control()) {
  1818. Control *pc = get_parent_control();
  1819. if (pc)
  1820. pc->minimum_size_changed();
  1821. }
  1822. }
  1823. int Control::get_v_size_flags() const{
  1824. return data.v_size_flags;
  1825. }
  1826. void Control::set_ignore_mouse(bool p_ignore) {
  1827. data.ignore_mouse=p_ignore;
  1828. }
  1829. bool Control::is_ignoring_mouse() const {
  1830. return data.ignore_mouse;
  1831. }
  1832. void Control::set_stop_mouse(bool p_stop) {
  1833. data.stop_mouse=p_stop;
  1834. }
  1835. bool Control::is_stopping_mouse() const {
  1836. return data.stop_mouse;
  1837. }
  1838. Control *Control::get_focus_owner() const {
  1839. ERR_FAIL_COND_V(!is_inside_tree(),NULL);
  1840. ERR_FAIL_COND_V(!data.window,NULL);
  1841. return data.window->window->key_focus;
  1842. }
  1843. void Control::warp_mouse(const Point2& p_to_pos) {
  1844. ERR_FAIL_COND(!is_inside_tree());
  1845. get_viewport()->warp_mouse(get_global_transform().xform(p_to_pos));
  1846. }
  1847. void Control::_bind_methods() {
  1848. ObjectTypeDB::bind_method(_MD("_window_input_event"),&Control::_window_input_event);
  1849. ObjectTypeDB::bind_method(_MD("_gui_input"),&Control::_gui_input);
  1850. ObjectTypeDB::bind_method(_MD("_input_text"),&Control::_input_text);
  1851. // ObjectTypeDB::bind_method(_MD("_window_resize_event"),&Control::_window_resize_event);
  1852. ObjectTypeDB::bind_method(_MD("_window_remove_focus"),&Control::_window_remove_focus);
  1853. ObjectTypeDB::bind_method(_MD("_cancel_input_ID"),&Control::_window_cancel_input_ID);
  1854. ObjectTypeDB::bind_method(_MD("_cancel_tooltip"),&Control::_window_cancel_tooltip);
  1855. ObjectTypeDB::bind_method(_MD("_window_show_tooltip"),&Control::_window_show_tooltip);
  1856. ObjectTypeDB::bind_method(_MD("_size_changed"),&Control::_size_changed);
  1857. ObjectTypeDB::bind_method(_MD("_update_minimum_size"),&Control::_update_minimum_size);
  1858. ObjectTypeDB::bind_method(_MD("accept_event"),&Control::accept_event);
  1859. ObjectTypeDB::bind_method(_MD("get_minimum_size"),&Control::get_minimum_size);
  1860. ObjectTypeDB::bind_method(_MD("get_combined_minimum_size"),&Control::get_combined_minimum_size);
  1861. ObjectTypeDB::bind_method(_MD("is_window"),&Control::is_window);
  1862. ObjectTypeDB::bind_method(_MD("get_window"),&Control::get_window);
  1863. ObjectTypeDB::bind_method(_MD("set_anchor","margin","anchor_mode"),&Control::set_anchor);
  1864. ObjectTypeDB::bind_method(_MD("get_anchor","margin"),&Control::get_anchor);
  1865. ObjectTypeDB::bind_method(_MD("set_margin","margin","offset"),&Control::set_margin);
  1866. ObjectTypeDB::bind_method(_MD("set_anchor_and_margin","margin","anchor_mode","offset"),&Control::set_anchor_and_margin);
  1867. ObjectTypeDB::bind_method(_MD("set_begin","pos"),&Control::set_begin);
  1868. ObjectTypeDB::bind_method(_MD("set_end","pos"),&Control::set_end);
  1869. ObjectTypeDB::bind_method(_MD("set_pos","pos"),&Control::set_pos);
  1870. ObjectTypeDB::bind_method(_MD("set_size","size"),&Control::set_size);
  1871. ObjectTypeDB::bind_method(_MD("set_custom_minimum_size","size"),&Control::set_custom_minimum_size);
  1872. ObjectTypeDB::bind_method(_MD("set_global_pos","pos"),&Control::set_global_pos);
  1873. ObjectTypeDB::bind_method(_MD("get_margin","margin"),&Control::get_margin);
  1874. ObjectTypeDB::bind_method(_MD("get_begin"),&Control::get_begin);
  1875. ObjectTypeDB::bind_method(_MD("get_end"),&Control::get_end);
  1876. ObjectTypeDB::bind_method(_MD("get_pos"),&Control::get_pos);
  1877. ObjectTypeDB::bind_method(_MD("get_size"),&Control::get_size);
  1878. ObjectTypeDB::bind_method(_MD("get_custom_minimum_size"),&Control::get_custom_minimum_size);
  1879. ObjectTypeDB::bind_method(_MD("get_parent_area_size"),&Control::get_size);
  1880. ObjectTypeDB::bind_method(_MD("get_global_pos"),&Control::get_global_pos);
  1881. ObjectTypeDB::bind_method(_MD("get_rect"),&Control::get_rect);
  1882. ObjectTypeDB::bind_method(_MD("get_global_rect"),&Control::get_global_rect);
  1883. ObjectTypeDB::bind_method(_MD("set_area_as_parent_rect","margin"),&Control::set_area_as_parent_rect,DEFVAL(0));
  1884. ObjectTypeDB::bind_method(_MD("show_modal","exclusive"),&Control::show_modal,DEFVAL(false));
  1885. ObjectTypeDB::bind_method(_MD("set_focus_mode","mode"),&Control::set_focus_mode);
  1886. ObjectTypeDB::bind_method(_MD("has_focus"),&Control::has_focus);
  1887. ObjectTypeDB::bind_method(_MD("grab_focus"),&Control::grab_focus);
  1888. ObjectTypeDB::bind_method(_MD("release_focus"),&Control::release_focus);
  1889. ObjectTypeDB::bind_method(_MD("get_focus_owner:Control"),&Control::get_focus_owner);
  1890. ObjectTypeDB::bind_method(_MD("set_h_size_flags","flags"),&Control::set_h_size_flags);
  1891. ObjectTypeDB::bind_method(_MD("get_h_size_flags"),&Control::get_h_size_flags);
  1892. ObjectTypeDB::bind_method(_MD("set_stretch_ratio","ratio"),&Control::set_stretch_ratio);
  1893. ObjectTypeDB::bind_method(_MD("get_stretch_ratio"),&Control::get_stretch_ratio);
  1894. ObjectTypeDB::bind_method(_MD("set_v_size_flags","flags"),&Control::set_v_size_flags);
  1895. ObjectTypeDB::bind_method(_MD("get_v_size_flags"),&Control::get_v_size_flags);
  1896. ObjectTypeDB::bind_method(_MD("set_theme","theme:Theme"),&Control::set_theme);
  1897. ObjectTypeDB::bind_method(_MD("get_theme:Theme"),&Control::get_theme);
  1898. ObjectTypeDB::bind_method(_MD("add_icon_override","name","texture:Texture"),&Control::add_icon_override);
  1899. ObjectTypeDB::bind_method(_MD("add_style_override","name","stylebox:StyleBox"),&Control::add_style_override);
  1900. ObjectTypeDB::bind_method(_MD("add_font_override","name","font:Font"),&Control::add_font_override);
  1901. ObjectTypeDB::bind_method(_MD("add_color_override","name","color"),&Control::add_color_override);
  1902. ObjectTypeDB::bind_method(_MD("add_constant_override","name","constant"),&Control::add_constant_override);
  1903. ObjectTypeDB::bind_method(_MD("get_icon:Texture","name","type"),&Control::get_icon,DEFVAL(""));
  1904. ObjectTypeDB::bind_method(_MD("get_stylebox:StyleBox","name","type"),&Control::get_stylebox,DEFVAL(""));
  1905. ObjectTypeDB::bind_method(_MD("get_font:Font","name","type"),&Control::get_font,DEFVAL(""));
  1906. ObjectTypeDB::bind_method(_MD("get_color","name","type"),&Control::get_color,DEFVAL(""));
  1907. ObjectTypeDB::bind_method(_MD("get_constant","name","type"),&Control::get_constant,DEFVAL(""));
  1908. ObjectTypeDB::bind_method(_MD("get_parent_control:Control"),&Control::get_parent_control);
  1909. ObjectTypeDB::bind_method(_MD("set_tooltip","tooltip"),&Control::set_tooltip);
  1910. ObjectTypeDB::bind_method(_MD("get_tooltip","atpos"),&Control::get_tooltip,DEFVAL(Point2()));
  1911. ObjectTypeDB::bind_method(_MD("_get_tooltip"),&Control::_get_tooltip);
  1912. ObjectTypeDB::bind_method(_MD("set_default_cursor_shape","shape"),&Control::set_default_cursor_shape);
  1913. ObjectTypeDB::bind_method(_MD("get_default_cursor_shape"),&Control::get_default_cursor_shape);
  1914. ObjectTypeDB::bind_method(_MD("get_cursor_shape","pos"),&Control::get_cursor_shape,DEFVAL(Point2()));
  1915. ObjectTypeDB::bind_method(_MD("set_focus_neighbour","margin","neighbour"),&Control::set_focus_neighbour);
  1916. ObjectTypeDB::bind_method(_MD("get_focus_neighbour","margin"),&Control::get_focus_neighbour);
  1917. ObjectTypeDB::bind_method(_MD("set_ignore_mouse","ignore"),&Control::set_ignore_mouse);
  1918. ObjectTypeDB::bind_method(_MD("is_ignoring_mouse"),&Control::is_ignoring_mouse);
  1919. ObjectTypeDB::bind_method(_MD("force_drag","data","preview"),&Control::force_drag);
  1920. ObjectTypeDB::bind_method(_MD("set_stop_mouse","stop"),&Control::set_stop_mouse);
  1921. ObjectTypeDB::bind_method(_MD("is_stopping_mouse"),&Control::is_stopping_mouse);
  1922. ObjectTypeDB::bind_method(_MD("grab_click_focus"),&Control::grab_click_focus);
  1923. ObjectTypeDB::bind_method(_MD("set_drag_preview","control:Control"),&Control::set_drag_preview);
  1924. ObjectTypeDB::bind_method(_MD("warp_mouse","to_pos"),&Control::warp_mouse);
  1925. BIND_VMETHOD(MethodInfo("_input_event",PropertyInfo(Variant::INPUT_EVENT,"event")));
  1926. BIND_VMETHOD(MethodInfo(Variant::VECTOR2,"get_minimum_size"));
  1927. BIND_VMETHOD(MethodInfo(Variant::OBJECT,"get_drag_data",PropertyInfo(Variant::VECTOR2,"pos")));
  1928. BIND_VMETHOD(MethodInfo(Variant::BOOL,"can_drop_data",PropertyInfo(Variant::VECTOR2,"pos"),PropertyInfo(Variant::NIL,"data")));
  1929. BIND_VMETHOD(MethodInfo("drop_data",PropertyInfo(Variant::VECTOR2,"pos"),PropertyInfo(Variant::NIL,"data")));
  1930. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/left", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("set_anchor"),_SCS("get_anchor"), MARGIN_LEFT );
  1931. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/top", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("set_anchor"),_SCS("get_anchor"), MARGIN_TOP );
  1932. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/right", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("set_anchor"),_SCS("get_anchor"), MARGIN_RIGHT );
  1933. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/bottom", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("set_anchor"),_SCS("get_anchor"), MARGIN_BOTTOM );
  1934. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/left", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_LEFT );
  1935. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/top", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_TOP );
  1936. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/right", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_RIGHT );
  1937. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/bottom", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_BOTTOM );
  1938. ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect/pos", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_EDITOR), _SCS("set_pos"),_SCS("get_pos") );
  1939. ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect/size", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_EDITOR), _SCS("set_size"),_SCS("get_size") );
  1940. ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect/min_size"), _SCS("set_custom_minimum_size"),_SCS("get_custom_minimum_size") );
  1941. ADD_PROPERTYNZ( PropertyInfo(Variant::STRING,"hint/tooltip", PROPERTY_HINT_MULTILINE_TEXT), _SCS("set_tooltip"),_SCS("_get_tooltip") );
  1942. ADD_PROPERTYI( PropertyInfo(Variant::NODE_PATH,"focus_neighbour/left" ), _SCS("set_focus_neighbour"),_SCS("get_focus_neighbour"),MARGIN_LEFT );
  1943. ADD_PROPERTYI( PropertyInfo(Variant::NODE_PATH,"focus_neighbour/top" ), _SCS("set_focus_neighbour"),_SCS("get_focus_neighbour"),MARGIN_TOP );
  1944. ADD_PROPERTYI( PropertyInfo(Variant::NODE_PATH,"focus_neighbour/right" ), _SCS("set_focus_neighbour"),_SCS("get_focus_neighbour"),MARGIN_RIGHT );
  1945. ADD_PROPERTYI( PropertyInfo(Variant::NODE_PATH,"focus_neighbour/bottom" ), _SCS("set_focus_neighbour"),_SCS("get_focus_neighbour"),MARGIN_BOTTOM );
  1946. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"focus/ignore_mouse"), _SCS("set_ignore_mouse"),_SCS("is_ignoring_mouse") );
  1947. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"focus/stop_mouse"), _SCS("set_stop_mouse"),_SCS("is_stopping_mouse") );
  1948. ADD_PROPERTYNZ( PropertyInfo(Variant::INT,"size_flags/horizontal", PROPERTY_HINT_FLAGS, "Expand,Fill"), _SCS("set_h_size_flags"),_SCS("get_h_size_flags") );
  1949. ADD_PROPERTYNZ( PropertyInfo(Variant::INT,"size_flags/vertical", PROPERTY_HINT_FLAGS, "Expand,Fill"), _SCS("set_v_size_flags"),_SCS("get_v_size_flags") );
  1950. ADD_PROPERTY( PropertyInfo(Variant::INT,"size_flags/stretch_ratio", PROPERTY_HINT_RANGE, "1,128,0.01"), _SCS("set_stretch_ratio"),_SCS("get_stretch_ratio") );
  1951. ADD_PROPERTYNZ( PropertyInfo(Variant::OBJECT,"theme/theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), _SCS("set_theme"),_SCS("get_theme") );
  1952. BIND_CONSTANT( ANCHOR_BEGIN );
  1953. BIND_CONSTANT( ANCHOR_END );
  1954. BIND_CONSTANT( ANCHOR_RATIO );
  1955. BIND_CONSTANT( ANCHOR_CENTER );
  1956. BIND_CONSTANT( FOCUS_NONE );
  1957. BIND_CONSTANT( FOCUS_CLICK );
  1958. BIND_CONSTANT( FOCUS_ALL );
  1959. BIND_CONSTANT( NOTIFICATION_RESIZED );
  1960. BIND_CONSTANT( NOTIFICATION_MOUSE_ENTER );
  1961. BIND_CONSTANT( NOTIFICATION_MOUSE_EXIT );
  1962. BIND_CONSTANT( NOTIFICATION_FOCUS_ENTER );
  1963. BIND_CONSTANT( NOTIFICATION_FOCUS_EXIT );
  1964. BIND_CONSTANT( NOTIFICATION_THEME_CHANGED );
  1965. BIND_CONSTANT( NOTIFICATION_MODAL_CLOSE );
  1966. BIND_CONSTANT( CURSOR_ARROW );
  1967. BIND_CONSTANT( CURSOR_IBEAM );
  1968. BIND_CONSTANT( CURSOR_POINTING_HAND );
  1969. BIND_CONSTANT( CURSOR_CROSS );
  1970. BIND_CONSTANT( CURSOR_WAIT );
  1971. BIND_CONSTANT( CURSOR_BUSY );
  1972. BIND_CONSTANT( CURSOR_DRAG );
  1973. BIND_CONSTANT( CURSOR_CAN_DROP );
  1974. BIND_CONSTANT( CURSOR_FORBIDDEN );
  1975. BIND_CONSTANT( CURSOR_VSIZE );
  1976. BIND_CONSTANT( CURSOR_HSIZE );
  1977. BIND_CONSTANT( CURSOR_BDIAGSIZE );
  1978. BIND_CONSTANT( CURSOR_FDIAGSIZE );
  1979. BIND_CONSTANT( CURSOR_MOVE );
  1980. BIND_CONSTANT( CURSOR_VSPLIT );
  1981. BIND_CONSTANT( CURSOR_HSPLIT );
  1982. BIND_CONSTANT( CURSOR_HELP );
  1983. BIND_CONSTANT( SIZE_EXPAND );
  1984. BIND_CONSTANT( SIZE_FILL );
  1985. BIND_CONSTANT( SIZE_EXPAND_FILL );
  1986. ADD_SIGNAL( MethodInfo("resized") );
  1987. ADD_SIGNAL( MethodInfo("input_event") );
  1988. ADD_SIGNAL( MethodInfo("mouse_enter") );
  1989. ADD_SIGNAL( MethodInfo("mouse_exit") );
  1990. ADD_SIGNAL( MethodInfo("focus_enter") );
  1991. ADD_SIGNAL( MethodInfo("focus_exit") );
  1992. ADD_SIGNAL( MethodInfo("size_flags_changed") );
  1993. ADD_SIGNAL( MethodInfo("minimum_size_changed") );
  1994. }
  1995. Control::Control() {
  1996. data.parent=NULL;
  1997. data.window=NULL;
  1998. data.viewport=NULL;
  1999. data.ignore_mouse=false;
  2000. data.stop_mouse=true;
  2001. window=NULL;
  2002. data.SI=NULL;
  2003. data.MI=NULL;
  2004. data.modal=false;
  2005. data.theme_owner=NULL;
  2006. data.modal_exclusive=false;
  2007. data.default_cursor = CURSOR_ARROW;
  2008. data.h_size_flags=SIZE_FILL;
  2009. data.v_size_flags=SIZE_FILL;
  2010. data.expand=1;
  2011. data.pending_min_size_update=false;
  2012. for (int i=0;i<4;i++) {
  2013. data.anchor[i]=ANCHOR_BEGIN;
  2014. data.margin[i]=0;
  2015. }
  2016. data.focus_mode=FOCUS_NONE;
  2017. data.modal_prev_focus_owner=0;
  2018. }
  2019. Control::~Control()
  2020. {
  2021. }