aboutsummaryrefslogtreecommitdiffstats
path: root/spec/input_spec.moon
blob: 81f2d738ee3377e4235777aa151ac89e1ddf9cae (plain)
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
import Input from require 'input'
import vec2 from require 'cpml'

describe "Input", ->
  input = Input!

  it "sane initial state", ->
    assert.is_equal vec2!, input\mouse_delta!
    assert.is_falsy input\mouse_down!
    assert.is_falsy input\mouse_up!
    assert.is_falsy input\mouse_held!
    assert.is_equal 'hover', input\mouse_event!
    assert.is_falsy input\key_down  'w'
    assert.is_falsy input\key_up    'w'
    assert.is_falsy input\key_held  'w'
    assert.is_falsy input\key_event 'w'

  it "processes input events", ->
    input\mousemoved 0, 0
    input\frame!
    input\mousemoved 20, 20
    input\keypressed 'w'
    input\keypressed 'a'
    input\mousepressed 21, 21, 1
    input\mousepressed 21, 21, 2

  it "updates accordingly", ->
    assert.is_equal (vec2 20, 20), input.mouse
    assert.is_equal (vec2 20, 20), input\mouse_pos!
    assert.is_equal (vec2 20, 20), input\mouse_delta!

    assert.is_true  input\mouse_down!
    assert.is_true  input\mouse_held!
    assert.is_falsy input\mouse_up!
    assert.is_equal 'down', input\mouse_event!

    assert.is_true  input\mouse_down 2
    assert.is_true  input\mouse_held 2
    assert.is_falsy input\mouse_up   2
    assert.is_equal 'down', input\mouse_event 2

    assert.is_true  input\key_down 'w'
    assert.is_true  input\key_held 'w'
    assert.is_falsy input\key_up   'w'
    assert.is_equal 'down', input\key_event 'w'

  it "keep state after end-of-frame", ->
    input\frame!

    assert.is_equal (vec2 20, 20), input.mouse
    assert.is_equal (vec2 20, 20), input\mouse_pos!
    assert.is_equal vec2!,         input\mouse_delta!

    assert.is_true  input\mouse_held!
    assert.is_falsy input\mouse_down!
    assert.is_falsy input\mouse_up!
    assert.is_equal 'held', input\mouse_event!

    assert.is_true  input\mouse_held 2
    assert.is_falsy input\mouse_down 2
    assert.is_falsy input\mouse_up   2
    assert.is_equal 'held', input\mouse_event 2

    assert.is_true  input\key_held 'w'
    assert.is_falsy input\key_down 'w'
    assert.is_falsy input\key_up   'w'
    assert.is_equal 'held', input\key_event 'w'

  it "keeps updating", ->
    input\keyreleased 'a'
    input\mousereleased 21, 21, 2
    input\mousemoved 0, 40

    assert.is_equal (vec2 0, 40),   input.mouse
    assert.is_equal (vec2 0, 40),   input\mouse_pos!
    assert.is_equal (vec2 -20, 20), input\mouse_delta!

    assert.is_true  input\mouse_held!
    assert.is_falsy input\mouse_down!
    assert.is_falsy input\mouse_up!
    assert.is_equal 'held', input\mouse_event!

    assert.is_true  input\mouse_up   2
    assert.is_falsy input\mouse_held 2
    assert.is_falsy input\mouse_down 2
    assert.is_equal 'up', input\mouse_event 2

    assert.is_true  input\key_held 'w'
    assert.is_falsy input\key_down 'w'
    assert.is_falsy input\key_up   'w'
    assert.is_equal 'held', input\key_event 'w'

    assert.is_true  input\key_up   'a'
    assert.is_falsy input\key_held 'a'
    assert.is_falsy input\key_down 'a'
    assert.is_equal 'up', input\key_event 'a'

  it "allows capturing the keyboard", ->
    input\text_capture!
    assert.has_error -> input\text_capture!

    input\textinput 'Helloo'
    input\keypressed 'backspace'
    assert.is_falsy input\key_down 'backspace'
    input\frame!

    assert.is_equal 'Hello', input.text
    input\textinput ' World'
    assert.is_equal 'Hello World', input.text
    assert.is_falsy input\key_down 'backspace'

  it "doesn't capture meta-keys (return and escape)", ->
    input\keypressed 'return'
    input\keypressed 'escape'

    assert.is_true input\key_down 'return'
    assert.is_true input\key_down 'escape'

  it "returns the input' text after releasing the keyboard", ->
    assert.is_equal 'Hello World', input\text_release!

    input\keypressed 'backspace'
    assert.is_true input\key_down 'backspace'