Skip to content
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 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ add([
- added quadratic bezier and Catmull-Rom evaluation
- added evaluation of the first and second derivatives for all splines
- added higher order easing functions linear, steps and cubic-bezier
- added a text input component

## Deprecated

Expand Down
3 changes: 2 additions & 1 deletion examples/ciTest.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"easing",
"bench",
"gamepad",
"binding"
"binding",
"textInput"
]
}
31 changes: 31 additions & 0 deletions examples/textInput.js
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`
}
}
])
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export * from "./misc/lifespan";
export * from "./misc/named";
export * from "./misc/state";
export * from "./misc/stay";
export * from "./misc/textInput";
export * from "./misc/timer";
export * from "./physics/area";
export * from "./physics/body";
Expand Down
51 changes: 51 additions & 0 deletions src/components/misc/textInput.ts
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();
},
};
}
6 changes: 3 additions & 3 deletions src/kaboom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ import {
state,
stay,
text,
textInput,
tile,
timer,
uvquad,
Expand Down Expand Up @@ -5155,9 +5156,7 @@ const kaplay = <
if (data[tag]) {
// pushes the inspect function (eg: `sprite: "bean"`)
lines.push(`${data[tag]}`);
}

else {
} else {
// pushes only the tag (name of the component)
lines.push(`${tag}`);
}
Expand Down Expand Up @@ -5709,6 +5708,7 @@ const kaplay = <
body,
doubleJump,
shader,
textInput,
timer,
fixed,
stay,
Expand Down
19 changes: 19 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import type {
StayComp,
TextComp,
TextCompOpt,
TextInputComp,
TileComp,
TileCompOpt,
TimerComp,
Expand Down Expand Up @@ -135,6 +136,7 @@ export type {
StayComp,
TextComp,
TextCompOpt,
TextInputComp,
Texture,
TileComp,
TileCompOpt,
Expand Down Expand Up @@ -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.
Copy link
Member

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.

*
* @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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just can't input text right? You can still see it.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.
*
Expand Down