// ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    public override MadLevelIcon GetIcon(string levelName)
    {
        MadDebug.Assert(!string.IsNullOrEmpty(levelName), "null or empty level name");
        return(MadTransform.FindChild <MadLevelIcon>(draggable.transform, (icon) => {
            return MadGameObject.IsActive(icon.gameObject) && icon.level.name == levelName;
        }, 0));
    }
Exemple #2
0
    /// <summary>
    /// Gets all active icons available in the layout.
    /// </summary>
    /// <returns></returns>
    public MadLevelIcon[] GetAllIcons()
    {
        // find under the page
        var icons = MadTransform.FindChildren <MadLevelIcon>(
            transform, (ic) => MadGameObject.IsActive(ic.gameObject), 3);

        return(icons.ToArray());
    }
Exemple #3
0
    static void SlideSetActive(MadSprite icon, bool act)
    {
        bool currentlyActive = MadGameObject.IsActive(icon.gameObject);

        if (currentlyActive != act)
        {
            MadGameObject.SetActive(icon.gameObject, act);
        }
    }
Exemple #4
0
    static void SlideSetActive(MadSprite icon, bool act)
    {
        if (!MadGameObject.IsActive(icon.gameObject))
        {
            MadGameObject.SetActive(icon.gameObject, true);
        }

        bool currentlyActive = icon.visible;

        if (currentlyActive != act)
        {
            icon.visible = act;
        }
    }
#pragma warning disable 429, 162
    private void Unity5Check()
    {
        // generated check
        // ReSharper disable once ConditionIsAlwaysTrueOrFalse
        if ("MadLevelManager" != ("_NAME" + "SPACE_") && "MadLevelManager" != "MadLevelManager")
        {
            return;
        }

        if (Application.isPlaying)
        {
            return;
        }

#if UNITY_EDITOR
        int majorVersion;
        if (int.TryParse(Application.unityVersion.Split('.')[0], out majorVersion))
        {
            if (majorVersion >= 5 && renderMode == RenderMode.Legacy)
            {
                var nonAtlasSprite = MadTransform.FindChild <MadSprite>(transform,
                                                                        (s) => {
                    if (s is MadText)
                    {
                        return(((MadText)s).atlas == null);
                    }
                    else
                    {
                        return(s.inputType == MadSprite.InputType.SingleTexture && MadGameObject.IsActive(s.gameObject));
                    }
                });
                if (nonAtlasSprite != null)
                {
                    bool sw = EditorUtility.DisplayDialog(
                        "Unity 5 Compatibility",
                        "With Unity 5 is recommended to switch to Depth-Based render mode or Legacy mode with atlases only. " +
                        "Please see Unity 5 Compatibility docmentation page for more information. " +
                        "Do you want to switch to Depth-Based mode?\n\n",
                        "Yes (Recommended)", "No");
                    if (sw)
                    {
                        renderMode = RenderMode.DepthBased;
                    }
                }
            }
        }
#endif
    }
