Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created April 4, 2019 02:40
Show Gist options
  • Save rust-play/1a2dffdb8418988494c226a8f2950817 to your computer and use it in GitHub Desktop.
Save rust-play/1a2dffdb8418988494c226a8f2950817 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
/// Colors are stored in RGBA format
/// The first byte is R, the second byte is G, etc.
#[derive(Debug)]
struct Color(u32);
#[derive(Debug)]
struct RelDimen {
x:i16, y:i16, w:i16, h:i16
}
impl RelDimen {
fn abs(&self) -> AbsDimen {
AbsDimen {
x: self.x.abs() as u16,
y: self.y.abs() as u16,
w: self.w.abs() as u16,
h: self.h.abs() as u16,
}
}
}
#[derive(Debug)]
struct AbsDimen {
x:u16, y:u16, w:u16, h:u16
}
#[derive(Debug)]
enum Message {
Rect{dimen:AbsDimen, fill:Color},
Text{dimen:AbsDimen, fill:Color, fg:Color},
}
type MessageList = Vec<Message>;
trait Widget {
fn draw(&self) -> MessageList;
fn window(&self) -> Option<&Window>;
}
struct Box {
dimen: RelDimen,
}
impl Box {
fn new(x: i16, y:i16, w:i16, h:i16) -> Box {
return Box{dimen: RelDimen{x:x, y:y, w:w, h:h}};
}
}
impl Widget for Box {
fn draw(&self) -> MessageList {
vec![Message::Rect{dimen:self.dimen.abs(), fill:Color(0)}]
}
fn window(&self) -> Option<&Window> {
None
}
}
struct Window {
dimen: AbsDimen,
}
fn main() {
let b = Box::new(0,0,50,50);
println!("{:?}",b.draw());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment