basic drawing
This commit is contained in:
+144
-37
@@ -8,15 +8,20 @@
|
||||
//! sub-menus. `Esc` walks back up the tree, opening the overlay
|
||||
//! [editor menu](editor_menu::editor_menu_items) at the root.
|
||||
|
||||
use crate::editor_menu;
|
||||
use crate::editor_menu::{MenuEntry, MenuLevel};
|
||||
use crate::log::{LogWidget, log_preview_line};
|
||||
use crate::render::{BoardWidget, board_to_screen, screen_to_board};
|
||||
use crate::ui::Ui;
|
||||
use crate::utils::rgba8_to_color;
|
||||
use kiln_core::cp437::tile_to_char;
|
||||
use kiln_core::glyph::Glyph;
|
||||
use kiln_core::log::LogLine;
|
||||
use kiln_core::world::World;
|
||||
use kiln_core::{Archetype, Board};
|
||||
use kiln_ui::code_editor::{CodeEditor, CodeEditorOutcome};
|
||||
use kiln_ui::dialog::{Dialog, DialogResult, ListDialogResponse};
|
||||
use kiln_ui::glyph_dialog::GlyphDialog;
|
||||
use crate::log::{log_preview_line, LogWidget};
|
||||
use crate::render::{board_to_screen, screen_to_board, BoardWidget};
|
||||
use crate::ui::Ui;
|
||||
use kiln_core::Board;
|
||||
use kiln_core::log::LogLine;
|
||||
use kiln_core::world::World;
|
||||
use ratatui::Frame;
|
||||
use ratatui::crossterm::event::Event;
|
||||
use ratatui::layout::{Constraint, Layout, Rect, Spacing};
|
||||
@@ -24,9 +29,7 @@ use ratatui::style::{Color, Style};
|
||||
use ratatui::symbols::merge::MergeStrategy;
|
||||
use ratatui::text::{Line, Span};
|
||||
use ratatui::widgets::{Block, Paragraph};
|
||||
use std::cell::Ref;
|
||||
use crate::editor_menu;
|
||||
use crate::editor_menu::{MenuEntry, MenuLevel};
|
||||
use std::cell::{Ref, RefMut};
|
||||
|
||||
/// How long each half of the cursor blink lasts, in seconds.
|
||||
const BLINK_SECS: f32 = 0.5;
|
||||
@@ -36,6 +39,8 @@ const DEFAULT_SIDEBAR_WIDTH: u16 = 24;
|
||||
const MIN_PANEL_WIDTH: u16 = 8;
|
||||
/// The cursor glyph: a solid full block (CP437 219) drawn in light gray.
|
||||
const CURSOR_CHAR: char = '█';
|
||||
/// Height (in rows) of the drawing-controls footer pinned to the sidebar bottom.
|
||||
const DRAW_FOOTER_HEIGHT: u16 = 5;
|
||||
|
||||
/// All state for an active editing session.
|
||||
///
|
||||
@@ -61,6 +66,15 @@ pub(crate) struct EditorState {
|
||||
pub(crate) glyph_dialog: Option<GlyphDialog<EditorState>>,
|
||||
/// Cursor position in board cells, clamped to the board bounds.
|
||||
cursor: (i32, i32),
|
||||
/// The archetype the drawing tools stamp on [`place_current`](EditorState::place_current).
|
||||
/// Defaults to [`Archetype::Wall`]; the UI to change it comes later.
|
||||
current_archetype: Archetype,
|
||||
/// The glyph the drawing tools stamp. Starts as the current archetype's default
|
||||
/// glyph and is edited via the glyph picker (`g`).
|
||||
current_glyph: Glyph,
|
||||
/// Whether draw mode is on: when set, moving the cursor stamps the current thing
|
||||
/// into each newly-entered cell (as if `space` were pressed after the move).
|
||||
draw_mode: bool,
|
||||
/// Width of the right-hand sidebar in terminal columns (mouse-resizable).
|
||||
sidebar_width: u16,
|
||||
/// Whether the blinking cursor is currently in its visible phase.
|
||||
@@ -87,6 +101,9 @@ impl EditorState {
|
||||
code_editor: None,
|
||||
glyph_dialog: None,
|
||||
cursor: (0, 0),
|
||||
current_archetype: Archetype::Wall,
|
||||
current_glyph: Archetype::Wall.default_glyph(),
|
||||
draw_mode: false,
|
||||
sidebar_width: DEFAULT_SIDEBAR_WIDTH,
|
||||
blink_on: true,
|
||||
blink_timer: 0.0,
|
||||
@@ -128,11 +145,15 @@ impl EditorState {
|
||||
/// Opens a text dialog to rename the world; OK writes [`World::name`].
|
||||
pub(crate) fn open_name_dialog(&mut self) {
|
||||
let current = self.world.name.clone();
|
||||
self.dialog = Some(Dialog::text("World Name", current, |opt, ed: &mut EditorState| {
|
||||
if let Some(s) = opt {
|
||||
ed.world.name = s;
|
||||
}
|
||||
}));
|
||||
self.dialog = Some(Dialog::text(
|
||||
"World Name",
|
||||
current,
|
||||
|opt, ed: &mut EditorState| {
|
||||
if let Some(s) = opt {
|
||||
ed.world.name = s;
|
||||
}
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
/// Opens a list dialog to choose the entry board; selecting one writes [`World::start`].
|
||||
@@ -155,22 +176,27 @@ impl EditorState {
|
||||
pub(crate) fn open_scripts_dialog(&mut self) {
|
||||
let mut names: Vec<String> = self.world.scripts.keys().cloned().collect();
|
||||
names.sort();
|
||||
self.dialog = Some(Dialog::list("Scripts", names, true, |resp, ed: &mut EditorState| {
|
||||
match resp {
|
||||
self.dialog = Some(Dialog::list(
|
||||
"Scripts",
|
||||
names,
|
||||
true,
|
||||
|resp, ed: &mut EditorState| match resp {
|
||||
ListDialogResponse::Select(name) | ListDialogResponse::Create(name) => {
|
||||
let source = ed.world.scripts.get(&name).cloned().unwrap_or_default();
|
||||
ed.code_editor = Some(CodeEditor::new(&name, &source));
|
||||
}
|
||||
ListDialogResponse::Cancel => {}
|
||||
}
|
||||
}));
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
/// Closes the code editor, saving its text back into the in-memory script pool
|
||||
/// (mirrors how the Name/Entry-board dialogs mutate the loaded `World`).
|
||||
pub(crate) fn close_script_editor(&mut self) {
|
||||
if let Some(editor) = self.code_editor.take() {
|
||||
self.world.scripts.insert(editor.name().to_string(), editor.text());
|
||||
self.world
|
||||
.scripts
|
||||
.insert(editor.name().to_string(), editor.text());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,35 +204,54 @@ impl EditorState {
|
||||
pub(crate) fn open_boards_dialog(&mut self) {
|
||||
let mut names: Vec<String> = self.world.boards.keys().cloned().collect();
|
||||
names.sort();
|
||||
self.dialog = Some(Dialog::list("Boards", names, false, |_resp, _ed: &mut EditorState| {}));
|
||||
self.dialog = Some(Dialog::list(
|
||||
"Boards",
|
||||
names,
|
||||
false,
|
||||
|_resp, _ed: &mut EditorState| {},
|
||||
));
|
||||
}
|
||||
|
||||
/// Opens the glyph picker seeded from the glyph currently under the cursor.
|
||||
/// On OK the chosen glyph is logged (there is no paint tool yet).
|
||||
/// Opens the glyph picker seeded from the current drawing glyph. On OK the chosen
|
||||
/// glyph becomes the new drawing glyph (used by subsequent placements).
|
||||
pub(crate) fn open_glyph_picker(&mut self) {
|
||||
let glyph = {
|
||||
let board = self.board();
|
||||
board.glyph_at(self.cursor.0 as usize, self.cursor.1 as usize)
|
||||
};
|
||||
self.glyph_dialog = Some(GlyphDialog::new(
|
||||
"Glyph",
|
||||
glyph,
|
||||
self.current_glyph,
|
||||
|chosen, ed: &mut EditorState| {
|
||||
if let Some(g) = chosen {
|
||||
ed.log.push(LogLine::raw(format!(
|
||||
"glyph tile={} fg=#{:02X}{:02X}{:02X} bg=#{:02X}{:02X}{:02X}",
|
||||
g.tile, g.fg.r, g.fg.g, g.fg.b, g.bg.r, g.bg.g, g.bg.b
|
||||
)));
|
||||
ed.current_glyph = g;
|
||||
}
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
/// Stamps the current drawing thing (archetype + glyph) into the cell under the
|
||||
/// cursor, applying the placement rules (see [`Board::place_archetype`]).
|
||||
pub(crate) fn place_current(&mut self) {
|
||||
let (x, y) = (self.cursor.0 as usize, self.cursor.1 as usize);
|
||||
let (arch, glyph) = (self.current_archetype, self.current_glyph);
|
||||
self.board_mut().place_archetype(x, y, arch, glyph);
|
||||
}
|
||||
|
||||
/// Toggles draw mode (whether cursor movement auto-stamps the current thing).
|
||||
pub(crate) fn toggle_draw_mode(&mut self) {
|
||||
self.draw_mode = !self.draw_mode;
|
||||
if self.draw_mode {
|
||||
self.place_current()
|
||||
}
|
||||
}
|
||||
|
||||
/// Borrows the board currently being edited.
|
||||
pub(crate) fn board(&self) -> Ref<'_, Board> {
|
||||
self.world.boards[&self.board_name].borrow()
|
||||
}
|
||||
|
||||
/// Mutably borrows the board currently being edited.
|
||||
pub(crate) fn board_mut(&self) -> RefMut<'_, Board> {
|
||||
self.world.boards[&self.board_name].borrow_mut()
|
||||
}
|
||||
|
||||
/// Advances the cursor blink by `dt` seconds (the editor's only animation).
|
||||
pub(crate) fn tick(&mut self, dt: f32) {
|
||||
self.blink_timer += dt;
|
||||
@@ -216,22 +261,33 @@ impl EditorState {
|
||||
}
|
||||
}
|
||||
|
||||
/// Moves the cursor by `(dx, dy)`, clamped to the board bounds.
|
||||
/// Moves the cursor by `(dx, dy)`, clamped to the board bounds. When draw mode is
|
||||
/// on and the cursor lands on a new cell, stamps the current thing there.
|
||||
pub(crate) fn move_cursor(&mut self, dx: i32, dy: i32) {
|
||||
let (w, h) = {
|
||||
let b = self.board();
|
||||
(b.width as i32, b.height as i32)
|
||||
};
|
||||
let before = self.cursor;
|
||||
self.cursor.0 = (self.cursor.0 + dx).clamp(0, w - 1);
|
||||
self.cursor.1 = (self.cursor.1 + dy).clamp(0, h - 1);
|
||||
if self.draw_mode && self.cursor != before {
|
||||
self.place_current();
|
||||
}
|
||||
}
|
||||
|
||||
/// Moves the cursor to the cell under a screen position, if it lands on the
|
||||
/// board (right-click handling). No-op when the click is off the board.
|
||||
/// board (right-click handling). No-op when the click is off the board. When draw
|
||||
/// mode is on and the cursor lands on a new cell, stamps the current thing there.
|
||||
pub(crate) fn set_cursor_at_screen(&mut self, sx: u16, sy: u16) {
|
||||
let cell = screen_to_board(self.last_board_area, &self.board(), sx, sy);
|
||||
if let Some((bx, by)) = cell {
|
||||
self.cursor = (bx as i32, by as i32);
|
||||
let target = (bx as i32, by as i32);
|
||||
let moved = target != self.cursor;
|
||||
self.cursor = target;
|
||||
if self.draw_mode && moved {
|
||||
self.place_current();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,7 +339,9 @@ pub(crate) fn handle_glyph_dialog_input(event: Event, ed: &mut EditorState) {
|
||||
///
|
||||
/// Forwards key/mouse events to the editor; on `Exit` (Esc) it closes and saves the script.
|
||||
pub(crate) fn handle_code_editor_input(event: Event, ed: &mut EditorState) {
|
||||
let Some(editor) = ed.code_editor.as_mut() else { return };
|
||||
let Some(editor) = ed.code_editor.as_mut() else {
|
||||
return;
|
||||
};
|
||||
if let CodeEditorOutcome::Exit = editor.handle_event(&event) {
|
||||
ed.close_script_editor();
|
||||
}
|
||||
@@ -350,7 +408,7 @@ pub(crate) fn draw_editor(frame: &mut Frame, ed: &mut EditorState, ui: &mut Ui)
|
||||
let mut lines: Vec<Line> = Vec::new();
|
||||
for entry in ed.current_menu().entries() {
|
||||
match entry {
|
||||
MenuEntry::Item{ key, label, .. } => {
|
||||
MenuEntry::Item { key, label, .. } => {
|
||||
lines.push(Line::from(vec![
|
||||
Span::styled(format!("[{}] ", key), key_style),
|
||||
Span::styled(label, label_style),
|
||||
@@ -373,7 +431,16 @@ pub(crate) fn draw_editor(frame: &mut Frame, ed: &mut EditorState, ui: &mut Ui)
|
||||
"[esc] menu",
|
||||
Style::default().fg(Color::DarkGray),
|
||||
)));
|
||||
frame.render_widget(Paragraph::new(lines), sb_inner);
|
||||
// Split the sidebar: the menu fills the top, the drawing-controls footer (the
|
||||
// current thing being drawn) is pinned to the bottom regardless of menu level.
|
||||
let [menu_area, footer_area] =
|
||||
Layout::vertical([Constraint::Min(0), Constraint::Length(DRAW_FOOTER_HEIGHT)])
|
||||
.areas(sb_inner);
|
||||
frame.render_widget(Paragraph::new(lines), menu_area);
|
||||
frame.render_widget(
|
||||
Paragraph::new(draw_footer_lines(ed, key_style, label_style, sep_style)),
|
||||
footer_area,
|
||||
);
|
||||
|
||||
// ── Log panel (full width, below board + sidebar) ─────────────────────────
|
||||
if let Some(log_area) = log_area {
|
||||
@@ -381,6 +448,46 @@ pub(crate) fn draw_editor(frame: &mut Frame, ed: &mut EditorState, ui: &mut Ui)
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds the drawing-controls footer: the current archetype name, the glyph control
|
||||
/// (label + a live preview of the current drawing glyph), and the place / draw-mode
|
||||
/// key hints. Shown at the bottom of the sidebar regardless of the open menu level.
|
||||
fn draw_footer_lines<'a>(
|
||||
ed: &EditorState,
|
||||
key_style: Style,
|
||||
label_style: Style,
|
||||
sep_style: Style,
|
||||
) -> Vec<Line<'a>> {
|
||||
// A one-cell preview of the current glyph in its own fg/bg colors.
|
||||
let preview = Span::styled(
|
||||
tile_to_char(ed.current_glyph.tile).to_string(),
|
||||
Style::default()
|
||||
.fg(rgba8_to_color(ed.current_glyph.fg))
|
||||
.bg(rgba8_to_color(ed.current_glyph.bg)),
|
||||
);
|
||||
vec![
|
||||
// Separator marking off the footer from the menu above.
|
||||
Line::from(Span::styled("─".repeat(64), sep_style)),
|
||||
// The archetype currently being drawn (no UI to change it yet).
|
||||
Line::from(Span::styled(ed.current_archetype.name(), label_style)),
|
||||
Line::from(vec![
|
||||
Span::styled("[g] ", key_style),
|
||||
Span::styled("Glyph ", label_style),
|
||||
preview,
|
||||
]),
|
||||
Line::from(vec![
|
||||
Span::styled("[spc] ", key_style),
|
||||
Span::styled("Place", label_style),
|
||||
]),
|
||||
Line::from(vec![
|
||||
Span::styled("[tab] ", key_style),
|
||||
Span::styled(
|
||||
format!("Draw mode ({})", if ed.draw_mode { "on" } else { "off" }),
|
||||
label_style,
|
||||
),
|
||||
]),
|
||||
]
|
||||
}
|
||||
|
||||
/// Computes the board / sidebar / optional-log rects for the editor.
|
||||
///
|
||||
/// When the log is open the screen splits vertically first (so the log spans the
|
||||
|
||||
@@ -126,8 +126,11 @@ pub(crate) fn handle_editor_input(event: Event, term_w: u16, ed: &mut EditorStat
|
||||
KeyCode::Left => ed.move_cursor(-1, 0),
|
||||
KeyCode::Right => ed.move_cursor(1, 0),
|
||||
KeyCode::Char('l') => ui.log.toggle(),
|
||||
// `g` opens the glyph picker for the cell under the cursor.
|
||||
// `g` opens the glyph picker to edit the current drawing glyph.
|
||||
KeyCode::Char('g') => ed.open_glyph_picker(),
|
||||
// Space stamps the current thing; Tab toggles draw mode.
|
||||
KeyCode::Char(' ') => ed.place_current(),
|
||||
KeyCode::Tab => ed.toggle_draw_mode(),
|
||||
// A menu letter opens its dialog; unmatched letters are ignored.
|
||||
KeyCode::Char(c) => {
|
||||
ed.menu_key(c);
|
||||
|
||||
Reference in New Issue
Block a user