#pragma once #include "../src/gui/widgets.h" #include "../src/gui/indent_section.h" #include "imgui_internal.h" inline void test_style_widgets() { ImGui::CreateContext();auto& io=ImGui::GetIO();io.IniFilename=nullptr; io.DisplaySize={800,600};io.DeltaTime=1.f/60;io.ConfigInputTrickleEventQueue=false; io.Fonts->AddFontDefault();unsigned char* pixels;int width,height;io.Fonts->GetTexDataAsRGBA32(&pixels,&width,&height); theme::apply(); bool checked=false,open=false;int integer=25,selected=0;float number=.5f; char text[32]="amnesia";ImVec2 check_pos{},slider_pos{},combo_pos{}; auto frame=[&](ImVec2 mouse,bool down) { io.AddMousePosEvent(mouse.x,mouse.y);io.AddMouseButtonEvent(0,down); ImGui::NewFrame();ImGui::SetNextWindowPos({20,20});ImGui::SetNextWindowSize({400,520}); ImGui::Begin("style controls",nullptr,ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_NoMove); auto* draw=ImGui::GetWindowDrawList();draw->ChannelsSplit(2);draw->ChannelsSetCurrent(1); check_pos=ImGui::GetCursorScreenPos();widgets::checkbox("toggle",&checked,260,24); widgets::slider_int("integer",&integer,0,100,260);slider_pos=ImGui::GetItemRectMin(); widgets::slider_float("float",&number,0,1,260); widgets::input_text("##text",text,sizeof(text),260); combo_pos=ImGui::GetCursorScreenPos();open=widgets::begin_combo("##mode",selected ? "second" : "first",260); if(open) { if(widgets::selectable("first",selected==0))selected=0; if(widgets::selectable("second",selected==1))selected=1; ImGui::EndCombo(); } widgets::button("action","apply",false,260,26); draw->ChannelsMerge();ImGui::End(); assert(GImGui->ColorStack.empty() && GImGui->StyleVarStack.empty()); ImGui::Render(); for(int i=0;iCmdListsCount;++i) for(const auto& v:ImGui::GetDrawData()->CmdLists[i]->VtxBuffer) assert(std::isfinite(v.pos.x) && std::isfinite(v.pos.y)); }; frame({-100,-100},false); const auto initial_check=check_pos,initial_slider=slider_pos,initial_combo=combo_pos; frame({check_pos.x+7,check_pos.y+10},false);frame({check_pos.x+7,check_pos.y+10},true);frame({check_pos.x+7,check_pos.y+10},false); assert(checked); frame({slider_pos.x+200,slider_pos.y+10},false);frame({slider_pos.x+200,slider_pos.y+10},true);frame({slider_pos.x+200,slider_pos.y+10},false); assert(integer>65 && integer<90 && number==.5f && std::string(text)=="amnesia"); assert(check_pos.x==initial_check.x && check_pos.y==initial_check.y); assert(slider_pos.x==initial_slider.x && slider_pos.y==initial_slider.y); assert(combo_pos.x==initial_combo.x && combo_pos.y==initial_combo.y); frame({combo_pos.x+100,combo_pos.y+10},false);frame({combo_pos.x+100,combo_pos.y+10},true);frame({combo_pos.x+100,combo_pos.y+10},false); assert(open); // The settings reveal must move following rows, preserve scrolling bounds, // and prevent partially revealed controls from accepting clicks. float measured=0;bool activated=false; auto reveal_frame=[&](float amount,ImVec2 mouse,bool down) { io.AddMousePosEvent(mouse.x,mouse.y);io.AddMouseButtonEvent(0,down); ImGui::NewFrame();ImGui::SetNextWindowPos({20,20});ImGui::SetNextWindowSize({400,520}); ImGui::Begin("reveal",nullptr,ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_NoMove); const auto start=ImGui::GetCursorScreenPos(); widgets::indent_section(260,start.y,amount,measured,amount==1,[&](float w) { ImGui::TextUnformatted("settings"); activated|=widgets::button("hidden action","apply",false,w,22); }); const float occupied=ImGui::GetItemRectSize().y; assert(std::abs(occupied-measured*amount)<.01f); assert(ImGui::GetCurrentWindow()->DC.CursorMaxPos.y<=start.y+occupied+.01f); const float below=ImGui::GetCursorScreenPos().y; ImGui::Button("next module",{260,26});ImGui::End(); assert(GImGui->ColorStack.empty() && GImGui->StyleVarStack.empty() && GImGui->GroupStack.empty()); ImGui::Render();return below; }; const float full=reveal_frame(1,{-100,-100},false); const float half=reveal_frame(.5f,{-100,-100},false); const float closed=reveal_frame(0,{-100,-100},false); assert(full>half && half>closed); // A click at the lower action's full-size location cannot fire while sliding. reveal_frame(.5f,{80,full-13},false);reveal_frame(.5f,{80,full-13},true);reveal_frame(.5f,{80,full-13},false); assert(!activated); float a=0,b=0; for(int i=0;i<12;++i)a=widgets::expand_step(a,true,1.f/60); for(int i=0;i<24;++i)b=widgets::expand_step(b,true,1.f/120); assert(std::abs(a-b)<.00001f && a>0 && a<1); assert(widgets::expand_step(a,false,1.f/60)