diff --git a/core/Base/Renderer.cpp b/core/Base/Renderer.cpp index 6afb9a2e..af63a9f8 100644 --- a/core/Base/Renderer.cpp +++ b/core/Base/Renderer.cpp @@ -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) diff --git a/core/Collider/Collider.cpp b/core/Collider/Collider.cpp index bc67b86a..ae4748db 100644 --- a/core/Collider/Collider.cpp +++ b/core/Collider/Collider.cpp @@ -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(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); } diff --git a/core/Common/Color.cpp b/core/Common/Color.cpp new file mode 100644 index 00000000..4bf0cce2 --- /dev/null +++ b/core/Common/Color.cpp @@ -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(r)) + , g(static_cast(g)) + , b(static_cast(b)) + , a(static_cast(1)) +{ +} + +e2d::Color::Color(double r, double g, double b, double alpha) + : r(static_cast(r)) + , g(static_cast(g)) + , b(static_cast(b)) + , a(static_cast(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((rgb & sc_redMask) >> sc_redShift) / 255.f; + g = static_cast((rgb & sc_greenMask) >> sc_greenShift) / 255.f; + b = static_cast((rgb & sc_blueMask) >> sc_blueShift) / 255.f; + a = static_cast(alpha); +} + +D2D1_COLOR_F e2d::Color::toColorF() const +{ + return D2D1::ColorF(r, g, b, a); +} diff --git a/core/Common/TextStyle.cpp b/core/Common/TextStyle.cpp index 7b67249b..a021194f 100644 --- a/core/Common/TextStyle.cpp +++ b/core/Common/TextStyle.cpp @@ -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) diff --git a/core/Node/Shape/Shape.cpp b/core/Node/Shape/Shape.cpp index e3b47f5a..5ed17542 100644 --- a/core/Node/Shape/Shape.cpp +++ b/core/Node/Shape/Shape.cpp @@ -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; } diff --git a/core/Node/Text.cpp b/core/Node/Text.cpp index 0bc43992..7cef69ed 100644 --- a/core/Node/Text.cpp +++ b/core/Node/Text.cpp @@ -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(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(m_TextStyle.fontSize), diff --git a/core/e2dbase.h b/core/e2dbase.h index e69f2421..3cf95463 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -231,9 +231,12 @@ class Renderer friend Window; public: + // 获取背景色 + static Color getBackgroundColor(); + // 修改背景色 static void setBackgroundColor( - UINT32 color + Color color ); // 显示 FPS diff --git a/core/e2dcollider.h b/core/e2dcollider.h index cd3ac15d..543b0659 100644 --- a/core/e2dcollider.h +++ b/core/e2dcollider.h @@ -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; }; diff --git a/core/e2dcommon.h b/core/e2dcommon.h index fae16ea1..ab31f99c 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -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 ); }; diff --git a/core/e2dnode.h b/core/e2dnode.h index 9600f95e..c5b0b137 100644 --- a/core/e2dnode.h +++ b/core/e2dnode.h @@ -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; }; diff --git a/core/e2dshape.h b/core/e2dshape.h index 8c58b964..b0bde97f 100644 --- a/core/e2dshape.h +++ b/core/e2dshape.h @@ -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; }; diff --git a/core/easy2d.h b/core/easy2d.h index d1a44e20..024405e3 100644 --- a/core/easy2d.h +++ b/core/easy2d.h @@ -36,5 +36,4 @@ #endif -using namespace e2d; -using namespace e2d::action; \ No newline at end of file +using namespace e2d; \ No newline at end of file diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index 98147691..2d422607 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -215,6 +215,7 @@ + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 6f00eac0..e2d4b8d2 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -225,6 +225,9 @@ Common + + Common +