/* Copyright (c) 2023 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "shell/ShellKeyCodes.h" #include "uitoolkit/UIContinuousSlider.h" #include "uitoolkit/UIToolkitAppearance.h" #include "uitoolkit/UIToolkitCursor.h" #include "uitoolkit/UIToolkitDrawing.h" #include #define stemobject_implementation UIContinuousSlider v_begin(); v_func(dispose); v_func(hitTest); v_func(mouseDown); v_func(mouseUp); v_func(mouseDragged); v_func(scrollWheel); v_func(getBounds); v_func(draw); v_end(); UIContinuousSlider * UIContinuousSlider_create(Vector2f value, Vector2f position, Vector2f relativeOrigin, Vector2f size, UIContinuousSlider_axis axis, UIContinuousSliderCallback changeCallback, UIContinuousSliderCallback changeCompleteCallback, void * callbackContext, UIAppearance appearance) { stemobject_create_implementation(init, value, position, relativeOrigin, size, axis, changeCallback, changeCompleteCallback, callbackContext, appearance) } bool UIContinuousSlider_init(UIContinuousSlider * self, Vector2f value, Vector2f position, Vector2f relativeOrigin, Vector2f size, UIContinuousSlider_axis axis, UIContinuousSliderCallback changeCallback, UIContinuousSliderCallback changeCompleteCallback, void * callbackContext, UIAppearance appearance) { call_super(init, self, position, relativeOrigin, appearance); self->value = value; self->size = size; self->axis = axis; self->changeCallback = changeCallback; self->changeCompleteCallback = changeCompleteCallback; self->callbackContext = callbackContext; self->dragRatio = 1.0f; self->preciseDragMultiplier = 0.1f; self->scrollWheelMultiplier = 5.0f; self->minValue = VECTOR2f_REPEAT(-FLT_MAX); self->maxValue = VECTOR2f_REPEAT(FLT_MAX); self->gridSpacing = 16.0f; self->enabled = true; self->dragging = false; self->resetOnControlClick = false; self->resetValue = VECTOR2f_ZERO; return true; } void UIContinuousSlider_dispose(UIContinuousSlider * self) { call_super_virtual(dispose, self); } bool UIContinuousSlider_hitTest(UIContinuousSlider * self, float x, float y, UIHitTestType type, int * outPriority, bool * outForwardNext) { if (call_super_virtual(hitTest, self, x, y, type, outPriority, outForwardNext)) { if (type == HIT_TEST_SCROLL_WHEEL) { *outPriority -= 10; } return true; } return false; } UIEventResponse UIContinuousSlider_mouseDown(UIContinuousSlider * self, unsigned int buttonNumber, unsigned int buttonMask, float x, float y, unsigned int modifiers, bool isFinalTarget, double referenceTime) { if (buttonNumber == 0 && self->enabled) { if (self->resetOnControlClick && (modifiers & MODIFIER_CONTROL_BIT)) { self->totalOffset = Vector2f_subtract(self->resetValue, self->value); self->value = self->resetValue; if (self->changeCallback != NULL) { self->changeCallback(self, self->value, self->totalOffset, self->totalOffset, referenceTime, self->callbackContext); } if (self->changeCompleteCallback != NULL) { self->changeCompleteCallback(self, self->value, VECTOR2f_ZERO, self->totalOffset, referenceTime, self->callbackContext); } return RESPONSE_HANDLED; } UIToolkit_pushMouseDeltaMode(); self->totalOffset = VECTOR2f_ZERO; self->changed = false; self->dragging = true; return RESPONSE_HANDLED; } return RESPONSE_UNHANDLED; } bool UIContinuousSlider_mouseUp(UIContinuousSlider * self, unsigned int buttonNumber, unsigned int buttonMask, float x, float y, unsigned int modifiers, double referenceTime) { if (buttonNumber == 0 && self->dragging) { UIToolkit_popMouseDeltaMode(); if (self->changed && self->changeCompleteCallback != NULL) { self->changeCompleteCallback(self, self->value, VECTOR2f_ZERO, self->totalOffset, referenceTime, self->callbackContext); } self->totalOffset = VECTOR2f_ZERO; self->dragging = false; return true; } return false; } static void handleDrag(UIContinuousSlider * self, float deltaX, float deltaY, unsigned int modifiers, double referenceTime) { float dragMultiplier = 1.0f; if (modifiers & MODIFIER_SHIFT_BIT) { dragMultiplier = self->preciseDragMultiplier; } else if (modifiers & MODIFIER_ANY_ALT_BIT) { dragMultiplier = 1.0f / self->preciseDragMultiplier; } if (modifiers & MODIFIER_CONTROL_BIT) { dragMultiplier *= dragMultiplier; } dragMultiplier *= self->dragRatio; Vector2f offset = VECTOR2f_ZERO; if (self->axis & UIContinuousSlider_X) { offset.x = deltaX * dragMultiplier; } if (self->axis & UIContinuousSlider_Y) { offset.y = -deltaY * dragMultiplier; } if (offset.x + self->value.x < self->minValue.x) { offset.x = self->minValue.x - self->value.x; } else if (offset.x + self->value.x > self->maxValue.x) { offset.x = self->maxValue.x - self->value.x; } if (offset.y + self->value.y < self->minValue.y) { offset.y = self->minValue.y - self->value.y; } else if (offset.y + self->value.y > self->maxValue.y) { offset.y = self->maxValue.y - self->value.y; } if (offset.x != 0.0f || offset.y != 0.0f) { self->value.x += offset.x; self->value.y += offset.y; self->totalOffset.x += offset.x; self->totalOffset.y += offset.y; self->changed = true; if (self->changeCallback != NULL) { self->changeCallback(self, self->value, offset, self->totalOffset, referenceTime, self->callbackContext); } } } bool UIContinuousSlider_mouseDragged(UIContinuousSlider * self, unsigned int buttonMask, float x, float y, float deltaX, float deltaY, unsigned int modifiers, double referenceTime) { if (self->dragging) { handleDrag(self, deltaX, deltaY, modifiers, referenceTime); return true; } return false; } UIEventResponse UIContinuousSlider_scrollWheel(UIContinuousSlider * self, float x, float y, int scrollDeltaX, int scrollDeltaY, unsigned int buttonMask, unsigned int modifiers, bool isFinalTarget, double referenceTime) { if (self->enabled) { if (self->axis == UIContinuousSlider_X || (self->axis == UIContinuousSlider_XY && (modifiers & MODIFIER_SHIFT_BIT))) { scrollDeltaX = scrollDeltaY; scrollDeltaY = 0; } modifiers = (modifiers & ~(MODIFIER_CONTROL_BIT | MODIFIER_SHIFT_BIT)) | (MODIFIER_SHIFT_BIT & !!(modifiers & MODIFIER_CONTROL_BIT)); handleDrag(self, scrollDeltaX * self->scrollWheelMultiplier, scrollDeltaY * self->scrollWheelMultiplier, modifiers, referenceTime); } return RESPONSE_HANDLED; } Rect4f UIContinuousSlider_getBounds(UIContinuousSlider * self) { return UIElement_boundsRectWithOrigin(self->position, self->relativeOrigin, self->size); } static void drawHorizontalLine(float value, Color4f color, float opposingAxisMin, float opposingAxisMax, Rect4f textureBounds, UIDrawingInterface * drawingInterface, VertexIO * vertexIO) { call_virtual(drawQuad, drawingInterface, RECT4f(value, value + 1, opposingAxisMin, opposingAxisMax), textureBounds, color, vertexIO); } static void drawVerticalLine(float lineValue, Color4f color, float opposingAxisMin, float opposingAxisMax, Rect4f textureBounds, UIDrawingInterface * drawingInterface, VertexIO * vertexIO) { call_virtual(drawQuad, drawingInterface, RECT4f(opposingAxisMin, opposingAxisMax, lineValue, lineValue + 1), textureBounds, color, vertexIO); } static void enumerateGridLines(float value, float spacing, float valueMin, float valueMax, float opposingAxisMin, float opposingAxisMax, Color4f color, Rect4f textureBounds, UIDrawingInterface * drawingInterface, VertexIO * vertexIO, void (* callback)(float value, Color4f color, float opposingAxisMin, float opposingAxisMax, Rect4f textureBounds, UIDrawingInterface * drawingInterface, VertexIO * vertexIO)) { float lineOffset = fmodf(value, spacing); if (lineOffset < 0.0f) { lineOffset += spacing; } float halfAreaSize = (valueMax - valueMin) * 0.5f; unsigned int lineBeforeCenter = (lineOffset + halfAreaSize) / spacing; for (float lineValue = valueMin + lineOffset + halfAreaSize - spacing * lineBeforeCenter; lineValue < valueMax; lineValue += spacing) { callback(lineValue, color, opposingAxisMin, opposingAxisMax, textureBounds, drawingInterface, vertexIO); } } void UIContinuousSlider_draw(UIContinuousSlider * self, Vector2f offset, UIDrawingInterface * drawingInterface, VertexIO * vertexIO) { Rect4f bounds = Rect4f_offset(call_virtual(getBounds, self), offset); call_virtual(drawSlicedQuad3x3, drawingInterface, bounds, UIAtlasEntry_boundsForScale(getAppearanceAtlasEntry(self->appearance, UIContinuousSlider_frame), drawingInterface->scaleFactor), getAppearanceSliceGrid3x3(self->appearance, UIContinuousSlider_frameSlices), getAppearanceColor4f(self->appearance, self->enabled ? UIContinuousSlider_color : UIContinuousSlider_colorDisabled), vertexIO); Color4f gridLineColor1 = getAppearanceColor4f(self->appearance, self->enabled ? UIContinuousSlider_lineColor : UIContinuousSlider_lineColorDisabled); Color4f gridLineColor2 = Color4f_multiply(gridLineColor1, COLOR4f(0.5f, 0.5f, 0.5f, 0.5f)); Color4f gridLineColor3 = Color4f_multiply(gridLineColor1, COLOR4f(0.1875f, 0.1875f, 0.1875f, 0.1875f)); UIAtlasEntry atlasEntry = getAppearanceAtlasEntry(self->appearance, UIToolkit_white); if (self->axis & UIContinuousSlider_X) { enumerateGridLines(self->value.x / self->dragRatio, self->gridSpacing, bounds.xMin + 1, bounds.xMax - 1, bounds.yMin + 1, bounds.yMax - 1, gridLineColor1, atlasEntry.bounds, drawingInterface, vertexIO, drawHorizontalLine); enumerateGridLines(self->value.x / self->dragRatio + self->gridSpacing * 0.25f, self->gridSpacing * 0.5f, bounds.xMin + 1, bounds.xMax - 1, bounds.yMin + 1, bounds.yMax - 1, gridLineColor2, atlasEntry.bounds, drawingInterface, vertexIO, drawHorizontalLine); enumerateGridLines(self->value.x / self->dragRatio + self->gridSpacing * 0.5f, self->gridSpacing, bounds.xMin + 1, bounds.xMax - 1, bounds.yMin + 1, bounds.yMax - 1, gridLineColor3, atlasEntry.bounds, drawingInterface, vertexIO, drawHorizontalLine); } if (self->axis & UIContinuousSlider_Y) { enumerateGridLines(self->value.y / self->dragRatio, self->gridSpacing, bounds.yMin + 1, bounds.yMax - 1, bounds.xMin + 1, bounds.xMax - 1, gridLineColor1, atlasEntry.bounds, drawingInterface, vertexIO, drawVerticalLine); enumerateGridLines(self->value.y / self->dragRatio + self->gridSpacing * 0.25f, self->gridSpacing * 0.5f, bounds.yMin + 1, bounds.yMax - 1, bounds.xMin + 1, bounds.xMax - 1, gridLineColor2, atlasEntry.bounds, drawingInterface, vertexIO, drawVerticalLine); enumerateGridLines(self->value.y / self->dragRatio + self->gridSpacing * 0.5f, self->gridSpacing, bounds.yMin + 1, bounds.yMax - 1, bounds.xMin + 1, bounds.xMax - 1, gridLineColor3, atlasEntry.bounds, drawingInterface, vertexIO, drawVerticalLine); } }