Beispiel #1
0
        internal void DrawOption(object sender, DrawEventArgs e)
        {
            int idx = (int)e.Data;

            if (Options.Count == 0)
            {
                return;
            }

            OptionDrawingObject _option = Options [idx];

            e.Context.SetPangoLargeFontSize();

            DrawObjects(e.Context, _option.DrawingObjects, idx);
        }
Beispiel #2
0
        public int GetOptionCorrectAnswerIndex()
        {
            if (options == null || options.Count < 0)
            {
                return(-1);
            }

            for (int i = 0; i < options.Count; i++)
            {
                OptionDrawingObject option = options [i];
                if (option.Correct == true)
                {
                    return(i);
                }
            }
            throw new InvalidOperationException("GameXmlDrawing.GetOptionCorrectAnswerIndex. Invalid value.");
        }
Beispiel #3
0
        void RandomizeOptions(DrawingObject [] drawing_objects, int randomized_options)
        {
            OptionDrawingObject option;

            // Randomize the order of the options
            if (randomized_options > 0)
            {
                OptionDrawingObject [] originals;
                ArrayListIndicesRandom random_indices;
                int index = 0;

                random_indices = new ArrayListIndicesRandom(randomized_options);
                originals      = new OptionDrawingObject [randomized_options];
                random_indices.Initialize();

                // Backup originals
                for (int i = 0; i < drawing_objects.Length; i++)
                {
                    option = drawing_objects[i] as OptionDrawingObject;

                    if (option == null)
                    {
                        continue;
                    }

                    originals[index] = option.Copy();
                    index++;
                }

                // Swap
                index = 0;
                for (int i = 0; i < drawing_objects.Length; i++)
                {
                    option = drawing_objects[i] as OptionDrawingObject;

                    if (option == null)
                    {
                        continue;
                    }

                    option.CopyRandomizedProperties(originals [random_indices [index]]);
                    index++;
                }
            }
        }
Beispiel #4
0
        // Deep copy
        public OptionDrawingObject Copy()
        {
            OptionDrawingObject option = new OptionDrawingObject();

            option.X               = X;
            option.Y               = Y;
            option.Width           = Width;
            option.Height          = Height;
            option.AnswerText      = AnswerText;
            option.Correct         = Correct;
            option.RandomizedOrder = RandomizedOrder;

            foreach (DrawingObject obj in DrawingObjects)
            {
                option.AddDrawingObject(obj);
            }

            return(option);
        }
