/* 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/ShellKeyCodes.h" #include "uitoolkit/UICheckbox.h" #include "uitoolkit/UIToolkitAppearance.h" #include "uitoolkit/UIToolkitContext.h" #include "uitoolkit/UIToolkitDrawing.h" #include "utilities/IOUtilities.h" #include #include #define stemobject_implementation UICheckbox stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(getBounds); stemobject_vtable_entry(draw); stemobject_vtable_entry(invokeAction); stemobject_vtable_end(); UICheckbox * UICheckbox_create(String text, Vector2f position, Vector2f relativeOrigin, float width, UIOverflowMode overflowMode, bool checked, UICheckboxActionCallback actionCallback, void * actionCallbackContext, UIAppearance appearance) { stemobject_create_implementation(init, text, position, relativeOrigin, width, overflowMode, checked, actionCallback, actionCallbackContext, appearance) } static void updateWidth(UICheckbox * self) { UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, UIToolkit_currentContext()->drawingInterface); float textPadding = getAppearanceFloat(self->appearance, UICheckbox_textPadding); UIAtlasEntry atlasEntry = getAppearanceAtlasEntry(self->appearance, UICheckbox_checkboxUp); self->width = ceilf(atlasEntry.size.x + textPadding + call_virtual(measureString, typeface, self->text)); if (self->width < self->minWidth) { self->width = self->minWidth; } } bool UICheckbox_init(UICheckbox * self, String text, Vector2f position, Vector2f relativeOrigin, float width, UIOverflowMode overflowMode, bool checked, UICheckboxActionCallback actionCallback, void * actionCallbackContext, UIAppearance appearance) { call_super(init, self, text, position, relativeOrigin, width, overflowMode, NULL, actionCallbackContext, appearance); self->checked = checked; self->mixed = false; self->checkboxActionCallback = actionCallback; self->minWidth = width; if (overflowMode == OVERFLOW_RESIZE) { updateWidth(self); } else { self->width = width; } return true; } void UICheckbox_dispose(UICheckbox * self) { call_super(dispose, self); } void UICheckbox_setText(UICheckbox * self, String text) { String_free(self->text); self->text = String_copy(text); if (self->overflowMode == OVERFLOW_RESIZE) { updateWidth(self); } self->dirty = true; } Rect4f UICheckbox_getBounds(UICheckbox * self) { UIAtlasEntry atlasEntry = getAppearanceAtlasEntry(self->appearance, UICheckbox_checkboxUp); UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, UIToolkit_currentContext()->drawingInterface); float height = call_virtual(getLineHeight, typeface); if (atlasEntry.size.y > height) { height = atlasEntry.size.y; } return UIElement_boundsRectWithOrigin(self->position, self->relativeOrigin, VECTOR2f(self->width, height)); } void UICheckbox_draw(UICheckbox * 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) { if (self->mixed) { atlasEntry = getAppearanceAtlasEntry(self->appearance, UICheckbox_checkboxDownMixed); } else if (self->checked) { atlasEntry = getAppearanceAtlasEntry(self->appearance, UICheckbox_checkboxDownChecked); } else { atlasEntry = getAppearanceAtlasEntry(self->appearance, UICheckbox_checkboxDown); } } else if ((self->rollover && drawingInterface->drawMouseRolloverState && self->enabled) || (drawingInterface->drawFocusedElementInRolloverState && call_virtual(getFocusedElement, UIElement_getTopParent(self)) == (UIElement *) self)) { if (self->mixed) { atlasEntry = getAppearanceAtlasEntry(self->appearance, UICheckbox_checkboxRolloverMixed); } else if (self->checked) { atlasEntry = getAppearanceAtlasEntry(self->appearance, UICheckbox_checkboxRolloverChecked); } else { atlasEntry = getAppearanceAtlasEntry(self->appearance, UICheckbox_checkboxRollover); } } else { if (self->mixed) { atlasEntry = getAppearanceAtlasEntry(self->appearance, UICheckbox_checkboxUpMixed); } else if (self->checked) { atlasEntry = getAppearanceAtlasEntry(self->appearance, UICheckbox_checkboxUpChecked); } else { atlasEntry = getAppearanceAtlasEntry(self->appearance, UICheckbox_checkboxUp); } } float alpha = self->enabled ? 1.0f : 0.5f; Rect4f graphicBounds; graphicBounds.xMin = bounds.xMin; graphicBounds.xMax = graphicBounds.xMin + atlasEntry.size.x; graphicBounds.yMin = roundpositivef(bounds.yMin + (bounds.yMax - bounds.yMin - atlasEntry.size.y) * 0.5f); graphicBounds.yMax = graphicBounds.yMin + atlasEntry.size.y; call_virtual(drawQuad, drawingInterface, graphicBounds, UIAtlasEntry_boundsForScale(atlasEntry, drawingInterface->scaleFactor), drawingInterface->alphaPremultiplied ? COLOR4f(alpha, alpha, alpha, alpha) : COLOR4f(1.0f, 1.0f, 1.0f, alpha), vertexIO); if (self->text.bytes != NULL) { UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, drawingInterface); float textPadding = getAppearanceFloat(self->appearance, UICheckbox_textPadding); Color4f textColor = getAppearanceColor4f(self->appearance, UICheckbox_textColor); if (!self->enabled) { if (drawingInterface->alphaPremultiplied) { textColor.red *= 0.5f; textColor.green *= 0.5f; textColor.blue *= 0.5f; } textColor.alpha *= 0.5f; } unsigned int lastIndex = vertexIO->indexCount; call_virtual(drawString, drawingInterface, typeface, self->text, VECTOR2f(roundpositivef(bounds.xMin + atlasEntry.size.x + textPadding), roundpositivef(bounds.yMin + (bounds.yMax - bounds.yMin) * 0.5f)), VECTOR2f(0.0f, 0.5f), 1.0f, textColor, vertexIO); if (self->overflowMode == OVERFLOW_TRUNCATE) { clipVerticesInsideRect(lastIndex, vertexIO->indexCount - lastIndex, bounds, vertexIO); } } } void UICheckbox_invokeAction(UICheckbox * self, unsigned int modifiers, double referenceTime) { if (self->mixed) { self->mixed = false; self->checked = !(modifiers & MODIFIER_CONTROL_BIT); } else { self->checked = !self->checked; } if (self->checkboxActionCallback != NULL) { self->checkboxActionCallback(self, self->checked, modifiers, referenceTime, self->actionCallbackContext); } }