public void GenerateStep()
        {
#if UNITY_EDITOR
            UnityEditor.Undo.RecordObject(this.gameObject, "Generation Step");
            UnityEditor.Undo.RecordObject(this, "Generation Step");
            UnityEditor.Undo.RecordObject(patternView, "Generation Step");
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(this.gameObject);
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(this);
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(patternView);
#endif
            // we execute until we have visible change
            for (int i = 0; i < 100; i++)
            {
                GenerationStepResult result = GenerationStepInternal();
                if (result == GenerationStepResult.EndOfFile)
                {
                    return;
                }
                if (result == GenerationStepResult.VisibleChange)
                {
                    break;
                }
            }
            PatternView.UpdateView();
        }
        public void InitializeGeneration()
        {
#if UNITY_EDITOR
            UnityEditor.Undo.RecordObject(this.gameObject, "Intialize Generation");
            UnityEditor.Undo.RecordObject(this, "Intialize Generation");
            UnityEditor.Undo.RecordObject(patternView, "Intialize Generation");
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(this.gameObject);
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(this);
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(patternView);
#endif
            if (increaseSeed)
            {
                seed++;
            }

            patternView.pattern = new Pattern(startSize, Tile.Empty);
            ReplacementEngine re = GetComponent <ReplacementEngine>();
            re.ResetGeneration(seed);
            PatternView.UpdateView();

            ParseInstructions();
            currentInstruction = 0;
            applicationCounter = 0;
            gotoStack          = new Stack <ProgramState>();
        }
示例#3
0
 private void OnValidate()
 {
     // assign pattern view automatically
     if (patternView == null)
     {
         patternView = GetComponent <PatternView>();
     }
 }
示例#4
0
        public void DoSubdividision(Vector3Int delta)
        {
            PatternView patternView = GetComponent <PatternView>();
            Pattern     mainPattern = patternView.pattern;

            patternView.Subdivide(delta);

            // erases matches and will trigger new search
            matches = null;
        }
示例#5
0
        public void GenerateStep()
        {
#if UNITY_EDITOR
            UnityEditor.Undo.RecordObject(this.gameObject, "Generation Step");
            UnityEditor.Undo.RecordObject(this, "Generation Step");
            UnityEditor.Undo.RecordObject(patternView, "Generation Step");
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(this.gameObject);
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(this);
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(patternView);
#endif

            ReplacementEngine re = GetComponent <ReplacementEngine>();
            re.ReplaceMatch();
            PatternView.UpdateView();
        }
