增加颜色类,包含rgba四个属性

This commit is contained in:
Nomango 2018-04-22 13:15:57 +08:00
parent eb8b7e5f7e
commit 40ad1c7282
14 changed files with 171 additions and 67 deletions

View File

@ -230,9 +230,14 @@ void e2d::Renderer::__render()
}
void e2d::Renderer::setBackgroundColor(UINT32 color)
e2d::Color e2d::Renderer::getBackgroundColor()
{
s_nClearColor = D2D1::ColorF(color);
return Color(s_nClearColor.r, s_nClearColor.g, s_nClearColor.b, s_nClearColor.a);
}
void e2d::Renderer::setBackgroundColor(Color color)
{
s_nClearColor = D2D1::ColorF(color.r, color.g, color.b, color.a);
}
void e2d::Renderer::showFps(bool show)

View File

@ -4,8 +4,7 @@
e2d::Collider::Collider()
: m_bIsVisiable(true)
, m_nColor(Color::RED)
, m_fOpacity(1)
, m_nColor(Color::RED, 0.7f)
, m_pParentNode(nullptr)
, m_pTransformedGeometry(nullptr)
, m_bEnable(true)
@ -23,6 +22,11 @@ e2d::Node * e2d::Collider::getParentNode() const
return m_pParentNode;
}
e2d::Color e2d::Collider::getColor() const
{
return m_nColor;
}
void e2d::Collider::setEnable(bool bEnable)
{
m_bEnable = bEnable;
@ -33,16 +37,11 @@ void e2d::Collider::setVisiable(bool bVisiable)
m_bIsVisiable = bVisiable;
}
void e2d::Collider::setColor(UINT32 color)
void e2d::Collider::setColor(Color color)
{
m_nColor = color;
}
void e2d::Collider::setOpacity(double opacity)
{
m_fOpacity = min(max(static_cast<float>(opacity), 0), 1);
}
void e2d::Collider::setAutoResize(bool bEnable)
{
m_bAutoResize = bEnable;
@ -55,7 +54,7 @@ void e2d::Collider::_render()
// 获取纯色画刷
ID2D1SolidColorBrush * pBrush = Renderer::getSolidColorBrush();
// 设置画刷颜色和透明度
pBrush->SetColor(D2D1::ColorF(m_nColor, m_fOpacity));
pBrush->SetColor(m_nColor.toColorF());
// 绘制几何碰撞体
Renderer::getRenderTarget()->DrawGeometry(m_pTransformedGeometry, pBrush);
}

56
core/Common/Color.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "..\e2dcommon.h"
static const UINT32 sc_redShift = 16;
static const UINT32 sc_greenShift = 8;
static const UINT32 sc_blueShift = 0;
static const UINT32 sc_redMask = 0xff << sc_redShift;
static const UINT32 sc_greenMask = 0xff << sc_greenShift;
static const UINT32 sc_blueMask = 0xff << sc_blueShift;
e2d::Color::Color()
: r(0)
, g(0)
, b(0)
, a(1)
{
}
e2d::Color::Color(double r, double g, double b)
: r(static_cast<float>(r))
, g(static_cast<float>(g))
, b(static_cast<float>(b))
, a(static_cast<float>(1))
{
}
e2d::Color::Color(double r, double g, double b, double alpha)
: r(static_cast<float>(r))
, g(static_cast<float>(g))
, b(static_cast<float>(b))
, a(static_cast<float>(alpha))
{
}
e2d::Color::Color(UINT32 rgb)
{
init(rgb, 1);
}
e2d::Color::Color(UINT32 rgb, double alpha)
{
init(rgb, alpha);
}
void e2d::Color::init(UINT32 rgb, double alpha)
{
r = static_cast<float>((rgb & sc_redMask) >> sc_redShift) / 255.f;
g = static_cast<float>((rgb & sc_greenMask) >> sc_greenShift) / 255.f;
b = static_cast<float>((rgb & sc_blueMask) >> sc_blueShift) / 255.f;
a = static_cast<float>(alpha);
}
D2D1_COLOR_F e2d::Color::toColorF() const
{
return D2D1::ColorF(r, g, b, a);
}

