added scroll list
This commit is contained in:
@@ -157,6 +157,7 @@ typedef struct TimEvent {
|
||||
char s[32]; // string representation of key, used by TimEvent_Key
|
||||
} TimEvent;
|
||||
|
||||
|
||||
typedef struct TimEditState {
|
||||
bool masked; // if true prints '*' instead of buffer content
|
||||
i32 cursor; // cursor position (utf8)
|
||||
@@ -165,6 +166,22 @@ typedef struct TimEditState {
|
||||
char* s; // zero terminated buffer
|
||||
} TimEditState;
|
||||
|
||||
|
||||
typedef struct TimScrollItem {
|
||||
void* data;
|
||||
void* focus_target; // is assigned to tim->focus
|
||||
i32 h; // height of the item to know where to draw next item
|
||||
void (*draw)(bool is_selected, TimRect place, void* data);
|
||||
} TimScrollItem;
|
||||
|
||||
typedef struct TimScrollState {
|
||||
TimScrollItem* items;
|
||||
u32 len;
|
||||
u32 cur;
|
||||
bool use_mouse_wheel;
|
||||
} TimScrollState;
|
||||
|
||||
|
||||
typedef struct TimState {
|
||||
i32 w; // screen width
|
||||
i32 h; // screen height
|
||||
@@ -247,8 +264,6 @@ i32 tim_exit_scope(void);
|
||||
|
||||
#pragma region widgets
|
||||
|
||||
// TODO: create enum TimColor and struct TimStyle
|
||||
|
||||
// frame
|
||||
// color: background, frame
|
||||
void tim_frame(i32 x, i32 y, i32 w, i32 h, TimStyle style);
|
||||
@@ -281,7 +296,7 @@ bool tim_radiobutton(cstr txt, i32* state, i32 v, i32 x, i32 y, i32 w, TimStyle
|
||||
|
||||
/// text edit - value in state
|
||||
/// @param e persistent edit state, use TimEditState_construct() to create new state
|
||||
/// @param color frame, background, text
|
||||
/// @param style frame, background, text
|
||||
/// @return key id or 0
|
||||
TimKey tim_edit(TimEditState* e, i32 x, i32 y, i32 w, TimStyle style);
|
||||
|
||||
@@ -291,15 +306,20 @@ TimKey tim_edit(TimEditState* e, i32 x, i32 y, i32 w, TimStyle style);
|
||||
void TimEditState_construct(TimEditState* e, i32 capacity, cstr initial_content);
|
||||
|
||||
static inline void TimEditState_destroy(TimEditState* e) {
|
||||
if(!e)
|
||||
return;
|
||||
if(!e) return;
|
||||
free(e->s);
|
||||
}
|
||||
|
||||
void TimEditState_insert(TimEditState* e, cstr s);
|
||||
|
||||
void TimEditState_delete(TimEditState* e);
|
||||
|
||||
/// @param l list of rows to display
|
||||
/// @param style frame, background, text
|
||||
TimScrollItem* tim_scroll(TimScrollState* l, i32 x, i32 y, i32 w, i32 h, TimStyle style);
|
||||
|
||||
void TimScrollState_selectNext(TimScrollState* l);
|
||||
void TimScrollState_selectPrev(TimScrollState* l);
|
||||
|
||||
#pragma endregion
|
||||
|
||||
|
||||
|
||||
45
src/scroll.c
Executable file
45
src/scroll.c
Executable file
@@ -0,0 +1,45 @@
|
||||
#include "tim.h"
|
||||
|
||||
void TimScrollState_selectNext(TimScrollState* l){
|
||||
l->cur = (l->cur + 1) % l->len;
|
||||
}
|
||||
|
||||
void TimScrollState_selectPrev(TimScrollState* l){
|
||||
l->cur = (l->len + l->cur - 1) % l->len;
|
||||
}
|
||||
|
||||
TimScrollItem* tim_scroll(TimScrollState* l, i32 x, i32 y, i32 w, i32 h, TimStyle style){
|
||||
// select with buttons and mouse wheel
|
||||
if(tim_is_key_press(TimKey_Down)
|
||||
|| (l->use_mouse_wheel && tim_is_mouse_scroll_down()))
|
||||
{
|
||||
TimScrollState_selectNext(l);
|
||||
}
|
||||
if(tim_is_key_press(TimKey_Up)
|
||||
|| (l->use_mouse_wheel && tim_is_mouse_scroll_up()))
|
||||
{
|
||||
TimScrollState_selectPrev(l);
|
||||
}
|
||||
|
||||
tim->focus = l->items[l->cur].focus_target;
|
||||
|
||||
tim_frame(x, y, w, h, style);
|
||||
|
||||
TimRect absolute = tim_scope_rect_to_absolute(x, y, w, h);
|
||||
TimRect place = { .x = x + 1, .y = y + 1, .w = absolute.w - 2, .h = absolute.h - 2 };
|
||||
for(u32 i = 0; i < l->len; i++){
|
||||
TimScrollItem* item = &l->items[i];
|
||||
place.h = item->h;
|
||||
|
||||
// select with mouse click
|
||||
if(tim_is_mouse_click_over(tim_scope_rect_to_absolute(place.x, place.y, place.w, place.h))){
|
||||
l->cur = i;
|
||||
tim->focus = item->focus_target;
|
||||
}
|
||||
|
||||
item->draw(i == l->cur, place, item->data);
|
||||
place.y += place.h;
|
||||
}
|
||||
|
||||
return &l->items[l->cur];
|
||||
}
|
||||
Reference in New Issue
Block a user