Skip to content

Commit c262031

Browse files
committed
move Prop to a separate file
1 parent 17e723b commit c262031

File tree

2 files changed

+120
-116
lines changed

2 files changed

+120
-116
lines changed

Source/DynamicProperties/Prop.cs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using UnityEngine;
4+
5+
namespace Shabby.DynamicProperties;
6+
7+
internal static class PropIdToName
8+
{
9+
private static readonly string[] CommonProperties = [
10+
"TransparentFX",
11+
"_BumpMap",
12+
"_Color",
13+
"_EmissiveColor",
14+
"_MainTex",
15+
"_MaxX",
16+
"_MaxY",
17+
"_MinX",
18+
"_MinY",
19+
"_Multiplier",
20+
"_Opacity",
21+
"_RimColor",
22+
"_RimFalloff",
23+
"_TC1Color",
24+
"_TC1MetalBlend",
25+
"_TC1Metalness",
26+
"_TC1SmoothBlend",
27+
"_TC1Smoothness",
28+
"_TC2Color",
29+
"_TC2MetalBlend",
30+
"_TC2Metalness",
31+
"_TC2SmoothBlend",
32+
"_TC2Smoothness",
33+
"_TemperatureColor",
34+
"_Tex",
35+
"_Tint",
36+
"_TintColor",
37+
"_subdiv",
38+
"localMatrix",
39+
"upMatrix"
40+
];
41+
42+
private static readonly Dictionary<int, string> IdToName =
43+
CommonProperties.ToDictionary(Shader.PropertyToID, name => name);
44+
45+
internal static string Get(int id) =>
46+
IdToName.TryGetValue(id, out var name) ? name : $"<{id}>";
47+
}
48+
49+
internal abstract class Prop
50+
{
51+
internal abstract void Write(int id, MaterialPropertyBlock mpb);
52+
}
53+
54+
internal abstract class Prop<T>(T value) : Prop
55+
{
56+
internal T Value = value;
57+
58+
internal abstract bool UpdateIfChanged(T value);
59+
public override string ToString() => Value.ToString();
60+
}
61+
62+
internal class PropColor(Color value) : Prop<Color>(value)
63+
{
64+
internal override bool UpdateIfChanged(Color value)
65+
{
66+
if (Utils.ApproxEquals(value, Value)) return false;
67+
Value = value;
68+
return true;
69+
}
70+
71+
internal override void Write(int id, MaterialPropertyBlock mpb) => mpb.SetColor(id, Value);
72+
}
73+
74+
internal class PropFloat(float value) : Prop<float>(value)
75+
{
76+
internal override bool UpdateIfChanged(float value)
77+
{
78+
if (Utils.ApproxEqualsRel(value, Value)) return false;
79+
Value = value;
80+
return true;
81+
}
82+
83+
internal override void Write(int id, MaterialPropertyBlock mpb) => mpb.SetFloat(id, Value);
84+
}
85+
86+
internal class PropInt(int value) : Prop<int>(value)
87+
{
88+
internal override bool UpdateIfChanged(int value)
89+
{
90+
if (value == Value) return false;
91+
Value = value;
92+
return true;
93+
}
94+
95+
internal override void Write(int id, MaterialPropertyBlock mpb) => mpb.SetInt(id, Value);
96+
}
97+
98+
internal class PropTexture(Texture value) : Prop<Texture>(value)
99+
{
100+
internal override bool UpdateIfChanged(Texture value)
101+
{
102+
if (ReferenceEquals(value, Value)) return false;
103+
Value = value;
104+
return true;
105+
}
106+
107+
internal override void Write(int id, MaterialPropertyBlock mpb) => mpb.SetTexture(id, Value);
108+
}
109+
110+
internal class PropVector(Vector4 value) : Prop<Vector4>(value)
111+
{
112+
internal override bool UpdateIfChanged(Vector4 value)
113+
{
114+
if (Utils.ApproxEqualsRel(value, Value)) return false;
115+
Value = value;
116+
return true;
117+
}
118+
119+
internal override void Write(int id, MaterialPropertyBlock mpb) => mpb.SetVector(id, Value);
120+
}

