/* 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 "gamemath/Scalar.h" #include "shell/Shell.h" #include "shell/ShellKeyCodes.h" #include "uitoolkit/UIPopUpView.h" #include "uitoolkit/UIToolkitAppearance.h" #include "uitoolkit/UIToolkitDrawing.h" #include #define stemobject_implementation UIPopUpView stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(hitTest); stemobject_vtable_entry(hitTestList); stemobject_vtable_entry(mouseDown); stemobject_vtable_entry(scrollWheel); stemobject_vtable_entry(keyDown); stemobject_vtable_entry(getBounds); stemobject_vtable_entry(draw); stemobject_vtable_entry(listRenderables); stemobject_vtable_entry(setInvokerBounds); stemobject_vtable_entry(setSidePreference); stemobject_vtable_end(); #define POP_UP_HIT_TEST_PRIORITY 2000 #define POP_UP_DRAW_PRIORITY 100 UIPopUpView * UIPopUpView_create(Rect4f invokerBounds, UISidePreference sidePreference, Vector2f size, bool clipContents, UIPopUpView_callback closeCallback, void * callbackContext, UIAppearance appearance) { stemobject_create_implementation(init, invokerBounds, sidePreference, size, clipContents, closeCallback, callbackContext, appearance) } bool UIPopUpView_init(UIPopUpView * self, Rect4f invokerBounds, UISidePreference sidePreference, Vector2f size, bool clipContents, UIPopUpView_callback closeCallback, void * callbackContext, UIAppearance appearance) { call_super(init, self, VECTOR2f(0.0f, 0.0f), VECTOR2f(0.0f, 0.0f), size, clipContents, appearance); self->invokerBounds = invokerBounds; self->sidePreference = sidePreference; self->lastUIBounds = getAppearanceRect4f(appearance, UIToolkit_safeDisplayBounds); self->boundsDirty = true; self->closeCallback = closeCallback; self->callbackContext = callbackContext; self->renderable = createUIElementRenderableWithDefaultSettings(self, PRIMITIVE_TRIANGLES); return true; } void UIPopUpView_dispose(UIPopUpView * self) { call_super_virtual(dispose, self); } bool UIPopUpView_hitTest(UIPopUpView * self, float x, float y, UIHitTestType type, int * outPriority, bool * outForwardNext) { if (!self->visible) { return false; } *outPriority = POP_UP_HIT_TEST_PRIORITY; return true; } void UIPopUpView_hitTestList(UIPopUpView * self, float x, float y, UIHitTestType type, int priorityOffset, UIHitTestResultIO * resultIO) { call_super(hitTestList, self, x, y, type, priorityOffset + POP_UP_HIT_TEST_PRIORITY, resultIO); int priority = 0; bool forwardNext = false; if (call_virtual(hitTest, self, x, y, type, &priority, &forwardNext)) { call_virtual(addResult, resultIO, self, priorityOffset + priority, forwardNext); } } UIEventResponse UIPopUpView_mouseDown(UIPopUpView * self, unsigned int buttonNumber, unsigned int buttonMask, float x, float y, unsigned int modifiers, bool isFinalTarget, double referenceTime) { if (!Rect4f_containsVector2f(call_virtual(getBounds, self), VECTOR2f(x, y))) { self->closeCallback(self, self->callbackContext); } return RESPONSE_HANDLED; } UIEventResponse UIPopUpView_scrollWheel(UIPopUpView * self, float x, float y, int deltaX, int deltaY, unsigned int modifiers, bool isFinalTarget, double referenceTime) { Rect4f bounds = call_virtual(getBounds, self); if (Rect4f_containsVector2f(bounds, VECTOR2f(x, y))) { return RESPONSE_HANDLED; } return call_super(scrollWheel, self, x, y, deltaX, deltaY, modifiers, isFinalTarget, referenceTime); } UIEventResponse UIPopUpView_keyDown(UIPopUpView * self, unsigned int charCode, unsigned int keyCode, unsigned int modifiers, bool isRepeat, bool isFinalTarget, double referenceTime) { switch (keyCode) { case KEY_CODE_ESCAPE: case KEY_CODE_ENTER: case KEY_CODE_SPACE: self->closeCallback(self, self->callbackContext); return RESPONSE_HANDLED; } if (!isModifierKey(keyCode)) { Shell_systemBeep(); } return RESPONSE_HANDLED; } static void updateBounds(UIPopUpView * self) { Rect4f uiBounds = getAppearanceRect4f(self->appearance, UIToolkit_safeDisplayBounds); if (!Rect4f_isEqual(uiBounds, self->lastUIBounds)) { self->boundsDirty = true; self->lastUIBounds = uiBounds; } if (self->boundsDirty) { self->boundsDirty = false; Rect4f contentBounds = call_super_virtual(getBounds, self); float frameOutset = getAppearanceFloat(self->appearance, UIPopUpView_frameOutset); float invokerOffset = getAppearanceFloat(self->appearance, UIPopUpView_invokerOffset); Vector2f size = VECTOR2f(contentBounds.xMax - contentBounds.xMin + frameOutset * 2, contentBounds.yMax - contentBounds.yMin + frameOutset * 2); Rect4f safeUIBounds = Rect4f_offset(getAppearanceRect4f(self->appearance, UIToolkit_safeDisplayBounds), Vector2f_inverted(UIElement_getParentOffset(self))); Vector2f parentOffset = UIElement_getParentOffset(self->parent); UIToolkit_getBestInvokerAttachPoint(Rect4f_offset(self->invokerBounds, Vector2f_inverted(parentOffset)), size, self->sidePreference, frameOutset, invokerOffset, safeUIBounds, &self->tailPosition, &self->position, &self->relativeOrigin); if (self->relativeOrigin.x != 0.5f) { self->position.x -= frameOutset * (self->relativeOrigin.x * 2.0f - 1.0f); } else { self->position.y -= frameOutset * (self->relativeOrigin.y * 2.0f - 1.0f); } } } Rect4f UIPopUpView_getBounds(UIPopUpView * self) { updateBounds(self); return call_super_virtual(getBounds, self); } void UIPopUpView_draw(UIPopUpView * self, Vector2f offset, UIDrawingInterface * drawingInterface, VertexIO * vertexIO) { float frameOutset = getAppearanceFloat(self->appearance, UIPopUpView_frameOutset); Rect4f bounds = Rect4f_inset(Rect4f_offset(call_virtual(getBounds, self), offset), VECTOR2f_REPEAT(-frameOutset)); Color4f color = getAppearanceColor4f(self->appearance, UIPopUpView_backgroundColor); UIToolkit_drawPopUpFrameWithShadow(bounds, color, 0.375f, self->appearance, drawingInterface, vertexIO); UIToolkit_drawPopUpTail(bounds, Vector2f_add(self->tailPosition, offset), color, self->appearance, drawingInterface, vertexIO); } void UIPopUpView_listRenderables(UIPopUpView * self, RenderableIO * renderableIO, int drawOrderOffset, Rect4i clipBounds) { if (self->visible) { RenderableIO_addRenderable(renderableIO, self->renderable, drawOrderOffset + POP_UP_DRAW_PRIORITY, clipBounds); } call_super_virtual(listRenderables, self, renderableIO, drawOrderOffset + POP_UP_DRAW_PRIORITY, clipBounds); } void UIPopUpView_setInvokerBounds(UIPopUpView * self, Rect4f invokerBounds) { self->invokerBounds = invokerBounds; self->boundsDirty = true; } void UIPopUpView_setSidePreference(UIPopUpView * self, UISidePreference sidePreference) { self->sidePreference = sidePreference; self->boundsDirty = true; }