From 4a3b3fb33964de537a5df830b02e956d082835a7 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 11 Sep 2019 15:51:05 +0200 Subject: [PATCH] Add multitouch --- Assets/Drawing/Prefabs/Drawable Frame.prefab | 17 ++------- Assets/Drawing/Scripts/DrawableFrame.cs | 25 +++++++++++-- Assets/Drawing/Scripts/DrawingManager.cs | 8 ++++- Assets/Drawing/Scripts/PencilTool.cs | 36 +++++++++++++++---- .../Scripts/SetSpriteFromDrawableFrame.cs | 17 --------- .../SetSpriteFromDrawableFrame.cs.meta | 11 ------ Assets/Drawing/Scripts/Timer.cs | 34 +++++++++++++----- Assets/Scenes/SampleScene.unity | 6 ++++ ProjectSettings/ProjectSettings.asset | 2 +- 9 files changed, 93 insertions(+), 63 deletions(-) delete mode 100644 Assets/Drawing/Scripts/SetSpriteFromDrawableFrame.cs delete mode 100644 Assets/Drawing/Scripts/SetSpriteFromDrawableFrame.cs.meta diff --git a/Assets/Drawing/Prefabs/Drawable Frame.prefab b/Assets/Drawing/Prefabs/Drawable Frame.prefab index 37fd897..111eff7 100644 --- a/Assets/Drawing/Prefabs/Drawable Frame.prefab +++ b/Assets/Drawing/Prefabs/Drawable Frame.prefab @@ -11,7 +11,6 @@ GameObject: - component: {fileID: 6807415978649219551} - component: {fileID: 6807415978649219550} - component: {fileID: 6807415978649219545} - - component: {fileID: 6807415978649219544} - component: {fileID: 6807415977000239074} m_Layer: 0 m_Name: Drawable Frame @@ -98,20 +97,8 @@ MonoBehaviour: m_EditorClassIdentifier: resolution: {x: 500, y: 500} startingColor: {r: 0, g: 0, b: 0, a: 0} - texture: {fileID: 0} - sprite: {fileID: 0} ---- !u!114 &6807415978649219544 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 6807415978649219547} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 21d58fc36c2296c4787f0492c99a5d2c, type: 3} - m_Name: - m_EditorClassIdentifier: + locked: 0 + hasChanged: 0 --- !u!114 &6807415977000239074 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Drawing/Scripts/DrawableFrame.cs b/Assets/Drawing/Scripts/DrawableFrame.cs index b84297b..88ce205 100644 --- a/Assets/Drawing/Scripts/DrawableFrame.cs +++ b/Assets/Drawing/Scripts/DrawableFrame.cs @@ -8,9 +8,13 @@ public class DrawableFrame : MonoBehaviour public Vector2Int resolution; public Color startingColor = Color.white; + [Header("State")] + public bool locked = false; + public bool hasChanged = false; + [Header("References")] - public Texture2D texture = null; - public Sprite sprite = null; + protected Texture2D texture = null; + protected Sprite sprite = null; private SpriteRenderer spriteRenderer; @@ -44,6 +48,23 @@ public class DrawableFrame : MonoBehaviour sprite = Sprite.Create(texture, new Rect(0, 0, resolution.x, resolution.y), new Vector2(0.5f, 0.5f), resolution.x); spriteRenderer.sprite = sprite; } + + public Color[] GetPixels() { + return texture.GetPixels(); + } + + public void SetPixels(Color[] colors) { + if(!locked) { + texture.SetPixels(colors); + hasChanged = true; + } + } + + protected void Update() { + if(hasChanged) { + texture.Apply(); + } + } protected void OnDrawGizmos() { diff --git a/Assets/Drawing/Scripts/DrawingManager.cs b/Assets/Drawing/Scripts/DrawingManager.cs index cfc7ad4..d60db1d 100644 --- a/Assets/Drawing/Scripts/DrawingManager.cs +++ b/Assets/Drawing/Scripts/DrawingManager.cs @@ -43,6 +43,9 @@ public class DrawingManager : MonoBehaviour Debug.LogWarning("Invalid settings json string, using defaults."); } } + else { + Debug.Log(JsonUtility.ToJson(settings)); + } canvas = GameObject.FindGameObjectWithTag("Canvas").GetComponent(); @@ -77,6 +80,9 @@ public class DrawingManager : MonoBehaviour actDescription.text = settings.actDescription; timer = Instantiate(timerPrefab, canvas.transform).GetComponent(); - timer.StartTimer(settings.timeLimit); + timer.TimerSet(settings.timeLimit); + + //TODO: replace with a countdown or something + timer.TimerStart(); } } diff --git a/Assets/Drawing/Scripts/PencilTool.cs b/Assets/Drawing/Scripts/PencilTool.cs index 9bdcb0f..33a632d 100644 --- a/Assets/Drawing/Scripts/PencilTool.cs +++ b/Assets/Drawing/Scripts/PencilTool.cs @@ -12,8 +12,8 @@ public class PencilTool : DrawTool private Vector2Int? previousPixel; private Vector2Int? pixel; - protected Vector2Int? HoveredPixel() { - Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); + protected Vector2Int? GetPixelAtScreenPosition(Vector2 screenPosition) { + Vector2 worldPoint = Camera.main.ScreenToWorldPoint(screenPosition); if(frame.Bounds.Contains(worldPoint)) { Vector2 normalized = Rect.PointToNormalized(frame.Bounds, worldPoint); Vector2Int result = new Vector2Int(Mathf.FloorToInt(normalized.x * frame.resolution.x), Mathf.FloorToInt(normalized.y * frame.resolution.y)); @@ -24,13 +24,33 @@ public class PencilTool : DrawTool protected override void Start() { base.Start(); - colors = frame.texture.GetPixels(); + colors = frame.GetPixels(); + + Input.simulateMouseWithTouches = false; } protected void Update() { + bool hasChanged = false; + + // Touch + foreach(Touch t in Input.touches) { + pixel = GetPixelAtScreenPosition(t.position); + previousPixel = GetPixelAtScreenPosition(t.position - t.deltaPosition); + if(pixel.HasValue) { + if(previousPixel.HasValue) { + DrawBresenhamsThickLine(pixel.Value, previousPixel.Value, size, selectedColor); + } + else { + DrawFilledCircle(pixel.Value, size, selectedColor); + } + } + hasChanged = true; + } + + // Mouse previousPixel = pixel; if(Input.GetMouseButton(0)) { - pixel = HoveredPixel(); + pixel = GetPixelAtScreenPosition(Input.mousePosition); if(pixel.HasValue) { if(previousPixel.HasValue) { DrawBresenhamsThickLine(previousPixel.Value, pixel.Value, size, selectedColor); @@ -39,11 +59,15 @@ public class PencilTool : DrawTool DrawFilledCircle(pixel.Value, size, selectedColor); } } - Apply(); + hasChanged = true; } else { pixel = null; } + + if(hasChanged) { + frame.SetPixels(colors); + } } protected void DrawPixel(int x, int y, Color color) { @@ -124,7 +148,5 @@ public class PencilTool : DrawTool } protected void Apply() { - frame.texture.SetPixels(colors); - frame.texture.Apply(); } } diff --git a/Assets/Drawing/Scripts/SetSpriteFromDrawableFrame.cs b/Assets/Drawing/Scripts/SetSpriteFromDrawableFrame.cs deleted file mode 100644 index 4060c66..0000000 --- a/Assets/Drawing/Scripts/SetSpriteFromDrawableFrame.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class SetSpriteFromDrawableFrame : MonoBehaviour -{ - protected DrawableFrame frame; - protected SpriteRenderer spriteRenderer; - - void Start() - { - frame = GetComponent(); - spriteRenderer = GetComponent(); - Sprite sprite = Sprite.Create(frame.texture, new Rect(0, 0, frame.resolution.x, frame.resolution.y), new Vector2(0.5f, 0.5f), frame.resolution.x); - spriteRenderer.sprite = sprite; - } -} diff --git a/Assets/Drawing/Scripts/SetSpriteFromDrawableFrame.cs.meta b/Assets/Drawing/Scripts/SetSpriteFromDrawableFrame.cs.meta deleted file mode 100644 index 2f3b3f1..0000000 --- a/Assets/Drawing/Scripts/SetSpriteFromDrawableFrame.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 21d58fc36c2296c4787f0492c99a5d2c -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Drawing/Scripts/Timer.cs b/Assets/Drawing/Scripts/Timer.cs index 37687fd..a462cff 100644 --- a/Assets/Drawing/Scripts/Timer.cs +++ b/Assets/Drawing/Scripts/Timer.cs @@ -8,8 +8,9 @@ public class Timer : MonoBehaviour { public float startingTime = 0f; public float time = 0f; - public bool isTriggered = false; - public bool isRunning = false; + + private bool isTriggered = false; + private bool isRunning = false; protected void Update() { if(time >= 0f) { @@ -18,25 +19,40 @@ public class Timer : MonoBehaviour } } else { - time = 0f; if(isTriggered) { - OnTimeOut(EventArgs.Empty); + _OnTimeOut(EventArgs.Empty); + time = 0f; isTriggered = false; + isRunning = false; } } } - public void StartTimer(float startingTime) { + public void TimerSet(float startingTime) { isTriggered = true; - isRunning = true; + isRunning = false; this.startingTime = startingTime; time = startingTime; } - public event EventHandler TimeOut; - protected virtual void OnTimeOut(EventArgs e) + public void TimerStart() { + isRunning = true; + } + + public void TimerPause() { + isRunning = false; + } + + public void TimerCancel() { + time = 0f; + isTriggered = false; + isRunning = false; + } + + public event EventHandler OnTimeOut; + protected virtual void _OnTimeOut(EventArgs e) { - EventHandler handler = TimeOut; + EventHandler handler = OnTimeOut; handler?.Invoke(this, e); } } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c3bab48..9bf403d 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -435,5 +435,11 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} + - target: {fileID: 3241962964773812269, guid: 4fd713402edcc1c43b82d4d86a713998, + type: 3} + propertyPath: jsonString + value: '{"startingColor":{"r":1.0,"g":1.0,"b":1.0,"a":1.0},"palette":[{"r":1.0,"g":0.0,"b":0.0,"a":1.0},{"r":1.0,"g":1.0,"b":0.0,"a":1.0},{"r":0.0,"g":1.0,"b":0.0,"a":1.0},{"r":0.0,"g":1.0,"b":1.0,"a":1.0},{"r":0.0,"g":0.0,"b":1.0,"a":1.0},{"r":1.0,"g":0.0,"b":1.0,"a":1.0},{"r":1.0,"g":1.0,"b":1.0,"a":1.0}],"timeLimit":99.0,"actName":"RAINBOWS","actDescription":"Disegna + qualcosa con questi colori superluminosi!"}' + objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 4fd713402edcc1c43b82d4d86a713998, type: 3} diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 6524938..7c1b1e6 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -12,7 +12,7 @@ PlayerSettings: targetDevice: 2 useOnDemandResources: 0 accelerometerFrequency: 60 - companyName: DefaultCompany + companyName: Steffo productName: better-tee defaultCursor: {fileID: 0} cursorHotspot: {x: 0, y: 0}