Constructor
new Puzzle(optionsopt)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options |
Settings |
<optional> |
Members
connected :boolean
Wether all the pieces in this puzzle are connected
Type:
- boolean
dragMode :DragMode
Type:
head :Piece
Returns the first piece
Type:
headAnchor :Anchor
Returns the central anchor of the first piece
Type:
horizontalRequirement
Returns the attached horizontal ConnectionRequirement
function.
metadata :Array.<any>
Type:
- Array.<any>
pieceDiameter :Vector
The piece width, from edge to edge.
This is the double of the Puzzle#pieceRadius
Type:
pieceRadius :Vector
The piece width, from center to edge
Type:
pieces :Array.<Piece>
Type:
- Array.<Piece>
points :Array.<Pair>
Answers the list of points where
central anchors of pieces are located
Type:
- Array.<Pair>
refs :Array.<Pair>
Answers a list of points whose coordinates are scaled
to the Puzzle#pieceWidth
Type:
- Array.<Pair>
valid :boolean
Returns the current validation status
Calling this property will not fire any validation listeners.
Type:
- boolean
validator :Validator
Type:
verticalRequirement
Returns the attached vertical ConnectionRequirement
function.
Methods
(static) import(dump) → {Puzzle}
Parameters:
Name | Type | Description |
---|---|---|
dump |
PuzzleDump |
Returns:
- Type
- Puzzle
addPiece(piece)
Parameters:
Name | Type | Description |
---|---|---|
piece |
Piece |
addPieces(pieces)
Parameters:
Name | Type | Description |
---|---|---|
pieces |
Array.<Piece> |
annotate(metadata)
Annotates all the pieces with the given list of metadata
Parameters:
Name | Type | Description |
---|---|---|
metadata |
Array.<object> |
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 |
attachValidator(validator)
Parameters:
Name | Type | Description |
---|---|---|
validator |
Validator |
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 |
autoconnect()
Tries to connect pieces in their current positions
This method is O(n^2)
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 |
clearConnectionRequirements()
Removes the vertical and horizontal connection requirements, if any.
disconnect()
Disconnects all pieces
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
|
Returns:
- Type
- PuzzleDump
forceConnectionWhileDragging()
Prevents pieces from disconnecting
forceDisconnectionWhileDragging()
Forces pieces to disconnect
isValid() → {boolean}
Checks whether this puzzle is valid.
Calling this method will not fire any validation listeners nor update the
valid property.
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 |
Returns:
the new piece
- Type
- Piece
onConnect(f)
Parameters:
Name | Type | Description |
---|---|---|
f |
ConnectionListener |
onDisconnect(f)
Parameters:
Name | Type | Description |
---|---|---|
f |
ConnectionListener |
onTranslate(f)
Parameters:
Name | Type | Description |
---|---|---|
f |
TranslationListener |
onValid(f)
Parameters:
Name | Type | Description |
---|---|---|
f |
ValidationListener |
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 |
relocateTo(points)
Relocates all the pieces to the given list of points
Parameters:
Name | Type | Description |
---|---|---|
points |
Array.<Pair> |
shuffle(maxX, maxY)
Parameters:
Name | Type | Description |
---|---|---|
maxX |
number | |
maxY |
number |
shuffleWith(shuffler)
Parameters:
Name | Type | Description |
---|---|---|
shuffler |
Shuffler |
translate(dx, dy)
Parameters:
Name | Type | Description |
---|---|---|
dx |
number | |
dy |
number |
tryDisconnectionWhileDragging()
Forces pieces to disconnect
updateValidity()
Checks whether this puzzle is valid, updating valid property.
Validations listeners are NOT fired.
validate()
Checks whether this puzzle is valid, updating valid property
and firing validation listeners if becomes valid