Munashedov

Untitled

Mar 11th, 2020
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.01 KB | None | 0 0
  1. PVector screenMouse = new PVector(0,0);
  2. PVector worldMouse = new PVector(0,0);
  3. PVector offset = new PVector(0,0);
  4.  
  5. void setup(){
  6.   size(800, 800);
  7.   //offset.x = -width/2;
  8.   //offset.y = -height/2;
  9. }
  10.  
  11. void draw(){
  12.   frameRate(144);
  13.   background(50);
  14.  
  15.   pushMatrix();
  16.   updateZoom();
  17.   worldRender();
  18.   popMatrix();
  19.  
  20.   screenRender(); // draw stuff that shouldn't move relative to the screenspace
  21. }
  22.  
  23. void worldRender() {
  24.   drawGrid(); // draw a grid
  25.   worldCircle(); // draw a circle at mouse's world coords
  26.   offsetLines();
  27. }
  28.  
  29. void screenRender() {
  30.   screenCircle(); // draw red marker at mouse's screenspace coords
  31.   drawDebug(); // draw a bunch of debug text info
  32. }
  33.  
  34. //=================================================================
  35. // FUNCTIONS
  36.  
  37. PVector world = new PVector(0,0);
  38. PVector STW(PVector screen) {
  39.   world.x = (screen.x / zoomScalar) + offset.x;
  40.   world.y = (screen.y / zoomScalar) + offset.y;
  41.   return world;
  42. }
  43.  
  44. PVector screen = new PVector(0,0);
  45. PVector WTS(PVector world) {
  46.   screen.x = int((world.x - offset.x) * zoomScalar);
  47.   screen.y = int((world.y - offset.y) * zoomScalar);
  48.   return screen;
  49. }
  50.  
  51. PVector worldMousePreZoom, worldMousePostZoom = new PVector(0,0);
  52. void updateZoom() {
  53.   screenMouse.x = mouseX;
  54.   screenMouse.y = mouseY;
  55.   worldMouse = STW(screenMouse);
  56.   worldMousePreZoom = STW(screenMouse);
  57.   scale(zoomScalar);
  58.   worldMousePostZoom = STW(screenMouse);
  59.   offset.x += (worldMousePreZoom.x - worldMousePostZoom.x);
  60.   offset.y += (worldMousePreZoom.y - worldMousePostZoom.y);
  61.   translate(-offset.x, -offset.y);
  62. }
  63.  
  64. //=================================================================
  65. // CONTROLS
  66.  
  67. float zoomRate = 1.1; //
  68. float zoomScalar = 1;
  69. void mouseWheel(MouseEvent event) {
  70.   if (event.getCount() > 0) {
  71.     zoomScalar /= zoomRate;
  72.   }
  73.   if (event.getCount() < 0) {
  74.     zoomScalar *= zoomRate;
  75.   }
  76. }
  77.  
  78. PVector startPan = new PVector(0,0);
  79. void mousePressed() {
  80.   if (mouseButton == LEFT) {
  81.     startPan.x = mouseX;
  82.     startPan.y = mouseY;
  83.   }
  84. }
  85.  
  86. void mouseDragged() {
  87.   if (mouseButton == LEFT) {
  88.     offset.x -= (mouseX - startPan.x) / zoomScalar;
  89.     offset.y -= (mouseY - startPan.y) / zoomScalar;
  90.     startPan.x = mouseX;
  91.     startPan.y = mouseY;
  92.   }
  93. }
  94.  
  95. //==================================================================
  96. // DEBUG STUFF
  97.  
  98. int markerSize = 10;
  99. void worldCircle() {
  100.   fill(GREEN);
  101.   ellipse(worldMouse.x, worldMouse.y, markerSize*2, markerSize*2);
  102. }
  103. void screenCircle() {
  104.   fill(RED);
  105.   ellipse(mouseX, mouseY, markerSize, markerSize);
  106. }
  107.  
  108. void offsetLines() {
  109.   textAlign(CENTER);
  110.   stroke(YELLOW);
  111.   fill(YELLOW);
  112.   line(0, 0, offset.x, 0);
  113.   text("offset.x: " + offset.x, offset.x/2, 20);
  114.   line(0, 0, 0, offset.y);
  115.   text("offset.y: " + offset.y, 60, offset.y/2);
  116.   stroke(RED);
  117.   fill(RED);
  118.   line(0, 0, worldMouse.x, 0);
  119.   text("worldMouse.x: " + worldMouse.x, worldMouse.x/2, 20);
  120.   line(worldMouse.x, 0, worldMouse.x, worldMouse.y);
  121.   text("worldMouse.y: " + worldMouse.y, worldMouse.x+90, worldMouse.y/2);
  122. }
  123.  
  124. void drawGrid() {
  125.   for (int i = 0; i < 8; i++) {
  126.     for (int j = 0; j < 8; j++) {
  127.       fill(40);
  128.       stroke(GREEN);
  129.       rect(800/10*i, 800/10*j, 80, 80);
  130.       fill(50);
  131.     }
  132.   }
  133. }
  134.  
  135. void drawDebug() {
  136.   fill(YELLOW);
  137.   textAlign(LEFT);
  138.   text("screenMouseX: " + int(screenMouse.x), 10, 20);
  139.   text("screenMouseY: " + int(screenMouse.y), 10, 30);
  140.   text("worldMouseX: " + worldMouse.x, 10, 40);
  141.   text("worldMouseY: " + worldMouse.y, 10, 50);
  142.   text("worldMousePreZoom.x: " + int(worldMousePreZoom.x), 10, 60);
  143.   text("worldMousePreZoom.y: " + int(worldMousePreZoom.y), 10, 70);
  144.   text("worldMousePostZoom.x: " + int(worldMousePreZoom.x), 10, 80);
  145.   text("worldMousePostZoom.y: " + int(worldMousePreZoom.y), 10, 90);
  146. }
  147.  
  148. //==================================================================
  149. // COLORS
  150.  
  151. color WHITE = #ffffff;
  152. color YELLOW = #fff700;
  153. color BLACK = #0a0a0a;
  154. color RED = #ff0000;
  155. color GREEN = #0a9600;
  156. color BLUE = #3256a8;
  157. color GREY = #dcdcdc;
Add Comment
Please, Sign In to add comment