Files
jlibwiiuse/WiiUseJ/src/wiiusej/utils/ClassicControllerButtonsEventPanel.java
guilhem.duche fa337aaef0 Added gui and fixes for Guitar hero controller
git-svn-id: http://wiiusej.googlecode.com/svn/trunk@185 ae48ae66-6a45-0410-b38e-211266189506
2008-06-15 13:16:26 +00:00

517 lines
18 KiB
Java

/**
* This file is part of WiiuseJ.
*
* WiiuseJ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WiiuseJ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WiiuseJ. If not, see <http://www.gnu.org/licenses/>.
*/
package wiiusej.utils;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import wiiusej.wiiusejevents.physicalevents.ClassicControllerButtonsEvent;
import wiiusej.wiiusejevents.physicalevents.ClassicControllerEvent;
import wiiusej.wiiusejevents.physicalevents.ExpansionEvent;
import wiiusej.wiiusejevents.physicalevents.IREvent;
import wiiusej.wiiusejevents.physicalevents.JoystickEvent;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
import wiiusej.wiiusejevents.utils.WiimoteListener;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;
/**
* This panel is used to display what happens on the classic controller.
*
* @author guiguito
*/
public class ClassicControllerButtonsEventPanel extends javax.swing.JPanel implements WiimoteListener {
private Image mImage;// image for double buffering
private Image wiimoteImage;// image for double buffering
private ClassicControllerEvent event;
private Color pressedColor = Color.RED;
private Color heldColor = Color.ORANGE;
private Color releasedColor = Color.YELLOW;
private Color joystickColor = Color.PINK;
private Color shoulderColor = Color.BLUE;
private Shape shapeJoystick = new java.awt.geom.Ellipse2D.Double(0, 0, 13, 13);
private Shape shapeButton = new java.awt.geom.Ellipse2D.Double(0, 0, 20, 20);
/**
* Default constructor. Red : button just pressed. Orange : button held.
* Yellow : button just released.
*/
public ClassicControllerButtonsEventPanel() {
Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
java.net.URL url = ButtonsEventPanel.class.getResource("/img/classiccontroller.png");
wiimoteImage = toolkit.createImage(url);
initComponents();
}
/**
* Constructor used to set colors and shape used.
*
* @param pressColor
* color of a button just pressed.
* @param hColor
* color of a button held.
* @param relColor
* color of a button just released.
* @param jsColor
* color of the joysticks.
* @param shouldColor
* color of the shoulders.
* @param js
* shape drawn on the joysticks.
* @param sh
* shape drawn on the buttons.
*/
public ClassicControllerButtonsEventPanel(Color pressColor, Color hColor, Color relColor,
Color jsColor, Color shouldColor, Shape js, Shape sh) {
pressedColor = pressColor;
heldColor = hColor;
releasedColor = relColor;
shapeButton = sh;
shapeJoystick = js;
joystickColor = jsColor;
shoulderColor = shouldColor;
Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
wiimoteImage = toolkit.createImage("img\\wiimote.png");
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension d = getSize();
checkOffScreenImage();
Graphics offG = mImage.getGraphics();
// offG.setColor(backgroundColor);
offG.fillRect(0, 0, d.width, d.height);
Graphics2D g2 = (Graphics2D) mImage.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//draw classic controller
g2.drawImage(wiimoteImage, 0, 0, this);
g2.setTransform(new AffineTransform());
if (event != null) {
// draw buttons pushed
ClassicControllerButtonsEvent buttons = event.getButtonsEvent();
/* button A */
if (buttons.isButtonAJustPressed()) {
drawFunction(g2, pressedColor, 53, 353, shapeButton);
}
if (buttons.isButtonAHeld()) {
drawFunction(g2, heldColor, 53, 353, shapeButton);
}
if (buttons.isButtonAJustReleased()) {
drawFunction(g2, releasedColor, 53, 353, shapeButton);
}
/* button B */
if (buttons.isButtonBJustPressed()) {
drawFunction(g2, pressedColor, 53, 353, shapeButton);
}
if (buttons.isButtonBHeld()) {
drawFunction(g2, heldColor, 53, 353, shapeButton);
}
if (buttons.isButtonBJustReleased()) {
drawFunction(g2, releasedColor, 53, 353, shapeButton);
}
/* button Down */
if (buttons.isButtonDownJustPressed()) {
drawFunction(g2, pressedColor, 53, 353, shapeButton);
}
if (buttons.isButtonDownHeld()) {
drawFunction(g2, heldColor, 53, 353, shapeButton);
}
if (buttons.isButtonDownJustReleased()) {
drawFunction(g2, releasedColor, 53, 353, shapeButton);
}
/* button FullLeft */
if (buttons.isButtonFullLeftJustPressed()) {
drawFunction(g2, pressedColor, 53, 353, shapeButton);
}
if (buttons.isButtonFullLeftHeld()) {
drawFunction(g2, heldColor, 53, 353, shapeButton);
}
if (buttons.isButtonFullLeftJustReleased()) {
drawFunction(g2, releasedColor, 53, 353, shapeButton);
}
/* button FullRight */
if (buttons.isButtonFullRightJustPressed()) {
drawFunction(g2, pressedColor, 53, 353, shapeButton);
}
if (buttons.isButtonFullRightHeld()) {
drawFunction(g2, heldColor, 53, 353, shapeButton);
}
if (buttons.isButtonFullRightJustReleased()) {
drawFunction(g2, releasedColor, 53, 353, shapeButton);
}
/* button Home */
if (buttons.isButtonHomeJustPressed()) {
drawFunction(g2, pressedColor, 53, 353, shapeButton);
}
if (buttons.isButtonHomeHeld()) {
drawFunction(g2, heldColor, 53, 353, shapeButton);
}
if (buttons.isButtonHomeJustReleased()) {
drawFunction(g2, releasedColor, 53, 353, shapeButton);
}
/* button Left */
if (buttons.isButtonLeftJustPressed()) {
drawFunction(g2, pressedColor, 53, 353, shapeButton);
}
if (buttons.isButtonLeftHeld()) {
drawFunction(g2, heldColor, 53, 353, shapeButton);
}
if (buttons.isButtonLeftJustReleased()) {
drawFunction(g2, releasedColor, 53, 353, shapeButton);
}
/* button Minus */
if (buttons.isButtonMinusJustPressed()) {
drawFunction(g2, pressedColor, 53, 353, shapeButton);
}
if (buttons.isButtonMinusHeld()) {
drawFunction(g2, heldColor, 53, 353, shapeButton);
}
if (buttons.isButtonMinusJustReleased()) {
drawFunction(g2, releasedColor, 53, 353, shapeButton);
}
/* button Plus */
if (buttons.isButtonPlusJustPressed()) {
drawFunction(g2, pressedColor, 53, 353, shapeButton);
}
if (buttons.isButtonPlusHeld()) {
drawFunction(g2, heldColor, 53, 353, shapeButton);
}
if (buttons.isButtonPlusJustReleased()) {
drawFunction(g2, releasedColor, 53, 353, shapeButton);
}
/* button Right */
if (buttons.isButtonRightJustPressed()) {
drawFunction(g2, pressedColor, 53, 353, shapeButton);
}
if (buttons.isButtonRightHeld()) {
drawFunction(g2, heldColor, 53, 353, shapeButton);
}
if (buttons.isButtonRightJustReleased()) {
drawFunction(g2, releasedColor, 53, 353, shapeButton);
}
/* button Up */
if (buttons.isButtonUpJustPressed()) {
drawFunction(g2, pressedColor, 53, 353, shapeButton);
}
if (buttons.isButtonUpHeld()) {
drawFunction(g2, heldColor, 53, 353, shapeButton);
}
if (buttons.isButtonUpJustReleased()) {
drawFunction(g2, releasedColor, 53, 353, shapeButton);
}
/* button X */
if (buttons.isButtonXJustPressed()) {
drawFunction(g2, pressedColor, 53, 353, shapeButton);
}
if (buttons.isButtonXHeld()) {
drawFunction(g2, heldColor, 53, 353, shapeButton);
}
if (buttons.isButtonXJustReleased()) {
drawFunction(g2, releasedColor, 53, 353, shapeButton);
}
/* button Y */
if (buttons.isButtonYJustPressed()) {
drawFunction(g2, pressedColor, 53, 353, shapeButton);
}
if (buttons.isButtonYHeld()) {
drawFunction(g2, heldColor, 53, 353, shapeButton);
}
if (buttons.isButtonYJustReleased()) {
drawFunction(g2, releasedColor, 53, 353, shapeButton);
}
/* button ZL */
if (buttons.isButtonZLJustPressed()) {
drawFunction(g2, pressedColor, 53, 353, shapeButton);
}
if (buttons.isButtonZLHeld()) {
drawFunction(g2, heldColor, 53, 353, shapeButton);
}
if (buttons.isButtonZLJustReleased()) {
drawFunction(g2, releasedColor, 53, 353, shapeButton);
}
/* button ZR */
if (buttons.isButtonZRJustPressed()) {
drawFunction(g2, pressedColor, 53, 353, shapeButton);
}
if (buttons.isButtonZRHeld()) {
drawFunction(g2, heldColor, 53, 353, shapeButton);
}
if (buttons.isButtonZRJustReleased()) {
drawFunction(g2, releasedColor, 53, 353, shapeButton);
}
// left joystick
JoystickEvent jl = event.getClassicControllerLeftJoystickEvent();
int xCenter1 = 50;
int yCenter1 = 50;
double xAng1 = Math.sin(jl.getAngle() * Math.PI / 180.0) * jl.getMagnitude();
double yAng1 = Math.cos(jl.getAngle() * Math.PI / 180.0) * jl.getMagnitude();
int dx1 = (int) Math.round(shapeJoystick.getBounds().getWidth() / 2);
int dy1 = (int) Math.round(shapeJoystick.getBounds().getHeight() / 2);
double xTemp1 = xAng1 * (xCenter1 - dx1 * 2);
double yTemp1 = yAng1 * (yCenter1 - dy1 * 2);
int x1 = xCenter1 - dx1 + (int) xTemp1;
int y1 = yCenter1 - dy1 - (int) yTemp1;
// shape
g2.translate(x1, y1);
g2.setPaint(joystickColor);
g2.draw(shapeJoystick);
g2.setPaint(joystickColor);
g2.fill(shapeJoystick);
g2.setTransform(new AffineTransform());
//Right joystick
JoystickEvent jr = event.getClassicControllerRightJoystickEvent();
int xCenter2 = 50;
int yCenter2 = 50;
double xAng2 = Math.sin(jl.getAngle() * Math.PI / 180.0) * jl.getMagnitude();
double yAng2 = Math.cos(jl.getAngle() * Math.PI / 180.0) * jl.getMagnitude();
int dx2 = (int) Math.round(shapeJoystick.getBounds().getWidth() / 2);
int dy2 = (int) Math.round(shapeJoystick.getBounds().getHeight() / 2);
double xTemp2 = xAng2 * (xCenter2 - dx2 * 2);
double yTemp2 = yAng2 * (yCenter2 - dy2 * 2);
int x2 = xCenter2 - dx2 + (int) xTemp2;
int y2 = yCenter2 - dy2 - (int) yTemp2;
// shape
g2.translate(x2, y2);
g2.setPaint(joystickColor);
g2.draw(shapeJoystick);
g2.setPaint(joystickColor);
g2.fill(shapeJoystick);
g2.setTransform(new AffineTransform());
// draw shoulders
float leftShoulder = event.getLeftShoulder();
g2.setPaint(shoulderColor);
g2.drawRect(10, 10, 10 + Math.round(leftShoulder * 70), 10 + 20);
g2.setTransform(new AffineTransform());
float rightShoulder = event.getRightShoulder();
g2.setPaint(shoulderColor);
g2.drawRect(10, 10, 10 + Math.round(leftShoulder * 70), 10 + 20);
g2.setTransform(new AffineTransform());
event = null;
}
// put offscreen image on the screen
g.drawImage(mImage, 0, 0, null);
}
/**
* Function used to factorize code.
*
* @param g2
* where to draw a shape.
* @param col
* color to use.
* @param x
* x coordinates.
* @param y
* y coordinates.
* @param sh
* shape to draw.
*/
private void drawFunction(Graphics2D g2, Color col, int x, int y, Shape sh) {
g2.setPaint(col);
g2.translate(x, y);
g2.draw(sh);
g2.fill(sh);
g2.setTransform(new AffineTransform());
}
/**
* check if the mImage variable has been initialized. If it's not the case
* it initializes it with the dimensions of the panel. mImage is for double
* buffering.
*/
private void checkOffScreenImage() {
Dimension d = getSize();
if (mImage == null || mImage.getWidth(null) != d.width || mImage.getHeight(null) != d.height) {
mImage = createImage(d.width, d.height);
}
}
public void onButtonsEvent(WiimoteButtonsEvent arg0) {
//do nothing
}
public void onIrEvent(IREvent arg0) {
//do nothing
}
public void onMotionSensingEvent(MotionSensingEvent arg0) {
//do nothing
}
public void onExpansionEvent(ExpansionEvent arg0) {
if (arg0 instanceof ClassicControllerEvent) {
event = (ClassicControllerEvent) arg0;
}
}
public void onStatusEvent(StatusEvent arg0) {
//do nothing
}
public void onDisconnectionEvent(DisconnectionEvent arg0) {
//do nothing
}
public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) {
//do nothing
}
public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) {
//do nothing
}
public void onGuitarHeroInsertedEvent(GuitarHeroInsertedEvent arg0) {
//do nothing
}
public void onGuitarHeroRemovedEvent(GuitarHeroRemovedEvent arg0) {
//do nothing
}
public void onClassicControllerInsertedEvent(ClassicControllerInsertedEvent arg0) {
//do nothing
}
public void onClassicControllerRemovedEvent(ClassicControllerRemovedEvent arg0) {
clearView();
}
public Color getHeldColor() {
return heldColor;
}
public Color getJoystickColor() {
return joystickColor;
}
public Color getPressedColor() {
return pressedColor;
}
public Color getReleasedColor() {
return releasedColor;
}
public Color getShoulderColor() {
return shoulderColor;
}
public Shape getShapeButton() {
return shapeButton;
}
public Shape getShapeJoystick() {
return shapeJoystick;
}
public void setHeldColor(Color heldColor) {
this.heldColor = heldColor;
}
public void setJoystickColor(Color joystickColor) {
this.joystickColor = joystickColor;
}
public void setPressedColor(Color pressedColor) {
this.pressedColor = pressedColor;
}
public void setReleasedColor(Color releasedColor) {
this.releasedColor = releasedColor;
}
public void setShoulderColor(Color shoulderColor) {
this.shoulderColor = shoulderColor;
}
public void setShapeButton(Shape shapeButton) {
this.shapeButton = shapeButton;
}
public void setShapeJoystick(Shape shapeJoystick) {
this.shapeJoystick = shapeJoystick;
}
public void clearView() {
event = null;
repaint();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}