Skip to content

Commit 0ef06bc

Browse files
Merge pull request #5 from adrian-miasik/feature/keyboard-support
Feature: Add Keyboard Support
2 parents 3714527 + 8bad142 commit 0ef06bc

File tree

8 files changed

+423
-43
lines changed

8 files changed

+423
-43
lines changed

UnityPomodoro/Assets/AdrianMiasik/Scenes/Main.unity

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,17 @@ MonoBehaviour:
572572
m_EditorClassIdentifier:
573573
m_AspectMode: 3
574574
m_AspectRatio: 1
575+
--- !u!114 &74629570 stripped
576+
MonoBehaviour:
577+
m_CorrespondingSourceObject: {fileID: 1830320757405263119, guid: 01ce964053042544a908dbc2b7805528, type: 3}
578+
m_PrefabInstance: {fileID: 1830320758431363088}
579+
m_PrefabAsset: {fileID: 0}
580+
m_GameObject: {fileID: 0}
581+
m_Enabled: 1
582+
m_EditorHideFlags: 0
583+
m_Script: {fileID: 11500000, guid: 984ef3fea536ce04d8d2611e6164a61d, type: 3}
584+
m_Name:
585+
m_EditorClassIdentifier:
575586
--- !u!1 &167431530
576587
GameObject:
577588
m_ObjectHideFlags: 0
@@ -1259,12 +1270,12 @@ MonoBehaviour:
12591270
m_Name:
12601271
m_EditorClassIdentifier:
12611272
m_Navigation:
1262-
m_Mode: 3
1273+
m_Mode: 4
12631274
m_WrapAround: 0
12641275
m_SelectOnUp: {fileID: 0}
12651276
m_SelectOnDown: {fileID: 0}
12661277
m_SelectOnLeft: {fileID: 0}
1267-
m_SelectOnRight: {fileID: 0}
1278+
m_SelectOnRight: {fileID: 2022457357}
12681279
m_Transition: 0
12691280
m_Colors:
12701281
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
@@ -1984,7 +1995,7 @@ MonoBehaviour:
19841995
m_faceColor:
19851996
serializedVersion: 2
19861997
rgba: 4294967295
1987-
m_fontSize: 43.8
1998+
m_fontSize: 36.05
19881999
m_fontSizeBase: 36
19892000
m_fontWeight: 400
19902001
m_enableAutoSizing: 1
@@ -2245,6 +2256,17 @@ Transform:
22452256
m_Father: {fileID: 0}
22462257
m_RootOrder: 2
22472258
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
2259+
--- !u!114 &810166524 stripped
2260+
MonoBehaviour:
2261+
m_CorrespondingSourceObject: {fileID: 1830320757405263119, guid: 01ce964053042544a908dbc2b7805528, type: 3}
2262+
m_PrefabInstance: {fileID: 1050340279}
2263+
m_PrefabAsset: {fileID: 0}
2264+
m_GameObject: {fileID: 0}
2265+
m_Enabled: 1
2266+
m_EditorHideFlags: 0
2267+
m_Script: {fileID: 11500000, guid: 984ef3fea536ce04d8d2611e6164a61d, type: 3}
2268+
m_Name:
2269+
m_EditorClassIdentifier:
22482270
--- !u!1 &837249547
22492271
GameObject:
22502272
m_ObjectHideFlags: 0
@@ -4546,6 +4568,7 @@ GameObject:
45464568
- component: {fileID: 1601903656}
45474569
- component: {fileID: 1601903657}
45484570
- component: {fileID: 1601903658}
4571+
- component: {fileID: 1601903659}
45494572
m_Layer: 5
45504573
m_Name: Pomodoro Timer
45514574
m_TagString: Untagged
@@ -4598,6 +4621,8 @@ MonoBehaviour:
45984621
secondDigits: {fileID: 2096418338}
45994622
text: {fileID: 650676969}
46004623
infoToggle: {fileID: 1617491758}
4624+
leftButtonClick: {fileID: 74629570}
4625+
rightButtonClick: {fileID: 810166524}
46014626
rightButton: {fileID: 1430356801}
46024627
breakSlider: {fileID: 1276951607}
46034628
creditsBubble: {fileID: 929885561}
@@ -4668,6 +4693,8 @@ MonoBehaviour:
46684693
breakMinutes: 5
46694694
breakSeconds: 0
46704695
xmlToast: {fileID: 4900000, guid: d3141a8c2f6b4f3bbd2831a62f225996, type: 3}
4696+
hotkeyDetector: {fileID: 1601903659}
4697+
selectedDigits: []
46714698
_fadeDuration: 0.1
46724699
_pauseHoldDuration: 0.75
46734700
--- !u!82 &1601903657
@@ -4782,6 +4809,18 @@ Animation:
47824809
m_PlayAutomatically: 0
47834810
m_AnimatePhysics: 0
47844811
m_CullingType: 0
4812+
--- !u!114 &1601903659
4813+
MonoBehaviour:
4814+
m_ObjectHideFlags: 0
4815+
m_CorrespondingSourceObject: {fileID: 0}
4816+
m_PrefabInstance: {fileID: 0}
4817+
m_PrefabAsset: {fileID: 0}
4818+
m_GameObject: {fileID: 1601903654}
4819+
m_Enabled: 1
4820+
m_EditorHideFlags: 0
4821+
m_Script: {fileID: 11500000, guid: ee5a48f537165b54dae3997a686be6f6, type: 3}
4822+
m_Name:
4823+
m_EditorClassIdentifier:
47854824
--- !u!1 &1608452770
47864825
GameObject:
47874826
m_ObjectHideFlags: 0

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

