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
|
/**
* glamac_view.h - header file from glamac_view.c.
*
* Copyright (C) 2025 https://optics-design.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* See the COPYING file for the full license text.
*/
#ifndef GLAMAC_VIEW_H
#define GLAMAC_VIEW_H
#include <SDL.h>
#include "glamacdef.h"
// Constants for view
#define PADDING_PERCENT 0.08f // Padding as percentage of window size
#define PAN_STEP 0.05f // Step size for keyboard panning
#define ZOOM_FACTOR 1.2f // Zoom factor for zoom operations
#define MIN_ZOOM 0.5f // Minimum zoom level
#define MAX_ZOOM 10.0f // Maximum zoom level
// State for zooming and panning
typedef struct {
f32 zoomLevel;
f32 offsetX;
f32 offsetY;
i32 windowWidth;
i32 windowHeight;
f32 minAbbe;
f32 maxAbbe;
f32 minRI;
f32 maxRI;
b32 showHelp; // Flag to show/hide help window
b32 gKeyPressed; // Flag to track if 'g' was pressed
u32 gKeyTime; // Time when 'g' was pressed for sequence timing
i32 selectedGlass; // Index of selected glass (-1 if none)
} ViewState;
// Initialize a view state with default values
void init_view_state(ViewState* view, i32 windowWidth, i32 windowHeight);
// Convert glass data to screen coordinates with zoom and offset
static inline void data_to_screen_coords(f32 abbeNumber, f32 refractiveIndex,
const ViewState* view, i32 *x, i32 *y) {
const i32 padding = (i32)(view->windowWidth * PADDING_PERCENT);
// Apply zoom and offset transformation
// FLIPPED: Use 1.0f - normalized to flip the Abbe number axis
f32 normalizedX = 1.0f - (abbeNumber - view->minAbbe) / (view->maxAbbe - view->minAbbe);
f32 normalizedY = (refractiveIndex - view->minRI) / (view->maxRI - view->minRI);
// Transform with zoom and offset
normalizedX = (normalizedX - 0.5f) * view->zoomLevel + 0.5f + view->offsetX;
normalizedY = (normalizedY - 0.5f) * view->zoomLevel + 0.5f + view->offsetY;
// Convert to screen coordinates
*x = padding + (i32)(normalizedX * (view->windowWidth - 2 * padding));
*y = view->windowHeight - padding - (i32)(normalizedY * (view->windowHeight - 2 * padding));
}
// Convert screen coordinates to data values
static inline void screen_to_data_coords(i32 x, i32 y, const ViewState* view,
f32 *abbeNumber, f32 *refractiveIndex) {
const i32 padding = (i32)(view->windowWidth * PADDING_PERCENT);
// Convert to normalized coordinates
f32 normalizedX = (f32)(x - padding) / (view->windowWidth - 2 * padding);
f32 normalizedY = (f32)(view->windowHeight - y - padding) / (view->windowHeight - 2 * padding);
// Reverse transform with zoom and offset
normalizedX = (normalizedX - view->offsetX - 0.5f) / view->zoomLevel + 0.5f;
normalizedY = (normalizedY - view->offsetY - 0.5f) / view->zoomLevel + 0.5f;
// Convert to data values - FLIPPED axis logic for Abbe
*abbeNumber = view->maxAbbe - normalizedX * (view->maxAbbe - view->minAbbe);
*refractiveIndex = view->minRI + normalizedY * (view->maxRI - view->minRI);
}
// Find the nearest glass to a given screen position
i32 find_nearest_glass(i32 x, i32 y, const ViewState* view, f32 maxDistance);
// Calculate visible data range based on current view
void get_visible_data_range(const ViewState* view, f32 *visibleMinAbbe, f32 *visibleMaxAbbe,
f32 *visibleMinRI, f32 *visibleMaxRI);
// Handle mouse wheel zoom
void handle_mouse_wheel_zoom(i32 wheelY, i32 mouseX, i32 mouseY, ViewState* view);
// Toggle fullscreen
void toggle_fullscreen(SDL_Window* window);
// Reset view to default
void reset_view(ViewState* view);
#endif /* GLAMAC_VIEW_H */
|