Recent Posts

Pages: [1] 2 3 ... 10
1
Help requests / Re: BitmapButton change image at runtime
« Last post by Blaxxun on 26 October 2019, 19:36:58 »
Okay, thank you for your explainations! Its much appreciated! :)

I watched this video about lambdas https://www.youtube.com/watch?v=mWgmBBz0y8c.
I also moved back my variables and edited the lambda captures [].
Now everything still works fine but without making my variables global.

I will look into shared pointers now.

Thank you texus for your help and also for this cool GUI ! :)

2
Help requests / Re: BitmapButton change image at runtime
« Last post by texus on 26 October 2019, 19:23:41 »
Quote
I use references in function headers to avoid copies, but in combination with a foreign library i fail.
To keep it simple you can assume that Widget::Ptr is a Widget* (so if you copy it then you are just copying the pointer and not the actual widget), but to fully understand how it works you would need to understand std::shared_ptr because Widget::Ptr is just an alias for std::shared_ptr<Widget>.

Quote
I know its wrong what i did. (Although i dont know why. I just read that its wrong to have global variables)
There are 2 reasons why global variables are bad. The first one is mostly relevant to larger projects while the second one is typically only relevant for classes.

The first reason is simply because it can make the code harder to understand. Global variables can be changed from anywhere in your code, so it would be harder to follow where the value is changed throughout the code. This is why having "const" variables in global scope isn't a problem, it is only a problem with regular variables that can be changed.

The second reason is because there is no guaranteed order of construction and destruction of global variables across source files. If you have a global variables A and B then they are constructed in the order they appear in your cpp file, but if they appear in different cpp files then A could be constructed either before or after B. This is only a problem if you try to e.g. initialize A with the value of B.
SFML relies on some global variables to work, so if you create an sf::RenderWindow in global scope then it is possible that it crashes because the globals that sf::RenderWindow relies on aren't initialized yet when the window is created. It is also possible to get lucky and have the SFML globals initialized before your window in which case everything works fine, but that is not something you will want to rely on (although you will always get the exact same outcome as long as you don't change your linker settings).

So having a few integers in global scope isn't really a big issue when quickly writing a small piece of code.
3
Help requests / Re: BitmapButton change image at runtime
« Last post by Blaxxun on 26 October 2019, 18:52:00 »
Yeah, iam still a noob.  :o  :-[

I know what scopes, references and pointers are but dont really fully understand them yet.
I use references in function headers to avoid copies, but in combination with a foreign library i fail.
I dont know what lamdas are, so i will learn about them now.

I'll try to move the variables back to the main function and try to change the content "in place" and avoiding copies.
I know its wrong what i did. (Although i dont know why. I just read that its wrong to have global variables)

Thanks for the tip with the radio buttons. I do not need any text on top of the buttons. Just an icon image and maybe a tooltip.
4
Help requests / Re: BitmapButton change image at runtime
« Last post by texus on 26 October 2019, 18:33:25 »
Actually putting those variables in the header isn't a good solution (I'm not even sure why it would work).
When I told you to use "=", I wasn't really looking at your code. The EraserON and Hue values probably still need to be passed as references to the lambda, by using "=" instead of "&" you are making copies of them and you would be changing a local variable inside the lambda instead of the variable that you had outside. You might want to look up how lambdas work, but the "=" and "&" are just the defaults for the variables. To pass some variables by value and some by reference you would do something like
slider->connect("ValueChanged", [&,slider]() { Hue = slider->getValue(); }); // All variables passed by reference except slider
or
slider->connect("ValueChanged", [=,&Hue]() { Hue = slider->getValue(); }); // All variables passed by value except Hue

Quote
When you choose a tool then the button stays pressed.
When u choose another tool then this button stays pressed and the previous goes back to unpressed.
I dont know if BitmapButton is the right choice here.
You might want to use RadioButton widgets for this. The downside is that the RadioButton places its text next to the image, so if you need text on top of the button then you would have to add a Label in front of it manually on which you call isIgnoringMouseEvents() to make sure clicks on the label are passed to the button instead of absorbed by the label. I'm not sure if there are better options, but if you don't need any text then what you need definitely sounds like how radio buttons work.
5
Help requests / Re: BitmapButton change image at runtime
« Last post by Blaxxun on 26 October 2019, 18:25:30 »
I actually want to simulate some Tool buttons.
When you choose a tool then the button stays pressed.
When u choose another tool then this button stays pressed and the previous goes back to unpressed.
I dont know if BitmapButton is the right choice here.

