Starting to add a tile-picker widget, too
Getty Ritter
6 years ago
| 1 |
use gdk;
|
1 | 2 |
use gtk::{
|
2 | 3 |
self,
|
3 | 4 |
BoxExt,
|
|
15 | 16 |
|
16 | 17 |
mod canvas;
|
17 | 18 |
use self::canvas::GridCanvas;
|
| 19 |
|
| 20 |
mod picker;
|
| 21 |
use self::picker::Picker;
|
18 | 22 |
|
19 | 23 |
pub struct App {
|
20 | 24 |
pub window: gtk::Window,
|
|
139 | 143 |
tile_width: gtk::Entry,
|
140 | 144 |
tile_height: gtk::Entry,
|
141 | 145 |
load_tileset_btn: gtk::Button,
|
| 146 |
picker: Picker,
|
142 | 147 |
}
|
143 | 148 |
|
144 | 149 |
impl Toolbar {
|
|
154 | 159 |
|
155 | 160 |
let load_tileset_btn = gtk::Button::new_with_label("Load Tileset Image");
|
156 | 161 |
|
| 162 |
let picker = Picker::new();
|
| 163 |
|
157 | 164 |
container.pack_start(&tile_label, false, true, 0);
|
158 | 165 |
container.pack_start(&tile_width, false, true, 0);
|
159 | 166 |
container.pack_start(&tile_height, false, true, 0);
|
160 | 167 |
container.pack_start(&load_tileset_btn, false, true, 0);
|
| 168 |
container.pack_start(&picker.canvas, false, true, 0);
|
161 | 169 |
|
162 | 170 |
Toolbar {
|
163 | 171 |
toolbar: container,
|
164 | 172 |
tile_width,
|
165 | 173 |
tile_height,
|
166 | 174 |
load_tileset_btn,
|
| 175 |
picker,
|
167 | 176 |
}
|
168 | 177 |
}
|
169 | 178 |
}
|
| 1 |
use gdk;
|
| 2 |
use gtk::{
|
| 3 |
self,
|
| 4 |
WidgetExt
|
| 5 |
};
|
| 6 |
use std::cell::RefCell;
|
| 7 |
use std::rc::Rc;
|
| 8 |
|
| 9 |
pub struct Picker {
|
| 10 |
pub canvas: gtk::DrawingArea,
|
| 11 |
pub mouse_loc: Rc<RefCell<Option<(i32, i32)>>>,
|
| 12 |
}
|
| 13 |
|
| 14 |
impl Picker {
|
| 15 |
pub fn new() -> Picker {
|
| 16 |
let canvas = gtk::DrawingArea::new();
|
| 17 |
let mouse_loc = Rc::new(RefCell::new(Some((2, 2))));
|
| 18 |
let reader_mouse = mouse_loc.clone();
|
| 19 |
let writer_mouse = mouse_loc.clone();
|
| 20 |
|
| 21 |
canvas.connect_draw(move |cv, ctx| {
|
| 22 |
let w = cv.get_allocated_width();
|
| 23 |
let h = cv.get_allocated_height();
|
| 24 |
ctx.set_source_rgb(1.0, 1.0, 1.0);
|
| 25 |
ctx.rectangle(0.0, 0.0, w as f64, h as f64);
|
| 26 |
ctx.fill();
|
| 27 |
|
| 28 |
reader_mouse.borrow().map(|(x, y)| {
|
| 29 |
ctx.set_source_rgb(0.9, 0.9, 0.9);
|
| 30 |
ctx.rectangle(
|
| 31 |
x as f64 * 32.0,
|
| 32 |
y as f64 * 32.0,
|
| 33 |
32.0,
|
| 34 |
32.0,
|
| 35 |
);
|
| 36 |
ctx.fill();
|
| 37 |
});
|
| 38 |
gtk::Inhibit(false)
|
| 39 |
});
|
| 40 |
|
| 41 |
canvas.connect_motion_notify_event(move |cv, motion| {
|
| 42 |
let (x, y) = motion.get_position();
|
| 43 |
*writer_mouse.borrow_mut() =
|
| 44 |
Some((x as i32 / 32, y as i32 / 32));
|
| 45 |
cv.queue_draw();
|
| 46 |
gtk::Inhibit(false)
|
| 47 |
});
|
| 48 |
|
| 49 |
canvas.add_events(gdk::POINTER_MOTION_MASK.bits() as i32);
|
| 50 |
|
| 51 |
Picker {
|
| 52 |
canvas,
|
| 53 |
mouse_loc,
|
| 54 |
}
|
| 55 |
}
|
| 56 |
}
|