Skip to main content

veloren_voxygen/hud/
controller_icons.rs

1use super::img_ids::Imgs;
2use crate::{Settings, game_input::GameInput, settings::Button, window::ControllerType};
3use conrod_core::image::Id as ConrodImageId;
4use gilrs::Button as GilButton;
5
6/// returns the left trigger (dark) icon based on controller type
7pub fn fetch_skillbar_gamepad_left(ctrl_type: ControllerType, imgs: &Imgs) -> ConrodImageId {
8    match ctrl_type {
9        ControllerType::Xbox => imgs.left_trigger_xbox_dark,
10        ControllerType::Nintendo => imgs.left_trigger_nin_dark,
11        ControllerType::Playstation => imgs.left_trigger_ps_dark,
12        _ => imgs.m1_ico,
13    }
14}
15
16/// returns the right trigger (dark) icon based on controller type
17pub fn fetch_skillbar_gamepad_right(ctrl_type: ControllerType, imgs: &Imgs) -> ConrodImageId {
18    match ctrl_type {
19        ControllerType::Xbox => imgs.right_trigger_xbox_dark,
20        ControllerType::Nintendo => imgs.right_trigger_nin_dark,
21        ControllerType::Playstation => imgs.right_trigger_ps_dark,
22        _ => imgs.m2_ico,
23    }
24}
25
26/// represents an input that has no binding.
27pub const UNBOUND_KEY: &str = ":none:";
28
29/// gets a string output for the controller input
30///
31/// a multi-input action will return like ":mod2: + "mod1: + :main:"
32pub fn get_controller_input_string(
33    input: GameInput,
34    settings: &Settings,
35    ctrl: ControllerType,
36) -> Option<String> {
37    let mut icon_tags = Vec::new();
38    let unknown = Button::Simple(GilButton::Unknown);
39
40    // extract just the button name
41    let get_tag = |b: Button| {
42        let mut name = match b {
43            Button::Simple(inner) => format!("{:?}", inner),
44            _ => format!("{:?}", b),
45        };
46        match ctrl {
47            ControllerType::Xbox => name.push_str("_x"),
48            ControllerType::Nintendo => name.push_str("_n"),
49            ControllerType::Playstation => name.push_str("_p"),
50            _ => {},
51        }
52        format!(":{}:", name)
53    };
54
55    // prioritize game layers over game buttons
56    if let Some(layer) = settings.controller.get_layer_button_binding(input) {
57        // add modifiers if they aren't Unknown
58        if layer.mod2 != unknown {
59            icon_tags.push(get_tag(layer.mod2));
60        }
61        if layer.mod1 != unknown {
62            icon_tags.push(get_tag(layer.mod1));
63        }
64
65        // add the main layer button
66        icon_tags.push(get_tag(layer.button));
67    } else if let Some(button) = settings.controller.get_game_button_binding(input) {
68        icon_tags.push(get_tag(button));
69    }
70
71    if icon_tags.is_empty() {
72        None
73    } else {
74        Some(icon_tags.join(" + ").to_lowercase())
75    }
76}
77
78/// returns a ConrodImageId for valid strings
79pub fn get_controller_icon_id_from_string(name: &str, imgs: &Imgs) -> ConrodImageId {
80    // TODO: either gilrs or we have to swap nintendo buttons to be accurate. Figure
81    // it out when controller type detection is working.
82    match name {
83        "south" | "south_x" | "south_n" => imgs.south_button_a,
84        "south_p" => imgs.south_button_ps_cross,
85        "east" | "east_x" | "east_n" => imgs.east_button_b,
86        "east_p" => imgs.east_button_ps_circle,
87        "west" | "west_x" | "west_n" => imgs.west_button_x,
88        "west_p" => imgs.west_button_ps_square,
89        "north" | "north_x" | "north_n" => imgs.north_button_y,
90        "north_p" => imgs.north_button_ps_triangle,
91        "leftaxis" | "leftaxis_x" | "leftaxis_n" | "leftaxis_p" => imgs.left_axis,
92        "rightaxis" | "rightaxis_x" | "rightaxis_n" | "rightaxis_p" => imgs.right_axis,
93        "leftthumb" | "leftthumb_x" | "leftthumb_n" | "leftthumb_p" => imgs.left_axis_button,
94        "rightthumb" | "rightthumb_x" | "rightthumb_n" | "rightthumb_p" => imgs.right_axis_button,
95        // trigger is shoulder
96        "lefttrigger" | "lefttrigger_x" => imgs.left_shoulder_xbox_lb,
97        "lefttrigger_n" => imgs.left_shoulder_nin_l,
98        "lefttrigger_p" => imgs.left_shoulder_ps_l1,
99        "righttrigger" | "righttrigger_x" => imgs.right_shoulder_xbox_rb,
100        "righttrigger_n" => imgs.right_shoulder_nin_r,
101        "righttrigger_p" => imgs.right_shoulder_ps_r1,
102        // trigger2 is trigger
103        "lefttrigger2" | "lefttrigger2_x" => imgs.left_trigger_xbox_lt,
104        "lefttrigger2_n" => imgs.left_trigger_nin_zl,
105        "lefttrigger2_p" => imgs.left_trigger_ps_l2,
106        "righttrigger2" | "righttrigger2_x" => imgs.right_trigger_xbox_rt,
107        "righttrigger2_n" => imgs.right_trigger_nin_zr,
108        "righttrigger2_p" => imgs.right_trigger_ps_r2,
109        "dpaddown" | "dpaddown_x" | "dpaddown_n" => imgs.dpad_down,
110        "dpaddown_p" => imgs.dpad_down_ps,
111        "dpadleft" | "dpadleft_x" | "dpadleft_n" => imgs.dpad_left,
112        "dpadleft_p" => imgs.dpad_left_ps,
113        "dpadright" | "dpadright_x" | "dpadright_n" => imgs.dpad_right,
114        "dpadright_p" => imgs.dpad_right_ps,
115        "dpadup" | "dpadup_x" | "dpadup_n" => imgs.dpad_up,
116        "dpadup_p" => imgs.dpad_up_ps,
117        "start" | "start_x" => imgs.start_button_xbox,
118        "start_n" => imgs.start_button_nin,
119        "start_p" => imgs.start_button_ps,
120        "select" | "select_x" => imgs.select_button_xbox,
121        "select_n" => imgs.select_button_nin,
122        "select_p" => imgs.select_button_ps,
123        "mode" => imgs.start_button_xbox, // TODO: add `mode` button for xbox, nin, ps
124        // gilrs supports c and z buttons, so here they are (I'm not making custom icons though)
125        "c" => imgs.south_button_a,
126        "z" => imgs.east_button_b,
127        _ => imgs.no_button,
128    }
129}