2018-10-15 02:08:05 +08:00
|
|
|
class Debug{
|
2022-02-11 22:28:22 +08:00
|
|
|
constructor(...args){
|
|
|
|
this.init(...args)
|
|
|
|
}
|
|
|
|
init(){
|
2018-10-15 06:00:40 +08:00
|
|
|
if(!assets.pages["debug"]){
|
|
|
|
return
|
|
|
|
}
|
2018-10-15 02:08:05 +08:00
|
|
|
this.debugDiv = document.createElement("div")
|
|
|
|
this.debugDiv.id = "debug"
|
|
|
|
this.debugDiv.innerHTML = assets.pages["debug"]
|
|
|
|
document.body.appendChild(this.debugDiv)
|
|
|
|
|
2019-02-18 00:26:46 +08:00
|
|
|
this.titleDiv = this.byClass("title")
|
|
|
|
this.minimiseDiv = this.byClass("minimise")
|
|
|
|
this.offsetDiv = this.byClass("offset")
|
|
|
|
this.measureNumDiv = this.byClass("measure-num")
|
|
|
|
this.branchHideDiv = this.byClass("branch-hide")
|
|
|
|
this.branchSelectDiv = this.byClass("branch-select")
|
|
|
|
this.branchSelect = this.branchSelectDiv.getElementsByTagName("select")[0]
|
|
|
|
this.branchResetBtn = this.branchSelectDiv.getElementsByClassName("reset")[0]
|
2019-03-16 05:34:48 +08:00
|
|
|
this.volumeDiv = this.byClass("music-volume")
|
2020-03-30 14:50:34 +08:00
|
|
|
this.lyricsHideDiv = this.byClass("lyrics-hide")
|
|
|
|
this.lyricsOffsetDiv = this.byClass("lyrics-offset")
|
2020-03-09 20:36:57 +08:00
|
|
|
this.restartLabel = this.byClass("change-restart-label")
|
2019-02-18 00:26:46 +08:00
|
|
|
this.restartCheckbox = this.byClass("change-restart")
|
|
|
|
this.autoplayLabel = this.byClass("autoplay-label")
|
|
|
|
this.autoplayCheckbox = this.byClass("autoplay")
|
|
|
|
this.restartBtn = this.byClass("restart-btn")
|
|
|
|
this.exitBtn = this.byClass("exit-btn")
|
2018-10-15 02:08:05 +08:00
|
|
|
|
|
|
|
this.moving = false
|
2020-03-09 20:36:57 +08:00
|
|
|
this.windowSymbol = Symbol()
|
|
|
|
pageEvents.add(window, ["mousedown", "mouseup", "touchstart", "touchend", "blur", "resize"], this.stopMove.bind(this), this.windowSymbol)
|
2019-02-03 20:04:25 +08:00
|
|
|
pageEvents.mouseAdd(this, this.onMove.bind(this))
|
2020-03-09 20:36:57 +08:00
|
|
|
pageEvents.add(window, "touchmove", this.onMove.bind(this))
|
|
|
|
pageEvents.add(this.titleDiv, ["mousedown", "touchstart"], this.startMove.bind(this))
|
|
|
|
pageEvents.add(this.minimiseDiv, ["click", "touchstart"], this.minimise.bind(this))
|
|
|
|
pageEvents.add(this.restartBtn, ["click", "touchstart"], this.restartSong.bind(this))
|
|
|
|
pageEvents.add(this.exitBtn, ["click", "touchstart"], this.clean.bind(this))
|
|
|
|
pageEvents.add(this.restartLabel, "touchstart", this.touchBox.bind(this))
|
|
|
|
pageEvents.add(this.autoplayLabel, "touchstart", this.touchBox.bind(this))
|
2018-10-15 07:18:01 +08:00
|
|
|
pageEvents.add(this.autoplayCheckbox, "change", this.toggleAutoplay.bind(this))
|
2019-02-18 00:26:46 +08:00
|
|
|
pageEvents.add(this.branchSelect, "change", this.branchChange.bind(this))
|
2020-03-09 20:36:57 +08:00
|
|
|
pageEvents.add(this.branchResetBtn, ["click", "touchstart"], this.branchReset.bind(this))
|
2018-10-15 02:08:05 +08:00
|
|
|
|
2018-10-15 04:14:58 +08:00
|
|
|
this.offsetSlider = new InputSlider(this.offsetDiv, -60, 60, 3)
|
2018-10-15 02:08:05 +08:00
|
|
|
this.offsetSlider.onchange(this.offsetChange.bind(this))
|
|
|
|
|
2018-10-15 04:14:58 +08:00
|
|
|
this.measureNumSlider = new InputSlider(this.measureNumDiv, 0, 1000, 0)
|
|
|
|
this.measureNumSlider.onchange(this.measureNumChange.bind(this))
|
|
|
|
this.measureNumSlider.set(0)
|
|
|
|
|
2019-03-16 05:34:48 +08:00
|
|
|
this.volumeSlider = new InputSlider(this.volumeDiv, 0, 3, 2)
|
|
|
|
this.volumeSlider.onchange(this.volumeChange.bind(this))
|
|
|
|
this.volumeSlider.set(1)
|
|
|
|
|
2020-03-30 14:50:34 +08:00
|
|
|
this.lyricsSlider = new InputSlider(this.lyricsOffsetDiv, -60, 60, 3)
|
|
|
|
this.lyricsSlider.onchange(this.lyricsChange.bind(this))
|
|
|
|
|
2018-10-15 02:08:05 +08:00
|
|
|
this.moveTo(100, 100)
|
|
|
|
this.restore()
|
|
|
|
this.updateStatus()
|
Custom scripting, #song=, translations
- A song can be linked directly by adding "#song=<id>" to the url, replace `<id>` with the id in the database, after loading it jumps immediately jumps to the difficulty selection
- Added tutorial translations
- Fixed song preview not playing
- Use text fallback for the logo when there are no vectors
- Increased combo cache by 1 pixel
- A custom javascript file can be loaded from config.json by defining "custom_js" value
- Added lots of events to help writing custom js files: `version-link, title-screen, language-change, song-select, song-select-move, song-select-difficulty, song-select-back, about, about-link, tutorial, import-songs, import-songs-default, session, session-start, session-end, debug, load-song, load-song-player2, load-song-unfocused, load-song-cancel, load-song-error, game-start, key-events, p2-game-end, p2-disconnected, p2-abandoned, pause, unpause, pause-restart, pause-song-select, game-lag, scoresheet, scoresheet-player2`
- Event syntax example:
```js
addEventListener("game-start", event => {
console.log("game-start", event.detail)
})
```
2019-02-14 17:32:45 +08:00
|
|
|
pageEvents.send("debug")
|
2018-10-15 02:08:05 +08:00
|
|
|
}
|
2019-02-18 00:26:46 +08:00
|
|
|
byClass(name){
|
|
|
|
return this.debugDiv.getElementsByClassName(name)[0]
|
|
|
|
}
|
2018-10-15 02:08:05 +08:00
|
|
|
startMove(event){
|
2020-03-09 20:36:57 +08:00
|
|
|
if(event.which === 1 || event.type === "touchstart"){
|
2018-10-15 02:08:05 +08:00
|
|
|
event.stopPropagation()
|
2020-03-09 20:36:57 +08:00
|
|
|
var divPos = this.debugDiv.getBoundingClientRect()
|
|
|
|
var click = event.type === "touchstart" ? event.changedTouches[0] : event
|
|
|
|
var x = click.pageX - divPos.left
|
|
|
|
var y = click.pageY - divPos.top
|
|
|
|
this.moving = {x: x, y: y}
|
2018-10-15 02:08:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
onMove(event){
|
|
|
|
if(this.moving){
|
2020-03-09 20:36:57 +08:00
|
|
|
var click = event.type === "touchmove" ? event.changedTouches[0] : event
|
|
|
|
var x = click.clientX - this.moving.x
|
|
|
|
var y = click.clientY - this.moving.y
|
2018-10-15 02:08:05 +08:00
|
|
|
this.moveTo(x, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stopMove(event){
|
2020-04-04 21:48:58 +08:00
|
|
|
if(this.debugDiv.style.display === "none"){
|
|
|
|
return
|
|
|
|
}
|
2020-03-09 20:36:57 +08:00
|
|
|
if(!event || event.type === "resize"){
|
|
|
|
var divPos = this.debugDiv.getBoundingClientRect()
|
|
|
|
var x = divPos.left
|
|
|
|
var y = divPos.top
|
|
|
|
}else{
|
|
|
|
var click = event.type === "touchstart" || event.type === "touchend" ? event.changedTouches[0] : event
|
|
|
|
if(event.type == "blur"){
|
|
|
|
var x = this.moving.x
|
|
|
|
var y = this.moving.y
|
|
|
|
}else{
|
|
|
|
var x = click.clientX - this.moving.x
|
|
|
|
var y = click.clientY - this.moving.y
|
|
|
|
}
|
|
|
|
}
|
2018-10-15 02:08:05 +08:00
|
|
|
var w = this.debugDiv.offsetWidth
|
|
|
|
var h = this.debugDiv.offsetHeight
|
|
|
|
if(x + w > innerWidth){
|
|
|
|
x = innerWidth - w
|
|
|
|
}
|
|
|
|
if(y + h > lastHeight){
|
|
|
|
y = lastHeight - h
|
|
|
|
}
|
|
|
|
if(x < 0){
|
|
|
|
x = 0
|
|
|
|
}
|
|
|
|
if(y < 0){
|
|
|
|
y = 0
|
|
|
|
}
|
|
|
|
this.moveTo(x, y)
|
|
|
|
this.moving = false
|
|
|
|
}
|
|
|
|
moveTo(x, y){
|
|
|
|
this.debugDiv.style.transform = "translate(" + x + "px, " + y + "px)"
|
|
|
|
}
|
|
|
|
restore(){
|
|
|
|
debugObj.state = "open"
|
|
|
|
this.debugDiv.style.display = ""
|
2020-03-09 20:36:57 +08:00
|
|
|
this.stopMove()
|
2018-10-15 02:08:05 +08:00
|
|
|
}
|
|
|
|
minimise(){
|
|
|
|
debugObj.state = "minimised"
|
|
|
|
this.debugDiv.style.display = "none"
|
|
|
|
}
|
|
|
|
updateStatus(){
|
|
|
|
if(debugObj.controller && !this.controller){
|
2019-02-18 00:26:46 +08:00
|
|
|
this.controller = debugObj.controller
|
|
|
|
|
2018-10-15 06:00:40 +08:00
|
|
|
this.restartBtn.style.display = "block"
|
2018-10-15 07:18:01 +08:00
|
|
|
this.autoplayLabel.style.display = "block"
|
2019-02-18 00:26:46 +08:00
|
|
|
if(this.controller.parsedSongData.branches){
|
|
|
|
this.branchHideDiv.style.display = "block"
|
|
|
|
}
|
2020-03-30 14:50:34 +08:00
|
|
|
if(this.controller.lyrics){
|
|
|
|
this.lyricsHideDiv.style.display = "block"
|
|
|
|
}
|
2018-10-15 06:00:40 +08:00
|
|
|
|
2018-10-15 02:08:05 +08:00
|
|
|
var selectedSong = this.controller.selectedSong
|
2018-10-15 06:00:40 +08:00
|
|
|
this.defaultOffset = selectedSong.offset || 0
|
2020-03-12 12:59:28 +08:00
|
|
|
if(this.songHash === selectedSong.hash){
|
2018-10-15 04:14:58 +08:00
|
|
|
this.offsetChange(this.offsetSlider.get(), true)
|
2019-02-18 00:26:46 +08:00
|
|
|
this.branchChange(null, true)
|
2019-03-16 05:34:48 +08:00
|
|
|
this.volumeChange(this.volumeSlider.get(), true)
|
2020-03-30 16:59:42 +08:00
|
|
|
this.lyricsChange(this.lyricsSlider.get(), true)
|
2018-10-15 02:08:05 +08:00
|
|
|
}else{
|
2020-03-12 12:59:28 +08:00
|
|
|
this.songHash = selectedSong.hash
|
2018-10-15 02:08:05 +08:00
|
|
|
this.offsetSlider.set(this.defaultOffset)
|
2019-02-18 00:26:46 +08:00
|
|
|
this.branchReset(null, true)
|
2019-03-16 05:34:48 +08:00
|
|
|
this.volumeSlider.set(this.controller.volume)
|
2020-03-30 14:50:34 +08:00
|
|
|
this.lyricsSlider.set(this.controller.lyrics ? this.controller.lyrics.vttOffset / 1000 : 0)
|
2018-10-15 02:08:05 +08:00
|
|
|
}
|
2018-10-15 04:14:58 +08:00
|
|
|
|
2019-02-21 04:48:21 +08:00
|
|
|
var measures = this.controller.parsedSongData.measures.filter((measure, i, array) => {
|
|
|
|
return i === 0 || Math.abs(measure.ms - array[i - 1].ms) > 0.01
|
|
|
|
})
|
2018-10-15 04:14:58 +08:00
|
|
|
this.measureNumSlider.setMinMax(0, measures.length - 1)
|
2020-03-15 23:00:23 +08:00
|
|
|
if(this.measureNum > 0 && measures.length >= this.measureNum){
|
|
|
|
var measureMS = measures[this.measureNum - 1].ms
|
2018-10-15 04:14:58 +08:00
|
|
|
var game = this.controller.game
|
|
|
|
game.started = true
|
2018-12-13 17:18:52 +08:00
|
|
|
var timestamp = Date.now()
|
2018-10-15 04:14:58 +08:00
|
|
|
var currentDate = timestamp - measureMS
|
|
|
|
game.startDate = currentDate
|
|
|
|
game.sndTime = timestamp - snd.buffer.getTime() * 1000
|
|
|
|
var circles = game.songData.circles
|
|
|
|
for(var i in circles){
|
2018-10-15 06:00:40 +08:00
|
|
|
game.currentCircle = i
|
2018-10-15 07:18:01 +08:00
|
|
|
if(circles[i].endTime >= measureMS){
|
2018-10-15 04:14:58 +08:00
|
|
|
break
|
|
|
|
}
|
2019-04-05 04:40:11 +08:00
|
|
|
game.skipNote(circles[i])
|
2018-10-15 04:14:58 +08:00
|
|
|
}
|
2018-10-25 22:18:41 +08:00
|
|
|
if(game.mainMusicPlaying){
|
|
|
|
game.mainMusicPlaying = false
|
|
|
|
game.mainAsset.stop()
|
|
|
|
}
|
2018-10-15 04:14:58 +08:00
|
|
|
}
|
2018-10-15 07:18:01 +08:00
|
|
|
this.autoplayCheckbox.checked = this.controller.autoPlayEnabled
|
2018-10-15 02:08:05 +08:00
|
|
|
}
|
|
|
|
if(this.controller && !debugObj.controller){
|
2018-10-15 06:00:40 +08:00
|
|
|
this.restartBtn.style.display = ""
|
2018-10-15 07:18:01 +08:00
|
|
|
this.autoplayLabel.style.display = ""
|
2019-02-18 00:26:46 +08:00
|
|
|
this.branchHideDiv.style.display = ""
|
2020-03-30 14:50:34 +08:00
|
|
|
this.lyricsHideDiv.style.display = ""
|
2018-10-15 02:08:05 +08:00
|
|
|
this.controller = null
|
|
|
|
}
|
2020-03-09 20:36:57 +08:00
|
|
|
this.stopMove()
|
2018-10-15 02:08:05 +08:00
|
|
|
}
|
2018-10-15 04:14:58 +08:00
|
|
|
offsetChange(value, noRestart){
|
2018-10-15 02:08:05 +08:00
|
|
|
if(this.controller){
|
|
|
|
var offset = (this.defaultOffset - value) * 1000
|
|
|
|
var songData = this.controller.parsedSongData
|
|
|
|
songData.circles.forEach(circle => {
|
|
|
|
circle.ms = circle.originalMS + offset
|
|
|
|
circle.endTime = circle.originalEndTime + offset
|
|
|
|
})
|
|
|
|
songData.measures.forEach(measure => {
|
|
|
|
measure.ms = measure.originalMS + offset
|
|
|
|
})
|
2019-02-18 00:26:46 +08:00
|
|
|
if(songData.branches){
|
|
|
|
songData.branches.forEach(branch => {
|
|
|
|
branch.ms = branch.originalMS + offset
|
|
|
|
})
|
|
|
|
}
|
2020-03-30 14:50:34 +08:00
|
|
|
if(this.controller.lyrics){
|
|
|
|
this.controller.lyrics.offsetChange(value * 1000)
|
|
|
|
}
|
2018-10-15 06:00:40 +08:00
|
|
|
if(this.restartCheckbox.checked && !noRestart){
|
|
|
|
this.restartSong()
|
2018-10-15 04:14:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
measureNumChange(value){
|
|
|
|
this.measureNum = value
|
2018-10-15 06:00:40 +08:00
|
|
|
if(this.restartCheckbox.checked){
|
|
|
|
this.restartSong()
|
|
|
|
}
|
|
|
|
}
|
2019-03-16 05:34:48 +08:00
|
|
|
volumeChange(value, noRestart){
|
|
|
|
if(this.controller){
|
|
|
|
snd.musicGain.setVolumeMul(value)
|
|
|
|
}
|
|
|
|
if(this.restartCheckbox.checked && !noRestart){
|
|
|
|
this.restartSong()
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 14:50:34 +08:00
|
|
|
lyricsChange(value, noRestart){
|
|
|
|
if(this.controller && this.controller.lyrics){
|
|
|
|
this.controller.lyrics.offsetChange(undefined, value * 1000)
|
|
|
|
}
|
|
|
|
if(this.restartCheckbox.checked && !noRestart){
|
|
|
|
this.restartSong()
|
|
|
|
}
|
|
|
|
}
|
2018-10-15 06:00:40 +08:00
|
|
|
restartSong(){
|
|
|
|
if(this.controller){
|
2018-10-15 04:14:58 +08:00
|
|
|
this.controller.restartSong()
|
2018-10-15 02:08:05 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-09 20:36:57 +08:00
|
|
|
toggleAutoplay(event){
|
2018-10-15 07:18:01 +08:00
|
|
|
if(this.controller){
|
|
|
|
this.controller.autoPlayEnabled = this.autoplayCheckbox.checked
|
2020-03-09 20:36:57 +08:00
|
|
|
if(this.controller.autoPlayEnabled){
|
|
|
|
this.controller.saveScore = false
|
|
|
|
}else{
|
2018-10-15 07:18:01 +08:00
|
|
|
var keyboard = debugObj.controller.keyboard
|
2019-04-17 02:06:41 +08:00
|
|
|
keyboard.setKey(false, "don_l")
|
|
|
|
keyboard.setKey(false, "don_r")
|
|
|
|
keyboard.setKey(false, "ka_l")
|
|
|
|
keyboard.setKey(false, "ka_r")
|
2018-10-15 07:18:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-18 00:26:46 +08:00
|
|
|
branchChange(event, noRestart){
|
|
|
|
if(this.controller){
|
|
|
|
var game = this.controller.game
|
|
|
|
var name = this.branchSelect.value
|
|
|
|
game.branch = name === "auto" ? false : name
|
2019-02-21 04:48:21 +08:00
|
|
|
game.branchSet = name === "auto"
|
2019-04-05 04:40:11 +08:00
|
|
|
if(noRestart){
|
|
|
|
game.branchStatic = true
|
|
|
|
}
|
2019-02-21 04:48:21 +08:00
|
|
|
var selectedOption = this.branchSelect.selectedOptions[0]
|
|
|
|
this.branchSelect.style.background = selectedOption.style.background
|
2019-02-18 00:26:46 +08:00
|
|
|
if(this.restartCheckbox.checked && !noRestart){
|
|
|
|
this.restartSong()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
branchReset(event, noRestart){
|
|
|
|
this.branchSelect.value = "auto"
|
|
|
|
this.branchChange(null, noRestart)
|
|
|
|
}
|
2020-03-09 20:36:57 +08:00
|
|
|
touchBox(event){
|
|
|
|
event.currentTarget.click()
|
|
|
|
}
|
2018-10-15 02:08:05 +08:00
|
|
|
clean(){
|
|
|
|
this.offsetSlider.clean()
|
2019-02-18 00:26:46 +08:00
|
|
|
this.measureNumSlider.clean()
|
2020-03-09 20:36:57 +08:00
|
|
|
this.volumeSlider.clean()
|
2020-03-30 14:50:34 +08:00
|
|
|
this.lyricsSlider.clean()
|
2018-10-15 02:08:05 +08:00
|
|
|
|
2020-03-09 20:36:57 +08:00
|
|
|
pageEvents.remove(window, ["mousedown", "mouseup", "touchstart", "touchend", "blur", "resize"], this.windowSymbol)
|
2019-02-03 20:04:25 +08:00
|
|
|
pageEvents.mouseRemove(this)
|
2020-03-09 20:36:57 +08:00
|
|
|
pageEvents.remove(window, "touchmove")
|
|
|
|
pageEvents.remove(this.titleDiv, ["mousedown", "touchstart"])
|
|
|
|
pageEvents.remove(this.minimiseDiv, ["click", "touchstart"])
|
|
|
|
pageEvents.remove(this.restartBtn, ["click", "touchstart"])
|
|
|
|
pageEvents.remove(this.exitBtn, ["click", "touchstart"])
|
|
|
|
pageEvents.remove(this.restartLabel, "touchstart")
|
|
|
|
pageEvents.remove(this.autoplayLabel, "touchstart")
|
2018-10-15 07:18:01 +08:00
|
|
|
pageEvents.remove(this.autoplayCheckbox, "change")
|
2019-02-18 00:26:46 +08:00
|
|
|
pageEvents.remove(this.branchSelect, "change")
|
2020-03-09 20:36:57 +08:00
|
|
|
pageEvents.remove(this.branchResetBtn, ["click", "touchstart"])
|
2018-10-15 02:08:05 +08:00
|
|
|
|
2020-03-09 20:36:57 +08:00
|
|
|
delete this.offsetSlider
|
|
|
|
delete this.measureNumSlider
|
|
|
|
delete this.volumeSlider
|
2018-10-15 02:08:05 +08:00
|
|
|
delete this.titleDiv
|
|
|
|
delete this.minimiseDiv
|
2018-10-15 07:18:01 +08:00
|
|
|
delete this.offsetDiv
|
|
|
|
delete this.measureNumDiv
|
2019-02-18 00:26:46 +08:00
|
|
|
delete this.branchHideDiv
|
|
|
|
delete this.branchSelectDiv
|
|
|
|
delete this.branchSelect
|
|
|
|
delete this.branchResetBtn
|
2019-03-16 05:34:48 +08:00
|
|
|
delete this.volumeDiv
|
2020-03-30 14:50:34 +08:00
|
|
|
delete this.lyricsHideDiv
|
|
|
|
delete this.lyricsOffsetDiv
|
2018-10-15 07:18:01 +08:00
|
|
|
delete this.restartCheckbox
|
|
|
|
delete this.autoplayLabel
|
|
|
|
delete this.autoplayCheckbox
|
|
|
|
delete this.restartBtn
|
|
|
|
delete this.exitBtn
|
2018-10-15 02:08:05 +08:00
|
|
|
delete this.controller
|
2020-03-09 20:36:57 +08:00
|
|
|
delete this.windowSymbol
|
2018-10-15 02:08:05 +08:00
|
|
|
|
|
|
|
debugObj.state = "closed"
|
|
|
|
debugObj.debug = null
|
|
|
|
document.body.removeChild(this.debugDiv)
|
2018-10-15 04:14:58 +08:00
|
|
|
|
|
|
|
delete this.debugDiv
|
2018-10-15 02:08:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
class InputSlider{
|
2022-02-11 22:28:22 +08:00
|
|
|
constructor(...args){
|
|
|
|
this.init(...args)
|
|
|
|
}
|
|
|
|
init(sliderDiv, min, max, fixedPoint){
|
2018-10-15 04:14:58 +08:00
|
|
|
this.fixedPoint = fixedPoint
|
|
|
|
this.mul = Math.pow(10, fixedPoint)
|
|
|
|
this.min = min * this.mul
|
|
|
|
this.max = max * this.mul
|
|
|
|
|
2018-10-15 02:08:05 +08:00
|
|
|
this.input = sliderDiv.getElementsByTagName("input")[0]
|
|
|
|
this.reset = sliderDiv.getElementsByClassName("reset")[0]
|
|
|
|
this.plus = sliderDiv.getElementsByClassName("plus")[0]
|
|
|
|
this.minus = sliderDiv.getElementsByClassName("minus")[0]
|
|
|
|
this.value = null
|
|
|
|
this.defaultValue = null
|
|
|
|
this.callbacks = []
|
2020-03-09 20:36:57 +08:00
|
|
|
this.touchEnd = []
|
|
|
|
this.windowSymbol = Symbol()
|
|
|
|
pageEvents.add(this.input, ["touchstart", "touchend"], event => {
|
|
|
|
event.stopPropagation()
|
|
|
|
})
|
|
|
|
pageEvents.add(window, ["mouseup", "touchstart", "touchend", "blur"], event => {
|
|
|
|
if(event.type !== "touchstart"){
|
|
|
|
this.touchEnd.forEach(func => func(event))
|
|
|
|
}else if(event.target !== this.input){
|
|
|
|
this.input.blur()
|
|
|
|
}
|
|
|
|
}, this.windowSymbol)
|
2018-10-15 02:08:05 +08:00
|
|
|
|
2020-03-09 20:36:57 +08:00
|
|
|
this.addTouchRepeat(this.plus, this.add.bind(this))
|
|
|
|
this.addTouchRepeat(this.minus, this.subtract.bind(this))
|
|
|
|
this.addTouch(this.reset, this.resetValue.bind(this))
|
2018-10-15 02:08:05 +08:00
|
|
|
pageEvents.add(this.input, "change", this.manualSet.bind(this))
|
2018-10-15 04:14:58 +08:00
|
|
|
pageEvents.add(this.input, "keydown", this.captureKeys.bind(this))
|
2018-10-15 02:08:05 +08:00
|
|
|
}
|
|
|
|
update(noCallback, force){
|
|
|
|
var oldValue = this.input.value
|
|
|
|
if(this.value === null){
|
|
|
|
this.input.value = ""
|
|
|
|
this.input.readOnly = true
|
|
|
|
}else{
|
2018-10-15 04:14:58 +08:00
|
|
|
if(this.value > this.max){
|
|
|
|
this.value = this.max
|
2018-10-15 02:08:05 +08:00
|
|
|
}
|
2018-10-15 04:14:58 +08:00
|
|
|
if(this.value < this.min){
|
|
|
|
this.value = this.min
|
2018-10-15 02:08:05 +08:00
|
|
|
}
|
2018-10-15 04:14:58 +08:00
|
|
|
this.input.value = this.get().toFixed(this.fixedPoint)
|
2018-10-15 02:08:05 +08:00
|
|
|
this.input.readOnly = false
|
|
|
|
}
|
|
|
|
if(force || !noCallback && oldValue !== this.input.value){
|
|
|
|
this.callbacks.forEach(callback => {
|
2018-10-15 04:14:58 +08:00
|
|
|
callback(this.get())
|
2018-10-15 02:08:05 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set(number){
|
2018-10-15 04:14:58 +08:00
|
|
|
this.value = Math.floor(number * this.mul)
|
2018-10-15 02:08:05 +08:00
|
|
|
this.defaultValue = this.value
|
|
|
|
this.update(true)
|
|
|
|
}
|
2018-10-15 04:14:58 +08:00
|
|
|
setMinMax(min, max){
|
|
|
|
this.min = min
|
|
|
|
this.max = max
|
|
|
|
this.update()
|
|
|
|
}
|
2018-10-15 02:08:05 +08:00
|
|
|
get(){
|
|
|
|
if(this.value === null){
|
|
|
|
return null
|
|
|
|
}else{
|
2018-10-15 04:14:58 +08:00
|
|
|
return Math.floor(this.value) / this.mul
|
2018-10-15 02:08:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
add(event){
|
|
|
|
if(this.value !== null){
|
|
|
|
var newValue = this.value + this.eventNumber(event)
|
2018-10-15 04:14:58 +08:00
|
|
|
if(newValue <= this.max){
|
2018-10-15 02:08:05 +08:00
|
|
|
this.value = newValue
|
|
|
|
this.update()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
subtract(event){
|
|
|
|
if(this.value !== null){
|
|
|
|
var newValue = this.value - this.eventNumber(event)
|
2018-10-15 04:14:58 +08:00
|
|
|
if(newValue >= this.min){
|
2018-10-15 02:08:05 +08:00
|
|
|
this.value = newValue
|
|
|
|
this.update()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
eventNumber(event){
|
|
|
|
return (event.ctrlKey ? 10 : 1) * (event.shiftKey ? 10 : 1) * (event.altKey ? 10 : 1) * 1
|
|
|
|
}
|
|
|
|
resetValue(){
|
|
|
|
this.value = this.defaultValue
|
|
|
|
this.update()
|
|
|
|
}
|
|
|
|
onchange(callback){
|
|
|
|
this.callbacks.push(callback)
|
|
|
|
}
|
|
|
|
manualSet(){
|
2018-10-15 04:14:58 +08:00
|
|
|
var number = parseFloat(this.input.value) * this.mul
|
|
|
|
if(Number.isFinite(number) && this.min <= number && number <= this.max){
|
|
|
|
this.value = number
|
2018-10-15 02:08:05 +08:00
|
|
|
}
|
|
|
|
this.update(false, true)
|
|
|
|
}
|
2018-10-15 04:14:58 +08:00
|
|
|
captureKeys(event){
|
|
|
|
event.stopPropagation()
|
|
|
|
}
|
2020-03-09 20:36:57 +08:00
|
|
|
addTouch(element, callback){
|
|
|
|
pageEvents.add(element, ["mousedown", "touchstart"], event => {
|
|
|
|
if(event.type === "touchstart"){
|
|
|
|
event.preventDefault()
|
|
|
|
}else if(event.which !== 1){
|
|
|
|
return
|
|
|
|
}
|
|
|
|
callback(event)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
addTouchRepeat(element, callback){
|
|
|
|
this.addTouch(element, event => {
|
|
|
|
var active = true
|
|
|
|
var func = () => {
|
|
|
|
active = false
|
|
|
|
this.touchEnd.splice(this.touchEnd.indexOf(func), 1)
|
|
|
|
}
|
|
|
|
this.touchEnd.push(func)
|
|
|
|
var repeat = delay => {
|
|
|
|
if(active && this.touchEnd){
|
|
|
|
callback(event)
|
|
|
|
setTimeout(() => repeat(50), delay)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
repeat(400)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
removeTouch(element){
|
|
|
|
pageEvents.remove(element, ["mousedown", "touchstart"])
|
|
|
|
}
|
2018-10-15 02:08:05 +08:00
|
|
|
clean(){
|
2020-03-09 20:36:57 +08:00
|
|
|
this.removeTouch(this.plus)
|
|
|
|
this.removeTouch(this.minus)
|
|
|
|
this.removeTouch(this.reset)
|
|
|
|
pageEvents.remove(this.input, ["touchstart", "touchend"])
|
|
|
|
pageEvents.remove(window, ["mouseup", "touchstart", "touchend", "blur"], this.windowSymbol)
|
|
|
|
pageEvents.remove(this.input, ["touchstart", "change", "keydown"])
|
2018-10-15 02:08:05 +08:00
|
|
|
|
|
|
|
delete this.input
|
|
|
|
delete this.reset
|
|
|
|
delete this.plus
|
|
|
|
delete this.minus
|
2020-03-09 20:36:57 +08:00
|
|
|
delete this.windowSymbol
|
|
|
|
delete this.touchEnd
|
2018-10-15 02:08:05 +08:00
|
|
|
}
|
|
|
|
}
|