public void ClearColor(ColorU color) { Bitmap spriteSheetTexBitmap = Texture.Surface.ToBitmap(); Color clearedColor = ImageHelper.ClearColor(ref spriteSheetTexBitmap, color.ToColor()); TransparentColors.Add(new ColorU(clearedColor)); ResetTextureFromBitmap(spriteSheetTexBitmap); spriteSheetTexBitmap.Dispose(); }
public Button(GuiManager manager, string text) : base(manager) { Text = text; TextColor = ColorU.White; Frames = new Dictionary <ControlState, Sprite>(); FrameBorder = new Vector2(4, 4); Font = ResourcesManager.Instance.GetFont("segoe_ui_15"); SetFrames(ResourcesManager.Instance.ResourceImageSheet.Sprites["gui_button_normal"], ResourcesManager.Instance.ResourceImageSheet.Sprites["gui_button_normal"], ResourcesManager.Instance.ResourceImageSheet.Sprites["gui_button_pressed"]); SetOverlay(ResourcesManager.Instance.ResourceImageSheet.Sprites["gui_button_overlay"]); }
private void ConfigurationWindowLoad(object sender, EventArgs e) { _lastCameraSpeedValue = Configuration.CameraSpeed; _lastFilterModeSelectedIndex = Configuration.TextureFilterMode.Equals(TextureFilter.Point) ? 0 : 1; _lastFrameRectColor = Configuration.FrameRectColor; _lastFrameRectHoveredColor = Configuration.HoverFrameRectColor; _lastFrameRectSelectedColor = Configuration.SelectedFrameRectColor; _lastBgColor = Configuration.BackgroundColor; txtCamSpeed.Text = _lastCameraSpeedValue.ToString(); cmbTextureFilterMode.SelectedIndex = _lastFilterModeSelectedIndex; ColorPickerBg.SelectedColor = Configuration.BackgroundColor.ToColor(); radAlphaMode.Checked = Configuration.OverwriteImageWhenTransparencyModified; radColorKeyMode.Checked = !radAlphaMode.Checked; chkPackSpriteSheet.Checked = Configuration.PackSpriteSheetWhenExportingSpriteMap; chkForcePowTwo.Checked = Configuration.ForcePowTwo; chkForceSquare.Checked = Configuration.ForceSquare; udPadding.Value = Configuration.Padding; ColorPickerFrameRect.SelectedColor = Configuration.FrameRectColor.ToColor(); ColorPickerFrameRectHovered.SelectedColor = Configuration.HoverFrameRectColor.ToColor(); ColorPickerFrameRectSelected.SelectedColor = Configuration.SelectedFrameRectColor.ToColor(); InputConfigurationHelper.LoadCurrentControlConfigIntoInputControl(Configuration.DragCameraControl, InputDetectorMoveCamera); InputConfigurationHelper.LoadCurrentControlConfigIntoInputControl(Configuration.SpriteMarkUpControl, InputDetectorMarkupSprite); InputConfigurationHelper.LoadCurrentControlConfigIntoInputControl(Configuration.SelectSpriteControl, InputDetectorSelectSprite); InputConfigurationHelper.LoadCurrentControlConfigIntoInputControl(Configuration.ViewZoomControl, InputDetectorViewZoom); txtCamSpeed.Focus(); }
public static Texture TrimmByAlpha(Texture tex) { var optimizedRect = ShrinkFrameSelect(tex, tex.Region, 0.12).ToRectangle(); var optimizedSpTexData = new ColorU[optimizedRect.Width * optimizedRect.Height]; tex.Surface.GetData(optimizedRect, optimizedSpTexData, PixelFormat.DefaultAlpha); var finalTex = new Texture(optimizedRect.Width, optimizedRect.Height, PixelFormat.DefaultAlpha); finalTex.Surface.SetData(optimizedSpTexData, PixelFormat.DefaultAlpha); tex.Dispose(); return(finalTex); }
public void ClearColor(int x, int y) { Bitmap spriteSheetTexBitmap = Texture.Surface.ToBitmap(); Color clearedColor = ImageHelper.ClearColor(ref spriteSheetTexBitmap, x, y); ColorU clearedColorConverted = new ColorU(clearedColor); if (!TransparentColors.Contains(clearedColorConverted)) { TransparentColors.Add(clearedColorConverted); } ResetTextureFromBitmap(spriteSheetTexBitmap); spriteSheetTexBitmap.Dispose(); }
private void DrawCells() { for (int i = 0; i < FIELD_WIDTH; i++) for (int c = 0; c < FIELD_HEIGHT; c++) { cell_color = RegularLife ? ColorU.Blue : GetCellColor(cells[i, c]); canvas.DrawFilledRect(i * CELL_SIZE + 1, c * CELL_SIZE + 1, CELL_SIZE - 1, CELL_SIZE - 1, cells[i, c].IsAlive ? cell_color : bgColor); } }
public void ImportSpriteMapXML() { string path = ShowOpenFileDialog(@"XML File(.xml)|*.xml", "Import SpriteMap XML..."); if (path.Length > 0) { string xmlDirectory = Path.GetDirectoryName(path); XmlDocument animDoc = new XmlDocument(); Dictionary <int, SpriteSheetFrame> SheetFrames = new Dictionary <int, SpriteSheetFrame>(); animDoc.Load(path); string textureRelativePath = animDoc.SelectSingleNode("//Texture/@Path").Value; // You have to decode the uri, because the Bitmap class can't understand // the code "%20" as a space and don't recognize the path textureRelativePath = Uri.UnescapeDataString(textureRelativePath); string textureFullPath = Path.Combine(xmlDirectory, textureRelativePath); LoadSpriteSheetDirectly(textureFullPath, false); var colorsToClear = new List <ColorU>(); var transparentColorsNodes = animDoc.SelectNodes("//TransparentColors/Color"); foreach (XmlNode colorNode in transparentColorsNodes) { string colorString = colorNode.Attributes["Value"].Value; ColorU color = Parser.ParseColorU(colorString); colorsToClear.Add(color); CurrentShownSpriteSheet.TransparentColors.Add(color); } foreach (var color in colorsToClear) { CurrentShownSpriteSheet.ClearColor(color); } colorsToClear.Clear(); var spriteNodes = animDoc.SelectNodes("//Sprites/Sprite"); foreach (XmlNode spriteNode in spriteNodes) { var name = spriteNode.Attributes["Name"].Value; var id = int.Parse(spriteNode.Attributes["Id"].Value); var x = int.Parse(spriteNode.SelectSingleNode("Coordinates/X").InnerText); var y = int.Parse(spriteNode.SelectSingleNode("Coordinates/Y").InnerText); var w = int.Parse(spriteNode.SelectSingleNode("Coordinates/Width").InnerText); var h = int.Parse(spriteNode.SelectSingleNode("Coordinates/Height").InnerText); var frameAdded = CurrentShownSpriteSheet.AddFrame(name, id, Rect.FromBox(x, y, w, h)); SheetFrames.Add(id, frameAdded); } var animationNodes = animDoc.SelectNodes("//Animations/Animation"); foreach (XmlNode animationNode in animationNodes) { var name = animationNode.Attributes["Name"].Value; var frameRate = float.Parse(animationNode.Attributes["FrameRate"].Value); var loop = bool.Parse(animationNode.Attributes["Loop"].Value); var pingPong = bool.Parse(animationNode.Attributes["PingPong"].Value); MakeAnimation(name, frameRate, loop, pingPong); var frames = new List <Frame>(); var frameNodes = animationNode.SelectNodes("Frames/Frame"); foreach (XmlNode frameNode in frameNodes) { var spriteId = int.Parse(frameNode.Attributes["SpriteId"].Value); var offSetX = int.Parse(frameNode.Attributes["OffSetX"].Value); var offSetY = int.Parse(frameNode.Attributes["OffSetY"].Value); var duration = float.Parse(frameNode.Attributes["Duration"].Value); var texture = CurrentShownSpriteSheet.Texture; var rect = SheetFrames[spriteId].BoundingRect; var frame = new Frame(new Sprite(texture, rect), spriteId); frame.OffSetX = offSetX; frame.OffSetY = offSetY; frame.FrameDuration = duration; frames.Add(frame); } AddFramesToAnimation(frames); } } }
/// <summary> /// Returns a list of sprite rectangles cut from an image by alpha channel. /// Modified from ClanLib SDK (http://clanlib.org) /// </summary> /// <param name="tex"></param> /// <param name="scanStartX"></param> /// <param name="scanStartY"></param> /// <param name="transparencyLimit"></param> /// <returns></returns> private static List <Rect> FindFrameRects(Texture tex, int scanStartX, int scanStartY, double transparencyLimit) { int width = (int)tex.Size.Width; int height = (int)tex.Size.Height; List <Rect> frameRects = new List <Rect>(); bool[] explored = new bool[width * height]; ColorU[] texData = new ColorU[width * height]; tex.Surface.GetData(texData, PixelFormat.DefaultAlpha); for (int y = scanStartY; y < height; y++) { for (int x = scanStartX; x < width; x++) { int texIndex = y * width + x; if (explored[texIndex]) { continue; } explored[y * width + x] = true; if ((texData[texIndex].A) <= transparencyLimit * 255) { continue; } int x2; int x1 = x2 = x; int y2; int y1 = y2 = y; bool more = true; while (more) { more = false; for (int i = x1; i <= x2; i++) { if (y2 + 1 < height) { explored[(y2 + 1) * width] = true; if ((texData[(y2 + 1) * width + i].A) > transparencyLimit * 255) { more = true; y2 += 1; } } } for (int j = y1; j <= y2; j++) { if (x2 + 1 < width) { explored[j * width + x2 + 1] = true; if ((texData[j * width + x2 + 1].A) > transparencyLimit * 255) { more = true; x2 += 1; } } if (x1 - 1 >= 0) { explored[j * width + x1 - 1] = true; if ((texData[j * width + x1 - 1].A) > transparencyLimit * 255) { more = true; x1 -= 1; } } } } for (int i = x1; i <= x2; i++) { for (int j = y1; j <= y2; j++) { explored[j * width + i] = true; } } frameRects.Add(new Rect(x1, y1, x2 + 1, y2 + 1)); } } return(frameRects); }
//------------------------------------------------------------------------------------------------------------------------------------- public static Rect ShrinkFrameSelect(Texture tex, Rect selectionRect, double alphaTolerance) { var selectionRectInt = selectionRect.ToRectangle(); ColorU[] selectionData = new ColorU[selectionRectInt.Width * selectionRectInt.Height]; tex.Surface.GetData(selectionRectInt, selectionData, 0, selectionRectInt.Width * selectionRectInt.Height, PixelFormat.DefaultAlpha); int x1 = 0, y1 = 0, x2 = 0, y2 = 0; int first = 0; bool found = false; for (int i = 0; i < selectionData.Length; i++) { if (selectionData[i].A >= alphaTolerance * 255) { found = true; x1 = i % selectionRectInt.Width; y1 = i / selectionRectInt.Width; x2 = x1; y2 = y1; first = i; break; } } if (found) { for (int i = first; i < selectionData.Length; i++) { if (selectionData[i].A >= alphaTolerance * 255) { int x = i % selectionRectInt.Width; int y = i / selectionRectInt.Width; if (x < x1) { x1 = x; } if (y < y1) { y1 = y; } if (x > x2) { x2 = x; } if (y > y2) { y2 = y; } } } x1 += selectionRectInt.X; y1 += selectionRectInt.Y; x2 += selectionRectInt.X + 1; y2 += selectionRectInt.Y + 1; return(new Rect(x1, y1, x2, y2)); } return(Rect.Empty); }