From ab0579acc9148a703ac29bd6f68f2f3c234be6b0 Mon Sep 17 00:00:00 2001 From: Tanyk2004 Date: Tue, 19 Apr 2022 20:52:33 +0530 Subject: [PATCH 1/5] calculator widget pull req_1 --- inc/Calculator.h | 394 +++++++++++++++++++++++++++++++++++++++++++++++ inc/Constants.h | 6 + src/Game.cpp | 11 +- 3 files changed, 407 insertions(+), 4 deletions(-) create mode 100644 inc/Calculator.h diff --git a/inc/Calculator.h b/inc/Calculator.h new file mode 100644 index 0000000..03b3365 --- /dev/null +++ b/inc/Calculator.h @@ -0,0 +1,394 @@ +#ifndef CALCUALTOR +#define CALCULATOR +#define RAYGUI_IMPLEMENTATION + +#include +#include "Widget.h" + +#include +#include +#include +#include "Constants.h" +#include +#include +#include +#include +using namespace std; + +class Calculator : public Widget { +std::string text; +Vector2 mousePoint; +Texture2D plusButton; +bool Button1Pressed; +int offset; +int lastOperatorIndex; + +stack operators; +stack operands; +public: + Calculator(uint32_t x, uint32_t y, uint32_t w, uint32_t h, std::string n) : + Widget (x, y, w, h, n) { + // Calls widget's constructor + //render = false; + text = ""; + mousePoint = {0.0f, 0.0f}; + Image plusIcon = LoadImage("../resources/images/plus_operator.png"); + ImageResize(&plusIcon, 50, 50); + plusButton = LoadTextureFromImage(plusIcon); + UnloadImage(plusIcon); + Button1Pressed = false; + offset = 25; + + } + void spawnButton(int x, int y, int w,int h, std::string buttonText) + { + DrawRectangleLines(x,y,w,h,SKYBLUE); + int buttonOffset = (50 - MeasureText(buttonText.c_str(),30))/2; + DrawText(buttonText.c_str(), buttonOffset+x, 10 + y, 30, SKYBLUE); + } + bool buttonInteract(int x, int y, int w,int h, std::string buttonText){ + if (CheckCollisionPointRec(mousePoint, (Rectangle){x,y,w,h})){ + DrawRectangle(x,y,w,h,SKYBLUE); + int buttonOffset = (50 - MeasureText(buttonText.c_str(),30))/2; + DrawText(buttonText.c_str(), buttonOffset+x, 10 + y, 30, BLACK); + + if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) return true; + else return false; + } + + + } + + /*int prec(char ch) { + if (ch == '^') + return 3; + else if (ch == '/' || ch == '*') + return 2; + else if (ch == '+' || ch == '-') + return 1; + else + return -1; +} +int applyOp(int b, int a, char op){ + switch(op){ + case '+': return a + b; + case '-': return b - a; + case '*': return a * b; + case '/': return b / a; + case '^': return pow(b, a); + } +} +float evaluateExpression(std::string expressionText) +{ + std::string lastOperand = ""; + float ans = 0.0f; + for( int i = 0; i <= expressionText.length(); i++){ + if(isalnum(expressionText[i])){ + lastOperand += expressionText[i]; + } + else{ + if (lastOperand.length() > 0){ + operands.push(std::stof(lastOperand)); + lastOperand = "";} + + if ( expressionText[i] == '('){ + operators.push(expressionText[i]); + } + else if(expressionText[i] == ')'){ + while (!operators.empty() && operators.top () != '('){ + int val2 = operands.top(); + operands.pop(); + int val1 = operands.top(); + operands.pop(); + char op = operators.top(); + operators.pop(); + + operands.push(applyOp(val1, val2, op)); + } + + if ( !operators.empty()){ + operators.pop(); + } + } + else{ + while (!operators.empty() && prec(operators.top()) >= prec(expressionText[i])){ + int val2 = operands.top(); + operands.pop(); + int val1 = operands.top(); + operands.pop(); + char op = operators.top(); + operators.pop(); + + operands.push(applyOp(val1, val2, op)); + } + operators.push(expressionText[i]); + } + } + } + while (!operands.empty()){ + int val2 = operands.top(); + operands.pop(); + int val1 = operands.top(); + operands.pop(); + + char op = operators.top(); + operators.pop(); + operands.push(applyOp(val1, val2, op)); + } + return operands.top(); +}*/ + +int precedence(char op){ + if(op == '+'||op == '-') + return 1; + if(op == '*'||op == '/') + return 2; + if(op == '^') + return 3; + return 0; +} + +// Function to perform arithmetic operations. +float applyOp(float a, float b, char op){ + switch(op){ + case '+': return a + b; + case '-': return a - b; + case '*': return a * b; + case '/': return a / b; + case '^': return pow(a,b); + } +} + +// Function that returns value of +// expression after evaluation. +float evaluate(string tokens){ + int i; + + // stack to store integer values. + stack values; + + // stack to store operators. + stack ops; + + for(i = 0; i < tokens.length(); i++){ + + // Current token is a whitespace, + // skip it. + if(tokens[i] == ' ') + continue; + + // Current token is an opening + // brace, push it to 'ops' + else if(tokens[i] == '('){ + ops.push(tokens[i]); + } + + // Current token is a number, push + // it to stack for numbers. + else if(isdigit(tokens[i]) ){ + float val = 0.0f; + std::string tokenVal = ""; + // There may be more than one + // digits in number. + while(i < tokens.length() && (isdigit(tokens[i]) || tokens[i] == '.')) + { + tokenVal += tokens[i]; + + i++; + } + val = std::stof(tokenVal); + values.push(val); + + // right now the i points to + // the character next to the digit, + // since the for loop also increases + // the i, we would skip one + // token position; we need to + // decrease the value of i by 1 to + // correct the offset. + i--; + } + + // Closing brace encountered, solve + // entire brace. + else if(tokens[i] == ')') + { + while(!ops.empty() && ops.top() != '(') + { + float val2 = values.top(); + values.pop(); + + float val1 = values.top(); + values.pop(); + + char op = ops.top(); + ops.pop(); + + values.push(applyOp(val1, val2, op)); + } + + // pop opening brace. + if(!ops.empty()) + ops.pop(); + } + + // Current token is an operator. + else + { + // While top of 'ops' has same or greater + // precedence to current token, which + // is an operator. Apply operator on top + // of 'ops' to top two elements in values stack. + while(!ops.empty() && precedence(ops.top()) + >= precedence(tokens[i])){ + float val2 = values.top(); + values.pop(); + + float val1 = values.top(); + values.pop(); + + char op = ops.top(); + ops.pop(); + + values.push(applyOp(val1, val2, op)); + } + + // Push current token to 'ops'. + ops.push(tokens[i]); + } + } + + // Entire expression has been parsed at this + // point, apply remaining ops to remaining + // values. + while(!ops.empty()){ + float val2 = values.top(); + values.pop(); + + float val1 = values.top(); + values.pop(); + + char op = ops.top(); + ops.pop(); + + values.push(applyOp(val1, val2, op)); + } + + // Top of 'values' contains result, return it. + return values.top(); +} + + +// + + void draw() override { + + // Put code here for drawing! + int32_t middle_x = pos_x + (width / 2); //posx => parameter in Widget + int32_t middle_y = pos_y ; + //+ (height / 2) + mousePoint = GetMousePosition(); + //checks if mouse is over the calc widget , the if block can be removed if the calc needs to be static + if (CheckCollisionPointRec(mousePoint, (Rectangle){pos_x, pos_y,CALC_W, CALC_H }) ){ + + int textWidth = MeasureText(text.c_str(), 30); + + uint32_t x_cord = middle_x - (textWidth / 2); + DrawText(text.c_str(), x_cord, middle_y + 20, 30, SKYBLUE); + + // DrawRectangleLines(x_cord, middle_y + 20, textWidth, 50, SKYBLUE); + Rectangle button_op1 = {x_cord , middle_y + 80, 50, 50}; + + + //column 1 + spawnButton(offset +pos_x, middle_y + 100 , 50,50, "1"); + spawnButton(offset +pos_x, middle_y + 150 , 50,50, "4"); + spawnButton(offset +pos_x, middle_y + 200 , 50,50, "7"); + spawnButton(offset +pos_x, middle_y + 250 , 50,50, "+"); + spawnButton(offset +pos_x, middle_y + 300 , 50,50, "-"); + spawnButton(offset +pos_x, middle_y + 350 , 50,50, "^"); + spawnButton(offset +pos_x, middle_y + 400 , 50,50, "CE"); + + //column 2 + spawnButton(offset +pos_x + 50, middle_y + 100 , 50,50, "2"); + spawnButton(offset +pos_x + 50, middle_y + 150 , 50,50, "5"); + spawnButton(offset +pos_x + 50, middle_y + 200 , 50,50, "8"); + spawnButton(offset +pos_x + 50, middle_y + 250 , 50,50, "0"); + spawnButton(offset +pos_x + 50, middle_y + 300 , 50,50, "/"); + spawnButton(offset +pos_x + 50, middle_y + 350 , 50,50, "*"); + spawnButton(offset +pos_x + 50, middle_y + 400 , 50,50, "C"); + + //column 3 + spawnButton(offset +pos_x + 100, middle_y + 100 , 50,50, "3"); + spawnButton(offset +pos_x + 100, middle_y + 150 , 50,50, "6"); + spawnButton(offset +pos_x + 100, middle_y + 200 , 50,50, "9"); + spawnButton(offset +pos_x + 100, middle_y + 250 , 50,50, "("); + spawnButton(offset +pos_x + 100, middle_y + 300 , 50,50, ")"); + spawnButton(offset +pos_x + 100, middle_y + 350 , 50,50, "."); + spawnButton(offset +pos_x + 100, middle_y + 400 , 50,50, "="); + + //****************************hover************************* + + //column 1 + if(buttonInteract(offset +pos_x, middle_y + 100 , 50,50, "1")){text += "1";} + if(buttonInteract(offset +pos_x, middle_y + 150 , 50,50, "4")){text += "4";} + if(buttonInteract(offset +pos_x, middle_y + 200 , 50,50, "7")){text += "7";} + + if(buttonInteract(offset +pos_x, middle_y + 250 , 50,50, "+") && (isalnum(text.c_str()[text.length()-1]) || text.c_str()[text.length()-1] == ')' ) ) + { + text += "+"; + } + if(buttonInteract(offset +pos_x, middle_y + 300 , 50,50, "-") && (isalnum(text.c_str()[text.length()-1]) || text.c_str()[text.length()-1] == ')' )) + {text += "-";} + if(buttonInteract(offset +pos_x, middle_y + 350 , 50,50, "^") && (isalnum(text.c_str()[text.length()-1]) || text.c_str()[text.length()-1] == ')' )) + {text += "^";} + if(buttonInteract(offset +pos_x, middle_y + 400 , 50,50, "CE")){text = "";} + + //column 2 + if(buttonInteract(offset +pos_x + 50, middle_y + 100 , 50,50, "2")){text += "2";} + if(buttonInteract(offset +pos_x + 50, middle_y + 150 , 50,50, "5")){text += "5";} + if(buttonInteract(offset +pos_x + 50, middle_y + 200 , 50,50, "8")){text += "8";} + + if(buttonInteract(offset +pos_x + 50, middle_y + 250 , 50,50, "0")){text += "0";} + if(buttonInteract(offset +pos_x + 50, middle_y + 300 , 50,50, "/") && (isalnum(text.c_str()[text.length()-1]) || text.c_str()[text.length()-1] == ')' )) + {text += "/";} + if(buttonInteract(offset +pos_x + 50, middle_y + 350 , 50,50, "*" ) && isalnum(text.c_str()[text.length()-1])) + {text += "*";} + if(buttonInteract(offset +pos_x + 50, middle_y + 400 , 50,50, "C")){text = text.substr(0, text.length() -1);} + + //column 3 + if(buttonInteract(offset +pos_x + 100, middle_y + 100 , 50,50, "3")){text += "3";} + if(buttonInteract(offset +pos_x + 100, middle_y + 150 , 50,50, "6")){text += "6";} + if(buttonInteract(offset +pos_x + 100, middle_y + 200 , 50,50, "9")){text += "9";} + + if(buttonInteract(offset +pos_x + 100, middle_y + 250 , 50,50, "(") && !isalnum(text.c_str()[text.length()-1])){text += "(";} + if(buttonInteract(offset +pos_x + 100, middle_y + 300 , 50,50, ")") && isalnum(text.c_str()[text.length()-1])){text += ")";} + if(buttonInteract(offset +pos_x + 100, middle_y + 350 , 50,50, ".") && isalnum(text.c_str()[text.length()-1])){text += ".";} + if(buttonInteract(offset +pos_x + 100, middle_y + 400 , 50,50, "=")){ + + float resultEval = evaluate(text); + //resultEval = round(100 * resultEval)/100.0; + int int_resultEval = round(resultEval * 100); + std::string str_resultEval = std::to_string(int_resultEval); + text = str_resultEval.substr(0, str_resultEval.length() - 2) + "." + str_resultEval.substr( str_resultEval.length() - 2, str_resultEval.length() - 1); + } + + } + + else{ + int hoverTextWidth; + std::string text_display = "CALCULATOR"; + hoverTextWidth = MeasureText(text_display.c_str(), 20); + uint32_t x_cord = middle_x - (hoverTextWidth / 2); + DrawText(text_display.c_str(), x_cord + 10, middle_y + 40, 20, SKYBLUE); + + DrawRectangleLines(x_cord, middle_y + 20, hoverTextWidth + 20, 50, SKYBLUE); + + } + } + +private: + +}; + +#endif \ No newline at end of file diff --git a/inc/Constants.h b/inc/Constants.h index 2b8a6b0..789bacc 100644 --- a/inc/Constants.h +++ b/inc/Constants.h @@ -31,6 +31,12 @@ #define POLY_MIN -180 #define POLY_SCALE POLY_MAX - POLY_MIN +//Calcuator widget Constants +#define CALC_W 200 +#define CALC_H 500 +#define CALC_X WEATHER_X +#define CALC_Y WEATHER_Y + WEATHER_H + 50 + // Shader Information #define GLSL_VERSION 100 diff --git a/src/Game.cpp b/src/Game.cpp index 9073d82..da12501 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -7,10 +7,11 @@ #include "TimeWidget.h" #include "WeatherWidget.h" #include "NewsWidget.h" - -Game::Game(int width, int height, std::string title) { - InitWindow(width, height, title.c_str()); - +#include "Calculator.h" +Game::Game(int width, int height, std::string title) //called from main.cpp + { + InitWindow(width, height, title.c_str()); //raylib function + glowing_shader = LoadShader(0, "../resources/shaders/glow.fs"); target = LoadRenderTexture(MONITOR_SIZE_X, MONITOR_SIZE_Y); int target0_loc = GetShaderLocation(glowing_shader, "texture0"); @@ -19,9 +20,11 @@ Game::Game(int width, int height, std::string title) { TimeWidget* timeWidget = new TimeWidget(TIME_X, TIME_Y, TIME_W, TIME_H, "Time Widget"); NewsWidget* newsWidget = new NewsWidget(NEWS_X, NEWS_Y, NEWS_W, NEWS_H, "News Widget"); WeatherWidget* weatherWidget = new WeatherWidget(WEATHER_X, WEATHER_Y, WEATHER_W, WEATHER_H, "Weather Widget"); + Calculator * calculator = new Calculator(CALC_X, CALC_Y , CALC_W, CALC_H, "Calculator"); widgets.push_back(timeWidget); widgets.push_back(newsWidget); widgets.push_back(weatherWidget); + widgets.push_back(calculator); } Game::~Game() noexcept { From 9ad05dd20b364756c3c05e2a22416dc785f3244a Mon Sep 17 00:00:00 2001 From: Tanyk2004 Date: Mon, 13 Jun 2022 23:37:02 +0530 Subject: [PATCH 2/5] improved code readability --- inc/Calculator.h | 175 ++++++++++++++--------------------------------- 1 file changed, 52 insertions(+), 123 deletions(-) diff --git a/inc/Calculator.h b/inc/Calculator.h index 03b3365..91eec24 100644 --- a/inc/Calculator.h +++ b/inc/Calculator.h @@ -16,36 +16,38 @@ using namespace std; class Calculator : public Widget { -std::string text; -Vector2 mousePoint; -Texture2D plusButton; -bool Button1Pressed; -int offset; -int lastOperatorIndex; - -stack operators; -stack operands; +private: + std::string text; + Vector2 mousePoint; + Texture2D plusButton; + bool Button1Pressed; + int offset; + int lastOperatorIndex; + stack operators; + stack operands; public: Calculator(uint32_t x, uint32_t y, uint32_t w, uint32_t h, std::string n) : Widget (x, y, w, h, n) { // Calls widget's constructor //render = false; text = ""; - mousePoint = {0.0f, 0.0f}; - Image plusIcon = LoadImage("../resources/images/plus_operator.png"); - ImageResize(&plusIcon, 50, 50); + mousePoint = { 0.0f , 0.0f }; + Image plusIcon = LoadImage( "../resources/images/plus_operator.png" ); + ImageResize( &plusIcon, 50, 50); plusButton = LoadTextureFromImage(plusIcon); UnloadImage(plusIcon); Button1Pressed = false; offset = 25; } +private: void spawnButton(int x, int y, int w,int h, std::string buttonText) { DrawRectangleLines(x,y,w,h,SKYBLUE); int buttonOffset = (50 - MeasureText(buttonText.c_str(),30))/2; DrawText(buttonText.c_str(), buttonOffset+x, 10 + y, 30, SKYBLUE); } +private: bool buttonInteract(int x, int y, int w,int h, std::string buttonText){ if (CheckCollisionPointRec(mousePoint, (Rectangle){x,y,w,h})){ DrawRectangle(x,y,w,h,SKYBLUE); @@ -59,97 +61,18 @@ stack operands; } - /*int prec(char ch) { - if (ch == '^') - return 3; - else if (ch == '/' || ch == '*') - return 2; - else if (ch == '+' || ch == '-') - return 1; - else - return -1; -} -int applyOp(int b, int a, char op){ - switch(op){ - case '+': return a + b; - case '-': return b - a; - case '*': return a * b; - case '/': return b / a; - case '^': return pow(b, a); - } -} -float evaluateExpression(std::string expressionText) -{ - std::string lastOperand = ""; - float ans = 0.0f; - for( int i = 0; i <= expressionText.length(); i++){ - if(isalnum(expressionText[i])){ - lastOperand += expressionText[i]; - } - else{ - if (lastOperand.length() > 0){ - operands.push(std::stof(lastOperand)); - lastOperand = "";} - - if ( expressionText[i] == '('){ - operators.push(expressionText[i]); - } - else if(expressionText[i] == ')'){ - while (!operators.empty() && operators.top () != '('){ - int val2 = operands.top(); - operands.pop(); - int val1 = operands.top(); - operands.pop(); - char op = operators.top(); - operators.pop(); - - operands.push(applyOp(val1, val2, op)); - } - - if ( !operators.empty()){ - operators.pop(); - } - } - else{ - while (!operators.empty() && prec(operators.top()) >= prec(expressionText[i])){ - int val2 = operands.top(); - operands.pop(); - int val1 = operands.top(); - operands.pop(); - char op = operators.top(); - operators.pop(); - - operands.push(applyOp(val1, val2, op)); - } - operators.push(expressionText[i]); - } - } - } - while (!operands.empty()){ - int val2 = operands.top(); - operands.pop(); - int val1 = operands.top(); - operands.pop(); - - char op = operators.top(); - operators.pop(); - operands.push(applyOp(val1, val2, op)); - } - return operands.top(); -}*/ +private: int precedence(char op){ - if(op == '+'||op == '-') - return 1; - if(op == '*'||op == '/') - return 2; - if(op == '^') - return 3; + if(op == '+'||op == '-') return 1; + if(op == '*'||op == '/') return 2; + if(op == '^') return 3; return 0; } // Function to perform arithmetic operations. -float applyOp(float a, float b, char op){ +private: + float applyOp(float a, float b, char op){ switch(op){ case '+': return a + b; case '-': return a - b; @@ -161,6 +84,7 @@ float applyOp(float a, float b, char op){ // Function that returns value of // expression after evaluation. +private: float evaluate(string tokens){ int i; @@ -280,16 +204,17 @@ float evaluate(string tokens){ // - +private: void draw() override { // Put code here for drawing! int32_t middle_x = pos_x + (width / 2); //posx => parameter in Widget int32_t middle_y = pos_y ; - //+ (height / 2) + mousePoint = GetMousePosition(); + //checks if mouse is over the calc widget , the if block can be removed if the calc needs to be static - if (CheckCollisionPointRec(mousePoint, (Rectangle){pos_x, pos_y,CALC_W, CALC_H }) ){ + if (CheckCollisionPointRec(mousePoint, (Rectangle){pos_x, pos_y, CALC_W, CALC_H }) ){ int textWidth = MeasureText(text.c_str(), 30); @@ -333,52 +258,56 @@ float evaluate(string tokens){ if(buttonInteract(offset +pos_x, middle_y + 100 , 50,50, "1")){text += "1";} if(buttonInteract(offset +pos_x, middle_y + 150 , 50,50, "4")){text += "4";} if(buttonInteract(offset +pos_x, middle_y + 200 , 50,50, "7")){text += "7";} - if(buttonInteract(offset +pos_x, middle_y + 250 , 50,50, "+") && (isalnum(text.c_str()[text.length()-1]) || text.c_str()[text.length()-1] == ')' ) ) - { - text += "+"; - } + { text += "+"; } if(buttonInteract(offset +pos_x, middle_y + 300 , 50,50, "-") && (isalnum(text.c_str()[text.length()-1]) || text.c_str()[text.length()-1] == ')' )) - {text += "-";} - if(buttonInteract(offset +pos_x, middle_y + 350 , 50,50, "^") && (isalnum(text.c_str()[text.length()-1]) || text.c_str()[text.length()-1] == ')' )) - {text += "^";} - if(buttonInteract(offset +pos_x, middle_y + 400 , 50,50, "CE")){text = "";} + { text += "-"; } + if(buttonInteract(offset +pos_x, middle_y + 350 , 50,50, "^") && (isalnum(text.c_str()[text.length()-1]) || + text.c_str()[text.length()-1] == ')' )) + { text += "^"; } + if(buttonInteract(offset +pos_x, middle_y + 400 , 50,50, "CE")) + { text = ""; } //column 2 if(buttonInteract(offset +pos_x + 50, middle_y + 100 , 50,50, "2")){text += "2";} if(buttonInteract(offset +pos_x + 50, middle_y + 150 , 50,50, "5")){text += "5";} if(buttonInteract(offset +pos_x + 50, middle_y + 200 , 50,50, "8")){text += "8";} - if(buttonInteract(offset +pos_x + 50, middle_y + 250 , 50,50, "0")){text += "0";} if(buttonInteract(offset +pos_x + 50, middle_y + 300 , 50,50, "/") && (isalnum(text.c_str()[text.length()-1]) || text.c_str()[text.length()-1] == ')' )) - {text += "/";} + { text += "/"; } if(buttonInteract(offset +pos_x + 50, middle_y + 350 , 50,50, "*" ) && isalnum(text.c_str()[text.length()-1])) - {text += "*";} - if(buttonInteract(offset +pos_x + 50, middle_y + 400 , 50,50, "C")){text = text.substr(0, text.length() -1);} + { text += "*"; } + if(buttonInteract(offset +pos_x + 50, middle_y + 400 , 50,50, "C")) + {text = text.substr(0, text.length() -1);} //column 3 if(buttonInteract(offset +pos_x + 100, middle_y + 100 , 50,50, "3")){text += "3";} if(buttonInteract(offset +pos_x + 100, middle_y + 150 , 50,50, "6")){text += "6";} if(buttonInteract(offset +pos_x + 100, middle_y + 200 , 50,50, "9")){text += "9";} - if(buttonInteract(offset +pos_x + 100, middle_y + 250 , 50,50, "(") && !isalnum(text.c_str()[text.length()-1])){text += "(";} - if(buttonInteract(offset +pos_x + 100, middle_y + 300 , 50,50, ")") && isalnum(text.c_str()[text.length()-1])){text += ")";} - if(buttonInteract(offset +pos_x + 100, middle_y + 350 , 50,50, ".") && isalnum(text.c_str()[text.length()-1])){text += ".";} + if(buttonInteract(offset +pos_x + 100, middle_y + 250 , 50,50, "(") && + !isalnum(text.c_str()[text.length()-1])) + { text += "("; } + if(buttonInteract(offset +pos_x + 100, middle_y + 300 , 50,50, ")") && + isalnum(text.c_str()[text.length()-1])) + { text += ")"; } + if(buttonInteract(offset +pos_x + 100, middle_y + 350 , 50,50, ".") && + isalnum(text.c_str()[text.length()-1])) + { text += "."; } if(buttonInteract(offset +pos_x + 100, middle_y + 400 , 50,50, "=")){ float resultEval = evaluate(text); //resultEval = round(100 * resultEval)/100.0; int int_resultEval = round(resultEval * 100); std::string str_resultEval = std::to_string(int_resultEval); - text = str_resultEval.substr(0, str_resultEval.length() - 2) + "." + str_resultEval.substr( str_resultEval.length() - 2, str_resultEval.length() - 1); - } + text = str_resultEval.substr(0, str_resultEval.length() - 2) + "." + + str_resultEval.substr( str_resultEval.length() - 2, str_resultEval.length() - 1); - } - - else{ - int hoverTextWidth; - std::string text_display = "CALCULATOR"; - hoverTextWidth = MeasureText(text_display.c_str(), 20); + }} + else{ + int hoverTextWidth; + std::string text_display = "CALCULATOR"; + hoverTextWidth = MeasureText(text_display.c_str(), 20); uint32_t x_cord = middle_x - (hoverTextWidth / 2); DrawText(text_display.c_str(), x_cord + 10, middle_y + 40, 20, SKYBLUE); From 1c1847d0e6cf996cf275057de350e9cfe079f60a Mon Sep 17 00:00:00 2001 From: Tanyk2004 Date: Mon, 13 Jun 2022 23:45:04 +0530 Subject: [PATCH 3/5] fixed curly braces spacing] --- inc/Calculator.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/inc/Calculator.h b/inc/Calculator.h index 91eec24..fca8b18 100644 --- a/inc/Calculator.h +++ b/inc/Calculator.h @@ -41,21 +41,23 @@ class Calculator : public Widget { } private: - void spawnButton(int x, int y, int w,int h, std::string buttonText) - { + void spawnButton(int x, int y, int w,int h, std::string buttonText){ DrawRectangleLines(x,y,w,h,SKYBLUE); int buttonOffset = (50 - MeasureText(buttonText.c_str(),30))/2; DrawText(buttonText.c_str(), buttonOffset+x, 10 + y, 30, SKYBLUE); } private: bool buttonInteract(int x, int y, int w,int h, std::string buttonText){ + if (CheckCollisionPointRec(mousePoint, (Rectangle){x,y,w,h})){ - DrawRectangle(x,y,w,h,SKYBLUE); - int buttonOffset = (50 - MeasureText(buttonText.c_str(),30))/2; - DrawText(buttonText.c_str(), buttonOffset+x, 10 + y, 30, BLACK); - if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) return true; + DrawRectangle(x,y,w,h,SKYBLUE); + int buttonOffset = (50 - MeasureText(buttonText.c_str(),30))/2; + DrawText(buttonText.c_str(), buttonOffset+x, 10 + y, 30, BLACK); + + if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) return true; else return false; + } From 6a93c5df9a8d97defa02ab8adb6ed2f8b3b65339 Mon Sep 17 00:00:00 2001 From: Tanay Garg Date: Fri, 17 Jun 2022 10:07:31 +0530 Subject: [PATCH 4/5] Converted switch case to if blocks --- inc/Calculator.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/inc/Calculator.h b/inc/Calculator.h index fca8b18..413352d 100644 --- a/inc/Calculator.h +++ b/inc/Calculator.h @@ -75,13 +75,13 @@ int precedence(char op){ // Function to perform arithmetic operations. private: float applyOp(float a, float b, char op){ - switch(op){ - case '+': return a + b; - case '-': return a - b; - case '*': return a * b; - case '/': return a / b; - case '^': return pow(a,b); - } + + if ( op == '+') return a+b; + else if ( op == '-') return a-b; + else if (op == '*') return a*b; + else if (op == '/') return a/b; + else if (op == '^') return pow(a,b); + } // Function that returns value of From 283f2f490a1de595a79da78b0f3b13dbe3a82d8a Mon Sep 17 00:00:00 2001 From: Tanay Garg Date: Fri, 24 Jun 2022 15:26:10 +0530 Subject: [PATCH 5/5] Calculator and weather widget --- inc/Calculator.h | 15 ++++++++++----- inc/WeatherWidget.h | 23 ++++++++++++++++++----- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/inc/Calculator.h b/inc/Calculator.h index 413352d..2c2366c 100644 --- a/inc/Calculator.h +++ b/inc/Calculator.h @@ -211,11 +211,11 @@ float evaluate(string tokens){ // Put code here for drawing! int32_t middle_x = pos_x + (width / 2); //posx => parameter in Widget - int32_t middle_y = pos_y ; + int32_t middle_y = pos_y ; mousePoint = GetMousePosition(); - //checks if mouse is over the calc widget , the if block can be removed if the calc needs to be static + //checks if mouse is over the caluclator widget , the if block can be removed if the calc needs to be static if (CheckCollisionPointRec(mousePoint, (Rectangle){pos_x, pos_y, CALC_W, CALC_H }) ){ int textWidth = MeasureText(text.c_str(), 30); @@ -223,7 +223,7 @@ float evaluate(string tokens){ uint32_t x_cord = middle_x - (textWidth / 2); DrawText(text.c_str(), x_cord, middle_y + 20, 30, SKYBLUE); - // DrawRectangleLines(x_cord, middle_y + 20, textWidth, 50, SKYBLUE); + Rectangle button_op1 = {x_cord , middle_y + 80, 50, 50}; @@ -299,18 +299,23 @@ float evaluate(string tokens){ if(buttonInteract(offset +pos_x + 100, middle_y + 400 , 50,50, "=")){ float resultEval = evaluate(text); - //resultEval = round(100 * resultEval)/100.0; + int int_resultEval = round(resultEval * 100); std::string str_resultEval = std::to_string(int_resultEval); text = str_resultEval.substr(0, str_resultEval.length() - 2) + "." + str_resultEval.substr( str_resultEval.length() - 2, str_resultEval.length() - 1); - }} + } + } else{ + int hoverTextWidth; std::string text_display = "CALCULATOR"; + hoverTextWidth = MeasureText(text_display.c_str(), 20); uint32_t x_cord = middle_x - (hoverTextWidth / 2); + + DrawText(text_display.c_str(), x_cord + 10, middle_y + 40, 20, SKYBLUE); DrawRectangleLines(x_cord, middle_y + 20, hoverTextWidth + 20, 50, SKYBLUE); diff --git a/inc/WeatherWidget.h b/inc/WeatherWidget.h index bae6b0c..1ffd659 100644 --- a/inc/WeatherWidget.h +++ b/inc/WeatherWidget.h @@ -20,6 +20,7 @@ class WeatherWidget : public Widget { Widget (x, y, w, h, n) { Image weatherIcons = LoadImage("../resources/Adjusted-Weather-Icons.png"); + ImageResize(&weatherIcons, IMAGE_SIZE_X, IMAGE_SIZE_Y); icons = LoadTextureFromImage(weatherIcons); UnloadImage(weatherIcons); @@ -57,14 +58,26 @@ class WeatherWidget : public Widget { temperature = "Loading..."; weatherConditions = "Loading..."; } - // Obtains rectangle for weather icon - Rectangle rect = iconMap["Clear"]; + + + // Obtains rectangle for weather icon + // so far doesn't change icon with weather conditions + + + Rectangle rect = iconMap[weatherConditions.c_str()]; + Rectangle rect2 = iconMap["Thunderstorm"]; // Position for icon on screen Vector2 icon_pos {pos_x, pos_y + 30}; - DrawTextureRec(icons, rect, icon_pos, WHITE); - DrawText(temperature.c_str(), pos_x + 80, pos_y + 60, 30, SKYBLUE); - DrawText(weatherConditions.c_str(), pos_x, pos_y + 100, 30, SKYBLUE); + //Renders the final weather report + DrawTextureRec(icons, rect, icon_pos, WHITE); + //renders another icon on top of the actual icon ******test***** + BeginBlendMode(1); + // Draw here for + // DrawTextureRec(icons, rect2, icon_pos, WHITE ); + EndBlendMode(); + DrawText(temperature.c_str(), pos_x + ICON_SIZE_X , pos_y + 60, 30, SKYBLUE); + DrawText(weatherConditions.c_str(), pos_x, pos_y+ ICON_SIZE_Y + 20, 30, SKYBLUE); DrawText(city.c_str(), pos_x, pos_y, 30, SKYBLUE); }