Skip to content

Commit

Permalink
Merge pull request #7 from katycat5e/loco_components
Browse files Browse the repository at this point in the history
Add custom loco components, integrated car spawning/TrainCarTypes
  • Loading branch information
katycat5e authored Aug 16, 2021
2 parents 80fbc05 + 4f2405b commit 30998e1
Show file tree
Hide file tree
Showing 69 changed files with 6,212 additions and 720 deletions.
83 changes: 83 additions & 0 deletions CCL_GameScripts/Attributes/Editor/MethodButtonAttributeDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System.Text.RegularExpressions;

[CustomPropertyDrawer(typeof(MethodButtonAttribute))]
public class MethodButtonAttributeDrawer : PropertyDrawer
{
private int buttonCount;
private readonly float buttonHeight = EditorGUIUtility.singleLineHeight * 1.2f;
private MethodButtonAttribute attr;

public override void OnGUI(Rect position, SerializedProperty editorFoldout, GUIContent label)
{
if (editorFoldout.name.Equals("editorFoldout") == false)
{
LogErrorMessage(editorFoldout);
return;
}

buttonCount = 0;

Rect foldoutRect = new Rect(position.x, position.y, position.width, 5 + buttonHeight);

editorFoldout.boolValue = EditorGUI.Foldout(foldoutRect, editorFoldout.boolValue, "Buttons", true);

if (editorFoldout.boolValue)
{
buttonCount++;

attr = (MethodButtonAttribute)attribute;

foreach (var name in attr.MethodNames)
{
buttonCount++;

Rect buttonRect = new Rect(position.x, position.y + ((1 + buttonHeight) * (buttonCount - 1)), position.width, buttonHeight - 1);

string buttonText = SplitCamelCase(name);
if (GUI.Button(buttonRect, buttonText))
{
InvokeMethod(editorFoldout, name);
}
}
}
}

private static string SplitCamelCase( string str )
{
return Regex.Replace(
Regex.Replace(
str,
@"(\P{Ll})(\P{Ll}\p{Ll})",
"$1 $2"
),
@"(\p{Ll})(\P{Ll})",
"$1 $2"
);
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label, true) + (buttonHeight) * (buttonCount);
}

private void InvokeMethod(SerializedProperty property, string name)
{
Object target = property.serializedObject.targetObject;
target.GetType().GetMethod(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly).Invoke(target, null);
}

private void LogErrorMessage(SerializedProperty editorFoldout)
{
Debug.LogError("<color=red><b>Possible improper usage of method button attribute!</b></color>");
#if NET_4_6
Debug.LogError($"Got field name: <b>{editorFoldout.name}</b>, Expected: <b>editorFoldout</b>");
Debug.LogError($"Please see <b>{"Usage"}</b> at <b><i><color=blue>{"https://github.com/GlassToeStudio/UnityMethodButtonAttribute/blob/master/README.md"}</color></i></b>");
#else
Debug.LogError(string.Format("Got field name: <b>{0}</b>, Expected: <b>editorFoldout</b>", editorFoldout.name));
Debug.LogError("Please see <b>\"Usage\"</b> at <b><i><color=blue>\"https://github.com/GlassToeStudio/UnityMethodButtonAttribute/blob/master/README.md \"</color></i></b>");
#endif
}
}
26 changes: 26 additions & 0 deletions CCL_GameScripts/Attributes/Editor/ReadOnlyDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using UnityEngine;
using UnityEditor;
/// <summary>
/// This class contain custom drawer for ReadOnly attribute.
/// </summary>
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyDrawer : PropertyDrawer
{
/// <summary>
/// Unity method for drawing GUI in Editor
/// </summary>
/// <param name="position">Position.</param>
/// <param name="property">Property.</param>
/// <param name="label">Label.</param>
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Saving previous GUI enabled value
var previousGUIState = GUI.enabled;
// Disabling edit for property
GUI.enabled = false;
// Drawing Property
EditorGUI.PropertyField(position, property, label);
// Setting old GUI enabled value
GUI.enabled = previousGUIState;
}
}
25 changes: 25 additions & 0 deletions CCL_GameScripts/Attributes/MethodButtonAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using UnityEngine;

