Skip to content

Commit

Permalink
implement TEXT_BOX_IO_C_STRING_READER
Browse files Browse the repository at this point in the history
  • Loading branch information
GorgonMeducer committed Feb 18, 2025
1 parent f777076 commit 2e9a8ba
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 45 deletions.
129 changes: 100 additions & 29 deletions examples/common/controls/text_box.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,27 @@

/*============================ MACROFIED FUNCTIONS ===========================*/
/*============================ TYPES =========================================*/
/*============================ GLOBAL VARIABLES ==============================*/

/*============================ PROTOTYPES ====================================*/
/*============================ LOCAL VARIABLES ===============================*/
static
int32_t __c_string_io_read_utf8_char_handler_t(text_box_t *ptThis,
uintptr_t pObj,
uint8_t *pchBuffer,
uint_fast16_t hwSize);

static
int32_t __c_string_io_seek_handler_t( text_box_t *ptThis,
uintptr_t pObj,
int32_t nOffset,
text_box_seek_whence_t enWhence);

/*============================ GLOBAL VARIABLES ==============================*/
const text_box_io_handler_t TEXT_BOX_IO_C_STRING_READER = {
.fnGetChar = &__c_string_io_read_utf8_char_handler_t,
.fnSeek = &__c_string_io_seek_handler_t,
};

/*============================ IMPLEMENTATION ================================*/

ARM_NONNULL(1,2)
Expand Down Expand Up @@ -109,46 +127,99 @@ void text_box_on_frame_complete( text_box_t *ptThis)

