microboard-temp 0.4.68 → 0.4.70

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.
@@ -47789,8 +47789,7 @@ var defaultCardData = {
47789
47789
  itemType: "Card",
47790
47790
  isOpen: false,
47791
47791
  faceUrl: "",
47792
- backsideUrl: "",
47793
- isInDeck: false
47792
+ backsideUrl: ""
47794
47793
  };
47795
47794
  var CARD_DIMENSIONS = { width: 250, height: 400 };
47796
47795
 
@@ -47799,7 +47798,6 @@ class Card extends BaseItem {
47799
47798
  faceUrl = "";
47800
47799
  backsideUrl = "";
47801
47800
  isOpen = false;
47802
- isInDeck = false;
47803
47801
  throttledBringToFront;
47804
47802
  face = null;
47805
47803
  backside = null;
@@ -47835,35 +47833,20 @@ class Card extends BaseItem {
47835
47833
  };
47836
47834
  this.updateImageToRender();
47837
47835
  }
47838
- setIsInDeck(isInDeck) {
47839
- this.emit({
47840
- class: "Card",
47841
- method: "setIsInDeck",
47842
- item: [this.getId()],
47843
- newData: { isInDeck },
47844
- prevData: { isInDeck: this.isInDeck }
47845
- });
47846
- }
47847
47836
  updateImageToRender() {
47848
47837
  this.imageToRender = this.backside;
47849
47838
  if (this.isOpen) {
47850
47839
  this.imageToRender = this.face;
47851
47840
  }
47852
47841
  }
