PCOL-ASSIST

为台球游戏添加虚拟按键,支持显示/隐藏切换

当前为 2025-07-20 提交的版本,查看 最新版本

// ==UserScript==
// @name         PCOL-ASSIST
// @author       葉月Hikaru
// @match        http://www.heyzxz.me/pcol/*
// @version 6.0
// @namespace https://gf.qytechs.cn/users/your-id
// @description 为台球游戏添加虚拟按键,支持显示/隐藏切换
// ==/UserScript==
(function() {
    'use strict';
    // 按键配置(保持原有)
    const keysConfig = [
        { key: '+', text: '-', top: 30, left: 40 },
        { key: 'w', text: 'W', top: 30, left: 50 },
        { key: '-', text: '+', top: 30, left: 60 },
        { key: 'space', text: 'SPACE', top: 30, left: 70 },
        { key: 'x', text: 'X', top: 30, left: 80 },
        { key: 'c', text: 'C', top: 30, left: 90 },
        { key: 'p', text: 'P', top: 120, left: 90 },
        { key: 'a', text: 'A', top: 120, left: 30 },
        { key: 's', text: 'S', top: 120, left: 50 },
        { key: 'd', text: 'D', top: 120, left: 70 }
    ];

    // 存储所有原有按钮的DOM引用
    const originalButtons = [];
    // 显示状态标记(默认显示)
    let areButtonsVisible = true;

    // 创建原有按键(保持逻辑,新增存储引用)
    function createVirtualKey(keyConfig) {
        const btn = document.createElement('button');
        btn.id = `pcol-assist-${keyConfig.key}-btn`;
        btn.textContent = keyConfig.text;
        btn.style.cssText = `
            position: fixed;
            top: ${keyConfig.top}px;
            left: ${keyConfig.left}%;
            transform: ${keyConfig.left === 50 ? 'translateX(-50%)' : 'translateX(0)'};
            width: 60px;
            height: 60px;
            font-size: ${keyConfig.text === 'SPACE' ? '16px' : '28px'};
            background-color: rgba(255, 165, 0, 0.9);
            color: white;
            border: 2px solid white;
            border-radius: 50%;
            cursor: pointer;
            z-index: 9999;
        `;

        // 触发事件逻辑(保持不变)
        function triggerEvent(type, keyInfo = {}) {
            if (keyConfig.key === '+' || keyConfig.key === '-') {
                const wheelEvent = new WheelEvent('wheel', {
                    deltaY: keyConfig.key === '+' ? -300 : 300,
                    bubbles: true,
                    cancelable: true
                });
                document.querySelector('canvas')?.dispatchEvent(wheelEvent) || document.dispatchEvent(wheelEvent);
            } else {
                const eventKey = keyInfo.key || (keyConfig.key === 'space' ? ' ' : keyConfig.key);
                const eventCode = keyInfo.code || (keyConfig.key === 'space' ? 'Space' : `Key${keyConfig.key.toUpperCase()}`);
                const eventKeyCode = keyInfo.keyCode || (keyConfig.key === 'w' ? 87 : keyConfig.key === 'a' ? 65 : keyConfig.key === 's' ? 83 : 
                        keyConfig.key === 'd' ? 68 : keyConfig.key === 'x' ? 88 : keyConfig.key === 'c' ? 67 : keyConfig.key === 'p' ? 80 : 32);
                const event = new KeyboardEvent(type, {
                    key: eventKey === ' ' ? 'Spacebar' : eventKey,
                    code: eventCode,
                    keyCode: eventKeyCode,
                    bubbles: true,
                    cancelable: true
                });
                document.querySelector('canvas')?.dispatchEvent(event) || document.dispatchEvent(event);
            }
        }

        ['touchstart', 'mousedown'].forEach(e => btn.addEventListener(e, e => {
            e.preventDefault();
            triggerEvent('keydown');
        }));
        ['touchend', 'mouseup'].forEach(e => btn.addEventListener(e, e => {
            e.preventDefault();
            triggerEvent('keyup');
        }));

        document.body.appendChild(btn);
        originalButtons.push(btn); // 存储按钮引用
    }

    // 创建显示/隐藏开关按钮
    function createVisibilityToggle() {
        const toggle = document.createElement('button');
        toggle.id = 'pcol-assist-visibility-toggle';
        toggle.textContent = '按钮: 显示'; // 初始状态文本
        toggle.style.cssText = `
            position: fixed;
            top: 20px;
            right: 20px; /* 靠近右上角 */
            width: 100px;
            height: 40px;
            font-size: 16px;
            background-color: rgba(0, 128, 0, 0.9); /* 初始绿色表示开启 */
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            z-index: 9999; /* 确保在最上层 */
        `;

        // 切换显示/隐藏状态
        toggle.addEventListener('click', () => {
            areButtonsVisible = !areButtonsVisible;
            // 更新所有原有按钮的显示状态
            originalButtons.forEach(btn => {
                btn.style.display = areButtonsVisible ? 'block' : 'none';
            });
            // 更新开关自身状态(文本+颜色)
            if (areButtonsVisible) {
                toggle.textContent = '按钮: 显示';
                toggle.style.backgroundColor = 'rgba(0, 128, 0, 0.9)';
            } else {
                toggle.textContent = '按钮: 隐藏';
                toggle.style.backgroundColor = 'rgba(128, 128, 128, 0.9)';
            }
        });

        document.body.appendChild(toggle);
    }

    // 延迟初始化
    setTimeout(() => {
        keysConfig.forEach(createVirtualKey);
        createVisibilityToggle(); // 添加新开关
        console.log('【PCOL-ASSIST】虚拟按键及显示切换功能已激活');
    }, 500);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址