Skip to content

Commit

Permalink
Added Texture component
Browse files Browse the repository at this point in the history
  • Loading branch information
neki-dev committed Sep 26, 2023
1 parent ff5b7c7 commit 84187a0
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 35 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ useRelativeScale(params: {

#### Get source texture image
```ts
useTexture(key: string): HTMLImageElement
useTexture(params: {
key: string,
frame?: string | number
}): HTMLImageElement
```

.
Expand All @@ -119,6 +122,11 @@ useTexture(key: string): HTMLImageElement
</RelativeScale>
```

#### Render texture image
```ts
<Texture key={string} frame={string? | number?} />
```

.

## Utils
Expand Down
1 change: 1 addition & 0 deletions dist/components/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './relative-scale';
export * from './relative-position';
export * from './texture';
5 changes: 5 additions & 0 deletions dist/components/texture.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';
import { TextureProps } from '../types/texture';
type Props = TextureProps;
export declare const Texture: React.FC<Props>;
export {};
3 changes: 2 additions & 1 deletion dist/hooks/use-texture.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export declare function useTexture(key: string): HTMLImageElement;
import { TextureProps } from '../types/texture';
export declare function useTexture({ key, frame }: TextureProps): HTMLImageElement;
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions dist/types/texture.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type TextureProps = {
key: string;
frame?: string | number;
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "phaser-react-ui",
"description": "React interface render for Phaser engine",
"version": "1.5.0",
"version": "1.6.0",
"keywords": [
"phaser",
"interface",
Expand Down
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './relative-scale';
export * from './relative-position';
export * from './texture';
16 changes: 5 additions & 11 deletions src/components/relative-position.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import React, { useEffect } from 'react';
import React, { useLayoutEffect } from 'react';
import { useRelativePosition } from '../hooks';
import { RelativePositionProps } from '../types/relative-position';

type Props = RelativePositionProps & {
children: React.ReactNode
};

export const RelativePosition: React.FC<Props> = ({
x, y, children,
}) => {
const ref = useRelativePosition<HTMLDivElement>({ x, y });
export const RelativePosition: React.FC<Props> = ({ children, ...props }) => {
const ref = useRelativePosition<HTMLDivElement>(props);

useEffect(() => {
useLayoutEffect(() => {
ref.current.style.position = 'absolute';
}, []);

return (
<div ref={ref}>
{children}
</div>
);
return <div ref={ref}>{children}</div>;
};
18 changes: 5 additions & 13 deletions src/components/relative-scale.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import React, { useEffect } from 'react';
import React, { useLayoutEffect } from 'react';
import { useRelativeScale } from '../hooks';
import { RelativeScaleProps } from '../types/relative-scale';

type Props = RelativeScaleProps & {
children: React.ReactNode
};

export const RelativeScale: React.FC<Props> = ({
target, min, max, round, children,
}) => {
const ref = useRelativeScale<HTMLDivElement>({
target, min, max, round,
});
export const RelativeScale: React.FC<Props> = ({ children, ...props }) => {
const ref = useRelativeScale<HTMLDivElement>(props);

useEffect(() => {
useLayoutEffect(() => {
ref.current.style.position = 'absolute';
}, []);

return (
<div ref={ref}>
{children}
</div>
);
return <div ref={ref}>{children}</div>;
};
17 changes: 17 additions & 0 deletions src/components/texture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React, { useRef, useLayoutEffect } from 'react';
import { useTexture } from '../hooks';
import { TextureProps } from '../types/texture';

type Props = TextureProps;

export const Texture: React.FC<Props> = (props) => {
const image = useTexture(props);

const ref = useRef<HTMLDivElement>(null);

useLayoutEffect(() => {
ref.current.appendChild(image);
}, []);

return <div ref={ref} data-texture-container />;
};
17 changes: 12 additions & 5 deletions src/hooks/use-texture.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { useMemo } from 'react';
import { useGame } from './use-game';
import { TextureProps } from '../types/texture';

export function useTexture(key: string) {
export function useTexture({ key, frame }: TextureProps) {
const game = useGame();

return useMemo(
() => game.textures.get(key).getSourceImage() as HTMLImageElement,
[key],
);
return useMemo(() => {
const texture = game.textures.get(key);

if (frame === undefined) {
return texture.getSourceImage() as HTMLImageElement;
}

// @ts-ignore
return texture.frames[frame].source.image as HTMLImageElement;
}, [key, frame]);
}
4 changes: 4 additions & 0 deletions src/types/texture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type TextureProps = {
key: string
frame?: string | number
};

0 comments on commit 84187a0

Please sign in to comment.