patch_9_rect.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*************************************************************************/
  2. /* patch_9_rect.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_rect.h"
  30. #include "servers/visual_server.h"
  31. void NinePatchRect::_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);
  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 NinePatchRect::get_minimum_size() const {
  57. return Size2(margin[MARGIN_LEFT]+margin[MARGIN_RIGHT],margin[MARGIN_TOP]+margin[MARGIN_BOTTOM]);
  58. }
  59. void NinePatchRect::_bind_methods() {
  60. ClassDB::bind_method(_MD("set_texture","texture"), & NinePatchRect::set_texture );
  61. ClassDB::bind_method(_MD("get_texture"), & NinePatchRect::get_texture );
  62. ClassDB::bind_method(_MD("set_patch_margin","margin","value"), & NinePatchRect::set_patch_margin );
  63. ClassDB::bind_method(_MD("get_patch_margin","margin"), & NinePatchRect::get_patch_margin );
  64. ClassDB::bind_method(_MD("set_region_rect","rect"),&NinePatchRect::set_region_rect);
  65. ClassDB::bind_method(_MD("get_region_rect"),&NinePatchRect::get_region_rect);
  66. ClassDB::bind_method(_MD("set_draw_center","draw_center"), & NinePatchRect::set_draw_center );
  67. ClassDB::bind_method(_MD("get_draw_center"), & NinePatchRect::get_draw_center );
  68. ADD_SIGNAL(MethodInfo("texture_changed"));
  69. ADD_PROPERTYNZ( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture","get_texture") ;
  70. ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "draw_center"), "set_draw_center","get_draw_center") ;
  71. ADD_PROPERTYNZ( PropertyInfo( Variant::RECT2, "region_rect"), "set_region_rect","get_region_rect");
  72. ADD_GROUP("Patch Margin","patch_margin_");
  73. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_left",PROPERTY_HINT_RANGE,"0,16384,1"), "set_patch_margin","get_patch_margin",MARGIN_LEFT );
  74. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_top",PROPERTY_HINT_RANGE,"0,16384,1"), "set_patch_margin","get_patch_margin",MARGIN_TOP );
  75. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_right",PROPERTY_HINT_RANGE,"0,16384,1"), "set_patch_margin","get_patch_margin",MARGIN_RIGHT );
  76. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_bottom",PROPERTY_HINT_RANGE,"0,16384,1"), "set_patch_margin","get_patch_margin",MARGIN_BOTTOM );
  77. }
  78. void NinePatchRect::set_texture(const Ref<Texture>& p_tex) {
  79. if (texture==p_tex)
  80. return;
  81. texture=p_tex;
  82. update();
  83. /*
  84. if (texture.is_valid())
  85. texture->set_flags(texture->get_flags()&(~Texture::FLAG_REPEAT)); //remove repeat from texture, it looks bad in sprites
  86. */
  87. minimum_size_changed();
  88. emit_signal("texture_changed");
  89. }
  90. Ref<Texture> NinePatchRect::get_texture() const {
  91. return texture;
  92. }
  93. void NinePatchRect::set_patch_margin(Margin p_margin,int p_size) {
  94. ERR_FAIL_INDEX(p_margin,4);
  95. margin[p_margin]=p_size;
  96. update();
  97. minimum_size_changed();
  98. switch (p_margin) {
  99. case MARGIN_LEFT:
  100. _change_notify("patch_margin/left");
  101. break;
  102. case MARGIN_TOP:
  103. _change_notify("patch_margin/top");
  104. break;
  105. case MARGIN_RIGHT:
  106. _change_notify("patch_margin/right");
  107. break;
  108. case MARGIN_BOTTOM:
  109. _change_notify("patch_margin/bottom");
  110. break;
  111. }
  112. }
  113. int NinePatchRect::get_patch_margin(Margin p_margin) const{
  114. ERR_FAIL_INDEX_V(p_margin,4,0);
  115. return margin[p_margin];
  116. }
  117. void NinePatchRect::set_region_rect(const Rect2& p_region_rect) {
  118. if (region_rect==p_region_rect)
  119. return;
  120. region_rect=p_region_rect;
  121. item_rect_changed();
  122. _change_notify("region_rect");
  123. }
  124. Rect2 NinePatchRect::get_region_rect() const {
  125. return region_rect;
  126. }
  127. void NinePatchRect::set_draw_center(bool p_draw) {
  128. draw_center=p_draw;
  129. update();
  130. }
  131. bool NinePatchRect::get_draw_center() const{
  132. return draw_center;
  133. }
  134. NinePatchRect::NinePatchRect() {
  135. margin[MARGIN_LEFT]=0;
  136. margin[MARGIN_RIGHT]=0;
  137. margin[MARGIN_BOTTOM]=0;
  138. margin[MARGIN_TOP]=0;
  139. set_mouse_filter(MOUSE_FILTER_IGNORE);
  140. draw_center=true;
  141. }
  142. NinePatchRect::~NinePatchRect()
  143. {
  144. }