-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
220 lines (215 loc) · 7.38 KB
/
main.cpp
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include<SFML/Graphics.hpp>
#include<iostream>
#include<functional>
#include<imgui.h>
#include<imgui-SFML.h>
#include"Function.h"
#include<thread>
//TODO
//intersection of function
int g_oneStep = 50.0f;
bool g_fieldHasChanged = true;
int g_width = 800;
int g_height = 600;
void DrawField(sf::RenderWindow& window);
int main(int argc, char* argv[])
{
sf::RenderWindow window(sf::VideoMode(g_width, g_height), "Function Graph");
sf::View view = window.getView();
view.setCenter({ 0.0f,0.0f });
window.setView(view);
ImGui::SFML::Init(window);
std::vector<Function> functions;
sf::Clock clock;
sf::Vector2f oldPosMouse;
int itemToDelete = 0;
char newFuncName[255]{};
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
ImGui::SFML::ProcessEvent(window, event);
if (event.type == sf::Event::Closed)
{
window.close();
}
if (event.type == sf::Event::Resized)
{
window.setSize({ event.size.width,event.size.height });
}
if (event.type == sf::Event::MouseWheelScrolled)
{
sf::View view = window.getView();
if (event.mouseWheelScroll.delta > 0)
{
view.zoom(0.9f);
}
else if (event.mouseWheelScroll.delta < 0)
{
view.zoom(1.1f);
}
window.setView(view);
g_fieldHasChanged = true;
}
if(event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Right)
{
oldPosMouse.x = event.mouseButton.x;
oldPosMouse.y = event.mouseButton.y;
}
if (event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Right)
{
float deltaX = oldPosMouse.x - event.mouseButton.x;
float deltaY = oldPosMouse.y - event.mouseButton.y;
sf::View currentView = window.getView();
sf::Vector2f currentCenter = currentView.getCenter();
currentView.setCenter({ currentCenter.x + deltaX,currentCenter.y + deltaY });
window.setView(currentView);
g_fieldHasChanged = true;
}
}
ImGui::SFML::Update(window, clock.restart());
if (g_fieldHasChanged)
{
for (size_t i = 0; i < functions.size(); i++)
{
functions[i].recalculate();
}
DrawField(window);
g_fieldHasChanged = false;
}
for (size_t i = 0; i < functions.size(); i++)
{
functions[i].draw(window, g_oneStep);
}
DrawField(window);
ImGui::Begin("FunctionGraph");
ImGui::SliderInt("OneStep", &g_oneStep,30,300);
if (ImGui::Button("Apply"))
{
g_fieldHasChanged = true;
}
ImGui::InputText("New Function name", newFuncName, 255);
if (ImGui::Button("create Function"))
{
functions.push_back(Function(newFuncName));
}
const char* items[255]{};
for (size_t i = 0; i < functions.size(); i++)
{
items[i] = functions[i].getName();
}
ImGui::ListBox("Functions:", &itemToDelete, items, functions.size());
if(ImGui::Button("delete Function"))
{
functions.erase(functions.begin()+ itemToDelete);
}
ImGui::End();
ImGui::SFML::Render(window);
window.display();
window.clear();
}
ImGui::SFML::Shutdown();
return 0;
}
void DrawField(sf::RenderWindow& window)
{
static std::vector<Line> lines;
static std::vector<sf::Text> texts;
sf::Font font;
font.loadFromFile("1.ttf");
sf::Text text("", font, g_oneStep / 3);
text.setStyle(sf::Text::Bold);
text.setColor(sf::Color::White);
sf::View view = window.getView();
sf::Vector2f sizeOfViewport = view.getSize();
sf::Vector2f centerOfViewport = view.getCenter();
sf::Vector2f bordersOfViewX = { centerOfViewport.x - sizeOfViewport.x / 2.0f,centerOfViewport.x + sizeOfViewport.x / 2.0f };
sf::Vector2f bordersOfViewY = { centerOfViewport.y - sizeOfViewport.y / 2.0f,centerOfViewport.y + sizeOfViewport.y / 2.0f };
if (g_fieldHasChanged)
{
lines.clear();
texts.clear();
const int indent = 5;
float widthIndentation = 0.0f;
//left
for (int i = 0; i > bordersOfViewX.x / g_oneStep; i--)
{
Line line(widthIndentation, indent, widthIndentation, -indent);
lines.push_back(line);
text.setString(std::to_string(i));
sf::Vertex* textPos = line.getVertices();
textPos->position.y -= 8;
text.setPosition(textPos->position);
texts.push_back(sf::Text(text));
widthIndentation -= g_oneStep;
}
//right
widthIndentation = 0.0f;
for (int i = 0; i < bordersOfViewX.y / g_oneStep; i++)
{
Line line(widthIndentation, indent, widthIndentation, -indent);
lines.push_back(line);
text.setString(std::to_string(i));
sf::Vertex* textPos = line.getVertices();
textPos->position.y -= 8;
text.setPosition(textPos->position);
if (i != 0)
{
texts.push_back(sf::Text(text));
}
widthIndentation += g_oneStep;
}
//up
float heightIndentation = 0.0f;
for (int i = 0; i > bordersOfViewY.x / g_oneStep; i--)
{
Line line(indent, heightIndentation, -indent, heightIndentation);
lines.push_back(line);
text.setString(std::to_string(abs(i)));
sf::Vertex* textPos = line.getVertices();
textPos->position.x += 8;
text.setPosition(textPos->position);
if (i != 0)
{
texts.push_back(sf::Text(text));
}
heightIndentation -= g_oneStep;
}
//down
heightIndentation = 0;
for (int i = 0; i < bordersOfViewY.y / g_oneStep; i++)
{
Line line(indent, heightIndentation, -indent, heightIndentation);
lines.push_back(line);
text.setString('-' + std::to_string(i));
sf::Vertex* textPos = line.getVertices();
textPos->position.x += 8;
text.setPosition(textPos->position);
if (i != 0)
{
texts.push_back(sf::Text(text));
}
heightIndentation += g_oneStep;
}
}
for (size_t i = 0; i < lines.size(); i++)
{
window.draw(lines[i].getVertices(), 2, sf::Lines);
}
for (size_t i = 0; i < texts.size(); i++)
{
window.draw(texts[i]);
}
text.setCharacterSize(15);
text.setString("X");
text.setPosition(sf::Vector2f(g_width /2.0f - 15.0f, -30.0f));
window.draw(text);
text.setString("Y");
text.setPosition(sf::Vector2f(30.0f, -g_height / 2.0f));
window.draw(text);
Line lineX(bordersOfViewX.x, 0.f, bordersOfViewX.y, 0.f);
Line lineY(0.0f, bordersOfViewY.x, 0.0f, bordersOfViewY.y);
window.draw(lineX.getVertices(), 2, sf::Lines);
window.draw(lineY.getVertices(), 2, sf::Lines);
}