[Change] Cleaned up the scrollbar renderer.

This commit is contained in:
Allanis 2014-03-08 21:17:27 +00:00
parent fff2863c86
commit 2f5ca43840
2 changed files with 45 additions and 20 deletions

View File

@ -71,6 +71,7 @@ typedef struct Widget_ {
int selected; /* Currently selected option. */ int selected; /* Currently selected option. */
int pos; /* Current topmost option (in view). */ int pos; /* Current topmost option (in view). */
void (*fptr) (unsigned int, char*); /* Modify callback. */ void (*fptr) (unsigned int, char*); /* Modify callback. */
int height; /**< Real height. */
} lst; } lst;
/* Widget rect. */ /* Widget rect. */
struct { struct {
@ -172,6 +173,7 @@ static void toolkit_renderInput(Widget* inp, double bx, double by);
static void toolkit_renderImageArray(Widget* iar, double bx, double by); static void toolkit_renderImageArray(Widget* iar, double bx, double by);
static void toolkit_drawOutline(double x, double y, double w, static void toolkit_drawOutline(double x, double y, double w,
double h, double b, glColour* c, glColour* lc); double h, double b, glColour* c, glColour* lc);
static void toolkit_drawScrollbar(double x, double y, double w, double h, double pos);
static void toolkit_clip(double x, double y, double w, double h); static void toolkit_clip(double x, double y, double w, double h);
static void toolkit_unclip(void); static void toolkit_unclip(void);
static void toolkit_drawRect(double x, double y, double w, double h, static void toolkit_drawRect(double x, double y, double w, double h,
@ -288,6 +290,12 @@ void window_addList(const unsigned int wid, const int x, const int y,
wgt->h = (double) h - ((h % (gl_defFont.h+2)) + 2); wgt->h = (double) h - ((h % (gl_defFont.h+2)) + 2);
toolkit_setPos(wdw, wgt, x, y); toolkit_setPos(wdw, wgt, x, y);
/* Check if needs scrollbar. */
if(2 + (nitems * (gl_defFont.h + 2)) > (int)wgt->h)
wgt->dat.lst.height = (2 + gl_defFont.h) * nitems + 2;
else
wgt->dat.lst.height = 0;
if(wdw->focus == -1) if(wdw->focus == -1)
/* Initialize the focus. */ /* Initialize the focus. */
toolkit_nextFocus(); toolkit_nextFocus();
@ -1137,8 +1145,10 @@ static void toolkit_renderImage(Widget* img, double bx, double by) {
/* Render the list. */ /* Render the list. */
static void toolkit_renderList(Widget* lst, double bx, double by) { static void toolkit_renderList(Widget* lst, double bx, double by) {
int i; int i;
double x, y, tx, ty; double x, y, tx, ty, miny;
double w, scroll_pos;
w = lst->w;
x = bx + lst->x; x = bx + lst->x;
y = by + lst->y; y = by + lst->y;
@ -1150,22 +1160,36 @@ static void toolkit_renderList(Widget* lst, double bx, double by) {
/* Outter outline. */ /* Outter outline. */
toolkit_drawOutline(x, y, lst->w, lst->h, 1., toolkit_colDark, NULL); toolkit_drawOutline(x, y, lst->w, lst->h, 1., toolkit_colDark, NULL);
/* Draw scrollbar. */
if(lst->dat.lst.height > 0) {
/* We need to make room for list. */
w -= 10.;
scroll_pos = (double)lst->dat.lst.pos / ((double)lst->dat.lst.height - lst->h);
toolkit_drawScrollbar(x + lst->w - 10., y, 10., lst->h, scroll_pos);
}
/* Draw selected. */ /* Draw selected. */
toolkit_drawRect(x, toolkit_drawRect(x,
y-1.+lst->h-(1+lst->dat.lst.selected-lst->dat.lst.pos)*(gl_defFont.h+2.), y-1.+lst->h-(1+lst->dat.lst.selected-lst->dat.lst.pos)*(gl_defFont.h+2.),
lst->w, gl_defFont.h+2., &cHilight, NULL); w, gl_defFont.h+2., &cHilight, NULL);
/* Draw content. */ /* Draw content. */
tx = (double)SCREEN_W/2. + x+2.; tx = (double)SCREEN_W/2. + x+2.;
ty = (double)SCREEN_H/2. + y+lst->h - 2. - gl_defFont.h; ty = (double)SCREEN_H/2. + y+lst->h - 2. - gl_defFont.h;
miny = ty - lst->h;
y = ty-2.; y = ty-2.;
w -= 4;
for(i = lst->dat.lst.pos; i < lst->dat.lst.noptions; i++) { for(i = lst->dat.lst.pos; i < lst->dat.lst.noptions; i++) {
gl_printMax(&gl_defFont, (int)lst->w-4, gl_printMax(&gl_defFont, (int)w-4,
tx, ty, &cBlack, lst->dat.lst.options[i]); tx, ty, &cBlack, lst->dat.lst.options[i]);
ty -= 2 + gl_defFont.h; ty -= 2 + gl_defFont.h;
if(ty-y > lst->h) break; /* Check if out if bounds. */
if(ty < miny) break;
} }
} }
@ -1242,7 +1266,7 @@ static void toolkit_renderInput(Widget* inp, double bx, double by) {
static void toolkit_renderImageArray(Widget* iar, double bx, double by) { static void toolkit_renderImageArray(Widget* iar, double bx, double by) {
int i, j; int i, j;
double x, y, w, h, xcurs, ycurs; double x, y, w, h, xcurs, ycurs;
double scroll_pos, sx, sy; double scroll_pos;
int xelem, yelem, xspace; int xelem, yelem, xspace;
glColour* c, *dc, *lc; glColour* c, *dc, *lc;
int is_selected; int is_selected;
@ -1263,21 +1287,8 @@ static void toolkit_renderImageArray(Widget* iar, double bx, double by) {
toolkit_drawRect(x, y, iar->w, iar->h, &cBlack, NULL); toolkit_drawRect(x, y, iar->w, iar->h, &cBlack, NULL);
/* Scrollbar. */ /* Scrollbar. */
/* scrollbar background. */
toolkit_drawRect(x+iar->w - 10., y, 10., iar->h,
toolkit_colDark, toolkit_col);
toolkit_drawOutline(x + iar->w - 10., y, 10., iar->h, 1.,
toolkit_colLight, toolkit_col);
toolkit_drawOutline(x + iar->w - 10., y, 10., iar->h, 2.,
toolkit_colDark, NULL);
/* The bar */
scroll_pos = iar->dat.iar.pos / (h * (yelem - (int)(iar->h / h))); scroll_pos = iar->dat.iar.pos / (h * (yelem - (int)(iar->h / h)));
sx = x + iar->w - 10.; toolkit_drawScrollbar(x + iar->w - 10., y, 10., iar->h, scroll_pos);
sy = y + iar->h - (iar->h - 30.) * scroll_pos - 30.;
toolkit_drawRect(sx, sy, 10., 30., toolkit_colLight, toolkit_col);
toolkit_drawOutline(sx, sy, 10., 30., 0., toolkit_colDark, NULL);
/* Main drawing loop. */ /* Main drawing loop. */
toolkit_clip(x, y, iar->w, iar->h); toolkit_clip(x, y, iar->w, iar->h);
@ -1335,6 +1346,20 @@ static void toolkit_renderImageArray(Widget* iar, double bx, double by) {
toolkit_drawOutline(x, y+1, iar->w-1, iar->h-1, 2., toolkit_colDark, NULL); toolkit_drawOutline(x, y+1, iar->w-1, iar->h-1, 2., toolkit_colDark, NULL);
} }
static void toolkit_drawScrollbar(double x, double y, double w, double h, double pos) {
double sy;
/* Scrollbar background. */
toolkit_drawRect(x, y, w, h, toolkit_colDark, toolkit_col);
toolkit_drawOutline(x, y, w, h, 1., toolkit_colLight, toolkit_col);
toolkit_drawOutline(x, y, w, h, 2., toolkit_colDark, NULL);
/* Bar itself. */
sy = y + (h - 30.) * (1.-pos);
toolkit_drawRect(x, sy, w, 30., toolkit_colLight, toolkit_col);
toolkit_drawOutline(x, sy, w, 30., 0., toolkit_colDark, NULL);
}
/* Handle input for input widget. */ /* Handle input for input widget. */
static int toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key) { static int toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key) {
int n; int n;

0
test
View File