Source/DynamicProperties/Props.cs

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,11 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Runtime.CompilerServices;
54
using KSPBuildTools;
65
using UnityEngine;
76

87
namespace Shabby.DynamicProperties;
98

10-
internal static class PropIdToName
11-
{
12-
private static readonly string[] CommonProperties = [
13-
"TransparentFX",
14-
"_BumpMap",
15-
"_Color",
16-
"_EmissiveColor",
17-
"_MainTex",
18-
"_MaxX",
19-
"_MaxY",
20-
"_MinX",
21-
"_MinY",
22-
"_Multiplier",
23-
"_Opacity",
24-
"_RimColor",
25-
"_RimFalloff",
26-
"_TC1Color",
27-
"_TC1MetalBlend",
28-
"_TC1Metalness",
29-
"_TC1SmoothBlend",
30-
"_TC1Smoothness",
31-
"_TC2Color",
32-
"_TC2MetalBlend",
33-
"_TC2Metalness",
34-
"_TC2SmoothBlend",
35-
"_TC2Smoothness",
36-
"_TemperatureColor",
37-
"_Tex",
38-
"_Tint",
39-
"_TintColor",
40-
"_subdiv",
41-
"localMatrix",
42-
"upMatrix"
43-
];
44-
45-
private static readonly Dictionary<int, string> IdToName =
46-
CommonProperties.ToDictionary(Shader.PropertyToID, name => name);
47-
48-
internal static string Get(int id) =>
49-
IdToName.TryGetValue(id, out var name) ? name : $"<{id}>";
50-
}
51-
52-
internal abstract class Prop
53-
{
54-
internal abstract void Write(int id, MaterialPropertyBlock mpb);
55-
}
56-
57-
internal abstract class Prop<T>(T value) : Prop
58-
{
59-
internal T Value = value;
60-
61-
internal abstract bool UpdateIfChanged(T value);
62-
public override string ToString() => Value.ToString();
63-
}
64-
65-
internal class PropColor(Color value) : Prop<Color>(value)
66-
{
67-
internal override bool UpdateIfChanged(Color value)
68-
{
69-
if (Utils.ApproxEquals(value, Value)) return false;
70-
Value = value;
71-
return true;
72-
}
73-
74-
internal override void Write(int id, MaterialPropertyBlock mpb) => mpb.SetColor(id, Value);
75-
}
76-
77-
internal class PropFloat(float value) : Prop<float>(value)
78-
{
79-
internal override bool UpdateIfChanged(float value)
80-
{
81-
if (Utils.ApproxEqualsRel(value, Value)) return false;
82-
Value = value;
83-
return true;
84-
}
85-
86-
internal override void Write(int id, MaterialPropertyBlock mpb) => mpb.SetFloat(id, Value);
87-
}
88-
89-
internal class PropInt(int value) : Prop<int>(value)
90-
{
91-
internal override bool UpdateIfChanged(int value)
92-
{
93-
if (value == Value) return false;
94-
Value = value;
95-
return true;
96-
}
97-
98-
internal override void Write(int id, MaterialPropertyBlock mpb) => mpb.SetInt(id, Value);
99-
}
100-
101-
internal class PropTexture(Texture value) : Prop<Texture>(value)
102-
{
103-
internal override bool UpdateIfChanged(Texture value)
104-
{
105-
if (ReferenceEquals(value, Value)) return false;
106-
Value = value;
107-
return true;
108-
}
109-
110-
internal override void Write(int id, MaterialPropertyBlock mpb) => mpb.SetTexture(id, Value);
111-
}
112-
113-
internal class PropVector(Vector4 value) : Prop<Vector4>(value)
114-
{
115-
internal override bool UpdateIfChanged(Vector4 value)
116-
{
117-
if (Utils.ApproxEqualsRel(value, Value)) return false;
118-
Value = value;
119-
return true;
120-
}
121-
122-
internal override void Write(int id, MaterialPropertyBlock mpb) => mpb.SetVector(id, Value);
123-
}
124-
1259
public sealed class Props(int priority) : IDisposable
12610
{
12711
/// Ordered by lowest to highest priority. Equal priority is disambiguated by unique IDs.

0 commit comments

Comments
 (0)