headbreaker icon indicating copy to clipboard operation
headbreaker copied to clipboard

Keep the solved pieces intact when dragging

Open hapiben opened this issue 2 years ago • 2 comments

Hi, I was wondering if there's a way we can lock the pieces when dragging them?

For example, I have three pieces of the puzzle solved. Dragging one of the pieces will be disconnected from the other two.

This doesn't happen when you drag a piece with solved pieces around it.

hapiben avatar Sep 22 '22 04:09 hapiben

Hi @hapiben

There is currently no manual lock mode. When pieces are connected, they will work a single unit as long as they are dragged in a direction that is not open, that is, there is no open connector. Otherwise they will get disconnected. As a consequence, user can control whether the puzzle remains locked or not depending on their gestures, instead of a shortcut or another configuration.

This is behavior is quite core to this library and won't probably change.

However it might be augmented by introducing something some kind of keyboard action, like pressing the shift key. Theoretically, the following method...

  drag(dx, dy, quiet = false) {
    if (Pair.isNull(dx, dy)) return;

    if (this.horizontallyOpenMovement(dx) && this.vericallyOpenMovement(dy)) { // here
      this.disconnect();
      this.translate(dx, dy, quiet);
    } else {
      this.push(dx, dy, quiet);
    }
  }

...could be modified to add an additional condition that forces locking. Perhaps something like this:

  drag(dx, dy, quiet = false) {
    //...

    if (!this.puzzle.locked && this.horizontallyOpenMovement(dx) && this.vericallyOpenMovement(dy)) { // here
      this.disconnect();
      this.translate(dx, dy, quiet);
    } else { //...
    }
  }

And then add a lock()/unlock() method pair to Puzzle, which you may control from UI.

flbulgarelli avatar Oct 07 '22 16:10 flbulgarelli

I have left some initial thoughts here: https://github.com/flbulgarelli/headbreaker/tree/experiment-puzzle-locking, but they will require more work. If you think this is what you need, feel free to contribute :smile:

flbulgarelli avatar Oct 07 '22 16:10 flbulgarelli

Hi @hapiben I am closing this for now. Feel free to reopen it if you have any other question with this topic.

flbulgarelli avatar Nov 11 '22 00:11 flbulgarelli