I_GRIN_I

table.js

Jan 4th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var _;
  2.  
  3. (function() {
  4.     _ = GameUI.CustomUIConfig().UtilLibrary;
  5.  
  6.     function Card(options) {
  7.         //$.Msg("Card constructor", options.rank, options.suit);
  8.         var self = this;
  9.         this._rank = options.rank;
  10.         this._suit = options.suit;
  11.         this._faceUp = options.faceUp || false;
  12.         options.layoutfile = options.layoutfile || "file://{resources}/layout/custom_game/cards/card.xml";
  13.         _.Panel.call(this, options);
  14.         if (this.panel) {
  15.             this.panel.SetHasClass("card-" + this._rank + this._suit, this._faceUp);
  16.             this.panel.SetHasClass("card-back", !this._faceUp);
  17.         }
  18.     }
  19.     _.inherits(Card, _.Panel);
  20.     Card.prototype.value = function() {
  21.         return this._rank + this._suit;
  22.     }
  23.     Card.prototype.suitColor = function() {
  24.         return (this._suit == "c" || this._suit == "s") ? "black" : "red";
  25.     }
  26.     Card.prototype.rank = function(value) {
  27.         if (value == undefined) return this._rank;
  28.         if (this.panel) this.panel.SetHasClass("card-" + this._rank + this._suit, false);
  29.         this._rank = value;
  30.         if (this.panel) this.panel.SetHasClass("card-" + this._rank + this._suit, true);
  31.     }
  32.     Card.prototype.suit = function(value) {
  33.         if (value == undefined) return this._suit;
  34.         if (this.panel) this.panel.SetHasClass("card-" + this._rank + this._suit, false);
  35.         this._suit = value;
  36.         if (this.panel) this.panel.SetHasClass("card-" + this._rank + this._suit, true);
  37.     }
  38.     Card.prototype.faceUp = function(value) {
  39.         if (value == undefined) return this._faceUp;
  40.         this._faceUp = value;
  41.         if (this.panel) {
  42.             this.panel.SetHasClass("card-" + this._rank + this._suit, this._faceUp);
  43.             this.panel.SetHasClass("card-back", !this._faceUp);
  44.             this.panel.style.z = "0px";
  45.             this.panel.style["z-index"] = this._zIndex;
  46.  
  47.             this.panel.SetHasClass("card-flip-y", true);
  48.             var self = this;
  49.             $.Schedule(0.2, function() {
  50.                 self.panel.SetHasClass("card-animate", false);
  51.                 self.panel.SetHasClass("card-flip-y", false);
  52.                 self.panel.SetHasClass("card-animate", true);
  53.             });
  54.         }
  55.     }
  56.     Card.prototype.render = function() {
  57.         if (this.panel) {
  58.             //$.Msg('card render', this.style, this.value());
  59.             this.panel.style.x = this.style.x() + this.style.x.suffix;
  60.             this.panel.style.y = this.style.y() + this.style.y.suffix;
  61.             this.panel.style.zIndex = this.style.zIndex();
  62.             this.panel.visible = this.visible();
  63.             this.panel.SetDraggable(this.draggable());
  64.         }
  65.     }
  66.     Card.prototype.OnMouseOver = function() {
  67.         // highlight this panel as a drop target
  68.         if (this.panel) this.panel.AddClass("highlight");
  69.     }
  70.     Card.prototype.OnMouseOut = function() {
  71.         // highlight this panel as a drop target
  72.         if (this.panel) this.panel.RemoveClass("highlight");
  73.     }
  74.     Card.prototype.OnDragEnter = function(a, draggedPanel) {
  75.         //$.Msg("Card OnDragEnter");
  76.         // highlight this panel as a drop target
  77.         if (this.droppable() && this.panel) this.panel.AddClass("potential_drop_target");
  78.     }
  79.     Card.prototype.OnDragLeave = function(panelId, draggedPanel, card) {
  80.         // un-highlight this panel
  81.         if (this.panel) this.panel.RemoveClass("potential_drop_target");
  82.     }
  83.     Card.prototype.OnDragStart = function(panelId, dragCallbacks, card) {
  84.         if (!this.draggable() || !this.panel) return;
  85.         this.draggable(false);
  86.  
  87.         // create a temp panel that will be dragged around
  88.         var displayPanel = $.CreatePanel("Panel", this.panel, "dragImage");
  89.         displayPanel.BLoadLayout("file://{resources}/layout/custom_game/cards/card.xml", false, false);
  90.         displayPanel.SetHasClass("card-" + this.rank() + this.suit(), this.faceUp());
  91.         displayPanel.SetHasClass("card-back", !this.faceUp());
  92.  
  93.         displayPanel.card = this; // whether the drag was successful
  94.  
  95.         // hook up the display panel, and specify the panel offset from the cursor
  96.         dragCallbacks.displayPanel = displayPanel;
  97.         dragCallbacks.offsetX = 50;
  98.         dragCallbacks.offsetY = 50;
  99.  
  100.         // grey out the source panel while dragging
  101.         this.panel.AddClass("dragging_from");
  102.     }
  103.     Card.prototype.OnDragEnd = function(panelId, draggedPanel, card) {
  104.         //$.Msg("Card OnDragEnd");
  105.         // kill the display panel
  106.         draggedPanel.DeleteAsync(0);
  107.  
  108.         this.draggable(true);
  109.  
  110.         // restore our look
  111.         this.panel.RemoveClass("dragging_from");
  112.     }
  113.  
  114.     function CardCollection(options) {
  115.         //$.Msg("CardCollection constructor");
  116.         this._cardOffset = options.cardOffset || {
  117.             x: 0,
  118.             y: 30
  119.         };
  120.         options.rank = "slot";
  121.         options.suit = "test";
  122.         var self = _.PanelCollection.call(this, options);
  123.         //$.Msg("CardCollection constructor", self.panel.paneltype);
  124.         options.panel = self.panel;
  125.         self.slot = new Card(options);
  126.         Object.setPrototypeOf(self, Object.getPrototypeOf(this));
  127.         self.updateStyles(options.style);
  128.         for (var event in self.slot._handlers) {
  129.             self.slot.unregisterHandler(event);
  130.             self.slot.registerHandler(event, self.fireEvent.bind(self, event));
  131.         }
  132.         self.slot.render();
  133.         return self;
  134.     }
  135.     _.inherits(CardCollection, _.PanelCollection);
  136.     CardCollection.prototype.OnDragEnter = function(a, draggedPanel) {
  137.         this.slot.OnDragEnter.apply(this.slot, arguments);
  138.     }
  139.     CardCollection.prototype.OnDragLeave = function(a, draggedPanel) {
  140.         this.slot.OnDragLeave.apply(this.slot, arguments);
  141.     }
  142.     CardCollection.prototype.cardOffset = function(value) {
  143.         if (value == undefined) return this._cardOffset;
  144.         this._cardOffset = value;
  145.         this.render();
  146.     }
  147.     CardCollection.prototype.last = function() {
  148.         if (this.length) {
  149.             return this[this.length - 1];
  150.         }
  151.         return null;
  152.     }
  153.     CardCollection.prototype.render = function() {
  154.         var self = this;
  155.         this.slot.render();
  156.         this.forEach(function(card, i) {
  157.             card.style.zIndex(self.style.zIndex() + i + 1);
  158.             card.style.x(self.style.x() + self.cardOffset().x * i);
  159.             card.style.y(self.style.y() + self.cardOffset().y * i);
  160.             card.visible(self.visible() && card.visible());
  161.         });
  162.     };
  163.  
  164.     function Pile(options) {
  165.         _.Mixable.call(this);
  166.         var self = this;
  167.         this.items = new _.Collection();
  168.         this.cardOffsetX = _.observable(options.cardOffsetX || 0);
  169.         this.cardOffsetX.subscribe(function() {
  170.             self.render()
  171.         });
  172.         this.cardOffsetY = _.observable(options.cardOffsetY || 0);
  173.         this.cardOffsetY.subscribe(function() {
  174.             self.render()
  175.         });
  176.         if (options.slot) {
  177.             this.slot = new Card(options.slot);
  178.             for (var event in this.slot._handlers) {
  179.                 this.slot.unregisterHandler(event);
  180.                 this.slot.registerHandler(event, this.fireEvent.bind(this, event));
  181.             }
  182.         }
  183.     }
  184.     _.inherits(Pile, _.Mixable);
  185.     _.mixin(Pile, _.mixInEvents);
  186.     _.mixin(Pile, _.mixInHandlers);
  187.     _.extend(Pile.prototype, {
  188.         get: function(i) {
  189.             return this.items.get(i);
  190.         },
  191.         isEmpty: function() {
  192.             return this.items.isEmpty();
  193.         },
  194.         length: function() {
  195.             return this.items.length();
  196.         },
  197.         last: function() {
  198.             return this.items.last();
  199.         },
  200.         OnDragEnter: function(a, draggedPanel) {
  201.             this.slot.OnDragEnter.apply(this.slot, arguments);
  202.         },
  203.         OnDragLeave: function(a, draggedPanel) {
  204.             this.slot.OnDragLeave.apply(this.slot, arguments);
  205.         },
  206.         render: function() {
  207.             var self = this;
  208.             if (this.slot) this.slot.render();
  209.             this.items.forEach(function(card, i) {
  210.                 card.style.zIndex(self.slot.style.zIndex() + i + 1);
  211.                 card.style.x(self.slot.style.x() + self.cardOffsetX() * i);
  212.                 card.style.y(self.slot.style.y() + self.cardOffsetY() * i);
  213.                 card.visible(self.slot.visible() && card.visible());
  214.             });
  215.         }
  216.     });
  217.  
  218.     function Deck(options) {
  219.         //$.Msg("CardLibrary CreateDeck", options);
  220.         this.cards = {};
  221.  
  222.         var options = _.extend({
  223.             ctor: Card,
  224.             parentPanel: $.GetContextPanel()
  225.         }, options);
  226.         options.parentPanel.RemoveAndDeleteChildren();
  227.         for (var i = 0; i < 52; i++) {
  228.             var rank = "a23456789tjqk".charAt(Math.floor(i / 4));
  229.             var suit = "cdhs".charAt(i % 4);
  230.             var cardOptions = _.extend({
  231.                 rank: rank,
  232.                 suit: suit,
  233.                 style: {
  234.                     x: i * 30,
  235.                     y: 0,
  236.                     zIndex: i
  237.                 }
  238.             }, options);
  239.             var card = new options.ctor(cardOptions);
  240.             this.cards[rank + suit] = card;
  241.         }
  242.     }
  243.  
  244.     GameUI.CustomUIConfig().CardLibrary = {
  245.         contextPanel: $.GetContextPanel(),
  246.         Deck: Deck,
  247.         Pile: Pile,
  248.         Card: Card,
  249.         CardCollection: CardCollection
  250.     };
  251.  
  252.     $.Msg("cards/table.js");
  253. })();
Add Comment
Please, Sign In to add comment