Skip to content

Commit

Permalink
added THREE.Silk and examples #26
Browse files Browse the repository at this point in the history
  • Loading branch information
hjoykim committed Oct 12, 2024
1 parent 6a4a53a commit 61ac8c4
Show file tree
Hide file tree
Showing 226 changed files with 39,800 additions and 0 deletions.
13 changes: 13 additions & 0 deletions SilkDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace SilkDemo
{
class Program
{
private static void Main(string[] args)
{
var demoMainWindow = new SilkDemoWindow();
demoMainWindow.Render();
demoMainWindow.window.Dispose();

}
}
}
22 changes: 22 additions & 0 deletions SilkDemo/SilkDemo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Compile Remove="THREE.Silk.Example\LearningThreeJs\Chapter11\**" />
<EmbeddedResource Remove="THREE.Silk.Example\LearningThreeJs\Chapter11\**" />
<None Remove="THREE.Silk.Example\LearningThreeJs\Chapter11\**" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SingleSceneSilkExample\SingleSceneSilkExample.csproj" />
<ProjectReference Include="..\THREE.Silk\THREE.Silk.csproj" />
<ProjectReference Include="..\ThreeSilkExample\THREESilkExample.csproj" />
</ItemGroup>

</Project>
270 changes: 270 additions & 0 deletions SilkDemo/SilkDemoWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
using ImGuiNET;
using Silk.NET.Maths;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using THREE.Silk;
using THREE.Silk.Example;
namespace SilkDemo
{
public class TreeNode<T> : IEnumerable<TreeNode<T>>
{

public T Data { get; set; }
public TreeNode<T> Parent { get; set; }
public ICollection<TreeNode<T>> Children { get; set; }

public bool IsRoot
{
get { return Parent == null; }
}

public bool IsLeaf
{
get { return Children.Count == 0; }
}

public int Level
{
get
{
if (this.IsRoot)
return 0;
return Parent.Level + 1;
}
}

public object Tag { get; set; }

public TreeNode(T data)
{
this.Data = data;
this.Children = new LinkedList<TreeNode<T>>();

this.ElementsIndex = new LinkedList<TreeNode<T>>();
this.ElementsIndex.Add(this);
}

public TreeNode<T> AddChild(T child)
{
TreeNode<T> childNode = new TreeNode<T>(child) { Parent = this };
this.Children.Add(childNode);

this.RegisterChildForSearch(childNode);

return childNode;
}

public override string ToString()
{
return Data != null ? Data.ToString() : "[data null]";
}


#region searching

private ICollection<TreeNode<T>> ElementsIndex { get; set; }

private void RegisterChildForSearch(TreeNode<T> node)
{
ElementsIndex.Add(node);
if (Parent != null)
Parent.RegisterChildForSearch(node);
}

public TreeNode<T> FindTreeNode(Func<TreeNode<T>, bool> predicate)
{
return this.ElementsIndex.FirstOrDefault(predicate);
}

#endregion


#region iterating

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public IEnumerator<TreeNode<T>> GetEnumerator()
{
yield return this;
foreach (var directChild in this.Children)
{
foreach (var anyChild in directChild)
yield return anyChild;
}
}

#endregion
}
public class SilkDemoWindow : ThreeSilkWindow
{
private List<TreeNode<string>> examplesList;
private Dictionary<string, Example> exampleInstances = new Dictionary<string, Example>();
public SilkDemoWindow() : base() { }

private TreeNode<string> LoadExampleFromAssembly(Assembly assembly)
{
TreeNode<string> treeView = new TreeNode<string>("Root");
if (assembly == null)
throw new ArgumentNullException("assembly");

var types = assembly.GetTypes();
foreach (var type in types)
{
var attributes = type.GetCustomAttributes(false);

foreach (var exampleType in attributes)
{
if (exampleType is ExampleAttribute)
{
var example = exampleType as ExampleAttribute;
string key = example.Category.ToString();
TreeNode<string> rootNode = treeView.FindTreeNode(node => node.Data != null && node.Data.Equals(key));
if (rootNode == null)
{
rootNode = treeView.AddChild(key);
}
TreeNode<string> subNode = rootNode.FindTreeNode(node => node.Data != null && node.Data.Equals(example.Subcategory));
if (subNode == null)
{
subNode = rootNode.AddChild(example.Subcategory);
}
TreeNode<string> nodeItem = subNode.FindTreeNode(node => node.Data != null && node.Data.Equals(example.Title));
if (nodeItem == null)
{
nodeItem = subNode.AddChild(example.Title);
nodeItem.Tag = new ExampleInfo(type, example);
//var exampleInstance = (Example)Activator.CreateInstance((nodeItem.Tag as ExampleInfo).Example);
//if (null != exampleInstance)
//{
// imGuiManager.Dispose();
// imGuiManager = new ImGuiManager(this);
// exampleInstance.imGuiManager = imGuiManager;
// exampleInstances[example.Title] = exampleInstance;
//}
}
}
}
}
return treeView;
}

private int SortByName(TreeNode<string> a, TreeNode<string> b)
{
return a.Data.CompareTo(b.Data);
}
private void GetExamplesList()
{
Type t = typeof(Example);
examplesList = LoadExampleFromAssembly(Assembly.GetAssembly(t)).Children.ToList();
examplesList.Sort(SortByName);
}
private string MakeExampleTitle(string exampleName)
{
if (string.IsNullOrEmpty(exampleName))
return "THREE.Silk.Example";
else
return "THREE.Silk.Example : " + exampleName;
}
private void ShowExamplesMenu()
{
if (ImGui.BeginMainMenuBar())
{
if (ImGui.BeginMenu("File"))
{
if (ImGui.MenuItem("Exit", "Ctrl+E"))
{
System.Environment.Exit(0);
}
ImGui.EndMenu();
}
ImGui.EndMainMenuBar();
}
ImGui.Begin("Examples");
for (int i = 0; i < examplesList.Count; i++)
{
TreeNode<string> category = examplesList[i];
if (ImGui.CollapsingHeader(category.Data))
{
List<TreeNode<string>> subList = category.Children.ToList();
subList.Sort(SortByName);
for (int j = 0; j < subList.Count; j++)
{
TreeNode<string> subCategory = subList[j];
if (ImGui.TreeNode(subCategory.Data))
{
List<TreeNode<string>> titleList = subCategory.Children.ToList();
titleList.Sort(SortByName);
for (int k = 0; k < titleList.Count; k++)
{
var title = titleList[k];
if (ImGui.Button(title.Data))
{
RunSample(title.Tag as ExampleInfo);
}
}
ImGui.TreePop();
}
}
}
}
ImGui.End();
}
private void RunSample(ExampleInfo e)
{
if (null != currentThreeContainer)
{
currentThreeContainer.Dispose();
currentThreeContainer = null;
Title = MakeExampleTitle("");
}

//Application.Idle -= Application_Idle;

currentThreeContainer = (Example)Activator.CreateInstance(e.Example);
if (null != currentThreeContainer)
{
currentThreeContainer.Load(this.window);
currentThreeContainer.imGuiManager = imGuiManager;
Title = MakeExampleTitle(e.Attribute.Title);
Vector2D<int> size;
size.X = window.Size.X;
size.Y = window.Size.Y;
OnResize(size);
}
}
public override void OnLoad()
{
base.OnLoad();
currentThreeContainer = new FirstSceneExample();
currentThreeContainer.Load(window);
currentThreeContainer.imGuiManager = imGuiManager;
currentThreeContainer.OnResize(new ResizeEventArgs(window.Size.X,window.Size.Y));
GetExamplesList();

}
public override void OnRender(double deltaTime)
{
imGuiManager.Update((float)deltaTime);
if (currentThreeContainer == null) return;

ShowExamplesMenu();

currentThreeContainer.Render();
if (currentThreeContainer.AddGuiControlsAction != null)
{
currentThreeContainer.AddGuiControlsAction();
}
imGuiManager.Render();
}
}


}
13 changes: 13 additions & 0 deletions SingleSceneSilkExample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using THREE.Silk.Example;
namespace THREE.Silk
{
class Program
{
private static void Main(string[] args)
{
var window = new ThreeSilkWindow();
window.SetCurrentExample(new FirstSceneExample());
window.Render();
}
}
}
16 changes: 16 additions & 0 deletions SingleSceneSilkExample/SingleSceneSilkExample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\THREE.Silk\THREE.Silk.csproj" />
<ProjectReference Include="..\ThreeSilkExample\THREESilkExample.csproj" />
<ProjectReference Include="..\THREE\THREE.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 61ac8c4

Please sign in to comment.