/* 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 "gamemath/Scalar.h" #include "shell/ShellKeyCodes.h" #include "uitoolkit/UIPopUpMenu.h" #include "uitoolkit/UIToolkitAppearance.h" #include "uitoolkit/UIToolkitContext.h" #include "uitoolkit/UIToolkitDrawing.h" #define stemobject_implementation UIPopUpMenu 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(keyDown); stemobject_vtable_entry(menuActionDown); stemobject_vtable_entry(menuActionUp); stemobject_vtable_entry(menuDirectionDown); stemobject_vtable_entry(acceptsFocus); stemobject_vtable_entry(ignoreClipForHitTest); stemobject_vtable_entry(getBounds); stemobject_vtable_entry(draw); stemobject_vtable_entry(listRenderables); stemobject_vtable_end(); #define SINGLE_CLICK_INTERVAL 0.3 UIPopUpMenu * UIPopUpMenu_create(unsigned int itemCount, UITextListView_item * items, Vector2f position, Vector2f relativeOrigin, float width, UIOverflowMode overflowMode, UIPopUpMenuActionCallback actionCallback, void * actionCallbackContext, UIAppearance appearance) { stemobject_create_implementation(init, itemCount, items, position, relativeOrigin, width, overflowMode, actionCallback, actionCallbackContext, appearance) } static void drawClosed(Vector2f offset, UIDrawingInterface * drawingInterface, VertexIO * vertexIO, void * context) { UIPopUpMenu * self = context; if (!self->visible) { return; } Rect4f bounds = Rect4f_offset(call_virtual(getBounds, self), offset); UIAtlasEntry frameAtlasEntry; if (self->rollover) { frameAtlasEntry = getAppearanceAtlasEntry(self->appearance, UIPopUpMenu_frameRollover); } else { frameAtlasEntry = getAppearanceAtlasEntry(self->appearance, UIPopUpMenu_frame); } call_virtual(drawSlicedQuad3x3, drawingInterface, bounds, UIAtlasEntry_boundsForScale(frameAtlasEntry, drawingInterface->scaleFactor), getAppearanceSliceGrid3x3(self->appearance, UIPopUpMenu_frameSlices), getAppearanceColor4f(self->appearance, UIPopUpMenu_backgroundColor), vertexIO); Color4f textColor = getAppearanceColor4f(self->appearance, UIPopUpMenu_textColor); if (!self->enabled) { if (drawingInterface->alphaPremultiplied) { textColor.red *= 0.5f; textColor.green *= 0.5f; textColor.blue *= 0.5f; } textColor.alpha *= 0.5f; } if (self->selectedItemIndex < self->itemCount) { unsigned int lastIndex = vertexIO->indexCount; UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, drawingInterface); Rect4f textPadding = getAppearanceRect4f(self->appearance, UIPopUpMenu_textPadding); float lineHeight = call_virtual(getLineHeight, typeface); call_virtual(drawString, drawingInterface, typeface, self->items[self->selectedItemIndex].text, VECTOR2f(roundpositivef(bounds.xMin + textPadding.xMin), roundpositivef(bounds.yMin + textPadding.yMin + (bounds.yMax - bounds.yMin - lineHeight - 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); } } UIAtlasEntry indicatorAtlasEntry = getAppearanceAtlasEntry(self->appearance, UIPopUpMenu_popUpMenuIndicator); Rect4f indicatorRect; indicatorRect.xMax = bounds.xMax - 2; indicatorRect.xMin = indicatorRect.xMax - indicatorAtlasEntry.size.x; indicatorRect.yMin = bounds.yMin + 2; indicatorRect.yMax = indicatorRect.yMin + indicatorAtlasEntry.size.y; Color4f indicatorColor = COLOR4f(1.0f, 1.0f, 1.0f, self->enabled ? 1.0f : 0.5f); if (drawingInterface->alphaPremultiplied) { indicatorColor = Color4f_premultiply(indicatorColor); } call_virtual(drawQuad, drawingInterface, indicatorRect, UIAtlasEntry_boundsForScale(indicatorAtlasEntry, drawingInterface->scaleFactor), indicatorColor, vertexIO); } static Rect4f getOpenBounds(UIPopUpMenu * self) { Rect4f closedBounds = call_virtual(getBounds, self); Rect4f openBounds; openBounds.xMin = closedBounds.xMin; openBounds.xMax = closedBounds.xMin + fmaxf(self->width, self->itemMaxWidth); unsigned int rowsAbove = self->openCenterIndex; if (rowsAbove > self->openItemCountMaxAbove) { rowsAbove = self->openItemCountMaxAbove; } unsigned int rowsBelow = self->itemCount - self->openCenterIndex - 1; if (rowsBelow == UINT_MAX) { rowsBelow = 0; } if (rowsBelow > self->openItemCountMaxBelow) { rowsBelow = self->openItemCountMaxBelow; } openBounds.yMax = closedBounds.yMax + rowsAbove * self->rowHeight; openBounds.yMin = openBounds.yMax - self->rowHeight * (rowsAbove + 1 + rowsBelow) - 2; Rect4f safeBounds = UIElement_rootToLocalRect(self, getAppearanceRect4f(self->appearance, UIToolkit_safeDisplayBounds)); openBounds = Rect4f_constrainWithin(openBounds, safeBounds); return openBounds; } static unsigned int getTopOpenItemIndex(UIPopUpMenu * self) { unsigned int topItemIndex = 0; if (self->openCenterIndex > self->openItemCountMaxAbove) { topItemIndex = self->openCenterIndex - self->openItemCountMaxAbove; } return topItemIndex; } static unsigned int getBottomOpenItemIndex(UIPopUpMenu * self) { unsigned int bottomItemIndex = self->itemCount - 1; if (self->itemCount >= self->openItemCountMaxBelow && self->openCenterIndex + self->openItemCountMaxBelow < self->itemCount - 1) { bottomItemIndex = self->openCenterIndex + self->openItemCountMaxBelow; } return bottomItemIndex; } static void drawOpen(Vector2f offset, UIDrawingInterface * drawingInterface, VertexIO * vertexIO, void * context) { UIPopUpMenu * self = context; if (!self->visible) { return; } Rect4f bounds = UIElement_localToRootRect(self, getOpenBounds(self)); float dropShadowOutset = getAppearanceFloat(self->appearance, UIMenuBar_dropShadowOutset); Vector2f dropShadowOffset = getAppearanceVector2f(self->appearance, UIMenuBar_dropShadowOffset); call_virtual(drawSlicedQuad3x3, drawingInterface, Rect4f_offset(Rect4f_inset(bounds, VECTOR2f(-dropShadowOutset, -dropShadowOutset)), dropShadowOffset), UIAtlasEntry_boundsForScale(getAppearanceAtlasEntry(self->appearance, UIMenuBar_menuDropShadow), drawingInterface->scaleFactor), getAppearanceSliceGrid3x3(self->appearance, UIMenuBar_dropShadowSlices), getAppearanceColor4f(self->appearance, UIMenuBar_dropShadowColor), vertexIO); call_virtual(drawSlicedQuad3x3, drawingInterface, bounds, UIAtlasEntry_boundsForScale(getAppearanceAtlasEntry(self->appearance, UIPopUpMenu_frame), drawingInterface->scaleFactor), getAppearanceSliceGrid3x3(self->appearance, UIPopUpMenu_frameSlices), getAppearanceColor4f(self->appearance, UIPopUpMenu_backgroundColor), vertexIO); unsigned int topItemIndex = getTopOpenItemIndex(self); unsigned int bottomItemIndex = getBottomOpenItemIndex(self); unsigned int topItemIndexWithoutScrollArrow = topItemIndex; if (self->highlightedItemIndex < self->itemCount && (topItemIndex == 0 || self->highlightedItemIndex != topItemIndex) && (bottomItemIndex == self->itemCount - 1 || self->highlightedItemIndex != bottomItemIndex)) { Rect4f highlightBounds; highlightBounds.xMin = bounds.xMin + 1; highlightBounds.xMax = bounds.xMax - 1; highlightBounds.yMax = bounds.yMax - self->rowHeight * (self->highlightedItemIndex - topItemIndex) - 1; highlightBounds.yMin = highlightBounds.yMax - self->rowHeight; call_virtual(drawQuad, drawingInterface, highlightBounds, getAppearanceAtlasEntry(self->appearance, UIToolkit_white).bounds, getAppearanceColor4f(self->appearance, UIPopUpMenu_highlightColor), vertexIO); } UIAtlasEntry arrowAtlasEntry = getAppearanceAtlasEntry(self->appearance, UIPopUpMenu_popUpScrollArrow); Rect4f arrowAtlasEntryBounds = UIAtlasEntry_boundsForScale(arrowAtlasEntry, drawingInterface->scaleFactor); Rect4f arrowBounds; arrowBounds.xMin = roundpositivef(bounds.xMin + (bounds.xMax - bounds.xMin - arrowAtlasEntry.size.x) / 2); arrowBounds.xMax = arrowBounds.xMin + arrowAtlasEntry.size.x; if (topItemIndex > 0) { Rect4f arrowAtlasEntryFlipped = arrowAtlasEntryBounds; arrowAtlasEntryFlipped.yMax = arrowAtlasEntryBounds.yMin; arrowAtlasEntryFlipped.yMin = arrowAtlasEntryBounds.yMax; arrowBounds.yMax = roundpositivef(bounds.yMax - (self->rowHeight - arrowAtlasEntry.size.y) / 2 - 1); arrowBounds.yMin = arrowBounds.yMax - arrowAtlasEntry.size.y; call_virtual(drawQuad, drawingInterface, arrowBounds, arrowAtlasEntryFlipped, COLOR4f(1.0f, 1.0f, 1.0f, 1.0f), vertexIO); topItemIndex++; } if (bottomItemIndex < self->itemCount - 1) { arrowBounds.yMin = roundpositivef(bounds.yMin + (self->rowHeight - arrowAtlasEntry.size.y) / 2 + 1); arrowBounds.yMax = arrowBounds.yMin + arrowAtlasEntry.size.y; call_virtual(drawQuad, drawingInterface, arrowBounds, arrowAtlasEntryBounds, COLOR4f(1.0f, 1.0f, 1.0f, 1.0f), vertexIO); bottomItemIndex--; } if (bottomItemIndex != UINT_MAX) { UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, drawingInterface); float lineHeight = call_virtual(getLineHeight, typeface); Color4f textColor = getAppearanceColor4f(self->appearance, UIPopUpMenu_textColor); Rect4f textPadding = getAppearanceRect4f(self->appearance, UIPopUpMenu_textPadding); for (unsigned int itemIndex = topItemIndex; itemIndex <= bottomItemIndex; itemIndex++) { call_virtual(drawString, drawingInterface, typeface, self->items[itemIndex].text, VECTOR2f(bounds.xMin + textPadding.xMin, bounds.yMax - self->rowHeight * (itemIndex - topItemIndexWithoutScrollArrow) - (self->rowHeight - lineHeight) / 2 - 1), VECTOR2f(0.0f, 1.0f), 1.0f, textColor, vertexIO); } } } static void measureItemMaxWidth(UIPopUpMenu * self) { float maxWidth = 20.0f; UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, UIToolkit_currentContext()->drawingInterface); Rect4f textPadding = getAppearanceRect4f(self->appearance, UIPopUpMenu_textPadding); for (unsigned int itemIndex = 0; itemIndex < self->itemCount; itemIndex++) { float itemWidth = ceilf(call_virtual(measureString, typeface, self->items[itemIndex].text) + textPadding.xMin + textPadding.xMax); if (maxWidth < itemWidth) { maxWidth = itemWidth; } } self->itemMaxWidth = maxWidth; } static void disposeItems(UIPopUpMenu * self) { for (unsigned int itemIndex = 0; itemIndex < self->itemCount; itemIndex++) { String_free(self->items[itemIndex].text); } free(self->items); } static void assignItems(UIPopUpMenu * self, unsigned int itemCount, UITextListView_item * items) { self->itemCount = itemCount; self->items = malloc(itemCount * sizeof(*self->items)); for (unsigned int itemIndex = 0; itemIndex < itemCount; itemIndex++) { self->items[itemIndex].text = String_copy(items[itemIndex].text); self->items[itemIndex].identifier = items[itemIndex].identifier; } } bool UIPopUpMenu_init(UIPopUpMenu * self, unsigned int itemCount, UITextListView_item * items, Vector2f position, Vector2f relativeOrigin, float width, UIOverflowMode overflowMode, UIPopUpMenuActionCallback actionCallback, void * actionCallbackContext, UIAppearance appearance) { call_super(initNoRenderable, self, position, relativeOrigin, appearance); self->renderable = createUIElementRenderableWithDefaultSettings(self, PRIMITIVE_TRIANGLES); self->actionCallback = actionCallback; self->actionCallbackContext = actionCallbackContext; assignItems(self, itemCount, items); self->selectedItemIndex = 0; self->enabled = true; self->highlightedItemIndex = 0; self->lastFocusedElement = NULL; self->rollover = false; self->open = false; self->openTime = 0.0; self->overflowMode = overflowMode; measureItemMaxWidth(self); if (overflowMode == OVERFLOW_RESIZE) { self->width = self->itemMaxWidth; } else { self->width = width; } Rect4f textPadding = getAppearanceRect4f(self->appearance, UIPopUpMenu_textPadding); UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, UIToolkit_currentContext()->drawingInterface); float lineHeight = call_virtual(getLineHeight, typeface); self->rowHeight = lineHeight + textPadding.yMin + textPadding.yMax - 2; self->scrollTimer = SHELL_TIMER_INVALID; self->scrollingFast = false; return true; } void UIPopUpMenu_dispose(UIPopUpMenu * self) { disposeItems(self); Shell_cancelTimer(self->scrollTimer); call_super_virtual(dispose, self); } bool UIPopUpMenu_hitTest(UIPopUpMenu * self, float x, float y, UIHitTestType type, int * outPriority, bool * outForwardNext) { if (self->open) { *outPriority = POP_UP_MENU_OPEN_HIT_TEST_PRIORITY; return true; } return call_super_virtual(hitTest, self, x, y, type, outPriority, outForwardNext); } static void openMenu(UIPopUpMenu * self, double referenceTime) { self->open = true; self->openTime = referenceTime; self->openCenterIndex = self->selectedItemIndex; if (self->openCenterIndex >= self->itemCount) { self->openCenterIndex = 0; } self->highlightedItemIndex = self->selectedItemIndex; if (self->highlightedItemIndex == ITEM_INDEX_NONE && self->itemCount > 0) { self->highlightedItemIndex = 0; } Rect4f safeBounds = UIElement_rootToLocalRect(self, getAppearanceRect4f(self->appearance, UIToolkit_safeDisplayBounds)); Rect4f closedBounds = call_virtual(getBounds, self); int rowsAbove = (safeBounds.yMax - closedBounds.yMax) / self->rowHeight; if (rowsAbove < 1) { self->openItemCountMaxAbove = 1; } else { self->openItemCountMaxAbove = rowsAbove; } int rowsBelow = (closedBounds.yMin - safeBounds.yMin) / self->rowHeight; if (rowsBelow < 1) { self->openItemCountMaxBelow = 1; } else { self->openItemCountMaxBelow = rowsBelow; } } static void closeMenu(UIPopUpMenu * self) { self->open = false; UIElement * topParent = UIElement_getTopParent(self); if (topParent != NULL && self->lastFocusedElement != (UIElement *) self) { if (self->lastFocusedElement == NULL) { UIElement_unfocus(self); } else { call_virtual(setFocusedElement, topParent, self->lastFocusedElement, NULL, UI_NONE); } } self->lastFocusedElement = NULL; } UIEventResponse UIPopUpMenu_mouseDown(UIPopUpMenu * self, unsigned int buttonNumber, unsigned int buttonMask, float x, float y, unsigned int modifiers, bool isFinalTarget, double referenceTime) { if (!self->visible || !self->enabled) { return RESPONSE_UNHANDLED; } if (self->open) { if (!Rect4f_containsVector2f(getOpenBounds(self), VECTOR2f(x, y))) { closeMenu(self); } else { self->openTime = 0.0; } } else { UIElement * topParent = UIElement_getTopParent(self); self->lastFocusedElement = call_virtual(getFocusedElement, topParent); if (self->lastFocusedElement != (UIElement *) self) { call_virtual(setFocusedElement, topParent, self, NULL, UI_NONE); } openMenu(self, referenceTime); } return RESPONSE_HANDLED; } bool UIPopUpMenu_mouseUp(UIPopUpMenu * self, unsigned int buttonNumber, unsigned int buttonMask, float x, float y, unsigned int modifiers, double referenceTime) { if (self->open && referenceTime - self->openTime >= SINGLE_CLICK_INTERVAL) { closeMenu(self); if (Rect4f_containsVector2f(getOpenBounds(self), VECTOR2f(x, y)) && self->itemCount > 0) { unsigned int topItemIndex = getTopOpenItemIndex(self); unsigned int bottomItemIndex = getBottomOpenItemIndex(self); if ((topItemIndex == 0 || self->highlightedItemIndex != topItemIndex) && (bottomItemIndex == self->itemCount - 1 || self->highlightedItemIndex != bottomItemIndex)) { self->selectedItemIndex = self->highlightedItemIndex; if (self->actionCallback != NULL) { self->actionCallback(self, self->items[self->selectedItemIndex].identifier, referenceTime, self->actionCallbackContext); } } } return true; } return false; } #define MOUSE_SCROLL_INTERVAL_SLOW 0.15 #define MOUSE_SCROLL_INTERVAL_FAST 0.05 static void scrollTimerCallback(ShellTimer timerID, void * context) { UIPopUpMenu * self = context; unsigned int topItemIndex = getTopOpenItemIndex(self); unsigned int bottomItemIndex = getBottomOpenItemIndex(self); if (self->highlightedItemIndex == topItemIndex && topItemIndex > 0) { self->openCenterIndex--; self->highlightedItemIndex--; Shell_redisplay(); } else if (self->highlightedItemIndex == bottomItemIndex && bottomItemIndex < self->itemCount - 1) { self->openCenterIndex++; self->highlightedItemIndex++; Shell_redisplay(); } else { Shell_cancelTimer(timerID); self->scrollTimer = SHELL_TIMER_INVALID; self->scrollingFast = false; } } static bool mouseMovedOrDragged(UIPopUpMenu * self, float x, float y) { if (self->open) { unsigned int lastHighlightedItemIndex = self->highlightedItemIndex; Rect4f openBounds = getOpenBounds(self); unsigned int topItemIndex = getTopOpenItemIndex(self); unsigned int bottomItemIndex = getBottomOpenItemIndex(self); if (Rect4f_containsVector2f(openBounds, VECTOR2f(x, y))) { self->highlightedItemIndex = ((openBounds.yMax - openBounds.yMin) - (y - openBounds.yMin)) / self->rowHeight + getTopOpenItemIndex(self); if (self->highlightedItemIndex > bottomItemIndex) { self->highlightedItemIndex = bottomItemIndex; } } else { if (topItemIndex > 0 && x >= openBounds.xMin && x <= openBounds.xMax && y > openBounds.yMax - 1 && !self->scrollingFast) { self->highlightedItemIndex = topItemIndex; if (self->scrollTimer == SHELL_TIMER_INVALID) { self->openCenterIndex--; self->highlightedItemIndex--; } else { Shell_cancelTimer(self->scrollTimer); } self->scrollTimer = Shell_setTimer(MOUSE_SCROLL_INTERVAL_FAST, true, scrollTimerCallback, self); self->scrollingFast = true; return true; } if (bottomItemIndex < self->itemCount - 1 && x >= openBounds.xMin && x <= openBounds.xMax && y < openBounds.yMin + 1 && !self->scrollingFast) { self->highlightedItemIndex = bottomItemIndex; if (self->scrollTimer == SHELL_TIMER_INVALID) { self->openCenterIndex++; self->highlightedItemIndex++; } else { Shell_cancelTimer(self->scrollTimer); } self->scrollTimer = Shell_setTimer(MOUSE_SCROLL_INTERVAL_FAST, true, scrollTimerCallback, self); self->scrollingFast = true; return true; } if (!self->scrollingFast || x < openBounds.xMin || x > openBounds.xMax) { self->highlightedItemIndex = ITEM_INDEX_NONE; } } if (self->highlightedItemIndex == topItemIndex && topItemIndex > 0) { if (self->scrollTimer == SHELL_TIMER_INVALID) { self->openCenterIndex--; self->highlightedItemIndex--; self->scrollTimer = Shell_setTimer(MOUSE_SCROLL_INTERVAL_SLOW, true, scrollTimerCallback, self); self->scrollingFast = false; } } else if (self->highlightedItemIndex == bottomItemIndex && bottomItemIndex < self->itemCount - 1) { if (self->scrollTimer == SHELL_TIMER_INVALID) { self->openCenterIndex++; self->highlightedItemIndex++; self->scrollTimer = Shell_setTimer(MOUSE_SCROLL_INTERVAL_SLOW, true, scrollTimerCallback, self); self->scrollingFast = false; } } else if (x < openBounds.xMin || x > openBounds.xMax || (x > openBounds.yMin && x < openBounds.yMax)) { Shell_cancelTimer(self->scrollTimer); self->scrollTimer = SHELL_TIMER_INVALID; self->scrollingFast = false; } return lastHighlightedItemIndex != self->highlightedItemIndex; } return false; } bool UIPopUpMenu_mouseMoved(UIPopUpMenu * 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)); bool wasOpen = self->open; self->open = false; self->rollover = UIElement_hitTestSingle(topParent, rootPosition.x, rootPosition.y, HIT_TEST_MOUSE_OVER) == (UIElement *) self; self->open = wasOpen; if (!self->open && self->rollover != wasRolledOver) { self->dirty = true; return true; } return mouseMovedOrDragged(self, x, y); } bool UIPopUpMenu_mouseDragged(UIPopUpMenu * self, unsigned int buttonMask, float x, float y, float deltaX, float deltaY, unsigned int modifiers, double referenceTime) { return mouseMovedOrDragged(self, x, y); } UIEventResponse UIPopUpMenu_scrollWheel(UIPopUpMenu * self, float x, float y, int deltaX, int deltaY, unsigned int modifiers, bool isFinalTarget, double referenceTime) { if (self->open) { unsigned int topItemIndex = getTopOpenItemIndex(self); unsigned int bottomItemIndex = getBottomOpenItemIndex(self); if (topItemIndex > 0 && deltaY < 0) { if ((unsigned int) -deltaY > topItemIndex) { deltaY = -topItemIndex; } self->openCenterIndex += deltaY; if (self->highlightedItemIndex != ITEM_INDEX_NONE) { self->highlightedItemIndex += deltaY; } return RESPONSE_HANDLED; } if (bottomItemIndex < self->itemCount - 1 && deltaY > 0) { if ((unsigned int) deltaY > self->itemCount - bottomItemIndex - 1) { deltaY = self->itemCount - bottomItemIndex - 1; } self->openCenterIndex += deltaY; if (self->highlightedItemIndex != ITEM_INDEX_NONE) { self->highlightedItemIndex += deltaY; } return RESPONSE_HANDLED; } return RESPONSE_IGNORE; } return RESPONSE_UNHANDLED; } UIEventResponse UIPopUpMenu_keyDown(UIPopUpMenu * self, unsigned int charCode, unsigned int keyCode, unsigned int modifiers, bool isRepeat, bool isFinalTarget, double referenceTime) { if (self->open) { // TODO: Keyboard controls if (keyCode == KEY_CODE_ESCAPE) { closeMenu(self); } return RESPONSE_HANDLED; } return call_super_virtual(keyDown, self, charCode, keyCode, modifiers, isRepeat, isFinalTarget, referenceTime); } bool UIPopUpMenu_menuActionDown(UIPopUpMenu * self, unsigned int actionNumber, bool isRepeat, double referenceTime) { if (!self->visible || !self->enabled) { return false; } if (actionNumber == 0 && !isRepeat) { if (self->open) { closeMenu(self); self->selectedItemIndex = self->highlightedItemIndex; if (self->actionCallback != NULL) { self->actionCallback(self, self->items[self->selectedItemIndex].identifier, referenceTime, self->actionCallbackContext); } } else { UIElement * topParent = UIElement_getTopParent(self); self->lastFocusedElement = call_virtual(getFocusedElement, topParent); openMenu(self, referenceTime); } return true; } if (actionNumber == 1 && self->open) { closeMenu(self); return true; } return false; } bool UIPopUpMenu_menuActionUp(UIPopUpMenu * self, unsigned int actionNumber, double referenceTime) { if (self->open && referenceTime - self->openTime >= SINGLE_CLICK_INTERVAL) { closeMenu(self); self->selectedItemIndex = self->highlightedItemIndex; if (self->actionCallback != NULL) { self->actionCallback(self, self->items[self->selectedItemIndex].identifier, referenceTime, self->actionCallbackContext); } return true; } return false; } static void scrollToHighlightedItem(UIPopUpMenu * self) { unsigned int topItemIndex = getTopOpenItemIndex(self); if (topItemIndex > 0 && self->highlightedItemIndex != 0) { topItemIndex++; } unsigned int bottomItemIndex = getBottomOpenItemIndex(self); if (bottomItemIndex < self->itemCount - 1 && self->highlightedItemIndex != self->itemCount - 1) { bottomItemIndex--; } if (self->highlightedItemIndex < topItemIndex) { self->openCenterIndex -= topItemIndex - self->highlightedItemIndex; } else if (self->highlightedItemIndex > bottomItemIndex) { self->openCenterIndex += self->highlightedItemIndex - bottomItemIndex; } } bool UIPopUpMenu_menuDirectionDown(UIPopUpMenu * self, UINavigationDirection direction, bool isRepeat, double referenceTime) { if (self->open) { unsigned int lastHighlightedItemIndex = self->highlightedItemIndex; if (direction == UI_DOWN || direction == UI_NEXT) { self->highlightedItemIndex = (self->highlightedItemIndex + 1) % self->itemCount; } else if (direction == UI_UP || direction == UI_PREVIOUS) { self->highlightedItemIndex = (self->highlightedItemIndex + self->itemCount -1) % self->itemCount; } scrollToHighlightedItem(self); return lastHighlightedItemIndex != self->highlightedItemIndex; } return call_super_virtual(menuDirectionDown, self, direction, isRepeat, referenceTime); } bool UIPopUpMenu_acceptsFocus(UIPopUpMenu * self) { return self->visible && self->enabled; } bool UIPopUpMenu_ignoreClipForHitTest(UIPopUpMenu * self, UIHitTestType type) { return self->open; } Rect4f UIPopUpMenu_getBounds(UIPopUpMenu * self) { return UIElement_boundsRectWithOrigin(self->position, self->relativeOrigin, VECTOR2f(self->width, self->rowHeight + 2)); } void UIPopUpMenu_draw(UIPopUpMenu * self, Vector2f offset, UIDrawingInterface * drawingInterface, VertexIO * vertexIO) { call_super(draw, self, offset, drawingInterface, vertexIO); drawClosed(offset, drawingInterface, vertexIO, self); if (self->open) { drawOpen(offset, drawingInterface, vertexIO, self); } } void UIPopUpMenu_listRenderables(UIPopUpMenu * self, RenderableIO * renderableIO, int drawOrderOffset, Rect4i clipBounds) { self->dirty = false; if (!self->visible) { return; } clipBounds = UIElement_intersectClipBounds(clipBounds, call_virtual(getAbsoluteClipBounds, self)); RenderableIO_addRenderable(renderableIO, self->renderable, drawOrderOffset + (self->open ? 1000 : 0), self->open ? RECT4i_EMPTY : clipBounds); } void UIPopUpMenu_setItems(UIPopUpMenu * self, unsigned int itemCount, UITextListView_item * items) { int selectedIdentifier = UIPopUpMenu_getSelectedItemIdentifier(self); disposeItems(self); assignItems(self, itemCount, items); UIPopUpMenu_setSelectedItemIdentifier(self, selectedIdentifier); measureItemMaxWidth(self); if (self->overflowMode == OVERFLOW_RESIZE) { self->width = self->itemMaxWidth; } } bool UIPopUpMenu_setSelectedItemIdentifier(UIPopUpMenu * self, int identifier) { if (identifier == ITEM_IDENTIFIER_NONE) { self->selectedItemIndex = ITEM_INDEX_NONE; return true; } for (unsigned int itemIndex = 0; itemIndex < self->itemCount; itemIndex++) { if (self->items[itemIndex].identifier == identifier) { self->selectedItemIndex = itemIndex; return true; } } return false; } int UIPopUpMenu_getSelectedItemIdentifier(UIPopUpMenu * self) { if (self->selectedItemIndex >= self->itemCount) { return ITEM_IDENTIFIER_NONE; } return self->items[self->selectedItemIndex].identifier; }