Skip to content

Commit e5e0dc3

Browse files
authored
Merge pull request #1 from OpenSourceUnityPackage/feature/clean
Feature/clean
2 parents c1f0e14 + f3f9d14 commit e5e0dc3

File tree

13 files changed

+294
-491
lines changed

13 files changed

+294
-491
lines changed

Runtime/ContextualMenu.cs

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,93 @@
11
using System.Collections.Generic;
2-
using UnityEngine;
32

4-
namespace UnitSelectionPackage
3+
namespace ContextualMenuPackage
54
{
6-
public class ContextualMenu<T> where T : MonoBehaviour, IContextualizable
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 class ContextualMenu<T> where T : IContextualizable
715
{
8-
protected List<T> m_observedSelectables; // ref
16+
protected List<T> m_contextualizable; // ref
917

1018
protected Dictionary<string, ITask<T>> m_tasks = new Dictionary<string, ITask<T>>();
1119
protected Dictionary<string, List<T>> m_taskOwned = new Dictionary<string, List<T>>();
1220

21+
/// <summary>
22+
/// Use this function to add task associate to it's id.
23+
/// When the ID is invoked, this task is called
24+
/// </summary>
25+
/// <param name="id"></param>
26+
/// <param name="task"></param>
1327
public void AddTask(string id, ITask<T> task)
1428
{
1529
m_tasks.Add(id, task);
1630
}
1731

32+
/// <summary>
33+
/// Allow to invoke task thanks to it's ID.
34+
/// Task will receive contextualizable that also own this ID.
35+
/// </summary>
36+
/// <param name="taskId"></param>
1837
public void InvokeTask(string taskId)
1938
{
20-
if (m_tasks.ContainsKey(taskId))
39+
if (m_tasks.ContainsKey(taskId) && m_taskOwned.ContainsKey(taskId))
2140
{
22-
m_tasks[taskId].OnSelect(m_taskOwned[taskId]);
41+
m_tasks[taskId].OnInvoked(m_taskOwned[taskId]);
2342
}
2443
}
2544

45+
/// <summary>
46+
/// Call this function to get list of task's IDs callable based on contextualizables task.
47+
/// </summary>
48+
/// <returns></returns>
2649
public string[] GetTasks()
2750
{
2851
string[] str = new string[m_taskOwned.Count];
2952
m_taskOwned.Keys.CopyTo(str, 0);
3053
return str;
3154
}
3255

33-
void SortActions()
56+
/// <summary>
57+
/// Set contextualizables to managed contextual menu base on it.
58+
/// </summary>
59+
/// <param name="contextualizables"></param>
60+
public void SetContextualizable(in List<T> contextualizables)
61+
{
62+
m_contextualizable = contextualizables;
63+
SortTasks();
64+
}
65+
66+
/// <summary>
67+
/// Clean the contextualizable buffer to remove all possible action possible.
68+
/// </summary>
69+
public void CleanContextualizable()
3470
{
3571
m_taskOwned.Clear();
36-
foreach (T observecSelectable in m_observedSelectables)
72+
}
73+
74+
/// <summary>
75+
/// Sort action based on contextualizables.
76+
/// </summary>
77+
protected void SortTasks()
78+
{
79+
CleanContextualizable();
80+
81+
foreach (T observedSelectable in m_contextualizable)
3782
{
38-
foreach (string task in observecSelectable.GetTasks())
83+
foreach (string task in observedSelectable.GetTasks())
3984
{
4085
if (!m_taskOwned.ContainsKey(task))
4186
m_taskOwned.Add(task, new List<T>());
4287

43-
m_taskOwned[task].Add(observecSelectable);
88+
m_taskOwned[task].Add(observedSelectable);
4489
}
4590
}
4691
}
47-
48-
public void SetObserver(in List<T> observedSelectable)
49-
{
50-
m_observedSelectables = observedSelectable;
51-
SortActions();
52-
}
5392
}
5493
}

Runtime/IContextualizable.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
using System;
21
using System.Collections.Generic;
3-
using UnityEngine;
42

5-
public interface IContextualizable
3+
namespace ContextualMenuPackage
64
{
7-
List<string> GetTasks();
5+
/// <summary>
6+
/// IContextualizable is an interface that allow you to define which task your class can process.
7+
/// Inherit from IContextualizable your class to access to Contextual menu feature.
8+
/// </summary>
9+
public interface IContextualizable
10+
{
11+
/// <summary>
12+
/// Get the identifier of the tasks that the class it inherits can handle.
13+
/// </summary>
14+
/// <returns></returns>
15+
public List<string> GetTasks();
16+
}
817
}

Runtime/ITask.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
using System.Collections.Generic;
2-
using UnityEngine;
32

4-
namespace UnitSelectionPackage
3+
namespace ContextualMenuPackage
54
{
6-
public interface ITask<T> where T : MonoBehaviour, IContextualizable
5+
/// <summary>
6+
/// Inherit from ITask to define task that contextualizable can handle
7+
/// </summary>
8+
/// <typeparam name="T"></typeparam>
9+
public interface ITask<T> where T : IContextualizable
710
{
8-
public void OnSelect(List<T> targets);
11+
/// <summary>
12+
/// Invoke task with list of contextualizable that own it
13+
/// </summary>
14+
/// <param name="contextualizables"></param>
15+
public void OnInvoked(List<T> contextualizables);
916
}
1017
}

Samples.meta

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

0 commit comments

Comments
 (0)