private DeviceChunkDrawTextEffect AcquireDeviceChunkDrawTextEffect( HtSpan span, string id, string text, HtFont font, HtColor color, DrawTextDeco deco, bool decoStop, DrawTextEffect effect, int effectAmount, HtColor effectColor, bool prevIsWord) { DeviceChunkDrawTextEffect textChunk = (DeviceChunkDrawTextEffect)OP_DeviceChunkDrawTextEffect.Acquire(); textChunk.Span = span; textChunk.Id = id; textChunk.Text = text; textChunk.Font = font; textChunk.Color = color; textChunk.Deco = deco; textChunk.DecoStop = decoStop; textChunk.Effect = effect; textChunk.EffectAmount = effectAmount; textChunk.EffectColor = effectColor; textChunk.PrevIsWord = prevIsWord; textChunk.MeasureSize(); return textChunk; }
/// <summary> /// FillRect implementation /// </summary> /// <param name="rect"></param> /// <param name="color"></param> /// <param name="userData"></param> public override void FillRect(HtRect rect, HtColor color, object userData) { var root = userData as Transform; if (root != null) { var go = new GameObject("fill", typeof(UISprite)); go.layer = root.gameObject.layer; go.transform.parent = root; go.transform.localPosition = new Vector3(rect.X + rect.Width / 2, -rect.Y - rect.Height / 2, 0f); go.transform.localScale = Vector3.one; var spr = go.GetComponent <UISprite>(); spr.pivot = UIWidget.Pivot.Center; spr.atlas = Resources.Load("atlases/white", typeof(UIAtlas)) as UIAtlas; spr.spriteName = "white"; spr.color = new Color32(color.R, color.G, color.B, color.A); spr.type = UISprite.Type.Sliced; spr.width = rect.Width != 0 ? rect.Width : 1; spr.height = rect.Height != 0 ? rect.Height : 1; spr.depth = NGUIHTML.currentDepth; spr.MakePixelPerfect(); } else { HtEngine.Log(HtLogLevel.Error, "Can't draw without root."); } }
/// <summary> /// Draw method. /// </summary> /// <param name="rect">Where to draw</param> /// <param name="color">Text color</param> /// <param name="text">Text</param> public override void Draw(HtRect rect, HtColor color, string text) { // just common implementation using GUIStyle content.text = text; style.normal.textColor = new Color32(color.R, color.G, color.B, color.A); style.Draw(new Rect(rect.X, rect.Y, rect.Width, rect.Height), content, false, false, false, false); }
private DeviceChunkDrawTextEffect AcquireDeviceChunkDrawTextEffect( string id, string text, HtFont font, HtColor color, DrawTextDeco deco, bool decoStop, DrawTextEffect effect, int effectAmount, HtColor effectColor, bool prevIsWord) { DeviceChunkDrawTextEffect textChunk = OP <DeviceChunkDrawTextEffect> .Acquire(); textChunk.Id = id; textChunk.Text = text; textChunk.Font = font; textChunk.Color = color; textChunk.Deco = deco; textChunk.DecoStop = decoStop; textChunk.Effect = effect; textChunk.EffectAmount = effectAmount; textChunk.EffectColor = effectColor; textChunk.PrevIsWord = prevIsWord; textChunk.MeasureSize(); return(textChunk); }
public override void FillRect(HtRect rect, HtColor color) { Debug.Assert(Context != null); var c = Color.FromArgb(color.A, color.R, color.G, color.B); Context.FillRectangle(new SolidBrush(c), rect.X, rect.Y, rect.Width, rect.Height); }
/// <summary> /// Draw method /// </summary> /// <param name="rect">Where to draw</param> /// <param name="color">Color to use (ignored for now)</param> /// <param name="linkText">Link text</param> /// <param name="userData">User data</param> public override void Draw(string id, HtRect rect, HtColor color, string linkText, object userData) { if (isTime) { var now = DateTime.Now; timeStyle.Draw(new Rect(rect.X, rect.Y, rect.Width, rect.Height), string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", now.Hour, now.Minute, now.Second, now.Millisecond), false, false, false, false); } else if (this.Texture != null) { // store current gui color var oldColor = GUI.color; // set control id. if (!string.IsNullOrEmpty(id)) { GUI.SetNextControlName(id); } // fill rect with given color GUI.color = new Color32(color.R, color.G, color.B, color.A); GUI.DrawTexture(new Rect(rect.X, rect.Y, rect.Width, rect.Height), this.Texture); // restore gui color GUI.color = oldColor; } }
public override void Draw(HtRect rect, HtColor color, string text) { Debug.Assert(HtmlDevice.Context != null); var c = Color.FromArgb(color.A, color.R, color.G, color.B); HtmlDevice.Context.DrawString(text, this.font, new SolidBrush(c), rect.X, rect.Y, StringFormat.GenericTypographic); }
/// <summary> /// FillRect implementation /// </summary> /// <param name="rect"></param> /// <param name="color"></param> /// <param name="userData"></param> public override void FillRect(HtRect rect, HtColor color, object userData) { var root = userData as Transform; if (root != null) { var go = new GameObject("fill", typeof(UISprite)); go.layer = root.gameObject.layer; go.transform.parent = root; go.transform.localPosition = new Vector3(rect.X + rect.Width / 2, -rect.Y - rect.Height / 2 - 2, -1f); go.transform.localScale = new Vector3(rect.Width, rect.Height, 1f); var spr = go.GetComponent <UISprite>(); spr.pivot = UIWidget.Pivot.Center; spr.atlas = Fillatlas; //PacketManage.Single.GetPacket("rom_upd").Load("html_engine") as UIAtlas; //Resources.Load("atlases/white", typeof(UIAtlas)) as UIAtlas; spr.spriteName = "white"; spr.color = new Color32(color.R, color.G, color.B, color.A); spr.MakePixelPerfect(); if (go.transform.localScale.y == 0f) { go.transform.localScale = new Vector3(go.transform.localScale.x, 1f, 1f); } } else { HtEngine.Log(HtLogLevel.Error, "Can't draw without root."); } }
public override void Draw(HtRect rect, HtColor color, string text) { if (this.font != null) { HtmlDevice.Context.DrawString(this.font, text, new Vector2(rect.X, rect.Y), new Color(color.R, color.G, color.B, color.A)); } }
public override void Draw(HtRect rect, HtColor color) { Debug.Assert(HtmlDevice.Context != null); if (this.texture != null) { HtmlDevice.Context.Draw(this.texture, new Rectangle(rect.X, rect.Y, rect.Width, rect.Height), Color.White); } }
/// <summary> /// Draw method. /// </summary> /// <param name="rect">Where to draw</param> /// <param name="color">Text color</param> /// <param name="text">Text</param> /// <param name="isEffect">Is effect</param> /// <param name="effect">Effect</param> /// <param name="effectColor">Effect color</param> /// <param name="effectAmount">Effect amount</param> /// <param name="linkText">Link text</param> /// <param name="userData">User data</param> public override void Draw(string id, HtRect rect, HtColor color, string text, bool isEffect, Core.DrawTextEffect effect, HtColor effectColor, int effectAmount, string linkText, object userData) { // just common implementation using GUIStyle if (string.IsNullOrEmpty(id)) { GUI.SetNextControlName(id); } content.text = text; style.normal.textColor = new Color32(color.R, color.G, color.B, color.A); style.Draw(new Rect(rect.X, rect.Y, rect.Width, rect.Height), content, false, false, false, false); }
public override void FillRect(HtRect rect, HtColor color) { Debug.Assert(Context != null); if (WhiteTexture == null) { WhiteTexture = new Texture2D(Context.GraphicsDevice, 1, 1); WhiteTexture.SetData(new[] {Color.White}); } Context.Draw(WhiteTexture, new Rectangle(rect.X, rect.Y, rect.Width, rect.Height), new Color(color.R, color.G, color.B, color.A)); }
public override void FillRect(HtRect rect, HtColor color) { Debug.Assert(Context != null); if (WhiteTexture == null) { WhiteTexture = new Texture2D(Context.GraphicsDevice, 1, 1); WhiteTexture.SetData(new[] { Color.White }); } Context.Draw(WhiteTexture, new Rectangle(rect.X, rect.Y, rect.Width, rect.Height), new Color(color.R, color.G, color.B, color.A)); }
/// <summary> /// Draw method /// </summary> /// <param name="rect">Where to draw</param> /// <param name="color">Color to use (ignored for now)</param> public override void Draw(HtRect rect, HtColor color) { if (isTime) { var now = DateTime.Now; timeStyle.Draw(new Rect(rect.X, rect.Y, rect.Width, rect.Height), string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", now.Hour, now.Minute, now.Second, now.Millisecond), false, false, false, false); } else if (this.Texture != null) { GUI.DrawTexture(new Rect(rect.X, rect.Y, rect.Width, rect.Height), this.Texture); } }
/// <summary> /// Draw method /// </summary> /// <param name="rect">Where to draw</param> /// <param name="color">Color to use (ignored for now)</param> public override void Draw(HtRect rect, HtColor color) { if (isTime) { var now = DateTime.Now; timeStyle.Draw(new Rect(rect.X, rect.Y, rect.Width, rect.Height), string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}",now.Hour,now.Minute,now.Second,now.Millisecond), false, false, false, false); } else if (this.Texture != null) { GUI.DrawTexture(new Rect(rect.X, rect.Y, rect.Width, rect.Height), this.Texture); } }
/// <summary> /// FillRect implementation /// </summary> /// <param name="rect"></param> /// <param name="color"></param> public override void FillRect(HtRect rect, HtColor color) { // create white texture if need if (whiteTex==null) { whiteTex = new Texture2D(1, 1, TextureFormat.RGBA32, false); whiteTex.SetPixel(0, 0, Color.white); whiteTex.Apply(false, true); } // store current gui color var oldColor = GUI.color; // fill rect with given color GUI.color = new Color32(color.R,color.G,color.B,color.A); GUI.DrawTexture(new Rect(rect.X, rect.Y, rect.Width, rect.Height), whiteTex); // restore gui color GUI.color = oldColor; }
/// <summary> /// FillRect implementation /// </summary> /// <param name="rect"></param> /// <param name="color"></param> public override void FillRect(HtRect rect, HtColor color) { // create white texture if need if (whiteTex == null) { whiteTex = new Texture2D(1, 1, TextureFormat.RGBA32, false); whiteTex.SetPixel(0, 0, Color.white); whiteTex.Apply(false, true); } // store current gui color var oldColor = GUI.color; // fill rect with given color GUI.color = new Color32(color.R, color.G, color.B, color.A); GUI.DrawTexture(new Rect(rect.X, rect.Y, rect.Width, rect.Height), whiteTex); // restore gui color GUI.color = oldColor; }
public void Awake() { Debug.Log("Initializing Demo"); // our logger HtEngine.RegisterLogger(new Unity3DLogger()); // our device HtEngine.RegisterDevice(new NGUIDevice()); // link hover color. HtEngine.LinkHoverColor = HtColor.Parse("#FF4444"); // link pressed factor. HtEngine.LinkPressedFactor = 0.5f; // link function name. HtEngine.LinkFunctionName = "onLinkClicked"; html = GetComponent <NGUIHTML>(); html.html = demo0; }
/// <summary> /// FillRect implementation /// </summary> /// <param name="rect"></param> /// <param name="color"></param> /// <param name="userData"></param> public override void FillRect(HtRect rect, HtColor color, object userData) { var root = userData as Transform; if (root != null) { var go = new GameObject("fill", typeof(UISprite)); go.layer = root.gameObject.layer; go.transform.parent = root; go.transform.localPosition = new Vector3(rect.X + rect.Width / 2, -rect.Y - rect.Height / 2, 0f); go.transform.localScale = new Vector3(1f, 0.5f, 1f);//Vector3.one; var spr = go.GetComponent<UISprite>(); spr.pivot = UIWidget.Pivot.Center; spr.atlas = Resources.Load("HTMLEngine/white", typeof(UIAtlas)) as UIAtlas; spr.spriteName = "white"; spr.color = new Color32(color.R, color.G, color.B, color.A); spr.type = UISprite.Type.Sliced; spr.width = rect.Width != 0 ? rect.Width : 1; spr.height = rect.Height != 0 ? rect.Height : 1; spr.depth = NGUIHTML.currentDepth; //spr.MakePixelPerfect(); } else { HtEngine.Log(HtLogLevel.Error, "Can't draw without root."); } }
/// <summary> /// Draw method. /// </summary> /// <param name="rect">Where to draw</param> /// <param name="color">Text color</param> /// <param name="text">Text</param> /// <param name="isEffect">Is effect</param> /// <param name="effect">Effect</param> /// <param name="effectColor">Effect color</param> /// <param name="effectAmount">Effect amount</param> /// <param name="linkText">Link text</param> /// <param name="userData">User data</param> public override void Draw(string id, HtRect rect, HtColor color, string text, bool isEffect, Core.DrawTextEffect effect, HtColor effectColor, int effectAmount, string linkText, object userData) { if (isEffect) { return; } var root = userData as Transform; if (root != null) { var go = new GameObject(string.IsNullOrEmpty(id) ? "label" : id, typeof(UILabel)); go.layer = root.gameObject.layer; go.transform.parent = root; go.transform.localPosition = new Vector3(rect.X + rect.Width / 2, -rect.Y - rect.Height / 2, 0f); go.transform.localScale = Vector3.one;//Vector3.zero; //new Vector3(this.style.font.fontSize, this.style.font.fontSize, 1f); var lab = go.GetComponent <UILabel>(); lab.pivot = UIWidget.Pivot.Center; lab.supportEncoding = false; lab.bitmapFont = m_font; lab.fontSize = this.Size; lab.text = text; lab.color = new Color32(color.R, color.G, color.B, color.A); switch (effect) { case Core.DrawTextEffect.Outline: lab.effectStyle = UILabel.Effect.Outline; break; case Core.DrawTextEffect.Shadow: lab.effectStyle = UILabel.Effect.Shadow; break; } if (this.Bold && this.Italic) { lab.fontStyle = FontStyle.BoldAndItalic; } else if (this.Bold) { lab.fontStyle = FontStyle.Bold; } else if (this.Italic) { lab.fontStyle = FontStyle.Italic; } lab.effectColor = new Color32(effectColor.R, effectColor.G, effectColor.B, effectColor.A); lab.effectDistance = new Vector2(effectAmount, effectAmount); lab.MakePixelPerfect(); lab.width = rect.Width + 1; lab.height = rect.Height + 1; // build link. if (!string.IsNullOrEmpty(linkText)) { var collider = go.AddComponent <BoxCollider>(); collider.isTrigger = true; lab.autoResizeBoxCollider = true; lab.ResizeCollider(); var nguiLinkText = go.AddComponent <NGUILinkText>(); nguiLinkText.linkText = linkText; var uiButtonColor = go.AddComponent <UIButtonColor>(); uiButtonColor.tweenTarget = go; uiButtonColor.hover = new Color32( HtEngine.LinkHoverColor.R, HtEngine.LinkHoverColor.G, HtEngine.LinkHoverColor.B, HtEngine.LinkHoverColor.A); uiButtonColor.pressed = new Color( lab.color.r * HtEngine.LinkPressedFactor, lab.color.g * HtEngine.LinkPressedFactor, lab.color.b * HtEngine.LinkPressedFactor, lab.color.a); uiButtonColor.duration = 0f; var uiButtonMessage = go.AddComponent <UIButtonMessage>(); uiButtonMessage.target = root.gameObject; uiButtonMessage.functionName = HtEngine.LinkFunctionName; } } else { HtEngine.Log(HtLogLevel.Error, "Can't draw without root."); } }
public abstract void Draw(HtSpan span, string id, HtRect rect, HtColor color, string text, bool isEffect, Core.DrawTextEffect effect, HtColor effectColor, int effectAmount, Core.HtmlLink link, object userData);
public abstract void FillRect(HtRect rect, HtColor color, object userData);
public override void Draw(HtRect rect, HtColor color) { Debug.Assert(HtmlDevice.Context != null); HtmlDevice.Context.DrawImage(this.image, rect.X, rect.Y, rect.Width, rect.Height); }
public void Parse(IEnumerator<HtmlChunk> htmlChunks, int viewportWidth, string id = null, HtFont font = null, HtColor color = default(HtColor), TextAlign align = TextAlign.Left, VertAlign valign = VertAlign.Bottom) { this.Clear(); var defaultFont = HtEngine.Device.LoadFont(HtEngine.DefaultFontFace, HtEngine.DefaultFontSize, false, false, 0, 0); font = font == null ? defaultFont : font; color = (color.R == 0 && color.G == 0 && color.B == 0 && color.A == 0) ? HtEngine.DefaultColor : color; //string id = null; //var align = TextAlign.Left; //var valign = VertAlign.Bottom; DrawTextDeco deco = DrawTextDeco.None; DrawTextEffect effect = DrawTextEffect.None; HtColor effectColor = HtEngine.DefaultColor; int effectAmount = 1; HtmlLink currentLink = null; bool prevIsWord = false; Stack<HtSpan> spanStack = new Stack<HtSpan>(); //top item is current span spanStack.Push(null); DeviceChunkLine currLine = null; DeviceChunkDrawText lastTextChunk = null; //for (int i = 0; i < htmlChunks.Count; i++) while (htmlChunks.MoveNext()) { HtmlChunk htmlChunk = htmlChunks.Current; var word = htmlChunk as HtmlChunkWord; if (word != null) { if (currLine == null) { currLine = this.NewLine(null, viewportWidth, align, valign); } if (effect == DrawTextEffect.None) { lastTextChunk = AcquireDeviceChunkDrawText( spanStack.Peek(), id, word.Text, font, color, deco, lastTextChunk != null && lastTextChunk.Deco != deco, prevIsWord); } else { lastTextChunk = AcquireDeviceChunkDrawTextEffect( spanStack.Peek(), id, word.Text, font, color, deco, lastTextChunk != null && lastTextChunk.Deco != deco, effect, effectAmount, effectColor, prevIsWord); } if (currLine.AddChunk(lastTextChunk, prevIsWord)) { if (currentLink != null && !this.Links.ContainsKey(lastTextChunk)) this.Links.Add(lastTextChunk, currentLink); } else { //prevIsWord = true; //why? AddChunk return false means a soft line break is needed //currLine.IsFull = true; string lastText = lastTextChunk.Text; lastTextChunk.Dispose(); lastTextChunk = null; bool decoStop = lastTextChunk != null && lastTextChunk.Deco != deco; //Modify dfn.li //if (!currLine.IsFull) { var prevWidth = currLine.PrevChunkWidth(); var avaWidth = currLine.AvailWidth; string sFix = ""; int nPos = 0; for (nPos=0; nPos < lastText.Length; ++nPos ) { char ch = lastText[nPos]; var len = font.Measure(ch.ToString()).Width; if (prevWidth + len > viewportWidth) { break; } else { sFix = sFix + ch; prevWidth += len; } } //补全上一行 if (!string.IsNullOrEmpty(sFix)) { DeviceChunkDrawText tmpTextChunk; if (effect == DrawTextEffect.None) { tmpTextChunk = AcquireDeviceChunkDrawText( spanStack.Peek(), id, sFix, font, color, deco, lastTextChunk != null && lastTextChunk.Deco != deco, prevIsWord); } else { tmpTextChunk = AcquireDeviceChunkDrawTextEffect( spanStack.Peek(), id, sFix, font, color, deco, lastTextChunk != null && lastTextChunk.Deco != deco, effect, effectAmount, effectColor, prevIsWord); } if (currLine.AddChunk(tmpTextChunk, prevIsWord, false)) { if (currentLink != null && !this.Links.ContainsKey(tmpTextChunk)) this.Links.Add(tmpTextChunk, currentLink); } else { tmpTextChunk.Dispose(); tmpTextChunk = null; } } lastText = lastText.Substring(nPos); //currLine.IsFull = true; } //Modify End // find prefix ascii string. //string prefixAsciiText = null; int pos = 0; //for (pos = 0; pos < lastText.Length; ++pos) { // char ch = lastText[pos]; // if (ch > 255 || ch == ' ') { // prefixAsciiText = lastText.Substring(0, pos); // break; // } //} //if (prefixAsciiText != null) { // DeviceChunkDrawText prefixAsciiTextChunk; // if (effect == DrawTextEffect.None) { // prefixAsciiTextChunk = AcquireDeviceChunkDrawText( // id, // prefixAsciiText, // font, // color, // deco, // decoStop, // prevIsWord); // } else { // prefixAsciiTextChunk = AcquireDeviceChunkDrawTextEffect( // id, // prefixAsciiText, // font, // color, // deco, // decoStop, // effect, // effectAmount, // effectColor, // prevIsWord); // } // if (currLine.AddChunk(prefixAsciiTextChunk, prevIsWord)) { // if (currentLink != null && !this.Links.ContainsKey(prefixAsciiTextChunk)) // this.Links.Add(prefixAsciiTextChunk, currentLink); // lastText = lastText.Substring(pos); // decoStop = false; // } //} //// reset the pos. //pos = 0; // add multi-lines. int remainingWidth = viewportWidth; for (; pos < lastText.Length; ) { char ch = lastText[pos]; remainingWidth -= font.Measure(ch.ToString()).Width; if (remainingWidth < 0) { string tmpText = lastText.Substring(0, pos); DeviceChunkDrawText tmpTextChunk; if (effect == DrawTextEffect.None) { tmpTextChunk = AcquireDeviceChunkDrawText( spanStack.Peek(), id, tmpText, font, color, deco, decoStop, prevIsWord); } else { tmpTextChunk = AcquireDeviceChunkDrawTextEffect( spanStack.Peek(), id, tmpText, font, color, deco, decoStop, effect, effectAmount, effectColor, prevIsWord); } currLine = this.NewLine(currLine, viewportWidth, align, valign); if (currLine.AddChunk(tmpTextChunk, prevIsWord,false)) { if (currentLink != null && !this.Links.ContainsKey(tmpTextChunk)) this.Links.Add(tmpTextChunk, currentLink); } else { tmpTextChunk.Dispose(); tmpTextChunk = null; } lastText = lastText.Substring(pos); pos = 0; remainingWidth = viewportWidth; } else { ++pos; } } // add last line. if (!string.IsNullOrEmpty(lastText)) { if (effect == DrawTextEffect.None) { lastTextChunk = AcquireDeviceChunkDrawText( spanStack.Peek(), id, lastText, font, color, deco, decoStop, prevIsWord); } else { lastTextChunk = AcquireDeviceChunkDrawTextEffect( spanStack.Peek(), id, lastText, font, color, deco, decoStop, effect, effectAmount, effectColor, prevIsWord); } currLine = this.NewLine(currLine, viewportWidth, align, valign); if (currLine.AddChunk(lastTextChunk, prevIsWord,false)) { if (currentLink != null && !this.Links.ContainsKey(lastTextChunk)) this.Links.Add(lastTextChunk, currentLink); } else { lastTextChunk.Dispose(); lastTextChunk = null; } } } prevIsWord = true; } else { prevIsWord = false; } var tag = htmlChunk as HtmlChunkTag; if (tag != null) { switch (tag.Tag) { case "spin": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { id = null; this.FinishLine(currLine, align, valign); return; // return control to parent } else { id = tag.GetAttr("id"); ExctractAligns(tag, ref align, ref valign); var compiled = (DeviceChunkDrawCompiled)OP_DeviceChunkDrawCompiled.Acquire(); compiled.Font = font; var scompiledWidth = tag.GetAttr("width") ?? "0"; var compiledWidth = 0; if (!int.TryParse(scompiledWidth, out compiledWidth)) compiledWidth = 0; if (compiledWidth == 0) { compiledWidth = currLine == null ? viewportWidth : currLine.AvailWidth - font.WhiteSize; } if (compiledWidth > 0) { if (compiledWidth > viewportWidth) compiledWidth = viewportWidth; compiled.Parse(htmlChunks, compiledWidth, id, font, color, align, valign); compiled.MeasureSize(); if (currLine == null) { currLine = this.NewLine(null, viewportWidth, align, valign); } if (!currLine.AddChunk(compiled, prevIsWord)) { currLine.IsFull = true; currLine = this.NewLine(currLine, viewportWidth, align, valign); if (!currLine.AddChunk(compiled, prevIsWord)) { HtEngine.Log(HtLogLevel.Error, "Could not fit spin into line. Word is too big: {0}", compiled); compiled.Dispose(); compiled = null; } } } else { HtEngine.Log(HtLogLevel.Warning, "spin width is not given"); } } break; case "effect": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { effect = DrawTextEffect.None; } else { var name = tag.GetAttr("name") ?? "outline"; switch (name) { case "shadow": effect = DrawTextEffect.Shadow; effectAmount = 1; effectColor = HtColor.RGBA(0, 0, 0, 80); break; case "outline": effect = DrawTextEffect.Outline; effectAmount = 1; effectColor = HtColor.RGBA(0xFF, 0xFF, 0xFF, 80); break; } var amount = tag.GetAttr("amount"); if (amount != null) { if (!int.TryParse(amount, out effectAmount)) { HtEngine.Log(HtLogLevel.Error, "Invalid numeric value: " + amount); } } var colors = tag.GetAttr("color"); if (colors != null) effectColor = HtColor.Parse(colors); } break; case "u": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { deco &= ~DrawTextDeco.Underline; } else { deco |= DrawTextDeco.Underline; } break; case "s": case "strike": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { deco &= ~DrawTextDeco.Strike; } else { deco |= DrawTextDeco.Strike; } break; case "code": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { font = this.fontStack.Count > 0 ? this.fontStack.Pop() : defaultFont; } else { this.fontStack.Push(font); //const string fontName = "code"; const string fontName = "default"; int fontSize = font.Size; bool fontBold = font.Bold; bool fontItal = font.Italic; int spacingX = font.SpacingX; int spacingY = font.SpacingY; font = HtEngine.Device.LoadFont(fontName, fontSize, fontBold, fontItal, spacingX, spacingY); } break; case "b": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { font = this.fontStack.Count > 0 ? this.fontStack.Pop() : defaultFont; } else { this.fontStack.Push(font); string fontName = font.Face; int fontSize = font.Size; const bool fontBold = true; bool fontItal = font.Italic; int spacingX = font.SpacingX; int spacingY = font.SpacingY; font = HtEngine.Device.LoadFont(fontName, fontSize, fontBold, fontItal, spacingX, spacingY); } break; case "i": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { font = this.fontStack.Count > 0 ? this.fontStack.Pop() : defaultFont; } else { this.fontStack.Push(font); string fontName = font.Face; int fontSize = font.Size; bool fontBold = font.Bold; const bool fontItal = true; int spacingX = font.SpacingX; int spacingY = font.SpacingY; font = HtEngine.Device.LoadFont(fontName, fontSize, fontBold, fontItal, spacingX, spacingY); } break; case "a": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { id = null; //if (this.colorStack.Count>0) //不添加颜色。需要颜色时从外部指定 // color = this.colorStack.Pop(); currentLink = null; } else { id = tag.GetAttr("id"); currentLink = new HtmlLink(); currentLink.linkID = linkID; currentLink.linkText = tag.GetAttr("href"); ++linkID; //this.colorStack.Push(color); //不添加颜色。需要颜色时从外部指定 //color = HtEngine.DefaultLinkColor; } break; case "font": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { font = this.fontStack.Count > 0 ? this.fontStack.Pop() : defaultFont; color = this.colorStack.Count > 0 ? this.colorStack.Pop() : HtEngine.DefaultColor; } else { this.fontStack.Push(font); this.colorStack.Push(color); string fontName = tag.GetAttr("face") ?? font.Face; string fontSizeS = tag.GetAttr("size"); string fontSpaceXS = tag.GetAttr("spacing-x"); string fontSpaceYS = tag.GetAttr("spacing-y"); int fontSize; if (fontSizeS == null || !int.TryParse(fontSizeS, out fontSize)) fontSize = font.Size; int fontSpaceX; if (fontSpaceXS == null || !int.TryParse(fontSpaceXS, out fontSpaceX)) fontSpaceX = 0; int fontSpaceY; if (fontSpaceYS == null || !int.TryParse(fontSpaceYS, out fontSpaceY)) fontSpaceY = 0; bool fontBold = font.Bold; bool fontItal = font.Italic; font = HtEngine.Device.LoadFont(fontName, fontSize, fontBold, fontItal, fontSpaceX, fontSpaceY); color = HtColor.Parse(tag.GetAttr("color"), color); } break; case "br": currLine = this.NewLine(currLine, viewportWidth, align, valign); currLine.Height = font.LineSpacing; break; case "img": if (tag.IsClosing) { // ignore closing tags } else { var src = tag.GetAttr("src"); var widthS = tag.GetAttr("width"); var heightS = tag.GetAttr("height"); var fpsS = tag.GetAttr("fps"); var imgId = tag.GetAttr("id"); var snapS = tag.GetAttr("snap"); int w, h, fps; bool snap;//true : 原像素,false:进行缩放 if (widthS == null || !int.TryParse(widthS, out w)) w = -1; if (heightS == null || !int.TryParse(heightS, out h)) h = -1; if (fpsS == null || !int.TryParse(fpsS, out fps)) fps = -1; if (snapS == null || !bool.TryParse(snapS, out snap)) snap = true; //var img = HtEngine.Device.LoadImage(src, fps); var img = HtEngine.Device.LoadImageEx(src, fps, snap, w, h); if (w < 0) w = img.Width; if (h < 0) h = img.Height; var dChunk = (DeviceChunkDrawImage)OP_DeviceChunkDrawImage.Acquire(); if (currLine == null) currLine = this.NewLine(null, viewportWidth, align, valign); dChunk.Image = img; dChunk.Rect.Width = snap ? img.Width : w; dChunk.Rect.Height = snap ? img.Height : h; dChunk.Font = font; // for whitespace measure dChunk.Id = imgId; //HtEngine.Log(HtLogLevel.Debug, "Adding image w={0} h={1}",dChunk.Width,dChunk.Height); if (currLine.AddChunk(dChunk, prevIsWord)) { if (currentLink != null && !this.Links.ContainsKey(dChunk)) this.Links.Add(dChunk, currentLink); } else { currLine.IsFull = true; currLine = this.NewLine(currLine, viewportWidth, align, valign); if (currLine.AddChunk(dChunk, prevIsWord)) { if (currentLink != null && !this.Links.ContainsKey(dChunk)) this.Links.Add(dChunk, currentLink); } else { HtEngine.Log(HtLogLevel.Error, "Could not fit image into line. Image is too big: {0}", dChunk); dChunk.Dispose(); dChunk = null; } } } break; case "gameobj": if (tag.IsClosing) { // ignore closing tags } else { var command = tag.GetAttr("command"); var boxcollider = tag.GetAttr("boxcollider"); var componentname = tag.GetAttr("componentname"); var param = tag.GetAttr("param"); var width = tag.GetAttr("width"); var height = tag.GetAttr("height"); var objid = tag.GetAttr("id"); int w, h; bool b; if (width == null || !int.TryParse(width, out w)) w = 1; if (height == null || !int.TryParse(height, out h)) h = 1; if (boxcollider == null || !bool.TryParse(boxcollider, out b)) b = false; var dChunk = (DeviceChunkDrawGameObject)OP_DeviceChunkDrawGameObject.Acquire(); if (currLine == null) currLine = this.NewLine(null, viewportWidth, align, valign); var go = new HTMLEngine.NGUI.NGUIGameObject(command, b, componentname, param); dChunk.gameobject = go; dChunk.Rect.Width = w; dChunk.Rect.Height = h; dChunk.Font = font; // for whitespace measure dChunk.Id = objid; //HtEngine.Log(HtLogLevel.Debug, "Adding image w={0} h={1}",dChunk.Width,dChunk.Height); if (currLine.AddChunk(dChunk, prevIsWord)) { if (currentLink != null && !this.Links.ContainsKey(dChunk)) this.Links.Add(dChunk, currentLink); } else { currLine.IsFull = true; currLine = this.NewLine(currLine, viewportWidth, align, valign); if (currLine.AddChunk(dChunk, prevIsWord)) { if (currentLink != null && !this.Links.ContainsKey(dChunk)) this.Links.Add(dChunk, currentLink); } else { HtEngine.Log(HtLogLevel.Error, "Could not fit gameobj into line. gameobj is too big: {0}", dChunk); dChunk.Dispose(); dChunk = null; } } } break; case "p": if (tag.IsClosing) { id = null; } else { id = tag.GetAttr("id"); currLine = this.NewLine(currLine, viewportWidth, align, valign); ExctractAligns(tag, ref align, ref valign); } break; case "span": if (tag.IsClosing) { spanStack.Pop(); id = null; } else { HtSpan currentSpan = spanStack.Peek(); HtSpan newSpan = new HtSpan(currentSpan, tag.GetAllAttributes()); spanStack.Push(newSpan); id = tag.GetAttr("id"); //currLine = this.NewLine(currLine, viewportWidth, align, valign); ExctractAligns(tag, ref align, ref valign); } break; default: HtEngine.Log(HtLogLevel.Error, "Unsupported html tag {0}", tag); break; } } } // align last line this.FinishLine(currLine, align, valign); }
public abstract void Draw(HtRect rect, HtColor color, string text);
public override void Draw(HtRect rect, HtColor color, string text) { if (this.font != null) HtmlDevice.Context.DrawString(this.font, text, new Vector2(rect.X, rect.Y), new Color(color.R, color.G, color.B, color.A)); }
public void Parse(IEnumerator <HtmlChunk> htmlChunks, int viewportWidth, TextAlign align = TextAlign.Left, VertAlign valign = VertAlign.Bottom) { this.Clear(); var defaultFont = HtEngine.Device.LoadFont(HtEngine.DefaultFontFace, HtEngine.DefaultFontSize, false, false); var font = defaultFont; var color = HtEngine.DefaultColor; //var align = TextAlign.Left; //var valign = VertAlign.Bottom; DrawTextDeco deco = DrawTextDeco.None; DrawTextEffect effect = DrawTextEffect.None; HtColor effectColor = HtEngine.DefaultColor; int effectAmount = 1; string currentLink = null; DeviceChunkLine currLine = null; DeviceChunkDrawText lastTextChunk = null; //for (int i = 0; i < htmlChunks.Count; i++) while (htmlChunks.MoveNext()) { HtmlChunk htmlChunk = htmlChunks.Current; var word = htmlChunk as HtmlChunkWord; if (word != null) { if (currLine == null) { currLine = this.NewLine(null, viewportWidth, align, valign); } if (effect == DrawTextEffect.None) { var textChunk = OP <DeviceChunkDrawText> .Acquire(); textChunk.Text = word.Text; textChunk.Font = font; textChunk.Color = color; textChunk.Deco = deco; if (lastTextChunk != null && lastTextChunk.Deco != deco) { lastTextChunk.DecoStop = true; } textChunk.MeasureSize(); lastTextChunk = textChunk; } else { var textChunk = OP <DeviceChunkDrawTextEffect> .Acquire(); textChunk.Text = word.Text; textChunk.Font = font; textChunk.Color = color; textChunk.Deco = deco; if (lastTextChunk != null && lastTextChunk.Deco != deco) { lastTextChunk.DecoStop = true; } textChunk.Effect = effect; textChunk.EffectAmount = effectAmount; textChunk.EffectColor = effectColor; textChunk.MeasureSize(); lastTextChunk = textChunk; } if (currentLink != null) { this.Links.Add(new KeyValuePair <DeviceChunk, string>(lastTextChunk, currentLink)); } if (!currLine.AddChunk(lastTextChunk)) { currLine.IsFull = true; currLine = this.NewLine(currLine, viewportWidth, align, valign); if (!currLine.AddChunk(lastTextChunk)) { HtEngine.Log(HtLogLevel.Error, "Could not fit word into line. Word is too big: {0}", lastTextChunk); lastTextChunk.Dispose(); lastTextChunk = null; } } } var tag = htmlChunk as HtmlChunkTag; if (tag != null) { switch (tag.Tag) { case "div": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { this.FinishLine(currLine, align, valign); return; // return control to parent } else { ExctractAligns(tag, ref align, ref valign); var compiled = OP <DeviceChunkDrawCompiled> .Acquire(); compiled.Font = font; var scompiledWidth = tag.GetAttr("width") ?? "0"; var compiledWidth = 0; if (!int.TryParse(scompiledWidth, out compiledWidth)) { compiledWidth = 0; } if (compiledWidth == 0) { compiledWidth = currLine == null ? viewportWidth : currLine.AvailWidth - font.WhiteSize; } if (compiledWidth > 0) { compiled.Parse(htmlChunks, compiledWidth, align, valign); compiled.MeasureSize(); if (currLine == null) { currLine = this.NewLine(null, viewportWidth, align, valign); } if (!currLine.AddChunk(compiled)) { currLine.IsFull = true; currLine = this.NewLine(currLine, viewportWidth, align, valign); if (!currLine.AddChunk(compiled)) { HtEngine.Log(HtLogLevel.Error, "Could not fit div into line. Word is too big: {0}", lastTextChunk); compiled.Dispose(); compiled = null; } } } else { HtEngine.Log(HtLogLevel.Warning, "div width is not given"); } } break; case "effect": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { effect = DrawTextEffect.None; } else { var name = tag.GetAttr("name") ?? "outline"; switch (name) { case "shadow": effect = DrawTextEffect.Shadow; effectAmount = 1; effectColor = HtColor.RGBA(0, 0, 0, 80); break; case "outline": effect = DrawTextEffect.Outline; effectAmount = 1; effectColor = HtColor.RGBA(0xFF, 0xFF, 0xFF, 80); break; } var amount = tag.GetAttr("amount"); if (amount != null) { if (!int.TryParse(amount, out effectAmount)) { HtEngine.Log(HtLogLevel.Error, "Invalid numeric value: " + amount); } } var colors = tag.GetAttr("color"); if (colors != null) { effectColor = HtColor.Parse(colors); } } break; case "u": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { deco &= ~DrawTextDeco.Underline; } else { deco |= DrawTextDeco.Underline; } break; case "s": case "strike": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { deco &= ~DrawTextDeco.Strike; } else { deco |= DrawTextDeco.Strike; } break; case "code": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { font = this.fontStack.Count > 0 ? this.fontStack.Pop() : defaultFont; } else { this.fontStack.Push(font); const string fontName = "code"; int fontSize = font.Size; bool fontBold = font.Bold; bool fontItal = font.Italic; font = HtEngine.Device.LoadFont(fontName, fontSize, fontBold, fontItal); } break; case "b": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { font = this.fontStack.Count > 0 ? this.fontStack.Pop() : defaultFont; } else { this.fontStack.Push(font); string fontName = font.Face; int fontSize = font.Size; const bool fontBold = true; bool fontItal = font.Italic; font = HtEngine.Device.LoadFont(fontName, fontSize, fontBold, fontItal); } break; case "i": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { font = this.fontStack.Count > 0 ? this.fontStack.Pop() : defaultFont; } else { this.fontStack.Push(font); string fontName = font.Face; int fontSize = font.Size; bool fontBold = font.Bold; const bool fontItal = true; font = HtEngine.Device.LoadFont(fontName, fontSize, fontBold, fontItal); } break; case "a": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { if (this.colorStack.Count > 0) { color = this.colorStack.Pop(); } currentLink = null; } else { currentLink = tag.GetAttr("href"); this.colorStack.Push(color); color = HtEngine.DefaultLinkColor; } break; case "font": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { font = this.fontStack.Count > 0 ? this.fontStack.Pop() : defaultFont; color = this.colorStack.Count > 0 ? this.colorStack.Pop() : HtEngine.DefaultColor; } else { this.fontStack.Push(font); this.colorStack.Push(color); string fontName = tag.GetAttr("face") ?? font.Face; string fontSizeS = tag.GetAttr("size"); int fontSize; if (fontSizeS == null || !int.TryParse(fontSizeS, out fontSize)) { fontSize = font.Size; } bool fontBold = font.Bold; bool fontItal = font.Italic; font = HtEngine.Device.LoadFont(fontName, fontSize, fontBold, fontItal); color = HtColor.Parse(tag.GetAttr("color"), color); } break; case "br": currLine = this.NewLine(currLine, viewportWidth, align, valign); currLine.Height = font.LineSpacing; break; case "img": if (tag.IsClosing) { // ignore closing tags } else { var src = tag.GetAttr("src"); var widthS = tag.GetAttr("width"); var heightS = tag.GetAttr("height"); int w, h; if (widthS == null || !int.TryParse(widthS, out w)) { w = -1; } if (heightS == null || !int.TryParse(heightS, out h)) { h = -1; } var img = HtEngine.Device.LoadImage(src); if (w < 0) { w = img.Width; } if (h < 0) { h = img.Height; } var dChunk = OP <DeviceChunkDrawImage> .Acquire(); if (currLine == null) { currLine = this.NewLine(null, viewportWidth, align, valign); } dChunk.Image = img; dChunk.Rect.Width = w; dChunk.Rect.Height = h; dChunk.Font = font; // for whitespace measure //HtEngine.Log(HtLogLevel.Debug, "Adding image w={0} h={1}",dChunk.Width,dChunk.Height); if (currentLink != null) { this.Links.Add(new KeyValuePair <DeviceChunk, string>(dChunk, currentLink)); } if (!currLine.AddChunk(dChunk)) { currLine.IsFull = true; currLine = this.NewLine(currLine, viewportWidth, align, valign); if (!currLine.AddChunk(dChunk)) { HtEngine.Log(HtLogLevel.Error, "Could not fit image into line. Image is too big: {0}", dChunk); dChunk.Dispose(); } } } break; case "p": if (tag.IsClosing) { // ignore closing tags } else { currLine = this.NewLine(currLine, viewportWidth, align, valign); ExctractAligns(tag, ref align, ref valign); } break; default: HtEngine.Log(HtLogLevel.Error, "Unsupported html tag {0}", tag); break; } } } // align last line this.FinishLine(currLine, align, valign); }
public abstract void Draw(string id, HtRect rect, HtColor color, Core.HtmlLink link, object userData);
/// <summary> /// Draw method /// </summary> /// <param name="rect">Where to draw</param> /// <param name="color">Color to use (ignored for now)</param> /// <param name="linkText">Link text</param> /// <param name="userData">User data</param> public override void Draw(string id, HtRect rect, HtColor color, Core.HtmlLink link, object userData) { if (isTime) { var now = DateTime.Now; timeFont.Draw( null, //no span info "time", rect, color, string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", now.Hour, now.Minute, now.Second, now.Millisecond), false, Core.DrawTextEffect.None, HtColor.white, 0, link, userData); } else if (uiAtlas != null) { var root = userData as Transform; if (root != null) { var go = new GameObject(string.IsNullOrEmpty(id) ? "image" : id, typeof(UISprite)); go.layer = root.gameObject.layer; go.transform.parent = root; go.transform.localPosition = new Vector3(rect.X + rect.Width / 2, -rect.Y - rect.Height / 2, -1f); go.transform.localScale = Vector3.one; var spr = go.GetComponent<UISprite>(); spr.pivot = UIWidget.Pivot.Center; spr.atlas = uiAtlas; spr.color = new Color32(color.R, color.G, color.B, color.A); spr.type = UISprite.Type.Simple; spr.width = rect.Width != 0 ? rect.Width : 1; spr.height = rect.Height != 0 ? rect.Height : 1; spr.depth = NGUIHTML.currentDepth; if (isAnim) { var sprAnim = go.AddComponent<UISpriteAnimation>(); sprAnim.framesPerSecond = FPS; sprAnim.namePrefix = spriteName; sprAnim.pixelSnap = PixelSnap; sprAnim.loop = true; sprAnim.Reset(); } else { spr.spriteName = spriteName; spr.MakePixelPerfect(); } if (link != null && !string.IsNullOrEmpty(link.linkText)) { var collider = go.AddComponent<BoxCollider>(); collider.isTrigger = true; collider.center = new Vector3(0f, 0f, -0.25f); collider.size = new Vector3(1f, 1f, 1f); spr.autoResizeBoxCollider = true; spr.ResizeCollider(); var nguiLinkText = go.AddComponent<NGUILinkText>(); string[] split = link.linkText.Split('#'); if (split.Length >= 1) { nguiLinkText.linkText = split[0]; if (split.Length >= 2) { var componet = go.AddComponent(split[1]); } if (split.Length >= 3) { var componet = go.GetComponent<NGUIHtmlComponent>(); if (componet != null) { componet.param = split[2]; } } } var uiButtonColor = go.AddComponent<UIButtonColor>(); uiButtonColor.tweenTarget = go; uiButtonColor.hover = new Color32( HtEngine.LinkHoverColor.R, HtEngine.LinkHoverColor.G, HtEngine.LinkHoverColor.B, HtEngine.LinkHoverColor.A); uiButtonColor.pressed = new Color( spr.color.r * HtEngine.LinkPressedFactor, spr.color.g * HtEngine.LinkPressedFactor, spr.color.b * HtEngine.LinkPressedFactor, spr.color.a); uiButtonColor.duration = 0f; var uiButtonMessage = go.AddComponent<UIButtonMessage>(); uiButtonMessage.target = root.gameObject; uiButtonMessage.functionName = HtEngine.LinkFunctionName; } } else { HtEngine.Log(HtLogLevel.Error, "Can't draw without root."); } } }
public void Parse(IEnumerator<HtmlChunk> source, int width, string id = null, HtFont font = null, HtColor color = default(HtColor), TextAlign align = TextAlign.Left, VertAlign valign = VertAlign.Bottom) { compiled.Compile(source, width, id, font, color, align, valign); offsetApplied = false; }
public abstract void Draw(string id, HtRect rect, HtColor color, string linkText, object userData);
public override void Draw(HtRect rect, HtColor color, string text) { Debug.Assert(HtmlDevice.Context != null); var c = Color.FromArgb(color.A, color.R, color.G, color.B); HtmlDevice.Context.DrawString(text, this.font, new SolidBrush(c), rect.X, rect.Y,StringFormat.GenericTypographic); }
public void Parse(IEnumerator <HtmlChunk> htmlChunks, int viewportWidth, string id = null, HtFont font = null, HtColor color = default(HtColor), TextAlign align = TextAlign.Left, VertAlign valign = VertAlign.Bottom) { this.Clear(); var defaultFont = HtEngine.Device.LoadFont(HtEngine.DefaultFontFace, HtEngine.DefaultFontSize, false, false); font = font == null ? defaultFont : font; color = (color.R == 0 && color.G == 0 && color.B == 0 && color.A == 0) ? HtEngine.DefaultColor : color; //string id = null; //var align = TextAlign.Left; //var valign = VertAlign.Bottom; DrawTextDeco deco = DrawTextDeco.None; DrawTextEffect effect = DrawTextEffect.None; HtColor effectColor = HtEngine.DefaultColor; int effectAmount = 1; string currentLink = null; bool prevIsWord = false; DeviceChunkLine currLine = null; DeviceChunkDrawText lastTextChunk = null; //for (int i = 0; i < htmlChunks.Count; i++) while (htmlChunks.MoveNext()) { HtmlChunk htmlChunk = htmlChunks.Current; var word = htmlChunk as HtmlChunkWord; if (word != null) { if (currLine == null) { currLine = this.NewLine(null, viewportWidth, align, valign); } if (effect == DrawTextEffect.None) { lastTextChunk = AcquireDeviceChunkDrawText( id, word.Text, font, color, deco, lastTextChunk != null && lastTextChunk.Deco != deco, prevIsWord); } else { lastTextChunk = AcquireDeviceChunkDrawTextEffect( id, word.Text, font, color, deco, lastTextChunk != null && lastTextChunk.Deco != deco, effect, effectAmount, effectColor, prevIsWord); } if (currentLink != null && !this.Links.ContainsKey(lastTextChunk)) { this.Links.Add(lastTextChunk, currentLink); } if (!currLine.AddChunk(lastTextChunk, prevIsWord)) { prevIsWord = true; //currLine.IsFull = true; string lastText = lastTextChunk.Text; lastTextChunk.Dispose(); lastTextChunk = null; bool decoStop = lastTextChunk != null && lastTextChunk.Deco != deco; // find prefix ascii string. //string prefixAsciiText = null; int pos = 0; //for (pos = 0; pos < lastText.Length; ++pos) { // char ch = lastText[pos]; // if (ch > 255 || ch == ' ') { // prefixAsciiText = lastText.Substring(0, pos); // break; // } //} //if (prefixAsciiText != null) { // DeviceChunkDrawText prefixAsciiTextChunk; // if (effect == DrawTextEffect.None) { // prefixAsciiTextChunk = AcquireDeviceChunkDrawText( // id, // prefixAsciiText, // font, // color, // deco, // decoStop, // prevIsWord); // } else { // prefixAsciiTextChunk = AcquireDeviceChunkDrawTextEffect( // id, // prefixAsciiText, // font, // color, // deco, // decoStop, // effect, // effectAmount, // effectColor, // prevIsWord); // } // if (currLine.AddChunk(prefixAsciiTextChunk, prevIsWord)) { // if (currentLink != null && !this.Links.ContainsKey(prefixAsciiTextChunk)) // this.Links.Add(prefixAsciiTextChunk, currentLink); // lastText = lastText.Substring(pos); // decoStop = false; // } //} //// reset the pos. //pos = 0; // add multi-lines. int remainingWidth = viewportWidth; for (; pos < lastText.Length;) { char ch = lastText[pos]; remainingWidth -= font.Measure(ch.ToString()).Width; if (remainingWidth < 0) { string tmpText = lastText.Substring(0, pos); DeviceChunkDrawText tmpTextChunk; if (effect == DrawTextEffect.None) { tmpTextChunk = AcquireDeviceChunkDrawText( id, tmpText, font, color, deco, decoStop, prevIsWord); } else { tmpTextChunk = AcquireDeviceChunkDrawTextEffect( id, tmpText, font, color, deco, decoStop, effect, effectAmount, effectColor, prevIsWord); } currLine = this.NewLine(currLine, viewportWidth, align, valign); currLine.AddChunk(tmpTextChunk, prevIsWord); if (currentLink != null && !this.Links.ContainsKey(tmpTextChunk)) { this.Links.Add(tmpTextChunk, currentLink); } lastText = lastText.Substring(pos); pos = 0; remainingWidth = viewportWidth; } else { ++pos; } } // add last line. if (!string.IsNullOrEmpty(lastText)) { if (effect == DrawTextEffect.None) { lastTextChunk = AcquireDeviceChunkDrawText( id, lastText, font, color, deco, decoStop, prevIsWord); } else { lastTextChunk = AcquireDeviceChunkDrawTextEffect( id, lastText, font, color, deco, decoStop, effect, effectAmount, effectColor, prevIsWord); } currLine = this.NewLine(currLine, viewportWidth, align, valign); currLine.AddChunk(lastTextChunk, prevIsWord); if (currentLink != null && !this.Links.ContainsKey(lastTextChunk)) { this.Links.Add(lastTextChunk, currentLink); } } } prevIsWord = true; } else { prevIsWord = false; } var tag = htmlChunk as HtmlChunkTag; if (tag != null) { switch (tag.Tag) { case "spin": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { id = null; this.FinishLine(currLine, align, valign); return; // return control to parent } else { id = tag.GetAttr("id"); ExctractAligns(tag, ref align, ref valign); var compiled = OP <DeviceChunkDrawCompiled> .Acquire(); compiled.Font = font; var scompiledWidth = tag.GetAttr("width") ?? "0"; var compiledWidth = 0; if (!int.TryParse(scompiledWidth, out compiledWidth)) { compiledWidth = 0; } if (compiledWidth == 0) { compiledWidth = currLine == null ? viewportWidth : currLine.AvailWidth - font.WhiteSize; } if (compiledWidth > 0) { if (compiledWidth > viewportWidth) { compiledWidth = viewportWidth; } compiled.Parse(htmlChunks, compiledWidth, id, font, color, align, valign); compiled.MeasureSize(); if (currLine == null) { currLine = this.NewLine(null, viewportWidth, align, valign); } if (!currLine.AddChunk(compiled, prevIsWord)) { currLine.IsFull = true; currLine = this.NewLine(currLine, viewportWidth, align, valign); if (!currLine.AddChunk(compiled, prevIsWord)) { HtEngine.Log(HtLogLevel.Error, "Could not fit spin into line. Word is too big: {0}", lastTextChunk); compiled.Dispose(); compiled = null; } } } else { HtEngine.Log(HtLogLevel.Warning, "spin width is not given"); } } break; case "effect": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { effect = DrawTextEffect.None; } else { var name = tag.GetAttr("name") ?? "outline"; switch (name) { case "shadow": effect = DrawTextEffect.Shadow; effectAmount = 1; effectColor = HtColor.RGBA(0, 0, 0, 80); break; case "outline": effect = DrawTextEffect.Outline; effectAmount = 1; effectColor = HtColor.RGBA(0xFF, 0xFF, 0xFF, 80); break; } var amount = tag.GetAttr("amount"); if (amount != null) { if (!int.TryParse(amount, out effectAmount)) { HtEngine.Log(HtLogLevel.Error, "Invalid numeric value: " + amount); } } var colors = tag.GetAttr("color"); if (colors != null) { effectColor = HtColor.Parse(colors); } } break; case "u": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { deco &= ~DrawTextDeco.Underline; } else { deco |= DrawTextDeco.Underline; } break; case "s": case "strike": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { deco &= ~DrawTextDeco.Strike; } else { deco |= DrawTextDeco.Strike; } break; case "code": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { font = this.fontStack.Count > 0 ? this.fontStack.Pop() : defaultFont; } else { this.fontStack.Push(font); const string fontName = "code"; int fontSize = font.Size; bool fontBold = font.Bold; bool fontItal = font.Italic; font = HtEngine.Device.LoadFont(fontName, fontSize, fontBold, fontItal); } break; case "b": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { font = this.fontStack.Count > 0 ? this.fontStack.Pop() : defaultFont; } else { this.fontStack.Push(font); string fontName = font.Face; int fontSize = font.Size; const bool fontBold = true; bool fontItal = font.Italic; font = HtEngine.Device.LoadFont(fontName, fontSize, fontBold, fontItal); } break; case "i": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { font = this.fontStack.Count > 0 ? this.fontStack.Pop() : defaultFont; } else { this.fontStack.Push(font); string fontName = font.Face; int fontSize = font.Size; bool fontBold = font.Bold; const bool fontItal = true; font = HtEngine.Device.LoadFont(fontName, fontSize, fontBold, fontItal); } break; case "a": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { id = null; if (this.colorStack.Count > 0) { color = this.colorStack.Pop(); } currentLink = null; } else { id = tag.GetAttr("id"); currentLink = tag.GetAttr("href"); this.colorStack.Push(color); color = HtEngine.DefaultLinkColor; } break; case "font": if (tag.IsSingle) { // do nothing } else if (tag.IsClosing) { font = this.fontStack.Count > 0 ? this.fontStack.Pop() : defaultFont; color = this.colorStack.Count > 0 ? this.colorStack.Pop() : HtEngine.DefaultColor; } else { this.fontStack.Push(font); this.colorStack.Push(color); string fontName = tag.GetAttr("face") ?? font.Face; string fontSizeS = tag.GetAttr("size"); int fontSize; if (fontSizeS == null || !int.TryParse(fontSizeS, out fontSize)) { fontSize = font.Size; } bool fontBold = font.Bold; bool fontItal = font.Italic; font = HtEngine.Device.LoadFont(fontName, fontSize, fontBold, fontItal); color = HtColor.Parse(tag.GetAttr("color"), color); } break; case "br": currLine = this.NewLine(currLine, viewportWidth, align, valign); currLine.Height = font.LineSpacing; break; case "img": if (tag.IsClosing) { // ignore closing tags } else { var src = tag.GetAttr("src"); var widthS = tag.GetAttr("width"); var heightS = tag.GetAttr("height"); var fpsS = tag.GetAttr("fps"); var imgId = tag.GetAttr("id"); int w, h, fps; if (widthS == null || !int.TryParse(widthS, out w)) { w = -1; } if (heightS == null || !int.TryParse(heightS, out h)) { h = -1; } if (fpsS == null || !int.TryParse(fpsS, out fps)) { fps = -1; } var img = HtEngine.Device.LoadImage(src, fps); if (w < 0) { w = img.Width; } if (h < 0) { h = img.Height; } var dChunk = OP <DeviceChunkDrawImage> .Acquire(); if (currLine == null) { currLine = this.NewLine(null, viewportWidth, align, valign); } dChunk.Image = img; dChunk.Rect.Width = w; dChunk.Rect.Height = h; dChunk.Font = font; // for whitespace measure dChunk.Id = imgId; //HtEngine.Log(HtLogLevel.Debug, "Adding image w={0} h={1}",dChunk.Width,dChunk.Height); if (currentLink != null && !this.Links.ContainsKey(dChunk)) { this.Links.Add(dChunk, currentLink); } if (!currLine.AddChunk(dChunk, prevIsWord)) { currLine.IsFull = true; currLine = this.NewLine(currLine, viewportWidth, align, valign); if (!currLine.AddChunk(dChunk, prevIsWord)) { HtEngine.Log(HtLogLevel.Error, "Could not fit image into line. Image is too big: {0}", dChunk); dChunk.Dispose(); } } } break; case "p": if (tag.IsClosing) { id = null; } else { id = tag.GetAttr("id"); currLine = this.NewLine(currLine, viewportWidth, align, valign); ExctractAligns(tag, ref align, ref valign); } break; default: HtEngine.Log(HtLogLevel.Error, "Unsupported html tag {0}", tag); break; } } } // align last line this.FinishLine(currLine, align, valign); }
/* * internal void onLinkClicked(GameObject senderGo) * { * var nguiLinkText = senderGo.GetComponent<NGUILinkText>(); * if (nguiLinkText != null) * { * //Debug.Log("xxx " + nguiLinkText.linkText); * } * }*/ void IUpdate() { if (changed && compiler != null) { // link hover color. HtEngine.LinkHoverColor = HtColor.RGBA( (byte)(LinkHoverColor.r * 255), (byte)(LinkHoverColor.g * 255), (byte)(LinkHoverColor.b * 255), (byte)(LinkHoverColor.a * 255) ); // link pressed factor. HtEngine.LinkPressedFactor = LinkPressedFactor; //设置字体列表 NGUIFont.FontList = fonts; NGUIImage.imgs = imgs; //设置填充图集 NGUIDevice.Fillatlas = Fillatlas; UIWidget cmWidget = gameObject.GetComponent <UIWidget>(); // we have new html text, so compile it compiler.Compile(html, cmWidget != null ? cmWidget.width : Screen.width); //根据本控件的对齐方式,进行偏移修正 { switch (cmWidget.pivot) { case UIWidget.Pivot.Center: compiler.Offset(-cmWidget.width / 2, -cmWidget.height / 2); break; case UIWidget.Pivot.Top: compiler.Offset(-cmWidget.width / 2, 0); break; case UIWidget.Pivot.Left: compiler.Offset(0, -cmWidget.height / 2); break; case UIWidget.Pivot.TopRight: compiler.Offset(-cmWidget.width, 0); break; case UIWidget.Pivot.Right: compiler.Offset(-cmWidget.width, -cmWidget.height / 2); break; case UIWidget.Pivot.BottomLeft: compiler.Offset(0, -cmWidget.height); break; case UIWidget.Pivot.Bottom: compiler.Offset(-cmWidget.width / 2, -cmWidget.height); break; case UIWidget.Pivot.BottomRight: compiler.Offset(-cmWidget.width, -cmWidget.height); break; } } // destroy old widgets. foreach (Transform childTr in transform) { Destroy(childTr.gameObject); } // generate the widgets. compiler.Draw(Time.deltaTime, transform); // release changed flag changed = false; if (autoScroll != AutoScrollType.MANUAL) { StartCoroutine(updateAutoScroll()); } } }
public void Parse(IEnumerator <HtmlChunk> source, int width, string id = null, HtFont font = null, HtColor color = default(HtColor), TextAlign align = TextAlign.Left, VertAlign valign = VertAlign.Bottom) { compiled.Compile(source, width, id, font, color, align, valign); offsetApplied = false; }
/// <summary> /// Draw method /// </summary> /// <param name="rect">Where to draw</param> /// <param name="color">Color to use (ignored for now)</param> /// <param name="linkText">Link text</param> /// <param name="userData">User data</param> public override void Draw(string id, HtRect rect, HtColor color, string linkText, object userData) { if (isTime) { var now = DateTime.Now; timeFont.Draw( "time", rect, color, string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", now.Hour, now.Minute, now.Second, now.Millisecond), false, Core.DrawTextEffect.None, HtColor.white, 0, linkText, userData); } else if (uiAtlas != null) { var root = userData as Transform; if (root != null) { var go = new GameObject(string.IsNullOrEmpty(id) ? "image" : id, typeof(UISprite)); go.layer = root.gameObject.layer; go.transform.parent = root; go.transform.localPosition = new Vector3(rect.X + rect.Width / 2, -rect.Y - rect.Height / 2, -1f); go.transform.localScale = Vector3.one; var spr = go.GetComponent <UISprite>(); spr.pivot = UIWidget.Pivot.Center; spr.atlas = uiAtlas; spr.color = new Color32(color.R, color.G, color.B, color.A); spr.type = UISprite.Type.Simple; spr.width = rect.Width != 0 ? rect.Width : 1; spr.height = rect.Height != 0 ? rect.Height : 1; spr.depth = NGUIHTML.currentDepth; if (isAnim) { var sprAnim = go.AddComponent <UISpriteAnimation>(); sprAnim.framesPerSecond = FPS; sprAnim.namePrefix = spriteName; sprAnim.loop = true; sprAnim.Reset(); } else { spr.spriteName = spriteName; spr.MakePixelPerfect(); } if (!string.IsNullOrEmpty(linkText)) { var collider = go.AddComponent <BoxCollider>(); collider.isTrigger = true; collider.center = new Vector3(0f, 0f, -0.25f); collider.size = new Vector3(1f, 1f, 1f); var nguiLinkText = go.AddComponent <NGUILinkText>(); nguiLinkText.linkText = linkText; var uiButtonColor = go.AddComponent <UIButtonColor>(); uiButtonColor.tweenTarget = go; uiButtonColor.hover = new Color32( HtEngine.LinkHoverColor.R, HtEngine.LinkHoverColor.G, HtEngine.LinkHoverColor.B, HtEngine.LinkHoverColor.A); uiButtonColor.pressed = new Color( spr.color.r * HtEngine.LinkPressedFactor, spr.color.g * HtEngine.LinkPressedFactor, spr.color.b * HtEngine.LinkPressedFactor, spr.color.a); uiButtonColor.duration = 0f; var uiButtonMessage = go.AddComponent <UIButtonMessage>(); uiButtonMessage.target = root.gameObject; uiButtonMessage.functionName = HtEngine.LinkFunctionName; } } else { HtEngine.Log(HtLogLevel.Error, "Can't draw without root."); } } }
public abstract void FillRect(HtRect rect, HtColor color);
public abstract void Draw(HtRect rect, HtColor color);
/// <summary> /// Draw method. /// </summary> /// <param name="rect">Where to draw</param> /// <param name="color">Text color</param> /// <param name="text">Text</param> /// <param name="isEffect">Is effect</param> /// <param name="effect">Effect</param> /// <param name="effectColor">Effect color</param> /// <param name="effectAmount">Effect amount</param> /// <param name="linkText">Link text</param> /// <param name="userData">User data</param> public override void Draw(HtSpan span, string id, HtRect rect, HtColor color, string text, bool isEffect, Core.DrawTextEffect effect, HtColor effectColor, int effectAmount, Core.HtmlLink link, object userData) { // NGUI do not need to draw effect. if (isEffect) return; var root = userData as Transform; if (root != null) { var go = new GameObject(string.IsNullOrEmpty(id) ? "label" : id, typeof(UILabel)); go.layer = root.gameObject.layer; go.transform.parent = root; go.transform.localPosition = new Vector3(rect.X + rect.Width / 2, -rect.Y - rect.Height / 2, 0f); go.transform.localScale = Vector3.one; var lab = go.GetComponent<UILabel>(); lab.enabled = false; lab.pivot = UIWidget.Pivot.Center; //lab.supportEncoding = false; //L.DF lab.fontSize = style.fontSize; lab.fontStyle = style.fontStyle; lab.color = new Color32(color.R, color.G, color.B, color.A); lab.spacingX = SpacingX; lab.spacingY = SpacingY; switch (effect) { case Core.DrawTextEffect.Outline: lab.effectStyle = UILabel.Effect.Outline; break; case Core.DrawTextEffect.Shadow: lab.effectStyle = UILabel.Effect.Shadow; break; } lab.effectColor = new Color32(effectColor.R, effectColor.G, effectColor.B, effectColor.A); lab.effectDistance = new Vector2(effectAmount, effectAmount); if (NGUIHTML.currentLabelOverflow == UILabel.Overflow.ShrinkContent && NGUIHTML.currentLabelCrispness != UILabel.Crispness.Never) { lab.width = rect.Width + Mathf.Max(1, (int)(rect.Width * 0.05f)); lab.height = rect.Height + Mathf.Max(1, (int)(rect.Height * 0.05f)); } else { lab.width = 0;//rect.Width; lab.height = 0;//rect.Height; } lab.depth = NGUIHTML.currentDepth; lab.overflowMethod = NGUIHTML.currentLabelOverflow; lab.keepCrispWhenShrunk = NGUIHTML.currentLabelCrispness; lab.trueTypeFont = style.font; lab.enabled = true; if (lab.supportEncoding) lab.text = text.Replace("\\", "\\\\"); else lab.text = text; lab.MakePixelPerfect(); // build link. if (link != null && !string.IsNullOrEmpty(link.linkText)) { var collider = go.AddComponent<BoxCollider>(); collider.isTrigger = true; collider.center = new Vector3(0f, 0f, -0.25f); collider.size = new Vector3(lab.width, lab.height, 1f); // var nguiLinkText = go.AddComponent<NGUILinkText>(); // nguiLinkText.linkText = linkText; var nguiLinkText = go.AddComponent<NGUILinkText>(); nguiLinkText.span = span; string[] split = link.linkText.Split('#'); if (split.Length >= 1) { nguiLinkText.linkText = split[0]; if (split.Length >= 2) { var componet = go.AddComponent(split[1]); } if (split.Length >= 3) { var componet = go.GetComponent<NGUIHtmlComponent>(); if (componet != null) { componet.param = split[2]; } } } var uiButtonColor = lab.gameObject.GetComponent<UIButtonColorExt>(); if (uiButtonColor == null) { uiButtonColor = lab.gameObject.AddComponent<UIButtonColorExt>(); } link.labelList.Add(lab.gameObject); uiButtonColor.tweenTargets = link.labelList; uiButtonColor.hover = new Color32( HtEngine.LinkHoverColor.R, HtEngine.LinkHoverColor.G, HtEngine.LinkHoverColor.B, HtEngine.LinkHoverColor.A); uiButtonColor.pressed = new Color( lab.color.r * HtEngine.LinkPressedFactor, lab.color.g * HtEngine.LinkPressedFactor, lab.color.b * HtEngine.LinkPressedFactor, lab.color.a); uiButtonColor.duration = 0f; var uiButtonMessage = go.AddComponent<UIButtonMessage>(); uiButtonMessage.target = root.gameObject; uiButtonMessage.functionName = HtEngine.LinkFunctionName; } } else { HtEngine.Log(HtLogLevel.Error, "Can't draw without root."); } }
public abstract void Draw(string id, HtRect rect, HtColor color, string text, bool isEffect, Core.DrawTextEffect effect, HtColor effectColor, int effectAmount, string linkText, object userData);