Skip to content
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

Control panel for simulations #443

Open
wants to merge 47 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
1d769c0
Simple button group class
lemniscate8 Aug 2, 2021
be39c0c
Idea for concatenating button groups
lemniscate8 Aug 2, 2021
1a160a4
Toggle button setup for play/pause button
lemniscate8 Aug 2, 2021
ce2d52e
Some setup for the toggle button
lemniscate8 Aug 3, 2021
8992466
Fix switch styling in Bootstrap v5.0
lemniscate8 Aug 3, 2021
22cb37f
Get toggle button working
lemniscate8 Aug 3, 2021
033ac59
Some documentation
lemniscate8 Aug 4, 2021
5db885f
Add styling for imbedded toggle button groups
lemniscate8 Aug 4, 2021
8f649f3
Input works on radio buttons now
lemniscate8 Aug 4, 2021
6ebd496
No need for components as members
lemniscate8 Aug 9, 2021
9f5a3fd
Control panel basics
lemniscate8 Aug 9, 2021
875640b
Allow grouping buttons in ControlPanel
lemniscate8 Aug 10, 2021
7408f8f
No more control panel for now
lemniscate8 Aug 18, 2021
2149bef
Ironing out
lemniscate8 Aug 19, 2021
b9fcedc
Use checker function
lemniscate8 Aug 19, 2021
c2e1aa0
Fix bug with toggle clicks
lemniscate8 Aug 20, 2021
3ae3215
Optimize checker and fix reference issues
lemniscate8 Aug 20, 2021
50ba064
Document control panel
lemniscate8 Aug 20, 2021
b099a3b
Remove step button disabling
lemniscate8 Aug 20, 2021
0d3256e
Add documentation
lemniscate8 Aug 20, 2021
b0be177
Swap rate and unit parameters in constructor
lemniscate8 Aug 25, 2021
5a4cc26
Add unit test for control panel
lemniscate8 Aug 25, 2021
21f8b50
Test for ToggleButtonGroup
lemniscate8 Aug 25, 2021
d0faa5c
Address feedback
lemniscate8 Sep 3, 2021
e4f3f2a
More documentation
lemniscate8 Sep 3, 2021
ca489f3
Simplify some stuff
lemniscate8 Sep 3, 2021
2aac8ac
Remove cout
lemniscate8 Sep 3, 2021
c9abe28
More descriptive template types
lemniscate8 Sep 9, 2021
8ba6455
Explain DivInfo constructor for Div
lemniscate8 Sep 9, 2021
e3db915
Use map to hold checker instances
lemniscate8 Sep 22, 2021
6be2bb2
Name change for clarity
lemniscate8 Sep 27, 2021
892c1d0
Merge branch 'master' into control-panel
mmore500 Sep 29, 2021
0ef6beb
Include string header
mmore500 Sep 29, 2021
7bab656
Include string header
mmore500 Sep 29, 2021
9179f2e
Implement and test DisjointVariant
mmore500 Sep 29, 2021
30e0ed1
Use relative path for in-library includes
mmore500 Sep 29, 2021
9927976
Fix duplicated constructor
mmore500 Sep 29, 2021
30ae1ca
Add missing header
mmore500 Sep 29, 2021
e65067c
Add Assign and Activate methods
mmore500 Sep 29, 2021
b150900
Refactor ControlPanel to use DisjointVariant
mmore500 Sep 29, 2021
359be34
Add DyanmicRefreshChecker
mmore500 Sep 29, 2021
167ce5b
Add zero-overhead static test
mmore500 Sep 29, 2021
4deacf4
Buff DisjointVariant static asserts
mmore500 Sep 29, 2021
6083037
Implement and test ApplyToAll
mmore500 Oct 1, 2021
eae6c72
Mock up describe functions
mmore500 Oct 1, 2021
a656377
Revert "Refactor ControlPanel to use DisjointVariant"
mmore500 Oct 1, 2021
414e349
Minor refactoring on @lemniscate8's approach
mmore500 Oct 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions include/emp/datastructs/DisjointVariant.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* @note This file is part of Empirical, https://github.com/devosoft/Empirical
* @copyright Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
* @date 2021
*
* @file DisjointVariant.hpp
* @brief A container similar to std::variant, where only one of a set of
* types can be active, but state is maintained for inactive types (they are
* not destructed or overwritten).
*/


#ifndef EMP_DISJOINT_VARIANT_HPP

#include <tuple>
#include <type_traits>
#include <utility>
#include <variant>

