Skip to content

Commit 86e2c5b

Browse files
add code examples to classes, as reusable component
1 parent 96727f8 commit 86e2c5b

File tree

10 files changed

+82
-27
lines changed

10 files changed

+82
-27
lines changed

TODO.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Generate pages with list of OOP functions by Element type for server and client
2+
https://wiki.multitheftauto.com/wiki/OOP_server
3+
https://wiki.multitheftauto.com/wiki/OOP_client

classes/Matrix/Matrix.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,13 @@ name: 'matrix'
22
description: |
33
Matrices are one of the most powerful features of MTA [OOP](/OOP). We did have a presence of Matrices before with [getElementMatrix](/getElementMatrix), but we were given an ugly disgusting table to play with. Now, with the new Matrix class, we can make and magically manipulate Matrices.
44
5+
examples:
6+
- path: examples/matrix-1.lua
7+
description: >
8+
Say you wanted to create a bin - object 1337 - two units in front of a player. You don't want to manually do trigonometry and you don't want to play with the complicated tables. You just want to get the position two units in front of the player whilst taking into account the rotation of the player. You only need to use Matrix.getForward, Matrix.getPosition and getElementMatrix. It's just:
9+
- path: examples/matrix-2.lua
10+
description: >
11+
If you wanted the opposite of matrix.forward you'll still use matrix.forward but you minus it instead of matrix.backward which doesn't exist. Same applies with matrix.right and matrix.up. To make the bin object appear behind a player:
12+
- path: examples/matrix-3.lua
13+
description: >
14+
Say you'd like to get find the position underneath a vehicle. This is the position at any rotation. So if it was upside down, the Z value would be higher than the vehicle Z value. If the vehicle was the right way round, the Z value for underneath car would be less than the Z value for the car.

classes/Matrix/examples/matrix-1.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Object ( 1337, player.matrix.position + player.matrix.forward * 2 )

classes/Matrix/examples/matrix-2.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Object ( 1337, player.matrix.position - player.matrix.forward * 2 )

classes/Matrix/examples/matrix-3.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--
2+
-- Instead of:
3+
--
4+
local matrix = getElementMatrix(vehicle)
5+
local offX = 0 * matrix[1][1] + 0 * matrix[2][1] - 1 * matrix[3][1] + matrix[4][1]
6+
local offY = 0 * matrix[1][2] + 0 * matrix[2][2] - 1 * matrix[3][2] + matrix[4][2]
7+
local offZ = 0 * matrix[1][3] + 0 * matrix[2][3] - 1 * matrix[3][3] + matrix[4][3]
8+
9+
local pX, pY, pZ = getElementPosition(vehicle)
10+
local positionBelow = {offX-pX, offY-pY, offZ-pZ}
11+
12+
--
13+
-- You only have to do:
14+
--
15+
local positionBelow = vehicle.position - vehicle.matrix.up

schemas/class.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ properties:
1212
description:
1313
type: string
1414
description: Description of the OOP class.
15+
examples:
16+
$ref: 'common-defs.yaml#/$defs/examples'
1517
see_also:
1618
$ref: 'common-defs.yaml#/$defs/see_also'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
import { Code } from '@astrojs/starlight/components';
3+
import { marked } from 'marked';
4+
5+
export interface CodeExample {
6+
description?: string;
7+
luaCode: string;
8+
}
9+
10+
export interface Props {
11+
codeExamples: CodeExample[];
12+
}
13+
14+
const { codeExamples } = Astro.props;
15+
---
16+
17+
{codeExamples.length > 0 && (
18+
<div class="examples-section">
19+
<h4>Code Examples</h4>
20+
{codeExamples.map((example) => (
21+
<div class="function-example">
22+
{example.description && (
23+
<Fragment set:html={marked(example.description)} />
24+
)}
25+
<Code code={example.luaCode} lang="lua" />
26+
</div>
27+
))}
28+
</div>
29+
)}

