コード例 #1
0
        /*public void ProcessHtmlString(string text, int width)
         * {
         *  bool tagMode = false;
         *  string tag = "";
         *  FormattedWord newWord = new FormattedWord();
         *  FormattedLine newLine = new FormattedLine();
         *  int lineHeight = 0;
         *
         *  foreach (char c in text)
         *  {
         #region Start/Stop Tags
         *      //start a tag and check for end of word
         *      if (c == '<')
         *      {
         *          tagMode = true;
         *
         *          if (newWord.text != "")
         *          {
         *              newWord.fontStyle = GetFontStyle();
         *              newWord.fontWeight = GetFontWeight();
         *              newWord.fontSize = GetFontSizeInPixels();
         *              newWord.color = GetColor();
         *              gv.textFormat = new SharpDX.DirectWrite.TextFormat(gv.factoryDWrite, gv.family.Name, gv.CurrentFontCollection, newWord.fontWeight, newWord.fontStyle, FontStretch.Normal, newWord.fontSize) { TextAlignment = TextAlignment.Leading, ParagraphAlignment = ParagraphAlignment.Near };
         *              gv.textLayout = new SharpDX.DirectWrite.TextLayout(gv.factoryDWrite, newWord.text + " ", gv.textFormat, gv.Width, gv.Height);
         *              //font = new Font(gv.family, newWord.fontSize, newWord.fontStyle);
         *              float height = gv.textLayout.Metrics.Height;
         *              float wordWidth = gv.textLayout.Metrics.WidthIncludingTrailingWhitespace;
         *              if (height > lineHeight) { lineHeight = (int)height; }
         *              //int wordWidth = (int)(frm.gCanvas.MeasureString(newWord.word, font)).Width;
         *              if (xLoc + wordWidth > width) //word wrap
         *              {
         *                  //end last line and add it to the log
         *                  newLine.lineHeight = lineHeight;
         *                  logLinesList.Add(newLine);
         *                  //start a new line and add this word
         *                  newLine = new FormattedLine();
         *                  newLine.wordsList.Add(newWord);
         *                  xLoc = 0;
         *              }
         *              else //no word wrap, just add word
         *              {
         *                  newLine.wordsList.Add(newWord);
         *              }
         *              //instead of drawing, just add to line list
         *              //DrawString(g, word, font, brush, xLoc, yLoc);
         *              xLoc += wordWidth;
         *              newWord = new FormattedWord();
         *          }
         *          continue;
         *      }
         *      //end a tag
         *      else if (c == '>')
         *      {
         *          //check for ending type tag
         *          if (tag.StartsWith("/"))
         *          {
         *              //if </>, remove corresponding tag from stack
         *              string tagMinusSlash = tag.Substring(1);
         *              if (tag.StartsWith("/font"))
         *              {
         *                  for (int i = tagStack.Count - 1; i > 0; i--)
         *                  {
         *                      if (tagStack[i].StartsWith("font"))
         *                      {
         *                          tagStack.RemoveAt(i);
         *                          break;
         *                      }
         *                  }
         *              }
         *              else
         *              {
         *                  tagStack.Remove(tagMinusSlash);
         *              }
         *          }
         *          else
         *          {
         *              //check for line break
         *              if ((tag.ToLower() == "br") || (tag == "BR"))
         *              {
         *                  newWord.fontStyle = GetFontStyle();
         *                  newWord.fontWeight = GetFontWeight();
         *                  newWord.fontSize = GetFontSizeInPixels();
         *                  newWord.color = GetColor();
         *                  //end last line and add it to the log
         *                  newLine.lineHeight = lineHeight;
         *                  logLinesList.Add(newLine);
         *                  //start a new line and add this word
         *                  newLine = new FormattedLine();
         *                  //newLine.wordsList.Add(newWord);
         *                  xLoc = 0;
         *              }
         *              //else if <>, add this tag to the stack
         *              tagStack.Add(tag);
         *          }
         *          tagMode = false;
         *          tag = "";
         *          continue;
         *      }
         #endregion
         *
         #region Words
         *      if (!tagMode)
         *      {
         *          if (c != ' ') //keep adding to word until hit a space
         *          {
         *              newWord.text += c;
         *          }
         *          else //hit a space so end word
         *          {
         *              newWord.fontStyle = GetFontStyle();
         *              newWord.fontWeight = GetFontWeight();
         *              newWord.fontSize = GetFontSizeInPixels();
         *              newWord.color = GetColor();
         *              gv.textFormat = new SharpDX.DirectWrite.TextFormat(gv.factoryDWrite, gv.family.Name, gv.CurrentFontCollection, newWord.fontWeight, newWord.fontStyle, FontStretch.Normal, newWord.fontSize) { TextAlignment = TextAlignment.Leading, ParagraphAlignment = ParagraphAlignment.Near };
         *              gv.textLayout = new SharpDX.DirectWrite.TextLayout(gv.factoryDWrite, newWord.text + " ", gv.textFormat, gv.Width, gv.Height);
         *              //font = new Font(gv.family, newWord.fontSize, newWord.fontStyle);
         *              float wordWidth = gv.textLayout.Metrics.WidthIncludingTrailingWhitespace;
         *              float height = gv.textLayout.Metrics.Height;
         *              if (height > lineHeight) { lineHeight = (int)height; }
         *
         *              if (xLoc + wordWidth > width) //word wrap
         *              {
         *                  //end last line and add it to the log
         *                  newLine.lineHeight = lineHeight;
         *                  logLinesList.Add(newLine);
         *                  //start a new line and add this word
         *                  newLine = new FormattedLine();
         *                  newLine.wordsList.Add(newWord);
         *                  xLoc = 0;
         *              }
         *              else //no word wrap, just add word
         *              {
         *                  newLine.wordsList.Add(newWord);
         *              }
         *              //instead of drawing, just add to line list
         *              //DrawString(g, word, font, brush, xLoc, yLoc);
         *              xLoc += wordWidth;
         *              newWord = new FormattedWord();
         *          }
         *      }
         *      else if (tagMode)
         *      {
         *          tag += c;
         *      }
         #endregion
         *  }
         * }*/

        public void onDrawLogBox()
        {
            //only draw lines needed to fill textbox
            float xLoc = 0;
            float yLoc = 0;

            //loop through 5 lines from current index point
            for (int i = 0; i < logLinesList.Count; i++)
            {
                //loop through each line and print each word
                foreach (FormattedWord word in logLinesList[i].wordsList)
                {
                    if (gv.textFormat != null)
                    {
                        gv.textFormat.Dispose();
                        gv.textFormat = null;
                    }

                    if (gv.textLayout != null)
                    {
                        gv.textLayout.Dispose();
                        gv.textLayout = null;
                    }
                    gv.textFormat = new SharpDX.DirectWrite.TextFormat(gv.factoryDWrite, gv.family.Name, gv.CurrentFontCollection, word.fontWeight, word.fontStyle, FontStretch.Normal, word.fontSize)
                    {
                        TextAlignment = TextAlignment.Leading, ParagraphAlignment = ParagraphAlignment.Near
                    };
                    gv.textLayout = new SharpDX.DirectWrite.TextLayout(gv.factoryDWrite, word.text + " ", gv.textFormat, gv.Width, gv.Height);
                    float ht         = gv.textLayout.Metrics.Height;
                    float wd         = gv.textLayout.Metrics.Width;
                    int   difYheight = logLinesList[i].lineHeight - (int)word.fontSize;
                    DrawString(word.text + " ", xLoc, yLoc + difYheight, word.fontWeight, word.fontStyle, word.color, word.fontSize, word.underlined);
                    //gv.DrawRectangle(new IbRect((int)xLoc + tbXloc, (int)yLoc + difYheight + tbYloc, (int)wd, (int)ht), SharpDX.Color.White, 1);
                    xLoc += gv.textLayout.Metrics.WidthIncludingTrailingWhitespace;

                    //OLD STUFF
                    //print each word and move xLoc
                    //font = new Font(fontfamily, word.fontSize, word.fontStyle);
                    //int wordWidth = (int)(gv.gCanvas.MeasureString(word.text, font)).Width;
                    //int wordWidth = 12;
                    //brush.Color = word.color;
                    //int difYheight = logLinesList[i].lineHeight - (int)word.fontSize;
                    //DrawString(word.text, xLoc, yLoc + difYheight, word.fontWeight, word.fontStyle, word.color);
                    //xLoc += wordWidth;
                }
                xLoc  = 0;
                yLoc += logLinesList[i].lineHeight;
            }

            //draw border for debug info
            if (showBoxBorder)
            {
                gv.DrawRectangle(new IbRect(tbXloc, tbYloc, tbWidth, tbHeight), Color.DimGray, 1);
            }
        }