Exemple #6
0
    private static bool SceneHasOnlyMainCamera()
    {
        bool hasOtherObjects = false;
        bool hasMainCamera   = false;

        var allObjects = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[];

        foreach (var obj in allObjects)
        {
            if (MadGameObject.IsActive(obj))
            {
                if (obj.name == "Main Camera")
                {
                    hasMainCamera = true;
                }
                else
                {
                    hasOtherObjects = true;
                }
            }
        }

        return(hasMainCamera && !hasOtherObjects);
    }
    void Build()
    {
        List <MadLevelIcon> allIcons = new List <MadLevelIcon>();

        allIcons.AddRange(MadTransform.FindChildren <MadLevelIcon>(transform));

        int     levelCount    = currentConfiguration.LevelCount(MadLevel.Type.Level, configurationGroup);
        Vector2 currentOffset = Vector2.zero;

        MadLevelIcon previousIcon = null;

        // find out min and max depth
        int min, max;

        iconTemplate.MinMaxDepthRecursively(out min, out max);
        const string name = "level {0:D3}";

        HashSet <MadLevelIcon> activeIcons = new HashSet <MadLevelIcon>();

        for (int levelIndex = 0; levelIndex < levelCount; ++levelIndex)
        {
            MadLevelIcon levelIcon = MadTransform.FindChild <MadLevelIcon>(
                draggable.transform, (ic) => ic.levelIndex == levelIndex, 0);
            bool newInstance = levelIcon == null;

            // create new icon instance if it's not exists
            if (newInstance)
            {
                levelIcon = CreateIcon(draggable.transform, string.Format(name, levelIndex + 1), iconTemplate);

                // position & scale
                levelIcon.pivotPoint           = MadSprite.PivotPoint.Center;
                levelIcon.transform.localScale = Vector3.one;

                do
                {
                    levelIcon.transform.localPosition = currentOffset;
                    currentOffset += offset;
                } while (Collides(levelIcon, allIcons));

                allIcons.Add(levelIcon);
            }

            // make it active if deactivated
            if (!MadGameObject.IsActive(levelIcon.gameObject))
            {
                MadGameObject.SetActive(levelIcon.gameObject, true);
            }

            // setup level properties
            levelIcon.levelGroup            = configurationGroup;
            levelIcon.levelIndex            = levelIndex;
            levelIcon.configuration         = currentConfiguration;
            levelIcon.hasLevelConfiguration = true;

            // set level number if exists
            if (levelIcon.levelNumber != null)
            {
                levelIcon.levelNumber.text = GetEnumerationValue(levelIndex);
            }

            // level unlock if set
            if (previousIcon != null)
            {
                if (newInstance)
                {
                    previousIcon.unlockOnComplete.Add(levelIcon);
                }
            }
            else
            {
                levelIcon.locked = false;
            }

            if (!Application.isPlaying || !MadLevelProfile.IsLockedSet(levelIcon.level.name))
            {
                levelIcon.locked = levelIcon.level.lockedByDefault;
            }

            previousIcon = levelIcon;

            activeIcons.Add(levelIcon);
        }

        BuildBackgroundTexture();

        DeactivateAllOther(activeIcons);
    }
    /// <summary>
    /// Will replace all icons in the layout with selected icon. Position, scale and rotation will be preserved.
    /// This method is meant for editor-use only.
    /// </summary>
    /// <param name="newIcon"></param>
    public void ReplaceIcons(GameObject newIcon)
    {
        if (Application.isPlaying)
        {
            Debug.LogError("This method can be called only from the editor");
            return;
        }

        MadUndo.LegacyRegisterSceneUndo("Replaced Icons");

        var icons       = MadTransform.FindChildren <MadLevelIcon>(draggable.transform);
        var activeIcons = from i in icons where MadGameObject.IsActive(i.gameObject) select i;

        // keep track of unlock on complete settings
        Dictionary <int, List <int> > unlockOnCompleteDict = new Dictionary <int, List <int> >();
        List <MadLevelIcon>           createdIcons         = new List <MadLevelIcon>();

        foreach (var icon in activeIcons)
        {
            var position      = icon.transform.position;
            var rotation      = icon.transform.rotation;
            var localScale    = icon.transform.localScale;
            var name          = icon.name;
            var baseDepth     = icon.guiDepth;
            var levelIndex    = icon.levelIndex;
            var configuration = icon.configuration;

            // build unlock on complete dict
            List <int> toUnlockList = new List <int>();
            foreach (var unlock in icon.unlockOnComplete)
            {
                toUnlockList.Add(unlock.levelIndex);
            }
            unlockOnCompleteDict[icon.levelIndex] = toUnlockList;


            MadUndo.DestroyObjectImmediate(icon.gameObject);

            var nIcon = CreateIcon(draggable.transform, name, iconTemplate);
            nIcon.transform.position    = position;
            nIcon.transform.rotation    = rotation;
            nIcon.transform.localScale  = localScale;
            nIcon.guiDepth              = baseDepth;
            nIcon.levelIndex            = levelIndex;
            nIcon.configuration         = configuration;
            nIcon.hasLevelConfiguration = true;

            createdIcons.Add(nIcon);


            var childSprites = MadTransform.FindChildren <MadSprite>(nIcon.transform);
            foreach (var cs in childSprites)
            {
                cs.guiDepth += baseDepth;
            }

            MadUndo.RegisterCreatedObjectUndo(nIcon.gameObject, "Replaced Icons");
        }

        icons = MadTransform.FindChildren <MadLevelIcon>(draggable.transform);

        // apply unlock on complete list
        foreach (var icon in createdIcons)
        {
            List <int> unlockList = unlockOnCompleteDict[icon.levelIndex];
            foreach (var unlockLevelIndex in unlockList)
            {
                var          query        = from i in icons where i.levelIndex == unlockLevelIndex select i;
                MadLevelIcon iconToUnlock = query.First();
                icon.unlockOnComplete.Add(iconToUnlock);
            }
        }
    }
