From 1069dfcaf4ccbab3cf11283fbb8fef86309064fa Mon Sep 17 00:00:00 2001 From: Luc <8822552+luc-github@users.noreply.github.com> Date: Sat, 15 Aug 2020 14:42:44 +0200 Subject: [PATCH] Add UI for mixed extruder --- src/components/printer/extrusion.js | 201 +++++++++++++++++++++- src/components/printer/preferences.js | 8 +- src/server/public/preferences.json | 2 +- src/stylesheets/components/_controls.scss | 9 + 4 files changed, 217 insertions(+), 3 deletions(-) diff --git a/src/components/printer/extrusion.js b/src/components/printer/extrusion.js index b0cf29e6..43b5ec89 100644 --- a/src/components/printer/extrusion.js +++ b/src/components/printer/extrusion.js @@ -22,7 +22,7 @@ import { h } from "preact" import { T } from "../translations" import { X } from "preact-feather" import { useStoreon } from "storeon/preact" -import { preferences, getPanelIndex } from "../app" +import { esp3dSettings, preferences, getPanelIndex } from "../app" import { useEffect } from "preact/hooks" import { SendCommand } from "../http" import { showDialog } from "../dialog" @@ -37,6 +37,8 @@ let currentFeedRate let extrudeDistance = 10 let userextrudeDistance = 5 let userextrudeselected = false +let mixedpercent = [] +let islocked = [] /* * Send command query error @@ -190,6 +192,199 @@ const FeedRateInput = ({ id, label }) => { ) } +/* + * CheckboxControl + */ +const CheckboxControl = ({ id, label }) => { + let ids = id.split("_") + if (typeof islocked[parseInt(ids[1])] == "undefined") + islocked[parseInt(ids[1])] = false + const toggleCheckbox = e => { + if (e.target.checked && !canLockMixer()) e.target.checked = false + islocked[parseInt(ids[1])] = e.target.checked + } + return ( + + ) +} + +function adjustMixer(id, ignorezero = false) { + let total = -100 + let next = -1 + let value + total = -100 + for (let i = 0; i < preferences.settings.enumber; i++) { + total += parseFloat(mixedpercent[i]) + if (!(i == id || islocked[i]) && next == -1) { + if (ignorezero) { + if (parseFloat(mixedpercent[i]) != 0) { + value = parseFloat(mixedpercent[i]) + next = i + } + } else { + value = parseFloat(mixedpercent[i]) + next = i + } + } + } + if (total == 0) { + return + } + if (total < 0) { + let newval = value - total + mixedpercent[next] = newval + } else { + let newval = value - total + if (newval < 0) { + newval = 0 + mixedpercent[next] = newval + if (canDecrease(id)) { + adjustMixer(id, true) + } else { + let lockvalue = mixedpercent[id] - total + mixedpercent[id] = lockvalue + } + } else { + mixedpercent[next] = newval + } + } + for (let i = 0; i < preferences.settings.enumber; i++) { + document.getElementById("inputmixed_" + i).value = mixedpercent[i] + document.getElementById("slidermixed_" + i).value = mixedpercent[i] + } +} + +function canDecrease(id) { + for (let i = 0; i < preferences.settings.enumber; i++) { + if (!islocked[i] && i != id && mixedpercent[i] > 0) return true + } + return false +} + +function canLockMixer() { + let total = 0 + for (let i = 0; i < preferences.settings.enumber; i++) { + if (islocked[i]) total++ + } + if (preferences.settings.enumber - total > 2) return true + return false +} + +const MixedExtrusionPanel = ({ visible }) => { + if (!visible || esp3dSettings.FWTarget == "smoothieware") return null + const onChange = e => { + let id = e.target.id.split("_") + if (islocked[parseInt(id[1])]) { + e.target.value = mixedpercent[parseInt(id[1])] + return + } + mixedpercent[parseInt(id[1])] = parseInt(e.target.value) + document.getElementById("inputmixed_" + id[1]).value = e.target.value + adjustMixer(parseInt(id[1])) + } + const onInput = e => { + let id = e.target.id.split("_") + if (islocked[parseInt(id[1])]) { + e.target.value = mixedpercent[parseInt(id[1])] + return + } + if (e.target.value < 0 || isNaN(e.target.value)) e.target.value = 0 + if (e.target.value.length == 0) { + mixedpercent[parseInt(id[1])] = 0 + e.target.value = "" + } else mixedpercent[parseInt(id[1])] = parseInt(e.target.value) + document.getElementById("slidermixed_" + id[1]).value = + mixedpercent[parseInt(id[1])] + adjustMixer(parseInt(id[1])) + } + let panel = [] + let bgcolors = preferences.settings.ecolors.split(";") + for (let i = 0; i < preferences.settings.enumber; i++) { + let line = [] + let colorbg + if (typeof bgcolors[i] != "undefined") { + colorbg = "background-color:" + bgcolors[i] + } + if (typeof mixedpercent[i] == "undefined") mixedpercent[i] = 0 + line.push( +
2 + ? "justify-content-center" + : "d-none" + } + > + {" "} +
+ ) + line.push( +
+ + {String.fromCharCode(65 + i)} + +
+ ) + line.push(
) + line.push( +
+
+ +
+
+ ) + line.push( +
+
+ +
+ + % + +
+
+
+ ) + panel.push( +
+ {line} +
+ ) + } + return
{panel}
+} + /* * Extrusion panel control * @@ -395,6 +590,10 @@ const ExtrusionPanel = () => {
+ +