-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_events.c
133 lines (116 loc) · 3.87 KB
/
handle_events.c
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_events.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asepulve <asepulve@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/22 14:43:41 by asepulve #+# #+# */
/* Updated: 2024/08/13 00:25:31 by asepulve ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
int destroy_window(t_scr *scr)
{
mlx_destroy_image(scr->mlx, scr->img.fractol);
mlx_clear_window(scr->mlx, scr->win);
mlx_destroy_window(scr->mlx, scr->win);
mlx_destroy_display(scr->mlx);
free(scr->mlx);
exit(EXIT_SUCCESS);
}
/*
* Converts the coordenates to a vector.
*/
int mouse_handler(int button, int x, int y, t_scr *scr)
{
if (button == SCROLL_DOWN || button == SCROLL_UP)
event_handler(button, x - W_WIDTH / 2, W_HEIGHT / 2 - y, scr);
return (1);
}
int key_handler(int key, t_scr *scr)
{
float x_span = fabs(scr->stats.max_x - scr->stats.min_x);
float y_span = fabs(scr->stats.max_y - scr->stats.min_y);
if (scr->win == NULL)
return (0);
if (key == XK_Escape)
return (destroy_window(scr));
if (key == XK_Up || key == XK_Down || key == XK_Right || key == XK_Left
|| key == XK_KP_Subtract || key == XK_KP_Add
|| key == '1' || key == '2' || key == '3')
{
if (strchr("123", key))
scr->stats.color = key;
if (key == XK_KP_Add)
scr->stats.it += 10;
if (key == XK_KP_Subtract)
scr->stats.it -= 10;
if (key == XK_Up)
{
scr->stats.min_y += y_span / 10;
scr->stats.max_y = scr->stats.min_y + y_span;
}
if (key == XK_Down)
{
scr->stats.min_y -= y_span / 10;
scr->stats.max_y = scr->stats.min_y + y_span;
}
if (key == XK_Right)
{
scr->stats.min_x -= x_span / 10;
scr->stats.max_x = scr->stats.min_x + x_span;
}
if (key == XK_Left)
{
scr->stats.min_x += x_span / 10;
scr->stats.max_x = scr->stats.min_x + x_span;
}
render_img(scr);
mlx_put_image_to_window(scr->mlx, scr->win, scr->img.fractol, 0, 0);
}
return (1);
}
/*
* (x, y) is a vector, we are going change its proportions;
*/
void event_handler(int key, int x, int y, t_scr *scr)
{
if (x == 0 && y == 0)
{
render_img(scr);
mlx_put_image_to_window(scr->mlx, scr->win, scr->img.fractol, 0, 0);
return ;
}
printf("x: %d y: %d\n", x, y); // * I need to scale this x and y
float width = fabs(scr->stats.max_x - scr->stats.min_x);
float heigth = fabs(scr->stats.max_y - scr->stats.min_y);
if (key == SCROLL_DOWN)
{
scr->stats.min_x /= 1.1;
scr->stats.max_x /= 1.1;
scr->stats.min_y /= 1.1;
scr->stats.max_y /= 1.1;
}
if (key == SCROLL_UP)
{
scr->stats.min_x *= 1.1;
scr->stats.max_x *= 1.1;
scr->stats.min_y *= 1.1;
scr->stats.max_y *= 1.1;
}
float zoom_width = fabs(scr->stats.max_x - scr->stats.min_x);
float zoom_heigth = fabs(scr->stats.max_y - scr->stats.min_y);
float x_0 = x, y_0 = y;
// * x_0 and y_0 are the coordenates of the new point;
float rel_x = x_0 / width; // * calc the relative position of x
// * proportion rule applies in this scenario
float rel_y = y_0 / heigth; // * calc the relative position of y
// * Calc the coordenates of the visible area
scr->stats.min_x = x_0 - rel_x * zoom_width;
scr->stats.min_y = y_0 - rel_y * zoom_heigth;
scr->stats.max_x = scr->stats.min_x - width;
scr->stats.max_y = scr->stats.max_y - heigth;
render_img(scr);
mlx_put_image_to_window(scr->mlx, scr->win, scr->img.fractol, 0, 0);
}