Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: import console ui #37

Merged
merged 7 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/libtrx/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include "config/common.h"
#include "config/file.h"
#include "config/map.h"
#include "config/option.h"
27 changes: 27 additions & 0 deletions include/libtrx/config/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "../event_manager.h"
#include "../json.h"
#include "./option.h"

#include <stdbool.h>
#include <stdint.h>

void Config_Init(void);
void Config_Shutdown(void);

bool Config_Read(void);
bool Config_Write(void);

int32_t Config_SubscribeChanges(EVENT_LISTENER listener, void *user_data);
void Config_UnsubscribeChanges(int32_t listener_id);

extern const char *Config_GetPath(void);

extern void Config_Sanitize(void);
extern void Config_ApplyChanges(void);

extern const CONFIG_OPTION *Config_GetOptionMap(void);

extern void Config_LoadFromJSON(JSON_OBJECT *root_obj);
extern void Config_DumpToJSON(JSON_OBJECT *root_obj);
9 changes: 0 additions & 9 deletions include/libtrx/config/config.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "../enum_str.h"
#include "../json.h"
#include "config_option.h"
#include "./option.h"

#include <stdbool.h>
#include <stdint.h>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../enum_str.h"
#include "../utils.h"
#include "config_option.h"
#include "./option.h"

#include <stdbool.h>
#include <stddef.h>
Expand Down
24 changes: 24 additions & 0 deletions include/libtrx/event_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <stdint.h>

typedef struct {
const char *name;
const void *sender;
void *data;
} EVENT;

typedef void (*EVENT_LISTENER)(const EVENT *, void *user_data);

typedef struct EVENT_MANAGER EVENT_MANAGER;

EVENT_MANAGER *EventManager_Create(void);
void EventManager_Free(EVENT_MANAGER *manager);

int32_t EventManager_Subscribe(
EVENT_MANAGER *manager, const char *event_name, const void *sender,
EVENT_LISTENER listener, void *user_data);

void EventManager_Unsubscribe(EVENT_MANAGER *manager, int32_t listener_id);

void EventManager_Fire(EVENT_MANAGER *manager, const EVENT *event);
2 changes: 2 additions & 0 deletions include/libtrx/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const char *File_GetGameDirectory(void);
// only be necessary when interacting with external libraries.
char *File_GetFullPath(const char *path);

char *File_GetParentDirectory(const char *path);

char *File_GuessExtension(const char *path, const char **extensions);

MYFILE *File_Open(const char *path, FILE_OPEN_MODE mode);
Expand Down
3 changes: 3 additions & 0 deletions include/libtrx/game/clock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

extern double Clock_GetHighPrecisionCounter(void);
2 changes: 1 addition & 1 deletion include/libtrx/game/console/cmd/config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "../../../config/config_option.h"
#include "../../../config/option.h"
#include "../common.h"

extern CONSOLE_COMMAND g_Console_Cmd_Config;
Expand Down
15 changes: 15 additions & 0 deletions include/libtrx/game/console/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../types.h"

#include <stdbool.h>
#include <stdint.h>

