Skip to content

Commit 2d16472

Browse files
committed
Calculator tab is now functional!
1 parent 0073030 commit 2d16472

File tree

10 files changed

+216
-3
lines changed

10 files changed

+216
-3
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE
6565
CFLAGS := -g -Wall -O2 -ffunction-sections \
6666
$(ARCH) $(DEFINES)
6767

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

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

include/aboutTab.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <borealis.hpp>
4+
5+
struct AboutTab : public brls::Box
6+
{
7+
AboutTab();
8+
9+
static brls::View* create();
10+
11+
private:
12+
BRLS_BIND(brls::Label, versionLabel, "Version Label");
13+
};

include/calculatorTab.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,13 @@ struct CalculatorTab : public brls::Box
99
static brls::View* create();
1010

1111
private:
12+
void onButtonPressed(char c);
13+
bool onEqualButtonPressed(brls::View *view);
14+
bool onDeleteButtonPressed(brls::View *view);
15+
16+
void updateScreenBufferFromExpStr();
17+
void updateScreen();
18+
19+
BRLS_BIND(brls::Label, screenLabel, "screen");
20+
std::string expressionStr = "", screenBuffer;
1221
};

resources/i18n/en-US/text.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,10 @@
99

1010
"calculator": {
1111
"expression_enter": "Enter an expression"
12+
},
13+
14+
"about": {
15+
"appname": "Calculator_NX",
16+
"developer": "Developed by EmreTech"
1217
}
1318
}

resources/xml/activity/main.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
<brls:Separator/>
1313

14-
<brls:Tab label="@i18n/text/tabs/about"/>
14+
<brls:Tab label="@i18n/text/tabs/about">
15+
<AboutTab />
16+
</brls:Tab>
1517

1618
</brls:TabFrame>

resources/xml/tabs/about.xml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<brls:Box
2+
width="auto"
3+
height="auto"
4+
alignItems="center"
5+
justifyContent="center">
6+
7+
<brls:Box
8+
width="auto"
9+
height="auto"
10+
axis="column"
11+
paddingLeft="@style/brls/tab_frame/content_padding_sides"
12+
paddingRight="@style/brls/tab_frame/content_padding_sides"
13+
paddingTop="@style/brls/tab_frame/content_padding_top_bottom"
14+
paddingBottom="@style/brls/tab_frame/content_padding_top_bottom">
15+
16+
<brls:Image
17+
width="auto"
18+
height="auto"
19+
image="@res/Calculator_NX.jpg"
20+
marginBottom="20px"/>
21+
22+
<brls:Box
23+
width="auto"
24+
height="auto"
25+
axis="column"
26+
justifyContent="spaceEvenly">
27+
28+
<brls:Label
29+
width="auto"
30+
height="auto"
31+
horizontalAlign="center"
32+
fontSize="40"
33+
marginBottom="10px"
34+
text="@i18n/text/about/appname"/>
35+
36+
<brls:Label
37+
id="Version Label"
38+
width="auto"
39+
height="auto"
40+
horizontalAlign="center"
41+
fontSize="15"
42+
text="Unknown"/>
43+
44+
<brls:Label
45+
width="auto"
46+
height="auto"
47+
horizontalAlign="center"
48+
fontSize="20"
49+
text="@i18n/text/about/developer"/>
50+
51+
</brls:Box>
52+
53+
</brls:Box>
54+
55+
</brls:Box>

resources/xml/tabs/calculator.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
height="20%">
1414

1515
<brls:Label
16+
id="screen"
1617
width="auto"
1718
height="auto"
1819
text="@i18n/text/calculator/expression_enter"
@@ -30,17 +31,26 @@
3031
axis="column">
3132

3233
<brls:Button
34+
id="(Button"
3335
width="auto"
3436
height="auto"
3537
style="primary"
3638
text="("/>
3739

3840
<brls:Button
41+
id=")Button"
3942
width="auto"
4043
height="auto"
4144
style="primary"
4245
text=")"/>
4346

47+
<brls:Button
48+
id="DelButton"
49+
width="auto"
50+
height="auto"
51+
style="primary"
52+
text="DEL"/>
53+
4454
</brls:Box>
4555

4656
<brls:Box
@@ -49,24 +59,28 @@
4959
axis="column">
5060

5161
<brls:Button
62+
id="9Button"
5263
width="auto"
5364
height="auto"
5465
style="primary"
5566
text="9"/>
5667

5768
<brls:Button
69+
id="6Button"
5870
width="auto"
5971
height="auto"
6072
style="primary"
6173
text="6"/>
6274

6375
<brls:Button
76+
id="3Button"
6477
width="auto"
6578
height="auto"
6679
style="primary"
6780
text="3"/>
6881

6982
<brls:Button
83+
id="0Button"
7084
width="auto"
7185
height="auto"
7286
style="primary"
@@ -80,24 +94,28 @@
8094
axis="column">
8195

8296
<brls:Button
97+
id="8Button"
8398
width="auto"
8499
height="auto"
85100
style="primary"
86101
text="8"/>
87102

88103
<brls:Button
104+
id="5Button"
89105
width="auto"
90106
height="auto"
91107
style="primary"
92108
text="5"/>
93109

