Compare commits

...

1 Commits

Author SHA1 Message Date
5ef7f91764 partial fix of scroll_view 2026-02-11 15:16:52 +05:00
4 changed files with 66 additions and 59 deletions

View File

@@ -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

View File

@@ -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];

View File

@@ -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
}

View File

@@ -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);
}
}