unnecessary complication
parent
d56b3db1fe
commit
75e354a16b
|
@ -1,8 +1,8 @@
|
||||||
//! Main map editor module.
|
//! Main map editor module.
|
||||||
//!
|
//!
|
||||||
//! The entry point is responsible for maintaining the lifetime of the editor
|
//! The entry point is responsible for maintaining the lifetime of the editor
|
||||||
//! state and human interactions with it, but is otherwise not permitted to
|
//! and human interactions with it, but is otherwise not permitted to directly
|
||||||
//! directly edit it.
|
//! edit it.
|
||||||
|
|
||||||
pub mod block;
|
pub mod block;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
|
@ -12,18 +12,11 @@ use super::{color, draw::*};
|
||||||
|
|
||||||
impl MapEditor
|
impl MapEditor
|
||||||
{
|
{
|
||||||
/// Creates a closed map editor.
|
|
||||||
#[inline]
|
|
||||||
pub const fn new_closed() -> Self
|
|
||||||
{
|
|
||||||
MapEditor::Closed
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Opens the editor with a new empty map.
|
/// Opens the editor with a new empty map.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn open_new(&mut self)
|
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.
|
/// Draws the screen for this editor state.
|
||||||
|
@ -52,8 +45,10 @@ impl MapEditor
|
||||||
d.text((4, dh - 4), tx_bot, color::RED);
|
d.text((4, dh - 4), tx_bot, color::RED);
|
||||||
}
|
}
|
||||||
MapEditor::Opened(st) => {
|
MapEditor::Opened(st) => {
|
||||||
|
let text = &format!("tool: {:?}", st.tool);
|
||||||
|
|
||||||
d.clear(Color16::new(0, 0, 0));
|
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
|
impl Default for MapEditor
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn default() -> Self {Self::new_closed()}
|
fn default() -> Self {MapEditor::Closed}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An entire map editor, which may be opened or closed.
|
/// An entire map editor, which may be opened or closed.
|
||||||
pub enum MapEditor
|
pub enum MapEditor
|
||||||
{
|
{
|
||||||
Closed,
|
Closed,
|
||||||
Opened(state::State),
|
Opened(state::OpenMap),
|
||||||
}
|
}
|
||||||
|
|
||||||
// EOF
|
// EOF
|
||||||
|
|
|
@ -9,7 +9,7 @@ impl Default for Block
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Copyable map state.
|
/// Copyable, versioned map state.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Block
|
pub struct Block
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
|
//! Map editor state.
|
||||||
|
|
||||||
use super::block;
|
use super::block;
|
||||||
|
|
||||||
impl State
|
impl OpenMap
|
||||||
{
|
{
|
||||||
fn cur_block(&self) -> &block::Block
|
fn cur_block(&self) -> &block::Block
|
||||||
{
|
{
|
||||||
|
@ -13,7 +15,7 @@ impl State
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for State
|
impl Default for OpenMap
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn default() -> Self
|
fn default() -> Self
|
||||||
|
@ -30,7 +32,7 @@ impl Default for Tool
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The state of an opened map editor.
|
/// The state of an opened map editor.
|
||||||
pub struct State
|
pub struct OpenMap
|
||||||
{
|
{
|
||||||
pub(super) blocks: Vec<block::Block>,
|
pub(super) blocks: Vec<block::Block>,
|
||||||
pub(super) tool: Tool,
|
pub(super) tool: Tool,
|
||||||
|
|
|
@ -1156,7 +1156,7 @@ express Statement of Purpose.
|
||||||
<object class="GtkToolButton" id="btn-point">
|
<object class="GtkToolButton" id="btn-point">
|
||||||
<property name="visible">True</property>
|
<property name="visible">True</property>
|
||||||
<property name="can_focus">False</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="use_underline">True</property>
|
||||||
<property name="icon_widget">img-points</property>
|
<property name="icon_widget">img-points</property>
|
||||||
<child internal-child="accessible">
|
<child internal-child="accessible">
|
||||||
|
@ -1175,7 +1175,7 @@ express Statement of Purpose.
|
||||||
<object class="GtkToolButton" id="btn-lines">
|
<object class="GtkToolButton" id="btn-lines">
|
||||||
<property name="visible">True</property>
|
<property name="visible">True</property>
|
||||||
<property name="can_focus">False</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="use_underline">True</property>
|
||||||
<property name="icon_widget">img-lines</property>
|
<property name="icon_widget">img-lines</property>
|
||||||
<child internal-child="accessible">
|
<child internal-child="accessible">
|
||||||
|
@ -1195,7 +1195,7 @@ express Statement of Purpose.
|
||||||
<object class="GtkToolButton" id="btn-polys">
|
<object class="GtkToolButton" id="btn-polys">
|
||||||
<property name="visible">True</property>
|
<property name="visible">True</property>
|
||||||
<property name="can_focus">False</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="use_underline">True</property>
|
||||||
<property name="icon_widget">img-polys</property>
|
<property name="icon_widget">img-polys</property>
|
||||||
<child internal-child="accessible">
|
<child internal-child="accessible">
|
||||||
|
|
|
@ -3,7 +3,11 @@
|
||||||
use cairo_sys::*;
|
use cairo_sys::*;
|
||||||
use gdk_pixbuf_sys::*;
|
use gdk_pixbuf_sys::*;
|
||||||
use gdk_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.
|
/// Converts a `Color` to a `f64` triple.
|
||||||
fn flt_color(cr: impl Color) -> (f64, f64, f64)
|
fn flt_color(cr: impl Color) -> (f64, f64, f64)
|
||||||
|
@ -98,7 +102,7 @@ impl DrawArea for CrDrawArea
|
||||||
let x = f64::from(pos.0);
|
let x = f64::from(pos.0);
|
||||||
let y = f64::from(pos.1);
|
let y = f64::from(pos.1);
|
||||||
|
|
||||||
let text = CString::new(text).unwrap();
|
let text = ffi::CString::new(text).unwrap();
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
cairo_set_source_rgb(self.ctx, r, g, b);
|
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`.
|
/// An image for a `CrDrawArea`.
|
||||||
pub struct CrImage(pub *const GdkPixbuf);
|
pub struct CrImage(pub *const GdkPixbuf);
|
||||||
|
|
||||||
|
@ -130,4 +181,14 @@ pub struct CrDrawArea
|
||||||
h: Coord,
|
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
|
// EOF
|
||||||
|
|
|
@ -7,7 +7,7 @@ use gio_sys::*;
|
||||||
use glib_sys::*;
|
use glib_sys::*;
|
||||||
use gobject_sys::*;
|
use gobject_sys::*;
|
||||||
use gtk_sys::*;
|
use gtk_sys::*;
|
||||||
use maraiah::{c_str, durandal::ffi, rozinante::editor};
|
use maraiah::{c_str, durandal::ffi};
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
/// Called when the application activates in order to set everything up.
|
/// 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"));
|
let wid: *mut GtkDrawingArea = get_obj(b, c_str!("draw-area"));
|
||||||
|
|
||||||
// add a callback to draw the area when updated
|
edit.borrow_mut().set_draw(wid as _);
|
||||||
edit.borrow_mut().call.push(Box::new(move || {
|
|
||||||
gtk_widget_queue_draw(wid as _);
|
|
||||||
}));
|
|
||||||
|
|
||||||
// get all of the necessary state and related objects
|
// get all of the necessary state and related objects
|
||||||
let ax: *mut GtkAdjustment = get_obj(b, c_str!("adj-map-horz"));
|
let ax: *mut GtkAdjustment = get_obj(b, c_str!("adj-map-horz"));
|
||||||
|
@ -384,9 +381,7 @@ fn main()
|
||||||
g_static_resource_init(&mut resource);
|
g_static_resource_init(&mut resource);
|
||||||
|
|
||||||
// create a container for the editor state
|
// create a container for the editor state
|
||||||
let edit = MapEditor{edit: editor::MapEditor::new_closed(),
|
let edit = MapEditor::default();
|
||||||
call: Vec::new(),
|
|
||||||
done: Vec::new()};
|
|
||||||
let edit = RefCell::new(edit);
|
let edit = RefCell::new(edit);
|
||||||
let edit = Rc::new(edit);
|
let edit = Rc::new(edit);
|
||||||
let eptr = Rc::into_raw(edit.clone());
|
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
|
// EOF
|
||||||
|
|
Loading…
Reference in New Issue