button_array.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*************************************************************************/
  2. /* button_array.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 "button_array.h"
  30. bool ButtonArray::_set(const StringName& p_name, const Variant& p_value) {
  31. String n=String(p_name);
  32. if (n.begins_with("button/")) {
  33. String what = n.get_slicec('/',1);
  34. if (what=="count") {
  35. int new_size=p_value;
  36. if (new_size>0 && buttons.size()==0) {
  37. selected=0;
  38. }
  39. if (new_size < buttons.size()) {
  40. if (selected>=new_size)
  41. selected=new_size-1;
  42. }
  43. buttons.resize(new_size);
  44. _change_notify();
  45. minimum_size_changed();
  46. } else if (what=="align") {
  47. set_align(Align(p_value.operator int()));
  48. } else if (what=="selected") {
  49. set_selected(p_value);
  50. } else if (what == "min_button_size") {
  51. min_button_size = p_value;
  52. } else {
  53. int idx=what.to_int();
  54. ERR_FAIL_INDEX_V(idx,buttons.size(),false);
  55. String f = n.get_slicec('/',2);
  56. if (f=="text")
  57. buttons[idx].text=p_value;
  58. else if (f=="icon")
  59. buttons[idx].icon=p_value;
  60. else
  61. return false;
  62. }
  63. update();
  64. return true;
  65. }
  66. return false;
  67. }
  68. bool ButtonArray::_get(const StringName& p_name,Variant &r_ret) const {
  69. String n=String(p_name);
  70. if (n.begins_with("button/")) {
  71. String what = n.get_slicec('/',1);
  72. if (what=="count") {
  73. r_ret=buttons.size();
  74. } else if (what=="align") {
  75. r_ret=get_align();
  76. } else if (what=="selected") {
  77. r_ret=get_selected();
  78. } else if (what == "min_button_size"){
  79. r_ret = min_button_size;
  80. } else {
  81. int idx=what.to_int();
  82. ERR_FAIL_INDEX_V(idx,buttons.size(),false);
  83. String f = n.get_slicec('/',2);
  84. if (f=="text")
  85. r_ret=buttons[idx].text;
  86. else if (f=="icon")
  87. r_ret=buttons[idx].icon;
  88. else
  89. return false;
  90. }
  91. return true;
  92. }
  93. return false;
  94. }
  95. void ButtonArray::_get_property_list( List<PropertyInfo> *p_list) const {
  96. p_list->push_back( PropertyInfo( Variant::INT, "button/count",PROPERTY_HINT_RANGE,"0,512,1"));
  97. p_list->push_back( PropertyInfo( Variant::INT, "button/min_button_size",PROPERTY_HINT_RANGE,"0,1024,1"));
  98. p_list->push_back( PropertyInfo( Variant::INT, "button/align",PROPERTY_HINT_ENUM,"Begin,Center,End,Fill,Expand"));
  99. for(int i=0;i<buttons.size();i++) {
  100. String base="button/"+itos(i)+"/";
  101. p_list->push_back( PropertyInfo( Variant::STRING, base+"text"));
  102. p_list->push_back( PropertyInfo( Variant::OBJECT, base+"icon",PROPERTY_HINT_RESOURCE_TYPE,"Texture"));
  103. }
  104. if (buttons.size()>0) {
  105. p_list->push_back( PropertyInfo( Variant::INT, "button/selected",PROPERTY_HINT_RANGE,"0,"+itos(buttons.size()-1)+",1"));
  106. }
  107. }
  108. Size2 ButtonArray::get_minimum_size() const {
  109. Ref<StyleBox> style_normal = get_stylebox("normal");
  110. Ref<StyleBox> style_selected = get_stylebox("selected");
  111. Ref<Font> font_normal = get_font("font");
  112. Ref<Font> font_selected = get_font("font_selected");
  113. int icon_sep = get_constant("icon_separator");
  114. int button_sep = get_constant("button_separator");
  115. Size2 minsize;
  116. for(int i=0;i<buttons.size();i++) {
  117. Ref<StyleBox> sb = i==selected ? style_selected : style_normal;
  118. Ref<Font> f = i==selected ? font_selected : font_normal;
  119. Size2 ms;
  120. ms = f->get_string_size(buttons[i].text);
  121. if (buttons[i].icon.is_valid()) {
  122. Size2 bs = buttons[i].icon->get_size();
  123. ms.height = MAX(ms.height,bs.height);
  124. ms.width+=bs.width+icon_sep;
  125. }
  126. ms+=sb->get_minimum_size();
  127. buttons[i]._ms_cache=ms[orientation];
  128. minsize[orientation]+=ms[orientation];
  129. if (i>0)
  130. minsize[orientation]+=button_sep;
  131. minsize[!orientation] = MAX(minsize[!orientation],ms[!orientation]);
  132. }
  133. return minsize;
  134. }
  135. void ButtonArray::_notification(int p_what) {
  136. switch(p_what) {
  137. case NOTIFICATION_MOUSE_EXIT:{
  138. hover=-1;
  139. update();
  140. }break;
  141. case NOTIFICATION_READY:{
  142. MethodInfo mi;
  143. mi.name="mouse_sub_enter";
  144. add_user_signal(mi);
  145. }break;
  146. case NOTIFICATION_DRAW: {
  147. Size2 size=get_size();
  148. Size2 minsize=get_combined_minimum_size();
  149. Ref<StyleBox> style_normal = get_stylebox("normal");
  150. Ref<StyleBox> style_selected = get_stylebox("selected");
  151. Ref<StyleBox> style_focus = get_stylebox("focus");
  152. Ref<StyleBox> style_hover = get_stylebox("hover");
  153. Ref<Font> font_normal = get_font("font");
  154. Ref<Font> font_selected = get_font("font_selected");
  155. int icon_sep = get_constant("icon_separator");
  156. int button_sep = get_constant("button_separator");
  157. Color color_normal = get_color("font_color");
  158. Color color_selected = get_color("font_color_selected");
  159. int sep=button_sep;
  160. int ofs=0;
  161. int expand=0;
  162. switch(align) {
  163. case ALIGN_BEGIN: {
  164. ofs=0;
  165. } break;
  166. case ALIGN_CENTER: {
  167. ofs=Math::floor((size[orientation] - minsize[orientation])/2);
  168. } break;
  169. case ALIGN_END: {
  170. ofs=Math::floor((size[orientation] - minsize[orientation]));
  171. } break;
  172. case ALIGN_FILL: {
  173. if (buttons.size()>1)
  174. sep+=Math::floor((size[orientation]- minsize[orientation])/(buttons.size()-1.0));
  175. ofs=0;
  176. } break;
  177. case ALIGN_EXPAND_FILL: {
  178. ofs=0;
  179. expand=size[orientation] - minsize[orientation];
  180. } break;
  181. }
  182. int op_size = orientation==VERTICAL ? size.width : size.height;
  183. for(int i=0;i<buttons.size();i++) {
  184. int ms = buttons[i]._ms_cache;
  185. int s=ms;
  186. if (expand>0) {
  187. s+=expand/buttons.size();
  188. }
  189. if(min_button_size != -1 && s < min_button_size){
  190. s = min_button_size;
  191. }
  192. Rect2 r;
  193. r.pos[orientation]=ofs;
  194. r.pos[!orientation]=0;
  195. r.size[orientation]=s;
  196. r.size[!orientation]=op_size;
  197. Ref<Font> f;
  198. Color c;
  199. Point2 sbsize;
  200. Point2 sbofs;
  201. if (i==selected) {
  202. draw_style_box(style_selected,r);
  203. sbsize=style_selected->get_minimum_size();
  204. sbofs=style_selected->get_offset();
  205. f=font_selected;
  206. c=color_selected;
  207. if (has_focus())
  208. draw_style_box(style_focus,r);
  209. } else {
  210. if (hover==i)
  211. draw_style_box(style_hover,r);
  212. else
  213. draw_style_box(style_normal,r);
  214. sbsize=style_selected->get_minimum_size();
  215. sbofs=style_normal->get_offset();
  216. f=font_normal;
  217. c=color_normal;
  218. }
  219. Size2 ssize = f->get_string_size(buttons[i].text);
  220. if (buttons[i].icon.is_valid()) {
  221. ssize.x+=buttons[i].icon->get_width();
  222. }
  223. Point2 text_ofs=((r.size-ssize-sbsize)/2.0+Point2(0,f->get_ascent())).floor()+sbofs;
  224. if (buttons[i].icon.is_valid()) {
  225. draw_texture(buttons[i].icon,r.pos+Point2(text_ofs.x,Math::floor((r.size.height-buttons[i].icon->get_height())/2.0)));
  226. text_ofs.x+=buttons[i].icon->get_width()+icon_sep;
  227. }
  228. draw_string(f,text_ofs+r.pos,buttons[i].text,c);
  229. buttons[i]._pos_cache=ofs;
  230. buttons[i]._size_cache=s;
  231. ofs+=s;
  232. ofs+=sep;
  233. }
  234. } break;
  235. }
  236. }
  237. void ButtonArray::_input_event(const InputEvent& p_event) {
  238. if (
  239. ( (orientation==HORIZONTAL && p_event.is_action("ui_left") ) ||
  240. (orientation==VERTICAL && p_event.is_action("ui_up") ) )
  241. && p_event.is_pressed() && selected>0) {
  242. set_selected(selected-1);
  243. accept_event();
  244. emit_signal("button_selected",selected);
  245. return;
  246. }
  247. if (
  248. ( (orientation==HORIZONTAL && p_event.is_action("ui_right") ) ||
  249. (orientation==VERTICAL && p_event.is_action("ui_down") ) )
  250. && p_event.is_pressed() && selected<(buttons.size()-1)) {
  251. set_selected(selected+1);
  252. accept_event();
  253. emit_signal("button_selected",selected);
  254. return;
  255. }
  256. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed && p_event.mouse_button.button_index==BUTTON_LEFT) {
  257. int ofs = orientation==HORIZONTAL ? p_event.mouse_button.x: p_event.mouse_button.y;
  258. for(int i=0;i<buttons.size();i++) {
  259. if (ofs>=buttons[i]._pos_cache && ofs<buttons[i]._pos_cache+buttons[i]._size_cache) {
  260. set_selected(i);
  261. emit_signal("button_selected",i);
  262. return;
  263. }
  264. }
  265. }
  266. if (p_event.type==InputEvent::MOUSE_MOTION) {
  267. int ofs = orientation==HORIZONTAL ? p_event.mouse_motion.x: p_event.mouse_motion.y;
  268. int new_hover=-1;
  269. for(int i=0;i<buttons.size();i++) {
  270. if (ofs>=buttons[i]._pos_cache && ofs<buttons[i]._pos_cache+buttons[i]._size_cache) {
  271. new_hover=i;
  272. break;
  273. }
  274. }
  275. if (new_hover!=hover) {
  276. hover=new_hover;
  277. emit_signal("mouse_sub_enter");
  278. update();
  279. }
  280. }
  281. }
  282. void ButtonArray::set_align(Align p_align) {
  283. align=p_align;
  284. update();
  285. }
  286. ButtonArray::Align ButtonArray::get_align() const {
  287. return align;
  288. }
  289. void ButtonArray::add_button(const String& p_text) {
  290. Button button;
  291. button.text=p_text;
  292. buttons.push_back(button);
  293. update();
  294. if (selected==-1)
  295. selected=0;
  296. minimum_size_changed();
  297. }
  298. void ButtonArray::add_icon_button(const Ref<Texture>& p_icon,const String& p_text) {
  299. Button button;
  300. button.text=p_text;
  301. button.icon=p_icon;
  302. buttons.push_back(button);
  303. if (selected==-1)
  304. selected=0;
  305. update();
  306. }
  307. void ButtonArray::set_button_text(int p_button, const String& p_text) {
  308. ERR_FAIL_INDEX(p_button,buttons.size());
  309. buttons[p_button].text=p_text;
  310. update();
  311. minimum_size_changed();
  312. }
  313. void ButtonArray::set_button_icon(int p_button, const Ref<Texture>& p_icon) {
  314. ERR_FAIL_INDEX(p_button,buttons.size());
  315. buttons[p_button].icon=p_icon;
  316. update();
  317. minimum_size_changed();
  318. }
  319. String ButtonArray::get_button_text(int p_button) const {
  320. ERR_FAIL_INDEX_V(p_button,buttons.size(),"");
  321. return buttons[p_button].text;
  322. }
  323. Ref<Texture> ButtonArray::get_button_icon(int p_button) const {
  324. ERR_FAIL_INDEX_V(p_button,buttons.size(),Ref<Texture>());
  325. return buttons[p_button].icon;
  326. }
  327. int ButtonArray::get_selected() const {
  328. return selected;
  329. }
  330. int ButtonArray::get_hovered() const {
  331. return hover;
  332. }
  333. void ButtonArray::set_selected(int p_selected) {
  334. ERR_FAIL_INDEX(p_selected,buttons.size());
  335. selected=p_selected;
  336. update();
  337. }
  338. void ButtonArray::erase_button(int p_button) {
  339. ERR_FAIL_INDEX(p_button,buttons.size());
  340. buttons.remove(p_button);
  341. if (p_button>=selected)
  342. selected--;
  343. if (selected<0)
  344. selected=0;
  345. if (selected>=buttons.size())
  346. selected=buttons.size()-1;
  347. update();
  348. }
  349. void ButtonArray::clear(){
  350. buttons.clear();
  351. selected=-1;
  352. update();
  353. }
  354. int ButtonArray::get_button_count() const {
  355. return buttons.size();
  356. }
  357. void ButtonArray::get_translatable_strings(List<String> *p_strings) const {
  358. for(int i=0;i<buttons.size();i++)
  359. p_strings->push_back(buttons[i].text);
  360. }
  361. void ButtonArray::_bind_methods() {
  362. ObjectTypeDB::bind_method(_MD("add_button","text"),&ButtonArray::add_button);
  363. ObjectTypeDB::bind_method(_MD("add_icon_button","icon:Texture","text"),&ButtonArray::add_icon_button,DEFVAL(""));
  364. ObjectTypeDB::bind_method(_MD("set_button_text","button_idx","text"),&ButtonArray::set_button_text);
  365. ObjectTypeDB::bind_method(_MD("set_button_icon","button_idx","icon:Texture"),&ButtonArray::set_button_icon);
  366. ObjectTypeDB::bind_method(_MD("get_button_text","button_idx"),&ButtonArray::get_button_text);
  367. ObjectTypeDB::bind_method(_MD("get_button_icon:Texture","button_idx"),&ButtonArray::get_button_icon);
  368. ObjectTypeDB::bind_method(_MD("get_button_count"),&ButtonArray::get_button_count);
  369. ObjectTypeDB::bind_method(_MD("get_selected"),&ButtonArray::get_selected);
  370. ObjectTypeDB::bind_method(_MD("get_hovered"),&ButtonArray::get_hovered);
  371. ObjectTypeDB::bind_method(_MD("set_selected","button_idx"),&ButtonArray::set_selected);
  372. ObjectTypeDB::bind_method(_MD("erase_button","button_idx"),&ButtonArray::erase_button);
  373. ObjectTypeDB::bind_method(_MD("clear"),&ButtonArray::clear);
  374. ObjectTypeDB::bind_method(_MD("_input_event"),&ButtonArray::_input_event);
  375. BIND_CONSTANT( ALIGN_BEGIN );
  376. BIND_CONSTANT( ALIGN_CENTER );
  377. BIND_CONSTANT( ALIGN_END );
  378. BIND_CONSTANT( ALIGN_FILL );
  379. BIND_CONSTANT( ALIGN_EXPAND_FILL );
  380. ADD_SIGNAL( MethodInfo("button_selected",PropertyInfo(Variant::INT,"button_idx")));
  381. }
  382. ButtonArray::ButtonArray(Orientation p_orientation) {
  383. orientation=p_orientation;
  384. selected=-1;
  385. set_focus_mode(FOCUS_ALL);
  386. hover=-1;
  387. min_button_size = -1;
  388. }