Advertisement
Guest User

Meiguro's 3DS Touch Code

a guest
Jul 25th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name Twitch Plays Pokémon Touch Controller
  3. // @version 0.3.2
  4. // @author Meiguro <[email protected]> http://meiguro.com/
  5. // @namespace https://github.com/Meiguro/twitch-plays-control
  6. // @description Add Touch controls to Twitch Plays Pokemon touch-enabled games.
  7. // @include /^https?://(www|beta)?\.?twitch.tv/twitch_?plays.*$/
  8. // @grant unsafeWindow, GM_addStyle, GM_info
  9. // @run-at document-start
  10. // @updateURL https://raw.githubusercontent.com/Meiguro/twitch-plays-control/master/twitch-plays-control.meta.js
  11. // @installURL https://raw.githubusercontent.com/Meiguro/twitch-plays-control/master/twitch-plays-control.user.js
  12. // @downloadURL https://raw.githubusercontent.com/Meiguro/twitch-plays-control/master/twitch-plays-control.user.js
  13. // ==/UserScript==
  14.  
  15. /**
  16. * v0.3.2 CHANGELOG ༼ つ ◕_◕ ༽つ
  17. *
  18. * - Fixed the coordinate range to be 1,1 - 319,239.
  19. *
  20. * v0.3.1
  21. *
  22. * - Updated to the new 3DS layout. Reset your controller settings if the
  23. * touch input box is in the wrong location.
  24. *
  25. * - The logic has been separated into components for maintainability. There
  26. * should be no break in functionality.
  27. *
  28. * Enjoy!
  29. *
  30. * - Meiguro
  31. */
  32.  
  33. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  34. /* globals GM_addStyle, GM_info */
  35.  
  36. var util2 = require('util2');
  37.  
  38. var dd = require('dd');
  39. dd.ui = require('dd-ui');
  40.  
  41. var Entity = require('entity');
  42. var Touch = require('touch');
  43. var Chat = require('chat');
  44. var Settings = require('settings');
  45. var mywindow = require('window');
  46.  
  47. require('gm-shims');
  48.  
  49. var Control = function(def) {
  50. Entity.call(this, def);
  51. this.config = {};
  52. };
  53.  
  54. util2.inherit(Control, Entity, Control.prototype);
  55.  
  56. Control.DefaultConfig = {
  57. delay: 50,
  58. screen: {
  59. aspect: 1280 / 720,
  60. position: [0.997, 0.977],
  61. scale: 0.392,
  62. size: [320, 240],
  63. barHeight: 30
  64. },
  65. enabled: true,
  66. showBorder: true,
  67. showCoordTooltip: true,
  68. showCross: true,
  69. autoSend: true,
  70. showDroplets: true,
  71. streamDelay: 15,
  72. };
  73.  
  74. Control.PlayerSelector = '.dynamic-player, .player-container';
  75.  
  76. Control.prototype.updateLoad = function() {
  77. if (this.loaded) { return; }
  78. if (!this.loadable()) { return; }
  79.  
  80. this.onload();
  81. };
  82.  
  83. Control.prototype.update = function(force) {
  84. this.updateLoad();
  85.  
  86. Entity.prototype.update.call(this, force);
  87. };
  88.  
  89. Control.prototype.onClick = function(e) {
  90. this.component('touch').onClick(e);
  91. };
  92.  
  93. Control.prototype.onMove = function(e) {
  94. this.component('touch').onMove(e);
  95. };
  96.  
  97. Control.prototype.onPressReset = function(e) {
  98. $.extend(true, this.config, Control.DefaultConfig);
  99.  
  100. this.eachComponent(function(component) {
  101. for (var k in component) {
  102. var $elem = component[k];
  103. if ($elem.$change) {
  104. $elem.$change();
  105. }
  106. }
  107. });
  108.  
  109. this.update(true);
  110. this.saveConfig();
  111. };
  112.  
  113. Control.prototype.onPressChangeChatServer = function(e) {
  114. this.component('chat').onPressChangeChatServer(e);
  115. };
  116.  
  117. Control.prototype.saveConfig = function() {
  118. localStorage.TPControl = JSON.stringify(this.config);
  119. };
  120.  
  121. Control.prototype.loadConfig = function() {
  122. if (!localStorage.TPControl) {
  123. localStorage.TPControl = this.config;
  124. } else {
  125. try {
  126. var lastConfig = JSON.parse(localStorage.TPControl);
  127. for (var k in lastConfig) {
  128. this.config[k] = lastConfig[k];
  129. }
  130. } catch(e) {
  131. console.log(e);
  132. }
  133. }
  134. };
  135.  
  136. Control.prototype.init = function() {
  137. var self = this;
  138.  
  139. window.$ = mywindow.jQuery;
  140.  
  141. $.extend(true, this.config, Control.DefaultConfig);
  142.  
  143. this.loadConfig();
  144. this.saveConfig();
  145.  
  146. dd.onChange = this.saveConfig.bind(this);
  147.  
  148. if (this.config.screen.size[0] != Control.DefaultConfig.screen.size[0]) {
  149. $.extend(true, this.config.screen, Control.DefaultConfig.screen);
  150. }
  151.  
  152. $('.tpc-mouse-box').remove();
  153. $('.tpc-control-settings').remove();
  154.  
  155. var touch = new Touch();
  156. var chat = new Chat();
  157. var settings = new Settings();
  158.  
  159. this.addComponent(touch);
  160. this.addComponent(chat);
  161. this.addComponent(settings);
  162.  
  163. var $player = touch.$player = $(Control.PlayerSelector);
  164. var $mouseBox = touch.$mouseBox = $('<div/>').addClass('tpc-mouse-box');
  165. var $coordTooltip = touch.$coordTooltip = $('<div/>').addClass('tpc-coord-tooltip');
  166.  
  167. var $chatSettings = settings.$chatSettings = $(Settings.ChatSelector);
  168. var $controlSettings = settings.$controlSettings = $('<div/>').addClass('tpc-control-settings');
  169.  
  170. $player.css({ position: 'relative' });
  171. $chatSettings.css({ position: 'absolute' });
  172.  
  173. $mouseBox.css({
  174. cursor: 'pointer',
  175. border: '2px solid rgba(150, 150, 150, 0.2)',
  176. borderRadius: '5px'
  177. });
  178.  
  179. GM_addStyle(
  180. '.tpc-mouse-box .tpc-coord-tooltip { display: none; color: #444; font-weight: normal } ' +
  181. '.tpc-mouse-box:hover .tpc-coord-tooltip { display: block }');
  182.  
  183. $coordTooltip.css({
  184. padding: '0px 5px',
  185. background: 'rgba(255, 255, 255, 0.8)',
  186. borderRadius: '2px'
  187. });
  188.  
  189. $mouseBox.empty();
  190. $mouseBox.append($coordTooltip);
  191.  
  192. $controlSettings.empty();
  193. $controlSettings.append(
  194. '<div class="chat-menu tpc-touch-menu">' +
  195. '<div class="chat-menu-header">Touch Control Settings</div>' +
  196. '<div class="chat-menu-content tpc-control-sliders"></div>' +
  197. '<div class="chat-menu-content tpc-control-checkboxes"></div>' +
  198. '<div class="chat-menu-content tpc-control-last"></div>' +
  199. '</div>' +
  200. '<div class="chat-menu tpc-chat-menu">' +
  201. '<div class="chat-menu-header">Chat Server</div>' +
  202. '<div class="chat-menu-content tpc-control-chat"></div>' +
  203. '</div>');
  204.  
  205. $controlSettings.find('.tpc-control-sliders')
  206. .append(touch.xSlider = dd.ui.slider(
  207. 'tpc-x-slider tpc-slider', 'Touch-box x-position', { min: 0, max: 1, step: 0.0005 },
  208. this.config, 'screen.position.0', function() { touch.updateMouseBox(true); }))
  209. .append(touch.ySlider = dd.ui.slider(
  210. 'tpc-y-slider tpc-slider', 'Touch-box y-position', { min: 0, max: 1, step: 0.0005 },
  211. this.config, 'screen.position.1', function() { touch.updateMouseBox(true); }))
  212. .append(touch.scaleSlider = dd.ui.slider(
  213. 'tpc-scale-slider tpc-slider', 'Touch-box scale', { min: 0, max: 1, step: 0.0005 },
  214. this.config, 'screen.scale', function() { touch.updateMouseBox(true); }));
  215.  
  216. $controlSettings.find('.tpc-control-checkboxes')
  217. .append(touch.enabledCheckbox = dd.ui.checkbox(
  218. 'tpc-enabled-checkbox', 'Enable touch control', this.config, 'enabled',
  219. function() {
  220. touch.$mouseBox.css({ display: self.config.enabled ? 'block' : 'none' });
  221. }))
  222. .append(touch.borderCheckbox = dd.ui.checkbox(
  223. 'tpc-border-checkbox', 'Show border box', this.config, 'showBorder',
  224. function() {
  225. touch.$mouseBox.css({ border: self.config.showBorder ? '2px solid rgba(255, 255, 255, 0.5)' : 'none' });
  226. }))
  227. .append(touch.coordTooltipCheckbox = dd.ui.checkbox(
  228. 'tpc-tooltip-checkbox', 'Show coord tooltip', this.config, 'showCoordTooltip',
  229. function() {
  230. if (self.config.showCoordTooltip) {
  231. touch.$mouseBox.append(touch.$coordTooltip);
  232. } else {
  233. touch.$coordTooltip.remove();
  234. }
  235. }))
  236. .append(touch.crossCheckbox = dd.ui.checkbox(
  237. 'tpc-cross-checkbox', 'Use cross pointer', this.config, 'showCross',
  238. function() {
  239. touch.$mouseBox.css({ cursor: self.config.showCross ? 'crosshair' : 'default' });
  240. }))
  241. .append(touch.autoSendCheckbox = dd.ui.checkbox(
  242. 'tpc-auto-send-checkbox', 'Auto-send touches', this.config, 'autoSend'))
  243. .append(touch.dropletsCheckbox = dd.ui.checkbox(
  244. 'tpc-droplet-checkbox', 'Show touch droplets', this.config, 'showDroplets'));
  245.  
  246. $controlSettings.find('.tpc-control-last')
  247. .append(touch.resetButton = dd.ui.button(
  248. 'tpc-reset-button', 'Reset controller settings', this.onPressReset.bind(this)));
  249.  
  250. $controlSettings.find('.tpc-control-chat')
  251. .append('<p><label>Current <span class="tpc-chat-server"></span></label></p>')
  252. .append(chat.chatServerButton = dd.ui.button(
  253. 'tpc-chat-server-button', 'Change chat server', this.onPressChangeChatServer.bind(this)));
  254.  
  255. $player.append($mouseBox);
  256. $chatSettings.append($controlSettings);
  257.  
  258. $mouseBox.on('click', this.onClick.bind(this));
  259. $mouseBox.on('mousemove', this.onMove.bind(this));
  260.  
  261. settings.update(true);
  262.  
  263. this.loaded = true;
  264.  
  265. this.start();
  266.  
  267. setTimeout(function() {
  268. $('.chat-room .loading-mask').remove();
  269. }, 5000);
  270. };
  271.  
  272. Control.prototype.loadable = function() {
  273. var $ = mywindow.jQuery;
  274. if (typeof $ !== 'function') { return false; }
  275.  
  276. var hasPlayer = $(Control.PlayerSelector).length;
  277. var hasChatSettings = $(Settings.ChatSelector).length;
  278. return hasPlayer || hasChatSettings;
  279. };
  280.  
  281. Control.prototype.onload = function() {
  282. this.init();
  283. if (typeof GM_info === 'object') {
  284. console.log(GM_info.script.name + ' v' + GM_info.script.version + ' loaded!');
  285. } else {
  286. console.log('Twitch Plays Control loaded!');
  287. }
  288. };
  289.  
  290. var control = new Control();
  291.  
  292. setInterval(control.update.bind(control), Control.DefaultConfig.delay);
  293.  
  294. control.update();
  295.  
  296. mywindow.TPControl = control;
  297.  
  298. },{"chat":2,"dd":5,"dd-ui":4,"entity":6,"gm-shims":7,"settings":8,"touch":9,"util2":10,"window":11}],2:[function(require,module,exports){
  299. var util2 = require('util2');
  300. var Component = require('component');
  301.  
  302. var mywindow = require('window');
  303.  
  304. var Chat = function(def) {
  305. Component.call(this, def);
  306. };
  307.  
  308. util2.inherit(Chat, Component, Chat.prototype);
  309.  
  310. Chat.InputSelector = '.ember-text-area';
  311. Chat.ButtonSelector = '.send-chat-button button';
  312. Chat.HiddenSelector = '.chat-hidden-overlay';
  313. Chat.LogSelector = '.chat-messages .tse-content';
  314. Chat.ServerAddress = '199.9.252.26:6667';
  315.  
  316. Chat.prototype.name = 'chat';
  317.  
  318. Chat.prototype.start = function() {
  319. };
  320.  
  321. Chat.prototype.getChatSession = function() {
  322. if (this.chatSession) {
  323. return this.chatSession;
  324. }
  325.  
  326. var App = mywindow.App;
  327. if (App && App.Room) {
  328. return (this.chatSession = App.Room._getTmiSession().fulfillmentValue);
  329. }
  330. };
  331.  
  332. Chat.prototype.getChatConnection = function() {
  333. var chatSession = this.getChatSession();
  334. var connections = chatSession._connections;
  335. var cluster = connections.prod || connections.event;
  336. if (cluster) { return cluster; }
  337. for (var k in connections) {
  338. return connections[k];
  339. }
  340. };
  341.  
  342. Chat.prototype.getCurrentChatAddress = function() {
  343. var chatSession = this.getChatSession();
  344. if (!chatSession) { return; }
  345. var connection = this.getChatConnection();
  346. if (!connection) { return; }
  347. var addr = connection._addrs[connection._currentAddressIndex];
  348. if (!addr) { return; }
  349. return addr.host + ':' + addr.port;
  350. };
  351.  
  352. Chat.prototype.connectServer = function(address) {
  353. var addr = address.split(':');
  354. var chatSession = this.getChatSession();
  355. var connection = this.getChatConnection();
  356. if (!connection) {
  357. window.alert('TPC: Couldn\'t obtain a chat connection to manipulate!');
  358. return;
  359. }
  360.  
  361. connection.close();
  362. connection._addrs = [{ host: addr[0], port: addr[1] }];
  363. connection._currentAddressIndex = 0;
  364. connection._numSocketConnectAttempts = 0;
  365. connection.open();
  366.  
  367. var msg = 'connecting to chat server ' + address + '...';
  368. $(Chat.LogSelector + ' .ember-view:last')
  369. .before('<div class="chat-line admin"><span class="message">' + msg + '</span></div>');
  370. };
  371.  
  372. Chat.prototype.isChatVisible = function() {
  373. return $(Chat.InputSelector).length && !$(Chat.HiddenSelector).is(':visible');
  374. };
  375.  
  376. Chat.prototype.setInput = function(input, broadcast) {
  377. $(Chat.InputSelector).val(input).focus().change().blur();
  378. if (this.config.autoSend) {
  379. $(Chat.ButtonSelector).click();
  380. }
  381. if (!this.isChatVisible() && broadcast !== false) {
  382. localStorage.TPCInput = input;
  383. }
  384. };
  385.  
  386. Chat.prototype.updateInput = function() {
  387. if (!this.entity.loaded) { return; }
  388. if (!this.config.enabled) { return; }
  389.  
  390. if (!this.isChatVisible()) { return; }
  391.  
  392. var input = localStorage.TPCInput;
  393. delete localStorage.TPCInput;
  394. if (typeof input === 'string' && input.length > 0) {
  395. this.setInput(input, false);
  396. }
  397. };
  398.  
  399. Chat.prototype.updateChatSession = function() {
  400. if (this.chatSession) { return; }
  401.  
  402. var TMI = mywindow.TMI;
  403. if (!TMI) { return; }
  404.  
  405. if (this.entity.loaded) {
  406. this.getChatSession();
  407. }
  408. };
  409.  
  410. Chat.prototype.update = function() {
  411. this.updateChatSession();
  412. this.updateInput();
  413. };
  414.  
  415. Chat.prototype.onPressChangeChatServer = function(e) {
  416. var defaultAddress = Chat.ServerAddress;
  417. var currentAddress = this.getCurrentChatAddress();
  418. var address = window.prompt(
  419. 'Enter chat server address:' +
  420. (currentAddress ?
  421. '\nCurrent ' + currentAddress :
  422. '\nExample ' + defaultAddress),
  423. defaultAddress) || '';
  424. address = address.replace(/\s/, '');
  425. if (address.length === 0) {
  426. return;
  427. }
  428. if (!address.match(':')) {
  429. address += ':6667';
  430. }
  431. this.connectServer(address);
  432. };
  433.  
  434. module.exports = Chat;
  435.  
  436. },{"component":3,"util2":10,"window":11}],3:[function(require,module,exports){
  437. var Component = function(def) {
  438. this.state = def || {};
  439. };
  440.  
  441. Component.prototype.component = function(name) {
  442. return this.entity.component(name);
  443. };
  444.  
  445. Component.prototype.start = function() {};
  446.  
  447. Component.prototype.update = function() {};
  448.  
  449. module.exports = Component;
  450.  
  451. },{}],4:[function(require,module,exports){
  452. var dd = require('dd');
  453.  
  454. dd.ui = dd.ui || {};
  455.  
  456. dd.ui.checkbox = function(id, label, obj, ref, onChange) {
  457. var $elem = $('<p><label for="'+id+'"><input id="'+id+'" type="checkbox"> '+label+'</label></p>');
  458. var $input = $elem.find('input');
  459. dd.bindGetSet($elem, $input.prop.bind($input, 'checked'));
  460. $input.on('change', dd.bindElement($elem, obj, ref, onChange));
  461. return $elem;
  462. };
  463.  
  464. dd.ui.slider = function(klass, label, options, obj, ref, onChange) {
  465. var $elem = $('<p><label>'+label+'</label></p>');
  466. var $slider = $('<div class="'+klass+'"></div>');
  467. $slider.slider(options);
  468. $elem.prepend($slider);
  469. dd.bindGetSet($elem, $slider.slider.bind($slider, 'value'));
  470. $slider.slider({ slide: dd.bindElement($elem, obj, ref, onChange) });
  471. return $elem;
  472. };
  473.  
  474. dd.ui.button = function(klass, label, onPress) {
  475. var $elem = $('<button class="'+klass+'">'+label+'</button>');
  476. $elem.on('click', onPress);
  477. return $elem;
  478. };
  479.  
  480. module.exports = dd.ui;
  481.  
  482. },{"dd":5}],5:[function(require,module,exports){
  483. var dd = {};
  484.  
  485. dd.last = function(a) {
  486. return a[a.length - 1];
  487. };
  488.  
  489. dd.toKey = function(obj, key) {
  490. return obj instanceof Array ? parseInt(key) : key;
  491. };
  492.  
  493. dd.getByKeys = function(obj, keys, offset) {
  494. for (var i = 0, ii = keys.length - (offset || 0); i < ii; ++i) {
  495. obj = obj[dd.toKey(obj, keys[i])];
  496. }
  497. return obj;
  498. };
  499.  
  500. dd.setByKey = function(obj, key, value) {
  501. obj[dd.toKey(obj, key)] = value;
  502. };
  503.  
  504. dd.set = function(obj, ref, value) {
  505. var keys = ref.split('.');
  506. dd.setByKey(dd.getByKeys(obj, keys, 1), dd.last(keys), value);
  507. };
  508.  
  509. dd.get = function(obj, ref) {
  510. return dd.getByKeys(obj, ref.split('.'));
  511. };
  512.  
  513. dd.bindGetSet = function($elem, get, set) {
  514. $elem.$get = get;
  515. $elem.$set = set || get;
  516. };
  517.  
  518. dd.bindElement = function($elem, obj, ref, onChange) {
  519. $elem.$obj = obj;
  520. $elem.$ref = ref;
  521. $elem.$change = function(e) {
  522. if (e) {
  523. dd.set(obj, ref, $elem.$get());
  524. } else {
  525. $elem.$set(dd.get(obj, ref));
  526. }
  527. if (onChange) { onChange(e); }
  528. if (dd.onChange) { dd.onChange(e); }
  529. };
  530. $elem.$change();
  531. return $elem.$change;
  532. };
  533.  
  534. module.exports = dd;
  535.  
  536. },{}],6:[function(require,module,exports){
  537. var Entity = function(def) {
  538. this.state = def || {};
  539. this._components = [];
  540. this._componentsByName = {};
  541. };
  542.  
  543. Entity.prototype.component = function(name) {
  544. return this._componentsByName[name];
  545. };
  546.  
  547. Entity.prototype.eachComponent = function(callback) {
  548. this._components.forEach(callback.bind(this));
  549. };
  550.  
  551. Entity.prototype.addComponent = function(component) {
  552. if (component.entity) {
  553. component.entity.removeComponent(component);
  554. }
  555. this._componentsByName[component.name] = component;
  556. this._components.push(component);
  557. component.entity = this;
  558. component.config = this.config;
  559. };
  560.  
  561. Entity.prototype.removeComponent = function(component) {
  562. var index = this._components.indexOf(component);
  563. if (index === -1) { return; }
  564. this._components.splice(index, 1);
  565. delete this._componentsByName[component.name];
  566. delete component.entity;
  567. delete component.config;
  568. };
  569.  
  570. Entity.prototype.start = function() {
  571. for (var i = 0, ii = this._components.length; i < ii; ++i) {
  572. this._components[i].start();
  573. }
  574. };
  575.  
  576. Entity.prototype.update = function(force) {
  577. for (var i = 0, ii = this._components.length; i < ii; ++i) {
  578. this._components[i].update(force);
  579. }
  580. };
  581.  
  582. module.exports = Entity;
  583.  
  584. },{}],7:[function(require,module,exports){
  585. (function (global){
  586.  
  587. if (typeof GM_addStyle === 'undefined') {
  588. global.GM_addStyle = function(style) {
  589. $('head:first').append($('<style>').text(style));
  590. };
  591. }
  592.  
  593. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  594. },{}],8:[function(require,module,exports){
  595. var util2 = require('util2');
  596. var Component = require('component');
  597.  
  598. var Settings = function(def) {
  599. Component.call(this, def);
  600. };
  601.  
  602. util2.inherit(Settings, Component, Settings.prototype);
  603.  
  604. Settings.ChatSelector = '.js-chat-settings';
  605. Settings.ButtonSelector = '.settings.button';
  606.  
  607. Settings.prototype.name = 'settings';
  608.  
  609. Settings.prototype.start = function() {
  610. };
  611.  
  612. Settings.prototype.updateServerLabel = function() {
  613. var $chatServer = this.$chatSettings.find('.tpc-chat-server');
  614.  
  615. var labelAddress = $chatServer.text();
  616. var currentAddress = this.component('chat').getCurrentChatAddress();
  617. if (currentAddress !== labelAddress) {
  618. $chatServer.text(currentAddress);
  619. }
  620. };
  621.  
  622. Settings.prototype.updatePosition = function(force) {
  623. var overflow = this.$chatSettings.css('overflowY') || '';
  624.  
  625. if (overflow.match(/(auto|scroll)/)) {
  626. this.$controlSettings.css({ position: 'static', left: 'none', top: 'none' });
  627. return;
  628. }
  629.  
  630. var minWidth = Math.max(200, this.$chatSettings.width());
  631. var position = { left: 0, top: 0 };
  632. var offset = this.$chatSettings.offset();
  633. var isSlim = offset.left > 0 && offset.left < minWidth;
  634.  
  635. this.$controlSettings.css({
  636. position: 'absolute',
  637. minWidth: minWidth,
  638. left: position.left + (minWidth + 5) * (isSlim ? 1 : -1),
  639. top: position.top + this.$chatSettings.height() - this.$controlSettings.height()
  640. });
  641. };
  642.  
  643. Settings.prototype.update = function(force) {
  644. if (!this.entity.loaded) { return; }
  645.  
  646. if (!this.$chatSettings.length) { return; }
  647. if (!this.$chatSettings.is(':visible') && force !== true) { return; }
  648.  
  649. this.updateServerLabel();
  650. this.updatePosition();
  651. };
  652.  
  653. module.exports = Settings;
  654.  
  655. },{"component":3,"util2":10}],9:[function(require,module,exports){
  656. var util2 = require('util2');
  657. var Component = require('component');
  658.  
  659. var Touch = function(def) {
  660. Component.call(this, def);
  661. };
  662.  
  663. util2.inherit(Touch, Component, Touch.prototype);
  664.  
  665. Touch.SizeEdge = [false, false];
  666.  
  667. Touch.prototype.name = 'touch';
  668.  
  669. Touch.prototype.start = function() {
  670. };
  671.  
  672. Touch.prototype.getBorderSize = function() {
  673. return parseInt(this.$mouseBox.css('borderTopWidth'));
  674. };
  675.  
  676. Touch.prototype.getTouchPosition = function(e) {
  677. var offset = this.$mouseBox.offset();
  678. var borderSize = this.getBorderSize();
  679. var x = e.clientX - offset.left - 2 * borderSize;
  680. var y = e.clientY - offset.top - 2 * borderSize;
  681.  
  682. var minX = 0 + (Touch.SizeEdge[0] ? 0 : 1);
  683. var minY = 0 + (Touch.SizeEdge[1] ? 0 : 1);
  684. var maxX = this.config.screen.size[0] - (Touch.SizeEdge[0] ? 0 : 1);
  685. var maxY = this.config.screen.size[1] - (Touch.SizeEdge[1] ? 0 : 1);
  686.  
  687. var touchX = Math.ceil(x / this.scale);
  688. var touchY = Math.ceil(y / this.scale);
  689. var valid = (touchX >= minX && touchX <= maxX) &&
  690. (touchY >= minY && touchY <= maxY);
  691. return {
  692. mouse: [x, y],
  693. position: [touchX, touchY],
  694. input: touchX + ',' + touchY,
  695. valid: valid
  696. };
  697. };
  698.  
  699. Touch.prototype.spawnDroplet = function(position) {
  700. var $drop = $('<div/>').addClass('tpc-droplet');
  701. var size = 10;
  702. $drop.css({
  703. background: 'rgba(255, 255, 255, 0.8)',
  704. borderRadius: size,
  705. position: 'absolute',
  706. left: position[0] - size / 2,
  707. top: position[1] - size / 2,
  708. width: size,
  709. height: size
  710. });
  711. this.$mouseBox.append($drop);
  712. $drop
  713. .animate({
  714. left: position[0],
  715. top: position[1],
  716. width: 0,
  717. height: 0
  718. }, this.config.streamDelay * 1000)
  719. .queue(function() {
  720. $drop.remove();
  721. $drop.dequeue();
  722. });
  723. };
  724.  
  725. Touch.prototype.updateMouseBox = function(force) {
  726. var playerWidth = this.$player.width();
  727. var playerHeight = this.$player.height() - this.config.screen.barHeight;
  728.  
  729. if ((this.playerWidth === playerWidth &&
  730. this.playerHeight === playerHeight) && force !== true) {
  731. return;
  732. }
  733.  
  734. this.playerWidth = playerWidth;
  735. this.playerHeight = playerHeight;
  736.  
  737. var aspect = playerWidth / playerHeight;
  738.  
  739. var excessWidth = 0;
  740. if (aspect > this.config.screen.aspect) {
  741. excessWidth = playerWidth - playerHeight * this.config.screen.aspect;
  742. }
  743.  
  744. this.scale = this.config.screen.scale * playerHeight / this.config.screen.size[1];
  745.  
  746. var borderSize = this.getBorderSize();
  747. var width = this.scale * this.config.screen.size[0];
  748. var height = this.scale * this.config.screen.size[1];
  749.  
  750. this.$mouseBox.css({
  751. position: 'absolute',
  752. left: excessWidth / 2 + (playerWidth - width - excessWidth) * this.config.screen.position[0] - borderSize,
  753. top: (playerHeight - height) * this.config.screen.position[1] - borderSize,
  754. width: width,
  755. height: height
  756. });
  757.  
  758. if (!this.component('settings').$chatSettings.is(':visible')) {
  759. this.$mouseBox.stop(true, true).css({ background: 'transparent' });
  760. return;
  761. }
  762.  
  763. this.$mouseBox
  764. .stop(true, true)
  765. .animate({ backgroundColor: 'rgba(255, 255, 255, 0.5)' }, 100)
  766. .delay(1000)
  767. .animate({ backgroundColor: 'rgba(255, 255, 255, 0)' })
  768. .animate({ background: 'transparent' }, 0);
  769. };
  770.  
  771. Touch.prototype.update = function(force) {
  772. if (!this.entity.loaded) { return; }
  773. if (!this.config.enabled) { return; }
  774.  
  775. this.updateMouseBox(force);
  776. };
  777.  
  778. Touch.prototype.onClick = function(e) {
  779. var touch = this.getTouchPosition(e);
  780. if (!touch.valid) { return; }
  781. this.component('chat').setInput(touch.input);
  782. if (this.config.showDroplets) {
  783. this.spawnDroplet(touch.mouse);
  784. }
  785. };
  786.  
  787. Touch.prototype.onMove = function(e) {
  788. if (!this.config.showCoordTooltip) { return; }
  789. var touch = this.getTouchPosition(e);
  790. var borderSize = this.getBorderSize();
  791. this.$coordTooltip
  792. .text(touch.valid ? touch.input : '')
  793. .css({
  794. position: 'absolute',
  795. left: touch.mouse[0] + borderSize + 15,
  796. top: touch.mouse[1] + borderSize - this.$coordTooltip.outerHeight() / 2
  797. });
  798. };
  799.  
  800. module.exports = Touch;
  801.  
  802. },{"component":3,"util2":10}],10:[function(require,module,exports){
  803. /*
  804. * util2.js by Meiguro - MIT License
  805. */
  806.  
  807. var util2 = (function(){
  808.  
  809. var util2 = {};
  810.  
  811. util2.noop = function() {};
  812.  
  813. util2.copy = function(a, b) {
  814. b = b || (a instanceof Array ? [] : {});
  815. for (var k in a) { b[k] = a[k]; }
  816. return b;
  817. };
  818.  
  819. util2.inherit = function(child, parent, proto) {
  820. child.prototype = Object.create(parent.prototype);
  821. child.prototype.constructor = child;
  822. if (proto) {
  823. util2.copy(proto, child.prototype);
  824. }
  825. return child.prototype;
  826. };
  827.  
  828. if (typeof module !== 'undefined') {
  829. module.exports = util2;
  830. }
  831.  
  832. return util2;
  833.  
  834. })();
  835.  
  836. },{}],11:[function(require,module,exports){
  837. /* global unsafeWindow */
  838. module.exports = typeof unsafeWindow !== 'undefined' ? unsafeWindow : window;
  839.  
  840. },{}]},{},[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement