1
Fork 0
mirror of https://github.com/Steffo99/better-tee.git synced 2024-11-27 17:14:18 +00:00

Refactor A LOT OF CODE

This commit is contained in:
Steffo 2019-09-12 17:37:20 +02:00
parent cfa2d87e85
commit 83a152c3d6
19 changed files with 417 additions and 221 deletions

View file

@ -1,16 +1,18 @@
using System.Collections; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class GameController : MonoBehaviour public class GameController : MonoBehaviour
{ {
void Start() public void NextAct() {
{ //Get act data
string jsonData = "";
ActController.ActSettings unknownSettings = JsonUtility.FromJson<ActController.ActSettings>(jsonData);
} }
void Update() public void SendActResults() {
{
} }
} }

View file

@ -377,6 +377,7 @@ GameObject:
serializedVersion: 6 serializedVersion: 6
m_Component: m_Component:
- component: {fileID: 1832035425} - component: {fileID: 1832035425}
- component: {fileID: 1832035426}
m_Layer: 0 m_Layer: 0
m_Name: Game Controller m_Name: Game Controller
m_TagString: GameController m_TagString: GameController
@ -398,3 +399,15 @@ Transform:
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 1 m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1832035426
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1832035424}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 30f1ce6b92701f848ad6e0c94ec374c2, type: 3}
m_Name:
m_EditorClassIdentifier:

View file

@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public abstract class ActController : MonoBehaviour
{
[Header("Settings")]
public ActSettings settings = null;
protected ActPhase phase = ActPhase.NONE;
[Header("Results")]
public ActResults results = null;
[Header("Objects")]
public Canvas canvas = null;
public EventSystem eventSystem = null;
[Serializable]
public class ActSettings {
public string type;
}
[Serializable]
public class ActResults {
};
public class InvalidPhaseException : Exception {
public readonly ActPhase currentPhase;
public InvalidPhaseException(ActPhase currentPhase) {
this.currentPhase = currentPhase;
}
};
public class MissingSettingsException : Exception {};
[Serializable]
public enum ActPhase {
NONE,
INIT,
START,
END,
CLEANUP
}
public ActPhase Phase { get; }
/// <summary>
/// Call this to initialize the Act (GameObjects, ActSettings, etc). All interactable components should be disabled in this phase.
/// </summary>
public virtual void ActInit() {
phase = ActPhase.INIT;
if(settings == null) {
throw new MissingSettingsException();
}
}
/// <summary>
/// Call this to enable all interactable components in the Act. It should be called when the player is ready to play.
/// </summary>
public virtual void ActStart() {
if(Phase != ActPhase.INIT) throw new InvalidPhaseException(phase);
phase = ActPhase.START;
}
/// <summary>
/// Call this to disable once again all interactable components in the Act. It should be called when the Act is finished (time ran out, player reached submission limit, etc).
/// </summary>
public virtual void ActEnd() {
if(Phase != ActPhase.START) throw new InvalidPhaseException(phase);
phase = ActPhase.END;
}
/// <summary>
/// Call this to cleanup all GameObjects created during the Init phase.
/// </summary>
public virtual void ActCleanup() {
if(Phase != ActPhase.END) {
Debug.LogWarningFormat("ActCleanup() was called during {0}", Phase);
}
phase = ActPhase.CLEANUP;
}
protected virtual void Start() {
canvas = GameObject.FindGameObjectWithTag("Canvas")?.GetComponent<Canvas>();
eventSystem = GameObject.FindGameObjectWithTag("EventSystem")?.GetComponent<EventSystem>();
ActInit();
}
protected virtual void Update() {
}
protected virtual void OnDestroy() {
ActCleanup();
}
}

View file

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 12f494a09d5848e4c8843d84f864396e guid: 1b7c9976c1c705342bf5b92905a0533e
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View file

@ -20,7 +20,7 @@ public class Timer : MonoBehaviour
} }
else { else {
if(isTriggered) { if(isTriggered) {
_OnTimeOut(this, EventArgs.Empty); OnTimeOut();
time = 0f; time = 0f;
isTriggered = false; isTriggered = false;
isRunning = false; isRunning = false;
@ -49,10 +49,5 @@ public class Timer : MonoBehaviour
isRunning = false; isRunning = false;
} }
public event EventHandler OnTimeOut; public event Action OnTimeOut;
protected virtual void _OnTimeOut(object sender, EventArgs e)
{
EventHandler handler = OnTimeOut;
handler?.Invoke(this, e);
}
} }

