1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- use gdk;
- use gtk::{
- self,
- WidgetExt
- };
- use std::cell::RefCell;
- use std::rc::Rc;
- pub struct HexGridCanvas {
- pub canvas: gtk::DrawingArea,
- pub mouse_loc: Rc<RefCell<Option<(i32, i32)>>>,
- }
- impl HexGridCanvas {
- pub fn new() -> HexGridCanvas {
- let canvas = gtk::DrawingArea::new();
- let mouse_loc = Rc::new(RefCell::new(None));
- let reader_mouse = mouse_loc.clone();
- let writer_mouse = mouse_loc.clone();
- canvas.connect_draw(move |cv, ctx| {
- let w = cv.get_allocated_width();
- let h = cv.get_allocated_height();
- ctx.set_source_rgb(1.0, 1.0, 1.0);
- ctx.rectangle(0.0, 0.0, w as f64, h as f64);
- ctx.fill();
- ctx.set_source_rgb(0.9, 0.9, 0.9);
- reader_mouse.borrow().map(|(x, y)| {
- ctx.rectangle(x as f64 * 32.0, y as f64 * 32.0, 32.0, 32.0);
- ctx.fill();
- });
- ctx.set_source_rgb(0.8, 0.8, 0.8);
- for x in 0..((w / 32) + 1) {
- ctx.move_to(x as f64 * 32.0, 0.0);
- ctx.line_to(x as f64 * 32.0, h as f64);
- ctx.stroke();
- }
- for y in 0..((h / 32) + 1) {
- ctx.move_to(0.0, y as f64 * 32.0);
- ctx.line_to(w as f64, y as f64 * 32.0);
- ctx.stroke();
- }
- gtk::Inhibit(false)
- });
- canvas.connect_motion_notify_event(move |cv, motion| {
- let (x, y) = motion.get_position();
- *writer_mouse.borrow_mut() = Some((x as i32 / 32, y as i32 / 32));
- cv.queue_draw();
- gtk::Inhibit(false)
- });
- canvas.add_events(gdk::POINTER_MOTION_MASK.bits() as i32);
- HexGridCanvas {
- canvas,
- mouse_loc,
- }
- }
- }
|