예제 #1
0
    public override bool addElement(int type, string id)
    {
        BookParagraph newBookParagraph = null;

        if (type == Controller.BOOK_TITLE_PARAGRAPH)
        {
            newBookParagraph = new BookParagraph(BookParagraph.TITLE);
        }

        else if (type == Controller.BOOK_TEXT_PARAGRAPH)
        {
            newBookParagraph = new BookParagraph(BookParagraph.TEXT);
        }

        else if (type == Controller.BOOK_BULLET_PARAGRAPH)
        {
            newBookParagraph = new BookParagraph(BookParagraph.BULLET);
        }

        else if (type == Controller.BOOK_IMAGE_PARAGRAPH)
        {
            newBookParagraph = new BookParagraph(BookParagraph.IMAGE);
        }

        // If a paragraph was added, add the controller to the list
        if (newBookParagraph != null)
        {
            bookParagraphsList.Add(newBookParagraph);
            bookParagraphsDataControlList.Add(new BookParagraphDataControl(newBookParagraph));
            //controller.dataModified( );
        }

        return(newBookParagraph != null);
    }
예제 #2
0
    public object Clone()
    {
        BookParagraph bp = (BookParagraph)this.MemberwiseClone();

        bp.content = (content != null ? content : null);
        bp.type    = type;
        return(bp);
    }
예제 #3
0
 /**
  * Adds a paragraph to the book
  *
  * @param paragraph
  *            New paragraph to be added
  */
 public void addParagraph(BookParagraph paragraph)
 {
     paragraphs.Add(paragraph);
     // add the page to the structure that gather all elements with assets (for chapter importation)
     if (paragraph.getType() == BookParagraph.IMAGE)
     {
         AllElementsWithAssets.addAsset(paragraph);
     }
 }
예제 #4
0
    /**
     * Returns the info of the book paragraphs contained in the list.
     *
     * @return Array with the information of the book paragraphs. It contains
     *         the number of each paragraph, the type of the paragraph, and the
     *         word count if applicable
     */
    public string[][] getBookParagraphsInfo()
    {
        string[][] bookParagraphsInfo = null;

        // Create the list for the book paragraphs
        bookParagraphsInfo = new string[bookParagraphsList.Count][];
        for (int i = 0; i < bookParagraphsList.Count; i++)
        {
            bookParagraphsInfo[i] = new string[3];
        }

        // Fill the array with the info
        for (int i = 0; i < bookParagraphsList.Count; i++)
        {
            BookParagraph bookParagraph = bookParagraphsList[i];

            if (bookParagraph.getType() == BookParagraph.TEXT)
            {
                bookParagraphsInfo[i][0] = TC.get("BookParagraphsList.TextParagraph", (i + 1).ToString());
            }
            else if (bookParagraph.getType() == BookParagraph.TITLE)
            {
                bookParagraphsInfo[i][0] = TC.get("BookParagraphsList.TitleParagraph", (i + 1).ToString());
            }
            else if (bookParagraph.getType() == BookParagraph.BULLET)
            {
                bookParagraphsInfo[i][0] = TC.get("BookParagraphsList.BulletParagraph", (i + 1).ToString());
            }
            else if (bookParagraph.getType() == BookParagraph.IMAGE)
            {
                bookParagraphsInfo[i][0] = TC.get("BookParagraphsList.ImageParagraph", (i + 1).ToString());
            }

            if (bookParagraph.getType() != BookParagraph.IMAGE)
            {
                bookParagraphsInfo[i][1] = TC.get("BookParagraphsList.WordCount", (bookParagraph.getContent().Split(' ').Length.ToString()));
            }
            else
            {
                bookParagraphsInfo[i][1] = TC.get("BookParagraphsList.NotApplicable");
            }
        }

        return(bookParagraphsInfo);
    }
