九.《Imgui》常用的控件
源碼所處位置

普通按鈕
ImGui::Button
static?int?clicked?=?0;
if?(ImGui::Button("Button"))
????clicked++;
if?(clicked?&?1)
{
????ImGui::SameLine();//同一行
????ImGui::Text("Thanks?for?clicking?me!");
}
顏色按鈕-壓棧彈棧的方式設(shè)置按鈕樣式
for?(int?i?=?0;?i?<?7;?i++)
{
????if?(i?>?0)
????????ImGui::SameLine();
????ImGui::PushID(i);
????ImGui::PushStyleColor(ImGuiCol_Button,?(ImVec4)ImColor::HSV(i?/?7.0f,?0.6f,?0.6f));
????ImGui::PushStyleColor(ImGuiCol_ButtonHovered,?(ImVec4)ImColor::HSV(i?/?7.0f,?0.7f,?0.7f));
????ImGui::PushStyleColor(ImGuiCol_ButtonActive,?(ImVec4)ImColor::HSV(i?/?7.0f,?0.8f,?0.8f));
????ImGui::Button("Click");
????ImGui::PopStyleColor(3);
????ImGui::PopID();
}
單選按鈕

選擇控件改變變量

static?int?e?=?0;
ImGui::RadioButton("radio?a",?&e,?0);?ImGui::SameLine();
ImGui::RadioButton("radio?b",?&e,?1);?ImGui::SameLine();
ImGui::RadioButton("radio?c",?&e,?2);
箭頭按鈕改變變量


//?Use?AlignTextToFramePadding()?to?align?text?baseline?to?the?baseline?of?framed?widgets?elements
//?(otherwise?a?Text+SameLine+Button?sequence?will?have?the?text?a?little?too?high?by?default!)
//?See?'Demo->Layout->Text?Baseline?Alignment'?for?details.
//使用AlignTextToFramePadding()將文本基線(xiàn)與框架小部件元素的基線(xiàn)對(duì)齊
//(否則,默認(rèn)情況下,Text+SameLine+按鈕序列的文本將有點(diǎn)太高?。?br>ImGui::AlignTextToFramePadding();
ImGui::Text("Hold?to?repeat:");
ImGui::SameLine();
//?Arrow?buttons?with?Repeater
IMGUI_DEMO_MARKER("Widgets/Basic/Buttons?(Repeating)");
static?int?counter?=?0;
float?spacing?=?ImGui::GetStyle().ItemInnerSpacing.x;
ImGui::PushButtonRepeat(true);//壓入設(shè)置當(dāng)前Button狀態(tài)?
//建議還是用ImGui::PushButtonRepeat(true);來(lái)設(shè)置好?這樣每次只要是激活就會(huì)返回true
if?(ImGui::ArrowButton("##left",?ImGuiDir_Left))?{?counter--;?}
ImGui::SameLine(0.0f,?spacing);
if?(ImGui::ArrowButton("##right",?ImGuiDir_Right))?{?counter++;?}
ImGui::PopButtonRepeat();//彈出設(shè)置當(dāng)前Button狀態(tài)
ImGui::SameLine();
ImGui::Text("%d",?counter);
ImGui::RadioButton按鈕狀態(tài)
static?int?e?=?0;
ImGui::RadioButton("radio?a",?&e,?0);?ImGui::SameLine();
ImGui::RadioButton("radio?b",?&e,?1);?ImGui::SameLine();
ImGui::RadioButton("radio?c",?&e,?2);
if?(e?==?0)
{
//
}
else?if?(e==1)
{
//
}
else?if?(e?==?2)
{
//
}
ImGui::Button按鈕狀態(tài)
if?(ImGui::IsItemActive())
{
????counter++;
????ImGui::SameLine();
????ImGui::Text(u8"激活");
????ImGui::SameLine();
????ImGui::Text("%d",?counter);
}
else?if?(ImGui::IsItemClicked())
{
????//很快?基本是一幀
????ImGui::SameLine();
????ImGui::Text(u8"被點(diǎn)擊");
}
重復(fù)按鈕狀態(tài)

1.這是另外一種方法 通過(guò)函數(shù)判斷
2.但是如果是2個(gè)按鈕就不好判斷了
3.建議還是上面用ImGui::PushButtonRepeat(true);來(lái)設(shè)置好 這樣每次只要是激活就會(huì)返回true
重復(fù)

ImGui::ArrowButton("##left",?ImGuiDir_Left);
ImGui::SameLine(0.0f,?spacing);
ImGui::ArrowButton("##right",?ImGuiDir_Right);
if?(ImGui::IsItemActive())//最近一個(gè)是否是激活狀態(tài)
{
????counter++;
????ImGui::SameLine();
????ImGui::Text(u8"激活");
????ImGui::SameLine();
????ImGui::Text("%d",?counter);
}
else?if?(ImGui::IsItemClicked())
{
????//很快?基本是一幀
????ImGui::SameLine();
????ImGui::Text(u8"被點(diǎn)擊");
}
顏色按鈕狀態(tài)
1.注意對(duì)于這種循環(huán)添加的按鈕
2.一定要吧ID設(shè)置不一樣
顏色按鈕的響應(yīng)

static?int?index?=?-1;
for?(int?i?=?0;?i?<?7;?i++)
{
????if?(i?>?0)
????ImGui::SameLine();
????ImGui::PushID(i);
????ImGui::PushStyleColor(ImGuiCol_Button,?(ImVec4)ImColor::HSV(i?/?7.0f,?0.6f,?0.6f));
????ImGui::PushStyleColor(ImGuiCol_ButtonHovered,?(ImVec4)ImColor::HSV(i?/?7.0f,?0.7f,?0.7f));
????ImGui::PushStyleColor(ImGuiCol_ButtonActive,?(ImVec4)ImColor::HSV(i?/?7.0f,?0.8f,?0.8f));
????if?(ImGui::Button("Click"))
????{
????????index?=i;
????}
????ImGui::PopStyleColor(3);
????ImGui::PopID();
}
ImGui::Text(u8"ColorButton被點(diǎn)擊:%d",?index);
按鈕顏色樣式
1.還可以通過(guò)全局設(shè)置樣式
2.這個(gè)之前講過(guò)了就不講了
3.只需要通過(guò)下圖,所改就可以了
