Improve window management demo

This commit is contained in:
Aaron Franke
2020-02-02 03:44:02 -05:00
parent ac882369bc
commit f07c7091fc
5 changed files with 85 additions and 120 deletions

View File

@@ -1,27 +1,27 @@
extends Control
# Member variables
var mousepos
onready var observer = $"../Observer"
func _ready():
if not check_wm_api():
set_physics_process(false)
set_process_input(false)
func _physics_process(_delta):
var modetext = "Mode:\n"
if OS.is_window_fullscreen():
modetext += "Fullscreen\n"
else:
modetext += "Windowed\n"
if !OS.is_window_resizable():
modetext += "FixedSize\n"
if OS.is_window_minimized():
modetext += "Minimized\n"
if OS.is_window_maximized():
modetext += "Maximized\n"
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
modetext += "MouseGrab\n"
$Label_MouseModeCaptured_KeyInfo.show()
@@ -63,73 +63,6 @@ func _physics_process(_delta):
$Button_MouseModeCaptured.set_pressed(Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED)
func check_wm_api():
var s = ""
if !OS.has_method("get_screen_count"):
s += " - get_screen_count()\n"
if !OS.has_method("get_current_screen"):
s += " - get_current_screen()\n"
if !OS.has_method("set_current_screen"):
s += " - set_current_screen()\n"
if !OS.has_method("get_screen_position"):
s += " - get_screen_position()\n"
if !OS.has_method("get_screen_size"):
s += " - get_screen_size()\n"
if !OS.has_method("get_window_position"):
s += " - get_window_position()\n"
if !OS.has_method("set_window_position"):
s += " - set_window_position()\n"
if !OS.has_method("get_window_size"):
s += " - get_window_size()\n"
if !OS.has_method("set_window_size"):
s += " - set_window_size()\n"
if !OS.has_method("set_window_fullscreen"):
s += " - set_window_fullscreen()\n"
if !OS.has_method("is_window_fullscreen"):
s += " - is_window_fullscreen()\n"
if !OS.has_method("set_window_resizable"):
s += " - set_window_resizable()\n"
if !OS.has_method("is_window_resizable"):
s += " - is_window_resizable()\n"
if !OS.has_method("set_window_minimized"):
s += " - set_window_minimized()\n"
if !OS.has_method("is_window_minimized"):
s += " - is_window_minimized()\n"
if !OS.has_method("set_window_maximized"):
s += " - set_window_maximized()\n"
if !OS.has_method("is_window_maximized"):
s += " - is_window_maximized()\n"
if s.length() == 0:
return true
else:
$"ImplementationDialog/Text".text += s
$ImplementationDialog.show()
return false
func _ready():
if not check_wm_api():
set_physics_process(false)
set_process_input(false)
func _input(event):
if event is InputEventMouseMotion:
mousepos = event.position
@@ -147,6 +80,51 @@ func _input(event):
_on_Button_MouseModeCaptured_pressed()
func check_wm_api():
var s = ""
if !OS.has_method("get_screen_count"):
s += " - get_screen_count()\n"
if !OS.has_method("get_current_screen"):
s += " - get_current_screen()\n"
if !OS.has_method("set_current_screen"):
s += " - set_current_screen()\n"
if !OS.has_method("get_screen_position"):
s += " - get_screen_position()\n"
if !OS.has_method("get_screen_size"):
s += " - get_screen_size()\n"
if !OS.has_method("get_window_position"):
s += " - get_window_position()\n"
if !OS.has_method("set_window_position"):
s += " - set_window_position()\n"
if !OS.has_method("get_window_size"):
s += " - get_window_size()\n"
if !OS.has_method("set_window_size"):
s += " - set_window_size()\n"
if !OS.has_method("set_window_fullscreen"):
s += " - set_window_fullscreen()\n"
if !OS.has_method("is_window_fullscreen"):
s += " - is_window_fullscreen()\n"
if !OS.has_method("set_window_resizable"):
s += " - set_window_resizable()\n"
if !OS.has_method("is_window_resizable"):
s += " - is_window_resizable()\n"
if !OS.has_method("set_window_minimized"):
s += " - set_window_minimized()\n"
if !OS.has_method("is_window_minimized"):
s += " - is_window_minimized()\n"
if !OS.has_method("set_window_maximized"):
s += " - set_window_maximized()\n"
if !OS.has_method("is_window_maximized"):
s += " - is_window_maximized()\n"
if s.length() == 0:
return true
else:
$"ImplementationDialog/Text".text += s
$ImplementationDialog.show()
return false
func _on_Button_MoveTo_pressed():
OS.set_window_position(Vector2(100, 100))
@@ -200,4 +178,5 @@ func _on_Button_MouseModeHidden_pressed():
func _on_Button_MouseModeCaptured_pressed():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
observer.state = observer.STATE_GRAB

