Просмотр исходного кода

Pixel Studio: fix text hit-testing and lock toast

Move tool hit-testing now treats a text layer as hit anywhere inside its content bounds, so clicking in the gaps between letters no longer falls through to the background.

Selecting a position-locked layer with a plain click no longer shows the "Layer position is locked" toast. The toast now appears only once the pointer is dragged more than 3 screen pixels.
Toby Chui 3 недель назад
Родитель
Сommit
c05e1d46e7
1 измененных файлов с 20 добавлено и 3 удалено
  1. 20 3
      src/web/Pixel Studio/js/tools.js

+ 20 - 3
src/web/Pixel Studio/js/tools.js

@@ -724,7 +724,9 @@ PS.paintCursorOverlay = function (size, square) {
 
 
     function centreOf(b) { return { x: b.x + b.w / 2, y: b.y + b.h / 2 }; }
     function centreOf(b) { return { x: b.x + b.w / 2, y: b.y + b.h / 2 }; }
 
 
-    // visible layers with a pixel under pt, topmost first
+    // visible layers under pt, topmost first: a layer is hit by a pixel of
+    // its own, a text layer anywhere inside its box (the gaps between the
+    // letters count, so clicking a word never falls through to the background)
     PS.layersAtPoint = function (pt) {
     PS.layersAtPoint = function (pt) {
         var x = Math.floor(pt.x), y = Math.floor(pt.y);
         var x = Math.floor(pt.x), y = Math.floor(pt.y);
         if (!PS.doc || x < 0 || y < 0 || x >= PS.doc.width || y >= PS.doc.height) { return []; }
         if (!PS.doc || x < 0 || y < 0 || x >= PS.doc.width || y >= PS.doc.height) { return []; }
@@ -732,12 +734,17 @@ PS.paintCursorOverlay = function (size, square) {
         PS.layerDisplayList(true).forEach(function (row) {
         PS.layerDisplayList(true).forEach(function (row) {
             var l = row.layer;
             var l = row.layer;
             if (!l.canvas || !PS.isEffectivelyVisible(l)) { return; }
             if (!l.canvas || !PS.isEffectivelyVisible(l)) { return; }
+            if (l.kind === "text") {
+                var b = PS.layerContentBounds(l);
+                if (b && pt.x >= b.x && pt.x < b.x + b.w && pt.y >= b.y && pt.y < b.y + b.h) { out.push(l); }
+                return;
+            }
             if (l.canvas.getContext("2d").getImageData(x, y, 1, 1).data[3] > 16) { out.push(l); }
             if (l.canvas.getContext("2d").getImageData(x, y, 1, 1).data[3] > 16) { out.push(l); }
         });
         });
         return out;
         return out;
     };
     };
 
 
-    // Auto-Select: the topmost visible layer with a pixel under the pointer
+    // Auto-Select: the topmost visible layer under the pointer
     PS.layerAtPoint = function (pt, wantGroup) {
     PS.layerAtPoint = function (pt, wantGroup) {
         var l = PS.layersAtPoint(pt)[0];
         var l = PS.layersAtPoint(pt)[0];
         if (!l) { return null; }
         if (!l) { return null; }
@@ -947,7 +954,9 @@ PS.paintCursorOverlay = function (size, square) {
                     return !l.locks.position && !PS.ancestorsOf(l).some(function (a) { return a.locks.position; });
                     return !l.locks.position && !PS.ancestorsOf(l).some(function (a) { return a.locks.position; });
                 };
                 };
                 if (!PS.withLinkedLayers(PS.topLevelSelected()).filter(movable).length) {
                 if (!PS.withLinkedLayers(PS.topLevelSelected()).filter(movable).length) {
-                    PS.toast("Layer position is locked", true);
+                    // a plain click (e.g. picking the Background) says nothing;
+                    // only an actual drag warns that the layer cannot move
+                    drag = { mode: "locked", start: pt, warned: false };
                     return;
                     return;
                 }
                 }
                 // Alt-drag moves a copy
                 // Alt-drag moves a copy
@@ -993,6 +1002,13 @@ PS.paintCursorOverlay = function (size, square) {
         },
         },
         onMove: function (pt, e) {
         onMove: function (pt, e) {
             if (!drag) { PS.moveHoverUpdate(pt, e); return; }
             if (!drag) { PS.moveHoverUpdate(pt, e); return; }
+            if (drag.mode === "locked") {
+                if (!drag.warned && Math.hypot(pt.x - drag.start.x, pt.y - drag.start.y) * PS.zoom > 3) {
+                    drag.warned = true;
+                    PS.toast("Layer position is locked", true);
+                }
+                return;
+            }
             var mdx = pt.x - drag.start.x, mdy = pt.y - drag.start.y;
             var mdx = pt.x - drag.start.x, mdy = pt.y - drag.start.y;
             if (e && e.shiftKey) {
             if (e && e.shiftKey) {
                 // Shift constrains to horizontal / vertical
                 // Shift constrains to horizontal / vertical
@@ -1015,6 +1031,7 @@ PS.paintCursorOverlay = function (size, square) {
         },
         },
         onUp: function () {
         onUp: function () {
             if (!drag) { return; }
             if (!drag) { return; }
+            if (drag.mode === "locked") { drag = null; return; }
             if (drag.mode === "layers") {
             if (drag.mode === "layers") {
                 PS.movePreview = null;
                 PS.movePreview = null;
                 if (drag.dx || drag.dy) { PS.translateLayers(drag.layers, drag.dx, drag.dy, "Move"); }
                 if (drag.dx || drag.dy) { PS.translateLayers(drag.layers, drag.dx, drag.dy, "Move"); }