patch_9_frame.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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),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_draw_center","draw_center"), & Patch9Frame::set_draw_center );
  39. ObjectTypeDB::bind_method(_MD("get_draw_center"), & Patch9Frame::get_draw_center );
  40. ADD_PROPERTYNZ( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), _SCS("set_texture"),_SCS("get_texture") );
  41. ADD_PROPERTYNO( PropertyInfo( Variant::COLOR, "modulate"), _SCS("set_modulate"),_SCS("get_modulate") );
  42. ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "draw_center"), _SCS("set_draw_center"),_SCS("get_draw_center") );
  43. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin/left",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_LEFT );
  44. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin/top",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_TOP );
  45. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin/right",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_RIGHT );
  46. ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin/bottom",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_BOTTOM );
  47. }
  48. void Patch9Frame::set_texture(const Ref<Texture>& p_tex) {
  49. texture=p_tex;
  50. update();
  51. //if (texture.is_valid())
  52. // texture->set_flags(texture->get_flags()&(~Texture::FLAG_REPEAT)); //remove repeat from texture, it looks bad in sprites
  53. minimum_size_changed();
  54. }
  55. Ref<Texture> Patch9Frame::get_texture() const {
  56. return texture;
  57. }
  58. void Patch9Frame::set_modulate(const Color& p_tex) {
  59. modulate=p_tex;
  60. update();
  61. }
  62. Color Patch9Frame::get_modulate() const{
  63. return modulate;
  64. }
  65. void Patch9Frame::set_patch_margin(Margin p_margin,int p_size) {
  66. ERR_FAIL_INDEX(p_margin,4);
  67. margin[p_margin]=p_size;
  68. update();
  69. minimum_size_changed();
  70. }
  71. int Patch9Frame::get_patch_margin(Margin p_margin) const{
  72. ERR_FAIL_INDEX_V(p_margin,4,0);
  73. return margin[p_margin];
  74. }
  75. void Patch9Frame::set_draw_center(bool p_draw) {
  76. draw_center=p_draw;
  77. update();
  78. }
  79. bool Patch9Frame::get_draw_center() const{
  80. return draw_center;
  81. }
  82. Patch9Frame::Patch9Frame() {
  83. margin[MARGIN_LEFT]=0;
  84. margin[MARGIN_RIGHT]=0;
  85. margin[MARGIN_BOTTOM]=0;
  86. margin[MARGIN_TOP]=0;
  87. modulate=Color(1,1,1,1);
  88. set_ignore_mouse(true);
  89. draw_center=true;
  90. }
  91. Patch9Frame::~Patch9Frame()
  92. {
  93. }