207 lines
4.4 KiB
Rust
207 lines
4.4 KiB
Rust
//! Implemented interfaces for Rozinante.
|
|
|
|
use cairo_sys::*;
|
|
use gdk_pixbuf_sys::*;
|
|
use gdk_sys::*;
|
|
use gobject_sys::*;
|
|
use gtk_sys::*;
|
|
use maraiah::{c_str,
|
|
durandal::{ffi, image::*},
|
|
rozinante::{draw::*, editor}};
|
|
use std::marker::PhantomData;
|
|
|
|
/// Converts a `Color` to a `f64` triple.
|
|
fn flt_color(cr: impl Color) -> (f64, f64, f64)
|
|
{
|
|
fn flt_color(n: u16) -> f64 {f64::from(n) / f64::from(u16::max_value())}
|
|
|
|
(flt_color(cr.r()), flt_color(cr.g()), flt_color(cr.b()))
|
|
}
|
|
|
|
impl CacheImage for CrImage
|
|
{
|
|
fn w(&self) -> Coord {unsafe {gdk_pixbuf_get_width(self.0) as Coord}}
|
|
fn h(&self) -> Coord {unsafe {gdk_pixbuf_get_height(self.0) as Coord}}
|
|
}
|
|
|
|
impl CrDrawArea
|
|
{
|
|
pub const fn new(ctx: *mut cairo_t, w: f64, h: f64) -> Self
|
|
{
|
|
CrDrawArea{ctx, w: w as Coord, h: h as Coord}
|
|
}
|
|
}
|
|
|
|
impl DrawArea for CrDrawArea
|
|
{
|
|
type NativeImage = CrImage;
|
|
|
|
fn w(&self) -> Coord {self.w}
|
|
fn h(&self) -> Coord {self.h}
|
|
|
|
fn clear(&mut self, cr: impl Color)
|
|
{
|
|
self.rect(Rect{x: 0, y: 0, w: self.w(), h: self.h()}, cr);
|
|
|
|
let sl = FONT_SLANT_NORMAL;
|
|
let wt = FONT_WEIGHT_NORMAL;
|
|
|
|
unsafe {
|
|
cairo_select_font_face(self.ctx, c_str!("Monospace"), sl, wt);
|
|
cairo_set_font_size(self.ctx, 14.0);
|
|
cairo_set_line_width(self.ctx, 1.0);
|
|
}
|
|
}
|
|
|
|
fn line_width(&mut self, width: u8)
|
|
{
|
|
let width = f64::from(width);
|
|
|
|
unsafe {
|
|
cairo_set_line_width(self.ctx, width);
|
|
}
|
|
}
|
|
|
|
fn line(&mut self, p1: Point, p2: Point, cr: impl Color)
|
|
{
|
|
let (r, g, b) = flt_color(cr);
|
|
|
|
let x1 = f64::from(p1.0);
|
|
let y1 = f64::from(p1.1);
|
|
|
|
let x2 = f64::from(p2.0);
|
|
let y2 = f64::from(p2.1);
|
|
|
|
unsafe {
|
|
cairo_set_source_rgb(self.ctx, r, g, b);
|
|
cairo_move_to(self.ctx, x1, y1);
|
|
cairo_line_to(self.ctx, x2, y2);
|
|
cairo_stroke(self.ctx);
|
|
}
|
|
}
|
|
|
|
fn rect(&mut self, rect: Rect, cr: impl Color)
|
|
{
|
|
let px = f64::from(rect.x);
|
|
let py = f64::from(rect.y);
|
|
let sx = f64::from(rect.w);
|
|
let sy = f64::from(rect.h);
|
|
|
|
let (r, g, b) = flt_color(cr);
|
|
|
|
unsafe {
|
|
cairo_set_source_rgb(self.ctx, r, g, b);
|
|
cairo_rectangle(self.ctx, px, py, sx, sy);
|
|
cairo_fill(self.ctx);
|
|
}
|
|
}
|
|
|
|
fn text(&mut self, pos: Point, text: &str, cr: impl Color)
|
|
{
|
|
let (r, g, b) = flt_color(cr);
|
|
|
|
let x = f64::from(pos.0);
|
|
let y = f64::from(pos.1);
|
|
|
|
let text = ffi::CString::new(text).unwrap();
|
|
|
|
unsafe {
|
|
cairo_set_source_rgb(self.ctx, r, g, b);
|
|
cairo_move_to(self.ctx, x, y);
|
|
cairo_show_text(self.ctx, text.as_ptr());
|
|
}
|
|
}
|
|
|
|
fn image(&mut self, pos: Point, im: &Self::NativeImage)
|
|
{
|
|
let x = f64::from(pos.0);
|
|
let y = f64::from(pos.1);
|
|
|
|
unsafe {
|
|
gdk_cairo_set_source_pixbuf(self.ctx, im.0, x, y);
|
|
cairo_paint(self.ctx);
|
|
}
|
|
}
|
|
}
|
|
|
|
impl MapEditor
|
|
{
|
|
/// Propagates updated information to widgets.
|
|
pub fn cause_update(&mut self)
|
|
{
|
|
unsafe {
|
|
gtk_widget_queue_draw(*self.draw);
|
|
}
|
|
}
|
|
}
|
|
|
|
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}
|
|
}
|
|
|
|
impl<T> std::ops::Deref for Refc<'_, T>
|
|
{
|
|
type Target = *mut T;
|
|
|
|
fn deref(&self) -> &Self::Target {&self.p}
|
|
}
|
|
|
|
impl<T> Drop for Refc<'_, T>
|
|
{
|
|
fn drop(&mut self)
|
|
{
|
|
unsafe {
|
|
g_object_unref(self.p as _);
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T> Refc<'_, T>
|
|
{
|
|
/// Creates a new `Refc` which will own an already referenced object.
|
|
pub const fn new(p: *mut T) -> Self {Self{p, l: PhantomData}}
|
|
|
|
/// Creates a new `Refc` which will hold a reference to the object.
|
|
pub fn own(p: *mut T) -> Self {unsafe {g_object_ref(p as _);} Self::new(p)}
|
|
}
|
|
|
|
/// An image for a `CrDrawArea`.
|
|
pub struct CrImage(pub *const GdkPixbuf);
|
|
|
|
/// A `DrawArea` for a Cairo surface.
|
|
pub struct CrDrawArea
|
|
{
|
|
ctx: *mut cairo_t,
|
|
w: Coord,
|
|
h: Coord,
|
|
}
|
|
|
|
/// Specialized map editor which has callbacks for frontend purposes.
|
|
pub struct MapEditor
|
|
{
|
|
pub edit: editor::MapEditor,
|
|
pub draw: Refc<'static, GtkWidget>,
|
|
}
|
|
|
|
/// A runtime reference to the map editor.
|
|
pub type MapEditorRef = std::cell::RefCell<MapEditor>;
|
|
|
|
/// A GObject owned pointer.
|
|
pub struct Refc<'a, T>
|
|
{
|
|
p: *mut T,
|
|
l: PhantomData<&'a *mut T>,
|
|
}
|
|
|
|
// EOF
|