Mercurial > hg > jslab
diff src/samer/maths/MatEditor.java @ 0:bf79fb79ee13
Initial Mercurial check in.
author | samer |
---|---|
date | Tue, 17 Jan 2012 17:50:20 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/samer/maths/MatEditor.java Tue Jan 17 17:50:20 2012 +0000 @@ -0,0 +1,142 @@ +/* + * 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.maths; + +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 MatEditor extends MouseAdapter + implements MouseMotionListener, Agent +{ + Component canvas; + Mat M; + VDouble setto; + Observable obs; + int lasti=-1, lastj=-1; + double dragval=0; + boolean add=false; + boolean b1down=false; + + public MatEditor( Mat m, Component c, Observable v, Viewer vwr) + { + M=m; + canvas=c; + setto = new VDouble("button1",1,0); + canvas.addMouseListener(this); + canvas.addMouseMotionListener(this); + obs=v; + + vwr.exposeCommands(this); + } + + 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 = (M.height()*e.getY())/canvas.getHeight(); + int j = (M.width()*e.getX())/canvas.getWidth(); + + // check bounds + if (i<0 || i>=M.height()) return; + if (j<0 || j>=M.width()) return; + + // correct for vertical flip + i=M.height()-i-1; + + if (!add) { + if (i==lasti && j==lastj) return; + M.set(i,j,dragval); + } else M.set(i,j,M.get(i,j)+dragval); + + lasti=i; + lastj=j; + obs.notifyObservers(new Point(j,i)); // Point as x,y into image raster + } + + public void mouseMoved(MouseEvent e) {} + public void mouseDragged(MouseEvent e) { if (b1down) { handle(e); e.consume(); } } + public void mousePressed(MouseEvent e) + { + int f = e.getModifiers(); + + if ((f & InputEvent.BUTTON1_MASK)!=0) b1down=true; + if (b1down) { + + // if ((f&(InputEvent.BUTTON2_MASK|InputEvent.BUTTON3_MASK))==0) { + if (!e.isControlDown()) { + dragval=setto.value; + } else { + dragval=add ? -setto.value : 0; + // canvas.addMouseListener(this); // to catch mouse up! + // b2down=true; + } + + lasti=lastj=-1; + handle(e); + e.consume(); + + } + } + + public void mouseReleased(MouseEvent e) + { + int f = e.getModifiers(); + + if ((f & InputEvent.BUTTON1_MASK)!=0) { + // button 1 up - stop all dragging + b1down=false; + + //if (b2down) { + // canvas.removeMouseListener(this); + // b2down=false; + //} + e.consume(); + } /* else { + + // something else up + if (b1down) { + + if ((f&(InputEvent.BUTTON2_MASK|InputEvent.BUTTON3_MASK))!=0) { + // button 2 up + dragval=setto.value; + b2down=false; + canvas.removeMouseListener(this); + handle(e); + } + e.consume(); + + } else { + Shell.print("button up while button 1 not down?"); + } + } */ + } + + public void detach() { setto.dispose(); } +}