public bool SetDrawable(IDrawn drawable)
        {
            if (!nameList.ContainsKey(drawable.Name))
            {
                return(false);
            }
            if (nameList[drawable.Name] != drawable.DrawnType)
            {
                return(false);
            }
            switch (drawable.DrawnType)
            {
            case DrawnType.Animation:
                Animation animation = (Animation)drawable;
                animationList[animation.Name] = animation;
                return(true);

            case DrawnType.Shape:
                IDrawnShape shape = (IDrawnShape)drawable;
                shapeList[shape.Name] = shape;
                return(true);

            case DrawnType.String:
                DrawnString drawString = (DrawnString)drawable;
                stringList[drawString.Name] = drawString;
                return(true);
            }
            return(false);
        }
        public DrawnString GetDrawString(string objectName)
        {
            if (!drawList.ContainsKey(objectName))
            {
                throw new Exception("String " + objectName + " does not exist");
            }
            DrawnString drawString = (DrawnString)drawList[objectName];

            return(drawString);
        }
Exemple #3
0
        public GraphicsData(Dictionary <string, IDrawn> drawnObjects, Dictionary <string, Texture2D> animationTextures, Dictionary <string, Texture2D> pixelTextures)
        {
            DrawnObjects      = new List <IDrawn>();
            AnimationTextures = new List <BinaryTexture>();
            PixelTextures     = new List <BinaryTexture>();

            foreach (KeyValuePair <string, IDrawn> drawnObject in drawnObjects)
            {
                switch (drawnObject.Value.DrawnType)
                {
                case DrawnType.Animation:
                    Animation animation = (Animation)drawnObject.Value;
                    DrawnObjects.Add(animation.CloneAnimation(animation.Name));
                    break;

                case DrawnType.Shape:
                    IDrawnShape shape = (IDrawnShape)drawnObject.Value;
                    switch (shape.Shape)
                    {
                    case ShapeType.Line:
                        DrawnLine line = (DrawnLine)shape;
                        DrawnObjects.Add(line.Clone(line.Name));
                        break;

                    case ShapeType.Rectangle:
                        DrawnRectangle rectangle = (DrawnRectangle)shape;
                        DrawnObjects.Add(rectangle.Clone(rectangle.Name));
                        break;
                    }
                    break;

                case DrawnType.String:
                    DrawnString drawString = (DrawnString)drawnObject.Value;
                    DrawnObjects.Add(drawString.Clone(drawString.Name));
                    break;
                }
            }
            foreach (BinaryTexture newTexture in animationTextures.Select(pair => new BinaryTexture(pair.Value)))
            {
                AnimationTextures.Add(newTexture);
            }
            foreach (BinaryTexture pixelTexture in pixelTextures.Select(pair => new BinaryTexture(pair.Value)))
            {
                PixelTextures.Add(pixelTexture);
            }
        }
        public bool AddDrawable(IDrawn drawable)
        {
            switch (drawable.DrawnType)
            {
            case DrawnType.Animation:
                Animation animation = (Animation)drawable;
                if (nameList.ContainsKey(animation.Name))
                {
                    return(false);
                }
                animationList.Add(animation.Name, animation);
                nameList.Add(animation.Name, DrawnType.Animation);
                return(true);

            case DrawnType.Shape:
                IDrawnShape shape = (IDrawnShape)drawable;
                if (nameList.ContainsKey(shape.Name))
                {
                    return(false);
                }
                shapeList.Add(shape.Name, shape);
                nameList.Add(shape.Name, DrawnType.Shape);
                textureManager.PixelTextures.Add(shape.Name, CreatePixel(shape.Color));
                return(true);

            case DrawnType.String:
                DrawnString drawString = (DrawnString)drawable;
                if (nameList.ContainsKey(drawString.Name))
                {
                    return(false);
                }
                nameList.Add(drawString.Name, DrawnType.String);
                stringList.Add(drawString.Name, drawString);
                return(true);
            }
            return(false);
        }
        public void Draw()
        {
            foreach (KeyValuePair <string, IDrawn> drawable in drawList)
            {
                switch (drawable.Value.DrawnType)
                {
                case DrawnType.Animation:
                    Animation animation      = (Animation)drawable.Value;
                    Texture2D drawTexture    = textureManager.Textures[animation.Texture];
                    Rectangle frameRectangle = GameRectangle.ConvertToRectangle(animation.Frames[animation.Frame].TextureSource);
                    SpriteBatch.Draw(drawTexture, animation.GetPosition(), frameRectangle, Color.White, animation.Rotation, animation.GetOrigin(), animation.Scale, SpriteEffects.None, animation.Depth);
                    break;

                case DrawnType.Shape:
                    IDrawnShape shape = (IDrawnShape)drawable.Value;
                    switch (shape.Shape)
                    {
                    case ShapeType.Line:
                        DrawnLine drawLine = (DrawnLine)shape;
                        DrawLine(drawLine);
                        break;

                    case ShapeType.Rectangle:
                        DrawnRectangle drawRectangle = (DrawnRectangle)shape;
                        DrawRectangle(drawRectangle);
                        break;
                    }
                    break;

                case DrawnType.String:
                    DrawnString drawString = (DrawnString)drawable.Value;
                    DrawString(drawString);
                    break;
                }
            }
        }
 private void DrawString(DrawnString drawString)
 {
     SpriteBatch.DrawString(drawString.Font, drawString.Value, new Vector2(drawString.X, drawString.Y), drawString.Color);
 }
 private void DrawString(DrawnString drawString)
 {
     SpriteBatch.DrawString(drawString.Font, drawString.Value, new Vector2(drawString.X, drawString.Y), drawString.Color);
 }
        public bool UpdateDrawList(DrawParam param)
        {
            if (!drawList.ContainsKey(param.ObjectName))
            {
                return(false);
            }
            if (drawCache.Cache.ContainsKey(param.ObjectName))
            {
                ObjectCache cache = drawCache.Cache[param.ObjectName];
                if (cache.Cache.ContainsKey(param.DrawableName))
                {
                    IDrawn drawableObject = cache.Cache[param.DrawableName];
                    drawableObject.X = param.Position.X;
                    drawableObject.Y = param.Position.Y;
                    switch (drawableObject.DrawnType)
                    {
                    case DrawnType.Animation:
                        Animation animation = (Animation)drawableObject;
                        animation.Reset();
                        drawList[param.ObjectName] = animation;
                        return(true);

                    default:
                        drawList[param.ObjectName] = drawableObject;
                        return(true);
                    }
                }
            }
            else
            {
                drawCache.Cache.Add(param.ObjectName, new ObjectCache());
            }
            ObjectCache objCache = drawCache.Cache[param.ObjectName];

            switch (param.DrawType)
            {
            case DrawnType.Animation:
                if (!animationList.ContainsKey(param.DrawableName))
                {
                    return(false);
                }
                Animation animation = animationList[param.DrawableName].CloneAnimation(param.DrawableName);
                animation.Reset();
                animation.X = param.Position.X;
                animation.Y = param.Position.Y;
                drawList.Add(param.ObjectName, animation);
                objCache.Cache.Add(animation.Name, animation);
                break;

            case DrawnType.Shape:
                if (!shapeList.ContainsKey(param.DrawableName))
                {
                    return(false);
                }
                IDrawnShape shape = shapeList[param.DrawableName];
                switch (shape.Shape)
                {
                case ShapeType.Line:
                    DrawnLine line     = (DrawnLine)shape;
                    DrawnLine drawLine = line.Clone(line.Name);
                    drawLine.X = param.Position.X;
                    drawLine.Y = param.Position.Y;
                    drawList.Add(param.ObjectName, drawLine);
                    objCache.Cache.Add(drawLine.Name, drawLine);
                    return(true);

                case ShapeType.Rectangle:
                    DrawnRectangle rectangle     = (DrawnRectangle)shape;
                    DrawnRectangle drawRectangle = rectangle.Clone(rectangle.Name);
                    drawRectangle.X = param.Position.X;
                    drawRectangle.Y = param.Position.Y;
                    drawList.Add(param.ObjectName, drawRectangle);
                    objCache.Cache.Add(drawRectangle.Name, drawRectangle);
                    return(true);
                }
                break;

            case DrawnType.String:
                if (!stringList.ContainsKey(param.DrawableName))
                {
                    return(false);
                }
                DrawnString drawString = stringList[param.DrawableName].Clone(param.DrawableName);
                drawString.X = param.Position.X;
                drawString.Y = param.Position.Y;
                drawList.Add(param.ObjectName, drawString);
                objCache.Cache.Add(drawString.Name, drawString);
                return(true);
            }
            return(false);
        }