Skip to content

Commit

Permalink
Merge pull request #42 from msi-se/paper-plane-animation
Browse files Browse the repository at this point in the history
Paper plane animation
  • Loading branch information
Fabi-02 authored Apr 14, 2024
2 parents f0732e7 + 7e9a228 commit fcc8f9f
Show file tree
Hide file tree
Showing 11 changed files with 500 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package de.htwg_konstanz.mobilelearning.services.quiz.socket;

import org.bson.types.ObjectId;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import de.htwg_konstanz.mobilelearning.helper.ObjectIdTypeAdapter;

public class Fun {

public String action; // THROW_PAPER_PLANE
public double percentageX;
public double percentageY;

public Fun(String message) {


Gson gson = new GsonBuilder().registerTypeAdapter(ObjectId.class, new ObjectIdTypeAdapter()).create();
Fun funString = gson.fromJson(message, Fun.class);
this.action = funString.action;
this.percentageX = funString.percentageX;
this.percentageY = funString.percentageY;

System.out.println("Fun Action: " + this.action);
System.out.println("Fun percentageX: " + this.percentageX);
System.out.println("Fun percentageY: " + this.percentageY);
}

public Fun(String action, double percentageX, double percentageY) {
this.action = action;
this.percentageX = percentageX;
this.percentageY = percentageY;
}

public String toJson() {
Gson gson = new GsonBuilder().registerTypeAdapter(ObjectId.class, new ObjectIdTypeAdapter()).create();
return gson.toJson(this);
}

public Fun copy() {
return new Fun(this.action, this.percentageX, this.percentageY);
}

public static Fun getByJsonWithForm(String message) {
Gson gson = new GsonBuilder().registerTypeAdapter(ObjectId.class, new ObjectIdTypeAdapter()).create();
return gson.fromJson(message, Fun.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,8 @@ private void broadcast(LiveQuizSocketMessage message, String courseId, String fo
messageToSend.form.fillQuestionContents(course);
if (connection.getType().equals(SocketConnectionType.PARTICIPANT)) {
messageToSend.form.clearResults();
} else {
messageToSend.form.setParticipants(message.form.getParticipants());
}
messageToSend.form.setParticipants(message.form.getParticipants());

// send the message
String messageString = messageToSend.toJson();
Expand All @@ -291,6 +290,10 @@ private Boolean evaluateMessage(LiveQuizSocketMessage quizSocketMessage, String
System.out.println("Action is null");
return false;
}

if (quizSocketMessage.action.equals("FUN")) {
return this.fun(quizSocketMessage, formId);
}

// if the user is not an owner or participant, return
User user = userRepository.findById(new ObjectId(userId));
Expand Down Expand Up @@ -471,4 +474,28 @@ private Boolean next(LiveQuizSocketMessage quizSocketMessage, Course course, Str
return true;
}

private Boolean fun(LiveQuizSocketMessage quizSocketMessage, String formId) {

System.out.println("Throw paper plane");

quizSocketMessage = new LiveQuizSocketMessage(quizSocketMessage.action, quizSocketMessage.fun);

// send the message
String messageString = quizSocketMessage.toJson();

connections.values().forEach(connection -> {
// check if the form ID match
if (!connection.getFormId().equals(new ObjectId(formId))) {
return;
}

connection.session.getAsyncRemote().sendObject(messageString, result -> {
if (result.getException() != null) {
System.out.println("Unable to send message: " + result.getException());
}
});
});

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class LiveQuizSocketMessage {

// outgoing message
public QuizForm form;

// fun stuff (throw paper plane)
public Fun fun;

public Boolean userHasAnsweredCorrectly;
public List<String> correctAnswers;

Expand All @@ -36,6 +40,7 @@ public LiveQuizSocketMessage(String message) {
this.formStatus = quizSocketMessage.formStatus;
this.resultElementId = quizSocketMessage.resultElementId;
this.resultValues = quizSocketMessage.resultValues;
this.fun = quizSocketMessage.fun;
this.form = null;

System.out.println("Action: " + this.action);
Expand All @@ -44,6 +49,17 @@ public LiveQuizSocketMessage(String message) {
System.out.println("Result value: " + this.resultValues);
}

public LiveQuizSocketMessage(String action, Fun fun) {
this.action = action;
this.formStatus = null;
this.resultElementId = null;
this.resultValues = null;
this.form = null;
this.userHasAnsweredCorrectly = null;
this.correctAnswers = null;
this.fun = fun;
}

public LiveQuizSocketMessage(String action, String formStatus, String resultElementId, List<String> resultValues, QuizForm form) {
this.action = action;
this.formStatus = formStatus;
Expand All @@ -52,6 +68,7 @@ public LiveQuizSocketMessage(String action, String formStatus, String resultElem
this.form = form;
this.userHasAnsweredCorrectly = null;
this.correctAnswers = null;
this.fun = null;
}

public LiveQuizSocketMessage(String action, String formStatus, String resultElementId, List<String> resultValues, QuizForm form, Boolean userHasAnsweredCorrectly) {
Expand All @@ -62,6 +79,7 @@ public LiveQuizSocketMessage(String action, String formStatus, String resultElem
this.form = form;
this.userHasAnsweredCorrectly = userHasAnsweredCorrectly;
this.correctAnswers = null;
this.fun = null;
}

public LiveQuizSocketMessage(String action, String formStatus, String resultElementId, List<String> resultValues, QuizForm form, Boolean userHasAnsweredCorrectly, List<String> correctAnswers) {
Expand All @@ -72,6 +90,18 @@ public LiveQuizSocketMessage(String action, String formStatus, String resultElem
this.form = form;
this.userHasAnsweredCorrectly = userHasAnsweredCorrectly;
this.correctAnswers = correctAnswers;
this.fun = null;
}

public LiveQuizSocketMessage(String action, String formStatus, String resultElementId, List<String> resultValues, QuizForm form, Boolean userHasAnsweredCorrectly, List<String> correctAnswers, Fun fun) {
this.action = action;
this.formStatus = formStatus;
this.resultElementId = resultElementId;
this.resultValues = resultValues;
this.form = form;
this.userHasAnsweredCorrectly = userHasAnsweredCorrectly;
this.correctAnswers = correctAnswers;
this.fun = fun;
}

public String toJson() {
Expand All @@ -80,7 +110,7 @@ public String toJson() {
}

public LiveQuizSocketMessage copy() {
return new LiveQuizSocketMessage(this.action, this.formStatus, this.resultElementId, this.resultValues, this.form, this.userHasAnsweredCorrectly, this.correctAnswers);
return new LiveQuizSocketMessage(this.action, this.formStatus, this.resultElementId, this.resultValues, this.form, this.userHasAnsweredCorrectly, this.correctAnswers, this.fun);
}

public static LiveQuizSocketMessage getByJsonWithForm(String message) {
Expand Down
Binary file added frontend/assets/animations/rive/dart.riv
Binary file not shown.
Binary file added frontend/assets/animations/rive/paper_plane.riv
Binary file not shown.
Binary file added frontend/assets/animations/rive/stone.riv
Binary file not shown.
91 changes: 91 additions & 0 deletions frontend/lib/components/animations/throw.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:rive/rive.dart';

enum ThrowType {
paperPlane(
filename: "paper_plane.riv",
artboard: "Artboard",
stateMachine: "Hit State Machine",
width: 700,
height: 1200,
hitX: 602,
hitY: 144),
stone(
filename: "stone.riv",
artboard: "New Artboard",
stateMachine: "State Machine 1",
width: 700,
height: 500,
hitX: 614,
hitY: 96),
dart(
filename: "dart.riv",
artboard: "New Artboard",
stateMachine: "State Machine 1",
width: 700,
height: 1000,
hitX: 660,
hitY: 41);
// ball("ball.riv"),

final String filename;
final String artboard;
final String stateMachine;
final double width;
final double height;
final double hitX;
final double hitY;

const ThrowType(
{required this.filename,
required this.artboard,
required this.stateMachine,
required this.hitX,
required this.width,
required this.height,
required this.hitY});
}

class Throw extends StatefulWidget {
final ThrowType throwType;
final double clickX;
final double clickY;

const Throw(
{required this.throwType,
required this.clickX,
required this.clickY,
super.key});

@override
State<Throw> createState() => _ThrowState();
}

class _ThrowState extends State<Throw> {
final riveDirName = 'assets/animations/rive';

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return Positioned(
key: UniqueKey(),
left: widget.clickX - widget.throwType.hitX,
top: widget.clickY - widget.throwType.hitY,
child: SizedBox(
width: widget.throwType.width,
height: widget.throwType.height,
child: RiveAnimation.asset(
"$riveDirName/${widget.throwType.filename}",
fit: BoxFit.cover,
artboard: widget.throwType.artboard,
stateMachines: [widget.throwType.stateMachine],
),
),
);
}
}
Loading

0 comments on commit fcc8f9f

Please sign in to comment.