View File

@@ -1,56 +1,33 @@
extends KinematicBody
# Constants
const STATE_MENU = 0
const STATE_GRAB = 1
# Member variables
var r_pos = Vector2()
var state = STATE_MENU
onready var camera = $Camera
func direction(vector):
var v = $Camera.get_global_transform().basis * vector
v = v.normalized()
return v
func _physics_process(delta):
if (state != STATE_GRAB):
func _process(delta):
if state != STATE_GRAB:
return
if (Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED):
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
var dir = Vector3()
if (Input.is_action_pressed("move_forward")):
dir += direction(Vector3(0, 0, -1))
if (Input.is_action_pressed("move_backwards")):
dir += direction(Vector3(0, 0, 1))
if (Input.is_action_pressed("move_left")):
dir += direction(Vector3(-1, 0, 0))
if (Input.is_action_pressed("move_right")):
dir += direction(Vector3(1, 0, 0))
dir = dir.normalized()
move_and_collide(dir * 10 * delta)
var d = delta * 0.1
# set yaw
rotate(Vector3(0, 1, 0), d*r_pos.x)
# set pitch
var pitch = $Camera.get_transform().rotated(Vector3(1, 0, 0), d * r_pos.y)
$Camera.set_transform(pitch)
r_pos = Vector2()
var x_movement = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
var z_movement = Input.get_action_strength("move_backwards") - Input.get_action_strength("move_forward")
var dir = direction(Vector3(x_movement, 0, z_movement))
transform.origin += dir * 10 * delta
var d = delta * 0.1 # Scale the input, easiest to do by scaling the delta.
rotate(Vector3.UP, d * r_pos.x) # Yaw
camera.transform = camera.transform.rotated(Vector3.RIGHT, d * r_pos.y) # Pitch
r_pos = Vector2.ZERO # We've dealt with all the input, so set it to zero.
func _input(event):
if (event is InputEventMouseMotion):
r_pos = -event.relative
if (event.is_action("ui_cancel") and event.is_pressed() and !event.is_echo()):
if (state == STATE_GRAB):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
@@ -58,3 +35,8 @@ func _input(event):
else:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
state = STATE_GRAB
func direction(vector):
var v = camera.get_global_transform().basis * vector
return v.normalized()

View File

@@ -16,4 +16,4 @@ near = 0.1
far = 1000.0
[node name="OmniLight" type="OmniLight" parent="."]
omni_range = 8.0

View File

@@ -51,20 +51,24 @@ mouse_mode_visible={
move_backwards={
"deadzone": 0.5,
"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)
, 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)
]
}
move_forward={
"deadzone": 0.5,
"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)
, 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)
]
}
move_left={
"deadzone": 0.5,
"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)
, 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)
]
}
move_right={
"deadzone": 0.5,
"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)
, 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)
]
}

View File

@@ -10,12 +10,12 @@ albedo_color = Color( 0.835294, 0.133333, 0.133333, 1 )
material = SubResource( 1 )
size = Vector3( 5, 5, 5 )
[node name="Spatial" type="Spatial"]
[node name="WindowManagement" type="Spatial"]
[node name="Observer" parent="." instance=ExtResource( 1 )]
transform = Transform( 0.910685, 0, -0.4131, 0, 1, 0, 0.4131, 0, 0.910685, -4.81287, -0.152566, 9.90641 )
[node name="MeshInstance" type="MeshInstance" parent="."]
[node name="TestCube" type="MeshInstance" parent="."]
mesh = SubResource( 2 )
material/0 = null
@@ -292,9 +292,9 @@ margin_right = 286.0
margin_bottom = -63.0
size_flags_horizontal = 2
size_flags_vertical = 0
text = "F1: activate MOUSE_MODE_VISIBLE
F2: activate MOUSE_MODE_HIDDEN
F3: activate MOUSE_MODE_CAPTURED"
text = "F1: Activate MOUSE_MODE_VISIBLE
F2: Activate MOUSE_MODE_HIDDEN
F3: Activate MOUSE_MODE_CAPTURED"
valign = 2
[node name="Label_MouseModeCaptured_KeyInfo" type="Label" parent="Control"]
@@ -306,9 +306,9 @@ margin_right = 286.0
margin_bottom = -11.0
size_flags_horizontal = 2
size_flags_vertical = 0
text = "ESC: deactivate MOUSE_MODE_CAPTURED
W, S: move forward, backward
A, D: strafe left, right"
text = "ESC: Deactivate MOUSE_MODE_CAPTURED
W, S: Move forward, backward
A, D: Strafe left, right"
valign = 2
[node name="Label_MouseModes" type="Label" parent="Control"]