diff --git a/4coder_custom.h b/4coder_custom.h index 490fb2ab..b9bec6b6 100644 --- a/4coder_custom.h +++ b/4coder_custom.h @@ -5,7 +5,10 @@ #include "4coder_version.h" #include "4coder_keycodes.h" #include "4coder_style.h" +#include "4coder_rect.h" +#include "4coder_mem.h" #include "4coder_buffer_types.h" +#include "4coder_gui.h" #ifndef FRED_STRING_STRUCT #define FRED_STRING_STRUCT @@ -311,9 +314,10 @@ view_summary_zero(){ return(summary); } -#define UserInputKey 0 -#define UserInputMouse 1 - +enum User_Input_Type{ + UserInputKey, + UserInputMouse +}; struct User_Input{ int type; int abort; @@ -331,13 +335,22 @@ struct Query_Bar{ String string; }; +enum Event_Message_Type{ + EM_Open_View, + EM_Frame, + EM_Close_View +}; +struct Event_Message{ + int type; +}; + struct Theme_Color{ Style_Tag tag; unsigned int color; }; -#define VIEW_ROUTINE_SIG(name) void name(int view_id) +#define VIEW_ROUTINE_SIG(name) void name(struct Application_Links *app, int view_id) #define GET_BINDING_DATA(name) int name(void *data, int size) #define CUSTOM_COMMAND_SIG(name) void name(struct Application_Links *app) #define HOOK_SIG(name) int name(struct Application_Links *app) @@ -403,11 +416,14 @@ struct Application_Links; // Directly get user input #define GET_USER_INPUT_SIG(n) User_Input n(Application_Links *app, unsigned int get_type, unsigned int abort_type) #define GET_COMMAND_INPUT_SIG(n) User_Input n(Application_Links *app) +#define GET_EVENT_MESSAGE_SIG(n) Event_Message n(Application_Links *app) // Queries and information display #define START_QUERY_BAR_SIG(n) int n(Application_Links *app, Query_Bar *bar, unsigned int flags) #define END_QUERY_BAR_SIG(n) void n(Application_Links *app, Query_Bar *bar, unsigned int flags) #define PRINT_MESSAGE_SIG(n) void n(Application_Links *app, char *string, int len) +#define GET_GUI_FUNCTIONS_SIG(n) GUI_Functions* n(Application_Links *app); +#define GET_GUI_SIG(n) GUI* n(Application_Links *app, int view_id); // Color settings #define CHANGE_THEME_SIG(n) void n(Application_Links *app, char *name, int len) @@ -415,6 +431,7 @@ struct Application_Links; #define SET_THEME_COLORS_SIG(n) void n(Application_Links *app, Theme_Color *colors, int count) + // Boundry type flags #define BoundryWhitespace 0x1 #define BoundryToken 0x2 @@ -487,11 +504,14 @@ extern "C"{ // Directly get user input typedef GET_USER_INPUT_SIG(Get_User_Input_Function); typedef GET_COMMAND_INPUT_SIG(Get_Command_Input_Function); + typedef GET_EVENT_MESSAGE_SIG(Get_Event_Message_Function); - // Queries + // GUI typedef START_QUERY_BAR_SIG(Start_Query_Bar_Function); typedef END_QUERY_BAR_SIG(End_Query_Bar_Function); typedef PRINT_MESSAGE_SIG(Print_Message_Function); + typedef GET_GUI_FUNCTIONS_SIG(Get_GUI_Functions_Function); + typedef GET_GUI_SIG(Get_GUI_Function); // Color settings typedef CHANGE_THEME_SIG(Change_Theme_Function); @@ -554,11 +574,16 @@ struct Application_Links{ Get_User_Input_Function *get_user_input; Get_Command_Input_Function *get_command_input; + Get_Event_Message_Function *get_event_message; + // Queries Start_Query_Bar_Function *start_query_bar; End_Query_Bar_Function *end_query_bar; Print_Message_Function *print_message; + Get_GUI_Functions_Function *get_gui_functions; + Get_GUI_Function *get_gui; + // Theme Change_Theme_Function *change_theme; Change_Font_Function *change_font; diff --git a/4coder_default_bindings.cpp b/4coder_default_bindings.cpp index 8111b81d..6d9e6d2a 100644 --- a/4coder_default_bindings.cpp +++ b/4coder_default_bindings.cpp @@ -445,6 +445,94 @@ get_bindings(void *data, int size){ return(result); } +struct Custom_Vars{ + int initialized; + Partition part; +}; + +enum View_Mode{ + ViewMode_File, +}; + +struct View_Vars{ + int id; + View_Mode mode; + + GUI_Scroll_Vars scroll; + i32_Rect scroll_region; + + int buffer_id; +}; +inline View_Vars +view_vars_zero(){ + View_Vars vars = {0}; + return(vars); +} + +extern "C" void +view_routine(Application_Links *app, int view_id){ + Custom_Vars *vars = (Custom_Vars*)app->memory; + View_Vars view = {0}; + view.id = view_id; + + int show_scrollbar = 1; + + if (!vars->initialized){ + vars->initialized = 1; + vars->part = make_part(app->memory, app->memory_size); + push_struct(&vars->part, Custom_Vars); + } + + for(;;){ + Event_Message message = {0}; + message = app->get_event_message(app); + + switch (message.type){ + case EM_Open_View: + { + view = view_vars_zero(); + view.id = view_id; + }break; + + case EM_Frame: + { + GUI_Functions *guifn = app->get_gui_functions(app); + GUI *gui = app->get_gui(app, view_id); + + guifn->begin(gui); + guifn->top_bar(gui); + + switch (view.mode){ + case ViewMode_File: + // TODO(allen): Overlapped widget + GUI_id scroll_id; + scroll_id.id[1] = view.mode; + scroll_id.id[0] = view.buffer_id; + + guifn->get_scroll_vars(gui, scroll_id, &view.scroll, + &view.scroll_region); + guifn->begin_scrollable(gui, scroll_id, view.scroll, + 144.f, show_scrollbar); + guifn->file(gui, view.buffer_id); + guifn->end_scrollable(gui); + break; + } + + guifn->end(gui); + + // TODO(allen): Put this code in charge of dispatching + // to the command or command coroutine or whatever. + + // TODO(allen): Put this code in charge of when to process + // the GUI with input and retrieve new layout data. + }break; + + case EM_Close_View: + {}break; + } + } +} + #endif // BOTTOM diff --git a/4coder_gui.h b/4coder_gui.h new file mode 100644 index 00000000..2771b490 --- /dev/null +++ b/4coder_gui.h @@ -0,0 +1,71 @@ + +#ifndef FCODER_GUI_H +#define FCODER_GUI_H + +#include + +struct GUI_id{ + uint64_t id[2]; +}; +inline GUI_id +gui_id_zero(){ + GUI_id id = {0}; + return(id); +} + +struct GUI_Scroll_Vars{ + float scroll_y; + float target_y; + float prev_target_y; + float min_y, max_y; + + float scroll_x; + float target_x; + float prev_target_x; +}; + +typedef struct GUI GUI; + +#define GUI_BEGIN_SIG(n) void n(GUI *gui) +#define GUI_END_SIG(n) void n(GUI *gui) + +#define GUI_TOP_BAR_SIG(n) void n(GUI *gui) + +// TODO(allen): Do we want to break this call +// down a little more? I think maybe we do. +#define GUI_GET_SCROLL_VARS_SIG(n) void n(GUI *gui, GUI_id scroll_id, GUI_Scroll_Vars *vars, i32_Rect *region) + +#define GUI_BEGIN_SCROLLABLE_SIG(n) int n(GUI *gui, GUI_id scroll_id, GUI_Scroll_Vars vars, float delta, int show_scrollbar) +#define GUI_END_SCROLLABLE_SIG(n) void n(GUI *gui) + +#define GUI_FILE_SIG(n) void n(GUI *gui, int buffer_id) + +extern "C"{ + typedef GUI_BEGIN_SIG(GUI_Begin_Function); + typedef GUI_END_SIG(GUI_End_Function); + + typedef GUI_TOP_BAR_SIG(GUI_Top_Bar_Function); + + typedef GUI_GET_SCROLL_VARS_SIG(GUI_Get_Scroll_Vars_Function); + + typedef GUI_BEGIN_SCROLLABLE_SIG(GUI_Begin_Scrollable_Function); + typedef GUI_END_SCROLLABLE_SIG(GUI_End_Scrollable_Function); + + typedef GUI_FILE_SIG(GUI_File_Function); +} + +struct GUI_Functions{ + GUI_Begin_Function *begin; + GUI_End_Function *end; + + GUI_Top_Bar_Function *top_bar; + + GUI_Get_Scroll_Vars_Function *get_scroll_vars; + + GUI_Begin_Scrollable_Function *begin_scrollable; + GUI_End_Scrollable_Function *end_scrollable; + + GUI_File_Function *file; +}; + +#endif diff --git a/4coder_mem.h b/4coder_mem.h new file mode 100644 index 00000000..02f0556c --- /dev/null +++ b/4coder_mem.h @@ -0,0 +1,75 @@ + +#ifndef FCODER_MEM_H +#define FCODER_MEM_H + +struct Partition{ + char *base; + int pos, max; +}; + +struct Temp_Memory{ + void *handle; + int pos; +}; + +inline Partition +make_part(void *memory, int size){ + Partition partition; + partition.base = (char*)memory; + partition.pos = 0; + partition.max = size; + return partition; +} + +inline void* +partition_allocate(Partition *data, int size){ + void *ret = 0; + if (size > 0 && data->pos + size <= data->max){ + ret = data->base + data->pos; + data->pos += size; + } + return ret; +} + +inline void +partition_align(Partition *data, unsigned int boundary){ + --boundary; + data->pos = (data->pos + boundary) & (~boundary); +} + +inline void* +partition_current(Partition *data){ + return data->base + data->pos; +} + +inline int +partition_remaining(Partition *data){ + return data->max - data->pos; +} + +inline Partition +partition_sub_part(Partition *data, int size){ + Partition result = {}; + void *d = partition_allocate(data, size); + if (d) result = make_part(d, size); + return result; +} + +#define push_struct(part, T) (T*)partition_allocate(part, sizeof(T)) +#define push_array(part, T, size) (T*)partition_allocate(part, sizeof(T)*(size)) +#define push_block(part, size) partition_allocate(part, size) + +inline Temp_Memory +begin_temp_memory(Partition *data){ + Temp_Memory result; + result.handle = data; + result.pos = data->pos; + return result; +} + +inline void +end_temp_memory(Temp_Memory temp){ + ((Partition*)temp.handle)->pos = temp.pos; +} + +#endif \ No newline at end of file diff --git a/4coder_rect.h b/4coder_rect.h new file mode 100644 index 00000000..3a4283d9 --- /dev/null +++ b/4coder_rect.h @@ -0,0 +1,146 @@ + +#ifndef FCODER_RECT_H +#define FCODER_RECT_H + +#include + +struct i32_Rect{ + int32_t x0, y0; + int32_t x1, y1; +}; + +struct f32_Rect{ + float x0, y0; + float x1, y1; +}; + +inline i32_Rect +i32_rect_zero(){ + i32_Rect rect={0}; + return(rect); +} + +inline f32_Rect +f32_rect_zero(){ + f32_Rect rect={0}; + return(rect); +} + +inline i32_Rect +i32R(int32_t l, int32_t t, int32_t r, int32_t b){ + i32_Rect rect; + rect.x0 = l; rect.y0 = t; + rect.x1 = r; rect.y1 = b; + return rect; +} + +inline i32_Rect +i32R(f32_Rect r){ + i32_Rect rect; + rect.x0 = (int32_t)r.x0; + rect.y0 = (int32_t)r.y0; + rect.x1 = (int32_t)r.x1; + rect.y1 = (int32_t)r.y1; + return rect; +} + +inline i32_Rect +i32XYWH(int32_t x, int32_t y, int32_t w, int32_t h){ + i32_Rect rect; + rect.x0 = x; rect.y0 = y; + rect.x1 = x+w; rect.y1 = y+h; + return rect; +} + +inline f32_Rect +f32R(float l, float t, float r, float b){ + f32_Rect rect; + rect.x0 = l; rect.y0 = t; + rect.x1 = r; rect.y1 = b; + return rect; +} + +inline f32_Rect +f32R(i32_Rect r){ + f32_Rect rect; + rect.x0 = (float)r.x0; + rect.y0 = (float)r.y0; + rect.x1 = (float)r.x1; + rect.y1 = (float)r.y1; + return rect; +} + +inline f32_Rect +f32XYWH(float x, float y, float w, float h){ + f32_Rect rect; + rect.x0 = x; rect.y0 = y; + rect.x1 = x+w; rect.y1 = y+h; + return rect; +} + +inline int +rect_equal(i32_Rect r1, i32_Rect r2){ + int result = (r1.x0 == r2.x0 && + r1.y0 == r2.y0 && + r1.x1 == r2.x1 && + r1.y1 == r2.y1); + return(result); +} + +inline int +hit_check(int32_t x, int32_t y, int32_t x0, int32_t y0, int32_t x1, int32_t y1){ + return (x >= x0 && x < x1 && y >= y0 && y < y1); +} + +inline int +hit_check(int32_t x, int32_t y, i32_Rect rect){ + return (hit_check(x, y, rect.x0, rect.y0, rect.x1, rect.y1)); +} + +inline int +hit_check(int32_t x, int32_t y, float x0, float y0, float x1, float y1){ + return (x >= x0 && x < x1 && y >= y0 && y < y1); +} + +inline int +hit_check(int32_t x, int32_t y, f32_Rect rect){ + return (hit_check(x, y, rect.x0, rect.y0, rect.x1, rect.y1)); +} + +inline int +positive_area(i32_Rect rect){ + return (rect.x0 < rect.x1 && rect.y0 < rect.y1); +} + +inline i32_Rect +get_inner_rect(i32_Rect outer, int32_t margin){ + i32_Rect r; + r.x0 = outer.x0 + margin; + r.y0 = outer.y0 + margin; + r.x1 = outer.x1 - margin; + r.y1 = outer.y1 - margin; + return r; +} + +inline int +fits_inside(i32_Rect rect, i32_Rect outer){ + return (rect.x0 >= outer.x0 && rect.x1 <= outer.x1 && + rect.y0 >= outer.y0 && rect.y1 <= outer.y1); +} + +inline i32_Rect +rect_clamp_to_rect(i32_Rect rect, i32_Rect clamp_box){ + if (rect.x0 < clamp_box.x0) rect.x0 = clamp_box.x0; + if (rect.y0 < clamp_box.y0) rect.y0 = clamp_box.y0; + if (rect.x1 > clamp_box.x1) rect.x1 = clamp_box.x1; + if (rect.y1 > clamp_box.y1) rect.y1 = clamp_box.y1; + return rect; +} + +inline i32_Rect +rect_clamp_to_rect(int32_t left, int32_t top, int32_t right, int32_t bottom, i32_Rect clamp_box){ + return rect_clamp_to_rect(i32R(left, top, right, bottom), clamp_box); +} + + +#endif \ No newline at end of file diff --git a/4ed.cpp b/4ed.cpp index f5a98e37..0bb68d4a 100644 --- a/4ed.cpp +++ b/4ed.cpp @@ -2513,8 +2513,9 @@ command_caller(Coroutine *coroutine){ internal void view_caller(Coroutine *coroutine){ View *view = (View*)coroutine->in; + View_Persistent *persistent = &view->persistent; - view->view_routine(view->id); + persistent->view_routine(&persistent->models->app_links, persistent->id); } internal void @@ -3027,7 +3028,7 @@ app_vars_zero(){ internal App_Vars* app_setup_memory(Application_Memory *memory){ - Partition _partition = partition_open(memory->vars_memory, memory->vars_memory_size); + Partition _partition = make_part(memory->vars_memory, memory->vars_memory_size); App_Vars *vars = push_struct(&_partition, App_Vars); Assert(vars); *vars = app_vars_zero(); @@ -3151,7 +3152,8 @@ App_Init_Sig(app_init){ } { - View *vptr = 0; + View *view = 0; + View_Persistent *persistent = 0; i32 i = 0; i32 max = 0; @@ -3163,31 +3165,14 @@ App_Init_Sig(app_init){ dll_init_sentinel(&vars->live_set.free_sentinel); max = vars->live_set.max; - vptr = vars->live_set.views; - for (i = 0; i < max; ++i, ++vptr){ - dll_insert(&vars->live_set.free_sentinel, vptr); - } - } - - { - Panel *panel = 0, *used_panels = 0; - View *view = 0; - - used_panels = &models->layout.used_sentinel; - for (dll_items(panel, used_panels)){ - view = panel->view; + view = vars->live_set.views; + for (i = 0; i < max; ++i, ++view){ + dll_insert(&vars->live_set.free_sentinel, view); - view->view_routine = models->config_api.view_routine; - view->coroutine = - system->create_coroutine(view_caller); - - view->coroutine = - system->launch_coroutine(view->coroutine, view, view); - - if (!view->coroutine){ - // TODO(allen): Error message and recover - NotImplemented; - } + persistent = &view->persistent; + persistent->id = i; + persistent->models = models; + persistent->view_routine = models->config_api.view_routine; } } @@ -3774,6 +3759,35 @@ App_Step_Sig(app_step){ cmd->part = partition_sub_part(&models->mem.part, 16 << 10); if (first_step){ + { + View *view = 0; + View_Persistent *persistent = 0; + i32 i = 0; + i32 max = 0; + + max = vars->live_set.max; + view = vars->live_set.views; + for (i = 1; i <= max; ++i, ++view){ + persistent = &view->persistent; + + persistent->coroutine = + system->create_coroutine(view_caller); + + models->command_coroutine = persistent->coroutine; + persistent->coroutine = + system->launch_coroutine(persistent->coroutine, + view, + &view->persistent.coroutine_flags); + + if (!persistent->coroutine){ + // TODO(allen): Error message and recover + NotImplemented; + } + } + + models->command_coroutine = 0; + } + General_Memory *general = &models->mem.general; Editing_File *file = working_set_alloc_always(&models->working_set, general); file_create_read_only(system, models, file, "*messages*"); diff --git a/4ed_app_target.cpp b/4ed_app_target.cpp index 9b46d1a9..544c875a 100644 --- a/4ed_app_target.cpp +++ b/4ed_app_target.cpp @@ -9,6 +9,8 @@ // TOP +#include "4coder_custom.h" + #include "4ed_config.h" #define BUFFER_EXPERIMENT_SCALPEL 0 @@ -22,7 +24,6 @@ #include "4ed_mem.cpp" #include "4ed_math.cpp" -#include "4coder_custom.h" #include "4ed_system.h" #include "4ed_rendering.h" #include "4ed.h" diff --git a/4ed_file_view.cpp b/4ed_file_view.cpp index 459d03cc..af3c712b 100644 --- a/4ed_file_view.cpp +++ b/4ed_file_view.cpp @@ -204,18 +204,23 @@ context_eq(Scroll_Context a, Scroll_Context b){ return(result); } -struct View{ - View *next, *prev; - Panel *panel; - b32 in_use; +struct View_Persistent{ i32 id; - Coroutine *coroutine; View_Routine_Function *view_routine; + Coroutine *coroutine; + u32 coroutine_flags[2]; // TODO(allen): eliminate this models pointer: explicitly parameterize. Models *models; +}; + +struct View{ + View_Persistent persistent; + View *next, *prev; + Panel *panel; + b32 in_use; Command_Map *map; File_Viewing_Data file_data; @@ -268,6 +273,16 @@ struct View{ b32 reinit_scrolling; }; +inline void* +get_view_body(View *view){ + char *result = (char*)view; + result += sizeof(View_Persistent); + return(result); +} +inline i32 +get_view_size(){ + return(sizeof(View) - sizeof(View_Persistent)); +} struct View_And_ID{ View *view; @@ -1256,7 +1271,7 @@ file_post_history(General_Memory *general, Editing_File *file, inline Full_Cursor view_compute_cursor_from_pos(View *view, i32 pos){ Editing_File *file = view->file_data.file; - Models *models = view->models; + Models *models = view->persistent.models; Render_Font *font = get_font_info(models->font_set, models->global_font.font_id)->font; Full_Cursor result = {}; @@ -1271,7 +1286,7 @@ view_compute_cursor_from_pos(View *view, i32 pos){ inline Full_Cursor view_compute_cursor_from_unwrapped_xy(View *view, f32 seek_x, f32 seek_y, b32 round_down = 0){ Editing_File *file = view->file_data.file; - Models *models = view->models; + Models *models = view->persistent.models; Render_Font *font = get_font_info(models->font_set, models->global_font.font_id)->font; Full_Cursor result = {}; @@ -1287,7 +1302,7 @@ view_compute_cursor_from_unwrapped_xy(View *view, f32 seek_x, f32 seek_y, b32 ro internal Full_Cursor view_compute_cursor_from_wrapped_xy(View *view, f32 seek_x, f32 seek_y, b32 round_down = 0){ Editing_File *file = view->file_data.file; - Models *models = view->models; + Models *models = view->persistent.models; Render_Font *font = get_font_info(models->font_set, models->global_font.font_id)->font; Full_Cursor result = {}; @@ -1304,7 +1319,7 @@ view_compute_cursor_from_wrapped_xy(View *view, f32 seek_x, f32 seek_y, b32 roun internal Full_Cursor view_compute_cursor_from_line_pos(View *view, i32 line, i32 pos){ Editing_File *file = view->file_data.file; - Models *models = view->models; + Models *models = view->persistent.models; Render_Font *font = get_font_info(models->font_set, models->global_font.font_id)->font; Full_Cursor result = {}; @@ -1480,7 +1495,7 @@ view_move_view_to_cursor(View *view, GUI_Scroll_Vars *scroll){ inline void file_view_nullify_file(View *view){ - General_Memory *general = &view->models->mem.general; + General_Memory *general = &view->persistent.models->mem.general; if (view->file_data.line_wrap_y){ general_memory_free(general, view->file_data.line_wrap_y); } @@ -1498,7 +1513,7 @@ view_set_file(View *view, Editing_File *file, Models *models){ file_view_nullify_file(view); view->file_data.file = file; - + if (file){ u64 unique_buffer_id = file->unique_buffer_id; Recent_File_Data *recent = view->recent; @@ -1506,9 +1521,9 @@ view_set_file(View *view, Editing_File *file, Models *models){ i32 i = 0; i32 max = ArrayCount(view->recent)-1; b32 found_recent_entry = 0; - + view->file_data.unwrapped_lines = file->settings.unwrapped_lines; - + for (; i < max; ++i, ++recent){ if (recent->unique_buffer_id == unique_buffer_id){ temp_recent = *recent; @@ -2944,7 +2959,7 @@ internal void remeasure_file_view(System_Functions *system, View *view){ if (file_is_ready(view->file_data.file)){ Relative_Scrolling relative = view_get_relative_scrolling(view); - view_measure_wraps(&view->models->mem.general, view); + view_measure_wraps(&view->persistent.models->mem.general, view); view_cursor_move(view, view->recent->cursor.pos); view->recent->preferred_x = view_get_cursor_x(view); view_set_relative_scrolling(view, relative); @@ -2970,7 +2985,7 @@ view_show_interactive(System_Functions *system, View *view, Command_Map *gui_map, Interactive_Action action, Interactive_Interaction interaction, String query){ - Models *models = view->models; + Models *models = view->persistent.models; view->showing_ui = VUI_Interactive; view->action = action; @@ -2999,10 +3014,10 @@ inline void view_show_file(View *view){ Editing_File *file = view->file_data.file; if (file){ - view->map = get_map(view->models, file->settings.base_map_id); + view->map = get_map(view->persistent.models, file->settings.base_map_id); } else{ - view->map = get_map(view->models, mapid_global); + view->map = get_map(view->persistent.models, mapid_global); } view->showing_ui = VUI_None; view->current_scroll = &view->recent->scroll; @@ -3010,7 +3025,7 @@ view_show_file(View *view){ internal void interactive_view_complete(View *view, String dest, i32 user_action){ - Models *models = view->models; + Models *models = view->persistent.models; Panel *panel = view->panel; Editing_File *old_file = view->file_data.file; @@ -3094,7 +3109,7 @@ update_highlighting(View *view){ return; } - Models *models = view->models; + Models *models = view->persistent.models; Style *style = &models->style; i32 pos = view_get_cursor_pos(file_view); @@ -3624,7 +3639,7 @@ app_single_number_input_step(System_Functions *system, Key_Event_Data key, Strin internal b32 step_file_view(System_Functions *system, View *view, View *active_view, Input_Summary input){ GUI_Target *target = &view->gui_target; - Models *models = view->models; + Models *models = view->persistent.models; Key_Summary keys = input.keys; b32 show_scrollbar = !view->hide_scrollbar; @@ -4470,9 +4485,9 @@ do_input_file_view(System_Functions *system, Exchange *exchange, if (scroll_vars.target_x != scroll_vars.prev_target_x) is_new_target = 1; if (scroll_vars.target_y != scroll_vars.prev_target_y) is_new_target = 1; - if (view->models->scroll_rule(scroll_vars.target_x, scroll_vars.target_y, + if (view->persistent.models->scroll_rule(scroll_vars.target_x, scroll_vars.target_y, &scroll_vars.scroll_x, &scroll_vars.scroll_y, - (view->id) + 1, is_new_target)){ + (view->persistent.id) + 1, is_new_target)){ result.is_animating = 1; } @@ -4507,7 +4522,7 @@ do_input_file_view(System_Functions *system, Exchange *exchange, internal i32 draw_file_loaded(View *view, i32_Rect rect, b32 is_active, Render_Target *target){ - Models *models = view->models; + Models *models = view->persistent.models; Editing_File *file = view->file_data.file; Style *style = main_style(models); i32 line_height = view->font_height; @@ -4675,7 +4690,7 @@ draw_file_loaded(View *view, i32_Rect rect, b32 is_active, Render_Target *target internal void draw_text_field(Render_Target *target, View *view, i32_Rect rect, String p, String t){ - Models *models = view->models; + Models *models = view->persistent.models; Style *style = main_style(models); u32 back_color = style->main.margin_color; @@ -4696,7 +4711,7 @@ draw_text_field(Render_Target *target, View *view, i32_Rect rect, String p, Stri internal void draw_text_with_cursor(Render_Target *target, View *view, i32_Rect rect, String s, i32 pos){ - Models *models = view->models; + Models *models = view->persistent.models; Style *style = main_style(models); u32 back_color = style->main.margin_color; @@ -4742,7 +4757,7 @@ draw_text_with_cursor(Render_Target *target, View *view, i32_Rect rect, String s internal void draw_file_bar(Render_Target *target, View *view, Editing_File *file, i32_Rect rect){ File_Bar bar; - Models *models = view->models; + Models *models = view->persistent.models; Style_Font *font = &models->global_font; Style *style = main_style(models); Interactive_Style bar_style = style->main.file_info_style; @@ -4839,7 +4854,7 @@ get_margin_color(i32 active_level, Style *style){ internal void draw_color_button(GUI_Target *gui_target, Render_Target *target, View *view, i32_Rect rect, GUI_id id, u32 fore, u32 back, String text){ - Models *models = view->models; + Models *models = view->persistent.models; i32 active_level = gui_active_level(gui_target, id); i16 font_id = models->global_font.font_id; @@ -4855,7 +4870,7 @@ draw_color_button(GUI_Target *gui_target, Render_Target *target, View *view, internal void draw_font_button(GUI_Target *gui_target, Render_Target *target, View *view, i32_Rect rect, GUI_id id, i16 font_id, String text){ - Models *models = view->models; + Models *models = view->persistent.models; Style *style = main_style(models); i32 active_level = gui_active_level(gui_target, id); @@ -4872,7 +4887,7 @@ draw_font_button(GUI_Target *gui_target, Render_Target *target, View *view, internal void draw_fat_option_block(GUI_Target *gui_target, Render_Target *target, View *view, i32_Rect rect, GUI_id id, String text, String pop, i8 checkbox = -1){ - Models *models = view->models; + Models *models = view->persistent.models; Style *style = main_style(models); i32 active_level = gui_active_level(gui_target, id); @@ -4913,7 +4928,7 @@ draw_fat_option_block(GUI_Target *gui_target, Render_Target *target, View *view, internal void draw_button(GUI_Target *gui_target, Render_Target *target, View *view, i32_Rect rect, GUI_id id, String text){ - Models *models = view->models; + Models *models = view->persistent.models; Style *style = main_style(models); i32 active_level = gui_active_level(gui_target, id); @@ -4939,7 +4954,7 @@ draw_button(GUI_Target *gui_target, Render_Target *target, View *view, i32_Rect internal void draw_style_preview(GUI_Target *gui_target, Render_Target *target, View *view, i32_Rect rect, GUI_id id, Style *style){ - Models *models = view->models; + Models *models = view->persistent.models; i32 active_level = gui_active_level(gui_target, id); i16 font_id = models->global_font.font_id; @@ -5088,7 +5103,7 @@ do_render_file_view(System_Functions *system, Exchange *exchange, { GUI_Interactive *b = (GUI_Interactive*)h; i32 style_index = *(i32*)(b + 1); - Style *style = get_style(view->models, style_index); + Style *style = get_style(view->persistent.models, style_index); draw_style_preview(gui_target, target, view, gui_session.rect, b->id, style); }break; @@ -5120,7 +5135,7 @@ do_render_file_view(System_Functions *system, Exchange *exchange, case guicom_scrollable_bar: { - Models *models = view->models; + Models *models = view->persistent.models; Style *style = main_style(models); u32 back; @@ -5145,7 +5160,7 @@ do_render_file_view(System_Functions *system, Exchange *exchange, case guicom_scrollable_bottom: { GUI_id id; - Models *models = view->models; + Models *models = view->persistent.models; Style *style = main_style(models); i32_Rect box = gui_session.rect; @@ -5223,7 +5238,7 @@ kill_file(System_Functions *system, Exchange *exchange, Models *models, Editing_ inline void file_view_free_buffers(View *view){ - General_Memory *general = &view->models->mem.general; + General_Memory *general = &view->persistent.models->mem.general; if (view->file_data.line_wrap_y){ general_memory_free(general, view->file_data.line_wrap_y); view->file_data.line_wrap_y = 0; @@ -5444,16 +5459,16 @@ live_set_alloc_view(Live_Views *live_set, Panel *panel, Models *models){ result.view = live_set->free_sentinel.next; result.id = (i32)(result.view - live_set->views); + Assert(result.id == result.view->persistent.id); dll_remove(result.view); - memset(result.view, 0, sizeof(*result.view)); - result.view->id = result.id; + memset(get_view_body(result.view), 0, get_view_size()); result.view->in_use = 1; panel->view = result.view; result.view->panel = panel; - result.view->models = models; + result.view->persistent.models = models; result.view->scrub_max = 1; result.view->current_scroll = &result.view->recent->scroll; @@ -5464,7 +5479,7 @@ live_set_alloc_view(Live_Views *live_set, Panel *panel, Models *models){ void *gui_mem = general_memory_allocate(&models->mem.general, gui_mem_size + 8, 0); result.view->gui_mem = gui_mem; gui_mem = advance_to_alignment(gui_mem); - result.view->gui_target.push = partition_open(gui_mem, gui_mem_size); + result.view->gui_target.push = make_part(gui_mem, gui_mem_size); } return(result); diff --git a/4ed_gui.cpp b/4ed_gui.cpp index 939c2e6b..c43c5503 100644 --- a/4ed_gui.cpp +++ b/4ed_gui.cpp @@ -112,26 +112,6 @@ super_color_post_byte(Super_Color *color, i32 channel, u8 byte){ return packed; } -struct GUI_id{ - u64 id[2]; -}; -inline GUI_id -gui_id_zero(){ - GUI_id id = {0}; - return(id); -} - -struct GUI_Scroll_Vars{ - f32 scroll_y; - f32 target_y; - f32 prev_target_y; - f32 min_y, max_y; - - f32 scroll_x; - f32 target_x; - f32 prev_target_x; -}; - struct GUI_Target{ Partition push; diff --git a/4ed_math.cpp b/4ed_math.cpp index 58197861..86919308 100644 --- a/4ed_math.cpp +++ b/4ed_math.cpp @@ -27,35 +27,29 @@ ABS(f32 x){ return x; } +#if C_MATH inline f32 MOD(f32 x, i32 m){ -#if C_MATH f32 whole, frac; frac = modff(x, &whole); return ((i32)(whole) % m) + frac; -#endif } inline f32 SQRT(f32 x){ -#if C_MATH return sqrt(x); -#endif } inline f32 SIN(f32 x_degrees){ -#if C_MATH return sinf(x_degrees * DEG_TO_RAD); -#endif } inline f32 COS(f32 x_degrees){ -#if C_MATH return cosf(x_degrees * DEG_TO_RAD); -#endif } +#endif /* * Rounding @@ -94,148 +88,6 @@ ROUNDPOT32(u32 v){ return v; } -/* - * Rectangles - */ - -struct i32_Rect{ - i32 x0, y0; - i32 x1, y1; -}; - -struct f32_Rect{ - f32 x0, y0; - f32 x1, y1; -}; - -inline i32_Rect -i32_rect_zero(){ - i32_Rect rect={0}; - return(rect); -} - -inline f32_Rect -f32_rect_zero(){ - f32_Rect rect={0}; - return(rect); -} - -inline i32_Rect -i32R(i32 l, i32 t, i32 r, i32 b){ - i32_Rect rect; - rect.x0 = l; rect.y0 = t; - rect.x1 = r; rect.y1 = b; - return rect; -} - -inline i32_Rect -i32R(f32_Rect r){ - i32_Rect rect; - rect.x0 = (i32)r.x0; - rect.y0 = (i32)r.y0; - rect.x1 = (i32)r.x1; - rect.y1 = (i32)r.y1; - return rect; -} - -inline i32_Rect -i32XYWH(i32 x, i32 y, i32 w, i32 h){ - i32_Rect rect; - rect.x0 = x; rect.y0 = y; - rect.x1 = x+w; rect.y1 = y+h; - return rect; -} - -inline f32_Rect -f32R(f32 l, f32 t, f32 r, f32 b){ - f32_Rect rect; - rect.x0 = l; rect.y0 = t; - rect.x1 = r; rect.y1 = b; - return rect; -} - -inline f32_Rect -f32R(i32_Rect r){ - f32_Rect rect; - rect.x0 = (f32)r.x0; - rect.y0 = (f32)r.y0; - rect.x1 = (f32)r.x1; - rect.y1 = (f32)r.y1; - return rect; -} - -inline f32_Rect -f32XYWH(f32 x, f32 y, f32 w, f32 h){ - f32_Rect rect; - rect.x0 = x; rect.y0 = y; - rect.x1 = x+w; rect.y1 = y+h; - return rect; -} - -inline b32 -rect_equal(i32_Rect r1, i32_Rect r2){ - b32 result = (r1.x0 == r2.x0 && - r1.y0 == r2.y0 && - r1.x1 == r2.x1 && - r1.y1 == r2.y1); - return(result); -} - -inline b32 -hit_check(i32 x, i32 y, i32 x0, i32 y0, i32 x1, i32 y1){ - return (x >= x0 && x < x1 && y >= y0 && y < y1); -} - -inline b32 -hit_check(i32 x, i32 y, i32_Rect rect){ - return (hit_check(x, y, rect.x0, rect.y0, rect.x1, rect.y1)); -} - -inline b32 -hit_check(i32 x, i32 y, f32 x0, f32 y0, f32 x1, f32 y1){ - return (x >= x0 && x < x1 && y >= y0 && y < y1); -} - -inline b32 -hit_check(i32 x, i32 y, f32_Rect rect){ - return (hit_check(x, y, rect.x0, rect.y0, rect.x1, rect.y1)); -} - -inline b32 -positive_area(i32_Rect rect){ - return (rect.x0 < rect.x1 && rect.y0 < rect.y1); -} - -inline i32_Rect -get_inner_rect(i32_Rect outer, i32 margin){ - i32_Rect r; - r.x0 = outer.x0 + margin; - r.y0 = outer.y0 + margin; - r.x1 = outer.x1 - margin; - r.y1 = outer.y1 - margin; - return r; -} - -inline b32 -fits_inside(i32_Rect rect, i32_Rect outer){ - return (rect.x0 >= outer.x0 && rect.x1 <= outer.x1 && - rect.y0 >= outer.y0 && rect.y1 <= outer.y1); -} - -inline i32_Rect -rect_clamp_to_rect(i32_Rect rect, i32_Rect clamp_box){ - if (rect.x0 < clamp_box.x0) rect.x0 = clamp_box.x0; - if (rect.y0 < clamp_box.y0) rect.y0 = clamp_box.y0; - if (rect.x1 > clamp_box.x1) rect.x1 = clamp_box.x1; - if (rect.y1 > clamp_box.y1) rect.y1 = clamp_box.y1; - return rect; -} - -inline i32_Rect -rect_clamp_to_rect(i32 left, i32 top, i32 right, i32 bottom, i32_Rect clamp_box){ - return rect_clamp_to_rect(i32R(left, top, right, bottom), clamp_box); -} - /* * Vectors */ @@ -576,6 +428,7 @@ rotate(Vec2 v, real32 theta_degrees){ return result; } +#if 0 /* * Coordinates */ @@ -606,6 +459,7 @@ invert(Matrix2 m){ result.y_axis.y = m.x_axis.x*det; return result; } +#endif /* * Lerps, Clamps, Thresholds, Etc diff --git a/4ed_mem.cpp b/4ed_mem.cpp index ad0b1f31..535dbee8 100644 --- a/4ed_mem.cpp +++ b/4ed_mem.cpp @@ -9,16 +9,6 @@ // TOP -struct Partition{ - u8 *base; - i32 pos, max; -}; - -struct Temp_Memory{ - void *handle; - int pos; -}; - enum Memory_Bubble_Flag{ MEM_BUBBLE_USED = 0x1, MEM_BUBBLE_DEBUG = 0xD3000000, @@ -44,53 +34,6 @@ struct Mem_Options{ General_Memory general; }; -inline Partition -partition_open(void *memory, i32 size){ - Partition partition; - partition.base = (u8*)memory; - partition.pos = 0; - partition.max = size; - return partition; -} - -inline void* -partition_allocate(Partition *data, i32 size){ - void *ret = 0; - if (size > 0 && data->pos + size <= data->max){ - ret = data->base + data->pos; - data->pos += size; - } - return ret; -} - -inline void -partition_align(Partition *data, u32 boundary){ - --boundary; - data->pos = (data->pos + boundary) & (~boundary); -} - -inline void* -partition_current(Partition *data){ - return data->base + data->pos; -} - -inline i32 -partition_remaining(Partition *data){ - return data->max - data->pos; -} - -inline Partition -partition_sub_part(Partition *data, i32 size){ - Partition result = {}; - void *d = partition_allocate(data, size); - if (d) result = partition_open(d, size); - return result; -} - -#define push_struct(part, T) (T*)partition_allocate(part, sizeof(T)) -#define push_array(part, T, size) (T*)partition_allocate(part, sizeof(T)*(size)) -#define push_block(part, size) partition_allocate(part, size) - inline void insert_bubble(Bubble *prev, Bubble *bubble){ bubble->prev = prev; @@ -238,19 +181,6 @@ general_memory_reallocate_nocopy(General_Memory *general, void *old, i32 size, u return general_memory_reallocate(general, old, 0, size, type); } -internal Temp_Memory -begin_temp_memory(Partition *data){ - Temp_Memory result; - result.handle = data; - result.pos = data->pos; - return result; -} - -internal void -end_temp_memory(Temp_Memory temp){ - ((Partition*)temp.handle)->pos = temp.pos; -} - #define reset_temp_memory end_temp_memory #define gen_struct(g, T) (T*)general_memory_allocate(g, sizeof(T), 0) diff --git a/4ed_meta.h b/4ed_meta.h index 65ef8a63..77bfbdde 100644 --- a/4ed_meta.h +++ b/4ed_meta.h @@ -96,7 +96,9 @@ _OutDbgStr(u8*); #define FatalError(message) system_fatal_error((u8*)message) #define AllowLocal(name) (void)name +#ifndef ArrayCount #define ArrayCount(array) (sizeof(array)/sizeof(array[0])) +#endif #define OffsetOfStruct(S,c) ((i64)(& ((S*)0)->c )) #define OffsetOfPtr(s,c) ((i64)((char*)(&(s)->c) - (char*)(s))) diff --git a/4ed_rendering.cpp b/4ed_rendering.cpp index 20143f1a..52066b1d 100644 --- a/4ed_rendering.cpp +++ b/4ed_rendering.cpp @@ -385,6 +385,7 @@ draw_font_info_load(Partition *partition, return(result); } +// TODO(allen): Why the hell am I not just passing in a partition here? internal i32 draw_font_load(void *base_block, i32 size, Render_Font *font_out, @@ -402,7 +403,7 @@ draw_font_load(void *base_block, i32 size, File_Data file; file = system_load_file(filename.str); - Partition partition_ = partition_open(base_block, size); + Partition partition_ = make_part(base_block, size); Partition *partition = &partition_; stbtt_packedchar *chardata = font_out->chardata; diff --git a/buildsuper.bat b/buildsuper.bat index 105a74b3..83f0802e 100644 --- a/buildsuper.bat +++ b/buildsuper.bat @@ -11,7 +11,7 @@ SET OPTS=/W4 /wd4310 /wd4100 /wd4201 /wd4505 /wd4996 /wd4127 /wd4510 /wd4512 /wd SET OPTS=%OPTS% /GR- /nologo SET DEBUG=/Zi set BUILD_DLL=/LD /link /INCREMENTAL:NO /OPT:REF -SET EXPORTS=/EXPORT:get_bindings /EXPORT:get_alpha_4coder_version +SET EXPORTS=/EXPORT:view_routine /EXPORT:get_bindings /EXPORT:get_alpha_4coder_version REM SET LINKS=user32.lib gdi32.lib SET LINKS= diff --git a/power/4coder_experiments.cpp b/power/4coder_experiments.cpp index bfc959b8..81bb017e 100644 --- a/power/4coder_experiments.cpp +++ b/power/4coder_experiments.cpp @@ -347,7 +347,8 @@ HOOK_SIG(experimental_start_hook){ return(0); } -int get_bindings(void *data, int size){ +extern "C" int +get_bindings(void *data, int size){ Bind_Helper context_ = begin_bind_helper(data, size); Bind_Helper *context = &context_; @@ -376,5 +377,10 @@ int get_bindings(void *data, int size){ return(result); } +extern "C" void +view_routine(Application_Links *app, int view_id){ + app->get_user_input(app, 0, 0); +} + // BOTTOM diff --git a/win32_4ed.cpp b/win32_4ed.cpp index 87e3bfc3..a1380e92 100644 --- a/win32_4ed.cpp +++ b/win32_4ed.cpp @@ -9,6 +9,8 @@ // TOP +#include "4coder_default_bindings.cpp" + #include "4ed_config.h" #include "4ed_meta.h" @@ -26,8 +28,6 @@ #include -#include "4coder_default_bindings.cpp" - #undef exec_command #undef exec_command_keep_stack #undef clear_parameters @@ -241,7 +241,7 @@ Win32ScratchPartition(i32 size){ Partition part; void *data; data = Win32GetMemory(size); - part = partition_open(data, size); + part = make_part(data, size); return(part); } @@ -252,7 +252,7 @@ Win32ScratchPartitionGrow(Partition *part, i32 new_size){ data = Win32GetMemory(new_size); memcpy(data, part->base, part->pos); Win32FreeMemory(part->base); - part->base = (u8*)data; + part->base = (char*)data; } } @@ -1889,30 +1889,30 @@ int main(int argc, char **argv){ &files, &file_count, clparams); // - + if (output_size > 0){ DWORD written; WriteFile(original_out, memory_vars.target_memory, output_size, &written, 0); } if (output_size != 0) return 0; - + #ifdef FRED_SUPER char *custom_file_default = "4coder_custom.dll"; char *custom_file; if (win32vars.settings.custom_dll) custom_file = win32vars.settings.custom_dll; else custom_file = custom_file_default; - + win32vars.custom = LoadLibraryA(custom_file); if (!win32vars.custom && custom_file != custom_file_default){ if (!win32vars.settings.custom_dll_is_strict){ win32vars.custom = LoadLibraryA(custom_file_default); } } - + if (win32vars.custom){ win32vars.custom_api.get_alpha_4coder_version = (_Get_Version_Function*) GetProcAddress(win32vars.custom, "get_alpha_4coder_version"); - // + if (win32vars.custom_api.get_alpha_4coder_version == 0 || win32vars.custom_api.get_alpha_4coder_version(MAJOR, MINOR, PATCH) == 0){ printf("Error: application and custom version numbers don't match"); @@ -1920,6 +1920,8 @@ int main(int argc, char **argv){ } win32vars.custom_api.get_bindings = (Get_Binding_Data_Function*) GetProcAddress(win32vars.custom, "get_bindings"); + win32vars.custom_api.view_routine = (View_Routine_Function*) + GetProcAddress(win32vars.custom, "view_routine"); } #endif