typedef enum {
CR_SUCCESS,
Expand All @@ -22,5 +23,19 @@ typedef struct __PACKING CONSOLE_COMMAND {
COMMAND_RESULT (*proc)(const COMMAND_CONTEXT *ctx);
} CONSOLE_COMMAND;

void Console_Init(void);
void Console_Shutdown(void);

void Console_Open(void);
void Console_Close(void);
bool Console_IsOpened(void);

void Console_ScrollLogs(void);
int32_t Console_GetVisibleLogCount(void);
int32_t Console_GetMaxLogCount(void);

void Console_Log(const char *fmt, ...);
COMMAND_RESULT Console_Eval(const char *cmdline);

void Console_Draw(void);
extern void Console_DrawBackdrop(void);
4 changes: 4 additions & 0 deletions include/libtrx/game/input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

extern void Input_EnterListenMode(void);
extern void Input_ExitListenMode(void);
24 changes: 24 additions & 0 deletions include/libtrx/game/ui/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "./events.h"

typedef enum {
UI_KEY_LEFT,
UI_KEY_RIGHT,
UI_KEY_HOME,
UI_KEY_END,
UI_KEY_BACK,
UI_KEY_RETURN,
UI_KEY_ESCAPE,
} UI_INPUT;

void UI_Init(void);
void UI_Shutdown(void);

void UI_HandleKeyDown(uint32_t key);
void UI_HandleKeyUp(uint32_t key);
void UI_HandleTextEdit(const char *text);

extern int32_t UI_GetCanvasWidth(void);
extern int32_t UI_GetCanvasHeight(void);
extern UI_INPUT UI_TranslateInput(uint32_t system_keycode);
17 changes: 17 additions & 0 deletions include/libtrx/game/ui/events.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "../../event_manager.h"
#include "./widgets/base.h"

typedef void (*EVENT_LISTENER)(const EVENT *, void *user_data);

void UI_Events_Init(void);
void UI_Events_Shutdown(void);

int32_t UI_Events_Subscribe(
const char *event_name, const UI_WIDGET *sender, EVENT_LISTENER listener,
void *user_data);

void UI_Events_Unsubscribe(int32_t listener_id);

void UI_Events_Fire(const EVENT *event);
25 changes: 25 additions & 0 deletions include/libtrx/game/ui/widgets/base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <stdbool.h>
#include <stdint.h>

struct UI_WIDGET;

typedef void (*UI_WIDGET_CONTROL)(struct UI_WIDGET *self);
typedef void (*UI_WIDGET_DRAW)(struct UI_WIDGET *self);
typedef int32_t (*UI_WIDGET_GET_WIDTH)(const struct UI_WIDGET *self);
typedef int32_t (*UI_WIDGET_GET_HEIGHT)(const struct UI_WIDGET *self);
typedef void (*UI_WIDGET_SET_POSITION)(
struct UI_WIDGET *self, int32_t x, int32_t y);
typedef void (*UI_WIDGET_FREE)(struct UI_WIDGET *self);

typedef struct UI_WIDGET {
UI_WIDGET_CONTROL control;
UI_WIDGET_DRAW draw;
UI_WIDGET_GET_WIDTH get_width;
UI_WIDGET_GET_HEIGHT get_height;
UI_WIDGET_SET_POSITION set_position;
UI_WIDGET_FREE free;
} UI_WIDGET;

typedef UI_WIDGET UI_WIDGET_VTABLE;
12 changes: 12 additions & 0 deletions include/libtrx/game/ui/widgets/console.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "./base.h"

UI_WIDGET *UI_Console_Create(void);

void UI_Console_HandleOpen(UI_WIDGET *widget);
void UI_Console_HandleClose(UI_WIDGET *widget);
void UI_Console_HandleLog(UI_WIDGET *widget, const char *text);
void UI_Console_ScrollLogs(UI_WIDGET *widget);
int32_t UI_Console_GetVisibleLogCount(UI_WIDGET *widget);
int32_t UI_Console_GetMaxLogCount(UI_WIDGET *widget);
19 changes: 19 additions & 0 deletions include/libtrx/game/ui/widgets/label.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "./base.h"

#define UI_LABEL_AUTO_SIZE (-1)

extern UI_WIDGET *UI_Label_Create(
const char *text, int32_t width, int32_t height);

extern void UI_Label_ChangeText(UI_WIDGET *widget, const char *text);
extern const char *UI_Label_GetText(UI_WIDGET *widget);
extern void UI_Label_SetSize(UI_WIDGET *widget, int32_t width, int32_t height);

extern void UI_Label_AddFrame(UI_WIDGET *widget);
extern void UI_Label_RemoveFrame(UI_WIDGET *widget);
extern void UI_Label_Flash(UI_WIDGET *widget, bool enable, int32_t rate);
extern void UI_Label_SetScale(UI_WIDGET *widget, float scale);
extern void UI_Label_SetZIndex(UI_WIDGET *widget, int32_t z_index);
extern int32_t UI_Label_MeasureTextWidth(UI_WIDGET *widget);
12 changes: 12 additions & 0 deletions include/libtrx/game/ui/widgets/prompt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "./base.h"

UI_WIDGET *UI_Prompt_Create(int32_t width, int32_t height);
void UI_Prompt_SetSize(UI_WIDGET *widget, int32_t width, int32_t height);

void UI_Prompt_SetFocus(UI_WIDGET *widget, bool is_focused);
void UI_Prompt_Clear(UI_WIDGET *widget);

extern const char *UI_Prompt_GetPromptChar(void);
extern int32_t UI_Prompt_GetCaretFlashRate(void);
6 changes: 6 additions & 0 deletions include/libtrx/game/ui/widgets/spacer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include "./base.h"

UI_WIDGET *UI_Spacer_Create(int32_t width, int32_t height);
void UI_Spacer_SetSize(UI_WIDGET *widget, int32_t width, int32_t height);
30 changes: 30 additions & 0 deletions include/libtrx/game/ui/widgets/stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "./base.h"

#define UI_STACK_AUTO_SIZE (-1)

typedef enum {
UI_STACK_LAYOUT_HORIZONTAL,
UI_STACK_LAYOUT_VERTICAL,
} UI_STACK_LAYOUT;

typedef enum {
UI_STACK_H_ALIGN_LEFT,
UI_STACK_H_ALIGN_CENTER,
UI_STACK_H_ALIGN_RIGHT,
} UI_STACK_H_ALIGN;

typedef enum {
UI_STACK_V_ALIGN_TOP,
UI_STACK_V_ALIGN_CENTER,
UI_STACK_V_ALIGN_BOTTOM,
} UI_STACK_V_ALIGN;

UI_WIDGET *UI_Stack_Create(
UI_STACK_LAYOUT layout, int32_t width, int32_t height);
void UI_Stack_SetHAlign(UI_WIDGET *self, UI_STACK_H_ALIGN align);
void UI_Stack_SetVAlign(UI_WIDGET *self, UI_STACK_V_ALIGN align);
void UI_Stack_AddChild(UI_WIDGET *self, UI_WIDGET *child);
void UI_Stack_SetSize(UI_WIDGET *widget, int32_t width, int32_t height);
void UI_Stack_DoLayout(UI_WIDGET *self);
7 changes: 7 additions & 0 deletions include/libtrx/game/ui/widgets/window.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include "./base.h"

extern UI_WIDGET *UI_Window_Create(
UI_WIDGET *root, int32_t border_top, int32_t border_right,
int32_t border_bottom, int32_t border_left);
12 changes: 10 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ endif

sources = [
'src/benchmark.c',
'src/config/config_file.c',
'src/config/common.c',
'src/config/file.c',
'src/engine/audio.c',
'src/engine/audio_sample.c',
'src/engine/audio_stream.c',
'src/engine/image.c',
'src/enum_str.c',
'src/event_manager.c',
'src/filesystem.c',
'src/game/backpack.c',
'src/game/console/cmd/config.c',
Expand All @@ -81,9 +83,15 @@ sources = [
'src/game/console/cmd/set_health.c',
'src/game/console/cmd/sfx.c',
'src/game/console/cmd/teleport.c',
'src/game/game_string.c',
'src/game/console/common.c',
'src/game/game_string.c',
'src/game/items.c',
'src/game/ui/common.c',
'src/game/ui/events.c',
'src/game/ui/widgets/console.c',
'src/game/ui/widgets/prompt.c',
'src/game/ui/widgets/spacer.c',
'src/game/ui/widgets/stack.c',
'src/gfx/2d/2d_renderer.c',
'src/gfx/2d/2d_surface.c',
'src/gfx/3d/3d_renderer.c',
Expand Down
60 changes: 60 additions & 0 deletions src/config/common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "config/common.h"

#include "config/file.h"

#include <assert.h>

EVENT_MANAGER *m_EventManager = NULL;

void Config_Init(void)
{
m_EventManager = EventManager_Create();
}

void Config_Shutdown(void)
{
EventManager_Free(m_EventManager);
m_EventManager = NULL;
}

bool Config_Read(void)
{
const bool result = ConfigFile_Read(Config_GetPath(), &Config_LoadFromJSON);
if (result) {
Config_Sanitize();
Config_ApplyChanges();
}
return result;
}

bool Config_Write(void)
{
Config_Sanitize();
const bool updated = ConfigFile_Write(Config_GetPath(), &Config_DumpToJSON);
if (updated) {
Config_ApplyChanges();
if (m_EventManager != NULL) {
const EVENT event = {
.name = "write",
.sender = NULL,
.data = NULL,
};
EventManager_Fire(m_EventManager, &event);
}
}
return updated;
}

int32_t Config_SubscribeChanges(
const EVENT_LISTENER listener, void *const user_data)
{
assert(m_EventManager != NULL);
return EventManager_Subscribe(
m_EventManager, "write", NULL, listener, user_data);
}

void Config_UnsubscribeChanges(const int32_t listener_id)
{
assert(m_EventManager != NULL);
return EventManager_Unsubscribe(m_EventManager, listener_id);
}
Loading