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
|
pub mod layout_data {
use std::collections::{HashMap, VecDeque};
use bincode::{Decode, Encode};
use serde::{Deserialize, Serialize};
use crate::pad_data::pad_messages::nogamepads_messages::ControlMessage;
use crate::pad_data::pad_player_info::nogamepads_player_info::PlayerInfo;
use crate::pad_service::server::nogamepads_server::PadServer;
#[derive(Encode, Decode, Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct LayoutKeyRegisters {
pub direction_keys : HashMap<u8, String>, // 注册方向键
pub axis_keys : HashMap<u8, String>, // 注册轴向键
pub button_keys : HashMap<u8, String>, // 注册按钮
}
impl Default for LayoutKeyRegisters {
fn default() -> LayoutKeyRegisters {
LayoutKeyRegisters {
direction_keys: Default::default(),
axis_keys: Default::default(),
button_keys: Default::default(),
}
}
}
pub struct LayoutKeyRuntimeData {
directions : HashMap<u8, HashMap<String, (f64, f64)>>, // <键, <玩家Hash, (x, y)>>
axes : HashMap<u8, HashMap<String, f64>>, // <键, <玩家Hash, 轴向>>
button : HashMap<u8, HashMap<String, bool>>, // <键, <玩家Hash, 是否按下>>
events : VecDeque<(String, ControlMessage)>, // (玩家Hash, 信息)
}
impl Default for LayoutKeyRuntimeData {
fn default() -> Self {
LayoutKeyRuntimeData {
directions: Default::default(),
axes: Default::default(),
button: Default::default(),
events: Default::default(),
}
}
}
impl LayoutKeyRuntimeData {
// 输入控制信息到数据
pub fn insert_control(&mut self, who: PlayerInfo, msg: ControlMessage) {
match msg {
// 消息放入信息队列待读取
ControlMessage::Msg(_) => {
self.events.push_back((who.account.player_hash, msg))
}
// 按钮更新对应玩家的状态,并且放入信息队列待读取
ControlMessage::Pressed(button_key) => {
self.button.entry(button_key)
.or_insert_with(HashMap::new)
.insert(who.account.player_hash.clone(), true);
self.events.push_back((who.account.player_hash, msg))
}
ControlMessage::Released(button_key) => {
self.button.entry(button_key)
.or_insert_with(HashMap::new)
.insert(who.account.player_hash.clone(), false);
self.events.push_back((who.account.player_hash, msg))
}
// 轴向更新直接传入玩家状态
ControlMessage::Axis(axis_key, axis) => {
self.axes.entry(axis_key)
.or_insert_with(HashMap::new)
.insert(who.account.player_hash.clone(), axis);
}
ControlMessage::Dir(dir_key, (x, y)) => {
self.directions.entry(dir_key)
.or_insert_with(HashMap::new)
.insert(who.account.player_hash.clone(), (x, y));
}
_ => { }
}
}
pub fn pop_control_event(&mut self, server: &PadServer) -> Option<(PlayerInfo, ControlMessage)> {
let pop = self.events.pop_front();
if pop.is_some() {
let (hash, msg) = pop.unwrap();
let info = server.find_online_player(hash);
if info.is_some() {
Some((info.unwrap(), msg))
} else {
None
}
} else {
None
}
}
pub fn get_direction(&self, who: &PlayerInfo, key: &u8) -> Option<(f64, f64)> {
Self::get(&self.directions, who, key)
}
pub fn get_axis(&self, who: &PlayerInfo, key: &u8) -> Option<f64> {
Self::get(&self.axes, who, key)
}
pub fn get_button_status(&self, who: &PlayerInfo, key: &u8) -> Option<bool> {
Self::get(&self.button, who, key)
}
fn get<V: Clone>(map: &HashMap<u8, HashMap<String, V>>, who: &PlayerInfo, key: &u8) -> Option<V> {
let key = map.get(key);
if key.is_some() {
let value = key.unwrap().get(&who.account.player_hash);
if value.is_some() {
let result = value.unwrap();
Some(result.clone())
} else { None }
} else { None }
}
}
}
pub mod layout_gamepad {
use bincode::{Decode, Encode};
use serde::{Deserialize, Serialize};
#[derive(Encode, Decode, Serialize, Deserialize, PartialEq, Debug)]
pub struct PadLayout {
}
pub trait ButtonArea {
}
}
|