From b9ff268133e5dcfd499d90361e45d4168fa96063 Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Fri, 7 Feb 2020 20:10:28 -0500 Subject: [PATCH] Add a button to order entities by height --- macrovision.html | 1 + macrovision.js | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/macrovision.html b/macrovision.html index 2d4ab1a7..4eae4021 100644 --- a/macrovision.html +++ b/macrovision.html @@ -21,6 +21,7 @@
diff --git a/macrovision.js b/macrovision.js index dcdf8691..0a0dafa6 100644 --- a/macrovision.js +++ b/macrovision.js @@ -483,6 +483,17 @@ function testClick(event) { } } +function arrangeEntities(order) { + let x = 0.1; + + order.forEach(key => { + document.querySelector("#entity-" + key).dataset.x = x; + x += 0.8 / order.length + }); + + updateSizes(); +} + function removeAllEntities() { Object.keys(entities).forEach(key => { removeEntity(document.querySelector("#entity-" + key)); @@ -599,6 +610,21 @@ document.addEventListener("DOMContentLoaded", () => { document.querySelector("#menu-clear").addEventListener("click", e => { removeAllEntities(); }); + + document.querySelector("#menu-order").addEventListener("click", e => { + const order = Object.keys(entities).sort((a,b) => { + const entA = entities[a]; + const entB = entities[b]; + const viewA = document.querySelector("#entity-" + a).dataset.view; + const viewB = document.querySelector("#entity-" + b).dataset.view; + const heightA = entA.views[viewA].height.to("meter").value; + const heightB = entB.views[viewB].height.to("meter").value; + return heightA - heightB; + }); + + arrangeEntities(order); + }); + prepareEntities(); });