Can receive pointerDragged but not pointerPressed in a container - events

I have a UI in Codenameone where a container contains another container which contains some widgets. On the bottom level container I'm able to receive pointerDragged events but not pointerPressed. These (pointerPressed) seem to be consumed by the widgets on the top of the hierarchy, but not move down to the bottom container.
How can I fix this?
I'd like to do this to detect left-right swipes on the bottom container. Is there perhaps a better way to do that?

Only focusable components receive the events directly in Codename One otherwise we would have to deliver events to multiple components which might inhibit performance.
The best way to do this is to use an existing component, e.g. Tabs supports swiping and hiding the tabs so you can just use that.
Making the Container focusable is probably not as desireable (although possible) since it might make interaction with the components within difficult.
You can use a pointer listener on the parent form as the Tabs component does internally. This should always work since the form gets all the events: https://github.com/codenameone/CodenameOne/

Related

drag and drop between different recyclerviews with animation

I'm creating a simple (or so I thought) app that allows a user to drag/drop/reorder items within one of two recyclerview lists, or from one list to another. It should animate as you are dragging items, opening gaps that help you easily drop the item into the target spot. This functionality needs to go both ways between the lists. And for extra fun, drag should start on touch, not long press.
In case it makes any difference I'm trying to stick to MVVM, and my 2 recyclerviews are in 2 different fragments, but combined in one screen for this activity.
I'm a relative newbie to android, and using kotlin.
What I have managed so far
I can achieve a nice animation within one recyclerview, using ItemTouchHelper.
OR
I can achieve drag and drop from one recyclerview to the other, and I can start that on touch rather than long press, using View.OnDragListener.
What I can't seem to do is get both of those working at once--drag to another view, but with those beautiful animations ItemTouchHelper provides as the drag is underway.
I have tested all kinds of combinations of attaching customized drag or touchlisteners to my viewholders or recyclerviews. The closest I've gotten to getting things working is attaching the draglistener to the recyclerviews only, in order to receive the dropped view. Then I have set draglisteners and touchhelpers on the viewholders (via the recyclerview adapter).
With this setup I can drag between or within recyclerviews on touch, and have managed to get an animation AFTER drop by calling onItemMove of my adapter (provided as a parameter in my custom draglistener) from within the ACTION_DROP of my dragListener.
I've been working on this for a VERY LONG TIME and googled the heck out of it without luck. Help deeply appreciated.

Programming custom GUI OPenGL

I am creating my own GUI in OpenTK.
I want to fire a mouse event when the cursor is, for example, in one of the GUI controls. How can I do that? Because now I'm just iterating through a list of items in the main class, and in the Opentk´s window´s MouseMove event I'm just checking if the mouse coordinates are within the "region" of the component I'm drawing.
This works for now, but I think it could be done in a better way. This way my code is unordered and in the main class, and I would rather have it in the specific component class.
What I would like is to have an event attached to each component of my GUI, so that I can define many events for one component.
I mean, I would like to have for example a button component where I can override or just use a method that fires when an event occurs. Same as OpenGL´s window where you can override events.
This is not a complete answer, because your question is quite broad, but I hope it helps.
In order to implement such a system, here are the core components for a potential design:
UI Components: Some kind of standard interface where different component types can define logic for interactions. Depending on the language, the most common approach is probably something like a parent class Component, with methods to be overridden. These would probably include things like:
Mouse Hover
Mouse Click (press / release)
Click drag
It will also likely need some additional associated information:
Some way to determine the component's location. Could be providing a bounding box, or perhaps a method that tests if a given point is within this component or not.
Information or functionality for drawing the component.
Display and layering settings (is it visible or hidden, should it draw on top of other components or behind).
UI Context: The context is a structure that defines the set of components that are existent in the UI. This could be something like a list structure of Components. In order to build your UI, you would add components to this context. The context will define some behaviour:
Managing components (add / remove / modify).
How to draw the entire context (for example, looping over each component and executing the draw functionality for each).
Handling of events (see next section)
Event Dispatch: To make your UI usable, you can insert an "adapter" layer that handles events from your windowing library (OpenTK) then translates them into usable events for your components and dispatches them. Here is an example of how this might work for a "click" event (pseudo-code):
function TK_Event_ClickPressed(point) {
for component in context {
if component.ContainsPoint(point) {
component.EventClickPressed()
}
}
}
This is actually the more tricky part of the design, in my opinion, because there are some tricky conventions around how component based UI works. You don't necessarily have to follow them, but they're important to be aware of at least because it is probably how people expect your UI to work:
After click press, click drag continues to occur until click release, even if the cursor leaves the component area.
"Actions" occur on click release.
Click release only takes action if the corresponding click press occurred on the same component.
The click release doesn't take any action if the cursor is no longer inside the component (leaving and re-entering the component before release still does the action, though).
You can only be actively clicking one component at a time (the one shown on top), even if multiple components overlap at that spot.
Assuming that you follow these conventions, this means that dispatching events is actually a bit more complicated than just checking if the event point was in a given component or not. You need to maintain some kind of state to keep track of whether the context is currently in a click or not, and which component, if any, is "consuming" the current click. That is, which component should be given the click release and drag events if they occur.
With these systems in place, you just need to create a window, create a UI context, register the adapter layer to the window to act on that context, set up the window to draw the context on frame, then use the context to add / remove / modify components in your program.

