Skip to content

Commit

Permalink
Teams
Browse files Browse the repository at this point in the history
- Teams added to database.
- Teams script.
- Team editor.
- Window for teams.
  • Loading branch information
WyrnCael committed Apr 3, 2018
1 parent e11879d commit 50e93b6
Show file tree
Hide file tree
Showing 14 changed files with 439 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Assets/TRPGMaker/Database/Database.asset
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ MonoBehaviour:
- {fileID: 11400000, guid: 75942776545ea02418c221a9273f406b, type: 2}
- {fileID: 11400000, guid: 496024b31fb8d604ba8b12317f0d4762, type: 2}
- {fileID: 11400000, guid: bac31a58c9f786c4a9d5b67034ba5482, type: 2}
teams:
- {fileID: 11400000, guid: c7870a66a01c2a54e90fa9d468b19807, type: 2}
10 changes: 10 additions & 0 deletions Assets/TRPGMaker/Database/Teams.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Assets/TRPGMaker/Database/Teams/NewTeam.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4a7b1fcdece9778478b2575993f98a72, type: 3}
m_Name: NewTeam
m_EditorClassIdentifier:
name: NewTeam
id: 0
characters:
- {fileID: 11400000, guid: caa5b8cc4b5205b4297289922d3b7e33, type: 2}
- {fileID: 11400000, guid: 75942776545ea02418c221a9273f406b, type: 2}
10 changes: 10 additions & 0 deletions Assets/TRPGMaker/Database/Teams/NewTeam.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions Assets/TRPGMaker/Editor/TeamEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

[CustomEditor(typeof(Team))]
public class TeamEditor : Editor {

private ReorderableList listCharacters;
private Vector2 scrollPosition;

private void OnEnable()
{

// Get characters
listCharacters = new ReorderableList(serializedObject,
serializedObject.FindProperty("characters"),
true, true, true, true);

// Draw characters
listCharacters.drawElementCallback =
(Rect rect, int index, bool isActive, bool isFocused) => {
var element = listCharacters.serializedProperty.GetArrayElementAtIndex(index);
Character character = element.objectReferenceValue as Character;

rect.y += 2;
EditorGUI.LabelField(
new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight),
character.name);
};

// Characters header
listCharacters.drawHeaderCallback = (Rect rect) => {
EditorGUI.LabelField(rect, "Characters");
};

// Add character
listCharacters.onAddDropdownCallback = (Rect buttonRect, ReorderableList l) => {
var menu = new GenericMenu();
foreach (Character character in Database.Instance.characters)
{
menu.AddItem(new GUIContent(character.name),
false, clickHandlerCharacters,
character);
}
menu.ShowAsContext();
};
}

public override void OnInspectorGUI()
{
serializedObject.Update();

var customStyle = new GUIStyle();
customStyle.alignment = TextAnchor.UpperCenter;
customStyle.fontSize = 17;
GUI.Label(new Rect(EditorGUILayout.GetControlRect().x, EditorGUILayout.GetControlRect().y, EditorGUILayout.GetControlRect().width, 30), "Editing \"" + ((Team)target).name + "\" team:", customStyle);

EditorGUILayout.BeginVertical();
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);

EditorGUILayout.PropertyField(serializedObject.FindProperty("id"), new GUIContent("Id: "), GUILayout.MinWidth(100));
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(serializedObject.FindProperty("name"), new GUIContent("Name: "), GUILayout.MinWidth(100));
if (EditorGUI.EndChangeCheck())
{
AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath((SpecializedClass)target), ((Team)target).name + ".asset");
}
listCharacters.DoLayoutList();
serializedObject.ApplyModifiedProperties();

EditorGUILayout.EndScrollView();
EditorGUILayout.EndVertical();
}

private void clickHandlerCharacters(object target)
{
var data = (Character)target;
((Team)this.target).characters.Add(data);
serializedObject.ApplyModifiedProperties();
}
}
13 changes: 13 additions & 0 deletions Assets/TRPGMaker/Editor/TeamEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions Assets/TRPGMaker/Editor/Windows/Database/DatabaseWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class DatabaseWindow : EditorWindow
private LayoutWindow itemWindow;
private LayoutWindow specilizedWindow;
private LayoutWindow characterWindow;
private LayoutWindow teamsWindow;

[MenuItem("TRPGMaker/Database editor", false, 0)]
public static void ShowWindow()
Expand All @@ -39,7 +40,8 @@ public enum MenuOptions
SLOT_TYPE,
ITEMS,
SPECIALIZED_CLASS,
CHARACTERS
CHARACTERS,
TEAMS
};

public void createMenuLayouts()
Expand All @@ -64,6 +66,9 @@ public void createMenuLayouts()

characterWindow = (CharacterWindow)ScriptableObject.CreateInstance(typeof(CharacterWindow));
characterWindow.Init();

teamsWindow = (TeamsWindow)ScriptableObject.CreateInstance(typeof(TeamsWindow));
teamsWindow.Init();
}

void OnGUI()
Expand Down Expand Up @@ -105,6 +110,9 @@ void OnGUI()
case MenuOptions.CHARACTERS:
rightWindow = characterWindow;
break;
case MenuOptions.TEAMS:
rightWindow = teamsWindow;
break;
default:
break;
}
Expand Down Expand Up @@ -155,7 +163,13 @@ void DrawLeftMenu()
if (menuOption != MenuOptions.CHARACTERS)
rightWindow.selected = false;
menuOption = MenuOptions.CHARACTERS;
}
}
if (teamsWindow.Button(windowArea))
{
if (menuOption != MenuOptions.TEAMS)
rightWindow.selected = false;
menuOption = MenuOptions.TEAMS;
}
}