Beispiel #5
0
        public void Read(string file)
        {
            GameXmlDefinition game;
            string name, str, plural;
            bool processing_variant = false;
            int variant = 0;
            OptionDrawingObject option = null;
            DrawingObject last_drawing_object = null;
            string last_context = null;

            if (read == true)
                return;

            try
            {
                StreamReader stream = new StreamReader (file);
                XmlTextReaderLiteral reader = new XmlTextReaderLiteral (stream);
                game = null;

                while (reader.Read ())
                {
                    // Strings are only used because msgctxt requirement
                    if (reader.NodeType == XmlNodeType.Text)
                    {
                        const string CONTEXT_GLUE = "\u0004";
                        string text;

                        text = reader.ReadString ();

                        TextDrawingObject drawing_object = (TextDrawingObject) last_drawing_object;
                        // GetText processes msgctxt as msgctxt + context_glue + text to retrieve them
                        drawing_object.Text = last_context + CONTEXT_GLUE + text;

                        // If the string does not exits, return regular English string without context
                        if (GetText.StringExists (drawing_object.Text) == false)
                            drawing_object.Text = text;

                        continue;
                    }

                    name = reader.Name.ToLower ();
                    switch (name) {
                    case "games":
                        break;
                    case "type":
                        if (reader.NodeType != XmlNodeType.Element)
                            break;

                        game.Type = GameTypesDescription.FromString (reader.ReadElementString ());
                        break;
                    case "game":
                        if (reader.NodeType == XmlNodeType.Element) {
                            game = new GameXmlDefinition ();
                        } else if (reader.NodeType == XmlNodeType.EndElement) {
                            games.Add (game);
                        }
                        break;
                    case "_name":
                        if (reader.NodeType != XmlNodeType.Element)
                            break;

                        game.Name = reader.ReadElementString ();
                        break;
                    case "difficulty":
                        if (reader.NodeType != XmlNodeType.Element)
                            break;

                        str = reader.ReadElementString ();
                        game.Difficulty = GameDifficultyDescription.FromString (str);
                        break;
                    case "svg":
                        if (reader.NodeType != XmlNodeType.Element)
                            break;

                        ImageDrawingObject draw_image = new ImageDrawingObject ();

                        if (processing_variant)
                            game.Variants[variant].AddDrawingObject (draw_image);
                        else
                            game.AddDrawingObject (draw_image);

                        draw_image.Filename = reader.GetAttribute ("file");

                        str = reader.GetAttribute ("x");
                        if (String.IsNullOrEmpty (str) == false)
                            draw_image.X = Double.Parse (str, CultureInfo.InvariantCulture);
                        else
                            draw_image.X = 0.1;

                        str = reader.GetAttribute ("y");
                        if (String.IsNullOrEmpty (str) == false)
                            draw_image.Y = Double.Parse (str, CultureInfo.InvariantCulture);
                        else
                            draw_image.Y = 0.1;

                        str = reader.GetAttribute ("width");
                        if (String.IsNullOrEmpty (str) == false)
                            draw_image.Width = Double.Parse (str, CultureInfo.InvariantCulture);
                        else
                            draw_image.Width = 0.8;

                        str = reader.GetAttribute ("height");
                        if (String.IsNullOrEmpty (str) == false)
                            draw_image.Height = Double.Parse (str, CultureInfo.InvariantCulture);
                        else
                            draw_image.Height = 0.8;

                        break;
                    case "string":
                    case "_string":
                        if (reader.NodeType != XmlNodeType.Element)
                            break;

                        TextDrawingObject draw_string = new TextDrawingObject ();
                        last_drawing_object = draw_string;

                        if (option != null)
                        {
                            option.AddDrawingObject (draw_string);
                        }
                        else
                        {
                            if (processing_variant)
                                game.Variants[variant].AddDrawingObject (draw_string);
                            else
                                game.AddDrawingObject (draw_string);
                        }

                        last_context = reader.GetAttribute ("msgctxt");

                        draw_string.Text = reader.GetAttribute ("text");

                        if (String.IsNullOrEmpty (draw_string.Text))
                            draw_string.Text = reader.GetAttribute ("_text");

                        str = reader.GetAttribute ("x");
                        if (String.IsNullOrEmpty (str) == false)
                            draw_string.X = Double.Parse (str, CultureInfo.InvariantCulture);
                        else
                            draw_string.X = 0.1;

                        str = reader.GetAttribute ("y");
                        if (String.IsNullOrEmpty (str) == false)
                            draw_string.Y = Double.Parse (str, CultureInfo.InvariantCulture);
                        else
                            draw_string.Y = 0.1;

                        str = reader.GetAttribute ("centered");
                        if (String.Compare (str, "yes", true) == 0)
                            draw_string.Centered = true;
                        else
                            draw_string.Centered = false;

                        str = reader.GetAttribute ("size");

                        if (String.IsNullOrEmpty (str) == false)
                        {
                            switch (str.ToLower ()) {
                            case "small":
                                draw_string.Size = TextDrawingObject.Sizes.Small;
                                break;
                            case "medium":
                                draw_string.Size = TextDrawingObject.Sizes.Medium;
                                break;
                            case "large":
                                draw_string.Size = TextDrawingObject.Sizes.Large;
                                break;
                            case "x-large":
                                draw_string.Size = TextDrawingObject.Sizes.XLarge;
                                break;
                            case "xx-large":
                                draw_string.Size = TextDrawingObject.Sizes.XXLarge;
                                break;
                            default:
                                Console.WriteLine ("GameXmlFactory. Unsupported value for size attribute: {0}", str);
                                break;
                            }
                        }
                        break;
                    case "_question":
                    case "question":
                        if (reader.NodeType != XmlNodeType.Element)
                            break;

                        // Create object if needed
                        if (processing_variant) {
                            if (game.Variants[variant].Question == null)
                                game.Variants[variant].Question = new LocalizableString ();
                        }
                        else {
                            if (game.Question == null)
                                game.Question = new LocalizableString ();
                        }

                        plural = reader.GetAttribute ("plural");

                        if (String.IsNullOrEmpty (plural) == false) { // Plural
                            if (processing_variant) {
                                game.Variants[variant].Question.PluralString = reader.ReadElementStringAsItIs ();
                                game.Variants[variant].Question.Value = plural;
                            }
                            else {
                                game.Question.PluralString = reader.ReadElementStringAsItIs ();
                                game.Question.Value = plural;
                            }
                        }
                        else {
                            if (processing_variant)
                            {
                                game.Variants[variant].Question.String = reader.ReadElementStringAsItIs ();
                            }
                            else
                                game.Question.String = reader.ReadElementStringAsItIs ();
                        }
                        break;
                    case "rationale":
                    case "_rationale":
                        if (reader.NodeType != XmlNodeType.Element)
                            break;

                        // Create object if needed
                        if (processing_variant) {
                            if (game.Variants[variant].Rationale == null)
                                game.Variants[variant].Rationale = new LocalizableString ();
                        }
                        else {
                            if (game.Rationale == null)
                                game.Rationale = new LocalizableString ();
                        }

                        plural = reader.GetAttribute ("plural");

                        if (String.IsNullOrEmpty (plural) == false) { // Plural
                            if (processing_variant) {
                                game.Variants[variant].Rationale.PluralString = reader.ReadElementStringAsItIs ();
                                game.Variants[variant].Rationale.Value = plural;
                            }
                            else {
                                game.Rationale.PluralString = reader.ReadElementStringAsItIs ();
                                game.Rationale.Value = plural;
                            }
                        }
                        else {
                            if (processing_variant)
                                game.Variants[variant].Rationale.String = reader.ReadElementStringAsItIs ();
                            else
                                game.Rationale.String = reader.ReadElementStringAsItIs ();
                        }
                        break;
                    case "answer":
                    case "_answer":
                        if (reader.NodeType != XmlNodeType.Element)
                            break;

                        if (processing_variant)
                            game.Variants[variant].AnswerText = reader.ReadElementString ();
                        else
                            game.AnswerText = reader.ReadElementString ();

                        break;
                    case "_answer_show":
                    case "answer_show":
                        if (reader.NodeType != XmlNodeType.Element)
                            break;

                        if (processing_variant)
                            game.Variants[variant].AnswerShow = reader.ReadElementString ();
                        else
                            game.AnswerShow = reader.ReadElementString ();

                        break;
                    case "answer_expression":
                        if (reader.NodeType != XmlNodeType.Element)
                            break;

                        if (processing_variant)
                            game.Variants[variant].AnswerCheckExpression = reader.ReadElementString ();
                        else
                            game.AnswerCheckExpression = reader.ReadElementString ();

                        break;
                    case "answer_checkattributes":
                        if (reader.NodeType != XmlNodeType.Element)
                            break;

                        if (processing_variant)
                            game.Variants[variant].CheckAttributes = GameAnswerCheckAttributesDescription.FromString (reader.ReadElementString ());
                        else
                            game.CheckAttributes = GameAnswerCheckAttributesDescription.FromString (reader.ReadElementString ());

                        break;
                    case "_tip":
                        if (reader.NodeType != XmlNodeType.Element)
                            break;

                        if (processing_variant)
                            game.Variants[variant].Tip = reader.ReadElementStringAsItIs ();
                        else
                            game.Tip = reader.ReadElementStringAsItIs ();

                        break;
                    case "variant":
                        if (reader.NodeType == XmlNodeType.Element) {
                            game.NewVariant ();
                            variant = game.Variants.Count - 1;
                            processing_variant = true;
                        } else if (reader.NodeType == XmlNodeType.EndElement) {
                            processing_variant = false;
                        }
                        break;
                    case "variables":
                        if (processing_variant)
                            game.Variants[variant].Variables = reader.ReadElementString ();
                        else
                            game.Variables = reader.ReadElementString ();
                        break;

                    case "option":

                        switch (reader.NodeType) {
                        case XmlNodeType.Element:
                            option = new OptionDrawingObject ();
                            break;
                        case XmlNodeType.EndElement:
                            if (String.IsNullOrEmpty (option.AnswerText) && option.RandomizedOrder == false)
                                throw new InvalidOperationException ("If the option is not randomized, you need to define an answer");

                            option = null;
                            break;
                        default: // Do any processing
                            break;
                        }

                        if (option == null)
                            break;

                        if (processing_variant)
                            game.Variants[variant].AddDrawingObject (option);
                        else
                            game.AddDrawingObject (option);

                        option.AnswerText = reader.GetAttribute ("answer");

                        if (String.IsNullOrEmpty (option.AnswerText))
                            option.AnswerText = reader.GetAttribute ("_answer");

                        str = reader.GetAttribute ("x");
                        if (String.IsNullOrEmpty (str) == false)
                            option.X = Double.Parse (str, CultureInfo.InvariantCulture);
                        else
                            option.X = 0.1;

                        str = reader.GetAttribute ("y");
                        if (String.IsNullOrEmpty (str) == false)
                            option.Y = Double.Parse (str, CultureInfo.InvariantCulture);
                        else
                            option.Y = 0.1;

                        str = reader.GetAttribute ("width");
                        if (String.IsNullOrEmpty (str) == false)
                            option.Width = Double.Parse (str, CultureInfo.InvariantCulture);
                        else
                            option.Width = 0.1;

                        str = reader.GetAttribute ("height");
                        if (String.IsNullOrEmpty (str) == false)
                            option.Height = Double.Parse (str, CultureInfo.InvariantCulture);
                        else
                            option.Height = 0.1;

                        str = reader.GetAttribute ("order");
                        if (String.IsNullOrEmpty (str) == false)
                            option.RandomizedOrder = true;

                        str = reader.GetAttribute ("correct");
                        if (String.Compare (str, "yes", true) == 0)
                            option.Correct = true;

                        if (option.X + option.Width > 1 || option.Y + option.Height > 1)
                            Console.WriteLine ("GameXmlFactory. Wrong option size. x+width and y+height cannot be larger than 1");

                        break;
                    default:
                        if (String.IsNullOrEmpty (name) == false)
                            Console.WriteLine ("GameXmlFactory. Unsupported tag: {0}", name);

                        break;
                    }
                }

                reader.Close ();
                stream.Dispose ();
                read = true;

                GameXml.Definitions = games;
            }

            catch (Exception e)
            {
                read = true;
                Console.WriteLine ("GameXmlFactory. Error loading: {0}", e.Message);
            }
        }
