lotussim-shared 0.6.2

Shared types for LOTUS-Simulator scripts. Depend on `lotussim-script` instead of using this crate directly.
Documentation
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! Farben und Typen zum Zeichnen auf Script-Texturen.
//!
//! Colors and script texture drawing types.

use serde::{Deserialize, Serialize};

#[cfg(feature = "internal")]
use crate::content::ContentId;

/// Eine Farbe im RGBA-Format.
///
/// A color in the RGBA format.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct Color {
    /// Rotkanal, 0–255.
    ///
    /// Red channel, 0–255.
    pub r: u8,
    /// Grünkanal, 0–255.
    ///
    /// Green channel, 0–255.
    pub g: u8,
    /// Blaukanal, 0–255.
    ///
    /// Blue channel, 0–255.
    pub b: u8,
    /// Alphakanal, 0–255.
    ///
    /// Alpha channel, 0–255.
    pub a: u8,
}

impl Color {
    /// Deckendes Weiß.
    ///
    /// Opaque white.
    pub const WHITE: Self = Self::rgb(255, 255, 255);
    /// Deckendes Schwarz.
    ///
    /// Opaque black.
    pub const BLACK: Self = Self::rgb(0, 0, 0);
    /// Deckendes Rot.
    ///
    /// Opaque red.
    pub const RED: Self = Self::rgb(255, 0, 0);
    /// Deckendes Grün.
    ///
    /// Opaque green.
    pub const GREEN: Self = Self::rgb(0, 255, 0);
    /// Deckendes Blau.
    ///
    /// Opaque blue.
    pub const BLUE: Self = Self::rgb(0, 0, 255);
    /// Deckendes Gelb.
    ///
    /// Opaque yellow.
    pub const YELLOW: Self = Self::rgb(255, 255, 0);
    /// Deckendes Cyan.
    ///
    /// Opaque cyan.
    pub const CYAN: Self = Self::rgb(0, 255, 255);
    /// Deckendes Magenta.
    ///
    /// Opaque magenta.
    pub const MAGENTA: Self = Self::rgb(255, 0, 255);

    /// Erstellt eine deckende RGB-Farbe.
    ///
    /// Creates an opaque RGB color.
    pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
        Self::rgba(r, g, b, 255)
    }

    /// Erstellt eine RGBA-Farbe.
    ///
    /// Creates an RGBA color.
    pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self { r, g, b, a }
    }
}

impl From<u32> for Color {
    fn from(value: u32) -> Self {
        let r = ((value >> 24) & 0xFF) as u8;
        let g = ((value >> 16) & 0xFF) as u8;
        let b = ((value >> 8) & 0xFF) as u8;
        let a = (value & 0xFF) as u8;

        Color::rgba(r, g, b, a)
    }
}

impl From<Color> for u32 {
    fn from(value: Color) -> Self {
        let r = value.r as u32;
        let g = value.g as u32;
        let b = value.b as u32;
        let a = value.a as u32;

        (r << 24) | (g << 16) | (b << 8) | a
    }
}

#[cfg(feature = "bevy")]
mod _bevy {
    use super::*;

    impl From<bevy::color::Color> for Color {
        fn from(value: bevy::color::Color) -> Self {
            let value = value.to_srgba();

            Self::rgba(
                (value.red * 255.0) as u8,
                (value.green * 255.0) as u8,
                (value.blue * 255.0) as u8,
                (value.alpha * 255.0) as u8,
            )
        }
    }

    impl From<Color> for bevy::color::Color {
        fn from(value: Color) -> Self {
            bevy::color::Color::srgba(
                value.r as f32 / 255.0,
                value.g as f32 / 255.0,
                value.b as f32 / 255.0,
                value.a as f32 / 255.0,
            )
        }
    }
}

#[cfg(feature = "image")]
mod _image {
    use super::*;

    impl From<image::Rgba<u8>> for Color {
        fn from(value: image::Rgba<u8>) -> Self {
            Self::rgba(value[0], value[1], value[2], value[3])
        }
    }

    impl From<Color> for image::Rgba<u8> {
        fn from(value: Color) -> Self {
            [value.r, value.g, value.b, value.a].into()
        }
    }
}

/// Erstellung und Zeichenbefehle für Script-Texturen.
///
/// Script texture creation and drawing commands.
pub mod textures {
    use std::borrow::Cow;

    use glam::IVec2;
    use serde::{Deserialize, Serialize};

    use crate::{
        content::ContentId,
        math::{Rectangle, UVec2},
    };

    use super::Color;

