Skip to content

Commit

Permalink
Simplified usage of combo boxes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brotcrunsher committed Dec 16, 2023
1 parent 8dcfd32 commit e15af7f
Showing 1 changed file with 28 additions and 70 deletions.
98 changes: 28 additions & 70 deletions Examples/ExampleMother/ExampleMother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,6 @@ struct Task
INTEGER,
FLOAT,
};
static constexpr const char* inputTypeItems[] = { "None", "Integer", "Float" };
static InputType strToInputType(const char* str)
{
if (strcmp(str, inputTypeItems[0]) == 0) return InputType::NONE;
if (strcmp(str, inputTypeItems[1]) == 0) return InputType::INTEGER;
if (strcmp(str, inputTypeItems[2]) == 0) return InputType::FLOAT;
throw bbe::IllegalStateException();
}
static const char* inputTypeToStr(InputType it)
{
if (it == InputType::NONE) return inputTypeItems[0];
if (it == InputType::INTEGER) return inputTypeItems[1];
if (it == InputType::FLOAT) return inputTypeItems[2];
throw bbe::IllegalStateException();
}

char title[1024] = {};
int32_t repeatDays = 0;
Expand All @@ -63,7 +48,7 @@ struct Task
int32_t internalValue = 0;
int32_t internalValueIncrease = 0;
int32_t followUp2 = 0;
InputType inputType = InputType::NONE;
int32_t inputType = 0;
bbe::List<float> history;
bool advanceable = false;
bool oneShot = false;
Expand All @@ -77,7 +62,6 @@ struct Task
bool earlyAdvanceable = true;

// Non-Persisted Helper Data below.
const char* inputTypeStr = inputTypeItems[0];
int32_t inputInt = 0;
float inputFloat = 0;
bool armedToPlaySound = false;
Expand Down Expand Up @@ -173,10 +157,7 @@ struct Task
buffer.read(retVal.internalValue);
buffer.read(retVal.internalValueIncrease);
buffer.read(retVal.followUp2);
int32_t inputType = 0;
buffer.read(inputType);
retVal.inputType = (InputType)inputType;
retVal.inputTypeStr = inputTypeToStr(retVal.inputType);
buffer.read(retVal.inputType);
buffer.read(retVal.history);
buffer.read(retVal.advanceable);
buffer.read(retVal.oneShot);
Expand Down Expand Up @@ -239,34 +220,16 @@ struct Task

struct Process
{
static constexpr const char* typeStrings[] = { "Unknown", "System", "Other", "Game" };
static constexpr int32_t TYPE_UNKNOWN = 0;
static constexpr int32_t TYPE_SYSTEM = 1;
static constexpr int32_t TYPE_OTHER = 2;
static constexpr int32_t TYPE_GAME = 3;
static int32_t strToType(const char* str)
{
if (strcmp(str, typeStrings[0]) == 0) return TYPE_UNKNOWN;
if (strcmp(str, typeStrings[1]) == 0) return TYPE_SYSTEM;
if (strcmp(str, typeStrings[2]) == 0) return TYPE_OTHER;
if (strcmp(str, typeStrings[3]) == 0) return TYPE_GAME;
throw bbe::IllegalStateException();
}
static const char* typeToStr(int32_t it)
{
if (it == TYPE_UNKNOWN) return typeStrings[0];
if (it == TYPE_SYSTEM) return typeStrings[1];
if (it == TYPE_OTHER) return typeStrings[2];
if (it == TYPE_GAME) return typeStrings[3];
throw bbe::IllegalStateException();
}

char title[1024] = {};
int32_t type = TYPE_UNKNOWN;


// Non-Persisted Helper Data below.
const char* inputTypeStr = typeStrings[0];

void serialize(bbe::ByteBuffer& buffer) const
{
Expand All @@ -279,7 +242,6 @@ struct Process

strcpy(retVal.title, buffer.readNullString());
buffer.read(retVal.type);
retVal.inputTypeStr = typeToStr(retVal.type);

return retVal;
}
Expand Down Expand Up @@ -730,7 +692,7 @@ class MyGame : public bbe::Game
ImGui::TableSetColumnIndex(column++);
if (showDone)
{
if (t.inputType == Task::InputType::NONE)
if (t.inputType == 0 /*None*/)
{
if (!t.oneShot)
{
Expand All @@ -750,7 +712,7 @@ class MyGame : public bbe::Game
}
}
}
else if (t.inputType == Task::InputType::INTEGER)
else if (t.inputType == 1 /*Integer*/)
{
if (ImGui::InputInt("##input", &t.inputInt, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue))
{
Expand All @@ -759,7 +721,7 @@ class MyGame : public bbe::Game
contentsChanged = true;
}
}
else if (t.inputType == Task::InputType::FLOAT)
else if (t.inputType == 2 /*Float*/)
{
if (ImGui::InputFloat("##input", &t.inputFloat, 0, 0, "%.3f", ImGuiInputTextFlags_EnterReturnsTrue))
{
Expand Down Expand Up @@ -846,19 +808,8 @@ class MyGame : public bbe::Game
tooltip("An internal value that can be printed out in the title via %%d, [SEC], and [MIN].");
taskChanged |= ImGui::InputInt("Internal Value Increase", &t.internalValueIncrease);
tooltip("Increases the Internal Value on ever Done by this much.");
if (ImGui::BeginCombo("Input Type", t.inputTypeStr))
{
for (int i = 0; i < IM_ARRAYSIZE(Task::inputTypeItems); i++)
{
if (ImGui::Selectable(Task::inputTypeItems[i]))
{
t.inputTypeStr = Task::inputTypeItems[i];
t.inputType = Task::strToInputType(t.inputTypeStr);
taskChanged = true;
}
}
ImGui::EndCombo();
}
taskChanged |= combo("Input Type", { "None", "Integer", "Float" }, t.inputType);

return taskChanged;
}

Expand Down Expand Up @@ -931,6 +882,26 @@ class MyGame : public bbe::Game
tooltip(text.getRaw());
}

bool combo(const char* label, const bbe::List<bbe::String>& selections, int32_t& selection)
{
bool retVal = false;

if (ImGui::BeginCombo(label, selections[selection].getRaw()))
{
for (int32_t i = 0; i < selections.getLength(); i++)
{
if (ImGui::Selectable(selections[i].getRaw()))
{
selection = i;
retVal = true;
}
}
ImGui::EndCombo();
}

return retVal;
}

virtual void draw3D(bbe::PrimitiveBrush3D& brush) override
{
if (!editMode)
Expand Down Expand Up @@ -1009,19 +980,7 @@ class MyGame : public bbe::Game
ImGui::Text(p.title);

ImGui::TableSetColumnIndex(1);
if (ImGui::BeginCombo("Type", p.inputTypeStr))
{
for (int i = 0; i < IM_ARRAYSIZE(Process::typeStrings); i++)
{
if (ImGui::Selectable(Process::typeStrings[i]))
{
p.inputTypeStr = Process::typeStrings[i];
p.type = i;
processChanged = true;
}
}
ImGui::EndCombo();
}
processChanged |= combo("Type", { "Unknown", "System", "Other", "Game" }, p.type);
ImGui::PopID();
}
if (processChanged)
Expand Down Expand Up @@ -1091,7 +1050,6 @@ class MyGame : public bbe::Game

if (ImGui::Button("New Task"))
{
tempTask.inputType = Task::strToInputType(tempTask.inputTypeStr);
tempTask.setNextExecution(year, month, day);
tasks.add(tempTask);
tempTask = Task();
Expand Down

0 comments on commit e15af7f

Please sign in to comment.