web/src/pages/[event].astro

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import NoteBox from '@src/components/NoteBox.astro';
1212
import type { NotesType } from '@src/utils/types';
1313
1414
import SeeAlsoSection from '@src/components/SeeAlsoSection.astro';
15+
import CodeExamplesSection from '@src/components/CodeExamplesSection.astro';
1516
1617
export async function getStaticPaths() {
1718
const events = await getCollection('events');
@@ -105,19 +106,7 @@ if (Array.isArray(event.data.notes) && event.data.notes.length > 0) {
105106
`addEventHandler("${event.id}", elementAttachedTo, ${eventHandlerExample.funcName})`
106107
} lang="lua" />
107108

108-
{eventExamples.length > 0 && (
109-
<div class="examples-section">
110-
<h4>Code Examples</h4>
111-
{eventExamples.map((example: any) => (
112-
<div class="function-example">
113-
{example.description && (
114-
<Fragment set:html={marked(example.description)} />
115-
)}
116-
<Code code={example.luaCode} lang="lua"/>
117-
</div>
118-
))}
119-
</div>
120-
)}
109+
<CodeExamplesSection codeExamples={eventExamples} />
121110

122111
<SeeAlsoSection seeAlsoLinks={getSeeAlsoLinksForItem(event)} currentId={event.id} />
123112
</StarlightPage>

web/src/pages/[func].astro

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import '@src/styles/function-page.css';
1313
import type { NotesType } from '@src/utils/types';
1414
1515
import SeeAlsoSection from '@src/components/SeeAlsoSection.astro';
16+
import CodeExamplesSection from '@src/components/CodeExamplesSection.astro';
1617
1718
export async function getStaticPaths() {
1819
const functions = await getCollection('functions');
@@ -171,20 +172,7 @@ let funcSyntaxes = parseFunctionSyntaxes(func.id, func.data);
171172
</div>
172173
))}
173174

174-
<!-- Examples -->
175-
{funcExamples.length > 0 && (
176-
<div class="examples-section">
177-
<h4>Code Examples</h4>
178-
{funcExamples.map((example: any) => (
179-
<div class="function-example">
180-
{example.description && (
181-
<Fragment set:html={marked(example.description)} />
182-
)}
183-
<Code code={example.luaCode} lang="lua"/>
184-
</div>
185-
))}
186-
</div>
187-
)}
175+
<CodeExamplesSection codeExamples={funcExamples} />
188176

189177
<SeeAlsoSection seeAlsoLinks={getSeeAlsoLinksForItem(func)} currentId={func.id} />
190178
</StarlightPage>

web/src/pages/[oopclass].astro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';
33
import { getCollection } from 'astro:content';
44
import { marked } from 'marked';
5+
import fs from "fs";
6+
import path from "path";
57
import { getSeeAlsoLinksForItem } from '@src/utils/general';
68
79
import SeeAlsoSection from '@src/components/SeeAlsoSection.astro';
10+
import CodeExamplesSection from '@src/components/CodeExamplesSection.astro';
811
912
export async function getStaticPaths() {
1013
const classes = await getCollection('classes');
@@ -16,6 +19,18 @@ export async function getStaticPaths() {
1619
1720
const { oopclass } = Astro.props;
1821
oopclass.data.itemType = 'class';
22+
const oopClassPath = path.dirname(oopclass.filePath ?? "");
23+
24+
let codeExamples: any[] = oopclass.data.examples || [];
25+
codeExamples = codeExamples.map((example: any) => {
26+
try {
27+
const luaCode = fs.readFileSync(path.resolve(`${oopClassPath}`, example.path), "utf8");
28+
return { ...example, luaCode };
29+
} catch (error) {
30+
console.error(`Error reading ${example.path}:`, error);
31+
return { ...example, luaCode: "Loading example error." };
32+
}
33+
});
1934
2035
---
2136
<StarlightPage frontmatter={{
@@ -25,6 +40,8 @@ oopclass.data.itemType = 'class';
2540
}}>
2641
<Fragment set:html={marked(oopclass.data.description)} />
2742

43+
<CodeExamplesSection codeExamples={codeExamples} />
44+
2845
<SeeAlsoSection seeAlsoLinks={getSeeAlsoLinksForItem(oopclass)} currentId={oopclass.id} />
2946
</StarlightPage>
3047

0 commit comments

Comments
 (0)