Skip to content

Commit 76b3994

Browse files
authored
Merge pull request #2 from OpenSourceUnityPackage/feature/clean
Feature/clean
2 parents 3659402 + 3a64b94 commit 76b3994

File tree

8 files changed

+194
-104
lines changed

8 files changed

+194
-104
lines changed

Runtime/AContextualMenu.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Collections.Generic;
2+
3+
namespace ContextualMenuPackage
4+
{
5+
/// <summary>
6+
/// ContextualMenu is a class that allow you to manage task depending on action available by the generic type.
7+
/// If you combine it with your own UI system and selection, you can process Contextual menu like an RTS.
8+
/// Architecture of this class is made to be independent as possible and online friendly.
9+
/// </summary>
10+
/// <typeparam name="T">
11+
/// Contextualizable that represent object that you will be stored on ContextualMenu and process tasks.
12+
/// This object need to inheritance of interface IContextualizable
13+
/// </typeparam>
14+
public abstract class AContextualMenu<T> where T : IContextualizable
15+
{
16+
protected List<T> m_contextualizable; // ref
17+
protected readonly Dictionary<string, ITask<T>> m_tasks = new Dictionary<string, ITask<T>>();
18+
19+
/// <summary>
20+
/// Use this function to add task associate to its ID.
21+
/// When the ID is invoked, this task is called
22+
/// </summary>
23+
/// <param name="id"></param>
24+
/// <param name="task"></param>
25+
public virtual void AddTask(string id, ITask<T> task)
26+
{
27+
m_tasks.Add(id, task);
28+
}
29+
30+
/// <summary>
31+
/// Allow to invoke task thanks to it's ID.
32+
/// Task will receive contextualizable that also own this ID.
33+
/// </summary>
34+
/// <param name="taskId"></param>
35+
public abstract void InvokeTask(string taskId);
36+
37+
/// <summary>
38+
/// Call this function to get list of task's IDs callable based on contextualizables task.
39+
/// </summary>
40+
/// <returns></returns>
41+
public abstract string[] GetTasks();
42+
43+
/// <summary>
44+
/// Set contextualizables to managed contextual menu.
45+
/// </summary>
46+
/// <param name="contextualizables">null to clear</param>
47+
public virtual void SetContextualizable(in List<T> contextualizables)
48+
{
49+
m_contextualizable = contextualizables;
50+
}
51+
}
52+
}

Runtime/ContextualMenu.cs

Lines changed: 0 additions & 93 deletions
This file was deleted.

Runtime/GlobalContextualMenu.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Collections.Generic;
2+
3+
namespace ContextualMenuPackage
4+
{
5+
/// <summary>
6+
/// GlobalContextualMenu handles all task of contextualizable even if they are not shared by all.
7+
/// </summary>
8+
/// <typeparam name="T"></typeparam>
9+
public sealed class GlobalContextualMenu<T> : AContextualMenu<T> where T : IContextualizable
10+
{
11+
private readonly Dictionary<string, List<T>> m_taskOwned = new Dictionary<string, List<T>>();
12+
13+
public override void InvokeTask(string taskId)
14+
{
15+
if (m_tasks.ContainsKey(taskId) && m_taskOwned.ContainsKey(taskId))
16+
{
17+
m_tasks[taskId].OnInvoked(m_taskOwned[taskId]);
18+
}
19+
}
20+
21+
public override string[] GetTasks()
22+
{
23+
string[] str = new string[m_taskOwned.Count];
24+
m_taskOwned.Keys.CopyTo(str, 0);
25+
return str;
26+
}
27+
28+
public override void SetContextualizable(in List<T> contextualizables)
29+
{
30+
base.SetContextualizable(contextualizables);
31+
SortTasks();
32+
}
33+
34+
private void SortTasks()
35+
{
36+
m_taskOwned.Clear();
37+
38+
if (m_contextualizable == null)
39+
return;
40+
foreach (T observedSelectable in m_contextualizable)
41+
{
42+
List<string> tasks = observedSelectable.GetTasks();
43+
foreach (string task in tasks)
44+
{
45+
if (!m_taskOwned.ContainsKey(task))
46+
m_taskOwned.Add(task, new List<T>());
47+
48+
m_taskOwned[task].Add(observedSelectable);
49+
}
50+
}
51+
}
52+
}
53+
}

Runtime/GlobalContextualMenu.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/SharedContextualMenu.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Collections.Generic;
2+
3+
namespace ContextualMenuPackage
4+
{
5+
/// <summary>
6+
/// SharedContextualMenu only handles the shared task of contextualizables
7+
/// </summary>
8+
/// <typeparam name="T"></typeparam>
9+
public class SharedContextualMenu<T> : AContextualMenu<T> where T : IContextualizable
10+
{
11+
private readonly List<string> m_sharedTask = new List<string>();
12+
13+
public override void InvokeTask(string taskId)
14+
{
15+
if (m_tasks.ContainsKey(taskId) && m_sharedTask.Contains(taskId))
16+
{
17+
m_tasks[taskId].OnInvoked(m_contextualizable);
18+
}
19+
}
20+
21+
public override string[] GetTasks()
22+
{
23+
return m_sharedTask.ToArray();
24+
}
25+
26+
public override void SetContextualizable(in List<T> contextualizables)
27+
{
28+
base.SetContextualizable(contextualizables);
29+
SortGlobalTasks();
30+
}
31+
32+
void SortGlobalTasks()
33+
{
34+
m_sharedTask.Clear();
35+
if (m_contextualizable == null || m_contextualizable.Count == 0)
36+
return;
37+
38+
foreach (KeyValuePair<string, ITask<T>> task in m_tasks)
39+
{
40+
m_sharedTask.Add(task.Key);
41+
}
42+
43+
foreach (T observedSelectable in m_contextualizable)
44+
{
45+
List<string> tasks = observedSelectable.GetTasks();
46+
for (int index = 0; index < m_sharedTask.Count;)
47+
{
48+
string sharedTask = m_sharedTask[index];
49+
50+
if (!tasks.Contains(sharedTask))
51+
m_sharedTask.RemoveAt(index);
52+
else
53+
++index;
54+
}
55+
}
56+
57+
}
58+
}
59+
}

Runtime/SharedContextualMenu.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Samples~/Demo/Scripts/GameManager.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,22 @@ public enum ETeam : int
1515

1616
public class GameManager : MonoBehaviour
1717
{
18+
private SharedContextualMenu<Unit> m_contextualMenu = new SharedContextualMenu<Unit>();
19+
20+
// Uncomment to test GlobalContextMenu
21+
//private GlobalContextualMenu<Unit> m_contextualMenu = new GlobalContextualMenu<Unit>();
22+
private List<Unit>[] m_teamsUnits = new List<Unit>[(int) ETeam.TeamCount];
23+
1824
public Camera mainCamera;
1925
private UnitSelection<Unit> m_unitSelection = new UnitSelection<Unit>();
20-
private ContextualMenu<Unit> m_contextualMenu = new ContextualMenu<Unit>();
21-
private List<Unit>[] m_teamsUnits = new List<Unit>[(int) ETeam.TeamCount];
2226
private bool m_isSelecting;
2327
private EventSystem m_eventSystem;
24-
25-
[System.Serializable]
26-
public struct TaskUIElement
27-
{
28-
public string task;
29-
public Button button;
30-
}
31-
28+
private int m_layerGround;
29+
3230
public Toggle btnMove;
3331
public Button btnStop;
3432

3533
public Action<Vector3> RequestPosition { get; set; }
36-
public int m_layerGround;
3734

3835
#region Singleton
3936

0 commit comments

Comments
 (0)