Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
ui/console: support TR1X text scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Sep 25, 2024
1 parent 07cd736 commit 4969b57
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions include/libtrx/game/ui/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ 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);
1 change: 1 addition & 0 deletions include/libtrx/game/ui/widgets/label.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern UI_WIDGET *UI_Label_Create(

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);
Expand Down
1 change: 1 addition & 0 deletions include/libtrx/game/ui/widgets/prompt.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#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);
Expand Down
2 changes: 1 addition & 1 deletion include/libtrx/game/ui/widgets/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ typedef enum {
UI_WIDGET *UI_Stack_Create(
UI_STACK_LAYOUT layout, int32_t width, int32_t height);
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);
void UI_Stack_SetInverse(UI_WIDGET *self, bool inverse);
21 changes: 19 additions & 2 deletions src/game/ui/widgets/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "game/clock.h"
#include "game/console/common.h"
#include "game/ui/common.h"
#include "game/ui/events.h"
#include "game/ui/widgets/label.h"
#include "game/ui/widgets/prompt.h"
Expand All @@ -27,6 +28,7 @@ typedef struct {

int32_t listener1;
int32_t listener2;
int32_t listener3;

struct {
double expire_at;
Expand All @@ -36,6 +38,7 @@ typedef struct {

static void M_HandlePromptCancel(const UI_EVENT *event, void *data);
static void M_HandlePromptConfirm(const UI_EVENT *event, void *data);
static void M_HandleConfigChange(const UI_EVENT *event, void *data);
static void M_UpdateLogCount(UI_CONSOLE *self);

static int32_t M_GetWidth(const UI_CONSOLE *self);
Expand All @@ -57,6 +60,17 @@ static void M_HandlePromptConfirm(const UI_EVENT *const event, void *const data)
Console_Close();
}

static void M_HandleConfigChange(const UI_EVENT *event, void *data)
{
UI_CONSOLE *const self = (UI_CONSOLE *)data;
UI_Prompt_SetSize(self->prompt, M_GetWidth(self), TEXT_HEIGHT + LOG_MARGIN);
for (int32_t i = 0; i < MAX_LOG_LINES; i++) {
UI_Label_SetSize(
self->logs[i].label, M_GetWidth(self), UI_LABEL_AUTO_SIZE);
}
UI_Stack_SetSize(self->container, M_GetWidth(self), M_GetHeight(self));
}

static void M_UpdateLogCount(UI_CONSOLE *const self)
{
self->logs_on_screen = 0;
Expand All @@ -70,12 +84,12 @@ static void M_UpdateLogCount(UI_CONSOLE *const self)

static int32_t M_GetWidth(const UI_CONSOLE *const self)
{
return 640 - 2 * WINDOW_MARGIN;
return UI_GetCanvasWidth() - 2 * WINDOW_MARGIN;
}

static int32_t M_GetHeight(const UI_CONSOLE *const self)
{
return 480 - 2 * WINDOW_MARGIN;
return UI_GetCanvasHeight() - 2 * WINDOW_MARGIN;
}

static void M_SetPosition(UI_CONSOLE *const self, int32_t x, int32_t y)
Expand Down Expand Up @@ -103,6 +117,7 @@ static void M_Free(UI_CONSOLE *const self)
self->container->free(self->container);
UI_Events_Unsubscribe(self->listener1);
UI_Events_Unsubscribe(self->listener2);
UI_Events_Unsubscribe(self->listener3);
Memory_Free(self);
}

Expand Down Expand Up @@ -137,6 +152,8 @@ UI_WIDGET *UI_Console_Create(void)
"confirm", self->prompt, M_HandlePromptConfirm, NULL);
self->listener2 =
UI_Events_Subscribe("cancel", self->prompt, M_HandlePromptCancel, NULL);
self->listener3 =
UI_Events_Subscribe("config_change", NULL, M_HandleConfigChange, self);

return (UI_WIDGET *)self;
}
Expand Down
6 changes: 6 additions & 0 deletions src/game/ui/widgets/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ UI_WIDGET *UI_Prompt_Create(const int32_t width, const int32_t height)
return (UI_WIDGET *)self;
}

void UI_Prompt_SetSize(UI_WIDGET *widget, int32_t width, int32_t height)
{
UI_PROMPT *const self = (UI_PROMPT *)widget;
UI_Label_SetSize(self->label, width, height);
}

void UI_Prompt_SetFocus(UI_WIDGET *const widget, const bool is_focused)
{
UI_PROMPT *const self = (UI_PROMPT *)widget;
Expand Down
9 changes: 9 additions & 0 deletions src/game/ui/widgets/stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ UI_WIDGET *UI_Stack_Create(
return (UI_WIDGET *)self;
}

void UI_Stack_SetSize(
UI_WIDGET *const widget, const int32_t width, const int32_t height)
{
UI_STACK *const self = (UI_STACK *)widget;
self->width = width;
self->height = height;
UI_Stack_DoLayout(widget);
}

void UI_Stack_DoLayout(UI_WIDGET *const widget)
{
UI_STACK *const self = (UI_STACK *)widget;
Expand Down

0 comments on commit 4969b57

Please sign in to comment.