patch_9_frame.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "patch_9_frame.h"
  2. #include "servers/visual_server.h"
  3. void Patch9Frame::_notification(int p_what) {
  4. if (p_what==NOTIFICATION_DRAW) {
  5. if (texture.is_null())
  6. return;
  7. Size2 s=get_size();
  8. RID ci = get_canvas_item();
  9. VS::get_singleton()->canvas_item_add_style_box(ci,Rect2(Point2(),s),region_rect,texture->get_rid(),Vector2(margin[MARGIN_LEFT],margin[MARGIN_TOP]),Vector2(margin[MARGIN_RIGHT],margin[MARGIN_BOTTOM]),draw_center,modulate);
  10. // draw_texture_rect(texture,Rect2(Point2(),s),false,modulate);
  11. /*
  12. Vector<Point2> points;
  13. points.resize(4);
  14. points[0]=Point2(0,0);
  15. points[1]=Point2(s.x,0);
  16. points[2]=Point2(s.x,s.y);
  17. points[3]=Point2(0,s.y);
  18. Vector<Point2> uvs;
  19. uvs.resize(4);
  20. uvs[0]=Point2(0,0);
  21. uvs[1]=Point2(1,0);
  22. uvs[2]=Point2(1,1);
  23. uvs[3]=Point2(0,1);
  24. VisualServer::get_singleton()->canvas_item_add_primitive(ci,points,Vector<Color>(),uvs,texture->get_rid());
  25. */
  26. }
  27. }
  28. Size2 Patch9Frame::get_minimum_size() const {
  29. return Size2(margin[MARGIN_LEFT]+margin[MARGIN_RIGHT],margin[MARGIN_TOP]+margin[MARGIN_BOTTOM]);
  30. }
  31. void Patch9Frame::_bind_methods() {
  32. ObjectTypeDB::bind_method(_MD("set_texture","texture"), & Patch9Frame::set_texture );
  33. ObjectTypeDB::bind_method(_MD("get_texture"), & Patch9Frame::get_texture );
  34. ObjectTypeDB::bind_method(_MD("set_modulate","modulate"), & Patch9Frame::set_modulate );
  35. ObjectTypeDB::bind_method(_MD("get_modulate"), & Patch9Frame::get_modulate );
  36. ObjectTypeDB::bind_method(_MD("set_patch_margin","margin","value"), & Patch9Frame::set_patch_margin );
  37. ObjectTypeDB::bind_method(_MD("get_patch_margin","margin"), & Patch9Frame::get_patch_margin );
  38. ObjectTypeDB::bind_method(_MD("set_region_rect","rect"),&Patch9Frame::set_region_rect);
  39. ObjectTypeDB::bind_method(_MD("get_region_rect"),&Patch9Frame::get_region_rect);
  40. ObjectTypeDB::bind_method(_MD("set_draw_center","draw_center"), & Patch9Frame::set_draw_center );
  41. ObjectTypeDB::bind_method(_MD("get_draw_center"), & Patch9Frame::get_draw_center );
  42. ADD_PROPERTYNZ( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), _SCS("set_texture"),_SCS("get_texture") );
  43. ADD_PROPERTYNO( PropertyInfo( Variant::COLOR, "modulate"), _SCS("set_modulate"),_SCS("get_modulate") );
  44. ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "draw_center"), _SCS("set_draw_center"),_SCS("get_draw_center") );
  45. ADD_PROPERTYNZ( PropertyInfo( Variant::RECT2, "region_rect"), _SCS("set_region_rect"),_SCS("get_region_rect"));
  46. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin/left",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_LEFT );
  47. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin/top",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_TOP );
  48. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin/right",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_RIGHT );
  49. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin/bottom",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_BOTTOM );
  50. }
  51. void Patch9Frame::set_texture(const Ref<Texture>& p_tex) {
  52. texture=p_tex;
  53. update();
  54. //if (texture.is_valid())
  55. // texture->set_flags(texture->get_flags()&(~Texture::FLAG_REPEAT)); //remove repeat from texture, it looks bad in sprites
  56. minimum_size_changed();
  57. }
  58. Ref<Texture> Patch9Frame::get_texture() const {
  59. return texture;
  60. }
  61. void Patch9Frame::set_modulate(const Color& p_tex) {
  62. modulate=p_tex;
  63. update();
  64. }
  65. Color Patch9Frame::get_modulate() const{
  66. return modulate;
  67. }
  68. void Patch9Frame::set_patch_margin(Margin p_margin,int p_size) {
  69. ERR_FAIL_INDEX(p_margin,4);
  70. margin[p_margin]=p_size;
  71. update();
  72. minimum_size_changed();
  73. switch (p_margin) {
  74. case MARGIN_LEFT:
  75. _change_notify("patch_margin/left");
  76. break;
  77. case MARGIN_TOP:
  78. _change_notify("patch_margin/top");
  79. break;
  80. case MARGIN_RIGHT:
  81. _change_notify("patch_margin/right");
  82. break;
  83. case MARGIN_BOTTOM:
  84. _change_notify("patch_margin/bottom");
  85. break;
  86. }
  87. }
  88. int Patch9Frame::get_patch_margin(Margin p_margin) const{
  89. ERR_FAIL_INDEX_V(p_margin,4,0);
  90. return margin[p_margin];
  91. }
  92. void Patch9Frame::set_region_rect(const Rect2& p_region_rect) {
  93. if (region_rect==p_region_rect)
  94. return;
  95. region_rect=p_region_rect;
  96. item_rect_changed();
  97. _change_notify("region_rect");
  98. }
  99. Rect2 Patch9Frame::get_region_rect() const {
  100. return region_rect;
  101. }
  102. void Patch9Frame::set_draw_center(bool p_draw) {
  103. draw_center=p_draw;
  104. update();
  105. }
  106. bool Patch9Frame::get_draw_center() const{
  107. return draw_center;
  108. }
  109. Patch9Frame::Patch9Frame() {
  110. margin[MARGIN_LEFT]=0;
  111. margin[MARGIN_RIGHT]=0;
  112. margin[MARGIN_BOTTOM]=0;
  113. margin[MARGIN_TOP]=0;
  114. modulate=Color(1,1,1,1);
  115. set_ignore_mouse(true);
  116. draw_center=true;
  117. }
  118. Patch9Frame::~Patch9Frame()
  119. {
  120. }