/* 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 "uitoolkit/UIRadioButton.h" #include "uitoolkit/UIToolkitAppearance.h" #include "uitoolkit/UIToolkitContext.h" #include "uitoolkit/UIToolkitDrawing.h" #include #include #define stemobject_implementation UIRadioButton stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(getBounds); stemobject_vtable_entry(draw); stemobject_vtable_entry(invokeAction); stemobject_vtable_end(); UIRadioButton * UIRadioButton_create(String text, Vector2f position, Vector2f relativeOrigin, float width, UIOverflowMode overflowMode, bool checked, UIRadioGroup * radioGroup, UIRadioButtonActionCallback actionCallback, void * actionCallbackContext, UIAppearance appearance) { stemobject_create_implementation(init, text, position, relativeOrigin, width, overflowMode, checked, radioGroup, actionCallback, actionCallbackContext, appearance) } static void updateWidth(UIRadioButton * self) { UIAtlasEntry atlasEntry = getAppearanceAtlasEntry(self->appearance, UIRadioButton_radioUp); float textPadding = getAppearanceFloat(self->appearance, UIRadioButton_textPadding); UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, UIToolkit_currentContext()->drawingInterface); self->width = atlasEntry.size.x + textPadding + ceilf(call_virtual(measureString, typeface, self->text)); if (self->width < self->minWidth) { self->width = self->minWidth; } } bool UIRadioButton_init(UIRadioButton * self, String text, Vector2f position, Vector2f relativeOrigin, float width, UIOverflowMode overflowMode, bool checked, UIRadioGroup * radioGroup, UIRadioButtonActionCallback actionCallback, void * actionCallbackContext, UIAppearance appearance) { call_super(init, self, text, position, relativeOrigin, width, overflowMode, NULL, actionCallbackContext, appearance); self->checked = checked; self->radioButtonActionCallback = actionCallback; if (overflowMode == OVERFLOW_RESIZE) { updateWidth(self); } else { self->width = width; } self->radioGroup = radioGroup; UIRadioGroup_incrementReferenceCount(self->radioGroup); if (self->checked) { UIRadioGroup_setCheckedButton(self->radioGroup, self); } return true; } void UIRadioButton_dispose(UIRadioButton * self) { UIRadioGroup_decrementReferenceCount(self->radioGroup); call_super(dispose, self); } void UIRadioButton_setText(UIRadioButton * self, String text) { String_free(self->text); self->text = String_copy(text); if (self->overflowMode == OVERFLOW_RESIZE) { updateWidth(self); } } void UIRadioButton_check(UIRadioButton * self) { self->checked = true; UIRadioGroup_setCheckedButton(self->radioGroup, self); } void UIRadioButton_uncheck(UIRadioButton * self) { self->checked = false; } Rect4f UIRadioButton_getBounds(UIRadioButton * self) { UIAtlasEntry atlasEntry = getAppearanceAtlasEntry(self->appearance, UIRadioButton_radioUp); UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, UIToolkit_currentContext()->drawingInterface); float textHeight = call_virtual(getLineHeight, typeface); float height = atlasEntry.size.y; if (textHeight > height) { height = textHeight; } return UIElement_boundsRectWithOrigin(self->position, self->relativeOrigin, VECTOR2f(self->width, height)); } void UIRadioButton_draw(UIRadioButton * 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->checked) { atlasEntry = getAppearanceAtlasEntry(self->appearance, UIRadioButton_radioDownChecked); } else { atlasEntry = getAppearanceAtlasEntry(self->appearance, UIRadioButton_radioDown); } } else if ((self->rollover && drawingInterface->drawMouseRolloverState) || (drawingInterface->drawFocusedElementInRolloverState && call_virtual(getFocusedElement, UIElement_getTopParent(self)) == (UIElement *) self)) { if (self->checked) { atlasEntry = getAppearanceAtlasEntry(self->appearance, UIRadioButton_radioRolloverChecked); } else { atlasEntry = getAppearanceAtlasEntry(self->appearance, UIRadioButton_radioRollover); } } else { if (self->checked) { atlasEntry = getAppearanceAtlasEntry(self->appearance, UIRadioButton_radioUpChecked); } else { atlasEntry = getAppearanceAtlasEntry(self->appearance, UIRadioButton_radioUp); } } 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), COLOR4f(1.0f, 1.0f, 1.0f, 1.0f), vertexIO); unsigned int lastIndex = vertexIO->indexCount; UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, drawingInterface); call_virtual(drawString, drawingInterface, typeface, self->text, VECTOR2f(roundpositivef(bounds.xMin + atlasEntry.size.x + getAppearanceFloat(self->appearance, UIRadioButton_textPadding)), roundpositivef(bounds.yMin + (bounds.yMax - bounds.yMin) * 0.5f)), VECTOR2f(0.0f, 0.5f), 1.0f, getAppearanceColor4f(self->appearance, UIRadioButton_textColor), vertexIO); if (self->overflowMode == OVERFLOW_TRUNCATE) { clipVerticesInsideRect(lastIndex, vertexIO->indexCount - lastIndex, bounds, vertexIO); } } void UIRadioButton_invokeAction(UIRadioButton * self, unsigned int modifiers, double referenceTime) { if (!self->checked) { self->checked = true; UIRadioGroup_setCheckedButton(self->radioGroup, self); if (self->radioButtonActionCallback != NULL) { self->radioButtonActionCallback(self, modifiers, referenceTime, self->actionCallbackContext); } } }