pub struct Button<'a> { /* private fields */ }
Implementations§
source§impl<'a> Button<'a>
impl<'a> Button<'a>
sourcepub fn new<S>(content: S) -> Button<'a>
pub fn new<S>(content: S) -> Button<'a>
Examples found in repository?
examples/ui.rs (line 209)
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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
async fn main() {
let mut data = Data::new();
let mut data0 = String::new();
let mut data1 = String::new();
let mut text0 = String::new();
let mut text1 = String::new();
let mut number0 = 0.;
let mut number1 = 0.;
let texture: Texture2D = load_texture("examples/ferris.png").await.unwrap();
loop {
clear_background(WHITE);
widgets::Window::new(hash!(), vec2(400., 200.), vec2(320., 400.))
.label("Shop")
.titlebar(true)
.ui(&mut *root_ui(), |ui| {
for i in 0..30 {
Group::new(hash!("shop", i), Vec2::new(300., 80.)).ui(ui, |ui| {
ui.label(Vec2::new(10., 10.), &format!("Item N {}", i));
ui.label(Vec2::new(260., 40.), "10/10");
ui.label(Vec2::new(200., 58.), &format!("{} kr", 800));
if ui.button(Vec2::new(260., 55.), "buy") {
data.inventory.push(format!("Item {}", i));
}
});
}
});
widgets::Window::new(hash!(), vec2(100., 220.), vec2(542., 430.))
.label("Fitting window")
.titlebar(true)
.ui(&mut *root_ui(), |ui| {
Group::new(hash!(), Vec2::new(230., 400.)).ui(ui, |ui| {
data.slots(ui);
});
Group::new(hash!(), Vec2::new(280., 400.)).ui(ui, |ui| {
data.inventory(ui);
});
});
widgets::Window::new(hash!(), vec2(470., 50.), vec2(300., 300.))
.label("Megaui Showcase Window")
.ui(&mut *root_ui(), |ui| {
ui.tree_node(hash!(), "input", |ui| {
ui.label(None, "Some random text");
if ui.button(None, "click me") {
println!("hi");
}
ui.separator();
ui.label(None, "Some other random text");
if ui.button(None, "other button") {
println!("hi2");
}
ui.separator();
ui.input_text(hash!(), "<- input text 1", &mut data0);
ui.input_text(hash!(), "<- input text 2", &mut data1);
ui.label(
None,
&format!("Text entered: \"{}\" and \"{}\"", data0, data1),
);
ui.separator();
});
ui.tree_node(hash!(), "buttons", |ui| {
widgets::Button::new(texture.clone())
.size(vec2(120., 70.))
.ui(ui);
ui.same_line(0.);
widgets::Button::new("Button").size(vec2(120., 70.)).ui(ui);
widgets::Button::new("Button").size(vec2(120., 70.)).ui(ui);
ui.same_line(0.);
widgets::Button::new(texture.clone())
.size(vec2(120., 70.))
.ui(ui);
});
ui.tree_node(hash!(), "sliders", |ui| {
ui.slider(hash!(), "[-10 .. 10]", -10f32..10f32, &mut number0);
ui.slider(hash!(), "[0 .. 100]", 0f32..100f32, &mut number1);
});
ui.tree_node(hash!(), "editbox 1", |ui| {
ui.label(None, "This is editbox!");
ui.editbox(hash!(), vec2(285., 165.), &mut text0);
});
ui.tree_node(hash!(), "editbox 2", |ui| {
ui.label(None, "This is editbox!");
ui.editbox(hash!(), vec2(285., 165.), &mut text1);
});
});
match data.fit_command.take() {
Some(FittingCommand::Unfit { target_slot }) => data.set_item(target_slot, None),
Some(FittingCommand::Fit { target_slot, item }) => {
data.set_item(target_slot, Some(item));
}
Some(FittingCommand::Refit {
target_slot,
origin_slot,
}) => {
let origin_item = data
.slots
.iter()
.find_map(|(_, slot)| {
if slot.id == origin_slot {
Some(slot.item.clone())
} else {
None
}
})
.flatten();
data.set_item(target_slot, origin_item);
data.set_item(origin_slot, None);
}
None => {}
};
next_frame().await;
}
}
More examples
examples/ui_skins.rs (line 242)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
async fn main() {
let skin1 = {
let label_style = root_ui()
.style_builder()
.font(include_bytes!("../examples/ui_assets/HTOWERT.TTF"))
.unwrap()
.text_color(Color::from_rgba(180, 180, 120, 255))
.font_size(30)
.build();
let window_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/window_background.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(20.0, 20.0, 10.0, 10.0))
.margin(RectOffset::new(-20.0, -30.0, 0.0, 0.0))
.build();
let button_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_background.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(37.0, 37.0, 5.0, 5.0))
.margin(RectOffset::new(10.0, 10.0, 0.0, 0.0))
.background_hovered(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_hovered_background.png"),
None,
)
.unwrap(),
)
.background_clicked(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_clicked_background.png"),
None,
)
.unwrap(),
)
.font(include_bytes!("../examples/ui_assets/HTOWERT.TTF"))
.unwrap()
.text_color(Color::from_rgba(180, 180, 100, 255))
.font_size(40)
.build();
let editbox_style = root_ui()
.style_builder()
.background_margin(RectOffset::new(0., 0., 0., 0.))
.font(include_bytes!("../examples/ui_assets/HTOWERT.TTF"))
.unwrap()
.text_color(Color::from_rgba(120, 120, 120, 255))
.color_selected(Color::from_rgba(190, 190, 190, 255))
.font_size(50)
.build();
Skin {
editbox_style,
window_style,
button_style,
label_style,
..root_ui().default_skin()
}
};
let skin2 = {
let label_style = root_ui()
.style_builder()
.font(include_bytes!("../examples/ui_assets/MinimalPixel v2.ttf"))
.unwrap()
.text_color(Color::from_rgba(120, 120, 120, 255))
.font_size(25)
.build();
let window_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/window_background_2.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(52.0, 52.0, 52.0, 52.0))
.margin(RectOffset::new(-30.0, 0.0, -30.0, 0.0))
.build();
let button_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_background_2.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(8.0, 8.0, 8.0, 8.0))
.background_hovered(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_hovered_background_2.png"),
None,
)
.unwrap(),
)
.background_clicked(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_clicked_background_2.png"),
None,
)
.unwrap(),
)
.font(include_bytes!("../examples/ui_assets/MinimalPixel v2.ttf"))
.unwrap()
.text_color(Color::from_rgba(180, 180, 100, 255))
.font_size(40)
.build();
let checkbox_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/checkbox_background.png"),
None,
)
.unwrap(),
)
.background_hovered(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/checkbox_hovered_background.png"),
None,
)
.unwrap(),
)
.background_clicked(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/checkbox_clicked_background.png"),
None,
)
.unwrap(),
)
.build();
let editbox_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/editbox_background.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(2., 2., 2., 2.))
.font(include_bytes!("../examples/ui_assets/MinimalPixel v2.ttf"))
.unwrap()
.text_color(Color::from_rgba(120, 120, 120, 255))
.font_size(25)
.build();
let combobox_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/combobox_background.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(4., 25., 6., 6.))
.font(include_bytes!("../examples/ui_assets/MinimalPixel v2.ttf"))
.unwrap()
.text_color(Color::from_rgba(120, 120, 120, 255))
.color(Color::from_rgba(210, 210, 210, 255))
.font_size(25)
.build();
Skin {
window_style,
button_style,
label_style,
checkbox_style,
editbox_style,
combobox_style,
..root_ui().default_skin()
}
};
let default_skin = root_ui().default_skin().clone();
let mut window1_skin = skin1.clone();
let mut window2_skin = skin2.clone();
let mut checkbox = false;
let mut text = String::new();
let mut number = 0.0f32;
let mut combobox = 0;
loop {
clear_background(GRAY);
root_ui().group(hash!(), vec2(70.0, 100.0), |ui| {
ui.label(None, "Window 1");
if ui.button(None, "Skin 1") {
window1_skin = skin1.clone();
}
if ui.button(None, "Skin 2") {
window1_skin = skin2.clone();
}
if ui.button(None, "No Skin") {
window1_skin = default_skin.clone();
}
});
root_ui().same_line(0.);
root_ui().group(hash!(), vec2(70.0, 100.0), |ui| {
ui.label(None, "Window 2");
if ui.button(None, "Skin 1") {
window2_skin = skin1.clone();
}
if ui.button(None, "Skin 2") {
window2_skin = skin2.clone();
}
if ui.button(None, "No Skin") {
window2_skin = default_skin.clone();
}
});
root_ui().push_skin(&window1_skin);
root_ui().window(hash!(), vec2(20., 250.), vec2(300., 300.), |ui| {
widgets::Button::new("Play")
.position(vec2(65.0, 15.0))
.ui(ui);
widgets::Button::new("Options")
.position(vec2(40.0, 75.0))
.ui(ui);
widgets::Button::new("Quit")
.position(vec2(65.0, 195.0))
.ui(ui);
});
root_ui().pop_skin();
root_ui().push_skin(&window2_skin);
root_ui().window(hash!(), vec2(250., 20.), vec2(500., 250.), |ui| {
ui.checkbox(hash!(), "Checkbox 1", &mut checkbox);
ui.combo_box(
hash!(),
"Combobox",
&["First option", "Second option"],
&mut combobox,
);
ui.input_text(hash!(), "Text", &mut text);
ui.drag(hash!(), "Drag", None, &mut number);
widgets::Button::new("Apply")
.position(vec2(80.0, 150.0))
.ui(ui);
widgets::Button::new("Cancel")
.position(vec2(280.0, 150.0))
.ui(ui);
});
root_ui().pop_skin();
next_frame().await;
}
}
sourcepub fn position<P: Into<Option<Vec2>>>(self, position: P) -> Self
pub fn position<P: Into<Option<Vec2>>>(self, position: P) -> Self
Examples found in repository?
examples/ui_skins.rs (line 243)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
async fn main() {
let skin1 = {
let label_style = root_ui()
.style_builder()
.font(include_bytes!("../examples/ui_assets/HTOWERT.TTF"))
.unwrap()
.text_color(Color::from_rgba(180, 180, 120, 255))
.font_size(30)
.build();
let window_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/window_background.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(20.0, 20.0, 10.0, 10.0))
.margin(RectOffset::new(-20.0, -30.0, 0.0, 0.0))
.build();
let button_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_background.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(37.0, 37.0, 5.0, 5.0))
.margin(RectOffset::new(10.0, 10.0, 0.0, 0.0))
.background_hovered(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_hovered_background.png"),
None,
)
.unwrap(),
)
.background_clicked(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_clicked_background.png"),
None,
)
.unwrap(),
)
.font(include_bytes!("../examples/ui_assets/HTOWERT.TTF"))
.unwrap()
.text_color(Color::from_rgba(180, 180, 100, 255))
.font_size(40)
.build();
let editbox_style = root_ui()
.style_builder()
.background_margin(RectOffset::new(0., 0., 0., 0.))
.font(include_bytes!("../examples/ui_assets/HTOWERT.TTF"))
.unwrap()
.text_color(Color::from_rgba(120, 120, 120, 255))
.color_selected(Color::from_rgba(190, 190, 190, 255))
.font_size(50)
.build();
Skin {
editbox_style,
window_style,
button_style,
label_style,
..root_ui().default_skin()
}
};
let skin2 = {
let label_style = root_ui()
.style_builder()
.font(include_bytes!("../examples/ui_assets/MinimalPixel v2.ttf"))
.unwrap()
.text_color(Color::from_rgba(120, 120, 120, 255))
.font_size(25)
.build();
let window_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/window_background_2.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(52.0, 52.0, 52.0, 52.0))
.margin(RectOffset::new(-30.0, 0.0, -30.0, 0.0))
.build();
let button_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_background_2.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(8.0, 8.0, 8.0, 8.0))
.background_hovered(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_hovered_background_2.png"),
None,
)
.unwrap(),
)
.background_clicked(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_clicked_background_2.png"),
None,
)
.unwrap(),
)
.font(include_bytes!("../examples/ui_assets/MinimalPixel v2.ttf"))
.unwrap()
.text_color(Color::from_rgba(180, 180, 100, 255))
.font_size(40)
.build();
let checkbox_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/checkbox_background.png"),
None,
)
.unwrap(),
)
.background_hovered(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/checkbox_hovered_background.png"),
None,
)
.unwrap(),
)
.background_clicked(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/checkbox_clicked_background.png"),
None,
)
.unwrap(),
)
.build();
let editbox_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/editbox_background.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(2., 2., 2., 2.))
.font(include_bytes!("../examples/ui_assets/MinimalPixel v2.ttf"))
.unwrap()
.text_color(Color::from_rgba(120, 120, 120, 255))
.font_size(25)
.build();
let combobox_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/combobox_background.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(4., 25., 6., 6.))
.font(include_bytes!("../examples/ui_assets/MinimalPixel v2.ttf"))
.unwrap()
.text_color(Color::from_rgba(120, 120, 120, 255))
.color(Color::from_rgba(210, 210, 210, 255))
.font_size(25)
.build();
Skin {
window_style,
button_style,
label_style,
checkbox_style,
editbox_style,
combobox_style,
..root_ui().default_skin()
}
};
let default_skin = root_ui().default_skin().clone();
let mut window1_skin = skin1.clone();
let mut window2_skin = skin2.clone();
let mut checkbox = false;
let mut text = String::new();
let mut number = 0.0f32;
let mut combobox = 0;
loop {
clear_background(GRAY);
root_ui().group(hash!(), vec2(70.0, 100.0), |ui| {
ui.label(None, "Window 1");
if ui.button(None, "Skin 1") {
window1_skin = skin1.clone();
}
if ui.button(None, "Skin 2") {
window1_skin = skin2.clone();
}
if ui.button(None, "No Skin") {
window1_skin = default_skin.clone();
}
});
root_ui().same_line(0.);
root_ui().group(hash!(), vec2(70.0, 100.0), |ui| {
ui.label(None, "Window 2");
if ui.button(None, "Skin 1") {
window2_skin = skin1.clone();
}
if ui.button(None, "Skin 2") {
window2_skin = skin2.clone();
}
if ui.button(None, "No Skin") {
window2_skin = default_skin.clone();
}
});
root_ui().push_skin(&window1_skin);
root_ui().window(hash!(), vec2(20., 250.), vec2(300., 300.), |ui| {
widgets::Button::new("Play")
.position(vec2(65.0, 15.0))
.ui(ui);
widgets::Button::new("Options")
.position(vec2(40.0, 75.0))
.ui(ui);
widgets::Button::new("Quit")
.position(vec2(65.0, 195.0))
.ui(ui);
});
root_ui().pop_skin();
root_ui().push_skin(&window2_skin);
root_ui().window(hash!(), vec2(250., 20.), vec2(500., 250.), |ui| {
ui.checkbox(hash!(), "Checkbox 1", &mut checkbox);
ui.combo_box(
hash!(),
"Combobox",
&["First option", "Second option"],
&mut combobox,
);
ui.input_text(hash!(), "Text", &mut text);
ui.drag(hash!(), "Drag", None, &mut number);
widgets::Button::new("Apply")
.position(vec2(80.0, 150.0))
.ui(ui);
widgets::Button::new("Cancel")
.position(vec2(280.0, 150.0))
.ui(ui);
});
root_ui().pop_skin();
next_frame().await;
}
}
sourcepub fn size(self, size: Vec2) -> Self
pub fn size(self, size: Vec2) -> Self
Examples found in repository?
examples/ui.rs (line 210)
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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
async fn main() {
let mut data = Data::new();
let mut data0 = String::new();
let mut data1 = String::new();
let mut text0 = String::new();
let mut text1 = String::new();
let mut number0 = 0.;
let mut number1 = 0.;
let texture: Texture2D = load_texture("examples/ferris.png").await.unwrap();
loop {
clear_background(WHITE);
widgets::Window::new(hash!(), vec2(400., 200.), vec2(320., 400.))
.label("Shop")
.titlebar(true)
.ui(&mut *root_ui(), |ui| {
for i in 0..30 {
Group::new(hash!("shop", i), Vec2::new(300., 80.)).ui(ui, |ui| {
ui.label(Vec2::new(10., 10.), &format!("Item N {}", i));
ui.label(Vec2::new(260., 40.), "10/10");
ui.label(Vec2::new(200., 58.), &format!("{} kr", 800));
if ui.button(Vec2::new(260., 55.), "buy") {
data.inventory.push(format!("Item {}", i));
}
});
}
});
widgets::Window::new(hash!(), vec2(100., 220.), vec2(542., 430.))
.label("Fitting window")
.titlebar(true)
.ui(&mut *root_ui(), |ui| {
Group::new(hash!(), Vec2::new(230., 400.)).ui(ui, |ui| {
data.slots(ui);
});
Group::new(hash!(), Vec2::new(280., 400.)).ui(ui, |ui| {
data.inventory(ui);
});
});
widgets::Window::new(hash!(), vec2(470., 50.), vec2(300., 300.))
.label("Megaui Showcase Window")
.ui(&mut *root_ui(), |ui| {
ui.tree_node(hash!(), "input", |ui| {
ui.label(None, "Some random text");
if ui.button(None, "click me") {
println!("hi");
}
ui.separator();
ui.label(None, "Some other random text");
if ui.button(None, "other button") {
println!("hi2");
}
ui.separator();
ui.input_text(hash!(), "<- input text 1", &mut data0);
ui.input_text(hash!(), "<- input text 2", &mut data1);
ui.label(
None,
&format!("Text entered: \"{}\" and \"{}\"", data0, data1),
);
ui.separator();
});
ui.tree_node(hash!(), "buttons", |ui| {
widgets::Button::new(texture.clone())
.size(vec2(120., 70.))
.ui(ui);
ui.same_line(0.);
widgets::Button::new("Button").size(vec2(120., 70.)).ui(ui);
widgets::Button::new("Button").size(vec2(120., 70.)).ui(ui);
ui.same_line(0.);
widgets::Button::new(texture.clone())
.size(vec2(120., 70.))
.ui(ui);
});
ui.tree_node(hash!(), "sliders", |ui| {
ui.slider(hash!(), "[-10 .. 10]", -10f32..10f32, &mut number0);
ui.slider(hash!(), "[0 .. 100]", 0f32..100f32, &mut number1);
});
ui.tree_node(hash!(), "editbox 1", |ui| {
ui.label(None, "This is editbox!");
ui.editbox(hash!(), vec2(285., 165.), &mut text0);
});
ui.tree_node(hash!(), "editbox 2", |ui| {
ui.label(None, "This is editbox!");
ui.editbox(hash!(), vec2(285., 165.), &mut text1);
});
});
match data.fit_command.take() {
Some(FittingCommand::Unfit { target_slot }) => data.set_item(target_slot, None),
Some(FittingCommand::Fit { target_slot, item }) => {
data.set_item(target_slot, Some(item));
}
Some(FittingCommand::Refit {
target_slot,
origin_slot,
}) => {
let origin_item = data
.slots
.iter()
.find_map(|(_, slot)| {
if slot.id == origin_slot {
Some(slot.item.clone())
} else {
None
}
})
.flatten();
data.set_item(target_slot, origin_item);
data.set_item(origin_slot, None);
}
None => {}
};
next_frame().await;
}
}
pub fn selected(self, selected: bool) -> Self
sourcepub fn ui(self, ui: &mut Ui) -> bool
pub fn ui(self, ui: &mut Ui) -> bool
Examples found in repository?
examples/ui.rs (line 211)
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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
async fn main() {
let mut data = Data::new();
let mut data0 = String::new();
let mut data1 = String::new();
let mut text0 = String::new();
let mut text1 = String::new();
let mut number0 = 0.;
let mut number1 = 0.;
let texture: Texture2D = load_texture("examples/ferris.png").await.unwrap();
loop {
clear_background(WHITE);
widgets::Window::new(hash!(), vec2(400., 200.), vec2(320., 400.))
.label("Shop")
.titlebar(true)
.ui(&mut *root_ui(), |ui| {
for i in 0..30 {
Group::new(hash!("shop", i), Vec2::new(300., 80.)).ui(ui, |ui| {
ui.label(Vec2::new(10., 10.), &format!("Item N {}", i));
ui.label(Vec2::new(260., 40.), "10/10");
ui.label(Vec2::new(200., 58.), &format!("{} kr", 800));
if ui.button(Vec2::new(260., 55.), "buy") {
data.inventory.push(format!("Item {}", i));
}
});
}
});
widgets::Window::new(hash!(), vec2(100., 220.), vec2(542., 430.))
.label("Fitting window")
.titlebar(true)
.ui(&mut *root_ui(), |ui| {
Group::new(hash!(), Vec2::new(230., 400.)).ui(ui, |ui| {
data.slots(ui);
});
Group::new(hash!(), Vec2::new(280., 400.)).ui(ui, |ui| {
data.inventory(ui);
});
});
widgets::Window::new(hash!(), vec2(470., 50.), vec2(300., 300.))
.label("Megaui Showcase Window")
.ui(&mut *root_ui(), |ui| {
ui.tree_node(hash!(), "input", |ui| {
ui.label(None, "Some random text");
if ui.button(None, "click me") {
println!("hi");
}
ui.separator();
ui.label(None, "Some other random text");
if ui.button(None, "other button") {
println!("hi2");
}
ui.separator();
ui.input_text(hash!(), "<- input text 1", &mut data0);
ui.input_text(hash!(), "<- input text 2", &mut data1);
ui.label(
None,
&format!("Text entered: \"{}\" and \"{}\"", data0, data1),
);
ui.separator();
});
ui.tree_node(hash!(), "buttons", |ui| {
widgets::Button::new(texture.clone())
.size(vec2(120., 70.))
.ui(ui);
ui.same_line(0.);
widgets::Button::new("Button").size(vec2(120., 70.)).ui(ui);
widgets::Button::new("Button").size(vec2(120., 70.)).ui(ui);
ui.same_line(0.);
widgets::Button::new(texture.clone())
.size(vec2(120., 70.))
.ui(ui);
});
ui.tree_node(hash!(), "sliders", |ui| {
ui.slider(hash!(), "[-10 .. 10]", -10f32..10f32, &mut number0);
ui.slider(hash!(), "[0 .. 100]", 0f32..100f32, &mut number1);
});
ui.tree_node(hash!(), "editbox 1", |ui| {
ui.label(None, "This is editbox!");
ui.editbox(hash!(), vec2(285., 165.), &mut text0);
});
ui.tree_node(hash!(), "editbox 2", |ui| {
ui.label(None, "This is editbox!");
ui.editbox(hash!(), vec2(285., 165.), &mut text1);
});
});
match data.fit_command.take() {
Some(FittingCommand::Unfit { target_slot }) => data.set_item(target_slot, None),
Some(FittingCommand::Fit { target_slot, item }) => {
data.set_item(target_slot, Some(item));
}
Some(FittingCommand::Refit {
target_slot,
origin_slot,
}) => {
let origin_item = data
.slots
.iter()
.find_map(|(_, slot)| {
if slot.id == origin_slot {
Some(slot.item.clone())
} else {
None
}
})
.flatten();
data.set_item(target_slot, origin_item);
data.set_item(origin_slot, None);
}
None => {}
};
next_frame().await;
}
}
More examples
examples/ui_skins.rs (line 244)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
async fn main() {
let skin1 = {
let label_style = root_ui()
.style_builder()
.font(include_bytes!("../examples/ui_assets/HTOWERT.TTF"))
.unwrap()
.text_color(Color::from_rgba(180, 180, 120, 255))
.font_size(30)
.build();
let window_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/window_background.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(20.0, 20.0, 10.0, 10.0))
.margin(RectOffset::new(-20.0, -30.0, 0.0, 0.0))
.build();
let button_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_background.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(37.0, 37.0, 5.0, 5.0))
.margin(RectOffset::new(10.0, 10.0, 0.0, 0.0))
.background_hovered(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_hovered_background.png"),
None,
)
.unwrap(),
)
.background_clicked(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_clicked_background.png"),
None,
)
.unwrap(),
)
.font(include_bytes!("../examples/ui_assets/HTOWERT.TTF"))
.unwrap()
.text_color(Color::from_rgba(180, 180, 100, 255))
.font_size(40)
.build();
let editbox_style = root_ui()
.style_builder()
.background_margin(RectOffset::new(0., 0., 0., 0.))
.font(include_bytes!("../examples/ui_assets/HTOWERT.TTF"))
.unwrap()
.text_color(Color::from_rgba(120, 120, 120, 255))
.color_selected(Color::from_rgba(190, 190, 190, 255))
.font_size(50)
.build();
Skin {
editbox_style,
window_style,
button_style,
label_style,
..root_ui().default_skin()
}
};
let skin2 = {
let label_style = root_ui()
.style_builder()
.font(include_bytes!("../examples/ui_assets/MinimalPixel v2.ttf"))
.unwrap()
.text_color(Color::from_rgba(120, 120, 120, 255))
.font_size(25)
.build();
let window_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/window_background_2.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(52.0, 52.0, 52.0, 52.0))
.margin(RectOffset::new(-30.0, 0.0, -30.0, 0.0))
.build();
let button_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_background_2.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(8.0, 8.0, 8.0, 8.0))
.background_hovered(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_hovered_background_2.png"),
None,
)
.unwrap(),
)
.background_clicked(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/button_clicked_background_2.png"),
None,
)
.unwrap(),
)
.font(include_bytes!("../examples/ui_assets/MinimalPixel v2.ttf"))
.unwrap()
.text_color(Color::from_rgba(180, 180, 100, 255))
.font_size(40)
.build();
let checkbox_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/checkbox_background.png"),
None,
)
.unwrap(),
)
.background_hovered(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/checkbox_hovered_background.png"),
None,
)
.unwrap(),
)
.background_clicked(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/checkbox_clicked_background.png"),
None,
)
.unwrap(),
)
.build();
let editbox_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/editbox_background.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(2., 2., 2., 2.))
.font(include_bytes!("../examples/ui_assets/MinimalPixel v2.ttf"))
.unwrap()
.text_color(Color::from_rgba(120, 120, 120, 255))
.font_size(25)
.build();
let combobox_style = root_ui()
.style_builder()
.background(
Image::from_file_with_format(
include_bytes!("../examples/ui_assets/combobox_background.png"),
None,
)
.unwrap(),
)
.background_margin(RectOffset::new(4., 25., 6., 6.))
.font(include_bytes!("../examples/ui_assets/MinimalPixel v2.ttf"))
.unwrap()
.text_color(Color::from_rgba(120, 120, 120, 255))
.color(Color::from_rgba(210, 210, 210, 255))
.font_size(25)
.build();
Skin {
window_style,
button_style,
label_style,
checkbox_style,
editbox_style,
combobox_style,
..root_ui().default_skin()
}
};
let default_skin = root_ui().default_skin().clone();
let mut window1_skin = skin1.clone();
let mut window2_skin = skin2.clone();
let mut checkbox = false;
let mut text = String::new();
let mut number = 0.0f32;
let mut combobox = 0;
loop {
clear_background(GRAY);
root_ui().group(hash!(), vec2(70.0, 100.0), |ui| {
ui.label(None, "Window 1");
if ui.button(None, "Skin 1") {
window1_skin = skin1.clone();
}
if ui.button(None, "Skin 2") {
window1_skin = skin2.clone();
}
if ui.button(None, "No Skin") {
window1_skin = default_skin.clone();
}
});
root_ui().same_line(0.);
root_ui().group(hash!(), vec2(70.0, 100.0), |ui| {
ui.label(None, "Window 2");
if ui.button(None, "Skin 1") {
window2_skin = skin1.clone();
}
if ui.button(None, "Skin 2") {
window2_skin = skin2.clone();
}
if ui.button(None, "No Skin") {
window2_skin = default_skin.clone();
}
});
root_ui().push_skin(&window1_skin);
root_ui().window(hash!(), vec2(20., 250.), vec2(300., 300.), |ui| {
widgets::Button::new("Play")
.position(vec2(65.0, 15.0))
.ui(ui);
widgets::Button::new("Options")
.position(vec2(40.0, 75.0))
.ui(ui);
widgets::Button::new("Quit")
.position(vec2(65.0, 195.0))
.ui(ui);
});
root_ui().pop_skin();
root_ui().push_skin(&window2_skin);
root_ui().window(hash!(), vec2(250., 20.), vec2(500., 250.), |ui| {
ui.checkbox(hash!(), "Checkbox 1", &mut checkbox);
ui.combo_box(
hash!(),
"Combobox",
&["First option", "Second option"],
&mut combobox,
);
ui.input_text(hash!(), "Text", &mut text);
ui.drag(hash!(), "Drag", None, &mut number);
widgets::Button::new("Apply")
.position(vec2(80.0, 150.0))
.ui(ui);
widgets::Button::new("Cancel")
.position(vec2(280.0, 150.0))
.ui(ui);
});
root_ui().pop_skin();
next_frame().await;
}
}
Auto Trait Implementations§
impl<'a> RefUnwindSafe for Button<'a>
impl<'a> Send for Button<'a>
impl<'a> Sync for Button<'a>
impl<'a> Unpin for Button<'a>
impl<'a> UnwindSafe for Button<'a>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more