public void Update()
    {
        RecursionUtil.MoveEntities_Rec(TreeView.ItemsSource, layerMask_terrain);

        // keyboard functions

        // draw NATO icons
        if (Input.GetKeyDown(KeyCode.I))
        {
            showNatoIcons = !showNatoIcons;
        }

        //draw treeview
        if (Input.GetKeyUp(KeyCode.T))
        {
            showTreeview = !showTreeview;
        }

        // draw event log
        if (Input.GetKeyDown(KeyCode.L))
        {
            showEventLog = !showEventLog;
        }

        // draw background
        //Treeview_SetBoxHeight();
    }
    public void SelectClickedSystem()
    {
        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, Mathf.Infinity, 2147483391))
        {
            int entityID;

            if (int.TryParse(RecursionUtil.GetRootFather(hit.transform.gameObject).name, out entityID))
            {
                this.treeviewHandlerScript.TreeView.SelectedItem =
                    RecursionUtil.GetTreeviewItemByEntityId(entityID, this.treeviewHandlerScript.TreeView.ItemsSource);
            }
        }
    }
    // Use this for initialization
    public void Start()
    {
        this.treeviewHandlerScript.InitilizeTreeview();
        // branje XMLjev
        // mapiranje entitet
        this.entityTypes = XmlReader.ParseMappedEntityTypes(Application.dataPath + Constants.MappedEntityTypes);
        // branje vhodnih podatkov

        // TODO uncomment after adding initial screen

        Dictionary <int, UnitNode> blueForces    = ParseJcatsXml(resourceHandlerScript.blueFilePath, materialFrendlyForces);
        Dictionary <int, UnitNode> neutralForces = ParseJcatsXml(resourceHandlerScript.neutralFilePath, materialNeutralForces);
        Dictionary <int, UnitNode> redForces     = ParseJcatsXml(resourceHandlerScript.redFilePath, materialHostileForces);

        /*
         * Dictionary<int, UnitNode> blueForces = ParseJcatsXml(Application.dataPath + "/Resources/XMLs/kosa-m.xml", materialFrendlyForces);
         * Dictionary<int, UnitNode> neutralForces = ParseJcatsXml(Application.dataPath + "/Resources/XMLs/kosa-o.xml", materialNeutralForces);
         * Dictionary<int, UnitNode> redForces = ParseJcatsXml(Application.dataPath + "/Resources/XMLs/kosa-r.xml", materialHostileForces);
         */
        // Supply the treeview with the data - this is where you replace this code with your own..
        Treeview_DataModel itemsSource = new Treeview_DataModel {
            Text = "Show forces", EntityID = -1, IsUnit = true, Father = null
        };

        Treeview_DataModel blueItems    = Treeview_FillWithItems_Ent(blueForces, "FRIEND", itemsSource);
        Treeview_DataModel neutralItems = Treeview_FillWithItems_Ent(neutralForces, "NEUTRAL", itemsSource);
        Treeview_DataModel redItems     = Treeview_FillWithItems_Ent(redForces, "HOSTILE", itemsSource);

        itemsSource.Children.Add(blueItems);
        itemsSource.Children.Add(neutralItems);
        itemsSource.Children.Add(redItems);
        Treeview_DataModel yellowItems = new Treeview_DataModel {
            Text = "UNKNOWN", EntityID = 0, IsUnit = true, Father = itemsSource
        };
        WWW www = new WWW("file:///" + Application.dataPath + "/" + "Resources/NATO_MilitarySymbols/LandUnit_U.png");

        StartCoroutine(LoadTextureViaWWW(www, yellowItems));
        itemsSource.Children.Add(yellowItems);

        // assign gameobjects to treeview items
        RecursionUtil.Treeview_SetGameObjects_Rec(itemsSource);
        DisableShadows_Rec(itemsSource);
        this.treeviewHandlerScript.TreeView.ItemsSource = itemsSource;

        this.treeviewHandlerScript.finishedInitEntityHandler = true;
    }
    public void SendDisEvent(DetonationPdu detonationPdu)
    {
        DateTime timestamp = DateTime.Now;

        Vector3D geographicalCoordinates = CalculationUtil.ConvertToGeographicalCoordinates(new Vector3D(detonationPdu.LocationInWorldCoordinates.X, detonationPdu.LocationInWorldCoordinates.Y, detonationPdu.LocationInWorldCoordinates.Z));
        string   message = String.Format("Detonation at {0:0.000000}° lat, {1:0.000000}° lon. by entity", geographicalCoordinates.y, geographicalCoordinates.x);

        Treeview_DataModel firingEntity = RecursionUtil.GetTreeviewItemByEntityId(detonationPdu.FiringEntityID.Entity, this.treeviewHandlerScript.TreeView.ItemsSource);

        if (firingEntity == null)
        {
            message += " \"UNK\".";
        }
        else
        {
            message += " \"" + firingEntity.Text + "\".";
        }

        this.treeviewHandlerScript.AddEventToLog(new DisEvent(timestamp, message));
    }
    //////////////////////////////////////////////////////
    //// FUNCIONS FOR EXTERNAL ACCESS

    public void AddUnknownEntityToTreeview(int entityID)
    {
        finishedInitTreeviewHandler = false;

        //cloning entity
        GameObject clone = new GameObject();

        clone.name = "" + entityID;
        GameObject model = Instantiate(Resources.Load("Models/vojak") as GameObject);

        model.transform.parent   = clone.transform;
        clone.transform.position = new Vector3(0, UnityEngine.Random.Range(-1000000, -10000));

        foreach (Transform child in model.transform)
        {
            child.GetComponent <Renderer>().material             = this.entityHandlerScript.materialUnknownForces;
            child.GetComponent <Renderer>().material.mainTexture = this.entityHandlerScript.militaryTexture;
        }

        // should not occur
        // adding to treeview
        if (TreeView.ItemsSource.Children.Count < 3)
        {
            Debug.Log("Not enough children of root parent!");
            finishedInitTreeviewHandler = true;
            return;
        }

        TreeView.ItemsSource.Children[3].Children.Add(
            new Treeview_DataModel {
            Text = Constants.UnknownEntityName + entityID, EntityID = entityID, IsUnit = false, Health = 0, Father = TreeView.ItemsSource.Children[3]
        });

        this.rowContentActivator = new RowContentActivator <Treeview_DataModel>();
        this.cachingObserver     = new CachingObserver <Treeview_DataModel>(this.log);
        this.treeviewRenderer    = new GuiTreeviewRenderer <Treeview_DataModel>(this.guiLayout, this.rowClickableLocations, this.cachingObserver, this.rowRenderer);

        RecursionUtil.Treeview_SetGameObjects_Rec(TreeView.ItemsSource);
        finishedInitTreeviewHandler = true;
    }
    public void SendDisEvent(FirePdu firePdu)
    {
        DateTime timestamp = DateTime.Now;
        string   message;

        Treeview_DataModel firingEntity = RecursionUtil.GetTreeviewItemByEntityId(firePdu.FiringEntityID.Entity, this.treeviewHandlerScript.TreeView.ItemsSource);

        if (firingEntity == null)
        {
            message = "Entity \"UNK\" fired";
        }
        else
        {
            message = "Entity \"" + firingEntity.Text + "\" fired";
        }

        if (firePdu.TargetEntityID.Entity != 0)
        {
            Treeview_DataModel targetEntity = RecursionUtil.GetTreeviewItemByEntityId(firePdu.TargetEntityID.Entity, this.treeviewHandlerScript.TreeView.ItemsSource);

            if (targetEntity == null)
            {
                message += " upon entity \"UNK\"";
            }
            else
            {
                message += " upon entity \"" + targetEntity.Text + "\"";
            }
        }

        Vector3D geographicalCoordinates = CalculationUtil.ConvertToGeographicalCoordinates(new Vector3D(firePdu.LocationInWorldCoordinates.X, firePdu.LocationInWorldCoordinates.Y, firePdu.LocationInWorldCoordinates.Z));

        message += String.Format(" at {0:0.000000}° lat, {1:0.000000}° lon.", geographicalCoordinates.y, geographicalCoordinates.x);

        this.treeviewHandlerScript.AddEventToLog(new DisEvent(timestamp, message));
    }
    public void DrawInfoWindow()
    {
        if (this.treeview.SelectedItem.EntityID < 0)
        {
            return;
        }

        Treeview_DataModel selected = this.treeview.SelectedItem;
        float width  = 280;
        float height = 20;
        float left   = Screen.width - width - 10;
        float index  = 54;

        if (!selected.IsUnit && selected.GeographicCoordinates != null)
        {
            index += 22 * 6;
        }
        else if (selected.IsUnit)
        {
            index += 22 * (3 + selected.Children.Count);
        }
        else
        {
            index += 22 * 3;
        }

        if (index < 120)
        {
            index = 120;
        }

        // draw background
        //GUI.Box(new Rect(left - 15, 10, width + 15, index + 30), "");
        GUI.DrawTexture(new Rect(left - 15, 10, width + 15, 28), this.texture_TitleWindow);
        GUI.DrawTexture(new Rect(left - 15, 40, width + 15, index), this.texture_BackgroundWindow);

        if (GUI.Button(new Rect(left + width - 23, 13, 20, height), "×"))
        {
            this.treeview.SelectedItem = null;
        }

        if (selected.IsUnit)
        {
            GUI.Label(new Rect(left + 103, 15, width, height), "UNIT INFO");
            GUI.DrawTexture(new Rect(left - 15, 10, Constants.NatoIconWidth * 1.5f, Constants.NatoIconHeight * 1.5f), selected.NATO_Icon);
        }
        else
        {
            GUI.Label(new Rect(left + 90, 15, width, height), "SYSTEM INFO");
            GUI.DrawTexture(new Rect(left - 15, 10, Constants.NatoIconWidth * 1.5f, Constants.NatoIconHeight * 1.5f), selected.Father.NATO_Icon);
        }

        index = 22;

        GUI.Label(new Rect(left + 110, index += 22, width, height), "Name: " + selected.Text);

        if (!selected.IsUnit && selected.GeographicCoordinates != null)
        {
            if (!selected.Moving)
            {
                GUI.Label(new Rect(left + 110, index += 22, width, height), "Latitude: " + CalculationUtil.ConvertToDegMinSec(selected.GeographicCoordinates.y));
                GUI.Label(new Rect(left + 110, index += 22, width, height), "Longitude: " + CalculationUtil.ConvertToDegMinSec(selected.GeographicCoordinates.x));
            }
            else
            {
                GUI.Label(new Rect(left + 110, index += 22, width, height), "Latitude: " +
                          CalculationUtil.ConvertToDegMinSec(CalculationUtil.Latitude_LeftBottom + selected.GameObject.transform.position.z / CalculationUtil.DMR_map_size));
                GUI.Label(new Rect(left + 110, index += 22, width, height), "Longitude: " +
                          CalculationUtil.ConvertToDegMinSec(CalculationUtil.Longitude_LeftBottom + selected.GameObject.transform.position.x / CalculationUtil.DMR_map_size));
            }

            GUI.Label(new Rect(left + 110, index += 22, width, height), String.Format("Altitude: {0:0.00} m", selected.GeographicCoordinates.z));
            GUI.Label(new Rect(left + 110, index += 22, width, height), "Health: " + ((3 - selected.Health) * 100 / 3) + " %");
            GUI.Label(new Rect(left + 110, index += 22, width, height), "Visible: " + (!selected.IsAggregated).ToString().ToUpper());
            GUI.Label(new Rect(left + 110, index += 22, width, height), "Moving: " + selected.Moving.ToString().ToUpper());

            if (selected.Moving)
            {
                GUI.Label(new Rect(left + 110, index += 22, width, height), String.Format("Speed: {0:0.00} km/h",
                                                                                          CalculationUtil.CalculateSpeedInKmPerH(selected.MovingSpeed, selected.MovingDirection)));
            }
        }
        else if (selected.IsUnit)
        {
            GUI.Label(new Rect(left + 110, index += 22, width, height), "Health: " + selected.Health + " %");
            GUI.Label(new Rect(left + 110, index += 22, width, height), "Num. of systems: " + RecursionUtil.GetNumberOfSystems_Rec(selected));
            GUI.Label(new Rect(left + 110, index += 22, width, height), "Visible: " + (!selected.Father.IsAggregated).ToString().ToUpper());
            index += 15;
            //GUI.Label(new Rect(left + 110, index += 22, width, height), "Subunits: ");

            for (int i = 0; i < selected.Children.Count; i++)
            {
                /*GUI.Label(new Rect(left, index += 22, width, height), "      " + (
                 * i + 1) + ".)  " + selected.Children[i].Text);
                 *
                 */
                /*if (GUI.Button(new Rect(left + 110, index += 22, width, height), "      " + (i + 1) + ".)  " + selected.Children[i].Text, "Label"))
                 *  this.treeview.SelectedItem = selected.Children[i];
                 *
                 * if (selected.Children[i].IsUnit)
                 *  GUI.DrawTexture(new Rect(Screen.width - 40, index-17, 55 * 612 / 792, 55), selected.Children[i].NATO_Icon);
                 */

                if (GUI.Button(new Rect(left + 50, index += 22, width, height), selected.Children[i].Text, "Label"))
                {
                    this.treeview.SelectedItem = selected.Children[i];
                }

                if (selected.Children[i].IsUnit)
                {
                    GUI.DrawTexture(new Rect(left, index - 17, 55 * 612 / 792, 55), selected.Children[i].NATO_Icon);
                }
                else
                {
                    GUI.DrawTexture(new Rect(left, index - 17, 55 * 612 / 792, 55), selected.Children[i].Father.NATO_Icon);
                }
            }
        }
        else
        {
            GUI.Label(new Rect(left + 110, index += 22, width, height), "Health: " + ((3 - selected.Health) * 100 / 3) + " %");
            GUI.Label(new Rect(left + 110, index += 22, width, height), "Visible: " + (!selected.IsAggregated).ToString().ToUpper());
            GUI.Label(new Rect(left + 110, index += 22, width, height), "Moving: " + selected.Moving.ToString().ToUpper());
        }
    }
    //////////////////////////////////////////////////////
    //// FUNCIONS FOR EXTERNAL ACCESS

    public void SetEntityMovementAndDamage(int entityID, bool moving, Vector3 moveDirection, float speed, int entityDamage, Vector3D geodeticLocation, Vector3 position, byte entityDomain)
    {
        // find entity in treeview
        Treeview_DataModel treeviewEntity = RecursionUtil.GetTreeviewItemByEntityId(entityID, this.treeviewHandlerScript.TreeView.ItemsSource);
        string             message        = "";
        bool newEntitySpawned             = false;

        if (treeviewEntity == null)
        {
            newEntitySpawned = true;

            Debug.Log("Entity " + entityID + " wasn't found!");
            this.treeviewHandlerScript.AddUnknownEntityToTreeview(entityID);
            treeviewEntity = RecursionUtil.GetTreeviewItemByEntityId(entityID, this.treeviewHandlerScript.TreeView.ItemsSource);
            message        = "New entity";

            treeviewEntity.Health = -1;

            // TODO move to SetEntityMovementAndDamage
            // set airborne
            if (entityDomain == 2)
            {
                treeviewEntity.IsAirborne = true;
            }
        }
        else if (treeviewEntity.GameObject.transform.position.y < -2000)
        {
            newEntitySpawned = true;
            message          = "Known entity";

            if (entityDomain == 2)
            {
                treeviewEntity.IsAirborne = true;
            }
        }

        // update geodetic location
        treeviewEntity.GeographicCoordinates = geodeticLocation;

        // send entity state event
        if (newEntitySpawned)
        {
            DateTime timestamp = DateTime.Now;
            message += String.Format(" \"{0}\" spawned at {1:0.000000}° lat, {2:0.000000}° lon.", treeviewEntity.Text,
                                     treeviewEntity.GeographicCoordinates.y, treeviewEntity.GeographicCoordinates.x);
            this.treeviewHandlerScript.AddEventToLog(new DisEvent(timestamp, message));
        }


        if (!treeviewEntity.IsUnit)
        {
            // set entity position
            // TODO check maybe needs to be deleted to be faster
            if (!CalculationUtil.HaveSameValues(position, treeviewEntity.GameObject.transform.position))
            {
                treeviewEntity.GameObject.transform.position = position;
                RecursionUtil.SetWorldPositionOfParentUnit_Rec(treeviewEntity);
            }

            // set entity rotation
            if (moveDirection != Vector3.zero)
            {
                treeviewEntity.GameObject.transform.rotation = Quaternion.LookRotation(moveDirection);
            }

            if (treeviewEntity.Health != entityDamage)
            {
                // TODO 1 if(treeviewEntity.Health != entityDamage) (set new health && updateHealth)
                // TODO 2 UpdateHealth change to go up
                // update entity damage
                treeviewEntity.Health = entityDamage;

                // TODO confirm that it is working
                //RecursionUtil.UpdateHealth_Rec(this.treeviewHandlerScript.treeview.ItemsSource);
                RecursionUtil.UpdateHealthOfAncestors_Rec(treeviewEntity);
            }
        }

        // set entity movement
        if (!moving)
        {
            treeviewEntity.Moving = false;
        }
        else
        {
            treeviewEntity.Moving          = true;
            treeviewEntity.MovingSpeed     = speed;
            treeviewEntity.MovingDirection = moveDirection;
        }
    }