Skip to content

Commit

Permalink
Calculator tab is now functional!
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmmaTech committed Jul 24, 2021
1 parent 0073030 commit 2d16472
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE
CFLAGS := -g -Wall -O2 -ffunction-sections \
$(ARCH) $(DEFINES)

CFLAGS += $(INCLUDE) -D__SWITCH__
CFLAGS += $(INCLUDE) -D__SWITCH__ -DVERSION_NUM=\"$(VERSION)\" -DSTABLE=\"$(STABLE)\"

CXXFLAGS := $(CFLAGS) -std=c++17 -O2 -Wno-volatile

Expand Down
13 changes: 13 additions & 0 deletions include/aboutTab.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <borealis.hpp>

struct AboutTab : public brls::Box
{
AboutTab();

static brls::View* create();

private:
BRLS_BIND(brls::Label, versionLabel, "Version Label");
};
9 changes: 9 additions & 0 deletions include/calculatorTab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ struct CalculatorTab : public brls::Box
static brls::View* create();

private:
void onButtonPressed(char c);
bool onEqualButtonPressed(brls::View *view);
bool onDeleteButtonPressed(brls::View *view);

void updateScreenBufferFromExpStr();
void updateScreen();

BRLS_BIND(brls::Label, screenLabel, "screen");
std::string expressionStr = "", screenBuffer;
};
5 changes: 5 additions & 0 deletions resources/i18n/en-US/text.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@

"calculator": {
"expression_enter": "Enter an expression"
},

"about": {
"appname": "Calculator_NX",
"developer": "Developed by EmreTech"
}
}
4 changes: 3 additions & 1 deletion resources/xml/activity/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

<brls:Separator/>

<brls:Tab label="@i18n/text/tabs/about"/>
<brls:Tab label="@i18n/text/tabs/about">
<AboutTab />
</brls:Tab>

</brls:TabFrame>
55 changes: 55 additions & 0 deletions resources/xml/tabs/about.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<brls:Box
width="auto"
height="auto"
alignItems="center"
justifyContent="center">

<brls:Box
width="auto"
height="auto"
axis="column"
paddingLeft="@style/brls/tab_frame/content_padding_sides"
paddingRight="@style/brls/tab_frame/content_padding_sides"
paddingTop="@style/brls/tab_frame/content_padding_top_bottom"
paddingBottom="@style/brls/tab_frame/content_padding_top_bottom">

<brls:Image
width="auto"
height="auto"
image="@res/Calculator_NX.jpg"
marginBottom="20px"/>

<brls:Box
width="auto"
height="auto"
axis="column"
justifyContent="spaceEvenly">

<brls:Label
width="auto"
height="auto"
horizontalAlign="center"
fontSize="40"
marginBottom="10px"
text="@i18n/text/about/appname"/>

<brls:Label
id="Version Label"
width="auto"
height="auto"
horizontalAlign="center"
fontSize="15"
text="Unknown"/>

<brls:Label
width="auto"
height="auto"
horizontalAlign="center"
fontSize="20"
text="@i18n/text/about/developer"/>

</brls:Box>

</brls:Box>

</brls:Box>
26 changes: 26 additions & 0 deletions resources/xml/tabs/calculator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
height="20%">

<brls:Label
id="screen"
width="auto"
height="auto"
text="@i18n/text/calculator/expression_enter"
Expand All @@ -30,17 +31,26 @@
axis="column">

<brls:Button
id="(Button"
width="auto"
height="auto"
style="primary"
text="("/>

<brls:Button
id=")Button"
width="auto"
height="auto"
style="primary"
text=")"/>

<brls:Button
id="DelButton"
width="auto"
height="auto"
style="primary"
text="DEL"/>

</brls:Box>

<brls:Box
Expand All @@ -49,24 +59,28 @@
axis="column">

<brls:Button
id="9Button"
width="auto"
height="auto"
style="primary"
text="9"/>

<brls:Button
id="6Button"
width="auto"
height="auto"
style="primary"
text="6"/>

<brls:Button
id="3Button"
width="auto"
height="auto"
style="primary"
text="3"/>

<brls:Button
id="0Button"
width="auto"
height="auto"
style="primary"
Expand All @@ -80,24 +94,28 @@
axis="column">

