Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defaultStyle = {
- fontName: "sans-serif"
- fontSize: 16
- topPadding: 0
- leftPadding: 0
- rightPadding: 0
- bottomPadding: 0
- spacing: 5
- indent: 10
- verticalSpacing: 0
- color: "black"
- selection: "blue"
- #background: "green"
- #border: "red"
- }
- buildStyle = (parent, style) ->
- for name, value of parent
- style[name] ?= value
- return style
- labelStyle = buildStyle defaultStyle, {
- color: "purple"
- fontSize: 12
- }
- condStyle = buildStyle defaultStyle, {
- indent: 0
- selection: "yellow"
- }
- addFrame = (container, node) ->
- if isMark(node, 'cr')
- return container.newline(node)
- if isList(node, 'cond')
- container.push frame = newFrame node, condStyle
- first = true
- for item in node.list
- frame.newline() unless first
- if isList(item, 'else')
- row = newFrame item, defaultStyle
- row.push newDeco("else", labelStyle)
- for subitem in item.list
- addFrame row, subitem
- else
- row = newFrame item, defaultStyle
- row.push newDeco("else", labelStyle) unless first
- row.push newDeco("if", labelStyle)
- for subitem in item.list
- addFrame row, subitem
- frame.push row
- first = false
- else if isList(node, 'infix')
- container.push frame = newFrame node, buildStyle defaultStyle, {
- selection: "green"
- }
- #frame.push newDeco("{", labelStyle)
- addFrame frame, item for item in node.list
- #frame.push newDeco("}", labelStyle)
- else if isList(node)
- container.push frame = newFrame node, buildStyle defaultStyle, {
- selection: "blue"
- }
- if node.label?
- frame.push newDeco(node.label, labelStyle)
- for item in node.list
- addFrame(frame, item)
- else if isText(node, 'int')
- container.push frame = newFrame node, buildStyle container.style, {
- color: "blue"
- }
- else
- container.push frame = newFrame node, container.style
- return null
- window.addEventListener 'load', () ->
- canvas = autoResize document.getElementById('editor')
- bc = canvas.getContext '2d'
- mouse = mouseInput(canvas)
- root = newList([
- newList([
- newList([
- newText("square")
- newText("x")
- ])
- newMark('cr')
- newList([
- newText("x")
- newText("*")
- newText("x")
- ], 'infix')
- ], 'define')
- newList([
- newList([
- newText("factorial")
- newText("n")
- ])
- newMark('cr')
- newList([
- newList([
- newList([
- newText("n")
- newText("=")
- newText("1", "int")
- ], 'infix')
- newMark("cr")
- newText("1", "int")
- ])
- newList([
- newList([
- newText("n")
- newText("=")
- newText("0", "int")
- ], 'infix')
- newMark("cr")
- newText("1", "int")
- ])
- newList([
- newMark("cr")
- newList([
- newText("n")
- newText("*")
- newList([
- newText("factorial")
- newList([
- newText("n")
- newText("-")
- newText("1", "int")
- ], 'infix')
- ])
- ], 'infix')
- ], 'else')
- ], 'cond')
- ], 'define')
- ])
- frame = newFrame root, buildStyle defaultStyle, {
- indent: 0
- verticalSpacing: 25
- }
- first = true
- for node in frame.node.list
- frame.newline() unless first
- addFrame(frame, node)
- first = false
- draw = () ->
- bc.fillStyle = "#ccc"
- bc.fillRect(0, 0, canvas.width, canvas.height)
- frame.layout(bc)
- frame.x = 50
- frame.y = 50
- frame.paint(bc)
- near = frame.nearest(mouse.point...)
- over = null
- over = near.frame.parent if near?
- # if over?
- # [dx, dy] = mouse.point
- # if over.parent?
- # {x, y} = over.parent.getPosition()
- # dx -= x
- # dy -= y
- # near = over.nearest(dx, dy)
- bc.globalAlpha = 0.1
- while over?
- bc.strokeStyle = over.style.selection
- {x, y} = over.getPosition()
- bc.strokeRect(x, y, over.width, over.height)
- over = over.parent
- bc.globalAlpha = 0.5
- if near? and near.frame? and near.dist < 100
- index = near.index
- near = near.frame
- bc.strokeStyle = "black"
- bc.fillStyle = "black"
- {x, y} = near.getPosition()
- near.paintSelection(bc, index, index)
- bc.globalAlpha = 1.0
- requestAnimationFrame draw
- draw()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement