Exemplo n.º 1
0
        /// <summary>
        /// Updates the drawable. If the drawable changed in any
        /// visible manner, then this returns true.
        /// </summary>
        public bool Update(DrawableState state)
        {
            // Don't bother if we only have one frame
            if (tile.Count == 1)
            {
                return(false);
            }

            // Figure out the different in time
            TileFrame frame = tile.Frames[state.Frame];
            long      now   = DateTime.UtcNow.Ticks / 10000;
            long      diff  = now - state.LastUpdate;

            // If not enough time has passed, just move on
            if (diff < frame.Delay)
            {
                return(false);
            }

            // We need to move to the next one
            long fdiff = diff - frame.Delay;

            if (fdiff > frame.Delay)
            {
                fdiff %= frame.Delay;
            }

            state.Frame      = frame.NextFrame;
            state.LastUpdate = now + fdiff;
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This method is called when the drawable has to draw itself
        /// to a Gdk.Drawable object.
        /// </summary>
        public void Render(
            Drawable dest,
            GC gc,
            DrawableState state)
        {
            // Check for single frame image
            if (tile.Count == 1)
            {
                drawable.Render(dest, gc, state);
                return;
            }

            // Render ourselves
            Pixbuf pixbuf = GetPixbuf(state);

            dest.DrawPixbuf(
                gc,
                pixbuf,
                0,
                0,
                state.X,
                state.Y,
                pixbuf.Width,
                pixbuf.Height,
                RgbDither.None,
                0,
                0);
        }
Exemplo n.º 3
0
        /// <summary>
        /// An internal function to actually create the rsvg. This
        /// method also adds it to the cache.
        /// </summary>
        protected override Pixbuf CreatePixbuf(
			DrawableState state,
			string cacheKey)
        {
            // Render the image
            Pixbuf p = Rsvg.Pixbuf.FromFileAtSize(
                file.FullName, state.Width, state.Height);
            PixbufCache[cacheKey] = p;
            return p;
        }
Exemplo n.º 4
0
        /// <summary>
        /// An internal function to actually create the rsvg. This
        /// method also adds it to the cache.
        /// </summary>
        protected override Pixbuf CreatePixbuf(
            DrawableState state,
            string cacheKey)
        {
            // Render the image
            Pixbuf p = Rsvg.Pixbuf.FromFileAtSize(
                file.FullName, state.Width, state.Height);

            PixbufCache[cacheKey] = p;
            return(p);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Contains the rendered pixbuf of this element.
        /// </summary>
        public virtual Pixbuf GetPixbuf(DrawableState state)
        {
            // Figure out our key for the cache
            string cacheKey = GetCacheKey(state);

            // Check the cache
            if (PixbufCache.Contains(cacheKey))
            {
                return(PixbufCache[cacheKey]);
            }
            else
            {
                return(CreatePixbuf(state, cacheKey));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Contains the rendered pixbuf of this element.
        /// </summary>
        public virtual Pixbuf GetPixbuf(DrawableState state)
        {
            // Figure out our key for the cache
            string cacheKey = GetCacheKey(state);

            // Check the cache
            if (PixbufCache.Contains(cacheKey))
            {
                return PixbufCache[cacheKey];
            }
            else
            {
                return CreatePixbuf(state, cacheKey);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// This method is called when the drawable has to draw itself
        /// to a Gdk.Drawable area. This handles the scaling and does
        /// the actual image loading.
        /// </summary>
        public void Render(
            Drawable dest,
            GC gc,
            DrawableState state)
        {
            Pixbuf pixbuf = GetPixbuf(state);

            dest.DrawPixbuf(
                gc,
                pixbuf,
                0,
                0,
                state.X,
                state.Y,
                pixbuf.Width,
                pixbuf.Height,
                RgbDither.None,
                0,
                0);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Randomizes the drawable state based on this specific
        /// drawable.
        /// </summary>
        public void Randomize(DrawableState state)
        {
            // Pick a random frame. We loop this since a frame may not
            // be randomizeable (the Random property is false).
            while (true)
            {
                // Pick one
                int       seq   = random.Next(0, tile.Count - 1);
                TileFrame frame = tile.Frames[seq];

                if (frame.Random)
                {
                    // We can use this as a random. We also randomize
                    // the last update to have not everything updating
                    // at the same time.
                    state.Frame      = seq;
                    state.LastUpdate = DateTime.UtcNow.Ticks / 10000 +
                                       random.Next(0, frame.Delay);
                    return;
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// An internal function to actually create the pixbuf. This
        /// method also adds it to the cache.
        /// </summary>
        protected virtual Pixbuf CreatePixbuf(
            DrawableState state,
            string cacheKey)
        {
            // Create the image from the disk. If we are exactly the
            // right size, return it immediately.
            Pixbuf image = new Pixbuf(file.FullName);

            if (image.Width == state.Width && image.Height == state.Height)
            {
                return(image);
            }

            // Create a pixbuf of the given size
            Colorspace colorspace    = image.Colorspace;
            bool       hasAlpha      = image.HasAlpha;
            int        bitsPerSample = image.BitsPerSample;
            Pixbuf     p             = new Pixbuf(
                colorspace, hasAlpha, bitsPerSample, state.Width, state.Height);

            // Scale copy it in
            image.Composite(
                p,
                0,
                0,
                state.Width,
                state.Height,
                0,
                0,
                image.Width / state.Width,
                image.Height / state.Height,
                InterpType.Bilinear,
                255);

            // Cache and return
            PixbufCache[cacheKey] = p;
            return(p);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Constructs the token sprite based on the individual
        /// components. This is the "view" part of a token.
        /// </summary>
        public TokenSprite(
			Display display,
			Token token)
        {
            this.display = display;
            this.token = token;

            // Create the tile
            typeSprite = token.TypeSpriteName;
            tileDrawable = Game.Theme.DrawableFactory.Create(typeSprite);

            // Create the letter
            valueSprite = token.ValueSpriteName;
            letterDrawable = Game.Theme.DrawableFactory.Create(valueSprite);
            letterState = new DrawableState();

            // Create the selected
            selectedDrawable = Game.Theme.DrawableFactory.Create("selected");
            selectedState = new DrawableState();

            // Randomize it
            Randomize();
        }
Exemplo n.º 11
0
 /// <summary>
 /// Every drawable contains a CacheKey which is a unique
 /// string to identify the drawable, it's size, and its
 /// alterations (such as color mapping).
 /// </summary>
 public string GetCacheKey(DrawableState state)
 {
     return(String.Format("{0}:f{1}", drawable.GetCacheKey(state), state.Frame));
 }
Exemplo n.º 12
0
 /// <summary>
 /// Randomizes the drawable state based on this specific
 /// drawable.
 /// </summary>
 public virtual void Randomize(DrawableState state)
 {
 }
Exemplo n.º 13
0
        /// <summary>
        /// This method is called when the drawable has to draw itself
        /// to a Gdk.Drawable object.
        /// </summary>
        public void Render(
			Drawable dest,
			GC gc,
			DrawableState state)
        {
            // Check for single frame image
            if (tile.Count == 1)
            {
                drawable.Render(dest, gc, state);
                return;
            }

            // Render ourselves
            Pixbuf pixbuf = GetPixbuf(state);

            dest.DrawPixbuf(
                gc,
                pixbuf,
                0,
                0,
                state.X,
                state.Y,
                pixbuf.Width,
                pixbuf.Height,
                RgbDither.None,
                0,
                0);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Updates the drawable. If the drawable changed in any
        /// visible manner, then this returns true.
        /// </summary>
        public bool Update(DrawableState state)
        {
            // Don't bother if we only have one frame
            if (tile.Count == 1)
            {
                return false;
            }

            // Figure out the different in time
            TileFrame frame = tile.Frames[state.Frame];
            long now = DateTime.UtcNow.Ticks / 10000;
            long diff = now - state.LastUpdate;

            // If not enough time has passed, just move on
            if (diff < frame.Delay)
            {
                return false;
            }

            // We need to move to the next one
            long fdiff = diff - frame.Delay;

            if (fdiff > frame.Delay)
            {
                fdiff %= frame.Delay;
            }

            state.Frame = frame.NextFrame;
            state.LastUpdate = now + fdiff;
            return true;
        }
Exemplo n.º 15
0
        /// <summary>
        /// Create/retrieves a pixbuf based on the given state.
        /// </summary>
        public Pixbuf GetPixbuf(DrawableState state)
        {
            // Check for optimization (one frame)
            if (tile.Count == 1)
            {
                return drawable.GetPixbuf(state);
            }

            // Figure out our key for the cache
            int frame = state.Frame;
            string cacheKey = GetCacheKey(state);

            // Check the cache
            if (PixbufCache.Contains(cacheKey))
            {
                return PixbufCache[cacheKey];
            }

            // We have to create the pixbuf

            // Adjust our own drawable state
            drawableState.Width = state.Width * tile.Columns;
            drawableState.Height = state.Height * tile.Rows;
            drawableState.X = state.X;
            drawableState.Y = state.Y;

            // Figure out the frame and coordinates
            int row = frame / tile.Columns;
            int col = frame % tile.Columns;
            int dx = col * state.Width;
            int dy = row * state.Height;

            // Get the master image from our backing
            Pixbuf image = drawable.GetPixbuf(drawableState);

            if (image == null)
            {
                throw new SpriteException("Cannot create tile drawable: " + tile.ID);
            }

            // Create a new pixbuf
            Colorspace colorspace = image.Colorspace;
            bool hasAlpha = image.HasAlpha;
            int bitsPerSample = image.BitsPerSample;
            Pixbuf p = new Pixbuf(
                colorspace, hasAlpha, bitsPerSample, state.Width, state.Height);

            // Scale copy it in. We have to flood fill to get rid of
            // random noise.
            p.Fill(0x00000000);
            image.Composite(
                p,
                0,
                0,
                state.Width,
                state.Height,
                -dx,
                -dy,
                1.0,
                1.0,
                InterpType.Bilinear,
                255);

            // Cache it and return the sliced results
            PixbufCache[cacheKey] = p;
            return p;
        }
Exemplo n.º 16
0
        /// <summary>
        /// Randomizes the drawable state based on this specific
        /// drawable.
        /// </summary>
        public void Randomize(DrawableState state)
        {
            // Pick a random frame. We loop this since a frame may not
            // be randomizeable (the Random property is false).
            while (true)
            {
                // Pick one
                int seq = random.Next(0, tile.Count - 1);
                TileFrame frame = tile.Frames[seq];

                if (frame.Random)
                {
                    // We can use this as a random. We also randomize
                    // the last update to have not everything updating
                    // at the same time.
                    state.Frame = seq;
                    state.LastUpdate = DateTime.UtcNow.Ticks / 10000 +
                                       random.Next(0, frame.Delay);
                    return;
                }
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// Create/retrieves a pixbuf based on the given state.
        /// </summary>
        public Pixbuf GetPixbuf(DrawableState state)
        {
            // Check for optimization (one frame)
            if (tile.Count == 1)
            {
                return(drawable.GetPixbuf(state));
            }

            // Figure out our key for the cache
            int    frame    = state.Frame;
            string cacheKey = GetCacheKey(state);

            // Check the cache
            if (PixbufCache.Contains(cacheKey))
            {
                return(PixbufCache[cacheKey]);
            }

            // We have to create the pixbuf

            // Adjust our own drawable state
            drawableState.Width  = state.Width * tile.Columns;
            drawableState.Height = state.Height * tile.Rows;
            drawableState.X      = state.X;
            drawableState.Y      = state.Y;

            // Figure out the frame and coordinates
            int row = frame / tile.Columns;
            int col = frame % tile.Columns;
            int dx  = col * state.Width;
            int dy  = row * state.Height;

            // Get the master image from our backing
            Pixbuf image = drawable.GetPixbuf(drawableState);

            if (image == null)
            {
                throw new SpriteException("Cannot create tile drawable: " + tile.ID);
            }

            // Create a new pixbuf
            Colorspace colorspace    = image.Colorspace;
            bool       hasAlpha      = image.HasAlpha;
            int        bitsPerSample = image.BitsPerSample;
            Pixbuf     p             = new Pixbuf(
                colorspace, hasAlpha, bitsPerSample, state.Width, state.Height);

            // Scale copy it in. We have to flood fill to get rid of
            // random noise.
            p.Fill(0x00000000);
            image.Composite(
                p,
                0,
                0,
                state.Width,
                state.Height,
                -dx,
                -dy,
                1.0,
                1.0,
                InterpType.Bilinear,
                255);

            // Cache it and return the sliced results
            PixbufCache[cacheKey] = p;
            return(p);
        }
Exemplo n.º 18
0
 /// <summary>
 /// Every drawable contains a CacheKey which is a unique
 /// string to identify the drawable, it's size, and its
 /// alterations (such as color mapping).
 /// </summary>
 public string GetCacheKey(DrawableState state)
 {
     return String.Format("{0}:f{1}", drawable.GetCacheKey(state), state.Frame);
 }
Exemplo n.º 19
0
        /// <summary>
        /// An internal function to actually create the pixbuf. This
        /// method also adds it to the cache.
        /// </summary>
        protected virtual Pixbuf CreatePixbuf(
			DrawableState state,
			string cacheKey)
        {
            // Create the image from the disk. If we are exactly the
            // right size, return it immediately.
            Pixbuf image = new Pixbuf(file.FullName);

            if (image.Width == state.Width && image.Height == state.Height)
            {
                return image;
            }

            // Create a pixbuf of the given size
            Colorspace colorspace = image.Colorspace;
            bool hasAlpha = image.HasAlpha;
            int bitsPerSample = image.BitsPerSample;
            Pixbuf p = new Pixbuf(
                colorspace, hasAlpha, bitsPerSample, state.Width, state.Height);

            // Scale copy it in
            image.Composite(
                p,
                0,
                0,
                state.Width,
                state.Height,
                0,
                0,
                image.Width / state.Width,
                image.Height / state.Height,
                InterpType.Bilinear,
                255);

            // Cache and return
            PixbufCache[cacheKey] = p;
            return p;
        }
Exemplo n.º 20
0
 /// <summary>
 /// Contains the cache key for this drawable.
 /// </summary>
 public virtual string GetCacheKey(DrawableState state)
 {
     return String.Format(
         "file://{0}:{1}x{2}", file.FullName, state.Width, state.Height);
 }
Exemplo n.º 21
0
 /// <summary>
 /// Updates the drawable. If the drawable changed in any
 /// visible manner, then this returns true.
 /// </summary>
 public bool Update(DrawableState state)
 {
     return false;
 }
Exemplo n.º 22
0
        /// <summary>
        /// This method is called when the drawable has to draw itself
        /// to a Gdk.Drawable area. This handles the scaling and does
        /// the actual image loading.
        /// </summary>
        public void Render(
			Drawable dest,
			GC gc,
			DrawableState state)
        {
            Pixbuf pixbuf = GetPixbuf(state);

            dest.DrawPixbuf(
                gc,
                pixbuf,
                0,
                0,
                state.X,
                state.Y,
                pixbuf.Width,
                pixbuf.Height,
                RgbDither.None,
                0,
                0);
        }
Exemplo n.º 23
0
 /// <summary>
 /// Contains the cache key for this drawable.
 /// </summary>
 public virtual string GetCacheKey(DrawableState state)
 {
     return(String.Format(
                "file://{0}:{1}x{2}", file.FullName, state.Width, state.Height));
 }
Exemplo n.º 24
0
 /// <summary>
 /// Randomizes the drawable state based on this specific
 /// drawable.
 /// </summary>
 public virtual void Randomize(DrawableState state)
 {
 }
Exemplo n.º 25
0
 /// <summary>
 /// Updates the drawable. If the drawable changed in any
 /// visible manner, then this returns true.
 /// </summary>
 public bool Update(DrawableState state)
 {
     return(false);
 }