Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

132
LINES

< > BotCompany Repo | #1035241 // ComponentResizeDragger - resize a GUI Component with mouse

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (8265L) is out of date.

1  
// This class uses the border of the component that caught
2  
// the mouse event to determine the drag direction.
3  
4  
sclass ComponentResizeDragger extends MouseAdapter {
5  
  Component component; // component to resize
6  
  int cornerSize = 10; // how big do we assume the corners are
7  
  settable bool debug;
8  
9  
  MouseEvent mouseDownEvent; // if != null, we're engaged
10  
  JComponent src;
11  
  Rect originalPosition, srcBounds;
12  
  int dragX, dragY; // -1, 0 or 1 depending on what we are dragging
13  
  Cursor originalCursor, ourCursor;
14  
  
15  
  *(Component *component, JComponent *src) {
16  
    addMouseAndMotionListener(src, this);
17  
  }
18  
  
19  
  bool engaged() { ret mouseDownEvent != null; } 
20  
  
21  
  void start(MouseEvent e) {
22  
    this.mouseDownEvent = e;
23  
    originalPosition = toRect(getBounds(component));
24  
    cursorCalc(e, true);
25  
  }
26  
  
27  
  void cursorCalc(MouseEvent e, bool inside) {
28  
    srcBounds = toRect(boundsOnScreen(src));
29  
    Pt mouse = pt(e.getXOnScreen(), e.getYOnScreen());
30  
    
31  
    if (distance(mouse.y, srcBounds.y2()) <= cornerSize)
32  
      dragY = 1;
33  
    else if (distance(mouse.y, srcBounds.y1()) <= cornerSize)
34  
      dragY = -1;
35  
    else
36  
      dragY = 0;
37  
    
38  
    if (distance(mouse.x, srcBounds.x2()) <= cornerSize)
39  
      dragX = 1;
40  
    else if (distance(mouse.x, srcBounds.x1()) <= cornerSize)
41  
      dragX = -1;
42  
    else
43  
      dragX = 0;
44  
      
45  
    originalCursor = src.getCursor();
46  
    int iCursor = dragY*3+dragX+4;
47  
    int cursor = new int[] {
48  
      Cursor.NW_RESIZE_CURSOR,
49  
      Cursor.N_RESIZE_CURSOR,
50  
      Cursor.NE_RESIZE_CURSOR,
51  
      Cursor.W_RESIZE_CURSOR,
52  
      -1,
53  
      Cursor.E_RESIZE_CURSOR,
54  
      Cursor.SW_RESIZE_CURSOR,
55  
      Cursor.S_RESIZE_CURSOR,
56  
      Cursor.SE_RESIZE_CURSOR,
57  
    }[iCursor];
58  
    Cursor newCursor = cursor >= 0 && inside ? Cursor.getPredefinedCursor(cursor) : null;
59  
    //if (debug) printVars(iCursor, +ourCursor, +newCursor);
60  
    if (newCursor != ourCursor) {
61  
      ourCursor = newCursor;
62  
      src.setCursor(newCursor);
63  
    }
64  
      
65  
    /*if (debug)
66  
      printVars("WindowResizeDragger", +dragX, +dragY, +mouse, +srcBounds, +cornerSize);*/
67  
  }
68  
  
69  
  @Override public void mouseEntered(MouseEvent e) {
70  
    if (debug) print("mouseEntered");
71  
    if (!engaged())
72  
      cursorCalc(e, true);
73  
  }
74  
75  
  @Override public void mouseExited(MouseEvent e) {
76  
    if (debug) print("mouseExited");
77  
    if (!engaged())
78  
      cursorCalc(e, false);
79  
  }
80  
81  
  public void mousePressed(MouseEvent e) {
82  
    if (e.getButton() != MouseEvent.BUTTON1) ret;
83  
    //if (component cast JFrame && isMaximized(component)) ret;
84  
    if (engaged()) ret; // what?
85  
    if (!mouseEventIsInBorder(e)) ret;
86  
    start(e);
87  
  }
88  
  
89  
  public void mouseDragged(MouseEvent e) {
90  
    if (!engaged()) ret;
91  
    updatePosition(e);
92  
  }
93  
  
94  
  public void mouseMoved(MouseEvent e) {
95  
    cursorCalc(e, true);
96  
  }
97  
  
98  
  public void mouseReleased(MouseEvent e) {
99  
    if (mouseDownEvent == null) ret;
100  
    if (e.getButton() != mouseDownEvent.getButton()) ret;
101  
    updatePosition(e);
102  
    if (debug) print("drag done");
103  
    if (src.getCursor() == ourCursor)
104  
      src.setCursor(originalCursor);
105  
    mouseDownEvent = null;
106  
    dragDone();
107  
  }
108  
  
109  
  void updatePosition(MouseEvent e) {
110  
    //if (debug) printVars("updatePosition", +e);
111  
    Rect w = toRect(component.getBounds());
112  
    Pt a = topLeftCorner(w), b = bottomRightCorner(w);
113  
    Pt mouse = pt(e.getXOnScreen(), e.getYOnScreen());
114  
    Pt minSize = toPt(component.getMinimumSize());
115  
    
116  
    if (dragX < 0)
117  
      a.x = max(0, min(b.x-minSize.x, mouse.x + originalPosition.x - mouseDownEvent.getXOnScreen()));
118  
    else if (dragX > 0)
119  
      b.x = max(a.x+minSize.x, mouse.x + originalPosition.x2() - mouseDownEvent.getXOnScreen());
120  
      
121  
    if (dragY < 0)
122  
      a.y = max(0, min(b.y-minSize.y, mouse.y + originalPosition.y - mouseDownEvent.getYOnScreen()));
123  
    else if (dragY > 0)
124  
      b.y = max(a.y+minSize.y, mouse.y + originalPosition.y2() - mouseDownEvent.getYOnScreen());
125  
      
126  
    Rect r = rectFromPoints(a, b);
127  
    if (debug) printVars("ComponentResizeDragger.updatePosition", +w, +mouse, +a, +b, +dragX, +dragY);
128  
    component.setBounds(toRectangle(r));
129  
  }
130  
  
131  
  void dragDone {}
132  
}

Author comment

Began life as a copy of #1033471

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035241
Snippet name: ComponentResizeDragger - resize a GUI Component with mouse
Eternal ID of this version: #1035241/10
Text MD5: 417262331dfa0257a67776fc90e62a62
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-30 16:21:26
Source code size: 4238 bytes / 132 lines
Pitched / IR pitched: No / No
Views / Downloads: 81 / 140
Version history: 9 change(s)
Referenced in: [show references]