Maybe you have a better oversight of possible features i can use?
Is there a better choice when every button should have a little tool icon?
6
Help requests / Re: BitmapButton change image at runtime
« Last post by Blaxxun on 26 October 2019, 18:21:17 »
Okay, i changed it now to
Code: [Select]
BBTN_Erase->connect("pressed", [=]() { do something }); and it works.
First several variables i had in there went red. I had them right after
Code: [Select]
int main(void){}.
Now i put those variables in the header main.h and it seems to work now.

I also had a slider that used [&] first and always worked before (Hue was already in the header).
Now i changed it to
Code: [Select]
slider->connect("ValueChanged", [=]() { Hue = slider->getValue(); }); and it works again.

Thank you very much!  :)
7
Help requests / Re: BitmapButton change image at runtime
« Last post by texus on 26 October 2019, 17:52:53 »
Is the issue resolved when changing the "&" to "=" in the BBTN_Erase->connect line?
You are currently storing a reference in that lambda. If the BBTN_Erase variable goes out of scope (so at the end of the function that contains the code you showed) then this reference will become invalid. This would cause a crash when trying to use the variable when the lambda function is being executed.
The Widget::Ptr is a pointer, so it can be passed by value without having to worry about performance with copying it.
8
Help requests / Re: BitmapButton change image at runtime
« Last post by Blaxxun on 26 October 2019, 17:43:59 »
I understand.
i did this now to create the button (minimal code):

Code: [Select]
auto BBTN_Erase = tgui::BitmapButton::create();
BBTN_Erase->setImage("img/BTN_Eraser.png");
BBTN_Erase->connect("pressed", [&]()
{
if (EraserON) {
EraserON = false;
BBTN_Erase->setImage("img/BTN_Eraser.png");}
else {
EraserON = true;
BBTN_Erase->setImage("img/BTN_EraserON.png");}  <---------- Exception thrown here when i hit keyboard
});
gui.add(BBTN_Erase,"BBTN_Erase");


Code: [Select]
if (event.key.code == sf::Keyboard::E)
{
if (EraserON) {
EraserON = false;
tgui::BitmapButton::Ptr BBTN_Erase = gui.get<tgui::BitmapButton>("BBTN_Erase");
BBTN_Erase->setImage("img/BTN_Eraser.png");
}
else {
EraserON = true;
tgui::BitmapButton::Ptr BBTN_Erase = gui.get<tgui::BitmapButton>("BBTN_Erase");
BBTN_Erase->setImage("img/BTN_EraserON.png");
}

Compiles fine but i get an exeption in the button creation part! When i hit my shortcut "E" on my keyboard.
9
Help requests / Re: BitmapButton change image at runtime
« Last post by texus on 26 October 2019, 17:14:05 »
It is impossible for a program to know what variable name you used, the compiler just translates it to an address in memory, so you do need to pass the string yourself
gui.add(BBTN_Erase, "BBTN_Erase");
10
Help requests / Re: BitmapButton change image at runtime
« Last post by Blaxxun on 26 October 2019, 17:10:19 »
Hello, thankls for your answer!

First i created the BBTN_Erase BitmapButton right after
Code: [Select]
int main(void)
{
with
Code: [Select]
auto BBTN_Erase = tgui::BitmapButton::create();
and i tryd to change/set the Image in the event part of the code with 
Code: [Select]
if (event.key.code == sf::Keyboard::E)
Code: [Select]
BBTN_Erase->setImage("img/BTN_Eraser.png");But the
Code: [Select]
BBTN_Erase was unknown in this scope.


So i changed the code to the code that i posted in my first post.

I add the BitmapButton with
Code: [Select]
gui.add(BBTN_Erase); like in my first post, when u scroll down the code section u see it.

So i guess that, the error i made, is to believe that the pointer name is the widget name...

I will check... thanks!
Pages: [1] 2 3 ... 10