spacevil icon indicating copy to clipboard operation
spacevil copied to clipboard

How to track mouse release?

Open SWayfarer opened this issue 5 years ago • 10 comments

Hi! In Prototype class we have field eventMousePress, but how i can handle mouse release event?

SWayfarer avatar Apr 06 '20 18:04 SWayfarer

Hi! eventMouseClick is for release mouse buttons.

spvessel avatar Apr 07 '20 16:04 spvessel

Hi! A mouse click is pressing and releasing a button on a widget without exiting it. If I exit the mouse cursor with the button pressed from the widget, and then release it, the click does not occur. How then to handle the release of the mouse button?

SWayfarer avatar Apr 07 '20 23:04 SWayfarer

Do you need to do something with the widget that you are leaving, or do something with another widget (which is on hover) when the mouse button is released?

spvessel avatar Apr 08 '20 00:04 spvessel

I am writing a slider that, by clicking on the moved part, remembers that it is pressed and depending on the movement of the mouse, it moves itself. I need to track the mouse release event to stop moving slider.

SWayfarer avatar Apr 08 '20 00:04 SWayfarer

Here some code to understand =) https://pastebin.com/N9YbNQHe

SWayfarer avatar Apr 08 '20 00:04 SWayfarer

Yeah, wait a minute! I am writing an example for you))

spvessel avatar Apr 08 '20 00:04 spvessel

OK. Something like that. You need to implement InterfaceDraggable and InterfaceHLayout (for horizontal slider).

import java.awt.Color;
import com.spvessel.spacevil.*;
import com.spvessel.spacevil.Core.*;
import com.spvessel.spacevil.Decorations.Style;
import com.spvessel.spacevil.Flags.*;

// InterfaceDraggable to recieve mouse drag events
// InterfaceHLayout to disable auto position by X axis.
public class MyWidget extends Prototype implements InterfaceDraggable, InterfaceHLayout {
    private Rectangle _handler = null;

    public MyWidget() {
        // appearance of MyWidget
        setStyle(Style.getDefaultCommonStyle());
        setWidthPolicy(SizePolicy.EXPAND);
        setSize(100, 30);
        setBackground(121, 223, 152);
        // appearance of handler
        _handler = new Rectangle();
        _handler.setStyle(Style.getDefaultCommonStyle());
        _handler.setHeightPolicy(SizePolicy.EXPAND);
        _handler.setBackground(Color.BLACK);
        _handler.setWidth(20);
    }

    @Override
    public void initElements() {
        // add handler
        addItem(_handler);
        //
        eventMouseClick.add(this::onRelease);
        eventMouseDrag.add(this::onDrag);
        eventMousePress.add(this::onPress);
        
        updateLayout();
    }

    // all overrides are necessary
    @Override
    public void setWidth(int width) {
        super.setWidth(width);
        updateLayout();
    }

    // InterfaceHLayout implementation
    @Override
    public void updateLayout() {

        _handler.setX(valueToXPos(_percent));
    }

    float _percent = 0; // percent

    private void onPress(InterfaceItem sender, MouseArgs args) {
        System.out.println("Press");
        _percent = 100.0f / (float) getWidth() * getActualXPos(args.position);
        updateLayout();
    }

    private void onDrag(InterfaceItem sender, MouseArgs args) {
        System.out.println("Drag");
        _percent = 100.0f / (float) getWidth() * getActualXPos(args.position);
        updateLayout();
    }

    private void onRelease(InterfaceItem sender, MouseArgs args) {
        System.out.println("Release");
        _percent = 100.0f / (float) getWidth() * getActualXPos(args.position);
        updateLayout();
    }

    // limit our handler by width and position of MyWidget
    private int getActualXPos(Position pos) {
        int actualX = pos.getX() - _handler.getWidth() / 2;
        if (actualX < getX())
            actualX = getX();
        if (actualX > getX() + getWidth() - _handler.getWidth())
            actualX = getX() + getWidth() - _handler.getWidth();
        return actualX;
    }

    private int valueToXPos(float value) {
        return (int) ((float) getWidth() / 100.f * value);
    }
}

spvessel avatar Apr 08 '20 01:04 spvessel

Updated: I replaced BlankItem (Prototype) with Rectangle (Primitive) so that _handler doesn't get focus.

spvessel avatar Apr 08 '20 09:04 spvessel

Hi! It works! Thanks! Do I understand correctly that these two empty interfaces do all the magic?

SWayfarer avatar Apr 08 '20 16:04 SWayfarer

You're right. InterfaceDraggable is completely empty and just defines the draggable element within the framework. InterfaceHLayout disables the basic horizontal (basic vertical layout is still working) layout and allows you to control the position of elements in the container. But you need to implement the "updateLayout ()" method and override "setWidth ()" and "setX ()". Something like that. I tried to write about it in article: https://www.codeproject.com/Articles/5257813/SpaceVIL-Framework-Cross-Platform-GUI-with-NET-JVM

In this article, you may encounter interfaces called IDraggable due to the .NET version of SpaceVIL. Just remember rule: IDraggable (.NET) <=> InterfaceDraggable (JVM), IHLayout (.NET) <=> InterfaceHLayout

spvessel avatar Apr 08 '20 17:04 spvessel