Skip to content

Commit

Permalink
add line separation option in line tool
Browse files Browse the repository at this point in the history
  • Loading branch information
lets-all-be-stupid-forever committed Oct 7, 2024
1 parent 9a8930b commit 6fa6d79
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 37 deletions.
26 changes: 14 additions & 12 deletions src/img.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ void ImageEnsureMaxSize(Image* img) {
}

void DrawImageLineTool(Vector2Int start, RectangleInt tool_rect,
RectangleInt img_rect, int ls, bool corner,
RectangleInt img_rect, int ls, int sep, bool corner,
bool end_corner, Color c, Image* out, Vector2Int* off) {
const int DIR_HORIZONTAL = 0;
const int DIR_VERTICAL = 1;
RectangleInt r = tool_rect;
int startx = start.x;
int starty = start.y;
int endx = 2 * r.x + r.width - startx - 1;
int endx = 2 * r.x + r.width - startx - 1; // r.x+r.width ->
int endy = 2 * r.y + r.height - starty - 1;

int dir = -1;
Expand All @@ -266,14 +266,14 @@ void DrawImageLineTool(Vector2Int start, RectangleInt tool_rect,
.x = r.x,
.y = starty,
.width = r.width,
.height = 2 * ls - 1,
.height = (sep + 1) * ls - 1,
};
} else {
dir = DIR_VERTICAL;
r = (RectangleInt){
.x = startx,
.y = r.y,
.width = 2 * ls - 1,
.width = (sep + 1) * ls - 1,
.height = r.height,
};
}
Expand All @@ -285,13 +285,15 @@ void DrawImageLineTool(Vector2Int start, RectangleInt tool_rect,
}
int w = r.width;
int h = r.height;
int hh = 2 * ls - 1;
int ww = 2 * ls - 1;
Image img = GenImageFilled(w, h, BLANK);
*out = img;
// horizontal
Color* data = GetPixels(*out);
if (dir == DIR_HORIZONTAL) {
for (int y = 0; y < img.height; y += 2) {
Color* line = data + y * img.width;
for (int yy = 0, y = 0; yy < img.height; yy += sep + 1, y += 2) {
Color* line = data + yy * img.width;
for (int x = 0; x < img.width; x++) {
// Horizontal lines
// When end_corner is true, it's like it starts from the other end.
Expand All @@ -301,7 +303,7 @@ void DrawImageLineTool(Vector2Int start, RectangleInt tool_rect,
dx = dx < 0 ? -dx : dx;
dy = dy < 0 ? -dy : dy;
if (endx > startx && dx < dy) continue;
if (endx < startx && dx < (h - dy - 1)) continue;
if (endx < startx && dx < (hh - dy - 1)) continue;
}
if (end_corner) {
// Just exchange startx and endx
Expand All @@ -310,32 +312,32 @@ void DrawImageLineTool(Vector2Int start, RectangleInt tool_rect,
dx = dx < 0 ? -dx : dx;
dy = dy < 0 ? -dy : dy;
if (startx > endx && dx < dy) continue;
if (startx < endx && dx < (h - dy - 1)) continue;
if (startx < endx && dx < (hh - dy - 1)) continue;
}
line[x] = c;
}
}
} else {
for (int y = 0; y < img.height; y++) {
Color* line = data + y * img.width;
for (int x = 0; x < img.width; x += 2) {
for (int xx = 0, x = 0; xx < img.width; xx += sep + 1, x += 2) {
if (corner) {
int dx = x + r.x - startx;
int dy = y + r.y - starty;
dx = dx < 0 ? -dx : dx;
dy = dy < 0 ? -dy : dy;
if (endy > starty && dx > dy) continue;
if (endy < starty && (w - dx - 1) > dy) continue;
if (endy < starty && (ww - dx - 1) > dy) continue;
}
if (end_corner) {
int dx = x + r.x - startx;
int dy = y + r.y - endy;
dx = dx < 0 ? -dx : dx;
dy = dy < 0 ? -dy : dy;
if (starty > endy && dx > dy) continue;
if (starty < endy && (w - dx - 1) > dy) continue;
if (starty < endy && (ww - dx - 1) > dy) continue;
}
line[x] = c;
line[xx] = c;
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/img.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ Color* GetPixels(Image img);
// `corner` is a flag for the corner at the beginning tip of the wire and
// `end_corner` is a flag for corenr at the end tip of the lines.
//
// `sep` is the separation between consecutive lines.
//
// The generated image `out` is a subset of the full image . The full image
// size is defined by `img_rect`, and the offset of the subimage within the
// full image is given by the `off` output.
void DrawImageLineTool(Vector2Int start, RectangleInt tool_rect,
RectangleInt img_rect, int ls, bool corner,
RectangleInt img_rect, int ls, int sep, bool corner,
bool end_corner, Color c, Image* out, Vector2Int* off);

// Algorithm for the "Bucket Tool".
Expand Down
37 changes: 28 additions & 9 deletions src/paint.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ static Vector2Int PaintFindBestOffsetForClipboard(Paint* ca);
static bool PaintGetToolIsPickerInPractice(Paint* ca);

static void PaintOnToolChange(Paint* ca) {
ca->line_key_width = 0;
ca->line_key_width_time = -1;
ca->line_key = 0;
ca->line_key_time = -1;
}

void PaintPerformToolAction(Paint* ca) {
Expand Down Expand Up @@ -214,6 +214,7 @@ void PaintLoad(Paint* ca) {
ca->camera_x = 0;
ca->camera_y = 0;
ca->line_tool_size = 1;
ca->line_tool_sep = 1;
ca->queued_toggled_x = -1;
ca->queued_toggled_y = -1;
ca->fg_color = WHITE;
Expand Down Expand Up @@ -381,9 +382,10 @@ static void PaintMakeToolSubImage(Paint* ca, Image* img, Vector2Int* off) {
.height = buffer.height,
};
int ls = ca->line_tool_size == 0 ? 1 : ca->line_tool_size;
int sep = ca->line_tool_sep <= 0 ? 1 : ca->line_tool_sep;
bool corner = IsKeyDown(KEY_LEFT_SHIFT);
bool end_corner = IsKeyDown(KEY_LEFT_CONTROL);
DrawImageLineTool(start, PaintMakeToolRect(ca), img_rect, ls, corner,
DrawImageLineTool(start, PaintMakeToolRect(ca), img_rect, ls, sep, corner,
end_corner, c, img, off);
} else if (HistGetTool(&ca->h) == TOOL_BRUSH) {
BrushMakeImage(&ca->brush, c, buffer.width, buffer.height, img, off);
Expand Down Expand Up @@ -850,16 +852,17 @@ static int GetNumberKeyPressed() {
}

static void PaintAppendLineWidthNumber(Paint* ca, int key) {
int old_size = ca->line_key_width;
int old_size = ca->line_key;
if (!PaintGetKeyLineWidthHasJustChanged(ca)) {
old_size = 0;
}
int new_size = old_size * 10 + key;
if (new_size > MAX_LINE_WIDTH) new_size = MAX_LINE_WIDTH;
if (new_size > 0) {
ca->line_key_width = new_size;
ca->line_tool_size = new_size;
ca->line_key_width_time = GetTime();
ca->line_key = new_size;
if (ca->line_key_mode == 0) ca->line_tool_size = new_size;
if (ca->line_key_mode == 1) ca->line_tool_sep = new_size;
ca->line_key_time = GetTime();
}
}

Expand All @@ -869,6 +872,16 @@ void PaintHandleKeys(Paint* ca) {
}
if (ca->mode == MODE_EDIT) {
if (PaintGetTool(ca) == TOOL_LINE) {
if (IsKeyPressed(KEY_W)) {
ca->line_key = 0;
ca->line_key_mode = 0;
ca->line_key_time = GetTime();
}
if (IsKeyPressed(KEY_S)) {
ca->line_key = 0;
ca->line_key_mode = 1;
ca->line_key_time = GetTime();
}
int key = GetNumberKeyPressed();
if (key >= 0) {
PaintAppendLineWidthNumber(ca, key);
Expand Down Expand Up @@ -1150,11 +1163,15 @@ void PaintGetSimuPixelToggleState(Paint* ca, int* cur_state) {
}
}

int PaintGetLineWidth(Paint* ca) { return ca->line_key_width; }
int PaintGetLineWidth(Paint* ca) { return ca->line_tool_size; }
int PaintSetLineWidth(Paint* ca, int lw) { ca->line_tool_size = lw; }

int PaintGetLineSep(Paint* ca) { return ca->line_tool_sep; }
int PaintSetLineSep(Paint* ca, int sep) { ca->line_tool_sep = sep; }

bool PaintGetKeyLineWidthHasJustChanged(Paint* ca) {
double t = GetTime();
double last_t = ca->line_key_width_time;
double last_t = ca->line_key_time;
if (last_t < 0) return false;
return t - last_t < line_modif_threshold;
}
Expand Down Expand Up @@ -1193,3 +1210,5 @@ void PaintSetClockSpeed(Paint* ca, int c) {
}

int PaintGetClockSpeed(Paint* ca) { return ca->clock_speed; }

int PaintSetLineKeyMode(Paint* ca) { return ca->line_key_mode; }
15 changes: 13 additions & 2 deletions src/paint.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ typedef struct {
bool tool_pressed;
// Line size for Line tool.
int line_tool_size;
// line tool separation size
int line_tool_sep;
// Shift of the image on screen in screen pixels (x).
float camera_x;
// Shift of the image on screen in screen pixels (y).
Expand Down Expand Up @@ -103,10 +105,13 @@ typedef struct {
// Extra y space in the top of the image (in pixels).
int extray;
// Line width entered via keyboard. (line tool)
int line_key_width;
int line_key;
// mode=0 --> changing width
// mode=1 --> changing sep
int line_key_mode;
// Frame where change was made.
// It's used to allow the user to type a multi-digit line width.
double line_key_width_time;
double line_key_time;
// Clock speed index. 0 = slower, 5 = faster.
int clock_speed;
// Temporary new size when user is changing the size of the image via the
Expand Down Expand Up @@ -243,6 +248,12 @@ bool PaintGetIsToolSelMoving(Paint* ca);

// Returns the active line width for the line tool.
int PaintGetLineWidth(Paint* ca);
int PaintSetLineWidth(Paint* ca, int lw);

int PaintSetLineKeyMode(Paint* ca);
//
int PaintGetLineSep(Paint* ca);
int PaintSetLineSep(Paint* ca, int sep);

// Returns whether the line width for the line tool has just changed (ie, the
// user is still entering the width size on keyboard).
Expand Down
Loading

0 comments on commit 6fa6d79

Please sign in to comment.