/* Copyright (c) 2021 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 "uitoolkit/UIStepper.h" #include "uitoolkit/UIToolkitAppearance.h" #include "uitoolkit/UIToolkitCursor.h" #include "gamemath/Scalar.h" #define stemobject_implementation UIStepper v_begin(); v_func(dispose); v_func(hitTest); v_func(mouseDown); v_func(mouseUp); v_func(mouseMoved); v_func(mouseDragged); v_func(mouseLeave); v_func(scrollWheel); v_func(menuActionDown); v_func(menuActionUp); v_func(menuDirectionDown); v_func(setFocusedElement); v_func(acceptsFocus); v_func(getBounds); v_func(getFocusBounds); v_func(draw); v_end(); UIStepper * UIStepper_create(Vector2f position, Vector2f relativeOrigin, UIStepper_orientation orientation, bool centerCell, bool repeat, UIStepperLiveUpdateCallback liveUpdateCallback, UIStepperUpdateCompleteCallback updateCompleteCallback, void * callbackContext, UIAppearance appearance) { stemobject_create_implementation(init, position, relativeOrigin, orientation, centerCell, repeat, liveUpdateCallback, updateCompleteCallback, callbackContext, appearance) } bool UIStepper_init(UIStepper * self, Vector2f position, Vector2f relativeOrigin, UIStepper_orientation orientation, bool centerCell, bool repeat, UIStepperLiveUpdateCallback liveUpdateCallback, UIStepperUpdateCompleteCallback updateCompleteCallback, void * callbackContext, UIAppearance appearance) { call_super(init, self, position, relativeOrigin, appearance); self->orientation = orientation; self->liveUpdateCallback = liveUpdateCallback; self->updateCompleteCallback = updateCompleteCallback; self->callbackContext = callbackContext; self->focusedCell = UIStepper_up; self->rollover = UIStepper_none; self->clickInProgress = UIStepper_none; self->down = UIStepper_none; self->upEnabled = true; self->downEnabled = true; self->centerCell = centerCell; self->repeat = repeat; self->repeatTimerID = SHELL_TIMER_INVALID; self->allowScrollWheelInput = true; self->centerDragDistancePerStep = 6; return true; } void UIStepper_dispose(UIStepper * self) { Shell_cancelTimer(self->repeatTimerID); call_super_virtual(dispose, self); } static enum UIStepper_cellState getCellAtPosition(UIStepper * self, float x, float y) { Rect4f bottomBounds = call_virtual(getBounds, self); Rect4f topBounds = bottomBounds, centerBounds = bottomBounds; float cellHeight = getAppearanceFloat(self->appearance, self->centerCell ? UIStepper_cellHeightWithCenter : UIStepper_cellHeight); if (self->orientation == STEPPER_HORIZONTAL) { bottomBounds.xMax = bottomBounds.xMin + cellHeight; topBounds.xMin = topBounds.xMax - cellHeight; centerBounds.xMin += cellHeight; centerBounds.xMax -= cellHeight; } else { bottomBounds.yMax = bottomBounds.yMin + cellHeight; topBounds.yMin = topBounds.yMax - cellHeight; centerBounds.yMin += cellHeight; centerBounds.yMax -= cellHeight; } if (Rect4f_containsVector2f(topBounds, VECTOR2f(x, y))) { return UIStepper_up; } if (self->centerCell && Rect4f_containsVector2f(centerBounds, VECTOR2f(x, y))) { return UIStepper_center; } if (Rect4f_containsVector2f(bottomBounds, VECTOR2f(x, y))) { return UIStepper_down; } return UIStepper_none; } bool UIStepper_hitTest(UIStepper * self, float x, float y, UIHitTestType type, int * outPriority, bool * outForwardNext) { if (!self->visible) { return false; } switch (type) { case HIT_TEST_MOUSE_DOWN: case HIT_TEST_MOUSE_OVER: case HIT_TEST_SCROLL_WHEEL: if (getCellAtPosition(self, x, y) != UIStepper_none) { return true; } break; case HIT_TEST_KEY_DOWN: break; } return false; } static void repeatTimer(ShellTimer timerID, void * context) { UIStepper * self = context; if (self->liveUpdateCallback != NULL && self->down != UIStepper_none && ((self->down == UIStepper_up && self->upEnabled) || (self->down == UIStepper_down && self->downEnabled))) { self->liveUpdateCallback(self, (self->down == UIStepper_up) * 2 - 1, Shell_getModifierKeys(), Shell_getCurrentTime(), self->callbackContext); } } static void repeatInitialTimer(ShellTimer timerID, void * context) { UIStepper * self = context; self->repeatTimerID = Shell_setTimer(getAppearanceDouble(self->appearance, UIStepper_repeatInterval), true, repeatTimer, self); } UIEventResponse UIStepper_mouseDown(UIStepper * self, unsigned int buttonNumber, unsigned int buttonMask, float x, float y, unsigned int modifiers, bool isFinalTarget, double referenceTime) { if (!self->visible) { return RESPONSE_UNHANDLED; } if (buttonNumber == 0 && self->clickInProgress == UIStepper_none) { enum UIStepper_cellState cellState = getCellAtPosition(self, x, y); if ((cellState == UIStepper_up && !self->upEnabled) || (cellState == UIStepper_down && !self->downEnabled)) { return RESPONSE_UNHANDLED; } self->clickInProgress = self->down = cellState; self->dirty = true; if (cellState == UIStepper_center) { UIToolkit_pushMouseDeltaMode(); self->centerDragDelta = 0.0f; } else if (self->repeat) { if (self->liveUpdateCallback != NULL) { self->liveUpdateCallback(self, (self->down == UIStepper_up) * 2 - 1, modifiers, referenceTime, self->callbackContext); } self->repeatTimerID = Shell_setTimer(getAppearanceDouble(self->appearance, UIStepper_repeatDelay), false, repeatInitialTimer, self); } return RESPONSE_HANDLED; } return RESPONSE_UNHANDLED; } bool UIStepper_mouseUp(UIStepper * self, unsigned int buttonNumber, unsigned int buttonMask, float x, float y, unsigned int modifiers, double referenceTime) { if (buttonNumber == 0 && self->clickInProgress != UIStepper_none) { self->clickInProgress = UIStepper_none; self->rollover = getCellAtPosition(self, x, y); Shell_cancelTimer(self->repeatTimerID); if (self->down == UIStepper_down || self->down == UIStepper_up) { if (!self->repeat) { if (self->liveUpdateCallback != NULL) { self->liveUpdateCallback(self, (self->down == UIStepper_up) * 2 - 1, modifiers, referenceTime, self->callbackContext); } if (self->updateCompleteCallback != NULL) { self->updateCompleteCallback(self, referenceTime, self->callbackContext); } } if (self->repeat && self->updateCompleteCallback != NULL) { self->updateCompleteCallback(self, referenceTime, self->callbackContext); } } else if (self->down == UIStepper_center) { UIToolkit_popMouseDeltaMode(); if (self->updateCompleteCallback != NULL && roundpositivef(self->centerDragDelta / self->centerDragDistancePerStep) != 0) { self->updateCompleteCallback(self, referenceTime, self->callbackContext); } } self->down = UIStepper_none; self->dirty = true; return true; } return false; } bool UIStepper_mouseMoved(UIStepper * self, float x, float y, float deltaX, float deltaY, unsigned int modifiers, double referenceTime) { enum UIStepper_cellState lastRollover = self->rollover; UIElement * topParent = UIElement_getTopParent(self); Vector2f rootPosition = UIElement_localToRootVector(self, VECTOR2f(x, y)); if (UIElement_hitTestSingle(topParent, rootPosition.x, rootPosition.y, HIT_TEST_MOUSE_OVER) == (UIElement *) self) { self->rollover = getCellAtPosition(self, x, y); } else { self->rollover = false; } if (self->rollover != lastRollover) { self->dirty = true; return true; } return false; } bool UIStepper_mouseDragged(UIStepper * self, unsigned int buttonMask, float x, float y, float deltaX, float deltaY, unsigned int modifiers, double referenceTime) { if ((buttonMask & 0x1) && self->clickInProgress != UIStepper_none) { if (self->clickInProgress == UIStepper_center) { int lastUnits = roundpositivef(self->centerDragDelta / self->centerDragDistancePerStep); if (self->orientation == STEPPER_HORIZONTAL) { self->centerDragDelta += deltaX; } else { self->centerDragDelta -= deltaY; } int units = roundpositivef(self->centerDragDelta / self->centerDragDistancePerStep); if (units != lastUnits && self->liveUpdateCallback != NULL) { self->liveUpdateCallback(self, units - lastUnits, modifiers, referenceTime, self->callbackContext); } } enum UIStepper_cellState lastDown = self->down; if (getCellAtPosition(self, x, y) == self->clickInProgress) { self->down = self->clickInProgress; } else { self->down = UIStepper_none; } self->rollover = self->down; if (lastDown != self->down) { self->dirty = true; return true; } } return false; } bool UIStepper_mouseLeave(UIStepper * self, unsigned int modifiers, double referenceTime) { enum UIStepper_cellState lastRollover = self->rollover; self->rollover = UIStepper_none; if (self->rollover != lastRollover) { self->dirty = true; return true; } return false; } UIEventResponse UIStepper_scrollWheel(UIStepper * self, float x, float y, int scrollDeltaX, int scrollDeltaY, unsigned int buttonMask, unsigned int modifiers, bool isFinalTarget, double referenceTime) { if (scrollDeltaY != 0 && self->clickInProgress == UIStepper_none && self->allowScrollWheelInput) { if (self->liveUpdateCallback != NULL) { self->liveUpdateCallback(self, -scrollDeltaY, modifiers, referenceTime, self->callbackContext); } if (self->updateCompleteCallback != NULL) { self->updateCompleteCallback(self, referenceTime, self->callbackContext); } return RESPONSE_HANDLED; } return RESPONSE_UNHANDLED; } bool UIStepper_menuActionDown(UIStepper * self, unsigned int actionNumber, bool isRepeat, double referenceTime) { if (actionNumber == 0 && !isRepeat && self->clickInProgress == UIStepper_none && ((self->focusedCell == UIStepper_up && self->upEnabled) || (self->focusedCell == UIStepper_down && self->downEnabled))) { self->clickInProgress = self->down = self->focusedCell; self->dirty = true; if (self->repeat && self->focusedCell != UIStepper_center) { if (self->liveUpdateCallback != NULL) { self->liveUpdateCallback(self, (self->focusedCell == UIStepper_up) * 2 - 1, 0, referenceTime, self->callbackContext); } self->repeatTimerID = Shell_setTimer(getAppearanceDouble(self->appearance, UIStepper_repeatDelay), false, repeatInitialTimer, self); } return true; } return false; } bool UIStepper_menuActionUp(UIStepper * self, unsigned int actionNumber, double referenceTime) { if (actionNumber == 0 && self->clickInProgress != UIStepper_none) { self->clickInProgress = UIStepper_none; if (!self->repeat) { if (self->liveUpdateCallback != NULL) { self->liveUpdateCallback(self, (self->focusedCell == UIStepper_up) * 2 - 1, 0, referenceTime, self->callbackContext); } } if (self->updateCompleteCallback != NULL) { self->updateCompleteCallback(self, referenceTime, self->callbackContext); } self->down = UIStepper_none; self->dirty = true; Shell_cancelTimer(self->repeatTimerID); return true; } return false; } bool UIStepper_menuDirectionDown(UIStepper * self, UINavigationDirection direction, bool isRepeat, double referenceTime) { UINavigationDirection effectiveDirection = direction; if (self->orientation == STEPPER_HORIZONTAL) { if (direction == UI_LEFT) { effectiveDirection = UI_PREVIOUS; } else if (direction == UI_RIGHT) { effectiveDirection = UI_NEXT; } } else { if (direction == UI_DOWN) { effectiveDirection = UI_PREVIOUS; } else if (direction == UI_UP) { effectiveDirection = UI_NEXT; } } if (self->focusedCell == UIStepper_up && effectiveDirection == UI_PREVIOUS) { if (self->centerCell) { self->focusedCell = UIStepper_center; return true; } if (self->downEnabled) { self->focusedCell = UIStepper_down; return true; } } if (self->focusedCell == UIStepper_down && effectiveDirection == UI_NEXT) { if (self->centerCell) { self->focusedCell = UIStepper_center; return true; } if (self->upEnabled) { self->focusedCell = UIStepper_up; return true; } } if (self->focusedCell == UIStepper_center) { if (effectiveDirection == UI_PREVIOUS && self->downEnabled) { self->focusedCell = UIStepper_down; return true; } if (effectiveDirection == UI_NEXT && self->upEnabled) { self->focusedCell = UIStepper_up; return true; } } return call_super_virtual(menuDirectionDown, self, direction, isRepeat, referenceTime); } bool UIStepper_setFocusedElement(UIStepper * self, compat_type(UIElement *) element, compat_type(UIElement *) fromElement, UINavigationDirection directionFromElement) { if (element == (UIElement *) self) { if ((self->orientation == STEPPER_HORIZONTAL && directionFromElement == UI_LEFT) || (self->orientation == STEPPER_VERTICAL && directionFromElement == UI_DOWN) || directionFromElement == UI_PREVIOUS) { self->focusedCell = UIStepper_up; } else if ((self->orientation == STEPPER_HORIZONTAL && directionFromElement == UI_RIGHT) || (self->orientation == STEPPER_VERTICAL && directionFromElement == UI_UP) || directionFromElement == UI_NEXT) { self->focusedCell = UIStepper_down; } if (self->focusedCell == UIStepper_up && !self->upEnabled) { self->focusedCell = UIStepper_down; } else if (self->focusedCell == UIStepper_down && !self->downEnabled) { self->focusedCell = UIStepper_up; } } return call_super_virtual(setFocusedElement, self, element, fromElement, directionFromElement); } bool UIStepper_acceptsFocus(UIStepper * self, bool finalTargetOnly) { return self->upEnabled || self->downEnabled; } Rect4f UIStepper_getBounds(UIStepper * self) { float width, height, length; float cellSpacing = getAppearanceFloat(self->appearance, UIStepper_cellSpacing); float stepperWidth = getAppearanceFloat(self->appearance, UIStepper_width); if (self->centerCell) { length = getAppearanceFloat(self->appearance, UIStepper_cellHeightWithCenter) * 2 + getAppearanceFloat(self->appearance, UIStepper_centerCellHeight) + cellSpacing * 2; } else { length = getAppearanceFloat(self->appearance, UIStepper_cellHeight) * 2 + cellSpacing; } if (self->orientation == STEPPER_HORIZONTAL) { width = length; height = stepperWidth; } else { width = stepperWidth; height = length; } return UIElement_boundsRectWithOrigin(self->position, self->relativeOrigin, VECTOR2f(width, height)); } Rect4f UIStepper_getFocusBounds(UIStepper * self) { Rect4f bounds = call_virtual(getAbsoluteBounds, self); float cellHeight = getAppearanceFloat(self->appearance, self->centerCell ? UIStepper_cellHeightWithCenter : UIStepper_cellHeight); if (self->focusedCell == UIStepper_up) { if (self->orientation == STEPPER_HORIZONTAL) { bounds.xMin = bounds.xMax - cellHeight; } else { bounds.yMin = bounds.yMax - cellHeight; } } else if (self->focusedCell == UIStepper_center) { if (self->orientation == STEPPER_HORIZONTAL) { bounds.xMin += cellHeight; bounds.xMax -= cellHeight; } else { bounds.yMin += cellHeight; bounds.yMax -= cellHeight; } } else { if (self->orientation == STEPPER_HORIZONTAL) { bounds.xMax = bounds.xMin + cellHeight; } else { bounds.yMax = bounds.yMin + cellHeight; } } return bounds; } void UIStepper_draw(UIStepper * self, Vector2f offset, UIDrawingInterface * drawingInterface, VertexIO * vertexIO) { call_super(draw, self, offset, drawingInterface, vertexIO); if (!self->visible) { return; } Rect4f bounds = Rect4f_offset(call_virtual(getBounds, self), offset); UIAtlasEntry atlasEntryBottom, atlasEntryCenter, atlasEntryTop; Rect4f bottomBounds = bounds, centerBounds = bounds, topBounds = bounds; quadTransformBits transform; bool hasFocus = drawingInterface->drawFocusedElementInRolloverState && call_virtual(getFocusedElement, call_super(getTopParent, self)) == (UIElement *) self; if (!self->downEnabled) { atlasEntryBottom = getAppearanceAtlasEntry(self->appearance, self->centerCell ? UIStepper_stepperBottomThinDisabled : UIStepper_stepperBottomDisabled); } else if (self->down == UIStepper_down) { atlasEntryBottom = getAppearanceAtlasEntry(self->appearance, self->centerCell ? UIStepper_stepperBottomThinDown : UIStepper_stepperBottomDown); } else if ((self->rollover == UIStepper_down && drawingInterface->drawMouseRolloverState) || hasFocus) { atlasEntryBottom = getAppearanceAtlasEntry(self->appearance, self->centerCell ? UIStepper_stepperBottomThinRollover : UIStepper_stepperBottomRollover); } else { atlasEntryBottom = getAppearanceAtlasEntry(self->appearance, self->centerCell ? UIStepper_stepperBottomThinUp : UIStepper_stepperBottomUp); } float cellHeight = getAppearanceFloat(self->appearance, self->centerCell ? UIStepper_cellHeightWithCenter : UIStepper_cellHeight); if (self->orientation == STEPPER_HORIZONTAL) { bottomBounds.xMax = bottomBounds.xMin + cellHeight; topBounds.xMin = topBounds.xMax - cellHeight; centerBounds.xMin += cellHeight; centerBounds.xMax -= cellHeight; transform = QUAD_TRANSFORM_ROTATE_CW; } else { bottomBounds.yMax = bottomBounds.yMin + cellHeight; topBounds.yMin = topBounds.yMax - cellHeight; centerBounds.yMin += cellHeight; centerBounds.yMax -= cellHeight; transform = 0; } call_virtual(drawQuadWithTransform, drawingInterface, bottomBounds, UIAtlasEntry_boundsForScale(atlasEntryBottom, drawingInterface->scaleFactor), transform, COLOR4f(1.0f, 1.0f, 1.0f, 1.0f), vertexIO); if (!self->upEnabled) { atlasEntryTop = getAppearanceAtlasEntry(self->appearance, self->centerCell ? UIStepper_stepperTopThinDisabled : UIStepper_stepperTopDisabled); } else if (self->down == UIStepper_up) { atlasEntryTop = getAppearanceAtlasEntry(self->appearance, self->centerCell ? UIStepper_stepperTopThinDown : UIStepper_stepperTopDown); } else if ((self->rollover == UIStepper_up && drawingInterface->drawMouseRolloverState) || hasFocus) { atlasEntryTop = getAppearanceAtlasEntry(self->appearance, self->centerCell ? UIStepper_stepperTopThinRollover : UIStepper_stepperTopRollover); } else { atlasEntryTop = getAppearanceAtlasEntry(self->appearance, self->centerCell ? UIStepper_stepperTopThinUp : UIStepper_stepperTopUp); } call_virtual(drawQuadWithTransform, drawingInterface, topBounds, UIAtlasEntry_boundsForScale(atlasEntryTop, drawingInterface->scaleFactor), transform, COLOR4f(1.0f, 1.0f, 1.0f, 1.0f), vertexIO); if (self->centerCell) { if (!self->upEnabled && !self->downEnabled) { atlasEntryCenter = getAppearanceAtlasEntry(self->appearance, UIStepper_stepperCenterDisabled); } else if (self->down == UIStepper_center) { atlasEntryCenter = getAppearanceAtlasEntry(self->appearance, UIStepper_stepperCenterDown); } else if ((self->rollover == UIStepper_center && drawingInterface->drawMouseRolloverState) || hasFocus) { atlasEntryCenter = getAppearanceAtlasEntry(self->appearance, UIStepper_stepperCenterRollover); } else { atlasEntryCenter = getAppearanceAtlasEntry(self->appearance, UIStepper_stepperCenterUp); } call_virtual(drawQuadWithTransform, drawingInterface, centerBounds, UIAtlasEntry_boundsForScale(atlasEntryCenter, drawingInterface->scaleFactor), transform, COLOR4f(1.0f, 1.0f, 1.0f, 1.0f), vertexIO); } }