Start to build edit game rules screen

This commit is contained in:
2022-10-02 15:50:54 +02:00
parent 72af5df56f
commit 075b9e33e4
11 changed files with 392 additions and 8 deletions

View File

@ -0,0 +1,35 @@
use crate::ui_screens::utils::centered_rect_size;
use std::fmt::Display;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::*;
pub struct ButtonWidget {
is_hovered: bool,
label: String,
}
impl ButtonWidget {
pub fn new<D: Display>(label: D, is_hovered: bool) -> Self {
Self {
label: label.to_string(),
is_hovered,
}
}
}
impl Widget for ButtonWidget {
fn render(self, area: Rect, buf: &mut Buffer) {
let label = format!(" {} ", self.label);
let area = centered_rect_size(label.len() as u16, 1, area);
let input = Paragraph::new(label.as_ref()).style(match &self.is_hovered {
false => Style::default().bg(Color::DarkGray),
true => Style::default().fg(Color::White).bg(Color::Yellow),
});
input.render(area, buf);
}
}

View File

@ -0,0 +1,56 @@
use std::fmt::Display;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::*;
pub struct CheckboxWidget {
is_editing: bool,
checked: bool,
label: String,
is_radio: bool,
}
impl CheckboxWidget {
pub fn new<D: Display>(label: D, checked: bool, is_editing: bool) -> Self {
Self {
is_editing,
checked,
label: label.to_string(),
is_radio: false,
}
}
pub fn set_radio(mut self) -> Self {
self.is_radio = true;
self
}
}
impl Widget for CheckboxWidget {
fn render(self, area: Rect, buf: &mut Buffer) {
let paragraph = format!(
"{}{}{} {}",
match self.is_radio {
true => "(",
false => "[",
},
match self.checked {
true => "X",
false => " ",
},
match self.is_radio {
true => ")",
false => "]",
},
self.label
);
let input = Paragraph::new(paragraph.as_ref()).style(match &self.is_editing {
false => Style::default(),
true => Style::default().fg(Color::Yellow),
});
input.render(area, buf);
}
}

View File

@ -0,0 +1,3 @@
pub mod button_widget;
pub mod checkbox_widget;
pub mod text_editor_widget;

View File

@ -0,0 +1,46 @@
use std::fmt::Display;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{Block, Borders, Paragraph, Widget};
pub enum InputMode {
Normal,
Editing,
}
pub struct TextEditorWidget {
input_mode: InputMode,
value: String,
label: String,
}
impl TextEditorWidget {
pub fn new<D: Display>(label: D, value: D, is_editing: bool) -> Self {
Self {
input_mode: match is_editing {
true => InputMode::Editing,
false => InputMode::Normal,
},
value: value.to_string(),
label: label.to_string(),
}
}
}
impl Widget for TextEditorWidget {
fn render(self, area: Rect, buf: &mut Buffer) {
let input = Paragraph::new(self.value.as_ref())
.style(match &self.input_mode {
InputMode::Normal => Style::default(),
InputMode::Editing => Style::default().fg(Color::Yellow),
})
.block(
Block::default()
.borders(Borders::ALL)
.title(self.label.as_ref()),
);
input.render(area, buf);
}
}