Commits

Anonymous committed 682f91c

Refactoring and loosely implemented IoC, in preparation of 1.1.0 duty cycle feature.

  • Participants
  • Parent commits 5276e17
  • Branches 1.1

Comments (0)

Files changed (6)

File src/com/joshjcarrier/minecontrol/App.java

 package com.joshjcarrier.minecontrol;
 
+import com.joshjcarrier.minecontrol.controller.ControllerManager;
+import com.joshjcarrier.minecontrol.controller.mapping.ControllerPlugin;
+import com.joshjcarrier.minecontrol.controller.mapping.MsXbox360ControllerPlugin;
+import com.joshjcarrier.minecontrol.notch.MinecraftController;
+import com.joshjcarrier.minecontrol.serviceprovider.MinecontrolServiceProvider;
 import com.joshjcarrier.minecontrol.ui.ConfigurationManager;
 
 /**
  */
 public class App {
 
-	public static String VERSION = "1.0.1";
+	public static String VERSION = "1.1.0";
+	
+	public App()
+	{
+		this(MinecontrolServiceProvider.getInstance());
+	}
+	
+	public App(MinecontrolServiceProvider resolver)
+	{
+		ControllerManager controllerManager = resolver.resolve(ControllerManager.class);
+		
+		// controller polling thread
+		Thread thread = new Thread(controllerManager);
+		thread.start();
+		
+		// this could be dynamically detected plugins
+		ControllerPlugin[] controllerPlugins = new ControllerPlugin[]{ new MsXbox360ControllerPlugin() };
+		
+		// TODO this can be initialized from persisted settings
+		MinecraftController controllerConfig = resolver.resolve(MinecraftController.class);
+		
+		new ConfigurationManager(controllerManager, controllerPlugins, controllerConfig);
+	}
 	
 	/**
 	 * @param args
 	 */
-	public static void main(String[] args) {
-		ConfigurationManager.Launch();
+	public static void main(String[] args) 
+	{
+		new App();
 	}
-
 }

File src/com/joshjcarrier/minecontrol/controller/ControllerManager.java

 	private HashMap<String, Integer> tristateSuppression;
 	private Controller currentController;
 	
-	public ControllerManager() {
+	public ControllerManager() 
+	{
 		eventMapping = new HashMap<String, MinecraftAction>();
 		tristateSuppression = new HashMap<String, Integer>();
 	}
 		this.eventMapping = controller.getActionMapping();
 		this.currentController = controller.getControllerConnection();
 	}
+	
+	public String getConnectedControllerName()
+	{
+		return this.currentController.getName();
+	}
 
-	public void run() {
-			
-		while (true) {
+	// The runnable thread which does input polling.
+	// TODO new strategy will run on duty cycles for better movement control
+	public void run() 
+	{
+		while (true) 
+		{
+			// controller disconnected
 			if(currentController == null)
 			{
-				return;
+				try {
+					Thread.sleep(1000);
+				} catch (InterruptedException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				}
+				continue;
 			}
 
 			currentController.poll();

File src/com/joshjcarrier/minecontrol/controller/mapping/ControllerPlugin.java

 
 		return null;
 	}
+	
+	@Override
+	public String toString()
+	{
+		return this.getName();
+	}
 }

File src/com/joshjcarrier/minecontrol/controller/mapping/MsXbox360ControllerPlugin.java

 	public HashMap<String, MinecraftAction> getActionMapping() {
 		return eventMapping;
 	}
-
 }

File src/com/joshjcarrier/minecontrol/serviceprovider/MinecontrolServiceProvider.java

+package com.joshjcarrier.minecontrol.serviceprovider;
+
+import java.util.HashMap;
+
+import com.joshjcarrier.minecontrol.controller.ControllerManager;
+import com.joshjcarrier.minecontrol.notch.MinecraftController;
+
+/**
+ * Provides service resolution.
+ * @author joshjcarrier
+ *
+ */
+public class MinecontrolServiceProvider
+{
+	private static MinecontrolServiceProvider instance;
+	
+	private final HashMap<Class<?>, Object> instanceMapping;
+	
+	/**
+	 * Hides the default constructor of MinecontrolServiceProvider.
+	 */
+	private MinecontrolServiceProvider()
+	{
+		instanceMapping = new HashMap<Class<?>, Object>();
+		
+		this.Initialize();
+	}
+	
+	public static MinecontrolServiceProvider getInstance()
+	{
+		if(instance == null)
+		{
+			instance = new MinecontrolServiceProvider();
+		}
+		
+		return instance;
+	}
+	
+	@SuppressWarnings("unchecked")
+	public <T> T resolve(Class<? extends T> clazz)
+	{
+		Object service = this.instanceMapping.get(clazz);
+		if(service == null || !clazz.isAssignableFrom(service.getClass()))
+		{
+			throw new InstantiationError("Service resolution failure");
+		}
+		
+		return (T) service;
+	}
+	
+	private void Initialize()
+	{
+		instanceMapping.put(ControllerManager.class, new ControllerManager());
+		instanceMapping.put(MinecraftController.class, new MinecraftController());
+	}
+}

File src/com/joshjcarrier/minecontrol/ui/ConfigurationManager.java

 import java.awt.GridLayout;
 import java.awt.Point;
 import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.util.HashMap;
 
 import javax.swing.ImageIcon;
 import com.joshjcarrier.minecontrol.App;
 import com.joshjcarrier.minecontrol.controller.ControllerManager;
 import com.joshjcarrier.minecontrol.controller.mapping.ControllerPlugin;
-import com.joshjcarrier.minecontrol.controller.mapping.MsXbox360ControllerPlugin;
 import com.joshjcarrier.minecontrol.notch.MinecraftAction;
 import com.joshjcarrier.minecontrol.notch.MinecraftController;
 
  * @author joshjcarrier
  *
  */
-public class ConfigurationManager {
-
-	private ControllerManager joystickControllerManager;
-	private MinecraftController minecraftController;
+public class ConfigurationManager 
+{
+	// controller manager
+	private final ControllerManager controllerManager;
+	
+	// list of available plugins
+	private ControllerPlugin[] controllerPlugins;
+	
+	// controller settings
+	private MinecraftController controllerConfig;
+	
+	// a controller definition
 	private ControllerPlugin currentControllerPlugin;
 	
 	private JCheckBox connectedCheckBox;
 	
-	private ConfigurationManager()
+	public ConfigurationManager(ControllerManager controllerManager, ControllerPlugin[] controllerPlugins, MinecraftController controllerConfig)
 	{
-		currentControllerPlugin = new MsXbox360ControllerPlugin();
-		joystickControllerManager = new ControllerManager();
-		joystickControllerManager.setController(currentControllerPlugin);
-		minecraftController = new MinecraftController();
-		joystickControllerManager.addObserver(minecraftController);
+		this.controllerManager = controllerManager;
+	
+		this.controllerPlugins = controllerPlugins;
+		currentControllerPlugin = controllerPlugins[0];
+				
+		this.controllerConfig = controllerConfig;
+		controllerManager.addObserver(controllerConfig);
 		
 		JFrame configWindow = buildWindow();
 		configWindow.setVisible(true);
-				
-		if(currentControllerPlugin.getControllerConnection() != null)
-		{
-			connectedCheckBox.setSelected(true);
-			Thread thread = new Thread(joystickControllerManager);
-			thread.start();
-		}
-		else
-		{
-			String controllerList = joystickControllerManager.getControllers();
-			JOptionPane.showMessageDialog(configWindow, "Controller not found. Connect the joystick and restart Minecontrol.\nDetected input devices:\n" + controllerList);
-		}
+		
+		// FIXME this one messes with the ui..
+		this.setCurrentController(currentControllerPlugin);
+		
+//		if(currentControllerPlugin.getControllerConnection() != null)
+//		{
+//			connectedCheckBox.setSelected(true);
+//		}
+//		else
+//		{
+//			String controllerList = controllerManager.getControllers();
+//			JOptionPane.showMessageDialog(configWindow, "Controller not found. Connect the joystick and restart Minecontrol.\nDetected input devices:\n" + controllerList);
+//		}
 	}
 	
 	private JFrame buildWindow()
 	{
 		final JPanel creditsPanel = new JPanel(new BorderLayout());
 		
-		JComboBox controllerList = new JComboBox(new Object[]{"Microsoft Xbox 360 controller"});
+		final JComboBox controllerList = new JComboBox(this.controllerPlugins);
+		controllerList.addActionListener(new ActionListener(){
+
+			public void actionPerformed(ActionEvent arg0) 
+			{
+				// changes the current controller
+				setCurrentController((ControllerPlugin)controllerList.getSelectedItem());
+			}
+		});
+		
 		creditsPanel.add(controllerList, BorderLayout.NORTH);
 		
 		ImageIcon controllerIcon = new ImageIcon(currentControllerPlugin.getControllerImage());
 			
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				minecraftController.setLookInversion(invertPitchCheckBox.isSelected());
+				controllerConfig.setLookInversion(invertPitchCheckBox.isSelected());
 			}
 		});
 		invertPitchCheckBox.setText("Invert look up/down");
 		lookVerticalSlider.setMinorTickSpacing(1);
 		lookVerticalSlider.setPaintTicks(true);
 		lookVerticalSlider.setPaintLabels(true);
-		lookVerticalSlider.setValue(minecraftController.mouseSensitivity[0].y);
+		lookVerticalSlider.setValue(controllerConfig.mouseSensitivity[0].y);
 		lookVerticalSlider.setMinimum(1);
 		lookVerticalSlider.setMaximum(20);
 		
 		lookHorizontalSlider.setMinorTickSpacing(1);
 		lookHorizontalSlider.setPaintTicks(true);
 		lookHorizontalSlider.setPaintLabels(true);
-		lookHorizontalSlider.setValue(minecraftController.mouseSensitivity[0].x);
+		lookHorizontalSlider.setValue(controllerConfig.mouseSensitivity[0].x);
 		lookHorizontalSlider.setMinimum(1);
 		lookHorizontalSlider.setMaximum(20);
 		
 		lookVerticalSlider.addChangeListener(new ChangeListener() {
 			public void stateChanged(ChangeEvent e) {
-				minecraftController.mouseSensitivity[0] = new Point(lookHorizontalSlider.getValue(), lookVerticalSlider.getValue());
+				controllerConfig.mouseSensitivity[0] = new Point(lookHorizontalSlider.getValue(), lookVerticalSlider.getValue());
 			}
 		});
 		lookHorizontalSlider.addChangeListener(new ChangeListener() {
 			public void stateChanged(ChangeEvent e) {
-				minecraftController.mouseSensitivity[0] = new Point(lookHorizontalSlider.getValue(), lookVerticalSlider.getValue());
+				controllerConfig.mouseSensitivity[0] = new Point(lookHorizontalSlider.getValue(), lookVerticalSlider.getValue());
 			}
 		});
 		
 		lookMouseVerticalSlider.setMinorTickSpacing(1);
 		lookMouseVerticalSlider.setPaintTicks(true);
 		lookMouseVerticalSlider.setPaintLabels(true);
-		lookMouseVerticalSlider.setValue(minecraftController.mouseSensitivity[1].y);
+		lookMouseVerticalSlider.setValue(controllerConfig.mouseSensitivity[1].y);
 		lookMouseVerticalSlider.setMinimum(1);
 		lookMouseVerticalSlider.setMaximum(20);
 						
 		lookMouseHorizontalSlider.setMinorTickSpacing(1);
 		lookMouseHorizontalSlider.setPaintTicks(true);
 		lookMouseHorizontalSlider.setPaintLabels(true);
-		lookMouseHorizontalSlider.setValue(minecraftController.mouseSensitivity[1].x);
+		lookMouseHorizontalSlider.setValue(controllerConfig.mouseSensitivity[1].x);
 		lookMouseHorizontalSlider.setMinimum(1);
 		lookMouseHorizontalSlider.setMaximum(20);
 		
 		lookMouseVerticalSlider.addChangeListener(new ChangeListener() {
 			public void stateChanged(ChangeEvent e) {
-				minecraftController.mouseSensitivity[1] = new Point(lookMouseHorizontalSlider.getValue(), lookMouseVerticalSlider.getValue());
+				controllerConfig.mouseSensitivity[1] = new Point(lookMouseHorizontalSlider.getValue(), lookMouseVerticalSlider.getValue());
 			}
 		});
 		lookMouseHorizontalSlider.addChangeListener(new ChangeListener() {
 			public void stateChanged(ChangeEvent e) {
-				minecraftController.mouseSensitivity[1] = new Point(lookMouseHorizontalSlider.getValue(), lookMouseVerticalSlider.getValue());
+				controllerConfig.mouseSensitivity[1] = new Point(lookMouseHorizontalSlider.getValue(), lookMouseVerticalSlider.getValue());
 			}
 		});
 		
 		return mappingMessage;
 	}
 	
-	public static void Launch()
+	private void setCurrentController(ControllerPlugin newCurrentController)
 	{
-		new ConfigurationManager();
+		this.currentControllerPlugin = newCurrentController;
+		controllerManager.setController(newCurrentController);
+		connectedCheckBox.setSelected((newCurrentController.getControllerConnection() != null));
 	}
 }