Skip to content

Commit ead77b3

Browse files
committed
feat: NotificationService now also accepts Node icon, closes #1149
1 parent efd3676 commit ead77b3

File tree

6 files changed

+62
-10
lines changed

6 files changed

+62
-10
lines changed

fxgl-samples/src/main/java/advanced/CustomServicesSample.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.almasb.fxgl.app.GameApplication;
1010
import com.almasb.fxgl.app.GameSettings;
1111
import com.almasb.fxgl.notification.NotificationService;
12+
import javafx.scene.Node;
1213
import javafx.scene.input.KeyCode;
1314
import javafx.scene.paint.Color;
1415

@@ -37,6 +38,11 @@ public void pushNotification(String message) {
3738
System.out.println("Notify: " + message);
3839
}
3940

41+
@Override
42+
public void pushNotification(String message, Node icon) {
43+
44+
}
45+
4046
@Override
4147
public void setTextColor(Color textColor) {
4248

fxgl-samples/src/main/java/intermediate/NotificationSample.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,23 @@
1313
import static com.almasb.fxgl.dsl.FXGL.*;
1414

1515
/**
16+
* Shows how to use the notification service.
1617
*
1718
* @author Almas Baimagambetov (AlmasB) ([email protected])
1819
*/
1920
public class NotificationSample extends GameApplication {
2021

2122
@Override
22-
protected void initSettings(GameSettings settings) { }
23+
protected void initSettings(GameSettings settings) {
24+
settings.setWidth(1280);
25+
settings.setHeight(720);
26+
}
2327

2428
@Override
2529
protected void initInput() {
26-
onKeyDown(KeyCode.F, "Notify", () -> getNotificationService().pushNotification("Hello! " + random(1, 10000)));
30+
onKeyDown(KeyCode.F, () -> getNotificationService().pushNotification("Hello! " + random(1, 10000)));
31+
32+
onKeyDown(KeyCode.G, () -> getNotificationService().pushNotification("Hello! " + random(1, 10000), texture("brick.png", 32, 32)));
2733
}
2834

2935
public static void main(String[] args) {

fxgl-scene/src/main/kotlin/com/almasb/fxgl/notification/Notification.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66

77
package com.almasb.fxgl.notification
88

9+
import javafx.scene.Node
10+
911
/**
1012
* Represents a notification message.
1113
*
1214
* @author Almas Baimagambetov (AlmasB) ([email protected])
1315
*/
14-
class Notification internal constructor(val message: String)
16+
class Notification internal constructor(
17+
val message: String,
18+
val icon: Node
19+
)

fxgl-scene/src/main/kotlin/com/almasb/fxgl/notification/NotificationService.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package com.almasb.fxgl.notification
88

99
import com.almasb.fxgl.core.EngineService
10+
import javafx.scene.Node
1011
import javafx.scene.paint.Color
1112

1213
/**
@@ -30,4 +31,9 @@ abstract class NotificationService : EngineService() {
3031
* Push a notification with given [message].
3132
*/
3233
abstract fun pushNotification(message: String)
34+
35+
/**
36+
* Push a notification with given [message] and [icon].
37+
*/
38+
abstract fun pushNotification(message: String, icon: Node)
3339
}

fxgl-scene/src/main/kotlin/com/almasb/fxgl/notification/impl/NotificationServiceProvider.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import com.almasb.fxgl.notification.Notification
1212
import com.almasb.fxgl.notification.NotificationService
1313
import com.almasb.fxgl.notification.view.NotificationView
1414
import com.almasb.fxgl.scene.SceneService
15+
import javafx.scene.Group
16+
import javafx.scene.Node
1517
import javafx.scene.paint.Color
1618
import javafx.util.Duration
1719
import java.util.*
@@ -63,7 +65,11 @@ class NotificationServiceProvider : NotificationService() {
6365
* @param message the text to show
6466
*/
6567
override fun pushNotification(message: String) {
66-
val notification = Notification(message)
68+
pushNotification(message, Group())
69+
}
70+
71+
override fun pushNotification(message: String, icon: Node) {
72+
val notification = Notification(message, icon)
6773

6874
queue.add(notification)
6975

fxgl-scene/src/main/kotlin/com/almasb/fxgl/notification/view/XboxNotificationView.kt

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import com.almasb.fxgl.animation.Animation
1010
import com.almasb.fxgl.animation.AnimationBuilder
1111
import com.almasb.fxgl.notification.Notification
1212
import javafx.geometry.Point2D
13+
import javafx.scene.Group
14+
import javafx.scene.Node
1315
import javafx.scene.shape.Circle
1416
import javafx.scene.shape.Rectangle
1517
import javafx.scene.text.Font
@@ -28,6 +30,8 @@ class XboxNotificationView : NotificationView() {
2830
*/
2931
private val circle = Circle(30.0, 30.0, 30.0)
3032

33+
private val iconViewHolder = Group()
34+
3135
private val bg = Rectangle(400.0, 57.0)
3236
private val bgClip = Rectangle(bg.width + 15.0, bg.height)
3337

@@ -70,13 +74,13 @@ class XboxNotificationView : NotificationView() {
7074
translateX = appWidth / 2 - bg.width / 2 + 200
7175
translateY = 50.0
7276

73-
centerTextX(text1, 65.0, 395.0)
77+
centerNodeX(text1, 65.0, 395.0)
7478

7579
scaleX = 0.0
7680
scaleY = 0.0
7781

7882
// make sure we only have circle for proper centering during the first animation
79-
children.setAll(circle)
83+
children.setAll(circle, iconViewHolder)
8084

8185
// move the whole view to left
8286
translateThis = AnimationBuilder()
@@ -125,13 +129,24 @@ class XboxNotificationView : NotificationView() {
125129
private var animText2: Animation<*>? = null
126130
private var animText1: Animation<*>? = null
127131

132+
private var iconViewFadeIn: Animation<*>? = null
133+
128134
override fun push(notification: Notification) {
129135
text2.text = notification.message
130136
text2.translateY = -35.0
131137

138+
iconViewHolder.children.setAll(notification.icon)
139+
centerNodeX(iconViewHolder, 0.0, 60.0)
140+
centerNodeY(iconViewHolder, 0.0, 60.0)
141+
142+
iconViewFadeIn = AnimationBuilder()
143+
.duration(Duration.seconds(1.33))
144+
.fadeIn(iconViewHolder)
145+
.build()
146+
132147
children.add(text2)
133148

134-
centerTextX(text2, 65.0, 395.0)
149+
centerNodeX(text2, 65.0, 395.0)
135150

136151
// move text 2 to replace text 1
137152
animText2 = AnimationBuilder()
@@ -159,6 +174,7 @@ class XboxNotificationView : NotificationView() {
159174

160175
animText2!!.start()
161176
animText1!!.start()
177+
iconViewFadeIn!!.start()
162178
}
163179

164180
override fun playOutAnimation() {
@@ -169,9 +185,10 @@ class XboxNotificationView : NotificationView() {
169185

170186
translateBG?.onFinished = Runnable {
171187

172-
children.setAll(circle)
188+
children.setAll(circle, iconViewHolder)
173189

174190
scale = AnimationBuilder()
191+
.onFinished { iconViewHolder.children.clear() }
175192
.duration(Duration.seconds(0.3))
176193
.scale(this)
177194
.from(Point2D(1.0, 1.0))
@@ -192,9 +209,15 @@ class XboxNotificationView : NotificationView() {
192209

193210
animText2?.onUpdate(tpf)
194211
animText1?.onUpdate(tpf)
212+
213+
iconViewFadeIn?.onUpdate(tpf)
214+
}
215+
216+
private fun centerNodeX(node: Node, minX: Double, maxX: Double) {
217+
node.translateX = (minX + maxX) / 2 - node.layoutBounds.width / 2
195218
}
196219

197-
private fun centerTextX(text: Text, minX: Double, maxX: Double) {
198-
text.translateX = (minX + maxX) / 2 - text.layoutBounds.width / 2
220+
private fun centerNodeY(node: Node, minY: Double, maxY: Double) {
221+
node.translateY = (minY + maxY) / 2 - node.layoutBounds.height / 2
199222
}
200223
}

0 commit comments

Comments
 (0)