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