Skip to content

Commit

Permalink
cleanup and add comments for Screen
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Nov 3, 2019
1 parent b36e7cc commit 0140608
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 184 deletions.
9 changes: 8 additions & 1 deletion src/Event.c
Expand Up @@ -10,7 +10,7 @@ struct _BlockEventsList BlockEvents;
struct _WorldEventsList WorldEvents;
struct _ChatEventsList ChatEvents;
struct _WindowEventsList WindowEvents;
struct _KeyEventsList InputEvents;
struct _InputEventsList InputEvents;
struct _PointerEventsList PointerEvents;
struct _NetEventsList NetEvents;

Expand Down Expand Up @@ -105,3 +105,10 @@ void Event_RaiseInput(struct Event_Input* handlers, int key, cc_bool repeating)
handlers->Handlers[i](handlers->Objs[i], key, repeating);
}
}

void Event_RaiseString(struct Event_String* handlers, const String* str) {
int i;
for (i = 0; i < handlers->Count; i++) {
handlers->Handlers[i](handlers->Objs[i], str);
}
}
14 changes: 13 additions & 1 deletion src/Event.h
Expand Up @@ -58,6 +58,12 @@ struct Event_Input {
void* Objs[EVENT_MAX_CALLBACKS]; int Count;
};

typedef void (*Event_String_Callback)(void* obj, const String* str);
struct Event_String {
Event_String_Callback Handlers[EVENT_MAX_CALLBACKS];
void* Objs[EVENT_MAX_CALLBACKS]; int Count;
};

/* Registers a callback function for the given event. */
/* NOTE: Trying to register a callback twice or over EVENT_MAX_CALLBACKS callbacks will terminate the game. */
CC_API void Event_Register(struct Event_Void* handlers, void* obj, Event_Void_Callback handler);
Expand Down Expand Up @@ -113,6 +119,11 @@ void Event_RaiseInput(struct Event_Input* handlers, int key, cc_bool repeating);
#define Event_RegisterInput(handlers, obj, handler) Event_RegisterMacro(handlers, obj, handler)
#define Event_UnregisterInput(handlers, obj, handler) Event_UnregisterMacro(handlers, obj, handler)

/* Calls all registered callbacks for an event which has a string argument. */
void Event_RaiseString(struct Event_String* handlers, const String* str);
#define Event_RegisterString(handlers, obj, handler) Event_RegisterMacro(handlers, obj, handler)
#define Event_UnregisterString(handlers, obj, handler) Event_UnregisterMacro(handlers, obj, handler)