/// <summary>
/// Usage:
/// <para> #if UNITY_EDITOR</para>
/// <para> [MethodButton("MethodName1", "MethodName2", "MethodNameN")] // Must match a the method name!</para>
/// <para> [SerializeField] private bool showButtons; // this bool is mandatory!</para>
/// <para> #endif</para>
/// </summary>
public class MethodButtonAttribute : PropertyAttribute
{
/// <summary>
/// Array of different method names for which a button will be displayed.
/// <para>"MethodOne", MethodTwo" ... "MethodN"</para>
/// </summary>
public string[] MethodNames { get; private set; }

/// <summary>
/// Default constructor.
/// </summary>
public MethodButtonAttribute(params string[] args)
{
MethodNames = args;
}
}
6 changes: 6 additions & 0 deletions CCL_GameScripts/Attributes/ReadOnlyAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using UnityEngine;
/// <summary>
/// Read Only attribute.
/// Attribute is use only to mark ReadOnly properties.
/// </summary>
public class ReadOnlyAttribute : PropertyAttribute { }
35 changes: 35 additions & 0 deletions CCL_GameScripts/BogieSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace CCL_GameScripts
{
public class BogieSetup : MonoBehaviour
{
[Header("Axles")]
[Tooltip("Number of axles, used for calculating joint sounds")]
[Range(0, 10)]
public int AxleCount = 2;

[Tooltip("Distance between 2 consecutive axles on a bogie")]
[Range(0, 10)]
public float AxleSeparation = 1.5f;

[Header("Physics")]
[Range(0, 100000)]
public float BrakingForcePerBar = 10000f;

[Range(0, 0.02f)]
public float RollingResistanceCoefficient = 0.004f;

public JSONObject GetJSON()
{
var repr = new JSONObject();
repr.AddField("axleCount", AxleCount);
repr.AddField("axleSeparation", AxleSeparation);
repr.AddField("brakingForcePerBar", BrakingForcePerBar);
repr.AddField("rollingResistance", RollingResistanceCoefficient);
return repr;
}
}
}
86 changes: 86 additions & 0 deletions CCL_GameScripts/CCL_GameScripts.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C191C5AC-990E-4CA6-9847-342B9CD92465}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CCL_GameScripts</RootNamespace>
<AssemblyName>CCL_GameScripts</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="UnityEditor, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL" />
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL" />
<Reference Include="UnityEngine.AssetBundleModule, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL" />
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL" />
<Reference Include="UnityEngine.IMGUIModule, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL" />
<Reference Include="UnityEngine.PhysicsModule, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL" />
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\Editor\MethodButtonAttributeDrawer.cs" />
<Compile Include="Attributes\Editor\ReadOnlyDrawer.cs" />
<Compile Include="Attributes\MethodButtonAttribute.cs" />
<Compile Include="Attributes\ReadOnlyAttribute.cs" />
<Compile Include="BogieSetup.cs" />
<Compile Include="CabControls\CabInputSetup.cs" />
<Compile Include="CabControls\CabIOTypes.cs" />
<Compile Include="CabControls\ControlSetupBase.cs" />
<Compile Include="CabControls\CopiedCabControl.cs" />
<Compile Include="CabControls\CopiedGauge.cs" />
<Compile Include="CabControls\CopiedLamp.cs" />
<Compile Include="CabControls\CopiedLever.cs" />
<Compile Include="CabControls\GaugeSetup.cs" />
<Compile Include="CabControls\IndicatorSetupBase.cs" />
<Compile Include="CabControls\LeverSetup.cs" />
<Compile Include="CarJSONKeys.cs" />
<Compile Include="ComponentInitSpec.cs" />
<Compile Include="CustomCarEnums.cs" />
<Compile Include="DamageConfigBasic.cs" />
<Compile Include="DamageConfigDiesel.cs" />
<Compile Include="Editor\DamageConfigEditor.cs" />
<Compile Include="Editor\AddLocoParams.cs" />
<Compile Include="Editor\CreateAssetBundles.cs" />
<Compile Include="Editor\DVSTUDIOExtensions.cs" />
<Compile Include="Editor\ExportTrainCar.cs" />
<Compile Include="Editor\FBXMeshExtractor.cs" />
<Compile Include="Editor\FindMissingScripts.cs" />
<Compile Include="JSONObject\JSONObject.cs" />
<Compile Include="JSONObject\VectorTemplates.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SimParamsBase.cs" />
<Compile Include="SimParamsDiesel.cs" />
<Compile Include="Editor\SimParamsEditor.cs" />
<Compile Include="TrainCarSetup.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
47 changes: 47 additions & 0 deletions CCL_GameScripts/CabControls/CabIOTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace CCL_GameScripts.CabControls
{
public enum CabInputType
{
IndependentBrake,
TrainBrake,
Throttle,
Reverser,
Horn
}

public enum CabIndicatorType
{
BrakePipe,
BrakeReservoir,
EngineTemp,
Fuel,
Oil,
Sand,
Speed,
}

public enum SimEventType
{
Couplers,
EngineDamage,
EngineTemp,
Fuel,
Oil,
Sand,
Wheelslip,
}

public enum SimThresholdDirection
{
Above, Below
}

public enum SimAmount
{
Depleted = 0,
Low = 1,
Mid = 2,
High = 3,
Full = 4
}
}
39 changes: 39 additions & 0 deletions CCL_GameScripts/CabControls/CabInputSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections;
using UnityEngine;

namespace CCL_GameScripts.CabControls
{
public class CabInputSetup : MonoBehaviour
{
public GameObject Brake;
public GameObject IndependentBrake;
public GameObject Reverser;
public GameObject Throttle;

public void SetInputObject( CabInputType inputType, GameObject controlRoot )
{
switch( inputType )
{
case CabInputType.TrainBrake:
Brake = controlRoot;
break;

case CabInputType.IndependentBrake:
IndependentBrake = controlRoot;
break;

case CabInputType.Reverser:
Reverser = controlRoot;
break;

case CabInputType.Throttle:
Throttle = controlRoot;
break;

default:
Debug.LogWarning("Tried to set input of unsupported type");
break;
}
}
}
}
22 changes: 22 additions & 0 deletions CCL_GameScripts/CabControls/ControlSetupBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using UnityEngine;

namespace CCL_GameScripts.CabControls
{
[DisallowMultipleComponent]
public abstract class ControlSetupBase : ComponentInitSpec
{
public abstract CabControlType ControlType { get; }

public CabInputType InputBinding;

[ProxyField("colliderGameObjects")]
public GameObject[] InteractionColliders;
}

public enum CabControlType
{
Lever,
Button
}
}
Loading

0 comments on commit 30998e1

Please sign in to comment.