View File

@ -4,7 +4,7 @@ e2d::TextStyle::TextStyle()
: fontFamily("")
, fontSize(22)
, color(Color::WHITE)
, weight(FontWeight::NORMAL)
, fontWeight(FontWeight::NORMAL)
, italic(false)
, underline(false)
, strikethrough(false)
@ -17,20 +17,20 @@ e2d::TextStyle::TextStyle()
e2d::TextStyle::TextStyle(
String fontFamily,
double fontSize,
UINT32 color,
UINT32 textStyleWeight,
Color color,
UINT32 fontWeight,
bool italic,
bool hasUnderline,
bool hasStrikethrough,
bool showOutline,
UINT32 outlineColor,
UINT32 outlineWidth,
Color outlineColor,
double outlineWidth,
int outlineJoin
)
: fontFamily(fontFamily)
, fontSize(fontSize)
, color(color)
, weight(textStyleWeight)
, fontWeight(fontWeight)
, italic(italic)
, underline(hasUnderline)
, strikethrough(hasStrikethrough)

View File

@ -3,7 +3,7 @@
e2d::Shape::Shape()
: m_nStyle(ShapeStyle::SOLID)
, m_nFillColor(Color::WHITE)
, m_nLineColor(Color::BLUE)
, m_nLineColor(Color::BLUE, 0.5)
, m_fStrokeWidth(1)
{
}
@ -14,28 +14,31 @@ e2d::Shape::~Shape()
void e2d::Shape::onRender()
{
auto pBrush = Renderer::getSolidColorBrush();
pBrush->SetOpacity(m_fDisplayOpacity);
switch (m_nStyle)
{
case ShapeStyle::FILL:
{
Renderer::getSolidColorBrush()->SetColor(D2D1::ColorF(m_nFillColor, m_fDisplayOpacity));
pBrush->SetColor(m_nFillColor.toColorF());
this->_renderFill();
Renderer::getSolidColorBrush()->SetColor(D2D1::ColorF(m_nLineColor, m_fDisplayOpacity));
pBrush->SetColor(m_nLineColor.toColorF());
this->_renderLine();
break;
}
case ShapeStyle::ROUND:
{
Renderer::getSolidColorBrush()->SetColor(D2D1::ColorF(m_nLineColor, m_fDisplayOpacity));
pBrush->SetColor(m_nLineColor.toColorF());
this->_renderLine();
break;
}
case ShapeStyle::SOLID:
{
Renderer::getSolidColorBrush()->SetColor(D2D1::ColorF(m_nFillColor, m_fDisplayOpacity));
pBrush->SetColor(m_nFillColor.toColorF());
this->_renderFill();
break;
}
@ -45,12 +48,12 @@ void e2d::Shape::onRender()
}
}
UINT32 e2d::Shape::getFillColor() const
e2d::Color e2d::Shape::getFillColor() const
{
return m_nFillColor;
}
UINT32 e2d::Shape::getLineColor() const
e2d::Color e2d::Shape::getLineColor() const
{
return m_nLineColor;
}
@ -65,12 +68,12 @@ int e2d::Shape::getStyle() const
return m_nStyle;
}
void e2d::Shape::setFillColor(UINT32 fillColor)
void e2d::Shape::setFillColor(Color fillColor)
{
m_nFillColor = fillColor;
}
void e2d::Shape::setLineColor(UINT32 lineColor)
void e2d::Shape::setLineColor(Color lineColor)
{
m_nLineColor = lineColor;
}

View File

