/* 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 "gamemath/Scalar.h" #define stemobject_implementation UIStepper stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(hitTest); stemobject_vtable_entry(mouseDown); stemobject_vtable_entry(mouseUp); stemobject_vtable_entry(mouseMoved); stemobject_vtable_entry(mouseDragged); stemobject_vtable_entry(scrollWheel); stemobject_vtable_entry(menuActionDown); stemobject_vtable_entry(menuActionUp); stemobject_vtable_entry(menuDirectionDown); stemobject_vtable_entry(setFocusedElement); stemobject_vtable_entry(acceptsFocus); stemobject_vtable_entry(getBounds); stemobject_vtable_entry(getFocusBounds); stemobject_vtable_entry(draw); stemobject_vtable_end(); UIStepper * UIStepper_create(Vector2f position, Vector2f relativeOrigin, UIStepper_orientation orientation, bool repeat, UIStepperLiveUpdateCallback liveUpdateCallback, UIStepperUpdateCompleteCallback updateCompleteCallback, void * callbackContext, UIAppearance appearance) { stemobject_create_implementation(init, position, relativeOrigin, orientation, repeat, liveUpdateCallback, updateCompleteCallback, callbackContext, appearance) } bool UIStepper_init(UIStepper * self, Vector2f position, Vector2f relativeOrigin, UIStepper_orientation orientation, 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_top; self->rollover = UIStepper_none; self->clickInProgress = UIStepper_none; self->down = UIStepper_none; self->upEnabled = true; self->downEnabled = true; self->repeat = repeat; self->repeatTimerID = SHELL_TIMER_INVALID; self->allowScrollWheelInput = true; 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; float stepperCellHeight = getAppearanceFloat(self->appearance, UIStepper_cellHeight); if (self->orientation == STEPPER_HORIZONTAL) { bottomBounds.xMax = bottomBounds.xMin + stepperCellHeight; topBounds.xMin = topBounds.xMax - stepperCellHeight; } else { bottomBounds.yMax = bottomBounds.yMin + stepperCellHeight; topBounds.yMin = topBounds.yMax - stepperCellHeight; } if (Rect4f_containsVector2f(topBounds, VECTOR2f(x, y))) { return UIStepper_top; } if (Rect4f_containsVector2f(bottomBounds, VECTOR2f(x, y))) { return UIStepper_bottom; } 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_top && self->upEnabled) || (self->down == UIStepper_bottom && self->downEnabled))) { self->liveUpdateCallback(self, (self->down == UIStepper_top) * 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_top && !self->upEnabled) || (cellState == UIStepper_bottom && !self->downEnabled)) { return RESPONSE_UNHANDLED; } self->clickInProgress = self->down = cellState; self->dirty = true; if (self->repeat) { if (self->liveUpdateCallback != NULL) { self->liveUpdateCallback(self, (self->down == UIStepper_top) * 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); if (self->down != UIStepper_none) { if (!self->repeat) { if (self->liveUpdateCallback != NULL) { self->liveUpdateCallback(self, (self->down == UIStepper_top) * 2 - 1, modifiers, referenceTime, self->callbackContext); } if (self->updateCompleteCallback != NULL) { self->updateCompleteCallback(self, referenceTime, self->callbackContext); } } self->down = UIStepper_none; self->dirty = true; } if (self->repeat && self->updateCompleteCallback != NULL) { self->updateCompleteCallback(self, referenceTime, self->callbackContext); } Shell_cancelTimer(self->repeatTimerID); 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; self->rollover = getCellAtPosition(self, x, y); 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) { 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; } UIEventResponse UIStepper_scrollWheel(UIStepper * self, float x, float y, int deltaX, int deltaY, unsigned int modifiers, bool isFinalTarget, double referenceTime) { if (deltaY != 0 && self->clickInProgress == UIStepper_none && self->allowScrollWheelInput) { if (self->liveUpdateCallback != NULL) { self->liveUpdateCallback(self, -deltaY, 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_top && self->upEnabled) || (self->focusedCell == UIStepper_bottom && self->downEnabled))) { self->clickInProgress = self->down = self->focusedCell; self->dirty = true; if (self->repeat) { if (self->liveUpdateCallback != NULL) { self->liveUpdateCallback(self, (self->focusedCell == UIStepper_top) * 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_top) * 2 - 1, 0, referenceTime, self->callbackContext); } if (self->updateCompleteCallback != NULL) { self->updateCompleteCallback(self, referenceTime, self->callbackContext); } } self->down = UIStepper_none; self->dirty = true; if (self->repeat && self->updateCompleteCallback != NULL) { self->updateCompleteCallback(self, referenceTime, self->callbackContext); } Shell_cancelTimer(self->repeatTimerID); return true; } return false; } bool UIStepper_menuDirectionDown(UIStepper * self, UINavigationDirection direction, bool isRepeat, double referenceTime) { if (self->focusedCell == UIStepper_top && ((self->orientation == STEPPER_HORIZONTAL && direction == UI_LEFT) || (self->orientation == STEPPER_VERTICAL && direction == UI_DOWN) || direction == UI_PREVIOUS) && self->downEnabled) { self->focusedCell = UIStepper_bottom; return true; } if (self->focusedCell == UIStepper_bottom && ((self->orientation == STEPPER_HORIZONTAL && direction == UI_RIGHT) || (self->orientation == STEPPER_VERTICAL && direction == UI_UP) || direction == UI_NEXT) && self->upEnabled) { self->focusedCell = UIStepper_top; 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_top; } else if ((self->orientation == STEPPER_HORIZONTAL && directionFromElement == UI_RIGHT) || (self->orientation == STEPPER_VERTICAL && directionFromElement == UI_UP) || directionFromElement == UI_NEXT) { self->focusedCell = UIStepper_bottom; } if (self->focusedCell == UIStepper_top && !self->upEnabled) { self->focusedCell = UIStepper_bottom; } else if (self->focusedCell == UIStepper_bottom && !self->downEnabled) { self->focusedCell = UIStepper_top; } } return call_super_virtual(setFocusedElement, self, element, fromElement, directionFromElement); } bool UIStepper_acceptsFocus(UIStepper * self) { return self->upEnabled || self->downEnabled; } Rect4f UIStepper_getBounds(UIStepper * self) { float width, height; float stepperCellHeight = getAppearanceFloat(self->appearance, UIStepper_cellHeight); float stepperCellSpacing = getAppearanceFloat(self->appearance, UIStepper_cellSpacing); float stepperWidth = getAppearanceFloat(self->appearance, UIStepper_width); if (self->orientation == STEPPER_HORIZONTAL) { width = stepperCellHeight * 2 + stepperCellSpacing; height = stepperWidth; } else { width = stepperWidth; height = stepperCellHeight * 2 + stepperCellSpacing; } return UIElement_boundsRectWithOrigin(self->position, self->relativeOrigin, VECTOR2f(width, height)); } Rect4f UIStepper_getFocusBounds(UIStepper * self) { Rect4f bounds = call_virtual(getAbsoluteBounds, self); float stepperCellHeight = getAppearanceFloat(self->appearance, UIStepper_cellHeight); if (self->focusedCell == UIStepper_top) { if (self->orientation == STEPPER_HORIZONTAL) { bounds.xMin = bounds.xMax - stepperCellHeight; } else { bounds.yMin = bounds.yMax - stepperCellHeight; } } else { if (self->orientation == STEPPER_HORIZONTAL) { bounds.xMax = bounds.xMin + stepperCellHeight; } else { bounds.yMax = bounds.yMin + stepperCellHeight; } } 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, atlasEntryTop; Rect4f bottomBounds = 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, UIStepper_stepperBottomDisabled); } else if (self->down == UIStepper_bottom) { atlasEntryBottom = getAppearanceAtlasEntry(self->appearance, UIStepper_stepperBottomDown); } else if ((self->rollover == UIStepper_bottom && drawingInterface->drawMouseRolloverState) || hasFocus) { atlasEntryBottom = getAppearanceAtlasEntry(self->appearance, UIStepper_stepperBottomRollover); } else { atlasEntryBottom = getAppearanceAtlasEntry(self->appearance, UIStepper_stepperBottomUp); } float stepperCellHeight = getAppearanceFloat(self->appearance, UIStepper_cellHeight); if (self->orientation == STEPPER_HORIZONTAL) { bottomBounds.xMax = bottomBounds.xMin + stepperCellHeight; topBounds.xMin = topBounds.xMax - stepperCellHeight; transform = QUAD_TRANSFORM_ROTATE_CW; } else { bottomBounds.yMax = bottomBounds.yMin + stepperCellHeight; topBounds.yMin = topBounds.yMax - stepperCellHeight; 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, UIStepper_stepperTopDisabled); } else if (self->down == UIStepper_top) { atlasEntryTop = getAppearanceAtlasEntry(self->appearance, UIStepper_stepperTopDown); } else if ((self->rollover == UIStepper_top && drawingInterface->drawMouseRolloverState) || hasFocus) { atlasEntryTop = getAppearanceAtlasEntry(self->appearance, UIStepper_stepperTopRollover); } else { atlasEntryTop = getAppearanceAtlasEntry(self->appearance, UIStepper_stepperTopUp); } call_virtual(drawQuadWithTransform, drawingInterface, topBounds, UIAtlasEntry_boundsForScale(atlasEntryTop, drawingInterface->scaleFactor), transform, COLOR4f(1.0f, 1.0f, 1.0f, 1.0f), vertexIO); }