Skip to content

add_button

Daniel edited this page May 6, 2021 · 15 revisions

The add_button function will add a button to the ui handler, add_button does not return the FUI_Element*.

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 by manually specifying parent_id

// 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 by letting the UI_Handler automatically set the parent

// set the default parent to the window containing the ID "window1"
ui.set_active_window("window1");

// create button without specifying parent_id (This will make the UI handler use the default parent)
ui.add_button("button1", "Example button", { 10, 10 }, { 100, 20 }, [&] 
    { 
        /*some actions to happen when button is pressed*/ 
    });

// clear the default parent
ui.clear_active_window();

Example usage for standalone button

// create a button that does not have a parent or group
ui.add_button("button1", "Example button", { 10, 10 }, { 100, 20 }, [&]
    { 
        /*some actions to happen when button is pressed*/ 
    });

UI elements can also be added into groups which allows you to only render elements of that group

// create the group
ui.add_group("group1");

// set the active group (elements added to the ui handler after this point will be added to this group)
ui.set_active_group("group1");

// create a button that has no parent but will be apart of the group "group1"
ui.add_button("button1", "Example button", { 10, 10 }, { 100, 20 }, [&]
    { 
        /*some actions to happen when button is pressed*/ 
    });

// by clearing the active group we tell the ui handler that after this point we don't want other elements to be added to any group
ui.clear_active_group();

// create a button without a group or parent (this button will set the active_group to "group1" allowing the other button which is apart of group1 to be rendered when this button is pressed)
ui.add_button("button1", "Example button", { 10, 10 }, { 100, 20 }, [&] 
    { 
        ui.set_active_group("group1")
    });

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
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);

Clone this wiki locally