-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.rs
198 lines (179 loc) · 5.64 KB
/
draw.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use std::{error::Error, io};
use crossterm::{event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, execute, terminal::{
disable_raw_mode, enable_raw_mode, Clear, ClearType, EnterAlternateScreen,
LeaveAlternateScreen,
}};
use glam::{dvec2, DMat3, DVec2};
use tactile::{get_tiling_type, IsohedralTiling};
use tui::{
backend::{Backend, CrosstermBackend},
layout::{Alignment, Constraint, Layout},
style::{Color, Modifier, Style},
text::{Span, Spans},
widgets::{
canvas::{Canvas, Context, Line},
Block, Borders, Paragraph,
},
Frame, Terminal,
};
struct App {
tile_type_num: usize,
tiling: IsohedralTiling,
edges_shapes: Vec<Vec<DVec2>>,
bound: f64,
}
impl App {
pub fn new() -> Self {
let tile_type_num = 0;
let tiling = IsohedralTiling::new(get_tiling_type(tile_type_num));
// initialise edge shapes with simple lines
let mut app = App {
tile_type_num,
tiling,
edges_shapes: vec![],
bound: 3.0,
};
app.set_default_edges();
app
}
pub fn prev_tile_type(&mut self) {
if self.tile_type_num > 0 {
self.tile_type_num -= 1;
}
self.tiling.reset(get_tiling_type(self.tile_type_num));
self.set_default_edges();
}
pub fn next_tile_type(&mut self) {
if self.tile_type_num < 80 {
self.tile_type_num += 1;
}
self.tiling.reset(get_tiling_type(self.tile_type_num));
self.set_default_edges();
}
pub fn set_default_edges(&mut self) {
self.edges_shapes.clear();
for _ in 0..self.tiling.num_edge_shapes() {
let mut edge = vec![];
edge.push(dvec2(0.0, 0.0));
edge.push(dvec2(1.0, 0.0));
self.edges_shapes.push(edge);
}
}
}
fn main() -> Result<(), Box<dyn Error>> {
// setup terminal
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(
stdout,
EnterAlternateScreen,
EnableMouseCapture,
Clear(ClearType::All)
)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = tui::Terminal::new(backend)?;
// create app and run it
let app = App::new();
let res = run_app(&mut terminal, app);
// restore terminal
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
if let Err(err) = res {
println!("{:?}", err)
}
Ok(())
}
fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<()> {
loop {
terminal.draw(|f| ui(f, &app))?;
if let Event::Key(key) = event::read()? {
match key.code {
KeyCode::Char('q') => {
return Ok(());
}
KeyCode::Up => {
app.bound += 0.5;
}
KeyCode::Down => {
if app.bound > 1.0 {
app.bound -= 0.5;
}
}
KeyCode::Right => {
app.next_tile_type();
}
KeyCode::Left => {
app.prev_tile_type();
}
_ => {}
}
}
}
}
fn ui<B: Backend>(f: &mut Frame<B>, app: &App) {
let colors = [Color::Blue, Color::Red, Color::Green];
let chunks = Layout::default()
.constraints([Constraint::Min(1), Constraint::Length(1)])
.direction(tui::layout::Direction::Vertical)
.split(f.size());
// Tiling canvas
let aspect = chunks[0].height as f64 / chunks[0].width as f64;
let xbound = app.bound;
let ybound = app.bound * aspect * 2.0;
let canvas = Canvas::default()
.block(Block::default().borders(Borders::ALL).title(format!(
" Tiling type: {} ",
get_tiling_type(app.tile_type_num)
)))
.paint(|ctx| {
for tile in &app
.tiling
.fill_region(-xbound, -ybound, xbound, ybound)
{
let c = app.tiling.colour(tile.t1, tile.t2, tile.aspect);
draw_tile(ctx, app, &tile.transform, colors[c as usize]);
}
})
.x_bounds([-xbound, xbound])
.y_bounds([-ybound, ybound]);
f.render_widget(canvas, chunks[0]);
// Status bar
let text = vec![Spans::from(vec![
Span::styled("←/→", Style::default().add_modifier(Modifier::BOLD)),
Span::styled(
": change type, ",
Style::default().add_modifier(Modifier::ITALIC),
),
Span::styled("↑/↓", Style::default().add_modifier(Modifier::BOLD)),
Span::styled(
": zoom in/out, ",
Style::default().add_modifier(Modifier::ITALIC),
),
Span::styled("q", Style::default().add_modifier(Modifier::BOLD)),
Span::styled(": quit, ", Style::default().add_modifier(Modifier::ITALIC)),
])];
let status_bar = Paragraph::new(text)
.alignment(Alignment::Left)
.style(Style::default().fg(Color::Gray));
f.render_widget(status_bar, chunks[1]);
}
fn draw_tile(ctx: &mut Context, app: &App, t: &DMat3, c: Color) {
for e in app.tiling.shapes() {
let edge = &app.edges_shapes[e.id()];
let transform = *t * e.transform();
let p1 = transform.transform_point2(edge[0]);
let p2 = transform.transform_point2(edge[1]);
ctx.draw(&Line {
x1: p1.x,
y1: p1.y,
x2: p2.x,
y2: p2.y,
color: c,
});
}
}