Skip to content

Commit

Permalink
Added keyboard prompt object
Browse files Browse the repository at this point in the history
  • Loading branch information
EPICGameGuy committed Jan 5, 2024
1 parent 6c80029 commit f28cb1f
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 28 deletions.
25 changes: 24 additions & 1 deletion include/input/keyboard.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
#pragma once

#include <cstdint>
#include <array>

#include "libpad.h"
#include "tamtypes.h"

namespace Input::Keyboard
{
enum class KeyStatus : uint8_t {
none = 0,
pressed = 1,
holding = 2,
released = 3,
};

void init();
void read_inputs();
u8 get_key_status(unsigned char key);
KeyStatus get_key_status(unsigned char ascii_key);
unsigned char convert_ascii_to_keyboard_key(unsigned char ascii_key);
unsigned char convert_keyboard_key_to_ascii(unsigned char keyboard_key);

// Note: convert ascii keys to keyboard keys before looking up keys in the array
std::array<KeyStatus, 256>& get_all_key_statuses();

// Returns true if a key has been just pressed or is held down
bool is_key_down(unsigned char key);

// Returns true if a key has been just released or wasn't pressed
bool is_key_up(unsigned char key);

bool is_shift_down();
} // namespace Input::Keyboard
7 changes: 7 additions & 0 deletions include/objects/components/transform_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

#include <set>

class TransformComponent;

class RootComponentInterface
{
public:
virtual TransformComponent* get_root_component() = 0;
};

class TransformComponent
{
Expand Down
11 changes: 8 additions & 3 deletions include/objects/text_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#include "utils/debuggable.hpp"
#include "components/transform_component.hpp"


class TextObject: public TextRenderable, public Debuggable
class TextObject: public TextRenderable, public RootComponentInterface, public Debuggable
{
public:
TextObject();
Expand All @@ -14,8 +13,14 @@ class TextObject: public TextRenderable, public Debuggable
void draw(bool flush = false);

TransformComponent transform;
virtual TransformComponent* get_root_component() override { return &transform; }

virtual void set_text(std::string_view new_text);
std::string_view get_text() const;

std::string text;
virtual const char* get_name() const override;
virtual const char* get_type_name() const { return typeid(TextObject).name(); }

protected:
std::string text;
};
18 changes: 18 additions & 0 deletions include/objects/text_prompt.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include "objects/text_object.hpp"
#include "tick.hpp"

class TextPrompt: public Tickable, public RootComponentInterface
{
public:
std::string prompt;
std::string inputted_text;

// Actual 3d text rendered
TextObject text_object;

virtual void tick(float deltaTime) override;
virtual TransformComponent* get_root_component() override { return text_object.get_root_component(); }

void set_prompt(std::string_view new_prompt);
};
70 changes: 60 additions & 10 deletions src/input/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
#include "libkbd.h"
#include "ps2kbd.h"

static u8 keyboard_status[256];

namespace Input
{
void Keyboard::init()
static std::array<Keyboard::KeyStatus, 256> keyboard_status;

std::array<Keyboard::KeyStatus, 256>& Keyboard::get_all_key_statuses()
{
return keyboard_status;
}

memset(keyboard_status, sizeof(keyboard_status), 0);
void Keyboard::init()
{
memset(keyboard_status.data(), sizeof(keyboard_status), 0);

{
int ret = SifLoadModule("ps2kbd.irx"_p.to_full_filepath(), 0, nullptr);
Expand All @@ -29,24 +33,70 @@ void Keyboard::init()
}

PS2KbdSetReadmode(PS2KBD_READMODE_RAW);
PS2KbdSetBlockingMode(false);
}

void Keyboard::read_inputs()
{
for (int i = 0; i < 256; ++i)
{
if (keyboard_status[i] == KeyStatus::pressed)
{
keyboard_status[i] = KeyStatus::holding;
}
else if (keyboard_status[i] == KeyStatus::released)
{
keyboard_status[i] = KeyStatus::none;
}
}

PS2KbdRawKey key;
while (PS2KbdReadRaw(&key) != 0)
{
unsigned char c = (key.key + 'a') - 4;
//printf("New key: %u, %u, (%u, %c)\n", key.key, key.state, c, c);
keyboard_status[key.key] = key.state & 0xF;
printf("New key: %u, %u, (%u, %c)\n", key.key, key.state, c, c);
if (key.state & 1)
{
keyboard_status[key.key] = KeyStatus::pressed;
}
else
{
keyboard_status[key.key] = KeyStatus::released;
}
}
}

u8 Keyboard::get_key_status(unsigned char key)
Keyboard::KeyStatus Keyboard::get_key_status(unsigned char ascii_key)
{
return keyboard_status[convert_ascii_to_keyboard_key(ascii_key)];
}

unsigned char Keyboard::convert_ascii_to_keyboard_key(unsigned char ascii_key)
{
return (ascii_key - 'a') + 4;
}

unsigned char Keyboard::convert_keyboard_key_to_ascii(unsigned char keyboard_key)
{
return (keyboard_key + 'a') - 4;
}

bool Keyboard::is_key_down(unsigned char key)
{
const KeyStatus k = Keyboard::get_key_status(key);
return k == KeyStatus::holding || k == KeyStatus::pressed;
}

bool Keyboard::is_key_up(unsigned char key)
{
const KeyStatus k = Keyboard::get_key_status(key);
return k == KeyStatus::released || k == KeyStatus::none;
}

bool Keyboard::is_shift_down()
{
const unsigned char actual_key = (key - 'a') + 4;
//check(actual_key >= 0 && actual_key <= 255);
return keyboard_status[actual_key];
const KeyStatus k = keyboard_status[225];
return k == KeyStatus::holding || k == KeyStatus::pressed;
}

} // namespace Input
30 changes: 18 additions & 12 deletions src/objects/movement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,20 @@ void ThirdPersonMovement::calculate_rotation_input(float delta_time)
input_vector.yaw = (buttons.rjoy_h - 128.f) / 128.f;
input_vector.pitch = (buttons.rjoy_v - 128.f) / 128.f;

// Left arrow
input_vector.yaw += Input::Keyboard::get_key_status(173) * -1;
{
using namespace Input::Keyboard;
// Left arrow
input_vector.yaw += is_key_down(173) * -1;

// Right arrow
input_vector.yaw += Input::Keyboard::get_key_status(172);
// Right arrow
input_vector.yaw += is_key_down(172);

// Up arrow
input_vector.pitch += Input::Keyboard::get_key_status(175);
// Up arrow
input_vector.pitch += is_key_down(175);

// Down arrow
input_vector.pitch += Input::Keyboard::get_key_status(174) * -1;
// Down arrow
input_vector.pitch += is_key_down(174) * -1;
}

const float input_length = input_vector.length();
if (input_length > dead_zone)
Expand Down Expand Up @@ -197,11 +200,14 @@ void ThirdPersonMovement::calculate_movement_input(float delta_time)
input_vector += ((buttons.ljoy_h - 128.f) / 128.f) * right_movement_vector;
input_vector += ((buttons.ljoy_v - 128.f) / 128.f) * forward_movement_vector;

input_vector += Input::Keyboard::get_key_status('a') * right_movement_vector * -1;
input_vector += Input::Keyboard::get_key_status('d') * right_movement_vector;
{
using namespace Input::Keyboard;
input_vector += is_key_down('a') * right_movement_vector * -1;
input_vector += is_key_down('d') * right_movement_vector;

input_vector += Input::Keyboard::get_key_status('s') * forward_movement_vector;
input_vector += Input::Keyboard::get_key_status('w') * forward_movement_vector * -1;
input_vector += is_key_down('s') * forward_movement_vector;
input_vector += is_key_down('w') * forward_movement_vector * -1;
}

if (paddata & PAD_CROSS)
{
Expand Down
10 changes: 10 additions & 0 deletions src/objects/text_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ void TextObject::render(const GS::GSState& gs_state)
}
}

