Struct mooeye::ui::Transition
source · pub struct Transition<T: Copy + Eq + Hash> { /* private fields */ }
Expand description
A Transition stuct that can be added to an UiElement to slowly change that elements properties over time. A transition can change the elements layout, visuals, hover_visuals, content and tooltip by first augmenting the transition with the relevant methods.
Implementations§
source§impl<T: Copy + Eq + Hash> Transition<T>
impl<T: Copy + Eq + Hash> Transition<T>
sourcepub fn new(duration: Duration) -> Self
pub fn new(duration: Duration) -> Self
Creates a new transition with the specified duration and all possible augmentations set to none. Can be used without adding changes to delay transitions added later.
Examples found in repository?
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
pub fn new(ctx: &Context) -> GameResult<Self> {
// This title will change based on transitions whenever certain buttons are clicked.
let title = Text::new("Move this element with the buttons.\nYou have not yet clicked a button.")
// First, we style the title.
.set_font("Bahnschrift")
.set_scale(28.)
.to_owned()
.to_element_builder(0, ctx)
// Then, we add a message handler to the element.
.with_message_handler(
// The message handle receives a set of messages and a mutable transition vector.
|messages,_,transitions| {
let ids = [11,12,13,21,22,23,];
// We check if any of the buttons that interest us were clicked in the last frame.
for id in ids{
for message in messages{
if *message == ui::UiMessage::<()>::Clicked(id){
// If yes, we add a new transition to the vector.
transitions.push_back(
// Transitions are initalized with the duration they should take to complete and augmented via the builder pattern.
ui::Transition::new(Duration::ZERO)
// Here, we add a new content that will replace the old text once the transition completes.
.with_new_content(Text::new(format!(
"Move this element with the buttons.\nYou clicked a button with id {}.",
id
))
.set_font("Bahnschrift")
.set_scale(24.)
.to_owned())
)
}
}
}
})
.build();
// Define a general visual style to use for all buttons.
let vis = ui::Visuals::new(
Color::from_rgb(77, 109, 191),
Color::from_rgb(55, 67, 87),
2.,
4.,
);
// Create a grid box to hold all buttons.
let mut grid_box = ui::containers::GridBox::new(2, 3);
// Now, we create 6 buttons to move the element to all possible vertical and horizontal alignments and add them to the grid.
let vert_up = Text::new(" ^ ")
.set_font("Bahnschrift")
.to_owned()
.to_element_builder(11, ctx)
.with_visuals(vis)
// We can also set a sound to be played on click/key press
.with_trigger_sound(ggez::audio::Source::new(ctx, "/blipSelect.wav").ok())
.build();
grid_box
.add(vert_up, 0, 0)?;
let vert_ce = Text::new(" . ")
.set_font("Bahnschrift")
.to_owned()
.to_element_builder(12, ctx)
.with_visuals(vis)
.build();
grid_box
.add(vert_ce, 0, 1)?;
let vert_do = Text::new(" v ")
.set_font("Bahnschrift")
.to_owned().to_element_builder(13, ctx)
.with_visuals(vis)
.build();
grid_box
.add(vert_do, 0, 2)?;
let hor_up = Text::new(" < ")
.set_font("Bahnschrift")
.to_owned().to_element_builder(21, ctx)
.with_visuals(vis)
.build();
grid_box
.add(hor_up, 1, 0)?;
let hor_ce = Text::new(" . ")
.set_font("Bahnschrift")
.to_owned().to_element_builder(22, ctx)
.with_visuals(vis)
.build();
grid_box
.add(hor_ce, 1, 1)?;
let hor_do = Text::new(" > ")
.set_font("Bahnschrift")
.to_owned().to_element_builder(23, ctx)
.with_visuals(vis)
.build();
grid_box
.add(hor_do, 1, 2)?;
// The well-known back button will take us back to scene select.
let back = Text::new("Back")
.set_font("Bahnschrift")
.to_owned()
.to_element_builder(1, ctx)
.with_visuals(vis)
.as_fill()
.build();
// We create a general VBox to contain our UI
// We can create Vertical and Horizontal Boxes with the 'spaced' constructor to set its spacing value.
let gui_box = ui::containers::VerticalBox::new_spaced(6.)
.to_element_builder(0, ctx)
// We put the title, grid and back button together in a box.
.with_child(title)
.with_child(grid_box.to_element(30, ctx))
.with_child(back)
.with_size(ui::Size::Shrink(128., f32::INFINITY), ui::Size::Shrink(0., f32::INFINITY))
.with_visuals(ui::Visuals::new(
Color::from_rgb(120, 170, 200),
Color::from_rgb(55, 67, 87),
2.,
0.,
))
// Another message handler. Along with the messages and transition vector as discussed above, we also receive a layout struct
// that represents the layout struct of the element in the moment the transition was initialized. This allows us to only change
// the relevant parts of the layout without having to recreate it from scratch.
.with_message_handler(|messages, layout, transitions| {
// This guards prevent spam clicking a button from locking up the element with 1.5-second transitions.
if !transitions.is_empty() {
return;
}
let vert_map = HashMap::from([
(11, ui::Alignment::Min),
(12, ui::Alignment::Center),
(13, ui::Alignment::Max),
]);
let hor_map = HashMap::from([
(21, ui::Alignment::Min),
(22, ui::Alignment::Center),
(23, ui::Alignment::Max),
]);
// Check if any vertical buttons were clicked.
for (key, val) in vert_map {
if messages.contains(&ui::UiMessage::Triggered(key)) {
transitions.push_back(
// If yes, add a transition.
ui::Transition::new(Duration::from_secs_f32(1.5))
// This time, we don't change the content, but the layout.
// Layout, visuals and hover_visuals are not changed on completion like content, but instead applied gradually.
.with_new_layout(ui::Layout{
y_alignment: val,
..layout
}),
);
}
}
// Repeat for horizontal keys.
for (key, val) in hor_map {
if messages.contains(&ui::UiMessage::Triggered(key)) {
transitions.push_back(
ui::Transition::new(Duration::from_secs_f32(1.5)).with_new_layout(ui::Layout{
x_alignment: val,
..layout
}),
);
}
}
})
.build();
// Finally, we wrap our gui_box into a space-filling stack pane so we have a place to later add further elements
Ok(Self {
gui: ui::containers::StackBox::new()
.to_element_builder(100, ctx)
.as_fill()
.with_child(gui_box)
.build()
})
}
sourcepub fn with_new_layout(self, new_layout: Layout) -> Self
pub fn with_new_layout(self, new_layout: Layout) -> Self
Augments this transition to now (gradually) change the layout of the UiElement it is added to, moving it smoothly between the two positions.
Examples found in repository?
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
pub fn new(ctx: &Context) -> GameResult<Self> {
// This title will change based on transitions whenever certain buttons are clicked.
let title = Text::new("Move this element with the buttons.\nYou have not yet clicked a button.")
// First, we style the title.
.set_font("Bahnschrift")
.set_scale(28.)
.to_owned()
.to_element_builder(0, ctx)
// Then, we add a message handler to the element.
.with_message_handler(
// The message handle receives a set of messages and a mutable transition vector.
|messages,_,transitions| {
let ids = [11,12,13,21,22,23,];
// We check if any of the buttons that interest us were clicked in the last frame.
for id in ids{
for message in messages{
if *message == ui::UiMessage::<()>::Clicked(id){
// If yes, we add a new transition to the vector.
transitions.push_back(
// Transitions are initalized with the duration they should take to complete and augmented via the builder pattern.
ui::Transition::new(Duration::ZERO)
// Here, we add a new content that will replace the old text once the transition completes.
.with_new_content(Text::new(format!(
"Move this element with the buttons.\nYou clicked a button with id {}.",
id
))
.set_font("Bahnschrift")
.set_scale(24.)
.to_owned())
)
}
}
}
})
.build();
// Define a general visual style to use for all buttons.
let vis = ui::Visuals::new(
Color::from_rgb(77, 109, 191),
Color::from_rgb(55, 67, 87),
2.,
4.,
);
// Create a grid box to hold all buttons.
let mut grid_box = ui::containers::GridBox::new(2, 3);
// Now, we create 6 buttons to move the element to all possible vertical and horizontal alignments and add them to the grid.
let vert_up = Text::new(" ^ ")
.set_font("Bahnschrift")
.to_owned()
.to_element_builder(11, ctx)
.with_visuals(vis)
// We can also set a sound to be played on click/key press
.with_trigger_sound(ggez::audio::Source::new(ctx, "/blipSelect.wav").ok())
.build();
grid_box
.add(vert_up, 0, 0)?;
let vert_ce = Text::new(" . ")
.set_font("Bahnschrift")
.to_owned()
.to_element_builder(12, ctx)
.with_visuals(vis)
.build();
grid_box
.add(vert_ce, 0, 1)?;
let vert_do = Text::new(" v ")
.set_font("Bahnschrift")
.to_owned().to_element_builder(13, ctx)
.with_visuals(vis)
.build();
grid_box
.add(vert_do, 0, 2)?;
let hor_up = Text::new(" < ")
.set_font("Bahnschrift")
.to_owned().to_element_builder(21, ctx)
.with_visuals(vis)
.build();
grid_box
.add(hor_up, 1, 0)?;
let hor_ce = Text::new(" . ")
.set_font("Bahnschrift")
.to_owned().to_element_builder(22, ctx)
.with_visuals(vis)
.build();
grid_box
.add(hor_ce, 1, 1)?;
let hor_do = Text::new(" > ")
.set_font("Bahnschrift")
.to_owned().to_element_builder(23, ctx)
.with_visuals(vis)
.build();
grid_box
.add(hor_do, 1, 2)?;
// The well-known back button will take us back to scene select.
let back = Text::new("Back")
.set_font("Bahnschrift")
.to_owned()
.to_element_builder(1, ctx)
.with_visuals(vis)
.as_fill()
.build();
// We create a general VBox to contain our UI
// We can create Vertical and Horizontal Boxes with the 'spaced' constructor to set its spacing value.
let gui_box = ui::containers::VerticalBox::new_spaced(6.)
.to_element_builder(0, ctx)
// We put the title, grid and back button together in a box.
.with_child(title)
.with_child(grid_box.to_element(30, ctx))
.with_child(back)
.with_size(ui::Size::Shrink(128., f32::INFINITY), ui::Size::Shrink(0., f32::INFINITY))
.with_visuals(ui::Visuals::new(
Color::from_rgb(120, 170, 200),
Color::from_rgb(55, 67, 87),
2.,
0.,
))
// Another message handler. Along with the messages and transition vector as discussed above, we also receive a layout struct
// that represents the layout struct of the element in the moment the transition was initialized. This allows us to only change
// the relevant parts of the layout without having to recreate it from scratch.
.with_message_handler(|messages, layout, transitions| {
// This guards prevent spam clicking a button from locking up the element with 1.5-second transitions.
if !transitions.is_empty() {
return;
}
let vert_map = HashMap::from([
(11, ui::Alignment::Min),
(12, ui::Alignment::Center),
(13, ui::Alignment::Max),
]);
let hor_map = HashMap::from([
(21, ui::Alignment::Min),
(22, ui::Alignment::Center),
(23, ui::Alignment::Max),
]);
// Check if any vertical buttons were clicked.
for (key, val) in vert_map {
if messages.contains(&ui::UiMessage::Triggered(key)) {
transitions.push_back(
// If yes, add a transition.
ui::Transition::new(Duration::from_secs_f32(1.5))
// This time, we don't change the content, but the layout.
// Layout, visuals and hover_visuals are not changed on completion like content, but instead applied gradually.
.with_new_layout(ui::Layout{
y_alignment: val,
..layout
}),
);
}
}
// Repeat for horizontal keys.
for (key, val) in hor_map {
if messages.contains(&ui::UiMessage::Triggered(key)) {
transitions.push_back(
ui::Transition::new(Duration::from_secs_f32(1.5)).with_new_layout(ui::Layout{
x_alignment: val,
..layout
}),
);
}
}
})
.build();
// Finally, we wrap our gui_box into a space-filling stack pane so we have a place to later add further elements
Ok(Self {
gui: ui::containers::StackBox::new()
.to_element_builder(100, ctx)
.as_fill()
.with_child(gui_box)
.build()
})
}
sourcepub fn with_new_visuals(self, new_visuals: Visuals) -> Self
pub fn with_new_visuals(self, new_visuals: Visuals) -> Self
Augments the transition to now (gradually) change the visuals of the UiElement it is added to, blending over smoothly.
sourcepub fn with_new_hover_visuals(self, new_hover_visuals: Option<Visuals>) -> Self
pub fn with_new_hover_visuals(self, new_hover_visuals: Option<Visuals>) -> Self
Augments this transition to now (gradually) change the visuals of the UiElement it is added to when hovered, blending over smoothly. Can be set to None to remove any special visuals when hovered. In this case, the transition will make the elements hover visuals slowly blend to the elements non-hover visuals.
sourcepub fn with_new_content<E>(self, new_content: E) -> Selfwhere
E: UiContent<T> + 'static,
pub fn with_new_content<E>(self, new_content: E) -> Selfwhere E: UiContent<T> + 'static,
Augment this transition to now change the content of the UiElement it is added to. This change happens in a single frame as soon as the transitions duration has elapsed.
Examples found in repository?
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
pub fn new(ctx: &Context) -> GameResult<Self> {
// This title will change based on transitions whenever certain buttons are clicked.
let title = Text::new("Move this element with the buttons.\nYou have not yet clicked a button.")
// First, we style the title.
.set_font("Bahnschrift")
.set_scale(28.)
.to_owned()
.to_element_builder(0, ctx)
// Then, we add a message handler to the element.
.with_message_handler(
// The message handle receives a set of messages and a mutable transition vector.
|messages,_,transitions| {
let ids = [11,12,13,21,22,23,];
// We check if any of the buttons that interest us were clicked in the last frame.
for id in ids{
for message in messages{
if *message == ui::UiMessage::<()>::Clicked(id){
// If yes, we add a new transition to the vector.
transitions.push_back(
// Transitions are initalized with the duration they should take to complete and augmented via the builder pattern.
ui::Transition::new(Duration::ZERO)
// Here, we add a new content that will replace the old text once the transition completes.
.with_new_content(Text::new(format!(
"Move this element with the buttons.\nYou clicked a button with id {}.",
id
))
.set_font("Bahnschrift")
.set_scale(24.)
.to_owned())
)
}
}
}
})
.build();
// Define a general visual style to use for all buttons.
let vis = ui::Visuals::new(
Color::from_rgb(77, 109, 191),
Color::from_rgb(55, 67, 87),
2.,
4.,
);
// Create a grid box to hold all buttons.
let mut grid_box = ui::containers::GridBox::new(2, 3);
// Now, we create 6 buttons to move the element to all possible vertical and horizontal alignments and add them to the grid.
let vert_up = Text::new(" ^ ")
.set_font("Bahnschrift")
.to_owned()
.to_element_builder(11, ctx)
.with_visuals(vis)
// We can also set a sound to be played on click/key press
.with_trigger_sound(ggez::audio::Source::new(ctx, "/blipSelect.wav").ok())
.build();
grid_box
.add(vert_up, 0, 0)?;
let vert_ce = Text::new(" . ")
.set_font("Bahnschrift")
.to_owned()
.to_element_builder(12, ctx)
.with_visuals(vis)
.build();
grid_box
.add(vert_ce, 0, 1)?;
let vert_do = Text::new(" v ")
.set_font("Bahnschrift")
.to_owned().to_element_builder(13, ctx)
.with_visuals(vis)
.build();
grid_box
.add(vert_do, 0, 2)?;
let hor_up = Text::new(" < ")
.set_font("Bahnschrift")
.to_owned().to_element_builder(21, ctx)
.with_visuals(vis)
.build();
grid_box
.add(hor_up, 1, 0)?;
let hor_ce = Text::new(" . ")
.set_font("Bahnschrift")
.to_owned().to_element_builder(22, ctx)
.with_visuals(vis)
.build();
grid_box
.add(hor_ce, 1, 1)?;
let hor_do = Text::new(" > ")
.set_font("Bahnschrift")
.to_owned().to_element_builder(23, ctx)
.with_visuals(vis)
.build();
grid_box
.add(hor_do, 1, 2)?;
// The well-known back button will take us back to scene select.
let back = Text::new("Back")
.set_font("Bahnschrift")
.to_owned()
.to_element_builder(1, ctx)
.with_visuals(vis)
.as_fill()
.build();
// We create a general VBox to contain our UI
// We can create Vertical and Horizontal Boxes with the 'spaced' constructor to set its spacing value.
let gui_box = ui::containers::VerticalBox::new_spaced(6.)
.to_element_builder(0, ctx)
// We put the title, grid and back button together in a box.
.with_child(title)
.with_child(grid_box.to_element(30, ctx))
.with_child(back)
.with_size(ui::Size::Shrink(128., f32::INFINITY), ui::Size::Shrink(0., f32::INFINITY))
.with_visuals(ui::Visuals::new(
Color::from_rgb(120, 170, 200),
Color::from_rgb(55, 67, 87),
2.,
0.,
))
// Another message handler. Along with the messages and transition vector as discussed above, we also receive a layout struct
// that represents the layout struct of the element in the moment the transition was initialized. This allows us to only change
// the relevant parts of the layout without having to recreate it from scratch.
.with_message_handler(|messages, layout, transitions| {
// This guards prevent spam clicking a button from locking up the element with 1.5-second transitions.
if !transitions.is_empty() {
return;
}
let vert_map = HashMap::from([
(11, ui::Alignment::Min),
(12, ui::Alignment::Center),
(13, ui::Alignment::Max),
]);
let hor_map = HashMap::from([
(21, ui::Alignment::Min),
(22, ui::Alignment::Center),
(23, ui::Alignment::Max),
]);
// Check if any vertical buttons were clicked.
for (key, val) in vert_map {
if messages.contains(&ui::UiMessage::Triggered(key)) {
transitions.push_back(
// If yes, add a transition.
ui::Transition::new(Duration::from_secs_f32(1.5))
// This time, we don't change the content, but the layout.
// Layout, visuals and hover_visuals are not changed on completion like content, but instead applied gradually.
.with_new_layout(ui::Layout{
y_alignment: val,
..layout
}),
);
}
}
// Repeat for horizontal keys.
for (key, val) in hor_map {
if messages.contains(&ui::UiMessage::Triggered(key)) {
transitions.push_back(
ui::Transition::new(Duration::from_secs_f32(1.5)).with_new_layout(ui::Layout{
x_alignment: val,
..layout
}),
);
}
}
})
.build();
// Finally, we wrap our gui_box into a space-filling stack pane so we have a place to later add further elements
Ok(Self {
gui: ui::containers::StackBox::new()
.to_element_builder(100, ctx)
.as_fill()
.with_child(gui_box)
.build()
})
}
sourcepub fn with_new_tooltip(self, new_tooltip: Option<UiElement<T>>) -> Self
pub fn with_new_tooltip(self, new_tooltip: Option<UiElement<T>>) -> Self
Augment this transition to now change the tooltip of the UiElement it is added to. This change happens in a single frame as soon as the transitions duration has elapsed.