summaryrefslogtreecommitdiff
path: root/bindings/minimgui.py
blob: 45382476a9167e8d39ed0d02e0c2c8919c00ba43 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from cffi import FFI

ffi = FFI()
ffi.cdef("""
typedef struct {
    float x, y;
} V2;

typedef struct {
    float x, y;
    float w, h;
} Rect;

typedef struct {
    float a, r, g, b;
} Color;

typedef void* Image;

typedef bool(*const InitFn)();
typedef bool(*const LoopFn)(float dt);
typedef void(*const DeinitFn)();
void run (
    InitFn const init_fn,
    LoopFn const loop_fn,
    DeinitFn const deinit_fn
);

typedef void* Context;
Context context_init(void);
void context_deinit(Context ctx);

typedef struct {
    Color color;
    Image image;
    bool* hover;
    bool consume_hover;
    bool ignore_hover_if_consumed;
    bool begin_drag_if_hover_and;
    bool end_drag_if;
    V2** drag;
} RectangleOptions_;
bool rectangle_(Context ctx, Rect rect, RectangleOptions_ o);
""")
lib = ffi.dlopen("../zig-out/lib/libminimgui.so")

class Context:
    def __init__(self):
        self.ctx = lib.context_init()

    def deinit(self):
        lib.context_deinit(self.ctx)

    def rectangle(self, *args, color = {"a": 1.0, "r": 1.0, "g": 1.0, "b": 1.0}):
        if len(args) == 4:
            x, y, w, h = args
            rect = {
                "x": x,
                "y": y,
                "w": w,
                "h": h
            }
        elif len(args) == 1:
            rect = args[0]

        lib.rectangle_(self.ctx, rect, {
            "color": to_color(color)
        })

# bgra
def color_hex(x):
    a = (x >> 24) & 0xFF
    r = (x >> 16) & 0xFF
    g = (x >> 8) & 0xFF
    b = x & 0xFF
    return (a / 255.0, r / 255.0, g / 255.0, b / 255.0)
def to_color(color):
    if isinstance(color, int):
        return color_hex(color)
    else:
        return color

def run(*args):
    if len(args) == 3:
        init_fn = args[0]
        loop_fn = args[1]
        deinit_fn = args[2]
    elif len(args) <= 1:
        def init_fn():
            global global_context
            global_context = Context()
        def deinit_fn():
            global_context.deinit()
    if len(args) == 1:
        loop_fn = args[0]
    else:
        import sys
        main = sys.modules["__main__"]
        loop_fn = getattr(main, "loop", None)

    @ffi.callback("InitFn")
    def _init():
        res = init_fn()
        if res is None or res is True:
            return True
        elif res is False:
            return False
        else:
            raise ValueError("minimgui.init() must return None or True")

    @ffi.callback("LoopFn")
    def _loop(dt):
        res = loop_fn(dt)
        if res is None or res is True:
            return True
        elif res is False:
            return False
        else:
            raise ValueError("minimgui.loop() must return None or True")

    @ffi.callback("DeinitFn")
    def _deinit():
        deinit_fn()

    lib.run(_init, _loop, _deinit)

def __getattr__(name):
    return getattr(global_context, name)