Skip to content

Package: DragPanningController$2

DragPanningController$2

nameinstructionbranchcomplexitylinemethod
mouseDragged(MouseEvent)
M: 53 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * Mistral: open source imaging engine
5: * http://tidalwave.it/projects/mistral
6: *
7: * Copyright (C) 2003 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/mistral-src
23: * git clone https://github.com/tidalwave-it/mistral-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.image.render;
28:
29: import java.awt.Point;
30: import java.awt.event.MouseAdapter;
31: import java.awt.event.MouseEvent;
32: import java.awt.event.MouseListener;
33: import java.awt.event.MouseMotionAdapter;
34: import java.awt.event.MouseMotionListener;
35:
36: /***********************************************************************************************************************
37: *
38: * This class activated panning-by-dragging, that is the capability of moving the
39: * photo in the viewport by dragging with the mouse.
40: *
41: * @author Fabrizio Giudici
42: *
43: **********************************************************************************************************************/
44: public class DragPanningController
45: {
46: /**
47: * The attached renderer.
48: */
49: private final EditableImageRenderer imageRenderer;
50:
51: /**
52: * The enablement state.
53: */
54: private boolean enabled;
55:
56: /**
57: * The previous mouse position is used to compute the delta motion.
58: */
59: private Point previousMousePosition;
60:
61: /*******************************************************************************************************************
62: *
63: *
64: ******************************************************************************************************************/
65: private final MouseListener mouseListener = new MouseAdapter()
66: {
67: @Override
68: public void mousePressed (final MouseEvent event)
69: {
70: previousMousePosition = event.getPoint();
71: }
72:
73: @Override
74: public void mouseReleased (final MouseEvent event)
75: {
76: previousMousePosition = null;
77: }
78: };
79:
80: /*******************************************************************************************************************
81: *
82: *
83: ******************************************************************************************************************/
84: private final MouseMotionListener dragListener = new MouseMotionAdapter()
85: {
86: @Override
87: public void mouseDragged (final MouseEvent event)
88: {
89: final var newMousePosition = event.getPoint();
90:
91:• if (previousMousePosition != null)
92: {
93: final var deltaX = (int)(newMousePosition.getX() - previousMousePosition.getX());
94: final var deltaY = (int)(newMousePosition.getY() - previousMousePosition.getY());
95: final var scale = imageRenderer.getScale();
96: imageRenderer.moveOrigin(-(int)Math.round(deltaX / scale), -(int)Math.round(deltaY / scale));
97: }
98:
99: previousMousePosition = newMousePosition;
100: }
101: };
102:
103: /*******************************************************************************************************************
104: *
105: * Creates a new instance of this class, attached to the given renderer.
106: * This controller must be activated with <code>setEnabled(true)</code> in
107: * order to be used.
108: *
109: * @param imageRenderer the image renderer
110: *
111: ******************************************************************************************************************/
112: public DragPanningController (final EditableImageRenderer imageRenderer)
113: {
114: if (imageRenderer == null)
115: {
116: throw new IllegalArgumentException("imageRenderer is mandatory");
117: }
118:
119: this.imageRenderer = imageRenderer;
120: }
121:
122: /*******************************************************************************************************************
123: *
124: * Enables or disables this controller. As this class attaches some
125: * listeners to the image renderer component, it's advisable to disable it
126: * when it's not needed, in order to facilitate garbage collection.
127: *
128: * @param enabled true if must be enabled, false otherwise
129: *
130: ******************************************************************************************************************/
131: public void setEnabled (final boolean enabled)
132: {
133: if (this.enabled != enabled)
134: {
135: this.enabled = enabled;
136:
137: if (enabled)
138: {
139: imageRenderer.addMouseListener(mouseListener);
140: imageRenderer.addMouseMotionListener(dragListener);
141: }
142:
143: else
144: {
145: imageRenderer.removeMouseListener(mouseListener);
146: imageRenderer.removeMouseMotionListener(dragListener);
147: }
148: }
149: }
150:
151: /*******************************************************************************************************************
152: *
153: * Returns true if the controller is enabled.
154: *
155: * @return true if enabled
156: *
157: ******************************************************************************************************************/
158: public boolean isEnabled()
159: {
160: return enabled;
161: }
162:
163: /*******************************************************************************************************************
164: *
165: * Centers the image on the screen, keeping the current scale.
166: *
167: ******************************************************************************************************************/
168: public void centerImage()
169: {
170: imageRenderer.centerImage();
171: }
172: }