gdritter repos palladio / cbdb2e0
Starting to add a tile-picker widget, too Getty Ritter 6 years ago
2 changed file(s) with 65 addition(s) and 0 deletion(s). Collapse all Expand all
1 use gdk;
12 use gtk::{
23 self,
34 BoxExt,
1516
1617 mod canvas;
1718 use self::canvas::GridCanvas;
19
20 mod picker;
21 use self::picker::Picker;
1822
1923 pub struct App {
2024 pub window: gtk::Window,
139143 tile_width: gtk::Entry,
140144 tile_height: gtk::Entry,
141145 load_tileset_btn: gtk::Button,
146 picker: Picker,
142147 }
143148
144149 impl Toolbar {
154159
155160 let load_tileset_btn = gtk::Button::new_with_label("Load Tileset Image");
156161
162 let picker = Picker::new();
163
157164 container.pack_start(&tile_label, false, true, 0);
158165 container.pack_start(&tile_width, false, true, 0);
159166 container.pack_start(&tile_height, false, true, 0);
160167 container.pack_start(&load_tileset_btn, false, true, 0);
168 container.pack_start(&picker.canvas, false, true, 0);
161169
162170 Toolbar {
163171 toolbar: container,
164172 tile_width,
165173 tile_height,
166174 load_tileset_btn,
175 picker,
167176 }
168177 }
169178 }
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 }