-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs.cs
More file actions
340 lines (305 loc) · 9.56 KB
/
js.cs
File metadata and controls
340 lines (305 loc) · 9.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using System;
using System.Collections.Generic;
using System.Windows;
namespace blekenbleu.jsonio
{
public partial class JSONio
{
// check whether current properties differ from JSON
bool Changed()
{
bool changed = false;
if ((0 > gndx || 0 > cndx))
if (!SaveSlim())
return changed;
// this should be unnecessary if slim.Reconcile() works..
if (gCount != slim.data.gList[gndx].cList[0].Vlist.Count
|| pCount != slim.data.gList[gndx].cList[cndx].Vlist.Count)
changed = true;
else for (int p = 0; p < gCount; p++)
if (simValues[p].Default != slim.data.gList[gndx].cList[0].Vlist[p] // per-game default change?
|| p < pCount && simValues[p].Current != slim.data.gList[gndx].cList[cndx].Vlist[p]) // per-car change?
{
changed = true;
break;
}
View.Model.ChangedVisibility = changed ? Visibility.Visible : Visibility.Hidden;
return changed;
}
internal void SetSlider()
{
if (0 > slider)
return;
View.Model.SliderProperty = simValues[slider].Name;
/* slider View.SL.Maximum = 100; scale property to it, based on Steps[slider]
; Steps Guestimated range
; 1 (0.01) 0 - 2
; 10 (0.10) 0 - 10
; 100 (1) 0 - 100
; 1000 (10) 0 - 1000
*/
if (100 < Convert.ToDouble(simValues[slider].Default))
{
SliderFactor[0] = 10; // slider to value
SliderFactor[1] = 0.1; // value to slider
}
else if (0 != Steps[slider] % 10)
{
SliderFactor[0] = 0.02; // slider to value
SliderFactor[1] = 50; // value to slider
} else if (0 != Steps[slider] % 100) {
SliderFactor[0] = 0.1; // slider to value
SliderFactor[1] = 10; // value to slider
} else {
SliderFactor[0] = 1; // slider to value
SliderFactor[1] = 1; // value to slider
}
ToSlider();
}
List<string> DefaultCopy() // called in SaveSlim(), End()
{
int i;
List<string> New = new List<string> { };
for (i = 0; i < gCount; i++)
New.Add(string.Copy(simValues[i].Default));
return New;
}
List<string> CurrentCopy() // called in SaveSlim()
{
int i;
List<string> New = new List<string> { };
for (i = 0; i < pCount; i++)
New.Add(string.Copy(simValues[i].Current));
return New;
}
int GameIndex(string gnew)
{
if (1 > gnew.Length)
return gndx; // should be unlikely
for (int g = 0; g < slim.data.gList.Count; g++)
if (0 == slim.data.gList[g].cList.Count
|| null == slim.data.gList[g].cList[0].Name)
slim.data.gList.RemoveAt(g--); // Reconcile() failure
else if (gnew == slim.data.gList[g].cList[0].Name)
gndx = g;
return gndx;
}
bool SaveSlim() // called in End(), CarChange() and maybe Changed()
{
if (null == CurrentCar || 0 == gCount)
return false; // nothing to save
if (0 > GameIndex(Gname)) // first car for this game?
{
write = true; // first car
gndx = slim.data.gList.Count;
slim.data.gList.Add(new GameList
{ cList = new List<CarL>
{ new CarL { Name = string.Copy(Gname),
Vlist = DefaultCopy()
}
}
}
);
}
if (0 > (cndx = slim.data.gList[gndx].cList.FindIndex(c => c.Name == CurrentCar)))
{ // add car to game
write = true; // add car
cndx = slim.data.gList[gndx].cList.Count;
slim.data.gList[gndx].cList.Add(new CarL
{ Name = string.Copy(CurrentCar),
Vlist = CurrentCopy()
}
);
} else { // property value changes?
for (int i = 0; i < gCount; i++)
if (slim.data.gList[gndx].cList[0].Vlist[i] != simValues[i].Default)
{
slim.data.gList[gndx].cList[0].Vlist = DefaultCopy();
write = true; // per-game property change
break;
}
for (int i = 0; i < pCount; i++)
if (slim.data.gList[gndx].cList[cndx].Vlist[i] != simValues[i].Current)
{
slim.data.gList[gndx].cList[cndx].Vlist = CurrentCopy();
write = true; // per-car property change
break;
}
}
return write; // SaveSlim()
}
// Control.xaml methods -------------------------------------------------
internal void FromSlider(double value)
{
simValues[slider].Current = (SliderFactor[0] * (int)value).ToString();
Changed();
View.Model.SliderProperty = simValues[slider].Name + ": " + simValues[slider].Current;
}
internal void ToSlider()
{
if(0 > slider)
return;
View.Model.SliderProperty = simValues[slider].Name + ": " + simValues[slider].Current;
View.Model.SliderValue = SliderFactor[1] * Convert.ToDouble(simValues[slider].Current);
}
/// <summary>
/// Helper functions used in Init() AddAction()s and Control.xaml.cs button Clicks
/// </summary>
/// <param name="sign"></param> should be 1 or -1
/// <param name="prefix"></param> should be "in" or "de"
public void Ment(int sign)
{
if (0 == Gname.Length || null == CurrentCar || 0 == CurrentCar.Length)
return;
int step = Steps[View.Selection];
int iv = (int)(0.004 + 100 * float.Parse(simValues[View.Selection].Current));
iv += sign * step;
if (0 <= iv)
{
if (0 != step % 100)
simValues[View.Selection].Current = $"{(float)(0.01 * iv)}";
else simValues[View.Selection].Current = $"{(int)(0.004 + 0.01 * iv)}";
Changed();
if (slider == View.Selection)
ToSlider();
}
}
private void SelectedStatus()
{
View.Model.SelectedProperty = (0 > View.Selection) ? "unKnown" : simValues[View.Selection].Name;
View.Model.StatusText = Gname + " " + CurrentCar + ":\t" + View.Model.SelectedProperty;
}
/// <summary>
/// Select next or prior property; exception if invoked on other than UI thread
/// </summary>
/// <param name="next"></param> false for prior
public void Select(bool next)
{
if (0 == Gname.Length || null == CurrentCar || 0 == CurrentCar.Length)
return;
if (next)
{
if (++View.Selection >= simValues.Count)
View.Selection = 0;
}
else if (0 < View.Selection) // prior
View.Selection--;
else View.Selection = (byte)(simValues.Count - 1);
SelectedStatus(); // Select()
}
public void Swap()
{
string temp;
for (int i = 0; i < simValues.Count; i++)
{
temp = simValues[i].Previous;
simValues[i].Previous = simValues[i].Current;
simValues[i].Current = temp;
}
ToSlider(); // Swap()
Changed();
}
// set "CurrentAsDefaults" action
internal void SetDefault() // List<GameList> Glist)
{
if (0 == Gname.Length)
OOps("SetDefault: no Gname");
else {
int p = View.Selection;
simValues[p].Default = simValues[p].Current; // End() sorts per-game changes
Changed();
}
}
// set "SelectedAsSlider" action
internal void SelectSlider() // List<GameList> Glist)
{
slider = View.Selection;
SetSlider();
}
/*--------------------------------------------------------------
; invoked for CarId changes, based on this `NCalcScripts/JSONio.ini` entry:
; [ExportEvent]
; name='CarChange'
; trigger=changed(200, [DataCorePlugin.GameData.CarId])
;--------------------------------------------------------------- */
void CarChange(string cname, string gnew, bool once)
{
int ml = 0;
if (0 == simValues.Count)
return;
if (null !=cname && 0 < cname.Length && null != gnew && 0 < gnew.Length) // valid?
{
GameList game = null;
int i, count = 0, vcount = 0;
Msg = "Current Car: " + cname;
if (0 < Gname.Length && SaveSlim()) // do not save first instance
Msg += $"; {CurrentCar} saved";
ml = Msg.Length;
for (i = 0; i < simValues.Count; i++) // copy Current to previous
simValues[i].Previous = simValues[i].Current;
// indices for new car
if (0 <= GameIndex(gnew)) // sets gndx
{
game = slim.data.gList[gndx];
cndx = game.cList.FindIndex(c => c.Name == cname);
vcount = game.cList[0].Vlist.Count;
count = gCount > vcount ? vcount : gCount;
}
else cndx = -1;
if (0 > cndx)
{
NewCar = "true";
if (0 <= gndx) // set at line 132
{ // not a new game
if (gnew != Settings.game)
{ // different game
count = pCount > vcount ? vcount : pCount;
for (i = 0; i < count; i++) // per-car defaults
simValues[i].Default = game.cList[0].Vlist[i];
}
for (i = pCount; i < count; i++) // per-game defaults
simValues[i].Default = game.cList[0].Vlist[i]; // perhaps altered since .ini
}
}
else
{ // existing car
NewCar = "false";
if (cname != Settings.carid) // previous car?
for (i = 0; i < pCount; i++)
simValues[i].Current = game.cList[cndx].Vlist[i];
if (null == CurrentCar) // first in this game instance?
{ // restore game defaults
count = pCount > vcount ? vcount : pCount;
for (i = 0; i < count; i++)
simValues[i].Default = game.cList[0].Vlist[i];
count = gCount > vcount ? vcount : gCount;
for(i = pCount; i < count; i++)
simValues[i].Current = simValues[i].Default = game.cList[0].Vlist[i];
}
}
Settings.carid = CurrentCar = cname;
Changed();
}
else if (null == cname)
Msg = "null CarID";
else if (0 == cname.Length)
Msg = "empty CarID";
if (null == gnew)
Msg += ", null CurrentGame Name, ";
else if (0 == gnew.Length)
Msg += ", empty CurrentGame Name, ";
else Gname = gnew;
if (ml < Msg.Length)
{
if (once)
Msg = "";
else OOpsMB();
return;
}
else Msg = "";
SelectedStatus(); // CarChange()
ToSlider();
View.Model.ButtonVisibility = System.Windows.Visibility.Visible; // ready
} // CarChange()
} // public partial class JSONio
}