samer@0: /* samer@0: * MatEditor.java samer@0: * samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.maths; samer@0: samer@0: import java.awt.*; samer@0: import java.awt.event.*; samer@0: import java.util.*; samer@0: import samer.core.*; samer@0: import samer.core.types.*; samer@0: import samer.core.util.*; samer@0: samer@0: /** samer@0: This is a mouse event handler that interprets mouse clicks samer@0: to edit a matrix. It assumes that it is handling mouse samer@0: events for a component the contains some representation of the samer@0: matrix that is arranged in rows and columns, just like the samer@0: matrix itself (it doesn't have to be an image). The window samer@0: relative coordinates are simply mapped to a matrix element samer@0: address. samer@0: */ samer@0: samer@0: public class MatEditor extends MouseAdapter samer@0: implements MouseMotionListener, Agent samer@0: { samer@0: Component canvas; samer@0: Mat M; samer@0: VDouble setto; samer@0: Observable obs; samer@0: int lasti=-1, lastj=-1; samer@0: double dragval=0; samer@0: boolean add=false; samer@0: boolean b1down=false; samer@0: samer@0: public MatEditor( Mat m, Component c, Observable v, Viewer vwr) samer@0: { samer@0: M=m; samer@0: canvas=c; samer@0: setto = new VDouble("button1",1,0); samer@0: canvas.addMouseListener(this); samer@0: canvas.addMouseMotionListener(this); samer@0: obs=v; samer@0: samer@0: vwr.exposeCommands(this); samer@0: } samer@0: samer@0: public void getCommands(Agent.Registry r) { r.add("add",add); } samer@0: public void execute(String cmd, Environment env) { samer@0: add=X._bool(env.datum(),!add); samer@0: } samer@0: samer@0: private void handle(MouseEvent e) samer@0: { samer@0: // get position in matrix samer@0: int i = (M.height()*e.getY())/canvas.getHeight(); samer@0: int j = (M.width()*e.getX())/canvas.getWidth(); samer@0: samer@0: // check bounds samer@0: if (i<0 || i>=M.height()) return; samer@0: if (j<0 || j>=M.width()) return; samer@0: samer@0: // correct for vertical flip samer@0: i=M.height()-i-1; samer@0: samer@0: if (!add) { samer@0: if (i==lasti && j==lastj) return; samer@0: M.set(i,j,dragval); samer@0: } else M.set(i,j,M.get(i,j)+dragval); samer@0: samer@0: lasti=i; samer@0: lastj=j; samer@0: obs.notifyObservers(new Point(j,i)); // Point as x,y into image raster samer@0: } samer@0: samer@0: public void mouseMoved(MouseEvent e) {} samer@0: public void mouseDragged(MouseEvent e) { if (b1down) { handle(e); e.consume(); } } samer@0: public void mousePressed(MouseEvent e) samer@0: { samer@0: int f = e.getModifiers(); samer@0: samer@0: if ((f & InputEvent.BUTTON1_MASK)!=0) b1down=true; samer@0: if (b1down) { samer@0: samer@0: // if ((f&(InputEvent.BUTTON2_MASK|InputEvent.BUTTON3_MASK))==0) { samer@0: if (!e.isControlDown()) { samer@0: dragval=setto.value; samer@0: } else { samer@0: dragval=add ? -setto.value : 0; samer@0: // canvas.addMouseListener(this); // to catch mouse up! samer@0: // b2down=true; samer@0: } samer@0: samer@0: lasti=lastj=-1; samer@0: handle(e); samer@0: e.consume(); samer@0: samer@0: } samer@0: } samer@0: samer@0: public void mouseReleased(MouseEvent e) samer@0: { samer@0: int f = e.getModifiers(); samer@0: samer@0: if ((f & InputEvent.BUTTON1_MASK)!=0) { samer@0: // button 1 up - stop all dragging samer@0: b1down=false; samer@0: samer@0: //if (b2down) { samer@0: // canvas.removeMouseListener(this); samer@0: // b2down=false; samer@0: //} samer@0: e.consume(); samer@0: } /* else { samer@0: samer@0: // something else up samer@0: if (b1down) { samer@0: samer@0: if ((f&(InputEvent.BUTTON2_MASK|InputEvent.BUTTON3_MASK))!=0) { samer@0: // button 2 up samer@0: dragval=setto.value; samer@0: b2down=false; samer@0: canvas.removeMouseListener(this); samer@0: handle(e); samer@0: } samer@0: e.consume(); samer@0: samer@0: } else { samer@0: Shell.print("button up while button 1 not down?"); samer@0: } samer@0: } */ samer@0: } samer@0: samer@0: public void detach() { setto.dispose(); } samer@0: }