Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Copy events in a new render window  (Read 5104 times)

0 Members and 1 Guest are viewing this topic.

aceman1209

  • Newbie
  • *
  • Posts: 4
    • View Profile
Copy events in a new render window
« on: March 14, 2018, 07:19:04 pm »
Hi all. SFML.net does not have a Create () method for window. And to apply the changes it needs to be closed and re-created. But events will be reset if i recreate window.

How can I copy all the events in a new rendering window?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Copy events in a new render window
« Reply #1 on: March 14, 2018, 08:16:22 pm »
What are you trying to achieve?
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Copy events in a new render window
« Reply #2 on: March 14, 2018, 08:19:00 pm »
So what's the issue really, because when you say you want to copy events, you're clearly doing something wrong. ;)

What's your code?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

aceman1209

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Copy events in a new render window
« Reply #3 on: March 14, 2018, 09:44:42 pm »
This is my own GameWindow class
namespace Amari.Display
{
    public class GameWindow
    {
        internal RenderWindow renderWindow;

        public Viewport viewport;
        public bool IsFullscreen { get; private set; }

        public event EventHandler<SizeEventArgs> Resized;

        public bool IsOpen
        {
            get
            {
                return renderWindow.IsOpen;
            }
        }

        public GameWindow(bool IsFullScreen)
        {
            renderWindow = new RenderWindow(new VideoMode(800, 600), "", IsFullscreen ? Styles.Fullscreen : Styles.Default);
        }

        public void UpdateVideoMode(VideoMode videoMode, bool IsFullscreen)
        {
            renderWindow = new RenderWindow(videoMode, "", IsFullscreen ? Styles.Fullscreen : Styles.Default);
        }

        public void DispatchEvents() => renderWindow.DispatchEvents();
        public void Display() => renderWindow.Display();
    }
 

When i use UpdateVideoMode, the window is recreated and apply new videomode but all events will be reseted.

I add a simple code in GameWindow class and change constructor

        public GameWindow(bool IsFullScreen)
        {
            renderWindow = new RenderWindow(new VideoMode(800, 600), "", IsFullscreen ? Styles.Fullscreen : Styles.Default);
            renderWindow.Resized += RenderWindow_Resized;
        }

        private void RenderWindow_Resized(object sender, SizeEventArgs e)
        {
            Console.WriteLine($"Window resized");
        }
 

When i resize a window i get a message in console "Window resized"

Okay, its right.

But i add a two new lines in costructor
    renderWindow.Close();
    VideoModeUpdate(new VideoMode(1280, 720), false);
 

For example i set a new videomode in constructor.

Window initialized and works, but event doesnt.

I attach a screenshots.

Why i need to copy events? Because in other classes i use a renderWindow events and i dont want to update them after every window change. For example, my own mouse class part.

         private void GameWindow_MouseButtonPressed(object sender, MouseButtonEventArgs e)
        {
            is_Button_Pressed[(int)e.Button] = true;
            is_Button_Released[(int)e.Button] = false;
        }

        private void GameWindow_MouseButtonReleased(object sender, MouseButtonEventArgs e)
        {
            is_Button_Pressed[(int)e.Button] = false;
            is_Button_Released[(int)e.Button] = true;    
        }
 

If i recreate window i need to update this events, but object already created and in use. I need write a method for events link to a new window. But it's not convenient.

Sorry for my english, i'm not very strong in it.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Copy events in a new render window
« Reply #4 on: March 15, 2018, 08:23:24 am »
Oh, I see. "Events" in .Net have a different meaning than in C++. So what you want to keep are the installed event handlers, which makes more sense indeed.

I admit that I've never thought about this issue, and I have no solution for you right now.
Laurent Gomila - SFML developer

aceman1209

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Copy events in a new render window
« Reply #5 on: March 15, 2018, 05:26:07 pm »
I was responded to a ru.stackoverflow.com. I think this is a working solution.

Add my own event with Render Window event args.
    public event EventHandler<SizeEventArgs> Resized;
 

Then add a listener for Render Window resized event. I wrote a method for this.

         private void LinkEvents()
        {
            renderWindow.Resized += RenderWindow_Resized;
        }
 

When I recreate window I call LinkEvents() method after window initializing.

Listener method is very simple.
     private void RenderWindow_Resized(object sender, SizeEventArgs e) => Resized?.Invoke(sender, e);
 

When render window event works i call a my event Invoke with arguments. And it works.