summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/StonePanel.java
blob: 9bcffaed78824facc761255189d0ce537e74f05c (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
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;

@SuppressWarnings("serial")
abstract class StonePanel 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>();
  
  protected StonePainter getStonePainter() {
    return stonePainter;
  }
  
  public StonePanel(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;
  }

}