-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElectronPanel_.cs
More file actions
1277 lines (1052 loc) · 40.4 KB
/
ElectronPanel_.cs
File metadata and controls
1277 lines (1052 loc) · 40.4 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using NWN2Toolset;
using NWN2Toolset.NWN2.Data.Blueprints;
using NWN2Toolset.NWN2.Data.Instances;
using NWN2Toolset.NWN2.Data.Templates;
using NWN2Toolset.NWN2.Data.TypedCollections;
using NWN2Toolset.NWN2.NetDisplay;
using NWN2Toolset.NWN2.Views;
using OEIShared.IO;
using OEIShared.NetDisplay;
using OEIShared.OEIMath;
using OEIShared.UI;
using OEIShared.UI.Input;
using OEIShared.Utils;
namespace creaturevisualizer
{
/// <summary>
/// Credit: The Grinning Fool's Creature Creation Wizard
/// https://neverwintervault.org/project/nwn2/other/grinning-fools-creature-creation-wizard
/// and the NwN2 toolset's Appearance Wizard, etc.
/// Etc etc etc.
/// </summary>
sealed class ElectronPanel_
: ElectronPanel
{
#region Fields (static)
// Camera ->
internal const float CAM_START_TET = (float)Math.PI / 2F;
internal const float CAM_START_PHI = (float)Math.PI / 32F;
internal const float CAM_START_DIST = 5F;
internal static Vector3 CAM_START_POS;
internal static Vector3 CAM_BASEHEIGHT = new Vector3(0F,0F, CreatureVisualizerPreferences.that.CameraBaseHeight);
// Model ->
const float MODEL_START_ROT = (float)Math.PI * 7F / 8F;
static Vector3 ScaInitial;
// Light ->
internal static Vector3 LIGHT_START_POS = new Vector3(-0.5F, -4F, 2F);
const float LIGHT_START_RANGE = 50F; // default 10F
// DayNightCycle ->
static Vector3 DEF_SUNMOON_DIRECTION = new Vector3(-0.33F, -0.67F, -0.67F);
const float DEF_SHADOW_INTENSITY = 0F;
// Colors ->
internal static Color ? ColorDiffuse;
internal static Color ? ColorSpecular;
internal static Color ? ColorAmbient;
internal static bool ColorCheckedDiffuse;
internal static bool ColorCheckedSpecular;
internal static bool ColorCheckedAmbient;
#endregion Fields (static)
#region Fields
readonly CreVisF _f;
#endregion Fields
#region Properties
// Note that when an Instance is selected the visualizer uses that
// Instance. But when a Blueprint is selected the visualizer creates
// its own instance of the Blueprint. The instance will then be used to
// instantiate a NetDisplayObject aka the 'Model'.
//
// That is, in order to test whether a Blueprint or an Instance is
// currently displayed, test Blueprint==null (NOT Instance==null)
// since objects based on Blueprints *do have an Instance*.
/// <summary>
/// The currently selected Blueprint. Can be changed by the "Apply"
/// operation.
/// </summary>
internal INWN2Blueprint Blueprint_base
{ get; set; }
/// <summary>
/// The currently displayed Blueprint (ie, instantiate a duplicate of
/// 'Blueprint_base'). Can be changed by the "Display" operation.
/// </summary>
internal INWN2Blueprint Blueprint
{ get; set; }
/// <summary>
/// The currently selected Instance. Can be changed by the "Apply"
/// operation.
/// </summary>
internal INWN2Instance Instance_base
{ get; set; }
INWN2Instance _instance;
/// <summary>
/// The currently displayed Instance (ie, instantiate a duplicate of
/// 'Instance_base'). Can be changed by the "Display" operation.
/// </summary>
internal INWN2Instance Instance
{
get { return _instance; }
private set
{
bool valid = (_instance = value) != null
&& _instance is NWN2CreatureTemplate;
_f.EnableSaveToModule(valid);
_f.EnableSaveToCampaign(valid);
_f.EnableSaveToFile(valid);
}
}
readonly NetDisplayObjectCollection _objects = new NetDisplayObjectCollection();
NetDisplayObjectCollection _objectsModel;
NetDisplayObjectCollection _objectsLight;
internal NetDisplayObject Model
{ get; private set; }
internal NetDisplayLightPoint Light
{ get; private set; }
internal ModelViewerInputCameraReceiver Receiver
{ get; private set; }
#endregion Properties
/* /// <summary>
/// This works great. Absolutely kills flicker on redraws.
/// </summary>
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
} */
#region cTor
internal ElectronPanel_(CreVisF f)
{
_f = f;
// DoubleBuffered = true;
RecreateMousePanel();
OpenWindow();
_t2.Tick += tick;
_t2.Interval = 35;
}
/* /// <summary>
/// Calls SetDoubleBuffered(object) on an array of objects.
/// </summary>
/// <param name="controls"></param>
static void SetDoubleBuffered(object[] controls)
{
foreach (var control in controls)
SetDoubleBuffered(control);
}
/// <summary>
/// Some controls, such as the DataGridView, do not allow setting the
/// DoubleBuffered property. It is set as a protected property. This
/// method is a work-around to allow setting it. Call this in the
/// constructor just after InitializeComponent().
/// https://stackoverflow.com/questions/118528/horrible-redraw-performance-of-the-datagridview-on-one-of-my-two-screens#answer-16625788
/// @note I wonder if this works on Mono. It stops the redraw-flick when
/// setting the sprite-phase on return from SpritesetviewF on my system
/// (Win7-64). Also stops flicker on the IsoLoft panel. etc.
/// </summary>
/// <param name="control">the Control on which to set DoubleBuffered to true</param>
static void SetDoubleBuffered(object control)
{
// if not remote desktop session then enable double-buffering optimization
if (!SystemInformation.TerminalServerSession)
{
// set instance non-public property with name "DoubleBuffered" to true
typeof(Control).InvokeMember("DoubleBuffered",
System.Reflection.BindingFlags.SetProperty
| System.Reflection.BindingFlags.Instance
| System.Reflection.BindingFlags.NonPublic,
null,
control,
new object[] { true });
}
} */
/// <summary>
/// Disposes their MousePanel and instantiates a new MousePanel to
/// replace it. CreatureVisualizer-specific inputhandlers can be hooked
/// up to this ElectronPanel that totally bypass the tangle of Obsidian
/// inputhandlers. good luck
/// @note A 'MousePanel' is not a panel nor does it handle mouse-input
/// exclusively; it should have been labeled 'InputController'.
/// </summary>
void RecreateMousePanel()
{
MousePanel.Dispose(); // OMG it effin worked.
MousePanel = new MousePanel();
var resourcer = new ComponentResourceManager(typeof(ElectronPanel));
resourcer.ApplyResources(MousePanel, "MousePanel");
MousePanel.Name = "MousePanel";
MousePanel.ThrottleMouse = true; // no idea ...
CameraMovementReceiver = new ModelViewerInputCameraReceiver();
AddInputReceiver(CameraMovementReceiver);
Receiver = CameraMovementReceiver as ModelViewerInputCameraReceiver;
// MousePanel.MouseDown += ᐁ;
// MousePanel.MouseMove += ᐄ;
// MousePanel.MouseButtonHeld += ᐃ;
// MousePanel.KeyUp += ᐂ;
// MousePanel.DragHandler += ᐁ;
// MousePanel.KeyPress += ᐁ;
// MousePanel.MouseUp += ᐂ;
// MousePanel.KeyDown += ᐁ;
// MousePanel.MouseWheel += ᐃ;
// MousePanel.LostFocus += ᐁ;
MousePanel.MouseDown += mousedown;
MousePanel.MouseUp += mouseup;
Controls.Add(MousePanel);
}
#endregion cTor
#region Methods
/// <summary>
/// Creates an instance of a blueprint and tries to render it in the
/// ElectronPanel.
/// </summary>
internal void CreateModel()
{
//MessageBox.Show("CreateModel()");
// IMPORTANT: Policy #256 - *never* allow the instance to be other
// than the Blueprint or Instance that the user has currently
// selected. This will greatly ease the "Apply to ..." operation.
// THE INSTANCE DISPLAYED IN THE VISUALIZER MUST ALWAYS REFERENCE
// THE BLUEPRINT OR INSTANCE THAT THE USER HAS CURRENTLY SELECTED.
//
// That is, if the user clicks away from his/her currently selected
// Blueprint or Instance (ie. by selecting a different Blueprint or
// Instance) clear the visualizer-display after asking to either
// Save or Clear - but only if the currently displayed instance has
// actually been changed ofc w/ "Display". Note that if an
// instance's displayed characteristics have been "Applied" to the
// current Blueprint/Instance then no confirmation is required to
// instantiate an instance that's different than the current
// Blueprint/Instance.
//
// IMPORTANT: Policy #257 - it is the user's responsibility to save
// any applied changes before closing the toolset. Note, "save to
// file" needs to be wired up to write the applied instance; it can
// and will bypass most of these shenanigans.
if (!CreVisF.BypassCreate) // don't recreate the instance when returning from a dialog when "RefreshOnFocus" is enabled.
{
// _f._bypassInsert = true; // prevent anything in here from firing CreVisF.OnObjectsInserted()
_f._bypassAppearanceChanged = true; // prevent anything in here from firing CreVisF.OnAppearanceChanged()
// if (Instance != null)
// {
// // ask to ignore, Apply (if not stock resource), or save-to-file (disable the Cancel option)
// _f.ConfirmClose(false);
// }
StoreTelemetry();
lock (NWN2NetDisplayManager.Instance.Objects.SyncRoot) // not sure ...
{
NWN2NetDisplayManager.Instance.RemoveObjects(_objects);
}
_objects.Clear();
_f.ClearResourceInfo();
// _f.Changed = CreVisF.ChangedType.ct_nul; // set '_f.Text'
Blueprint = null; // is instantiated only by a Blueprint
Instance = null; // is instantiated by either a Blueprint or an Instance
Model = null;
// _f.bu_creature_apply1.Enabled =
// _f.bu_creature_apply2.Enabled = false;
// _f.EnableCreaturePages(false);
if (MousePanel != null && !MousePanel.IsDisposed) // safety. ElectronPanel.MousePanel could go disposed for no good reason.
{
bool different = false;
// first check areaviewer for a selected Instance ->
NWN2AreaViewer areaviewer;
NWN2InstanceCollection collection;
if ((areaviewer = NWN2ToolsetMainForm.App.GetActiveViewer() as NWN2AreaViewer) != null
&& (collection = areaviewer.SelectedInstances) != null && collection.Count == 1
&& ( collection[0] is NWN2CreatureTemplate
|| collection[0] is NWN2DoorTemplate
|| collection[0] is NWN2PlaceableTemplate))
{
if (Instance_base == null || (collection[0] as INWN2Instance) != Instance_base)
{
Instance_base = collection[0] as INWN2Instance;
different = true;
}
// NOTE: Instances without any value for "Template" have a null template ResourceEntry
// while Instances with an invalid value for "Template" are ResourceType 0 .RES
Instance = CommonUtils.SerializationClone(Instance_base) as INWN2Instance;
Instance.Area = Instance_base.Area; // req'd.
// string info = String.Empty;
// info += "area= " + (Instance.Area != null ? Instance.Area.Name + " (" + Instance.Area.Tag + ")" : "null");
// info += "\ncomment= " + Instance.Comment;
// info += "\nobjectId= " + Instance.ObjectID;
// info += "\nresourcetype= " + (Instance.Template != null ? Instance.Template.ResourceType.ToString() : "null");
// info += "\nfullname= " + (Instance.Template != null ? Instance.Template.FullName : "null");
// info += "\nresref= " + (Instance.Template.ResRef != null ? Instance.Template.ResRef.Value : "null");
// info += "\nrepo= " + (Instance.Template.Repository != null ? Instance.Template.Repository.Name : "null");
// MessageBox.Show(info);
switch ((Instance as INWN2Template).ObjectType)
{
case NWN2ObjectType.Creature:
Io.AppearanceSEF = (Instance as NWN2CreatureTemplate).AppearanceSEF;
(Instance as NWN2CreatureTemplate).AppearanceSEF = null;
ProcessEquippedItems(Instance as NWN2CreatureTemplate);
ProcessInventory(Instance as INWN2Template);
break;
case NWN2ObjectType.Item:
(Instance as NWN2ItemTemplate).AppearanceSEF = null;
break;
case NWN2ObjectType.Placeable:
(Instance as NWN2PlaceableTemplate).AppearanceSEF = null;
break;
}
_f.PrintResourceInfo(Instance);
// if (Instance.ObjectType == NWN2ObjectType.Creature)
// {
// _f.bu_creature_apply1.Enabled =
// _f.bu_creature_apply2.Enabled = Instance.Template != null; // NOTE: 'Template' should actually be 'Resource' or 'TemplateResource'
// _f.EnableCreaturePages(true); // like ya know 'Blueprint.Resource' is ... since it's not a 'Template'
// // it's a 'ResourceEntry'. 'Template' has a distinct meaning and it's
// _f.InitializeCreaturePages(Instance as NWN2CreatureTemplate); // not 'ResourceEntry'.
// }
// _f.bu_creature_apply1.Text =
// _f.bu_creature_apply2.Text = "APPLY to Instance";
}
// second check blueprint tree for a selected Blueprint ->
else
{
object[] selection = NWN2ToolsetMainForm.App.BlueprintView.Selection;
if (selection != null && selection.Length == 1)
{
switch ((selection[0] as INWN2Template).ObjectType) // better not be null
{
case NWN2ObjectType.Creature:
case NWN2ObjectType.Door:
case NWN2ObjectType.Item:
case NWN2ObjectType.Placeable:
case NWN2ObjectType.PlacedEffect:
if (Blueprint_base == null || (selection[0] as INWN2Blueprint) != Blueprint_base)
{
Blueprint_base = selection[0] as INWN2Blueprint;
different = true;
}
DuplicateBlueprint();
_f.PrintResourceInfo(Blueprint);
// var resource0 = Blueprint.Resource;
// IResourceRepository repo = NWN2ToolsetMainForm.App.Module.Repository;
// var resource1 = repo.FindResource(new OEIResRef(Blueprint.Resource.ResRef.Value), 2027);
// MessageBox.Show("is resource0 valid= " + (resource0 != null) + "\n"
// + "is resource1 valid= " + (resource1 != null) + "\n"
// + "is resource0 resource1= " + (resource0 == resource1) + "\n"
// + "is resource0 equal to resource1= " + resource0.Equals(resource1));
Instance = NWN2GlobalBlueprintManager.CreateInstanceFromBlueprint(Blueprint);
/* switch (type)
{
case NWN2ObjectType.Creature:
// _f.bu_creature_apply1.Enabled =
// _f.bu_creature_apply2.Enabled = (Blueprint.Resource.Repository as DirectoryResourceRepository) != null;
// _f.EnableCreaturePages(true);
// _f.InitializeCreaturePages(Blueprint as NWN2CreatureTemplate);
// ((NWN2CreatureBlueprint)blueprint).AppearanceHair; // byte
// // etc ...
//
// // bool ->
// ((NWN2CreatureBlueprint)blueprint).AppearanceFacialHair;
//
// ((NWN2CreatureBlueprint)blueprint).HasBelt;
// ((NWN2CreatureBlueprint)blueprint).HasBoots;
// ((NWN2CreatureBlueprint)blueprint).HasCloak;
// ((NWN2CreatureBlueprint)blueprint).HasGloves;
// ((NWN2CreatureBlueprint)blueprint).HasHelm;
//
// ((NWN2CreatureBlueprint)blueprint).NeverDrawHelmet;
// ((NWN2CreatureBlueprint)blueprint).NeverShowArmor;
//
// // TwoDAReference ->
// ((NWN2CreatureBlueprint)blueprint).Tail;
// ((NWN2CreatureBlueprint)blueprint).Wings;
//
// // OEITintSet ->
// ((NWN2CreatureBlueprint)blueprint).BaseTint;
// ((NWN2CreatureBlueprint)blueprint).Tint;
// ((NWN2CreatureBlueprint)blueprint).TintHair;
// ((NWN2CreatureBlueprint)blueprint).TintHead;
//
// // Color ->
// ((NWN2CreatureBlueprint)blueprint).TintArmor1;
// ((NWN2CreatureBlueprint)blueprint).TintArmor2;
// ((NWN2CreatureBlueprint)blueprint).TintEyes;
// ((NWN2CreatureBlueprint)blueprint).TintFacialHair;
// ((NWN2CreatureBlueprint)blueprint).TintHair1;
// ((NWN2CreatureBlueprint)blueprint).TintHair2;
// ((NWN2CreatureBlueprint)blueprint).TintHairAccessory;
// ((NWN2CreatureBlueprint)blueprint).TintSkin;
goto case NWN2ObjectType.Item;
case NWN2ObjectType.Door:
case NWN2ObjectType.Placeable:
case NWN2ObjectType.PlacedEffect:
case NWN2ObjectType.Item: // <- TODO: works for weapons (see Preview tab) but clothes appear on a default creature (in the ArmorSet tab)
Instance = NWN2GlobalBlueprintManager.CreateInstanceFromBlueprint(Blueprint);
break;
} */
break;
}
}
// _f.bu_creature_apply1.Text =
// _f.bu_creature_apply2.Text = "APPLY to Blueprint";
}
AddModel(different);
}
// _f._bypassInsert = false;
_f._bypassAppearanceChanged = false;
}
_f.SetTitleText();
}
/// <summary>
/// Clones the toolset blueprint to 'Blueprint' ... and creates a valid
/// IResourceEntry for it.
/// TODO: A new resource doesn't have to be created here, but only if
/// the Blueprint/Instance is saved to a file that doesn't already have
/// a resource.
/// Cf. Io.CreateBlueprint()
/// - based on
/// NWN2Toolset.NWN2.Data.Blueprints.NWN2CreatureBlueprint.CreateFromBlueprint(NWN2CreatureBlueprint, IResourceRepository, bool)
/// - see also
/// NWN2Toolset.NWN2.Data.Blueprints.NWN2GlobalBlueprintManager.CreateCopyOfBlueprint(INWN2Blueprint, INWN2BlueprintSet, IResourceRepository, bool)
/// </summary>
void DuplicateBlueprint() // attempt at cloning the toolset blueprint to 'Blueprint' ... and creating a valid Resource for it
{
switch (Blueprint_base.ObjectType)
{
case NWN2ObjectType.Creature: Blueprint = new NWN2CreatureBlueprint(); break;
case NWN2ObjectType.Door: Blueprint = new NWN2DoorBlueprint(); break;
case NWN2ObjectType.Item: Blueprint = new NWN2ItemBlueprint(); break;
case NWN2ObjectType.Placeable: Blueprint = new NWN2PlaceableBlueprint(); break;
case NWN2ObjectType.PlacedEffect: Blueprint = new NWN2PlacedEffectBlueprint(); break;
}
(Blueprint as INWN2Template).CopyFromTemplate(Blueprint_base as INWN2Template);
// IMPORTANT: The resource owned by the blueprint has equal values
// to but is not the same resource-object as the resource owned by
// the respository.
Blueprint.BlueprintLocation = Blueprint_base.BlueprintLocation; // note: enum 'NWN2BlueprintLocationType' ought be value-type (ie. copied not referenced)
Blueprint.Resource = CommonUtils.SerializationClone(Blueprint_base.Resource) as IResourceEntry;
Blueprint.TemplateResRef = CommonUtils.SerializationClone(Blueprint_base.TemplateResRef) as OEIResRef;
Blueprint.Comment = String.Copy(Blueprint_base.Comment); // note: strings are immutable
// TODO: AppearanceSEF creates a latent bug. Can freeze toolset ...
// esp. after the visualizer closes and re-opens.
// Workaround: Store a pointer in 'Io' to the Sef and re-apply it
// if the blueprint is saved. But null it here.
switch ((Blueprint as INWN2Template).ObjectType)
{
case NWN2ObjectType.Creature:
Io.AppearanceSEF = (Blueprint as NWN2CreatureTemplate).AppearanceSEF;
(Blueprint as NWN2CreatureTemplate).AppearanceSEF = null;
// (Blueprint as NWN2CreatureTemplate).AppearanceSEF = (IResourceEntry)CommonUtils.SerializationClone((Blueprint_base as NWN2CreatureTemplate).AppearanceSEF); // nope.
ProcessEquippedItems(Blueprint as NWN2CreatureTemplate);
ProcessInventory(Blueprint as INWN2Template);
break;
case NWN2ObjectType.Item:
(Blueprint as NWN2ItemTemplate).AppearanceSEF = null;
break;
case NWN2ObjectType.Placeable:
(Blueprint as NWN2PlaceableTemplate).AppearanceSEF = null;
break;
}
}
/* INWN2Instance CreateInstance()
{
// NOTE: Instances without any value for "Template" have a null template ResourceEntry
// while Instances with an invalid value for "Template" are ResourceType 0 .RES
if (Instance_base.Template == null // TODO: allow Instances w/out a valid Template ... (although it should be discouraged)
|| ( Instance_base.Template.ResourceType != (ushort)2027 // utc
&& Instance_base.Template.ResourceType != (ushort)2042 // utd
&& Instance_base.Template.ResourceType != (ushort)2044)) // utp
{
CreVisF.BypassCreate = true;
using (var f = new ErrorF("The instance's Template is invalid."))
f.ShowDialog();
CreVisF.BypassCreate = false;
}
else
{
var iinstance = CommonUtils.SerializationClone(Instance_base) as INWN2Instance;
iinstance.Area = Instance_base.Area;
if (iinstance.ObjectType == NWN2ObjectType.Creature)
{
ProcessEquippedItems(iinstance as NWN2CreatureTemplate);
ProcessInventory(iinstance as INWN2Template);
}
return iinstance;
}
return null;
} */
void ProcessEquippedItems(NWN2CreatureTemplate template)
{
for (int i = 0; i != template.EquippedItems.Count; ++i)
{
switch (template.EquippedItems[i].Slot)
{
case "Head": // TODO: figure out how to use legit enums for these ->
case "Chest":
case "Boots":
case "Arms":
case "Cloak":
case "Left Ring":
case "Right Ring":
case "Neck":
case "Belt":
case "Arrows":
case "Bullets":
case "Bolts":
case "Creature Weapon 1":
case "Creature Weapon 2":
case "Creature Weapon 3":
case "Creature Hide":
if (!CreatureVisualizerPreferences.that.ProcessEquipped_body)
{
template.EquippedItems[i].Item = null;
}
break;
case "Right Hand":
case "Left Hand":
if (!CreatureVisualizerPreferences.that.ProcessEquipped_held)
{
template.EquippedItems[i].Item = null;
}
break;
}
}
}
void ProcessInventory(INWN2Template itemplate)
{
if (CreatureVisualizerPreferences.that.ProcessInventory)
{
if ((itemplate as NWN2CreatureBlueprint) != null)
{
object inventory = CommonUtils.SerializationClone((Blueprint_base as NWN2CreatureBlueprint).Inventory);
(itemplate as NWN2CreatureBlueprint).Inventory = inventory as NWN2BlueprintInventoryItemCollection;
}
else if ((itemplate as NWN2CreatureInstance) != null)
{
object inventory = CommonUtils.SerializationClone((Instance_base as NWN2CreatureInstance).Inventory);
(itemplate as NWN2CreatureInstance).Inventory = inventory as NWN2InstanceInventoryItemCollection;
}
}
else
{
if ((itemplate as NWN2CreatureBlueprint) != null)
{
(itemplate as NWN2CreatureBlueprint).Inventory.Clear();
}
else if ((itemplate as NWN2CreatureInstance) != null)
{
(itemplate as NWN2CreatureInstance).Inventory.Clear();
}
}
}
/* /// <summary>
/// Updates the NetDisplayModel after changing creature characteristics.
/// </summary>
internal void UpdateModel()
{
//MessageBox.Show("RecreateModel()");
if (Blueprint != null)
{
Instance = NWN2GlobalBlueprintManager.CreateInstanceFromBlueprint(Blueprint); // do you really want to trust that
AddModel();
// NWN2NetDisplayManager.Instance.HandleAppearanceChange();
// NWN2NetDisplayManager.Instance.UpdateAppearanceForCreatureInventory();
// NWN2NetDisplayManager.Instance.UpdateAppearanceForInstance();
// NWN2NetDisplayManager.Instance.Update();
}
else if (Instance != null)
{
AddModel();
}
} */
bool _inited;
// Camera ->
float _xy = CAM_START_TET;
float _yz = CAM_START_PHI;
float _dist = CAM_START_DIST;
// Model ->
Vector3 _pos = new Vector3(0F,0F,0F); // position of the Model
RHQuaternion _rot = RHQuaternion.RotationZ(MODEL_START_ROT); // rotation of the Model
Vector3 _sca; // scale of the Model // no default, usually 1,1,1 but each creature has its own set in Appearances.2da
// Light ->
Vector3 _posLight = LIGHT_START_POS;
/// <summary>
/// Stores the previous state of this panel, before another Model loads,
/// so that the scene can be created as before, after the Model loads.
/// </summary>
void StoreTelemetry()
{
if (_inited)
{
if (Receiver != null)
{
_xy = Receiver.CameraAngleXY;
_yz = Receiver.CameraAngleYZ;
_dist = Receiver.Distance;
}
if (Model != null)
{
_pos = Model.Position;
_rot = Model.Orientation;
_sca = Model.Scale;
}
if (Light != null)
{
_posLight = Light.Position;
}
}
}
/// <summary>
/// Adds a model-instance to the scene.
/// </summary>
/// <param name="different"></param>
void AddModel(bool different)
{
//MessageBox.Show("AddModel()");
if (Instance != null && Initialize())
{
// _f.Changed = CreVisF.ChangedType.ct_non;
Receiver.CameraAngleXY = _xy;
Receiver.CameraAngleYZ = _yz;
Receiver.Distance = _dist;
if (!_inited)
{
_inited = true;
Receiver.DistanceMin = 0.001F;
CAM_START_POS = CameraPosition;
}
CameraPosition += CAM_BASEHEIGHT + CreVisF.Offset;
_f.PrintCameraPosition();
Instance.BeginAppearanceUpdate();
// create Model ->
Model = NWN2NetDisplayManager.Instance.CreateNDOForInstance(Instance, Scene, 0); // 0=NetDisplayModel
_objects.Add(Model);
// set Model position ->
_objectsModel = new NetDisplayObjectCollection() { Model }; // can't move a single object - only a collection (of 1).
NWN2NetDisplayManager.Instance.MoveObjects(_objectsModel, ChangeType.Absolute, false, _pos);
// set Model rotation ->
Model.Orientation = _rot;
NWN2NetDisplayManager.Instance.RotateObject(Model, ChangeType.Absolute, Model.Orientation);
_f.PrintModelPosition(Model);
Instance.EndAppearanceUpdate();
// set Model scale ->
_f.PrintOriginalScale(Model.Scale.X.ToString("N2"));
ScaInitial = Model.Scale; // NOTE: Scale comes from the creature blueprint/instance/template/whatver.
// That is, there's no default parameter for scale in this Scene like
// there is for position and rotation.
Vector3 scale;
if (different) scale = ScaInitial;
else scale = _sca;
Model.Scale = scale; // NOTE: after EndAppearanceUpdate().
NWN2NetDisplayManager.Instance.SetObjectScale(Model, Model.Scale);
ResetModel(ResetType.RESET_scale); // this is needed to reset placed-instance scale
_f.PrintModelScale();
}
}
// var state = Receiver.CameraState as ModelViewerInputCameraReceiverState;
// state.FocusTheta = (float)Math.PI / 2F;
// state.FocusPhi = (float)Math.PI / 32F;
// state.Distance = 4.5F;
// Receiver.FocusPoint = Object.Position + OFF_Zd;
// Receiver.PitchMin = -(float)Math.PI / 2f;// + 0.145F;
// Receiver.PitchMax = (float)Math.PI / 2f - 0.010F;
// float yaw = 0F, pitch = 0F, roll = 0F;
// CameraOrientation.GetYawPitchRoll(out yaw, out pitch, out roll);
// MessageBox.Show("yaw= " + yaw + " pitch= " + pitch + " roll= " + roll);
/// <summary>
/// Initializes the scene. Clears any objects, sets up default lighting,
/// and adds a lightpoint.
/// </summary>
bool Initialize()
{
//MessageBox.Show("Initialize()");
if (_f.WindowState != FormWindowState.Minimized)
{
// NOTE: CloseWindow() wipes out the Scene along with its Objects.
// But does it clear 'NWN2NetDisplayManager.Instance.Objects' correctly.
CloseWindow(); // safety - try not to confuse the NWN2NetDisplayManager.Instance ... too late.
if (NDWindow == null)
OpenWindow();
if (NDWindow != null && Scene != null)
{
if (Scene.DayNightCycleStages[(int)DayNightStageType.Default] != null)
{
Scene.DayNightCycleStages[(int)DayNightStageType.Default].SunMoonDirection = DEF_SUNMOON_DIRECTION;
Scene.DayNightCycleStages[(int)DayNightStageType.Default].ShadowIntensity = DEF_SHADOW_INTENSITY;
}
/* case DayNightStageType.Default: // OEIShared.NetDisplay.DayNightState ->
SunMoonDirection = new Vector3(-0.05f, 0.08f, -0.1f);
SunMoon.DiffuseColor = Color.FromArgb(194, 139, 87);
SunMoon.SpecularColor = Color.FromArgb(173, 188, 163);
SunMoon.AmbientColor = Color.FromArgb(255, 255, 255);
SunMoon.Intensity = 1f;
SkyLight.DiffuseColor = Color.FromArgb(215, 229, 250);
SkyLight.SpecularColor = Color.FromArgb(194, 139, 87);
SkyLight.AmbientColor = Color.FromArgb(255, 255, 255);
SkyLight.Intensity = 0.2f;
GroundLight.DiffuseColor = Color.FromArgb(221, 194, 161);
GroundLight.SpecularColor = Color.FromArgb(221, 194, 161);
GroundLight.AmbientColor = Color.FromArgb(255, 255, 255);
GroundLight.Intensity = 0.45f;
SkyHorizon = Color.FromArgb(163, 189, 255);
SkyZenith = Color.FromArgb(173, 194, 255);
Fog.FogColor = Color.FromArgb(134, 153, 211);
Fog.FarClip = 200f;
Fog.FogStart = 60f;
Fog.FogEnd = 170f;
AvgLuminance = 0.65f;
BloomBlurRadius = 6.4f;
BloomGlowIntensity = 0f;
BloomHighlightIntensity = 0.54f;
BloomHighlightThreshold = 0.86f;
BloomSceneIntensity = 1f;
CloudCover = 0.8f;
CloudMovementRateX = 0.04f;
CloudMovementRateY = 0f;
Exposure = 5f;
HighlightThreshold = 5f;
MaxLuminance = 3.65f;
ShadowIntensity = 0.42f;
SunCoronaIntensity = 0.33f;
break;
*/
CreateLight();
_f.PrintDiffuseColor();
_f.PrintSpecularColor();
_f.PrintAmbientColor();
// SetDoubleBuffered(NDWindow);
// SetDoubleBuffered(NWN2NetDisplayManager.Instance.Windows);
// var a = new NetDisplayWindow[NWN2NetDisplayManager.Instance.Windows.Count];
// for (int i = 0; i != NWN2NetDisplayManager.Instance.Windows.Count; ++i)
// a[i] = NWN2NetDisplayManager.Instance.Windows[i];
// SetDoubleBuffered(a);
return true;
}
}
return false;
}
void CreateLight()
{
//MessageBox.Show("CreateLight()");
Light = new NetDisplayLightPoint();
Light.Position = _posLight;
Light.Color.Intensity = CreatureVisualizerPreferences.that.LightIntensity;
Light.Range = LIGHT_START_RANGE;
Light.CastsShadow = false;
Light.ID = NetDisplayManager.Instance.NextObjectID;
Light.Tag = Light;
if (ColorCheckedDiffuse) Light.Color.DiffuseColor = (Color)ColorDiffuse;
if (ColorCheckedSpecular) Light.Color.SpecularColor = (Color)ColorSpecular;
if (ColorCheckedAmbient) Light.Color.AmbientColor = (Color)ColorAmbient;
lock (Scene.Objects.SyncRoot)
{
Scene.Objects.Add(Light);
}
lock (NWN2NetDisplayManager.Instance.Objects.SyncRoot)
{
NWN2NetDisplayManager.Instance.Objects.Add(Light);
}
NWN2NetDisplayManager.Instance.LightParameters(Scene, Light);
_f.PrintLightPosition(Light.Position);
_f.PrintLightIntensity(Light.Color.Intensity);
_objects.Add(Light);
_objectsLight = new NetDisplayObjectCollection() { Light };
}
// int iter;
/// <summary>
/// Moves the light-object.
/// </summary>
/// <param name="posabs"></param>
internal void MoveLight(Vector3 posabs)
{
// if (++iter == 5)
// {
// var t = new System.Diagnostics.StackTrace();
// System.IO.File.WriteAllText(@"C:\GIT\CreatureVisualizer\t\stacktrace.txt", t.ToString());
// System.Diagnostics.Process.GetCurrentProcess().Kill();
// }
//MessageBox.Show("MoveLight()");
NWN2NetDisplayManager.Instance.MoveObjects(_objectsLight, ChangeType.Absolute, false, posabs);
_f.PrintLightPosition(Light.Position);
}
/* void ClearLight()
{
var objects = new NetDisplayObjectCollection();
foreach (NetDisplayObject @object in Scene.Objects)
{
// if (@object.Tag == @object)
// if ((@object as NetDisplayLight).GetDisplayType() == NetDisplayType.NETDISPLAY_TYPE_LIGHT_POINT)
if ((@object as NetDisplayLight) != null)
objects.Add(@object);
}
NWN2NetDisplayManager.Instance.RemoveObjects(objects);
} */
// NWN2NetDisplayManager.NWN2CreatureTemplate.AppearanceChanged;
// internal void UpdateScene()
// {
// NWN2NetDisplayManager.Instance.UpdateAppearanceForInstance(_instance);
// }
#endregion Methods
#region Methods (model)
static Vector3 scalar = new Vector3(0.1F, 0.1F, 0.1F);
internal void MoveModel(Vector3 posrel)
{
NWN2NetDisplayManager.Instance.MoveObjects(_objectsModel, ChangeType.Relative, false, posrel);
_f.PrintModelPosition(Model);
}
internal void RotateModel(float f)
{
RHQuaternion rotate = RHQuaternion.RotationZ(f);
NWN2NetDisplayManager.Instance.RotateObject(Model, ChangeType.Relative, rotate);
Model.Orientation = RHQuaternion.Multiply(Model.Orientation, rotate);
_f.PrintModelPosition(Model);
}
internal void ScaleModel(Vector3 vec)
{
NWN2NetDisplayManager.Instance.SetObjectScale(Model, (Model.Scale += vec));
_f.PrintModelScale();
}
internal void ScaleModel(int dir)
{
var vec = _f.grader(scalar);
switch (dir)
{
case +1: Model.Scale += vec; break;
case -1: Model.Scale -= vec; break;
}
NWN2NetDisplayManager.Instance.SetObjectScale(Model, Model.Scale);
_f.PrintModelScale();
}
internal void ResetModel()
{
NWN2NetDisplayManager.Instance.MoveObjects(_objectsModel, ChangeType.Absolute, false, new Vector3());
Model.Orientation = RHQuaternion.RotationZ(MODEL_START_ROT);
NWN2NetDisplayManager.Instance.RotateObject(Model, ChangeType.Absolute, Model.Orientation);
_f.PrintModelPosition(Model);
Model.Scale = ScaInitial;
NWN2NetDisplayManager.Instance.SetObjectScale(Model, Model.Scale);
_f.PrintModelScale();
}