예제 #5
0
    public override bool moveElementDown(DataControl dataControl)
    {
        bool elementMoved = false;
        int  elementIndex = bookParagraphsList.IndexOf((BookParagraph)dataControl.getContent());

        if (elementIndex < bookParagraphsList.Count - 1)
        {
            BookParagraph            e = bookParagraphsList[elementIndex];
            BookParagraphDataControl c = bookParagraphsDataControlList[elementIndex];
            bookParagraphsList.RemoveAt(elementIndex);
            bookParagraphsDataControlList.RemoveAt(elementIndex);
            bookParagraphsList.Insert(elementIndex + 1, e);
            bookParagraphsDataControlList.Insert(elementIndex + 1, c);
            elementMoved = true;
        }

        return(elementMoved);
    }
    /**
     * Constructor.
     *
     * @param bookParagraph
     *            Contained book paragraph
     */
    public BookParagraphDataControl(BookParagraph bookParagraph)
    {
        this.bookParagraph = bookParagraph;

        // Store the type of the paragraph
        switch (bookParagraph.getType())
        {
        case BookParagraph.TITLE:
            bookParagraphType = Controller.BOOK_TITLE_PARAGRAPH;
            break;

        case BookParagraph.TEXT:
            bookParagraphType = Controller.BOOK_TEXT_PARAGRAPH;
            break;

        case BookParagraph.IMAGE:
            bookParagraphType = Controller.BOOK_IMAGE_PARAGRAPH;
            break;

        case BookParagraph.BULLET:
            bookParagraphType = Controller.BOOK_BULLET_PARAGRAPH;
            break;
        }
    }
        private GUIStyle GetElementStyle(BookParagraph nextElement)
        {
            GUIStyle style = new GUIStyle();

            switch (nextElement.getType())
            {
            case BookParagraph.IMAGE:
                style = GUI.skin.GetStyle("image");
                break;

            case BookParagraph.BULLET:
                style = GUI.skin.GetStyle("bookBullet");
                break;

            case BookParagraph.TEXT:
                style = GUI.skin.GetStyle("bookText");
                break;

            case BookParagraph.TITLE:
                style = GUI.skin.GetStyle("bookTitle");
                break;
            }
            return(style);
        }
 public ChangeParagraphContentTool(BookParagraph bookParagraph, string content)
 {
     this.bookParagraph = bookParagraph;
     this.newContent    = content;
     this.oldContent    = bookParagraph.getContent();
 }