CC_VAR extern struct _EntityEventsList {
struct Event_Int Added; /* Entity is spawned in the current world */
struct Event_Int Removed; /* Entity is despawned from the current world */
Expand Down Expand Up @@ -172,11 +183,12 @@ CC_VAR extern struct _WindowEventsList {
struct Event_Void Created; /* Window has been created, Window_Handle is valid now. */
} WindowEvents;

CC_VAR extern struct _KeyEventsList {
CC_VAR extern struct _InputEventsList {
struct Event_Int Press; /* Key input character is typed. Arg is a character */
struct Event_Input Down; /* Key or button is pressed. Arg is a member of Key enumeration */
struct Event_Int Up; /* Key or button is released. Arg is a member of Key enumeration */
struct Event_Float Wheel; /* Mouse wheel is moved/scrolled (Arg is wheel delta) */
struct Event_String TextChanged; /* HTML text input changed */
} InputEvents;

CC_VAR extern struct _PointerEventsList {
Expand Down
2 changes: 1 addition & 1 deletion src/Game.c
Expand Up @@ -278,7 +278,7 @@ static void Game_OnResize(void* obj) {
Game_UpdateDimensions();
Gfx_OnWindowResize();
Game_UpdateProjection();
Gui_OnResize();
Gui_Layout();
}

static void Game_OnNewMapCore(void* obj) {
Expand Down
6 changes: 3 additions & 3 deletions src/Gui.c
Expand Up @@ -28,7 +28,7 @@ void Widget_SetLocation(void* widget, cc_uint8 horAnchor, cc_uint8 verAnchor, in
w->horAnchor = horAnchor; w->verAnchor = verAnchor;
w->xOffset = Display_ScaleX(xOffset);
w->yOffset = Display_ScaleY(yOffset);
Widget_Reposition(w);
Widget_Layout(w);
}

void Widget_CalcPosition(void* widget) {
Expand Down Expand Up @@ -293,13 +293,13 @@ void Gui_RenderGui(double delta) {
Gfx_Mode3D();
}

void Gui_OnResize(void) {
void Gui_Layout(void) {
struct Screen* s;
int i;

for (i = 0; i < Gui_ScreensCount; i++) {
s = Gui_Screens[i];
s->VTABLE->OnResize(s);
s->VTABLE->Layout(s);
}
}

Expand Down
47 changes: 30 additions & 17 deletions src/Gui.h
Expand Up @@ -36,28 +36,41 @@ extern cc_bool Gui_TabAutocomplete;
extern cc_bool Gui_ShowFPS;

struct ScreenVTABLE {
/* Initialises persistent state. */
void (*Init)(void* elem);
/* Draws this screen and its widgets on screen. */
void (*Render)(void* elem, double delta);
/* Frees/releases persistent state. */
void (*Free)(void* elem);
cc_bool (*HandlesKeyDown)(void* elem, Key key);
cc_bool (*HandlesKeyUp)(void* elem, Key key);
cc_bool (*HandlesKeyPress)(void* elem, char keyChar);
cc_bool (*HandlesPointerDown)(void* elem, int id, int x, int y);
cc_bool (*HandlesPointerUp)(void* elem, int id, int x, int y);
cc_bool (*HandlesPointerMove)(void* elem, int id, int x, int y);
cc_bool (*HandlesMouseScroll)(void* elem, float delta);
void (*OnResize)(void* elem);
Event_Void_Callback ContextLost;
Event_Void_Callback ContextRecreated;
/* Returns non-zero if an input press is handled. */
int (*HandlesKeyDown)(void* elem, Key key);
/* Returns non-zero if an input release is handled. */
int (*HandlesKeyUp)(void* elem, Key key);
/* Returns non-zero if a key character press is handled. */
int (*HandlesKeyPress)(void* elem, char keyChar);
/* Returns non-zero if a pointer press is handled. */
int (*HandlesPointerDown)(void* elem, int id, int x, int y);
/* Returns non-zero if a pointer release is handled. */
int (*HandlesPointerUp)(void* elem, int id, int x, int y);
/* Returns non-zero if a pointer movement is handled. */
int (*HandlesPointerMove)(void* elem, int id, int x, int y);
/* Returns non-zero if a mouse wheel scroll is handled. */
int (*HandlesMouseScroll)(void* elem, float delta);
/* Positions widgets on screen. Typically called on window resize. */
void (*Layout)(void* elem);
/* Destroys graphics resources. (textures, vertex buffers, etc) */
void (*ContextLost)(void* elem);
/* Allocates graphics resources. (textures, vertex buffers, etc) */
void (*ContextRecreated)(void* elem);
};
#define Screen_Layout const struct ScreenVTABLE* VTABLE; \
#define Screen_Body const struct ScreenVTABLE* VTABLE; \
cc_bool grabsInput; /* Whether this screen grabs input. Causes the cursor to become visible. */ \
cc_bool blocksWorld; /* Whether this screen completely and opaquely covers the game world behind it. */ \
cc_bool closable; /* Whether this screen is automatically closed when pressing Escape */ \
struct Widget** widgets; int numWidgets;

/* Represents a container of widgets and other 2D elements. May cover entire window. */
struct Screen { Screen_Layout };
struct Screen { Screen_Body };

typedef void (*Widget_LeftClick)(void* screen, void* widget);
struct WidgetVTABLE {
Expand All @@ -71,7 +84,7 @@ struct WidgetVTABLE {
cc_bool (*HandlesPointerUp)(void* elem, int id, int x, int y);
cc_bool (*HandlesPointerMove)(void* elem, int id, int x, int y);
};
#define Widget_Layout const struct WidgetVTABLE* VTABLE; \
#define Widget_Body const struct WidgetVTABLE* VTABLE; \
int x, y, width, height; /* Top left corner, and dimensions, of this widget */ \
cc_bool active; /* Whether this widget is currently being moused over */ \
cc_bool disabled; /* Whether widget is prevented from being interacted with */ \
Expand All @@ -80,7 +93,7 @@ struct WidgetVTABLE {
Widget_LeftClick MenuClick;

/* Represents an individual 2D gui component. */
struct Widget { Widget_Layout };
struct Widget { Widget_Body };
void Widget_SetLocation(void* widget, cc_uint8 horAnchor, cc_uint8 verAnchor, int xOffset, int yOffset);
void Widget_CalcPosition(void* widget);
/* Resets Widget struct fields to 0/NULL (except VTABLE) */
Expand Down Expand Up @@ -148,7 +161,7 @@ void Gui_RefreshChat(void);
void Gui_Refresh(struct Screen* s);

void Gui_RenderGui(double delta);
void Gui_OnResize(void);
void Gui_Layout(void);

#define TEXTATLAS_MAX_WIDTHS 16
struct TextAtlas {
Expand All @@ -174,6 +187,6 @@ void TextAtlas_AddInt(struct TextAtlas* atlas, int value, VertexP3fT2fC4b** vert
#define Elem_HandlesPointerUp(elem, id, x, y) (elem)->VTABLE->HandlesPointerUp(elem, id, x, y)
#define Elem_HandlesPointerMove(elem, id, x, y) (elem)->VTABLE->HandlesPointerMove(elem, id, x, y)

#define Widget_Reposition(widget) (widget)->VTABLE->Reposition(widget);
#define Elem_TryFree(elem) if ((elem)->VTABLE) { Elem_Free(elem); }
#define Widget_Layout(widget) (widget)->VTABLE->Reposition(widget);
#define Elem_TryFree(elem) if ((elem)->VTABLE) { Elem_Free(elem); }
#endif

0 comments on commit 0140608

Please sign in to comment.