style_box_line.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**************************************************************************/
  2. /* style_box_line.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_line.h"
  31. #include "servers/rendering_server.h"
  32. float StyleBoxLine::get_style_margin(Side p_side) const {
  33. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  34. if (vertical) {
  35. if (p_side == SIDE_LEFT || p_side == SIDE_RIGHT) {
  36. return thickness / 2.0;
  37. }
  38. } else if (p_side == SIDE_TOP || p_side == SIDE_BOTTOM) {
  39. return thickness / 2.0;
  40. }
  41. return 0;
  42. }
  43. void StyleBoxLine::set_color(const Color &p_color) {
  44. color = p_color;
  45. emit_changed();
  46. }
  47. Color StyleBoxLine::get_color() const {
  48. return color;
  49. }
  50. void StyleBoxLine::set_thickness(int p_thickness) {
  51. thickness = p_thickness;
  52. emit_changed();
  53. }
  54. int StyleBoxLine::get_thickness() const {
  55. return thickness;
  56. }
  57. void StyleBoxLine::set_vertical(bool p_vertical) {
  58. vertical = p_vertical;
  59. emit_changed();
  60. }
  61. bool StyleBoxLine::is_vertical() const {
  62. return vertical;
  63. }
  64. void StyleBoxLine::set_grow_end(float p_grow_end) {
  65. grow_end = p_grow_end;
  66. emit_changed();
  67. }
  68. float StyleBoxLine::get_grow_end() const {
  69. return grow_end;
  70. }
  71. void StyleBoxLine::set_grow_begin(float p_grow_begin) {
  72. grow_begin = p_grow_begin;
  73. emit_changed();
  74. }
  75. float StyleBoxLine::get_grow_begin() const {
  76. return grow_begin;
  77. }
  78. void StyleBoxLine::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  79. RenderingServer *vs = RenderingServer::get_singleton();
  80. Rect2i r = p_rect;
  81. if (vertical) {
  82. r.position.y -= grow_begin;
  83. r.size.y += (grow_begin + grow_end);
  84. r.size.x = thickness;
  85. } else {
  86. r.position.x -= grow_begin;
  87. r.size.x += (grow_begin + grow_end);
  88. r.size.y = thickness;
  89. }
  90. vs->canvas_item_add_rect(p_canvas_item, r, color);
  91. }
  92. void StyleBoxLine::_bind_methods() {
  93. ClassDB::bind_method(D_METHOD("set_color", "color"), &StyleBoxLine::set_color);
  94. ClassDB::bind_method(D_METHOD("get_color"), &StyleBoxLine::get_color);
  95. ClassDB::bind_method(D_METHOD("set_thickness", "thickness"), &StyleBoxLine::set_thickness);
  96. ClassDB::bind_method(D_METHOD("get_thickness"), &StyleBoxLine::get_thickness);
  97. ClassDB::bind_method(D_METHOD("set_grow_begin", "offset"), &StyleBoxLine::set_grow_begin);
  98. ClassDB::bind_method(D_METHOD("get_grow_begin"), &StyleBoxLine::get_grow_begin);
  99. ClassDB::bind_method(D_METHOD("set_grow_end", "offset"), &StyleBoxLine::set_grow_end);
  100. ClassDB::bind_method(D_METHOD("get_grow_end"), &StyleBoxLine::get_grow_end);
  101. ClassDB::bind_method(D_METHOD("set_vertical", "vertical"), &StyleBoxLine::set_vertical);
  102. ClassDB::bind_method(D_METHOD("is_vertical"), &StyleBoxLine::is_vertical);
  103. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  104. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "grow_begin", PROPERTY_HINT_RANGE, "-300,300,1,suffix:px"), "set_grow_begin", "get_grow_begin");
  105. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "grow_end", PROPERTY_HINT_RANGE, "-300,300,1,suffix:px"), "set_grow_end", "get_grow_end");
  106. ADD_PROPERTY(PropertyInfo(Variant::INT, "thickness", PROPERTY_HINT_RANGE, "0,100,suffix:px"), "set_thickness", "get_thickness");
  107. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical"), "set_vertical", "is_vertical");
  108. }
  109. StyleBoxLine::StyleBoxLine() {}
  110. StyleBoxLine::~StyleBoxLine() {}