timesynk/engine/ui/elements.h

107 lines
3.0 KiB
C

/******
elements.c/.h provide the structures and functions for user interface elements (i.e., text areas, buttons, etc.). These functions and structs are intended to be abstracted from any actual drawing mechanics - that is to say, they are written to be usable, with some extra effort, by any visual library, be it X11, SDL, or even ncurses. As such, the Element structs provide the void pointer *user. This can be used to point to an SDL_Surface, a struct for storing more data, etc..
******/
#ifndef ELEMENTS_H
#define ELEMENTS_H
#include <stdint.h>
#include "../../common/c_extra.h"
#define E_MAX_EVENTS 6
enum {
E_EVENT_NEW = 0,
E_EVENT_FREE = 1,
E_EVENT_CHANGE = 2,
E_EVENT_ACTIVE = 3
};
enum {
E_TYPE_TEXT = 1,
E_TYPE_BUTTON = 2
};
enum {
E_STATE_NORMAL = 1,
E_STATE_ACTIVE = 2
};
enum {
E_FLAG_HIDE = 1,
E_FLAG_UPDATE = 2
};
enum {
E_CHANGE_TEXT = 1,
E_CHANGE_VALUE = 2,
E_CHANGE_FONT = 4
};
struct ElementList {
struct Element *first;
struct Element *last;
void *user;
};
struct ElementList *newElementList();
void addElementToList(struct ElementList *list, struct Element *element);
void freeElementList(struct ElementList *list);
void freeElementsFromList(struct ElementList *list);
struct Dimension {
int16_t x, y;
uint16_t w, h;
};
struct Element {
float sx; // start x
float sy; // start y
struct Dimension dimen;
int type;
int id;
int state;
int key_pos; // index in string of below key
int key; // shortcut key
unsigned int flags;
struct Element *next;
struct Element *prev;
void *data; // Sub-element type - TextElement, ButtonElement, etc.
void (*callback)(); // function callback for element activation
void *self; // interface-specific data for the button (i.e., SDL_Surface for button)
void *target; // interface-specific target to draw to
void *font; // pointer to font, if it exists.
void *user; // pointer for extra user-specific data
void (*onEvent[E_MAX_EVENTS])(struct Element *element);
};
struct TextElement {
int length;
char *string;
void *font;
};
struct ButtonElement {
int length;
char *string;
void *font;
};
struct Element *newElement(int type, int id, void *function, const struct Dimension dimen);
void removeElementFromList(struct ElementList *list, struct Element *element);
void freeElementFromList(struct ElementList *list, struct Element *element);
void freeElement(struct Element *element);
int setElementText(struct Element *element, const char *string);
int setElementValue(struct Element *element, int value);
int setElementFont(struct Element *element, void *font);
int setElementEvent(struct Element *element, int event_id, void *event_function);
int setElementKey(struct Element *element, int key, int key_pos);
char *getElementText(struct Element *element);
// NOTE: the following functions are defined here, but must be implemented in the interface.
void genElementDimensions(struct Element *element);
int getElementX(struct Element *element);
int getElementY(struct Element *element);
#endif