diff --git a/include/tim.h b/include/tim.h index 2b71920..0170867 100644 --- a/include/tim.h +++ b/include/tim.h @@ -179,10 +179,10 @@ typedef struct TimScrollItem { typedef struct TimScrollState { TimScrollItem* items; - u32 len; - u32 cur; + i32 len; + i32 cur; bool draw_border; - bool show_scroll_bar_horizontal; + bool show_scroll_bar_vertical; bool use_mouse_wheel; } TimScrollState; @@ -264,6 +264,15 @@ 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 + ); +} + #pragma endregion diff --git a/src/drawing.c b/src/drawing.c index 4c7a723..aa1079a 100755 --- a/src/drawing.c +++ b/src/drawing.c @@ -15,31 +15,49 @@ void tim_clear_cells(void) { } void tim_draw_chr(TimCell cell, i32 x, i32 y) { - if (x >= 0 && x < tim->w && y >= 0 && y < tim->h) { + TimRect scope = tim->scopes[tim->scope]; + if(!tim_rect_does_fit(scope)) + return; + + if (x >= scope.x && x < scope.x + scope.w && + y >= scope.y && y < scope.y + scope.h) + { tim->cells[x + y * tim->w] = cell; } } void tim_draw_row(TimCell cell, i32 x, i32 y, i32 w) { - if (y >= 0 && y < tim->h && w > 0) { - for (i32 i = MAX(x, 0); i < MIN(x + w, tim->w); i++) { + TimRect scope = tim->scopes[tim->scope]; + if(!tim_rect_does_fit(scope)) + return; + + 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++) { tim->cells[i + y * tim->w] = cell; } } } void tim_draw_col(TimCell cell, i32 x, i32 y, i32 h) { - if (x >= 0 && x < tim->w && h > 0) { - for (i32 i = MAX(y, 0); i < MIN(y + h, tim->h); i++) { + TimRect scope = tim->scopes[tim->scope]; + if(!tim_rect_does_fit(scope)) + return; + + 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++) { tim->cells[x + i * tim->w] = cell; } } } 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; + if (w > 0 && h > 0) { - for (i32 iy = MAX(y, 0); iy < MIN(y + h, tim->h); iy++) { - for (i32 ix = MAX(x, 0); ix < MIN(x + w, tim->w); ix++) { + for (i32 iy = MAX(y, scope.y); iy < MIN(y + h, scope.y + scope.h); iy++) { + for (i32 ix = MAX(x, scope.x); ix < MIN(x + w, scope.x + scope.w); ix++) { tim->cells[ix + iy * tim->w] = cell; } } @@ -47,8 +65,12 @@ 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) { - if (s && y >= 0 && x < tim->w && y < tim->h ) { - i32 end = MIN(x + w, tim->w); + TimRect scope = 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 ) { + i32 end = MIN(x + w, scope.x + scope.w); bool wide = false; for (i32 i = 0; s[i] && x < end; x++) { TimCell c = tim_cell(&s[i], fg, bg); @@ -75,8 +97,12 @@ 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) { - if (y >= 0 && y < tim->h && w > 0) { - for (i32 i = MAX(x, 0); i < MIN(x + w, tim->w); i++) { + TimRect scope = tim->scopes[tim->scope]; + if(!tim_rect_does_fit(scope)) + return; + + 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++) { TimCell c = tim->cells[i + y * tim->w]; tim->cells[i + y * tim->w].fg = c.bg; tim->cells[i + y * tim->w].bg = c.fg; diff --git a/src/scope.c b/src/scope.c index 05da233..a3e7477 100755 --- a/src/scope.c +++ b/src/scope.c @@ -35,10 +35,13 @@ TimRect tim_scope_rect_to_absolute(i32 x, i32 y, i32 w, i32 h) { } i32 tim_enter_scope(i32 x, i32 y, i32 w, i32 h) { - if (tim->scope + 1 >= TIM_MAX_SCOPE) { - return 0; - } + if (tim->scope + 1 >= TIM_MAX_SCOPE) + 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; diff --git a/src/scroll.c b/src/scroll.c index 1445a1c..f0c2210 100755 --- a/src/scroll.c +++ b/src/scroll.c @@ -2,11 +2,13 @@ #include void TimScrollState_selectNext(TimScrollState* l){ - l->cur = (l->cur + 1) % l->len; + if(l->cur + 1 < l->len) + l->cur++; } void TimScrollState_selectPrev(TimScrollState* l){ - l->cur = (l->len + l->cur - 1) % l->len; + if(l->cur - 1 >= 0) + l->cur--; } TimScrollItem* tim_scroll(TimScrollState* l, i32 x, i32 y, i32 w, i32 h, TimStyle style){ @@ -38,22 +40,56 @@ TimScrollItem* tim_scroll(TimScrollState* l, i32 x, i32 y, i32 w, i32 h, TimStyl } // draw scroll bar and shrink items_scope - if(l->show_scroll_bar_horizontal){ + if(l->show_scroll_bar_vertical){ items_scope.w -= 1; - i32 scrollbar_x = items_scope.x + items_scope.w; - i32 scrollbar_y = items_scope.y; - i32 scrollbar_h = items_scope.h - 2; - f32 slider_y_normalized = 0; - i32 slider_h = 0; - if(l->len != 0){ - slider_y_normalized = (f32)l->cur / l->len + 0.001f; - slider_h = ceilf((f32)scrollbar_h / l->len); + TimRect arrow_up_r = { + .x = items_scope.x + items_scope.w, + .y = items_scope.y, + .w = 1, + .h = 1 + }; + TimRect scrollbar_r = { + .x = arrow_up_r.x, + .y = arrow_up_r.y + 1, + .w = 1, + .h = items_scope.h - 2 + }; + TimRect arrow_down_r = { + .x = scrollbar_r.x, + .y = scrollbar_r.y + scrollbar_r.h, + .w = 1, + .h = 1 + }; + + if (tim->event.type == TimEvent_Draw) { + f32 scroll_ratio = 0; + i32 slider_h = 0; + if(l->len != 0){ + scroll_ratio = (f32)l->cur / l->len + 0.001f; + slider_h = ceilf((f32)scrollbar_r.h / l->len); + } + i32 slider_y = scrollbar_r.y + scrollbar_r.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); + tim_draw_col(tim_cell("█", style.brd, style.bg), scrollbar_r.x, slider_y, slider_h); + 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)){ + TimScrollState_selectPrev(l); + } + if(tim_is_mouse_click_over(arrow_down_r)){ + TimScrollState_selectNext(l); + } + if(tim_is_mouse_click_over(scrollbar_r)){ + i32 click_y_rel = tim->event.y - scrollbar_r.y; + f32 scroll_ratio = 0; + if(l->len != 0){ + scroll_ratio = (f32)click_y_rel / scrollbar_r.h + 0.001f; + l->cur = l->len * scroll_ratio; + } } - i32 slider_y = scrollbar_y + 1 + scrollbar_h * slider_y_normalized; - tim_draw_chr(tim_cell("▲", style.brd, style.bg), scrollbar_x, scrollbar_y); - tim_draw_col(tim_cell("░", style.brd, style.bg), scrollbar_x, scrollbar_y + 1, scrollbar_h); - tim_draw_col(tim_cell("█", style.brd, style.bg), scrollbar_x, slider_y, slider_h); - tim_draw_chr(tim_cell("▼", style.brd, style.bg), scrollbar_x, scrollbar_y + scrollbar_h + 1); } tim->scopes[tim->scope] = items_scope; // fit current scope inside frame and scrollbar