Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ChromaHotkeyPainter.cs
Go to the documentation of this file.
1using System;
3using System.Linq;
5using Microsoft.Xna.Framework.Input;
9
10namespace Terraria.GameContent;
11
13{
14 private class ReactiveRGBKey
15 {
16 public readonly Keys XNAKey;
17
18 public readonly string WhatIsThisKeyFor;
19
20 private readonly Color _color;
21
22 private readonly TimeSpan _duration;
23
25
27
28 private RgbKey _rgbKey;
29
30 public bool Expired => _expireTime < Main.gameTimeCache.TotalGameTime;
31
33 {
34 _color = color;
35 XNAKey = key;
38 _startTime = Main.gameTimeCache.TotalGameTime;
39 }
40
41 public void Update()
42 {
43 float amount = (float)Utils.GetLerpValue(_startTime.TotalSeconds, _expireTime.TotalSeconds, Main.gameTimeCache.TotalGameTime.TotalSeconds, clamped: true);
45 }
46
47 public void Clear()
48 {
49 _rgbKey.Clear();
50 }
51
52 public void Unbind()
53 {
54 Main.Chroma.UnbindKey(XNAKey);
55 }
56
57 public void Bind()
58 {
60 }
61
62 public void Refresh()
63 {
64 _startTime = Main.gameTimeCache.TotalGameTime;
67 }
68 }
69
70 private class PaintKey
71 {
72 private string _triggerName;
73
75
77
79 {
81 _xnaKeys = new List<Keys>();
82 foreach (string key in keys)
83 {
84 if (Enum.TryParse<Keys>(key, ignoreCase: true, out var result))
85 {
86 _xnaKeys.Add(result);
87 }
88 }
89 _rgbKeys = new List<RgbKey>();
90 }
91
92 public void Unbind()
93 {
94 foreach (RgbKey rgbKey in _rgbKeys)
95 {
96 Main.Chroma.UnbindKey(rgbKey.Key);
97 }
98 }
99
100 public void Bind()
101 {
102 foreach (Keys xnaKey in _xnaKeys)
103 {
105 }
106 _rgbKeys = _rgbKeys.Distinct().ToList();
107 }
108
109 public void SetSolid(Color color)
110 {
111 foreach (RgbKey rgbKey in _rgbKeys)
112 {
113 rgbKey.SetSolid(color);
114 }
115 }
116
117 public void SetClear()
118 {
119 foreach (RgbKey rgbKey in _rgbKeys)
120 {
121 rgbKey.Clear();
122 }
123 }
124
125 public bool UsesKey(Keys key)
126 {
127 return _xnaKeys.Contains(key);
128 }
129
130 public void SetAlert(Color colorBase, Color colorFlash, float time, float flashesPerSecond)
131 {
132 if (time == -1f)
133 {
134 time = 10000f;
135 }
136 foreach (RgbKey rgbKey in _rgbKeys)
137 {
138 rgbKey.SetFlashing(colorBase, colorFlash, time, flashesPerSecond);
139 }
140 }
141
143 {
144 return new List<Keys>(_xnaKeys);
145 }
146 }
147
148 private static class PainterColors
149 {
150 private const float HOTKEY_COLOR_MULTIPLIER = 1f;
151
152 public static readonly Color MovementKeys = Color.Gray * 1f;
153
154 public static readonly Color QuickMount = Color.RoyalBlue * 1f;
155
156 public static readonly Color QuickGrapple = Color.Lerp(Color.RoyalBlue, Color.Blue, 0.5f) * 1f;
157
158 public static readonly Color QuickHealReady = Color.Pink * 1f;
159
160 public static readonly Color QuickHealReadyUrgent = Color.DeepPink * 1f;
161
162 public static readonly Color QuickHealCooldown = Color.HotPink * 0.5f * 1f;
163
164 public static readonly Color QuickMana = new Color(40, 0, 230) * 1f;
165
166 public static readonly Color Throw = Color.Red * 0.2f * 1f;
167
168 public static readonly Color SmartCursor = Color.Gold;
169
170 public static readonly Color SmartSelect = Color.Goldenrod;
171
172 public static readonly Color DangerKeyBlocked = Color.Red * 1f;
173 }
174
176
178
180
182
183 private int _quickHealAlert;
184
186
188
190
192
194
196
198
200
202
204
205 public bool PotionAlert => _quickHealAlert != 0;
206
207 public void CollectBoundKeys()
208 {
210 {
211 key.Value.Unbind();
212 }
213 _keys.Clear();
214 foreach (KeyValuePair<string, List<string>> item in PlayerInput.CurrentProfile.InputModes[InputMode.Keyboard].KeyStatus)
215 {
216 _keys.Add(item.Key, new PaintKey(item.Key, item.Value));
217 }
219 {
220 key2.Value.Bind();
221 }
223 {
224 _keys["Up"],
225 _keys["Down"],
226 _keys["Left"],
227 _keys["Right"]
228 };
229 _healKey = _keys["QuickHeal"];
230 _mountKey = _keys["QuickMount"];
231 _jumpKey = _keys["Jump"];
232 _grappleKey = _keys["Grapple"];
233 _throwKey = _keys["Throw"];
234 _manaKey = _keys["QuickMana"];
235 _buffKey = _keys["QuickBuff"];
236 _smartCursorKey = _keys["SmartCursor"];
237 _smartSelectKey = _keys["SmartSelect"];
241 {
242 _xnaKeysInUse.AddRange(key3.Value.GetXNAKeysInUse());
243 }
244 _xnaKeysInUse = _xnaKeysInUse.Distinct().ToList();
245 }
246
247 [Old("Reactive keys are no longer used so this catch-all method isn't used")]
248 public void PressKey(Keys key)
249 {
250 }
251
253 {
254 return _reactiveKeys.FirstOrDefault((ReactiveRGBKey x) => x.XNAKey == keyTarget);
255 }
256
257 public void Update()
258 {
260 if (!Main.hasFocus)
261 {
263 return;
264 }
265 if (PotionAlert)
266 {
268 {
269 if (key.Key != "QuickHeal")
270 {
271 key.Value.SetClear();
272 }
273 }
275 }
276 else
277 {
280 }
281 if (Main.InGameUI.CurrentState == Main.ManageControlsMenu)
282 {
285 }
287 }
288
290 {
291 foreach (PaintKey key in keys)
292 {
293 key.SetSolid(color);
294 }
295 }
296
298 {
299 foreach (PaintKey key in keys)
300 {
301 key.SetClear();
302 }
303 }
304
318
320 {
321 foreach (ReactiveRGBKey key in _reactiveKeys.FindAll((ReactiveRGBKey x) => x.Expired))
322 {
323 key.Clear();
324 if (!_keys.Any((KeyValuePair<string, PaintKey> x) => x.Value.UsesKey(key.XNAKey)))
325 {
326 key.Unbind();
327 }
328 }
329 _reactiveKeys.RemoveAll((ReactiveRGBKey x) => x.Expired);
331 {
332 reactiveKey.Update();
333 }
334 }
335
336 private void Step_ClearAll()
337 {
339 {
340 key.Value.SetClear();
341 }
342 }
343
344 private void Step_SmartKeys()
345 {
349 {
350 smartCursorKey.SetClear();
351 smartSelectKey.SetClear();
352 return;
353 }
355 {
357 }
358 else
359 {
360 smartCursorKey.SetClear();
361 }
362 if (_player.nonTorch >= 0)
363 {
365 }
366 else
367 {
368 smartSelectKey.SetClear();
369 }
370 }
371
372 private void Step_Movement()
373 {
375 bool flag = _player.frozen || _player.tongued || _player.webbed || _player.stoned;
377 {
379 }
380 else if (flag)
381 {
383 }
384 else
385 {
387 }
388 }
389
390 private void Step_Mount()
391 {
394 {
395 mountKey.SetClear();
396 }
398 {
400 if (_player.gravDir == -1f)
401 {
402 mountKey.SetSolid(PainterColors.DangerKeyBlocked * 0.6f);
403 }
404 }
405 else
406 {
408 }
409 }
410
411 private void Step_Grapple()
412 {
415 {
416 grappleKey.SetClear();
417 }
419 {
421 }
422 else
423 {
425 }
426 }
427
428 private void Step_Jump()
429 {
432 {
433 jumpKey.SetClear();
434 }
436 {
438 }
439 else
440 {
442 }
443 }
444
445 private void Step_QuickHeal()
446 {
449 {
450 healKey.SetClear();
451 _quickHealAlert = 0;
452 }
453 else if (_player.potionDelay > 0)
454 {
457 healKey.SetSolid(solid);
458 _quickHealAlert = 0;
459 }
461 {
462 healKey.SetClear();
463 _quickHealAlert = 0;
464 }
465 else if ((float)_player.statLife <= (float)_player.statLifeMax2 / 4f)
466 {
467 if (_quickHealAlert != 1)
468 {
469 _quickHealAlert = 1;
471 }
472 }
473 else if ((float)_player.statLife <= (float)_player.statLifeMax2 / 2f)
474 {
475 if (_quickHealAlert != 2)
476 {
477 _quickHealAlert = 2;
479 }
480 }
481 else
482 {
484 _quickHealAlert = 0;
485 }
486 }
487
488 private void Step_QuickMana()
489 {
492 {
493 manaKey.SetClear();
494 }
495 else
496 {
498 }
499 }
500
501 private void Step_Throw()
502 {
506 {
507 throwKey.SetClear();
508 }
510 {
511 throwKey.SetClear();
512 }
513 else
514 {
515 throwKey.SetSolid(PainterColors.Throw);
516 }
517 }
518}
void Add(TKey key, TValue value)
void AddRange(IEnumerable< T > collection)
Definition List.cs:275
static bool TryParse(Type enumType, string? value, out object? result)
Definition Enum.cs:416
PaintKey(string triggerName, List< string > keys)
void SetAlert(Color colorBase, Color colorFlash, float time, float flashesPerSecond)
ReactiveRGBKey(Keys key, Color color, TimeSpan duration, string whatIsThisKeyFor)
void SetGroupColorBase(List< PaintKey > keys, Color color)
readonly List< ReactiveRGBKey > _reactiveKeys
ReactiveRGBKey FindReactiveKey(Keys keyTarget)
readonly Dictionary< string, PaintKey > _keys
static PlayerInputProfile CurrentProfile
bool favorited
Definition Item.cs:135
static GameTime gameTimeCache
Definition Main.cs:409
static UIManageControls ManageControlsMenu
Definition Main.cs:1150
static UserInterface InGameUI
Definition Main.cs:383
static bool hasFocus
Definition Main.cs:1781
static ChromaEngine Chroma
Definition Main.cs:285
static Player LocalPlayer
Definition Main.cs:2829
static bool SmartCursorWanted
Definition Main.cs:2840
Item QuickGrapple_GetItemToUse()
Definition Player.cs:5447
Item QuickMana_GetItemToUse()
Definition Player.cs:4756
Item QuickHeal_GetItemToUse()
Definition Player.cs:4689
int potionDelayTime
Definition Player.cs:2443
Item QuickMount_GetItemToUse()
Definition Player.cs:5116
static float GetLerpValue(float from, float to, float t, bool clamped=false)
Definition Utils.cs:203
static Color Lerp(Color value1, Color value2, float amount)
Definition Color.cs:491
double TotalSeconds
Definition TimeSpan.cs:64
TimeSpan Add(TimeSpan ts)
Definition TimeSpan.cs:103