瀏覽代碼

Better mouse handling

master
Lee 12 年之前
父節點
當前提交
45006aaf8b
共有 1 個檔案被更改,包括 13 行新增30 行删除
  1. +13
    -30
      src/Render.java

+ 13
- 30
src/Render.java 查看文件

@@ -31,7 +31,7 @@ public class Render extends Canvas implements Runnable, MouseListener,
private JFrame _frame;
private JButton pauseButton;
private Random random = new Random();
private int _lastX, _lastY;
private Patch _lastPatch;
public Render(int width, int height, Grid g, String[] rules) {
addMouseListener(this);
@@ -48,7 +48,7 @@ public class Render extends Canvas implements Runnable, MouseListener,
_pixels = ((DataBufferInt) _image.getRaster().getDataBuffer()).getData();
_lastTick = 0;
_rules = rules;
_lastX = _lastY = -1;
_lastPatch = null;
fps_now = 15;
setFrame();
}
@@ -215,35 +215,22 @@ public class Render extends Canvas implements Runnable, MouseListener,
@Override
public void mouseDragged(MouseEvent e) {
int states = Goldfish.getMaxStates(rule) - 1;
if (e.getX() < 0 || e.getY() < 0 || e.getX() / scale > width || e.getY() / scale > height)
if (e.getX() < 0 || e.getY() < 0 || e.getX() / scale >= width || e.getY() / scale >= height)
return;
if (e.getX()/scale != _lastX && e.getY()/scale != _lastY) {
_lastX = e.getX()/scale;
_lastY = e.getY()/scale;
Patch p = _grid.getPatch(_lastX, _lastY);
if (p.getState() == states) {
Patch p = _grid.getPatch(e.getX()/scale, e.getY()/scale);
if (_lastPatch != p) {
if (p.getState() != 0) {
p.setState(0);
} else {
_grid.getPatch(e.getX() / scale, e.getY() / scale).setState(states);
}
_lastPatch = p;
}
e.consume();
}
@Override
public void mouseClicked(MouseEvent e) {
int states = Goldfish.getMaxStates(rule) - 1;
if (e.getX()/scale != _lastX && e.getY()/scale != _lastY) {
_lastX = e.getX()/scale;
_lastY = e.getY()/scale;
Patch p = _grid.getPatch(_lastX, _lastY);
if (p.getState() == states) {
p.setState(0);
} else {
_grid.getPatch(e.getX() / scale, e.getY() / scale).setState(states);
}
}
e.consume();
}
@Override
@@ -257,22 +244,18 @@ public class Render extends Canvas implements Runnable, MouseListener,
@Override
public void mousePressed(MouseEvent e) {
int states = Goldfish.getMaxStates(rule) - 1;
if (e.getX()/scale != _lastX && e.getY()/scale != _lastY) {
_lastX = e.getX()/scale;
_lastY = e.getY()/scale;
Patch p = _grid.getPatch(_lastX, _lastY);
if (p.getState() == states) {
p.setState(0);
} else {
_grid.getPatch(e.getX() / scale, e.getY() / scale).setState(states);
}
Patch p = _grid.getPatch(e.getX()/scale, e.getY()/scale);
if (p.getState() == states) {
p.setState(0);
} else {
_grid.getPatch(e.getX() / scale, e.getY() / scale).setState(states);
}
e.consume();
}
@Override
public void mouseReleased(MouseEvent e) {
_lastX = _lastY = -1;
_lastPatch = null;
}
@Override


Loading…
取消
儲存