Skip to content

Commit a482209

Browse files
committed
Versione 0.2
Fixed bug negative plotting the graph along the Y-axis, added validate adding point, refactoring of the code
1 parent 08f72a9 commit a482209

File tree

3 files changed

+86
-76
lines changed

3 files changed

+86
-76
lines changed

Assets/Scripts/MathPart/ParserHelp.cs renamed to Assets/Scripts/MathPart/CustomParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using UnityEngine;
44

5-
public static class ParserHelp {
5+
public static class CustomParser {
66
public static double ParseToDouble(string value) {
77
double result = Double.NaN;
88
value = value.Trim();

Assets/Scripts/MathPart/GraphCreator.cs

Lines changed: 83 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class GraphCreator : MonoBehaviour {
99
[Header("Точки для вставки в график")]
1010
[SerializeField] GameObject PointFor;
1111
[SerializeField] Transform PointForUTransform;
12-
Transform PointForUTransformTmp;
12+
protected Transform PointForUTransformTmp;
1313

1414
[Header("UILineRenderer для создания графикa")]
1515
[SerializeField] UILineRenderer LineRenderForCreateGraph;
@@ -38,6 +38,18 @@ public class GraphCreator : MonoBehaviour {
3838
public List<Vector2> dataPoints = new List<Vector2>();
3939
RectTransform mainTransform;
4040

41+
[Header("стартовая координата по оси Х")]
42+
[SerializeField]
43+
float StartCoordinateX = 0;
44+
45+
[Header("стартовая координата по оси У")]
46+
[SerializeField]
47+
float StartCoordinateY = 0;
48+
protected float step = 0;
49+
50+
public float valueX { private get; set; }
51+
public float valueY { private get; set; }
52+
protected Vector2 lastPoint;
4153

4254
private void Awake() {
4355
mainTransform = GetComponent<RectTransform>();
@@ -53,113 +65,113 @@ public void CreateStartGraph() {
5365
AddTextForY();
5466
}
5567

56-
[SerializeField]
57-
float StartCoordinateX=0;
58-
void AddTextForX() {
59-
if (sizeStepX>=0){
60-
float step = sizeStepX / Xsteps;
68+
protected void AddTextForX() {
69+
if (sizeStepX >= 0) {
70+
step = sizeStepX / Xsteps;
6171
for (float i = StartCoordinateX; i <= sizeStepX; i += step) {
62-
GameObject newText = Instantiate(axisTextHolder.GetChild(0).gameObject, axisTextHolder);
63-
newText.GetComponent<RectTransform>().anchoredPosition = new Vector2((i * mainTransform.sizeDelta.x) / sizeStepX, 0f);
64-
newText.GetComponent<Text>().text = Math.Round(i, 1).ToString();
65-
newText.transform.GetChild(0).GetComponent<RectTransform>().sizeDelta = new Vector2(1, mainTransform.sizeDelta.y);
66-
if (i == 0f) newText.transform.GetChild(0).gameObject.SetActive(false);
72+
addTestForX(i);
6773
}
68-
axisTextHolder.GetChild(0).gameObject.SetActive(false);
6974
} else {
70-
float step = Math.Abs(sizeStepX / Xsteps);
75+
step = Math.Abs(sizeStepX / Xsteps);
7176
for (float i = StartCoordinateX; i >= sizeStepX; i -= step) {
72-
GameObject newText = Instantiate(axisTextHolder.GetChild(0).gameObject, axisTextHolder);
73-
newText.GetComponent<RectTransform>().anchoredPosition = new Vector2((i * mainTransform.sizeDelta.x) / sizeStepX, 0f);
74-
newText.GetComponent<Text>().text = Math.Round(sizeStepX-i, 1).ToString();
75-
newText.transform.GetChild(0).GetComponent<RectTransform>().sizeDelta = new Vector2(1, mainTransform.sizeDelta.y);
76-
if (i == 0f) newText.transform.GetChild(0).gameObject.SetActive(false);
77+
addTestForX(i, true);
7778
}
78-
axisTextHolder.GetChild(0).gameObject.SetActive(false);
7979
}
80+
axisTextHolder.GetChild(0).gameObject.SetActive(false);
8081
}
81-
[SerializeField]
82-
float StartCoordinateY=0;
83-
void AddTextForY() {
84-
if (sizeStepY>=0){
85-
float step = sizeStepY / Ysteps;
82+
83+
protected void AddTextForY() {
84+
if (sizeStepY >= 0) {
85+
step = sizeStepY / Ysteps;
8686
for (float i = StartCoordinateY; i <= sizeStepY; i += step) {
87-
GameObject newText = Instantiate(axisTextHolder.GetChild(1).gameObject, axisTextHolder);
88-
newText.GetComponent<RectTransform>().anchoredPosition = new Vector2(0f, (i * mainTransform.sizeDelta.y) / sizeStepY);
89-
if (i > 0f) newText.GetComponent<Text>().text = Math.Round(i, 1).ToString() + " ";
90-
newText.transform.GetChild(0).GetComponent<RectTransform>().sizeDelta = new Vector2(mainTransform.sizeDelta.x, 1);
91-
if (i == 0f) newText.transform.GetChild(0).gameObject.SetActive(false);
87+
addTestForY(i);
9288
}
93-
axisTextHolder.GetChild(1).gameObject.SetActive(false);
9489
} else {
95-
float step = sizeStepY / Ysteps;
90+
step = Math.Abs(sizeStepY / Ysteps);
9691
for (float i = StartCoordinateY; i >= sizeStepY; i -= step) {
97-
GameObject newText = Instantiate(axisTextHolder.GetChild(1).gameObject, axisTextHolder);
98-
newText.GetComponent<RectTransform>().anchoredPosition = new Vector2(0f, (i * mainTransform.sizeDelta.y) / sizeStepY);
99-
if (i > 0f) newText.GetComponent<Text>().text = Math.Round(sizeStepY-1, 1).ToString() + " ";
100-
newText.transform.GetChild(0).GetComponent<RectTransform>().sizeDelta = new Vector2(mainTransform.sizeDelta.x, 1);
101-
if (i == 0f) newText.transform.GetChild(0).gameObject.SetActive(false);
92+
addTestForY(i, true);
10293
}
103-
axisTextHolder.GetChild(1).gameObject.SetActive(false);
10494
}
95+
axisTextHolder.GetChild(1).gameObject.SetActive(false);
96+
}
97+
98+
protected void addTestForX(float i, bool negative = false) {
99+
GameObject newText = Instantiate(axisTextHolder.GetChild(0).gameObject, axisTextHolder);
100+
newText.GetComponent<RectTransform>().anchoredPosition = new Vector2((i * mainTransform.sizeDelta.x) / sizeStepX, 0f);
101+
if (!negative) newText.GetComponent<Text>().text = Math.Round(i, 1).ToString();
102+
else newText.GetComponent<Text>().text = Math.Round(sizeStepX - i, 1).ToString();
103+
newText.transform.GetChild(0).GetComponent<RectTransform>().sizeDelta = new Vector2(1, mainTransform.sizeDelta.y);
104+
if (i == 0f) newText.transform.GetChild(0).gameObject.SetActive(false);
105+
}
106+
107+
protected void addTestForY(float i, bool negative = false) {
108+
GameObject newText = Instantiate(axisTextHolder.GetChild(1).gameObject, axisTextHolder);
109+
newText.GetComponent<RectTransform>().anchoredPosition = new Vector2(0f, (i * mainTransform.sizeDelta.y) / sizeStepY);
110+
if (i > 0f)
111+
if (!negative) newText.GetComponent<Text>().text = Math.Round(i, 1).ToString() + " ";
112+
else newText.GetComponent<Text>().text = Math.Round(sizeStepY - 1, 1).ToString() + " ";
113+
newText.transform.GetChild(0).GetComponent<RectTransform>().sizeDelta = new Vector2(mainTransform.sizeDelta.x, 1);
114+
if (i == 0f) newText.transform.GetChild(0).gameObject.SetActive(false);
105115
}
106116

107117
public void DeleteAllPoints() {
108-
DeleteEveryGameObj(InstancePoints);
109-
LineRenderForCreateGraph.Points = new Vector2[0];
110-
PointForUTransform = PointForUTransformTmp;
111-
lastPoint = Vector2.zero;
112-
dataPoints.Clear();
118+
if (InstancePoints.Count != 0) {
119+
DeleteEveryGameObj(InstancePoints);
120+
LineRenderForCreateGraph.Points = new Vector2[0];
121+
PointForUTransform = PointForUTransformTmp;
122+
lastPoint = Vector2.zero;
123+
dataPoints.Clear();
124+
}
113125
}
114126

115-
void DeleteEveryGameObj(List<GameObject> DeleteGameObj) {
127+
protected void DeleteEveryGameObj(List<GameObject> DeleteGameObj) {
116128
if (DeleteGameObj.Count != 0) {
117129
for (int i = DeleteGameObj.Count - 1; i >= 0; i--)
118130
Destroy(DeleteGameObj[i]);
119131
DeleteGameObj.Clear();
120132
}
121133
}
122134

123-
public float valueX { private get; set; }
124-
public float valueY { private get; set; }
125-
Vector2 lastPoint;
126-
// int pointIplus = 0;
127-
128135
public void AddPoint() {
129-
130-
if (lastPoint == new Vector2(valueX, valueY)) return;
131-
dataPoints.Insert(0, new Vector2(valueX, valueY));
132-
InstancePoints.Insert(0, Instantiate(PointFor, PointForUTransform.position,
133-
PointForUTransform.rotation));
134-
InstancePoints[0].SetActive(true);
135-
InstancePoints[0].transform.SetParent(PointFor.transform.parent);
136-
InstancePoints[0].transform.localScale = Vector3.one;
137-
InstancePoints[0].GetComponent<RectTransform>().anchoredPosition =
138-
new Vector3(InstancePoints[0].GetComponent<RectTransform>().anchoredPosition.x
139-
+ valueX *
140-
Grafik.GetComponent<RectTransform>().sizeDelta.x / sizeStepX,
141-
InstancePoints[0].GetComponent<RectTransform>().anchoredPosition.y
142-
+ valueY *
143-
Grafik.GetComponent<RectTransform>().sizeDelta.y / sizeStepY, 0);
144-
LineRenderForCreateGraph.Points = new Vector2[InstancePoints.Count];
145-
for (int i = 0; i < InstancePoints.Count; i++) {
146-
LineRenderForCreateGraph.Points[i] = new Vector2(InstancePoints[i].GetComponent<RectTransform>().anchoredPosition.x,
147-
InstancePoints[i].GetComponent<RectTransform>().anchoredPosition.y);
136+
if (ValidateAddingPoint()) {
137+
if (lastPoint == new Vector2(valueX, valueY)) return;
138+
dataPoints.Insert(0, new Vector2(valueX, valueY));
139+
InstancePoints.Insert(0, Instantiate(PointFor, PointForUTransform.position,
140+
PointForUTransform.rotation));
141+
InstancePoints[0].SetActive(true);
142+
InstancePoints[0].transform.SetParent(PointFor.transform.parent);
143+
InstancePoints[0].transform.localScale = Vector3.one;
144+
InstancePoints[0].GetComponent<RectTransform>().anchoredPosition =
145+
new Vector3(InstancePoints[0].GetComponent<RectTransform>().anchoredPosition.x
146+
+ valueX *
147+
Grafik.GetComponent<RectTransform>().sizeDelta.x / sizeStepX,
148+
InstancePoints[0].GetComponent<RectTransform>().anchoredPosition.y
149+
+ valueY *
150+
Grafik.GetComponent<RectTransform>().sizeDelta.y / sizeStepY, 0);
151+
LineRenderForCreateGraph.Points = new Vector2[InstancePoints.Count];
152+
for (int i = 0; i < InstancePoints.Count; i++) {
153+
LineRenderForCreateGraph.Points[i] = new Vector2(InstancePoints[i].GetComponent<RectTransform>().anchoredPosition.x,
154+
InstancePoints[i].GetComponent<RectTransform>().anchoredPosition.y);
148155

156+
}
157+
lastPoint = new Vector2(valueX, valueY);
149158
}
150-
//pointIplus++;
151-
lastPoint = new Vector2(valueX, valueY);
152159
}
153160

154161
public void DeletePoint() {
155162
if (LineRenderForCreateGraph.Points.Length == 0) return;
156163
Vector2[] newPoints = new Vector2[LineRenderForCreateGraph.Points.Length - 1];
157164
for (int i = 0; i < newPoints.Length; i++) newPoints[i] = LineRenderForCreateGraph.Points[i + 1];
158165
LineRenderForCreateGraph.Points = newPoints;
159-
160166
Destroy(InstancePoints[0]);
161167
InstancePoints.RemoveAt(0);
162168
lastPoint = Vector2.zero;
163169
dataPoints.RemoveAt(0);
164170
}
165-
}
171+
172+
protected bool ValidateAddingPoint() {
173+
if (Math.Abs(valueX) > Math.Abs(sizeStepX) || Math.Abs(valueY) > Math.Abs(sizeStepY)) return false;
174+
return true;
175+
}
176+
}
177+

Assets/Scripts/MathPart/GraphCreatorInputContoller.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using UnityEngine;
33
using UnityEngine.UI;
4-
using UnityEngine.UI.Extensions;
5-
using System;
64

75
public class GraphCreatorInputContoller : MonoBehaviour {
86
// Start is called before the first frame update
@@ -12,9 +10,9 @@ public class GraphCreatorInputContoller : MonoBehaviour {
1210
public InputField inputY;
1311

1412
public void SetPoint() {
15-
if (ParserHelp.ParseToDouble(inputX.text)!=Double.NaN) graph.valueX = (float)ParserHelp.ParseToDouble(inputX.text);
13+
if (CustomParser.ParseToDouble(inputX.text)!=Double.NaN) graph.valueX = (float)CustomParser.ParseToDouble(inputX.text);
1614
else return;
17-
if (ParserHelp.ParseToDouble(inputY.text) != Double.NaN) graph.valueY = (float)ParserHelp.ParseToDouble(inputY.text);
15+
if (CustomParser.ParseToDouble(inputY.text) != Double.NaN) graph.valueY = (float)CustomParser.ParseToDouble(inputY.text);
1816
else return;
1917
graph.AddPoint();
2018
}

0 commit comments

Comments
 (0)