-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawLayerMod.cs
More file actions
175 lines (142 loc) · 6.48 KB
/
DrawLayerMod.cs
File metadata and controls
175 lines (142 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using KSP.UI.Screens;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace com.github.lhervier.ksp {
[KSPAddon(KSPAddon.Startup.PSystemSpawn, true)]
public class DrawLayerMod : MonoBehaviour {
// Composants du mod
private UIManager uiManager;
private MarkerRenderer markerRenderer;
private ConfigManager configManager;
// Application launcher
private ApplicationLauncherButton appLauncherButton;
private void InitDebugMode() {
try {
// Utiliser le ConfigManager pour lire la configuration
if (configManager != null) {
bool debugMode = configManager.DebugMode;
Logger.SetDebugMode(debugMode);
Logger.LogInfo($"Debug mode initialized: {debugMode}");
} else {
Logger.LogError("ConfigManager not available for debug mode initialization");
}
}
catch (Exception ex) {
Logger.LogError($"Error initializing debug mode: {ex.Message}");
}
}
protected void Awake()
{
Logger.LogInfo("Awaked");
DontDestroyOnLoad(this);
// Initialiser les composants
configManager = new ConfigManager();
markerRenderer = new MarkerRenderer();
uiManager = new UIManager(configManager);
// Initialiser le mode debug après avoir créé le ConfigManager
InitDebugMode();
}
public void Start() {
Logger.LogInfo("Plugin started");
configManager.LoadConfig();
// Ajouter le bouton à l'Application Launcher
GameEvents.onGUIApplicationLauncherReady.Add(OnGUIApplicationLauncherReady);
GameEvents.onGUIApplicationLauncherDestroyed.Add(OnGUIApplicationLauncherDestroyed);
}
public void OnDestroy() {
Logger.LogInfo("Plugin stopped");
configManager.SaveConfig();
// Nettoyer les composants
markerRenderer?.Dispose();
// Supprimer le bouton de l'Application Launcher
RemoveAppLauncherButton();
}
private void OnGUIApplicationLauncherReady() {
if (ApplicationLauncher.Instance != null && appLauncherButton == null) {
// Créer une texture pour l'icône (cercle simple)
Texture2D iconTexture = CreateIconTexture();
appLauncherButton = ApplicationLauncher.Instance.AddModApplication(
OnAppLauncherTrue, // Callback quand le bouton est activé
OnAppLauncherFalse, // Callback quand le bouton est désactivé
null, // Callback quand la souris survole le bouton
null, // Callback quand la souris quitte le bouton
null, // Callback quand le bouton est cliqué
null, // Callback quand le bouton est cliqué avec le bouton droit
ApplicationLauncher.AppScenes.ALWAYS,
iconTexture
);
Logger.LogInfo("Application Launcher button added");
}
}
private void OnGUIApplicationLauncherDestroyed() {
RemoveAppLauncherButton();
}
private void RemoveAppLauncherButton() {
if (appLauncherButton != null) {
ApplicationLauncher.Instance.RemoveModApplication(appLauncherButton);
appLauncherButton = null;
Logger.LogInfo("Application Launcher button removed");
}
}
private void OnAppLauncherTrue() {
uiManager.ShowUI = true;
Logger.LogDebug("UI opened via Application Launcher");
}
private void OnAppLauncherFalse() {
uiManager.ShowUI = false;
Logger.LogDebug("UI closed via Application Launcher");
}
private Texture2D CreateIconTexture() {
// Créer une texture 24x24 pixels
Texture2D texture = new Texture2D(24, 24);
Color[] pixels = new Color[24 * 24];
// Couleur de fond (transparente)
Color backgroundColor = new Color(0, 0, 0, 0);
// Couleur de l'icône (blanc)
Color iconColor = Color.white;
// Remplir avec la couleur de fond
for (int i = 0; i < pixels.Length; i++) {
pixels[i] = backgroundColor;
}
// Dessiner un cercle simple
Vector2 center = new Vector2(12, 12);
float radius = 8f;
for (int x = 0; x < 24; x++) {
for (int y = 0; y < 24; y++) {
Vector2 pos = new Vector2(x, y);
float distance = Vector2.Distance(pos, center);
if (distance <= radius && distance >= radius - 2) {
pixels[y * 24 + x] = iconColor;
}
}
}
// Ajouter quelques points pour représenter des repères
pixels[6 * 24 + 12] = iconColor; // Point central vertical
pixels[12 * 24 + 6] = iconColor; // Point central horizontal
pixels[12 * 24 + 18] = iconColor; // Point central horizontal
pixels[18 * 24 + 12] = iconColor; // Point central vertical
texture.SetPixels(pixels);
texture.Apply();
return texture;
}
public void OnGUI() {
if( uiManager == null ) return;
uiManager.DrawUI();
}
public void OnRenderObject() {
if( markerRenderer == null ) return;
if( uiManager == null ) return;
// Get a copy of the list of markers, excluding the edited one
List<VisualMarker> markers = new List<VisualMarker>(configManager.Markers);
if( uiManager.EditingMarkerIndex >= 0 ) {
markers.RemoveAt(uiManager.EditingMarkerIndex);
}
markerRenderer.DrawMarkers(
markers,
uiManager.EditingMarker
);
}
}
}