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.
@@ -47638,8 +47638,7 @@ var defaultCardData = {
47638
47638
  itemType: "Card",
47639
47639
  isOpen: false,
47640
47640
  faceUrl: "",
47641
- backsideUrl: "",
47642
- isInDeck: false
47641
+ backsideUrl: ""
47643
47642
  };
47644
47643
  var CARD_DIMENSIONS = { width: 250, height: 400 };
47645
47644
 
@@ -47648,7 +47647,6 @@ class Card extends BaseItem {
47648
47647
  faceUrl = "";
47649
47648
  backsideUrl = "";
47650
47649
  isOpen = false;
47651
- isInDeck = false;
47652
47650
  throttledBringToFront;
47653
47651
  face = null;
47654
47652
  backside = null;
@@ -47684,35 +47682,20 @@ class Card extends BaseItem {
47684
47682
  };
47685
47683
  this.updateImageToRender();
47686
47684
  }
47687
- setIsInDeck(isInDeck) {
47688
- this.emit({
47689
- class: "Card",
47690
- method: "setIsInDeck",
47691
- item: [this.getId()],
47692
- newData: { isInDeck },
47693
- prevData: { isInDeck: this.isInDeck }
47694
- });
47695
- }
47696
47685
  updateImageToRender() {
47697
47686
  this.imageToRender = this.backside;
47698
47687
  if (this.isOpen) {
47699
47688
  this.imageToRender = this.face;
47700
47689
  }
47701
47690
  }
