-
Notifications
You must be signed in to change notification settings - Fork 4
/
img_viewer.cs
367 lines (328 loc) · 12.7 KB
/
img_viewer.cs
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// -------------------------------------------------------------------------------------------------
// img_viewer.cs 0.3
//
// Simple KSP plugin to view images ingame.
// Copyright (C) 2015 Iván Atienza
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//
// Email: mecagoenbush at gmail dot com
// Freenode: hashashin
//
// -------------------------------------------------------------------------------------------------
using KSP.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine;
using File = KSP.IO.File;
namespace img_viewer
{
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
public class ImgViewer : MonoBehaviour
{
private Rect _windowRect;
private Rect _windowRect2 = new Rect(Screen.width / 2 - 150f, Screen.height / 2 - 75f, 260f, 390f);
private string _keybind;
private bool _visible;
private IButton _button;
private const string _tooltip = "Image Viewer Menu";
private const string _btextureOn = "img_viewer/Textures/icon_on";
private const string _btextureOff = "img_viewer/Textures/icon_off";
private string _version;
private string _versionlastrun;
private Texture2D _image;
private WWW _imagetex;
private string _imagefile;
private readonly string _imageurl = "file://" + KSPUtil.ApplicationRootPath.Replace("\\", "/") +
"/GameData/img_viewer/Plugins/PluginData/Images/";
private readonly string _imagedir = KSPUtil.ApplicationRootPath.Replace("\\", "/") +
"/GameData/img_viewer/Plugins/PluginData/Images/";
private List<string> _imageList;
private Vector2 _scrollViewVector = Vector2.zero;
private int _selectionGridInt;
private bool _showList;
private bool _useKSPskin;
private int _lastimg = -1;
private void Awake()
{
LoadVersion();
VersionCheck();
LoadSettings();
}
private void Start()
{
//populate the list of images
if (_imageList == null)
{
GetImages();
}
// toolbar stuff
if (!ToolbarManager.ToolbarAvailable) return;
_button = ToolbarManager.Instance.add("img_viewer", "toggle");
_button.TexturePath = _btextureOff;
_button.ToolTip = _tooltip;
_button.OnClick += (e => TogglePopupMenu(_button));
}
private void OnGUI()
{
// Saves the current Gui.skin for later restore
GUISkin _defGuiSkin = GUI.skin;
if (_visible)
{
GUI.skin = _useKSPskin ? HighLogic.Skin : _defGuiSkin;
_windowRect = GUI.Window(GUIUtility.GetControlID(0, FocusType.Passive), _windowRect, IvWindow,
"Image viewer");
}
if (_showList)
{
GUI.skin = _useKSPskin ? HighLogic.Skin : _defGuiSkin;
_windowRect2 = GUI.Window(GUIUtility.GetControlID(FocusType.Passive), _windowRect2, ListWindow,
"Image list");
}
//Restore the skin
GUI.skin = _defGuiSkin;
}
private void IvWindow(int windowID)
{
if (_image != null)
{
_windowRect = new Rect(_windowRect.xMin, _windowRect.yMin, _image.width, _image.height + 20f);
GUI.DrawTexture(new Rect(0f, 20f, _image.width, _image.height), _image, ScaleMode.ScaleToFit, true, 0f);
}
else
{
_windowRect = new Rect(Screen.width / 2f, Screen.height / 2f, 100f, 100f);
}
if (GUI.Button(new Rect(2f, 2f, 13f, 13f), "X"))
{
Toggle();
}
GUI.DragWindow();
}
private void ListWindow(int windowId)
{
if (_imageList != null)
{
// Notes list gui.
_scrollViewVector = GUILayout.BeginScrollView(_scrollViewVector);
var _options = new[] { GUILayout.Width(225f), GUILayout.ExpandWidth(false) };
_selectionGridInt = GUILayout.SelectionGrid(_selectionGridInt, _imageList.ToArray(), 1, _options);
GUILayout.EndScrollView();
// Refresh images list.
GUILayout.BeginHorizontal();
if (GUILayout.Button("-10% size"))
{
ImageZm();
}
if (GUILayout.Button("Original size"))
{
ImageOrig();
}
if (GUILayout.Button("+10% size"))
{
ImageZp();
}
GUILayout.EndHorizontal();
}
GUI.contentColor = Color.green;
if (GUILayout.Button("Refresh list"))
{
GetImages();
}
GUI.contentColor = Color.white;
// Close the list window.
if (GUI.Button(new Rect(2f, 2f, 13f, 13f), "X"))
{
_showList = !_showList;
}
// Makes the window dragable.
GUI.DragWindow();
}
private void Update()
{
if (Input.GetKey(KeyCode.LeftAlt) && Input.GetKeyDown(_keybind))
{
Toggle();
}
if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(_keybind))
{
_showList = !_showList;
}
if (_imageList == null) return;
if (_lastimg == _selectionGridInt) return;
Destroy(_image);
_imagefile = _imageList[_selectionGridInt];
_imagetex = new WWW(_imageurl + _imagefile);
_image = _imagetex.texture;
_imagetex.Dispose();
_lastimg = _selectionGridInt;
}
private void GetImages()
{
if (Directory.GetFiles(_imagedir, "*").Any())
{
_imageList = new List<string>(Directory.GetFiles(_imagedir, "*"));
for (int i = 0; i < _imageList.Count; i++)
{
_imageList[i] = Path.GetFileName(_imageList[i]);
}
}
}
private void OnDestroy()
{
SaveSettings();
if (_button != null)
{
_button.Destroy();
}
}
private void LoadSettings()
{
print("[img_viewer.dll] Loading Config...");
PluginConfiguration _configfile = PluginConfiguration.CreateForType<ImgViewer>();
_configfile.load();
_windowRect = _configfile.GetValue<Rect>("windowpos");
_windowRect2 = _configfile.GetValue("windowpos2",
new Rect(Screen.width / 2 - 150f, Screen.height / 2 - 75f, 270f, 390f));
_keybind = _configfile.GetValue("keybind", "i");
_versionlastrun = _configfile.GetValue<string>("version");
_useKSPskin = _configfile.GetValue("kspskin", false);
_visible = _configfile.GetValue("visible", false);
_selectionGridInt = _configfile.GetValue("lastimage", 0);
print("[img_viewer.dll] Config Loaded Successfully");
}
private void SaveSettings()
{
print("[img_viewer.dll] Saving Config...");
PluginConfiguration _configfile = PluginConfiguration.CreateForType<ImgViewer>();
_configfile.SetValue("windowpos", _windowRect);
_configfile.SetValue("windowpos2", _windowRect2);
_configfile.SetValue("keybind", _keybind);
_configfile.SetValue("version", _version);
_configfile.SetValue("kspskin", _useKSPskin);
_configfile.SetValue("visible", _visible);
_configfile.SetValue("lastimage", _selectionGridInt);
_configfile.save();
print("[img_viewer.dll] Config Saved ");
}
private void Toggle()
{
if (_visible)
{
_visible = false;
_button.TexturePath = _btextureOff;
if (_showList)
{
_showList = false;
}
}
else
{
_visible = true;
_button.TexturePath = _btextureOn;
}
}
private void VersionCheck()
{
_version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
print("img_viewer.dll version: " + _version);
if ((_version != _versionlastrun) && (File.Exists<ImgViewer>("config.xml")))
{
File.Delete<ImgViewer>("config.xml");
}
#if DEBUG
File.Delete<ImgViewer>("config.xml");
#endif
}
private void LoadVersion()
{
PluginConfiguration _configfile = PluginConfiguration.CreateForType<ImgViewer>();
_configfile.load();
_versionlastrun = _configfile.GetValue<string>("version");
}
private void TogglePopupMenu(IButton button)
{
if (button.Drawable == null)
{
createPopupMenu(button);
}
else
{
destroyPopupMenu(button);
}
}
private void createPopupMenu(IButton button)
{
// create menu drawable
PopupMenuDrawable _menu = new PopupMenuDrawable();
// create menu options
IButton _option1 = _menu.AddOption("Show/Hide image");
_option1.OnClick += e => Toggle();
IButton _option2 = _menu.AddOption("Show/hide image list");
_option2.OnClick += e => _showList = !_showList;
IButton _option3 = _menu.AddOption("Change skin");
_option3.OnClick += e => _useKSPskin = !_useKSPskin;
IButton _option4 = _menu.AddOption("Next image");
_option4.OnClick += e => ImageNext();
IButton _option5 = _menu.AddOption("Prev image");
_option5.OnClick += e => ImagePrev();
IButton _option6 = _menu.AddOption("-10% size");
_option6.OnClick += e => ImageZm();
IButton _option7 = _menu.AddOption("Original");
_option7.OnClick += e => ImageOrig();
IButton _option8 = _menu.AddOption("+10% size");
_option8.OnClick += e => ImageZp();
// auto-close popup menu when any option is clicked
_menu.OnAnyOptionClicked += () => destroyPopupMenu(button);
// hook drawable to button
button.Drawable = _menu;
}
private void ImagePrev()
{
if (_selectionGridInt == 0) return;
_selectionGridInt--;
}
private void ImageNext()
{
if (_selectionGridInt == _imageList.Count - 1) return;
_selectionGridInt++;
}
private void ImageZm()
{
TextureScale.Bilinear(_image, _image.width - ((_image.width * 10) / 100), _image.height - ((_image.height * 10) / 100));
GUI.DrawTexture(new Rect(0f, 20f, _image.width, _image.height), _image, ScaleMode.ScaleToFit, true, 0f);
}
private void ImageOrig()
{
_imagetex = new WWW(_imageurl + _imagefile);
_image = _imagetex.texture;
_imagetex.Dispose();
GUI.DrawTexture(new Rect(0f, 20f, _image.width, _image.height), _image, ScaleMode.ScaleToFit, true, 0f);
}
private void ImageZp()
{
TextureScale.Bilinear(_image, _image.width + ((_image.width * 10) / 100), _image.height + ((_image.height * 10) / 100));
GUI.DrawTexture(new Rect(0f, 20f, _image.width, _image.height), _image, ScaleMode.ScaleToFit, true, 0f);
}
private void destroyPopupMenu(IButton button)
{
// PopupMenuDrawable must be destroyed explicitly
((PopupMenuDrawable)button.Drawable).Destroy();
// unhook drawable
button.Drawable = null;
}
}
}