melonjs 20.12.0 → 20.13.0

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.
Files changed (120) hide show
  1. package/README.md +2 -1
  2. package/build/camera/camera2d.d.ts +2 -0
  3. package/build/camera/camera2d.d.ts.map +1 -1
  4. package/build/camera/effects/fade_effect.d.ts.map +1 -1
  5. package/build/geometries/ellipse.d.ts +3 -0
  6. package/build/geometries/ellipse.d.ts.map +1 -1
  7. package/build/geometries/line.d.ts.map +1 -1
  8. package/build/geometries/polygon.d.ts +2 -1
  9. package/build/geometries/polygon.d.ts.map +1 -1
  10. package/build/index.d.ts +13 -2
  11. package/build/index.d.ts.map +1 -1
  12. package/build/index.js +5409 -1500
  13. package/build/index.js.map +4 -4
  14. package/build/input/gamepad.d.ts.map +1 -1
  15. package/build/input/pointerevent.d.ts.map +1 -1
  16. package/build/level/tiled/TMXLayer.d.ts +48 -14
  17. package/build/level/tiled/TMXLayer.d.ts.map +1 -1
  18. package/build/level/tiled/TMXTileMap.d.ts +1 -1
  19. package/build/level/tiled/TMXTileMap.d.ts.map +1 -1
  20. package/build/level/tiled/TMXTileset.d.ts.map +1 -1
  21. package/build/level/tiled/TMXUtils.d.ts.map +1 -1
  22. package/build/level/tiled/renderer/TMXHexagonalRenderer.d.ts.map +1 -1
  23. package/build/level/tiled/renderer/TMXIsometricRenderer.d.ts.map +1 -1
  24. package/build/level/tiled/renderer/TMXObliqueRenderer.d.ts.map +1 -1
  25. package/build/level/tiled/renderer/TMXOrthogonalRenderer.d.ts.map +1 -1
  26. package/build/level/tiled/renderer/TMXRenderer.d.ts +1 -1
  27. package/build/level/tiled/renderer/TMXRenderer.d.ts.map +1 -1
  28. package/build/level/tiled/renderer/TMXStaggeredRenderer.d.ts.map +1 -1
  29. package/build/loader/loader.d.ts +55 -11
  30. package/build/loader/loader.d.ts.map +1 -1
  31. package/build/math/color.d.ts.map +1 -1
  32. package/build/math/math.d.ts +6 -1
  33. package/build/math/math.d.ts.map +1 -1
  34. package/build/math/observableVector2d.d.ts +6 -6
  35. package/build/math/observableVector2d.d.ts.map +1 -1
  36. package/build/math/observableVector3d.d.ts +8 -8
  37. package/build/math/observableVector3d.d.ts.map +1 -1
  38. package/build/particles/emitter.d.ts +15 -3
  39. package/build/particles/emitter.d.ts.map +1 -1
  40. package/build/particles/particle.d.ts +6 -0
  41. package/build/particles/particle.d.ts.map +1 -1
  42. package/build/particles/settings.d.ts +22 -5
  43. package/build/particles/settings.d.ts.map +1 -1
  44. package/build/physics/builtin/body.d.ts +14 -4
  45. package/build/physics/builtin/body.d.ts.map +1 -1
  46. package/build/physics/builtin/raycast.d.ts.map +1 -1
  47. package/build/physics/world.d.ts.map +1 -1
  48. package/build/renderable/container.d.ts +40 -6
  49. package/build/renderable/container.d.ts.map +1 -1
  50. package/build/renderable/frameAnimation.d.ts.map +1 -1
  51. package/build/renderable/renderable.d.ts.map +1 -1
  52. package/build/renderable/sprite.d.ts +4 -2
  53. package/build/renderable/sprite.d.ts.map +1 -1
  54. package/build/renderable/text/bitmaptext.d.ts +12 -3
  55. package/build/renderable/text/bitmaptext.d.ts.map +1 -1
  56. package/build/renderable/text/text.d.ts +147 -4
  57. package/build/renderable/text/text.d.ts.map +1 -1
  58. package/build/renderable/text/textmetrics.d.ts +10 -2
  59. package/build/renderable/text/textmetrics.d.ts.map +1 -1
  60. package/build/renderable/text/textstyle.d.ts.map +1 -1
  61. package/build/renderable/ui/panelpaint.d.ts +5 -1
  62. package/build/renderable/ui/panelpaint.d.ts.map +1 -1
  63. package/build/renderable/ui/progressbar.d.ts +267 -95
  64. package/build/renderable/ui/progressbar.d.ts.map +1 -1
  65. package/build/renderable/ui/uibaseelement.d.ts +436 -50
  66. package/build/renderable/ui/uibaseelement.d.ts.map +1 -1
  67. package/build/renderable/ui/uicheckbox.d.ts +383 -0
  68. package/build/renderable/ui/uicheckbox.d.ts.map +1 -0
  69. package/build/renderable/ui/uifocus.d.ts +6 -0
  70. package/build/renderable/ui/uifocus.d.ts.map +1 -0
  71. package/build/renderable/ui/uilabel.d.ts +31 -0
  72. package/build/renderable/ui/uilabel.d.ts.map +1 -0
  73. package/build/renderable/ui/uipanel.d.ts +589 -116
  74. package/build/renderable/ui/uipanel.d.ts.map +1 -1
  75. package/build/renderable/ui/uiskin.d.ts +25 -0
  76. package/build/renderable/ui/uiskin.d.ts.map +1 -0
  77. package/build/renderable/ui/uislider.d.ts +518 -0
  78. package/build/renderable/ui/uislider.d.ts.map +1 -0
  79. package/build/renderable/ui/uispriteelement.d.ts +161 -85
  80. package/build/renderable/ui/uispriteelement.d.ts.map +1 -1
  81. package/build/renderable/ui/uitextbutton.d.ts +369 -63
  82. package/build/renderable/ui/uitextbutton.d.ts.map +1 -1
  83. package/build/state/stage.d.ts.map +1 -1
  84. package/build/system/pool.d.ts.map +1 -1
  85. package/build/system/save.d.ts +2 -1
  86. package/build/system/save.d.ts.map +1 -1
  87. package/build/tweens/tween.d.ts +5 -2
  88. package/build/tweens/tween.d.ts.map +1 -1
  89. package/build/utils/fetchdata.d.ts.map +1 -1
  90. package/build/video/canvas/canvas_renderer.d.ts +0 -5
  91. package/build/video/canvas/canvas_renderer.d.ts.map +1 -1
  92. package/build/video/renderer.d.ts +20 -4
  93. package/build/video/renderer.d.ts.map +1 -1
  94. package/build/video/renderstate.d.ts +26 -3
  95. package/build/video/renderstate.d.ts.map +1 -1
  96. package/build/video/utils/tessellation.d.ts +11 -0
  97. package/build/video/utils/tessellation.d.ts.map +1 -1
  98. package/build/video/webgl/webgl_renderer.d.ts +3 -6
  99. package/build/video/webgl/webgl_renderer.d.ts.map +1 -1
  100. package/build/video/webgpu/batchers/primitive_batcher.d.ts.map +1 -1
  101. package/build/video/webgpu/webgpu_renderer.d.ts +3 -6
  102. package/build/video/webgpu/webgpu_renderer.d.ts.map +1 -1
  103. package/package.json +1 -1
  104. package/skills/melonjs/SKILL.md +3 -3
  105. package/skills/melonjs-audio/SKILL.md +7 -5
  106. package/skills/melonjs-camera-and-drawing/SKILL.md +41 -1
  107. package/skills/melonjs-effects-and-shaders/SKILL.md +1 -0
  108. package/skills/melonjs-events/SKILL.md +3 -1
  109. package/skills/melonjs-getting-started/SKILL.md +1 -2
  110. package/skills/melonjs-input/SKILL.md +38 -2
  111. package/skills/melonjs-loading-assets/SKILL.md +53 -12
  112. package/skills/melonjs-math/SKILL.md +14 -3
  113. package/skills/melonjs-particles-and-trails/SKILL.md +72 -8
  114. package/skills/melonjs-physics/SKILL.md +53 -4
  115. package/skills/melonjs-plugins/SKILL.md +7 -2
  116. package/skills/melonjs-renderables/SKILL.md +7 -0
  117. package/skills/melonjs-scenes-and-state/SKILL.md +15 -0
  118. package/skills/melonjs-sprites-and-animation/SKILL.md +14 -0
  119. package/skills/melonjs-tilemaps/SKILL.md +96 -2
  120. package/skills/melonjs-ui-and-text/SKILL.md +560 -23
