1 /*************************************************************************/
2 /*  slider.cpp                                                           */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #include "slider.h"
32 #include "core/os/keyboard.h"
33 
get_minimum_size() const34 Size2 Slider::get_minimum_size() const {
35 
36 	Ref<StyleBox> style = get_stylebox("slider");
37 	Size2i ss = style->get_minimum_size() + style->get_center_size();
38 
39 	Ref<Texture> grabber = get_icon("grabber");
40 	Size2i rs = grabber->get_size();
41 
42 	if (orientation == HORIZONTAL)
43 		return Size2i(ss.width, MAX(ss.height, rs.height));
44 	else
45 		return Size2i(MAX(ss.width, rs.width), ss.height);
46 }
47 
_gui_input(Ref<InputEvent> p_event)48 void Slider::_gui_input(Ref<InputEvent> p_event) {
49 
50 	if (!editable) {
51 		return;
52 	}
53 
54 	Ref<InputEventMouseButton> mb = p_event;
55 
56 	if (mb.is_valid()) {
57 		if (mb->get_button_index() == BUTTON_LEFT) {
58 
59 			if (mb->is_pressed()) {
60 				Ref<Texture> grabber = get_icon(mouse_inside || has_focus() ? "grabber_highlight" : "grabber");
61 				grab.pos = orientation == VERTICAL ? mb->get_position().y : mb->get_position().x;
62 
63 				double grab_width = (double)grabber->get_size().width;
64 				double grab_height = (double)grabber->get_size().height;
65 				double max = orientation == VERTICAL ? get_size().height - grab_height : get_size().width - grab_width;
66 				if (orientation == VERTICAL)
67 					set_as_ratio(1 - (((double)grab.pos - (grab_height / 2.0)) / max));
68 				else
69 					set_as_ratio(((double)grab.pos - (grab_width / 2.0)) / max);
70 				grab.active = true;
71 				grab.uvalue = get_as_ratio();
72 			} else {
73 				grab.active = false;
74 			}
75 		} else if (scrollable) {
76 			if (mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_UP) {
77 				set_value(get_value() + get_step());
78 			} else if (mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_DOWN) {
79 				set_value(get_value() - get_step());
80 			}
81 		}
82 	}
83 
84 	Ref<InputEventMouseMotion> mm = p_event;
85 
86 	if (mm.is_valid()) {
87 		if (grab.active) {
88 
89 			Size2i size = get_size();
90 			Ref<Texture> grabber = get_icon("grabber");
91 			float motion = (orientation == VERTICAL ? mm->get_position().y : mm->get_position().x) - grab.pos;
92 			if (orientation == VERTICAL)
93 				motion = -motion;
94 			float areasize = orientation == VERTICAL ? size.height - grabber->get_size().height : size.width - grabber->get_size().width;
95 			if (areasize <= 0)
96 				return;
97 			float umotion = motion / float(areasize);
98 			set_as_ratio(grab.uvalue + umotion);
99 		}
100 	}
101 
102 	if (!mm.is_valid() && !mb.is_valid()) {
103 
104 		if (p_event->is_action_pressed("ui_left", true)) {
105 
106 			if (orientation != HORIZONTAL)
107 				return;
108 			set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
109 			accept_event();
110 		} else if (p_event->is_action_pressed("ui_right", true)) {
111 
112 			if (orientation != HORIZONTAL)
113 				return;
114 			set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
115 			accept_event();
116 		} else if (p_event->is_action_pressed("ui_up", true)) {
117 
118 			if (orientation != VERTICAL)
119 				return;
120 
121 			set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
122 			accept_event();
123 		} else if (p_event->is_action_pressed("ui_down", true)) {
124 
125 			if (orientation != VERTICAL)
126 				return;
127 			set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
128 			accept_event();
129 		} else if (p_event->is_action("ui_home") && p_event->is_pressed()) {
130 
131 			set_value(get_min());
132 			accept_event();
133 		} else if (p_event->is_action("ui_end") && p_event->is_pressed()) {
134 
135 			set_value(get_max());
136 			accept_event();
137 		}
138 	}
139 }
140 
_notification(int p_what)141 void Slider::_notification(int p_what) {
142 
143 	switch (p_what) {
144 		case NOTIFICATION_THEME_CHANGED: {
145 
146 			minimum_size_changed();
147 			update();
148 		} break;
149 		case NOTIFICATION_MOUSE_ENTER: {
150 
151 			mouse_inside = true;
152 			update();
153 		} break;
154 		case NOTIFICATION_MOUSE_EXIT: {
155 
156 			mouse_inside = false;
157 			update();
158 		} break;
159 		case NOTIFICATION_VISIBILITY_CHANGED: // fallthrough
160 		case NOTIFICATION_EXIT_TREE: {
161 
162 			mouse_inside = false;
163 			grab.active = false;
164 		} break;
165 		case NOTIFICATION_DRAW: {
166 			RID ci = get_canvas_item();
167 			Size2i size = get_size();
168 			Ref<StyleBox> style = get_stylebox("slider");
169 			bool highlighted = mouse_inside || has_focus();
170 			Ref<StyleBox> grabber_area = get_stylebox(highlighted ? "grabber_area_highlight" : "grabber_area");
171 			Ref<Texture> grabber = get_icon(editable ? (highlighted ? "grabber_highlight" : "grabber") : "grabber_disabled");
172 			Ref<Texture> tick = get_icon("tick");
173 			double ratio = Math::is_nan(get_as_ratio()) ? 0 : get_as_ratio();
174 
175 			if (orientation == VERTICAL) {
176 
177 				int widget_width = style->get_minimum_size().width + style->get_center_size().width;
178 				float areasize = size.height - grabber->get_size().height;
179 				style->draw(ci, Rect2i(Point2i(size.width / 2 - widget_width / 2, 0), Size2i(widget_width, size.height)));
180 				grabber_area->draw(ci, Rect2i(Point2i((size.width - widget_width) / 2, size.height - areasize * ratio - grabber->get_size().height / 2), Size2i(widget_width, areasize * ratio + grabber->get_size().width / 2)));
181 
182 				if (ticks > 1) {
183 					int grabber_offset = (grabber->get_size().height / 2 - tick->get_height() / 2);
184 					for (int i = 0; i < ticks; i++) {
185 						if (!ticks_on_borders && (i == 0 || i + 1 == ticks)) continue;
186 						int ofs = (i * areasize / (ticks - 1)) + grabber_offset;
187 						tick->draw(ci, Point2i((size.width - widget_width) / 2, ofs));
188 					}
189 				}
190 				grabber->draw(ci, Point2i(size.width / 2 - grabber->get_size().width / 2, size.height - ratio * areasize - grabber->get_size().height));
191 			} else {
192 
193 				int widget_height = style->get_minimum_size().height + style->get_center_size().height;
194 				float areasize = size.width - grabber->get_size().width;
195 
196 				style->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(size.width, widget_height)));
197 				grabber_area->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(areasize * ratio + grabber->get_size().width / 2, widget_height)));
198 
199 				if (ticks > 1) {
200 					int grabber_offset = (grabber->get_size().width / 2 - tick->get_width() / 2);
201 					for (int i = 0; i < ticks; i++) {
202 						if ((!ticks_on_borders) && ((i == 0) || ((i + 1) == ticks))) continue;
203 						int ofs = (i * areasize / (ticks - 1)) + grabber_offset;
204 						tick->draw(ci, Point2i(ofs, (size.height - widget_height) / 2));
205 					}
206 				}
207 				grabber->draw(ci, Point2i(ratio * areasize, size.height / 2 - grabber->get_size().height / 2));
208 			}
209 
210 		} break;
211 	}
212 }
213 
set_custom_step(float p_custom_step)214 void Slider::set_custom_step(float p_custom_step) {
215 
216 	custom_step = p_custom_step;
217 }
218 
get_custom_step() const219 float Slider::get_custom_step() const {
220 
221 	return custom_step;
222 }
223 
set_ticks(int p_count)224 void Slider::set_ticks(int p_count) {
225 
226 	ticks = p_count;
227 	update();
228 }
229 
get_ticks() const230 int Slider::get_ticks() const {
231 
232 	return ticks;
233 }
234 
get_ticks_on_borders() const235 bool Slider::get_ticks_on_borders() const {
236 	return ticks_on_borders;
237 }
238 
set_ticks_on_borders(bool _tob)239 void Slider::set_ticks_on_borders(bool _tob) {
240 	ticks_on_borders = _tob;
241 	update();
242 }
243 
set_editable(bool p_editable)244 void Slider::set_editable(bool p_editable) {
245 
246 	editable = p_editable;
247 	update();
248 }
249 
is_editable() const250 bool Slider::is_editable() const {
251 
252 	return editable;
253 }
254 
set_scrollable(bool p_scrollable)255 void Slider::set_scrollable(bool p_scrollable) {
256 
257 	scrollable = p_scrollable;
258 }
259 
is_scrollable() const260 bool Slider::is_scrollable() const {
261 
262 	return scrollable;
263 }
264 
_bind_methods()265 void Slider::_bind_methods() {
266 
267 	ClassDB::bind_method(D_METHOD("_gui_input"), &Slider::_gui_input);
268 	ClassDB::bind_method(D_METHOD("set_ticks", "count"), &Slider::set_ticks);
269 	ClassDB::bind_method(D_METHOD("get_ticks"), &Slider::get_ticks);
270 
271 	ClassDB::bind_method(D_METHOD("get_ticks_on_borders"), &Slider::get_ticks_on_borders);
272 	ClassDB::bind_method(D_METHOD("set_ticks_on_borders", "ticks_on_border"), &Slider::set_ticks_on_borders);
273 
274 	ClassDB::bind_method(D_METHOD("set_editable", "editable"), &Slider::set_editable);
275 	ClassDB::bind_method(D_METHOD("is_editable"), &Slider::is_editable);
276 	ClassDB::bind_method(D_METHOD("set_scrollable", "scrollable"), &Slider::set_scrollable);
277 	ClassDB::bind_method(D_METHOD("is_scrollable"), &Slider::is_scrollable);
278 
279 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
280 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scrollable"), "set_scrollable", "is_scrollable");
281 	ADD_PROPERTY(PropertyInfo(Variant::INT, "tick_count", PROPERTY_HINT_RANGE, "0,4096,1"), "set_ticks", "get_ticks");
282 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ticks_on_borders"), "set_ticks_on_borders", "get_ticks_on_borders");
283 }
284 
Slider(Orientation p_orientation)285 Slider::Slider(Orientation p_orientation) {
286 	orientation = p_orientation;
287 	mouse_inside = false;
288 	grab.active = false;
289 	ticks = 0;
290 	ticks_on_borders = false;
291 	custom_step = -1;
292 	editable = true;
293 	scrollable = true;
294 	set_focus_mode(FOCUS_ALL);
295 }
296