Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' into add-assets

  • Loading branch information...
commit 12b50c360b01bb52b3974076dd678076a1d5c3b8 2 parents cb861f5 + abf6147
@rafd rafd authored
View
BIN  _appMobi/.DS_Store
Binary file not shown
View
186 _appMobi/xhr.js
@@ -0,0 +1,186 @@
+/* xhr.js
+ * This overrides the XMLHTTPRequest object to allow cross domain ajax requests
+ */
+(function () {
+ document.addEventListener("appMobi.device.remote.data", getRemoteExtCB, false);
+ var ajaxCallbacks = [];
+
+ function getRemoteExtCB(obj) {
+ if (ajaxCallbacks.length > 0 && ajaxCallbacks[obj.id]) {
+ ajaxCallbacks[obj.id](obj);
+ }
+ }
+
+ XMLHttpRequest_Native = XMLHttpRequest;
+ XMLHttpRequest.Extension = new Object;
+
+ XMLHttpRequest.Extension.addObject = function (object) {
+ uniqueId = Math.floor(Math.random() * 99999999);
+ object.uniqueId = uniqueId;
+ this[uniqueId] = object;
+ return uniqueId;
+ }
+
+ XMLHttpRequest.Extension.sendXMLHTTP = function (data) {
+ var myparams = new AppMobi.Device.RemoteDataParameters();
+ for (var j in data.headers) {
+ myparams.addHeader(j, data.headers[j]);
+ }
+
+ myparams.url = data.requestData.URL;
+ myparams.id = data.uniqueId;
+ myparams.method = data.requestData.method
+ myparams.body = data.body;
+ try{
+ if(typeof myparams.body=="object"){
+ myparams.body=JSON.stringify(myparams.body);
+ }
+ ajaxCallbacks[myparams.id] = this.handleResponseData;
+ AppMobi.device.getRemoteDataExt(myparams);
+ }
+ catch(e){}
+ }
+
+ XMLHttpRequest.Extension.handleResponseData = function (object) {
+
+ var XMLObj = XMLHttpRequest.Extension[object.id];
+ //EMULATED "HEADERS RECEIVED" CHANGES
+ var newHeaders = [];
+ for (var j in object.extras.headers) {
+ newHeaders[j.toLowerCase()] = object.extras.headers[j]; //jQuery looks for lowercase
+ newHeaders[j] = object.extras.headers[j];
+ }
+ XMLObj.responseData.headers = newHeaders;
+ XMLObj.readyState = XMLObj.HEADERS_RECEIVED;
+ if (typeof XMLObj.onreadystatechange == 'function') XMLObj.onreadystatechange();
+
+ XMLObj.readyState = XMLObj.LOADING;
+ if (typeof XMLObj.onreadystatechange == 'function') XMLObj.onreadystatechange();
+
+ XMLObj.response = object.response;
+ XMLObj.status = object.extras.status;
+ XMLObj.responseText = object.response;
+ XMLObj.responseXML = object.response;
+ XMLObj.readyState = XMLObj.DONE;
+
+ if (typeof XMLObj.onreadystatechange == 'function') XMLObj.onreadystatechange();
+ }
+
+
+ // XMLHTTP REDEFINE
+ //=======================================================================================================================
+ //DEFINE "CONSTANTS" FOR CONSTRUCTOR
+ XMLHttpRequest.UNSENT = 0; //const
+ XMLHttpRequest.OPENED = 1; //const
+ XMLHttpRequest.HEADERS_RECEIVED = 2; //const
+ XMLHttpRequest.LOADING = 3; //const
+ XMLHttpRequest.DONE = 4; //const
+
+ //DEFINE "CONSTANTS" PROTOTYPE
+ XMLHttpRequest.prototype.UNSENT = 0; //const
+ XMLHttpRequest.prototype.OPENED = 1; //const
+ XMLHttpRequest.prototype.HEADERS_RECEIVED = 2; //const
+ XMLHttpRequest.prototype.LOADING = 3; //const
+ XMLHttpRequest.prototype.DONE = 4; //const
+ //XMLHttpRequest = {readyState:0 };
+ XMLHttpRequest.prototype.open;
+ XMLHttpRequest.prototype.readyState = 0;
+ XMLHttpRequest.prototype.onreadystatechange;
+ XMLHttpRequest.prototype.headers = {};
+ XMLHttpRequest.prototype.body = "";
+
+
+
+ XMLHttpRequest.prototype.requestData = {
+ 'method': null,
+ 'URL': null,
+ 'asynchronous': true,
+ 'username': null,
+ 'password': null,
+ 'headers': null
+ };
+ XMLHttpRequest.prototype.responseData = {
+ 'headers': null
+ };
+
+
+ XMLHttpRequest.prototype.abort = function abort() {};
+ XMLHttpRequest.prototype.addEventListener = function addEventListener() {};
+ XMLHttpRequest.prototype.constructor = function XMLHttpRequest() {};
+ XMLHttpRequest.prototype.dispatchEvent = function dispatchEvent() {};
+
+ XMLHttpRequest.prototype.getAllResponseHeaders = function getAllResponseHeaders() {
+ if (this.readyState == this.OPENED || this.readyState == this.UNSENT) return "";
+ else {
+ return this.responseData.headers;
+ }
+ };
+
+ XMLHttpRequest.prototype.getResponseHeader = function getResponseHeader(header) {
+ return this.responseData.headers && this.responseData.headers[header] ? this.responseData.headers[header] : "";
+ };
+
+ XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
+ //supported methods: CONNECT, DELETE, GET, HEAD, OPTIONS, POST, PUT, TRACE, or TRACK
+ /* Empty the list of author request headers.
+ Set the request method to method.
+ Set the request URL to url.
+ Set the request username to temp user.
+ Set the request password to temp password.
+ Set the asynchronous flag to the value of async.
+ */
+ this.requestData.method = method;
+ this.requestData.URL = url;
+ this.requestData.asynchronous = async;
+ this.requestData.user = user;
+ this.requestData.password = password;
+ this.readyState = this.OPENED;
+ if (typeof this.onreadystatechange == 'function') this.onreadystatechange();
+
+ }
+
+ XMLHttpRequest.prototype.overrideMimeType = function overrideMimeType() {};
+ XMLHttpRequest.prototype.removeEventListener = function removeEventListener() {};
+
+ XMLHttpRequest.prototype.send = function send(data) {
+ this.body = data;
+ if(this.requestData.asynchronous===false)
+ {
+ throw ("Synchronous XMLHtppRequest calls are not allowed. Please change your request to be asynchronous");
+ return;
+ }
+ XMLHttpRequest.Extension.sendXMLHTTP(this);
+ };
+
+ XMLHttpRequest.prototype.setRequestHeader = function setRequestHeader(header, value) {
+ this.headers[header] = value;
+ };
+
+
+ function XMLHttpRequest() {
+ XMLHttpRequest.Extension.addObject(this);
+ this.onabort = null;
+ this.onerror = null;
+ this.onload = null;
+ this.onloadstart = null;
+ this.onprogress = null;
+ this.onreadystatechange = null;
+ this.readyState = 0;
+ this.response = "";
+ this.responseText = "";
+ this.responseType = "";
+ this.responseXML = null;
+ this.status = 0;
+ this.statusText = "";
+ this.withCredentials = false;
+ this.requestData = {
+ 'method': null,
+ 'URL': null,
+ 'asynchronous': null,
+ 'username': null,
+ 'password': null,
+ 'headers': null
+ };
+ }
+ window.XMLHttpRequest = XMLHttpRequest;
+})();
View
BIN  images/.DS_Store
Binary file not shown
View
BIN  images/tc_bg_phone.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
92 index.html
@@ -1,3 +1,93 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
-test
+
+<head>
+<title>Hello World Application</title>
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
+ <script type="text/javascript" charset="utf-8" src="http://localhost:58888/_appMobi/appmobi.js"></script>
+ <script type="text/javascript" charset="utf-8" src="lib/caat/caat.js"></script>
+ <link href="styles.css" rel="stylesheet" type="text/css">
+ <style type="text/css">
+ * {
+ -webkit-user-select: none; /* prevent copy paste for all elements */
+ }
+ body { }
+ </style>
+
+ <script type="text/javascript" language="javascript">
+ //global variables
+ //Initialization function
+ function init() {}
+
+ //*** Prevent Default Scrolling ******
+ preventDefaultScroll = function(event) {
+ // Prevent scrolling on this element
+ event.preventDefault();
+ window.scroll(0,0);
+ return false;
+ };
+
+ window.document.addEventListener('touchmove', preventDefaultScroll, false);
+
+ function setupCAAT() {
+ console.log("Creating director");
+ var director = new CAAT.Director().initialize(
+ 320, 480, document.getElementById("thecanvas")
+ );
+
+ console.log("Creating scene");
+ var scene = director.createScene();
+
+ console.log("Creating circle");
+ var circle = new CAAT.ShapeActor().
+ setLocation(20, 20).
+ setSize(60, 60).
+ setFillStyle('#ff0000').
+ setStrokeStyle('#000000');
+
+ console.log("Adding circle to scene");
+ scene.addChild(circle);
+ console.log("Looping");
+ CAAT.loop(1);
+
+ }
+ //*** Device Ready Code **********
+ //This event handler is fired once the AppMobi libraries are ready
+ //AppMobi is ready to roll
+ function testingStuffCallback(res) {
+ document.getElementById('contentstuff').innerHTML = 'gamma(5) = ' + res;
+ }
+ function onDeviceReady() {
+ //use AppMobi viewport
+ AppMobi.display.useViewport(320,480);
+
+ //lock orientation
+ AppMobi.device.setRotateOrientation("portrait");
+ AppMobi.device.setAutoRotate(false);
+
+ //manage power
+ AppMobi.device.managePower(true,false);
+ AppMobi.device.hideSplashScreen();
+
+ // Test function
+ document.getElementById('debugging').textContent = AppMobi.testingStuff;
+ document.getElementById('contentstuff').innerHTML = 'Running test function...';
+ AppMobi.testingStuff(5, 'testingStuffCallback');
+
+ setupCAAT();
+ }
+ if (typeof(AppMobi) === "undefined") {
+ document.addEventListener("appMobi.device.ready",onDeviceReady,false);
+ } else {
+ setupCAAT();
+ }
+ </script>
+
+</head>
+
+
+<body onload="init();">
+
+ <canvas id="thecanvas"></canvas>
+
</html>
View
46 lib/caat/caat-box2d-min.js
@@ -0,0 +1,46 @@
+
+/*
+The MIT License
+
+Copyright (c) 2010-2011-2012 Ibon Tolosana [@hyperandroid]
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+Version: 0.4 build: 318
+
+Created on:
+DATE: 2012-09-15
+TIME: 16:50:46
+*/
+
+
+(function(){CAAT.enableBox2DDebug=function(b,a,c){if(b){b=new Box2D.Dynamics.b2DebugDraw;try{b.m_sprite.graphics.clear=function(){}}catch(d){}c.SetDebugDraw(b);b.SetSprite(a.ctx);b.SetDrawScale(CAAT.PMR);b.SetFillAlpha(0.5);b.SetLineThickness(1);b.SetFlags(3)}else c.SetDebugDraw(null)}})();
+(function(){CAAT.B2DBodyActor=function(){CAAT.B2DBodyActor.superclass.constructor.call(this);return this};CAAT.B2DBodyActor.prototype={restitution:0.5,friction:0.5,density:1,bodyType:Box2D.Dynamics.b2Body.b2_dynamicBody,worldBody:null,world:null,worldBodyFixture:null,bodyDef:null,fixtureDef:null,bodyData:null,recycle:false,setRecycle:function(){this.recycle=true;return this},destroy:function(){CAAT.B2DBodyActor.superclass.destroy.call(this);if(this.recycle)this.setLocation(-Number.MAX_VALUE,-Number.MAX_VALUE),
+this.setAwake(false);else{var b=this.worldBody;b.DestroyFixture(this.worldBodyFixture);this.world.DestroyBody(b)}return this},setAwake:function(b){this.worldBody.SetAwake(b);return this},setSleepingAllowed:function(b){this.worldBody.SetSleepingAllowed(b);return this},setLocation:function(b,a){this.worldBody.SetPosition(new Box2D.Common.Math.b2Vec2((b+this.width/2)/CAAT.PMR,(a+this.height/2)/CAAT.PMR));return this},setDensity:function(b){this.density=b;return this},setFriction:function(b){this.friction=
+b;return this},setRestitution:function(b){this.restitution=b;return this},setBodyType:function(b){this.bodyType=b;return this},check:function(b,a,c){b[a]||(b[a]=c)},createBody:function(b,a){if(a)this.check(a,"density",1),this.check(a,"friction",0.5),this.check(a,"restitution",0.2),this.check(a,"bodyType",Box2D.Dynamics.b2Body.b2_staticBody),this.check(a,"userData",{}),this.check(a,"image",null),this.density=a.density,this.friction=a.friction,this.restitution=a.restitution,this.bodyType=a.bodyType,
+this.image=a.image;this.world=b;return this},getCenter:function(){return{x:0,y:0}},getDistanceJointLocalAnchor:function(){return{x:0,y:0}}};extend(CAAT.B2DBodyActor,CAAT.Actor)})();
+(function(){CAAT.B2DPolygonBody=function(){CAAT.B2DPolygonBody.superclass.constructor.call(this);return this};CAAT.B2DPolygonBody.Type={EDGE:"edge",BOX:"box",POLYGON:"polygon"};CAAT.B2DPolygonBody.prototype={boundingBox:null,getDistanceJointLocalAnchor:function(){return this.worldBody.GetFixtureList().GetShape().m_centroid},getCenter:function(){var b=this.worldBody,a=b.m_xf,b=b.GetFixtureList().GetShape();return Box2D.Common.Math.b2Math.MulX(a,b.m_centroid)},animate:function(b,a){var c=this.worldBody,
+d=c.m_xf,e=this.worldBodyFixture.GetShape();e&&(d=Box2D.Common.Math.b2Math.MulX(d,e.m_centroid),CAAT.Actor.prototype.setLocation.call(this,d.x*CAAT.PMR-this.width/2,d.y*CAAT.PMR-this.height/2),this.setRotation(c.GetAngle()));return CAAT.B2DPolygonBody.superclass.animate.call(this,b,a)},createBody:function(b,a){CAAT.B2DPolygonBody.superclass.createBody.call(this,b,a);var c=CAAT.B2DPolygonBody.createPolygonBody(b,a);a.userData.actor=this;this.worldBody=c.worldBody;this.worldBodyFixture=c.worldBodyFixture;
+this.fixtureDef=c.fixDef;this.bodyDef=c.bodyDef;this.bodyData=a;this.boundingBox=c.boundingBox;this.setBackgroundImage(a.image).setSize(c.boundingBox[1].x-c.boundingBox[0].x+1,c.boundingBox[1].y-c.boundingBox[0].y+1).setFillStyle(c.worldBodyFixture.IsSensor()?"red":"green").setImageTransformation(CAAT.SpriteImage.prototype.TR_FIXED_TO_SIZE);return this}};CAAT.B2DPolygonBody.createPolygonBody=function(b,a){var c=new Box2D.Dynamics.b2FixtureDef;c.density=a.density;c.friction=a.friction;c.restitution=
+a.restitution;c.shape=new Box2D.Collision.Shapes.b2PolygonShape;var d=Number.MAX_VALUE,e=-Number.MAX_VALUE,g=Number.MAX_VALUE,j=-Number.MAX_VALUE,k=[],l=a.bodyDefScale||1;l+=(a.bodyDefScaleTolerance||0)*Math.random();for(var f=0;f<a.bodyDef.length;f++){var h=a.bodyDef[f].x*l,i=a.bodyDef[f].y*l;h<d&&(d=h);h>e&&(e=h);i<g&&(g=i);i>j&&(j=i);h+=a.x||0;i+=a.y||0;k.push(new Box2D.Common.Math.b2Vec2(h/CAAT.PMR,i/CAAT.PMR))}l=[{x:d,y:g},{x:e,y:j}];f=new Box2D.Dynamics.b2BodyDef;f.type=a.bodyType;if(a.polygonType===
+CAAT.B2DPolygonBody.Type.EDGE)c.shape.SetAsEdge(k[0],k[1]);else if(a.polygonType===CAAT.B2DPolygonBody.Type.BOX)c.shape.SetAsBox((e-d)/2/CAAT.PMR,(j-g)/2/CAAT.PMR),f.position.x=((e-d)/2+(a.x||0))/CAAT.PMR,f.position.y=((j-g)/2+(a.y||0))/CAAT.PMR;else if(a.polygonType===CAAT.B2DPolygonBody.Type.POLYGON)c.shape.SetAsArray(k,k.length);else throw"Unkown bodyData polygonType: "+a.polygonType;c.userData=a.userData;f.userData=a.userData;d=b.CreateBody(f);e=d.CreateFixture(c);a.isSensor&&e.SetSensor(true);
+return{worldBody:d,worldBodyFixture:e,fixDef:c,bodyDef:f,boundingBox:l}};extend(CAAT.B2DPolygonBody,CAAT.B2DBodyActor)})();
+(function(){CAAT.B2DCircularBody=function(){CAAT.B2DCircularBody.superclass.constructor.call(this);return this};CAAT.B2DCircularBody.prototype={radius:1,getDistanceJointLocalAnchor:function(){return new Box2D.Common.Math.b2Vec2(0,0)},getCenter:function(){return this.worldBody.m_xf.position},animate:function(b,a){var c=this.worldBody,d=c.m_xf;CAAT.Actor.prototype.setLocation.call(this,CAAT.PMR*d.position.x-this.width/2,CAAT.PMR*d.position.y-this.height/2);this.setRotation(c.GetAngle());return CAAT.B2DCircularBody.superclass.animate.call(this,
+b,a)},createBody:function(b,a){var c=a.radius||1;c+=(a.bodyDefScaleTolerance||0)*Math.random();a.radius=c;CAAT.B2DCircularBody.superclass.createBody.call(this,b,a);if(a.radius)this.radius=a.radius;c=new Box2D.Dynamics.b2FixtureDef;c.density=this.density;c.friction=this.friction;c.restitution=this.restitution;c.shape=new Box2D.Collision.Shapes.b2CircleShape(this.radius/CAAT.PMR);var d=new Box2D.Dynamics.b2BodyDef;d.type=this.bodyType;d.position.x=a.x/CAAT.PMR;d.position.y=a.y/CAAT.PMR;a.userData.actor=
+this;c.userData=a.userData;d.userData=a.userData;var e=b.CreateBody(d),g=e.CreateFixture(c);a.isSensor&&g.SetSensor(true);this.worldBody=e;this.worldBodyFixture=g;this.fixtureDef=c;this.bodyDef=d;this.bodyData=a;this.setFillStyle(this.worldBodyFixture.IsSensor()?"red":"blue").setBackgroundImage(this.image).setSize(2*this.radius,2*this.radius).setImageTransformation(CAAT.SpriteImage.prototype.TR_FIXED_TO_SIZE);return this}};extend(CAAT.B2DCircularBody,CAAT.B2DBodyActor)})();
View
572 lib/caat/caat-box2d.js
@@ -0,0 +1,572 @@
+/*
+The MIT License
+
+Copyright (c) 2010-2011-2012 Ibon Tolosana [@hyperandroid]
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+Version: 0.4 build: 319
+
+Created on:
+DATE: 2012-09-15
+TIME: 16:51:26
+*/
+
+
+/**
+ * See LICENSE file.
+ *
+ * Box2D actors.
+ */
+
+(function() {
+
+ /**
+ * Ad Eemeli Kelokorpi suggested,
+ * @param set
+ * @param director
+ * @param world
+ */
+ CAAT.enableBox2DDebug= function(set, director, world) {
+
+ if ( set ) {
+ var debugDraw = new Box2D.Dynamics.b2DebugDraw();
+ try {
+ debugDraw.m_sprite.graphics.clear= function() {};
+ } catch( e ) {}
+
+ world.SetDebugDraw(debugDraw);
+
+ debugDraw.SetSprite(director.ctx);
+ debugDraw.SetDrawScale(CAAT.PMR);
+ debugDraw.SetFillAlpha(.5);
+ debugDraw.SetLineThickness(1.0);
+ debugDraw.SetFlags(0x0001 | 0x0002);
+
+ } else {
+ world.SetDebugDraw( null );
+ }
+ }
+})();
+
+(function() {
+
+ /**
+ *
+ * Define a binding beween a box2D Body instance and a CAAT.Actor.
+ * This function object should not be directly instantiated.
+ * By now, one Box2D body can contain one single fixture and shape.
+ *
+ * @constructor
+ */
+ CAAT.B2DBodyActor= function() {
+ CAAT.B2DBodyActor.superclass.constructor.call(this);
+ return this;
+ };
+
+ CAAT.B2DBodyActor.prototype= {
+
+ /**
+ * Body restitution.
+ */
+ restitution: .5,
+
+ /**
+ * Body friction.
+ */
+ friction: .5,
+
+ /**
+ * Body dentisy
+ */
+ density: 1,
+
+ /**
+ * Dynamic bodies by default
+ */
+ bodyType: Box2D.Dynamics.b2Body.b2_dynamicBody,
+
+ /**
+ * Box2D body
+ */
+ worldBody: null,
+
+ world: null,
+
+ /**
+ * Box2d fixture
+ */
+ worldBodyFixture: null,
+ bodyDef: null,
+ fixtureDef: null,
+
+ /**
+ * BodyData object linked to the box2D body.
+ */
+ bodyData: null,
+
+ recycle: false,
+
+ /**
+ * set this actor to recycle its body, that is, do not destroy it.
+ */
+ setRecycle : function() {
+ this.recycle= true;
+ return this;
+ },
+ destroy : function() {
+
+ CAAT.B2DBodyActor.superclass.destroy.call(this);
+ if ( this.recycle ) {
+ this.setLocation(-Number.MAX_VALUE, -Number.MAX_VALUE);
+ this.setAwake(false);
+ } else {
+ var body= this.worldBody;
+ body.DestroyFixture( this.worldBodyFixture );
+ this.world.DestroyBody(body);
+ }
+
+ return this;
+ },
+ setAwake : function( bool ) {
+ this.worldBody.SetAwake(bool);
+ return this;
+ },
+ setSleepingAllowed : function(bool) {
+ this.worldBody.SetSleepingAllowed(bool);
+ return this;
+ },
+ setLocation : function(x,y) {
+ this.worldBody.SetPosition(
+ new Box2D.Common.Math.b2Vec2(
+ (x+this.width/2)/CAAT.PMR,
+ (y+this.height/2)/CAAT.PMR) );
+ return this;
+ },
+ /**
+ * Set this body's
+ * density.
+ * @param d {number}
+ */
+ setDensity : function(d) {
+ this.density= d;
+ return this;
+ },
+
+ /**
+ * Set this body's friction.
+ * @param f {number}
+ */
+ setFriction : function(f) {
+ this.friction= f;
+ return this;
+ },
+
+ /**
+ * Set this body's restitution coeficient.
+ * @param r {number}
+ */
+ setRestitution : function(r) {
+ this.restitution= r;
+ return this;
+ },
+
+ /**
+ * Set this body's type:
+ * @param bodyType {Box2D.Dynamics.b2Body.b2_*}
+ */
+ setBodyType : function(bodyType) {
+ this.bodyType= bodyType;
+ return this;
+ },
+
+ /**
+ * Helper method to check whether this js object contains a given property and if it doesn't exist
+ * create and set it to def value.
+ * @param obj {object}
+ * @param prop {string}
+ * @param def {object}
+ */
+ check : function(obj, prop, def) {
+ if ( !obj[prop] ) {
+ obj[prop]= def;
+ }
+ },
+
+ /**
+ * Create an actor as a box2D body binding, create it on the given world and with
+ * the initialization data set in bodyData object.
+ * @param world {Box2D.Dynamics.b2World} a Box2D world instance
+ * @param bodyData {object} An object with body info.
+ */
+ createBody : function(world, bodyData) {
+
+ if ( bodyData ) {
+ this.check( bodyData, 'density', 1 );
+ this.check( bodyData, 'friction', .5 );
+ this.check( bodyData, 'restitution', .2 );
+ this.check( bodyData, 'bodyType', Box2D.Dynamics.b2Body.b2_staticBody );
+ this.check( bodyData, 'userData', {} );
+ this.check( bodyData, 'image', null );
+
+ this.density= bodyData.density;
+ this.friction= bodyData.friction;
+ this.restitution= bodyData.restitution;
+ this.bodyType= bodyData.bodyType;
+ this.image= bodyData.image;
+
+ }
+
+ this.world= world;
+
+ return this;
+ },
+
+ /**
+ * Get this body's center on screen regardless of its shape.
+ * This method will return box2d body's centroid.
+ */
+ getCenter : function() {
+ return {x:0, y:0};
+ },
+
+ /**
+ * Get a distance joint's position on pixels.
+ */
+ getDistanceJointLocalAnchor : function() {
+ return {x:0, y:0};
+ }
+ };
+
+ extend( CAAT.B2DBodyActor, CAAT.Actor );
+
+})();
+
+(function() {
+
+ /**
+ *
+ * Create a polygon shaped body in box2D and its CAAT.Actor counterpart.
+ *
+ * @constructor
+ */
+ CAAT.B2DPolygonBody= function() {
+ CAAT.B2DPolygonBody.superclass.constructor.call(this);
+ return this;
+ };
+
+ /**
+ *
+ * Define different types of polygon body types.
+ *
+ * @enum
+ */
+ CAAT.B2DPolygonBody.Type= {
+ EDGE: 'edge',
+ BOX: 'box',
+ POLYGON:'polygon'
+ };
+
+ CAAT.B2DPolygonBody.prototype= {
+
+ /**
+ * Measured body's bounding box.
+ */
+ boundingBox: null,
+
+ /**
+ * Get on-screen distance joint coordinate.
+ */
+ getDistanceJointLocalAnchor : function() {
+ var b= this.worldBody;
+ var xf= b.m_xf;
+ var poly= b.GetFixtureList().GetShape();
+ return poly.m_centroid;
+ },
+
+ /**
+ * Get this polygon's centroid on screen coordinates.
+ */
+ getCenter : function() {
+ var b= this.worldBody;
+ var xf= b.m_xf;
+ var poly= b.GetFixtureList().GetShape();
+ return Box2D.Common.Math.b2Math.MulX(xf, poly.m_centroid);
+ },
+
+ /**
+ * Method override to get position and rotation angle from box2d body.
+ * @param director {CAAT.Director}
+ * @param time {number}
+ */
+ animate: function(director, time) {
+
+ var b= this.worldBody;
+ var xf= b.m_xf;
+
+ var poly= this.worldBodyFixture.GetShape();
+ if ( poly ) {
+ var v= Box2D.Common.Math.b2Math.MulX(xf, poly.m_centroid);
+ //this.setLocation(
+ CAAT.Actor.prototype.setLocation.call( this,
+ v.x*CAAT.PMR - this.width/2,
+ v.y*CAAT.PMR - this.height/2 );
+ this.setRotation( b.GetAngle() );
+ }
+
+ return CAAT.B2DPolygonBody.superclass.animate.call(this,director,time);
+ },
+
+ /**
+ * Create a box2d body and link it to this CAAT.Actor.
+ * @param world {Box2D.Dynamics.b2World}
+ * @param bodyData {object}
+ */
+ createBody : function(world, bodyData) {
+ CAAT.B2DPolygonBody.superclass.createBody.call(this,world,bodyData);
+
+ var box2D_data= CAAT.B2DPolygonBody.createPolygonBody(world,bodyData);
+
+ bodyData.userData.actor = this;
+
+ this.worldBody= box2D_data.worldBody;
+ this.worldBodyFixture= box2D_data.worldBodyFixture;
+ this.fixtureDef= box2D_data.fixDef;
+ this.bodyDef= box2D_data.bodyDef;
+ this.bodyData= bodyData;
+ this.boundingBox= box2D_data.boundingBox;
+
+ this.setBackgroundImage( bodyData.image ).
+ setSize(
+ box2D_data.boundingBox[1].x-box2D_data.boundingBox[0].x+1,
+ box2D_data.boundingBox[1].y-box2D_data.boundingBox[0].y+1 ).
+ setFillStyle( box2D_data.worldBodyFixture.IsSensor() ? 'red' : 'green').
+ setImageTransformation(CAAT.SpriteImage.prototype.TR_FIXED_TO_SIZE);
+
+ return this;
+ }
+ };
+
+ /**
+ * Helper function to aid in box2d polygon shaped bodies.
+ * @param world
+ * @param bodyData
+ */
+ CAAT.B2DPolygonBody.createPolygonBody= function(world, bodyData) {
+ var fixDef = new Box2D.Dynamics.b2FixtureDef();
+ fixDef.density = bodyData.density;
+ fixDef.friction = bodyData.friction;
+ fixDef.restitution = bodyData.restitution;
+ fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape();
+
+ var minx = Number.MAX_VALUE;
+ var maxx = -Number.MAX_VALUE;
+ var miny = Number.MAX_VALUE;
+ var maxy = -Number.MAX_VALUE;
+
+ var vec = [];
+
+ var scale = (bodyData.bodyDefScale || 1);
+ scale = scale + (bodyData.bodyDefScaleTolerance || 0) * Math.random();
+
+ for (var i = 0; i < bodyData.bodyDef.length; i++) {
+ var x = bodyData.bodyDef[i].x * scale;
+ var y = bodyData.bodyDef[i].y * scale;
+ if (x < minx) {
+ minx = x;
+ }
+ if (x > maxx) {
+ maxx = x;
+ }
+ if (y < miny) {
+ miny = y;
+ }
+ if (y > maxy) {
+ maxy = y;
+ }
+
+ x += bodyData.x || 0;
+ y += bodyData.y || 0;
+ vec.push(new Box2D.Common.Math.b2Vec2(x / CAAT.PMR, y / CAAT.PMR));
+ }
+
+ var boundingBox = [
+ {x:minx, y:miny},
+ {x:maxx, y:maxy}
+ ];
+
+ var bodyDef = new Box2D.Dynamics.b2BodyDef();
+ bodyDef.type = bodyData.bodyType;
+
+ if (bodyData.polygonType === CAAT.B2DPolygonBody.Type.EDGE) {
+
+ fixDef.shape.SetAsEdge(vec[0], vec[1]);
+
+ } else if (bodyData.polygonType === CAAT.B2DPolygonBody.Type.BOX) {
+
+ fixDef.shape.SetAsBox(
+ (maxx - minx) / 2 / CAAT.PMR,
+ (maxy - miny) / 2 / CAAT.PMR);
+ bodyDef.position.x = ((maxx - minx) / 2 + (bodyData.x || 0)) / CAAT.PMR;
+ bodyDef.position.y = ((maxy - miny) / 2 + (bodyData.y || 0)) / CAAT.PMR;
+
+ } else if (bodyData.polygonType === CAAT.B2DPolygonBody.Type.POLYGON ) {
+
+ fixDef.shape.SetAsArray(vec, vec.length);
+
+ } else {
+ throw 'Unkown bodyData polygonType: '+bodyData.polygonType;
+ }
+
+ // link entre cuerpo fisico box2d y caat.
+ fixDef.userData = bodyData.userData;
+ bodyDef.userData = bodyData.userData;
+
+ var worldBody = world.CreateBody(bodyDef);
+ var worldBodyFixture = worldBody.CreateFixture(fixDef);
+
+
+ if (bodyData.isSensor) {
+ worldBodyFixture.SetSensor(true);
+ }
+
+ return {
+ worldBody: worldBody,
+ worldBodyFixture: worldBodyFixture,
+ fixDef: fixDef,
+ bodyDef: bodyDef,
+ boundingBox: boundingBox
+ };
+ };
+
+ extend( CAAT.B2DPolygonBody, CAAT.B2DBodyActor );
+})();
+
+(function() {
+
+ /**
+ *
+ * Create a box2d circular body.
+ *
+ * @constructor
+ */
+ CAAT.B2DCircularBody= function() {
+ CAAT.B2DCircularBody.superclass.constructor.call(this);
+ return this;
+ };
+
+ CAAT.B2DCircularBody.prototype= {
+
+ /**
+ * Default radius.
+ */
+ radius: 1,
+
+ /**
+ * Get a given dinstance joint's local anchor position.
+ * For a circular body, it is its center.
+ */
+ getDistanceJointLocalAnchor : function() {
+ return new Box2D.Common.Math.b2Vec2(0,0);
+ },
+
+ /**
+ * Get this box2d body's center position on screen.
+ */
+ getCenter : function() {
+ return this.worldBody.m_xf.position;
+ },
+
+ /**
+ * Method override to get position and rotation angle from box2d body.
+ * @param director {CAAT.Director}
+ * @param time {number}
+ */
+ animate: function(director, time) {
+
+ var b= this.worldBody;
+ var xf= b.m_xf;
+ //this.setLocation(
+ CAAT.Actor.prototype.setLocation.call( this,
+ CAAT.PMR*xf.position.x - this.width/2,
+ CAAT.PMR*xf.position.y - this.height/2 );
+ this.setRotation( b.GetAngle() );
+
+ return CAAT.B2DCircularBody.superclass.animate.call(this,director,time);
+ },
+
+ /**
+ * Create a box2d body and link it to this CAAT.Actor instance.
+ * @param world {Box2D.Dynamics.b2World} a Box2D world instance
+ * @param bodyData {object}
+ */
+ createBody : function(world, bodyData) {
+
+ var scale= (bodyData.radius || 1);
+ scale= scale+ (bodyData.bodyDefScaleTolerance || 0)*Math.random();
+ bodyData.radius= scale;
+
+ CAAT.B2DCircularBody.superclass.createBody.call(this,world,bodyData);
+
+ if ( bodyData.radius ) this.radius= bodyData.radius;
+
+ var fixDef= new Box2D.Dynamics.b2FixtureDef();
+ fixDef.density= this.density;
+ fixDef.friction= this.friction;
+ fixDef.restitution= this.restitution;
+ fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(this.radius/CAAT.PMR);
+
+ var bodyDef = new Box2D.Dynamics.b2BodyDef();
+ bodyDef.type = this.bodyType;
+ bodyDef.position.x= bodyData.x/CAAT.PMR;
+ bodyDef.position.y= bodyData.y/CAAT.PMR;
+
+ // link entre cuerpo fisico box2d y caat.
+ bodyData.userData.actor= this;
+ fixDef.userData= bodyData.userData;
+ bodyDef.userData= bodyData.userData;
+
+ var worldBody= world.CreateBody(bodyDef);
+ var worldBodyFixture= worldBody.CreateFixture(fixDef);
+
+ if ( bodyData.isSensor ) {
+ worldBodyFixture.SetSensor(true);
+ }
+
+ this.worldBody= worldBody;
+ this.worldBodyFixture= worldBodyFixture;
+ this.fixtureDef= fixDef;
+ this.bodyDef= bodyDef;
+ this.bodyData= bodyData;
+
+
+ this.setFillStyle(this.worldBodyFixture.IsSensor() ? 'red' : 'blue').
+ setBackgroundImage(this.image).
+ setSize(2*this.radius,2*this.radius).
+ setImageTransformation(CAAT.SpriteImage.prototype.TR_FIXED_TO_SIZE);
+
+
+ return this;
+ }
+ };
+
+ extend( CAAT.B2DCircularBody, CAAT.B2DBodyActor );
+})();
+
+
View
32 lib/caat/caat-css-min.js
@@ -0,0 +1,32 @@
+
+/*
+The MIT License
+
+Copyright (c) 2010-2011-2012 Ibon Tolosana [@hyperandroid]
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+Version: 0.4 build: 318
+
+Created on:
+DATE: 2012-09-15
+TIME: 16:50:46
+*/
+
+
View
17,988 lib/caat/caat-css.js
17,988 additions, 0 deletions not shown
View
467 lib/caat/caat-min.js
467 additions, 0 deletions not shown
View
21,005 lib/caat/caat.js
21,005 additions, 0 deletions not shown
View
3  styles.css
@@ -0,0 +1,3 @@
+#contentstuff {
+ background-color: red;
+}

0 comments on commit 12b50c3

Please sign in to comment.
Something went wrong with that request. Please try again.