View file

@ -1,4 +1,3 @@
public class Utils { public class Utils {
public static void Swap<T>(ref T lhs, ref T rhs) { public static void Swap<T>(ref T lhs, ref T rhs) {
T temp; temp = lhs; lhs = rhs; rhs = temp; T temp; temp = lhs; lhs = rhs; rhs = temp;

View file

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

View file

@ -0,0 +1,137 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DrawingController : ActController
{
[Header("Prefabs")]
public GameObject drawableFramePrefab;
public GameObject paletteButtonPrefab;
public GameObject radiusSliderPrefab;
public GameObject actNamePrefab;
public GameObject actDescriptionPrefab;
public GameObject actTimerPrefab;
[Header("Objects")]
protected PencilTool pencil;
protected DrawableFrame drawableFrame;
protected List<PaletteButton> paletteButtons;
protected RadiusSlider radiusSlider;
protected Text actName;
protected Text actDescription;
protected Timer actTimer;
[Serializable]
public class DrawingSettings : ActSettings {
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 DrawingSettings(Color startingColor, List<Color> palette, float timeLimit, string actName, string actDescription) {
this.type = "Drawing";
this.startingColor = startingColor;
this.palette = palette;
this.timeLimit = timeLimit;
this.actName = actName;
this.actDescription = actDescription;
}
}
[Serializable]
public class DrawingResults : ActResults {
public readonly byte[] pngBytes;
public DrawingResults(byte[] pngBytes) {
this.pngBytes = pngBytes;
}
}
protected override void Start() {
base.Start();
}
public override void ActInit() {
base.ActInit();
//Load settings
DrawingSettings drawingSettings = settings as DrawingSettings;
//Create drawable frame
drawableFrame = Instantiate(drawableFramePrefab, transform).GetComponent<DrawableFrame>();
drawableFrame.startingColor = drawingSettings.startingColor;
//Init PencilTool
pencil = drawableFrame.GetComponent<PencilTool>();
try {
pencil.selectedColor = drawingSettings.palette[0];
} catch(ArgumentOutOfRangeException) {
pencil.selectedColor = drawingSettings.startingColor;
}
//Init PaletteButtons
paletteButtons = new List<PaletteButton>();
for(int i = 0; i < drawingSettings.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 = drawingSettings.palette[i];
btnTransform.anchoredPosition = new Vector2(-420 + i * 110, 150);
paletteButtons.Add(button);
}
//Init RadiusSlider
radiusSlider = Instantiate(radiusSliderPrefab, canvas.transform).GetComponent<RadiusSlider>();
radiusSlider.pencil = pencil;
//Init actName Text
actName = Instantiate(actNamePrefab, canvas.transform).GetComponent<Text>();
actName.text = drawingSettings.actName;
//Init actDescription Text
actDescription = Instantiate(actDescriptionPrefab, canvas.transform).GetComponent<Text>();
actDescription.text = drawingSettings.actDescription;
//Init actTimer
actTimer = Instantiate(actTimerPrefab, canvas.transform).GetComponent<Timer>();
actTimer.TimerSet(drawingSettings.timeLimit);
}
public override void ActStart() {
base.ActStart();
//Unlock frame
drawableFrame.locked = false;
//Start timer
actTimer.OnTimeOut += new Action(ActEnd);
actTimer.TimerStart();
}
public override void ActEnd() {
base.ActEnd();
//Lock frame
drawableFrame.locked = true;
//Generate results
results = new DrawingResults(drawableFrame.ToPNG());
}
public override void ActCleanup() {
base.ActCleanup();
Destroy(drawableFrame.gameObject);
foreach(PaletteButton button in paletteButtons) {
Destroy(button.gameObject);
}
Destroy(radiusSlider.gameObject);
Destroy(actName.gameObject);
Destroy(actDescription.gameObject);
Destroy(actTimer.gameObject);
}
}

View file

@ -1,101 +0,0 @@
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;
[Header("Results")]
public byte[] png = null;
void Start() {
if(jsonString != "") {
JsonUtility.FromJsonOverwrite(jsonString, settings);
if(settings == null) {
Debug.LogWarning("Invalid settings json string, using defaults.");
}
}
else {
Debug.Log(JsonUtility.ToJson(settings));
}
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>();
timer.TimerSet(settings.timeLimit);
timer.OnTimeOut += ActEnd;
ActStart();
}
protected void ActStart() {
timer.TimerStart();
drawableFrame.locked = false;
}
protected void ActEnd(object sender, EventArgs e) {
drawableFrame.locked = true;
png = drawableFrame.ToPNG();
}
}

View file

@ -7,7 +7,7 @@ using UnityEngine.EventSystems;
public class Submit : MonoBehaviour public class Submit : MonoBehaviour
{ {
public InputField inputField; public InputField inputField;
public TypingManager typingManager; public TypingController typingController;
protected EventSystem eventSystem; protected EventSystem eventSystem;
@ -16,7 +16,7 @@ public class Submit : MonoBehaviour
} }
public void OnClick() { public void OnClick() {
typingManager.SubmitText(inputField.text); typingController.Submit(inputField.text);
inputField.text = ""; inputField.text = "";
eventSystem.SetSelectedGameObject(inputField.gameObject); eventSystem.SetSelectedGameObject(inputField.gameObject);
} }

View file

@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class TypingController : ActController
{
public List<String> submissions;
[Header("Prefabs")]
public GameObject actNamePrefab;
public GameObject actDescriptionPrefab;
public GameObject timerPrefab;
public GameObject submissionFieldPrefab;
public GameObject submitPrefab;
public GameObject submittedCountPrefab;
[Header("Objects")]
protected Text actName;
protected Text actDescription;
protected Timer actTimer;
protected InputField submissionField;
protected Submit submit;
protected Text submittedCount;
[Serializable]
public class TypingSettings : ActSettings {
public float timeLimit = 99f;
public string actName = "Untitled";
public string actDescription = "This Act is missing a description.";
public TypingSettings(float timeLimit, string actName, string actDescription) {
this.type = "Typing";
this.timeLimit = timeLimit;
this.actName = actName;
this.actDescription = actDescription;
}
}
[Serializable]
public class TypingResults : ActResults {
public List<string> submissions;
public TypingResults(List<string> submissions) {
this.submissions = submissions;
}
}
protected override void Start() {
base.Start();
submissions = new List<string>();
}
public override void ActInit() {
base.ActInit();
//Load settings
TypingSettings typingSettings = settings as TypingSettings;
//Init actName Text
actName = Instantiate(actNamePrefab, canvas.transform).GetComponent<Text>();
actName.text = typingSettings.actName;
//Init actDescription Text
actDescription = Instantiate(actDescriptionPrefab, canvas.transform).GetComponent<Text>();
actDescription.text = typingSettings.actDescription;
//Init actTimer
actTimer = Instantiate(timerPrefab, canvas.transform).GetComponent<Timer>();
actTimer.TimerSet(typingSettings.timeLimit);
//Init submissionInputField
submissionField = Instantiate(submissionFieldPrefab, canvas.transform).GetComponent<InputField>();
//Init submit Button
submit = Instantiate(submitPrefab, canvas.transform).GetComponent<Submit>();
submit.typingController = this;
submit.inputField = submissionField;
//Init submissionCount Text
submittedCount = Instantiate(submittedCountPrefab, canvas.transform).GetComponent<Text>();
submittedCount.text = "";
}
public override void ActStart() {
base.ActStart();
//Enable submissionField and submit Button
submissionField.interactable = true;
submit.GetComponent<Button>().interactable = true;
//Focus on submissionField
eventSystem.SetSelectedGameObject(submissionField.gameObject);
//Start timer
actTimer.OnTimeOut += new Action(ActEnd);
actTimer.TimerStart();
}
public override void ActEnd() {
base.ActEnd();
//Disable submissionField and submit Button
submissionField.interactable = false;
submit.GetComponent<Button>().interactable = false;
//Create results
results = new TypingResults(submissions);
}
public override void ActCleanup() {
base.ActCleanup();
Destroy(actName.gameObject);
Destroy(actDescription.gameObject);
Destroy(actTimer.gameObject);
Destroy(submissionField.gameObject);
Destroy(submit.gameObject);
Destroy(submittedCount.gameObject);
}
public void Submit(string submission) {
if(submission != "") {
submissions.Add(submission);
submittedCount.text = String.Format("Submitted: {0}", submissions.Count);
}
}
}

View file

@ -1,100 +0,0 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
[Serializable]
public class TypingSettings {
public float timeLimit = 99f;
public string actName = "Untitled";
public string actDescription = "This Act is missing a description.";
}
public class TypingManager : MonoBehaviour
{
[Header("Settings")]
public string jsonString = "";
public TypingSettings settings = null;
[Header("Prefabs")]
public GameObject actNamePrefab;
public GameObject actDescriptionPrefab;
public GameObject timerPrefab;
public GameObject inputFieldPrefab;
public GameObject submitPrefab;
public GameObject submittedCountPrefab;
[Header("Objects")]
protected Text actName;
protected Text actDescription;
protected Timer timer;
protected Canvas canvas;
protected InputField inputField;
protected Submit submit;
protected Text submittedCount;
protected EventSystem eventSystem;
[Header("Results")]
public List<string> texts;
protected void Start() {
if(jsonString != "") {
JsonUtility.FromJsonOverwrite(jsonString, settings);
if(settings == null) {
Debug.LogWarning("Invalid settings json string, using defaults.");
}
}
else {
Debug.Log(JsonUtility.ToJson(settings));
}
texts = new List<string>();
canvas = GameObject.FindGameObjectWithTag("Canvas").GetComponent<Canvas>();
eventSystem = GameObject.FindGameObjectWithTag("EventSystem").GetComponent<EventSystem>();
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>();
timer.TimerSet(settings.timeLimit);
timer.OnTimeOut += ActEnd;
inputField = Instantiate(inputFieldPrefab, canvas.transform).GetComponent<InputField>();
Submit inputFieldSubmit = inputField.GetComponent<Submit>();
inputFieldSubmit.typingManager = this;
inputFieldSubmit.inputField = inputField;
submit = Instantiate(submitPrefab, canvas.transform).GetComponent<Submit>();
submit.typingManager = this;
submit.inputField = inputField;
submittedCount = Instantiate(submittedCountPrefab, canvas.transform).GetComponent<Text>();
submittedCount.text = "";
ActStart();
}
protected void ActStart() {
timer.TimerStart();
eventSystem.SetSelectedGameObject(inputField.gameObject);
inputField.interactable = true;
submit.GetComponent<Button>().interactable = true;
}
protected void ActEnd(object sender, EventArgs e) {
inputField.interactable = false;
submit.GetComponent<Button>().interactable = false;
}
public void SubmitText(string text) {
if(text != "") {
texts.Add(text);
submittedCount.text = String.Format("Submitted: {0}", texts.Count);
}
}
}

View file

@ -3,19 +3,27 @@
--- !u!159 &1 --- !u!159 &1
EditorSettings: EditorSettings:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
serializedVersion: 7 serializedVersion: 9
m_ExternalVersionControlSupport: Visible Meta Files m_ExternalVersionControlSupport: Visible Meta Files
m_SerializationMode: 2 m_SerializationMode: 2
m_LineEndingsForNewScripts: 0 m_LineEndingsForNewScripts: 0
m_DefaultBehaviorMode: 1 m_DefaultBehaviorMode: 1
m_PrefabRegularEnvironment: {fileID: 0}
m_PrefabUIEnvironment: {fileID: 0}
m_SpritePackerMode: 4 m_SpritePackerMode: 4
m_SpritePackerPaddingPower: 1 m_SpritePackerPaddingPower: 1
m_EtcTextureCompressorBehavior: 1 m_EtcTextureCompressorBehavior: 1
m_EtcTextureFastCompressor: 1 m_EtcTextureFastCompressor: 1
m_EtcTextureNormalCompressor: 2 m_EtcTextureNormalCompressor: 2
m_EtcTextureBestCompressor: 4 m_EtcTextureBestCompressor: 4
m_ProjectGenerationIncludedExtensions: txt;xml;fnt;cd m_ProjectGenerationIncludedExtensions: txt;xml;fnt;cd;asmref
m_ProjectGenerationRootNamespace: m_ProjectGenerationRootNamespace:
m_UserGeneratedProjectSuffix:
m_CollabEditorSettings: m_CollabEditorSettings:
inProgressEnabled: 1 inProgressEnabled: 1
m_EnableTextureStreamingInEditMode: 1
m_EnableTextureStreamingInPlayMode: 1
m_AsyncShaderCompilation: 1
m_EnterPlayModeOptionsEnabled: 0
m_EnterPlayModeOptions: 3
m_ShowLightmapResolutionOverlay: 1
m_UseLegacyProbeSampleCount: 1