ImageJ base code to get access to all the classes and their methods to test new Plugins. https://imagejdocu.tudor.lu/howto/plugins/the_imagej_eclipse_howto
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
9.8 KiB

2 years ago
  1. // hit = 0x01<<i (left, middle, right, top, center, bottom)
  2. // r remarkable points, 6 for image + 6 per ROIs, same order as hit
  3. // dragged roi snaps to guides.
  4. macro "Move Overlay Tool - C037O33aaL03f3L303f" {
  5. getCursorLoc( x, y, z, flags );
  6. selectNone( );
  7. r = newArray( 0, getWidth / 2, getWidth, 0, getHeight / 2, getHeight );
  8. n = Overlay.size;
  9. if( n < 1 ) exit( "Overlay required" );
  10. id = Overlay.indexAt( x, y );
  11. for( i = 0; i < n; i ++ ) {
  12. if( i != id ) {
  13. Overlay.getBounds( i, bx, by, bw, bh );
  14. er = newArray( bx, bx + bw / 2, bx + bw, by, by + bh / 2, by + bh );
  15. r = Array.concat( r, er );
  16. }
  17. }
  18. while( flags & 16 > 0 ) {
  19. getCursorLoc( x, y, z, flags );
  20. Overlay.getBounds( id, bx, by, bw, bh );
  21. if( id < 0 ) break;
  22. Overlay.moveSelection( id, x - bw / 2, y - bh / 2 );
  23. Overlay.getBounds( id, bx, by, bw, bh );
  24. d = newArray( bx, bx + bw / 2, bx + bw, by, by + bw / 2, by + bh );
  25. while( Overlay.size > n ) Overlay.removeSelection( Overlay.size - 1 );
  26. hit = 0x00;
  27. for( i = 0; i < r.length; i = i + 6 ) { // each element
  28. for( p = 0; p < 6; p ++ ) // each remarkable point
  29. if( abs( d [ p ]- r [ i + p ] )< 5 ) {
  30. hit = hit |( 0x01 << p ); // allows for multiple hits
  31. }
  32. if( hit > 0 ) {
  33. ihit = i;
  34. break;
  35. }
  36. }
  37. for( i = 0; i < 6; i ++ ) {
  38. if( hit &( 1 << i )> 0 ) {
  39. if( i < 3 ) {
  40. Overlay.getBounds( id, bx, by, bw, bh );
  41. Overlay.drawLine( r [ ihit + i ], 0, r [ ihit + i ], getHeight );
  42. if( i == 0 ) Overlay.moveSelection( id, r [ ihit + i ], by );
  43. if( i == 1 ) Overlay.moveSelection( id, r [ ihit + i ]- bw / 2, by );
  44. if( i == 2 ) Overlay.moveSelection( id, r [ ihit + i ]- bw, by );
  45. }
  46. if( i >= 3 ) {
  47. Overlay.getBounds( id, bx, by, bw, bh );
  48. Overlay.drawLine( 0, r [ ihit + i ], getWidth, r [ ihit + i ] );
  49. if( i == 3 ) Overlay.moveSelection( id, bx, r [ ihit + i ] );
  50. if( i == 4 ) Overlay.moveSelection( id, bx, r [ ihit + i ]- bh / 2 );
  51. if( i == 5 ) Overlay.moveSelection( id, bx, r [ ihit + i ]- bh );
  52. }
  53. }
  54. }
  55. Overlay.show;
  56. wait( 20 );
  57. }
  58. while( Overlay.size > n ) Overlay.removeSelection( Overlay.size - 1 );
  59. }
  60. // a tool to select one or more overlay elements
  61. // selection is stored in preferences
  62. // click outside a roi to deselect all
  63. // a menu tool to align selected overlay elements
  64. macro "Select Overlay Tool - Ce00R22fbC0e0o0044C037L77ffL777aL77a7" {
  65. getCursorLoc( x, y, z, flags );
  66. Overlay.removeRois( 'ToolSelectedOverlayElement' );
  67. Overlay.show;
  68. n = Overlay.size;
  69. if( n < 1 ) exit( "Overlay required" );
  70. id = Overlay.indexAt( x, y );
  71. if( id < 0 ) {
  72. selectNone( );
  73. call( 'ij.Prefs.set', 'overlaytoolset.selected', '' );
  74. exit( );
  75. }
  76. if( flags & 1 > 0 ) {
  77. Overlay.activateSelection( id );
  78. if( Roi.getName != 'ToolSelectedOverlayElement' )
  79. selectElement( id, true );
  80. } else {
  81. Overlay.activateSelection( id );
  82. if( Roi.getName != 'ToolSelectedOverlayElement' )
  83. selectElement( id, false );
  84. }
  85. run( "Select None" );
  86. highlightSelectedROIs( );
  87. }
  88. function highlightSelectedROIs( ) {
  89. Overlay.removeRois( 'ToolSelectedOverlayElement' );
  90. selected = getSelectedElements( );
  91. s = split( selected, ',' );
  92. //print( selected );
  93. for( i = 0; i < s.length; i ++ ) {
  94. id = s [ i ];
  95. Overlay.getBounds( id, bx, by, bw, bh );
  96. makeRectangle( bx, by, bw, bh );
  97. Roi.setName( 'ToolSelectedOverlayElement' );
  98. Overlay.addSelection( '#90ff0000', 3 );
  99. }
  100. run( "Select None" );
  101. Overlay.show;
  102. }
  103. function selectNone( ) {
  104. Overlay.removeRois( 'ToolSelectedOverlayElement' );
  105. Overlay.show;
  106. //call( 'ij.Prefs.set', 'overlaytoolset.selected', '' );
  107. }
  108. function selectElement( id, add ) {
  109. if( add == true ) {
  110. selected = getSelectedElements( );
  111. s = split( selected, ',' );
  112. isSelected = false;
  113. for( i = 0; i < s.length; i ++ ) {
  114. if( 1 * s [ i ]== id ) isSelected = true;
  115. }
  116. if( ! isSelected ) {
  117. call( 'ij.Prefs.set', 'overlaytoolset.selected', selected + ',' + id );
  118. selected = getSelectedElements( );
  119. } else {
  120. unselectElement( id );
  121. }
  122. } else {
  123. call( 'ij.Prefs.set', 'overlaytoolset.selected', id );
  124. }
  125. highlightSelectedROIs( );
  126. }
  127. function unselectElement( id ) {
  128. selected = getSelectedElements( );
  129. s = split( selected, ',' );
  130. selected = '';
  131. for( i = 0; i < s.length; i ++ ) {
  132. if( s [ i ]!= id ) selected = selected + s [ i ]+ ',';
  133. }
  134. call( 'ij.Prefs.set', 'overlaytoolset.selected', selected );
  135. selected = getSelectedElements( );
  136. highlightSelectedROIs( );
  137. run( "Select None" );
  138. }
  139. function getSelectedElements( ) {
  140. selected = call( 'ij.Prefs.get', 'overlaytoolset.selected', '' );
  141. while( selected.endsWith( "," ) ) selected = substring( selected, 0, lengthOf( selected )- 1 );
  142. while( selected.startsWith( "," ) ) selected = substring( selected, 1, lengthOf( selected ) );
  143. return selected;
  144. }
  145. var dCmds = newMenu( "Align Overlay Menu Tool",
  146. newArray( "Top", "Middle", "Bottom", "-", "Left", "Center", "Right" ) );
  147. // a menu tool to align selected ROIs
  148. macro "Align Overlay Menu Tool - C037L00f0R2244R8248" {
  149. cmd = getArgument( );
  150. if( cmd == "Top" ) {
  151. s = getSelectedElements( );
  152. s = split( s, ',' );
  153. if( s.length < 1 ) exit( );
  154. tops = newArray( s.length );
  155. for( i = 0; i < tops.length; i ++ ) {
  156. Overlay.getBounds( s [ i ], x, y, w, h );
  157. tops [ i ]= y;
  158. }
  159. //Array.print( tops );
  160. Array.getStatistics( tops, min, max, mean, stdDev );
  161. for( i = 0; i < tops.length; i ++ ) {
  162. Overlay.getBounds( s [ i ], x, y, w, h );
  163. Overlay.moveSelection( s [ i ], x, min );
  164. }
  165. Overlay.show( );
  166. } else if( cmd == "Bottom" ) {
  167. s = getSelectedElements( );
  168. s = split( s, ',' );
  169. if( s.length < 1 ) exit( );
  170. tops = newArray( s.length );
  171. for( i = 0; i < tops.length; i ++ ) {
  172. Overlay.getBounds( s [ i ], x, y, w, h );
  173. tops [ i ]= y + h;
  174. }
  175. Array.getStatistics( tops, min, max, mean, stdDev );
  176. for( i = 0; i < tops.length; i ++ ) {
  177. Overlay.getBounds( s [ i ], x, y, w, h );
  178. Overlay.moveSelection( s [ i ], x, max - h );
  179. }
  180. selectNone( );
  181. } else if( cmd == "Left" ) {
  182. s = getSelectedElements( );
  183. s = split( s, ',' );
  184. if( s.length < 1 ) exit( );
  185. tops = newArray( s.length );
  186. for( i = 0; i < tops.length; i ++ ) {
  187. Overlay.getBounds( s [ i ], x, y, w, h );
  188. tops [ i ]= x;
  189. }
  190. Array.getStatistics( tops, min, max, mean, stdDev );
  191. for( i = 0; i < tops.length; i ++ ) {
  192. Overlay.getBounds( s [ i ], x, y, w, h );
  193. Overlay.moveSelection( s [ i ], min, y );
  194. }
  195. selectNone( );
  196. } else if( cmd == "Right" ) {
  197. s = getSelectedElements( );
  198. s = split( s, ',' );
  199. if( s.length < 1 ) exit( );
  200. tops = newArray( s.length );
  201. for( i = 0; i < tops.length; i ++ ) {
  202. Overlay.getBounds( s [ i ], x, y, w, h );
  203. tops [ i ]= x + w;
  204. }
  205. Array.getStatistics( tops, min, max, mean, stdDev );
  206. for( i = 0; i < tops.length; i ++ ) {
  207. Overlay.getBounds( s [ i ], x, y, w, h );
  208. Overlay.moveSelection( s [ i ], max - w, y );
  209. }
  210. selectNone( );
  211. } else if( cmd == "Middle" ) {
  212. s = getSelectedElements( );
  213. s = split( s, ',' );
  214. if( s.length < 1 ) exit( );
  215. Overlay.getBounds( s [ 0 ], x, y, w, h );
  216. middle = y + h / 2;
  217. for( i = 0; i < s.length; i ++ ) {
  218. Overlay.getBounds( s [ i ], x, y, w, h );
  219. Overlay.moveSelection( s [ i ], x, middle - h / 2 );
  220. }
  221. selectNone( );
  222. } else if( cmd == "Center" ) {
  223. s = getSelectedElements( );
  224. s = split( s, ',' );
  225. if( s.length < 1 ) exit( );
  226. Overlay.getBounds( s [ 0 ], x, y, w, h );
  227. center = x + w / 2;
  228. for( i = 0; i < s.length; i ++ ) {
  229. Overlay.getBounds( s [ i ], x, y, w, h );
  230. Overlay.moveSelection( s [ i ], center - w / 2, y );
  231. }
  232. selectNone( );
  233. }
  234. highlightSelectedROIs( );
  235. }
  236. // Click to delete Overlay element
  237. // A confirm dialog is shown
  238. // you can choose not to show it.
  239. macro "Delete Overlay Tool - C037R00ddB58Cd00L0088L0880" {
  240. getCursorLoc( x, y, z, flags );
  241. selectNone( );
  242. call( 'ij.Prefs.set', 'overlaytoolset.selected', '' );
  243. id = Overlay.indexAt( x, y );
  244. if( id != - 1 ) {
  245. showWarning = call( "ij.Prefs.get", "overlaytoolset.deletewarning", true );
  246. mustDelete = true;
  247. if( showWarning == true ) {
  248. Dialog.create( "Delete ROI tool options" );
  249. Dialog.addCheckbox( "Delete this ROI", true );
  250. Dialog.addCheckbox( "Show this dialog", showWarning );
  251. Dialog.show( );
  252. mustDelete = Dialog.getCheckbox( );
  253. showWarning = Dialog.getCheckbox( );
  254. call( "ij.Prefs.set", "overlaytoolset.deletewarning", showWarning );
  255. }
  256. if( mustDelete ) Overlay.removeSelection( id );
  257. }
  258. }
  259. macro "Overlay Toolset Help Action Tool - C037T3f18?" {
  260. html = "<html>" +
  261. "Toolset Description<br>" +
  262. "<br><p>* Move overlay tool" +
  263. "<br>Click and drag an overlay element;" +
  264. "<br>It snaps to alignment guides to other elements.</p>" +
  265. "<br><p>* Overlay select tool" +
  266. "<br>Click an overlay element to select it." +
  267. "<br>Shift-click to add elements to the selection." +
  268. "<br>Shift-click a selected element to deselect it.</p>" +
  269. "<br><p>* Overlay align tool menu" +
  270. "<br>Use this menu to align selected elements</p>" +
  271. "<br><p>* Overlay delete tool" +
  272. "<br>Click to remove an overlay element</p>" +
  273. "<br><p>* Overlay toolset help action tool" +
  274. "<br>This dialog. You can leave it open to try toolset functions.</p>" +
  275. "</html>";
  276. Dialog.createNonBlocking( "Overlay Toolset Help" );
  277. Dialog.addMessage( html );
  278. Dialog.show( );
  279. }