5 Komitmen 7022d08246 ... 2769df218b

Pembuat SHA1 Pesan Tanggal
  James Peret 2769df218b Bumped package version to 0.2.6 2 tahun lalu
  James Peret 306433f670 Added IMapDataGenerator interface 2 tahun lalu
  James Peret f64fa9ee41 Added Cronometer utility class 2 tahun lalu
  James Peret 75254aecc4 Added HideInspectorDerivedAttribute 2 tahun lalu
  James Peret a076f7a289 Fixed bug in TimeToString utility function 2 tahun lalu

+ 6 - 0
Readme.md

@@ -25,6 +25,12 @@ This contains the base code that other packages will use. It includes code for t
 
 ### 📄Changelog
 
+##### v0.2.6
+- Added the Cronometer utility
+- Added IMapDataGenerator interface
+- Added HideInspectorDerivedAttribute
+- Fixed bug in TimeToString utility function
+
 ##### v0.2.5
 - Updated the ResolutionOptions to include all types of displays with a minimum size
 

+ 233 - 0
Runtime/Attributes/HideInspectorDerivedAttribute.cs

@@ -0,0 +1,233 @@
+using UnityEngine;
+using UnityEditor;
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Linq;
+ 
+
+namespace KairoEngine.Core
+{
+  /// <summary>
+  /// Custom attributes
+  /// </summary>
+  
+  [AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, AllowMultiple = true)]
+  public class HideInspectorDerived : Attribute
+  {
+    public readonly Type    m_derivedtype;
+    public readonly string  m_fieldname;
+  
+    public HideInspectorDerived(Type derivedtype) // Hide from specific derived type
+    {
+      m_derivedtype    = derivedtype;
+      m_fieldname    = "";
+    }
+  
+    public HideInspectorDerived()           // Hide from all descendants
+    {
+      m_derivedtype    = null;
+      m_fieldname    = "";
+    }
+  
+    public HideInspectorDerived(string fieldName) // Hide field from class
+    {
+      m_derivedtype    = null;
+      m_fieldname    = fieldName;
+    }
+  }
+  
+  [AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, AllowMultiple = true)]
+  public class InspectorLabel : Attribute
+  {
+    public readonly string  m_label;
+    public readonly Type    m_derivedtype;
+    public readonly string  m_fieldname;
+  
+    public InspectorLabel(string label)            // relabel field
+    {
+      m_label        = label;
+      m_derivedtype    = null;
+      m_fieldname     = "";
+    }
+  
+    public InspectorLabel(string label, Type derivedtype)    // relabel field for specific  derived type
+    {
+      m_label      = label;
+      m_derivedtype    = derivedtype;
+      m_fieldname     = "";
+    }
+  
+    public InspectorLabel(string fieldName, string label)  // relabel field for class
+    {
+      m_label        = label;
+      m_derivedtype    = null;
+      m_fieldname  = fieldName;
+    }
+  }
+  
+  
+  #if UNITY_EDITOR
+  
+  [UnityEditor.CustomEditor(typeof(MonoBehaviour), true)]
+  [CanEditMultipleObjects()]
+  public class CustomInspectorAttribEditor : UnityEditor.Editor
+  {
+    struct CustomFieldInfo
+    {
+      public bool     m_displayField;
+      public string    m_label;
+    };
+  
+    Dictionary<string, CustomFieldInfo> m_customField = new Dictionary<string, CustomFieldInfo>();
+  
+    protected virtual void OnEnable()
+    {
+      Type        monoType        = this.target.GetType();
+      FieldInfo[]    componentFields   = monoType.GetFields(BindingFlags.Instance | BindingFlags.Public);
+      FieldInfo[]    declaredtFields   = monoType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
+      CustomFieldInfo custominfo;
+  
+      foreach(FieldInfo field in componentFields)
+      {
+        bool customField = false;
+  
+        custominfo.m_displayField    = false;
+        custominfo.m_label          = "";
+  
+        InspectorLabel labelattrib  = Attribute.GetCustomAttribute(field, typeof(InspectorLabel)) as InspectorLabel;
+        
+        if ((labelattrib != null) && ((labelattrib.m_derivedtype == null) || labelattrib.m_derivedtype.IsAssignableFrom(monoType)))
+        {
+          custominfo.m_label       = labelattrib.m_label;
+          custominfo.m_displayField = true;
+          customField         = true;
+        }
+              
+        HideInspectorDerived hidederived = Attribute.GetCustomAttribute(field, typeof(HideInspectorDerived)) as HideInspectorDerived;
+        
+        if(hidederived != null)
+        {
+  
+          if((hidederived.m_derivedtype != null) && hidederived.m_derivedtype.IsAssignableFrom(monoType))
+          {
+            custominfo.m_displayField = false;
+            customField         = true;
+          }
+          else if((hidederived.m_derivedtype == null)  && (!declaredtFields.Contains(field)))
+          {
+            custominfo.m_displayField = false;
+            customField         = true;
+          }
+        }
+  
+        if(customField && !m_customField.ContainsKey(field.Name))
+          m_customField.Add (field.Name, custominfo);
+      }
+  
+      HideInspectorDerived [] hidelist = monoType.GetCustomAttributes(typeof(HideInspectorDerived), false) as HideInspectorDerived[];
+      if((hidelist != null) && (hidelist.Length > 0))
+      {
+        custominfo.m_displayField    = false;
+        custominfo.m_label          = "";
+  
+        foreach(HideInspectorDerived hide in hidelist)
+        {
+          if(hide.m_fieldname != "")
+          {
+    
+            if(m_customField.ContainsKey(hide.m_fieldname))
+            {
+              m_customField[hide.m_fieldname] = custominfo; // overwrite existing info...
+            }
+            else
+            {
+              m_customField.Add (hide.m_fieldname, custominfo);
+            }
+          }
+        }
+      }
+  
+      InspectorLabel [] labellist = monoType.GetCustomAttributes(typeof(InspectorLabel), false) as InspectorLabel[];
+      if((labellist != null) && (labellist.Length > 0))
+      {
+        custominfo.m_displayField    = true;
+        custominfo.m_label          = "";
+        
+        foreach(InspectorLabel label in labellist)
+        {
+          if((label.m_fieldname != "") && (label.m_label != ""))
+          {
+            custominfo.m_label = label.m_label;
+            if(m_customField.ContainsKey(label.m_fieldname))
+            {
+              m_customField[label.m_fieldname] = custominfo; // overwrite existing info...
+            }
+            else
+            {
+              m_customField.Add (label.m_fieldname, custominfo);
+            }
+          }
+        }
+      }
+    }
+    ////////////////////////////////
+  
+    public override void OnInspectorGUI()
+    {
+      DrawDefaultInspector();
+    }
+    ////////////////////////////////
+  
+    public new bool DrawDefaultInspector()
+    {
+      this.serializedObject.Update();
+  
+      if (this.serializedObject == null)
+        throw new System.ArgumentNullException("serializedObject");
+  
+      MonoBehaviour    mono     = serializedObject.targetObject as MonoBehaviour;
+      Type        monoType = mono.GetType();
+  
+      EditorGUI.BeginChangeCheck();
+  
+      SerializedProperty  iter     = serializedObject.GetIterator();
+      GUIContent        label     = new GUIContent();
+      CustomFieldInfo    info;
+      bool          enterChildren = true;
+  
+      while(iter.NextVisible(enterChildren))
+      {
+        if(m_customField != null && m_customField.TryGetValue(iter.name, out info))
+        {
+          if(info.m_displayField)
+          {
+            if(info.m_label != "")
+            {
+              label.text = info.m_label;
+              EditorGUILayout.PropertyField(iter, label, true);
+            }
+            else
+            {
+              EditorGUILayout.PropertyField(iter, true);
+            }
+          }
+        }
+        else
+        {
+          EditorGUILayout.PropertyField(iter, true);
+        }
+  
+        enterChildren = false;
+      }
+  
+      bool result = EditorGUI.EndChangeCheck();
+      this.serializedObject.ApplyModifiedProperties();
+  
+      return result;
+    }
+  }
+  #endif
+  
+}

