-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: send body to plugins, text input example
- Loading branch information
1 parent
d98a286
commit 264731f
Showing
14 changed files
with
477 additions
and
4 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
class TextInput extends RenJS.Plugin { | ||
|
||
async onCall(params) { | ||
const str = params.body.split(' '); | ||
await this.showTextInput(...str); | ||
this.game.resolveAction(); | ||
} | ||
|
||
onStart(){ | ||
// keep track of which names are changed during game | ||
this.game.managers.logic.vars.names = [] | ||
} | ||
|
||
onLoad(slot,dataParsed){ | ||
for (const name of dataParsed.vars.names){ | ||
// add names to character objects | ||
this.game.managers.character.characters[name].name = dataParsed.vars[`TextInputName_${name}`] | ||
} | ||
} | ||
|
||
showTextInput(propertyType,propertyName,defaultValue){ | ||
return new Promise(resolve=>{ | ||
// lock game scale so it won't resize while showing the text box, or it will look bad | ||
this.game.lockScale = true; | ||
// create html input | ||
const input = this.createInputElement(defaultValue); | ||
|
||
const confirmChange = async ()=>{ | ||
if (input.value != ""){ | ||
// play an sfx if you want | ||
// this.game.managers.audio.playSFX('buttonsfx'); | ||
if (propertyType == 'var'){ | ||
this.game.managers.logic.vars[propertyName] = input.value; | ||
} | ||
if (propertyType == 'name' && this.game.managers.character.characters[propertyName]){ | ||
this.game.managers.logic.vars[`TextInputName_${propertyName}`] = input.value; | ||
this.game.managers.logic.vars.names.push(propertyName); | ||
// change also the character name to show in nameBoxes | ||
this.game.managers.character.characters[propertyName].config.displayName = input.value; | ||
} | ||
|
||
// hide everything | ||
let tween = this.game.add.tween(input.style).to({opacity:0},750,Phaser.Easing.Linear.None,true); | ||
await this.game.screenEffects.transition.FADEOUT(btn); | ||
// destroy everything | ||
btn.destroy(); | ||
input.remove(); | ||
// re activate hud | ||
this.game.gui.hud.show(); | ||
// unlock scaling | ||
this.game.lockScale = false; | ||
|
||
// end action | ||
resolve(); | ||
} | ||
} | ||
|
||
// confirm on enter | ||
input.onkeydown = function(e){ | ||
if(e.keyCode==13){ | ||
confirmChange(); | ||
} | ||
e.stopPropagation(); | ||
}; | ||
input.style.opacity = 0; | ||
document.body.appendChild(input); | ||
// create confirm button ass well | ||
const btn = this.game.add.button( | ||
this.game.world.centerX, | ||
this.game.world.centerY+100, | ||
"confirmBtn", | ||
confirmChange, | ||
this,1,0,2,0); | ||
btn.anchor.set(0.5); | ||
|
||
// deactivate the hud, so players won't mess up with it while inputing text | ||
this.game.gui.hud.hide(); | ||
// fade in background and input element | ||
this.game.screenEffects.transition.FADEIN(btn); | ||
this.game.add.tween(input.style).to({opacity:1},750,Phaser.Easing.Linear.None,true); | ||
}) | ||
} | ||
|
||
createInputElement(defaultValue){ | ||
const input = document.createElement("input"); | ||
input.type = "text"; | ||
input.value = defaultValue ? defaultValue : "" | ||
// add a css class to the input | ||
// input.className = "canvas-input"; | ||
|
||
// if you want max input text length, add it here | ||
// input.setAttribute('maxlength', '10'); | ||
|
||
// the input element needs to be scaled along he game | ||
const scale = this.game.scale.width/this.game.width; | ||
const inputProps = { | ||
x:100, | ||
y:350, | ||
width:600, | ||
height:50, | ||
fontSize:25 | ||
} | ||
const canvas = this.game.canvas; | ||
const canvasWidth = canvas.offsetWidth; | ||
const canvasHeight = canvas.offsetHeight; | ||
const topLeft = [(canvasWidth/2)-(this.game.scale.width/2)+canvas.offsetLeft, (canvasHeight/2)-(this.game.scale.height/2)+canvas.offsetTop] | ||
input.style.position = "absolute"; | ||
input.style.left = (topLeft[0]+(inputProps.x*scale))+"px"; | ||
input.style.top = (topLeft[1]+(inputProps.y*scale))+"px"; | ||
input.style.width = (inputProps.width*scale)+"px"; | ||
input.style.height = (inputProps.height*scale)+"px"; | ||
input.style['font-size'] = (inputProps.fontSize*scale)+"px"; | ||
return input; | ||
} | ||
|
||
async changeProperty(value,propertyType,propertyName){ | ||
|
||
} | ||
} | ||
|
||
RenJSGame.addPlugin('TextInput',TextInput) |
Oops, something went wrong.