-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
199 lines (160 loc) · 7.1 KB
/
Copy pathmain.cpp
File metadata and controls
199 lines (160 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <chrono>
#include <thread>
#include <memory>
#include <stdexcept>
#include <unistd.h>
#include <GL/freeglut.h>
#include <Ixtli/Uuid.h>
#include <Ixtli/Graphics/Color.h>
#include <Ixtli/View/ContextProvider.h>
#include <Ixtli/View/MarginLayoutParams.h>
#include <Ixtli/Widget/Toast.h>
#include <Ixtli/Widget/EditText.h>
#include <Ixtli/View/View.h>
#include <Ixtli/View/Window.h>
#include <Ixtli/Events/KeyboardEvent.h>
#include "GrapherView.h"
#include <Ixtli/Widget/LinearLayout.h>
#include <Ixtli/Graphics/Paint.h>
#include <Ixtli/Graphics/Font.h>
//sin(3t)-cos(4t)+3
using namespace std;
using namespace Ixtli;
class GraphWindow : public Window, OnTextChangedListener, OnKeyEventListener {
private:
std::shared_ptr<GrapherView> grapher = nullptr;
UUID input1{};
UUID input2{};
public:
GraphWindow() : Window(){};
~GraphWindow(){};
void onCreate() override{
auto rootLayout = setRootView<View>();
rootLayout->setLayoutParams(MarginLayoutParams(LayoutParams::MATCH_PARENT));
rootLayout->setBackgroundColor(Color::BLACK);
std::cout << "CREATE ROOT LAYOUT " << *rootLayout << std::endl;
grapher = std::make_shared<GrapherView>();
grapher->setLayoutParams(MarginLayoutParams(LayoutParams::MATCH_PARENT, 0, 0, 50, 0));
rootLayout->addView(grapher);
auto separator = std::make_shared<View>();
separator->setLayoutParams(MarginLayoutParams(LayoutParams::MATCH_PARENT, LayoutParams::WRAP_CONTENT, 0, 0, 50, 0));
separator->setHeight(1);
separator->setBackgroundColor(Color::RED);
rootLayout->addView(separator);
auto exprListView = std::make_shared<LinearLayout>(false);
exprListView->setLayoutParams(MarginLayoutParams(LayoutParams::MATCH_PARENT, LayoutParams::WRAP_CONTENT, 0, 0, 51, 0));
exprListView->setHeight(100);
exprListView->setBackgroundColor(Color(0x121212).setAlpha(80));
rootLayout->addView(exprListView);
auto inputText = std::make_shared<EditText>();
inputText->setLayoutParams(MarginLayoutParams(LayoutParams::MATCH_PARENT, LayoutParams::WRAP_CONTENT));
inputText->setHeight(50);
inputText->setBackgroundColor(Color::BLACK);
inputText->setTextColor(Color::WHITE);
inputText->setTextSize(20);
inputText->setOnTextChangedListener(this);
rootLayout->addView(inputText);
input1 = inputText->getID();
//Load font
auto font = std::make_shared<Font>();
// if(!font->loadFromFile("BrownieStencil.ttf")){
if(!font->loadFromFile("NeoEuler.otf")){
std::cerr << "Unable to load font" << std::endl;
}else{
std::cout << "Font loaded successfully" << std::endl;
inputText->getTextPaint().setFont(font);
}
inputText->registerOnKeyEventListener(this);
// inputText = std::make_shared<EditText>();
// inputText->setLayoutParams(MarginLayoutParams(LayoutParams::MATCH_PARENT, LayoutParams::WRAP_CONTENT, 50, 50, 51, 0));
// inputText->setHeight(50);
// inputText->setBackgroundColor(Color(0x121212).setAlpha(80));
// inputText->setTextColor(Color::WHITE);
// inputText->setTextSize(20);
// //inputText->setText("avg(5,6,7)x+1=y+1+avg(x,1,2)");
// inputText->setText("sin(x)=y");
// inputText->setOnTextChangedListener(this);
// rootLayout->addView(inputText);
// input2 = inputText->getID();
auto expr1 = Expression::parseObj("(8/(pi*pi))(sin(pix)-1/9sin(3pix)+1/25sin(5pix)-1/49sin(7pix))");
// auto expr2 = Expression::parseObj(inputText->getText());
// std::cout << "txt: " << expr2->getExpression() << std::endl;
grapher->add(expr1, Color::parse("#9A63EC"), Equation2D::CARTESIAN);
inputText->setText(expr1->getExpression());
// grapher->add(expr2, Color::parse("#62EF6B"), Equation2D::CARTESIAN);
}
Color randomColor(){
return Color::fromARGB(255, rand()%256, rand()%256, rand()%256);
}
bool onKeyEvent(View* v, int key, KeyAction action) override{
if(v->getID() != input1){return false;}
if(key == Key::SHIFT && grapher->size() > 0){
grapher->setPathColor(grapher->size()-1, randomColor());
return true;
}
if(action != KeyAction::KEY_UP){ return false;}
if(key != Key::CARRY_RETURN) {
return false;
}
if(grapher->size() == 0){
return true;
}
auto expr = grapher->getExpression(grapher->size()-1);
if(!expr || !expr->isValid()){
return true;
}
grapher->add(nullptr, randomColor(), Equation2D::CARTESIAN);
auto editText = findViewByID<EditText>(input1);
if(!editText){
return true;
}
editText->setText("");
// grapher->add(Expression::parseObj("0"), Color::parse("#62EF6B"), Equation2D::CARTESIAN);
return true;
}
void onTextChanged(View* v, const std::string& before, const std::string& after) override{
if(v->getID() != input1) return;
if(grapher->size() == 0){
grapher->add(Expression::parseObj("0"), randomColor(), Equation2D::CARTESIAN);
}
auto expr = Expression::parseObj(after);
if(expr->isValid({'x'})){
grapher->setExpression(grapher->size()-1 , expr, Equation2D::CARTESIAN);
// std::cout << "Invalid expression: " << expr->getExpression() << std::endl;
// grapher->setExpression(grapher->size()-1 , nullptr);
// return;
}else if(expr->isValid({'t'})){
grapher->setExpression(grapher->size()-1 , expr, Equation2D::POLAR);
}else if(expr->isValid({'y'})){
grapher->setExpression(grapher->size()-1 , expr, Equation2D::INVERSE_CARTESIAN);
}else if(expr->isValid({'r'})){
grapher->setExpression(grapher->size()-1 , expr, Equation2D::INVERSE_POLAR);
}else{
// std::cout << "Invalid expression: " << expr->getExpression() << std::endl;
grapher->setExpression(grapher->size()-1 , nullptr);
}
// try{
// if(!expr->isValid({'x'})){
// std::cout << "InvE: " << expr->getExpression() << std::endl;
// }else{
// auto expr2= expr->fakeEvaluate();
// std::cout << "Fake eval (size="<<expr2.getRPN().size()<<"): " << expr2 << std::endl;
// }
// }catch(std::runtime_error& e){
// std::cerr << "Unable to set new equation, what: " << e.what() << std::endl;
// }catch(...){
// std::cerr << "Unable to set new equation, unknown error" << std::endl;
// }
}
};
int main(int argc, char** argv){
/*if(fork() != 0){
exit(0);
}*/
ContextProvider* context = ContextProvider::getProvider();
auto grapher = context->createWindow<GraphWindow>(1024, 720, Point(800, 300), "Math Equation Grapher");
//auto grapher = context->createWindow<GraphWindow>();
context->transferMainControl();
return 0;
}