Skip to content

Instantly share code, notes, and snippets.

@EnterTheNameHere
Created September 14, 2012 19:42
Show Gist options
  • Save EnterTheNameHere/3724245 to your computer and use it in GitHub Desktop.
Save EnterTheNameHere/3724245 to your computer and use it in GitHub Desktop.
#define SFML_STATIC
#include <SFML/Graphics.hpp>
#define SFGUI_STATIC
#include <SFGUI/SFGUI.hpp>
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
class ButtonExample {
public:
// Our button click handler.
void OnButtonClick();
void Run();
//
//My Stuff (1/4)
//
//Function called when toolboxTileSelectButton is clicked
void tileSelectButtonClicked();
//Function called when the tile type buttons are clicked
void tileTypeClicked();
//Boolean to indicate if the selection window is open or not
bool toolboxTileSelectWindowIsShown;
//Tile Select Window
sfg::Window::Ptr toolboxTileSelectWindow;
//
//End of my stuff (1/4)
//
private:
// Create an SFGUI. This is required before doing anything with SFGUI.
sfg::SFGUI m_sfgui;
// Create the label pointer here to reach it from OnButtonClick().
sfg::Label::Ptr m_label;
//
//My stuff (2/4)
//
//Button to open the selection window
sfg::Button::Ptr toolboxTileSelectButton;
//Box to contain toolboxTileSelectButton
sfg::Box::Ptr tileSelectBox;
//Alignment to center tileSelectBox
sfg::Alignment::Ptr tileSelectAlignment;
//Tile Select Window Stuff
sfg::Box::Ptr toolboxTileSelectBox;
sfg::Box::Ptr toolboxTileSelectScrolledWindowBox;
sfg::ScrolledWindow::Ptr toolboxTileSelectScrolledWindow;
//
//End of my stuff (2/4)
//
};
void ButtonExample::OnButtonClick() {
m_label->SetText( "Hello SFGUI, pleased to meet you!" );
}
void ButtonExample::Run() {
// Create SFML's window.
sf::RenderWindow render_window( sf::VideoMode( SCREEN_WIDTH, SCREEN_HEIGHT ), "Hello world!" );
//
//My stuff (3/4)
//
// Create the tile select button
toolboxTileSelectButton = sfg::Button::Create();
//Creating an SF image for the button default
sf::Image toolboxTileSelectButtonImage;
//Loading the default image for the button
toolboxTileSelectButtonImage.loadFromFile("default_box.png");
//Creating a SFG image using the previous SF image
sfg::Image::Ptr toolboxTileSelectButtonImageSFG = sfg::Image::Create(toolboxTileSelectButtonImage);
//Setting the image of the button to the created default SFG image
toolboxTileSelectButton->SetImage(toolboxTileSelectButtonImageSFG);
//Setting the current tool button's ID to the name of the image
toolboxTileSelectButton->SetId("default_box.png");
//Setting the button's on click signal to trigger the tile selection window
toolboxTileSelectButton->GetSignal( sfg::Widget::OnLeftClick ).Connect( &ButtonExample::tileSelectButtonClicked, this );
//Creating box and alignment to make button small and centered
tileSelectBox = sfg::Box::Create( sfg::Box::HORIZONTAL, 5.0f );
tileSelectBox->Pack( toolboxTileSelectButton, false );
tileSelectAlignment = sfg::Alignment::Create();
tileSelectBox->Pack( tileSelectAlignment, true, true );
tileSelectAlignment->Add( toolboxTileSelectButton );
tileSelectAlignment->SetScale( sf::Vector2f( .0f, .0f ) );
tileSelectAlignment->SetAlignment( sf::Vector2f( .5f, .5f ) );
//Box for inside the scrolled window
toolboxTileSelectScrolledWindowBox = sfg::Box::Create( sfg::Box::VERTICAL );
// Create the ScrolledWindow.
toolboxTileSelectScrolledWindow = sfg::ScrolledWindow::Create();
// Set the ScrolledWindow to always show the horizontal scrollbar
// and only show the vertical scrollbar when needed.
toolboxTileSelectScrolledWindow->SetScrollbarPolicy( sfg::ScrolledWindow::HORIZONTAL_AUTOMATIC | sfg::ScrolledWindow::VERTICAL_AUTOMATIC );
// Add the ScrolledWindow box to the ScrolledWindow
// and create a new viewport automatically.
toolboxTileSelectScrolledWindow->AddWithViewport( toolboxTileSelectScrolledWindowBox );
//Create a box for Tile Select Window and pack the contents
toolboxTileSelectBox = sfg::Box::Create( sfg::Box::VERTICAL, 5.0f );
toolboxTileSelectBox->Pack( toolboxTileSelectScrolledWindow );
// Create a window and add the box layouter to it. Also set the window's title.
toolboxTileSelectWindow = sfg::Window::Create();
toolboxTileSelectWindow->SetStyle(sfg::Window::TITLEBAR | sfg::Window::BACKGROUND );
toolboxTileSelectWindow->SetTitle( "Tile Selection" );
toolboxTileSelectWindow->Add( toolboxTileSelectBox );
toolboxTileSelectWindow->SetRequisition( sf::Vector2f( 150.f, 450.f ) );
sf::Vector2f toolboxTileSelectWindowCenter( (SCREEN_WIDTH - toolboxTileSelectWindow->GetClientRect().width )/2 ,
(SCREEN_HEIGHT - toolboxTileSelectWindow->GetClientRect().height)/2);
toolboxTileSelectWindow->SetPosition(toolboxTileSelectWindowCenter);
//Hide Tile Select Window by default
toolboxTileSelectWindowIsShown = false;
toolboxTileSelectWindow->Show(false);
sfg::Button::Ptr tempMapTileButton = sfg::Button::Create();
sf::Image tempTileSelectButtonImage;
tempTileSelectButtonImage.loadFromFile("empty_box.png");
sfg::Image::Ptr tempTileSelectButtonImageSFG = sfg::Image::Create(tempTileSelectButtonImage);
tempMapTileButton->SetImage(tempTileSelectButtonImageSFG);
tempMapTileButton->SetLabel("empty_box.png");
tempMapTileButton->SetId("empty_box.png");
tempMapTileButton->GetSignal( sfg::Widget::OnLeftClick ).Connect( &ButtonExample::tileTypeClicked, this );
sfg::Box::Ptr tempMapTileButtonBox = sfg::Box::Create( sfg::Box::HORIZONTAL, 5.0f );
tempMapTileButtonBox->Pack( tempMapTileButton, false, false );
toolboxTileSelectScrolledWindowBox->Pack( tempMapTileButtonBox );
tempMapTileButton = sfg::Button::Create();
tempTileSelectButtonImage;
tempTileSelectButtonImage.loadFromFile("default_box.png");
tempTileSelectButtonImageSFG = sfg::Image::Create(tempTileSelectButtonImage);
tempMapTileButton->SetImage(tempTileSelectButtonImageSFG);
tempMapTileButton->SetLabel("default_box.png");
tempMapTileButton->SetId("default_box.png");
tempMapTileButton->GetSignal( sfg::Widget::OnLeftClick ).Connect( &ButtonExample::tileTypeClicked, this );
tempMapTileButtonBox = sfg::Box::Create( sfg::Box::HORIZONTAL, 5.0f );
tempMapTileButtonBox->Pack( tempMapTileButton, false, false );
toolboxTileSelectScrolledWindowBox->Pack( tempMapTileButtonBox );
tempMapTileButton = sfg::Button::Create();
tempTileSelectButtonImage;
tempTileSelectButtonImage.loadFromFile("dirt_box.png");
tempTileSelectButtonImageSFG = sfg::Image::Create(tempTileSelectButtonImage);
tempMapTileButton->SetImage(tempTileSelectButtonImageSFG);
tempMapTileButton->SetLabel("dirt_box.png");
tempMapTileButton->SetId("dirt_box.png");
tempMapTileButton->GetSignal( sfg::Widget::OnLeftClick ).Connect( &ButtonExample::tileTypeClicked, this );
tempMapTileButtonBox = sfg::Box::Create( sfg::Box::HORIZONTAL, 5.0f );
tempMapTileButtonBox->Pack( tempMapTileButton, false, false );
toolboxTileSelectScrolledWindowBox->Pack( tempMapTileButtonBox );
tempMapTileButton = sfg::Button::Create();
tempTileSelectButtonImage;
tempTileSelectButtonImage.loadFromFile("moon_box.png");
tempTileSelectButtonImageSFG = sfg::Image::Create(tempTileSelectButtonImage);
tempMapTileButton->SetImage(tempTileSelectButtonImageSFG);
tempMapTileButton->SetLabel("moon_box.png");
tempMapTileButton->SetId("moon_box.png");
tempMapTileButton->GetSignal( sfg::Widget::OnLeftClick ).Connect( &ButtonExample::tileTypeClicked, this );
tempMapTileButtonBox = sfg::Box::Create( sfg::Box::HORIZONTAL, 5.0f );
tempMapTileButtonBox->Pack( tempMapTileButton, false, false );
toolboxTileSelectScrolledWindowBox->Pack( tempMapTileButtonBox );
//
//End of my stuff (3/4)
//
// Create the label.
m_label = sfg::Label::Create( "Hello world!" );
// Create a simple button and connect the click signal.
sfg::Button::Ptr button( sfg::Button::Create( "Greet SFGUI!" ) );
button->GetSignal( sfg::Widget::OnLeftClick ).Connect( &ButtonExample::OnButtonClick, this );
// Create a vertical box layouter with 5 pixels spacing and add the label
// and button to it.
sfg::Box::Ptr box( sfg::Box::Create( sfg::Box::VERTICAL, 5.0f ) );
box->Pack( m_label );
box->Pack( button, false );
box->Pack( tileSelectBox, false );
// Create a window and add the box layouter to it. Also set the window's title.
sfg::Window::Ptr window( sfg::Window::Create() );
window->SetTitle( "Hello world!" );
window->Add( box );
// Create a desktop and add the window to it.
sfg::Desktop desktop;
desktop.Add( window );
desktop.Add( toolboxTileSelectWindow );
// We're not using SFML to render anything in this program, so reset OpenGL
// states. Otherwise we wouldn't see anything.
render_window.resetGLStates();
// Main loop!
sf::Event event;
sf::Clock clock;
while( render_window.isOpen() ) {
// Event processing.
while( render_window.pollEvent( event ) ) {
desktop.HandleEvent( event );
// If window is about to be closed, leave program.
if( event.type == sf::Event::Closed ) {
render_window.close();
}
}
// Update SFGUI with elapsed seconds since last call.
desktop.Update( clock.restart().asSeconds() );
// Rendering.
render_window.clear();
m_sfgui.Display( render_window );
render_window.display();
}
}
int main() {
ButtonExample example;
example.Run();
return 0;
}
//
//My stuff (4/4)
//
void ButtonExample::tileSelectButtonClicked() {
if (toolboxTileSelectWindowIsShown){
toolboxTileSelectWindow->Show(false);
toolboxTileSelectWindowIsShown = false;
}
else{
toolboxTileSelectWindow->Refresh();
toolboxTileSelectWindow->Show(true);
toolboxTileSelectWindowIsShown = true;
}
}
void ButtonExample::tileTypeClicked(){
//Getting which button was clicked
sfg::Widget::Ptr buttonClicked ( sfg::Context::Get().GetActiveWidget() );
//Creating a new SF image for the current tool button
//sf::Image toolboxTileSelectButtonImage;
//Loading the SF image from a file based on which button was clicked
//toolboxTileSelectButtonImage.loadFromFile(buttonClicked->GetId());
//std::cout << "Loading \"" << buttonClicked->GetId() << "\"\n";
//Creating a new SFG image for the current tool button
//sfg::Image::Ptr toolboxTileSelectButtonImageSFG = sfg::Image::Create(toolboxTileSelectButtonImage);
//Setting the current tool button's image to the new SFG image
//toolboxTileSelectButton->SetImage(toolboxTileSelectButtonImageSFG);
// buttonClicked is now sfg::Widget pointer, so we need to cast it to sfg::Button pointer first
sfg::Button::PtrConst buttonPtr = sfg::DynamicPointerCast<sfg::Button>(buttonClicked);
// We can use buttonPtr now to get the Button's sfg::Image
sfg::Image::PtrConst buttonImage = buttonPtr->GetImage();
// sfg::Image uses sf::Image as a source, so we need to get the sf::Image
// which contains our tile picture.
sf::Image picture = buttonImage->GetImage();
// And we can set the sf::Image we got as a new source in Button's sfg::Image.
toolboxTileSelectButton->GetImage()->SetImage( picture );
/*
// Or shorter (but still readable) code:
sf::Image image = sfg::DynamicPointerCast<const sfg::Button>(buttonClicked)->GetImage()->GetImage();
toolboxTileSelectButton->GetImage()->SetImage( image );
*/
//
//Resetting the state due to a weird image placement glitch
//
// Use this to "fix" the glitch instead of setting state.
toolboxTileSelectButton->Invalidate();
//toolboxTileSelectButton->SetState(sfg::Widget::INSENSITIVE);
//toolboxTileSelectButton->SetState(sfg::Widget::NORMAL);
//
//Comment out the above to see the aforementioned glitch
//
//Hide the tile selection window and set relevant booleans to false
toolboxTileSelectWindow->Show(false);
toolboxTileSelectWindowIsShown = false;
}
//
//End of my stuff (4/4)
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment