Skip to content

Commit

Permalink
Numeric attributes can have ranges now
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuahhh committed Mar 1, 2017
1 parent 2bfb14e commit c8c4c15
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
21 changes: 19 additions & 2 deletions src/Model/Attribute.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = Attribute = Node.createVariant
parsing = @_parsing()

switch parsing.type
when "override", "number"
when "override", "number", "rangedNumber"
# These types have constant values stored in the parsing...

return parsing.value
Expand Down Expand Up @@ -64,6 +64,15 @@ module.exports = Attribute = Node.createVariant
_.extend @__parsing, {
type: "number"
value: parseFloat(@exprString)
precision: Util.precision(@exprString)
}
else if rangedNumberMatch = Util.matchRangedNumberString(@exprString)
_.extend @__parsing, {
type: "rangedNumber"
value: parseFloat(rangedNumberMatch.valueStr)
low: parseFloat(rangedNumberMatch.lowStr)
high: parseFloat(rangedNumberMatch.highStr)
precision: Util.precision(rangedNumberMatch.valueStr)
}
else
compiledExpression = new CompiledExpression(this)
Expand Down Expand Up @@ -118,7 +127,7 @@ module.exports = Attribute = Node.createVariant
hasReferences: -> _.any(@references(), -> true)

isNumber: ->
return @_parsing().type == "number"
return @_parsing().type in ["number", "rangedNumber"]

isString: ->
return Util.isStringLiteral(@exprString)
Expand All @@ -132,6 +141,14 @@ module.exports = Attribute = Node.createVariant
isNovel: ->
@hasOwnProperty("exprString")

precision: ->
return @_parsing().precision

range: ->
parsing = @_parsing()
if parsing.type == 'rangedNumber'
return {low: parsing.low, high: parsing.high}

# Descends through all recursively referenced attributes. An object is
# returned with two properties:
# dependencies: array consisting of the set of all recursive dependencies
Expand Down
11 changes: 11 additions & 0 deletions src/Util/Util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,21 @@ Util.numberFragment =
"[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?"
Util.numberRegExp =
new RegExp("^#{Util.numberFragment}$")
Util.rangedNumberRegExp =
new RegExp("^(#{Util.numberFragment}){(#{Util.numberFragment})-(#{Util.numberFragment})}$")

Util.isNumberString = (string) ->
return Util.numberRegExp.test(string)

Util.matchRangedNumberString = (string) ->
maybeMatch = string.match(Util.rangedNumberRegExp)
if not maybeMatch then return maybeMatch # no match
return {
valueStr: maybeMatch[1]
lowStr: maybeMatch[3]
highStr: maybeMatch[5]
}

Util.isStringLiteral = (string) ->
return /^(?:"(?:\\"|[^"\r\n])*"|'(?:\\'|[^'\r\n])*')$/.test(string)

Expand Down
19 changes: 16 additions & 3 deletions src/View/ApparatusCanvas.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ R.create "ApparatusCanvas",
initialValues = for attribute in attributesToChange
attribute.value()
precisions = for attribute in attributesToChange
Util.precision(attribute.exprString)
attribute.precision()
ranges = for attribute in attributesToChange
attribute.range()

objective = (trialValues) =>
for attribute, index in attributesToChange
Expand All @@ -385,14 +387,25 @@ R.create "ApparatusCanvas",
return error

solvedValues = Util.solve(objective, initialValues)

for attribute, index in attributesToChange
solvedValue = solvedValues[index]
precision = precisions[index]
# Hold the command key to "snap-drag" to one level coarser
# precision.
range = ranges[index]

# Apply range restrictions:
if range
solvedValue = Math.min(Math.max(solvedValue, range.low), range.high)

# Apply precision: (Hold the command key to "snap-drag" to one level
# coarser precision.)
if key.command
solvedValue = Util.roundToPrecision(solvedValue, precision - 1)
solvedValue = Util.toPrecision(solvedValue, precision)

if range
solvedValue = solvedValue + "{#{range.low}-#{range.high}}"

attribute.setExpression(solvedValue)

if startImmediately
Expand Down
7 changes: 6 additions & 1 deletion src/View/ExpressionCode.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,11 @@ R.create "ExpressionCode",
return {start, end}

_startScrubbingSelection: (mouseDownEvent) ->
dragManager = @context.dragManager
{attribute} = @props
{dragManager} = @context

originalValue = +@mirror.getSelection()
isAtStart = @mirror.getCursor("from").ch == 0
precision = Util.precision(@mirror.getSelection())

startX = mouseDownEvent.clientX
Expand All @@ -382,7 +384,10 @@ R.create "ExpressionCode",
dx = dx / 3
delta = dx * Math.pow(10, -precision)
newValue = originalValue + delta
if isAtStart and range = attribute.range()
newValue = Math.min(Math.max(newValue, range.low), range.high)
if key.command
newValue = Util.roundToPrecision(newValue, precision - 1)
newValue = Util.toPrecision(newValue, precision)

@mirror.replaceSelection(""+newValue, "around")

0 comments on commit c8c4c15

Please sign in to comment.