Skip to content

Commit 9f273e5

Browse files
committed
compatible with both keyboard and gamepad.
1 parent 040f5f5 commit 9f273e5

File tree

5 files changed

+165
-37
lines changed

5 files changed

+165
-37
lines changed

Assets/JCSUnity/Scenes/Demo/GUI/JCS_ButtonSelectionGroup.unity

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,13 @@ MonoBehaviour:
514514
m_Script: {fileID: 11500000, guid: b5a77e45332b60c47aa1d7f36872a539, type: 3}
515515
m_Name:
516516
m_EditorClassIdentifier:
517+
mDeactiveAtAwake: 1
517518
mButton: {fileID: 318557157}
519+
mSelectedEvent:
520+
m_PersistentCalls:
521+
m_Calls: []
522+
m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine, Version=0.0.0.0, Culture=neutral,
523+
PublicKeyToken=null
518524
mEffects:
519525
- {fileID: 196707934}
520526
--- !u!114 &318557154
@@ -1753,7 +1759,13 @@ MonoBehaviour:
17531759
m_Script: {fileID: 11500000, guid: b5a77e45332b60c47aa1d7f36872a539, type: 3}
17541760
m_Name:
17551761
m_EditorClassIdentifier:
1762+
mDeactiveAtAwake: 1
17561763
mButton: {fileID: 1201376773}
1764+
mSelectedEvent:
1765+
m_PersistentCalls:
1766+
m_Calls: []
1767+
m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine, Version=0.0.0.0, Culture=neutral,
1768+
PublicKeyToken=null
17571769
mEffects:
17581770
- {fileID: 1684827591}
17591771
--- !u!114 &1201376770
@@ -1974,6 +1986,7 @@ MonoBehaviour:
19741986
m_Script: {fileID: 11500000, guid: f6fd8a250f9136147a1d26644433f2ac, type: 3}
19751987
m_Name:
19761988
m_EditorClassIdentifier:
1989+
mActive: 1
19771990
mKeyActionType: 1
19781991
mMNext: 275
19791992
mMPrev: 276
@@ -2199,7 +2212,13 @@ MonoBehaviour:
21992212
m_Script: {fileID: 11500000, guid: b5a77e45332b60c47aa1d7f36872a539, type: 3}
22002213
m_Name:
22012214
m_EditorClassIdentifier:
2215+
mDeactiveAtAwake: 1
22022216
mButton: {fileID: 2006004933}
2217+
mSelectedEvent:
2218+
m_PersistentCalls:
2219+
m_Calls: []
2220+
m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine, Version=0.0.0.0, Culture=neutral,
2221+
PublicKeyToken=null
22032222
mEffects:
22042223
- {fileID: 912788094}
22052224
--- !u!114 &2006004930

Assets/JCSUnity/Scripts/GUI/JCS_Button.cs

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,33 @@ public abstract class JCS_Button
2626
/*******************************************/
2727
/* Public Variables */
2828
/*******************************************/
29+
// JCSUnity framework only callback, do not override this callback.
30+
public CallBackFunc btnSystemCallBack = null;
31+
public CallBackFuncBtn btnSystemCallBackBtn = null;
32+
// for user's callback.
33+
public CallBackFunc btnCallBack = null;
34+
public CallBackFuncBtn btnCallBackBtn = null;
2935

3036
/*******************************************/
3137
/* Private Variables */
3238
/*******************************************/
3339
public delegate void CallBackFunc();
3440
public delegate void CallBackFuncBtn(JCS_Button btn);
3541

36-
// JCSUnity framework only callback, do not override this callback.
37-
private CallBackFunc mBtnSystemCallBack = null;
38-
private CallBackFuncBtn mBtnSystemCallBackBtn = null;
39-
// for user's callback.
40-
protected CallBackFunc mBtnCallBack = null;
41-
private CallBackFuncBtn mBtnCallBackBtn = null;
42-
43-
4442
[Header("** Optional Variables (JCS_Button) **")]
4543

4644
[Tooltip("text under the button, no necessary.")]
4745
[SerializeField]
4846
protected Text mButtonText = null;
4947

48+
[Tooltip("Button Selection for if the button that are in the group.")]
49+
[SerializeField]
50+
protected JCS_ButtonSelection mButtonSelection = null;
51+
52+
// record down if selected in the group. work with
53+
// 'JCS_ButtonSelectionGroup' and 'JCS_ButtonSelection'.
54+
protected bool mIsSelectedInGroup = false;
55+
5056

5157
[Header("** Initialize Variables (JCS_Button) **")]
5258

