patch_9_frame.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*************************************************************************/
  2. /* patch_9_frame.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 "patch_9_frame.h"
  30. #include "servers/visual_server.h"
  31. void Patch9Frame::_notification(int p_what) {
  32. if (p_what==NOTIFICATION_DRAW) {
  33. if (texture.is_null())
  34. return;
  35. Size2 s=get_size();
  36. RID ci = get_canvas_item();
  37. VS::get_singleton()->canvas_item_add_nine_patch(ci,Rect2(Point2(),s),region_rect,texture->get_rid(),Vector2(margin[MARGIN_LEFT],margin[MARGIN_TOP]),Vector2(margin[MARGIN_RIGHT],margin[MARGIN_BOTTOM]),VS::NINE_PATCH_STRETCH,VS::NINE_PATCH_STRETCH,draw_center,modulate);
  38. // draw_texture_rect(texture,Rect2(Point2(),s),false,modulate);
  39. /*
  40. Vector<Point2> points;
  41. points.resize(4);
  42. points[0]=Point2(0,0);
  43. points[1]=Point2(s.x,0);
  44. points[2]=Point2(s.x,s.y);
  45. points[3]=Point2(0,s.y);
  46. Vector<Point2> uvs;
  47. uvs.resize(4);
  48. uvs[0]=Point2(0,0);
  49. uvs[1]=Point2(1,0);
  50. uvs[2]=Point2(1,1);
  51. uvs[3]=Point2(0,1);
  52. VisualServer::get_singleton()->canvas_item_add_primitive(ci,points,Vector<Color>(),uvs,texture->get_rid());
  53. */
  54. }
  55. }
  56. Size2 Patch9Frame::get_minimum_size() const {
  57. return Size2(margin[MARGIN_LEFT]+margin[MARGIN_RIGHT],margin[MARGIN_TOP]+margin[MARGIN_BOTTOM]);
  58. }
  59. void Patch9Frame::_bind_methods() {
  60. ClassDB::bind_method(_MD("set_texture","texture"), & Patch9Frame::set_texture );
  61. ClassDB::bind_method(_MD("get_texture"), & Patch9Frame::get_texture );
  62. ClassDB::bind_method(_MD("set_modulate","modulate"), & Patch9Frame::set_modulate );
  63. ClassDB::bind_method(_MD("get_modulate"), & Patch9Frame::get_modulate );
  64. ClassDB::bind_method(_MD("set_patch_margin","margin","value"), & Patch9Frame::set_patch_margin );
  65. ClassDB::bind_method(_MD("get_patch_margin","margin"), & Patch9Frame::get_patch_margin );
  66. ClassDB::bind_method(_MD("set_region_rect","rect"),&Patch9Frame::set_region_rect);
  67. ClassDB::bind_method(_MD("get_region_rect"),&Patch9Frame::get_region_rect);
  68. ClassDB::bind_method(_MD("set_draw_center","draw_center"), & Patch9Frame::set_draw_center );
  69. ClassDB::bind_method(_MD("get_draw_center"), & Patch9Frame::get_draw_center );
  70. ADD_SIGNAL(MethodInfo("texture_changed"));
  71. ADD_PROPERTYNZ( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), _SCS("set_texture"),_SCS("get_texture") );
  72. ADD_PROPERTYNO( PropertyInfo( Variant::COLOR, "modulate"), _SCS("set_modulate"),_SCS("get_modulate") );
  73. ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "draw_center"), _SCS("set_draw_center"),_SCS("get_draw_center") );
  74. ADD_PROPERTYNZ( PropertyInfo( Variant::RECT2, "region_rect"), _SCS("set_region_rect"),_SCS("get_region_rect"));
  75. ADD_GROUP("Patch Margin","patch_margin_");
  76. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_left",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_LEFT );
  77. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_top",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_TOP );
  78. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_right",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_RIGHT );
  79. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_bottom",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_BOTTOM );
  80. }
  81. void Patch9Frame::set_texture(const Ref<Texture>& p_tex) {
  82. if (texture==p_tex)
  83. return;
  84. texture=p_tex;
  85. update();
  86. //if (texture.is_valid())
  87. // texture->set_flags(texture->get_flags()&(~Texture::FLAG_REPEAT)); //remove repeat from texture, it looks bad in sprites
  88. minimum_size_changed();
  89. emit_signal("texture_changed");
  90. }
  91. Ref<Texture> Patch9Frame::get_texture() const {
  92. return texture;
  93. }
  94. void Patch9Frame::set_modulate(const Color& p_tex) {
  95. modulate=p_tex;
  96. update();
  97. }
  98. Color Patch9Frame::get_modulate() const{
  99. return modulate;
  100. }
  101. void Patch9Frame::set_patch_margin(Margin p_margin,int p_size) {
  102. ERR_FAIL_INDEX(p_margin,4);
  103. margin[p_margin]=p_size;
  104. update();
  105. minimum_size_changed();
  106. switch (p_margin) {
  107. case MARGIN_LEFT:
  108. _change_notify("patch_margin/left");
  109. break;
  110. case MARGIN_TOP:
  111. _change_notify("patch_margin/top");
  112. break;
  113. case MARGIN_RIGHT:
  114. _change_notify("patch_margin/right");
  115. break;
  116. case MARGIN_BOTTOM:
  117. _change_notify("patch_margin/bottom");
  118. break;
  119. }
  120. }
  121. int Patch9Frame::get_patch_margin(Margin p_margin) const{
  122. ERR_FAIL_INDEX_V(p_margin,4,0);
  123. return margin[p_margin];
  124. }
  125. void Patch9Frame::set_region_rect(const Rect2& p_region_rect) {
  126. if (region_rect==p_region_rect)
  127. return;
  128. region_rect=p_region_rect;
  129. item_rect_changed();
  130. _change_notify("region_rect");
  131. }
  132. Rect2 Patch9Frame::get_region_rect() const {
  133. return region_rect;
  134. }
  135. void Patch9Frame::set_draw_center(bool p_draw) {
  136. draw_center=p_draw;
  137. update();
  138. }
  139. bool Patch9Frame::get_draw_center() const{
  140. return draw_center;
  141. }
  142. Patch9Frame::Patch9Frame() {
  143. margin[MARGIN_LEFT]=0;
  144. margin[MARGIN_RIGHT]=0;
  145. margin[MARGIN_BOTTOM]=0;
  146. margin[MARGIN_TOP]=0;
  147. modulate=Color(1,1,1,1);
  148. set_ignore_mouse(true);
  149. draw_center=true;
  150. }
  151. Patch9Frame::~Patch9Frame()
  152. {
  153. }