+ 11 - 0
Runtime/Attributes/HideInspectorDerivedAttribute.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 59b54f3718561f14da9f1750a8bcef22
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 49 - 0
Runtime/Cronometer.cs

@@ -0,0 +1,49 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace KairoEngine.Core
+{
+    public class Cronometer
+    {
+        
+        private float startTime;
+        private float stopTime;
+        private float totalTime;
+        private bool running = false;
+
+        public Cronometer()
+        {
+            startTime = Time.realtimeSinceStartup;
+            running = true;
+        }
+
+        public void Start()
+        {
+            if(!running)
+            {
+                running = true;
+                startTime = Time.realtimeSinceStartup;
+            }
+        }
+
+        public Cronometer Stop()
+        {
+            if(running)
+            {
+                stopTime = Time.realtimeSinceStartup;
+                totalTime += stopTime - startTime;
+                running = false;
+                startTime = 0f;
+                stopTime = 0f;
+            }
+            return this;
+        }
+
+        public void Continue() => Start();
+
+        public float value => totalTime;
+
+        public string Print() => Utilities.TimeToString(value);
+    }
+}

+ 11 - 0
Runtime/Cronometer.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: ba7320bc5a976f64494367049aaf103f
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 12 - 0
Runtime/IMapDataGenerator.cs

@@ -0,0 +1,12 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace KairoEngine.Core
+{
+    public interface IMapDataGenerator
+    {
+        public float SamplePoint(float x, float y);
+        public uint SamplePointColor(float x, float y, float value);
+    }
+}

+ 11 - 0
Runtime/IMapDataGenerator.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 764d45de40a9dbd4898d8f2a2d888b24
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 5 - 0
Runtime/Utilities.cs

@@ -28,6 +28,11 @@ namespace KairoEngine.Core
         public static string TimeToString(float time)
         {
             string t = "";
+            if(time < 1)
+            {
+                int miliseconds = (int)(time * 1000);
+                return $"{miliseconds} ms";
+            }
             int minutes = (int)time / 60;
             int hours = (int)minutes / 60;
             minutes = (int)minutes % 60;

+ 1 - 1
package.json

@@ -1,7 +1,7 @@
 {
   "name": "at.kairoscope.kairoengine.core",
   "displayName" : "KairoEngine Core",
-  "version": "0.2.5",
+  "version": "0.2.6",
   "unity": "2020.3",
   "description": "Base package for the KairoEngine library by Kairoscope",
   "repository": {