Skip to content
Permalink
Browse files

cleanup and add comments for Widget

  • Loading branch information...
UnknownShadow200 committed Nov 4, 2019
1 parent 0140608 commit 9b49b67aa62b1f5af7536723fc2d7ad78269cc50
Showing with 50 additions and 38 deletions.
  1. +3 −3 src/Gui.c
  2. +25 −13 src/Gui.h
  3. +22 −22 src/Widgets.c
@@ -49,7 +49,7 @@ void Widget_Reset(void* widget) {
w->MenuClick = NULL;
}

cc_bool Widget_Contains(void* widget, int x, int y) {
int Widget_Contains(void* widget, int x, int y) {
struct Widget* w = (struct Widget*)widget;
return Gui_Contains(w->x, w->y, w->width, w->height, x, y);
}
@@ -67,11 +67,11 @@ int Gui_CalcPos(cc_uint8 anchor, int offset, int size, int axisLen) {
return (axisLen - size) / 2 + offset;
}

cc_bool Gui_Contains(int recX, int recY, int width, int height, int x, int y) {
int Gui_Contains(int recX, int recY, int width, int height, int x, int y) {
return x >= recX && y >= recY && x < (recX + width) && y < (recY + height);
}

cc_bool Gui_ContainsPointers(int x, int y, int width, int height) {
int Gui_ContainsPointers(int x, int y, int width, int height) {
int i, px, py;
for (i = 0; i < Pointers_Count; i++) {
px = Pointers[i].x; py = Pointers[i].y;
@@ -35,6 +35,7 @@ extern cc_bool Gui_TabAutocomplete;
/* Whether FPS counter (and other info) is shown in top left. */
extern cc_bool Gui_ShowFPS;

/* Functions for a Screen instance. */
struct ScreenVTABLE {
/* Initialises persistent state. */
void (*Init)(void* elem);
@@ -74,15 +75,24 @@ struct Screen { Screen_Body };

typedef void (*Widget_LeftClick)(void* screen, void* widget);
struct WidgetVTABLE {
/* Draws this particular widget on-screen. */
void (*Render)(void* elem, double delta);
/* Destroys allocated graphics resources. */
void (*Free)(void* elem);
/* Positions this widget on-screen. */
void (*Reposition)(void* elem);
cc_bool (*HandlesKeyDown)(void* elem, Key key);
cc_bool (*HandlesKeyUp)(void* elem, Key key);
cc_bool (*HandlesMouseScroll)(void* elem, float delta);
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);
/* 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 mouse wheel scroll is handled. */
int (*HandlesMouseScroll)(void* elem, float delta);
/* 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);
};
#define Widget_Body const struct WidgetVTABLE* VTABLE; \
int x, y, width, height; /* Top left corner, and dimensions, of this widget */ \
@@ -95,11 +105,13 @@ struct WidgetVTABLE {
/* Represents an individual 2D gui component. */
struct Widget { Widget_Body };
void Widget_SetLocation(void* widget, cc_uint8 horAnchor, cc_uint8 verAnchor, int xOffset, int yOffset);
/* Calculates where this widget should be on-screen based on its attributes. */
/* These attributes are width/height, horAnchor/verAnchor, xOffset/yOffset */
void Widget_CalcPosition(void* widget);
/* Resets Widget struct fields to 0/NULL (except VTABLE) */
void Widget_Reset(void* widget);
/* Whether the given point is located within the bounds of the widget. */
cc_bool Widget_Contains(void* widget, int x, int y);
/* Returns non-zero if the given point is located within the bounds of the widget. */
int Widget_Contains(void* widget, int x, int y);


extern GfxResourceID Gui_GuiTex, Gui_GuiClassicTex, Gui_IconsTex;
@@ -131,11 +143,11 @@ extern int Gui_ScreensCount;

/* Calculates position of an element on a particular axis */
/* For example, to calculate X position of a text widget on screen */
int Gui_CalcPos(cc_uint8 anchor, int offset, int size, int axisLen);
/* Returns whether the given rectangle contains the given point. */
cc_bool Gui_Contains(int recX, int recY, int width, int height, int x, int y);
/* Returns whether one or more pointers lie within the given rectangle. */
cc_bool Gui_ContainsPointers(int x, int y, int width, int height);
int Gui_CalcPos(cc_uint8 anchor, int offset, int size, int axisLen);
/* Returns non-zero if the given rectangle contains the given point. */
int Gui_Contains(int recX, int recY, int width, int height, int x, int y);
/* Returns non-zero if one or more pointers lie within the given rectangle. */
int Gui_ContainsPointers(int x, int y, int width, int height);
/* Shows HUD and Status screens. */
void Gui_ShowDefault(void);

@@ -20,10 +20,10 @@

#define Widget_UV(u1,v1, u2,v2) Tex_UV(u1/256.0f,v1/256.0f, u2/256.0f,v2/256.0f)
static void Widget_NullFunc(void* widget) { }
static cc_bool Widget_Pointer(void* elem, int id, int x, int y) { return false; }
static cc_bool Widget_Key(void* elem, Key key) { return false; }
static cc_bool Widget_PointerMove(void* elem, int id, int x, int y) { return false; }
static cc_bool Widget_MouseScroll(void* elem, float delta) { return false; }
static int Widget_Pointer(void* elem, int id, int x, int y) { return false; }
static int Widget_Key(void* elem, Key key) { return false; }
static int Widget_PointerMove(void* elem, int id, int x, int y) { return false; }
static int Widget_MouseScroll(void* elem, float delta) { return false; }

/*########################################################################################################################*
*-------------------------------------------------------TextWidget--------------------------------------------------------*
@@ -235,7 +235,7 @@ static void ScrollbarWidget_Render(void* widget, double delta) {
Gfx_Draw2DFlat(x, y - 1 + 4, width, SCROLL_BORDER, Scroll_BackCol);
}

static cc_bool ScrollbarWidget_PointerDown(void* widget, int id, int x, int y) {
static int ScrollbarWidget_PointerDown(void* widget, int id, int x, int y) {
struct ScrollbarWidget* w = (struct ScrollbarWidget*)widget;
int posY, height;

@@ -259,7 +259,7 @@ static cc_bool ScrollbarWidget_PointerDown(void* widget, int id, int x, int y) {
return true;
}

static cc_bool ScrollbarWidget_PointerUp(void* widget, int id, int x, int y) {
static int ScrollbarWidget_PointerUp(void* widget, int id, int x, int y) {
struct ScrollbarWidget* w = (struct ScrollbarWidget*)widget;
if (w->draggingId != id) return true;

@@ -268,7 +268,7 @@ static cc_bool ScrollbarWidget_PointerUp(void* widget, int id, int x, int y) {
return true;
}

static cc_bool ScrollbarWidget_MouseScroll(void* widget, float delta) {
static int ScrollbarWidget_MouseScroll(void* widget, float delta) {
struct ScrollbarWidget* w = (struct ScrollbarWidget*)widget;
int steps = Utils_AccumulateWheelDelta(&w->scrollingAcc, delta);

@@ -277,7 +277,7 @@ static cc_bool ScrollbarWidget_MouseScroll(void* widget, float delta) {
return true;
}

static cc_bool ScrollbarWidget_PointerMove(void* widget, int id, int x, int y) {
static int ScrollbarWidget_PointerMove(void* widget, int id, int x, int y) {
struct ScrollbarWidget* w = (struct ScrollbarWidget*)widget;
float scale;

@@ -398,7 +398,7 @@ static void HotbarWidget_Render(void* widget, double delta) {
HotbarWidget_RenderHotbarBlocks(w);
}

static cc_bool HotbarWidget_KeyDown(void* widget, Key key) {
static int HotbarWidget_KeyDown(void* widget, Key key) {
struct HotbarWidget* w = (struct HotbarWidget*)widget;
int index;
if (key < '1' || key > '9') return false;
@@ -414,7 +414,7 @@ static cc_bool HotbarWidget_KeyDown(void* widget, Key key) {
return true;
}

static cc_bool HotbarWidget_KeyUp(void* widget, Key key) {
static int HotbarWidget_KeyUp(void* widget, Key key) {
struct HotbarWidget* w = (struct HotbarWidget*)widget;
int index;

@@ -434,7 +434,7 @@ static cc_bool HotbarWidget_KeyUp(void* widget, Key key) {
return true;
}

static cc_bool HotbarWidget_PointerDown(void* widget, int id, int x, int y) {
static int HotbarWidget_PointerDown(void* widget, int id, int x, int y) {
struct HotbarWidget* w = (struct HotbarWidget*)widget;
int width, height;
int i, cellX, cellY;
@@ -456,7 +456,7 @@ static cc_bool HotbarWidget_PointerDown(void* widget, int id, int x, int y) {
return false;
}

static cc_bool HotbarWidget_MouseScroll(void* widget, float delta) {
static int HotbarWidget_MouseScroll(void* widget, float delta) {
struct HotbarWidget* w = (struct HotbarWidget*)widget;
int index;

@@ -699,7 +699,7 @@ static void TableWidget_ScrollRelative(struct TableWidget* w, int delta) {
TableWidget_MoveCursorToSelected(w);
}

static cc_bool TableWidget_PointerDown(void* widget, int id, int x, int y) {
static int TableWidget_PointerDown(void* widget, int id, int x, int y) {
struct TableWidget* w = (struct TableWidget*)widget;
w->pendingClose = false;

@@ -715,12 +715,12 @@ static cc_bool TableWidget_PointerDown(void* widget, int id, int x, int y) {
return false;
}

static cc_bool TableWidget_PointerUp(void* widget, int id, int x, int y) {
static int TableWidget_PointerUp(void* widget, int id, int x, int y) {
struct TableWidget* w = (struct TableWidget*)widget;
return Elem_HandlesPointerUp(&w->scroll, id, x, y);
}

static cc_bool TableWidget_MouseScroll(void* widget, float delta) {
static int TableWidget_MouseScroll(void* widget, float delta) {
struct TableWidget* w = (struct TableWidget*)widget;
int origTopRow, index;

@@ -741,7 +741,7 @@ static cc_bool TableWidget_MouseScroll(void* widget, float delta) {
return true;
}

static cc_bool TableWidget_PointerMove(void* widget, int id, int x, int y) {
static int TableWidget_PointerMove(void* widget, int id, int x, int y) {
struct TableWidget* w = (struct TableWidget*)widget;
int cellSize, maxHeight;
int i, cellX, cellY;
@@ -768,7 +768,7 @@ static cc_bool TableWidget_PointerMove(void* widget, int id, int x, int y) {
return true;
}

static cc_bool TableWidget_KeyDown(void* widget, Key key) {
static int TableWidget_KeyDown(void* widget, Key key) {
struct TableWidget* w = (struct TableWidget*)widget;
if (w->selectedIndex == -1) return false;

@@ -1150,7 +1150,7 @@ static void InputWidget_Reposition(void* widget) {
w->inputTex.X += w->x - oldX; w->inputTex.Y += w->y - oldY;
}

static cc_bool InputWidget_KeyDown(void* widget, Key key) {
static int InputWidget_KeyDown(void* widget, Key key) {
struct InputWidget* w = (struct InputWidget*)widget;
if (key == KEY_LEFT) {
InputWidget_LeftKey(w);
@@ -1170,9 +1170,9 @@ static cc_bool InputWidget_KeyDown(void* widget, Key key) {
return true;
}

static cc_bool InputWidget_KeyUp(void* widget, Key key) { return true; }
static int InputWidget_KeyUp(void* widget, Key key) { return true; }

static cc_bool InputWidget_PointerDown(void* widget, int id, int x, int y) {
static int InputWidget_PointerDown(void* widget, int id, int x, int y) {
String line; char lineBuffer[STRING_SIZE];
struct InputWidget* w = (struct InputWidget*)widget;
struct DrawTextArgs args;
@@ -1666,7 +1666,7 @@ static void ChatInputWidget_TabKey(struct InputWidget* w) {
}
}

static cc_bool ChatInputWidget_KeyDown(void* widget, Key key) {
static int ChatInputWidget_KeyDown(void* widget, Key key) {
struct InputWidget* w = (struct InputWidget*)widget;
if (key == KEY_TAB) { ChatInputWidget_TabKey(w); return true; }
if (key == KEY_UP) { ChatInputWidget_UpKey(w); return true; }
@@ -2658,7 +2658,7 @@ static void SpecialInputWidget_Reposition(void* widget) {
w->tex.X = w->x; w->tex.Y = w->y;
}

static cc_bool SpecialInputWidget_PointerDown(void* widget, int id, int x, int y) {
static int SpecialInputWidget_PointerDown(void* widget, int id, int x, int y) {
struct SpecialInputWidget* w = (struct SpecialInputWidget*)widget;
x -= w->x; y -= w->y;

0 comments on commit 9b49b67

Please sign in to comment.
You can’t perform that action at this time.