Exemplo n.º 1
0
  private static void addNewCanvas(
      final TabFolder tabFolder,
      final ExampleScene scene,
      final FrameHandler frameWork,
      final LogicalLayer logicalLayer) {
    i++;
    logger.info("Adding canvas");

    // Add a new tab to hold our canvas
    final TabItem item = new TabItem(tabFolder, SWT.NONE);
    item.setText("Canvas #" + i);
    tabFolder.setSelection(item);
    final Composite canvasParent = new Composite(tabFolder, SWT.NONE);
    canvasParent.setLayout(new FillLayout());
    item.setControl(canvasParent);

    final GLData data = new GLData();
    data.depthSize = 8;
    data.doubleBuffer = true;

    final SashForm splitter = new SashForm(canvasParent, SWT.HORIZONTAL);

    final SashForm splitterLeft = new SashForm(splitter, SWT.VERTICAL);
    final Composite topLeft = new Composite(splitterLeft, SWT.NONE);
    topLeft.setLayout(new FillLayout());
    final Composite bottomLeft = new Composite(splitterLeft, SWT.NONE);
    bottomLeft.setLayout(new FillLayout());

    final SashForm splitterRight = new SashForm(splitter, SWT.VERTICAL);
    final Composite topRight = new Composite(splitterRight, SWT.NONE);
    topRight.setLayout(new FillLayout());
    final Composite bottomRight = new Composite(splitterRight, SWT.NONE);
    bottomRight.setLayout(new FillLayout());

    canvasParent.layout();

    final SwtCanvas canvas1 = new SwtCanvas(topLeft, SWT.NONE, data);
    final LwjglCanvasRenderer lwjglCanvasRenderer1 = new LwjglCanvasRenderer(scene);
    addCallback(canvas1, lwjglCanvasRenderer1);
    canvas1.setCanvasRenderer(lwjglCanvasRenderer1);
    frameWork.addCanvas(canvas1);
    canvas1.addControlListener(newResizeHandler(canvas1, lwjglCanvasRenderer1));
    canvas1.setFocus();

    final SwtCanvas canvas2 = new SwtCanvas(bottomLeft, SWT.NONE, data);
    final LwjglCanvasRenderer lwjglCanvasRenderer2 = new LwjglCanvasRenderer(scene);
    addCallback(canvas2, lwjglCanvasRenderer2);
    canvas2.setCanvasRenderer(lwjglCanvasRenderer2);
    frameWork.addCanvas(canvas2);
    canvas2.addControlListener(newResizeHandler(canvas2, lwjglCanvasRenderer2));

    final SwtCanvas canvas3 = new SwtCanvas(topRight, SWT.NONE, data);
    final LwjglCanvasRenderer lwjglCanvasRenderer3 = new LwjglCanvasRenderer(scene);
    addCallback(canvas3, lwjglCanvasRenderer3);
    canvas3.setCanvasRenderer(lwjglCanvasRenderer3);
    frameWork.addCanvas(canvas3);
    canvas3.addControlListener(newResizeHandler(canvas3, lwjglCanvasRenderer3));

    final SwtCanvas canvas4 = new SwtCanvas(bottomRight, SWT.NONE, data);
    final LwjglCanvasRenderer lwjglCanvasRenderer4 = new LwjglCanvasRenderer(scene);
    addCallback(canvas4, lwjglCanvasRenderer4);
    canvas4.setCanvasRenderer(lwjglCanvasRenderer4);
    frameWork.addCanvas(canvas4);
    canvas4.addControlListener(newResizeHandler(canvas4, lwjglCanvasRenderer4));

    final SwtKeyboardWrapper keyboardWrapper = new SwtKeyboardWrapper(canvas1);
    final SwtMouseWrapper mouseWrapper = new SwtMouseWrapper(canvas1);
    final SwtFocusWrapper focusWrapper = new SwtFocusWrapper(canvas1);
    final SwtMouseManager mouseManager = new SwtMouseManager(canvas1);
    final ControllerWrapper controllerWrapper = new DummyControllerWrapper();

    final PhysicalLayer pl =
        new PhysicalLayer(keyboardWrapper, mouseWrapper, controllerWrapper, focusWrapper);

    logicalLayer.registerInput(canvas1, pl);

    logicalLayer.registerTrigger(
        new InputTrigger(
            new KeyPressedCondition(Key.H),
            new TriggerAction() {
              public void perform(
                  final Canvas source, final TwoInputStates inputStates, final double tpf) {
                if (source != canvas1) {
                  return;
                }

                if (_showCursor1.get(canvas1)) {
                  mouseManager.setCursor(_cursor1);
                } else {
                  mouseManager.setCursor(_cursor2);
                }

                _showCursor1.put(canvas1, !_showCursor1.get(canvas1));
              }
            }));
    logicalLayer.registerTrigger(
        new InputTrigger(
            new KeyPressedCondition(Key.J),
            new TriggerAction() {
              public void perform(
                  final Canvas source, final TwoInputStates inputStates, final double tpf) {
                if (source != canvas1) {
                  return;
                }

                mouseManager.setCursor(MouseCursor.SYSTEM_DEFAULT);
              }
            }));
    logicalLayer.registerTrigger(
        new InputTrigger(
            new KeyPressedCondition(Key.G),
            new TriggerAction() {
              public void perform(
                  final Canvas source, final TwoInputStates inputStates, final double tpf) {
                if (source != canvas1) {
                  return;
                }

                mouseManager.setGrabbed(
                    mouseManager.getGrabbed() == GrabbedState.NOT_GRABBED
                        ? GrabbedState.GRABBED
                        : GrabbedState.NOT_GRABBED);
              }
            }));

    final AWTImageLoader awtImageLoader = new AWTImageLoader();
    try {
      _cursor1 =
          createMouseCursor(awtImageLoader, "com/ardor3d/example/media/input/wait_cursor.png");
      _cursor2 = createMouseCursor(awtImageLoader, "com/ardor3d/example/media/input/movedata.gif");
    } catch (final IOException ioe) {
      ioe.printStackTrace();
    }

    _showCursor1.put(canvas1, true);
  }