social_cheesecake 0.0.1 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
@@ -0,0 +1,541 @@
1
+ /**
2
+ * KineticJS JavaScript Library v2.3.2
3
+ * http://www.kineticjs.com/
4
+ * Copyright 2011, Eric Rowell
5
+ * Licensed under the MIT or GPL Version 2 licenses.
6
+ * Date: Dec 01 2011
7
+ *
8
+ * Copyright (C) 2011 by Eric Rowell
9
+ *
10
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ * of this software and associated documentation files (the "Software"), to deal
12
+ * in the Software without restriction, including without limitation the rights
13
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ * copies of the Software, and to permit persons to whom the Software is
15
+ * furnished to do so, subject to the following conditions:
16
+ *
17
+ * The above copyright notice and this permission notice shall be included in
18
+ * all copies or substantial portions of the Software.
19
+ *
20
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
+ * THE SOFTWARE.
27
+ */
28
+ var Kinetic = {};
29
+
30
+ /****************************************
31
+ * Stage
32
+ */
33
+ Kinetic.Stage = function(containerId, width, height){
34
+ this.container = document.getElementById(containerId);
35
+ this.width = width;
36
+ this.height = height;
37
+ this.shapes = [];
38
+ this.zIndexCounter = 9999;
39
+ this.idCounter = 0;
40
+ this.dblClickWindow = 400; // ms
41
+ // desktop flags
42
+ this.mousePos = null;
43
+ this.mouseDown = false;
44
+ this.mouseUp = false;
45
+
46
+ // mobile flags
47
+ this.touchPos = null;
48
+ this.touchStart = false;
49
+ this.touchEnd = false;
50
+
51
+ // add stage canvas
52
+ this.canvas = document.createElement('canvas');
53
+ this.context = this.canvas.getContext('2d');
54
+ this.id = 0;
55
+
56
+ this.canvas.width = this.width;
57
+ this.canvas.height = this.height;
58
+ this.canvas.style.position = 'absolute';
59
+ this.container.appendChild(this.canvas);
60
+
61
+ this.listen();
62
+ };
63
+
64
+ /*
65
+ * clear stage canvas
66
+ */
67
+ Kinetic.Stage.prototype.clear = function(){
68
+ var context = this.getContext();
69
+ var canvas = this.getCanvas();
70
+ context.clearRect(0, 0, canvas.width, canvas.height);
71
+ };
72
+
73
+ /*
74
+ * clear stage canvas and all shape canvases
75
+ */
76
+ Kinetic.Stage.prototype.clearAll = function(){
77
+ // clear stage
78
+ this.clear();
79
+
80
+ // clear shapes
81
+ for (var n = 0; n < this.shapes.length; n++) {
82
+ this.shapes[n].clear();
83
+ }
84
+ };
85
+
86
+ /*
87
+ * draw all shapes
88
+ */
89
+ Kinetic.Stage.prototype.drawAll = function(){
90
+ // draw shapes
91
+ for (var n = 0; n < this.shapes.length; n++) {
92
+ this.shapes[n].draw();
93
+ }
94
+ };
95
+
96
+ /*
97
+ * remove a shape from the stage
98
+ */
99
+ Kinetic.Stage.prototype.remove = function(shape){
100
+ var shapes = this.shapes;
101
+
102
+ // remove canvas
103
+ this.container.removeChild(shape.getCanvas());
104
+
105
+ // remove from shapes array
106
+ for (var n = 0; n < shapes.length; n++) {
107
+ var id = shapes[n].id;
108
+ if (id == shape.id) {
109
+ this.shapes.splice(n, 1);
110
+ }
111
+ }
112
+ };
113
+
114
+ /*
115
+ * remove all shapes from the stage
116
+ */
117
+ Kinetic.Stage.prototype.removeAll = function(){
118
+ // remove all shapes
119
+ while (this.shapes.length > 0) {
120
+ var that = this;
121
+ this.remove(that.shapes[0]);
122
+ }
123
+ };
124
+
125
+ /*
126
+ * get stage canvas
127
+ */
128
+ Kinetic.Stage.prototype.getCanvas = function(){
129
+ return this.canvas;
130
+ };
131
+
132
+ /*
133
+ * get stage context
134
+ */
135
+ Kinetic.Stage.prototype.getContext = function(){
136
+ return this.context;
137
+ };
138
+
139
+ /*
140
+ * add event listener to stage (which is essentially
141
+ * the container DOM)
142
+ */
143
+ Kinetic.Stage.prototype.addEventListener = function(type, func){
144
+ this.container.addEventListener(type, func);
145
+ };
146
+
147
+ /*
148
+ * add shape to stage
149
+ */
150
+ Kinetic.Stage.prototype.add = function(shape){
151
+ shape.stage = this;
152
+ shape.canvas = document.createElement('canvas');
153
+ shape.context = shape.canvas.getContext('2d');
154
+ shape.id = ++this.idCounter;
155
+ shape.canvas.width = this.width;
156
+ shape.canvas.height = this.height;
157
+ shape.canvas.style.zIndex = ++this.zIndexCounter;
158
+ shape.canvas.style.position = 'absolute';
159
+ this.container.appendChild(shape.canvas);
160
+ this.shapes.push(shape);
161
+ shape.draw();
162
+ };
163
+
164
+ /*
165
+ * handle incoming event
166
+ */
167
+ Kinetic.Stage.prototype.handleEvent = function(evt){
168
+ if (!evt) {
169
+ evt = window.event;
170
+ }
171
+
172
+ this.setMousePosition(evt);
173
+ this.setTouchPosition(evt);
174
+ var that = this;
175
+ for (var n = this.shapes.length - 1; n >= 0; n--) {
176
+ var pubShape = this.shapes[n];
177
+
178
+ (function(){
179
+ var shape = pubShape;
180
+ var pos = that.touchPos || that.mousePos;
181
+ var el = shape.eventListeners;
182
+
183
+ if (pos !== null && shape.context.isPointInPath(pos.x, pos.y)) {
184
+ // handle onmousedown
185
+ if (that.mouseDown) {
186
+ that.mouseDown = false;
187
+ shape.clickStart = true;
188
+
189
+ if (el.onmousedown !== undefined) {
190
+ el.onmousedown(evt);
191
+ }
192
+
193
+ n = -1; // break;
194
+ }
195
+ // handle onmouseup & onclick
196
+ else if (that.mouseUp) {
197
+ that.mouseUp = false;
198
+ if (el.onmouseup !== undefined) {
199
+ el.onmouseup(evt);
200
+ }
201
+
202
+ // detect if click or double click occurred
203
+ if (shape.clickStart) {
204
+
205
+ if (el.onclick !== undefined) {
206
+ el.onclick(evt);
207
+ }
208
+
209
+ if (el.ondblclick !== undefined && shape.inDoubleClickWindow) {
210
+ el.ondblclick(evt);
211
+ }
212
+
213
+ shape.inDoubleClickWindow = true;
214
+
215
+ setTimeout(function(){
216
+ shape.inDoubleClickWindow = false;
217
+ }, that.dblClickWindow);
218
+ }
219
+
220
+ n = -1; // break;
221
+ }
222
+
223
+ // handle onmouseover
224
+ else if (!shape.mouseOver) {
225
+ shape.mouseOver = true;
226
+ if (el.onmouseover !== undefined) {
227
+ el.onmouseover(evt);
228
+ }
229
+
230
+ n = -1; // break;
231
+ }
232
+
233
+ // handle onmousemove
234
+ else if (el.onmousemove !== undefined) {
235
+ el.onmousemove(evt);
236
+
237
+ n = -1; // break;
238
+ }
239
+
240
+ // handle touchstart
241
+ else if (that.touchStart) {
242
+ that.touchStart = false;
243
+ if (el.touchstart !== undefined) {
244
+ el.touchstart(evt);
245
+ }
246
+
247
+ n = -1; // break;
248
+ }
249
+
250
+ // handle touchend
251
+ else if (that.touchEnd) {
252
+ that.touchEnd = false;
253
+ if (el.touchend !== undefined) {
254
+ el.touchend(evt);
255
+ }
256
+
257
+ n = -1; // break;
258
+ }
259
+
260
+ // handle touchmove
261
+ else if (!that.touchMove) {
262
+ if (el.touchmove !== undefined) {
263
+ el.touchmove(evt);
264
+ }
265
+
266
+ n = -1; // break;
267
+ }
268
+ }
269
+ // handle mouseout condition
270
+ else if (shape.mouseOver) {
271
+ shape.mouseOver = false;
272
+ if (el.onmouseout !== undefined) {
273
+ el.onmouseout(evt);
274
+ }
275
+
276
+ n = -1; // break;
277
+ }
278
+ }());
279
+ }
280
+ };
281
+
282
+ /*
283
+ * begin listening for events by adding event handlers
284
+ * to the container
285
+ */
286
+ Kinetic.Stage.prototype.listen = function(){
287
+ var that = this;
288
+
289
+ // desktop events
290
+ this.container.addEventListener("mousedown", function(evt){
291
+ that.mouseDown = true;
292
+ that.handleEvent(evt);
293
+ }, false);
294
+
295
+ this.container.addEventListener("mousemove", function(evt){
296
+ that.mouseUp = false;
297
+ that.mouseDown = false;
298
+ that.handleEvent(evt);
299
+ }, false);
300
+
301
+ this.container.addEventListener("mouseup", function(evt){
302
+ that.mouseUp = true;
303
+ that.mouseDown = false;
304
+ that.handleEvent(evt);
305
+
306
+ // clear all click starts
307
+ for (var i = 0; i < that.shapes.length; i++) {
308
+ that.shapes[i].clickStart = false;
309
+ }
310
+ }, false);
311
+
312
+ this.container.addEventListener("mouseover", function(evt){
313
+ that.handleEvent(evt);
314
+ }, false);
315
+
316
+ this.container.addEventListener("mouseout", function(evt){
317
+ that.mousePos = null;
318
+ }, false);
319
+
320
+ // mobile events
321
+ this.container.addEventListener("touchstart", function(evt){
322
+ evt.preventDefault();
323
+ that.touchStart = true;
324
+ that.handleEvent(evt);
325
+ }, false);
326
+
327
+ this.container.addEventListener("touchmove", function(evt){
328
+ evt.preventDefault();
329
+ that.handleEvent(evt);
330
+ }, false);
331
+
332
+ this.container.addEventListener("touchend", function(evt){
333
+ evt.preventDefault();
334
+ that.touchEnd = true;
335
+ that.handleEvent(evt);
336
+ }, false);
337
+ };
338
+
339
+ /*
340
+ * get mouse position for desktop apps
341
+ */
342
+ Kinetic.Stage.prototype.getMousePos = function(evt){
343
+ return this.mousePos;
344
+ };
345
+
346
+ /*
347
+ * get touch position for mobile apps
348
+ */
349
+ Kinetic.Stage.prototype.getTouchPos = function(evt){
350
+ return this.touchPos;
351
+ };
352
+
353
+ /*
354
+ * set mouse positon for desktop apps
355
+ */
356
+ Kinetic.Stage.prototype.setMousePosition = function(evt){
357
+ var mouseX = evt.clientX - this.getContainerPos().left + window.pageXOffset;
358
+ var mouseY = evt.clientY - this.getContainerPos().top + window.pageYOffset;
359
+ this.mousePos = {
360
+ x: mouseX,
361
+ y: mouseY
362
+ };
363
+ };
364
+
365
+ /*
366
+ * set touch position for mobile apps
367
+ */
368
+ Kinetic.Stage.prototype.setTouchPosition = function(evt){
369
+ if (evt.touches !== undefined && evt.touches.length == 1) { // Only deal with
370
+ // one finger
371
+ var touch = evt.touches[0];
372
+ // Get the information for finger #1
373
+ var touchX = touch.clientX - this.getContainerPos().left + window.pageXOffset;
374
+ var touchY = touch.clientY - this.getContainerPos().top + window.pageYOffset;
375
+
376
+ this.touchPos = {
377
+ x: touchX,
378
+ y: touchY
379
+ };
380
+ }
381
+ };
382
+
383
+ /*
384
+ * get container position
385
+ */
386
+ Kinetic.Stage.prototype.getContainerPos = function(){
387
+ var obj = this.container;
388
+ var top = 0;
389
+ var left = 0;
390
+ while (obj.tagName != "BODY") {
391
+ top += obj.offsetTop;
392
+ left += obj.offsetLeft;
393
+ obj = obj.offsetParent;
394
+ }
395
+ return {
396
+ top: top,
397
+ left: left
398
+ };
399
+ };
400
+
401
+ /*
402
+ * get container DOM element
403
+ */
404
+ Kinetic.Stage.prototype.getContainer = function(){
405
+ return this.container;
406
+ };
407
+
408
+ /****************************************
409
+ * Shape
410
+ */
411
+ Kinetic.Shape = function(drawFunc){
412
+ this.drawFunc = drawFunc;
413
+ this.x = 0;
414
+ this.y = 0;
415
+ this.scale = {
416
+ x: 1,
417
+ y: 1
418
+ };
419
+ this.rotation = 0; // radians
420
+ this.eventListeners = {};
421
+ this.mouseOver = false;
422
+ this.clickStart = false;
423
+ this.inDblClickWindow = false;
424
+ };
425
+
426
+ /*
427
+ * get stage
428
+ */
429
+ Kinetic.Shape.prototype.getStage = function(){
430
+ return this.stage();
431
+ };
432
+
433
+ /*
434
+ * draw shape
435
+ */
436
+ Kinetic.Shape.prototype.draw = function(args){
437
+ var context = this.getContext();
438
+ this.clear();
439
+ context.save();
440
+
441
+ if (this.x !== 0 || this.y !== 0) {
442
+ context.translate(this.x, this.y);
443
+ }
444
+ if (this.rotation !== 0) {
445
+ context.rotate(this.rotation);
446
+ }
447
+ if (this.scale.x != 1 || this.scale.y != 1) {
448
+ context.scale(this.scale.x, this.scale.y);
449
+ }
450
+
451
+ this.drawFunc(args);
452
+ context.restore();
453
+ };
454
+
455
+ /*
456
+ * get shape canvas
457
+ */
458
+ Kinetic.Shape.prototype.getCanvas = function(){
459
+ return this.canvas;
460
+ };
461
+
462
+ /*
463
+ * get shape context
464
+ */
465
+ Kinetic.Shape.prototype.getContext = function(){
466
+ return this.context;
467
+ };
468
+
469
+ /*
470
+ * set shape canvas scale
471
+ */
472
+ Kinetic.Shape.prototype.setScale = function(scale){
473
+ this.scale.x = scale;
474
+ this.scale.y = scale;
475
+ };
476
+
477
+ /*
478
+ * clear shape canvas
479
+ */
480
+ Kinetic.Shape.prototype.clear = function(){
481
+ var context = this.getContext();
482
+ var canvas = this.getCanvas();
483
+ context.clearRect(0, 0, canvas.width, canvas.height);
484
+ };
485
+
486
+ /*
487
+ * add event listener to shape
488
+ */
489
+ Kinetic.Shape.prototype.addEventListener = function(type, func){
490
+ var event = (type.indexOf('touch') == -1) ? 'on' + type : type;
491
+ this.eventListeners[event] = func;
492
+ };
493
+
494
+ /*
495
+ * move shape canvas to the top via z-index
496
+ */
497
+ Kinetic.Shape.prototype.moveToTop = function(){
498
+ var stage = this.stage;
499
+ // remove shape from shapes
500
+ for (var n = 0; n < stage.shapes.length; n++) {
501
+ var reg = stage.shapes[n];
502
+ if (reg.id == this.id) {
503
+ stage.shapes.splice(n, 1);
504
+ stage.shapes.push(this);
505
+ break;
506
+ }
507
+ }
508
+
509
+ // reorder canvases
510
+ for (var n = 0; n < stage.shapes.length; n++) {
511
+ var reg = stage.shapes[n];
512
+ reg.getCanvas().style.zIndex = ++stage.zIndexCounter;
513
+ }
514
+ };
515
+
516
+ /****************************************
517
+ * drawImage util
518
+ * This util function draws a rectangular shape
519
+ * over a canvas image to provide a detectable path
520
+ */
521
+ Kinetic.drawImage = function(imageObj, x, y, width, height){
522
+ if (!width) {
523
+ width = imageObj.width;
524
+ }
525
+ if (!height) {
526
+ height = imageObj.height;
527
+ }
528
+ return function(){
529
+ var context = this.getContext();
530
+ context.drawImage(imageObj, x, y, width, height);
531
+ context.beginPath();
532
+ context.rect(x, y, width, height);
533
+ context.closePath();
534
+ };
535
+ };
536
+
537
+
538
+
539
+
540
+
541
+
@@ -0,0 +1,161 @@
1
+ var socialCheesecake = socialCheesecake || {};
2
+
3
+ (function() {
4
+ var scripts = document.head.getElementsByTagName('script');
5
+ var rootPath = null;
6
+ for(var i in scripts){
7
+ if((scripts[i].getAttribute)&&(scripts[i].getAttribute('src').match("socialCheesecake.js"))){
8
+ rootPath = scripts[i].getAttribute('src').replace("socialCheesecake.js", "");
9
+ }else if((scripts[i].getAttribute)&&(scripts[i].getAttribute('src').match("application.js"))){
10
+ //In case you are using rails, a compiled application.js is required instead of socialCheesecake.js
11
+ rootPath = scripts[i].getAttribute('src').replace("application.js", "");
12
+ }
13
+ }
14
+ if((rootPath)||(rootPath=="")){
15
+ rootPath = rootPath.replace(/\s/g, "");
16
+ if(rootPath==""){
17
+ var href = document.location.href;
18
+ rootPath = href.slice(0, href.lastIndexOf("/")+1);
19
+ }
20
+ }
21
+ socialCheesecake = {
22
+ modules : {},
23
+ currentModule : undefined,
24
+ moduleQueue : [],
25
+ rootPath : rootPath || 'static/',
26
+ log: function(message, level){
27
+ var now = new Date();
28
+ var timestamp = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds() + "," + now.getMilliseconds();
29
+ if(level!=undefined){
30
+ level=level.toUpperCase() + ": ";
31
+ }else{
32
+ level="";
33
+ }
34
+ console.log("#" + timestamp + " >> " + level + message)
35
+ },
36
+ defineModule : function(name) {
37
+ if(socialCheesecake.modules[name] && socialCheesecake.modules[name].isDefined()) {
38
+ socialCheesecake.log("You are redefining module '" + name + "'. This may break the game.", "warning");
39
+ }
40
+ socialCheesecake.currentModule = new socialCheesecake.Module(name);
41
+ socialCheesecake.modules[name] = socialCheesecake.currentModule;
42
+ socialCheesecake.moduleQueue.push(socialCheesecake.currentModule);
43
+ return socialCheesecake;
44
+ },
45
+ dependsOn : function() {
46
+ socialCheesecake.currentModule.dependencies = [].splice.call(arguments, 0);
47
+ if(socialCheesecake.currentModule.isDefined()) {
48
+ socialCheesecake._activateModules();
49
+ } else {
50
+ return socialCheesecake;
51
+ }
52
+ },
53
+ withCode : function(code) {
54
+ socialCheesecake.currentModule.code = code;
55
+ if(socialCheesecake.currentModule.isDefined()) {
56
+ socialCheesecake._activateModules();
57
+ } else {
58
+ return socialCheesecake;
59
+ }
60
+ },
61
+ _downloadModule : function(name, origin) {
62
+ if(socialCheesecake.modules[name] == undefined) {
63
+ socialCheesecake.modules[name] = new socialCheesecake.Module(name);
64
+ }
65
+
66
+ var scriptTag = document.createElement("script");
67
+ scriptTag.type = 'text/javascript';
68
+ scriptTag.src = socialCheesecake.rootPath + name.toLowerCase().split("#").join("/") + '.js';
69
+ scriptTag.onload = function() {
70
+ socialCheesecake.log(name + " module downloaded.")
71
+ socialCheesecake._activateModules();
72
+ };
73
+ scriptTag.onerror = function() {
74
+ throw ('Module ' + name + ' (' + scriptTag.src + ') required from ' + origin + ' failed to load.');
75
+ };
76
+ document.getElementsByTagName("head")[0].appendChild(scriptTag);
77
+ },
78
+ _activateModules : function() {
79
+ var modsReady = false;
80
+ for(var i in socialCheesecake.moduleQueue) {
81
+ var mod = socialCheesecake.moduleQueue[i];
82
+ var depsReady = true;
83
+
84
+ for(var j in mod.dependencies) {
85
+ var depName = mod.dependencies[j];
86
+ if(!socialCheesecake.modules[depName]) {
87
+ depsReady = false;
88
+ socialCheesecake._downloadModule(depName, mod.name);
89
+ } else if(!socialCheesecake.modules[depName].ready) {
90
+ depsReady = false;
91
+ }
92
+ }
93
+
94
+ if(depsReady && mod.isDefined()) {
95
+ socialCheesecake.moduleQueue.splice(i, 1);
96
+ mod.ready = true;
97
+ mod.execute();
98
+ modsReady = true;
99
+ i--;
100
+ }
101
+ }
102
+
103
+ if(modsReady) {
104
+ socialCheesecake._activateModules();
105
+ }
106
+
107
+ // Check everything correct
108
+ }
109
+ }
110
+
111
+ /*************************************************************************************************
112
+ * SocialCheesecake Module prototype
113
+ *************************************************************************************************/
114
+ socialCheesecake.Module = function(name) {
115
+ this.name = name;
116
+ this.dependencies = [];
117
+ this.code = undefined;
118
+ this.ready = false;
119
+ }
120
+ socialCheesecake.Module.prototype.isDefined = function() {
121
+ return this.code != undefined;
122
+ }
123
+ socialCheesecake.Module.prototype.execute = function() {
124
+ if(this.isDefined()){
125
+ socialCheesecake.log(this.name + " module executed.");
126
+ return this.code();
127
+ }else{
128
+ throw(this.name + " module is undefined and can't be executed.'");
129
+ }
130
+ }
131
+ })();
132
+
133
+ socialCheesecake.defineModule(
134
+ 'SocialCheesecake#SocialCheesecake'
135
+ )
136
+ .dependsOn(
137
+ 'SocialCheesecake#Actor',
138
+ 'SocialCheesecake#Cheesecake',
139
+ 'SocialCheesecake#Grid',
140
+ 'SocialCheesecake#Sector',
141
+ 'SocialCheesecake#Text'
142
+ )
143
+ .withCode(function() {
144
+
145
+ });
146
+
147
+ /*
148
+ socialCheesecake.defineModule(
149
+ 'SocialCheesecake#X'
150
+ )
151
+ .dependsOn(
152
+
153
+ )
154
+ .withCode(function() {
155
+
156
+ });
157
+ */
158
+
159
+
160
+
161
+
@@ -0,0 +1,3 @@
1
+ //= require kinetic
2
+ //= require socialCheesecake
3
+