Skip to content

Commit 00ee7b7

Browse files
committed
fix: button sound on hover
1 parent 73f835f commit 00ee7b7

File tree

7 files changed

+49
-60
lines changed

7 files changed

+49
-60
lines changed

Assets/JCSUnity/Scripts/Effects/JCS_ButtonSoundEffect.cs

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ public class JCS_ButtonSoundEffect : MonoBehaviour
2424
{
2525
/* Variables */
2626

27-
private RectTransform mRectTransform = null;
2827
private EventTrigger mEventTrigger = null;
2928

29+
[Separator("Check Variables (JCS_ButtonSoundEffect)")]
30+
31+
[Tooltip("Is true when mouse is over this button.")]
32+
[SerializeField]
33+
[ReadOnly]
34+
private bool mIsEntered = false;
35+
3036
[Separator("Optional Variables (JCS_ButtonSoundEffect)")]
3137

3238
[Tooltip(@"Sound Player for this button, if this transform dose not
@@ -55,7 +61,6 @@ public class JCS_ButtonSoundEffect : MonoBehaviour
5561
[SerializeField]
5662
private AudioClip mOnMouseDoubleClickSound = null;
5763

58-
private bool mIsOver = false;
5964

6065
[SerializeField]
6166
private JCS_SoundMethod mOnMouseOverSoundMethod = JCS_SoundMethod.PLAY_SOUND;
@@ -141,7 +146,6 @@ private void Awake()
141146
{
142147
if (mSoundPlayer == null)
143148
mSoundPlayer = GetComponent<JCS_SoundPlayer>();
144-
mRectTransform = GetComponent<RectTransform>();
145149
mEventTrigger = GetComponent<EventTrigger>();
146150

147151
if (mBtn == null)
@@ -164,8 +168,7 @@ private void Start()
164168

165169
if (mAutoAddEvent)
166170
{
167-
JCS_UIUtil.AddEventTriggerEvent(mEventTrigger, EventTriggerType.PointerEnter, ItOnMouseOver);
168-
JCS_UIUtil.AddEventTriggerEvent(mEventTrigger, EventTriggerType.PointerEnter, ItOnMouseDoubleClick);
171+
JCS_UIUtil.AddEventTriggerEvent(mEventTrigger, EventTriggerType.PointerEnter, ItOnMouseEnter);
169172
JCS_UIUtil.AddEventTriggerEvent(mEventTrigger, EventTriggerType.PointerExit, ItOnMouseExit);
170173
JCS_UIUtil.AddEventTriggerEvent(mEventTrigger, EventTriggerType.PointerDown, ItOnMouseDown);
171174
JCS_UIUtil.AddEventTriggerEvent(mEventTrigger, EventTriggerType.PointerUp, ItOnMouseUp);
@@ -181,26 +184,17 @@ private void Update()
181184
private void HandleDoubleClick()
182185
{
183186
// IMPORTANT(JenChieh): only double click need update
184-
if (!mIsOver)
185-
return;
186-
187-
if (mBtn == null)
187+
if (mBtn == null || !mIsEntered)
188188
return;
189189

190190
if (JCS_Input.OnMouseDoubleClick(0))
191191
{
192-
// either time is out or double click,
193-
// both are all over the "double click event".
194-
mIsOver = false;
195-
196192
if (!mBtn.interactable)
197193
{
198194
// play not ineractable sound
199195
mSoundPlayer.PlayOneShotByMethod(
200196
mOnMouseDoubleClickRefuseSound,
201197
mOnMouseDoubleClickRefuseSoundMethod);
202-
203-
return;
204198
}
205199
else
206200
{
@@ -210,18 +204,16 @@ private void HandleDoubleClick()
210204
mOnMouseDoubleClickSoundMethod);
211205
}
212206
}
213-
214-
// check if the mouse still over or not
215-
if (!JCS_UIUtil.MouseOverGUI(mRectTransform))
216-
mIsOver = false;
217207
}
218208

219-
public void ItOnMouseOver()
209+
public void ItOnMouseEnter()
220210
{
221-
ItOnMouseOver(null);
211+
ItOnMouseEnter(null);
222212
}
223-
public void ItOnMouseOver(PointerEventData data)
213+
public void ItOnMouseEnter(PointerEventData data)
224214
{
215+
mIsEntered = true;
216+
225217
if (mBtn != null)
226218
{
227219
if (!mBtn.interactable)
@@ -245,6 +237,8 @@ public void ItOnMouseExit()
245237
}
246238
public void ItOnMouseExit(PointerEventData data)
247239
{
240+
mIsEntered = false;
241+
248242
if (mBtn == null)
249243
return;
250244

@@ -269,7 +263,7 @@ public void ItOnMouseDown()
269263
}
270264
public void ItOnMouseDown(PointerEventData data)
271265
{
272-
if (mBtn == null)
266+
if (mBtn == null || !mIsEntered)
273267
return;
274268

275269
if (!mBtn.interactable)
@@ -294,7 +288,7 @@ public void ItOnMouseUp()
294288
}
295289
public void ItOnMouseUp(PointerEventData data)
296290
{
297-
if (mBtn == null)
291+
if (mBtn == null || !mIsEntered)
298292
return;
299293

300294
if (!mBtn.interactable)
@@ -335,15 +329,5 @@ public void ItOnMouseClick(PointerEventData data)
335329
mOnMouseClickSoundMethod);
336330
}
337331
}
338-
339-
// plz put this in Pointer Enter event
340-
public void ItOnMouseDoubleClick()
341-
{
342-
ItOnMouseDoubleClick(null);
343-
}
344-
public void ItOnMouseDoubleClick(PointerEventData data)
345-
{
346-
mIsOver = true;
347-
}
348332
}
349333
}

Assets/JCSUnity/Scripts/Input/JCS_ButtonSelection.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,9 @@ private void Start()
138138
/// </summary>
139139
public void DoSelection()
140140
{
141-
if (mButton != null)
142-
mButton.ButtonClick();
141+
mButton?.InterBtnClick();
143142

144-
if (mSelectedEvent != null)
145-
mSelectedEvent.Invoke();
143+
mSelectedEvent?.Invoke();
146144
}
147145

148146
/// <summary>

Assets/JCSUnity/Scripts/Input/JCS_Input.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,17 @@ static bool OnMouseDoubleClick(int button)
258258
// Check first click
259259
if (!CLICK)
260260
{
261-
if (GetMouseButtonDown(button))
261+
if (GetMouseButtonUp(button))
262262
CLICK = true;
263263
}
264264
// Check double click
265265
else
266266
{
267-
if (GetMouseButtonDown(button))
267+
if (GetMouseButtonUp(button))
268268
{
269269
CLICK = false;
270+
CLICK_TIMER = 0.0f;
271+
270272
return true;
271273
}
272274
}

Assets/JCSUnity/Scripts/UI/Button/JCS_RollSelectorButton.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private void Start()
6969
/// <summary>
7070
/// Override
7171
/// </summary>
72-
public override void ButtonClick()
72+
public override void InterBtnClick()
7373
{
7474
if (mRollBtnSelector == null)
7575
{
@@ -84,7 +84,7 @@ public override void ButtonClick()
8484
}
8585
else
8686
{
87-
base.ButtonClick();
87+
base.InterBtnClick();
8888
}
8989
}
9090

@@ -146,7 +146,7 @@ private void OverwriteAllButton()
146146
mButton.onClick.RemoveAllListeners();
147147

148148
// only add this listener
149-
mButton.onClick.AddListener(ButtonClick);
149+
mButton.onClick.AddListener(InterBtnClick);
150150

151151
// get all the buttons on this transform
152152
btns = GetComponents<JCS_Button>();
@@ -171,8 +171,8 @@ private void ActiveAllOtherButtons()
171171
// don't call if is itself
172172
if (b == this)
173173
continue;
174-
175-
b.ButtonClick();
174+
175+
b.InterBtnClick();
176176
}
177177
}
178178
}

