diff options
Diffstat (limited to 'assets/javascripts/rectangles/util')
| -rw-r--r-- | assets/javascripts/rectangles/util/colors.js | 36 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/util/debug.js | 2 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/util/mouse.js | 114 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/util/sort.js | 37 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/util/wheel.js | 62 |
5 files changed, 251 insertions, 0 deletions
diff --git a/assets/javascripts/rectangles/util/colors.js b/assets/javascripts/rectangles/util/colors.js new file mode 100644 index 0000000..3bdebbc --- /dev/null +++ b/assets/javascripts/rectangles/util/colors.js @@ -0,0 +1,36 @@ +(function(){ + var color_palettes = { + alpha: [ + "rgba(0,0,0,0.1)", + ], + redblue: [ + "rgba(0,0,0,0.2)", + "rgba(255,0,0,0.2)", + "rgba(0,0,255,0.2)", + "rgba(0,255,0,0.2)", + ], + gray: [ + "rgba(0,0,0,0.1)", + "rgba(0,0,0,0.2)", + "rgba(0,0,0,0.3)", + "rgba(0,0,0,0.4)", + ], + colors: [ + "rgba(255,0,0,0.5)", + "rgba(255,128,0,0.5)", + "rgba(128,255,0,0.5)", + "rgba(0,255,0,0.5)", + "rgba(0,255,128,0.5)", + "rgba(0,128,255,0.5)", + "rgba(0,0,255,0.5)", + "rgba(128,0,255,0.5)", + "rgba(255,0,255,0.5)", + "rgba(255,0,128,0.5)", + ] + } + + var select = document.querySelector("#palette") + select.addEventListener("change", function(){ colors = color_palettes[select.value] }) + + window.colors = color_palettes[select.value] +})() diff --git a/assets/javascripts/rectangles/util/debug.js b/assets/javascripts/rectangles/util/debug.js new file mode 100644 index 0000000..437abb8 --- /dev/null +++ b/assets/javascripts/rectangles/util/debug.js @@ -0,0 +1,2 @@ +window.z = true; +document.body.addEventListener("mousedown", function(){ z = true }) diff --git a/assets/javascripts/rectangles/util/mouse.js b/assets/javascripts/rectangles/util/mouse.js new file mode 100644 index 0000000..b8d6045 --- /dev/null +++ b/assets/javascripts/rectangles/util/mouse.js @@ -0,0 +1,114 @@ +/* + usage: + + base.mouse = new mouse({ + el: document.querySelector("#map"), + down: function(e, cursor){ + // do something with val + // cursor.x.a + // cursor.y.a + }, + move: function(e, cursor, delta){ + // delta.a (x) + // delta.b (y) + }, + up: function(e, cursor){ + // cursor.x.a + // cursor.y.a + }, + }) + +*/ + +function mouse (opt) { + var base = this + + opt = defaults(opt, { + el: document, + down: null, + move: null, + drag: null, + up: null, + propagate: false, + locked: false, + val: 0, + }) + + base.down = false + + base.creating = false + base.dragging = false + + base.cursor = new rect(0,0,0,0) + + base.tube = new Tube () + opt.down && base.tube.on("down", opt.down) + opt.move && base.tube.on("move", opt.move) + opt.drag && base.tube.on("drag", opt.drag) + opt.up && base.tube.on("up", opt.up) + + base.init = function (){ + base.bind() + } + + base.bind = function(){ + opt.el.addEventListener("mousedown", base.mousedown) + window.addEventListener("mousemove", base.mousemove) + window.addEventListener("mouseup", base.mouseup) + } + + function positionFromMouse(e) { + var offset = opt.el.getBoundingClientRect() + var mx = offset.left - e.pageX + var my = e.pageY - offset.top + + return new vec2(mx, my) + } + + base.mousedown = function(e){ + e.stopPropagation() + + var pos = positionFromMouse(e) + + var x = pos.a, y = pos.b + base.cursor = new rect (x,y, x,y) + base.down = true + + base.tube("down", e, base.cursor) + } + base.mousemove = function(e){ + e.stopPropagation() + + var pos = positionFromMouse(e) + + if (e.shiftKey) { + pos.quantize(10) + } + + var x = pos.a, y = pos.b + + if (base.down) { + base.cursor.x.b = x + base.cursor.y.b = y + base.tube("drag", e, base.cursor) + } + else { + base.cursor.x.a = base.cursor.x.b = x + base.cursor.y.a = base.cursor.y.b = y + base.tube("move", e, base.cursor) + } + } + base.mouseup = function(e){ + e.stopPropagation() + + if (base.down) { + base.down = false + base.tube("up", e, base.cursor) + var pos = positionFromMouse(e) + base.cursor = new rect(pos.a, pos.b) + } + } + + base.init() +} + diff --git a/assets/javascripts/rectangles/util/sort.js b/assets/javascripts/rectangles/util/sort.js new file mode 100644 index 0000000..8ece95f --- /dev/null +++ b/assets/javascripts/rectangles/util/sort.js @@ -0,0 +1,37 @@ +function sort_rooms_by_position(list){ + return list.sort(function(a,b){ + return compare_rect_position(a.rect, b.rect) + }) +} + +function sort_rects_by_position(list){ + return list.sort(compare_rect_position) +} + +function compare_rect_position(a,b){ + if (a.x.a < b.x.a) { + return -1 + } + if (a.x.a > b.x.a) { + return 1 + } + if (a.y.a < b.y.a) { + return -1 + } + if (a.y.a > b.y.a) { + return 1 + } + return 0 +} + +function sort_rects_by_area(list){ + return list.map(function(r){ return [r.area(), r] }).sort(function(a,b){ + if (a[0] < b[0]) { + return 1 + } + if (a[0] > b[0]) { + return -1 + } + return 0 + }).map(function(r){ return r[1] }) +} diff --git a/assets/javascripts/rectangles/util/wheel.js b/assets/javascripts/rectangles/util/wheel.js new file mode 100644 index 0000000..6836772 --- /dev/null +++ b/assets/javascripts/rectangles/util/wheel.js @@ -0,0 +1,62 @@ +/* + usage: + + base.wheel = new wheel({ + el: document.querySelector("#map"), + update: function(e, val, delta){ + // do something with val + }, + }) + +*/ + +function wheel (opt) { + opt = defaults(opt, { + el: document, + fn: function(e, val, delta){}, + propagate: false, + locked: false, + reversible: true, + ratio: 0.02, + val: 0, + }) + + opt.el.addEventListener('mousewheel', onMouseWheel, false); + opt.el.addEventListener('DOMMouseScroll', onMouseWheel, false); + + function onMouseWheel (e) { + if (opt.locked) { + return + } + if (! opt.propagate) { + e.stopPropagation() + e.preventDefault() + } + + var delta = 0; + + // WebKit + if ( event.wheelDeltaY ) { + delta -= event.wheelDeltaY * opt.ratio + } + // Opera / Explorer 9 + else if ( event.wheelDelta ) { + delta -= event.wheelDelta * opt.ratio + } + // Firefox + else if ( event.detail ) { + delta += event.detail * 2 + } + if (! opt.reversible && delta < 0) return; + + opt.val = clamp(opt.val + delta, opt.min, opt.max) + + opt.update(e, opt.val, delta) + } + + opt.lock = function(){ opt.locked = true } + opt.unlock = function(){ opt.locked = false } + + return opt +} + |
