Commits

Anonymous committed ef9b405

Better tile selection.

Comments (0)

Files changed (1)

javascript/tile-stack.js

     var timer = 0;
     var selTileX = undefined;
     var selTileY = undefined;
+    var selTileZ = undefined;
 
     var colors = ["red", "yellow", "green", "blue"];
 
+    this.tileContainsPoint = function(tileX, tileY, tileZ, ptX, ptY) {
+        var xLeft = tileX * w - (depth * tileZ);
+        var yTop = tileY * h - (depth * tileZ);
+        return ptX > xLeft && ptX < xLeft + w && ptY > yTop && ptY < yTop + h;
+    }
+
     this.drawTile = function(tileX, tileY, tileZ, color) {
         ctx.beginPath();
         var xLeft = tileX * w - (depth * tileZ);
         p.foreach(function (x, y, value) {
             for (var i = 0; i < value.length; i++) {
                 var c = value[i];
-                if (selTileX === x && selTileY === y) {
+                if (selTileX === x && selTileY === y && selTileZ === i) {
                     c = "purple";
                 }
                 self.drawTile(x, y, i, c);
         p = new Playfield();
         canvas = c;
         ctx = canvas.getContext('2d');
+        var self = this;
 
         canvas.onmousedown = function(e) {
-          var can_x = e.pageX - canvas.offsetLeft;
-          var can_y = e.pageY - canvas.offsetTop;
-          
-          selTileX = Math.floor(can_x / w);
-          selTileY = Math.floor(can_y / h);
-          self.drawTile(tileX, tileY, 0, "purple");
+            var can_x = e.pageX - canvas.offsetLeft;
+            var can_y = e.pageY - canvas.offsetTop;
+
+            p.foreach(function (x, y, value) {
+                var z = value.length - 1;
+                if (self.tileContainsPoint(x, y, z, can_x, can_y)) {
+                    if (selTileZ === undefined || z > selTileZ) {
+                        selTileX = x;
+                        selTileY = y;
+                        selTileZ = z;
+                    }
+                }
+            });
         }
         canvas.onmouseup = function() {
-          selTileX = selTileY = undefined;
+            selTileX = selTileY = selTileZ = undefined;
         };
 
-        var self = this;
         intervalId = setInterval(function() { self.draw(); }, 33);
     };
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.