-
Couldn't load subscription status.
- Fork 1
Add control panel and toggle button group examples #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "emp/prefab/Card.hpp" | ||
| #include "emp/prefab/CodeBlock.hpp" | ||
| #include "emp/prefab/ControlPanel.hpp" | ||
| #include "emp/tools/string_utils.hpp" | ||
| #include "emp/web/Div.hpp" | ||
| #include "emp/web/Document.hpp" | ||
|
|
||
| int simulation_counter = 0; | ||
| int refresh_counter = 0; | ||
|
|
||
| void control_panel_example( emp::web::Document& doc ) { | ||
| emp::prefab::Card control_panel_ex("INIT_CLOSED"); | ||
| doc << control_panel_ex; | ||
| card_ex.AddHeaderContent("<h3>Control Panel</h3>"); | ||
| control_panel_ex << "<h3>Live Demo:</h3><hr>"; | ||
|
|
||
| emp::prefab::ControlPanel controls{1000, "MILLISECONDS"}; | ||
| emp::Div text_region; | ||
| text_region << emp::web::Live([](){ | ||
| ++refresh_counter; | ||
| return emp::to_string("Number of refreshes: ", refresh_counter); | ||
| }); | ||
| text_region << "<br>"; | ||
| text_region << emp::web::Live([](){ | ||
| int last_sim_count = simulation_counter; | ||
| last_sim_count = 0; | ||
| return emp::to_string("Simulation calls this period: ", refresh_counter); | ||
| }); | ||
| emp::web::Div laps; | ||
| text_region << laps; | ||
| controls.AddToRefreshList(text_region); | ||
| controls.SetSimulation([]() { ++simulation_counter; }); | ||
| controls << emp::web::Button([lap, lap_count=0](){ | ||
| laps << "Lap " << lap_count << ": " << simulation_counter; | ||
| ++lap_counter; | ||
| }, "Lap").SetAttr("class", "btn btn-primary"); | ||
|
|
||
| controls << ButtonGroup{}; // Streaming a button group acts like a newline | ||
| controls << emp::web::Button([lap](){ | ||
| lap.ClearChildren(); | ||
| }, "Reset Laps").SetAttr("class", "btn btn-danger"); | ||
|
|
||
| control_panel_ex << controls; | ||
| control_panel_ex << text_region; | ||
|
|
||
|
|
||
| const std::string control_panel_code = | ||
| R"( | ||
| // Global variables: refresh_counter and simulation_counter | ||
|
|
||
| emp::prefab::ControlPanel controls{1000, "MILLISECONDS"}; | ||
|
|
||
| emp::Div text_region; | ||
| text_region << emp::web::Live([](){ | ||
| ++refresh_counter; | ||
| return emp::to_string("Number of refreshes: ", refresh_counter); | ||
| }); | ||
| text_region << "<br>"; | ||
| text_region << emp::web::Live([](){ | ||
| int last_sim_count = simulation_counter; | ||
| last_sim_count = 0; | ||
| return emp::to_string("Simulation calls this period: ", refresh_counter); | ||
| }); | ||
| emp::web::Div laps; | ||
| text_region << laps; | ||
| controls.AddToRefreshList(text_region); | ||
| controls.SetSimulation([]() { ++simulation_counter; }); | ||
| controls << emp::web::Button([lap, lap_count=0](){ | ||
| laps << "Lap " << lap_count << ": " << simulation_counter; | ||
| ++lap_counter; | ||
| }, "Lap").SetAttr("class", "btn btn-primary"); | ||
|
|
||
| controls << ButtonGroup{}; // Streaming a button group acts like a newline | ||
| controls << emp::web::Button([lap](){ | ||
| lap.ClearChildren(); | ||
| }, "Reset Laps").SetAttr("class", "btn btn-danger"); | ||
|
|
||
| doc << controls; | ||
| doc << text_region; | ||
| )"; | ||
|
|
||
| emp::prefab::CodeBlock card_code_block(control_panel_code, "c++"); | ||
| control_panel_ex << control_panel_code; | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "emp/prefab/Card.hpp" | ||
| #include "emp/prefab/CodeBlock.hpp" | ||
| #include "emp/prefab/ButtonGroup.hpp" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alphabetization |
||
| #include "emp/prefab/ToggleButtonGroup.hpp" | ||
| #include "emp/prefab/FontAwesomeIcon.hpp" | ||
| #include "emp/tools/string_utils.hpp" | ||
| #include "emp/web/Div.hpp" | ||
| #include "emp/web/Document.hpp" | ||
|
|
||
| void toggle_button_group_example( emp::web::Document& doc ) { | ||
| emp::prefab::Card tbg_ex("INIT_CLOSED"); | ||
| doc << tbg_ex; | ||
| card_ex.AddHeaderContent("<h3>Toggle Button Group</h3>"); | ||
| tbg_ex << "<h3>Live Demo:</h3><hr>"; | ||
|
|
||
| tbg_ex << "<h5>On its own:<h5>"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Casette style |
||
| emp::prefab::ToggleButtonGroup reaction{ | ||
| emp::prefab::FontAwesomeIcon("fa-smile-o"), emp::prefab::FontAwesomeIcon("fa-frown-o"), | ||
| "success", "danger" | ||
| }; | ||
| emp::web::Div reaction_post; | ||
| reaction.SetCallback([reaction_post](bool happy) { | ||
| if (happy) { | ||
| reaction_post << "Happy!"; | ||
| } else { | ||
| reaction_post << "Sad :("; | ||
| } | ||
| }); | ||
| tbg_ex << reaction; | ||
| tbg_ex << reaction_post; | ||
| tbg_ex << "<h5>In button groups:<h5>"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this down to end |
||
| ButtonGroup grouping; | ||
| grouping << emp::prefab::ToggleButtonGroup{ "Default", "Coloring" }; | ||
| grouping << emp::prefab::ToggleButtonGroup{ | ||
| "Custom", "Coloring", "light", "dark" | ||
| }; | ||
| grouping << emp::prefab::ToggleButtonGroup{ | ||
| emp::prefab::FontAwesomeIcon("fa-motorcycle"), | ||
| emp::prefab::FontAwesomeIcon("fa-car"), | ||
| "primary", "secondary" | ||
| }; | ||
| tbg_ex << grouping; | ||
|
|
||
| tbg_ex << "<h5>Hiding the inactive label:<h5>"; | ||
| ButtonGroup hidden; | ||
| hidden << emp::prefab::ToggleButtonGroup{ | ||
| "Default", "Coloring", | ||
| "success", "warning", | ||
| false | ||
| }; | ||
| hidden << emp::prefab::ToggleButtonGroup{ | ||
| "Custom", "Coloring", "light", "dark", | ||
| false | ||
| }; | ||
| hidden << emp::prefab::ToggleButtonGroup{ | ||
| emp::prefab::FontAwesomeIcon("fa-motorcycle"), emp::prefab::FontAwesomeIcon("fa-car"), | ||
| "primary", "secondary", | ||
| false | ||
| }; | ||
| tbg_ex << hidden; | ||
|
|
||
| tbg_ex << "<h5>Graying out an inactive label:<h5>"; | ||
| tbg_ex << emp::prefab::ToggleButtonGroup{ | ||
| emp::prefab::FontAwesomeIcon("fa-tree"), emp::prefab::FontAwesomeIcon("fa-tint"), | ||
| "success", "primary", | ||
| false, true | ||
| }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you put each of these into a helper maker method? |
||
|
|
||
| const std::string tbg_code = | ||
| R"( | ||
| doc << "<h5>On its own:<h5>"; | ||
| emp::prefab::ToggleButtonGroup reaction{ | ||
| emp::prefab::FontAwesomeIcon("fa-smile-o"), emp::prefab::FontAwesomeIcon("fa-frown-o"), | ||
| "success", "danger" | ||
| }; | ||
| emp::web::Div reaction_post; | ||
| reaction.SetCallback([reaction_post](bool happy) { | ||
| if (happy) { | ||
| reaction_post << "Happy!"; | ||
| } else { | ||
| reaction_post << "Sad :("; | ||
| } | ||
| }); | ||
| doc << reaction; | ||
| doc << reaction_post; | ||
| doc << "<h5>In button groups:<h5>"; | ||
| ButtonGroup grouping; | ||
| grouping << emp::prefab::ToggleButtonGroup{ "Default", "Coloring" }; | ||
| grouping << emp::prefab::ToggleButtonGroup{ | ||
| "Custom", "Coloring", "light", "dark" | ||
| }; | ||
| grouping << emp::prefab::ToggleButtonGroup{ | ||
| emp::prefab::FontAwesomeIcon("fa-motorcycle"), | ||
| emp::prefab::FontAwesomeIcon("fa-car"), | ||
| "primary", "secondary" | ||
| }; | ||
| doc << grouping; | ||
|
|
||
| doc << "<h5>Hiding the inactive label:<h5>"; | ||
| ButtonGroup hidden; | ||
| hidden << emp::prefab::ToggleButtonGroup{ | ||
| "Default", "Coloring", | ||
| "success", "warning", | ||
| false | ||
| }; | ||
| hidden << emp::prefab::ToggleButtonGroup{ | ||
| "Custom", "Coloring", "light", "dark", | ||
| false | ||
| }; | ||
| hidden << emp::prefab::ToggleButtonGroup{ | ||
| emp::prefab::FontAwesomeIcon("fa-motorcycle"), emp::prefab::FontAwesomeIcon("fa-car"), | ||
| "primary", "secondary", | ||
| false | ||
| }; | ||
| doc << hidden; | ||
|
|
||
| doc << "<h5>Graying out an inactive label:<h5>"; | ||
| doc << emp::prefab::ToggleButtonGroup{ | ||
| emp::prefab::FontAwesomeIcon("fa-tree"), emp::prefab::FontAwesomeIcon("fa-tint"), | ||
| "success", "primary", | ||
| false, true | ||
| }; | ||
| )"; | ||
|
|
||
| emp::prefab::CodeBlock tbg_code_block(control_panel_code, "c++"); | ||
| tbg_ex << tbg_code_block; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a brief comment explaining what each of these are tracking