Click here to Skip to main content
15,900,461 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: BinaryFormatter a malware nest ? so, it comes to this ? Pin
Super Lloyd19-Dec-21 7:15
Super Lloyd19-Dec-21 7:15 
GeneralRe: BinaryFormatter a malware nest ? so, it comes to this ? Pin
BillWoodruff19-Dec-21 6:39
professionalBillWoodruff19-Dec-21 6:39 
GeneralRe: BinaryFormatter a malware nest ? so, it comes to this ? Pin
Super Lloyd18-Dec-21 17:16
Super Lloyd18-Dec-21 17:16 
GeneralRe: BinaryFormatter a malware nest ? so, it comes to this ? Pin
harold aptroot19-Dec-21 1:02
harold aptroot19-Dec-21 1:02 
JokeRe: BinaryFormatter a malware nest ? so, it comes to this ? Pin
0x01AA19-Dec-21 2:02
mve0x01AA19-Dec-21 2:02 
GeneralRe: BinaryFormatter a malware nest ? so, it comes to this ? Pin
BillWoodruff19-Dec-21 6:31
professionalBillWoodruff19-Dec-21 6:31 
GeneralRe: BinaryFormatter a malware nest ? so, it comes to this ? Pin
Gerry Schmitz19-Dec-21 8:06
mveGerry Schmitz19-Dec-21 8:06 
GeneralThe magic of IAsyncEnumerable and IObservable in action (code show off, sorry) Pin
Super Lloyd18-Dec-21 12:18
Super Lloyd18-Dec-21 12:18 
I am sometimes awash with intellectual satisfaction how elegant is mouse handling thanks to those 2 guys!

So gonna show off my work in progress, doesn't do much but show off the structure, which is perfect, mouse down handling in my item tool!

What you can behold below is as follow...

When left click, count the click count, if any, then either start dragging (if still in mouse down) or add a text editor and wait for it to complete. All in one single async method. So simple to write. As simple as I just described, grooooovy! Big Grin | :-D Rose | [Rose]
C#
protected internal override async void OnMapMouseDown(MouseButtonEventArgs e)
{
    base.OnMapMouseDown(e);

    var target = Target;
    if (target == null)
        return;
    if (e.ChangedButton != MouseButton.Left)
        return;

    if (inMouseDown)
        return;
    inMouseDown = true;
    try
    {
        using var uScope = Map.Document?.UndoManager.BeginScope();
        var token = InputCancellationToken;

        var p = e.GetPosition(Layer);
        mouseDownLoc = target.Walls.Location(p.X, p.Y);

        var isMouseStillDown = true;
        await foreach (var (click, isIn) in GetClicksAsync(e, token))
        {
            isMouseStillDown = isIn;
            Trace.WriteLine($"Got One: ({click}, {isIn})");
        }
        if (token.IsCancellationRequested)
            return;

        if (isMouseStillDown)
        {
            await foreach (var move in MapMouseDragAsync(e, token))
            {

            }
        }
        else
        {
            var anchor = new AnchorPoint(0.5, 0);
            MapUtils.SetAutobounds(textEditor, () => p + anchor.OffsetFor(textEditor.TextBox, textEditor));
            await MapUtils.EditAsync(Map, textEditor, token);
        }
    }
    catch (TaskCanceledException) { }
    finally
    {
        inMouseDown = false;
    }
    Trace.WriteLine("OnMapMouseDown over");
}
bool inMouseDown;
DD.TileLocation mouseDownLoc;

But what is the black magic behind, say MapMouseDragAsync, you may ask?
Also very simple, once again, behold! Big Grin | :-D
C#
Subject<EventArgs> mMapLeave = new();
Subject<MouseButtonEventArgs> mMapMouseUp = new();
Subject<MouseEventArgs> mMapMouseMove = new();

public IObservable<EventArgs> MapLeave => mMapLeave;
public IObservable<MouseButtonEventArgs> MapMouseUp => mMapMouseUp;
public IObservable<MouseEventArgs> MapMouseMove => mMapMouseMove;

protected internal virtual void OnMapLeave()
{
    mMapLeave.OnNext(EventArgs.Empty);
    mInputCancellationToken?.Cancel();
    mInputCancellationToken = null;
}
protected internal virtual void OnMapMouseUp(MouseButtonEventArgs e) => mMapMouseUp.OnNext(e);
protected internal virtual void OnMapMouseMove(MouseEventArgs e) => mMapMouseMove.OnNext(e);

