Mercurial > hg > jslab
comparison src/samer/maths/MatEditor.java @ 0:bf79fb79ee13
Initial Mercurial check in.
author | samer |
---|---|
date | Tue, 17 Jan 2012 17:50:20 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:bf79fb79ee13 |
---|---|
1 /* | |
2 * MatEditor.java | |
3 * | |
4 * Copyright (c) 2000, Samer Abdallah, King's College London. | |
5 * All rights reserved. | |
6 * | |
7 * This software is provided AS iS and WITHOUT ANY WARRANTY; | |
8 * without even the implied warranty of MERCHANTABILITY or | |
9 * FITNESS FOR A PARTICULAR PURPOSE. | |
10 */ | |
11 | |
12 package samer.maths; | |
13 | |
14 import java.awt.*; | |
15 import java.awt.event.*; | |
16 import java.util.*; | |
17 import samer.core.*; | |
18 import samer.core.types.*; | |
19 import samer.core.util.*; | |
20 | |
21 /** | |
22 This is a mouse event handler that interprets mouse clicks | |
23 to edit a matrix. It assumes that it is handling mouse | |
24 events for a component the contains some representation of the | |
25 matrix that is arranged in rows and columns, just like the | |
26 matrix itself (it doesn't have to be an image). The window | |
27 relative coordinates are simply mapped to a matrix element | |
28 address. | |
29 */ | |
30 | |
31 public class MatEditor extends MouseAdapter | |
32 implements MouseMotionListener, Agent | |
33 { | |
34 Component canvas; | |
35 Mat M; | |
36 VDouble setto; | |
37 Observable obs; | |
38 int lasti=-1, lastj=-1; | |
39 double dragval=0; | |
40 boolean add=false; | |
41 boolean b1down=false; | |
42 | |
43 public MatEditor( Mat m, Component c, Observable v, Viewer vwr) | |
44 { | |
45 M=m; | |
46 canvas=c; | |
47 setto = new VDouble("button1",1,0); | |
48 canvas.addMouseListener(this); | |
49 canvas.addMouseMotionListener(this); | |
50 obs=v; | |
51 | |
52 vwr.exposeCommands(this); | |
53 } | |
54 | |
55 public void getCommands(Agent.Registry r) { r.add("add",add); } | |
56 public void execute(String cmd, Environment env) { | |
57 add=X._bool(env.datum(),!add); | |
58 } | |
59 | |
60 private void handle(MouseEvent e) | |
61 { | |
62 // get position in matrix | |
63 int i = (M.height()*e.getY())/canvas.getHeight(); | |
64 int j = (M.width()*e.getX())/canvas.getWidth(); | |
65 | |
66 // check bounds | |
67 if (i<0 || i>=M.height()) return; | |
68 if (j<0 || j>=M.width()) return; | |
69 | |
70 // correct for vertical flip | |
71 i=M.height()-i-1; | |
72 | |
73 if (!add) { | |
74 if (i==lasti && j==lastj) return; | |
75 M.set(i,j,dragval); | |
76 } else M.set(i,j,M.get(i,j)+dragval); | |
77 | |
78 lasti=i; | |
79 lastj=j; | |
80 obs.notifyObservers(new Point(j,i)); // Point as x,y into image raster | |
81 } | |
82 | |
83 public void mouseMoved(MouseEvent e) {} | |
84 public void mouseDragged(MouseEvent e) { if (b1down) { handle(e); e.consume(); } } | |
85 public void mousePressed(MouseEvent e) | |
86 { | |
87 int f = e.getModifiers(); | |
88 | |
89 if ((f & InputEvent.BUTTON1_MASK)!=0) b1down=true; | |
90 if (b1down) { | |
91 | |
92 // if ((f&(InputEvent.BUTTON2_MASK|InputEvent.BUTTON3_MASK))==0) { | |
93 if (!e.isControlDown()) { | |
94 dragval=setto.value; | |
95 } else { | |
96 dragval=add ? -setto.value : 0; | |
97 // canvas.addMouseListener(this); // to catch mouse up! | |
98 // b2down=true; | |
99 } | |
100 | |
101 lasti=lastj=-1; | |
102 handle(e); | |
103 e.consume(); | |
104 | |
105 } | |
106 } | |
107 | |
108 public void mouseReleased(MouseEvent e) | |
109 { | |
110 int f = e.getModifiers(); | |
111 | |
112 if ((f & InputEvent.BUTTON1_MASK)!=0) { | |
113 // button 1 up - stop all dragging | |
114 b1down=false; | |
115 | |
116 //if (b2down) { | |
117 // canvas.removeMouseListener(this); | |
118 // b2down=false; | |
119 //} | |
120 e.consume(); | |
121 } /* else { | |
122 | |
123 // something else up | |
124 if (b1down) { | |
125 | |
126 if ((f&(InputEvent.BUTTON2_MASK|InputEvent.BUTTON3_MASK))!=0) { | |
127 // button 2 up | |
128 dragval=setto.value; | |
129 b2down=false; | |
130 canvas.removeMouseListener(this); | |
131 handle(e); | |
132 } | |
133 e.consume(); | |
134 | |
135 } else { | |
136 Shell.print("button up while button 1 not down?"); | |
137 } | |
138 } */ | |
139 } | |
140 | |
141 public void detach() { setto.dispose(); } | |
142 } |