aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-11-24 01:37:24 +0100
committerkdx <kikoodx@paranoici.org>2023-11-24 01:37:24 +0100
commit447c82adb31163a1e57b5a9b2ec4c1697bd3c473 (patch)
treedc7105ad6235a2925f7ad33d4382bd7de49df38d
parentc570c514bcd0de80070ce9c5d21e46f0b056e105 (diff)
downloadaancyk-447c82adb31163a1e57b5a9b2ec4c1697bd3c473.tar.gz
area draw/delete
-rw-r--r--src/editor.c81
1 files changed, 57 insertions, 24 deletions
diff --git a/src/editor.c b/src/editor.c
index 5c9f80a..f89f7c1 100644
--- a/src/editor.c
+++ b/src/editor.c
@@ -15,7 +15,7 @@ editor_redraw(Window *this, Root *root)
tileset_set_alpha(root->tileset, 191);
grid_draw(root->brush, root->tileset, this, dx, dy);
tileset_set_alpha(root->tileset, 255);
- } else if (this->in_focus) {
+ } else if (this->in_focus && this->clicked != EDCLICK_ERASE) {
const SDL_Rect rect = {
this->mouse_x * root->cfg->tile_width,
this->mouse_y * root->cfg->tile_height,
@@ -99,8 +99,9 @@ static void
_ev_mouse(Window *this, Root *root)
{
Input *const input = &this->input;
- if (input_button_pressed(input, SDL_BUTTON_LEFT) &&
- input_key_down(input, SDL_SCANCODE_LCTRL)) {
+ const bool lctrl_down = input_key_down(input, SDL_SCANCODE_LCTRL);
+ const bool lshft_down = input_key_down(input, SDL_SCANCODE_LSHIFT);
+ if (input_button_pressed(input, SDL_BUTTON_LEFT) && lctrl_down) {
this->clicked = EDCLICK_PICK;
this->clicked_x = this->mouse_x;
this->clicked_y = this->mouse_y;
@@ -119,34 +120,66 @@ _ev_mouse(Window *this, Root *root)
}
}
this->redraw = true;
- } else if (input_button_down(input, SDL_BUTTON_RIGHT)) {
+ } else if (input_button_pressed(input, SDL_BUTTON_RIGHT) &&
+ (lshft_down || lctrl_down)) {
+ this->clicked = EDCLICK_ERASE;
+ this->clicked_x = this->mouse_x;
+ this->clicked_y = this->mouse_y;
+ } else if (input_button_released(input, SDL_BUTTON_RIGHT) &&
+ this->clicked == EDCLICK_ERASE) {
+ this->clicked = false;
+ const int x = min(this->clicked_x, this->mouse_x);
+ const int y = min(this->clicked_y, this->mouse_y);
+ const int w = max(this->clicked_x, this->mouse_x) - x + 1;
+ const int h = max(this->clicked_y, this->mouse_y) - y + 1;
+ forloop(ty, y, y + h)
+ forloop (tx, x, x + w)
+ grid_set(root->cell, tx, ty, 0);
+ } else if (input_button_down(input, SDL_BUTTON_RIGHT) &&
+ this->clicked != EDCLICK_ERASE) {
if (grid_get(root->cell, this->mouse_x, this->mouse_y)) {
grid_set(root->cell, this->mouse_x, this->mouse_y, 0);
this->redraw = true;
}
- } else if (input_button_down(input, SDL_BUTTON_LEFT)) {
- const int prev_tile = grid_get(root->cell, this->mouse_x, this->mouse_y);
- if (input_key_down(input, SDL_SCANCODE_LCTRL)) {
- grid_resize(root->brush, 1, 1);
- grid_set(root->brush, 0, 0, prev_tile);
- this->redraw = true;
- } else {
- forloop (y, 0, root->brush->height) {
- const int sy = this->mouse_y + y;
- if (sy >= root->cell->height)
+ } else if (input_button_pressed(input, SDL_BUTTON_LEFT) &&
+ lshft_down) {
+ this->clicked = EDCLICK_DRAW;
+ this->clicked_x = this->mouse_x;
+ this->clicked_y = this->mouse_y;
+ } else if (input_button_released(input, SDL_BUTTON_LEFT) &&
+ this->clicked == EDCLICK_DRAW) {
+ this->clicked = 0;
+ const int x = min(this->clicked_x, this->mouse_x);
+ const int y = min(this->clicked_y, this->mouse_y);
+ const int w = max(this->clicked_x, this->mouse_x) - x + 1;
+ const int h = max(this->clicked_y, this->mouse_y) - y + 1;
+ forloop(ty, 0, h) {
+ const int iy = ty % root->brush->height;
+ forloop(tx, 0, w) {
+ const int ix = tx % root->brush->width;
+ const int tile = grid_get(root->brush, ix, iy);
+ if (tile == 0)
+ continue;
+ grid_set(root->cell, x + tx, y + ty, tile);
+ }
+ }
+ } else if (input_button_down(input, SDL_BUTTON_LEFT) &&
+ this->clicked == 0) {
+ forloop (y, 0, root->brush->height) {
+ const int sy = this->mouse_y + y;
+ if (sy >= root->cell->height)
+ break;
+ forloop (x, 0, root->brush->width) {
+ const int sx = this->mouse_x + x;
+ if (sx >= root->cell->width)
break;
- forloop (x, 0, root->brush->width) {
- const int sx = this->mouse_x + x;
- if (sx >= root->cell->width)
- break;
- const int tile = grid_get(root->brush, x, y);
- if (tile == 0)
- continue;
- grid_set(root->cell, sx, sy, tile);
- }
+ const int tile = grid_get(root->brush, x, y);
+ if (tile == 0)
+ continue;
+ grid_set(root->cell, sx, sy, tile);
}
- this->redraw = true;
}
+ this->redraw = true;
}
}