@@ -1,84 +1,298 @@
1
1
  import type Camera2d from "../../camera/camera2d.ts";
2
+ import type Pointer from "../../input/pointer.ts";
2
3
  import type { Matrix3d } from "../../math/matrix3d.ts";
3
4
  import type { Vector3d } from "../../math/vector3d.ts";
4
5
  import type Renderer from "../../video/renderer.js";
6
+ import type Renderable from "../renderable.js";
5
7
  import type { Paint } from "./panelpaint.ts";
8
+ import type { UIJuiceSettings } from "./uibaseelement.ts";
6
9
  import UIBaseElement from "./uibaseelement.ts";
10
+ import type { UILabel } from "./uilabel.ts";
11
+ import type { UISkin } from "./uiskin.ts";
7
12
  /**
8
- * Everything {@link UIPanel} takes.
13
+ * The art of a skinned {@link UIPanel}: one atlas region, drawn as a
14
+ * nine-slice over the whole panel and stretched with it.
15
+ * @category UI
16
+ * @example
17
+ * const panel = new UIPanel(100, 80, {
18
+ * width: 320,
19
+ * height: 240,
20
+ * skin: { atlas, region: "panel_blue", insetx: 16, insety: 16 },
21
+ * });
22
+ */
23
+ export interface UIPanelSkin extends UISkin {
24
+ /** the name of the atlas region drawn as the background */
25
+ region: string;
26
+ }
27
+ /**
28
+ * How a {@link UIPanel} places its children.
29
+ *
30
+ * - `"none"`: each child stays where you put it
31
+ * - `"column"`: one above the other, top to bottom
32
+ * - `"row"`: side by side, left to right
33
+ * - `"grid"`: in rows of {@link UIPanelSettings.columns} equal-width cells
34
+ *
35
+ * Children are placed in the order they were added.
36
+ * @category UI
37
+ */
38
+ export type UIPanelLayout = "none" | "column" | "row" | "grid";
39
+ /**
40
+ * Where a {@link UIPanel} lines its children up across the layout.
41
+ *
42
+ * - `"start"`: the left of a column, the top of a row, the top left of a grid cell
43
+ * - `"center"`: the middle
44
+ * - `"end"`: the right of a column, the bottom of a row
45
+ * - `"stretch"`: resize each child to the full width of a column, the full
46
+ * height of a row, or the width of a grid cell. Only a {@link UITextButton}
47
+ * or a {@link UIPanel} is resized; any other child keeps its size and lines
48
+ * up at the start.
49
+ * @category UI
50
+ */
51
+ export type UIPanelAlign = "start" | "center" | "end" | "stretch";
52
+ /**
53
+ * Which ways a {@link UIPanel} scrolls when its content does not fit.
54
+ * @category UI
55
+ */
56
+ export type UIPanelScroll = "none" | "vertical" | "horizontal" | "both";
57
+ /**
58
+ * The settings a {@link UIPanel} takes. Only `width` and `height` are
59
+ * required; with nothing else the panel is an invisible, blocking box that
60
+ * holds children.
61
+ * @category UI
9
62
  */
