public void DeletePart(PixelGridButton button)
 {
     buttonToPlacedPart.Remove(button);
     for (int i = 0; i < button.transform.childCount; i++)
     {
         Destroy(button.transform.GetChild(i).gameObject);
     }
 }
Exemplo n.º 2
0
    public void LoadVesselParts(string buildString)
    {
        List <VesselPart> parts = GameStringsHelper.PartsListFromBuildString(buildString);

        foreach (VesselPart part in parts)
        {
            PixelGridButton button = PixelGridManager.Instance().ButtonFromRelativePosition(part.position);
            PartPlacementManager.Instance().PlacePart(button, part);
        }
    }
Exemplo n.º 3
0
    public void SpawnButtons(int buttonSize)
    {
        colorButtons = new Dictionary <int, HashSet <UnityEngine.UI.Image> >();
        if (SpriteManager.SPRITE_SIZE % buttonSize != 0)
        {
            throw new UnityException(buttonSize.ToString() + " does not divide " + SpriteManager.SPRITE_SIZE + "!");
        }
        for (int i = 0; i < buttonGrid.GetLength(0); i++)
        {
            for (int j = 0; j < buttonGrid.GetLength(1); j++)
            {
                UnityEngine.UI.Button button = buttonGrid[i, j];
                Destroy(button.gameObject);
            }
        }
        int buttonGridSize = SpriteManager.SPRITE_SIZE / buttonSize;

        buttonGrid = new UnityEngine.UI.Button[buttonGridSize, buttonGridSize];
        for (int i = 0; i < buttonGrid.GetLength(0); i++)
        {
            for (int j = 0; j < buttonGrid.GetLength(1); j++)
            {
                GameObject            buttonObject    = new GameObject("Button " + i.ToString() + j.ToString());
                UnityEngine.UI.Button buttonComponent = buttonObject.AddComponent <UnityEngine.UI.Button>();
                RectTransform         rectTransform   = buttonObject.AddComponent <RectTransform>();
                buttonObject.transform.SetParent(gameObject.transform);
                rectTransform.anchorMin = new Vector2((float)i / buttonGridSize, (float)j / buttonGridSize);
                rectTransform.anchorMax = new Vector2((float)(i + 1) / buttonGridSize, (float)(j + 1) / buttonGridSize);
                rectTransform.offsetMin = Vector2.zero;
                rectTransform.offsetMax = Vector2.zero;
                UnityEngine.UI.Image image = buttonObject.AddComponent <UnityEngine.UI.Image>();
                image.sprite = SpriteManager.Instance().SpriteFromName("square");
                image.color  = Color.black;
                //buttonScript.targetGraphic = image;
                PixelGridButton buttonScript = buttonObject.AddComponent <PixelGridButton>();
                buttonScript.Initialize(new Vector2(i, j));
                buttonComponent.onClick.AddListener(buttonScript.OnClicked);
                UnityEngine.EventSystems.EventTrigger       eventTrigger = buttonObject.AddComponent <UnityEngine.EventSystems.EventTrigger>();
                UnityEngine.EventSystems.EventTrigger.Entry entry        = new UnityEngine.EventSystems.EventTrigger.Entry();
                entry.eventID = UnityEngine.EventSystems.EventTriggerType.PointerEnter;
                entry.callback.AddListener((eventData) => { buttonScript.MouseEnter(); });
                eventTrigger.triggers.Add(entry);
                buttonGrid[i, j] = buttonComponent;
            }
        }
        if (PartPlacementManager.Instance() != null)
        {
            PartPlacementManager.Instance().ResetParts();
        }
    }
Exemplo n.º 4
0
 public void ButtonClicked(PixelGridButton button)
 {
     if (currentTool == ToolType.placePart)
     {
         PartPlacementManager.Instance().PlacePartFromUI(button);
         currentTool = ToolType.noTool;
     }
     else if (currentTool == ToolType.deletePart)
     {
         PartPlacementManager.Instance().DeletePart(button);
     }
     else if (currentTool == ToolType.paint) //regular color-painting click
     {
         UnityEngine.UI.Image image = button.gameObject.GetComponent <UnityEngine.UI.Image>();
         image.color = SelectedColor();
         foreach (int key in colorButtons.Keys)
         {
             colorButtons[key].Remove(image);
         }
         ColorButtonsAdd(selectedColor, image);
     }
 }
    public void PlacePart(PixelGridButton button, VesselPart partToPlace)
    {
        if (buttonToPlacedPart.ContainsKey(button))
        {
            Debug.Log("Dictionary already contains key " + button.ToString());
            Debug.Log("There is already a part there.");
            return;
        }

        button.gameObject.transform.SetSiblingIndex(button.transform.parent.childCount - 1);

        GameObject    placedPart    = new GameObject("Placed Part");
        RectTransform rectTransform = placedPart.AddComponent <RectTransform>();

        placedPart.transform.SetParent(button.transform);
        rectTransform.anchorMin = Vector2.zero;
        rectTransform.anchorMax = Vector2.zero;
        float halfsize = partToPlace.size *
                         Vessel.partSizeFactor *
                         SpriteManager.SPRITE_SIZE *
                         PixelGridManager.Instance().GetSizeFactor() / 2;

        rectTransform.offsetMin   = Vector2.one * -1 * halfsize;
        rectTransform.offsetMax   = Vector2.one * halfsize;
        rectTransform.eulerAngles = new Vector3(0, 0, partToPlace.facing);
        UnityEngine.UI.Image image = placedPart.AddComponent <UnityEngine.UI.Image>();
        string spriteName;

        if (partToPlace.partType == VesselPartType.Bay)
        {
            spriteName = "square";
        }
        else if (partToPlace.partType == VesselPartType.Engine)
        {
            spriteName = "halfcircle";
        }
        else //(partInProgress.partType == VesselPartType.Launcher)
        {
            spriteName = "triangle";
        }
        image.sprite        = SpriteManager.Instance().SpriteFromName(spriteName);
        image.color         = Color.white * GetPartColor();
        image.raycastTarget = false;

        partToPlace.position = button.GetPosition();
        VesselPart newPart = partToPlace;

        buttonToPlacedPart[button] = newPart;

        GameObject deleter = new GameObject("Deleter");

        rectTransform = deleter.AddComponent <RectTransform>();
        deleter.transform.SetParent(button.transform);
        rectTransform.anchorMin = Vector2.zero;
        rectTransform.anchorMax = Vector2.one;
        rectTransform.offsetMin = Vector2.zero;
        rectTransform.offsetMax = Vector2.zero;
        image               = deleter.AddComponent <UnityEngine.UI.Image>();
        image.sprite        = SpriteManager.Instance().SpriteFromName("X");
        image.color         = new Color(0.75f, 0, 0);
        image.raycastTarget = false;
    }
 public void PlacePartFromUI(PixelGridButton button)
 {
     PlacePart(button, partInProgress);
 }