Un panneau d'instrument simple

Pour populariser ou demander de l'aide pour vos propres add-ons!
Avatar de l’utilisateur
Granolat
Messages : 607
Inscription : 30 janvier 2013, 09:04
Contact :

Re: Un panneau d'instrument simple

Message par Granolat » 29 avril 2016, 17:15

Bon, jusqu'ici tout allait bien.

Et puis la 1.1.1.1.1.1.1 est arrivée. Et bien sûr, mon mod ne fonctionne plus.
Voici le message d'erreur de la console :

[Exception]: TypeLoadException: Could not load type 'Callback' from assembly 'PappoloPanel'.

Évidemment, je n'y comprends que pouik. Voici le code complet du mod :
PRIME_BBCODE_SPOILER_SHOW PRIME_BBCODE_SPOILER:

Code : Tout sélectionner

using UnityEngine;
using System.Collections;

namespace PappoloInstrumentPanel
{
    [KSPAddon(KSPAddon.Startup.Flight, false)]
    public class PappoloPanel : MonoBehaviour
    {
// Variables------------------------------------------------------------------
        public Vessel myship;
        public float radalt;
        public double altitude;
        public long apoapsis, periapsis;
        public string strNorthing, strEasting;
        public static int LineHeight = 25;
        public static int lcdXpos = 53, lcdYpos = 17; // initial position of LCD digits
        public static int lightXpos = 8, lightYpos = 15; // initial position of LCD digits
        public int iconXpos, iconYpos;

        private bool activePanel;
        private static string iconPath = "GameData/PappoloPanel/Plugins/PluginData/PappoloPanel/";
        private static string root = KSPUtil.ApplicationRootPath.Replace("\\", "/"); // get game root directory
        private static Rect windowPosition;
        private static GUIStyle
            iconStyle = new GUIStyle();
//            windowStyle = null;
        private KSP.IO.PluginConfiguration cfg = KSP.IO.PluginConfiguration.CreateForType<PappoloPanel>(); // access to config.xml

        public static Texture2D
            iconOn = new Texture2D(32, 32, TextureFormat.ARGB32, false),
            iconOff = new Texture2D(32, 32, TextureFormat.ARGB32, false),
            panelIcon = new Texture2D(32, 32, TextureFormat.ARGB32, false),
            panelBackGround = new Texture2D(152, 150, TextureFormat.ARGB32, false),
            blankCharacter = new Texture2D(10, 14, TextureFormat.ARGB32, false),
            minusCharacter = new Texture2D(10, 14, TextureFormat.ARGB32, false),
            dotCharacter = new Texture2D(10, 14, TextureFormat.ARGB32, false),
            radarText = new Texture2D(33, 10, TextureFormat.ARGB32, false);
        public Texture2D[] fontImages = new Texture2D[10]; // will contain all digits
        public Texture2D[] lightImages = new Texture2D[5];

//------------------------------------------------------------------
        public void GetCoordinates(Vessel vessel)
        {
            double longitude = vessel.longitude;
            double latitude = vessel.latitude;
            if (longitude > 180) longitude -= 360;
            if (longitude < -180) longitude += 360;
            this.strNorthing = (latitude).ToString("0.0000");
            this.strEasting = (longitude).ToString("0.0000");
        }

//------------------------------------------------------------------
        public void LoadIcons()
        {
            var imageLoaderOn = new WWW("file://" + root + iconPath + "pappolopanel_on.png");
            var imageLoaderOff = new WWW("file://" + root + iconPath + "pappolopanel_off.png");
            var imageLoaderPanel = new WWW("file://" + root + iconPath + "background_panel.png");
            var imageBlank = new WWW("file://" + root + iconPath + "blank.png");
            var imageMinus = new WWW("file://" + root + iconPath + "minus.png");
            var imageDot = new WWW("file://" + root + iconPath + "dot.png");
            var imageRadar = new WWW("file://" + root + iconPath + "radar.png");
            imageLoaderOn.LoadImageIntoTexture(iconOn);
            imageLoaderOff.LoadImageIntoTexture(iconOff);
            imageLoaderPanel.LoadImageIntoTexture(panelBackGround);
            imageBlank.LoadImageIntoTexture(blankCharacter);
            imageMinus.LoadImageIntoTexture(minusCharacter);
            imageDot.LoadImageIntoTexture(dotCharacter);
            imageRadar.LoadImageIntoTexture(radarText);
            panelIcon = iconOn;
            for (int i = 0; i < fontImages.Length; i++)
            {
                Texture2D tex = new Texture2D(10, 14, TextureFormat.ARGB32, false);
                var imageLoader = new WWW("file://" + root + iconPath + i + ".png");
                imageLoader.LoadImageIntoTexture(tex);
                fontImages[i] = tex;
            }
            for (int i = 0; i < lightImages.Length; i++)
            {
                Texture2D tex = new Texture2D(31, 18, TextureFormat.ARGB32, false);
                var imageLoader = new WWW("file://" + root + iconPath + "light_on_" + i + ".png");
                imageLoader.LoadImageIntoTexture(tex);
                lightImages[i] = tex;
            }
        }

//------------------------------------------------------------------
        public void MakeDigit(string strValue, int panelLine) // Transform numeric string to image with LCD font at dedicated line
        {
            int nbChar = (strValue.ToString()).Length;
            int shift = 9 - nbChar; // the number of blank characters needed to align right
            for (int i = 0; i < shift; i++)
            {
                GUI.DrawTexture(new Rect(lcdXpos + i * 9, lcdYpos + panelLine * LineHeight, 9, 14), blankCharacter); // insert blank characters
            }
            for (int i = 0; i < (strValue.ToString()).Length; i++)
            {
                string strChar = strValue.Substring(i, 1); // extract the character to transform
                if (strChar == "-")
                    GUI.DrawTexture(new Rect(lcdXpos + (i + shift) * 9, lcdYpos + panelLine * LineHeight, 9, 14), minusCharacter);
                else if (strChar == ".")
                    GUI.DrawTexture(new Rect(lcdXpos + (i + shift) * 9, lcdYpos + panelLine * LineHeight, 9, 14), dotCharacter);
                else
                {
                    GUI.DrawTexture(new Rect(lcdXpos + (i + shift) * 9, lcdYpos + panelLine * LineHeight, 9, 14), fontImages[int.Parse(strChar)]);
                }
            }
        }

//------------------------------------------------------------------
        public void loadConfig()
        {
            cfg.load();
            iconXpos = cfg.GetValue<int>("iconxpos");
            iconYpos = cfg.GetValue<int>("iconypos");
            windowPosition = new Rect(cfg.GetValue<int>("panelxpos"), cfg.GetValue<int>("panelypos"), 152, 150);
            activePanel = cfg.GetValue<bool>("panelOn");
        }