private void OnDestroy()
Expand Down
171 changes: 171 additions & 0 deletions Assets/TRPGMaker/Editor/Windows/Database/TeamsWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

public class TeamsWindow : LayoutWindow {
private ReorderableList listTeams;
private Editor buttonEditor;
private Vector2 scrollPosition;

public override void Init()
{
createButtonReorderableList();
}

public override void Draw(Rect rect)
{
if (editor == null)
DrawMainView();
else
editor.OnInspectorGUI();
}

public void DrawMainView()
{
var customStyle = new GUIStyle();
customStyle.alignment = TextAnchor.UpperCenter;
customStyle.fontSize = 17;
GUI.Label(new Rect(EditorGUILayout.GetControlRect().x, EditorGUILayout.GetControlRect().y, EditorGUILayout.GetControlRect().width, 30), "List of teams:", customStyle);

// Create color for each line
GUIStyle gsLinePair = new GUIStyle();
gsLinePair.normal.background = MakeTextureColor.MakeTexture(600, 1, new Color(0.5f, 0.5f, 0.5f, 0.5f));
GUIStyle gsLineOdd = new GUIStyle();
gsLineOdd.normal.background = MakeTextureColor.MakeTexture(600, 1, new Color(0.5f, 0.5f, 0.5f, 0.0f));

EditorGUILayout.BeginVertical();
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);

for (int i = 0; i < Database.Instance.teams.Count; i++)
{
Team team = Database.Instance.teams[i];

// Changing line color
if (i % 2 == 0)
GUILayout.BeginHorizontal(gsLinePair);
else
GUILayout.BeginHorizontal(gsLineOdd);
Rect rect = EditorGUILayout.GetControlRect();
GUI.Label(rect, team.id + ": " + team.name);
if (GUILayout.Button(new GUIContent("Edit"), GUILayout.Width(50)))
{
editor = Editor.CreateEditor(team);
}
else if (GUILayout.Button(new GUIContent("Remove"), GUILayout.Width(90)))
{
removeTeam(team);
}
GUILayout.EndHorizontal();
}

EditorGUILayout.EndScrollView();
EditorGUILayout.EndVertical();
}

public override bool Button(Rect rect)
{
var teamTexture = (Texture2D)Resources.Load("Menu/Buttons/teams", typeof(Texture2D));
GUIStyle myStyle = new GUIStyle("Button");
myStyle.padding = new RectOffset(0, 0, 10, 10);

if (GUILayout.Button(new GUIContent("Teams", teamTexture), myStyle))
{
editor = null;
Draw(rect);
selected = true;
}

if (selected)
DrawButton();

return selected;
}

private void DrawButton()
{
buttonEditor.serializedObject.Update();

listTeams.DoLayoutList();

buttonEditor.serializedObject.ApplyModifiedProperties();
}

private void createButtonReorderableList()
{
buttonEditor = Editor.CreateEditor(Database.Instance);

// Get teams
listTeams = new ReorderableList(buttonEditor.serializedObject,
buttonEditor.serializedObject.FindProperty("teams"),
true, false, true, true);

// Draw teams
listTeams.drawElementCallback =
(Rect rect, int index, bool isActive, bool isFocused) => {
var element = listTeams.serializedProperty.GetArrayElementAtIndex(index);
var team = element.objectReferenceValue as Team;
rect.y += 2;
EditorGUI.LabelField(rect, team.id + ": " + team.name);
};

// On select team
listTeams.onSelectCallback = (ReorderableList l) => {
var team = l.serializedProperty.GetArrayElementAtIndex(l.index).objectReferenceValue as Team;
editor = Editor.CreateEditor(team);
};

// On new team
listTeams.onAddDropdownCallback = (Rect buttonRect, ReorderableList l) => {
Team team = (Team)ScriptableObject.CreateInstance(typeof(Team));

var _exists = AssetDatabase.LoadAssetAtPath("Assets/TRPGMaker/Database/Teams/NewTeam.asset", typeof(Team));
if (_exists == null)
{
team.name = "NewTeam";
AssetDatabase.CreateAsset(team, "Assets/TRPGMaker/Database/Teams/NewTeam.asset");
}
else
{
string[] existAssets = AssetDatabase.FindAssets("NewTeam");
bool seted = false;
int i = 0;
while (i <= existAssets.Length && !seted)
{
var _existsNumber = AssetDatabase.LoadAssetAtPath("Assets/TRPGMaker/Database/Teams/NewTeam(" + (i + 1) + ").asset", typeof(Team));
if (_existsNumber == null)
{
team.name = "NewTeam(" + (i + 1) + ")";
AssetDatabase.CreateAsset(team, "Assets/TRPGMaker/Database/Teams/NewTeam(" + (i + 1) + ").asset");
seted = true;
}
i++;
}
}

editor = Editor.CreateEditor(team);
Database.Instance.teams.Add(team);
listTeams.index = Database.Instance.teams.Count - 1;
DrawButton();
};

// On remove team
listTeams.onRemoveCallback = (ReorderableList l) => {
var team = l.serializedProperty.GetArrayElementAtIndex(l.index).objectReferenceValue as Team;
removeTeam(team);
editor = null;
DrawMainView();
DrawButton();
};

// No header
listTeams.headerHeight = 0;
}

private void removeTeam(Team team)
{
Database.Instance.teams.Remove(team);
AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(team));
}
}
13 changes: 13 additions & 0 deletions Assets/TRPGMaker/Editor/Windows/Database/TeamsWindow.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/TRPGMaker/Resources/Menu/Buttons/teams.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 50e93b6

Please sign in to comment.