Open SWayfarer opened 4 years ago
Hi! eventMouseClick is for release mouse buttons.
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?
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?
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.
Here some code to understand =) https://pastebin.com/N9YbNQHe
Yeah, wait a minute! I am writing an example for you))
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);
}
}
Updated: I replaced BlankItem (Prototype) with Rectangle (Primitive) so that _handler doesn't get focus.
Hi! It works! Thanks! Do I understand correctly that these two empty interfaces do all the magic?
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
Hi! In Prototype class we have field eventMousePress, but how i can handle mouse release event?