Example #1
0
        void GetNumTilesForSpriteSheet(tk2dSpriteSheetSource spriteSheet, out int numTilesX, out int numTilesY)
        {
            var tex = spriteSheet.texture;

            numTilesX = (tex.width - spriteSheet.tileMarginX + spriteSheet.tileSpacingX) / (spriteSheet.tileSpacingX + spriteSheet.tileWidth);
            numTilesY = (tex.height - spriteSheet.tileMarginY + spriteSheet.tileSpacingY) / (spriteSheet.tileSpacingY + spriteSheet.tileHeight);
        }
        void AddSprites(tk2dSpriteSheetSource spriteSheet)
        {
            int        spriteSheetId         = SpriteCollection.GetSpriteSheetId(spriteSheet);
            List <int> usedSpriteCoordinates = new List <int>();

            var tex      = spriteSheet.texture;
            int regionId = 0;

            int numTilesX, numTilesY;

            GetNumTilesForSpriteSheet(spriteSheet, out numTilesX, out numTilesY);
            for (int idY = 0; idY < numTilesY; ++idY)
            {
                for (int idX = 0; idX < numTilesX; ++idX)
                {
                    int x, y;
                    GetTileCoordinateForSpriteSheet(spriteSheet, idX, idY, out x, out y);

                    int spriteSlot = GetSpriteSlotForSpriteSheetCell(spriteSheetId, idX, idY);
                    SpriteCollection.textureRefs[spriteSlot] = spriteSheet.texture;
                    var param = SpriteCollection.textureParams[spriteSlot];
                    param.hasSpriteSheetId = true;
                    param.spriteSheetId    = spriteSheetId;
                    param.spriteSheetX     = idX;
                    param.spriteSheetY     = idY;
                    param.extractRegion    = true;
                    param.regionId         = regionId;
                    param.regionX          = x;
                    param.regionY          = tex.height - spriteSheet.tileHeight - y;
                    param.regionW          = spriteSheet.tileWidth;
                    param.regionH          = spriteSheet.tileHeight;
                    param.pad = spriteSheet.pad;
                    int id = idY * numTilesX + idX;
                    param.name = tex.name + "/" + id.ToString();
                    usedSpriteCoordinates.Add(GetSpriteCoordinateHash(idX, idY));

                    regionId++;
                }
            }

            // Delete sprites from sprite sheet which aren't required any more
            for (int i = 0; i < SpriteCollection.textureParams.Count; ++i)
            {
                var p = SpriteCollection.textureParams[i];
                if (p.hasSpriteSheetId && p.spriteSheetId == spriteSheetId)
                {
                    int coordinateHash = GetSpriteCoordinateHash(p.spriteSheetX, p.spriteSheetY);
                    if (usedSpriteCoordinates.IndexOf(coordinateHash) == -1)
                    {
                        SpriteCollection.textureRefs[i] = null;
                        SpriteCollection.textureParams[i].Clear();
                    }
                }
            }

            host.OnSpriteCollectionChanged(true);
        }
        void DrawTextureView(tk2dSpriteSheetSource spriteSheet)
        {
            int  spriteSheetId = SpriteCollection.GetSpriteSheetId(spriteSheet);
            var  tex           = spriteSheet.texture;
            Rect rect          = GUILayoutUtility.GetRect(tex.width * zoomAmount, tex.height * zoomAmount, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false));

            if (Event.current.type == EventType.ScrollWheel)
            {
                zoomAmount -= Event.current.delta.y * 0.01f;
                Event.current.Use();
                HandleUtility.Repaint();
            }

            GUI.DrawTexture(rect, tex);

            // Draw handles
            if (selectedMode == EditMode.Config)
            {
                DrawGridOverlay(spriteSheet, rect);
            }

            if (selectedMode == EditMode.Edit)
            {
                ProcessSpriteSelectionUI(spriteSheet, rect);

                // Cope with deleted selections
                List <SpriteCollectionEditorEntry> ss = new List <SpriteCollectionEditorEntry>();
                foreach (var sel in selectedSprites)
                {
                    if (sel.index >= SpriteCollection.textureParams.Count)
                    {
                        continue;
                    }
                    var sprite = SpriteCollection.textureParams[sel.index];
                    if (sprite.hasSpriteSheetId && sprite.spriteSheetId == spriteSheetId)
                    {
                        ss.Add(sel);
                    }
                }
                selectedSprites = ss;

                // Draw selection outlines
                foreach (var sel in selectedSprites)
                {
                    var sprite = SpriteCollection.textureParams[sel.index];
                    int tileX = sprite.spriteSheetX;
                    int tileY = sprite.spriteSheetY;
                    int x, y;
                    GetTileCoordinateForSpriteSheet(spriteSheet, tileX, tileY, out x, out y);
                    Vector3   v0        = new Vector3(rect.x + x * zoomAmount, rect.x + y * zoomAmount, 0);
                    Vector3   v1        = v0 + new Vector3(spriteSheet.tileWidth * zoomAmount, spriteSheet.tileHeight * zoomAmount, 0);
                    Vector3[] rectVerts = { new Vector3(v0.x, v0.y, 0), new Vector3(v1.x, v0.y, 0), new Vector3(v1.x, v1.y, 0), new Vector3(v0.x, v1.y, 0) };
                    Handles.DrawSolidRectangleWithOutline(rectVerts, new Color(1, 1, 1, 0.2f), new Color(1, 1, 1, 1));
                }
            }
        }
		void AddSprites(tk2dSpriteSheetSource spriteSheet)
		{
			int spriteSheetId = SpriteCollection.GetSpriteSheetId(spriteSheet);
			List<int> usedSpriteCoordinates = new List<int>();
			
			var tex = spriteSheet.texture;
			int regionId = 0;

			int numTilesX, numTilesY;
			GetNumTilesForSpriteSheet(spriteSheet, out numTilesX, out numTilesY);
			for (int idY = 0; idY < numTilesY; ++idY)
			{
				for (int idX = 0; idX < numTilesX; ++idX)
				{
					int x, y;
					GetTileCoordinateForSpriteSheet(spriteSheet, idX, idY, out x, out y);
					
					int spriteSlot = GetSpriteSlotForSpriteSheetCell(spriteSheetId, idX, idY);
					var param = SpriteCollection.textureParams[spriteSlot];
					param.texture = spriteSheet.texture;
					param.hasSpriteSheetId = true;
					param.spriteSheetId = spriteSheetId;
					param.spriteSheetX = idX;
					param.spriteSheetY = idY;
					param.extractRegion = true;
					param.regionId = regionId;
					param.regionX = x;
					param.regionY = tex.height - spriteSheet.tileHeight - y;
					param.regionW = spriteSheet.tileWidth;
					param.regionH = spriteSheet.tileHeight;
					param.pad = spriteSheet.pad;
					int id = idY * numTilesX + idX;
					param.name = tex.name + "/" + id.ToString();
					usedSpriteCoordinates.Add(GetSpriteCoordinateHash(idX, idY));
					
					regionId++;
				}
			}
			
			// Delete sprites from sprite sheet which aren't required any more
			for (int i = 0; i < SpriteCollection.textureParams.Count; ++i)
			{
				var p = SpriteCollection.textureParams[i];
				if (p.hasSpriteSheetId && p.spriteSheetId == spriteSheetId)
				{
					int coordinateHash = GetSpriteCoordinateHash(p.spriteSheetX, p.spriteSheetY);
					if (usedSpriteCoordinates.IndexOf(coordinateHash) == -1)
					{
						SpriteCollection.textureParams[i].Clear();
					}
				}
			}
			
			host.OnSpriteCollectionChanged(true);
		}
Example #5
0
 public void CopyFrom(tk2dSpriteSheetSource src)
 {
     texture      = src.texture;
     tilesX       = src.tilesX;
     tilesY       = src.tilesY;
     numTiles     = src.numTiles;
     anchor       = src.anchor;
     pad          = src.pad;
     scale        = src.scale;
     colliderType = src.colliderType;
 }
 public int GetSpriteSheetId(tk2dSpriteSheetSource spriteSheet)
 {
     for (int index = 0; index < spriteSheets.Count; ++index)
     {
         if (spriteSheets[index] == spriteSheet)
         {
             return(index);
         }
     }
     return(0);
 }
        // Delete all sprites from a spritesheet
        public void DeleteSpriteSheet(tk2dSpriteSheetSource spriteSheet)
        {
            int index = GetSpriteSheetId(spriteSheet);

            for (int i = 0; i < textureParams.Count; ++i)
            {
                if (textureParams[i].hasSpriteSheetId && textureParams[i].spriteSheetId == index)
                {
                    textureParams[i] = new tk2dSpriteCollectionDefinition();
                }
            }

            spriteSheets[index] = new tk2dSpriteSheetSource();
            Trim();
        }
