Skip to content

Commit

Permalink
Add swing ui
Browse files Browse the repository at this point in the history
ui by Asthowen
  • Loading branch information
is0ss committed Aug 23, 2020
1 parent 6ce9916 commit 1434ca5
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div align="center">
<h1>Chaika RPC (Chaika > all)</h1>
<img alt="version" src="https://img.shields.io/badge/version-v1.0-blue.svg">
<img alt="version" src="https://img.shields.io/badge/version-v1.1-blue.svg">
<img alt="licence" src="https://img.shields.io/badge/license-MIT-brightgreen.svg">
</div>

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'isoss.chaika.rpc'
version '1.0'
version '1.1'

compileJava.options.encoding = 'UTF-8'

Expand All @@ -19,7 +19,7 @@ shadowJar {

jar{
manifest{
attributes('Main-Class': 'Main')
attributes('Main-Class': 'asthowen.chaika.ui.Main')
}
}

Expand Down
10 changes: 0 additions & 10 deletions src/main/java/Main.java

This file was deleted.

67 changes: 67 additions & 0 deletions src/main/java/asthowen/chaika/ui/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package asthowen.chaika.ui;

import asthowen.chaika.ui.panels.ConfigPanel;
import asthowen.chaika.ui.panels.MainPanel;
import isoss.chaika.rpc.ChaikaPresence;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class Main extends JFrame {

public static ChaikaPresence presence;

public static Main main;
public static MainPanel mainPanel;

public ConfigPanel configPanel;

public Main(){
System.out.println("ROAD TO ADD CHAIKA'S EMOJI ON GITHUB !");

presence = new ChaikaPresence();
presence.init();

setTitle("Chaika Discord RPC");
setSize(1280, 720);
setResizable(false);
setLocationRelativeTo(null);

setContentPane(mainPanel = new MainPanel());

setDefaultCloseOperation(EXIT_ON_CLOSE);

try {
setIconImage(ImageIO.read(Main.class.getResourceAsStream("/chaika.png")));
} catch (IOException e) {
e.printStackTrace();
}

Color backgroundColor = Color.decode("#24343C");
getContentPane().setBackground(backgroundColor);

setVisible(true);

try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}

setContentPane(configPanel = new ConfigPanel());
getContentPane().setBackground(backgroundColor);

repaint();
revalidate();

}

public static void main(String[] args){
main = new Main();

}

}
98 changes: 98 additions & 0 deletions src/main/java/asthowen/chaika/ui/panels/ConfigPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package asthowen.chaika.ui.panels;

import asthowen.chaika.ui.Main;
import asthowen.chaika.ui.utils.Utils;
import isoss.chaika.rpc.ConfigWrapper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.nio.file.Paths;

public class ConfigPanel extends JPanel {

private static final JTextField applicationIDField = new JTextField();
private static final JLabel applicationIDLabel = new JLabel("Application ID :");

private static final JTextField applicationDetailsField = new JTextField();
private static final JLabel applicationDetailsLabel = new JLabel("Détails :");

private static final JTextField applicationLargeImageKeyField = new JTextField();
private static final JLabel applicationLargeImageKeyLabel = new JLabel("Large Image Key :");

private static final JTextField applicationLargeImageTextField = new JTextField();
private static final JLabel applicationLargeImageTextLabel = new JLabel("Large Image Text :");

private static final JTextField applicationSmallImageKeyField = new JTextField();
private static final JLabel applicationSmallImageKeyLabel = new JLabel("Small Image Key :");

private static final JTextField applicationSmallImageTextField = new JTextField();
private static final JLabel applicationSmallImageTextLabel = new JLabel("Small Image Text :");

private static final JButton enterButton = new JButton("Save");

public ConfigPanel(){
setLayout(null);

String applicationId = ConfigWrapper.get("applicationId");
String details = ConfigWrapper.get("details");
String largeImageKey = ConfigWrapper.get("largeImageKey");
String largeImageText = ConfigWrapper.get("largeImageText");
String smallImageKey = ConfigWrapper.get("smallImageKey");
String smallImageText = ConfigWrapper.get("smallImageText");

Utils.createFieldAndLabel(applicationIDField, applicationId, 100, applicationIDLabel, 65);
Utils.createFieldAndLabel(applicationDetailsField, details, 180, applicationDetailsLabel, 145);
Utils.createFieldAndLabel(applicationLargeImageKeyField, largeImageKey, 260, applicationLargeImageKeyLabel, 225);
Utils.createFieldAndLabel(applicationLargeImageTextField, largeImageText, 340, applicationLargeImageTextLabel, 305);
Utils.createFieldAndLabel(applicationSmallImageKeyField, smallImageKey, 420, applicationSmallImageKeyLabel, 385);
Utils.createFieldAndLabel(applicationSmallImageTextField, smallImageText, 500, applicationSmallImageTextLabel, 465);

applicationIDField.setText(applicationId);
applicationDetailsField.setText(details);
applicationLargeImageKeyField.setText(largeImageKey);
applicationLargeImageTextField.setText(largeImageText);
applicationSmallImageKeyField.setText(smallImageKey);
applicationSmallImageTextField.setText(smallImageText);

enterButton.setBounds(1280 / 2 - 275 /2, 600, 275, 40);
enterButton.setFont(new Font("Verdana", Font.PLAIN, 20));
enterButton.setForeground(Utils.colorForeground);
enterButton.setBackground(Utils.colorBackground);

ActionListener actionListener = event -> {
ConfigWrapper.set("applicationId", applicationIDField.getText());
ConfigWrapper.set("details", applicationDetailsField.getText());
ConfigWrapper.set("largeImageKey", applicationLargeImageKeyField.getText());
ConfigWrapper.set("largeImageText", applicationLargeImageTextField.getText());
ConfigWrapper.set("smallImageKey", applicationSmallImageKeyField.getText());
ConfigWrapper.set("smallImageText", applicationSmallImageTextField.getText());

Main.presence.updatePresence();

ConfigWrapper.write(Paths.get("config.json"));

System.out.println("Config saved !");
};

enterButton.addActionListener(actionListener);

add(applicationIDField);
add(applicationIDLabel);
add(applicationDetailsField);
add(applicationDetailsLabel);
add(applicationLargeImageKeyField);
add(applicationLargeImageKeyLabel);
add(applicationLargeImageKeyField);
add(applicationLargeImageTextField);
add(applicationLargeImageTextLabel);
add(applicationSmallImageKeyField);
add(applicationSmallImageKeyField);
add(applicationSmallImageKeyLabel);
add(applicationSmallImageTextField);
add(applicationSmallImageTextLabel);
add(enterButton);

}

}
23 changes: 23 additions & 0 deletions src/main/java/asthowen/chaika/ui/panels/MainPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package asthowen.chaika.ui.panels;

