texture_button.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*************************************************************************/
  2. /* texture_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 "texture_button.h"
  30. Size2 TextureButton::get_minimum_size() const {
  31. if (normal.is_null()) {
  32. if (pressed.is_null()) {
  33. if (hover.is_null())
  34. if (click_mask.is_null())
  35. return Size2();
  36. else
  37. return click_mask->get_size();
  38. else
  39. return hover->get_size();
  40. } else
  41. return pressed->get_size();
  42. } else
  43. return normal->get_size();
  44. }
  45. bool TextureButton::has_point(const Point2& p_point) const {
  46. if (click_mask.is_valid()) {
  47. Point2i p =p_point;
  48. if (p.x<0 || p.x>=click_mask->get_size().width || p.y<0 || p.y>=click_mask->get_size().height)
  49. return false;
  50. return click_mask->get_bit(p);
  51. }
  52. return Control::has_point(p_point);
  53. }
  54. void TextureButton::_notification(int p_what) {
  55. switch( p_what ) {
  56. case NOTIFICATION_DRAW: {
  57. RID canvas_item = get_canvas_item();
  58. DrawMode draw_mode = get_draw_mode();
  59. // if (normal.is_null())
  60. // break;
  61. switch (draw_mode) {
  62. case DRAW_NORMAL: {
  63. if (normal.is_valid())
  64. normal->draw(canvas_item,Point2());
  65. } break;
  66. case DRAW_PRESSED: {
  67. if (pressed.is_null()) {
  68. if (hover.is_null()) {
  69. if (normal.is_valid())
  70. normal->draw(canvas_item,Point2());
  71. } else
  72. hover->draw(canvas_item,Point2());
  73. } else
  74. pressed->draw(canvas_item,Point2());
  75. } break;
  76. case DRAW_HOVER: {
  77. if (hover.is_null()) {
  78. if (pressed.is_valid() && is_pressed())
  79. pressed->draw(canvas_item,Point2());
  80. else if (normal.is_valid())
  81. normal->draw(canvas_item,Point2());
  82. } else
  83. hover->draw(canvas_item,Point2());
  84. } break;
  85. case DRAW_DISABLED: {
  86. if (disabled.is_null()) {
  87. if (normal.is_valid())
  88. normal->draw(canvas_item,Point2());
  89. } else
  90. disabled->draw(canvas_item,Point2());
  91. } break;
  92. }
  93. if (has_focus() && focused.is_valid()) {
  94. focused->draw(canvas_item, Point2());
  95. };
  96. } break;
  97. }
  98. }
  99. void TextureButton::_bind_methods() {
  100. ObjectTypeDB::bind_method(_MD("set_normal_texture","texture:Texture"),&TextureButton::set_normal_texture);
  101. ObjectTypeDB::bind_method(_MD("set_pressed_texture","texture:Texture"),&TextureButton::set_pressed_texture);
  102. ObjectTypeDB::bind_method(_MD("set_hover_texture","texture:Texture"),&TextureButton::set_hover_texture);
  103. ObjectTypeDB::bind_method(_MD("set_disabled_texture","texture:Texture"),&TextureButton::set_disabled_texture);
  104. ObjectTypeDB::bind_method(_MD("set_focused_texture","texture:Texture"),&TextureButton::set_focused_texture);
  105. ObjectTypeDB::bind_method(_MD("set_click_mask","mask:BitMap"),&TextureButton::set_click_mask);
  106. ObjectTypeDB::bind_method(_MD("get_normal_texture:Texture"),&TextureButton::get_normal_texture);
  107. ObjectTypeDB::bind_method(_MD("get_pressed_texture:Texture"),&TextureButton::get_pressed_texture);
  108. ObjectTypeDB::bind_method(_MD("get_hover_texture:Texture"),&TextureButton::get_hover_texture);
  109. ObjectTypeDB::bind_method(_MD("get_disabled_texture:Texture"),&TextureButton::get_disabled_texture);
  110. ObjectTypeDB::bind_method(_MD("get_focused_texture:Texture"),&TextureButton::get_focused_texture);
  111. ObjectTypeDB::bind_method(_MD("get_click_mask:BitMap"),&TextureButton::get_click_mask);
  112. ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"textures/normal",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_normal_texture"), _SCS("get_normal_texture"));
  113. ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"textures/pressed",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_pressed_texture"), _SCS("get_pressed_texture"));
  114. ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"textures/hover",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_hover_texture"), _SCS("get_hover_texture"));
  115. ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"textures/disabled",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_disabled_texture"), _SCS("get_disabled_texture"));
  116. ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"textures/focused",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_focused_texture"), _SCS("get_focused_texture"));
  117. ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"textures/click_mask",PROPERTY_HINT_RESOURCE_TYPE,"BitMap"), _SCS("set_click_mask"), _SCS("get_click_mask")) ;
  118. }
  119. void TextureButton::set_normal_texture(const Ref<Texture>& p_normal) {
  120. normal=p_normal;
  121. update();
  122. minimum_size_changed();
  123. }
  124. void TextureButton::set_pressed_texture(const Ref<Texture>& p_pressed) {
  125. pressed=p_pressed;
  126. update();
  127. }
  128. void TextureButton::set_hover_texture(const Ref<Texture>& p_hover) {
  129. hover=p_hover;
  130. update();
  131. }
  132. void TextureButton::set_disabled_texture(const Ref<Texture>& p_disabled) {
  133. disabled=p_disabled;
  134. update();
  135. }
  136. void TextureButton::set_click_mask(const Ref<BitMap>& p_click_mask) {
  137. click_mask=p_click_mask;
  138. update();
  139. }
  140. Ref<Texture> TextureButton::get_normal_texture() const {
  141. return normal;
  142. }
  143. Ref<Texture> TextureButton::get_pressed_texture() const {
  144. return pressed;
  145. }
  146. Ref<Texture> TextureButton::get_hover_texture() const {
  147. return hover;
  148. }
  149. Ref<Texture> TextureButton::get_disabled_texture() const {
  150. return disabled;
  151. }
  152. Ref<BitMap> TextureButton::get_click_mask() const {
  153. return click_mask;
  154. }
  155. Ref<Texture> TextureButton::get_focused_texture() const {
  156. return focused;
  157. };
  158. void TextureButton::set_focused_texture(const Ref<Texture>& p_focused) {
  159. focused = p_focused;
  160. };
  161. TextureButton::TextureButton() {
  162. }