Exemple #9
0
    // builds level icons that are absent now
    void Build()
    {
        // create or get a draggable
        draggable = MadTransform.GetOrCreateChild <MadDragStopDraggable>(transform, "Draggable");

        draggable.dragStopCallback = (index) => {
            pageCurrentIndex = index;
        };

        float startX = -pixelsWidth / 2;
        float startY = pixelsHeight / 2;

        float dx = pixelsWidth / (gridWidth + 1);
        float dy = -pixelsHeight / (gridHeight + 1);

        int levelNumber = 1;

        MadLevelIcon previousIcon = null;

        int levelCount = configuration.LevelCount(MadLevel.Type.Level);
        int levelIndex = 0;

        int pageIndex = 0;

        while (levelIndex < levelCount)
        {
            Transform page = MadTransform.FindChild <Transform>(draggable.transform, (t) => t.name == "Page " + (pageIndex + 1));
            bool      createPageInstance = page == null;

            if (createPageInstance)
            {
                page = MadTransform.CreateChild <Transform>(draggable.transform, "Page " + (pageIndex + 1));
            }

            if (createPageInstance || generate)
            {
                page.localPosition = new Vector3(pagesXOffset * pageIndex, 0, 0);
            }


            for (int y = 1; y <= gridHeight && levelIndex < levelCount; ++y)
            {
                for (int x = 1; x <= gridWidth && levelIndex < levelCount; ++x, levelIndex++)
                {
                    // update info: in previous versions page was nested directly under draggable
                    // now they should be placed inside "Page X" transforms
                    MadLevelIcon levelIcon;

                    // look directly under Draggable
                    levelIcon = MadTransform.FindChild <MadLevelIcon>(
                        draggable.transform, (ic) => ic.levelIndex == levelIndex, 0);
                    if (levelIcon != null)
                    {
                        // move to page
                        levelIcon.transform.parent = page;
                    }
                    else
                    {
                        // find under the page
                        levelIcon = MadTransform.FindChild <MadLevelIcon>(
                            page.transform, (ic) => ic.levelIndex == levelIndex, 0);
                    }

                    bool createNewInstance = levelIcon == null;
                    var  level             = configuration.GetLevel(MadLevel.Type.Level, levelIndex);

                    if (createNewInstance)
                    {
                        levelIcon = MadTransform.CreateChild(
                            page.transform, level.name, iconTemplate);
                    }
                    else
                    {
                        levelIcon.name = level.name;
                    }

                    levelIcon.gameObject.hideFlags = generate ? HideFlags.HideInHierarchy : 0;

                    if (!MadGameObject.IsActive(levelIcon.gameObject))
                    {
                        MadGameObject.SetActive(levelIcon.gameObject, true);
                    }

                    levelIcon.levelIndex            = levelIndex;
                    levelIcon.configuration         = configuration;
                    levelIcon.hasLevelConfiguration = true;

                    if (generate || createNewInstance)
                    {
                        levelIcon.pivotPoint = MadSprite.PivotPoint.Center;

                        levelIcon.transform.localPosition =
                            new Vector3(startX + dx * x + iconOffset.x, startY + dy * y + iconOffset.y, 0);

                        levelIcon.transform.localScale = new Vector3(iconScale.x, iconScale.y, 1);

                        if (levelIcon.levelNumber != null)
                        {
                            levelIcon.levelNumber.text = levelNumber.ToString();
                        }
                    }

                    if (previousIcon != null)
                    {
                        if (createNewInstance)
                        {
                            previousIcon.unlockOnComplete.Add(levelIcon);
                        }
                    }
                    else
                    {
                        if (generate)
                        {
                            levelIcon.locked = false;
                        }
                    }

                    previousIcon = levelIcon;
                    levelNumber++;
                }
            }

            pageIndex++;
        }

        BuildSlideIcons();
        BuildDragging(draggable, (int)Mathf.Ceil((float)levelCount / (gridWidth * gridHeight)));

        // enable/disable selection based on hide settings
        var sprites = GetComponentsInChildren <MadSprite>();

        foreach (var sprite in sprites)
        {
            sprite.editorSelectable = !generate;
        }
    }