        public void saveConfig()
        {
            cfg.SetValue("panelxpos", (int)windowPosition.xMin);
            cfg.SetValue("panelypos", (int)windowPosition.yMin);
            cfg.SetValue("panelOn", activePanel);
            cfg.save();
        }

//--------------------------------------------------------------
        public void Awake()
        {
            RenderingManager.AddToPostDrawQueue(0, OnDraw);
        }

//------------------------------------------------------------------
        public void Start()
        {
            LoadIcons();
            loadConfig();
//            windowPosition = new Rect(182, 1, 152, 150); // Instrument Panel position & size
        }

//------------------------------------------------------------------
        public void Update()
        {
            if (activePanel == true)
            {
                panelIcon = iconOn;
            }
            else
            {
                panelIcon = iconOff;
            }
        }

//------------------------------------------------------------------
        private void OnDraw()
        {
            if (GUI.Button(new Rect(iconXpos, iconYpos, 32, 32), panelIcon, iconStyle))
            {
                if (activePanel == true)
                {
                    activePanel = false;
                }
                else
                {
                    activePanel = true;
                }
            }
            if (activePanel)
            {
//                windowPosition = GUI.Window(1234, windowPosition, OnWindow, "Pappolo Panel", windowStyle);
                windowPosition = GUI.Window(1234, windowPosition, OnWindow, panelBackGround, iconStyle);
            }
            myship = FlightGlobals.ActiveVessel;
            radalt = myship.GetHeightFromTerrain();
            altitude = myship.altitude;
            apoapsis = (long)myship.orbit.ApA;
            apoapsis = (long)(Mathf.Round(apoapsis / 10)) * 10;
            periapsis = (long)myship.orbit.PeA;
            periapsis = (long)(Mathf.Round(periapsis / 10)) * 10;
            GetCoordinates(myship);
            saveConfig();
        }

//------------------------------------------------------------------
        private void OnWindow(int windowID)
        {
            if (radalt < 5 || myship.Landed) // When sensor is too high to get altitude or landed
            {
                MakeDigit("--", 0);
            }
            else
            {
                GUI.DrawTexture(new Rect(52, 17, 33, 10), radarText); // add the "RADAR" icon
                GUI.DrawTexture(new Rect(lightXpos, lightYpos, 31, 18), lightImages[0]);
                if (radalt >= 200 && radalt < 5000)
                {
                    radalt = (Mathf.Round(radalt / 10)) * 10; // increment altitude by 10
                }
                if (radalt >= 5000)
                {
                    radalt = (Mathf.Round(radalt / 100)) * 100; // increment altitude by 100
                }
                MakeDigit(((int)radalt).ToString(), 0);
            }

            if (altitude > 1000000)
            {
                MakeDigit("--", 1);
                MakeDigit("--", 2);
            }
            else
            {
                MakeDigit(strNorthing, 1);
                MakeDigit(strEasting, 2);
                GUI.DrawTexture(new Rect(lightXpos, lightYpos + LineHeight, 31, 18), lightImages[1]);
                GUI.DrawTexture(new Rect(lightXpos, lightYpos + LineHeight * 2, 31, 18), lightImages[2]);
            }

            if (apoapsis < 3000 || apoapsis > 99999999)
                MakeDigit("--", 3);
            else
            {
                MakeDigit(apoapsis.ToString(), 3);
                GUI.DrawTexture(new Rect(lightXpos, lightYpos + LineHeight * 3, 31, 18), lightImages[3]);
            }

            if (periapsis < 0 || periapsis > 99999999)
                MakeDigit("--", 4); // Periapsis = null id under zero
            else
            {
                MakeDigit(periapsis.ToString(), 4);
                GUI.DrawTexture(new Rect(lightXpos, lightYpos + LineHeight * 4, 31, 18), lightImages[4]);
            }

            GUI.DragWindow();
        }
    }
}