gwt canvas- handling events altogether differently

Gwt HTML5 Canvas wrapper can responds to mouse and keyboard events, it binds to 5 - 6 types of different events, my question is, it is possible to define entirely new event system such as CanvasEvent (and related handler CanvasEventHandler extends to GwtEvent etc), bind this to canvas object and then handle all events differently using a handler interface methods will be something like onDraw(), onDrag(), onMove(), onSelect() etc.
I dont have good clarity of GWT event system but i want to know is this possible without individuality attaching separate event handlers to form a logic for handling my problem, can I access all possible event as one consolidated object and fire custom event based on conditions. What would be the best way to do it, there are threads with GWT custom events but they include senders, whereas in my case sender is already present (Canvas)
Thanks much
Certainly - remember that all of the GwtEvent objects are completely artificial and are based off of events fired from the native JavaScript. That JavaScript event object, (which comes in via onBrowserEvent) gets wrapped up as a ClickEvent or a KeyDownEvent based on the details of the original object, and then fired off via the internal HandlerManager instance. In the same way, you could listen for a MouseDownEvent, set some state, then if a MouseMoveEvent occurs, fire your own CanvasDrag event. You'd probably stop listening to those move events once a MouseUpEvent happens, then you would issue something like a CanvasDropEvent. If isntead the MouseUpEvent occurred right away with no move, you could trigger a CanvasSelectEvent (or you might have something else in mind for that select event).
Each of these new event types you declare then might contain specifics about whatever is going on. For example, while a MouseMoveEvent just has the element that the mouse is over and the x/y coords, you might be indicating what is being dragged around the page. That might be in the form of the shape that was last clicked, or even the data it represents.
Yes, the 'sender', or source, is already there, but it'll be easier to use if you expose some kind of a method to add handlers like addCanvasDragHandler, etc. This is not required - all users of your code could just use addHandler, but it does make it a little ambiguous about whether or not the widget supports the event in question. You would then call fireEvent on the canvas object to make all handlers of that event type listen.
Or, you could make a new class that contains an internal Canvas widget - possibly a Composite object or just something that implements IsWidget (Canvas has a private constructor, so you can't subclass it). This would enable you to add your own particular handlers, and keep your own HandlerManager/EventBus to track the events you are concerned with.

"coupled" NSWindows

I am trying to have "coupled" NSWindows.
I mean 2 NSWindows of the same size, positioned side by side. If I move one, the other one has to move as well to keep both side by side.
I tried to do this with child window but when the child window is moved, the "mother" stays in place.
How I can get this behavior?
Designate an object as the windows' delegate and respond to the ‑windowDidMove:, ‑windowWillResize:toSize: and/or ‑windowWillMove: delegate methods, or register to receive the corresponding notifications. You can then resize the other window as you see fit.
This sounds like an odd UI, by the way. Can you not just use a split view?

GWT DnD: Is it possible to drag disabled widgets?

I have a composite widget that contains a disabled TextArea on an AbsotutePanel. Now I want to be able to drag the composite widget, starting from anywhere on it, including the disabled text area.
Is that possible?
If you want to drag a composite widget, you need to have a reference to the "drag handle" wich is one of the elements in the composite widget that implements HasAllMouseHandlers. The easiest (and in my mind, cleanest) way to do this is to have the composite widget extend the interface HasDragHandle wich requires the composite to have the method:
Widget getDragHandle(); the interface does not explicitly tell you so but the returned widget must be a widget implementing the HasAllMouseHandlers interface (or you'll get a runtime error).
(Non-composite widgets implementing HasAllMouseHandlers can be used directly)
I'd reccomend using a Label as a drag handle (it doesn't need to contain any text it could just be styled so the user understands it can be used for dragging), and not a form element because then you're overloading it's behaviour in a way the user most likely won't expect. I'm not really sure how a disabled element would work as a drag handle, quite possibly disabling an element will stop any mouse listners from working to so it won't work as a drag handle (haven't tried it though).
Or, try putting your widget into a FocusPanel, which is already enabled for drag 'n drop anyway. I've done this with a TextBox and a button, and it seems to work fine. Disabling the widget inside of the FocusPanel also keeps it from accidentally being activated.

Resources