view 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
line wrap: on
line source
/*
 *	MatEditor.java	
 *
 *	Copyright (c) 2000, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
 *	without even the implied warranty of MERCHANTABILITY or 
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.units;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import samer.core.*;
import samer.core.types.*;
import samer.core.util.*;

/**
	This is a mouse event handler that interprets mouse clicks
	to edit a matrix. It assumes that it is handling mouse
	events for a component the contains some representation of the 
	matrix that is arranged in rows and columns, just like the
	matrix itself (it doesn't have to be an image). The window
	relative coordinates are simply mapped to a matrix element 
	address.
  */

public class Clicker extends MouseAdapter 
	implements MouseMotionListener // , Agent
{
	Component	canvas;
	int				w, h;
	VInteger		x, y;

	public Clicker( Component c, int w, int h)
	{
		canvas=c;
		canvas.addMouseListener(this);
		canvas.addMouseMotionListener(this);
		this.w=w;
		this.h=h;
		x=y=null;
		// vwr.exposeCommands(this);
	}

	public void setXReceiver(VInteger x) { this.x=x; }
	public void setYReceiver(VInteger y) { this.y=y; }
	
/*
	public void getCommands(Agent.Registry r) { r.add("add",add); }
	public void execute(String cmd, Environment env) {	
		add=X._bool(env.datum(),!add); 
	}
*/
	private void handle(MouseEvent e)
	{
		// get position in matrix
		int i = (h*e.getY())/canvas.getHeight();
		int j = (w*e.getX())/canvas.getWidth();
		// Shell.print("click: "+i+","+j);
		
		// check bounds
		if (i<0 || i>=h) return;
		if (j<0 || j>=w) return;

		// correct for vertical flip
		i=h-i-1;

		if (x!=null) x.set(j);
		if (y!=null) y.set(i); 
	}

	public void mouseMoved(MouseEvent e) {}
	public void mouseDragged(MouseEvent e) { handle(e); e.consume(); }
	public void mousePressed(MouseEvent e) { handle(e); e.consume(); }	

//	public void detach() { setto.dispose(); }
}