antosdk-apps/LuaPlayground/coffees/main.coffee

251 lines
7.9 KiB
CoffeeScript
Raw Permalink Normal View History

2019-05-06 16:08:46 +02:00
class DataViewer
constructor: (data) ->
@target = data
@el = ($ "<canvas>").attr("class", "viewer")[0]
2019-05-06 20:31:20 +02:00
@offset = 10
@points = []
@preprocess()
2019-05-06 16:08:46 +02:00
@getBound()
@prepare()
@render()
canvasPoint: (v) ->
2019-05-06 17:51:57 +02:00
return new paper.Point(v[0] / @target.resolution + @base.x, - v[1] / @target.resolution + @base.y)
2019-05-06 16:08:46 +02:00
2019-05-06 20:31:20 +02:00
preprocess: () ->
@points = @target.data
2019-05-06 16:08:46 +02:00
getBound: () ->
peak_tl = { x:0, y:0}
peak_rb = { x:0, y:0}
start = null
2019-05-06 20:31:20 +02:00
for v in @points
2019-05-06 16:08:46 +02:00
x = v[0] / @target.resolution
y = v[1] / @target.resolution
peak_tl.x = x if x < peak_tl.x
peak_tl.y = y if y < peak_tl.y
peak_rb.x = x if x > peak_rb.x
peak_rb.y = y if y > peak_rb.y
@bound = [ peak_tl, peak_rb ]
2019-05-06 17:51:57 +02:00
@base = {x: 0 - @bound[0].x+ @offset, y: @bound[1].y + @offset}
2019-05-06 16:08:46 +02:00
@width = peak_rb.x - peak_tl.x + 2*@offset
@height = peak_rb.y - peak_tl.y + 2*@offset
2019-05-06 19:15:23 +02:00
drawPoint: (v, color, size) ->
# center
new paper.Path.Circle {
center: @canvasPoint(v),
radius: size,
fillColor: color
}
drawGrid: (size, color) ->
wgridsize = @target.resolution*size
# draw y line
2019-05-06 20:31:20 +02:00
i = Math.ceil(@bound[0].x / size)
while i*size < @bound[1].x
start = new paper.Point(i*size + @base.x, -@bound[0].y + @base.y )
end = new paper.Point(i*size + @base.x, -@bound[1].y + @base.y )
2019-05-06 19:15:23 +02:00
path = new paper.Path()
path.strokeColor = color
path.moveTo start
path.lineTo end
i++
# draw x line
2019-05-06 20:31:20 +02:00
i = Math.ceil(@bound[0].y / size)
while i*size < @bound[1].y
start = new paper.Point(@bound[0].x + @base.x,-i*size + @base.y )
end = new paper.Point(@bound[1].x + @base.x, -i*size + @base.y )
2019-05-06 19:15:23 +02:00
path = new paper.Path()
path.strokeColor = color
path.moveTo start
path.lineTo end
i++
2019-05-06 20:31:20 +02:00
# draw text
text = new paper.PointText(@bound[0].x + @base.x, @base.y - @bound[1].y + @offset)
text.justification = 'left'
text.fillColor = '#494949'
text.content = "Resolution: #{@target.resolution}, grid size: #{wgridsize} mm"
2019-05-06 19:15:23 +02:00
drawAxis: (color) ->
# x axis
2019-05-06 16:08:46 +02:00
path = new paper.Path()
2019-05-06 19:15:23 +02:00
path.strokeColor = color
2019-05-06 20:31:20 +02:00
start = new paper.Point( @bound[0].x + @base.x, @base.y)
end = new paper.Point( @bound[1].x + @base.x, @base.y)
2019-05-06 16:08:46 +02:00
path.moveTo(start)
path.lineTo(end)
# y axis
path = new paper.Path()
2019-05-06 19:15:23 +02:00
path.strokeColor = color
2019-05-06 20:31:20 +02:00
start = new paper.Point(@base.x, -@bound[0].y + @base.y)
end = new paper.Point(@base.x, -@bound[1].y + @base.y)
2019-05-06 16:08:46 +02:00
path.moveTo(start)
path.lineTo(end)
2019-05-06 19:15:23 +02:00
@drawPoint [0,0], color, 3
2019-05-06 16:08:46 +02:00
2019-05-06 19:15:23 +02:00
prepare: () ->
ctx = @el.getContext "2d"
ctx.translate @base.x, @base.y
ctx.canvas.width = @width
ctx.canvas.height = @height
paper.setup @el
#tool = new paper.Tool()
#hitOptions = {
# segments: true,
# stroke: true,
# fill: true,
# tolerance: 5
#}
#tool.onMouseMove = (event) ->
# hitResult = paper.project.hitTest event.point, hitOptions
# return unless hitResult
# console.log hitResult
2019-05-06 16:08:46 +02:00
render:() ->
# sub class responsibility
class PointCloudViewer extends DataViewer
constructor: (data) ->
super data
2019-05-06 20:31:20 +02:00
p2c: (length, angle) ->
rad = angle*Math.PI / 180
return [length*Math.cos(rad), length*Math.sin(rad)]
preprocess: () ->
return @points = @target.data unless @target.coordinate is "polar"
@point = []
i = 0
for v in @target.data
@points.push @p2c v, @target.start + i*@target.angularResolution
i = i+1
2019-05-06 16:08:46 +02:00
# point clound render
render: () ->
2019-05-06 19:15:23 +02:00
@drawGrid 20, "#DBDBDB" # 20 px
@drawAxis("#0A84FF")
2019-05-06 20:31:20 +02:00
for v in @points
if @target.coordinate is "polar"
path = new paper.Path()
path.strokeColor = '#c2a10e'
start = @canvasPoint [0,0]
end = @canvasPoint v
path.moveTo start
path.lineTo end
@drawPoint v, "red", 3
2019-05-06 16:08:46 +02:00
paper.view.draw()
2020-06-05 17:45:07 +02:00
class LuaPlayground extends this.OS.application.BaseApplication
2018-04-14 17:41:57 +02:00
constructor: ( args ) ->
super "LuaPlayground", args
main: () ->
@datarea = @find "editorea"
@output = @find "output"
@.editor = ace.edit @datarea
@.editor.setOptions {
enableBasicAutocompletion: true,
enableLiveAutocompletion: true,
2020-05-21 21:56:16 +02:00
highlightActiveLine: true,
highlightSelectedWord: true,
behavioursEnabled: true,
wrap: true,
fontSize: "11pt",
showInvisibles: true
2018-04-14 17:41:57 +02:00
}
@editor.getSession().setUseWrapMode true
@editor.session.setMode "ace/mode/lua"
@editor.setTheme "ace/theme/monokai"
2020-05-21 21:56:16 +02:00
@on "vboxchange", () =>
@editor.resize()
2020-06-05 17:45:07 +02:00
(@find "log-clear").onbtclick = (e) =>
2020-05-21 21:56:16 +02:00
@log "clean"
2020-06-05 17:45:07 +02:00
(@find "code-run").onbtclick = (e) =>
2020-05-21 21:56:16 +02:00
@run()
2019-05-07 14:34:26 +02:00
2020-06-05 17:45:07 +02:00
(@find "code-stop").onbtclick = (e) =>
2020-05-21 21:56:16 +02:00
@socket.close() if @socket
2019-05-07 14:34:26 +02:00
2018-04-14 17:41:57 +02:00
@socket = null
2020-05-21 21:56:16 +02:00
@bindKey "CTRL-R", () => @run()
2023-07-14 12:05:12 +02:00
@morphon OS.GUI.RESPONSIVE.PORTRAIT, (fulfilled) =>
console.log fulfilled, "FULL"
if fulfilled
this.find("wrapper").dir = "column"
this.find("resizer").dir = "column"
else
this.find("wrapper").dir = "row"
this.find("resizer").dir = "row"
2018-04-14 17:41:57 +02:00
menu: () ->
menu = [{
text: "__(Code)",
2020-06-05 17:45:07 +02:00
nodes: [
2018-04-14 17:41:57 +02:00
{ text: "__(Run)", dataid: "#{@name}-Run", shortcut: "C-R" }
],
2020-05-21 21:56:16 +02:00
onchildselect: (e) => @run()
2018-04-14 17:41:57 +02:00
}]
menu
log: (t, m) ->
return $(@output).empty() if t is "clean"
p = ($ "<p>").attr("class", t.toLowerCase())[0]
$(p).html "#{t}: #{m.__()}"
($ @output).append p
($ @output).scrollTop @output.scrollHeight
run: () ->
value = @editor.getValue().trim()
return unless value and value isnt ""
2020-05-21 21:56:16 +02:00
@stream().then (s) =>
@socket = s
@socket.onopen = () =>
#send data to server
@socket.send( JSON.stringify { code: value } )
@socket.onmessage = (e) =>
return unless e.data
try
obj = JSON.parse e.data
@log "INFO", e.data unless @view obj
catch err
@log "INFO", e.data
console.log err
@socket.onclose = () =>
@socket = null
console.log "socket closed"
.catch (e) => @error __("Unable to get websocket stream")
2019-05-06 16:08:46 +02:00
view: (obj) ->
return false unless obj and obj.type and @[obj.type]
el = @[obj.type](obj).el
p = ($ "<p>").attr("class", "info")[0]
$(p).append el
($ @output).append p
($ @output).scrollTop @output.scrollHeight
return true
pc: (data) ->
return new PointCloudViewer(data)
2018-04-14 17:41:57 +02:00
cleanup: (e)->
@socket.close() if @socket
2021-04-21 11:37:58 +02:00
LuaPlayground.dependencies = [
"pkg://ACECore/core/ace.js",
"pkg://ACECore/path.js",
2023-07-14 12:05:12 +02:00
"pkg://ACECore/core/ext-language_tools.js",
"pkg://ACECore/core/ext-modelist.js",
"pkg://ACECore/core/ext-themelist.js",
2021-04-21 11:37:58 +02:00
]
2018-04-14 17:41:57 +02:00
this.OS.register "LuaPlayground", LuaPlayground