Added some bad graphics stuffs
This commit is contained in:
119
card_stuffs/src/main.rs
Normal file
119
card_stuffs/src/main.rs
Normal file
@@ -0,0 +1,119 @@
|
||||
use std::io;
|
||||
use card_stuffs;
|
||||
|
||||
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
layout::{Constraint, Layout, Rect},
|
||||
style::Stylize,
|
||||
symbols::border,
|
||||
text::{Line, Text},
|
||||
widgets::{block::title, Block, BorderType, Borders, Paragraph, Widget, Padding},
|
||||
DefaultTerminal, Frame,
|
||||
};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct App {
|
||||
cards: card_stuffs::Klondike,
|
||||
exit: bool,
|
||||
}
|
||||
|
||||
const CARD_HEIGHT: u16 = 12;
|
||||
const CARD_WIDTH: u16 = 10;
|
||||
|
||||
impl App {
|
||||
|
||||
/// runs the application's main loop until the user quits
|
||||
pub fn run(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> {
|
||||
while !self.exit {
|
||||
terminal.draw(|frame| self.draw(frame))?;
|
||||
self.handle_events()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draw(&self, frame: &mut Frame) {
|
||||
let vertical = Layout::vertical([
|
||||
Constraint::Length(1),
|
||||
Constraint::Min(0),
|
||||
Constraint::Length(1),
|
||||
]);
|
||||
let [title_bar, main_area, status_bar] = vertical.areas(frame.area());
|
||||
|
||||
frame.render_widget(
|
||||
Block::new()
|
||||
.borders(Borders::TOP)
|
||||
.border_type(BorderType::Thick)
|
||||
.title(Line::from("Legends of Soltar").bold()),
|
||||
title_bar
|
||||
);
|
||||
|
||||
let vertical = Layout::vertical([
|
||||
Constraint::Length(CARD_HEIGHT + 2), // for padding
|
||||
Constraint::Min(0)
|
||||
]);
|
||||
let [dwf_area, piles_area] = vertical.areas(main_area);
|
||||
|
||||
frame.render_widget(
|
||||
Block::new().borders(Borders::ALL).title("dwf_area"),
|
||||
dwf_area
|
||||
);
|
||||
frame.render_widget(
|
||||
Block::new().borders(Borders::ALL).title("piles_area"),
|
||||
piles_area
|
||||
);
|
||||
let horizontal = Layout::horizontal([
|
||||
Constraint::Length(CARD_WIDTH),
|
||||
Constraint::Length(2),
|
||||
Constraint::Length(CARD_WIDTH),
|
||||
Constraint::Length(2),
|
||||
Constraint::Length(CARD_WIDTH),
|
||||
Constraint::Length(2),
|
||||
Constraint::Length(CARD_WIDTH),
|
||||
Constraint::Length(2),
|
||||
Constraint::Length(CARD_WIDTH),
|
||||
Constraint::Length(2),
|
||||
Constraint::Length(CARD_WIDTH),
|
||||
Constraint::Length(2),
|
||||
Constraint::Length(CARD_WIDTH),
|
||||
Constraint::Length(2),
|
||||
]);
|
||||
let piles_and_spacers: [Rect; 14] = horizontal.areas(piles_area);
|
||||
for pile in 0..card_stuffs::NUM_PILES_KLONDIKE {
|
||||
frame.render_widget(
|
||||
Block::new().borders(Borders::ALL).title(format!("pile {}", pile)),
|
||||
piles_and_spacers[pile*2]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_events(&mut self) -> io::Result<()> {
|
||||
match event::read()? {
|
||||
// it's important to check that the event is a key press event as
|
||||
// crossterm also emits key release and repeat events on Windows.
|
||||
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
|
||||
self.handle_key_event(key_event)
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_key_event(&mut self, key_event: KeyEvent) {
|
||||
match key_event.code {
|
||||
KeyCode::Char('q') => self.exit(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn exit(&mut self) {
|
||||
self.exit = true;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let mut terminal = ratatui::init();
|
||||
let app_result = App::default().run(&mut terminal);
|
||||
ratatui::restore();
|
||||
app_result
|
||||
}
|
||||
Reference in New Issue
Block a user