finished scroll_view

This commit is contained in:
2026-01-16 19:34:06 +05:00
parent cbd1cc0cf1
commit ee6375f553
5 changed files with 64 additions and 68 deletions

View File

@@ -185,7 +185,6 @@ typedef struct TimPanel {
i32 len; // number of items
i32 cur; // index of current item
i32 spacing; // distance between items
bool draw_border;
bool is_horizontal;
} TimPanel;
@@ -273,13 +272,22 @@ i32 tim_enter_scope(i32 x, i32 y, i32 w, i32 h);
// exit scope and pop stack
i32 tim_exit_scope(void);
static inline bool tim_rect_does_fit(TimRect r){
return (
r.x >= 0 && r.y >= 0 &&
r.w >= 0 && r.h >= 0 &&
r.x + r.w <= tim->w &&
r.y + r.h <= tim->h
);
static inline TimRect tim_rect_fit(TimRect r){
if(r.x < 0)
r.x = 0;
else if(r.x >= tim->w)
r.w = 0;
else if(r.x + r.w > tim->w)
r.w = tim->w - r.x;
if(r.y < 0)
r.y = 0;
else if(r.y >= tim->h)
r.y = tim->h - 1;
else if(r.y + r.h > tim->h)
r.h = tim->h - r.y;
return r;
}
#pragma endregion
@@ -336,9 +344,8 @@ void TimEditState_delete(TimEditState* e);
/// Panel with sequence of items. You can select an item by arrow keys or mouse click.
/// @param self persistent state
/// @param is_selected if panel is not selected, it calls items[:]->draw(is_selected=false)
/// @param style frame, background, text
/// @return current item
TimPanelItem* tim_panel(TimPanel* self, bool is_selected, i32 x, i32 y, i32 w, i32 h, TimStyle style);
TimPanelItem* tim_panel(TimPanel* self, bool is_selected, i32 x, i32 y, i32 w, i32 h);
void TimPanel_selectNext(TimPanel* self);
void TimPanel_selectPrev(TimPanel* self);

View File

@@ -15,9 +15,7 @@ void tim_clear_cells(void) {
}
void tim_draw_chr(TimCell cell, i32 x, i32 y) {
TimRect scope = tim->scopes[tim->scope];
if(!tim_rect_does_fit(scope))
return;
TimRect scope = tim_rect_fit(tim->scopes[tim->scope]);
if (x >= scope.x && x < scope.x + scope.w &&
y >= scope.y && y < scope.y + scope.h)
@@ -27,9 +25,7 @@ void tim_draw_chr(TimCell cell, i32 x, i32 y) {
}
void tim_draw_row(TimCell cell, i32 x, i32 y, i32 w) {
TimRect scope = tim->scopes[tim->scope];
if(!tim_rect_does_fit(scope))
return;
TimRect scope = tim_rect_fit(tim->scopes[tim->scope]);
if (y >= scope.y && y < scope.y + scope.h && w > 0) {
for (i32 i = MAX(x, scope.x); i < MIN(x + w, scope.x + scope.w); i++) {
@@ -39,9 +35,7 @@ void tim_draw_row(TimCell cell, i32 x, i32 y, i32 w) {
}
void tim_draw_col(TimCell cell, i32 x, i32 y, i32 h) {
TimRect scope = tim->scopes[tim->scope];
if(!tim_rect_does_fit(scope))
return;
TimRect scope = tim_rect_fit(tim->scopes[tim->scope]);
if (x >= scope.x && x < scope.x + scope.w && h > 0) {
for (i32 i = MAX(y, scope.y); i < MIN(y + h, scope.y + scope.h); i++) {
@@ -51,9 +45,7 @@ void tim_draw_col(TimCell cell, i32 x, i32 y, i32 h) {
}
void tim_fill(TimCell cell, i32 x, i32 y, i32 w, i32 h) {
TimRect scope = tim->scopes[tim->scope];
if(!tim_rect_does_fit(scope))
return;
TimRect scope = tim_rect_fit(tim->scopes[tim->scope]);
if (w > 0 && h > 0) {
for (i32 iy = MAX(y, scope.y); iy < MIN(y + h, scope.y + scope.h); iy++) {
@@ -65,9 +57,7 @@ void tim_fill(TimCell cell, i32 x, i32 y, i32 w, i32 h) {
}
void tim_draw_str(cstr s, i32 x, i32 y, i32 w, u8 fg, u8 bg) {
TimRect scope = tim->scopes[tim->scope];
if(!tim_rect_does_fit(scope))
return;
TimRect scope = tim_rect_fit(tim->scopes[tim->scope]);
if (s && y >= 0 && x < scope.x + scope.w && y < scope.y + scope.h ) {
i32 end = MIN(x + w, scope.x + scope.w);
@@ -99,9 +89,7 @@ void tim_draw_box(i32 x, i32 y, i32 w, i32 h, u8 fg, u8 bg) {
}
void tim_draw_invert(i32 x, i32 y, i32 w) {
TimRect scope = tim->scopes[tim->scope];
if(!tim_rect_does_fit(scope))
return;
TimRect scope = tim_rect_fit(tim->scopes[tim->scope]);
if (y >= 0 && y < scope.y + scope.h && w > 0) {
for (i32 i = MAX(x, scope.x); i < MIN(x + w, scope.x + scope.w); i++) {

View File

@@ -10,36 +10,25 @@ void TimPanel_selectPrev(TimPanel* l){
l->cur--;
}
TimPanelItem* tim_panel(TimPanel* self, bool is_panel_selected, i32 x, i32 y, i32 w, i32 h, TimStyle style){
TimPanelItem* tim_panel(TimPanel* self, bool is_panel_selected, i32 x, i32 y, i32 w, i32 h){
// select item with keyboard
if(tim_is_key_press(self->is_horizontal ? TimKey_Left : TimKey_Up))
{
TimPanel_selectPrev(self);
}
if(tim_is_key_press(self->is_horizontal ? TimKey_Right : TimKey_Down))
else if(tim_is_key_press(self->is_horizontal ? TimKey_Right : TimKey_Down))
{
TimPanel_selectNext(self);
}
// set focus on current item
if(self->cur < self->len)
tim->focus = self->items[self->cur].focus_target;
tim_scope(x, y, w, h)
{
TimRect content_scope = tim->scopes[tim->scope];
// draw border and shrink items_scope
if(self->draw_border){
tim_frame(0, 0, ~0, ~0, style);
// put current scope inside frame
content_scope.x += 1;
content_scope.y += 1;
content_scope.w -= 2;
content_scope.h -= 2;
tim->scopes[tim->scope] = content_scope;
}
// draw items
// TODO: draw current item and as much previous items as possible in scope
TimRect item_place = { 0 };
for(i32 i = 0; i < self->len; i++){
TimPanelItem* item = &self->items[i];
@@ -47,10 +36,11 @@ TimPanelItem* tim_panel(TimPanel* self, bool is_panel_selected, i32 x, i32 y, i3
item_place.w = item->w;
if(item_place.w == A){
if(self->is_horizontal){
item_place.w = content_scope.w / self->len;
item_place.w = content_scope.w / self->len ;
// add remaining width to the last item
if(i == self->len - 1)
item_place.w += content_scope.w % self->len;
else item_place.w -= self->spacing;
}
else {
item_place.w = content_scope.w;
@@ -63,10 +53,11 @@ TimPanelItem* tim_panel(TimPanel* self, bool is_panel_selected, i32 x, i32 y, i3
item_place.h = content_scope.h;
}
else {
item_place.h = content_scope.h / self->len;
item_place.h = content_scope.h / self->len - self->spacing;
// add remaining height to the last item
if(i == self->len - 1)
item_place.h += content_scope.h % self->len;
else item_place.h -= self->spacing;
}
}
@@ -83,7 +74,7 @@ TimPanelItem* tim_panel(TimPanel* self, bool is_panel_selected, i32 x, i32 y, i3
// adjust place for next item
if(self->is_horizontal){
item_place.x += item_place.w;
item_place.x += item_place.w + self->spacing;
}
else {
item_place.y += item_place.h + self->spacing;

View File

@@ -39,9 +39,6 @@ i32 tim_enter_scope(i32 x, i32 y, i32 w, i32 h) {
return 0;
TimRect r = tim_scope_rect_to_absolute(x, y, w, h);
if(!tim_rect_does_fit(r))
return 0;
tim->scope += 1;
tim->scopes[tim->scope] = r;
return 1;

View File

@@ -7,7 +7,7 @@ void tim_scroll_view(TimScrollView* self, i32 x, i32 y, i32 w, i32 h, TimStyle s
TimRect content_scope = tim->scopes[tim->scope];
// shrink content_scope to put scrollbar
content_scope.w -= 1;
tim->scopes[tim->scope] = content_scope;
i32 max_offset = MAX(self->content_h - content_scope.h, 0);
// draw scroll bar and
TimRect arrow_up_r = {
@@ -31,12 +31,12 @@ void tim_scroll_view(TimScrollView* self, i32 x, i32 y, i32 w, i32 h, TimStyle s
if (tim->event.type == TimEvent_Draw) {
f32 scroll_ratio = 0;
i32 slider_h = 0;
if(self->content_h != 0){
scroll_ratio = (f32)self->offset / self->content_h + 0.001f;
slider_h = ceilf((f32)scrollbar_r.h / self->content_h);
f32 slider_h = 0;
if(max_offset != 0){
slider_h = ceilf((f32)scrollbar_r.h / max_offset);
scroll_ratio = (f32)self->offset / max_offset + 0.001f;
}
i32 slider_y = scrollbar_r.y + scrollbar_r.h * scroll_ratio;
i32 slider_y = scrollbar_r.y + (scrollbar_r.h - slider_h) * scroll_ratio;
tim_draw_chr(tim_cell("", style.brd, style.bg), arrow_up_r.x, arrow_up_r.y);
tim_draw_col(tim_cell("", style.brd, style.bg), scrollbar_r.x, scrollbar_r.y, scrollbar_r.h);
@@ -44,25 +44,38 @@ void tim_scroll_view(TimScrollView* self, i32 x, i32 y, i32 w, i32 h, TimStyle s
tim_draw_chr(tim_cell("", style.brd, style.bg), arrow_down_r.x, arrow_down_r.y);
}
if(tim_is_mouse_click_over(arrow_up_r)){
if(self->offset - 1 >= 0)
if(tim_is_mouse_click_over(arrow_up_r) || tim_is_mouse_scroll_up()){
self->offset--;
}
if(tim_is_mouse_click_over(arrow_down_r)){
if(self->offset + 1 < self->content_h)
else if(tim_is_mouse_click_over(arrow_down_r) || tim_is_mouse_scroll_down()){
self->offset++;
}
else if(tim_is_key_press(TimKey_PageUp)){
self->offset -= content_scope.h;
}
else if(tim_is_key_press(TimKey_PageDown)){
self->offset += content_scope.h;
}
if(self->offset > max_offset)
self->offset = max_offset;
else if(self->offset < 0)
self->offset = 0;
if(tim_is_mouse_click_over(scrollbar_r)){
i32 click_y_rel = tim->event.y - scrollbar_r.y;
f32 scroll_ratio = 0;
if(scrollbar_r.h != 0){
scroll_ratio = (f32)click_y_rel / scrollbar_r.h + 0.001f;
self->offset = self->content_h * scroll_ratio;
f32 slider_h = ceilf((f32)scrollbar_r.h / max_offset);
f32 scroll_ratio = (f32)click_y_rel / (scrollbar_r.h - slider_h) + 0.001f;
self->offset = max_offset * scroll_ratio;
}
}
// update current scope
tim->scopes[tim->scope] = content_scope;
// draw content
TimRect content_place = { .x = 0, .y = 0, .w = content_scope.w, .h = content_scope.h };
TimRect content_place = { .x = 0, .y = -self->offset - 1, .w = content_scope.w, .h = content_scope.h };
self->draw(self->data, content_place);
}
}