2019-05-06 16:08:46 +02:00
|
|
|
class DataViewer
|
|
|
|
constructor: (data) ->
|
|
|
|
@target = data
|
|
|
|
@el = ($ "<canvas>").attr("class", "viewer")[0]
|
|
|
|
@offset = 5
|
|
|
|
@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
|
|
|
|
|
|
|
getBound: () ->
|
|
|
|
peak_tl = { x:0, y:0}
|
|
|
|
peak_rb = { x:0, y:0}
|
|
|
|
start = null
|
|
|
|
for v in @target.data
|
|
|
|
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
|
|
|
|
prepare: () ->
|
|
|
|
ctx = @el.getContext "2d"
|
|
|
|
ctx.translate(@base.x, @base.y)
|
|
|
|
ctx.canvas.width = @width
|
|
|
|
ctx.canvas.height = @height
|
|
|
|
paper.setup @el
|
|
|
|
# draw the base
|
|
|
|
# x axis
|
|
|
|
path = new paper.Path()
|
|
|
|
path.strokeColor = '#BBBBBB'
|
|
|
|
start = @canvasPoint [@bound[0].x, 0]
|
|
|
|
end = @canvasPoint [@bound[1].x, 0]
|
|
|
|
path.moveTo(start)
|
|
|
|
path.lineTo(end)
|
|
|
|
# y axis
|
|
|
|
path = new paper.Path()
|
|
|
|
path.strokeColor = '#BBBBBB'
|
|
|
|
start = @canvasPoint [0, @bound[0].y]
|
|
|
|
end = @canvasPoint [0, @bound[1].y]
|
|
|
|
path.moveTo(start)
|
|
|
|
path.lineTo(end)
|
|
|
|
|
|
|
|
render:() ->
|
|
|
|
# sub class responsibility
|
|
|
|
|
|
|
|
class PointCloudViewer extends DataViewer
|
|
|
|
constructor: (data) ->
|
|
|
|
super data
|
|
|
|
|
|
|
|
# point clound render
|
|
|
|
render: () ->
|
|
|
|
path = new paper.Path()
|
|
|
|
path.strokeColor = 'black'
|
|
|
|
start = null
|
|
|
|
for v in @target.data
|
|
|
|
point = @canvasPoint v
|
|
|
|
if not start
|
|
|
|
start = point
|
|
|
|
path.moveTo(start)
|
|
|
|
else
|
|
|
|
path.lineTo point
|
|
|
|
paper.view.draw()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-14 17:41:57 +02:00
|
|
|
class LuaPlayground extends this.OS.GUI.BaseApplication
|
|
|
|
constructor: ( args ) ->
|
|
|
|
super "LuaPlayground", args
|
|
|
|
|
|
|
|
main: () ->
|
|
|
|
me = @
|
|
|
|
@datarea = @find "editorea"
|
|
|
|
@output = @find "output"
|
|
|
|
@.editor = ace.edit @datarea
|
|
|
|
@.editor.setOptions {
|
|
|
|
enableBasicAutocompletion: true,
|
|
|
|
enableLiveAutocompletion: true,
|
|
|
|
fontSize: "10pt"
|
|
|
|
}
|
|
|
|
@editor.getSession().setUseWrapMode true
|
|
|
|
@editor.session.setMode "ace/mode/lua"
|
|
|
|
@editor.setTheme "ace/theme/monokai"
|
|
|
|
@on "vboxchange", () ->
|
|
|
|
me.editor.resize()
|
2018-04-14 19:10:58 +02:00
|
|
|
(@find "log-clear").set "onbtclick", (e) ->
|
|
|
|
me.log "clean"
|
2018-04-14 17:41:57 +02:00
|
|
|
@socket = null
|
2018-04-14 19:10:58 +02:00
|
|
|
@bindKey "CTRL-R", () -> me.run()
|
2018-04-14 17:41:57 +02:00
|
|
|
menu: () ->
|
|
|
|
me = @
|
|
|
|
menu = [{
|
|
|
|
text: "__(Code)",
|
|
|
|
child: [
|
|
|
|
{ text: "__(Run)", dataid: "#{@name}-Run", shortcut: "C-R" }
|
|
|
|
],
|
|
|
|
onmenuselect: (e) -> me.run()
|
|
|
|
}]
|
|
|
|
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: () ->
|
|
|
|
me = @
|
|
|
|
value = @editor.getValue().trim()
|
|
|
|
return unless value and value isnt ""
|
|
|
|
proto = if window.location.protocol is "https:" then "wss://" else "ws://"
|
2018-09-16 20:29:49 +02:00
|
|
|
@socket = new WebSocket proto + @_api.HOST + "/system/apigateway?ws=1"
|
2018-04-14 17:41:57 +02:00
|
|
|
@socket.onopen = () ->
|
|
|
|
#send data to server
|
|
|
|
me.socket.send( JSON.stringify { code: value } )
|
|
|
|
|
2019-05-06 16:08:46 +02:00
|
|
|
@socket.onmessage = (e) ->
|
|
|
|
return unless e.data
|
|
|
|
try
|
|
|
|
obj = JSON.parse e.data
|
|
|
|
me.log "INFO", e.data unless me.view obj
|
|
|
|
catch err
|
|
|
|
me.log "INFO", e.data
|
|
|
|
console.log err
|
|
|
|
|
2018-04-14 17:41:57 +02:00
|
|
|
@socket.onclose = () ->
|
|
|
|
me.socket = null
|
|
|
|
console.log "socket closed"
|
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
|
|
|
|
|
2018-07-24 19:18:07 +02:00
|
|
|
LuaPlayground.dependencies = ["ace/ace"]
|
2018-04-14 17:41:57 +02:00
|
|
|
this.OS.register "LuaPlayground", LuaPlayground
|