Set View problem
« on: 05 April 2014, 08:03:43 »
Hi
I'm having a problem with TGUI not adjusting mouse coordinates (responding to button events) when the view is set to any size other than the window size.
I'm trying to display a 1280x1024 picture with a button on the screen using different View sizes.  Using sf::setView I can get the picture to scale with the view provided I use gui->draw(false) (i.e., no resize), but mouse coords are not converted (i.e., pressing the button does not trigger any events, but when I move the mouse pointer to actual screen corrds corresponding to non-resized button coords, I can press the button).

Below is the minimum code. How do i tell tgui to use resized (View) coords?  What am I doing wrong?
Note, I have tried setting SView2 when drawing and then SView1 for event handling but that did not work.  I don't want to manually resize and reposition all the GUI objects to suit different window sizes. 


Code: [Select]
#include <SFML/Graphics.hpp>
#include <TGUI/TGUI.hpp>
#include <windows.h>

int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, INT)
{
   // Initialise SFML Render Window

   // Create Render Window
   sf::RenderWindow window(sf::VideoMode(1280,1024), "Test", sf::Style::Fullscreen);

   sf::View      SView1;
   sf::View      SView2;   

   // Set view
   SView1.reset(sf::FloatRect(0, 0, 1280, 1024));   
   SView2.reset(sf::FloatRect(0, 0, 2000, 2000));
   
   window.setView(SView1);    // SView1 works fine
//   window.setView(SView2);  // SView2 resizes okay, but gui button doesn't work

   // Create GUI
   tgui::Gui gui(window);

   if (gui.setGlobalFont("../../Fonts/DejaVuSans.ttf") == false)
      return(false);

   // Load a 1280x1024 size form with a button
   if (!gui.loadWidgetsFromFile("form.txt"))
      return(false);

   while (window.isOpen())
   {
        sf::Event event;

        while (window.pollEvent(event))
      {
            if (event.type == sf::Event::Closed)
         {
                window.close();
            }
         else if (event.type == sf::Event::KeyPressed)
         {
            switch (event.key.code)
            {
               case sf::Keyboard::Escape:
                  window.close();
                  break;
            }
         }

         gui.handleEvent(event);
      }


   tgui::Callback callback;
   while (gui.pollCallback(callback))
   {
         // Do event handler for gui here
   }

   window.clear();
   gui.draw(false);
   window.display();

   }
   return(0);
}

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: Set View problem
« Reply #1 on: 05 April 2014, 10:52:43 »
The gui.draw function takes care of the rendering and the gui.handleEvent function takes care of sending the events to the widgets and making them respond in the right way.

So if you tell the draw function to use a different view then you should also give this view to handleEvent.
Code: [Select]
gui.handleEvent(event, false);
Also make sure that the same view is set at the moment the draw function is called and at the moment handleEvent is called. But this doesn't look like a problem in your minimum code.

Re: Set View problem
« Reply #2 on: 06 April 2014, 01:44:33 »
Thanks Texus for the quick reply and for making such a useful GUI tool ;D