    /// Optionen zum Erstellen einer Textur.
    ///
    /// Options for creating a texture.
    #[derive(Clone, Serialize, Deserialize)]
    pub struct TextureCreationOptions<'a> {
        /// Breite der Textur.
        ///
        /// The width of the texture.
        pub width: u32,
        /// Höhe der Textur.
        ///
        /// The height of the texture.
        pub height: u32,
        /// Texturdaten; derzeit Platzhalter für künftige Verwendung.
        ///
        /// The data of the texture. This is currently a placeholder for future use.
        pub data: Option<Cow<'a, [u8]>>,
        /// Ob für die Textur Mipmaps erzeugt werden sollen.
        ///
        /// Whether to generate mipmaps for the texture.
        pub mipmaps: bool,
    }

    impl From<(u32, u32)> for TextureCreationOptions<'_> {
        fn from((width, height): (u32, u32)) -> Self {
            Self {
                width,
                height,
                data: None,
                mipmaps: false,
            }
        }
    }

    /// Handle auf eine Textur.
    ///
    /// A handle to a texture.
    #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
    #[serde(transparent)]
    pub struct TextureHandle(u32);

    impl TextureHandle {
        /// Erstellt einen neuen Textur-Handle.
        ///
        /// Create a new texture handle.
        pub fn new(value: u32) -> Self {
            Self(value)
        }

        /// Gibt die ID des Textur-Handles zurück.
        ///
        /// Get the ID of the texture handle.
        pub fn id(&self) -> u32 {
            self.0
        }
    }

    /// Aktion, die auf einer Textur ausgeführt wird.
    ///
    /// An action to perform on a texture.
    #[derive(Clone, Serialize, Deserialize)]
    pub enum TextureAction {
        /// Füllt die Textur mit einer Farbe.
        ///
        /// Clear the texture with a color.
        Clear(Color),
        /// Zeichnet Pixel auf die Textur.
        ///
        /// Draw pixels on the texture.
        DrawPixels(Box<[DrawPixel]>),
        /// Zeichnet ein Rechteck auf die Textur.
        ///
        /// Draw a rectangle on the texture.
        DrawRect {
            /// Obere linke Ecke des Rechtecks.
            ///
            /// Top-left corner of the rectangle.
            start: UVec2,
            /// Untere rechte Ecke des Rechtecks.
            ///
            /// Bottom-right corner of the rectangle.
            end: UVec2,
            /// Füllfarbe des Rechtecks.
            ///
            /// Fill color of the rectangle.
            color: Color,
        },
        /// Zeichnet Text auf die Textur.
        ///
        /// Draw text on the texture.
        DrawText {
            /// Schrift zum Rendern des Textes.
            ///
            /// Font used to render the text.
            font: ContentId,
            /// Zu zeichnender Text.
            ///
            /// Text string to draw.
            text: String,
            /// Obere linke Textposition in Pixeln.
            ///
            /// Top-left position of the text in pixels.
            top_left: IVec2,
            /// Zusätzlicher Buchstabenabstand in Pixeln.
            ///
            /// Additional spacing between letters in pixels.
            letter_spacing: u32,
            /// Optionale Überschreibungsfarbe für den gesamten Text.
            ///
            /// Optional override color for the entire text.
            full_color: Option<Color>,
            /// Alpha-Mischmodus für den Text.
            ///
            /// Alpha blending mode for the text.
            alpha_mode: AlphaMode,
            /// Optionales Ziel-/Clip-Rechteck für den Text.
            ///
            /// Optional clipping/target rectangle for the text.
            target_rect: Option<Rectangle>,
        },
        // DrawTexture {
        //     texture: ContentId,
        //     options: DrawTextureOpts,
        // },
        /// Zeichnet eine Script-Textur auf die Textur.
        ///
        /// Draw a script texture on the texture.
        DrawScriptTexture {
            /// Handle der Quell-Script-Textur.
            ///
            /// Handle of the source script texture.
            handle: TextureHandle,
            /// Zeichenoptionen wie Quell- und Zielrechteck.
            ///
            /// Drawing options such as source and target rectangles.
            options: DrawTextureOpts,
        },
    }

    /// Steuert die Alpha-Behandlung beim Zeichnen.
    ///
    /// Controls how alpha (transparency) is handled when drawing.
    #[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
    pub enum AlphaMode {
        /// Textur wird deckend gezeichnet; Alphawerte werden ignoriert.
        /// Gezeichnete Pixel ersetzen vorhandene Pixel vollständig.
        ///
        /// The texture is drawn completely opaque, ignoring alpha values.
        /// Any pixels drawn will completely replace the existing pixels.
        #[default]
        Opaque,
        /// Alphawerte unter dem Schwellwert gelten als vollständig transparent,
        /// Werte ab dem Schwellwert als vollständig deckend.
        /// Der Schwellwert sollte zwischen 0.0 und 1.0 liegen.
        ///
        /// Alpha values below the threshold are considered fully transparent,
        /// while values above or equal to the threshold are considered fully opaque.
        /// The threshold should be between 0.0 and 1.0.
        Mask(f32),
        /// Alphawerte werden zum Mischen mit vorhandenen Pixeln verwendet.
        /// Der Alphakanal bestimmt die Deckkraft jedes gezeichneten Pixels.
        ///
        /// Alpha values are used to blend the new pixels with existing pixels.
        /// The alpha channel determines the opacity of each pixel being drawn.
        Blend,
    }

    /// Optionen zum Zeichnen einer Textur.
    ///
    /// Options for drawing a texture.
    #[derive(Default, Clone, Copy, Serialize, Deserialize)]
    pub struct DrawTextureOpts {
        /// Quellrechteck des zu zeichnenden Texturausschnitts.
        ///
        /// The source rectangle of the texture to draw.
        pub source_rect: Option<Rectangle>,
        /// Zielrechteck auf der Zieltextur.
        ///
        /// The target rectangle of the texture to draw to.
        pub target_rect: Option<Rectangle>,
    }

    /// Ein Pixel, der auf eine Textur gezeichnet wird.
    ///
    /// A pixel to draw on a texture.
    #[derive(Clone, Copy, Serialize, Deserialize)]
    pub struct DrawPixel {
        /// Position des Pixels.
        ///
        /// The position of the pixel.
        pub pos: UVec2,
        /// Farbe des Pixels.
        ///
        /// The color of the pixel.
        pub color: Color,
    }

    impl From<(UVec2, Color)> for DrawPixel {
        fn from((position, color): (UVec2, Color)) -> Self {
            Self {
                pos: position,
                color,
            }
        }
    }

    impl From<(u32, u32, Color)> for DrawPixel {
        fn from((x, y, color): (u32, u32, Color)) -> Self {
            Self {
                pos: UVec2 { x, y },
                color,
            }
        }
    }
}

