Small components are easier to select because also the text is selectable.

This commit is contained in:
hneemann 2017-03-08 22:21:58 +01:00
parent b8f2442276
commit 4f9239697e
4 changed files with 43 additions and 25 deletions

View File

@ -394,7 +394,7 @@ public class Circuit {
*/
public VisualElement getElementAt(Vector pos) {
for (VisualElement element : visualElements) {
if (element.matches(pos))
if (element.matches(pos, false))
return element;
}
return null;
@ -406,10 +406,10 @@ public class Circuit {
* @param pos the cursor 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<>();
for (VisualElement element : visualElements) {
if (element.matches(pos))
if (element.matches(pos, includeText))
list.add(element);
}
return list;

View File

@ -24,6 +24,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
private static final int PIN = 2;
private transient GraphicMinMax minMax;
private transient GraphicMinMax minMaxText;
private transient IOState ioState;
private transient InteractorInterface interactor;
private transient Element element;
@ -82,8 +83,13 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
@Override
public void attributeChanged(Key key) {
shape = null;
resetGeometry();
}
private void resetGeometry() {
transform = null;
minMax = null;
minMaxText = null;
}
/**
@ -101,8 +107,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
*/
public VisualElement setPos(Vector pos) {
this.pos = pos;
minMax = null;
transform = null;
resetGeometry();
return this;
}
@ -112,8 +117,8 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
* @param p a position
* @return true if p is inside the bounding box of the shape of this element.
*/
public boolean matches(Vector p) {
GraphicMinMax m = getMinMax();
public boolean matches(Vector p, boolean includeText) {
GraphicMinMax m = getMinMax(includeText);
return (m.getMin().x <= p.x)
&& (m.getMin().y <= p.y)
&& (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
*/
public boolean matches(Vector min, Vector max) {
GraphicMinMax m = getMinMax();
GraphicMinMax m = getMinMax(false);
return (min.x <= m.getMin().x)
&& (m.getMax().x <= max.x)
&& (min.y <= m.getMin().y)
@ -151,8 +156,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
public Shape getShape() {
if (shape == null) {
shape = shapeFactory.getShape(elementName, elementAttributes);
minMax = null;
transform = null;
resetGeometry();
}
return shape;
}
@ -163,7 +167,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
// draw circle around element
if (highLight) {
GraphicMinMax mm = getMinMax();
GraphicMinMax mm = getMinMax(false);
Vector delta = mm.getMax().sub(mm.getMin()).add(SIZE, SIZE).div(2);
Vector pos = mm.getMax().add(mm.getMin()).div(2);
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
*/
public GraphicMinMax getMinMax() {
if (minMax == null) {
GraphicMinMax mm = new GraphicMinMax(false);
drawShape(mm, false);
minMax = mm;
public GraphicMinMax getMinMax(boolean includeText) {
if (includeText) {
if (minMaxText == null) {
GraphicMinMax mm = new GraphicMinMax(true);
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

View File

@ -715,20 +715,24 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
super(cursor);
}
@Override
void clicked(MouseEvent e) {
Vector pos = getPosVector(e);
private VisualElement getVisualElement(Vector pos, boolean includeText) {
VisualElement vp = null;
List<VisualElement> list = circuit.getElementListAt(pos);
List<VisualElement> list = circuit.getElementListAt(pos, includeText);
if (list.size() == 1)
vp = list.get(0);
else if (list.size() > 1) {
ItemPicker<VisualElement> picker = new ItemPicker<VisualElement>(CircuitComponent.this, list);
vp = picker.select();
}
return vp;
}
@Override
void clicked(MouseEvent e) {
Vector pos = getPosVector(e);
if (e.getButton() == MouseEvent.BUTTON3) {
VisualElement vp = getVisualElement(pos, true);
if (vp != null)
editAttributes(vp, e);
else {
@ -737,6 +741,7 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
mouseMoveWire.activate(wire, pos);
}
} else if (e.getButton() == MouseEvent.BUTTON1) {
VisualElement vp = getVisualElement(pos, false);
if (vp != null) {
if (circuit.isPinPos(raster(pos), vp))
mouseWire.activate(pos);
@ -786,7 +791,7 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
@Override
void moved(MouseEvent e) {
if (delta == null) {
GraphicMinMax minMax = element.getMinMax();
GraphicMinMax minMax = element.getMinMax(false);
delta = element.getPos().sub(minMax.getMax());
}
element.setPos(raster(getPosVector(e).add(delta)));
@ -1274,7 +1279,7 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
}
private VisualElement getInteractableElementAt(MouseEvent e) {
List<VisualElement> elementList = circuit.getElementListAt(getPosVector(e));
List<VisualElement> elementList = circuit.getElementListAt(getPosVector(e), false);
for (VisualElement ve : elementList) {
if (ve.isInteractable())
return ve;

View File

@ -80,7 +80,7 @@ public class CircuitTransferable implements Transferable {
for (Moveable m : elements)
if (m instanceof VisualElement) {
((VisualElement) m).setShapeFactory(shapeFactory);
GraphicMinMax mm = ((VisualElement) m).getMinMax();
GraphicMinMax mm = ((VisualElement) m).getMinMax(false);
if (max == null)
max = mm.getMax();
else