create "closed" and "opened" states for the editor

gui-branch
an 2019-03-27 06:13:53 -04:00
parent da542c84e8
commit 3f3900d91c
2 changed files with 64 additions and 29 deletions

View File

@ -8,34 +8,65 @@ use maraiah::{durandal::image::*,
marathon::map,
rozinante::{color::*, draw::*}};
impl MapEditorState
impl MapEditor
{
/// Creates a new empty map.
pub fn new() -> MapEditorState
/// Creates a closed map editor.
#[inline]
pub const fn new_closed() -> Self
{
MapEditorState::default()
MapEditor::Closed
}
/// Draws the "no map" screen.
pub fn draw_none<D, I>(d: &D, im: &I)
/// Creates a new empty map.
#[inline]
pub fn new_opened() -> Self
{
MapEditor::Opened(MapEditorState::default())
}
/// Draws the screen for this editor state.
pub fn draw<D, I>(&self, d: &D, im: &I)
where D: DrawArea<NativeImage = I>,
I: CacheImage
{
d.clear(Color16::new(0, 0, 0));
match self {
MapEditor::Closed => {
d.clear(Color16::new(0, 0, 0));
d.image((d.w() / 2 - im.w() / 2, d.h() / 2 - im.h() / 2), im);
d.image((d.w() / 2 - im.w() / 2, d.h() / 2 - im.h() / 2), im);
d.rect(Rect{x: 0, y: 0, w: d.w(), h: 18}, CR_DARK_RED);
d.text((4, 14), "Map Required To Proceed", CR_RED);
let text = "Map Required To Proceed";
d.rect(Rect{x: 0, y: 0, w: d.w(), h: 18}, CR_DARK_RED);
d.text((4, 14), text, CR_RED);
d.rect(Rect{x: 0, y: d.h() - 18, w: d.w(), h: 18}, CR_DARK_RED);
d.text((4, d.h() - 4), "CAS.qterm//CyberAcme Systems Inc.", CR_RED);
let text = "CAS.qterm//CyberAcme Systems Inc.";
d.rect(Rect{x: 0, y: d.h() - 18, w: d.w(), h: 18}, CR_DARK_RED);
d.text((4, d.h() - 4), text, CR_RED);
}
MapEditor::Opened(_st) => {
d.clear(Color16::new(0, 0, 0));
}
}
}
/// Draws the currently open map.
pub fn draw_some(&self, d: &impl DrawArea)
/// Returns `true` if `self` is closed.
#[inline]
pub fn is_closed(&self) -> bool
{
d.clear(Color16::new(0, 0, 0));
match self {
MapEditor::Closed => true,
MapEditor::Opened(_) => false,
}
}
/// Returns `true` if `self` is opened.
#[inline]
pub fn is_opened(&self) -> bool
{
match self {
MapEditor::Closed => false,
MapEditor::Opened(_) => true,
}
}
}
@ -51,7 +82,7 @@ pub struct MapEditorStateBlock
info: map::Minf,
}
/// The state of a map editor.
/// The state of an opened map editor.
#[derive(Default)]
pub struct MapEditorState
{
@ -59,6 +90,13 @@ pub struct MapEditorState
tool: Tool,
}
/// An entire map editor, which may be opened or closed.
pub enum MapEditor
{
Closed,
Opened(MapEditorState),
}
/// A tool in the map editor.
pub enum Tool
{

View File

@ -1,7 +1,7 @@
mod editor;
mod interfaces;
use crate::{editor::MapEditorState,
use crate::{editor::MapEditor,
interfaces::*};
use gdk_pixbuf_sys::*;
use gdk_sys::*;
@ -17,7 +17,7 @@ unsafe extern "C" fn app_activate(app: *mut GtkApplication, edit: gpointer)
{
// this ref will be cloned around a bit, but will ultimately be dropped at
// the end of this function.
let edit = Rc::from_raw(edit as *mut Option<MapEditorState>);
let edit = Rc::from_raw(edit as *mut MapEditor);
setup_css();
@ -35,14 +35,14 @@ unsafe extern "C" fn app_activate(app: *mut GtkApplication, edit: gpointer)
}
/// Sets up the map view window's drawing area.
unsafe fn setup_draw_area(b: *mut GtkBuilder, edit: Rc<Option<MapEditorState>>)
unsafe fn setup_draw_area(b: *mut GtkBuilder, edit: Rc<MapEditor>)
{
struct RenderState
{
im_nomap: *mut GdkPixbuf,
ax: *mut GtkAdjustment,
ay: *mut GtkAdjustment,
edit: Rc<Option<MapEditorState>>,
edit: Rc<MapEditor>,
}
/// Callback to finalize the drawing area.
@ -78,10 +78,7 @@ unsafe fn setup_draw_area(b: *mut GtkBuilder, edit: Rc<Option<MapEditorState>>)
let im = CrImage(rend.im_nomap);
let dr = CrDrawArea::new(ctx, w, h);
match &*rend.edit {
Some(edit) => edit.draw_some(&dr),
None => MapEditorState::draw_none(&dr, &im),
}
rend.edit.draw(&dr, &im);
1
}
@ -217,7 +214,7 @@ unsafe fn setup_explicit_drop(b: *mut GtkBuilder, win: *mut GtkWindow)
/// Sets up the main menu window.
unsafe fn setup_win_main(b: *mut GtkBuilder,
app: *mut GtkApplication,
edit: Rc<Option<MapEditorState>>)
edit: Rc<MapEditor>)
{
/// Callback to close the window when the "Quit" button is pressed.
unsafe extern "C" fn c_quit_act(_: *mut GtkWidget, win: gpointer)
@ -228,10 +225,10 @@ unsafe fn setup_win_main(b: *mut GtkBuilder,
/// Callback to create a new editor state when the "New" button is pressed.
unsafe extern "C" fn c_new_act(_: *mut GtkWidget, edit: gpointer)
{
let edit = edit as *mut Option<MapEditorState>;
let edit = edit as *mut MapEditor;
let edit = &mut *edit;
if edit.is_some() {
if edit.is_opened() {
let titl = c_str!("Confirm");
let text = c_str!("Are you sure you want to create a new project? \
Data may be lost.");
@ -241,7 +238,7 @@ unsafe fn setup_win_main(b: *mut GtkBuilder,
}
}
*edit = Some(MapEditorState::new());
*edit = MapEditor::new_opened();
}
/// Callback to finalize the editor state reference.
@ -379,7 +376,7 @@ fn main()
g_static_resource_init(&mut resource);
// create a container for the editor state
let edit = Rc::new(None::<MapEditorState>);
let edit = Rc::new(MapEditor::new_closed());
let eptr = Rc::into_raw(edit.clone());
// create and run the app