Exemple #10
0
    public override void OnInspectorGUI()
    {
        if (levelIcon.generated && SetupMethodGenerate())
        {
            if (MadGUI.WarningFix("This icon instance has been generated. If you want to modify this icon, "
                                  + "please switch your Setup Method to Manual or change the template.", "Help"))
            {
                Application.OpenURL(MadLevelHelp.IconGenerated);
            }

            GUI.enabled = false;
        }

        if (MadGameObject.IsActive(levelIcon.gameObject))
        {
            Properties();
        }
        else
        {
            MadGUI.Warning("Not all functions are available if this object is disabled! Before editing please enable this game object!");
        }

        MadGUI.BeginBox("Visibility");

        MadGUI.Indent(() => {
            EditorGUILayout.Space();

            CheckPropertyError(levelIcon.showWhenLevelLocked);
            GUILayout.Label("Show when level is locked");
            ArrayFor(levelIcon.showWhenLevelLocked);

            CheckPropertyError(levelIcon.showWhenLevelUnlocked);
            GUILayout.Label("Show when level is unlocked");
            ArrayFor(levelIcon.showWhenLevelUnlocked);

            CheckPropertyError(levelIcon.showWhenLevelCompleted);
            GUILayout.Label("Show when level is completed");
            ArrayFor(levelIcon.showWhenLevelCompleted);

            CheckPropertyError(levelIcon.showWhenLevelNotCompleted);
            GUILayout.Label("Show when level is not completed");
            ArrayFor(levelIcon.showWhenLevelNotCompleted);

            CheckConflictError();

            serializedObject.UpdateIfDirtyOrScript();
            MadGUI.PropertyField(canFocusIfLocked, "Can Focus If Locked");

            serializedObject.ApplyModifiedProperties();

            if (levelIcon.generated)
            {
                serializedObject.UpdateIfDirtyOrScript();
                if (MadGUI.Foldout("Unlock On Complete", false))
                {
                    var arrayList = new MadGUI.ArrayList <MadLevelIcon>(
                        unlockOnComplete, (p) => { MadGUI.PropertyField(p, ""); });
                    arrayList.Draw();
                }
                serializedObject.ApplyModifiedProperties();
            }
        });
        MadGUI.EndBox();

        if (levelIcon.completedProperty != null || levelIcon.lockedProperty != null)
        {
            SectionSecialProperties();
        }

        MadGUI.BeginBox("Sprite");
        MadGUI.Indent(() => {
            SectionSprite();
        });
        MadGUI.EndBox();
    }
