control.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326
  1. /*************************************************************************/
  2. /* control.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "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. #ifdef TOOLS_ENABLED
  42. #include "tools/editor/editor_settings.h"
  43. #endif
  44. #include <stdio.h>
  45. Variant Control::edit_get_state() const {
  46. return get_rect();
  47. }
  48. void Control::edit_set_state(const Variant& p_state) {
  49. Rect2 state=p_state;
  50. set_pos(state.pos);
  51. set_size(state.size);
  52. }
  53. void Control::set_custom_minimum_size(const Size2& p_custom) {
  54. if (p_custom==data.custom_minimum_size)
  55. return;
  56. data.custom_minimum_size=p_custom;
  57. minimum_size_changed();
  58. }
  59. Size2 Control::get_custom_minimum_size() const{
  60. return data.custom_minimum_size;
  61. }
  62. Size2 Control::get_combined_minimum_size() const {
  63. Size2 minsize = get_minimum_size();
  64. minsize.x = MAX(minsize.x,data.custom_minimum_size.x);
  65. minsize.y = MAX(minsize.y,data.custom_minimum_size.y);
  66. return minsize;
  67. }
  68. Size2 Control::edit_get_minimum_size() const {
  69. return get_combined_minimum_size();
  70. }
  71. void Control::edit_set_rect(const Rect2& p_edit_rect) {
  72. Matrix32 postxf;
  73. postxf.set_rotation_and_scale(data.rotation,data.scale);
  74. Vector2 new_pos = postxf.xform(p_edit_rect.pos);
  75. Vector2 pos = get_pos()+new_pos;
  76. Rect2 new_rect=get_rect();
  77. new_rect.pos=pos.snapped(Vector2(1,1));
  78. new_rect.size=p_edit_rect.size.snapped(Vector2(1,1));
  79. set_pos(new_rect.pos);
  80. set_size(new_rect.size);
  81. }
  82. bool Control::_set(const StringName& p_name, const Variant& p_value) {
  83. String name= p_name;
  84. if (!name.begins_with("custom"))
  85. return false;
  86. if (p_value.get_type()==Variant::NIL) {
  87. if (name.begins_with("custom_icons/")) {
  88. String dname = name.get_slicec('/',1);
  89. data.icon_override.erase(dname);
  90. notification(NOTIFICATION_THEME_CHANGED);
  91. update();
  92. } else if (name.begins_with("custom_shaders/")) {
  93. String dname = name.get_slicec('/',1);
  94. data.shader_override.erase(dname);
  95. notification(NOTIFICATION_THEME_CHANGED);
  96. update();
  97. } else if (name.begins_with("custom_styles/")) {
  98. String dname = name.get_slicec('/',1);
  99. data.style_override.erase(dname);
  100. notification(NOTIFICATION_THEME_CHANGED);
  101. update();
  102. } else if (name.begins_with("custom_fonts/")) {
  103. String dname = name.get_slicec('/',1);
  104. data.font_override.erase(dname);
  105. notification(NOTIFICATION_THEME_CHANGED);
  106. update();
  107. } else if (name.begins_with("custom_colors/")) {
  108. String dname = name.get_slicec('/',1);
  109. data.color_override.erase(dname);
  110. notification(NOTIFICATION_THEME_CHANGED);
  111. update();
  112. } else if (name.begins_with("custom_constants/")) {
  113. String dname = name.get_slicec('/',1);
  114. data.constant_override.erase(dname);
  115. notification(NOTIFICATION_THEME_CHANGED);
  116. update();
  117. } else
  118. return false;
  119. } else {
  120. if (name.begins_with("custom_icons/")) {
  121. String dname = name.get_slicec('/',1);
  122. notification(NOTIFICATION_THEME_CHANGED);
  123. add_icon_override(dname,p_value);
  124. } else if (name.begins_with("custom_shaders/")) {
  125. String dname = name.get_slicec('/',1);
  126. add_shader_override(dname,p_value);
  127. notification(NOTIFICATION_THEME_CHANGED);
  128. } else if (name.begins_with("custom_styles/")) {
  129. String dname = name.get_slicec('/',1);
  130. add_style_override(dname,p_value);
  131. notification(NOTIFICATION_THEME_CHANGED);
  132. } else if (name.begins_with("custom_fonts/")) {
  133. String dname = name.get_slicec('/',1);
  134. add_font_override(dname,p_value);
  135. notification(NOTIFICATION_THEME_CHANGED);
  136. } else if (name.begins_with("custom_colors/")) {
  137. String dname = name.get_slicec('/',1);
  138. add_color_override(dname,p_value);
  139. notification(NOTIFICATION_THEME_CHANGED);
  140. } else if (name.begins_with("custom_constants/")) {
  141. String dname = name.get_slicec('/',1);
  142. add_constant_override(dname,p_value);
  143. notification(NOTIFICATION_THEME_CHANGED);
  144. } else
  145. return false;
  146. }
  147. return true;
  148. }
  149. void Control::_update_minimum_size() {
  150. if (!is_inside_tree())
  151. return;
  152. data.pending_min_size_update=false;
  153. Size2 minsize = get_combined_minimum_size();
  154. if (minsize.x > data.size_cache.x ||
  155. minsize.y > data.size_cache.y
  156. ) {
  157. _size_changed();
  158. }
  159. emit_signal(SceneStringNames::get_singleton()->minimum_size_changed);
  160. }
  161. bool Control::_get(const StringName& p_name,Variant &r_ret) const {
  162. String sname=p_name;
  163. if (!sname.begins_with("custom"))
  164. return false;
  165. if (sname.begins_with("custom_icons/")) {
  166. String name = sname.get_slicec('/',1);
  167. r_ret= data.icon_override.has(name)?Variant(data.icon_override[name]):Variant();
  168. } else if (sname.begins_with("custom_shaders/")) {
  169. String name = sname.get_slicec('/',1);
  170. r_ret= data.shader_override.has(name)?Variant(data.shader_override[name]):Variant();
  171. } else if (sname.begins_with("custom_styles/")) {
  172. String name = sname.get_slicec('/',1);
  173. r_ret= data.style_override.has(name)?Variant(data.style_override[name]):Variant();
  174. } else if (sname.begins_with("custom_fonts/")) {
  175. String name = sname.get_slicec('/',1);
  176. r_ret= data.font_override.has(name)?Variant(data.font_override[name]):Variant();
  177. } else if (sname.begins_with("custom_colors/")) {
  178. String name = sname.get_slicec('/',1);
  179. r_ret= data.color_override.has(name)?Variant(data.color_override[name]):Variant();
  180. } else if (sname.begins_with("custom_constants/")) {
  181. String name = sname.get_slicec('/',1);
  182. r_ret= data.constant_override.has(name)?Variant(data.constant_override[name]):Variant();
  183. } else
  184. return false;
  185. return true;
  186. }
  187. void Control::_get_property_list( List<PropertyInfo> *p_list) const {
  188. Ref<Theme> theme;
  189. if (data.theme.is_valid()) {
  190. theme=data.theme;
  191. } else {
  192. theme=Theme::get_default();
  193. }
  194. {
  195. List<StringName> names;
  196. theme->get_icon_list(get_type_name(),&names);
  197. for(List<StringName>::Element *E=names.front();E;E=E->next()) {
  198. uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE;
  199. if (data.icon_override.has(E->get()))
  200. hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED;
  201. p_list->push_back( PropertyInfo(Variant::OBJECT,"custom_icons/"+E->get(),PROPERTY_HINT_RESOURCE_TYPE, "Texture",hint) );
  202. }
  203. }
  204. {
  205. List<StringName> names;
  206. theme->get_shader_list(get_type_name(),&names);
  207. for(List<StringName>::Element *E=names.front();E;E=E->next()) {
  208. uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE;
  209. if (data.shader_override.has(E->get()))
  210. hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED;
  211. p_list->push_back( PropertyInfo(Variant::OBJECT,"custom_shaders/"+E->get(),PROPERTY_HINT_RESOURCE_TYPE, "CanvasItemShader,CanvasItemShaderGraph",hint) );
  212. }
  213. }
  214. {
  215. List<StringName> names;
  216. theme->get_stylebox_list(get_type_name(),&names);
  217. for(List<StringName>::Element *E=names.front();E;E=E->next()) {
  218. uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE;
  219. if (data.style_override.has(E->get()))
  220. hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED;
  221. p_list->push_back( PropertyInfo(Variant::OBJECT,"custom_styles/"+E->get(),PROPERTY_HINT_RESOURCE_TYPE, "StyleBox",hint) );
  222. }
  223. }
  224. {
  225. List<StringName> names;
  226. theme->get_font_list(get_type_name(),&names);
  227. for(List<StringName>::Element *E=names.front();E;E=E->next()) {
  228. uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE;
  229. if (data.font_override.has(E->get()))
  230. hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED;
  231. p_list->push_back( PropertyInfo(Variant::OBJECT,"custom_fonts/"+E->get(),PROPERTY_HINT_RESOURCE_TYPE, "Font",hint) );
  232. }
  233. }
  234. {
  235. List<StringName> names;
  236. theme->get_color_list(get_type_name(),&names);
  237. for(List<StringName>::Element *E=names.front();E;E=E->next()) {
  238. uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE;
  239. if (data.color_override.has(E->get()))
  240. hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED;
  241. p_list->push_back( PropertyInfo(Variant::COLOR,"custom_colors/"+E->get(),PROPERTY_HINT_NONE, "",hint) );
  242. }
  243. }
  244. {
  245. List<StringName> names;
  246. theme->get_constant_list(get_type_name(),&names);
  247. for(List<StringName>::Element *E=names.front();E;E=E->next()) {
  248. uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE;
  249. if (data.constant_override.has(E->get()))
  250. hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED;
  251. p_list->push_back( PropertyInfo(Variant::INT,"custom_constants/"+E->get(),PROPERTY_HINT_RANGE, "-16384,16384",hint) );
  252. }
  253. }
  254. }
  255. Control *Control::get_parent_control() const {
  256. return data.parent;
  257. }
  258. void Control::_resize(const Size2& p_size) {
  259. _size_changed();
  260. }
  261. void Control::_notification(int p_notification) {
  262. switch(p_notification) {
  263. case NOTIFICATION_ENTER_TREE: {
  264. _size_changed();
  265. } break;
  266. case NOTIFICATION_EXIT_TREE: {
  267. get_viewport()->_gui_remove_control(this);
  268. } break;
  269. case NOTIFICATION_ENTER_CANVAS: {
  270. data.parent=get_parent()->cast_to<Control>();
  271. if (is_set_as_toplevel()) {
  272. data.SI=get_viewport()->_gui_add_subwindow_control(this);
  273. } else {
  274. Node *parent=this; //meh
  275. Node *parent_control=NULL;
  276. bool subwindow=false;
  277. while(parent) {
  278. parent=parent->get_parent();
  279. if (!parent)
  280. break;
  281. CanvasItem *ci =parent->cast_to<CanvasItem>();
  282. if (ci && ci->is_set_as_toplevel()) {
  283. subwindow=true;
  284. break;
  285. }
  286. if (parent->cast_to<Control>()) {
  287. parent_control=parent->cast_to<Control>();
  288. break;
  289. } else if (ci) {
  290. } else {
  291. break;
  292. }
  293. }
  294. if (parent_control) {
  295. //do nothing, has a parent control
  296. } else if (subwindow) {
  297. //is a subwindow (process input before other controls for that canvas)
  298. data.SI=get_viewport()->_gui_add_subwindow_control(this);
  299. } else {
  300. //is a regular root control
  301. data.RI=get_viewport()->_gui_add_root_control(this);
  302. }
  303. data.parent_canvas_item=get_parent_item();
  304. if (data.parent_canvas_item) {
  305. data.parent_canvas_item->connect("item_rect_changed",this,"_size_changed");
  306. } else {
  307. //connect viewport
  308. get_viewport()->connect("size_changed",this,"_size_changed");
  309. }
  310. }
  311. } break;
  312. case NOTIFICATION_EXIT_CANVAS: {
  313. if (data.parent_canvas_item) {
  314. data.parent_canvas_item->disconnect("item_rect_changed",this,"_size_changed");
  315. data.parent_canvas_item=NULL;
  316. } else if (!is_set_as_toplevel()) {
  317. //disconnect viewport
  318. get_viewport()->disconnect("size_changed",this,"_size_changed");
  319. }
  320. if (data.MI) {
  321. get_viewport()->_gui_remove_modal_control(data.MI);
  322. data.MI=NULL;
  323. }
  324. if (data.SI) {
  325. get_viewport()->_gui_remove_subwindow_control(data.SI);
  326. data.SI=NULL;
  327. }
  328. if (data.RI) {
  329. get_viewport()->_gui_remove_root_control(data.RI);
  330. data.RI=NULL;
  331. }
  332. data.parent=NULL;
  333. data.parent_canvas_item=NULL;
  334. } break;
  335. case NOTIFICATION_PARENTED: {
  336. Control * parent = get_parent()->cast_to<Control>();
  337. //make children reference them theme
  338. if (parent && data.theme.is_null() && parent->data.theme_owner) {
  339. _propagate_theme_changed(parent->data.theme_owner);
  340. }
  341. } break;
  342. case NOTIFICATION_UNPARENTED: {
  343. //make children unreference the theme
  344. if (data.theme.is_null() && data.theme_owner) {
  345. _propagate_theme_changed(NULL);
  346. }
  347. } break;
  348. case NOTIFICATION_MOVED_IN_PARENT: {
  349. // some parents need to know the order of the childrens to draw (like TabContainer)
  350. // update if necesary
  351. if (data.parent)
  352. data.parent->update();
  353. update();
  354. if (data.SI) {
  355. get_viewport()->_gui_set_subwindow_order_dirty();
  356. }
  357. if (data.RI) {
  358. get_viewport()->_gui_set_root_order_dirty();
  359. }
  360. } break;
  361. case NOTIFICATION_RESIZED: {
  362. emit_signal(SceneStringNames::get_singleton()->resized);
  363. } break;
  364. case NOTIFICATION_DRAW: {
  365. Matrix32 xform=Matrix32(data.rotation,get_pos());
  366. xform.scale_basis(data.scale);
  367. VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(),xform);
  368. VisualServer::get_singleton()->canvas_item_set_custom_rect( get_canvas_item(),true, Rect2(Point2(),get_size()));
  369. //emit_signal(SceneStringNames::get_singleton()->draw);
  370. } break;
  371. case NOTIFICATION_MOUSE_ENTER: {
  372. emit_signal(SceneStringNames::get_singleton()->mouse_enter);
  373. } break;
  374. case NOTIFICATION_MOUSE_EXIT: {
  375. emit_signal(SceneStringNames::get_singleton()->mouse_exit);
  376. } break;
  377. case NOTIFICATION_FOCUS_ENTER: {
  378. emit_signal(SceneStringNames::get_singleton()->focus_enter);
  379. update();
  380. } break;
  381. case NOTIFICATION_FOCUS_EXIT: {
  382. emit_signal(SceneStringNames::get_singleton()->focus_exit);
  383. update();
  384. } break;
  385. case NOTIFICATION_THEME_CHANGED: {
  386. update();
  387. } break;
  388. case NOTIFICATION_MODAL_CLOSE: {
  389. emit_signal("modal_close");
  390. } break;
  391. case NOTIFICATION_VISIBILITY_CHANGED: {
  392. if (!is_visible()) {
  393. get_viewport()->_gui_hid_control(this);
  394. _modal_stack_remove();
  395. minimum_size_changed();
  396. //remove key focus
  397. //remove modalness
  398. } else {
  399. _size_changed();
  400. }
  401. } break;
  402. case SceneTree::NOTIFICATION_WM_UNFOCUS_REQUEST: {
  403. get_viewport()->_gui_unfocus_control(this);
  404. } break;
  405. }
  406. }
  407. bool Control::clips_input() const {
  408. return false;
  409. }
  410. bool Control::has_point(const Point2& p_point) const {
  411. if (get_script_instance()) {
  412. Variant v=p_point;
  413. const Variant *p=&v;
  414. Variant::CallError ce;
  415. Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->has_point,&p,1,ce);
  416. if (ce.error==Variant::CallError::CALL_OK) {
  417. return ret;
  418. }
  419. }
  420. /*if (has_stylebox("mask")) {
  421. Ref<StyleBox> mask = get_stylebox("mask");
  422. return mask->test_mask(p_point,Rect2(Point2(),get_size()));
  423. }*/
  424. return Rect2( Point2(), get_size() ).has_point(p_point);
  425. }
  426. Variant Control::get_drag_data(const Point2& p_point) {
  427. if (get_script_instance()) {
  428. Variant v=p_point;
  429. const Variant *p=&v;
  430. Variant::CallError ce;
  431. Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->get_drag_data,&p,1,ce);
  432. if (ce.error==Variant::CallError::CALL_OK)
  433. return ret;
  434. }
  435. return Variant();
  436. }
  437. bool Control::can_drop_data(const Point2& p_point,const Variant& p_data) const {
  438. if (get_script_instance()) {
  439. Variant v=p_point;
  440. const Variant *p[2]={&v,&p_data};
  441. Variant::CallError ce;
  442. Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->can_drop_data,p,2,ce);
  443. if (ce.error==Variant::CallError::CALL_OK)
  444. return ret;
  445. }
  446. return Variant();
  447. }
  448. void Control::drop_data(const Point2& p_point,const Variant& p_data){
  449. if (get_script_instance()) {
  450. Variant v=p_point;
  451. const Variant *p[2]={&v,&p_data};
  452. Variant::CallError ce;
  453. Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->drop_data,p,2,ce);
  454. if (ce.error==Variant::CallError::CALL_OK)
  455. return;
  456. }
  457. }
  458. void Control::force_drag(const Variant& p_data,Control *p_control) {
  459. ERR_FAIL_COND(!is_inside_tree());
  460. ERR_FAIL_COND(p_data.get_type()==Variant::NIL);
  461. get_viewport()->_gui_force_drag(this,p_data,p_control);
  462. }
  463. void Control::set_drag_preview(Control *p_control) {
  464. ERR_FAIL_COND(!is_inside_tree());
  465. get_viewport()->_gui_set_drag_preview(this,p_control);
  466. }
  467. bool Control::is_window_modal_on_top() const {
  468. if (!is_inside_tree())
  469. return false;
  470. return get_viewport()->_gui_is_modal_on_top(this);
  471. }
  472. Size2 Control::get_minimum_size() const {
  473. ScriptInstance *si = const_cast<Control*>(this)->get_script_instance();
  474. if (si) {
  475. Variant::CallError ce;
  476. Variant s = si->call(SceneStringNames::get_singleton()->get_minimum_size,NULL,0,ce);
  477. if (ce.error==Variant::CallError::CALL_OK)
  478. return s;
  479. }
  480. return Size2();
  481. }
  482. Ref<Texture> Control::get_icon(const StringName& p_name,const StringName& p_type) const {
  483. if (p_type==StringName()) {
  484. const Ref<Texture>* tex = data.icon_override.getptr(p_name);
  485. if (tex)
  486. return *tex;
  487. }
  488. StringName type = p_type?p_type:get_type_name();
  489. // try with custom themes
  490. Control *theme_owner = data.theme_owner;
  491. while(theme_owner) {
  492. if (theme_owner->data.theme->has_icon(p_name, type ) )
  493. return data.theme_owner->data.theme->get_icon(p_name, type );
  494. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  495. if (parent)
  496. theme_owner=parent->data.theme_owner;
  497. else
  498. theme_owner=NULL;
  499. }
  500. return Theme::get_default()->get_icon( p_name, type );
  501. }
  502. Ref<Shader> Control::get_shader(const StringName& p_name,const StringName& p_type) const {
  503. if (p_type==StringName()) {
  504. const Ref<Shader>* sdr = data.shader_override.getptr(p_name);
  505. if (sdr)
  506. return *sdr;
  507. }
  508. StringName type = p_type?p_type:get_type_name();
  509. // try with custom themes
  510. Control *theme_owner = data.theme_owner;
  511. while(theme_owner) {
  512. if (theme_owner->data.theme->has_shader(p_name, type))
  513. return data.theme_owner->data.theme->get_shader(p_name, type );
  514. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  515. if (parent)
  516. theme_owner=parent->data.theme_owner;
  517. else
  518. theme_owner=NULL;
  519. }
  520. return Theme::get_default()->get_shader( p_name, type );
  521. }
  522. Ref<StyleBox> Control::get_stylebox(const StringName& p_name,const StringName& p_type) const {
  523. if (p_type==StringName()) {
  524. const Ref<StyleBox>* style = data.style_override.getptr(p_name);
  525. if (style)
  526. return *style;
  527. }
  528. StringName type = p_type?p_type:get_type_name();
  529. // try with custom themes
  530. Control *theme_owner = data.theme_owner;
  531. while(theme_owner) {
  532. if (theme_owner->data.theme->has_stylebox(p_name, type ) )
  533. return data.theme_owner->data.theme->get_stylebox(p_name, type );
  534. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  535. if (parent)
  536. theme_owner=parent->data.theme_owner;
  537. else
  538. theme_owner=NULL;
  539. }
  540. return Theme::get_default()->get_stylebox( p_name, type );
  541. }
  542. Ref<Font> Control::get_font(const StringName& p_name,const StringName& p_type) const {
  543. if (p_type==StringName()) {
  544. const Ref<Font>* font = data.font_override.getptr(p_name);
  545. if (font)
  546. return *font;
  547. }
  548. StringName type = p_type?p_type:get_type_name();
  549. // try with custom themes
  550. Control *theme_owner = data.theme_owner;
  551. while(theme_owner) {
  552. if (theme_owner->data.theme->has_font(p_name, type ) )
  553. return data.theme_owner->data.theme->get_font(p_name, type );
  554. if (theme_owner->data.theme->get_default_theme_font().is_valid())
  555. return theme_owner->data.theme->get_default_theme_font();
  556. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  557. if (parent)
  558. theme_owner=parent->data.theme_owner;
  559. else
  560. theme_owner=NULL;
  561. }
  562. return Theme::get_default()->get_font( p_name, type );
  563. }
  564. Color Control::get_color(const StringName& p_name,const StringName& p_type) const {
  565. if (p_type==StringName()) {
  566. const Color* color = data.color_override.getptr(p_name);
  567. if (color)
  568. return *color;
  569. }
  570. StringName type = p_type?p_type:get_type_name();
  571. // try with custom themes
  572. Control *theme_owner = data.theme_owner;
  573. while(theme_owner) {
  574. if (theme_owner->data.theme->has_color(p_name, type ) )
  575. return data.theme_owner->data.theme->get_color(p_name, type );
  576. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  577. if (parent)
  578. theme_owner=parent->data.theme_owner;
  579. else
  580. theme_owner=NULL;
  581. }
  582. return Theme::get_default()->get_color( p_name, type );
  583. }
  584. int Control::get_constant(const StringName& p_name,const StringName& p_type) const {
  585. if (p_type==StringName()) {
  586. const int* constant = data.constant_override.getptr(p_name);
  587. if (constant)
  588. return *constant;
  589. }
  590. StringName type = p_type?p_type:get_type_name();
  591. // try with custom themes
  592. Control *theme_owner = data.theme_owner;
  593. while(theme_owner) {
  594. if (theme_owner->data.theme->has_constant(p_name, type ) )
  595. return data.theme_owner->data.theme->get_constant(p_name, type );
  596. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  597. if (parent)
  598. theme_owner=parent->data.theme_owner;
  599. else
  600. theme_owner=NULL;
  601. }
  602. return Theme::get_default()->get_constant( p_name, type );
  603. }
  604. bool Control::has_icon(const StringName& p_name,const StringName& p_type) const {
  605. if (p_type==StringName()) {
  606. const Ref<Texture>* tex = data.icon_override.getptr(p_name);
  607. if (tex)
  608. return true;
  609. }
  610. StringName type = p_type?p_type:get_type_name();
  611. // try with custom themes
  612. Control *theme_owner = data.theme_owner;
  613. while(theme_owner) {
  614. if (theme_owner->data.theme->has_icon(p_name, type ) )
  615. return true;
  616. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  617. if (parent)
  618. theme_owner=parent->data.theme_owner;
  619. else
  620. theme_owner=NULL;
  621. }
  622. return Theme::get_default()->has_icon( p_name, type );
  623. }
  624. bool Control::has_shader(const StringName &p_name, const StringName &p_type) const
  625. {
  626. if (p_type==StringName()) {
  627. const Ref<Shader>* sdr = data.shader_override.getptr(p_name);
  628. if (sdr)
  629. return true;
  630. }
  631. StringName type = p_type?p_type:get_type_name();
  632. // try with custom themes
  633. Control *theme_owner = data.theme_owner;
  634. while(theme_owner) {
  635. if (theme_owner->data.theme->has_shader(p_name, type))
  636. return true;
  637. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  638. if (parent)
  639. theme_owner=parent->data.theme_owner;
  640. else
  641. theme_owner=NULL;
  642. }
  643. return Theme::get_default()->has_shader( p_name, type );
  644. }
  645. bool Control::has_stylebox(const StringName& p_name,const StringName& p_type) const {
  646. if (p_type==StringName()) {
  647. const Ref<StyleBox>* style = data.style_override.getptr(p_name);
  648. if (style)
  649. return true;
  650. }
  651. StringName type = p_type?p_type:get_type_name();
  652. // try with custom themes
  653. Control *theme_owner = data.theme_owner;
  654. while(theme_owner) {
  655. if (theme_owner->data.theme->has_stylebox(p_name, type ) )
  656. return true;
  657. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  658. if (parent)
  659. theme_owner=parent->data.theme_owner;
  660. else
  661. theme_owner=NULL;
  662. }
  663. return Theme::get_default()->has_stylebox( p_name, type );
  664. }
  665. bool Control::has_font(const StringName& p_name,const StringName& p_type) const {
  666. if (p_type==StringName()) {
  667. const Ref<Font>* font = data.font_override.getptr(p_name);
  668. if (font)
  669. return true;
  670. }
  671. StringName type = p_type?p_type:get_type_name();
  672. // try with custom themes
  673. Control *theme_owner = data.theme_owner;
  674. while(theme_owner) {
  675. if (theme_owner->data.theme->has_font(p_name, type ) )
  676. return true;
  677. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  678. if (parent)
  679. theme_owner=parent->data.theme_owner;
  680. else
  681. theme_owner=NULL;
  682. }
  683. return Theme::get_default()->has_font( p_name, type );
  684. }
  685. bool Control::has_color(const StringName& p_name,const StringName& p_type) const {
  686. if (p_type==StringName()) {
  687. const Color* color = data.color_override.getptr(p_name);
  688. if (color)
  689. return true;
  690. }
  691. StringName type = p_type?p_type:get_type_name();
  692. // try with custom themes
  693. Control *theme_owner = data.theme_owner;
  694. while(theme_owner) {
  695. if (theme_owner->data.theme->has_color(p_name, type ) )
  696. return true;
  697. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  698. if (parent)
  699. theme_owner=parent->data.theme_owner;
  700. else
  701. theme_owner=NULL;
  702. }
  703. return Theme::get_default()->has_color( p_name, type );
  704. }
  705. bool Control::has_constant(const StringName& p_name,const StringName& p_type) const {
  706. if (p_type==StringName()) {
  707. const int* constant = data.constant_override.getptr(p_name);
  708. if (constant)
  709. return true;
  710. }
  711. StringName type = p_type?p_type:get_type_name();
  712. // try with custom themes
  713. Control *theme_owner = data.theme_owner;
  714. while(theme_owner) {
  715. if (theme_owner->data.theme->has_constant(p_name, type ) )
  716. return true;
  717. Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
  718. if (parent)
  719. theme_owner=parent->data.theme_owner;
  720. else
  721. theme_owner=NULL;
  722. }
  723. return Theme::get_default()->has_constant( p_name, type );
  724. }
  725. Size2 Control::get_parent_area_size() const {
  726. ERR_FAIL_COND_V(!is_inside_tree(),Size2());
  727. Size2 parent_size;
  728. if (data.parent_canvas_item) {
  729. parent_size=data.parent_canvas_item->get_item_rect().size;
  730. } else {
  731. parent_size=get_viewport()->get_visible_rect().size;
  732. }
  733. return parent_size;
  734. }
  735. void Control::_size_changed() {
  736. if (!is_inside_tree())
  737. return;
  738. Size2 parent_size = get_parent_area_size();
  739. float margin_pos[4];
  740. for(int i=0;i<4;i++) {
  741. float area = parent_size[i&1];
  742. switch(data.anchor[i]) {
  743. case ANCHOR_BEGIN: {
  744. margin_pos[i]=data.margin[i];
  745. } break;
  746. case ANCHOR_END: {
  747. margin_pos[i]=area-data.margin[i];
  748. } break;
  749. case ANCHOR_RATIO: {
  750. margin_pos[i]=area*data.margin[i];
  751. } break;
  752. case ANCHOR_CENTER: {
  753. margin_pos[i]=(area/2)-data.margin[i];
  754. } break;
  755. }
  756. }
  757. Point2 new_pos_cache=Point2(margin_pos[0],margin_pos[1]).floor();
  758. Size2 new_size_cache=Point2(margin_pos[2],margin_pos[3]).floor()-new_pos_cache;
  759. Size2 minimum_size=get_combined_minimum_size();
  760. new_size_cache.x = MAX( minimum_size.x, new_size_cache.x );
  761. new_size_cache.y = MAX( minimum_size.y, new_size_cache.y );
  762. if (new_pos_cache == data.pos_cache && new_size_cache == data.size_cache)
  763. return; // did not change, don't emit signal
  764. data.pos_cache=new_pos_cache;
  765. data.size_cache=new_size_cache;
  766. notification(NOTIFICATION_RESIZED);
  767. item_rect_changed();
  768. _change_notify_margins();
  769. _notify_transform();
  770. }
  771. float Control::_get_parent_range(int p_idx) const {
  772. if (!is_inside_tree()) {
  773. return 1.0;
  774. } if (data.parent_canvas_item) {
  775. return data.parent_canvas_item->get_item_rect().size[p_idx&1];
  776. } else {
  777. return get_viewport()->get_visible_rect().size[p_idx&1];
  778. }
  779. return 1.0;
  780. }
  781. float Control::_get_range(int p_idx) const {
  782. p_idx&=1;
  783. float parent_range = _get_parent_range( p_idx );
  784. float from = _a2s( data.margin[p_idx], data.anchor[p_idx], parent_range );
  785. float to = _a2s( data.margin[p_idx+2], data.anchor[p_idx+2], parent_range );
  786. return to-from;
  787. }
  788. float Control::_s2a(float p_val, AnchorType p_anchor,float p_range) const {
  789. switch(p_anchor) {
  790. case ANCHOR_BEGIN: {
  791. return p_val;
  792. } break;
  793. case ANCHOR_END: {
  794. return p_range-p_val;
  795. } break;
  796. case ANCHOR_RATIO: {
  797. return p_val/p_range;
  798. } break;
  799. case ANCHOR_CENTER: {
  800. return (p_range/2)-p_val;
  801. } break;
  802. }
  803. return 0;
  804. }
  805. float Control::_a2s(float p_val, AnchorType p_anchor,float p_range) const {
  806. switch(p_anchor) {
  807. case ANCHOR_BEGIN: {
  808. return Math::floor(p_val);
  809. } break;
  810. case ANCHOR_END: {
  811. return Math::floor(p_range-p_val);
  812. } break;
  813. case ANCHOR_RATIO: {
  814. return Math::floor(p_range*p_val);
  815. } break;
  816. case ANCHOR_CENTER: {
  817. return Math::floor((p_range/2)-p_val);
  818. } break;
  819. }
  820. return 0;
  821. }
  822. void Control::set_anchor(Margin p_margin,AnchorType p_anchor, bool p_keep_margin) {
  823. if (!is_inside_tree()) {
  824. data.anchor[p_margin] = p_anchor;
  825. } else if(!p_keep_margin) {
  826. float pr = _get_parent_range(p_margin);
  827. float s = _a2s( data.margin[p_margin], data.anchor[p_margin], pr );
  828. data.anchor[p_margin] = p_anchor;
  829. data.margin[p_margin] = _s2a( s, p_anchor, pr );
  830. } else {
  831. data.anchor[p_margin] = p_anchor;
  832. _size_changed();
  833. }
  834. _change_notify();
  835. }
  836. void Control::_set_anchor(Margin p_margin,AnchorType p_anchor) {
  837. #ifdef TOOLS_ENABLED
  838. SceneTree *st=OS::get_singleton()->get_main_loop()->cast_to<SceneTree>();
  839. if (st && st->is_editor_hint()) {
  840. set_anchor(p_margin, p_anchor, EDITOR_DEF("2d_editor/keep_margins_when_changing_anchors", false));
  841. } else {
  842. set_anchor(p_margin, p_anchor);
  843. }
  844. #else
  845. set_anchor(p_margin, p_anchor);
  846. #endif
  847. }
  848. void Control::set_anchor_and_margin(Margin p_margin,AnchorType p_anchor, float p_pos) {
  849. set_anchor(p_margin,p_anchor);
  850. set_margin(p_margin,p_pos);
  851. }
  852. Control::AnchorType Control::get_anchor(Margin p_margin) const {
  853. return data.anchor[p_margin];
  854. }
  855. void Control::_change_notify_margins() {
  856. // this avoids sending the whole object data again on a change
  857. _change_notify("margin/left");
  858. _change_notify("margin/top");
  859. _change_notify("margin/right");
  860. _change_notify("margin/bottom");
  861. _change_notify("rect/pos");
  862. _change_notify("rect/size");
  863. }
  864. void Control::set_margin(Margin p_margin,float p_value) {
  865. data.margin[p_margin]=p_value;
  866. _size_changed();
  867. }
  868. void Control::set_begin(const Size2& p_point) {
  869. data.margin[0]=p_point.x;
  870. data.margin[1]=p_point.y;
  871. _size_changed();
  872. }
  873. void Control::set_end(const Size2& p_point) {
  874. data.margin[2]=p_point.x;
  875. data.margin[3]=p_point.y;
  876. _size_changed();
  877. }
  878. float Control::get_margin(Margin p_margin) const {
  879. return data.margin[p_margin];
  880. }
  881. Size2 Control::get_begin() const {
  882. return Size2( data.margin[0], data.margin[1] );
  883. }
  884. Size2 Control::get_end() const {
  885. return Size2( data.margin[2], data.margin[3] );
  886. }
  887. Point2 Control::get_global_pos() const {
  888. return get_global_transform().get_origin();
  889. }
  890. void Control::set_global_pos(const Point2& p_point) {
  891. Matrix32 inv;
  892. if (data.parent_canvas_item) {
  893. inv = data.parent_canvas_item->get_global_transform().affine_inverse();
  894. }
  895. set_pos(inv.xform(p_point));
  896. }
  897. void Control::set_pos(const Size2& p_point) {
  898. float pw = _get_parent_range(0);
  899. float ph = _get_parent_range(1);
  900. float x = _a2s( data.margin[0], data.anchor[0], pw );
  901. float y = _a2s( data.margin[1], data.anchor[1], ph );
  902. float x2 = _a2s( data.margin[2], data.anchor[2], pw );
  903. float y2 = _a2s( data.margin[3], data.anchor[3], ph );
  904. Size2 ret = Size2(x2-x,y2-y);
  905. Size2 min = get_combined_minimum_size();
  906. Size2 size = Size2(MAX( min.width, ret.width),MAX( min.height, ret.height));
  907. float w=size.x;
  908. float h=size.y;
  909. x=p_point.x;
  910. y=p_point.y;
  911. data.margin[0] = _s2a( x, data.anchor[0], pw );
  912. data.margin[1] = _s2a( y, data.anchor[1], ph );
  913. data.margin[2] = _s2a( x+w, data.anchor[2], pw );
  914. data.margin[3] = _s2a( y+h, data.anchor[3], ph );
  915. _size_changed();
  916. }
  917. void Control::set_size(const Size2& p_size) {
  918. Size2 new_size=p_size;
  919. Size2 min=get_combined_minimum_size();
  920. if (new_size.x<min.x)
  921. new_size.x=min.x;
  922. if (new_size.y<min.y)
  923. new_size.y=min.y;
  924. float pw = _get_parent_range(0);
  925. float ph = _get_parent_range(1);
  926. float x = _a2s( data.margin[0], data.anchor[0], pw );
  927. float y = _a2s( data.margin[1], data.anchor[1], ph );
  928. float w=new_size.width;
  929. float h=new_size.height;
  930. data.margin[2] = _s2a( x+w, data.anchor[2], pw );
  931. data.margin[3] = _s2a( y+h, data.anchor[3], ph );
  932. _size_changed();
  933. }
  934. Size2 Control::get_pos() const {
  935. return data.pos_cache;
  936. }
  937. Size2 Control::get_size() const {
  938. return data.size_cache;
  939. }
  940. Rect2 Control::get_global_rect() const {
  941. return Rect2( get_global_pos(), get_size() );
  942. }
  943. Rect2 Control::get_window_rect() const {
  944. ERR_FAIL_COND_V(!is_inside_tree(),Rect2());
  945. Rect2 gr = get_global_rect();
  946. gr.pos+=get_viewport()->get_visible_rect().pos;
  947. return gr;
  948. }
  949. Rect2 Control::get_rect() const {
  950. return Rect2(get_pos(),get_size());
  951. }
  952. Rect2 Control::get_item_rect() const {
  953. return Rect2(Point2(),get_size());
  954. }
  955. void Control::set_area_as_parent_rect(int p_margin) {
  956. data.anchor[MARGIN_LEFT]=ANCHOR_BEGIN;
  957. data.anchor[MARGIN_TOP]=ANCHOR_BEGIN;
  958. data.anchor[MARGIN_RIGHT]=ANCHOR_END;
  959. data.anchor[MARGIN_BOTTOM]=ANCHOR_END;
  960. for(int i=0;i<4;i++)
  961. data.margin[i]=p_margin;
  962. _size_changed();
  963. }
  964. void Control::add_icon_override(const StringName& p_name, const Ref<Texture>& p_icon) {
  965. ERR_FAIL_COND(p_icon.is_null());
  966. data.icon_override[p_name]=p_icon;
  967. notification(NOTIFICATION_THEME_CHANGED);
  968. update();
  969. }
  970. void Control::add_shader_override(const StringName &p_name, const Ref<Shader> &p_shader) {
  971. ERR_FAIL_COND(p_shader.is_null());
  972. data.shader_override[p_name]=p_shader;
  973. notification(NOTIFICATION_THEME_CHANGED);
  974. update();
  975. }
  976. void Control::add_style_override(const StringName& p_name, const Ref<StyleBox>& p_style) {
  977. ERR_FAIL_COND(p_style.is_null());
  978. data.style_override[p_name]=p_style;
  979. notification(NOTIFICATION_THEME_CHANGED);
  980. update();
  981. }
  982. void Control::add_font_override(const StringName& p_name, const Ref<Font>& p_font) {
  983. ERR_FAIL_COND(p_font.is_null());
  984. data.font_override[p_name]=p_font;
  985. notification(NOTIFICATION_THEME_CHANGED);
  986. update();
  987. }
  988. void Control::add_color_override(const StringName& p_name, const Color& p_color) {
  989. data.color_override[p_name]=p_color;
  990. notification(NOTIFICATION_THEME_CHANGED);
  991. update();
  992. }
  993. void Control::add_constant_override(const StringName& p_name, int p_constant) {
  994. data.constant_override[p_name]=p_constant;
  995. notification(NOTIFICATION_THEME_CHANGED);
  996. update();
  997. }
  998. void Control::set_focus_mode(FocusMode p_focus_mode) {
  999. if (is_inside_tree() && p_focus_mode == FOCUS_NONE && data.focus_mode!=FOCUS_NONE && has_focus())
  1000. release_focus();
  1001. data.focus_mode=p_focus_mode;
  1002. }
  1003. static Control *_next_control(Control *p_from) {
  1004. if (p_from->is_set_as_toplevel())
  1005. return NULL; // can't go above
  1006. Control *parent = p_from->get_parent()?p_from->get_parent()->cast_to<Control>():NULL;
  1007. if (!parent) {
  1008. return NULL;
  1009. }
  1010. int next = p_from->get_position_in_parent();
  1011. ERR_FAIL_INDEX_V(next,parent->get_child_count(),NULL);
  1012. for(int i=(next+1);i<parent->get_child_count();i++) {
  1013. Control *c = parent->get_child(i)->cast_to<Control>();
  1014. if (!c || !c->is_visible() || c->is_set_as_toplevel())
  1015. continue;
  1016. return c;
  1017. }
  1018. //no next in parent, try the same in parent
  1019. return _next_control(parent);
  1020. }
  1021. Control *Control::find_next_valid_focus() const {
  1022. Control *from = const_cast<Control*>(this);
  1023. while(true) {
  1024. // find next child
  1025. Control *next_child=NULL;
  1026. for(int i=0;i<from->get_child_count();i++) {
  1027. Control *c = from->get_child(i)->cast_to<Control>();
  1028. if (!c || !c->is_visible() || c->is_set_as_toplevel()) {
  1029. continue;
  1030. }
  1031. next_child=c;
  1032. break;
  1033. }
  1034. if (next_child) {
  1035. from = next_child;
  1036. } else {
  1037. next_child=_next_control(from);
  1038. if (!next_child) { //nothing else.. go up and find either window or subwindow
  1039. next_child=const_cast<Control*>(this);
  1040. while(next_child && !next_child->is_set_as_toplevel()) {
  1041. if (next_child->get_parent()) {
  1042. next_child=next_child->get_parent()->cast_to<Control>();
  1043. } else
  1044. next_child=NULL;
  1045. }
  1046. if (!next_child) {
  1047. next_child=const_cast<Control*>(this);;
  1048. while(next_child) {
  1049. if (next_child->data.SI || next_child->data.RI)
  1050. break;
  1051. next_child=next_child->get_parent_control();
  1052. }
  1053. }
  1054. }
  1055. }
  1056. if (next_child==this) // no next control->
  1057. return (get_focus_mode()==FOCUS_ALL)?next_child:NULL;
  1058. if (next_child->get_focus_mode()==FOCUS_ALL)
  1059. return next_child;
  1060. from = next_child;
  1061. }
  1062. return NULL;
  1063. }
  1064. static Control *_prev_control(Control *p_from) {
  1065. Control *child=NULL;
  1066. for(int i=p_from->get_child_count()-1;i>=0;i--) {
  1067. Control *c = p_from->get_child(i)->cast_to<Control>();
  1068. if (!c || !c->is_visible() || c->is_set_as_toplevel())
  1069. continue;
  1070. child=c;
  1071. break;
  1072. }
  1073. if (!child)
  1074. return p_from;
  1075. //no prev in parent, try the same in parent
  1076. return _prev_control(child);
  1077. }
  1078. Control *Control::find_prev_valid_focus() const {
  1079. Control *from = const_cast<Control*>(this);
  1080. while(true) {
  1081. // find prev child
  1082. Control *prev_child = NULL;
  1083. if ( from->is_set_as_toplevel() || !from->get_parent() || !from->get_parent()->cast_to<Control>()) {
  1084. //find last of the childs
  1085. prev_child=_prev_control(from);
  1086. } else {
  1087. for(int i=(from->get_position_in_parent()-1);i>=0;i--) {
  1088. Control *c = from->get_parent()->get_child(i)->cast_to<Control>();
  1089. if (!c || !c->is_visible() || c->is_set_as_toplevel()) {
  1090. continue;
  1091. }
  1092. prev_child=c;
  1093. break;
  1094. }
  1095. if (!prev_child) {
  1096. prev_child = from->get_parent()->cast_to<Control>();
  1097. } else {
  1098. prev_child = _prev_control(prev_child);
  1099. }
  1100. }
  1101. if (prev_child==this) // no prev control->
  1102. return (get_focus_mode()==FOCUS_ALL)?prev_child:NULL;
  1103. if (prev_child->get_focus_mode()==FOCUS_ALL)
  1104. return prev_child;
  1105. from = prev_child;
  1106. }
  1107. return NULL;
  1108. return NULL;
  1109. }
  1110. Control::FocusMode Control::get_focus_mode() const {
  1111. return data.focus_mode;
  1112. }
  1113. bool Control::has_focus() const {
  1114. return is_inside_tree() && get_viewport()->_gui_control_has_focus(this);
  1115. }
  1116. void Control::grab_focus() {
  1117. if (!is_inside_tree()){
  1118. ERR_FAIL_COND(!is_inside_tree());
  1119. }
  1120. if (data.focus_mode==FOCUS_NONE)
  1121. return;
  1122. get_viewport()->_gui_control_grab_focus(this);
  1123. }
  1124. void Control::release_focus() {
  1125. ERR_FAIL_COND(!is_inside_tree());
  1126. if (!has_focus())
  1127. return;
  1128. get_viewport()->_gui_remove_focus();
  1129. update();
  1130. }
  1131. bool Control::is_toplevel_control() const {
  1132. return is_inside_tree() && (!data.parent_canvas_item && !data.RI && is_set_as_toplevel());
  1133. }
  1134. void Control::show_modal(bool p_exclusive) {
  1135. ERR_FAIL_COND(!is_inside_tree());
  1136. ERR_FAIL_COND(!data.SI);
  1137. if (is_visible())
  1138. hide();
  1139. ERR_FAIL_COND( data.MI!=NULL );
  1140. show();
  1141. raise();
  1142. data.modal_exclusive=p_exclusive;
  1143. data.MI=get_viewport()->_gui_show_modal(this);
  1144. }
  1145. void Control::_modal_set_prev_focus_owner(ObjectID p_prev) {
  1146. data.modal_prev_focus_owner=p_prev;
  1147. }
  1148. void Control::_modal_stack_remove() {
  1149. ERR_FAIL_COND(!is_inside_tree());
  1150. if (!data.MI)
  1151. return;
  1152. get_viewport()->_gui_remove_from_modal_stack(data.MI,data.modal_prev_focus_owner);
  1153. data.MI=NULL;
  1154. data.modal_prev_focus_owner=0;
  1155. }
  1156. void Control::_propagate_theme_changed(Control *p_owner) {
  1157. for(int i=0;i<get_child_count();i++) {
  1158. Control *child = get_child(i)->cast_to<Control>();
  1159. if (child && child->data.theme.is_null()) //has no theme, propagate
  1160. child->_propagate_theme_changed(p_owner);
  1161. }
  1162. data.theme_owner=p_owner;
  1163. _notification(NOTIFICATION_THEME_CHANGED);
  1164. update();
  1165. }
  1166. void Control::set_theme(const Ref<Theme>& p_theme) {
  1167. data.theme=p_theme;
  1168. if (!p_theme.is_null()) {
  1169. _propagate_theme_changed(this);
  1170. } else {
  1171. Control *parent = get_parent()?get_parent()->cast_to<Control>():NULL;
  1172. if (parent && parent->data.theme_owner) {
  1173. _propagate_theme_changed(parent->data.theme_owner);
  1174. } else {
  1175. _propagate_theme_changed(NULL);
  1176. }
  1177. }
  1178. }
  1179. void Control::accept_event() {
  1180. if (is_inside_tree())
  1181. get_viewport()->_gui_accept_event();
  1182. }
  1183. Ref<Theme> Control::get_theme() const {
  1184. return data.theme;
  1185. }
  1186. void Control::set_tooltip(const String& p_tooltip) {
  1187. data.tooltip=p_tooltip;
  1188. }
  1189. String Control::get_tooltip(const Point2& p_pos) const {
  1190. return data.tooltip;
  1191. }
  1192. void Control::set_default_cursor_shape(CursorShape p_shape) {
  1193. data.default_cursor=p_shape;
  1194. }
  1195. Control::CursorShape Control::get_default_cursor_shape() const {
  1196. return data.default_cursor;
  1197. }
  1198. Control::CursorShape Control::get_cursor_shape(const Point2& p_pos) const {
  1199. return data.default_cursor;
  1200. }
  1201. Matrix32 Control::get_transform() const {
  1202. Matrix32 xform=Matrix32(data.rotation,get_pos());
  1203. xform.scale_basis(data.scale);
  1204. return xform;
  1205. }
  1206. String Control::_get_tooltip() const {
  1207. return data.tooltip;
  1208. }
  1209. void Control::set_focus_neighbour(Margin p_margin, const NodePath &p_neighbour) {
  1210. ERR_FAIL_INDEX(p_margin,4);
  1211. data.focus_neighbour[p_margin]=p_neighbour;
  1212. }
  1213. NodePath Control::get_focus_neighbour(Margin p_margin) const {
  1214. ERR_FAIL_INDEX_V(p_margin,4,NodePath());
  1215. return data.focus_neighbour[p_margin];
  1216. }
  1217. #define MAX_NEIGHBOUR_SEARCH_COUNT 512
  1218. Control *Control::_get_focus_neighbour(Margin p_margin,int p_count) {
  1219. if (p_count >= MAX_NEIGHBOUR_SEARCH_COUNT)
  1220. return NULL;
  1221. if (!data.focus_neighbour[p_margin].is_empty()) {
  1222. Control *c=NULL;
  1223. Node * n = get_node(data.focus_neighbour[p_margin]);
  1224. if (n) {
  1225. c=n->cast_to<Control>();
  1226. if (!c) {
  1227. ERR_EXPLAIN("Next focus node is not a control: "+n->get_name());
  1228. ERR_FAIL_V(NULL);
  1229. }
  1230. } else {
  1231. return NULL;
  1232. }
  1233. bool valid=true;
  1234. if (c->is_hidden())
  1235. valid=false;
  1236. if (c->get_focus_mode()==FOCUS_NONE)
  1237. valid=false;
  1238. if (valid)
  1239. return c;
  1240. c=c->_get_focus_neighbour(p_margin,p_count+1);
  1241. return c;
  1242. }
  1243. float dist=1e7;
  1244. Control * result=NULL;
  1245. Point2 points[4];
  1246. Matrix32 xform = get_global_transform();
  1247. Rect2 rect = get_item_rect();
  1248. points[0]=xform.xform(rect.pos);
  1249. points[1]=xform.xform(rect.pos + Point2(rect.size.x, 0));
  1250. points[2]=xform.xform(rect.pos + rect.size);
  1251. points[3]=xform.xform(rect.pos + Point2(0, rect.size.y));
  1252. const Vector2 dir[4]={
  1253. Vector2(-1,0),
  1254. Vector2(0,-1),
  1255. Vector2(1,0),
  1256. Vector2(0,1)
  1257. };
  1258. Vector2 vdir=dir[p_margin];
  1259. float maxd=-1e7;
  1260. for(int i=0;i<4;i++) {
  1261. float d = vdir.dot(points[i]);
  1262. if (d>maxd)
  1263. maxd=d;
  1264. }
  1265. Node *base=this;
  1266. while (base) {
  1267. Control *c = base->cast_to<Control>();
  1268. if (c) {
  1269. if (c->data.SI)
  1270. break;
  1271. if (c->data.RI)
  1272. break;
  1273. }
  1274. base=base->get_parent();
  1275. }
  1276. if (!base)
  1277. return NULL;
  1278. _window_find_focus_neighbour(vdir,base,points,maxd,dist,&result);
  1279. return result;
  1280. }
  1281. 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) {
  1282. if (p_at->cast_to<Viewport>())
  1283. return; //bye
  1284. Control *c = p_at->cast_to<Control>();
  1285. if (c && c !=this && c->get_focus_mode()==FOCUS_ALL && c->is_visible()) {
  1286. Point2 points[4];
  1287. Matrix32 xform = c->get_global_transform();
  1288. Rect2 rect = c->get_item_rect();
  1289. points[0]=xform.xform(rect.pos);
  1290. points[1]=xform.xform(rect.pos + Point2(rect.size.x, 0));
  1291. points[2]=xform.xform(rect.pos + rect.size);
  1292. points[3]=xform.xform(rect.pos + Point2(0, rect.size.y));
  1293. float min=1e7;
  1294. for(int i=0;i<4;i++) {
  1295. float d = p_dir.dot(points[i]);
  1296. if (d < min)
  1297. min =d;
  1298. }
  1299. if (min>(p_min-CMP_EPSILON)) {
  1300. for(int i=0;i<4;i++) {
  1301. Vector2 la=p_points[i];
  1302. Vector2 lb=p_points[(i+1)%4];
  1303. for(int j=0;j<4;j++) {
  1304. Vector2 fa=points[j];
  1305. Vector2 fb=points[(j+1)%4];
  1306. Vector2 pa,pb;
  1307. float d=Geometry::get_closest_points_between_segments(la,lb,fa,fb,pa,pb);
  1308. //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));
  1309. if (d<r_closest_dist) {
  1310. r_closest_dist=d;
  1311. *r_closest=c;
  1312. }
  1313. }
  1314. }
  1315. }
  1316. }
  1317. for(int i=0;i<p_at->get_child_count();i++) {
  1318. Node *child=p_at->get_child(i);
  1319. Control *childc = child->cast_to<Control>();
  1320. if (childc && childc->data.SI)
  1321. continue; //subwindow, ignore
  1322. _window_find_focus_neighbour(p_dir,p_at->get_child(i),p_points,p_min,r_closest_dist,r_closest);
  1323. }
  1324. }
  1325. void Control::set_h_size_flags(int p_flags) {
  1326. if (data.h_size_flags==p_flags)
  1327. return;
  1328. data.h_size_flags=p_flags;
  1329. emit_signal(SceneStringNames::get_singleton()->size_flags_changed);
  1330. }
  1331. int Control::get_h_size_flags() const{
  1332. return data.h_size_flags;
  1333. }
  1334. void Control::set_v_size_flags(int p_flags) {
  1335. if (data.v_size_flags==p_flags)
  1336. return;
  1337. data.v_size_flags=p_flags;
  1338. emit_signal(SceneStringNames::get_singleton()->size_flags_changed);
  1339. }
  1340. void Control::set_stretch_ratio(float p_ratio) {
  1341. if (data.expand==p_ratio)
  1342. return;
  1343. data.expand=p_ratio;
  1344. emit_signal(SceneStringNames::get_singleton()->size_flags_changed);
  1345. }
  1346. float Control::get_stretch_ratio() const {
  1347. return data.expand;
  1348. }
  1349. void Control::grab_click_focus() {
  1350. ERR_FAIL_COND(!is_inside_tree());
  1351. get_viewport()->_gui_grab_click_focus(this);
  1352. }
  1353. void Control::minimum_size_changed() {
  1354. if (!is_inside_tree())
  1355. return;
  1356. if (data.pending_min_size_update)
  1357. return;
  1358. data.pending_min_size_update=true;
  1359. MessageQueue::get_singleton()->push_call(this,"_update_minimum_size");
  1360. if (!is_toplevel_control()) {
  1361. Control *pc = get_parent_control();
  1362. if (pc)
  1363. pc->minimum_size_changed();
  1364. }
  1365. }
  1366. int Control::get_v_size_flags() const{
  1367. return data.v_size_flags;
  1368. }
  1369. void Control::set_ignore_mouse(bool p_ignore) {
  1370. data.ignore_mouse=p_ignore;
  1371. }
  1372. bool Control::is_ignoring_mouse() const {
  1373. return data.ignore_mouse;
  1374. }
  1375. void Control::set_stop_mouse(bool p_stop) {
  1376. data.stop_mouse=p_stop;
  1377. }
  1378. bool Control::is_stopping_mouse() const {
  1379. return data.stop_mouse;
  1380. }
  1381. Control *Control::get_focus_owner() const {
  1382. ERR_FAIL_COND_V(!is_inside_tree(),NULL);
  1383. return get_viewport()->_gui_get_focus_owner();
  1384. }
  1385. void Control::warp_mouse(const Point2& p_to_pos) {
  1386. ERR_FAIL_COND(!is_inside_tree());
  1387. get_viewport()->warp_mouse(get_global_transform().xform(p_to_pos));
  1388. }
  1389. bool Control::is_text_field() const {
  1390. /*
  1391. if (get_script_instance()) {
  1392. Variant v=p_point;
  1393. const Variant *p[2]={&v,&p_data};
  1394. Variant::CallError ce;
  1395. Variant ret = get_script_instance()->call("is_text_field",p,2,ce);
  1396. if (ce.error==Variant::CallError::CALL_OK)
  1397. return ret;
  1398. }
  1399. */
  1400. return false;
  1401. }
  1402. void Control::_set_rotation_deg(float p_rot) {
  1403. set_rotation(Math::deg2rad(p_rot));
  1404. }
  1405. float Control::_get_rotation_deg() const {
  1406. return Math::rad2deg(get_rotation());
  1407. }
  1408. void Control::set_rotation(float p_rotation) {
  1409. data.rotation=p_rotation;
  1410. update();
  1411. _notify_transform();
  1412. }
  1413. float Control::get_rotation() const{
  1414. return data.rotation;
  1415. }
  1416. void Control::set_scale(const Vector2& p_scale){
  1417. data.scale=p_scale;
  1418. update();
  1419. _notify_transform();
  1420. }
  1421. Vector2 Control::get_scale() const{
  1422. return data.scale;
  1423. }
  1424. Control *Control::get_root_parent_control() const {
  1425. const CanvasItem *ci=this;
  1426. const Control *root=this;
  1427. while(ci) {
  1428. const Control *c = ci->cast_to<Control>();
  1429. if (c) {
  1430. root=c;
  1431. if (c->data.RI || c->data.MI || c->is_toplevel_control())
  1432. break;
  1433. }
  1434. ci=ci->get_parent_item();
  1435. }
  1436. return const_cast<Control*>(root);
  1437. }
  1438. void Control::_bind_methods() {
  1439. // ObjectTypeDB::bind_method(_MD("_window_resize_event"),&Control::_window_resize_event);
  1440. ObjectTypeDB::bind_method(_MD("_size_changed"),&Control::_size_changed);
  1441. ObjectTypeDB::bind_method(_MD("_update_minimum_size"),&Control::_update_minimum_size);
  1442. ObjectTypeDB::bind_method(_MD("accept_event"),&Control::accept_event);
  1443. ObjectTypeDB::bind_method(_MD("get_minimum_size"),&Control::get_minimum_size);
  1444. ObjectTypeDB::bind_method(_MD("get_combined_minimum_size"),&Control::get_combined_minimum_size);
  1445. ObjectTypeDB::bind_method(_MD("set_anchor","margin","anchor_mode","keep_margin"),&Control::set_anchor,DEFVAL(false));
  1446. ObjectTypeDB::bind_method(_MD("_set_anchor","margin","anchor_mode"),&Control::_set_anchor);
  1447. ObjectTypeDB::bind_method(_MD("get_anchor","margin"),&Control::get_anchor);
  1448. ObjectTypeDB::bind_method(_MD("set_margin","margin","offset"),&Control::set_margin);
  1449. ObjectTypeDB::bind_method(_MD("set_anchor_and_margin","margin","anchor_mode","offset"),&Control::set_anchor_and_margin);
  1450. ObjectTypeDB::bind_method(_MD("set_begin","pos"),&Control::set_begin);
  1451. ObjectTypeDB::bind_method(_MD("set_end","pos"),&Control::set_end);
  1452. ObjectTypeDB::bind_method(_MD("set_pos","pos"),&Control::set_pos);
  1453. ObjectTypeDB::bind_method(_MD("set_size","size"),&Control::set_size);
  1454. ObjectTypeDB::bind_method(_MD("set_custom_minimum_size","size"),&Control::set_custom_minimum_size);
  1455. ObjectTypeDB::bind_method(_MD("set_global_pos","pos"),&Control::set_global_pos);
  1456. ObjectTypeDB::bind_method(_MD("set_rotation","rotation"),&Control::set_rotation);
  1457. ObjectTypeDB::bind_method(_MD("_set_rotation_deg","rotation"),&Control::_set_rotation_deg);
  1458. ObjectTypeDB::bind_method(_MD("set_scale","scale"),&Control::set_scale);
  1459. ObjectTypeDB::bind_method(_MD("get_margin","margin"),&Control::get_margin);
  1460. ObjectTypeDB::bind_method(_MD("get_begin"),&Control::get_begin);
  1461. ObjectTypeDB::bind_method(_MD("get_end"),&Control::get_end);
  1462. ObjectTypeDB::bind_method(_MD("get_pos"),&Control::get_pos);
  1463. ObjectTypeDB::bind_method(_MD("get_size"),&Control::get_size);
  1464. ObjectTypeDB::bind_method(_MD("get_rotation"),&Control::get_rotation);
  1465. ObjectTypeDB::bind_method(_MD("get_scale"),&Control::get_scale);
  1466. ObjectTypeDB::bind_method(_MD("get_custom_minimum_size"),&Control::get_custom_minimum_size);
  1467. ObjectTypeDB::bind_method(_MD("get_parent_area_size"),&Control::get_size);
  1468. ObjectTypeDB::bind_method(_MD("get_global_pos"),&Control::get_global_pos);
  1469. ObjectTypeDB::bind_method(_MD("get_rect"),&Control::get_rect);
  1470. ObjectTypeDB::bind_method(_MD("_get_rotation_deg"),&Control::_get_rotation_deg);
  1471. ObjectTypeDB::bind_method(_MD("get_global_rect"),&Control::get_global_rect);
  1472. ObjectTypeDB::bind_method(_MD("set_area_as_parent_rect","margin"),&Control::set_area_as_parent_rect,DEFVAL(0));
  1473. ObjectTypeDB::bind_method(_MD("show_modal","exclusive"),&Control::show_modal,DEFVAL(false));
  1474. ObjectTypeDB::bind_method(_MD("set_focus_mode","mode"),&Control::set_focus_mode);
  1475. ObjectTypeDB::bind_method(_MD("has_focus"),&Control::has_focus);
  1476. ObjectTypeDB::bind_method(_MD("grab_focus"),&Control::grab_focus);
  1477. ObjectTypeDB::bind_method(_MD("release_focus"),&Control::release_focus);
  1478. ObjectTypeDB::bind_method(_MD("get_focus_owner:Control"),&Control::get_focus_owner);
  1479. ObjectTypeDB::bind_method(_MD("set_h_size_flags","flags"),&Control::set_h_size_flags);
  1480. ObjectTypeDB::bind_method(_MD("get_h_size_flags"),&Control::get_h_size_flags);
  1481. ObjectTypeDB::bind_method(_MD("set_stretch_ratio","ratio"),&Control::set_stretch_ratio);
  1482. ObjectTypeDB::bind_method(_MD("get_stretch_ratio"),&Control::get_stretch_ratio);
  1483. ObjectTypeDB::bind_method(_MD("set_v_size_flags","flags"),&Control::set_v_size_flags);
  1484. ObjectTypeDB::bind_method(_MD("get_v_size_flags"),&Control::get_v_size_flags);
  1485. ObjectTypeDB::bind_method(_MD("set_theme","theme:Theme"),&Control::set_theme);
  1486. ObjectTypeDB::bind_method(_MD("get_theme:Theme"),&Control::get_theme);
  1487. ObjectTypeDB::bind_method(_MD("add_icon_override","name","texture:Texture"),&Control::add_icon_override);
  1488. ObjectTypeDB::bind_method(_MD("add_shader_override","name","shader:Shader"),&Control::add_shader_override);
  1489. ObjectTypeDB::bind_method(_MD("add_style_override","name","stylebox:StyleBox"),&Control::add_style_override);
  1490. ObjectTypeDB::bind_method(_MD("add_font_override","name","font:Font"),&Control::add_font_override);
  1491. ObjectTypeDB::bind_method(_MD("add_color_override","name","color"),&Control::add_color_override);
  1492. ObjectTypeDB::bind_method(_MD("add_constant_override","name","constant"),&Control::add_constant_override);
  1493. ObjectTypeDB::bind_method(_MD("get_icon:Texture","name","type"),&Control::get_icon,DEFVAL(""));
  1494. ObjectTypeDB::bind_method(_MD("get_stylebox:StyleBox","name","type"),&Control::get_stylebox,DEFVAL(""));
  1495. ObjectTypeDB::bind_method(_MD("get_font:Font","name","type"),&Control::get_font,DEFVAL(""));
  1496. ObjectTypeDB::bind_method(_MD("get_color","name","type"),&Control::get_color,DEFVAL(""));
  1497. ObjectTypeDB::bind_method(_MD("get_constant","name","type"),&Control::get_constant,DEFVAL(""));
  1498. ObjectTypeDB::bind_method(_MD("get_parent_control:Control"),&Control::get_parent_control);
  1499. ObjectTypeDB::bind_method(_MD("set_tooltip","tooltip"),&Control::set_tooltip);
  1500. ObjectTypeDB::bind_method(_MD("get_tooltip","atpos"),&Control::get_tooltip,DEFVAL(Point2()));
  1501. ObjectTypeDB::bind_method(_MD("_get_tooltip"),&Control::_get_tooltip);
  1502. ObjectTypeDB::bind_method(_MD("set_default_cursor_shape","shape"),&Control::set_default_cursor_shape);
  1503. ObjectTypeDB::bind_method(_MD("get_default_cursor_shape"),&Control::get_default_cursor_shape);
  1504. ObjectTypeDB::bind_method(_MD("get_cursor_shape","pos"),&Control::get_cursor_shape,DEFVAL(Point2()));
  1505. ObjectTypeDB::bind_method(_MD("set_focus_neighbour","margin","neighbour"),&Control::set_focus_neighbour);
  1506. ObjectTypeDB::bind_method(_MD("get_focus_neighbour","margin"),&Control::get_focus_neighbour);
  1507. ObjectTypeDB::bind_method(_MD("set_ignore_mouse","ignore"),&Control::set_ignore_mouse);
  1508. ObjectTypeDB::bind_method(_MD("is_ignoring_mouse"),&Control::is_ignoring_mouse);
  1509. ObjectTypeDB::bind_method(_MD("force_drag","data","preview"),&Control::force_drag);
  1510. ObjectTypeDB::bind_method(_MD("set_stop_mouse","stop"),&Control::set_stop_mouse);
  1511. ObjectTypeDB::bind_method(_MD("is_stopping_mouse"),&Control::is_stopping_mouse);
  1512. ObjectTypeDB::bind_method(_MD("grab_click_focus"),&Control::grab_click_focus);
  1513. ObjectTypeDB::bind_method(_MD("set_drag_preview","control:Control"),&Control::set_drag_preview);
  1514. ObjectTypeDB::bind_method(_MD("warp_mouse","to_pos"),&Control::warp_mouse);
  1515. BIND_VMETHOD(MethodInfo("_input_event",PropertyInfo(Variant::INPUT_EVENT,"event")));
  1516. BIND_VMETHOD(MethodInfo(Variant::VECTOR2,"get_minimum_size"));
  1517. BIND_VMETHOD(MethodInfo(Variant::OBJECT,"get_drag_data",PropertyInfo(Variant::VECTOR2,"pos")));
  1518. BIND_VMETHOD(MethodInfo(Variant::BOOL,"can_drop_data",PropertyInfo(Variant::VECTOR2,"pos"),PropertyInfo(Variant::NIL,"data")));
  1519. BIND_VMETHOD(MethodInfo("drop_data",PropertyInfo(Variant::VECTOR2,"pos"),PropertyInfo(Variant::NIL,"data")));
  1520. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/left", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("_set_anchor"),_SCS("get_anchor"), MARGIN_LEFT );
  1521. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/top", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("_set_anchor"),_SCS("get_anchor"), MARGIN_TOP );
  1522. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/right", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("_set_anchor"),_SCS("get_anchor"), MARGIN_RIGHT );
  1523. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/bottom", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("_set_anchor"),_SCS("get_anchor"), MARGIN_BOTTOM );
  1524. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/left", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_LEFT );
  1525. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/top", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_TOP );
  1526. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/right", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_RIGHT );
  1527. ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/bottom", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_BOTTOM );
  1528. ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect/pos", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_EDITOR), _SCS("set_pos"),_SCS("get_pos") );
  1529. ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect/size", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_EDITOR), _SCS("set_size"),_SCS("get_size") );
  1530. ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect/min_size"), _SCS("set_custom_minimum_size"),_SCS("get_custom_minimum_size") );
  1531. ADD_PROPERTYNZ( PropertyInfo(Variant::REAL,"rect/rotation",PROPERTY_HINT_RANGE,"-1080,1080,0.01"), _SCS("_set_rotation_deg"),_SCS("_get_rotation_deg") );
  1532. ADD_PROPERTYNO( PropertyInfo(Variant::VECTOR2,"rect/scale"), _SCS("set_scale"),_SCS("get_scale") );
  1533. ADD_PROPERTYNZ( PropertyInfo(Variant::STRING,"hint/tooltip", PROPERTY_HINT_MULTILINE_TEXT), _SCS("set_tooltip"),_SCS("_get_tooltip") );
  1534. ADD_PROPERTYINZ( PropertyInfo(Variant::NODE_PATH,"focus_neighbour/left" ), _SCS("set_focus_neighbour"),_SCS("get_focus_neighbour"),MARGIN_LEFT );
  1535. ADD_PROPERTYINZ( PropertyInfo(Variant::NODE_PATH,"focus_neighbour/top" ), _SCS("set_focus_neighbour"),_SCS("get_focus_neighbour"),MARGIN_TOP );
  1536. ADD_PROPERTYINZ( PropertyInfo(Variant::NODE_PATH,"focus_neighbour/right" ), _SCS("set_focus_neighbour"),_SCS("get_focus_neighbour"),MARGIN_RIGHT );
  1537. ADD_PROPERTYINZ( PropertyInfo(Variant::NODE_PATH,"focus_neighbour/bottom" ), _SCS("set_focus_neighbour"),_SCS("get_focus_neighbour"),MARGIN_BOTTOM );
  1538. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"focus/ignore_mouse"), _SCS("set_ignore_mouse"),_SCS("is_ignoring_mouse") );
  1539. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"focus/stop_mouse"), _SCS("set_stop_mouse"),_SCS("is_stopping_mouse") );
  1540. ADD_PROPERTYNZ( PropertyInfo(Variant::INT,"size_flags/horizontal", PROPERTY_HINT_FLAGS, "Expand,Fill"), _SCS("set_h_size_flags"),_SCS("get_h_size_flags") );
  1541. ADD_PROPERTYNZ( PropertyInfo(Variant::INT,"size_flags/vertical", PROPERTY_HINT_FLAGS, "Expand,Fill"), _SCS("set_v_size_flags"),_SCS("get_v_size_flags") );
  1542. ADD_PROPERTYNO( PropertyInfo(Variant::INT,"size_flags/stretch_ratio", PROPERTY_HINT_RANGE, "1,128,0.01"), _SCS("set_stretch_ratio"),_SCS("get_stretch_ratio") );
  1543. ADD_PROPERTYNZ( PropertyInfo(Variant::OBJECT,"theme/theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), _SCS("set_theme"),_SCS("get_theme") );
  1544. BIND_CONSTANT( ANCHOR_BEGIN );
  1545. BIND_CONSTANT( ANCHOR_END );
  1546. BIND_CONSTANT( ANCHOR_RATIO );
  1547. BIND_CONSTANT( ANCHOR_CENTER );
  1548. BIND_CONSTANT( FOCUS_NONE );
  1549. BIND_CONSTANT( FOCUS_CLICK );
  1550. BIND_CONSTANT( FOCUS_ALL );
  1551. BIND_CONSTANT( NOTIFICATION_RESIZED );
  1552. BIND_CONSTANT( NOTIFICATION_MOUSE_ENTER );
  1553. BIND_CONSTANT( NOTIFICATION_MOUSE_EXIT );
  1554. BIND_CONSTANT( NOTIFICATION_FOCUS_ENTER );
  1555. BIND_CONSTANT( NOTIFICATION_FOCUS_EXIT );
  1556. BIND_CONSTANT( NOTIFICATION_THEME_CHANGED );
  1557. BIND_CONSTANT( NOTIFICATION_MODAL_CLOSE );
  1558. BIND_CONSTANT( CURSOR_ARROW );
  1559. BIND_CONSTANT( CURSOR_IBEAM );
  1560. BIND_CONSTANT( CURSOR_POINTING_HAND );
  1561. BIND_CONSTANT( CURSOR_CROSS );
  1562. BIND_CONSTANT( CURSOR_WAIT );
  1563. BIND_CONSTANT( CURSOR_BUSY );
  1564. BIND_CONSTANT( CURSOR_DRAG );
  1565. BIND_CONSTANT( CURSOR_CAN_DROP );
  1566. BIND_CONSTANT( CURSOR_FORBIDDEN );
  1567. BIND_CONSTANT( CURSOR_VSIZE );
  1568. BIND_CONSTANT( CURSOR_HSIZE );
  1569. BIND_CONSTANT( CURSOR_BDIAGSIZE );
  1570. BIND_CONSTANT( CURSOR_FDIAGSIZE );
  1571. BIND_CONSTANT( CURSOR_MOVE );
  1572. BIND_CONSTANT( CURSOR_VSPLIT );
  1573. BIND_CONSTANT( CURSOR_HSPLIT );
  1574. BIND_CONSTANT( CURSOR_HELP );
  1575. BIND_CONSTANT( SIZE_EXPAND );
  1576. BIND_CONSTANT( SIZE_FILL );
  1577. BIND_CONSTANT( SIZE_EXPAND_FILL );
  1578. ADD_SIGNAL( MethodInfo("resized") );
  1579. ADD_SIGNAL( MethodInfo("input_event",PropertyInfo(Variant::INPUT_EVENT,"ev")) );
  1580. ADD_SIGNAL( MethodInfo("mouse_enter") );
  1581. ADD_SIGNAL( MethodInfo("mouse_exit") );
  1582. ADD_SIGNAL( MethodInfo("focus_enter") );
  1583. ADD_SIGNAL( MethodInfo("focus_exit") );
  1584. ADD_SIGNAL( MethodInfo("size_flags_changed") );
  1585. ADD_SIGNAL( MethodInfo("minimum_size_changed") );
  1586. ADD_SIGNAL( MethodInfo("modal_close") );
  1587. }
  1588. Control::Control() {
  1589. data.parent=NULL;
  1590. data.ignore_mouse=false;
  1591. data.stop_mouse=true;
  1592. data.SI=NULL;
  1593. data.MI=NULL;
  1594. data.RI=NULL;
  1595. data.modal=false;
  1596. data.theme_owner=NULL;
  1597. data.modal_exclusive=false;
  1598. data.default_cursor = CURSOR_ARROW;
  1599. data.h_size_flags=SIZE_FILL;
  1600. data.v_size_flags=SIZE_FILL;
  1601. data.expand=1;
  1602. data.pending_min_size_update=false;
  1603. data.rotation=0;
  1604. data.parent_canvas_item=NULL;
  1605. data.scale=Vector2(1,1);
  1606. for (int i=0;i<4;i++) {
  1607. data.anchor[i]=ANCHOR_BEGIN;
  1608. data.margin[i]=0;
  1609. }
  1610. data.focus_mode=FOCUS_NONE;
  1611. data.modal_prev_focus_owner=0;
  1612. }
  1613. Control::~Control()
  1614. {
  1615. }