Skip to content

Latest commit

 

History

History
193 lines (158 loc) · 11.2 KB

Puzzle.md

File metadata and controls

193 lines (158 loc) · 11.2 KB

Puzzle object

Puzzle object is declared as class pzpr.Puzzle. You can generate it by new pzpr.Puzzle() constructor.

Constructor

  • pzpr.Puzzle([canvas][,option]) Create puzzle object.
    • canvas:Node The canvas of the puzzle.
    • option:object See below.
      • option.type:string Select puzzle object type.
        • option.type==='editor' Default value. The puzzle can be inputted question data and answer data.
        • option.type==='player' The puzzle can be only taken answer data.
        • option.type==='viewer' The puzzle can display the puzzle but reject any input.
      • option.config:object Input initial config data as a object. See pzpr.Puzzle.Config section.
      • option.mode Select initial mode: editmode or playmode. Value is the same as puzzle.setMain().
      • option.graphic:string Specify graphic type. It can be 'svg' or 'canvas'.
      • option.width:number and option.height:number Specify the size of the canvas.
      • option.cellsize:number Specify the size of the canvas by the cellsize of the board.

If neither width, height nor cellsize is given, the size of given canvas element is used for the size of puzzle.

Methods for open/save puzzles

  • puzzle.open(data[, variety][, callback]) Open the specific URL/file data.
    • This API can be also used for opening new empty board as '(pid)/cols/rows' format, like puzzle.open('nurikabe/5/5').
    • Return value: puzzle object itself
    • data:string Data to be opened.
    • data:URLData or FileData Data can be a URLData or FileData object generated by pzpr.parser functions.
    • pid:string Predefined puzzle veriety name.
    • callback:function It will be called after opening operation finishs and ready event fired.
  • puzzle.getURL([urltype]) Return URL of the board.
    • Return value: Generated URL string.
    • urltype:URLType Specify URL type.
  • puzzle.getFileData([filetype[, option]]) Return File Data of the board.
    • Return value: Generated File data.
    • filetype:FileType Specify URL type. Default is pzpr.parser.FILE_PZPR.
    • option.history:boolean If filetype is FILE_PZPR and it is true, operation history will be outputted.
    • option.time:boolean If filetype is FILE_PZPR, option.history and it is true, operation history and operation time (millisecond) will be outputted.
    • option.trial:boolean If filetype is FILE_PZPR, option.history is false and it is true in trial mode, trial mode data will be outputted.
  • puzzle.clone([option]) Return cloned puzzle object from the puzzle.
    • Return value: cloned puzzle object.
    • option.type:string It means the same as option.type in constructor. If not specified, the type of the puzzle will be used.
    • option.width:number and option.height:number Set the size of the canvas of cloned puzzle object. If not specified, the size of current canvas will be used.

Methods for events

  • puzzle.on(eventtype, listener) Register event listener to pzpr object.
  • puzzle.once(eventtype, listener) Register once event listener to pzpr object.
    • eventtype:string Event type. See event section below.
    • listener:function Function to be called.

Methods for configuration

  • puzzle.setMode(type) It changes the puzzle into playmode or editmode when the puzzle type is 'editor'.
    • type If puzzle.MODE_PLAYER or 'play', it will change the puzzle mode to playmode.
      If puzzle.MODE_EDITOR or 'edit', puzzle mode will change into editmode.
  • puzzle.getConfig(configname) Get the value of the config data. See below to get the list of the config.
    • Return value: the value of the specified config.
    • configname:string the id name of the config. See pzpr.Puzzle.Config class secrion.
  • puzzle.setConfig(configname, value) Set the config data to the given value.
    • configname:string the id name of the config. See pzpr.Puzzle.Config class secrion.
    • value value to be set. Type is either string, number or boolean.
  • puzzle.resetConfig(configname) Reset the config data to the initial value. See below to get the list of the config.
    • configname:string the id name of the config. See pzpr.Puzzle.Config class secrion.
  • puzzle.getCurrentConfig() Return the list of the config values for current puzzle variety.
    • Return value: object.
  • puzzle.saveConfig() Return current changed config list from default value as object.
    • Return value: object.
  • puzzle.restoreConfig(object) Set config list by given object.
    • object:object the list of the config to be restored. Usually this is the object which puzzle.saveConfig() returned.

Methods for canvas

  • puzzle.setCanvas(canvas[, graphictype]) Set the canvas element after the puzzle object created.
    • canvas:Node The canvas of the puzzle.
    • graphictype:string Graphic type used for the canvas. 'svg' or 'canvas'.
  • puzzle.setCanvasSize(width, height) Set the canvas of the canvas.
  • puzzle.setCanvasSizeByCellSize(cellsize) Set the canvas of the canvas by the cellsize of the board.
  • puzzle.redraw([flush]) Redraw the whole canvas.
    • flush:boolean If true, it will destroy all cached data and redraw, otherwise cached data will be kept.
  • puzzle.irowake() Regenerate the color of the line if config.irowake (set individual color to line) is set.

Methods for outputting image

