-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathScaleWidget.zig
80 lines (66 loc) · 2.21 KB
/
ScaleWidget.zig
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
const std = @import("std");
const dvui = @import("../dvui.zig");
const Event = dvui.Event;
const Options = dvui.Options;
const Rect = dvui.Rect;
const RectScale = dvui.RectScale;
const Size = dvui.Size;
const Widget = dvui.Widget;
const WidgetData = dvui.WidgetData;
const BoxWidget = dvui.BoxWidget;
const ScaleWidget = @This();
wd: WidgetData = undefined,
scale: f32 = undefined,
box: BoxWidget = undefined,
pub fn init(src: std.builtin.SourceLocation, scale_in: f32, opts: Options) ScaleWidget {
var self = ScaleWidget{};
const defaults = Options{ .name = "Scale" };
self.wd = WidgetData.init(src, .{}, defaults.override(opts));
self.scale = scale_in;
return self;
}
pub fn install(self: *ScaleWidget) !void {
dvui.parentSet(self.widget());
try self.wd.register();
try self.wd.borderAndBackground(.{});
self.box = BoxWidget.init(@src(), .vertical, false, self.wd.options.strip().override(.{ .expand = .both }));
try self.box.install();
try self.box.drawBackground();
}
pub fn widget(self: *ScaleWidget) Widget {
return Widget.init(self, data, rectFor, screenRectScale, minSizeForChild, processEvent);
}
pub fn data(self: *ScaleWidget) *WidgetData {
return &self.wd;
}
pub fn rectFor(self: *ScaleWidget, id: u32, min_size: Size, e: Options.Expand, g: Options.Gravity) Rect {
var s: f32 = undefined;
if (self.scale > 0) {
s = 1.0 / self.scale;
} else {
// prevent divide by zero
s = 1_000_000.0;
}
_ = id;
return dvui.placeIn(self.wd.contentRect().justSize().scale(s), min_size, e, g);
}
pub fn screenRectScale(self: *ScaleWidget, rect: Rect) RectScale {
var rs = self.wd.contentRectScale();
rs.s *= self.scale;
return rs.rectToRectScale(rect);
}
pub fn minSizeForChild(self: *ScaleWidget, s: Size) void {
self.wd.minSizeMax(self.wd.options.padSize(s.scale(self.scale)));
}
pub fn processEvent(self: *ScaleWidget, e: *Event, bubbling: bool) void {
_ = bubbling;
if (e.bubbleable()) {
self.wd.parent.processEvent(e, true);
}
}
pub fn deinit(self: *ScaleWidget) void {
self.box.deinit();
self.wd.minSizeSetAndRefresh();
self.wd.minSizeReportToParent();
dvui.parentReset(self.wd.id, self.wd.parent);
}