Last active
January 16, 2020 05:56
-
-
Save jcarcangiu/ecf02db7963f8c261ca33863379f82b6 to your computer and use it in GitHub Desktop.
WoF - User App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using Vuforia; | |
public class Camera_Focus : MonoBehaviour | |
{ | |
void Start() | |
{ | |
var vuforia = VuforiaARController.Instance; | |
vuforia.RegisterVuforiaStartedCallback(OnVuforiaStarted); | |
vuforia.RegisterOnPauseCallback(OnPaused); | |
} | |
private void OnVuforiaStarted() | |
{ | |
CameraDevice.Instance.SetFocusMode( | |
CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO); | |
} | |
private void OnPaused(bool paused) | |
{ | |
if (!paused) // resumed | |
{ | |
// Set again autofocus mode when app is resumed | |
CameraDevice.Instance.SetFocusMode( | |
CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.IO; | |
using System; | |
using System.Linq; | |
public class JC_CSVReader : MonoBehaviour | |
{ | |
public TextAsset csvFileImport; | |
public TextAsset csvFileExport; | |
public JC_Data[] allDataSetsImport; | |
public JC_Data[] allDataSetsExport; | |
public long meanImport; | |
public void Awake() | |
{ | |
string[,] gridImport = SplitCsvGrid(csvFileImport.text); | |
//Debug.Log("size = " + (1 + gridImport.GetUpperBound(0)) + "," + (1 + gridImport.GetUpperBound(1))); | |
allDataSetsImport = new JC_Data[gridImport.GetUpperBound(1) - 2]; | |
InitializeDataSet(gridImport, allDataSetsImport); | |
ConvertData(gridImport, allDataSetsImport); | |
string[,] gridExport = SplitCsvGrid(csvFileExport.text); | |
//Debug.Log("size = " + (1 + gridExport.GetUpperBound(0)) + "," + (1 + gridExport.GetUpperBound(1))); | |
allDataSetsExport = new JC_Data[gridExport.GetUpperBound(1) - 2]; | |
InitializeDataSet(gridExport, allDataSetsExport); | |
ConvertData(gridExport, allDataSetsExport); | |
} | |
void InitializeDataSet(string[,] grid, JC_Data[] dataSet) | |
{ | |
for (int y = 0; y < grid.GetUpperBound(1) - 2; y++) | |
{ | |
if (dataSet[y] == null) | |
{ | |
dataSet[y] = new JC_Data(); | |
} | |
if (dataSet[y].yearsData == null) | |
{ | |
dataSet[y].yearsData = new float[grid.GetUpperBound(0) - 5, 2]; | |
} | |
} | |
foreach (JC_Data data in dataSet) | |
{ | |
for (int x = 0; x < grid.GetUpperBound(0) - 5; x++) | |
{ | |
data.yearsData[x, 0] = float.Parse(grid[x, 0]); | |
} | |
} | |
int _y = 1; | |
foreach (JC_Data data in dataSet) | |
{ | |
data.countryName = grid[grid.GetUpperBound(0) - 5, _y]; | |
_y++; | |
} | |
} | |
void ConvertData(string[,] grid, JC_Data[] dataSet) | |
{ | |
for (int y = 1; y < grid.GetUpperBound(1) - 1; y++) | |
{ | |
for (int x = 0; x < grid.GetUpperBound(0) - 5; x++) | |
{ | |
if (grid[x, y] != "") | |
{ | |
dataSet[y - 1].yearsData[x, 1] = float.Parse(grid[x, y]); | |
} | |
else | |
{ | |
dataSet[y - 1].yearsData[x, 1] = 0; | |
} | |
} | |
} | |
meanImport = CalculateMean(); | |
//print(CalculateMean()); | |
} | |
long CalculateMean() | |
{ | |
float _sum = 0; | |
float _amount = 0; | |
float _mean = 0; | |
foreach (JC_Data data in allDataSetsImport) | |
{ | |
for (int x = 0; x < data.yearsData.GetUpperBound(0); x++) | |
{ | |
_amount++; | |
_sum += data.yearsData[x, 1]; | |
} | |
} | |
_mean = _sum / _amount; | |
return (long)_mean; | |
} | |
// Outputs the content of a 2D array, useful for checking the importer. | |
static public void DebugOutputGrid(string[,] grid) | |
{ | |
string textOutput = ""; | |
for (int y = 0; y < grid.GetUpperBound(1); y++) | |
{ | |
for (int x = 0; x < grid.GetUpperBound(0); x++) | |
{ | |
textOutput += grid[x, y]; | |
textOutput += "|"; | |
} | |
textOutput += "\n"; | |
} | |
Debug.Log(textOutput); | |
} | |
// Splits a CSV file into a 2D string array. | |
static public string[,] SplitCsvGrid(string csvText) | |
{ | |
string[] lines = csvText.Split("\n"[0]); | |
// Finds the max width of row. | |
int width = 0; | |
for (int i = 0; i < lines.Length; i++) | |
{ | |
string[] row = SplitCsvLine(lines[i]); | |
width = Mathf.Max(width, row.Length); | |
} | |
// Creates new 2D string grid to output to. | |
string[,] outputGrid = new string[width + 1, lines.Length + 1]; | |
for (int y = 0; y < lines.Length; y++) | |
{ | |
string[] row = SplitCsvLine(lines[y]); | |
for (int x = 0; x < row.Length; x++) | |
{ | |
outputGrid[x, y] = row[x]; | |
// This line was to replace "" with " in my output. | |
// Include or edit it as you wish. | |
outputGrid[x, y] = outputGrid[x, y].Replace("\"\"", "\""); | |
} | |
} | |
return outputGrid; | |
} | |
// Splits a CSV row. | |
static public string[] SplitCsvLine(string line) | |
{ | |
return (from System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches(line, | |
@"(((?<x>(?=[,\r\n]+))|""(?<x>([^""]|"""")+)""|(?<x>[^,\r\n]+)),?)", | |
System.Text.RegularExpressions.RegexOptions.ExplicitCapture) | |
select m.Groups[1].Value).ToArray(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[System.Serializable] | |
public class JC_Data | |
{ | |
public string countryName; | |
public float[,] yearsData; | |
public JC_Data() | |
{ | |
countryName = ""; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class JC_ImageViewer : MonoBehaviour | |
{ | |
private JC_SliderBar_VuforiaOverride[] allTrackingObjs; | |
public Canvas imageViewer; | |
public RawImage rawImage; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
imageViewer.gameObject.SetActive(false); | |
allTrackingObjs = FindObjectsOfType<JC_SliderBar_VuforiaOverride>(); | |
StartCoroutine(CheckForTrackers(0.1f)); | |
} | |
public void OpenImage() | |
{ | |
if (!imageViewer.gameObject.activeInHierarchy) | |
{ | |
imageViewer.gameObject.SetActive(true); | |
} | |
} | |
public void CloseImage() | |
{ | |
if (imageViewer.gameObject.activeInHierarchy) | |
{ | |
imageViewer.gameObject.SetActive(false); | |
} | |
} | |
IEnumerator CheckForTrackers(float vCoroutineRate) | |
{ | |
while (true) | |
{ | |
int trackedObjs = 0; | |
for (int i = 0; i < allTrackingObjs.Length; i++) | |
{ | |
if (allTrackingObjs[i].tracking) | |
{ | |
trackedObjs++; | |
} | |
} | |
if (trackedObjs == 0) | |
{ | |
imageViewer.gameObject.SetActive(false); | |
} | |
yield return new WaitForSeconds(vCoroutineRate); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class JC_PlayPause : MonoBehaviour | |
{ | |
private JC_SliderBar_VuforiaOverride[] allTrackingObjs; | |
public Button pause; | |
public Button play; | |
public JC_ScaleWithButtons buttonScaler; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
allTrackingObjs = FindObjectsOfType<JC_SliderBar_VuforiaOverride>(); | |
StartCoroutine(CheckForTrackers(0.1f)); | |
} | |
private void Update() | |
{ | |
if (buttonScaler.useButtons) | |
{ | |
pause.gameObject.SetActive(false); | |
play.gameObject.SetActive(true); | |
} | |
else | |
{ | |
pause.gameObject.SetActive(true); | |
play.gameObject.SetActive(false); | |
} | |
} | |
IEnumerator CheckForTrackers(float vCoroutineRate) | |
{ | |
while (true) | |
{ | |
int trackedObjs = 0; | |
for (int i = 0; i < allTrackingObjs.Length; i++) | |
{ | |
if (allTrackingObjs[i].tracking) | |
{ | |
trackedObjs++; | |
} | |
} | |
if (trackedObjs == 0) | |
{ | |
gameObject.GetComponent<Canvas>().enabled = false; | |
} | |
else | |
{ | |
gameObject.GetComponent<Canvas>().enabled = true; | |
} | |
yield return new WaitForSeconds(vCoroutineRate); | |
} | |
} | |
public JC_SpriteScaler[] allSprites; | |
public void Play() | |
{ | |
foreach (JC_SpriteScaler scaler in allSprites) | |
{ | |
if (buttonScaler.useButtons) | |
{ | |
buttonScaler.useButtons = !buttonScaler.useButtons; | |
} | |
} | |
} | |
public void Pause() | |
{ | |
foreach (JC_SpriteScaler scaler in allSprites) | |
{ | |
if (!buttonScaler.useButtons) | |
{ | |
buttonScaler.useButtons = !buttonScaler.useButtons; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using Vuforia; | |
public class JC_SliderBar_VuforiaOverride : DefaultTrackableEventHandler | |
{ | |
public bool tracking = false; | |
protected override void OnTrackingLost() | |
{ | |
base.OnTrackingLost(); | |
tracking = false; | |
} | |
protected override void OnTrackingFound() | |
{ | |
base.OnTrackingFound(); | |
tracking = true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Video; | |
using UnityEngine.UI; | |
using System.Collections; | |
using System.Collections.Generic; | |
using Vuforia; | |
public class JC_VideoContoller : MonoBehaviour | |
{ | |
// Camera to render the video to. | |
public Canvas playPauseCanvas; | |
public Canvas fullscreenCanvas; | |
public Button playButton; | |
public Button fullscreenResumeButton; | |
public Button fullscreenPauseButton; | |
public float fullScreenPauseButtonShowTime = 2.5f; | |
public RectTransform progressBar; | |
public VideoPlayer videoPlayer; | |
public ImageTargetBehaviour[] allImageTargets; | |
public bool isLoadedFromFolder = false; | |
public string url = "/Users/Jasmine/Videos/Captures/Unity 2018.3.6f1 Education - TestingScene.unity - Testing0.1 - Android _DX11 on DX9 GPU_ 2019-04-28 01-54-59.mp4"; | |
private bool videoRollBack = false; | |
// New stuff: add video as link, add video in a specific folder in the build. (Rename videos!) | |
// https://answers.unity.com/questions/1432801/dynamicaly-load-a-video-on-a-gameobject.html | |
// https://www.youtube.com/watch?v=Or8yDLsYxSw | |
private void Awake() | |
{ | |
videoPlayer.Prepare(); | |
videoPlayer.frame = 0; | |
} | |
// Start is called before the first frame update | |
void Start() | |
{ | |
Input.simulateMouseWithTouches = true; | |
allImageTargets = FindObjectsOfType<ImageTargetBehaviour>(); | |
if (isLoadedFromFolder) | |
{ | |
videoPlayer.source = VideoSource.Url; | |
videoPlayer.url = url; | |
} | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
// If video is enables in fullScreen | |
if (fullscreenCanvas.gameObject.activeInHierarchy) | |
{ | |
// If the video is playing | |
if (videoPlayer.isPlaying) | |
{ | |
// Show video progress with the bar. | |
if (!videoRollBack) | |
{ | |
ProgressBarPlay(); | |
} | |
VideoRollback(); | |
} | |
if (!fullscreenPauseButton.gameObject.activeInHierarchy) | |
{ | |
StopAllCoroutines(); | |
} | |
} | |
if (fullscreenPauseButton.gameObject.activeInHierarchy) | |
{ | |
fullscreenResumeButton.gameObject.SetActive(false); | |
} | |
if (fullscreenResumeButton.gameObject.activeInHierarchy) | |
{ | |
fullscreenPauseButton.gameObject.SetActive(false); | |
} | |
} | |
#region Canvas Management | |
public void ActivateCanvases(bool vPlayPause) | |
{ | |
if (vPlayPause) | |
{ | |
playPauseCanvas.gameObject.SetActive(true); | |
fullscreenCanvas.gameObject.SetActive(false); | |
} | |
else | |
{ | |
playPauseCanvas.gameObject.SetActive(false); | |
fullscreenCanvas.gameObject.SetActive(true); | |
} | |
} | |
public void SwitchCanvases() | |
{ | |
// Disable PlayPause Canvas. | |
if (playPauseCanvas.gameObject.activeInHierarchy && !fullscreenCanvas.gameObject.activeInHierarchy) | |
{ | |
playPauseCanvas.gameObject.SetActive(false); | |
fullscreenCanvas.gameObject.SetActive(true); | |
} | |
// Enable Fullscreen Canvas. | |
else if (!playPauseCanvas.gameObject.activeInHierarchy && fullscreenCanvas.gameObject.activeInHierarchy) | |
{ | |
fullscreenCanvas.gameObject.SetActive(false); | |
playPauseCanvas.gameObject.SetActive(true); | |
} | |
} | |
public void DisableCanvasesOnTrackingLost() | |
{ | |
playPauseCanvas.gameObject.SetActive(false); | |
fullscreenCanvas.gameObject.SetActive(false); | |
} | |
public void StartPauseCoroutine() | |
{ | |
//print("StartPauseCoroutine"); | |
StartCoroutine("ShowFullscreenPauseButton", fullScreenPauseButtonShowTime); | |
} | |
IEnumerator ShowFullscreenPauseButton(float vAmountOfSecs) | |
{ | |
while (vAmountOfSecs <= fullScreenPauseButtonShowTime) | |
{ | |
if (vAmountOfSecs == fullScreenPauseButtonShowTime) | |
{ | |
if (!fullscreenPauseButton.gameObject.activeInHierarchy && !fullscreenResumeButton.gameObject.activeInHierarchy) | |
{ | |
//Show Fullscreen Button for 5 Seconds. | |
fullscreenPauseButton.gameObject.SetActive(true); | |
vAmountOfSecs -= Time.deltaTime; | |
} | |
} | |
else | |
{ | |
vAmountOfSecs -= Time.deltaTime; | |
if (vAmountOfSecs < 0.1f) | |
{ | |
if (fullscreenPauseButton.gameObject.activeInHierarchy) | |
{ | |
fullscreenPauseButton.gameObject.SetActive(false); | |
} | |
} | |
} | |
yield return null; | |
} | |
} | |
#endregion | |
#region Play & Pause Video | |
// Play full screen video. | |
public void PlayFullscreen() | |
{ | |
//print("Play Fullscreen"); | |
SwitchCanvases(); | |
videoPlayer.Play(); | |
} | |
public void Pause() | |
{ | |
//print("Pause Video"); | |
if (videoPlayer.isPlaying) | |
{ | |
fullscreenPauseButton.gameObject.SetActive(false); | |
videoPlayer.Pause(); | |
fullscreenResumeButton.gameObject.SetActive(true); | |
} | |
} | |
public void Resume() | |
{ | |
//print("Resume Video"); | |
if (videoPlayer.isPaused) | |
{ | |
fullscreenResumeButton.gameObject.SetActive(false); | |
videoPlayer.Play(); | |
} | |
} | |
public void Quit() | |
{ | |
//print("Quit Video"); | |
if (videoPlayer.isPlaying) | |
{ | |
videoPlayer.Stop(); | |
} | |
if (fullscreenPauseButton.gameObject.activeInHierarchy) | |
{ | |
fullscreenPauseButton.gameObject.SetActive(false); | |
} | |
if (fullscreenResumeButton.gameObject.activeInHierarchy) | |
{ | |
fullscreenResumeButton.gameObject.SetActive(false); | |
} | |
SwitchCanvases(); | |
} | |
#endregion | |
#region Video Playbar & Rollback | |
void VideoRollback() | |
{ | |
if (Input.touchCount > 0) | |
{ | |
Touch touch = Input.GetTouch(0); | |
if (touch.phase == TouchPhase.Began) | |
{ | |
if (touch.position.y < 60) | |
{ | |
videoRollBack = true; | |
float frame = (float)videoPlayer.frame; | |
float count = (float)videoPlayer.frameCount; | |
if (progressBar != null) | |
{ | |
progressBar.sizeDelta = new Vector2(GetProgressBarPercentage() * (Screen.width / 100), progressBar.sizeDelta.y); | |
float estimatedFrame = ((float)videoPlayer.frameCount / 100) * GetProgressBarPercentage(); | |
videoPlayer.frame = (int)estimatedFrame; | |
videoRollBack = false; | |
videoPlayer.Play(); | |
} | |
} | |
} | |
if (touch.phase == TouchPhase.Moved) | |
{ | |
if (touch.position.y < 60) | |
{ | |
videoRollBack = true; | |
float frame = (float)videoPlayer.frame; | |
float count = (float)videoPlayer.frameCount; | |
if (progressBar != null) | |
{ | |
progressBar.sizeDelta = new Vector2(GetProgressBarPercentage() * (Screen.width / 100), progressBar.sizeDelta.y); | |
float estimatedFrame = ((float)videoPlayer.frameCount / 100) * GetProgressBarPercentage(); | |
videoPlayer.frame = (int)estimatedFrame; | |
videoRollBack = false; | |
videoPlayer.Play(); | |
} | |
} | |
} | |
} | |
} | |
// Get where we've clicked on the bar in percentage. | |
float GetProgressBarPercentage() | |
{ | |
Debug.Log("GetProgressBarPercentage"); | |
float percentage; | |
if ((Input.mousePosition.x == 0 || Input.mousePosition.x <= 1f)) | |
percentage = 0; | |
else | |
percentage = (100 * Input.mousePosition.x) / Screen.width; | |
if (Input.touchCount > 0) | |
{ | |
Touch touch = Input.GetTouch(0); | |
if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) | |
{ | |
if (touch.position.x == 0 || touch.position.x <= 1f) | |
{ | |
percentage = 0; | |
} | |
} | |
} | |
else | |
percentage = (100 * Input.mousePosition.x) / Screen.width; | |
return percentage; | |
} | |
// Show current video progress. | |
void ProgressBarPlay() | |
{ | |
if (videoPlayer.isPlaying) | |
{ | |
if (videoPlayer.frameCount < float.MaxValue) | |
{ | |
float frame = (float)videoPlayer.frame; | |
float count = (float)videoPlayer.frameCount; | |
float progressPercentage = 0; | |
if (count > 0) | |
{ | |
progressPercentage = (frame / count) * (float)Screen.width; | |
} | |
if (progressBar != null) | |
{ | |
progressBar.sizeDelta = new Vector2((float)progressPercentage, progressBar.sizeDelta.y); | |
} | |
} | |
} | |
} | |
#endregion | |
private void OnDisable() | |
{ | |
videoPlayer.Stop(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using Vuforia; | |
public class Vuforia_Override : DefaultTrackableEventHandler | |
{ | |
// This Method has to be attached directly on the TargetImage and not children of it. | |
private JC_VideoContoller videoController; | |
protected override void OnTrackingLost() | |
{ | |
if (videoController == null) | |
{ | |
videoController = GetComponentInChildren<JC_VideoContoller>(); | |
} | |
videoController.fullscreenPauseButton.gameObject.SetActive(false); | |
videoController.fullscreenResumeButton.gameObject.SetActive(false); | |
videoController.DisableCanvasesOnTrackingLost(); | |
base.OnTrackingLost(); | |
videoController.videoPlayer.Pause(); | |
} | |
protected override void OnTrackingFound() | |
{ | |
base.OnTrackingFound(); | |
if (videoController == null) | |
{ | |
videoController = GetComponentInChildren<JC_VideoContoller>(); | |
} | |
videoController.ActivateCanvases(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment