// // This file is part of the Tremble package by Tiny Goose. // Copyright (c) 2024-2026 TinyGoose Ltd., All Rights Reserved. // using System; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; namespace TinyGoose.Tremble.Editor { [ManualPage("Entities/Point Entity Builder", showInTree: false)] public class PointBuilderPage : BuilderPageBase { public class SampleData : ScriptableObject { [SerializeField] internal string m_UnityName = "MyCoolPoint"; [SerializeField] internal string m_MapName = ""; [SerializeField] internal string m_Category = ""; [SerializeField] internal Color m_Colour = Color.white; [SerializeField] internal float m_Size = 0.25f; [SerializeField] internal Texture2D m_Sprite; } protected override void OnPropertiesGUI() { RenderProperty(nameof(SampleData.m_UnityName), title: "MonoBehaviour Name", description: "The name of the main MonoBehaviour script of your point entity."); RenderProperty(nameof(SampleData.m_MapName), title: "TrenchBroom name", description: "(optional) The entity name to use in TrenchBroom. Leave this blank to use the default."); RenderProperty(nameof(SampleData.m_Category), title: "Category Name", description: "(optional) The category/group name to use in TrenchBroom. Leave this blank to use the default."); RenderProperty(nameof(SampleData.m_Colour), title: "Preview Colour", description: "The colour of preview cube to represent this Point entity in TrenchBroom."); RenderProperty(nameof(SampleData.m_Size), title: "Preview Size", description: "The size of preview cube to represent this Point entity in TrenchBroom."); RenderProperty(nameof(SampleData.m_Sprite), title: "Preview Sprite", description: "(optional) A sprite to use to represent this Point entity in TrenchBroom.", objectReferenceType: typeof(Texture2D)); } protected override void OnPostPropertiesGUI() { List attributeParts = new(16); if (!Sample.m_MapName.IsNullOrEmpty()) { attributeParts.Add($"\"{Sample.m_MapName}\""); } if (!Sample.m_Category.IsNullOrEmpty()) { attributeParts.Add($"category: \"{Sample.m_Category}\""); } if (Sample.m_Sprite) { attributeParts.Add($"sprite: \"{Sample.m_Sprite.name}\""); } else { if (Sample.m_Colour != Color.white) { attributeParts.Add($"colour: \"{Sample.m_Colour.ToStringInvariant()}\""); } if (Sample.m_Size != 0.25f) { attributeParts.Add($"size: {Sample.m_Size}"); } } string attrName = FormatAttributeName(typeof(PointEntityAttribute)); string attributeLine = attributeParts.Count == 0 ? $"[{attrName}]" : $"[{attrName}({String.Join(", ", attributeParts)})]"; string unityName = Sample.m_UnityName.IsNullOrEmpty() ? "" : Sample.m_UnityName; string catName = Sample.m_Category.IsNullOrEmpty() ? FgdConsts.POINT_PREFIX : Sample.m_Category; string entName = Sample.m_MapName.IsNullOrEmpty() ? Sample.m_UnityName.ToNamingConvention(SyncSettings.TypeNamingConvention) : Sample.m_MapName; HorizontalLine(); GUILayout.BeginHorizontal(); { GUILayout.BeginVertical(); { H3("Create New Entity"); string code = "// Generated by Tremble v1.7.0\n" + $"// Point entity '{catName}_{entName}'\n" + "\n" + "using UnityEngine;\n" + $"using {nameof(TinyGoose)}.{nameof(Tremble)};\n" + "\n" + attributeLine + "\n" + $"public class {unityName} : MonoBehaviour\n" + "{\n" + " // Your cool class here ;)\n" + "}"; CodeWithHeaderNoCopy($"Full source for {unityName}.cs", code); ActionBar_SingleAction("Save to new MonoBehaviour class", () => { string assetsFolder = Path.Combine(Directory.GetCurrentDirectory(), "Assets"); string filename = EditorUtility.SaveFilePanel($"Choose where to save {unityName}", assetsFolder, $"{unityName}.cs", "cs"); if (!filename.IsNullOrEmpty()) { File.WriteAllText(filename, code); EditorCompileUtil.CompileThen(typeof(PointBuilderPage), nameof(OnPostCompile)); } }); } GUILayout.EndVertical(); GUILayout.Label("-or-", EditorStyles.centeredGreyMiniLabel, GUILayout.ExpandHeight(true)); GUILayout.BeginVertical(); { H3("Add to existing MonoBehaviour"); CodeWithHeaderNoCopy("Attribute line only (for existing MonoBehaviour)", attributeLine); ActionBar_SingleAction("Copy to clipboard", () => EditorGUIUtility.systemCopyBuffer = attributeLine); } GUILayout.EndVertical(); } GUILayout.EndHorizontal(); } private static void OnPostCompile() { string message = TrenchBroomUtil.IsTrenchBroomRunning ? "Be sure to press F6 in TrenchBroom to reload entity definitions, so that your new entity appears!" : "All done!"; EditorUtility.DisplayDialog("MonoBehaviour created!", message, "Great!"); ManualWindow mw = EditorWindow.GetWindow(); if (mw) { mw.GoToPage(typeof(PointEntitiesPage)); } } } }