diff options
| author | Aldrik Ramaekers <aldrik.ramaekers@protonmail.com> | 2020-01-31 13:12:19 +0100 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrik.ramaekers@protonmail.com> | 2020-01-31 13:12:19 +0100 |
| commit | a5e8a8b3c99fe69ae181dcaeac8ae687d061a7b5 (patch) | |
| tree | 75af06f20a5004ab7094645956db887a17e92b21 /src | |
| parent | 260f05025631031b7cc4904805d5017feaf53eda (diff) | |
work
Diffstat (limited to 'src')
| -rw-r--r-- | src/assets.h | 5 | ||||
| -rw-r--r-- | src/config.h | 2 | ||||
| -rw-r--r-- | src/linux/platform.c | 4 | ||||
| -rw-r--r-- | src/mo_edit.c | 231 | ||||
| -rw-r--r-- | src/test.txt | 1 | ||||
| -rw-r--r-- | src/ui.c | 70 | ||||
| -rw-r--r-- | src/ui.h | 7 | ||||
| -rw-r--r-- | src/windows/platform.c | 4 |
8 files changed, 267 insertions, 57 deletions
diff --git a/src/assets.h b/src/assets.h index 3cfac52..7202997 100644 --- a/src/assets.h +++ b/src/assets.h @@ -59,6 +59,11 @@ extern u8 _binary____data_imgs_delete_png_end[]; extern u8 _binary____data_imgs_exclaim_png_start[]; extern u8 _binary____data_imgs_exclaim_png_end[]; +extern u8 _binary____data_imgs_add_png_start[]; +extern u8 _binary____data_imgs_add_png_end[]; + +extern u8 _binary____data_imgs_set_png_start[]; +extern u8 _binary____data_imgs_set_png_end[]; typedef struct t_image { u8 *start_addr; diff --git a/src/config.h b/src/config.h index a1a7611..3bfb3c3 100644 --- a/src/config.h +++ b/src/config.h @@ -15,4 +15,6 @@ #define MAX_ERROR_MESSAGE_LENGTH 120 #define MAX_STATUS_TEXT_LENGTH 120 +#define MAX_TERM_NAME_LENGTH 100 + #endif
\ No newline at end of file diff --git a/src/linux/platform.c b/src/linux/platform.c index e650097..a4d85cf 100644 --- a/src/linux/platform.c +++ b/src/linux/platform.c @@ -140,7 +140,7 @@ void platform_create_config_directory() { char *env = getenv("HOME"); char tmp[PATH_MAX]; - snprintf(tmp, PATH_MAX, "%s%s", env, "/.config/text-search"); + snprintf(tmp, PATH_MAX, "%s%s", env, "/.config/moedit"); if (!platform_directory_exists(tmp)) { @@ -151,7 +151,7 @@ void platform_create_config_directory() char* get_config_save_location(char *buffer) { char *env = getenv("HOME"); - snprintf(buffer, PATH_MAX, "%s%s", env, "/.config/text-search/config.txt"); + snprintf(buffer, PATH_MAX, "%s%s", env, "/.config/moedit/config.txt"); return buffer; } diff --git a/src/mo_edit.c b/src/mo_edit.c index 019dbbe..fe511ac 100644 --- a/src/mo_edit.c +++ b/src/mo_edit.c @@ -13,7 +13,7 @@ typedef struct t_translation { - s32 country_index; + bool valid; char *value; } translation; @@ -34,12 +34,18 @@ translation_project *current_project = 0; scroll_state term_scroll; scroll_state lang_scroll; +scroll_state trans_scroll; button_state btn_new_project; button_state btn_new_language; button_state btn_summary; +button_state btn_set_term_name; dropdown_state dd_available_countries; textbox_state tb_filter; +textbox_state tb_new_term; +textbox_state tb_translation_list[COUNTRY_CODE_COUNT]; +image *set_img; +image *add_img; image *list_img; image *exclaim_img; image *delete_img; @@ -63,6 +69,10 @@ static void load_assets() _binary____data_imgs_logo_64_png_end, true); delete_img = assets_load_image(_binary____data_imgs_delete_png_start, _binary____data_imgs_delete_png_end, false); + add_img = assets_load_image(_binary____data_imgs_add_png_start, + _binary____data_imgs_add_png_end, false); + set_img = assets_load_image(_binary____data_imgs_set_png_start, + _binary____data_imgs_set_png_end, false); font_medium = assets_load_font(_binary____data_fonts_mono_ttf_start, _binary____data_fonts_mono_ttf_end, 18); @@ -109,19 +119,104 @@ s32 get_translated_count_for_language(s32 index) { term *t = array_at(¤t_project->terms, i); - for (s32 x = 0; x < COUNTRY_CODE_COUNT; x++) + translation *tr = &t->translations[index]; + if (tr->valid && tr->value) { - translation *tr = &t->translations[x]; - if (tr->country_index == i) - { - count++; - } + count++; } } return count; } +bool term_name_is_available(char *name) +{ + for (s32 i = 0; i < current_project->terms.length; i++) + { + term *tr = array_at(¤t_project->terms, i); + + if (string_equals(tr->name, name)) + { + return false; + } + } + + return true; +} + +void add_country_to_project() +{ + array_push(¤t_project->languages, &dd_available_countries.selected_index); + + for (s32 x = 0; x < current_project->terms.length; x++) + { + term *t = array_at(¤t_project->terms, x); + + translation *tr = &t->translations[dd_available_countries.selected_index]; + tr->valid = true; + } +} + +void set_term_name(s32 index, char *name) +{ + term *t = array_at(¤t_project->terms, index); + string_copyn(t->name, name, MAX_TERM_NAME_LENGTH); +} + +void select_term(s32 index) +{ + current_project->selected_term_index = index; + term *t = array_at(¤t_project->terms, index); + ui_set_textbox_text(&tb_new_term, t->name); + + for (s32 i = 0; i < COUNTRY_CODE_COUNT; i++) + { + textbox_state tb = tb_translation_list[i]; + + if (t->translations[i].value) + { + string_copyn(tb.buffer, t->translations[i].value, MAX_INPUT_LENGTH); + } + else + { + string_copyn(tb.buffer, "", MAX_INPUT_LENGTH); + } + } +} + +s32 add_term_to_project() +{ + term t; + t.name = mem_alloc(MAX_TERM_NAME_LENGTH); + + s32 count = 0; + do + { + char buffer[MAX_TERM_NAME_LENGTH]; + sprintf(buffer, "term_%d", count); + string_copyn(t.name, buffer, MAX_TERM_NAME_LENGTH); + count++; + } + while(!term_name_is_available(t.name)); + + for (s32 x = 0; x < COUNTRY_CODE_COUNT; x++) + { + translation tr; + tr.value = 0; + tr.valid = false; + t.translations[x] = tr; + } + + for (s32 i = 0; i < current_project->languages.length; i++) + { + s32 index = *(s32*)array_at(¤t_project->languages, i); + translation *tr = &t.translations[index]; + tr->valid = true; + } + + return array_push(¤t_project->terms, &t); +} + void start_new_project() { current_project = mem_alloc(sizeof(translation_project)); @@ -134,23 +229,6 @@ void start_new_project() array_reserve(¤t_project->languages, 100); current_project->languages.reserve_jump = 100; - for (s32 i = 0; i < 3; i++) - { - term t; - t.name = mem_alloc(10); - string_copyn(t.name, "meme_1", 10); - - for (s32 x = 0; x < COUNTRY_CODE_COUNT; x++) - { - translation tr; - tr.value = 0; - tr.country_index = -1; - t.translations[x] = tr; - } - - array_push(¤t_project->terms, &t); - } - current_project->selected_term_index = -1; } @@ -213,10 +291,16 @@ int main(int argc, char **argv) dd_available_countries = ui_create_dropdown(); term_scroll = ui_create_scroll(1); lang_scroll = ui_create_scroll(1); + trans_scroll = ui_create_scroll(1); btn_summary = ui_create_button(); + btn_set_term_name = ui_create_button(); btn_new_project = ui_create_button(); btn_new_language = ui_create_button(); tb_filter = ui_create_textbox(MAX_INPUT_LENGTH); + tb_new_term = ui_create_textbox(MAX_TERM_NAME_LENGTH); + + for (s32 i = 0; i < COUNTRY_CODE_COUNT; i++) + tb_translation_list[i] = ui_create_textbox(MAX_INPUT_LENGTH); // asset worker thread asset_queue_worker1 = thread_start(assets_queue_worker, NULL); @@ -299,12 +383,20 @@ int main(int argc, char **argv) { ui_block_begin(LAYOUT_HORIZONTAL); { - ui_push_button_image(&btn_summary, "", list_img); + if (ui_push_button_image(&btn_summary, "", list_img)) + { + current_project->selected_term_index = -1; + } + // TODO(Aldrik): translate ui_push_textf_width(font_medium, "Terms", global_ui_context.layout.width-150); - ui_push_button_image(&btn_summary, "", delete_img); - ui_push_button_image(&btn_summary, "", delete_img); + if (ui_push_button_image(&btn_summary, "", add_img)) + { + select_term(add_term_to_project()); + } + + //ui_push_button_image(&btn_summary, "", delete_img); } ui_block_end(); @@ -312,6 +404,7 @@ int main(int argc, char **argv) ui_block_begin(LAYOUT_HORIZONTAL); { // TODO(Aldrik): translate + TEXTBOX_WIDTH = 280; ui_push_textbox(&tb_filter, "Filter terms.."); } ui_block_end(); @@ -327,7 +420,20 @@ int main(int argc, char **argv) ui_push_button_image(&btn_summary, "", delete_img); //ui_push_image(exclaim_img, 14, 14, 1, rgb(255,255,255)); - ui_push_text_width(t->name, global_ui_context.layout.width-100); + + if (i == current_project->selected_term_index) + { + ui_push_rect(10, global_ui_context.style.textbox_active_border); + } + else + { + ui_push_rect(10, global_ui_context.style.background); + } + + if (ui_push_text_width(t->name, global_ui_context.layout.width-100, true)) + { + select_term(i); + } ui_block_end(); } @@ -350,7 +456,53 @@ int main(int argc, char **argv) if (current_project && current_project->selected_term_index >= 0) { + bool set_name = keyboard_is_key_pressed(&keyboard, KEY_ENTER) && + tb_new_term.state; + ui_block_begin(LAYOUT_HORIZONTAL); + { + // editor + ui_push_textbox(&tb_new_term, "Term name"); + + if (set_name) + set_term_name(current_project->selected_term_index, tb_new_term.buffer); + + if (ui_push_button_image(&btn_set_term_name, "", set_img)) + { + set_term_name(current_project->selected_term_index, tb_new_term.buffer); + } + } + ui_block_end(); + + global_ui_context.layout.offset_x = 310; + ui_push_separator(); + + term *t = array_at(¤t_project->terms, + current_project->selected_term_index); + + trans_scroll.height = main_window->height-global_ui_context.layout.offset_y; + + ui_scroll_begin(&trans_scroll); + { + for (s32 i = 0; i < COUNTRY_CODE_COUNT; i++) + { + translation *tr = &t->translations[i]; + + if (tr->valid) + { + TEXTBOX_WIDTH = global_ui_context.layout.width - 100; + + ui_push_textbox(&tb_translation_list[i], ""); + ui_push_image(list_img, TEXTBOX_HEIGHT,TEXTBOX_HEIGHT,1,rgb(255,255,255)); + ui_push_text_width(global_langues[i].code, 25, false); + + global_ui_context.layout.offset_y += TEXTBOX_HEIGHT + WIDGET_PADDING; + global_ui_context.layout.offset_x = 310; + } + + } + } + ui_scroll_end(); } else if (current_project) { @@ -388,10 +540,7 @@ int main(int argc, char **argv) { if (!country_has_been_added_to_project(i)) { - if (ui_push_dropdown_item(0, global_langues[i].fullname, i)) - { - - } + ui_push_dropdown_item(0, global_langues[i].fullname, i); } } } @@ -399,8 +548,7 @@ int main(int argc, char **argv) // TODO(Aldrik): translate if (ui_push_button(&btn_new_language, "Add")) { - array_push(¤t_project->languages,&dd_available_countries.selected_index); - + add_country_to_project(); dd_available_countries.selected_index = -1; } } @@ -425,7 +573,7 @@ int main(int argc, char **argv) } s32 index = *(s32*)array_at(¤t_project->languages, i); - ui_push_text_width(global_langues[index].fullname, global_ui_context.layout.width-200); + ui_push_text_width(global_langues[index].fullname, global_ui_context.layout.width-200, false); color c = global_ui_context.style.foreground; global_ui_context.style.foreground = rgb(110,110,110); @@ -473,21 +621,12 @@ int main(int argc, char **argv) settings_page_hide_without_save(); // write config file -#if 0 - settings_config_set_string(&config, "SEARCH_DIRECTORY", textbox_path.buffer); - settings_config_set_number(&config, "SEARCH_DIRECTORIES", checkbox_recursive.state); - settings_config_set_string(&config, "SEARCH_TEXT", textbox_search_text.buffer); - settings_config_set_string(&config, "FILE_FILTER", textbox_file_filter.buffer); - settings_config_set_number(&config, "MAX_THEAD_COUNT", global_settings_page.max_thread_count); - settings_config_set_number(&config, "MAX_FILE_SIZE", global_settings_page.max_file_size); + //settings_config_set_string(&config, "ACTIVE_PROJECT", textbox_path.buffer); vec2 win_size = platform_get_window_size(&window); settings_config_set_number(&config, "WINDOW_WIDTH", win_size.x); settings_config_set_number(&config, "WINDOW_HEIGHT", win_size.y); - settings_config_set_number(&config, "STYLE", global_ui_context.style.id); - settings_config_set_number(&config, "DOUBLE_CLICK_ACTION", global_settings_page.selected_double_click_selection_option); - if (global_localization.active_localization != 0) { char *current_locale_id = locale_get_id(); @@ -499,7 +638,6 @@ int main(int argc, char **argv) settings_config_write_to_file(&config, config_path_buffer); settings_config_destroy(&config); -#endif settings_page_destroy(); @@ -517,6 +655,7 @@ int main(int argc, char **argv) assets_destroy_image(list_img); assets_destroy_image(logo_small_img); assets_destroy_image(delete_img); + assets_destroy_image(add_img); assets_destroy_font(font_small); assets_destroy_font(font_mini); diff --git a/src/test.txt b/src/test.txt new file mode 100644 index 0000000..4d6a3b4 --- /dev/null +++ b/src/test.txt @@ -0,0 +1 @@ +ECHO is on. @@ -86,6 +86,11 @@ inline scroll_state ui_create_scroll(s32 scroll) return state; } +void ui_set_textbox_text(textbox_state *textbox, char *text) +{ + string_copyn(textbox->buffer, text, textbox->max_len); +} + inline dropdown_state ui_create_dropdown() { dropdown_state state; @@ -169,11 +174,13 @@ static void ui_pop_scissor() { if (global_ui_context.layout.scroll->in_scroll) { + s32 w = global_ui_context.layout.width; + s32 h = global_ui_context.layout.height; + s32 x = global_ui_context.layout.offset_x + global_ui_context.camera->x; + s32 y = global_ui_context.layout.offset_y + global_ui_context.camera->y - WIDGET_PADDING; + render_set_scissor(global_ui_context.layout.active_window, - global_ui_context.layout.offset_x, - global_ui_context.layout.scroll->scroll_start_offset_y - WIDGET_PADDING + 1, - global_ui_context.layout.width, - global_ui_context.layout.scroll->height + WIDGET_PADDING - 3); + x,y,w,h); } else { @@ -1049,25 +1056,76 @@ void ui_push_text(char *text) global_ui_context.layout.offset_y += CHECKBOX_SIZE + WIDGET_PADDING; } -void ui_push_text_width(char *text, s32 maxw) +void ui_push_rect(s32 w, color c) { s32 spacing_y = (BLOCK_HEIGHT - CHECKBOX_SIZE)/2; s32 x = global_ui_context.layout.offset_x + global_ui_context.camera->x; + s32 y = global_ui_context.layout.offset_y + global_ui_context.camera->y + ui_get_scroll(); + s32 total_w = w + + WIDGET_PADDING + WIDGET_PADDING; + s32 h = BUTTON_HEIGHT; + + if (global_ui_context.layout.block_height < h) + global_ui_context.layout.block_height = h; + + { + render_rectangle(x+WIDGET_PADDING,y,w,h,c); + } + + if (global_ui_context.layout.layout_direction == LAYOUT_HORIZONTAL) + global_ui_context.layout.offset_x += total_w; + else + global_ui_context.layout.offset_y += BUTTON_HEIGHT + WIDGET_PADDING; +} + +bool ui_push_text_width(char *text, s32 maxw, bool active) +{ + bool result = false; + + s32 spacing_y = (BLOCK_HEIGHT - CHECKBOX_SIZE)/2; + s32 x = global_ui_context.layout.offset_x + global_ui_context.camera->x; s32 y = global_ui_context.layout.offset_y + global_ui_context.camera->y + ui_get_scroll() - spacing_y; s32 text_x = x + WIDGET_PADDING; + s32 h = BUTTON_HEIGHT; s32 text_y = y + (BLOCK_HEIGHT/2) - (global_ui_context.font_small->px_h/2) + spacing_y; s32 total_w = maxw + WIDGET_PADDING + WIDGET_PADDING; + s32 mouse_x = global_ui_context.mouse->x + global_ui_context.camera->x; + s32 mouse_y = global_ui_context.mouse->y + global_ui_context.camera->y; + s32 virt_top = y; + s32 virt_bottom = y + h; if (global_ui_context.layout.block_height < global_ui_context.font_small->px_h) global_ui_context.layout.block_height = global_ui_context.font_small->px_h+5; + if (active) + { + bool hovered = false; + if (mouse_x >= x && mouse_x < x + total_w && mouse_y >= virt_top && mouse_y < virt_bottom && !global_ui_context.item_hovered) + { + hovered = true; + platform_set_cursor(global_ui_context.layout.active_window, CURSOR_POINTER); + if (is_left_clicked(global_ui_context.mouse)) + { + result = true; + } + } + + if (hovered) + { + render_rectangle_outline(x-1,y+spacing_y,total_w, h, 1, global_ui_context.style.textbox_active_border); + } + } + render_text_ellipsed(global_ui_context.font_small, text_x, text_y, maxw, text, global_ui_context.style.foreground); + if (global_ui_context.layout.layout_direction == LAYOUT_HORIZONTAL) global_ui_context.layout.offset_x += total_w; else - global_ui_context.layout.offset_y += CHECKBOX_SIZE + WIDGET_PADDING; + global_ui_context.layout.offset_y += BUTTON_HEIGHT + WIDGET_PADDING; + + return result; } bool ui_push_checkbox(checkbox_state *state, char *title) @@ -14,7 +14,9 @@ #define BUTTON_HORIZONTAL_TEXT_PADDING 15 #define MENU_ITEM_WIDTH 220 #define CHECKBOX_SIZE BLOCK_HEIGHT - 8 -#define TEXTBOX_WIDTH 280 + +static s32 TEXTBOX_WIDTH = 280; + #define TEXTBOX_HEIGHT BLOCK_HEIGHT #define BUTTON_HEIGHT BLOCK_HEIGHT #define BUTTON_IMAGE_PADDING 5 @@ -166,6 +168,7 @@ char* name_of_day(s32 day); char* name_of_month(s32 month); void ui_set_style(u16 style); void set_active_textbox(textbox_state *textbox); +void ui_set_textbox_text(textbox_state *textbox, char *text); // widget initialization checkbox_state ui_create_checkbox(bool selected); @@ -185,11 +188,13 @@ void ui_push_menu_item_separator(); bool ui_push_dropdown(dropdown_state *state, char *title); bool ui_push_dropdown_item(image *icon, char *title, s32 index); void ui_push_separator(); +void ui_push_rect(s32 w, color rec); void ui_push_vertical_dragbar(); void ui_block_begin(layout_direction direction); void ui_block_end(); void ui_end_menu_bar(); void ui_push_text(char *text); +bool ui_push_text_width(char *text, s32 maxw, bool active); void ui_push_textf(font *f, char *text); void ui_push_textf_width(font *f, char *text, s32 maxw); bool ui_push_hypertext_link(char *text); diff --git a/src/windows/platform.c b/src/windows/platform.c index 2187806..12c9065 100644 --- a/src/windows/platform.c +++ b/src/windows/platform.c @@ -285,7 +285,7 @@ void platform_create_config_directory() char tmp[PATH_MAX]; if(SUCCEEDED(SHGetFolderPathA(0, CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE, NULL, 0, tmp))) { - string_appendn(tmp, "/text-search", PATH_MAX); + string_appendn(tmp, "/moedit", PATH_MAX); } @@ -299,7 +299,7 @@ char* get_config_save_location(char *buffer) { if(SUCCEEDED(SHGetFolderPathA(0, CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE, NULL, 0, buffer))) { - string_appendn(buffer, "\\text-search\\config.txt", MAX_INPUT_LENGTH); + string_appendn(buffer, "\\moedit\\config.txt", MAX_INPUT_LENGTH); return buffer; } |
