/// <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); }
/// <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); }
/// <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; }
/// <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); }
/// <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)); } }
/// <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); } }
/// <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); }
/// <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; } } }
/// <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); }
/// <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(); }
/// <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)); }
/// <summary> /// Randomizes the drawable state based on this specific /// drawable. /// </summary> public virtual void Randomize(DrawableState state) { }
/// <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; }
/// <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; }
/// <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); }
/// <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); }
/// <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; }
/// <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); }
/// <summary> /// Updates the drawable. If the drawable changed in any /// visible manner, then this returns true. /// </summary> public bool Update(DrawableState state) { return false; }
/// <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)); }
/// <summary> /// Updates the drawable. If the drawable changed in any /// visible manner, then this returns true. /// </summary> public bool Update(DrawableState state) { return(false); }