Files
k3/src/k3menu.h

226 lines
4.8 KiB
C

#pragma once
#include<stdint.h>
#include<stdlib.h>
#include<stdbool.h>
#include<stddef.h>
struct k3Font;
struct k3MObj;
#define k3M_EVENT_MOUSE_ENTER 0
#define k3M_EVENT_MOUSE_MOTION 1
#define k3M_EVENT_MOUSE_LEAVE 2
#define k3M_EVENT_MOUSE_PRESS 3
#define k3M_EVENT_MOUSE_RELEASE 4
#define k3M_EVENT_MOUSE_CLICK 5
#define k3M_EVENT_KEY_PRESS 6
#define k3M_EVENT_KEY_RELEASE 7
#define k3M_EVENT_INPUT 8
#define k3M_EVENT_DRAW 9
#define k3M_EVENT_COMPLETE 10
#define k3M_EVENT_MEASURE 11
#define k3M_EVENT_ARRANGE 12
#define k3M_EVENT_POST_ARRANGE 13
#define k3M_EVENT_SET_HEIGHT_FROM_WIDTH 14
#define k3M_EVENT_ALL 15
#define k3M_MOUSE_BUTTON_0 0
#define k3M_MOUSE_BUTTON_1 1
#define k3M_MOUSE_BUTTON_2 2
#define k3M_EVENTKIND_DIRECT 0
#define k3M_EVENTKIND_CAPTURE 1
#define k3M_EVENTKIND_BUBBLE 2
#define k3M_USERDATA_SIZE 32
#define k3M_ALIGN_LEFT 0
#define k3M_ALIGN_TOP 3
#define k3M_ALIGN_CENTER 1
#define k3M_ALIGN_RIGHT 2
#define k3M_ALIGN_BOTTOM 4
#define k3M_IDX_TOP 0
#define k3M_IDX_RIGHT 1
#define k3M_IDX_BOTTOM 2
#define k3M_IDX_LEFT 3
#ifdef k3M_FIXED_POINT
typedef uint8_t k3MCC;
#else
typedef float k3MCC;
#endif
#define k3M_UNIT_ABSOLUTE 0
#define k3M_UNIT_PROPORTION 1
#include"k3menu_internal.h"
struct k3MEvent {
uint16_t code;
uint8_t kind;
struct k3MObj *original;
struct k3MObj *target;
union {
struct {
int button;
int16_t x;
int16_t y;
} mouse;
struct {
uint32_t num;
} key;
struct {
uint32_t code;
} input;
};
};
typedef bool(*k3MEventHandlerFunc)(struct k3MEvent*, uint8_t *ud);
typedef struct k3MEventHandler {
uint8_t ud[k3M_USERDATA_SIZE];
k3MEventHandlerFunc callback;
uint16_t code;
} k3MEventHandler;
enum k3MPropertyType {
k3M_PROP_BG_COLOR,
k3M_PROP_FG_COLOR,
k3M_PROP_BORDER_RADIUS,
k3M_PROP_MARGIN,
k3M_PROP_PADDING,
k3M_PROP_HORIZONTAL_ALIGNMENT,
k3M_PROP_VERTICAL_ALIGNMENT,
k3M_PROP_LEFT,
k3M_PROP_TOP,
k3M_PROP_WIDTH,
k3M_PROP_HEIGHT,
k3M_PROP_FONT_SIZE,
k3M_PROP_SCROLL_ANYWHERE,
k3M_PROP_SCROLLBAR_SIZE,
};
struct k3MProperty {
enum k3MPropertyType type;
uint8_t units[4];
union {
intmax_t si[4];
void *ptr[4];
uint8_t buf[32];
float f[4];
};
};
struct k3MObj {
struct k3MObj *parent;
size_t childCount;
struct k3MObj **children;
int16_t x;
int16_t y;
int16_t w;
int16_t h;
int16_t wDesired;
int16_t hDesired;
bool invisible, hovered, disabled, crop;
k3MEventHandler *handlers;
size_t handlerCount;
struct k3MProperty *properties;
size_t propertyCount;
};
struct k3MObj *k3MObj();
/* Akin to the measure pass of WPF. Recursive, called by parent. Sets wDesired and hDesired. */
void k3MMeasure(struct k3MObj *this);
/* Akin to the arrange pass of WPF. Recursive, called by parent after w and h is set. Adjusts all children using it's own definitive size. */
void k3MArrange(struct k3MObj *this);
/* Set linear layout management on this object. Must not be called on descendants of k3MObj. */
void k3MSetLayoutLinear(struct k3MObj *this, bool vertical);
#define k3MenuSetBounds(a, x, y, w, h) k3MenuSetBounds_((struct k3MObj*) (a), (x), (y), (w), (h))
void k3MenuSetBounds_(struct k3MObj *this, int16_t x, int16_t y, int16_t w, int16_t h);
#define k3MRegisterEventHandler(a, c, cb, ud, udsz) k3MRegisterEventHandler_((struct k3MObj*) (a), (c), (cb), (ud), (udsz))
int k3MRegisterEventHandler_(struct k3MObj *obj, uint16_t evcode, k3MEventHandlerFunc callback, uint8_t *ud, size_t udSize);
bool k3MEventSend(struct k3MEvent *ev);
int k3MRemoveChild(struct k3MObj *parent, struct k3MObj *child);
void k3MAddChild(struct k3MObj *parent, struct k3MObj *child);
void k3MOverrideProperty(struct k3MObj *obj, struct k3MProperty);
struct k3MProperty *k3MFindProperty(struct k3MObj *obj, enum k3MPropertyType, bool direct);
struct k3MLabel {
struct k3MObj;
struct k3Font *font;
float sz;
char *txt;
};
struct k3MLabel *k3MLabel(struct k3Font *font, float sz, const char *txt);
struct k3MScreen {
struct k3MObj;
struct k3MObj *keyboardFocus;
struct k3MObj *lastHover;
struct k3MObj *mouseCapture;
};
struct k3MScreen *k3MScreen();
bool k3MCaptureMouse(struct k3MScreen*, struct k3MObj*);
void k3MReleaseMouse(struct k3MScreen*);
struct k3MTextButton {
struct k3MObj;
struct k3Font *font;
float sz;
char *txt;
};
struct k3MTextButton *k3MTextButton(struct k3Font *font, float sz, const char *txt);
struct k3MTextInput {
struct k3MObj;
struct k3Font *font;
float sz;
char *placeholder;
char *txt;
};
struct k3MTextInput *k3MTextInput(struct k3Font *font, float sz, const char *placeholder, const char *txt);
struct k3MImage {
struct k3MObj;
k3MImageData data;
};
struct k3MImage *k3MImage(k3MImageData);
struct k3MScrollbox {
struct k3MObj;
int mouseHeld;
int16_t mouseX;
int16_t mouseY;
float scrollbarOffset;
int16_t scrollbarLength;
};
struct k3MScrollbox *k3MScrollbox();
struct k3MObj *k3MScrollboxGetContent(struct k3MScrollbox*);