File size: 3,216 Bytes
248d96b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const { LitElement, html, css } = require('lit')

const audioContext = new window.AudioContext()
const sounds = {}

async function playSound (path) {
  let volume = 1
  const options = document.getElementById('options-screen')
  if (options) {
    volume = options.sound / 100
  }

  let soundBuffer = sounds[path]

  if (!soundBuffer) {
    const res = await window.fetch(path)
    const data = await res.arrayBuffer()

    soundBuffer = await audioContext.decodeAudioData(data)
    sounds[path] = soundBuffer
  }

  const gainNode = audioContext.createGain()
  const source = audioContext.createBufferSource()
  source.buffer = soundBuffer
  source.connect(gainNode)
  gainNode.connect(audioContext.destination)
  gainNode.gain.value = volume
  source.start(0)
}

class Button extends LitElement {
  static get styles () {
    return css`

      .button {

        --txrV: 66px;

        position: relative;

        width: 200px;

        height: 20px;

        font-family: minecraft, mojangles, monospace;

        font-size: 10px;

        color: white;

        text-shadow: 1px 1px #222;

        border: none;

        z-index: 1;

        outline: none;

      }



      .button:hover,

      .button:focus-visible {

        --txrV: 86px;

      }



      .button:disabled {

        --txrV: 46px;

        color: #A0A0A0;

        text-shadow: 1px 1px #111;

      }



      .button::after {

        content: '';

        display: block;

        position: absolute;

        top: 0;

        left: 0;

        width: 50%;

        height: 20px;

        background: url('textures/1.17.1/gui/widgets.png');

        background-size: 256px;

        background-position-y: calc(var(--txrV) * -1);

        z-index: -1;

      }



      .button::before {

        content: '';

        display: block;

        position: absolute;

        top: 0;

        left: 50%;

        width: 50%;

        height: 20px;

        background: url('textures/1.17.1/gui/widgets.png');

        background-size: 256px;

        background-position-x: calc(-200px + 100%);

        background-position-y: calc(var(--txrV) * -1);

        z-index: -1;

      }

    `
  }

  static get properties () {
    return {
      label: {
        type: String,
        attribute: 'pmui-label'
      },
      width: {
        type: String,
        attribute: 'pmui-width'
      },
      disabled: {
        type: Boolean,
        attribute: 'pmui-disabled'
      },
      onPress: {
        type: Function,
        attribute: 'pmui-click'
      }
    }
  }

  constructor () {
    super()
    this.label = ''
    this.disabled = false
    this.width = '200px'
    this.onPress = () => {}
  }

  render () {
    return html`

    <button

      class="button"

      ?disabled=${this.disabled}

      @click=${this.onBtnClick}

      style="width: ${this.width};"

    >

      ${this.label}

    </button>`
  }

  onBtnClick () {
    playSound('click_stereo.ogg')
    this.dispatchEvent(new window.CustomEvent('pmui-click'))
  }
}

window.customElements.define('pmui-button', Button)
const _playSound = playSound
export { _playSound as playSound }