-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapColorChangerPlugin.cpp
More file actions
338 lines (279 loc) · 8.51 KB
/
Copy pathMapColorChangerPlugin.cpp
File metadata and controls
338 lines (279 loc) · 8.51 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
//========================================================================================
//
// $File: //ai/ai18/devtech/sdk/public/samplecode/MapColorChanger/Source/MapColorChangerPlugin.cpp $
//
// $Revision: #1 $
//
// Copyright 1987 Adobe Systems Incorporated. All rights reserved.
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
// with the terms of the Adobe license agreement accompanying it. If you have received
// this file from a source other than Adobe, then your use, modification, or
// distribution of it requires the prior written permission of Adobe.
//
//========================================================================================
#include "IllustratorSDK.h"
#include "MapColorChangerPlugin.h"
#include "AICSXS.h"
#define CHECK_RESULT if(result) { return result;}
/*
*/
Plugin* AllocatePlugin(SPPluginRef pluginRef)
{
return new MapColorChangerPlugin(pluginRef);
}
/*
*/
void FixupReload(Plugin* plugin)
{
MapColorChangerPlugin::FixupVTable((MapColorChangerPlugin*) plugin);
}
/*
*/
MapColorChangerPlugin::MapColorChangerPlugin(SPPluginRef pluginRef)
: Plugin(pluginRef),
// fRunToolMenu(NULL),
fAppShutdownNotifier(NULL),
fCSXSPlugPlugSetupCompleteNotifier(NULL),
fNumFilters(0),
fMapColorChanger(NULL)
{
strncpy(fPluginName, kMapColorChangerPluginName, kMaxStringLength);
}
/*
*/
ASErr MapColorChangerPlugin::Message(char* caller, char* selector, void *message)
{
ASErr error = kNoErr;
try {
error = Plugin::Message(caller, selector, message);
}
catch (ai::Error& ex) {
error = ex;
}
catch (...) {
error = kCantHappenErr;
}
if (error) {
if (error == kUnhandledMsgErr) {
// Defined by Plugin.hpp and used in Plugin::Message - ignore.
error = kNoErr;
}
else {
Plugin::ReportError(error, caller, selector, message);
}
}
return error;
}
/*
*/
ASErr MapColorChangerPlugin::StartupPlugin(SPInterfaceMessage* message)
{
ASErr result = kNoErr;
result = Plugin::StartupPlugin(message);
CHECK_RESULT
result = this->AddMenus(message);
CHECK_RESULT
//result = this->AddFilters(message);
CHECK_RESULT
//result = this->AddLiveEffects(message);
CHECK_RESULT
result = this->AddNotifiers(message);
return result;
}
/*
*/
ASErr MapColorChangerPlugin::PostStartupPlugin()
{
ASErr result = kNoErr;
// Create LiveDropShadowGroup
if (this->fMapColorChanger == NULL)
{
this->fMapColorChanger = new MapColorChanger();
// TODO error handling
}
return result;
}
/*
*/
ASErr MapColorChangerPlugin::Notify(AINotifierMessage* message)
{
ASErr result = kNoErr;
if (message->notifier == this->fAppShutdownNotifier)
{
if (this->fMapColorChanger)
{
delete this->fMapColorChanger;
}
Plugin::LockPlugin(false);
}
else if ( message->notifier == fApplyFilterNotifier )
{
GoFilter((AIFilterMessage *)message);
}
else if ( message->notifier == fApplyLiveEffectNotifier )
{
GoLiveEffect((AILiveEffectGoMessage *) message);
}
return result;
}
/*
*/
ASErr MapColorChangerPlugin::AddNotifiers(SPInterfaceMessage* message)
{
ASErr result = kNoErr;
result = sAINotifier->AddNotifier(message->d.self, "MapColorChanger " kAIApplicationShutdownNotifier,
kAIApplicationShutdownNotifier, &this->fAppShutdownNotifier);
result = sAINotifier->AddNotifier(message->d.self, "MapColorChanger " kAICSXSPlugPlugSetupCompleteNotifier,
kAICSXSPlugPlugSetupCompleteNotifier, &fCSXSPlugPlugSetupCompleteNotifier);
result = sAINotifier->AddNotifier(message->d.self, kMapColorChangerApplyCaller, kTwirlApplyFilterNotifier,
&fApplyFilterNotifier);
result = sAINotifier->AddNotifier(message->d.self, kMapColorChangerApplyCaller, kTwirlApplyEffectNotifier,
&fApplyLiveEffectNotifier);
return result;
}
/*
*/
ASErr MapColorChangerPlugin::AddMenus(SPInterfaceMessage* message)
{
ASErr error = kNoErr;
AIPlatformAddMenuItemDataUS menuData;
AIMenuGroup menuGroup;
AIMenuItemHandle menuItemHandle = nil;
// add menu group in edit
menuData.groupName = kEditMenuGroup;
menuData.itemText = ai::UnicodeString( kMenuGroupText );
error = sAIMenu->AddMenuItem( message->d.self,
NULL,
&menuData,
kMenuItemWantsUpdateOption,
&menuItemHandle);
error = sAIMenu->UpdateMenuItemAutomatically(menuItemHandle, kAutoEnableMenuItemAction, 0, 0, kIfMesh, 0, 0, 0);
if (error) { goto error; }
error = sAIMenu->AddMenuGroupAsSubMenu(kMenuGroupText, kMenuGroupNoOptions, menuItemHandle, &menuGroup);
// add recolor menu item
menuData.groupName = kMenuGroupText;
menuData.itemText = ai::UnicodeString( kMenuRecolorItemText );
error = sAIMenu->AddMenuItem( message->d.self,
kMenuRecolorItemText,
&menuData,
kMenuGroupNoOptions,
&this->fRecolorMenuItem);
if (error) { goto error; }
/* Not yet implimented
// add kMenuFlipHItemText menu item
menuData.groupName = kMenuGroupText;
menuData.itemText = ai::UnicodeString( kMenuFlipHItemText );
error = sAIMenu->AddMenuItem( message->d.self,
kMenuFlipHItemText,
&menuData,
kMenuGroupNoOptions,
&this->fFlipHMenuItem);
if (error) { goto error; }
// add first menu item
menuData.groupName = kMenuGroupText;
menuData.itemText = ai::UnicodeString( kMenuFlipVItemText );
error = sAIMenu->AddMenuItem( message->d.self,
kMenuFlipVItemText,
&menuData,
kMenuGroupNoOptions,
&this->fFlipVMenuItem);
if (error) { goto error; }*/
// add first menu item
menuData.groupName = kMenuGroupText;
menuData.itemText = ai::UnicodeString( kMenuCreateRefSpotsItemText );
error = sAIMenu->AddMenuItem( message->d.self,
kMenuCreateRefSpotsItemText,
&menuData,
kMenuGroupNoOptions,
&this->fCreateRefSpotsMenuItem);
if (error) { goto error; }
/*
error = sAIMenu->UpdateMenuItemAutomatically(this->fRunToolMenu, kAutoEnableMenuItemAction, 0, 0, kIfMesh, 0, 0, 0);
if (error) { goto error; }*/
error:
return error;
}
ai::FilePath MapColorChangerPlugin::PromptForCsvFile()
{
ai::FilePath path;
ai::UnicodeString title("Select csv");
ai::UnicodeString desc("CSV map data file");
ai::UnicodeString pattern("*.csv");
AIFileDialogFilters options;
options.AddFilter(desc, pattern);
sAIUser->GetFileDialog(title, &options, path);
return path;
}
ASErr MapColorChangerPlugin::GetMeshesFromSelection(AIArtHandle*** matches, ai::int32* numMatches){
AIMatchingArtSpec spec[1];
ASErr error = kNoErr;
spec[0].type = kMeshArt;
spec[0].whichAttr = kArtSelected;
spec[0].attr = kArtSelected;
error = sAIMatchingArt->GetMatchingArt( spec, 1, matches, numMatches );
return error;
}
ASErr MapColorChangerPlugin::CreateMaps(AIArtHandle mesh){
ASErr error = kNoErr;
ai::FilePath path = this->PromptForCsvFile();
if(path.IsEmpty()){ goto cancel; }
this->fCSVData = new CSVData();
this->fCSVData->ParseCsv(path);
this->fCSVData->BuildMaps();
for (int i = 0; i < this->fCSVData->Maps.size(); i++)
{
this->fCSVData->Maps[i].FlipV();
this->fCSVData->Maps[i].FlipH();
error = sAIArt->DuplicateArt(mesh, kPlaceAbove, mesh, &fCSVData->Maps[i].fMesh);
error = fMapColorChanger->RecolorMap(&fCSVData->Maps[i], &fCSVData->Maps[i].fMesh);
if(error){ // prevent looping error message
error = sAIArt->DisposeArt(fCSVData->Maps[i].fMesh);
break;
}
sAIArt->SetArtName(fCSVData->Maps[i].fMesh, ai::UnicodeString(fCSVData->Maps[i].MapTitle));
}
error = sAIArt->DisposeArt(mesh);
return error;
cancel:
return true;
}
/*
*/
ASErr MapColorChangerPlugin::GoMenuItem(AIMenuMessage* message)
{
AIArtHandle **matches;
ai::int32 numMatches;
this->GetMeshesFromSelection(&matches, &numMatches);
if(message->menuItem == this->fRecolorMenuItem){
if(numMatches == 1){
this->CreateMaps((*matches)[0]);
}else{
char alertMessage [50];
sprintf (alertMessage, "Can only use one mesh as template. %d selected.", numMatches);
sAIUser->ErrorAlert(ai::UnicodeString(alertMessage));
}
}else{
for (int i = 0; i < numMatches; i++)
{
if(message->menuItem == this->fCreateRefSpotsMenuItem){
this->fMapColorChanger->CreateReferenceSpheres((*matches)[i]);
}
}
}
return kNoErr;
}
/////////////////////////////////////////
//
// Utility functions
//
/////////////////////////////////////////
/*
*/
void MapColorChangerPlugin::CStrToPStr(char *s, ai::UnicodeString::size_type len)
{
const ai::UnicodeString sAsUnicode((const char*)s);
ai::PStr sAsPStr((unsigned char*) s);
sAsUnicode.getToBuffer(sAsPStr, len, kAIUTF8CharacterEncoding );
}
// End MapColorChangerPlugin.cpp