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:
+33
-19
@@ -5,10 +5,12 @@
|
||||
|
||||
#include "./app.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool displaySortInput(shared_ptr<SortInput> input) {
|
||||
cout << "name: " + input->name + " values: ";
|
||||
for (size_t i = 0; i < input->values.size(); ++i) {
|
||||
printf("%d | ",input->values[i]);
|
||||
printf("%d | ", input->values[i]);
|
||||
}
|
||||
printf("\n");
|
||||
return false;
|
||||
@@ -18,37 +20,49 @@ string arraytostr(shared_ptr<SortInput> input) {
|
||||
string myoutput;
|
||||
ostringstream stringrep;
|
||||
for (size_t i = 0; i < input->values.size(); ++i) {
|
||||
stringrep << input->values[i] <<" | ";
|
||||
}
|
||||
stringrep << input->values[i] << " | ";
|
||||
}
|
||||
return stringrep.str();
|
||||
}
|
||||
|
||||
App::App(vector<shared_ptr<SortInput>> inputs, unique_ptr<SortSession> mysortsession, unique_ptr<ViewSession> myviewsession)
|
||||
: inputs(std::move(inputs))
|
||||
, mysortsession(std::move(mysortsession))
|
||||
, myviewsession(std::move(myviewsession))
|
||||
{
|
||||
App::App(unique_ptr<ViewSession> myviewsession)
|
||||
: viewsession(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(){
|
||||
//set up ui
|
||||
while (myviewsession->mywindow.isOpen()){
|
||||
while (const optional event = myviewsession->mywindow.pollEvent()){
|
||||
void App::run() {
|
||||
// set up ui
|
||||
while (viewsession->mywindow.isOpen()) {
|
||||
while (const optional event = viewsession->mywindow.pollEvent()) {
|
||||
if (event->is<sf::Event::Closed>())
|
||||
myviewsession->mywindow.close();
|
||||
viewsession->mywindow.close();
|
||||
}
|
||||
|
||||
myviewsession->mywindow.clear(sf::Color::Black);
|
||||
myviewsession->mywindow.draw(myviewsession->text);
|
||||
myviewsession->text.setString(arraytostr(inputs[0]));
|
||||
|
||||
mysortsession->update_sorts();
|
||||
//draw
|
||||
viewsession->mywindow.clear(sf::Color::Black);
|
||||
viewsession->mywindow.draw(viewsession->text);
|
||||
viewsession->text.setString(arraytostr(inputs[0]));
|
||||
|
||||
for (shared_ptr<SortInput> input : inputs) {
|
||||
displaySortInput(input);
|
||||
}
|
||||
viewsession->mywindow.display();
|
||||
|
||||
myviewsession->mywindow.display();
|
||||
//update logic
|
||||
timermanager.update_timers();
|
||||
this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||
}
|
||||
}
|
||||
|
||||
+7
-4
@@ -7,18 +7,21 @@
|
||||
#include <SFML/Graphics/Text.hpp>
|
||||
#include <memory>
|
||||
|
||||
#include "../timer.hpp"
|
||||
#include "../sorters.hpp"
|
||||
#include "../sessions.hpp"
|
||||
|
||||
//should connect the different parts
|
||||
//for now data, mutation, and rendering
|
||||
struct App{
|
||||
vector<shared_ptr<SortInput>> inputs;
|
||||
unique_ptr<SortSession> mysortsession;
|
||||
unique_ptr<ViewSession> myviewsession;
|
||||
std::vector<std::shared_ptr<SortInput>> inputs;
|
||||
SortSession sortsession;
|
||||
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();
|
||||
};
|
||||
|
||||
|
||||
+3
-14
@@ -7,30 +7,19 @@
|
||||
#include <vector>
|
||||
|
||||
#include "sorters.hpp"
|
||||
#include "timer.hpp"
|
||||
|
||||
|
||||
//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 {
|
||||
vector<unique_ptr<Steppable>> sortsessions;
|
||||
TimerManager SortTimer;
|
||||
std::vector<std::unique_ptr<Steppable>> sortsessions;
|
||||
|
||||
SortSession(){
|
||||
}
|
||||
|
||||
//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"){
|
||||
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"
|
||||
|
||||
using namespace std;
|
||||
|
||||
//ground truth domain data
|
||||
struct SortInput {
|
||||
string name;
|
||||
vector<int> values;
|
||||
std::string name;
|
||||
std::vector<int> values;
|
||||
|
||||
SortInput(string name, vector<int> data)
|
||||
SortInput(std::string name, std::vector<int> data)
|
||||
: name(name), values(data) {}
|
||||
};
|
||||
|
||||
//ro rendering of data
|
||||
struct SortView {
|
||||
shared_ptr<const SortInput> values;
|
||||
std::shared_ptr<const SortInput> values;
|
||||
size_t currenttarget = 0;
|
||||
int x,y;
|
||||
int color;
|
||||
SortView(shared_ptr<const SortInput> in): values(in) {}
|
||||
SortView(std::shared_ptr<const SortInput> in): values(in) {}
|
||||
};
|
||||
|
||||
//mutates the domain data
|
||||
class SteppedBsort: public Steppable {
|
||||
public:
|
||||
shared_ptr<SortInput> data;
|
||||
std::shared_ptr<SortInput> data;
|
||||
size_t stepcount = 0;
|
||||
size_t i = 0;
|
||||
size_t j = 0;
|
||||
bool is_finished = false;
|
||||
|
||||
SteppedBsort(shared_ptr<SortInput> data);
|
||||
SteppedBsort(std::shared_ptr<SortInput> data);
|
||||
|
||||
bool step();
|
||||
};
|
||||
|
||||
+4
-10
@@ -18,27 +18,21 @@
|
||||
using namespace std::chrono;
|
||||
using namespace std;
|
||||
|
||||
|
||||
vector<int> dataprovider(){
|
||||
return vector<int> {9,8,1,2,7,3,6,4,5};
|
||||
}
|
||||
|
||||
int main() {
|
||||
//create data
|
||||
vector<shared_ptr<SortInput>> inputs;
|
||||
auto myinput = make_shared<SortInput>("arr1", 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>();
|
||||
|
||||
//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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user