ARM_NONNULL(1)
void text_box_show( text_box_t *ptThis,
const arm_2d_tile_t *ptTile,
const arm_2d_region_t *ptRegion,
bool bIsNewFrame)
const arm_2d_tile_t *ptTile,
const arm_2d_region_t *ptRegion,
__arm_2d_color_t tColour,
uint8_t chOpacity,
bool bIsNewFrame)
{
if (-1 == (intptr_t)ptTile) {
ptTile = arm_2d_get_default_frame_buffer();
}

assert(NULL!= ptThis);

if (bIsNewFrame) {
int32_t iResult;
arm_2d_container(ptTile, __text_box, ptRegion) {
arm_2d_draw_box(&__text_box, NULL, 1, GLCD_COLOR_BLUE, 128);
}

ARM_2D_OP_WAIT_ASYNC();
}

ARM_NONNULL(1,2)
text_box_c_str_reader_t *text_box_c_str_reader_init(
text_box_c_str_reader_t *ptThis,
const char *pchString,
size_t tMaxLen)
{
assert(NULL != ptThis);
assert(NULL != pchString);

memset(ptThis, 0, sizeof(text_box_c_str_reader_t));
this.pchString = pchString;
this.tSizeInByte = strnlen(pchString, tMaxLen);

/* generate a cosine wave for opacity */
arm_2d_helper_time_cos_slider(0, 255, 2000, 0, &iResult, &this.lTimestamp[0]);
this.chOpacity = (uint8_t)iResult;
return ptThis;
}

static
int32_t __c_string_io_read_utf8_char_handler_t( text_box_t *ptTextBox,
uintptr_t pObj,
uint8_t *pchBuffer,
uint_fast16_t hwSize)
{
text_box_c_str_reader_t *ptThis = (text_box_c_str_reader_t *)pObj;
assert(NULL != ptThis);

if (NULL == pchBuffer) {
return -1;
} else if (0 == hwSize) {
return 0;
} else if (this.tPosition >= this.tSizeInByte) {
return 0;
}

uintptr_t wReadPosition = this.tPosition + (uintptr_t)this.pchString;

size_t tLeftToRead = this.tSizeInByte - this.tPosition;
hwSize = MIN(tLeftToRead, hwSize);

memcpy(pchBuffer, (uint8_t *)wReadPosition, hwSize);
this.tPosition += hwSize;

return hwSize;
}
static
int32_t __c_string_io_seek_handler_t( text_box_t *ptTextBox,
uintptr_t pObj,
int32_t nOffset,
text_box_seek_whence_t enWhence)
{
text_box_c_str_reader_t *ptThis = (text_box_c_str_reader_t *)pObj;
assert(NULL != ptThis);
int64_t lPosition = this.tPosition;
switch (enWhence) {
case TEXT_BOX_SEEK_SET:
lPosition = nOffset;
break;
case TEXT_BOX_SEEK_CUR:
lPosition = this.tPosition + nOffset;
break;
case TEXT_BOX_SEEK_END:
lPosition = (this.tSizeInByte - 1) + nOffset;
break;
default:
break;
}

arm_2d_container(ptTile, __control, ptRegion) {
/* put your drawing code inside here
* - &__control is the target tile (please do not use ptTile anymore)
* - __control_canvas is the canvas
*/

/* example code: flash a 50x50 red box in the centre */
arm_2d_align_centre(__control_canvas, 50, 50) {

arm_2d_fill_colour_with_opacity(
&__control,
&__centre_region,
(__arm_2d_color_t) {GLCD_COLOR_RED},
this.chOpacity
);

/* make sure the operation is complete */
ARM_2D_OP_WAIT_ASYNC();
}
if (lPosition < 0) {
lPosition = 0;
} else if (lPosition >= this.tSizeInByte) {
lPosition = this.tSizeInByte;
}
this.tPosition = lPosition;

ARM_2D_OP_WAIT_ASYNC();
return this.tPosition;
}

#if defined(__clang__)
Expand Down
62 changes: 53 additions & 9 deletions examples/common/controls/text_box.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,68 @@ extern "C" {
/*============================ MACROS ========================================*/
/*============================ MACROFIED FUNCTIONS ===========================*/
/*============================ TYPES =========================================*/
typedef enum {
TEXT_BOX_SEEK_SET,
TEXT_BOX_SEEK_CUR,
TEXT_BOX_SEEK_END
} text_box_seek_whence_t;

typedef struct text_box_t text_box_t;

typedef int32_t text_box_io_read_handler_t(text_box_t *ptThis,
uintptr_t pObj,
uint8_t *pchBuffer,
uint_fast16_t hwSize);
typedef int32_t text_box_io_seek_handler_t( text_box_t *ptThis,
uintptr_t pObj,
int32_t nOffset,
text_box_seek_whence_t enWhence);

typedef struct text_box_io_handler_t {
text_box_io_read_handler_t *fnGetChar;
text_box_io_seek_handler_t *fnSeek;
} text_box_io_handler_t;

typedef struct text_box_io_text_stream_reader_t {
const text_box_io_handler_t *ptIO;
uintptr_t pTarget;
} text_box_io_text_stream_reader_t;

typedef struct text_box_cfg_t {
const arm_2d_font_t *ptFont;
float fScale;

text_box_io_text_stream_reader_t tStreamIO;

arm_2d_scene_t *ptScene;
} text_box_cfg_t;

/*!
* \brief a user class for user defined control
*/
typedef struct text_box_t text_box_t;


struct text_box_t {

ARM_PRIVATE(

text_box_cfg_t tCFG;

/* place your private member here, following two are examples */
int64_t lTimestamp[1];
uint8_t chOpacity;
)
/* place your public member here */

};


typedef struct text_box_c_str_reader_t {
ARM_PRIVATE(
const char *pchString;
size_t tSizeInByte;
size_t tPosition;
)
} text_box_c_str_reader_t;

/*============================ GLOBAL VARIABLES ==============================*/
extern
const text_box_io_handler_t TEXT_BOX_IO_C_STRING_READER;
/*============================ PROTOTYPES ====================================*/

extern
Expand All @@ -99,10 +135,18 @@ void text_box_on_frame_complete( text_box_t *ptThis);
extern
ARM_NONNULL(1)
void text_box_show( text_box_t *ptThis,
const arm_2d_tile_t *ptTile,
const arm_2d_region_t *ptRegion,
bool bIsNewFrame);
const arm_2d_tile_t *ptTile,
const arm_2d_region_t *ptRegion,
__arm_2d_color_t tColour,
uint8_t chOpacity,
bool bIsNewFrame);

extern
ARM_NONNULL(1,2)
text_box_c_str_reader_t *text_box_c_str_reader_init(
text_box_c_str_reader_t *ptThis,
const char *pchString,
size_t tMaxLen);

#if defined(__clang__)
# pragma clang diagnostic pop
Expand Down
36 changes: 29 additions & 7 deletions examples/demos/arm_2d_scene_text_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ struct {
/*============================ PROTOTYPES ====================================*/
/*============================ LOCAL VARIABLES ===============================*/



/*
* \brief This text sample is designed to test a GUI textbox's ability to handle
* complex content. It features exceptionally long and compound words
Expand Down Expand Up @@ -132,6 +134,7 @@ static void __on_scene_text_reader_load(arm_2d_scene_t *ptScene)
user_scene_text_reader_t *ptThis = (user_scene_text_reader_t *)ptScene;
ARM_2D_UNUSED(ptThis);

text_box_on_load(&this.tTextPanel);
}

static void __after_scene_text_reader_switching(arm_2d_scene_t *ptScene)
Expand All @@ -146,6 +149,8 @@ static void __on_scene_text_reader_depose(arm_2d_scene_t *ptScene)
user_scene_text_reader_t *ptThis = (user_scene_text_reader_t *)ptScene;
ARM_2D_UNUSED(ptThis);

text_box_depose(&this.tTextPanel);

ptScene->ptPlayer = NULL;

arm_foreach(int64_t,this.lTimestamp, ptItem) {
Expand Down Expand Up @@ -181,13 +186,15 @@ static void __on_scene_text_reader_frame_start(arm_2d_scene_t *ptScene)
user_scene_text_reader_t *ptThis = (user_scene_text_reader_t *)ptScene;
ARM_2D_UNUSED(ptThis);

text_box_on_frame_start(&this.tTextPanel);
}

static void __on_scene_text_reader_frame_complete(arm_2d_scene_t *ptScene)
{
user_scene_text_reader_t *ptThis = (user_scene_text_reader_t *)ptScene;
ARM_2D_UNUSED(ptThis);

text_box_on_frame_complete(&this.tTextPanel);
#if 0
/* switch to next scene after 3s */
if (arm_2d_helper_is_time_out(3000, &this.lTimestamp[0])) {
Expand Down Expand Up @@ -231,14 +238,13 @@ IMPL_PFB_ON_DRAW(__pfb_draw_scene_text_reader_handler)
#endif

arm_2d_dock_with_margin(__dock_region, 4) {
arm_2d_draw_box(ptTile, &__dock_region, 1, GLCD_COLOR_BLUE, 128);

/* draw text at the top-left corner */
arm_lcd_text_set_target_framebuffer((arm_2d_tile_t *)ptTile);
arm_lcd_text_set_draw_region(&__dock_region);
arm_lcd_text_set_font((const arm_2d_font_t *)&ARM_2D_FONT_Arial14_A4);
arm_lcd_text_set_colour(GLCD_COLOR_BLACK, GLCD_COLOR_WHITE);
arm_lcd_printf("%s", c_chStory);
text_box_show( &this.tTextPanel,
ptTile,
&__dock_region,
(__arm_2d_color_t) {GLCD_COLOR_BLACK},
255,
bIsNewFrame);

}

Expand Down Expand Up @@ -303,6 +309,22 @@ user_scene_text_reader_t *__arm_2d_scene_text_reader_init( arm_2d_scene_player

/* ------------ initialize members of user_scene_text_reader_t begin ---------------*/

/* initialize textbox */
do {
text_box_cfg_t tCFG = {
.ptFont = (arm_2d_font_t *)&ARM_2D_FONT_Arial14_A4,
.tStreamIO = {
.ptIO = &TEXT_BOX_IO_C_STRING_READER,
.pTarget = (uintptr_t)text_box_c_str_reader_init(&this.tStringReader,
c_chStory,
sizeof(c_chStory)),
},

.ptScene = (arm_2d_scene_t *)ptThis,
};

text_box_init(&this.tTextPanel, &tCFG);
} while(0);

/* ------------ initialize members of user_scene_text_reader_t end ---------------*/

Expand Down
3 changes: 3 additions & 0 deletions examples/demos/arm_2d_scene_text_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#if defined(RTE_Acceleration_Arm_2D_Helper_PFB)

#include "arm_2d_helper.h"
#include "arm_2d_example_controls.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -86,6 +87,8 @@ ARM_PRIVATE(
int64_t lTimestamp[1];
bool bUserAllocated;

text_box_c_str_reader_t tStringReader;
text_box_t tTextPanel;
)
/* place your public member here */

Expand Down

0 comments on commit 2e9a8ba

Please sign in to comment.