示例#6
0
 private void OnDrawGizmos()
 {
     if (showDebugInformation)
     {
         PatternView pv = GetComponent <PatternView>();
         if (matches != null)
         {
             foreach (Match m in matches)
             {
                 Gizmos.color = Color.yellow;
                 Vector3 p1 = pv.GetPositionInWorldSpace(m.position, pv.displayDelta);
                 Vector3 p2 = pv.GetPositionInWorldSpace(m.position + m.rule.leftSide.Size - Vector3Int.one, pv.displayDelta);
                 Gizmos.DrawWireCube((p1 + p2) / 2, (p2 - p1) + pv.displayDelta);
                 Gizmos.color = Preferences.RoomLabelColor;
                 GUI.color    = Preferences.RoomLabelColor;
                 Utils.DrawLabel(m.rule.name, p1);
             }
         }
     }
 }
        public override void OnInspectorGUI()
        {
            PatternView patternView = (PatternView)target;

            DrawDefaultInspector();

            // draw a custom int vector UI
            EditorGUILayout.BeginHorizontal();
            float previousLabelWidth = EditorGUIUtility.labelWidth;
            float previousFieldWidth = EditorGUIUtility.fieldWidth;

            EditorGUIUtility.labelWidth = 1f;
            EditorGUIUtility.fieldWidth = 1f;
            EditorGUILayout.LabelField("Size:");
            EditorGUIUtility.labelWidth = 12f;

            EditorGUI.BeginChangeCheck();

            int x = EditorGUILayout.IntField("X", patternView.pattern.Size.x);
            int y = EditorGUILayout.IntField("Y", patternView.pattern.Size.y);
            int z = EditorGUILayout.IntField("Z", patternView.pattern.Size.z);


            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(target, "Changed Pattern");
                PrefabUtility.RecordPrefabInstancePropertyModifications(target);
                patternView.pattern.Size = new Vector3Int(x, y, z);
            }

            EditorGUIUtility.labelWidth = previousLabelWidth;
            EditorGUIUtility.fieldWidth = previousFieldWidth;
            EditorGUILayout.EndHorizontal();

            // Button for test pattern generation
            if (GUI.Button(EditorGUILayout.GetControlRect(), "Set test pattern"))
            {
                patternView.GenerateTestPattern();
            }
        }
        // method called from PatternView
        public void OnPatternChange()
        {
            PatternView patternView = GetComponent <PatternView>();
            Pattern     pattern     = patternView.pattern;
            GameObject  container   = Utils.CreateChildWithName(transform, "[GeneratedRooms]");

#if !UNITY_EDITOR
            if (executeInEditorOnly)
            {
                return;
            }
#endif
            foreach (Vector3Int position in Utils.IterateGrid3D(pattern.Size))
            {
                string[] tags = pattern.GetTile(position).Label.Split('_');
                if (tags.Length < 2)
                {
                    if (logWarnings)
                    {
                        Debug.LogWarning("Could not parse tile label: Label has to consist of at least two tags separated by an underscore.", gameObject);
                    }
                    continue;
                }
                string lastTag = tags[tags.Length - 1];

                if (lastTag == "")
                {
                    continue;
                }
                int rotation;
                try
                {
                    rotation = int.Parse(lastTag);
                }
                catch (System.FormatException)
                {
                    if (logWarnings)
                    {
                        Debug.LogWarning("Could not parse tile label: Last tag in label is not an integer.", gameObject);
                    }
                    continue;
                }
                catch (System.OverflowException)
                {
                    if (logWarnings)
                    {
                        Debug.LogWarning("Could not parse tile label: The rotation is fractional or out of the integer number range.", gameObject);
                    }
                    continue;
                }

                GameObject[] prefabs = prefabDictionary.ToPrefabs(tags);
                for (int i = 0; i < prefabs.Length - 1; i++)
                {
                    GameObject prefab = prefabs[i];

                    if (prefab == null)
                    {
                        if (logWarnings)
                        {
                            Debug.LogWarning("Could not parse tile label: Specified tag \"" + tags[0] + "\"does not exist.");
                        }
                        continue;
                    }

                    GameObject instance = Instantiate(
                        prefab,
                        patternView.GetPositionInWorldSpace(position, patternView.displayDelta),
                        Quaternion.Euler(0, rotation * 90, 0)
                        );
                    instance.transform.SetParent(container.transform, true);
                }
            }
        }
        public override void OnInspectorGUI()
        {
            // Rule editor GUI
            internalOptionsToggleState = EditorGUILayout.Foldout(internalOptionsToggleState, "Rule editor settings");

            if (internalOptionsToggleState)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(matchPatternView);
                EditorGUILayout.PropertyField(replacementPatternView);
                EditorGUI.indentLevel--;
            }

            serializedObject.ApplyModifiedProperties();

            EditorGUILayout.LabelField("Current Rule: ", EditorStyles.boldLabel);

            // Rule GUI
            RuleEditor  ruleEditor = (RuleEditor)target;
            RuleSet     ruleSet    = ruleEditor.GetComponent <RuleSet>();
            List <Rule> rules      = ruleSet.rules;

            bool ruleIndexChanged = previousRuleIndex != ruleSet.selectedRuleIndex;

            previousRuleIndex = ruleSet.selectedRuleIndex;

            if (ruleSet.selectedRuleIndex >= 0 && ruleSet.selectedRuleIndex < ruleSet.rules.Count)
            {
                Rule currentRule = rules[ruleSet.selectedRuleIndex];

                Undo.RecordObject(ruleSet, "Edit Rule");
                EditorGUI.BeginChangeCheck();

                // some extra fields
                currentRule.name   = EditorGUILayout.TextField("Rule name", currentRule.name);
                currentRule.weight = EditorGUILayout.FloatField("Rule weight", currentRule.weight);
                currentRule.maximumApplications = EditorGUILayout.IntField("Maximum applications", currentRule.maximumApplications);
                currentRule.waitSteps           = EditorGUILayout.IntField("Wait steps", currentRule.waitSteps);

                // rotation
                currentRule.strictRotation = EditorGUILayout.BeginToggleGroup("Obey rule orientation", currentRule.strictRotation);
                bool doRotation = GUILayout.Button("Rotate Rule");
                EditorGUILayout.EndToggleGroup();

                // reverse
                currentRule.reverseApplication = EditorGUILayout.Toggle("Reversible", currentRule.reverseApplication);

                if (EditorGUI.EndChangeCheck())
                {
                    PrefabUtility.RecordPrefabInstancePropertyModifications(ruleSet);
                }

                PatternView leftPatternView = ((PatternView)matchPatternView.objectReferenceValue);
                Undo.RecordObject(leftPatternView, "Edit Rule");
                // text areas for rule editing
                try{
                    string leftSideText = SerializedRule.PatternToString(currentRule.leftSide);
                    EditorGUI.BeginChangeCheck();
                    EditorGUILayout.PrefixLabel(new GUIContent("Match: ", "String representation of left side (match). \";\", \",\", \" \" are separators for x, y, z dimension"));
                    leftSideText = EditorGUILayout.TextField(leftSideText, GUILayout.ExpandHeight(true));
                    if (EditorGUI.EndChangeCheck() || ruleIndexChanged)
                    {
                        currentRule.leftSide = SerializedRule.StringToPattern(leftSideText);
                        if (doRotation)
                        {
                            currentRule.leftSide = Pattern.Rotate90Y(currentRule.leftSide);
                        }
                        leftPatternView.pattern = currentRule.leftSide;
                        PrefabUtility.RecordPrefabInstancePropertyModifications(leftPatternView);
                    }
                }
                catch (System.ArgumentException) {
                    // parsing failed, do nothing
                }

                PatternView rightPatternView = ((PatternView)replacementPatternView.objectReferenceValue);
                Undo.RecordObject(rightPatternView, "Edit Rule");
                try{
                    string rightSideText = SerializedRule.PatternToString(currentRule.rightSide);
                    EditorGUI.BeginChangeCheck();
                    EditorGUILayout.PrefixLabel(new GUIContent("Replacement: ", "String representation of right side (replacement). \";\", \",\", \" \" are separators for x, y, z dimension"));
                    rightSideText = EditorGUILayout.TextField(rightSideText, GUILayout.ExpandHeight(true));
                    if (EditorGUI.EndChangeCheck() || ruleIndexChanged)
                    {
                        currentRule.rightSide = SerializedRule.StringToPattern(rightSideText);
                        if (doRotation)
                        {
                            currentRule.rightSide = Pattern.Rotate90Y(currentRule.rightSide);
                        }
                        rightPatternView.pattern = currentRule.rightSide;
                        PrefabUtility.RecordPrefabInstancePropertyModifications(rightPatternView);
                    }
                }
                catch (System.ArgumentException) {
                    // parsing failed, do nothing
                }
            }
            else
            {
                EditorGUILayout.HelpBox("Please select a rule in the RuleSet component.", MessageType.Info);
            }
        }