style_box.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**************************************************************************/
  2. /* style_box.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "style_box.h"
  31. #include "scene/main/canvas_item.h"
  32. Size2 StyleBox::get_minimum_size() const {
  33. Size2 min_size = Size2(get_margin(SIDE_LEFT) + get_margin(SIDE_RIGHT), get_margin(SIDE_TOP) + get_margin(SIDE_BOTTOM));
  34. Size2 custom_size;
  35. GDVIRTUAL_CALL(_get_minimum_size, custom_size);
  36. if (min_size.x < custom_size.x) {
  37. min_size.x = custom_size.x;
  38. }
  39. if (min_size.y < custom_size.y) {
  40. min_size.y = custom_size.y;
  41. }
  42. return min_size;
  43. }
  44. void StyleBox::set_content_margin(Side p_side, float p_value) {
  45. ERR_FAIL_INDEX((int)p_side, 4);
  46. content_margin[p_side] = p_value;
  47. emit_changed();
  48. }
  49. void StyleBox::set_content_margin_all(float p_value) {
  50. for (int i = 0; i < 4; i++) {
  51. content_margin[i] = p_value;
  52. }
  53. emit_changed();
  54. }
  55. void StyleBox::set_content_margin_individual(float p_left, float p_top, float p_right, float p_bottom) {
  56. content_margin[SIDE_LEFT] = p_left;
  57. content_margin[SIDE_TOP] = p_top;
  58. content_margin[SIDE_RIGHT] = p_right;
  59. content_margin[SIDE_BOTTOM] = p_bottom;
  60. emit_changed();
  61. }
  62. float StyleBox::get_content_margin(Side p_side) const {
  63. ERR_FAIL_INDEX_V((int)p_side, 4, 0.0);
  64. return content_margin[p_side];
  65. }
  66. float StyleBox::get_margin(Side p_side) const {
  67. ERR_FAIL_INDEX_V((int)p_side, 4, 0.0);
  68. if (content_margin[p_side] < 0) {
  69. return get_style_margin(p_side);
  70. } else {
  71. return content_margin[p_side];
  72. }
  73. }
  74. Point2 StyleBox::get_offset() const {
  75. return Point2(get_margin(SIDE_LEFT), get_margin(SIDE_TOP));
  76. }
  77. void StyleBox::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  78. GDVIRTUAL_CALL(_draw, p_canvas_item, p_rect);
  79. }
  80. Rect2 StyleBox::get_draw_rect(const Rect2 &p_rect) const {
  81. Rect2 ret;
  82. if (GDVIRTUAL_CALL(_get_draw_rect, p_rect, ret)) {
  83. return ret;
  84. }
  85. return p_rect;
  86. }
  87. CanvasItem *StyleBox::get_current_item_drawn() const {
  88. return CanvasItem::get_current_item_drawn();
  89. }
  90. bool StyleBox::test_mask(const Point2 &p_point, const Rect2 &p_rect) const {
  91. bool ret = true;
  92. GDVIRTUAL_CALL(_test_mask, p_point, p_rect, ret);
  93. return ret;
  94. }
  95. void StyleBox::_bind_methods() {
  96. ClassDB::bind_method(D_METHOD("get_minimum_size"), &StyleBox::get_minimum_size);
  97. ClassDB::bind_method(D_METHOD("set_content_margin", "margin", "offset"), &StyleBox::set_content_margin);
  98. ClassDB::bind_method(D_METHOD("set_content_margin_all", "offset"), &StyleBox::set_content_margin_all);
  99. ClassDB::bind_method(D_METHOD("get_content_margin", "margin"), &StyleBox::get_content_margin);
  100. ClassDB::bind_method(D_METHOD("get_margin", "margin"), &StyleBox::get_margin);
  101. ClassDB::bind_method(D_METHOD("get_offset"), &StyleBox::get_offset);
  102. ClassDB::bind_method(D_METHOD("draw", "canvas_item", "rect"), &StyleBox::draw);
  103. ClassDB::bind_method(D_METHOD("get_current_item_drawn"), &StyleBox::get_current_item_drawn);
  104. ClassDB::bind_method(D_METHOD("test_mask", "point", "rect"), &StyleBox::test_mask);
  105. ADD_GROUP("Content Margins", "content_margin_");
  106. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_left", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_content_margin", "get_content_margin", SIDE_LEFT);
  107. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_top", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_content_margin", "get_content_margin", SIDE_TOP);
  108. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_right", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_content_margin", "get_content_margin", SIDE_RIGHT);
  109. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_bottom", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_content_margin", "get_content_margin", SIDE_BOTTOM);
  110. GDVIRTUAL_BIND(_draw, "to_canvas_item", "rect")
  111. GDVIRTUAL_BIND(_get_draw_rect, "rect")
  112. GDVIRTUAL_BIND(_get_minimum_size)
  113. GDVIRTUAL_BIND(_test_mask, "point", "rect")
  114. }
  115. StyleBox::StyleBox() {
  116. for (int i = 0; i < 4; i++) {
  117. content_margin[i] = -1;
  118. }
  119. }