Enable deleting a selection by pressing DEL on keyboard
Currently, if you press DELETE the entire sprite is removed. If you have a selection and press delete, everything is lost. This commit checks if you have a selection. If so, it only removes the selected area.
This commit is contained in:
		
							
								
								
									
										41
									
								
								src/sprite.c
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								src/sprite.c
									
									
									
									
									
								
							@@ -31,6 +31,11 @@
 | 
				
			|||||||
#define PALETTE_HEIGHT (PALETTE_ROWS * PALETTE_CELL_SIZE)
 | 
					#define PALETTE_HEIGHT (PALETTE_ROWS * PALETTE_CELL_SIZE)
 | 
				
			||||||
#define SHEET_COLS (TIC_SPRITESHEET_SIZE / TIC_SPRITESIZE)
 | 
					#define SHEET_COLS (TIC_SPRITESHEET_SIZE / TIC_SPRITESIZE)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void clearCanvasSelection(Sprite* sprite)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						SDL_memset(&sprite->select.rect, 0, sizeof(SDL_Rect));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static u8 getSheetPixel(Sprite* sprite, s32 x, s32 y)
 | 
					static u8 getSheetPixel(Sprite* sprite, s32 x, s32 y)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	return getSpritePixel(sprite->tic->cart.bank.tiles.data, x, sprite->index >= TIC_BANK_SPRITES ? y + TIC_SPRITESHEET_SIZE: y);
 | 
						return getSpritePixel(sprite->tic->cart.bank.tiles.data, x, sprite->index >= TIC_BANK_SPRITES ? y + TIC_SPRITESHEET_SIZE: y);
 | 
				
			||||||
@@ -446,6 +451,24 @@ static void rightCanvas(Sprite* sprite)
 | 
				
			|||||||
	pasteSelection(sprite);
 | 
						pasteSelection(sprite);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void deleteCanvas(Sprite* sprite)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						SDL_Rect* rect = &sprite->select.rect;
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						s32 left = getIndexPosX(sprite) + rect->x;
 | 
				
			||||||
 | 
						s32 top = getIndexPosY(sprite) + rect->y;
 | 
				
			||||||
 | 
						s32 right = left + rect->w;
 | 
				
			||||||
 | 
						s32 bottom = top + rect->h;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for(s32 pixel_y = top; pixel_y < bottom; pixel_y++)
 | 
				
			||||||
 | 
							for(s32 pixel_x = left; pixel_x < right; pixel_x++)
 | 
				
			||||||
 | 
								setSheetPixel(sprite, pixel_x, pixel_y, sprite->color2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						clearCanvasSelection(sprite);
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						history_add(sprite->history);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void drawMoveButtons(Sprite* sprite)
 | 
					static void drawMoveButtons(Sprite* sprite)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if(hasCanvasSelection(sprite))
 | 
						if(hasCanvasSelection(sprite))
 | 
				
			||||||
@@ -502,9 +525,10 @@ static void drawMoveButtons(Sprite* sprite)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		static void(* const Func[])(Sprite*) = {upCanvas, downCanvas, leftCanvas, rightCanvas};
 | 
							static void(* const Func[])(Sprite*) = {upCanvas, downCanvas, leftCanvas, rightCanvas};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							bool down = false;
 | 
				
			||||||
		for(s32 i = 0; i < sizeof Icons / 8; i++)
 | 
							for(s32 i = 0; i < sizeof Icons / 8; i++)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			bool down = false;
 | 
								down = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if(checkMousePos(&Rects[i]))
 | 
								if(checkMousePos(&Rects[i]))
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
@@ -882,11 +906,6 @@ static void drawPaletteOvr(Sprite* sprite, s32 x, s32 y)
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void clearCanvasSelection(Sprite* sprite)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	SDL_memset(&sprite->select.rect, 0, sizeof(SDL_Rect));
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static void selectSprite(Sprite* sprite, s32 x, s32 y)
 | 
					static void selectSprite(Sprite* sprite, s32 x, s32 y)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
@@ -1067,23 +1086,23 @@ static void drawSpriteTools(Sprite* sprite, s32 x, s32 y)
 | 
				
			|||||||
		0b00111110,
 | 
							0b00111110,
 | 
				
			||||||
		0b00000000,
 | 
							0b00000000,
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
						static const char* Tooltips[] = {"FLIP HORZ [5]", "FLIP VERT [6]", "ROTATE [7]", "ERASE [8]"};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	enum{Gap = TIC_SPRITESIZE + 3};
 | 
						enum{Gap = TIC_SPRITESIZE + 3};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for(s32 i = 0; i < COUNT_OF(Icons)/BITS_IN_BYTE; i++)
 | 
						for(s32 i = 0; i < COUNT_OF(Icons)/BITS_IN_BYTE; i++)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		SDL_Rect rect = {x + i * Gap, y, TIC_SPRITESIZE, TIC_SPRITESIZE};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		bool pushed = false;
 | 
							bool pushed = false;
 | 
				
			||||||
		bool over = false;
 | 
							bool over = false;
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
							SDL_Rect rect = {x + i * Gap, y, TIC_SPRITESIZE, TIC_SPRITESIZE};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if(checkMousePos(&rect))
 | 
							if(checkMousePos(&rect))
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			setCursor(SDL_SYSTEM_CURSOR_HAND);
 | 
								setCursor(SDL_SYSTEM_CURSOR_HAND);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			over = true;
 | 
								over = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			static const char* Tooltips[] = {"FLIP HORZ [5]", "FLIP VERT [6]", "ROTATE [7]", "ERASE [8]"};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			showTooltip(Tooltips[i]);
 | 
								showTooltip(Tooltips[i]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if(checkMouseDown(&rect, SDL_BUTTON_LEFT)) pushed = true;
 | 
								if(checkMouseDown(&rect, SDL_BUTTON_LEFT)) pushed = true;
 | 
				
			||||||
@@ -1327,7 +1346,7 @@ static void processKeydown(Sprite* sprite, SDL_Keycode keycode)
 | 
				
			|||||||
			case SDLK_DOWN: 	downCanvas(sprite); break;
 | 
								case SDLK_DOWN: 	downCanvas(sprite); break;
 | 
				
			||||||
			case SDLK_LEFT: 	leftCanvas(sprite); break;
 | 
								case SDLK_LEFT: 	leftCanvas(sprite); break;
 | 
				
			||||||
			case SDLK_RIGHT: 	rightCanvas(sprite); break;
 | 
								case SDLK_RIGHT: 	rightCanvas(sprite); break;
 | 
				
			||||||
			case SDLK_DELETE:	deleteSprite(sprite); break;
 | 
								case SDLK_DELETE:	deleteCanvas(sprite); break;
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user