Skip to content

Commit

Permalink
Mixins, plus some changes to the library documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
cheese3660 committed Dec 14, 2023
1 parent 6a9447a commit 2d8ef7a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Writerside/topics/Libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ An example of using the library described earlier from another mod is as follows
$tau: $example-CONSTANT_B * 2; // Define tau as 2 pi, where pi is defined in the library.
```

If you want to use every library from a mod you can do `@use "mod_id:*"`

## Using Builtin Libraries

Using builtin libraries is exactly the same as using libraries from other mods, except the prefix is `builtin:`.
Expand All @@ -79,3 +81,5 @@ $half-pi: $PI/2; // Defines half-pi as pi/2
> unprefixed names, such as `$PI`, `$TAU`, `$E` in the case of `builtin:math`
>
{style="warning"}

Similary to using libraries from other mods, if you wish to use every builtin, you can do `@use "builtin:*"`
85 changes: 84 additions & 1 deletion Writerside/topics/Mixins.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,86 @@
# Mixins

Start typing here...
Mixins are a simple way of preventing yourself from repeating the same pattern over and over in your patches.
They are quite simple to declare, and to use.

## Declaring a mixin

Mixins are declared using the `@mixin` top level state followed by the name of the mixin, and then an `(`, an optional
list of mixin parameters, followed by a `)` and then a `{` and then any amount of [Selection Actions](Selection-Actions.md).
then a `}`.

For a very simple mixin declaration with no parameters:

```
@mixin scale-mass-by-two() {
mass: *2;
}
```

### Mixin Parameters

As mentioned before, mixins can have parameters, they are a comma separated list of `$parameter-name`, with optional
default values, which then make them of the form `$parameter-name:[default-value]`.

Here are a few examples of the above:

```
// Mixin with one parameter
@mixin scale-mass-by($N) {
mass: *$N;
}
// Mixin with one default parameter
@mixin scale-max-temp-by($N:2) {
maxTemp: *$N;
}
// Mixin with 2 parameters, one default
@mixin scale-mass-by-and-max-temp-by($mass-scale,$temp-scale:$mass-scale) {
mass: *$mass-scale;
maxTemp: *$temp-scale;
}
```

As you can see with the last one, the default values of parameters can depend on previous parameters, as default parameters
are computed from left to right at inclusion time.

## Using Mixins

Now that you have declared mixins, you need to be able to include them into your selection blocks, using the following
construct: `@include mixin-name([parameters])`, the parameters are a comma separated list of values corresponding to
the parameters defined in the mixin declaration, but they can also be named in the form of `$parameter-name:[passed-value]`.

What including a mixin does, is essentially copy and paste all the selection actions declared within the mixin into your
selection block in a sub environment, and replace the parameters in the mixin with the values you passed.

Here are a few examples of including the mixins defined above:

```
:parts #wheel_0v_rover {
@include scale-mass-by-two(); // Scale the mass by 2 using the parameterless mixin
@include scale-max-temp-by(); // Scale the max temp by 2, as the default value of $N is 2
}
:parts #wheel_1v_rover {
@include scale-mass-by(2); // Scale the mass by 2 using the parameterized mixin
@include scale-max-temp-by($N:2); // Scale the max temp by 2, and explicitly name the argument
}
:parts #wheel_2v_rover {
@include scale-mass-by-and-max-temp-by(2); // Scale the mass by 2, and the max temp by 2 as the $temp-scale is equal to $mass-scale by default
}
:parts #wheel_2v_rover_rugged {
@include scale-mass-by-and-max-temp-by(2,4); // Scale the mass by 2, and the max temp by 4
}
:parts #wheel_3v_rover {
@include scale-mass-by-and-max-temp-by($temp-scale:4,$mass-scale:2); // Scale the mass by 2, and the max temp by 4, passing the parameters in reverse
}
```


> Due to mixins copying the selection actions into a sub environment, any variables you declare in a mixin will only
> be available in the mixin, and not in the outer selection block.
>
{style="warning"}

0 comments on commit 2d8ef7a

Please sign in to comment.