#include "../polyfill/type_identity.hpp"

namespace emp {

template<typename ...Ts>
class DisjointVariant {

// Holds state for each element.
std::tuple<Ts...> disjoint_data;

/// Tracks which type is active.
std::variant<std::type_identity<Ts>...> active_typeid;

public:

/// Forwarding constructor.
template<typename... Args>
DisjointVariant(Args&&... args)
: disjoint_data(std::forward<Args>(args)...)
{}

/// Switch which data element is active.
template<typename T>
void Activate() {
using wrapped_active_type_t = std::type_identity<T>;
active_typeid.template emplace<wrapped_active_type_t>();
}

/// Assign to data element.
template<typename T>
void AssignToElement(T&& val) {
std::get<T>(disjoint_data) = std::forward<T>(val);
}

/// Assign data element and set that element as active.
template<typename T>
void AssignAndActivate(T&& val) {
AssignToElement<T>( std::forward<T>(val) );
Activate<T>();
}

/// Wraps std::visit to execute visitor on active data element.
template<class Visitor>
decltype(auto) Visit(Visitor&& visitor) {
return std::visit(
[this, &visitor]( const auto& typeid_ ){
using wrapped_active_type_t = std::decay_t<decltype(typeid_)>;
using active_type_t = typename wrapped_active_type_t::type;
auto& active_data = std::get<active_type_t>( disjoint_data );
return std::forward<Visitor>(visitor)(active_data);
},
active_typeid
);
}


/// Wraps std::apply to execute function on each data element.
template<class UnaryFunction>
void ApplyToAll(UnaryFunction&& f) {
// adapted from https://stackoverflow.com/a/54053084
std::apply(
[&f](auto&&... args){
(( f(args) ), ...);
},
disjoint_data
);
}


};

} // namespace emp

#endif
23 changes: 23 additions & 0 deletions include/emp/polyfill/type_identity.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef POLYFILL_TYPE_IDENTITY_H
#define POLYFILL_TYPE_IDENTITY_H

#if __cplusplus <= 201703L

// TODO: C++20 || cpp20
namespace std {

// adapted from https://en.cppreference.com/w/cpp/types/type_identity
template< class T >
struct type_identity {
using type = T;
};

}

#else // #if __cplusplus <= 201703L

#include <type_traits>

#endif // #if __cplusplus <= 201703L

#endif // #ifndef POLYFILL_TYPE_IDENTITY_H
61 changes: 61 additions & 0 deletions include/emp/prefab/ButtonGroup.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @note This file is part of Empirical, https://github.com/devosoft/Empirical
* @copyright Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
* @date 2021
*
* @file ButtonGroup.hpp
* @brief ButtonGroups add styling to compactly display a group of buttons and
* provides methods useful for moving buttons between groups.
*/

#ifndef EMP_BUTTON_GROUP_HPP
mmore500 marked this conversation as resolved.
Show resolved Hide resolved
#define EMP_BUTTON_GROUP_HPP

#include "emp/web/Div.hpp"

namespace emp::prefab {
/**
* A ButtonGroup is a container with styling specifically to display buttons.
* It also provides methods for moving buttons from one group into another
* allowing the user to combine groups.
*
* Use a ButtonGroup to place buttons of a similar role into the same
* container or to save space by placing buttons without gaps between them.
mmore500 marked this conversation as resolved.
Show resolved Hide resolved
*/
class ButtonGroup : public web::Div {

protected:
/**
* A protected contructor for a ButtonGroup for internal use only. See the
* prefab/README.md for more information on this design pattern.
*
* @param info_ref shared pointer containing presistent state
*/
ButtonGroup(web::internal::DivInfo * info_ref) : web::Div(info_ref) {
SetAttr("class", "btn-group");
}

public:
/**
* Constructor for a ButtonGroup.
* @param in_id HTML ID of ButtonGroup div
*/
ButtonGroup(const std::string & in_id="")
: ButtonGroup(new web::internal::DivInfo(in_id)) { ; }

/**
* A function useful for joining two button groups together into one unit.
* Removes buttons from the ButtonGroup passed in and appends them in order
* to this button group group.
*
* @param btn_group a button group
mmore500 marked this conversation as resolved.
Show resolved Hide resolved
*/
ButtonGroup & TakeChildren(ButtonGroup && btn_group) {
*this << btn_group.Children();
btn_group.Clear();
return (*this);
}
};
} // namespace emp::prefab

#endif // #ifndef EMP_BUTTON_GROUP_HPP
Loading