Rev 2097 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
964 | jag | 1 | /* |
2 | * viewwin.cc |
||
2302 | jag | 3 | * DIN Is Noise is copyright (c) 2006-2025 Jagannathan Sampath |
1479 | jag | 4 | * For more information, please visit https://dinisnoise.org/ |
964 | jag | 5 | */ |
6 | |||
7 | |||
8 | #include "dingl.h" |
||
9 | #include "viewwin.h" |
||
10 | #include "main.h" |
||
11 | #include "log.h" |
||
2075 | jag | 12 | #include <iostream> |
964 | jag | 13 | |
14 | using namespace std; |
||
15 | |||
16 | viewport::viewport () { |
||
17 | width = height = 0; |
||
18 | xmax = ymax = 0; |
||
19 | xmax_1 = ymax_1 = 0; |
||
20 | } |
||
21 | |||
22 | viewport::~viewport () { |
||
2075 | jag | 23 | dlog << "--- destroyed viewport: " << width << spc << height << " ---" << endl; |
964 | jag | 24 | } |
25 | |||
26 | void viewport::operator() (int w, int h) { |
||
2075 | jag | 27 | if (w != width || h != height) calc (w, h); |
964 | jag | 28 | } |
29 | |||
30 | void viewport::calc (int w, int h) { |
||
31 | width = w; |
||
32 | height = h; |
||
33 | xmax = width - 1; |
||
34 | ymax = height - 1; |
||
35 | xmax_1 = 1.0f / xmax; |
||
36 | ymax_1 = 1.0f / ymax; |
||
1374 | jag | 37 | midx = width / 2; |
38 | midy = height / 2; |
||
964 | jag | 39 | handle_factor = handle_radius * 1.0f / width; |
2075 | jag | 40 | dlog << "*** viewport changed to : " << w << spc << h << " ***" << endl; |
964 | jag | 41 | } |
42 | |||
43 | bool viewport::inside (int x, int y) { |
||
2075 | jag | 44 | int xi = (x > -1) && (x < width); |
45 | int yi = (y > -1) && (y < height); |
||
964 | jag | 46 | return (xi && yi); |
47 | } |
||
48 | |||
49 | void window::calc_panxy () { |
||
50 | pan_x = PAN_AMOUNT * width; |
||
51 | pan_y = PAN_AMOUNT * height; |
||
52 | } |
||
53 | |||
54 | void window::calc_handle_radius () { |
||
55 | handle_radius = viewport::handle_factor * width; |
||
56 | handle_radius2 = handle_radius * handle_radius; |
||
57 | sus_handle_radius = 2 * handle_radius; |
||
58 | } |
||
59 | |||
60 | window::window () : box<float>() { |
||
61 | width_1 = height_1 = 0; |
||
62 | aspect_ratio = 0; |
||
63 | } |
||
64 | |||
65 | window::window (float l, float b, float r, float t) { |
||
66 | set (l, b, r, t); |
||
67 | } |
||
68 | |||
69 | void window::set (float l, float b, float r, float t) { |
||
70 | operator() (l, b, r, t); |
||
71 | aspect_ratio = width * height_1; |
||
72 | calc_panxy (); |
||
73 | calc_viewwin (); |
||
74 | calc_handle_radius (); |
||
75 | window::calc_repeats (); |
||
76 | } |
||
77 | |||
78 | void window::get (float& l, float& b, float& r, float& t) { |
||
79 | l = left; |
||
80 | b = bottom; |
||
81 | r = right; |
||
82 | t = top; |
||
83 | } |
||
84 | |||
85 | void window::panx (int dir) { |
||
86 | float dpan = pan_x * dir; |
||
87 | left = left + dpan; |
||
88 | right = left + width; |
||
89 | mousex += dpan; |
||
90 | } |
||
91 | |||
92 | void window::pany (int dir) { |
||
93 | float dpan = pan_y * dir; |
||
94 | bottom = bottom + pan_y * dir; |
||
95 | top = bottom + height; |
||
96 | mousey += dpan; |
||
97 | } |
||
98 | |||
99 | void window::zoom (int dir) { |
||
100 | float dw = ZOOM_AMOUNT * width * dir, dh = dw / aspect_ratio, dw2 = dw / 2, dh2 = dh / 2; |
||
101 | float l = left - dw2; |
||
102 | float r = right + dw2; |
||
103 | float b = bottom - dh2; |
||
104 | float t = top + dh2; |
||
105 | float w = r - l, h = t - b; |
||
106 | if ((w > 0) && (h > 0)) set (l, b, r, t); |
||
107 | } |
||
108 | |||
109 | void window::locate_mouse () { |
||
110 | extern viewport view; |
||
111 | extern int mousex, mousey; |
||
112 | view2win (mousex, mousey, window::mousex, window::mousey, view, *this); |
||
113 | prev_mousex = mousex; |
||
114 | prev_mousey = mousey; |
||
115 | } |
||
116 | |||
117 | void window::update_mouse () { |
||
118 | extern int mousex, mousey; |
||
119 | mousex_prev = window::mousex; |
||
120 | mousey_prev = window::mousey; |
||
121 | window::mousex += (mousex - prev_mousex) * vwx; |
||
122 | window::mousey -= (mousey - prev_mousey) * vwy; |
||
123 | prev_mousex = mousex; |
||
124 | prev_mousey = mousey; |
||
125 | } |
||
126 | |||
127 | int window::diff_mouse (float& dx, float& dy) { |
||
128 | dx = mousex - mousex_prev; |
||
129 | dy = mousey - mousey_prev; |
||
130 | if (dx || dy) return 1; else return 0; |
||
131 | } |
||
132 | |||
133 | void window::calc_viewwin () { |
||
134 | extern viewport view; |
||
135 | vwx = width / view.xmax; |
||
136 | vwy = height / view.ymax; |
||
137 | locate_mouse (); |
||
138 | } |
||
139 | |||
140 | void window::calc_repeats () { |
||
141 | if (PAN_RATE > 0) PAN_REPEAT = 1. / PAN_RATE; |
||
142 | if (ZOOM_RATE > 0) ZOOM_REPEAT = 1. / ZOOM_RATE; |
||
143 | } |
||
144 | |||
145 | void window::calc () { |
||
146 | box<float>::calc (); |
||
147 | calc_repeats (); |
||
148 | calc_panxy (); |
||
149 | calc_viewwin (); |
||
150 | calc_handle_radius (); |
||
151 | } |
||
152 | |||
153 | void view2win (int vx, int vy, float& wx, float& wy, viewport& view, window& win) { |
||
154 | float xr = vx * view.xmax_1; |
||
155 | float yr = 1 - vy * view.ymax_1; |
||
156 | wx = win.left + xr * win.width; |
||
157 | wy = win.bottom + yr * win.height; |
||
158 | } |
||
159 | |||
160 | void win2view (float wx, float wy, int& vx, int& vy, window& win, viewport& view) { |
||
161 | float xr = (wx - win.left) * win.width_1; |
||
162 | float yr = (wy - win.bottom) * win.height_1; |
||
163 | vx = (int) (xr * view.xmax + 0.5); |
||
164 | vy = (int) (yr * view.ymax + 0.5); |
||
165 | } |