diff --git a/source/rozinante/editor.rs b/source/rozinante/editor.rs index 3192f0d..30ec7ee 100644 --- a/source/rozinante/editor.rs +++ b/source/rozinante/editor.rs @@ -1,8 +1,8 @@ //! Main map editor module. //! //! The entry point is responsible for maintaining the lifetime of the editor -//! state and human interactions with it, but is otherwise not permitted to -//! directly edit it. +//! and human interactions with it, but is otherwise not permitted to directly +//! edit it. pub mod block; pub mod state; @@ -12,18 +12,11 @@ use super::{color, draw::*}; impl MapEditor { - /// Creates a closed map editor. - #[inline] - pub const fn new_closed() -> Self - { - MapEditor::Closed - } - /// Opens the editor with a new empty map. #[inline] pub fn open_new(&mut self) { - *self = MapEditor::Opened(state::State::default()); + *self = MapEditor::Opened(state::OpenMap::default()); } /// Draws the screen for this editor state. @@ -52,8 +45,10 @@ impl MapEditor d.text((4, dh - 4), tx_bot, color::RED); } MapEditor::Opened(st) => { + let text = &format!("tool: {:?}", st.tool); + d.clear(Color16::new(0, 0, 0)); - d.text((dw/2, dh/2), &format!("tool: {:?}", st.tool), color::RED); + d.text((dw/2, dh/2), text, color::RED); } } } @@ -82,14 +77,14 @@ impl MapEditor impl Default for MapEditor { #[inline] - fn default() -> Self {Self::new_closed()} + fn default() -> Self {MapEditor::Closed} } /// An entire map editor, which may be opened or closed. pub enum MapEditor { Closed, - Opened(state::State), + Opened(state::OpenMap), } // EOF diff --git a/source/rozinante/editor/block.rs b/source/rozinante/editor/block.rs index 9f03e7b..439b810 100644 --- a/source/rozinante/editor/block.rs +++ b/source/rozinante/editor/block.rs @@ -9,7 +9,7 @@ impl Default for Block } } -/// Copyable map state. +/// Copyable, versioned map state. #[derive(Clone)] pub struct Block { diff --git a/source/rozinante/editor/state.rs b/source/rozinante/editor/state.rs index 61bfe4f..6bdc0bf 100644 --- a/source/rozinante/editor/state.rs +++ b/source/rozinante/editor/state.rs @@ -1,6 +1,8 @@ +//! Map editor state. + use super::block; -impl State +impl OpenMap { fn cur_block(&self) -> &block::Block { @@ -13,7 +15,7 @@ impl State } } -impl Default for State +impl Default for OpenMap { #[inline] fn default() -> Self @@ -30,7 +32,7 @@ impl Default for Tool } /// The state of an opened map editor. -pub struct State +pub struct OpenMap { pub(super) blocks: Vec, pub(super) tool: Tool, diff --git a/source/tycho/data/ui.xml b/source/tycho/data/ui.xml index be86b96..70b0c46 100644 --- a/source/tycho/data/ui.xml +++ b/source/tycho/data/ui.xml @@ -1156,7 +1156,7 @@ express Statement of Purpose. True False - Points + _Points True img-points @@ -1175,7 +1175,7 @@ express Statement of Purpose. True False - Lines + _Lines True img-lines @@ -1195,7 +1195,7 @@ express Statement of Purpose. True False - Polygons + P_olygons True img-polys diff --git a/source/tycho/interfaces.rs b/source/tycho/interfaces.rs index 1eb8bbf..a245b3f 100644 --- a/source/tycho/interfaces.rs +++ b/source/tycho/interfaces.rs @@ -3,7 +3,11 @@ use cairo_sys::*; use gdk_pixbuf_sys::*; use gdk_sys::*; -use maraiah::{c_str, durandal::{ffi::*, image::*}, rozinante::draw::*}; +use gobject_sys::*; +use gtk_sys::*; +use maraiah::{c_str, + durandal::{ffi, image::*}, + rozinante::{draw::*, editor}}; /// Converts a `Color` to a `f64` triple. fn flt_color(cr: impl Color) -> (f64, f64, f64) @@ -98,7 +102,7 @@ impl DrawArea for CrDrawArea let x = f64::from(pos.0); let y = f64::from(pos.1); - let text = CString::new(text).unwrap(); + let text = ffi::CString::new(text).unwrap(); unsafe { cairo_set_source_rgb(self.ctx, r, g, b); @@ -119,6 +123,53 @@ impl DrawArea for CrDrawArea } } +impl MapEditor +{ + /// Propagates updated information to widgets. + pub fn cause_update(&mut self) + { + unsafe {gtk_widget_queue_draw(self.draw);} + } + + /// Sets the drawing area widget. + pub fn set_draw(&mut self, draw: *mut GtkWidget) + { + self.draw = draw; + unsafe {g_object_ref(self.draw as _);} + } +} + +impl Default for MapEditor +{ + fn default() -> Self + { + Self{edit: editor::MapEditor::default(), + draw: ffi::null_mut()} + } +} + +impl Drop for MapEditor +{ + fn drop(&mut self) + { + unsafe {g_object_unref(self.draw as _);} + } +} + +impl std::ops::Deref for MapEditor +{ + type Target = editor::MapEditor; + + #[inline] + fn deref(&self) -> &Self::Target {&self.edit} +} + +impl std::ops::DerefMut for MapEditor +{ + #[inline] + fn deref_mut(&mut self) -> &mut Self::Target {&mut self.edit} +} + /// An image for a `CrDrawArea`. pub struct CrImage(pub *const GdkPixbuf); @@ -130,4 +181,14 @@ pub struct CrDrawArea h: Coord, } +/// Specialized map editor which has callbacks for frontend purposes. +pub struct MapEditor +{ + edit: editor::MapEditor, + draw: *mut GtkWidget, +} + +/// A runtime reference to the map editor. +pub type MapEditorRef = std::cell::RefCell; + // EOF diff --git a/source/tycho/main.rs b/source/tycho/main.rs index 7e9f2ef..b7da4cc 100644 --- a/source/tycho/main.rs +++ b/source/tycho/main.rs @@ -7,7 +7,7 @@ use gio_sys::*; use glib_sys::*; use gobject_sys::*; use gtk_sys::*; -use maraiah::{c_str, durandal::ffi, rozinante::editor}; +use maraiah::{c_str, durandal::ffi}; use std::{cell::RefCell, rc::Rc}; /// Called when the application activates in order to set everything up. @@ -84,10 +84,7 @@ unsafe fn setup_draw_area(b: *mut GtkBuilder, edit: Rc) let wid: *mut GtkDrawingArea = get_obj(b, c_str!("draw-area")); - // add a callback to draw the area when updated - edit.borrow_mut().call.push(Box::new(move || { - gtk_widget_queue_draw(wid as _); - })); + edit.borrow_mut().set_draw(wid as _); // get all of the necessary state and related objects let ax: *mut GtkAdjustment = get_obj(b, c_str!("adj-map-horz")); @@ -384,9 +381,7 @@ fn main() g_static_resource_init(&mut resource); // create a container for the editor state - let edit = MapEditor{edit: editor::MapEditor::new_closed(), - call: Vec::new(), - done: Vec::new()}; + let edit = MapEditor::default(); let edit = RefCell::new(edit); let edit = Rc::new(edit); let eptr = Rc::into_raw(edit.clone()); @@ -410,39 +405,4 @@ fn main() } } -impl MapEditor -{ - /// Causes all update callbacks to be called. - fn cause_update(&mut self) {for cb in &mut self.call {cb();}} -} - -impl Drop for MapEditor -{ - /// Calls all finalization callbacks on drop. - fn drop(&mut self) {for cb in &mut self.done {cb();}} -} - -impl std::ops::Deref for MapEditor -{ - type Target = editor::MapEditor; - - fn deref(&self) -> &editor::MapEditor {&self.edit} -} - -impl std::ops::DerefMut for MapEditor -{ - fn deref_mut(&mut self) -> &mut editor::MapEditor {&mut self.edit} -} - -/// Specialized map editor which has callbacks for frontend purposes. -struct MapEditor -{ - edit: editor::MapEditor, - call: Vec>, - done: Vec>, -} - -/// A runtime reference to the map editor. -type MapEditorRef = RefCell; - // EOF