/* 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/UILabel.h" #include "uitoolkit/UIToolkitAppearance.h" #include "uitoolkit/UIToolkitContext.h" #include "uitoolkit/UIToolkitDrawing.h" #include #include #define stemobject_implementation UILabel stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(getBounds); stemobject_vtable_entry(draw); stemobject_vtable_entry(setText); stemobject_vtable_end(); UILabel * UILabel_create(String text, Vector2f position, Vector2f size, Vector2f relativeOrigin, TextAlignMode alignMode, WordWrapBehavior wrapBehavior, UIOverflowMode overflowModeX, UIOverflowMode overflowModeY, UIAppearance appearance) { stemobject_create_implementation(init, text, position, size, relativeOrigin, alignMode, wrapBehavior, overflowModeX, overflowModeY, appearance) } static void updateSize(UILabel * self) { if (self->overflowModeX == OVERFLOW_RESIZE) { self->size.x = ceilf(call_virtual(measureString, self->textLayout).x); } else { call_virtual(setWrapWidth, self->textLayout, self->size.x); } if (self->overflowModeY == OVERFLOW_RESIZE) { self->size.y = ceilf(call_virtual(getLineCount, self->textLayout) * call_virtual(getLineHeight, self->textLayout->typeface)); } self->lastSize = self->size; } bool UILabel_init(UILabel * self, String text, Vector2f position, Vector2f size, Vector2f relativeOrigin, TextAlignMode alignMode, WordWrapBehavior wrapBehavior, UIOverflowMode overflowModeX, UIOverflowMode overflowModeY, UIAppearance appearance) { call_super(init, self, position, relativeOrigin, appearance); UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, UIToolkit_currentContext()->drawingInterface); self->textLayout = UITextLayout_create(typeface, text, alignMode, wrapBehavior, size.x); self->overflowModeX = overflowModeX; self->overflowModeY = overflowModeY; self->size = size; updateSize(self); return true; } void UILabel_dispose(UILabel * self) { UITextLayout_dispose(self->textLayout); call_super(dispose, self); } UILabel * UILabel_createSimple(String text, Vector2f position, Vector2f relativeOrigin, UIAppearance appearance) { stemobject_create_implementation(initSimple, text, position, relativeOrigin, appearance) } bool UILabel_initSimple(UILabel * self, String text, Vector2f position, Vector2f relativeOrigin, UIAppearance appearance) { TextAlignMode alignMode = ALIGN_LEFT; if (relativeOrigin.x >= 0.25f) { if (relativeOrigin.x > 0.75f) { alignMode = ALIGN_RIGHT; } else { alignMode = ALIGN_CENTER; } } return UILabel_init(self, text, position, VECTOR2f_ZERO, relativeOrigin, alignMode, 0, OVERFLOW_RESIZE, OVERFLOW_RESIZE, appearance); } void UILabel_setText(UILabel * self, String text) { UITextLayout_setString(self->textLayout, text); updateSize(self); } String UILabel_getText(UILabel * self) { return self->textLayout->string; } Rect4f UILabel_getBounds(UILabel * self) { UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, UIToolkit_currentContext()->drawingInterface); if (typeface != self->textLayout->typeface) { call_virtual(setTypeface, self->textLayout, typeface); } if (self->size.x != self->lastSize.x || self->size.y != self->lastSize.y || self->textLayout->dirty) { updateSize(self); } return UIElement_boundsRectWithOrigin(self->position, self->relativeOrigin, VECTOR2f(self->size.x, self->size.y)); } static Vector2f getTextLayoutPosition(UILabel * self) { Rect4f bounds = call_virtual(getBounds, self); Vector2f position = {.x = 0.0f, .y = roundpositivef(bounds.yMax)}; switch (self->textLayout->alignMode) { case ALIGN_LEFT: position.x = bounds.xMin; break; case ALIGN_CENTER: position.x = bounds.xMin + (bounds.xMax - bounds.xMin) * 0.5f; break; case ALIGN_RIGHT: position.x = bounds.xMax; break; } return position; } void UILabel_draw(UILabel * self, Vector2f offset, UIDrawingInterface * drawingInterface, VertexIO * vertexIO) { call_super(draw, self, offset, drawingInterface, vertexIO); if (!self->visible) { return; } Rect4f bounds = Rect4f_offset(call_virtual(getBounds, self), offset); Color4f backgroundColor = getAppearanceColor4f(self->appearance, UILabel_backgroundColor); UIAtlasEntry whiteAtlasEntry = getAppearanceAtlasEntry(self->appearance, UIToolkit_white); Rect4f outset = getAppearanceRect4f(self->appearance, UILabel_backgroundMarginOutset); Rect4f backgroundRect = Rect4f_outsetMargins(bounds, outset); if (backgroundColor.alpha > 0.0f) { call_virtual(drawQuad, drawingInterface, backgroundRect, whiteAtlasEntry.bounds, backgroundColor, vertexIO); } Color4f borderColor = getAppearanceColor4f(self->appearance, UILabel_borderColor); if (borderColor.alpha > 0.0f) { UIToolkit_drawRectOutline(backgroundRect, 1, borderColor, whiteAtlasEntry.bounds, drawingInterface, vertexIO); } unsigned int lastIndex = vertexIO->indexCount; call_virtual(drawTextLayout, drawingInterface, self->textLayout, Vector2f_add(getTextLayoutPosition(self), offset), VECTOR2f(0.0f, 1.0f), 1.0f, getAppearanceColor4f(self->appearance, UILabel_textColor), vertexIO); if (self->overflowModeX == OVERFLOW_TRUNCATE || self->overflowModeY == OVERFLOW_TRUNCATE) { Rect4f clipBounds = {-INFINITY, INFINITY, -INFINITY, INFINITY}; if (self->overflowModeX == OVERFLOW_TRUNCATE) { clipBounds.xMin = bounds.xMin; clipBounds.xMax = bounds.xMax; } if (self->overflowModeY == OVERFLOW_TRUNCATE) { clipBounds.yMin = bounds.yMin; clipBounds.yMax = bounds.yMax; } clipVerticesInsideRect(lastIndex, vertexIO->indexCount - lastIndex, clipBounds, vertexIO); } }