Skip to content

Commit

Permalink
Added an option to retain connection to a Logitow device when a build…
Browse files Browse the repository at this point in the history
… is finished
  • Loading branch information
itsMatoosh committed Apr 22, 2018
1 parent 9fdd365 commit c051926
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 66 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dependencies {
// https://mvnrepository.com/artifact/net.sf.jni4net/jni4net.j
compile group: 'net.sf.jni4net', name: 'jni4net.j', version: '0.8.8.0', ext: 'pom'
compile 'org.apache.commons:commons-lang3:3.5'
compile name: 'logitow-bridge-0.9-all'
compile name: 'logitow-bridge-0.9.1-all'

// you may put jars on which you depend on in ./libs
// or you may define them like so..
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

Expand All @@ -24,7 +26,7 @@ public class BluetoothDialogGui extends GuiScreen {
/**
* The height of the container graphic.
*/
final int containerHeight = 140;
final int containerHeight = 210;
/**
* The width of the container graphic.
*/
Expand All @@ -40,6 +42,13 @@ public class BluetoothDialogGui extends GuiScreen {
*/
final int CLOSE_DIALOG_BUTTON_ID = 0;

public static final ITextComponent TEXT_BLUETOOTH_DIALOG_ENABLE_BLUETOOTH = new TextComponentTranslation("logitow.bluetoothdialog.enablebluetooth");
public static final ITextComponent TEXT_BLUETOOTH_DIALOG_ENABLE_BLUETOOTH_INFO = new TextComponentTranslation("logitow.bluetoothdialog.enablebluetooth.infotext");
public static final ITextComponent TEXT_BLUETOOTH_DIALOG_COULDNT_ACCESS_BT = new TextComponentTranslation("logitow.bluetoothdialog.couldntaccessbluetooth");
public static final ITextComponent TEXT_BLUETOOTH_DIALOG_COULDNT_ACCESS_BT_INFO = new TextComponentTranslation("logitow.bluetoothdialog.couldntaccessbluetooth.infotext");
public static final ITextComponent TEXT_BLUETOOTH_DIALOG_DISMISS = new TextComponentTranslation("logitow.bluetoothdialog.dismiss");


/**
* Creates the dialog based on the bluetooth state.
* @param bluetoothState
Expand Down Expand Up @@ -78,17 +87,17 @@ public void drawScreen(int p_drawScreen_1_, int p_drawScreen_2_, float p_drawScr
//Bluetooth enabled, closing the dialog.
Minecraft.getMinecraft().displayGuiScreen(null);
} else if(currentBluetoothState == BluetoothState.PoweredOff) {
drawString(fontRenderer, "Enable Bluetooth!", (width/2) - fontRenderer.getStringWidth("Enable Bluetooth!")/2 + 42, (height/2) - containerHeight/2+30, 0xff0055);
infoText = "Make sure that Bluetooth\nfunctionality is enabled\non your system.";
drawString(fontRenderer, TEXT_BLUETOOTH_DIALOG_ENABLE_BLUETOOTH.getFormattedText(), (width/2) - fontRenderer.getStringWidth(TEXT_BLUETOOTH_DIALOG_ENABLE_BLUETOOTH.getFormattedText())/2 + 42, (height/2) - containerHeight/2+30, 0xff0055);
infoText = TEXT_BLUETOOTH_DIALOG_ENABLE_BLUETOOTH_INFO.getFormattedText();
} else {
drawString(fontRenderer, "Couldn't access Bluetooth!", (width/2) - fontRenderer.getStringWidth("Couldn't access Bluetooth!")/2+42, (height/2) - containerHeight/2+30, 0xff0055);
infoText = "Make sure that your \nsystem supports \nBluetooth Low Energy \nand no other application \nis using it.";
drawString(fontRenderer, TEXT_BLUETOOTH_DIALOG_COULDNT_ACCESS_BT.getFormattedText(), (width/2) - fontRenderer.getStringWidth(TEXT_BLUETOOTH_DIALOG_COULDNT_ACCESS_BT.getFormattedText())/2+42, (height/2) - containerHeight/2+30, 0xff0055);
infoText = TEXT_BLUETOOTH_DIALOG_COULDNT_ACCESS_BT_INFO.getFormattedText();
}

//Drawing each line.
int lineOffset = 0;
for (String line :
infoText.split("\n")) {
infoText.split("/")) {
drawString(fontRenderer, line, (width/2) - fontRenderer.getStringWidth(line)/2 + 42, (height/2) - containerHeight/2+47 + lineOffset, 0xffffff);
lineOffset += 11;
}
Expand All @@ -99,7 +108,7 @@ public void drawScreen(int p_drawScreen_1_, int p_drawScreen_2_, float p_drawScr
@Override
public void initGui() {
buttonList.clear();
buttonList.add(new GuiButton(CLOSE_DIALOG_BUTTON_ID, width/2 - 40 + 42, height/2 + 30, 80, 20, "Dismiss"));
buttonList.add(new GuiButton(CLOSE_DIALOG_BUTTON_ID, width/2 - 40 + 42, height/2 + 30, 80, 20, TEXT_BLUETOOTH_DIALOG_DISMISS.getFormattedText()));
super.initGui();
}

Expand Down
111 changes: 63 additions & 48 deletions src/main/java/com/logitow/logimine/client/gui/DeviceManagerGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.logitow.logimine.LogiMine;
import com.logitow.logimine.event.LogitowBridgeEvent;
import com.logitow.logimine.networking.LogitowDeviceAssignMessage;
import com.logitow.logimine.proxy.ClientProxy;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
Expand Down Expand Up @@ -100,15 +101,16 @@ private int getDeviceStartPosition () {
//Translated elements.
public static final ITextComponent TEXT_DEVICE_MANAGER_TITLE = new TextComponentTranslation("logitow.devicemanager.title");
public static final ITextComponent TEXT_DEVICE_MANAGER_SELECT_DEVICE = new TextComponentTranslation("logitow.devicemanager.selectdevice");
public static final ITextComponent TEXT_DEVICE_MANAGER_AVAILEBLE_DEVICES = new TextComponentTranslation("logitow.devicemanager.availabledevices");
public static final ITextComponent TEXT_DEVICE_MANAGER_AVAILABLE_DEVICES = new TextComponentTranslation("logitow.devicemanager.availabledevices");
public static final ITextComponent TEXT_DEVICE_MANAGER_SCANNING = new TextComponentTranslation("logitow.devicemanager.scanning");
public static final ITextComponent TEXT_DEVICE_MANAGER_CONNECT_BUTTON = new TextComponentTranslation("logitow.devicemanager.connectbutton");
public static final ITextComponent TEXT_DEVICE_MANAGER_DISCONNECT_BUTTON = new TextComponentTranslation("logitow.devicemanager.disconnectbutton");
public static final ITextComponent TEXT_DEVICE_MANAGER_ASSIGN_BUTTON = new TextComponentTranslation("logitow.devicemanager.assignbutton");
public static final ITextComponent TEXT_DEVICE_MANAGER_UNASSIGN_BUTTON = new TextComponentTranslation("logitow.devicemanager.unassignbutton");
public static final ITextComponent TEXT_DEVICE_MANAGER_CONNECTED_LABEL = new TextComponentTranslation("logitow.devicemanager.connectedlabel");
public static final ITextComponent TEXT_DEVICE_MANAGER_SCAN_BUTTON = new TextComponentTranslation("logitow.devicemanager.scanbutton");
public static final ITextComponent TEXT_DEVICE_MANAGER_CANCEL_BUTTON = new TextComponentTranslation("logitow.devicemanager.cancelbutton");
public static final String TEXT_DEVICE_MANAGER_CONNECTING = "logitow.devicemanager.connecting";
public static final String TEXT_DEVICE_MANAGER_DISCONNECTING = "logitow.devicemanager.disconnecting";
public static final String TEXT_DEVICE_MANAGER_NOTCONNECTED = "logitow.devicemanager.notconnected";
public static final String TEXT_DEVICE_MANAGER_ASSIGNING = "logitow.devicemanager.assigning";

/**
* Draws the screen and all the components in it.
Expand Down Expand Up @@ -139,6 +141,15 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) {
}
}

//Connect/Assign button.
if(deviceChosen != -1) {
if(deviceChosen >= 200) {
connectButton.displayString = TEXT_DEVICE_MANAGER_CONNECT_BUTTON.getFormattedText();
} else {
connectButton.displayString = TEXT_DEVICE_MANAGER_ASSIGN_BUTTON.getFormattedText();
}
}

//Available devices/Select device
if(HubGui.instance.getSelectedKeyBlock() != null) {
String selectDevice = TEXT_DEVICE_MANAGER_SELECT_DEVICE.getFormattedText();
Expand All @@ -149,7 +160,7 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) {
connectButton.enabled = false;
}
} else {
String availableDevices = TEXT_DEVICE_MANAGER_AVAILEBLE_DEVICES.getFormattedText();
String availableDevices = TEXT_DEVICE_MANAGER_AVAILABLE_DEVICES.getFormattedText();
drawString(fontRenderer, availableDevices, (width/2) - fontRenderer.getStringWidth(availableDevices)/2, (height/2) - containerHeight/3 +17, 0x8b8b8b);
connectButton.enabled = false;
}
Expand Down Expand Up @@ -217,8 +228,32 @@ public void initGui() {
//Adding the new device.
int buttonId = 100 + instance.buttonList.size() - 3;
instance.ASSIGNED_DEVICE_BUTTON_ID = buttonId;
instance.buttonList.add(new GuiButton(buttonId, instance.width / 2 - 70, newYPos + instance.deviceButtonSeparation, 140, 20, "[" + TEXT_DEVICE_MANAGER_DISCONNECT_BUTTON.getFormattedText().toUpperCase() + "] " + HubGui.instance.getSelectedKeyBlock().getAssignedDevice()));
instance.buttonList.add(new GuiButton(buttonId, instance.width / 2 - 70, newYPos + instance.deviceButtonSeparation, 140, 20, "[" + TEXT_DEVICE_MANAGER_UNASSIGN_BUTTON.getFormattedText().toUpperCase() + "] " + HubGui.instance.getSelectedKeyBlock().getAssignedDevice()));
}

//Adding the connected devices list.
for (Device connected :
LogitowDeviceManager.current.connectedDevices) {
//Skipping the assigned device.
if (HubGui.instance.getSelectedKeyBlock() != null && HubGui.instance.getSelectedKeyBlock().getAssignedDevice() != null && connected.equals(HubGui.instance.getSelectedKeyBlock().getAssignedDevice())) return;

//The new y pos of the element.
int newYPos = getDeviceStartPosition() - deviceButtonSeparation;

//Getting the highest y position of the elements.
for (GuiButton button :
buttonList) {
if (button.id > 100 && button.y > newYPos) {
newYPos = button.y;
}
}

//Adding the new device.
int buttonId = 100 + buttonList.size() - 3;
buttonList.add(new GuiButton(buttonId, width / 2 - 70, newYPos + deviceButtonSeparation, 140, 20, "[" + TEXT_DEVICE_MANAGER_CONNECTED_LABEL.getFormattedText().toUpperCase() + "] " + connected.toString()));
discoveredDevices.put(connected, buttonId);
}

super.initGui();
}

Expand All @@ -234,18 +269,31 @@ protected void actionPerformed(GuiButton button) throws IOException {
for (Device device :
discoveredDevices.keySet()) {
if(discoveredDevices.get(device) == deviceChosen) {
Minecraft.getMinecraft().player.sendMessage(new TextComponentTranslation(TEXT_DEVICE_MANAGER_CONNECTING, device.info.friendlyName));

//Unassigning if there is one device connected already.
if(instance != null && HubGui.instance.getSelectedKeyBlock() != null && HubGui.instance.getSelectedKeyBlock().getAssignedDevice() != null) {
HubGui.instance.getSelectedKeyBlock().getAssignedDevice().disconnect();
if(HubGui.instance.getSelectedKeyBlock().getAssignedDevice().equals(device)) {
Minecraft.getMinecraft().displayGuiScreen(null);
return;
}
LogiMine.networkWrapper.sendToServer(new LogitowDeviceAssignMessage(HubGui.instance.getSelectedKeyBlock().getPos(), null));
HubGui.instance.getSelectedKeyBlock().assignDevice(null, null);
}

//Found the appropriate device. Assigning on client.
//Assigning the new device locally.
HubGui.instance.getSelectedKeyBlock().assignDevice(Minecraft.getMinecraft().player, device);

//Avoiding slowdowns.
new Thread(() -> device.connect()).start();
if(deviceChosen >= 200) {
//Connect
Minecraft.getMinecraft().player.sendMessage(new TextComponentTranslation(TEXT_DEVICE_MANAGER_CONNECTING, device));

//Avoiding slowdowns.
new Thread(() -> device.connect()).start();
} else {
//Assigning on the server.
Minecraft.getMinecraft().player.sendMessage(new TextComponentTranslation(TEXT_DEVICE_MANAGER_ASSIGNING, device));

LogiMine.networkWrapper.sendToServer(new LogitowDeviceAssignMessage(HubGui.instance.getSelectedKeyBlock().getPos(), device));
}
}
}
Minecraft.getMinecraft().displayGuiScreen(null);
Expand All @@ -266,44 +314,11 @@ protected void actionPerformed(GuiButton button) throws IOException {
//Device button has been selected.
if(button.id >= 100) {
if(button.id == ASSIGNED_DEVICE_BUTTON_ID) {
//Disconnecting the device.
Minecraft.getMinecraft().player.sendMessage(new TextComponentTranslation(TEXT_DEVICE_MANAGER_DISCONNECTING, HubGui.instance.getSelectedKeyBlock().getAssignedDevice().info.friendlyName));
if(!HubGui.instance.getSelectedKeyBlock().getAssignedDevice().disconnect()) {
//Not connected
Minecraft.getMinecraft().player.sendMessage(new TextComponentTranslation(TEXT_DEVICE_MANAGER_NOTCONNECTED, HubGui.instance.getSelectedKeyBlock().getAssignedDevice().info.friendlyName));
//Closing the dialog.
Minecraft.getMinecraft().displayGuiScreen(null);
}
button.enabled = false;
System.out.println("Disconnecting device: " + HubGui.instance.getSelectedKeyBlock().getAssignedDevice() + ", unassigning it from block " + HubGui.instance.getSelectedKeyBlock());

//Unassigning
HubGui.instance.getSelectedKeyBlock().assignDevice(null, null);
LogiMine.networkWrapper.sendToServer(new LogitowDeviceAssignMessage(HubGui.instance.getSelectedKeyBlock().getPos(), null));

//Updating the buttons.
GuiButton buttonToRemove = null;
int lastY = getDeviceStartPosition()-deviceButtonSeparation;
for (GuiButton registered :
buttonList) {
if(registered.id == ASSIGNED_DEVICE_BUTTON_ID) {
buttonToRemove = registered;
}
else if(registered.id > 100) {
registered.y = lastY + deviceButtonSeparation;
lastY = registered.y;
}
}

//Removing the button.
buttonList.remove(buttonToRemove);

//Closing the dialog.
Minecraft.getMinecraft().displayGuiScreen(null);
((ClientProxy)LogiMine.proxy).showUnassignDialog(HubGui.instance.getSelectedKeyBlock().getAssignedDevice(), HubGui.getSelectedKeyBlock());
}
if(HubGui.instance.getSelectedKeyBlock() != null) {
System.out.println("Selected device " + deviceChosen);
deviceChosen = button.id;
System.out.println("Selected device " + deviceChosen);
}
}

Expand Down Expand Up @@ -360,7 +375,7 @@ public void logitowEventReceived(LogitowBridgeEvent event) {
}

//Adding the new device.
int buttonId = 100 + buttonList.size()-3;
int buttonId = 200 + buttonList.size()-3;
buttonList.add(new GuiButton(buttonId, width/2 - 70, newYPos + deviceButtonSeparation, 140, 20, discoveredEvent.device.toString()));
discoveredDevices.put(discoveredEvent.device, buttonId);
} else if(event.deviceEvent instanceof DeviceLostEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ public void drawScreen(int p_drawScreen_1_, int p_drawScreen_2_, float p_drawScr

@Override
protected void actionPerformed(GuiButton button) throws IOException {
System.out.println("Action performed!");

if(button.id == CANCEL_BUTTON_ID) {
Minecraft.getMinecraft().displayGuiScreen(null);
return;
Expand Down Expand Up @@ -130,7 +128,7 @@ public void initGui() {
field.setFocused(true);
field.setCursorPosition(0);

Minecraft.getMinecraft().mouseHelper.ungrabMouseCursor();
//Minecraft.getMinecraft().mouseHelper.ungrabMouseCursor();

open = true;

Expand Down
Loading

0 comments on commit c051926

Please sign in to comment.