button.cpp 7.5 KB

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