Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Mar 6, 2025. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathimageeditor.ts
More file actions
Latest commit
254 lines (233 loc) · 9.94 KB
/
Copy pathimageeditor.ts
File metadata and controls
254 lines (233 loc) · 9.94 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
module tileworld{
constyoff=4;
constcolorSize=8;
constpaintSize=6;
constcolorsY=30;
constcolorsX=5;
consteditorY=paintSize*4;
constcolorOut=img`
1 1 1 1 1 1 1 1
1 . . . . . . 1
1 . . . . . . 1
1 . . . . . . 1
1 . . . . . . 1
1 . . . . . . 1
1 . . . . . . 1
1 1 1 1 1 1 1 1
`;
constcolorIn=img`
. . . . . . . .
. 1 1 1 1 1 1 .
. 1 . . . . 1 .
. 1 . . . . 1 .
. 1 . . . . 1 .
. 1 . . . . 1 .
. 1 1 1 1 1 1 .
. . . . . . . .
`;
constpaintOut=img`
1 1 1 1 1 1
1 . . . . 1
1 . . . . 1
1 . . . . 1
1 . . . . 1
1 1 1 1 1 1
`;
constpaintIn=img`
. . . . . .
. 1 1 1 1 .
. 1 . . 1 .
. 1 . . 1 .
. 1 1 1 1 .
. . . . . .
`;
enumCursorType{Color,Paint,Menu}
// UI region order
// Menu -> sprite -> color choice -> canvas
exportclassImageEditorextendsBackgroundBase{
privatecursorType: CursorType;// are we selecting a color or painting?
privatecolorCursor: Sprite;
privatepaintCursor: Sprite;
privatemenuCursor: Sprite;
privateselectedColor: number;
privateimage: Image;// 16x16
privateAdown=false;
privatekind=0;
privatedirty=false;
constructor(privatep: AllExport){
super();
this.image=p.getImage(this.kind);
this.colorCursor=sprites.create(colorOut)
this.colorCursor.x=colorsX+(colorSize>>1);
this.colorCursor.y=colorsY+colorSize*8;
utilities.cursorAnimation(this.colorCursor,colorIn);
this.selectedColor=0;
this.paintCursor=sprites.create(paintOut);
utilities.cursorAnimation(this.paintCursor,paintIn);
this.paintCursor.x=paintSize*5+2;
this.paintCursor.y=editorY+2;
this.menuCursor=sprites.create(cursorIn);
this.menuCursor.x=8;
this.menuCursor.y=yoff+8
utilities.cursorAnimation(this.menuCursor,cursorOut);
this.setCursor(CursorType.Menu);
this.update();
controller.left.onEvent(ControllerButtonEvent.Pressed,()=>this.moveLeft());
controller.left.onEvent(ControllerButtonEvent.Repeated,()=>this.moveLeft());
controller.right.onEvent(ControllerButtonEvent.Pressed,()=>this.moveRight());
controller.right.onEvent(ControllerButtonEvent.Repeated,()=>this.moveRight());
controller.up.onEvent(ControllerButtonEvent.Pressed,()=>this.moveUp());
controller.up.onEvent(ControllerButtonEvent.Repeated,()=>this.moveUp());
controller.down.onEvent(ControllerButtonEvent.Pressed,()=>this.moveDown());
controller.down.onEvent(ControllerButtonEvent.Repeated,()=>this.moveDown());
controller.A.onEvent(ControllerButtonEvent.Pressed,()=>{this.Adown=true;this.paintPixel()});
controller.A.onEvent(ControllerButtonEvent.Released,()=>{this.Adown=false;});
controller.B.onEvent(ControllerButtonEvent.Pressed,()=>{
if(this.cursorType==CursorType.Menu)
this.saveAndPop();
elseif(this.cursorType==CursorType.Paint)
this.setCursor(CursorType.Color);
else
this.setCursor(CursorType.Menu);
});
}
privatepaintPixel(){
if(!this.Adown)
return;
if(this.cursorType==CursorType.Color){
constcol=((this.colorCursor.x-colorsX)/colorSize)|0x0;
constrow=((this.colorCursor.y-(colorSize<<1)-colorsY)/colorSize)|0x0;
this.selectedColor=row*2+col;
this.setCursor(CursorType.Paint);
}elseif(this.cursorType==CursorType.Paint){
this.dirty=true;
constcol=((this.paintCursor.x-(paintSize*5+2))/paintSize)|0x0;
constrow=((this.paintCursor.y-(editorY+2))/paintSize)|0x0;
this.image.setPixel(col,row,this.selectedColor);
}else{
constcol=this.menuCursor.x>>4;
if(2<=col&&col<2+this.p.getImages().length){
if(this.dirty)
this.p.saveImage(this.kind);
this.kind=col-2;
this.image=this.p.getImage(this.kind);
this.dirty=false;
}
}
this.update()
}
privatemoveLeft(){
if(this.cursorType==CursorType.Color){
if(this.colorCursor.x>colorsX+colorSize)
this.colorCursor.x-=colorSize
}elseif(this.cursorType==CursorType.Menu){
if(this.menuCursor.x>8)
this.menuCursor.x-=16;
}else{
if(this.paintCursor.x>paintSize*6-1)
this.paintCursor.x-=paintSize
else{
// transition cursor to color editor
this.setCursor(CursorType.Color);
}
}
this.paintPixel();
}
privatemoveRight(){
if(this.cursorType==CursorType.Color){
if(this.colorCursor.x<colorsX+colorSize)
this.colorCursor.x+=colorSize
else{
// transition cursor to paint editor
this.setCursor(CursorType.Paint);
}
}elseif(this.cursorType==CursorType.Menu){
if(this.menuCursor.x<9<<4)
this.menuCursor.x+=16;
}else{
if(this.paintCursor.x<(paintSize*5+2)+paintSize*15)
this.paintCursor.x+=paintSize
}
this.paintPixel();
}
privatemoveUp(){
if(this.cursorType==CursorType.Color){
if(this.colorCursor.y>colorsY+(colorSize<<1)+(colorSize-1))
this.colorCursor.y-=colorSize;
}elseif(this.cursorType==CursorType.Paint){
if(this.paintCursor.y>(editorY+paintSize+1))
this.paintCursor.y-=paintSize
else{
this.setCursor(CursorType.Menu);
}
}
this.paintPixel();
}
privatemoveDown(){
if(this.cursorType==CursorType.Color){
if(this.colorCursor.y<colorsY+(colorSize<<1)+colorSize*(colorSize-1))
this.colorCursor.y+=colorSize
}elseif(this.cursorType==CursorType.Menu){
this.setCursor(CursorType.Paint);
}else{
if(this.paintCursor.y<editorY+2+paintSize*15)
this.paintCursor.y+=paintSize
}
this.paintPixel();
}
privatesaveAndPop(){
this.p.saveImage(this.kind);
game.popScene();
}
privatesetCursor(ct: CursorType){
this.colorCursor.setFlag(SpriteFlag.Invisible,ct!=CursorType.Color);
this.paintCursor.setFlag(SpriteFlag.Invisible,ct!=CursorType.Paint);
this.menuCursor.setFlag(SpriteFlag.Invisible,ct!=CursorType.Menu);
this.cursorType=ct;
}
protectedupdate(): void{
screen.fill(0);
screen.fillRect(0,yoff,16,16,11);
screen.drawTransparentImage(paint,0,yoff)
this.p.getImages().forEach((img,index)=>{
screen.drawImage(img,(2+index)*16,yoff);
if(index==this.kind){
screen.drawTransparentImage(cursorOut,(2+index)*16,yoff);
}
});
//screen.fill(0)
// draw the 16 colors
for(letrow=0;row<8;row++){
for(letcol=0;col<2;col++){
constcolor=row*2+col
constyOffset=colorsY+colorSize+(colorSize>>1)
screen.fillRect(colorsX+col*colorSize+1,yOffset+row*colorSize+1,colorSize-2,colorSize-2,color)
if(this.selectedColor==color){
screen.drawRect(colorsX+col*colorSize,yOffset+row*colorSize,colorSize,colorSize,1)
}
}
}
// take care of transparent
screen.fillRect(colorsX+1,colorsY+13,3,3,13)
screen.fillRect(colorsX+4,colorsY+16,3,3,13)
// frame the sprite editor
// draw the sprite editor
for(letrow=0;row<this.image.height;row++){
consty=editorY+row*paintSize
for(letcol=0;col<this.image.width;col++){
constx=paintSize*5+col*paintSize
constcolor=this.image.getPixel(col,row)
screen.fillRect(x,y,paintSize-1,paintSize-1,color)
if(color==0){
screen.fillRect(x,y,(paintSize>>1)-1,(paintSize>>1)-1,13)
screen.fillRect(x+(paintSize>>1),y+(paintSize>>1),(paintSize>>1)-1,(paintSize>>1)-1,13)
}
}
}
screen.drawRect(28,10+paintSize*2,paintSize*16+(paintSize-2),paintSize*16+(paintSize-2),1)
// draw the sprite
//screen.drawImage(this.image, 134, 12 + paintSize* 2)
//screen.drawRect(133, 11 + paintSize* 2, 18, 18, 1)
}
}
}