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

Author Topic: How to make the entire window draggable?  (Read 1001 times)

0 Members and 1 Guest are viewing this topic.

KnIfER

  • Newbie
  • *
  • Posts: 5
  • Personal Text
    • View Profile
How to make the entire window draggable?
« on: March 24, 2018, 02:28:56 am »
Hi all :),I am using sfml on the platform windows,to mimic potplayer. 
with sfml and windows my application is blocked while resizing or moving,so I've decided to abandon the default title bar and the four borders:
sf::RenderWindowmy window(vmode, "Imgui-sfml widgets test", 0);

Now I have a lot of questions:
  I.How to make the entire window draggable?(solved)
 II.Is it possible to fake a title bar and four borders using imgui?
III.Once I've faked four borders,how do I change mouse cursor when it is hovering on one of the boders?

I think the first question is the simplest so I use that as my title.
« Last Edit: March 24, 2018, 03:57:19 am by KnIfER »
 
      Humble like a tiger
         Gentle like a killer

KnIfER

  • Newbie
  • *
  • Posts: 5
  • Personal Text
    • View Profile
Re: How to make the entire window draggable?
« Reply #1 on: March 24, 2018, 02:46:38 am »
in main loop:
event loop:

         case sf::Event::MouseMoved:
            if (isMouseDragging) {
            window.setPosition(window.getPosition() + sf::Vector2<int>(event.mouseMove.x- lastDownX, event.mouseMove.y- lastDownY));
            }
            break;
         case sf::Event::MouseButtonPressed:
            lastDownX = event.mouseButton.x;
            lastDownY = event.mouseButton.y;
            isMouseDragging = true;
            break;
         case sf::Event::MouseButtonReleased:
            isMouseDragging = false;
            break;

this will enable dragging on the window. 
The only problem however,is that dragging on imgui widgets doesn't consume the event.
luckily for me, IMGui has some useful APIs,and that solves the problem.
https://github.com/kjetand/imgui-sfml/issues/1


bug:after window.setposition programmatically,  window lost response to alt+f4,but still it is detecting key events,and after moving the mouse,window regain the response to alt+f4.
« Last Edit: March 24, 2018, 04:20:35 am by KnIfER »
 
      Humble like a tiger
         Gentle like a killer

KnIfER

  • Newbie
  • *
  • Posts: 5
  • Personal Text
    • View Profile
Re: How to make the entire window draggable?
« Reply #2 on: March 24, 2018, 08:13:18 am »
this will allow for scaling by dragging the four borders.
and won't block the main loop.
and you can grab one of the corners to scale as well.

    case sf::Event::MouseMoved:
            if (ImGui::IsAnyItemActive() || ImGui::IsAnyWindowFocused())
               break;
            if (isMouseDragging) {
               window.setPosition(window.getPosition() + sf::Vector2<int>(event.mouseMove.x - lastDownX, event.mouseMove.y - lastDownY));
            }
            //
            if (isScalingLeft) {
               window.setPosition(window.getPosition() + sf::Vector2<int>(event.mouseMove.x - lastDownX, 0));
               window.setSize(window.getSize() - sf::Vector2u{ (unsigned int)(event.mouseMove.x - lastDownX ), 0 });
            }
            if (isScalingRight) {
               //window.setSize(windowOldSize + sf::Vector2u{ (unsigned int)(event.mouseMove.x - lastDownX ), 0 });
               window.setSize(window.getSize() + sf::Vector2u{ (unsigned int)(event.mouseMove.x - lastDownX), 0 });
               lastDownX = event.mouseMove.x;
            }
            if (isScalingTop) {
               window.setPosition(window.getPosition() + sf::Vector2<int>(0,event.mouseMove.y - lastDownY));
               window.setSize(window.getSize() - sf::Vector2u{0, (unsigned int)(event.mouseMove.y - lastDownY)});
            }
            if (isScalingBottom) {
               //window.setSize(windowOldSize + sf::Vector2u{ 0,(unsigned int)(event.mouseMove.y - lastDownY)});
               window.setSize(window.getSize() + sf::Vector2u{ 0, (unsigned int)(event.mouseMove.y - lastDownY) });
               lastDownY = event.mouseMove.y;
            }
            break;
    case sf::Event::MouseButtonPressed:
            std::cout <<"MouseButtonPressed"<< std::endl;
            lastDownX = event.mouseButton.x;
            lastDownY = event.mouseButton.y;
            //
            if (lastDownX <= borderWidth) {
               isScalingLeft = true;
            }
            if (lastDownX >= window.getSize().x - borderWidth) {
               isScalingRight = true;
            }
            if (lastDownY <= borderWidth) {
               isScalingTop = true;
            }
            if (lastDownY >= window.getSize().y - borderWidth) {
               isScalingBottom = true;
            }
            if(isScalingLeft || isScalingRight || isScalingTop || isScalingBottom)break;
            isMouseDragging = true;
            break;
    case sf::Event::MouseButtonReleased:
            isMouseDragging =
            isScalingBottom=
            isScalingTop=
            isScalingRight=
            isScalingLeft = false;
            break;

The remaining problem however, ;),is that can i change the mouse cursor when hovering on the borders?
well,I can,but only for the latest build.
« Last Edit: March 24, 2018, 01:31:48 pm by KnIfER »
 
      Humble like a tiger
         Gentle like a killer

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Re: How to make the entire window draggable?
« Reply #3 on: March 24, 2018, 12:18:52 pm »
Don't you think you're doing a lot, and inviting yourself to many issues just because "with sfml and windows my application is blocked while resizing or moving" ?

Want to play movies in your SFML application? Check out sfeMovie!

KnIfER

  • Newbie
  • *
  • Posts: 5
  • Personal Text
    • View Profile
Re: How to make the entire window draggable?
« Reply #4 on: March 24, 2018, 01:15:17 pm »
I think potplayer just did the same thing——disable default titlebar、borders and implements it's own
And I love the process too :),feeling like I am still dealing with android stuffs:custom views,touch events,etc.
Others either leave users enduring visual artifacts,or simply disable resizing,for me neither is a good idea.

 
      Humble like a tiger
         Gentle like a killer

KnIfER

  • Newbie
  • *
  • Posts: 5
  • Personal Text
    • View Profile
Re: How to make the entire window draggable?
« Reply #5 on: March 24, 2018, 02:14:40 pm »
new issue:click on the border and immediately move the mouse cursor out of the application window quickly,instead of scaling out the window,I find the window loses all mouseMove events.
and with mouseButton still pressed,move the cursor backwards,then I can scale in the window correctly once the cursor has reached the borders.

clickling on the border then after a while moving cursor out of the application window quickly won's repeat this issue.

I have pushed my code and made a pre-release to show this issue.
https://github.com/KnIfER/imgui-sfml-ui-test/releases/tag/a
the application only looks laggy when I am recording the screen.

The major problem is that when I abandon titlebar&borders,I also give up all windows features,such as auto maximization when I drag the window to screen edge.
I guess that Plotplayer actually doesn't abandon titlebar&borders,instead it draws UI on the non-client area.

I will move forward anyway..
« Last Edit: March 25, 2018, 05:57:04 am by KnIfER »
 
      Humble like a tiger
         Gentle like a killer