From 5ef7f91764ac1484530fd7fb00376cee5c3dbfa5 Mon Sep 17 00:00:00 2001 From: Timerix Date: Wed, 11 Feb 2026 15:16:52 +0500 Subject: [PATCH] partial fix of scroll_view --- include/tim.h | 3 +- src/panel.c | 105 ++++++++++++++++++++++++---------------------- src/scope.c | 15 +++---- src/scroll_view.c | 2 +- 4 files changed, 66 insertions(+), 59 deletions(-) diff --git a/include/tim.h b/include/tim.h index fb3bbb5..c5839ba 100644 --- a/include/tim.h +++ b/include/tim.h @@ -350,7 +350,8 @@ TimPanelItem* tim_panel(TimPanel* self, bool is_selected, i32 x, i32 y, i32 w, i void TimPanel_selectNext(TimPanel* self); void TimPanel_selectPrev(TimPanel* self); -/// +/// Scrollable scope. +/// WARNING: draw it before anything else or it will overlap void tim_scroll_view(TimScrollView* self, i32 x, i32 y, i32 w, i32 h, TimStyle style); #pragma endregion diff --git a/src/panel.c b/src/panel.c index fbb6661..e1790d0 100755 --- a/src/panel.c +++ b/src/panel.c @@ -10,7 +10,7 @@ void TimPanel_selectPrev(TimPanel* l){ l->cur--; } -TimPanelItem* tim_panel(TimPanel* self, bool is_panel_selected, i32 x, i32 y, i32 w, i32 h){ +TimPanelItem* tim_panel(TimPanel* self, bool is_panel_selected, i32 x1, i32 y1, i32 w1, i32 h1){ // select item with keyboard if(tim_is_key_press(self->is_horizontal ? TimKey_Left : TimKey_Up)) { @@ -25,61 +25,66 @@ TimPanelItem* tim_panel(TimPanel* self, bool is_panel_selected, i32 x, i32 y, i3 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]; - // 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]; + // TODO: draw current item and as much previous items as possible in scope + TimRect absolute = tim_scope_rect_to_absolute(x1, y1, w1, h1); + TimRect scope = tim->scopes[tim->scope]; + TimRect item_place = { + .x = absolute.x - scope.x, + .y = absolute.y - scope.y, + .w = 0, + .h = 0 + }; + for(i32 i = 0; i < self->len; i++){ + TimPanelItem* item = &self->items[i]; - item_place.w = item->w; - if(item_place.w == A){ - if(self->is_horizontal){ - 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; - } - } - - item_place.h = item->h; - if(item_place.h == A){ - if(self->is_horizontal){ - item_place.h = content_scope.h; - } - else { - 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; - } - } - - // select item with mouse click - if(tim_is_mouse_click_over(tim_scope_rect_to_absolute(item_place.x, item_place.y, item_place.w, item_place.h))){ - self->cur = i; - tim->focus = item->focus_target; - } - - bool is_item_selected = false; - if(is_panel_selected) - is_item_selected = i == self->cur; - item->draw(item->data, item_place, is_item_selected); - - // adjust place for next item + // calculate item width + item_place.w = item->w; + if(item_place.w == A){ if(self->is_horizontal){ - item_place.x += item_place.w + self->spacing; + item_place.w = absolute.w / self->len ; + // add remaining width to the last item + if(i == self->len - 1) + item_place.w += absolute.w % self->len; + else item_place.w -= self->spacing; } else { - item_place.y += item_place.h + self->spacing; + item_place.w = absolute.w; } } + + // calculate item height + item_place.h = item->h; + if(item_place.h == A){ + if(self->is_horizontal){ + item_place.h = absolute.h; + } + else { + item_place.h = absolute.h / self->len - self->spacing; + // add remaining height to the last item + if(i == self->len - 1) + item_place.h += absolute.h % self->len; + else item_place.h -= self->spacing; + } + } + + // select item with mouse click + if(tim_is_mouse_click_over(tim_scope_rect_to_absolute(item_place.x, item_place.y, item_place.w, item_place.h))){ + self->cur = i; + tim->focus = item->focus_target; + } + + bool is_item_selected = false; + if(is_panel_selected) + is_item_selected = i == self->cur; + item->draw(item->data, item_place, is_item_selected); + + // adjust place for next item + if(self->is_horizontal){ + item_place.x += item_place.w + self->spacing; + } + else { + item_place.y += item_place.h + self->spacing; + } } return &self->items[self->cur]; diff --git a/src/scope.c b/src/scope.c index 33552f9..60d12f5 100755 --- a/src/scope.c +++ b/src/scope.c @@ -17,17 +17,18 @@ TimRect tim_scope_rect_to_absolute(i32 x, i32 y, i32 w, i32 h) { if (x == A) { // x = p.x + (p.w - w) / 2; // center x on parent } else { // - if (x < 0) { // - x += p.w - w + 1; // anchor x to right - } // - x += p.x; // anchor x to left + // if (x < 0) { // + // x += p.w - w + 1; // anchor x to right + // } // + if(x >= 0) + x += p.x; // anchor x to left } // if (y == A) { // y = p.y + (p.h - h) / 2; // center y on parent } else { // - if (y < 0) { // - y += p.h - h + 1; // anchor y to bottom - } // + // if (y < 0) { // + // y += p.h - h + 1; // anchor y to bottom + // } // y += p.y; // anchor y to top } diff --git a/src/scroll_view.c b/src/scroll_view.c index bda4319..52da499 100644 --- a/src/scroll_view.c +++ b/src/scroll_view.c @@ -75,7 +75,7 @@ void tim_scroll_view(TimScrollView* self, i32 x, i32 y, i32 w, i32 h, TimStyle s tim->scopes[tim->scope] = content_scope; // draw content - TimRect content_place = { .x = 0, .y = -self->offset - 1, .w = content_scope.w, .h = content_scope.h }; + TimRect content_place = { .x = 0, .y = -self->offset, .w = content_scope.w, .h = content_scope.h }; self->draw(self->data, content_place); } } \ No newline at end of file