forked from Spore-Community/Spore-ModAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimEditorMode.cpp
More file actions
405 lines (329 loc) · 10.3 KB
/
AnimEditorMode.cpp
File metadata and controls
405 lines (329 loc) · 10.3 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
#include "stdafx.h"
#include "AnimEditorMode.h"
#include "Debugging.h"
#include <Spore\Anim\IAnimManager.h>
#include <Spore\Anim\SPAnimation.h>
AnimEditorMode::AnimEditorMode()
: mInput()
, mpUI()
, mpModelWorld()
, mpLightingWorld()
, mpEffectWorld()
, mpAnimWorld()
, mCreatures()
, mCreatureKeys()
, mNames()
, mCurrentAnimID(0xFFFFFFFF)
, mCurrentAnimMode()
, mAnimIndices()
, mClock(Clock::Mode::Seconds)
, mLastCheckTime()
, mPath()
, mpBlocks()
{
Initialize(nullptr);
}
AnimEditorMode::~AnimEditorMode()
{
}
// For internal use, do not touch this.
int AnimEditorMode::AddRef()
{
return DefaultRefCounted::AddRef();
}
// For internal use, do not touch this.
int AnimEditorMode::Release()
{
return DefaultRefCounted::Release();
}
// The use of this function is unknown.
bool AnimEditorMode::func0Ch()
{
return false;
}
AnimEditorMode* sInstance;
bool AnimEditorMode::Initialize(App::IGameModeManager* pManager)
{
sInstance = this;
class Cheat
: public ArgScript::ICommand
{
public:
void ParseLine(const ArgScript::Line& line) override
{
if (GameModeManager.GetActiveModeID() != MODE_ID) {
GameModeManager.SetActiveMode(MODE_ID);
}
else {
if (auto args = line.GetOption("help", 1))
{
App::ConsolePrintF("Params: -add, -remove, -play [hash], -default, -help, -exit");
}
if (auto args = line.GetOption("remove", 1))
{
int index = mpFormatParser->ParseInt(args[0]);
sInstance->RemoveCreature(index);
}
if (line.HasFlag("add"))
{
Sporepedia::ShopperRequest request(sInstance);
request.shopperID = id("CreatureShopper");
Sporepedia::ShopperRequest::Show(request);
}
else if (line.HasFlag("exit") || line.HasFlag("quit"))
{
GameModeManager.SetActiveMode(kGGEMode);
}
size_t numArgs;
if (auto args = line.GetOptionRange("play", &numArgs, 1, 2))
{
uint32_t animID = mpFormatParser->ParseUInt(args[0]);
int mode = numArgs == 1 ? 0 : mpFormatParser->ParseInt(args[1]);
sInstance->PlayAnimation(animID, mode);
}
else if (auto args = line.GetOptionRange("default", &numArgs, 0, 1))
{
int mode = numArgs == 0 ? 0 : mpFormatParser->ParseInt(args[0]);
sInstance->PlayAnimation(ANIM_ID, mode);
}
}
}
const char* GetDescription(ArgScript::DescriptionMode mode) const override
{
return "Enters into anim editor. Use the flags -add and -remove to add or change creatures, and -exit to leave the mode.";
}
};
CheatManager.AddCheat("animEditor", new Cheat());
return true;
}
bool AnimEditorMode::Dispose()
{
CheatManager.RemoveCheat("animEditor");
return true;
}
bool AnimEditorMode::OnEnter()
{
mpModelWorld = ModelManager.CreateWorld(MODE_ID);
mpLightingWorld = LightingManager.CreateLightingWorld(MODE_ID);
mpEffectWorld = EffectsManager.CreateWorld(MODE_ID);
EffectsManager.SetDefaultWorld(mpEffectWorld.get());
mpAnimWorld = AnimManager.CreateWorld(u"AnimEditorMode");
mpAnimWorld->SetEffectWorld(mpEffectWorld.get());
mpAnimWorld->SetModelWorld(mpModelWorld.get());
mpLightingWorld->SetLightingState(id("CreatureEditor"));
mpModelWorld->SetLightingWorld(mpLightingWorld.get(), 0, false);
mpModelWorld->SetActive(true);
Renderer.RegisterLayer(mpModelWorld->AsLayer(), 8);
Renderer.RegisterLayer(mpAnimWorld.get(), 9);
CameraManager.SetActiveCameraByID(id("EffectEditorCamera"));
// Add one creature by default
AddCreature({ 0x67cd060, TypeIDs::crt, GroupIDs::CreatureModels });
// Disabled blocks because the load transform crashes the game for unexplainable reasons
/*mpBlocks = new AnimEditorBlocks();
mpBlocks->GenerateMesh(mCreatures[0]->p_cid);*/
DebugInformation* pDebugInformation = nullptr;
Resource::Database* pDBPF = nullptr;
ResourceKey name = { id("_SporeModder_AnimEditor"), TypeIDs::animation, GroupIDs::Global };
pDBPF = ResourceManager.FindDatabase(name);
if (!Debugging::Get()->GetDebugInformation(pDBPF, &pDebugInformation)
|| !pDebugInformation->GetFilePath(name, &mPath))
{
mPath = u"";
}
UpdateFileTime();
App::ConsolePrintF("%ls", mPath.c_str());
//Renderer.RegisterLayer(this, 10);
/*mpUI = new AnimEditorUI();
mpUI->Load();*/
PlayAnimation(ANIM_ID, 0);
return true;
}
void AnimEditorMode::OnExit()
{
mCreatures.clear();
if (mpAnimWorld) {
mpAnimWorld->Dispose();
mpAnimWorld = nullptr;
}
if (mpEffectWorld) {
EffectsManager.RemoveWorld(MODE_ID);
mpEffectWorld = nullptr;
}
if (mpModelWorld) {
ModelManager.DisposeWorld(MODE_ID);
mpModelWorld = nullptr;
}
if (mpLightingWorld) {
LightingManager.RemoveLightingWorld(MODE_ID);
mpLightingWorld = nullptr;
}
}
// The use of this function is unknown.
void* AnimEditorMode::func20h(int)
{
return nullptr;
}
//// INPUT LISTENERS ////
// Called when a keyboard key button is pressed.
bool AnimEditorMode::OnKeyDown(int virtualKey, KeyModifiers modifiers)
{
mInput.OnKeyDown(virtualKey, modifiers);
// Return true if the keyboard event has been handled in this method.
return false;
}
// Called when a keyboard key button is released.
bool AnimEditorMode::OnKeyUp(int virtualKey, KeyModifiers modifiers)
{
mInput.OnKeyUp(virtualKey, modifiers);
// Return true if the keyboard event has been handled in this method.
return false;
}
// Called when a mouse button is pressed (this includes the mouse wheel button).
bool AnimEditorMode::OnMouseDown(MouseButton mouseButton, float mouseX, float mouseY, MouseState mouseState)
{
mInput.OnMouseDown(mouseButton, mouseX, mouseY, mouseState);
// Return true if the mouse event has been handled in this method.
return false;
}
// Called when a mouse button is released (this includes the mouse wheel button).
bool AnimEditorMode::OnMouseUp(MouseButton mouseButton, float mouseX, float mouseY, MouseState mouseState)
{
mInput.OnMouseUp(mouseButton, mouseX, mouseY, mouseState);
// Return true if the mouse event has been handled in this method.
return false;
}
// Called when the mouse is moved.
bool AnimEditorMode::OnMouseMove(float mouseX, float mouseY, MouseState mouseState)
{
mInput.OnMouseMove(mouseX, mouseY, mouseState);
// Return true if the mouse event has been handled in this method.
return false;
}
// Called when the mouse wheel is scrolled.
// This method is not called when the mouse wheel is pressed.
bool AnimEditorMode::OnMouseWheel(int wheelDelta, float mouseX, float mouseY, MouseState mouseState)
{
mInput.OnMouseWheel(wheelDelta, mouseX, mouseY, mouseState);
// Return true if the mouse event has been handled in this method.
return false;
}
//// UPDATE FUNCTION ////
bool hasUpdated = false;
float updatedTime = 0.0f;
void AnimEditorMode::Update(float dt, float delta2)
{
if (mpAnimWorld && !hasUpdated) {
mpAnimWorld->Update(dt);
updatedTime += dt;
SporeDebugPrint("Update time: %f", updatedTime);
//hasUpdated = true;
}
//// Update every second
//if (mCurrentAnimID == ANIM_ID && mClock.GetElapsed() >= 1.0f)
//{
// ULARGE_INTEGER oldTime = mLastCheckTime;
// UpdateFileTime();
// if (mLastCheckTime.QuadPart > oldTime.QuadPart) {
// PlayAnimation(ANIM_ID, mCurrentAnimMode);
// }
//}
}
void AnimEditorMode::DrawLayer(int flags, int layerIndex, App::cViewer** pViewers, Graphics::RenderStatistics&)
{
/*if (mpBlocks) {
CameraManager.GetViewer()->LoadTransformations();
mpBlocks->Render();
}*/
}
void AnimEditorMode::OnShopperAccept(const ResourceKey& selection)
{
AddCreature(selection);
PlayAnimation(ANIM_ID, 0);
}
const static Vector3 kCreatureSeparation = { 2.5f, 0.0f, 0.0f };
void AnimEditorMode::AddCreature(const ResourceKey& selection)
{
App::ConsolePrintF("Adding creature 0x%x ! 0x%x", selection.groupID, selection.instanceID);
if (auto creature = mpAnimWorld->LoadCreature(selection))
{
Vector3 position = { 0.0f, 0.0f, 0.0f };
if (!mCreatures.empty()) {
position = mCreatures[mCreatures.size() - 1]->mPosition + kCreatureSeparation;
}
creature->mPosition = position;
creature->mOrientation = Quaternion();
mCreatures.push_back(creature);
mCreatureKeys.push_back(selection);
mAnimIndices.push_back(-1);
mpModelWorld->StallUntilLoaded(creature->GetModel());
mpModelWorld->SetInWorld(creature->GetModel(), true);
int index = mCreatures.size() - 1;
string16 name = u"";
cAssetMetadataPtr metadata;
if (Pollinator::GetMetadata(selection.instanceID, selection.groupID, metadata)) {
name = metadata->GetName();
}
mNames.push_back(name);
App::ConsolePrintF("Added creature %ls to index %d", name.c_str(), index);
}
else {
App::ConsolePrintF("Error: creature doesn't exist");
}
}
void AnimEditorMode::RemoveCreature(int index)
{
if (index >= (int)mCreatures.size() || index < 0) {
App::ConsolePrintF("Cannot remove creature %d, invalid index", index);
}
else {
mpAnimWorld->DestroyCreature(mCreatures[index].get());
mCreatures.erase(mCreatures.begin() + index);
mAnimIndices.erase(mAnimIndices.begin() + index);
mCreatureKeys.erase(mCreatureKeys.begin() + index);
App::ConsolePrintF("Removed creature %s", mNames[index].c_str());
mNames.erase(mNames.begin() + index);
// Move existing creatures
for (unsigned int i = index; i < mCreatures.size(); ++i) {
mCreatures[i]->mPosition -= kCreatureSeparation;
}
}
}
void AnimEditorMode::PlayAnimation(uint32_t animID, int mode)
{
AnimManager.ClearAnimationCache();
AnimManager.LoadRequiredAnimations(animID);
mCurrentAnimID = animID;
mCurrentAnimMode = mode;
for (unsigned int i = 0; i < mCreatures.size(); ++i)
{
// Stop previous animation
if (mAnimIndices[i] != -1)
{
mCreatures[i]->SetLoop(mAnimIndices[i], false);
mCreatures[i]->SetLoopTimes(mAnimIndices[i], 0.0f);
}
auto animIndex = mCreatures[i]->LoadAnimation(animID);
mCreatures[i]->SetAnimationMode(animIndex, mode);
mCreatures[i]->StartAnimation(animIndex);
mCreatures[i]->SetLoop(animIndex, true);
mCreatures[i]->SetLoopTimes(animIndex, -1.0f);
mAnimIndices[i] = animIndex;
}
// Stop and start the clock again
mClock.Reset();
mClock.Start();
}
void AnimEditorMode::UpdateFileTime()
{
if (!mPath.empty()) {
HANDLE handle = CreateFile((wchar_t*)mPath.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
FILETIME lastWriteTime;
if (GetFileTime(handle, NULL, NULL, &lastWriteTime))
{
mLastCheckTime.LowPart = lastWriteTime.dwLowDateTime;
mLastCheckTime.HighPart = lastWriteTime.dwHighDateTime;
}
CloseHandle(handle);
}
}