void TextObject::set_text(std::string_view new_text)
{
text = new_text;
}

std::string_view TextObject::get_text() const
{
return text;
}

const char* TextObject::get_name() const
{
if (text.size() > 0)
Expand Down
58 changes: 58 additions & 0 deletions src/objects/text_prompt.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "objects/text_prompt.hpp"

#include "input/keyboard.hpp"

#include <stdio.h>
#include <ctype.h>

void TextPrompt::tick(float deltaTime)
{
using namespace Input::Keyboard;

bool new_text = false;

const auto& keyboard_keys = get_all_key_statuses();
for (int i = 0; i < 256; ++i)
{
if (keyboard_keys[i] == KeyStatus::pressed)
{
const unsigned char ascii_key = convert_keyboard_key_to_ascii(i);
if (isalpha(ascii_key))
{
inputted_text += ascii_key;
new_text = true;
}
else if (ascii_key == '.' || ascii_key == ',')
{
inputted_text += ascii_key;
new_text = true;
}
else if (i == 56 && is_shift_down())
{
inputted_text += '?';
new_text = true;
}
else if (i == 44) // space bar
{
inputted_text += ' ';
new_text = true;
}
else if (i == 42 && inputted_text.size() > 0) // delete key
{
inputted_text.pop_back();
new_text = true;
}
}
}

if (new_text)
{
text_object.set_text(prompt + "\n" + inputted_text);
}
}

void TextPrompt::set_prompt(std::string_view new_prompt)
{
prompt = new_prompt;
text_object.set_text(prompt + "\n" + inputted_text);
}
7 changes: 5 additions & 2 deletions src/world/levels/level1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "objects/teapot.hpp"
#include "objects/camera.hpp"
#include "objects/movement.hpp"
#include "objects/text_prompt.hpp"

#include "input/gamepad.hpp"

Expand Down Expand Up @@ -49,10 +50,11 @@ class Level1: public World::Level
Level1(Asset::Reference level)
: World::Level(level)
{
printf("Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");

// Player teapot
player.teapot_model.transform.set_location(Vector(0.f, 0.f, 0.f));

t1.get_root_component()->set_location(Vector(100.f, 0.f, 0.f));
t1.set_prompt("Welcome to level one!");
}

virtual void initialize() override
Expand All @@ -65,6 +67,7 @@ class Level1: public World::Level
}

Level1Player player;
TextPrompt t1;
};

static struct Level1ConstructorHelper
Expand Down

0 comments on commit f28cb1f

Please sign in to comment.