joystickView.java
Go to the documentation of this file.
1 package jp.go.aist.hrp.joystick.views;
2 
5 import jp.go.aist.rtm.RTC.*;
6 
7 import org.eclipse.swt.widgets.*;
8 import org.eclipse.ui.part.*;
9 import org.eclipse.swt.events.*;
10 import org.eclipse.swt.graphics.*;
11 import org.eclipse.swt.SWT;
12 
13 
14 
34 public class joystickView extends ViewPart {
35  private Canvas canvas;
36  private static Point pj = new Point(0,0);
37  private static Point PC = pj;
38  private boolean mouseDrag = false;
39  private Region region;
40 
41  public joystickView() {
42  super();
43  }
44 
45 
49  public void execJoystick() {
50  System.out.println("[Joystick] COMPONENT START");
51 
52  String confPath = Activator.getConfigFilePath();
53 
54  if(confPath=="") {
55  System.out.println("Failed to locate the path to configuration file : rtc.conf");
56  System.out.println("Joystick Aborted");
57  } else {
58  String[] args = {"-f", confPath};
59 
60  // String pluginPath = Activator.getPath();
61  //
62  // // Initialize manager
63  // String[] args = {"-f", pluginPath+"/rtc.conf"};
64  final Manager manager = Manager.init(args);
65 
66  // Set module initialization procedure
67  // This procedure will be invoked in activateManager() function.
68  JoystickComp joystickComp = new JoystickComp();
69  manager.setModuleInitProc(joystickComp);
70 
71  if(manager.getPOAManager().get_state().value()!=1) {
72  // Activate manager and register to naming service
73  manager.activateManager();
74 
75  // If you want to run the manager in non-blocking mode, do like this
76  manager.runManager(true);
77  }
78  }
79  }
80 
81  public static Point getJoystickPosition() {
82  Point pos = new Point(pj.x-PC.x,pj.y-PC.y);
83  pos.y = -pos.y;
84  return pos;
85  }
86 
87  public void createPartControl(Composite parent) {
88  canvas = new Canvas(parent, SWT.BORDER | SWT.NO_BACKGROUND);
89  final int pitch=50;
90  execJoystick();
91 
92  // Create a paint handler for the canvas
93  canvas.addPaintListener(new PaintListener() {
94  public void paintControl(PaintEvent e) {
95  Point p0 = canvas.getLocation();
96  Point p1 = canvas.getSize();
97  Point P0 = new Point(p0.x,p0.y);
98  Point P1 = new Point(p0.x+p1.x,p0.y+p1.y);
99  Point pc = new Point((P0.x+P1.x)/2,(P0.y+P1.y)/2);
100  PC = pc;
101 
102  Color col1 = new Color(e.display,221,221,221);
103  Color col2 = new Color(e.display,238,238,238);
104  Color white= new Color(e.display,255,255,255);
105  Color color;
106 
107  Image image = new Image(canvas.getDisplay(), canvas.getBounds());
108  GC gcImage = new GC(image);
109 
110  gcImage.setBackground(e.gc.getBackground());
111  gcImage.fillRectangle(image.getBounds());
112 
113  gcImage.setForeground(white);
114 
115  for(int i=10;i>0;i--) {
116  p0.x = pc.x - pitch * i;
117  p0.y = pc.y - pitch * i;
118  p1.x = pc.x + pitch * i;
119  p1.y = pc.y + pitch * i;
120 
121  if(i%2 == 0) {
122  color = col1;
123  } else {
124  color = col2;
125  }
126  gcImage.setBackground(color);
127  gcImage.fillOval(p0.x,p0.y,p1.x-p0.x,p1.y-p0.y);
128  gcImage.drawOval(p0.x,p0.y,p1.x-p0.x,p1.y-p0.y);
129  }
130  gcImage.setForeground(e.display.getSystemColor(SWT.COLOR_BLACK));
131  gcImage.drawLine(pc.x, P0.y, pc.x, P1.y);
132  gcImage.drawLine(P0.x, pc.y, P1.x, pc.y);
133 
134  gcImage.drawString("x", P1.x-20, pc.y+10);
135  gcImage.drawString("y", pc.x+10, P0.y);
136 
137  if(!mouseDrag) {
138  pj = new Point(pc.x,pc.y);
139  }
140 
141  Point pos = getJoystickPosition();
142  double _r = Math.round(Math.hypot(pos.x, pos.y)*100)/100.0;
143  double _th = Math.round(Math.toDegrees(Math.atan2(pos.y,pos.x))*100)/100.0;
144  gcImage.drawString("x: "+pos.x, (pc.x+P1.x)/2-20, P1.y-40);
145  gcImage.drawString("y: "+pos.y, (pc.x+P1.x)/2+55, P1.y-40);
146  gcImage.drawString("r: "+_r, (pc.x+P1.x)/2-20, P1.y-20);
147  gcImage.drawString("th: "+_th, (pc.x+P1.x)/2+50, P1.y-20);
148 
149  gcImage.setBackground(col1);
150  gcImage.drawLine(pc.x, pc.y, pj.x, pj.y);
151  gcImage.fillOval(pj.x-10, pj.y-10, 20, 20);
152  gcImage.drawOval(pj.x-10, pj.y-10, 20, 20);
153 
154  e.gc.drawImage(image, 0, 0);
155 
156  image.dispose();
157  gcImage.dispose();
158 
159  region = new Region();
160  region.add(new Rectangle(pj.x-10, pj.y-10, 20, 20));
161  }
162  });
163 
164  canvas.addMouseListener(new MouseAdapter() {
165  public void mouseDown(MouseEvent e) {
166  Point ep = new Point(e.x, e.y);
167  if(region.contains(ep)) {
168  mouseDrag = true;
169  }
170  }
171 
172  public void mouseUp(MouseEvent e) {
173  mouseDrag = false;
174  canvas.redraw();
175  }
176  });
177 
178  canvas.addMouseMoveListener(new MouseMoveListener() {
179  public void mouseMove(MouseEvent e) {
180  if(mouseDrag) {
181  pj.x = e.x;
182  pj.y = e.y;
183  canvas.redraw();
184  }
185  }
186  });
187  }
188 
189  public void setFocus() {
190 
191  }
192 
193 }
manager
png_uint_32 i
Definition: png.h:2735
png_bytepp image
Definition: png.h:1772
void execJoystick()
Joystick コンポーネントの立ち上げ
Standalone component Class.
org


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat Apr 13 2019 02:14:23