|
| 1 | +//! A click in a container screen, as vanilla `AbstractContainerMenu.clicked` |
| 2 | +//! takes it: a slot, a button and a `ContainerInput`. |
| 3 | +
|
| 4 | +use pomme_protocol::serverbound::ContainerInput; |
| 5 | + |
| 6 | +/// One click, by what it does. |
| 7 | +#[derive(Clone, Debug)] |
| 8 | +pub enum ClickOperation { |
| 9 | + Pickup(PickupClick), |
| 10 | + QuickMove(QuickMoveClick), |
| 11 | + Swap(SwapClick), |
| 12 | + Clone(CloneClick), |
| 13 | + Throw(ThrowClick), |
| 14 | + QuickCraft(QuickCraftClick), |
| 15 | + PickupAll(PickupAllClick), |
| 16 | +} |
| 17 | + |
| 18 | +/// A left or right click; with no slot it is outside the menu and drops |
| 19 | +/// what the cursor carries, which the packet sends as slot -999. |
| 20 | +#[derive(Clone, Debug)] |
| 21 | +pub enum PickupClick { |
| 22 | + Left { slot: Option<u16> }, |
| 23 | + Right { slot: Option<u16> }, |
| 24 | +} |
| 25 | + |
| 26 | +/// A shift-click, with either button. |
| 27 | +#[derive(Clone, Debug)] |
| 28 | +pub enum QuickMoveClick { |
| 29 | + Left { slot: u16 }, |
| 30 | + Right { slot: u16 }, |
| 31 | +} |
| 32 | + |
| 33 | +/// A number key or the swap-hands key over a slot. |
| 34 | +#[derive(Clone, Debug)] |
| 35 | +pub struct SwapClick { |
| 36 | + pub source_slot: u16, |
| 37 | + /// The hotbar slot, 0 to 8, or 40 for the offhand. |
| 38 | + pub target_slot: u8, |
| 39 | +} |
| 40 | + |
| 41 | +/// A middle click, which copies a stack in creative. |
| 42 | +#[derive(Clone, Debug)] |
| 43 | +pub struct CloneClick { |
| 44 | + pub slot: u16, |
| 45 | +} |
| 46 | + |
| 47 | +/// The drop key over a slot: one item, or the whole stack with control. |
| 48 | +#[derive(Clone, Debug)] |
| 49 | +pub enum ThrowClick { |
| 50 | + Single { slot: u16 }, |
| 51 | + All { slot: u16 }, |
| 52 | +} |
| 53 | + |
| 54 | +/// One step of a drag across slots. |
| 55 | +#[derive(Clone, Debug, Eq, PartialEq)] |
| 56 | +pub struct QuickCraftClick { |
| 57 | + pub kind: QuickCraftKind, |
| 58 | + pub status: QuickCraftStatus, |
| 59 | +} |
| 60 | + |
| 61 | +/// The button a drag is made with, in `getQuickcraftType`'s order. |
| 62 | +// TODO: creative's middle-button drag (type 2), which fills each slot, is |
| 63 | +// not started anywhere yet. |
| 64 | +#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 65 | +pub enum QuickCraftKind { |
| 66 | + Left, |
| 67 | + Right, |
| 68 | +} |
| 69 | + |
| 70 | +/// Where in a drag a step is, in `getQuickcraftHeader`'s order. |
| 71 | +#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 72 | +pub enum QuickCraftStatus { |
| 73 | + Start, |
| 74 | + Add { slot: u16 }, |
| 75 | + End, |
| 76 | +} |
| 77 | + |
| 78 | +/// A double click, which gathers what matches the carried stack. |
| 79 | +#[derive(Clone, Debug)] |
| 80 | +pub struct PickupAllClick { |
| 81 | + pub slot: u16, |
| 82 | +} |
| 83 | + |
| 84 | +impl ClickOperation { |
| 85 | + /// The slot the click acts on, if it has one. |
| 86 | + pub fn slot_num(&self) -> Option<u16> { |
| 87 | + match self { |
| 88 | + Self::Pickup(PickupClick::Left { slot } | PickupClick::Right { slot }) => *slot, |
| 89 | + Self::QuickMove(QuickMoveClick::Left { slot } | QuickMoveClick::Right { slot }) |
| 90 | + | Self::Throw(ThrowClick::Single { slot } | ThrowClick::All { slot }) |
| 91 | + | Self::Clone(CloneClick { slot }) |
| 92 | + | Self::PickupAll(PickupAllClick { slot }) => Some(*slot), |
| 93 | + Self::Swap(swap) => Some(swap.source_slot), |
| 94 | + Self::QuickCraft(drag) => match drag.status { |
| 95 | + QuickCraftStatus::Add { slot } => Some(slot), |
| 96 | + QuickCraftStatus::Start | QuickCraftStatus::End => None, |
| 97 | + }, |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /// The packet's button: the mouse button, a swap's target, or a drag's |
| 102 | + /// `getQuickcraftMask`. |
| 103 | + pub fn button_num(&self) -> u8 { |
| 104 | + match self { |
| 105 | + Self::Pickup(PickupClick::Left { .. }) |
| 106 | + | Self::QuickMove(QuickMoveClick::Left { .. }) |
| 107 | + | Self::Throw(ThrowClick::Single { .. }) |
| 108 | + | Self::PickupAll(_) => 0, |
| 109 | + Self::Pickup(PickupClick::Right { .. }) |
| 110 | + | Self::QuickMove(QuickMoveClick::Right { .. }) |
| 111 | + | Self::Throw(ThrowClick::All { .. }) => 1, |
| 112 | + Self::Clone(_) => 2, |
| 113 | + Self::Swap(swap) => swap.target_slot, |
| 114 | + Self::QuickCraft(drag) => { |
| 115 | + let header = match drag.status { |
| 116 | + QuickCraftStatus::Start => 0, |
| 117 | + QuickCraftStatus::Add { .. } => 1, |
| 118 | + QuickCraftStatus::End => 2, |
| 119 | + }; |
| 120 | + header | (drag.kind as u8) << 2 |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + pub fn input(&self) -> ContainerInput { |
| 126 | + match self { |
| 127 | + Self::Pickup(_) => ContainerInput::Pickup, |
| 128 | + Self::QuickMove(_) => ContainerInput::QuickMove, |
| 129 | + Self::Swap(_) => ContainerInput::Swap, |
| 130 | + Self::Clone(_) => ContainerInput::Clone, |
| 131 | + Self::Throw(_) => ContainerInput::Throw, |
| 132 | + Self::QuickCraft(_) => ContainerInput::QuickCraft, |
| 133 | + Self::PickupAll(_) => ContainerInput::PickupAll, |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +#[cfg(test)] |
| 139 | +mod tests { |
| 140 | + use super::*; |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn a_drags_button_packs_its_step_and_its_mouse_button() { |
| 144 | + let button = |kind, status| { |
| 145 | + ClickOperation::QuickCraft(QuickCraftClick { kind, status }).button_num() |
| 146 | + }; |
| 147 | + let add = QuickCraftStatus::Add { slot: 3 }; |
| 148 | + assert_eq!(button(QuickCraftKind::Left, QuickCraftStatus::Start), 0); |
| 149 | + assert_eq!(button(QuickCraftKind::Left, add), 1); |
| 150 | + assert_eq!(button(QuickCraftKind::Left, QuickCraftStatus::End), 2); |
| 151 | + assert_eq!(button(QuickCraftKind::Right, QuickCraftStatus::Start), 4); |
| 152 | + assert_eq!(button(QuickCraftKind::Right, add), 5); |
| 153 | + assert_eq!(button(QuickCraftKind::Right, QuickCraftStatus::End), 6); |
| 154 | + } |
| 155 | + |
| 156 | + #[test] |
| 157 | + fn a_click_names_its_slot_button_and_input() { |
| 158 | + let parts = |op: ClickOperation| (op.slot_num(), op.button_num(), op.input()); |
| 159 | + assert_eq!( |
| 160 | + parts(ClickOperation::Pickup(PickupClick::Right { slot: None })), |
| 161 | + (None, 1, ContainerInput::Pickup) |
| 162 | + ); |
| 163 | + assert_eq!( |
| 164 | + parts(ClickOperation::Swap(SwapClick { |
| 165 | + source_slot: 12, |
| 166 | + target_slot: 40, |
| 167 | + })), |
| 168 | + (Some(12), 40, ContainerInput::Swap) |
| 169 | + ); |
| 170 | + assert_eq!( |
| 171 | + parts(ClickOperation::Throw(ThrowClick::All { slot: 7 })), |
| 172 | + (Some(7), 1, ContainerInput::Throw) |
| 173 | + ); |
| 174 | + assert_eq!( |
| 175 | + parts(ClickOperation::Clone(CloneClick { slot: 5 })), |
| 176 | + (Some(5), 2, ContainerInput::Clone) |
| 177 | + ); |
| 178 | + assert_eq!( |
| 179 | + parts(ClickOperation::QuickMove(QuickMoveClick::Right { slot: 9 })), |
| 180 | + (Some(9), 1, ContainerInput::QuickMove) |
| 181 | + ); |
| 182 | + assert_eq!( |
| 183 | + parts(ClickOperation::PickupAll(PickupAllClick { slot: 4 })), |
| 184 | + (Some(4), 0, ContainerInput::PickupAll) |
| 185 | + ); |
| 186 | + } |
| 187 | +} |
0 commit comments