summaryrefslogtreecommitdiff
path: root/client/assets/javascripts/rectangles/engine/map/ui.js
diff options
context:
space:
mode:
Diffstat (limited to 'client/assets/javascripts/rectangles/engine/map/ui.js')
-rw-r--r--client/assets/javascripts/rectangles/engine/map/ui.js131
1 files changed, 131 insertions, 0 deletions
diff --git a/client/assets/javascripts/rectangles/engine/map/ui.js b/client/assets/javascripts/rectangles/engine/map/ui.js
new file mode 100644
index 0000000..6e9a5ab
--- /dev/null
+++ b/client/assets/javascripts/rectangles/engine/map/ui.js
@@ -0,0 +1,131 @@
+
+map.ui = new function(){
+
+ var base = this
+
+ base.creating = base.dragging = base.resizing = false
+
+ base.mouse = new mouse({
+ el: map.el,
+ down: down,
+ move: move,
+ drag: drag,
+ up: up,
+ rightclick: rightclick,
+ })
+
+ base.wheel = new wheel({
+ el: map.el,
+ update: mousewheel,
+ })
+
+ //
+
+ function down (e, cursor){
+ cursor.x.div(w).add(0.5).mul(map.bounds.a / map.zoom).add(map.center.a)
+ cursor.y.div(h).sub(0.5).mul(map.bounds.b / map.zoom).sub(map.center.b)
+
+ if (e.ctrlKey || e.which === 3) {
+ cursor.quantize(1)
+ map.center.a = cursor.x.a
+ map.center.b = -cursor.y.a
+ console.log(map.center+"")
+ cursor.x.b = cursor.x.a
+ cursor.y.b = cursor.y.a
+ base.mouse.down = false
+ e.preventDefault()
+ e.stopPropagation()
+ return
+ }
+
+ var intersects = Rooms.filter(function(r){
+ return r.focused = r.rect.contains(cursor.x.a, cursor.y.a)
+ })
+
+ if (intersects.length) {
+ base.dragging = intersects[0]
+ base.resizing = base.dragging.rect.nearEdge(cursor.x.a, cursor.y.a, resize_margin / map.zoom)
+ base.dragging.rect.translation.sides = base.resizing
+ }
+ else {
+ base.creating = true
+ }
+
+ if (e.shiftKey && base.dragging) {
+ base.dragging.rect.quantize(10)
+ }
+ }
+
+ function move (e, cursor) {
+ cursor.x.div(w).add(0.5).mul(map.bounds.a / map.zoom).add(map.center.a)
+ cursor.y.div(h).sub(0.5).mul(map.bounds.b / map.zoom).sub(map.center.b)
+ }
+
+ function drag (e, cursor) {
+ cursor.x.b = ((cursor.x.b/w)+0.5) * map.bounds.a / map.zoom + map.center.a
+ cursor.y.b = ((cursor.y.b/h)-0.5) * map.bounds.b / map.zoom - map.center.b
+
+ if (base.resizing) {
+ var x_length = base.dragging.rect.x.length(),
+ y_length = base.dragging.rect.y.length()
+
+ if (base.resizing & LEFT) {
+ base.dragging.rect.translation.a = clamp( cursor.x.magnitude(), x_length - side_max, x_length - side_min )
+ }
+ if (base.resizing & RIGHT) {
+ base.dragging.rect.translation.a = clamp( cursor.x.magnitude(), side_min - x_length, side_max - x_length )
+ }
+ if (base.resizing & FRONT) {
+ base.dragging.rect.translation.b = clamp( cursor.y.magnitude(), y_length - side_max, y_length - side_min )
+ }
+ if (base.resizing & BACK) {
+ base.dragging.rect.translation.b = clamp( cursor.y.magnitude(), side_min - y_length, side_max - y_length )
+ }
+ }
+ else if (base.dragging) {
+ base.dragging.rect.translation.a = cursor.x.magnitude()
+ base.dragging.rect.translation.b = cursor.y.magnitude()
+ }
+ }
+
+ function up (e, cursor, new_cursor) {
+ new_cursor.x.div(w).add(0.5).mul(map.bounds.a / map.zoom).add(map.center.a)
+ new_cursor.y.div(h).sub(0.5).mul(map.bounds.b / map.zoom).sub(map.center.b)
+
+ if (base.creating) {
+ if (cursor.height() > side_min && cursor.width() > side_min) {
+ cursor.x.abs().quantize(1)
+ cursor.y.abs().quantize(1)
+ Rooms.add_with_rect( cursor )
+ }
+ }
+
+ if (base.resizing) {
+ base.dragging.rect.resize()
+ }
+ else if (base.dragging) {
+ base.dragging.rect.translate()
+ }
+
+ base.creating = base.dragging = base.resizing = false
+ }
+
+ function mousewheel (e, val, delta){
+ var cursor = base.mouse.cursor
+
+ var intersects = Rooms.filter(function(r){
+ return r.focused = r.rect.contains(cursor.x.a, cursor.y.a)
+ })
+
+ if (intersects.length) {
+ intersects[0].height = clamp( ~~(intersects[0].height - delta), height_min, height_max )
+ Rooms.clipper.update()
+ }
+ else {
+ map.set_zoom(map.zoom_exponent - delta/20)
+ }
+ }
+
+ function rightclick (e){
+ }
+}