lib: misc typos
[nit.git] / lib / gamnit / camera_control_linux.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Mouse wheel and middle mouse button to control camera
16 module camera_control_linux
17
18 import linux
19 import camera_control
20
21 redef class EulerCamera
22
23 # Zoom factor, default at 1.2, higher means more reactive zoom effect
24 var camera_zoom_mod = 1.2 is writable
25
26 # Scroll trigger button mask from SDL2 (1: left, 2: middle, 4: right)
27 #
28 # Set to 0 to deactivate the scrolling feature.
29 var camera_pan_mask = 2 is writable
30
31 redef fun accept_scroll_and_zoom(event)
32 do
33 # Zoom
34 if event isa GamnitMouseWheelEvent then
35 var dy = event.y
36 var mod = camera_zoom_mod
37 if dy > 0.0 then
38 # Zoom in when moving the wheel up
39 mod = 1.0/mod
40 else dy = -dy
41
42 position.z *= dy * mod
43 return true
44 end
45
46 # Scroll
47 var but_mask = camera_pan_mask
48 if but_mask != 0 and event isa GamnitPointerEvent then
49 var native = event.native
50 if native isa SDLMouseMotionEvent and native.state & but_mask == but_mask then
51 var dx = native.xrel.to_f
52 var dy = native.yrel.to_f
53
54 var world_height = field_of_view_y.sin * position.z
55 var mod = app.display.as(not null).height.to_f / world_height
56 position.x -= dx / mod
57 position.y += dy / mod # Y is inverted between the input and output
58 return true
59 end
60 end
61
62 return false
63 end
64 end