unnecessary complication

gui-branch
an 2019-03-27 17:02:15 -04:00
parent d56b3db1fe
commit 75e354a16b
6 changed files with 83 additions and 65 deletions

View File

@ -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

View File

@ -9,7 +9,7 @@ impl Default for Block
}
}
/// Copyable map state.
/// Copyable, versioned map state.
#[derive(Clone)]
pub struct Block
{

View File

@ -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<block::Block>,
pub(super) tool: Tool,

View File

@ -1156,7 +1156,7 @@ express Statement of Purpose.
<object class="GtkToolButton" id="btn-point">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Points</property>
<property name="label" translatable="yes">_Points</property>
<property name="use_underline">True</property>
<property name="icon_widget">img-points</property>
<child internal-child="accessible">
@ -1175,7 +1175,7 @@ express Statement of Purpose.
<object class="GtkToolButton" id="btn-lines">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Lines</property>
<property name="label" translatable="yes">_Lines</property>
<property name="use_underline">True</property>
<property name="icon_widget">img-lines</property>
<child internal-child="accessible">
@ -1195,7 +1195,7 @@ express Statement of Purpose.
<object class="GtkToolButton" id="btn-polys">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Polygons</property>
<property name="label" translatable="yes">P_olygons</property>
<property name="use_underline">True</property>
<property name="icon_widget">img-polys</property>
<child internal-child="accessible">

View File

@ -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<MapEditor>;
// EOF

View File

@ -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<MapEditorRef>)
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<Box<dyn FnMut()>>,
done: Vec<Box<dyn FnMut()>>,
}
/// A runtime reference to the map editor.
type MapEditorRef = RefCell<MapEditor>;
// EOF