From e33ab321d0979eac3b9ca1c486f78f0e872105db Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Sun, 2 Mar 2025 16:12:06 +0200 Subject: [PATCH] add reset view button --- src/app.zig | 55 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/src/app.zig b/src/app.zig index 0baf257..45a3a0e 100644 --- a/src/app.zig +++ b/src/app.zig @@ -22,8 +22,6 @@ const max_files = 32; const FileChannel = struct { path: []u8, - min_value: f64, - max_value: f64, samples: []f64, fn deinit(self: FileChannel, allocator: std.mem.Allocator) void { @@ -42,8 +40,6 @@ const DeviceChannel = struct { units: i32 = NIDaq.c.DAQmx_Val_Volts, min_sample_rate: f64, max_sample_rate: f64, - min_value: f64, - max_value: f64, active_task: ?*TaskPool.Entry = null, @@ -57,7 +53,12 @@ const ChannelView = struct { view_rect: Graph.ViewOptions, follow: bool = false, - height: f32 = 150, + height: f32 = 200, + + default_from: f32, + default_to: f32, + default_min_value: f64, + default_max_value: f64, source: union(enum) { file: usize, @@ -262,22 +263,24 @@ pub fn appendChannelFromFile(self: *App, path: []const u8) !void { const loaded_file_index = findFreeSlot(FileChannel, &self.loaded_files) orelse return error.FileLimitReached; self.loaded_files[loaded_file_index] = FileChannel{ - .min_value = min_value, - .max_value = max_value, .path = path_dupe, .samples = samples }; errdefer self.loaded_files[loaded_file_index] = null; - const margin = 0.1; - const sample_range = max_value - min_value; + const from: f32 = 0; + const to: f32 = @floatFromInt(samples.len); self.channel_views.appendAssumeCapacity(ChannelView{ .view_rect = .{ - .from = 0, - .to = @floatFromInt(samples.len), - .min_value = min_value - sample_range * margin, - .max_value = max_value + sample_range * margin + .from = from, + .to = to, + .min_value = min_value, + .max_value = max_value }, + .default_from = from, + .default_to = to, + .default_min_value = min_value, + .default_max_value = max_value, .source = .{ .file = loaded_file_index } }); errdefer _ = self.channel_views.pop(); @@ -310,8 +313,6 @@ pub fn appendChannelFromDevice(self: *App, channel_name: []const u8) !void { .name = name_buff, .min_sample_rate = ni_daq.getMinSampleRate(channel_name_z) catch max_sample_rate, .max_sample_rate = max_sample_rate, - .min_value = min_value, - .max_value = max_value, .samples = std.ArrayList(f64).init(self.allocator) }; errdefer self.device_channels[device_channel_index] = null; @@ -323,6 +324,10 @@ pub fn appendChannelFromDevice(self: *App, channel_name: []const u8) !void { .min_value = min_value, .max_value = max_value }, + .default_from = 0, + .default_to = 0, + .default_min_value = min_value, + .default_max_value = max_value, .source = .{ .device = device_channel_index } }); errdefer _ = self.channel_views.pop(); @@ -499,6 +504,8 @@ fn showChannelView(self: *App, channel_view: *ChannelView) !void { source.lockSamples(); defer source.unlockSamples(); + var channel_rect_opts: *Graph.ViewOptions = &channel_view.view_rect; + const channel_box = self.ui.newBoxFromPtr(channel_view); channel_box.background = rl.Color.blue; channel_box.layout_axis = .Y; @@ -516,6 +523,18 @@ fn showChannelView(self: *App, channel_view: *ChannelView) !void { self.ui.pushParent(tools_box); defer self.ui.popParent(); + { + const reset_view_button = self.ui.button(.text, "Reset view"); + reset_view_button.size.y = UI.Size.percent(1, 0); + + if (self.ui.signalFromBox(reset_view_button).clicked()) { + channel_rect_opts.from = channel_view.default_from; + channel_rect_opts.to = channel_view.default_to; + channel_rect_opts.min_value = channel_view.default_min_value; + channel_rect_opts.max_value = channel_view.default_max_value; + } + } + if (source == .device) { const device_channel = source.device; @@ -542,8 +561,8 @@ fn showChannelView(self: *App, channel_view: *ChannelView) !void { .continous = .{ .sample_rate = device_channel.max_sample_rate } }, .{ - .min_value = device_channel.min_value, - .max_value = device_channel.max_value, + .min_value = channel_view.default_min_value, + .max_value = channel_view.default_max_value, .units = device_channel.units, .channel = channel_name } @@ -571,8 +590,6 @@ fn showChannelView(self: *App, channel_view: *ChannelView) !void { } { - var channel_rect_opts: *Graph.ViewOptions = &channel_view.view_rect; - const graph_box = self.ui.newBoxFromString("Graph"); graph_box.flags.insert(.clickable); graph_box.flags.insert(.draggable);