Assets/JCSUnity/Scripts/UI/JCS_Button.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ protected virtual void Awake()
137137
/// <summary>
138138
/// Intialize the button once.
139139
/// </summary>
140-
public void Init(bool forceInit = false)
140+
public void Init(bool force = false)
141141
{
142-
if (!forceInit)
142+
if (!force)
143143
{
144144
if (mInitialized)
145145
return;
@@ -163,14 +163,14 @@ public void Init(bool forceInit = false)
163163

164164
if (mAutoListener)
165165
{
166-
// add listener itself, but it won't show in the inspector
167-
mButton.onClick.AddListener(ButtonClick);
166+
// Add listener itself, but it won't show in the inspector.
167+
mButton.onClick.AddListener(InterBtnClick);
168168
}
169169

170-
// set the stating interactable.
170+
// Set the stating interactable.
171171
SetInteractable();
172172

173-
// part of the on click callback
173+
// Part of the on click callback.
174174
SetSystemCallback(OnClick);
175175

176176
mInitialized = true;
@@ -182,7 +182,7 @@ public void Init(bool forceInit = false)
182182
///
183183
/// * Good for organize code and game data file in Unity.
184184
/// </summary>
185-
public virtual void ButtonClick()
185+
public virtual void InterBtnClick()
186186
{
187187
mIsSelectedInGroup = IsSelected();
188188

Assets/JCSUnity/Scripts/UI/JCS_GamepadButton.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected virtual void Update()
8585
{
8686
if (JCS_Input.IsAnyKeyBuffer(mKeyActionType))
8787
{
88-
ButtonClick();
88+
InterBtnClick();
8989
PlayButtonClickSound();
9090
}
9191
}
@@ -97,7 +97,7 @@ protected virtual void Update()
9797
// listen to game pad.
9898
JCS_Input.GetJoystickKeyByAction(mKeyActionType, mJoystickLitener, mJKeyToListen))
9999
{
100-
ButtonClick();
100+
InterBtnClick();
101101
PlayButtonClickSound();
102102
}
103103
}

Assets/JCSUnity/Scripts/Util/JCS_UIUtil.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ public static bool IsUnityDefinedUI(Component comp)
6060
/// <param name="te"></param>
6161
/// <param name="type"></param>
6262
/// <param name="func"></param>
63-
public static void AddEventTriggerEvent(EventTrigger te, EventTriggerType type, Action<PointerEventData> func)
63+
public static void AddEventTriggerEvent(
64+
EventTrigger te, EventTriggerType type,
65+
Action<PointerEventData> func)
6466
{
65-
EventTrigger.Entry entry = new EventTrigger.Entry();
67+
var entry = new EventTrigger.Entry();
6668
entry.eventID = type;
67-
entry.callback.AddListener((data) => { func((PointerEventData)data); });
69+
entry.callback.AddListener((data) => { func?.Invoke((PointerEventData)data); });
6870
te.triggers.Add(entry);
6971
}
7072

@@ -74,11 +76,14 @@ public static void AddEventTriggerEvent(EventTrigger te, EventTriggerType type,
7476
/// <param name="te"></param>
7577
/// <param name="type"></param>
7678
/// <param name="func"></param>
77-
public static void AddEventTriggerEvent(EventTrigger te, EventTriggerType type, Action<PointerEventData, JCS_ButtonSelection> func, JCS_ButtonSelection selection)
79+
public static void AddEventTriggerEvent(
80+
EventTrigger te, EventTriggerType type,
81+
Action<PointerEventData, JCS_ButtonSelection> func,
82+
JCS_ButtonSelection selection)
7883
{
79-
EventTrigger.Entry entry = new EventTrigger.Entry();
84+
var entry = new EventTrigger.Entry();
8085
entry.eventID = type;
81-
entry.callback.AddListener((data) => { func((PointerEventData)data, selection); });
86+
entry.callback.AddListener((data) => { func?.Invoke((PointerEventData)data, selection); });
8287
te.triggers.Add(entry);
8388
}
8489

0 commit comments

Comments
 (0)