<brls:Button
id="8Button"
width="auto"
height="auto"
style="primary"
text="8"/>

<brls:Button
id="5Button"
width="auto"
height="auto"
style="primary"
text="5"/>

<brls:Button
id="2Button"
width="auto"
height="auto"
style="primary"
text="2"/>

<brls:Button
id="DotButton"
width="auto"
height="auto"
style="primary"
Expand All @@ -111,18 +129,21 @@
axis="column">

<brls:Button
id="7Button"
width="auto"
height="auto"
style="primary"
text="7"/>

<brls:Button
id="4Button"
width="auto"
height="auto"
style="primary"
text="4"/>

<brls:Button
id="1Button"
width="auto"
height="auto"
style="primary"
Expand All @@ -141,30 +162,35 @@
axis="column">

<brls:Button
id="PlusButton"
width="auto"
height="auto"
style="primary"
text="+"/>

<brls:Button
id="MinusButton"
width="auto"
height="auto"
style="primary"
text="-"/>

<brls:Button
id="MultiplyButton"
width="auto"
height="auto"
style="primary"
text="*"/>

<brls:Button
id="DivideButton"
width="auto"
height="auto"
style="primary"
text="/"/>

<brls:Button
id="EqualButton"
width="auto"
height="auto"
style="primary"
Expand Down
13 changes: 13 additions & 0 deletions source/aboutTab.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <aboutTab.hpp>


AboutTab::AboutTab()
{
this->inflateFromXMLRes("xml/tabs/about.xml");
versionLabel->setText(VERSION_NUM + std::string(" | ") + STABLE);
}

brls::View *AboutTab::create()
{
return new AboutTab();
}
84 changes: 84 additions & 0 deletions source/calculatorTab.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,95 @@
#include <calculatorTab.hpp>

#include <vector>
#include <utility>

#include <calculator.hpp>

std::vector<std::pair<std::string,char>> allButtons {
std::make_pair("(Button", '('),
std::make_pair(")Button", ')'),
std::make_pair("9Button", '9'),
std::make_pair("6Button", '6'),
std::make_pair("3Button", '3'),
std::make_pair("0Button", '0'),
std::make_pair("8Button", '8'),
std::make_pair("5Button", '5'),
std::make_pair("2Button", '2'),
std::make_pair("DotButton", '.'),
std::make_pair("7Button", '7'),
std::make_pair("4Button", '4'),
std::make_pair("1Button", '1'),
std::make_pair("PlusButton", '+'),
std::make_pair("MinusButton", '-'),
std::make_pair("MultiplyButton", '*'),
std::make_pair("DivideButton", '/'),
};

CalculatorTab::CalculatorTab()
{
this->inflateFromXMLRes("xml/tabs/calculator.xml");

for (auto &e : allButtons)
{
char c = e.second;

this->getView(e.first)->registerAction(
"", brls::BUTTON_A,
[this, c](brls::View *view) {
onButtonPressed(c);
return true;
},
false, brls::SOUND_CLICK
);
}

BRLS_REGISTER_CLICK_BY_ID("EqualButton", this->onEqualButtonPressed);
BRLS_REGISTER_CLICK_BY_ID("DelButton", this->onDeleteButtonPressed);
}

brls::View* CalculatorTab::create()
{
return new CalculatorTab();
}

void CalculatorTab::onButtonPressed(char c)
{
expressionStr += c;
updateScreenBufferFromExpStr();
}

bool CalculatorTab::onEqualButtonPressed(brls::View *view)
{
if (expressionStr != "")
{
double result = Calculator::getInstance().evaluateExpression(expressionStr);
screenBuffer += " = " + std::to_string(result);
updateScreen();

expressionStr = "";
}
return true;
}

bool CalculatorTab::onDeleteButtonPressed(brls::View *view)
{
if (expressionStr != "")
{
expressionStr.pop_back();
updateScreenBufferFromExpStr();
}

return true;
}

void CalculatorTab::updateScreen()
{
screenLabel->setText(screenBuffer);
}

void CalculatorTab::updateScreenBufferFromExpStr()
{
screenBuffer = expressionStr;
if (expressionStr.length() >= 30)
screenBuffer = expressionStr.substr(29);
}
Loading

0 comments on commit 2d16472

Please sign in to comment.