joystickView.java
Go to the documentation of this file.
00001 package jp.go.aist.hrp.joystick.views;
00002 
00003 import jp.go.aist.hrp.joystick.Activator;
00004 import jp.go.aist.hrp.joystick.rtc.JoystickComp;
00005 import jp.go.aist.rtm.RTC.*;
00006 
00007 import org.eclipse.swt.widgets.*;
00008 import org.eclipse.ui.part.*;
00009 import org.eclipse.swt.events.*;
00010 import org.eclipse.swt.graphics.*;
00011 import org.eclipse.swt.SWT;
00012 
00013 
00014 
00034 public class joystickView extends ViewPart {
00035         private Canvas canvas;
00036         private static Point pj = new Point(0,0);
00037         private static Point PC = pj;
00038         private boolean mouseDrag = false;
00039         private Region region;
00040         
00041         public joystickView() {
00042         super();        
00043     }
00044     
00045 
00049         public void execJoystick() {
00050         System.out.println("[Joystick] COMPONENT START");
00051         
00052         String confPath = Activator.getConfigFilePath();
00053         
00054         if(confPath=="") {
00055                 System.out.println("Failed to locate the path to configuration file : rtc.conf");
00056                 System.out.println("Joystick Aborted");
00057         } else {
00058                 String[] args = {"-f", confPath};        
00059                 
00060         //      String pluginPath = Activator.getPath();
00061         //        
00062         //      // Initialize manager
00063         //      String[] args = {"-f", pluginPath+"/rtc.conf"};
00064                 final Manager manager = Manager.init(args);
00065                 
00066                 // Set module initialization procedure
00067                 // This procedure will be invoked in activateManager() function.
00068                 JoystickComp joystickComp = new JoystickComp();
00069                 manager.setModuleInitProc(joystickComp); 
00070         
00071                 if(manager.getPOAManager().get_state().value()!=1) {
00072                                 // Activate manager and register to naming service
00073                                 manager.activateManager();
00074                                 
00075                                 // If you want to run the manager in non-blocking mode, do like this
00076                                 manager.runManager(true);
00077                 }
00078         }
00079     }    
00080     
00081     public static Point getJoystickPosition() {
00082         Point pos = new Point(pj.x-PC.x,pj.y-PC.y);
00083         pos.y = -pos.y;
00084         return pos;
00085     }
00086 
00087     public void createPartControl(Composite parent) {
00088         canvas = new Canvas(parent, SWT.BORDER | SWT.NO_BACKGROUND);
00089         final int pitch=50;
00090         execJoystick();
00091         
00092        // Create a paint handler for the canvas
00093         canvas.addPaintListener(new PaintListener() {
00094           public void paintControl(PaintEvent e) {
00095             Point p0 = canvas.getLocation();
00096                 Point p1 = canvas.getSize();
00097                 Point P0 = new Point(p0.x,p0.y);
00098                 Point P1 = new Point(p0.x+p1.x,p0.y+p1.y);
00099                 Point pc = new Point((P0.x+P1.x)/2,(P0.y+P1.y)/2);
00100                 PC = pc;
00101                 
00102                 Color col1 = new Color(e.display,221,221,221);
00103                 Color col2 = new Color(e.display,238,238,238);
00104                 Color white= new Color(e.display,255,255,255);
00105                 Color color; 
00106                 
00107                 Image image = new Image(canvas.getDisplay(), canvas.getBounds());
00108                 GC gcImage = new GC(image);
00109                 
00110             gcImage.setBackground(e.gc.getBackground());
00111             gcImage.fillRectangle(image.getBounds());
00112             
00113             gcImage.setForeground(white);
00114             
00115             for(int i=10;i>0;i--) {
00116                 p0.x = pc.x - pitch * i;
00117                 p0.y = pc.y - pitch * i;
00118                 p1.x = pc.x + pitch * i;
00119                 p1.y = pc.y + pitch * i;
00120                 
00121                 if(i%2 == 0) {
00122                         color = col1;
00123                 } else {
00124                         color = col2;
00125                 }
00126                 gcImage.setBackground(color);
00127                 gcImage.fillOval(p0.x,p0.y,p1.x-p0.x,p1.y-p0.y);                
00128                 gcImage.drawOval(p0.x,p0.y,p1.x-p0.x,p1.y-p0.y);
00129             }
00130             gcImage.setForeground(e.display.getSystemColor(SWT.COLOR_BLACK));
00131             gcImage.drawLine(pc.x, P0.y, pc.x, P1.y);
00132             gcImage.drawLine(P0.x, pc.y, P1.x, pc.y);
00133               
00134             gcImage.drawString("x", P1.x-20, pc.y+10);
00135             gcImage.drawString("y", pc.x+10, P0.y);
00136             
00137             if(!mouseDrag) {
00138                 pj = new Point(pc.x,pc.y);
00139             }
00140             
00141             Point pos = getJoystickPosition();
00142             double _r = Math.round(Math.hypot(pos.x, pos.y)*100)/100.0;
00143             double _th =  Math.round(Math.toDegrees(Math.atan2(pos.y,pos.x))*100)/100.0;
00144             gcImage.drawString("x: "+pos.x, (pc.x+P1.x)/2-20, P1.y-40);
00145             gcImage.drawString("y: "+pos.y, (pc.x+P1.x)/2+55, P1.y-40);
00146             gcImage.drawString("r: "+_r, (pc.x+P1.x)/2-20, P1.y-20);
00147             gcImage.drawString("th: "+_th, (pc.x+P1.x)/2+50, P1.y-20);
00148             
00149             gcImage.setBackground(col1);
00150             gcImage.drawLine(pc.x, pc.y, pj.x, pj.y);
00151             gcImage.fillOval(pj.x-10, pj.y-10, 20, 20);                
00152             gcImage.drawOval(pj.x-10, pj.y-10, 20, 20);
00153             
00154             e.gc.drawImage(image, 0, 0);
00155 
00156             image.dispose();
00157             gcImage.dispose();
00158             
00159             region = new Region();
00160             region.add(new Rectangle(pj.x-10, pj.y-10, 20, 20));
00161           }
00162         });
00163         
00164         canvas.addMouseListener(new MouseAdapter() {
00165                 public void mouseDown(MouseEvent e) {
00166                         Point ep = new Point(e.x, e.y);
00167                         if(region.contains(ep)) {
00168                                 mouseDrag = true;
00169                         }
00170                 }
00171                 
00172                 public void mouseUp(MouseEvent e) {
00173                         mouseDrag = false;
00174                         canvas.redraw();
00175                 }
00176         });
00177         
00178         canvas.addMouseMoveListener(new MouseMoveListener() {
00179                         public void mouseMove(MouseEvent e) {
00180                                 if(mouseDrag) {
00181                                         pj.x = e.x;
00182                                         pj.y = e.y;
00183                                         canvas.redraw();
00184                                 }
00185                         } 
00186         });
00187     }
00188 
00189     public void setFocus() { 
00190         
00191     }
00192 
00193 } 


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:55