Skip to content

Commit

Permalink
Duplicate Quad, Settings, Hotkeys.
Browse files Browse the repository at this point in the history
Added the option to duplicate the selected quad.
Added settings for focus, duplication, hotkeys.
Added hotkeys, can be enabled in the settings file.
  • Loading branch information
maxartz15 committed Mar 31, 2019
1 parent 7f5e22b commit 82efcbb
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 6 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using MA_Editor;

namespace MA_TextureAtlasserPro
{
[System.Serializable]
public class MA_TextureAtlasserProSettings : ScriptableObject
{
[Header("Selection")]
public bool autoFocus = true;

[Header("Duplication:")]
public bool copySelectedQuadData = false;
public string duplicatedQuadNamePrefix = "new ";

[Header("Hotkeys:")]
public bool useHotkeys = false;
public KeyCode addQuadHotKey = KeyCode.Q;
public KeyCode removeQuadHotKey = KeyCode.R;
public KeyCode duplicateHotKey = KeyCode.D;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static class MA_TextureAtlasserProIcons
public static GUIContent exportAtlasIcon;
public static GUIContent createQuadIcon;
public static GUIContent removeQuadIcon;
public static GUIContent duplicateQuadIcon;
public static GUIContent showTexturesOnIcon;
public static GUIContent showTexturesOffIcon;
public static GUIContent dragHandleIcon;
Expand All @@ -24,6 +25,7 @@ public static void LoadIcons()
exportAtlasIcon = new GUIContent("", (Texture)EditorGUIUtility.Load(LOADICONPATH + "exportAtlasIcon" + ".png"));
createQuadIcon = new GUIContent("", (Texture)EditorGUIUtility.Load(LOADICONPATH + "createQuadIcon" + ".png"));
removeQuadIcon = new GUIContent("", (Texture)EditorGUIUtility.Load(LOADICONPATH + "removeQuadIcon" + ".png"));
duplicateQuadIcon = new GUIContent("", (Texture)EditorGUIUtility.Load(LOADICONPATH + "duplicateQuadIcon" + ".png"));
showTexturesOnIcon = new GUIContent("", (Texture)EditorGUIUtility.Load(LOADICONPATH + "showTexturesOnIcon" + ".png"));
showTexturesOffIcon = new GUIContent("", (Texture)EditorGUIUtility.Load(LOADICONPATH + "showTexturesOffIcon" + ".png"));
dragHandleIcon = new GUIContent("", (Texture)EditorGUIUtility.Load(LOADICONPATH + "dragHandleIcon" + ".png"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,43 @@ namespace MA_TextureAtlasserPro
{
public static class MA_TextureAtlasserProUtils
{
public const string SETTINGSASSETPATH = "Assets/MA_ToolBox/MA_TextureAtlasserPro/Settings/";
public const string SAVEASSETPATH = "Assets/MA_ToolBox/MA_TextureAtlasserPro/Atlasses/";
public const string LOADASSETPATH = "Assets/MA_ToolBox/MA_TextureAtlasserPro/Atlasses/";
public const string EXPORTASSETPATH = "Assets/MA_ToolBox/MA_TextureAtlasserPro/Exports/";
public const float VIEWOFFSET = 20;
public const string DEFAULTTEXTUREGROUPNAME = "Albedo";

public static MA_TextureAtlasserProSettings CreateSettings()
{
MA_TextureAtlasserProSettings _settings = (MA_TextureAtlasserProSettings)ScriptableObject.CreateInstance<MA_TextureAtlasserProSettings>();

if(_settings != null)
{
AssetDatabase.CreateAsset(_settings, SETTINGSASSETPATH + "MA_TextureAtlasserProSettings.asset");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();

return _settings;
}
else
{
return null;
}
}

public static MA_TextureAtlasserProSettings LoadSettings()
{
MA_TextureAtlasserProSettings _settings = AssetDatabase.LoadAssetAtPath(SETTINGSASSETPATH + "MA_TextureAtlasserProSettings.asset", typeof(MA_TextureAtlasserProSettings)) as MA_TextureAtlasserProSettings;

if (_settings == null)
{
_settings = CreateSettings();
}

return _settings;
}

public static MA_TextureAtlasserProAtlas CreateTextureAtlas(string name, Vector2 size)
{
MA_TextureAtlasserProAtlas _atlas = (MA_TextureAtlasserProAtlas)ScriptableObject.CreateInstance<MA_TextureAtlasserProAtlas>();
Expand Down Expand Up @@ -113,7 +144,7 @@ public static void MA_CheckTextureAtlas(MA_TextureAtlasserProAtlas atlas)
}
}

public static void CreateTextureQuad(MA_TextureAtlasserProAtlas atlas, string name, Rect rect)
public static void CreateTextureQuad(MA_TextureAtlasserProAtlas atlas, string name, Rect rect, bool focus = true)
{
if(atlas != null)
{
Expand All @@ -140,6 +171,11 @@ public static void CreateTextureQuad(MA_TextureAtlasserProAtlas atlas, string na
AssetDatabase.AddObjectToAsset(_quad, atlas);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();

if(focus)
{
atlas.selectedTextureQuad = atlas.textureQuads[atlas.textureQuads.Count - 1];
}
}
else
{
Expand All @@ -152,14 +188,41 @@ public static void CreateTextureQuad(MA_TextureAtlasserProAtlas atlas, string na
}
}

public static void RemoveTextureQuad(MA_TextureAtlasserProAtlas atlas)
public static void RemoveTextureQuad(MA_TextureAtlasserProAtlas atlas, bool focus = true)
{
if(atlas != null && atlas.selectedTextureQuad != null)
{
atlas.textureQuads.Remove(atlas.selectedTextureQuad);
int _index = atlas.textureQuads.IndexOf(atlas.selectedTextureQuad);

atlas.textureQuads.RemoveAt(_index);
GameObject.DestroyImmediate(atlas.selectedTextureQuad, true);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();

if (focus && atlas.textureQuads.Count > 0)
{
_index = Mathf.Clamp(_index, 0, atlas.textureQuads.Count - 1);
atlas.selectedTextureQuad = atlas.textureQuads[_index];
}
}
}

public static void DuplicateTextureQuad(MA_TextureAtlasserProAtlas atlas, bool focus = true, bool copyData = false, string namePrefix = "new ")
{
if(atlas != null && atlas.selectedTextureQuad != null)
{
CreateTextureQuad(atlas, namePrefix + atlas.selectedTextureQuad.name, atlas.selectedTextureQuad.rect);

if(copyData)
{
atlas.textureQuads[atlas.textureQuads.Count - 1].meshes = atlas.selectedTextureQuad.meshes;
atlas.textureQuads[atlas.textureQuads.Count - 1].textureGroups = atlas.selectedTextureQuad.textureGroups;
}

if(focus)
{
atlas.selectedTextureQuad = atlas.textureQuads[atlas.textureQuads.Count - 1];
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@ public override void UpdateView(Event e, Rect editorViewRect)
GUILayout.Space(MA_TextureAtlasserProUtils.VIEWOFFSET);
if(GUILayout.Button(MA_TextureAtlasserProIcons.createQuadIcon, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true)))
{
MA_TextureAtlasserProUtils.CreateTextureQuad(curWindow.textureAtlas, "new Quad", new Rect(0, 0, 128, 128));
MA_TextureAtlasserProUtils.CreateTextureQuad(curWindow.textureAtlas, "new Quad", new Rect(0, 0, 128, 128), curWindow.settings.autoFocus);
}
if(curWindow.textureAtlas.selectedTextureQuad != null && GUILayout.Button(MA_TextureAtlasserProIcons.removeQuadIcon, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true)))
{
if(curWindow.textureAtlas.selectedTextureQuad != null)
MA_TextureAtlasserProUtils.RemoveTextureQuad(curWindow.textureAtlas);
MA_TextureAtlasserProUtils.RemoveTextureQuad(curWindow.textureAtlas, curWindow.settings.autoFocus);
}
if (curWindow.textureAtlas.selectedTextureQuad != null && GUILayout.Button(MA_TextureAtlasserProIcons.duplicateQuadIcon, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true)))
{
if (curWindow.textureAtlas.selectedTextureQuad != null)
MA_TextureAtlasserProUtils.DuplicateTextureQuad(curWindow.textureAtlas, curWindow.settings.autoFocus, curWindow.settings.copySelectedQuadData, curWindow.settings.duplicatedQuadNamePrefix);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ protected override void ProcessEvents(Event e, Rect editorViewRect)

e.Use();
}

//Hotkeys.
if (curWindow.settings.useHotkeys)
{
if (e.type == EventType.KeyDown && e.keyCode == curWindow.settings.addQuadHotKey)
{
MA_TextureAtlasserProUtils.CreateTextureQuad(curWindow.textureAtlas, "new Quad", new Rect(0, 0, 128, 128), curWindow.settings.autoFocus);
e.Use();
}

if (curWindow.textureAtlas.selectedTextureQuad != null)
{
if (e.type == EventType.KeyDown && e.keyCode == curWindow.settings.removeQuadHotKey)
{
MA_TextureAtlasserProUtils.RemoveTextureQuad(curWindow.textureAtlas, curWindow.settings.autoFocus);
e.Use();
}

if (e.type == EventType.KeyDown && e.keyCode == curWindow.settings.duplicateHotKey)
{
MA_TextureAtlasserProUtils.DuplicateTextureQuad(curWindow.textureAtlas, curWindow.settings.autoFocus);
e.Use();
}
}
}
}

private Vector2 ConvertScreenCoordsToZoomCoords(Vector2 screenCoords)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace MA_TextureAtlasserPro
public class MA_TextureAtlasserProWindow : EditorWindow
{
public static MA_TextureAtlasserProWindow thisWindow;
public MA_TextureAtlasserProSettings settings;
public MA_TextureAtlasserProAtlas textureAtlas;

public MA_TextureAtlasserProWorkView workView;
Expand Down Expand Up @@ -56,6 +57,7 @@ private static void CreateViews()
GetCurrentWindow();
}

thisWindow.settings = MA_TextureAtlasserProUtils.LoadSettings();
thisWindow.workView = new MA_TextureAtlasserProWorkView(thisWindow, "workView");
thisWindow.menuView = new MA_TextureAtlasserProMenuView(thisWindow, "menuView");
thisWindow.inspectorView = new MA_TextureAtlasserProInspectorView(thisWindow, "inspectorView");
Expand Down Expand Up @@ -86,7 +88,7 @@ private void OnGUI()
}

//Check views
if(workView == null || menuView == null || inspectorView == null || debugView == null)
if(settings == null || workView == null || menuView == null || inspectorView == null || debugView == null)
{
CreateViews();
return;
Expand Down
1 change: 1 addition & 0 deletions MA_ToolBox/MA_TextureAtlasserPro/Settings/Settings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Settigns are supposed to be here.

0 comments on commit 82efcbb

Please sign in to comment.