<commit_before>{ToolWithStroke} = require './base'
{createShape} = require '../core/shapes'

module.exports = class Pencil extends ToolWithStroke

  name: 'Polygon'
  iconName: 'polygon'
  usesSimpleAPI: false

  didBecomeActive: (lc) ->
    unsubscribeFuncs = []
    @unsubscribe = =>
      for func in unsubscribeFuncs
        func()

    @points = null
    @maybePoint = null

    onUp = =>
      return @_close(lc) if @_getWillFinish()
      @_ensureFinishButtonsExist(lc) unless @points

      if @points
        @points.push(@maybePoint)
      else
        @points = [@maybePoint]

      @maybePoint = {x: @maybePoint.x, y: @maybePoint.y}
      lc.setShapesInProgress(@_getShapes(lc))
      lc.repaintLayer('main')

    onMove = ({x, y}) =>
      if @maybePoint
        @maybePoint.x = x
        @maybePoint.y = y
        lc.setShapesInProgress(@_getShapes(lc))
        lc.repaintLayer('main')

    onDown = ({x, y}) =>
      @maybePoint = {x, y}
      lc.setShapesInProgress(@_getShapes(lc))
      lc.repaintLayer('main')

    unsubscribeFuncs.push lc.on 'lc-pointerdown', onDown
    unsubscribeFuncs.push lc.on 'lc-pointerdrag', onMove
    unsubscribeFuncs.push lc.on 'lc-pointermove', onMove
    unsubscribeFuncs.push lc.on 'lc-pointerup', onUp

  willBecomeInactive: (lc) ->
    @_cancel(lc) if @points or @maybePoint
    @unsubscribe()

  _getArePointsClose: (a, b) ->
    return (Math.abs(a.x - b.x) + Math.abs(a.y - b.y)) < 10

  _getWillClose: ->
    return false unless @points and @points.length > 1
    return false unless @maybePoint
    return @_getArePointsClose(@points[0], @maybePoint)

  _getWillFinish: ->
    return false unless @points and @points.length > 1<commit_msg>Polygon tool cancels if you undo/redo<commit_after>{ToolWithStroke} = require './base'
{createShape} = require '../core/shapes'

module.exports = class Pencil extends ToolWithStroke

  name: 'Polygon'
  iconName: 'polygon'
  usesSimpleAPI: false

  didBecomeActive: (lc) ->
    unsubscribeFuncs = []
    @unsubscribe = =>
      for func in unsubscribeFuncs
        func()

    @points = null
    @maybePoint = null

    onUp = =>
      return @_close(lc) if @_getWillFinish()
      @_ensureFinishButtonsExist(lc) unless @points

      if @points
        @points.push(@maybePoint)
      else
        @points = [@maybePoint]

      @maybePoint = {x: @maybePoint.x, y: @maybePoint.y}
      lc.setShapesInProgress(@_getShapes(lc))
      lc.repaintLayer('main')

    onMove = ({x, y}) =>
      if @maybePoint
        @maybePoint.x = x
        @maybePoint.y = y
        lc.setShapesInProgress(@_getShapes(lc))
        lc.repaintLayer('main')

    onDown = ({x, y}) =>
      @maybePoint = {x, y}
      lc.setShapesInProgress(@_getShapes(lc))
      lc.repaintLayer('main')

    unsubscribeFuncs.push lc.on 'undo', => @_cancel(lc)
    unsubscribeFuncs.push lc.on 'redo', => @_cancel(lc)
    unsubscribeFuncs.push lc.on 'lc-pointerdown', onDown
    unsubscribeFuncs.push lc.on 'lc-pointerdrag', onMove
    unsubscribeFuncs.push lc.on 'lc-pointermove', onMove
    unsubscribeFuncs.push lc.on 'lc-pointerup', onUp

  willBecomeInactive: (lc) ->
    @_cancel(lc) if @points or @maybePoint
    @unsubscribe()

  _getArePointsClose: (a, b) ->
    return (Math.abs(a.x - b.x) + Math.abs(a.y - b.y)) < 10

  _getWillClose: ->
    return false unless @points and @points.length > 1
    return false unless @maybePoint
    return @_getArePointsClose(@points[0], @maybePoint)

  _getWillFinish: ->
    return false unless @points and @points.length > 1
{'fraction_non_alphanumeric': 0.13293650793650794, 'fraction_numerical': 0.002834467120181406, 'mean_word_length': 2.9607182940516275, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3186648', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1209, 'n_words': 264}