Skip to content

Commit 01216c4

Browse files
committed
Add partial keyboard support
Adds the ability to copy and paste the formatted digit values using "ctrl + c" & "ctrl + v" respectively. Adds the ability to cycle through the digits using "tab". Adds the ability to select all digits using "ctrl + a" Adds the ability to deselect digits using "escape" Adds the ability to delete/clear the current selected digit(s) using "backspace"
1 parent 81e035c commit 01216c4

File tree

5 files changed

+313
-23
lines changed

5 files changed

+313
-23
lines changed

UnityPomodoro/Assets/AdrianMiasik/Scenes/Main.unity

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4546,6 +4546,7 @@ GameObject:
45464546
- component: {fileID: 1601903656}
45474547
- component: {fileID: 1601903657}
45484548
- component: {fileID: 1601903658}
4549+
- component: {fileID: 1601903659}
45494550
m_Layer: 5
45504551
m_Name: Pomodoro Timer
45514552
m_TagString: Untagged
@@ -4668,6 +4669,12 @@ MonoBehaviour:
46684669
breakMinutes: 5
46694670
breakSeconds: 0
46704671
xmlToast: {fileID: 4900000, guid: d3141a8c2f6b4f3bbd2831a62f225996, type: 3}
4672+
hotkeyDetector: {fileID: 1601903659}
4673+
selectedDigits: []
4674+
tabElements:
4675+
- {fileID: 1524052414}
4676+
- {fileID: 429705704}
4677+
- {fileID: 2096418338}
46714678
_fadeDuration: 0.1
46724679
_pauseHoldDuration: 0.75
46734680
--- !u!82 &1601903657
@@ -4782,6 +4789,18 @@ Animation:
47824789
m_PlayAutomatically: 0
47834790
m_AnimatePhysics: 0
47844791
m_CullingType: 0
4792+
--- !u!114 &1601903659
4793+
MonoBehaviour:
4794+
m_ObjectHideFlags: 0
4795+
m_CorrespondingSourceObject: {fileID: 0}
4796+
m_PrefabInstance: {fileID: 0}
4797+
m_PrefabAsset: {fileID: 0}
4798+
m_GameObject: {fileID: 1601903654}
4799+
m_Enabled: 1
4800+
m_EditorHideFlags: 0
4801+
m_Script: {fileID: 11500000, guid: ee5a48f537165b54dae3997a686be6f6, type: 3}
4802+
m_Name:
4803+
m_EditorClassIdentifier:
47854804
--- !u!1 &1608452770
47864805
GameObject:
47874806
m_ObjectHideFlags: 0

UnityPomodoro/Assets/AdrianMiasik/Scripts/Components/DoubleDigit.cs

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using AdrianMiasik.Components.Core;
23
using TMPro;
34
using UnityEngine;
@@ -23,7 +24,7 @@ public class DoubleDigit : MonoBehaviour, ISelectHandler, IPointerClickHandler,
2324

2425
[Header("Wobble")]
2526
[SerializeField] private Animation pulseWobble;
26-
27+
2728
private PomodoroTimer.Digits digit;
2829
private PomodoroTimer timer;
2930
private bool isInteractable;
@@ -41,31 +42,31 @@ public class DoubleDigit : MonoBehaviour, ISelectHandler, IPointerClickHandler,
4142
// Deselection
4243
private float accumulatedSelectionTime;
4344
private float deselectionDuration = 3f; // How long to wait before deselecting automatically on touch devices
44-
45+
4546
// Controls
4647
private TMP_SelectionCaret caret;
4748
private bool ignoreFirstClick = true;
4849

4950
// Shaders
5051
private Material _instanceMaterial;
5152
private static readonly int SquircleColor = Shader.PropertyToID("Color_297012532bf444df807f8743bdb7e4fd");
52-
53+
5354
// Unity Events
5455
public UnityEvent OnSelection;
5556

