using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace Gaia
{
/////
///// Priority definitions. The idea is to give the priority a human readable identification and control all priorities in a central location
///// instead of 100s of places within the application. The priority of these items is defined by their position in this enum, first entry = lowest priority, last entry = highest priority.
/////
public enum ProgressBarPriority { TerrainLoading, MinMaxHeightCalculation, CreateBiomeTools, CreateSceneTools, BiomeRemoval, MultiTerrainAction, MaskMapExport, Stamping, Spawning, WorldCreation, GaiaGeospatial, Maintenance };
//public class ProgressBarPriority
//{
// public string m_name;
// public int m_priority;
//}
///
/// Static class to display an unified progress bar in Gaia. Allows to set a priority so that a progress bar for a longer, overarching process
/// is not constantly interrupted by smaller processes, e.g. the world creation progress bar is not interrupted by progress bars for terrain loading.
///
public static class ProgressBar
{
#if UNITY_EDITOR
///
/// The current priority of the last progress bar that was shown.
///
public static int m_currentPriority =0;
///
/// The average duration in milliseconds to perform one of the progress bar steps.
///
static long m_averageStepDuration = 0;
///
/// The timestamp when the last step for this progress bar was completed before.
///
static long m_lastStepTimeStamp = 0;
#endif
///
/// Shows a progress bar with a given priority.
///
/// An enum value that controls the priority of this progress bar. (See ProgressBar.cs for the definitions!)
/// The title for the progress bar window. Will be appended by step count and ETA if chosen.
/// The info text below the progress bar.
/// The current step for the process that the progress bar represents. If the process can't be split into steps, put 0 in here.
/// The total amount of steps for the process that the progress bar represents. If the process can't be split into steps, put 0 in here.
/// Whether you want an ETA (estimated time of arrival) to be displayed in the title
/// Whether the progress bar should have a cancel button.
/// Returns true if the user clicked on the cancel button in the progress bar.
public static bool Show(ProgressBarPriority priority, string title, string info, int currentStep = 0, int totalSteps = 0, bool displayETA = false, bool cancelable = false)
{
#if UNITY_EDITOR
int newPriority = 0;
newPriority = (int)priority;
//New priority needs to be higher as current one, otherwise we won't interrupt the current progress bar
if (newPriority < m_currentPriority)
{
return false;
}
if (newPriority != m_currentPriority)
{
m_currentPriority = newPriority;
//new priority? reset the variables for ETA calculation as well
m_averageStepDuration = 0;
m_lastStepTimeStamp = 0;
}
if (totalSteps > 0)
{
title = string.Format(title + " - Step {0} of {1}", currentStep.ToString(), totalSteps.ToString());
}
//ETA calculations
if (m_lastStepTimeStamp > 0 && currentStep > 0)
{
m_averageStepDuration = (m_averageStepDuration * (currentStep -1) + (GaiaUtils.GetUnixTimestamp() - m_lastStepTimeStamp)) / (long)currentStep;
}
m_lastStepTimeStamp = GaiaUtils.GetUnixTimestamp();
if (displayETA && currentStep >=1)
{
title += ", ETA: " + TimeSpan.FromMilliseconds(m_averageStepDuration * (totalSteps -currentStep)).ToString(@"hh\:mm\:ss");
}
//Progress calculations
float progress = 0.5f;
if (totalSteps > 0)
{
progress = (float)currentStep / (float)totalSteps;
}
if (cancelable)
{
if (EditorUtility.DisplayCancelableProgressBar(title, info, progress))
{
//cancel was pressed, make sure the process bar is being cleared in any case.
m_currentPriority = 0;
m_averageStepDuration = 0;
m_lastStepTimeStamp = 0;
EditorUtility.ClearProgressBar();
return true;
}
else
{
return false;
}
}
else
{
EditorUtility.DisplayProgressBar(title, info, progress);
return false;
}
#else
return false;
#endif
}
///
/// Clears the current progress bar, if the given name is current priority or higher.
///
public static void Clear(ProgressBarPriority priority)
{
#if UNITY_EDITOR
//Get the priority of the given name
int newPriority = 0;
newPriority = (int)priority;
//Progress bar with lower priority may not clear the current one
if (newPriority < m_currentPriority)
{
return;
}
m_currentPriority = 0;
m_averageStepDuration = 0;
m_lastStepTimeStamp = 0;
EditorUtility.ClearProgressBar();
#endif
}
}
}