<commit_before>﻿using System.Collections.Generic;
using System.IO;
using TNRD.Editor.Core;
using TNRD.Json;
using UnityEditor;
using UnityEngine;

public class NoteWindow : ExtendedWindow {

    private string previousScene = string.Empty;

    public NoteWindow() : base( new ExtendedWindowSettings() { DrawToolbar = true, UseOnSceneGUI = true } ) { }

    public override void OnInitialize() {
        base.OnInitialize();

        WindowStyle = GUIStyle.none;
    }

    public override void OnToolbarGUI() {
        if ( ExtendedGUI.ToolbarButton( "Add Note" ) ) {
            AddControl( new NoteControl() );
        }
        if ( ExtendedGUI.ToolbarButton( "Clear Notes" ) ) {
            Controls.Clear();
            SaveNotes();
        }
    }

    public override void Update( bool hasFocus ) {
        base.Update( hasFocus );

        if ( EditorApplication.currentScene != previousScene ) {
            ReloadNotes();
        }

        previousScene = EditorApplication.currentScene;

        var notes = GetControlsByType<NoteControl>();
        var position = ToWorldSize( new Vector2( -Size.x, Size.y ) / 2 );
        var isFirst = true;
        foreach ( var item in notes ) {
            if (isFirst) {
                isFirst = false;
                position += new Vector2( item.Size.x, -item.Size.y ) / 2;
                position += new Vector2( 0.1f, -0.1f );
            }
            item.Position = position;
            position.y -= item.Size.y;
            position.y -= 0.1f;
        }
    }

    public void SaveNotes() {
        var controls = GetControlsByType<NoteControl>();
        var notes = new List<NoteControl.Serializable>();
        foreach ( var item in controls ) {
            notes.Add( NoteControl.Serializable.FromNote( item ) );
        }

        var json = JsonConvert.SerializeObject( notes );

        var path = GetNotePath();
        if ( !string.IsNullOrEmpty( path ) ) {
            File.WriteAllText( GetNotePath(), json );
        }
    }

    private void ReloadNotes() {
        var controls = GetControlsByType<NoteControl>();
        foreach ( var item in controls ) {
            RemoveControl( item );
        }

        var path = GetNotePath();
        if ( !string.IsNullOrEmpty( path ) && File.Exists( path ) ) {
            var json = File.ReadAllText( GetNotePath() );
            if ( !string.IsNullOrEmpty( json ) ) {
                var notes = JsonConvert.DeserializeObject<List<NoteControl.Serializable>>( json );
                foreach ( var item in notes ) {
                    AddControl( NoteControl.Serializable.ToNote( item ) );
                }
            }
        }
    }

    private string GetNotePath() {
        var scenePath = EditorApplication.currentScene;
        if ( string.IsNullOrEmpty( scenePath ) ) return "";

        var sceneName = Path.GetFileNameWithoutExtension( scenePath );
        var folderPath = Path.GetDirectoryName( scenePath );
        return Path.Combine( folderPath, sceneName + ".notes" );
    }
}<commit_msg>Updated NoteWindow to make use of ControlList<commit_after>﻿using System.Collections.Generic;
using System.IO;
using TNRD.Editor.Core;
using TNRD.Json;
using UnityEditor;
using UnityEngine;

public class NoteWindow : ExtendedWindow {

    private string previousScene = string.Empty;

    public NoteWindow() : base( new ExtendedWindowSettings() { DrawToolbar = true, UseOnSceneGUI = true } ) { }

    private ControlList controlList = new ControlList( new Vector2( 10, 10 ) );

    public override void OnInitialize() {
        base.OnInitialize();

        WindowStyle = GUIStyle.none;
        AddControl( controlList );
    }

    public override void OnToolbarGUI() {
        if ( ExtendedGUI.ToolbarButton( "Add Note" ) ) {
            controlList.Add( new NoteControl() );
        }
        if ( ExtendedGUI.ToolbarButton( "Clear Notes" ) ) {
            controlList.Clear();
            SaveNotes();
        }
    }

    public override void Update( bool hasFocus ) {
        base.Update( hasFocus );

        if ( EditorApplication.currentScene != previousScene ) {
            ReloadNotes();
        }

        previousScene = EditorApplication.currentScene;
    }

    public void SaveNotes() {
        var controls = controlList.GetAll<NoteControl>();
        var notes = new List<NoteControl.Serializable>();
        foreach ( var item in controls ) {
            notes.Add( NoteControl.Serializable.FromNote( item ) );
        }

        var json = JsonConvert.SerializeObject( notes );

        var path = GetNotePath();
        if ( !string.IsNullOrEmpty( path ) ) {
            File.WriteAllText( GetNotePath(), json );
        }
    }

    private void ReloadNotes() {
        controlList.Clear();

        var path = GetNotePath();
        if ( !string.IsNullOrEmpty( path ) && File.Exists( path ) ) {
            var json = File.ReadAllText( GetNotePath() );
            if ( !string.IsNullOrEmpty( json ) ) {
                var notes = JsonConvert.DeserializeObject<List<NoteControl.Serializable>>( json );
                foreach ( var item in notes ) {
                    controlList.Add( NoteControl.Serializable.ToNote( item ) );
                }
            }
        }
    }

    private string GetNotePath() {
        var scenePath = EditorApplication.currentScene;
        if ( string.IsNullOrEmpty( scenePath ) ) return "";

        var sceneName = Path.GetFileNameWithoutExtension( scenePath );
        var folderPath = Path.GetDirectoryName( scenePath );
        return Path.Combine( folderPath, sceneName + ".notes" );
    }
}
{'fraction_non_alphanumeric': 0.10471765546819156, 'fraction_numerical': 0.0028591851322373124, 'mean_word_length': 2.100831024930748, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4736334', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1475, 'n_words': 370}