summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/AbstractStonePanel.java
blob: 8ab18d89b9f248d6e70794adae9a9e0a790ec96d (plain)
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
package jrummikub.view.impl;

import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import jrummikub.model.Position;
import jrummikub.util.Event2;
import jrummikub.view.IClickable;

/**
 * Base class for panels that draw stones
 */
@SuppressWarnings("serial")
abstract class AbstractStonePanel extends JPanel implements IClickable {
  private StonePainter stonePainter;

  private Event2<Position, Boolean> clickEvent = new Event2<Position, Boolean>();
  private Event2<Position, Boolean> rangeClickEvent = new Event2<Position, Boolean>();
  private Event2<Position, Boolean> setClickEvent = new Event2<Position, Boolean>();

  /**
   * @return the stone painter
   */
  protected StonePainter getStonePainter() {
    return stonePainter;
  }

  /**
   * Create a new StonePanel with default scale factor
   */
  public AbstractStonePanel() {
    this(1);
  }

  /**
   * Create a new StonePanel with a given scale factor
   * 
   * @param scale
   *          the grid scale
   */
  public AbstractStonePanel(float scale) {
    super(true); // Set double buffered

    stonePainter = new StonePainter(scale);

    addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
        Insets insets = getInsets();
        Event2<Position, Boolean> event = clickEvent;

        if (e.isShiftDown())
          event = rangeClickEvent;
        else if (e.getClickCount() >= 2)
          event = setClickEvent;

        event.fire(
            stonePainter.calculatePosition(e.getX() - insets.left, e.getY()
                - insets.top), e.isControlDown());
      }
    });
  }

  @Override
  public Event2<Position, Boolean> getClickEvent() {
    return clickEvent;
  }

  @Override
  public Event2<Position, Boolean> getRangeClickEvent() {
    return rangeClickEvent;
  }

  @Override
  public Event2<Position, Boolean> getSetClickEvent() {
    return setClickEvent;
  }

}