button_array.cpp 15 KB

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