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 len; // number of items
i32 cur; // index of current item i32 cur; // index of current item
i32 spacing; // distance between items i32 spacing; // distance between items
bool draw_border;
bool is_horizontal; bool is_horizontal;
} TimPanel; } TimPanel;
@@ -273,13 +272,22 @@ i32 tim_enter_scope(i32 x, i32 y, i32 w, i32 h);
// exit scope and pop stack // exit scope and pop stack
i32 tim_exit_scope(void); i32 tim_exit_scope(void);
static inline bool tim_rect_does_fit(TimRect r){ static inline TimRect tim_rect_fit(TimRect r){
return ( if(r.x < 0)
r.x >= 0 && r.y >= 0 && r.x = 0;
r.w >= 0 && r.h >= 0 && else if(r.x >= tim->w)
r.x + r.w <= tim->w && r.w = 0;
r.y + r.h <= tim->h 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 #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. /// Panel with sequence of items. You can select an item by arrow keys or mouse click.
/// @param self persistent state /// @param self persistent state
/// @param is_selected if panel is not selected, it calls items[:]->draw(is_selected=false) /// @param is_selected if panel is not selected, it calls items[:]->draw(is_selected=false)
/// @param style frame, background, text
/// @return current item /// @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_selectNext(TimPanel* self);
void TimPanel_selectPrev(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) { void tim_draw_chr(TimCell cell, i32 x, i32 y) {
TimRect scope = tim->scopes[tim->scope]; TimRect scope = tim_rect_fit(tim->scopes[tim->scope]);
if(!tim_rect_does_fit(scope))
return;
if (x >= scope.x && x < scope.x + scope.w && if (x >= scope.x && x < scope.x + scope.w &&
y >= scope.y && y < scope.y + scope.h) 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) { void tim_draw_row(TimCell cell, i32 x, i32 y, i32 w) {
TimRect scope = tim->scopes[tim->scope]; TimRect scope = tim_rect_fit(tim->scopes[tim->scope]);
if(!tim_rect_does_fit(scope))
return;
if (y >= scope.y && y < scope.y + scope.h && w > 0) { 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++) { 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) { void tim_draw_col(TimCell cell, i32 x, i32 y, i32 h) {
TimRect scope = tim->scopes[tim->scope]; TimRect scope = tim_rect_fit(tim->scopes[tim->scope]);
if(!tim_rect_does_fit(scope))
return;
if (x >= scope.x && x < scope.x + scope.w && h > 0) { 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++) { 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) { void tim_fill(TimCell cell, i32 x, i32 y, i32 w, i32 h) {
TimRect scope = tim->scopes[tim->scope]; TimRect scope = tim_rect_fit(tim->scopes[tim->scope]);
if(!tim_rect_does_fit(scope))
return;
if (w > 0 && h > 0) { if (w > 0 && h > 0) {
for (i32 iy = MAX(y, scope.y); iy < MIN(y + h, scope.y + scope.h); iy++) { 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) { void tim_draw_str(cstr s, i32 x, i32 y, i32 w, u8 fg, u8 bg) {
TimRect scope = tim->scopes[tim->scope]; TimRect scope = tim_rect_fit(tim->scopes[tim->scope]);
if(!tim_rect_does_fit(scope))
return;
if (s && y >= 0 && x < scope.x + scope.w && y < scope.y + scope.h ) { if (s && y >= 0 && x < scope.x + scope.w && y < scope.y + scope.h ) {
i32 end = MIN(x + w, scope.x + scope.w); 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) { void tim_draw_invert(i32 x, i32 y, i32 w) {
TimRect scope = tim->scopes[tim->scope]; TimRect scope = tim_rect_fit(tim->scopes[tim->scope]);
if(!tim_rect_does_fit(scope))
return;
if (y >= 0 && y < scope.y + scope.h && w > 0) { 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++) { 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--; 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 // select item with keyboard
if(tim_is_key_press(self->is_horizontal ? TimKey_Left : TimKey_Up)) if(tim_is_key_press(self->is_horizontal ? TimKey_Left : TimKey_Up))
{ {
TimPanel_selectPrev(self); 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); TimPanel_selectNext(self);
} }
// set focus on current item // set focus on current item
tim->focus = self->items[self->cur].focus_target; if(self->cur < self->len)
tim->focus = self->items[self->cur].focus_target;
tim_scope(x, y, w, h) tim_scope(x, y, w, h)
{ {
TimRect content_scope = tim->scopes[tim->scope]; TimRect content_scope = tim->scopes[tim->scope];
// TODO: draw current item and as much previous items as possible in 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
TimRect item_place = { 0 }; TimRect item_place = { 0 };
for(i32 i = 0; i < self->len; i++){ for(i32 i = 0; i < self->len; i++){
TimPanelItem* item = &self->items[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; item_place.w = item->w;
if(item_place.w == A){ if(item_place.w == A){
if(self->is_horizontal){ 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 // add remaining width to the last item
if(i == self->len - 1) if(i == self->len - 1)
item_place.w += content_scope.w % self->len; item_place.w += content_scope.w % self->len;
else item_place.w -= self->spacing;
} }
else { else {
item_place.w = content_scope.w; 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; item_place.h = content_scope.h;
} }
else { 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 // add remaining height to the last item
if(i == self->len - 1) if(i == self->len - 1)
item_place.h += content_scope.h % self->len; 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 // adjust place for next item
if(self->is_horizontal){ if(self->is_horizontal){
item_place.x += item_place.w; item_place.x += item_place.w + self->spacing;
} }
else { else {
item_place.y += item_place.h + self->spacing; 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; return 0;
TimRect r = tim_scope_rect_to_absolute(x, y, w, h); TimRect r = tim_scope_rect_to_absolute(x, y, w, h);
if(!tim_rect_does_fit(r))
return 0;
tim->scope += 1; tim->scope += 1;
tim->scopes[tim->scope] = r; tim->scopes[tim->scope] = r;
return 1; 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]; TimRect content_scope = tim->scopes[tim->scope];
// shrink content_scope to put scrollbar // shrink content_scope to put scrollbar
content_scope.w -= 1; 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 // draw scroll bar and
TimRect arrow_up_r = { 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) { if (tim->event.type == TimEvent_Draw) {
f32 scroll_ratio = 0; f32 scroll_ratio = 0;
i32 slider_h = 0; f32 slider_h = 0;
if(self->content_h != 0){ if(max_offset != 0){
scroll_ratio = (f32)self->offset / self->content_h + 0.001f; slider_h = ceilf((f32)scrollbar_r.h / max_offset);
slider_h = ceilf((f32)scrollbar_r.h / self->content_h); 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_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); 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); 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(tim_is_mouse_click_over(arrow_up_r) || tim_is_mouse_scroll_up()){
if(self->offset - 1 >= 0) self->offset--;
self->offset--;
} }
if(tim_is_mouse_click_over(arrow_down_r)){ else if(tim_is_mouse_click_over(arrow_down_r) || tim_is_mouse_scroll_down()){
if(self->offset + 1 < self->content_h) self->offset++;
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)){ if(tim_is_mouse_click_over(scrollbar_r)){
i32 click_y_rel = tim->event.y - scrollbar_r.y; i32 click_y_rel = tim->event.y - scrollbar_r.y;
f32 scroll_ratio = 0;
if(scrollbar_r.h != 0){ if(scrollbar_r.h != 0){
scroll_ratio = (f32)click_y_rel / scrollbar_r.h + 0.001f; f32 slider_h = ceilf((f32)scrollbar_r.h / max_offset);
self->offset = self->content_h * scroll_ratio; 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 // 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); self->draw(self->data, content_place);
} }
} }