Created
August 13, 2009 00:32
-
-
Save aalmiray/166875 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.swing.SwingBuilder | |
import griffon.builder.trident.* | |
import griffon.builder.gfx.* | |
import griffon.builder.gfx.swing.* | |
import java.awt.Color | |
import java.beans.PropertyChangeEvent | |
class SquareNode extends CustomGfxNode { | |
@GfxAttribute Color foreground = Colors.get("yellow") | |
@GfxAttribute Color background = Colors.get("black") | |
@GfxAttribute double x = 0 | |
@GfxAttribute double y = 0 | |
@GfxAttribute(alias="w") double width = 20 | |
@GfxAttribute(alias="h") double height = 20 | |
private final TridentBuilder trident | |
private timeline | |
private boolean rollover | |
private GfxNode square | |
SquareNode(TridentBuilder trident) { | |
this.trident = trident | |
} | |
DrawableNode createNode(GfxBuilder builder) { | |
builder.group(borderColor: 'none') { | |
square = rect(x: x, y: y, width: width, height: height, fill: background, | |
mouseEntered: onMouseIn, mouseExited: onMouseOut, mouseClicked: swapColor) | |
} | |
} | |
void onDirty(PropertyChangeEvent evt) { | |
if(evt.propertyName == "foreground" || evt.propertyName == "background") { | |
timeline = null | |
} | |
} | |
def onMouseIn = { | |
if(rollover) return | |
rollover = true | |
if(!timeline) { | |
timeline = trident.timeline(square, duration: 2500) { | |
interpolatedProperty("fill", from: foreground, to: background) | |
} | |
timeline.play() | |
} else { | |
timeline.replay() | |
} | |
} | |
def onMouseOut = { | |
rollover = false | |
} | |
def swapColor = { | |
foreground = foreground == Colors.get("yellow") ? Colors.get("red") : Colors.get("yellow") | |
} | |
} | |
def trident = new TridentBuilder() | |
def gfx = new GfxBuilder() | |
def node = gfx.group { | |
antialias true | |
background(color: color("black")) | |
(0..<20).each { i -> | |
(0..<20).each { j -> | |
customNode(new SquareNode(trident), x: i*20, y: j*20) | |
} | |
} | |
} | |
def swing = new SwingBuilder() | |
swing.edt { | |
frame(title: "Snake", size: [410,430], visible: true) { | |
widget(new GfxCanvas(), id: "canvas", node: node) | |
} | |
} | |
trident.swingRepaintTimeline(swing.canvas, loop: true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment