button.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*************************************************************************/
  2. /* 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 "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. Ref<StyleBox> style = get_stylebox("normal" );
  79. Ref<Font> font=get_font("font");
  80. Ref<Texture> _icon;
  81. if (icon.is_null() && has_icon("icon"))
  82. _icon=Control::get_icon("icon");
  83. else
  84. _icon=icon;
  85. Point2 icon_ofs = (!_icon.is_null())?Point2( _icon->get_width() + get_constant("hseparation"), 0):Point2();
  86. int text_clip=size.width - style->get_minimum_size().width - icon_ofs.width;
  87. Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - font->get_string_size( text ) )/2.0;
  88. switch(align) {
  89. case ALIGN_LEFT: {
  90. text_ofs.x = style->get_margin(MARGIN_LEFT) + icon_ofs.x;
  91. text_ofs.y+=style->get_offset().y;
  92. } break;
  93. case ALIGN_CENTER: {
  94. text_ofs+=icon_ofs;
  95. text_ofs+=style->get_offset();
  96. } break;
  97. case ALIGN_RIGHT: {
  98. text_ofs.x=size.x - style->get_margin(MARGIN_RIGHT) - font->get_string_size( text ).x;
  99. text_ofs.y+=style->get_offset().y;
  100. } break;
  101. }
  102. text_ofs.y+=font->get_ascent();
  103. font->draw( ci, text_ofs.floor(), text, color,clip_text?text_clip:-1);
  104. if (!_icon.is_null()) {
  105. _icon->draw(ci,Point2(style->get_offset().x, Math::floor( (size.height-_icon->get_height())/2.0 ) ),is_disabled()?Color(1,1,1,0.4):Color(1,1,1) );
  106. }
  107. if (has_focus()) {
  108. Ref<StyleBox> style = get_stylebox("focus");
  109. style->draw(ci,Rect2(Point2(),size));
  110. }
  111. }
  112. }
  113. void Button::set_text(const String& p_text) {
  114. if (text==p_text)
  115. return;
  116. text=XL_MESSAGE(p_text);
  117. update();
  118. _change_notify("text");
  119. minimum_size_changed();
  120. }
  121. String Button::get_text() const {
  122. return text;
  123. }
  124. void Button::set_icon(const Ref<Texture>& p_icon) {
  125. if (icon==p_icon)
  126. return;
  127. icon=p_icon;
  128. update();
  129. _change_notify("icon");
  130. minimum_size_changed();
  131. }
  132. Ref<Texture> Button::get_icon() const {
  133. return icon;
  134. }
  135. void Button::set_flat(bool p_flat) {
  136. flat=p_flat;
  137. update();
  138. _change_notify("flat");
  139. }
  140. bool Button::is_flat() const {
  141. return flat;
  142. }
  143. void Button::set_clip_text(bool p_clip_text) {
  144. clip_text=p_clip_text;
  145. update();
  146. minimum_size_changed();
  147. }
  148. bool Button::get_clip_text() const {
  149. return clip_text;
  150. }
  151. void Button::set_text_align(TextAlign p_align) {
  152. align=p_align;
  153. update();
  154. }
  155. Button::TextAlign Button::get_text_align() const {
  156. return align;
  157. }
  158. void Button::_bind_methods() {
  159. ObjectTypeDB::bind_method(_MD("set_text","text"),&Button::set_text);
  160. ObjectTypeDB::bind_method(_MD("get_text"),&Button::get_text);
  161. ObjectTypeDB::bind_method(_MD("set_button_icon","texture:Texture"),&Button::set_icon);
  162. ObjectTypeDB::bind_method(_MD("get_button_icon:Texture"),&Button::get_icon);
  163. ObjectTypeDB::bind_method(_MD("set_flat","enabled"),&Button::set_flat);
  164. ObjectTypeDB::bind_method(_MD("set_clip_text","enabled"),&Button::set_clip_text);
  165. ObjectTypeDB::bind_method(_MD("get_clip_text"),&Button::get_clip_text);
  166. ObjectTypeDB::bind_method(_MD("set_text_align","align"),&Button::set_text_align);
  167. ObjectTypeDB::bind_method(_MD("get_text_align"),&Button::get_text_align);
  168. ObjectTypeDB::bind_method(_MD("is_flat"),&Button::is_flat);
  169. ADD_PROPERTY( PropertyInfo( Variant::STRING, "text", PROPERTY_HINT_NONE,"",PROPERTY_USAGE_DEFAULT_INTL ), _SCS("set_text"),_SCS("get_text") );
  170. ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture" ), _SCS("set_button_icon"),_SCS("get_button_icon") );
  171. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "flat" ), _SCS("set_flat"),_SCS("is_flat") );
  172. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "clip_text" ), _SCS("set_clip_text"),_SCS("get_clip_text") );
  173. ADD_PROPERTY( PropertyInfo( Variant::INT, "align",PROPERTY_HINT_ENUM,"Left,Center,Right" ), _SCS("set_text_align"),_SCS("get_text_align") );
  174. }
  175. Button::Button(const String &p_text) {
  176. flat=false;
  177. clip_text=false;
  178. set_stop_mouse(true);
  179. set_text(p_text);
  180. align=ALIGN_CENTER;
  181. }
  182. Button::~Button()
  183. {
  184. }