47853
- render(context, deckRenderData) {
47854
- if (this.transformationRenderBlock || this.isInDeck && !deckRenderData) {
47842
+ render(context) {
47843
+ if (this.transformationRenderBlock) {
47855
47844
  return;
47856
47845
  }
47857
47846
  const ctx = context.ctx;
47858
47847
  if (this.imageToRender && this.imageToRender.complete) {
47859
47848
  ctx.save();
47860
- let left = this.left;
47861
- let top = this.top;
47862
- if (deckRenderData) {
47863
- left = deckRenderData.left + 2 * deckRenderData.cardPosition;
47864
- top = deckRenderData.top;
47865
- }
47866
- ctx.drawImage(this.imageToRender, left, top, CARD_DIMENSIONS.width, CARD_DIMENSIONS.height);
47849
+ ctx.drawImage(this.imageToRender, this.left, this.top, CARD_DIMENSIONS.width, CARD_DIMENSIONS.height);
47867
47850
  ctx.restore();
47868
47851
  }
47869
47852
  }
@@ -47874,12 +47857,6 @@ class Card extends BaseItem {
47874
47857
  this.right = this.left + CARD_DIMENSIONS.width * scaleX;
47875
47858
  this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
47876
47859
  }
47877
- getMbr() {
47878
- if (this.isInDeck) {
47879
- return new Mbr(1e4, 1e4, 1e4, 1e4);
47880
- }
47881
- return super.getMbr();
47882
- }
47883
47860
  getPath() {
47884
47861
  return new Path(this.getMbr().getLines());
47885
47862
  }
@@ -47912,9 +47889,6 @@ class Card extends BaseItem {
47912
47889
  this.isOpen = op.newData.isOpen;
47913
47890
  this.updateImageToRender();
47914
47891
  break;
47915
- case "setIsInDeck":
47916
- this.isInDeck = op.newData.isInDeck;
47917
- break;
47918
47892
  }
47919
47893
  break;
47920
47894
  }
@@ -47927,22 +47901,17 @@ registerItem({
47927
47901
  });
47928
47902
  // src/Items/Examples/CardGame/Deck/Deck.ts
47929
47903
  var defaultDeckData = {
47930
- itemType: "Deck",
47931
- cardIds: []
47904
+ itemType: "Deck"
47932
47905
  };
47933
47906
 
47934
47907
  class Deck extends BaseItem {
47935
47908
  subject = new Subject;
47936
47909
  shouldUseCustomRender = false;
47937
- cardIds = [];
47938
- cards = [];
47939
47910
  constructor(board, id = "", defaultData2, cards) {
47940
- super(board, id, defaultDeckData);
47911
+ super(board, id, defaultDeckData, true);
47941
47912
  if (cards) {
47942
- this.cards = cards;
47943
- cards.forEach((card) => card.setIsInDeck(true));
47944
47913
  this.transformation.matrix = cards[cards.length - 1].transformation.matrix;
47945
- this.cardIds = cards.map((card) => card.getId());
47914
+ this.applyAddChildren(cards.map((card) => card.getId()));
47946
47915
  }
47947
47916
  this.transformation.subject.subscribe(() => {
47948
47917
  this.updateMbr();
@@ -47950,128 +47919,113 @@ class Deck extends BaseItem {
47950
47919
  });
47951
47920
  this.updateMbr();
47952
47921
  }
47922
+ applyAddChildren(childIds) {
47923
+ if (!this.index) {
47924
+ return;
47925
+ }
47926
+ childIds.forEach((childId) => {
47927
+ const foundItem = this.board.items.getById(childId);
47928
+ if (this.parent !== childId && this.getId() !== childId) {
47929
+ if (!this.index?.getById(childId) && foundItem && foundItem.itemType === "Card") {
47930
+ foundItem.transformation.apply({
47931
+ class: "Transformation",
47932
+ method: "translateTo",
47933
+ item: [this.id],
47934
+ x: this.left + (this.index?.list().length || 0) * 2,
47935
+ y: this.top
47936
+ });
47937
+ foundItem.parent = this.getId();
47938
+ this.board.items.index.remove(foundItem);
47939
+ this.index?.insert(foundItem);
47940
+ }
47941
+ }
47942
+ });
47943
+ this.updateMbr();
47944
+ this.subject.publish(this);
47945
+ }
47946
+ applyRemoveChildren(childIds) {
47947
+ if (!this.index) {
47948
+ return;
47949
+ }
47950
+ childIds.forEach((childId) => {
47951
+ const foundItem = this.index?.getById(childId);
47952
+ if (this.parent !== childId && this.getId() !== childId) {
47953
+ if (foundItem) {
47954
+ foundItem.transformation.apply({
47955
+ class: "Transformation",
47956
+ method: "translateTo",
47957
+ item: [this.id],
47958
+ x: this.left,
47959
+ y: this.top - this.getHeight() / 2
47960
+ });
47961
+ foundItem.parent = "Board";
47962
+ this.index?.remove(foundItem);
47963
+ this.board.items.index.insert(foundItem);
47964
+ }
47965
+ }
47966
+ });
47967
+ this.updateMbr();
47968
+ this.subject.publish(this);
47969
+ }
47953
47970
  getDeck() {
47954
- return this.cards;
47971
+ return this.index?.list() || [];
47955
47972
  }
47956
47973
  getTopCard() {
47957
- const cardId = this.cardIds[this.cardIds.length - 1];
47958
- return this.getCards([cardId])[0];
47974
+ const card = this.index?.list()[this.index?.list().length - 1];
47975
+ if (card) {
47976
+ this.removeChildItems(card);
47977
+ return card;
47978
+ }
47959
47979
  }
47960
47980
  getBottomCard() {
47961
- const cardId = this.cardIds[0];
47962
- return this.getCards([cardId])[0];
47981
+ const card = this.index?.list()[0];
47982
+ if (card) {
47983
+ this.removeChildItems(card);
47984
+ return card;
47985
+ }
47963
47986
  }
47964
47987
  getRandomCard() {
47965
- const cardId = this.cardIds[Math.ceil(Math.random() * this.cardIds.length) - 1];
47966
- return this.getCards([cardId])[0];
47967
- }
47968
- getCards(cardIds) {
47969
- const cards = this.findCardsOnBoard(cardIds);
47970
- this.removeCards(cards);
47971
- return cards;
47972
- }
47973
- findCardsOnBoard(cardIds) {
47974
- return cardIds.map((cardId) => {
47975
- return this.board.items.getById(cardId);
47976
- }).filter((card) => !!card);
47977
- }
47978
- updateCards() {
47979
- if (this.cardIds.length === this.cards.length) {
47980
- return this.cards;
47988
+ const card = this.index?.list()[Math.floor(Math.random() * this.index?.list().length)];
47989
+ if (card) {
47990
+ this.removeChildItems(card);
47991
+ return card;
47981
47992
  }
47982
- this.cards = this.findCardsOnBoard(this.cardIds);
47983
- return this.cards;
47984
47993
  }
47985
47994
  shuffleDeck() {
47986
- const shuffled = [...this.cardIds];
47995
+ if (!this.index) {
47996
+ return;
47997
+ }
47998
+ const shuffled = [...this.index.list()];
47987
47999
  for (let i = shuffled.length - 1;i > 0; i--) {
47988
48000
  const j = Math.floor(Math.random() * (i + 1));
47989
48001
  [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
47990
48002
  }
47991
- const cards = this.findCardsOnBoard(shuffled);
47992
- this.addCards(cards, true);
47993
- }
47994
- addCards(cards, shouldReplaceExistingCards = false) {
47995
- cards.forEach((card) => {
47996
- card.setIsInDeck(true);
47997
- });
47998
- this.board.bringToFront(cards);
47999
- this.emit({
48000
- class: "Deck",
48001
- method: "addCards",
48002
- item: [this.getId()],
48003
- newData: {
48004
- cardIds: cards.map((card) => card.getId()),
48005
- shouldReplaceExistingCards
48006
- },
48007
- prevData: { cardIds: this.cardIds, shouldReplaceExistingCards }
48008
- });
48009
- }
48010
- removeCards(cards) {
48011
- cards.forEach((card) => {
48012
- card.setIsInDeck(false);
48013
- });
48014
- this.emit({
48015
- class: "Deck",
48016
- method: "removeCards",
48017
- item: [this.getId()],
48018
- newData: { cardIds: cards.map((card) => card.getId()) },
48019
- prevData: { cardIds: this.cardIds }
48020
- });
48003
+ this.index.clear();
48004
+ shuffled.forEach((card) => this.index.insert(card));
48021
48005
  }
48022
48006
  apply(op) {
48023
48007
  super.apply(op);
48024
- switch (op.class) {
48025
- case "Deck":
48026
- switch (op.method) {
48027
- case "addCards":
48028
- if (op.newData.shouldReplaceExistingCards) {
48029
- this.cardIds = op.newData.cardIds;
48030
- this.cards = this.findCardsOnBoard(this.cardIds);
48031
- } else {
48032
- this.cardIds = [
48033
- ...op.newData.cardIds,
48034
- ...this.cardIds
48035
- ];
48036
- this.updateCards();
48037
- this.updateMbr();
48038
- }
48039
- break;
48040
- case "removeCards":
48041
- this.cardIds = this.cardIds.filter((card) => {
48042
- return !op.newData.cardIds.includes(card);
48043
- });
48044
- this.updateCards();
48045
- this.updateMbr();
48046
- break;
48047
- }
48048
- break;
48049
- }
48050
48008
  this.subject.publish(this);
48051
48009
  }
48052
48010
  updateMbr() {
48053
- const { translateX, translateY, scaleX, scaleY } = this.transformation.matrix;
48011
+ const { translateX, translateY } = this.transformation.matrix;
48012
+ const { right, bottom } = this.index.getMbr();
48054
48013
  this.left = translateX;
48055
48014
  this.top = translateY;
48056
- this.right = this.left + CARD_DIMENSIONS.width * scaleX + 2 * this.cardIds.length;
48057
- this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
48015
+ this.right = right;
48016
+ this.bottom = bottom;
48058
48017
  }
48059
48018
  deserialize(data) {
48060
48019
  super.deserialize(data);
48061
- this.updateCards();
48020
+ this.updateMbr();
48021
+ this.subject.publish(this);
48062
48022
  return this;
48063
48023
  }
48064
48024
  render(context) {
48065
48025
  if (this.transformationRenderBlock) {
48066
48026
  return;
48067
48027
  }
48068
- this.cards.forEach((card, index2) => {
48069
- card.render(context, {
48070
- top: this.top,
48071
- left: this.left,
48072
- cardPosition: index2 + 1
48073
- });
48074
- });
48028
+ super.render(context);
48075
48029
  }
48076
48030
  }
48077
48031
  registerItem({
@@ -48387,6 +48341,8 @@ class Hand extends BaseItem {
48387
48341
  ownerId;
48388
48342
  subject = new Subject;
48389
48343
  path;
48344
+ borderWidth = 1;
48345
+ backgroundColor = "#FFFFFF";
48390
48346
  constructor(board, id = "", ownerId = "") {
48391
48347
  super(board, id, defaultHandData, true);
48392
48348
  this.ownerId = ownerId;
@@ -48400,8 +48356,74 @@ class Hand extends BaseItem {
48400
48356
  }
48401
48357
  apply(op) {
48402
48358
  super.apply(op);
48359
+ switch (op.class) {
48360
+ case "Hand":
48361
+ switch (op.method) {
48362
+ case "setBorderWidth":
48363
+ this.applyBorderWidth(op.newData.borderWidth);
48364
+ break;
48365
+ case "setBackgroundColor":
48366
+ this.applyBackgroundColor(op.newData.backgroundColor);
48367
+ break;
48368
+ case "setBorderColor":
48369
+ this.applyBorderColor(op.newData.borderColor);
48370
+ break;
48371
+ }
48372
+ break;
48373
+ }
48403
48374
  this.subject.publish(this);
48404
48375
  }
48376
+ getBackgroundColor() {
48377
+ return this.backgroundColor;
48378
+ }
48379
+ getBorderStyle() {
48380
+ return this.borderStyle;
48381
+ }
48382
+ getStrokeColor() {
48383
+ return this.borderColor;
48384
+ }
48385
+ getStrokeWidth() {
48386
+ return this.borderWidth;
48387
+ }
48388
+ applyBackgroundColor(backgroundColor) {
48389
+ this.backgroundColor = backgroundColor;
48390
+ this.path.setBackgroundColor(backgroundColor);
48391
+ }
48392
+ setBackgroundColor(backgroundColor) {
48393
+ this.emit({
48394
+ class: "Dice",
48395
+ method: "setBackgroundColor",
48396
+ item: [this.getId()],
48397
+ newData: { backgroundColor },
48398
+ prevData: { backgroundColor: this.backgroundColor }
48399
+ });
48400
+ }
48401
+ applyBorderWidth(borderWidth) {
48402
+ this.borderWidth = borderWidth;
48403
+ this.path.setBorderWidth(borderWidth);
48404
+ }
48405
+ setBorderWidth(borderWidth) {
48406
+ this.emit({
48407
+ class: "Dice",
48408
+ method: "setBorderWidth",
48409
+ item: [this.getId()],
48410
+ newData: { borderWidth },
48411
+ prevData: { borderWidth: this.borderWidth }
48412
+ });
48413
+ }
48414
+ applyBorderColor(borderColor) {
48415
+ this.borderColor = borderColor;
48416
+ this.path.setBorderColor(borderColor);
48417
+ }
48418
+ setBorderColor(borderColor) {
48419
+ this.emit({
48420
+ class: "Dice",
48421
+ method: "setBorderColor",
48422
+ item: [this.getId()],
48423
+ newData: { borderColor },
48424
+ prevData: { borderColor: this.borderColor }
48425
+ });
48426
+ }
48405
48427
  applyOwnerId(ownerId) {
48406
48428
  this.ownerId = ownerId;
48407
48429
  }
@@ -48410,6 +48432,7 @@ class Hand extends BaseItem {
48410
48432
  this.path.transform(this.transformation.matrix);
48411
48433
  this.path.setBackgroundColor(this.backgroundColor);
48412
48434
  this.path.setBorderColor(this.borderColor);
48435
+ this.path.setBorderWidth(this.borderWidth);
48413
48436
  }
48414
48437
  updateMbr() {
48415
48438
  const { left, top, right, bottom } = this.path.getMbr();