Si quelqu'un avait la moindre idée...

Akinatronic
Messages : 1710
Inscription : 24 décembre 2014, 17:55
Localisation : Dans les étoiles
Contact :

Re: Un panneau d'instrument simple

Message par Akinatronic » 29 avril 2016, 17:25

Il est de retour !
Je crois que tu dois le recompiler sous Unity 5, mais Malah t'aidera sûrement plus que moi :)

Avatar de l’utilisateur
Granolat
Messages : 607
Inscription : 30 janvier 2013, 09:04
Contact :

Re: Un panneau d'instrument simple

Message par Granolat » 29 avril 2016, 17:51

Je n'ai jamais été très loin. Disons que j'attendais la 1.1 pour m'y remettre sérieusement. ;)

Je vais déjà tester la recompil'

Edit : en fait, l'erreur est là.

Code : Tout sélectionner

        public void Awake()
        {
            RenderingManager.AddToPostDrawQueue(0, OnDraw);
        }
Image

Avatar de l’utilisateur
Malah
Messages : 1488
Inscription : 02 novembre 2014, 02:18
Localisation : Rennes
Contact :

Re: Un panneau d'instrument simple

Message par Malah » 29 avril 2016, 19:21

Salut, oui, c'est l'un des changements de la 1.1, l'utilisation de RenderingManager c'est fini, il faut obligatoirement passer par le void OnGUI() (on peut aussi passer par des prefab créé avec l'éditeur d'Unity, mais là je n'ai pas tout saisis et comme on ne peut pas réutiliser l'interface de KSP avec cette méthode, je ne vais pas me documenter dessus).
Si tu veux plus d'info : http://forum.kerbalspaceprogram.com/ind ... to-ksp-11/

Si tu cherches un exemple : https://github.com/malahx/QuickMute/blo ... cs#L69-L74 c'est banal ;)

Avatar de l’utilisateur
Granolat
Messages : 607
Inscription : 30 janvier 2013, 09:04
Contact :

Re: Un panneau d'instrument simple

Message par Granolat » 29 avril 2016, 19:43

Je vais regarder ça. J'ai commencé à créer un projet perso sur Unity 5 (rien à voir avec KSP), les arcanes de Unity 4 me paraissent bien compliquées. :mrgreen:

Merci, m'sieur !

Avatar de l’utilisateur
Granolat
Messages : 607
Inscription : 30 janvier 2013, 09:04
Contact :

Re: Un panneau d'instrument simple

Message par Granolat » 29 avril 2016, 20:23

Bon, j'ai fait une première correction, ça compile, mais ça ce marche pas. Je prendrais le temps de regarder de plus près demain !

Avatar de l’utilisateur
Granolat
Messages : 607
Inscription : 30 janvier 2013, 09:04
Contact :

Re: Un panneau d'instrument simple

Message par Granolat » 12 octobre 2016, 14:50

Bon, avec l'arrivée de la 1.2, et mon envie irrésistible de m'y remettre, je remets les mains dans le cambouis pour mettre à jour mon petit mod, quitte à repartir d'une feuille blanche.

Parce-que mine de rien, il est super pratique !

Avatar de l’utilisateur
Granolat
Messages : 607
Inscription : 30 janvier 2013, 09:04
Contact :

Re: Un panneau d'instrument simple

Message par Granolat » 12 octobre 2016, 23:36

Aha, ça remarche ! :D
Je vous refais deux trois modifs et je vous prépare un petit package. :P

Répondre

Qui est en ligne ?

Utilisateurs parcourant ce forum : Aucun utilisateur inscrit