Skip to main content

Enable effect

2 replies [Last post]
macdonag
Offline
Joined: 2008-02-27
Points: 0

Hi,
Is these a quick way of enabling/disabling an effect, in effect making the node pass through to it's children? I want to create a drop shadow on a rectangle, but only render it if it's selected. I could set the effect to null and reassign it as needed, but this seems a bit long winded. Basically, I think a way of enabling/disabling an SGEffect's effect if a good idea :-)

Cheers,
g

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
william13163
Offline
Joined: 2008-03-19
Points: 0

The way I do it is to use a sibling Leaf and simply attach/detach the sibling.

Like this:
[pre]
...
/
SGGroup
/ \
/ \
/ \
/ \
visual SGNode drop shadow effect Leaf (attach/detach OR make it invisible)
[/pre]

The code below is a snippet where my Leaf is either visible or invisible. Some of my other Leafs actually attach/detach from the parent group.

[code]
public class SGHighlightEffectLeaf extends SGLeaf implements SGMouseListener
{
private Color lineColor = new Color(0.5f, 0.2f, 0.2f);
private BasicStroke stroke = new BasicStroke(1.0f);

public void paint(Graphics2D g)
{
// Paint the highlight as rectangle.
if (isVisible())
{
SGHighlightEffectGroup parentGroup = (SGHighlightEffectGroup) getParent();
Rectangle2D rectangle = parentGroup.child.getBounds();

// BEGIN State management
AffineTransform gt = g.getTransform();
Color c = g.getColor();
Stroke s = g.getStroke();

g.setColor(lineColor);
stroke = new BasicStroke(1.0f / (float) gt.getScaleX());
g.setStroke(stroke);

if ((parentGroup.child instanceof SGImage))
{
g.drawRect((int) rectangle.getMinX(), (int) rectangle.getMinY(),
(int) rectangle.getWidth(), (int) rectangle.getHeight());
}
else
{
g.drawRect((int) rectangle.getMinX(), (int) rectangle.getMinY(),
(int) rectangle.getWidth()-1, (int) rectangle.getHeight()-1);
}

// END State management
g.setColor(c);
g.setStroke(s);
g.setTransform(gt);
}
}

public Rectangle2D getBounds(AffineTransform transform)
{

// Get the SGGroup that is containing this node.
SGHighlightEffectGroup parentGroup = (SGHighlightEffectGroup) getParent();
Rectangle2D rectangle = parentGroup.child.getBounds(transform);
if ((parentGroup.child instanceof SGImage)) {
rectangle.setFrame(rectangle.getMinX(), rectangle.getMinY(),
rectangle.getWidth(), rectangle.getHeight());
}
...
[/code]

I gleamed this from the Highlight example. Basically, you have your effect attach itself to a group node and interogate the group's first child. Something like:

[code]
public class SGHighlightEffectGroup extends SGGroup
{
final SGNode child;
private SGHighlightEffectLeaf highlightEffectLeaf = null;

public SGHighlightEffectGroup(SGNode child)
{
this.child = child;

highlightEffectLeaf = new SGHighlightEffectLeaf();
highlightEffectLeaf.setVisible(false);

// Route mouse events from the group into the leaf.
addMouseListener(highlightEffectLeaf);

add(child);
add(highlightEffectLeaf);
}
}

[/code]

So you have something configured like:
[pre]
...
/
SGHighlightEffectGroup
/ \
/ \
/ \
/ \
visual SGNode SGHighlightEffectLeaf
[/pre]

Hope that helps.

macdonag
Offline
Joined: 2008-02-27
Points: 0

Thanks. I went for the simple approach of reassigning the Effect in the SGEffect. It would still be nice to be able to enable/disable it with a single call as required though.

Cheers for your answer,
g