annotate src/samer/units/Clicker.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
rev   line source
samer@0 1 /*
samer@0 2 * MatEditor.java
samer@0 3 *
samer@0 4 * Copyright (c) 2000, Samer Abdallah, King's College London.
samer@0 5 * All rights reserved.
samer@0 6 *
samer@0 7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@0 8 * without even the implied warranty of MERCHANTABILITY or
samer@0 9 * FITNESS FOR A PARTICULAR PURPOSE.
samer@0 10 */
samer@0 11
samer@0 12 package samer.units;
samer@0 13
samer@0 14 import java.awt.*;
samer@0 15 import java.awt.event.*;
samer@0 16 import java.util.*;
samer@0 17 import samer.core.*;
samer@0 18 import samer.core.types.*;
samer@0 19 import samer.core.util.*;
samer@0 20
samer@0 21 /**
samer@0 22 This is a mouse event handler that interprets mouse clicks
samer@0 23 to edit a matrix. It assumes that it is handling mouse
samer@0 24 events for a component the contains some representation of the
samer@0 25 matrix that is arranged in rows and columns, just like the
samer@0 26 matrix itself (it doesn't have to be an image). The window
samer@0 27 relative coordinates are simply mapped to a matrix element
samer@0 28 address.
samer@0 29 */
samer@0 30
samer@0 31 public class Clicker extends MouseAdapter
samer@0 32 implements MouseMotionListener // , Agent
samer@0 33 {
samer@0 34 Component canvas;
samer@0 35 int w, h;
samer@0 36 VInteger x, y;
samer@0 37
samer@0 38 public Clicker( Component c, int w, int h)
samer@0 39 {
samer@0 40 canvas=c;
samer@0 41 canvas.addMouseListener(this);
samer@0 42 canvas.addMouseMotionListener(this);
samer@0 43 this.w=w;
samer@0 44 this.h=h;
samer@0 45 x=y=null;
samer@0 46 // vwr.exposeCommands(this);
samer@0 47 }
samer@0 48
samer@0 49 public void setXReceiver(VInteger x) { this.x=x; }
samer@0 50 public void setYReceiver(VInteger y) { this.y=y; }
samer@0 51
samer@0 52 /*
samer@0 53 public void getCommands(Agent.Registry r) { r.add("add",add); }
samer@0 54 public void execute(String cmd, Environment env) {
samer@0 55 add=X._bool(env.datum(),!add);
samer@0 56 }
samer@0 57 */
samer@0 58 private void handle(MouseEvent e)
samer@0 59 {
samer@0 60 // get position in matrix
samer@0 61 int i = (h*e.getY())/canvas.getHeight();
samer@0 62 int j = (w*e.getX())/canvas.getWidth();
samer@0 63 // Shell.print("click: "+i+","+j);
samer@0 64
samer@0 65 // check bounds
samer@0 66 if (i<0 || i>=h) return;
samer@0 67 if (j<0 || j>=w) return;
samer@0 68
samer@0 69 // correct for vertical flip
samer@0 70 i=h-i-1;
samer@0 71
samer@0 72 if (x!=null) x.set(j);
samer@0 73 if (y!=null) y.set(i);
samer@0 74 }
samer@0 75
samer@0 76 public void mouseMoved(MouseEvent e) {}
samer@0 77 public void mouseDragged(MouseEvent e) { handle(e); e.consume(); }
samer@0 78 public void mousePressed(MouseEvent e) { handle(e); e.consume(); }
samer@0 79
samer@0 80 // public void detach() { setto.dispose(); }
samer@0 81 }