-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
79 lines (65 loc) · 2.13 KB
/
camera.py
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
from vec2 import Vec2
import pyglet
from pyglet.window import key
from pyglet.window import mouse
from pyglet.gl import *
import os
from collections import defaultdict
class Camera(object):
"""The editor 2D camera.
"""
def __init__(self, window):
"""Create a camera for the given window.
"""
self.window = window
self.pos = Vec2(0, 0)
self.speed = 2000.0
self._zoom = 1.0
self.zoom_speed = 1.2
## KeyState for camera movement
self.key_state = key.KeyStateHandler()
window.push_handlers(self.key_state)
pyglet.clock.schedule(self.check_input)
def _set_zoom(self, value):
self._zoom = value
if self._zoom < 0.01:
self._zoom = 0.01
def _get_zoom(self):
return self._zoom
zoom = property(_get_zoom, _set_zoom)
def transform(self):
"""Apply the camera transformation.
"""
half_win = Vec2(self.window.width/2, self.window.height/2)
t = (self.pos + half_win)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(half_win.x, half_win.y, 0.0)
glScalef(self.zoom, self.zoom, 1.0)
glTranslatef(-t.x, -t.y, 0.0)
def screen_to_world(self, x, y):
"""Converts from screen to world coordinates.
"""
half_win = Vec2(self.window.width/2, self.window.height/2)
spos = Vec2(x, y)
return ((spos - half_win) / self.zoom ) + (self.pos + half_win)
def check_input(self, dt):
"""Update method called at each frame.
"""
dx, dy = 0, 0
if self.key_state[key.UP]:
dy = 1
elif self.key_state[key.DOWN]:
dy = -1
if self.key_state[key.RIGHT]:
dx = 1
elif self.key_state[key.LEFT]:
dx = -1
if dx != 0 or dy != 0:
vel = Vec2(dx, dy).normalize() * self.speed * dt
self.pos = self.pos + vel
#print str(dt)
#if self.key_state[key.DOWN]:
# self.zoom -= self.zoom_speed * dt
#elif self.key_state[key.UP]:
# self.zoom += self.zoom_speed * dt