Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
54 lines (44 sloc) 1.29 KB
use tetra::graphics::{self, Color, Font, Text};
use tetra::input::{self, Key};
use tetra::math::Vec2;
use tetra::{Context, Settings, State};
struct GameState {
input: String,
text: Text,
}
impl GameState {
fn new() -> GameState {
GameState {
input: String::new(),
text: Text::new("", Font::default(), 32.0),
}
}
}
impl State for GameState {
fn update(&mut self, ctx: &mut Context) -> tetra::Result {
if input::is_key_pressed(ctx, Key::Enter) {
self.input += "\n";
self.text.set_content(self.input.as_str());
}
if input::is_key_pressed(ctx, Key::Backspace) {
self.input.pop();
self.text.set_content(self.input.as_str());
}
if let Some(new_input) = input::get_text_input(ctx) {
self.input += new_input;
self.text.set_content(self.input.as_str());
}
Ok(())
}
fn draw(&mut self, ctx: &mut Context, _dt: f64) -> tetra::Result {
graphics::clear(ctx, Color::rgb(0.392, 0.584, 0.929));
graphics::draw(ctx, &self.text, Vec2::new(16.0, 16.0));
Ok(())
}
}
fn main() {
tetra::run(
&Settings::new("Keyboard Input", 640, 480).quit_on_escape(true),
|_| Ok(GameState::new()),
);
}
You can’t perform that action at this time.