<commit_before>﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniRx;
using Service.MemoryBrowserService;
using System.Linq;
using Service.DevUIService;
using System.Text;

namespace UserInterface {
    public class GameDataScrollView : DataScrollView {
        //The data cells that were actually spawned and will be filled with data cell objects
        private List<List<GameDataCell>> dataRows = new List<List<GameDataCell>>();
        //The virtual rows that we put into the spawned cells on scrolling
        private List<List<DataCellObject>> data = new List<List<DataCellObject>>();

        [System.Serializable]
        public class DataCellObject {
            public CellType cellType;
            public object value;
            public List<string> dropdownValues;
            public Action<string> callback;

            public enum CellType { Output, EditableOutput, Dropdown, Button, Header, Subdata,Empty };
        }

        [Serializable]
        public class ColumnDefinition {
            public float width = 100;
            public string colName = null;
        }

        [Serializable]
        public class DataTable {
            public List<object> history = null;
            public List<ColumnDefinition> columnDef=new List<ColumnDefinition>();
            public List<List<DataCellObject>> rows = new List<List<DataCellObject>>();
        }


        private void SetupData(DataTable dataTable) {
            ClearTable();
            data = dataTable.rows;
            Setup(dataTable.columnDef, dataTable.rows.Count);
            InitializeTable();
        }

        private string outputListAsStrings(IList theList) {
            StringBuilder stb = new StringBuilder();
            for (int i = 0; i < theList.Count; i++) {
                if (i > 0) {
                    stb.Append(",");
                }
                stb.Append(theList[i].ToString());
            }
            return stb.ToString();
        }

