-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCamera2D.gd
158 lines (132 loc) · 4.65 KB
/
Camera2D.gd
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
extends Camera2D
#export var panSpeed = 30.0
export var speed = 30.0
export var zoomspeed = 20.0
#export var zoommargin = 0.1 #technically never used
export var zoomMin = 0.25
export var zoomMax = 60.0 #3.0
#export var marginX = 200.0
#export var marginY = 200.0
var mousepos = Vector2()
var mouseposGlobal = Vector2()
var start = Vector2()
var startv = Vector2()
var end = Vector2()
var endv = Vector2()
var zoomfactor = 1.0
var zooming = false
var is_dragging = false
var move_to_point = Vector2()
onready var rectd = $'../UI/Base/draw_rect'
signal area_selected
signal start_move_selection
onready var zoom_in_button = preload("res://Other_Scenes/ZoomIn.tscn")
onready var zoom_out_button = preload("res://Other_Scenes/ZoomOut.tscn")
func _ready():
#connect("area_selected", get_parent(), "area_selected", [self])
#connect("start_move_selection", get_parent(), "start_move_selection", [self])
position.x = 0
position.y = 700
zoom.x = 2
zoom.y = zoom.x
var but3 = zoom_in_button.instance()
but3.connect_me(self)
var but4 = zoom_out_button.instance()
but4.connect_me(self)
func Up():
return(int(Input.is_action_pressed("ui_up")))
func Down():
return(int(Input.is_action_pressed("ui_down")))
func Left():
return(int(Input.is_action_pressed("ui_left")))
func Right():
return(int(Input.is_action_pressed("ui_right")))
func _process(delta):
#smooth movement
var inpx = ((Right()) - (Left()))
var inpy = ((Down()) - (Up()))
position.x = lerp(position.x, position.x + inpx *speed * zoom.x,speed * delta)
position.y = lerp(position.y, position.y + inpy *speed * zoom.y,speed * delta)
# if Input.is_key_pressed(KEY_CONTROL):
# #check mousepos
# if mousepos.x < marginX:
# position.x = lerp(position.x, position.x - abs(mousepos.x - marginX)/marginX * panSpeed * zoom.x, panSpeed * delta)
# elif mousepos.x > OS.window_size.x - marginX:
# position.x = lerp(position.x, position.x + abs(mousepos.x - OS.window_size.x + marginX)/marginX * panSpeed * zoom.x, panSpeed * delta)
# if mousepos.y < marginY:
# position.y = lerp(position.y, position.y - abs(mousepos.y - marginY)/marginY * panSpeed * zoom.y, panSpeed * delta)
# elif mousepos.y > OS.window_size.y - marginY:
# position.y = lerp(position.y, position.y + abs(mousepos.y - OS.window_size.y + marginY)/marginY * panSpeed * zoom.y, panSpeed * delta)
#
# if Input.is_action_just_pressed("ui_left_mouse_button"):
# start = mouseposGlobal
# startv = mousepos
# is_dragging = true
# if is_dragging:
# end = mouseposGlobal
# endv = mousepos
# draw_area()
# if Input.is_action_just_released("ui_left_mouse_button"):
# if startv.distance_to(mousepos) > 20:
# end = mouseposGlobal
# endv = mousepos
# is_dragging = false
# draw_area(false)
# emit_signal("area_selected")
# else:
# end = start
# is_dragging = false
# draw_area(false)
#
# if Input.is_action_just_released("ui_right_mouse_button"):
# move_to_point = mouseposGlobal
# emit_signal("start_move_selection")
#zoom in
zoom.x = lerp(zoom.x, zoom.x * zoomfactor, zoomspeed * delta)
zoom.y = lerp(zoom.y, zoom.y * zoomfactor, zoomspeed * delta)
zoom.x = clamp(zoom.x, zoomMin, zoomMax)
zoom.y = clamp(zoom.y, zoomMin, zoomMax)
if not zooming:
zoomfactor = 1.0
func draw_area(s = true):
rectd.rect_size = Vector2(abs(startv.x-endv.x), abs(startv.y - endv.y))
if startv.y <= endv.y and startv.x <= endv.x: # bottom right
rectd.rect_position = Vector2(startv.x, startv.y - OS.window_size.y)
elif startv.x >= endv.x and startv.y >= endv.y: # top left
rectd.rect_position = Vector2(endv.x, endv.y - OS.window_size.y)
elif startv.x >= endv.x and startv.y <= endv.y: # bottom left
rectd.rect_position = Vector2(endv.x, startv.y - OS.window_size.y)
elif startv.x <= endv.x and startv.y >= endv.y: # top right
rectd.rect_position = Vector2(startv.x, endv.y - OS.window_size.y)
var pos = Vector2()
pos.x = min(startv.x, endv.x)
pos.y = min(startv.y, endv.y)
pos.y -= OS.window_size.y
rectd.rect_position = pos
rectd.rect_size *= int(s) # true = 1 and false = 0
func _input(event):
if event is InputEvent:
if event.is_pressed():
zooming = true
if event.is_action_pressed("ui_plus"):
zoomIn()
if event.is_action_pressed("ui_minus"):
zoomOut()
else:
zooming = false
if event is InputEventMouseButton:
if event.is_pressed():
zooming = true
if event.button_index == BUTTON_WHEEL_UP:
zoomfactor -= 0.01 * zoomspeed
if event.button_index == BUTTON_WHEEL_DOWN:
zoomfactor += 0.01 * zoomspeed
else:
zooming = false
if event is InputEventMouse:
mousepos = event.position
mouseposGlobal = get_global_mouse_position()
func zoomIn():
zoomfactor -= 0.01 * zoomspeed
func zoomOut():
zoomfactor += 0.01 * zoomspeed