Skip to content

Commit

Permalink
[Virtual Desktop] Dragging fix
Browse files Browse the repository at this point in the history
    - Fixes issue where mascot gets stuck on drag
    - Workaround for parent frame consuming events (presumably due to panel movement)
  • Loading branch information
LavenderSnek committed Nov 4, 2023
1 parent 1bbcca3 commit 556cec1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.*;

public class VirtualWindowPanel extends JPanel implements TranslucentWindow {

protected static final Color CLEAR = new Color(0, 0, 0, 0);

private TranslucentWindowEventHandler eventHandler = TranslucentWindowEventHandler.DEFAULT;

private VirtualImage image;

private final MouseAdapter parentListener = new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (dragging) {
super.mouseReleased(e);
var p = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), VirtualWindowPanel.this);
endDrag(new TranslucentWindowEvent(p));
}
}
};

private boolean dragging = false;

public VirtualWindowPanel() {
super();
setBounds(0,0,0,0);
Expand All @@ -31,19 +41,37 @@ public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
showPopupMenu(e);
} else if (SwingUtilities.isLeftMouseButton(e)) {
getEventHandler().onDragBegin(new TranslucentWindowEvent(e.getPoint()));
beginDrag(new TranslucentWindowEvent(e.getPoint()));
}
}

@Override
public void mouseReleased(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
getEventHandler().onDragEnd(new TranslucentWindowEvent(e.getPoint()));
endDrag(new TranslucentWindowEvent(e.getPoint()));
}
}
});
}

private void beginDrag(TranslucentWindowEvent e) {
if (dragging) {return;}
getEventHandler().onDragBegin(e);
dragging = true;
}

private void endDrag(TranslucentWindowEvent e) {
if (!dragging) {return;}
getEventHandler().onDragEnd(e);
dragging = false;
}

@Override
public void addNotify() {
super.addNotify();
getParent().addMouseListener(parentListener);
}

protected void showPopupMenu(MouseEvent e) {
TopLevelMenuRep menuRep = getEventHandler().getContextMenuRep();
if (menuRep == null) {
Expand All @@ -55,6 +83,7 @@ protected void showPopupMenu(MouseEvent e) {

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image.bufferedImage(), 0, 0, null);
}

Expand Down Expand Up @@ -86,6 +115,7 @@ public void dispose() {
if (parent != null) {
parent.remove(this);
parent.repaint();
parent.removeMouseListener(parentListener);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import com.group_finity.mascotnative.virtualdesktop.VirtualWindowPanel;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.*;
import java.awt.*;
import java.util.List;

Expand All @@ -16,7 +15,7 @@ public class VirtualEnvironmentDisplay {
private final JFrame frame;

public VirtualEnvironmentDisplay() {
frame = new JFrame("Shimeji");
frame = new JFrame("ShimejiEE");
frame.getContentPane().setPreferredSize(new Dimension(INIT_WIDTH, INIT_HEIGHT));
frame.getContentPane().setLayout(null);
frame.getContentPane().setBackground(BG_COLOR);
Expand All @@ -40,6 +39,7 @@ public Point getCursorLocation(Point screenCoordinates) {
public void addShimejiWindow(VirtualWindowPanel panel) {
SwingUtilities.invokeLater(() -> {
frame.getContentPane().add(panel);
frame.validate();
});
}

Expand Down

0 comments on commit 556cec1

Please sign in to comment.