Merge JigsawX
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
/*
|
||||
* Copyright (c) 2013 Justinfront Ltd
|
||||
* author: Justin L Mills
|
||||
* email: JLM at Justinfront dot net
|
||||
* created: 8 August 2013
|
||||
* language: Haxe 3
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * 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.
|
||||
* * Neither the name of the Justinfront Ltd nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Justinfront Ltd ''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 Justinfront Ltd 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.
|
||||
*/
|
||||
|
||||
|
||||
package jigsawxtargets.hxjava;
|
||||
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.geom.GeneralPath;
|
||||
import java.awt.TexturePaint;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.Image;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.image.RescaleOp;
|
||||
|
||||
import java.NativeArray;
|
||||
import java.javax.imageio.ImageIO;
|
||||
|
||||
import jigsawx.JigsawPiece;
|
||||
import jigsawx.math.Vec2;
|
||||
import jigsawxtargets.hxjava.JigsawxJava;
|
||||
|
||||
#if applet
|
||||
import java.javax.swing.JApplet;
|
||||
import java.net.URL;
|
||||
|
||||
#else
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
#end
|
||||
|
||||
class GraphicsTexture
|
||||
{
|
||||
|
||||
public var textureRectangle: Rectangle;
|
||||
public var generalPath: GeneralPath;
|
||||
var x: Float = 0;
|
||||
var y: Float = 0;
|
||||
var sx: Float;
|
||||
var sy: Float;
|
||||
public var jig: JigsawPiece;
|
||||
public var image: BufferedImage;
|
||||
|
||||
@:isVar var texturePaint( get, null ): TexturePaint;
|
||||
|
||||
public function new( jig_: JigsawPiece
|
||||
, imageSrc: String
|
||||
, textureRectangle_ : Rectangle
|
||||
, sx_: Float
|
||||
, sy_: Float
|
||||
)
|
||||
{
|
||||
|
||||
|
||||
jig = jig_;
|
||||
createPath( jig_ );
|
||||
sx = sx_;
|
||||
sy = sy_;
|
||||
alpha = 1.;
|
||||
image = GraphicsTexture.bufferedImage( imageSrc );
|
||||
textureRectangle = textureRectangle_;
|
||||
|
||||
}
|
||||
|
||||
@:isVar public var alpha( get, set ): Float;
|
||||
public function get_alpha( ) : Float
|
||||
{
|
||||
return alpha;
|
||||
}
|
||||
public function set_alpha( alpha_: Float ): Float
|
||||
{
|
||||
alpha = alpha_;
|
||||
return alpha;
|
||||
}
|
||||
|
||||
|
||||
private function createPath( jig: JigsawPiece )
|
||||
{
|
||||
generalPath = new GeneralPath();
|
||||
var first = jig.getFirst();
|
||||
generalPath.moveTo( first.x, first.y );
|
||||
for( v in jig.getPoints() ) generalPath.lineTo( v.x, v.y );
|
||||
generalPath.closePath();
|
||||
}
|
||||
|
||||
public function get_texturePaint( ): TexturePaint
|
||||
{
|
||||
return texturePaint;
|
||||
}
|
||||
|
||||
public function getLocation(): Vec2
|
||||
{
|
||||
return new Vec2( x, y );
|
||||
}
|
||||
|
||||
public function setLocation( x_: Float, y_: Float )
|
||||
{
|
||||
generalPath.transform( AffineTransform.getTranslateInstance( x_ - x, y_ - y ) );
|
||||
textureRectangle = new Rectangle( Std.int( x_ - sx + 25 )
|
||||
, Std.int( y_ - sy + 25)
|
||||
, Std.int( 360 * 3/2 )
|
||||
, Std.int( 240 * 3/2 ) );
|
||||
texturePaint = new TexturePaint( cast image, textureRectangle );
|
||||
x = x_;
|
||||
y = y_;
|
||||
}
|
||||
|
||||
public function render( g2D: Graphics2D )
|
||||
{
|
||||
|
||||
g2D.setClip( generalPath );
|
||||
|
||||
var arr = [ 1., 1., 1., alpha ];
|
||||
var scaleFactors = new NativeArray<Single>( 4 );
|
||||
for ( i in 0...arr.length ) scaleFactors[ i ] = arr[ i ];
|
||||
|
||||
var offsets = new NativeArray<Single>( 4 );
|
||||
for ( i in 0...arr.length ) offsets[ i ] = arr[ i ];
|
||||
arr = [ 0., 0., 0., 0. ];
|
||||
|
||||
var rescaleOp = new RescaleOp( scaleFactors, offsets, null );
|
||||
var alphaImage = rescaleOp.filter( image, null );
|
||||
g2D.drawImage( alphaImage
|
||||
, textureRectangle.x
|
||||
, textureRectangle.y
|
||||
, textureRectangle.width
|
||||
, textureRectangle.height
|
||||
, null );
|
||||
|
||||
}
|
||||
|
||||
public static function bufferedImage( imageSrc: String ): BufferedImage
|
||||
{
|
||||
try{
|
||||
var alphaImage = new BufferedImage( 100, 50
|
||||
, BufferedImage.TYPE_INT_ARGB );
|
||||
#if applet
|
||||
// Issue with permissions
|
||||
#else
|
||||
var file = new File( imageSrc );
|
||||
var loadedImg = ImageIO.read( file );
|
||||
alphaImage = new BufferedImage(
|
||||
loadedImg.getWidth( null )
|
||||
, loadedImg.getHeight( null )
|
||||
, BufferedImage.TYPE_INT_ARGB);
|
||||
alphaImage.getGraphics().drawImage( loadedImg, 0, 0, null );
|
||||
#end
|
||||
return alphaImage;
|
||||
}
|
||||
catch
|
||||
#if applet
|
||||
( e: Dynamic )
|
||||
#else
|
||||
( e: IOException )
|
||||
#end
|
||||
{
|
||||
#if applet
|
||||
e.printStackTrace();
|
||||
#end
|
||||
trace('image load fail');
|
||||
}
|
||||
return new BufferedImage( 100, 50, BufferedImage.TYPE_INT_ARGB );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,376 @@
|
||||
/*
|
||||
* Copyright (c) 2013 Justinfront Ltd
|
||||
* author: Justin L Mills
|
||||
* email: JLM at Justinfront dot net
|
||||
* created: 8 August 2013
|
||||
* language: Haxe 3
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * 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.
|
||||
* * Neither the name of the Justinfront Ltd nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Justinfront Ltd ''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 Justinfront Ltd 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.
|
||||
*/
|
||||
|
||||
package jigsawxtargets.hxjava;
|
||||
|
||||
import jigsawx.Jigsawx;
|
||||
import java.javax.swing.JPanel;
|
||||
import java.awt.Color;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.Cursor;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.awt.event.MouseMotionListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.Image;
|
||||
import java.awt.Point;
|
||||
import java.javax.swing.JSlider;
|
||||
import java.StdTypes;// Int64
|
||||
|
||||
#if applet
|
||||
import javax.swing.JApplet;
|
||||
#elseif update_task
|
||||
import java.lang.System;
|
||||
import java.javax.swing.JFrame;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import jigsawxtargets.hxjava.UpdateTask;
|
||||
#else
|
||||
import java.lang.System;
|
||||
import java.javax.swing.JFrame;
|
||||
#end
|
||||
|
||||
class JigsawxJava
|
||||
#if applet
|
||||
extends JApplet
|
||||
#else
|
||||
extends JFrame
|
||||
#end
|
||||
implements KeyListener
|
||||
implements MouseListener
|
||||
implements MouseMotionListener
|
||||
{
|
||||
|
||||
var wid: Int;
|
||||
var hi: Int;
|
||||
var rows: Int;
|
||||
var cols: Int;
|
||||
var jigsawx: Jigsawx;
|
||||
var surface: Surface;
|
||||
var currentGraphic: GraphicsTexture;
|
||||
var pressPoint: Point;
|
||||
var enabledMove: Bool;
|
||||
var up = KeyEvent.VK_UP;
|
||||
var down = KeyEvent.VK_DOWN;
|
||||
var right = KeyEvent.VK_RIGHT;
|
||||
var left = KeyEvent.VK_LEFT;
|
||||
#if applet
|
||||
public static var japplet: JigsawxJava;
|
||||
#elseif update_task
|
||||
var mutex: Dynamic;
|
||||
var updateTask: UpdateTask;
|
||||
var updateTimer: Timer;
|
||||
static var slowUpdateSpeed: Int64;
|
||||
#end
|
||||
|
||||
inline static var imageSrc: String = "tablecloth.jpg";
|
||||
|
||||
function setup()
|
||||
{
|
||||
setSize( 700, 500 );
|
||||
|
||||
#if applet
|
||||
japplet = this;
|
||||
#else
|
||||
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
|
||||
setBackground( Color.black );
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
public static function main() { new JigsawxJava(); } public function new()
|
||||
{
|
||||
#if applet
|
||||
super();
|
||||
#else
|
||||
super( 'Jigsawx Java Example' );
|
||||
System.setProperty( "sun.java2d.opengl", "True" );
|
||||
#end
|
||||
|
||||
setup();
|
||||
createSurface();
|
||||
|
||||
setVisible( true );
|
||||
}
|
||||
|
||||
function createSurface()
|
||||
{
|
||||
#if update_task
|
||||
var mutex = {};
|
||||
#end
|
||||
|
||||
surface = new Surface(
|
||||
|
||||
);
|
||||
#if applet
|
||||
#elseif update_task
|
||||
surface.mutex = mutex;
|
||||
#end
|
||||
surface.graphicsTextures = createGraphicsTextures();
|
||||
getContentPane().add( surface );
|
||||
|
||||
|
||||
surface.addKeyListener( this );
|
||||
surface.setFocusable( true );
|
||||
surface.requestFocusInWindow();
|
||||
addMouseListener( this );
|
||||
addMouseMotionListener( this );
|
||||
|
||||
#if update_task
|
||||
updateTimer = new java.util.Timer();
|
||||
updateTask = new UpdateTask();
|
||||
updateTask.newMore( surface, mutex );
|
||||
// Re-retrieve this value before directly starting the timer for max
|
||||
// accuracy.
|
||||
updateTask.oldTime = System.nanoTime();
|
||||
// Start the timer in 0ms (right away), updating every
|
||||
// "slowUpdateSpeed" milliseconds
|
||||
slowUpdateSpeed = 20;
|
||||
var startAt: Int64 = 0;
|
||||
updateTimer.schedule( updateTask
|
||||
, startAt
|
||||
, slowUpdateSpeed
|
||||
);
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
public function mousePressed( e: MouseEvent )
|
||||
{
|
||||
for( graphicsTexture in surface.graphicsTextures )
|
||||
{
|
||||
if( graphicsTexture.jig.enabled == true )
|
||||
{
|
||||
var point = e.getPoint();
|
||||
pressPoint = new Point(
|
||||
cast graphicsTexture.getLocation().x - point.x
|
||||
, cast graphicsTexture.getLocation().y - point.y );
|
||||
point.y -= 20;
|
||||
|
||||
if( graphicsTexture.generalPath.contains( point ) )
|
||||
{
|
||||
currentGraphic = graphicsTexture;
|
||||
surface.moveToTop( graphicsTexture );
|
||||
enabledMove = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function mouseDragged( e: MouseEvent )
|
||||
{
|
||||
if( enabledMove == true )
|
||||
{
|
||||
var x = e.getX();
|
||||
var y = e.getY();// + 20;
|
||||
|
||||
if( checkLock( new Point( x, y ) ) == true ) return;
|
||||
currentGraphic.setLocation( x + pressPoint.x, y + pressPoint.y );
|
||||
|
||||
#if applet
|
||||
surface.repaint();
|
||||
#elseif update_task
|
||||
|
||||
#else
|
||||
surface.repaint();
|
||||
#end
|
||||
}
|
||||
}
|
||||
|
||||
public function mouseExited( e: MouseEvent )
|
||||
{
|
||||
enabledMove = false;
|
||||
}
|
||||
|
||||
public function mouseMoved( e: MouseEvent ) {
|
||||
overCheck( e );
|
||||
}
|
||||
|
||||
public function overCheck( e: MouseEvent )
|
||||
{
|
||||
var showCursor = false;
|
||||
for( graphicsTexture in surface.graphicsTextures )
|
||||
{
|
||||
if( currentGraphic.jig.enabled == true )
|
||||
{
|
||||
var point = e.getPoint();
|
||||
point.y -= 20;
|
||||
if( graphicsTexture.generalPath.contains( point ) )
|
||||
{
|
||||
showCursor = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( showCursor )
|
||||
{
|
||||
e.getComponent().setCursor(
|
||||
Cursor.getPredefinedCursor( Cursor.HAND_CURSOR )
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
e.getComponent().setCursor(
|
||||
Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function mouseEntered( e: MouseEvent ) {}
|
||||
public function mouseClicked( e: MouseEvent ) {}
|
||||
public function mouseReleased( e: MouseEvent )
|
||||
{
|
||||
enabledMove = false;
|
||||
}
|
||||
|
||||
private function checkLock( tempSp: Point ):Bool
|
||||
{
|
||||
if( !enabledMove ) return true;
|
||||
var jig = currentGraphic.jig;
|
||||
if( jig == null ) return true;
|
||||
if( Math.abs( jig.xy.x - ( tempSp.x - wid ) ) < ( wid + hi )/7 &&
|
||||
Math.abs( jig.xy.y - ( tempSp.y - hi ) ) < ( wid + hi )/7 )
|
||||
{
|
||||
|
||||
currentGraphic.setLocation( jig.xy.x, jig.xy.y );
|
||||
currentGraphic.alpha = 1;
|
||||
jig.enabled = false;
|
||||
enabledMove = false;
|
||||
#if applet
|
||||
surface.repaint();
|
||||
#elseif update_task
|
||||
|
||||
#else
|
||||
surface.repaint();
|
||||
#end
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function keyTyped( e: KeyEvent ){}
|
||||
public function keyReleased(e: KeyEvent ){}
|
||||
public function keyPressed( e: KeyEvent ) nudge( e );
|
||||
|
||||
private function nudge( e: KeyEvent ){
|
||||
var location = currentGraphic.getLocation();
|
||||
switch( e.getKeyCode() )
|
||||
{
|
||||
case up:
|
||||
currentGraphic.setLocation( location.x, location.y - 1 );
|
||||
case down:
|
||||
currentGraphic.setLocation( location.x, location.y + 1 );
|
||||
case left:
|
||||
currentGraphic.setLocation( location.x - 1, location.y );
|
||||
case right:
|
||||
currentGraphic.setLocation( location.x + 1, location.y );
|
||||
}
|
||||
|
||||
#if applet
|
||||
surface.repaint();
|
||||
#elseif update_task
|
||||
|
||||
#else
|
||||
surface.repaint();
|
||||
#end
|
||||
}
|
||||
|
||||
function createGraphicsTextures(): Array<GraphicsTexture>
|
||||
{
|
||||
var arr = new Array<GraphicsTexture>();
|
||||
rows = 3;//6;
|
||||
cols = 5;//10;
|
||||
wid = 100;//50;
|
||||
hi = 100;//50;
|
||||
jigsawx = new Jigsawx( wid, hi, rows, cols );
|
||||
|
||||
var count = 0;
|
||||
var isRandom = false;
|
||||
var unCompletedPieces = new Array<GraphicsTexture>();
|
||||
|
||||
for( jig in jigsawx.jigs )
|
||||
{
|
||||
var rec = new Rectangle( Std.int( wid/2 )
|
||||
, Std.int( hi/2 )
|
||||
, Std.int( 360 * 3/2 )
|
||||
, Std.int( 240 * 3/2 )
|
||||
);
|
||||
|
||||
var graphicsTexture = new GraphicsTexture( jig
|
||||
, imageSrc
|
||||
, rec
|
||||
, jig.xy.x
|
||||
, jig.xy.y
|
||||
);
|
||||
var randX = 0.;
|
||||
var randY = 0.;
|
||||
|
||||
if( Math.random()*5 > 2 )
|
||||
{
|
||||
randX = Math.random()*200;
|
||||
randY = Math.random()*100;
|
||||
isRandom = true;
|
||||
graphicsTexture.alpha = 0.6;
|
||||
}
|
||||
else
|
||||
{
|
||||
isRandom = false;
|
||||
graphicsTexture.jig.enabled = false;
|
||||
}
|
||||
|
||||
graphicsTexture.setLocation( jig.xy.x + randX, jig.xy.y + randY );
|
||||
|
||||
if( isRandom )
|
||||
{
|
||||
unCompletedPieces.push( graphicsTexture );
|
||||
}
|
||||
else
|
||||
{
|
||||
arr.push( graphicsTexture );
|
||||
}
|
||||
|
||||
count++;
|
||||
}
|
||||
|
||||
currentGraphic = arr[0];
|
||||
|
||||
// push un completed on top
|
||||
for( graphicsTexture in unCompletedPieces ) arr.push( graphicsTexture );
|
||||
|
||||
return arr;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (c) 2013 Justinfront Ltd
|
||||
* author: Justin L Mills
|
||||
* email: JLM at Justinfront dot net
|
||||
* created: 8 August 2013
|
||||
* language: Haxe 3
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * 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.
|
||||
* * Neither the name of the Justinfront Ltd nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Justinfront Ltd ''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 Justinfront Ltd 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.
|
||||
*/
|
||||
|
||||
package jigsawxtargets.hxjava;
|
||||
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
|
||||
import java.javax.swing.JPanel;
|
||||
|
||||
import jigsawxtargets.hxjava.GraphicsTexture;
|
||||
|
||||
class Surface extends JPanel
|
||||
{
|
||||
|
||||
public var graphicsTextures: Array<GraphicsTexture>;
|
||||
|
||||
#if applet
|
||||
#elseif update_task
|
||||
public var mutex: Dynamic;
|
||||
#else
|
||||
#end
|
||||
|
||||
public function moveToTop( graphicsTexture: GraphicsTexture )
|
||||
{
|
||||
graphicsTextures.remove( graphicsTexture );
|
||||
graphicsTextures.push( graphicsTexture );
|
||||
}
|
||||
|
||||
public function new()
|
||||
{
|
||||
super( true );
|
||||
}
|
||||
@:overload override
|
||||
public function paintComponent( g: Graphics )
|
||||
{
|
||||
|
||||
#if applet
|
||||
#elseif update_task
|
||||
untyped __lock__(mutex,
|
||||
{
|
||||
#else
|
||||
#end
|
||||
super.paintComponent( g );
|
||||
var g2D: Graphics2D = cast g;
|
||||
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON );
|
||||
g2D.setRenderingHint(RenderingHints.KEY_RENDERING,
|
||||
RenderingHints.VALUE_RENDER_QUALITY );
|
||||
for( i in 0...graphicsTextures.length ) graphicsTextures[ i ].render( g2D );
|
||||
g2D.dispose();
|
||||
1;
|
||||
#if applet
|
||||
#elseif update_task
|
||||
});
|
||||
#else
|
||||
#end
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright (c) 2013 Justinfront Ltd
|
||||
* author: Justin L Mills
|
||||
* email: JLM at Justinfront dot net
|
||||
* created: 8 August 2013
|
||||
* language: Haxe 3
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * 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.
|
||||
* * Neither the name of the Justinfront Ltd nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Justinfront Ltd ''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 Justinfront Ltd 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.
|
||||
*/
|
||||
|
||||
package jigsawxtargets.hxjava;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.javax.swing.JPanel;
|
||||
import java.lang.System;
|
||||
import java.StdTypes;// Int64
|
||||
|
||||
class UpdateTask extends TimerTask
|
||||
{
|
||||
// Set true to limit fps (sleep the thread), false to not
|
||||
var limitingFPS: Bool;
|
||||
// Set true to sync draws and updates together, false to not
|
||||
var syncingUpdates: Bool;
|
||||
public var oldTime: Int64;
|
||||
var nanoseconds: Int64;
|
||||
var frames: Int;
|
||||
var updates: Int;
|
||||
// Holds the latest calculated value of updates per second
|
||||
var fps: Int;
|
||||
// Holds the latest calculated value of updates per second
|
||||
var ups: Int;
|
||||
var jpanel: JPanel;
|
||||
var mutex: Dynamic;
|
||||
|
||||
public function new()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public function newMore(jpanel_: JPanel, mutex_: Dynamic )
|
||||
{
|
||||
jpanel = jpanel_;
|
||||
mutex = mutex_;
|
||||
init();
|
||||
}
|
||||
|
||||
public function init()
|
||||
{
|
||||
oldTime = System.nanoTime();
|
||||
limitingFPS = true;
|
||||
syncingUpdates = true;
|
||||
nanoseconds = 0;
|
||||
frames = 0;
|
||||
updates = 0;
|
||||
fps = 0;
|
||||
ups = 0;
|
||||
}
|
||||
|
||||
@:overload override public function run()
|
||||
{
|
||||
|
||||
//synchronized( mutex )
|
||||
untyped __lock__( mutex,
|
||||
{
|
||||
// Calculating a new fps/ups value every second
|
||||
if( nanoseconds >= 1000000000 )
|
||||
{
|
||||
fps = frames;
|
||||
ups = updates;
|
||||
nanoseconds = nanoseconds - 1000000000;
|
||||
frames = 0;
|
||||
updates = 0;
|
||||
}
|
||||
|
||||
var elapsedTime: Int64 = System.nanoTime();
|
||||
elapsedTime = elapsedTime - oldTime;
|
||||
oldTime = oldTime - elapsedTime;
|
||||
nanoseconds = nanoseconds + elapsedTime;
|
||||
//update(elapsedTime);
|
||||
// An update occured, increment.
|
||||
updates++;
|
||||
|
||||
// Ask for a repaint
|
||||
jpanel.repaint();
|
||||
1;
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user