Exemplo n.º 1
0
    def on_update(self, dt):
        """ Center the camera on target  """
        if not self._track_target:
            return

        # -- clamp target to camera bounds
        sx, sy = self.size / 2
        px, py = self._track_target.position
        position = Vec2(
            clamp(px, self.bounds.left + sx, self.bounds.right - sx),
            clamp(py, self.bounds.bottom + sy, self.bounds.top - sy),
        )

        # -- set camera offset (distance from center of camera to target_pos)
        self.offset = self._size / 2 - position

        # -- move the camera steadily to reduce offset
        epsilon = 2.0
        dist = self.offset.get_distance(self._position)
        norm = (self.offset - self._position).normalized()
        if dist >= epsilon:
            # XXX NOTE: dist is added to speed to prevent camera from lagging behind
            self._position += norm * dt * (self.speed + dist)

        pg.gl.glMatrixMode(pg.gl.GL_MODELVIEW)
        pg.gl.glLoadIdentity()
        pg.gl.glTranslatef(*self._position, 0)
        pg.gl.glScalef(*self._scale, 1)
Exemplo n.º 2
0
    def on_mouse_scroll(self, x, y, sx, sy):
        if not mouse_over_rect((x, y), *self.get_rect()):
            return

        # -- update active level based on scroll
        self.active_level = clamp(self.active_level + sy, 0,
                                  len(self.tabs) - 1)
        self.switch_level(self.tabs[self.active_level].text)
Exemplo n.º 3
0
    def zoom_camera(self, dy):
        # TODO minimum and maximum zoom levels
        # TODO user-configurable scale_factor (or not???)

        dy = clamp(dy, -1.1, 1.1)
        # prevent view from transforming weirdly (i.e. flipping upside down)
        if dy < 0:
            dy = 1 / -dy
        self.scale(dy, dy)
Exemplo n.º 4
0
    def on_mouse_scroll(self, x, y, sx, sy):
        if not mouse_over_rect((x, y), *self.get_rect()):
            return

        _sum = lambda x, y, val: (x + val, y + val)
        if sy < 0:
            self._zoom = _sum(*self._zoom, -self._zoom_sensitivity)
        else:
            self._zoom = _sum(*self._zoom, self._zoom_sensitivity)

        # -- clamp zoom to (0.2, 10.0) and round to  d.p
        self._zoom = tuple(map(lambda x: clamp(x, 0.2, 10.0), self._zoom))
        self._zoom = tuple(map(lambda x: round(x, 1), self._zoom))
Exemplo n.º 5
0
def test_clamp_maximum():
    assert clamp(2, 0, 1) == 1
    assert clamp(5, -2, -1) == -1
    assert clamp(10, 6, 8) == 8
Exemplo n.º 6
0
def test_clamp_minimum():
    assert clamp(-1, 0, 1) == 0
    assert clamp(-5, -2, -1) == -2
    assert clamp(2, 4, 8) == 4
Exemplo n.º 7
0
def test_clamp_untouched():
    assert clamp(0.5, 0, 1) == 0.5
    assert clamp(-2, -4, 1) == -2