neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
slider.ipp
Go to the documentation of this file.
1 // slider.inl
2/*
3 neogfx C++ App/Game Engine
4 Copyright (c) 2015, 2020 Leigh Johnston. All Rights Reserved.
5
6 This program is free software: you can redistribute it and / or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#pragma once
21
24
25namespace neogfx
26{
27 template <typename T>
28 basic_slider<T>::basic_slider(slider_orientation aOrientation) :
29 slider_impl(aOrientation), iMinimum{}, iMaximum{}, iStep{}, iValue{}, iSettingNormalizedValue{ false }
30 {
31 }
32
33 template <typename T>
34 basic_slider<T>::basic_slider(i_widget& aParent, slider_orientation aOrientation) :
35 slider_impl(aParent, aOrientation), iMinimum{}, iMaximum{}, iStep{}, iValue{}, iSettingNormalizedValue{ false }
36 {
37 }
38
39 template <typename T>
40 basic_slider<T>::basic_slider(i_layout& aLayout, slider_orientation aOrientation) :
41 slider_impl(aLayout, aOrientation), iMinimum{}, iMaximum{}, iStep{}, iValue{}, iSettingNormalizedValue{ false }
42 {
43 }
44
45 template <typename T>
46 typename basic_slider<T>::value_type basic_slider<T>::minimum() const
47 {
48 return iMinimum;
49 }
50
51 template <typename T>
52 void basic_slider<T>::set_minimum(value_type aMinimum)
53 {
54 iMinimum = aMinimum;
55 ConstraintsChanged.trigger();
56 if (iValue < minimum())
57 set_value(minimum());
58 }
59
60 template <typename T>
61 typename basic_slider<T>::value_type basic_slider<T>::maximum() const
62 {
63 return iMaximum;
64 }
65
66 template <typename T>
67 void basic_slider<T>::set_maximum(value_type aMaximum)
68 {
69 iMaximum = aMaximum;
70 ConstraintsChanged.trigger();
71 if (iValue > maximum())
72 set_value(maximum());
73 }
74
75 template <typename T>
76 typename basic_slider<T>::value_type basic_slider<T>::step() const
77 {
78 return iStep;
79 }
80
81 template <typename T>
82 void basic_slider<T>::set_step(value_type aStep)
83 {
84 iStep = aStep;
85 ConstraintsChanged.trigger();
86 }
87
88 template <typename T>
89 typename basic_slider<T>::value_type basic_slider<T>::value() const
90 {
91 return iValue;
92 }
93
94 template <typename T>
95 void basic_slider<T>::set_value(value_type aValue)
96 {
97 if (iValue != aValue)
98 {
99 iValue = aValue;
100 if (!iSettingNormalizedValue)
101 slider_impl::set_normalized_value(normalized_value());
102 if (!handling_event())
103 ValueChanged.sync_trigger();
104 else
105 ValueChanged.trigger();
106 }
107 }
108
109 template <typename T>
110 double basic_slider<T>::normalized_step_value() const
111 {
112 auto range = maximum() - minimum();
113 if (range == 0)
114 return 1.0;
115 return static_cast<double>(step()) / range;
116 }
117
118 template <typename T>
119 double basic_slider<T>::normalized_value() const
120 {
121 auto range = maximum() - minimum();
122 if (range == 0)
123 return 1.0;
124 return (static_cast<double>(value()) - minimum()) / range;
125 }
126
127 template <typename T>
128 void basic_slider<T>::set_normalized_value(double aValue)
129 {
130 double const stepValue = normalized_step_value();
131 double steps = 0.0;
132 auto r = std::modf(aValue / stepValue, &steps);
133 if (r > stepValue / 2.0)
134 steps += 1.0;
135 aValue = std::max(0.0, std::min(1.0, steps * stepValue));
136 neolib::scoped_flag sf{ iSettingNormalizedValue };
137 auto const range = maximum() - minimum();
138 auto denormalized = range * aValue + minimum();
139 if (std::is_integral<value_type>())
140 {
141 if (denormalized < 0.0)
142 denormalized = std::floor(denormalized + 0.5);
143 else if (denormalized > 0.0)
144 denormalized = std::ceil(denormalized - 0.5);
145 }
146 set_value(static_cast<value_type>(denormalized));
147 slider_impl::set_normalized_value(aValue);
148 }
149}
slider_orientation
Definition slider.hpp:28