option_button.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*************************************************************************/
  2. /* option_button.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 "option_button.h"
  30. #include "print_string.h"
  31. Size2 OptionButton::get_minimum_size() const {
  32. Size2 minsize = Button::get_minimum_size();
  33. if (has_icon("arrow"))
  34. minsize.width+=Control::get_icon("arrow")->get_width();
  35. return minsize;
  36. }
  37. void OptionButton::_notification(int p_what) {
  38. switch(p_what) {
  39. case NOTIFICATION_DRAW: {
  40. if (!has_icon("arrow"))
  41. return;
  42. RID ci = get_canvas_item();
  43. Ref<Texture> arrow = Control::get_icon("arrow");
  44. Ref<StyleBox> normal = get_stylebox("normal" );
  45. Size2 size = get_size();
  46. Point2 ofs( size.width - arrow->get_width() - get_constant("arrow_margin"), int(Math::abs((size.height-arrow->get_height())/2)));
  47. arrow->draw(ci,ofs);
  48. } break;
  49. }
  50. }
  51. void OptionButton::_selected(int p_which) {
  52. int selid = -1;
  53. for (int i=0;i<popup->get_item_count();i++) {
  54. bool is_clicked = popup->get_item_ID(i)==p_which;
  55. if (is_clicked) {
  56. selid=i;
  57. break;
  58. }
  59. }
  60. ERR_FAIL_COND(selid==-1);
  61. _select(selid,true);
  62. }
  63. void OptionButton::pressed() {
  64. Size2 size=get_size();
  65. popup->set_global_pos( get_global_pos() + Size2( 0, size.height ) );
  66. popup->set_size( Size2( size.width, 0) );
  67. popup->popup();
  68. }
  69. void OptionButton::add_icon_item(const Ref<Texture>& p_icon,const String& p_label,int p_ID) {
  70. popup->add_icon_check_item( p_icon, p_label, p_ID );
  71. if (popup->get_item_count()==1)
  72. select(0);
  73. }
  74. void OptionButton::add_item(const String& p_label,int p_ID) {
  75. popup->add_check_item( p_label, p_ID );
  76. if (popup->get_item_count()==1)
  77. select(0);
  78. }
  79. void OptionButton::set_item_text(int p_idx,const String& p_text) {
  80. popup->set_item_text(p_idx,p_text);
  81. }
  82. void OptionButton::set_item_icon(int p_idx,const Ref<Texture>& p_icon) {
  83. popup->set_item_icon(p_idx,p_icon);
  84. }
  85. void OptionButton::set_item_ID(int p_idx,int p_ID) {
  86. popup->set_item_ID(p_idx,p_ID);
  87. }
  88. void OptionButton::set_item_metadata(int p_idx,const Variant& p_metadata) {
  89. popup->set_item_metadata(p_idx,p_metadata);
  90. }
  91. void OptionButton::set_item_disabled(int p_idx,bool p_disabled) {
  92. popup->set_item_disabled(p_idx,p_disabled);
  93. }
  94. String OptionButton::get_item_text(int p_idx) const {
  95. return popup->get_item_text(p_idx);
  96. }
  97. Ref<Texture> OptionButton::get_item_icon(int p_idx) const {
  98. return popup->get_item_icon(p_idx);
  99. }
  100. int OptionButton::get_item_ID(int p_idx) const {
  101. return popup->get_item_ID(p_idx);
  102. }
  103. Variant OptionButton::get_item_metadata(int p_idx) const {
  104. return popup->get_item_metadata(p_idx);
  105. }
  106. bool OptionButton::is_item_disabled(int p_idx) const {
  107. return popup->is_item_disabled(p_idx);
  108. }
  109. int OptionButton::get_item_count() const {
  110. return popup->get_item_count();
  111. }
  112. void OptionButton::add_separator() {
  113. popup->add_separator();
  114. }
  115. void OptionButton::clear() {
  116. popup->clear();
  117. set_text("");
  118. current=-1;
  119. }
  120. void OptionButton::_select(int p_idx,bool p_emit) {
  121. if (p_idx<0)
  122. return;
  123. if (p_idx==current)
  124. return;
  125. ERR_FAIL_INDEX( p_idx, popup->get_item_count() );
  126. for (int i=0;i<popup->get_item_count();i++) {
  127. popup->set_item_checked(i,i==p_idx);
  128. }
  129. current=p_idx;
  130. set_text( popup->get_item_text( current ) );
  131. set_icon( popup->get_item_icon( current ) );
  132. if (is_inside_scene() && p_emit)
  133. emit_signal("item_selected",current);
  134. }
  135. void OptionButton::_select_int(int p_which) {
  136. if (p_which<0 || p_which>=popup->get_item_count())
  137. return;
  138. _select(p_which,false);
  139. }
  140. void OptionButton::select(int p_idx) {
  141. _select(p_idx,false);
  142. }
  143. int OptionButton::get_selected() const {
  144. return current;
  145. }
  146. int OptionButton::get_selected_ID() const {
  147. int idx = get_selected();
  148. if (idx<0)
  149. return 0;
  150. return get_item_ID(current);
  151. }
  152. Variant OptionButton::get_selected_metadata() const {
  153. int idx = get_selected();
  154. if (idx<0)
  155. return Variant();
  156. return get_item_metadata(current);
  157. }
  158. void OptionButton::remove_item(int p_idx) {
  159. popup->remove_item(p_idx);
  160. }
  161. Array OptionButton::_get_items() const {
  162. Array items;
  163. for(int i=0;i<get_item_count();i++) {
  164. items.push_back(get_item_text(i));
  165. items.push_back(get_item_icon(i));
  166. items.push_back(is_item_disabled(i));
  167. items.push_back(get_item_ID(i));
  168. items.push_back(get_item_metadata(i));
  169. }
  170. return items;
  171. }
  172. void OptionButton::_set_items(const Array& p_items){
  173. ERR_FAIL_COND(p_items.size() % 5);
  174. clear();
  175. for(int i=0;i<p_items.size();i+=5) {
  176. String text=p_items[i+0];
  177. Ref<Texture> icon=p_items[i+1];
  178. bool disabled=p_items[i+2];
  179. int id=p_items[i+3];
  180. Variant meta = p_items[i+4];
  181. int idx=get_item_count();
  182. add_item(text,id);
  183. set_item_icon(idx,icon);
  184. set_item_disabled(idx,disabled);
  185. set_item_metadata(idx,meta);
  186. }
  187. }
  188. void OptionButton::get_translatable_strings(List<String> *p_strings) const {
  189. return popup->get_translatable_strings(p_strings);
  190. }
  191. void OptionButton::_bind_methods() {
  192. ObjectTypeDB::bind_method(_MD("_selected"),&OptionButton::_selected);
  193. ObjectTypeDB::bind_method(_MD("add_item","label","id"),&OptionButton::add_item,DEFVAL(-1));
  194. ObjectTypeDB::bind_method(_MD("add_icon_item","texture:Texture","label","id"),&OptionButton::add_icon_item);
  195. ObjectTypeDB::bind_method(_MD("set_item_text","idx","text"),&OptionButton::set_item_text);
  196. ObjectTypeDB::bind_method(_MD("set_item_icon","idx","texture:Texture"),&OptionButton::set_item_icon);
  197. ObjectTypeDB::bind_method(_MD("set_item_disabled","idx","disabled"),&OptionButton::set_item_disabled);
  198. ObjectTypeDB::bind_method(_MD("set_item_ID","idx","id"),&OptionButton::set_item_ID);
  199. ObjectTypeDB::bind_method(_MD("set_item_metadata","idx","metadata"),&OptionButton::set_item_metadata);
  200. ObjectTypeDB::bind_method(_MD("get_item_text","idx"),&OptionButton::get_item_text);
  201. ObjectTypeDB::bind_method(_MD("get_item_icon:Texture","idx"),&OptionButton::get_item_icon);
  202. ObjectTypeDB::bind_method(_MD("get_item_ID","idx"),&OptionButton::get_item_ID);
  203. ObjectTypeDB::bind_method(_MD("get_item_metadata","idx"),&OptionButton::get_item_metadata);
  204. ObjectTypeDB::bind_method(_MD("is_item_disabled","idx"),&OptionButton::is_item_disabled);
  205. ObjectTypeDB::bind_method(_MD("get_item_count"),&OptionButton::get_item_count);
  206. ObjectTypeDB::bind_method(_MD("add_separator"),&OptionButton::add_separator);
  207. ObjectTypeDB::bind_method(_MD("clear"),&OptionButton::clear);
  208. ObjectTypeDB::bind_method(_MD("select"),&OptionButton::select);
  209. ObjectTypeDB::bind_method(_MD("get_selected"),&OptionButton::get_selected);
  210. ObjectTypeDB::bind_method(_MD("get_selected_ID"),&OptionButton::get_selected_ID);
  211. ObjectTypeDB::bind_method(_MD("get_selected_metadata"),&OptionButton::get_selected_metadata);
  212. ObjectTypeDB::bind_method(_MD("remove_item","idx"),&OptionButton::remove_item);
  213. ObjectTypeDB::bind_method(_MD("_select_int"),&OptionButton::_select_int);
  214. ObjectTypeDB::bind_method(_MD("_set_items"),&OptionButton::_set_items);
  215. ObjectTypeDB::bind_method(_MD("_get_items"),&OptionButton::_get_items);
  216. ADD_PROPERTY( PropertyInfo(Variant::INT,"selected"), _SCS("_select_int"),_SCS("get_selected") );
  217. ADD_PROPERTY( PropertyInfo(Variant::ARRAY,"items",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), _SCS("_set_items"),_SCS("_get_items") );
  218. ADD_SIGNAL( MethodInfo("item_selected", PropertyInfo( Variant::INT,"ID") ) );
  219. }
  220. OptionButton::OptionButton() {
  221. popup = memnew( PopupMenu );
  222. popup->hide();
  223. popup->set_as_toplevel(true);
  224. add_child(popup);
  225. popup->connect("item_pressed", this,"_selected");
  226. current=-1;
  227. set_text_align(ALIGN_LEFT);
  228. }
  229. OptionButton::~OptionButton() {
  230. }