-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
icon.layer.ts
128 lines (122 loc) · 4.03 KB
/
icon.layer.ts
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
import {TPlatformFeatures} from '../../platforms/platform';
import {addAlphaChannelToImageData} from '../../utils';
import {Point} from '../point';
import {AbstractImageLayer} from './abstract-image.layer';
import {EditMode, TLayerModifiers, TModifierType} from './abstract.layer';
export class IconLayer extends AbstractImageLayer {
protected type: ELayerType = 'icon';
modifiers: TLayerModifiers = {
x: {
getValue: () => this.position.x,
setValue: (v: number) => {
this.position.x = v;
this.updateBounds();
this.draw();
},
type: TModifierType.number
},
y: {
getValue: () => this.position.y,
setValue: (v: number) => {
this.position.y = v;
this.updateBounds();
this.draw();
},
type: TModifierType.number
},
w: {
getValue: () => this.size.x,
type: TModifierType.number
},
h: {
getValue: () => this.size.y,
type: TModifierType.number
},
icon: {
getValue: () => this.data,
setValue: (v: HTMLImageElement) => {
this.imageName = v.dataset.name;
const [w, h] = [Number(v.dataset.w), Number(v.dataset.h)];
if (w && h) {
this.size = new Point(w, h);
}
const buf = document.createElement('canvas');
const ctx = buf.getContext('2d');
buf.width = this.size.x;
buf.height = this.size.y;
ctx.drawImage(v, 0, 0);
this.data = addAlphaChannelToImageData(ctx.getImageData(0, 0, this.size.x, this.size.y), this.color);
this.updateBounds();
this.applyColor();
this.draw();
},
type: TModifierType.image
},
color: {
getValue: () => this.color,
setValue: (v: string) => {
this.color = v;
this.applyColor();
this.draw();
},
type: TModifierType.color
},
overlay: {
getValue: () => this.overlay,
setValue: (v: boolean) => {
this.overlay = v;
this.draw();
},
type: TModifierType.boolean
},
inverted: {
getValue: () => this.inverted,
setValue: (v: boolean) => {
this.inverted = v;
this.draw();
},
type: TModifierType.boolean
}
};
startEdit(mode: EditMode, point: Point) {
this.pushHistory();
this.mode = mode;
this.editState = {
firstPoint: point,
position: this.position.clone(),
size: this.size.clone()
};
}
constructor(protected features: TPlatformFeatures) {
super(features);
if (!this.features.hasRGBSupport && !this.features.hasIndexedColors) {
delete this.modifiers.color;
}
if (!this.features.hasInvertedColors) {
delete this.modifiers.inverted;
}
this.color = this.features.defaultColor;
}
edit(point: Point, originalEvent: MouseEvent) {
const {position, size, firstPoint} = this.editState;
switch (this.mode) {
case EditMode.MOVING:
this.position = position.clone().add(point.clone().subtract(firstPoint)).round();
break;
case EditMode.RESIZING:
// this.size = size.clone().add(point.clone().subtract(firstPoint)).round();
// todo
break;
case EditMode.CREATING:
this.position = point.clone();
this.size = new Point(10, 10);
break;
}
this.updateBounds();
this.draw();
}
stopEdit() {
this.mode = EditMode.NONE;
this.editState = null;
}
}