Beispiel #6
0
        // Deep copy
        public OptionDrawingObject Copy()
        {
            OptionDrawingObject option = new OptionDrawingObject ();

            option.X = X;
            option.Y = Y;
            option.Width = Width;
            option.Height = Height;
            option.AnswerText = AnswerText;
            option.Correct = Correct;
            option.RandomizedOrder = RandomizedOrder;

            foreach (DrawingObject obj in DrawingObjects)
                option.AddDrawingObject (obj);

            return option;
        }
Beispiel #7
0
 public void CopyRandomizedProperties(OptionDrawingObject obj)
 {
     AnswerText = obj.AnswerText;
     Correct = obj.Correct;
     drawing_objects = obj.drawing_objects;
 }
Beispiel #8
0
 public void CopyRandomizedProperties(OptionDrawingObject obj)
 {
     AnswerText      = obj.AnswerText;
     Correct         = obj.Correct;
     drawing_objects = obj.drawing_objects;
 }
Beispiel #9
0
        void CreateDrawingObjects(GameXmlDefinitionVariant game)
        {
            OptionDrawingObject option;
            double x = 0, y = 0, width = 0, height = 0;
            bool first = true;
            int randomized_options = 0;

            if (game.DrawingObjects == null)
                return;

            // Calculate the size of container from the options and count the number of random options
            foreach (DrawingObject draw_object in game.DrawingObjects)
            {
                option = draw_object as OptionDrawingObject;

                if (option == null)
                    continue;

                if (option.RandomizedOrder)
                    randomized_options++;

                if (first == true)
                {
                    x = option.X;
                    y = option.Y;
                    width = option.Width;
                    height = option.Height;
                    first = false;
                    continue;
                }

                if (option.X < x)
                    x = option.X;

                if (option.Y < y)
                    y = option.Y;

                if (option.X + option.Width > width)
                    width = option.X + option.Width;

                if (option.Y + option.Height > height)
                    height = option.Y + option.Height;
            }

            if (first == true)
                return;

            // Randomize the order of the options
            if (randomized_options > 0)
            {
                OptionDrawingObject [] originals;
                ArrayListIndicesRandom random_indices;
                int index = 0;

                random_indices = new ArrayListIndicesRandom (randomized_options);
                originals = new OptionDrawingObject [randomized_options];
                random_indices.Initialize ();

                // Backup originals
                for (int i = 0; i < game.DrawingObjects.Length; i++)
                {
                    option = game.DrawingObjects[i] as OptionDrawingObject;

                    if (option == null)
                        continue;

                    originals[index] = option.Copy ();
                    index++;
                }

                // Swap
                index = 0;
                for (int i = 0; i < game.DrawingObjects.Length; i++)
                {
                    option = game.DrawingObjects[i] as OptionDrawingObject;

                    if (option == null)
                        continue;

                    option.CopyRandomizedProperties (originals [random_indices [index]]);
                    index++;
                }
            }

            Container container = new Container (x, y, width - x, height - y);
            AddWidget (container);

            if (options == null)
                options = new List <OptionDrawingObject> ();

            int idx = 0;

            // Create drawing widgets objects
            foreach (DrawingObject draw_object in game.DrawingObjects)
            {
                option = draw_object as OptionDrawingObject;

                if (option == null)
                    continue;

                DrawableArea drawable_area = new DrawableArea (option.Width, option.Height);
                drawable_area.X = option.X;
                drawable_area.Y = option.Y; // + i * 0.15;
                container.AddChild (drawable_area);

                drawable_area.Data = idx;
                drawable_area.DataEx = Answer.GetMultiOption (idx);
                options.Add (option);

                idx++;
                drawable_area.DrawEventHandler += DrawOption;
            }
        }
