Exemplo n.º 1
0
    //build the popup from the given list of descriptors
    //also receives the component that this popup will edit and the type of component used to find the descriptors
    public void buildPopup(GameRuleDesignComponent cr, List<GameRuleDesignComponentDescriptor> pd, System.Type describedType)
    {
        //instead of Start(), this is the place where stuff will get initialized
        //store a bunch of size information for positioning things
        RectTransform iconArea = (RectTransform)(transform.GetChild(1));
        iconAreaBottomLeft = iconArea.offsetMin;
        iconAreaTopRight = iconArea.offsetMax; //x and y are both negative for an inset corner
        iconAreaTotalBounds = iconAreaBottomLeft - iconAreaTopRight;
        iconContainer = (RectTransform)(transform.GetChild(2));
        //throw the container out to under the canvas so that it renders over the whole rule
        iconContainer.SetParent(GameRuleDesigner.instance.uiCanvas);
        RectTransform iconContainerSpacing = (RectTransform)(iconContainer.GetChild(0));
        iconContainerSpacingSize = iconContainerSpacing.sizeDelta;
        //we only needed these to get their size, we can get rid of them now
        Destroy(iconArea.gameObject);
        Destroy(iconContainerSpacing.gameObject);

        //now we can build the popup
        transform.SetParent(GameRuleDesigner.instance.iconContainerTransform);
        //setting the parent screwed up the scale so fix it
        transform.localScale = new Vector3(1.0f, 1.0f);
        componentRepresented = cr;
        popupDescriptors = pd;
        //go through all the descriptors and add their icon to this popup
        foreach (GameRuleDesignComponentDescriptor descriptor in popupDescriptors) {
            RectTransform popupIcon;
            //int descriptors don't have icon prefabs, so we have to construct the icons
            if (descriptor is GameRuleDesignComponentIntDescriptor) {
                List<GameObject> iconList = new List<GameObject>();
                int i = ((GameRuleDesignComponentIntDescriptor)descriptor).intDescribed;
                if (i >= 0) {
                    if (describedType == typeof(GameRulePointsPlayerEffect))
                        iconList.Add(GameRuleIconStorage.instance.charPlusIcon);
                    GameRuleIconStorage.instance.addDigitIcons(i, iconList);
                } else {
                    iconList.Add(GameRuleIconStorage.instance.charMinusIcon);
                    GameRuleIconStorage.instance.addDigitIcons(-i, iconList);
                }
                //make a gameobject to hold all of the icons
                popupIcon = groupIcons(iconList);
            //almost all other descriptors we can construct normally
            //if it doesn't have an icon then it's a special case we know about
            } else if (descriptor.displayIcon == null) {
                if (descriptor is GameRuleDesignComponentClassDescriptor) {
                    System.Type otherType = ((GameRuleDesignComponentClassDescriptor)descriptor).typeDescribed;
                    //a points effect shows up as "+?"
                    if (otherType == typeof(GameRulePointsPlayerEffect))
                        popupIcon = groupIcons(new List<GameObject>(new GameObject[] {
                            GameRuleIconStorage.instance.charPlusIcon,
                            GameRuleIconStorage.instance.charQmarkIcon
                        }));
                    else
                        throw new System.Exception("Bug: no special popup icon cases for type " + otherType);
                } else
                    throw new System.Exception("Bug: no special popup icon cases for " + descriptor);
            //it has an icon, it's probably normal
            } else
                popupIcon = componentRepresented.getIconMaybeOpponent(descriptor);
            popupIcon.SetParent(iconContainer);
            popupIcons.Add(popupIcon);
        }
        //we add a "cancel" icon at the end to ensure there's room to cancel out of the popup
        RectTransform cancelIcon = instantiateIcon(GameRuleDesigner.instance.cancelIconPopupPrefab);
        cancelIcon.SetParent(iconContainer);
        popupIcons.Add(cancelIcon);

        //if we're doing a points effect, default to the "+1" choice
        if (describedType == typeof(GameRulePointsPlayerEffect))
            cr.assignDescriptor(popupDescriptors[
                GameRulePointsPlayerEffect.POINTS_SERIALIZATION_MASK - GameRulePointsPlayerEffect.POINTS_SERIALIZATION_MAX_VALUE + 1]);
        //if we're doing a fixed duration, default to the rule generator min value
        else if (describedType == typeof(GameRuleActionFixedDuration))
            cr.assignDescriptor(popupDescriptors[GameRuleGenerator.ACTION_DURATION_SECONDS_SHORTEST]);
        else
            cr.assignDescriptor(popupDescriptors[0]);
    }