1
Fork 0
mirror of https://github.com/Steffo99/better-tee.git synced 2024-11-21 23:04:19 +00:00

Add multitouch

This commit is contained in:
Steffo 2019-09-11 15:51:05 +02:00
parent 815ea194c0
commit 4a3b3fb339
9 changed files with 93 additions and 63 deletions

View file

@ -11,7 +11,6 @@ GameObject:
- component: {fileID: 6807415978649219551} - component: {fileID: 6807415978649219551}
- component: {fileID: 6807415978649219550} - component: {fileID: 6807415978649219550}
- component: {fileID: 6807415978649219545} - component: {fileID: 6807415978649219545}
- component: {fileID: 6807415978649219544}
- component: {fileID: 6807415977000239074} - component: {fileID: 6807415977000239074}
m_Layer: 0 m_Layer: 0
m_Name: Drawable Frame m_Name: Drawable Frame
@ -98,20 +97,8 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
resolution: {x: 500, y: 500} resolution: {x: 500, y: 500}
startingColor: {r: 0, g: 0, b: 0, a: 0} startingColor: {r: 0, g: 0, b: 0, a: 0}
texture: {fileID: 0} locked: 0
sprite: {fileID: 0} hasChanged: 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:
--- !u!114 &6807415977000239074 --- !u!114 &6807415977000239074
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View file

@ -8,9 +8,13 @@ public class DrawableFrame : MonoBehaviour
public Vector2Int resolution; public Vector2Int resolution;
public Color startingColor = Color.white; public Color startingColor = Color.white;
[Header("State")]
public bool locked = false;
public bool hasChanged = false;
[Header("References")] [Header("References")]
public Texture2D texture = null; protected Texture2D texture = null;
public Sprite sprite = null; protected Sprite sprite = null;
private SpriteRenderer spriteRenderer; private SpriteRenderer spriteRenderer;
@ -45,6 +49,23 @@ public class DrawableFrame : MonoBehaviour
spriteRenderer.sprite = sprite; 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() protected void OnDrawGizmos()
{ {
Gizmos.DrawWireCube(transform.position, transform.localScale); Gizmos.DrawWireCube(transform.position, transform.localScale);

View file

@ -43,6 +43,9 @@ public class DrawingManager : MonoBehaviour
Debug.LogWarning("Invalid settings json string, using defaults."); Debug.LogWarning("Invalid settings json string, using defaults.");
} }
} }
else {
Debug.Log(JsonUtility.ToJson(settings));
}
canvas = GameObject.FindGameObjectWithTag("Canvas").GetComponent<Canvas>(); canvas = GameObject.FindGameObjectWithTag("Canvas").GetComponent<Canvas>();
@ -77,6 +80,9 @@ public class DrawingManager : MonoBehaviour
actDescription.text = settings.actDescription; actDescription.text = settings.actDescription;
timer = Instantiate(timerPrefab, canvas.transform).GetComponent<Timer>(); timer = Instantiate(timerPrefab, canvas.transform).GetComponent<Timer>();
timer.StartTimer(settings.timeLimit); timer.TimerSet(settings.timeLimit);
//TODO: replace with a countdown or something
timer.TimerStart();
} }
} }

View file

@ -12,8 +12,8 @@ public class PencilTool : DrawTool
private Vector2Int? previousPixel; private Vector2Int? previousPixel;
private Vector2Int? pixel; private Vector2Int? pixel;
protected Vector2Int? HoveredPixel() { protected Vector2Int? GetPixelAtScreenPosition(Vector2 screenPosition) {
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 worldPoint = Camera.main.ScreenToWorldPoint(screenPosition);
if(frame.Bounds.Contains(worldPoint)) { if(frame.Bounds.Contains(worldPoint)) {
Vector2 normalized = Rect.PointToNormalized(frame.Bounds, 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)); 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() { protected override void Start() {
base.Start(); base.Start();
colors = frame.texture.GetPixels(); colors = frame.GetPixels();
Input.simulateMouseWithTouches = false;
} }
protected void Update() { 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; previousPixel = pixel;
if(Input.GetMouseButton(0)) { if(Input.GetMouseButton(0)) {
pixel = HoveredPixel(); pixel = GetPixelAtScreenPosition(Input.mousePosition);
if(pixel.HasValue) { if(pixel.HasValue) {
if(previousPixel.HasValue) { if(previousPixel.HasValue) {
DrawBresenhamsThickLine(previousPixel.Value, pixel.Value, size, selectedColor); DrawBresenhamsThickLine(previousPixel.Value, pixel.Value, size, selectedColor);
@ -39,11 +59,15 @@ public class PencilTool : DrawTool
DrawFilledCircle(pixel.Value, size, selectedColor); DrawFilledCircle(pixel.Value, size, selectedColor);
} }
} }
Apply(); hasChanged = true;
} }
else { else {
pixel = null; pixel = null;
} }
if(hasChanged) {
frame.SetPixels(colors);
}
} }
protected void DrawPixel(int x, int y, Color color) { protected void DrawPixel(int x, int y, Color color) {
@ -124,7 +148,5 @@ public class PencilTool : DrawTool
} }
protected void Apply() { protected void Apply() {
frame.texture.SetPixels(colors);
frame.texture.Apply();
} }
} }

View file

@ -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<DrawableFrame>();
spriteRenderer = GetComponent<SpriteRenderer>();
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;
}
}

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 21d58fc36c2296c4787f0492c99a5d2c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -8,8 +8,9 @@ public class Timer : MonoBehaviour
{ {
public float startingTime = 0f; public float startingTime = 0f;
public float time = 0f; public float time = 0f;
public bool isTriggered = false;
public bool isRunning = false; private bool isTriggered = false;
private bool isRunning = false;
protected void Update() { protected void Update() {
if(time >= 0f) { if(time >= 0f) {
@ -18,25 +19,40 @@ public class Timer : MonoBehaviour
} }
} }
else { else {
time = 0f;
if(isTriggered) { if(isTriggered) {
OnTimeOut(EventArgs.Empty); _OnTimeOut(EventArgs.Empty);
time = 0f;
isTriggered = false; isTriggered = false;
isRunning = false;
} }
} }
} }
public void StartTimer(float startingTime) { public void TimerSet(float startingTime) {
isTriggered = true; isTriggered = true;
isRunning = true; isRunning = false;
this.startingTime = startingTime; this.startingTime = startingTime;
time = startingTime; time = startingTime;
} }
public event EventHandler TimeOut; public void TimerStart() {
protected virtual void OnTimeOut(EventArgs e) 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); handler?.Invoke(this, e);
} }
} }

View file

@ -435,5 +435,11 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z propertyPath: m_LocalEulerAnglesHint.z
value: 0 value: 0
objectReference: {fileID: 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_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 4fd713402edcc1c43b82d4d86a713998, type: 3} m_SourcePrefab: {fileID: 100100000, guid: 4fd713402edcc1c43b82d4d86a713998, type: 3}

View file

@ -12,7 +12,7 @@ PlayerSettings:
targetDevice: 2 targetDevice: 2
useOnDemandResources: 0 useOnDemandResources: 0
accelerometerFrequency: 60 accelerometerFrequency: 60
companyName: DefaultCompany companyName: Steffo
productName: better-tee productName: better-tee
defaultCursor: {fileID: 0} defaultCursor: {fileID: 0}
cursorHotspot: {x: 0, y: 0} cursorHotspot: {x: 0, y: 0}