X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Frtk.c;h=4049f882cfd5e89b6e88ff93e078cecbcd5a9118;hb=3c3612761bcf77a5c4ec6810aa47e0e3f0131bf1;hp=43de80db560eebb84e4a4b420e8c6a88f6fcd111;hpb=c4a043b45ff38d532579cfdf0b6a7e3f50527559;p=retroray diff --git a/src/rtk.c b/src/rtk.c index 43de80d..4049f88 100644 --- a/src/rtk.c +++ b/src/rtk.c @@ -2,10 +2,21 @@ #include #include "imago2.h" #include "rtk_impl.h" +#include "app.h" static rtk_draw_ops gfx; static void calc_widget_rect(rtk_widget *w, rtk_rect *rect); +static void draw_window(rtk_widget *w); +static void draw_button(rtk_widget *w); +static void draw_checkbox(rtk_widget *w); +static void draw_separator(rtk_widget *w); + +static void invalfb(rtk_widget *w); + + +static rtk_widget *hover, *focused, *pressed; + void rtk_setup(rtk_draw_ops *drawop) { @@ -48,7 +59,7 @@ void rtk_move(rtk_widget *w, int x, int y) { w->any.x = x; w->any.y = y; - w->any.flags |= GEOMCHG; + w->any.flags |= GEOMCHG | DIRTY; } void rtk_pos(rtk_widget *w, int *xptr, int *yptr) @@ -61,7 +72,7 @@ void rtk_resize(rtk_widget *w, int xsz, int ysz) { w->any.width = xsz; w->any.height = ysz; - w->any.flags |= GEOMCHG; + w->any.flags |= GEOMCHG | DIRTY; } void rtk_size(rtk_widget *w, int *xptr, int *yptr) @@ -70,6 +81,14 @@ void rtk_size(rtk_widget *w, int *xptr, int *yptr) *yptr = w->any.height; } +void rtk_get_rect(rtk_widget *w, rtk_rect *r) +{ + r->x = w->any.x; + r->y = w->any.y; + r->width = w->any.width; + r->height = w->any.height; +} + int rtk_set_text(rtk_widget *w, const char *str) { rtk_rect rect; @@ -81,6 +100,7 @@ int rtk_set_text(rtk_widget *w, const char *str) calc_widget_rect(w, &rect); rtk_resize(w, rect.width, rect.height); + rtk_invalidate(w); return 0; } @@ -92,6 +112,7 @@ const char *rtk_get_text(rtk_widget *w) void rtk_set_value(rtk_widget *w, int val) { w->any.value = val; + rtk_invalidate(w); } int rtk_get_value(rtk_widget *w) @@ -105,6 +126,33 @@ void rtk_set_callback(rtk_widget *w, rtk_callback cbfunc, void *cls) w->any.cbcls = cls; } +void rtk_show(rtk_widget *w) +{ + w->any.flags |= VISIBLE; + rtk_invalidate(w); +} + +void rtk_hide(rtk_widget *w) +{ + w->any.flags &= ~VISIBLE; + invalfb(w); +} + +int rtk_visible(const rtk_widget *w) +{ + return w->any.flags & VISIBLE; +} + +void rtk_invalidate(rtk_widget *w) +{ + w->any.flags |= DIRTY; +} + +void rtk_validate(rtk_widget *w) +{ + w->any.flags &= ~DIRTY; +} + void rtk_win_layout(rtk_widget *w, int layout) { w->win.layout = layout; @@ -123,6 +171,7 @@ void rtk_win_clear(rtk_widget *w) } w->win.clist = w->win.ctail = 0; + rtk_invalidate(w); } void rtk_win_add(rtk_widget *par, rtk_widget *child) @@ -146,6 +195,7 @@ void rtk_win_add(rtk_widget *par, rtk_widget *child) child->any.next = 0; child->any.par = par; + rtk_invalidate(par); } void rtk_win_rm(rtk_widget *par, rtk_widget *child) @@ -167,6 +217,7 @@ void rtk_win_rm(rtk_widget *par, rtk_widget *child) prev = prev->any.next; } par->win.clist = dummy.any.next; + rtk_invalidate(par); } int rtk_win_has(rtk_widget *par, rtk_widget *child) @@ -186,11 +237,23 @@ int rtk_win_has(rtk_widget *par, rtk_widget *child) } /* --- button functions --- */ +void rtk_bn_mode(rtk_widget *w, int mode) +{ + RTK_ASSERT_TYPE(w, RTK_BUTTON); + w->bn.mode = mode; + rtk_invalidate(w); +} void rtk_bn_set_icon(rtk_widget *w, rtk_icon *icon) { + rtk_rect rect; + RTK_ASSERT_TYPE(w, RTK_BUTTON); w->bn.icon = icon; + + calc_widget_rect(w, &rect); + rtk_resize(w, rect.width, rect.height); + rtk_invalidate(w); } rtk_icon *rtk_bn_get_icon(rtk_widget *w) @@ -201,17 +264,21 @@ rtk_icon *rtk_bn_get_icon(rtk_widget *w) /* --- constructors --- */ -rtk_widget *rtk_create_window(rtk_widget *par, const char *title, int x, int y, int width, int height) +rtk_widget *rtk_create_window(rtk_widget *par, const char *title, int x, int y, + int width, int height, unsigned int flags) { rtk_widget *w; if(!(w = rtk_create_widget())) { return 0; } + w->type = RTK_WIN; if(par) rtk_win_add(par, w); rtk_set_text(w, title); rtk_move(w, x, y); rtk_resize(w, width, height); + + w->any.flags |= flags << 16; return w; } @@ -222,6 +289,7 @@ rtk_widget *rtk_create_button(rtk_widget *par, const char *str, rtk_callback cbf if(!(w = rtk_create_widget())) { return 0; } + w->type = RTK_BUTTON; if(par) rtk_win_add(par, w); rtk_set_text(w, str); rtk_set_callback(w, cbfunc, 0); @@ -235,6 +303,7 @@ rtk_widget *rtk_create_iconbutton(rtk_widget *par, rtk_icon *icon, rtk_callback if(!(w = rtk_create_widget())) { return 0; } + w->type = RTK_BUTTON; if(par) rtk_win_add(par, w); rtk_bn_set_icon(w, icon); rtk_set_callback(w, cbfunc, 0); @@ -248,6 +317,7 @@ rtk_widget *rtk_create_label(rtk_widget *par, const char *text) if(!(w = rtk_create_widget())) { return 0; } + w->type = RTK_LABEL; if(par) rtk_win_add(par, w); rtk_set_text(w, text); return w; @@ -260,6 +330,7 @@ rtk_widget *rtk_create_checkbox(rtk_widget *par, const char *text, int chk, rtk_ if(!(w = rtk_create_widget())) { return 0; } + w->type = RTK_CHECKBOX; if(par) rtk_win_add(par, w); rtk_set_text(w, text); rtk_set_value(w, chk ? 1 : 0); @@ -267,6 +338,19 @@ rtk_widget *rtk_create_checkbox(rtk_widget *par, const char *text, int chk, rtk_ return w; } +rtk_widget *rtk_create_separator(rtk_widget *par) +{ + rtk_widget *w; + + if(!(w = rtk_create_widget())) { + return 0; + } + w->type = RTK_SEP; + if(par) rtk_win_add(par, w); + return w; +} + + /* --- icon functions --- */ rtk_iconsheet *rtk_load_iconsheet(const char *fname) { @@ -334,6 +418,11 @@ static void calc_widget_rect(rtk_widget *w, rtk_rect *rect) } switch(w->type) { + case RTK_WIN: + rect->width = w->any.width; + rect->height = w->any.height; + break; + case RTK_BUTTON: if(w->bn.icon) { rect->width = w->bn.icon->width + OFFS * 2; @@ -354,6 +443,18 @@ static void calc_widget_rect(rtk_widget *w, rtk_rect *rect) rect->height = txrect.height + PAD * 2; break; + case RTK_SEP: + if(w->any.par->win.layout == RTK_VBOX) { + rect->width = w->any.par->any.width - PAD * 2; + rect->height = PAD * 4 + BEVELSZ * 2; + } else if(w->any.par->win.layout == RTK_HBOX) { + rect->width = PAD * 4 + BEVELSZ * 2; + rect->height = w->any.par->any.height - PAD * 2; + } else { + rect->width = rect->height = 0; + } + break; + default: rect->width = rect->height = 0; } @@ -383,6 +484,7 @@ static void calc_layout(rtk_widget *w) { int x, y; rtk_widget *c; + rtk_rect rect; if(w->any.type == RTK_WIN && w->win.layout != RTK_NONE) { x = y = PAD; @@ -393,28 +495,40 @@ static void calc_layout(rtk_widget *w) calc_layout(c); if(w->win.layout == RTK_VBOX) { - y += c->any.height + PAD; + y += c->any.height + PAD * 2; } else { - x += c->any.width + PAD; + x += c->any.width + PAD * 2; } c = c->any.next; } } - w->any.flags = (w->any.flags & ~GEOMCHG) | DIRTY; -} + calc_widget_rect(w, &rect); + w->any.width = rect.width; + w->any.height = rect.height; -static void draw_window(rtk_widget *w); -static void draw_button(rtk_widget *w); -static void draw_checkbox(rtk_widget *w); + w->any.flags &= ~GEOMCHG; + rtk_invalidate(w); +} void rtk_draw_widget(rtk_widget *w) { + int dirty; + + if(!(w->any.flags & VISIBLE)) { + return; + } + if(need_relayout(w)) { calc_layout(w); } + dirty = w->any.flags & DIRTY; + if(!dirty && w->any.type != RTK_WIN) { + return; + } + switch(w->any.type) { case RTK_WIN: draw_window(w); @@ -428,11 +542,18 @@ void rtk_draw_widget(rtk_widget *w) draw_checkbox(w); break; + case RTK_SEP: + draw_separator(w); + break; + default: break; } - w->any.flags &= ~DIRTY; + if(dirty) { + rtk_validate(w); + invalfb(w); + } } static void widget_rect(rtk_widget *w, rtk_rect *rect) @@ -460,34 +581,363 @@ static void abs_pos(rtk_widget *w, int *xpos, int *ypos) *ypos = y; } -#define COL_BG 0xa0a0a0 -#define COL_LBEV 0xcccccc -#define COL_SBEV 0x202020 -#define COL_TEXT 0 +#define COL_BG 0xff666666 +#define COL_BGHL 0xff808080 +#define COL_LBEV 0xffaaaaaa +#define COL_SBEV 0xff222222 +#define COL_TEXT 0xff000000 +#define COL_WINFRM 0xff2244aa +#define COL_WINFRM_LIT 0xff4466ff +#define COL_WINFRM_SHAD 0xff051166 -static void draw_window(rtk_widget *w) +static void hline(int x, int y, int sz, uint32_t col) { rtk_rect rect; + rect.x = x; + rect.y = y; + rect.width = sz; + rect.height = 1; + gfx.fill(&rect, col); +} - widget_rect(w, &rect); - gfx.fill(&rect, COL_BG); +static void vline(int x, int y, int sz, uint32_t col) +{ + rtk_rect rect; + rect.x = x; + rect.y = y; + rect.width = 1; + rect.height = sz; + gfx.fill(&rect, col); +} + +enum {FRM_SOLID, FRM_OUTSET, FRM_INSET}; + +enum {UICOL_BG, UICOL_LBEV, UICOL_SBEV}; +static uint32_t uicol[3]; + +static void uicolor(uint32_t col, uint32_t lcol, uint32_t scol) +{ + uicol[UICOL_BG] = col; + uicol[UICOL_LBEV] = lcol; + uicol[UICOL_SBEV] = scol; +} + +static void draw_frame(rtk_rect *rect, int type) +{ + int tlcol, brcol; + + switch(type) { + case FRM_SOLID: + tlcol = brcol = 0xff000000; + break; + case FRM_OUTSET: + tlcol = uicol[UICOL_LBEV]; + brcol = uicol[UICOL_SBEV]; + break; + case FRM_INSET: + tlcol = uicol[UICOL_SBEV]; + brcol = uicol[UICOL_LBEV]; + break; + default: + break; + } + + hline(rect->x, rect->y, rect->width, tlcol); + vline(rect->x, rect->y + 1, rect->height - 2, tlcol); + hline(rect->x, rect->y + rect->height - 1, rect->width, brcol); + vline(rect->x + rect->width - 1, rect->y + 1, rect->height - 2, brcol); +} + +#define WINFRM_SZ 2 +#define WINFRM_TBAR 16 + +static void draw_window(rtk_widget *w) +{ + rtk_rect rect, frmrect, tbrect; + rtk_widget *c; + int win_dirty = w->any.flags & DIRTY; + + if(win_dirty) { + widget_rect(w, &rect); + + if(w->any.flags & FRAME) { + uicolor(COL_WINFRM, COL_WINFRM_LIT, COL_WINFRM_SHAD); + + frmrect = rect; + frmrect.width += WINFRM_SZ * 2; + frmrect.height += WINFRM_SZ * 2 + WINFRM_TBAR; + frmrect.x -= WINFRM_SZ; + frmrect.y -= WINFRM_SZ + WINFRM_TBAR; + + tbrect.x = rect.x; + tbrect.y = rect.y - WINFRM_TBAR; + tbrect.width = rect.width; + tbrect.height = WINFRM_TBAR; + + draw_frame(&frmrect, FRM_OUTSET); + frmrect.x++; + frmrect.y++; + frmrect.width -= 2; + frmrect.height -= 2; + draw_frame(&frmrect, FRM_INSET); + + draw_frame(&tbrect, FRM_OUTSET); + tbrect.x++; + tbrect.y++; + tbrect.width -= 2; + tbrect.height -= 2; + gfx.fill(&tbrect, COL_WINFRM); + } + + gfx.fill(&rect, COL_BG); + } + + c = w->win.clist; + while(c) { + if(win_dirty) { + rtk_invalidate(c); + } + rtk_draw_widget(c); + c = c->any.next; + } } static void draw_button(rtk_widget *w) { + int pressed; rtk_rect rect; widget_rect(w, &rect); abs_pos(w, &rect.x, &rect.y); - gfx.fill(&rect, COL_BG); + if(w->bn.mode == RTK_TOGGLEBN) { + pressed = w->any.value; + } else { + pressed = w->any.flags & PRESS; + } + + uicolor(COL_BG, COL_LBEV, COL_SBEV); + + if(rect.width > 2 && rect.height > 2) { + draw_frame(&rect, pressed ? FRM_INSET : FRM_OUTSET); + + rect.x++; + rect.y++; + rect.width -= 2; + rect.height -= 2; + } + + gfx.fill(&rect, w->any.flags & HOVER ? COL_BGHL : COL_BG); if(w->bn.icon) { - gfx.blit(rect.x + OFFS, rect.y + OFFS, w->bn.icon); + int offs = w->any.flags & PRESS ? PAD + 1 : PAD; + gfx.blit(rect.x + offs, rect.y + offs, w->bn.icon); } else { - gfx.fill(&rect, 0x802020); + gfx.fill(&rect, 0xff802020); } } static void draw_checkbox(rtk_widget *w) { } + +static void draw_separator(rtk_widget *w) +{ + rtk_widget *win = w->any.par; + rtk_rect rect; + + if(!win) return; + + uicolor(COL_BG, COL_LBEV, COL_SBEV); + + widget_rect(w, &rect); + abs_pos(w, &rect.x, &rect.y); + + switch(win->win.layout) { + case RTK_VBOX: + rect.y += PAD * 2; + rect.height = 2; + break; + + case RTK_HBOX: + rect.x += PAD * 2; + rect.width = 2; + break; + + default: + break; + } + + draw_frame(&rect, FRM_INSET); +} + + +static int hittest(rtk_widget *w, int x, int y) +{ + if(x < w->any.x || y < w->any.y) return 0; + if(x >= w->any.x + w->any.width) return 0; + if(y >= w->any.y + w->any.height) return 0; + return 1; +} + +static void sethover(rtk_widget *w) +{ + if(hover == w) return; + + if(hover) { + hover->any.flags &= ~HOVER; + + if(hover->type != RTK_WIN) { + rtk_invalidate(hover); + invalfb(hover); + } + } + hover = w; + if(w) { + w->any.flags |= HOVER; + + if(w->type != RTK_WIN) { + rtk_invalidate(w); + invalfb(w); + } + } +} + +static void setpress(rtk_widget *w) +{ + if(pressed == w) return; + + if(pressed) { + pressed->any.flags &= ~PRESS; + rtk_invalidate(pressed); + invalfb(pressed); + } + pressed = w; + if(w) { + w->any.flags |= PRESS; + rtk_invalidate(w); + invalfb(w); + } +} + +static void click(rtk_widget *w, int x, int y) +{ + switch(w->type) { + case RTK_BUTTON: + if(w->bn.mode == RTK_TOGGLEBN) { + case RTK_CHECKBOX: + w->any.value ^= 1; + } + if(w->any.cbfunc) { + w->any.cbfunc(w, w->any.cbcls); + } + rtk_invalidate(w); + invalfb(w); + break; + + default: + break; + } +} + +int rtk_input_key(rtk_widget *w, int key, int press) +{ + return 0; +} + +int rtk_input_mbutton(rtk_widget *w, int bn, int press, int x, int y) +{ + if(!hittest(w, x, y)) { + return 0; + } + + if(press) { + if(hover && hittest(hover, x, y)) { + setpress(hover); + } + } else { + if(pressed && hittest(pressed, x, y)) { + click(pressed, x, y); + } + setpress(0); + } + + return 1; +} + +int rtk_input_mmotion(rtk_widget *w, int x, int y) +{ + rtk_widget *c; + + if(!hittest(w, x, y)) { + int res = hover ? 1 : 0; + sethover(0); + return res; + } + + if(w->type == RTK_WIN) { + c = w->win.clist; + while(c) { + if(hittest(c, x, y)) { + return rtk_input_mmotion(c, x, y); + } + c = c->any.next; + } + } + + if(hover != w) { + sethover(w); + return 1; + } + return 0; +} + +void rtk_fix_rect(rtk_rect *rect) +{ + int x, y, w, h; + + x = rect->x; + y = rect->y; + + if(rect->width < 0) { + w = -rect->width; + x += rect->width; + } else { + w = rect->width; + } + if(rect->height < 0) { + h = -rect->height; + y += rect->height; + } else { + h = rect->height; + } + + rect->x = x; + rect->y = y; + rect->width = w; + rect->height = h; +} + +void rtk_rect_union(rtk_rect *a, const rtk_rect *b) +{ + int x0, y0, x1, y1; + + x0 = a->x; + y0 = a->y; + x1 = a->x + a->width; + y1 = a->y + a->height; + + if(b->x < x0) x0 = b->x; + if(b->y < y0) y0 = b->y; + if(b->x + b->width > x1) x1 = b->x + b->width; + if(b->y + b->height > y1) y1 = b->y + b->height; + + a->x = x0; + a->y = y0; + a->width = x1 - x0; + a->height = y1 - y0; +} + +static void invalfb(rtk_widget *w) +{ + app_redisplay(w->any.x, w->any.y, w->any.width, w->any.height); +}