123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- using CommonLang;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace CommonUI.Display.Text
- {
-
- public interface TextDrawable : BaseRichTextLayer.Drawable, ICloneable
- {
- bool Equals(TextDrawable obj);
- bool FromText(string text);
- }
-
- public abstract class ITextDrawableFactory
- {
- static ITextDrawableFactory() { new DefaultTextDrawableFactory(); }
- public static ITextDrawableFactory Instance { get; private set; }
- public ITextDrawableFactory()
- {
- Instance = this;
- }
- public abstract TextDrawable CreateTextDrawable(string name, string value);
- private class DefaultTextDrawableFactory : ITextDrawableFactory
- {
- public override TextDrawable CreateTextDrawable(string name, string value)
- {
- return null;
- }
- }
- }
-
- public class TextDrawableFactory : ITextDrawableFactory
- {
- public const string TYPE_IMAGE = "image";
- public const string TYPE_ATLAS = "atlas";
- public const string TYPE_SPRITE = "sprite";
- public string ResourceRoot { get; set; }
- protected HashMap<string, Image> mImageMap = new HashMap<string, Image>();
- protected HashMap<string, Cell.CPJResource> mResMap = new HashMap<string, Cell.CPJResource>();
- protected HashMap<string, Image> mSelfImageMap = new HashMap<string, Image>();
- protected HashMap<string, Cell.CPJResource> mSelfResMap = new HashMap<string, Cell.CPJResource>();
- private static TextDrawableFactory mInstance;
- public TextDrawableFactory()
- {
- ResourceRoot = "";
- mInstance = this;
- }
- public override TextDrawable CreateTextDrawable(string name, string value)
- {
- switch (name)
- {
- case TYPE_IMAGE:
- {
- ImageDraw ret = new ImageDraw();
- if (ret.FromText(value)) return ret;
- }
- break;
- case TYPE_ATLAS:
- {
- AtlasDraw ret = new AtlasDraw();
- if (ret.FromText(value)) return ret;
- }
- break;
- case TYPE_SPRITE:
- {
- SpriteDraw ret = new SpriteDraw();
- if (ret.FromText(value)) return ret;
- }
- break;
- }
- return null;
- }
- public virtual Image LoadImage(string file)
- {
- Image img = mImageMap.Get(file);
- if (img == null)
- {
- img = Driver.Instance.createImage(ResourceRoot + file);
- if (img != null)
- {
- mSelfImageMap.Put(file, img);
- mImageMap.Put(file, img);
- }
- }
- return img;
- }
- public virtual Cell.CPJResource LoadResource(string file)
- {
- Cell.CPJResource res = mResMap.Get(file);
- if (res == null)
- {
- res = Cell.CPJResource.CreateResource(file);
- if (res != null)
- {
- mSelfResMap.Put(file, res);
- mResMap.Put(file, res);
- }
- }
- return res;
- }
- public virtual void CacheImage(string file, Image img)
- {
- if (img != null)
- {
- mImageMap.Put(file, img);
- }
- }
- public virtual void CacheResource(string file, Cell.CPJResource res)
- {
- if (res != null)
- {
- mResMap.Put(file, res);
- }
- }
- public void Dispose()
- {
- mImageMap.Clear();
- mResMap.Clear();
- foreach (KeyValuePair<string, Image> kvp in mSelfImageMap)
- {
- kvp.Value.Dispose();
- }
- mSelfImageMap.Clear();
- foreach (KeyValuePair<string, Cell.CPJResource> kvp in mSelfResMap)
- {
- kvp.Value.Dispose();
- }
- mSelfResMap.Clear();
- }
-
-
-
-
- public class ImageDraw : TextDrawable
- {
- public Image ImageSrc { get; private set; }
- public float CharWidth { get { return zoom_w; } }
- public float CharHeight { get { return zoom_h; } }
- private float zoom_w = 0;
- private float zoom_h = 0;
- public bool FromText(string text)
- {
- string[] kvs = text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
- if (kvs.Length > 0)
- {
- ImageSrc = mInstance.LoadImage(kvs[0]);
- if (ImageSrc != null)
- {
- zoom_w = ImageSrc.Width;
- zoom_h = ImageSrc.Height;
- if (kvs.Length >= 3)
- {
- float.TryParse(kvs[1], out zoom_w);
- float.TryParse(kvs[2], out zoom_h);
- }
- return true;
- }
- }
- return false;
- }
- public bool Equals(TextDrawable obj)
- {
- if (obj is ImageDraw)
- {
- var b = obj as ImageDraw;
- if (this.ImageSrc != b.ImageSrc) return false;
- if (this.zoom_w != b.zoom_w) return false;
- if (this.zoom_h != b.zoom_h) return false;
- return true;
- }
- return false;
- }
- public object Clone()
- {
- ImageDraw ret = new ImageDraw();
- ret.ImageSrc = this.ImageSrc;
- ret.zoom_w = this.zoom_w;
- ret.zoom_h = this.zoom_h;
- return ret;
- }
- public void Render(Graphics g, RichTextLayer.Region rg, float x, float y)
- {
- g.drawImageZoom(ImageSrc, x, y, zoom_w, zoom_h);
- }
- public void Hide(BaseRichTextLayer.Region self, float x, float y) { }
- public void Dispose()
- {
- }
- }
-
-
-
-
- public class AtlasDraw : TextDrawable
- {
- public Cell.CPJAtlas Atlas { get; private set; }
- public float CharWidth { get; private set; }
- public float CharHeight { get; private set; }
- private int tile_id = 0;
- public bool FromText(string text)
- {
- string[] kvs = text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
- if (kvs.Length >= 3)
- {
- Cell.CPJResource res = mInstance.LoadResource(kvs[0]);
- if (res != null)
- {
- this.Atlas = res.GetAtlas(kvs[1]);
- if (Atlas != null && int.TryParse(kvs[2], out tile_id))
- {
- CharWidth = Atlas.getWidth(tile_id);
- CharHeight = Atlas.getHeight(tile_id);
- return true;
- }
- }
- }
- return false;
- }
- public bool Equals(TextDrawable obj)
- {
- if (obj is AtlasDraw)
- {
- var b = obj as AtlasDraw;
- if (this.Atlas != b.Atlas)
- return false;
- if (this.tile_id != b.tile_id)
- return false;
- return true;
- }
- return false;
- }
- public object Clone()
- {
- AtlasDraw ret = new AtlasDraw();
- ret.Atlas = this.Atlas;
- ret.tile_id = this.tile_id;
- ret.CharWidth = this.CharWidth;
- ret.CharHeight = this.CharHeight;
- return ret;
- }
- public void Render(Graphics g, RichTextLayer.Region rg, float x, float y)
- {
- Atlas.render(g, tile_id, x, y, Trans.TRANS_NONE);
- }
- public void Hide(BaseRichTextLayer.Region self, float x, float y) { }
- public void Dispose()
- {
- }
- }
-
- public class SpriteDraw : TextDrawable
- {
- public CommonUI.Cell.Game.CSpriteMeta SpriteSrc { get; private set; }
- public float CharWidth { get { return bounds.Width; } }
- public float CharHeight { get { return bounds.Height; } }
- private int anim;
- private int frame_count;
- private int frame = 0;
- private CommonUI.Cell.Game.CCD bounds;
- public bool FromText(string text)
- {
- string[] kvs = text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
- if (kvs.Length >= 3)
- {
- Cell.CPJResource res = mInstance.LoadResource(kvs[0]);
- if (res != null)
- {
- this.SpriteSrc = res.GetSpriteMeta(kvs[1]);
- if (SpriteSrc != null && int.TryParse(kvs[2], out anim))
- {
- this.bounds = SpriteSrc.getVisibleBounds(anim);
- this.frame_count = SpriteSrc.getFrameCount(anim);
- return true;
- }
- }
- }
- return false;
- }
- public bool Equals(TextDrawable obj)
- {
- if (obj is SpriteDraw)
- {
- var b = obj as SpriteDraw;
- if (this.SpriteSrc != b.SpriteSrc)
- return false;
- if (this.anim != b.anim)
- return false;
- return true;
- }
- return false;
- }
- public object Clone()
- {
- SpriteDraw ret = new SpriteDraw();
- ret.SpriteSrc = this.SpriteSrc;
- ret.anim = this.anim;
- ret.frame_count = this.frame_count;
- ret.bounds = this.bounds;
- return ret;
- }
- public void Render(Graphics g, RichTextLayer.Region rg, float x, float y)
- {
- SpriteSrc.render(g, anim, frame, x - bounds.X1, y - bounds.Y1);
- frame++;
- frame = frame % frame_count;
- }
- public void Hide(BaseRichTextLayer.Region self, float x, float y) { }
- public void Dispose()
- {
- }
- }
-
- }
- }
|