-
-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: text inputs #191
Merged
Merged
feat: text inputs #191
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -52,6 +52,7 @@ | |
"easing", | ||
"bench", | ||
"gamepad", | ||
"binding" | ||
"binding", | ||
"textInput" | ||
] | ||
} |
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,31 @@ | ||
// Start kaboom | ||
kaplay(); | ||
|
||
setBackground(BLACK) | ||
|
||
// Add the game object that asks a question | ||
add([ | ||
anchor("top"), | ||
pos(width()/2, 0), | ||
text("Whats your favorite food :D") | ||
]); | ||
|
||
// Add the node that you write in | ||
const food = add([ | ||
text(""), | ||
textInput(true, 10), // make it have focus and only be 20 chars max | ||
pos(width()/2, height()/2), | ||
anchor("center"), | ||
]); | ||
|
||
// add the response | ||
add([ | ||
text(""), | ||
anchor("bot"), | ||
pos(width()/2, height()), | ||
{ | ||
update(){ | ||
this.text = `wow i didnt know you love ${food.text} so much, but i like it too :D` | ||
} | ||
} | ||
]) |
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
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,51 @@ | ||
import { getKaboomContext } from "../../kaboom"; | ||
import type { Comp, KEventController } from "../../types"; | ||
|
||
/** | ||
* The {@link stay `stay()`} component. | ||
* | ||
* @group Component Types | ||
*/ | ||
export interface TextInputComp extends Comp { | ||
/** | ||
* Enable the text input array from being modified by user input. | ||
*/ | ||
hasFocus: boolean; | ||
} | ||
|
||
export function textInput( | ||
hasFocus: boolean = true, | ||
maxInputLength?: number, | ||
): TextInputComp { | ||
let k = getKaboomContext(this); | ||
let charEv: KEventController; | ||
let backEv: KEventController; | ||
return { | ||
id: "textInput", | ||
hasFocus: hasFocus, | ||
add() { | ||
charEv = k.onCharInput((character) => { | ||
if ( | ||
this.hasFocus | ||
&& (!maxInputLength || this.text.length < maxInputLength) | ||
) { | ||
if (k.isKeyDown("shift")) { | ||
this.text += character.toUpperCase(); | ||
} else { | ||
this.text += character; | ||
} | ||
} | ||
}); | ||
|
||
backEv = k.onKeyPress("backspace", () => { | ||
if (this.hasFocus) { | ||
this.text = this.text.slice(0, -1); | ||
} | ||
}); | ||
}, | ||
destroy() { | ||
charEv.cancel(); | ||
backEv.cancel(); | ||
}, | ||
}; | ||
} |
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
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 |
---|---|---|
|
@@ -42,6 +42,7 @@ import type { | |
StayComp, | ||
TextComp, | ||
TextCompOpt, | ||
TextInputComp, | ||
TileComp, | ||
TileCompOpt, | ||
TimerComp, | ||
|
@@ -135,6 +136,7 @@ export type { | |
StayComp, | ||
TextComp, | ||
TextCompOpt, | ||
TextInputComp, | ||
Texture, | ||
TileComp, | ||
TileCompOpt, | ||
|
@@ -699,6 +701,23 @@ export interface KaboomCtx< | |
* @group Components | ||
*/ | ||
shader(id: string, uniform?: Uniform | (() => Uniform)): ShaderComp; | ||
/** | ||
* Enable timer related functions like wait(), loop(), tween() on the game object. | ||
* | ||
* @example | ||
* ```js | ||
* const obj = add([ | ||
* text(""), | ||
* textInput(), | ||
* ]) | ||
* | ||
* obj.hasFocus = false | ||
* debug.log(obj.text) // oh no i cant see my new text since it was disabled | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You just can't input text right? You can still see it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, when hasFocus is false then you cant put new text, you can still see all the old text |
||
* ``` | ||
* | ||
* @group Components | ||
*/ | ||
textInput(hasFocus?: boolean, maxInputLength?: number): TextInputComp; | ||
/** | ||
* Enable timer related functions like wait(), loop(), tween() on the game object. | ||
* | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this needs to be Enables text input on an object with a text component.