Skip to content
0.2.0-dev Download ZIP
164 lines ·5.2 KB Raw
1/**************************************************************************/
2/* scroll_bar.h */
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#pragma once
32
33#include "scene/gui/range.h"
34
35class ScrollBar : public Range {
36 GDCLASS(ScrollBar, Range);
37
38 enum HighlightStatus {
39 HIGHLIGHT_NONE,
40 HIGHLIGHT_DECR,
41 HIGHLIGHT_RANGE,
42 HIGHLIGHT_INCR,
43 };
44
45 static bool focus_by_default;
46
47 Orientation orientation;
48 Size2 size;
49 float custom_step = -1.0;
50
51 HighlightStatus highlight = HIGHLIGHT_NONE;
52
53 bool incr_active = false;
54 bool decr_active = false;
55
56 struct Drag {
57 bool active = false;
58 float pos_at_click = 0.0;
59 float value_at_click = 0.0;
60 } drag;
61
62 double get_grabber_size() const;
63 double get_grabber_min_size() const;
64 double get_area_size() const;
65 double get_grabber_offset() const;
66
67 static void set_can_focus_by_default(bool p_can_focus);
68
69 Node *drag_node = nullptr;
70 NodePath drag_node_path;
71 bool drag_node_enabled = true;
72
73 Vector2 drag_node_speed;
74 Vector2 drag_node_accum;
75 Vector2 drag_node_from;
76 Vector2 last_drag_node_accum;
77 float time_since_motion = 0.0;
78 bool drag_node_touching = false;
79 bool drag_node_touching_deaccel = false;
80
81 bool scrolling = false;
82 double target_scroll = 0.0;
83 bool smooth_scroll_enabled = false;
84
85 void _drag_node_exit();
86 void _drag_node_input(const Ref<InputEvent> &p_input);
87 Size2 _fit_icon_size(const Size2 &p_size) const;
88
89 virtual void gui_input(const Ref<InputEvent> &p_event) override;
90
91protected:
92 struct ThemeCache {
93 Ref<StyleBox> scroll_style;
94 Ref<StyleBox> scroll_focus_style;
95 Ref<StyleBox> scroll_offset_style;
96 Ref<StyleBox> grabber_style;
97 Ref<StyleBox> grabber_hl_style;
98 Ref<StyleBox> grabber_pressed_style;
99
100 Ref<Texture2D> increment_icon;
101 Ref<Texture2D> increment_hl_icon;
102 Ref<Texture2D> increment_pressed_icon;
103 Ref<Texture2D> decrement_icon;
104 Ref<Texture2D> decrement_hl_icon;
105 Ref<Texture2D> decrement_pressed_icon;
106
107 int padding_left = 0;
108 int padding_top = 0;
109 int padding_right = 0;
110 int padding_bottom = 0;
111
112 int icon_max_size = 0;
113
114 Ref<AudioStream> drag_started_sound;
115 Ref<AudioStream> drag_ended_sound;
116 Ref<AudioStream> value_changed_sound;
117 Ref<AudioStream> value_change_rejected_sound;
118 } theme_cache;
119
120 void _notification(int p_what);
121 static void _bind_methods();
122
123public:
124 static inline const int PAGE_DIVISOR = 8;
125
126 void scroll(double p_amount);
127 void scroll_to(double p_position);
128
129 void set_custom_step(float p_custom_step);
130 float get_custom_step() const;
131
132 void set_drag_node(const NodePath &p_path);
133 NodePath get_drag_node() const;
134 void set_drag_node_enabled(bool p_enable);
135
136 void set_smooth_scroll_enabled(bool p_enable);
137 bool is_smooth_scroll_enabled() const;
138
139 virtual Size2 get_minimum_size() const override;
140 ScrollBar(Orientation p_orientation = VERTICAL);
141 ~ScrollBar();
142};
143
144class HScrollBar : public ScrollBar {
145 GDCLASS(HScrollBar, ScrollBar);
146
147protected:
148 static void _bind_methods();
149
150public:
151 HScrollBar() :
152 ScrollBar(HORIZONTAL) { set_v_size_flags(SIZE_SHRINK_BEGIN); }
153};
154
155class VScrollBar : public ScrollBar {
156 GDCLASS(VScrollBar, ScrollBar);
157
158protected:
159 static void _bind_methods();
160
161public:
162 VScrollBar() :
163 ScrollBar(VERTICAL) { set_h_size_flags(SIZE_SHRINK_BEGIN); }
164};