option_button.cpp 9.4 KB

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