/// <summary> /// Sets the currently active skin, and flags a redraw. /// </summary> /// <remarks> /// Does nothing if no skin has been set for the specified index, or /// clears current skin if -1. /// </remarks> /// <param name="index">Skin index.</param> private void SetActiveSkin(int index) { // Minimize redraws if (index != this.currentSkin) { ComponentSkin skin = null; if (index != -1) { skin = GetSkin(index); } if (skin != null || index == -1) { this.currentSkin = index; Redraw(); } } }
/// <summary> /// Draws all skins associated with control, after clipping them to the /// parent control area. /// </summary> /// <param name="spriteBatch">SpriteBatch to draw with.</param> /// <param name="parentScissor">The scissor region of the parent control.</param> protected override void DrawControl(SpriteBatch spriteBatch, Rectangle parentScissor) { ComponentSkin skin = GetSkin(this.currentSkin); if (skin != null) { Texture2D texture; // Should the default GUI skin be used? if (skin.UseCustomSkin) { texture = skin.Skin; } else { texture = GUIManager.SkinTexture; } bool draw; Rectangle source; Rectangle destination; int dif; foreach (GUIRect rect in skin.Rects) { draw = true; source = rect.Source; // Convert to absolute position destination = rect.Destination; destination.X += AbsolutePosition.X; destination.Y += AbsolutePosition.Y; if (!parentScissor.Contains(destination)) { // Perform culling if (parentScissor.Intersects(destination)) { // Perform clipping if (destination.X < parentScissor.X) { dif = parentScissor.X - destination.X; if (destination.Width == source.Width) { source.Width -= dif; source.X += dif; destination.Width -= dif; destination.X += dif; } else { destination.Width -= dif; destination.X += dif; } } else if (destination.Right > parentScissor.Right) { dif = destination.Right - parentScissor.Right; if (destination.Width == source.Width) { source.Width -= dif; destination.Width -= dif; } else { destination.Width -= dif; } } if (destination.Y < parentScissor.Y) { dif = parentScissor.Y - destination.Y; if (destination.Height == source.Height) { source.Height -= dif; source.Y += dif; destination.Height -= dif; destination.Y += dif; } else { destination.Height -= dif; destination.Y += dif; } } else if (destination.Bottom > parentScissor.Bottom) { dif = destination.Bottom - parentScissor.Bottom; if (destination.Height == source.Height) { source.Height -= dif; destination.Height -= dif; } else { destination.Height -= dif; } } } else { draw = false; } } if (draw) { // Actually draw finally! spriteBatch.Draw( texture, destination, source, Color.White ); } } } }