From d8cecfc282c54f4ec6019c0c4997549e6c690426 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Fri, 4 Oct 2024 02:03:45 -0700 Subject: [PATCH] Port all to new version of iced --- iced-tetris/src/main.rs | 158 ++++++---------------------------------- 1 file changed, 23 insertions(+), 135 deletions(-) diff --git a/iced-tetris/src/main.rs b/iced-tetris/src/main.rs index 703135f..33203da 100644 --- a/iced-tetris/src/main.rs +++ b/iced-tetris/src/main.rs @@ -8,8 +8,8 @@ use tetris_logic::{BlockGrid, MoveDirection, Tetromino}; */ use iced::{ - widget::{button, canvas, text}, - Element, Renderer, Subscription, Task, Theme, + widget::canvas::{self, Path, Stroke, Text}, + Color, Element, Length, Pixels, Point, Renderer, Size, Subscription, Task, Theme, }; use tetris_logic::{BlockGrid, MoveDirection, Tetromino}; @@ -33,7 +33,7 @@ enum Message { fn update(state: &mut Tetris, message: Message) { match message { Message::Pause => state.paused = !state.paused, - Message::Tick(_) => { + Message::Tick(_time) => { if !state.paused { state.ticks += 1; } @@ -76,20 +76,6 @@ fn subscription(_state: &Tetris) -> Subscription { _ => None, } }); - /* - let keyboard_subscription = subscription::events_with(|event, status| { - if let event::Status::Captured = status { - return None; - } - - match event { - Event::Keyboard(keyboard::Event::KeyPressed { key_code, .. }) => { - handle_keypress(key_code) - } - _ => None, - } - }); - */ let time_subscription = iced::time::every(std::time::Duration::from_millis(50)) .map(|_| Message::Tick(chrono::Local::now())); @@ -97,11 +83,14 @@ fn subscription(_state: &Tetris) -> Subscription { } fn view(state: &Tetris) -> Element { - canvas(state).into() + iced::widget::canvas(state) + .width(Length::Fill) + .height(Length::Fill) + .into() } struct Tetris { - //background_cache: canvas::Cache, + background_cache: canvas::Cache, blocks: BlockGrid, ticks: usize, paused: bool, @@ -115,7 +104,7 @@ impl Tetris { fn new() -> Tetris { Tetris { - //background_cache: canvas::Cache::default(), + background_cache: canvas::Cache::default(), blocks: BlockGrid::new(), ticks: 0, paused: false, @@ -124,113 +113,32 @@ impl Tetris { } } -/* -impl Application for Tetris { - type Executor = executor::Default; - type Message = Message; - type Flags = (); - - fn new(_flags: ()) -> (Self, Command) { - (Tetris::new(), Command::none()) - } - - fn title(&self) -> String { - String::from("Tetris - Iced") - } - - fn update(&mut self, message: Self::Message) -> Command { - match message { - Message::Pause => self.paused = !self.paused, - Message::Tick(_) => { - if !self.paused { - self.ticks += 1 - } - } - Message::Up => { - self.blocks.rotate_active_piece(); - } - Message::Down => { - self.blocks.move_active_piece(MoveDirection::HardDrop); - } - Message::Left => { - self.blocks.move_active_piece(MoveDirection::Left); - } - Message::Right => { - self.blocks.move_active_piece(MoveDirection::Right); - } - }; - - if self.blocks.piece_currently_active() { - if self.ticks % 10 == 0 && !self.paused { - self.blocks.move_active_piece(MoveDirection::SoftDrop); - } - } else { - let piece: Tetromino = rand::random(); - self.blocks.drop_piece(piece); - } - - let lines_removed = self.blocks.clear_pieces(); - self.lines_removed += lines_removed; - Command::none() - } - - fn subscription(&self) -> Subscription { - let keyboard_subscription = subscription::events_with(|event, status| { - if let event::Status::Captured = status { - return None; - } - - match event { - Event::Keyboard(keyboard::Event::KeyPressed { key_code, .. }) => { - handle_keypress(key_code) - } - _ => None, - } - }); - - let time_subscription = time::every(std::time::Duration::from_millis(50)) - .map(|_| Message::Tick(chrono::Local::now())); - Subscription::batch([time_subscription, keyboard_subscription]) - } - - fn view(&mut self) -> Element { - canvas::Canvas::new(self) - .width(Length::Fill) - .height(Length::Fill) - .into() - } -} -*/ - impl canvas::Program for Tetris { type State = (); fn draw( &self, - state: &Self::State, + _state: &Self::State, renderer: &Renderer, - theme: &Theme, + _theme: &Theme, bounds: iced::Rectangle, _cursor: iced::mouse::Cursor, ) -> Vec> { - todo!() - } - - /* - fn draw(&self, bounds: Rectangle, _cursor: canvas::Cursor) -> Vec { let game_width = bounds.width / 3.0; let block_length = game_width / 10.0; let center = bounds.center(); let top_left = Point::new(center.x - game_width / 2.0, 0.0); - let background = self.background_cache.draw(bounds.size(), |frame| { - let game_size = Size::new(block_length * 10.0, block_length * 20.0); - let game_bg = Path::rectangle(top_left, game_size); - frame.fill(&game_bg, Color::BLACK); - }); + let background = self + .background_cache + .draw(renderer, bounds.size(), |frame| { + let game_size = Size::new(block_length * 10.0, block_length * 20.0); + let game_bg = Path::rectangle(top_left, game_size); + frame.fill(&game_bg, Color::BLACK); + }); let block_size = Size::new(block_length, block_length); - let mut frame = canvas::Frame::new(bounds.size()); + let mut frame = canvas::Frame::new(renderer, bounds.size()); for (i, j, tetronimo) in self.blocks.iter() { let point = Point::new( @@ -247,21 +155,17 @@ impl canvas::Program for Tetris { color.1.checked_sub(20).unwrap_or(0), color.2.checked_sub(20).unwrap_or(0), ); - let stroke = Stroke { - width: 3.0, - color: stroke_color, - ..Stroke::default() - }; + let stroke = Stroke::default().with_width(3.0).with_color(stroke_color); frame.stroke(&block, stroke); } let text_color = Color::from_rgb8(255, 30, 30); - let text_size = 32.0; + let text_size = Pixels(32.0); let score = Text { content: format!("Lines removed: {}", self.lines_removed), position: Point::new(10.0, 30.0), - color: text_color, size: text_size, + color: text_color, ..Default::default() }; @@ -269,7 +173,7 @@ impl canvas::Program for Tetris { if self.paused { let paused = Text { - content: format!("PAUSED"), + content: "PAUSED".to_string(), position: Point::new(10.0, 60.0), color: text_color, size: text_size, @@ -280,20 +184,4 @@ impl canvas::Program for Tetris { vec![background, frame.into_geometry()] } - */ } - -/* -fn handle_keypress(key_code: keyboard::KeyCode) -> Option { - use keyboard::KeyCode; - - Some(match key_code { - KeyCode::Up => Message::Up, - KeyCode::Down => Message::Down, - KeyCode::Right => Message::Right, - KeyCode::Left => Message::Left, - KeyCode::Space => Message::Pause, - _ => return None, - }) -} -*/