@@ -84,10 +90,6 @@ public abstract class JCS_Button
8490
public Image Image { get { return this.mImage; } }
8591
public RectTransform GetRectTransfom() { return this.mRectTransform; }
8692
public int DialogueIndex { get { return this.mDialogueIndex; } set { this.mDialogueIndex = value; } }
87-
public void SetCallback(CallBackFunc func) { this.mBtnCallBack += func; }
88-
public void SetCallback(CallBackFuncBtn func) { this.mBtnCallBackBtn += func; }
89-
public void SetSystemCallback(CallBackFunc func) { this.mBtnSystemCallBack += func; }
90-
public void SetSystemCallback(CallBackFuncBtn func) { this.mBtnSystemCallBackBtn += func; }
9193
public bool AutoListener { get { return this.mAutoListener; } set { this.mAutoListener = value; } }
9294
public bool Interactable {
9395
get { return this.mInteractable; }
@@ -100,6 +102,14 @@ public bool Interactable {
100102
}
101103
}
102104
public Text ButtonText { get { return this.mButtonText; } }
105+
public JCS_ButtonSelection ButtonSelection { get { return this.mButtonSelection; } set { this.mButtonSelection = value; } }
106+
public bool IsSelectedInGroup { get { return this.mIsSelectedInGroup; } }
107+
108+
/* Compatible with 1.5.3 version of JCSUnity */
109+
public void SetCallback(CallBackFunc func) { this.btnCallBack += func; }
110+
public void SetCallback(CallBackFuncBtn func) { this.btnCallBackBtn += func; }
111+
public void SetSystemCallback(CallBackFunc func) { this.btnSystemCallBack += func; }
112+
public void SetSystemCallback(CallBackFuncBtn func) { this.btnSystemCallBackBtn += func; }
103113

104114
/*******************************************/
105115
/* Unity's function */
@@ -111,7 +121,8 @@ protected virtual void Awake()
111121
mImage = this.GetComponent<Image>();
112122

113123
// try to get the text from the child.
114-
mButtonText = this.GetComponentInChildren<Text>();
124+
if (mButtonText == null)
125+
mButtonText = this.GetComponentInChildren<Text>();
115126

116127
if (mAutoListener)
117128
{
@@ -137,19 +148,24 @@ protected virtual void Awake()
137148
/// </summary>
138149
public virtual void JCS_ButtonClick()
139150
{
151+
this.mIsSelectedInGroup = IsSelected();
152+
153+
if (!mIsSelectedInGroup)
154+
return;
155+
140156
/* System callback */
141-
if (mBtnSystemCallBack != null)
142-
mBtnSystemCallBack.Invoke();
157+
if (btnSystemCallBack != null)
158+
btnSystemCallBack.Invoke();
143159

144-
if (mBtnSystemCallBackBtn != null)
145-
mBtnSystemCallBackBtn.Invoke(this);
160+
if (btnSystemCallBackBtn != null)
161+
btnSystemCallBackBtn.Invoke(this);
146162

147163
/* User callback */
148-
if (mBtnCallBack != null)
149-
mBtnCallBack.Invoke();
164+
if (btnCallBack != null)
165+
btnCallBack.Invoke();
150166

151-
if (mBtnCallBackBtn != null)
152-
mBtnCallBackBtn.Invoke(this);
167+
if (btnCallBackBtn != null)
168+
btnCallBackBtn.Invoke(this);
153169
}
154170

155171
/// <summary>
@@ -181,5 +197,28 @@ public virtual void SetInteractable()
181197
//----------------------
182198
// Private Functions
183199

200+
/// <summary>
201+
/// Check if this button selected. If you are using with
202+
/// the 'JCS_ButtonSelectionGroup' and 'JCS_ButtonSelection'
203+
/// then you might need this check to call out the on click event.
204+
/// </summary>
205+
/// <returns>
206+
/// true: is selected in the group.
207+
/// false: vice versa.
208+
/// </returns>
209+
private bool IsSelected()
210+
{
211+
if (mButtonSelection == null)
212+
return true;
213+
214+
if (!mButtonSelection.IsSelected())
215+
{
216+
// Make it selected.
217+
mButtonSelection.MakeSelect();
218+
return false;
219+
}
220+
221+
return true;
222+
}
184223
}
185224
}

Assets/JCSUnity/Scripts/Input/JCS_ButtonSelection.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public class JCS_ButtonSelection
3939
/* Private Variables */
4040
/*******************************************/
4141

42+
public JCS_ButtonSelectionGroup mButtonSelectionGroup = null;
43+
4244
[Header("** Check Variables (JCS_ButtonSelection) **")]
4345

4446
private bool mActive = false;
@@ -81,6 +83,7 @@ public bool Active
8183
DoActive();
8284
}
8385
}
86+
public JCS_ButtonSelectionGroup ButtonSelectionGroup { get { return this.mButtonSelectionGroup; } set { this.mButtonSelectionGroup = value; } }
8487

8588
/*******************************************/
8689
/* Unity's function */
@@ -90,8 +93,12 @@ private void Awake()
9093
if (mDeactiveAtAwake)
9194
{
9295
// Deactive every at start
93-
Active = false;
96+
this.mActive = false;
9497
}
98+
99+
// let the button know this is going to be control in the group.
100+
if (mButton != null)
101+
mButton.ButtonSelection = this;
95102
}
96103