Exemple #11
0
    // builds level icons that are absent now
    private void Build(bool forceDelete)
    {
        // create or get a draggable
        draggable = MadTransform.GetOrCreateChild <MadDragStopDraggable>(transform, "Draggable");

        draggable.dragStopCallback = OnDragStopCallback;

        float startX = -pixelsWidth / 2;
        float startY = pixelsHeight / 2;

        float dx = pixelsWidth / (gridWidth + 1);
        float dy = -pixelsHeight / (gridHeight + 1);

        MadLevelIcon previousIcon = null;

        int levelCount = currentConfiguration.LevelCount(MadLevel.Type.Level, configurationGroup);
        int levelIndex = 0;

        int pageIndex = 0;

        while (levelIndex < levelCount)
        {
            Transform page = MadTransform.FindChild <Transform>(draggable.transform,
                                                                (t) => t.name == "Page " + (pageIndex + 1));
            bool createPageInstance = page == null;

            if (createPageInstance)
            {
                page           = MadTransform.CreateChild <Transform>(draggable.transform, "Page " + (pageIndex + 1));
                page.hideFlags = generate && hideManagedObjects ? HideFlags.HideInHierarchy : 0;
            }

            Transform pageAnchor = MadTransform.FindChild <Transform>(draggable.transform,
                                                                      (t) => t.name == "Anchor " + (pageIndex + 1));
            if (pageAnchor == null)
            {
                pageAnchor = MadTransform.CreateChild <Transform>(draggable.transform, "Anchor " + (pageIndex + 1));
                pageAnchor.gameObject.AddComponent <MadFollow>();
            }

            var followPage = pageAnchor.GetComponent <MadFollow>();
            followPage.followTransform = page;

            if (createPageInstance || generate)
            {
                switch (pagingMethod)
                {
                case PagingMethod.HorizontalSimple:
                    page.localPosition = ComputePageOffsetHoriz(pageIndex);
                    break;

                case PagingMethod.VerticalSimple:
                    page.localPosition = ComputePageOffsetVert(pageIndex);
                    break;

                case PagingMethod.HorizontalZoom:
                    page.localPosition = ComputePageOffsetHoriz(pageIndex);
                    break;

                case PagingMethod.VerticalZoom:
                    page.localPosition = ComputePageOffsetVert(pageIndex);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            int  levelPageIndex = 0;
            bool stop           = false;
            for (int y = 1; y <= gridHeight && levelIndex < levelCount; ++y)
            {
                if (stop)
                {
                    break;
                }

                for (int x = 1; x <= gridWidth && levelIndex < levelCount; ++x, levelIndex++, levelPageIndex++)
                {
                    if (limitLevelsPerPage && generate && levelPageIndex == levelsPerPage)
                    {
                        stop = true;
                        break;
                    }

                    // update info: in previous versions page was nested directly under draggable
                    // now they should be placed inside "Page X" transforms
                    MadLevelIcon levelIcon = null;

                    if (!forceDelete)
                    {
                        // look directly under Draggable
                        levelIcon = MadTransform.FindChild <MadLevelIcon>(
                            draggable.transform, (ic) => ic.levelIndex == levelIndex, 0);
                        if (levelIcon != null)
                        {
                            // move to page
                            levelIcon.transform.parent = page;
                        }
                        else
                        {
                            // find under the page
                            levelIcon = MadTransform.FindChild <MadLevelIcon>(
                                page.transform, (ic) => ic.levelIndex == levelIndex, 0);
                        }
                    }

                    var  level             = currentConfiguration.GetLevel(MadLevel.Type.Level, configurationGroup, levelIndex);
                    bool createNewInstance = levelIcon == null;

                    if (createNewInstance)
                    {
                        levelIcon = CreateIcon(
                            page.transform, level.name, iconTemplate);
                    }
                    else
                    {
                        levelIcon.name = level.name;
                    }

                    levelIcon.gameObject.hideFlags = generate && hideManagedObjects ? HideFlags.HideInHierarchy : 0;

                    levelIcon.levelGroup            = configurationGroup;
                    levelIcon.levelIndex            = levelIndex;
                    levelIcon.configuration         = currentConfiguration;
                    levelIcon.hasLevelConfiguration = true;

                    if (!MadGameObject.IsActive(levelIcon.gameObject))
                    {
                        MadGameObject.SetActive(levelIcon.gameObject, true);
                    }
                    else
                    {
                        // re-enable icon to reload its properties
                        MadGameObject.SetActive(levelIcon.gameObject, false);
                        MadGameObject.SetActive(levelIcon.gameObject, true);
                    }

                    if (generate || createNewInstance)
                    {
                        levelIcon.pivotPoint = MadSprite.PivotPoint.Center;

                        if (!generate)
                        {
                            levelIcon.transform.localPosition =
                                new Vector3(startX + dx * x + iconOffset.x, startY + dy * y + iconOffset.y, 0);
                        }
                        else
                        {
                            levelIcon.transform.localPosition = IconGeneratedPosition(levelIndex, levelCount, x - 1,
                                                                                      y - 1);
                        }

                        levelIcon.transform.localScale = new Vector3(iconScale.x, iconScale.y, 1);

                        if (levelIcon.levelNumber != null)
                        {
                            levelIcon.levelNumber.text = GetEnumerationValue(levelIndex);
                        }
                    }

                    if (previousIcon != null)
                    {
                        if (createNewInstance)
                        {
                            previousIcon.unlockOnComplete.Add(levelIcon);
                        }
                    }

                    if (!Application.isPlaying || !MadLevelProfile.IsLockedSet(level.name))
                    {
                        levelIcon.locked = levelIcon.level.lockedByDefault;
                    }

                    previousIcon = levelIcon;
                }
            }

            pageIndex++;
        }

        BuildSlideIcons();
        BuildDragging();

        // enable/disable selection based on hide settings
        var sprites = GetComponentsInChildren <MadSprite>();

        foreach (var sprite in sprites)
        {
            sprite.editorSelectable = !generate;
        }

#if UNITY_EDITOR
        EditorApplication.delayCall += () => EditorUtility.SetDirty(this);
#endif
    }
Exemple #12
0
    void Build()
    {
        int     levelCount    = configuration.LevelCount(MadLevel.Type.Level);
        Vector2 currentOffset = Vector2.zero;

        MadLevelIcon previousIcon = null;

        // find out min and max depth
        int min, max;

        iconTemplate.MinMaxDepthRecursively(out min, out max);
        int depthDiff = (max - min) + 1;

        const string name = "level {0:D3}";

        for (int levelIndex = 0; levelIndex < levelCount; ++levelIndex)
        {
            MadLevelIcon levelIcon = MadTransform.FindChild <MadLevelIcon>(
                draggable.transform, (ic) => ic.levelIndex == levelIndex, 0);
            bool newInstance = levelIcon == null;

            // create new icon instance if it's not exists
            if (newInstance)
            {
                levelIcon = MadTransform.CreateChild(
                    draggable.transform, string.Format(name, levelIndex + 1), iconTemplate);

                // adjust gui depth for each icon
                levelIcon.guiDepth += levelIndex * depthDiff;
                var sprites = MadTransform.FindChildren <MadSprite>(levelIcon.transform);
                foreach (var sprite in sprites)
                {
                    sprite.guiDepth += levelIndex * depthDiff;
                }

                // position & scale
                levelIcon.pivotPoint = MadSprite.PivotPoint.Center;
                levelIcon.transform.localPosition = currentOffset;
                currentOffset += offset;

                levelIcon.transform.localScale = Vector3.one;
            }

            // make it active if deactivated
            if (!MadGameObject.IsActive(levelIcon.gameObject))
            {
                MadGameObject.SetActive(levelIcon.gameObject, true);
            }

            // setup level properties
            levelIcon.levelIndex            = levelIndex;
            levelIcon.configuration         = configuration;
            levelIcon.hasLevelConfiguration = true;

            // set level number if exists
            if (levelIcon.levelNumber != null)
            {
                levelIcon.levelNumber.text = (levelIndex + 1).ToString();
            }

            // level unlock if set
            if (previousIcon != null)
            {
                if (newInstance)
                {
                    previousIcon.unlockOnComplete.Add(levelIcon);
                }
            }
            else
            {
                levelIcon.locked = false;
            }

            previousIcon = levelIcon;
        }

        BuildBackgroundTexture();
    }