@ -114,15 +114,15 @@ double e2d::Text::getFontSize() const
UINT32 e2d::Text::getFontWeight() const
{
return m_TextStyle.weight;
return m_TextStyle.fontWeight;
}
UINT32 e2d::Text::getColor() const
e2d::Color e2d::Text::getColor() const
{
return m_TextStyle.color;
}
UINT32 e2d::Text::getOutlineColor() const
e2d::Color e2d::Text::getOutlineColor() const
{
return m_TextStyle.outlineColor;
}
@ -185,13 +185,13 @@ void e2d::Text::setFontSize(double fontSize)
_reset();
}
void e2d::Text::setFontWeight(UINT32 textStyleWeight)
void e2d::Text::setFontWeight(UINT32 fontWeight)
{
m_TextStyle.weight = textStyleWeight;
m_TextStyle.fontWeight = fontWeight;
_reset();
}
void e2d::Text::setColor(UINT32 color)
void e2d::Text::setColor(Color color)
{
m_TextStyle.color = color;
}
@ -257,7 +257,7 @@ void e2d::Text::showOutline(bool showOutline)
m_TextStyle.showOutline = showOutline;
}
void e2d::Text::setOutlineColor(UINT32 outlineColor)
void e2d::Text::setOutlineColor(Color outlineColor)
{
m_TextStyle.outlineColor = outlineColor;
}
@ -283,9 +283,9 @@ void e2d::Text::onRender()
// »ñÈ¡Îı¾äÖȾÆ÷
auto pTextRenderer = Renderer::getCustomTextRenderer();
pTextRenderer->SetTextStyle(
D2D1::ColorF(m_TextStyle.color),
m_TextStyle.color.toColorF(),
m_TextStyle.showOutline,
D2D1::ColorF(m_TextStyle.outlineColor),
m_TextStyle.outlineColor.toColorF(),
static_cast<FLOAT>(m_TextStyle.outlineWidth),
D2D1_LINE_JOIN(m_TextStyle.outlineJoin)
);
@ -308,7 +308,7 @@ void e2d::Text::_createFormat()
HRESULT hr = Renderer::getIDWriteFactory()->CreateTextFormat(
m_TextStyle.fontFamily,
NULL,
DWRITE_FONT_WEIGHT(m_TextStyle.weight),
DWRITE_FONT_WEIGHT(m_TextStyle.fontWeight),
m_TextStyle.italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
static_cast<float>(m_TextStyle.fontSize),

View File

@ -231,9 +231,12 @@ class Renderer
friend Window;
public:
// ťńČĄąłž°ÉŤ
static Color getBackgroundColor();
// Ð޸ı³¾°É«
static void setBackgroundColor(
UINT32 color
Color color
);
// ÏÔʾ FPS

View File

@ -28,6 +28,9 @@ public:
// 获取父节点
Node * getParentNode() const;
// ťńČĄťćÖĆŃŐÉŤ
Color getColor() const;
// 启用或关闭该碰撞体
virtual void setEnable(
bool bEnable
@ -40,12 +43,7 @@ public:
// 设置绘制颜色
void setColor(
UINT32 color
);
// ÉèÖûæÖÆÍ¸Ã÷¶È
void setOpacity(
double opacity
Color color
);
// 设置大小跟随
@ -70,8 +68,7 @@ protected:
bool m_bEnable;
bool m_bIsVisiable;
bool m_bAutoResize;
UINT32 m_nColor;
float m_fOpacity;
Color m_nColor;
Node * m_pParentNode;
ID2D1TransformedGeometry * m_pTransformedGeometry;
};

View File

@ -210,7 +210,13 @@ private:
class Color
{
public:
enum : UINT32
float r;
float g;
float b;
float a;
public:
enum RGB_VALUE : UINT32
{
ALICE_BLUE = 0xF0F8FF,
AQUA = 0x00FFFF,
@ -275,6 +281,38 @@ public:
YELLOW = 0xFFFF00,
YELLOW_GREEN = 0x9ACD32
};
public:
Color();
Color(
double r,
double g,
double b
);
Color(
double r,
double g,
double b,
double alpha
);
Color(
UINT32 rgb
);
Color(
UINT32 rgb,
double alpha
);
void init(
UINT32 rgb,
double alpha
);
D2D1_COLOR_F toColorF() const;
};
@ -457,13 +495,13 @@ struct TextStyle
{
String fontFamily; // 字体
double fontSize; // 字号
UINT32 color; // ÑÕÉ«
UINT32 weight; // ´Öϸֵ
Color color; // ŃŐÉŤ
UINT32 fontWeight; // ´Öϸ־
bool italic; // 斜体
bool underline; // 下划线
bool strikethrough; // 删除线
bool showOutline; // 显示描边
UINT32 outlineColor; // Ãè±ßÑÕÉ«
Color outlineColor; // ĂčąßŃŐÉŤ
double outlineWidth; // 描边线宽
int outlineJoin; // 描边线相交样式
@ -473,14 +511,14 @@ struct TextStyle
TextStyle(
String fontFamily,
double fontSize = 22,
UINT32 color = Color::WHITE,
UINT32 textStyleWeight = FontWeight::NORMAL,
Color color = Color::WHITE,
UINT32 fontWeight = FontWeight::NORMAL,
bool italic = false,
bool hasUnderline = false,
bool hasStrikethrough = false,
bool showOutline = true,
UINT32 outlineColor = Color::BLACK,
UINT32 outlineWidth = 1.0,
Color outlineColor = Color::BLACK,
double outlineWidth = 1.0,
int outlineJoin = LineJoin::ROUND
);
};

View File

@ -592,10 +592,10 @@ public:
UINT32 getFontWeight() const;
// 获取文字颜色
UINT32 getColor() const;
Color getColor() const;
// 获取描边颜色
UINT32 getOutlineColor() const;
Color getOutlineColor() const;
// 获取描边线宽
double getOutlineWidth() const;
@ -634,12 +634,12 @@ public:
// 设置字体粗细值(默认值为 FontWeight::NORMAL
void setFontWeight(
UINT32 textStyleWeight
UINT32 fontWeight
);
// 设置文字颜色(默认值为 Color::WHITE
void setColor(
UINT32 color
Color color
);
// 设置文字斜体(默认值为 false
@ -679,7 +679,7 @@ public:
// 设置描边颜色
void setOutlineColor(
UINT32 outlineColor
Color outlineColor
);
// 设置描边线宽
@ -709,9 +709,9 @@ protected:
String m_sText;
bool m_bWrappingEnable;
float m_fWrappingWidth;
TextStyle m_TextStyle;
float m_fLineSpacing;
int m_nAlign;
TextStyle m_TextStyle;
IDWriteTextFormat * m_pDWriteTextFormat;
IDWriteTextLayout * m_pDWriteTextLayout;
};

