-
Notifications
You must be signed in to change notification settings - Fork 0
/
scr_witchcraft_toolbox.gml
306 lines (290 loc) · 7.64 KB
/
scr_witchcraft_toolbox.gml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! Misc useful extensions to the GML standard library.
//# feather use syntax-errors
show_debug_message("enchanted with Witchcraft::toolbox by @katsaii");
/// All drawing beyond this point will use a solid colour. Might only
/// work for 2D games with an orthographic projection. Will need some
/// testing.
///
/// @param {Real} [colour]
/// The colour to draw with. Defaults to the current draw colour.
function wdraw_set_solid(colour) {
gml_pragma("forceinline");
gpu_set_fog(true, colour ?? draw_get_colour(), 0, 0);
}
/// Stops drawing with a solid colour.
function wdraw_reset_solid() {
gml_pragma("forceinline");
gpu_set_fog(false, c_black, 0, 1);
}
/// Draws part of a source surface region to part of a destination surface
/// region.
///
/// TODO: document
function wsurface_copy_part_pos_ext(
dest, destX1, destY1, destX2, destY2,
src, srcX1, srcY1, srcX2, srcY2, colour = c_white
) {
var reflectX = false;
var reflectY = false;
if (srcX2 < srcX1) {
var tx = srcX1;
srcX1 = srcX2;
srcX2 = tx;
reflectX = !reflectX;
}
if (srcY2 < srcY1) {
var ty = srcY1;
srcY1 = srcY2;
srcY2 = ty;
reflectY = !reflectY;
}
if (destX2 < destX1) {
var tx = destX1;
destX1 = destX2;
destX2 = tx;
reflectX = !reflectX;
}
if (destY2 < destY1) {
var ty = destY1;
destY1 = destY2;
destY2 = ty;
reflectY = !reflectY;
}
var scaleX = (destX2 - destX1) / (srcX2 - srcX1);
var scaleY = (destY2 - destY1) / (srcY2 - srcY1);
var posX, posY;
if (reflectX) {
posX = destX2;
scaleX *= -1;
} else {
posX = destX1;
}
if (reflectY) {
posY = destY2;
scaleY *= -1;
} else {
posY = destY1;
}
surface_set_target(dest);
draw_surface_part_ext(
src, srcX1, srcY1, srcX2 - srcX1, srcY2 - srcY1,
posX, posY, scaleX, scaleY, colour, 1
);
surface_reset_target();
}
/// Draws part of a source surface region scale the the whole size of a
/// destination surface.
///
/// TODO: document
function wsurface_copy_part_pos(
dest, src, srcX1, srcY1, srcX2, srcY2, colour = undefined
) {
wsurface_copy_part_pos_ext(
dest, 0, 0, surface_get_width(dest), surface_get_height(dest),
src, srcX1, srcY1, srcX2, srcY2, colour
);
}
/// Takes a rectangle and an aspect ratio, then returns the bounding box of
/// the largest rectangle with the expected aspect ratio that will fit
/// entirely into the target rectangle. The resulting bounding box will be
/// centred within the target rectangle.
///
/// @param {Real} x1
/// The left position of the target rectangle.
///
/// @param {Real} y1
/// The top position of the target rectangle.
///
/// @param {Real} x2
/// The right position of the target rectangle.
///
/// @param {Real} y2
/// The bottom position of the target rectangle.
///
/// @param {Real} width
/// The width component of the aspect ratio.
///
/// @param {Real} height
/// The height component of the aspect ratio.
///
/// @param {Function} [scaleModifier]
/// An optional function which can be used to modify the scale value of
/// the fitted rectangle. For example, clamping scale within a range,
/// or rounding it to whole numbers.
///
/// @return {Array<Real>}
function wrectangle_scale_to_fit(
x1, y1, x2, y2, width, height, scaleModifier = undefined
) {
if (x2 < x1) {
var t = x1;
x1 = x2;
x2 = t;
}
if (y2 < y1) {
var t = y1;
y1 = y2;
y2 = t;
}
var scaleWidth = width == 0 ? infinity : (x2 - x1) / width;
var scaleHeight = height == 0 ? infinity : (y2 - y1) / height;
var scale = min(scaleWidth, scaleHeight);
if (scaleModifier != undefined) {
scale = scaleModifier(scale);
}
var centreX = mean(x1, x2);
var centreY = mean(y1, y2);
var fitWidth = width * scale;
var fitHeight = height * scale;
var fitLeft = floor(centreX - fitWidth / 2);
var fitTop = floor(centreY - fitHeight / 2);
var fitRight = fitLeft + fitWidth;
var fitBottom = fitTop + fitHeight;
return [fitLeft, fitTop, fitRight, fitBottom];
}
/// Maps a number from one range to another.
///
/// @param {Real} value
/// The value to map.
///
/// @param {Real} min1
/// The minimum bound of the source range.
///
/// @param {Real} max1
/// The maximum bound of the source range.
///
/// @param {Real} min2
/// The minimum bound of the destination range.
///
/// @param {Real} max2
/// The maximum bound of the destination range.
function wmap_range(value, min1, max1, min2, max2) {
gml_pragma("forceinline");
var dx = max1 - min1;
var dy = max2 - min2;
return dx == 0 ? NaN : (value - min1) / dx * dy + min2;
}
/// Checks whether the device is a desktop OS.
///
/// @return {Bool}
function wos_is_pc() {
gml_pragma("forceinline");
if (os_browser == browser_not_a_browser) {
switch (os_type) {
case os_windows:
case os_linux:
case os_macosx:
return true;
}
}
return false;
}
/// Checks whether the device is a browser.
///
/// @return {Bool}
function wos_is_browser() {
gml_pragma("forceinline");
return os_browser != browser_not_a_browser;
}
/// Converts a value into a string using its raw representation.
///
/// @param {Any} value
/// The value to convert.
///
/// @return {String}
function wstring_repr(value) {
if (is_string(value)) {
return value;
}
if (wis_ref(value)) {
return string(ptr(value));
}
return string(value);
}
/// Returns whether this value is a reference type.
///
/// @param {Any} value
/// The argument to check.
///
/// @return {Bool}
function wis_ref(value) {
return is_struct(value) || is_method(value) || is_array(value);
}
/// Returns whether a value is allowed to be used as a conditional.
///
/// @param {Any} value
/// The argument to check.
///
/// @return {Bool}
function wis_conditional(value) {
try {
if (value) {
return true;
}
} catch (_) {
return false;
}
return true;
}
/// Returns whether a value is 'truthy'. This follows basic type checking
/// rules, except any value which would otherwise raise an exception when
/// used as a conditional will return `false` instead.
///
/// @param {Any} value
/// The argument to check.
///
/// @return {Bool}
function wis_truthy(value) {
try {
return value ? true : false;
} catch (_) {
return false;
}
}
/// Returns whether a value is 'falsy'. This follows basic type checking
/// rules, except any value which would otherwise raise an exception when
/// used as a conditional will return `true` instead.
///
/// @param {Any} value
/// The argument to check.
///
/// @return {Bool}
function wis_falsy(value) {
try {
return value ? false : true;
} catch (_) {
return true;
}
}
/// Computes the weighted sum of an arbitrary number of samples.
///
/// @param {Real} w1
/// The weight of the first sample.
///
/// @param {Real} v1
/// The value of the first sample.
///
/// @param {Real} w2
/// The weight of the second sample.
///
/// @param {Real} v2
/// The value of the second sample.
///
/// @param {Real} ...
/// Addtional samples in the same weight-value format.
///
/// @return {Real}
function wblend() {
var sum = 0;
var sum_weight = 0;
for (var i = 0; i < argument_count; i += 2) {
sum_weight += argument[i + 0];
}
if (sum_weight == 0) {
sum_weight = 1;
}
for (var i = 0; i < argument_count; i += 2) {
sum += argument[i + 0] / sum_weight * argument[i + 1];
}
return sum;
}