1- using System ;
1+ using KSP . UI . Screens ;
2+ using System ;
23using UnityEngine ;
3- using KSP . UI . Screens ;
44
55namespace EditorExtensionsRedux
66{
7- [ KSPAddon ( KSPAddon . Startup . EditorAny , false ) ]
8- public class AppLauncherButton : MonoBehaviour
9- {
10- private ApplicationLauncherButton button = null ;
11-
12- public static AppLauncherButton Instance ;
13-
14- const string texPathDefault = "EditorExtensionsRedux/Textures/AppLauncherIcon" ;
15- const string texPathOn = "EditorExtensionsRedux/Textures/AppLauncherIcon-On" ;
16- const string texPathOff = "EditorExtensionsRedux/Textures/AppLauncherIcon-Off" ;
17-
18- private void Start ( )
19- {
20- if ( button == null ) {
21- OnGuiAppLauncherReady ( ) ;
22- }
23- }
24-
25- private void Awake ( )
26- {
27- if ( AppLauncherButton . Instance == null ) {
28- GameEvents . onGUIApplicationLauncherReady . Add ( this . OnGuiAppLauncherReady ) ;
29- Instance = this ;
30- }
31- }
32-
33- private void OnDestroy ( )
34- {
35- GameEvents . onGUIApplicationLauncherReady . Remove ( this . OnGuiAppLauncherReady ) ;
36- if ( this . button != null ) {
37- ApplicationLauncher . Instance . RemoveModApplication ( this . button ) ;
38- }
39- }
40-
41- private void ButtonState ( bool state )
42- {
43- Log . Debug ( "ApplicationLauncher on" + state . ToString ( ) ) ;
44- EditorExtensions . Instance . Visible = state ;
45- }
7+ [ KSPAddon ( KSPAddon . Startup . EditorAny , false ) ]
8+ public class AppLauncherButton : MonoBehaviour
9+ {
10+ private ApplicationLauncherButton button = null ;
11+
12+ public static AppLauncherButton Instance ;
13+
14+ const string texPathDefault = "EditorExtensionsRedux/Textures/AppLauncherIcon" ;
15+ const string texPathOn = "EditorExtensionsRedux/Textures/AppLauncherIcon-On" ;
16+ const string texPathOff = "EditorExtensionsRedux/Textures/AppLauncherIcon-Off" ;
17+
18+ private void Start ( )
19+ {
20+ if ( button == null )
21+ {
22+ OnGuiAppLauncherReady ( ) ;
23+ }
24+ }
25+
26+ private void Awake ( )
27+ {
28+ if ( AppLauncherButton . Instance == null )
29+ {
30+ GameEvents . onGUIApplicationLauncherReady . Add ( this . OnGuiAppLauncherReady ) ;
31+ Instance = this ;
32+ }
33+ }
34+
35+ private void OnDestroy ( )
36+ {
37+ GameEvents . onGUIApplicationLauncherReady . Remove ( this . OnGuiAppLauncherReady ) ;
38+ if ( this . button != null )
39+ {
40+ ApplicationLauncher . Instance . RemoveModApplication ( this . button ) ;
41+ }
42+ }
43+
44+ private void ButtonState ( bool state )
45+ {
46+ Log . Debug ( "ApplicationLauncher on" + state . ToString ( ) ) ;
47+ EditorExtensions . Instance . Visible = state ;
48+ }
4649 /// <summary>
4750 /// Copied from ToolbarController
4851 /// </summary>
4952 ///
50- // // The following function was initially copied from @JPLRepo's AmpYear mod, which is covered by the GPL, as is this mod // // This function will attempt to load either a PNG or a JPG from the specified path. // It first checks to see if the actual file is there, if not, it then looks for either a PNG or a JPG // // easier to specify different cases than to change case to lower. This will fail on MacOS and Linux // if a suffix has mixed case
51- static string [ ] imgSuffixes = new string [ ] { ".png" , ".jpg" , ".gif" , ".PNG" , ".JPG" , ".GIF" } ; public static Boolean LoadImageFromFile ( ref Texture2D tex , String fileNamePath ) { Boolean blnReturn = false ; try { string path = fileNamePath ; if ( ! System . IO . File . Exists ( fileNamePath ) ) { // Look for the file with an appended suffix. for ( int i = 0 ; i < imgSuffixes . Length ; i ++ ) if ( System . IO . File . Exists ( fileNamePath + imgSuffixes [ i ] ) ) { path = fileNamePath + imgSuffixes [ i ] ; break ; } }
53+ //
54+ // The following function was initially copied from @JPLRepo's AmpYear mod, which is covered by the GPL, as is this mod
55+ //
56+ // This function will attempt to load either a PNG or a JPG from the specified path.
57+ // It first checks to see if the actual file is there, if not, it then looks for either a PNG or a JPG
58+ //
59+ // easier to specify different cases than to change case to lower. This will fail on MacOS and Linux
60+ // if a suffix has mixed case
61+
62+ #region NO_LOCALIZATION
63+
64+ static string [ ] imgSuffixes = new string [ ] { ".png" , ".jpg" , ".gif" , ".PNG" , ".JPG" , ".GIF" } ;
65+
66+ #endregion
67+ public static Boolean LoadImageFromFile ( ref Texture2D tex , String fileNamePath ) { Boolean blnReturn = false ; try { string path = fileNamePath ; if ( ! System . IO . File . Exists ( fileNamePath ) ) {
68+ // Look for the file with an appended suffix.
69+ for ( int i = 0 ; i < imgSuffixes . Length ; i ++ ) if ( System . IO . File . Exists ( fileNamePath + imgSuffixes [ i ] ) ) { path = fileNamePath + imgSuffixes [ i ] ; break ; } }
5270 //File Exists check
5371 if ( System . IO . File . Exists ( path ) ) { try { tex . LoadImage ( System . IO . File . ReadAllBytes ( path ) ) ; blnReturn = true ; } catch ( Exception ex ) { Log . Error ( "Failed to load the texture:" + path ) ; Log . Error ( ex . Message ) ; } } else { Log . Error ( "Cannot find texture to load:" + fileNamePath ) ; } } catch ( Exception ex ) { Log . Error ( "Failed to load (are you missing a file):" + fileNamePath ) ; Log . Error ( ex . Message ) ; } return blnReturn ; }
5472 Texture2D GetTexture ( string path , bool b ) { Texture2D tex = new Texture2D ( 16 , 16 , TextureFormat . ARGB32 , false ) ;
5573 if ( LoadImageFromFile ( ref tex , KSPUtil . ApplicationRootPath + "GameData/" + path ) ) return tex ; return tex ; }
56- /// <summary>
57- /// End of copy
58- /// </summary>
59-
60- private void OnGuiAppLauncherReady ( )
61- {
62- if ( this . button == null ) {
63- try {
64- this . button = ApplicationLauncher . Instance . AddModApplication (
65- ( ) => {
66- EditorExtensions . Instance . Show ( ) ;
67- } , //RUIToggleButton.onTrue
68- ( ) => {
69- EditorExtensions . Instance . Hide ( ) ;
70- } , //RUIToggleButton.onFalse
71- ( ) => {
72- EditorExtensions . Instance . ShowMenu ( ) ;
73- } , //RUIToggleButton.OnHover
74- ( ) => {
75- EditorExtensions . Instance . HideMenu ( ) ;
76- } , //RUIToggleButton.onHoverOut
77- null , //RUIToggleButton.onEnable
78- null , //RUIToggleButton.onDisable
79- ApplicationLauncher . AppScenes . VAB | ApplicationLauncher . AppScenes . SPH , //visibleInScenes
80- GetTexture ( texPathDefault , false ) //texture
81- ) ;
82- Log . Debug ( "Added ApplicationLauncher button" ) ;
83- } catch ( Exception ex ) {
84- Log . Error ( "Error adding ApplicationLauncher button: " + ex . Message ) ;
85- }
86- }
87-
88- }
89-
90- private void Update ( )
91- {
92- if ( this . button == null ) {
93- return ;
94- }
95-
96- // if(this.button.State != RUIToggleButton.ButtonState.TRUE)
97- // this.button.SetTexture(GameDatabase.Instance.GetTexture (texPathOn, false));
98- // else
99- // this.button.SetTexture(GameDatabase.Instance.GetTexture (texPathOff, false));
100-
101- try {
102- if ( EditorLogic . fetch != null ) {
103- // if (EditorExtensions.Instance.Visible && this.button.State != RUIToggleButton.ButtonState.TRUE) {
104- if ( EditorExtensions . Instance . Visible && ! this . button . enabled ) {
105- this . button . SetTrue ( ) ;
106- //this.button.SetTexture(GameDatabase.Instance.GetTexture (texPathOn, false));
107- // } else if (!EditorExtensions.Instance.Visible && this.button.State != RUIToggleButton.ButtonState.FALSE) {
108- } else if ( ! EditorExtensions . Instance . Visible && this . button . enabled ) {
109- this . button . SetFalse ( ) ;
110- //this.button.SetTexture(GameDatabase.Instance.GetTexture (texPathOff, false));
111- }
112- // } else if (this.button.State != RUIToggleButton.ButtonState.DISABLED) {
113- } else if ( this . button . enabled ) {
114- this . button . Disable ( ) ;
115- }
116- } catch ( Exception ex ) {
117- Log . Error ( "Error updating ApplicationLauncher button: " + ex . Message ) ;
118- }
119- }
120- }
74+ /// <summary>
75+ /// End of copy
76+ /// </summary>
77+
78+ #region NO_LOCALIZATION
79+
80+ private void OnGuiAppLauncherReady ( )
81+ {
82+ if ( this . button == null )
83+ {
84+ try
85+ {
86+ this . button = ApplicationLauncher . Instance . AddModApplication (
87+ ( ) =>
88+ {
89+ EditorExtensions . Instance . Show ( ) ;
90+ } , //RUIToggleButton.onTrue
91+ ( ) =>
92+ {
93+ EditorExtensions . Instance . Hide ( ) ;
94+ } , //RUIToggleButton.onFalse
95+ ( ) =>
96+ {
97+ EditorExtensions . Instance . ShowMenu ( ) ;
98+ } , //RUIToggleButton.OnHover
99+ ( ) =>
100+ {
101+ EditorExtensions . Instance . HideMenu ( ) ;
102+ } , //RUIToggleButton.onHoverOut
103+ null , //RUIToggleButton.onEnable
104+ null , //RUIToggleButton.onDisable
105+ ApplicationLauncher . AppScenes . VAB | ApplicationLauncher . AppScenes . SPH , //visibleInScenes
106+ GetTexture ( texPathDefault , false ) //texture
107+ ) ;
108+ Log . Debug ( "Added ApplicationLauncher button" ) ;
109+ }
110+ catch ( Exception ex )
111+ {
112+ Log . Error ( "Error adding ApplicationLauncher button: " + ex . Message ) ;
113+ }
114+ }
115+
116+ }
117+
118+ #endregion
119+
120+ private void Update ( )
121+ {
122+ if ( this . button == null )
123+ {
124+ return ;
125+ }
126+
127+ // if(this.button.State != RUIToggleButton.ButtonState.TRUE)
128+ // this.button.SetTexture(GameDatabase.Instance.GetTexture (texPathOn, false));
129+ // else
130+ // this.button.SetTexture(GameDatabase.Instance.GetTexture (texPathOff, false));
131+
132+ try
133+ {
134+ if ( EditorLogic . fetch != null )
135+ {
136+ // if (EditorExtensions.Instance.Visible && this.button.State != RUIToggleButton.ButtonState.TRUE) {
137+ if ( EditorExtensions . Instance . Visible && ! this . button . enabled )
138+ {
139+ this . button . SetTrue ( ) ;
140+ //this.button.SetTexture(GameDatabase.Instance.GetTexture (texPathOn, false));
141+ // } else if (!EditorExtensions.Instance.Visible && this.button.State != RUIToggleButton.ButtonState.FALSE) {
142+ }
143+ else if ( ! EditorExtensions . Instance . Visible && this . button . enabled )
144+ {
145+ this . button . SetFalse ( ) ;
146+ //this.button.SetTexture(GameDatabase.Instance.GetTexture (texPathOff, false));
147+ }
148+ // } else if (this.button.State != RUIToggleButton.ButtonState.DISABLED) {
149+ }
150+ else if ( this . button . enabled )
151+ {
152+ this . button . Disable ( ) ;
153+ }
154+ }
155+ catch ( Exception ex )
156+ {
157+ Log . Error ( "Error updating ApplicationLauncher button: " + ex . Message ) ;
158+ }
159+ }
160+ }
121161}
0 commit comments