fixed wrong tick marks issue when zoomed

This commit is contained in:
Vicente Ferrari Smith 2024-12-17 14:56:21 +01:00
parent d9e44bca22
commit ce61ac812d

View File

@ -15,13 +15,11 @@ PLOT_DATA_STYLE :: enum {
EVERY_FRAME;
}
draw_plot :: (key: string, xdata: []float64, yarrays: [][]float64, using settings: PlotSettings, data_style := PLOT_DATA_STYLE.ONCE) {
plot_size := ImGui.GetContentRegionAvail();
draw_plot :: (key: string, size: Vector2, xdata: []float64, yarrays: [][]float64, using settings: PlotSettings, data_style := PLOT_DATA_STYLE.ONCE) {
plot, success := table_find(*plots, key);
if !success {
plot = New(Plot);
init_plot(plot, key, xx plot_size.x, xx plot_size.y);
init_plot(plot, key, xx size.x, xx size.y);
table_set(*plots, key, plot);
if data_style == .ONCE {
@ -150,6 +148,11 @@ draw_plot :: (key: string, xdata: []float64, yarrays: [][]float64, using setting
ImGui.RadioButton("Tick marks", xx *plot.plot_style, 0); ImGui.SameLine();
ImGui.RadioButton("Scale marker", xx *plot.plot_style, 1);
child_size := ImGui.GetContentRegionAvail();
child_size.y = 260;
if ImGui.BeginChild(key.data, child_size, ImGui.ChildFlags.None, ImGui.WindowFlags.HorizontalScrollbar) {
if plot.plot_style == {
case .TICK_MARKS;
plot.left_bearing = 40;
@ -166,11 +169,11 @@ draw_plot :: (key: string, xdata: []float64, yarrays: [][]float64, using setting
//plot_size = .{plot_size.x, min(260.0, plot_size.y / workspace.f64_arrays.count)};
if plot_size.x < 0 || plot_size.y < 0 {
if plot.width < 0 || plot.height < 0 {
log("[Warning] The plot has dimensions lower than 0.");
} else {
if plot.width != cast(s32) plot_size.x || plot.height != cast(s32) plot_size.y {
plot_resize(plot, xx plot_size.x, xx plot_size.y);
if plot.width != cast(s32) size.x || plot.height != cast(s32) size.y {
plot_resize(plot, xx size.x, xx size.y);
}
ar : float = cast(float) plot.width / cast(float) plot.height;
@ -255,7 +258,7 @@ draw_plot :: (key: string, xdata: []float64, yarrays: [][]float64, using setting
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
ImGui.Image(cast(ImGui.ImTextureID) plot.texture, plot_size, .{0.0, 1.0}, .{1.0, 0.0});
ImGui.Image(cast(ImGui.ImTextureID) plot.texture, size, .{0.0, 1.0}, .{1.0, 0.0});
plot.last_frame = frame;
}
@ -268,6 +271,9 @@ draw_plot :: (key: string, xdata: []float64, yarrays: [][]float64, using setting
remove it;
}
}
}
ImGui.EndChild();
}
free_old_plots :: () {
@ -503,8 +509,8 @@ draw_axis :: (using plot: Plot, style: PLOT_STYLE, colors: PlotColors) {
x_ticks := 10;
for 0..x_ticks {
xpos : s64 = cast(s64) (cast(float) it / cast(float) x_ticks * cast(float) (content_width));
xxx := (-0.5 + cast(float) it / cast(float) x_ticks) / zoom + 0.5;
xxx = xxx * (xmax - pos.x / zoom) + (1 - xxx) * (xmin - pos.x / zoom);
xxx := (((cast(float) it / cast(float) x_ticks) - 0.5) / zoom) + 0.5;
xxx = xxx * (xmax - (pos.x)) + (1 - xxx) * (xmin - (pos.x));
array_add(*positions, .{cast(float) xpos, 0.0});
array_add(*positions, .{cast(float) xpos, cast(float) (content_height )});
@ -523,7 +529,7 @@ draw_axis :: (using plot: Plot, style: PLOT_STYLE, colors: PlotColors) {
for 0..y_ticks {
ypos : s64 = cast(s64) (cast(float) it / cast(float) y_ticks * (content_height ));
yyy := ((0.5) - cast(float) it / cast(float) y_ticks) / zoom + 0.5;
yyy = yyy * (ymax - pos.y / zoom) + (1 - yyy) * (ymin - pos.y / zoom);
yyy = yyy * (ymax - pos.y) + (1 - yyy) * (ymin - pos.y);
array_add(*positions, .{0.0, cast(float) ypos});
array_add(*positions, .{cast(float) (content_width ), cast(float) ypos});