aboutsummaryrefslogtreecommitdiffstats
path: root/src/app.zig
blob: 51d55c4679c746bd972b4f6b13c4f85b72ab7836 (plain) (blame)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const std = @import("std");
const math = @import("math.zig");
const config = @import("config.zig");
const c = @import("c.zig").c;

const Vec2f = math.Vec2f;
const Config = config.Config;

pub const Camera = struct {
    position: Vec2f = .{},
    velocity: Vec2f = .{},
    scale: f32 = 1.0,
    delta_scale: f32 = 0.0,
    scale_pivot: Vec2f = .{},
};

pub const Mouse = struct {
    curr: Vec2f = .{},
    prev: Vec2f = .{},
    drag: bool = false,
};

pub const Flashlight = struct {
    enabled: bool = false,
    shadow: f32 = 0.0,
    radius: f32 = 0.0,
    delta_radius: f32 = 0.0,
};

pub const State = struct {
    camera: Camera = .{},
    mouse: Mouse = .{},
    flashlight: Flashlight = .{},
    dt: f32 = 0.0,
    running: bool = true,
    mirror: bool = false,
};

pub const App = struct {
    config: Config = Config.default(),
    state: State = .{},
    config_path: ?[]const u8 = null,
    allocator: std.mem.Allocator,

    pub fn init(alloc: std.mem.Allocator, cfg_path: ?[]const u8) !App {
        var app = App{
            .allocator = alloc,
            .config = Config.default(),
        };

        if (cfg_path) |p| {
            app.config_path = try alloc.dupe(u8, p);
            app.config.loadFromFile(p);
        }

        app.state.flashlight.radius = app.config.initial_radius;

        return app;
    }

    pub fn deinit(self: *App) void {
        if (self.config_path) |p| {
            self.allocator.free(p);
        }
    }

    pub fn cameraUpdate(self: *App, ws: Vec2f) void {
        const cfg = &self.config;
        const cam = &self.state.camera;
        const m = &self.state.mouse;
        const dt = self.state.dt;

        if (@abs(cam.delta_scale) > cfg.scale_change_threshold) {
            const half = ws.mul(0.5);
            const sub = cam.scale_pivot.sub(half);
            const p0 = sub.div(cam.scale);

            cam.scale += cam.delta_scale * dt;
            if (cam.scale < cfg.min_scale) cam.scale = cfg.min_scale;

            const p1 = sub.div(cam.scale);
            cam.position = cam.position.add(p0.sub(p1));
            cam.delta_scale -= cam.delta_scale * dt * cfg.scale_friction;
        }

        if (!m.drag and cam.velocity.length() > cfg.velocity_threshold) {
            cam.position = cam.position.add(cam.velocity.mul(dt));
            cam.velocity = cam.velocity.sub(cam.velocity.mul(dt * cfg.drag_friction));
        }
    }

    pub fn flashlightUpdate(self: *App) void {
        const fl = &self.state.flashlight;
        const dt = self.state.dt;
        const cfg = &self.config;

        fl.shadow = if (fl.enabled)
            @min(fl.shadow + cfg.fade_speed * dt, cfg.max_shadow_opacity)
        else
            @max(fl.shadow - cfg.fade_speed * dt, 0.0);

        if (@abs(fl.delta_radius) > cfg.radius_change_threshold) {
            fl.radius = @max(0.0, fl.radius + fl.delta_radius * dt);
            fl.delta_radius -= fl.delta_radius * cfg.radius_damping * dt;
        }
    }

    fn worldPosition(camera: *Camera, pos: Vec2f) Vec2f {
        return pos.div(camera.scale);
    }

    pub fn processEvents(self: *App, x11: anytype) void {
        var ev: c.XEvent = undefined;
        while (c.XPending(x11.display) != 0) {
            _ = c.XNextEvent(x11.display, &ev);

            switch (ev.type) {
                c.KeyPress => self.handleKeypress(&ev.xkey),
                c.MotionNotify => self.handleMousemove(&ev.xmotion, x11.refresh_rate),
                c.ButtonPress => self.handleButtonpress(&ev.xbutton),
                c.ButtonRelease => self.handleButtonrelease(&ev.xbutton),
                c.ClientMessage => {
                    if (@as(c.Atom, @bitCast(ev.xclient.data.l[0])) == x11.wm_delete_window)
                        self.state.running = false;
                },
                else => {},
            }
        }
    }

    fn handleKeypress(self: *App, ke: *c.XKeyEvent) void {
        const key = c.XLookupKeysym(ke, 0);

        if (key == self.config.key_escape or key == c.XK_q)
            self.state.running = false;

        if (key == c.XK_r) {
            if (self.config_path) |p| {
                self.config.loadFromFile(p);
            }
        }

        if (key == self.config.key_flashlight)
            self.state.flashlight.enabled = !self.state.flashlight.enabled;

        if (key == self.config.key_reset) {
            self.state.camera = .{ .scale = 1.0 };
            self.state.flashlight.shadow = 0.0;
            self.state.flashlight.radius = self.config.initial_radius;
            self.state.flashlight.delta_radius = 0.0;
        }

        if (key == self.config.key_mirror)
            self.state.mirror = !self.state.mirror;

        if (key == self.config.key_zoom_in) {
            self.state.camera.delta_scale += self.config.scroll_speed;
            self.state.camera.scale_pivot = self.state.mouse.curr;
        }

        if (key == self.config.key_zoom_out) {
            self.state.camera.delta_scale -= self.config.scroll_speed;
            self.state.camera.scale_pivot = self.state.mouse.curr;
        }
    }

    fn handleMousemove(self: *App, motion: *c.XMotionEvent, rr: i32) void {
        self.state.mouse.curr = .{ .x = @floatFromInt(motion.x), .y = @floatFromInt(motion.y) };

        if (self.state.mouse.drag) {
            const prev = worldPosition(&self.state.camera, self.state.mouse.prev);
            const cur = worldPosition(&self.state.camera, self.state.mouse.curr);
            self.state.camera.position = self.state.camera.position.add(prev.sub(cur));
            self.state.camera.velocity = prev.sub(cur).mul(@floatFromInt(rr));
        }

        self.state.mouse.prev = self.state.mouse.curr;
    }

    fn handleButtonpress(self: *App, be: *c.XButtonEvent) void {
        const ctrl_pressed = (be.state & self.config.modifier_flashlight) != 0;

        if (be.button == self.config.button_drag) {
            self.state.mouse.prev = self.state.mouse.curr;
            self.state.mouse.drag = true;
            self.state.camera.velocity = .{};
        } else if (be.button == self.config.button_zoom_in) {
            if (ctrl_pressed and self.state.flashlight.enabled) {
                self.state.flashlight.delta_radius += self.config.initial_delta_radius;
            } else {
                self.state.camera.delta_scale += self.config.scroll_speed;
                self.state.camera.scale_pivot = self.state.mouse.curr;
            }
        } else if (be.button == self.config.button_zoom_out) {
            if (ctrl_pressed and self.state.flashlight.enabled) {
                self.state.flashlight.delta_radius -= self.config.initial_delta_radius;
            } else {
                self.state.camera.delta_scale -= self.config.scroll_speed;
                self.state.camera.scale_pivot = self.state.mouse.curr;
            }
        }
    }

    fn handleButtonrelease(self: *App, be: *c.XButtonEvent) void {
        if (be.button == self.config.button_drag)
            self.state.mouse.drag = false;
    }
};