GrxRangeSensorView.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
10 /*
11  * GrxRangeSensorView.java
12  *
13  * Copyright (C) 2007 GeneralRobotix, Inc.
14  * All Rights Reserved
15  *
16  * @author Yuichiro Kawasumi (General Robotix, Inc.)
17  */
18 
19 package com.generalrobotix.ui.view;
20 
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24 
25 import jp.go.aist.hrp.simulator.SensorState;
26 
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.PaintEvent;
29 import org.eclipse.swt.events.PaintListener;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.events.SelectionListener;
32 import org.eclipse.swt.graphics.Rectangle;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Canvas;
36 import org.eclipse.swt.widgets.Combo;
37 import org.eclipse.swt.widgets.Composite;
38 
50 
54 @SuppressWarnings("serial")
55 public class GrxRangeSensorView extends GrxBaseView implements PaintListener{
56  public static final String TITLE = "Range Sensor View";
57 
58  private Composite canvas_;
59  private GrxWorldStateItem currentWorld_ = null;
60  private GrxModelItem currentModel_ = null;
61  private GrxSensorItem currentSensor_ = null;
62  private SensorState currentSensorState_ = null;
63 
64  private Combo comboModelName_;
65  private Combo comboSensorName_;
66  private List<GrxModelItem> modelList_;
67  private List<GrxSensorItem> sensorList_;
68 
76  public GrxRangeSensorView(String name, GrxPluginManager manager, GrxBaseViewPart vp, Composite parent) {
77  super(name, manager, vp, parent);
78 
79  GridLayout layout = new GridLayout(1, false);
80  composite_.setLayout(layout);
81 
82  Composite northPane = new Composite(composite_,SWT.NONE);
83  northPane.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
84  GridLayout gridLayout = new GridLayout(2,true);
85  northPane.setLayout(gridLayout);
86 
87  comboModelName_ = new Combo(northPane,SWT.READ_ONLY);
88  comboModelName_.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
89  modelList_ = new ArrayList<GrxModelItem>();
90 
91  comboModelName_.addSelectionListener(new SelectionListener(){
92  public void widgetSelected(SelectionEvent e) {
93  GrxModelItem item = modelList_.get(comboModelName_.getSelectionIndex());
94  if (item == null || item == currentModel_)
95  return;
96 
97  currentModel_ = item;
98  List<GrxSensorItem> sensors = currentModel_.getSensors("Range");
99  comboSensorName_.removeAll();
100  sensorList_.clear();
101  if (sensors != null){
102  for (int i=0; i<sensors.size(); i++){
103  comboSensorName_.add(sensors.get(i).getName());
104  sensorList_.add(sensors.get(i));
105  }
106  if(sensors.size()>0){
107  comboSensorName_.select(0);
108  comboSensorName_.notifyListeners(SWT.Selection, null);
109  }
110  }else
111  updateCanvas(null);
112 
113  }
114  public void widgetDefaultSelected(SelectionEvent e) {
115  }
116  });
117 
118  comboSensorName_ = new Combo(northPane,SWT.READ_ONLY);
119  comboSensorName_.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
120  sensorList_ = new ArrayList<GrxSensorItem>();
121 
122  comboSensorName_.addSelectionListener(new SelectionListener(){
123  public void widgetSelected(SelectionEvent e){
124  currentSensor_ = sensorList_.get(comboSensorName_.getSelectionIndex());
125  if(currentWorld_!=null)
126  updateCanvas(currentWorld_.getValue());
127  }
128  public void widgetDefaultSelected(SelectionEvent e) {
129  }
130  });
131  canvas_ = new Canvas(composite_,SWT.NONE);
132  canvas_.addPaintListener(this);
133  canvas_.setLayoutData(new GridData(GridData.FILL_BOTH));
134 
135  setUp();
136  manager_.registerItemChangeListener(this, GrxModelItem.class);
137  manager_.registerItemChangeListener(this, GrxWorldStateItem.class);
138  }
139 
140  public void setUp(){
141  Iterator<GrxModelItem> it = modelList_.iterator();
142  while(it.hasNext())
143  comboModelName_.remove(it.next().getName());
144  modelList_ = manager_.<GrxModelItem>getSelectedItemList(GrxModelItem.class);
145  it = modelList_.iterator();
146  while(it.hasNext())
147  comboModelName_.add(it.next().getName());
148  if(comboModelName_.getItemCount()>0){
149  comboModelName_.select(0);
150  comboModelName_.notifyListeners(SWT.Selection, null);
151  }
152 
153  if(currentWorld_ != null){
154  currentWorld_.deleteObserver(this);
155  currentWorld_.deletePosObserver(this);
156  }
157  currentWorld_ = manager_.<GrxWorldStateItem>getSelectedItem(GrxWorldStateItem.class, null);
158  if(currentWorld_!=null){
159  currentWorld_.addObserver(this);
160  currentWorld_.addPosObserver(this);
161  updateCanvas(currentWorld_.getValue());
162  }else
163  updateCanvas(null);
164 
165  }
166 
171  public void paintControl(PaintEvent e) {
172  if (currentSensor_ != null && currentSensorState_ != null){
173  double step = currentSensor_.getDbl("scanStep", 0.1);
174  double maxD = currentSensor_.getDbl("maxDistance", 10.0);
175  if (currentSensor_.id_ >= 0 && currentSensor_.id_ < currentSensorState_.range.length){
176  Rectangle bounds = canvas_.getBounds();
177  if(bounds.height < bounds.width) {
178  bounds.x = (bounds.width - bounds.height) / 2;
179  bounds.width = bounds.height;
180  bounds.y = 0;
181  } else {
182  bounds.y = (bounds.height - bounds.width) / 2;
183  bounds.height = bounds.width;
184  bounds.x = 0;
185  }
186  e.gc.drawArc(bounds.x, bounds.y, bounds.width-1, bounds.height-1,0,360);
187  double [] distances = currentSensorState_.range[currentSensor_.id_];
188  int centerx = bounds.x + bounds.width / 2;
189  int centery = bounds.y + bounds.height / 2;
190  double scale = bounds.width/(maxD*2);
191  e.gc.setBackground(Activator.getDefault().getColor("darkGray"));
192  int stepAngle = (int)(step*57.29579+0.5);
193  int half = (int)(distances.length/2);
194  double startAngle = 1.570796-step*half-step/2;
195  for(int i=0; i<distances.length; i++){
196  double distance = distances[i];
197  if(distance==0)
198  distance = maxD;
199  int iDistance = (int)(scale*distance);
200  e.gc.fillArc(centerx-iDistance, centery-iDistance, iDistance*2, iDistance*2, (int)(startAngle*57.29579), stepAngle);
201  startAngle += step;
202  }
203  }
204  }
205  }
206 
207  public void updatePosition(GrxBasePlugin plugin, Integer arg_pos){
208  if(currentWorld_!=plugin) return;
209 
210  int pos = arg_pos.intValue();
211  WorldStateEx state = currentWorld_.getValue(pos);
212  updateCanvas(state);
213  }
214 
215  private void updateCanvas( WorldStateEx state ){
216  if (state != null && currentModel_ != null) {
217  CharacterStateEx charStat = state.get(currentModel_.getName());
218  if (charStat != null ) {
219  currentSensorState_ = charStat.sensorState;
220  canvas_.redraw();
221  }
222  }else{
223  currentSensorState_ = null;
224  canvas_.redraw();
225  }
226  }
227 
228  public void registerItemChange(GrxBaseItem item, int event){
229  if(item instanceof GrxModelItem){
230  GrxModelItem modelItem = (GrxModelItem) item;
231  switch(event){
233  if(!modelList_.contains(modelItem)){
234  modelList_.add(modelItem);
235  comboModelName_.add(modelItem.getName());
236  if(currentModel_==null){
237  comboModelName_.select(0);
238  comboModelName_.notifyListeners(SWT.Selection, null);
239  }
240  }
241  break;
243  if(modelList_.contains(modelItem)){
244  modelList_.remove(modelItem);
245  comboModelName_.remove(modelItem.getName());
246  if(currentModel_==modelItem){
247  if(modelList_.size()>0){
248  comboModelName_.select(0);
249  comboModelName_.notifyListeners(SWT.Selection, null);
250  }else
251  currentModel_=null;
252  }
253  }
254  break;
255  default:
256  break;
257  }
258  }else if(item instanceof GrxWorldStateItem){
259  GrxWorldStateItem worldStateItem = (GrxWorldStateItem) item;
260  switch(event){
262  if(currentWorld_!=worldStateItem){
263  currentWorld_ = worldStateItem;
264  currentWorld_.addObserver(this);
265  currentWorld_.addPosObserver(this);
266  updateCanvas(currentWorld_.getValue());
267  }
268  break;
271  if(currentWorld_==worldStateItem){
272  currentWorld_.deleteObserver(this);
273  currentWorld_.deletePosObserver(this);
274  currentWorld_ = null;
275  updateCanvas(null);
276  }
277  break;
278  default:
279  break;
280  }
281  }
282  }
283 
284  public void shutdown() {
285  manager_.removeItemChangeListener(this, GrxModelItem.class);
286  manager_.removeItemChangeListener(this, GrxWorldStateItem.class);
287  if(currentWorld_!=null) {
288  currentWorld_.deleteObserver(this);
289  currentWorld_.deletePosObserver(this);
290  }
291  }
292 }
state
#define null
our own NULL pointer
Definition: IceTypes.h:57
png_infop png_charpp name
Definition: png.h:2382
item corresponds to a robot model
void paintControl(PaintEvent e)
draw range sensor output if sensor is choosed
png_uint_32 i
Definition: png.h:2735
void registerItemChange(GrxBaseItem item, int event)
void updatePosition(GrxBasePlugin plugin, Integer arg_pos)
List< GrxSensorItem > getSensors(String type)
final Double getDbl(String key, Double defaultVal)
get double value associated to key
typedef int
Definition: png.h:1113
プラグイン管理クラス GrxUIの核になるクラス。プラグインのロード等の、初期化を実行する。 プラグインとそ...
org
GrxRangeSensorView(String name, GrxPluginManager manager, GrxBaseViewPart vp, Composite parent)
constructor


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Sep 8 2022 02:24:03