10
- export interface UIPanelSettings {
63
+ export interface UIPanelSettings extends UIJuiceSettings {
11
64
  /** the panel's width, in pixels */
12
65
  width: number;
13
66
  /** the panel's height, in pixels */
14
67
  height: number;
15
68
  /**
16
- * The fill behind the children; `null` leaves it transparent.
69
+ * The background colour; `null` leaves it transparent.
17
70
  *
18
- * NOT `backgroundColor`, which a `Container` already has and which does
19
- * something else entirely: that one is handed to `clearColor` and wipes
20
- * the whole render target, so a panel setting it would repaint the
21
- * screen. The name here matches {@link ProgressBarSettings} instead.
71
+ * Use this, not `backgroundColor`: a `Container`'s `backgroundColor`
72
+ * clears the whole screen to that colour.
73
+ * @default null
22
74
  */
23
75
  fillColor?: Paint | null;
24
- /** drawn around the edge; `null` for none */
76
+ /**
77
+ * the colour of the border drawn around the edge; `null` for none
78
+ * @default null
79
+ */
25
80
  borderColor?: Paint | null;
26
- /** how thick that border is */
81
+ /**
82
+ * the border's thickness, in pixels, drawn inside the edge
83
+ * @default 1
84
+ */
27
85
  borderWidth?: number;
28
- /** corner radius; `0` is square */
86
+ /**
87
+ * the corner radius, in pixels; `0` is square. The fill, the border and
88
+ * the shadow all follow it.
89
+ * @default 0
90
+ */
29
91
  radius?: number;
30
92
  /**
31
- * How far the panel drifts up and down, in pixels. `0`, the default,
32
- * leaves it still.
33
- *
93
+ * The colour of a drop shadow under the panel; `null` for none. Its
94
+ * alpha sets how dark the shadow is, e.g. `"#00000066"`.
95
+ * @default null
96
+ */
97
+ shadowColor?: Paint | null;
98
+ /**
99
+ * how far right the shadow falls, in pixels; negative goes left
100
+ * @default 0
101
+ */
102
+ shadowOffsetX?: number;
103
+ /**
104
+ * how far down the shadow falls, in pixels; negative goes up
105
+ * @default 4
106
+ */
107
+ shadowOffsetY?: number;
108
+ /**
109
+ * how far the shadow's soft edge spreads, in pixels; `0` is a sharp edge
110
+ * @default 8
111
+ */
112
+ shadowBlur?: number;
113
+ /**
114
+ * How far the panel floats up and down, in pixels; `0` leaves it still.
34
115
  * See {@link UIPanel#bobAmplitude}.
116
+ * @default 0
35
117
  */
36
118
  bobAmplitude?: number;
37
119
  /**
38
- * How long one full up and down cycle takes, in milliseconds.
39
- *
120
+ * How long one full up and down float takes, in milliseconds.
40
121
  * See {@link UIPanel#bobPeriod}.
122
+ * @default 2000
41
123
  */
42
124
  bobPeriod?: number;
43
125
  /**
44
- * Whether the panel swallows pointer events aimed at what is behind it.
45
- *
46
- * `true` by default, which is what a dialog wants: a click on the panel,
47
- * or on the dimmed scene it covers, must not reach the game underneath.
48
- * Children are unaffected either way, because the dispatcher asks a child
49
- * before the container holding it.
126
+ * Whether the panel stops pointer events from reaching what is behind
127
+ * it: clicks, releases and hover on the panel itself, and the wheel over a
128
+ * scrolling panel. Widgets inside the panel still get their events.
50
129
  *
51
- * Set `false` for a panel that is only a backdrop, such as a HUD frame
52
- * drawn over a playfield that still has to receive clicks.
130
+ * Keep it `true` for a dialog. Set it `false` for a frame that only
131
+ * decorates, such as a HUD box over a playfield that still takes clicks.
132
+ * @default true
53
133
  */
54
134
  blocking?: boolean;
135
+ /**
136
+ * draw the background from an atlas region, as a nine-slice; a
137
+ * `fillColor` set as well is drawn over it
138
+ */
139
+ skin?: UIPanelSkin;
140
+ /** a title, centred in a band at the top; the content starts below it */
141
+ title?: string;
142
+ /**
143
+ * the title's font: a CSS font family, or the name of a loaded bitmap font
144
+ * @default "sans-serif"
145
+ */
146
+ titleFont?: string;
147
+ /**
148
+ * the title's text height, in pixels; the title band is 1.6 times this
149
+ * @default 20
150
+ */
151
+ titleFontSize?: number;
152
+ /**
153
+ * the title's colour
154
+ * @default "#ffffff"
155
+ */
156
+ titleFillStyle?: string;
157
+ /**
158
+ * Let the player move the panel by dragging it. It is moved by its title
159
+ * or its background, never through a widget on it; a scrolling panel is
160
+ * moved by its title and frame only, as a drag on its content scrolls.
161
+ * The panel comes to the front of its container when pressed.
162
+ * @default false
163
+ */
164
+ draggable?: boolean;
165
+ /**
166
+ * how the children are placed; see {@link UIPanelLayout}
167
+ * @default "none"
168
+ */
169
+ layout?: UIPanelLayout;
170
+ /**
171
+ * the number of cells in each row of a `"grid"` layout
172
+ * @default 2
173
+ */
174
+ columns?: number;
175
+ /**
176
+ * The space between the content and the panel's frame, in pixels: inside
177
+ * the border or the skin's insets, and below the title.
178
+ * @default 0
179
+ */
180
+ padding?: number;
181
+ /**
182
+ * the space between children in a layout, in pixels
183
+ * @default 8
184
+ */
185
+ gap?: number;
186
+ /**
187
+ * where children line up across the layout; see {@link UIPanelAlign}
188
+ * @default "start"
189
+ */
190
+ align?: UIPanelAlign;
191
+ /**
192
+ * Resize the panel to fit its laid out content: the height of a column or
193
+ * a grid, the width of a row, and the other side of a column or a row too
194
+ * unless `align` is `"stretch"`. Ignored by a scrolling panel.
195
+ * @default false
196
+ */
197
+ autoSize?: boolean;
198
+ /**
199
+ * Which ways the content scrolls when it does not fit: with the mouse
200
+ * wheel over the panel, and by dragging the content, with momentum.
201
+ * @default "none"
202
+ */
203
+ scroll?: UIPanelScroll;
204
+ /** called each time the content scrolls, with the new offsets in pixels */
205
+ onScroll?: (scrollX: number, scrollY: number, panel: UIPanel) => void;
206
+ /**
207
+ * the colour of the scrollbar, shown while the content does not fit
208
+ * @default "#00000059"
209
+ */
210
+ scrollbarColor?: Paint;
55
211
  }
56
212
  /**
57
- * A rectangular surface with a background, an optional border and children.
213
+ * A rectangular frame that holds other widgets: the base of a menu, a
214
+ * dialog, a pause screen or a HUD box. Add children to it with `addChild`;
215
+ * they are positioned relative to the panel's top left corner, drawn over its
216
+ * background, and move with it.
217
+ *
218
+ * ## Background
219
+ * Draw one with no assets: `fillColor`, `borderColor`, `borderWidth` and
220
+ * `radius` for rounded corners. Or give a nine-slice `skin` from an atlas,
221
+ * which stretches to any size with its corners kept sharp. With neither, the
222
+ * panel is invisible but still holds and lays out its children.
223
+ *
224
+ * ## Title
225
+ * `title` draws a line of text centred in a band at the top, styled with
226
+ * `titleFont`, `titleFontSize` and `titleFillStyle`. The content area starts
227
+ * below the band.
228
+ *
229
+ * ## Dragging
230
+ * With `draggable`, the player moves the panel by dragging its title or its
231
+ * background, never through a widget on it (a drag on the content of a
232
+ * scrolling panel scrolls it instead). A pressed panel comes to the front of
233
+ * its container, so the window last touched is on top.
234
+ *
235
+ * ## Blocking
236
+ * By default (`blocking: true`) the panel stops clicks, releases and hover
237
+ * on it from reaching whatever is behind it, and so does the wheel over a
238
+ * scrolling panel. Widgets inside it still get their events. Set
239
+ * `blocking: false` for a frame that only decorates.
240
+ *
241
+ * ## Layout
242
+ * `layout` places the children for you, in the order they were added:
243
+ * `"column"`, `"row"` or `"grid"` (with `columns` per row). `padding` is the
244
+ * space inside the frame, `gap` the space between children, and `align`
245
+ * lines them up across the layout (`"start"`, `"center"`, `"end"`, or
246
+ * `"stretch"`, which resizes buttons and panels to fill the width of a column
247
+ * or a cell, or the height of a row; other children keep their size).
248
+ * `autoSize` resizes the panel to fit what it holds. The layout runs again
249
+ * when a child is added or removed and when the panel is resized; call
250
+ * {@link UIPanel#relayout} after changing a child's size.
251
+ *
252
+ * ## Scrolling
253
+ * `scroll` (`"vertical"`, `"horizontal"` or `"both"`) crops the children to
254
+ * the content area and lets the player scroll whatever does not fit: with
255
+ * the mouse wheel, or by dragging the content, which carries on with
256
+ * momentum. A scrollbar shows how far along the content is.
257
+ * - From code: {@link UIPanel#scrollTo}, {@link UIPanel#scrollToChild}, or
258
+ * set {@link UIPanel#scrollX} and {@link UIPanel#scrollY}.
259
+ * {@link UIPanel#maxScrollX} and {@link UIPanel#maxScrollY} say how far it
260
+ * goes, and `onScroll` is called on every change.
261
+ * - A drag that starts on a button scrolls the panel and calls off the
262
+ * button's press, so it is not clicked on release. A slider keeps its own
263
+ * drag. A scrolling panel inside another scrolls on its own.
264
+ * - A widget given the keyboard or gamepad focus is scrolled into view.
265
+ * - {@link UIPanel#clearChildren} empties the panel and scrolls back to the
266
+ * start, ready to be filled again.
267
+ *
268
+ * With `layout: "none"`, place children at their position in the content, as
269
+ * if it were not scrolled, and call `relayout()` after moving one by hand.
58
270
  *
59
- * The thing every menu, dialog and HUD frame starts by writing: a rounded
60
- * rectangle, a fill, an outline, and some labels on top. One example in this
61
- * repository had three of them, each repeating the same half dozen renderer
62
- * calls, and each repeating the same correction for the way `setColor` folds
63
- * the global alpha in. That correction is the reason this is a widget rather
64
- * than a snippet: it is easy to leave out, and leaving it out does not fail,
65
- * it just renders a panel at the wrong opacity.
271
+ * ## Drop shadow
272
+ * `shadowColor` draws a soft shadow under the panel; its alpha sets how dark
273
+ * it is. `shadowOffsetX` and `shadowOffsetY` move it, `shadowBlur` softens
274
+ * its edge, and it follows `radius`. The shadow is drawn outside the panel's
275
+ * bounds and never takes the pointer.
66
276
  *
67
- * It is a {@link UIBaseElement}, so it is a container that carries pointer
68
- * events, and children added to it are drawn over its background and are
69
- * asked about a click before it is.
277
+ * ## Idle float
278
+ * `bobAmplitude` and `bobPeriod` make the panel drift slowly up and down,
279
+ * so a screen waiting for input does not look frozen. Its widgets stay
280
+ * clickable where they are drawn.
70
281
  *
71
- * It is floating, as every UI element is, and where it lands in the paint
72
- * order is the depth it is added at. In a world sorted on `"depth"`, which
73
- * is every `Camera3d` scene, that depth is read the other way round from
74
- * the 2D habit: a floating child is ranked by `|pos.z|` there and the
75
- * SMALLER magnitude draws on top, so a panel added at 9000 to sit above
76
- * everything sits behind everything instead, and a dialog there wants a
77
- * small depth, 0 or 1.
282
+ * ## Things to know
283
+ * - Like every UI element, a panel is floating: it is drawn in screen
284
+ * coordinates, over the scene. With a `Camera3d`, floating children are
285
+ * sorted the other way round: the SMALLER depth draws on top, so add a
286
+ * dialog at a depth of 0 or 1, not 9000.
287
+ * - A scrolling panel inside another clipping container (such as another
288
+ * scrolling panel) is cropped to both: its rows show only where the two
289
+ * overlap, on every renderer.
290
+ * @category UI
78
291
  * @example
292
+ * // a drawn dialog, with no assets
79
293
  * import { UIPanel, Text } from "melonjs";
80
294
  *
81
- * const panel = new UIPanel(cx - 200, cy - 120, {
295
+ * const dialog = new UIPanel(220, 140, {
82
296
  * width: 400,
83
297
  * height: 240,
84
298
  * fillColor: "#0a1526",
@@ -86,59 +300,203 @@ export interface UIPanelSettings {
86
300
  * borderWidth: 2,
87
301
  * radius: 14,
88
302
  * });
89
- * // positioned relative to the panel, not to the screen
90
- * panel.addChild(new Text(200, 24, { text: "PAUSED", textAlign: "center" }));
91
- * app.world.addChild(panel);
92
- * @category UI
303
+ * // relative to the panel's top left corner, not to the screen
304
+ * dialog.addChild(new Text(200, 24, {
305
+ * font: "sans-serif",
306
+ * size: 32,
307
+ * text: "PAUSED",
308
+ * textAlign: "center",
309
+ * }));
310
+ * app.world.addChild(dialog);
311
+ * @example
312
+ * // a skinned options menu: a column of widgets, sized to fit
313
+ * import { UIPanel, UISlider, UICheckbox, UITextButton } from "melonjs";
314
+ *
315
+ * const menu = new UIPanel(240, 100, {
316
+ * width: 320,
317
+ * height: 100, // autoSize sets the height
318
+ * skin: { atlas, region: "panel_brown" },
319
+ * title: "Options",
320
+ * layout: "column",
321
+ * padding: 16,
322
+ * gap: 12,
323
+ * align: "stretch",
324
+ * autoSize: true,
325
+ * });
326
+ * menu.addChild(new UISlider(0, 0, {
327
+ * label: "Music",
328
+ * value: 0.8,
329
+ * onChange: (value) => { musicVolume = value; },
330
+ * }));
331
+ * menu.addChild(new UICheckbox(0, 0, {
332
+ * label: "Show hints",
333
+ * checked: true,
334
+ * onChange: (checked) => { showHints = checked; },
335
+ * }));
336
+ * // stretched to the full width of the column
337
+ * menu.addChild(new UITextButton(0, 0, {
338
+ * text: "Back",
339
+ * skin: { atlas, normal: "button", pressed: "button_pressed" },
340
+ * onClick: () => { app.world.removeChild(menu); },
341
+ * }));
342
+ * app.world.addChild(menu);
343
+ * @example
344
+ * // a scrolling grid of level buttons
345
+ * const levels = new UIPanel(160, 60, {
346
+ * width: 480,
347
+ * height: 360,
348
+ * fillColor: "#1b2333",
349
+ * radius: 10,
350
+ * title: "Select a level",
351
+ * layout: "grid",
352
+ * columns: 4,
353
+ * padding: 12,
354
+ * gap: 10,
355
+ * align: "stretch",
356
+ * scroll: "vertical",
357
+ * });
358
+ * const buttons = [];
359
+ * for (let i = 1; i <= 40; i++) {
360
+ * const button = new UITextButton(0, 0, {
361
+ * text: String(i),
362
+ * height: 64,
363
+ * onClick: () => { startLevel(i); },
364
+ * });
365
+ * buttons.push(button);
366
+ * levels.addChild(button);
367
+ * }
368
+ * app.world.addChild(levels);
369
+ * // open on the last level unlocked, with 8 pixels to spare around it
370
+ * levels.scrollToChild(buttons[lastUnlocked - 1], 8);
371
+ * @example
372
+ * // a draggable window with a title
373
+ * const inventory = new UIPanel(40, 40, {
374
+ * width: 260,
375
+ * height: 180,
376
+ * fillColor: "#2b2f3a",
377
+ * borderColor: "#8899aa",
378
+ * radius: 6,
379
+ * title: "Inventory",
380
+ * titleFontSize: 18,
381
+ * layout: "column",
382
+ * padding: 10,
383
+ * draggable: true,
384
+ * });
385
+ * inventory.addChild(new Text(0, 0, { font: "sans-serif", size: 16, text: "3 potions" }));
386
+ * app.world.addChild(inventory);
387
+ * @example
388
+ * // a drop shadow, soft and a little below
389
+ * const card = new UIPanel(300, 200, {
390
+ * width: 200,
391
+ * height: 120,
392
+ * fillColor: "#ffffff",
393
+ * radius: 12,
394
+ * shadowColor: "#00000066", // the alpha sets how dark
395
+ * shadowOffsetX: 2,
396
+ * shadowOffsetY: 6,
397
+ * shadowBlur: 12,
398
+ * });
399
+ * @example
400
+ * // a HUD frame that lets clicks through to the game behind it
401
+ * const hud = new UIPanel(8, 8, {
402
+ * width: 220,
403
+ * height: 48,
404
+ * fillColor: "#00000080",
405
+ * radius: 8,
406
+ * blocking: false,
407
+ * });
93
408
  */
94
409
  export default class UIPanel extends UIBaseElement {
95
- /** the fill behind the children; `null` leaves it transparent */
410
+ /** the background colour; `null` leaves it transparent */
96
411
  fillColor: Paint | null;
97
- /** drawn around the edge; `null` for none */
412
+ /** the colour of the border around the edge; `null` for none */
98
413
  borderColor: Paint | null;
99
- /** how thick that border is */
414
+ /** the border's thickness, in pixels */
100
415
  borderWidth: number;
101
- /** corner radius; `0` is square */
416
+ /** the corner radius, in pixels; `0` is square */
102
417
  radius: number;
103
- /** whether it swallows pointer events aimed at what is behind it */
418
+ /** the colour of the drop shadow, its alpha setting how dark; `null` for none */
419
+ shadowColor: Paint | null;
420
+ /** how far right the shadow falls, in pixels */
421
+ shadowOffsetX: number;
422
+ /** how far down the shadow falls, in pixels */
423
+ shadowOffsetY: number;
424
+ /** how far the shadow's soft edge spreads, in pixels */
425
+ shadowBlur: number;
426
+ /**
427
+ * Whether the panel stops pointer events on it from reaching what is
428
+ * behind it. Widgets inside still get theirs.
429
+ * @default true
430
+ */
104
431
  blocking: boolean;
105
432
  /**
106
- * How far the panel drifts up and down, in pixels. `0`, the default,
107
- * leaves it still.
433
+ * How far the panel floats up and down, in pixels; `0` leaves it still.
108
434
  *
109
- * The slow float an idle dialogue or pause panel has in a lot of games,
110
- * which is there so a screen waiting for input does not look frozen. It
111
- * is a sine, so this is the distance to either extreme and the panel
112
- * covers twice it over a cycle; 2 to 4 is the usual range, and it starts
113
- * by rising rather than sinking.
114
- *
115
- * Do not go much below 2. The offset is a whole number of pixels, so an
116
- * amplitude of 1 has three positions to move between and the panel
117
- * twitches rather than drifts.
118
- *
119
- * The panel is CLICKABLE where it is drawn: the offset goes into
120
- * {@link UIPanel#getAbsolutePosition}, which is what the bounds pipeline
121
- * reads, so the hit area of the panel and of every widget inside it
122
- * follows the float at any depth. `pos` is deliberately left alone, so a
123
- * caller still reads and writes the resting place rather than a value
124
- * that moves under them.
125
- *
126
- * The bounds are rebuilt only on the frames the offset actually changes,
127
- * so a panel with no bob costs nothing.
435
+ * A slow float keeps a dialog waiting for input from looking frozen. The
436
+ * panel rises this far above its position and sinks this far below it;
437
+ * 2 to 4 works well, as smaller values step a pixel at a time and look
438
+ * jerky. The panel, and every widget in it, is clickable where it is
439
+ * drawn, while `pos` keeps its resting place.
128
440
  * @default 0
441
+ * @example
442
+ * dialog.bobAmplitude = 3;
443
+ * dialog.bobPeriod = 2500;
129
444
  */
130
445
  bobAmplitude: number;
131
446
  /**
132
- * How long one full up and down cycle takes, in milliseconds.
133
- *
134
- * Anything at or below zero turns the bob off rather than dividing by it.
135
- * That is not politeness: the offset would be `NaN`, and one `translate`
136
- * by `NaN` writes it into the transform, where every batched vertex under
137
- * it inherits the poison and the GPU clips the lot away. A panel that
138
- * silently vanished would be a long way from an obviously bad period.
447
+ * How long one full up and down float takes, in milliseconds. `0` or
448
+ * less turns the float off.
139
449
  * @default 2000
140
450
  */
141
451
  bobPeriod: number;
452
+ /** the background art, when the panel is skinned */
453
+ readonly skin: UIPanelSkin | undefined;
454
+ /** the title's text object, when the panel has a title */
455
+ readonly title: UILabel | undefined;
456
+ /** how the children are placed; see {@link UIPanelLayout}. Call {@link UIPanel#relayout} after changing it. */
457
+ layout: UIPanelLayout;
458
+ /** the number of cells in each row of a `"grid"` layout */
459
+ columns: number;
460
+ /** the space between the content and the frame, in pixels */
461
+ padding: number;
462
+ /** the space between children in a layout, in pixels */
463
+ gap: number;
464
+ /** where children line up across the layout; see {@link UIPanelAlign} */
465
+ align: UIPanelAlign;
466
+ /** whether the panel resizes to fit its laid out content */
467
+ autoSize: boolean;
468
+ /** which ways the content scrolls; see {@link UIPanelScroll} */
469
+ scroll: UIPanelScroll;
470
+ /**
471
+ * Called each time the content scrolls, with the new offsets in pixels.
472
+ * @example
473
+ * // fade an arrow out once the list is scrolled to the bottom
474
+ * list.onScroll = (scrollX, scrollY, panel) => {
475
+ * moreArrow.setOpacity(scrollY < panel.maxScrollY ? 1 : 0);
476
+ * };
477
+ */
478
+ onScroll: ((scrollX: number, scrollY: number, panel: UIPanel) => void) | undefined;
479
+ /** the colour of the scrollbar, shown while the content does not fit */
480
+ scrollbarColor: Paint;
481
+ /** @ignore */
482
+ private readonly _skin;
483
+ /** the title band's height, in pixels */
484
+ private readonly _titleHeight;
485
+ /** how far the content is scrolled */
486
+ private _scrollX;
487
+ private _scrollY;
488
+ /** how far it can be */
489
+ private _maxScrollX;
490
+ private _maxScrollY;
491
+ /** set while a layout pass runs, so the resize it makes does not start another */
492
+ private _layingOut;
493
+ /** a press on the content that may become a scroll */
494
+ private _press;
495
+ /** the fling speed, in pixels per millisecond */
496
+ private _velocityX;
497
+ private _velocityY;
498
+ /** whether the current press moves the panel */
499
+ private _moving;
142
500
  /**
143
501
  * @param x - position of the panel's left edge
144
502
  * @param y - position of the panel's top edge
@@ -158,55 +516,170 @@ export default class UIPanel extends UIBaseElement {
158
516
  */
159
517
  draw(renderer: Renderer, viewport?: Camera2d): void;
160
518
  /**
161
- * Where the panel IS, bob included.
162
- *
163
- * THE hook that makes a floating panel honest. Both bounds paths funnel
164
- * through this one method, and a child's absolute position is the sum of
165
- * the chain above it, so adding the offset here reaches the panel's own
166
- * hit area and every descendant's, at any depth, without any of them
167
- * knowing a bob exists.
168
- *
169
- * Not `currentTransform`, which was the obvious guess: that reaches a
170
- * renderable's OWN bounds and not its children's, so a bobbing panel's
171
- * swallow rectangle would drift off the widgets sitting in it. And not a
172
- * write to `pos`, which would reach everything correctly but silently
173
- * overwrite a caller's own write on the very next frame.
174
- *
175
- * {@link Particle} overrides this method for the same reason, its
176
- * placement not being in its `pos` chain either.
177
- * @returns the absolute position, offset by the bob
519
+ * The panel's absolute position, including the idle float, so the hit
520
+ * areas of the panel and of every widget in it follow the float.
521
+ * @returns the absolute position, offset by the float
178
522
  */
179
523
  getAbsolutePosition(): Vector3d;
180
524
  /**
181
- * The matrix form of the same offset.
182
- *
183
- * Without it `getWorldTransform()` and `getAbsolutePosition()` disagree
184
- * about a bobbing panel, and the contract on those two says that for a
185
- * `Container` they coincide.
525
+ * The panel's local transform, including the idle float, so it agrees
526
+ * with {@link UIPanel#getAbsolutePosition}.
186
527
  * @param out - matrix to write into
187
528
  * @returns `out`, for chaining
188
529
  */
189
530
  protected getLocalTransform(out: Matrix3d): Matrix3d;
190
531
  /**
191
- * Resize this panel's bounds, and refresh its children's.
192
- *
193
- * `Container` does those two jobs in ONE loop, both gated on
194
- * `enableChildBoundsUpdate`, and a panel wants exactly one of them. The
195
- * flag is off here so that a label overhanging the edge cannot grow the
196
- * rectangle a blocking panel swallows clicks inside, and turning it off
197
- * took the other job with it: a child's bounds are absolute, worked out
198
- * once through `getAbsolutePosition()` and then cached, so moving the
199
- * panel left every child's HIT AREA behind at the old place while the
200
- * whole panel drew at the new one. A dialog slid or snapped into position
201
- * after it was built had buttons that only answered where they used to
202
- * be.
203
- *
204
- * This costs nothing per frame: `pos` calls `updateBounds` when it is
205
- * written, so the walk happens when the panel actually moves.
532
+ * Update this panel's bounds, and its children's, so their hit areas
533
+ * follow the panel when it moves. The panel's own bounds are its size,
534
+ * never grown by a child hanging over the edge.
206
535
  * @param absolute - update in world absolute coordinates
207
536
  * @returns this panel's bounds
208
537
  */
209
538
  updateBounds(absolute?: boolean): import("../../index.ts").Bounds;
539
+ /**
540
+ * Place the children again, and work out how far the content scrolls.
541
+ *
542
+ * It runs by itself when a child is added or removed and when the panel
543
+ * is resized, not every frame. Call it after a child changes size, after
544
+ * changing `layout`, `columns`, `padding`, `gap` or `align`, or after
545
+ * moving children by hand in a scrolling panel.
546
+ * @returns this panel
547
+ * @example
548
+ * menu.gap = 4;
549
+ * menu.relayout();
550
+ */
551
+ relayout(): this;
552
+ /**
553
+ * How far the content is scrolled to the right, in pixels, from `0` to
554
+ * {@link UIPanel#maxScrollX}. Setting it scrolls, clamped to that range.
555
+ * @default 0
556
+ */
557
+ get scrollX(): number;
558
+ set scrollX(value: number);
559
+ /**
560
+ * How far the content is scrolled down, in pixels, from `0` to
561
+ * {@link UIPanel#maxScrollY}. Setting it scrolls, clamped to that range.
562
+ * @default 0
563
+ * @example
564
+ * // back to the top
565
+ * list.scrollY = 0;
566
+ */
567
+ get scrollY(): number;
568
+ set scrollY(value: number);
569
+ /**
570
+ * How far the content can scroll to the right, in pixels; `0` when it
571
+ * fits or the panel does not scroll that way. Read only.
572
+ */
573
+ get maxScrollX(): number;
574
+ /**
575
+ * How far the content can scroll down, in pixels; `0` when it fits or
576
+ * the panel does not scroll that way. Read only.
577
+ */
578
+ get maxScrollY(): number;
579
+ /**
580
+ * Scroll the content to an offset, clamped to `0` up to
581
+ * {@link UIPanel#maxScrollX} and {@link UIPanel#maxScrollY}, with no
582
+ * animation. A fling still coasting from a drag stops.
583
+ *
584
+ * Scrolling moves the children, so a child's `pos` is where it is drawn.
585
+ * @param x - the horizontal offset, in pixels
586
+ * @param y - the vertical offset, in pixels
587
+ * @returns this panel
588
+ * @example
589
+ * // jump to the end of a chat log
590
+ * log.scrollTo(0, log.maxScrollY);
591
+ */
592
+ scrollTo(x: number, y: number): this;
593
+ /**
594
+ * move the content to an offset, clamped, leaving a fling alone: the
595
+ * scrolling the panel does itself (fling, drag, wheel, relayout)
596
+ * @param x - the horizontal offset, in pixels
597
+ * @param y - the vertical offset, in pixels
598
+ * @returns this panel
599
+ * @ignore
600
+ */
601
+ private moveContent;
602
+ /**
603
+ * Scroll just enough to bring a child fully into view; nothing moves if
604
+ * it already is. A widget that is not a direct child of this panel is
605
+ * ignored.
606
+ * @param child - one of this panel's children
607
+ * @param margin - room to leave around it, in pixels, such as for a
608
+ * focus ring
609
+ * @returns this panel
610
+ * @example
611
+ * // show the selected save slot, with 8 pixels to spare
612
+ * slots.scrollToChild(selectedSlot, 8);
613
+ */
614
+ scrollToChild(child: Renderable, margin?: number): this;
615
+ /**
616
+ * Whether the panel shows its children at a point: anywhere for a panel
617
+ * that does not scroll, only in its content area for one that does.
618
+ * @param x - the point, in screen pixels for a floating panel
619
+ * @param y - the point
620
+ * @returns false where the children are cropped away
621
+ */
622
+ clipContains(x: number, y: number): boolean;
623
+ private readonly _onPressMove;
624
+ private readonly _onPressEnd;
625
+ private readonly _onWheel;
626
+ /**
627
+ * Bring a draggable panel to the front when it is grabbed, and start
628
+ * watching a press on the content of a scrolling one.
629
+ * @ignore
630
+ */
631
+ clicked(event: Pointer): boolean | void;
632
+ /**
633
+ * Move a draggable panel, unless the press began on the content of a
634
+ * scrolling one.
635
+ * @ignore
636
+ */
637
+ pointerMove(event: Pointer): void;
638
+ /** set while the panel is emptied, so it does not lay out each step */
639
+ private _clearing;
640
+ /**
641
+ * Remove every child and scroll back to the start, ready to fill the
642
+ * panel again.
643
+ * @example
644
+ * // refill a scrolling list
645
+ * list.clearChildren();
646
+ * for (const name of names) {
647
+ * list.addChild(new Text(0, 0, { font: "sans-serif", size: 16, text: name }));
648
+ * }
649
+ */
650
+ clearChildren(): void;
651
+ /**
652
+ * Lay a new child out with the others.
653
+ * @ignore
654
+ */
655
+ addChild(child: Renderable, z?: number): Renderable;
656
+ /**
657
+ * Lay a new child out with the others.
658
+ * @ignore
659
+ */
660
+ addChildAt(child: Renderable, index: number): Renderable;
661
+ /**
662
+ * Let the layout close the gap.
663
+ * @ignore
664
+ */
665
+ removeChildNow(child: Renderable, keepalive?: boolean): void;
666
+ /**
667
+ * Resize the panel, and lay the children out again.
668
+ * @param w - the new width
669
+ * @param h - the new height
670
+ * @returns this panel
671
+ */
672
+ resize(w: number, h: number): this;
673
+ /**
674
+ * Listen for the wheel, and for hover a blocking panel swallows.
675
+ * @ignore
676
+ */
677
+ onActivateEvent(): void;
678
+ /**
679
+ * Stop listening, and drop a press or a fling in progress.
680
+ * @ignore
681
+ */
682
+ onDeactivateEvent(): void;
210
683
  /**
211
684
  * Consume a click on the panel itself, unless told not to.
212
685
  *