1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
|
public class DrawingCanvas extends Canvas {
/*
* Le contrôleur du canvas
*/
private DrawingController controller;
/*
* La fiche de manoeuvre
*/
private SheetOfManoeuvre sheetOfManoeuvre;
/*
* La marge autour du schéma
*/
private static final float MARGIN = 40;
/*
* L'échelle utilisé pour ce schéma
*/
private float scale = 1f;
/*
* Les curseurs
*/
private Cursor arrowCursor;
private Cursor crossCursor;
/*
* Le rectangle de selection
*/
private Path rectangleFeedback;
private Rectangle2D planBoundsCache;
private boolean planBoundsCacheValid;
public DrawingCanvas(Composite parent, int style, DrawingController controller) {
super(parent, style);
this.controller = controller;
this.sheetOfManoeuvre = controller.getSheetOfManoeuvre();
Rectangle2D plansBounds = getPlanBounds();
Point point = new Point(
Math.round(((float) plansBounds.getWidth() + MARGIN * 2)
* scale) + 2 * getBorderWidth(),
Math.round(((float) plansBounds.getHeight() + MARGIN * 2)
* scale) + 2 * getBorderWidth());
this.computeSize(point.x, point.y);
//initialisation des curseurs
arrowCursor = new Cursor(this.getDisplay(), SWT.CURSOR_ARROW);
crossCursor = new Cursor(this.getDisplay(), SWT.CURSOR_CROSS);
//ajoute des listeners sur la souris
addMouseListeners();
//ajoute des listeners sur les touches clavier
addKeyListener();
//ajoute un listener sur le focus
addFocusListener();
//ajoute des listeners sur le modèle
addModelListener();
//ajoute des listeners sur le dessin
addCanvasListener();
}
public DrawingCanvas getDrawingCanvas(){
return this;
}
/**
* Ajoute un listener sur la souris
*/
private void addMouseListeners() {
this.addMouseListener(new MouseAdapter() {
public void mouseDoubleClick(MouseEvent ev) {
if (isEnabled()) {
controller.mouseDoubleClick(ev.x, ev.y,(ev.stateMask & SWT.SHIFT) != 0);
}
}
public void mouseDown(MouseEvent ev) {
if (isEnabled()){
controller.mouseDown(ev.x,ev.y,(ev.stateMask & SWT.SHIFT) != 0);
}
}
public void mouseUp(MouseEvent ev) {
if (isEnabled()){
controller.mouseUp(ev.x, ev.y);
}
}
});
this.addMouseMoveListener(new MouseMoveListener() {
public void mouseMove(MouseEvent ev) {
if(isEnabled()){
controller.mouseMove(ev.x, ev.y);
}
}
});
}
/**
* Ajoute un listener sur les touches du clavier
*/
private void addKeyListener() {
this.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent ev) {
switch (ev.keyCode) {
case SWT.BS:
case SWT.DEL:
controller.deleteSelection();
break;
case SWT.ESC:
controller.escape();
break;
case SWT.ARROW_LEFT :
controller.moveSelection(-1 / scale, 0);
break;
case SWT.ARROW_UP :
controller.moveSelection(0, -1 / scale);
break;
case SWT.ARROW_DOWN :
controller.moveSelection(0, 1 / scale);
break;
case SWT.ARROW_RIGHT :
controller.moveSelection(1 / scale, 0);
break;
}
}
public void keyReleased(KeyEvent ev) {
}
});
}
/**
* Ajoute un listener
* Adds SWT focus listener to this component that calls back
* <code>controller</code> escape method on focus lost event.
*/
private void addFocusListener() {
this.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent ev) {
controller.escape();
}
});
}
/**
* Ajoute un listener pour recevoir les notifications
* de la fiche de manoeuvre
*/
private void addModelListener() {
//ajoute un listener qui écoute les modifications sur les traits
sheetOfManoeuvre.getDrawing().addLineListener(new LineListener() {
public void lineChanged(LineEvent ev) {
if (getParent() instanceof ScrolledComposite) {
((ScrolledComposite)getParent()).setMinSize(
computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
resetPlanBoundsAndRedraw();
}
});
//ajoute un listener qui écoute les selections
sheetOfManoeuvre.getDrawing().addSelectionListener(new SelectionListener() {
public void selectionChanged(SelectionEvent ev) {
redraw();
}
});
}
/**
* Ajoute un listener pour recevoir les notifications
* de la fiche de manoeuvre
*/
private void addCanvasListener() {
//ajoute un listener qui écoute les modifications sur le dessin
this.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent event) {
DrawingCanvas.this.paintControl(event.gc);
}
});
this.addListener(SWT.Resize, new Listener() {
@Override
public void handleEvent(Event event) {
// Initialisation de la taille de la scrollbar
ScrollBar sbh = DrawingCanvas.this.getHorizontalBar();
sbh.setMinimum(0);
sbh.setMaximum((int) getPlanBounds().getWidth());
sbh.setSelection(0);
sbh.setThumb(getSize().x);
ScrollBar sbv = DrawingCanvas.this.getVerticalBar();
sbv.setMinimum(0);
sbv.setMaximum((int) getPlanBounds().getWidth());
sbv.setSelection(0);
sbv.setThumb(getSize().y);
}});
final ScrollBar hBar = getHorizontalBar ();
hBar.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event e) {
redraw();
}
});
final ScrollBar vBar = getVerticalBar ();
vBar.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event e) {
redraw();
}
});
this.addDisposeListener(new DisposeListener () {
public void widgetDisposed(DisposeEvent e) {
if (rectangleFeedback != null) {
rectangleFeedback.dispose();
}
arrowCursor.dispose();
crossCursor.dispose();
}
});
}
/**
* Dessine le canvas
*/
private void paintControl(GC gc) {
Image image = (Image) getData("double-buffer-image");
if (image == null || image.getBounds().width != getSize().x || image.getBounds().height != getSize().y) {
image = new Image(getDisplay(),getSize().x,getSize().y);
setData("double-buffer-image", image);
}
GC imageGC = new GC(image);
paintBackground(imageGC);
imageGC.setAntialias(SWT.ON);
// Dessine la grille de fond
paintGrid(imageGC);
//Dessine les traits
paintLines(imageGC);
//dessine les rectangles de selections
paintRectangleFeedback(imageGC);
gc.drawImage(image, 0, 0);
imageGC.dispose();
gc.dispose();
}
/**
* Remplit le fond avec la couleur du systeme
*/
private void paintBackground(GC gc) {
gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
gc.fillRectangle(0, 0, this.getSize().x,
this.getSize().y);
}
/**
* Ré-initialise les dimmensions du schéma
* et re-dessine le canvas
*/
private void resetPlanBoundsAndRedraw() {
planBoundsCacheValid = false;
// Re-dessine le canvas
redraw();
}
/**
* Renvoie le rectangle englobant le schéma affiché par ce composant
*/
private Rectangle2D getPlanBounds() {
//Initialise le rectangle englobant le schéma s'il est null
if (this.planBoundsCache == null) {
this.planBoundsCache = new Rectangle2D.Float(0, 0, 1000, 1000);
}
if(!this.planBoundsCacheValid){
//Agrandissement du rectangle englobant les différents items
Rectangle2D itemsBounds = getItemsBounds();
if (itemsBounds != null) {
this.planBoundsCache.add(itemsBounds);
}
this.planBoundsCacheValid = true;
}
//Renvoi de la valeur en cache
return this.planBoundsCache;
}
/**
* Renvoie dimmensions des différents items déssiné dans le Canvas (Trait,
* arc, cercle, équipements)
*/
private Rectangle2D getItemsBounds() {
Rectangle2D itemsBounds = null;
// Agrandissement du rectangle englobant les extrémités des traits
for (Line line : sheetOfManoeuvre.getDrawing().getLines()) {
if (itemsBounds == null) {
itemsBounds = new Rectangle2D.Float(line.getXStart(), line
.getYStart(), 0, 0);
itemsBounds.add(line.getXEnd(), line.getYEnd());
} else {
itemsBounds.add(line.getXStart(), line.getYStart());
itemsBounds.add(line.getXEnd(), line.getYEnd());
}
}
return itemsBounds;
}
/**
* Dessine la grille de fond
*/
private void paintGrid(GC gc) {
float mainGridSize = 100;
float[] gridSizes = new float[] { 1, 2, 5, 10, 20, 50, 100 };
// Compute grid size to get a grid where the space between each line is
// around 10 pixels
float gridSize = gridSizes[0];
for (int i = 1; i < gridSizes.length && gridSize * this.scale < 10; i++) {
gridSize = gridSizes[i];
}
Rectangle2D planBounds = getPlanBounds();
float xMin = (float) planBounds.getMinX() /*- MARGIN*/;
float yMin = (float) planBounds.getMinY() /*- MARGIN*/;
/*float xMax = this.getSize().x;
float yMax = this.getSize().y;*/
float xMax = convertXPixelToModel(this.getSize().x);
float yMax = convertYPixelToModel(this.getSize().y);
gc.setForeground(this.getDisplay().getSystemColor(
SWT.COLOR_GRAY));
// No line thickness in float with SWT !
gc.setLineWidth((int) Math.round(1 / this.scale));
// Draw vertical lines
for (float x = (int) (xMin / gridSize) * gridSize; x < xMax; x += gridSize) {
Path linesPath = new Path(this.getDisplay());
linesPath.moveTo(x, yMin);
linesPath.lineTo(x, yMax);
gc.drawPath(linesPath);
linesPath.dispose();
}
// Draw horizontal lines
for (float y = (int) (yMin / gridSize) * gridSize; y < yMax; y += gridSize) {
Path linesPath = new Path(this.getDisplay());
linesPath.moveTo(xMin, y);
linesPath.lineTo(xMax, y);
gc.drawPath(linesPath);
linesPath.dispose();
}
if (mainGridSize != gridSize) {
gc.setLineWidth((int) Math.round(2 / this.scale));
// Draw main vertical lines
for (float x = (int) (xMin / mainGridSize) * mainGridSize; x < xMax; x += mainGridSize) {
Path linesPath = new Path(this.getDisplay());
linesPath.moveTo(x, yMin);
linesPath.lineTo(x, yMax);
gc.drawPath(linesPath);
linesPath.dispose();
}
// Draw positive main horizontal lines
for (float y = (int) (yMin / mainGridSize) * mainGridSize; y < yMax; y += mainGridSize) {
Path linesPath = new Path(this.getDisplay());
linesPath.moveTo(xMin, y);
linesPath.lineTo(xMax, y);
gc.drawPath(linesPath);
linesPath.dispose();
}
}
}
/**
* Dessine le rectangle de selection
*/
private void paintRectangleFeedback(GC gc) {
if (this.rectangleFeedback != null) {
Color selectionColor = this.getDisplay().getSystemColor(
SWT.COLOR_LIST_SELECTION);
gc.setBackground(selectionColor);
gc.setAlpha(32);
gc.fillPath(this.rectangleFeedback);
gc.setForeground(selectionColor);
gc.setAlpha(255);
gc.setLineWidth((int) Math.round(1 / this.scale));
gc.drawPath(this.rectangleFeedback);
}
}
/**
* Dessine les traits
*/
private void paintLines(GC gc) {
//Dessine les traits selectionnés en les surlignants
List<Object> selectedItems = this.sheetOfManoeuvre.getDrawing().getSelectedItems();
int decalageX = getHorizontalBar().getSelection();
int decalageY = getVerticalBar().getSelection();
if (!selectedItems.isEmpty()) {
gc.setForeground(this.getDisplay().getSystemColor(
SWT.COLOR_LIST_SELECTION));
gc.setAlpha(128);
gc.setLineWidth((int) Math.round(6 / this.scale));
gc.setLineJoin(SWT.JOIN_ROUND);
for (Object item : selectedItems) {
if (item instanceof Line) {
Line line = (Line)item;
gc.drawLine(Math.round(line.getXStart())- decalageX, Math.round(line
.getYStart())- decalageY, Math.round(line.getXEnd())-decalageX, Math
.round(line.getYEnd())- decalageY);
}
}
}
// Dessine les traits
gc.setForeground(this.getDisplay().getSystemColor(
SWT.COLOR_LIST_FOREGROUND));
gc.setAlpha(255);
gc.setLineWidth((int) Math.round(2 / this.scale));
gc.setLineJoin(SWT.JOIN_MITER);
for (Line line : this.sheetOfManoeuvre.getDrawing().getLines()) {
gc.drawLine(Math.round(line.getXStart())- decalageX, Math.round(line
.getYStart())- decalageY, Math.round(line.getXEnd())- decalageX, Math.round(line
.getYEnd())- decalageY);
}
}
/**
* Modifie le rectangle de selection
*/
public void setRectangleFeedback(float x0, float y0, float x1, float y1) {
//supprime le rectangle de selecftion, s'il existe
if (this.rectangleFeedback != null) {
this.rectangleFeedback.dispose();
}
this.rectangleFeedback = new Path(this.getDisplay());
float rx, ry, width, height;
// si le rectangle de selection a une absice négative
if (x1-x0 <0) {
rx = x1;
width = x0-x1;
} else {
rx = x0;
width = x1-x0;
}
//Si le rectangle de selection a une ordonnée négative
if (y1-y0 <0) {
ry = y1;
height = y0-y1;
} else {
ry = y0;
height = y1-y0;
}
this.rectangleFeedback.addRectangle(rx, ry, width, height);
redraw();
}
/**
* Supprime le rectangle de selection
*/
public void deleteRectangleFeedback() {
this.rectangleFeedback = null;
redraw();
}
/**
* S'assure que les traits selectionné sont visible à l'écran
* et bouge les scollbars si besoin
*/
public void makeSelectionVisible() {
List<Object> selectedItems = this.sheetOfManoeuvre.getDrawing().getSelectedItems();
if (!selectedItems.isEmpty()) {
float minX = Float.MAX_VALUE;
float minY = Float.MAX_VALUE;
for (Object item : selectedItems) {
if (item instanceof Line) {
Line line = (Line)item;
minX = Math.min(minX, line.getXStart());
minY = Math.min(minY, line.getYEnd());
}
}
makePointVisible(minX, minY);
}
}
/**
* S'assure que le point au (<code>xPixel</code>, <code>yPixel</code>) est
* est visible et bouge le scrollbars si besoin
*/
public void makePointVisible(float x, float y) {
int decalageX = getHorizontalBar().getSelection();
int decalageY = getVerticalBar().getSelection();
System.out.println("decalageX = " + decalageX+" / x = "+x+" / getSize().x - decalageX = "+ (getSize().x - decalageX));
if( !((decalageX < x ) && ( x < getSize().x - (decalageX+30) ))){
getHorizontalBar().setSelection((int)x);
}else if(!((decalageY < y) && (y < getSize().y - (decalageY+30) ))){
getVerticalBar().setSelection((int)y);
}
}
/**
* Convertit l'abscisse x exprimée en pixel en son abscisse équivalente dans
* le système de coordonnées du modèle
*/
private float convertXPixelToModel(int x) {
Rectangle2D linesBounds = getPlanBounds();
return x / this.scale /*- MARGIN*/ + (float) linesBounds.getMinX();
}
/**
* Convertit l'ordonnée y exprimée en pixel en son ordonnée équivalente dans
* le système de coordonnées du modèle
*/
private float convertYPixelToModel(int y) {
Rectangle2D linesBounds = getPlanBounds();
return y / this.scale /*- MARGIN*/ + (float) linesBounds.getMinY();
}
/**
* Returns the scale used to display the plan.
*/
public float getScale() {
return this.scale;
}
/**
* Modifie le curseur suivant le mode activé
* LINE_CREATION SELECTION
*/
public void setCursor(DrawingController.Mode mode) {
//si le mode est la création de ligne utilisation
//du curseur en forme de croix
if (mode == DrawingController.Mode.LINE_CREATION) {
this.setCursor(this.crossCursor);
} else {
this.setCursor(this.arrowCursor);
}
}
} |
Partager