-
-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added samples for fxgl-intelligence features
- Loading branch information
Showing
2 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
fxgl-samples/src/main/java/sandbox/net/HandTrackingSample.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* FXGL - JavaFX Game Library. The MIT License (MIT). | ||
* Copyright (c) AlmasB (almaslvl@gmail.com). | ||
* See LICENSE for details. | ||
*/ | ||
|
||
package sandbox.net; | ||
|
||
import com.almasb.fxgl.app.GameApplication; | ||
import com.almasb.fxgl.app.GameSettings; | ||
import com.almasb.fxgl.core.math.Vec2; | ||
import com.almasb.fxgl.gesturerecog.HandTrackingService; | ||
import javafx.geometry.Point2D; | ||
import javafx.scene.canvas.Canvas; | ||
import javafx.scene.canvas.GraphicsContext; | ||
import javafx.scene.paint.Color; | ||
import javafx.scene.shape.Circle; | ||
import javafx.util.Duration; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.almasb.fxgl.dsl.FXGL.*; | ||
|
||
/** | ||
* | ||
* | ||
* @author Almas Baim (https://github.com/AlmasB) | ||
*/ | ||
public class HandTrackingSample extends GameApplication { | ||
|
||
private GraphicsContext g; | ||
|
||
private boolean isDrawing = true; | ||
private double oldX = -1; | ||
private double oldY = -1; | ||
private Circle pointer; | ||
|
||
private List<Point2D> points; | ||
|
||
@Override | ||
protected void initSettings(GameSettings settings) { | ||
settings.setWidth(1600); | ||
settings.setHeight(900); | ||
settings.addEngineService(HandTrackingService.class); | ||
} | ||
|
||
@Override | ||
protected void initGame() { | ||
points = new ArrayList<>(); | ||
|
||
getService(HandTrackingService.class).addInputHandler(hand -> { | ||
getExecutor().startAsyncFX(() -> { | ||
|
||
var dist = hand.getPoints().get(4).distance(hand.getPoints().get(8)); | ||
|
||
isDrawing = dist < 0.06; | ||
|
||
var indexFingerTip = hand.getPoints().get(8); | ||
|
||
pointer.setTranslateX(Math.round((1 - indexFingerTip.getX()) * getAppWidth())); | ||
pointer.setTranslateY(Math.round(indexFingerTip.getY() * getAppHeight())); | ||
|
||
if (!isDrawing) { | ||
oldX = -1; | ||
oldY = -1; | ||
|
||
if (points.size() >= 2) { | ||
g.clearRect(0, 0, getAppWidth(), getAppHeight()); | ||
|
||
var v0 = points.get(0); | ||
var v1 = points.getLast(); | ||
|
||
g.strokeLine(v0.getX(), v0.getY(), v1.getX(), v1.getY()); | ||
} | ||
|
||
points.clear(); | ||
return; | ||
} | ||
|
||
if (oldX != -1 && oldY != -1) { | ||
var v = new Vec2(pointer.getTranslateX(), pointer.getTranslateY()) | ||
.subLocal(oldX, oldY) | ||
.mulLocal(0.85) | ||
.addLocal(oldX, oldY); | ||
|
||
g.strokeLine(oldX, oldY, v.x, v.y); | ||
//g.strokeLine(oldX, oldY, pointer.getTranslateX(), pointer.getTranslateY()); | ||
|
||
points.add(v.toPoint2D()); | ||
} | ||
|
||
oldX = pointer.getTranslateX(); | ||
oldY = pointer.getTranslateY(); | ||
}); | ||
}); | ||
|
||
getService(HandTrackingService.class).start(); | ||
|
||
run(() -> { | ||
g.clearRect(0, 0, getAppWidth(), getAppHeight()); | ||
}, Duration.seconds(50)); | ||
} | ||
|
||
@Override | ||
protected void initUI() { | ||
pointer = new Circle(5, Color.RED); | ||
|
||
var canvas = new Canvas(getAppWidth(), getAppHeight()); | ||
g = canvas.getGraphicsContext2D(); | ||
g.setStroke(Color.RED); | ||
g.setLineWidth(6); | ||
|
||
addUINode(canvas); | ||
} | ||
|
||
public static void main(String[] args) { | ||
launch(args); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* FXGL - JavaFX Game Library. The MIT License (MIT). | ||
* Copyright (c) AlmasB (almaslvl@gmail.com). | ||
* See LICENSE for details. | ||
*/ | ||
|
||
package sandbox.net; | ||
|
||
import com.almasb.fxgl.app.GameApplication; | ||
import com.almasb.fxgl.app.GameSettings; | ||
import com.almasb.fxgl.intelligence.tts.TextToSpeechService; | ||
import com.almasb.fxgl.speechrecog.SpeechRecognitionService; | ||
import com.almasb.fxgl.ui.FontType; | ||
import javafx.scene.control.Button; | ||
import javafx.scene.control.TextArea; | ||
|
||
import static com.almasb.fxgl.dsl.FXGL.*; | ||
|
||
/** | ||
* | ||
* | ||
* @author Almas Baim (https://github.com/AlmasB) | ||
*/ | ||
public class TTSSample extends GameApplication { | ||
|
||
private TextArea output; | ||
|
||
@Override | ||
protected void initSettings(GameSettings settings) { | ||
settings.addEngineService(SpeechRecognitionService.class); | ||
settings.addEngineService(TextToSpeechService.class); | ||
} | ||
|
||
@Override | ||
protected void initGame() { | ||
getService(SpeechRecognitionService.class).addInputHandler(input -> { | ||
if (input.isEmpty() || input.trim().isEmpty()) | ||
return; | ||
|
||
getService(TextToSpeechService.class).speak(input); | ||
}); | ||
|
||
//getService(SpeechRecognitionService.class).start(); | ||
getService(TextToSpeechService.class).start(); | ||
} | ||
|
||
@Override | ||
protected void initUI() { | ||
output = new TextArea(); | ||
output.setWrapText(true); | ||
output.setPrefSize(getAppWidth(), getAppHeight() - 200); | ||
output.setFont(getUIFactoryService().newFont(FontType.MONO, 18)); | ||
|
||
addUINode(output); | ||
|
||
var btn = new Button("Speak"); | ||
btn.setOnAction(e -> { | ||
getService(TextToSpeechService.class).speak(output.getText()); | ||
}); | ||
|
||
addUINode(btn, 50, getAppHeight() - 150); | ||
} | ||
|
||
public static void main(String[] args) { | ||
launch(args); | ||
} | ||
} |