/* Copyright (c) 2022 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/UIModalDialog.h" #include "uitoolkit/UIToolkitAppearance.h" #define stemobject_implementation UIModalDialog stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(hitTest); stemobject_vtable_entry(hitTestList); stemobject_vtable_entry(mouseDown); stemobject_vtable_entry(keyDown); stemobject_vtable_entry(scrollWheel); stemobject_vtable_entry(draw); stemobject_vtable_entry(listRenderables); stemobject_vtable_entry(isModal); stemobject_vtable_entry(drawScreenOverlay); stemobject_vtable_entry(dialogKeyDown); stemobject_vtable_end(); bool UIModalDialog_init(UIModalDialog * self, Vector2f size, String title, UIWindowView_callback closeCallback, UIWindowView_resizeCallback resizeCallback, void * callbackContext, UIWindowRoot * windowRoot, UIAppearance appearance) { Rect4f uiBounds = getAppearanceRect4f(appearance, UIToolkit_safeDisplayBounds); call_super(init, self, VECTOR2f(roundpositivef(uiBounds.xMin + (uiBounds.xMax - uiBounds.xMin) / 2), roundpositivef(uiBounds.yMin + (uiBounds.yMax - uiBounds.yMin) / 2)), VECTOR2f(0.5f, 0.5f), size, title, closeCallback, resizeCallback, callbackContext, windowRoot, appearance); self->titleColorTop = getAppearanceColor4f(appearance, UIModalDialog_windowTitleColorTop); self->titleColorBottom = getAppearanceColor4f(appearance, UIModalDialog_windowTitleColorBottom); self->backgroundColor = getAppearanceColor4f(appearance, UIModalDialog_windowBackgroundColor); self->visible = true; self->protected_ivar(hitTestPriority) = MODAL_DIALOG_HIT_TEST_PRIORITY; return true; } void UIModalDialog_dispose(UIModalDialog * self) { call_super_virtual(dispose, self); } bool UIModalDialog_hitTest(UIModalDialog * self, float x, float y, UIHitTestType type, int * outPriority, bool * outForwardNext) { int priority = 0; call_super(hitTest, self, x, y, type, &priority, outForwardNext); *outPriority += self->protected_ivar(hitTestPriority); *outForwardNext = false; return true; } void UIModalDialog_hitTestList(UIModalDialog * self, float x, float y, UIHitTestType type, int priorityOffset, UIHitTestResultIO * resultIO) { call_super(hitTestList, self, x, y, type, priorityOffset + self->protected_ivar(hitTestPriority), resultIO); } UIEventResponse UIModalDialog_mouseDown(UIModalDialog * self, unsigned int buttonNumber, unsigned int buttonMask, float x, float y, unsigned int modifiers, bool isFinalTarget, double referenceTime) { Rect4f bounds = call_virtual(getBounds, self); if (self->resizeCallback != NULL) { float resizeAreaOutset = getAppearanceFloat(self->appearance, UIWindowView_resizeAreaOutset); bounds = Rect4f_inset(bounds, VECTOR2f(-resizeAreaOutset, -resizeAreaOutset)); } if (isFinalTarget && !Rect4f_containsVector2f(bounds, VECTOR2f(x, y))) { Shell_systemBeep(); return RESPONSE_HANDLED; } call_super_virtual(mouseDown, self, buttonNumber, buttonMask, x, y, modifiers, isFinalTarget, referenceTime); return RESPONSE_HANDLED; } UIEventResponse UIModalDialog_keyDown(UIModalDialog * self, unsigned int charCode, unsigned int keyCode, unsigned int modifiers, bool isRepeat, bool isFinalTarget, double referenceTime) { if (!isFinalTarget) { UIEventResponse response = call_super_virtual(keyDown, self, charCode, keyCode, modifiers, isRepeat, isFinalTarget, referenceTime); if (response == RESPONSE_HANDLED) { return response; } } return call_virtual(dialogKeyDown, self, charCode, keyCode, modifiers, isRepeat, referenceTime) ? RESPONSE_HANDLED : RESPONSE_UNHANDLED; } UIEventResponse UIModalDialog_scrollWheel(UIModalDialog * self, float x, float y, int deltaX, int deltaY, unsigned int modifiers, bool isFinalTarget, double referenceTime) { if (isFinalTarget && !Rect4f_containsVector2f(call_virtual(getBounds, self), VECTOR2f(x, y))) { return RESPONSE_HANDLED; } return call_super_virtual(scrollWheel, self, x, y, deltaX, deltaY, modifiers, isFinalTarget, referenceTime); } void UIModalDialog_draw(UIModalDialog * self, Vector2f offset, UIDrawingInterface * drawingInterface, VertexIO * vertexIO) { call_virtual(drawScreenOverlay, self, drawingInterface, vertexIO); call_super_virtual(draw, self, offset, drawingInterface, vertexIO); } void UIModalDialog_listRenderables(UIModalDialog * self, RenderableIO * renderableIO, int drawOrderOffset, Rect4i clipBounds) { call_super_virtual(listRenderables, self, renderableIO, drawOrderOffset + 1000, clipBounds); } bool UIModalDialog_isModal(UIModalDialog * self) { return true; } void UIModalDialog_drawScreenOverlay(UIModalDialog * self, UIDrawingInterface * drawingInterface, VertexIO * vertexIO) { Rect4f uiBounds = getAppearanceRect4f(self->appearance, UIToolkit_uiBounds); call_virtual(drawQuad, drawingInterface, uiBounds, getAppearanceAtlasEntry(self->appearance, UIToolkit_white).bounds, getAppearanceColor4f(self->appearance, UIModalDialog_screenDimColor), vertexIO); } bool UIModalDialog_dialogKeyDown(UIModalDialog * self, unsigned int charCode, unsigned int keyCode, unsigned int modifiers, bool isRepeat, double referenceTime) { // TODO: Can maybe remove this with new keyDown/hitTest handling if (!isModifierKey(keyCode)) { Shell_systemBeep(); } return true; }