diff --git a/backend/Calculator.cpp b/backend/Calculator.cpp index ccb917f..6b4a288 100644 --- a/backend/Calculator.cpp +++ b/backend/Calculator.cpp @@ -1,11 +1,27 @@ #include "Calculator.h" +Calculator::Calculator() { + std::stringstream *s = new stringstream; + this->ip = s; + this->ts = new TokenStream(ip); + this->parser = new Parser(ts); + this->ownsStream = true; // we own the stream and have to delete it +} + Calculator::Calculator(std::istream *ip) { this->ip = ip; - TokenStream *ts = new TokenStream{ip}; - Parser *ps = new Parser{ts}; - this->parser = ps; + this->ts = new TokenStream{ip}; + this->parser = new Parser{ts}; + this->ownsStream = false; // we don't own the stream and can't delete it +} + +Calculator::~Calculator() { + if (this->ownsStream) { + delete this->ip; + } + delete this->ts; + delete this->parser; } void Calculator::calculate(std::string s) { diff --git a/backend/Calculator.h b/backend/Calculator.h index 65f1c87..0dd8315 100644 --- a/backend/Calculator.h +++ b/backend/Calculator.h @@ -7,12 +7,18 @@ #include "Parser.h" #include "TokenStream.h" + class Calculator { private: std::istream *ip; + TokenStream *ts; Parser *parser; + bool ownsStream; public: + Calculator(); Calculator(std::istream *ip); + ~Calculator(); + void calculate(std::string s); std::string getMemory(); }; diff --git a/gui/CalculatorApp.cpp b/gui/CalculatorApp.cpp index be50501..0a900c8 100644 --- a/gui/CalculatorApp.cpp +++ b/gui/CalculatorApp.cpp @@ -6,7 +6,7 @@ CalculatorApp::CalculatorApp(SDL_Window *win, SDL_Renderer *ren) : App(win, ren) { // widget for memory TextOutput *memory = new TextOutput{ - win, ren, 30, 30, 600, 400, currentMemory(), 24 + win, ren, 30, 30, 600, 400, "", 18 }; // widget for input prompt @@ -15,18 +15,19 @@ CalculatorApp::CalculatorApp(SDL_Window *win, SDL_Renderer *ren) : App(win, ren) }; // widget for input - CalculatorInput *input = new CalculatorInput{win, ren, 300, 450, 600, 500, "", 24, memory}; + this->calculator = new Calculator{}; + CalculatorInput *input = new CalculatorInput{ + win, ren, 300, 450, 600, 500, "", 24, memory, this->calculator + }; + widgets.push_back(memory); widgets.push_back(prompt); widgets.push_back(input); } -// TODO: we don't need this here, CalculatorInput takes care of it -std::string CalculatorApp::currentMemory() { - // TODO: call calculator and get memory - // or if it's not the same instance every time, maybe have a separate memory - // as member of Calculator App, that will be passed every time a new - // Calculator App instance is made - return "[m1] 15.2\n[m2] 122\n[m3] 99"; + +CalculatorApp::~CalculatorApp() { + delete this->calculator; + // deletion of widgets happens in superclass destructor } diff --git a/gui/CalculatorApp.h b/gui/CalculatorApp.h index 2d50338..fd84bd7 100644 --- a/gui/CalculatorApp.h +++ b/gui/CalculatorApp.h @@ -1,14 +1,21 @@ -#ifndef _CALCULATOR_H_ -#define _CALCULATOR_H_ +#ifndef _CALCULATOR_APP_H_ +#define _CALCULATOR_APP_H_ #include +#include "backend/Calculator.h" #include "App.h" + +class Calculator; + + class CalculatorApp : public App { +private: + Calculator *calculator; public: CalculatorApp(SDL_Window *window, SDL_Renderer *renderer); - std::string currentMemory(); + virtual ~CalculatorApp(); }; #endif diff --git a/gui/CalculatorInput.cpp b/gui/CalculatorInput.cpp index eaca7f2..2969db0 100644 --- a/gui/CalculatorInput.cpp +++ b/gui/CalculatorInput.cpp @@ -3,9 +3,10 @@ CalculatorInput::CalculatorInput( SDL_Window *w, SDL_Renderer *r, int x1, int y1, int x2, int y2, - std::string text, int fontSize, TextOutput *output + std::string text, int fontSize, TextOutput *output, Calculator *calculator ) : LineInput(w, r, x1, y1, x2, y2, text, fontSize) { this->output = output; + this->calculator = calculator; } App * CalculatorInput::handleEvent(SDL_Event event) { @@ -15,16 +16,9 @@ App * CalculatorInput::handleEvent(SDL_Event event) { } if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RETURN) { - // TODO: call math-tool calculate_single, then call math-tool get_current_memory(), - // then assign the result of that to this->output - this->output->setText("[m1] 15\n[m2] 27.2"); + this->calculator->calculate(this->text); + this->output->setText(this->calculator->getMemory()); this->text = ""; } return nextApp; // if it's nullptr, we want to stay in the calculator } - - -double CalculatorInput::calculate(std::string text) { - // TODO: call the math-tool Calculator here - return 14.2; -} diff --git a/gui/CalculatorInput.h b/gui/CalculatorInput.h index 83d6038..2287560 100644 --- a/gui/CalculatorInput.h +++ b/gui/CalculatorInput.h @@ -1,20 +1,23 @@ #ifndef _CALCULATOR_INPUT_H_ #define _CALCULATOR_INPUT_H_ +#include "backend/Calculator.h" #include "TextOutput.h" #include "LineInput.h" +class Calculator; + class CalculatorInput : public LineInput{ private: TextOutput *output; + Calculator *calculator; public: CalculatorInput( SDL_Window *w, SDL_Renderer *r, int x1, int y1, int x2, int y2, - std::string text, int fontSize, TextOutput *output + std::string text, int fontSize, TextOutput *output, Calculator *c ); virtual App * handleEvent(SDL_Event event); - double calculate(std::string text); }; #endif diff --git a/gui/FunctionPlotterApp.h b/gui/FunctionPlotterApp.h index 310eccd..ed25e2c 100644 --- a/gui/FunctionPlotterApp.h +++ b/gui/FunctionPlotterApp.h @@ -1,5 +1,5 @@ -#ifndef _FUNCTION_PLOTTER_H_ -#define _FUNCTION_PLOTTER_H_ +#ifndef _FUNCTION_PLOTTER_APP_H_ +#define _FUNCTION_PLOTTER_APP_H_ #include "App.h" #include "Graph.h" diff --git a/gui/GeometryApp.h b/gui/GeometryApp.h index 93abaf4..1c3eaaa 100644 --- a/gui/GeometryApp.h +++ b/gui/GeometryApp.h @@ -1,5 +1,5 @@ -#ifndef _GEOMETRY_H_ -#define _GEOMETRY_H_ +#ifndef _GEOMETRY_APP_H_ +#define _GEOMETRY_APP_H_ #include "App.h" #include "Graph.h" diff --git a/gui/render_text.cpp b/gui/render_text.cpp index 5d5d8cc..37b1d39 100644 --- a/gui/render_text.cpp +++ b/gui/render_text.cpp @@ -17,10 +17,11 @@ void render_text( // helper variables int textW = xEnd - xStart; int textH = yEnd - yStart; + int lineHeight = fontSize * 1.4; // determine space between lines // starting point of message rectangle int startX = xStart; - int startY = yStart - 70; + int startY = yStart - lineHeight; // text has to be converted to istream in order to call getline std::stringstream ss(text); @@ -28,7 +29,13 @@ void render_text( // break at newline and render single line while(getline(ss, textToRender, '\n')) { - startY += 70; + if (startY >= yEnd) { + // reset startY + startY = yStart - lineHeight; + // clear screen + SDL_RenderClear(renderer); + } + startY += lineHeight; SDL_Surface *textSurface = TTF_RenderText_Solid( font, textToRender.c_str(), color );