Example #1
0
        public void SetBuffers2(string text)
        {
            bool foundArg = false;

            Command.ArgTypes lastArg = Command.ArgTypes.None;
            //foreach (StringDrawable sd in colors.Values)
            //{
            //    sd.Dispose();
            //}
            //colors.Clear();
            foreach (Sprite sd in AllTogether)
            {
                if (!(sd is StringDrawable))
                {
                    continue;
                }
                if (sd == selection || sd == choices)
                {
                    continue;
                }
                (sd as StringDrawable).Text = "";
            }
            int    curX = 0, curY = 0;
            string toColor    = null;
            string cmd        = "";
            int    colorLines = 0;
            int    argNumber  = 0;

            Command.ArgTypes[] argTypes = new Command.ArgTypes[] { };
            bool   isColoring           = false;
            bool   isQ       = false;
            bool   isC       = false;
            bool   wasQ      = false;
            bool   wasC      = false;
            int    index     = 0;
            string lastAdded = "";

            char[]             allDelims  = delims.Concat(delims2).ToArray();
            string             toAdd      = "";
            SortedSet <string> newSprites = new SortedSet <string>();
            Texture            texture    = null;

            start = 0;
            end   = 0;
            bool lastDelim = false;

            while (index < text.Length)
            {
                int ind = text.IndexOfAny(allDelims, index);
                if (ind == -1)
                {
                    ind = text.Length;
                }
                toAdd = text.Substring(index, ind - index);
                if (text[index] == '-' && index > 0 && allDelims.Contains(text[index - 1]))
                {
                    ind = text.IndexOfAny(allDelims, ind + 1);
                    if (ind == -1)
                    {
                        ind = text.Length;
                    }
                    toAdd = text.Substring(index, ind - index);
                }
                StringDrawable addTo = null;
                if (toAdd == "" && (!isColoring || text[index] == '\n'))
                {
                    wasQ = isQ;
                    wasC = isC;
                    isQ  = false;
                    isC  = false;
                    char dl = text[index];
                    toAdd = dl.ToString();
                    addTo = delim;
                    if (lastDelim)
                    {
                        lastAdded = "";
                    }
                    lastDelim = true;
                    if (dl == '\n')
                    {
                        argNumber = 0;
                        texture   = null;
                        if (colorLines > 0 && toColor is object && !isColoring)
                        {
                            isColoring = true;
                        }
                        else if (isColoring)
                        {
                            colorLines -= 1;
                            if (colorLines <= 0)
                            {
                                colorLines = 0;
                                toColor    = null;
                                isColoring = false;
                            }
                        }
                    }
                    else if (delims.Contains(dl))
                    {
                        argNumber += 1;
                    }
                    else
                    {
                        if (dl == '?')
                        {
                            isQ = true;
                        }
                        else if (dl == ':')
                        {
                            isC = true;
                        }
                    }
                    if (!foundArg && selection is object && selection.SelectionStart + selection.SelectionLength <= index && selection.SelectionStart <= index)
                    {
                        Command.ArgTypes type = Command.ArgTypes.Command;
                        if (argNumber > 0)
                        {
                            type = argTypes.ElementAtOrDefault(argNumber - 2);
                            if (wasQ)
                            {
                                type = Command.ArgTypes.Identifier;
                            }
                            else if (wasC)
                            {
                                type = Command.ArgTypes.Property;
                            }
                        }
                        currentChoices = GetChoices(type, newSprites, texture);
                        Choices        = currentChoices.ToList().FindAll((s) =>
                        {
                            return(s.StartsWith(lastAdded));
                        });
                        foundArg = true;
                        end      = index;
                    }
                    if (!foundArg)
                    {
                        start = index + 1;
                    }
                }
                else
                {
                    lastDelim = false;
                    lastAdded = toAdd;
                    if (isColoring)
                    {
                        ind = text.IndexOf('\n', index);
                        if (ind == -1)
                        {
                            ind = text.Length;
                        }
                        toAdd = text.Substring(index, ind - index);
                        addTo = colors[toColor];
                    }
                    else if (argNumber == 0)
                    {
                        if (commands.Contains(toAdd))
                        {
                            addTo = command;
                        }
                        else
                        {
                            addTo = white;
                        }
                        cmd      = toAdd;
                        argTypes = Command.GetArgs(cmd);
                    }
                    else
                    {
                        if (argTypes.Length >= argNumber)
                        {
                            lastArg = argTypes[argNumber - 1];
                            switch (lastArg)
                            {
                            case Command.ArgTypes.None:
                            {
                                addTo = white;
                                break;
                            }

                            case Command.ArgTypes.Int:
                            {
                                if (int.TryParse(toAdd, out int i))
                                {
                                    addTo      = number;
                                    colorLines = i;
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.Sprite:
                            {
                                addTo = isSprite(toAdd, newSprites);
                                break;
                            }

                            case Command.ArgTypes.Texture:
                            {
                                if ((texture = owner.TextureFromName(toAdd)) is object)
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.Animation:
                            {
                                if (texture is object && texture.AnimationFromName(toAdd) is object)
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.Sound:
                            {
                                if (owner.GetSound(toAdd) is object)
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.Color:
                            {
                                Color?c = owner.GetColor(toAdd, null, null);
                                if (c.HasValue)
                                {
                                    Color color = c.Value;
                                    if (!colors.ContainsKey(toAdd))
                                    {
                                        StringDrawable newColor = new StringDrawable(xPosition, yPosition, tex, "", color);
                                        newColor.Size = size;
                                        AllTogether.Add(newColor);
                                        colors.Add(toAdd, newColor);
                                    }
                                    toColor = toAdd;
                                    addTo   = colors[toAdd];
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.Bool:
                            {
                                if (bool.TryParse(toAdd, out bool b))
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.Number:
                            {
                                if (isQ)
                                {
                                    if (Command.Pointers.Contains(toAdd))
                                    {
                                        addTo = keyword2;
                                    }
                                    else
                                    {
                                        addTo = isSprite(toAdd, newSprites);
                                    }
                                }
                                else if (isC)
                                {
                                    if (Command.SpritePointers.Contains(toAdd))
                                    {
                                        addTo = keyword2;
                                    }
                                    else
                                    {
                                        addTo = white;
                                    }
                                }
                                else if (float.TryParse(toAdd, out float f))
                                {
                                    addTo = number;
                                }
                                else if (owner.Vars.TryGetValue(toAdd, out Number n))
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.Mood:
                            {
                                string ta = toAdd.ToLower();
                                if (ta == "sad" || ta == "happy")
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.Position1:
                            {
                                string ta = toAdd.ToLower();
                                if (ta == "centerx" || ta == "centery" || ta == "center")
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = isSprite(toAdd, newSprites);
                                }
                                break;
                            }

                            case Command.ArgTypes.Position2:
                            {
                                string ta = toAdd.ToLower();
                                if (ta == "above" || ta == "below")
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.AI:
                            {
                                string ta = toAdd.ToLower();
                                if (ta == "stand" || ta == "face" || ta == "follow")
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.Squeak:
                            {
                                if (Command.presetcolors.Contains(toAdd))
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = isSprite(toAdd, newSprites);
                                }
                                break;
                            }

                            case Command.ArgTypes.Script:
                            {
                                if (owner.ScriptFromName(toAdd) is object)
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.If:
                            {
                                string ta = toAdd.ToLower();
                                if (ta == "stop" || ta == "wait" || ta == "continue")
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.SpriteName:
                            {
                                newSprites.Add(toAdd);
                                addTo = keyword2;
                                break;
                            }

                            case Command.ArgTypes.PosX:
                            {
                                string ta = toAdd.ToLower();
                                if (ta == "x" || ta == "centerx" || ta == "right")
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.PosY:
                            {
                                string ta = toAdd.ToLower();
                                if (ta == "y" || ta == "centery" || ta == "bottom")
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.Music:
                            {
                                if (!owner.GetMusic(toAdd).IsNull)
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.Marker:
                            {
                                break;
                            }

                            case Command.ArgTypes.Type:
                            {
                                if (Command.Types.ContainsKey(toAdd))
                                {
                                    addTo = keyword2;
                                }
                                else
                                {
                                    addTo = white;
                                }
                                break;
                            }

                            case Command.ArgTypes.NumberFormat:
                            {
                                addTo = keyword2;
                                break;
                            }
                            }
                            if (addTo is null)
                            {
                                addTo = white;
                            }
                        }
                        else
                        {
                            addTo = white;
                        }
                    }
                }
                Point p = addTo.AddText(curX, curY, toAdd);
                index += toAdd.Length;
                curX   = p.X;
                curY   = p.Y;
            }
            if (!foundArg)
            {
                Command.ArgTypes type = Command.ArgTypes.Command;
                if (argNumber > 0)
                {
                    type = argTypes.ElementAtOrDefault(argNumber - 1);
                    if (isQ)
                    {
                        type = Command.ArgTypes.Identifier;
                    }
                    else if (isC)
                    {
                        type = Command.ArgTypes.Property;
                    }
                }
                currentChoices = GetChoices(type, newSprites, texture);
                Choices        = currentChoices.ToList().FindAll((s) =>
                {
                    return(s.StartsWith(lastAdded));
                });
                foundArg = true;
                end      = index;
            }
        }
Example #2
0
        private string[] GetChoices(Command.ArgTypes argType, SortedSet <string> createdSprites, Texture texture)
        {
            switch (argType)
            {
            case Command.ArgTypes.Command:
            {
                return(Command.CommandNames);
            }

            case Command.ArgTypes.Sprite:
            {
                SortedSet <string> spriteNames = new SortedSet <string>(createdSprites);
                foreach (string sprite in owner.UserAccessSprites.Keys)
                {
                    spriteNames.Add(sprite);
                }
                spriteNames.Add("player");
                spriteNames.Add("self");
                spriteNames.Add("target");
                return(spriteNames.ToArray());
            }

            case Command.ArgTypes.Texture:
            {
                string[] ret = new string[owner.Textures.Count];
                for (int i = 0; i < ret.Length; i++)
                {
                    ret[i] = owner.Textures.Keys[i];
                }
                return(ret);
            }

            case Command.ArgTypes.Animation:
            {
                if (texture is null)
                {
                    return new string[] { }
                }
                ;
                string[] ret = new string[texture.Animations.Count];
                for (int i = 0; i < ret.Length; i++)
                {
                    ret[i] = texture.Animations.Keys[i];
                }
                return(ret);
            }

            case Command.ArgTypes.Sound:
            {
                string[] ret = new string[owner.Sounds.Count];
                for (int i = 0; i < ret.Length; i++)
                {
                    ret[i] = owner.Sounds.Keys[i];
                }
                return(ret);
            }

            case Command.ArgTypes.Color:
            {
                SortedSet <string> ret = new SortedSet <string>(Enum.GetNames(typeof(KnownColor)));
                foreach (string clr in Game.colors.Keys)
                {
                    if (!ret.Contains(clr))
                    {
                        ret.Add(clr);
                    }
                }
                foreach (string sp in GetChoices(Command.ArgTypes.Sprite, createdSprites, texture))
                {
                    if (!ret.Contains(sp))
                    {
                        ret.Add(sp);
                    }
                }
                return(ret.ToArray());
            }

            case Command.ArgTypes.Bool:
            {
                return(new string[] { "false", "true" });
            }

            case Command.ArgTypes.Number:
            {
                return(owner.Vars.Keys.ToArray());
            }

            case Command.ArgTypes.Mood:
            {
                return(new string[] { "happy", "sad" });
            }

            case Command.ArgTypes.Position1:
            {
                SortedSet <string> ret = new SortedSet <string>(GetChoices(Command.ArgTypes.Sprite, createdSprites, texture));
                if (!ret.Contains("center"))
                {
                    ret.Add("center");
                }
                if (!ret.Contains("centerx"))
                {
                    ret.Add("centerx");
                }
                if (!ret.Contains("centery"))
                {
                    ret.Add("centery");
                }
                return(ret.ToArray());
            }

            case Command.ArgTypes.Position2:
            {
                return(new string[] { "above", "below" });
            }

            case Command.ArgTypes.AI:
            {
                return(new string[] { "face", "follow", "stand" });
            }

            case Command.ArgTypes.Squeak:
            {
                SortedSet <string> ret = new SortedSet <string>(Game.colors.Keys);
                foreach (string sp in GetChoices(Command.ArgTypes.Sprite, createdSprites, texture))
                {
                    if (!ret.Contains(sp))
                    {
                        ret.Add(sp);
                    }
                }
                return(ret.ToArray());
            }

            case Command.ArgTypes.Script:
            {
                return(owner.Scripts.Keys.ToArray());
            }

            case Command.ArgTypes.If:
            {
                return(new string[] { "continue", "stop", "wait" });
            }

            case Command.ArgTypes.PosX:
            {
                return(new string[] { "centerx", "right", "x" });
            }

            case Command.ArgTypes.PosY:
            {
                return(new string[] { "bottom", "centery", "y" });
            }

            case Command.ArgTypes.Music:
            {
                return(owner.Songs.Keys.ToArray());
            }

            case Command.ArgTypes.Identifier:
            {
                SortedSet <string> ret = new SortedSet <string>(GetChoices(Command.ArgTypes.Sprite, createdSprites, texture));
                foreach (string item in Command.Pointers)
                {
                    ret.Add(item);
                }
                return(ret.ToArray());
            }

            case Command.ArgTypes.Property:
            {
                return(Command.SpritePointers.ToArray());
            }

            case Command.ArgTypes.NumberFormat:
            {
                return(new string[] { "x.x", "x.00", "h:mm:ss", "mm:ss", "mm:ss.00" });
            }
            }
            return(new string[] { });
        }