import asthowen.chaika.ui.utils.Utils;

import javax.swing.*;
import java.awt.*;

public class MainPanel extends JPanel {

public MainPanel(){
setLayout(new GridBagLayout());

//Add Title
JLabel textLabel = new JLabel("Bienvenue sur Chaika Discord RPC !");
textLabel.setFont(new Font("Verdana", 0, 50));
textLabel.setForeground(Utils.colorForeground);
textLabel.setBackground(Utils.colorBackground);
textLabel.setSize(550, 150);
add(textLabel);

}

}
26 changes: 26 additions & 0 deletions src/main/java/asthowen/chaika/ui/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package asthowen.chaika.ui.utils;

import javax.swing.*;
import java.awt.*;

public class Utils {
public static Color colorBackground = Color.decode("#24343C");
public static Color colorForeground = Color.decode("#99AAB5");

public static void createFieldAndLabel(JTextField textField, String text, int y, JLabel textLabel, int y2){
//Create TextField
textField.setBounds(1280 / 2 - 550 /2, y, 550, 40);
textField.setFont(new Font("Verdana", 0, 30));
textField.setForeground(colorForeground);
textField.setBackground(colorBackground);
textField.setText(text);

//Create JLabel
textLabel.setBounds(1280 / 2 - 550 /2, y2, 550, 40);
textLabel.setFont(new Font("Verdana", 0, 20));
textLabel.setForeground(colorForeground);
textLabel.setBackground(colorBackground);

}

}
40 changes: 28 additions & 12 deletions src/main/java/isoss/chaika/rpc/ChaikaPresence.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
public class ChaikaPresence {

private DiscordRichPresence chaikaPresence;
private long startTimestamp;

public ChaikaPresence() {
this.chaikaPresence = new DiscordRichPresence();

}

public void init() {
DiscordRPC rpc = DiscordRPC.INSTANCE;
Expand All @@ -16,25 +22,35 @@ public void init() {

rpc.Discord_Initialize(ConfigWrapper.get("applicationId"), handlers, true, "");

this.chaikaPresence = new DiscordRichPresence();
this.chaikaPresence.startTimestamp = System.currentTimeMillis() / 1000;
this.chaikaPresence.largeImageKey = ConfigWrapper.get("largeImageKey");
this.chaikaPresence.largeImageText = ConfigWrapper.get("largeImageText");
this.chaikaPresence.smallImageKey = ConfigWrapper.get("smallImageKey");
this.chaikaPresence.smallImageText = ConfigWrapper.get("smallImageText");
this.chaikaPresence.details = ConfigWrapper.get("details");
this.startTimestamp = System.currentTimeMillis() / 1000;
this.updatePresence();

rpc.Discord_UpdatePresence(chaikaPresence);

new Thread(() -> {
Thread callbackHandler = new Thread(() -> {

while(!Thread.currentThread().isInterrupted()) {
while(true) {
rpc.Discord_RunCallbacks();
try { Thread.sleep(2000); } catch(InterruptedException e) {}

}

}, "ChaikaRPC-Callback-Handler").start();
}, "ChaikaRPC-Callback-Handler");

callbackHandler.setDaemon(true);
callbackHandler.start();

}

public void updatePresence() {
DiscordRPC rpc = DiscordRPC.INSTANCE;

this.chaikaPresence.startTimestamp = startTimestamp;
this.chaikaPresence.largeImageKey = ConfigWrapper.get("largeImageKey");
this.chaikaPresence.largeImageText = ConfigWrapper.get("largeImageText");
this.chaikaPresence.smallImageKey = ConfigWrapper.get("smallImageKey");
this.chaikaPresence.smallImageText = ConfigWrapper.get("smallImageText");
this.chaikaPresence.details = ConfigWrapper.get("details");

rpc.Discord_UpdatePresence(chaikaPresence);

}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/isoss/chaika/rpc/ConfigWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ConfigWrapper {

Expand All @@ -17,11 +18,11 @@ public class ConfigWrapper {
config = new JsonObject();

setDefault("applicationId", Constants.APPLICATION_ID);
setDefault("details", Constants.DETAILS);
setDefault("largeImageKey", Constants.LARGE_IMAGE_KEY);
setDefault("largeImageText", Constants.LARGE_IMAGE_TEXT);
setDefault("smallImageKey", Constants.SMALL_IMAGE_KEY);
setDefault("smallImageText", Constants.SMALL_IMAGE_TEXT);
setDefault("details", Constants.DETAILS);

}

Expand Down

0 comments on commit 1434ca5

Please sign in to comment.