-
Notifications
You must be signed in to change notification settings - Fork 0
/
InventoryGUI.cs
300 lines (280 loc) · 14.4 KB
/
InventoryGUI.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
using UnityEngine;
public class InventoryGUI : MonoBehaviour
{
private PlayerController playerController;
private InventoryManager playerInventory;
private InventoryHandler inventoryHandler;
private TextureDictionary textureDictionary;
private GuiCoordinates guiCoordinates;
private string storageComputerSearchText = "";
public bool outOfSpace;
public bool cannotAfford;
public bool missingItem;
private float outOfSpaceTimer;
private float cannotAffordTimer;
private float missingItemTimer;
//! Called by unity engine on start up to initialize variables.
public void Start()
{
playerController = GetComponent<PlayerController>();
playerInventory = GetComponent<InventoryManager>();
inventoryHandler = new InventoryHandler(playerController, playerInventory);
GameManager gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
textureDictionary = gameManager.GetComponent<TextureDictionary>();
guiCoordinates = new GuiCoordinates();
}
//! Called by unity engine for rendering and handling GUI events.
public void OnGUI()
{
// Style.
GUI.skin = GetComponent<PlayerGUI>().thisGUIskin;
// Aspect ratio.
float ScreenHeight = Screen.height;
float ScreenWidth = Screen.width;
if (ScreenWidth / ScreenHeight < 1.7f)
{
ScreenHeight = (ScreenHeight * 0.75f);
}
if (ScreenHeight < 700)
{
GUI.skin.label.fontSize = 10;
}
if (!playerController.stateManager.Busy() && GetComponent<MainMenu>().finishedLoading == true)
{
if (playerController.inventoryOpen == true)
{
gameObject.GetComponent<MSCameraController>().enabled = false;
GUI.DrawTexture(guiCoordinates.inventoryBackgroundRect, textureDictionary.dictionary["Container Background"]);
GUI.DrawTexture(guiCoordinates.inventoryDropSlotRect, textureDictionary.dictionary["Shaded Box"]);
GUI.Label(guiCoordinates.inventoryDropSlotLabelRect, "DROP");
GUI.DrawTexture(guiCoordinates.inventoryTrashSlotRect, textureDictionary.dictionary["Shaded Box"]);
GUI.Label(guiCoordinates.inventoryTrashSlotLabelRect, "TRASH");
// Inventory item drawing.
int inventorySlotNumber = 0;
foreach (InventorySlot slot in playerInventory.inventory)
{
if (!slot.typeInSlot.Equals("") && !slot.typeInSlot.Equals("nothing"))
{
if (textureDictionary.dictionary.ContainsKey(slot.typeInSlot+"_Icon"))
{
GUI.DrawTexture(guiCoordinates.inventorySlotRects[inventorySlotNumber], textureDictionary.dictionary[slot.typeInSlot+"_Icon"]);
}
else
{
GUI.DrawTexture(guiCoordinates.inventorySlotRects[inventorySlotNumber], textureDictionary.dictionary[slot.typeInSlot]);
}
}
if (slot.amountInSlot != 0)
{
GUI.Label(guiCoordinates.inventorySlotLabelRects[inventorySlotNumber], slot.amountInSlot.ToString());
}
inventorySlotNumber++;
}
// Storage container item drawing.
if (playerController.storageGUIopen == true)
{
GUI.DrawTexture(guiCoordinates.inventoryInfoRectBG, textureDictionary.dictionary["Interface Background"]);
GUI.Label(guiCoordinates.inventoryInfoRect, "\nLeft Shift + Click & Drag to split stack.\n\nLeft Control + Click to transfer entire stack.");
GUI.DrawTexture(guiCoordinates.storageInventoryBackgroundRect, textureDictionary.dictionary["Container Background"]);
int storageInventorySlotNumber = 0;
foreach (InventorySlot slot in playerController.storageInventory.inventory)
{
if (!slot.typeInSlot.Equals("") && !slot.typeInSlot.Equals("nothing"))
{
if (textureDictionary.dictionary.ContainsKey(slot.typeInSlot+"_Icon"))
{
GUI.DrawTexture(guiCoordinates.storageInventorySlotRects[storageInventorySlotNumber], textureDictionary.dictionary[slot.typeInSlot+"_Icon"]);
}
else
{
GUI.DrawTexture(guiCoordinates.storageInventorySlotRects[storageInventorySlotNumber], textureDictionary.dictionary[slot.typeInSlot]);
}
}
if (slot.amountInSlot != 0)
{
GUI.Label(guiCoordinates.storageInventorySlotLabelRects[storageInventorySlotNumber], slot.amountInSlot.ToString());
}
storageInventorySlotNumber++;
}
}
// // // // // DRAG AND DROP // // // // //
Vector2 mousePos = Event.current.mousePosition;
// Dragging items from the player's inventory.
int inventoryDragSlot = 0;
foreach (Rect rect in guiCoordinates.inventorySlotRects)
{
if (rect.Contains(mousePos))
{
InventorySlot dragSlot = playerInventory.inventory[inventoryDragSlot];
if (dragSlot.typeInSlot != "" && !dragSlot.typeInSlot.Equals("nothing"))
{
GUI.Label(guiCoordinates.inventoryMesageRect, dragSlot.typeInSlot);
if (Input.GetKeyDown(KeyCode.Mouse0))
{
inventoryHandler.DragItemFromSlot(inventoryDragSlot, dragSlot, playerController.storageInventory);
}
}
}
inventoryDragSlot++;
}
// The player is accessing a storage container.
if (playerController.storageGUIopen == true)
{
int storageInventoryDragSlot = 0;
foreach (Rect rect in guiCoordinates.storageInventorySlotRects)
{
if (rect.Contains(mousePos))
{
InventorySlot dragSlot = playerController.storageInventory.inventory[storageInventoryDragSlot];
if (dragSlot.typeInSlot != "" && !dragSlot.typeInSlot.Equals("nothing"))
{
if (playerController.remoteStorageActive == true)
{
GUI.Label(guiCoordinates.storageComputerMessageRect, dragSlot.typeInSlot);
}
else
{
GUI.Label(guiCoordinates.storageInventoryMessageRect, dragSlot.typeInSlot);
}
if (Input.GetKeyDown(KeyCode.Mouse0))
{
inventoryHandler.DragItemFromSlot(storageInventoryDragSlot, dragSlot, playerInventory);
}
}
}
storageInventoryDragSlot++;
}
}
// The player is dragging an inventory item.
if (playerController.draggingItem == true)
{
float orgX = Event.current.mousePosition.x - ScreenWidth * 0.0145f;
float orgY = Event.current.mousePosition.y - ScreenHeight * 0.03f;
Rect mouseRect = new Rect(orgX, orgY, ScreenWidth * 0.029f, ScreenHeight * 0.06f);
if (textureDictionary.dictionary.ContainsKey(inventoryHandler.itemToDrag+"_Icon"))
{
GUI.DrawTexture(mouseRect, textureDictionary.dictionary[inventoryHandler.itemToDrag+"_Icon"]);
}
else
{
GUI.DrawTexture(mouseRect, textureDictionary.dictionary[inventoryHandler.itemToDrag]);
}
if (Input.GetKeyUp(KeyCode.Mouse0))
{
inventoryHandler.DropItemInSlot(mousePos, playerController.storageGUIopen);
}
}
// // // // // END DRAG AND DROP // // // // //
// Cycling between inventories connected to a storage computer.
if (playerController.storageGUIopen == true && playerController.remoteStorageActive == true)
{
StorageComputer computer = playerController.currentStorageComputer.GetComponent<StorageComputer>();
GUI.Label(guiCoordinates.storageSearchLabelRect, "SEARCH");
storageComputerSearchText = GUI.TextField(guiCoordinates.storageComputerSearchRect, storageComputerSearchText);
if (Event.current.isKey && Event.current.keyCode != KeyCode.LeftShift && Event.current.keyCode != KeyCode.LeftControl)
{
inventoryHandler.SearchComputerContainers(computer, storageComputerSearchText);
}
if (GUI.Button(guiCoordinates.storageComputerPreviousRect, "<-"))
{
if (playerController.storageComputerInventory > 0)
{
playerController.storageComputerInventory -= 1;
playerController.storageInventory = computer.computerContainers[playerController.storageComputerInventory];
}
playerController.PlayButtonSound();
}
if (GUI.Button(guiCoordinates.storageComputerNextRect, "->"))
{
if (playerController.storageComputerInventory < computer.computerContainers.Length - 1)
{
playerController.storageComputerInventory += 1;
playerController.storageInventory = computer.computerContainers[playerController.storageComputerInventory];
}
playerController.PlayButtonSound();
}
if (GUI.Button(guiCoordinates.storageComputerRebootRect, "REBOOT"))
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
playerController.inventoryOpen = false;
playerController.craftingGUIopen = false;
playerController.storageGUIopen = false;
computer.Reboot();
playerController.PlayButtonSound();
}
}
// Message displayed when the player attempts to purchase something they cannot afford from the market.
if (cannotAfford == true)
{
if (cannotAffordTimer < 3)
{
GUI.Label(guiCoordinates.inventoryMesageRect, "Cannot afford.");
cannotAffordTimer += 1 * Time.deltaTime;
}
else
{
cannotAfford = false;
cannotAffordTimer = 0;
}
}
// Message displayed when the player is missing a required item for sale or crafting.
if (missingItem == true)
{
if (missingItemTimer < 3)
{
GUI.Label(guiCoordinates.inventoryMesageRect, "Missing items.");
missingItemTimer += 1 * Time.deltaTime;
}
else
{
missingItem = false;
missingItemTimer = 0;
}
}
// Message displayed when the player does not have adequate inventory space to craft or purchase an item.
if (outOfSpace == true)
{
if (outOfSpaceTimer < 3)
{
GUI.Label(guiCoordinates.inventoryMesageRect, "\nNo space in inventory.");
outOfSpaceTimer += 1 * Time.deltaTime;
}
else
{
outOfSpace = false;
outOfSpaceTimer = 0;
}
}
// Buttons.
if (playerController.marketGUIopen == false)
{
if (playerController.storageGUIopen == false)
{
if (GUI.Button(guiCoordinates.craftingButtonRect, "CRAFTING"))
{
if (playerController.craftingGUIopen == false && playerController.storageGUIopen == false)
{
playerController.craftingGUIopen = true;
}
else
{
playerController.craftingGUIopen = false;
}
playerController.PlayButtonSound();
}
}
if (GUI.Button(guiCoordinates.closeButtonRect, "CLOSE"))
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
playerController.inventoryOpen = false;
playerController.craftingGUIopen = false;
playerController.storageGUIopen = false;
playerController.PlayButtonSound();
}
}
}
}
}
}