Created
September 12, 2015 22:01
-
-
Save GeePawHill/f7a4d3572f1641120e08 to your computer and use it in GitHub Desktop.
This file contains 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
package org.geepawhill.whiteboard.action; | |
import javafx.animation.Transition; | |
import javafx.geometry.Bounds; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.layout.Pane; | |
import javafx.scene.paint.Color; | |
import javafx.scene.text.Font; | |
import javafx.scene.text.Text; | |
import javafx.scene.text.TextAlignment; | |
import javafx.util.Duration; | |
public class EnterCenterTyping extends AbstractAction | |
{ | |
private Text text; | |
String value; | |
private double startX; | |
private double startY; | |
/////////////// | |
// This is mid-refactoring. Every AbstractAction has a transition member. I was just about to make that "transition=" line into | |
// super(transition). But there are AbstractAction-to-be's (in about five more minutes) that can't make their transition in advance, | |
// because the transition depends on parameters (like duration but there are others) that aren't known until runtime. | |
/////////////// | |
public EnterCenterTyping(BorderPane root, String id, String value, double startX, double startY) | |
{ | |
this.value = value; | |
this.startX = startX; | |
this.startY = startY; | |
this.duration = new Duration(value.length()*150); | |
makeNodes(root,id); | |
transition = makeTransition(); | |
} | |
private Transition makeTransition() | |
{ | |
return new Transition() | |
{ | |
{ | |
setCycleDuration(duration); | |
} | |
@Override | |
protected void interpolate(double frac) | |
{ | |
if(frac==0.0) showNodes(); | |
else animateNodes(frac); | |
} | |
}; | |
} | |
protected void showNodes() | |
{ | |
text.setVisible(true); | |
} | |
private void makeNodes(Pane root, String id) | |
{ | |
this.text = new Text(startX,startY,""); | |
text.setVisible(false); | |
text.setId(id); | |
text.setStroke(Color.RED); | |
text.setStrokeWidth(2); | |
text.setOpacity(.4d); | |
text.setFont(new Font("Viner Hand ITC",30d)); | |
text.setTextAlignment(TextAlignment.CENTER); | |
root.getChildren().add(text); | |
} | |
private void animateNodes(double frac) | |
{ | |
String partialValue = value.substring(0, (int) (frac*value.length())); | |
text.setText(partialValue); | |
Bounds bounds = text.getBoundsInLocal(); | |
text.setX(this.startX-bounds.getWidth()/2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment