button_array.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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-2014 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_slice("/",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_slice("/",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_slice("/",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_slice("/",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_READY:{
  138. MethodInfo mi;
  139. mi.name="mouse_sub_enter";
  140. add_user_signal(mi);
  141. }break;
  142. case NOTIFICATION_DRAW: {
  143. Size2 size=get_size();
  144. Size2 minsize=get_combined_minimum_size();
  145. Ref<StyleBox> style_normal = get_stylebox("normal");
  146. Ref<StyleBox> style_selected = get_stylebox("selected");
  147. Ref<StyleBox> style_focus = get_stylebox("focus");
  148. Ref<StyleBox> style_hover = get_stylebox("hover");
  149. Ref<Font> font_normal = get_font("font");
  150. Ref<Font> font_selected = get_font("font_selected");
  151. int icon_sep = get_constant("icon_separator");
  152. int button_sep = get_constant("button_separator");
  153. Color color_normal = get_color("font_color");
  154. Color color_selected = get_color("font_color_selected");
  155. int sep=button_sep;
  156. int ofs=0;
  157. int expand=0;
  158. switch(align) {
  159. case ALIGN_BEGIN: {
  160. ofs=0;
  161. } break;
  162. case ALIGN_CENTER: {
  163. ofs=Math::floor((size[orientation] - minsize[orientation])/2);
  164. } break;
  165. case ALIGN_END: {
  166. ofs=Math::floor((size[orientation] - minsize[orientation]));
  167. } break;
  168. case ALIGN_FILL: {
  169. if (buttons.size()>1)
  170. sep+=Math::floor((size[orientation]- minsize[orientation])/(buttons.size()-1.0));
  171. ofs=0;
  172. } break;
  173. case ALIGN_EXPAND_FILL: {
  174. ofs=0;
  175. expand=size[orientation] - minsize[orientation];
  176. } break;
  177. }
  178. int op_size = orientation==VERTICAL ? size.width : size.height;
  179. for(int i=0;i<buttons.size();i++) {
  180. int ms = buttons[i]._ms_cache;
  181. int s=ms;
  182. if (expand>0) {
  183. s+=expand/buttons.size();
  184. }
  185. if(min_button_size != -1 && s < min_button_size){
  186. s = min_button_size;
  187. }
  188. Rect2 r;
  189. r.pos[orientation]=ofs;
  190. r.pos[!orientation]=0;
  191. r.size[orientation]=s;
  192. r.size[!orientation]=op_size;
  193. Ref<Font> f;
  194. Color c;
  195. if (i==selected) {
  196. draw_style_box(style_selected,r);
  197. f=font_selected;
  198. c=color_selected;
  199. if (has_focus())
  200. draw_style_box(style_focus,r);
  201. } else {
  202. if (hover==i)
  203. draw_style_box(style_hover,r);
  204. else
  205. draw_style_box(style_normal,r);
  206. f=font_normal;
  207. c=color_normal;
  208. }
  209. Size2 ssize = f->get_string_size(buttons[i].text);
  210. if (buttons[i].icon.is_valid()) {
  211. ssize.x+=buttons[i].icon->get_width();
  212. }
  213. Point2 text_ofs=((r.size-ssize)/2.0+Point2(0,f->get_ascent())).floor();
  214. if (buttons[i].icon.is_valid()) {
  215. draw_texture(buttons[i].icon,r.pos+Point2(text_ofs.x,Math::floor((r.size.height-buttons[i].icon->get_height())/2.0)));
  216. text_ofs.x+=buttons[i].icon->get_width()+icon_sep;
  217. }
  218. draw_string(f,text_ofs+r.pos,buttons[i].text,c);
  219. buttons[i]._pos_cache=ofs;
  220. buttons[i]._size_cache=s;
  221. ofs+=s;
  222. ofs+=sep;
  223. }
  224. } break;
  225. }
  226. }
  227. void ButtonArray::_input_event(const InputEvent& p_event) {
  228. if (
  229. ( (orientation==HORIZONTAL && p_event.is_action("ui_left") ) ||
  230. (orientation==VERTICAL && p_event.is_action("ui_up") ) )
  231. && p_event.is_pressed() && selected>0) {
  232. set_selected(selected-1);
  233. accept_event();
  234. emit_signal("button_selected",selected);
  235. return;
  236. }
  237. if (
  238. ( (orientation==HORIZONTAL && p_event.is_action("ui_right") ) ||
  239. (orientation==VERTICAL && p_event.is_action("ui_down") ) )
  240. && p_event.is_pressed() && selected<(buttons.size()-1)) {
  241. set_selected(selected+1);
  242. accept_event();
  243. emit_signal("button_selected",selected);
  244. return;
  245. }
  246. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed && p_event.mouse_button.button_index==BUTTON_LEFT) {
  247. int ofs = orientation==HORIZONTAL ? p_event.mouse_button.x: p_event.mouse_button.y;
  248. for(int i=0;i<buttons.size();i++) {
  249. if (ofs>=buttons[i]._pos_cache && ofs<buttons[i]._pos_cache+buttons[i]._size_cache) {
  250. set_selected(i);
  251. emit_signal("button_selected",i);
  252. return;
  253. }
  254. }
  255. }
  256. if (p_event.type==InputEvent::MOUSE_MOTION) {
  257. int ofs = orientation==HORIZONTAL ? p_event.mouse_motion.x: p_event.mouse_motion.y;
  258. int new_hover=-1;
  259. for(int i=0;i<buttons.size();i++) {
  260. if (ofs>=buttons[i]._pos_cache && ofs<buttons[i]._pos_cache+buttons[i]._size_cache) {
  261. new_hover=i;
  262. break;
  263. }
  264. }
  265. if (new_hover!=hover) {
  266. hover=new_hover;
  267. emit_signal("mouse_sub_enter");
  268. update();
  269. }
  270. }
  271. }
  272. void ButtonArray::set_align(Align p_align) {
  273. align=p_align;
  274. update();
  275. }
  276. ButtonArray::Align ButtonArray::get_align() const {
  277. return align;
  278. }
  279. void ButtonArray::add_button(const String& p_button) {
  280. Button button;
  281. button.text=p_button;
  282. buttons.push_back(button);
  283. update();
  284. if (selected==-1)
  285. selected=0;
  286. minimum_size_changed();
  287. }
  288. void ButtonArray::add_icon_button(const Ref<Texture>& p_icon,const String& p_button) {
  289. Button button;
  290. button.text=p_button;
  291. button.icon=p_icon;
  292. buttons.push_back(button);
  293. if (selected==-1)
  294. selected=0;
  295. update();
  296. }
  297. void ButtonArray::set_button_text(int p_button, const String& p_text) {
  298. ERR_FAIL_INDEX(p_button,buttons.size());
  299. buttons[p_button].text=p_text;
  300. update();
  301. minimum_size_changed();
  302. }
  303. void ButtonArray::set_button_icon(int p_button, const Ref<Texture>& p_icon) {
  304. ERR_FAIL_INDEX(p_button,buttons.size());
  305. buttons[p_button].icon=p_icon;
  306. update();
  307. minimum_size_changed();
  308. }
  309. String ButtonArray::get_button_text(int p_button) const {
  310. ERR_FAIL_INDEX_V(p_button,buttons.size(),"");
  311. return buttons[p_button].text;
  312. }
  313. Ref<Texture> ButtonArray::get_button_icon(int p_button) const {
  314. ERR_FAIL_INDEX_V(p_button,buttons.size(),Ref<Texture>());
  315. return buttons[p_button].icon;
  316. }
  317. int ButtonArray::get_selected() const {
  318. return selected;
  319. }
  320. int ButtonArray::get_hovered() const {
  321. return hover;
  322. }
  323. void ButtonArray::set_selected(int p_selected) {
  324. ERR_FAIL_INDEX(p_selected,buttons.size());
  325. selected=p_selected;
  326. update();
  327. }
  328. void ButtonArray::erase_button(int p_button) {
  329. ERR_FAIL_INDEX(p_button,buttons.size());
  330. buttons.remove(p_button);
  331. if (p_button>=selected)
  332. selected--;
  333. if (selected<0)
  334. selected=0;
  335. if (selected>=buttons.size())
  336. selected=buttons.size()-1;
  337. update();
  338. }
  339. void ButtonArray::clear(){
  340. buttons.clear();
  341. selected=-1;
  342. update();
  343. }
  344. int ButtonArray::get_button_count() const {
  345. return buttons.size();
  346. }
  347. void ButtonArray::get_translatable_strings(List<String> *p_strings) const {
  348. for(int i=0;i<buttons.size();i++)
  349. p_strings->push_back(buttons[i].text);
  350. }
  351. void ButtonArray::_bind_methods() {
  352. ObjectTypeDB::bind_method(_MD("add_button","text"),&ButtonArray::add_button);
  353. ObjectTypeDB::bind_method(_MD("add_icon_button","icon","text"),&ButtonArray::add_icon_button,DEFVAL(""));
  354. ObjectTypeDB::bind_method(_MD("set_button_text","button","text"),&ButtonArray::set_button_text);
  355. ObjectTypeDB::bind_method(_MD("set_button_icon","button","icon"),&ButtonArray::set_button_icon);
  356. ObjectTypeDB::bind_method(_MD("get_button_text","button"),&ButtonArray::get_button_text);
  357. ObjectTypeDB::bind_method(_MD("get_button_icon","button"),&ButtonArray::get_button_icon);
  358. ObjectTypeDB::bind_method(_MD("get_button_count"),&ButtonArray::get_button_count);
  359. ObjectTypeDB::bind_method(_MD("get_selected"),&ButtonArray::get_selected);
  360. ObjectTypeDB::bind_method(_MD("get_hovered"),&ButtonArray::get_hovered);
  361. ObjectTypeDB::bind_method(_MD("set_selected","button"),&ButtonArray::set_selected);
  362. ObjectTypeDB::bind_method(_MD("erase_button","button"),&ButtonArray::erase_button);
  363. ObjectTypeDB::bind_method(_MD("clear"),&ButtonArray::clear);
  364. ObjectTypeDB::bind_method(_MD("_input_event"),&ButtonArray::_input_event);
  365. BIND_CONSTANT( ALIGN_BEGIN );
  366. BIND_CONSTANT( ALIGN_CENTER );
  367. BIND_CONSTANT( ALIGN_END );
  368. BIND_CONSTANT( ALIGN_FILL );
  369. BIND_CONSTANT( ALIGN_EXPAND_FILL );
  370. ADD_SIGNAL( MethodInfo("button_selected",PropertyInfo(Variant::INT,"button")));
  371. }
  372. ButtonArray::ButtonArray(Orientation p_orientation) {
  373. orientation=p_orientation;
  374. selected=-1;
  375. set_focus_mode(FOCUS_ALL);
  376. hover=-1;
  377. min_button_size = -1;
  378. }