DVUI

processEvent

Parameters

#
self:*ScaleWidget
*ScaleWidget
e:*Event
*Event

Source

Implementation

#
pub fn processEvent(self: *ScaleWidget, e: *Event) void {
    if (e.evt == .mouse and (e.evt.mouse.button == .touch0 or e.evt.mouse.button == .touch1)) {
        const idx: usize = if (e.evt.mouse.button == .touch0) 0 else 1;
        switch (e.evt.mouse.action) {
            .press => {
                self.touchPoints[idx] = e.evt.mouse.p;
                if (self.touchPoints[1 - idx] != null) {
                    // both fingers down, grab capture
                    e.handle(@src(), self.data());

                    // end any drag that might have been happening
                    dvui.dragEnd();
                    dvui.captureMouse(self.data(), e.num);
                }
            },
            .release => {
                self.touchPoints[idx] = null;
                if (dvui.captured(self.data().id)) {
                    e.handle(@src(), self.data());
                    dvui.captureMouse(null, e.num);
                }
            },
            .motion => {
                if (self.touchPoints[0] != null and self.touchPoints[1] != null) {
                    e.handle(@src(), self.data());

                    if (self.old_dist == null) {
                        const dx = self.touchPoints[0].?.x - self.touchPoints[1].?.x;
                        const dy = self.touchPoints[0].?.y - self.touchPoints[1].?.y;
                        self.old_dist = @sqrt(dx * dx + dy * dy);
                        self.old_scale = self.scale.*;
                    }

                    self.touchPoints[idx] = e.evt.mouse.p;

                    const dx = self.touchPoints[0].?.x - self.touchPoints[1].?.x;
                    const dy = self.touchPoints[0].?.y - self.touchPoints[1].?.y;
                    const new_dist: f32 = @sqrt(dx * dx + dy * dy);

                    self.scale.* = std.math.clamp(self.old_scale * new_dist / self.old_dist.?, 0.1, 10);
                }
            },
            else => {},
        }
    }
}