UNPKG

decentraland-ui

Version:

Decentraland's UI components and styles

296 lines (295 loc) 16.1 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EmoteControls = void 0; /* eslint-disable @typescript-eslint/no-empty-function */ var React = __importStar(require("react")); var classnames_1 = __importDefault(require("classnames")); var preview_emote_event_type_1 = require("@dcl/schemas/dist/dapps/preview/preview-emote-event-type"); var Button_1 = require("../Button/Button"); var __1 = require("../.."); require("./EmoteControls.css"); var EmoteControls = /** @class */ (function (_super) { __extends(EmoteControls, _super); function EmoteControls() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.state = { isPlaying: false, frame: 0, isSoundEnabled: false, hasSound: undefined, shouldResumePlaying: false, isChangingFrame: false }; _this.handleAnimationLoop = function () { _this.setState({ frame: 0 }); }; _this.handleAnimationEnd = function () { var _a = _this.state, frame = _a.frame, length = _a.length; _this.setState({ isPlaying: false, frame: frame >= Math.floor(length * 100) ? 0 : frame // using to Fixed(1) }); }; _this.handleAnimationPause = function () { _this.setState({ isPlaying: false }); }; _this.handleAnimationPlay = function () { return __awaiter(_this, void 0, void 0, function () { var _a, stateLength, isChangingFrame, emoteLength; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = this.state, stateLength = _a.length, isChangingFrame = _a.isChangingFrame; emoteLength = stateLength; if (!!stateLength) return [3 /*break*/, 2]; return [4 /*yield*/, this.previewController.emote.getLength()]; case 1: emoteLength = _b.sent(); _b.label = 2; case 2: if (!isChangingFrame) { this.setState({ isPlaying: true, length: emoteLength }); } return [2 /*return*/]; } }); }); }; _this.handleAnimationPlaying = function (_a) { var length = _a.length; _this.setState({ frame: Math.ceil((length !== null && length !== void 0 ? length : 0) * 100) }); }; _this.handlePlay = function () { return __awaiter(_this, void 0, void 0, function () { var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, ((_a = this.previewController) === null || _a === void 0 ? void 0 : _a.emote.play())]; case 1: _b.sent(); return [2 /*return*/]; } }); }); }; _this.handlePause = function () { return __awaiter(_this, void 0, void 0, function () { var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, ((_a = this.previewController) === null || _a === void 0 ? void 0 : _a.emote.pause())]; case 1: _b.sent(); return [2 /*return*/]; } }); }); }; _this.handlePlayPause = function () { return __awaiter(_this, void 0, void 0, function () { var isPlaying; return __generator(this, function (_a) { switch (_a.label) { case 0: isPlaying = this.state.isPlaying; if (!isPlaying) return [3 /*break*/, 2]; return [4 /*yield*/, this.handlePause()]; case 1: _a.sent(); return [3 /*break*/, 4]; case 2: return [4 /*yield*/, this.handlePlay()]; case 3: _a.sent(); _a.label = 4; case 4: return [2 /*return*/]; } }); }); }; _this.handleSoundToggle = function () { var _a, _b; var isSoundEnabled = _this.state.isSoundEnabled; if (isSoundEnabled) { (_a = _this.previewController) === null || _a === void 0 ? void 0 : _a.emote.disableSound(); } else { (_b = _this.previewController) === null || _b === void 0 ? void 0 : _b.emote.enableSound(); } _this.setState({ isSoundEnabled: !isSoundEnabled }); }; _this.handleFrameChange = function (value) { return __awaiter(_this, void 0, void 0, function () { var targetValue, _a, isPlaying, length; var _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: if (isNaN(value)) { return [2 /*return*/]; } targetValue = value; _a = this.state, isPlaying = _a.isPlaying, length = _a.length; if (length * 100 < value) { targetValue = length * 100; } this.setState({ frame: targetValue, isChangingFrame: true }); if (!isPlaying) return [3 /*break*/, 2]; return [4 /*yield*/, ((_b = this.previewController) === null || _b === void 0 ? void 0 : _b.emote.pause())]; case 1: _d.sent(); this.setState({ shouldResumePlaying: true }); _d.label = 2; case 2: return [4 /*yield*/, ((_c = this.previewController) === null || _c === void 0 ? void 0 : _c.emote.goTo(targetValue / 100))]; case 3: _d.sent(); return [2 /*return*/]; } }); }); }; _this.handleMouseUp = function () { return __awaiter(_this, void 0, void 0, function () { var shouldResumePlaying; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: shouldResumePlaying = this.state.shouldResumePlaying; this.setState({ isChangingFrame: false }); if (!shouldResumePlaying) return [3 /*break*/, 2]; return [4 /*yield*/, ((_a = this.previewController) === null || _a === void 0 ? void 0 : _a.emote.play())]; case 1: _b.sent(); this.setState({ shouldResumePlaying: false }); _b.label = 2; case 2: return [2 /*return*/]; } }); }); }; return _this; } EmoteControls.prototype.componentDidMount = function () { var wearablePreviewController = this.props.wearablePreviewController; var previewController = wearablePreviewController !== null && wearablePreviewController !== void 0 ? wearablePreviewController : __1.WearablePreview.createController(this.props.wearablePreviewId); previewController.emote.events.on(preview_emote_event_type_1.PreviewEmoteEventType.ANIMATION_PLAY, this.handleAnimationPlay); previewController.emote.events.on(preview_emote_event_type_1.PreviewEmoteEventType.ANIMATION_PLAYING, this.handleAnimationPlaying); previewController.emote.events.on(preview_emote_event_type_1.PreviewEmoteEventType.ANIMATION_END, this.handleAnimationEnd); previewController.emote.events.on(preview_emote_event_type_1.PreviewEmoteEventType.ANIMATION_PAUSE, this.handleAnimationPause); previewController.emote.events.on(preview_emote_event_type_1.PreviewEmoteEventType.ANIMATION_LOOP, this.handleAnimationLoop); this.previewController = previewController; }; EmoteControls.prototype.componentDidUpdate = function () { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { if (this.state.hasSound === undefined) { this.previewController.emote .hasSound() .then(function (hasSound) { return _this.setState({ hasSound: hasSound }); }); } return [2 /*return*/]; }); }); }; EmoteControls.prototype.render = function () { var _a; var _this = this; var _b = this.props, className = _b.className, hideFrameInput = _b.hideFrameInput, hidePlayButton = _b.hidePlayButton, hideSoundButton = _b.hideSoundButton, hideProgressInput = _b.hideProgressInput, renderPlayButton = _b.renderPlayButton, renderSoundButton = _b.renderSoundButton, renderProgressBar = _b.renderProgressBar, renderFrameInput = _b.renderFrameInput; var _c = this.state, frame = _c.frame, isPlaying = _c.isPlaying, length = _c.length, hasSound = _c.hasSound, isSoundEnabled = _c.isSoundEnabled; return (React.createElement("div", { className: "EmoteControls ".concat(className) }, hidePlayButton ? null : renderPlayButton ? (renderPlayButton({ isPlaying: isPlaying, onPlay: this.handlePlay, onPause: this.handlePause, onToggle: this.handlePlayPause })) : (React.createElement(Button_1.Button, { className: "play-control", onClick: this.handlePlayPause }, React.createElement(__1.Icon, { name: isPlaying ? 'pause' : 'play' }))), !hideProgressInput ? (renderProgressBar ? (renderProgressBar({ frame: frame, length: length !== null && length !== void 0 ? length : 0, onChange: this.handleFrameChange, onMouseUp: this.handleMouseUp })) : (React.createElement("input", { type: "range", value: frame, max: Math.ceil((length !== null && length !== void 0 ? length : 0) * 100), min: 0, step: "1", onChange: function (e) { return _this.handleFrameChange(Number(e.target.value)); }, onMouseUp: this.handleMouseUp }))) : null, hideFrameInput ? null : (React.createElement("div", { className: "frame-control" }, renderFrameInput ? (renderFrameInput({ frame: Math.round(frame / 100), onChange: function (value) { return _this.handleFrameChange(value * 100); } })) : (React.createElement("input", { value: Math.round(frame / 100), onChange: function (e) { return _this.handleFrameChange(Number(e.target.value) * 100); } })))), hideSoundButton || !hasSound ? null : renderSoundButton ? (renderSoundButton({ isSoundEnabled: isSoundEnabled, hasSound: hasSound, onToggle: this.handleSoundToggle })) : (React.createElement(Button_1.Button, { className: (0, classnames_1.default)('sound-control', (_a = {}, _a['muted'] = !isSoundEnabled, _a)), onClick: this.handleSoundToggle })))); }; return EmoteControls; }(React.PureComponent)); exports.EmoteControls = EmoteControls;