From 81ae76236ce3b2002366eb52f6224580a3131f97 Mon Sep 17 00:00:00 2001 From: Allanis <allanis@saracraft.net> Date: Fri, 23 May 2014 23:05:30 +0100 Subject: [PATCH] [Add] I think I've been documenting this thing all day. D: --- src/lephisto.c | 34 +-- src/lluadef.h | 6 +- src/toolkit.c | 711 +++++++++++++++++++++++++++++++++++++++---------- src/toolkit.h | 10 +- src/unidiff.c | 34 +-- 5 files changed, 584 insertions(+), 211 deletions(-) diff --git a/src/lephisto.c b/src/lephisto.c index fc1df01..42e4a89 100644 --- a/src/lephisto.c +++ b/src/lephisto.c @@ -13,12 +13,12 @@ /* Global. */ #include <string.h> -#if defined(LINUX) && !defined(NODEBUG) +#if defined(LINUX) && !defined(DEBUGGING) #include <signal.h> #include <execinfo.h> #include <stdlib.h> #include <unistd.h> -#endif /* defined(LINUX) && !defined(NODEBUG) */ +#endif /* defined(LINUX) && !defined(DEBUGGING) */ /* Local. */ #include "lephisto.h" @@ -96,8 +96,6 @@ void main_loop(void); /* dialogue.c */ /** - * @fn int main(int argc, char** argv) - * * @brief The entry point of Lephisto. * * @param[in] argc Number of arguments. @@ -278,8 +276,6 @@ int main(int argc, char** argv) { } /** - * @fn void loadscreen_load(void) - * * @brief Display a loading screen. */ void loadscreen_load(void) { @@ -309,8 +305,6 @@ void loadscreen_load(void) { } /** - * @fn void loadscreen_render(double done, const char* msg) - * * @brief Render the load screen with message. * @param done Amount done (1. == completed). * @param msg Loading screen message. @@ -356,8 +350,6 @@ void loadscreen_render(double done, const char* msg) { } /** - * @fn static void loadscreen_unload(void) - * * @brief Frees the loading screen. */ static void loadscreen_unload(void) { @@ -367,8 +359,6 @@ static void loadscreen_unload(void) { } /** - * @fn void load_all(void) - * * @brief Load all the data, makes main() simpler. */ #define LOADING_STAGES 9. /**< Amount of loading stages. */ @@ -395,8 +385,6 @@ void load_all(void) { } /** - * @fn void unload_all(void) - * * @brief Unloads all data, simplifies main(). */ void unload_all(void) { @@ -462,8 +450,6 @@ static void fps_control(void) { static const double fps_min = 1./50.0; /** - * @fn static void update_all(void) - * * @brief Updates the game itself (player flying around etc). */ static void update_all(void) { @@ -492,8 +478,6 @@ static void update_all(void) { } /** - * @fn static void update_routine(double dt) - * * @brief Actually runs the update. * @param[in] dt Current delta tick. */ @@ -506,8 +490,6 @@ static void update_routine(double dt) { } /** - * @fn static void render_all(void) - * * @brief Renders the game itself (player flying around etc). * * Blitting order. (layers) @@ -579,8 +561,6 @@ static void display_fps(const double dt) { } /** - * @fn static void window_caption(void) - * * @brief Set the window caption. */ static void window_caption(void) { @@ -593,8 +573,6 @@ static void window_caption(void) { static char human_version[50]; /**< Stores human readable version string. */ /** - * @fn char* lephisto_version(void) - * * @brief Return the version in a human readable string. * * @return The human readable version string. @@ -607,8 +585,6 @@ char* lephisto_version(void) { } /** - * @fn static void print print_SDLversion. - * * @bief Print the SDL version to console. */ static void print_SDLversion(void) { @@ -628,7 +604,7 @@ static void print_SDLversion(void) { WARN("SDL is older than compiled version."); } -#if defined(LINUX) && !defined(NODEBUG) +#if defined(LINUX) && !defined(DEBUGGING) /** * @brief Get the string related to the signal code. * @param sig Signal to which code belongs. @@ -695,7 +671,7 @@ static void debug_sigHandler(int sig, siginfo_t* info, void* unused) { * @brief Set up the SignalHandler for Linux. */ static void debug_sigInit(void) { -#if defined(LINUX) && !defined(NODEBUG) +#if defined(LINUX) && !defined(DEBUGGING) struct sigaction sa, so; /* Set up handler. */ @@ -714,6 +690,6 @@ static void debug_sigInit(void) { sigaction(SIGABRT, &sa, &so); if(so.sa_handler == SIG_IGN) DEBUG("Unable to get set up SIGABRT signal handler."); -#endif /* #if defined(LINUX) && !defined(NODEBUG) */ +#endif /* #if defined(LINUX) && !defined(DEBUGGING) */ } diff --git a/src/lluadef.h b/src/lluadef.h index 22426cb..59e8584 100644 --- a/src/lluadef.h +++ b/src/lluadef.h @@ -5,7 +5,7 @@ #include "lualib.h" /* Debug stuff. */ -#ifdef NODEBUG +#ifdef DEBUGGING #ifdef DEBUG_PARANOID #define LLUA_DEBUG(str, args...) \ (fprintf(stdout, "Lua: "str"\n, ## args), abort()) @@ -24,11 +24,11 @@ luaL_error(L, "Too few arguments for %s.", __func__); \ return 0; \ } -#else /* NODEBUG. */ +#else /* DEBUGGING. */ #define LLUA_DEBUG(str, args...) do {;} while(0) #define LLUA_MIN_ARGS(n) do {;} while(0) #define LLUA_INVALID_PARAMETER() do {;} while(0) -#endif /* NODEBUG. */ +#endif /* DEBUGGING. */ /* Comfortability macros. */ #define luaL_dobuffer(L, b, n, s) \ diff --git a/src/toolkit.c b/src/toolkit.c index bf70bd8..0d2178b 100644 --- a/src/toolkit.c +++ b/src/toolkit.c @@ -26,7 +26,6 @@ typedef enum WidgetType_ { WIDGET_CUST, WIDGET_INPUT, WIDGET_IMAGEARRAY, - WIDGET_SCROLLBAR, WIDGET_FADER } WidgetType; @@ -38,11 +37,15 @@ typedef enum WidgetStatus_ { } WidgetStatus; typedef struct Widget_ { - char* name; /* Widget name. */ - WidgetType type; /* type.. */ + char* name; /**< Widget name. */ + WidgetType type; /**< Widget type. */ - double x,y; /* Position. */ - double w,h; /* Dimensions. */ + unsigned int wdw; /**< Widgets parent window. */ + + double x; /**< X position within the window. */ + double y; /**< Y position within the window. */ + double w; /**< Widget width. */ + double h; /**< Widget height. */ WidgetStatus status; @@ -103,9 +106,6 @@ typedef struct Widget_ { int ih; /**< Image height to use. */ void(*fptr)(unsigned int, char*); /**< Modify callback - triggered on selection. */ } iar; /**< WIDGET_IMAGEARRAY */ - struct { - double value; /**< Current value. */ - } scb; /**< WIDGET_SCROLLBAR */ struct { double value; /**< Current value. */ @@ -144,6 +144,11 @@ static Window* windows = NULL; static int nwindows = 0; static int mwindows = 0; +/* Simulate key presses when holding. */ +static SDLKey input_key; /**< Current pressed key. */ +static unsigned int input_keyTime; /**< Tick pressed. */ +static int input_keyCounter; /**< Number of repititions. */ + /* Default outline colours. */ static glColour* toolkit_colLight = &cGrey90; static glColour* toolkit_col = &cGrey70; @@ -185,7 +190,6 @@ static void toolkit_renderRect(Widget* rct, double bx, double by); static void toolkit_renderCust(Widget* cst, double bx, double by); static void toolkit_renderInput(Widget* inp, double bx, double by); static void toolkit_renderImageArray(Widget* iar, double bx, double by); -static void toolkit_renderScrollbar(Widget* scb, double bx, double by); static void toolkit_renderFader(Widget* fad, double bx, double by); static void toolkit_drawOutline(double x, double y, double w, double h, double b, glColour* c, glColour* lc); @@ -195,7 +199,13 @@ static void toolkit_unclip(void); static void toolkit_drawRect(double x, double y, double w, double h, glColour* c, glColour* lc); -/* Set the internal widget position. */ +/** + * @brief Set the internal widget position. + * @param wdw Window to which the widget belongs. + * @param wgt Widget to set position of. + * @param x X position to use. + * @param y Y position to use. + */ static void toolkit_setPos(Window* wdw, Widget* wgt, int x, int y) { if(x < 0) wgt->x = wdw->w - wgt->w + x; else wgt->x = (double) x; @@ -203,11 +213,24 @@ static void toolkit_setPos(Window* wdw, Widget* wgt, int x, int y) { else wgt->y = (double) y; } -/* Add a button that when pressed will trigger call, passing it's name as the */ -/* only parameter. */ +/** + * @brief Add a button to a window. + * + * Position origin is 0,0 at bottom left. If you use negative X or Y + * positions. They actually count from the opposite side in. + * @param wid ID of the window to add the widget to. + * @param x X position within the window to use. + * @param y Y position within the window to use. + * @param w Width of the widget. + * @param h Height of the widget. + * @param name Name of the widget to use internally. + * @param display Text displayed on the button (centered). + * @param call Function to call when button is pressed. Parameter passed + * is the name of the button. + */ void window_addButton(const unsigned int wid, const int x, const int y, const int w, const int h, char* name, - char* display, void (*call)(unsigned int, char*)) { + char* display, void (*call)(unsigned int wgt, char* wdwname)) { Window* wdw = window_wget(wid); Widget* wgt = window_newWidget(wdw); @@ -215,6 +238,7 @@ void window_addButton(const unsigned int wid, const int x, const int y, /* Generic. */ wgt->type = WIDGET_BUTTON; wgt->name = strdup(name); + wgt->wdw = wid; /* Specific. */ wgt->dat.btn.display = strdup(display); @@ -232,7 +256,22 @@ void window_addButton(const unsigned int wid, const int x, const int y, toolkit_nextFocus(); } -/* Add text to the window. */ +/** + * @brief Add a text widget to the window. + * + * Position origin is 0,0 at bottom left. If you use negative X or Y + * position, they actually count from the opposite side in. + * @param wid ID of the window to add the widget to. + * @param x X position within the window to use. + * @param y Y position within the window to use. + * @param w Max width of the text. + * @param h Max height of the text. + * @param centered Whether text should be centered. + * @param name Name of the widget to use internally. + * @param font Font to use (NULL is default). + * @param colour Colour to use (NULL is default). + * @param string Text to display. + */ void window_addText(const unsigned int wid, const int x, const int y, const int w, const int h, const int centered, char* name, glFont* font, glColour* colour, char* string) { @@ -243,6 +282,7 @@ void window_addText(const unsigned int wid, const int x, const int y, /* Generic. */ wgt->type = WIDGET_TEXT; wgt->name = strdup(name); + wgt->wdw = wid; /* Specific. */ if(font == NULL) wgt->dat.txt.font = &gl_defFont; @@ -261,7 +301,18 @@ void window_addText(const unsigned int wid, const int x, const int y, toolkit_setPos(wdw, wgt, x, y); } -/* Add a graphic to the window. */ +/** + * @brief Add an image widget to the window. + * + * Position origin is 0,0 at bottom left. If you use negative X or Y + * positions, they actually count from the opposite side in. + * @param wid ID of the window to add the widget to. + * @param x X position within the window to use. + * @param y Y position within the window to use. + * @param name Name of the widget to use internally. + * @param image Image to use. + * @param border Whether to use a border. + */ void window_addImage(const unsigned int wid, const int x, const int y, char* name, glTexture* image, int border) { @@ -271,6 +322,7 @@ void window_addImage(const unsigned int wid, const int x, const int y, /* Generic. */ wgt->type = WIDGET_IMAGE; wgt->name = strdup(name); + wgt->wdw = wid; /* Specific. */ wgt->dat.img.image = image; @@ -283,12 +335,30 @@ void window_addImage(const unsigned int wid, const int x, const int y, toolkit_setPos(wdw, wgt, x, y); } +/** + * @brief Add a list widget to a window. + * + * Position origin is 0,0 at bottom left. If you use negative X or Y + * positions, they actually count from the opposite side in. + * @param wid ID of the window to add the widget to. + * @param x X position within the window to use. + * @param y Y position within the window to use. + * @param w Width of the widget. + * @param h Height of the widget. + * @param name Name of the widget to use internally. + * @param items Items in the list (will be freed automatically). + * @param nitems Number of items in items parameter. + * @param defitem Default item to select. + * @param call Function to call when new item is selected. Parameter passed + * is the name of the list. + */ 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, void(*call) (unsigned int, char*)) { + int defitem, void(*call) (unsigned int wdw, char* wgtname)) { Window *wdw = window_wget(wid); Widget* wgt = window_newWidget(wdw); + wgt->wdw = wid; /* Generic. */ wgt->type = WIDGET_LIST; @@ -317,6 +387,20 @@ void window_addList(const unsigned int wid, const int x, const int y, toolkit_nextFocus(); } +/** + * @brief Add a rectangle widget to a window. + * + * Position origin is 0,0 at bottom left. If you use negative X or Y + * positions, they actually count from the opposite side in. + * @param wid ID of the window to add the widget to. + * @param x X position within the window to use. + * @param y Y position within the window to use. + * @param w Width of the widget. + * @param h Height of the widget. + * @param name Name of the widget to use internally. + * @param colour Colour of the rectangle. + * @param border Whether or not it should have a border. + */ void window_addRect(const unsigned int wid, const int x, const int y, const int w, const int h, char* name, glColour* colour, int border) { @@ -326,6 +410,7 @@ void window_addRect(const unsigned int wid, const int x, const int y, /* Generic. */ wgt->type = WIDGET_RECT; wgt->name = strdup(name); + wgt->wdw = wid; /* Specific. */ wgt->dat.rct.colour = colour; @@ -337,6 +422,26 @@ void window_addRect(const unsigned int wid, const int x, const int y, toolkit_setPos(wdw, wgt, x, y); } +/** + * @brief Add a custom widget to a window. + * + * Position origin is 0,0 at bottom left. If you use negative X or Y + * positions, they actually count from the opposite side in. + * + * You are in charge of the rendering and handling mouse input for this widget. + * Mouse events outside the widget position won't be passed on. + * @param wid ID of the window to add the widget to. + * @param x X position within the window to use. + * @param y Y position within the window to use. + * @param w Width of the widget. + * @param h Height of the widget. + * @param name Name of the widget to use internally. + * @param border Whether or not it should have a border. + * @param render Render function, passes the position and dimensions of the + * widget as parameters. + * @param mouse Mouse function passes the window id, event and position as + * parameters. + */ void window_addCust(const unsigned int wid, const int x, const int y, const int w, const int h, char* name, const int border, void(*render) (double x, double y, double w, double h), @@ -348,6 +453,7 @@ void window_addCust(const unsigned int wid, const int x, const int y, /* Generic. */ wgt->type = WIDGET_CUST; wgt->name = strdup(name); + wgt->wdw = wid; /* Specific. */ wgt->dat.cst.border = border; @@ -360,7 +466,20 @@ void window_addCust(const unsigned int wid, const int x, const int y, toolkit_setPos(wdw, wgt, x, y); } -/* Add an input widget. */ +/** + * @brief Add an input widget to a window. + * + * Position origin is 0,0 at bottom left, if you use negative X or Y + * positions, they actually count from the opposite side in. + * @param wid ID of the window to add the widget to. + * @param x X position within the window to use. + * @param y Y position within the window to use. + * @param w Width of the widget. + * @param h Height of the widget to use. + * @param name Name of the widget to use internally. + * @param max Max amount of characters that can be written. + * @param oneline Whether widget should only be one line long. + */ void window_addInput(const unsigned int wid, const int x, const int y, const int w, const int h, @@ -372,6 +491,7 @@ void window_addInput(const unsigned int wid, /* Generic. */ wgt->type = WIDGET_INPUT; wgt->name = strdup(name); + wgt->wdw = wid; /* Specific. */ wgt->dat.inp.max = max+1; @@ -388,7 +508,10 @@ void window_addInput(const unsigned int wid, } /** - * @brief Add an Image array. + * @brief Add an Image Array widget. + * + * Position origin is 0,0 at bottom left. If you use negative X or Y + * positions, they actually count from the opposite side in. * @param wid Window to add to. * @param x X position. * @param y Y position. @@ -407,7 +530,7 @@ void window_addImageArray(const unsigned int wid, const int w, const int h, /* Size. */ char* name, const int iw, const int ih, /* Name and image sizes. */ glTexture** tex, char** caption, int nelem, /* Elements. */ - void(*call) (unsigned int, char*)) { + void(*call) (unsigned int wdw, char* wgtname)) { Window* wdw = window_wget(wid); Widget* wgt = window_newWidget(wdw); @@ -415,6 +538,7 @@ void window_addImageArray(const unsigned int wid, /* Generic. */ wgt->type = WIDGET_IMAGEARRAY; wgt->name = strdup(name); + wgt->wdw = wid; /* Specific. */ wgt->dat.iar.images = tex; @@ -435,30 +559,24 @@ void window_addImageArray(const unsigned int wid, toolkit_nextFocus(); } -void window_addScrollbar(const unsigned int wid, - const int x, const int y, /* Position. */ - const int w, const int h, /* Size. */ - char* name, double value) { - - Window* wdw = window_wget(wid); - Widget* wgt = window_newWidget(wdw); - - /* Genetic. */ - wgt->type = WIDGET_SCROLLBAR; - wgt->name = strdup(name); - - /* Specific. */ - wgt->dat.scb.value = value; - - /* Position/size. */ - wgt->w = (double)w; - wgt->h = (double)h; - toolkit_setPos(wdw, wgt, x, y); - - if(wdw->focus == -1) /* Initialize the focus. */ - toolkit_nextFocus(); -} - +/** + * @brief Add a fader. + * + * Position origin is 0,0 at bottom left. If you use negative X or Y + * positions, they actually count from the opposite side in. + * + * If width is bigger then height, it's horizontal, otherwise it's vertical. + * @param wid Window to add to. + * @param x X position. + * @param y Y position. + * @param w Width. + * @param h Height. + * @param name Internal widget name. + * @param min Min value. + * @param max Max value. + * @param def Default value. + * @param call Callback when fader is modified. + */ void window_addFader(const unsigned int wid, const int x, const int y, /* Position. */ const int w, const int h, /* Size, if w > h fader is horizontal, else vertical. */ @@ -472,6 +590,7 @@ void window_addFader(const unsigned int wid, /* Generic. */ wgt->type = WIDGET_FADER; wgt->name = strdup(name); + wgt->wdw = wid; /* Specific. */ wgt->dat.fad.value = min; @@ -489,22 +608,33 @@ void window_addFader(const unsigned int wid, toolkit_nextFocus(); } -/* Return pointer to newly allocated widget. */ +/** + * @brief Allocate room fro a new widget. + * @param w Window to create widget in. + * @return Newly allocated widget. + */ static Widget* window_newWidget(Window* w) { Widget* wgt = NULL; + /* Grow widget list. */ w->nwidgets++; w->widgets = realloc(w->widgets, sizeof(Widget)*w->nwidgets); if(w->widgets == NULL) WARN("Out of memory"); + /* Set sane defaults. */ wgt = &w->widgets[w->nwidgets - 1]; + memset(wgt, 0, sizeof(Widget)); wgt->type = WIDGET_NULL; wgt->status = WIDGET_STATUS_NORMAL; return wgt; } -/* Return the window of id wid. */ +/** + * @brief Get a window by ID. + * @param wid ID of the window to get. + * @return Window matching wid. + */ static Window* window_wget(const unsigned int wid) { int i; for(i = 0; i < nwindows; i++) @@ -514,9 +644,22 @@ static Window* window_wget(const unsigned int wid) { return NULL; } +/** + * @brief Get a widget from window id and widgetname. + * @param wid ID of the window to get widget from. + * @param name Name of the widget to get. + * @return Widget matching name in the window. + */ static Widget* window_getwgt(const unsigned int wid, char* name) { int i; - Window* wdw = window_wget(wid); + Window* wdw; + + /* Get the window. */ + wdw = window_wget(wid); + if(wdw == NULL) + return NULL; + + /* Find the widget. */ for(i = 0; i < wdw->nwidgets; i++) if(strcmp(wdw->widgets[i].name, name)==0) return &wdw->widgets[i]; @@ -525,116 +668,304 @@ static Widget* window_getwgt(const unsigned int wid, char* name) { return NULL; } +/** + * @brief Modifies an existing text widget. + * @param wid Window to which the text widget belongs. + * @param name Name of the text widget. + * @param newstring String to set for the text widget. + */ void window_modifyText(const unsigned int wid, char* name, char* newstring) { - Widget* wgt = window_getwgt(wid, name); + Widget* wgt; + /* Get the widget. */ + wgt = window_getwgt(wid, name); + if(wgt == NULL) + return; + + /* Check type. */ + if(wgt->type != WIDGET_TEXT) { + WARN("Not modifying text on non-text widget '%s'.", name); + return; + } + + /* Set text. */ if(wgt->dat.txt.text) free(wgt->dat.txt.text); wgt->dat.txt.text = (newstring) ? strdup(newstring) : NULL; } -/* Get a widgets position. */ +/** + * @brief Get a widgets position. + * @param wid ID of the window to get widget from. + * @param name Name of the widget to get position of. + * @param[out] x X position of the widget. + * @param[out] y Y position of the widget. + */ void window_posWidget(const unsigned int wid, char* name, int* x, int* y) { - Widget* wgt = window_getwgt(wid, name); + Widget* wgt; - (*x) = wgt->x; - (*y) = wgt->y; + /* Get the widget. */ + wgt = window_getwgt(wid, name); + if(wgt == NULL) + return; + + /* Return positions. */ + if(x != NULL) (*x) = wgt->x; + if(y != NULL) (*y) = wgt->y; } -/* Move a widget. */ +/** + * @brief Move a widget. + * @param wid ID of the window to get widget form. + * @param name Name of the widget to set position to. + * @param x New X position to set widget to. + * @param y New Y position to set widget to. + */ void window_moveWidget(const unsigned int wid, char* name, int x, int y) { - Window* wdw = window_wget(wid); - Widget* wgt = window_getwgt(wid, name); + Window* wdw; + Widget* wgt; + /* Get window. */ + wdw = window_wget(wid); + if(wdw == NULL) + return; + + /* Get widget. */ + wgt = window_getwgt(wid, name); + if(wgt == NULL) + return; + + /* set position. */ toolkit_setPos(wdw, wgt, x, y); } -/* Disable a button. */ +/** + * @brief Disable a button. + * @param wid ID of the window to get widget from. + * @param name Name of the button to disable. + */ void window_disableButton(const unsigned int wid, char* name) { - Widget* wgt = window_getwgt(wid, name); + Widget* wgt; + /* Get widget. */ + wgt = window_getwgt(wid, name); + if(wgt == NULL) + return; + + /* Check type. */ if(wgt->type != WIDGET_BUTTON) { DEBUG("Trying to disable a non-button widget '%s'", name); return; } + + /* Disable button. */ wgt->dat.btn.disabled = 1; } -/* Enable a button. */ +/** + * @brief Enable a button. + * @param wid ID of the window to get widget from. + * @param name Name of the button to enable. + */ void window_enableButton(const unsigned int wid, char* name) { - Widget* wgt = window_getwgt(wid, name); + Widget* wgt; + /* Get widget. */ + wgt = window_getwgt(wid, name); + if(wgt == NULL) + return; + + /* Check type. */ if(wgt->type != WIDGET_BUTTON) { DEBUG("Trying to enable a non-button widget '%s'", name); return; } + + /* Enable button. */ wgt->dat.btn.disabled = 0; } +/** + * @brief Modifies an existing images image. + * @param wid ID of the window to get widget from. + * @param name Name of the widget to modify image of. + * @param image New image to set. + */ void window_modifyImage(const unsigned int wid, char* name, glTexture* image) { - Widget* wgt = window_getwgt(wid, name); + Widget* wgt; + + /* Get the widget. */ + wgt = window_getwgt(wid, name); + if(wgt == NULL) + return; + + /* Check the type. */ + if(wgt->type != WIDGET_IMAGE) { + WARN("Not modifying image on non-image widget '%s'.", name); + return; + } + + /* Set the image. */ wgt->dat.img.image = image; } +/** + * @brief Modifies an existing images colour. + * @param wid ID of the window to get widget from. + * @param name Name of the widget to modify image colour of. + * @param colour New colour to use. + */ void window_imgColour(const unsigned int wid, char* name, glColour* colour) { - Widget* wgt = window_getwgt(wid, name); + Widget* wgt; + /* Get the widget. */ + wgt = window_getwgt(wid, name); + if(wgt == NULL) + return; + + /* Check the type. */ + if(wgt->type != WIDGET_IMAGE) { + WARN("Not modifying image on non-image widget '%s'.", name); + return; + } + + /* Set the colour. */ wgt->dat.img.colour = colour; } /** - * Set scrollbar value. + * @brief Set a fader widgets value. + * @param wid ID of the window to get widget from. + * @param name Name of the widget. + * @param value Value to set fader to. */ -void window_scrollbarValue(const unsigned int wid, - char* name, double value) { - Widget* wgt = window_getwgt(wid, name); - - wgt->dat.scb.value = value; -} - -/* Set fader value. */ void window_faderValue(const unsigned int wid, char* name, double value) { - Widget* wgt = window_getwgt(wid, name); + Widget* wgt; + + /* Get the widget. */ + wgt = window_getwgt(wid, name); + if(wgt == NULL) + return; + + /* Check the type. */ + if(wgt->type != WIDGET_FADER) { + WARN("Not setting fader value on non-fader widget '%s'.", name); + return; + } + + /* Set fader value. */ toolkit_faderSetValue(wgt, value); } -/* Set minimum and maximum fader values. */ +/** + * @brief Set a fader widgets boundries. + * @param wid ID of the window to get widget from. + * @param name Name of the widget. + * @param min Min fader value. + * @param max Max fader value. + */ void window_faderBounds(const unsigned int wid, char* name, double min, double max) { - Widget* wgt = window_getwgt(wid, name); + + double value; + Widget* wgt; + + /* Get the widget. */ + wgt = window_getwgt(wid, name); + if(wgt == NULL) + return; + + /* Check the type. */ + if(wgt->type != WIDGET_FADER) { + WARN("Not setting fader value on non-fader widget '%s'.", name); + return; + } + + /* Set the fader boundries. */ wgt->dat.fad.min = min; wgt->dat.fad.max = max; - double value = MAX(MIN(value, wgt->dat.fad.max), wgt->dat.fad.min); + + /* Set the value. */ + value = MAX(MIN(value, wgt->dat.fad.max), wgt->dat.fad.min); toolkit_faderSetValue(wgt, value); } +/** + * @brief Get the image from an image widget. + * @param wid ID of the window to get widget from. + * @param name Name of the widget. + */ glTexture* window_getImage(const unsigned int wid, char* name) { - Widget* wgt = window_getwgt(wid, name); + Widget* wgt; + + /* get the widget. */ + wgt = window_getwgt(wid, name); + if(wgt == NULL) + return NULL; + + /* Check the type. */ + if(wgt->type != WIDGET_IMAGE) { + WARN("Trying to get image from non-image widget '%s'.", name); + return NULL; + } + + /* Get the value. */ return (wgt) ? wgt->dat.img.image : NULL; } -/* Get the input from an input widget. */ +/** + * @brief Get the input from an input widget. + * @param wid ID of the window to get widget from. + * @param name Name of the widget. + */ char* window_getInput(const unsigned int wid, char* name) { - Widget* wgt = window_getwgt(wid, name); + Widget* wgt; + + /* Get the widget. */ + if(wgt == NULL) + return NULL; + + /* Check the type. */ + if(wgt->type != WIDGET_INPUT) { + WARN("Trying to get input from non-input widget '%s'.", name); + return NULL; + } + + /* Get the value. */ return(wgt) ? wgt->dat.inp.input : NULL; } -/* Get the value from a scrollbar widget. */ -double window_getScrollbarValue(const unsigned int wid, char* name) { - Widget* wgt = window_getwgt(wid, name); - return (wgt) ? wgt->dat.scb.value : 0.; -} - -/* Get value of fader. */ +/** + * @brief Get value of the fader widget. + * @param wid ID of the window to get widget from. + * @param name Name of the widget. + */ double window_getFaderValue(const unsigned int wid, char* name) { - Widget* wgt = window_getwgt(wid, name); + Widget* wgt; + + /* Get the widget. */ + wgt = window_getwgt(wid, name); + if(wgt == NULL) + return 0.; + + /* Check the type. */ + if(wgt->type != WIDGET_FADER) { + WARN("Trying to get fader favlue from non-fader widget '%s'.", name); + return 0.; + } + + /* Return the value. */ return (wgt) ? wgt->dat.fad.value : 0.; } -/* Check if a window exists. */ +/** + * @brief Check to see if a window exists. + * @param wdwname Name of the window to check. + * @return 1 if it exists, 0 if it doesn't. + */ int window_exists(const char* wdwname) { int i; for(i = 0; i < nwindows; i++) @@ -644,7 +975,11 @@ int window_exists(const char* wdwname) { return 0; /* Does not exits! */ } -/* Return the id of a window. */ +/** + * @brief Get the ID of a window. + * @param wdwname Name of the window to get ID of. + * @return ID of the window. + */ unsigned int window_get(const char* wdwname) { int i; for(i = 0; i < nwindows; i++) @@ -653,7 +988,15 @@ unsigned int window_get(const char* wdwname) { return 0; } -/* Create a window. */ +/** + * @brief Create a window. + * @param name Name of the window to create. + * @param x X position of the window (-1 centers). + * @param y Y position of the window (-1 centers). + * @param w Width. + * @param h Height. + * @return Newly created windows ID. + */ unsigned int window_create(char* name, const int x, const int y, const int w, const int h) { @@ -714,8 +1057,6 @@ unsigned int window_create(char* name, const int x, const int y, } /** - * @fn void window_setAccept(const unsigned int wid, void(*accept)(unsigned int, char*)) - * * @brief Set the default accept function of the window. * * This function is called whenever 'enter' is pressed and the current widget @@ -727,13 +1068,15 @@ unsigned int window_create(char* name, const int x, const int y, void window_setAccept(const unsigned int wid, void(*accept)(unsigned int, char*)) { Window* wdw; + /* Get the window. */ wdw = window_wget(wid); - if(wdw != NULL) wdw->accept_fptr = accept; + if(wdw == NULL) return; + + /* Set the accept function. */ + wdw->accept_fptr = accept; } /** - * @fn void window_setCancel(const unsigned int wid, void(*cancel)(unsigned int, char*)) - * * @brief Set the default cancel function of the window. * * This function is called whenever 'escape' is hit and the current widget @@ -745,15 +1088,25 @@ void window_setAccept(const unsigned int wid, void(*accept)(unsigned int, char*) void window_setCancel(const unsigned int wid, void(*cancel)(unsigned int, char*)) { Window* wdw; + /* Get the window. */ wdw = window_wget(wid); - if(wdw != NULL) wdw->cancel_fptr = cancel; + if(wdw == NULL) return; + + /* Set the cancel function. */ + wdw->cancel_fptr = cancel; } -/* Destroy a widget. */ +/** + * @brief Destroys a widget. + * @param widget Widget to destroy. + */ static void widget_cleanup(Widget* widget) { int i; + + /* General freeing. */ if(widget->name) free(widget->name); + /* Type specific. */ switch(widget->type) { case WIDGET_BUTTON: /* Must clear the button display text. */ if(widget->dat.btn.display) free(widget->dat.btn.display); @@ -932,6 +1285,17 @@ static void toolkit_drawOutline(double x, double y, double w, double h, glEnd(); } +/** + * @brief Draw a rectangle. + * + * If lc is NULL, colour will be flat. + * @param x X position to draw at. + * @param y Y position to draw at. + * @param w Width. + * @param h Height. + * @param c Colour. + * @param lc Light colour. + */ static void toolkit_drawRect(double x, double y, double w, double h, glColour* c, glColour* lc) { glShadeModel((lc) ? GL_SMOOTH : GL_FLAT); @@ -1167,11 +1531,6 @@ static void window_render(Window* w) { case WIDGET_IMAGEARRAY: toolkit_renderImageArray(&w->widgets[i], x, y); break; - - case WIDGET_SCROLLBAR: - toolkit_renderScrollbar(&w->widgets[i], x, y); - break; - case WIDGET_FADER: toolkit_renderFader(&w->widgets[i], x, y); } @@ -1187,7 +1546,12 @@ static void window_render(Window* w) { } } -/* Renders a button. */ +/** + * @brief Render a button widget. + * @param btn WIDGET_BUTTON widget to render. + * @param bx Base X position. + * @param by Base Y position. + */ static void toolkit_renderButton(Widget* btn, double bx, double by) { glColour* c, *dc, *lc; double x, y; @@ -1242,6 +1606,12 @@ static void toolkit_renderButton(Widget* btn, double bx, double by) { &cDarkRed, btn->dat.btn.display); } +/** + * @brief Render a text widget. + * @param txt Text widget to render. + * @param bx Base X position. + * @param by Base y position. + */ static void toolkit_renderText(Widget* txt, double bx, double by) { if(txt->dat.txt.text == NULL) return; @@ -1257,7 +1627,12 @@ static void toolkit_renderText(Widget* txt, double bx, double by) { txt->dat.txt.colour, txt->dat.txt.text); } -/* Render the image. */ +/** + * @brief Render an image widget. + * @param img Image widget to render. + * @param bx Base X position. + * @param by Base Y position. + */ static void toolkit_renderImage(Widget* img, double bx, double by) { double x, y; @@ -1283,7 +1658,12 @@ static void toolkit_renderImage(Widget* img, double bx, double by) { } } -/* Render the list. */ +/** + * @brief Render a list widget. + * @param lst List widget to render. + * @param bx Bsae X position. + * @param by Base Y position. + */ static void toolkit_renderList(Widget* lst, double bx, double by) { int i; double x, y, tx, ty, miny; @@ -1335,7 +1715,12 @@ static void toolkit_renderList(Widget* lst, double bx, double by) { } } -/* Render a rectangle. */ +/** + * @brief Render a rectangle widget. + * @param wct Rectangle widget to render. + * @param bx Bsae X position. + * @param by Base Y position. + */ static void toolkit_renderRect(Widget* rct, double bx, double by) { double x, y; @@ -1354,7 +1739,12 @@ static void toolkit_renderRect(Widget* rct, double bx, double by) { } } -/* Render a custom widget. */ +/** + * @brief Renders a custom widget. + * @param cst Custom widget to render. + * @param bx Base X position. + * @param by Base Y position. + */ static void toolkit_renderCust(Widget* cst, double bx, double by) { double x, y; @@ -1375,7 +1765,12 @@ static void toolkit_renderCust(Widget* cst, double bx, double by) { toolkit_unclip(); } -/* Render an input widget. */ +/** + * @brief Render an input widget. + * @param inp Input widget to render. + * @param bx Base X position. + * @param by Base Y position. + */ static void toolkit_renderInput(Widget* inp, double bx, double by) { double x, y, ty; @@ -1401,9 +1796,10 @@ static void toolkit_renderInput(Widget* inp, double bx, double by) { } /** - * @fn static void toolkit_renderImageArray(Widget* iar, double bx, double by) - * * @brief Renders an image array. + * @param iar Image array widget to render. + * @param bx Base X position. + * @param by Base Y position. */ static void toolkit_renderImageArray(Widget* iar, double bx, double by) { int i, j; @@ -1502,18 +1898,6 @@ static void toolkit_drawScrollbar(double x, double y, double w, double h, double toolkit_drawOutline(x, sy, w, 30., 0., toolkit_colDark, NULL); } -/** - * @brief Render a scrollbar. - * @param scb WIDGET_SCROLLBAR widget to render. - * @param bx Base X position. - * @param by Base Y position. - */ -static void toolkit_renderScrollbar(Widget* scb, double bx, double by) { - toolkit_drawScrollbar(bx + scb->x, by + scb->y, - scb->w, scb->h, - scb->dat.scb.value); -} - /** * @brief Render a fader. * @param fad WIDGET_FADER widget to render. @@ -1615,7 +1999,9 @@ static int toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key) { return 0; } -/* Render the window. */ +/** + * @brief Render the windows. + */ void toolkit_render(void) { int i; @@ -1630,8 +2016,11 @@ void toolkit_render(void) { } -/* Toolkit input is handled here. */ -/* If we return 1, the input isn't passed. */ +/** + * @brief Toolkit input handled here. + * @param event Event to handle. + * @return 1 if input was used, 0 if it wasn't. + */ int toolkit_input(SDL_Event* event) { switch(event->type) { case SDL_MOUSEMOTION: @@ -1646,8 +2035,11 @@ int toolkit_input(SDL_Event* event) { return 0; /* Don't block unput. */ } -/* Input. */ -static int mouse_down = 0; +static int mouse_down = 0; /**< Record if mouse is down. */ +/** + * @brief Handle the mouse events. + * @param event Mouse events to handle. + */ static void toolkit_mouseEvent(SDL_Event* event) { int i; double d; @@ -1710,8 +2102,6 @@ static void toolkit_mouseEvent(SDL_Event* event) { toolkit_listMove(wgt, y-wgt->y); if(wgt->type == WIDGET_IMAGEARRAY) toolkit_imgarrMove(wgt, rel_y); - if(wgt->type == WIDGET_SCROLLBAR) - wgt->dat.scb.value -= rel_y / wgt->h; if(wgt->type == WIDGET_FADER) { d = (wgt->w > wgt->h) ? rel_x / wgt->w : rel_y / wgt->h; toolkit_faderSetValue(wgt, wgt->dat.fad.value + d); @@ -1775,10 +2165,10 @@ static void toolkit_mouseEvent(SDL_Event* event) { if(wgt_func) (*wgt_func->dat.btn.fptr)(w->id, wgt_func->name); } -/* Handle the key events. */ -static SDLKey input_key; -static unsigned int input_keyTime; -static int input_keyCounter; +/** + * @brief Registers a key as down (for key repitition. + * @param key Key to register as down. + */ static void toolkit_regKey(SDLKey key) { if((input_key == 0) && (input_keyTime == 0)) { input_key = key; @@ -1787,6 +2177,10 @@ static void toolkit_regKey(SDLKey key) { } } +/** + * @brief Unregisters a key. + * @param key Key to unregister. + */ static void toolkit_unregKey(SDLKey key) { if(input_key == key) { input_key = 0; @@ -1795,19 +2189,29 @@ static void toolkit_unregKey(SDLKey key) { } } +/** + * @brief Clear the registered key. + */ static void toolkit_clearKey(void) { input_key = 0; input_keyTime = 0; input_keyCounter = 0; } +/** + * @brief Handles keyboard events. + * @param event Keyboard event to handle. + * @return 1 if the event is used, 0 if it isn't. + */ static int toolkit_keyEvent(SDL_Event* event) { Window* wdw; Widget* wgt; SDLKey key; + /* Needs to have at least one window. */ if(nwindows <= 0) return 0; + /* Get event and key. */ wdw = &windows[nwindows-1]; wgt = (wdw->focus != -1) ? &wdw->widgets[wdw->focus] : NULL; key = event->key.keysym.sym; @@ -1823,6 +2227,7 @@ static int toolkit_keyEvent(SDL_Event* event) { /* Grab all the events it wants. */ if(toolkit_inputInput(event->type, wgt, key)) return 1; + /* Key specific. */ switch(key) { case SDLK_TAB: if(event->type == SDL_KEYDOWN) @@ -1860,6 +2265,9 @@ static int toolkit_keyEvent(SDL_Event* event) { } } +/** + * @brief Updates the toolkit input for repeating keys. + */ void toolkit_update(void) { unsigned int t; Window* wdw; @@ -1867,13 +2275,17 @@ void toolkit_update(void) { t = SDL_GetTicks(); + /* Must have a key pressed. */ if(input_key == 0) return; + /* Should be repeating. */ if(input_keyTime + INPUT_DELAY + input_keyCounter*INPUT_FREQ > t) return; + /* Increment counter. */ input_keyCounter++; + /* Check to see what it affects. */ if(nwindows > 0) { wdw = &windows[nwindows-1]; wgt = (wdw->focus >= 0) ? &wdw->widgets[wdw->focus] : NULL; @@ -1882,6 +2294,7 @@ void toolkit_update(void) { toolkit_inputInput(SDL_KEYDOWN, wgt, input_key); } + /* Handle the press. */ switch(input_key) { case SDLK_UP: toolkit_listScroll(toolkit_getFocus(), +1); @@ -1894,7 +2307,9 @@ void toolkit_update(void) { } } -/* Focus next widget. */ +/** + * @brief Focus next widget. + */ static void toolkit_nextFocus(void) { Window* wdw = &windows[nwindows-1]; /* Get active window. */ @@ -1909,7 +2324,11 @@ static void toolkit_nextFocus(void) { toolkit_nextFocus(); } -/* Return 1 if the window is focusable. */ +/** + * @brief Check to see if a widget is focusable. + * @param wgt Widget to check if is focusable. + * @return 1 if it's focusable, 0 if it isn't. + */ static int toolkit_isFocusable(Widget* wgt) { if(wgt == NULL) return 0; @@ -1925,6 +2344,9 @@ static int toolkit_isFocusable(Widget* wgt) { } } +/** + * @brief Trigger the focused widget. + */ static void toolkit_triggerFocus(void) { Window* wdw; Widget* wgt; @@ -2014,13 +2436,16 @@ static void toolkit_listScroll(Widget* wgt, int direction) { /** * @brief Change fader value. + * @param fad Fader to set value of. + * @param value Value to set fader to. */ static void toolkit_faderSetValue(Widget* fad, double value) { - Window* w = &windows[nwindows-1]; - value = MAX(MIN(value, fad->dat.fad.max), fad->dat.fad.min); - fad->dat.fad.value = value; + /* Sanity check and value set. */ + fad->dat.fad.value = MAX(MIN(value, fad->dat.fad.max), fad->dat.fad.min); + + /* Run function if needed. */ if(fad->dat.fad.fptr != NULL) - (*fad->dat.fad.fptr)(w->id, fad->name); + (*fad->dat.fad.fptr)(fad->wdw, fad->name); } /** @@ -2234,7 +2659,10 @@ static void toolkit_imgarrMove(Widget* iar, double ry) { } } -/* Return the focused widget. */ +/** + * @brief Get the focused widget. + * @return The focused widget. + */ static Widget* toolkit_getFocus(void) { Window* wdw; wdw = &windows[nwindows-1]; @@ -2244,7 +2672,10 @@ static Widget* toolkit_getFocus(void) { return &wdw->widgets[wdw->focus]; } -/* Init. */ +/** + * @brief Initializes the toolkit. + * @return 0 on success. + */ int toolkit_init(void) { windows = malloc(sizeof(Window)*MIN_WINDOWS); nwindows = 0; @@ -2253,7 +2684,9 @@ int toolkit_init(void) { return 0; } -/* Exit the toolkit. */ +/** + * @brief Exit the toolkit. + */ void toolkit_exit(void) { while(nwindows > 0) window_destroy(windows[0].id); diff --git a/src/toolkit.h b/src/toolkit.h index ced72f2..fa8e8dc 100644 --- a/src/toolkit.h +++ b/src/toolkit.h @@ -50,11 +50,6 @@ void window_addImageArray(const unsigned int wid, glTexture** tex, char** caption, int nelem, /* Elements. */ void(*call)(unsigned int, char*)); -void window_addScrollbar(const unsigned int wid, - const int x, const int y, /* Position. */ - const int w, const int h, /* Size. */ - char* name, double value); /* Value. */ - void window_addFader(const unsigned int wid, const int x, const int y, /* Position. */ const int w, const int h, /* Size, if w > h fader is horizontal, else vertical. */ @@ -73,9 +68,7 @@ void window_enableButton(const unsigned int wid, char* name); /* Image. */ void window_modifyImage(const unsigned int wid, char* name, glTexture* image); void window_imgColour(const unsigned int wid, char* name, glColour* colour); -/* Scroll bar. */ -void window_scrollbarValue(const unsigned int wid, - char* name, double value); +/* Fader. */ void faderValue(const unsigned int wid, char* name, double value); void faderBounds(const unsigned int wid, @@ -96,7 +89,6 @@ char* toolkit_getList(const unsigned int wid, char* name); int toolkit_getListPos(const unsigned int wid, char* name); glTexture* window_getImage(const unsigned int wid, char* name); -double window_getScrollbarValue(const unsigned int wid, char* name); double window_getFaderValue(const unsigned wid, char* name); /* Destroy window. */ diff --git a/src/unidiff.c b/src/unidiff.c index 2ef6272..f79abfc 100644 --- a/src/unidiff.c +++ b/src/unidiff.c @@ -104,12 +104,10 @@ static void diff_hunkSuccess(UniDiff_t* diff, UniHunk_t* hunk); static void diff_cleanup(UniDiff_t* diff); static void diff_cleanupHunk(UniHunk_t* hunk); /* Externed. */ -int diff_save(xmlTextWriterPtr writer); -int diff_load(xmlNodePtr parent); +int diff_save(xmlTextWriterPtr writer); /**< Used in save.c */ +int diff_load(xmlNodePtr parent); /**< Used in save.c */ /** - * @fn int diff_isApplied(char* name) - * * @brief Check if a diff is currently applied. * @param name Diff to check. * @return 0 if it's not applied, 1 if it is. @@ -121,8 +119,6 @@ int diff_isApplied(char* name) { } /** - * @fn static UniDiff_t* diff_get(char* name) - * * @brief Get a diff by name. * @param name Name of the diff to get. * @return The diff if found or NULL if not found. @@ -136,8 +132,6 @@ static UniDiff_t* diff_get(char* name) { } /** - * @fn int diff_apply(char* name) - * * @brief Applies a diff to the universe. * @param name Diff to apply. * @return 0 on success. @@ -196,8 +190,6 @@ int diff_apply(char* name) { } /** - * @fn static int diff_patch(xmlNodePtr parent) - * * @brief Actually applies a diff in XML node form. * @param parent Node containing the diff information. * @return 0 on success. @@ -314,8 +306,6 @@ static int diff_patch(xmlNodePtr parent) { } /** - * @fn static int diff_patchHunk(UniHunk_t* hunk) - * * @brief Applies a hunk and adds it to the diff. * @param diff Diff to which the hunk belongs. * @param hunk Hunk to apply. @@ -345,8 +335,6 @@ static int diff_patchHunk(UniHunk_t* hunk) { } /** - * @fn static void diff_hunkFailed(UniDiff_t* diff, UniHunk_t* hunk) - * * @brief Add a hunk to the failed list. * @param diff Diff to add hunk to. * @param hunk Hunk that failed to apply. @@ -364,8 +352,6 @@ static void diff_hunkFailed(UniDiff_t* diff, UniHunk_t* hunk) { } /** - * @fn static void diff_hunkSuccess(UniDiff_t* diff, UniHunk_t* hunk) - * * @brief Add a hunk to the applied list. * @param diff Diff to add hunk to. * @param hunk Hunk that applied correctly. @@ -383,8 +369,6 @@ static void diff_hunkSuccess(UniDiff_t* diff, UniHunk_t* hunk) { } /** - * @fn void diff_remove(char* name) - * * @brief Remove a diff from the universe. * @param name Diff to remove. */ @@ -400,7 +384,7 @@ void diff_remove(char* name) { } /** - * @fn void diff_clear(void) + * @brief Remove all active diffs. */ void diff_clear(void) { while(diff_nstack > 0) { @@ -409,8 +393,6 @@ void diff_clear(void) { } /** - * @fn static UniDiff_t* diff_newDiff(void) - * * @brief Creates a new UniDiff_t for usage. * @return A newly created UniDiff_t. */ @@ -434,8 +416,6 @@ static UniDiff_t* diff_newDiff(void) { } /** - * @fn static int diff_removeDiff(UniDiff_t* diff) - * * @brief Removes a diff. * @param diff Diff to remove. * @return 0 on success. @@ -481,8 +461,6 @@ static int diff_removeDiff(UniDiff_t* diff) { } /** - * @fn static void diff_cleanup(UniDiff_t* diff) - * * @brief Clean up a diff. * @param diff Diff to clean up. */ @@ -502,8 +480,6 @@ static void diff_cleanup(UniDiff_t* diff) { } /** - * @fn static void diff_cleanupHunk(UniHunk_t* hunk) - * * @brief Clean up a hunk. * @param hunk Hunk to clean up. */ @@ -525,8 +501,6 @@ static void diff_cleanupHunk(UniHunk_t* hunk) { } /** - * @fn int diff_save(xmlTextWriterPtr writer) - * * @brief Save the active diffs. * @param writer XML Writer to use. * @return 0 on success. @@ -547,8 +521,6 @@ int diff_save(xmlTextWriterPtr writer) { } /** - * @fn int diff_load(xmlNodePtr parent) - * * @brief Loads the diffs. * @param parent Parent node containing diffs. * @return 0 on success.