From 7129971fbf261c7bc3c266a9a6311f68e7e8610b Mon Sep 17 00:00:00 2001 From: stevenmah Date: Mon, 8 Aug 2016 13:08:31 -0700 Subject: [PATCH 1/6] MOAIDraw: Implemented lateral gradients for circular slices/arcs --- src/moaicore/MOAIDraw.cpp | 153 ++++++++++++++++++++++++++++++++++++++ src/moaicore/MOAIDraw.h | 2 + 2 files changed, 155 insertions(+) diff --git a/src/moaicore/MOAIDraw.cpp b/src/moaicore/MOAIDraw.cpp index a91c0f3215..c0a0a7e034 100644 --- a/src/moaicore/MOAIDraw.cpp +++ b/src/moaicore/MOAIDraw.cpp @@ -863,6 +863,80 @@ int MOAIDraw::_fillCircularSliceGradient(lua_State *L){ return 0; } +//----------------------------------------------------------------// +/** @name fillCircularSliceVerticalGradient + + @overload + @in number x x-coordinate of circle + @in number y y-coordinate of circle + @in number radius + @in number angle angle of the slice of the circle in degrees + @in number offset the offset clockwise from positive y axis in degrees. + @in number blurMargin default to zero + @in number steps + @in number centerR red of starting color + @in number centerG green of starting color + @in number centerB blue of starting color + @in number centerA alpha of starting color + @in number edgeR red of ending color + @in number edgeG green of ending color + @in number edgeB blue of ending color + @in number edgeA alpha of ending color + @out nil + + @overload + @in number x x-coordinate of circle + @in number y y-coordinate of circle + @in number radius + @in number angle angle of the slice of the circle in degrees + @in number offset the offset clockwise from positive y axis in degrees. + @in number blurMargin default to zero + @in number steps + @in MOAIColor startColor + @in MOAIColor endColor + @out nil + */ +int MOAIDraw::_fillCircularSliceVerticalGradient(lua_State *L){ + + MOAILuaState state ( L ); + + float x = state.GetValue < float >( 1, 0.0f ); + float y = state.GetValue < float >( 2, 0.0f ); + float radius = state.GetValue < float >( 3, 0.0f ); + float angle = state.GetValue < float >( 4, 0.0f ); + float offset = state.GetValue < float >( 5, 0.0f ); + float blurMargin = state.GetValue < float >( 6, 0.0f ); + + u32 steps = state.GetValue < u32 > ( 7, DEFAULT_ELLIPSE_STEPS ); + + USColorVec startColor, endColor; + MOAIColor *color1, *color2; + + if ( ( color1 = state.GetLuaObject < MOAIColor > ( 8, false ) ) && + ( color2 = state.GetLuaObject < MOAIColor > ( 9, false ) ) ) { + startColor = color1->GetColorTrait(); + endColor = color2->GetColorTrait(); + } + else { + float centerR = state.GetValue < float > (8, 1.0f); + float centerG = state.GetValue < float > (9, 1.0f); + float centerB = state.GetValue < float > (10, 1.0f); + float centerA = state.GetValue < float > (11, 1.0f); + + float edgeR = state.GetValue < float > (12, 1.0f); + float edgeG = state.GetValue < float > (13, 1.0f); + float edgeB = state.GetValue < float > (14, 1.0f); + float edgeA = state.GetValue < float > (15, 1.0f); + + startColor.Set(centerR, centerG, centerB, centerA); + endColor.Set(edgeR, edgeG, edgeB, edgeA); + } + + MOAIDraw::DrawEllipticalSliceVerticalGradientFill(x, y, radius, radius, angle, offset, blurMargin, steps, startColor, endColor); + + return 0; +} + //----------------------------------------------------------------// /** @name fillEllipse @text Draw a filled ellipse. @@ -3054,6 +3128,84 @@ void MOAIDraw::DrawEllipticalSliceGradientFill(float x, float y, float xRad, flo } +//----------------------------------------------------------------// +// NOTE: This function only works for angle values up to and including 90 degrees +void MOAIDraw::DrawEllipticalSliceVerticalGradientFill(float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps, const USColorVec &startColor, const USColorVec &endColor){ + + MOAIGfxDevice& gfxDevice = MOAIGfxDevice::Get (); + + bool renderBlur = blurMargin > 0.0f; + + float theta = angle * ( float )D2R / ( float )steps; + float thetaStep = offset * (float)D2R; + + USColorVec penColor = gfxDevice.GetPenColor(); + + gfxDevice.BeginPrim( GL_TRIANGLE_FAN ); + + gfxDevice.SetPenColor(startColor); + gfxDevice.WriteVtx(x, y, 0.0f); + gfxDevice.WriteFinalColor4b(); + + float t = 0; + USColorVec interpolatedColor = startColor; + + gfxDevice.SetPenColor(interpolatedColor); + u32 i; + for (i = 0; i <= steps; ++i, thetaStep += theta) { + t = Cos ( thetaStep ); + gfxDevice.WriteVtx ( + x + ( Sin ( thetaStep ) * xRad ), + y + ( t * yRad ), + 0.0f + ); + interpolatedColor.Lerp ( startColor, endColor, t ); + gfxDevice.SetPenColor(interpolatedColor); + gfxDevice.WriteFinalColor4b (); + } + + gfxDevice.EndPrim(); + +// TODO: Implement blur code +// if (renderBlur) { +// USColorVec transColor(edgeColor); +// transColor.mA = 0.0f; +// if (gfxDevice.GetColorPremultiply()) { +// transColor.Set(0.0f, 0.0f, 0.0f, 0.0f); +// } +// +// thetaStep = offset * (float)D2R; +// // render the arc section +// gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); +// for (i = 0; i <= steps; ++i, thetaStep += theta ) { +// +// // point at blur margin +// gfxDevice.SetPenColor(transColor); +// gfxDevice.WriteVtx ( +// x + ( Sin ( thetaStep ) * (xRad + blurMargin) ), +// y + ( Cos ( thetaStep ) * (yRad + blurMargin) ), +// 0.0f +// ); +// gfxDevice.WriteFinalColor4b (); +// +// +// gfxDevice.SetPenColor(edgeColor); +// gfxDevice.WriteVtx ( +// x + ( Sin ( thetaStep ) * xRad ), +// y + ( Cos ( thetaStep ) * yRad ), +// 0.0f +// ); +// gfxDevice.WriteFinalColor4b (); +// +// } +// +// gfxDevice.EndPrim(); +// +// } + gfxDevice.SetPenColor(penColor); + +} + //----------------------------------------------------------------// void MOAIDraw::DrawGrid ( const USRect& rect, u32 xCells, u32 yCells ) { @@ -5821,6 +5973,7 @@ void MOAIDraw::RegisterLuaClass ( MOAILuaState& state ) { { "fillCircularGradient", _fillCircularGradient }, { "fillCircularSlice", _fillCircularSlice }, { "fillCircularSliceGradient", _fillCircularSliceGradient }, + { "fillCircularSliceVerticalGradient", _fillCircularSliceVerticalGradient}, { "fillEllipse", _fillEllipse }, { "fillEllipticalGradient", _fillEllipticalGradient }, { "fillEllipticalSlice", _fillEllipticalSlice }, diff --git a/src/moaicore/MOAIDraw.h b/src/moaicore/MOAIDraw.h index 3b8ee8c0e7..82e2c52cc6 100644 --- a/src/moaicore/MOAIDraw.h +++ b/src/moaicore/MOAIDraw.h @@ -51,6 +51,7 @@ class MOAIDraw : static int _fillCircularGradient ( lua_State* L ); static int _fillCircularSlice ( lua_State* L ); static int _fillCircularSliceGradient ( lua_State* L ); + static int _fillCircularSliceVerticalGradient ( lua_State* L ); static int _fillEllipse ( lua_State* L ); static int _fillEllipticalGradient ( lua_State* L ); static int _fillEllipticalSlice( lua_State* L ); @@ -96,6 +97,7 @@ class MOAIDraw : static void DrawEllipticalGradientFill ( float x, float y, float xRad, float yRad, u32 steps, const USColorVec ¢erColor, const USColorVec &edgeColor ); static void DrawEllipticalSliceFill ( float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps ); static void DrawEllipticalSliceGradientFill ( float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps, const USColorVec ¢erColor, const USColorVec &edgeColor ); + static void DrawEllipticalSliceVerticalGradientFill ( float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps, const USColorVec &startColor, const USColorVec &endColor ); static void DrawGrid ( const USRect& rect, u32 xCells, u32 yCells ); static void DrawJoinedCorner ( float x0, float y0, float x1, float y1, float x2, float y2, float lineWidth, float blurMargin ); static void DrawJoinedLine ( lua_State* L, float lineWidth, float blurMargin ); From 0dda19b198377ddc10c88bfdd043311bd7a36d24 Mon Sep 17 00:00:00 2001 From: stevenmah Date: Mon, 8 Aug 2016 16:16:32 -0700 Subject: [PATCH 2/6] MOAIDraw: Implemented vertical rounded rectangle gradients, but need to fix bug with circular slice gradients --- src/moaicore/MOAIDraw.cpp | 315 +++++++++++++++++++++++++++++++++++++- src/moaicore/MOAIDraw.h | 2 + 2 files changed, 316 insertions(+), 1 deletion(-) diff --git a/src/moaicore/MOAIDraw.cpp b/src/moaicore/MOAIDraw.cpp index c0a0a7e034..76fb41c503 100644 --- a/src/moaicore/MOAIDraw.cpp +++ b/src/moaicore/MOAIDraw.cpp @@ -1373,6 +1373,82 @@ int MOAIDraw::_fillRoundedRectangularGradient ( lua_State *L ) { return 0; } +//----------------------------------------------------------------// +/** @name fillRoundedRectangularGradient + @text Draw a filled rectangle with rounded corners. The edge of the rectangle + is one color and the center is another color. The center is inset by the + corner radius. + + @overload + @in x0 + @in y0 + @in x1 + @in y1 + @in cornerRadius + @in blurMargin + @in steps The number of steps to make each corner. + @in number centerR red of central color + @in number centerG green of central color + @in number centerB blue of central color + @in number centerA alpha of central color + @in number edgeR red of outer color + @in number edgeG green of outer color + @in number edgeB blue of outer color + @in number edgeA alpha of outer color + + @overload + @in number x0 + @in number y0 + @in number x1 + @in number y1 + @in number cornerRadius + @in number blurMargin + @in number steps The number of steps to make each corner. + @in MOAIColor edgeColor + @in MOAIColor centerColor + @out nil + */ + +int MOAIDraw::_fillRoundedRectangularVerticalGradient ( lua_State *L ) { + + MOAILuaState state ( L ); + + float x0 = state.GetValue < float > ( 1, 0.0f ); + float y0 = state.GetValue < float > ( 2, 0.0f ); + float x1 = state.GetValue < float > ( 3, 0.0f ); + float y1 = state.GetValue < float > ( 4, 0.0f ); + float cornerRadius = state.GetValue < float > (5, 0.0f); + float blurMargin = state.GetValue < float > (6, 0.0f); + u32 steps = state.GetValue < u32 > (7, DEFAULT_CURVE_STEPS); + + USColorVec topColor, bottomColor; + MOAIColor *color1, *color2; + + if ( ( color1 = state.GetLuaObject < MOAIColor > ( 8, false ) ) && + ( color2 = state.GetLuaObject < MOAIColor > ( 9, false ) ) ) { + topColor = color1->GetColorTrait(); + bottomColor = color2->GetColorTrait(); + } + else { + float centerR = state.GetValue < float > (8, 1.0f); + float centerG = state.GetValue < float > (9, 1.0f); + float centerB = state.GetValue < float > (10, 1.0f); + float centerA = state.GetValue < float > (11, 1.0f); + + float edgeR = state.GetValue < float > (12, 1.0f); + float edgeG = state.GetValue < float > (13, 1.0f); + float edgeB = state.GetValue < float > (14, 1.0f); + float edgeA = state.GetValue < float > (15, 1.0f); + + topColor.Set(centerR, centerG, centerB, centerA); + bottomColor.Set(edgeR, edgeG, edgeB, edgeA); + } + + MOAIDraw::DrawRoundedRectVerticalGradientFill(x0, y0, x1, y1, cornerRadius, blurMargin, steps, topColor, bottomColor); + + return 0; +} + //----------------------------------------------------------------// /** @name fillTriangularGradient @text Draw a filled triangle with a different color for each vertex. @@ -3147,19 +3223,23 @@ void MOAIDraw::DrawEllipticalSliceVerticalGradientFill(float x, float y, float x gfxDevice.WriteVtx(x, y, 0.0f); gfxDevice.WriteFinalColor4b(); + float s = 0; float t = 0; USColorVec interpolatedColor = startColor; gfxDevice.SetPenColor(interpolatedColor); u32 i; for (i = 0; i <= steps; ++i, thetaStep += theta) { + s = Sin ( thetaStep ); t = Cos ( thetaStep ); gfxDevice.WriteVtx ( - x + ( Sin ( thetaStep ) * xRad ), + x + ( s * xRad ), y + ( t * yRad ), 0.0f ); + interpolatedColor.Lerp ( startColor, endColor, t ); + gfxDevice.SetPenColor(interpolatedColor); gfxDevice.WriteFinalColor4b (); } @@ -5741,6 +5821,238 @@ void MOAIDraw::DrawRoundedRectGradientFill(float left, float top, float right, f } + +//----------------------------------------------------------------// +void MOAIDraw::DrawRoundedRectVerticalGradientFill(float left, float top, float right, float bottom, float cornerRadius, float blurMargin, u32 steps, const USColorVec &topColor, const USColorVec &bottomColor){ + + MOAIGfxDevice& gfxDevice = MOAIGfxDevice::Get (); + + bool renderBlur = blurMargin > 0.0f; + // make sure left is less than right + if (left > right) { + float temp = left; + left = right; + right = temp; + } + + // make sure bottom is less than top + if (bottom > top) { + float temp = top; + top = bottom; + bottom = temp; + } + + if (steps == 0){ + steps = 1; + } + + float height = top - bottom; + + USColorVec penColor = gfxDevice.GetPenColor(); + USColorVec workingColor = penColor; +// USColorVec transColor(edgeColor); + +// transColor.mA = 0.0f; +// if (gfxDevice.GetColorPremultiply()) { +// transColor.Set(0.0f, 0.0f, 0.0f, 0.0f); +// } + + + // draw rect in center (left + cornerRadius, bottom + cornerRadius, right - cornerRadius, top - cornerRadius) + gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); + + float t = cornerRadius / height; + workingColor.Lerp (bottomColor, topColor, t); + + gfxDevice.SetPenColor(workingColor); + gfxDevice.WriteVtx(left + cornerRadius, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(right - cornerRadius, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + t = (top - cornerRadius) / height; + workingColor.Lerp (bottomColor, topColor, t); + + gfxDevice.SetPenColor(workingColor); + gfxDevice.WriteVtx(left + cornerRadius, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(right - cornerRadius, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.EndPrim(); + + if (cornerRadius > 0.0f) { + // draw top rect (left + cornerRadius, top - cornerRadius, right - cornerRadius, top) + gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); + + t = (top - cornerRadius) / height; + workingColor.Lerp (bottomColor, topColor, t); + + gfxDevice.SetPenColor(workingColor); + gfxDevice.WriteVtx(left + cornerRadius, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(right - cornerRadius, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + + workingColor = topColor; + + gfxDevice.SetPenColor(workingColor); + gfxDevice.WriteVtx(left + cornerRadius, top); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(right - cornerRadius, top); + gfxDevice.WriteFinalColor4b(); + +// if (renderBlur) { +// gfxDevice.SetPenColor(transColor); +// gfxDevice.WriteVtx(left + cornerRadius, top + blurMargin); +// gfxDevice.WriteFinalColor4b(); +// +// gfxDevice.WriteVtx(right - cornerRadius, top + blurMargin); +// gfxDevice.WriteFinalColor4b(); +// +// } + + gfxDevice.EndPrim(); + + // draw bottom rect (left + cornerRadius, bottom , right - cornerRadius, bottom + cornerRadius) + gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); + +// if (renderBlur) { +// gfxDevice.SetPenColor(transColor); +// gfxDevice.WriteVtx(left + cornerRadius, bottom - blurMargin); +// gfxDevice.WriteFinalColor4b(); +// +// gfxDevice.WriteVtx(right - cornerRadius, bottom - blurMargin); +// gfxDevice.WriteFinalColor4b(); +// +// } + + workingColor = bottomColor; + + gfxDevice.SetPenColor(workingColor); + gfxDevice.WriteVtx(left + cornerRadius, bottom); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(right - cornerRadius, bottom); + gfxDevice.WriteFinalColor4b(); + + t = cornerRadius / height; + workingColor.Lerp (bottomColor, topColor, t); + + gfxDevice.SetPenColor(workingColor); + gfxDevice.WriteVtx(left + cornerRadius, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(right - cornerRadius, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.EndPrim(); + + // draw left rect (left, bottom + cornerRadius, left + cornerRadius, top - cornerRadius) + gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); + +// if (renderBlur) { +// gfxDevice.SetPenColor(transColor); +// gfxDevice.WriteVtx(left - blurMargin, bottom + cornerRadius); +// gfxDevice.WriteFinalColor4b(); +// +// gfxDevice.WriteVtx(left - blurMargin, top - cornerRadius); +// gfxDevice.WriteFinalColor4b(); +// } + + t = cornerRadius / height; + workingColor.Lerp (bottomColor, topColor, t); + + gfxDevice.SetPenColor(workingColor); + gfxDevice.WriteVtx(left, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(left + cornerRadius, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + t = (top - cornerRadius) / height; + workingColor.Lerp (bottomColor, topColor, t); + + gfxDevice.SetPenColor(workingColor); + gfxDevice.WriteVtx(left, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(left + cornerRadius, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.EndPrim(); + + // draw right rect (right - cornerRadius, bottom + cornerRadius, right, top - cornerRadius ) + gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); + + t = cornerRadius / height; + workingColor.Lerp (bottomColor, topColor, t); + + gfxDevice.SetPenColor(workingColor); + gfxDevice.WriteVtx(right - cornerRadius, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(right, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + t = (top - cornerRadius) / height; + workingColor.Lerp (bottomColor, topColor, t); + + gfxDevice.SetPenColor(workingColor); + gfxDevice.WriteVtx(right - cornerRadius, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(right, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + +// if (renderBlur) { +// gfxDevice.SetPenColor(transColor); +// gfxDevice.WriteVtx(right + blurMargin, bottom + cornerRadius); +// gfxDevice.WriteFinalColor4b(); +// +// gfxDevice.WriteVtx(right + blurMargin, top - cornerRadius); +// gfxDevice.WriteFinalColor4b(); +// +// } + + gfxDevice.EndPrim(); + + float angle = 90.0f; + float offset = -90; + + USColorVec cornerTopColor = topColor; + USColorVec cornerBottomColor = bottomColor; + + // draw upper left corner + t = (top - cornerRadius) / height; + cornerBottomColor.Lerp (bottomColor, topColor, t); + MOAIDraw::DrawEllipticalSliceVerticalGradientFill(left + cornerRadius, top - cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerBottomColor, cornerTopColor); + + // draw upper right corner + offset += angle; + MOAIDraw::DrawEllipticalSliceVerticalGradientFill(right - cornerRadius, top - cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerBottomColor, cornerTopColor); + + // draw lower right corner + t = cornerRadius / height; + cornerTopColor.Lerp (bottomColor, topColor, t); + cornerBottomColor = bottomColor; + offset += angle; + MOAIDraw::DrawEllipticalSliceVerticalGradientFill(right - cornerRadius, bottom + cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerBottomColor, cornerTopColor); + + // draw lower left corner + offset += angle; + MOAIDraw::DrawEllipticalSliceVerticalGradientFill(left + cornerRadius, bottom + cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerBottomColor, cornerTopColor); + } + + // restor pen color + gfxDevice.SetPenColor(penColor); + +} + //----------------------------------------------------------------// void MOAIDraw::DrawRoundedRectOutline(float left, float top, float right, float bottom, float cornerRadius, u32 steps){ MOAIGfxDevice& gfxDevice = MOAIGfxDevice::Get (); @@ -5984,6 +6296,7 @@ void MOAIDraw::RegisterLuaClass ( MOAILuaState& state ) { { "fillRect", _fillRect }, { "fillRoundedRect", _fillRoundedRect }, { "fillRoundedRectangularGradient", _fillRoundedRectangularGradient }, + { "fillRoundedRectangularVerticalGradient", _fillRoundedRectangularVerticalGradient}, { "fillTriangularGradient", _fillTriangularGradient }, { "fillVerticalRectangularGradient", _fillVerticalRectangularGradient }, { NULL, NULL } diff --git a/src/moaicore/MOAIDraw.h b/src/moaicore/MOAIDraw.h index 82e2c52cc6..f79bdd3abc 100644 --- a/src/moaicore/MOAIDraw.h +++ b/src/moaicore/MOAIDraw.h @@ -62,6 +62,7 @@ class MOAIDraw : static int _fillRect ( lua_State* L ); static int _fillRoundedRect ( lua_State* L ); static int _fillRoundedRectangularGradient ( lua_State* L ); + static int _fillRoundedRectangularVerticalGradient ( lua_State* L ); static int _fillTriangularGradient ( lua_State* L ); static int _fillVerticalRectangularGradient ( lua_State* L ); static int _drawTexture ( lua_State* L ); @@ -121,6 +122,7 @@ class MOAIDraw : static void DrawRoundBeveledLine ( lua_State* L, float lineWidth, float blurMargin, u32 steps ); static void DrawRoundedRectFill ( float left, float top, float right, float bottom, float cornerRadius, float blurMargin, u32 steps ); static void DrawRoundedRectGradientFill ( float left, float top, float right, float bottom, float cornerRadius, float blurMargin, u32 steps, const USColorVec ¢erColor, const USColorVec &edgeColor ); + static void DrawRoundedRectVerticalGradientFill ( float left, float top, float right, float bottom, float cornerRadius, float blurMargin, u32 steps, const USColorVec &topColor, const USColorVec &bottomColor ); static void DrawRoundedRectOutline ( float left, float top, float right, float bottom, float cornerRadius, u32 steps ); static void DrawTexture ( float left, float top, float right, float bottom, MOAITexture* texture ); static void DrawTriangularGradientFill (const USVec2D& v0, const USVec2D& v1, const USVec2D& v2, const USColorVec &color0, const USColorVec &color1, const USColorVec &color2); From 8d0bead1920f5547eb3dde50659ab7d0eec2d571 Mon Sep 17 00:00:00 2001 From: stevenmah Date: Mon, 8 Aug 2016 16:38:20 -0700 Subject: [PATCH 3/6] MOAIDraw: Fixed color issue caused by interpolating a negative t-value --- src/moaicore/MOAIDraw.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/moaicore/MOAIDraw.cpp b/src/moaicore/MOAIDraw.cpp index 76fb41c503..4989214d21 100644 --- a/src/moaicore/MOAIDraw.cpp +++ b/src/moaicore/MOAIDraw.cpp @@ -3225,9 +3225,8 @@ void MOAIDraw::DrawEllipticalSliceVerticalGradientFill(float x, float y, float x float s = 0; float t = 0; - USColorVec interpolatedColor = startColor; + USColorVec interpolatedColor; - gfxDevice.SetPenColor(interpolatedColor); u32 i; for (i = 0; i <= steps; ++i, thetaStep += theta) { s = Sin ( thetaStep ); @@ -3237,7 +3236,8 @@ void MOAIDraw::DrawEllipticalSliceVerticalGradientFill(float x, float y, float x y + ( t * yRad ), 0.0f ); - + printf("t: %f \n", t); + t = fabs (t); interpolatedColor.Lerp ( startColor, endColor, t ); gfxDevice.SetPenColor(interpolatedColor); From e8adacb63388038ab9bb89d4a1ad6986a5891458 Mon Sep 17 00:00:00 2001 From: stevenmah Date: Mon, 8 Aug 2016 17:24:36 -0700 Subject: [PATCH 4/6] MOAIDraw: Fixed flipped corner colors and removed a print statement --- src/moaicore/MOAIDraw.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/moaicore/MOAIDraw.cpp b/src/moaicore/MOAIDraw.cpp index 4989214d21..91ca847198 100644 --- a/src/moaicore/MOAIDraw.cpp +++ b/src/moaicore/MOAIDraw.cpp @@ -3236,7 +3236,7 @@ void MOAIDraw::DrawEllipticalSliceVerticalGradientFill(float x, float y, float x y + ( t * yRad ), 0.0f ); - printf("t: %f \n", t); + t = fabs (t); interpolatedColor.Lerp ( startColor, endColor, t ); @@ -6041,11 +6041,11 @@ void MOAIDraw::DrawRoundedRectVerticalGradientFill(float left, float top, float cornerTopColor.Lerp (bottomColor, topColor, t); cornerBottomColor = bottomColor; offset += angle; - MOAIDraw::DrawEllipticalSliceVerticalGradientFill(right - cornerRadius, bottom + cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerBottomColor, cornerTopColor); + MOAIDraw::DrawEllipticalSliceVerticalGradientFill(right - cornerRadius, bottom + cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerTopColor, cornerBottomColor); // draw lower left corner offset += angle; - MOAIDraw::DrawEllipticalSliceVerticalGradientFill(left + cornerRadius, bottom + cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerBottomColor, cornerTopColor); + MOAIDraw::DrawEllipticalSliceVerticalGradientFill(left + cornerRadius, bottom + cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerTopColor, cornerBottomColor); } // restor pen color From 5d4c078978feb30487bd824277348d31e41e521e Mon Sep 17 00:00:00 2001 From: stevenmah Date: Tue, 9 Aug 2016 11:35:08 -0700 Subject: [PATCH 5/6] MOAIDraw: Added horizontal draw functions for rounded rectangles and circles --- src/moaicore/MOAIDraw.cpp | 490 ++++++++++++++++++++++++++++++++++++-- src/moaicore/MOAIDraw.h | 4 + 2 files changed, 479 insertions(+), 15 deletions(-) diff --git a/src/moaicore/MOAIDraw.cpp b/src/moaicore/MOAIDraw.cpp index 91ca847198..51eff569e3 100644 --- a/src/moaicore/MOAIDraw.cpp +++ b/src/moaicore/MOAIDraw.cpp @@ -863,6 +863,80 @@ int MOAIDraw::_fillCircularSliceGradient(lua_State *L){ return 0; } +//----------------------------------------------------------------// +/** @name fillCircularSliceHorizontalGradient + + @overload + @in number x x-coordinate of circle + @in number y y-coordinate of circle + @in number radius + @in number angle angle of the slice of the circle in degrees + @in number offset the offset clockwise from positive y axis in degrees. + @in number blurMargin default to zero + @in number steps + @in number centerR red of starting color + @in number centerG green of starting color + @in number centerB blue of starting color + @in number centerA alpha of starting color + @in number edgeR red of ending color + @in number edgeG green of ending color + @in number edgeB blue of ending color + @in number edgeA alpha of ending color + @out nil + + @overload + @in number x x-coordinate of circle + @in number y y-coordinate of circle + @in number radius + @in number angle angle of the slice of the circle in degrees + @in number offset the offset clockwise from positive y axis in degrees. + @in number blurMargin default to zero + @in number steps + @in MOAIColor startColor + @in MOAIColor endColor + @out nil + */ +int MOAIDraw::_fillCircularSliceHorizontalGradient(lua_State *L){ + + MOAILuaState state ( L ); + + float x = state.GetValue < float >( 1, 0.0f ); + float y = state.GetValue < float >( 2, 0.0f ); + float radius = state.GetValue < float >( 3, 0.0f ); + float angle = state.GetValue < float >( 4, 0.0f ); + float offset = state.GetValue < float >( 5, 0.0f ); + float blurMargin = state.GetValue < float >( 6, 0.0f ); + + u32 steps = state.GetValue < u32 > ( 7, DEFAULT_ELLIPSE_STEPS ); + + USColorVec startColor, endColor; + MOAIColor *color1, *color2; + + if ( ( color1 = state.GetLuaObject < MOAIColor > ( 8, false ) ) && + ( color2 = state.GetLuaObject < MOAIColor > ( 9, false ) ) ) { + startColor = color1->GetColorTrait(); + endColor = color2->GetColorTrait(); + } + else { + float centerR = state.GetValue < float > (8, 1.0f); + float centerG = state.GetValue < float > (9, 1.0f); + float centerB = state.GetValue < float > (10, 1.0f); + float centerA = state.GetValue < float > (11, 1.0f); + + float edgeR = state.GetValue < float > (12, 1.0f); + float edgeG = state.GetValue < float > (13, 1.0f); + float edgeB = state.GetValue < float > (14, 1.0f); + float edgeA = state.GetValue < float > (15, 1.0f); + + startColor.Set(centerR, centerG, centerB, centerA); + endColor.Set(edgeR, edgeG, edgeB, edgeA); + } + + MOAIDraw::DrawEllipticalSliceHorizontalGradientFill(x, y, radius, radius, angle, offset, blurMargin, steps, startColor, endColor); + + return 0; +} + //----------------------------------------------------------------// /** @name fillCircularSliceVerticalGradient @@ -1374,7 +1448,7 @@ int MOAIDraw::_fillRoundedRectangularGradient ( lua_State *L ) { } //----------------------------------------------------------------// -/** @name fillRoundedRectangularGradient +/** @name fillRoundedRectangularHorizontalGradient @text Draw a filled rectangle with rounded corners. The edge of the rectangle is one color and the center is another color. The center is inset by the corner radius. @@ -1404,8 +1478,84 @@ int MOAIDraw::_fillRoundedRectangularGradient ( lua_State *L ) { @in number cornerRadius @in number blurMargin @in number steps The number of steps to make each corner. - @in MOAIColor edgeColor - @in MOAIColor centerColor + @in MOAIColor leftColor + @in MOAIColor rightColor + @out nil + */ + +int MOAIDraw::_fillRoundedRectangularHorizontalGradient ( lua_State *L ) { + + MOAILuaState state ( L ); + + float x0 = state.GetValue < float > ( 1, 0.0f ); + float y0 = state.GetValue < float > ( 2, 0.0f ); + float x1 = state.GetValue < float > ( 3, 0.0f ); + float y1 = state.GetValue < float > ( 4, 0.0f ); + float cornerRadius = state.GetValue < float > (5, 0.0f); + float blurMargin = state.GetValue < float > (6, 0.0f); + u32 steps = state.GetValue < u32 > (7, DEFAULT_CURVE_STEPS); + + USColorVec leftColor, rightColor; + MOAIColor *color1, *color2; + + if ( ( color1 = state.GetLuaObject < MOAIColor > ( 8, false ) ) && + ( color2 = state.GetLuaObject < MOAIColor > ( 9, false ) ) ) { + leftColor = color1->GetColorTrait(); + rightColor = color2->GetColorTrait(); + } + else { + float centerR = state.GetValue < float > (8, 1.0f); + float centerG = state.GetValue < float > (9, 1.0f); + float centerB = state.GetValue < float > (10, 1.0f); + float centerA = state.GetValue < float > (11, 1.0f); + + float edgeR = state.GetValue < float > (12, 1.0f); + float edgeG = state.GetValue < float > (13, 1.0f); + float edgeB = state.GetValue < float > (14, 1.0f); + float edgeA = state.GetValue < float > (15, 1.0f); + + leftColor.Set(centerR, centerG, centerB, centerA); + rightColor.Set(edgeR, edgeG, edgeB, edgeA); + } + + MOAIDraw::DrawRoundedRectHorizontalGradientFill(x0, y0, x1, y1, cornerRadius, blurMargin, steps, leftColor, rightColor); + + return 0; +} + +//----------------------------------------------------------------// +/** @name fillRoundedRectangularVerticalGradient + @text Draw a filled rectangle with rounded corners. The edge of the rectangle + is one color and the center is another color. The center is inset by the + corner radius. + + @overload + @in x0 + @in y0 + @in x1 + @in y1 + @in cornerRadius + @in blurMargin + @in steps The number of steps to make each corner. + @in number centerR red of central color + @in number centerG green of central color + @in number centerB blue of central color + @in number centerA alpha of central color + @in number edgeR red of outer color + @in number edgeG green of outer color + @in number edgeB blue of outer color + @in number edgeA alpha of outer color + + @overload + @in number x0 + @in number y0 + @in number x1 + @in number y1 + @in number cornerRadius + @in number blurMargin + @in number steps The number of steps to make each corner. + @in MOAIColor topColor + @in MOAIColor bottomColor @out nil */ @@ -3205,9 +3355,90 @@ void MOAIDraw::DrawEllipticalSliceGradientFill(float x, float y, float xRad, flo } //----------------------------------------------------------------// -// NOTE: This function only works for angle values up to and including 90 degrees -void MOAIDraw::DrawEllipticalSliceVerticalGradientFill(float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps, const USColorVec &startColor, const USColorVec &endColor){ +void MOAIDraw::DrawEllipticalSliceHorizontalGradientFill(float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps, const USColorVec &startColor, const USColorVec &endColor){ + + MOAIGfxDevice& gfxDevice = MOAIGfxDevice::Get (); + + bool renderBlur = blurMargin > 0.0f; + + float theta = angle * ( float )D2R / ( float )steps; + float thetaStep = offset * (float)D2R; + + USColorVec penColor = gfxDevice.GetPenColor(); + + gfxDevice.BeginPrim( GL_TRIANGLE_FAN ); + + gfxDevice.SetPenColor(startColor); + gfxDevice.WriteVtx(x, y, 0.0f); + gfxDevice.WriteFinalColor4b(); + + float s = 0; + float t = 0; + USColorVec interpolatedColor; + + u32 i; + for (i = 0; i <= steps; ++i, thetaStep += theta) { + s = Sin ( thetaStep ); + t = Cos ( thetaStep ); + gfxDevice.WriteVtx ( + x + ( s * xRad ), + y + ( t * yRad ), + 0.0f + ); + + s = fabs (s); + interpolatedColor.Lerp ( startColor, endColor, s ); + + gfxDevice.SetPenColor(interpolatedColor); + gfxDevice.WriteFinalColor4b (); + } + + gfxDevice.EndPrim(); + + // TODO: Implement blur code + // if (renderBlur) { + // USColorVec transColor(edgeColor); + // transColor.mA = 0.0f; + // if (gfxDevice.GetColorPremultiply()) { + // transColor.Set(0.0f, 0.0f, 0.0f, 0.0f); + // } + // + // thetaStep = offset * (float)D2R; + // // render the arc section + // gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); + // for (i = 0; i <= steps; ++i, thetaStep += theta ) { + // + // // point at blur margin + // gfxDevice.SetPenColor(transColor); + // gfxDevice.WriteVtx ( + // x + ( Sin ( thetaStep ) * (xRad + blurMargin) ), + // y + ( Cos ( thetaStep ) * (yRad + blurMargin) ), + // 0.0f + // ); + // gfxDevice.WriteFinalColor4b (); + // + // + // gfxDevice.SetPenColor(edgeColor); + // gfxDevice.WriteVtx ( + // x + ( Sin ( thetaStep ) * xRad ), + // y + ( Cos ( thetaStep ) * yRad ), + // 0.0f + // ); + // gfxDevice.WriteFinalColor4b (); + // + // } + // + // gfxDevice.EndPrim(); + // + // } + gfxDevice.SetPenColor(penColor); +} + + +//----------------------------------------------------------------// +void MOAIDraw::DrawEllipticalSliceVerticalGradientFill(float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps, const USColorVec &startColor, const USColorVec &endColor){ + MOAIGfxDevice& gfxDevice = MOAIGfxDevice::Get (); bool renderBlur = blurMargin > 0.0f; @@ -5811,9 +6042,237 @@ void MOAIDraw::DrawRoundedRectGradientFill(float left, float top, float right, f // draw lower right corner offset += angle; MOAIDraw::DrawEllipticalSliceGradientFill(right - cornerRadius, bottom + cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, centerColor, edgeColor); + } + + // restor pen color + gfxDevice.SetPenColor(penColor); + +} + +//----------------------------------------------------------------// +void MOAIDraw::DrawRoundedRectHorizontalGradientFill(float left, float top, float right, float bottom, float cornerRadius, float blurMargin, u32 steps, const USColorVec &leftColor, const USColorVec &rightColor){ + + MOAIGfxDevice& gfxDevice = MOAIGfxDevice::Get (); + + bool renderBlur = blurMargin > 0.0f; + // make sure left is less than right + if (left > right) { + float temp = left; + left = right; + right = temp; + } + + // make sure bottom is less than top + if (bottom > top) { + float temp = top; + top = bottom; + bottom = temp; + } + + if (steps == 0){ + steps = 1; + } + + float width = right - left; + + USColorVec penColor = gfxDevice.GetPenColor(); + USColorVec workingColor = penColor; + // USColorVec transColor(edgeColor); + + // transColor.mA = 0.0f; + // if (gfxDevice.GetColorPremultiply()) { + // transColor.Set(0.0f, 0.0f, 0.0f, 0.0f); + // } + + + // draw rect in center (left + cornerRadius, bottom + cornerRadius, right - cornerRadius, top - cornerRadius) + gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); + + float t = cornerRadius / width; + workingColor.Lerp (leftColor, rightColor, t); + gfxDevice.SetPenColor(workingColor); + + gfxDevice.WriteVtx(left + cornerRadius, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(left + cornerRadius, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + + t = (right - cornerRadius) / width; + workingColor.Lerp (leftColor, rightColor, t); + gfxDevice.SetPenColor(workingColor); + + gfxDevice.WriteVtx(right - cornerRadius, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(right - cornerRadius, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.EndPrim(); + + if (cornerRadius > 0.0f) { + // draw top rect (left + cornerRadius, top - cornerRadius, right - cornerRadius, top) + gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); + t = cornerRadius / width; + workingColor.Lerp (leftColor, rightColor, t); + gfxDevice.SetPenColor(workingColor); + gfxDevice.WriteVtx(left + cornerRadius, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(left + cornerRadius, top); + gfxDevice.WriteFinalColor4b(); + + t = (right - cornerRadius) / width; + workingColor.Lerp(leftColor, rightColor, t); + gfxDevice.SetPenColor(workingColor); + gfxDevice.WriteVtx(right - cornerRadius, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(right - cornerRadius, top); + gfxDevice.WriteFinalColor4b(); + + // if (renderBlur) { + // gfxDevice.SetPenColor(transColor); + // gfxDevice.WriteVtx(left + cornerRadius, top + blurMargin); + // gfxDevice.WriteFinalColor4b(); + // + // gfxDevice.WriteVtx(right - cornerRadius, top + blurMargin); + // gfxDevice.WriteFinalColor4b(); + // + // } + + gfxDevice.EndPrim(); + + // draw bottom rect (left + cornerRadius, bottom , right - cornerRadius, bottom + cornerRadius) + gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); + + // if (renderBlur) { + // gfxDevice.SetPenColor(transColor); + // gfxDevice.WriteVtx(left + cornerRadius, bottom - blurMargin); + // gfxDevice.WriteFinalColor4b(); + // + // gfxDevice.WriteVtx(right - cornerRadius, bottom - blurMargin); + // gfxDevice.WriteFinalColor4b(); + // + // } + + t = cornerRadius / width; + workingColor.Lerp(leftColor, rightColor, t); + gfxDevice.SetPenColor(workingColor); + + gfxDevice.WriteVtx(left + cornerRadius, bottom); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(left + cornerRadius, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + t = (right - cornerRadius) / width; + workingColor.Lerp (leftColor, rightColor, t); + gfxDevice.SetPenColor(workingColor); + + gfxDevice.WriteVtx(right - cornerRadius, bottom); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(right - cornerRadius, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.EndPrim(); + + // draw left rect (left, bottom + cornerRadius, left + cornerRadius, top - cornerRadius) + gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); + + // if (renderBlur) { + // gfxDevice.SetPenColor(transColor); + // gfxDevice.WriteVtx(left - blurMargin, bottom + cornerRadius); + // gfxDevice.WriteFinalColor4b(); + // + // gfxDevice.WriteVtx(left - blurMargin, top - cornerRadius); + // gfxDevice.WriteFinalColor4b(); + // } + + workingColor = leftColor; + gfxDevice.SetPenColor(workingColor); + + gfxDevice.WriteVtx(left, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(left, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + + t = cornerRadius / width; + workingColor.Lerp (leftColor, rightColor, t); + gfxDevice.SetPenColor(workingColor); + + gfxDevice.WriteVtx(left + cornerRadius, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(left + cornerRadius, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.EndPrim(); + + // draw right rect (right - cornerRadius, bottom + cornerRadius, right, top - cornerRadius ) + gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); + + t = (right - cornerRadius) / width; + workingColor.Lerp (leftColor, rightColor, t); + gfxDevice.SetPenColor(workingColor); + + gfxDevice.WriteVtx(right - cornerRadius, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(right - cornerRadius, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + + workingColor = rightColor; + gfxDevice.SetPenColor(workingColor); + + gfxDevice.WriteVtx(right, bottom + cornerRadius); + gfxDevice.WriteFinalColor4b(); + + gfxDevice.WriteVtx(right, top - cornerRadius); + gfxDevice.WriteFinalColor4b(); + + // if (renderBlur) { + // gfxDevice.SetPenColor(transColor); + // gfxDevice.WriteVtx(right + blurMargin, bottom + cornerRadius); + // gfxDevice.WriteFinalColor4b(); + // + // gfxDevice.WriteVtx(right + blurMargin, top - cornerRadius); + // gfxDevice.WriteFinalColor4b(); + // + // } + + gfxDevice.EndPrim(); + + float angle = 90.0f; + float offset = 0.0f; + + USColorVec cornerStartColor = leftColor; + USColorVec cornerEndColor = rightColor; + + // draw upper right corner + t = (right - cornerRadius) / width; + cornerStartColor.Lerp (leftColor, rightColor, t); + MOAIDraw::DrawEllipticalSliceHorizontalGradientFill(right - cornerRadius, top - cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerStartColor, cornerEndColor); + + // draw lower right corner + offset += angle; + MOAIDraw::DrawEllipticalSliceHorizontalGradientFill(right - cornerRadius, bottom + cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerStartColor, cornerEndColor); + + // draw lower left corner + t = cornerRadius / width; + cornerStartColor.Lerp (leftColor, rightColor, t); + cornerEndColor = leftColor; + offset += angle; + MOAIDraw::DrawEllipticalSliceHorizontalGradientFill(left + cornerRadius, bottom + cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerStartColor, cornerEndColor); + + // draw upper left corner + offset += angle; + MOAIDraw::DrawEllipticalSliceHorizontalGradientFill(left + cornerRadius, top - cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerStartColor, cornerEndColor); } // restor pen color @@ -5821,7 +6280,6 @@ void MOAIDraw::DrawRoundedRectGradientFill(float left, float top, float right, f } - //----------------------------------------------------------------// void MOAIDraw::DrawRoundedRectVerticalGradientFill(float left, float top, float right, float bottom, float cornerRadius, float blurMargin, u32 steps, const USColorVec &topColor, const USColorVec &bottomColor){ @@ -6024,28 +6482,28 @@ void MOAIDraw::DrawRoundedRectVerticalGradientFill(float left, float top, float float angle = 90.0f; float offset = -90; - USColorVec cornerTopColor = topColor; - USColorVec cornerBottomColor = bottomColor; + USColorVec cornerStartColor = bottomColor; + USColorVec cornerEndColor = topColor; // draw upper left corner t = (top - cornerRadius) / height; - cornerBottomColor.Lerp (bottomColor, topColor, t); - MOAIDraw::DrawEllipticalSliceVerticalGradientFill(left + cornerRadius, top - cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerBottomColor, cornerTopColor); + cornerStartColor.Lerp (bottomColor, topColor, t); + MOAIDraw::DrawEllipticalSliceVerticalGradientFill(left + cornerRadius, top - cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerStartColor, cornerEndColor); // draw upper right corner offset += angle; - MOAIDraw::DrawEllipticalSliceVerticalGradientFill(right - cornerRadius, top - cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerBottomColor, cornerTopColor); + MOAIDraw::DrawEllipticalSliceVerticalGradientFill(right - cornerRadius, top - cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerStartColor, cornerEndColor); // draw lower right corner t = cornerRadius / height; - cornerTopColor.Lerp (bottomColor, topColor, t); - cornerBottomColor = bottomColor; + cornerStartColor.Lerp (bottomColor, topColor, t); + cornerEndColor = bottomColor; offset += angle; - MOAIDraw::DrawEllipticalSliceVerticalGradientFill(right - cornerRadius, bottom + cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerTopColor, cornerBottomColor); + MOAIDraw::DrawEllipticalSliceVerticalGradientFill(right - cornerRadius, bottom + cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerStartColor, cornerEndColor); // draw lower left corner offset += angle; - MOAIDraw::DrawEllipticalSliceVerticalGradientFill(left + cornerRadius, bottom + cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerTopColor, cornerBottomColor); + MOAIDraw::DrawEllipticalSliceVerticalGradientFill(left + cornerRadius, bottom + cornerRadius, cornerRadius, cornerRadius, angle, offset, blurMargin, steps, cornerStartColor, cornerEndColor); } // restor pen color @@ -6285,6 +6743,7 @@ void MOAIDraw::RegisterLuaClass ( MOAILuaState& state ) { { "fillCircularGradient", _fillCircularGradient }, { "fillCircularSlice", _fillCircularSlice }, { "fillCircularSliceGradient", _fillCircularSliceGradient }, + { "fillCircularSliceHorizontalGradient", _fillCircularSliceHorizontalGradient}, { "fillCircularSliceVerticalGradient", _fillCircularSliceVerticalGradient}, { "fillEllipse", _fillEllipse }, { "fillEllipticalGradient", _fillEllipticalGradient }, @@ -6296,6 +6755,7 @@ void MOAIDraw::RegisterLuaClass ( MOAILuaState& state ) { { "fillRect", _fillRect }, { "fillRoundedRect", _fillRoundedRect }, { "fillRoundedRectangularGradient", _fillRoundedRectangularGradient }, + { "fillRoundedRectangularHorizontalGradient", _fillRoundedRectangularHorizontalGradient}, { "fillRoundedRectangularVerticalGradient", _fillRoundedRectangularVerticalGradient}, { "fillTriangularGradient", _fillTriangularGradient }, { "fillVerticalRectangularGradient", _fillVerticalRectangularGradient }, diff --git a/src/moaicore/MOAIDraw.h b/src/moaicore/MOAIDraw.h index f79bdd3abc..ce13e5cedb 100644 --- a/src/moaicore/MOAIDraw.h +++ b/src/moaicore/MOAIDraw.h @@ -52,6 +52,7 @@ class MOAIDraw : static int _fillCircularSlice ( lua_State* L ); static int _fillCircularSliceGradient ( lua_State* L ); static int _fillCircularSliceVerticalGradient ( lua_State* L ); + static int _fillCircularSliceHorizontalGradient ( lua_State* L ); static int _fillEllipse ( lua_State* L ); static int _fillEllipticalGradient ( lua_State* L ); static int _fillEllipticalSlice( lua_State* L ); @@ -63,6 +64,7 @@ class MOAIDraw : static int _fillRoundedRect ( lua_State* L ); static int _fillRoundedRectangularGradient ( lua_State* L ); static int _fillRoundedRectangularVerticalGradient ( lua_State* L ); + static int _fillRoundedRectangularHorizontalGradient ( lua_State* L ); static int _fillTriangularGradient ( lua_State* L ); static int _fillVerticalRectangularGradient ( lua_State* L ); static int _drawTexture ( lua_State* L ); @@ -99,6 +101,7 @@ class MOAIDraw : static void DrawEllipticalSliceFill ( float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps ); static void DrawEllipticalSliceGradientFill ( float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps, const USColorVec ¢erColor, const USColorVec &edgeColor ); static void DrawEllipticalSliceVerticalGradientFill ( float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps, const USColorVec &startColor, const USColorVec &endColor ); + static void DrawEllipticalSliceHorizontalGradientFill ( float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps, const USColorVec &startColor, const USColorVec &endColor ); static void DrawGrid ( const USRect& rect, u32 xCells, u32 yCells ); static void DrawJoinedCorner ( float x0, float y0, float x1, float y1, float x2, float y2, float lineWidth, float blurMargin ); static void DrawJoinedLine ( lua_State* L, float lineWidth, float blurMargin ); @@ -123,6 +126,7 @@ class MOAIDraw : static void DrawRoundedRectFill ( float left, float top, float right, float bottom, float cornerRadius, float blurMargin, u32 steps ); static void DrawRoundedRectGradientFill ( float left, float top, float right, float bottom, float cornerRadius, float blurMargin, u32 steps, const USColorVec ¢erColor, const USColorVec &edgeColor ); static void DrawRoundedRectVerticalGradientFill ( float left, float top, float right, float bottom, float cornerRadius, float blurMargin, u32 steps, const USColorVec &topColor, const USColorVec &bottomColor ); + static void DrawRoundedRectHorizontalGradientFill ( float left, float top, float right, float bottom, float cornerRadius, float blurMargin, u32 steps, const USColorVec &leftColor, const USColorVec &rightColor ); static void DrawRoundedRectOutline ( float left, float top, float right, float bottom, float cornerRadius, u32 steps ); static void DrawTexture ( float left, float top, float right, float bottom, MOAITexture* texture ); static void DrawTriangularGradientFill (const USVec2D& v0, const USVec2D& v1, const USVec2D& v2, const USColorVec &color0, const USColorVec &color1, const USColorVec &color2); From 3a69ec3d3db5b99b587aba536aeea391884e96cb Mon Sep 17 00:00:00 2001 From: stevenmah Date: Tue, 9 Aug 2016 14:36:14 -0700 Subject: [PATCH 6/6] MOAIDraw: Rearranged some functions and removed commented out code --- src/moaicore/MOAIDraw.cpp | 205 ++++---------------------------------- src/moaicore/MOAIDraw.h | 8 +- 2 files changed, 24 insertions(+), 189 deletions(-) diff --git a/src/moaicore/MOAIDraw.cpp b/src/moaicore/MOAIDraw.cpp index 51eff569e3..fa9592ce0b 100644 --- a/src/moaicore/MOAIDraw.cpp +++ b/src/moaicore/MOAIDraw.cpp @@ -1461,14 +1461,14 @@ int MOAIDraw::_fillRoundedRectangularGradient ( lua_State *L ) { @in cornerRadius @in blurMargin @in steps The number of steps to make each corner. - @in number centerR red of central color - @in number centerG green of central color - @in number centerB blue of central color - @in number centerA alpha of central color - @in number edgeR red of outer color - @in number edgeG green of outer color - @in number edgeB blue of outer color - @in number edgeA alpha of outer color + @in number centerR red of left color + @in number centerG green of left color + @in number centerB blue of left color + @in number centerA alpha of left color + @in number edgeR red of right color + @in number edgeG green of right color + @in number edgeB blue of right color + @in number edgeA alpha of right color @overload @in number x0 @@ -1537,14 +1537,14 @@ int MOAIDraw::_fillRoundedRectangularHorizontalGradient ( lua_State *L ) { @in cornerRadius @in blurMargin @in steps The number of steps to make each corner. - @in number centerR red of central color - @in number centerG green of central color - @in number centerB blue of central color - @in number centerA alpha of central color - @in number edgeR red of outer color - @in number edgeG green of outer color - @in number edgeB blue of outer color - @in number edgeA alpha of outer color + @in number centerR red of top color + @in number centerG green of top color + @in number centerB blue of top color + @in number centerA alpha of top color + @in number edgeR red of bottom color + @in number edgeG green of bottom color + @in number edgeB blue of bottom color + @in number edgeA alpha of bottom color @overload @in number x0 @@ -3359,8 +3359,6 @@ void MOAIDraw::DrawEllipticalSliceHorizontalGradientFill(float x, float y, float MOAIGfxDevice& gfxDevice = MOAIGfxDevice::Get (); - bool renderBlur = blurMargin > 0.0f; - float theta = angle * ( float )D2R / ( float )steps; float thetaStep = offset * (float)D2R; @@ -3396,41 +3394,6 @@ void MOAIDraw::DrawEllipticalSliceHorizontalGradientFill(float x, float y, float gfxDevice.EndPrim(); // TODO: Implement blur code - // if (renderBlur) { - // USColorVec transColor(edgeColor); - // transColor.mA = 0.0f; - // if (gfxDevice.GetColorPremultiply()) { - // transColor.Set(0.0f, 0.0f, 0.0f, 0.0f); - // } - // - // thetaStep = offset * (float)D2R; - // // render the arc section - // gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); - // for (i = 0; i <= steps; ++i, thetaStep += theta ) { - // - // // point at blur margin - // gfxDevice.SetPenColor(transColor); - // gfxDevice.WriteVtx ( - // x + ( Sin ( thetaStep ) * (xRad + blurMargin) ), - // y + ( Cos ( thetaStep ) * (yRad + blurMargin) ), - // 0.0f - // ); - // gfxDevice.WriteFinalColor4b (); - // - // - // gfxDevice.SetPenColor(edgeColor); - // gfxDevice.WriteVtx ( - // x + ( Sin ( thetaStep ) * xRad ), - // y + ( Cos ( thetaStep ) * yRad ), - // 0.0f - // ); - // gfxDevice.WriteFinalColor4b (); - // - // } - // - // gfxDevice.EndPrim(); - // - // } gfxDevice.SetPenColor(penColor); } @@ -3441,8 +3404,6 @@ void MOAIDraw::DrawEllipticalSliceVerticalGradientFill(float x, float y, float x MOAIGfxDevice& gfxDevice = MOAIGfxDevice::Get (); - bool renderBlur = blurMargin > 0.0f; - float theta = angle * ( float )D2R / ( float )steps; float thetaStep = offset * (float)D2R; @@ -3477,42 +3438,7 @@ void MOAIDraw::DrawEllipticalSliceVerticalGradientFill(float x, float y, float x gfxDevice.EndPrim(); -// TODO: Implement blur code -// if (renderBlur) { -// USColorVec transColor(edgeColor); -// transColor.mA = 0.0f; -// if (gfxDevice.GetColorPremultiply()) { -// transColor.Set(0.0f, 0.0f, 0.0f, 0.0f); -// } -// -// thetaStep = offset * (float)D2R; -// // render the arc section -// gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); -// for (i = 0; i <= steps; ++i, thetaStep += theta ) { -// -// // point at blur margin -// gfxDevice.SetPenColor(transColor); -// gfxDevice.WriteVtx ( -// x + ( Sin ( thetaStep ) * (xRad + blurMargin) ), -// y + ( Cos ( thetaStep ) * (yRad + blurMargin) ), -// 0.0f -// ); -// gfxDevice.WriteFinalColor4b (); -// -// -// gfxDevice.SetPenColor(edgeColor); -// gfxDevice.WriteVtx ( -// x + ( Sin ( thetaStep ) * xRad ), -// y + ( Cos ( thetaStep ) * yRad ), -// 0.0f -// ); -// gfxDevice.WriteFinalColor4b (); -// -// } -// -// gfxDevice.EndPrim(); -// -// } + // TODO: Implement blur code gfxDevice.SetPenColor(penColor); } @@ -6054,7 +5980,6 @@ void MOAIDraw::DrawRoundedRectHorizontalGradientFill(float left, float top, floa MOAIGfxDevice& gfxDevice = MOAIGfxDevice::Get (); - bool renderBlur = blurMargin > 0.0f; // make sure left is less than right if (left > right) { float temp = left; @@ -6075,15 +6000,9 @@ void MOAIDraw::DrawRoundedRectHorizontalGradientFill(float left, float top, floa float width = right - left; + // TODO: Implement blur code USColorVec penColor = gfxDevice.GetPenColor(); USColorVec workingColor = penColor; - // USColorVec transColor(edgeColor); - - // transColor.mA = 0.0f; - // if (gfxDevice.GetColorPremultiply()) { - // transColor.Set(0.0f, 0.0f, 0.0f, 0.0f); - // } - // draw rect in center (left + cornerRadius, bottom + cornerRadius, right - cornerRadius, top - cornerRadius) gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); @@ -6134,31 +6053,11 @@ void MOAIDraw::DrawRoundedRectHorizontalGradientFill(float left, float top, floa gfxDevice.WriteVtx(right - cornerRadius, top); gfxDevice.WriteFinalColor4b(); - // if (renderBlur) { - // gfxDevice.SetPenColor(transColor); - // gfxDevice.WriteVtx(left + cornerRadius, top + blurMargin); - // gfxDevice.WriteFinalColor4b(); - // - // gfxDevice.WriteVtx(right - cornerRadius, top + blurMargin); - // gfxDevice.WriteFinalColor4b(); - // - // } - gfxDevice.EndPrim(); // draw bottom rect (left + cornerRadius, bottom , right - cornerRadius, bottom + cornerRadius) gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); - // if (renderBlur) { - // gfxDevice.SetPenColor(transColor); - // gfxDevice.WriteVtx(left + cornerRadius, bottom - blurMargin); - // gfxDevice.WriteFinalColor4b(); - // - // gfxDevice.WriteVtx(right - cornerRadius, bottom - blurMargin); - // gfxDevice.WriteFinalColor4b(); - // - // } - t = cornerRadius / width; workingColor.Lerp(leftColor, rightColor, t); gfxDevice.SetPenColor(workingColor); @@ -6184,15 +6083,6 @@ void MOAIDraw::DrawRoundedRectHorizontalGradientFill(float left, float top, floa // draw left rect (left, bottom + cornerRadius, left + cornerRadius, top - cornerRadius) gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); - // if (renderBlur) { - // gfxDevice.SetPenColor(transColor); - // gfxDevice.WriteVtx(left - blurMargin, bottom + cornerRadius); - // gfxDevice.WriteFinalColor4b(); - // - // gfxDevice.WriteVtx(left - blurMargin, top - cornerRadius); - // gfxDevice.WriteFinalColor4b(); - // } - workingColor = leftColor; gfxDevice.SetPenColor(workingColor); @@ -6236,16 +6126,6 @@ void MOAIDraw::DrawRoundedRectHorizontalGradientFill(float left, float top, floa gfxDevice.WriteVtx(right, top - cornerRadius); gfxDevice.WriteFinalColor4b(); - // if (renderBlur) { - // gfxDevice.SetPenColor(transColor); - // gfxDevice.WriteVtx(right + blurMargin, bottom + cornerRadius); - // gfxDevice.WriteFinalColor4b(); - // - // gfxDevice.WriteVtx(right + blurMargin, top - cornerRadius); - // gfxDevice.WriteFinalColor4b(); - // - // } - gfxDevice.EndPrim(); float angle = 90.0f; @@ -6285,7 +6165,6 @@ void MOAIDraw::DrawRoundedRectVerticalGradientFill(float left, float top, float MOAIGfxDevice& gfxDevice = MOAIGfxDevice::Get (); - bool renderBlur = blurMargin > 0.0f; // make sure left is less than right if (left > right) { float temp = left; @@ -6306,15 +6185,10 @@ void MOAIDraw::DrawRoundedRectVerticalGradientFill(float left, float top, float float height = top - bottom; + // TODO: Implement blur code + USColorVec penColor = gfxDevice.GetPenColor(); USColorVec workingColor = penColor; -// USColorVec transColor(edgeColor); - -// transColor.mA = 0.0f; -// if (gfxDevice.GetColorPremultiply()) { -// transColor.Set(0.0f, 0.0f, 0.0f, 0.0f); -// } - // draw rect in center (left + cornerRadius, bottom + cornerRadius, right - cornerRadius, top - cornerRadius) gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); @@ -6364,31 +6238,11 @@ void MOAIDraw::DrawRoundedRectVerticalGradientFill(float left, float top, float gfxDevice.WriteVtx(right - cornerRadius, top); gfxDevice.WriteFinalColor4b(); -// if (renderBlur) { -// gfxDevice.SetPenColor(transColor); -// gfxDevice.WriteVtx(left + cornerRadius, top + blurMargin); -// gfxDevice.WriteFinalColor4b(); -// -// gfxDevice.WriteVtx(right - cornerRadius, top + blurMargin); -// gfxDevice.WriteFinalColor4b(); -// -// } - gfxDevice.EndPrim(); // draw bottom rect (left + cornerRadius, bottom , right - cornerRadius, bottom + cornerRadius) gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); -// if (renderBlur) { -// gfxDevice.SetPenColor(transColor); -// gfxDevice.WriteVtx(left + cornerRadius, bottom - blurMargin); -// gfxDevice.WriteFinalColor4b(); -// -// gfxDevice.WriteVtx(right - cornerRadius, bottom - blurMargin); -// gfxDevice.WriteFinalColor4b(); -// -// } - workingColor = bottomColor; gfxDevice.SetPenColor(workingColor); @@ -6413,15 +6267,6 @@ void MOAIDraw::DrawRoundedRectVerticalGradientFill(float left, float top, float // draw left rect (left, bottom + cornerRadius, left + cornerRadius, top - cornerRadius) gfxDevice.BeginPrim( GL_TRIANGLE_STRIP ); -// if (renderBlur) { -// gfxDevice.SetPenColor(transColor); -// gfxDevice.WriteVtx(left - blurMargin, bottom + cornerRadius); -// gfxDevice.WriteFinalColor4b(); -// -// gfxDevice.WriteVtx(left - blurMargin, top - cornerRadius); -// gfxDevice.WriteFinalColor4b(); -// } - t = cornerRadius / height; workingColor.Lerp (bottomColor, topColor, t); @@ -6467,16 +6312,6 @@ void MOAIDraw::DrawRoundedRectVerticalGradientFill(float left, float top, float gfxDevice.WriteVtx(right, top - cornerRadius); gfxDevice.WriteFinalColor4b(); -// if (renderBlur) { -// gfxDevice.SetPenColor(transColor); -// gfxDevice.WriteVtx(right + blurMargin, bottom + cornerRadius); -// gfxDevice.WriteFinalColor4b(); -// -// gfxDevice.WriteVtx(right + blurMargin, top - cornerRadius); -// gfxDevice.WriteFinalColor4b(); -// -// } - gfxDevice.EndPrim(); float angle = 90.0f; diff --git a/src/moaicore/MOAIDraw.h b/src/moaicore/MOAIDraw.h index ce13e5cedb..7ec8f8097f 100644 --- a/src/moaicore/MOAIDraw.h +++ b/src/moaicore/MOAIDraw.h @@ -51,8 +51,8 @@ class MOAIDraw : static int _fillCircularGradient ( lua_State* L ); static int _fillCircularSlice ( lua_State* L ); static int _fillCircularSliceGradient ( lua_State* L ); - static int _fillCircularSliceVerticalGradient ( lua_State* L ); static int _fillCircularSliceHorizontalGradient ( lua_State* L ); + static int _fillCircularSliceVerticalGradient ( lua_State* L ); static int _fillEllipse ( lua_State* L ); static int _fillEllipticalGradient ( lua_State* L ); static int _fillEllipticalSlice( lua_State* L ); @@ -63,8 +63,8 @@ class MOAIDraw : static int _fillRect ( lua_State* L ); static int _fillRoundedRect ( lua_State* L ); static int _fillRoundedRectangularGradient ( lua_State* L ); - static int _fillRoundedRectangularVerticalGradient ( lua_State* L ); static int _fillRoundedRectangularHorizontalGradient ( lua_State* L ); + static int _fillRoundedRectangularVerticalGradient ( lua_State* L ); static int _fillTriangularGradient ( lua_State* L ); static int _fillVerticalRectangularGradient ( lua_State* L ); static int _drawTexture ( lua_State* L ); @@ -100,8 +100,8 @@ class MOAIDraw : static void DrawEllipticalGradientFill ( float x, float y, float xRad, float yRad, u32 steps, const USColorVec ¢erColor, const USColorVec &edgeColor ); static void DrawEllipticalSliceFill ( float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps ); static void DrawEllipticalSliceGradientFill ( float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps, const USColorVec ¢erColor, const USColorVec &edgeColor ); - static void DrawEllipticalSliceVerticalGradientFill ( float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps, const USColorVec &startColor, const USColorVec &endColor ); static void DrawEllipticalSliceHorizontalGradientFill ( float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps, const USColorVec &startColor, const USColorVec &endColor ); + static void DrawEllipticalSliceVerticalGradientFill ( float x, float y, float xRad, float yRad, float angle, float offset, float blurMargin, u32 steps, const USColorVec &startColor, const USColorVec &endColor ); static void DrawGrid ( const USRect& rect, u32 xCells, u32 yCells ); static void DrawJoinedCorner ( float x0, float y0, float x1, float y1, float x2, float y2, float lineWidth, float blurMargin ); static void DrawJoinedLine ( lua_State* L, float lineWidth, float blurMargin ); @@ -125,8 +125,8 @@ class MOAIDraw : static void DrawRoundBeveledLine ( lua_State* L, float lineWidth, float blurMargin, u32 steps ); static void DrawRoundedRectFill ( float left, float top, float right, float bottom, float cornerRadius, float blurMargin, u32 steps ); static void DrawRoundedRectGradientFill ( float left, float top, float right, float bottom, float cornerRadius, float blurMargin, u32 steps, const USColorVec ¢erColor, const USColorVec &edgeColor ); - static void DrawRoundedRectVerticalGradientFill ( float left, float top, float right, float bottom, float cornerRadius, float blurMargin, u32 steps, const USColorVec &topColor, const USColorVec &bottomColor ); static void DrawRoundedRectHorizontalGradientFill ( float left, float top, float right, float bottom, float cornerRadius, float blurMargin, u32 steps, const USColorVec &leftColor, const USColorVec &rightColor ); + static void DrawRoundedRectVerticalGradientFill ( float left, float top, float right, float bottom, float cornerRadius, float blurMargin, u32 steps, const USColorVec &topColor, const USColorVec &bottomColor ); static void DrawRoundedRectOutline ( float left, float top, float right, float bottom, float cornerRadius, u32 steps ); static void DrawTexture ( float left, float top, float right, float bottom, MOAITexture* texture ); static void DrawTriangularGradientFill (const USVec2D& v0, const USVec2D& v1, const USVec2D& v2, const USColorVec &color0, const USColorVec &color1, const USColorVec &color2);