47702
- render(context, deckRenderData) {
47703
- if (this.transformationRenderBlock || this.isInDeck && !deckRenderData) {
47691
+ render(context) {
47692
+ if (this.transformationRenderBlock) {
47704
47693
  return;
47705
47694
  }
47706
47695
  const ctx = context.ctx;
47707
47696
  if (this.imageToRender && this.imageToRender.complete) {
47708
47697
  ctx.save();
47709
- let left = this.left;
47710
- let top = this.top;
47711
- if (deckRenderData) {
47712
- left = deckRenderData.left + 2 * deckRenderData.cardPosition;
47713
- top = deckRenderData.top;
47714
- }
47715
- ctx.drawImage(this.imageToRender, left, top, CARD_DIMENSIONS.width, CARD_DIMENSIONS.height);
47698
+ ctx.drawImage(this.imageToRender, this.left, this.top, CARD_DIMENSIONS.width, CARD_DIMENSIONS.height);
47716
47699
  ctx.restore();
47717
47700
  }
47718
47701
  }
@@ -47723,12 +47706,6 @@ class Card extends BaseItem {
47723
47706
  this.right = this.left + CARD_DIMENSIONS.width * scaleX;
47724
47707
  this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
47725
47708
  }
47726
- getMbr() {
47727
- if (this.isInDeck) {
47728
- return new Mbr(1e4, 1e4, 1e4, 1e4);
47729
- }
47730
- return super.getMbr();
47731
- }
47732
47709
  getPath() {
47733
47710
  return new Path(this.getMbr().getLines());
47734
47711
  }
@@ -47761,9 +47738,6 @@ class Card extends BaseItem {
47761
47738
  this.isOpen = op.newData.isOpen;
47762
47739
  this.updateImageToRender();
47763
47740
  break;
47764
- case "setIsInDeck":
47765
- this.isInDeck = op.newData.isInDeck;
47766
- break;
47767
47741
  }
47768
47742
  break;
47769
47743
  }
@@ -47776,22 +47750,17 @@ registerItem({
47776
47750
  });
47777
47751
  // src/Items/Examples/CardGame/Deck/Deck.ts
47778
47752
  var defaultDeckData = {
47779
- itemType: "Deck",
47780
- cardIds: []
47753
+ itemType: "Deck"
47781
47754
  };
47782
47755
 
47783
47756
  class Deck extends BaseItem {
47784
47757
  subject = new Subject;
47785
47758
  shouldUseCustomRender = false;
47786
- cardIds = [];
47787
- cards = [];
47788
47759
  constructor(board, id = "", defaultData2, cards) {
47789
- super(board, id, defaultDeckData);
47760
+ super(board, id, defaultDeckData, true);
47790
47761
  if (cards) {
47791
- this.cards = cards;
47792
- cards.forEach((card) => card.setIsInDeck(true));
47793
47762
  this.transformation.matrix = cards[cards.length - 1].transformation.matrix;
47794
- this.cardIds = cards.map((card) => card.getId());
47763
+ this.applyAddChildren(cards.map((card) => card.getId()));
47795
47764
  }
47796
47765
  this.transformation.subject.subscribe(() => {
47797
47766
  this.updateMbr();
@@ -47799,128 +47768,113 @@ class Deck extends BaseItem {
47799
47768
  });
47800
47769
  this.updateMbr();
47801
47770
  }
47771
+ applyAddChildren(childIds) {
47772
+ if (!this.index) {
47773
+ return;
47774
+ }
47775
+ childIds.forEach((childId) => {
47776
+ const foundItem = this.board.items.getById(childId);
47777
+ if (this.parent !== childId && this.getId() !== childId) {
47778
+ if (!this.index?.getById(childId) && foundItem && foundItem.itemType === "Card") {
47779
+ foundItem.transformation.apply({
47780
+ class: "Transformation",
47781
+ method: "translateTo",
47782
+ item: [this.id],
47783
+ x: this.left + (this.index?.list().length || 0) * 2,
47784
+ y: this.top
47785
+ });
47786
+ foundItem.parent = this.getId();
47787
+ this.board.items.index.remove(foundItem);
47788
+ this.index?.insert(foundItem);
47789
+ }
47790
+ }
47791
+ });
47792
+ this.updateMbr();
47793
+ this.subject.publish(this);
47794
+ }
47795
+ applyRemoveChildren(childIds) {
47796
+ if (!this.index) {
47797
+ return;
47798
+ }
47799
+ childIds.forEach((childId) => {
47800
+ const foundItem = this.index?.getById(childId);
47801
+ if (this.parent !== childId && this.getId() !== childId) {
47802
+ if (foundItem) {
47803
+ foundItem.transformation.apply({
47804
+ class: "Transformation",
47805
+ method: "translateTo",
47806
+ item: [this.id],
47807
+ x: this.left,
47808
+ y: this.top - this.getHeight() / 2
47809
+ });
47810
+ foundItem.parent = "Board";
47811
+ this.index?.remove(foundItem);
47812
+ this.board.items.index.insert(foundItem);
47813
+ }
47814
+ }
47815
+ });
47816
+ this.updateMbr();
47817
+ this.subject.publish(this);
47818
+ }
47802
47819
  getDeck() {
47803
- return this.cards;
47820
+ return this.index?.list() || [];
47804
47821
  }
47805
47822
  getTopCard() {
47806
- const cardId = this.cardIds[this.cardIds.length - 1];
47807
- return this.getCards([cardId])[0];
47823
+ const card = this.index?.list()[this.index?.list().length - 1];
47824
+ if (card) {
47825
+ this.removeChildItems(card);
47826
+ return card;
47827
+ }
47808
47828
  }
47809
47829
  getBottomCard() {
47810
- const cardId = this.cardIds[0];
47811
- return this.getCards([cardId])[0];
47830
+ const card = this.index?.list()[0];
47831
+ if (card) {
47832
+ this.removeChildItems(card);
47833
+ return card;
47834
+ }
47812
47835
  }
47813
47836
  getRandomCard() {
47814
- const cardId = this.cardIds[Math.ceil(Math.random() * this.cardIds.length) - 1];
47815
- return this.getCards([cardId])[0];
47816
- }
47817
- getCards(cardIds) {
47818
- const cards = this.findCardsOnBoard(cardIds);
47819
- this.removeCards(cards);
47820
- return cards;
47821
- }
47822
- findCardsOnBoard(cardIds) {
47823
- return cardIds.map((cardId) => {
47824
- return this.board.items.getById(cardId);
47825
- }).filter((card) => !!card);
47826
- }
47827
- updateCards() {
47828
- if (this.cardIds.length === this.cards.length) {
47829
- return this.cards;
47837
+ const card = this.index?.list()[Math.floor(Math.random() * this.index?.list().length)];
47838
+ if (card) {
47839
+ this.removeChildItems(card);
47840
+ return card;
47830
47841
  }
47831
- this.cards = this.findCardsOnBoard(this.cardIds);
47832
- return this.cards;
47833
47842
  }
47834
47843
  shuffleDeck() {
47835
- const shuffled = [...this.cardIds];
47844
+ if (!this.index) {
47845
+ return;
47846
+ }
47847
+ const shuffled = [...this.index.list()];
47836
47848
  for (let i = shuffled.length - 1;i > 0; i--) {
47837
47849
  const j = Math.floor(Math.random() * (i + 1));
47838
47850
  [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
47839
47851
  }
47840
- const cards = this.findCardsOnBoard(shuffled);
47841
- this.addCards(cards, true);
47842
- }
47843
- addCards(cards, shouldReplaceExistingCards = false) {
47844
- cards.forEach((card) => {
47845
- card.setIsInDeck(true);
47846
- });
47847
- this.board.bringToFront(cards);
47848
- this.emit({
47849
- class: "Deck",
47850
- method: "addCards",
47851
- item: [this.getId()],
47852
- newData: {
47853
- cardIds: cards.map((card) => card.getId()),
47854
- shouldReplaceExistingCards
47855
- },
47856
- prevData: { cardIds: this.cardIds, shouldReplaceExistingCards }
47857
- });
47858
- }
47859
- removeCards(cards) {
47860
- cards.forEach((card) => {
47861
- card.setIsInDeck(false);
47862
- });
47863
- this.emit({
47864
- class: "Deck",
47865
- method: "removeCards",
47866
- item: [this.getId()],
47867
- newData: { cardIds: cards.map((card) => card.getId()) },
47868
- prevData: { cardIds: this.cardIds }
47869
- });
47852
+ this.index.clear();
47853
+ shuffled.forEach((card) => this.index.insert(card));
47870
47854
  }
47871
47855
  apply(op) {
47872
47856
  super.apply(op);
47873
- switch (op.class) {
47874
- case "Deck":
47875
- switch (op.method) {
47876
- case "addCards":
47877
- if (op.newData.shouldReplaceExistingCards) {
47878
- this.cardIds = op.newData.cardIds;
47879
- this.cards = this.findCardsOnBoard(this.cardIds);
47880
- } else {
47881
- this.cardIds = [
47882
- ...op.newData.cardIds,
47883
- ...this.cardIds
47884
- ];
47885
- this.updateCards();
47886
- this.updateMbr();
47887
- }
47888
- break;
47889
- case "removeCards":
47890
- this.cardIds = this.cardIds.filter((card) => {
47891
- return !op.newData.cardIds.includes(card);
47892
- });
47893
- this.updateCards();
47894
- this.updateMbr();
47895
- break;
47896
- }
47897
- break;
47898
- }
47899
47857
  this.subject.publish(this);
47900
47858
  }
47901
47859
  updateMbr() {
47902
- const { translateX, translateY, scaleX, scaleY } = this.transformation.matrix;
47860
+ const { translateX, translateY } = this.transformation.matrix;
47861
+ const { right, bottom } = this.index.getMbr();
47903
47862
  this.left = translateX;
47904
47863
  this.top = translateY;
47905
- this.right = this.left + CARD_DIMENSIONS.width * scaleX + 2 * this.cardIds.length;
47906
- this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
47864
+ this.right = right;
47865
+ this.bottom = bottom;
47907
47866
  }
47908
47867
  deserialize(data) {
47909
47868
  super.deserialize(data);
47910
- this.updateCards();
47869
+ this.updateMbr();
47870
+ this.subject.publish(this);
47911
47871
  return this;
47912
47872
  }
47913
47873
  render(context) {
47914
47874
  if (this.transformationRenderBlock) {
47915
47875
  return;
47916
47876
  }
47917
- this.cards.forEach((card, index2) => {
47918
- card.render(context, {
47919
- top: this.top,
47920
- left: this.left,
47921
- cardPosition: index2 + 1
47922
- });
47923
- });
47877
+ super.render(context);
47924
47878
  }
47925
47879
  }
47926
47880
  registerItem({
@@ -48236,6 +48190,8 @@ class Hand extends BaseItem {
48236
48190
  ownerId;
48237
48191
  subject = new Subject;
48238
48192
  path;
48193
+ borderWidth = 1;
48194
+ backgroundColor = "#FFFFFF";
48239
48195
  constructor(board, id = "", ownerId = "") {
48240
48196
  super(board, id, defaultHandData, true);
48241
48197
  this.ownerId = ownerId;
@@ -48249,8 +48205,74 @@ class Hand extends BaseItem {
48249
48205
  }
48250
48206
  apply(op) {
48251
48207
  super.apply(op);
48208
+ switch (op.class) {
48209
+ case "Hand":
48210
+ switch (op.method) {
48211
+ case "setBorderWidth":
48212
+ this.applyBorderWidth(op.newData.borderWidth);
48213
+ break;
48214
+ case "setBackgroundColor":
48215
+ this.applyBackgroundColor(op.newData.backgroundColor);
48216
+ break;
48217
+ case "setBorderColor":
48218
+ this.applyBorderColor(op.newData.borderColor);
48219
+ break;
48220
+ }
48221
+ break;
48222
+ }
48252
48223
  this.subject.publish(this);
48253
48224
  }
48225
+ getBackgroundColor() {
48226
+ return this.backgroundColor;
48227
+ }
48228
+ getBorderStyle() {
48229
+ return this.borderStyle;
48230
+ }
48231
+ getStrokeColor() {
48232
+ return this.borderColor;
48233
+ }
48234
+ getStrokeWidth() {
48235
+ return this.borderWidth;
48236
+ }
48237
+ applyBackgroundColor(backgroundColor) {
48238
+ this.backgroundColor = backgroundColor;
48239
+ this.path.setBackgroundColor(backgroundColor);
48240
+ }
48241
+ setBackgroundColor(backgroundColor) {
48242
+ this.emit({
48243
+ class: "Dice",
48244
+ method: "setBackgroundColor",
48245
+ item: [this.getId()],
48246
+ newData: { backgroundColor },
48247
+ prevData: { backgroundColor: this.backgroundColor }
48248
+ });
48249
+ }
48250
+ applyBorderWidth(borderWidth) {
48251
+ this.borderWidth = borderWidth;
48252
+ this.path.setBorderWidth(borderWidth);
48253
+ }
48254
+ setBorderWidth(borderWidth) {
48255
+ this.emit({
48256
+ class: "Dice",
48257
+ method: "setBorderWidth",
48258
+ item: [this.getId()],
48259
+ newData: { borderWidth },
48260
+ prevData: { borderWidth: this.borderWidth }
48261
+ });
48262
+ }
48263
+ applyBorderColor(borderColor) {
48264
+ this.borderColor = borderColor;
48265
+ this.path.setBorderColor(borderColor);
48266
+ }
48267
+ setBorderColor(borderColor) {
48268
+ this.emit({
48269
+ class: "Dice",
48270
+ method: "setBorderColor",
48271
+ item: [this.getId()],
48272
+ newData: { borderColor },
48273
+ prevData: { borderColor: this.borderColor }
48274
+ });
48275
+ }
48254
48276
  applyOwnerId(ownerId) {
48255
48277
  this.ownerId = ownerId;
48256
48278
  }
@@ -48259,6 +48281,7 @@ class Hand extends BaseItem {
48259
48281
  this.path.transform(this.transformation.matrix);
48260
48282
  this.path.setBackgroundColor(this.backgroundColor);
48261
48283
  this.path.setBorderColor(this.borderColor);
48284
+ this.path.setBorderWidth(this.borderWidth);
48262
48285
  }
48263
48286
  updateMbr() {
48264
48287
  const { left, top, right, bottom } = this.path.getMbr();