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

Author Topic: handling different mouse areas (one over another)  (Read 2220 times)

0 Members and 1 Guest are viewing this topic.

djm_jm

  • Newbie
  • *
  • Posts: 23
    • View Profile
handling different mouse areas (one over another)
« on: November 07, 2016, 06:59:09 pm »
I need one tip. Assume that my game has some objects and each is one menu having methods with mouse colision and actions about it. My problem is that the "areas" are one over another and it causes many bugs in the game.

There are any pratical method for solve this?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: handling different mouse areas (one over another)
« Reply #1 on: November 07, 2016, 07:23:52 pm »
Use some sort of event dispatcher that also has access to the "layer order". So for example you pass events from SFML to the dispatcher, which then checks what object might be affected by the event, then checks which is the top element and dispatches the event to the found object.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

djm_jm

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: handling different mouse areas (one over another)
« Reply #2 on: November 07, 2016, 07:32:56 pm »
Use some sort of event dispatcher that also has access to the "layer order". So for example you pass events from SFML to the dispatcher, which then checks what object might be affected by the event, then checks which is the top element and dispatches the event to the found object.

Chains of "ifs and elses", it's that?

if answer is yes, will be the same ssolution that I think, but it's one bit of work and with the project's development will be hard implement.

This "sort of event" will be one interface between the objects's methods, so I implement it inside the methods/classes as a parameter choosing which acess firstly? right?
« Last Edit: November 07, 2016, 07:39:22 pm by djm_jm »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: handling different mouse areas (one over another)
« Reply #3 on: November 07, 2016, 09:16:15 pm »
Chains of "ifs and elses", it's that?
If that's your idea of programming, then maybe? ;D

There are many ways to implement such a dispatcher. One for example would be with the will known subscribe-publisher pattern or a full on message bus or something even simpler. Of course it will take some time to redesign your current implementation, but well that's part of programming. Sometimes you need to completely rewrite stuff to make it work with a different design.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

felaugmar

  • Guest
Re: handling different mouse areas (one over another)
« Reply #4 on: November 08, 2016, 03:30:40 am »
I do this for my Controls,

std::unordered_set<std::string> m_controlsWithMouseOver;

std::unordered_set<std::string> m_controlsOnMouseEnter;
std::unordered_set<std::string> m_controlsOnMouseOver;
std::unordered_set<std::string> m_controlsOnMouseExit;
std::unordered_map<std::string, std::shared_ptr<Control>> m_controls;

The m_controls variable hold all my controls;
m_controlsOnMouseEnter, m_controlsOnMouseOver, m_controlsOnMouseExit hold the controls that I'm checking for mouseEvents;
And m_controlsWithMouseOver are the controls with 'Over'.

In each 'mouseMove sf::Event', I check based on those variables which function I should call, then I just call it.
control->mouseEnter();
control->mouseOver();
control->mouseExit();

And yeah, there's some ifs and elses, but not so much, just the needed.

djm_jm

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: handling different mouse areas (one over another)
« Reply #5 on: November 08, 2016, 07:45:14 pm »
I do this for my Controls,

std::unordered_set<std::string> m_controlsWithMouseOver;

std::unordered_set<std::string> m_controlsOnMouseEnter;
std::unordered_set<std::string> m_controlsOnMouseOver;
std::unordered_set<std::string> m_controlsOnMouseExit;
std::unordered_map<std::string, std::shared_ptr<Control>> m_controls;

The m_controls variable hold all my controls;
m_controlsOnMouseEnter, m_controlsOnMouseOver, m_controlsOnMouseExit hold the controls that I'm checking for mouseEvents;
And m_controlsWithMouseOver are the controls with 'Over'.

In each 'mouseMove sf::Event', I check based on those variables which function I should call, then I just call it.
control->mouseEnter();
control->mouseOver();
control->mouseExit();

And yeah, there's some ifs and elses, but not so much, just the needed.

Thank you, I implement one new class "layer" wich has one atribute int, its atribute controls the "layer where is the mouse". After this come the chains of ifs and elses inside the other classes. (unfortunately I needid rewrite each one)
« Last Edit: November 08, 2016, 07:46:53 pm by djm_jm »