control.cpp 76 KB

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