CustomControls configuring example

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
sigonasr2 2022-10-07 00:44:55 -05:00
parent 66aa6d03c6
commit a7540c87d1
5 changed files with 148 additions and 21 deletions

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -1,21 +1,25 @@
#define OLC_PGE_APPLICATION #define OLC_PGE_APPLICATION
#include "pixelGameEngine.h" #include "pixelGameEngine.h"
//-------------------------------------------------------------------------------------------------- class CustomControls : public olc::PixelGameEngine
// class
//--------------------------------------------------------------------------------------------------
class PressDetect : public olc::PixelGameEngine
{ {
public: public:
PressDetect() CustomControls()
{ {
sAppName = "Double Press Down Detect"; sAppName = "Configuring Custom Controls";
} }
private: private:
bool bothKeysPressed=false; olc::Key MOVE_UP=olc::W;
olc::Key MOVE_DOWN=olc::S;
olc::Key MOVE_RIGHT=olc::D;
olc::Key MOVE_LEFT=olc::A;
olc::vd2d pos = {0,0};
std::array<std::string,4> keysList = {"UP","RIGHT","DOWN","LEFT"};
int configuringKeyIndex = 0;
public: public:
@ -24,25 +28,74 @@ public:
return true; return true;
} }
//--------------------------------------------------------------------------------------------------
// Main Game Function
//--------------------------------------------------------------------------------------------------
bool OnUserUpdate(float fElapsedTime) override bool OnUserUpdate(float fElapsedTime) override
{ {
if (GetKey(olc::F1).bPressed) {
if (GetKey(olc::SHIFT).bHeld&&GetKey(olc::A).bHeld&&!bothKeysPressed) { TextEntryEnable(true);
bothKeysPressed=true; configuringKeyIndex=0;
std::cout<<"Both Keys pressed\n";
} }
if (bothKeysPressed&&(GetKey(olc::SHIFT).bReleased||GetKey(olc::A).bReleased)) { if (TextEntryGetString().size()>0) {
bothKeysPressed=false; char c = TextEntryGetString()[0];
std::cout<<"Both Keys released\n"; olc::Key customKey;
if (c>='a'&&c<='z') {
c-=32; //Capitalize the letter.
customKey=(olc::Key)(c-'A'+1);
} else
if (c>='A'&&c<='Z') {
customKey=(olc::Key)(c-'A'+1);
} else
if (c>='0'&&c<='9') {
customKey=(olc::Key)(c-'0'+olc::Key::K0);
} else
{
return true;
}
switch (configuringKeyIndex) {
case 0:{
MOVE_UP=customKey;
}break;
case 1:{
MOVE_RIGHT=customKey;
}break;
case 2:{
MOVE_DOWN=customKey;
}break;
case 3:{
MOVE_LEFT=customKey;
}break;
}
configuringKeyIndex++;
if (configuringKeyIndex<keysList.size()) {
TextEntryEnable(true);
} else {
TextEntryEnable(false);
}
} }
if (GetKey(MOVE_UP).bHeld) {
pos+={0,-5*fElapsedTime};
}
if (GetKey(MOVE_DOWN).bHeld) {
pos+={0,5*fElapsedTime};
}
if (GetKey(MOVE_RIGHT).bHeld) {
pos+={5*fElapsedTime,0};
}
if (GetKey(MOVE_LEFT).bHeld) {
pos+={-5*fElapsedTime,0};
}
Clear(olc::VERY_DARK_BLUE); Clear(olc::VERY_DARK_BLUE);
DrawRect({32,32},{32,32},(bothKeysPressed)?olc::YELLOW:olc::BLACK); DrawRect(pos,{32,32},olc::WHITE);
if (IsTextEntryEnabled()) {
DrawString({0,0},"Please press a key for "+keysList[configuringKeyIndex]);
}
return true; return true;
} }
@ -51,7 +104,7 @@ public:
int main() int main()
{ {
PressDetect game; CustomControls game;
if (game.Construct(256, 240, 4, 4)) if (game.Construct(256, 240, 4, 4))
game.Start(); game.Start();