void checkAspect(ref retinaProRuntimeScreen rs, float aspect, float deviceAspect, ref int i, ref int s, ref float bestAspectDiff, ref int bestIdx, ref int bestScreenIdx, ref int highestWidth)
    {
        float d = Mathf.Abs(aspect - deviceAspect);

        if (bestIdx == -1)
        {
            bestAspectDiff = d;
            bestIdx        = i;
            bestScreenIdx  = s;
            highestWidth   = rs.width;
        }
        else
        {
            if (d < bestAspectDiff && rs.width >= highestWidth)
            {
                bestAspectDiff = d;
                bestIdx        = i;
                bestScreenIdx  = s;
                highestWidth   = rs.width;
            }
        }
    }
Beispiel #2
0
    bool loadDevices(ref XMLNode nodes)
    {
        XMLNodeList nodeList = nodes.GetNodeList("retinaProData>0>ArrayOfRetinaProDevice>0>retinaProDevice");

        if (nodeList == null)
        {
            return(false);
        }

        foreach (XMLNode node in nodeList)
        {
            retinaProRuntimeDevice rd = new retinaProRuntimeDevice();

            foreach (DictionaryEntry pair in node)
            {
                if (pair.Key.ToString().CompareTo("name") == 0)
                {
                    XMLNodeList vs = (XMLNodeList)pair.Value;
                    foreach (XMLNode v in vs)
                    {
                        foreach (DictionaryEntry dp in v)
                        {
                            if (dp.Key.ToString().CompareTo("_text") == 0)
                            {
                                rd.name = dp.Value.ToString();
                            }
                        }
                    }
                }
                else if (pair.Key.ToString().CompareTo("pixelSize") == 0)
                {
                    XMLNodeList vs = (XMLNodeList)pair.Value;
                    foreach (XMLNode v in vs)
                    {
                        foreach (DictionaryEntry dp in v)
                        {
                            if (dp.Key.ToString().CompareTo("_text") == 0)
                            {
                                string str = dp.Value.ToString();
                                Single.TryParse(str, out rd.pixelSize);
                            }
                        }
                    }
                }
                else if (pair.Key.ToString().CompareTo("screens") == 0)
                {
                    XMLNodeList vs = (XMLNodeList)pair.Value;
                    foreach (XMLNode v in vs)
                    {
                        retinaProRuntimeScreen rs = new retinaProRuntimeScreen();

                        foreach (DictionaryEntry dp in v)
                        {
                            if (dp.Key.ToString().CompareTo("width") == 0)
                            {
                                XMLNodeList vs2 = (XMLNodeList)dp.Value;
                                foreach (XMLNode v2 in vs2)
                                {
                                    foreach (DictionaryEntry dp2 in v2)
                                    {
                                        if (dp2.Key.ToString().CompareTo("_text") == 0)
                                        {
                                            string str = dp2.Value.ToString();
                                            Int32.TryParse(str, out rs.width);
                                        }
                                    }
                                }
                            }
                            else if (dp.Key.ToString().CompareTo("height") == 0)
                            {
                                XMLNodeList vs2 = (XMLNodeList)dp.Value;
                                foreach (XMLNode v2 in vs2)
                                {
                                    foreach (DictionaryEntry dp2 in v2)
                                    {
                                        if (dp2.Key.ToString().CompareTo("_text") == 0)
                                        {
                                            string str = dp2.Value.ToString();
                                            Int32.TryParse(str, out rs.height);
                                        }
                                    }
                                }
                            }
                            else if (dp.Key.ToString().CompareTo("useForBothLandscapePortrait") == 0)
                            {
                                XMLNodeList vs2 = (XMLNodeList)dp.Value;
                                foreach (XMLNode v2 in vs2)
                                {
                                    foreach (DictionaryEntry dp2 in v2)
                                    {
                                        if (dp2.Key.ToString().CompareTo("_text") == 0)
                                        {
                                            string str = dp2.Value.ToString();
                                            Boolean.TryParse(str, out rs.useForBothLandscapePortrait);
                                        }
                                    }
                                }
                            }
                        }

                        rd.screens.Add(rs);
                    }
                }
                else if (pair.Key.ToString().CompareTo("rootWidth") == 0)
                {
                    XMLNodeList vs = (XMLNodeList)pair.Value;
                    foreach (XMLNode v in vs)
                    {
                        foreach (DictionaryEntry dp in v)
                        {
                            if (dp.Key.ToString().CompareTo("_text") == 0)
                            {
                                string str = dp.Value.ToString();

                                Int32.TryParse(str, out rd.rootWidth);
                            }
                        }
                    }
                }
                else if (pair.Key.ToString().CompareTo("rootHeight") == 0)
                {
                    XMLNodeList vs = (XMLNodeList)pair.Value;
                    foreach (XMLNode v in vs)
                    {
                        foreach (DictionaryEntry dp in v)
                        {
                            if (dp.Key.ToString().CompareTo("_text") == 0)
                            {
                                string str = dp.Value.ToString();

                                Int32.TryParse(str, out rd.rootHeight);
                            }
                        }
                    }
                }
                else if (pair.Key.ToString().CompareTo("rootAuto") == 0)
                {
                    XMLNodeList vs = (XMLNodeList)pair.Value;
                    foreach (XMLNode v in vs)
                    {
                        foreach (DictionaryEntry dp in v)
                        {
                            if (dp.Key.ToString().CompareTo("_text") == 0)
                            {
                                string str = dp.Value.ToString();

                                bool.TryParse(str, out rd.rootAuto);
                            }
                        }
                    }
                }
                else if (pair.Key.ToString().CompareTo("rootUseBothPortLand") == 0)
                {
                    XMLNodeList vs = (XMLNodeList)pair.Value;
                    foreach (XMLNode v in vs)
                    {
                        foreach (DictionaryEntry dp in v)
                        {
                            if (dp.Key.ToString().CompareTo("_text") == 0)
                            {
                                string str = dp.Value.ToString();

                                bool.TryParse(str, out rd.rootUseBothPortLand);
                            }
                        }
                    }
                }
            }

            // add one to the device list
            deviceList.Add(rd);
#if RETINAPRO_DEBUGLOG
            {
                string debug = "";

                debug += "RetinaPro Device = " + rd.name + ", ";
                debug += "pixelSize = " + rd.pixelSize + ", ";
                if (rd.rootAuto)
                {
                    debug += "root = Auto, ";
                }
                else
                {
                    debug += "root (" + rd.rootWidth + " x " + rd.rootHeight + "), Port & Land = " + rd.rootUseBothPortLand + ", ";
                }

                for (int si = 0; si < rd.screens.Count; si++)
                {
                    debug += "screen[" + si + "] = { " + rd.screens[si].width + " X " + rd.screens[si].height + ", Port & Land = " + rd.screens[si].useForBothLandscapePortrait + " }";
                    if (si != rd.screens.Count - 1)
                    {
                        debug += ", ";
                    }
                }

                Debug.Log(debug);
            }
#endif
        }

        return(true);
    }
    public void refreshActiveDevice()
    {
        activeDeviceIdx = -1;
        activeScreenIdx = -1;

        float aspect = ((float)Screen.width) / ((float)Screen.height);

#if RETINAPRO_ATLASCONTROLLER_DEBUGLOG
        Debug.Log("Screen Size (" + Screen.width + ", " + Screen.height + ")");
        Debug.Log("Screen aspect: " + aspect);
#endif

        // what resolution are we running at
        // compare against our devices

        for (int i = 0; i < rt.deviceList.Count; i++)
        {
            retinaProRuntimeDevice dd = rt.deviceList[i];

            for (int s = 0; s < dd.screens.Count; s++)
            {
                retinaProRuntimeScreen rs = dd.screens[s];

                if (rs.useForBothLandscapePortrait)
                {
                    if (
                        (rs.width == Screen.width && rs.height == Screen.height) ||
                        (rs.width == Screen.height && rs.height == Screen.width)
                        )
                    {
                        activeDeviceIdx = i;
                        activeScreenIdx = s;
                        break;
                    }
                }
                else
                {
                    if (rs.width == Screen.width && rs.height == Screen.height)
                    {
                        activeDeviceIdx = i;
                        activeScreenIdx = s;
                        break;
                    }
                }
            }
        }

        if (activeDeviceIdx == -1)
        {
            // we did not find any devices that match the current screen resolution
            Debug.LogWarning("Did not find any devices that match the current resolution");

            int   bestIdx        = -1;
            int   bestScreenIdx  = -1;
            float bestAspectDiff = 0.0f;
            int   highestWidth   = 0;

            // check the aspect ratio and find the closest and highest resolution that matches that aspect ratio
            for (int i = 0; i < rt.deviceList.Count; i++)
            {
                retinaProRuntimeDevice dd = rt.deviceList[i];

                for (int s = 0; s < dd.screens.Count; s++)
                {
                    retinaProRuntimeScreen rs = dd.screens[s];

                    float deviceAspect = ((float)rs.width) / ((float)rs.height);
                    checkAspect(ref rs, aspect, deviceAspect, ref i, ref s, ref bestAspectDiff, ref bestIdx, ref bestScreenIdx, ref highestWidth);

                    if (rs.useForBothLandscapePortrait)
                    {
                        deviceAspect = ((float)rs.height) / ((float)rs.width);
                        checkAspect(ref rs, aspect, deviceAspect, ref i, ref s, ref bestAspectDiff, ref bestIdx, ref bestScreenIdx, ref highestWidth);
                    }
                }
            }

            activeDeviceIdx = bestIdx;
            activeScreenIdx = bestScreenIdx;
        }

        if (activeDeviceIdx != -1)
        {
            Debug.Log("Using device: " + rt.deviceList[activeDeviceIdx].name + ", pixel size = " + rt.deviceList[activeDeviceIdx].pixelSize);

            if (activeScreenIdx != -1)
            {
                Debug.Log("Using screen: " +
                          rt.deviceList[activeDeviceIdx].screens[activeScreenIdx].width + " X " +
                          rt.deviceList[activeDeviceIdx].screens[activeScreenIdx].height + ", Port & Land = " +
                          rt.deviceList[activeDeviceIdx].screens[activeScreenIdx].useForBothLandscapePortrait);
            }
        }
    }