aboutsummaryrefslogtreecommitdiff
path: root/teensy/teensy_ui.h
blob: 1e97376d3baa3334e10beb8e244640c29dcffb57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef TEENSY_UI_H_
#define TEENSY_UI_H_

#include "teensy_common.h"
#include "teensy.h"

#define TYUI_MAX_LAYOUT_SIZE (16)
#define TYUI_DEFAULT_MIN_WINDOW_SIZE (ty_vec2i(32, 32))

#define TYUI_WIN_NORESIZE  (1 << 0)
#define TYUI_WIN_NOCLOSE   (1 << 1)
#define TYUI_WIN_NOMOVE    (1 << 2)
#define TYUI_WIN_INVISIBLE (1 << 3)

// Marks the beginning of a window, with sane defaults.
#define tyui_begin_window(_title, _rect, _id) \
    tyui_begin_window_ex((tyui_Window_Conf){ \
        .title = _title, \
        .rect = _rect, \
        .min_size = TYUI_DEFAULT_MIN_WINDOW_SIZE, \
        .id = _id, \
        .flags = 0, \
    }, NULL)

// Displays left-aligned, formatted text.
#define tyui_text(...) tyui_text_ex(TYUI_ALIGN_LEFT, __VA_ARGS__)
// Displays a slider with a centered value display.
#define tyui_slider(min, max, value_ptr) \
    tyui_slider_ex(min, max, value_ptr, true, TYUI_ALIGN_CENTER)
// Displays a slider without a value display.
#define tyui_blank_slider(min, max, value_ptr) \
    tyui_slider_ex(min, max, value_ptr, false, 0)

typedef uint8_t tyui_Align;
enum {
    TYUI_ALIGN_LEFT,
    TYUI_ALIGN_RIGHT,
    TYUI_ALIGN_CENTER,
};

// An ID to refer to resources from TYUI.
typedef uint8_t tyui_Id;

typedef struct {
    ty_Color fg_normal;
    ty_Color bg_normal;
    ty_Color fg_hover;
    ty_Color bg_hover;
    ty_Color fg_pressed;
    ty_Color bg_pressed;
    ty_Color win_bg;
    ty_Color win_border;
    ty_Color win_title;
    ty_Color frame;
    int title_bar_height;
    int padding;
    int control_padding;
    int frame_size;
    int grabber_size;
    const char *slider_fmt;
} tyui_Style;

typedef struct {
    const char *title;
    ty_Recti rect;
    ty_Vec2i min_size;
    tyui_Id *id;
    uint32_t flags;
} tyui_Window_Conf;

void tyui_init(const ty_Font *font);
void tyui_deinit(void);

// Pushes a layout to the layout stack. `column_widths` is either whole widths
// in pixels, a negative number to calculate the width from the right side of
// the layout, or a percentage of the width, from 0-1.
void tyui_push_layout(const float *column_widths);
// Removes the most recently defined layout.
void tyui_pop_layout(void);

// Marks the beginning of a window. Use `tyui_begin_window()` unless you need
// something specific.
bool tyui_begin_window_ex(tyui_Window_Conf conf, bool *closed_ptr);
// Marks the end of a window. Must only be called if the window actually opens.
void tyui_end_window(void);

// Displays a button. Returns true if it has been clicked.
bool tyui_button(const char *text);
// Displays text with a custom text alignment.
void tyui_text_ex(tyui_Align align, const char *fmt, ...);
// Displays a slider with custom alignment.
void tyui_slider_ex(
    float min,
    float max,
    float *value_ptr,
    bool show_value,
    tyui_Align value_align
);

// Draws all accumulated draw commands and initializes the next frame.
void tyui_draw(void);

#endif // TEENSY_UI_H_