@rpg-engine/long-bow 0.1.66 → 0.1.67

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.
@@ -1,4 +1,4 @@
1
- import React, { useState, useEffect } from 'react';
1
+ import React, { useState, useEffect, useRef } from 'react';
2
2
  import styled from 'styled-components';
3
3
  import dayjs from 'dayjs';
4
4
  import { ErrorBoundary } from 'react-error-boundary';
@@ -6,7 +6,7 @@ import Draggable from 'react-draggable';
6
6
  import { v4 } from 'uuid';
7
7
  import 'rpgui/rpgui.min.css';
8
8
  import 'rpgui/rpgui.min.js';
9
- import { GRID_WIDTH, GRID_HEIGHT, ItemType, ActionsByItemType } from '@rpg-engine/shared';
9
+ import { ItemType, ActionsByItemType, GRID_WIDTH, GRID_HEIGHT } from '@rpg-engine/shared';
10
10
 
11
11
  function _extends() {
12
12
  _extends = Object.assign || function (target) {
@@ -295,14 +295,23 @@ var DraggableContainer = function DraggableContainer(_ref) {
295
295
  title = _ref.title,
296
296
  imgSrc = _ref.imgSrc,
297
297
  _ref$imgWidth = _ref.imgWidth,
298
- imgWidth = _ref$imgWidth === void 0 ? '20px' : _ref$imgWidth;
298
+ imgWidth = _ref$imgWidth === void 0 ? '20px' : _ref$imgWidth,
299
+ cancelDrag = _ref.cancelDrag,
300
+ onPositionChange = _ref.onPositionChange;
299
301
  return React.createElement(Draggable, {
300
- handle: ".drag-handdler",
301
- cancel: ".container-close"
302
+ cancel: ".container-close," + cancelDrag,
303
+ onDrag: function onDrag(_e, data) {
304
+ if (onPositionChange) {
305
+ onPositionChange({
306
+ x: data.x,
307
+ y: data.y
308
+ });
309
+ }
310
+ }
302
311
  }, React.createElement(Container$2, {
303
312
  width: width,
304
313
  height: height || 'auto',
305
- className: "rpgui-container " + type + " " + className + " rpgui-draggable"
314
+ className: "rpgui-container " + type + " " + className
306
315
  }, React.createElement(TitleContainer, {
307
316
  className: "drag-handler"
308
317
  }, React.createElement(Title, null, imgSrc && React.createElement(Icon, {
@@ -389,6 +398,32 @@ var Dropdown = function Dropdown(_ref) {
389
398
  }));
390
399
  };
391
400
 
401
+ function useOutsideClick(ref, id) {
402
+ useEffect(function () {
403
+ /**
404
+ * Alert if clicked on outside of element
405
+ */
406
+ function handleClickOutside(event) {
407
+ if (ref.current && !ref.current.contains(event.target)) {
408
+ var _event = new CustomEvent('clickOutside', {
409
+ detail: {
410
+ id: id
411
+ }
412
+ });
413
+
414
+ document.dispatchEvent(_event);
415
+ }
416
+ } // Bind the event listener
417
+
418
+
419
+ document.addEventListener('mousedown', handleClickOutside);
420
+ return function () {
421
+ // Unbind the event listener on clean up
422
+ document.removeEventListener('mousedown', handleClickOutside);
423
+ };
424
+ }, [ref]);
425
+ }
426
+
392
427
  var ListMenu = function ListMenu(_ref) {
393
428
  var options = _ref.options,
394
429
  onSelected = _ref.onSelected,
@@ -441,12 +476,57 @@ var ItemCard = function ItemCard(_ref) {
441
476
  var Container$4 = /*#__PURE__*/styled.div.withConfig({
442
477
  displayName: "ItemCard__Container",
443
478
  componentId: "sc-170rits-0"
444
- })(["position:absolute;top:", "px;left:", "px;font-size:7px;color:white;"], function (props) {
479
+ })(["position:absolute;top:", "px;left:", "px;font-size:0.5rem;color:white;background-color:black;border-radius:5px;padding:0.5rem;min-width:20px;opacity:0.5;"], function (props) {
445
480
  return props.y || 0;
446
481
  }, function (props) {
447
482
  return props.x || 0;
448
483
  });
449
484
 
485
+ var handleContextMenuList = function handleContextMenuList(itemType) {
486
+ var generateContextList = function generateContextList(actionsByTypeList) {
487
+ var contextMenu = actionsByTypeList.map(function (action) {
488
+ return {
489
+ id: action,
490
+ text: action
491
+ };
492
+ });
493
+ return contextMenu;
494
+ };
495
+
496
+ var contextActionMenu = [];
497
+
498
+ switch (itemType) {
499
+ case ItemType.Weapon:
500
+ case ItemType.Armor:
501
+ case ItemType.Accessory:
502
+ case ItemType.Jewelry:
503
+ case ItemType.Tool:
504
+ contextActionMenu = generateContextList(ActionsByItemType.Equipment);
505
+ break;
506
+
507
+ case ItemType.Consumable:
508
+ contextActionMenu = generateContextList(ActionsByItemType.Consumable);
509
+ break;
510
+
511
+ case ItemType.CraftMaterial:
512
+ contextActionMenu = generateContextList(ActionsByItemType.CraftMaterial);
513
+ break;
514
+
515
+ case ItemType.Other:
516
+ case ItemType.Information:
517
+ case ItemType.Quest:
518
+ case ItemType.Container:
519
+ contextActionMenu = generateContextList(ActionsByItemType.Other);
520
+ break;
521
+
522
+ default:
523
+ contextActionMenu = generateContextList(ActionsByItemType.Other);
524
+ break;
525
+ }
526
+
527
+ return contextActionMenu;
528
+ };
529
+
450
530
  var frames = {
451
531
  "accessories/bandana.png": {
452
532
  frame: {
@@ -2199,20 +2279,27 @@ var ItemSlot = function ItemSlot(_ref) {
2199
2279
  var slotIndex = _ref.slotIndex,
2200
2280
  item = _ref.item,
2201
2281
  _onMouseOver = _ref.onMouseOver,
2202
- onActionContextMenu = _ref.onActionContextMenu;
2282
+ _onClick = _ref.onClick,
2283
+ onCancelContextMenu = _ref.onCancelContextMenu;
2203
2284
 
2204
- var getLeftPostionValue = function getLeftPostionValue(quantity) {
2285
+ var getLeftPositionValue = function getLeftPositionValue(quantity) {
2205
2286
  if (quantity > 0 && quantity < 10) return '2.5rem';else if (quantity > 9 && quantity < 100) return '2.0rem';else if (quantity > 99) return '1rem';
2206
2287
  return '2.5rem';
2207
2288
  };
2208
2289
 
2209
2290
  return React.createElement(Container$6, {
2210
- onContextMenu: function onContextMenu(event) {
2211
- onActionContextMenu(event, slotIndex);
2212
- },
2213
2291
  className: "rpgui-icon empty-slot",
2214
2292
  onMouseOver: function onMouseOver(event) {
2215
- return _onMouseOver(event, slotIndex, item);
2293
+ return _onMouseOver(event, slotIndex, item, event.clientX, event.clientY);
2294
+ },
2295
+ onClick: function onClick(e) {
2296
+ if (item) {
2297
+ console.log(e);
2298
+
2299
+ _onClick(item, e.clientX, e.clientY);
2300
+ } else {
2301
+ onCancelContextMenu();
2302
+ }
2216
2303
  }
2217
2304
  }, item && item.texturePath ? React.createElement(SpriteFromAtlas, {
2218
2305
  atlasIMG: img,
@@ -2220,7 +2307,7 @@ var ItemSlot = function ItemSlot(_ref) {
2220
2307
  spriteKey: item.texturePath,
2221
2308
  scale: 3
2222
2309
  }) : null, item && item.isStackable && item != null && item.stackQty ? React.createElement(ItemQty, {
2223
- left: getLeftPostionValue(item.stackQty)
2310
+ left: getLeftPositionValue(item.stackQty)
2224
2311
  }, ' ', item.stackQty, ' ') : null);
2225
2312
  };
2226
2313
  var Container$6 = /*#__PURE__*/styled.div.withConfig({
@@ -2238,181 +2325,158 @@ var ItemContainer = function ItemContainer(_ref) {
2238
2325
  var itemContainer = _ref.itemContainer,
2239
2326
  onClose = _ref.onClose,
2240
2327
  onMouseOver = _ref.onMouseOver,
2241
- onActionSelected = _ref.onActionSelected;
2328
+ onActionSelected = _ref.onActionSelected,
2329
+ _ref$initialPosition = _ref.initialPosition,
2330
+ initialPosition = _ref$initialPosition === void 0 ? {
2331
+ x: 0,
2332
+ y: 0
2333
+ } : _ref$initialPosition;
2334
+
2335
+ // we use this draggable position to offset the menu position, after the container is dragged (otherwise, it bugs!)
2336
+ var _useState = useState(initialPosition),
2337
+ draggablePosition = _useState[0],
2338
+ setDraggablePosition = _useState[1];
2339
+
2340
+ var draggableRef = useRef(null);
2341
+ useOutsideClick(draggableRef, 'item-container');
2342
+ useEffect(function () {
2343
+ document.addEventListener('clickOutside', function (event) {
2344
+ var e = event;
2345
+
2346
+ if (e.detail.id === 'item-container') {
2347
+ clearContextMenu();
2348
+ clearItemHoverDetail();
2349
+ }
2350
+ });
2351
+ return function () {
2352
+ document.removeEventListener('clickOutside', function (_e) {});
2353
+ };
2354
+ }, []);
2355
+ var initX = initialPosition.x,
2356
+ initY = initialPosition.y;
2242
2357
 
2243
- var _useState = useState({
2358
+ var _useState2 = useState({
2244
2359
  visible: false,
2245
- posX: 0,
2246
- posY: 0,
2360
+ posX: initX,
2361
+ posY: initY,
2247
2362
  contextActions: [],
2248
2363
  slotItem: null
2249
2364
  }),
2250
- contextData = _useState[0],
2251
- setContextData = _useState[1];
2365
+ contextData = _useState2[0],
2366
+ setContextData = _useState2[1];
2252
2367
 
2253
- var _useState2 = useState({
2368
+ var _useState3 = useState({
2254
2369
  visible: false,
2255
- posX: 0,
2256
- posY: 0,
2370
+ posX: initX,
2371
+ posY: initY,
2257
2372
  item: null
2258
2373
  }),
2259
- itemHoverDetail = _useState2[0];
2260
-
2261
- var selectedSlotContext = null;
2262
-
2263
- var handleContextMenuList = function handleContextMenuList(itemType) {
2264
- var generateContextList = function generateContextList(actionsByTypeList) {
2265
- var contextMenu = actionsByTypeList.map(function (action) {
2266
- return {
2267
- id: action,
2268
- text: action
2269
- };
2270
- });
2271
- return contextMenu;
2272
- };
2273
-
2274
- var contextActionMenu = [];
2275
-
2276
- switch (itemType) {
2277
- case ItemType.Weapon:
2278
- case ItemType.Armor:
2279
- case ItemType.Accessory:
2280
- case ItemType.Jewelry:
2281
- case ItemType.Tool:
2282
- contextActionMenu = generateContextList(ActionsByItemType.Equipment);
2283
- break;
2284
-
2285
- case ItemType.Consumable:
2286
- contextActionMenu = generateContextList(ActionsByItemType.Consumable);
2287
- break;
2288
-
2289
- case ItemType.CraftMaterial:
2290
- contextActionMenu = generateContextList(ActionsByItemType.CraftMaterial);
2291
- break;
2292
-
2293
- case ItemType.Other:
2294
- case ItemType.Information:
2295
- case ItemType.Quest:
2296
- case ItemType.Container:
2297
- contextActionMenu = generateContextList(ActionsByItemType.Other);
2298
- break;
2299
-
2300
- default:
2301
- contextActionMenu = generateContextList(ActionsByItemType.Other);
2302
- break;
2303
- }
2304
-
2305
- return contextActionMenu;
2374
+ itemHoverDetail = _useState3[0],
2375
+ setItemHoverDetail = _useState3[1];
2376
+
2377
+ var clearContextMenu = function clearContextMenu() {
2378
+ setContextData({
2379
+ visible: false,
2380
+ posX: 0,
2381
+ posY: 0,
2382
+ contextActions: [],
2383
+ slotItem: null
2384
+ });
2306
2385
  };
2307
2386
 
2308
- useEffect(function () {
2309
- var contextMenuEventHandler = function contextMenuEventHandler(event) {
2310
- event.preventDefault();
2311
-
2312
- if (selectedSlotContext !== null) {
2313
- var _itemContainer$slots$, _itemContainer$slots;
2314
-
2315
- var itemData = (_itemContainer$slots$ = (_itemContainer$slots = itemContainer.slots) == null ? void 0 : _itemContainer$slots[selectedSlotContext]) != null ? _itemContainer$slots$ : null;
2316
-
2317
- if (itemData) {
2318
- // TODO: generate a menuActionList based on type and subType of the item.
2319
- var contextActionMenu = handleContextMenuList(itemData.type);
2320
- setContextData(_extends({}, contextData, {
2321
- visible: true,
2322
- posX: event.clientX,
2323
- posY: event.clientY,
2324
- slotItem: itemData,
2325
- contextActions: contextActionMenu
2326
- }));
2327
- } else {
2328
- // console.log("Empty Slot")
2329
- setContextData(_extends({}, contextData, {
2330
- visible: false,
2331
- posX: 0,
2332
- posY: 0,
2333
- slotIndex: null,
2334
- slotItem: null,
2335
- contextActions: []
2336
- }));
2337
- }
2338
- }
2339
- };
2340
-
2341
- var offClickHandler = function offClickHandler(event) {
2342
- console.log(event);
2343
- setContextData(_extends({}, contextData, {
2344
- visible: false,
2345
- posX: 0,
2346
- posY: 0,
2347
- slotIndex: null,
2348
- slotItem: null
2387
+ var handleOnMouseHover = function handleOnMouseHover(event, slotIndex, item, x, y) {
2388
+ if (item) {
2389
+ setItemHoverDetail(_extends({}, itemHoverDetail, {
2390
+ visible: true,
2391
+ item: item,
2392
+ posX: x - draggablePosition.x,
2393
+ posY: y - draggablePosition.y
2349
2394
  }));
2350
- };
2351
-
2352
- document.addEventListener('contextmenu', contextMenuEventHandler);
2353
- document.addEventListener('click', offClickHandler);
2354
- return function () {
2355
- document.removeEventListener('contextmenu', contextMenuEventHandler);
2356
- document.removeEventListener('click', offClickHandler);
2357
- };
2358
- }, [contextData, selectedSlotContext]);
2395
+ onMouseOver(event, slotIndex, item);
2396
+ } else {
2397
+ clearItemHoverDetail();
2398
+ }
2399
+ };
2359
2400
 
2360
- var onActionContextMenu = function onActionContextMenu(event, slotIndex) {
2361
- selectedSlotContext = slotIndex;
2362
- console.log(event); // setContextData({...contextData, slotItem: slotIndex})
2401
+ var clearItemHoverDetail = function clearItemHoverDetail() {
2402
+ setItemHoverDetail(_extends({}, itemHoverDetail, {
2403
+ visible: false,
2404
+ item: null
2405
+ }));
2363
2406
  };
2364
2407
 
2365
- var handleOnMouseHover = function handleOnMouseHover(event, slotIndex, item) {
2366
- // TODO: Show a tip view with basic info about the item.
2367
- // set a minimun hover period before show infos.
2368
- // setItemHoverDetail({...itemHoverDetail, visible: true, item: item, posX: event.clientX, posY: event.clientY})
2369
- onMouseOver(event, slotIndex, item);
2408
+ var handleOnItemClick = function handleOnItemClick(item, posX, posY) {
2409
+ var contextList = handleContextMenuList(item.type);
2410
+ setContextData(_extends({}, contextData, {
2411
+ visible: true,
2412
+ posX: posX,
2413
+ posY: posY,
2414
+ slotItem: item,
2415
+ contextActions: contextList
2416
+ }));
2417
+ clearItemHoverDetail();
2370
2418
  };
2371
2419
 
2372
2420
  var onSelected = function onSelected(selectedActionId) {
2373
2421
  var payloadData = {
2374
2422
  actionType: selectedActionId,
2375
2423
  item: contextData.slotItem
2376
- }; // TODO: create a function to validate the selection set the paylod data.
2377
-
2424
+ };
2378
2425
  onActionSelected(payloadData);
2426
+ clearContextMenu();
2379
2427
  };
2380
2428
 
2381
2429
  var onRenderSlots = function onRenderSlots() {
2382
2430
  var slots = [];
2383
2431
 
2384
2432
  for (var i = 0; i < itemContainer.slotQty; i++) {
2385
- var _itemContainer$slots2;
2433
+ var _itemContainer$slots;
2386
2434
 
2387
2435
  slots.push(React.createElement(ItemSlot, {
2388
2436
  key: i,
2389
2437
  slotIndex: i,
2390
- item: ((_itemContainer$slots2 = itemContainer.slots) == null ? void 0 : _itemContainer$slots2[i]) || null,
2438
+ item: ((_itemContainer$slots = itemContainer.slots) == null ? void 0 : _itemContainer$slots[i]) || null,
2391
2439
  onMouseOver: handleOnMouseHover,
2392
- onActionContextMenu: onActionContextMenu
2440
+ onClick: handleOnItemClick,
2441
+ onCancelContextMenu: function onCancelContextMenu() {
2442
+ clearContextMenu();
2443
+ }
2393
2444
  }));
2394
2445
  }
2395
2446
 
2396
2447
  return slots;
2397
2448
  };
2398
2449
 
2399
- return React.createElement(DraggableContainer, {
2450
+ return React.createElement("div", {
2451
+ ref: draggableRef
2452
+ }, React.createElement(DraggableContainer, {
2400
2453
  title: itemContainer.name || 'Container',
2401
2454
  type: RPGUIContainerTypes.Framed,
2402
2455
  onCloseButton: function onCloseButton() {
2403
2456
  return onClose();
2404
2457
  },
2405
- width: "330px"
2406
- }, React.createElement(ItemsContainer, null, onRenderSlots()), contextData.visible ? React.createElement(ListMenu, {
2407
- x: contextData.posX,
2408
- y: contextData.posY,
2458
+ width: "330px",
2459
+ cancelDrag: ".item-container-body",
2460
+ onPositionChange: function onPositionChange(_ref2) {
2461
+ var x = _ref2.x,
2462
+ y = _ref2.y;
2463
+ setDraggablePosition({
2464
+ x: x,
2465
+ y: y
2466
+ });
2467
+ }
2468
+ }, React.createElement(ItemsContainer, {
2469
+ className: "item-container-body"
2470
+ }, onRenderSlots()), contextData.visible ? React.createElement(ListMenu, {
2471
+ x: contextData.posX - draggablePosition.x,
2472
+ y: contextData.posY - draggablePosition.y,
2409
2473
  options: contextData.contextActions,
2410
2474
  onSelected: onSelected
2411
2475
  }) : null, itemHoverDetail.visible ? React.createElement(ItemCard, {
2412
2476
  item: itemHoverDetail.item,
2413
2477
  x: itemHoverDetail.posX,
2414
2478
  y: itemHoverDetail.posY
2415
- }) : null);
2479
+ }) : null));
2416
2480
  };
2417
2481
  var ItemsContainer = /*#__PURE__*/styled.div.withConfig({
2418
2482
  displayName: "ItemContainer__ItemsContainer",