@rpg-engine/long-bow 0.5.24 → 0.5.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,8 @@ declare const meta: Meta;
5
5
  export default meta;
6
6
  interface ITemplateProps {
7
7
  type: ItemContainerType;
8
+ scale?: number;
8
9
  }
9
10
  export declare const Inventory: import("@storybook/csf").AnnotatedStoryFn<import("@storybook/react").ReactFramework, IItemContainerProps & ITemplateProps>;
11
+ export declare const InventoryScaledDown: import("@storybook/csf").AnnotatedStoryFn<import("@storybook/react").ReactFramework, IItemContainerProps & ITemplateProps>;
10
12
  export declare const Depot: import("@storybook/csf").AnnotatedStoryFn<import("@storybook/react").ReactFramework, IItemContainerProps & ITemplateProps>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpg-engine/long-bow",
3
- "version": "0.5.24",
3
+ "version": "0.5.26",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -162,7 +162,7 @@ export const EquipmentSet: React.FC<IEquipmentSetProps> = ({
162
162
 
163
163
  return (
164
164
  <DraggingProvider>
165
- <DraggedItem atlasIMG={atlasIMG} atlasJSON={atlasJSON} />
165
+ <DraggedItem atlasIMG={atlasIMG} atlasJSON={atlasJSON} scale={scale} />
166
166
  <DraggableContainer
167
167
  title={'Equipments'}
168
168
  type={RPGUIContainerTypes.Framed}
@@ -12,14 +12,18 @@ const OFFSET = CONTAINER_SIZE / 2;
12
12
  interface IProps {
13
13
  atlasJSON: any;
14
14
  atlasIMG: any;
15
+ scale?: number;
15
16
  }
16
17
 
17
18
  export const DraggedItem = ({
18
19
  atlasJSON,
19
20
  atlasIMG,
21
+ scale,
20
22
  }: IProps): JSX.Element | null => {
21
23
  const { item } = useDragging();
22
- const { x, y } = useCursorPosition();
24
+ const { x, y } = useCursorPosition({
25
+ scale,
26
+ });
23
27
 
24
28
  if (!item) {
25
29
  return null;
@@ -159,7 +159,7 @@ export const ItemContainer: React.FC<IItemContainerProps> = ({
159
159
 
160
160
  return (
161
161
  <DraggingProvider>
162
- <DraggedItem atlasIMG={atlasIMG} atlasJSON={atlasJSON} />
162
+ <DraggedItem atlasIMG={atlasIMG} atlasJSON={atlasJSON} scale={scale} />
163
163
  <SlotsContainer
164
164
  title={itemContainer.name || 'Container'}
165
165
  onClose={onClose}
@@ -5,4 +5,4 @@ export const SHORTCUTS_STORAGE_KEY = 'shortcuts';
5
5
  export const defaultShortcut: IShortcut = {
6
6
  type: ShortcutType.None,
7
7
  payload: null,
8
- };
8
+ };
@@ -0,0 +1,22 @@
1
+ import styled, { css } from 'styled-components';
2
+ import { UI_BREAKPOINT_MOBILE } from '../../constants/uiBreakpoints';
3
+
4
+ interface IScalableContainerProps {
5
+ scale?: number;
6
+ centralize?: boolean;
7
+ breakPoint?: string;
8
+ }
9
+
10
+ export const ScalableContainer = styled.div<IScalableContainerProps>`
11
+ ${({ centralize = true }) =>
12
+ centralize &&
13
+ css`
14
+ display: flex;
15
+ justify-content: center;
16
+ align-items: center;
17
+ `}
18
+
19
+ @media (max-width: ${({ breakPoint }) => breakPoint ?? UI_BREAKPOINT_MOBILE}) {
20
+ transform: scale(${({ scale }) => scale ?? 0.75});
21
+ }
22
+ `;
@@ -0,0 +1,3 @@
1
+
2
+ export const UI_BREAKPOINT_MOBILE = '950px';
3
+ export const UI_BREAKPOINT_SMALL_LAPTOP = '1400px';
@@ -1,30 +1,46 @@
1
+ import { GRID_HEIGHT, GRID_WIDTH } from '@rpg-engine/shared';
1
2
  import { useEffect, useState } from 'react';
3
+ import { IPosition } from '../types/eventTypes';
2
4
 
3
- type CursorPosition = {
4
- x: number;
5
- y: number;
6
- };
5
+ interface ICursorPositionProps {
6
+ scale?: number;
7
+ }
7
8
 
8
- export const useCursorPosition = (): CursorPosition => {
9
- const [position, setPosition] = useState<CursorPosition>({ x: 0, y: 0 });
9
+ export const useCursorPosition = ({ scale = 1 }: ICursorPositionProps): IPosition => {
10
+ const [position, setPosition] = useState<IPosition>({ x: 0, y: 0 });
10
11
 
11
12
  const setFromEvent = (e: MouseEvent | TouchEvent) => {
13
+ let x, y;
14
+ const viewportCenterX = window.innerWidth / 2 - GRID_WIDTH;
15
+ const viewportCenterY = window.innerHeight / - GRID_HEIGHT;
16
+
12
17
  if ('touches' in e) {
13
- setPosition({ x: e.touches[0].clientX, y: e.touches[0].clientY });
18
+ x = e.touches[0].clientX;
19
+ y = e.touches[0].clientY;
14
20
  } else {
15
- setPosition({ x: e.clientX, y: e.clientY });
21
+ x = e.clientX;
22
+ y = e.clientY;
16
23
  }
24
+
25
+ // Adjusting for the global scale
26
+ // Assuming the element is centrally located
27
+ setPosition({
28
+ x: (x - viewportCenterX) / scale + viewportCenterX,
29
+ y: (y - viewportCenterY) / scale + viewportCenterY
30
+ });
17
31
  };
18
32
 
19
33
  useEffect(() => {
20
34
  window.addEventListener('mousemove', setFromEvent);
21
35
  window.addEventListener('touchmove', setFromEvent);
22
36
 
37
+ console.log("SCALE IS ", scale)
38
+
23
39
  return () => {
24
40
  window.removeEventListener('mousemove', setFromEvent);
25
41
  window.removeEventListener('touchmove', setFromEvent);
26
42
  };
27
- }, []);
43
+ }, [scale]);
28
44
 
29
45
  return position;
30
46
  };
@@ -8,6 +8,8 @@ import {
8
8
  import { RPGUIRoot } from '../../src/components/RPGUIRoot';
9
9
  import { equipmentSetMock } from '../../src/mocks/equipmentSet.mocks';
10
10
  import { itemContainerMock } from '../../src/mocks/itemContainer.mocks';
11
+ import { ScalableContainer } from '../components/shared/ScalableContainer';
12
+ import { UI_BREAKPOINT_SMALL_LAPTOP } from '../constants/uiBreakpoints';
11
13
  import { useShortcuts } from '../hooks/useShortcuts';
12
14
  import atlasJSON from '../mocks/atlas/items/items.json';
13
15
  import atlasIMG from '../mocks/atlas/items/items.png';
@@ -48,9 +50,13 @@ let dropSlot = -1;
48
50
 
49
51
  interface ITemplateProps {
50
52
  type: ItemContainerType;
53
+ scale?: number;
51
54
  }
52
55
 
53
- const Template: Story<IItemContainerProps & ITemplateProps> = ({ type }) => {
56
+ const Template: Story<IItemContainerProps & ITemplateProps> = ({
57
+ type,
58
+ scale,
59
+ }) => {
54
60
  const mock = itemContainerMock({ name: type });
55
61
 
56
62
  const [itemContainer, setItemContainer] = useState(mock);
@@ -59,81 +65,93 @@ const Template: Story<IItemContainerProps & ITemplateProps> = ({ type }) => {
59
65
  itemContainer: mock,
60
66
  });
61
67
 
62
- return (
63
- <RPGUIRoot>
64
- <ItemContainer
65
- itemContainer={itemContainer}
66
- onClose={() => console.log('closing item container')}
67
- onMouseOver={onMouseOver}
68
- onSelected={onSelected}
69
- onItemClick={onItemClick}
70
- checkIfItemCanBeMoved={() => allowedToDrop}
71
- onItemDragEnd={quantity => {
72
- // THIS IS ONLY LONG-BOW EXAMPLE FOR DRAG AND DROP
73
-
74
- if (quantity === 0) {
75
- setItemContainer({ ...itemContainer });
76
- } else if (allowedToDrop && dropSlot !== -1) {
77
- const newContainer = { ...itemContainer };
78
-
79
- if (quantity && dragItem && dragItem.stackQty) {
80
- newContainer.slots[dropSlot] = {
68
+ const coreItemContainer = (
69
+ <ItemContainer
70
+ itemContainer={itemContainer}
71
+ onClose={() => console.log('closing item container')}
72
+ onMouseOver={onMouseOver}
73
+ onSelected={onSelected}
74
+ onItemClick={onItemClick}
75
+ checkIfItemCanBeMoved={() => allowedToDrop}
76
+ onItemDragEnd={quantity => {
77
+ // THIS IS ONLY LONG-BOW EXAMPLE FOR DRAG AND DROP
78
+
79
+ if (quantity === 0) {
80
+ setItemContainer({ ...itemContainer });
81
+ } else if (allowedToDrop && dropSlot !== -1) {
82
+ const newContainer = { ...itemContainer };
83
+
84
+ if (quantity && dragItem && dragItem.stackQty) {
85
+ newContainer.slots[dropSlot] = {
86
+ ...dragItem,
87
+ stackQty: quantity + (dropItem?.stackQty || 0),
88
+ };
89
+
90
+ if (dragItem.stackQty - quantity === 0)
91
+ newContainer.slots[dragSlot] = null;
92
+ else
93
+ newContainer.slots[dragSlot] = {
81
94
  ...dragItem,
82
- stackQty: quantity + (dropItem?.stackQty || 0),
95
+ stackQty: dragItem.stackQty - quantity,
83
96
  };
84
-
85
- if (dragItem.stackQty - quantity === 0)
86
- newContainer.slots[dragSlot] = null;
87
- else
88
- newContainer.slots[dragSlot] = {
89
- ...dragItem,
90
- stackQty: dragItem.stackQty - quantity,
91
- };
92
- } else {
93
- newContainer.slots[dropSlot] = dragItem;
94
- newContainer.slots[dragSlot] = null;
95
- }
96
- setTimeout(() => {
97
- setItemContainer(newContainer);
98
- }, 100);
97
+ } else {
98
+ newContainer.slots[dropSlot] = dragItem;
99
+ newContainer.slots[dragSlot] = null;
99
100
  }
100
-
101
+ setTimeout(() => {
102
+ setItemContainer(newContainer);
103
+ }, 100);
104
+ }
105
+
106
+ allowedToDrop = false;
107
+ dropSlot = -1;
108
+ dropItem = null;
109
+ dragItem = null;
110
+ }}
111
+ onItemDragStart={(item, slotIndex) => {
112
+ dragItem = item;
113
+ dragSlot = slotIndex;
114
+ }}
115
+ onItemPlaceDrop={(item, slotIndex) => {
116
+ // THIS IS ONLY LONG-BOW EXAMPLE FOR DRAG AND DROP
117
+
118
+ if (!item || (dragItem?.key === item?.key && dragSlot !== slotIndex)) {
119
+ console.log('allow');
120
+ allowedToDrop = true;
121
+ dropSlot = slotIndex;
122
+ dropItem = item ? item : null;
123
+ } else {
101
124
  allowedToDrop = false;
102
125
  dropSlot = -1;
103
126
  dropItem = null;
104
- dragItem = null;
105
- }}
106
- onItemDragStart={(item, slotIndex) => {
107
- dragItem = item;
108
- dragSlot = slotIndex;
109
- }}
110
- onItemPlaceDrop={(item, slotIndex) => {
111
- // THIS IS ONLY LONG-BOW EXAMPLE FOR DRAG AND DROP
112
-
113
- if (
114
- !item ||
115
- (dragItem?.key === item?.key && dragSlot !== slotIndex)
116
- ) {
117
- console.log('allow');
118
- allowedToDrop = true;
119
- dropSlot = slotIndex;
120
- dropItem = item ? item : null;
121
- } else {
122
- allowedToDrop = false;
123
- dropSlot = -1;
124
- dropItem = null;
125
- }
126
- }}
127
- onOutsideDrop={(item, pos) => console.log('drop', item, pos)}
128
- type={type}
129
- atlasIMG={atlasIMG}
130
- atlasJSON={atlasJSON}
131
- shortcuts={shortcuts}
132
- setItemShortcut={setItemShortcut}
133
- removeShortcut={removeShortcut}
134
- equipmentSet={equipmentSetMock}
135
- isDepotSystem
136
- />
127
+ }
128
+ }}
129
+ onOutsideDrop={(item, pos) => console.log('drop', item, pos)}
130
+ type={type}
131
+ atlasIMG={atlasIMG}
132
+ atlasJSON={atlasJSON}
133
+ shortcuts={shortcuts}
134
+ setItemShortcut={setItemShortcut}
135
+ removeShortcut={removeShortcut}
136
+ equipmentSet={equipmentSetMock}
137
+ isDepotSystem
138
+ scale={scale}
139
+ />
140
+ );
141
+
142
+ return (
143
+ <RPGUIRoot>
144
+ {scale ? (
145
+ <ScalableContainer
146
+ scale={scale}
147
+ centralize={false}
148
+ breakPoint={UI_BREAKPOINT_SMALL_LAPTOP}
149
+ >
150
+ {coreItemContainer}
151
+ </ScalableContainer>
152
+ ) : (
153
+ coreItemContainer
154
+ )}
137
155
  </RPGUIRoot>
138
156
  );
139
157
  };
@@ -143,6 +161,12 @@ Inventory.args = {
143
161
  type: ItemContainerType.Inventory,
144
162
  };
145
163
 
164
+ export const InventoryScaledDown = Template.bind({});
165
+ InventoryScaledDown.args = {
166
+ type: ItemContainerType.Inventory,
167
+ scale: 0.8,
168
+ };
169
+
146
170
  export const Depot = Template.bind({});
147
171
  Depot.args = {
148
172
  type: ItemContainerType.Depot,