View File

@ -18,22 +18,22 @@ public:
int getStyle() const;
// ťńČĄĚîłäŃŐÉŤ
UINT32 getFillColor() const;
Color getFillColor() const;
// ťńČĄĎßĚőŃŐÉŤ
UINT32 getLineColor() const;
Color getLineColor() const;
// ťńČĄĎßĚőżíśČ
double getStrokeWidth() const;
// ÉčÖĂĚîłäŃŐÉŤ
void setFillColor(
UINT32 fillColor
Color fillColor
);
// ÉčÖĂĎßĚőŃŐÉŤ
void setLineColor(
UINT32 lineColor
Color lineColor
);
// ÉčÖĂĎßĚőżíśČ
@ -57,8 +57,8 @@ protected:
protected:
int m_nStyle;
float m_fStrokeWidth;
UINT32 m_nLineColor;
UINT32 m_nFillColor;
Color m_nLineColor;
Color m_nFillColor;
};

View File

@ -36,5 +36,4 @@
#endif
using namespace e2d;
using namespace e2d::action;
using namespace e2d;

View File

@ -215,6 +215,7 @@
<ClCompile Include="..\..\core\Collider\ColliderCircle.cpp" />
<ClCompile Include="..\..\core\Collider\ColliderEllipse.cpp" />
<ClCompile Include="..\..\core\Collider\ColliderRect.cpp" />
<ClCompile Include="..\..\core\Common\Color.cpp" />
<ClCompile Include="..\..\core\Common\TextStyle.cpp" />
<ClCompile Include="..\..\core\Common\Object.cpp" />
<ClCompile Include="..\..\core\Common\Point.cpp" />

View File

@ -225,6 +225,9 @@
<ClCompile Include="..\..\core\Common\TextStyle.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Color.cpp">
<Filter>Common</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\core\easy2d.h" />