[Add] Clicky focus for lists as well as keyboard input.

This commit is contained in:
Allanis 2013-03-06 20:37:58 +00:00
parent 8881dc5265
commit 820c477d7b
3 changed files with 22 additions and 4 deletions

View File

@ -58,7 +58,7 @@ static void commodity_exchange(void) {
"Close", commodity_exchange_close);
window_addList(secondary_wid, 20, -40, COMMODITY_WIDTH-30, COMMODITY_HEIGHT-80-BUTTON_HEIGHT,
"lstGoods", goods, ngoods, 0);
"lstGoods", goods, ngoods, 0, NULL);
}
static void commodity_exchange_close(char* str) {

View File

@ -33,7 +33,7 @@ typedef struct Widget_ {
union {
// Widget button.
struct {
void(*fptr) (char*); // Callback.
void(*fptr) (char*); // Active callback.
char* display; // Stored text.
} btn;
// Widget text.
@ -53,6 +53,7 @@ typedef struct Widget_ {
int noptions; // total number of options.
int selected; // Currently selected option.
int pos; // Current topmost option (in view).
void (*fptr) (char*); // Modify callback.
} lst;
} dat;
} Widget;
@ -87,10 +88,12 @@ static Widget* window_getwgt(const unsigned int wid, char* name);
// Input.
static void toolkit_mouseEvent(SDL_Event* event);
static int toolkit_keyEvent(SDL_Event* event);
// Focus.
static void toolkit_nextFocus(void);
static void toolkit_triggerFocus(void);
static Widget* toolkit_getFocus(void);
static void toolkit_listScroll(Widget* wgt, int direction);
static void toolkit_listFocus(Widget* lst, double bx, double by);
// Render.
static void window_render(Window* w);
static void toolkit_renderButton(Widget* btn, double bx, double by);
@ -171,7 +174,8 @@ void window_addImage(const unsigned int wid, const int x, const int y,
}
void window_addList(const unsigned int wid, const int x, const int y,
const int w, const int h, char* name, char** items, int nitems, int defitem) {
const int w, const int h, char* name, char** items, int nitems, int defitem,
void(*call) (char*)) {
Window *wdw = window_wget(wid);
Widget* wgt = window_newWidget(wdw);
@ -183,6 +187,7 @@ void window_addList(const unsigned int wid, const int x, const int y,
wgt->dat.lst.noptions = nitems;
wgt->dat.lst.selected = defitem; // -1 would be none.
wgt->dat.lst.pos = 0;
wgt->dat.lst.fptr = call;
wgt->w = (double) w;
wgt->h = (double) h - ((h % (gl_defFont.h+2)) + 2);
if(x < 0) wgt->x = wdw->w - wgt->w + x;
@ -787,6 +792,10 @@ static void toolkit_mouseEvent(SDL_Event* event) {
case SDL_MOUSEBUTTONDOWN:
wgt->status = WIDGET_STATUS_MOUSEDOWN;
w->focus = i;
if(wgt->type == WIDGET_LIST)
toolkit_listFocus(wgt, x-wgt->x, y-wgt->y);
break;
case SDL_MOUSEBUTTONUP:
if(wgt->status == WIDGET_STATUS_MOUSEDOWN) {
@ -926,12 +935,20 @@ static void toolkit_listScroll(Widget* wgt, int direction) {
wgt->dat.lst.selected -= direction;
wgt->dat.lst.selected = MAX(0, wgt->dat.lst.selected);
wgt->dat.lst.selected = MIN(wgt->dat.lst.selected, wgt->dat.lst.noptions-1);
if(wgt->dat.lst.fptr) (*wgt->dat.lst.fptr)(wgt->name);
break;
default:
break;
}
}
// List mouse even focus.
static void toolkit_listFocus(Widget* lst, double bx, double by) {
(void)bx;
lst->dat.lst.selected = (lst->h - by) / (gl_defFont.h+2.);
toolkit_listScroll(lst, 0); // Check boundaries and trigger callback.
}
// Return the focused widget.
static Widget* toolkit_getFocus(void) {
Window* wdw;

View File

@ -22,7 +22,8 @@ void window_addImage(const unsigned int wid, const int x, const int y,
void window_addList(const unsigned int wid,
const int x, const int y, // Position.
const int w, const int h, // Size.
char* name, char** items, int nitems, int defitem);
char* name, char** items, int nitems, int defitem,
void(*call)(char*));
// Modification
void window_modifyText(const unsigned int wid, char* name, char* newstring);