context: using UnityEngine;
  using UnityEditor;
  using System.Collections;
  using System.Collections.Generic;
  using System.Text;
  using qtools.qhierarchy.pdata;
  using System;
  
  namespace qtools.qhierarchy.phierarchy
  {
  	public class QHierarchySettingsWindow : EditorWindow 
  	{	
          // STATIC
  		[MenuItem ("Tools/QHierarchy")]	
  		public static void ShowWindow () 
  		{ 
  			EditorWindow window = EditorWindow.GetWindow(typeof(QHierarchySettingsWindow));           
              window.minSize = new Vector2(375, 400);
  
              #if UNITY_4_6 || UNITY_4_7 || UNITY_5_0
                  window.title = "QHierarchy";
              #else
                  window.titleContent = new GUIContent("QHierarchy");
              #endif
  		}
  
          // PRIVATE
          private bool inited = false;
          private Rect lastRect;
          private bool isProSkin;
          private int indentLevel;
          private Texture2D checkBoxChecked;
          private Texture2D checkBoxUnchecked;
          private Texture2D orderUp;
          private Texture2D orderDown;
          private Vector2 scrollPosition = new Vector2();
          private Color separatorColor;
          private Color yellowColor;
          private float totalWidth;
  
          // INIT
          private void init() 
          { 
              inited            = true;
              isProSkin         = EditorGUIUtility.isProSkin;
              separatorColor    = isProSkin ? new Color(0.18f, 0.18f, 0.18f) : new Color(0.59f, 0.59f, 0.59f);
              yellowColor       = isProSkin ? new Color(1.00f, 0.90f, 0.40f) : new Color(0.31f, 0.31f, 0.31f);
              checkBoxChecked   = QResources.getInstance().getTexture(QTexture.QCheckBoxChecked);
              checkBoxUnchecked = QResources.getInstance().getTexture(QTexture.QCheckBoxUnchecked);
              orderUp           = QResources.getInstance().getTexture(QTexture.QArrowUpButton);
              orderDown         = QResources.getInstance().getTexture(QTexture.QArrowDownButton);
          }
           
          // GUI
  		void OnGUI()
  		{
              if (!inited || isProSkin != EditorGUIUtility.isProSkin)  
                  init();
  
              indentLevel = 8;
              scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
              {
                  Rect targetRect = EditorGUILayout.GetControlRect(GUILayout.Height(lastRect.y + lastRect.height));
                  if (Event.current.type == EventType.Repaint) totalWidth = targetRect.width + 8;
  
                  this.lastRect = new Rect(0, 1, 0, 0);
  
                  // COMPONENTS
                  drawSection("COMPONENTS SETTINGS");
                  float sectionStartY = lastRect.y + lastRect.height;
                  drawSpace(5);  
  
                  drawTreeMapComponentSettings();
                  drawSeparator();
                  drawMonoBehaviourIconComponentSettings();
                  drawSeparator();
                  drawSeparatorComponentSettings();
                  drawSeparator();
                  drawVisibilityComponentSettings();
                  drawSeparator();
                  drawLockComponentSettings();
                  drawSeparator();
                  drawStaticComponentSettings();
                  drawSeparator();
                  drawErrorComponentSettings();
                  drawSeparator();
                  drawRendererComponentSettings();
                  drawSeparator();
                  drawPrefabComponentSettings();
                  drawSeparator();
                  drawTagLayerComponentSettings();
                  drawSeparator();
                  drawColorComponentSettings();
                  drawSeparator();
                  drawGameObjectIconComponentSettings();
                  drawSeparator();
                  drawTagIconComponentSettings();
                  drawSeparator();
                  drawChildrenCountComponentSettings();
                  drawSeparator();
                  drawComponentsComponentSettings();
  
                  drawSpace(3);  
                  drawLeftLine(sectionStartY, lastRect.y + lastRect.height, separatorColor);
  
                  // ORDER
                  drawSection("ORDER OF COMPONENTS");         
                  sectionStartY = lastRect.y + lastRect.height;
                  drawSpace(8);  
                  drawOrderSettings();
                  drawSpace(6);      
                  drawLeftLine(sectionStartY, lastRect.y + lastRect.height, separatorColor);
  
                  // ADDITIONAL
                  drawSection("ADDITIONAL SETTINGS");             
                  sectionStartY = lastRect.y + lastRect.height;
                  drawSpace(3);  
  
                  drawAdditionalSettings();
  
                  drawLeftLine(sectionStartY, lastRect.y + lastRect.height + 4, separatorColor);
  
                  indentLevel -= 1;
              }
  
              EditorGUILayout.EndScrollView();
          }
  
          // ORDER
          private void drawOrderSettings()
          {
              indentLevel += 8;
  
              string componentOrder = QSettings.getInstance().get<string>(QSetting.ComponentOrder);
              string[] componentIds = componentOrder.Split(';');
  
              for (int i = 0; i < componentIds.Length; i++)
              {
                  QHierarchyComponentEnum type = (QHierarchyComponentEnum)int.Parse(componentIds[i]);
  
                  Rect rect = getControlRect(14, 17);
  
                  if (i > 0)
                  {
                      if (GUI.Button(rect, orderUp, GUIStyle.none))
                      {
                          string newIconOrder = "";
                          for (int j = 0; j < componentIds.Length; j++)
                          {
                              if (j == i - 1) newIconOrder += componentIds[i] + ";";
                              else if (j == i) newIconOrder += componentIds[i-1] + ";";
                              else newIconOrder += componentIds[j] + ";";
                          }
                          newIconOrder = newIconOrder.TrimEnd(';');
                          QSettings.getInstance().set(QSetting.ComponentOrder, newIconOrder);
                      }
                  }
                  
                  rect.x += 17; 
                  
                  if (i < componentIds.Length - 1)
                  {
                      if (GUI.Button(rect, orderDown, GUIStyle.none))
                      {
                          string newIconOrder = "";
                          for (int j = 0; j < componentIds.Length; j++)
                          {
                              if (j == i) newIconOrder += componentIds[i+1] + ";";
                              else if (j == i + 1) newIconOrder += componentIds[i] + ";";
                              else newIconOrder += componentIds[j] + ";";
                          }
                          newIconOrder = newIconOrder.TrimEnd(';');
                          QSettings.getInstance().set(QSetting.ComponentOrder, newIconOrder);
                      }
                  }
                  
                  rect.x += 19;
                  rect.y -= 1;
                  rect.width = 200; 
                  rect.height = 22;
                  GUI.Label(rect, getTextWithSpaces(type.ToString()));
              }
  
              indentLevel -= 8;
          }             
          
          // COMPONENTS
          private void drawTreeMapComponentSettings() 
          {
              drawComponentCheckBox(QSetting.ShowTreeMapComponent, "Hierarchy Tree");
          }
          
          private void drawMonoBehaviourIconComponentSettings() 
          {
              if (drawComponentCheckBox(QSetting.ShowMonoBehaviourIconComponent, "MonoBehaviour Icon"))
              {
                  Rect rect = getControlRect(0, 18);
                  drawBackground(rect.x, rect.y, rect.width, 18 * 2 + 5);
                  rect.x += 31;
                  rect.width -= 31 + 6;
  
                  drawCheckBoxRight(rect, QSetting.ShowMonoBehaviourIconComponentDuringPlayMode, "Show Component During Play Mode");
                  drawCheckBoxRight(getControlRect(0, 18, 31, 6), QSetting.IgnoreUnityMonobehaviour, "Ignore Unity MonoBehaviour");
                  drawSpace(5);
              }
          }
  
          private void drawBackground(float x, float y, float width, float height)
          {
              EditorGUI.DrawRect(new Rect(x, y, width, height), separatorColor);
          }
  
          private void drawSeparatorComponentSettings() 
          {
              if (drawComponentCheckBox(QSetting.ShowSeparatorComponent, "Separator"))
              {
                  Rect rect = getControlRect(0, 18);
                  drawBackground(rect.x, rect.y, rect.width, 18 + 5);
                  rect.x += 31;
                  rect.width -= 31 + 6;
  
                  drawCheckBoxRight(rect, QSetting.ShowRowShading, "Row Shading");
                  drawSpace(5);
              }
          }
  
          private void drawVisibilityComponentSettings() 
          {
              if (drawComponentCheckBox(QSetting.ShowVisibilityComponent, "Visibility"))
              {
                  Rect rect = getControlRect(0, 18);
                  drawBackground(rect.x, rect.y, rect.width, 18 + 5);
                  rect.x += 31;
                  rect.width -= 31 + 6;
  
                  drawCheckBoxRight(rect, QSetting.ShowVisibilityComponentDuringPlayMode, "Show Component During Play Mode");
                  drawSpace(5);
              }
          }
  
          private void drawLockComponentSettings() 
          {
              if (drawComponentCheckBox(QSetting.ShowLockComponent, "Lock"))
              {   
                  Rect rect = getControlRect(0, 18);
                  drawBackground(rect.x, rect.y, rect.width, 18 * 2 + 5);
                  rect.x += 31;
                  rect.width -= 31 + 6;
  
                  drawCheckBoxRight(rect, QSetting.ShowLockComponentDuringPlayMode, "Show Component During Play Mode");
                  drawCheckBoxRight(getControlRect(0, 18, 31, 6), QSetting.PreventSelectionOfLockedObjects, "Prevent Selection Of Locked Objects");
                  drawSpace(5);
              }
          }
  
          private void drawStaticComponentSettings() 
          {
              if (drawComponentCheckBox(QSetting.ShowStaticComponent, "Static"))
              {
                  Rect rect = getControlRect(0, 18);
                  drawBackground(rect.x, rect.y, rect.width, 18 + 5);
                  rect.x += 31;
                  rect.width -= 31 + 6;
  
                  drawCheckBoxRight(rect, QSetting.ShowStaticComponentDuringPlayMode, "Show Component During Play Mode");
                  drawSpace(5);
              }        
          }
  
          private void drawErrorComponentSettings() 
          {
              if (drawComponentCheckBox(QSetting.ShowErrorComponent, "Error"))
              {
  
gt:                 Rect rect = getControlRect(0, 18);