Repository navigation
Expand file tree
/
Copy pathinput_tick.go
More file actions
181 lines (166 loc) · 5.09 KB
/
Copy pathinput_tick.go
File metadata and controls
181 lines (166 loc) · 5.09 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package spx
import (
"fmt"
"slices"
"time"
inputstate "github.com/goplus/spx/v4/internal/core/input"
)
type inputSessionTick struct {
frame InputReplayFrame
initialLeftPressed bool
firstTick bool
}
// inputPlaybackState contains only scalar values, so queries cannot expose the
// session's held-key slice or require an owned copy of it.
type inputPlaybackState struct {
position InputReplayMouse
buttons uint8
keyPressed bool
}
// consumeSampledInputTickLocked requires the caller to hold s.operationMu.
// It acquires s.mu itself to protect session state during sampling and resolution.
func (s *inputSession) consumeSampledInputTickLocked(
delta float64,
sample func() (InputReplayState, []InputReplayMouseEvent, []InputReplayKeyEvent),
) (inputSessionTick, error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.phase != InputSessionPhaseRunning {
return inputSessionTick{}, fmt.Errorf("input session cannot consume a tick in phase %q", s.phase)
}
live, mouseEvents, keyEvents := sample()
status := s.controller.Status()
if s.mode == InputSessionModeRecording && s.initialPending && status.NextFrame == 0 {
initial := inputStateBeforeEvents(live, mouseEvents, keyEvents)
if err := s.controller.SynchronizeRecordingInitial(initial); err != nil {
return inputSessionTick{}, err
}
s.initialLeftPressed = initial.Buttons&1 != 0
s.initialPending = false
}
frame, firstTick, err := s.controller.Resolve(live, mouseEvents, keyEvents, delta)
if err != nil {
return inputSessionTick{}, err
}
s.current = inputstate.CloneInputReplayState(frame.State)
s.currentTime = frame.Time
s.currentTick = frame.Frame
if s.mode == InputSessionModeReplaying && firstTick && frame.Frame < 0 {
// An empty replay still owns one synthetic effective tick so its initial
// state can be observed for a complete rendered frame.
s.currentTick = 0
}
s.hasCurrentTick = true
if s.mode == InputSessionModeReplaying && s.controller.Status().Exhausted {
s.phase = InputSessionPhaseFinishing
}
return inputSessionTick{
frame: frame,
initialLeftPressed: s.initialLeftPressed,
firstTick: firstTick,
}, nil
}
func (s *inputSession) playbackInput(key Key) (inputPlaybackState, bool) {
if s == nil {
return inputPlaybackState{}, false
}
s.mu.Lock()
defer s.mu.Unlock()
if s.mode != InputSessionModeReplaying || s.phase == InputSessionPhaseAborted {
return inputPlaybackState{}, false
}
pressed := len(s.current.KeysDown) != 0
if key != KeyAny {
_, pressed = slices.BinarySearch(s.current.KeysDown, int64(key))
}
return inputPlaybackState{
position: s.current.Mouse,
buttons: s.current.Buttons,
keyPressed: pressed,
}, true
}
func (s *inputSession) logicalClock() time.Time {
s.mu.Lock()
defer s.mu.Unlock()
return time.Unix(0, int64(s.currentTime*float64(time.Second)))
}
func (p *Game) inputClock() time.Time {
if session := p.currentInputSession(); session != nil {
return session.logicalClock()
}
return time.Now()
}
// captureInputTick returns a copy of the active game's latest input tick, or nil.
func captureInputTick() *int64 {
session := activeInputSession()
if session == nil {
return nil
}
session.mu.Lock()
defer session.mu.Unlock()
if !session.hasCurrentTick || session.phase == InputSessionPhaseAborted {
return nil
}
tick := session.currentTick
return &tick
}
func sortedPressedKeys(keys map[int64]bool) []int64 {
pressed := make([]int64, 0, len(keys))
for key, down := range keys {
if down {
pressed = append(pressed, key)
}
}
slices.Sort(pressed)
return pressed
}
// inputStateBeforeEvents reconstructs the state immediately before tick zero's
// ordered edges, preserving a complete press/release pair in that first tick.
func inputStateBeforeEvents(
live InputReplayState,
mouseEvents []InputReplayMouseEvent,
keyEvents []InputReplayKeyEvent,
) InputReplayState {
initial := live
buttons := live.Buttons
for _, event := range slices.Backward(mouseEvents) {
if event.Button < MOUSE_BUTTON_LEFT || event.Button > MOUSE_BUTTON_MIDDLE {
continue
}
mask := uint8(1 << (event.Button - 1))
if event.Pressed {
buttons &^= mask
} else {
buttons |= mask
}
}
initial.Buttons = buttons
keys := make(map[int64]bool, len(live.KeysDown))
for _, key := range live.KeysDown {
keys[key] = true
}
for _, event := range slices.Backward(keyEvents) {
if event.Pressed {
delete(keys, event.Key)
} else {
keys[event.Key] = true
}
}
initial.KeysDown = sortedPressedKeys(keys)
return initial
}