/// Eigenschaften eines zeichenbaren Textur-Slots, der an Script-Variablen gebunden ist.
///
/// Properties of a drawable game texture slot bound to script variables.
#[cfg(feature = "internal")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DrawableTextureProperties {
    /// Texturbreite in Pixeln.
    ///
    /// Texture width in pixels.
    pub width: u32,
    /// Texturhöhe in Pixeln.
    ///
    /// Texture height in pixels.
    pub height: u32,
    /// Variablenname für die Textur-ID bzw. den Handle.
    ///
    /// Variable name holding the texture content id or handle.
    pub texture_variable_id: String,
    /// Schrift zum Zeichnen von Text auf die Textur.
    ///
    /// Font used when drawing text onto the texture.
    pub font: ContentId,
    /// Variablenname für den zu zeichnenden Text.
    ///
    /// Variable name holding the text to draw.
    pub text_variable_id: String,
    /// Ob eine benutzerdefinierte Textfarbe verwendet werden soll.
    ///
    /// Whether a custom text color should be applied.
    pub set_color: bool,
    /// Textfarbe, wenn [`set_color`](Self::set_color) aktiv ist.
    ///
    /// Text color when [`set_color`](Self::set_color) is true.
    pub color: Color,
    /// Horizontale Textausrichtung.
    ///
    /// Horizontal text alignment.
    pub horizontal_alignment: TextHorizontalAlignment,
    /// Vertikale Textausrichtung.
    ///
    /// Vertical text alignment.
    pub vertical_alignment: TextVerticalAlignment,
    /// Auflösung des Ausrichtungsrasters im Content-Tool.
    ///
    /// Alignment grid resolution used by the content tool.
    pub alignment_resolution: u8,
}

/// Horizontale Textausrichtung auf zeichenbaren Texturen.
///
/// Horizontal alignment of text on drawable textures.
#[cfg(feature = "internal")]
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub enum TextHorizontalAlignment {
    /// Horizontal zentriert.
    ///
    /// Centered horizontally.
    #[default]
    Center,
    /// Am linken Rand ausgerichtet.
    ///
    /// Aligned to the left edge.
    Left,
    /// Am rechten Rand ausgerichtet.
    ///
    /// Aligned to the right edge.
    Right,
    /// Ganzzahlig zentriert mit Tendenz nach links.
    ///
    /// Integer-centered with bias to the left.
    IntCenterLeft,
    /// Ganzzahlig zentriert mit Tendenz nach rechts.
    ///
    /// Integer-centered with bias to the right.
    IntCenterRight,
}

/// Vertikale Textausrichtung auf zeichenbaren Texturen.
///
/// Vertical alignment of text on drawable textures.
#[cfg(feature = "internal")]
#[derive(Debug, Copy, Clone, Default, Serialize, Deserialize)]
pub enum TextVerticalAlignment {
    /// Vertikal zentriert.
    ///
    /// Centered vertically.
    #[default]
    Center,
    /// Am oberen Rand ausgerichtet.
    ///
    /// Aligned to the top edge.
    Top,
    /// Am unteren Rand ausgerichtet.
    ///
    /// Aligned to the bottom edge.
    Bottom,
}