Browse Source

scrolling etc

Johann Woelper 6 năm trước cách đây
mục cha
commit
5a2a043cba
1 tập tin đã thay đổi với 50 bổ sung44 xóa
  1. 50 44
      src/main.rs

+ 50 - 44
src/main.rs

@@ -1,5 +1,9 @@
-extern crate piston_window;
 
+#![windows_subsystem = "windows"]
+
+
+
+extern crate piston_window;
 use clap;
 use clap::{App, Arg, SubCommand};
 use piston_window::*;
@@ -19,7 +23,7 @@ fn main() {
     let img = matches.value_of("INPUT").unwrap();
 
     let opengl = OpenGL::V3_2;
-    let mut window: PistonWindow = WindowSettings::new("Oculante", [300, 300])
+    let mut window: PistonWindow = WindowSettings::new("Oculante", [800, 800])
         .exit_on_esc(true)
         .graphics_api(opengl)
         .build()
@@ -35,68 +39,70 @@ fn main() {
     ) {
         Ok(texture) => {
             window.set_lazy(true);
-
-            let mut events = Events::new(EventSettings::new().lazy(true));
             let mut offset = (100.0,0.0);
+            let mut scale = 1.0;
+            let mut drag = false;
+            let mut events = Events::new(EventSettings::new().lazy(true));
+
+  
+
             while let Some(e) = events.next(&mut window) {
-                if let Some(Button::Mouse(button)) = e.press_args() {
-                    println!("Pressed mouse button '{:?}'", button);
+                
+
+                if let Some(Button::Mouse(_)) = e.press_args() {
+                    drag = true;
                 }
-   
+
+                if let Some(Button::Mouse(_)) = e.release_args() {
+                    drag = false;
+                };
 
                 e.mouse_scroll(|d| {
+                    println!("Scrolled mouse '{}, {}'", d[0], d[1]);
+                    if d[1] > 0.0 {
+                        scale += 0.1;
+                    } else {
+                        scale -= 0.1;
+                    }
                 });
-                e.mouse_cursor(|d| {
-                    println!("Relative mouse moved '{} {}'", d[0], d[1]);
-                    // offset.0 = d[0];
-                    // offset.1 = d[1];
-                    // println!("Scrolled mouse '{}, {}'", d[0], d[1]);
-                    println!("offset {:?}", offset);
+                e.mouse_relative(|d| {
+
+                    if drag {
+                        offset.0 += d[0];
+                        offset.1 += d[1];
+                    }
                     
                 });
                 e.resize(|args| {
                     println!("Resized '{}, {}'", args.window_size[0], args.window_size[1])
                 });
     
+
+
+                    
                 if let Some(args) = e.render_args() {
-                    window.draw_2d(&e, |c, gfx, _| {
-                        clear([0.3; 4], gfx);
+                    window.draw_2d(&e, |c, gfx, _device| {
+                        clear([0.2; 4], gfx);
                         // c.trans(111.0,1.0);
-                        let transform = c.transform.trans(offset.0, offset.1);
-
+                        let transform = c.transform.trans(offset.0, offset.1).scale(scale, scale);
                         image(&texture, transform, gfx);
                     });
-                }
-            }
-
-            // while let Some(e) = window.next() {
-            //     let mut offset = (0.0, 0.0);
-
-            //     e.mouse_cursor(|d| {
-            //             offset.0 = d[0];
-            //             offset.1 = d[1];
-            //             // image(&texture, transform, gfx);
-            //             // println!("Relative mouse moved '{} {}'", d[0], d[1])
+                } 
+       
 
-            //             window.draw_2d(&e, |c, gfx, _| {
-            //                 clear([0.3; 4], gfx);
-            //                 // c.trans(111.0,1.0);
-            //                 let transform = c.transform.trans(offset.0, offset.1);
 
-            //                 image(&texture, transform, gfx);
+                // if let Some(e) = window.next() {
+                //     window.draw_2d(&e, |c, gfx, _| {
+                //         clear([0.2; 4], gfx);
+                //         // image(&texture, c.transform, g);
+                //         let transform = c.transform.trans(offset.0, offset.1).scale(scale, scale);
+                //         image(&texture, transform, gfx);
+                //     });
+                // }
 
-            //             });
-            //         }
-            //     );
-
-            //     // offset = (100.0, 0.0);
-
-            //     println!("Offset '{} {}'", offset.0, offset.1);
+            }
 
-            //     if let Some(Button::Mouse(button)) = e.press_args() {
-            //         println!("Pressed mouse button '{:?}'", button);
-            //     }
-            // }
+ 
         }
         Err(e) => println!("Could not create texture. {}", e),
     }