/* Copyright (c) 2010, Chris Want Please see BSD style licence at the end of this file */ import java.awt.event.MouseWheelListener; class Scene implements MouseWheelListener { PImage bg=null; // Background color bgcol=color(0, 0, 0); /* View related variables */ float xRot=2.88; // current X rotation float yRot=-0.64; // current Y rotation float currXRot,currYRot; // temp X,Y rotation float clickX,clickY; // mouse click position float currScale = 200.0; float scaleMin = 100.0, scaleMax = 400.0, scaleStep = 1.10; float transX = 0.0, transY = 0.0, transZ = 0.0; float pretransX = 0.0, pretransY = 0.0, pretransZ = 0.0; float fieldScale=1.0; boolean updateRotation=false; boolean updateView=true; boolean zUp=false; Scene() { addMouseWheelListener(this); } Scene(String bg_name) { this(); bg=loadImage("bg.jpg"); } void doRotation () { if (updateRotation) { xRot=min(max(xRot+(clickY-mouseY)*0.0007,2.2),3.5); yRot+=(clickX-mouseX)*0.0007; // force update updateView=true; } currXRot=xRot; currYRot=yRot; } void drawBackground() { drawBackground(g); } void drawBackground(PGraphics pg) { drawBackground(pg, 255); } void drawBackground(PGraphics pg, int a) { if (bg != null) pg.background(bg); //else pg.background(bgcol, 0); else pg.background(bgcol, a); } void transformView() { transformView(g); } void zUpOn() { zUp = true; } void zUpOff() { zUp = false; } void transformView(PGraphics pg) { pg.pushMatrix(); pg.translate(transX, transY, transZ); pg.rotateX(currXRot); pg.rotateY(currYRot); pg.scale(fieldScale * currScale); if (zUp) { pg.rotateX(PI/2.0); pg.scale(1.0, 1.0, -1.0); } pg.translate(pretransX, pretransY, pretransZ); } void restoreView() { restoreView(g); } void restoreView(PGraphics pg) { pg.popMatrix(); } void mouseMoved() { // force redraw // CW --huh ? updateView=true; } void mousePressed() { clickX=(float)mouseX; clickY=(float)mouseY; updateRotation=true; } void mouseReleased() { updateRotation=false; } void setUpdateView(boolean state) { updateView = state; } boolean getUpdateView() { return updateView; } void setView(Field field) { setView(field, g); } void setView(Field field, PGraphics pg) { float xWidth = field.xMax - field.xMin; float yWidth = field.yMax - field.yMin; float zWidth = field.zMax - field.zMin; float maxWidth = max(xWidth, yWidth, zWidth); transX = pg.width/2; transY = pg.height/2; fieldScale = 1.0 / maxWidth; } void setViewCenter(Field field) { setViewCenter(field, g); } void setViewCenter(Field field, PGraphics pg) { float xWidth = field.xMax - field.xMin; float yWidth = field.yMax - field.yMin; float zWidth = field.zMax - field.zMin; float maxWidth = max(xWidth, yWidth, zWidth); pretransX = -(field.xMax + field.xMin)/2; pretransY = -(field.yMax + field.yMin)/2; pretransZ = -(field.zMax + field.zMin)/2; transX = pg.width/2; transY = pg.height/2; fieldScale = 1.0 / maxWidth; } float getScale() { return currScale; } void stepZoomIn() { currScale = min(currScale * scaleStep, scaleMax); updateView = true; } void stepZoomOut() { currScale = max(currScale / scaleStep, scaleMin); updateView = true; } void mouseWheelMoved(java.awt.event.MouseWheelEvent e) { int notches = e.getWheelRotation(); if (notches < 0) { this.stepZoomIn(); } else { this.stepZoomOut(); } } } /* Copyright (c) 2010, Chris Want, Research Support Group, AICT, University of Alberta. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Contributors: Chris Want (University of Alberta) */