Beispiel #10
0
        public void Read(string file)
        {
            GameXmlDefinition game;
            string            name, str, plural;
            bool processing_variant    = false;
            int  variant               = 0;
            OptionDrawingObject option = null;
            DrawingObject       last_drawing_object = null;
            string last_context = null;

            if (read == true)
            {
                return;
            }

            try
            {
                StreamReader         stream = new StreamReader(file);
                XmlTextReaderLiteral reader = new XmlTextReaderLiteral(stream);
                game = null;

                while (reader.Read())
                {
                    // Strings are only used because msgctxt requirement
                    if (reader.NodeType == XmlNodeType.Text)
                    {
                        const string CONTEXT_GLUE = "\u0004";
                        string       text;

                        text = reader.ReadString();

                        TextDrawingObject drawing_object = (TextDrawingObject)last_drawing_object;
                        // GetText processes msgctxt as msgctxt + context_glue + text to retrieve them
                        drawing_object.Text = last_context + CONTEXT_GLUE + text;

                        // If the string does not exits, return regular English string without context
                        if (GetText.StringExists(drawing_object.Text) == false)
                        {
                            drawing_object.Text = text;
                        }

                        continue;
                    }

                    name = reader.Name.ToLower();
                    switch (name)
                    {
                    case "games":
                        break;

                    case "type":
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            break;
                        }

                        game.Type = GameTypesDescription.FromString(reader.ReadElementString());
                        break;

                    case "game":
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            game = new GameXmlDefinition();
                        }
                        else if (reader.NodeType == XmlNodeType.EndElement)
                        {
                            games.Add(game);
                        }
                        break;

                    case "_name":
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            break;
                        }

                        game.Name = reader.ReadElementString();
                        break;

                    case "difficulty":
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            break;
                        }

                        str             = reader.ReadElementString();
                        game.Difficulty = GameDifficultyDescription.FromString(str);
                        break;

                    case "svg":
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            break;
                        }

                        ImageDrawingObject draw_image = new ImageDrawingObject();

                        if (processing_variant)
                        {
                            game.Variants[variant].AddDrawingObject(draw_image);
                        }
                        else
                        {
                            game.AddDrawingObject(draw_image);
                        }

                        draw_image.Filename = reader.GetAttribute("file");

                        str = reader.GetAttribute("x");
                        if (String.IsNullOrEmpty(str) == false)
                        {
                            draw_image.X = Double.Parse(str, CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            draw_image.X = 0.1;
                        }

                        str = reader.GetAttribute("y");
                        if (String.IsNullOrEmpty(str) == false)
                        {
                            draw_image.Y = Double.Parse(str, CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            draw_image.Y = 0.1;
                        }

                        str = reader.GetAttribute("width");
                        if (String.IsNullOrEmpty(str) == false)
                        {
                            draw_image.Width = Double.Parse(str, CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            draw_image.Width = 0.8;
                        }

                        str = reader.GetAttribute("height");
                        if (String.IsNullOrEmpty(str) == false)
                        {
                            draw_image.Height = Double.Parse(str, CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            draw_image.Height = 0.8;
                        }

                        break;

                    case "string":
                    case "_string":
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            break;
                        }

                        TextDrawingObject draw_string = new TextDrawingObject();
                        last_drawing_object = draw_string;

                        if (option != null)
                        {
                            option.AddDrawingObject(draw_string);
                        }
                        else
                        {
                            if (processing_variant)
                            {
                                game.Variants[variant].AddDrawingObject(draw_string);
                            }
                            else
                            {
                                game.AddDrawingObject(draw_string);
                            }
                        }

                        last_context = reader.GetAttribute("msgctxt");

                        draw_string.Text = reader.GetAttribute("text");

                        if (String.IsNullOrEmpty(draw_string.Text))
                        {
                            draw_string.Text = reader.GetAttribute("_text");
                        }

                        str = reader.GetAttribute("x");
                        if (String.IsNullOrEmpty(str) == false)
                        {
                            draw_string.X = Double.Parse(str, CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            draw_string.X = 0.1;
                        }

                        str = reader.GetAttribute("y");
                        if (String.IsNullOrEmpty(str) == false)
                        {
                            draw_string.Y = Double.Parse(str, CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            draw_string.Y = 0.1;
                        }

                        str = reader.GetAttribute("centered");
                        if (String.Compare(str, "yes", true) == 0)
                        {
                            draw_string.Centered = true;
                        }
                        else
                        {
                            draw_string.Centered = false;
                        }

                        str = reader.GetAttribute("size");

                        if (String.IsNullOrEmpty(str) == false)
                        {
                            switch (str.ToLower())
                            {
                            case "small":
                                draw_string.Size = TextDrawingObject.Sizes.Small;
                                break;

                            case "medium":
                                draw_string.Size = TextDrawingObject.Sizes.Medium;
                                break;

                            case "large":
                                draw_string.Size = TextDrawingObject.Sizes.Large;
                                break;

                            case "x-large":
                                draw_string.Size = TextDrawingObject.Sizes.XLarge;
                                break;

                            case "xx-large":
                                draw_string.Size = TextDrawingObject.Sizes.XXLarge;
                                break;

                            default:
                                Console.WriteLine("GameXmlFactory. Unsupported value for size attribute: {0}", str);
                                break;
                            }
                        }
                        break;

                    case "_question":
                    case "question":
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            break;
                        }

                        // Create object if needed
                        if (processing_variant)
                        {
                            if (game.Variants[variant].Question == null)
                            {
                                game.Variants[variant].Question = new LocalizableString();
                            }
                        }
                        else
                        {
                            if (game.Question == null)
                            {
                                game.Question = new LocalizableString();
                            }
                        }

                        plural = reader.GetAttribute("plural");

                        if (String.IsNullOrEmpty(plural) == false)                            // Plural
                        {
                            if (processing_variant)
                            {
                                game.Variants[variant].Question.PluralString = reader.ReadElementStringAsItIs();
                                game.Variants[variant].Question.Value        = plural;
                            }
                            else
                            {
                                game.Question.PluralString = reader.ReadElementStringAsItIs();
                                game.Question.Value        = plural;
                            }
                        }
                        else
                        {
                            if (processing_variant)
                            {
                                game.Variants[variant].Question.String = reader.ReadElementStringAsItIs();
                            }
                            else
                            {
                                game.Question.String = reader.ReadElementStringAsItIs();
                            }
                        }
                        break;

                    case "rationale":
                    case "_rationale":
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            break;
                        }

                        // Create object if needed
                        if (processing_variant)
                        {
                            if (game.Variants[variant].Rationale == null)
                            {
                                game.Variants[variant].Rationale = new LocalizableString();
                            }
                        }
                        else
                        {
                            if (game.Rationale == null)
                            {
                                game.Rationale = new LocalizableString();
                            }
                        }

                        plural = reader.GetAttribute("plural");

                        if (String.IsNullOrEmpty(plural) == false)                            // Plural
                        {
                            if (processing_variant)
                            {
                                game.Variants[variant].Rationale.PluralString = reader.ReadElementStringAsItIs();
                                game.Variants[variant].Rationale.Value        = plural;
                            }
                            else
                            {
                                game.Rationale.PluralString = reader.ReadElementStringAsItIs();
                                game.Rationale.Value        = plural;
                            }
                        }
                        else
                        {
                            if (processing_variant)
                            {
                                game.Variants[variant].Rationale.String = reader.ReadElementStringAsItIs();
                            }
                            else
                            {
                                game.Rationale.String = reader.ReadElementStringAsItIs();
                            }
                        }
                        break;

                    case "answer":
                    case "_answer":
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            break;
                        }

                        if (processing_variant)
                        {
                            game.Variants[variant].AnswerText = reader.ReadElementString();
                        }
                        else
                        {
                            game.AnswerText = reader.ReadElementString();
                        }

                        break;

                    case "_answer_show":
                    case "answer_show":
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            break;
                        }

                        if (processing_variant)
                        {
                            game.Variants[variant].AnswerShow = reader.ReadElementString();
                        }
                        else
                        {
                            game.AnswerShow = reader.ReadElementString();
                        }

                        break;

                    case "answer_expression":
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            break;
                        }

                        if (processing_variant)
                        {
                            game.Variants[variant].AnswerCheckExpression = reader.ReadElementString();
                        }
                        else
                        {
                            game.AnswerCheckExpression = reader.ReadElementString();
                        }

                        break;

                    case "answer_checkattributes":
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            break;
                        }

                        if (processing_variant)
                        {
                            game.Variants[variant].CheckAttributes = GameAnswerCheckAttributesDescription.FromString(reader.ReadElementString());
                        }
                        else
                        {
                            game.CheckAttributes = GameAnswerCheckAttributesDescription.FromString(reader.ReadElementString());
                        }

                        break;

                    case "_tip":
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            break;
                        }

                        if (processing_variant)
                        {
                            game.Variants[variant].Tip = reader.ReadElementStringAsItIs();
                        }
                        else
                        {
                            game.Tip = reader.ReadElementStringAsItIs();
                        }

                        break;

                    case "variant":
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            game.NewVariant();
                            variant            = game.Variants.Count - 1;
                            processing_variant = true;
                        }
                        else if (reader.NodeType == XmlNodeType.EndElement)
                        {
                            processing_variant = false;
                        }
                        break;

                    case "variables":
                        if (processing_variant)
                        {
                            game.Variants[variant].Variables = reader.ReadElementString();
                        }
                        else
                        {
                            game.Variables = reader.ReadElementString();
                        }
                        break;

                    case "option":

                        switch (reader.NodeType)
                        {
                        case XmlNodeType.Element:
                            option = new OptionDrawingObject();
                            break;

                        case XmlNodeType.EndElement:
                            if (String.IsNullOrEmpty(option.AnswerText) && option.RandomizedOrder == false)
                            {
                                throw new InvalidOperationException("If the option is not randomized, you need to define an answer");
                            }

                            option = null;
                            break;

                        default:                         // Do any processing
                            break;
                        }

                        if (option == null)
                        {
                            break;
                        }

                        if (processing_variant)
                        {
                            game.Variants[variant].AddDrawingObject(option);
                        }
                        else
                        {
                            game.AddDrawingObject(option);
                        }

                        option.AnswerText = reader.GetAttribute("answer");

                        if (String.IsNullOrEmpty(option.AnswerText))
                        {
                            option.AnswerText = reader.GetAttribute("_answer");
                        }

                        str = reader.GetAttribute("x");
                        if (String.IsNullOrEmpty(str) == false)
                        {
                            option.X = Double.Parse(str, CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            option.X = 0.1;
                        }

                        str = reader.GetAttribute("y");
                        if (String.IsNullOrEmpty(str) == false)
                        {
                            option.Y = Double.Parse(str, CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            option.Y = 0.1;
                        }

                        str = reader.GetAttribute("width");
                        if (String.IsNullOrEmpty(str) == false)
                        {
                            option.Width = Double.Parse(str, CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            option.Width = 0.1;
                        }

                        str = reader.GetAttribute("height");
                        if (String.IsNullOrEmpty(str) == false)
                        {
                            option.Height = Double.Parse(str, CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            option.Height = 0.1;
                        }

                        str = reader.GetAttribute("order");
                        if (String.IsNullOrEmpty(str) == false)
                        {
                            option.RandomizedOrder = true;
                        }

                        str = reader.GetAttribute("correct");
                        if (String.Compare(str, "yes", true) == 0)
                        {
                            option.Correct = true;
                        }

                        if (option.X + option.Width > 1 || option.Y + option.Height > 1)
                        {
                            Console.WriteLine("GameXmlFactory. Wrong option size. x+width and y+height cannot be larger than 1");
                        }

                        break;

                    default:
                        if (String.IsNullOrEmpty(name) == false)
                        {
                            Console.WriteLine("GameXmlFactory. Unsupported tag: {0}", name);
                        }

                        break;
                    }
                }

                reader.Close();
                stream.Dispose();
                read = true;

                GameXml.Definitions = games;
            }

            catch (Exception e)
            {
                read = true;
                Console.WriteLine("GameXmlFactory. Error loading: {0}", e.Message);
            }
        }