These methods have arguments below.

  • graphictype:string supports 'svg', 'png', 'jpeg', 'gif' and 'webp' if the environment supports. By default, 'svg' is used.

  • quality:number Compression quality for jpeg or webp.

  • option.cellsize:number or cellsize:number Identify output size by the size of cells.

  • option.bgcolor:string Specify background color. If this is empty, background will be transparent.

  • puzzle.toDataURL([graphictype][, quality][, cellsize][, option]) Return the canvas graphic data as Data URL.

    • Return value: Generated Data URL.
  • puzzle.toBuffer([graphictype][, quality][, cellsize][, option]) Return raw image data of the canvas.

    • Return value: Generated data as string.
  • puzzle.toBlob(callback[, graphictype][, quality][, cellsize][, option]) Generate the canvas graphic data as Blob.

    • Return value: No return value. Please use callback(blob).

Methods for the board model

  • puzzle.check([activemode]) Check if the board is modified or not.
    • Return value: CheckInfo which contains if the check succeed or not and failure information.
      • info.complete true if the answer is correct.
      • info.text contains error description unless activemode===false.
      • info.gettext(['en' or 'ja']) method gets error description in English or Japanese.
    • activemode:boolean
      • If true, checking answer routine will set error information and display what has error.
      • If false, it will only check errors, ignore 'multierr' config and info.text remains empty.
      • Otherwise, it will only check errors but respect 'multierr' config and set info.text.
  • puzzle.ansclear() Clear answer data on the board but it preserves operation history data.
  • puzzle.subclear() Clear auxiliary marks on the board.
  • puzzle.errclear() Wipe out the error information given after check() is called.
  • puzzle.clear() For 'editor' type puzzle, clean all board data. As for 'player' type puzzle, it clears answer data. In addition, it clears operation history data for both 'editor' and 'player'.

Methods for operation history

  • puzzle.undo() Undo an operation.
    • Return value: true if it is the first operation.
  • puzzle.redo() Redo an operation.
    • Return value: true if it is the last operation.
  • puzzle.undoall() Undo all recorded operations. No return value.
  • puzzle.redoall() Redo all recorded operations. No return value.
  • puzzle.ismodified() Check if the board is modified or not.
    • Return value: true when the puzzle is modified and the puzzle type is 'editor'.
  • puzzle.saved() Let puzzle.ismodified() return false.

Methods for trial mode

  • puzzle.enterTrial() Enter trial mode.
  • puzzle.acceptTrial() Finalize all trial mode changes and exit trial mode.
  • puzzle.rejectTrial() Erase all trial mode changes and exit trial mode.
  • puzzle.rejectCurrentTrial() Erase current trial mode stage changes and back to previous trial stage.

Methods for general purpose

  • puzzle.getTime() Return consumed time from the puzzle data is created or URL/filedata is opened.
    • Return value: consumed time in millisecond scale.

Events

  • 'ready' Emitted after puzzle.open finished successfully.
    • Callback: function(puzzle)
  • 'fail-open' Emitted if puzzle.open failed.
    • Callback: function(puzzle)
  • 'canvasReady' Emitted after both canvas is initialized and puzzle become ready. It is fired after 'ready' and 'resize' events finished.
    • Callback: function(puzzle)
  • 'resize' Emitted if the size of the canvas is changed.
    • Callback: function(puzzle)
  • 'adjust' Emitted after rows and cols on the board is expanded, reduced or the board is flipped or turned.
    • Callback: function(puzzle)
  • 'config' Emitted after a config has changed.
    • Callback: function(puzzle, configname, value)
  • 'key' Emitted when keyboard event is called, before common routine is called.
    • Callback: function(puzzle, keycode)
  • 'mouse' Emitted when mouse/touch event is called, before common routine is called.
    • Callback: function(puzzle)
  • 'history' Emitted after operation history is added or undo/redo is executed.
    • Callback: function(puzzle)
  • 'trial' Emitted after trial mode stage is changed.
    • Callback: function(puzzle, trialstage)
  • 'mode' Emitted after ready and mode (playmode or editmode) is changed.
    • Callback: function(puzzle)

Properties

  • puzzle.pzpr Reference to pzpr object.
  • puzzle.ready:boolean true after the URL/FileData is opened and board, painter and other related objects are set.
  • puzzle.pid:string specifies the variety/genre of the current puzzle.
  • puzzle.info:object Reference to VarietyInfo of the current puzzle.
  • puzzle.canvas:Node The node currently the puzzle has been using.
  • puzzle.editmode:boolean true if the puzzle is inputting question data at the moment.
  • puzzle.playmode:boolean true if the puzzle is inputting answer data at the moment.

For editmode or playmode property, one is true and the other is false.

Puzzle object sub-objects

Puzzle object has puzzle.board, puzzle.metadata, puzzle.moouse, puzzle.key and so on. For detail, see sub-objects of puzzle object document

class pzpr.Puzzle.Config

There are some puzzle configurations. For details, see list of puzzle config document.

Name Type Default value Description
irowake boolean false Set individual color to lines
autocmp boolean false Show complete numbers apart from incompleted one automatically.
autocmp_area boolean false Paint background of completed blocks/areas automatically.
autoerr boolean false Show incomplete/wrong numbers automatically.
multierr boolean false Check prural errors in puzzle.check() API.