/* Copyright (c) 2018 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 "gamemath/Scalar.h" #include "shell/ShellThreads.h" #include "uitoolkit/UIButton.h" #include "uitoolkit/UIToolkitAppearance.h" #include "uitoolkit/UIToolkitContext.h" #include "uitoolkit/UIToolkitDrawing.h" #include "utilities/IOUtilities.h" #include #include #define stemobject_implementation UIButton stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(mouseDown); stemobject_vtable_entry(mouseUp); stemobject_vtable_entry(mouseMoved); stemobject_vtable_entry(mouseDragged); stemobject_vtable_entry(menuActionDown); stemobject_vtable_entry(acceptsFocus); stemobject_vtable_entry(getBounds); stemobject_vtable_entry(draw); stemobject_vtable_entry(getTooltipAtPosition); stemobject_vtable_entry(invokeAction); stemobject_vtable_end(); UIButton * UIButton_create(String text, Vector2f position, Vector2f relativeOrigin, float width, UIOverflowMode overflowMode, UIButtonActionCallback actionCallback, void * actionCallbackContext, UIAppearance appearance) { stemobject_create_implementation(init, text, position, relativeOrigin, width, overflowMode, actionCallback, actionCallbackContext, appearance) } static void updateWidth(UIButton * self) { UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, UIToolkit_currentContext()->drawingInterface); Rect4f textPadding = getAppearanceRect4f(self->appearance, UIButton_textPadding); self->width = ceilf(call_virtual(measureString, typeface, self->text) + textPadding.xMin + textPadding.xMax); if (self->width < self->minWidth) { self->width = self->minWidth; } } bool UIButton_init(UIButton * self, String text, Vector2f position, Vector2f relativeOrigin, float width, UIOverflowMode overflowMode, UIButtonActionCallback actionCallback, void * actionCallbackContext, UIAppearance appearance) { call_super(init, self, position, relativeOrigin, appearance); self->text = String_copy(text); self->actionCallback = actionCallback; self->actionCallbackContext = actionCallbackContext; self->overflowMode = overflowMode; self->minWidth = width; if (overflowMode == OVERFLOW_RESIZE) { updateWidth(self); } else { self->width = width; } self->enabled = true; self->clickInProgress = false; self->down = false; self->rollover = false; self->invokeActionAfterAnimation = false; self->animationTimer = SHELL_TIMER_INVALID; return true; } void UIButton_dispose(UIButton * self) { String_free(self->text); Shell_cancelTimer(self->animationTimer); call_super(dispose, self); } void UIButton_setText(UIButton * self, String text) { String_free(self->text); self->text = String_copy(text); if (self->overflowMode == OVERFLOW_RESIZE) { updateWidth(self); } self->dirty = true; } static void animationTimerCallback(ShellTimer timerID, void * context) { UIButton * self = context; self->dirty = true; Shell_redisplay(); self->animationTimer = SHELL_TIMER_INVALID; if (self->invokeActionAfterAnimation) { call_virtual(invokeAction, self, self->actionModifiers, Shell_getCurrentTime()); } } void UIButton_simulateClick(UIButton * self, unsigned int modifiers) { if (self->animationTimer == SHELL_TIMER_INVALID) { self->dirty = true; self->invokeActionAfterAnimation = true; self->actionModifiers = modifiers; self->animationTimer = Shell_setTimer(getAppearanceDouble(self->appearance, UIToolkit_actionAnimationDuration), false, animationTimerCallback, self); } } UIEventResponse UIButton_mouseDown(UIButton * self, unsigned int buttonNumber, unsigned int buttonMask, float x, float y, unsigned int modifiers, bool isFinalTarget, double referenceTime) { if (!self->visible || !self->enabled || self->animationTimer != SHELL_TIMER_INVALID) { return RESPONSE_UNHANDLED; } if (buttonNumber == 0) { self->clickInProgress = true; self->down = true; self->dirty = true; return RESPONSE_HANDLED; } return RESPONSE_UNHANDLED; } bool UIButton_mouseUp(UIButton * self, unsigned int buttonNumber, unsigned int buttonMask, float x, float y, unsigned int modifiers, double referenceTime) { if (self->clickInProgress && self->down) { self->clickInProgress = false; self->down = false; self->dirty = true; call_virtual(invokeAction, self, modifiers, referenceTime); return true; } return false; } bool UIButton_mouseMoved(UIButton * self, float x, float y, float deltaX, float deltaY, unsigned int modifiers, double referenceTime) { bool wasRolledOver = self->rollover; UIElement * topParent = UIElement_getTopParent(self); Vector2f rootPosition = UIElement_localToRootVector(self, VECTOR2f(x, y)); self->rollover = UIElement_hitTestSingle(topParent, rootPosition.x, rootPosition.y, HIT_TEST_MOUSE_OVER) == (UIElement *) self; if (self->rollover != wasRolledOver) { self->dirty = true; return true; } return false; } bool UIButton_mouseDragged(UIButton * self, unsigned int buttonMask, float x, float y, float deltaX, float deltaY, unsigned int modifiers, double referenceTime) { if ((buttonMask & 0x1) && self->clickInProgress) { bool wasDown = self->down; int priority; bool forwardNext; self->down = call_virtual(hitTest, self, x, y, HIT_TEST_MOUSE_OVER, &priority, &forwardNext); UIElement * topParent = UIElement_getTopParent(self); Vector2f rootPosition = UIElement_localToRootVector(self, VECTOR2f(x, y)); self->rollover = UIElement_hitTestSingle(topParent, rootPosition.x, rootPosition.y, HIT_TEST_MOUSE_OVER) == (UIElement *) self; if (wasDown != self->down) { self->dirty = true; return true; } } return false; } bool UIButton_menuActionDown(UIButton * self, unsigned int actionNumber, bool isRepeat, double referenceTime) { if (actionNumber == 0 && self->enabled && self->animationTimer == SHELL_TIMER_INVALID) { self->dirty = true; self->invokeActionAfterAnimation = false; self->animationTimer = Shell_setTimer(getAppearanceDouble(self->appearance, UIToolkit_actionAnimationDuration), false, animationTimerCallback, self); call_virtual(invokeAction, self, 0, referenceTime); return true; } return false; } bool UIButton_acceptsFocus(UIButton * self) { return self->visible && self->enabled; } Rect4f UIButton_getBounds(UIButton * self) { UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, UIToolkit_currentContext()->drawingInterface); Rect4f textPadding = getAppearanceRect4f(self->appearance, UIButton_textPadding); float height = call_virtual(getLineHeight, typeface) + textPadding.yMin + textPadding.yMax; return UIElement_boundsRectWithOrigin(self->position, self->relativeOrigin, VECTOR2f(self->width, height)); } void UIButton_draw(UIButton * self, Vector2f offset, UIDrawingInterface * drawingInterface, VertexIO * vertexIO) { self->dirty = false; if (!self->visible) { return; } Rect4f bounds = Rect4f_offset(call_virtual(getBounds, self), offset); UIAtlasEntry atlasEntry; if (self->down || self->animationTimer != SHELL_TIMER_INVALID) { atlasEntry = getAppearanceAtlasEntry(self->appearance, UIButton_buttonDown); } else if ((self->rollover && drawingInterface->drawMouseRolloverState && self->enabled) || (drawingInterface->drawFocusedElementInRolloverState && call_virtual(getFocusedElement, UIElement_getTopParent(self)) == (UIElement *) self)) { atlasEntry = getAppearanceAtlasEntry(self->appearance, UIButton_buttonRollover); } else { atlasEntry = getAppearanceAtlasEntry(self->appearance, UIButton_buttonUp); } call_virtual(drawSlicedQuad3x3, drawingInterface, bounds, UIAtlasEntry_boundsForScale(atlasEntry, drawingInterface->scaleFactor), getAppearanceSliceGrid3x3(self->appearance, UIButton_backgroundSlices), getAppearanceColor4f(self->appearance, self->enabled ? UIButton_backgroundColor : UIButton_backgroundColorDisabled), vertexIO); if (self->text.bytes != NULL) { UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, drawingInterface); Rect4f textPadding = getAppearanceRect4f(self->appearance, UIButton_textPadding); Color4f textColor = getAppearanceColor4f(self->appearance, self->enabled ? UIButton_textColor : UIButton_textColorDisabled); float stringWidth = call_virtual(measureString, typeface, self->text); float typefaceHeight = call_virtual(getLineHeight, typeface); unsigned int lastIndex = vertexIO->indexCount; call_virtual(drawString, drawingInterface, typeface, self->text, VECTOR2f(roundpositivef(bounds.xMin + textPadding.xMin + (bounds.xMax - bounds.xMin - stringWidth - textPadding.xMin - textPadding.xMax) * 0.5f), roundpositivef(bounds.yMin + textPadding.yMin + (bounds.yMax - bounds.yMin - typefaceHeight - textPadding.yMin - textPadding.yMax) * 0.5f)), VECTOR2f(0.0f, 0.0f), 1.0f, textColor, vertexIO); if (self->overflowMode == OVERFLOW_TRUNCATE) { clipVerticesInsideRect(lastIndex, vertexIO->indexCount - lastIndex, bounds, vertexIO); } } } UITooltip UIButton_getTooltipAtPosition(UIButton * self, float x, float y) { if (!self->enabled) { return UITooltip_none; } UITooltip tooltip = call_super_virtual(getTooltipAtPosition, self, x, y); tooltip.sidePreference = UISidePreference_bottom; return tooltip; } void UIButton_invokeAction(UIButton * self, unsigned int modifiers, double referenceTime) { if (self->actionCallback != NULL) { self->actionCallback(self, modifiers, referenceTime, self->actionCallbackContext); } }