Fixed annoying bugs with multiple textboxes and clicking between them.

master
sigonasr2 2 years ago
parent 28493a2102
commit e844a8f5ec
  1. 52
      FiestaCraftingCalculator/FiestaCraftingCalculator.cpp
  2. 26
      FiestaCraftingCalculator/olcPGEX_QuickGUI.h
  3. 4
      FiestaCraftingCalculator/olcPixelGameEngine.h

@ -11,12 +11,8 @@ using namespace olc::QuickGUI;
int PANEL_WIDTH=620;
int PANEL_HEIGHT=240;
class FiestaCraftingCalculator : public olc::PixelGameEngine
{
enum Image{
PRODUCTION,
COLLECTIBLE,
@ -126,10 +122,12 @@ class FiestaCraftingCalculator : public olc::PixelGameEngine
ImageCheckBox*DecompProd;
CustomButton*CloseButton;
ListBox*RecipeList;
TextBox*Amount;
std::string DisplayProdText;
std::string SelectedProdText;
std::vector<std::string> recipeItems;
Recipe*selectedItem;
static bool disabledAllCalculators;
public:
Calculator(){
PotionProd=new ImageCheckBox(manager,FiestaCraftingCalculator::ProdIcon,false,displayPos+vi2d{10,10},{40,40},{192,128},{32,32});
@ -139,8 +137,13 @@ class FiestaCraftingCalculator : public olc::PixelGameEngine
DecompProd=new ImageCheckBox(manager,FiestaCraftingCalculator::ProdIcon,false,displayPos+vi2d{190,10},{40,40},{64,160},{32,32});
CloseButton=new CustomButton(manager,FiestaCraftingCalculator::CloseIcon,FiestaCraftingCalculator::CloseBackIcon,displayPos+vf2d{PANEL_WIDTH-30.f,0},{26,26},{0,0},{26,26});
RecipeList=new ListBox(manager,recipeItems,displayPos+vi2d{10,90},{300,140},16);
Amount=new TextBox(manager,"1",displayPos+vi2d{356,10},{68,24},{2,2});
Amount->bVisible=false;
RecipeList->bVisible=false;
}
void OnTextEntryComplete(const std::string& sText){
//Amount->m_bTextEdit=false;
}
bool Update(FiestaCraftingCalculator*pge,int panelNumber){
displayPos=vf2d{float(10+panelNumber%2*PANEL_WIDTH),float(10+panelNumber/2*PANEL_HEIGHT)};
PotionProd->vPos=displayPos+vi2d{10,10};
@ -152,6 +155,7 @@ class FiestaCraftingCalculator : public olc::PixelGameEngine
RecipeList->vPos=displayPos+vi2d{10,90};
RecipeList->m_pSlider->vPosMin=displayPos+vi2d{10,90}+vf2d{300 - manager.fGrabRad - 1,manager.fGrabRad + 1 };
RecipeList->m_pSlider->vPosMax=displayPos+vi2d{10,90}+vf2d{300 - manager.fGrabRad - 1,140-manager.fGrabRad - 1 };
Amount->vPos=displayPos+vi2d{356,10};
manager.Update(pge);
DisplayProdText=SelectedProdText;
if(CloseButton->bPressed){
@ -160,7 +164,7 @@ class FiestaCraftingCalculator : public olc::PixelGameEngine
if(PotionProd->bPressed){
StoneProd->bChecked=ScrollProd->bChecked=CompProd->bChecked=DecompProd->bChecked=false;
SelectedProdText="Potion Production";
RecipeList->bVisible=true;
RecipeList->bVisible=Amount->bVisible=true;
recipeItems.clear();
RecipeList->m_pSlider->fValue=0;
RecipeList->nSelectedItem=RecipeList->nPreviouslySelectedItem=0;
@ -175,7 +179,7 @@ class FiestaCraftingCalculator : public olc::PixelGameEngine
if(StoneProd->bPressed){
PotionProd->bChecked=ScrollProd->bChecked=CompProd->bChecked=DecompProd->bChecked=false;
SelectedProdText="Stone Production";
RecipeList->bVisible=true;
RecipeList->bVisible=Amount->bVisible=true;
recipeItems.clear();
RecipeList->m_pSlider->fValue=0;
RecipeList->nSelectedItem=RecipeList->nPreviouslySelectedItem=0;
@ -190,7 +194,7 @@ class FiestaCraftingCalculator : public olc::PixelGameEngine
if(ScrollProd->bPressed){
StoneProd->bChecked=PotionProd->bChecked=CompProd->bChecked=DecompProd->bChecked=false;
SelectedProdText="Scroll Production";
RecipeList->bVisible=true;
RecipeList->bVisible=Amount->bVisible=true;
recipeItems.clear();
RecipeList->m_pSlider->fValue=0;
RecipeList->nSelectedItem=RecipeList->nPreviouslySelectedItem=0;
@ -205,7 +209,7 @@ class FiestaCraftingCalculator : public olc::PixelGameEngine
if(CompProd->bPressed){
StoneProd->bChecked=ScrollProd->bChecked=PotionProd->bChecked=DecompProd->bChecked=false;
SelectedProdText="Material Composition";
RecipeList->bVisible=true;
RecipeList->bVisible=Amount->bVisible=true;
recipeItems.clear();
RecipeList->m_pSlider->fValue=0;
RecipeList->nSelectedItem=RecipeList->nPreviouslySelectedItem=0;
@ -220,7 +224,7 @@ class FiestaCraftingCalculator : public olc::PixelGameEngine
if(DecompProd->bPressed){
StoneProd->bChecked=ScrollProd->bChecked=CompProd->bChecked=PotionProd->bChecked=false;
SelectedProdText="Material Decomposition";
RecipeList->bVisible=true;
RecipeList->bVisible=Amount->bVisible=true;
recipeItems.clear();
RecipeList->m_pSlider->fValue=0;
RecipeList->nSelectedItem=RecipeList->nPreviouslySelectedItem=0;
@ -278,12 +282,29 @@ class FiestaCraftingCalculator : public olc::PixelGameEngine
pge->DrawStringPropDecal(displayPos+vf2d{360,110+yOffset},"x "+std::to_string(item.second),WHITE,{1.75,1.75});
yOffset+=36;
}
if(Amount->m_bTextEdit){
for(Calculator*c:calculators){
if(c!=this){
c->Amount->Enable(false);
}
}
disabledAllCalculators=true;
} else
if (disabledAllCalculators){
for(Calculator*c:calculators){
c->Amount->Enable(true);
}
}
if(Amount->sText.size()>4){
Amount->sText=Amount->sText.substr(0,4);
pge->sTextEntryString=Amount->sText;
pge->nTextEntryCursor=4;
}
}
manager.DrawDecal(pge);
return true;
}
};
std::vector<Calculator*>calculators;
Renderable Karen;
public:
FiestaCraftingCalculator()
@ -291,6 +312,7 @@ public:
sAppName = "Fiesta Crafting Calculator";
}
static std::vector<Calculator*>calculators;
static Renderable ProdIcon,CloseIcon,CloseBackIcon,ProductionIcons,CollectibleIcons,CollectibleIcons2,GradeIcons;
public:
std::string slurp(std::ifstream& in) {
@ -299,6 +321,14 @@ public:
return sstr.str();
}
void OnTextEntryComplete(const std::string& sText)override{
for(auto it=calculators.begin();it!=calculators.end();++it){
Calculator*c=*it;
c->OnTextEntryComplete(sText);
}
}
bool OnUserCreate() override
{
// Called once at the start, so create things here
@ -583,6 +613,8 @@ std::map<std::string,FiestaCraftingCalculator::ItemData>FiestaCraftingCalculator
{"Converter2",{"Alchemy Stone[2]",{255,255,0},{4,2},PRODUCTION}},
{"Converter4",{"Alchemy Stone[4]",{0,255,255},{4,2},PRODUCTION}},
};
std::vector<FiestaCraftingCalculator::Calculator*>FiestaCraftingCalculator::calculators;
bool FiestaCraftingCalculator::Calculator::disabledAllCalculators=false;
int main()
{
FiestaCraftingCalculator calculator;

@ -210,16 +210,17 @@ namespace olc::QuickGUI
TextBox(olc::QuickGUI::Manager& manager, // Associate with a Manager
const std::string& text, // Text to display
const olc::vf2d& pos, // Location of text box top-left
const olc::vf2d& size); // Size of text box
const olc::vf2d& size,
const olc::vf2d& fontSize={1,1}); // Size of text box
public: // BaseControl overrides
void Update(olc::PixelGameEngine* pge) override;
void Draw(olc::PixelGameEngine* pge) override;
void DrawDecal(olc::PixelGameEngine* pge) override;
protected:
bool m_bTextEdit = false;
protected:
vf2d fontSize;
};
// Creates a Button Control - a clickable, labelled rectangle
@ -569,8 +570,8 @@ namespace olc::QuickGUI
#pragma region TextBox
TextBox::TextBox(olc::QuickGUI::Manager& manager, const std::string& text, const olc::vf2d& pos, const olc::vf2d& size)
: Label(manager, text, pos, size)
TextBox::TextBox(olc::QuickGUI::Manager& manager, const std::string& text, const olc::vf2d& pos, const olc::vf2d& size,const olc::vf2d& fontSize)
: Label(manager, text, pos, size),fontSize(fontSize)
{
nAlign = Alignment::Left;
bHasBorder = true;
@ -587,6 +588,12 @@ namespace olc::QuickGUI
olc::vf2d vMouse = pge->GetMousePos();
if (m_bTextEdit && !pge->IsTextEntryEnabled()){
//sText = pge->TextEntryGetString();
m_bTextEdit = false;
}
if (vMouse.x >= vPos.x && vMouse.x < vPos.x + vSize.x &&
vMouse.y >= vPos.y && vMouse.y < vPos.y + vSize.y)
{
@ -609,7 +616,6 @@ namespace olc::QuickGUI
bHeld = pge->GetMouse(olc::Mouse::LEFT).bHeld;
}
else
{
@ -618,13 +624,13 @@ namespace olc::QuickGUI
bReleased = pge->GetMouse(olc::Mouse::LEFT).bReleased;
bHeld = pge->GetMouse(olc::Mouse::LEFT).bHeld;
if (bPressed && m_bTextEdit)
if (bPressed && m_bTextEdit && pge->IsTextEntryEnabled())
{
sText = pge->TextEntryGetString();
pge->TextEntryEnable(false);
m_bTextEdit = false;
}
}
}
if (m_bTextEdit && pge->IsTextEntryEnabled())
sText = pge->TextEntryGetString();
@ -678,13 +684,13 @@ namespace olc::QuickGUI
{
// Draw Cursor
int32_t i = pge->TextEntryGetCursor();
olc::vf2d vCursorPos = pge->GetTextSizeProp(sText.substr(0, i));
olc::vf2d vCursorPos = pge->GetTextSizeProp(sText.substr(0, i))*fontSize;
pge->FillRectDecal(olc::vf2d(vPos.x + 2.0f + vCursorPos.x, (vPos.y + (vSize.y - 10.0f) * 0.5f)), { 2, 10 }, m_manager.colText);
}
// Draw Text
olc::vf2d vText = pge->GetTextSizeProp(sText);
pge->DrawStringPropDecal(olc::vf2d(vPos.x + 2.0f, vPos.y + (vSize.y - vText.y) * 0.5f), sText, m_manager.colText);
pge->DrawStringPropDecal(olc::vf2d(vPos.x + 2.0f, vPos.y + (vSize.y - vText.y) * 0.5f)-vf2d{0,1*fontSize.y}, sText, m_manager.colText,fontSize);
}
#pragma endregion

@ -1204,6 +1204,8 @@ namespace olc
#endif
public: // Branding
std::string sAppName;
std::string sTextEntryString = "";
int32_t nTextEntryCursor = 0;
private: // Inner mysterious workings
olc::Sprite* pDrawTarget = nullptr;
@ -1260,8 +1262,6 @@ namespace olc
// Text Entry Specific
bool bTextEntryEnable = false;
std::string sTextEntryString = "";
int32_t nTextEntryCursor = 0;
std::vector<std::tuple<olc::Key, std::string, std::string>> vKeyboardMap;

Loading…
Cancel
Save