Port all to new version of iced
This commit is contained in:
parent
73b7af9777
commit
d8cecfc282
@ -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<Message> {
|
||||
_ => 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<Message> {
|
||||
}
|
||||
|
||||
fn view(state: &Tetris) -> Element<Message> {
|
||||
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<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 {
|
||||
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<canvas::Geometry<Renderer>> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/*
|
||||
fn draw(&self, bounds: Rectangle, _cursor: canvas::Cursor) -> Vec<canvas::Geometry> {
|
||||
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 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<Message> 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<Message> 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<Message> for Tetris {
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user