1// MIT License
2
3// Copyright (c) 2017 Vadim Grigoruk @nesbox // grigoruk@gmail.com
4
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#include "sprite.h"
24#include "ext/history.h"
25
26#include <math.h>
27#include <ctype.h>
28
29#define CANVAS_SIZE (64)
30#define PALETTE_CELL_SIZE 8
31#define PALETTE_ROWS 2
32#define PALETTE_COLS (TIC_PALETTE_SIZE / PALETTE_ROWS)
33#define PALETTE_WIDTH (PALETTE_COLS * PALETTE_CELL_SIZE)
34#define PALETTE_HEIGHT (PALETTE_ROWS * PALETTE_CELL_SIZE)
35#define BRUSH_SIZES 4
36
37enum
38{
39 ToolbarH = TOOLBAR_SIZE,
40 CanvasX = 24, CanvasY = 20, CanvasW = CANVAS_SIZE, CanvasH = CANVAS_SIZE,
41 PaletteX = 24, PaletteY = 112, PaletteW = PALETTE_WIDTH, PaletteH = PALETTE_HEIGHT,
42 SheetX = TIC80_WIDTH - TIC_SPRITESHEET_SIZE - 1, SheetY = ToolbarH, SheetW = TIC_SPRITESHEET_SIZE, SheetH = TIC_SPRITESHEET_SIZE,
43};
44
45// !TODO: move it to helpers place
46static void drawPanelBorder(tic_mem* tic, s32 x, s32 y, s32 w, s32 h)
47{
48 tic_api_rect(tic, x, y-1, w, 1, tic_color_dark_grey);
49 tic_api_rect(tic, x-1, y, 1, h, tic_color_dark_grey);
50 tic_api_rect(tic, x, y+h, w, 1, tic_color_light_grey);
51 tic_api_rect(tic, x+w, y, 1, h, tic_color_light_grey);
52}
53
54static void clearCanvasSelection(Sprite* sprite)
55{
56 memset(&sprite->select.rect, 0, sizeof(tic_rect));
57}
58
59static void initTileSheet(Sprite* sprite)
60{
61 sprite->blit.page %= sprite->blit.pages;
62 sprite->sheet = tic_tilesheet_get((( sprite->blit.pages + sprite->blit.page) << 1) + sprite->blit.bank, (u8*)sprite->src);
63}
64
65static void updateIndex(Sprite* sprite)
66{
67 sprite->index = sprite->y * sprite->blit.pages * TIC_SPRITESHEET_COLS + sprite->x;
68 // index has changed, clear selection
69 clearCanvasSelection(sprite);
70}
71
72static inline bool isIdle(Sprite* sprite)
73{
74 return sprite->anim.movie == &sprite->anim.idle;
75}
76
77static void selectViewportPage(Sprite* sprite, u8 page)
78{
79 if(isIdle(sprite))
80 {
81 Anim* anim = sprite->anim.page.items;
82 anim->start = (page - sprite->blit.page) * TIC_SPRITESHEET_SIZE;
83 sprite->anim.movie = resetMovie(&sprite->anim.page);
84
85 sprite->blit.page = page;
86 updateIndex(sprite);
87 initTileSheet(sprite);
88 }
89}
90
91static void leftViewport(Sprite* sprite)
92{
93 s32 page = sprite->blit.page + sprite->blit.pages - 1;
94 selectViewportPage(sprite, page % sprite->blit.pages);
95}
96
97static void rightViewport(Sprite* sprite)
98{
99 s32 page = sprite->blit.page + sprite->blit.pages + 1;
100 selectViewportPage(sprite, page % sprite->blit.pages);
101}
102
103static s32 getIndexPosX(Sprite* sprite)
104{
105 return (sprite->x + sprite->blit.page * TIC_SPRITESHEET_COLS) * TIC_SPRITESIZE;
106}
107
108static s32 getIndexPosY(Sprite* sprite)
109{
110 return (sprite->y + sprite->blit.bank * TIC_SPRITESHEET_COLS) * TIC_SPRITESIZE;
111}
112
113static void drawSelection(Sprite* sprite, s32 x, s32 y, s32 w, s32 h)
114{
115 tic_mem* tic = sprite->tic;
116
117 enum{Step = 3};
118 u8 color = tic_color_white;
119
120 s32 index = sprite->tickCounter / 10;
121 for(s32 i = x; i < (x+w); i++) { tic_api_pix(tic, i, y, index++ % Step ? color : 0, false);} index++;
122 for(s32 i = y; i < (y+h); i++) { tic_api_pix(tic, x + w-1, i, index++ % Step ? color : 0, false);} index++;
123 for(s32 i = (x+w-1); i >= x; i--) { tic_api_pix(tic, i, y + h-1, index++ % Step ? color : 0, false);} index++;
124 for(s32 i = (y+h-1); i >= y; i--) { tic_api_pix(tic, x, i, index++ % Step ? color : 0, false);}
125}
126
127static tic_rect getSpriteRect(Sprite* sprite)
128{
129 s32 x = getIndexPosX(sprite);
130 s32 y = getIndexPosY(sprite);
131
132 return (tic_rect){x, y, sprite->size, sprite->size};
133}
134
135static void drawCursorBorder(Sprite* sprite, s32 x, s32 y, s32 w, s32 h)
136{
137 tic_mem* tic = sprite->tic;
138
139 tic_api_rectb(tic, x, y, w, h, tic_color_black);
140 tic_api_rectb(tic, x-1, y-1, w+2, h+2, tic_color_white);
141}
142
143static void processPickerCanvasMouse(Sprite* sprite, s32 x, s32 y, s32 sx, s32 sy)
144{
145 tic_mem* tic = sprite->tic;
146 tic_rect rect = {x, y, CANVAS_SIZE, CANVAS_SIZE};
147 const s32 Size = CANVAS_SIZE / sprite->size;
148
149 if(checkMousePos(sprite->studio, &rect))
150 {
151 setCursor(sprite->studio, tic_cursor_hand);
152
153 s32 mx = tic_api_mouse(tic).x - x;
154 s32 my = tic_api_mouse(tic).y - y;
155
156 mx -= mx % Size;
157 my -= my % Size;
158
159 drawCursorBorder(sprite, x + mx, y + my, Size, Size);
160
161 if(checkMouseDown(sprite->studio, &rect, tic_mouse_left))
162 sprite->color = tic_tilesheet_getpix(&sprite->sheet, sx + mx / Size, sy + my / Size);
163
164 if(checkMouseDown(sprite->studio, &rect, tic_mouse_right))
165 sprite->color2 = tic_tilesheet_getpix(&sprite->sheet, sx + mx / Size, sy + my / Size);
166 }
167}
168
169static void paintPoint(Sprite* sprite, u8 color, s32 x, s32 y)
170{
171 s32 pixels = sprite->brushSize;
172
173 for(s32 j = 0; j < pixels; j++)
174 for(s32 i = 0; i < pixels; i++)
175 tic_tilesheet_setpix(&sprite->sheet, x+i, y+j, color);
176}
177
178static void paintLine(Sprite* sprite, u8 color, s32 x1, s32 y1, s32 x2, s32 y2)
179{
180 float dx = x2 - x1;
181 float dy = y2 - y1;
182
183 // If infinite slope, draw vertical line
184 if (dx == 0)
185 {
186 s32 step = y2 > y1 ? 1 : -1;
187 for (s32 ly = y1; y2 > y1 ? ly <= y2 : ly >= y2; ly += step)
188 paintPoint(sprite, color, x1, ly);
189 }
190 else
191 {
192 float slope = dy / dx;
193 float intercept = y1 - slope * x1;
194
195 // Draw for each point along whichever axis is longer
196 if (fabs(dx) >= fabs(dy))
197 {
198 s32 step = x2 > x1 ? 1 : -1;
199 for (s32 lx = x1; x2 > x1 ? lx <= x2 : lx >= x2; lx += step)
200 paintPoint(sprite, color, lx, slope * lx + intercept);
201 }
202 else
203 {
204 s32 step = y2 > y1 ? 1 : -1;
205 for (s32 ly = y1; y2 > y1 ? ly <= y2 : ly >= y2; ly += step)
206 paintPoint(sprite, color, (ly - intercept) / slope, ly);
207 }
208 }
209}
210
211static s32 toCanvasCoord(s32 Size, s32 brushSize, s32 canvas_x, s32 x)
212{
213 s32 offset = (brushSize - Size) / 2;
214
215 x = (x - canvas_x) - offset;
216 x -= x % Size;
217
218 if (x < 0) x = 0;
219 if (x + brushSize >= CANVAS_SIZE) x = CANVAS_SIZE - brushSize;
220
221 return x;
222}
223
224static void processDrawCanvasMouse(Sprite* sprite, s32 x, s32 y, s32 sx, s32 sy)
225{
226 tic_mem* tic = sprite->tic;
227 tic_rect rect = {x, y, CANVAS_SIZE, CANVAS_SIZE};
228 const s32 Size = CANVAS_SIZE / sprite->size;
229
230 if(checkMousePos(sprite->studio, &rect))
231 {
232 setCursor(sprite->studio, tic_cursor_hand);
233
234 s32 brushSize = sprite->brushSize*Size;
235 s32 mx = toCanvasCoord(Size, brushSize, x, tic_api_mouse(tic).x);
236 s32 my = toCanvasCoord(Size, brushSize, y, tic_api_mouse(tic).y);
237 s32 pmx = toCanvasCoord(Size, brushSize, x, sprite->previousMouse.x);
238 s32 pmy = toCanvasCoord(Size, brushSize, y, sprite->previousMouse.y);
239
240 SHOW_TOOLTIP(sprite->studio, "[x=%02i y=%02i]", mx / Size, my / Size);
241
242 drawCursorBorder(sprite, x + mx, y + my, brushSize, brushSize);
243
244 bool left = checkMouseDown(sprite->studio, &rect, tic_mouse_left);
245 bool right = checkMouseDown(sprite->studio, &rect, tic_mouse_right);
246
247 if(left || right)
248 {
249 u8 color = left ? sprite->color : sprite->color2;
250 paintLine(sprite, color,
251 sx + pmx / Size,
252 sy + pmy / Size,
253 sx + mx / Size,
254 sy + my / Size
255 );
256 history_add(sprite->history);
257 }
258 }
259}
260
261static void pasteSelection(Sprite* sprite)
262{
263 s32 l = getIndexPosX(sprite);
264 s32 t = getIndexPosY(sprite);
265 s32 r = l + sprite->size;
266 s32 b = t + sprite->size;
267
268 for(s32 sy = t, i = 0; sy < b; sy++)
269 for(s32 sx = l; sx < r; sx++)
270 tic_tilesheet_setpix(&sprite->sheet, sx, sy, sprite->select.back[i++]);
271
272 tic_rect* rect = &sprite->select.rect;
273
274 l += rect->x;
275 t += rect->y;
276 r = l + rect->w;
277 b = t + rect->h;
278
279 for(s32 sy = t, i = 0; sy < b; sy++)
280 for(s32 sx = l; sx < r; sx++)
281 tic_tilesheet_setpix(&sprite->sheet, sx, sy, sprite->select.front[i++]);
282
283 history_add(sprite->history);
284}
285
286static void copySelection(Sprite* sprite)
287{
288 tic_rect rect = getSpriteRect(sprite);
289 s32 r = rect.x + rect.w;
290 s32 b = rect.y + rect.h;
291
292 for(s32 sy = rect.y, i = 0; sy < b; sy++)
293 for(s32 sx = rect.x; sx < r; sx++)
294 sprite->select.back[i++] = tic_tilesheet_getpix(&sprite->sheet, sx, sy);
295
296 {
297 tic_rect* rect = &sprite->select.rect;
298 memset(sprite->select.front, 0, CANVAS_SIZE * CANVAS_SIZE);
299
300 for(s32 j = rect->y, index = 0; j < (rect->y + rect->h); j++)
301 for(s32 i = rect->x; i < (rect->x + rect->w); i++)
302 {
303 u8* color = &sprite->select.back[i+j*sprite->size];
304 sprite->select.front[index++] = *color;
305 *color = sprite->color2;
306 }
307 }
308}
309
310static void processSelectCanvasMouse(Sprite* sprite, s32 x, s32 y)
311{
312 tic_mem* tic = sprite->tic;
313
314 tic_rect rect = {x, y, CANVAS_SIZE, CANVAS_SIZE};
315 const s32 Size = CANVAS_SIZE / sprite->size;
316
317 bool endDrag = false;
318
319 if(checkMousePos(sprite->studio, &rect))
320 {
321 setCursor(sprite->studio, tic_cursor_hand);
322
323 s32 mx = tic_api_mouse(tic).x - x;
324 s32 my = tic_api_mouse(tic).y - y;
325
326 mx -= mx % Size;
327 my -= my % Size;
328
329 drawCursorBorder(sprite, x + mx, y + my, Size, Size);
330
331 if(checkMouseDown(sprite->studio, &rect, tic_mouse_left))
332 {
333 if(sprite->select.drag)
334 {
335 s32 x = mx / Size;
336 s32 y = my / Size;
337
338 s32 rl = MIN(x, sprite->select.start.x);
339 s32 rt = MIN(y, sprite->select.start.y);
340 s32 rr = MAX(x, sprite->select.start.x);
341 s32 rb = MAX(y, sprite->select.start.y);
342
343 sprite->select.rect = (tic_rect){rl, rt, rr - rl + 1, rb - rt + 1};
344 }
345 else
346 {
347 sprite->select.drag = true;
348 sprite->select.start = (tic_point){mx / Size, my / Size};
349 sprite->select.rect = (tic_rect){sprite->select.start.x, sprite->select.start.y, 1, 1};
350 }
351 }
352 else endDrag = sprite->select.drag;
353 }
354 else endDrag = !tic->ram->input.mouse.left && sprite->select.drag;
355
356 if(endDrag)
357 {
358 copySelection(sprite);
359 sprite->select.drag = false;
360 }
361}
362
363static void floodFill(Sprite* sprite, s32 l, s32 t, s32 r, s32 b, s32 x, s32 y, u8 color, u8 fill)
364{
365 if(tic_tilesheet_getpix(&sprite->sheet, x, y) == color)
366 {
367 tic_tilesheet_setpix(&sprite->sheet, x, y, fill);
368
369 if(x > l) floodFill(sprite, l, t, r, b, x-1, y, color, fill);
370 if(x < r) floodFill(sprite, l, t, r, b, x+1, y, color, fill);
371 if(y > t) floodFill(sprite, l, t, r, b, x, y-1, color, fill);
372 if(y < b) floodFill(sprite, l, t, r, b, x, y+1, color, fill);
373 }
374}
375
376static void replaceColor(Sprite* sprite, s32 l, s32 t, s32 r, s32 b, s32 x, s32 y, u8 color, u8 fill)
377{
378 for(s32 sy = t; sy <= b; sy++)
379 for(s32 sx = l; sx <= r; sx++)
380 if(tic_tilesheet_getpix(&sprite->sheet, sx, sy) == color)
381 tic_tilesheet_setpix(&sprite->sheet, sx, sy, fill);
382}
383
384static void processFillCanvasMouse(Sprite* sprite, s32 x, s32 y, s32 l, s32 t)
385{
386 tic_mem* tic = sprite->tic;
387 tic_rect rect = {x, y, CANVAS_SIZE, CANVAS_SIZE};
388 const s32 Size = CANVAS_SIZE / sprite->size;
389
390 if(checkMousePos(sprite->studio, &rect))
391 {
392 setCursor(sprite->studio, tic_cursor_hand);
393
394 s32 mx = tic_api_mouse(tic).x - x;
395 s32 my = tic_api_mouse(tic).y - y;
396
397 mx -= mx % Size;
398 my -= my % Size;
399
400 drawCursorBorder(sprite, x + mx, y + my, Size, Size);
401
402 bool left = checkMouseClick(sprite->studio, &rect, tic_mouse_left);
403 bool right = checkMouseClick(sprite->studio, &rect, tic_mouse_right);
404
405 if(left || right)
406 {
407 s32 sx = l + mx / Size;
408 s32 sy = t + my / Size;
409
410 u8 color = tic_tilesheet_getpix(&sprite->sheet, sx, sy);
411 u8 fill = left ? sprite->color : sprite->color2;
412
413 if(color != fill)
414 {
415 tic_api_key(tic, tic_key_ctrl)
416 ? replaceColor(sprite, l, t, l + sprite->size-1, t + sprite->size-1, sx, sy, color, fill)
417 : floodFill(sprite, l, t, l + sprite->size-1, t + sprite->size-1, sx, sy, color, fill);
418 }
419
420 history_add(sprite->history);
421 }
422 }
423}
424
425static bool hasCanvasSelection(Sprite* sprite)
426{
427 return sprite->mode == SPRITE_SELECT_MODE && sprite->select.rect.w && sprite->select.rect.h;
428}
429
430static void drawBrushSlider(Sprite* sprite, s32 x, s32 y)
431{
432 tic_mem* tic = sprite->tic;
433
434 enum {Count = BRUSH_SIZES, Size = 5};
435
436 tic_rect rect = {x, y, Size, (Size+1)*Count};
437
438 bool over = false;
439 if(checkMousePos(sprite->studio, &rect))
440 {
441 setCursor(sprite->studio, tic_cursor_hand);
442
443 showTooltip(sprite->studio, "BRUSH SIZE");
444 over = true;
445
446 if(checkMouseDown(sprite->studio, &rect, tic_mouse_left))
447 {
448 s32 my = tic_api_mouse(tic).y - y;
449
450 sprite->brushSize = Count - my / (Size+1);
451 }
452 }
453
454 tic_api_rect(tic, x+1, y, Size-2, Size*Count, tic_color_black);
455
456 for(s32 i = 0; i < Count; i++)
457 {
458 s32 offset = y + i*(Size+1);
459
460 tic_api_rect(tic, x, offset, Size, Size, tic_color_black);
461 tic_api_rect(tic, x + 6, offset + 2, Count - i, 1, tic_color_black);
462 }
463
464 tic_api_rect(tic, x+2, y+1, 1, Size*Count+1, (over ? tic_color_white : tic_color_grey));
465
466 s32 offset = y + (Count - sprite->brushSize)*(Size+1);
467 tic_api_rect(tic, x, offset, Size, Size, tic_color_black);
468 tic_api_rect(tic, x+1, offset+1, Size-2, Size-2, (over ? tic_color_white : tic_color_grey));
469}
470
471static void drawCanvasVBank1(Sprite* sprite, s32 x, s32 y)
472{
473 tic_mem* tic = sprite->tic;
474
475 const s32 Size = CANVAS_SIZE / sprite->size;
476 const tic_rect rect = getSpriteRect(sprite);
477
478 const tic_rect canvasRect = {x, y, CANVAS_SIZE, CANVAS_SIZE};
479 if(checkMouseDown(sprite->studio, &canvasRect, tic_mouse_middle))
480 {
481 s32 mx = tic_api_mouse(tic).x - x;
482 s32 my = tic_api_mouse(tic).y - y;
483 sprite->color = tic_tilesheet_getpix(&sprite->sheet, rect.x + mx / Size, rect.y + my / Size);
484 }
485
486 drawPanelBorder(tic, canvasRect.x - 1, canvasRect.y - 1, canvasRect.w + 2, canvasRect.h + 2);
487 tic_api_rectb(tic, canvasRect.x - 1, canvasRect.y - 1, canvasRect.w + 2, canvasRect.h + 2, tic_color_black);
488
489 if(!sprite->palette.edit)
490 {
491 switch(sprite->mode)
492 {
493 case SPRITE_DRAW_MODE:
494 processDrawCanvasMouse(sprite, x, y, rect.x, rect.y);
495 drawBrushSlider(sprite, x - 15, y + 20);
496 break;
497 case SPRITE_PICK_MODE: processPickerCanvasMouse(sprite, x, y, rect.x, rect.y); break;
498 case SPRITE_SELECT_MODE: processSelectCanvasMouse(sprite, x, y); break;
499 case SPRITE_FILL_MODE: processFillCanvasMouse(sprite, x, y, rect.x, rect.y); break;
500 }
501 }
502
503 if(hasCanvasSelection(sprite))
504 drawSelection(sprite, x + sprite->select.rect.x * Size - 1, y + sprite->select.rect.y * Size - 1,
505 sprite->select.rect.w * Size + 2, sprite->select.rect.h * Size + 2);
506 else
507 {
508 char buf[sizeof "#9999"];
509 sprintf(buf, "#%i", sprite->index + tic_blit_calc_index(&sprite->blit));
510
511 s32 ix = x + (CANVAS_SIZE - strlen(buf) * TIC_FONT_WIDTH) / 2;
512 s32 iy = TIC_SPRITESIZE + 2;
513 tic_api_print(tic, buf, ix, iy+1, tic_color_black, true, 1, false);
514 tic_api_print(tic, buf, ix, iy, tic_color_white, true, 1, false);
515 }
516}
517
518static void drawCanvas(Sprite* sprite, s32 x, s32 y)
519{
520 tic_mem* tic = sprite->tic;
521
522 tic_rect rect = getSpriteRect(sprite);
523 s32 r = rect.x + rect.w;
524 s32 b = rect.y + rect.h;
525
526 const s32 Size = CANVAS_SIZE / sprite->size;
527
528 for(s32 sy = rect.y, j = y; sy < b; sy++, j += Size)
529 for(s32 sx = rect.x, i = x; sx < r; sx++, i += Size)
530 tic_api_rect(tic, i, j, Size, Size, tic_tilesheet_getpix(&sprite->sheet, sx, sy));
531}
532
533static void upCanvas(Sprite* sprite)
534{
535 tic_rect* rect = &sprite->select.rect;
536 if(rect->y > 0) rect->y--;
537 pasteSelection(sprite);
538}
539
540static void downCanvas(Sprite* sprite)
541{
542 tic_rect* rect = &sprite->select.rect;
543 if(rect->y + rect->h < sprite->size) rect->y++;
544 pasteSelection(sprite);
545}
546
547static void leftCanvas(Sprite* sprite)
548{
549 tic_rect* rect = &sprite->select.rect;
550 if(rect->x > 0) rect->x--;
551 pasteSelection(sprite);
552}
553
554static void rightCanvas(Sprite* sprite)
555{
556 tic_rect* rect = &sprite->select.rect;
557 if(rect->x + rect->w < sprite->size) rect->x++;
558 pasteSelection(sprite);
559}
560
561static void rotateSelectRect(Sprite* sprite)
562{
563 tic_rect rect = sprite->select.rect;
564
565 s32 selection_center_x = rect.x + rect.w/2;
566 s32 selection_center_y = rect.y + rect.h/2;
567
568 // Rotate
569 sprite->select.rect.w = rect.h;
570 sprite->select.rect.h = rect.w;
571
572 // Make the new center be at the position of the previous center
573 sprite->select.rect.x -= (sprite->select.rect.x + sprite->select.rect.w/2) - selection_center_x;
574 sprite->select.rect.y -= (sprite->select.rect.y + sprite->select.rect.h/2) - selection_center_y;
575
576 // Check if we are not out of boundaries
577 if (sprite->select.rect.x < 0) sprite->select.rect.x = 0;
578 if (sprite->select.rect.y < 0) sprite->select.rect.y = 0;
579
580 if (sprite->select.rect.x + sprite->select.rect.w >= sprite->size)
581 {
582 sprite->select.rect.x -= sprite->select.rect.x + sprite->select.rect.w - sprite->size;
583 }
584
585 if (sprite->select.rect.y + sprite->select.rect.h >= sprite->size)
586 {
587 sprite->select.rect.y -= sprite->select.rect.y + sprite->select.rect.h - sprite->size;
588 }
589}
590
591static void rotateCanvas(Sprite* sprite)
592{
593 u8* buffer = (u8*)malloc(CANVAS_SIZE*CANVAS_SIZE);
594
595 if(buffer)
596 {
597 {
598 tic_rect rect = sprite->select.rect;
599 const s32 Size = rect.h * rect.w;
600 s32 diff = 0;
601
602 for(s32 y = 0, i = 0; y < rect.w; y++)
603 for(s32 x = 0; x < rect.h; x++)
604 {
605 diff = rect.w * (x + 1) -y;
606 buffer[i++] = sprite->select.front[Size - diff];
607 }
608
609 for (s32 i = 0; i<Size; i++)
610 sprite->select.front[i] = buffer[i];
611
612 rotateSelectRect(sprite);
613 pasteSelection(sprite);
614 history_add(sprite->history);
615 }
616
617 free(buffer);
618 }
619}
620
621static void deleteCanvas(Sprite* sprite)
622{
623 tic_rect* rect = &sprite->select.rect;
624
625 s32 left = getIndexPosX(sprite) + rect->x;
626 s32 top = getIndexPosY(sprite) + rect->y;
627 s32 right = left + rect->w;
628 s32 bottom = top + rect->h;
629
630 for(s32 pixel_y = top; pixel_y < bottom; pixel_y++)
631 for(s32 pixel_x = left; pixel_x < right; pixel_x++)
632 tic_tilesheet_setpix(&sprite->sheet, pixel_x, pixel_y, sprite->color2);
633
634 clearCanvasSelection(sprite);
635
636 history_add(sprite->history);
637}
638
639static void flipCanvasHorz(Sprite* sprite)
640{
641 tic_rect* rect = &sprite->select.rect;
642
643 s32 sprite_x = getIndexPosX(sprite);
644 s32 sprite_y = getIndexPosY(sprite);
645
646 s32 right = sprite_x + rect->x + rect->w/2;
647 s32 bottom = sprite_y + rect->y + rect->h;
648
649 for(s32 y = sprite_y + rect->y; y < bottom; y++)
650 for(s32 x = sprite_x + rect->x, i = sprite_x + rect->x + rect->w - 1; x < right; x++, i--)
651 {
652 u8 color = tic_tilesheet_getpix(&sprite->sheet, x, y);
653 tic_tilesheet_setpix(&sprite->sheet, x, y, tic_tilesheet_getpix(&sprite->sheet, i, y));
654 tic_tilesheet_setpix(&sprite->sheet, i, y, color);
655 }
656
657 history_add(sprite->history);
658 copySelection(sprite);
659}
660
661static void flipCanvasVert(Sprite* sprite)
662{
663 tic_rect* rect = &sprite->select.rect;
664
665 s32 sprite_x = getIndexPosX(sprite);
666 s32 sprite_y = getIndexPosY(sprite);
667
668 s32 right = sprite_x + rect->x + rect->w;
669 s32 bottom = sprite_y + rect->y + rect->h/2;
670
671 for(s32 y = sprite_y + rect->y, i = sprite_y + rect->y + rect->h - 1; y < bottom; y++, i--)
672 for(s32 x = sprite_x + rect->x; x < right; x++)
673 {
674 u8 color = tic_tilesheet_getpix(&sprite->sheet, x, y);
675 tic_tilesheet_setpix(&sprite->sheet, x, y, tic_tilesheet_getpix(&sprite->sheet, x, i));
676 tic_tilesheet_setpix(&sprite->sheet, x, i, color);
677 }
678
679 history_add(sprite->history);
680 copySelection(sprite);
681}
682
683static s32* getSpriteIndexes(Sprite* sprite)
684{
685 static s32 indexes[TIC_SPRITESIZE*TIC_SPRITESIZE+1];
686 memset(indexes, -1, sizeof indexes);
687
688 u16 sheet_cols = TIC_SPRITESHEET_COLS * sprite->blit.pages;
689
690 {
691 tic_rect r = {sprite->index % sheet_cols, sprite->index / sheet_cols
692 , sprite->size / TIC_SPRITESIZE, sprite->size / TIC_SPRITESIZE};
693
694 s32 c = 0;
695 for(s32 j = r.y; j < r.h + r.y; j++)
696 for(s32 i = r.x; i < r.w + r.x; i++)
697 indexes[c++] = (i + j * sheet_cols) + sprite->blit.bank * TIC_BANK_SPRITES;
698 }
699
700 return indexes;
701}
702
703static void drawFlags(Sprite* sprite, s32 x, s32 y)
704{
705 tic_mem* tic = sprite->tic;
706
707 if(hasCanvasSelection(sprite)) return;
708
709 enum {Size = 5};
710
711 u8* flags = getBankFlags(sprite->studio)->data;
712 u8 or = 0;
713 u8 and = 0xff;
714
715 const s32* indexes = getSpriteIndexes(sprite);
716
717 {
718 const s32* i = indexes;
719 while(*i >= 0)
720 {
721 u8 mask = flags[*i++];
722 or |= mask;
723 and &= mask;
724 }
725 }
726
727 for(s32 i = 0; i < BITS_IN_BYTE; i++)
728 {
729 const u8 mask = 1 << i;
730 tic_rect rect = {x, y + (Size+1)*i, Size, Size};
731
732 bool over = false;
733 if(checkMousePos(sprite->studio, &rect))
734 {
735 setCursor(sprite->studio, tic_cursor_hand);
736 over = true;
737
738 SHOW_TOOLTIP(sprite->studio, "set flag [%i]", i);
739
740 if(checkMouseClick(sprite->studio, &rect, tic_mouse_left))
741 {
742 const s32* i = indexes;
743
744 if(or & mask)
745 while(*i >= 0)
746 flags[*i++] &= ~mask;
747 else
748 while(*i >= 0)
749 flags[*i++] |= mask;
750 }
751 }
752
753 tic_api_rect(tic, rect.x, rect.y, Size, Size, tic_color_black);
754
755 u8 flagColor = i + 2;
756
757 if(or & mask)
758 tic_api_pix(tic, rect.x + 2, rect.y + 2, flagColor, false);
759 else if(over)
760 tic_api_rect(tic, rect.x + 1, rect.y + 1, Size - 2, Size - 2, flagColor);
761
762 if(and & mask)
763 {
764 tic_api_rect(tic, rect.x + 1, rect.y + 1, Size - 2, Size - 2, flagColor);
765 tic_api_pix(tic, rect.x + 3, rect.y + 1, tic_color_white, false);
766 }
767
768 tic_api_print(tic, (char[]){'0' + i, '\0'}, rect.x + (Size+2), rect.y, tic_color_light_grey, false, 1, true);
769 }
770}
771
772static void switchBitMode(Sprite* sprite, tic_bpp bpp)
773{
774 tic_blit_update_bpp(&sprite->blit, bpp);
775 updateIndex(sprite);
776 initTileSheet(sprite);
777
778 sprite->color %= 1 << sprite->blit.mode;
779 sprite->color2 %= 1 << sprite->blit.mode;
780}
781
782static void drawBitMode(Sprite* sprite, s32 x, s32 y, s32 w, s32 h)
783{
784 tic_mem* tic = sprite->tic;
785 s32 label_w = tic_api_print(tic, "BPP :", x+2, y, tic_color_dark_grey, false, 1, true);
786 x += label_w+4;
787 w -= label_w+4;
788
789 enum {Modes = 3, SizeY = 5, SizeX = 5, OffsetX = 15};
790
791 s32 centerX = x + w / 2;
792
793 for(s32 i = 0; i < Modes; i++)
794 {
795 tic_bpp mode = 1 << (2-i);
796 bool current = mode == sprite->blit.mode;
797
798 tic_rect rect = {centerX - SizeX / 2 + (i-1) * OffsetX, y, SizeX, SizeY};
799
800 bool over = false;
801 if(checkMousePos(sprite->studio, &rect))
802 {
803 setCursor(sprite->studio, tic_cursor_hand);
804 over = true;
805
806 if(mode > 1)
807 SHOW_TOOLTIP(sprite->studio, "%iBITS PER PIXEL", mode);
808 else
809 SHOW_TOOLTIP(sprite->studio, "%iBIT PER PIXEL", mode);
810
811 if(checkMouseClick(sprite->studio, &rect, tic_mouse_left))
812 {
813 switchBitMode(sprite, mode);
814 }
815 }
816
817 u8 label_color = current ? tic_color_white : tic_color_dark_grey;
818
819 tic_api_rect(tic, rect.x, rect.y, SizeX, SizeY, tic_color_dark_grey);
820 if (current) {
821 tic_api_rect(tic, rect.x+1, rect.y+1, SizeX-2, SizeY-2, tic_color_yellow-i);
822 tic_api_pix(tic, rect.x+3, rect.y+1,tic_color_white, false);
823 }
824 else if (over)
825 tic_api_rect(tic, rect.x+1, rect.y+1, SizeX-2, SizeY-2, tic_color_light_grey);
826
827 tic_api_print(tic, (char[]){'0' + mode, '\0'}, rect.x - 4, rect.y, label_color, false, 1, true);
828 }
829}
830
831static void drawMoveButtons(Sprite* sprite)
832{
833 if(hasCanvasSelection(sprite))
834 {
835 enum { x = 24 };
836 enum { y = 20 };
837
838 static const u8 Icons[] = {tic_icon_bigup, tic_icon_bigdown, tic_icon_bigleft, tic_icon_bigright};
839
840 static const tic_rect Rects[] =
841 {
842 {x + (CANVAS_SIZE - TIC_SPRITESIZE)/2, y - TIC_SPRITESIZE, TIC_SPRITESIZE, TIC_SPRITESIZE/2},
843 {x + (CANVAS_SIZE - TIC_SPRITESIZE)/2, y + CANVAS_SIZE + TIC_SPRITESIZE/2, TIC_SPRITESIZE, TIC_SPRITESIZE/2},
844 {x - TIC_SPRITESIZE, y + (CANVAS_SIZE - TIC_SPRITESIZE)/2, TIC_SPRITESIZE/2, TIC_SPRITESIZE},
845 {x + CANVAS_SIZE + TIC_SPRITESIZE/2, y + (CANVAS_SIZE - TIC_SPRITESIZE)/2, TIC_SPRITESIZE/2, TIC_SPRITESIZE},
846 };
847
848 static void(* const Func[])(Sprite*) = {upCanvas, downCanvas, leftCanvas, rightCanvas};
849
850 bool down = false;
851 for(s32 i = 0; i < COUNT_OF(Icons); i++)
852 {
853 down = false;
854
855 if(checkMousePos(sprite->studio, &Rects[i]))
856 {
857 setCursor(sprite->studio, tic_cursor_hand);
858
859 if(checkMouseDown(sprite->studio, &Rects[i], tic_mouse_left)) down = true;
860
861 if(checkMouseClick(sprite->studio, &Rects[i], tic_mouse_left))
862 Func[i](sprite);
863 }
864
865 drawBitIcon(sprite->studio, Icons[i], Rects[i].x, Rects[i].y+1, down ? tic_color_white : tic_color_black);
866
867 if(!down) drawBitIcon(sprite->studio, Icons[i], Rects[i].x, Rects[i].y, tic_color_white);
868 }
869 }
870}
871
872static void drawRGBSlider(Sprite* sprite, s32 x, s32 y, u8* value)
873{
874 tic_mem* tic = sprite->tic;
875
876 enum {Size = CANVAS_SIZE, Max = 255};
877
878 {
879 tic_rect rect = {x, y-2, Size, 5};
880
881 if(checkMousePos(sprite->studio, &rect))
882 {
883 setCursor(sprite->studio, tic_cursor_hand);
884
885 if(checkMouseDown(sprite->studio, &rect, tic_mouse_left))
886 {
887 s32 mx = tic_api_mouse(tic).x - x;
888 *value = mx * Max / (Size-1);
889 }
890 }
891
892 tic_api_rect(tic, x, y+1, Size, 1, tic_color_black);
893 tic_api_rect(tic, x, y, Size, 1, tic_color_white);
894
895 {
896 s32 offset = x + *value * (Size-1) / Max - 2;
897 drawBitIcon(sprite->studio, tic_icon_pos, offset, y-1, tic_color_black);
898 drawBitIcon(sprite->studio, tic_icon_pos, offset, y-2, tic_color_white);
899 }
900 }
901
902 {
903 tic_rect rect = {x - 4, y - 1, 2, 3};
904
905 bool down = false;
906 if(checkMousePos(sprite->studio, &rect))
907 {
908 setCursor(sprite->studio, tic_cursor_hand);
909
910 if(checkMouseDown(sprite->studio, &rect, tic_mouse_left))
911 down = true;
912
913 if(checkMouseClick(sprite->studio, &rect, tic_mouse_left))
914 (*value)--;
915 }
916
917 if(down)
918 {
919 drawBitIcon(sprite->studio, tic_icon_tinyleft, rect.x-1, rect.y, tic_color_white);
920 }
921 else
922 {
923 drawBitIcon(sprite->studio, tic_icon_tinyleft, rect.x-1, rect.y, tic_color_black);
924 drawBitIcon(sprite->studio, tic_icon_tinyleft, rect.x-1, rect.y-1, tic_color_white);
925 }
926 }
927
928 {
929 tic_rect rect = {x + Size + 2, y - 1, 2, 3};
930
931 bool down = false;
932 if(checkMousePos(sprite->studio, &rect))
933 {
934 setCursor(sprite->studio, tic_cursor_hand);
935
936 if(checkMouseDown(sprite->studio, &rect, tic_mouse_left))
937 down = true;
938
939 if(checkMouseClick(sprite->studio, &rect, tic_mouse_left))
940 (*value)++;
941 }
942
943 if(down)
944 {
945 drawBitIcon(sprite->studio, tic_icon_tinyright, rect.x-1, rect.y, tic_color_white);
946 }
947 else
948 {
949 drawBitIcon(sprite->studio, tic_icon_tinyright, rect.x-1, rect.y, tic_color_black);
950 drawBitIcon(sprite->studio, tic_icon_tinyright, rect.x-1, rect.y-1, tic_color_white);
951 }
952 }
953}
954
955static void pasteColor(Sprite* sprite)
956{
957 bool ovr = sprite->palette.vbank1;
958 if(!fromClipboard(&getBankPalette(sprite->studio, ovr)->colors[sprite->color], sizeof(tic_rgb), false, true, false))
959 fromClipboard(getBankPalette(sprite->studio, ovr)->data, sizeof(tic_palette), false, true, false);
960}
961
962static void drawRGBTools(Sprite* sprite, s32 x, s32 y)
963{
964 {
965 enum{Size = 5};
966
967 tic_rect rect = {x, y, Size, Size};
968
969 bool over = false;
970 bool down = false;
971
972 if(checkMousePos(sprite->studio, &rect))
973 {
974 setCursor(sprite->studio, tic_cursor_hand);
975
976 showTooltip(sprite->studio, "COPY PALETTE");
977 over = true;
978
979 if(checkMouseDown(sprite->studio, &rect, tic_mouse_left))
980 down = true;
981
982 if(checkMouseClick(sprite->studio, &rect, tic_mouse_left))
983 toClipboard(getBankPalette(sprite->studio, sprite->palette.vbank1)->data, sizeof(tic_palette), false);
984 }
985
986 if(down)
987 {
988 drawBitIcon(sprite->studio, tic_icon_copy, rect.x-1, rect.y, tic_color_light_grey);
989 }
990 else
991 {
992 drawBitIcon(sprite->studio, tic_icon_copy, rect.x-1, rect.y, tic_color_black);
993 drawBitIcon(sprite->studio, tic_icon_copy, rect.x-1, rect.y-1, (over ? tic_color_light_grey : tic_color_white));
994 }
995 }
996
997 {
998 enum{Size = 5};
999
1000 tic_rect rect = {x, y + 8, Size, Size};
1001 bool over = false;
1002 bool down = false;
1003
1004 if(checkMousePos(sprite->studio, &rect))
1005 {
1006 setCursor(sprite->studio, tic_cursor_hand);
1007
1008 showTooltip(sprite->studio, "PASTE PALETTE");
1009 over = true;
1010
1011 if(checkMouseDown(sprite->studio, &rect, tic_mouse_left))
1012 down = true;
1013
1014 if(checkMouseClick(sprite->studio, &rect, tic_mouse_left))
1015 {
1016 pasteColor(sprite);
1017 }
1018 }
1019
1020 if(down)
1021 {
1022 drawBitIcon(sprite->studio, tic_icon_paste, rect.x-1, rect.y, tic_color_light_grey);
1023 }
1024 else
1025 {
1026 drawBitIcon(sprite->studio, tic_icon_paste, rect.x-1, rect.y, tic_color_black);
1027 drawBitIcon(sprite->studio, tic_icon_paste, rect.x-1, rect.y-1, (over ? tic_color_light_grey : tic_color_white));
1028 }
1029 }
1030}
1031
1032static void drawRGBSliders(Sprite* sprite, s32 x, s32 y)
1033{
1034 tic_mem* tic = sprite->tic;
1035
1036 enum
1037 {
1038 Cols = BITS_IN_BYTE / TIC_PALETTE_BPP,
1039 Rows = sizeof(tic_rgb),
1040 Width = TIC_FONT_WIDTH + 1,
1041 Height = TIC_FONT_HEIGHT + 1
1042 };
1043
1044 u8* data = &getBankPalette(sprite->studio, sprite->palette.vbank1)->data[sprite->color * Rows];
1045
1046 {
1047 tic_rect rect = {x - 20, y - 3, TIC_FONT_WIDTH * Cols + 1, TIC_FONT_HEIGHT * Rows + 1};
1048
1049 if(checkMousePos(sprite->studio, &rect))
1050 {
1051 setCursor(sprite->studio, tic_cursor_hand);
1052
1053 if(checkMouseDown(sprite->studio, &rect, tic_mouse_left))
1054 {
1055 s32 mx = tic_api_mouse(tic).x - rect.x;
1056 s32 my = tic_api_mouse(tic).y - rect.y;
1057
1058 sprite->palette.focus = mx / Width + my / Height * Cols;
1059 }
1060 }
1061
1062 bool hasFocus = sprite->palette.focus >= 0;
1063 if(hasFocus)
1064 {
1065 drawPanelBorder(sprite->tic, rect.x, rect.y, rect.w, rect.h);
1066 tic_api_rect(sprite->tic, rect.x, rect.y, rect.w, rect.h, tic_color_black);
1067 }
1068
1069 for(s32 i = 0; i < Rows; i++)
1070 {
1071 char buf[sizeof "FF"];
1072 sprintf(buf, "%02X", data[i]);
1073 tic_api_print(tic, buf, rect.x + 1, rect.y + i * TIC_FONT_HEIGHT + 1,
1074 hasFocus ? tic_color_grey : tic_color_light_grey, true, 1, false);
1075 }
1076
1077 if(hasFocus)
1078 {
1079 s32 col = sprite->palette.focus % Cols;
1080 s32 row = sprite->palette.focus / Cols;
1081 s32 x = rect.x + col * TIC_FONT_WIDTH;
1082 s32 y = rect.y + row * TIC_FONT_HEIGHT;
1083 tic_api_rect(sprite->tic, x, y, Width, Height, tic_color_red);
1084
1085 {
1086 char buf[sizeof "FF"];
1087 sprintf(buf, "%02X", data[row]);
1088 tic_api_print(tic, (char[]){buf[col], '\0'}, x + 1, y + 1, tic_color_black, true, 1, false);
1089 }
1090 }
1091 }
1092
1093 for(s32 i = 0; i < Rows; i++)
1094 drawRGBSlider(sprite, x, y + TIC_FONT_HEIGHT * i, &data[i]);
1095
1096 drawRGBTools(sprite, x + 74, y);
1097}
1098
1099static tic_palette_dimensions getPaletteDimensions(Sprite* sprite)
1100{
1101
1102 tic_bpp bpp = sprite->blit.mode;
1103
1104 s32 cols = bpp == tic_bpp_4 ? PALETTE_COLS : bpp == tic_bpp_2 ? 4 : 2;
1105 s32 rows = bpp == tic_bpp_4 ? PALETTE_ROWS : 1;
1106
1107 s32 cell_w = (PALETTE_COLS / cols) * PALETTE_CELL_SIZE;
1108 s32 cell_h = (PALETTE_ROWS / rows) * PALETTE_CELL_SIZE;
1109
1110 return (tic_palette_dimensions){cell_w, cell_h, cols, rows, cols*rows};
1111}
1112
1113static void drawPaletteVBank1(Sprite* sprite, s32 x, s32 y)
1114{
1115 tic_mem* tic = sprite->tic;
1116 tic_rect rect = {x, y, PALETTE_WIDTH-1, PALETTE_HEIGHT-1};
1117 tic_palette_dimensions palette = getPaletteDimensions(sprite);
1118
1119 if(checkMousePos(sprite->studio, &rect))
1120 {
1121 setCursor(sprite->studio, tic_cursor_hand);
1122
1123 s32 mx = tic_api_mouse(tic).x - x;
1124 s32 my = tic_api_mouse(tic).y - y;
1125
1126 mx /= palette.cell_w;
1127 my /= palette.cell_h;
1128
1129 s32 index = mx + my * palette.cols;
1130
1131 SHOW_TOOLTIP(sprite->studio, "color [%02i]", index);
1132
1133 bool left = checkMouseDown(sprite->studio, &rect, tic_mouse_left);
1134 bool right = checkMouseDown(sprite->studio, &rect, tic_mouse_right);
1135
1136 if(left || right)
1137 {
1138 if(left) sprite->color = index;
1139 if(right) sprite->color2 = index;
1140 }
1141 }
1142
1143 enum {Gap = 1};
1144
1145 drawPanelBorder(tic, x - Gap, y - Gap, PaletteW + Gap, PaletteH + Gap);
1146
1147 for(s32 row = 0, i = 0; row < palette.rows; row++)
1148 for(s32 col = 0; col < palette.cols; col++)
1149 {
1150 tic_api_rectb(tic, x + col * palette.cell_w - Gap, y + row * palette.cell_h - Gap,
1151 palette.cell_w + Gap, palette.cell_h + Gap, tic_color_black);
1152 }
1153
1154 {
1155 s32 offsetX = x + (sprite->color % PALETTE_COLS) * palette.cell_w;
1156 s32 offsetY = y + (sprite->color / PALETTE_COLS) * palette.cell_h;
1157 tic_api_rectb(tic, offsetX - 1, offsetY - 1, palette.cell_w + 1, palette.cell_h + 1, tic_color_white);
1158 }
1159
1160 {
1161 s32 offsetX = x + (sprite->color2 % PALETTE_COLS) * palette.cell_w;
1162 s32 offsetY = y + (sprite->color2 / PALETTE_COLS) * palette.cell_h;
1163
1164 for(u8 i=0; i<palette.cell_w+1;i+=2) {
1165 tic_api_pix(tic, offsetX+i-1, offsetY-1, tic_color_white, false);
1166 tic_api_pix(tic, offsetX+i-1, offsetY + palette.cell_h-1, tic_color_white, false);
1167 }
1168
1169 for(u8 i=0; i<palette.cell_h+1;i+=2) {
1170 tic_api_pix(tic, offsetX-1, offsetY+i-1, tic_color_white, false);
1171 tic_api_pix(tic, offsetX+palette.cell_w-1, offsetY + i-1, tic_color_white, false);
1172 }
1173 }
1174
1175 if(sprite->advanced)
1176 {
1177 tic_rect rect = {x - 22, y + 1, 19, 5};
1178
1179 bool down = false;
1180 bool over = false;
1181 if(checkMousePos(sprite->studio, &rect))
1182 {
1183 setCursor(sprite->studio, tic_cursor_hand);
1184 over = true;
1185
1186 showTooltip(sprite->studio, "VBANK0 PALETTE");
1187
1188 if(checkMouseDown(sprite->studio, &rect, tic_mouse_left))
1189 down = true;
1190
1191 if(checkMouseClick(sprite->studio, &rect, tic_mouse_left))
1192 sprite->palette.vbank1 = false;
1193 }
1194
1195 {
1196 static const char* Label = "bank0";
1197 if(!sprite->palette.vbank1)
1198 tic_api_print(tic, Label, rect.x, rect.y + 1, tic_color_black, false, 1, true);
1199
1200 tic_api_print(tic, Label, rect.x, rect.y, sprite->palette.vbank1 ? tic_color_dark_grey : tic_color_white, false, 1, true);
1201 }
1202 }
1203
1204 if(sprite->advanced)
1205 {
1206 tic_rect rect = {x - 22, y + 9, 19, 5};
1207
1208 bool down = false;
1209 bool over = false;
1210 if(checkMousePos(sprite->studio, &rect))
1211 {
1212 setCursor(sprite->studio, tic_cursor_hand);
1213 over = true;
1214
1215 showTooltip(sprite->studio, "VBANK1 PALETTE");
1216
1217 if(checkMouseDown(sprite->studio, &rect, tic_mouse_left))
1218 down = true;
1219
1220 if(checkMouseClick(sprite->studio, &rect, tic_mouse_left))
1221 sprite->palette.vbank1 = true;
1222 }
1223
1224 {
1225 static const char* Label = "bank1";
1226 if(sprite->palette.vbank1)
1227 tic_api_print(tic, Label, rect.x, rect.y + 1, tic_color_black, false, 1, true);
1228
1229 tic_api_print(tic, Label, rect.x, rect.y, sprite->palette.vbank1 ? tic_color_white : tic_color_dark_grey, false, 1, true);
1230 }
1231 }
1232
1233 if(sprite->advanced)
1234 {
1235 tic_rect rect = {x + PALETTE_WIDTH + 3, y + (PALETTE_HEIGHT-8)/2-1, 8, 8};
1236
1237 bool down = false;
1238 bool over = false;
1239 if(checkMousePos(sprite->studio, &rect))
1240 {
1241 setCursor(sprite->studio, tic_cursor_hand);
1242 over = true;
1243
1244 showTooltip(sprite->studio, "EDIT PALETTE");
1245
1246 if(checkMouseDown(sprite->studio, &rect, tic_mouse_left))
1247 down = true;
1248
1249 if(checkMouseClick(sprite->studio, &rect, tic_mouse_left))
1250 {
1251 sprite->palette.edit = !sprite->palette.edit;
1252
1253 if(!sprite->palette.edit)
1254 sprite->palette.focus = -1;
1255 }
1256 }
1257
1258 if(sprite->palette.edit || down)
1259 {
1260 drawBitIcon(sprite->studio, tic_icon_rgb, rect.x, rect.y+1, (over ? tic_color_light_grey : tic_color_white));
1261 }
1262 else
1263 {
1264 drawBitIcon(sprite->studio, tic_icon_rgb, rect.x, rect.y+1, tic_color_black);
1265 drawBitIcon(sprite->studio, tic_icon_rgb, rect.x, rect.y, (over ? tic_color_light_grey : tic_color_white));
1266 }
1267 }
1268}
1269
1270static void drawPalette(Sprite* sprite, s32 x, s32 y)
1271{
1272 tic_mem* tic = sprite->tic;
1273 tic_palette_dimensions palette = getPaletteDimensions(sprite);
1274
1275 for(s32 row = 0, i = 0; row < palette.rows; row++)
1276 for(s32 col = 0; col < palette.cols; col++)
1277 tic_api_rect(tic, x + col * palette.cell_w, y + row * palette.cell_h, palette.cell_w-1, palette.cell_h-1, i++);
1278}
1279
1280static void selectSprite(Sprite* sprite, s32 x, s32 y)
1281{
1282 {
1283 s32 size = TIC_SPRITESHEET_SIZE - sprite->size;
1284 if(x < 0) x = 0;
1285 if(y < 0) y = 0;
1286 if(x > size) x = size;
1287 if(y > size) y = size;
1288 }
1289 sprite->x = x / TIC_SPRITESIZE;
1290 sprite->y = y / TIC_SPRITESIZE;
1291 updateIndex(sprite);
1292}
1293
1294static void updateSpriteSize(Sprite* sprite, s32 size)
1295{
1296 if(size != sprite->size)
1297 {
1298 sprite->size = size;
1299 selectSprite(sprite, sprite->x*TIC_SPRITESIZE, sprite->y*TIC_SPRITESIZE);
1300 }
1301}
1302
1303static void drawSheetVBank1(Sprite* sprite, s32 x, s32 y)
1304{
1305 tic_mem* tic = sprite->tic;
1306
1307 tic_rect rect = {x, y, TIC_SPRITESHEET_SIZE, TIC_SPRITESHEET_SIZE};
1308
1309 tic_api_rectb(tic, rect.x - 1, rect.y - 1, rect.w + 2, rect.h + 2, tic_color_white);
1310
1311 for(s32 i = 1; i < rect.h; i += 4)
1312 {
1313 if (sprite->blit.page > 0)
1314 {
1315 tic_api_pix(tic, rect.x-1, rect.y + i, tic_color_black, false);
1316 tic_api_pix(tic, rect.x-1, rect.y + i + 1, tic_color_black, false);
1317 }
1318
1319 if (sprite->blit.page < sprite->blit.pages - 1)
1320 {
1321 tic_api_pix(tic, rect.x+rect.w, rect.y + i, tic_color_black, false);
1322 tic_api_pix(tic, rect.x+rect.w, rect.y + i + 1, tic_color_black, false);
1323 }
1324 }
1325
1326 if(checkMousePos(sprite->studio, &rect))
1327 {
1328 setCursor(sprite->studio, tic_cursor_hand);
1329
1330 if(checkMouseDown(sprite->studio, &rect, tic_mouse_left))
1331 {
1332 s32 offset = (sprite->size - TIC_SPRITESIZE) / 2;
1333 selectSprite(sprite, tic_api_mouse(tic).x - x - offset, tic_api_mouse(tic).y - y - offset);
1334 }
1335 }
1336
1337 s32 bx = sprite->x*TIC_SPRITESIZE + x - 1;
1338 s32 by = sprite->y*TIC_SPRITESIZE + y - 1;
1339
1340 tic_api_rectb(tic, bx, by, sprite->size + 2, sprite->size + 2, tic_color_white);
1341}
1342
1343static void drawSheet(Sprite* sprite, s32 x, s32 y)
1344{
1345 tic_mem* tic = sprite->tic;
1346 tiles2ram(tic->ram, sprite->src);
1347
1348 tic_blit blit = sprite->blit;
1349 SCOPE(tic->ram->vram.blit.segment = TIC_DEFAULT_BLIT_MODE)
1350 {
1351 tic_point start =
1352 {
1353 x - blit.page * TIC_SPRITESHEET_SIZE + sprite->anim.pos.page,
1354 y - blit.bank * TIC_SPRITESHEET_SIZE + sprite->anim.pos.bank
1355 }, pos = start;
1356
1357 for(blit.bank = 0; blit.bank < TIC_SPRITE_BANKS; ++blit.bank, pos.y += TIC_SPRITESHEET_SIZE, pos.x = start.x)
1358 {
1359 for(blit.page = 0; blit.page < blit.pages; ++blit.page, pos.x += TIC_SPRITESHEET_SIZE)
1360 {
1361 tic->ram->vram.blit.segment = tic_blit_calc_segment(&blit);
1362 tic_api_spr(tic, 0, pos.x, pos.y, TIC_SPRITESHEET_COLS, TIC_SPRITESHEET_COLS, NULL, 0, 1, tic_no_flip, tic_no_rotate);
1363 }
1364 }
1365 }
1366}
1367
1368static void flipSpriteHorz(Sprite* sprite)
1369{
1370 tic_rect rect = getSpriteRect(sprite);
1371 s32 r = rect.x + rect.w/2;
1372 s32 b = rect.y + rect.h;
1373
1374 for(s32 y = rect.y; y < b; y++)
1375 for(s32 x = rect.x, i = rect.x + rect.w - 1; x < r; x++, i--)
1376 {
1377 u8 color = tic_tilesheet_getpix(&sprite->sheet, x, y);
1378 tic_tilesheet_setpix(&sprite->sheet, x, y, tic_tilesheet_getpix(&sprite->sheet, i, y));
1379 tic_tilesheet_setpix(&sprite->sheet, i, y, color);
1380 }
1381
1382 history_add(sprite->history);
1383}
1384
1385static void flipSpriteVert(Sprite* sprite)
1386{
1387 tic_rect rect = getSpriteRect(sprite);
1388 s32 r = rect.x + rect.w;
1389 s32 b = rect.y + rect.h/2;
1390
1391 for(s32 y = rect.y, i = rect.y + rect.h - 1; y < b; y++, i--)
1392 for(s32 x = rect.x; x < r; x++)
1393 {
1394 u8 color = tic_tilesheet_getpix(&sprite->sheet, x, y);
1395 tic_tilesheet_setpix(&sprite->sheet, x, y, tic_tilesheet_getpix(&sprite->sheet, x, i));
1396 tic_tilesheet_setpix(&sprite->sheet, x, i, color);
1397 }
1398
1399 history_add(sprite->history);
1400}
1401
1402static void rotateSprite(Sprite* sprite)
1403{
1404 const s32 Size = sprite->size;
1405 u8* buffer = (u8*)malloc(Size * Size);
1406
1407 if(buffer)
1408 {
1409 {
1410 tic_rect rect = getSpriteRect(sprite);
1411 s32 r = rect.x + rect.w;
1412 s32 b = rect.y + rect.h;
1413
1414 for(s32 y = rect.y, i = 0; y < b; y++)
1415 for(s32 x = rect.x; x < r; x++)
1416 buffer[i++] = tic_tilesheet_getpix(&sprite->sheet, x, y);
1417
1418 for(s32 y = rect.y, j = 0; y < b; y++, j++)
1419 for(s32 x = rect.x, i = 0; x < r; x++, i++)
1420 tic_tilesheet_setpix(&sprite->sheet, x, y, buffer[j + (Size-i-1)*Size]);
1421
1422 history_add(sprite->history);
1423 }
1424
1425 free(buffer);
1426 }
1427}
1428
1429static inline bool is4bpp(Sprite* sprite)
1430{
1431 return sprite->blit.mode == 4;
1432}
1433
1434static void deleteSprite(Sprite* sprite)
1435{
1436 tic_rect rect = getSpriteRect(sprite);
1437 s32 r = rect.x + rect.w;
1438 s32 b = rect.y + rect.h;
1439
1440 for(s32 y = rect.y; y < b; y++)
1441 for(s32 x = rect.x; x < r; x++)
1442 tic_tilesheet_setpix(&sprite->sheet, x, y, sprite->color2);
1443
1444 if(is4bpp(sprite))
1445 {
1446 u8* flags = getBankFlags(sprite->studio)->data;
1447 for(const s32* it = getSpriteIndexes(sprite); *it >= 0; ++it)
1448 flags[*it] = 0;
1449 }
1450
1451 clearCanvasSelection(sprite);
1452
1453 history_add(sprite->history);
1454}
1455
1456static void(* const SpriteToolsFunc[])(Sprite*) = {flipSpriteHorz, flipSpriteVert, rotateSprite, deleteSprite};
1457static void(* const CanvasToolsFunc[])(Sprite*) = {flipCanvasHorz, flipCanvasVert, rotateCanvas, deleteCanvas};
1458
1459static void drawSpriteTools(Sprite* sprite, s32 x, s32 y)
1460{
1461 static const u8 Icons[] = {tic_icon_fliphorz, tic_icon_flipvert, tic_icon_rotate, tic_icon_erase};
1462 static const char* Tooltips[] = {"FLIP HORZ [5]", "FLIP VERT [6]", "ROTATE [7]", "ERASE [8]"};
1463
1464 enum{Gap = TIC_SPRITESIZE + 3};
1465
1466 for(s32 i = 0; i < COUNT_OF(Icons); i++)
1467 {
1468 bool pushed = false;
1469 bool over = false;
1470
1471 tic_rect rect = {x + i * Gap, y, TIC_SPRITESIZE, TIC_SPRITESIZE};
1472
1473 if(checkMousePos(sprite->studio, &rect))
1474 {
1475 setCursor(sprite->studio, tic_cursor_hand);
1476
1477 over = true;
1478
1479 showTooltip(sprite->studio, Tooltips[i]);
1480
1481 if(checkMouseDown(sprite->studio, &rect, tic_mouse_left)) pushed = true;
1482
1483 if(checkMouseClick(sprite->studio, &rect, tic_mouse_left))
1484 {
1485 if(hasCanvasSelection(sprite))
1486 {
1487 CanvasToolsFunc[i](sprite);
1488 }
1489 else
1490 {
1491 SpriteToolsFunc[i](sprite);
1492 clearCanvasSelection(sprite);
1493 }
1494 }
1495 }
1496
1497 if(pushed)
1498 {
1499 drawBitIcon(sprite->studio, Icons[i], rect.x, y + 1, (over ? tic_color_light_grey : tic_color_white));
1500 }
1501 else
1502 {
1503 drawBitIcon(sprite->studio, Icons[i], rect.x, y+1, tic_color_black);
1504 drawBitIcon(sprite->studio, Icons[i], rect.x, y, (over ? tic_color_light_grey : tic_color_white));
1505 }
1506 }
1507}
1508
1509static void drawTools(Sprite* sprite, s32 x, s32 y)
1510{
1511 enum{Gap = TIC_SPRITESIZE + 3};
1512
1513 static const u8 Icons[] = {tic_icon_bigpen, tic_icon_bigpicker, tic_icon_bigselect, tic_icon_bigfill};
1514
1515 for(s32 i = 0; i < COUNT_OF(Icons); i++)
1516 {
1517 tic_rect rect = {x + i * Gap, y, TIC_SPRITESIZE, TIC_SPRITESIZE};
1518
1519 bool over = false;
1520 if(checkMousePos(sprite->studio, &rect))
1521 {
1522 setCursor(sprite->studio, tic_cursor_hand);
1523 over = true;
1524
1525 static const char* Tooltips[] = {"BRUSH [1]", "COLOR PICKER [2]", "SELECT [3]", "FILL [4]"};
1526
1527 showTooltip(sprite->studio, Tooltips[i]);
1528
1529 if(checkMouseClick(sprite->studio, &rect, tic_mouse_left))
1530 {
1531 sprite->mode = i;
1532
1533 clearCanvasSelection(sprite);
1534 }
1535 }
1536
1537 bool pushed = i == sprite->mode;
1538
1539 if(pushed)
1540 {
1541 drawBitIcon(sprite->studio, tic_icon_down, rect.x, y - 5, tic_color_black);
1542 drawBitIcon(sprite->studio, tic_icon_down, rect.x, y - 6, tic_color_white);
1543
1544 drawBitIcon(sprite->studio, Icons[i], rect.x, y + 1, (over ? tic_color_light_grey : tic_color_white));
1545 }
1546 else
1547 {
1548 drawBitIcon(sprite->studio, Icons[i], rect.x, y+1, tic_color_black);
1549 drawBitIcon(sprite->studio, Icons[i], rect.x, y, (over ? tic_color_light_grey : tic_color_white));
1550 }
1551 }
1552
1553 drawSpriteTools(sprite, x + COUNT_OF(Icons) * Gap + 1, y);
1554}
1555
1556static inline s32 getClipboardSpritesSize(Sprite* sprite)
1557{
1558 return sprite->size * sprite->size * TIC_PALETTE_BPP / BITS_IN_BYTE;
1559}
1560
1561static inline s32 getClipboardFlagsSize(Sprite* sprite)
1562{
1563 return is4bpp(sprite) ? sprite->size * sprite->size / (TIC_SPRITESIZE * TIC_SPRITESIZE) : 0;
1564}
1565
1566static inline s32 getClipboardSize(Sprite* sprite)
1567{
1568 return getClipboardSpritesSize(sprite) + getClipboardFlagsSize(sprite);
1569}
1570
1571static void copyToClipboard(Sprite* sprite)
1572{
1573 s32 size = getClipboardSize(sprite);
1574
1575 u8* buffer = malloc(size);
1576 SCOPE(free(buffer))
1577 {
1578 tic_rect rect = getSpriteRect(sprite);
1579 s32 r = rect.x + rect.w;
1580 s32 b = rect.y + rect.h;
1581
1582 for(s32 y = rect.y, i = 0; y < b; y++)
1583 for(s32 x = rect.x; x < r; x++)
1584 tic_tool_poke4(buffer, i++, tic_tilesheet_getpix(&sprite->sheet, x, y) & 0xf);
1585
1586 if(is4bpp(sprite))
1587 {
1588 u8* ptr = buffer + getClipboardSpritesSize(sprite);
1589
1590 const u8* flags = getBankFlags(sprite->studio)->data;
1591 for(const s32* it = getSpriteIndexes(sprite); *it >= 0; ++it)
1592 *ptr++ = flags[*it];
1593 }
1594
1595 toClipboard(buffer, size, true);
1596 }
1597}
1598
1599static void cutToClipboard(Sprite* sprite)
1600{
1601 copyToClipboard(sprite);
1602 deleteSprite(sprite);
1603}
1604
1605static void copyFromClipboard(Sprite* sprite)
1606{
1607 s32 size = getClipboardSize(sprite);
1608
1609 u8* buffer = malloc(size);
1610 SCOPE(free(buffer))
1611 {
1612 if(fromClipboard(buffer, size, true, false, true))
1613 {
1614 tic_rect rect = getSpriteRect(sprite);
1615 s32 r = rect.x + rect.w;
1616 s32 b = rect.y + rect.h;
1617
1618 for(s32 y = rect.y, i = 0; y < b; y++)
1619 for(s32 x = rect.x; x < r; x++)
1620 tic_tilesheet_setpix(&sprite->sheet, x, y, tic_tool_peek4(buffer, i++));
1621
1622 if(is4bpp(sprite))
1623 {
1624 const u8* ptr = buffer + getClipboardSpritesSize(sprite);
1625 u8* flags = getBankFlags(sprite->studio)->data;
1626
1627 for(const s32* it = getSpriteIndexes(sprite); *it >= 0; ++it)
1628 flags[*it] = *ptr++;
1629 }
1630
1631 history_add(sprite->history);
1632 }
1633 }
1634}
1635
1636static void upSprite(Sprite* sprite)
1637{
1638 if (sprite->y > 0) sprite->y--;
1639 updateIndex(sprite);
1640}
1641
1642static void downSprite(Sprite* sprite)
1643{
1644 if ((sprite->y + sprite->size/TIC_SPRITESIZE) < TIC_SPRITESHEET_COLS) sprite->y++;
1645 updateIndex(sprite);
1646}
1647
1648static void leftSprite(Sprite* sprite)
1649{
1650 if (sprite->x > 0) sprite->x--;
1651 updateIndex(sprite);
1652}
1653
1654static void rightSprite(Sprite* sprite)
1655{
1656 if ((sprite->x + sprite->size/TIC_SPRITESIZE) < TIC_SPRITESHEET_COLS) sprite->x++;
1657 updateIndex(sprite);
1658}
1659
1660static void undo(Sprite* sprite)
1661{
1662 history_undo(sprite->history);
1663}
1664
1665static void redo(Sprite* sprite)
1666{
1667 history_redo(sprite->history);
1668}
1669
1670static void switchBanks(Sprite* sprite)
1671{
1672 if(isIdle(sprite))
1673 {
1674 s32 bank = (sprite->blit.bank + 1) % TIC_SPRITE_BANKS;
1675 Anim* anim = sprite->anim.bank.items;
1676 anim->start = (bank - sprite->blit.bank) * TIC_SPRITESHEET_SIZE;
1677 sprite->anim.movie = resetMovie(&sprite->anim.bank);
1678
1679 sprite->blit.bank = bank;
1680
1681 updateIndex(sprite);
1682 initTileSheet(sprite);
1683 }
1684}
1685
1686static void drawTab(Sprite* sprite, s32 x, s32 y, s32 w, s32 h, u8 icon, bool active, bool over)
1687{
1688 tic_color tab_color = active ? tic_color_white : over ? tic_color_light_grey : tic_color_dark_grey;
1689 tic_color label_color = active ? tic_color_dark_grey : tic_color_grey;
1690
1691 tic_api_rect(sprite->tic, x+1, y, w-1, h, tab_color);
1692 tic_api_line(sprite->tic, x, y+1, x, y+h-2, tab_color);
1693
1694 if (active)
1695 {
1696 tic_api_line(sprite->tic, x+1, y + h, x + w-1, y + h, tic_color_black);
1697 tic_api_pix(sprite->tic, x, y-1 + h, label_color, false);
1698 }
1699
1700 drawBitIcon(sprite->studio, icon, x + 1, y, label_color);
1701}
1702
1703static void drawBankTabs(Sprite* sprite, s32 x, s32 y)
1704{
1705 tic_mem* tic = sprite->tic;
1706
1707 if(hasCanvasSelection(sprite)) return;
1708
1709 enum {Banks = 2, SizeY = 7, SizeX = 9};
1710
1711 static const u8 Icons[] = {tic_icon_tiles, tic_icon_sprites};
1712 static const char* tooltips[] = {"TILES [tab]", "SPRITES [tab]"};
1713
1714 for(s32 i = 0; i < Banks; i++)
1715 {
1716 bool current = i == sprite->blit.bank;
1717
1718 tic_rect rect = {x - SizeX, y + (SizeY + 1) * i, SizeX, SizeY};
1719
1720 bool over = false;
1721 if(checkMousePos(sprite->studio, &rect))
1722 {
1723 setCursor(sprite->studio, tic_cursor_hand);
1724 over = true;
1725
1726 showTooltip(sprite->studio, tooltips[i]);
1727
1728 if(checkMouseClick(sprite->studio, &rect, tic_mouse_left))
1729 {
1730 if (!current)
1731 {
1732 switchBanks(sprite);
1733 }
1734 }
1735 }
1736
1737 drawTab(sprite, rect.x, rect.y, SizeX, SizeY, Icons[i], current, over);
1738 }
1739}
1740
1741static void updateBrushSize(Sprite* sprite, s32 val)
1742{
1743 sprite->brushSize = (sprite->brushSize + val + (BRUSH_SIZES - 1)) % BRUSH_SIZES + 1;
1744}
1745
1746static void updateColorIndex(Sprite* sprite, s32 val)
1747{
1748 s32 colors = 1 << sprite->blit.mode;
1749 sprite->color = (sprite->color + val + colors) % colors;
1750}
1751
1752static void processKeyboard(Sprite* sprite)
1753{
1754 tic_mem* tic = sprite->tic;
1755
1756 switch(getClipboardEvent(sprite->studio))
1757 {
1758 case TIC_CLIPBOARD_CUT: cutToClipboard(sprite); break;
1759 case TIC_CLIPBOARD_COPY: copyToClipboard(sprite); break;
1760 case TIC_CLIPBOARD_PASTE: copyFromClipboard(sprite); break;
1761 default: break;
1762 }
1763
1764 if(tic_api_key(tic, tic_key_alt))
1765 return;
1766
1767 if(sprite->palette.edit)
1768 {
1769 if(sprite->palette.focus >= 0)
1770 {
1771 enum{Cols = BITS_IN_BYTE / TIC_PALETTE_BPP, Rows = sizeof(tic_rgb)};
1772 s32 col = sprite->palette.focus % Cols;
1773 s32 row = sprite->palette.focus / Cols;
1774
1775 if(keyWasPressed(sprite->studio, tic_key_up)) --row;
1776 else if(keyWasPressed(sprite->studio, tic_key_down)) ++row;
1777 else if(keyWasPressed(sprite->studio, tic_key_left)) --col;
1778 else if(keyWasPressed(sprite->studio, tic_key_right)) ++col;
1779 else
1780 {
1781 char sym = getKeyboardText(sprite->studio);
1782
1783 if(isxdigit(sym))
1784 {
1785 u8* data = &getBankPalette(sprite->studio, sprite->palette.vbank1)->data[sprite->color * Rows + row];
1786 char buf[sizeof "FF"];
1787 sprintf(buf, "%02X", *data);
1788 buf[col] = toupper(sym);
1789 *data = (u8)strtol(buf, NULL, 16);
1790 ++col;
1791 }
1792 }
1793
1794 sprite->palette.focus = (col + row * Cols + Cols * Rows) % (Cols * Rows);
1795 }
1796 }
1797 else
1798 {
1799 bool ctrl = tic_api_key(tic, tic_key_ctrl);
1800
1801 if(ctrl)
1802 {
1803 if(keyWasPressed(sprite->studio, tic_key_z)) undo(sprite);
1804 else if(keyWasPressed(sprite->studio, tic_key_y)) redo(sprite);
1805
1806 else if(keyWasPressed(sprite->studio, tic_key_left)) leftViewport(sprite);
1807 else if(keyWasPressed(sprite->studio, tic_key_right)) rightViewport(sprite);
1808
1809 else if(keyWasPressed(sprite->studio, tic_key_tab))
1810 switchBitMode(sprite, sprite->blit.mode == tic_bpp_4
1811 ? tic_bpp_2
1812 : sprite->blit.mode == tic_bpp_2
1813 ? tic_bpp_1
1814 : tic_bpp_4);
1815 }
1816 else
1817 {
1818 if(hasCanvasSelection(sprite))
1819 {
1820 if(!sprite->select.drag)
1821 {
1822 if(keyWasPressed(sprite->studio, tic_key_up)) upCanvas(sprite);
1823 else if(keyWasPressed(sprite->studio, tic_key_down)) downCanvas(sprite);
1824 else if(keyWasPressed(sprite->studio, tic_key_left)) leftCanvas(sprite);
1825 else if(keyWasPressed(sprite->studio, tic_key_right)) rightCanvas(sprite);
1826 else if(keyWasPressed(sprite->studio, tic_key_delete)) deleteCanvas(sprite);
1827 }
1828 }
1829 else
1830 {
1831 if(keyWasPressed(sprite->studio, tic_key_up)) upSprite(sprite);
1832 else if(keyWasPressed(sprite->studio, tic_key_down)) downSprite(sprite);
1833 else if(keyWasPressed(sprite->studio, tic_key_left)) leftSprite(sprite);
1834 else if(keyWasPressed(sprite->studio, tic_key_right)) rightSprite(sprite);
1835 else if(keyWasPressed(sprite->studio, tic_key_delete)) deleteSprite(sprite);
1836 else if(keyWasPressed(sprite->studio, tic_key_tab)) switchBanks(sprite);
1837
1838 if(!sprite->palette.edit)
1839 {
1840
1841 if(keyWasPressed(sprite->studio, tic_key_1)) sprite->mode = SPRITE_DRAW_MODE;
1842 else if(keyWasPressed(sprite->studio, tic_key_2)) sprite->mode = SPRITE_PICK_MODE;
1843 else if(keyWasPressed(sprite->studio, tic_key_3)) sprite->mode = SPRITE_SELECT_MODE;
1844 else if(keyWasPressed(sprite->studio, tic_key_4)) sprite->mode = SPRITE_FILL_MODE;
1845
1846 else if(keyWasPressed(sprite->studio, tic_key_5)) flipSpriteHorz(sprite);
1847 else if(keyWasPressed(sprite->studio, tic_key_6)) flipSpriteVert(sprite);
1848 else if(keyWasPressed(sprite->studio, tic_key_7)) rotateSprite(sprite);
1849 else if(keyWasPressed(sprite->studio, tic_key_8)) deleteSprite(sprite);
1850
1851 if(sprite->mode == SPRITE_DRAW_MODE)
1852 {
1853 if(keyWasPressed(sprite->studio, tic_key_minus)) updateBrushSize(sprite, -1);
1854 else if(keyWasPressed(sprite->studio, tic_key_equals)) updateBrushSize(sprite, +1);
1855 else if(keyWasPressed(sprite->studio, tic_key_leftbracket)) updateColorIndex(sprite, -1);
1856 else if(keyWasPressed(sprite->studio, tic_key_rightbracket)) updateColorIndex(sprite, +1);
1857 }
1858 }
1859 }
1860 }
1861 }
1862}
1863
1864
1865static void drawSpriteToolbar(Sprite* sprite)
1866{
1867 tic_mem* tic = sprite->tic;
1868
1869 tic_api_rect(tic, 0, 0, TIC80_WIDTH, TOOLBAR_SIZE, tic_color_white);
1870
1871 // draw sprite size control
1872 {
1873 tic_rect rect = {TIC80_WIDTH - 58, 1, 23, 5};
1874
1875 if(checkMousePos(sprite->studio, &rect))
1876 {
1877 setCursor(sprite->studio, tic_cursor_hand);
1878
1879 showTooltip(sprite->studio, "CANVAS ZOOM");
1880
1881 if(checkMouseDown(sprite->studio, &rect, tic_mouse_left))
1882 {
1883 s32 mx = tic_api_mouse(tic).x - rect.x;
1884 mx /= 6;
1885
1886 s32 size = 1;
1887 while(mx--) size <<= 1;
1888
1889 updateSpriteSize(sprite, size * TIC_SPRITESIZE);
1890 }
1891 }
1892
1893 for(s32 i = 0; i < 4; i++)
1894 tic_api_rect(tic, rect.x + i*6, 1, 5, 5, tic_color_black);
1895
1896 tic_api_rect(tic, rect.x, 2, 23, 3, tic_color_black);
1897 tic_api_rect(tic, rect.x+1, 3, 21, 1, tic_color_white);
1898
1899 s32 size = sprite->size / TIC_SPRITESIZE, val = 0;
1900 while(size >>= 1) val++;
1901
1902 tic_api_rect(tic, rect.x + val*6, 1, 5, 5, tic_color_black);
1903 tic_api_rect(tic, rect.x+1 + val*6, 2, 3, 3, tic_color_white);
1904 }
1905
1906 {
1907 u8 nbPages = sprite->blit.pages;
1908
1909 if (nbPages > 1) {
1910 enum {SizeX = 7, SizeY = TOOLBAR_SIZE};
1911
1912 for(s32 page = 0; page < nbPages; page++)
1913 {
1914 bool active = page == sprite->blit.page;
1915
1916 tic_rect rect = {TIC80_WIDTH - 1 - 7*(nbPages-page), 0, 7, TOOLBAR_SIZE};
1917
1918 bool over = false;
1919 if(checkMousePos(sprite->studio, &rect))
1920 {
1921 setCursor(sprite->studio, tic_cursor_hand);
1922 over = true;
1923
1924 SHOW_TOOLTIP(sprite->studio, "PAGE %i", page + 1);
1925
1926 if(checkMouseClick(sprite->studio, &rect, tic_mouse_left))
1927 {
1928 selectViewportPage(sprite, page);
1929 }
1930 }
1931
1932 if (active) tic_api_rect(tic, rect.x, rect.y, rect.w, rect.h, tic_color_black);
1933 tic_api_print(tic, (char[]){'1' + page, '\0'}, rect.x + 2, rect.y + 1, active ? tic_color_white : tic_color_grey, false, 1, true);
1934 }
1935 }
1936 }
1937}
1938
1939static void scanline(tic_mem* tic, s32 row, void* data)
1940{
1941 Sprite* sprite = (Sprite*)data;
1942
1943 if(row == 0)
1944 memcpy(&tic->ram->vram.palette, getBankPalette(sprite->studio, sprite->palette.vbank1), sizeof(tic_palette));
1945}
1946
1947static void drawAdvancedButton(Sprite* sprite, s32 x, s32 y)
1948{
1949 tic_mem* tic = sprite->tic;
1950
1951 tic_rect rect = {x, y, 8, 5};
1952
1953 bool over = false;
1954 if(checkMousePos(sprite->studio, &rect))
1955 {
1956 setCursor(sprite->studio, tic_cursor_hand);
1957 over = true;
1958 showTooltip(sprite->studio, "ADVANCED MODE");
1959
1960 if(checkMouseClick(sprite->studio, &rect, tic_mouse_left))
1961 sprite->advanced = !sprite->advanced;
1962
1963 if(!sprite->advanced)
1964 {
1965 sprite->palette.edit = false;
1966 sprite->palette.focus = -1;
1967 }
1968 }
1969
1970 enum {Size = 3, Gap = 1};
1971
1972 tic_api_rect(tic, rect.x, rect.y, rect.w, rect.h, tic_color_black);
1973 tic_api_rect(tic, rect.x + Gap + (sprite->advanced ? Size : 0), rect.y + Gap, Size, Size, over ? tic_color_light_grey : tic_color_grey);
1974}
1975
1976static void tick(Sprite* sprite)
1977{
1978 tic_mem* tic = sprite->tic;
1979
1980 if (sprite->tickCounter == 0) sprite->previousMouse = tic_api_mouse(tic);
1981 processAnim(sprite->anim.movie, sprite);
1982
1983 // process scroll
1984 {
1985 tic80_input* input = &tic->ram->input;
1986
1987 if(input->mouse.scrolly)
1988 {
1989 s32 size = sprite->size;
1990 s32 delta = input->mouse.scrolly;
1991
1992 if(delta > 0)
1993 {
1994 if(size < (TIC_SPRITESIZE * TIC_SPRITESIZE)) size <<= 1;
1995 }
1996 else if(size > TIC_SPRITESIZE) size >>= 1;
1997
1998 updateSpriteSize(sprite, size);
1999 }
2000 }
2001
2002 processKeyboard(sprite);
2003
2004 drawSheet(sprite, SheetX, SheetY);
2005 drawCanvas(sprite, CanvasX, CanvasY);
2006 drawPalette(sprite, PaletteX, PaletteY);
2007
2008 VBANK(tic, 1)
2009 {
2010 tic_api_cls(tic, tic->ram->vram.vars.clear = tic_color_dark_blue);
2011
2012 static const tic_rect bg[] =
2013 {
2014 {0, ToolbarH, SheetX, CanvasY-ToolbarH},
2015 {0, CanvasY, CanvasX, CanvasH},
2016 {CanvasX + CanvasW, CanvasY, SheetX - (CanvasX + CanvasW), CanvasH},
2017
2018 {0, CanvasY + CanvasH, SheetX, PaletteY - CanvasY - CanvasH},
2019
2020 {0, PaletteY, PaletteX, PaletteH},
2021 {PaletteX + PaletteW, PaletteY, SheetX - PaletteX - PaletteW, PaletteH},
2022
2023 {0, PaletteY + PaletteH, SheetX, TIC80_HEIGHT - PaletteY - PaletteH},
2024 };
2025
2026 memcpy(tic->ram->vram.palette.data, getConfig(sprite->studio)->cart->bank0.palette.vbank0.data, sizeof(tic_palette));
2027
2028 for(const tic_rect* r = bg; r < bg + COUNT_OF(bg); r++)
2029 tic_api_rect(tic, r->x, r->y, r->w, r->h, tic_color_grey);
2030
2031 drawCanvasVBank1(sprite, 24, 20);
2032 drawMoveButtons(sprite);
2033
2034 if(sprite->advanced)
2035 {
2036 if(is4bpp(sprite))
2037 drawFlags(sprite, 24+64+7, 20+8);
2038
2039 drawBitMode(sprite, PaletteX, PaletteY + PaletteH + 2, PaletteW, 8);
2040 }
2041
2042 drawBankTabs(sprite, SheetX, 8);
2043
2044 sprite->palette.edit
2045 ? drawRGBSliders(sprite, 24, 91)
2046 : drawTools(sprite, 12, 96);
2047
2048 drawPaletteVBank1(sprite, 24, 112);
2049 drawSheetVBank1(sprite, TIC80_WIDTH - TIC_SPRITESHEET_SIZE - 1, 7);
2050 drawAdvancedButton(sprite, 4, 11);
2051
2052 drawSpriteToolbar(sprite);
2053 drawToolbar(sprite->studio, tic, false);
2054 }
2055
2056 sprite->previousMouse = tic_api_mouse(sprite->tic);
2057 sprite->tickCounter++;
2058}
2059
2060static void onStudioEvent(Sprite* sprite, StudioEvent event)
2061{
2062 switch(event)
2063 {
2064 case TIC_TOOLBAR_CUT: cutToClipboard(sprite); break;
2065 case TIC_TOOLBAR_COPY: copyToClipboard(sprite); break;
2066 case TIC_TOOLBAR_PASTE: copyFromClipboard(sprite); break;
2067 case TIC_TOOLBAR_UNDO: undo(sprite); break;
2068 case TIC_TOOLBAR_REDO: redo(sprite); break;
2069 }
2070}
2071
2072static void emptyDone(void* data) {}
2073
2074static void setIdle(void* data)
2075{
2076 Sprite* sprite = data;
2077 sprite->anim.movie = resetMovie(&sprite->anim.idle);
2078}
2079
2080static void freeAnim(Sprite* sprite)
2081{
2082 FREE(sprite->anim.bank.items);
2083 FREE(sprite->anim.page.items);
2084}
2085
2086void initSprite(Sprite* sprite, Studio* studio, tic_tiles* src)
2087{
2088 if(sprite->select.back == NULL) sprite->select.back = (u8*)malloc(CANVAS_SIZE*CANVAS_SIZE);
2089 if(sprite->select.front == NULL) sprite->select.front = (u8*)malloc(CANVAS_SIZE*CANVAS_SIZE);
2090 if(sprite->history) history_delete(sprite->history);
2091 freeAnim(sprite);
2092
2093 *sprite = (Sprite)
2094 {
2095 .studio = studio,
2096 .tic = getMemory(studio),
2097 .tick = tick,
2098 .tickCounter = 0,
2099 .src = src,
2100 .x = 1,
2101 .y = 0,
2102 .advanced = false,
2103 .blit = {0},
2104 .color = 2,
2105 .color2 = 0,
2106 .size = TIC_SPRITESIZE,
2107 .palette =
2108 {
2109 .edit = false,
2110 .focus = -1,
2111 },
2112 .brushSize = 1,
2113 .select =
2114 {
2115 .rect = {0,0,0,0},
2116 .start = {0,0},
2117 .drag = false,
2118 .back = sprite->select.back,
2119 .front = sprite->select.front,
2120 },
2121 .mode = SPRITE_DRAW_MODE,
2122 .history = history_create(src, TIC_SPRITES * sizeof(tic_tile)),
2123 .anim =
2124 {
2125 .idle = {.done = emptyDone,},
2126
2127 .bank = MOVIE_DEF(STUDIO_ANIM_TIME, setIdle,
2128 {
2129 {0, 0, STUDIO_ANIM_TIME, &sprite->anim.pos.bank, AnimEaseIn},
2130 }),
2131
2132 .page = MOVIE_DEF(STUDIO_ANIM_TIME, setIdle,
2133 {
2134 {0, 0, STUDIO_ANIM_TIME, &sprite->anim.pos.page, AnimEaseIn},
2135 }),
2136 },
2137 .event = onStudioEvent,
2138 .scanline = scanline,
2139 };
2140
2141 sprite->anim.movie = resetMovie(&sprite->anim.idle);
2142
2143 switchBitMode(sprite, TIC_DEFAULT_BIT_DEPTH);
2144}
2145
2146void freeSprite(Sprite* sprite)
2147{
2148 freeAnim(sprite);
2149 free(sprite->select.back);
2150 free(sprite->select.front);
2151 history_delete(sprite->history);
2152 free(sprite);
2153}
2154