Update text rendering.

This commit is contained in:
jmsgrogan 2023-01-18 17:31:16 +00:00
parent 8536908eab
commit 8130308f7f
27 changed files with 503 additions and 77 deletions

View file

@ -6,6 +6,8 @@
#include "ThemeManager.h"
#include "PaintEvent.h"
#include "FontTokens.h"
#include "MouseEvent.h"
#include "FileLogger.h"
@ -16,6 +18,10 @@ Button::Button(ButtonData::Component component)
{
mStyle.mComponent = component;
mName = "Button";
setHeight(mStyle.getContainerHeight());
setMaxHeight(mStyle.getContainerHeight());
setRadius(mStyle.getContainerCornerRadius());
}
Button::~Button()
@ -66,7 +72,39 @@ void Button::setEnabled(bool isEnabled)
void Button::updateState()
{
setBackground(mStyle.getContainerColor());
setBackgroundTone(mStyle.getContainerSurfaceTintColor());
setElevation(mStyle.getContainerElevation());
setLabelTextColor(mStyle.getLabelTextColor());
setLabelTextOpacity(mStyle.getLabelOpacity());
setLabelTextTypescale(mStyle.getLabelTypescale());
}
void Button::setLabelTextColor(Theme::Sys::Color color)
{
if (mLabelTextColor != color)
{
mLabelTextColor = color;
mMaterialDirty = true;
}
}
void Button::setLabelTextOpacity(float opacity)
{
if (mLabelOpacity != opacity)
{
mLabelOpacity = opacity;
mMaterialDirty = true;
}
}
void Button::setLabelTextTypescale(Theme::Sys::Typescale typescale)
{
if (mLabelTextTypescale != typescale)
{
mLabelTextTypescale = typescale;
mMaterialDirty = true;
}
}
void Button::onMyMouseEvent(const MouseEvent* event)
@ -131,29 +169,31 @@ void Button::doPaint(const PaintEvent* event)
void Button::updateLabel(const PaintEvent* event)
{
unsigned fontOffset = unsigned(mLabel.size()) * 4;
auto middle = DiscretePoint(mLocation.getX() + mSize.mWidth/2 - fontOffset, mLocation.getY() + mSize.mHeight/2 + 4);
if (!mTextNode)
{
mTextNode = TextNode::Create(mLabel, middle);
mTextNode = TextNode::Create(mLabel, mLocation);
mTextNode->setName(mName + "_TextNode");
mTextNode->setContent(mLabel);
mTextNode->setWidth(mSize.mWidth);
mTextNode->setHeight(mSize.mHeight);
mRootNode->addChild(mTextNode.get());
}
if (mTransformDirty)
{
mTextNode->setLocation(middle);
mTextNode->setLocation(mLocation);
mTextNode->setWidth(mSize.mWidth);
mTextNode->setHeight(mSize.mHeight);
}
if (mMaterialDirty)
{
mTextNode->setFillColor(event->getThemesManager()->getColor(mBackground));
auto fill_color = event->getThemesManager()->getColor(mLabelTextColor);
fill_color.setAlpha(mLabelOpacity);
mTextNode->setFillColor(fill_color);
auto size = FontTokens::getSize(mLabelTextTypescale);
auto family = FontTokens::getFont(FontTokens::getFont(mLabelTextTypescale));
auto font_data = FontItem(family, static_cast<float>(size));
mTextNode->setFont(font_data);
}
if (mContentDirty)

View file

@ -34,6 +34,10 @@ protected:
bool isDirty() const override;
void doPaint(const PaintEvent* event) override;
void setLabelTextColor(Theme::Sys::Color color);
void setLabelTextOpacity(float opacity);
void setLabelTextTypescale(Theme::Sys::Typescale typescale);
void updateLabel(const PaintEvent* event);
void setState(ButtonData::State state);
@ -44,6 +48,10 @@ private:
ButtonData mStyle;
std::string mLabel;
Theme::Sys::Color mLabelTextColor;
Theme::Sys::Typescale mLabelTextTypescale;
float mLabelOpacity{ 1.0 };
clickFunc mClickFunc;
std::unique_ptr<TextNode> mTextNode;

View file

@ -218,6 +218,29 @@ float ButtonData::getStateLayerOverlayOpacity() const
}
}
float ButtonData::getLabelOpacity() const
{
if (auto iter = mLabelTextOpacity.find(std::make_pair(mComponent, mState)); iter != mLabelTextOpacity.end())
{
return iter->second;
}
else
{
if (mState == State::Enabled)
{
return DEFAULT_CONTAINER_OPACITY;
}
else if (auto iter = mLabelTextOpacity.find(std::make_pair(mComponent, State::Enabled)); iter != mLabelTextOpacity.end())
{
return iter->second;
}
else
{
return DEFAULT_CONTAINER_OPACITY;
}
}
}
Theme::Sys::Typescale ButtonData::getLabelTypescale() const
{
switch (mComponent)

View file

@ -40,20 +40,21 @@ public:
Theme::Sys::Color getContainerShadowColor() const;
Theme::Sys::Color getContainerSurfaceTintColor() const;
Theme::Sys::Elevation getContainerElevation() const;
float getStateLayerOverlayOpacity() const;
Theme::Sys::Color getLabelTextColor() const;
Theme::Sys::Typescale getLabelTypescale() const;
float getLabelOpacity() const;
bool canHaveIcon() const;
Theme::Sys::Color getIconColor() const;
float getStateLayerOverlayOpacity() const;
unsigned getContainerHeight() const;
unsigned getContainerCornerRadius() const;
unsigned getIconSize() const;
unsigned getLeftRightPadding() const;
unsigned getLeftRightPadding() const;
unsigned getLeftPaddingWithIcon() const;
unsigned getRightPaddingWithIcon() const;

View file

@ -0,0 +1,64 @@
#include "FontTokens.h"
std::string FontTokens::getFont(Theme::Ref::Typeface::Font font)
{
switch (font)
{
case Theme::Ref::Typeface::Font::Brand:
return "Segoe UI";
case Theme::Ref::Typeface::Font::Plain:
return "Segoe UI";
default:
return "Segoe UI";
}
}
Theme::Ref::Typeface::Font FontTokens::getFont(Theme::Sys::Typescale typescale)
{
switch (typescale)
{
case Theme::Sys::Typescale::Label_Large:
return Theme::Ref::Typeface::Font::Brand;
default:
return Theme::Ref::Typeface::Font::Brand;
}
}
unsigned FontTokens::getLineHeight(Theme::Sys::Typescale typescale)
{
switch (typescale)
{
case Theme::Sys::Typescale::Label_Large:
return 67;
default:
return 67;
}
}
unsigned FontTokens::getSize(Theme::Sys::Typescale typescale)
{
switch (typescale)
{
case Theme::Sys::Typescale::Label_Large:
return static_cast<unsigned>(57/3);
default:
return 57;
}
}
unsigned FontTokens::getTracking(Theme::Sys::Typescale typescale)
{
return 0;
}
unsigned FontTokens::getWeight(Theme::Ref::Typeface::Font font)
{
return 0;
}
Theme::Ref::Typeface::Font FontTokens::getWeight(Theme::Sys::Typescale typescale)
{
return Theme::Ref::Typeface::Font::Brand;
}

View file

@ -51,6 +51,7 @@ namespace Theme
class FontTokens
{
public:
static std::string getFont(Theme::Ref::Typeface::Font font);
static Theme::Ref::Typeface::Font getFont(Theme::Sys::Typescale typescale);

View file

@ -77,6 +77,25 @@ void BoxGeometry::setBounds(unsigned width, unsigned height)
}
}
void BoxGeometry::setWidth(unsigned width)
{
setBounds(width, mSize.mHeight);
}
void BoxGeometry::setHeight(unsigned height)
{
setBounds(mSize.mWidth, height);
}
void BoxGeometry::setMaxHeight(unsigned maxHieght)
{
if (mSize.mMaxHeight != maxHieght)
{
mTransformDirty = true;
mSize.mMaxHeight = maxHieght;
}
}
void BoxGeometry::setLocation(const DiscretePoint& loc)
{
if (mLocation != loc)

View file

@ -59,6 +59,12 @@ public:
const DiscretePoint& getLocation() const;
void setWidth(unsigned width);
void setHeight(unsigned height);
void setMaxHeight(unsigned maxHieght);
void setBounds(unsigned width, unsigned height);
void setSize(const BoundedSize& size);

View file

@ -16,7 +16,6 @@
#include <algorithm>
#include <iterator>
#include <iostream>
Widget::Widget()
: BoxGeometry(),
@ -73,6 +72,24 @@ void Widget::setBackground(Theme::Sys::Color token)
}
}
void Widget::setBackgroundTone(Theme::Sys::Color token)
{
if (mBackgroundTone != token)
{
mBackgroundTone = token;
mMaterialDirty = true;
}
}
void Widget::setElevation(Theme::Sys::Elevation elevation)
{
if (mElevation != elevation)
{
mElevation = elevation;
mMaterialDirty = true;
}
}
void Widget::setBackgroundOpacity(float opacity)
{
if (mBackgroundOpacity != opacity)
@ -173,6 +190,9 @@ void Widget::onPaintEvent(const PaintEvent* event)
}
doPaint(event);
mGeometryDirty = false;
mMaterialDirty = false;
mTransformDirty = false;
if (mVisibilityDirty)
{
@ -293,32 +313,30 @@ void Widget::onMyMouseEvent(const MouseEvent* event)
void Widget::createOrUpdateGeometry()
{
const auto deltaX = mSize.mWidth - mMargin.mLeft - mMargin.mRight;
const auto deltaY = mSize.mHeight - mMargin.mTop - mMargin.mBottom;
if (!mBackgroundNode)
{
unsigned locX = mLocation.getX() + mMargin.mLeft;
unsigned locY = mLocation.getY() + mMargin.mTop;
unsigned deltaX = mSize.mWidth - mMargin.mLeft - mMargin.mRight;
unsigned deltaY = mSize.mHeight - mMargin.mTop - mMargin.mBottom;
const auto locX = mLocation.getX() + mMargin.mLeft;
const auto locY = mLocation.getY() + mMargin.mTop;
mBackgroundNode = std::make_unique<RectangleNode>(DiscretePoint(locX, locY), deltaX, deltaY);
mBackgroundNode->setRadius(mRadius);
mBackgroundNode->setName(mName + "_BackgroundNode");
mRootNode->addChild(mBackgroundNode.get());
}
else
{
mBackgroundNode->setWidth(deltaX);
mBackgroundNode->setHeight(deltaY);
mBackgroundNode->setRadius(mRadius);
}
}
void Widget::updateTransform()
{
unsigned locX = mLocation.getX() + mMargin.mLeft;
unsigned locY = mLocation.getY() + mMargin.mTop;
unsigned deltaX = mSize.mWidth - mMargin.mLeft - mMargin.mRight;
unsigned deltaY = mSize.mHeight - mMargin.mTop - mMargin.mBottom;
mBackgroundNode->setWidth(deltaX);
mBackgroundNode->setHeight(deltaY);
const auto locX = mLocation.getX() + mMargin.mLeft;
const auto locY = mLocation.getY() + mMargin.mTop;
mBackgroundNode->setLocation(DiscretePoint(locX, locY));
}
@ -326,9 +344,16 @@ void Widget::updateMaterial(const PaintEvent* event)
{
if (mBackground != Theme::Sys::Color::None)
{
auto background_color = event->getThemesManager()->getColor(mBackground);
background_color.setAlpha(mBackgroundOpacity);
mBackgroundNode->setFillColor(background_color);
//if (mBackgroundTone == Theme::Sys::Color::None || mElevation == Theme::Sys::Elevation::Level_0)
//{
auto background_color = event->getThemesManager()->getColor(mBackground);
background_color.setAlpha(mBackgroundOpacity);
mBackgroundNode->setFillColor(background_color);
//}
//else
//{
//event->getThemesManager()->getColor(mBackground);
//}
}
else
{
@ -348,22 +373,14 @@ void Widget::updateMaterial(const PaintEvent* event)
void Widget::updateBackground(const PaintEvent* event)
{
unsigned locX = mLocation.getX() + mMargin.mLeft;
unsigned locY = mLocation.getY() + mMargin.mTop;
unsigned deltaX = mSize.mWidth - mMargin.mLeft - mMargin.mRight;
unsigned deltaY = mSize.mHeight - mMargin.mTop - mMargin.mBottom;
if (!mBackgroundNode)
if (mGeometryDirty)
{
mBackgroundNode = std::make_unique<RectangleNode>(DiscretePoint(locX, locY), deltaX, deltaY);
mBackgroundNode->setName(mName + "_BackgroundNode");
mRootNode->addChild(mBackgroundNode.get());
createOrUpdateGeometry();
}
if (mTransformDirty)
{
updateTransform();
mTransformDirty = false;
}
if (mMaterialDirty)

View file

@ -1,11 +1,13 @@
#pragma once
#include "FontItem.h"
#include "ITheme.h"
#include "WidgetState.h"
#include "BoxGeometry.h"
#include "TransformNode.h"
#include "ITheme.h"
#include "ElevationTokens.h"
#include <memory>
#include <vector>
#include <string>
@ -55,6 +57,10 @@ public:
void setBackground(Theme::Sys::Color token);
void setBackgroundTone(Theme::Sys::Color token);
void setElevation(Theme::Sys::Elevation elevation);
void setOutlineThickness(double thickness);
void setOutline(Theme::Sys::Color token);
@ -102,7 +108,9 @@ protected:
Theme::Sys::Color mBorder;
double mBorderThickness{0};
Theme::Sys::Color mBackgroundTone;
Theme::Sys::Color mBackground;
Theme::Sys::Elevation mElevation{ Theme::Sys::Elevation::Level_0 };
float mBackgroundOpacity{ 1.0 };
bool mVisible{true};