Skip to content

Instantly share code, notes, and snippets.

@amitkhare
Last active September 4, 2019 03:58
Show Gist options
  • Save amitkhare/708b3b9d3287688fc8ddff7c7d4a8630 to your computer and use it in GitHub Desktop.
Save amitkhare/708b3b9d3287688fc8ddff7c7d4a8630 to your computer and use it in GitHub Desktop.
WASP3D List Files
/*
VARIABLES TO CREATE
WInteger
imgIndex : 0
imgTotal : 0
WString
imgPath : ""
selectedFile : ""
WBoolean
isComplete : false
isLoop: false
*/
// using System.IO; // add this to namespaces
// using System.Text.RegularExpressions; // this too
public string[] imageFiles;
public int imgIndex;
public void setupItems(){
imgIndex = -1;
this.WBoolean.isComplete = false;
string selectedPath = Path.GetDirectoryName(this.WString.selectedFile.ToString());
String searchFolder = @"" + selectedPath;
var filters = new String[] { "jpg", "jpeg", "png", "bmp" };
imageFiles = GetFilesFrom(searchFolder, filters, false);
this.WInteger.imgTotal = imageFiles.Length;
this.WString.imgPath = imageFiles[imgIndex].ToString();
}
public void nextItem (){
if(imgIndex < imageFiles.Length - 1) {
this.WBoolean.isComplete = false;
imgIndex += 1;
} else {
imgIndex = 0;
this.WBoolean.isComplete = true;
}
this.WString.imgPath = imageFiles[imgIndex].ToString();
this.WInteger.imgIndex = imgIndex;
}
public static String[] GetFilesFrom(String searchFolder, String[] filters, bool isRecursive)
{
List<String> filesFound = new List<String>();
var searchOption = isRecursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
foreach (var filter in filters)
{
filesFound.AddRange(Directory.GetFiles(searchFolder, String.Format("*.{0}", filter), searchOption));
}
String[] filesFoundArray = filesFound.ToArray();
NumericalSort(filesFoundArray);
return filesFoundArray;
}
public static void NumericalSort(string[] ar)
{
Regex rgx = new Regex("([^0-9]*)([0-9]+)");
Array.Sort(ar, (a, b) =>
{
var ma = rgx.Matches(a);
var mb = rgx.Matches(b);
for (int i = 0; i < ma.Count; ++i)
{
int ret = ma[i].Groups[1].Value.CompareTo(mb[i].Groups[1].Value);
if (ret != 0)
return ret;
ret = int.Parse(ma[i].Groups[2].Value) - int.Parse(mb[i].Groups[2].Value);
if (ret != 0)
return ret;
}
return 0;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment