Example #1
0
    static int NewObject(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 1 && TypeChecker.CheckTypes(L, 1, typeof(string)))
            {
                string           arg0 = ToLua.ToString(L, 1);
                FairyGUI.GObject o    = FairyGUI.UIObjectFactory.NewObject(arg0);
                ToLua.PushObject(L, o);
                return(1);
            }
            else if (count == 1 && TypeChecker.CheckTypes(L, 1, typeof(FairyGUI.PackageItem)))
            {
                FairyGUI.PackageItem arg0 = (FairyGUI.PackageItem)ToLua.ToObject(L, 1);
                FairyGUI.GObject     o    = FairyGUI.UIObjectFactory.NewObject(arg0);
                ToLua.PushObject(L, o);
                return(1);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: FairyGUI.UIObjectFactory.NewObject"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pi"></param>
        /// <returns></returns>
        public static GObject NewObject(PackageItem pi)
        {
            Stats.LatestObjectCreation++;

            switch (pi.type)
            {
                case PackageItemType.Image:
                    return new GImage();

                case PackageItemType.MovieClip:
                    return new GMovieClip();

                case PackageItemType.Component:
                    {
                        ConstructorInfo extentionConstructor;
                        if (packageItemExtensions.TryGetValue(pi.owner.id + pi.id, out extentionConstructor))
                        {
                            GComponent g = (GComponent)extentionConstructor.Invoke(null);
                            if (g == null)
                                throw new Exception("Unable to create instance of '" + extentionConstructor.Name + "'");

                            return g;
                        }

                        XML xml = pi.componentData;
                        string extention = xml.GetAttribute("extention");
                        if (extention != null)
                        {
                            switch (extention)
                            {
                                case "Button":
                                    return new GButton();

                                case "Label":
                                    return new GLabel();

                                case "ProgressBar":
                                    return new GProgressBar();

                                case "Slider":
                                    return new GSlider();

                                case "ScrollBar":
                                    return new GScrollBar();

                                case "ComboBox":
                                    return new GComboBox();

                                default:
                                    return new GComponent();
                            }
                        }
                        else
                            return new GComponent();
                    }
            }
            return null;
        }
 static public int get_id(IntPtr l)
 {
     try {
         FairyGUI.PackageItem self = (FairyGUI.PackageItem)checkSelf(l);
         pushValue(l, true);
         pushValue(l, self.id);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 static public int constructor(IntPtr l)
 {
     try {
         FairyGUI.PackageItem o;
         o = new FairyGUI.PackageItem();
         pushValue(l, true);
         pushValue(l, o);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Example #5
0
        public BitmapFont(PackageItem item)
        {
            this.packageItem = item;
            this.name = UIPackage.URL_PREFIX + packageItem.owner.id + packageItem.id;
            this.canTint = true;
            this.canLight = false;
            this.canOutline = true;
            this.hasChannel = false;
            this.shader = ShaderConfig.bmFontShader;

            _dict = new Dictionary<int, BMGlyph>();
            this.scale = 1;
        }
 static public int Load(IntPtr l)
 {
     try {
         FairyGUI.PackageItem self = (FairyGUI.PackageItem)checkSelf(l);
         var ret = self.Load();
         pushValue(l, true);
         pushValue(l, ret);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 static public int set_frames(IntPtr l)
 {
     try {
         FairyGUI.PackageItem       self = (FairyGUI.PackageItem)checkSelf(l);
         FairyGUI.MovieClip.Frame[] v;
         checkArray(l, 2, out v);
         self.frames = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 static public int set_bitmapFont(IntPtr l)
 {
     try {
         FairyGUI.PackageItem self = (FairyGUI.PackageItem)checkSelf(l);
         FairyGUI.BitmapFont  v;
         checkType(l, 2, out v);
         self.bitmapFont = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 static public int set_audioClip(IntPtr l)
 {
     try {
         FairyGUI.PackageItem  self = (FairyGUI.PackageItem)checkSelf(l);
         UnityEngine.AudioClip v;
         checkType(l, 2, out v);
         self.audioClip = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 static public int set_repeatDelay(IntPtr l)
 {
     try {
         FairyGUI.PackageItem self = (FairyGUI.PackageItem)checkSelf(l);
         System.Single        v;
         checkType(l, 2, out v);
         self.repeatDelay = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 static public int set_swing(IntPtr l)
 {
     try {
         FairyGUI.PackageItem self = (FairyGUI.PackageItem)checkSelf(l);
         System.Boolean       v;
         checkType(l, 2, out v);
         self.swing = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 static public int set_binary(IntPtr l)
 {
     try {
         FairyGUI.PackageItem self = (FairyGUI.PackageItem)checkSelf(l);
         System.Byte[]        v;
         checkArray(l, 2, out v);
         self.binary = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 static public int set_type(IntPtr l)
 {
     try {
         FairyGUI.PackageItem     self = (FairyGUI.PackageItem)checkSelf(l);
         FairyGUI.PackageItemType v;
         checkEnum(l, 2, out v);
         self.type = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 static public int set_scale9Grid(IntPtr l)
 {
     try {
         FairyGUI.PackageItem self = (FairyGUI.PackageItem)checkSelf(l);
         Rect v;
         checkValueType(l, 2, out v);
         self.scale9Grid = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 static public int set_componentData(IntPtr l)
 {
     try {
         FairyGUI.PackageItem self = (FairyGUI.PackageItem)checkSelf(l);
         FairyGUI.Utils.XML   v;
         checkType(l, 2, out v);
         self.componentData = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Example #16
0
 static int GetItemByURL(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 1);
         string arg0            = ToLua.CheckString(L, 1);
         FairyGUI.PackageItem o = FairyGUI.UIPackage.GetItemByURL(arg0);
         ToLua.PushObject(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Example #17
0
 static int ConstructFromResource(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 2);
         FairyGUI.GImage      obj  = (FairyGUI.GImage)ToLua.CheckObject(L, 1, typeof(FairyGUI.GImage));
         FairyGUI.PackageItem arg0 = (FairyGUI.PackageItem)ToLua.CheckObject(L, 2, typeof(FairyGUI.PackageItem));
         obj.ConstructFromResource(arg0);
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Example #18
0
        public override void ConstructFromResource(PackageItem pkgItem)
        {
            _packageItem = pkgItem;
            sourceWidth = _packageItem.width;
            sourceHeight = _packageItem.height;
            initWidth = sourceWidth;
            initHeight = sourceHeight;
            _content.scale9Grid = _packageItem.scale9Grid;
            _content.scaleByTile = _packageItem.scaleByTile;

            _packageItem.Load();

            _content.texture = _packageItem.texture;

            SetSize(sourceWidth, sourceHeight);
        }
Example #19
0
 static int GetItemByName(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 2);
         FairyGUI.UIPackage   obj  = (FairyGUI.UIPackage)ToLua.CheckObject(L, 1, typeof(FairyGUI.UIPackage));
         string               arg0 = ToLua.CheckString(L, 2);
         FairyGUI.PackageItem o    = obj.GetItemByName(arg0);
         ToLua.PushObject(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Example #20
0
 static int SetItemAsset(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 4);
         FairyGUI.UIPackage   obj  = (FairyGUI.UIPackage)ToLua.CheckObject <FairyGUI.UIPackage>(L, 1);
         FairyGUI.PackageItem arg0 = (FairyGUI.PackageItem)ToLua.CheckObject <FairyGUI.PackageItem>(L, 2);
         object arg1 = ToLua.ToVarObject(L, 3);
         FairyGUI.DestroyMethod arg2 = (FairyGUI.DestroyMethod)ToLua.CheckObject(L, 4, typeof(FairyGUI.DestroyMethod));
         obj.SetItemAsset(arg0, arg1, arg2);
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Example #21
0
    static int GetItemAsset(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 2 && TypeChecker.CheckTypes <FairyGUI.UIPackage, FairyGUI.PackageItem>(L, 1))
            {
                FairyGUI.UIPackage   obj  = (FairyGUI.UIPackage)ToLua.ToObject(L, 1);
                FairyGUI.PackageItem arg0 = (FairyGUI.PackageItem)ToLua.ToObject(L, 2);
                object o = obj.GetItemAsset(arg0);
                ToLua.Push(L, o);
                return(1);
            }
            else if (count == 2 && TypeChecker.CheckTypes <FairyGUI.UIPackage, string>(L, 1))
            {
                FairyGUI.UIPackage obj  = (FairyGUI.UIPackage)ToLua.ToObject(L, 1);
                string             arg0 = ToLua.ToString(L, 2);
                object             o    = obj.GetItemAsset(arg0);
                ToLua.Push(L, o);
                return(1);
            }
            else if (count == 2 && TypeChecker.CheckTypes <string, string>(L, 1))
            {
                string arg0 = ToLua.ToString(L, 1);
                string arg1 = ToLua.ToString(L, 2);
                object o    = FairyGUI.UIPackage.GetItemAsset(arg0, arg1);
                ToLua.Push(L, o);
                return(1);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: FairyGUI.UIPackage.GetItemAsset"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
    static int NewObject(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 1 && TypeChecker.CheckTypes <FairyGUI.PackageItem>(L, 1))
            {
                FairyGUI.PackageItem arg0 = (FairyGUI.PackageItem)ToLua.ToObject(L, 1);
                FairyGUI.GObject     o    = FairyGUI.UIObjectFactory.NewObject(arg0);
                ToLua.PushObject(L, o);
                return(1);
            }
            else if (count == 1 && TypeChecker.CheckTypes <FairyGUI.ObjectType>(L, 1))
            {
                FairyGUI.ObjectType arg0 = (FairyGUI.ObjectType)ToLua.ToObject(L, 1);
                FairyGUI.GObject    o    = FairyGUI.UIObjectFactory.NewObject(arg0);
                ToLua.PushObject(L, o);
                return(1);
            }
            else if (count == 2)
            {
                FairyGUI.PackageItem arg0 = (FairyGUI.PackageItem)ToLua.CheckObject <FairyGUI.PackageItem>(L, 1);
                System.Type          arg1 = ToLua.CheckMonoType(L, 2);
                FairyGUI.GObject     o    = FairyGUI.UIObjectFactory.NewObject(arg0, arg1);
                ToLua.PushObject(L, o);
                return(1);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: FairyGUI.UIObjectFactory.NewObject"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
Example #23
0
        public static void TranslateComponent(PackageItem item)
        {
            if (TranslationHelper.strings == null)
            {
                return;
            }

            Dictionary <string, string> strings;

            if (!TranslationHelper.strings.TryGetValue(item.owner.id + item.id, out strings))
            {
                return;
            }

            string     elementId, value;
            ByteBuffer buffer = item.rawData;

            buffer.Seek(0, 2);

            int childCount = buffer.ReadShort();

            for (int i = 0; i < childCount; i++)
            {
                int dataLen = buffer.ReadShort();
                int curPos  = buffer.position;

                buffer.Seek(curPos, 0);

                ObjectType type = (ObjectType)buffer.ReadByte();
                buffer.Skip(4);
                elementId = buffer.ReadS();

                if (type == ObjectType.Component)
                {
                    if (buffer.Seek(curPos, 6))
                    {
                        type = (ObjectType)buffer.ReadByte();
                    }
                }

                buffer.Seek(curPos, 1);

                if (strings.TryGetValue(elementId + "-tips", out value))
                {
                    buffer.WriteS(value);
                }

                buffer.Seek(curPos, 2);

                int gearCnt = buffer.ReadShort();
                for (int j = 0; j < gearCnt; j++)
                {
                    int nextPos = buffer.ReadShort();
                    nextPos += buffer.position;

                    if (buffer.ReadByte() == 6)                //gearText
                    {
                        buffer.Skip(2);                        //controller
                        int valueCnt = buffer.ReadShort();
                        for (int k = 0; k < valueCnt; k++)
                        {
                            string page = buffer.ReadS();
                            if (page != null)
                            {
                                if (strings.TryGetValue(elementId + "-texts_" + k, out value))
                                {
                                    buffer.WriteS(value);
                                }
                                else
                                {
                                    buffer.Skip(2);
                                }
                            }
                        }

                        if (buffer.ReadBool() && strings.TryGetValue(elementId + "-texts_def", out value))
                        {
                            buffer.WriteS(value);
                        }
                    }

                    buffer.position = nextPos;
                }

                switch (type)
                {
                case ObjectType.Text:
                case ObjectType.RichText:
                case ObjectType.InputText:
                {
                    if (strings.TryGetValue(elementId, out value))
                    {
                        buffer.Seek(curPos, 6);
                        buffer.WriteS(value);
                    }
                    if (strings.TryGetValue(elementId + "-prompt", out value))
                    {
                        buffer.Seek(curPos, 4);
                        buffer.WriteS(value);
                    }
                    break;
                }

                case ObjectType.List:
                {
                    buffer.Seek(curPos, 8);
                    buffer.Skip(2);
                    int itemCount = buffer.ReadShort();
                    for (int j = 0; j < itemCount; j++)
                    {
                        int nextPos = buffer.ReadShort();
                        nextPos += buffer.position;

                        buffer.Skip(2);                                         //url
                        if (strings.TryGetValue(elementId + "-" + j, out value))
                        {
                            buffer.WriteS(value);
                        }
                        else
                        {
                            buffer.Skip(2);
                        }
                        if (strings.TryGetValue(elementId + "-" + j + "-0", out value))
                        {
                            buffer.WriteS(value);
                        }
                        buffer.position = nextPos;
                    }
                    break;
                }

                case ObjectType.Label:
                {
                    if (buffer.Seek(curPos, 6) && (ObjectType)buffer.ReadByte() == type)
                    {
                        if (strings.TryGetValue(elementId, out value))
                        {
                            buffer.WriteS(value);
                        }
                        else
                        {
                            buffer.Skip(2);
                        }

                        buffer.Skip(2);
                        if (buffer.ReadBool())
                        {
                            buffer.Skip(4);
                        }
                        buffer.Skip(4);
                        if (buffer.ReadBool() && strings.TryGetValue(elementId + "-prompt", out value))
                        {
                            buffer.WriteS(value);
                        }
                    }
                    break;
                }

                case ObjectType.Button:
                {
                    if (buffer.Seek(curPos, 6) && (ObjectType)buffer.ReadByte() == type)
                    {
                        if (strings.TryGetValue(elementId, out value))
                        {
                            buffer.WriteS(value);
                        }
                        else
                        {
                            buffer.Skip(2);
                        }
                        if (strings.TryGetValue(elementId + "-0", out value))
                        {
                            buffer.WriteS(value);
                        }
                    }
                    break;
                }

                case ObjectType.ComboBox:
                {
                    if (buffer.Seek(curPos, 6) && (ObjectType)buffer.ReadByte() == type)
                    {
                        int itemCount = buffer.ReadShort();
                        for (int j = 0; j < itemCount; j++)
                        {
                            int nextPos = buffer.ReadShort();
                            nextPos += buffer.position;

                            if (strings.TryGetValue(elementId + "-" + j, out value))
                            {
                                buffer.WriteS(value);
                            }

                            buffer.position = nextPos;
                        }

                        if (strings.TryGetValue(elementId, out value))
                        {
                            buffer.WriteS(value);
                        }
                    }

                    break;
                }
                }

                buffer.position = curPos + dataLen;
            }
        }
        void LoadAtlas(PackageItem item)
        {
            string ext      = Path.GetExtension(item.file);
            string fileName = item.file.Substring(0, item.file.Length - ext.Length);

            Texture       tex      = null;
            Texture       alphaTex = null;
            DestroyMethod dm;

            if (_fromBundle)
            {
                if (_resBundle != null)
                {
#if (UNITY_5 || UNITY_5_3_OR_NEWER)
                    tex = _resBundle.LoadAsset <Texture>(fileName);
#else
                    tex = (Texture2D)_resBundle.Load(fileName, typeof(Texture2D));
#endif
                }
                else
                {
                    Debug.LogWarning("FairyGUI: bundle already unloaded.");
                }

                dm = DestroyMethod.None;
            }
            else
            {
                tex = (Texture)_loadFunc(fileName, ext, typeof(Texture), out dm);
            }

            if (tex == null)
            {
                Debug.LogWarning("FairyGUI: texture '" + item.file + "' not found in " + this.name);
            }
            else if (!(tex is Texture2D))
            {
                Debug.LogWarning("FairyGUI: settings for '" + item.file + "' is wrong! Correct values are: (Texture Type=Default, Texture Shape=2D)");
                tex = null;
            }
            else
            {
                if (((Texture2D)tex).mipmapCount > 1)
                {
                    Debug.LogWarning("FairyGUI: settings for '" + item.file + "' is wrong! Correct values are: (Generate Mip Maps=unchecked)");
                }
            }

            if (tex != null)
            {
                fileName = fileName + "!a";
                if (_fromBundle)
                {
                    if (_resBundle != null)
                    {
#if (UNITY_5 || UNITY_5_3_OR_NEWER)
                        alphaTex = _resBundle.LoadAsset <Texture2D>(fileName);
#else
                        alphaTex = (Texture2D)_resBundle.Load(fileName, typeof(Texture2D));
#endif
                    }
                }
                else
                {
                    alphaTex = (Texture2D)_loadFunc(fileName, ext, typeof(Texture2D), out dm);
                }
            }


            if (tex == null)
            {
                tex = NTexture.CreateEmptyTexture();
                dm  = DestroyMethod.Destroy;
            }

            if (item.texture == null)
            {
                item.texture = new NTexture(tex, alphaTex, (float)tex.width / item.width, (float)tex.height / item.height);
                item.texture.destroyMethod = dm;
            }
            else
            {
                item.texture.Reload(tex, alphaTex);
                item.texture.destroyMethod = dm;
            }
        }
Example #25
0
        internal object GetItemAsset(PackageItem item)
        {
            switch (item.type)
            {
            case PackageItemType.Image:
                if (!item.decoded)
                {
                    item.decoded = true;
                    AtlasSprite sprite;
                    if (_sprites.TryGetValue(item.id, out sprite))
                    {
                        item.texture = CreateSpriteTexture(sprite);
                    }
                    else
                    {
                        item.texture = NTexture.Empty;
                    }
                }
                return(item.texture);

            case PackageItemType.Atlas:
                if (!item.decoded)
                {
                    item.decoded = true;
                    string fileName = string.IsNullOrEmpty(item.file) ? (item.id + ".png") : item.file;
                    string filePath = _assetNamePrefix + Path.GetFileNameWithoutExtension(fileName);
                    string ext      = Path.GetExtension(fileName);

                    Texture2D tex;
                    if (_resBundle != null)
                    {
#if UNITY_5
                        tex = _resBundle.LoadAsset <Texture2D>(filePath);
#else
                        tex = (Texture2D)_resBundle.Load(filePath, typeof(Texture2D));
#endif
                    }
                    else
                    {
                        tex = (Texture2D)_loadFunc(filePath, ext, typeof(Texture2D));
                    }
                    if (tex == null)
                    {
                        Debug.LogWarning("FairyGUI: texture '" + fileName + "' not found in " + this.name);
                        item.texture = NTexture.Empty;
                    }
                    else
                    {
                        if (tex.mipmapCount > 1)
                        {
                            Debug.LogWarning("FairyGUI: texture '" + fileName + "' in " + this.name + " is mipmaps enabled.");
                        }
                        item.texture             = new NTexture(tex, (float)tex.width / item.width, (float)tex.height / item.height);
                        item.texture.storedODisk = _resBundle == null;

                        filePath = filePath + "!a";
                        if (_resBundle != null)
                        {
#if UNITY_5
                            tex = _resBundle.LoadAsset <Texture2D>(filePath);
#else
                            tex = (Texture2D)_resBundle.Load(filePath, typeof(Texture2D));
#endif
                        }
                        else
                        {
                            tex = (Texture2D)_loadFunc(filePath, ext, typeof(Texture2D));
                        }
                        if (tex != null)
                        {
                            item.texture.alphaTexture             = new NTexture(tex);
                            item.texture.alphaTexture.storedODisk = _resBundle == null;
                        }
                    }
                }
                return(item.texture);

            case PackageItemType.Sound:
                if (!item.decoded)
                {
                    item.decoded = true;
                    string fileName = _assetNamePrefix + Path.GetFileNameWithoutExtension(item.file);
                    string ext      = Path.GetExtension(item.file);
                    if (_resBundle != null)
                    {
#if UNITY_5
                        item.audioClip = _resBundle.LoadAsset <AudioClip>(fileName);
#else
                        item.audioClip = (AudioClip)_resBundle.Load(fileName, typeof(AudioClip));
#endif
                    }
                    else
                    {
                        item.audioClip = (AudioClip)_loadFunc(fileName, ext, typeof(AudioClip));
                    }
                }
                return(item.audioClip);

            case PackageItemType.Font:
                if (!item.decoded)
                {
                    item.decoded = true;
                    LoadFont(item);
                }
                return(item.bitmapFont);

            case PackageItemType.MovieClip:
                if (!item.decoded)
                {
                    item.decoded = true;
                    LoadMovieClip(item);
                }
                return(item.frames);

            case PackageItemType.Component:
                if (!item.decoded)
                {
                    item.decoded = true;
                    string str = _descPack[item.id + ".xml"];
                    XML    xml = new XML(str);
                    if (_stringsSource != null)
                    {
                        Dictionary <string, string> strings;
                        if (_stringsSource.TryGetValue(this.id + item.id, out strings))
                        {
                            TranslateComponent(xml, strings);
                        }
                    }
                    item.componentData = xml;
                }
                return(item.componentData);

            default:
                if (!item.decoded)
                {
                    item.decoded = true;
                    item.binary  = LoadBinary(item.file);
                }
                return(item.binary);
            }
        }
Example #26
0
 void CreateObjectAsync(PackageItem item, CreateObjectCallback callback)
 {
     Timers.inst.StartCoroutine(_CreateObjectAsync(item, callback));
 }
Example #27
0
        /// <summary>
        /// 收集创建目标对象所需的所有类型信息
        /// </summary>
        /// <param name="item"></param>
        /// <param name="list"></param>
        void CollectComponentChildren(PackageItem item, List<DisplayListItem> list)
        {
            if (!item.decoded)
                LoadComponent(item);
            if (item.displayList == null)
            {
                LoadComponentChildren(item);
                TranslateComponent(item);
            }

                int cnt = item.displayList.Length;
            for (int i = 0; i < cnt; i++)
            {
                DisplayListItem di = item.displayList[i];
                if (di.packageItem != null && di.packageItem.type == PackageItemType.Component)
                    CollectComponentChildren(di.packageItem, list);
                else if (di.type == "list") //也要收集列表的item
                {
                    XMLList.Enumerator et = di.desc.GetEnumerator("item");
                    string defaultItem = null;
                    di.listItemCount = 0;
                    while (et.MoveNext())
                    {
                        XML ix = et.Current;
                        string url = ix.GetAttribute("url");
                        if (string.IsNullOrEmpty(url))
                        {
                            if (defaultItem == null)
                                defaultItem = di.desc.GetAttribute("defaultItem");
                            url = defaultItem;
                            if (string.IsNullOrEmpty(url))
                                continue;
                        }

                        PackageItem pi = UIPackage.GetItemByURL(url);
                        if (pi != null)
                        {
                            if (pi.type == PackageItemType.Component)
                                CollectComponentChildren(pi, list);

                            list.Add(new DisplayListItem(pi, null));
                            di.listItemCount++;
                        }
                    }
                }
                list.Add(di);
            }
        }
        public override void ConstructFromResource(PackageItem pkgItem)
        {
            _packageItem = pkgItem;

            sourceWidth = _packageItem.width;
            sourceHeight = _packageItem.height;
            initWidth = sourceWidth;
            initHeight = sourceHeight;

            _packageItem.Load();
            _content.interval = _packageItem.interval;
            _content.swing = _packageItem.swing;
            _content.repeatDelay = _packageItem.repeatDelay;
            _content.SetData(_packageItem.texture, _packageItem.frames, new Rect(0, 0, sourceWidth, sourceHeight));

            SetSize(sourceWidth, sourceHeight);
        }
Example #29
0
        public override void ConstructFromResource(PackageItem pkgItem)
        {
            _packageItem = pkgItem;

            sourceWidth = _packageItem.width;
            sourceHeight = _packageItem.height;
            initWidth = sourceWidth;
            initHeight = sourceHeight;

            _packageItem.Load();
            _content.interval = _packageItem.interval;
            _content.frames = _packageItem.frames;
            _content.boundsRect = new Rect(0, 0, sourceWidth, sourceHeight);

            SetSize(sourceWidth, sourceHeight);
        }
Example #30
0
        void LoadAtlas(PackageItem item)
        {
            string fileName = string.IsNullOrEmpty(item.file) ? (item.id + ".png") : item.file;
            string filePath = _assetNamePrefix + Path.GetFileNameWithoutExtension(fileName);
            string ext = Path.GetExtension(fileName);

            Texture2D tex;
            if (_resBundle != null)
            {
            #if UNITY_5
                tex = _resBundle.LoadAsset<Texture2D>(filePath);
            #else
                tex = (Texture2D)_resBundle.Load(filePath, typeof(Texture2D));
            #endif
            }
            else
                tex = (Texture2D)_loadFunc(filePath, ext, typeof(Texture2D));
            if (tex == null)
            {
                Debug.LogWarning("FairyGUI: texture '" + fileName + "' not found in " + this.name);
                item.texture = NTexture.Empty;
            }
            else
            {
                if (tex.mipmapCount > 1)
                    Debug.LogWarning("FairyGUI: texture '" + fileName + "' in " + this.name + " is mipmaps enabled.");
                item.texture = new NTexture(tex, (float)tex.width / item.width, (float)tex.height / item.height);
                item.texture.storedODisk = _resBundle == null;

                filePath = filePath + "!a";
                if (_resBundle != null)
                {
            #if UNITY_5
                    tex = _resBundle.LoadAsset<Texture2D>(filePath);
            #else
                    tex = (Texture2D)_resBundle.Load(filePath, typeof(Texture2D));
            #endif
                }
                else
                    tex = (Texture2D)_loadFunc(filePath, ext, typeof(Texture2D));
                if (tex != null)
                {
                    item.texture.alphaTexture = new NTexture(tex);
                    item.texture.alphaTexture.storedODisk = _resBundle == null;
                }
            }
        }
Example #31
0
        internal object GetItemAsset(PackageItem item)
        {
            switch (item.type)
            {
                case PackageItemType.Image:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        AtlasSprite sprite;
                        if (_sprites.TryGetValue(item.id, out sprite))
                            item.texture = CreateSpriteTexture(sprite);
                        else
                            item.texture = NTexture.Empty;
                    }
                    return item.texture;

                case PackageItemType.Atlas:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        string fileName = (item.file != null && item.file.Length > 0) ? item.file : (item.id + ".png");
                        string filePath = _resourceNamePrefix + Path.GetFileNameWithoutExtension(fileName);

                        Texture2D tex;
                        if (_resBundle != null)
            #if UNITY_5
                            tex = _resBundle.LoadAsset<Texture2D>(filePath);
            #else
                            tex = (Texture2D)_resBundle.Load(filePath, typeof(Texture2D));
            #endif
                        else
                            tex = (Texture2D)Resources.Load(filePath, typeof(Texture2D));
                        if (tex == null)
                        {
                            Debug.LogWarning("FairyGUI: texture '" + fileName + "' not found in " + this.name);
                            item.texture = NTexture.Empty;
                        }
                        else
                        {
                            if (tex.mipmapCount > 1)
                                Debug.LogWarning("FairyGUI: texture '" + fileName + "' in " + this.name + " is mipmaps enabled.");
                            item.texture = new NTexture(tex, (float)tex.width / item.width, (float)tex.height / item.height);

                            filePath = filePath + "!a";
                            if (_resBundle != null)
            #if UNITY_5
                                tex = _resBundle.LoadAsset<Texture2D>(filePath);
            #else
                                tex = (Texture2D)_resBundle.Load(filePath, typeof(Texture2D));
            #endif
                            else
                                tex = (Texture2D)Resources.Load(filePath, typeof(Texture2D));
                            if (tex != null)
                                item.texture.alphaTexture = tex;
                        }
                    }
                    return item.texture;

                case PackageItemType.Sound:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        string fileName = _resourceNamePrefix + Path.GetFileNameWithoutExtension(item.file);
                        if (_resBundle != null)
            #if UNITY_5
                            item.audioClip = _resBundle.LoadAsset<AudioClip>(fileName);
            #else
                            item.audioClip = (AudioClip)_resBundle.Load(fileName, typeof(AudioClip));
            #endif
                        else
                            item.audioClip = (AudioClip)Resources.Load(fileName, typeof(AudioClip));
                    }
Example #32
0
        IEnumerator _CreateObjectAsync(PackageItem item, CreateObjectCallback callback)
        {
            Stats.LatestObjectCreation = 0;
            Stats.LatestGraphicsCreation = 0;

            float frameTime = UIConfig.frameTimeForAsyncUIConstruction;

            List<DisplayListItem> itemList = new List<DisplayListItem>();
            CollectComponentChildren(item, itemList);
            itemList.Add(new DisplayListItem(item, null));

            int cnt = itemList.Count;
            List<GObject> objectPool = new List<GObject>(cnt);
            GObject obj;
            DisplayListItem di;
            float t = Time.realtimeSinceStartup;

            for (int i = 0; i < cnt; i++)
            {
                di = itemList[i];
                if (di.packageItem != null)
                {
                    obj = UIObjectFactory.NewObject(di.packageItem);
                    obj.packageItem = di.packageItem;
                    objectPool.Add(obj);

                    _constructing++;
                    if (di.packageItem.type == PackageItemType.Component)
                    {
                        int poolStart = objectPool.Count - di.packageItem.displayList.Length - 1;

                        ((GComponent)obj).ConstructFromResource(objectPool, poolStart);

                        objectPool.RemoveRange(poolStart, di.packageItem.displayList.Length);
                    }
                    else
                    {
                        GetItemAsset(di.packageItem);
                        obj.ConstructFromResource();
                    }
                    _constructing--;
                }
                else
                {
                    obj = UIObjectFactory.NewObject(di.type);
                    objectPool.Add(obj);

                    if (di.type == "list" && di.listItemCount > 0)
                    {
                        int poolStart = objectPool.Count - di.listItemCount - 1;
                        for (int k = 0; k < di.listItemCount; k++) //把他们都放到pool里,这样GList在创建时就不需要创建对象了
                            ((GList)obj).itemPool.ReturnObject(objectPool[k + poolStart]);
                        objectPool.RemoveRange(poolStart, di.listItemCount);
                    }
                }

                if ((i % 5 == 0) && Time.realtimeSinceStartup - t >= frameTime)
                {
                    yield return null;
                    t = Time.realtimeSinceStartup;
                }
            }

            callback(objectPool[0]);
        }
Example #33
0
        protected void LoadFromPackage(string itemURL)
        {
            _contentItem = UIPackage.GetItemByURL(itemURL);

            if (_contentItem != null)
            {
                _contentItem.Load();
                if (_contentItem.type == PackageItemType.Image)
                {
                    _image.texture = _contentItem.texture;
                    _image.scale9Grid = _contentItem.scale9Grid;
                    _image.scaleByTile = _contentItem.scaleByTile;
                    _activeObject = _image;

                    _contentSourceWidth = _contentItem.width;
                    _contentSourceHeight = _contentItem.height;
                    UpdateLayout();
                }
                else if (_contentItem.type == PackageItemType.MovieClip)
                {
                    if (_movieClip == null)
                    {
                        _movieClip = new MovieClip();
                        if (grayed)
                            _movieClip.SetGrayed(true);
                        _container.AddChild(_movieClip);
                    }
                    _movieClip.interval = _contentItem.interval;
                    _movieClip.frames = _contentItem.frames;
                    _movieClip.boundsRect = new Rect(0, 0, _contentSourceWidth, _contentSourceHeight);
                    _movieClip.playing = _playing;
                    _movieClip.currentFrame = _frame;
                    _activeObject = _movieClip;

                    _contentSourceWidth = _contentItem.width;
                    _contentSourceHeight = _contentItem.height;
                    UpdateLayout();
                }
                else
                    SetErrorState();
            }
            else
                SetErrorState();
        }
        bool LoadPackage(ByteBuffer buffer, string packageSource, string assetNamePrefix)
        {
            if (buffer.ReadUint() != 0x46475549)
            {
                if (Application.isPlaying)
                {
                    throw new Exception("FairyGUI: old package format found in '" + packageSource + "'");
                }
                else
                {
                    Debug.LogWarning("FairyGUI: old package format found in '" + packageSource + "'");
                    return(false);
                }
            }

            buffer.version = buffer.ReadInt();
            buffer.ReadBool(); //compressed
            id   = buffer.ReadString();
            name = buffer.ReadString();
            if (_packageInstById.ContainsKey(id) && name != _packageInstById[id].name)
            {
                Debug.LogWarning("FairyGUI: Package id conflicts, '" + name + "' and '" + _packageInstById[id].name + "'");
                return(false);
            }
            buffer.Skip(20);
            int indexTablePos = buffer.position;
            int cnt;

            buffer.Seek(indexTablePos, 4);

            cnt = buffer.ReadInt();
            string[] stringTable = new string[cnt];
            for (int i = 0; i < cnt; i++)
            {
                stringTable[i] = buffer.ReadString();
            }
            buffer.stringTable = stringTable;

            buffer.Seek(indexTablePos, 1);

            PackageItem pi;

            if (assetNamePrefix == null)
            {
                assetNamePrefix = string.Empty;
            }
            else if (assetNamePrefix.Length > 0)
            {
                assetNamePrefix = assetNamePrefix + "_";
            }

            cnt = buffer.ReadShort();
            for (int i = 0; i < cnt; i++)
            {
                int nextPos = buffer.ReadInt();
                nextPos += buffer.position;

                pi       = new PackageItem();
                pi.owner = this;
                pi.type  = (PackageItemType)buffer.ReadByte();
                pi.id    = buffer.ReadS();
                pi.name  = buffer.ReadS();
                buffer.ReadS(); //path
                pi.file     = buffer.ReadS();
                pi.exported = buffer.ReadBool();
                pi.width    = buffer.ReadInt();
                pi.height   = buffer.ReadInt();

                switch (pi.type)
                {
                case PackageItemType.Image:
                {
                    pi.objectType = ObjectType.Image;
                    int scaleOption = buffer.ReadByte();
                    if (scaleOption == 1)
                    {
                        Rect rect = new Rect();
                        rect.x        = buffer.ReadInt();
                        rect.y        = buffer.ReadInt();
                        rect.width    = buffer.ReadInt();
                        rect.height   = buffer.ReadInt();
                        pi.scale9Grid = rect;

                        pi.tileGridIndice = buffer.ReadInt();
                    }
                    else if (scaleOption == 2)
                    {
                        pi.scaleByTile = true;
                    }

                    buffer.ReadBool();         //smoothing
                    break;
                }

                case PackageItemType.MovieClip:
                {
                    buffer.ReadBool();         //smoothing
                    pi.objectType = ObjectType.MovieClip;
                    pi.rawData    = buffer.ReadBuffer();
                    break;
                }

                case PackageItemType.Font:
                {
                    pi.rawData = buffer.ReadBuffer();
                    break;
                }

                case PackageItemType.Component:
                {
                    int extension = buffer.ReadByte();
                    if (extension > 0)
                    {
                        pi.objectType = (ObjectType)extension;
                    }
                    else
                    {
                        pi.objectType = ObjectType.Component;
                    }
                    pi.rawData = buffer.ReadBuffer();

                    UIObjectFactory.ResolvePackageItemExtension(pi);
                    break;
                }

                case PackageItemType.Atlas:
                case PackageItemType.Sound:
                case PackageItemType.Misc:
                {
                    pi.file = assetNamePrefix + pi.file;
                    break;
                }
                }
                _items.Add(pi);
                _itemsById[pi.id] = pi;
                if (pi.name != null)
                {
                    _itemsByName[pi.name] = pi;
                }

                buffer.position = nextPos;
            }

            buffer.Seek(indexTablePos, 2);

            cnt = buffer.ReadShort();
            for (int i = 0; i < cnt; i++)
            {
                int nextPos = buffer.ReadShort();
                nextPos += buffer.position;

                string itemId = buffer.ReadS();
                pi = _itemsById[buffer.ReadS()];

                AtlasSprite sprite = new AtlasSprite();
                sprite.atlas       = pi;
                sprite.rect.x      = buffer.ReadInt();
                sprite.rect.y      = buffer.ReadInt();
                sprite.rect.width  = buffer.ReadInt();
                sprite.rect.height = buffer.ReadInt();
                sprite.rotated     = buffer.ReadBool();
                _sprites[itemId]   = sprite;

                buffer.position = nextPos;
            }

            if (buffer.Seek(indexTablePos, 3))
            {
                cnt = buffer.ReadShort();
                for (int i = 0; i < cnt; i++)
                {
                    int nextPos = buffer.ReadInt();
                    nextPos += buffer.position;

                    if (_itemsById.TryGetValue(buffer.ReadS(), out pi))
                    {
                        if (pi.type == PackageItemType.Image)
                        {
                            pi.pixelHitTestData = new PixelHitTestData();
                            pi.pixelHitTestData.Load(buffer);
                        }
                    }

                    buffer.position = nextPos;
                }
            }

            if (!Application.isPlaying)
            {
                _items.Sort(ComparePackageItem);
            }

            return(true);
        }
 public virtual void ConstructFromResource(PackageItem pkgItem)
 {
     _packageItem = pkgItem;
 }
Example #36
0
 void LoadComponent(PackageItem item)
 {
     string str = _descPack[item.id + ".xml"];
     item.componentData = new XML(str);
 }
Example #37
0
        GObject CreateObject(PackageItem item, System.Type userClass)
        {
            Stats.LatestObjectCreation = 0;
            Stats.LatestGraphicsCreation = 0;

            GetItemAsset(item);

            GObject g = null;
            if (item.type == PackageItemType.Component)
            {
                if (userClass != null)
                    g = (GComponent)userClass.Assembly.CreateInstance(userClass.FullName);
                else
                    g = UIObjectFactory.NewObject(item);
            }
            else
                g = UIObjectFactory.NewObject(item);

            if (g == null)
                return null;

            _constructing++;
            g.packageItem = item;
            g.ConstructFromResource();
            _constructing--;
            return g;
        }
        protected void LoadFromPackage(string itemURL)
        {
            _contentItem = UIPackage.GetItemByURL(itemURL);

            if (_contentItem != null)
            {
                _contentItem.Load();
                if (_contentItem.type == PackageItemType.Image)
                {
                    _content.texture = _contentItem.texture;
                    _content.scale9Grid = _contentItem.scale9Grid;
                    _content.scaleByTile = _contentItem.scaleByTile;

                    _contentSourceWidth = _contentItem.width;
                    _contentSourceHeight = _contentItem.height;
                    UpdateLayout();
                }
                else if (_contentItem.type == PackageItemType.MovieClip)
                {
                    _contentSourceWidth = _contentItem.width;
                    _contentSourceHeight = _contentItem.height;

                    _content.interval = _contentItem.interval;
                    _content.swing = _contentItem.swing;
                    _content.repeatDelay = _contentItem.repeatDelay;
                    _content.SetData(_contentItem.texture, _contentItem.frames, new Rect(0, 0, _contentSourceWidth, _contentSourceHeight));
                    _content.playing = _playing;
                    _content.currentFrame = _frame;

                    UpdateLayout();
                }
                else
                    SetErrorState();
            }
            else
                SetErrorState();
        }
Example #39
0
        void TranslateComponent(PackageItem item)
        {
            if (_stringsSource == null)
                return;

            Dictionary<string, string> strings;
            if (!_stringsSource.TryGetValue(this.id + item.id, out strings))
                return;

            string ename, elementId, value;
            XML cxml, dxml;
            int dcnt = item.displayList.Length;
            for (int i = 0; i < dcnt; i++)
            {
                cxml = item.displayList[i].desc;
                ename = cxml.name;
                elementId = cxml.GetAttribute("id");
                if (cxml.HasAttribute("tooltips"))
                {
                    if (strings.TryGetValue(elementId + "-tips", out value))
                        cxml.SetAttribute("tooltips", value);
                }

                dxml = cxml.GetNode("gearText");
                if (dxml != null)
                {
                    if (strings.TryGetValue(elementId + "-texts", out value))
                        dxml.SetAttribute("values", value);

                    if (strings.TryGetValue(elementId + "-texts_def", out value))
                        dxml.SetAttribute("default", value);
                }

                if (ename == "text" || ename == "richtext")
                {
                    if (strings.TryGetValue(elementId, out value))
                        cxml.SetAttribute("text", value);
                    if (strings.TryGetValue(elementId + "-prompt", out value))
                        cxml.SetAttribute("prompt", value);
                }
                else if (ename == "list")
                {
                    XMLList.Enumerator et = cxml.GetEnumerator("item");
                    int j = 0;
                    while (et.MoveNext())
                    {
                        if (strings.TryGetValue(elementId + "-" + j, out value))
                            et.Current.SetAttribute("title", value);
                        j++;
                    }
                }
                else if (ename == "component")
                {
                    dxml = cxml.GetNode("Button");
                    if (dxml != null)
                    {
                        if (strings.TryGetValue(elementId, out value))
                            dxml.SetAttribute("title", value);
                        if (strings.TryGetValue(elementId + "-0", out value))
                            dxml.SetAttribute("selectedTitle", value);
                        continue;
                    }

                    dxml = cxml.GetNode("Label");
                    if (dxml != null)
                    {
                        if (strings.TryGetValue(elementId, out value))
                            dxml.SetAttribute("title", value);
                        continue;
                    }

                    dxml = cxml.GetNode("ComboBox");
                    if (dxml != null)
                    {
                        if (strings.TryGetValue(elementId, out value))
                            dxml.SetAttribute("title", value);

                        XMLList.Enumerator et = dxml.GetEnumerator("item");
                        int j = 0;
                        while (et.MoveNext())
                        {
                            if (strings.TryGetValue(elementId + "-" + j, out value))
                                et.Current.SetAttribute("title", value);
                            j++;
                        }

                        continue;
                    }
                }
            }
        }
Example #40
0
        void LoadComponentChildren(PackageItem item)
        {
            XML listNode = item.componentData.GetNode("displayList");
            if (listNode != null)
            {
                XMLList col = listNode.Elements();
                int dcnt = col.Count;
                item.displayList = new DisplayListItem[dcnt];
                DisplayListItem di;
                for (int i = 0; i < dcnt; i++)
                {
                    XML cxml = col[i];

                    string src = cxml.GetAttribute("src");
                    if (src != null)
                    {
                        string pkgId = cxml.GetAttribute("pkg");
                        UIPackage pkg;
                        if (pkgId != null && pkgId != item.owner.id)
                            pkg = UIPackage.GetById(pkgId);
                        else
                            pkg = item.owner;

                        PackageItem pi = pkg != null ? pkg.GetItem(src) : null;
                        if (pi != null)
                            di = new DisplayListItem(pi, null);
                        else
                            di = new DisplayListItem(null, cxml.name);
                    }
                    else
                    {
                        if (cxml.name == "text" && cxml.GetAttributeBool("input", false))
                            di = new DisplayListItem(null, "inputtext");
                        else
                            di = new DisplayListItem(null, cxml.name);
                    }

                    di.desc = cxml;
                    item.displayList[i] = di;
                }
            }
            else
                item.displayList = new DisplayListItem[0];
        }
Example #41
0
        private void ClearContent()
        {
            ClearErrorState();

            if (_activeObject != null)
            {
                if (_image == _activeObject)
                {
                    if (_image.texture != null)
                    {
                        if (_contentItem == null)
                            FreeExternal(image.texture);
                        _image.texture = null;
                    }
                }
                else if (_movieClip == _activeObject)
                    _movieClip.frames = null;
                _activeObject = null;
            }

            _contentItem = null;
        }
Example #42
0
 void LoadSound(PackageItem item)
 {
     string fileName = _assetNamePrefix + Path.GetFileNameWithoutExtension(item.file);
     string ext = Path.GetExtension(item.file);
     if (_resBundle != null)
     {
     #if UNITY_5
         item.audioClip = _resBundle.LoadAsset<AudioClip>(fileName);
     #else
         item.audioClip = (AudioClip)_resBundle.Load(fileName, typeof(AudioClip));
     #endif
     }
     else
         item.audioClip = (AudioClip)_loadFunc(fileName, ext, typeof(AudioClip));
 }
Example #43
0
        void LoadPackage()
        {
            string[] arr = null;
            string str;

            str = LoadString("sprites.bytes");
            arr = str.Split(sep1);
            int cnt = arr.Length;
            for (int i = 1; i < cnt; i++)
            {
                str = arr[i];
                if (str.Length == 0)
                    continue;

                string[] arr2 = str.Split(sep2);
                AtlasSprite sprite = new AtlasSprite();
                string itemId = arr2[0];
                int binIndex = int.Parse(arr2[1]);
                if (binIndex >= 0)
                    sprite.atlas = "atlas" + binIndex;
                else
                {
                    int pos = itemId.IndexOf("_");
                    if (pos == -1)
                        sprite.atlas = "atlas_" + itemId;
                    else
                        sprite.atlas = "atlas_" + itemId.Substring(0, pos);
                }
                sprite.rect.x = int.Parse(arr2[2]);
                sprite.rect.y = int.Parse(arr2[3]);
                sprite.rect.width = int.Parse(arr2[4]);
                sprite.rect.height = int.Parse(arr2[5]);
                sprite.rotated = arr2[6] == "1";
                _sprites[itemId] = sprite;
            }

            str = GetDesc("package.xml");
            XML xml = new XML(str);

            id = xml.GetAttribute("id");
            name = xml.GetAttribute("name");

            XML rxml = xml.GetNode("resources");
            if (rxml == null)
                throw new Exception("Invalid package xml");

            XMLList resources = rxml.Elements();

            _itemsById = new Dictionary<string, PackageItem>();
            _itemsByName = new Dictionary<string, PackageItem>(); ;
            PackageItem pi;

            foreach (XML cxml in resources)
            {
                pi = new PackageItem();
                pi.type = FieldTypes.ParsePackageItemType(cxml.name);
                pi.id = cxml.GetAttribute("id");
                pi.name = cxml.GetAttribute("name");
                pi.file = cxml.GetAttribute("file");
                str = cxml.GetAttribute("size");
                if (str != null)
                {
                    arr = str.Split(sep0);
                    pi.width = int.Parse(arr[0]);
                    pi.height = int.Parse(arr[1]);
                }
                switch (pi.type)
                {
                    case PackageItemType.Image:
                        {
                            string scale = cxml.GetAttribute("scale");
                            if (scale == "9grid")
                            {
                                arr = cxml.GetAttributeArray("scale9grid");
                                if (arr != null)
                                {
                                    Rect rect = new Rect();
                                    rect.x = int.Parse(arr[0]);
                                    rect.y = int.Parse(arr[1]);
                                    rect.width = int.Parse(arr[2]);
                                    rect.height = int.Parse(arr[3]);
                                    pi.scale9Grid = rect;
                                }
                            }
                            else if (scale == "tile")
                                pi.scaleByTile = true;
                            break;
                        }
                }
                pi.owner = this;
                _items.Add(pi);
                _itemsById[pi.id] = pi;
                if (pi.name != null)
                    _itemsByName[pi.name] = pi;
            }

            cnt = _items.Count;
            for (int i = 0; i < cnt; i++)
            {
                pi = _items[i];
                if (pi.type == PackageItemType.Font)
                {
                    pi.Load();
                    FontManager.RegisterFont(pi.bitmapFont, null);
                }
                else
                    GetItemAsset(pi);
            }

            if (_resBundle != null)
            {
                _resBundle.Unload(false);
                _resBundle = null;
            }
        }
Example #44
0
        void LoadPackage()
        {
            string[] arr = null;
            string str;

            str = LoadString("sprites.bytes");
            if (str == null)
            {
                Debug.LogError("FairyGUI: cannot load package from " + _assetNamePrefix);
                return;
            }
            arr = str.Split(sep1);
            int cnt = arr.Length;
            for (int i = 1; i < cnt; i++)
            {
                str = arr[i];
                if (str.Length == 0)
                    continue;

                string[] arr2 = str.Split(sep2);
                AtlasSprite sprite = new AtlasSprite();
                string itemId = arr2[0];
                int binIndex = int.Parse(arr2[1]);
                if (binIndex >= 0)
                    sprite.atlas = "atlas" + binIndex;
                else
                {
                    int pos = itemId.IndexOf("_");
                    if (pos == -1)
                        sprite.atlas = "atlas_" + itemId;
                    else
                        sprite.atlas = "atlas_" + itemId.Substring(0, pos);
                }
                sprite.rect.x = int.Parse(arr2[2]);
                sprite.rect.y = int.Parse(arr2[3]);
                sprite.rect.width = int.Parse(arr2[4]);
                sprite.rect.height = int.Parse(arr2[5]);
                sprite.rotated = arr2[6] == "1";
                _sprites[itemId] = sprite;
            }

            byte[] hittestData = LoadBinary("hittest.bytes");
            if (hittestData != null)
            {
                ByteBuffer ba = new ByteBuffer(hittestData);
                while (ba.bytesAvailable)
                {
                    PixelHitTestData pht = new PixelHitTestData();
                    _hitTestDatas[ba.ReadString()] = pht;
                    pht.Load(ba);
                }
            }

            str = _descPack["package.xml"];
            XML xml = new XML(str);

            id = xml.GetAttribute("id");
            name = xml.GetAttribute("name");

            XML rxml = xml.GetNode("resources");
            if (rxml == null)
                throw new Exception("Invalid package xml");

            XMLList resources = rxml.Elements();

            _itemsById = new Dictionary<string, PackageItem>();
            _itemsByName = new Dictionary<string, PackageItem>();
            PackageItem pi;

            foreach (XML cxml in resources)
            {
                pi = new PackageItem();
                pi.owner = this;
                pi.type = FieldTypes.ParsePackageItemType(cxml.name);
                pi.id = cxml.GetAttribute("id");
                pi.name = cxml.GetAttribute("name");
                pi.exported = cxml.GetAttributeBool("exported");
                pi.file = cxml.GetAttribute("file");
                str = cxml.GetAttribute("size");
                if (str != null)
                {
                    arr = str.Split(sep0);
                    pi.width = int.Parse(arr[0]);
                    pi.height = int.Parse(arr[1]);
                }
                switch (pi.type)
                {
                    case PackageItemType.Image:
                        {
                            string scale = cxml.GetAttribute("scale");
                            if (scale == "9grid")
                            {
                                arr = cxml.GetAttributeArray("scale9grid");
                                if (arr != null)
                                {
                                    Rect rect = new Rect();
                                    rect.x = int.Parse(arr[0]);
                                    rect.y = int.Parse(arr[1]);
                                    rect.width = int.Parse(arr[2]);
                                    rect.height = int.Parse(arr[3]);
                                    pi.scale9Grid = rect;
                                }
                            }
                            else if (scale == "tile")
                                pi.scaleByTile = true;
                            break;
                        }

                    case PackageItemType.Font:
                        {
                            pi.bitmapFont = new BitmapFont(pi);
                            FontManager.RegisterFont(pi.bitmapFont, null);
                            break;
                        }
                }
                _items.Add(pi);
                _itemsById[pi.id] = pi;
                if (pi.name != null)
                    _itemsByName[pi.name] = pi;
            }

            bool preloadAll = Application.isPlaying;
            if (preloadAll)
            {
                cnt = _items.Count;
                for (int i = 0; i < cnt; i++)
                    GetItemAsset(_items[i]);

                _descPack = null;
                _sprites = null;
            }
            else
                _items.Sort(ComparePackageItem);

            if (_resBundle != null)
            {
                _resBundle.Unload(false);
                _resBundle = null;
            }
        }
Example #45
0
        internal void ConstructFromResource(List <GObject> objectPool, int poolIndex)
        {
            if (!packageItem.translated)
            {
                packageItem.translated = true;
                TranslationHelper.TranslateComponent(packageItem);
            }

            ByteBuffer buffer = packageItem.rawData;

            buffer.Seek(0, 0);

            underConstruct = true;

            sourceWidth  = buffer.ReadInt();
            sourceHeight = buffer.ReadInt();
            initWidth    = sourceWidth;
            initHeight   = sourceHeight;

            SetSize(sourceWidth, sourceHeight);

            if (buffer.ReadBool())
            {
                minWidth  = buffer.ReadInt();
                maxWidth  = buffer.ReadInt();
                minHeight = buffer.ReadInt();
                maxHeight = buffer.ReadInt();
            }

            if (buffer.ReadBool())
            {
                float f1 = buffer.ReadFloat();
                float f2 = buffer.ReadFloat();
                SetPivot(f1, f2, buffer.ReadBool());
            }

            if (buffer.ReadBool())
            {
                _margin.top    = buffer.ReadInt();
                _margin.bottom = buffer.ReadInt();
                _margin.left   = buffer.ReadInt();
                _margin.right  = buffer.ReadInt();
            }

            OverflowType overflow = (OverflowType)buffer.ReadByte();

            if (overflow == OverflowType.Scroll)
            {
                int savedPos = buffer.position;
                buffer.Seek(0, 7);
                SetupScroll(buffer);
                buffer.position = savedPos;
            }
            else
            {
                SetupOverflow(overflow);
            }

            if (buffer.ReadBool())             //clipSoftness
            {
                buffer.Skip(8);
            }

            _buildingDisplayList = true;

            buffer.Seek(0, 1);

            int controllerCount = buffer.ReadShort();

            for (int i = 0; i < controllerCount; i++)
            {
                int nextPos = buffer.ReadShort();
                nextPos += buffer.position;

                Controller controller = new Controller();
                _controllers.Add(controller);
                controller.parent = this;
                controller.Setup(buffer);

                buffer.position = nextPos;
            }

            buffer.Seek(0, 2);

            GObject child;
            int     childCount = buffer.ReadShort();

            for (int i = 0; i < childCount; i++)
            {
                int dataLen = buffer.ReadShort();
                int curPos  = buffer.position;

                if (objectPool != null)
                {
                    child = objectPool[poolIndex + i];
                }
                else
                {
                    buffer.Seek(curPos, 0);

                    ObjectType type  = (ObjectType)buffer.ReadByte();
                    string     src   = buffer.ReadS();
                    string     pkgId = buffer.ReadS();

                    PackageItem pi = null;
                    if (src != null)
                    {
                        UIPackage pkg;
                        if (pkgId != null)
                        {
                            pkg = UIPackage.GetById(pkgId);
                        }
                        else
                        {
                            pkg = packageItem.owner;
                        }

                        pi = pkg != null?pkg.GetItem(src) : null;
                    }

                    if (pi != null)
                    {
                        child             = UIObjectFactory.NewObject(pi);
                        child.packageItem = pi;
                        child.ConstructFromResource();
                    }
                    else
                    {
                        child = UIObjectFactory.NewObject(type);
                    }
                }

                child.underConstruct = true;
                child.Setup_BeforeAdd(buffer, curPos);
                child.InternalSetParent(this);
                _children.Add(child);

                buffer.position = curPos + dataLen;
            }

            buffer.Seek(0, 3);
            this.relations.Setup(buffer, true);

            buffer.Seek(0, 2);
            buffer.Skip(2);

            for (int i = 0; i < childCount; i++)
            {
                int nextPos = buffer.ReadShort();
                nextPos += buffer.position;

                buffer.Seek(buffer.position, 3);
                _children[i].relations.Setup(buffer, false);

                buffer.position = nextPos;
            }

            buffer.Seek(0, 2);
            buffer.Skip(2);

            for (int i = 0; i < childCount; i++)
            {
                int nextPos = buffer.ReadShort();
                nextPos += buffer.position;

                child = _children[i];
                child.Setup_AfterAdd(buffer, buffer.position);
                child.underConstruct = false;

                buffer.position = nextPos;
            }

            buffer.Seek(0, 4);

            buffer.Skip(2);             //customData
            this.opaque = buffer.ReadBool();
            int maskId = buffer.ReadShort();

            if (maskId != -1)
            {
                this.mask = GetChildAt(maskId).displayObject;
                buffer.ReadBool();                 //reversedMask
            }
            string hitTestId = buffer.ReadS();

            if (hitTestId != null)
            {
                PackageItem pi = packageItem.owner.GetItem(hitTestId);
                if (pi != null && pi.pixelHitTestData != null)
                {
                    int i1 = buffer.ReadInt();
                    int i2 = buffer.ReadInt();
                    this.rootContainer.hitArea = new PixelHitTest(pi.pixelHitTestData, i1, i2);
                }
            }

            buffer.Seek(0, 5);

            int transitionCount = buffer.ReadShort();

            for (int i = 0; i < transitionCount; i++)
            {
                int nextPos = buffer.ReadShort();
                nextPos += buffer.position;

                Transition trans = new Transition(this);
                trans.Setup(buffer);
                _transitions.Add(trans);

                buffer.position = nextPos;
            }

            if (_transitions.Count > 0)
            {
                this.onAddedToStage.Add(__addedToStage);
                this.onRemovedFromStage.Add(__removedFromStage);
            }

            ApplyAllControllers();

            _buildingDisplayList = false;
            underConstruct       = false;

            BuildNativeDisplayList();
            SetBoundsChangedFlag();

            if (packageItem.objectType != ObjectType.Component)
            {
                ConstructExtension(buffer);
            }

            OnConstruct();
        }
Example #46
0
        internal object GetItemAsset(PackageItem item)
        {
            switch (item.type)
            {
                case PackageItemType.Image:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        AtlasSprite sprite;
                        if (_sprites.TryGetValue(item.id, out sprite))
                            item.texture = CreateSpriteTexture(sprite);
                        else
                            item.texture = NTexture.Empty;
                    }
                    return item.texture;

                case PackageItemType.Atlas:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        string fileName = string.IsNullOrEmpty(item.file) ? (item.id + ".png") : item.file;
                        string filePath = _assetNamePrefix + Path.GetFileNameWithoutExtension(fileName);
                        string ext = Path.GetExtension(fileName);

                        Texture2D tex;
                        if (_resBundle != null)
                        {
            #if UNITY_5
                            tex = _resBundle.LoadAsset<Texture2D>(filePath);
            #else
                            tex = (Texture2D)_resBundle.Load(filePath, typeof(Texture2D));
            #endif
                        }
                        else
                            tex = (Texture2D)_loadFunc(filePath, ext, typeof(Texture2D));
                        if (tex == null)
                        {
                            Debug.LogWarning("FairyGUI: texture '" + fileName + "' not found in " + this.name);
                            item.texture = NTexture.Empty;
                        }
                        else
                        {
                            if (tex.mipmapCount > 1)
                                Debug.LogWarning("FairyGUI: texture '" + fileName + "' in " + this.name + " is mipmaps enabled.");
                            item.texture = new NTexture(tex, (float)tex.width / item.width, (float)tex.height / item.height);

                            filePath = filePath + "!a";
                            if (_resBundle != null)
                            {
            #if UNITY_5
                                tex = _resBundle.LoadAsset<Texture2D>(filePath);
            #else
                                tex = (Texture2D)_resBundle.Load(filePath, typeof(Texture2D));
            #endif
                            }
                            else
                                tex = (Texture2D)_loadFunc(filePath, ext, typeof(Texture2D));
                            if (tex != null)
                                item.texture.alphaTexture = tex;
                        }
                    }
                    return item.texture;

                case PackageItemType.Sound:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        string fileName = _assetNamePrefix + Path.GetFileNameWithoutExtension(item.file);
                        string ext = Path.GetExtension(item.file);
                        if (_resBundle != null)
                        {
            #if UNITY_5
                            item.audioClip = _resBundle.LoadAsset<AudioClip>(fileName);
            #else
                            item.audioClip = (AudioClip)_resBundle.Load(fileName, typeof(AudioClip));
            #endif
                        }
                        else
                            item.audioClip = (AudioClip)_loadFunc(fileName, ext, typeof(AudioClip));
                    }
                    return item.audioClip;

                case PackageItemType.Font:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        LoadFont(item);
                    }
                    return item.bitmapFont;

                case PackageItemType.MovieClip:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        LoadMovieClip(item);
                    }
                    return item.frames;

                case PackageItemType.Component:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        string str = _descPack[item.id + ".xml"];
                        XML xml = new XML(str);
                        if (_stringsSource != null)
                        {
                            Dictionary<string, string> strings;
                            if (_stringsSource.TryGetValue(this.id + item.id, out strings))
                                TranslateComponent(xml, strings);
                        }
                        item.componentData = xml;
                    }
                    return item.componentData;

                default:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        item.binary = LoadBinary(item.file);
                    }
                    return item.binary;
            }
        }
        private void ClearContent()
        {
            ClearErrorState();

            if (_content.texture != null)
            {
                if (_contentItem == null)
                    FreeExternal(image.texture);
                _content.texture = null;
            }

            _content.Clear();
            _contentItem = null;
        }
Example #48
0
        void LoadPackage()
        {
            string[] arr = null;
            string   str;

            str = LoadString("sprites.bytes");
            if (str == null)
            {
                Debug.LogError("FairyGUI: cannot load package from '" + _assetNamePrefix + "'");
                return;
            }
            arr = str.Split('\n');
            int cnt = arr.Length;

            for (int i = 1; i < cnt; i++)
            {
                str = arr[i];
                if (str.Length == 0)
                {
                    continue;
                }

                string[]    arr2     = str.Split(' ');
                AtlasSprite sprite   = new AtlasSprite();
                string      itemId   = arr2[0];
                int         binIndex = int.Parse(arr2[1]);
                if (binIndex >= 0)
                {
                    sprite.atlas = "atlas" + binIndex;
                }
                else
                {
                    int pos = itemId.IndexOf("_");
                    if (pos == -1)
                    {
                        sprite.atlas = "atlas_" + itemId;
                    }
                    else
                    {
                        sprite.atlas = "atlas_" + itemId.Substring(0, pos);
                    }
                }
                sprite.rect.x      = int.Parse(arr2[2]);
                sprite.rect.y      = int.Parse(arr2[3]);
                sprite.rect.width  = int.Parse(arr2[4]);
                sprite.rect.height = int.Parse(arr2[5]);
                sprite.rotated     = arr2[6] == "1";
                _sprites[itemId]   = sprite;
            }

            byte[] hittestData = LoadBinary("hittest.bytes");
            if (hittestData != null)
            {
                ByteBuffer ba = new ByteBuffer(hittestData);
                while (ba.bytesAvailable)
                {
                    PixelHitTestData pht = new PixelHitTestData();
                    _hitTestDatas[ba.ReadString()] = pht;
                    pht.Load(ba);
                }
            }

            if (!_descPack.TryGetValue("package.xml", out str))
            {
                throw new Exception("FairyGUI: invalid package '" + _assetNamePrefix + "'");
            }

            XML xml = new XML(str);

            id   = xml.GetAttribute("id");
            name = xml.GetAttribute("name");

            XML rxml = xml.GetNode("resources");

            if (rxml == null)
            {
                throw new Exception("FairyGUI: invalid package xml '" + _assetNamePrefix + "'");
            }

            XMLList resources = rxml.Elements();

            _itemsById   = new Dictionary <string, PackageItem>();
            _itemsByName = new Dictionary <string, PackageItem>();
            PackageItem pi;

            foreach (XML cxml in resources)
            {
                pi          = new PackageItem();
                pi.owner    = this;
                pi.type     = FieldTypes.ParsePackageItemType(cxml.name);
                pi.id       = cxml.GetAttribute("id");
                pi.name     = cxml.GetAttribute("name");
                pi.exported = cxml.GetAttributeBool("exported");
                pi.file     = cxml.GetAttribute("file");
                str         = cxml.GetAttribute("size");
                if (str != null)
                {
                    arr       = str.Split(',');
                    pi.width  = int.Parse(arr[0]);
                    pi.height = int.Parse(arr[1]);
                }
                switch (pi.type)
                {
                case PackageItemType.Image:
                {
                    string scale = cxml.GetAttribute("scale");
                    if (scale == "9grid")
                    {
                        arr = cxml.GetAttributeArray("scale9grid");
                        if (arr != null)
                        {
                            Rect rect = new Rect();
                            rect.x        = int.Parse(arr[0]);
                            rect.y        = int.Parse(arr[1]);
                            rect.width    = int.Parse(arr[2]);
                            rect.height   = int.Parse(arr[3]);
                            pi.scale9Grid = rect;
                        }
                    }
                    else if (scale == "tile")
                    {
                        pi.scaleByTile = true;
                    }
                    break;
                }

                case PackageItemType.Font:
                {
                    pi.bitmapFont = new BitmapFont(pi);
                    FontManager.RegisterFont(pi.bitmapFont, null);
                    break;
                }
                }
                _items.Add(pi);
                _itemsById[pi.id] = pi;
                if (pi.name != null)
                {
                    _itemsByName[pi.name] = pi;
                }
            }

            bool preloadAll = Application.isPlaying;

            if (preloadAll)
            {
                cnt = _items.Count;
                for (int i = 0; i < cnt; i++)
                {
                    GetItemAsset(_items[i]);
                }

                _descPack = null;
                _sprites  = null;
            }
            else
            {
                _items.Sort(ComparePackageItem);
            }

            if (_resBundle != null)
            {
                _resBundle.Unload(false);
                _resBundle = null;
            }
        }
Example #49
0
        public override void ConstructFromResource(PackageItem pkgItem)
        {
            _packageItem = pkgItem;
            _packageItem.Load();

            ConstructFromXML(_packageItem.componentData);
        }
Example #50
0
        void LoadPackage()
        {
            string[] arr = null;
            string   str;

            str = LoadString("sprites.bytes");
            arr = str.Split(sep1);
            int cnt = arr.Length;

            for (int i = 1; i < cnt; i++)
            {
                str = arr[i];
                if (str.Length == 0)
                {
                    continue;
                }

                string[]    arr2     = str.Split(sep2);
                AtlasSprite sprite   = new AtlasSprite();
                string      itemId   = arr2[0];
                int         binIndex = int.Parse(arr2[1]);
                if (binIndex >= 0)
                {
                    sprite.atlas = "atlas" + binIndex;
                }
                else
                {
                    int pos = itemId.IndexOf("_");
                    if (pos == -1)
                    {
                        sprite.atlas = "atlas_" + itemId;
                    }
                    else
                    {
                        sprite.atlas = "atlas_" + itemId.Substring(0, pos);
                    }
                }
                sprite.rect.x      = int.Parse(arr2[2]);
                sprite.rect.y      = int.Parse(arr2[3]);
                sprite.rect.width  = int.Parse(arr2[4]);
                sprite.rect.height = int.Parse(arr2[5]);
                sprite.rotated     = arr2[6] == "1";
                _sprites[itemId]   = sprite;
            }

            str = GetDesc("package.xml");
            XML xml = new XML(str);

            id   = xml.GetAttribute("id");
            name = xml.GetAttribute("name");

            XML rxml = xml.GetNode("resources");

            if (rxml == null)
            {
                throw new Exception("Invalid package xml");
            }

            XMLList resources = rxml.Elements();

            _itemsById   = new Dictionary <string, PackageItem>();
            _itemsByName = new Dictionary <string, PackageItem>();;
            PackageItem pi;

            foreach (XML cxml in resources)
            {
                pi      = new PackageItem();
                pi.type = FieldTypes.ParsePackageItemType(cxml.name);
                pi.id   = cxml.GetAttribute("id");
                pi.name = cxml.GetAttribute("name");
                pi.file = cxml.GetAttribute("file");
                str     = cxml.GetAttribute("size");
                if (str != null)
                {
                    arr       = str.Split(sep0);
                    pi.width  = int.Parse(arr[0]);
                    pi.height = int.Parse(arr[1]);
                }
                switch (pi.type)
                {
                case PackageItemType.Image:
                {
                    string scale = cxml.GetAttribute("scale");
                    if (scale == "9grid")
                    {
                        arr = cxml.GetAttributeArray("scale9grid");
                        if (arr != null)
                        {
                            Rect rect = new Rect();
                            rect.x        = int.Parse(arr[0]);
                            rect.y        = int.Parse(arr[1]);
                            rect.width    = int.Parse(arr[2]);
                            rect.height   = int.Parse(arr[3]);
                            pi.scale9Grid = rect;
                        }
                    }
                    else if (scale == "tile")
                    {
                        pi.scaleByTile = true;
                    }
                    break;
                }
                }
                pi.owner = this;
                _items.Add(pi);
                _itemsById[pi.id] = pi;
                if (pi.name != null)
                {
                    _itemsByName[pi.name] = pi;
                }
            }

            cnt = _items.Count;
            for (int i = 0; i < cnt; i++)
            {
                pi = _items[i];
                if (pi.type == PackageItemType.Font)
                {
                    pi.Load();
                    FontManager.RegisterFont(pi.bitmapFont, null);
                }
                else
                {
                    GetItemAsset(pi);
                }
            }

            if (_resBundle != null)
            {
                _resBundle.Unload(false);
                _resBundle = null;
            }
        }
        void LoadFont(PackageItem item)
        {
            BitmapFont font = new BitmapFont(item);

            item.bitmapFont = font;
            ByteBuffer buffer = item.rawData;

            buffer.Seek(0, 0);

            bool ttf = buffer.ReadBool();

            font.canTint    = buffer.ReadBool();
            font.resizable  = buffer.ReadBool();
            font.hasChannel = buffer.ReadBool();
            int fontSize   = buffer.ReadInt();
            int xadvance   = buffer.ReadInt();
            int lineHeight = buffer.ReadInt();

            float       texScaleX   = 1;
            float       texScaleY   = 1;
            NTexture    mainTexture = null;
            AtlasSprite mainSprite  = null;

            if (ttf && _sprites.TryGetValue(item.id, out mainSprite))
            {
                mainTexture = (NTexture)GetItemAsset(mainSprite.atlas);
                texScaleX   = mainTexture.root.uvRect.width / mainTexture.width;
                texScaleY   = mainTexture.root.uvRect.height / mainTexture.height;
            }

            buffer.Seek(0, 1);

            BitmapFont.BMGlyph bg;
            int cnt = buffer.ReadInt();

            for (int i = 0; i < cnt; i++)
            {
                int nextPos = buffer.ReadShort();
                nextPos += buffer.position;

                bg = new BitmapFont.BMGlyph();
                char ch = buffer.ReadChar();
                font.AddChar(ch, bg);

                string img = buffer.ReadS();
                int    bx  = buffer.ReadInt();
                int    by  = buffer.ReadInt();
                bg.offsetX = buffer.ReadInt();
                bg.offsetY = buffer.ReadInt();
                bg.width   = buffer.ReadInt();
                bg.height  = buffer.ReadInt();
                bg.advance = buffer.ReadInt();
                bg.channel = buffer.ReadByte();
                if (bg.channel == 1)
                {
                    bg.channel = 3;
                }
                else if (bg.channel == 2)
                {
                    bg.channel = 2;
                }
                else if (bg.channel == 3)
                {
                    bg.channel = 1;
                }

                if (ttf)
                {
                    if (mainSprite.rotated)
                    {
                        bg.uv[0] = new Vector2((float)(by + bg.height + mainSprite.rect.x) * texScaleX,
                                               1 - (float)(mainSprite.rect.yMax - bx) * texScaleY);
                        bg.uv[1] = new Vector2(bg.uv[0].x - (float)bg.height * texScaleX, bg.uv[0].y);
                        bg.uv[2] = new Vector2(bg.uv[1].x, bg.uv[0].y + (float)bg.width * texScaleY);
                        bg.uv[3] = new Vector2(bg.uv[0].x, bg.uv[2].y);
                    }
                    else
                    {
                        bg.uv[0] = new Vector2((float)(bx + mainSprite.rect.x) * texScaleX,
                                               1 - (float)(by + bg.height + mainSprite.rect.y) * texScaleY);
                        bg.uv[1] = new Vector2(bg.uv[0].x, bg.uv[0].y + (float)bg.height * texScaleY);
                        bg.uv[2] = new Vector2(bg.uv[0].x + (float)bg.width * texScaleX, bg.uv[1].y);
                        bg.uv[3] = new Vector2(bg.uv[2].x, bg.uv[0].y);
                    }

                    bg.lineHeight = lineHeight;
                }
                else
                {
                    PackageItem charImg;
                    if (_itemsById.TryGetValue(img, out charImg))
                    {
                        GetItemAsset(charImg);
                        Rect uvRect = charImg.texture.uvRect;
                        bg.uv[0] = uvRect.position;
                        bg.uv[1] = new Vector2(uvRect.xMin, uvRect.yMax);
                        bg.uv[2] = new Vector2(uvRect.xMax, uvRect.yMax);
                        bg.uv[3] = new Vector2(uvRect.xMax, uvRect.yMin);
                        if (charImg.texture.rotated)
                        {
                            NGraphics.RotateUV(bg.uv, ref uvRect);
                        }
                        bg.width  = charImg.texture.width;
                        bg.height = charImg.texture.height;

                        if (mainTexture == null)
                        {
                            mainTexture = charImg.texture.root;
                        }
                    }

                    if (fontSize == 0)
                    {
                        fontSize = bg.height;
                    }

                    if (bg.advance == 0)
                    {
                        if (xadvance == 0)
                        {
                            bg.advance = bg.offsetX + bg.width;
                        }
                        else
                        {
                            bg.advance = xadvance;
                        }
                    }

                    bg.lineHeight = bg.offsetY < 0 ? bg.height : (bg.offsetY + bg.height);
                    if (bg.lineHeight < font.size)
                    {
                        bg.lineHeight = font.size;
                    }
                }

                buffer.position = nextPos;
            }

            font.size        = fontSize;
            font.mainTexture = mainTexture;
            if (!font.hasChannel)
            {
                font.shader = ShaderConfig.imageShader;
            }
        }
Example #52
0
        public object GetItemAsset(PackageItem item)
        {
            switch (item.type)
            {
                case PackageItemType.Image:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        AtlasSprite sprite;
                        if (_sprites.TryGetValue(item.id, out sprite))
                            item.texture = CreateSpriteTexture(sprite);
                        else
                            item.texture = NTexture.Empty;
                    }
                    return item.texture;

                case PackageItemType.Atlas:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        LoadAtlas(item);

                    }
                    return item.texture;

                case PackageItemType.Sound:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        LoadSound(item);
                    }
                    return item.audioClip;

                case PackageItemType.Font:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        LoadFont(item);
                    }
                    return item.bitmapFont;

                case PackageItemType.MovieClip:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        LoadMovieClip(item);
                    }
                    return item.frames;

                case PackageItemType.Component:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        LoadComponent(item);
                    }
                    if (!_loadingPackage && item.displayList == null)
                    {
                        LoadComponentChildren(item);
                        TranslateComponent(item);
                    }
                    return item.componentData;

                default:
                    if (!item.decoded)
                    {
                        item.decoded = true;
                        item.binary = LoadBinary(item.file);
                    }
                    return item.binary;
            }
        }
Example #53
0
        void LoadMovieClip(PackageItem item)
        {
            string str = GetDesc(item.id + ".xml");
            XML xml = new XML(str);
            string[] arr = null;

            arr = xml.GetAttributeArray("pivot");
            if (arr != null)
            {
                item.pivot.x = int.Parse(arr[0]);
                item.pivot.y = int.Parse(arr[1]);
            }
            str = xml.GetAttribute("interval");
            if (str != null)
                item.interval = float.Parse(str) / 1000f;
            item.swing = xml.GetAttributeBool("swing", false);
            str = xml.GetAttribute("repeatDelay");
            if (str != null)
                item.repeatDelay = float.Parse(str) / 1000f;
            int frameCount = xml.GetAttributeInt("frameCount");
            item.frames = new Frame[frameCount];

            XMLList frameNodes = xml.GetNode("frames").Elements();

            int i = 0;
            foreach (XML frameNode in frameNodes)
            {
                Frame frame = new Frame();
                arr = frameNode.GetAttributeArray("rect");
                frame.rect = new Rect(int.Parse(arr[0]), int.Parse(arr[1]), int.Parse(arr[2]), int.Parse(arr[3]));
                str = frameNode.GetAttribute("addDelay");
                if (str != null)
                    frame.addDelay = float.Parse(str) / 1000f;

                AtlasSprite sprite;
                if (_sprites.TryGetValue(item.id + "_" + i, out sprite))
                    frame.texture = CreateSpriteTexture(sprite);
                item.frames[i] = frame;
                i++;
            }
        }
Example #54
0
        void LoadFont(PackageItem item)
        {
            BitmapFont font = item.bitmapFont;

            string str = _descPack[item.id + ".fnt"];
            string[] arr = str.Split(sep1);
            int cnt = arr.Length;
            Dictionary<string, string> kv = new Dictionary<string, string>();
            NTexture mainTexture = null;
            Vector2 atlasOffset = new Vector2();
            bool ttf = false;
            int size = 0;
            int xadvance = 0;
            bool resizable = false;
            BitmapFont.BMGlyph bg = null;

            for (int i = 0; i < cnt; i++)
            {
                str = arr[i];
                if (str.Length == 0)
                    continue;

                str = str.Trim();

                string[] arr2 = str.Split(sep2, StringSplitOptions.RemoveEmptyEntries);
                for (int j = 1; j < arr2.Length; j++)
                {
                    string[] arr3 = arr2[j].Split(sep3, StringSplitOptions.RemoveEmptyEntries);
                    kv[arr3[0]] = arr3[1];
                }

                str = arr2[0];
                if (str == "char")
                {
                    bg = new BitmapFont.BMGlyph();
                    int bx = 0, by = 0;
                    if (kv.TryGetValue("x", out str))
                        bx = int.Parse(str);
                    if (kv.TryGetValue("y", out str))
                        by = int.Parse(str);
                    if (kv.TryGetValue("xoffset", out str))
                        bg.offsetX = int.Parse(str);
                    if (kv.TryGetValue("yoffset", out str))
                        bg.offsetY = int.Parse(str);
                    if (kv.TryGetValue("width", out str))
                        bg.width = int.Parse(str);
                    if (kv.TryGetValue("height", out str))
                        bg.height = int.Parse(str);
                    if (kv.TryGetValue("xadvance", out str))
                        bg.advance = int.Parse(str);
                    if (kv.TryGetValue("chnl", out str))
                    {
                        bg.channel = int.Parse(str);
                        if (bg.channel == 15)
                            bg.channel = 4;
                        else if (bg.channel == 1)
                            bg.channel = 3;
                        else if (bg.channel == 2)
                            bg.channel = 2;
                        else
                            bg.channel = 1;
                    }

                    if (!ttf)
                    {
                        if (kv.TryGetValue("img", out str))
                        {
                            PackageItem charImg;
                            if (_itemsById.TryGetValue(str, out charImg))
                            {
                                charImg.Load();
                                bg.uvRect = charImg.texture.uvRect;
                                if (mainTexture == null)
                                    mainTexture = charImg.texture.root;
                                bg.width = charImg.texture.width;
                                bg.height = charImg.texture.height;
                            }
                        }
                    }
                    else
                    {
                        Rect region = new Rect(bx + atlasOffset.x, by + atlasOffset.y, bg.width, bg.height);
                        bg.uvRect = new Rect(region.x / mainTexture.width, 1 - region.yMax / mainTexture.height,
                            region.width / mainTexture.width, region.height / mainTexture.height);
                    }

                    if (ttf)
                        bg.lineHeight = size;
                    else
                    {
                        if (bg.advance == 0)
                        {
                            if (xadvance == 0)
                                bg.advance = bg.offsetX + bg.width;
                            else
                                bg.advance = xadvance;
                        }

                        bg.lineHeight = bg.offsetY < 0 ? bg.height : (bg.offsetY + bg.height);
                        if (bg.lineHeight < size)
                            bg.lineHeight = size;
                    }

                    int ch = int.Parse(kv["id"]);
                    font.AddChar((char)ch, bg);
                }
                else if (str == "info")
                {
                    if (kv.TryGetValue("face", out str))
                    {
                        ttf = true;

                        AtlasSprite sprite;
                        if (_sprites.TryGetValue(item.id, out sprite))
                        {
                            atlasOffset = new Vector2(sprite.rect.x, sprite.rect.y);
                            PackageItem atlasItem = _itemsById[sprite.atlas];
                            mainTexture = (NTexture)GetItemAsset(atlasItem);
                        }
                    }
                    if (kv.TryGetValue("size", out str))
                        size = int.Parse(str);
                    if (kv.TryGetValue("resizable", out str))
                        resizable = str == "true";
                }
                else if (str == "common")
                {
                    if (size == 0 && kv.TryGetValue("lineHeight", out str))
                        size = int.Parse(str);
                    if (kv.TryGetValue("xadvance", out str))
                        xadvance = int.Parse(str);
                }
            }

            if (size == 0 && bg != null)
                size = bg.height;

            font.hasChannel = ttf;
            font.canTint = ttf;
            font.size = size;
            font.resizable = resizable;
            font.mainTexture = mainTexture;
            if (!ttf)
                font.shader = ShaderConfig.imageShader;
        }
Example #55
0
        internal GObject CreateObject(PackageItem item, System.Type userClass)
        {
            GObject g = null;
            if (item.type == PackageItemType.Component)
            {
                if (userClass != null)
                    g = (GComponent)userClass.Assembly.CreateInstance(userClass.FullName);
                else
                    g = UIObjectFactory.NewObject(item);
            }
            else
                g = UIObjectFactory.NewObject(item);

            if (g == null)
                return null;

            _constructing++;
            g.ConstructFromResource(item);
            _constructing--;
            return g;
        }
Example #56
0
        protected void LoadFromPackage(string itemURL)
        {
            _contentItem = UIPackage.GetItemByURL(itemURL);

            if (_contentItem != null)
            {
                _contentItem = _contentItem.getBranch();
                sourceWidth  = _contentItem.width;
                sourceHeight = _contentItem.height;
                _contentItem = _contentItem.getHighResolution();
                _contentItem.Load();

                if (_contentItem.type == PackageItemType.Image)
                {
                    _content.texture        = _contentItem.texture;
                    _content.textureScale   = new Vector2(_contentItem.width / (float)sourceWidth, _contentItem.height / (float)sourceHeight);
                    _content.scale9Grid     = _contentItem.scale9Grid;
                    _content.scaleByTile    = _contentItem.scaleByTile;
                    _content.tileGridIndice = _contentItem.tileGridIndice;

                    UpdateLayout();
                }
                else if (_contentItem.type == PackageItemType.MovieClip)
                {
                    _content.interval    = _contentItem.interval;
                    _content.swing       = _contentItem.swing;
                    _content.repeatDelay = _contentItem.repeatDelay;
                    _content.frames      = _contentItem.frames;

                    UpdateLayout();
                }
                else if (_contentItem.type == PackageItemType.Component)
                {
                    GObject obj = UIPackage.CreateObjectFromURL(itemURL);
                    if (obj == null)
                    {
                        SetErrorState();
                    }
                    else if (!(obj is GComponent))
                    {
                        obj.Dispose();
                        SetErrorState();
                    }
                    else
                    {
                        _content2 = (GComponent)obj;
                        ((Container)displayObject).AddChild(_content2.displayObject);
                        UpdateLayout();
                    }
                }
                else
                {
                    if (_autoSize)
                    {
                        this.SetSize(_contentItem.width, _contentItem.height);
                    }

                    SetErrorState();

                    Debug.LogWarning("Unsupported type of GLoader: " + _contentItem.type);
                }
            }
            else
            {
                SetErrorState();
            }
        }
Example #57
0
 static int ComparePackageItem(PackageItem p1, PackageItem p2)
 {
     if (p1.name != null && p2.name != null)
         return p1.name.CompareTo(p2.name);
     else
         return 0;
 }
Example #58
0
        void LoadFont(PackageItem item)
        {
            BitmapFont font = item.bitmapFont;

            string str = _descPack[item.id + ".fnt"];

            string[] arr = str.Split('\n');
            int      cnt = arr.Length;
            Dictionary <string, string> kv = new Dictionary <string, string>();
            NTexture mainTexture           = null;
            Vector2  atlasOffset           = new Vector2();
            bool     ttf       = false;
            int      size      = 0;
            int      xadvance  = 0;
            bool     resizable = false;
            bool     canTint   = false;

            BitmapFont.BMGlyph bg = null;

            char[] splitter0 = new char[] { ' ' };
            char[] splitter1 = new char[] { '=' };

            for (int i = 0; i < cnt; i++)
            {
                str = arr[i];
                if (str.Length == 0)
                {
                    continue;
                }

                str = str.Trim();

                string[] arr2 = str.Split(splitter0, StringSplitOptions.RemoveEmptyEntries);
                for (int j = 1; j < arr2.Length; j++)
                {
                    string[] arr3 = arr2[j].Split(splitter1, StringSplitOptions.RemoveEmptyEntries);
                    kv[arr3[0]] = arr3[1];
                }

                str = arr2[0];
                if (str == "char")
                {
                    bg = new BitmapFont.BMGlyph();
                    int bx = 0, by = 0;
                    if (kv.TryGetValue("x", out str))
                    {
                        bx = int.Parse(str);
                    }
                    if (kv.TryGetValue("y", out str))
                    {
                        by = int.Parse(str);
                    }
                    if (kv.TryGetValue("xoffset", out str))
                    {
                        bg.offsetX = int.Parse(str);
                    }
                    if (kv.TryGetValue("yoffset", out str))
                    {
                        bg.offsetY = int.Parse(str);
                    }
                    if (kv.TryGetValue("width", out str))
                    {
                        bg.width = int.Parse(str);
                    }
                    if (kv.TryGetValue("height", out str))
                    {
                        bg.height = int.Parse(str);
                    }
                    if (kv.TryGetValue("xadvance", out str))
                    {
                        bg.advance = int.Parse(str);
                    }
                    if (kv.TryGetValue("chnl", out str))
                    {
                        bg.channel = int.Parse(str);
                        if (bg.channel == 15)
                        {
                            bg.channel = 4;
                        }
                        else if (bg.channel == 1)
                        {
                            bg.channel = 3;
                        }
                        else if (bg.channel == 2)
                        {
                            bg.channel = 2;
                        }
                        else
                        {
                            bg.channel = 1;
                        }
                    }

                    if (!ttf)
                    {
                        if (kv.TryGetValue("img", out str))
                        {
                            PackageItem charImg;
                            if (_itemsById.TryGetValue(str, out charImg))
                            {
                                charImg.Load();
                                bg.uvRect = charImg.texture.uvRect;
                                if (mainTexture == null)
                                {
                                    mainTexture = charImg.texture.root;
                                }
                                bg.width  = charImg.texture.width;
                                bg.height = charImg.texture.height;
                            }
                        }
                    }
                    else
                    {
                        Rect region = new Rect(bx + atlasOffset.x, by + atlasOffset.y, bg.width, bg.height);
                        bg.uvRect = new Rect(region.x / mainTexture.width, 1 - region.yMax / mainTexture.height,
                                             region.width / mainTexture.width, region.height / mainTexture.height);
                    }

                    if (ttf)
                    {
                        bg.lineHeight = size;
                    }
                    else
                    {
                        if (bg.advance == 0)
                        {
                            if (xadvance == 0)
                            {
                                bg.advance = bg.offsetX + bg.width;
                            }
                            else
                            {
                                bg.advance = xadvance;
                            }
                        }

                        bg.lineHeight = bg.offsetY < 0 ? bg.height : (bg.offsetY + bg.height);
                        if (bg.lineHeight < size)
                        {
                            bg.lineHeight = size;
                        }
                    }

                    int ch = int.Parse(kv["id"]);
                    font.AddChar((char)ch, bg);
                }
                else if (str == "info")
                {
                    if (kv.TryGetValue("face", out str))
                    {
                        ttf     = true;
                        canTint = true;

                        AtlasSprite sprite;
                        if (_sprites.TryGetValue(item.id, out sprite))
                        {
                            atlasOffset = new Vector2(sprite.rect.x, sprite.rect.y);
                            PackageItem atlasItem = _itemsById[sprite.atlas];
                            mainTexture = (NTexture)GetItemAsset(atlasItem);
                        }
                    }
                    if (kv.TryGetValue("size", out str))
                    {
                        size = int.Parse(str);
                    }
                    if (kv.TryGetValue("resizable", out str))
                    {
                        resizable = str == "true";
                    }
                    if (kv.TryGetValue("colored", out str))
                    {
                        canTint = str == "true";
                    }
                }
                else if (str == "common")
                {
                    if (size == 0 && kv.TryGetValue("lineHeight", out str))
                    {
                        size = int.Parse(str);
                    }
                    if (kv.TryGetValue("xadvance", out str))
                    {
                        xadvance = int.Parse(str);
                    }
                }
            }

            if (size == 0 && bg != null)
            {
                size = bg.height;
            }

            font.hasChannel  = ttf;
            font.canTint     = canTint;
            font.size        = size;
            font.resizable   = resizable;
            font.mainTexture = mainTexture;
            if (!ttf)
            {
                font.shader = ShaderConfig.imageShader;
            }
        }
Example #59
0
        void LoadMovieClip(PackageItem item)
        {
            string str = _descPack[item.id + ".xml"];
            XML xml = new XML(str);
            string[] arr = null;

            str = xml.GetAttribute("interval");
            if (str != null)
                item.interval = float.Parse(str) / 1000f;
            item.swing = xml.GetAttributeBool("swing", false);
            str = xml.GetAttribute("repeatDelay");
            if (str != null)
                item.repeatDelay = float.Parse(str) / 1000f;
            int frameCount = xml.GetAttributeInt("frameCount");
            item.frames = new MovieClip.Frame[frameCount];

            XMLList frameNodes = xml.GetNode("frames").Elements();

            int i = 0;
            foreach (XML frameNode in frameNodes)
            {
                MovieClip.Frame frame = new MovieClip.Frame();
                arr = frameNode.GetAttributeArray("rect");
                frame.rect = new Rect(int.Parse(arr[0]), int.Parse(arr[1]), int.Parse(arr[2]), int.Parse(arr[3]));
                str = frameNode.GetAttribute("addDelay");
                if (str != null)
                    frame.addDelay = float.Parse(str) / 1000f;

                AtlasSprite sprite;
                if (_sprites.TryGetValue(item.id + "_" + i, out sprite))
                {
                    PackageItem atlasItem = _itemsById[sprite.atlas];
                    if (atlasItem != null)
                    {
                        if (item.texture == null)
                            item.texture = (NTexture)GetItemAsset(atlasItem);
                        frame.uvRect = new Rect(sprite.rect.x / item.texture.width * item.texture.uvRect.width,
                            1 - sprite.rect.yMax * item.texture.uvRect.height / item.texture.height,
                            sprite.rect.width * item.texture.uvRect.width / item.texture.width,
                            sprite.rect.height * item.texture.uvRect.height / item.texture.height);
                    }
                }
                item.frames[i] = frame;
                i++;
            }
        }
Example #60
0
        protected void LoadFromPackage(string itemURL)
        {
            _contentItem = UIPackage.GetItemByURL(itemURL);

            if (_contentItem != null)
            {
                _contentItem.Load();

                if (_contentItem.type == PackageItemType.Image)
                {
                    _content.texture        = _contentItem.texture;
                    _content.scale9Grid     = _contentItem.scale9Grid;
                    _content.scaleByTile    = _contentItem.scaleByTile;
                    _content.tileGridIndice = _contentItem.tileGridIndice;

                    _contentSourceWidth  = _contentItem.width;
                    _contentSourceHeight = _contentItem.height;
                    UpdateLayout();
                }
                else if (_contentItem.type == PackageItemType.MovieClip)
                {
                    _contentSourceWidth  = _contentItem.width;
                    _contentSourceHeight = _contentItem.height;

                    _content.interval    = _contentItem.interval;
                    _content.swing       = _contentItem.swing;
                    _content.repeatDelay = _contentItem.repeatDelay;
                    _content.SetData(_contentItem.texture, _contentItem.frames, new Rect(0, 0, _contentSourceWidth, _contentSourceHeight));

                    UpdateLayout();
                }
                else if (_contentItem.type == PackageItemType.Component)
                {
                    _contentSourceWidth  = _contentItem.width;
                    _contentSourceHeight = _contentItem.height;

                    GObject obj = UIPackage.CreateObjectFromURL(itemURL);
                    if (obj == null)
                    {
                        SetErrorState();
                    }
                    else if (!(obj is GComponent))
                    {
                        obj.Dispose();
                        SetErrorState();
                    }
                    else
                    {
                        _content2 = (GComponent)obj;
                        ((Container)displayObject).AddChild(_content2.displayObject);
                        UpdateLayout();
                    }
                }
                else
                {
                    if (_autoSize)
                    {
                        this.SetSize(_contentItem.width, _contentItem.height);
                    }

                    SetErrorState();
                }
            }
            else
            {
                SetErrorState();
            }
        }