nine_patch_rect.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*************************************************************************/
  2. /* nine_patch_rect.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "nine_patch_rect.h"
  31. #include "scene/scene_string_names.h"
  32. #include "servers/rendering_server.h"
  33. void NinePatchRect::_notification(int p_what) {
  34. switch (p_what) {
  35. case NOTIFICATION_DRAW: {
  36. if (texture.is_null()) {
  37. return;
  38. }
  39. Rect2 rect = Rect2(Point2(), get_size());
  40. Rect2 src_rect = region_rect;
  41. texture->get_rect_region(rect, src_rect, rect, src_rect);
  42. RID ci = get_canvas_item();
  43. RS::get_singleton()->canvas_item_add_nine_patch(ci, rect, src_rect, texture->get_rid(), Vector2(margin[SIDE_LEFT], margin[SIDE_TOP]), Vector2(margin[SIDE_RIGHT], margin[SIDE_BOTTOM]), RS::NinePatchAxisMode(axis_h), RS::NinePatchAxisMode(axis_v), draw_center);
  44. } break;
  45. }
  46. }
  47. Size2 NinePatchRect::get_minimum_size() const {
  48. return Size2(margin[SIDE_LEFT] + margin[SIDE_RIGHT], margin[SIDE_TOP] + margin[SIDE_BOTTOM]);
  49. }
  50. void NinePatchRect::_bind_methods() {
  51. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &NinePatchRect::set_texture);
  52. ClassDB::bind_method(D_METHOD("get_texture"), &NinePatchRect::get_texture);
  53. ClassDB::bind_method(D_METHOD("set_patch_margin", "margin", "value"), &NinePatchRect::set_patch_margin);
  54. ClassDB::bind_method(D_METHOD("get_patch_margin", "margin"), &NinePatchRect::get_patch_margin);
  55. ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &NinePatchRect::set_region_rect);
  56. ClassDB::bind_method(D_METHOD("get_region_rect"), &NinePatchRect::get_region_rect);
  57. ClassDB::bind_method(D_METHOD("set_draw_center", "draw_center"), &NinePatchRect::set_draw_center);
  58. ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &NinePatchRect::is_draw_center_enabled);
  59. ClassDB::bind_method(D_METHOD("set_h_axis_stretch_mode", "mode"), &NinePatchRect::set_h_axis_stretch_mode);
  60. ClassDB::bind_method(D_METHOD("get_h_axis_stretch_mode"), &NinePatchRect::get_h_axis_stretch_mode);
  61. ClassDB::bind_method(D_METHOD("set_v_axis_stretch_mode", "mode"), &NinePatchRect::set_v_axis_stretch_mode);
  62. ClassDB::bind_method(D_METHOD("get_v_axis_stretch_mode"), &NinePatchRect::get_v_axis_stretch_mode);
  63. ADD_SIGNAL(MethodInfo("texture_changed"));
  64. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  65. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled");
  66. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect", PROPERTY_HINT_NONE, "suffix:px"), "set_region_rect", "get_region_rect");
  67. ADD_GROUP("Patch Margin", "patch_margin_");
  68. ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_left", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_patch_margin", "get_patch_margin", SIDE_LEFT);
  69. ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_top", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_patch_margin", "get_patch_margin", SIDE_TOP);
  70. ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_right", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_patch_margin", "get_patch_margin", SIDE_RIGHT);
  71. ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_bottom", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_patch_margin", "get_patch_margin", SIDE_BOTTOM);
  72. ADD_GROUP("Axis Stretch", "axis_stretch_");
  73. ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_horizontal", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_h_axis_stretch_mode", "get_h_axis_stretch_mode");
  74. ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_vertical", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_v_axis_stretch_mode", "get_v_axis_stretch_mode");
  75. BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_STRETCH);
  76. BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE);
  77. BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE_FIT);
  78. }
  79. void NinePatchRect::set_texture(const Ref<Texture2D> &p_tex) {
  80. if (texture == p_tex) {
  81. return;
  82. }
  83. texture = p_tex;
  84. queue_redraw();
  85. update_minimum_size();
  86. emit_signal(SceneStringNames::get_singleton()->texture_changed);
  87. }
  88. Ref<Texture2D> NinePatchRect::get_texture() const {
  89. return texture;
  90. }
  91. void NinePatchRect::set_patch_margin(Side p_side, int p_size) {
  92. ERR_FAIL_INDEX((int)p_side, 4);
  93. if (margin[p_side] == p_size) {
  94. return;
  95. }
  96. margin[p_side] = p_size;
  97. queue_redraw();
  98. update_minimum_size();
  99. }
  100. int NinePatchRect::get_patch_margin(Side p_side) const {
  101. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  102. return margin[p_side];
  103. }
  104. void NinePatchRect::set_region_rect(const Rect2 &p_region_rect) {
  105. if (region_rect == p_region_rect) {
  106. return;
  107. }
  108. region_rect = p_region_rect;
  109. item_rect_changed();
  110. }
  111. Rect2 NinePatchRect::get_region_rect() const {
  112. return region_rect;
  113. }
  114. void NinePatchRect::set_draw_center(bool p_enabled) {
  115. if (draw_center == p_enabled) {
  116. return;
  117. }
  118. draw_center = p_enabled;
  119. queue_redraw();
  120. }
  121. bool NinePatchRect::is_draw_center_enabled() const {
  122. return draw_center;
  123. }
  124. void NinePatchRect::set_h_axis_stretch_mode(AxisStretchMode p_mode) {
  125. if (axis_h == p_mode) {
  126. return;
  127. }
  128. axis_h = p_mode;
  129. queue_redraw();
  130. }
  131. NinePatchRect::AxisStretchMode NinePatchRect::get_h_axis_stretch_mode() const {
  132. return axis_h;
  133. }
  134. void NinePatchRect::set_v_axis_stretch_mode(AxisStretchMode p_mode) {
  135. if (axis_v == p_mode) {
  136. return;
  137. }
  138. axis_v = p_mode;
  139. queue_redraw();
  140. }
  141. NinePatchRect::AxisStretchMode NinePatchRect::get_v_axis_stretch_mode() const {
  142. return axis_v;
  143. }
  144. NinePatchRect::NinePatchRect() {
  145. set_mouse_filter(MOUSE_FILTER_IGNORE);
  146. }
  147. NinePatchRect::~NinePatchRect() {
  148. }