-
Notifications
You must be signed in to change notification settings - Fork 38
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
1d769c0
be39c0c
1a160a4
ce2d52e
8992466
22cb37f
033ac59
5db885f
8f649f3
6ebd496
9f5a3fd
875640b
7408f8f
2149bef
b9fcedc
c2e1aa0
3ae3215
50ba064
b099a3b
0d3256e
b0be177
5a4cc26
21f8b50
d0faa5c
e4f3f2a
ca489f3
2aac8ac
c9abe28
8ba6455
e3db915
6be2bb2
892c1d0
0ef6beb
7bab656
9179f2e
30e0ed1
9927976
30ae1ca
e65067c
b150900
359be34
167ce5b
4deacf4
6083037
eae6c72
a656377
414e349
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 |
---|---|---|
|
@@ -25,20 +25,44 @@ namespace emp::prefab { | |
std::map<std::string, int> refresh_rates{ | ||
{"MILLISECONDS", 100}, {"FRAMES", 5} | ||
}; | ||
checker_func_t redraw_checker; | ||
|
||
const std::map<std::string, const checker_func_t> refresh_checkers{ | ||
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. explain what they take as a param and what they return 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. hmm... it looks like you're only allowing one refresh checker at a time? What I would do is make two functor classes as public members of this class ControlPanel::MillisecondRefresher ControlPanel::FrameRefresher Both functor classes would take a value as their constructor instead of storing refresh_rates, refrhesh_units, and refresh_checkers I would just store a std::function refresh_checker |
||
{ "MILLISECONDS", | ||
[elapsed_milliseconds = 0, this] | ||
(const web::Animate & anim) mutable { | ||
int rate = this->refresh_rates[this->refresh_unit]; | ||
elapsed_milliseconds += anim.GetStepTime(); | ||
if (elapsed_milliseconds > rate) { | ||
elapsed_milliseconds -= rate; | ||
if (elapsed_milliseconds > rate) elapsed_milliseconds = 0; | ||
return true; | ||
} | ||
return false; | ||
}}, | ||
{ "FRAMES", | ||
[this](const web::Animate & anim) { | ||
return anim.GetFrameCount() % this->refresh_rates[this->refresh_unit]; | ||
} | ||
} | ||
}; | ||
|
||
checker_func_t do_redraw; | ||
|
||
emp::vector<web::Widget> refresh_list; | ||
std::function<void()> simulation; | ||
|
||
public: | ||
ControlPanelInfo(const std::string & in_id="") | ||
: DivInfo(in_id), simulation([](){ ; }) { ; } | ||
: DivInfo(in_id), | ||
refresh_unit("MILLISECONDS"), | ||
do_redraw(refresh_checkers.at(refresh_unit)), | ||
simulation([](){ ; }) { ; } | ||
|
||
const checker_func_t & GetRedrawChecker() const { | ||
return redraw_checker; | ||
return do_redraw; | ||
} | ||
|
||
void SetSimulation(std::function<void()> & sim) { | ||
void SetSimulation(const std::function<void()> & sim) { | ||
simulation = sim; | ||
} | ||
|
||
|
@@ -48,24 +72,7 @@ namespace emp::prefab { | |
|
||
void SetUnit(const std::string & unit) { | ||
refresh_unit = unit; | ||
|
||
const int & rate = refresh_rates[refresh_unit]; | ||
if (unit == "MILLISECONDS") { | ||
redraw_checker = [elapsed_milliseconds = 0, rate] | ||
(const web::Animate & anim) mutable { | ||
elapsed_milliseconds += anim.GetStepTime(); | ||
if (elapsed_milliseconds > rate) { | ||
elapsed_milliseconds -= rate; | ||
if (elapsed_milliseconds > rate) elapsed_milliseconds = 0; | ||
return true; | ||
} | ||
return false; | ||
}; | ||
} else if (unit == "FRAMES") { | ||
redraw_checker = [rate](const web::Animate & anim) { | ||
return anim.GetFrameCount() % rate; | ||
}; | ||
} | ||
do_redraw = refresh_checkers.at(refresh_unit); | ||
} | ||
|
||
void SetRate(const int & rate) { | ||
|
@@ -139,15 +146,17 @@ namespace emp::prefab { | |
); | ||
|
||
AddAnimation(GetID(), | ||
[&, run_sim=GetSimulation(), | ||
refresh_list=Info()->GetRefreshList(), | ||
check_redraw=Info()->GetRedrawChecker()] | ||
[&run_sim=GetSimulation(), | ||
&refresh_list=Info()->GetRefreshList(), | ||
&do_redraw=Info()->GetRedrawChecker()] | ||
(const web::Animate & anim) mutable { | ||
// Run the simulation function every frame | ||
run_sim(); | ||
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. I would name this step_sim, or update_sim |
||
// Redraw widgets according to a rule | ||
if(check_redraw(anim)) { | ||
for (emp::web::Widget & wid : refresh_list) { | ||
if(do_redraw(anim)) { | ||
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. rename do_redraw should_redraw I might also move auto& wid : refresh_list wid.Redraw to a helper method (probably private?) called do_redraw 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. or DoRedraw |
||
std::cout << "List size " << refresh_list.size() << std::endl; | ||
for (auto & wid : refresh_list) { | ||
std::cout << "Redrawing " << wid.GetID() << std::endl; | ||
wid.Redraw(); | ||
} | ||
} | ||
|
@@ -158,6 +167,7 @@ namespace emp::prefab { | |
[&anim=Animate(GetID()), step=web::Button(step)] | ||
(bool is_active) mutable { | ||
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. const bool |
||
if (is_active) { | ||
|
||
anim.Start(); | ||
} else { | ||
anim.Stop(); | ||
|
@@ -181,7 +191,7 @@ namespace emp::prefab { | |
new internal::ControlPanelInfo(in_id) | ||
) { ; } | ||
|
||
ControlPanel & SetSimulation(std::function<void()> & sim) { | ||
ControlPanel & SetSimulation(const std::function<void()> & sim) { | ||
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. SetSimulationUpdateCallback |
||
Info()->SetSimulation(sim); | ||
return *this; | ||
} | ||
|
@@ -201,6 +211,7 @@ namespace emp::prefab { | |
|
||
void AddToRefreshList(Widget & area) { | ||
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. Is there a definite use case for public access to this? We can make it private and then always make it public later |
||
Info()->GetRefreshList().push_back(area); | ||
std::cout << Info()->GetRefreshList().size() << std::endl; | ||
} | ||
|
||
template <typename IN_TYPE> | ||
|
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.
unordered_map