mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-29 07:50:29 -04:00
Small components are easier to select because also the text is selectable.
This commit is contained in:
parent
b8f2442276
commit
4f9239697e
@ -394,7 +394,7 @@ public class Circuit {
|
|||||||
*/
|
*/
|
||||||
public VisualElement getElementAt(Vector pos) {
|
public VisualElement getElementAt(Vector pos) {
|
||||||
for (VisualElement element : visualElements) {
|
for (VisualElement element : visualElements) {
|
||||||
if (element.matches(pos))
|
if (element.matches(pos, false))
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@ -406,10 +406,10 @@ public class Circuit {
|
|||||||
* @param pos the cursor position
|
* @param pos the cursor position
|
||||||
* @return the elements or an empty list if there is no element at the given position
|
* @return the elements or an empty list if there is no element at the given position
|
||||||
*/
|
*/
|
||||||
public List<VisualElement> getElementListAt(Vector pos) {
|
public List<VisualElement> getElementListAt(Vector pos, boolean includeText) {
|
||||||
ArrayList<VisualElement> list = new ArrayList<>();
|
ArrayList<VisualElement> list = new ArrayList<>();
|
||||||
for (VisualElement element : visualElements) {
|
for (VisualElement element : visualElements) {
|
||||||
if (element.matches(pos))
|
if (element.matches(pos, includeText))
|
||||||
list.add(element);
|
list.add(element);
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
|
@ -24,6 +24,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
|
|||||||
private static final int PIN = 2;
|
private static final int PIN = 2;
|
||||||
|
|
||||||
private transient GraphicMinMax minMax;
|
private transient GraphicMinMax minMax;
|
||||||
|
private transient GraphicMinMax minMaxText;
|
||||||
private transient IOState ioState;
|
private transient IOState ioState;
|
||||||
private transient InteractorInterface interactor;
|
private transient InteractorInterface interactor;
|
||||||
private transient Element element;
|
private transient Element element;
|
||||||
@ -82,8 +83,13 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
|
|||||||
@Override
|
@Override
|
||||||
public void attributeChanged(Key key) {
|
public void attributeChanged(Key key) {
|
||||||
shape = null;
|
shape = null;
|
||||||
|
resetGeometry();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resetGeometry() {
|
||||||
transform = null;
|
transform = null;
|
||||||
minMax = null;
|
minMax = null;
|
||||||
|
minMaxText = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -101,8 +107,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
|
|||||||
*/
|
*/
|
||||||
public VisualElement setPos(Vector pos) {
|
public VisualElement setPos(Vector pos) {
|
||||||
this.pos = pos;
|
this.pos = pos;
|
||||||
minMax = null;
|
resetGeometry();
|
||||||
transform = null;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,8 +117,8 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
|
|||||||
* @param p a position
|
* @param p a position
|
||||||
* @return true if p is inside the bounding box of the shape of this element.
|
* @return true if p is inside the bounding box of the shape of this element.
|
||||||
*/
|
*/
|
||||||
public boolean matches(Vector p) {
|
public boolean matches(Vector p, boolean includeText) {
|
||||||
GraphicMinMax m = getMinMax();
|
GraphicMinMax m = getMinMax(includeText);
|
||||||
return (m.getMin().x <= p.x)
|
return (m.getMin().x <= p.x)
|
||||||
&& (m.getMin().y <= p.y)
|
&& (m.getMin().y <= p.y)
|
||||||
&& (p.x <= m.getMax().x)
|
&& (p.x <= m.getMax().x)
|
||||||
@ -128,7 +133,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
|
|||||||
* @return true if the given box completely contains this element
|
* @return true if the given box completely contains this element
|
||||||
*/
|
*/
|
||||||
public boolean matches(Vector min, Vector max) {
|
public boolean matches(Vector min, Vector max) {
|
||||||
GraphicMinMax m = getMinMax();
|
GraphicMinMax m = getMinMax(false);
|
||||||
return (min.x <= m.getMin().x)
|
return (min.x <= m.getMin().x)
|
||||||
&& (m.getMax().x <= max.x)
|
&& (m.getMax().x <= max.x)
|
||||||
&& (min.y <= m.getMin().y)
|
&& (min.y <= m.getMin().y)
|
||||||
@ -151,8 +156,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
|
|||||||
public Shape getShape() {
|
public Shape getShape() {
|
||||||
if (shape == null) {
|
if (shape == null) {
|
||||||
shape = shapeFactory.getShape(elementName, elementAttributes);
|
shape = shapeFactory.getShape(elementName, elementAttributes);
|
||||||
minMax = null;
|
resetGeometry();
|
||||||
transform = null;
|
|
||||||
}
|
}
|
||||||
return shape;
|
return shape;
|
||||||
}
|
}
|
||||||
@ -163,7 +167,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
|
|||||||
|
|
||||||
// draw circle around element
|
// draw circle around element
|
||||||
if (highLight) {
|
if (highLight) {
|
||||||
GraphicMinMax mm = getMinMax();
|
GraphicMinMax mm = getMinMax(false);
|
||||||
Vector delta = mm.getMax().sub(mm.getMin()).add(SIZE, SIZE).div(2);
|
Vector delta = mm.getMax().sub(mm.getMin()).add(SIZE, SIZE).div(2);
|
||||||
Vector pos = mm.getMax().add(mm.getMin()).div(2);
|
Vector pos = mm.getMax().add(mm.getMin()).div(2);
|
||||||
graphic.drawCircle(pos.sub(delta), pos.add(delta), Style.HIGHLIGHT);
|
graphic.drawCircle(pos.sub(delta), pos.add(delta), Style.HIGHLIGHT);
|
||||||
@ -193,13 +197,22 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
|
|||||||
/**
|
/**
|
||||||
* @return the bounding box of the shape of this element, text is ignored
|
* @return the bounding box of the shape of this element, text is ignored
|
||||||
*/
|
*/
|
||||||
public GraphicMinMax getMinMax() {
|
public GraphicMinMax getMinMax(boolean includeText) {
|
||||||
if (minMax == null) {
|
if (includeText) {
|
||||||
GraphicMinMax mm = new GraphicMinMax(false);
|
if (minMaxText == null) {
|
||||||
drawShape(mm, false);
|
GraphicMinMax mm = new GraphicMinMax(true);
|
||||||
minMax = mm;
|
drawShape(mm, false);
|
||||||
|
minMaxText = mm;
|
||||||
|
}
|
||||||
|
return minMaxText;
|
||||||
|
} else {
|
||||||
|
if (minMax == null) {
|
||||||
|
GraphicMinMax mm = new GraphicMinMax(false);
|
||||||
|
drawShape(mm, false);
|
||||||
|
minMax = mm;
|
||||||
|
}
|
||||||
|
return minMax;
|
||||||
}
|
}
|
||||||
return minMax;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -715,20 +715,24 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
|
|||||||
super(cursor);
|
super(cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private VisualElement getVisualElement(Vector pos, boolean includeText) {
|
||||||
void clicked(MouseEvent e) {
|
|
||||||
Vector pos = getPosVector(e);
|
|
||||||
VisualElement vp = null;
|
VisualElement vp = null;
|
||||||
|
List<VisualElement> list = circuit.getElementListAt(pos, includeText);
|
||||||
List<VisualElement> list = circuit.getElementListAt(pos);
|
|
||||||
if (list.size() == 1)
|
if (list.size() == 1)
|
||||||
vp = list.get(0);
|
vp = list.get(0);
|
||||||
else if (list.size() > 1) {
|
else if (list.size() > 1) {
|
||||||
ItemPicker<VisualElement> picker = new ItemPicker<VisualElement>(CircuitComponent.this, list);
|
ItemPicker<VisualElement> picker = new ItemPicker<VisualElement>(CircuitComponent.this, list);
|
||||||
vp = picker.select();
|
vp = picker.select();
|
||||||
}
|
}
|
||||||
|
return vp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void clicked(MouseEvent e) {
|
||||||
|
Vector pos = getPosVector(e);
|
||||||
|
|
||||||
if (e.getButton() == MouseEvent.BUTTON3) {
|
if (e.getButton() == MouseEvent.BUTTON3) {
|
||||||
|
VisualElement vp = getVisualElement(pos, true);
|
||||||
if (vp != null)
|
if (vp != null)
|
||||||
editAttributes(vp, e);
|
editAttributes(vp, e);
|
||||||
else {
|
else {
|
||||||
@ -737,6 +741,7 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
|
|||||||
mouseMoveWire.activate(wire, pos);
|
mouseMoveWire.activate(wire, pos);
|
||||||
}
|
}
|
||||||
} else if (e.getButton() == MouseEvent.BUTTON1) {
|
} else if (e.getButton() == MouseEvent.BUTTON1) {
|
||||||
|
VisualElement vp = getVisualElement(pos, false);
|
||||||
if (vp != null) {
|
if (vp != null) {
|
||||||
if (circuit.isPinPos(raster(pos), vp))
|
if (circuit.isPinPos(raster(pos), vp))
|
||||||
mouseWire.activate(pos);
|
mouseWire.activate(pos);
|
||||||
@ -786,7 +791,7 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
|
|||||||
@Override
|
@Override
|
||||||
void moved(MouseEvent e) {
|
void moved(MouseEvent e) {
|
||||||
if (delta == null) {
|
if (delta == null) {
|
||||||
GraphicMinMax minMax = element.getMinMax();
|
GraphicMinMax minMax = element.getMinMax(false);
|
||||||
delta = element.getPos().sub(minMax.getMax());
|
delta = element.getPos().sub(minMax.getMax());
|
||||||
}
|
}
|
||||||
element.setPos(raster(getPosVector(e).add(delta)));
|
element.setPos(raster(getPosVector(e).add(delta)));
|
||||||
@ -1274,7 +1279,7 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
|
|||||||
}
|
}
|
||||||
|
|
||||||
private VisualElement getInteractableElementAt(MouseEvent e) {
|
private VisualElement getInteractableElementAt(MouseEvent e) {
|
||||||
List<VisualElement> elementList = circuit.getElementListAt(getPosVector(e));
|
List<VisualElement> elementList = circuit.getElementListAt(getPosVector(e), false);
|
||||||
for (VisualElement ve : elementList) {
|
for (VisualElement ve : elementList) {
|
||||||
if (ve.isInteractable())
|
if (ve.isInteractable())
|
||||||
return ve;
|
return ve;
|
||||||
|
@ -80,7 +80,7 @@ public class CircuitTransferable implements Transferable {
|
|||||||
for (Moveable m : elements)
|
for (Moveable m : elements)
|
||||||
if (m instanceof VisualElement) {
|
if (m instanceof VisualElement) {
|
||||||
((VisualElement) m).setShapeFactory(shapeFactory);
|
((VisualElement) m).setShapeFactory(shapeFactory);
|
||||||
GraphicMinMax mm = ((VisualElement) m).getMinMax();
|
GraphicMinMax mm = ((VisualElement) m).getMinMax(false);
|
||||||
if (max == null)
|
if (max == null)
|
||||||
max = mm.getMax();
|
max = mm.getMax();
|
||||||
else
|
else
|
||||||
|
Loading…
x
Reference in New Issue
Block a user