Use textbox shortcuts from the host platform

This commit is contained in:
Ilia Demianenko 2023-08-22 01:08:23 -07:00 committed by MartinFouilleul
parent 7755827485
commit a6f81a1320
11 changed files with 228 additions and 32 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -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)
{

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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_

View File

@ -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

View File

@ -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)

View File

@ -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_

View File

@ -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": []
}
]