📄 transition3d.java.svn-base
字号:
/* * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */package com.sun.lwuit.animations;import com.sun.lwuit.Component;import com.sun.lwuit.Dialog;import com.sun.lwuit.Display;import com.sun.lwuit.Graphics;import com.sun.lwuit.Image;import com.sun.lwuit.M3G;import javax.microedition.m3g.AnimationController;import javax.microedition.m3g.AnimationTrack;import javax.microedition.m3g.Appearance;import javax.microedition.m3g.Background;import javax.microedition.m3g.Camera;import javax.microedition.m3g.CompositingMode;import javax.microedition.m3g.Graphics3D;import javax.microedition.m3g.Group;import javax.microedition.m3g.Image2D;import javax.microedition.m3g.IndexBuffer;import javax.microedition.m3g.KeyframeSequence;import javax.microedition.m3g.Mesh;import javax.microedition.m3g.Node;import javax.microedition.m3g.PolygonMode;import javax.microedition.m3g.Texture2D;import javax.microedition.m3g.Transform;import javax.microedition.m3g.TriangleStripArray;import javax.microedition.m3g.VertexArray;import javax.microedition.m3g.VertexBuffer;import javax.microedition.m3g.World;/** * Transitions utilizing the M3G API for 3D effects, this transition requires * M3G (JSR 184) support on the device in order to work properly. Currently * none of these transitions work with dialogs or any component type that * is not a form * * @author Shai Almog */public final class Transition3D extends Transition implements M3G.Callback { private static final int FLY_IN = 2; private static final int ROTATION = 1; private static final int CUBE_ROTATION = 3; private static final int STATIC_ROTATION = 4; private static final int SWING_IN = 5; private int transitionType; private World world; //private int rotation; private long startTime; private int duration = 1000; private static int maxTextureResolution = -1; private boolean rotateRight; private boolean highQualityMode; private boolean firstFinished; private int clipX, clipY, clipW, clipH; private boolean firstTime = true; private Transition3D(int transitionType) { this.transitionType = transitionType; } /** * @inheritDoc */ public void cleanup() { super.cleanup(); world = null; try { Graphics3D.getInstance().setCamera(null, null); Graphics3D.getInstance().resetLights(); } catch(Throwable err) { } System.gc(); firstTime = true; } private static void initMaxTexture() { maxTextureResolution = M3G.getInstance().getMaxTextureDimension(); // some low resources devices support very high texture values creating out of // memory errors, this limits the texture resolution on such low end devices if(Display.getInstance().isLightMode()) { if(maxTextureResolution > 128) { maxTextureResolution = 128; } else { maxTextureResolution = 64; } } } /** * Allows performance/memory sensitive devices to define a maximum size for the * texture thus increasing performance on the expence of quality. * @param size */ public static void setMaxTextureDimension(int size) { maxTextureResolution = size; } /** * Creates a rotation transition from the current form to the next form * * @param duration duration in milliseconds of the transition * @param rotateRight indicates rotating towards the right side or the left side true == right * @return newly created transition object */ public static Transition3D createRotation(int duration, boolean rotateRight) { initMaxTexture(); Transition3D t3d = new Transition3D(ROTATION); t3d.duration = duration; t3d.rotateRight = rotateRight; return t3d; } /** * Creates a rotation transition from the current form to the next dialog, in this rotation only * the dialog will rotate and the form will remain static * * @param duration duration in milliseconds of the transition * @param rotateRight indicates rotating towards the right side or the left side true == right * @return newly created transition object */ public static Transition3D createStaticRotation(int duration, boolean rotateRight) { initMaxTexture(); Transition3D t3d = new Transition3D(STATIC_ROTATION); t3d.duration = duration; t3d.rotateRight = rotateRight; return t3d; } /** * Creates a rotation transition from the top to the bottom giving a feeling of Swinging into place * * @param duration duration in milliseconds of the transition */ public static Transition3D createSwingIn(int duration) { return createSwingIn(duration, true); } /** * Creates a rotation transition from the top to the bottom giving a feeling of Swinging into place * * @param duration duration in milliseconds of the transition * @param topDown indicates rotating downwards or upwards */ public static Transition3D createSwingIn(int duration, boolean topDown) { initMaxTexture(); Transition3D t3d = new Transition3D(SWING_IN); t3d.duration = duration; t3d.rotateRight = topDown; return t3d; } /** * Creates a cube rotation transition from the current form to the next form * * @param duration duration in milliseconds of the transition * @param rotateRight indicates rotating towards the right side or the left side true == right * @return newly created transition object */ public static Transition3D createCube(int duration, boolean rotateRight) { initMaxTexture(); Transition3D t3d = new Transition3D(CUBE_ROTATION); t3d.duration = duration; t3d.rotateRight = rotateRight; return t3d; } /** * Creates a fly in transition object. * * @param duration duration in milliseconds of the transition * @return newly created transition object */ public static Transition3D createFlyIn(int duration) { initMaxTexture(); Transition3D t3d = new Transition3D(FLY_IN); t3d.duration = duration; return t3d; } /** * @inheritDoc */ public Transition copy() { Transition3D t3d = new Transition3D(transitionType); t3d.duration = duration; t3d.rotateRight = rotateRight; return t3d; } /** * @inheritDoc */ public boolean animate() { if(world == null) { return false; } long time = System.currentTimeMillis(); int current = (int)(time - startTime); world.animate(current); // after the motion finished we need to paint one last time otherwise // there will be a "bump" in sliding if(firstFinished) { return false; } boolean finished = current >= duration; if(finished) { if(!firstFinished) { firstFinished = true; } } return true; } /** * @inheritDoc */ public void paint(Graphics g) { // prevents painting when the components are too small if(world == null) { return; } // paint the finished component if the animation completed if(firstFinished) { getDestination().paintComponent(g); return; } Component c = getSource(); int titleHeight = 0; if(c instanceof Dialog) { Dialog dlg = (Dialog)c; c = dlg.getContentPane(); titleHeight = dlg.getTitleComponent().getHeight(); } else { if(getDestination() instanceof Dialog) { Dialog dlg = (Dialog)getDestination(); c = dlg.getContentPane(); titleHeight = dlg.getTitleComponent().getHeight(); } else { firstTime = false; } } if(firstTime) { firstTime = false; // paint the whole source form once to make sure we have the proper // tinting behavior if(getDestination() instanceof Dialog) { getSource().paintComponent(g); } else { getDestination().paintComponent(g); } } clipY = c.getAbsoluteY() - titleHeight; clipX = c.getAbsoluteX(); clipW = c.getWidth(); clipH = c.getHeight() + titleHeight; g.setClip(clipX, clipY, clipW, clipH); M3G.getInstance().renderM3G(g, false, Graphics3D.ANTIALIAS, this); } /** * @inheritDoc */ public void initTransition() { Component source = getSource(); Component dest = getDestination(); // prevent painting when the components are too small if(source == null || dest == null || source.getWidth() < 4 || source.getHeight() < 4 || dest.getWidth() < 4 || dest.getHeight() < 4) { return; } firstFinished = false; world = new World(); Camera camera = new Camera(); camera.setPerspective(30.0f, 1, 1.0f, 45.0f); Transform cameraTransform = new Transform(); Graphics3D g3d = Graphics3D.getInstance(); g3d.setCamera(camera, cameraTransform); // the whole world is within a single group in the scene graph tree Group group = new Group(); world.addChild(group); world.setActiveCamera(camera); group.addChild(camera); Background background3D = new Background(); background3D.setColorClearEnable(true); background3D.setDepthClearEnable(true); background3D.setColor(0xff000000 | source.getStyle().getBgColor()); world.setBackground(background3D); switch(transitionType) { case FLY_IN: { Mesh destMesh = createMesh(dest); group.addChild(destMesh); createFlyIn(destMesh, background3D); break; } case CUBE_ROTATION: { Mesh destMesh = createMesh(dest); createCubeRotation(group, destMesh); break; } case ROTATION: { Mesh destMesh = createMesh(dest); group.addChild(destMesh); createRotation(group, destMesh); break; } case SWING_IN: case STATIC_ROTATION: { Mesh destMesh; if(source instanceof Dialog) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -