Skip to content

Commit f07c709

Browse files
committed
Improve window management demo
1 parent ac88236 commit f07c709

File tree

5 files changed

+57
-92
lines changed

5 files changed

+57
-92
lines changed

misc/window_management/control.gd

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
extends Control
22

3-
# Member variables
43
var mousepos
54

65
onready var observer = $"../Observer"
76

7+
func _ready():
8+
if not check_wm_api():
9+
set_physics_process(false)
10+
set_process_input(false)
11+
12+
813
func _physics_process(_delta):
914
var modetext = "Mode:\n"
10-
1115
if OS.is_window_fullscreen():
1216
modetext += "Fullscreen\n"
1317
else:
1418
modetext += "Windowed\n"
15-
1619
if !OS.is_window_resizable():
1720
modetext += "FixedSize\n"
18-
1921
if OS.is_window_minimized():
2022
modetext += "Minimized\n"
21-
2223
if OS.is_window_maximized():
2324
modetext += "Maximized\n"
24-
2525
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
2626
modetext += "MouseGrab\n"
2727
$Label_MouseModeCaptured_KeyInfo.show()
@@ -63,56 +63,57 @@ func _physics_process(_delta):
6363
$Button_MouseModeCaptured.set_pressed(Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED)
6464

6565

66+
func _input(event):
67+
if event is InputEventMouseMotion:
68+
mousepos = event.position
69+
70+
if event is InputEventKey:
71+
if Input.is_action_pressed("mouse_mode_visible"):
72+
observer.state = observer.STATE_MENU
73+
_on_Button_MouseModeVisible_pressed()
74+
75+
if Input.is_action_pressed("mouse_mode_hidden"):
76+
observer.state = observer.STATE_MENU
77+
_on_Button_MouseModeHidden_pressed()
78+
79+
if Input.is_action_pressed("mouse_mode_captured"):
80+
_on_Button_MouseModeCaptured_pressed()
81+
82+
6683
func check_wm_api():
6784
var s = ""
6885
if !OS.has_method("get_screen_count"):
6986
s += " - get_screen_count()\n"
70-
7187
if !OS.has_method("get_current_screen"):
7288
s += " - get_current_screen()\n"
73-
7489
if !OS.has_method("set_current_screen"):
7590
s += " - set_current_screen()\n"
76-
7791
if !OS.has_method("get_screen_position"):
7892
s += " - get_screen_position()\n"
79-
8093
if !OS.has_method("get_screen_size"):
8194
s += " - get_screen_size()\n"
82-
8395
if !OS.has_method("get_window_position"):
8496
s += " - get_window_position()\n"
85-
8697
if !OS.has_method("set_window_position"):
8798
s += " - set_window_position()\n"
88-
8999
if !OS.has_method("get_window_size"):
90100
s += " - get_window_size()\n"
91-
92101
if !OS.has_method("set_window_size"):
93102
s += " - set_window_size()\n"
94-
95103
if !OS.has_method("set_window_fullscreen"):
96104
s += " - set_window_fullscreen()\n"
97-
98105
if !OS.has_method("is_window_fullscreen"):
99106
s += " - is_window_fullscreen()\n"
100-
101107
if !OS.has_method("set_window_resizable"):
102108
s += " - set_window_resizable()\n"
103-
104109
if !OS.has_method("is_window_resizable"):
105110
s += " - is_window_resizable()\n"
106-
107111
if !OS.has_method("set_window_minimized"):
108112
s += " - set_window_minimized()\n"
109-
110113
if !OS.has_method("is_window_minimized"):
111114
s += " - is_window_minimized()\n"
112-
113115
if !OS.has_method("set_window_maximized"):
114116
s += " - set_window_maximized()\n"
115-
116117
if !OS.has_method("is_window_maximized"):
117118
s += " - is_window_maximized()\n"
118119

@@ -124,29 +125,6 @@ func check_wm_api():
124125
return false
125126

126127

127-
func _ready():
128-
if not check_wm_api():
129-
set_physics_process(false)
130-
set_process_input(false)
131-
132-
133-
func _input(event):
134-
if event is InputEventMouseMotion:
135-
mousepos = event.position
136-
137-
if event is InputEventKey:
138-
if Input.is_action_pressed("mouse_mode_visible"):
139-
observer.state = observer.STATE_MENU
140-
_on_Button_MouseModeVisible_pressed()
141-
142-
if Input.is_action_pressed("mouse_mode_hidden"):
143-
observer.state = observer.STATE_MENU
144-
_on_Button_MouseModeHidden_pressed()
145-
146-
if Input.is_action_pressed("mouse_mode_captured"):
147-
_on_Button_MouseModeCaptured_pressed()
148-
149-
150128
func _on_Button_MoveTo_pressed():
151129
OS.set_window_position(Vector2(100, 100))
152130

@@ -200,4 +178,5 @@ func _on_Button_MouseModeHidden_pressed():
200178

201179

202180
func _on_Button_MouseModeCaptured_pressed():
181+
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
203182
observer.state = observer.STATE_GRAB
Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,42 @@
11
extends KinematicBody
22

3-
# Constants
43
const STATE_MENU = 0
54
const STATE_GRAB = 1
65

7-
# Member variables
86
var r_pos = Vector2()
97
var state = STATE_MENU
108

9+
onready var camera = $Camera
1110

