Use textbox shortcuts from the host platform #52

Merged
MartinFouilleul merged 3 commits from ilidemi/orca:platform-text-edit into main 2023-08-22 20:15:22 +00:00
8 changed files with 214 additions and 29 deletions

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,3 @@
#include "platform.h"
oc_host_platform ORCA_IMPORT(oc_get_host_platform)();

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",
"cname": "oc_get_host_platform",
"ret": {"name": "int", "tag": "i"},
"args": []
}
]