Skip to content

Commit

Permalink
WIP: add buttons to save and load selection
Browse files Browse the repository at this point in the history
  • Loading branch information
lets-all-be-stupid-forever committed Oct 3, 2024
1 parent 9b7cfd5 commit e2ef32c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
Binary file modified assets/sprite4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
#define rect_info ((Rectangle){432, 16, 13, 13})
#define rect_exit ((Rectangle){240, 16, 13, 13})

// Selection open/save
#define rect_sel_open ((Rectangle){464, 16, 13, 13})
#define rect_sel_save ((Rectangle){464, 0, 13, 13})

// Clock speed icons
#define rect_hz0 ((Rectangle){352, 16, 13, 13})
#define rect_hz1 ((Rectangle){352, 0, 13, 13})
Expand Down
65 changes: 64 additions & 1 deletion src/w_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ static struct {
Btn btn_fill;
Btn btn_fliph;
Btn btn_flipv;
Btn btn_sel_open;
Btn btn_sel_save;
Btn btn_clockopt[6];

// Right side buttons
Expand Down Expand Up @@ -93,6 +95,8 @@ static bool MainGetIsCursorInTargeTimage();
static char* MainGetFilename();
static RectangleInt MainGetTargetRegion();
static void MainLoadImageFromPath(const char* path);
static void MainOpenSelection(Ui* ui);
static void MainSaveSelection(Ui* ui);

static bool RectHover(Rectangle hitbox, Vector2 pos) {
return CheckCollisionPointRec(pos, hitbox);
Expand Down Expand Up @@ -254,6 +258,45 @@ void MainUpdateControls(Ui* ui) {
PaintHandleKeys(&C.ca);
}

void MainOpenSelection(Ui* ui) {
ModalResult mr = ModalOpenFile(NULL);
if (mr.ok) {
Image img = LoadImage(mr.path);
PaintPasteImage(&C.ca, img);
free(mr.path);
} else if (mr.cancel) {
} else {
char txt[500];
sprintf(txt, "ERROR: %s\n", mr.error_msg);
MsgAdd(txt, MSG_DURATION);
}
}

void MainSaveSelection(Ui* ui) {
ModalResult mr = ModalSaveFile(NULL, NULL);
if (mr.cancel) {
return;
} else if (!mr.ok) {
char txt[500];
sprintf(txt, "ERROR: %s\n", mr.error_msg);
MsgAdd(txt, MSG_DURATION);
return;
}

if (mr.path && mr.ok) {
Image out = CloneImage(PaintGetSelBuffer(&C.ca));
// Before saving, add black pixels back
ImageAddBlacks(out);
if (!ExportImage(out, mr.path)) {
MsgAdd("ERROR: Could not save selection ...", MSG_DURATION);
return;
}
UnloadImage(out);
MsgAdd("Selection Image Saved.", MSG_DURATION);
return;
}
}

void MainUpdateHud(Ui* ui) {
if (BtnUpdate(&C.btn_new, ui)) MainAskForSaveAndProceed(ui, MainNewFile);
if (BtnUpdate(&C.btn_open, ui))
Expand All @@ -263,6 +306,9 @@ void MainUpdateHud(Ui* ui) {
if (BtnUpdate(&C.btn_about, ui)) AboutOpen(ui);
if (BtnUpdate(&C.btn_exit, ui)) ui->close_requested = true;

if (BtnUpdate(&C.btn_sel_open, ui)) MainOpenSelection(ui);
if (BtnUpdate(&C.btn_sel_save, ui)) MainSaveSelection(ui);

if (BtnUpdate(&C.btn_line, ui)) PaintSetTool(&C.ca, TOOL_LINE);
if (BtnUpdate(&C.btn_brush, ui)) PaintSetTool(&C.ca, TOOL_BRUSH);
if (BtnUpdate(&C.btn_marquee, ui)) PaintSetTool(&C.ca, TOOL_SEL);
Expand Down Expand Up @@ -419,6 +465,8 @@ void MainDraw(Ui* ui) {
BtnDrawIcon(&C.btn_fliph, bscale, ui->sprites, rect_fliph);
BtnDrawIcon(&C.btn_flipv, bscale, ui->sprites, rect_flipv);
BtnDrawIcon(&C.btn_fill, bscale, ui->sprites, rect_fill);
BtnDrawIcon(&C.btn_sel_open, bscale, ui->sprites, rect_sel_open);
BtnDrawIcon(&C.btn_sel_save, bscale, ui->sprites, rect_sel_save);

BtnDrawIcon(&C.btn_clockopt[0], bscale, ui->sprites, rect_hz0);
BtnDrawIcon(&C.btn_clockopt[1], bscale, ui->sprites, rect_hz1);
Expand Down Expand Up @@ -448,6 +496,9 @@ void MainDraw(Ui* ui) {
BtnDrawLegend(&C.btn_about, bscale, "About Circuit Artist");
BtnDrawLegend(&C.btn_exit, bscale, "Exit");

BtnDrawLegend(&C.btn_sel_open, bscale, "Import Image as Selection");
BtnDrawLegend(&C.btn_sel_save, bscale, "Save Selection as Image");

BtnDrawLegend(
&C.btn_simu, bscale,
simu_on ? "Stop Simulation (SPACE)" : "Start Simulation (SPACE)");
Expand Down Expand Up @@ -540,7 +591,10 @@ void MainUpdateLayout(Ui* ui) {
C.btn_about.hitbox = (Rectangle){x4, y0, bw, bh};
C.btn_exit.hitbox = (Rectangle){x5, y0, bw, bh};

C.btn_tutorial.hitbox = (Rectangle){x8, y0, 3 * bw, bh};
C.btn_sel_open.hitbox = (Rectangle){x7, y0, bw, bh};
C.btn_sel_save.hitbox = (Rectangle){x8, y0, bw, bh};

C.btn_tutorial.hitbox = (Rectangle){x10, y0, 3 * bw, bh};
}

int sh = GetScreenHeight() / s;
Expand All @@ -556,6 +610,7 @@ void MainUpdateLayout(Ui* ui) {
int by3 = by2 + 18 * s + 4 * s;
int by4 = by3 + 18 * s;
int by5 = by4 + 18 * s;
int by5b = by4 + 18 * s + 4 * s;

int bx1 = bx0 + 18 * s;
int bw = 17 * s;
Expand All @@ -572,6 +627,8 @@ void MainUpdateLayout(Ui* ui) {
C.btn_flipv.hitbox = (Rectangle){bx1, by3, bw, bh};
C.btn_rotate.hitbox = (Rectangle){bx0, by4, bw, bh};
C.btn_fill.hitbox = (Rectangle){bx1, by4, bw, bh};
// C.btn_sel_open.hitbox = (Rectangle){bx0, by5b, bw, bh};
// C.btn_sel_save.hitbox = (Rectangle){bx1, by5b, bw, bh};

C.btn_clockopt[0].hitbox = (Rectangle){bx0, by3, bw, bh};
C.btn_clockopt[1].hitbox = (Rectangle){bx1, by3, bw, bh};
Expand Down Expand Up @@ -829,11 +886,17 @@ void MainUpdateWidgets() {
C.btn_fliph.disabled = !has_sel || ned;
C.btn_fill.disabled = !has_sel || ned;

C.btn_sel_open.disabled = ned;
C.btn_sel_save.disabled = !has_sel || ned;

C.btn_rotate.hidden = (tool != TOOL_SEL) || ned;
C.btn_flipv.hidden = (tool != TOOL_SEL) || ned;
C.btn_fliph.hidden = (tool != TOOL_SEL) || ned;
C.btn_fill.hidden = (tool != TOOL_SEL) || ned;

// C.btn_sel_open.hidden = (tool != TOOL_SEL) || ned;
// C.btn_sel_save.hidden = (tool != TOOL_SEL) || ned;

for (int i = 0; i < 6; i++) {
C.btn_clockopt[i].hidden = C.ca.mode != MODE_SIMU;
C.btn_clockopt[i].toggled = PaintGetClockSpeed(&C.ca) == i;
Expand Down

0 comments on commit e2ef32c

Please sign in to comment.