tgui-core 1.1.8 → 1.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/common/assets.ts +38 -0
- package/lib/common/collections.ts +27 -0
- package/lib/common/color.ts +88 -0
- package/lib/common/constants.ts +349 -0
- package/lib/common/events.ts +262 -0
- package/{dist/common/exhaustive.d.ts → lib/common/exhaustive.ts} +3 -1
- package/lib/common/format.ts +167 -0
- package/{dist/common/fp.d.ts → lib/common/fp.ts} +16 -2
- package/lib/common/hotkeys.ts +207 -0
- package/lib/common/http.ts +16 -0
- package/lib/common/keycodes.ts +86 -0
- package/{dist/common/keys.d.ts → lib/common/keys.ts} +24 -21
- package/lib/common/math.ts +76 -0
- package/lib/common/perf.ts +72 -0
- package/lib/common/random.ts +32 -0
- package/lib/common/react.ts +59 -0
- package/lib/common/redux.ts +187 -0
- package/lib/common/storage.ts +207 -0
- package/lib/common/string.ts +169 -0
- package/lib/common/timer.ts +63 -0
- package/lib/common/type-utils.ts +41 -0
- package/lib/common/types.d.ts +12 -0
- package/lib/common/uuid.ts +18 -0
- package/lib/components/AnimatedNumber.tsx +180 -0
- package/lib/components/Autofocus.tsx +23 -0
- package/lib/components/Blink.tsx +91 -0
- package/lib/components/BlockQuote.tsx +9 -0
- package/lib/components/BodyZoneSelector.tsx +149 -0
- package/lib/components/Box.tsx +252 -0
- package/lib/components/Button.tsx +425 -0
- package/lib/components/ByondUi.jsx +110 -0
- package/lib/components/Chart.tsx +155 -0
- package/lib/components/Collapsible.tsx +43 -0
- package/lib/components/ColorBox.tsx +29 -0
- package/lib/components/Dialog.tsx +81 -0
- package/lib/components/Dimmer.tsx +13 -0
- package/lib/components/Divider.tsx +20 -0
- package/lib/components/DmIcon.tsx +86 -0
- package/lib/components/DraggableControl.jsx +276 -0
- package/lib/components/Dropdown.tsx +246 -0
- package/lib/components/FakeTerminal.jsx +52 -0
- package/lib/components/FitText.tsx +99 -0
- package/lib/components/Flex.tsx +159 -0
- package/lib/components/Icon.tsx +95 -0
- package/lib/components/Image.tsx +54 -0
- package/lib/components/InfinitePlane.jsx +192 -0
- package/lib/components/Input.tsx +176 -0
- package/lib/components/KeyListener.tsx +40 -0
- package/lib/components/Knob.tsx +178 -0
- package/lib/components/LabeledControls.tsx +44 -0
- package/lib/components/LabeledList.tsx +154 -0
- package/lib/components/MenuBar.tsx +228 -0
- package/lib/components/Modal.tsx +23 -0
- package/lib/components/NoticeBox.tsx +45 -0
- package/lib/components/NumberInput.tsx +328 -0
- package/lib/components/Popper.tsx +100 -0
- package/lib/components/ProgressBar.tsx +105 -0
- package/lib/components/RestrictedInput.jsx +301 -0
- package/lib/components/RoundGauge.tsx +180 -0
- package/lib/components/Section.tsx +120 -0
- package/lib/components/Slider.tsx +169 -0
- package/lib/components/Stack.tsx +96 -0
- package/lib/components/StyleableSection.tsx +33 -0
- package/lib/components/Table.tsx +84 -0
- package/lib/components/Tabs.tsx +89 -0
- package/lib/components/TextArea.tsx +182 -0
- package/lib/components/TimeDisplay.jsx +64 -0
- package/lib/components/Tooltip.tsx +152 -0
- package/lib/components/TrackOutsideClicks.tsx +35 -0
- package/lib/components/VirtualList.tsx +69 -0
- package/lib/styles/atomic/candystripe.scss +8 -0
- package/lib/styles/atomic/centered-image.scss +7 -0
- package/lib/styles/atomic/color.scss +21 -0
- package/lib/styles/atomic/debug-layout.scss +17 -0
- package/lib/styles/atomic/fit-text.scss +14 -0
- package/lib/styles/atomic/links.scss +12 -0
- package/lib/styles/atomic/outline.scss +47 -0
- package/lib/styles/atomic/text.scss +44 -0
- package/lib/styles/base.scss +32 -0
- package/lib/styles/colors.scss +92 -0
- package/lib/styles/components/BlockQuote.module.scss +20 -0
- package/lib/styles/components/BlockQuote.module.scss.d.ts +4 -0
- package/lib/styles/components/Button.module.scss +157 -0
- package/lib/styles/components/Button.module.scss.d.ts +46 -0
- package/lib/styles/components/ColorBox.module.scss +12 -0
- package/lib/styles/components/ColorBox.module.scss.d.ts +4 -0
- package/lib/styles/components/Dialog.module.scss +60 -0
- package/lib/styles/components/Dialog.module.scss.d.ts +10 -0
- package/lib/styles/components/Dimmer.module.scss +22 -0
- package/lib/styles/components/Dimmer.module.scss.d.ts +4 -0
- package/lib/styles/components/Divider.module.scss +27 -0
- package/lib/styles/components/Divider.module.scss.d.ts +6 -0
- package/lib/styles/components/Dropdown.scss +72 -0
- package/lib/styles/components/Flex.module.scss +13 -0
- package/lib/styles/components/Flex.module.scss.d.ts +5 -0
- package/lib/styles/components/Icon.module.scss +25 -0
- package/lib/styles/components/Icon.module.scss.d.ts +5 -0
- package/lib/styles/components/Input.module.scss +64 -0
- package/lib/styles/components/Input.module.scss.d.ts +8 -0
- package/lib/styles/components/Knob.module.scss +131 -0
- package/lib/styles/components/Knob.module.scss.d.ts +33 -0
- package/lib/styles/components/LabeledList.module.scss +49 -0
- package/lib/styles/components/LabeledList.module.scss.d.ts +8 -0
- package/lib/styles/components/MenuBar.module.scss +75 -0
- package/lib/styles/components/MenuBar.module.scss.d.ts +14 -0
- package/lib/styles/components/Modal.module.scss +14 -0
- package/lib/styles/components/Modal.module.scss.d.ts +4 -0
- package/lib/styles/components/NoticeBox.module.scss +65 -0
- package/lib/styles/components/NoticeBox.module.scss.d.ts +27 -0
- package/lib/styles/components/NumberInput.module.scss +71 -0
- package/lib/styles/components/NumberInput.module.scss.d.ts +9 -0
- package/lib/styles/components/ProgressBar.module.scss +63 -0
- package/lib/styles/components/ProgressBar.module.scss.d.ts +27 -0
- package/lib/styles/components/RoundGauge.module.scss +85 -0
- package/lib/styles/components/RoundGauge.module.scss.d.ts +49 -0
- package/lib/styles/components/Section.module.scss +130 -0
- package/lib/styles/components/Section.module.scss.d.ts +13 -0
- package/lib/styles/components/Slider.module.scss +54 -0
- package/lib/styles/components/Slider.module.scss.d.ts +8 -0
- package/lib/styles/components/Stack.module.scss +60 -0
- package/lib/styles/components/Stack.module.scss.d.ts +12 -0
- package/lib/styles/components/Table.module.scss +44 -0
- package/lib/styles/components/Table.module.scss.d.ts +10 -0
- package/lib/styles/components/Tabs.module.scss +144 -0
- package/lib/styles/components/Tabs.module.scss.d.ts +35 -0
- package/lib/styles/components/TextArea.module.scss +86 -0
- package/lib/styles/components/TextArea.module.scss.d.ts +11 -0
- package/lib/styles/components/Tooltip.module.scss +24 -0
- package/lib/styles/components/Tooltip.module.scss.d.ts +4 -0
- package/lib/styles/functions.scss +79 -0
- package/lib/styles/input.scss +9 -0
- package/lib/styles/main.scss +20 -0
- package/lib/styles/reset.scss +68 -0
- package/package.json +6 -6
- package/dist/ProgressBar.module-BkAFfFy0.js +0 -29
- package/dist/Section.module-CLVHJ4yA.js +0 -15
- package/dist/assets/BlockQuote.css +0 -1
- package/dist/assets/Button.css +0 -1
- package/dist/assets/ColorBox.css +0 -1
- package/dist/assets/Dialog.css +0 -1
- package/dist/assets/Dimmer.css +0 -1
- package/dist/assets/Divider.css +0 -1
- package/dist/assets/Flex.css +0 -1
- package/dist/assets/Icon.css +0 -6
- package/dist/assets/Input.css +0 -1
- package/dist/assets/Knob.css +0 -1
- package/dist/assets/LabeledList.css +0 -1
- package/dist/assets/MenuBar.css +0 -1
- package/dist/assets/Modal.css +0 -1
- package/dist/assets/NoticeBox.css +0 -1
- package/dist/assets/NumberInput.css +0 -1
- package/dist/assets/ProgressBar.css +0 -1
- package/dist/assets/RoundGauge.css +0 -1
- package/dist/assets/Section.css +0 -1
- package/dist/assets/Slider.css +0 -1
- package/dist/assets/Stack.css +0 -1
- package/dist/assets/Table.css +0 -1
- package/dist/assets/Tabs.css +0 -1
- package/dist/assets/TextArea.css +0 -1
- package/dist/assets/Tooltip.css +0 -1
- package/dist/common/assets.d.ts +0 -4
- package/dist/common/assets.js +0 -25
- package/dist/common/collections.d.ts +0 -10
- package/dist/common/collections.js +0 -15
- package/dist/common/color.d.ts +0 -25
- package/dist/common/color.js +0 -69
- package/dist/common/constants.d.ts +0 -102
- package/dist/common/constants.js +0 -312
- package/dist/common/events.d.ts +0 -33
- package/dist/common/events.js +0 -147
- package/dist/common/exhaustive.js +0 -6
- package/dist/common/format.d.ts +0 -11
- package/dist/common/format.js +0 -114
- package/dist/common/fp.js +0 -9
- package/dist/common/hotkeys.d.ts +0 -25
- package/dist/common/hotkeys.js +0 -112
- package/dist/common/http.d.ts +0 -4
- package/dist/common/http.js +0 -10
- package/dist/common/keycodes.d.ts +0 -85
- package/dist/common/keycodes.js +0 -88
- package/dist/common/keys.js +0 -8
- package/dist/common/math.d.ts +0 -39
- package/dist/common/math.js +0 -41
- package/dist/common/perf.d.ts +0 -24
- package/dist/common/perf.js +0 -33
- package/dist/common/random.d.ts +0 -16
- package/dist/common/random.js +0 -18
- package/dist/common/react.d.ts +0 -23
- package/dist/common/react.js +0 -30
- package/dist/common/redux.d.ts +0 -64
- package/dist/common/redux.js +0 -72
- package/dist/common/storage.js +0 -124
- package/dist/common/string.d.ts +0 -65
- package/dist/common/string.js +0 -83
- package/dist/common/timer.d.ts +0 -18
- package/dist/common/timer.js +0 -28
- package/dist/common/type-utils.d.ts +0 -9
- package/dist/common/type-utils.js +0 -25
- package/dist/common/uuid.d.ts +0 -9
- package/dist/common/uuid.js +0 -10
- package/dist/components/AnimatedNumber.d.ts +0 -60
- package/dist/components/AnimatedNumber.js +0 -76
- package/dist/components/Autofocus.d.ts +0 -4
- package/dist/components/Autofocus.js +0 -17
- package/dist/components/Blink.d.ts +0 -26
- package/dist/components/Blink.js +0 -56
- package/dist/components/BlockQuote.d.ts +0 -3
- package/dist/components/BlockQuote.js +0 -13
- package/dist/components/BodyZoneSelector.d.ts +0 -28
- package/dist/components/BodyZoneSelector.js +0 -115
- package/dist/components/Box.d.ts +0 -91
- package/dist/components/Box.js +0 -133
- package/dist/components/Button.d.ts +0 -93
- package/dist/components/Button.js +0 -298
- package/dist/components/ByondUi.js +0 -73
- package/dist/components/Chart.d.ts +0 -28
- package/dist/components/Chart.js +0 -95
- package/dist/components/Collapsible.d.ts +0 -15
- package/dist/components/Collapsible.js +0 -27
- package/dist/components/ColorBox.d.ts +0 -8
- package/dist/components/ColorBox.js +0 -24
- package/dist/components/Dialog.d.ts +0 -24
- package/dist/components/Dialog.js +0 -67
- package/dist/components/Dimmer.d.ts +0 -3
- package/dist/components/Dimmer.js +0 -13
- package/dist/components/Divider.d.ts +0 -6
- package/dist/components/Divider.js +0 -22
- package/dist/components/DmIcon.d.ts +0 -31
- package/dist/components/DmIcon.js +0 -34
- package/dist/components/DraggableControl.js +0 -176
- package/dist/components/Dropdown.d.ts +0 -48
- package/dist/components/Dropdown.js +0 -152
- package/dist/components/FakeTerminal.js +0 -38
- package/dist/components/FitText.d.ts +0 -22
- package/dist/components/FitText.js +0 -63
- package/dist/components/Flex.d.ts +0 -93
- package/dist/components/Flex.js +0 -72
- package/dist/components/Icon.d.ts +0 -30
- package/dist/components/Icon.js +0 -51
- package/dist/components/Image.d.ts +0 -14
- package/dist/components/Image.js +0 -35
- package/dist/components/InfinitePlane.js +0 -139
- package/dist/components/Input.d.ts +0 -61
- package/dist/components/Input.js +0 -89
- package/dist/components/KeyListener.d.ts +0 -15
- package/dist/components/KeyListener.js +0 -23
- package/dist/components/Knob.d.ts +0 -49
- package/dist/components/Knob.js +0 -162
- package/dist/components/LabeledControls.d.ts +0 -11
- package/dist/components/LabeledControls.js +0 -39
- package/dist/components/LabeledList.d.ts +0 -57
- package/dist/components/LabeledList.js +0 -94
- package/dist/components/MenuBar.d.ts +0 -28
- package/dist/components/MenuBar.js +0 -174
- package/dist/components/Modal.d.ts +0 -3
- package/dist/components/Modal.js +0 -25
- package/dist/components/NoticeBox.d.ts +0 -20
- package/dist/components/NoticeBox.js +0 -49
- package/dist/components/NumberInput.d.ts +0 -45
- package/dist/components/NumberInput.js +0 -221
- package/dist/components/Popper.d.ts +0 -27
- package/dist/components/Popper.js +0 -177
- package/dist/components/ProgressBar.d.ts +0 -46
- package/dist/components/ProgressBar.js +0 -37
- package/dist/components/RestrictedInput.js +0 -155
- package/dist/components/RoundGauge.d.ts +0 -53
- package/dist/components/RoundGauge.js +0 -147
- package/dist/components/Section.d.ts +0 -63
- package/dist/components/Section.js +0 -62
- package/dist/components/Slider.d.ts +0 -46
- package/dist/components/Slider.js +0 -124
- package/dist/components/Stack.d.ts +0 -27
- package/dist/components/Stack.js +0 -67
- package/dist/components/StyleableSection.d.ts +0 -11
- package/dist/components/StyleableSection.js +0 -16
- package/dist/components/Table.d.ts +0 -29
- package/dist/components/Table.js +0 -67
- package/dist/components/Tabs.d.ts +0 -23
- package/dist/components/Tabs.js +0 -89
- package/dist/components/TextArea.d.ts +0 -39
- package/dist/components/TextArea.js +0 -118
- package/dist/components/TimeDisplay.js +0 -34
- package/dist/components/Tooltip.d.ts +0 -29
- package/dist/components/Tooltip.js +0 -83
- package/dist/components/TrackOutsideClicks.d.ts +0 -13
- package/dist/components/TrackOutsideClicks.js +0 -24
- package/dist/components/VirtualList.d.ts +0 -8
- package/dist/components/VirtualList.js +0 -34
- package/dist/components/index.js +0 -92
- package/dist/popper-CiqSDJTE.js +0 -906
- /package/{dist/components/index.d.ts → lib/components/index.ts} +0 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* All possible browser keycodes, in one file.
|
|
3
|
+
*
|
|
4
|
+
* @file
|
|
5
|
+
* @copyright 2020 Aleksej Komarov
|
|
6
|
+
* @license MIT
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const KEY_BACKSPACE = 8;
|
|
10
|
+
export const KEY_TAB = 9;
|
|
11
|
+
export const KEY_ENTER = 13;
|
|
12
|
+
export const KEY_SHIFT = 16;
|
|
13
|
+
export const KEY_CTRL = 17;
|
|
14
|
+
export const KEY_ALT = 18;
|
|
15
|
+
export const KEY_PAUSE = 19;
|
|
16
|
+
export const KEY_CAPSLOCK = 20;
|
|
17
|
+
export const KEY_ESCAPE = 27;
|
|
18
|
+
export const KEY_SPACE = 32;
|
|
19
|
+
export const KEY_PAGEUP = 33;
|
|
20
|
+
export const KEY_PAGEDOWN = 34;
|
|
21
|
+
export const KEY_END = 35;
|
|
22
|
+
export const KEY_HOME = 36;
|
|
23
|
+
export const KEY_LEFT = 37;
|
|
24
|
+
export const KEY_UP = 38;
|
|
25
|
+
export const KEY_RIGHT = 39;
|
|
26
|
+
export const KEY_DOWN = 40;
|
|
27
|
+
export const KEY_INSERT = 45;
|
|
28
|
+
export const KEY_DELETE = 46;
|
|
29
|
+
export const KEY_0 = 48;
|
|
30
|
+
export const KEY_1 = 49;
|
|
31
|
+
export const KEY_2 = 50;
|
|
32
|
+
export const KEY_3 = 51;
|
|
33
|
+
export const KEY_4 = 52;
|
|
34
|
+
export const KEY_5 = 53;
|
|
35
|
+
export const KEY_6 = 54;
|
|
36
|
+
export const KEY_7 = 55;
|
|
37
|
+
export const KEY_8 = 56;
|
|
38
|
+
export const KEY_9 = 57;
|
|
39
|
+
export const KEY_A = 65;
|
|
40
|
+
export const KEY_B = 66;
|
|
41
|
+
export const KEY_C = 67;
|
|
42
|
+
export const KEY_D = 68;
|
|
43
|
+
export const KEY_E = 69;
|
|
44
|
+
export const KEY_F = 70;
|
|
45
|
+
export const KEY_G = 71;
|
|
46
|
+
export const KEY_H = 72;
|
|
47
|
+
export const KEY_I = 73;
|
|
48
|
+
export const KEY_J = 74;
|
|
49
|
+
export const KEY_K = 75;
|
|
50
|
+
export const KEY_L = 76;
|
|
51
|
+
export const KEY_M = 77;
|
|
52
|
+
export const KEY_N = 78;
|
|
53
|
+
export const KEY_O = 79;
|
|
54
|
+
export const KEY_P = 80;
|
|
55
|
+
export const KEY_Q = 81;
|
|
56
|
+
export const KEY_R = 82;
|
|
57
|
+
export const KEY_S = 83;
|
|
58
|
+
export const KEY_T = 84;
|
|
59
|
+
export const KEY_U = 85;
|
|
60
|
+
export const KEY_V = 86;
|
|
61
|
+
export const KEY_W = 87;
|
|
62
|
+
export const KEY_X = 88;
|
|
63
|
+
export const KEY_Y = 89;
|
|
64
|
+
export const KEY_Z = 90;
|
|
65
|
+
export const KEY_F1 = 112;
|
|
66
|
+
export const KEY_F2 = 113;
|
|
67
|
+
export const KEY_F3 = 114;
|
|
68
|
+
export const KEY_F4 = 115;
|
|
69
|
+
export const KEY_F5 = 116;
|
|
70
|
+
export const KEY_F6 = 117;
|
|
71
|
+
export const KEY_F7 = 118;
|
|
72
|
+
export const KEY_F8 = 119;
|
|
73
|
+
export const KEY_F9 = 120;
|
|
74
|
+
export const KEY_F10 = 121;
|
|
75
|
+
export const KEY_F11 = 122;
|
|
76
|
+
export const KEY_F12 = 123;
|
|
77
|
+
export const KEY_SEMICOLON = 186;
|
|
78
|
+
export const KEY_EQUAL = 187;
|
|
79
|
+
export const KEY_COMMA = 188;
|
|
80
|
+
export const KEY_MINUS = 189;
|
|
81
|
+
export const KEY_PERIOD = 190;
|
|
82
|
+
export const KEY_SLASH = 191;
|
|
83
|
+
export const KEY_LEFT_BRACKET = 219;
|
|
84
|
+
export const KEY_BACKSLASH = 220;
|
|
85
|
+
export const KEY_RIGHT_BRACKET = 221;
|
|
86
|
+
export const KEY_QUOTE = 222;
|
|
@@ -20,27 +20,28 @@
|
|
|
20
20
|
*
|
|
21
21
|
*
|
|
22
22
|
*/
|
|
23
|
-
export
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
23
|
+
export enum KEY {
|
|
24
|
+
Alt = 'Alt',
|
|
25
|
+
Backspace = 'Backspace',
|
|
26
|
+
Control = 'Control',
|
|
27
|
+
Delete = 'Delete',
|
|
28
|
+
Down = 'ArrowDown',
|
|
29
|
+
End = 'End',
|
|
30
|
+
Enter = 'Enter',
|
|
31
|
+
Esc = 'Esc',
|
|
32
|
+
Escape = 'Escape',
|
|
33
|
+
Home = 'Home',
|
|
34
|
+
Insert = 'Insert',
|
|
35
|
+
Left = 'ArrowLeft',
|
|
36
|
+
PageDown = 'PageDown',
|
|
37
|
+
PageUp = 'PageUp',
|
|
38
|
+
Right = 'ArrowRight',
|
|
39
|
+
Shift = 'Shift',
|
|
40
|
+
Space = ' ',
|
|
41
|
+
Tab = 'Tab',
|
|
42
|
+
Up = 'ArrowUp',
|
|
43
43
|
}
|
|
44
|
+
|
|
44
45
|
/**
|
|
45
46
|
* ### isEscape
|
|
46
47
|
*
|
|
@@ -52,4 +53,6 @@ export declare enum KEY {
|
|
|
52
53
|
* @param key - the key to check, typically from event.key
|
|
53
54
|
* @returns true if key is Escape or Esc, false otherwise
|
|
54
55
|
*/
|
|
55
|
-
export
|
|
56
|
+
export function isEscape(key: string): boolean {
|
|
57
|
+
return key === KEY.Esc || key === KEY.Escape;
|
|
58
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Limits a number to the range between 'min' and 'max'.
|
|
3
|
+
*/
|
|
4
|
+
export function clamp(value, min, max) {
|
|
5
|
+
return value < min ? min : value > max ? max : value;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Limits a number between 0 and 1.
|
|
10
|
+
*/
|
|
11
|
+
export function clamp01(value) {
|
|
12
|
+
return value < 0 ? 0 : value > 1 ? 1 : value;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Scales a number to fit into the range between min and max.
|
|
17
|
+
*/
|
|
18
|
+
export function scale(value, min, max) {
|
|
19
|
+
return (value - min) / (max - min);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Robust number rounding, similar to PHP's round() function.
|
|
24
|
+
*
|
|
25
|
+
* @url https://stackoverflow.com/questions/53450248/how-to-round-in-javascript-like-php-do/54721202#54721202
|
|
26
|
+
*/
|
|
27
|
+
export function round(num, dec) {
|
|
28
|
+
const num_sign = num >= 0 ? 1 : -1;
|
|
29
|
+
return parseFloat(
|
|
30
|
+
(
|
|
31
|
+
Math.round(num * Math.pow(10, dec) + num_sign * 0.0001) /
|
|
32
|
+
Math.pow(10, dec)
|
|
33
|
+
).toFixed(dec),
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Returns a string representing a number in fixed point notation.
|
|
39
|
+
*/
|
|
40
|
+
export function toFixed(value, fractionDigits = 0) {
|
|
41
|
+
return Number(value).toFixed(Math.max(fractionDigits, 0));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Checks whether a value is within the provided range.
|
|
46
|
+
*
|
|
47
|
+
* Range is an array of two numbers, for example: [0, 15].
|
|
48
|
+
*/
|
|
49
|
+
export function inRange(value, range) {
|
|
50
|
+
return range && value >= range[0] && value <= range[1];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Walks over the object with ranges, comparing value against every range,
|
|
55
|
+
* and returns the key of the first matching range.
|
|
56
|
+
*
|
|
57
|
+
* Range is an array of two numbers, for example: [0, 15].
|
|
58
|
+
*/
|
|
59
|
+
export function keyOfMatchingRange(value, ranges) {
|
|
60
|
+
for (const rangeName of Object.keys(ranges)) {
|
|
61
|
+
const range = ranges[rangeName];
|
|
62
|
+
if (inRange(value, range)) {
|
|
63
|
+
return rangeName;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get number of digits following the decimal point in a number
|
|
70
|
+
*/
|
|
71
|
+
export function numberOfDecimalDigits(value) {
|
|
72
|
+
if (Math.floor(value) !== value) {
|
|
73
|
+
return value.toString().split('.')[1].length || 0;
|
|
74
|
+
}
|
|
75
|
+
return 0;
|
|
76
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ghetto performance measurement tools.
|
|
3
|
+
*
|
|
4
|
+
* Uses NODE_ENV to remove itself from production builds.
|
|
5
|
+
*
|
|
6
|
+
* @file
|
|
7
|
+
* @copyright 2020 Aleksej Komarov
|
|
8
|
+
* @license MIT
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const FPS = 60;
|
|
12
|
+
const FRAME_DURATION = 1000 / FPS;
|
|
13
|
+
|
|
14
|
+
// True if Performance API is supported
|
|
15
|
+
const supportsPerf = !!window.performance?.now;
|
|
16
|
+
// High precision markers
|
|
17
|
+
const hpMarkersByName: Record<string, number> = {};
|
|
18
|
+
// Low precision markers
|
|
19
|
+
const lpMarkersByName: Record<string, number> = {};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Marks a certain spot in the code for later measurements.
|
|
23
|
+
*/
|
|
24
|
+
function mark(name: string, timestamp?: number): void {
|
|
25
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
26
|
+
if (supportsPerf && !timestamp) {
|
|
27
|
+
hpMarkersByName[name] = performance.now();
|
|
28
|
+
}
|
|
29
|
+
lpMarkersByName[name] = timestamp || Date.now();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Calculates and returns the difference between two markers as a string.
|
|
35
|
+
*
|
|
36
|
+
* Use logger.log() to print the measurement.
|
|
37
|
+
*/
|
|
38
|
+
function measure(markerNameA: string, markerNameB: string): string | undefined {
|
|
39
|
+
if (process.env.NODE_ENV === 'production') return;
|
|
40
|
+
|
|
41
|
+
let markerA = hpMarkersByName[markerNameA];
|
|
42
|
+
let markerB = hpMarkersByName[markerNameB];
|
|
43
|
+
|
|
44
|
+
if (!markerA || !markerB) {
|
|
45
|
+
markerA = lpMarkersByName[markerNameA];
|
|
46
|
+
markerB = lpMarkersByName[markerNameB];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const duration = Math.abs(markerB - markerA);
|
|
50
|
+
|
|
51
|
+
return formatDuration(duration);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Formats a duration in milliseconds and frames.
|
|
56
|
+
*/
|
|
57
|
+
function formatDuration(duration: number): string {
|
|
58
|
+
const durationInFrames = duration / FRAME_DURATION;
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
duration.toFixed(duration < 10 ? 1 : 0) +
|
|
62
|
+
'ms ' +
|
|
63
|
+
'(' +
|
|
64
|
+
durationInFrames.toFixed(2) +
|
|
65
|
+
' frames)'
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const perf = {
|
|
70
|
+
mark,
|
|
71
|
+
measure,
|
|
72
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { clamp } from './math';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns random number between lowerBound exclusive and upperBound inclusive
|
|
5
|
+
*/
|
|
6
|
+
export function randomNumber(lowerBound: number, upperBound: number) {
|
|
7
|
+
return Math.random() * (upperBound - lowerBound) + lowerBound;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Returns random integer between lowerBound exclusive and upperBound inclusive
|
|
12
|
+
*/
|
|
13
|
+
export function randomInteger(lowerBound: number, upperBound: number) {
|
|
14
|
+
lowerBound = Math.ceil(lowerBound);
|
|
15
|
+
upperBound = Math.floor(upperBound);
|
|
16
|
+
return Math.floor(Math.random() * (upperBound - lowerBound) + lowerBound);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Returns random array element
|
|
21
|
+
*/
|
|
22
|
+
export const randomPick = <T>(array: T[]) => {
|
|
23
|
+
return array[Math.floor(Math.random() * array.length)];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Return 1 with probability P percent; otherwise 0
|
|
28
|
+
*/
|
|
29
|
+
export function randomProb(probability: number) {
|
|
30
|
+
const normalized = clamp(probability, 0, 100) / 100;
|
|
31
|
+
return Math.random() <= normalized;
|
|
32
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper for conditionally adding/removing classes in React
|
|
3
|
+
*/
|
|
4
|
+
export function classes(classNames: (string | BooleanLike)[]) {
|
|
5
|
+
let className = '';
|
|
6
|
+
for (let i = 0; i < classNames.length; i++) {
|
|
7
|
+
const part = classNames[i];
|
|
8
|
+
if (typeof part === 'string') {
|
|
9
|
+
className += part + ' ';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return className;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Normalizes children prop, so that it is always an array of VDom
|
|
17
|
+
* elements.
|
|
18
|
+
*/
|
|
19
|
+
export function normalizeChildren<T>(children: T | T[]) {
|
|
20
|
+
if (Array.isArray(children)) {
|
|
21
|
+
return children.flat().filter((value) => value) as T[];
|
|
22
|
+
}
|
|
23
|
+
if (typeof children === 'object') {
|
|
24
|
+
return [children];
|
|
25
|
+
}
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Shallowly checks if two objects are different.
|
|
31
|
+
* Credit: https://github.com/developit/preact-compat
|
|
32
|
+
*/
|
|
33
|
+
export function shallowDiffers(a: object, b: object) {
|
|
34
|
+
let i;
|
|
35
|
+
for (i in a) {
|
|
36
|
+
if (!(i in b)) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
for (i in b) {
|
|
41
|
+
if (a[i] !== b[i]) {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A common case in tgui, when you pass a value conditionally, these are
|
|
50
|
+
* the types that can fall through the condition.
|
|
51
|
+
*/
|
|
52
|
+
export type BooleanLike = number | boolean | null | undefined;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A helper to determine whether the object is renderable by React.
|
|
56
|
+
*/
|
|
57
|
+
export function canRender(value: unknown) {
|
|
58
|
+
return value !== undefined && value !== null && typeof value !== 'boolean';
|
|
59
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
export type Reducer<State = any, ActionType extends Action = AnyAction> = (
|
|
2
|
+
state: State | undefined,
|
|
3
|
+
action: ActionType,
|
|
4
|
+
) => State;
|
|
5
|
+
|
|
6
|
+
export type Store<State = any, ActionType extends Action = AnyAction> = {
|
|
7
|
+
dispatch: Dispatch<ActionType>;
|
|
8
|
+
getState: () => State;
|
|
9
|
+
subscribe: (listener: () => void) => void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type MiddlewareAPI<State = any, ActionType extends Action = AnyAction> = {
|
|
13
|
+
dispatch: Dispatch<ActionType>;
|
|
14
|
+
getState: () => State;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type Middleware = <State = any, ActionType extends Action = AnyAction>(
|
|
18
|
+
storeApi: MiddlewareAPI<State, ActionType>,
|
|
19
|
+
) => (next: Dispatch<ActionType>) => Dispatch<ActionType>;
|
|
20
|
+
|
|
21
|
+
export type Action<TType = any> = {
|
|
22
|
+
type: TType;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type AnyAction = Action & {
|
|
26
|
+
[extraProps: string]: any;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type Dispatch<ActionType extends Action = AnyAction> = (
|
|
30
|
+
action: ActionType,
|
|
31
|
+
) => void;
|
|
32
|
+
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
34
|
+
type StoreEnhancer = (createStoreFunction: Function) => Function;
|
|
35
|
+
|
|
36
|
+
type PreparedAction = {
|
|
37
|
+
meta?: any;
|
|
38
|
+
payload?: any;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Creates a Redux store.
|
|
43
|
+
*/
|
|
44
|
+
export function createStore<State, ActionType extends Action = AnyAction>(
|
|
45
|
+
reducer: Reducer<State, ActionType>,
|
|
46
|
+
enhancer?: StoreEnhancer,
|
|
47
|
+
): Store<State, ActionType> {
|
|
48
|
+
// Apply a store enhancer (applyMiddleware is one of them).
|
|
49
|
+
if (enhancer) {
|
|
50
|
+
return enhancer(createStore)(reducer);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let currentState: State;
|
|
54
|
+
const listeners: Array<() => void> = [];
|
|
55
|
+
|
|
56
|
+
const getState = (): State => currentState;
|
|
57
|
+
|
|
58
|
+
function subscribe(listener: () => void): void {
|
|
59
|
+
listeners.push(listener);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function dispatch(action: ActionType): void {
|
|
63
|
+
currentState = reducer(currentState, action);
|
|
64
|
+
for (let i = 0; i < listeners.length; i++) {
|
|
65
|
+
listeners[i]();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// This creates the initial store by causing each reducer to be called
|
|
70
|
+
// with an undefined state
|
|
71
|
+
dispatch({ type: '@@INIT' } as ActionType);
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
dispatch,
|
|
75
|
+
subscribe,
|
|
76
|
+
getState,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Creates a store enhancer which applies middleware to all dispatched
|
|
82
|
+
* actions.
|
|
83
|
+
*/
|
|
84
|
+
export function applyMiddleware(...middlewares: Middleware[]): StoreEnhancer {
|
|
85
|
+
return (
|
|
86
|
+
createStoreFunction: (reducer: Reducer, enhancer?: StoreEnhancer) => Store,
|
|
87
|
+
) => {
|
|
88
|
+
return (reducer, ...args): Store => {
|
|
89
|
+
const store = createStoreFunction(reducer, ...args);
|
|
90
|
+
|
|
91
|
+
let dispatch: Dispatch = () => {
|
|
92
|
+
throw new Error(
|
|
93
|
+
'Dispatching while constructing your middleware is not allowed.',
|
|
94
|
+
);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const storeApi: MiddlewareAPI = {
|
|
98
|
+
getState: store.getState,
|
|
99
|
+
dispatch: (action, ...args) => dispatch(action, ...args),
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const chain = middlewares.map((middleware) => middleware(storeApi));
|
|
103
|
+
dispatch = chain.reduceRight(
|
|
104
|
+
(next, middleware) => middleware(next),
|
|
105
|
+
store.dispatch,
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
...store,
|
|
110
|
+
dispatch,
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Combines reducers by running them in their own object namespaces as
|
|
118
|
+
* defined in reducersObj paramter.
|
|
119
|
+
*
|
|
120
|
+
* Main difference from redux/combineReducers is that it preserves keys
|
|
121
|
+
* in the state that are not present in the reducers object. This function
|
|
122
|
+
* is also more flexible than the redux counterpart.
|
|
123
|
+
*/
|
|
124
|
+
export function combineReducers(reducersObj: Record<string, Reducer>): Reducer {
|
|
125
|
+
const keys = Object.keys(reducersObj);
|
|
126
|
+
|
|
127
|
+
return (prevState = {}, action) => {
|
|
128
|
+
const nextState = { ...prevState };
|
|
129
|
+
let hasChanged = false;
|
|
130
|
+
|
|
131
|
+
for (const key of keys) {
|
|
132
|
+
const reducer = reducersObj[key];
|
|
133
|
+
const prevDomainState = prevState[key];
|
|
134
|
+
const nextDomainState = reducer(prevDomainState, action);
|
|
135
|
+
|
|
136
|
+
if (prevDomainState !== nextDomainState) {
|
|
137
|
+
hasChanged = true;
|
|
138
|
+
nextState[key] = nextDomainState;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return hasChanged ? nextState : prevState;
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* A utility function to create an action creator for the given action
|
|
148
|
+
* type string. The action creator accepts a single argument, which will
|
|
149
|
+
* be included in the action object as a field called payload. The action
|
|
150
|
+
* creator function will also have its toString() overriden so that it
|
|
151
|
+
* returns the action type, allowing it to be used in reducer logic that
|
|
152
|
+
* is looking for that action type.
|
|
153
|
+
*
|
|
154
|
+
* @param {string} type The action type to use for created actions.
|
|
155
|
+
* @param {any} prepare (optional) a method that takes any number of arguments
|
|
156
|
+
* and returns { payload } or { payload, meta }. If this is given, the
|
|
157
|
+
* resulting action creator will pass it's arguments to this method to
|
|
158
|
+
* calculate payload & meta.
|
|
159
|
+
*
|
|
160
|
+
* @public
|
|
161
|
+
*/
|
|
162
|
+
export function createAction<TAction extends string>(
|
|
163
|
+
type: TAction,
|
|
164
|
+
prepare?: (...args: any[]) => PreparedAction,
|
|
165
|
+
) {
|
|
166
|
+
function actionCreator(...args: any[]) {
|
|
167
|
+
let action: Action<TAction> & PreparedAction = { type };
|
|
168
|
+
|
|
169
|
+
if (prepare) {
|
|
170
|
+
const prepared = prepare(...args);
|
|
171
|
+
if (!prepared) {
|
|
172
|
+
throw new Error('prepare function did not return an object');
|
|
173
|
+
}
|
|
174
|
+
action = { ...action, ...prepared };
|
|
175
|
+
} else {
|
|
176
|
+
action.payload = args[0];
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return action;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
actionCreator.toString = () => type;
|
|
183
|
+
actionCreator.type = type;
|
|
184
|
+
actionCreator.match = (action) => action.type === type;
|
|
185
|
+
|
|
186
|
+
return actionCreator;
|
|
187
|
+
}
|