summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-01-28 15:44:16 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-01-28 15:44:16 -0500
commit5b90057859cd38b284573460460ab27bd108aa0e (patch)
tree4b9ac2e17b8eb8f3cc6c3ac042327226b509cccc
parentaea002f290e5eddde98eb394fa7a9145c325fffe (diff)
finished warn on overwrite
-rw-r--r--Untitled2
-rw-r--r--math.c4
-rw-r--r--menu.c23
-rw-r--r--ui.c46
4 files changed, 63 insertions, 12 deletions
diff --git a/Untitled b/Untitled
index b375b10..f2a5887 100644
--- a/Untitled
+++ b/Untitled
@@ -1,2 +1,2 @@
-1 2 3 4
+a 1 2 3 4
56 78 910 1112
diff --git a/math.c b/math.c
index 6497040..fe28ef1 100644
--- a/math.c
+++ b/math.c
@@ -195,6 +195,10 @@ static v2 v2_mul(v2 a, v2 b) {
return V2(a.x * b.x, a.y * b.y);
}
+static v2 v2_clamp(v2 x, v2 a, v2 b) {
+ return V2(clampf(x.x, a.x, b.x), clampf(x.y, a.y, b.y));
+}
+
static float v2_dot(v2 a, v2 b) {
return a.x * b.x + a.y * b.y;
}
diff --git a/menu.c b/menu.c
index a307a62..68f941a 100644
--- a/menu.c
+++ b/menu.c
@@ -56,18 +56,39 @@ static void menu_update(Ted *ted, Menu menu) {
case MENU_NONE: break;
case MENU_SAVE_AS: {
if (*ted->warn_overwrite) {
+ switch (popup_update(ted)) {
+ case POPUP_NONE:
+ // no option selected
+ break;
+ case POPUP_YES:
+ // overwrite it!
+ if (ted->prev_active_buffer)
+ buffer_save_as(ted->prev_active_buffer, ted->warn_overwrite);
+ menu_close(ted, true);
+ break;
+ case POPUP_NO:
+ // back to the file selector
+ *ted->warn_overwrite = '\0';
+ ted->active_buffer = &ted->line_buffer;
+ break;
+ case POPUP_CANCEL:
+ // close "save as" menu
+ menu_close(ted, true);
+ break;
+ }
} else {
char *selected_file = file_selector_update(ted, &ted->file_selector);
if (selected_file) {
TextBuffer *buffer = ted->prev_active_buffer;
if (buffer) {
if (fs_path_type(selected_file) != FS_NON_EXISTENT) {
+ // file already exists! warn about overwriting it.
strbuf_cpy(ted->warn_overwrite, selected_file);
ted->active_buffer = NULL;
} else {
+ // create the new file.
buffer_save_as(buffer, selected_file);
menu_close(ted, true);
- file_selector_free(&ted->file_selector);
}
}
free(selected_file);
diff --git a/ui.c b/ui.c
index 827f73c..7207a7d 100644
--- a/ui.c
+++ b/ui.c
@@ -479,6 +479,16 @@ static void button_render(Ted *ted, Rect button, char const *text, u32 color) {
text_render_anchored(ted->font, text, pos.x, pos.y, ANCHOR_MIDDLE);
}
+// returns true if the button was clicked on.
+static bool button_update(Ted *ted, Rect button) {
+ for (u16 i = 0; i < ted->nmouse_clicks[SDL_BUTTON_LEFT]; ++i) {
+ if (rect_contains_point(button, ted->mouse_clicks[SDL_BUTTON_LEFT][i])) {
+ return true;
+ }
+ }
+ return false;
+}
+
typedef enum {
POPUP_NONE,
POPUP_YES,
@@ -486,22 +496,45 @@ typedef enum {
POPUP_CANCEL
} PopupOption;
+
+static void popup_get_rects(Ted const *ted, Rect *popup, Rect *button_yes, Rect *button_no, Rect *button_cancel) {
+ float window_width = ted->window_width, window_height = ted->window_height;
+
+ *popup = rect_centered(V2(window_width * 0.5f, window_height * 0.5f), V2(300, 200));
+ float button_height = 30;
+ u16 nbuttons = 3;
+ float button_width = popup->size.x / nbuttons;
+ popup->size = v2_clamp(popup->size, v2_zero, V2(window_width, window_height));
+ *button_yes = rect(V2(popup->pos.x, rect_y2(*popup) - button_height), V2(button_width, button_height));
+ *button_no = rect_translate(*button_yes, V2(button_width, 0));
+ *button_cancel = rect_translate(*button_no, V2(button_width, 0));
+
+}
+
static PopupOption popup_update(Ted *ted) {
- (void)ted;
+ Rect r, button_yes, button_no, button_cancel;
+ popup_get_rects(ted, &r, &button_yes, &button_no, &button_cancel);
+ if (button_update(ted, button_yes))
+ return POPUP_YES;
+ if (button_update(ted, button_no))
+ return POPUP_NO;
+ if (button_update(ted, button_cancel))
+ return POPUP_CANCEL;
return POPUP_NONE;
}
static void popup_render(Ted *ted, char const *title, char const *body) {
float window_width = ted->window_width;
- float window_height = ted->window_height;
Font *font = ted->font;
Font *font_bold = ted->font_bold;
- Rect r = rect_centered(V2(window_width * 0.5f, window_height * 0.5f), V2(300, 200));
+ Rect r, button_yes, button_no, button_cancel;
Settings const *settings = &ted->settings;
u32 const *colors = settings->colors;
float const char_height_bold = text_font_char_height(font_bold);
float const padding = settings->padding;
+ popup_get_rects(ted, &r, &button_yes, &button_no, &button_cancel);
+
glBegin(GL_QUADS);
gl_color_rgba(colors[COLOR_MENU_BG]);
rect_render(r);
@@ -538,13 +571,6 @@ static void popup_render(Ted *ted, char const *title, char const *body) {
};
text_render_with_state(font, &state, body, text_x1, y);
- float button_height = 30;
- u16 nbuttons = 3;
- float button_width = r.size.x / nbuttons;
- Rect button_yes = rect(V2(r.pos.x, rect_y2(r) - button_height), V2(button_width, button_height));
- Rect button_no = rect_translate(button_yes, V2(button_width, 0));
- Rect button_cancel = rect_translate(button_no, V2(button_width, 0));
-
button_render(ted, button_yes, "Yes", colors[COLOR_YES]);
button_render(ted, button_no, "No", colors[COLOR_NO]);
button_render(ted, button_cancel, "Cancel", colors[COLOR_CANCEL]);