1 /* 2 * Copyright (c) 2016 Helmut Neemann 3 * Use of this source code is governed by the GPL v3 license 4 * that can be found in the LICENSE file. 5 */ 6 package de.neemann.digital.draw.shapes; 7 8 import de.neemann.digital.core.element.ElementAttributes; 9 import de.neemann.digital.core.element.Keys; 10 import de.neemann.digital.core.element.PinDescriptions; 11 import de.neemann.digital.draw.elements.IOState; 12 import de.neemann.digital.draw.elements.Pin; 13 import de.neemann.digital.draw.elements.Pins; 14 import de.neemann.digital.draw.graphics.*; 15 16 import static de.neemann.digital.draw.shapes.GenericShape.SIZE; 17 18 /** 19 * The Demuxer shape 20 */ 21 public class DemuxerShape implements Shape { 22 private final int outputCount; 23 private final boolean hasInput; 24 private final boolean flip; 25 private final int height; 26 private final PinDescriptions inputs; 27 private final PinDescriptions outputs; 28 private Pins pins; 29 30 /** 31 * Creates a new instance 32 * 33 * @param attr the attributes 34 * @param inputs the inputs 35 * @param outputs the outputs 36 */ DemuxerShape(ElementAttributes attr, PinDescriptions inputs, PinDescriptions outputs)37 public DemuxerShape(ElementAttributes attr, PinDescriptions inputs, PinDescriptions outputs) { 38 this.inputs = inputs; 39 this.outputs = outputs; 40 this.flip = attr.get(Keys.FLIP_SEL_POSITON); 41 outputCount = 1 << attr.get(Keys.SELECTOR_BITS); 42 hasInput = inputs.size() > 1; 43 height = hasInput || (outputCount <= 2) ? outputCount * SIZE : (outputCount - 1) * SIZE; 44 } 45 46 @Override getPins()47 public Pins getPins() { 48 if (pins == null) { 49 pins = new Pins(); 50 pins.add(new Pin(new Vector(SIZE, flip ? 0 : height), inputs.get(0))); 51 if (outputCount == 2) { 52 pins.add(new Pin(new Vector(SIZE * 2, 0 * SIZE), outputs.get(0))); 53 pins.add(new Pin(new Vector(SIZE * 2, 2 * SIZE), outputs.get(1))); 54 } else 55 for (int i = 0; i < outputCount; i++) { 56 pins.add(new Pin(new Vector(SIZE * 2, i * SIZE), outputs.get(i))); 57 } 58 if (hasInput) 59 pins.add(new Pin(new Vector(0, (outputCount / 2) * SIZE), inputs.get(1))); 60 } 61 return pins; 62 } 63 64 @Override applyStateMonitor(IOState ioState)65 public Interactor applyStateMonitor(IOState ioState) { 66 return null; 67 } 68 69 @Override drawTo(Graphic graphic, Style highLight)70 public void drawTo(Graphic graphic, Style highLight) { 71 graphic.drawPolygon(new Polygon(true) 72 .add(1, 5) 73 .add(SIZE * 2 - 1, -4) 74 .add(SIZE * 2 - 1, height + 4) 75 .add(1, height - 5), Style.NORMAL); 76 graphic.drawText(new Vector(SIZE * 2 - 3, 2), "0", Orientation.RIGHTTOP, Style.SHAPE_PIN); 77 } 78 } 79