button.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*************************************************************************/
  2. /* button.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.h"
  30. #include "servers/visual_server.h"
  31. #include "print_string.h"
  32. #include "translation.h"
  33. Size2 Button::get_minimum_size() const {
  34. Size2 minsize=get_font("font")->get_string_size( xl_text );
  35. if (clip_text)
  36. minsize.width=0;
  37. Ref<Texture> _icon;
  38. if (icon.is_null() && has_icon("icon"))
  39. _icon=Control::get_icon("icon");
  40. else
  41. _icon=icon;
  42. if (!_icon.is_null()) {
  43. minsize.height=MAX( minsize.height, _icon->get_height() );
  44. minsize.width+=_icon->get_width();
  45. if (xl_text!="")
  46. minsize.width+=get_constant("hseparation");
  47. }
  48. return get_stylebox("normal" )->get_minimum_size() + minsize;
  49. }
  50. void Button::_notification(int p_what) {
  51. if (p_what==NOTIFICATION_TRANSLATION_CHANGED) {
  52. xl_text=XL_MESSAGE(text);
  53. minimum_size_changed();
  54. update();
  55. }
  56. if (p_what==NOTIFICATION_DRAW) {
  57. RID ci = get_canvas_item();
  58. Size2 size=get_size();
  59. Color color;
  60. //print_line(get_text()+": "+itos(is_flat())+" hover "+itos(get_draw_mode()));
  61. Ref<StyleBox> style = get_stylebox("normal");
  62. switch( get_draw_mode() ) {
  63. case DRAW_NORMAL: {
  64. style = get_stylebox("normal");
  65. if (!flat)
  66. style->draw( ci, Rect2(Point2(0,0), size) );
  67. color=get_color("font_color");
  68. } break;
  69. case DRAW_PRESSED: {
  70. style = get_stylebox("pressed");
  71. style->draw( ci, Rect2(Point2(0,0), size) );
  72. if (has_color("font_color_pressed"))
  73. color=get_color("font_color_pressed");
  74. else
  75. color=get_color("font_color");
  76. } break;
  77. case DRAW_HOVER: {
  78. style = get_stylebox("hover");
  79. style->draw( ci, Rect2(Point2(0,0), size) );
  80. color=get_color("font_color_hover");
  81. } break;
  82. case DRAW_DISABLED: {
  83. style = get_stylebox("disabled");
  84. style->draw( ci, Rect2(Point2(0,0), size) );
  85. color=get_color("font_color_disabled");
  86. } break;
  87. }
  88. if (has_focus()) {
  89. Ref<StyleBox> style = get_stylebox("focus");
  90. style->draw(ci,Rect2(Point2(),size));
  91. }
  92. Ref<Font> font=get_font("font");
  93. Ref<Texture> _icon;
  94. if (icon.is_null() && has_icon("icon"))
  95. _icon=Control::get_icon("icon");
  96. else
  97. _icon=icon;
  98. Point2 icon_ofs = (!_icon.is_null())?Point2( _icon->get_width() + get_constant("hseparation"), 0):Point2();
  99. int text_clip=size.width - style->get_minimum_size().width - icon_ofs.width;
  100. Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - font->get_string_size( xl_text ) )/2.0;
  101. switch(align) {
  102. case ALIGN_LEFT: {
  103. text_ofs.x = style->get_margin(MARGIN_LEFT) + icon_ofs.x;
  104. text_ofs.y+=style->get_offset().y;
  105. } break;
  106. case ALIGN_CENTER: {
  107. if (text_ofs.x<0)
  108. text_ofs.x=0;
  109. text_ofs+=icon_ofs;
  110. text_ofs+=style->get_offset();
  111. } break;
  112. case ALIGN_RIGHT: {
  113. text_ofs.x=size.x - style->get_margin(MARGIN_RIGHT) - font->get_string_size( xl_text ).x;
  114. text_ofs.y+=style->get_offset().y;
  115. } break;
  116. }
  117. text_ofs.y+=font->get_ascent();
  118. font->draw( ci, text_ofs.floor(), xl_text, color,clip_text?text_clip:-1);
  119. if (!_icon.is_null()) {
  120. int valign = size.height-style->get_minimum_size().y;
  121. _icon->draw(ci,style->get_offset()+Point2(0, Math::floor( (valign-_icon->get_height())/2.0 ) ),is_disabled()?Color(1,1,1,0.4):Color(1,1,1) );
  122. }
  123. }
  124. }
  125. void Button::set_text(const String& p_text) {
  126. if (text==p_text)
  127. return;
  128. text=p_text;
  129. xl_text=XL_MESSAGE(p_text);
  130. update();
  131. _change_notify("text");
  132. minimum_size_changed();
  133. }
  134. String Button::get_text() const {
  135. return text;
  136. }
  137. void Button::set_icon(const Ref<Texture>& p_icon) {
  138. if (icon==p_icon)
  139. return;
  140. icon=p_icon;
  141. update();
  142. _change_notify("icon");
  143. minimum_size_changed();
  144. }
  145. Ref<Texture> Button::get_icon() const {
  146. return icon;
  147. }
  148. void Button::set_flat(bool p_flat) {
  149. flat=p_flat;
  150. update();
  151. _change_notify("flat");
  152. }
  153. bool Button::is_flat() const {
  154. return flat;
  155. }
  156. void Button::set_clip_text(bool p_clip_text) {
  157. clip_text=p_clip_text;
  158. update();
  159. minimum_size_changed();
  160. }
  161. bool Button::get_clip_text() const {
  162. return clip_text;
  163. }
  164. void Button::set_text_align(TextAlign p_align) {
  165. align=p_align;
  166. update();
  167. }
  168. Button::TextAlign Button::get_text_align() const {
  169. return align;
  170. }
  171. void Button::_bind_methods() {
  172. ClassDB::bind_method(_MD("set_text","text"),&Button::set_text);
  173. ClassDB::bind_method(_MD("get_text"),&Button::get_text);
  174. ClassDB::bind_method(_MD("set_button_icon","texture:Texture"),&Button::set_icon);
  175. ClassDB::bind_method(_MD("get_button_icon:Texture"),&Button::get_icon);
  176. ClassDB::bind_method(_MD("set_flat","enabled"),&Button::set_flat);
  177. ClassDB::bind_method(_MD("set_clip_text","enabled"),&Button::set_clip_text);
  178. ClassDB::bind_method(_MD("get_clip_text"),&Button::get_clip_text);
  179. ClassDB::bind_method(_MD("set_text_align","align"),&Button::set_text_align);
  180. ClassDB::bind_method(_MD("get_text_align"),&Button::get_text_align);
  181. ClassDB::bind_method(_MD("is_flat"),&Button::is_flat);
  182. BIND_CONSTANT( ALIGN_LEFT );
  183. BIND_CONSTANT( ALIGN_CENTER );
  184. BIND_CONSTANT( ALIGN_RIGHT );
  185. ADD_PROPERTYNZ( PropertyInfo( Variant::STRING, "text", PROPERTY_HINT_NONE,"",PROPERTY_USAGE_DEFAULT_INTL ), "set_text","get_text") ;
  186. ADD_PROPERTYNZ( PropertyInfo( Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture" ), "set_button_icon","get_button_icon") ;
  187. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "flat" ), "set_flat","is_flat") ;
  188. ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "clip_text" ), "set_clip_text","get_clip_text") ;
  189. ADD_PROPERTYNO( PropertyInfo( Variant::INT, "align",PROPERTY_HINT_ENUM,"Left,Center,Right" ), "set_text_align","get_text_align") ;
  190. }
  191. Button::Button(const String &p_text) {
  192. flat=false;
  193. clip_text=false;
  194. set_mouse_filter(MOUSE_FILTER_STOP);
  195. set_text(p_text);
  196. align=ALIGN_CENTER;
  197. }
  198. Button::~Button()
  199. {
  200. }