[Add] Clicky focus for lists as well as keyboard input.
This commit is contained in:
parent
8881dc5265
commit
820c477d7b
@ -58,7 +58,7 @@ static void commodity_exchange(void) {
|
|||||||
"Close", commodity_exchange_close);
|
"Close", commodity_exchange_close);
|
||||||
|
|
||||||
window_addList(secondary_wid, 20, -40, COMMODITY_WIDTH-30, COMMODITY_HEIGHT-80-BUTTON_HEIGHT,
|
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) {
|
static void commodity_exchange_close(char* str) {
|
||||||
|
@ -33,7 +33,7 @@ typedef struct Widget_ {
|
|||||||
union {
|
union {
|
||||||
// Widget button.
|
// Widget button.
|
||||||
struct {
|
struct {
|
||||||
void(*fptr) (char*); // Callback.
|
void(*fptr) (char*); // Active callback.
|
||||||
char* display; // Stored text.
|
char* display; // Stored text.
|
||||||
} btn;
|
} btn;
|
||||||
// Widget text.
|
// Widget text.
|
||||||
@ -53,6 +53,7 @@ typedef struct Widget_ {
|
|||||||
int noptions; // total number of options.
|
int noptions; // total number of options.
|
||||||
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) (char*); // Modify callback.
|
||||||
} lst;
|
} lst;
|
||||||
} dat;
|
} dat;
|
||||||
} Widget;
|
} Widget;
|
||||||
@ -87,10 +88,12 @@ static Widget* window_getwgt(const unsigned int wid, char* name);
|
|||||||
// Input.
|
// Input.
|
||||||
static void toolkit_mouseEvent(SDL_Event* event);
|
static void toolkit_mouseEvent(SDL_Event* event);
|
||||||
static int toolkit_keyEvent(SDL_Event* event);
|
static int toolkit_keyEvent(SDL_Event* event);
|
||||||
|
// Focus.
|
||||||
static void toolkit_nextFocus(void);
|
static void toolkit_nextFocus(void);
|
||||||
static void toolkit_triggerFocus(void);
|
static void toolkit_triggerFocus(void);
|
||||||
static Widget* toolkit_getFocus(void);
|
static Widget* toolkit_getFocus(void);
|
||||||
static void toolkit_listScroll(Widget* wgt, int direction);
|
static void toolkit_listScroll(Widget* wgt, int direction);
|
||||||
|
static void toolkit_listFocus(Widget* lst, double bx, double by);
|
||||||
// Render.
|
// Render.
|
||||||
static void window_render(Window* w);
|
static void window_render(Window* w);
|
||||||
static void toolkit_renderButton(Widget* btn, double bx, double by);
|
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,
|
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);
|
Window *wdw = window_wget(wid);
|
||||||
Widget* wgt = window_newWidget(wdw);
|
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.noptions = nitems;
|
||||||
wgt->dat.lst.selected = defitem; // -1 would be none.
|
wgt->dat.lst.selected = defitem; // -1 would be none.
|
||||||
wgt->dat.lst.pos = 0;
|
wgt->dat.lst.pos = 0;
|
||||||
|
wgt->dat.lst.fptr = call;
|
||||||
wgt->w = (double) w;
|
wgt->w = (double) w;
|
||||||
wgt->h = (double) h - ((h % (gl_defFont.h+2)) + 2);
|
wgt->h = (double) h - ((h % (gl_defFont.h+2)) + 2);
|
||||||
if(x < 0) wgt->x = wdw->w - wgt->w + x;
|
if(x < 0) wgt->x = wdw->w - wgt->w + x;
|
||||||
@ -787,6 +792,10 @@ static void toolkit_mouseEvent(SDL_Event* event) {
|
|||||||
case SDL_MOUSEBUTTONDOWN:
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
wgt->status = WIDGET_STATUS_MOUSEDOWN;
|
wgt->status = WIDGET_STATUS_MOUSEDOWN;
|
||||||
w->focus = i;
|
w->focus = i;
|
||||||
|
|
||||||
|
if(wgt->type == WIDGET_LIST)
|
||||||
|
toolkit_listFocus(wgt, x-wgt->x, y-wgt->y);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case SDL_MOUSEBUTTONUP:
|
case SDL_MOUSEBUTTONUP:
|
||||||
if(wgt->status == WIDGET_STATUS_MOUSEDOWN) {
|
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 -= direction;
|
||||||
wgt->dat.lst.selected = MAX(0, wgt->dat.lst.selected);
|
wgt->dat.lst.selected = MAX(0, wgt->dat.lst.selected);
|
||||||
wgt->dat.lst.selected = MIN(wgt->dat.lst.selected, wgt->dat.lst.noptions-1);
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
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.
|
// Return the focused widget.
|
||||||
static Widget* toolkit_getFocus(void) {
|
static Widget* toolkit_getFocus(void) {
|
||||||
Window* wdw;
|
Window* wdw;
|
||||||
|
@ -22,7 +22,8 @@ void window_addImage(const unsigned int wid, const int x, const int y,
|
|||||||
void window_addList(const unsigned int wid,
|
void window_addList(const unsigned int wid,
|
||||||
const int x, const int y, // Position.
|
const int x, const int y, // Position.
|
||||||
const int w, const int h, // Size.
|
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
|
// Modification
|
||||||
void window_modifyText(const unsigned int wid, char* name, char* newstring);
|
void window_modifyText(const unsigned int wid, char* name, char* newstring);
|
||||||
|
Loading…
Reference in New Issue
Block a user