Skip to content

Commit

Permalink
fix: sub-scenes back button
Browse files Browse the repository at this point in the history
This commit fixes an issue that the back button after sub-scenes (setNextScene) would go back to the incorrect place since the code didn't know the last scene.
  • Loading branch information
ThePedroo committed Aug 30, 2023
1 parent 7c08f8a commit e3e619e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 28 deletions.
27 changes: 20 additions & 7 deletions android/app/src/main/java/com/perforvnm/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import android.content.Context
import android.content.SharedPreferences

class MainActivity : Activity() {
private var lastScene: String? = null
private val handler = Handler(Looper.getMainLooper())
private var textSpeed = 1000L
private var sEffectVolume = 1f
Expand Down Expand Up @@ -1097,12 +1098,7 @@ class MainActivity : Activity() {
mediaPlayer = null
}

when (buttonData.getString("scene")) {
"scene1" -> scene1()
"scene4" -> scene4(true)
"scene2" -> scene2(true)
"scene3" -> scene3(true)
}
switchScene(buttonData.getString("scene"))
}

frameLayoutScenes.addView(savesBackground)
Expand Down Expand Up @@ -1361,6 +1357,8 @@ class MainActivity : Activity() {
buttonSubScenes.layoutParams = layoutParamsSubScenes

buttonSubScenes.setOnClickListener {
lastScene = "scene1"

scene2(true)
}

Expand All @@ -1385,6 +1383,8 @@ class MainActivity : Activity() {
buttonSubScenes2.layoutParams = layoutParamsSubScenes2

buttonSubScenes2.setOnClickListener {
lastScene = "scene1"

scene3(true)
}

Expand Down Expand Up @@ -1619,7 +1619,7 @@ class MainActivity : Activity() {

findViewById<FrameLayout>(android.R.id.content).setOnClickListener(null)

scene1()
switchScene(lastScene!!)
}

frameLayout.addView(buttonBack)
Expand Down Expand Up @@ -1863,6 +1863,8 @@ class MainActivity : Activity() {
frameLayout.addView(buttonBack)

findViewById<FrameLayout>(android.R.id.content).setOnClickListener {
lastScene = "scene2"

scene4(true)
}

Expand Down Expand Up @@ -2105,11 +2107,22 @@ class MainActivity : Activity() {
frameLayout.addView(buttonBack)

findViewById<FrameLayout>(android.R.id.content).setOnClickListener {
lastScene = "scene3"

scene4(true)
}

setContentView(frameLayout)
}

private fun switchScene(scene: String) {
when (scene) {
"scene1" -> scene1()
"scene4" -> scene4(true)
"scene2" -> scene2(true)
"scene3" -> scene3(true)
}
}
}

class RectangleView(context: Context) : View(context) {
Expand Down
43 changes: 26 additions & 17 deletions src/coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import helper from './helper.js'

global.visualNovel = { menu: null, info: null, internalInfo: {}, code: '', scenes: [], subScenes: [], customXML: [] }
global.PerforVNM = {
codeGeneratorVersion: '1.21.0',
generatedCodeVersion: '1.18.8',
codeGeneratorVersion: '1.22.0',
generatedCodeVersion: '1.19.8',
repository: 'https://github.com/PerformanC/PerforVNMaker'
}

Expand Down Expand Up @@ -114,10 +114,11 @@ function init(options) {
function finalize() {
helper.replace('__PERFORVNM_CODE__', '')

let switchesCode = 'when (buttonData.getString("scene")) {'
let switchesCode = ' private fun switchScene(scene: String) {' + '\n' +
' when (scene) {'

if (visualNovel.scenes.length) {
let scenesCode = []
let scenesCode = ''

visualNovel.scenes.forEach((scene, i) => {
if (i != visualNovel.scenes.length - 1) {
Expand Down Expand Up @@ -192,7 +193,7 @@ function finalize() {
' override fun onAnimationRepeat(animation: Animation?) {}' + '\n' +
' })' + '\n'
} else {
code += ' ' + nextScene.name + '(' + functionParams.join(', ') + ')' + '\n'
code += ` ${nextScene.name}(${functionParams.join(', ')})` + '\n'
}

code += ' }'
Expand Down Expand Up @@ -240,7 +241,7 @@ function finalize() {
functionParams2.switch.push('true')
}

switchesCode += '\n' + ` "${scene.name}" -> ${scene.name}(${functionParams2.switch.join(', ')})`
switchesCode += '\n' + ` "${scene.name}" -> ${scene.name}(${functionParams2.switch.join(', ')})`

scene.code = scene.code.replace('__PERFORVNM_SCENE_PARAMS__', functionParams2.function.join(', '))

Expand Down Expand Up @@ -306,7 +307,7 @@ function finalize() {
scene.code = scene.code.replace('__PERFORVNM_SUBSCENE_2__', subScene2.name + '(' + subFunctionParams2.switch.join(', ') + ')')
}

switchesCode += '\n' + ` "${scene.name}" -> ${scene.name}(${functionParams.switch.join(', ')})`
switchesCode += '\n' + ` "${scene.name}" -> ${scene.name}(${functionParams.switch.join(', ')})`

scene.code = scene.code.replace('__PERFORVNM_SCENE_' + scene.name.toUpperCase() + '__', '')
scene.code = scene.code.replace('__PERFORVNM_SCENE_PARAMS__', functionParams.function.join(', '))
Expand All @@ -328,7 +329,7 @@ function finalize() {
}
}

scenesCode.push('\n\n' + scene.code)
scenesCode += '\n\n' + scene.code
})

visualNovel.subScenes.forEach((scene, i) => {
Expand Down Expand Up @@ -407,7 +408,7 @@ function finalize() {
' override fun onAnimationRepeat(animation: Animation?) {}' + '\n' +
' })' + '\n'
} else {
code += ' ' + nextScene.name + '(' + functionParams.join(', ') + ')' + '\n'
code += ` ${nextScene.name}(${functionParams.join(', ')})` + '\n'
}

code += ' }'
Expand All @@ -425,7 +426,7 @@ function finalize() {
}

scene.code = scene.code.replace('__PERFORVNM_NEXT_SCENE_PARAMS__', functionParams2.switch.join(', '))
switchesCode += '\n' + ` "${scene.name}" -> ${scene.name}(${functionParams2.switch.join(', ')})`
switchesCode += '\n' + ` "${scene.name}" -> ${scene.name}(${functionParams2.switch.join(', ')})`

scene.code = scene.code.replace('__PERFORVNM_SCENE_PARAMS__', functionParams2.function.join(', '))

Expand Down Expand Up @@ -464,7 +465,7 @@ function finalize() {
functionParams.switch.push('true')
}

switchesCode += '\n' + ` "${scene.name}" -> ${scene.name}(${functionParams.switch.join(', ')})`
switchesCode += '\n' + ` "${scene.name}" -> ${scene.name}(${functionParams.switch.join(', ')})`

scene.code = scene.code.replace('__PERFORVNM_SCENE_' + scene.name.toUpperCase() + '__', '')
scene.code = scene.code.replace('__PERFORVNM_SCENE_PARAMS__', functionParams.function.join(', '))
Expand All @@ -486,12 +487,18 @@ function finalize() {
}
}

scenesCode.push('\n\n' + scene.code)
scenesCode += '\n\n' + scene.code
})

helper.replace('__PERFORVNM_SCENES__', scenesCode.join(''))
} else helper.replace('__PERFORVNM_SCENES__', '')
helper.replace('__PERFORVNM_SCENES__', scenesCode + '__PERFORVNM_SCENES__')
}

switchesCode += '\n' + ' }' + '\n' +
' }'

helper.writeFunction(switchesCode)

helper.replace('__PERFORVNM_SCENES__', '')
helper.replace('__PERFORVNM_MENU__', '// No menu created.')
helper.replace('__PERFORVNM_CLASSES__', '')

Expand Down Expand Up @@ -523,12 +530,14 @@ function finalize() {

helper.replace(/__PERFORVNM_RELEASE_MEDIA_PLAYER__/g, releaseCode)

switchesCode += '\n' + ' }'

helper.replace(/__PERFORVNM_SWITCHES__/g, switchesCode)
helper.replace('__PERFORVNM_SWITCHES__', 'switchScene(buttonData.getString("scene"))')
}

let addHeaders = ''

/* TODO: Use lastScene only when necessary (only when sub-scenes are added) */
addHeaders += ' private var lastScene: String? = null' + '\n'

if (visualNovel.internalInfo.hasSpeech || visualNovel.internalInfo.hasDelayedSoundEffect || visualNovel.internalInfo.hasEffect || visualNovel.internalInfo.hasDelayedMusic || visualNovel.internalInfo.hasDelayedAnimation)
addHeaders += ' private val handler = Handler(Looper.getMainLooper())' + '\n'

Expand Down
19 changes: 15 additions & 4 deletions src/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -1135,10 +1135,15 @@ function finalize(scene) {
sceneCode += ' buttonBack.setOnClickListener {\n'
}

sceneCode += ` ${visualNovel.scenes[visualNovel.scenes.length - 1].name}(${(visualNovel.scenes[visualNovel.scenes.length - 1].speech ? 'false' : '' )})` + '\n' +
' }' + '\n\n' +
if (visualNovel.subScenes.find((subScene) => subScene.next == scene.name)) {
sceneCode += ` switchScene(lastScene!!)` + '\n'
} else {
sceneCode += ` ${visualNovel.scenes[visualNovel.scenes.length - 1].name}(${(visualNovel.scenes[visualNovel.scenes.length - 1].speech ? 'false' : '' )})` + '\n'
}

sceneCode += ' }' + '\n\n' +

' frameLayout.addView(buttonBack)' + '\n\n'
' frameLayout.addView(buttonBack)' + '\n\n'
}

if (scene.subScenes.length == 2) {
Expand Down Expand Up @@ -1166,6 +1171,8 @@ function finalize(scene) {
if (scene.subScenes[0].speech?.author?.name && scene.speech && !scene.speech?.author?.name && i + 1 != visualNovel.scenes.length - 1) functionParams.push('true')

sceneCode += ' buttonSubScenes.setOnClickListener {' + '\n' +
` lastScene = "${scene.name}"` + '\n\n' +

' __PERFORVNM_SUBSCENE_1__' + '\n' +
' }' + '\n\n' +

Expand All @@ -1190,6 +1197,8 @@ function finalize(scene) {
' buttonSubScenes2.layoutParams = layoutParamsSubScenes2' + '\n\n' +

' buttonSubScenes2.setOnClickListener {' + '\n' +
` lastScene = "${scene.name}"` + '\n\n' +

' __PERFORVNM_SUBSCENE_2__' + '\n' +
' }' + '\n\n' +

Expand Down Expand Up @@ -1486,7 +1495,9 @@ function finalize(scene) {
} else {
if (scene.next) {
sceneCode += ' findViewById<FrameLayout>(android.R.id.content).setOnClickListener {' + '\n' +
' ' + scene.next + '(__PERFORVNM_NEXT_SCENE_PARAMS__)' + '\n' +
` lastScene = "${scene.name}"` + '\n\n' +

` ${scene.next}(__PERFORVNM_NEXT_SCENE_PARAMS__)` + '\n' +
' }' + '\n\n' +

' setContentView(frameLayout)' + '\n' +
Expand Down

0 comments on commit e3e619e

Please sign in to comment.