diff options
Diffstat (limited to 'assets/javascripts/rectangles/map/ui.js')
| -rw-r--r-- | assets/javascripts/rectangles/map/ui.js | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/assets/javascripts/rectangles/map/ui.js b/assets/javascripts/rectangles/map/ui.js new file mode 100644 index 0000000..05fad90 --- /dev/null +++ b/assets/javascripts/rectangles/map/ui.js @@ -0,0 +1,103 @@ +/* +*/ + +window.ctx = window.w = window.h = null; + +var map = new function(){ + var base = this + base.bounds = new vec2(500,500) + base.center = new vec2(0,0) + + var canvas = document.createElement("canvas") + var ctx = window.ctx = canvas.getContext("2d") + var w = window.w = canvas.width = 500 + var h = window.h = canvas.height = 500 + document.querySelector("#map").appendChild(canvas) + + base.animate = function(){ + ctx.save() + clear_canvas() + draw_ruler() + + ctx.translate( map.center.a + map.bounds.a/2, map.center.b + map.bounds.b/2 ) + ctx.scale( -1, 1 ) + + draw_regions(clipper.regions) + draw_mouse(map.ui.mouse.cursor) + + ctx.restore() + } +} + +map.ui = new function(){ + var base = this + base.el = document.querySelector("#map") + + base.mouse = new mouse({ + el: base.el, + down: down, + move: move, + drag: drag, + up: up, + }) + + base.wheel = new wheel({ + el: base.el, + update: function(e, val, delta){ + // do something with val + }, + }) + + function down (e, cursor){ + cursor.x.add( map.center.a + map.bounds.a/2 ) + cursor.y.add( map.center.b - map.bounds.b/2 ) + + var intersects = clipper.rooms.filter(function(r){ + return r.focused = r.rect.contains(cursor) + }) + + if (intersects.length){ + base.dragging = intersects[0] + } + else { + base.creating = true + } + + if (e.shiftKey && base.dragging) { + base.dragging.rect.quantize(10) + } + } + + function move (e, cursor) { + cursor.x.add( map.center.a + map.bounds.a/2 ) + cursor.y.add( map.center.b - map.bounds.b/2 ) + z=true + } + + function drag (e, cursor) { + cursor.x.b += map.center.a + map.bounds.a/2 + cursor.y.b += map.center.b - map.bounds.b/2 + + if (base.dragging) { + base.dragging.rect.translation.a = cursor.x.magnitude() + base.dragging.rect.translation.b = cursor.y.magnitude() + } + } + + function up (e, cursor) { + + if (base.creating) { + if (cursor.height() != 0 && cursor.width() != 0) { + cursor.x.abs() + cursor.y.abs() + clipper.add_room( cursor ) + } + } + + if (base.dragging) { + base.dragging.rect.translate() + } + + base.creating = base.dragging = false + } +}
\ No newline at end of file |
