libxputty 0.1
Loading...
Searching...
No Matches
xadjustment.c
Go to the documentation of this file.
1/*
2 * 0BSD
3 *
4 * BSD Zero Clause License
5 *
6 * Copyright (c) 2019 Hermann Meyer
7 *
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted.
10
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 *
19 */
20
21#include "xadjustment.h"
22#include "xadjustment_private.h"
23
24
25Adjustment_t *add_adjustment(Widget_t *w, float std_value, float value,
26 float min_value,float max_value, float step, CL_type type) {
27 Adjustment_t *adj = (Adjustment_t*)malloc(sizeof(Adjustment_t));
28 assert(adj);
29 adj->log_scale = 20.0;
30
31 switch(type) {
32 case (CL_LOGSCALE) :
33 *(adj) = (Adjustment_t) {
34 .w = w,
35 .std_value = powf(10,(std_value/adj->log_scale)),
36 .value = powf(10,(value/adj->log_scale)),
37 .min_value = powf(10,(min_value/adj->log_scale)),
38 .max_value = powf(10,(max_value/adj->log_scale)),
39 .step = step,
40 .start_value = powf(10,(value/adj->log_scale)),
41 .scale = 1.0,
42 .type = type,
43 .log_scale = adj->log_scale
44 };
45 break;
46 case (CL_LOGARITHMIC) :
47 *(adj) = (Adjustment_t) {
48 .w = w,
49 .std_value = log10(std_value),
50 .value = log10(value),
51 .min_value = log10(min_value),
52 .max_value = log10(max_value),
53 .step = step,
54 .start_value = log10(value),
55 .scale = 1.0,
56 .type = type,
57 .log_scale = adj->log_scale
58 };
59 break;
60 default:
61 *(adj) = (Adjustment_t) {
62 .w = w,
63 .std_value = std_value,
64 .value = value,
65 .min_value = min_value,
66 .max_value = max_value,
67 .step = step,
68 .start_value = value,
69 .scale = 1.0,
70 .type = type,
71 .log_scale = adj->log_scale
72 };
73 break;
74 }
75
76 debug_print("Widget_t add adjustment\n");
77 return adj;
78}
79
80void set_adjustment(Adjustment_t *adj, float std_value, float value,
81 float min_value,float max_value, float step, CL_type type) {
82 if (!adj) adj = (Adjustment_t*)malloc(sizeof(Adjustment_t));
83 assert(adj);
84
85 switch(type) {
86 case (CL_LOGSCALE) :
87 *(adj) = (Adjustment_t) {
88 .w = adj->w,
89 .std_value = powf(10,(std_value/adj->log_scale)),
90 .value = powf(10,(value/adj->log_scale)),
91 .min_value = powf(10,(min_value/adj->log_scale)),
92 .max_value = powf(10,(max_value/adj->log_scale)),
93 .step = step,
94 .start_value = powf(10,(value/adj->log_scale)),
95 .scale = 1.0,
96 .type = type,
97 .log_scale = adj->log_scale
98 };
99 break;
100 case (CL_LOGARITHMIC) :
101 *(adj) = (Adjustment_t) {
102 .w = adj->w,
103 .std_value = log10(std_value),
104 .value = log10(value),
105 .min_value = log10(min_value),
106 .max_value = log10(max_value),
107 .step = step,
108 .start_value = log10(value),
109 .scale = 1.0,
110 .type = type,
111 .log_scale = adj->log_scale
112 };
113 break;
114 default:
115 *(adj) = (Adjustment_t) {
116 .w = adj->w,
117 .std_value = std_value,
118 .value = value,
119 .min_value = min_value,
120 .max_value = max_value,
121 .step = step,
122 .start_value = value,
123 .scale = 1.0,
124 .type = type,
125 .log_scale = adj->log_scale
126 };
127 break;
128 }
129
130 debug_print("Widget_t set adjustment\n");
131}
132
134 if(adj) {
135 free(adj);
136 debug_print("Widget_t delete adjustment\n");
137 }
138 return NULL;
139}
140
141void adj_set_state(Adjustment_t *adj, float state) {
142 if (!adj) return;
143 float nvalue = min(1.0,max(0.0,state));
144 float value = nvalue * (adj->max_value - adj->min_value) + adj->min_value;
145 check_value_changed(adj, &value);
146}
147
149 if (!adj) return 0.0;
150 return (adj->value - adj->min_value) /
151 (adj->max_value - adj->min_value);
152}
153
155 if (!adj) return 0.0;
156 if (adj->type == CL_LOGSCALE)
157 return log10(adj->value)*adj->log_scale;
158 else if (adj->type == CL_LOGARITHMIC)
159 return powf(10,adj->value);
160 return (adj->value);
161}
162
164 if (!adj) return 0.0;
165 if (adj->type == CL_LOGSCALE)
166 return log10(adj->std_value)*adj->log_scale;
167 else if (adj->type == CL_LOGARITHMIC)
168 return powf(10,adj->std_value);
169 return (adj->std_value);
170}
171
173 if (!adj) return 0.0;
174 if (adj->type == CL_LOGSCALE)
175 return log10(adj->min_value)*adj->log_scale;
176 else if (adj->type == CL_LOGARITHMIC)
177 return powf(10,adj->min_value);
178 return (adj->min_value);
179}
180
182 if (!adj) return 0.0;
183 if (adj->type == CL_LOGSCALE)
184 return log10(adj->max_value)*adj->log_scale;
185 else if (adj->type == CL_LOGARITHMIC)
186 return powf(10,adj->max_value);
187 return (adj->max_value);
188}
189
190void adj_set_value(Adjustment_t *adj, float v) {
191 if (!adj) return;
192 if (adj->type == CL_LOGSCALE)
193 v = powf(10,(v/adj->log_scale));
194 else if (adj->type == CL_LOGARITHMIC)
195 v = log10(v);
196 v = min(adj->max_value,max(adj->min_value, v));
197 check_value_changed(adj, &v);
198}
199
200void adj_set_min_value(Adjustment_t *adj, float v) {
201 if (!adj) return;
202 if (adj->type == CL_LOGSCALE)
203 v = powf(10,(v/adj->log_scale));
204 else if (adj->type == CL_LOGARITHMIC)
205 v = log10(v);
206 adj->min_value = v;
207}
208
209void adj_set_max_value(Adjustment_t *adj, float v) {
210 if (!adj) return;
211 if (adj->type == CL_LOGSCALE)
212 v = powf(10,(v/adj->log_scale));
213 else if (adj->type == CL_LOGARITHMIC)
214 v = log10(v);
215 adj->max_value = v;
216}
217
218void adj_set_std_value(Adjustment_t *adj, float v) {
219 if (!adj) return;
220 if (adj->type == CL_LOGSCALE)
221 v = powf(10,(v/adj->log_scale));
222 else if (adj->type == CL_LOGARITHMIC)
223 v = log10(v);
224 adj->std_value = v;
225}
226
227void adj_set_start_value(void *w) {
228 Widget_t * wid = (Widget_t*)w;
229 if(wid->adj_x)wid->adj_x->start_value = wid->adj_x->value;
230 if(wid->adj_y)wid->adj_y->start_value = wid->adj_y->value;
231}
232
233void adj_set_scale(Adjustment_t *adj, float value) {
234 adj->scale = value;
235}
236
237void adj_set_log_scale(Adjustment_t *adj, float value) {
238 adj->log_scale = value;
239}
240
241void adj_set_motion_state(void *w, float x, float y) {
242 Widget_t * wid = (Widget_t*)w;
243 if(wid->adj_x) {
244 float value= wid->adj_x->value;
245 switch(wid->adj_x->type) {
246 case (CL_LOGSCALE):
247 case (CL_LOGARITHMIC):
248 case (CL_CONTINUOS):
249 {
250 float state = (wid->adj_x->start_value - wid->adj_x->min_value) /
251 (wid->adj_x->max_value - wid->adj_x->min_value);
252 float nsteps = wid->adj_x->step / (wid->adj_x->max_value - wid->adj_x->min_value);
253 float nvalue = min(1.0,max(0.0,state + ((float)(x - wid->pos_x)*wid->adj_x->scale *nsteps)));
254 float prevalue = nvalue * (wid->adj_x->max_value - wid->adj_x->min_value) + wid->adj_x->min_value;
255 float mulscale = round(prevalue/wid->adj_x->step);
256 value = min(wid->adj_x->max_value,max(wid->adj_x->min_value,mulscale*wid->adj_x->step));
257 }
258 break;
259 case (CL_VIEWPORTSLIDER):
260 {
261 float state = (wid->adj_x->start_value - wid->adj_x->min_value) /
262 (wid->adj_x->max_value - wid->adj_x->min_value);
263 float nsteps = wid->adj_x->step / (wid->adj_x->max_value - wid->adj_x->min_value);
264 float nvalue = min(1.0,max(0.0,state - ((float)(x - wid->pos_x)*wid->adj_x->scale *nsteps)));
265 float prevalue = nvalue * (wid->adj_x->max_value - wid->adj_x->min_value) + wid->adj_x->min_value;
266 float mulscale = round(prevalue/wid->adj_x->step);
267 value = min(wid->adj_x->max_value,max(wid->adj_x->min_value,mulscale*wid->adj_x->step));
268 }
269 break;
270 case (CL_TOGGLE):
271 // dont toggle on motion!
272 // value = wid->adj_x->value ? 0.0 : 1.0;
273 break;
274 default:
275 break;
276 }
277 check_value_changed(wid->adj_x, &value);
278 }
279 if(wid->adj_y) {
280 float value = wid->adj_y->value;
281 switch(wid->adj_y->type) {
282 case (CL_LOGSCALE):
283 case (CL_LOGARITHMIC):
284 case (CL_CONTINUOS):
285 {
286 float state = (wid->adj_y->start_value - wid->adj_y->min_value) /
287 (wid->adj_y->max_value - wid->adj_y->min_value);
288 float nsteps = wid->adj_y->step / (wid->adj_y->max_value - wid->adj_y->min_value);
289 float nvalue = min(1.0,max(0.0,state + ((float)(wid->pos_y - y)*wid->adj_y->scale *nsteps)));
290 float prevalue = nvalue * (wid->adj_y->max_value - wid->adj_y->min_value) + wid->adj_y->min_value;
291 float mulscale = round(prevalue/wid->adj_y->step);
292 value = min(wid->adj_y->max_value,max(wid->adj_y->min_value,mulscale*wid->adj_y->step));
293 }
294 break;
295 case (CL_VIEWPORTSLIDER):
296 {
297 float state = (wid->adj_y->start_value - wid->adj_y->min_value) /
298 (wid->adj_y->max_value - wid->adj_y->min_value);
299 float nsteps = wid->adj_y->step / (wid->adj_y->max_value - wid->adj_y->min_value);
300 float nvalue = min(1.0,max(0.0,state - ((float)(wid->pos_y - y)*wid->adj_y->scale *nsteps)));
301 float prevalue = nvalue * (wid->adj_y->max_value - wid->adj_y->min_value) + wid->adj_y->min_value;
302 float mulscale = round(prevalue/wid->adj_y->step);
303 value = min(wid->adj_y->max_value,max(wid->adj_y->min_value,mulscale*wid->adj_y->step));
304 }
305 break;
306 case (CL_TOGGLE):
307 // dont toggle on motion!
308 // value = wid->adj_y->value ? 0.0 : 1.0;
309 break;
310 default:
311 break;
312 }
313 check_value_changed(wid->adj_y, &value);
314 }
315}
316
317void check_value_changed(Adjustment_t *adj, float *value) {
318 debug_print("Adjustment_t check_value_changed %f\n", *(value));
319 if(fabs(*(value) - adj->value)>=0.00001) {
320 adj->value = *(value);
321 adj->w->func.adj_callback(adj->w, NULL);
322 adj->w->func.value_changed_callback(adj->w, value);
323 }
324}
Adjustment_t - struct to hold a controller adjustment.
Definition xadjustment.h:82
float std_value
Definition xadjustment.h:86
Widget_t * w
Definition xadjustment.h:84
float min_value
Definition xadjustment.h:90
float max_value
Definition xadjustment.h:92
float start_value
Definition xadjustment.h:96
xevfunc adj_callback
Definition xwidget.h:89
xevfunc value_changed_callback
Definition xwidget.h:90
Widget_t - struct to hold the basic Widget_t info.
Definition xwidget.h:457
Resize_t scale
Definition xwidget.h:525
Adjustment_t * adj_y
Definition xwidget.h:495
Adjustment_t * adj_x
Definition xwidget.h:493
int pos_y
Definition xwidget.h:515
int pos_x
Definition xwidget.h:513
Func_t func
Definition xwidget.h:481
void * delete_adjustment(Adjustment_t *adj)
delete_adjustment - freeing the memory of the Adjustment_t You usually don't need to call this,...
void adj_set_log_scale(Adjustment_t *adj, float value)
adj_set_log_scale - internal use to set the logarithmic scale
void check_value_changed(Adjustment_t *adj, float *value)
check_value_changed - check if Adjustment_t value have changed and send value_changed_callback (VALUE...
void adj_set_motion_state(void *w, float x, float y)
adj_set_motion_state - internal use to set value and state of the Adjustment_t on mouse pointer movme...
float adj_get_max_value(Adjustment_t *adj)
adj_get_max_value - get the maximal value of the Adjustment_t
void adj_set_state(Adjustment_t *adj, float state)
adj_set_state - set the current state of the Adjustment_t
float adj_get_std_value(Adjustment_t *adj)
adj_get_std_value - get the standart value of the Adjustment_t
void adj_set_max_value(Adjustment_t *adj, float v)
adj_set_max_value - set the maximal value to the Adjustment_t
void adj_set_std_value(Adjustment_t *adj, float v)
adj_set_std_value - set the standart value to the Adjustment_t
float adj_get_value(Adjustment_t *adj)
adj_get_value - get the current value of the Adjustment_t
void adj_set_min_value(Adjustment_t *adj, float v)
adj_set_min_value - set the minimal value to the Adjustment_t
void adj_set_start_value(void *w)
adj_set_start_value - internal use to store the value when pointer movment starts
Adjustment_t * add_adjustment(Widget_t *w, float std_value, float value, float min_value, float max_value, float step, CL_type type)
*add_adjustment - adding a adjustment to a Widget_t
Definition xadjustment.c:25
void set_adjustment(Adjustment_t *adj, float std_value, float value, float min_value, float max_value, float step, CL_type type)
*set_adjustment - set a new range to a existing Adjustment_t or create if it not exists yet
Definition xadjustment.c:80
float adj_get_min_value(Adjustment_t *adj)
adj_get_min_value - get the minimal value of the Adjustment_t
float adj_get_state(Adjustment_t *adj)
adj_get_state - get the current state of the Adjustment_t
void adj_set_scale(Adjustment_t *adj, float value)
adj_set_scale - internal use to scale the pointer movement (0.1 -1.0)
void adj_set_value(Adjustment_t *adj, float v)
adj_set_value - set the current value to the Adjustment_t
CL_type
CL_type - define the type of the controller adjustment one of this types must be given when set up a ...
Definition xadjustment.h:45
@ CL_TOGGLE
Definition xadjustment.h:51
@ CL_CONTINUOS
Definition xadjustment.h:49
@ CL_LOGARITHMIC
Definition xadjustment.h:61
@ CL_LOGSCALE
Definition xadjustment.h:63
@ CL_VIEWPORTSLIDER
Definition xadjustment.h:65