예제 #9
0
 /**
  * Adds a paragraph to the book
  *
  * @param paragraph
  *            New paragraph to be added
  */
 public void addParagraph(BookParagraph paragraph)
 {
     paragraphs.Add(paragraph);
     // add the page to the structure that gather all elements with assets (for chapter importation)
     if (paragraph.getType() == BookParagraph.IMAGE)
         AllElementsWithAssets.addAsset(paragraph);
 }
        public void Draw(Rect rect)
        {
            if (Book == null || background == null)
            {
                return;
            }

            // Adapt the rect to the background ratio
            var rectRatio       = rect.width / rect.height;
            var backgroundRatio = background.width / (float)background.height;
            var rectCenter      = rect.center;

            if (rectRatio > backgroundRatio)
            {
                rect.width = rect.height * backgroundRatio;
            }
            else
            {
                rect.height = rect.width / backgroundRatio;
            }
            rect.center = rectCenter;

            // Set matrix
            Vector2   ratio     = new Vector2(rect.width / background.width, rect.height / background.height);
            Matrix4x4 guiMatrix = Matrix4x4.TRS(new Vector3(rect.x, rect.y, 1), Quaternion.identity, new Vector3(ratio.x, ratio.y, 1));

            rect.position = Vector2.zero;
            rect.size     = new Vector2(background.width, background.height);
            GUI.matrix    = guiMatrix;


            // First we change the background size to fit the background ratio
            GUI.DrawTexture(rect, background);

            // Draw the content
            var bcSkin = GUI.skin;

            GUI.skin = bookSkin;
            GUILayout.BeginArea(rect, new GUIContent(), "bookBackground");
            bool hasNextPage = false;

            if (Book.getType() == uAdventure.Core.Book.TYPE_PAGES)
            {
                GUILayout.Label("This type of book is not supported in uAdventure.", GUILayout.ExpandHeight(true), GUILayout.ExpandWidth(true));
            }
            else
            {
                IEnumerator <BookParagraph> paragraphs = Book.getParagraphs().GetEnumerator();
                int      lineIndex = 0;
                string[] lines     = new string[0];

                GUILayout.BeginHorizontal();
                {
                    float         nextElementHeight = 0;
                    GUIStyle      nextStyle         = new GUIStyle();
                    GUIContent    nextContent       = new GUIContent();
                    BookParagraph nextElement       = null;

                    var drawTillPage = (currentPage + 1) * 2 - 1;
                    // Draw two pages
                    for (int i = 0; i <= drawTillPage; ++i)
                    {
                        bool drawPage = i >= drawTillPage - 1;

                        if (drawPage)
                        {
                            GUILayout.BeginVertical("bookPage", GUILayout.MinWidth(bookRect.width / 2f),
                                                    GUILayout.Width(bookRect.width / 2f), GUILayout.MaxWidth(bookRect.width / 2f), GUILayout.ExpandHeight(true));
                            GUILayout.BeginVertical(GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
                        }
                        Rect  currentPageRect = pageRect[i % 2];
                        float heightRemaining = currentPageRect.height;
                        do
                        {
                            if (nextElement != null)
                            {
                                if (drawPage)
                                {
                                    GUILayout.Label(nextContent, nextStyle, GUILayout.MaxWidth(currentPageRect.width));
                                    if (lineIndex == 0 && nextElement.getType() == BookParagraph.BULLET)
                                    {
                                        GUI.Label(GUILayoutUtility.GetLastRect(), "•", "bullet");
                                    }
                                }
                                heightRemaining -= nextElementHeight;
                                lineIndex++;
                            }

                            if (lineIndex == lines.Length)
                            {
                                if (paragraphs.MoveNext())
                                {
                                    nextElement = paragraphs.Current;
                                    lineIndex   = 0;
                                    lines       = DivideParagraph(nextElement, currentPageRect.width);
                                    nextStyle   = GetElementStyle(nextElement);
                                }
                                else
                                {
                                    nextElement = null;
                                }
                            }

                            if (nextElement != null && lineIndex < lines.Length)
                            {
                                if (nextElement.getType() == BookParagraph.IMAGE)
                                {
                                    var imagePath = nextElement.getContent();
                                    if (!string.IsNullOrEmpty(imagePath))
                                    {
                                        nextContent.text  = null;
                                        nextContent.image = resourceManager.getImage(imagePath);
                                    }
                                }
                                else
                                {
                                    nextContent.text  = lines[lineIndex];
                                    nextContent.image = null;
                                }

                                nextElementHeight = nextStyle.CalcHeight(nextContent, currentPageRect.width);
                            }
                        } while (nextElementHeight <= heightRemaining && nextElement != null);

                        if (drawPage)
                        {
                            if (nextElement != null)
                            {
                                hasNextPage = i == drawTillPage;
                            }

                            GUILayout.EndVertical();
                            if (Event.current.type == EventType.Repaint)
                            {
                                pageRect[i % 2] = GUILayoutUtility.GetLastRect();
                            }
                            GUILayout.EndVertical();
                        }
                    }
                }
                GUILayout.EndHorizontal();
                if (Event.current.type == EventType.Repaint)
                {
                    bookRect = GUILayoutUtility.GetLastRect();
                }
            }

            GUILayout.EndArea();


            // Reset matrix

            // Lastly we draw the buttons
            if (Application.isPlaying && DoBookButton(rect.position, closeButton, closeButton, rect, false))
            {
                Game.Instance.CloseBook();
            }

            if (this.book != null)
            {
                if (currentPage > 0 && leftImage &&
                    DoBookButton(Book.getPreviousPageVector2(), leftImage, leftImageOver, rect, false))
                {
                    currentPage--;
                }

                if (hasNextPage && (rightImage || leftImage) &&
                    DoBookButton(Book.getNextPageVector2(), rightImage ?? leftImage, rightImageOver ?? leftImageOver, rect, !rightImage || rightIsDefault))
                {
                    currentPage++;
                }
            }
            GUI.matrix = Matrix4x4.identity;

            GUI.skin = bcSkin;
        }
        private string[] DivideParagraph(BookParagraph current, float maxWidth)
        {
            var guistyle   = GetElementStyle(current);
            var auxContent = new GUIContent {
                text = " "
            };
            var oneSpaceWidth = guistyle.CalcSize(auxContent).x;

            auxContent.text = "  ";
            var twoSpaceWidth = guistyle.CalcSize(auxContent).x;

            oneSpaceWidth = twoSpaceWidth - oneSpaceWidth;
            var paddings = twoSpaceWidth - 2 * oneSpaceWidth;

            maxWidth -= paddings;
            List <string> lines       = new List <string> ();
            var           currentLine = new StringBuilder();

            var paragraphLines = current.getContent().Split('\n');

            if (maxWidth < oneSpaceWidth * 2)
            {
                return(paragraphLines.ToList().ConvertAll(_ => "").ToArray());
            }

            foreach (var paragraphLine in paragraphLines)
            {
                var   words     = paragraphLine.Split(' ');
                float spaceLeft = maxWidth;
                foreach (var word in words)
                {
                    auxContent.text = word;
                    var wordWidth = guistyle.CalcSize(auxContent).x - paddings;
                    if (wordWidth <= spaceLeft)
                    {
                        // Fits in this line
                        if (currentLine.Length > 0)
                        {
                            currentLine.Append(" ");
                            spaceLeft -= oneSpaceWidth;
                        }
                        currentLine.Append(word);
                        spaceLeft -= wordWidth;
                    }
                    else if (wordWidth < maxWidth)
                    {
                        // Fits in next line
                        lines.Add(currentLine.ToString());
                        currentLine.Remove(0, currentLine.Length); // Clear
                        currentLine.Append(word);
                        spaceLeft = maxWidth - wordWidth;
                    }
                    else
                    {
                        // Since it doesn't fit alone, we cut it in pieces.
                        if (currentLine.Length > 0)
                        {
                            lines.Add(currentLine.ToString());
                            currentLine.Remove(0, currentLine.Length); // Clear
                            spaceLeft = maxWidth;
                        }

                        var remaining = word;
                        while (remaining.Length > 0)
                        {
                            auxContent.text = remaining;
                            wordWidth       = guistyle.CalcSize(auxContent).x - paddings;

                            if (wordWidth <= maxWidth)
                            {
                                // Base case
                                currentLine.Append(remaining);
                                remaining  = string.Empty;
                                spaceLeft -= wordWidth;
                            }
                            else
                            {
                                // Recursive case
                                var percent = maxWidth / wordWidth;
                                var index   = (int)percent * remaining.Length;

                                var lastValidIndex = -1;
                                do
                                {
                                    auxContent.text = remaining.Substring(0, index);
                                    var selectedWidth = guistyle.CalcSize(auxContent).x - paddings;
                                    if (selectedWidth <= maxWidth)
                                    {
                                        lastValidIndex = index;
                                        index++;
                                        if (remaining.Length == index)
                                        {
                                            // We cant move forward, so this is valid already
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        index--;
                                    }
                                } while (lastValidIndex != index);

                                lines.Add(remaining.Substring(0, index));
                                remaining = remaining.Substring(index);
                                spaceLeft = maxWidth;
                            }
                        }
                    }
                }
                lines.Add(currentLine.ToString());
                currentLine.Remove(0, currentLine.Length); // Clear
            }

            return(lines.ToArray());
        }