forked from extnet/Ext.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.cs
More file actions
512 lines (468 loc) · 16 KB
/
Sprite.cs
File metadata and controls
512 lines (468 loc) · 16 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
/********
* @version : 2.1.1 - Ext.NET Pro License
* @author : Ext.NET, Inc. http://www.ext.net/
* @date : 2012-12-10
* @copyright : Copyright (c) 2007-2012, Ext.NET, Inc. (http://www.ext.net/). All rights reserved.
* @license : See license.txt and http://www.ext.net/license/.
********/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net.Utilities;
namespace Ext.Net
{
/// <summary>
/// A Sprite is an object rendered in a Drawing surface.
///
/// Translation
///
/// For translate, the configuration object contains x and y attributes that indicate where to translate the object. For example:
///
/// sprite.setAttributes({
/// translate: {
/// x: 10,
/// y: 10
/// }
/// }, true);
/// Rotation
///
/// For rotation, the configuration object contains x and y attributes for the center of the rotation (which are optional), and a degrees attribute that specifies the rotation in degrees. For example:
///
/// sprite.setAttributes({
/// rotate: {
/// degrees: 90
/// }
/// }, true);
/// That example will create a 90 degrees rotation using the centroid of the Sprite as center of rotation, whereas:
///
/// sprite.setAttributes({
/// rotate: {
/// x: 0,
/// y: 0,
/// degrees: 90
/// }
/// }, true);
/// will create a rotation around the (0, 0) axis.
///
/// Scaling
///
/// For scaling, the configuration object contains x and y attributes for the x-axis and y-axis scaling. For example:
///
/// sprite.setAttributes({
/// scale: {
/// x: 10,
/// y: 3
/// }
/// }, true);
/// You can also specify the center of scaling by adding cx and cy as properties:
///
/// sprite.setAttributes({
/// scale: {
/// cx: 0,
/// cy: 0,
/// x: 10,
/// y: 3
/// }
/// }, true);
/// That last example will scale a sprite taking as centers of scaling the (0, 0) coordinate.
///
/// Creating and adding a Sprite to a Surface
///
/// Sprites can be created with a reference to a Ext.draw.Surface
///
/// var drawComponent = Ext.create('Ext.draw.Component', options here...);
///
/// var sprite = Ext.create('Ext.draw.Sprite', {
/// type: 'circle',
/// fill: '#ff0',
/// surface: drawComponent.surface,
/// radius: 5
/// });
/// Sprites can also be added to the surface as a configuration object:
///
/// var sprite = drawComponent.surface.add({
/// type: 'circle',
/// fill: '#ff0',
/// radius: 5
/// });
/// In order to properly apply properties and render the sprite we have to show the sprite setting the option redraw to true:
///
/// sprite.show(true);
/// The constructor configuration object of the Sprite can also be used and passed into the Ext.draw.Surface add method to append a new sprite to the canvas. For example:
///
/// drawComponent.surface.add({
/// type: 'circle',
/// fill: '#ffc',
/// radius: 100,
/// x: 100,
/// y: 100
/// });
/// </summary>
[Meta]
public partial class Sprite : SpriteAttributes
{
/// <summary>
///
/// </summary>
public Sprite()
{
}
public AbstractDrawComponent Draw
{
get;
set;
}
public bool IsGroup
{
get;
set;
}
/// <summary>
///
/// </summary>
[Meta]
[ConfigOption("id")]
[DefaultValue("")]
[Description("")]
public virtual string SpriteID
{
get
{
return this.State.Get<string>("SpriteID", "");
}
set
{
this.State.Set("SpriteID", value);
}
}
/// <summary>
/// True to make the sprite draggable.
/// </summary>
[Meta]
[DefaultValue(false)]
[Description("True to make the sprite draggable.")]
public virtual bool Draggable
{
get
{
return this.State.Get<bool>("Draggable", false);
}
set
{
this.State.Set("Draggable", value);
}
}
/// <summary>
///
/// </summary>
[DefaultValue("")]
[ConfigOption("draggable", JsonMode.Raw)]
protected virtual string DraggableConfigProxy
{
get
{
if (this.DraggableConfig == null)
{
return this.Draggable ? "true" : "";
}
string cfg = new ClientConfig().Serialize(this.DraggableConfig, true);
return cfg != Const.EmptyObject ? cfg : "";
}
}
private DragSource draggableConfig;
/// <summary>
/// Drag config object.
/// </summary>
[Meta]
[NotifyParentProperty(true)]
[DefaultValue(null)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[Description("Drag config object.")]
public virtual DragSource DraggableConfig
{
get
{
return this.draggableConfig;
}
set
{
if (value != null)
{
value.EnableViewState = this.DesignMode;
}
this.draggableConfig = value;
}
}
/// <summary>
/// The group that this sprite belongs to, or an array of groups. Only relevant when added to a Ext.draw.Surface
/// </summary>
[Meta]
[ConfigOption(typeof(StringArrayJsonConverter))]
[TypeConverter(typeof(StringArrayConverter))]
[DefaultValue(null)]
[Description("The group that this sprite belongs to, or an array of groups. Only relevant when added to a Ext.draw.Surface")]
public virtual string[] Group
{
get
{
return this.State.Get<string[]>("Group", null);
}
set
{
this.State.Set("Group", value);
}
}
protected virtual void CallTemplate(string name, params object[] args)
{
if (this.SpriteID.IsEmpty())
{
throw new Exception("You have to set sprite's ID to call its methods");
}
if (this.Draw == null)
{
throw new Exception("Sprite has no DrawComponent reference");
}
StringBuilder sb = new StringBuilder();
var comma = false;
if (args != null && args.Length > 0)
{
foreach (object arg in args)
{
if (comma)
{
sb.Append(",");
}
comma = true;
sb.Append(JSON.Serialize(arg, JSON.ScriptConvertersInternal));
}
}
var template = this.IsGroup ? "{0}.surface.getGroup(\"{1}\").{2}({3});" : "{0}.get(\"{1}\").{2}({3});";
string script = template.FormatWith(this.Draw.ClientID, this.SpriteID, name, sb.ToString());
this.Draw.AddScript(script);
}
/// <summary>
/// Adds one or more CSS classes to the element. Duplicate classes are automatically filtered out. Note this method is severly limited in VML.
/// </summary>
/// <param name="className">The CSS class to add, or an array of classes</param>
[Meta]
public virtual void AddCls(string className)
{
this.CallTemplate("addCls", className);
}
/// <summary>
/// Adds one or more CSS classes to the element. Duplicate classes are automatically filtered out. Note this method is severly limited in VML.
/// </summary>
/// <param name="className">The CSS class to add, or an array of classes</param>
[Meta]
public virtual void AddCls(string[] className)
{
this.CallTemplate("addCls", className);
}
/// <summary>
/// Perform custom animation on this object.
/// </summary>
/// <param name="cfg">An Ext.fx Anim object</param>
[Meta]
public virtual void Animate(AnimConfig cfg)
{
this.CallTemplate("animate", new JRawValue(new ClientConfig().Serialize(cfg)));
}
///<summary>
/// Ensures that all effects queued after sequenceFx is called on the element are run in sequence. This is the opposite of syncFx.
///</summary>
[Meta]
public virtual void SequenceFx()
{
this.CallTemplate("sequenceFx");
}
///<summary>
/// Stops any running effects and clears this object's internal effects queue if it contains any additional effects that haven't started yet.
///</summary>
[Meta]
public virtual void StopAnimation()
{
this.CallTemplate("stopAnimation");
}
///<summary>
/// Ensures that all effects queued after syncFx is called on the element are run concurrently. This is the opposite of sequenceFx.
///</summary>
[Meta]
public virtual void SyncFx()
{
this.CallTemplate("syncFx");
}
/// <summary>
/// Removes the sprite and clears all listeners.
/// </summary>
[Meta]
public virtual void Destroy()
{
this.CallTemplate("destroy");
}
/// <summary>
/// Hides the sprite.
/// </summary>
/// <param name="redraw">Flag to immediatly draw the change.</param>
[Meta]
public virtual void Hide(bool redraw)
{
this.CallTemplate("hide", redraw);
}
/// <summary>
/// Hides the sprite.
/// </summary>
[Meta]
public virtual void Hide()
{
this.CallTemplate("hide");
}
/// <summary>
/// Redraws the sprite.
/// </summary>
[Meta]
public virtual void Redraw()
{
this.CallTemplate("redraw");
}
/// <summary>
/// Removes the sprite.
/// </summary>
[Meta]
public virtual void Remove()
{
this.CallTemplate("remove");
}
/// <summary>
/// Removes one or more CSS classes from the element.
/// </summary>
/// <param name="className">The CSS class to remove, or an array of classes. Note this method is severly limited in VML.</param>
[Meta]
public virtual void RemoveCls(string className)
{
this.CallTemplate("removeCls", className);
}
/// <summary>
/// Removes one or more CSS classes from the element.
/// </summary>
/// <param name="className">The CSS class to remove, or an array of classes. Note this method is severly limited in VML.</param>
[Meta]
public virtual void RemoveCls(string[] className)
{
this.CallTemplate("removeCls", className);
}
/// <summary>
/// Change the attributes of the sprite.
/// </summary>
/// <param name="attrs">attributes to be changed on the sprite.</param>
/// <param name="redraw">Flag to immediatly draw the change.</param>
[Meta]
public virtual void SetAttributes(Dictionary<string,object> attrs, bool redraw)
{
this.CallTemplate("setAttributes", attrs, redraw);
}
/// <summary>
/// Change the attributes of the sprite.
/// </summary>
/// <param name="attrs">attributes to be changed on the sprite.</param>
[Meta]
public virtual void SetAttributes(Dictionary<string, object> attrs)
{
this.CallTemplate("setAttributes", attrs);
}
/// <summary>
/// Change the attributes of the sprite.
/// </summary>
/// <param name="attrs">attributes to be changed on the sprite.</param>
/// <param name="redraw">Flag to immediatly draw the change.</param>
[Meta]
public virtual void SetAttributes(SpriteAttributes attrs, bool redraw)
{
this.CallTemplate("setAttributes", JRawValue.From(attrs.Serialize()), redraw);
}
/// <summary>
/// Change the attributes of the sprite.
/// </summary>
/// <param name="attrs">attributes to be changed on the sprite.</param>
[Meta]
public virtual void SetAttributes(SpriteAttributes attrs)
{
this.CallTemplate("setAttributes", JRawValue.From(attrs.Serialize()));
}
/// <summary>
/// Wrapper for setting style properties, also takes single object parameter of multiple styles.
/// </summary>
/// <param name="styles">An object of multiple styles.</param>
[Meta]
public virtual void SetStyle(Dictionary<string, string> styles)
{
this.CallTemplate("setStyle", styles);
}
/// <summary>
/// Wrapper for setting style properties, also takes single object parameter of multiple styles.
/// </summary>
/// <param name="property">The style property to be set</param>
/// <param name="value">The value to apply to the given property</param>
[Meta]
public virtual void SetStyle(string property, string value)
{
this.CallTemplate("setStyle", property, value);
}
/// <summary>
/// Shows the sprite.
/// </summary>
/// <param name="redraw">Flag to immediatly draw the change.</param>
[Meta]
public virtual void Show(bool redraw)
{
this.CallTemplate("show", redraw);
}
/// <summary>
/// Shows the sprite.
/// </summary>
[Meta]
public virtual void Show()
{
this.CallTemplate("show");
}
public virtual string Proxy
{
get
{
if (this.SpriteID.IsEmpty())
{
throw new Exception("You have to set sprite's ID to call its methods");
}
if (this.Draw == null)
{
throw new Exception("Sprite has no DrawComponent reference");
}
var template = this.IsGroup ? "{0}.surface.getGroup(\"{1}\")" : "{0}.get(\"{1}\")";
return template.FormatWith(this.Draw.ClientID, this.SpriteID);
}
}
private SpriteListeners listeners;
/// <summary>
/// Client-side JavaScript Event Handlers
/// </summary>
[Meta]
[ConfigOption("listeners", JsonMode.Object)]
[Category("2. Observable")]
[NotifyParentProperty(true)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Description("Client-side JavaScript Event Handlers")]
public SpriteListeners Listeners
{
get
{
return this.listeners ?? (this.listeners = new SpriteListeners());
}
}
}
public class SpriteCollection : BaseItemCollection<Sprite>
{
}
}