// Load this file in the DisplayScript editor to view it. __fontName = "AvenirNext-UltraLight" state result = 0 state entered = #nothing state operation = #equals var gridLayout = [ [#readout], [#d(7), #d(8), #d(9), #o(#dividedBy)], [#d(4), #d(5), #d(6), #o(#times)], [#d(1), #d(2), #d(3), #o(#minus)], [#clear, #d(0), #o(#equals), #o(#plus)], ] function gridCellTitle(entry) { switch entry { case #d(digit): return "\(digit)" case #o(#plus): return "+" case #o(#minus): return "\u{2212}" case #o(#times): return "\u{d7}" case #o(#dividedBy): return "\u{f7}" case #o(#equals): return "=" case #clear: return "C" } } function applyOperationTo(n) { switch operation { case #plus: next result = result + n case #minus: next result = result - n case #times: next result = result * n case #dividedBy: if n != 0 { next result = result / n } default: next result = n } } function gridCellAction(entry) { switch entry { case #d(digit): switch entered { case #nothing: next entered = #number(digit) case #number(n): next entered = #number(n * 10 + digit) } case #o(op): switch entered { case #number(n): applyOperationTo(n) } next entered = #nothing next operation = op case #clear: next entered = #nothing next operation = #equals next result = 0 } } drawable gridCellButton(entry) { state buttonState = #unpressed var backgroundColor = srgb(0.58, 0.86, 1) if buttonState == #pressed { backgroundColor = srgb(0.82, 0.96, 1) } draw fill(backgroundColor) __fontSize = 40 draw centered(text(gridCellTitle(entry))) draw buttonHandler(function () { animate(smoothCurve(0.97), function () { next buttonState = #pressed }) }, function () { animate(smoothCurve(0.985), function () { next buttonState = #unpressed }) }, function () { gridCellAction(entry) }) } drawable gridCell(entry) { switch entry { case #readout: var displayNumber = result switch entered { case #number(n): displayNumber = n } __fontSize = 48 draw inset(10, rightAligned(text("\(displayNumber)"))) default: draw gridCellButton(entry) } } var cellGap = 1 for row at rowIndex in gridLayout { var cellWidth = (@width + cellGap) / count(row) var cellHeight = (@height + cellGap) / count(gridLayout) for entry at columnIndex in row { var x = round(columnIndex * cellWidth) var y = round(rowIndex * cellHeight) var nextX = round((columnIndex + 1) * cellWidth) - cellGap var nextY = round((rowIndex + 1) * cellHeight) - cellGap draw at(x, y, nextX - x, nextY - y, gridCell(entry)) } }