Merge pull request #26 from informatik-ag-ngl/f/system_commands

Added system commands ( features: custom argument number, default values, system command builder, ...).
Fixed bug not copying attachment when using copy and send.
This commit is contained in:
delvh
2020-07-24 13:54:05 +02:00
committed by GitHub
9 changed files with 641 additions and 13 deletions

View File

@ -30,6 +30,8 @@ import javafx.util.Duration;
import envoy.client.data.*;
import envoy.client.data.audio.AudioRecorder;
import envoy.client.data.commands.SystemCommandBuilder;
import envoy.client.data.commands.SystemCommandsMap;
import envoy.client.event.MessageCreationEvent;
import envoy.client.net.Client;
import envoy.client.net.WriteProxy;
@ -107,6 +109,8 @@ public final class ChatScene implements Restorable {
private Attachment pendingAttachment;
private boolean postingPermanentlyDisabled;
private final SystemCommandsMap messageTextAreaCommands = new SystemCommandsMap();
private static final Settings settings = Settings.getInstance();
private static final EventBus eventBus = EventBus.getInstance();
private static final Logger logger = EnvoyLog.getLogger(ChatScene.class);
@ -193,7 +197,7 @@ public final class ChatScene implements Restorable {
switch (e.getOperationType()) {
case ADD:
if (contact instanceof User) localDB.getUsers().put(contact.getName(), (User) contact);
Chat chat = contact instanceof User ? new Chat(contact) : new GroupChat(client.getSender(), contact);
final Chat chat = contact instanceof User ? new Chat(contact) : new GroupChat(client.getSender(), contact);
Platform.runLater(() -> chatList.getItems().add(chat));
break;
case REMOVE:
@ -203,6 +207,22 @@ public final class ChatScene implements Restorable {
});
}
/**
* Initializes all {@code SystemCommands} used in {@code ChatScene}.
*
* @since Envoy Client v0.2-beta
*/
private void initializeSystemCommandsMap() {
final var builder = new SystemCommandBuilder();
// Do A Barrel roll initialization
final var random = new Random();
builder.setAction(text -> doABarrelRoll(Integer.parseInt(text.get(0)), Double.parseDouble(text.get(1))))
.setDefaults(Integer.toString(random.nextInt(3) + 1), Double.toString(random.nextDouble() * 3 + 1))
.setDescription("See for yourself :)")
.setNumberOfArguments(2);
messageTextAreaCommands.add("DABR", builder.build());
}
/**
* Initializes all necessary data via dependency injection-
*
@ -225,6 +245,7 @@ public final class ChatScene implements Restorable {
if (!client.isOnline()) updateInfoLabel("You are offline", "infoLabel-info");
recorder = new AudioRecorder();
initializeSystemCommandsMap();
}
@Override
@ -376,20 +397,34 @@ public final class ChatScene implements Restorable {
}
/**
* Rotates every element in our application by 360° in at most 2.75s.
* Rotates every element in our application by (at most 4 *) 360° in at most
* 2.75s.
*
* @since Envoy Client v0.1-beta
*/
@FXML
private void doABarrelRoll() {
final var random = new Random();
doABarrelRoll(random.nextInt(3) + 1, random.nextDouble() * 3 + 1);
}
/**
* Rotates every element in our application by {@code rotations}*360° in
* {@code an}.
*
* @param rotations the amount of times the scene is rotated by 360°
* @param animationTime the time in seconds that this animation lasts
* @since Envoy Client v0.1-beta
*/
private void doABarrelRoll(int rotations, double animationTime) {
// contains all Node objects in ChatScene in alphabetical order
final var rotatableNodes = new Node[] { attachmentButton, attachmentView, contactLabel, infoLabel, messageList, messageTextArea,
postButton, remainingChars, rotateButton, scene, settingsButton, chatList, voiceButton };
final var random = new Random();
final var rotatableNodes = new Node[] { attachmentButton, attachmentView, contactLabel, infoLabel, messageList, messageTextArea, postButton,
remainingChars, rotateButton, scene, settingsButton, chatList, voiceButton };
for (final var node : rotatableNodes) {
// Defines at most four whole rotation in at most 4s
final var rotateTransition = new RotateTransition(Duration.seconds(random.nextDouble() * 3 + 1), node);
rotateTransition.setByAngle((random.nextInt(3) + 1) * 360);
// Sets the animation duration to {animationTime}
final var rotateTransition = new RotateTransition(Duration.seconds(animationTime), node);
// rotates every element {rotations} times
rotateTransition.setByAngle(rotations * 360);
rotateTransition.play();
// This is needed as for some strange reason objects could stop before being
// rotated back to 0°
@ -480,8 +515,8 @@ public final class ChatScene implements Restorable {
updateInfoLabel("You need to go online to send more messages", "infoLabel-error");
return;
}
final var text = messageTextArea.getText().strip();
try {
final var text = messageTextArea.getText().strip();
if (!messageTextAreaCommands.executeIfAnyPresent(text)) try {
// Creating the message and its metadata
final var builder = new MessageBuilder(localDB.getUser().getID(), currentChat.getRecipient().getID(), localDB.getIDGenerator())
.setText(text);
@ -569,9 +604,14 @@ public final class ChatScene implements Restorable {
private void copyAndPostMessage() {
final var messageText = messageTextArea.getText();
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(messageText), null);
final var image = attachmentView.getImage();
final var messageAttachment = pendingAttachment;
postMessage();
messageTextArea.setText(messageText);
updateRemainingCharsLabel();
postButton.setDisable(messageText.isBlank());
attachmentView.setImage(image);
if (attachmentView.getImage() != null) attachmentView.setVisible(true);
pendingAttachment = messageAttachment;
}
}