Class: Puzzle

Puzzle(optionsopt)

A set of a Pieces that can be manipulated as a whole, and that can be used as a pieces factory

Constructor

new Puzzle(optionsopt)

Parameters:
Name Type Attributes Description
options Settings <optional>
Source:

Members

connected :boolean

Wether all the pieces in this puzzle are connected
Type:
  • boolean
Source:

dragMode :DragMode

Type:
Source:
Returns the first piece
Type:
Source:

headAnchor :Anchor

Returns the central anchor of the first piece
Type:
Source:

horizontalRequirement

Returns the attached horizontal ConnectionRequirement function.
Source:

metadata :Array.<any>

Type:
  • Array.<any>
Source:

pieceDiameter :Vector

The piece width, from edge to edge. This is the double of the Puzzle#pieceRadius
Type:
Source:

pieceRadius :Vector

The piece width, from center to edge
Type:
Source:

pieces :Array.<Piece>

Type:
Source:

points :Array.<Pair>

Answers the list of points where central anchors of pieces are located
Type:
  • Array.<Pair>
Source:

refs :Array.<Pair>

Answers a list of points whose coordinates are scaled to the Puzzle#pieceWidth
Type:
  • Array.<Pair>
Source:

valid :boolean

Returns the current validation status Calling this property will not fire any validation listeners.
Type:
  • boolean
Source:

validator :Validator

Type:
Source:

verticalRequirement

Returns the attached vertical ConnectionRequirement function.
Source:

Methods

(static) import(dump) → {Puzzle}

Parameters:
Name Type Description
dump PuzzleDump
Source:
Returns:
Type
Puzzle

addPiece(piece)

Parameters:
Name Type Description
piece Piece
Source:

addPieces(pieces)

Parameters:
Name Type Description
pieces Array.<Piece>
Source:

annotate(metadata)

Annotates all the pieces with the given list of metadata
Parameters:
Name Type Description
metadata Array.<object>
Source:

attachConnectionRequirement(requirement)

Attaches the given connection requirement as both a vertical and horizontal requirement.
Parameters:
Name Type Description
requirement ConnectionRequirement
Source:
See:

attachHorizontalConnectionRequirement(requirement)

Attaches a connection requirement function that will be used to check whether two horizontally close and matching pieces can be actually connected. By default no horizontal connection requirement is imposed which means that any horizontally close and matching pieces will be connected.
Parameters:
Name Type Description
requirement ConnectionRequirement
Source:

attachValidator(validator)

Parameters:
Name Type Description
validator Validator
Source:

attachVerticalConnectionRequirement(requirement)

Attaches a connection requirement function that will be used to check whether two vertically close and matching pieces can be actually connected. By default no vertical connection requirement is imposed which means that any vertically close and matching pieces will be connected.
Parameters:
Name Type Description
requirement ConnectionRequirement
Source:

autoconnect()

Tries to connect pieces in their current positions This method is O(n^2)
Source:

autoconnectWith(piece)

Tries to connect the given piece to the rest of the set This method is O(n)
Parameters:
Name Type Description
piece Piece
Source:

clearConnectionRequirements()

Removes the vertical and horizontal connection requirements, if any.
Source:

disconnect()

Disconnects all pieces
Source:

dragShouldDisconnect(piece, dx, dy)

Parameters:
Name Type Description
piece Piece
dx number
dy number
Source:
See:

export(options) → {PuzzleDump}

Converts this piece into a plain, stringify-ready object. Pieces should have ids
Parameters:
Name Type Description
options object config options for export
Properties
Name Type Attributes Description
compact boolean <optional>
if connection information must be omitted
Source:
Returns:
Type
PuzzleDump

forceConnectionWhileDragging()

Prevents pieces from disconnecting
Source:

forceDisconnectionWhileDragging()

Forces pieces to disconnect
Source:

isValid() → {boolean}

Checks whether this puzzle is valid. Calling this method will not fire any validation listeners nor update the valid property.
Source:
Returns:
Type
boolean

newPiece(structureopt, configopt) → {Piece}

Creates and adds to this puzzle a new piece
Parameters:
Name Type Attributes Description
structure Structure <optional>
the piece structure
config PieceConfig <optional>
the piece config
Source:
Returns:
the new piece
Type
Piece

onConnect(f)

Parameters:
Name Type Description
f ConnectionListener
Source:

onDisconnect(f)

Parameters:
Name Type Description
f ConnectionListener
Source:

onTranslate(f)

Parameters:
Name Type Description
f TranslationListener
Source:

onValid(f)

Parameters:
Name Type Description
f ValidationListener
Source:

reframe(min, max)

Translates all the puzzle pieces so that are completely within the given bounds, if possible. If pieces can not be completly places within the given bounding box, the the `max` param is ignored.
Parameters:
Name Type Description
min Vector
max Vector
Source:

relocateTo(points)

Relocates all the pieces to the given list of points
Parameters:
Name Type Description
points Array.<Pair>
Source:

shuffle(maxX, maxY)

Parameters:
Name Type Description
maxX number
maxY number
Source:

shuffleWith(shuffler)

Parameters:
Name Type Description
shuffler Shuffler
Source:

translate(dx, dy)

Parameters:
Name Type Description
dx number
dy number
Source:

tryDisconnectionWhileDragging()

Forces pieces to disconnect
Source:

updateValidity()

Checks whether this puzzle is valid, updating valid property. Validations listeners are NOT fired.
Source:

validate()

Checks whether this puzzle is valid, updating valid property and firing validation listeners if becomes valid
Source: