11using 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}
0 commit comments