Giter Club home page Giter Club logo

Comments (10)

spvessel avatar spvessel commented on August 15, 2024

Hi!
eventMouseClick is for release mouse buttons.

from spacevil.

SWayfarer avatar SWayfarer commented on August 15, 2024

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?

from spacevil.

spvessel avatar spvessel commented on August 15, 2024

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?

from spacevil.

SWayfarer avatar SWayfarer commented on August 15, 2024

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.

from spacevil.

SWayfarer avatar SWayfarer commented on August 15, 2024

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

from spacevil.

spvessel avatar spvessel commented on August 15, 2024

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

from spacevil.

spvessel avatar spvessel commented on August 15, 2024

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);
    }
}

from spacevil.

spvessel avatar spvessel commented on August 15, 2024

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

from spacevil.

SWayfarer avatar SWayfarer commented on August 15, 2024

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

from spacevil.

spvessel avatar spvessel commented on August 15, 2024

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

from spacevil.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.