Lines changed: 71 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,21 @@ 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+
if (!input.isFocused)
108+
{
109+
SetValue(digit, "0");
110+
}
111+
}
112+
97113
// Scroll input
98114
if (Input.mouseScrollDelta.y > 0)
99115
{
@@ -109,7 +125,7 @@ private void Update()
109125
downArrow.OnPointerClick(null);
110126
}
111127
}
112-
128+
113129
// Arrow keys : Up arrow
114130
if (Input.GetKeyDown(KeyCode.UpArrow))
115131
{
@@ -120,7 +136,7 @@ private void Update()
120136
{
121137
upArrow.Release();
122138
}
123-
139+
124140
// Arrow keys : Down arrow
125141
if (Input.GetKeyDown(KeyCode.DownArrow))
126142
{
@@ -136,7 +152,7 @@ private void Update()
136152
if (Input.touchSupported && !input.isFocused)
137153
{
138154
accumulatedSelectionTime += Time.deltaTime;
139-
155+
140156
if (accumulatedSelectionTime > deselectionDuration)
141157
{
142158
accumulatedSelectionTime = 0;
@@ -154,8 +170,9 @@ private void SetSquircleColor(Color color)
154170
{
155171
_instanceMaterial = new Material(background.material);
156172
}
173+
157174
startingColor = _instanceMaterial.GetColor(SquircleColor);
158-
175+
159176
endingColor = color;
160177
accumulatedColorTime = 0;
161178
isColorAnimating = true;
@@ -167,10 +184,20 @@ public void SetDigitsLabel(int digits)
167184
if (digits.ToString("D2") != input.text)
168185
{
169186
// Update the digit
170-
input.text = digits.ToString("D2");
187+
input.text = digits.ToString("D2");
171188
}
172189
}
173190

191+
public string GetDigitsLabel()
192+
{
193+
return input.text;
194+
}
195+
196+
public Selectable GetSelectable()
197+
{
198+
return selectable;
199+
}
200+
174201
private void HideArrows()
175202
{
176203
upArrow.Hide();
@@ -209,8 +236,26 @@ public void SetTextColor(Color newColor)
209236
{
210237
input.textComponent.color = newColor;
211238
}
212-
239+
213240
// Unity Events
241+
public void SetValue(PomodoroTimer.Digits digit, string value)
242+
{
243+
switch (digit)
244+
{
245+
case PomodoroTimer.Digits.HOURS:
246+
SetHours(value);
247+
break;
248+
case PomodoroTimer.Digits.MINUTES:
249+
SetMinutes(value);
250+
break;
251+
case PomodoroTimer.Digits.SECONDS:
252+
SetSeconds(value);
253+
break;
254+
default:
255+
throw new ArgumentOutOfRangeException(nameof(digit), digit, null);
256+
}
257+
}
258+
214259
public void SetHours(string hours)
215260
{
216261
if (timer != null)
@@ -286,8 +331,18 @@ private void UpdateArrows()
286331
downArrow.Hide();
287332
}
288333
}
334+
335+
public void Highlight()
336+
{
337+
OnSelect(null, false);
338+
}
289339

290340
public void OnSelect(BaseEventData eventData)
341+
{
342+
OnSelect(eventData, true);
343+
}
344+
345+
public void OnSelect(BaseEventData eventData, bool setSelection)
291346
{
292347
if (!isInteractable)
293348
{
@@ -300,7 +355,10 @@ public void OnSelect(BaseEventData eventData)
300355
ShowArrows();
301356
SetSquircleColor(selectionColor);
302357

303-
timer.SetSelection(this);
358+
if (setSelection)
359+
{
360+
timer.SetSelection(this);
361+
}
304362

305363
OnSelection.Invoke();
306364
}
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)