- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/NewGamePhysics/GraphicalElements/DotPlotter.cs

    00001
    00002 using System;
    00003 using Microsoft.Xna.Framework;
    00004 using Microsoft.Xna.Framework.Graphics;
    00005
    00006 using NewGamePhysics.Utilities;
    00007
    00008 namespace NewGamePhysics.GraphicalElements
    00009 {
    00013     public class DotPlotter
    00014     {
    00018         private int plotStackSize;
    00019
    00023         Vector2[] plotPositions;
    00024
    00028         Color[] plotColors;
    00029
    00033         int[] plotType;
    00034
    00038         private int headOfPositionStack;
    00039
    00043         private int tailOfPositionStack;
    00044
    00048         private int pointsInStack;
    00049
    00054         public DotPlotter(int count)
    00055         {
    00056             if (count <= 0)
    00057             {
    00058                 throw new ArgumentOutOfRangeException("count");
    00059             }
    00060
    00061             // Keep size
    00062             plotStackSize = count;
    00063             pointsInStack = 0;
    00064
    00065             // Pre-allocate
    00066             plotPositions = new Vector2[plotStackSize];
    00067             plotColors = new Color[plotStackSize];
    00068             plotType = new int[plotStackSize];
    00069             for (int i = 0; i < plotStackSize; i++)
    00070             {
    00071                 plotPositions[i] = new Vector2(0.0f, 0.0f);
    00072                 plotColors[i] = new Color();
    00073                 plotType[i] = 0;
    00074             }
    00075
    00076             // Empty stack
    00077             headOfPositionStack = 0;
    00078             tailOfPositionStack = 0;
    00079         }
    00080
    00089         private void DrawSegment(Vector2 start, Vector2 finish, Color color, int emphasis, PrimitiveBatch primitiveBatch)
    00090         {
    00091             // Always draw line segment part
    00092             primitiveBatch.AddVertex(start, color);
    00093             primitiveBatch.AddVertex(finish, color);
    00094             // Emphasis
    00095             if (emphasis == 1)
    00096             {
    00097                 float size = 2.0f;
    00098                 Vector2 ul = new Vector2(-size, -size);
    00099                 Vector2 ur = new Vector2( size, -size);
    00100                 Vector2 bl = new Vector2(-size,  size);
    00101                 Vector2 br = new Vector2( size,  size);
    00102                 primitiveBatch.AddVertex(finish + ul, color);
    00103                 primitiveBatch.AddVertex(finish + ur, color);
    00104                 primitiveBatch.AddVertex(finish + ur, color);
    00105                 primitiveBatch.AddVertex(finish + br, color);
    00106                 primitiveBatch.AddVertex(finish + br, color);
    00107                 primitiveBatch.AddVertex(finish + bl, color);
    00108                 primitiveBatch.AddVertex(finish + bl, color);
    00109                 primitiveBatch.AddVertex(finish + ul, color);
    00110             }
    00111         }
    00112
    00118         public void Draw(GameTime gameTime, PrimitiveBatch primitiveBatch)
    00119         {
    00120             if (primitiveBatch == null)
    00121             {
    00122                 throw new ArgumentNullException("primitiveBatch");
    00123             }
    00124
    00125             // Do we have enough points
    00126             if (pointsInStack > 1)
    00127             {
    00128                 primitiveBatch.Begin(PrimitiveType.LineList);
    00129                 if (headOfPositionStack > tailOfPositionStack)
    00130                 {
    00131                     for (int i = tailOfPositionStack; i < (headOfPositionStack - 1); i++)
    00132                     {
    00133                         DrawSegment(plotPositions[i], plotPositions[(i + 1) % plotStackSize],
    00134                                     plotColors[i], plotType[i], primitiveBatch);
    00135                     }
    00136                 }
    00137                 else if (headOfPositionStack < tailOfPositionStack)
    00138                 {
    00139                     for (int i = tailOfPositionStack; i < plotStackSize; i++)
    00140                     {
    00141                         DrawSegment(plotPositions[i], plotPositions[(i + 1) % plotStackSize],
    00142                                     plotColors[i], plotType[i], primitiveBatch);
    00143                     }
    00144                     if (headOfPositionStack > 0)
    00145                     {
    00146                         for (int i = 0; i < (headOfPositionStack - 1); i++)
    00147                         {
    00148                             DrawSegment(plotPositions[i], plotPositions[(i + 1) % plotStackSize],
    00149                                         plotColors[i], plotType[i], primitiveBatch);
    00150                         }
    00151                     }
    00152                 }
    00153                 else
    00154                 {
    00155                     // if head==tail there is nothing to do
    00156                 }
    00157                 primitiveBatch.End();
    00158             }
    00159         }
    00160
    00165         public void MoveDots(Vector2 displacement)
    00166         {
    00167             for (int i=0; i<plotStackSize; i++)
    00168             {
    00169                 plotPositions[i] += displacement;
    00170             }
    00171         }
    00172
    00179         public void AddDot(Vector2 targetPosition, Color color, int type)
    00180         {
    00181             // Store plot position and attributes
    00182             plotPositions[headOfPositionStack].X = targetPosition.X;
    00183             plotPositions[headOfPositionStack].Y = targetPosition.Y;
    00184             plotColors[headOfPositionStack] = color;
    00185             plotType[headOfPositionStack] = type;
    00186
    00187             // Advance head to next empty slot
    00188             headOfPositionStack++;
    00189
    00190             // Maybe wrap head
    00191             if (headOfPositionStack == plotStackSize)
    00192             {
    00193                 headOfPositionStack = 0;
    00194             }
    00195
    00196             // Maybe move tail
    00197             if (headOfPositionStack == tailOfPositionStack)
    00198             {
    00199                 // Also advance tail
    00200                 tailOfPositionStack++;
    00201
    00202                 // Maybe wrap tail
    00203                 if (tailOfPositionStack == plotStackSize)
    00204                 {
    00205                     tailOfPositionStack = 0;
    00206                 }
    00207             }
    00208
    00209             // Count points
    00210             if (pointsInStack < plotStackSize)
    00211             {
    00212                 pointsInStack++;
    00213             }
    00214         }
    00215
    00220         public void SetTypeAtHead(int type)
    00221         {
    00222             // Do we have points?
    00223             if (pointsInStack > 0)
    00224             {
    00225                 if (headOfPositionStack > 0)
    00226                 {
    00227                     // Previous position is last point added.
    00228                     plotType[headOfPositionStack - 1] = type;
    00229                 }
    00230                 else
    00231                 {
    00232                     // Last element in stack is the last point added.
    00233                     plotType[plotStackSize - 1] = type;
    00234                 }
    00235             }
    00236         }
    00237     }
    00238 }