1 | using System;
|
---|
2 | using Oni.Imaging;
|
---|
3 |
|
---|
4 | namespace Oni.Particles
|
---|
5 | {
|
---|
6 | internal class Value
|
---|
7 | {
|
---|
8 | public const int ByteSize = 28;
|
---|
9 | public static readonly Value Empty = new Value(ValueType.Variable, string.Empty);
|
---|
10 | public static readonly Value FloatZero = new Value(0.0f);
|
---|
11 | public static readonly Value FloatOne = new Value(1.0f);
|
---|
12 |
|
---|
13 | #region Private data
|
---|
14 | private ValueType type;
|
---|
15 | private string name;
|
---|
16 | private float f1, f2;
|
---|
17 | private Color c1, c2;
|
---|
18 | private int i;
|
---|
19 | #endregion
|
---|
20 |
|
---|
21 | public Value(ValueType type, float value1, float value2)
|
---|
22 | {
|
---|
23 | this.type = type;
|
---|
24 | this.f1 = value1;
|
---|
25 | this.f2 = value2;
|
---|
26 | }
|
---|
27 |
|
---|
28 | public Value(float value)
|
---|
29 | : this(ValueType.Float, value, 0.0f)
|
---|
30 | {
|
---|
31 | }
|
---|
32 |
|
---|
33 | public Value(int value)
|
---|
34 | {
|
---|
35 | this.type = ValueType.Int32;
|
---|
36 | this.i = value;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public Value(ValueType type, string name)
|
---|
40 | {
|
---|
41 | this.type = type;
|
---|
42 | this.name = name;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public Value(Color color)
|
---|
46 | : this(ValueType.Color, color, Color.Black)
|
---|
47 | {
|
---|
48 | }
|
---|
49 |
|
---|
50 | public Value(ValueType type, Color color1, Color color2)
|
---|
51 | {
|
---|
52 | this.type = type;
|
---|
53 | this.c1 = color1;
|
---|
54 | this.c2 = color2;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public static Value Read(BinaryReader reader)
|
---|
58 | {
|
---|
59 | int startPosition = (int)reader.Position;
|
---|
60 |
|
---|
61 | ValueType type = (ValueType)reader.ReadInt32();
|
---|
62 | Value result = null;
|
---|
63 |
|
---|
64 | switch (type)
|
---|
65 | {
|
---|
66 | case ValueType.Variable:
|
---|
67 | string name = reader.ReadString(16);
|
---|
68 | if (!string.IsNullOrEmpty(name))
|
---|
69 | result = new Value(type, name);
|
---|
70 | break;
|
---|
71 |
|
---|
72 | case ValueType.InstanceName:
|
---|
73 | result = new Value(type, reader.ReadString(16));
|
---|
74 | break;
|
---|
75 |
|
---|
76 | case ValueType.Float:
|
---|
77 | result = new Value(reader.ReadSingle());
|
---|
78 | break;
|
---|
79 |
|
---|
80 | case ValueType.FloatRandom:
|
---|
81 | case ValueType.FloatBellCurve:
|
---|
82 | case ValueType.TimeCycle:
|
---|
83 | result = new Value(type, reader.ReadSingle(), reader.ReadSingle());
|
---|
84 | break;
|
---|
85 |
|
---|
86 | case ValueType.Color:
|
---|
87 | result = new Value(reader.ReadColor());
|
---|
88 | break;
|
---|
89 |
|
---|
90 | case ValueType.ColorRandom:
|
---|
91 | case ValueType.ColorBellCurve:
|
---|
92 | result = new Value(type, reader.ReadColor(), reader.ReadColor());
|
---|
93 | break;
|
---|
94 |
|
---|
95 | case ValueType.Int32:
|
---|
96 | result = new Value(reader.ReadInt32());
|
---|
97 | break;
|
---|
98 | }
|
---|
99 |
|
---|
100 | reader.Position = startPosition + ByteSize;
|
---|
101 |
|
---|
102 | return result;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public void Write(BinaryWriter writer)
|
---|
106 | {
|
---|
107 | int startPosition = (int)writer.Position;
|
---|
108 |
|
---|
109 | writer.Write((int)type);
|
---|
110 |
|
---|
111 | switch (type)
|
---|
112 | {
|
---|
113 | case ValueType.Variable:
|
---|
114 | case ValueType.InstanceName:
|
---|
115 | writer.Write(name, 16);
|
---|
116 | break;
|
---|
117 |
|
---|
118 | case ValueType.Float:
|
---|
119 | writer.Write(f1);
|
---|
120 | break;
|
---|
121 |
|
---|
122 | case ValueType.FloatRandom:
|
---|
123 | case ValueType.FloatBellCurve:
|
---|
124 | case ValueType.TimeCycle:
|
---|
125 | writer.Write(f1);
|
---|
126 | writer.Write(f2);
|
---|
127 | break;
|
---|
128 |
|
---|
129 | case ValueType.Color:
|
---|
130 | writer.Write(c1);
|
---|
131 | break;
|
---|
132 |
|
---|
133 | case ValueType.ColorRandom:
|
---|
134 | case ValueType.ColorBellCurve:
|
---|
135 | writer.Write(c1);
|
---|
136 | writer.Write(c2);
|
---|
137 | break;
|
---|
138 |
|
---|
139 | case ValueType.Int32:
|
---|
140 | writer.Write(i);
|
---|
141 | break;
|
---|
142 | }
|
---|
143 |
|
---|
144 | writer.Position = startPosition + ByteSize;
|
---|
145 | }
|
---|
146 |
|
---|
147 | public ValueType Type => type;
|
---|
148 | public string Name => name;
|
---|
149 | public float Float1 => f1;
|
---|
150 | public float Float2 => f2;
|
---|
151 | public Color Color1 => c1;
|
---|
152 | public Color Color2 => c2;
|
---|
153 | public int Int => i;
|
---|
154 | }
|
---|
155 | }
|
---|