public static void DrawLine(CCPoint origin, CCPoint destination, CCColor4B color) { var a = origin; var b = destination; var n = CCPoint.Normalize(CCPoint.PerpendicularCCW(a - b)); var lww = LineWidth * 0.5f; var nw = n * lww; var v0 = b - nw; var v1 = b + nw; var v2 = a - nw; var v3 = a + nw; // Triangles from beginning to end batch.AddVertex(v1, color, PrimitiveType.TriangleList); batch.AddVertex(v2, color, PrimitiveType.TriangleList); batch.AddVertex(v0, color, PrimitiveType.TriangleList); batch.AddVertex(v1, color, PrimitiveType.TriangleList); batch.AddVertex(v2, color, PrimitiveType.TriangleList); batch.AddVertex(v3, color, PrimitiveType.TriangleList); }
public void DrawPolygon(CCPoint[] verts, int count, CCColor4F fillColor, float borderWidth, CCColor4F borderColor) { var extrude = new ExtrudeVerts[count]; for (int i = 0; i < count; i++) { var v0 = verts[(i - 1 + count) % count]; var v1 = verts[i]; var v2 = verts[(i + 1) % count]; var n1 = CCPoint.Normalize(CCPoint.PerpendicularCCW(v1 - v0)); var n2 = CCPoint.Normalize(CCPoint.PerpendicularCCW(v2 - v1)); var offset = (n1 + n2) * (1.0f / (CCPoint.Dot(n1, n2) + 1.0f)); extrude[i] = new ExtrudeVerts() { offset = offset, n = n2 }; } bool outline = (borderColor.A > 0.0f && borderWidth > 0.0f); var colorFill = new CCColor4B(fillColor); var borderFill = new CCColor4B(borderColor); float inset = (!outline ? 0.5f : 0.0f); for (int i = 0; i < count - 2; i++) { var v0 = verts[0] - (extrude[0].offset * inset); var v1 = verts[i + 1] - (extrude[i + 1].offset * inset); var v2 = verts[i + 2] - (extrude[i + 2].offset * inset); AddTriangleVertex(new CCV3F_C4B(v0, colorFill)); //__t(v2fzero) AddTriangleVertex(new CCV3F_C4B(v1, colorFill)); //__t(v2fzero) AddTriangleVertex(new CCV3F_C4B(v2, colorFill)); //__t(v2fzero) } for (int i = 0; i < count; i++) { int j = (i + 1) % count; var v0 = verts[i]; var v1 = verts[j]; var n0 = extrude[i].n; var offset0 = extrude[i].offset; var offset1 = extrude[j].offset; if (outline) { var inner0 = (v0 - (offset0 * borderWidth)); var inner1 = (v1 - (offset1 * borderWidth)); var outer0 = (v0 + (offset0 * borderWidth)); var outer1 = (v1 + (offset1 * borderWidth)); AddTriangleVertex(new CCV3F_C4B(inner0, borderFill)); //__t(v2fneg(n0)) AddTriangleVertex(new CCV3F_C4B(inner1, borderFill)); //__t(v2fneg(n0)) AddTriangleVertex(new CCV3F_C4B(outer1, borderFill)); //__t(n0) AddTriangleVertex(new CCV3F_C4B(inner0, borderFill)); //__t(v2fneg(n0)) AddTriangleVertex(new CCV3F_C4B(outer0, borderFill)); //__t(n0) AddTriangleVertex(new CCV3F_C4B(outer1, borderFill)); //__t(n0) } else { var inner0 = (v0 - (offset0 * 0.5f)); var inner1 = (v1 - (offset1 * 0.5f)); var outer0 = (v0 + (offset0 * 0.5f)); var outer1 = (v1 + (offset1 * 0.5f)); AddTriangleVertex(new CCV3F_C4B(inner0, colorFill)); //__t(v2fzero) AddTriangleVertex(new CCV3F_C4B(inner1, colorFill)); //__t(v2fzero) AddTriangleVertex(new CCV3F_C4B(outer1, colorFill)); //__t(n0) AddTriangleVertex(new CCV3F_C4B(inner0, colorFill)); //__t(v2fzero) AddTriangleVertex(new CCV3F_C4B(outer0, colorFill)); //__t(n0) AddTriangleVertex(new CCV3F_C4B(outer1, colorFill)); //__t(n0) } } dirty = true; }
public void DrawPolygon(CCPoint[] verts, int count, CCColor4F fillColor, float borderWidth, CCColor4F borderColor, bool closePolygon = true) { var polycount = count; var colorFill = new CCColor4B(fillColor); var borderFill = new CCColor4B(borderColor); bool outline = (borderColor.A > 0.0f && borderWidth > 0.0f); bool fill = fillColor.A > 0.0f; var numberOfTriangles = outline ? (3 * polycount - 2) : (polycount - 2); if (numberOfTriangles > 0 && fill && closePolygon) { for (int i = 1; i < polycount - 1; i++) { AddTriangleVertex(new CCV3F_C4B(verts[0], colorFill)); AddTriangleVertex(new CCV3F_C4B(verts[i], colorFill)); AddTriangleVertex(new CCV3F_C4B(verts[i + 1], colorFill)); } } else { for (int i = 0; i < polycount - 1; i++) { DrawLine(verts[i], verts[i + 1], borderWidth, colorFill); } } if (outline) { var extrude = new ExtrudeVerts[polycount]; for (int i = 0; i < polycount; i++) { var v0 = verts[(i - 1 + polycount) % polycount]; var v1 = verts[i]; var v2 = verts[(i + 1) % polycount]; var n1 = CCPoint.Normalize(CCPoint.PerpendicularCCW(v1 - v0)); var n2 = CCPoint.Normalize(CCPoint.PerpendicularCCW(v2 - v1)); var offset = (n1 + n2) * (1.0f / (CCPoint.Dot(n1, n2) + 1.0f)); extrude[i] = new ExtrudeVerts() { offset = offset, n = n2 }; } float inset = (!outline ? 0.5f : 0.0f); for (int i = 0; i < polycount - 2; i++) { var v0 = verts[0] - (extrude[0].offset * inset); var v1 = verts[i + 1] - (extrude[i + 1].offset * inset); var v2 = verts[i + 2] - (extrude[i + 2].offset * inset); AddTriangleVertex(new CCV3F_C4B(v0, colorFill)); //__t(v2fzero) AddTriangleVertex(new CCV3F_C4B(v1, colorFill)); //__t(v2fzero) AddTriangleVertex(new CCV3F_C4B(v2, colorFill)); //__t(v2fzero) } for (int i = 0; i < polycount - 1; i++) { int j = (i + 1) % polycount; var v0 = verts[i]; var v1 = verts[j]; var offset0 = extrude[i].offset; var offset1 = extrude[j].offset; var inner0 = (v0 - (offset0 * borderWidth)); var inner1 = (v1 - (offset1 * borderWidth)); var outer0 = (v0 + (offset0 * borderWidth)); var outer1 = (v1 + (offset1 * borderWidth)); AddTriangleVertex(new CCV3F_C4B(inner0, borderFill)); AddTriangleVertex(new CCV3F_C4B(inner1, borderFill)); AddTriangleVertex(new CCV3F_C4B(outer1, borderFill)); AddTriangleVertex(new CCV3F_C4B(inner0, borderFill)); AddTriangleVertex(new CCV3F_C4B(outer0, borderFill)); AddTriangleVertex(new CCV3F_C4B(outer1, borderFill)); } if (closePolygon) { for (int i = polycount - 1; i < polycount; i++) { int j = (i + 1) % polycount; var v0 = verts[i]; var v1 = verts[j]; var offset0 = extrude[i].offset; var offset1 = extrude[j].offset; var inner0 = (v0 - (offset0 * borderWidth)); var inner1 = (v1 - (offset1 * borderWidth)); var outer0 = (v0 + (offset0 * borderWidth)); var outer1 = (v1 + (offset1 * borderWidth)); AddTriangleVertex(new CCV3F_C4B(inner0, borderFill)); AddTriangleVertex(new CCV3F_C4B(inner1, borderFill)); AddTriangleVertex(new CCV3F_C4B(outer1, borderFill)); AddTriangleVertex(new CCV3F_C4B(inner0, borderFill)); AddTriangleVertex(new CCV3F_C4B(outer0, borderFill)); AddTriangleVertex(new CCV3F_C4B(outer1, borderFill)); } } } dirty = true; }
/// Clamp CCPoint p to length len. public static CCPoint Clamp(CCPoint p, float len) { return((CCPoint.Dot(p, p) > len * len) ? CCPoint.Normalize(p) * len : p); }
void ProcessGamePad(CCWindow window, GamePadState gps, PlayerIndex player) { var dispatcher = window.EventDispatcher; var lastState = new GamePadState(); if (!priorGamePadState.ContainsKey(player) && gps.IsConnected) { gamePadConnection.IsConnected = true; gamePadConnection.Player = (CCPlayerIndex)player; dispatcher.DispatchEvent(gamePadConnection); } if (priorGamePadState.ContainsKey(player)) { lastState = priorGamePadState[player]; // Notify listeners when the gamepad is connected/disconnected. if ((lastState.IsConnected != gps.IsConnected)) { gamePadConnection.IsConnected = false; gamePadConnection.Player = (CCPlayerIndex)player; dispatcher.DispatchEvent(gamePadConnection); } // TODO: Check button pressed/released status for button tap events. } if (gps.IsConnected) { var caps = GamePad.GetCapabilities(player); if (caps.HasBackButton || caps.HasStartButton || caps.HasBigButton || caps.HasAButton || caps.HasBButton || caps.HasXButton || caps.HasYButton || caps.HasLeftShoulderButton || caps.HasRightShoulderButton) { var back = CCGamePadButtonStatus.NotApplicable; var start = CCGamePadButtonStatus.NotApplicable; var system = CCGamePadButtonStatus.NotApplicable; var a = CCGamePadButtonStatus.NotApplicable; var b = CCGamePadButtonStatus.NotApplicable; var x = CCGamePadButtonStatus.NotApplicable; var y = CCGamePadButtonStatus.NotApplicable; var leftShoulder = CCGamePadButtonStatus.NotApplicable; var rightShoulder = CCGamePadButtonStatus.NotApplicable; if (caps.HasBackButton) { back = (gps.Buttons.Back == ButtonState.Pressed ? CCGamePadButtonStatus.Pressed : CCGamePadButtonStatus.Released); } if (caps.HasStartButton) { start = (gps.Buttons.Start == ButtonState.Pressed ? CCGamePadButtonStatus.Pressed : CCGamePadButtonStatus.Released); } if (caps.HasBigButton) { system = (gps.Buttons.BigButton == ButtonState.Pressed ? CCGamePadButtonStatus.Pressed : CCGamePadButtonStatus.Released); } if (caps.HasAButton) { a = (gps.Buttons.A == ButtonState.Pressed ? CCGamePadButtonStatus.Pressed : CCGamePadButtonStatus.Released); } if (caps.HasBButton) { b = (gps.Buttons.B == ButtonState.Pressed ? CCGamePadButtonStatus.Pressed : CCGamePadButtonStatus.Released); } if (caps.HasXButton) { x = (gps.Buttons.X == ButtonState.Pressed ? CCGamePadButtonStatus.Pressed : CCGamePadButtonStatus.Released); } if (caps.HasYButton) { y = (gps.Buttons.Y == ButtonState.Pressed ? CCGamePadButtonStatus.Pressed : CCGamePadButtonStatus.Released); } if (caps.HasLeftShoulderButton) { leftShoulder = (gps.Buttons.LeftShoulder == ButtonState.Pressed ? CCGamePadButtonStatus.Pressed : CCGamePadButtonStatus.Released); } if (caps.HasRightShoulderButton) { rightShoulder = (gps.Buttons.RightShoulder == ButtonState.Pressed ? CCGamePadButtonStatus.Pressed : CCGamePadButtonStatus.Released); } gamePadButton.Back = back; gamePadButton.Start = start; gamePadButton.System = system; gamePadButton.A = a; gamePadButton.B = b; gamePadButton.X = x; gamePadButton.Y = y; gamePadButton.LeftShoulder = leftShoulder; gamePadButton.RightShoulder = rightShoulder; gamePadButton.Player = (CCPlayerIndex)player; dispatcher.DispatchEvent(gamePadButton); } // Process the game sticks if ((caps.HasLeftXThumbStick || caps.HasLeftYThumbStick || caps.HasRightXThumbStick || caps.HasRightYThumbStick || caps.HasLeftStickButton || caps.HasRightStickButton)) { CCPoint vecLeft; if (caps.HasLeftXThumbStick || caps.HasLeftYThumbStick) { vecLeft = new CCPoint(gps.ThumbSticks.Left); vecLeft.Normalize(); } else { vecLeft = CCPoint.Zero; } CCPoint vecRight; if (caps.HasRightXThumbStick || caps.HasRightYThumbStick) { vecRight = new CCPoint(gps.ThumbSticks.Right); vecRight.Normalize(); } else { vecRight = CCPoint.Zero; } var left = new CCGameStickStatus(); left.Direction = vecLeft; left.Magnitude = ((caps.HasLeftXThumbStick || caps.HasLeftYThumbStick) ? gps.ThumbSticks.Left.Length() : 0f); left.IsDown = ((caps.HasLeftStickButton) ? gps.IsButtonDown(Buttons.LeftStick) : false); var right = new CCGameStickStatus(); right.Direction = vecRight; right.Magnitude = ((caps.HasRightXThumbStick || caps.HasRightYThumbStick) ? gps.ThumbSticks.Right.Length() : 0f); right.IsDown = ((caps.HasLeftStickButton) ? gps.IsButtonDown(Buttons.RightStick) : false); gamePadStick.Left = left; gamePadStick.Right = right; gamePadStick.Player = (CCPlayerIndex)player; dispatcher.DispatchEvent(gamePadStick); } // Process the game triggers if (caps.HasLeftTrigger || caps.HasRightTrigger) { //GamePadTriggerUpdate (caps.HasLeftTrigger ? gps.Triggers.Left : 0f, caps.HasRightTrigger ? gps.Triggers.Right : 0f, player); gamePadTrigger.Left = caps.HasLeftTrigger ? gps.Triggers.Left : 0f; gamePadTrigger.Right = caps.HasRightTrigger ? gps.Triggers.Right : 0f; gamePadTrigger.Player = (CCPlayerIndex)player; dispatcher.DispatchEvent(gamePadTrigger); } // Process the D-Pad if (caps.HasDPadDownButton || caps.HasDPadUpButton || caps.HasDPadLeftButton || caps.HasDPadRightButton) { var leftButton = CCGamePadButtonStatus.NotApplicable; var rightButton = CCGamePadButtonStatus.NotApplicable; var upButton = CCGamePadButtonStatus.NotApplicable; var downButton = CCGamePadButtonStatus.NotApplicable; if (caps.HasDPadDownButton) { downButton = (gps.DPad.Down == ButtonState.Pressed ? CCGamePadButtonStatus.Pressed : CCGamePadButtonStatus.Released); } if (caps.HasDPadUpButton) { upButton = (gps.DPad.Up == ButtonState.Pressed ? CCGamePadButtonStatus.Pressed : CCGamePadButtonStatus.Released); } if (caps.HasDPadLeftButton) { leftButton = (gps.DPad.Left == ButtonState.Pressed ? CCGamePadButtonStatus.Pressed : CCGamePadButtonStatus.Released); } if (caps.HasDPadRightButton) { rightButton = (gps.DPad.Right == ButtonState.Pressed ? CCGamePadButtonStatus.Pressed : CCGamePadButtonStatus.Released); } gamePadDPad.Down = downButton; gamePadDPad.Up = upButton; gamePadDPad.Left = leftButton; gamePadDPad.Right = rightButton; gamePadDPad.Player = (CCPlayerIndex)player; dispatcher.DispatchEvent(gamePadDPad); } } priorGamePadState[player] = gps; }
static void VertexLineToPolygon(CCPoint[] points, float stroke, CCV3F_C4B_T2F[] vertices, int offset, int nuPoints) { nuPoints += offset; if (nuPoints <= 1) { return; } stroke *= 0.5f; int idx; int nuPointsMinus = nuPoints - 1; float rad70 = MathHelper.ToRadians(70); float rad170 = MathHelper.ToRadians(170); for (int i = offset; i < nuPoints; i++) { idx = i * 2; CCPoint p1 = points[i]; CCPoint perpVector; if (i == 0) { perpVector = CCPoint.PerpendicularCCW(CCPoint.Normalize(p1 - points[i + 1])); } else if (i == nuPointsMinus) { perpVector = CCPoint.PerpendicularCCW(CCPoint.Normalize(points[i - 1] - p1)); } else { CCPoint p2 = points[i + 1]; CCPoint p0 = points[i - 1]; CCPoint p2p1 = CCPoint.Normalize(p2 - p1); CCPoint p0p1 = CCPoint.Normalize(p0 - p1); // Calculate angle between vectors var angle = (float)Math.Acos(CCPoint.Dot(p2p1, p0p1)); if (angle < rad70) { perpVector = CCPoint.PerpendicularCCW(CCPoint.Normalize(CCPoint.Midpoint(p2p1, p0p1))); } else if (angle < rad170) { perpVector = CCPoint.Normalize(CCPoint.Midpoint(p2p1, p0p1)); } else { perpVector = CCPoint.PerpendicularCCW(CCPoint.Normalize(p2 - p0)); } } perpVector = perpVector * stroke; vertices[idx].Vertices = new CCVertex3F(p1.X + perpVector.X, p1.Y + perpVector.Y, 0); vertices[idx + 1].Vertices = new CCVertex3F(p1.X - perpVector.X, p1.Y - perpVector.Y, 0); } // Validate vertexes offset = (offset == 0) ? 0 : offset - 1; for (int i = offset; i < nuPointsMinus; i++) { idx = i * 2; int idx1 = idx + 2; CCVertex3F p1 = vertices[idx].Vertices; CCVertex3F p2 = vertices[idx + 1].Vertices; CCVertex3F p3 = vertices[idx1].Vertices; CCVertex3F p4 = vertices[idx1 + 1].Vertices; float s; bool fixVertex = !VertexLineIntersect(p1.X, p1.Y, p4.X, p4.Y, p2.X, p2.Y, p3.X, p3.Y, out s); if (!fixVertex) { if (s < 0.0f || s > 1.0f) { fixVertex = true; } } if (fixVertex) { vertices[idx1].Vertices = p4; vertices[idx1 + 1].Vertices = p3; } } }