1 module unde.viewers.image_viewer.events;
2 
3 import unde.global_state;
4 import unde.lib;
5 import unde.tick;
6 import unde.viewers.image_viewer.lib;
7 import unde.file_manager.events;
8 import unde.keybar.lib;
9 import unde.translations.lib;
10 
11 import derelict.sdl2.sdl;
12 
13 import std.stdio;
14 import std..string;
15 import std.functional;
16 
17 void rotate(GlobalState gs)
18 {
19     with (gs.image_viewer)
20     {
21         if (gs.shift)
22         {
23             rectsize.angle -= 90;
24             if (rectsize.angle <= -89)
25             {
26                 rectsize.angle = 270;
27             } 
28         }
29         else
30         {
31             rectsize.angle += 90;
32             if (rectsize.angle >= 359)
33             {
34                 rectsize.angle = 0;
35             } 
36         }
37 
38         rectsize.type = FileType.Image;
39         put_rectsize(gs);
40     }
41 }
42 
43 void process_event(GlobalState gs, ref SDL_Event event)
44 {
45     switch( event.type )
46     {
47         case SDL_MOUSEMOTION:
48             if (gs.mouse_buttons & unDE_MouseButtons.Left)
49             {
50                 with (gs.image_viewer)
51                 {
52                     if (texture_tick && texture_tick.texture)
53                     {
54                         rect.x += event.motion.xrel;
55                         rect.y += event.motion.yrel;
56                     }
57                 }
58             }
59             gs.mouse_screen_x = event.motion.x;
60             gs.mouse_screen_y = event.motion.y;
61             gs.moved_while_click++;
62             break;
63             
64         case SDL_MOUSEBUTTONDOWN:
65             switch (event.button.button)
66             {
67                 case SDL_BUTTON_LEFT:
68                     gs.mouse_buttons |= unDE_MouseButtons.Left;
69                     gs.moved_while_click = 0;
70                     break;
71                 case SDL_BUTTON_MIDDLE:
72                     gs.mouse_buttons |= unDE_MouseButtons.Middle;
73                     break;
74                 case SDL_BUTTON_RIGHT:
75                     gs.mouse_buttons |= unDE_MouseButtons.Right;
76                     break;
77                 default:
78                     break;
79             }
80             break;
81             
82         case SDL_MOUSEBUTTONUP:
83             if (gs.mouse_screen_x < gs.screen.w)
84             {
85                 switch (event.button.button)
86                 {
87                     case SDL_BUTTON_LEFT:
88                         gs.mouse_buttons &= ~unDE_MouseButtons.Left;
89                         if (!gs.moved_while_click)
90                         {
91                             if (SDL_GetTicks() - gs.last_left_click < DOUBLE_DELAY)
92                             {
93                                 gs.state = State.FileManager;
94                                 gs.dirty = true;
95                             }
96                             else
97                             {
98                             }
99                             gs.last_left_click = SDL_GetTicks();
100                         }
101                         break;
102                     case SDL_BUTTON_MIDDLE:
103                         gs.mouse_buttons &= ~unDE_MouseButtons.Middle;
104                         break;
105                     case SDL_BUTTON_RIGHT:
106                         gs.mouse_buttons &= ~unDE_MouseButtons.Right;
107 
108                         with (gs.image_viewer)
109                         {
110                             if (path in gs.selection_hash)
111                                 gs.selection_hash.remove(path);
112                             else
113                                 /* EN: FIXME: Sort type of the directory maybe other
114                                    RU: ИСПРАВЬ_МЕНЯ: Сортировка директории может быть другой */
115                                 gs.selection_hash[path] = rectsize.rect(SortType.BySize);
116                         }
117 
118                         break;
119                     default:
120                         break;
121                 }
122             }
123             break;
124 
125         case SDL_MOUSEWHEEL:
126             with (gs.image_viewer)
127             {
128                 if (texture_tick && texture_tick)
129                 {
130                     int mouse_center_x = gs.mouse_screen_x-rect.x;
131                     int mouse_center_y = gs.mouse_screen_y-rect.y;
132                     int oldw = rect.w;
133                     int oldh = rect.h;
134 
135                     while (event.wheel.y > 0)
136                     {
137                         rect.w = cast(int)(rect.w * 1.0905);
138                         rect.h = cast(int)(rect.h * 1.0905);
139                         event.wheel.y--;
140                     }
141 
142                     if (oldw > 20 && oldh > 20)
143                     {
144                         while (event.wheel.y < 0)
145                         {
146                             rect.w = cast(int)(rect.w / 1.0905);
147                             rect.h = cast(int)(rect.h / 1.0905);
148                             event.wheel.y++;
149                         }
150                     }
151 
152                     int mouse_center_x2 = mouse_center_x * rect.w/oldw;
153                     int mouse_center_y2 = mouse_center_y * rect.h/oldh;
154                     rect.x = gs.mouse_screen_x - mouse_center_x2;
155                     rect.y = gs.mouse_screen_y - mouse_center_y2;
156                 }
157             }
158             break;
159             
160         case SDL_QUIT:
161             /* Do something event.quit */
162             gs.finish=true;
163             break;
164             
165         default:
166             //writeln("Ignored event: "~to!string(event.type));
167             break;
168     }
169 }
170 
171 void
172 setup_keybar_imageviewer_default(GlobalState gs)
173 {
174     gs.keybar.handlers.clear();
175     gs.keybar.handlers_down.clear();
176     gs.keybar.handlers_double.clear();
177 
178     gs.keybar.handlers[SDL_SCANCODE_Q] = KeyHandler(toDelegate(&quit), _("Quit"), "exit.png");
179     gs.keybar.handlers[SDL_SCANCODE_PRINTSCREEN] = KeyHandler(toDelegate(&make_screenshot), _("Make screenshot"), "Prt Sc");
180     gs.keybar.handlers[SDL_SCANCODE_M] = KeyHandler(toDelegate(&mark), _("Make Mark"), "mark.png");
181     gs.keybar.handlers[SDL_SCANCODE_APOSTROPHE] = KeyHandler(toDelegate(&gomark), _("Go To Mark"), "gomark.png");
182     gs.keybar.handlers[SDL_SCANCODE_A] = KeyHandler(toDelegate(&deselect_all), _("Clear selection"), "deselect.png");
183     gs.keybar.handlers_down[SDL_SCANCODE_LSHIFT] = KeyHandler(toDelegate(&setup_keybar_imageviewer_shift), "", "Shift");
184     gs.keybar.handlers_down[SDL_SCANCODE_RSHIFT] = KeyHandler(toDelegate(&setup_keybar_imageviewer_shift), "", "");
185     gs.keybar.handlers[SDL_SCANCODE_LEFT] = KeyHandler(toDelegate(&image_prev), _("Next Image"), "←");
186     gs.keybar.handlers[SDL_SCANCODE_RIGHT] = KeyHandler(toDelegate(&image_next), _("Prev Image"), "→");
187     gs.keybar.handlers[SDL_SCANCODE_0] = KeyHandler(toDelegate(&setup_0_scale), _("Fit on the screen"), "0");
188     gs.keybar.handlers[SDL_SCANCODE_1] = KeyHandler(toDelegate(&setup_1_scale), _("100% Scale"), "100%");
189     gs.keybar.handlers[SDL_SCANCODE_R] = KeyHandler(toDelegate(&rotate), _("Rotate right"), "rotate_right.png");
190 }
191 
192 void
193 setup_keybar_imageviewer_shift(GlobalState gs)
194 {
195     gs.keybar.handlers.clear();
196     gs.keybar.handlers_down.clear();
197     gs.keybar.handlers_double.clear();
198 
199     gs.keybar.handlers[SDL_SCANCODE_M] = KeyHandler(toDelegate(&unmark), _("Delete Mark"), "unmark.png");
200     gs.keybar.handlers[SDL_SCANCODE_R] = KeyHandler(toDelegate(&rotate), _("Rotate left"), "rotate_left.png");
201     gs.keybar.handlers[SDL_SCANCODE_LSHIFT] = KeyHandler(toDelegate(&setup_keybar_imageviewer_default), "", "Shift");
202     gs.keybar.handlers[SDL_SCANCODE_RSHIFT] = KeyHandler(toDelegate(&setup_keybar_imageviewer_default), "", "");
203 }
204