Ejemplo n.º 1
0
    void DrawDecoration(GraphData data, Vector2 workingDimensions, Vector2 origin) //TODO refactor this
    {
        decorationMaterial.SetPass(0);

        //Cache some repeating calculations
        float relOriginX        = origin.x / graphSize.x;
        float relOriginY        = origin.y / graphSize.y;
        float relThicknessHalfX = axesLineThickness / (graphSize.x * 2.0f);
        float relThicknessHalfY = axesLineThickness / (graphSize.y * 2.0f);

        //Draw Axes
        GL.Begin(GL.QUADS);

        //Y axis always begins at left edge
        GL.Vertex3(relOriginX + relThicknessHalfX, relOriginY, 0.0f);
        GL.Vertex3(relOriginX - relThicknessHalfX, relOriginY, 0.0f);
        GL.Vertex3(relOriginX - relThicknessHalfX, (origin.y + workingDimensions.y) / graphSize.y, 0.0f);
        GL.Vertex3(relOriginX + relThicknessHalfX, (origin.y + workingDimensions.y) / graphSize.y, 0.0f);

        //The X axis is clamped to the zero value (if the values oscilate between positive and negative), or is the min (for all positive) or max (for all negative) value.
        float yCoord;

        if (data.isOscillatingAroundYAxis) //compute the position of y=zero.
        {
            yCoord = (origin.y + workingDimensions.y * (0.0f - data.minY) / data.rangeY) / graphSize.y;
        }
        else //compute position depending on whether all positive or negative
        {
            yCoord = (origin.y + Mathf.Min((Mathf.Sign(data.Value(0))) * workingDimensions.y, 0.0f)) / graphSize.y;
        }

        GL.Vertex3(relOriginX, yCoord - relThicknessHalfY, 0.0f);
        GL.Vertex3(relOriginX, yCoord + relThicknessHalfY, 0.0f);
        GL.Vertex3(relOriginX + workingDimensions.x / graphSize.x, yCoord + relThicknessHalfY, 0.0f);
        GL.Vertex3(relOriginX + workingDimensions.x / graphSize.x, yCoord - relThicknessHalfY, 0.0f);

        //Draw Grid
        //modify cached reThicknessHalfX and Y to use gridLineThickness
        relThicknessHalfX = gridLineThickness / (graphSize.x * 2.0f);
        relThicknessHalfY = gridLineThickness / (graphSize.y * 2.0f);

        int xGridCount = Mathf.Min(data.Length(), maxGridCountX);

        //The code bellow allows for dynamic fitting of the grid, the end segment will be varrying in size, but the preceding ones will be fixed.
        int   daysPerGridSpacing = Mathf.CeilToInt(data.Length() / (float)xGridCount);
        float xDistPerDay        = (workingDimensions.x / (float)data.Length()) / graphSize.x;

        for (int i = daysPerGridSpacing; i < data.Length(); i += daysPerGridSpacing)
        {
            GL.Vertex3(relOriginX + (i * xDistPerDay) + relThicknessHalfX, relOriginY, 0.0f);
            GL.Vertex3(relOriginX + (i * xDistPerDay) - relThicknessHalfX, relOriginY, 0.0f);
            GL.Vertex3(relOriginX + (i * xDistPerDay) - relThicknessHalfX, (origin.y + workingDimensions.y) / graphSize.y, 0.0f);
            GL.Vertex3(relOriginX + (i * xDistPerDay) + relThicknessHalfX, (origin.y + workingDimensions.y) / graphSize.y, 0.0f);
        }

        float yGridSpacing = workingDimensions.y / (float)maxGridCountY / graphSize.y;

        for (int i = 0; i <= maxGridCountY; i++)
        {
            GL.Vertex3(relOriginX, relOriginY + (i * yGridSpacing) - relThicknessHalfY, 0.0f);
            GL.Vertex3(relOriginX, relOriginY + (i * yGridSpacing) + relThicknessHalfY, 0.0f);
            GL.Vertex3((origin.x + workingDimensions.x) / graphSize.x, relOriginY + (i * yGridSpacing) + relThicknessHalfY, 0.0f);
            GL.Vertex3((origin.x + workingDimensions.x) / graphSize.x, relOriginY + (i * yGridSpacing) - relThicknessHalfY, 0.0f);
        }

        GL.End();

        //Spawn labels as Gameobjects with Text components (make them children of this transform)
        for (int i = 0; i < xGridCount; i++)
        {
            //Set visibility on
            labelsX[i].SetActive(true);

            //Position at grid line
            labelsX[i].GetComponent <RectTransform>().anchoredPosition = this.gameObject.GetComponent <RectTransform>().anchoredPosition - workingDimensions / 2.0f
                                                                         + new Vector2(i * daysPerGridSpacing * workingDimensions.x / data.Length(), -1.0f * padding.y);

            //Set text value/date
            labelsX[i].GetComponent <Text>().text = UIManager.DateToString(activeGraphData.DateByOffset(i * daysPerGridSpacing), true);
        }

        for (int i = 0; i <= maxGridCountY; i++)
        {
            //set visibility on
            labelsY[i].SetActive(true);

            //position at grid line
            labelsY[i].GetComponent <RectTransform>().anchoredPosition = this.gameObject.GetComponent <RectTransform>().anchoredPosition - workingDimensions / 2.0f
                                                                         + new Vector2(-1.0f * padding.x, i * workingDimensions.y / (float)maxGridCountY + 10.0f);

            //set text vale/date
            labelsY[i].GetComponent <Text>().text = (i * activeGraphData.rangeY / (maxGridCountY) + activeGraphData.minY).ToString();
        }


        //disable unused labels
        for (int i = xGridCount; i < labelsX.Count; i++)
        {
            labelsX[i].SetActive(false);
        }
    }