draw only inside current scope
This commit is contained in:
@@ -179,10 +179,10 @@ typedef struct TimScrollItem {
|
|||||||
|
|
||||||
typedef struct TimScrollState {
|
typedef struct TimScrollState {
|
||||||
TimScrollItem* items;
|
TimScrollItem* items;
|
||||||
u32 len;
|
i32 len;
|
||||||
u32 cur;
|
i32 cur;
|
||||||
bool draw_border;
|
bool draw_border;
|
||||||
bool show_scroll_bar_horizontal;
|
bool show_scroll_bar_vertical;
|
||||||
bool use_mouse_wheel;
|
bool use_mouse_wheel;
|
||||||
} TimScrollState;
|
} TimScrollState;
|
||||||
|
|
||||||
@@ -264,6 +264,15 @@ 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){
|
||||||
|
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
|
#pragma endregion
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -15,31 +15,49 @@ 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) {
|
||||||
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;
|
tim->cells[x + y * tim->w] = cell;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void tim_draw_row(TimCell cell, i32 x, i32 y, i32 w) {
|
void tim_draw_row(TimCell cell, i32 x, i32 y, i32 w) {
|
||||||
if (y >= 0 && y < tim->h && w > 0) {
|
TimRect scope = tim->scopes[tim->scope];
|
||||||
for (i32 i = MAX(x, 0); i < MIN(x + w, tim->w); i++) {
|
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;
|
tim->cells[i + y * tim->w] = cell;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void tim_draw_col(TimCell cell, i32 x, i32 y, i32 h) {
|
void tim_draw_col(TimCell cell, i32 x, i32 y, i32 h) {
|
||||||
if (x >= 0 && x < tim->w && h > 0) {
|
TimRect scope = tim->scopes[tim->scope];
|
||||||
for (i32 i = MAX(y, 0); i < MIN(y + h, tim->h); i++) {
|
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;
|
tim->cells[x + i * tim->w] = cell;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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];
|
||||||
|
if(!tim_rect_does_fit(scope))
|
||||||
|
return;
|
||||||
|
|
||||||
if (w > 0 && h > 0) {
|
if (w > 0 && h > 0) {
|
||||||
for (i32 iy = MAX(y, 0); iy < MIN(y + h, tim->h); iy++) {
|
for (i32 iy = MAX(y, scope.y); iy < MIN(y + h, scope.y + scope.h); iy++) {
|
||||||
for (i32 ix = MAX(x, 0); ix < MIN(x + w, tim->w); ix++) {
|
for (i32 ix = MAX(x, scope.x); ix < MIN(x + w, scope.x + scope.w); ix++) {
|
||||||
tim->cells[ix + iy * tim->w] = cell;
|
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) {
|
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 ) {
|
TimRect scope = tim->scopes[tim->scope];
|
||||||
i32 end = MIN(x + w, tim->w);
|
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;
|
bool wide = false;
|
||||||
for (i32 i = 0; s[i] && x < end; x++) {
|
for (i32 i = 0; s[i] && x < end; x++) {
|
||||||
TimCell c = tim_cell(&s[i], fg, bg);
|
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) {
|
void tim_draw_invert(i32 x, i32 y, i32 w) {
|
||||||
if (y >= 0 && y < tim->h && w > 0) {
|
TimRect scope = tim->scopes[tim->scope];
|
||||||
for (i32 i = MAX(x, 0); i < MIN(x + w, tim->w); i++) {
|
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];
|
TimCell c = tim->cells[i + y * tim->w];
|
||||||
tim->cells[i + y * tim->w].fg = c.bg;
|
tim->cells[i + y * tim->w].fg = c.bg;
|
||||||
tim->cells[i + y * tim->w].bg = c.fg;
|
tim->cells[i + y * tim->w].bg = c.fg;
|
||||||
|
|||||||
@@ -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) {
|
i32 tim_enter_scope(i32 x, i32 y, i32 w, i32 h) {
|
||||||
if (tim->scope + 1 >= TIM_MAX_SCOPE) {
|
if (tim->scope + 1 >= TIM_MAX_SCOPE)
|
||||||
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;
|
||||||
|
|||||||
68
src/scroll.c
68
src/scroll.c
@@ -2,11 +2,13 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
void TimScrollState_selectNext(TimScrollState* l){
|
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){
|
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){
|
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
|
// draw scroll bar and shrink items_scope
|
||||||
if(l->show_scroll_bar_horizontal){
|
if(l->show_scroll_bar_vertical){
|
||||||
items_scope.w -= 1;
|
items_scope.w -= 1;
|
||||||
i32 scrollbar_x = items_scope.x + items_scope.w;
|
TimRect arrow_up_r = {
|
||||||
i32 scrollbar_y = items_scope.y;
|
.x = items_scope.x + items_scope.w,
|
||||||
i32 scrollbar_h = items_scope.h - 2;
|
.y = items_scope.y,
|
||||||
f32 slider_y_normalized = 0;
|
.w = 1,
|
||||||
i32 slider_h = 0;
|
.h = 1
|
||||||
if(l->len != 0){
|
};
|
||||||
slider_y_normalized = (f32)l->cur / l->len + 0.001f;
|
TimRect scrollbar_r = {
|
||||||
slider_h = ceilf((f32)scrollbar_h / l->len);
|
.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
|
tim->scopes[tim->scope] = items_scope; // fit current scope inside frame and scrollbar
|
||||||
|
|||||||
Reference in New Issue
Block a user