mumuki-puzzle-runner 0.1.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,14 +28,14 @@ declare class Anchor {
28
28
  /**
29
29
  * Converts this anchor into a point
30
30
  */
31
- asPoint(): Pair;
31
+ asPair(): Pair;
32
32
  /**
33
- * Converts this anchor into a position
33
+ * Converts this anchor into a vector
34
34
  */
35
- asPosition(): Position;
36
- export(): Position;
35
+ asVector(): Vector;
36
+ export(): Vector;
37
37
  static atRandom(maxX: number, maxY: number): Anchor;
38
- static import(position: Position): Anchor;
38
+ static import(vector: Vector): Anchor;
39
39
  }
40
40
 
41
41
  /**
@@ -43,6 +43,8 @@ declare class Anchor {
43
43
  */
44
44
  declare function anchor(x: number, y: number): Anchor;
45
45
 
46
+ declare type Axis = Vertical | Horizontal;
47
+
46
48
  declare type Label = any;
47
49
 
48
50
  declare type Figure = {
@@ -76,9 +78,10 @@ declare type LabelMetadata = {
76
78
 
77
79
  declare type CanvasMetadata = {
78
80
  id?: string;
79
- targetPosition?: Position;
80
- currentPosition?: Position;
81
+ targetPosition?: Vector;
82
+ currentPosition?: Vector;
81
83
  color?: string;
84
+ fixed?: boolean;
82
85
  strokeColor?: string;
83
86
  image?: ImageLike;
84
87
  label?: LabelMetadata;
@@ -86,6 +89,7 @@ declare type CanvasMetadata = {
86
89
 
87
90
  declare type Template = {
88
91
  structure: StructureLike;
92
+ size?: Size;
89
93
  metadata: CanvasMetadata;
90
94
  };
91
95
 
@@ -93,25 +97,38 @@ declare type Template = {
93
97
  * An HTML graphical area where puzzles and pieces can be rendered. No assumption of the rendering backend is done - it may be
94
98
  * and be a plain HTML SVG or canvas element, or a higher-level library - and this task is fully delegated to {@link Painter}
95
99
  * @param id - the html id of the element where to place the canvas
100
+ * @param [options.pieceSize] - the piece size expresed as it edge-to-edge diameter
96
101
  * @param [options.borderFill] - the broder fill of the pieces, expresed in pixels. 0 means no border fill, 0.5 * pieceSize means full fill
97
102
  * @param [options.lineSoftness] - how soft the line will be
98
103
  * @param [options.image] - an optional background image for the puzzle that will be split across all pieces.
104
+ * @param [options.fixed] - whether the canvas can is fixed or can be dragged
99
105
  * @param [options.painter] - the Painter object used to actually draw figures in canvas
106
+ * @param [options.puzzleDiameter] - the puzzle diameter used to calculate the maximal width and height
107
+ * You only need to specify this option when pieces are manually sketched and images must be adjusted
108
+ * @param [options.maxPiecesCount] - the maximal amount of pieces used to calculate the maximal width and height.
109
+ * You only need to specify this option when pieces are manually sketched and images must be adjusted
100
110
  */
101
111
  declare class Canvas {
102
112
  constructor(id: string, options: {
103
113
  width: number;
104
114
  height: number;
105
- pieceSize?: number;
115
+ pieceSize?: Vector | number;
106
116
  proximity?: number;
107
- borderFill?: number;
117
+ borderFill?: Vector | number;
108
118
  strokeWidth?: number;
109
119
  strokeColor?: string;
110
120
  lineSoftness?: number;
111
121
  image?: ImageLike;
122
+ fixed?: boolean;
112
123
  painter?: Painter;
124
+ puzzleDiameter?: Vector | number;
125
+ maxPiecesCount?: Vector | number;
126
+ outline?: Outline;
113
127
  });
114
128
  _painter: Painter;
129
+ _maxPiecesCount: Vector;
130
+ _puzzleDiameter: Vector;
131
+ _imageAdjuster: any;
115
132
  _puzzle: Puzzle;
116
133
  figures: {
117
134
  [key: string]: Figure;
@@ -133,7 +150,7 @@ declare class Canvas {
133
150
  renderPieces(pieces: Piece[]): void;
134
151
  /**
135
152
  * Renders a previously created puzzle object. This method
136
- * overrides this canvas' {@link Canvas#pieceSize} and {@link Canvas#proximity}
153
+ * overrides this canvas' {@link Canvas#pieceDiameter} and {@link Canvas#proximity}
137
154
  */
138
155
  renderPuzzle(puzzle: Puzzle): void;
139
156
  /**
@@ -157,9 +174,17 @@ declare class Canvas {
157
174
  */
158
175
  sketchPieceUsingTemplate(id: string, templateName: string): void;
159
176
  /**
160
- * @param farness - from 0 to 1, how far pieces will be placed from x = pieceSize, y = pieceSize
177
+ * @param farness - from 0 to 1, how far pieces will be placed from x = pieceDiameter.x, y = pieceDiameter.y
161
178
  */
162
179
  shuffle(farness?: number): void;
180
+ /**
181
+ * **Warning**: this method requires {@code maxPiecesCount} to be set.
182
+ */
183
+ shuffleColumns(farness?: number): void;
184
+ /**
185
+ * **Warning**: this method requires {@code maxPiecesCount} to be set.
186
+ */
187
+ shuffleGrid(farness?: number): void;
163
188
  /**
164
189
  * Draws this canvas for the first time
165
190
  */
@@ -169,6 +194,13 @@ declare class Canvas {
169
194
  * being modified and you need changes to become visible
170
195
  */
171
196
  redraw(): void;
197
+ /**
198
+ * Refreshes image metadata.
199
+ *
200
+ * Use this method in order adjuster updates and image changes after initial draw
201
+ * to make effect.
202
+ */
203
+ refill(): void;
172
204
  /**
173
205
  * Clears the canvas, clearing the rendering backend and discarding all the created templates, figures, and pieces
174
206
  */
@@ -222,6 +254,14 @@ declare class Canvas {
222
254
  * Answers the visual representation for the given piece id.
223
255
  */
224
256
  getFigureById(id: string): Figure;
257
+ /**
258
+ * Sets the new width and height of the canvas
259
+ */
260
+ resize(width: number, height: number): void;
261
+ /**
262
+ * Scales the canvas contents to the given factor
263
+ */
264
+ scale(factor: Vector | number): void;
225
265
  _annotatePiecePosition(piece: Piece): void;
226
266
  /**
227
267
  * Configures updates from piece into group
@@ -231,8 +271,59 @@ declare class Canvas {
231
271
  * * Configures updates from group into piece
232
272
  */
233
273
  _bindPieceToGroup(piece: Piece, group: Group): void;
234
- _imageMetadataFor(model: Piece): ImageMetadata;
235
- _newPiece(structureLike: StructureLike, metadata: CanvasMetadata): void;
274
+ _baseImageMetadataFor(piece: Piece): ImageMetadata;
275
+ imageMetadataFor(piece: Piece): ImageMetadata;
276
+ /**
277
+ * Configures canvas to adjust images axis to puzzle's axis.
278
+ *
279
+ * **Warning**: this method requires {@code maxPiecesCount} or {@code puzzleDiameter} to be set.
280
+ */
281
+ adjustImagesToPuzzle(axis: Axis): void;
282
+ /**
283
+ * Configures canvas to adjust images width to puzzle's width
284
+ *
285
+ * **Warning**: this method requires {@code maxPiecesCount} or {@code puzzleDiameter} to be set.
286
+ */
287
+ adjustImagesToPuzzleWidth(): void;
288
+ /**
289
+ * Configures canvas to adjust images height to puzzle's height
290
+ *
291
+ * **Warning**: this method requires {@code maxPiecesCount} or {@code puzzleDiameter} to be set.
292
+ */
293
+ adjustImagesToPuzzleHeight(): void;
294
+ /**
295
+ * Configures canvas to adjust images axis to pieces's axis
296
+ *
297
+ * **Warning**: this method requires {@code maxPiecesCount} or {@code puzzleDiameter} to be set.
298
+ */
299
+ adjustImagesToPiece(axis: Axis): void;
300
+ /**
301
+ * Configures canvas to adjust images width to pieces's width
302
+ *
303
+ * **Warning**: this method requires {@code maxPiecesCount} or {@code puzzleDiameter} to be set.
304
+ */
305
+ adjustImagesToPieceWidth(): void;
306
+ /**
307
+ * Configures canvas to adjust images height to pieces's height
308
+ *
309
+ * **Warning**: this method requires {@code maxPiecesCount} or {@code puzzleDiameter} to be set.
310
+ */
311
+ adjustImagesToPieceHeight(): void;
312
+ /**
313
+ * @param structureLike - the piece structure
314
+ */
315
+ _newPiece(structureLike: StructureLike, size: Size, metadata: CanvasMetadata): void;
316
+ /**
317
+ * The puzzle diameter, using the
318
+ * configured puzzle diameter or the estimated one, if the first is not available.
319
+ */
320
+ puzzleDiameter: Vector;
321
+ /**
322
+ * The estimated puzzle diameter calculated using the the max pieces count.
323
+ */
324
+ estimatedPuzzleDiameter: Vector;
325
+ pieceRadio: Vector;
326
+ pieceDiameter: Vector;
236
327
  /**
237
328
  * The puzzle rendered by this canvas
238
329
  */
@@ -249,12 +340,12 @@ declare interface DummyPainter extends Painter {
249
340
  declare class DummyPainter implements Painter {
250
341
  initialize(canvas: Canvas, id: string): void;
251
342
  draw(canvas: Canvas): void;
252
- sketch(canvas: Canvas, _piece: Piece, _figure: Figure): void;
343
+ sketch(canvas: Canvas, _piece: Piece, _figure: Figure, outline: Outline): void;
253
344
  }
254
345
 
255
346
  declare type ImageMetadata = {
256
347
  content: HTMLImageElement;
257
- offset?: Position;
348
+ offset?: Vector;
258
349
  scale?: number;
259
350
  };
260
351
 
@@ -284,7 +375,10 @@ declare class KonvaPainter implements Painter {
284
375
  initialize(canvas: Canvas, id: string): void;
285
376
  draw(canvas: Canvas): void;
286
377
  reinitialize(canvas: Canvas): void;
287
- sketch(canvas: Canvas, piece: Piece, figure: Figure): void;
378
+ resize(canvas: Canvas, width: number, height: number): void;
379
+ scale(canvas: Canvas, factor: Vector): void;
380
+ sketch(canvas: Canvas, piece: Piece, figure: Figure, outline: Outline): void;
381
+ fill(canvas: Canvas, piece: Piece, figure: Figure): void;
288
382
  label(_canvas: Canvas, piece: Piece, figure: Figure): void;
289
383
  physicalTranslate(_canvas: Canvas, group: Group, piece: Piece): void;
290
384
  logicalTranslate(_canvas: Canvas, piece: Piece, group: any): void;
@@ -326,6 +420,8 @@ declare class Manufacturer {
326
420
 
327
421
  declare class Positioner {
328
422
  constructor(puzzle: Puzzle, headAnchor: Anchor);
423
+ initializeOffset(headAnchor: Anchor): void;
424
+ offset: Vector;
329
425
  naturalAnchor(x: number, y: number): void;
330
426
  }
331
427
 
@@ -339,12 +435,13 @@ declare module "Metadata" {
339
435
  function copy(metadata: T): T;
340
436
  }
341
437
 
438
+ declare type Outline = Squared | Rounded;
439
+
342
440
  /**
343
441
  * This module contains the draw function. Override it change pieces drawing strategy
344
442
  */
345
443
  declare module "Outline" {
346
- function select(insert: Insert, t: number, s: number, n: number): void;
347
- function draw(piece: Piece, size?: number): number[];
444
+ function select(insert: Insert, t: T, s: T, n: T): T;
348
445
  }
349
446
 
350
447
  declare type VectorAction = (dx: number, dy: number) => void;
@@ -356,6 +453,7 @@ declare type Action = () => void;
356
453
  * order to create UI representations of a puzzle.
357
454
  */
358
455
  declare interface Painter {
456
+ resize(canvas: Canvas, width: number, height: number): void;
359
457
  /**
360
458
  * Creates the rendering backend, initializig all its contents.
361
459
  * After this call, painter is ready to receive any other messages
@@ -371,11 +469,20 @@ declare interface Painter {
371
469
  * Draws the canvas figures in the rendering backend
372
470
  */
373
471
  draw(canvas: Canvas): void;
472
+ /**
473
+ * Scales the canvas contents
474
+ */
475
+ scale(canvas: Canvas, factor: Vector): void;
374
476
  /**
375
477
  * Adds a piece to the rendering backend, so that it is ready to be drawn
376
478
  * @param figure - the rendering backend information for this piece. This method may mutate it if necessary
377
479
  */
378
- sketch(canvas: Canvas, piece: Piece, figure: Figure): void;
480
+ sketch(canvas: Canvas, piece: Piece, figure: Figure, outline: Outline): void;
481
+ /**
482
+ * Fills a piece using the canvas image information
483
+ * assigned for it
484
+ */
485
+ fill(canvas: Canvas, piece: Piece, figure: Figure): void;
379
486
  /**
380
487
  * Adds piece's label to the given figure in the rendering backend
381
488
  * @param figure - the rendering backend information for this piece. This method may mutate it if necessary
@@ -404,7 +511,10 @@ declare module "Pair" {
404
511
  * Tells whether this pair is (0, 0)
405
512
  */
406
513
  function isNull(x: number, y: number): boolean;
407
- function equal(x1: number, y1: number, x2: number, y2: number): boolean;
514
+ /**
515
+ * @param [delta = 0] - tolerance in comparison
516
+ */
517
+ function equal(x1: number, y1: number, x2: number, y2: number, delta?: number): boolean;
408
518
  /**
409
519
  * Calculates the difference of two vectors
410
520
  */
@@ -415,25 +525,39 @@ declare type TranslationListener = (piece: Piece, dx: number, dy: number) => voi
415
525
 
416
526
  declare type ConnectionListener = (piece: Piece, target: Piece) => void;
417
527
 
528
+ declare type PieceConfig = {
529
+ centralAnchor?: Vector;
530
+ size?: Size;
531
+ metadata?: any;
532
+ };
533
+
418
534
  /**
419
535
  * A piece primitive representation that can be easily stringified, exchanged and persisted
420
536
  */
421
537
  declare type PieceDump = {
422
- centralAnchor: Position;
423
- structure: string;
424
- connections?: Orthogonal<object>;
538
+ centralAnchor: Vector;
539
+ size?: Size;
425
540
  metadata: any;
541
+ connections?: Orthogonal<object>;
542
+ structure: string;
426
543
  };
427
544
 
428
545
  /**
429
546
  * A jigsaw piece
430
547
  */
431
548
  declare class Piece {
432
- constructor(options?: Structure);
549
+ constructor(structure?: Structure, config?: PieceConfig);
550
+ metadata: any;
433
551
  centralAnchor: Anchor;
552
+ _size: Size;
434
553
  translateListeners: TranslationListener[];
435
554
  connectListeners: ConnectionListener[];
436
555
  disconnectListeners: ConnectionListener[];
556
+ /**
557
+ * Runs positining, sizing and metadata configurations
558
+ * in a single step
559
+ */
560
+ configure(config: PieceConfig): void;
437
561
  /**
438
562
  * Adds unestructured user-defined metadata on this piece.
439
563
  */
@@ -523,7 +647,16 @@ declare class Piece {
523
647
  rightAnchor: Anchor;
524
648
  upAnchor: Anchor;
525
649
  leftAnchor: Anchor;
526
- size: number;
650
+ /**
651
+ * Defines this piece's own dimmension, overriding puzzle's
652
+ * default dimmension
653
+ */
654
+ resize(size: Size): void;
655
+ radio: Vector;
656
+ /**
657
+ * The double of the radio
658
+ */
659
+ diameter: Vector;
527
660
  proximity: number;
528
661
  /**
529
662
  * This piece id. It is extracted from metadata
@@ -543,37 +676,6 @@ declare class Piece {
543
676
  static import(dump: PieceDump): Piece;
544
677
  }
545
678
 
546
- declare type Position = {
547
- x: number;
548
- y: number;
549
- };
550
-
551
- declare function position(x: number, y: number): Position;
552
-
553
- /**
554
- * This module contains functions for dealing with objects with x and y
555
- * coordinates that represent or include point data
556
- */
557
- declare module "Position" {
558
- /**
559
- * Returns a new (0, 0) position
560
- */
561
- function origin(): Position;
562
- /**
563
- * Compares two points
564
- */
565
- function equal(one: Position, other: Position): boolean;
566
- /**
567
- * Creates a copy of the given point
568
- */
569
- function copy(one: Position): Position;
570
- function update(position: Position, x: any, y: any): void;
571
- /**
572
- * @returns ;
573
- */
574
- function diff(one: Position, other: Position): Pair;
575
- }
576
-
577
679
  declare type Orthogonal = {
578
680
  up: A;
579
681
  down: A;
@@ -597,13 +699,13 @@ declare module "Prelude" {
597
699
  * A puzzle primitive representation that can be easily stringified, exchanged and persisted
598
700
  */
599
701
  declare type PuzzleDump = {
600
- pieceSize: number;
702
+ pieceRadio: Vector;
601
703
  proximity: number;
602
704
  pieces: PieceDump[];
603
705
  };
604
706
 
605
707
  declare type Settings = {
606
- pieceSize?: number;
708
+ pieceRadio?: Vector | number;
607
709
  proximity?: number;
608
710
  };
609
711
 
@@ -617,10 +719,11 @@ declare class Puzzle {
617
719
  validator: Validator;
618
720
  /**
619
721
  * Creates and adds to this puzzle a new piece
620
- * @param [options] - the piece structure
722
+ * @param [structure] - the piece structure
723
+ * @param [config] - the piece config
621
724
  * @returns the new piece
622
725
  */
623
- newPiece(options?: Structure): Piece;
726
+ newPiece(structure?: Structure, config?: PieceConfig): Piece;
624
727
  addPiece(piece: Piece): void;
625
728
  addPieces(pieces: Piece[]): void;
626
729
  /**
@@ -646,6 +749,7 @@ declare class Puzzle {
646
749
  */
647
750
  autoconnectWith(piece: Piece): void;
648
751
  shuffle(maxX: number, maxY: number): void;
752
+ shuffleWith(shuffler: Shuffler): void;
649
753
  translate(dx: number, dy: number): void;
650
754
  onTranslate(f: TranslationListener): void;
651
755
  onConnect(f: ConnectionListener): void;
@@ -701,9 +805,13 @@ declare class Puzzle {
701
805
  connected: boolean;
702
806
  /**
703
807
  * The piece width, from edge to edge.
704
- * This is the double of the {@link Puzzle#pieceSize}
808
+ * This is the double of the {@link Puzzle#pieceRadio}
705
809
  */
706
- pieceWidth: number;
810
+ pieceDiameter: Vector;
811
+ /**
812
+ * The piece width, from center to edge
813
+ */
814
+ pieceRadio: Vector;
707
815
  /**
708
816
  * Converts this piece into a plain, stringify-ready object.
709
817
  * Pieces should have ids
@@ -758,9 +866,30 @@ declare module "sequence" {
758
866
  }
759
867
  }
760
868
 
869
+ declare function random(maxX: number, maxY: number): Shuffler;
870
+
871
+ declare function grid(): void;
872
+
873
+ declare function columns(): void;
874
+
875
+ declare function padder(padding: number, width: number, height: number): Shuffler;
876
+
877
+ declare function noise(maxDistance: Vector): Shuffler;
878
+
879
+ declare function noop(): void;
880
+
881
+ declare type Size = {
882
+ radio: Vector;
883
+ diameter: Vector;
884
+ };
885
+
886
+ declare function radio(value: Vector | number): Size;
887
+
888
+ declare function diameter(value: Vector | number): Size;
889
+
761
890
  declare type SpatialMetadata = {
762
- targetPosition?: Position;
763
- currentPosition?: Position;
891
+ targetPosition?: Vector;
892
+ currentPosition?: Vector;
764
893
  };
765
894
 
766
895
  /**
@@ -768,10 +897,11 @@ declare type SpatialMetadata = {
768
897
  * and pieces and puzzles that are annotated with it
769
898
  */
770
899
  declare module "SpatialMetadata" {
900
+ function diffToTarget(piece: Piece): void;
771
901
  function solved(): void;
772
902
  function relativePosition(): void;
773
903
  function absolutePosition(): void;
774
- function initialize(metadata: SpatialMetadata, target: Position, current?: Position): void;
904
+ function initialize(metadata: SpatialMetadata, target: Vector, current?: Vector): void;
775
905
  }
776
906
 
777
907
  declare type Structure = {
@@ -837,6 +967,14 @@ declare class PieceValidator {
837
967
  declare class PuzzleValidator {
838
968
  constructor(f: PuzzleCondition);
839
969
  isValid(puzzle: Puzzle): void;
970
+ /**
971
+ * Compares two pairs
972
+ */
973
+ static equalDiffs(param0: Pair, param1: Pair): boolean;
974
+ /**
975
+ * The delta used to compare distances
976
+ */
977
+ static DIFF_DELTA: number;
840
978
  static connected(): void;
841
979
  /**
842
980
  * @param expected - the expected relative refs
@@ -852,3 +990,43 @@ declare class NullValidator {
852
990
  isNull: boolean;
853
991
  }
854
992
 
993
+ declare type Vector = {
994
+ x: number;
995
+ y: number;
996
+ };
997
+
998
+ declare function vector(x: number, y: number): Vector;
999
+
1000
+ declare function cast(value: Vector | number): Vector;
1001
+
1002
+ /**
1003
+ * This module contains functions for dealing with objects with x and y
1004
+ * coordinates that represent or include point data
1005
+ */
1006
+ declare module "Vector" {
1007
+ /**
1008
+ * Returns a new (0, 0) vector
1009
+ */
1010
+ function zero(): Vector;
1011
+ /**
1012
+ * Compares two points
1013
+ * @param [delta = 0] - the tolance in comparison
1014
+ */
1015
+ function equal(one: Vector, other: Vector, delta?: number): boolean;
1016
+ /**
1017
+ * Creates a copy of the given point
1018
+ */
1019
+ function copy(one: Vector): Vector;
1020
+ function update(vector: Vector, x: any, y: any): void;
1021
+ /**
1022
+ * @returns ;
1023
+ */
1024
+ function diff(one: Vector, other: Vector): Pair;
1025
+ function multiply(one: Vector | number, other: Vector | number): Vector;
1026
+ function divide(one: Vector | number, other: Vector | number): Vector;
1027
+ function plus(one: Vector | number, other: Vector | number): Vector;
1028
+ function minus(one: Vector | number, other: Vector | number): Vector;
1029
+ function min(one: Vector): number;
1030
+ function apply(one: Vector | number, other: Vector | number, f: any): Vector;
1031
+ }
1032
+