mirror of
https://github.com/cyberarm/i-mic-rts.git
synced 2025-12-14 07:12:34 +00:00
Implemented Zoom
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
class IMICRTS
|
||||
class Camera
|
||||
attr_reader :position, :velocity, :drag
|
||||
attr_reader :position, :velocity, :zoom, :drag
|
||||
def initialize(scroll_speed: 10, position: CyberarmEngine::Vector.new(0.0, 0.0))
|
||||
@scroll_speed = scroll_speed
|
||||
@position = position
|
||||
@velocity = CyberarmEngine::Vector.new(0.0, 0.0)
|
||||
|
||||
@drag = 0.8
|
||||
@zoom = 1.0
|
||||
@min_zoom = 0.25
|
||||
@max_zoom = 4.0
|
||||
|
||||
@drag = 0.8 # Used to arrest velocity
|
||||
@grab_drag = 0.5 # Used when camera is panned using middle mouse button
|
||||
end
|
||||
|
||||
def window; $window; end
|
||||
@@ -14,7 +19,10 @@ class IMICRTS
|
||||
def draw(&block)
|
||||
if block
|
||||
Gosu.translate(@position.x, @position.y) do
|
||||
block.call
|
||||
center_point = center
|
||||
Gosu.scale(@zoom, @zoom, center.x, center.y) do
|
||||
block.call
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -26,8 +34,18 @@ class IMICRTS
|
||||
@position += @velocity * @scroll_speed
|
||||
end
|
||||
|
||||
def mouse_pick(x, y)
|
||||
mouse = CyberarmEngine::Vector.new(x, y)
|
||||
|
||||
normalized = (mouse / @zoom - @position / @zoom) * @zoom
|
||||
normalized.x = normalized.x.floor
|
||||
normalized.y = normalized.y.floor
|
||||
|
||||
return normalized
|
||||
end
|
||||
|
||||
def center
|
||||
CyberarmEngine::Vector.new(window.width / 2, window.height / 2) - @position
|
||||
(CyberarmEngine::Vector.new(window.width / 2, window.height / 2) / @zoom - @position / @zoom) * @zoom
|
||||
end
|
||||
|
||||
def center_around(vector, factor)
|
||||
@@ -48,7 +66,7 @@ class IMICRTS
|
||||
|
||||
if @drag_start
|
||||
@velocity *= 0.0
|
||||
@position = CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y) - @drag_start
|
||||
@position = lerp(@position, CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y) - @drag_start, @grab_drag)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -57,9 +75,13 @@ class IMICRTS
|
||||
when Gosu::KB_H
|
||||
@position.x, @position.y = 0.0, 0.0
|
||||
@velocity *= 0.0
|
||||
when Gosu::MS_WHEEL_UP
|
||||
@zoom = (@zoom + 0.25).clamp(@min_zoom, @max_zoom)
|
||||
when Gosu::MS_WHEEL_DOWN
|
||||
@zoom = (@zoom - 0.25).clamp(@min_zoom, @max_zoom)
|
||||
when Gosu::MS_MIDDLE
|
||||
@position_start = @position.clone
|
||||
@drag_start = CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y) - @position
|
||||
@drag_start = CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user