94110
<brls:Button
111+
id="2Button"
95112
width="auto"
96113
height="auto"
97114
style="primary"
98115
text="2"/>
99116

100117
<brls:Button
118+
id="DotButton"
101119
width="auto"
102120
height="auto"
103121
style="primary"
@@ -111,18 +129,21 @@
111129
axis="column">
112130

113131
<brls:Button
132+
id="7Button"
114133
width="auto"
115134
height="auto"
116135
style="primary"
117136
text="7"/>
118137

119138
<brls:Button
139+
id="4Button"
120140
width="auto"
121141
height="auto"
122142
style="primary"
123143
text="4"/>
124144

125145
<brls:Button
146+
id="1Button"
126147
width="auto"
127148
height="auto"
128149
style="primary"
@@ -141,30 +162,35 @@
141162
axis="column">
142163

143164
<brls:Button
165+
id="PlusButton"
144166
width="auto"
145167
height="auto"
146168
style="primary"
147169
text="+"/>
148170

149171
<brls:Button
172+
id="MinusButton"
150173
width="auto"
151174
height="auto"
152175
style="primary"
153176
text="-"/>
154177

155178
<brls:Button
179+
id="MultiplyButton"
156180
width="auto"
157181
height="auto"
158182
style="primary"
159183
text="*"/>
160184

161185
<brls:Button
186+
id="DivideButton"
162187
width="auto"
163188
height="auto"
164189
style="primary"
165190
text="/"/>
166191

167192
<brls:Button
193+
id="EqualButton"
168194
width="auto"
169195
height="auto"
170196
style="primary"

source/aboutTab.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <aboutTab.hpp>
2+
3+
4+
AboutTab::AboutTab()
5+
{
6+
this->inflateFromXMLRes("xml/tabs/about.xml");
7+
versionLabel->setText(VERSION_NUM + std::string(" | ") + STABLE);
8+
}
9+
10+
brls::View *AboutTab::create()
11+
{
12+
return new AboutTab();
13+
}

source/calculatorTab.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,95 @@
11
#include <calculatorTab.hpp>
22

3+
#include <vector>
4+
#include <utility>
5+
6+
#include <calculator.hpp>
7+
8+
std::vector<std::pair<std::string,char>> allButtons {
9+
std::make_pair("(Button", '('),
10+
std::make_pair(")Button", ')'),
11+
std::make_pair("9Button", '9'),
12+
std::make_pair("6Button", '6'),
13+
std::make_pair("3Button", '3'),
14+
std::make_pair("0Button", '0'),
15+
std::make_pair("8Button", '8'),
16+
std::make_pair("5Button", '5'),
17+
std::make_pair("2Button", '2'),
18+
std::make_pair("DotButton", '.'),
19+
std::make_pair("7Button", '7'),
20+
std::make_pair("4Button", '4'),
21+
std::make_pair("1Button", '1'),
22+
std::make_pair("PlusButton", '+'),
23+
std::make_pair("MinusButton", '-'),
24+
std::make_pair("MultiplyButton", '*'),
25+
std::make_pair("DivideButton", '/'),
26+
};
27+
328
CalculatorTab::CalculatorTab()
429
{
530
this->inflateFromXMLRes("xml/tabs/calculator.xml");
31+
32+
for (auto &e : allButtons)
33+
{
34+
char c = e.second;
35+
36+
this->getView(e.first)->registerAction(
37+
"", brls::BUTTON_A,
38+
[this, c](brls::View *view) {
39+
onButtonPressed(c);
40+
return true;
41+
},
42+
false, brls::SOUND_CLICK
43+
);
44+
}
45+
46+
BRLS_REGISTER_CLICK_BY_ID("EqualButton", this->onEqualButtonPressed);
47+
BRLS_REGISTER_CLICK_BY_ID("DelButton", this->onDeleteButtonPressed);
648
}
749

850
brls::View* CalculatorTab::create()
951
{
1052
return new CalculatorTab();
53+
}
54+
55+
void CalculatorTab::onButtonPressed(char c)
56+
{
57+
expressionStr += c;
58+
updateScreenBufferFromExpStr();
59+
}
60+
61+
bool CalculatorTab::onEqualButtonPressed(brls::View *view)
62+
{
63+
if (expressionStr != "")
64+
{
65+
double result = Calculator::getInstance().evaluateExpression(expressionStr);
66+
screenBuffer += " = " + std::to_string(result);
67+
updateScreen();
68+
69+
expressionStr = "";
70+
}
71+
return true;
72+
}
73+
74+
bool CalculatorTab::onDeleteButtonPressed(brls::View *view)
75+
{
76+
if (expressionStr != "")
77+
{
78+
expressionStr.pop_back();
79+
updateScreenBufferFromExpStr();
80+
}
81+
82+
return true;
83+
}
84+
85+
void CalculatorTab::updateScreen()
86+
{
87+
screenLabel->setText(screenBuffer);
88+
}
89+
90+
void CalculatorTab::updateScreenBufferFromExpStr()
91+
{
92+
screenBuffer = expressionStr;
93+
if (expressionStr.length() >= 30)
94+
screenBuffer = expressionStr.substr(29);
1195
}

0 commit comments

Comments
 (0)