Clickable pictures.
« on: 22 November 2015, 20:48:12 »
Hey. I'm trying to set up clickable pictures. I searched the forums a bit, and tried to figure out how to do it, but it's not working.

This doesn't work:

   tgui::Picture::Ptr robot_face = std::make_shared<tgui::Picture>();
   robot_pic->connect("clicked", &Equip_Screen::select_robot, this);

If I make it a button, tho, it works

   tgui::Button::Ptr robot_face = theme->load("Button");
   robot_face->connect("pressed", &Equip_Screen::select_robot, this);

Assume that the positions and textures were set, I cut them out so the code is minimal.

What is wrong?
« Last Edit: 22 November 2015, 20:50:01 by desocupado »

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: Clickable pictures.
« Reply #1 on: 22 November 2015, 22:35:50 »
The code should work fine. In the code in your post you did use robot_face as widget while you called connect on robot_pic, but if they are called on the same object then it works here. If that was just a mistake in your post and not in your code then you might want to create a minimal code that shows the issue so that I can reproduce it here.

Re: Clickable pictures.
« Reply #2 on: 22 November 2015, 22:57:37 »
Sigh.

It was just that. Really sorry about that, it's the second time I make a silly mistake like this today.

Could you tell me if I'm right about something tho, in this line:

robot_face->connect("clicked", &Equip_Screen::select_robot, this, i);

The value of "i" is set at the time the line executes. So if I do something like this:

int i = 1;
robot_face->connect("clicked", &Equip_Screen::select_robot, this, i);
i = 2;

wait for button press

When the button is pressed, the select_robot function is going to be called with the value of 1 as argument, correct?

And if I do

robot_face->connect("clicked", &Equip_Screen::select_robot, this, std::ref(i));

Then it'll store a reference to i, and use it's value?
« Last Edit: 22 November 2015, 23:02:51 by desocupado »

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: Clickable pictures.
« Reply #3 on: 24 November 2015, 14:40:46 »
Yeah, that is correct.
I had to test myself what std::ref would do when the function itself doesn't take a reference as parameter but it does indeed passes the new value to the function.
But I guess you might have figured that out yourself by now.

Since I read your reply before the edit I only saw the edited version by chance. If you want to ask a new question it is better to add a new reply than to edit the post, that way I will get a new mail so I can't miss it.