Example #1
0
    private void StopDragging()
    {
        dragging           = false;
        dropShadow.enabled = false;

        // Find the nearest valid slot
        InputSlot nearestSlot      = null;
        float     shortestDistance = 0;

        foreach (InputSlot inputSlot in inputSlots)
        {
            float distance = CanvasScale.Instance.WorldToCanvas(Vector3.Distance(draggedKey.transform.position, inputSlot.transform.position));
            if (distance < snapDistance && (nearestSlot == null || distance < shortestDistance))
            {
                nearestSlot      = inputSlot;
                shortestDistance = distance;
            }
        }

        if (nearestSlot != null)
        {
            AudioManager.Instance.Play("key_insert");
            // Remove the key that was occupying the slot
            InputKey replacedKey = nearestSlot.GetInputKey();
            if (replacedKey != null)
            {
                replacedKey.SetInputSlot(null);

                // Fire off in a random direction
                //float randomAngle = Random.Range(0, 360);
                //replacedKey.SetVelocity(1000 * new Vector3(Mathf.Cos(randomAngle), Mathf.Sin(randomAngle), 0));

                // Fire off away from the dragged key
                replacedKey.SetVelocity(1000 * (replacedKey.transform.position - draggedKey.transform.position).normalized);

                replacedKey.transform.SetAsLastSibling();
                InputMapper.Instance.RemoveMapping(nearestSlot.GetAction());
            }

            nearestSlot.SetInputKey(draggedKey);
            draggedKey.SetInputSlot(nearestSlot);
            draggedKey.transform.SetAsFirstSibling();
            InputMapper.Instance.AddMapping(nearestSlot.GetAction(), draggedKey.GetKeyCode());
        }
    }