Initial
This commit is contained in:
		
							parent
							
								
									edeaa38113
								
							
						
					
					
						commit
						c1c96a783c
					
				
					 4 changed files with 3938 additions and 0 deletions
				
			
		
							
								
								
									
										3787
									
								
								Cargo.lock
									
										
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										3787
									
								
								Cargo.lock
									
										
									
										generated
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										15
									
								
								Cargo.toml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								Cargo.toml
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,15 @@ | ||||||
|  | [package] | ||||||
|  | name = "SolutionTM" | ||||||
|  | version = "0.1.0" | ||||||
|  | edition = "2021" | ||||||
|  | 
 | ||||||
|  | # This application was created and tested with Rust version 1.81.0-nightly | ||||||
|  | 
 | ||||||
|  | [dependencies] | ||||||
|  | 
 | ||||||
|  | # The tag referes to latest revision as of 2024-06-10. | ||||||
|  | # Use 'branch = "master"' for the latest version or check: | ||||||
|  | # https://github.com/iced-rs/iced/commits/master/ | ||||||
|  | [dependencies.iced] | ||||||
|  | git = "https://github.com/iced-rs/iced.git" | ||||||
|  | rev = "06ff17fcf87495663a295d1548df1c2ac03dafbd" | ||||||
							
								
								
									
										106
									
								
								src/main.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										106
									
								
								src/main.rs
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,106 @@ | ||||||
|  | mod race; | ||||||
|  | use race::Race; | ||||||
|  | 
 | ||||||
|  | use iced::widget::{button, column, combo_box, row, text, Column, ComboBox, Text}; | ||||||
|  | use iced::{Alignment, Padding}; | ||||||
|  | 
 | ||||||
|  | /// Represents the state of our application and handles
 | ||||||
|  | /// message matching and updating the state accordingly.
 | ||||||
|  | struct State { | ||||||
|  |     races: combo_box::State<Race>, | ||||||
|  |     selected_race: Option<Race>, | ||||||
|  |     text: String, | ||||||
|  |     counter_value: i32, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Default is expected by iced::run.
 | ||||||
|  | // We need to manually implement this since combo_box is not derive(Default)
 | ||||||
|  | impl Default for State { | ||||||
|  |     fn default() -> Self { | ||||||
|  |         Self::new() | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Represents the messages that can be sent to the "component"
 | ||||||
|  | #[derive(Debug, Clone, Copy)] | ||||||
|  | enum Command { | ||||||
|  |     /// Increment the counter
 | ||||||
|  |     Increment, | ||||||
|  |     /// Decrement the counter
 | ||||||
|  |     Decrement, | ||||||
|  |     /// User selected an option in the combo box
 | ||||||
|  |     Selected(Race), | ||||||
|  |     /// User hovered over an option in the combo box
 | ||||||
|  |     OptionHovered(Race), | ||||||
|  |     /// User closed the combo box
 | ||||||
|  |     Closed, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | impl State { | ||||||
|  |     // Constructor for the state, nothing special here.
 | ||||||
|  |     fn new() -> Self { | ||||||
|  |         Self { | ||||||
|  |             races: combo_box::State::new(Race::ALL.to_vec()), | ||||||
|  |             selected_race: None, | ||||||
|  |             text: String::from("Hello, World!"), | ||||||
|  |             counter_value: 10, | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // On each update, we recieve a command and update the state accordingly.
 | ||||||
|  |     fn update(&mut self, command: Command) { | ||||||
|  |         match command { | ||||||
|  |             Command::Increment => self.counter_value += 1, | ||||||
|  |             Command::Decrement => self.counter_value -= 1, | ||||||
|  |             Command::Selected(race) => self.selected_race = Some(race), | ||||||
|  |             Command::OptionHovered(race) => println!("Hovered over {}.", race.to_string()), | ||||||
|  |             Command::Closed => self.selected_race = None, | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // Called every frame to render the user interface.
 | ||||||
|  |     // Changes to the state are reflected here.
 | ||||||
|  |     fn view(&self) -> Column<Command> { | ||||||
|  |         // Our combo box
 | ||||||
|  |         let combo_box: ComboBox<Race, Command> = combo_box::ComboBox::new( | ||||||
|  |             &self.races, | ||||||
|  |             "Pick a race", | ||||||
|  |             self.selected_race.as_ref(), | ||||||
|  |             Command::Selected, | ||||||
|  |         ) | ||||||
|  |         .on_option_hovered(Command::OptionHovered) | ||||||
|  |         .on_close(Command::Closed) | ||||||
|  |         .width(250); | ||||||
|  | 
 | ||||||
|  |         // Counter value
 | ||||||
|  |         let counter_value: Text = Text::new(self.counter_value.to_string()) | ||||||
|  |             .size(50) | ||||||
|  |             .width(50) | ||||||
|  |             .horizontal_alignment(iced::alignment::Horizontal::Center); | ||||||
|  | 
 | ||||||
|  |         // Row containig the buttons and the counter value
 | ||||||
|  |         let counter_row = row![ | ||||||
|  |             button("Increment").on_press(Command::Increment), | ||||||
|  |             counter_value, | ||||||
|  |             button("Decrement").on_press(Command::Decrement), | ||||||
|  |         ] | ||||||
|  |         .spacing(10) | ||||||
|  |         .align_items(Alignment::Start); | ||||||
|  | 
 | ||||||
|  |         // Finally return a combination of the components
 | ||||||
|  |         column![counter_row, combo_box, text(&self.text)] | ||||||
|  |             .padding(Padding::new(self.counter_value as f32)) | ||||||
|  |             .align_items(Alignment::Start) | ||||||
|  |             .spacing(10) | ||||||
|  |             .width(iced::Length::Fill) | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn main() { | ||||||
|  |     // Iced::run is an entry point for the application.
 | ||||||
|  |     // It may fail so we match on the result.
 | ||||||
|  |     match iced::run("Test Example", State::update, State::view) { | ||||||
|  |         Ok(_) => {} | ||||||
|  |         Err(error) => eprintln!("Error: {}", error), | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										30
									
								
								src/race.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/race.rs
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,30 @@ | ||||||
|  | // This file is just an enum of different races along with some implementations for it.
 | ||||||
|  | 
 | ||||||
|  | /// Different races that a player can choose from
 | ||||||
|  | #[repr(u8)] | ||||||
|  | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||||||
|  | pub enum Race { | ||||||
|  |     Elf, | ||||||
|  |     Human, | ||||||
|  |     Orc, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // There is a crate that can generate this with a macro. It's called strum.
 | ||||||
|  | impl Race { | ||||||
|  |     pub const ALL: [Race; 3] = [Race::Elf, Race::Human, Race::Orc]; | ||||||
|  | 
 | ||||||
|  |     pub fn to_string(&self) -> &str { | ||||||
|  |         match self { | ||||||
|  |             Race::Elf => "Elf", | ||||||
|  |             Race::Human => "Human", | ||||||
|  |             Race::Orc => "Orc", | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // We use this in the user interface
 | ||||||
|  | impl std::fmt::Display for Race { | ||||||
|  |     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||||||
|  |         write!(f, "{}", self.to_string()) | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Imbus
						Imbus