tiger_lib/gui/
builtins.rs

1use strum::VariantNames;
2use strum_macros::{Display, FromRepr, IntoStaticStr, VariantNames};
3use thiserror::Error;
4
5use crate::game::GameFlags;
6use crate::lowercase::Lowercase;
7
8/// Widget types that are defined by the game engine and don't need to be defined in gui script.
9#[derive(
10    Debug, Clone, Copy, PartialEq, Eq, Hash, IntoStaticStr, VariantNames, FromRepr, Display,
11)]
12#[allow(non_camel_case_types)]
13pub enum BuiltinWidget {
14    axis,
15    background,
16    button,
17    button_group,
18    cameracontrolwidget,
19    checkbutton,
20    colormap_picker,
21    colorpicker,
22    container,
23    contextmenu,
24    datacontext_from_model,
25    dockable_container,
26    drag_drop_icon,
27    drag_drop_target,
28    dragdropicon,
29    dragdroptarget,
30    dropdown,
31    dynamicgridbox,
32    editbox,
33    fixedgridbox,
34    flowcontainer,
35    game_button,
36    hbox,
37    icon,
38    line,
39    line_deprecated,
40    margin_widget,
41    mini_map,
42    minimap,
43    minimap_window,
44    overlappingitembox,
45    piechart,
46    pieslice,
47    plotline,
48    portrait_button,
49    progressbar,
50    right_click_menu_widget,
51    scrollarea,
52    scrollbar,
53    taborder,
54    target,
55    text_occluder,
56    textbox,
57    tools_dragdrop_widget,
58    tools_keyframe_button,
59    tools_keyframe_editor,
60    tools_keyframe_editor_lane,
61    tools_player_timeline,
62    tools_table,
63    tree,
64    treemapchart,
65    treemapslice,
66    vbox,
67    webwindow,
68    widget,
69    window,
70    zoomarea,
71}
72
73#[derive(Error, Debug)]
74pub enum TryBuiltinWidgetError {
75    #[error("builtin widget `{0}` not found")]
76    NotFound(String),
77}
78
79impl<'a> TryFrom<&Lowercase<'a>> for BuiltinWidget {
80    type Error = TryBuiltinWidgetError;
81
82    fn try_from(s: &Lowercase<'a>) -> Result<Self, Self::Error> {
83        match BuiltinWidget::VARIANTS.binary_search(&s.as_str()) {
84            // unwrap is safe here because of how VARIANTS is constructed
85            Ok(i) => Ok(BuiltinWidget::from_repr(i).unwrap()),
86            Err(_) => Err(TryBuiltinWidgetError::NotFound(s.to_string())),
87        }
88    }
89}
90
91impl BuiltinWidget {
92    /// Return which games support the given builtin widget type
93    // LAST UPDATED CK3 VERSION 1.11.3
94    // LAST UPDATED VIC3 VERSION 1.3.6
95    // LAST UPDATED IMPERATOR VERSION 2.0.3
96    // LAST UPDATED EU5 VERSION 1.1.3
97    pub fn to_game_flags(self) -> GameFlags {
98        match self {
99            BuiltinWidget::drag_drop_icon
100            | BuiltinWidget::drag_drop_target
101            | BuiltinWidget::game_button => GameFlags::Ck3,
102
103            BuiltinWidget::minimap
104            | BuiltinWidget::minimap_window
105            | BuiltinWidget::right_click_menu_widget => GameFlags::Vic3,
106
107            BuiltinWidget::dragdropicon
108            | BuiltinWidget::mini_map
109            | BuiltinWidget::dragdroptarget
110            | BuiltinWidget::target
111            | BuiltinWidget::taborder => GameFlags::Imperator,
112
113            BuiltinWidget::colormap_picker
114            | BuiltinWidget::datacontext_from_model
115            | BuiltinWidget::tools_dragdrop_widget
116            | BuiltinWidget::tools_keyframe_button
117            | BuiltinWidget::tools_keyframe_editor
118            | BuiltinWidget::tools_keyframe_editor_lane
119            | BuiltinWidget::tools_player_timeline
120            | BuiltinWidget::treemapchart
121            | BuiltinWidget::treemapslice => GameFlags::Ck3 | GameFlags::Vic3 | GameFlags::Eu5,
122
123            BuiltinWidget::button | BuiltinWidget::webwindow => {
124                GameFlags::Vic3 | GameFlags::Imperator
125            }
126
127            _ => GameFlags::all(),
128        }
129    }
130
131    pub fn builtin_current_game(s: &Lowercase) -> Option<BuiltinWidget> {
132        if let Ok(builtin) = Self::try_from(s) {
133            if builtin.to_game_flags().contains(GameFlags::game()) {
134                return Some(builtin);
135            }
136        }
137        None
138    }
139}