Skip to content

Instantly share code, notes, and snippets.

@loloof64
Created December 24, 2022 16:52
Show Gist options
  • Save loloof64/be32392b8561ea49af5009433e0a65d8 to your computer and use it in GitHub Desktop.
Save loloof64/be32392b8561ea49af5009433e0a65d8 to your computer and use it in GitHub Desktop.
Simple canvas (Iced experiment)
use iced::widget::canvas::{self, Cursor, Event, Geometry};
use iced::widget::canvas::{Cache, Canvas, Path};
use iced::{event, mouse, Color, Element, Length, Rectangle, Theme};
#[derive(Debug, Clone)]
pub enum Message {}
pub enum Interaction {
None,
}
impl Default for Interaction {
fn default() -> Self {
Self::None
}
}
pub struct ChessBoardBackground {
size: u16,
cells_cache: Cache,
}
impl ChessBoardBackground {
pub fn new(size: u16) -> Self {
Self {
size,
cells_cache: Cache::default(),
}
}
pub fn view(&self) -> Element<Message> {
Canvas::new(self)
.width(Length::Units(self.size))
.height(Length::Units(self.size))
.into()
}
}
impl canvas::Program<Message> for ChessBoardBackground {
type State = Interaction;
fn update(
&self,
_interaction: &mut Interaction,
_event: Event,
_bounds: Rectangle,
_cursor: Cursor,
) -> (event::Status, Option<Message>) {
(event::Status::Ignored, None)
}
fn draw(
&self,
_interaction: &Interaction,
_theme: &Theme,
bounds: Rectangle,
_cursor: Cursor,
) -> Vec<Geometry> {
let cells = self.cells_cache.draw(bounds.size(), |frame| {
let background = Path::rectangle(bounds.position(), frame.size());
frame.fill(&background, Color::from_rgb8(0x00, 0x80, 0xFF));
});
vec![cells]
}
fn mouse_interaction(
&self,
_interaction: &Interaction,
_bounds: Rectangle,
_cursor: Cursor,
) -> mouse::Interaction {
mouse::Interaction::default()
}
}
mod background;
pub use background::ChessBoardBackground;
mod chessboard;
use chessboard::ChessBoardBackground;
use iced::theme::Theme;
use iced::widget::{container, column};
use iced::{executor, Command};
use iced::{Application, Element};
#[derive(Debug, Clone)]
pub enum Message {
None,
}
pub struct App {
background: ChessBoardBackground,
background_2: ChessBoardBackground,
}
impl Application for App {
type Message = Message;
type Theme = Theme;
type Executor = executor::Default;
type Flags = ();
fn new(_flags: ()) -> (Self, Command<Message>) {
(
Self {
background: ChessBoardBackground::new(200u16),
background_2: ChessBoardBackground::new(200u16),
},
Command::none(),
)
}
fn title(&self) -> String {
String::from("Iced chess experiment")
}
fn update(&mut self, _message: Message) -> Command<Message> {
Command::none()
}
fn view(&self) -> Element<Message> {
let bg1 = self.background.view().map(move |_message| Message::None);
let bg2 = self.background_2.view().map(move |_message| Message::None);
let content = column![bg1, bg2];
container(content)
.center_x()
.center_y()
.into()
}
fn theme(&self) -> Theme {
Theme::Dark
}
}
mod gui;
use gui::App;
use iced::{Settings, Application, window};
fn main() -> iced::Result {
App::run(Settings {
antialiasing: true,
window: window::Settings {
position: window::Position::Centered,
..window::Settings::default()
},
..Settings::default()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment