diff --git a/src/app/app.h b/src/app/app.h index 297165a..f339d4b 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -212,7 +212,6 @@ extern "C" OC_KEYMOD_SHIFT = 0x02, OC_KEYMOD_CTRL = 0x04, OC_KEYMOD_CMD = 0x08, - OC_KEYMOD_MAIN_MODIFIER = 0x16 /* CMD on Mac, CTRL on Win32 */ } oc_keymod_flags; typedef enum diff --git a/src/app/osx_app.m b/src/app/osx_app.m index 068d4ba..0136a4a 100644 --- a/src/app/osx_app.m +++ b/src/app/osx_app.m @@ -215,7 +215,6 @@ static oc_keymod_flags oc_convert_osx_mods(NSUInteger nsFlags) if(nsFlags & NSEventModifierFlagCommand) { mods |= OC_KEYMOD_CMD; - mods |= OC_KEYMOD_MAIN_MODIFIER; } return (mods); } diff --git a/src/app/win32_app.c b/src/app/win32_app.c index 9492784..18d5db7 100644 --- a/src/app/win32_app.c +++ b/src/app/win32_app.c @@ -196,7 +196,6 @@ static oc_keymod_flags oc_get_mod_keys() if(GetKeyState(VK_CONTROL) & 0x8000) { mods |= OC_KEYMOD_CTRL; - mods |= OC_KEYMOD_MAIN_MODIFIER; } if(GetKeyState(VK_MENU) & 0x8000) { diff --git a/src/orca.c b/src/orca.c index 5771dbf..a74ebe0 100644 --- a/src/orca.c +++ b/src/orca.c @@ -20,6 +20,7 @@ #include "platform/win32_path.c" #include "platform/win32_io.c" #include "platform/win32_thread.c" + #include "platform/win32_platform.c" //TODO #elif OC_PLATFORM_MACOS #include "platform/native_debug.c" @@ -27,6 +28,7 @@ #include "platform/osx_clock.c" #include "platform/posix_io.c" #include "platform/posix_thread.c" + #include "platform/osx_platform.c" /* #include"platform/unix_rng.c" @@ -50,6 +52,7 @@ #include "platform/orca_malloc.c" #include "platform/platform_io_common.c" #include "platform/orca_io_stubs.c" + #include "platform/orca_platform.c" #else #error "Unsupported platform" #endif diff --git a/src/platform/orca_platform.c b/src/platform/orca_platform.c new file mode 100644 index 0000000..3e46971 --- /dev/null +++ b/src/platform/orca_platform.c @@ -0,0 +1,17 @@ +#include "platform.h" + +oc_host_platform ORCA_IMPORT(oc_get_host_platform_impl)(); + +#ifdef __cplusplus +extern "C" +{ +#endif + + oc_host_platform oc_get_host_platform() + { + return oc_get_host_platform_impl(); + } + +#ifdef __cplusplus +} // extern "C" +#endif \ No newline at end of file diff --git a/src/platform/osx_platform.c b/src/platform/osx_platform.c new file mode 100644 index 0000000..6901721 --- /dev/null +++ b/src/platform/osx_platform.c @@ -0,0 +1,15 @@ +#include "platform.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + oc_host_platform oc_get_host_platform() + { + return OC_HOST_PLATFORM_MACOS; + } + +#ifdef __cplusplus +} // extern "C" +#endif \ No newline at end of file diff --git a/src/platform/platform.h b/src/platform/platform.h index ecbb347..febc474 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -108,4 +108,21 @@ #endif #endif +#ifdef __cplusplus +extern "C" +{ +#endif + + typedef enum + { + OC_HOST_PLATFORM_MACOS, + OC_HOST_PLATFORM_WINDOWS, + } oc_host_platform; + + ORCA_API oc_host_platform oc_get_host_platform(); + +#ifdef __cplusplus +} // extern "C" +#endif + #endif // __PLATFORM_H_ diff --git a/src/platform/win32_platform.c b/src/platform/win32_platform.c new file mode 100644 index 0000000..e9a847a --- /dev/null +++ b/src/platform/win32_platform.c @@ -0,0 +1,15 @@ +#include "platform.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + oc_host_platform oc_get_host_platform() + { + return OC_HOST_PLATFORM_WINDOWS; + } + +#ifdef __cplusplus +} // extern "C" +#endif \ No newline at end of file diff --git a/src/ui/ui.c b/src/ui/ui.c index f47872d..71d433f 100644 --- a/src/ui/ui.c +++ b/src/ui/ui.c @@ -1,10 +1,10 @@ -/************************************************************/ /** -* -* @file: ui.c -* @author: Martin Fouilleul -* @date: 08/08/2022 -* @revision: -* +/************************************************************/ /** +* +* @file: ui.c +* @author: Martin Fouilleul +* @date: 08/08/2022 +* @revision: +* *****************************************************************/ #include "ui.h" #include "platform/platform.h" @@ -597,12 +597,12 @@ void oc_ui_animate_f32(oc_ui_context* ui, f32* value, f32 target, f32 animationT else { - /*NOTE: - we use the euler approximation for df/dt = alpha(target - f) - the implicit form is f(t) = target*(1-e^(-alpha*t)) for the rising front, - and f(t) = e^(-alpha*t) for the falling front (e.g. classic RC circuit charge/discharge) - - Here we bake alpha = 1/tau = -ln(0.05)/tr, with tr the rise time to 95% of target + /*NOTE: + we use the euler approximation for df/dt = alpha(target - f) + the implicit form is f(t) = target*(1-e^(-alpha*t)) for the rising front, + and f(t) = e^(-alpha*t) for the falling front (e.g. classic RC circuit charge/discharge) + + Here we bake alpha = 1/tau = -ln(0.05)/tr, with tr the rise time to 95% of target */ f32 alpha = 3 / animationTime; f32 dt = ui->lastFrameDuration; @@ -2300,7 +2300,7 @@ typedef struct oc_ui_edit_command } oc_ui_edit_command; -const oc_ui_edit_command OC_UI_EDIT_COMMANDS[] = { +const oc_ui_edit_command OC_UI_EDIT_COMMANDS_MACOS[] = { //NOTE(martin): move one left { .key = OC_KEY_LEFT, @@ -2376,7 +2376,7 @@ const oc_ui_edit_command OC_UI_EDIT_COMMANDS[] = { //NOTE(martin): select all { .key = OC_KEY_Q, - .mods = OC_KEYMOD_MAIN_MODIFIER, + .mods = OC_KEYMOD_CMD, .operation = OC_UI_EDIT_SELECT_ALL, .move = OC_UI_EDIT_MOVE_NONE }, //NOTE(martin): delete @@ -2394,24 +2394,134 @@ const oc_ui_edit_command OC_UI_EDIT_COMMANDS[] = { //NOTE(martin): cut { .key = OC_KEY_X, - .mods = OC_KEYMOD_MAIN_MODIFIER, + .mods = OC_KEYMOD_CMD, .operation = OC_UI_EDIT_CUT, .move = OC_UI_EDIT_MOVE_NONE }, //NOTE(martin): copy { .key = OC_KEY_C, - .mods = OC_KEYMOD_MAIN_MODIFIER, + .mods = OC_KEYMOD_CMD, .operation = OC_UI_EDIT_COPY, .move = OC_UI_EDIT_MOVE_NONE }, //NOTE(martin): paste { .key = OC_KEY_V, - .mods = OC_KEYMOD_MAIN_MODIFIER, + .mods = OC_KEYMOD_CMD, .operation = OC_UI_EDIT_PASTE, .move = OC_UI_EDIT_MOVE_NONE } }; -const u32 OC_UI_EDIT_COMMAND_COUNT = sizeof(OC_UI_EDIT_COMMANDS) / sizeof(oc_ui_edit_command); +const oc_ui_edit_command OC_UI_EDIT_COMMANDS_WINDOWS[] = { + //NOTE(martin): move one left + { + .key = OC_KEY_LEFT, + .operation = OC_UI_EDIT_MOVE, + .move = OC_UI_EDIT_MOVE_ONE, + .direction = -1 }, + //NOTE(martin): move one right + { + .key = OC_KEY_RIGHT, + .operation = OC_UI_EDIT_MOVE, + .move = OC_UI_EDIT_MOVE_ONE, + .direction = 1 }, + //NOTE(martin): move start + { + .key = OC_KEY_HOME, + .operation = OC_UI_EDIT_MOVE, + .move = OC_UI_EDIT_MOVE_LINE, + .direction = -1 }, + { .key = OC_KEY_UP, + .operation = OC_UI_EDIT_MOVE, + .move = OC_UI_EDIT_MOVE_LINE, + .direction = -1 }, + //NOTE(martin): move end + { + .key = OC_KEY_END, + .operation = OC_UI_EDIT_MOVE, + .move = OC_UI_EDIT_MOVE_LINE, + .direction = 1 }, + { .key = OC_KEY_DOWN, + .operation = OC_UI_EDIT_MOVE, + .move = OC_UI_EDIT_MOVE_LINE, + .direction = 1 }, + //NOTE(martin): select one left + { + .key = OC_KEY_LEFT, + .mods = OC_KEYMOD_SHIFT, + .operation = OC_UI_EDIT_SELECT, + .move = OC_UI_EDIT_MOVE_ONE, + .direction = -1 }, + //NOTE(martin): select one right + { + .key = OC_KEY_RIGHT, + .mods = OC_KEYMOD_SHIFT, + .operation = OC_UI_EDIT_SELECT, + .move = OC_UI_EDIT_MOVE_ONE, + .direction = 1 }, + //NOTE(martin): extend select to start + { + .key = OC_KEY_HOME, + .mods = OC_KEYMOD_SHIFT, + .operation = OC_UI_EDIT_SELECT_EXTEND, + .move = OC_UI_EDIT_MOVE_LINE, + .direction = -1 }, + { .key = OC_KEY_UP, + .mods = OC_KEYMOD_SHIFT, + .operation = OC_UI_EDIT_SELECT_EXTEND, + .move = OC_UI_EDIT_MOVE_LINE, + .direction = -1 }, + //NOTE(martin): extend select to end + { + .key = OC_KEY_END, + .mods = OC_KEYMOD_SHIFT, + .operation = OC_UI_EDIT_SELECT_EXTEND, + .move = OC_UI_EDIT_MOVE_LINE, + .direction = 1 }, + { .key = OC_KEY_DOWN, + .mods = OC_KEYMOD_SHIFT, + .operation = OC_UI_EDIT_SELECT_EXTEND, + .move = OC_UI_EDIT_MOVE_LINE, + .direction = 1 }, + //NOTE(martin): select all + { + .key = OC_KEY_A, + .mods = OC_KEYMOD_CTRL, + .operation = OC_UI_EDIT_SELECT_ALL, + .move = OC_UI_EDIT_MOVE_NONE }, + //NOTE(martin): delete + { + .key = OC_KEY_DELETE, + .operation = OC_UI_EDIT_DELETE, + .move = OC_UI_EDIT_MOVE_ONE, + .direction = 1 }, + //NOTE(martin): backspace + { + .key = OC_KEY_BACKSPACE, + .operation = OC_UI_EDIT_DELETE, + .move = OC_UI_EDIT_MOVE_ONE, + .direction = -1 }, + //NOTE(martin): cut + { + .key = OC_KEY_X, + .mods = OC_KEYMOD_CTRL, + .operation = OC_UI_EDIT_CUT, + .move = OC_UI_EDIT_MOVE_NONE }, + //NOTE(martin): copy + { + .key = OC_KEY_C, + .mods = OC_KEYMOD_CTRL, + .operation = OC_UI_EDIT_COPY, + .move = OC_UI_EDIT_MOVE_NONE }, + //NOTE(martin): paste + { + .key = OC_KEY_V, + .mods = OC_KEYMOD_CTRL, + .operation = OC_UI_EDIT_PASTE, + .move = OC_UI_EDIT_MOVE_NONE } +}; + +const u32 OC_UI_EDIT_COMMAND_MACOS_COUNT = sizeof(OC_UI_EDIT_COMMANDS_MACOS) / sizeof(oc_ui_edit_command); +const u32 OC_UI_EDIT_COMMAND_WINDOWS_COUNT = sizeof(OC_UI_EDIT_COMMANDS_WINDOWS) / sizeof(oc_ui_edit_command); void oc_ui_edit_perform_move(oc_ui_context* ui, oc_ui_edit_move move, int direction, u32 textLen) { @@ -2733,10 +2843,26 @@ oc_ui_text_box_result oc_ui_text_box(const char* name, oc_arena* arena, oc_str8 //NOTE handle shortcuts oc_keymod_flags mods = oc_key_mods(&ui->input); - - for(int i = 0; i < OC_UI_EDIT_COMMAND_COUNT; i++) + const oc_ui_edit_command* editCommands; + u32 editCommandCount; + oc_host_platform hostPlatform = oc_get_host_platform(); + switch(hostPlatform) { - const oc_ui_edit_command* command = &(OC_UI_EDIT_COMMANDS[i]); + case OC_HOST_PLATFORM_MACOS: + editCommands = OC_UI_EDIT_COMMANDS_MACOS; + editCommandCount = OC_UI_EDIT_COMMAND_MACOS_COUNT; + break; + case OC_HOST_PLATFORM_WINDOWS: + editCommands = OC_UI_EDIT_COMMANDS_WINDOWS; + editCommandCount = OC_UI_EDIT_COMMAND_WINDOWS_COUNT; + break; + default: + OC_ASSERT(0, "unknown host platform: %i", hostPlatform); + } + + for(int i = 0; i < editCommandCount; i++) + { + const oc_ui_edit_command* command = &(editCommands[i]); if((oc_key_pressed(&ui->input, command->key) || oc_key_repeated(&ui->input, command->key)) && mods == command->mods) diff --git a/src/ui/ui.h b/src/ui/ui.h index 6fe35af..9e9b7a7 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -1,10 +1,10 @@ -/************************************************************/ /** -* -* @file: ui.h -* @author: Martin Fouilleul -* @date: 08/08/2022 -* @revision: -* +/************************************************************/ /** +* +* @file: ui.h +* @author: Martin Fouilleul +* @date: 08/08/2022 +* @revision: +* *****************************************************************/ #ifndef __UI_H_ #define __UI_H_ diff --git a/src/wasmbind/core_api.json b/src/wasmbind/core_api.json index eb715fa..be7a6e2 100644 --- a/src/wasmbind/core_api.json +++ b/src/wasmbind/core_api.json @@ -57,5 +57,11 @@ {"name": "note", "type": {"name": "const char*", "tag": "p"}} ] +}, +{ + "name": "oc_get_host_platform_impl", + "cname": "oc_get_host_platform", + "ret": {"name": "int", "tag": "i"}, + "args": [] } ]