Beispiel #11
0
        void CreateDrawingObjects(GameXmlDefinitionVariant game)
        {
            OptionDrawingObject option;
            double x = 0, y = 0, width = 0, height = 0;
            bool   first = true;
            int    randomized_options = 0;

            if (game.DrawingObjects == null)
            {
                return;
            }

            // Calculate the size of container from the options and count the number of random options
            foreach (DrawingObject draw_object in game.DrawingObjects)
            {
                option = draw_object as OptionDrawingObject;

                if (option == null)
                {
                    continue;
                }

                if (option.RandomizedOrder)
                {
                    randomized_options++;
                }

                if (first == true)
                {
                    x      = option.X;
                    y      = option.Y;
                    width  = option.Width;
                    height = option.Height;
                    first  = false;
                    continue;
                }

                if (option.X < x)
                {
                    x = option.X;
                }

                if (option.Y < y)
                {
                    y = option.Y;
                }

                if (option.X + option.Width > width)
                {
                    width = option.X + option.Width;
                }

                if (option.Y + option.Height > height)
                {
                    height = option.Y + option.Height;
                }
            }

            if (first == true)
            {
                return;
            }

            // Randomize the order of the options
            if (randomized_options > 0)
            {
                OptionDrawingObject [] originals;
                ArrayListIndicesRandom random_indices;
                int index = 0;

                random_indices = new ArrayListIndicesRandom(randomized_options);
                originals      = new OptionDrawingObject [randomized_options];
                random_indices.Initialize();

                // Backup originals
                for (int i = 0; i < game.DrawingObjects.Length; i++)
                {
                    option = game.DrawingObjects[i] as OptionDrawingObject;

                    if (option == null)
                    {
                        continue;
                    }

                    originals[index] = option.Copy();
                    index++;
                }

                // Swap
                index = 0;
                for (int i = 0; i < game.DrawingObjects.Length; i++)
                {
                    option = game.DrawingObjects[i] as OptionDrawingObject;

                    if (option == null)
                    {
                        continue;
                    }

                    option.CopyRandomizedProperties(originals [random_indices [index]]);
                    index++;
                }
            }

            Container container = new Container(x, y, width - x, height - y);

            AddWidget(container);

            if (options == null)
            {
                options = new List <OptionDrawingObject> ();
            }

            int idx = 0;

            // Create drawing widgets objects
            foreach (DrawingObject draw_object in game.DrawingObjects)
            {
                option = draw_object as OptionDrawingObject;

                if (option == null)
                {
                    continue;
                }

                DrawableArea drawable_area = new DrawableArea(option.Width, option.Height);
                drawable_area.X = option.X;
                drawable_area.Y = option.Y;                 // + i * 0.15;
                container.AddChild(drawable_area);

                drawable_area.Data   = idx;
                drawable_area.DataEx = Answer.GetMultiOption(idx);
                options.Add(option);

                idx++;
                drawable_area.DrawEventHandler += DrawOption;
            }
        }
