/* 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 stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(hitTest); stemobject_vtable_entry(mouseDown); stemobject_vtable_entry(mouseUp); stemobject_vtable_entry(mouseDragged); stemobject_vtable_entry(scrollWheel); stemobject_vtable_entry(getBounds); stemobject_vtable_entry(draw); stemobject_vtable_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->gridSpacing = 16.0f; 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) { UIToolkit_pushMouseDeltaMode(); self->totalOffset = VECTOR2f_ZERO; self->changed = false; 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) { UIToolkit_popMouseDeltaMode(); if (self->changed && self->changeCompleteCallback != NULL) { self->changeCompleteCallback(self, self->value, VECTOR2f_ZERO, self->totalOffset, referenceTime, self->callbackContext); } self->totalOffset = VECTOR2f_ZERO; 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_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 != 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 (buttonMask & 1) { handleDrag(self, deltaX, deltaY, modifiers, referenceTime); return true; } return false; } UIEventResponse UIContinuousSlider_scrollWheel(UIContinuousSlider * self, float x, float y, int deltaX, int deltaY, unsigned int modifiers, bool isFinalTarget, double referenceTime) { if (self->axis == UIContinuousSlider_X || (self->axis == UIContinuousSlider_XY && (modifiers & MODIFIER_SHIFT_BIT))) { deltaX = deltaY; deltaY = 0; } modifiers = (modifiers & ~(MODIFIER_CONTROL_BIT | MODIFIER_SHIFT_BIT)) | (MODIFIER_SHIFT_BIT & !!(modifiers & MODIFIER_CONTROL_BIT)); handleDrag(self, deltaX * self->scrollWheelMultiplier, deltaY * self->scrollWheelMultiplier, modifiers, referenceTime); return RESPONSE_HANDLED; } Rect4f UIContinuousSlider_getBounds(UIContinuousSlider * self) { return UIElement_boundsRectWithOrigin(self->position, self->relativeOrigin, self->size); } void UIContinuousSlider_draw(UIContinuousSlider * self, Vector2f offset, UIDrawingInterface * drawingInterface, VertexIO * vertexIO) { Rect4f bounds = Rect4f_offset(call_virtual(getBounds, self), offset); UIAtlasEntry atlasEntry = getAppearanceAtlasEntry(self->appearance, UIToolkit_white); call_virtual(drawQuad, drawingInterface, bounds, atlasEntry.bounds, COLOR4f(1.0f, 1.0f, 1.0f, 1.0f), vertexIO); UIToolkit_drawRectOutline(bounds, 1, COLOR4f(0.0f, 0.0f, 0.0f, 1.0f), atlasEntry.bounds, drawingInterface, vertexIO); if (self->axis & UIContinuousSlider_X) { float lineOffsetX = fmodf(self->value.x / self->dragRatio, self->gridSpacing); if (lineOffsetX < 0.0f) { lineOffsetX += self->gridSpacing; } float halfGridAreaWidth = (bounds.xMax - bounds.xMin - 2) * 0.5f; unsigned int lineCountLeftOfCenter = (lineOffsetX + halfGridAreaWidth) / self->gridSpacing; for (float lineX = bounds.xMin + 1 + lineOffsetX + halfGridAreaWidth - self->gridSpacing * lineCountLeftOfCenter; lineX <= bounds.xMax - 2; lineX += self->gridSpacing) { call_virtual(drawQuad, drawingInterface, RECT4f(lineX, lineX + 1, bounds.yMin + 1, bounds.yMax - 1), atlasEntry.bounds, COLOR4f(0.5f, 0.5f, 0.5f, 1.0f), vertexIO); } } if (self->axis & UIContinuousSlider_Y) { float lineOffsetY = fmodf(self->value.y / self->dragRatio, self->gridSpacing); if (lineOffsetY < 0.0f) { lineOffsetY += self->gridSpacing; } float halfGridAreaHeight = (bounds.yMax - bounds.yMin - 2) * 0.5f; unsigned int lineCountBelowCenter = (lineOffsetY + halfGridAreaHeight) / self->gridSpacing; for (float lineY = bounds.yMin + 1 + lineOffsetY + halfGridAreaHeight - self->gridSpacing * lineCountBelowCenter; lineY <= bounds.yMax - 2; lineY += self->gridSpacing) { call_virtual(drawQuad, drawingInterface, RECT4f(bounds.xMin + 1, bounds.xMax - 1, lineY, lineY + 1), atlasEntry.bounds, COLOR4f(0.5f, 0.5f, 0.5f, 1.0f), vertexIO); } } }