private void ParseAndLoadChars(XmlDocument xml, float scale) { XmlNodeList charNodes = xml.GetElementsByTagName("char"); for (int i = 0; i < charNodes.Count; i++) { XmlAttributeCollection attributes = charNodes[i].Attributes; float x = Convert.ToSingle(attributes["x"].Value); float y = Convert.ToSingle(attributes["y"].Value); float width = Convert.ToSingle(attributes["width"].Value); float height = Convert.ToSingle(attributes["height"].Value); float frameX = 0; if (_fontTexture.Frame != null) { frameX = _fontTexture.Frame.X; } float frameY = 0; if (_fontTexture.Frame != null) { frameY = _fontTexture.Frame.Y; } Rectangle region = new Rectangle(x / scale + frameX, y / scale + frameY, width / scale, height / scale); SubTexture texture = new SubTexture(_fontTexture, region); int charId = Convert.ToInt32(attributes["id"].Value); float xOffset = Convert.ToSingle(attributes["xoffset"].Value); float yOffset = Convert.ToSingle(attributes["yoffset"].Value); float xAdvance = Convert.ToSingle(attributes["xadvance"].Value); BitmapChar bitmapChar = new BitmapChar(charId, texture, xOffset, yOffset, xAdvance); _chars.Add(charId, bitmapChar); } }
public bool TryGetGlyphImage(Font font, int codepoint, out SubTexture texture, out ResourceHandle<Material> material) { material = textMaterial; var glyphId = new GlyphId(font, codepoint); if (glyphSubTextures.TryGetValue(glyphId, out texture)) return true; var nativeFont = font.EngineFont.Resolve(); var imageHandle = nativeFont.CreateGlyphImage(codepoint, font.Size); if (imageHandle.Id == ResourceHandle<Resource>.Invalid) return false; if (!textureAtlas.AddImage(imageHandle)) { throw new Exception("Atlas full"); } if (!textureAtlas.GetImageSubTexture(imageHandle, out texture)) return false; glyphSubTextures.Add(glyphId, texture); return true; }
public TileMap LoadTileMap(string imageFileName, string settingsFileName) { Texture2D layer = ScreenManager.Instance.Content.Load<Texture2D>(imageFileName); Color[] allPixels = new Color[(layer.Width * layer.Height)]; layer.GetData<Color>(allPixels); Dictionary<Color, SubTexture> lookupTable = LoadSettingsFile(settingsFileName); TileMap tm = new TileMap(layer.Width, layer.Height, 20, 20, 1, 1); // For now just 1 layer and 1 sublayer. (1 total texture per tile) for (int i = 0; i < allPixels.Length; i++) { int x = i % layer.Width; int y = i / layer.Width; SubTexture[,] textures = new SubTexture[1,1]; textures[0, 0] = lookupTable[allPixels[i]]; Tile t; t = textures[0,0] != null ? new Tile(textures) : null; tm.SetTile(t, x, y); } return tm; }
private void ParseTxt() { try { string[] lines = Regex.Split(xmlAsset.text, "\r\n|\r\n"); if (lines == null || lines.Length <= 0) { subTextures = null; return; } List <SubTexture> parsedSubTextures = new List <SubTexture>(); foreach (string item in lines) { if (string.IsNullOrEmpty(item)) { continue; } string[] line = item.Split(' '); if (line != null || line.Length == 6) { bool pass = true; foreach (string data in line) { if (string.IsNullOrEmpty(data)) { pass = false; break; } } if (!pass) { continue; } SubTexture subtexture = new SubTexture { name = line[0], x = Convert.ToInt32(line[2]), y = Convert.ToInt32(line[3]), width = Convert.ToInt32(line[4]), height = Convert.ToInt32(line[5]) }; parsedSubTextures.Add(subtexture); } } if (parsedSubTextures.Count > 0) { subTextures = parsedSubTextures.ToArray(); SetWantedDimensions(); } } catch (Exception) { subTextures = null; } }
public Tile(SubTexture[,] textures, bool[] edges) { this.initialEdges = new bool[4]; edges.CopyTo(initialEdges, 0); this.edges = new bool[4]; this.initialEdges.CopyTo(this.edges, 0); this.textures = textures; }
public Tile(SubTexture[,] textures) { this.initialEdges = new bool[4]; this.initialEdges[0] = this.initialEdges[1] = this.initialEdges[2] = this.initialEdges[3] = true; this.edges = new bool[4]; this.initialEdges.CopyTo(this.edges, 0); this.textures = textures; }
public bool TryGetGlyph(Font font, char c, out SubTexture subTexture) { var glyphHash = GlyphHash(font, c); if (glyphs.TryGetValue(glyphHash, out subTexture)) return true; subTexture = default(SubTexture); return false; }
/// <summary> /// Play the next frame in this animation slide. /// </summary> public void PlayFrame() { if (Index > Count - 1) { Index = 0; } CurrentTexture = Animations[Index]; Index++; }
private void ParseXML() { try { var document = new XmlDocument(); document.LoadXml(plistContent); XmlNodeList frames = new PlistFinder(document.DocumentElement.ChildNodes[0]).FindValueByKey("frames").ChildNodes; ArrayList subTexs = new ArrayList(); for (int i = 0; i < frames.Count; i++) { if (frames[i].Name.ToLower() == "key") { SubTexture subTex = new SubTexture(); subTex.name = frames[i].InnerText; PlistFinder finder = new PlistFinder(frames[++i]); XmlNode rotatedNode = finder.FindValueByKey("textureRotated"); rotatedNode = rotatedNode ?? finder.FindValueByKey("rotated"); bool isRotated = (rotatedNode.Name.ToLower() == "true"); XmlNode rectNode = finder.FindValueByKey("textureRect"); rectNode = rectNode ?? finder.FindValueByKey("frame"); string rect = rectNode.InnerText; var ints = rect.Replace('{', ' ').Replace('}', ' ').Split(new char[] { ',' }) .Select(num => Int32.Parse(num.Trim())).ToArray(); subTex.width = isRotated ? ints[3] : ints[2]; subTex.height = isRotated ? ints[2] : ints[3]; subTex.x = ints[0]; subTex.y = ints[1]; subTexs.Add(subTex); } } subTextures = subTexs.Cast <SubTexture>().ToArray(); wantedWidth = 0; wantedHeight = 0; foreach (var subTexture in subTextures) { var right = subTexture.x + subTexture.width; var bottom = subTexture.y + subTexture.height; wantedWidth = Mathf.Max(wantedWidth, right); wantedHeight = Mathf.Max(wantedHeight, bottom); } } catch (Exception) { subTextures = null; } }
public bool TryGetGlyph(Font font, char c, out SubTexture subTexture) { var glyphHash = GlyphHash(font, c); if (glyphs.TryGetValue(glyphHash, out subTexture)) { return(true); } subTexture = default(SubTexture); return(false); }
/// <summary> /// Initializes the GuiScrollList with a vertain number of text images. /// </summary> /// <param name="items">The text items to display.</param> /// <param name="width">The width of one text item.</param> /// <param name="height">The height of one text item.</param> /// <param name="font">The font to use for the text.</param> /// <param name="textColor">The color of the text.</param> /// <param name="outlineColor">The color of the outline or 0.</param> public void InitImages(string[] items, int width, int height, System.Drawing.Font font, int textColor, int outlineColor) { ITexture2d texture = Text.Create(items, width, height, font, textColor, outlineColor); SubTexture[] textures = SubTexture.Create(texture, items.Length, 1); images = new Image[textures.Length]; for (int i = 0; i < textures.Length; i++) { images[i] = new Image(textures[i]); } Init(images); SetSize(images[0].Size); }
public Window(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings) : base(gameWindowSettings, nativeWindowSettings) { Load_resources(); m_entities = new List <Entity>(); ScreenSize = new vec2(1024.0f, 576.0f); var texture = m_textures.GetResource("test"); m_subTexs.AddResouce(0, SubTexture.CreateFromCoords(texture.textureID, new vec2(texture.Width, texture.Height), new vec2(1, 0), new vec2(64, 64))); props = ParticleProps.Effect2; props.Gravity = 0.0f; emiter = new ParticlesSystem(); }
public Fragment(World world, SpriteBatch spriteBatch, Vector2 position, SubTexture texture, Rectangle rectangle) : base(world, spriteBatch, position) { this.texture = texture; // Mass is proportional to the percentage of the texture in area. this.mass = ((subRectangle.Width * subRectangle.Height) / (texture.Rectangle.Width * texture.Rectangle.Height)) * 0.025f; this.subRectangle = new Rectangle(texture.Rectangle.X + rectangle.X, texture.Rectangle.Y + rectangle.Y, rectangle.Width, rectangle.Height); hotspots = new List<CollisionHotspot>(); hotspots.Add(new CollisionHotspot(this, new Vector2(0, subRectangle.Height / 2), HOTSPOT_TYPE.left)); hotspots.Add(new CollisionHotspot(this, new Vector2(subRectangle.Width, subRectangle.Height / 2), HOTSPOT_TYPE.right)); hotspots.Add(new CollisionHotspot(this, new Vector2(subRectangle.Width/2, 0), HOTSPOT_TYPE.top)); hotspots.Add(new CollisionHotspot(this, new Vector2(subRectangle.Width / 2, subRectangle.Height), HOTSPOT_TYPE.bottom)); }
//--------------------------------------------------------------- #endregion //--------------------------------------------------------------- //--------------------------------------------------------------- #region Initialisation //--------------------------------------------------------------- /// <summary> /// Creates a new GuiScrollList element. /// </summary> public GuiScrollList(int totalNum, int columns, ITexture2d normal, ITexture2d hover, ITexture2d selected) { isInfinite = false; SubTexture[] subTexturesNormal = SubTexture.Create(normal, totalNum, columns); SubTexture[] subTexturesHover = SubTexture.Create(hover, totalNum, columns); SubTexture[] subTexturesSelected = SubTexture.Create(selected, totalNum, columns); Button[] buttons = new Button[subTexturesNormal.Length]; for (int i = 0; i < buttons.Length; i++) { buttons[i] = new Button(subTexturesNormal[i], subTexturesSelected[i], subTexturesHover[i]); } Init(images); }
//--------------------------------------------------------------- #endregion //--------------------------------------------------------------- //--------------------------------------------------------------- #region Initialisation //--------------------------------------------------------------- /// <summary> /// Creates a new GuiListBox object. /// </summary> /// <param name="items">The number of items.</param> /// <param name="background">The background texture.</param> /// <param name="hover">The hover texture.</param> /// <param name="selected">The selected texture.</param> public GuiListBox(int items, ITexture2d background, ITexture2d hover, ITexture2d selected) { this.background = background; this.hover = hover; this.selected = selected; this.images = new Image[items]; states = new ButtonState[items]; for (int i = 0; i < images.Length; i++) { SubTexture subTexture = SubTexture.Create(background, i, items, 1); images[i] = new Image(subTexture); images[i].Position = Vector2.MultiplyElements(images[i].Size, new Vector2(0.0f, i)); this.Children.Add(images[i]); } this.size = Vector2.MultiplyElements(images[0].Size, new Vector2(1, items)); }
public Texture GetTexture(float resolution = 1) { Texture texture; if (_numPasses >= 0) { if (_numPasses-- == 0) { return(null); } } if (_pool.Count > 0) { texture = _pool.Pop(); } else { texture = Texture.Empty(_nativeWidth / _scale, _nativeHeight / _scale, true, 0, true, _scale, TextureFormat); } if (!MathUtil.Equals(texture.Width, _width, 0.1f) || !MathUtil.Equals(texture.Height, _height, 0.1f) || !MathUtil.Equals(texture.Scale, _scale * resolution)) { Rectangle sRegion = Rectangle.Create(0, 0, _width * resolution, _height * resolution); var subTexture = texture as SubTexture; if (subTexture != null) { subTexture.SetTo(texture.Root, sRegion, true, null, false, resolution); } else { texture = new SubTexture(texture.Root, sRegion, true, null, false, resolution); } } texture.Root.Clear(); return(texture); }
private void SetImage(SubTexture subTexture, int levelIndex, int mipMapIndex) { if (levelIndex > 0) { levelIndex = sCubeMapIndices[levelIndex]; } var target = Target == TextureTarget.TextureCubeMap ? TextureTarget.TextureCubeMapPositiveX + levelIndex : TextureTarget.Texture2D; if (TextureFormatUtilities.IsBlockCompressed(subTexture.Format)) { GL.CompressedTexImage2D( target, mipMapIndex, sInternalFormatMap[subTexture.Format], subTexture.Width, subTexture.Height, 0, subTexture.Data.Length, subTexture.Data); } else { GL.TexImage2D( target, mipMapIndex, ( PixelInternalFormat )sInternalFormatMap[subTexture.Format], subTexture.Width, subTexture.Height, 0, sPixelFormatMap[subTexture.Format], sPixelTypeMap[subTexture.Format], subTexture.Data); } }
protected void prepare() { Console.WriteLine("Preparing files from atlas " + file); ImageList = new List <SubTexture>(); // Add tank icons with generated icon width and hieght foreach (TankStats tankData in stats) { SubTexture SubTexture = new SubTexture(); SubTexture.name = Path.GetFileNameWithoutExtension(tankData.FileName); SubTexture.dimension = new Rectangle(0, 0, Icon.WIDTH, Icon.HEIGHT); SubTexture.maxDimension = Math.Max(Icon.WIDTH, Icon.HEIGHT); ImageList.Add(SubTexture); } //CreateImageList(); String atlasXmlFile = Path.Combine(Properties.Settings.getResourcesLocation(), @"atlases", file + ".xml"); String atlasPngFile = Path.Combine(Properties.Settings.getResourcesLocation(), @"atlases", file + ".png"); tmpDir = Path.Combine(Properties.Settings.getOutputLocation(), "atlases", file); Directory.CreateDirectory(tmpDir); XmlDocument atlasXml = new XmlDocument(); atlasXml.Load(atlasXmlFile); Image altasImage = Image.FromFile(atlasPngFile); foreach (XmlNode node in atlasXml.DocumentElement.ChildNodes) { // read data from XML String iconName = node.SelectSingleNode("name").InnerText.Trim(); int iconX = Convert.ToInt32(node.SelectSingleNode("x").InnerText); int iconY = Convert.ToInt32(node.SelectSingleNode("y").InnerText); int iconWidth = Convert.ToInt32(node.SelectSingleNode("width").InnerText); int iconHeight = Convert.ToInt32(node.SelectSingleNode("height").InnerText); // extract image from sprite and save to tmp dir Rectangle cropArea = new Rectangle(iconX, iconY, iconWidth, iconHeight); using (Image cropedImage = Utils.ImageTools.cropImage(altasImage, cropArea)) { String iconFile = Path.Combine(tmpDir, iconName + ".png"); cropedImage.Save(iconFile); } // add subTexture into list SubTexture subTexture = new SubTexture( iconName, new Rectangle(0, 0, iconWidth, iconHeight), Math.Max(iconWidth, iconHeight) ); if (!ImageList.Contains(subTexture)) { ImageList.Add(subTexture); } } altasImage.Dispose(); }
static void SetPivotAlignment(XmlNode node, SubTexture st) { if (node.Attributes["pivotX"] == null) { st.pivot = lastPivot; } else { st.pivot = new Vector2((Convert.ToSingle(node.Attributes["pivotX"].Value) + st.frameX) / st.width, (Convert.ToSingle(node.Attributes["pivotY"].Value) + st.frameY) / st.height); lastPivot = st.pivot; } if (st.pivot.y == 0) { if (st.pivot.x == 0) { st.alignment = SpriteAlignment.TopLeft; } else if (st.pivot.x == 0.5f) { st.alignment = SpriteAlignment.TopCenter; } else if (st.pivot.x == 1) { st.alignment = SpriteAlignment.TopRight; } } else if (st.pivot.y == 0.5f) { if (st.pivot.x == 0) { st.alignment = SpriteAlignment.LeftCenter; } else if (st.pivot.x == 0.5f) { st.alignment = SpriteAlignment.Center; } else if (st.pivot.x == 1) { st.alignment = SpriteAlignment.RightCenter; } } else if (st.pivot.y == 1) { if (st.pivot.x == 0) { st.alignment = SpriteAlignment.BottomLeft; } else if (st.pivot.x == 0.5f) { st.alignment = SpriteAlignment.BottomCenter; } else if (st.pivot.x == 1) { st.alignment = SpriteAlignment.BottomRight; } } else { st.alignment = SpriteAlignment.Custom; } }
//SpriteEditorUtility public static Vector2 GetPivotValue(SpriteAlignment alignment, Vector2 customOffset, SubTexture subTexture) { if (subTexture.hasCustomPivot) { currentPivotX = subTexture.pivotX / subTexture.width; currentPivotY = 1 - subTexture.pivotY / subTexture.height; } Vector2 rep = new Vector2(currentPivotX, currentPivotY); return(rep); /* * switch (alignment) { * case SpriteAlignment.Center: * return new Vector2(0.5f, 0.5f); * case SpriteAlignment.TopLeft: * return new Vector2(0.0f, 1f); * case SpriteAlignment.TopCenter: * return new Vector2(0.5f, 1f); * case SpriteAlignment.TopRight: * return new Vector2(1f, 1f); * case SpriteAlignment.LeftCenter: * return new Vector2(0.0f, 0.5f); * case SpriteAlignment.RightCenter: * return new Vector2(1f, 0.5f); * case SpriteAlignment.BottomLeft: * return new Vector2(0.0f, 0.0f); * case SpriteAlignment.BottomCenter: * return new Vector2(0.5f, 0.0f); * case SpriteAlignment.BottomRight: * return new Vector2(1f, 0.0f); * case SpriteAlignment.Custom: * return customOffset; * default: * return Vector2.zero; * }*/ }
private void ParseXML() { try { var document = new XmlDocument(); document.LoadXml(xmlAsset.text); var root = document.DocumentElement; if (root == null || root.Name != "TextureAtlas") { return; } /*subTextures = root.ChildNodes * .Cast<XmlNode>() * .Where(childNode => childNode.Name == "SubTexture") * .Select(childNode => new SubTexture { * pivotX = valueOrZero("pivotX", childNode), * pivotY = valueOrZero("pivotY", childNode), * width = Convert.ToInt32(childNode.Attributes["width"].Value), * height = Convert.ToInt32(childNode.Attributes["height"].Value), * x = Convert.ToInt32(childNode.Attributes["x"].Value), * y = Convert.ToInt32(childNode.Attributes["y"].Value), * name = childNode.Attributes["name"].Value * * }).ToArray();*/ subTextures = new List <SubTexture>(); foreach (XmlNode childNode in root.ChildNodes) { if (childNode.Name != "SubTexture") { continue; } float pivX = 0.5f; float pivY = 0.5f; bool hasXPivot = false; bool hasYPivot = false; hasXPivot = valueOrMid("pivotX", childNode, out pivX); hasYPivot = valueOrMid("pivotY", childNode, out pivY); bool hasPivot = hasXPivot && hasYPivot; SubTexture sub = new SubTexture { pivotX = pivX, pivotY = pivY, hasCustomPivot = hasPivot, width = Convert.ToInt32(childNode.Attributes["width"].Value), height = Convert.ToInt32(childNode.Attributes["height"].Value), x = Convert.ToInt32(childNode.Attributes["x"].Value), y = Convert.ToInt32(childNode.Attributes["y"].Value), name = childNode.Attributes["name"].Value }; subTextures.Add(sub); } wantedWidth = 0; wantedHeight = 0; foreach (var subTexture in subTextures) { var right = subTexture.x + subTexture.width; var bottom = subTexture.y + subTexture.height; wantedWidth = Mathf.Max(wantedWidth, right); wantedHeight = Mathf.Max(wantedHeight, bottom); } } catch (Exception e) { Debug.LogError(e.ToString()); subTextures = null; } }
private Tile TileFromXElement(XElement xelement, SubTextureSheet[] sheets, int x, int y, World world) { if (xelement.Attribute("e").Value == "-1") { return null; } Tile returnVal; SubTexture[,] subTexturesArray = new SubTexture[layerCount, subLayerCount]; IEnumerable<XElement> subTextureElements = xelement.Descendants("x"); int i = 0, j = 0; //int index = 0; // Was needed when changing format. foreach( XElement subTextureElement in subTextureElements) { i = Int32.Parse(subTextureElement.Attribute("z").Value) / SubLayerCount; j = Int32.Parse(subTextureElement.Attribute("z").Value) % SubLayerCount; SubTexture subTex = new SubTexture(sheets[Int32.Parse(subTextureElement.Attribute("s").Value)], Int32.Parse(subTextureElement.Attribute("i").Value)); subTexturesArray[i, j] = subTex; } int edgeInt; edgeInt = Int32.Parse(xelement.Attribute("e").Value); bool[] edges = new bool[4]; for (int k = 0; k < 4; ++k) { edges[k] = (edgeInt & (int)Math.Pow(2, k)) != 0; } returnVal = new Tile(subTexturesArray, edges); // Check for special tile types: bool isDestructable = (from att in xelement.Attributes() where att.Name == "d" select att).Count() > 0; if (isDestructable) { List<IDestructionEffect> effects = new List<IDestructionEffect>(); effects.Add(new ExplosionEffect()); effects.Add(new ShatterEffect()); returnVal = new DestructableTile(returnVal, world, x, y, float.Parse(xelement.Attribute("d").Value), effects); } return returnVal; }
public static void DecodeToFile(SubTexture subTexture, string filePath) => Native.TextureDecoder.DecodeToFile(subTexture, filePath);
/// <summary> /// Add a new sub-texture to this animation slide. /// </summary> /// <param name="texture">Sub-texture to add to collection.</param> public void AddTextureToSlide(SubTexture texture) { Animations.Add(texture); CurrentTexture = texture; }
public void AddGlyph(Font font, char c, SubTexture subTexture) { var glyphHash = GlyphHash(font, c); glyphs.Add(glyphHash, subTexture); }
public GuiImage(SubTexture texture) : base(texture.Width, texture.Height) { this.texture = texture; }
public Unit SetMapSprite(SubTexture mapTexture) { this.mapTexture = mapTexture; return(this); }
public Unit SetPortraitSprite(SubTexture portraitTexture) { this.portraitTexture = portraitTexture; return(this); }
public void AddGlyph(Font font, char c, SubTexture subTexture) { var glyphHash = GlyphHash(font, c); glyphs.Add(glyphHash,subTexture); }
public static Bitmap DecodeToBitmap(SubTexture subTexture) => Native.TextureDecoder.DecodeToBitmap(subTexture);
/// <summary> /// Set Textures[layerIndex, subLayerIndex] of all selected tiles to the SubTexture passed in. /// </summary> /// <param name="subTexture">The SubTexture to set the tiles to (null will clear the texture).</param> /// <param name="layerIndex">The first index into each tile's Textures array; the layer index.</param> /// <param name="subLayerIndex">The second index into each tile's Textures array; the sub-layer index.</param> public void SetSubTexture(SubTexture subTexture, int layerIndex, int subLayerIndex) { foreach (int[] tileCoord in selectedTileCoordinates) { Tile tile = TileMap[tileCoord[0], tileCoord[1]]; if (tile == null) { // We have an empty tile... if (subTexture != null) { // We need to make a new one to hold this subTexture SubTexture[,] textures = new SubTexture[TileMap.LayerCount, TileMap.SubLayerCount]; textures[layerIndex, subLayerIndex] = subTexture; tile = new Tile(textures); // TODO: set the edges to what the edge toggler says the tile's edges should be? } // Otherwise we do nothing. } else { // We have a non-empty tile, so we simply set it's subTexture directly: tile.Textures[layerIndex, subLayerIndex] = subTexture; } // Finally, we set our tile so the tileMap's edges are properly updated. TileMap.SetTile(tile, tileCoord[0], tileCoord[1]); } notifyTilePropertyComponents(); }