/// <summary> /// Construct a new slider /// </summary> /// <param name="x">The horizontal offset of the slider [0 ; 1]</param> /// <param name="y">The vertical offset of the slider [0 ; 1]</param> /// <param name="w">The width of the slider [0 ; 1]</param> /// <param name="h">The height of the slider [0 ; 1]</param> /// <param name="text">The label of the slider</param> /// <param name="minValue">The minimal slider value</param> /// <param name="maxValue">The maximum slider value</param> /// <param name="startValue">The initial slider value</param> public Slider(double x, double y, double w, double h, double minValue, double maxValue, double startValue) { this.x = x; this.y = y; this.w = w; this.h = h; this.minValue = minValue; this.maxValue = maxValue; this.sliderValue = startValue; State = SliderStates.Mouseable; }
/// <summary> /// Update the slider /// </summary> /// <param name="panelRect">The rectangle the slider resides in</param> public override void Update(Rect panelRect) { int mouseX = Globals.mouseX; int mouseY = Globals.mouseY; var events = Globals.eventQueue; double _mouseX = (mouseX - panelRect.X) / (double)panelRect.W; double _mouseY = (mouseY - panelRect.Y) / (double)panelRect.H; bool hovering = false; bool mouseUp = (events.Any(e => e.type == SDL.SDL_EventType.SDL_MOUSEBUTTONUP)); bool mouseDown = (events.Any(e => e.type == SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN)); if (_mouseX > x - h / 2 && _mouseX < x + h / 2 + w && _mouseY > y && _mouseY < y + h) { hovering = true; } if (!hovering && State != SliderStates.Pressed) { State = SliderStates.Mouseable; } if (State == SliderStates.Mouseable && hovering) { State = SliderStates.Moused; } if (State == SliderStates.Moused && mouseDown) { State = SliderStates.Pressed; } if (State == SliderStates.Pressed && mouseUp) { State = SliderStates.Moused; } if (State == SliderStates.Pressed) { sliderValue = Math.Clamp((_mouseX - x) / w, 0, 1) * (maxValue - minValue) + minValue; } }