-
Notifications
You must be signed in to change notification settings - Fork 5
/
customdata_customgui.cpp
482 lines (362 loc) · 10.9 KB
/
customdata_customgui.cpp
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
#include "main.h"
#include "c4d.h"
#include "c4d_symbols.h"
//----------------------------------------------------------------------------------------
/// This example shows how to use custom datatypes in combination with custom GUIs.
///
/// iCustomDataTypeDots is the actual data object that stores the data, in this case an array of Vectors.
/// DotsCustomDataTypeClass is the plugin class that represents a new data type within Cinema 4D and handles iCustomDataTypeDots data.
/// SDKExampleCustomGUIDots is the plugin class that represents a new GUI element. It creates an iExampleCustomGUIDots dialog.
/// iExampleCustomGUIDots is a dialog that is the actual custom GUI. It includes a DotsUserArea GeUserArea.
/// DotsUserArea is a GeUserArea that displays the custom datatype and reacts to user interaction.
///
//----------------------------------------------------------------------------------------
#define ID_SDK_EXAMPLE_CUSTOMGUI_DOTS 1035302 ///> The ID of the custom GUI
#define ID_SDK_EXAMPLE_CUSTOMDATATYPE_DOTS 1035303 ///> The ID of the custom datatype
class DotsCustomDataTypeClass; // forward declaration
//---------------------------
/// the actual data
//---------------------------
class iCustomDataTypeDots : public iCustomDataType<iCustomDataTypeDots>
{
friend class DotsCustomDataTypeClass;
public:
// an array of Vectors
maxon::BaseArray<Vector> _points;
iCustomDataTypeDots()
{
}
};
//---------------------------
/// The user area used to display the custom datatype
//---------------------------
class DotsUserArea : public GeUserArea
{
public:
DotsUserArea();
virtual ~DotsUserArea();
virtual Bool Init();
virtual Bool InitValues();
virtual Bool GetMinSize(Int32& w, Int32& h);
virtual void DrawMsg(Int32 x1, Int32 y1, Int32 x2, Int32 y2, const BaseContainer& msg);
virtual Bool InputEvent(const BaseContainer& msg);
iCustomDataTypeDots* _data;
};
DotsUserArea::DotsUserArea()
{
_data = nullptr;
}
DotsUserArea::~DotsUserArea()
{
}
Bool DotsUserArea::Init()
{
return true;
}
Bool DotsUserArea::InitValues()
{
return true;
}
Bool DotsUserArea::GetMinSize(Int32& w, Int32& h)
{
w = 400;
h = 300;
return true;
}
void DotsUserArea::DrawMsg(Int32 x1, Int32 y1, Int32 x2, Int32 y2, const BaseContainer& msg)
{
this->OffScreenOn();
// drawing the white background
this->DrawSetPen(Vector(1.0));
this->DrawRectangle(0, 0, 400, 300);
// if the data is defined for each dot a rectangle is drawn
if (_data)
{
this->DrawSetPen(Vector(0.0));
for (Int32 i = 0; i < _data->_points.GetCount(); ++i)
{
const Vector vec = _data->_points[i];
this->DrawRectangle(SAFEINT32(vec.x)-5, SAFEINT32(vec.y)-5, SAFEINT32(vec.x) + 5, SAFEINT32(vec.y) + 5);
}
}
}
Bool DotsUserArea::InputEvent(const BaseContainer& msg)
{
// check the input device
switch (msg.GetInt32(BFM_INPUT_DEVICE))
{
// some mouse interaction
case BFM_INPUT_MOUSE:
{
// get the cursor position
Int32 mx = msg.GetInt32(BFM_INPUT_X);
Int32 my = msg.GetInt32(BFM_INPUT_Y);
Global2Local(&mx, &my);
// add the new position to the data
iferr (_data->_points.Append(Vector(mx, my, 0)))
return false;
// inform the parent that the data has changed
BaseContainer m(BFM_ACTION);
m.SetInt32(BFM_ACTION_ID, GetId());
SendParentMessage(m);
return true;
}
}
return false;
}
//------------
/// the iCustomGui dialog hosting the user area
//------------
#define ID_USERAREA 2000 ///< The ID of the user area GUI element.
class iExampleCustomGUIDots : public iCustomGui
{
INSTANCEOF(iExampleCustomGUIDots, iCustomGui)
private:
Bool _tristate;
iCustomDataTypeDots _data;
DotsUserArea _dotsUserArea;
public:
iExampleCustomGUIDots(const BaseContainer &settings, CUSTOMGUIPLUGIN *plugin);
virtual Bool CreateLayout();
virtual Bool InitValues();
virtual Bool Command(Int32 id, const BaseContainer &msg);
virtual Bool SetData(const TriState<GeData> &tristate);
virtual TriState<GeData> GetData();
};
iExampleCustomGUIDots::iExampleCustomGUIDots(const BaseContainer &settings, CUSTOMGUIPLUGIN *plugin) : iCustomGui(settings, plugin)
{
_tristate = false;
};
Bool iExampleCustomGUIDots::CreateLayout()
{
BaseContainer bc;
GroupBegin(1000, BFH_SCALEFIT|BFV_FIT, 2, 1, String(), 0);
{
GroupSpace(0, 0);
// creating the GeUserArea
C4DGadget* userarea = this->AddUserArea(ID_USERAREA, BFH_LEFT, 400, 300);
this->AttachUserArea(_dotsUserArea, userarea);
// assigning the data
_dotsUserArea._data = &_data;
}
GroupEnd();
return SUPER::CreateLayout();
};
Bool iExampleCustomGUIDots::InitValues()
{
return SUPER::InitValues();
};
Bool iExampleCustomGUIDots::Command(Int32 id, const BaseContainer &msg)
{
if (id == ID_USERAREA)
{
// a message from the user area was received
// inform the parent that the data has changed
BaseContainer m(msg);
m.SetInt32(BFM_ACTION_ID, GetId());
m.SetData(BFM_ACTION_VALUE, this->GetData().GetValue());
SendParentMessage(m);
// redrawing the user area
_dotsUserArea.Redraw();
return true;
}
return SUPER::Command(id, msg);
}
Bool iExampleCustomGUIDots::SetData(const TriState<GeData> &tristate)
{
// data is set from the outside
const iCustomDataTypeDots* const data = static_cast<const iCustomDataTypeDots*>(tristate.GetValue().GetCustomDataType(ID_SDK_EXAMPLE_CUSTOMDATATYPE_DOTS));
if (data)
{
_data._points.Flush();
iferr (_data._points.CopyFrom(data->_points))
return false;
_dotsUserArea.Redraw();
}
return true;
};
TriState<GeData> iExampleCustomGUIDots::GetData()
{
TriState<GeData> tri;
tri.Add(GeData(ID_SDK_EXAMPLE_CUSTOMDATATYPE_DOTS, _data));
return tri;
};
//---------------------
/// custom GUI data
//---------------------
static Int32 g_stringtable[] = { ID_SDK_EXAMPLE_CUSTOMDATATYPE_DOTS };
class SDKExampleCustomGUIDots : public CustomGuiData
{
public:
virtual Int32 GetId();
virtual CDialog* Alloc(const BaseContainer& settings);
virtual void Free(CDialog* dlg, void* userdata);
virtual const Char* GetResourceSym();
virtual CustomProperty* GetProperties();
virtual Int32 GetResourceDataType(Int32*& table);
};
Int32 SDKExampleCustomGUIDots::GetId()
{
return ID_SDK_EXAMPLE_CUSTOMGUI_DOTS;
};
CDialog* SDKExampleCustomGUIDots::Alloc(const BaseContainer& settings)
{
// creating the dialog that "is" the actual GUI
iferr (iExampleCustomGUIDots* const dlg = NewObj(iExampleCustomGUIDots, settings, GetPlugin()))
return nullptr;
CDialog *cdlg = dlg->Get();
if (!cdlg)
return nullptr;
return cdlg;
};
void SDKExampleCustomGUIDots::Free(CDialog* dlg, void* userdata)
{
if (!dlg || !userdata)
return;
iExampleCustomGUIDots* sub = static_cast<iExampleCustomGUIDots*>(userdata);
DeleteObj(sub);
};
const Char* SDKExampleCustomGUIDots::GetResourceSym()
{
// the symbol used in resource files
return "CUSTOMGUIDOTS";
};
CustomProperty* SDKExampleCustomGUIDots::GetProperties()
{
return nullptr;
};
Int32 SDKExampleCustomGUIDots::GetResourceDataType(Int32*& table)
{
// returns the list of datatypes this GUI can work with
table = g_stringtable;
return sizeof(g_stringtable)/sizeof(Int32);
};
//---------------------------
/// The datatype class
//---------------------------
class DotsCustomDataTypeClass: public CustomDataTypeClass
{
INSTANCEOF(DotsCustomDataTypeClass, CustomDataTypeClass)
public:
virtual Int32 GetId()
{
return ID_SDK_EXAMPLE_CUSTOMDATATYPE_DOTS;
}
virtual CustomDataType* AllocData()
{
iCustomDataTypeDots* data = NewObjClear(iCustomDataTypeDots);
return data;
};
virtual void FreeData(CustomDataType* data)
{
iCustomDataTypeDots* d = static_cast<iCustomDataTypeDots*>(data);
DeleteObj(d);
}
virtual Bool CopyData(const CustomDataType* src, CustomDataType* dst, AliasTrans* aliastrans)
{
// copy the data to the given target data
const iCustomDataTypeDots* s = static_cast<const iCustomDataTypeDots*>(src);
iCustomDataTypeDots* d = static_cast<iCustomDataTypeDots*>(dst);
if (!s || !d)
return false;
d->_points.Flush();
iferr (d->_points.CopyFrom(s->_points))
return false;
return true;
}
virtual Int32 Compare(const CustomDataType* d1, const CustomDataType* d2)
{
// compare the values of the given elements
// this is also used to determine if a parameter has changed
const iCustomDataTypeDots* const s = static_cast<const iCustomDataTypeDots*>(d1);
const iCustomDataTypeDots* const d = static_cast<const iCustomDataTypeDots*>(d2);
if (!s || !d)
return 0;
// this just compares the number of points
// a better implementation would also compare the values of the points
const maxon::Int countd1 = s->_points.GetCount();
const maxon::Int countd2 = d->_points.GetCount();
if (countd1 == countd2)
return 0;
if (countd1 < countd2)
return -1;
if (countd1 > countd2)
return 1;
return 0;
}
virtual Bool WriteData(const CustomDataType* t_d, HyperFile* hf)
{
// write the data of this datatype to the given hyperfile
const iCustomDataTypeDots* const d = static_cast<const iCustomDataTypeDots*>(t_d);
// save number of points
const maxon::Int length = d->_points.GetCount();
hf->WriteInt64((Int64)length);
// save points
for (Int64 i = 0; i < length; ++i)
{
hf->WriteVector(d->_points[i]);
}
return true;
}
virtual Bool ReadData(CustomDataType* t_d, HyperFile* hf, Int32 level)
{
// reads the data of this datatype from the given hyperfile
iCustomDataTypeDots* const d = static_cast<iCustomDataTypeDots*>(t_d);
if (level > 0)
{
// get number of points
Int64 length = 0;
if (hf->ReadInt64(&length))
{
// read points
for (Int64 i = 0; i < length; ++i)
{
Vector vec;
if (hf->ReadVector(&vec))
{
iferr (d->_points.Append(vec))
return false;
}
}
}
}
return true;
}
virtual const Char* GetResourceSym()
{
// this symbol can be used in resource files
return "DOTS";
}
virtual void GetDefaultProperties(BaseContainer &data)
{
// the default values of this datatype
// use the custom GUI as default
data.SetInt32(DESC_CUSTOMGUI, ID_SDK_EXAMPLE_CUSTOMGUI_DOTS);
data.SetInt32(DESC_ANIMATE, DESC_ANIMATE_ON);
}
};
//---------------------------
/// Register
//---------------------------
Bool RegisterCustomDatatypeCustomGUI()
{
// register custom datatype
if (!RegisterCustomDataTypePlugin(
GeLoadString(IDS_CUSTOMDATATYPE_DOTS),
CUSTOMDATATYPE_INFO_LOADSAVE |
CUSTOMDATATYPE_INFO_TOGGLEDISPLAY |
CUSTOMDATATYPE_INFO_HASSUBDESCRIPTION,
NewObjClear(DotsCustomDataTypeClass),
1))
return false;
// dummy library for custom GUI
static BaseCustomGuiLib myDotCustomGUI;
ClearMem(&myDotCustomGUI, sizeof(myDotCustomGUI));
FillBaseCustomGui(myDotCustomGUI);
if (!InstallLibrary(ID_SDK_EXAMPLE_CUSTOMGUI_DOTS, &myDotCustomGUI, 1000, sizeof(myDotCustomGUI)))
return false;
// register custom GUI
if (!RegisterCustomGuiPlugin(GeLoadString(IDS_CUSTOMGUI_DOTS), 0, NewObjClear(SDKExampleCustomGUIDots)))
return false;
return true;
}