Maraiah/source/rozinante/editor.rs

130 lines
3.0 KiB
Rust
Raw Normal View History

2019-03-24 17:04:49 -07:00
//! Main map editor module.
//!
//! The entry point is responsible for maintaining the lifetime of the editor
2019-03-27 14:02:15 -07:00
//! and human interactions with it, but is otherwise not permitted to directly
//! edit it.
2019-03-24 17:04:49 -07:00
2019-03-31 11:58:36 -07:00
mod block;
2019-04-03 18:30:22 -07:00
pub use block::*;
2019-03-27 11:07:33 -07:00
use super::{color, draw::*};
2019-04-11 21:04:39 -07:00
use crate::{durandal::image::*, marathon::{machdr, map}};
2019-03-24 17:04:49 -07:00
impl MapEditor
2019-03-24 17:04:49 -07:00
{
2019-04-03 18:30:22 -07:00
/// Opens the editor with a new map.
pub fn open_new(&mut self)
{
*self = Self::default();
}
2019-03-31 11:58:36 -07:00
/// Opens the editor with an existing map.
pub fn open_buf(&mut self, b: &[u8])
{
2019-04-03 18:30:22 -07:00
// TODO: handle errors gracefully
let b = &b[machdr::try_mac_header(b)..];
2019-04-11 21:04:39 -07:00
let wad = map::read(b).unwrap();
2019-04-03 18:30:22 -07:00
let ent = wad.entries.iter().nth(0).unwrap().1;
let info = ent.chunks.iter().find_map(|cnk| {
match cnk {
2019-04-11 21:04:39 -07:00
map::chnk::Chunk::Minf(info) => Some(info),
2019-04-03 18:30:22 -07:00
_ => None,
}
}).unwrap().clone();
let block = Block{info};
dbg!(&block);
*self = Self{blocks: vec![block],
tools: Self::default_tools()};
2019-03-24 17:04:49 -07:00
}
/// Draws the screen for this editor state.
2019-03-27 04:56:30 -07:00
pub fn draw<D, I>(&self, d: &mut D, im: &I)
2019-03-24 17:04:49 -07:00
where D: DrawArea<NativeImage = I>,
I: CacheImage
{
2019-04-03 18:30:22 -07:00
/*
2019-03-27 04:56:30 -07:00
let dw = d.w();
let dh = d.h();
let iw = im.w();
let ih = im.h();
2019-04-03 18:30:22 -07:00
let tx_top = "Map Required To Proceed";
let tx_bot = "CAS.qterm//CyberAcme Systems Inc.";
2019-04-03 18:30:22 -07:00
d.clear(Color16::new(0, 0, 0));
2019-03-24 17:04:49 -07:00
2019-04-03 18:30:22 -07:00
d.image((dw / 2 - iw / 2, dh / 2 - ih / 2), im);
2019-03-24 17:04:49 -07:00
2019-04-03 18:30:22 -07:00
d.rect(Rect{x: 0, y: 0, w: dw, h: 18}, color::DARK_RED);
d.text((4, 0), tx_top, color::RED);
2019-03-24 17:04:49 -07:00
2019-04-03 18:30:22 -07:00
d.rect(Rect{x: 0, y: dh - 18, w: dw, h: 18}, color::DARK_RED);
d.text((4, dh - 16), tx_bot, color::RED);
*/
2019-03-27 14:02:15 -07:00
2019-04-03 18:30:22 -07:00
let _ = im;
let text = &format!("{:#?}", &self);
d.clear(Color16::new(0, 0, 0));
d.text((0, 0), text, color::RED);
2019-03-24 17:04:49 -07:00
}
2019-03-27 02:32:25 -07:00
2019-04-03 18:30:22 -07:00
// Returns the default tools.
const fn default_tools() -> (Tool, Tool) {(Tool::Points, Tool::Lines)}
2019-04-03 18:30:22 -07:00
/// Returns a reference to the current block.
pub fn cur_block(&self) -> &Block {self.blocks.last().unwrap()}
/// Pushes a new block.
pub fn push_block(&mut self, blk: Block) {self.blocks.push(blk);}
/// Returns the current tool.
pub fn tool(&self) -> &Tool {&self.tools.0}
/// Sets the current tool, and returns the previous one.
pub fn set_tool(&mut self, t: Tool) -> &Tool
{
self.tools.1 = self.tools.0.clone();
self.tools.0 = t;
&self.tools.1
}
/// Returns true if the current map is unclean and needs to be saved.
pub fn unclean(&self) -> bool {true}
2019-03-24 17:04:49 -07:00
}
2019-03-27 11:07:33 -07:00
impl Default for MapEditor
2019-03-24 17:04:49 -07:00
{
2019-03-27 11:07:33 -07:00
#[inline]
2019-04-03 18:30:22 -07:00
fn default() -> Self
{
Self{blocks: vec![Block::default()], tools: Self::default_tools()}
}
2019-03-27 02:32:25 -07:00
}
2019-04-03 18:30:22 -07:00
/// The state of an opened map editor.
#[derive(Debug)]
2019-03-31 11:58:36 -07:00
pub struct MapEditor
{
2019-04-03 18:30:22 -07:00
blocks: Vec<Block>,
tools: (Tool, Tool),
}
/// A tool in the map editor.
#[derive(Clone, Debug)]
pub enum Tool
{
Points,
Lines,
Polygons,
2019-03-27 02:32:25 -07:00
}
2019-03-24 17:04:49 -07:00
// EOF