153 {
154
156
158
159 if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
160
161
162
163
164
167 } else {
168 const int x = event->button.x - frame.x;
169 const double step = this->step ?: (this->max - this->min) / 20.0;
170
171 $(
this,
setValue, this->value + (x > this->handle->
frame.x ? step : -step));
172
173 if (this->delegate.didSetValue) {
174 this->delegate.didSetValue(this, this->value);
175 }
176 }
177 return true;
178 }
179
180 else if (event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
182 self->
state &= ~ControlStateHighlighted;
183 }
184 return true;
185 }
186
187 else if (event->type == SDL_EVENT_MOUSE_MOTION) {
189 if (frame.w) {
190
191 const double fraction = (double) (event->motion.x - frame.x) / (double) frame.w;
192 double value = this->min + (this->max - this->min) * clamp(fraction, 0.0, 1.0);
193
194 if (this->snapToStep && this->step) {
195 value = clamp(round(value / this->step) * this->step, this->min, this->max);
196 }
197
198 const double delta = fabs(this->value - value);
199 if (delta > __DBL_EPSILON__) {
201
202 if (this->delegate.didSetValue) {
203 this->delegate.didSetValue(this, this->value);
204 }
205 }
206
207 }
208 }
209 return true;
210 }
211
212 if (event->type == SDL_EVENT_KEY_DOWN) {
213
214 double step = 0.0;
215 switch (event->key.key) {
216 case SDLK_LEFT:
217 step = -this->step ?: -(this->max - this->min) / 20.0;
218 break;
219 case SDLK_RIGHT:
220 step = this->step ?: (this->max - this->min) / 20.0;
221 break;
222 }
223
224 if (step) {
225 $(
this,
setValue, this->value + step);
226
227 if (this->delegate.didSetValue) {
228 this->delegate.didSetValue(this, this->value);
229 }
230 return true;
231 }
232 }
233
235}
@ ControlStateHighlighted
static bool captureEvent(Control *self, const SDL_Event *event)
static bool didReceiveEvent(const View *self, const SDL_Event *event)
static SDL_Rect renderFrame(const View *self)
Controls are Views which capture and respond to events.
unsigned int state
The bit mask of ControlState.
SDL_Rect frame
The frame, relative to the superview.