97104
/*******************************************/
@@ -112,6 +119,26 @@ public void DoSelection()
112119
mSelectedEvent.Invoke();
113120
}
114121

122+
/// <summary>
123+
/// Check if this selection get selected.
124+
/// </summary>
125+
/// <returns>
126+
/// true: selected.
127+
/// false: vice versa.
128+
/// </returns>
129+
public bool IsSelected()
130+
{
131+
return this.mActive;
132+
}
133+
134+
/// <summary>
135+
/// Make this selection selected.
136+
/// </summary>
137+
public void MakeSelect()
138+
{
139+
mButtonSelectionGroup.SelectSelection(this);
140+
}
141+
115142
//----------------------
116143
// Protected Functions
117144

Assets/JCSUnity/Scripts/Input/JCS_ButtonSelectionGroup.cs

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class JCS_ButtonSelectionGroup
2929
/*******************************************/
3030

3131
// Callback triggered when selection has changed.
32-
public SelectionChanged selectionChanged = null;
32+
public SelectionChanged selectionChanged = null;
3333

3434
/*******************************************/
3535
/* Private Variables */
@@ -63,6 +63,10 @@ private void Awake()
6363
{
6464
this.mSelections = JCS_Utility.RemoveEmptySlot(mSelections);
6565

66+
// let them know the grouper.
67+
foreach (JCS_ButtonSelection bs in mSelections)
68+
bs.ButtonSelectionGroup = this;
69+
6670
selectionChanged = EmptyCallbackSelectionChanged;
6771
}
6872

@@ -132,41 +136,80 @@ public void OkaySelection()
132136
/// </summary>
133137
public void NextSelection()
134138
{
135-
// disable current active selection.
136-
mSelections[mCurrentSelectIndex].Active = false;
137-
138-
++mCurrentSelectIndex;
139-
140-
// loop through the array, if at head of the array we set it to the tail.
141-
if (mCurrentSelectIndex >= mSelections.Count)
142-
mCurrentSelectIndex = 0;
139+
int tempSelectIndex = mCurrentSelectIndex;
140+
++tempSelectIndex;
143141

144-
// active the new active selection.
145-
mSelections[mCurrentSelectIndex].Active = true;
146-
147-
selectionChanged.Invoke();
142+
SelectSelection(tempSelectIndex);
148143
}
149144

150145
/// <summary>
151146
/// Change to the previous button selection.
152147
/// </summary>
153148
public void PrevSelection()
154149
{
150+
int tempSelectIndex = mCurrentSelectIndex;
151+
--tempSelectIndex;
152+
153+
SelectSelection(tempSelectIndex);
154+
}
155+
156+
/// <summary>
157+
/// Selection this selection.
158+
/// </summary>
159+
/// <param name="selectionIndex"> index to select. </param>
160+
public void SelectSelection(int selectionIndex)
161+
{
162+
// no need to do anything.
163+
if (mCurrentSelectIndex == selectionIndex)
164+
return;
165+
155166
// disable current active selection.
156167
mSelections[mCurrentSelectIndex].Active = false;
157168

158-
--mCurrentSelectIndex;
169+
this.mCurrentSelectIndex = selectionIndex;
159170

160171
// loop through the array, if at the tail of the array set it to head.
161172
if (mCurrentSelectIndex < 0)
162173
mCurrentSelectIndex = mSelections.Count - 1;
174+
// loop through the array, if at head of the array we set it to the tail.
175+
else if (mCurrentSelectIndex >= mSelections.Count)
176+
mCurrentSelectIndex = 0;
163177

164178
// active the new active selection.
165179
mSelections[mCurrentSelectIndex].Active = true;
166180

167181
selectionChanged.Invoke();
168182
}
169183

184+
/// <summary>
185+
/// Selection this selection.
186+
/// </summary>
187+
/// <param name="selection"> selection to select. </param>
188+
public void SelectSelection(JCS_ButtonSelection selection)
189+
{
190+
/*
191+
* Time complexity: O(n)
192+
*
193+
* NOTE(jenchieh): might need to change this if we there are
194+
* more than 30 selections.
195+
*/
196+
for (int index = 0;
197+
index < mSelections.Count;
198+
++index)
199+
{
200+
JCS_ButtonSelection bs = mSelections[index];
201+
202+
if (bs == selection)
203+
{
204+
SelectSelection(index);
205+
return;
206+
}
207+
}
208+
209+
JCS_Debug.LogError(@"Try to select a selection, but seems like the
210+
selection is not in the group...");
211+
}
212+
170213
//----------------------
171214
// Protected Functions
172215

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Build from DESKTOP-5QUO247 at 10/13/2017 12:56:13 PM
1+
Build from DESKTOP-5QUO247 at 10/14/2017 4:16:03 PM

0 commit comments

Comments
 (0)