Beispiel #12
0
        protected override void Initialize()
        {
            string            variables;
            bool              variants;
            LocalizableString localizable_question, localizable_rationale;

            compiler = ServiceLocator.Instance.GetService <ICSharpCompiler> ();

            variants = game.Variants.Count > 0;
            SetCheckAttributes();

            if (variants && game.Variants[current.Variant].Variables != null)
            {
                variables = game.Variants[current.Variant].Variables;
            }
            else
            {
                variables = game.Variables;
            }

            if (variants && game.Variants[current.Variant].Question != null)
            {
                localizable_question = game.Variants[current.Variant].Question;
            }
            else
            {
                localizable_question = game.Question;
            }

            if (variants && game.Variants[current.Variant].Rationale != null)
            {
                localizable_rationale = game.Variants[current.Variant].Rationale;
            }
            else
            {
                localizable_rationale = game.Rationale;
            }

            if (String.IsNullOrEmpty(variables) == false)
            {
                compiler.EvaluateCode(variables);

                try {
                    if (String.IsNullOrEmpty(localizable_question.Value) == false)
                    {
                        localizable_question.ValueComputed = Int32.Parse(ReplaceVariables(localizable_question.Value));
                    }

                    if (localizable_rationale != null && String.IsNullOrEmpty(localizable_rationale.Value) == false)
                    {
                        localizable_rationale.ValueComputed = Int32.Parse(ReplaceVariables(localizable_rationale.Value));
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("GameXml.Initialize. Error {0}", e);
                }
            }

            if (variants && game.Variants[current.Variant].Question != null)
            {
                question = CatalogGetString(game.Variants[current.Variant].Question);
            }
            else
            {
                question = CatalogGetString(game.Question);
            }

            if (variants && game.Variants[current.Variant].AnswerText != null)
            {
                answer = CatalogGetString(game.Variants[current.Variant].AnswerText);
            }
            else
            {
                answer = CatalogGetString(game.AnswerText);
            }

            if (variants && game.Variants[current.Variant].Rationale != null)
            {
                rationale = CatalogGetString(game.Variants[current.Variant].Rationale);
            }
            else
            {
                rationale = CatalogGetString(game.Rationale);
            }

            if (variants && game.Variants[current.Variant].AnswerShow != null)
            {
                answer_show = CatalogGetString(game.Variants[current.Variant].AnswerShow);
            }
            else
            {
                answer_show = CatalogGetString(game.AnswerShow);
            }

            if (String.IsNullOrEmpty(variables) == false)
            {
                question    = ReplaceVariables(question);
                rationale   = ReplaceVariables(rationale);
                answer      = ReplaceVariables(answer);
                answer_show = ReplaceVariables(answer_show);
            }

            if (options != null && options.Count > 0)
            {
                for (int i = 0; i < options.Count; i++)
                {
                    OptionDrawingObject option = options [i];
                    if (option.Correct == true)
                    {
                        Answer.SetMultiOptionAnswer(i, answer);
                        break;
                    }
                }

                // Translators {0}: list of options (A, B, C)
                string answers = String.Format(ServiceLocator.Instance.GetService <ITranslations> ().GetString("Answer {0}."),
                                               Answer.GetMultiOptionsPossibleAnswers(options.Count));
                question = question.Replace(option_answers, answers);
            }
            else
            {
                Answer.Correct = answer;
            }

            SetCheckExpression();
            SetAnswerCorrectShow();
        }
Beispiel #13
0
        void RandomizeOptions(DrawingObject [] drawing_objects, int randomized_options)
        {
            OptionDrawingObject option;

            // Randomize the order of the options
            if (randomized_options > 0)
            {
                OptionDrawingObject [] originals;
                ArrayListIndicesRandom random_indices;
                int index = 0;

                random_indices = new ArrayListIndicesRandom (randomized_options);
                originals = new OptionDrawingObject [randomized_options];
                random_indices.Initialize ();

                // Backup originals
                for (int i = 0; i < drawing_objects.Length; i++)
                {
                    option = drawing_objects[i] as OptionDrawingObject;

                    if (option == null)
                        continue;

                    originals[index] = option.Copy ();
                    index++;
                }

                // Swap
                index = 0;
                for (int i = 0; i < drawing_objects.Length; i++)
                {
                    option = drawing_objects[i] as OptionDrawingObject;

                    if (option == null)
                        continue;

                    option.CopyRandomizedProperties (originals [random_indices [index]]);
                    index++;
                }
            }
        }