2017-03-05 17:48:13 +00:00
|
|
|
module TS.SpaceTac.UI {
|
2017-03-05 23:29:02 +00:00
|
|
|
/**
|
|
|
|
* Interface for any graphical item that may receive an equipment as drop destination
|
|
|
|
*/
|
|
|
|
export interface CharacterEquipmentDestination {
|
|
|
|
canDropEquipment(equipment: Equipment, x: number, y: number): CharacterEquipmentDrop | null;
|
|
|
|
}
|
|
|
|
|
2017-03-05 17:48:13 +00:00
|
|
|
/**
|
|
|
|
* Display a ship equipment, either attached to a slot, in cargo, or being dragged down
|
|
|
|
*/
|
2017-03-15 21:40:19 +00:00
|
|
|
export class CharacterEquipment extends Phaser.Button {
|
2017-03-14 17:48:04 +00:00
|
|
|
equipment: Equipment;
|
|
|
|
|
2017-03-05 17:48:13 +00:00
|
|
|
constructor(sheet: CharacterSheet, equipment: Equipment) {
|
2017-03-09 19:11:19 +00:00
|
|
|
let icon = sheet.game.cache.checkImageKey(`equipment-${equipment.code}`) ? `equipment-${equipment.code}` : `battle-actions-${equipment.action.code}`;
|
|
|
|
super(sheet.game, 0, 0, icon);
|
2017-03-05 17:48:13 +00:00
|
|
|
|
2017-03-14 17:48:04 +00:00
|
|
|
this.equipment = equipment;
|
|
|
|
|
2017-03-05 17:48:13 +00:00
|
|
|
this.anchor.set(0.5, 0.5);
|
|
|
|
this.scale.set(0.5, 0.5);
|
2017-03-05 23:29:02 +00:00
|
|
|
|
2017-03-14 17:48:04 +00:00
|
|
|
this.setupDragDrop(sheet);
|
2017-03-15 21:40:19 +00:00
|
|
|
|
|
|
|
// TODO better tooltip
|
|
|
|
sheet.view.tooltip.bindStaticText(this, equipment.name);
|
2017-03-14 17:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable dragging to another slot
|
|
|
|
*/
|
|
|
|
setupDragDrop(sheet: CharacterSheet) {
|
2017-03-05 23:29:02 +00:00
|
|
|
this.inputEnabled = true;
|
|
|
|
this.input.enableDrag(false, true);
|
|
|
|
|
2017-03-14 17:48:04 +00:00
|
|
|
let origin: [number, number, number, number] | null = null;
|
2017-03-05 23:29:02 +00:00
|
|
|
let drop: CharacterEquipmentDrop | null = null;
|
|
|
|
this.events.onDragStart.add(() => {
|
2017-03-14 17:48:04 +00:00
|
|
|
origin = [this.x, this.y, this.scale.x, this.scale.y];
|
|
|
|
this.scale.set(0.5, 0.5);
|
2017-03-05 23:29:02 +00:00
|
|
|
this.alpha = 0.8;
|
|
|
|
});
|
|
|
|
this.events.onDragUpdate.add(() => {
|
2017-03-14 17:48:04 +00:00
|
|
|
drop = sheet.canDropEquipment(this.equipment, this.x, this.y);
|
2017-03-05 23:29:02 +00:00
|
|
|
});
|
|
|
|
this.events.onDragStop.add(() => {
|
|
|
|
if (drop) {
|
2017-03-14 17:48:04 +00:00
|
|
|
drop.callback(this.equipment);
|
2017-03-05 23:29:02 +00:00
|
|
|
sheet.refresh();
|
|
|
|
} else {
|
|
|
|
if (origin) {
|
|
|
|
this.position.set(origin[0], origin[1]);
|
2017-03-14 17:48:04 +00:00
|
|
|
this.scale.set(origin[2], origin[3]);
|
2017-03-05 23:29:02 +00:00
|
|
|
origin = null;
|
|
|
|
}
|
|
|
|
this.alpha = 1;
|
|
|
|
}
|
|
|
|
});
|
2017-03-05 17:48:13 +00:00
|
|
|
}
|
2017-03-14 17:48:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the scaling of container in which the equipment icon is snapped
|
|
|
|
*/
|
|
|
|
setContainerScale(scale: number) {
|
|
|
|
this.scale.set(0.5 * scale, 0.5 * scale);
|
|
|
|
}
|
2017-03-05 17:48:13 +00:00
|
|
|
}
|
|
|
|
}
|