        /// <summary>
        /// Method to create a DataTable to be visualized
        /// </summary>
        /// <param name="list"></param>
        /// <param name="history">history as breadcrumbs to jump back. root objects don't need one</param>
        /// <returns></returns>
        private DataTable CreateDataTableFromList(String title,System.Collections.IList list, List<HistoryElement> history=null) {
            if (list.Count == 0) {
                return null;
            }

            var dataTable = new DataTable();

            // root object? init history
            if (history == null) {
                history = new List<HistoryElement>();
            }


            bool addBackButton = history != null && history.Count > 0;

            // fake-headers-row:
            List<DataCellObject> header = new List<DataCellObject>();
            dataTable.rows.Add(header);
            for (int rowNr=0; rowNr < list.Count; rowNr++) {
                var rowObject = list[rowNr];

                List<DataCellObject> rowDataList = new List<DataCellObject>();

                Action<object, List<DataCellObject>> traverseObj = null;
                traverseObj = (rowObj,rowData) => {
                    DataBrowser.Traverse(rowObj, (varName, varObj, type, meta) => {
                        Debug.Log(varName + " : " + varObj);
                        if (rowNr == 0) {
                            // define the columns in the first row
                            var headerElement = new ColumnDefinition() {
                                width = meta == null ? 100.0f : meta.width,
                                colName = (meta == null || meta.visualName == null) ? varName : meta.visualName
                            };

                            dataTable.columnDef.Add(headerElement);

                            // --------fake headers------------
                            header.Add(new DataCellObject() {
                                value = headerElement.colName,
                                cellType = DataCellObject.CellType.Header,
                                callback = (st) => { Debug.Log("Pressed the header:" + headerElement.colName); }
                            });
                            // --------------------------------
                        }

                        // traverse another list and add the whole data in one DataCellObject
                        /*if (meta!=null && meta.type == DataBrowser.UIDBInclude.Type.subdata) {
                            if (type == MemoryBrowser.ElementType.listType) {
                                var subList = (IList)varObj;
                                List<List<DataCellObject>> subdata = new List<List<DataCellObject>>();
                                for (int i = 0; i < subList.Count; i++) {
                                    var subListElem = subList[i];
                                    List<DataCellObject> subRowData = new List<DataCellObject>();
                                    traverseObj(subListElem, subRowData);
                                    subdata.Add(subRowData);
                                }
                                var rowElem = new DataCellObject() {
                                    cellType = DataCellObject.CellType.Subdata,
                                    value = subdata
                                };
                                rowData.Add(rowElem);
                            }
                        }
                        else */
                        if (varObj == null || MemoryBrowser.IsSimple(varObj.GetType()) || varObj is Vector2 || varObj is Vector3) {
                            // SIMPLE TYPE-HANDLING (int,string,enums,bool,...)
                            var rowElem = new DataCellObject() {
                                value = varObj,
                                callback = (newVal) => {
                                    if (newVal == null) {
                                        // selection of readonly-cell
                                        return;
                                    }
                                    // try to set the value of this field/property
                                    bool successful = DataBrowser.SetValue(rowObj, varName, newVal);
                                    Debug.Log("Setting var " + varName + " to new Value:" + newVal + " [" + (successful ? "success" : "fail") + "]");
                                }

                            };

                            var onlyread = varObj == null || (meta != null && meta.type == DataBrowser.UIDBInclude.Type.onlyread);

                            if (onlyread) {
                                rowElem.cellType = DataCellObject.CellType.Output;
                            } else {
                                if (varObj is Enum) {
                                    rowElem.cellType = DataCellObject.CellType.Dropdown;
                                    rowElem.dropdownValues = new List<string>(Enum.GetNames(varObj.GetType()));
                                    rowElem.value = (int)varObj;
                                } else if (varObj.GetType() == typeof(bool)) {
                                    rowElem.cellType = DataCellObject.CellType.Dropdown;
                                    rowElem.dropdownValues = new List<string> { "false", "true" };
                                    rowElem.value = ((bool)varObj) ? 0 : 1;
                                } else {
                                    rowElem.cellType = DataCellObject.CellType.EditableOutput;
                                }
                            }
                            rowData.Add(rowElem);
                        } else {
                            // NOT SIMPLE TYPES ( Object-Instances, List, Dict...)<commit_msg>devui/databrowser: fixing bool-value(true was shown false and vice versa)<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniRx;
using Service.MemoryBrowserService;
using System.Linq;
using Service.DevUIService;
using System.Text;

namespace UserInterface {
    public class GameDataScrollView : DataScrollView {
        //The data cells that were actually spawned and will be filled with data cell objects
        private List<List<GameDataCell>> dataRows = new List<List<GameDataCell>>();
        //The virtual rows that we put into the spawned cells on scrolling
        private List<List<DataCellObject>> data = new List<List<DataCellObject>>();

        [System.Serializable]
        public class DataCellObject {
            public CellType cellType;
            public object value;
            public List<string> dropdownValues;
            public Action<string> callback;

            public enum CellType { Output, EditableOutput, Dropdown, Button, Header, Subdata,Empty };
        }

        [Serializable]
        public class ColumnDefinition {
            public float width = 100;
            public string colName = null;
        }

        [Serializable]
        public class DataTable {
            public List<object> history = null;
            public List<ColumnDefinition> columnDef=new List<ColumnDefinition>();
            public List<List<DataCellObject>> rows = new List<List<DataCellObject>>();
        }


        private void SetupData(DataTable dataTable) {
            ClearTable();
            data = dataTable.rows;
            Setup(dataTable.columnDef, dataTable.rows.Count);
            InitializeTable();
        }

        private string outputListAsStrings(IList theList) {
            StringBuilder stb = new StringBuilder();
            for (int i = 0; i < theList.Count; i++) {
                if (i > 0) {
                    stb.Append(",");
                }
                stb.Append(theList[i].ToString());
            }
            return stb.ToString();
        }

        /// <summary>
        /// Method to create a DataTable to be visualized
        /// </summary>
        /// <param name="list"></param>
        /// <param name="history">history as breadcrumbs to jump back. root objects don't need one</param>
        /// <returns></returns>
        private DataTable CreateDataTableFromList(String title,System.Collections.IList list, List<HistoryElement> history=null) {
            if (list.Count == 0) {
                return null;
            }

            var dataTable = new DataTable();

            // root object? init history
            if (history == null) {
                history = new List<HistoryElement>();
            }


            bool addBackButton = history != null && history.Count > 0;

            // fake-headers-row:
            List<DataCellObject> header = new List<DataCellObject>();
            dataTable.rows.Add(header);
            for (int rowNr=0; rowNr < list.Count; rowNr++) {
                var rowObject = list[rowNr];

                List<DataCellObject> rowDataList = new List<DataCellObject>();

                Action<object, List<DataCellObject>> traverseObj = null;
                traverseObj = (rowObj,rowData) => {
                    DataBrowser.Traverse(rowObj, (varName, varObj, type, meta) => {
                        Debug.Log(varName + " : " + varObj);
                        if (rowNr == 0) {
                            // define the columns in the first row
                            var headerElement = new ColumnDefinition() {
                                width = meta == null ? 100.0f : meta.width,
                                colName = (meta == null || meta.visualName == null) ? varName : meta.visualName
                            };

                            dataTable.columnDef.Add(headerElement);

                            // --------fake headers------------
                            header.Add(new DataCellObject() {
                                value = headerElement.colName,
                                cellType = DataCellObject.CellType.Header,
                                callback = (st) => { Debug.Log("Pressed the header:" + headerElement.colName); }
                            });
                            // --------------------------------
                        }

                        // traverse another list and add the whole data in one DataCellObject
                        /*if (meta!=null && meta.type == DataBrowser.UIDBInclude.Type.subdata) {
                            if (type == MemoryBrowser.ElementType.listType) {
                                var subList = (IList)varObj;
                                List<List<DataCellObject>> subdata = new List<List<DataCellObject>>();
                                for (int i = 0; i < subList.Count; i++) {
                                    var subListElem = subList[i];
                                    List<DataCellObject> subRowData = new List<DataCellObject>();
                                    traverseObj(subListElem, subRowData);
                                    subdata.Add(subRowData);
                                }
                                var rowElem = new DataCellObject() {
                                    cellType = DataCellObject.CellType.Subdata,
                                    value = subdata
                                };
                                rowData.Add(rowElem);
                            }
                        }
                        else */
                        if (varObj == null || MemoryBrowser.IsSimple(varObj.GetType()) || varObj is Vector2 || varObj is Vector3) {
                            // SIMPLE TYPE-HANDLING (int,string,enums,bool,...)
                            var rowElem = new DataCellObject() {
                                value = varObj,
                                callback = (newVal) => {
                                    if (newVal == null) {
                                        // selection of readonly-cell
                                        return;
                                    }
                                    // try to set the value of this field/property
                                    bool successful = DataBrowser.SetValue(rowObj, varName, newVal);
                                    Debug.Log("Setting var " + varName + " to new Value:" + newVal + " [" + (successful ? "success" : "fail") + "]");
                                }

                            };

                            var onlyread = varObj == null || (meta != null && meta.type == DataBrowser.UIDBInclude.Type.onlyread);

                            if (onlyread) {
                                rowElem.cellType = DataCellObject.CellType.Output;
                            } else {
                                if (varObj is Enum) {
                                    rowElem.cellType = DataCellObject.CellType.Dropdown;
                                    rowElem.dropdownValues = new List<string>(Enum.GetNames(varObj.GetType()));
                                    rowElem.value = (int)varObj;
                                } else if (varObj.GetType() == typeof(bool)) {
                                    rowElem.cellType = DataCellObject.CellType.Dropdown;
                                    rowElem.dropdownValues = new List<string> { "false", "true" };
                                    rowElem.value = ((bool)varObj) ? 1 : 0;
                                } else {
                                    rowElem.cellType = DataCellObject.CellType.EditableOutput;
                                }
                            }
                            rowData.Add(rowElem);
                        } else {
                            // NOT SIMPLE TYPES ( Object-Instances, List, Dict...)
{'fraction_non_alphanumeric': 0.09681697612732096, 'fraction_numerical': 0.0022735884804850324, 'mean_word_length': 1.1547149272009798, 'pattern_counts': {'":': 0, '<': 91, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3525971', 'n_tokens_mistral': 3697, 'n_tokens_neox': 3499, 'n_words': 867}