コード例 #2
0
ファイル: IbbHtmlLogBox.cs プロジェクト: grannypron/IB2Engine
        /*public void ProcessHtmlString(string text, int width)
         * {
         *  bool tagMode = false;
         *  string tag = "";
         *  FormattedWord newWord = new FormattedWord();
         *  FormattedLine newLine = new FormattedLine();
         *  int lineHeight = 0;
         *
         *  foreach (char c in text)
         *  {
         #region Start/Stop Tags
         *      //start a tag and check for end of word
         *      if (c == '<')
         *      {
         *          tagMode = true;
         *
         *          if (newWord.text != "")
         *          {
         *              newWord.fontStyle = GetFontStyle();
         *              newWord.fontWeight = GetFontWeight();
         *              newWord.fontSize = GetFontSizeInPixels();
         *              newWord.color = GetColor();
         *              gv.textFormat = new SharpDX.DirectWrite.TextFormat(gv.factoryDWrite, gv.family.Name, gv.CurrentFontCollection, newWord.fontWeight, newWord.fontStyle, FontStretch.Normal, newWord.fontSize) { TextAlignment = TextAlignment.Leading, ParagraphAlignment = ParagraphAlignment.Near };
         *              gv.textLayout = new SharpDX.DirectWrite.TextLayout(gv.factoryDWrite, newWord.text + " ", gv.textFormat, gv.Width, gv.Height);
         *              //font = new Font(gv.family, newWord.fontSize, newWord.fontStyle);
         *              float wordWidth = gv.textLayout.Metrics.WidthIncludingTrailingWhitespace;
         *              float height = gv.textLayout.Metrics.Height;
         *              if (height > lineHeight) { lineHeight = (int)height; }
         *
         *              if (xLoc + wordWidth > width) //word wrap
         *              {
         *                  //end last line and add it to the log
         *                  newLine.lineHeight = lineHeight;
         *                  logLinesList.Add(newLine);
         *                  //start a new line and add this word
         *                  newLine = new FormattedLine();
         *                  newLine.wordsList.Add(newWord);
         *                  xLoc = 0;
         *              }
         *              else //no word wrap, just add word
         *              {
         *                  newLine.wordsList.Add(newWord);
         *              }
         *              //instead of drawing, just add to line list
         *              //DrawString(g, word, font, brush, xLoc, yLoc);
         *              xLoc += wordWidth;
         *              newWord = new FormattedWord();
         *          }
         *          continue;
         *      }
         *      //end a tag
         *      else if (c == '>')
         *      {
         *          //check for ending type tag
         *          if (tag.StartsWith("/"))
         *          {
         *              //if </>, remove corresponding tag from stack
         *              string tagMinusSlash = tag.Substring(1);
         *              if (tag.StartsWith("/font"))
         *              {
         *                  for (int i = tagStack.Count - 1; i > 0; i--)
         *                  {
         *                      if (tagStack[i].StartsWith("font"))
         *                      {
         *                          tagStack.RemoveAt(i);
         *                          break;
         *                      }
         *                  }
         *              }
         *              else
         *              {
         *                  tagStack.Remove(tagMinusSlash);
         *              }
         *          }
         *          else
         *          {
         *              //check for line break
         *              if ((tag.ToLower() == "br") || (tag == "BR"))
         *              {
         *                  newWord.fontStyle = GetFontStyle();
         *                  newWord.fontWeight = GetFontWeight();
         *                  newWord.fontSize = GetFontSizeInPixels();
         *                  newWord.color = GetColor();
         *                  //end last line and add it to the log
         *                  newLine.lineHeight = lineHeight;
         *                  logLinesList.Add(newLine);
         *                  //start a new line and add this word
         *                  newLine = new FormattedLine();
         *                  //newLine.wordsList.Add(newWord);
         *                  xLoc = 0;
         *              }
         *              //else if <>, add this tag to the stack
         *              tagStack.Add(tag);
         *          }
         *          tagMode = false;
         *          tag = "";
         *          continue;
         *      }
         #endregion
         *
         #region Words
         *      if (!tagMode)
         *      {
         *          if (c != ' ') //keep adding to word until hit a space
         *          {
         *              newWord.text += c;
         *          }
         *          else //hit a space so end word
         *          {
         *              newWord.fontStyle = GetFontStyle();
         *              newWord.fontWeight = GetFontWeight();
         *              newWord.fontSize = GetFontSizeInPixels();
         *              newWord.color = GetColor();
         *              gv.textFormat = new SharpDX.DirectWrite.TextFormat(gv.factoryDWrite, gv.family.Name, gv.CurrentFontCollection, newWord.fontWeight, newWord.fontStyle, FontStretch.Normal, newWord.fontSize) { TextAlignment = TextAlignment.Leading, ParagraphAlignment = ParagraphAlignment.Near };
         *              gv.textLayout = new SharpDX.DirectWrite.TextLayout(gv.factoryDWrite, newWord.text + " ", gv.textFormat, gv.Width, gv.Height);
         *              //font = new Font(gv.family, newWord.fontSize, newWord.fontStyle);
         *              float wordWidth = gv.textLayout.Metrics.WidthIncludingTrailingWhitespace;
         *              float height = gv.textLayout.Metrics.Height;
         *              if (height > lineHeight) { lineHeight = (int)height; }
         *
         *              if (xLoc + wordWidth > width) //word wrap
         *              {
         *                  //end last line and add it to the log
         *                  newLine.lineHeight = lineHeight;
         *                  logLinesList.Add(newLine);
         *                  //start a new line and add this word
         *                  newLine = new FormattedLine();
         *                  newLine.wordsList.Add(newWord);
         *                  xLoc = 0;
         *              }
         *              else //no word wrap, just add word
         *              {
         *                  newLine.wordsList.Add(newWord);
         *              }
         *              //instead of drawing, just add to line list
         *              //DrawString(g, word, font, brush, xLoc, yLoc);
         *              xLoc += wordWidth;
         *              newWord = new FormattedWord();
         *          }
         *      }
         *      else if (tagMode)
         *      {
         *          tag += c;
         *      }
         #endregion
         *  }
         * }*/

        public void onDrawLogBox()
        {
            //ratio of #lines to #pixels
            float ratio = (float)(logLinesList.Count) / (float)(tbHeight - btn_down.PixelSize.Height - btn_up.PixelSize.Height - btn_scroll.PixelSize.Height);

            if (ratio < 1.0f)
            {
                ratio = 1.0f;
            }
            if (moveDeltaY != 0)
            {
                int lineMove = (startY + moveDeltaY) * (int)ratio;
                SetCurrentTopLineAbsoluteIndex(lineMove);
            }
            //only draw lines needed to fill textbox
            float xLoc     = 0;
            float yLoc     = 3.0f;
            int   maxLines = currentTopLineIndex + numberOfLinesToShow;

            if (maxLines > logLinesList.Count)
            {
                maxLines = logLinesList.Count;
            }
            for (int i = currentTopLineIndex; i < maxLines; i++)
            {
                //loop through each line and print each word
                foreach (FormattedWord word in logLinesList[i].wordsList)
                {
                    if (gv.textFormat != null)
                    {
                        gv.textFormat.Dispose();
                        gv.textFormat = null;
                    }

                    if (gv.textLayout != null)
                    {
                        gv.textLayout.Dispose();
                        gv.textLayout = null;
                    }
                    gv.textFormat = new SharpDX.DirectWrite.TextFormat(gv.factoryDWrite, gv.family.Name, gv.CurrentFontCollection, word.fontWeight, word.fontStyle, FontStretch.Normal, word.fontSize)
                    {
                        TextAlignment = TextAlignment.Leading, ParagraphAlignment = ParagraphAlignment.Near
                    };
                    gv.textLayout = new SharpDX.DirectWrite.TextLayout(gv.factoryDWrite, word.text + " ", gv.textFormat, gv.Width, gv.Height);
                    int difYheight = logLinesList[i].lineHeight - (int)word.fontSize;
                    if (word.underlined)
                    {
                        gv.textLayout.SetUnderline(true, new TextRange(0, word.text.Length - 1));
                    }
                    DrawString(word.text + " ", xLoc, yLoc + difYheight, word.fontWeight, word.fontStyle, word.color, word.fontSize, word.underlined);
                    xLoc += gv.textLayout.Metrics.WidthIncludingTrailingWhitespace;

                    //OLD STUFF
                    //print each word and move xLoc
                    //font = new Font(fontfamily, word.fontSize, word.fontStyle);
                    //int wordWidth = (int)(gv.gCanvas.MeasureString(word.text, font)).Width;
                    //int wordWidth = 12;
                    //brush.Color = word.color;
                    //int difYheight = logLinesList[i].lineHeight - font.Height;
                    //DrawString(word.text, font, brush, xLoc, yLoc + difYheight);
                    //xLoc += wordWidth;
                }
                xLoc  = 0;
                yLoc += logLinesList[i].lineHeight;
            }

            //determine the scrollbutton location
            scrollButtonYLoc = (currentTopLineIndex / (int)ratio);
            if (scrollButtonYLoc > tbHeight - btn_down.PixelSize.Height - btn_scroll.PixelSize.Height)
            {
                scrollButtonYLoc = tbHeight - btn_down.PixelSize.Height - btn_scroll.PixelSize.Height;
            }
            if (scrollButtonYLoc < 0 + btn_up.PixelSize.Height)
            {
                scrollButtonYLoc = 0 + btn_up.PixelSize.Height;
            }

            //draw scrollbar
            for (int y = 0; y < tbHeight - 10; y += 10)
            {
                DrawBitmap(bg_scroll, tbWidth - bg_scroll.PixelSize.Width - 5, y);
            }
            DrawBitmap(btn_up, tbWidth - btn_up.PixelSize.Width, 0);
            DrawBitmap(btn_down, tbWidth - btn_down.PixelSize.Width, tbHeight - btn_down.PixelSize.Height);
            DrawBitmap(btn_scroll, tbWidth - btn_scroll.PixelSize.Width - 1, scrollButtonYLoc);

            //draw border for debug info
            gv.DrawRectangle(new IbRect(tbXloc, tbYloc - gv.oYshift, tbWidth, tbHeight), Color.DimGray, 1);
        }