From 2b24b61b156a66dd518f739156c424dbede075b9 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Fri, 22 Jan 2021 14:02:10 -0500 Subject: got file selector to mostly work, better mouse dragging --- main.c | 34 +++++++++++++++++++--------------- ui.c | 11 +++++++---- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/main.c b/main.c index eddefa1..953b109 100644 --- a/main.c +++ b/main.c @@ -250,6 +250,8 @@ int main(int argc, char **argv) { memset(ted->nmouse_clicks, 0, sizeof ted->nmouse_clicks); + //printf("%p\n",(void *)ted->drag_buffer); + while (SDL_PollEvent(&event)) { TextBuffer *buffer = ted->active_buffer; u32 key_modifier = (u32)ctrl_down << KEY_MODIFIER_CTRL_BIT @@ -300,23 +302,18 @@ int main(int argc, char **argv) { } break; } } break; - case SDL_MOUSEBUTTONUP: - if (event.button.button == SDL_BUTTON_LEFT) + case SDL_MOUSEMOTION: { + float x = (float)event.motion.x, y = (float)event.motion.y; + if (ted->drag_buffer != ted->active_buffer) ted->drag_buffer = NULL; - break; - case SDL_MOUSEMOTION: - if (event.motion.state == SDL_BUTTON_LMASK) { - if (ted->drag_buffer != ted->active_buffer) - ted->drag_buffer = NULL; - if (ted->drag_buffer) { - BufferPos pos = {0}; - // drag to select - // we don't check the return value here, because it's okay to drag off the screen. - buffer_pixels_to_pos(ted->drag_buffer, V2((float)event.button.x, (float)event.button.y), &pos); - buffer_select_to_pos(ted->drag_buffer, pos); - } + if (ted->drag_buffer) { + BufferPos pos = {0}; + // drag to select + // we don't check the return value here, because it's okay to drag off the screen. + buffer_pixels_to_pos(ted->drag_buffer, V2(x, y), &pos); + buffer_select_to_pos(ted->drag_buffer, pos); } - break; + } break; case SDL_KEYDOWN: { SDL_Scancode scancode = event.key.keysym.scancode; SDL_Keymod modifier = event.key.keysym.mod; @@ -358,6 +355,13 @@ int main(int argc, char **argv) { } } + if (!(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON_LMASK)) { + // originally this was done on SDL_MOUSEBUTTONUP events but for some reason + // I was getting a bunch of those even when I was holding down the mouse. + // This makes it much smoother. + ted->drag_buffer = NULL; + } + menu_update(ted, ted->menu); u32 key_modifier = (u32)ctrl_down << KEY_MODIFIER_CTRL_BIT diff --git a/ui.c b/ui.c index 58f76d9..04680d2 100644 --- a/ui.c +++ b/ui.c @@ -41,7 +41,7 @@ static int qsort_file_entry_cmp(void const *av, void const *bv) { } // change directory of file selector. -void file_selector_cd(FileSelector *fs, char const *path) { +void file_selector_cd(Ted *ted, FileSelector *fs, char const *path) { // @TODO: handle .. properly if (path[0] == PATH_SEPARATOR #if _WIN32 @@ -57,6 +57,9 @@ void file_selector_cd(FileSelector *fs, char const *path) { arr_append_str(fs->cwd, PATH_SEPARATOR_STR); } arr_append_str(fs->cwd, path); + + // clear search term + buffer_clear(&ted->line_buffer); } // returns the name of the selected file, or NULL @@ -90,7 +93,7 @@ static char *file_selector_update(Ted *ted, FileSelector *fs) { if (path) return str_dup(path); break; case FS_DIRECTORY: - file_selector_cd(fs, name); + file_selector_cd(ted, fs, name); break; default: break; } @@ -106,7 +109,7 @@ static char *file_selector_update(Ted *ted, FileSelector *fs) { if (path) return str_dup(path); break; case FS_DIRECTORY: - file_selector_cd(fs, name); + file_selector_cd(ted, fs, name); break; default: break; } @@ -158,7 +161,7 @@ static char *file_selector_update(Ted *ted, FileSelector *fs) { size_t path_size = strlen(name) + strlen(cwd) + 3; char *path = ted_calloc(ted, 1, path_size); if (path) { - snprintf(path, path_size - 1, "%s%s%s", cwd, cwd_has_path_sep ? PATH_SEPARATOR_STR : "", name); + snprintf(path, path_size - 1, "%s%s%s", cwd, cwd_has_path_sep ? "" : PATH_SEPARATOR_STR, name); entries[i].path = path; entries[i].type = fs_path_type(path); } else { -- cgit v1.2.3