Port all to new version of iced

This commit is contained in:
Greg Shuflin 2024-10-04 02:03:45 -07:00
parent 73b7af9777
commit d8cecfc282

View File

@ -8,8 +8,8 @@ use tetris_logic::{BlockGrid, MoveDirection, Tetromino};
*/ */
use iced::{ use iced::{
widget::{button, canvas, text}, widget::canvas::{self, Path, Stroke, Text},
Element, Renderer, Subscription, Task, Theme, Color, Element, Length, Pixels, Point, Renderer, Size, Subscription, Task, Theme,
}; };
use tetris_logic::{BlockGrid, MoveDirection, Tetromino}; use tetris_logic::{BlockGrid, MoveDirection, Tetromino};
@ -33,7 +33,7 @@ enum Message {
fn update(state: &mut Tetris, message: Message) { fn update(state: &mut Tetris, message: Message) {
match message { match message {
Message::Pause => state.paused = !state.paused, Message::Pause => state.paused = !state.paused,
Message::Tick(_) => { Message::Tick(_time) => {
if !state.paused { if !state.paused {
state.ticks += 1; state.ticks += 1;
} }
@ -76,20 +76,6 @@ fn subscription(_state: &Tetris) -> Subscription<Message> {
_ => None, _ => 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)) let time_subscription = iced::time::every(std::time::Duration::from_millis(50))
.map(|_| Message::Tick(chrono::Local::now())); .map(|_| Message::Tick(chrono::Local::now()));
@ -97,11 +83,14 @@ fn subscription(_state: &Tetris) -> Subscription<Message> {
} }
fn view(state: &Tetris) -> Element<Message> { fn view(state: &Tetris) -> Element<Message> {
canvas(state).into() iced::widget::canvas(state)
.width(Length::Fill)
.height(Length::Fill)
.into()
} }
struct Tetris { struct Tetris {
//background_cache: canvas::Cache, background_cache: canvas::Cache,
blocks: BlockGrid, blocks: BlockGrid,
ticks: usize, ticks: usize,
paused: bool, paused: bool,
@ -115,7 +104,7 @@ impl Tetris {
fn new() -> Tetris { fn new() -> Tetris {
Tetris { Tetris {
//background_cache: canvas::Cache::default(), background_cache: canvas::Cache::default(),
blocks: BlockGrid::new(), blocks: BlockGrid::new(),
ticks: 0, ticks: 0,
paused: false, 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<Message>) {
(Tetris::new(), Command::none())
}
fn title(&self) -> String {
String::from("Tetris - Iced")
}
fn update(&mut self, message: Self::Message) -> Command<Message> {
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<Message> {
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<Self::Message> {
canvas::Canvas::new(self)
.width(Length::Fill)
.height(Length::Fill)
.into()
}
}
*/
impl canvas::Program<Message> for Tetris { impl canvas::Program<Message> for Tetris {
type State = (); type State = ();
fn draw( fn draw(
&self, &self,
state: &Self::State, _state: &Self::State,
renderer: &Renderer, renderer: &Renderer,
theme: &Theme, _theme: &Theme,
bounds: iced::Rectangle, bounds: iced::Rectangle,
_cursor: iced::mouse::Cursor, _cursor: iced::mouse::Cursor,
) -> Vec<canvas::Geometry<Renderer>> { ) -> Vec<canvas::Geometry<Renderer>> {
todo!()
}
/*
fn draw(&self, bounds: Rectangle, _cursor: canvas::Cursor) -> Vec<canvas::Geometry> {
let game_width = bounds.width / 3.0; let game_width = bounds.width / 3.0;
let block_length = game_width / 10.0; let block_length = game_width / 10.0;
let center = bounds.center(); let center = bounds.center();
let top_left = Point::new(center.x - game_width / 2.0, 0.0); let top_left = Point::new(center.x - game_width / 2.0, 0.0);
let background = self.background_cache.draw(bounds.size(), |frame| { 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_size = Size::new(block_length * 10.0, block_length * 20.0);
let game_bg = Path::rectangle(top_left, game_size); let game_bg = Path::rectangle(top_left, game_size);
frame.fill(&game_bg, Color::BLACK); frame.fill(&game_bg, Color::BLACK);
}); });
let block_size = Size::new(block_length, block_length); 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() { for (i, j, tetronimo) in self.blocks.iter() {
let point = Point::new( let point = Point::new(
@ -247,21 +155,17 @@ impl canvas::Program<Message> for Tetris {
color.1.checked_sub(20).unwrap_or(0), color.1.checked_sub(20).unwrap_or(0),
color.2.checked_sub(20).unwrap_or(0), color.2.checked_sub(20).unwrap_or(0),
); );
let stroke = Stroke { let stroke = Stroke::default().with_width(3.0).with_color(stroke_color);
width: 3.0,
color: stroke_color,
..Stroke::default()
};
frame.stroke(&block, stroke); frame.stroke(&block, stroke);
} }
let text_color = Color::from_rgb8(255, 30, 30); let text_color = Color::from_rgb8(255, 30, 30);
let text_size = 32.0; let text_size = Pixels(32.0);
let score = Text { let score = Text {
content: format!("Lines removed: {}", self.lines_removed), content: format!("Lines removed: {}", self.lines_removed),
position: Point::new(10.0, 30.0), position: Point::new(10.0, 30.0),
color: text_color,
size: text_size, size: text_size,
color: text_color,
..Default::default() ..Default::default()
}; };
@ -269,7 +173,7 @@ impl canvas::Program<Message> for Tetris {
if self.paused { if self.paused {
let paused = Text { let paused = Text {
content: format!("PAUSED"), content: "PAUSED".to_string(),
position: Point::new(10.0, 60.0), position: Point::new(10.0, 60.0),
color: text_color, color: text_color,
size: text_size, size: text_size,
@ -280,20 +184,4 @@ impl canvas::Program<Message> for Tetris {
vec![background, frame.into_geometry()] vec![background, frame.into_geometry()]
} }
*/
} }
/*
fn handle_keypress(key_code: keyboard::KeyCode) -> Option<Message> {
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,
})
}
*/