12-
func direction(vector):
13-
var v = $Camera.get_global_transform().basis * vector
14-
v = v.normalized()
15-
return v
16-
17-
18-
func _physics_process(delta):
19-
if (state != STATE_GRAB):
11+
func _process(delta):
12+
if state != STATE_GRAB:
2013
return
21-
22-
if (Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED):
23-
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
24-
25-
var dir = Vector3()
26-
if (Input.is_action_pressed("move_forward")):
27-
dir += direction(Vector3(0, 0, -1))
28-
if (Input.is_action_pressed("move_backwards")):
29-
dir += direction(Vector3(0, 0, 1))
30-
if (Input.is_action_pressed("move_left")):
31-
dir += direction(Vector3(-1, 0, 0))
32-
if (Input.is_action_pressed("move_right")):
33-
dir += direction(Vector3(1, 0, 0))
34-
35-
dir = dir.normalized()
36-
37-
move_and_collide(dir * 10 * delta)
38-
var d = delta * 0.1
39-
40-
# set yaw
41-
rotate(Vector3(0, 1, 0), d*r_pos.x)
42-
43-
# set pitch
44-
var pitch = $Camera.get_transform().rotated(Vector3(1, 0, 0), d * r_pos.y)
45-
$Camera.set_transform(pitch)
46-
47-
r_pos = Vector2()
14+
15+
var x_movement = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
16+
var z_movement = Input.get_action_strength("move_backwards") - Input.get_action_strength("move_forward")
17+
var dir = direction(Vector3(x_movement, 0, z_movement))
18+
transform.origin += dir * 10 * delta
19+
20+
var d = delta * 0.1 # Scale the input, easiest to do by scaling the delta.
21+
rotate(Vector3.UP, d * r_pos.x) # Yaw
22+
camera.transform = camera.transform.rotated(Vector3.RIGHT, d * r_pos.y) # Pitch
23+
24+
r_pos = Vector2.ZERO # We've dealt with all the input, so set it to zero.
4825

4926

5027
func _input(event):
5128
if (event is InputEventMouseMotion):
5229
r_pos = -event.relative
53-
30+
5431
if (event.is_action("ui_cancel") and event.is_pressed() and !event.is_echo()):
5532
if (state == STATE_GRAB):
5633
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
5734
state = STATE_MENU
5835
else:
5936
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
6037
state = STATE_GRAB
38+
39+
40+
func direction(vector):
41+
var v = camera.get_global_transform().basis * vector
42+
return v.normalized()

misc/window_management/observer/observer.tscn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ near = 0.1
1616
far = 1000.0
1717

1818
[node name="OmniLight" type="OmniLight" parent="."]
19-
19+
omni_range = 8.0

misc/window_management/project.godot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,24 @@ mouse_mode_visible={
5151
move_backwards={
5252
"deadzone": 0.5,
5353
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":83,"unicode":0,"echo":false,"script":null)
54+
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777234,"unicode":0,"echo":false,"script":null)
5455
]
5556
}
5657
move_forward={
5758
"deadzone": 0.5,
5859
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"unicode":0,"echo":false,"script":null)
60+
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"unicode":0,"echo":false,"script":null)
5961
]
6062
}
6163
move_left={
6264
"deadzone": 0.5,
6365
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null)
66+
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"unicode":0,"echo":false,"script":null)
6467
]
6568
}
6669
move_right={
6770
"deadzone": 0.5,
6871
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"unicode":0,"echo":false,"script":null)
72+
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"unicode":0,"echo":false,"script":null)
6973
]
7074
}

misc/window_management/window_management.tscn

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ albedo_color = Color( 0.835294, 0.133333, 0.133333, 1 )
1010
material = SubResource( 1 )
1111
size = Vector3( 5, 5, 5 )
1212

13-
[node name="Spatial" type="Spatial"]
13+
[node name="WindowManagement" type="Spatial"]
1414

1515
[node name="Observer" parent="." instance=ExtResource( 1 )]
1616
transform = Transform( 0.910685, 0, -0.4131, 0, 1, 0, 0.4131, 0, 0.910685, -4.81287, -0.152566, 9.90641 )
1717

18-
[node name="MeshInstance" type="MeshInstance" parent="."]
18+
[node name="TestCube" type="MeshInstance" parent="."]
1919
mesh = SubResource( 2 )
2020
material/0 = null
2121

@@ -292,9 +292,9 @@ margin_right = 286.0
292292
margin_bottom = -63.0
293293
size_flags_horizontal = 2
294294
size_flags_vertical = 0
295-
text = "F1: activate MOUSE_MODE_VISIBLE
296-
F2: activate MOUSE_MODE_HIDDEN
297-
F3: activate MOUSE_MODE_CAPTURED"
295+
text = "F1: Activate MOUSE_MODE_VISIBLE
296+
F2: Activate MOUSE_MODE_HIDDEN
297+
F3: Activate MOUSE_MODE_CAPTURED"
298298
valign = 2
299299

300300
[node name="Label_MouseModeCaptured_KeyInfo" type="Label" parent="Control"]
@@ -306,9 +306,9 @@ margin_right = 286.0
306306
margin_bottom = -11.0
307307
size_flags_horizontal = 2
308308
size_flags_vertical = 0
309-
text = "ESC: deactivate MOUSE_MODE_CAPTURED
310-
W, S: move forward, backward
311-
A, D: strafe left, right"
309+
text = "ESC: Deactivate MOUSE_MODE_CAPTURED
310+
W, S: Move forward, backward
311+
A, D: Strafe left, right"
312312
valign = 2
313313

314314
[node name="Label_MouseModes" type="Label" parent="Control"]

0 commit comments

Comments
 (0)