Maraiah/source/tycho/interfaces.rs

201 lines
4.2 KiB
Rust
Raw Normal View History

2019-03-24 17:04:49 -07:00
//! Implemented interfaces for Rozinante.
2019-03-23 05:33:04 -07:00
use cairo_sys::*;
use gdk_pixbuf_sys::*;
use gdk_sys::*;
2019-03-27 14:02:15 -07:00
use gobject_sys::*;
use gtk_sys::*;
use maraiah::{c_str,
durandal::{ffi, image::*},
rozinante::{draw::*, editor}};
2019-03-02 15:26:55 -08:00
2019-03-24 17:04:49 -07:00
/// Converts a `Color` to a `f64` triple.
2019-03-02 15:26:55 -08:00
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()))
}
2019-03-23 05:33:04 -07:00
impl CacheImage for CrImage
2019-03-02 17:49:35 -08:00
{
2019-03-23 05:33:04 -07:00
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}}
2019-03-02 17:49:35 -08:00
}
2019-03-23 05:33:04 -07:00
impl CrDrawArea
2019-03-02 15:26:55 -08:00
{
2019-03-23 05:33:04 -07:00
pub const fn new(ctx: *mut cairo_t, w: f64, h: f64) -> Self
2019-03-02 15:26:55 -08:00
{
2019-03-23 05:33:04 -07:00
CrDrawArea{ctx, w: w as Coord, h: h as Coord}
2019-03-02 15:26:55 -08:00
}
}
2019-03-23 05:33:04 -07:00
impl DrawArea for CrDrawArea
2019-03-02 15:26:55 -08:00
{
2019-03-23 05:33:04 -07:00
type NativeImage = CrImage;
2019-03-02 17:49:35 -08:00
fn w(&self) -> Coord {self.w}
fn h(&self) -> Coord {self.h}
2019-03-02 15:26:55 -08:00
2019-03-27 04:55:58 -07:00
fn clear(&mut self, cr: impl Color)
2019-03-02 15:26:55 -08:00
{
self.rect(Rect{x: 0, y: 0, w: self.w(), h: self.h()}, cr);
2019-03-23 05:33:04 -07:00
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);
2019-03-24 17:04:49 -07:00
cairo_set_line_width(self.ctx, 1.0);
}
}
2019-03-27 04:55:58 -07:00
fn line_width(&mut self, width: u8)
2019-03-24 17:04:49 -07:00
{
let width = f64::from(width);
unsafe {
cairo_set_line_width(self.ctx, width);
}
}
2019-03-27 04:55:58 -07:00
fn line(&mut self, p1: Point, p2: Point, cr: impl Color)
2019-03-24 17:04:49 -07:00
{
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);
2019-03-23 05:33:04 -07:00
}
2019-03-02 15:26:55 -08:00
}
2019-03-27 04:55:58 -07:00
fn rect(&mut self, rect: Rect, cr: impl Color)
2019-03-02 15:26:55 -08:00
{
2019-03-23 05:33:04 -07:00
let px = f64::from(rect.x);
let py = f64::from(rect.y);
let sx = f64::from(rect.w);
let sy = f64::from(rect.h);
2019-03-02 15:26:55 -08:00
let (r, g, b) = flt_color(cr);
2019-03-23 05:33:04 -07:00
unsafe {
cairo_set_source_rgb(self.ctx, r, g, b);
cairo_rectangle(self.ctx, px, py, sx, sy);
cairo_fill(self.ctx);
}
2019-03-02 15:26:55 -08:00
}
2019-03-27 04:55:58 -07:00
fn text(&mut self, pos: Point, text: &str, cr: impl Color)
2019-03-02 15:26:55 -08:00
{
let (r, g, b) = flt_color(cr);
2019-03-23 05:33:04 -07:00
let x = f64::from(pos.0);
let y = f64::from(pos.1);
2019-03-27 14:02:15 -07:00
let text = ffi::CString::new(text).unwrap();
2019-03-23 05:33:04 -07:00
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());
}
2019-03-02 15:26:55 -08:00
}
2019-03-02 17:49:35 -08:00
2019-03-27 04:55:58 -07:00
fn image(&mut self, pos: Point, im: &Self::NativeImage)
2019-03-02 17:49:35 -08:00
{
2019-03-23 05:33:04 -07:00
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);
}
2019-03-02 17:49:35 -08:00
}
2019-03-02 15:26:55 -08:00
}
2019-03-27 14:02:15 -07:00
impl MapEditor
{
/// Propagates updated information to widgets.
pub fn cause_update(&mut self)
{
2019-04-01 06:05:06 -07:00
unsafe {
gtk_widget_queue_draw(self.draw);
}
2019-03-27 14:02:15 -07:00
}
/// Sets the drawing area widget.
pub fn set_draw(&mut self, draw: *mut GtkWidget)
{
self.draw = draw;
2019-04-01 06:05:06 -07:00
unsafe {
g_object_ref(self.draw as _);
}
2019-03-27 14:02:15 -07:00
}
}
impl Default for MapEditor
{
fn default() -> Self
{
Self{edit: editor::MapEditor::default(),
draw: ffi::null_mut()}
}
}
impl Drop for MapEditor
{
fn drop(&mut self)
{
2019-04-01 06:05:06 -07:00
unsafe {
g_object_unref(self.draw as _);
}
2019-03-27 14:02:15 -07:00
}
}
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}
}
2019-03-24 17:04:49 -07:00
/// An image for a `CrDrawArea`.
2019-03-23 05:33:04 -07:00
pub struct CrImage(pub *const GdkPixbuf);
2019-03-02 21:45:04 -08:00
2019-03-24 17:04:49 -07:00
/// A `DrawArea` for a Cairo surface.
2019-03-23 05:33:04 -07:00
pub struct CrDrawArea
2019-03-02 15:26:55 -08:00
{
2019-03-23 05:33:04 -07:00
ctx: *mut cairo_t,
2019-03-02 17:49:35 -08:00
w: Coord,
h: Coord,
2019-03-02 15:26:55 -08:00
}
2019-03-27 14:02:15 -07:00
/// 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>;
2019-03-02 15:26:55 -08:00
// EOF