public async IAsyncEnumerable<MouseEventArgs> MapMouseDragAsync(MouseButtonEventArgs e, [EnumeratorCancellation] CancellationToken token)
{
    if (e.ChangedButton != MouseButton.Left || Map.IsMouseCaptured)
        yield break;

    var captured = Map.CaptureMouse();
    try
    {
        var events = this.MapMouseMove
            .TakeUntil(this.MapMouseUp.Where(z => z.ChangedButton == MouseButton.Left))
            .TakeUntil(this.MapLeave)
            .ToAsyncEnumerable(token);

        await foreach (var ev in events)
            yield return ev;
    }
    finally
    {
        if (captured)
            Map.ReleaseMouseCapture();
    }
}

One can create observable directly from event, but I prefer to explicitly create Subject, since it's the class firing the event here, and I want to make observable easier.
A new .NET Serializer
All in one Menu-Ribbon Bar
Taking over the world since 1371!


modified 18-Dec-21 19:56pm.

GeneralRe: The magic of IAsyncEnumerable and IObservable in action (code show off, sorry) Pin
honey the codewitch18-Dec-21 16:56
mvahoney the codewitch18-Dec-21 16:56 
GeneralRe: The magic of IAsyncEnumerable and IObservable in action (code show off, sorry) Pin
Super Lloyd18-Dec-21 17:02
Super Lloyd18-Dec-21 17:02 
GeneralRe: The magic of IAsyncEnumerable and IObservable in action (code show off, sorry) Pin
honey the codewitch18-Dec-21 17:05
mvahoney the codewitch18-Dec-21 17:05 
GeneralRe: The magic of IAsyncEnumerable and IObservable in action (code show off, sorry) Pin
Super Lloyd18-Dec-21 17:09
Super Lloyd18-Dec-21 17:09 
GeneralRe: The magic of IAsyncEnumerable and IObservable in action (code show off, sorry) Pin
honey the codewitch18-Dec-21 17:34
mvahoney the codewitch18-Dec-21 17:34 
GeneralRe: The magic of IAsyncEnumerable and IObservable in action (code show off, sorry) Pin
Super Lloyd18-Dec-21 17:58
Super Lloyd18-Dec-21 17:58 
GeneralRe: The magic of IAsyncEnumerable and IObservable in action (code show off, sorry) Pin
honey the codewitch18-Dec-21 18:03
mvahoney the codewitch18-Dec-21 18:03 
GeneralRe: The magic of IAsyncEnumerable and IObservable in action (code show off, sorry) Pin
Super Lloyd18-Dec-21 18:16
Super Lloyd18-Dec-21 18:16 
GeneralIt had to happen at some point Pin
honey the codewitch18-Dec-21 11:18
mvahoney the codewitch18-Dec-21 11:18 
GeneralRe: It had to happen at some point Pin
Super Lloyd18-Dec-21 11:57
Super Lloyd18-Dec-21 11:57 
GeneralRe: It had to happen at some point Pin
honey the codewitch18-Dec-21 11:58
mvahoney the codewitch18-Dec-21 11:58 
GeneralRe: It had to happen at some point PinPopular
PIEBALDconsult18-Dec-21 12:12
mvePIEBALDconsult18-Dec-21 12:12 
GeneralRe: It had to happen at some point Pin
Super Lloyd18-Dec-21 12:21
Super Lloyd18-Dec-21 12:21 
GeneralRe: It had to happen at some point Pin
enhzflep19-Dec-21 10:19
enhzflep19-Dec-21 10:19 
GeneralRe: It had to happen at some point Pin
Randor 18-Dec-21 22:44
professional Randor 18-Dec-21 22:44 
GeneralRe: It had to happen at some point Pin
honey the codewitch19-Dec-21 0:16
mvahoney the codewitch19-Dec-21 0:16 
GeneralRe: It had to happen at some point Pin
Randor 19-Dec-21 0:48
professional Randor 19-Dec-21 0:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Straw Poll

Were you affected by the geomagnetic storms this past weekend?
Communication disruptions, electrified pipes, random unexplained blue-screens in Windows - the list of effects is terrifying.
  Results   488 votes