1
Fork 0
mirror of https://github.com/Steffo99/better-tee.git synced 2024-11-26 16:54:17 +00:00
better-tee/Assets/Drawing/Scripts/DrawingManager.cs

102 lines
3.4 KiB
C#
Raw Normal View History

2019-09-11 13:13:31 +00:00
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[Serializable]
public class DrawingSettings {
public Color startingColor = Color.white;
public List<Color> palette = new List<Color>();
public float timeLimit = 99f;
public string actName = "Untitled";
public string actDescription = "This Act is missing a description.";
}
public class DrawingManager : MonoBehaviour
{
[Header("Settings")]
public string jsonString = "";
public DrawingSettings settings = null;
[Header("Prefabs")]
public GameObject drawableFramePrefab;
public GameObject paletteButtonPrefab;
public GameObject radiusSliderPrefab;
public GameObject actNamePrefab;
public GameObject actDescriptionPrefab;
public GameObject timerPrefab;
[Header("Objects")]
protected Canvas canvas;
protected PencilTool pencil;
protected DrawableFrame drawableFrame;
protected List<PaletteButton> paletteButtons;
protected RadiusSlider radiusSlider;
protected Text actName;
protected Text actDescription;
protected Timer timer;
2019-09-11 14:12:59 +00:00
[Header("Results")]
public byte[] png = null;
2019-09-11 13:13:31 +00:00
void Start() {
if(jsonString != "") {
JsonUtility.FromJsonOverwrite(jsonString, settings);
if(settings == null) {
Debug.LogWarning("Invalid settings json string, using defaults.");
}
}
2019-09-11 13:51:05 +00:00
else {
Debug.Log(JsonUtility.ToJson(settings));
}
2019-09-11 13:13:31 +00:00
canvas = GameObject.FindGameObjectWithTag("Canvas").GetComponent<Canvas>();
drawableFrame = Instantiate(drawableFramePrefab, transform).GetComponent<DrawableFrame>();
drawableFrame.startingColor = settings.startingColor;
pencil = drawableFrame.GetComponent<PencilTool>();
try {
pencil.selectedColor = settings.palette[0];
} catch(ArgumentOutOfRangeException) {
pencil.selectedColor = settings.startingColor;
}
paletteButtons = new List<PaletteButton>();
for(int i = 0; i < settings.palette.Count; i++) {
PaletteButton button = Instantiate(paletteButtonPrefab, canvas.transform).GetComponent<PaletteButton>();
RectTransform btnTransform = button.GetComponent<RectTransform>();
Image btnImage = button.GetComponent<Image>();
button.pencil = pencil;
btnImage.color = settings.palette[i];
btnTransform.anchoredPosition = new Vector2(-420 + i * 110, 150);
paletteButtons.Add(button);
}
radiusSlider = Instantiate(radiusSliderPrefab, canvas.transform).GetComponent<RadiusSlider>();
radiusSlider.pencil = pencil;
actName = Instantiate(actNamePrefab, canvas.transform).GetComponent<Text>();
actName.text = settings.actName;
actDescription = Instantiate(actDescriptionPrefab, canvas.transform).GetComponent<Text>();
actDescription.text = settings.actDescription;
timer = Instantiate(timerPrefab, canvas.transform).GetComponent<Timer>();
2019-09-11 13:51:05 +00:00
timer.TimerSet(settings.timeLimit);
2019-09-11 14:12:59 +00:00
timer.OnTimeOut += ActEnd;
ActStart();
}
2019-09-11 13:51:05 +00:00
2019-09-11 17:46:01 +00:00
protected void ActStart() {
2019-09-11 13:51:05 +00:00
timer.TimerStart();
2019-09-11 14:12:59 +00:00
drawableFrame.locked = false;
}
2019-09-11 17:46:01 +00:00
protected void ActEnd(object sender, EventArgs e) {
2019-09-11 14:12:59 +00:00
drawableFrame.locked = true;
png = drawableFrame.ToPNG();
2019-09-11 13:13:31 +00:00
}
}