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.

// This class uses the border of the component that caught
// the mouse event to determine the drag direction.

sclass ComponentResizeDragger extends MouseAdapter {
  Component component; // component to resize
  int cornerSize = 10; // how big do we assume the corners are
  settable bool debug;

  MouseEvent mouseDownEvent; // if != null, we're engaged
  JComponent src;
  Rect originalPosition, srcBounds;
  int dragX, dragY; // -1, 0 or 1 depending on what we are dragging
  Cursor originalCursor, ourCursor;
  
  *(Component *component, JComponent *src) {
    addMouseAndMotionListener(src, this);
  }
  
  bool engaged() { ret mouseDownEvent != null; } 
  
  void start(MouseEvent e) {
    this.mouseDownEvent = e;
    originalPosition = toRect(getBounds(component));
    cursorCalc(e, true);
  }
  
  void cursorCalc(MouseEvent e, bool inside) {
    srcBounds = toRect(boundsOnScreen(src));
    Pt mouse = pt(e.getXOnScreen(), e.getYOnScreen());
    
    if (distance(mouse.y, srcBounds.y2()) <= cornerSize)
      dragY = 1;
    else if (distance(mouse.y, srcBounds.y1()) <= cornerSize)
      dragY = -1;
    else
      dragY = 0;
    
    if (distance(mouse.x, srcBounds.x2()) <= cornerSize)
      dragX = 1;
    else if (distance(mouse.x, srcBounds.x1()) <= cornerSize)
      dragX = -1;
    else
      dragX = 0;
      
    originalCursor = src.getCursor();
    int iCursor = dragY*3+dragX+4;
    int cursor = new int[] {
      Cursor.NW_RESIZE_CURSOR,
      Cursor.N_RESIZE_CURSOR,
      Cursor.NE_RESIZE_CURSOR,
      Cursor.W_RESIZE_CURSOR,
      -1,
      Cursor.E_RESIZE_CURSOR,
      Cursor.SW_RESIZE_CURSOR,
      Cursor.S_RESIZE_CURSOR,
      Cursor.SE_RESIZE_CURSOR,
    }[iCursor];
    Cursor newCursor = cursor >= 0 && inside ? Cursor.getPredefinedCursor(cursor) : null;
    //if (debug) printVars(iCursor, +ourCursor, +newCursor);
    if (newCursor != ourCursor) {
      ourCursor = newCursor;
      src.setCursor(newCursor);
    }
      
    /*if (debug)
      printVars("WindowResizeDragger", +dragX, +dragY, +mouse, +srcBounds, +cornerSize);*/
  }
  
  @Override public void mouseEntered(MouseEvent e) {
    if (debug) print("mouseEntered");
    if (!engaged())
      cursorCalc(e, true);
  }

  @Override public void mouseExited(MouseEvent e) {
    if (debug) print("mouseExited");
    if (!engaged())
      cursorCalc(e, false);
  }

  public void mousePressed(MouseEvent e) {
    if (e.getButton() != MouseEvent.BUTTON1) ret;
    //if (component cast JFrame && isMaximized(component)) ret;
    if (engaged()) ret; // what?
    if (!mouseEventIsInBorder(e)) ret;
    start(e);
  }
  
  public void mouseDragged(MouseEvent e) {
    if (!engaged()) ret;
    updatePosition(e);
  }
  
  public void mouseMoved(MouseEvent e) {
    cursorCalc(e, true);
  }
  
  public void mouseReleased(MouseEvent e) {
    if (mouseDownEvent == null) ret;
    if (e.getButton() != mouseDownEvent.getButton()) ret;
    updatePosition(e);
    if (debug) print("drag done");
    if (src.getCursor() == ourCursor)
      src.setCursor(originalCursor);
    mouseDownEvent = null;
    dragDone();
  }
  
  void updatePosition(MouseEvent e) {
    //if (debug) printVars("updatePosition", +e);
    Rect w = toRect(component.getBounds());
    Pt a = topLeftCorner(w), b = bottomRightCorner(w);
    Pt mouse = pt(e.getXOnScreen(), e.getYOnScreen());
    Pt minSize = toPt(component.getMinimumSize());
    
    if (dragX < 0)
      a.x = max(0, min(b.x-minSize.x, mouse.x + originalPosition.x - mouseDownEvent.getXOnScreen()));
    else if (dragX > 0)
      b.x = max(a.x+minSize.x, mouse.x + originalPosition.x2() - mouseDownEvent.getXOnScreen());
      
    if (dragY < 0)
      a.y = max(0, min(b.y-minSize.y, mouse.y + originalPosition.y - mouseDownEvent.getYOnScreen()));
    else if (dragY > 0)
      b.y = max(a.y+minSize.y, mouse.y + originalPosition.y2() - mouseDownEvent.getYOnScreen());
      
    Rect r = rectFromPoints(a, b);
    if (debug) printVars("ComponentResizeDragger.updatePosition", +w, +mouse, +a, +b, +dragX, +dragY);
    component.setBounds(toRectangle(r));
  }
  
  void dragDone {}
}

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: 151 / 235
Version history: 9 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)