control.cpp 61 KB

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