I am trying to build a screen manager now. It will detect which object the user clicked with the mouse and work out which function to call. I've returned to C_ntua's earlier post (page 1, no.14) on this. Just wondering whether I'm getting the ideas right.
Here's what I have so far:
Included in the struct for each GUI object is another struct called "objectScreenData". The purpose is to serve as a set of data for use by the screen manager - to check which object has been clicked on and know where to go to call the relevant function.
Here's the struct
Code:
typedef struct objectScreenData {
ScreenBounds objectBounds;
ScreenBounds resizeBounds;
ScreenBounds textBounds;
int type;
int tag;
char name[20];
void * address; //stores address of struct - key issue. Intend to put a cast on it with
// relevant struct when function is called.
}ObjectScreenData;
Each time an object is drawn or redrawn, it sends a pointer to this information to the ScreenManager struct. Here's the screen manager:
Code:
typedef struct screenManager {
ObjectScreenData * objects[20];
int nObjects;
int selectedObject;
//Function pointers
void (*init)(struct screenManager *sm);
void (*destroy)(struct screenManager **sm);
int (*add)(struct screenManager *sm, ObjectScreenData *obj);
void (*checkWhichObject)(struct screenManager *sm, int x, int y);
int (*checkWhichFunction)(struct screenManager *sm, int x, int y);
void (*callFunction)(struct screenManager *sm, int tag);
}ScreenManager;
This is the add function for the screen manager:
Code:
int screenManagerAdd(ScreenManager *sm, ObjectScreenData *obj) {
if (sm->nObjects < 19) {
if ((obj->tag) < 0)
{
printf("\n\n+++ Adding %s to screen manager database", obj->name);
sm->objects[sm->nObjects] = obj;
sm->nObjects += 1;
obj->tag = sm->nObjects;
printf("\nObject added, tag = %d", obj->tag);
return sm->nObjects;
}
else{
printf("\nObject >>%s, ID = %d already registered", obj->name, obj->tag);
return obj->tag;
}
}
else{
printf("\nObject: %s could not be added to the Screen Manager", obj->name);
return -1;
}
}
And this is the checkWhichObject function. Still work n progress - it's a bit of a building site at the moment, but just to give an idea of where it is (or isn't?) going.
Code:
void screenManagerCheckWhichObject(ScreenManager *sm, int x, int y) {
for (int i = 0; i < sm->nObjects; i++) {
//get the objects screen boundaries
int minX = (sm->objects[i])->objectBounds.minX;
int minY = (sm->objects[i])->objectBounds.minY;
int maxX = (sm->objects[i])->objectBounds.maxX;
int maxY = (sm->objects[i])->objectBounds.maxY;
if ( x> minX && x < maxX && y > minY && y < maxY) {
sm->selectedObject = i;
printf("\nObject ID = %d, name = %s", i, (sm->objects[i])->name);
sm->checkWhichFunction(sm, x, y);
}
}
}
Comments are very welcome. The general question is - am I on the right lines, are there any glaring errors in the way I'm doing this?