[Add] Custom mouse events for custome widgets
This commit is contained in:
parent
c687e7a173
commit
7fb9bbedce
@ -111,7 +111,7 @@ static void outfits(void) {
|
|||||||
"Sell", outfits_sell);
|
"Sell", outfits_sell);
|
||||||
|
|
||||||
window_addCust(secondary_wid, -40-BUTTON_WIDTH, 60+2*BUTTON_HEIGHT,
|
window_addCust(secondary_wid, -40-BUTTON_WIDTH, 60+2*BUTTON_HEIGHT,
|
||||||
BUTTON_WIDTH, BUTTON_HEIGHT, "cstMod", 0, outfits_renderMod);
|
BUTTON_WIDTH, BUTTON_HEIGHT, "cstMod", 0, outfits_renderMod, NULL);
|
||||||
|
|
||||||
window_addText(secondary_wid, 40+200+20, -60,
|
window_addText(secondary_wid, 40+200+20, -60,
|
||||||
80, 96, 0, "txtSDesc", &gl_smallFont, &cDConsole,
|
80, 96, 0, "txtSDesc", &gl_smallFont, &cDConsole,
|
||||||
|
26
src/map.c
26
src/map.c
@ -20,9 +20,10 @@ static int map_selected = 0;
|
|||||||
extern StarSystem* systems_stack;
|
extern StarSystem* systems_stack;
|
||||||
extern int systems_nstack;
|
extern int systems_nstack;
|
||||||
|
|
||||||
static void map_render(double bx, double by, double w, double h);
|
|
||||||
static void map_close(char* str);
|
static void map_close(char* str);
|
||||||
static void map_update(void);
|
static void map_update(void);
|
||||||
|
static void map_render(double bx, double by, double w, double h);
|
||||||
|
static void map_mouse(Uint8 type, double mx, double my);
|
||||||
|
|
||||||
// Open the map window.
|
// Open the map window.
|
||||||
void map_open(void) {
|
void map_open(void) {
|
||||||
@ -50,7 +51,7 @@ void map_open(void) {
|
|||||||
&gl_smallFont, &cBlack, NULL);
|
&gl_smallFont, &cBlack, NULL);
|
||||||
|
|
||||||
window_addCust(map_wid, 20, 20, MAP_WIDTH - 150, MAP_HEIGHT - 60,
|
window_addCust(map_wid, 20, 20, MAP_WIDTH - 150, MAP_HEIGHT - 60,
|
||||||
"cstMap", 1, map_render);
|
"cstMap", 1, map_render, map_mouse);
|
||||||
|
|
||||||
window_addButton(map_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
|
window_addButton(map_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
|
||||||
"btnClose", "Close", map_close);
|
"btnClose", "Close", map_close);
|
||||||
@ -112,6 +113,12 @@ static void map_update(void) {
|
|||||||
// Render the map as a custom widget.
|
// Render the map as a custom widget.
|
||||||
static void map_render(double bx, double by, double w, double h) {
|
static void map_render(double bx, double by, double w, double h) {
|
||||||
int i;
|
int i;
|
||||||
|
double x, y, r;
|
||||||
|
StarSystem* sys;
|
||||||
|
|
||||||
|
r = 5.;
|
||||||
|
x = bx - map_xpos + w/2;
|
||||||
|
y = by - map_ypos + h/2;
|
||||||
// Background
|
// Background
|
||||||
COLOUR(cBlack);
|
COLOUR(cBlack);
|
||||||
glBegin(GL_QUADS);
|
glBegin(GL_QUADS);
|
||||||
@ -125,9 +132,18 @@ static void map_render(double bx, double by, double w, double h) {
|
|||||||
for(i = 0; i < systems_nstack; i++) {
|
for(i = 0; i < systems_nstack; i++) {
|
||||||
if(&systems_stack[i] == cur_system) COLOUR(cRadar_targ);
|
if(&systems_stack[i] == cur_system) COLOUR(cRadar_targ);
|
||||||
else COLOUR(cYellow);
|
else COLOUR(cYellow);
|
||||||
gl_drawCircleInRect(bx + systems_stack[i].pos.x - map_xpos + w/2,
|
gl_drawCircleInRect(x + systems_stack[i].pos.x,
|
||||||
by + systems_stack[i].pos.y - map_ypos + h/2,
|
y + systems_stack[i].pos.y,
|
||||||
5, bx, by, w, h);
|
r, bx, by, w, h);
|
||||||
}
|
}
|
||||||
|
// Selected planet.
|
||||||
|
sys = &systems_stack[map_selected];
|
||||||
|
COLOUR(cRed);
|
||||||
|
gl_drawCircleInRect(x + sys->pos.x, y + sys->pos.y, r+3., bx, by, w, h);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map event handling.
|
||||||
|
static void map_mouse(Uint8 type, double mx, double my) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,6 +66,7 @@ typedef struct Widget_ {
|
|||||||
struct {
|
struct {
|
||||||
int border;
|
int border;
|
||||||
void(*render) (double bx, double by, double bw, double bh);
|
void(*render) (double bx, double by, double bw, double bh);
|
||||||
|
void(*mouse) (Uint8 type, double bx, double by);
|
||||||
} cst;
|
} cst;
|
||||||
} dat;
|
} dat;
|
||||||
} Widget;
|
} Widget;
|
||||||
@ -129,8 +130,9 @@ static void toolkit_alertClose(char* str);
|
|||||||
|
|
||||||
// Add a button that when pressed will trigger call, passing it's name as the
|
// Add a button that when pressed will trigger call, passing it's name as the
|
||||||
// only parameter.
|
// only parameter.
|
||||||
void window_addButton(const unsigned int wid, const int x, const int y, const int w,
|
void window_addButton(const unsigned int wid, const int x, const int y,
|
||||||
const int h, char* name, char* display, void (*call)(char*)) {
|
const int w, const int h, char* name,
|
||||||
|
char* display, void (*call)(char*)) {
|
||||||
|
|
||||||
Window* wdw = window_wget(wid);
|
Window* wdw = window_wget(wid);
|
||||||
Widget* wgt = window_newWidget(wdw);
|
Widget* wgt = window_newWidget(wdw);
|
||||||
@ -200,8 +202,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,
|
||||||
void(*call) (char*)) {
|
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);
|
||||||
@ -226,8 +228,8 @@ void window_addList(const unsigned int wid, const int x, const int y,
|
|||||||
toolkit_nextFocus();
|
toolkit_nextFocus();
|
||||||
}
|
}
|
||||||
|
|
||||||
void window_addRect(const unsigned int wid, const int x, const int y, const int w,
|
void window_addRect(const unsigned int wid, const int x, const int y,
|
||||||
const int h, char* name, glColour* colour, int border) {
|
const int w, const int h, char* name, glColour* colour, int border) {
|
||||||
|
|
||||||
Window* wdw = window_wget(wid);
|
Window* wdw = window_wget(wid);
|
||||||
Widget* wgt = window_newWidget(wdw);
|
Widget* wgt = window_newWidget(wdw);
|
||||||
@ -248,7 +250,8 @@ void window_addRect(const unsigned int wid, const int x, const int y, const int
|
|||||||
|
|
||||||
void window_addCust(const unsigned int wid, const int x, const int y,
|
void window_addCust(const unsigned int wid, const int x, const int y,
|
||||||
const int w, const int h, char* name, const int border,
|
const int w, const int h, char* name, const int border,
|
||||||
void(*render) (double x, double y, double w, double h)) {
|
void(*render) (double x, double y, double w, double h),
|
||||||
|
void(*mouse) (Uint8 type, double x, double y)) {
|
||||||
|
|
||||||
Window* wdw = window_wget(wid);
|
Window* wdw = window_wget(wid);
|
||||||
Widget* wgt = window_newWidget(wdw);
|
Widget* wgt = window_newWidget(wdw);
|
||||||
@ -260,6 +263,7 @@ void window_addCust(const unsigned int wid, const int x, const int y,
|
|||||||
// Specific.
|
// Specific.
|
||||||
wgt->dat.cst.border = border;
|
wgt->dat.cst.border = border;
|
||||||
wgt->dat.cst.render = render;
|
wgt->dat.cst.render = render;
|
||||||
|
wgt->dat.cst.mouse = mouse;
|
||||||
|
|
||||||
// Position/size.
|
// Position/size.
|
||||||
wgt->w = (double)w;
|
wgt->w = (double)w;
|
||||||
@ -336,7 +340,8 @@ unsigned int window_get(const char* wdwname) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a window.
|
// Create a window.
|
||||||
unsigned int window_create(char* name, const int x, const int y, const int w, const int h) {
|
unsigned int window_create(char* name, const int x, const int y,
|
||||||
|
const int w, const int h) {
|
||||||
if(nwindows >= mwindows) {
|
if(nwindows >= mwindows) {
|
||||||
// We have reached our memory limit.
|
// We have reached our memory limit.
|
||||||
windows = realloc(windows, sizeof(Window)*(++mwindows));
|
windows = realloc(windows, sizeof(Window)*(++mwindows));
|
||||||
@ -783,7 +788,8 @@ static void toolkit_renderList(Widget* lst, double bx, double by) {
|
|||||||
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 selected.
|
// Draw selected.
|
||||||
toolkit_drawRect(x, y-1.+lst->h-(1+lst->dat.lst.selected-lst->dat.lst.pos)*(gl_defFont.h+2.),
|
toolkit_drawRect(x,
|
||||||
|
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);
|
lst->w, gl_defFont.h+2., &cHilight, NULL);
|
||||||
|
|
||||||
// Draw content.
|
// Draw content.
|
||||||
@ -903,36 +909,40 @@ static void toolkit_mouseEvent(SDL_Event* event) {
|
|||||||
|
|
||||||
for(i = 0; i < w->nwidgets; i++) {
|
for(i = 0; i < w->nwidgets; i++) {
|
||||||
wgt = &w->widgets[i];
|
wgt = &w->widgets[i];
|
||||||
if((x > wgt->x) && (x < (wgt->x + wgt->w)) && (y > wgt->y) && (y < (wgt->y + wgt->h))) {
|
if((x > wgt->x) && (x < (wgt->x + wgt->w)) &&
|
||||||
switch(event->type) {
|
(y > wgt->y) && (y < (wgt->y + wgt->h))) {
|
||||||
case SDL_MOUSEMOTION:
|
if((wgt->type == WIDGET_CUST) && wgt->dat.cst.mouse)
|
||||||
wgt->status = WIDGET_STATUS_MOUSEOVER;
|
(*wgt->dat.cst.mouse)(event->type, x, y);
|
||||||
break;
|
else
|
||||||
case SDL_MOUSEBUTTONDOWN:
|
switch(event->type) {
|
||||||
wgt->status = WIDGET_STATUS_MOUSEDOWN;
|
case SDL_MOUSEMOTION:
|
||||||
|
wgt->status = WIDGET_STATUS_MOUSEOVER;
|
||||||
|
break;
|
||||||
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
|
wgt->status = WIDGET_STATUS_MOUSEDOWN;
|
||||||
|
|
||||||
if(toolkit_isFocusable(wgt))
|
if(toolkit_isFocusable(wgt))
|
||||||
w->focus = i;
|
w->focus = i;
|
||||||
|
|
||||||
if(wgt->type == WIDGET_LIST)
|
if(wgt->type == WIDGET_LIST)
|
||||||
toolkit_listFocus(wgt, x-wgt->x, y-wgt->y);
|
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) {
|
||||||
if(wgt->type == WIDGET_BUTTON) {
|
if(wgt->type == WIDGET_BUTTON) {
|
||||||
if(wgt->dat.btn.fptr == NULL)
|
if(wgt->dat.btn.fptr == NULL)
|
||||||
DEBUG("Toolkit: Button '%s' of Window '%s'"
|
DEBUG("Toolkit: Button '%s' of Window '%s'"
|
||||||
"Does not have a function trigger",
|
"Does not have a function trigger",
|
||||||
wgt->name, w->name);
|
wgt->name, w->name);
|
||||||
else (*wgt->dat.btn.fptr)(wgt->name);
|
else (*wgt->dat.btn.fptr)(wgt->name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wgt->status = WIDGET_STATUS_NORMAL;
|
wgt->status = WIDGET_STATUS_NORMAL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
wgt->status = WIDGET_STATUS_NORMAL;
|
wgt->status = WIDGET_STATUS_NORMAL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,8 @@
|
|||||||
extern int toolkit;
|
extern int toolkit;
|
||||||
|
|
||||||
// Creation.
|
// Creation.
|
||||||
unsigned int window_create(char* name, const int x, const int y, const int w, const int h);
|
unsigned int window_create(char* name, const int x, const int y,
|
||||||
|
const int w, const int h);
|
||||||
|
|
||||||
void window_addButton(const unsigned int wid, const int x, const int y,
|
void window_addButton(const unsigned int wid, const int x, const int y,
|
||||||
const int w, const int h, char* name, char* display,
|
const int w, const int h, char* name, char* display,
|
||||||
@ -34,7 +35,8 @@ void window_addCust(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, const int border,
|
char* name, const int border,
|
||||||
void(*render) (double x, double y, double w, double h));
|
void(*render) (double x, double y, double w, double h),
|
||||||
|
void(*mouse) (Uint8 type, double x, double y));
|
||||||
|
|
||||||
// Popups and alerts.
|
// Popups and alerts.
|
||||||
void toolkit_alert(const char* fmt, ...);
|
void toolkit_alert(const char* fmt, ...);
|
||||||
|
Loading…
Reference in New Issue
Block a user