using Assets.Scripts; using Newtonsoft.Json; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UIElements; using XCharts.Runtime; public class GraphPanelScript : MonoBehaviour { #region PROPERTIES public SessionData Session { get; private set; } public LineChart LineChart {get; set; } #endregion #region VARIABLES //public VisualTreeAsset graphPanelTemplate; //public RenderTexture renderTexture; public UIDocument _document; private StatsMenuController _statsController; private VisualElement root; private VisualElement graphContainer; private VisualElement graphPanel; private VisualElement[,] anchors; #endregion #region EVENTS #endregion #region ENDPOINTS #endregion #region METHODS ////void UpdateChartPositionAndSize(RectTransform chartRectTransform, VisualElement chartContainer) ////{ //// // Obtenir les dimensions et la position du conteneur en pixels //// Rect containerRect = chartContainer.worldBound; //// // Convertir la position du VisualElement en coordonnées du Canvas //// var canvasRectTransform = _statsController.Canvas.GetComponent(); //// Vector2 containerSize = containerRect.size; //// Vector2 containerPosition = containerRect.position - new Vector2(canvasRectTransform.rect.width / 2, canvasRectTransform.rect.height / 2); //// // Ajuster la taille et la position du RectTransform pour qu'il corresponde au VisualElement //// chartRectTransform.anchorMin = Vector2.zero; //// chartRectTransform.anchorMax = Vector2.zero; //// chartRectTransform.pivot = new Vector2(0.5f, 0.5f); //// chartRectTransform.sizeDelta = containerSize; //// chartRectTransform.anchoredPosition = containerPosition; ////} void UpdateChartPositionAndSize(int anchorRow, int anchorCol) { //if (anchorRow < 0 || anchorRow >= 3 || anchorCol < 0 || anchorCol >= 3) //{ // Debug.LogError("Invalid anchor position"); // return; //} //VisualElement anchor = anchors[anchorRow, anchorCol]; // Positionner le LineChart //RectTransform lineChartRectTransform = LineChart.GetComponent(); //lineChartRectTransform.anchoredPosition = new Vector3(480, -184, -1); } //private Vector2 ConvertUIToCanvasPosition(VisualElement uiElement) //{ // // Trouver la caméra // Camera camera = Camera.main; // // Trouver la position de l'ancre dans l'espace du monde // Vector3 worldPosition = uiElement.worldBound.center; // // Convertir la position du monde en position de l'écran // Vector3 screenPosition = camera.WorldToScreenPoint(worldPosition); // // Convertir la position de l'écran en position de la toile (canvas) // RectTransform canvasRectTransform = lineChart.GetComponentInParent().GetComponent(); // Vector2 canvasPosition; // RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, screenPosition, camera, out canvasPosition); // return canvasPosition; //} #endregion #region LIFECYCLE protected void Awake() { _statsController = StatsMenuController.Instance; _document = _statsController.GetComponent(); root = _document.rootVisualElement; graphContainer = root.Q("ChartsParentContainer"); LineChart = gameObject.GetComponent(); var serie = LineChart.AddSerie(); serie.AddXYData(0, 10); serie.AddXYData(1, 20); serie.AddXYData(2, 15); // Synchroniser la position et les dimensions du LineChart avec le VisualElement RectTransform chartRectTransform = LineChart.GetComponent(); graphContainer.style.width = new StyleLength(Length.Percent(100)); graphContainer.style.height = new StyleLength(Length.Percent(100)); anchors = new VisualElement[3, 3]; int k = 0; for(int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { k++; anchors[i, j] = graphContainer.Q($"Anchor{k}"); } } UpdateChartPositionAndSize(1, 1); //graphContainer.RegisterCallback(evt => UpdateChartPositionAndSize(lineChart.GetComponent(), 1, 1, 300, 200)); } #endregion }