Example #8
0
        public void Select(tk2dSpriteSheetSource spriteSheet, int[] ids)
        {
            selectedSprites.Clear();
            activeSelectedSprites.Clear();
            rectSelectX             = rectSelectY = -1;
            textureViewScrollBar    = Vector2.zero;
            inspectorScrollBar      = Vector2.zero;
            activeSpriteSheetSource = spriteSheet;
            selectedMode            = EditMode.Edit;

            foreach (int id in ids)
            {
                var v = new SpriteCollectionEditorEntry();
                v.index = id;
                selectedSprites.Add(v);
            }
        }
        public int FindOrCreateEmptySpriteSheetSlot()
        {
            for (int index = 0; index < spriteSheets.Count; ++index)
            {
                if (!spriteSheets[index].active)
                {
                    spriteSheets[index].active  = true;
                    spriteSheets[index].version = tk2dSpriteSheetSource.CURRENT_VERSION;
                    return(index);
                }
            }
            var spriteSheet = new tk2dSpriteSheetSource();

            spriteSheet.active  = true;
            spriteSheet.version = tk2dSpriteSheetSource.CURRENT_VERSION;
            spriteSheets.Add(spriteSheet);
            return(spriteSheets.Count - 1);
        }
Example #10
0
        void DrawGridOverlay(tk2dSpriteSheetSource spriteSheet, Rect rect)
        {
            if (spriteSheet.tileWidth > 0 && spriteSheet.tileHeight > 0)
            {
                var tex = spriteSheet.texture;

                Color oldColor = Handles.color;
                Handles.color = Color.red;
                int numTilesX, numTilesY;
                GetNumTilesForSpriteSheet(spriteSheet, out numTilesX, out numTilesY);
                for (int tileY = 0; tileY < numTilesY; ++tileY)
                {
                    int x, y;
                    GetTileCoordinateForSpriteSheet(spriteSheet, 0, tileY, out x, out y);
                    Handles.DrawLine(new Vector3(rect.x, rect.y + y * zoomAmount, 0),
                                     new Vector3(rect.x + tex.width * zoomAmount, rect.y + y * zoomAmount, 0));
                    Handles.DrawLine(new Vector3(rect.x, rect.y + (y + spriteSheet.tileHeight) * zoomAmount, 0),
                                     new Vector3(rect.x + tex.width * zoomAmount, rect.y + (y + spriteSheet.tileHeight) * zoomAmount, 0));
                }
                for (int tileX = 0; tileX < numTilesX; ++tileX)
                {
                    int x, y;
                    GetTileCoordinateForSpriteSheet(spriteSheet, tileX, 0, out x, out y);
                    Handles.DrawLine(new Vector3(rect.x + x * zoomAmount, rect.y, 0),
                                     new Vector3(rect.x + x * zoomAmount, rect.y + tex.height * zoomAmount, 0));
                    Handles.DrawLine(new Vector3(rect.x + (x + spriteSheet.tileWidth) * zoomAmount, rect.y, 0),
                                     new Vector3(rect.x + (x + spriteSheet.tileWidth) * zoomAmount, rect.y + tex.height * zoomAmount, 0));
                }

                // Highlight ONE tile
                {
                    int x, y;
                    GetTileCoordinateForSpriteSheet(spriteSheet, 0, 0, out x, out y);
                    Vector3   v0        = new Vector3(rect.x + x * zoomAmount, rect.x + y * zoomAmount, 0);
                    Vector3   v1        = v0 + new Vector3(spriteSheet.tileWidth * zoomAmount, spriteSheet.tileHeight * zoomAmount, 0);
                    Vector3[] rectVerts = { new Vector3(v0.x, v0.y, 0), new Vector3(v1.x, v0.y, 0), new Vector3(v1.x, v1.y, 0), new Vector3(v0.x, v1.y, 0) };
                    Handles.DrawSolidRectangleWithOutline(rectVerts, new Color(1, 1, 1, 0.2f), new Color(1, 1, 1, 1));
                }
                Handles.color = oldColor;
            }
        }
Example #11
0
    public void CopyFrom(tk2dSpriteSheetSource src)
    {
        texture = src.texture;
        tilesX = src.tilesX;
        tilesY = src.tilesY;
        numTiles = src.numTiles;
        anchor = src.anchor;
        pad = src.pad;
        scale = src.scale;
        colliderType = src.colliderType;
        version = src.version;

        active = src.active;
        tileWidth = src.tileWidth;
        tileHeight = src.tileHeight;
        tileSpacingX = src.tileSpacingX;
        tileSpacingY = src.tileSpacingY;
        tileMarginX = src.tileMarginX;
        tileMarginY = src.tileMarginY;
        splitMethod = src.splitMethod;
    }
Example #12
0
    public void CopyFrom(tk2dSpriteSheetSource src)
    {
        texture      = src.texture;
        tilesX       = src.tilesX;
        tilesY       = src.tilesY;
        numTiles     = src.numTiles;
        anchor       = src.anchor;
        pad          = src.pad;
        scale        = src.scale;
        colliderType = src.colliderType;
        version      = src.version;

        active       = src.active;
        tileWidth    = src.tileWidth;
        tileHeight   = src.tileHeight;
        tileSpacingX = src.tileSpacingX;
        tileSpacingY = src.tileSpacingY;
        tileMarginX  = src.tileMarginX;
        tileMarginY  = src.tileMarginY;
        splitMethod  = src.splitMethod;
    }
Example #13
0
    public bool CompareTo(tk2dSpriteSheetSource src)
    {
        if (texture != src.texture) return false;
        if (tilesX != src.tilesX) return false;
        if (tilesY != src.tilesY) return false;
        if (numTiles != src.numTiles) return false;
        if (anchor != src.anchor) return false;
        if (pad != src.pad) return false;
        if (scale != src.scale) return false;
        if (colliderType != src.colliderType) return false;
        if (version != src.version) return false;

        if (active != src.active) return false;
        if (tileWidth != src.tileWidth) return false;
        if (tileHeight != src.tileHeight) return false;
        if (tileSpacingX != src.tileSpacingX) return false;
        if (tileSpacingY != src.tileSpacingY) return false;
        if (tileMarginX != src.tileMarginX) return false;
        if (tileMarginY != src.tileMarginY) return false;
        if (splitMethod != src.splitMethod) return false;

        return true;
    }
Example #14
0
    public bool CompareTo(tk2dSpriteSheetSource src)
    {
        if (texture != src.texture)
        {
            return(false);
        }
        if (tilesX != src.tilesX)
        {
            return(false);
        }
        if (tilesY != src.tilesY)
        {
            return(false);
        }
        if (numTiles != src.numTiles)
        {
            return(false);
        }
        if (anchor != src.anchor)
        {
            return(false);
        }
        if (pad != src.pad)
        {
            return(false);
        }
        if (scale != src.scale)
        {
            return(false);
        }
        if (colliderType != src.colliderType)
        {
            return(false);
        }

        return(true);
    }
