This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ControlDriver.cs
82 lines (73 loc) · 2.4 KB
/
ControlDriver.cs
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
using System;
using System.Threading.Tasks;
using Godot;
using GodotTestDriver.Input;
using GodotTestDriver.Util;
using JetBrains.Annotations;
namespace GodotTestDriver.Drivers
{
/// <summary>
/// Driver for <see cref="Control"/> nodes.
/// </summary>
[PublicAPI]
public partial class ControlDriver<T> : CanvasItemDriver<T> where T : Control
{
public ControlDriver(Func<T> producer, string description = "") : base(producer, description)
{
}
/// <summary>
/// Returns true if the Node is visible and fully inside the viewport rect.
/// </summary>
public bool IsFullyInView
{
get
{
if (!IsVisible)
{
return false;
}
var control = VisibleRoot;
var screenRect = control.GetViewport().WorldToScreen(control.GetGlobalRect());
return control.GetViewportRect().Encloses(screenRect);
}
}
/// <summary>
/// Clicks the control with the mouse in the center.
/// </summary>
public virtual async Task ClickCenter(MouseButton button = MouseButton.Left)
{
var control = VisibleRoot;
await control.GetViewport().ClickMouseAt(control.GetGlobalRect().Center(), button);
}
/// <summary>
/// Moves the mouse to the center of the control and hovers for the given amount
/// of seconds.
/// </summary>
public async Task Hover(float seconds)
{
var control = VisibleRoot;
await control.GetViewport().MoveMouseTo(control.GetGlobalRect().Center());
await control.SleepSeconds(seconds);
}
/// <summary>
/// Instructs the control to release the focus.
/// </summary>
public async Task ReleaseFocus()
{
var control = VisibleRoot;
await control.GetTree().NextFrame();
control.ReleaseFocus();
await control.GetTree().WaitForEvents();
}
/// <summary>
/// Instructs the control to grab the focus.
/// </summary>
public async Task GrabFocus()
{
var control = VisibleRoot;
await control.GetTree().NextFrame();
control.GrabFocus();
await control.GetTree().WaitForEvents();
}
}
}