Last active
May 2, 2024 21:30
-
-
Save mindfulvector/2404132ca2663c612f1148300727720f to your computer and use it in GitHub Desktop.
A notebook written in Unity IMGUI / C#
This file contains hidden or 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; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using UnityEngine; | |
public class Notes : MonoBehaviour | |
{ | |
public string directoryPath = "C:\\Users\\topfr\\OneDrive\\Documents\\Notes"; | |
private string configFileName = "config.ini"; | |
private Dictionary<string, string> configValues = new Dictionary<string, string>(); | |
private FileInfo[] textFiles; | |
private int[] controlIDs; | |
private string[] fileContents; | |
private Vector2[] scrollPositions; // Array of scroll positions for each text area | |
private string searchText = ""; | |
private Vector2 scrollPosition; | |
private float buttonWidth = 80; // Set the width of the buttons | |
GUIStyle headStyle, layoutStyle, toolbarStyle; | |
private List<Material> skyboxMaterials = new List<Material>(); | |
private int currentIndex = 0; | |
void Start() | |
{ | |
Screen.SetResolution(640, 960, false); | |
headStyle = new GUIStyle() { | |
fontSize = 30, | |
richText = true, | |
padding = new RectOffset(10, 20, 10, 0), | |
}; | |
toolbarStyle = new GUIStyle() | |
{ | |
padding = new RectOffset(0, 0, 10, 0), | |
}; | |
layoutStyle = new GUIStyle() | |
{ | |
padding = new RectOffset(20, 20, 0, 0), | |
}; | |
LoadTextFiles(); | |
LoadSkyboxMaterials(); | |
LoadConfig(); | |
} | |
void LoadTextFiles() | |
{ | |
DirectoryInfo dir = new DirectoryInfo(directoryPath); | |
textFiles = dir.GetFiles("*.txt").Concat(dir.GetFiles("*.md")).ToArray(); | |
controlIDs = new int[textFiles.Length]; | |
fileContents = new string[textFiles.Length]; | |
scrollPositions = new Vector2[textFiles.Length]; | |
for (int i = 0; i < textFiles.Length; i++) | |
{ | |
fileContents[i] = File.ReadAllText(textFiles[i].FullName); | |
} | |
} | |
void LoadSkyboxMaterials() | |
{ | |
// Load all materials from the "Resources/Skyboxes" folder | |
Material[] materials = Resources.LoadAll<Material>("Skyboxes"); | |
foreach (Material mat in materials) | |
{ | |
skyboxMaterials.Add(mat); | |
} | |
} | |
void LoadConfig() | |
{ | |
string fullPath = Path.Combine(Application.dataPath, directoryPath, configFileName); | |
if (File.Exists(fullPath)) | |
{ | |
string[] lines = File.ReadAllLines(fullPath); | |
foreach (string line in lines) | |
{ | |
string[] keyValue = line.Split('='); | |
if (keyValue.Length == 2) | |
{ | |
configValues[keyValue[0].Trim()] = keyValue[1].Trim(); | |
} | |
} | |
} | |
else | |
{ | |
Debug.Log("No config file found."); | |
} | |
if (configValues.ContainsKey("background")) | |
{ | |
int index; | |
if (int.TryParse(configValues["background"], out index)) | |
{ | |
currentIndex = index; | |
ApplySkybox(); | |
} | |
} | |
} | |
void SaveConfig() | |
{ | |
File.WriteAllText(Path.Combine(Application.dataPath, directoryPath, configFileName), | |
$"background={currentIndex}"); | |
} | |
void ApplySkybox() | |
{ | |
RenderSettings.skybox = skyboxMaterials[currentIndex]; | |
DynamicGI.UpdateEnvironment(); // Update the global illumination to reflect the new skybox | |
} | |
void OnGUI() | |
{ | |
GUILayout.BeginVertical(); | |
{ | |
// ################################################################################ | |
// Application Header and Toolbar | |
// ################################################################################ | |
GUILayout.BeginHorizontal(headStyle); | |
{ | |
GUILayout.Label("<color=white>soNotes</color>", headStyle);//, GUILayout.Color(Color.white)); | |
// Create a new note with a filename based on the current date | |
GUILayout.FlexibleSpace(); | |
GUILayout.BeginHorizontal(toolbarStyle); | |
{ | |
if (skyboxMaterials.Count == 0) | |
{ | |
GUILayout.Label("<No skybox materials found.>"); | |
} | |
else | |
{ | |
if (GUILayout.Button("Prev", GUILayout.Width(50))) | |
{ | |
currentIndex--; | |
if (currentIndex < 0) | |
currentIndex = skyboxMaterials.Count - 1; | |
ApplySkybox(); | |
SaveConfig(); | |
} | |
GUILayout.Label(skyboxMaterials[currentIndex].name, GUILayout.Width(150)); | |
if (GUILayout.Button("Next", GUILayout.Width(50))) | |
{ | |
currentIndex++; | |
if (currentIndex >= skyboxMaterials.Count) | |
currentIndex = 0; | |
ApplySkybox(); | |
SaveConfig(); | |
} | |
} | |
GUILayout.Label("|"); | |
GUILayout.Label("Go:", GUILayout.Width(25)); | |
if (GUILayout.Button("Today", GUILayout.Width(60))) | |
{ | |
string currentDate = DateTime.Now.ToString("yyyy-MM-dd"); | |
string newFilePath = Path.Combine(directoryPath, currentDate + ".txt"); | |
// Check if the file already exists | |
if (!File.Exists(newFilePath)) | |
{ | |
File.WriteAllText(newFilePath, ""); // Create an empty file | |
LoadTextFiles(); // Refresh file list | |
} | |
// Populate the search field with the current date to filter the list | |
searchText = currentDate; | |
} | |
GUILayout.Label("|"); | |
if (GUILayout.Button("Reload All", GUILayout.Width(buttonWidth))) | |
{ | |
LoadTextFiles(); | |
} | |
} | |
GUILayout.EndHorizontal(); | |
} | |
GUILayout.EndHorizontal(); | |
// ################################################################################ | |
// Search | |
// ################################################################################ | |
GUILayout.BeginHorizontal(layoutStyle); | |
{ | |
searchText = GUILayout.TextField(searchText, GUILayout.Width(200)); | |
if (GUILayout.Button("X", GUILayout.Width(25))) | |
{ | |
searchText = ""; | |
} | |
} | |
GUILayout.EndHorizontal(); | |
// ################################################################################ | |
// Listing | |
// ################################################################################ | |
scrollPosition = GUILayout.BeginScrollView(scrollPosition, layoutStyle); | |
{ | |
int visibleCount = 0; | |
for (int i = 0; i < textFiles.Length; i++) | |
{ | |
if (IsFileVisible(textFiles[i])) | |
{ | |
visibleCount++; | |
} | |
} | |
for (int i = 0; i < textFiles.Length; i++) | |
{ | |
if (IsFileVisible(textFiles[i])) | |
{ | |
GUILayout.Box(textFiles[i].Name);//, EditorStyles.boldLabel); | |
// The dingbats toolbar is not quite working right now | |
int toolbarInt = -1, toolbarInt2 = -1, toolbarInt3 = -1; | |
string[] toolbarStrings = { | |
"❑","✔","✕","✘","✚","✦","✧","✩","✱","✸","❖", | |
"❘","❙","❚","❤","❶","❷","❸","❹","❺","❻","❼","❽","❾","❿","➜", | |
"☀","☁","☂","★","☢","♨","☐","☑","※","﹝","﹞","〈","〉","《","》" | |
}; | |
toolbarInt = GUILayout.Toolbar(toolbarInt, toolbarStrings); | |
// Calculate height for the text area based on content | |
GUIStyle style = GUI.skin.textArea; | |
GUIContent content = new GUIContent(fileContents[i]); | |
float height = Mathf.Min(400, style.CalcHeight(content, GUILayoutUtility.GetLastRect().width))+20; | |
int maxHeight = 200; | |
if(visibleCount == 1) | |
{ | |
maxHeight = Screen.height - 200; | |
} | |
scrollPositions[i] = GUILayout.BeginScrollView(scrollPositions[i], GUILayout.Height(Mathf.Max(height, maxHeight))); | |
{ | |
fileContents[i] = GUILayout.TextArea(fileContents[i], GUILayout.ExpandHeight(true)); | |
var rect = GUILayoutUtility.GetLastRect(); | |
controlIDs[i] = GUIUtility.GetControlID(0, FocusType.Keyboard, rect); | |
} | |
GUILayout.EndScrollView(); | |
if (toolbarInt > -1) | |
{ | |
//var controlID = GUIUtility.GetControlID(FocusType.Keyboard); | |
//var controlID = controlIDs[i]; | |
//var stateObj = GUIUtility.GetStateObject(typeof(TextEditor), controlID) as TextEditor; | |
var stateObj = GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl) as TextEditor; | |
var caretPos = stateObj.selectIndex; | |
fileContents[i] = fileContents[i].Insert(caretPos, toolbarStrings[toolbarInt]); | |
stateObj.selectIndex += toolbarStrings[toolbarInt].Length; | |
//fileContents[i] += toolbarStrings[toolbarInt]; | |
} | |
GUILayout.BeginHorizontal(); | |
{ | |
if (GUILayout.Button("Save", GUILayout.Width(buttonWidth))) | |
{ | |
SaveTextFile(i); | |
} | |
GUILayout.Label(textFiles[i].Name); | |
} | |
GUILayout.EndHorizontal(); | |
} | |
} | |
} | |
GUILayout.EndScrollView(); | |
} | |
GUILayout.EndVertical(); | |
} | |
bool IsFileVisible(FileInfo file) | |
{ | |
string fileContent = File.ReadAllText(file.FullName); | |
return file.Name.Contains(searchText) || fileContent.Contains(searchText); | |
} | |
void SaveTextFile(int index) | |
{ | |
File.WriteAllText(textFiles[index].FullName, fileContents[index]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment