Repository navigation
add_button
Daniel edited this page Sep 27, 2021
·
15 revisions
The add_button function will add a button to the ui handler, add_button does not return the FUI_Element* related to the button.
add_button has two different implementations
add_button(
const std::string& identifier,
const std::string& text,
olc::vi2d position,
olc::vi2d size,
std::function<void()> callback
);
add_button(
const std::string& parent_id,
const std::string& identifier,
const std::string& text,
olc::vi2d position,
olc::vi2d size,
std::function<void()> callback
);Example usage for button with a parent
// create the UI handler
olc::FrostUI ui;
// create a window
ui.create_window("window1", "TITLE HERE", { 10, 10 }, { 300, 200 });
// create button while specifying the parent_id manually
ui.add_button("window1", "button1", "Example button", { 10, 10 }, { 100, 20 }, [&]
{
/*some actions to happen when button is pressed*/
});Example usage for standalone button
// create a button that does not have a parent
ui.add_button("button1", "Example button", { 10, 10 }, { 100, 20 }, [&]
{
/*some actions to happen when button is pressed*/
});We can also access the member functions by making use of find_element(const std::string& id)
// we need to pass a bool for the make_toggleable() member function
bool button_state = false;
// store the FUI_Element* connected with the button
auto button = ui.find_element("button1");
/*
This will make the button act as a checkbox
make_toggleable will make the button act like a checkbox but still look like a button
it simply changes from having 3 states to having a 4th which is "active"
that is why we need to pass a bool* so the framework can automatically update this variable
just like the add_checkbox function requires a bool
*/
button->make_toggleable(button_state);Available member functions for buttons
// set the size of the button
void set_size(olc::vi2d s);
// set the position of the button
void set_position(olc::vi2d p);
// set the button text
void set_text(const std::string& txt);
// set the text color of the button
void set_text_color(olc::Pixel color);
// get the group the button is apart of
std::string get_group() const;
// set the scaling of the text
void scale_text(olc::vf2d scale);
// will make the button act like a checkbox
void make_toggleable(bool* state);
// will return the position of the button
const olc::vi2d get_position() const;
// will return the text size of the element
olc::vi2d get_text_size(olc::PixelGameEngine* pge);