App now uses separate add_session to insert data into it
improved the hub and spoke-ish model of App by making it more clear that its components have specific responsibilities, which App then glues together. moved timermanager to App to seperate the invoker from receiver so we can eventually add interactivity also fixed the using namespace std error from a while ago
This commit is contained in:
+28
-14
@@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
#include "./app.hpp"
|
#include "./app.hpp"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
bool displaySortInput(shared_ptr<SortInput> input) {
|
bool displaySortInput(shared_ptr<SortInput> input) {
|
||||||
cout << "name: " + input->name + " values: ";
|
cout << "name: " + input->name + " values: ";
|
||||||
for (size_t i = 0; i < input->values.size(); ++i) {
|
for (size_t i = 0; i < input->values.size(); ++i) {
|
||||||
@@ -23,32 +25,44 @@ string arraytostr(shared_ptr<SortInput> input) {
|
|||||||
return stringrep.str();
|
return stringrep.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
App::App(vector<shared_ptr<SortInput>> inputs, unique_ptr<SortSession> mysortsession, unique_ptr<ViewSession> myviewsession)
|
App::App(unique_ptr<ViewSession> myviewsession)
|
||||||
: inputs(std::move(inputs))
|
: viewsession(std::move(myviewsession)) {}
|
||||||
, mysortsession(std::move(mysortsession))
|
|
||||||
, myviewsession(std::move(myviewsession))
|
//connects data with logic and invoker
|
||||||
{
|
void App::setup_session(shared_ptr<SortInput> myinput, string sorttype) {
|
||||||
|
cout << "adding sort " << sorttype << "\n";
|
||||||
|
//push data to app
|
||||||
|
inputs.push_back(myinput);
|
||||||
|
//connect data to logic
|
||||||
|
if (sorttype == "bsort") {
|
||||||
|
sortsession.add_session(myinput, sorttype);
|
||||||
|
}
|
||||||
|
//connect logic to timer (should not be hardcoded to timers)
|
||||||
|
auto prs = sortsession.sortsessions.back().get();
|
||||||
|
timermanager.add_timer(
|
||||||
|
0, [prs]() { return prs->step(); }, std::chrono::milliseconds(250));
|
||||||
}
|
}
|
||||||
|
|
||||||
void App::run() {
|
void App::run() {
|
||||||
// set up ui
|
// set up ui
|
||||||
while (myviewsession->mywindow.isOpen()){
|
while (viewsession->mywindow.isOpen()) {
|
||||||
while (const optional event = myviewsession->mywindow.pollEvent()){
|
while (const optional event = viewsession->mywindow.pollEvent()) {
|
||||||
if (event->is<sf::Event::Closed>())
|
if (event->is<sf::Event::Closed>())
|
||||||
myviewsession->mywindow.close();
|
viewsession->mywindow.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
myviewsession->mywindow.clear(sf::Color::Black);
|
//draw
|
||||||
myviewsession->mywindow.draw(myviewsession->text);
|
viewsession->mywindow.clear(sf::Color::Black);
|
||||||
myviewsession->text.setString(arraytostr(inputs[0]));
|
viewsession->mywindow.draw(viewsession->text);
|
||||||
|
viewsession->text.setString(arraytostr(inputs[0]));
|
||||||
mysortsession->update_sorts();
|
|
||||||
|
|
||||||
for (shared_ptr<SortInput> input : inputs) {
|
for (shared_ptr<SortInput> input : inputs) {
|
||||||
displaySortInput(input);
|
displaySortInput(input);
|
||||||
}
|
}
|
||||||
|
viewsession->mywindow.display();
|
||||||
|
|
||||||
myviewsession->mywindow.display();
|
//update logic
|
||||||
|
timermanager.update_timers();
|
||||||
this_thread::sleep_for(std::chrono::milliseconds(250));
|
this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-4
@@ -7,18 +7,21 @@
|
|||||||
#include <SFML/Graphics/Text.hpp>
|
#include <SFML/Graphics/Text.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#include "../timer.hpp"
|
||||||
#include "../sorters.hpp"
|
#include "../sorters.hpp"
|
||||||
#include "../sessions.hpp"
|
#include "../sessions.hpp"
|
||||||
|
|
||||||
//should connect the different parts
|
//should connect the different parts
|
||||||
//for now data, mutation, and rendering
|
//for now data, mutation, and rendering
|
||||||
struct App{
|
struct App{
|
||||||
vector<shared_ptr<SortInput>> inputs;
|
std::vector<std::shared_ptr<SortInput>> inputs;
|
||||||
unique_ptr<SortSession> mysortsession;
|
SortSession sortsession;
|
||||||
unique_ptr<ViewSession> myviewsession;
|
std::unique_ptr<ViewSession> viewsession;
|
||||||
|
TimerManager timermanager;
|
||||||
|
|
||||||
App(vector<shared_ptr<SortInput>> inputs, unique_ptr<SortSession> mysortsession, unique_ptr<ViewSession> myviewsession);
|
App(std::unique_ptr<ViewSession> myviewsession);
|
||||||
|
|
||||||
|
void setup_session(std::shared_ptr<SortInput> myinput, std::string sorttype);
|
||||||
void run();
|
void run();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+3
-14
@@ -7,30 +7,19 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "sorters.hpp"
|
#include "sorters.hpp"
|
||||||
#include "timer.hpp"
|
|
||||||
|
|
||||||
|
|
||||||
//owns and manages the sorters that mutate data, but does not own the data itself
|
//owns and manages the sorters that mutate data, but does not own the data itself
|
||||||
//steppables/sorts are currently updated by timermanager(should be decoupled)
|
|
||||||
struct SortSession {
|
struct SortSession {
|
||||||
vector<unique_ptr<Steppable>> sortsessions;
|
std::vector<std::unique_ptr<Steppable>> sortsessions;
|
||||||
TimerManager SortTimer;
|
|
||||||
|
|
||||||
SortSession(){
|
SortSession(){
|
||||||
}
|
}
|
||||||
|
|
||||||
//connects data with a sorter and controls it through a timer
|
//connects data with a sorter and controls it through a timer
|
||||||
void add_session(shared_ptr<SortInput> input, string sorttype){
|
void add_session(std::shared_ptr<SortInput> input, std::string sorttype){
|
||||||
if(sorttype == "bsort"){
|
if(sorttype == "bsort"){
|
||||||
sortsessions.push_back(make_unique<SteppedBsort>(input));
|
sortsessions.push_back(make_unique<SteppedBsort>(input));
|
||||||
}
|
};
|
||||||
auto prs = sortsessions.back().get();
|
|
||||||
SortTimer.add_timer(0,
|
|
||||||
[prs]() {return prs->step();},
|
|
||||||
std::chrono::milliseconds(250));
|
|
||||||
}
|
|
||||||
void update_sorts(){
|
|
||||||
SortTimer.update_timers();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+7
-9
@@ -8,36 +8,34 @@
|
|||||||
|
|
||||||
#include "steppable.hpp"
|
#include "steppable.hpp"
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
//ground truth domain data
|
//ground truth domain data
|
||||||
struct SortInput {
|
struct SortInput {
|
||||||
string name;
|
std::string name;
|
||||||
vector<int> values;
|
std::vector<int> values;
|
||||||
|
|
||||||
SortInput(string name, vector<int> data)
|
SortInput(std::string name, std::vector<int> data)
|
||||||
: name(name), values(data) {}
|
: name(name), values(data) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
//ro rendering of data
|
//ro rendering of data
|
||||||
struct SortView {
|
struct SortView {
|
||||||
shared_ptr<const SortInput> values;
|
std::shared_ptr<const SortInput> values;
|
||||||
size_t currenttarget = 0;
|
size_t currenttarget = 0;
|
||||||
int x,y;
|
int x,y;
|
||||||
int color;
|
int color;
|
||||||
SortView(shared_ptr<const SortInput> in): values(in) {}
|
SortView(std::shared_ptr<const SortInput> in): values(in) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
//mutates the domain data
|
//mutates the domain data
|
||||||
class SteppedBsort: public Steppable {
|
class SteppedBsort: public Steppable {
|
||||||
public:
|
public:
|
||||||
shared_ptr<SortInput> data;
|
std::shared_ptr<SortInput> data;
|
||||||
size_t stepcount = 0;
|
size_t stepcount = 0;
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
size_t j = 0;
|
size_t j = 0;
|
||||||
bool is_finished = false;
|
bool is_finished = false;
|
||||||
|
|
||||||
SteppedBsort(shared_ptr<SortInput> data);
|
SteppedBsort(std::shared_ptr<SortInput> data);
|
||||||
|
|
||||||
bool step();
|
bool step();
|
||||||
};
|
};
|
||||||
|
|||||||
+4
-10
@@ -18,27 +18,21 @@
|
|||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
vector<int> dataprovider(){
|
vector<int> dataprovider(){
|
||||||
return vector<int> {9,8,1,2,7,3,6,4,5};
|
return vector<int> {9,8,1,2,7,3,6,4,5};
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
//create data
|
//create data
|
||||||
vector<shared_ptr<SortInput>> inputs;
|
|
||||||
auto myinput = make_shared<SortInput>("arr1", dataprovider());
|
auto myinput = make_shared<SortInput>("arr1", dataprovider());
|
||||||
auto myinput2 = make_shared<SortInput>("arr2", dataprovider());
|
auto myinput2 = make_shared<SortInput>("arr2", dataprovider());
|
||||||
|
|
||||||
inputs.push_back(myinput);
|
|
||||||
inputs.push_back(myinput2);
|
|
||||||
|
|
||||||
//connect data with sorter
|
|
||||||
auto myssession = make_unique<SortSession>();
|
|
||||||
myssession->add_session(myinput, "bsort");
|
|
||||||
|
|
||||||
auto myviewsession = make_unique<ViewSession>();
|
auto myviewsession = make_unique<ViewSession>();
|
||||||
|
|
||||||
//create app with initialized dependencies
|
//create app with initialized dependencies
|
||||||
App myapp(std::move(inputs), std::move(myssession), std::move(myviewsession));
|
App myapp(std::move(myviewsession));
|
||||||
|
//add and connect data
|
||||||
|
myapp.setup_session(myinput, "bsort");
|
||||||
|
//actually start the app
|
||||||
myapp.run();
|
myapp.run();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user