-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcomponents.rs
More file actions
66 lines (57 loc) · 1.42 KB
/
components.rs
File metadata and controls
66 lines (57 loc) · 1.42 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
/// Author: John Klucinec (@johnklucinec)
use super::commands::CommandType;
use bevy::prelude::*;
use crossbeam_channel::{Receiver, Sender};
use std::fmt;
use std::{
collections::VecDeque,
process::{Child, ChildStdin},
};
#[allow(dead_code)]
#[derive(Event, Clone, Debug)]
pub struct CommandEvent {
pub command_type: CommandType,
pub value: Option<f32>,
pub string_value: String,
}
#[derive(Resource)]
#[allow(dead_code)]
pub struct PythonComms {
pub child: Child,
pub stdin: ChildStdin,
pub tx: Sender<String>,
pub rx: Receiver<String>,
}
#[derive(Default, Resource)]
pub struct CommandQueue {
queue: VecDeque<CommandMessage>,
}
#[allow(dead_code)]
impl CommandQueue {
pub fn enqueue(&mut self, command: CommandMessage) {
self.queue.push_back(command);
}
pub fn dequeue(&mut self) -> Option<CommandMessage> {
self.queue.pop_front()
}
pub fn is_empty(&self) -> bool {
self.queue.is_empty()
}
}
pub struct CommandMessage {
command_type: CommandType,
payload: String,
}
impl fmt::Display for CommandMessage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.command_type.to_string(), self.payload)
}
}
impl CommandMessage {
pub fn new(command_type: CommandType, payload: impl Into<String>) -> Self {
Self {
command_type,
payload: payload.into(),
}
}
}