gdritter repos palladio / 5517327
First lurching steps towards complete Palladio Getty Ritter 6 years ago
6 changed file(s) with 228 addition(s) and 0 deletion(s). Collapse all Expand all
1
2 /target/
3 **/*.rs.bk
4 *~
1 [package]
2 name = "palladio"
3 version = "0.1.0"
4 authors = ["Getty Ritter <palladio@infinitenegativeutility.com>"]
5
6 [dependencies]
7 gtk = { version = "0.2", features = ["v3_22"] }
8 rand = "0.4.2"
1 use std::ops;
2 use std::collections::{HashMap, HashSet};
3 use std::hash::Hash;
4 use rand::{Rng, thread_rng};
5
6 type Index = (i32, i32);
7
8 pub struct Rule<Cell> {
9 pub rule_name: Option<String>,
10 pub requires: HashSet<Cell>,
11 pub lhs: HashMap<Index, Cell>,
12 pub rhs: HashMap<Index, Cell>,
13 }
14
15 pub struct Board<Cell> {
16 pub width: u32,
17 pub height: u32,
18 pub cells: Vec<Cell>,
19 pub contents: HashSet<Cell>,
20 }
21
22 impl<Cell: Clone + Hash + Eq> Board<Cell> {
23 pub fn new(width: u32, height: u32, default: Cell) -> Board<Cell> {
24 let cells = (0..width*height).map(|_| default.clone()).collect();
25 let contents = vec![default].into_iter().collect();
26 Board { width, height, cells, contents }
27 }
28
29 pub fn get<'a>(&'a self, (w, h): Index) -> Option<&'a Cell> {
30 if w > 0 && w < self.width as i32 && h > 0 && h < self.height as i32 {
31 Some(&self[(w, h)])
32 } else {
33 None
34 }
35 }
36
37 pub fn indices(&self) -> Vec<Index> {
38 let mut v = Vec::new();
39 for x in 0..self.width {
40 for y in 0..self.height {
41 v.push((x as i32, y as i32))
42 }
43 }
44 v
45 }
46
47 pub fn random_indices(&self) -> Vec<Index> {
48 let mut v = self.indices();
49 {
50 let slice = v.as_mut_slice();
51 thread_rng().shuffle(slice);
52 }
53 v
54 }
55 }
56
57 impl Board<char> {
58 pub fn print(&self) {
59 for x in 0..self.width {
60 for y in 0..self.height {
61 print!("{} ", self[(x as i32, y as i32)]);
62 }
63 println!("");
64 }
65 }
66 }
67
68 impl<Cell> ops::Index<Index> for Board<Cell> {
69 type Output = Cell;
70
71 fn index(&self, (w, h): Index) -> &Cell {
72 let n: usize = (w + (h * self.width as i32)) as usize;
73 &self.cells[n as usize]
74 }
75 }
76
77 impl<Cell> ops::IndexMut<Index> for Board<Cell> {
78 fn index_mut(&mut self, (w, h): Index) -> &mut Cell {
79 let n: usize = (w + (h * self.width as i32)) as usize;
80 &mut self.cells[n as usize]
81 }
82 }
83
84 impl<Cell: Clone + Hash + Eq> Rule<Cell> {
85 pub fn new(
86 lhs: HashMap<Index, Cell>,
87 rhs: HashMap<Index, Cell>,
88 ) -> Rule<Cell> {
89 let rule_name = None;
90 let requires = lhs.values().cloned().collect();
91 Rule { rule_name, requires, lhs, rhs }
92 }
93
94 pub fn apply_to_board(&self, b: &mut Board<Cell>) -> bool {
95 'outer: for (x, y) in b.random_indices() {
96 for (&(i, j), v) in self.lhs.iter() {
97 match b.get((x + i, y + j)) {
98 Some(r) if v == r => (),
99 _ => continue 'outer,
100 }
101 }
102
103 for (&(i, j), r) in self.rhs.iter() {
104 b[(x + i, y + j)] = r.clone();
105 }
106
107 return true;
108 }
109 false
110 }
111 }
112
113
114 pub fn grammar_test() {
115 let rule1: Rule<char> = Rule::new(
116 vec![ ((0, 0), 'x') ].into_iter().collect(),
117 vec![ ((0, 0), 'y'), ((1, 0), 'y') ].into_iter().collect(),
118 );
119 let rule2: Rule<char> = Rule::new(
120 vec![ ((0, 0), 'y') ].into_iter().collect(),
121 vec![ ((0, 0), 'z') ].into_iter().collect(),
122 );
123 let mut board = Board::new(8, 8, '.');
124 board[(2, 2)] = 'x';
125 let _ = rule1.apply_to_board(&mut board);
126 let _ = rule2.apply_to_board(&mut board);
127 board.print();
128 }
1 extern crate gtk;
2 extern crate rand;
3
4 pub mod grammar;
5 pub mod model;
6 pub mod view;
7
8 fn main() {
9 grammar::grammar_test();
10
11 view::App::run();
12 }
1 pub struct State {
2 pub document: Option<Document>,
3 }
4
5 pub struct Document {
6 }
1 use gtk;
2 use gtk::{
3 ContainerExt,
4 HeaderBarExt,
5 WindowExt,
6 WidgetExt,
7 };
8 use std::process;
9
10 pub struct App {
11 pub window: gtk::Window,
12 pub canvas: gtk::DrawingArea,
13 pub header: Header,
14 }
15
16 pub struct Header {
17 pub container: gtk::HeaderBar,
18 }
19
20 impl App {
21 fn new() -> App {
22 let window = gtk::Window::new(gtk::WindowType::Toplevel);
23 let header = Header::new();
24 let canvas = gtk::DrawingArea::new();
25
26 window.set_titlebar(&header.container);
27 window.set_title("App name");
28 window.set_wmclass("app-name", "App name");
29 window.add(&canvas);
30
31 canvas.connect_draw(|cv, ctx| {
32 let w = cv.get_allocated_width();
33 let h = cv.get_allocated_height();
34 ctx.set_source_rgb(0.0, 0.0, 0.0);
35 ctx.rectangle(0.0, 0.0, w as f64, h as f64);
36 ctx.fill();
37 gtk::Inhibit(false)
38 });
39
40 gtk::Window::set_default_icon_name("iconname");
41
42 window.connect_delete_event(move |_, _| {
43 gtk::main_quit();
44 gtk::Inhibit(false)
45 });
46
47 App { window, header, canvas }
48 }
49
50 pub fn run() {
51 if gtk::init().is_err() {
52 eprintln!("Failed to initialize GTK application");
53 process::exit(1);
54 }
55
56 let app = App::new();
57 app.window.show_all();
58 gtk::main();
59 }
60 }
61
62 impl Header {
63 fn new() -> Header {
64 let container = gtk::HeaderBar::new();
65 container.set_title("App name");
66 container.set_show_close_button(true);
67
68 Header { container }
69 }
70 }