5657
public void Initialize(PomodoroTimer.Digits digit, PomodoroTimer timer, int digits)
5758
{
5859
this.digit = digit;
5960
this.timer = timer;
60-
61+
6162
// Disable run time caret interactions - We want to run input through this classes input events
6263
caret = input.textViewport.GetChild(0).GetComponent<TMP_SelectionCaret>();
6364
if (caret)
6465
{
6566
// Prevent input field from getting selection focus
6667
caret.raycastTarget = false;
6768
}
68-
69+
6970
SetSquircleColor(color);
7071
SetDigitsLabel(digits);
7172
HideArrows();
@@ -82,7 +83,7 @@ private void Update()
8283
{
8384
isColorAnimating = false;
8485
}
85-
86+
8687
float evaluatedTime = animationRamp.Evaluate(progress);
8788

8889
// Animate and modify
@@ -94,6 +95,18 @@ private void Update()
9495

9596
if (isSelected)
9697
{
98+
// // Deselection
99+
// if (Input.GetKeyDown(KeyCode.Escape))
100+
// {
101+
// Deselect();
102+
// timer.RemoveSelection(this);
103+
// }
104+
105+
if (Input.GetKeyDown(KeyCode.Delete) || Input.GetKeyDown(KeyCode.Backspace))
106+
{
107+
SetValue(digit, "0");
108+
}
109+
97110
// Scroll input
98111
if (Input.mouseScrollDelta.y > 0)
99112
{
@@ -109,7 +122,7 @@ private void Update()
109122
downArrow.OnPointerClick(null);
110123
}
111124
}
112-
125+
113126
// Arrow keys : Up arrow
114127
if (Input.GetKeyDown(KeyCode.UpArrow))
115128
{
@@ -120,7 +133,7 @@ private void Update()
120133
{
121134
upArrow.Release();
122135
}
123-
136+
124137
// Arrow keys : Down arrow
125138
if (Input.GetKeyDown(KeyCode.DownArrow))
126139
{
@@ -136,7 +149,7 @@ private void Update()
136149
if (Input.touchSupported && !input.isFocused)
137150
{
138151
accumulatedSelectionTime += Time.deltaTime;
139-
152+
140153
if (accumulatedSelectionTime > deselectionDuration)
141154
{
142155
accumulatedSelectionTime = 0;
@@ -154,8 +167,9 @@ private void SetSquircleColor(Color color)
154167
{
155168
_instanceMaterial = new Material(background.material);
156169
}
170+
157171
startingColor = _instanceMaterial.GetColor(SquircleColor);
158-
172+
159173
endingColor = color;
160174
accumulatedColorTime = 0;
161175
isColorAnimating = true;
@@ -167,10 +181,15 @@ public void SetDigitsLabel(int digits)
167181
if (digits.ToString("D2") != input.text)
168182
{
169183
// Update the digit
170-
input.text = digits.ToString("D2");
184+
input.text = digits.ToString("D2");
171185
}
172186
}
173187

188+
public string GetDigitsLabel()
189+
{
190+
return input.text;
191+
}
192+
174193
private void HideArrows()
175194
{
176195
upArrow.Hide();
@@ -209,8 +228,26 @@ public void SetTextColor(Color newColor)
209228
{
210229
input.textComponent.color = newColor;
211230
}
212-
231+
213232
// Unity Events
233+
public void SetValue(PomodoroTimer.Digits digit, string value)
234+
{
235+
switch (digit)
236+
{
237+
case PomodoroTimer.Digits.HOURS:
238+
SetHours(value);
239+
break;
240+
case PomodoroTimer.Digits.MINUTES:
241+
SetMinutes(value);
242+
break;
243+
case PomodoroTimer.Digits.SECONDS:
244+
SetSeconds(value);
245+
break;
246+
default:
247+
throw new ArgumentOutOfRangeException(nameof(digit), digit, null);
248+
}
249+
}
250+
214251
public void SetHours(string hours)
215252
{
216253
if (timer != null)
@@ -286,8 +323,18 @@ private void UpdateArrows()
286323
downArrow.Hide();
287324
}
288325
}
326+
327+
public void Highlight()
328+
{
329+
OnSelect(null, false);
330+
}
289331

290332
public void OnSelect(BaseEventData eventData)
333+
{
334+
OnSelect(eventData, true);
335+
}
336+
337+
public void OnSelect(BaseEventData eventData, bool setSelection)
291338
{
292339
if (!isInteractable)
293340
{
@@ -300,7 +347,10 @@ public void OnSelect(BaseEventData eventData)
300347
ShowArrows();
301348
SetSquircleColor(selectionColor);
302349

303-
timer.SetSelection(this);
350+
if (setSelection)
351+
{
352+
timer.SetSelection(this);
353+
}
304354

305355
OnSelection.Invoke();
306356
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace AdrianMiasik
5+
{
6+
public class HotkeyDetector : MonoBehaviour
7+
{
8+
private PomodoroTimer timer;
9+
private bool isInitialized = false;
10+
11+
public void Initialize(PomodoroTimer pomodoroTimer)
12+
{
13+
timer = pomodoroTimer;
14+
isInitialized = true;
15+
}
16+
17+
private void Update()
18+
{
19+
if (!isInitialized)
20+
{
21+
return;
22+
}
23+
24+
// Select All
25+
if (IsSelectingAll() && timer.state == PomodoroTimer.States.SETUP)
26+
{
27+
timer.SelectAll();
28+
}
29+
30+
// Copy
31+
if (IsCopying())
32+
{
33+
GUIUtility.systemCopyBuffer = timer.GetTimerString();
34+
}
35+
36+
// Paste
37+
if (IsPasting() && timer.state == PomodoroTimer.States.SETUP)
38+
{
39+
timer.SetTimerValue(GUIUtility.systemCopyBuffer);
40+
}
41+
}
42+
43+
private bool IsSelectingAll()
44+
{
45+
if (Input.GetKeyUp(KeyCode.A) && Input.GetKey(KeyCode.LeftControl) ||
46+
Input.GetKeyUp(KeyCode.A) && Input.GetKey(KeyCode.RightControl))
47+
{
48+
return true;
49+
}
50+
51+
return false;
52+
}
53+
54+
private bool IsCopying()
55+
{
56+
if (Input.GetKeyUp(KeyCode.C) && Input.GetKey(KeyCode.LeftControl) ||
57+
Input.GetKeyUp(KeyCode.C) && Input.GetKey(KeyCode.RightControl))
58+
{
59+
return true;
60+
}
61+
62+
return false;
63+
}
64+
65+
private bool IsPasting()
66+
{
67+
if (Input.GetKeyUp(KeyCode.V) && Input.GetKey(KeyCode.LeftControl) ||
68+
Input.GetKeyUp(KeyCode.V) && Input.GetKey(KeyCode.RightControl))
69+
{
70+
return true;
71+
}
72+
73+
return false;
74+
}
75+
}
76+
}

UnityPomodoro/Assets/AdrianMiasik/Scripts/Components/HotkeyDetector.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.

0 commit comments

Comments
 (0)