Example #15
0
        public void CopyFromSource()
        {
            this.obj.Upgrade();             // make sure its up to date

            textureParams = new List <tk2dSpriteCollectionDefinition>(obj.textureParams.Length);
            foreach (var v in obj.textureParams)
            {
                if (v == null)
                {
                    textureParams.Add(null);
                }
                else
                {
                    var t = new tk2dSpriteCollectionDefinition();
                    t.CopyFrom(v);
                    textureParams.Add(t);
                }
            }

            spriteSheets = new List <tk2dSpriteSheetSource>();
            if (obj.spriteSheets != null)
            {
                foreach (var v in obj.spriteSheets)
                {
                    if (v == null)
                    {
                        spriteSheets.Add(null);
                    }
                    else
                    {
                        var t = new tk2dSpriteSheetSource();
                        t.CopyFrom(v);
                        spriteSheets.Add(t);
                    }
                }
            }

            fonts = new List <tk2dSpriteCollectionFont>();
            if (obj.fonts != null)
            {
                foreach (var v in obj.fonts)
                {
                    if (v == null)
                    {
                        fonts.Add(null);
                    }
                    else
                    {
                        var t = new tk2dSpriteCollectionFont();
                        t.CopyFrom(v);
                        fonts.Add(t);
                    }
                }
            }

            UpgradeLegacySpriteSheets();

            var target = this;
            var source = obj;

            target.platforms = new List <tk2dSpriteCollectionPlatform>();
            foreach (tk2dSpriteCollectionPlatform plat in source.platforms)
            {
                tk2dSpriteCollectionPlatform p = new tk2dSpriteCollectionPlatform();
                p.CopyFrom(plat);
                target.platforms.Add(p);
            }
            if (target.platforms.Count == 0)
            {
                tk2dSpriteCollectionPlatform plat = new tk2dSpriteCollectionPlatform();                 // add a null platform
                target.platforms.Add(plat);
            }

            target.assetName = source.assetName;
            target.loadable  = source.loadable;

            target.maxTextureSize      = source.maxTextureSize;
            target.forceTextureSize    = source.forceTextureSize;
            target.forcedTextureWidth  = source.forcedTextureWidth;
            target.forcedTextureHeight = source.forcedTextureHeight;

            target.textureCompression   = source.textureCompression;
            target.atlasWidth           = source.atlasWidth;
            target.atlasHeight          = source.atlasHeight;
            target.forceSquareAtlas     = source.forceSquareAtlas;
            target.atlasWastage         = source.atlasWastage;
            target.allowMultipleAtlases = source.allowMultipleAtlases;

            target.spriteCollection   = source.spriteCollection;
            target.premultipliedAlpha = source.premultipliedAlpha;

            CopyArray(ref target.altMaterials, source.altMaterials);
            CopyArray(ref target.atlasMaterials, source.atlasMaterials);
            CopyArray(ref target.atlasTextures, source.atlasTextures);

            target.useTk2dCamera        = source.useTk2dCamera;
            target.targetHeight         = source.targetHeight;
            target.targetOrthoSize      = source.targetOrthoSize;
            target.globalScale          = source.globalScale;
            target.physicsDepth         = source.physicsDepth;
            target.disableTrimming      = source.disableTrimming;
            target.normalGenerationMode = source.normalGenerationMode;
            target.padAmount            = source.padAmount;
            target.autoUpdate           = source.autoUpdate;
            target.editorDisplayScale   = source.editorDisplayScale;

            // Texture settings
            target.filterMode = source.filterMode;
            target.wrapMode   = source.wrapMode;
            target.userDefinedTextureSettings = source.userDefinedTextureSettings;
            target.mipmapEnabled = source.mipmapEnabled;
            target.anisoLevel    = source.anisoLevel;
        }
		public void Select(tk2dSpriteSheetSource spriteSheet, int[] ids)
		{
			selectedSprites.Clear();
			activeSelectedSprites.Clear();
			rectSelectX = rectSelectY = -1;
			textureViewScrollBar = Vector2.zero;
			inspectorScrollBar = Vector2.zero;
			activeSpriteSheetSource = spriteSheet;
			selectedMode = EditMode.Edit;	
			
			foreach (int id in ids)
			{
				var v = new SpriteCollectionEditorEntry();
				v.index = id;
				selectedSprites.Add(v);
			}
		}
		public bool Draw(List<SpriteCollectionEditorEntry> selectedEntries)
		{
			if (selectedEntries.Count == 0 || selectedEntries[0].type != SpriteCollectionEditorEntry.Type.SpriteSheet)
				return false;
			
			var entry = selectedEntries[selectedEntries.Count - 1];
			var spriteSheet = SpriteCollection.spriteSheets[ entry.index ];

			if (activeSpriteSheetSource != spriteSheet)
			{
				// reset state data
				selectedSprites.Clear();
				activeSelectedSprites.Clear();
				rectSelectX = rectSelectY = -1;
				textureViewScrollBar = Vector2.zero;
				inspectorScrollBar = Vector2.zero;
				activeSpriteSheetSource = spriteSheet;
				selectedMode = EditMode.Edit;
			}

			if (spriteSheet.tileWidth == 0 || spriteSheet.tileHeight == 0)
				selectedMode = EditMode.Config;			
			
			bool doDelete = false;
			GUILayout.BeginHorizontal();
			
			// Texture View
			GUILayout.BeginVertical(tk2dEditorSkin.SC_BodyBackground, GUILayout.ExpandHeight(true), GUILayout.ExpandWidth(true));
			textureViewScrollBar = GUILayout.BeginScrollView(textureViewScrollBar);
			if (spriteSheet.texture != null)
			{
				spriteSheet.texture.filterMode = FilterMode.Point;
				DrawTextureView(spriteSheet);
			}
			GUILayout.EndScrollView();
			GUILayout.EndVertical();

			// Inspector
			tk2dGuiUtility.LookLikeControls(100.0f, 100.0f);
			inspectorScrollBar = GUILayout.BeginScrollView(inspectorScrollBar, GUILayout.ExpandHeight(true), GUILayout.Width(host.InspectorWidth));

			// Header
			GUILayout.BeginVertical(tk2dEditorSkin.SC_InspectorHeaderBG, GUILayout.ExpandWidth(true));
			GUILayout.Label("Sprite Sheet");
			GUILayout.BeginHorizontal();
			Texture2D newTexture = EditorGUILayout.ObjectField("Texture", spriteSheet.texture, typeof(Texture2D), false) as Texture2D;
			if (newTexture != spriteSheet.texture)
			{
				bool needFullRebuild = false;
				if (spriteSheet.texture == null)
					needFullRebuild = true;
				
				spriteSheet.texture = newTexture;
				if (needFullRebuild)
					host.OnSpriteCollectionChanged(true);
				else
					host.OnSpriteCollectionSortChanged();
			}
			GUILayout.FlexibleSpace();
			if (GUILayout.Button("Delete", EditorStyles.miniButton)) doDelete = true;
			GUILayout.EndHorizontal();
			GUILayout.EndVertical();
			
			bool textureReady = false;
			
			if (spriteSheet.texture != null)
			{
				GUILayout.BeginVertical(tk2dEditorSkin.SC_InspectorBG, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(false));
				string assetPath = AssetDatabase.GetAssetPath(spriteSheet.texture);
				if (assetPath.Length > 0)
				{
					// make sure the source texture is npot and readable, and uncompressed
					if (tk2dSpriteCollectionBuilder.IsTextureImporterSetUp(assetPath))
					{
						textureReady = true;
					}
					else
					{
						if (tk2dGuiUtility.InfoBoxWithButtons(
							"The texture importer needs to be reconfigured to be used as a sprite sheet source. " +
							"Please note that this will globally change this texture importer.",
							tk2dGuiUtility.WarningLevel.Info,
							"Set up") != -1)
						{
							tk2dSpriteCollectionBuilder.ConfigureSpriteTextureImporter(assetPath);
							AssetDatabase.ImportAsset(assetPath);
						}						
					}
				}			
				GUILayout.EndVertical();
			}
			
			// Body
			if (textureReady)
			{
				GUILayout.BeginVertical(tk2dEditorSkin.SC_InspectorBG, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(false));
				selectedMode = (EditMode)GUILayout.Toolbar((int)selectedMode, new string[] { "Edit", "Config" } );
				EditorGUILayout.Space();
				GUILayout.EndVertical();
				
				if (selectedMode == EditMode.Edit)
				{
					if (Event.current.type == EventType.Layout)
						activeSelectedSprites = new List<SpriteCollectionEditorEntry>(selectedSprites);
					
					if (activeSelectedSprites.Count > 0)
						host.SpriteView.DrawSpriteEditorInspector(activeSelectedSprites, true, true);
				}
				else
				{
					GUILayout.BeginVertical(tk2dEditorSkin.SC_InspectorBG, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(false));
					
					spriteSheet.tileWidth = EditorGUILayout.IntField("Tile Width", spriteSheet.tileWidth);
					spriteSheet.tileHeight = EditorGUILayout.IntField("Tile Height", spriteSheet.tileHeight);
					spriteSheet.tileMarginX = EditorGUILayout.IntField("Tile Margin X", spriteSheet.tileMarginX);
					spriteSheet.tileMarginY = EditorGUILayout.IntField("Tile Margin Y", spriteSheet.tileMarginY);
					spriteSheet.tileSpacingX = EditorGUILayout.IntField("Tile Spacing X", spriteSheet.tileSpacingX);
					spriteSheet.tileSpacingY = EditorGUILayout.IntField("Tile Spacing Y", spriteSheet.tileSpacingY);
					
					spriteSheet.pad = (tk2dSpriteCollectionDefinition.Pad)EditorGUILayout.EnumPopup("Pad", spriteSheet.pad);
					if (spriteSheet.pad == tk2dSpriteCollectionDefinition.Pad.Default)
					{
						tk2dGuiUtility.InfoBox("The sprite sheet is configured to use default padding mode. " +
							"It is advised to select an explicit padding mode depending on the usage of the " +
							"sprites within the sprite sheet.\n\n" +
							"BlackZeroAlpha - Recommended for animations\n" +
							"Extend - Recommended for tilemaps", tk2dGuiUtility.WarningLevel.Warning);
					}
					
					// Apply button
					GUILayout.Space(8);
					GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace();
					if (spriteSheet.texture != null &&
						spriteSheet.tileWidth > 0 && spriteSheet.tileWidth <= spriteSheet.texture.width &&
						spriteSheet.tileHeight > 0 && spriteSheet.tileHeight <= spriteSheet.texture.height &&
						GUILayout.Button("Apply", EditorStyles.miniButton))
					{
						AddSprites(spriteSheet);
						selectedMode = EditMode.Edit;
					}
					GUILayout.EndHorizontal();
					
					GUILayout.EndVertical();
				}
			}
			
			GUILayout.BeginVertical(tk2dEditorSkin.SC_InspectorBG, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
			GUILayout.EndVertical();

			
			// /Body
			GUILayout.EndScrollView();

			// make dragable
			tk2dPreferences.inst.spriteCollectionInspectorWidth -= (int)tk2dGuiUtility.DragableHandle(4819284, GUILayoutUtility.GetLastRect(), 0, tk2dGuiUtility.DragDirection.Horizontal);

			GUILayout.EndHorizontal();
			
			if (doDelete)
			{
				string message = "Deleting a sprite sheet will delete all sprites sourced from this sprite sheet. " +
					"Are you sure you want to do this?";
				if (EditorUtility.DisplayDialog("Delete sprite sheet", message, "Yes", "No"))
				{
					SpriteCollection.DeleteSpriteSheet(spriteSheet);
					host.OnSpriteCollectionChanged(false);
				}
			}
			
			return true;
		}
		void ProcessSpriteSelectionUI(tk2dSpriteSheetSource spriteSheet, Rect rect)
		{
			int spriteSheetId = SpriteCollection.GetSpriteSheetId(spriteSheet);
			if (rect.Contains(Event.current.mousePosition))
			{
				Vector2 localMousePos = (Event.current.mousePosition - new Vector2(rect.x, rect.y)) / zoomAmount;
				int tileX = ((int)localMousePos.x - spriteSheet.tileMarginX) / (spriteSheet.tileSpacingX + spriteSheet.tileWidth);
				int tileY = ((int)localMousePos.y - spriteSheet.tileMarginY) / (spriteSheet.tileSpacingY + spriteSheet.tileHeight);
				int numTilesX, numTilesY;
				GetNumTilesForSpriteSheet(spriteSheet, out numTilesX, out numTilesY);
				
				if (Event.current.type == EventType.MouseDown)
				{
					bool multiSelectKey = (Application.platform == RuntimePlatform.OSXEditor)?Event.current.command:Event.current.control;
					if (tileX >= 0 && tileX < numTilesX && tileY >= 0 && tileY < numTilesY)
					{
						if (!multiSelectKey)
							selectedSprites.Clear();
						
						int id = FindSpriteSlotForSpriteSheetCell(SpriteCollection.GetSpriteSheetId(spriteSheet), tileX, tileY);
						if (id != -1)
						{
							if (!multiSelectKey)
							{
								rectSelectX = tileX;
								rectSelectY = tileY;
							}
							
							bool found = false;
							foreach (var sel in selectedSprites)
							{
								if (sel.index == id)
								{
									found = true;
									selectedSprites.Remove(sel);
									break;
								}
							}
							if (!found)
							{
								var entry = new SpriteCollectionEditorEntry();
								entry.index = id;
								selectedSprites.Add(entry);
							}
							HandleUtility.Repaint();
						}
					}
				}
				else if (Event.current.type == EventType.MouseDrag)
				{
					if (rectSelectX != -1 && rectSelectY != -1)
					{
						int x0 = Mathf.Min(tileX, rectSelectX);
						int x1 = Mathf.Max(tileX, rectSelectX);
						int y0 = Mathf.Min(tileY, rectSelectY);
						int y1 = Mathf.Max(tileY, rectSelectY);
						selectedSprites.Clear();
						for (int y = y0; y <= y1; ++y)
						{
							for (int x = x0; x <= x1; ++x)
							{
								int id = FindSpriteSlotForSpriteSheetCell(spriteSheetId, x, y);
								if (id != -1)
								{
									var entry = new SpriteCollectionEditorEntry();
									entry.index = id;
									selectedSprites.Add(entry);
								}
							}
						}
						HandleUtility.Repaint();
					}
				}
				else if (Event.current.type == EventType.MouseUp)
				{
					rectSelectX = -1;
					rectSelectY = -1;
				}
			}			
		}
		void DrawTextureView(tk2dSpriteSheetSource spriteSheet)
		{
			int spriteSheetId = SpriteCollection.GetSpriteSheetId(spriteSheet);
			var tex = spriteSheet.texture;
			
			int border = 16;
			float width = tex.width * zoomAmount;
			float height = tex.height * zoomAmount;
			Rect baseRect = GUILayoutUtility.GetRect(border * 2 + width, border * 2 + height, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
			tk2dGrid.Draw(baseRect);
			Rect rect = new Rect(baseRect.x + border, baseRect.y + border, width, height);
			
			if (Event.current.type == EventType.ScrollWheel)
			{
				zoomAmount -= Event.current.delta.y * 0.01f;
				Event.current.Use();
				HandleUtility.Repaint();
			}
			
			GUI.DrawTexture(rect, tex);
			
			// Draw handles
			if (selectedMode == EditMode.Config)
			{
				DrawGridOverlay(spriteSheet, rect);
			}
			
			if (selectedMode == EditMode.Edit)
			{
				ProcessSpriteSelectionUI(spriteSheet, rect);
				
				// Cope with deleted selections
				List<SpriteCollectionEditorEntry> ss = new List<SpriteCollectionEditorEntry>();
				foreach (var sel in selectedSprites)
				{
					if (sel.index >= SpriteCollection.textureParams.Count)
						continue;
					var sprite = SpriteCollection.textureParams[sel.index];
					if (sprite.hasSpriteSheetId && sprite.spriteSheetId == spriteSheetId)
						ss.Add(sel);
				}
				selectedSprites = ss;
				
				// Draw selection outlines
				foreach (var sel in selectedSprites)
				{
					var sprite = SpriteCollection.textureParams[sel.index];
					int tileX = sprite.spriteSheetX;
					int tileY = sprite.spriteSheetY;
					int x, y;
					GetTileCoordinateForSpriteSheet(spriteSheet, tileX, tileY, out x, out y);
					Vector3 v0 = new Vector3(rect.x + x * zoomAmount, rect.x + y * zoomAmount, 0);
					Vector3 v1 = v0 + new Vector3(spriteSheet.tileWidth * zoomAmount, spriteSheet.tileHeight * zoomAmount, 0);
					Vector3[] rectVerts = { new Vector3(v0.x, v0.y, 0), new Vector3(v1.x, v0.y, 0), new Vector3(v1.x, v1.y, 0), new Vector3(v0.x, v1.y, 0) };
					Handles.DrawSolidRectangleWithOutline(rectVerts, new Color(1,1,1,0.2f), new Color(1,1,1,1));
				}
			}
		}
 public int GetSpriteSheetId(tk2dSpriteSheetSource spriteSheet)
 {
     for (int index = 0; index < spriteSheets.Count; ++index)
         if (spriteSheets[index] == spriteSheet) return index;
     return 0;
 }
		void DrawGridOverlay(tk2dSpriteSheetSource spriteSheet, Rect rect)
		{
			if (spriteSheet.tileWidth > 0 && spriteSheet.tileHeight > 0)
			{
				var tex = spriteSheet.texture;
				
				Color oldColor = Handles.color;
				Handles.color = Color.red;
				int numTilesX, numTilesY;
				GetNumTilesForSpriteSheet(spriteSheet, out numTilesX, out numTilesY);
				for (int tileY = 0; tileY < numTilesY; ++tileY)
				{
					int x, y;
					GetTileCoordinateForSpriteSheet(spriteSheet, 0, tileY, out x, out y);
					Handles.DrawLine(new Vector3(rect.x, rect.y + y * zoomAmount, 0),
									 new Vector3(rect.x + tex.width * zoomAmount, rect.y + y * zoomAmount, 0));
					Handles.DrawLine(new Vector3(rect.x, rect.y + (y + spriteSheet.tileHeight) * zoomAmount, 0),
									 new Vector3(rect.x + tex.width * zoomAmount, rect.y + (y + spriteSheet.tileHeight) * zoomAmount, 0));
				}
				for (int tileX = 0; tileX < numTilesX; ++tileX)
				{
					int x, y;
					GetTileCoordinateForSpriteSheet(spriteSheet, tileX, 0, out x, out y);
					Handles.DrawLine(new Vector3(rect.x + x * zoomAmount, rect.y, 0),
									 new Vector3(rect.x + x * zoomAmount, rect.y + tex.height * zoomAmount, 0));
					Handles.DrawLine(new Vector3(rect.x + (x + spriteSheet.tileWidth) * zoomAmount, rect.y, 0),
									 new Vector3(rect.x + (x + spriteSheet.tileWidth) * zoomAmount, rect.y + tex.height * zoomAmount, 0));
				}
				
				// Highlight ONE tile
				{
					int x, y;
					GetTileCoordinateForSpriteSheet(spriteSheet, 0, 0, out x, out y);
					Vector3 v0 = new Vector3(rect.x + x * zoomAmount, rect.x + y * zoomAmount, 0);
					Vector3 v1 = v0 + new Vector3(spriteSheet.tileWidth * zoomAmount, spriteSheet.tileHeight * zoomAmount, 0);
					Vector3[] rectVerts = { new Vector3(v0.x, v0.y, 0), new Vector3(v1.x, v0.y, 0), new Vector3(v1.x, v1.y, 0), new Vector3(v0.x, v1.y, 0) };
					Handles.DrawSolidRectangleWithOutline(rectVerts, new Color(1,1,1,0.2f), new Color(1,1,1,1));
				}
				Handles.color = oldColor;
			}			
		}
        public void CopyFromSource(bool copyBuilt)
        {
            this.obj.Upgrade();             // make sure its up to date

            textureParams = new List <tk2dSpriteCollectionDefinition>(obj.textureParams.Length);
            foreach (var v in obj.textureParams)
            {
                if (v == null)
                {
                    textureParams.Add(null);
                }
                else
                {
                    var t = new tk2dSpriteCollectionDefinition();
                    t.CopyFrom(v);
                    textureParams.Add(t);
                }
            }

            spriteSheets = new List <tk2dSpriteSheetSource>();
            if (obj.spriteSheets != null)
            {
                foreach (var v in obj.spriteSheets)
                {
                    if (v == null)
                    {
                        spriteSheets.Add(null);
                    }
                    else
                    {
                        var t = new tk2dSpriteSheetSource();
                        t.CopyFrom(v);
                        spriteSheets.Add(t);
                    }
                }
            }

            fonts = new List <tk2dSpriteCollectionFont>();
            if (obj.fonts != null)
            {
                foreach (var v in obj.fonts)
                {
                    if (v == null)
                    {
                        fonts.Add(null);
                    }
                    else
                    {
                        var t = new tk2dSpriteCollectionFont();
                        t.CopyFrom(v);
                        fonts.Add(t);
                    }
                }
            }

            attachPointTestSprites.Clear();
            foreach (var v in obj.attachPointTestSprites)
            {
                if (v.spriteCollection != null && v.spriteCollection.IsValidSpriteId(v.spriteId))
                {
                    tk2dSpriteCollection.AttachPointTestSprite ap = new tk2dSpriteCollection.AttachPointTestSprite();
                    ap.CopyFrom(v);
                    attachPointTestSprites[v.attachPointName] = v;
                }
            }

            UpgradeLegacySpriteSheets();

            var target = this;
            var source = obj;

            target.platforms = new List <tk2dSpriteCollectionPlatform>();
            foreach (tk2dSpriteCollectionPlatform plat in source.platforms)
            {
                tk2dSpriteCollectionPlatform p = new tk2dSpriteCollectionPlatform();
                p.CopyFrom(plat);
                target.platforms.Add(p);
            }
            if (target.platforms.Count == 0)
            {
                tk2dSpriteCollectionPlatform plat = new tk2dSpriteCollectionPlatform();                 // add a null platform
                target.platforms.Add(plat);
            }

            target.assetName   = source.assetName;
            target.loadable    = source.loadable;
            target.atlasFormat = source.atlasFormat;

            target.maxTextureSize      = source.maxTextureSize;
            target.forceTextureSize    = source.forceTextureSize;
            target.forcedTextureWidth  = source.forcedTextureWidth;
            target.forcedTextureHeight = source.forcedTextureHeight;

            target.textureCompression   = source.textureCompression;
            target.atlasWidth           = source.atlasWidth;
            target.atlasHeight          = source.atlasHeight;
            target.forceSquareAtlas     = source.forceSquareAtlas;
            target.atlasWastage         = source.atlasWastage;
            target.allowMultipleAtlases = source.allowMultipleAtlases;
            target.disableRotation      = source.disableRotation;
            target.removeDuplicates     = source.removeDuplicates;
            target.allowSpannedDicing   = source.allowSpannedDicing;

            target.premultipliedAlpha = source.premultipliedAlpha;

            target.sizeDef.CopyFrom(source.sizeDef);
            target.globalScale          = source.globalScale;
            target.globalTextureRescale = source.globalTextureRescale;
            target.physicsDepth         = source.physicsDepth;
            target.physicsEngine        = source.physicsEngine;
            target.disableTrimming      = source.disableTrimming;
            target.normalGenerationMode = source.normalGenerationMode;
            target.padAmount            = source.padAmount;
            target.autoUpdate           = source.autoUpdate;
            target.editorDisplayScale   = source.editorDisplayScale;

            // Texture settings
            target.filterMode = source.filterMode;
            target.wrapMode   = source.wrapMode;
            target.userDefinedTextureSettings = source.userDefinedTextureSettings;
            target.mipmapEnabled = source.mipmapEnabled;
            target.anisoLevel    = source.anisoLevel;

            target.linkedSpriteCollections.Clear();
            for (int i = 0; i < source.linkedSpriteCollections.Count; ++i)
            {
                tk2dLinkedSpriteCollection t = new tk2dLinkedSpriteCollection();
                t.name             = source.linkedSpriteCollections[i].name;
                t.spriteCollection = source.linkedSpriteCollections[i].spriteCollection;
                target.linkedSpriteCollections.Add(t);
            }

            if (copyBuilt)
            {
                CopyBuiltFromSource(source);
            }
        }
        public void CopyFromSource()
        {
            textureParams = new List<tk2dSpriteCollectionDefinition>(obj.textureParams.Length);
            foreach (var v in obj.textureParams)
            {
                if (v == null)
                    textureParams.Add(null);
                else
                {
                    var t = new tk2dSpriteCollectionDefinition();
                    t.CopyFrom(v);
                    textureParams.Add(t);
                }
            }

            textureRefs = new List<Texture2D>(obj.textureRefs.Length);
            foreach (var v in obj.textureRefs)
                textureRefs.Add(v);

            spriteSheets = new List<tk2dSpriteSheetSource>();
            if (obj.spriteSheets != null)
            {
                foreach (var v in obj.spriteSheets)
                {
                    if (v == null)
                        spriteSheets.Add(null);
                    else
                    {
                        var t = new tk2dSpriteSheetSource();
                        t.CopyFrom(v);
                        spriteSheets.Add(t);
                    }
                }
            }

            fonts = new List<tk2dSpriteCollectionFont>();
            if (obj.fonts != null)
            {
                foreach (var v in obj.fonts)
                {
                    if (v == null)
                        fonts.Add(null);
                    else
                    {
                        var t = new tk2dSpriteCollectionFont();
                        t.CopyFrom(v);
                        fonts.Add(t);
                    }
                }
            }

            UpgradeLegacySpriteSheets();

            var target = this;
            var source = obj;

            target.maxTextureSize = source.maxTextureSize;
            target.textureCompression = source.textureCompression;
            target.atlasWidth = source.atlasWidth;
            target.atlasHeight = source.atlasHeight;
            target.forceSquareAtlas = source.forceSquareAtlas;
            target.atlasWastage = source.atlasWastage;
            target.allowMultipleAtlases = source.allowMultipleAtlases;

            target.spriteCollection = source.spriteCollection;
            target.premultipliedAlpha = source.premultipliedAlpha;

            target.atlasMaterials = source.atlasMaterials;
            target.atlasTextures = source.atlasTextures;
            target.useTk2dCamera = source.useTk2dCamera;
            target.targetHeight = source.targetHeight;
            target.targetOrthoSize = source.targetOrthoSize;
            target.pixelPerfectPointSampled = source.pixelPerfectPointSampled;
            target.physicsDepth = source.physicsDepth;
            target.disableTrimming = source.disableTrimming;
            target.normalGenerationMode = source.normalGenerationMode;
            target.padAmount = source.padAmount;
            target.autoUpdate = source.autoUpdate;
        }
Example #24
0
        public void CopyFromSource()
        {
            this.obj.Upgrade();             // make sure its up to date

            textureParams = new List <tk2dSpriteCollectionDefinition>(obj.textureParams.Length);
            foreach (var v in obj.textureParams)
            {
                if (v == null)
                {
                    textureParams.Add(null);
                }
                else
                {
                    var t = new tk2dSpriteCollectionDefinition();
                    t.CopyFrom(v);
                    textureParams.Add(t);
                }
            }

            spriteSheets = new List <tk2dSpriteSheetSource>();
            if (obj.spriteSheets != null)
            {
                foreach (var v in obj.spriteSheets)
                {
                    if (v == null)
                    {
                        spriteSheets.Add(null);
                    }
                    else
                    {
                        var t = new tk2dSpriteSheetSource();
                        t.CopyFrom(v);
                        spriteSheets.Add(t);
                    }
                }
            }

            fonts = new List <tk2dSpriteCollectionFont>();
            if (obj.fonts != null)
            {
                foreach (var v in obj.fonts)
                {
                    if (v == null)
                    {
                        fonts.Add(null);
                    }
                    else
                    {
                        var t = new tk2dSpriteCollectionFont();
                        t.CopyFrom(v);
                        fonts.Add(t);
                    }
                }
            }

            attachPointTestSprites.Clear();
            foreach (var v in obj.attachPointTestSprites)
            {
                if (v.spriteCollection != null && v.spriteCollection.IsValidSpriteId(v.spriteId))
                {
                    tk2dSpriteCollection.AttachPointTestSprite ap = new tk2dSpriteCollection.AttachPointTestSprite();
                    ap.CopyFrom(v);
                    attachPointTestSprites[v.attachPointName] = v;
                }
            }

            UpgradeLegacySpriteSheets();

            var target = this;
            var source = obj;

            target.platforms = new List <tk2dSpriteCollectionPlatform>();
            foreach (tk2dSpriteCollectionPlatform plat in source.platforms)
            {
                tk2dSpriteCollectionPlatform p = new tk2dSpriteCollectionPlatform();
                p.CopyFrom(plat);
                target.platforms.Add(p);
            }
            if (target.platforms.Count == 0)
            {
                tk2dSpriteCollectionPlatform plat = new tk2dSpriteCollectionPlatform();                 // add a null platform
                target.platforms.Add(plat);
            }
            target.enablePlatformScaleVertex = source.enablePlatformScaleVertex;

            target.assetName   = source.assetName;
            target.loadable    = source.loadable;
            target.atlasFormat = source.atlasFormat;

            target.maxTextureSize                 = source.maxTextureSize;
            target.forceTextureSize               = source.forceTextureSize;
            target.forcedTextureWidth             = source.forcedTextureWidth;
            target.forcedTextureHeight            = source.forcedTextureHeight;
            target.freeSizeAtlas                  = source.freeSizeAtlas;
            target.amountOfPixelsToExtendFreeSize = source.amountOfPixelsToExtendFreeSize;

            target.forceA8     = source.forceA8;
            target.forceShader = source.forceShader;

            target.textureCompression   = source.textureCompression;
            target.atlasWidth           = source.atlasWidth;
            target.atlasHeight          = source.atlasHeight;
            target.forceSquareAtlas     = source.forceSquareAtlas;
            target.atlasWastage         = source.atlasWastage;
            target.allowMultipleAtlases = source.allowMultipleAtlases;
            target.disableRotation      = source.disableRotation;
            target.removeDuplicates     = source.removeDuplicates;
            target.usePosterize         = source.usePosterize;
            target.posterizeValue       = source.posterizeValue;

            target.spriteCollection   = source.spriteCollection;
            target.premultipliedAlpha = source.premultipliedAlpha;

            CopyArray(ref target.altMaterials, source.altMaterials);
            CopyArray(ref target.atlasMaterials, source.atlasMaterials);
            CopyArray(ref target.atlasTextures, source.atlasTextures);
            CopyArray(ref target.atlasTextureFiles, source.atlasTextureFiles);
            CopyArray(ref target.atlasTexturePaths, source.atlasTexturePaths);

            CopyArray(ref target.atlasWidthTexture, source.atlasWidthTexture);
            CopyArray(ref target.atlasHeightTexture, source.atlasHeightTexture);

            target.sizeDef.CopyFrom(source.sizeDef);
            target.globalScale          = source.globalScale;
            target.globalTextureRescale = source.globalTextureRescale;
            target.physicsDepth         = source.physicsDepth;
            target.physicsEngine        = source.physicsEngine;
            target.disableTrimming      = source.disableTrimming;
            target.normalGenerationMode = source.normalGenerationMode;
            target.padAmount            = source.padAmount;
            target.innerPadAmount       = source.innerPadAmount;
            target.autoUpdate           = source.autoUpdate;
            target.editorDisplayScale   = source.editorDisplayScale;

            // Texture settings
            target.filterMode = source.filterMode;
            target.wrapMode   = source.wrapMode;
            target.userDefinedTextureSettings = source.userDefinedTextureSettings;
            target.mipmapEnabled    = source.mipmapEnabled;
            target.asyncLoadEnabled = source.asyncLoadEnabled;
            target.anisoLevel       = source.anisoLevel;
            target.forceA8          = source.forceA8;
        }
 public bool CompareTo(tk2dSpriteSheetSource src)
 {
     if (this.texture != src.texture)
     {
         return false;
     }
     if (this.tilesX != src.tilesX)
     {
         return false;
     }
     if (this.tilesY != src.tilesY)
     {
         return false;
     }
     if (this.numTiles != src.numTiles)
     {
         return false;
     }
     if (this.anchor != src.anchor)
     {
         return false;
     }
     if (this.pad != src.pad)
     {
         return false;
     }
     if (this.scale != src.scale)
     {
         return false;
     }
     if (this.colliderType != src.colliderType)
     {
         return false;
     }
     if (this.version != src.version)
     {
         return false;
     }
     if (this.active != src.active)
     {
         return false;
     }
     if (this.tileWidth != src.tileWidth)
     {
         return false;
     }
     if (this.tileHeight != src.tileHeight)
     {
         return false;
     }
     if (this.tileSpacingX != src.tileSpacingX)
     {
         return false;
     }
     if (this.tileSpacingY != src.tileSpacingY)
     {
         return false;
     }
     if (this.tileMarginX != src.tileMarginX)
     {
         return false;
     }
     if (this.tileMarginY != src.tileMarginY)
     {
         return false;
     }
     if (this.splitMethod != src.splitMethod)
     {
         return false;
     }
     return true;
 }
        public void CopyFromSource()
        {
            textureParams = new List <tk2dSpriteCollectionDefinition>(obj.textureParams.Length);
            foreach (var v in obj.textureParams)
            {
                if (v == null)
                {
                    textureParams.Add(null);
                }
                else
                {
                    var t = new tk2dSpriteCollectionDefinition();
                    t.CopyFrom(v);
                    textureParams.Add(t);
                }
            }

            textureRefs = new List <Texture2D>(obj.textureRefs.Length);
            foreach (var v in obj.textureRefs)
            {
                textureRefs.Add(v);
            }

            spriteSheets = new List <tk2dSpriteSheetSource>();
            if (obj.spriteSheets != null)
            {
                foreach (var v in obj.spriteSheets)
                {
                    if (v == null)
                    {
                        spriteSheets.Add(null);
                    }
                    else
                    {
                        var t = new tk2dSpriteSheetSource();
                        t.CopyFrom(v);
                        spriteSheets.Add(t);
                    }
                }
            }

            fonts = new List <tk2dSpriteCollectionFont>();
            if (obj.fonts != null)
            {
                foreach (var v in obj.fonts)
                {
                    if (v == null)
                    {
                        fonts.Add(null);
                    }
                    else
                    {
                        var t = new tk2dSpriteCollectionFont();
                        t.CopyFrom(v);
                        fonts.Add(t);
                    }
                }
            }

            UpgradeLegacySpriteSheets();

            var target = this;
            var source = obj;

            target.maxTextureSize      = source.maxTextureSize;
            target.forceTextureSize    = source.forceTextureSize;
            target.forcedTextureWidth  = source.forcedTextureWidth;
            target.forcedTextureHeight = source.forcedTextureHeight;

            target.textureCompression   = source.textureCompression;
            target.atlasWidth           = source.atlasWidth;
            target.atlasHeight          = source.atlasHeight;
            target.forceSquareAtlas     = source.forceSquareAtlas;
            target.atlasWastage         = source.atlasWastage;
            target.allowMultipleAtlases = source.allowMultipleAtlases;

            target.spriteCollection   = source.spriteCollection;
            target.premultipliedAlpha = source.premultipliedAlpha;

            CopyArray(ref target.altMaterials, source.altMaterials);
            CopyArray(ref target.atlasMaterials, source.atlasMaterials);
            CopyArray(ref target.atlasTextures, source.atlasTextures);

            target.useTk2dCamera            = source.useTk2dCamera;
            target.targetHeight             = source.targetHeight;
            target.targetOrthoSize          = source.targetOrthoSize;
            target.pixelPerfectPointSampled = source.pixelPerfectPointSampled;
            target.physicsDepth             = source.physicsDepth;
            target.disableTrimming          = source.disableTrimming;
            target.normalGenerationMode     = source.normalGenerationMode;
            target.padAmount          = source.padAmount;
            target.autoUpdate         = source.autoUpdate;
            target.editorDisplayScale = source.editorDisplayScale;
        }
 public void CopyFrom(tk2dSpriteSheetSource src)
 {
     texture = src.texture;
     tilesX = src.tilesX;
     tilesY = src.tilesY;
     numTiles = src.numTiles;
     anchor = src.anchor;
     pad = src.pad;
     scale = src.scale;
     colliderType = src.colliderType;
 }
    public bool CompareTo(tk2dSpriteSheetSource src)
    {
        if (texture != src.texture) return false;
        if (tilesX != src.tilesX) return false;
        if (tilesY != src.tilesY) return false;
        if (numTiles != src.numTiles) return false;
        if (anchor != src.anchor) return false;
        if (pad != src.pad) return false;
        if (scale != src.scale) return false;
        if (colliderType != src.colliderType) return false;

        return true;
    }
Example #29
0
    public bool CompareTo(tk2dSpriteSheetSource src)
    {
        if (texture != src.texture)
        {
            return(false);
        }
        if (tilesX != src.tilesX)
        {
            return(false);
        }
        if (tilesY != src.tilesY)
        {
            return(false);
        }
        if (numTiles != src.numTiles)
        {
            return(false);
        }
        if (anchor != src.anchor)
        {
            return(false);
        }
        if (pad != src.pad)
        {
            return(false);
        }
        if (scale != src.scale)
        {
            return(false);
        }
        if (colliderType != src.colliderType)
        {
            return(false);
        }
        if (version != src.version)
        {
            return(false);
        }

        if (active != src.active)
        {
            return(false);
        }
        if (tileWidth != src.tileWidth)
        {
            return(false);
        }
        if (tileHeight != src.tileHeight)
        {
            return(false);
        }
        if (tileSpacingX != src.tileSpacingX)
        {
            return(false);
        }
        if (tileSpacingY != src.tileSpacingY)
        {
            return(false);
        }
        if (tileMarginX != src.tileMarginX)
        {
            return(false);
        }
        if (tileMarginY != src.tileMarginY)
        {
            return(false);
        }
        if (splitMethod != src.splitMethod)
        {
            return(false);
        }

        return(true);
    }
        // Delete all sprites from a spritesheet
        public void DeleteSpriteSheet(tk2dSpriteSheetSource spriteSheet)
        {
            int index = GetSpriteSheetId(spriteSheet);

            for (int i = 0; i < textureParams.Count; ++i)
            {
                if (textureParams[i].hasSpriteSheetId && textureParams[i].spriteSheetId == index)
                {
                    textureRefs[i] = null;
                    textureParams[i] = new tk2dSpriteCollectionDefinition();
                }
            }

            spriteSheets[index] = new tk2dSpriteSheetSource();
            Trim();
        }
Example #31
0
 void GetTileCoordinateForSpriteSheet(tk2dSpriteSheetSource spriteSheet, int tileX, int tileY, out int coordX, out int coordY)
 {
     coordX = spriteSheet.tileMarginX + (spriteSheet.tileSpacingX + spriteSheet.tileWidth) * tileX;
     coordY = spriteSheet.tileMarginY + (spriteSheet.tileSpacingY + spriteSheet.tileHeight) * tileY;
 }
		void GetNumTilesForSpriteSheet(tk2dSpriteSheetSource spriteSheet, out int numTilesX, out int numTilesY)
		{
			var tex = spriteSheet.texture;
			numTilesX = (tex.width - spriteSheet.tileMarginX + spriteSheet.tileSpacingX) / (spriteSheet.tileSpacingX + spriteSheet.tileWidth);
			numTilesY = (tex.height - spriteSheet.tileMarginY + spriteSheet.tileSpacingY) / (spriteSheet.tileSpacingY + spriteSheet.tileHeight);
		}
Example #33
0
        void ProcessSpriteSelectionUI(tk2dSpriteSheetSource spriteSheet, Rect rect)
        {
            int spriteSheetId = SpriteCollection.GetSpriteSheetId(spriteSheet);

            if (rect.Contains(Event.current.mousePosition))
            {
                Vector2 localMousePos = (Event.current.mousePosition - new Vector2(rect.x, rect.y)) / zoomAmount;
                int     tileX = ((int)localMousePos.x - spriteSheet.tileMarginX) / (spriteSheet.tileSpacingX + spriteSheet.tileWidth);
                int     tileY = ((int)localMousePos.y - spriteSheet.tileMarginY) / (spriteSheet.tileSpacingY + spriteSheet.tileHeight);
                int     numTilesX, numTilesY;
                GetNumTilesForSpriteSheet(spriteSheet, out numTilesX, out numTilesY);

                if (Event.current.type == EventType.MouseDown)
                {
                    bool multiSelectKey = (Application.platform == RuntimePlatform.OSXEditor)?Event.current.command:Event.current.control;
                    if (tileX >= 0 && tileX < numTilesX && tileY >= 0 && tileY < numTilesY)
                    {
                        if (!multiSelectKey)
                        {
                            selectedSprites.Clear();
                        }

                        int id = FindSpriteSlotForSpriteSheetCell(SpriteCollection.GetSpriteSheetId(spriteSheet), tileX, tileY);
                        if (id != -1)
                        {
                            if (!multiSelectKey)
                            {
                                rectSelectX = tileX;
                                rectSelectY = tileY;
                            }

                            bool found = false;
                            foreach (var sel in selectedSprites)
                            {
                                if (sel.index == id)
                                {
                                    found = true;
                                    selectedSprites.Remove(sel);
                                    break;
                                }
                            }
                            if (!found)
                            {
                                var entry = new SpriteCollectionEditorEntry();
                                entry.index = id;
                                selectedSprites.Add(entry);
                            }
                            HandleUtility.Repaint();
                        }
                    }
                }
                else if (Event.current.type == EventType.MouseDrag)
                {
                    if (rectSelectX != -1 && rectSelectY != -1)
                    {
                        int x0 = Mathf.Min(tileX, rectSelectX);
                        int x1 = Mathf.Max(tileX, rectSelectX);
                        int y0 = Mathf.Min(tileY, rectSelectY);
                        int y1 = Mathf.Max(tileY, rectSelectY);
                        selectedSprites.Clear();
                        for (int y = y0; y <= y1; ++y)
                        {
                            for (int x = x0; x <= x1; ++x)
                            {
                                int id = FindSpriteSlotForSpriteSheetCell(spriteSheetId, x, y);
                                if (id != -1)
                                {
                                    var entry = new SpriteCollectionEditorEntry();
                                    entry.index = id;
                                    selectedSprites.Add(entry);
                                }
                            }
                        }
                        HandleUtility.Repaint();
                    }
                }
                else if (Event.current.type == EventType.MouseUp)
                {
                    rectSelectX = -1;
                    rectSelectY = -1;
                }
            }
        }
		void GetTileCoordinateForSpriteSheet(tk2dSpriteSheetSource spriteSheet, int tileX, int tileY, out int coordX, out int coordY)
		{
			coordX = spriteSheet.tileMarginX + (spriteSheet.tileSpacingX + spriteSheet.tileWidth) * tileX;
			coordY = spriteSheet.tileMarginY + (spriteSheet.tileSpacingY + spriteSheet.tileHeight) * tileY;
		}
Example #35
0
        public bool Draw(List <SpriteCollectionEditorEntry> selectedEntries)
        {
            if (selectedEntries.Count == 0 || selectedEntries[0].type != SpriteCollectionEditorEntry.Type.SpriteSheet)
            {
                return(false);
            }

            var entry       = selectedEntries[selectedEntries.Count - 1];
            var spriteSheet = SpriteCollection.spriteSheets[entry.index];

            if (activeSpriteSheetSource != spriteSheet)
            {
                // reset state data
                selectedSprites.Clear();
                activeSelectedSprites.Clear();
                rectSelectX             = rectSelectY = -1;
                textureViewScrollBar    = Vector2.zero;
                inspectorScrollBar      = Vector2.zero;
                activeSpriteSheetSource = spriteSheet;
                selectedMode            = EditMode.Edit;
            }

            if (spriteSheet.tileWidth == 0 || spriteSheet.tileHeight == 0)
            {
                selectedMode = EditMode.Config;
            }

            bool doDelete = false;

            GUILayout.BeginHorizontal();

            // Texture View
            GUILayout.BeginVertical(tk2dEditorSkin.SC_BodyBackground, GUILayout.ExpandHeight(true), GUILayout.ExpandWidth(true));
            textureViewScrollBar = GUILayout.BeginScrollView(textureViewScrollBar);
            if (spriteSheet.texture != null)
            {
                spriteSheet.texture.filterMode = FilterMode.Point;
                DrawTextureView(spriteSheet);
            }
            GUILayout.EndScrollView();
            GUILayout.EndVertical();

            // Inspector
            EditorGUIUtility.LookLikeControls(100.0f, 100.0f);
            inspectorScrollBar = GUILayout.BeginScrollView(inspectorScrollBar, GUILayout.ExpandHeight(true), GUILayout.Width(host.InspectorWidth));

            // Header
            GUILayout.BeginVertical(tk2dEditorSkin.SC_InspectorHeaderBG, GUILayout.ExpandWidth(true));
            GUILayout.Label("Sprite Sheet");
            GUILayout.BeginHorizontal();
            Texture2D newTexture = EditorGUILayout.ObjectField("Texture", spriteSheet.texture, typeof(Texture2D), false) as Texture2D;

            if (newTexture != spriteSheet.texture)
            {
                bool needFullRebuild = false;
                if (spriteSheet.texture == null)
                {
                    needFullRebuild = true;
                }

                spriteSheet.texture = newTexture;
                if (needFullRebuild)
                {
                    host.OnSpriteCollectionChanged(true);
                }
                else
                {
                    host.OnSpriteCollectionSortChanged();
                }
            }
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Delete", EditorStyles.miniButton))
            {
                doDelete = true;
            }
            GUILayout.EndHorizontal();
            GUILayout.EndVertical();

            bool textureReady = false;

            if (spriteSheet.texture != null)
            {
                GUILayout.BeginVertical(tk2dEditorSkin.SC_InspectorBG, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(false));
                string assetPath = AssetDatabase.GetAssetPath(spriteSheet.texture);
                if (assetPath.Length > 0)
                {
                    // make sure the source texture is npot and readable, and uncompressed
                    if (tk2dSpriteCollectionBuilder.IsTextureImporterSetUp(assetPath))
                    {
                        textureReady = true;
                    }
                    else
                    {
                        if (tk2dGuiUtility.InfoBoxWithButtons(
                                "The texture importer needs to be reconfigured to be used as a sprite sheet source. " +
                                "Please note that this will globally change this texture importer.",
                                tk2dGuiUtility.WarningLevel.Info,
                                "Set up") != -1)
                        {
                            tk2dSpriteCollectionBuilder.ConfigureSpriteTextureImporter(assetPath);
                            AssetDatabase.ImportAsset(assetPath);
                        }
                    }
                }
                GUILayout.EndVertical();
            }

            // Body
            if (textureReady)
            {
                GUILayout.BeginVertical(tk2dEditorSkin.SC_InspectorBG, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(false));
                selectedMode = (EditMode)GUILayout.Toolbar((int)selectedMode, new string[] { "Edit", "Config" });
                EditorGUILayout.Space();
                GUILayout.EndVertical();

                if (selectedMode == EditMode.Edit)
                {
                    if (Event.current.type == EventType.Layout)
                    {
                        activeSelectedSprites = new List <SpriteCollectionEditorEntry>(selectedSprites);
                    }

                    if (activeSelectedSprites.Count > 0)
                    {
                        host.SpriteView.DrawSpriteEditorInspector(activeSelectedSprites, true, true);
                    }
                }
                else
                {
                    GUILayout.BeginVertical(tk2dEditorSkin.SC_InspectorBG, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(false));

                    spriteSheet.tileWidth    = EditorGUILayout.IntField("Tile Width", spriteSheet.tileWidth);
                    spriteSheet.tileHeight   = EditorGUILayout.IntField("Tile Height", spriteSheet.tileHeight);
                    spriteSheet.tileMarginX  = EditorGUILayout.IntField("Tile Margin X", spriteSheet.tileMarginX);
                    spriteSheet.tileMarginY  = EditorGUILayout.IntField("Tile Margin Y", spriteSheet.tileMarginY);
                    spriteSheet.tileSpacingX = EditorGUILayout.IntField("Tile Spacing X", spriteSheet.tileSpacingX);
                    spriteSheet.tileSpacingY = EditorGUILayout.IntField("Tile Spacing Y", spriteSheet.tileSpacingY);

                    spriteSheet.pad = (tk2dSpriteCollectionDefinition.Pad)EditorGUILayout.EnumPopup("Pad", spriteSheet.pad);
                    if (spriteSheet.pad == tk2dSpriteCollectionDefinition.Pad.Default)
                    {
                        tk2dGuiUtility.InfoBox("The sprite sheet is configured to use default padding mode. " +
                                               "It is advised to select an explicit padding mode depending on the usage of the " +
                                               "sprites within the sprite sheet.\n\n" +
                                               "BlackZeroAlpha - Recommended for animations\n" +
                                               "Extend - Recommended for tilemaps", tk2dGuiUtility.WarningLevel.Warning);
                    }

                    // Apply button
                    GUILayout.Space(8);
                    GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace();
                    if (spriteSheet.texture != null &&
                        spriteSheet.tileWidth > 0 && spriteSheet.tileWidth <= spriteSheet.texture.width &&
                        spriteSheet.tileHeight > 0 && spriteSheet.tileHeight <= spriteSheet.texture.height &&
                        GUILayout.Button("Apply", EditorStyles.miniButton))
                    {
                        AddSprites(spriteSheet);
                        selectedMode = EditMode.Edit;
                    }
                    GUILayout.EndHorizontal();

                    GUILayout.EndVertical();
                }
            }

            GUILayout.BeginVertical(tk2dEditorSkin.SC_InspectorBG, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
            GUILayout.EndVertical();


            // /Body
            GUILayout.EndScrollView();

            // make dragable
            tk2dPreferences.inst.spriteCollectionInspectorWidth -= (int)tk2dGuiUtility.DragableHandle(4819284, GUILayoutUtility.GetLastRect(), 0, tk2dGuiUtility.DragDirection.Horizontal);

            GUILayout.EndHorizontal();

            if (doDelete)
            {
                string message = "Deleting a sprite sheet will delete all sprites sourced from this sprite sheet. " +
                                 "Are you sure you want to do this?";
                if (EditorUtility.DisplayDialog("Delete sprite sheet", message, "Yes", "No"))
                {
                    SpriteCollection.DeleteSpriteSheet(spriteSheet);
                    host.OnSpriteCollectionChanged(false);
                }
            }

            return(true);
        }
 public int FindOrCreateEmptySpriteSheetSlot()
 {
     for (int index = 0; index < spriteSheets.Count; ++index)
     {
         if (!spriteSheets[index].active)
         {
             spriteSheets[index].active = true;
             spriteSheets[index].version = tk2dSpriteSheetSource.CURRENT_VERSION;
             return index;
         }
     }
     var spriteSheet = new tk2dSpriteSheetSource();
     spriteSheet.active = true;
     spriteSheet.version = tk2dSpriteSheetSource.CURRENT_VERSION;
     spriteSheets.Add(spriteSheet);
     return spriteSheets.Count - 1;
 }