Skip to main content

wackasm_opencomputers/
types.rs

1//! Enums for the numbers OpenComputers uses as codes.
2//! They convert into [`Number`], so they can be passed wherever a method
3//! takes a side or colour (and into `Value` through it).
4
5use crate::value::Number;
6
7/// A side, as robots, drones, redstone and transposers number them.
8/// Relative names apply to robots and drones; absolute ones to blocks.
9#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
10#[repr(u8)]
11pub enum Side {
12    /// Down / bottom.
13    Bottom = 0,
14    /// Up / top.
15    Top = 1,
16    /// North, or back for robots.
17    Back = 2,
18    /// South, or front for robots.
19    Front = 3,
20    /// West, or right for robots.
21    Right = 4,
22    /// East, or left for robots.
23    Left = 5,
24}
25
26impl Side {
27    pub const DOWN: Side = Side::Bottom;
28    pub const UP: Side = Side::Top;
29    pub const NORTH: Side = Side::Back;
30    pub const SOUTH: Side = Side::Front;
31    pub const WEST: Side = Side::Right;
32    pub const EAST: Side = Side::Left;
33    pub const ALL: [Side; 6] = [Side::Bottom, Side::Top, Side::Back, Side::Front, Side::Right, Side::Left];
34
35    pub fn from_index(i: i64) -> Option<Side> {
36        Side::ALL.get(usize::try_from(i).ok()?).copied()
37    }
38}
39
40impl From<Side> for Number {
41    fn from(side: Side) -> Number {
42        Number::Int(side as i64)
43    }
44}
45
46/// A bundled-redstone / dye colour index.
47#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
48#[repr(u8)]
49pub enum Color {
50    White = 0,
51    Orange = 1,
52    Magenta = 2,
53    LightBlue = 3,
54    Yellow = 4,
55    Lime = 5,
56    Pink = 6,
57    Gray = 7,
58    Silver = 8,
59    Cyan = 9,
60    Purple = 10,
61    Blue = 11,
62    Brown = 12,
63    Green = 13,
64    Red = 14,
65    Black = 15,
66}
67
68impl From<Color> for Number {
69    fn from(color: Color) -> Number {
70        Number::Int(color as i64)
71    }
72}
73
74/// Keyboard codes (`Signal::KeyDown::code`), for keys without a character.
75pub mod keys {
76    pub const ESCAPE: u32 = 1;
77    pub const BACKSPACE: u32 = 14;
78    pub const TAB: u32 = 15;
79    pub const ENTER: u32 = 28;
80    pub const LEFT_CONTROL: u32 = 29;
81    pub const LEFT_SHIFT: u32 = 42;
82    pub const SPACE: u32 = 57;
83    pub const HOME: u32 = 199;
84    pub const UP: u32 = 200;
85    pub const PAGE_UP: u32 = 201;
86    pub const LEFT: u32 = 203;
87    pub const RIGHT: u32 = 205;
88    pub const END: u32 = 207;
89    pub const DOWN: u32 = 208;
90    pub const PAGE_DOWN: u32 = 209;
91    pub const INSERT: u32 = 210;
92    pub const DELETE: u32 = 211;
93}