Maraiah/source/rozinante/editor.rs

82 lines
2.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;
mod state;
2019-03-27 11:07:33 -07:00
use super::{color, draw::*};
2019-04-01 06:05:06 -07:00
use crate::durandal::image::*;
2019-03-24 17:04:49 -07:00
impl MapEditor
2019-03-24 17:04:49 -07:00
{
/// Opens the editor with a new empty map.
#[inline]
2019-04-01 06:05:06 -07:00
pub fn open_new(&mut self) {self.map = Some(state::OpenMap::default());}
2019-03-31 11:58:36 -07:00
/// Opens the editor with an existing map.
pub fn open_buf(&mut self, b: &[u8])
{
self.map = Some(state::OpenMap::open_buf(b));
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-03-27 04:56:30 -07:00
let dw = d.w();
let dh = d.h();
let iw = im.w();
let ih = im.h();
2019-03-31 11:58:36 -07:00
match &self.map {
None => {
let tx_top = "Map Required To Proceed";
let tx_bot = "CAS.qterm//CyberAcme Systems Inc.";
d.clear(Color16::new(0, 0, 0));
2019-03-24 17:04:49 -07:00
d.image((dw / 2 - iw / 2, dh / 2 - ih / 2), im);
2019-03-24 17:04:49 -07:00
2019-03-27 11:07:33 -07:00
d.rect(Rect{x: 0, y: 0, w: dw, h: 18}, color::DARK_RED);
d.text((4, 14), tx_top, color::RED);
2019-03-24 17:04:49 -07:00
2019-03-27 11:07:33 -07:00
d.rect(Rect{x: 0, y: dh - 18, w: dw, h: 18}, color::DARK_RED);
d.text((4, dh - 4), tx_bot, color::RED);
}
2019-03-31 11:58:36 -07:00
Some(st) => {
let text = &format!("tool: {:?}", st.tool());
2019-03-27 14:02:15 -07:00
d.clear(Color16::new(0, 0, 0));
2019-04-01 06:05:06 -07:00
d.text((dw / 2, dh / 2), text, color::RED);
}
}
2019-03-24 17:04:49 -07:00
}
2019-03-27 02:32:25 -07:00
/// Returns `true` if `self` is closed.
#[inline]
2019-04-01 06:05:06 -07:00
pub fn is_closed(&self) -> bool {self.map.is_none()}
/// Returns `true` if `self` is opened.
#[inline]
2019-04-01 06:05:06 -07:00
pub fn is_opened(&self) -> bool {self.map.is_some()}
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-03-31 11:58:36 -07:00
fn default() -> Self {Self{map: None}}
2019-03-27 02:32:25 -07:00
}
2019-03-31 11:58:36 -07:00
/// An entire map editor, which may be opened or closed. Holds state which
/// outlives the opened map state.
pub struct MapEditor
{
2019-03-31 11:58:36 -07:00
map: Option<state::OpenMap>,
2019-03-27 02:32:25 -07:00
}
2019-03-24 17:04:49 -07:00
// EOF