2
0

imgui_demo.cpp 442 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757
  1. // dear imgui, v1.90.9 WIP
  2. // (demo code)
  3. // Help:
  4. // - Read FAQ at http://dearimgui.com/faq
  5. // - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp. All applications in examples/ are doing that.
  6. // - Need help integrating Dear ImGui in your codebase?
  7. // - Read Getting Started https://github.com/ocornut/imgui/wiki/Getting-Started
  8. // - Read 'Programmer guide' in imgui.cpp for notes on how to setup Dear ImGui in your codebase.
  9. // Read top of imgui.cpp and imgui.h for many details, documentation, comments, links.
  10. // Get the latest version at https://github.com/ocornut/imgui
  11. // How to easily locate code?
  12. // - Use the Item Picker to debug break in code by clicking any widgets: https://github.com/ocornut/imgui/wiki/Debug-Tools
  13. // - Browse an online version the demo with code linked to hovered widgets: https://pthom.github.io/imgui_manual_online/manual/imgui_manual.html
  14. // - Find a visible string and search for it in the code!
  15. //---------------------------------------------------
  16. // PLEASE DO NOT REMOVE THIS FILE FROM YOUR PROJECT!
  17. //---------------------------------------------------
  18. // Message to the person tempted to delete this file when integrating Dear ImGui into their codebase:
  19. // Think again! It is the most useful reference code that you and other coders will want to refer to and call.
  20. // Have the ImGui::ShowDemoWindow() function wired in an always-available debug menu of your game/app!
  21. // Also include Metrics! ItemPicker! DebugLog! and other debug features.
  22. // Removing this file from your project is hindering access to documentation for everyone in your team,
  23. // likely leading you to poorer usage of the library.
  24. // Everything in this file will be stripped out by the linker if you don't call ImGui::ShowDemoWindow().
  25. // If you want to link core Dear ImGui in your shipped builds but want a thorough guarantee that the demo will not be
  26. // linked, you can setup your imconfig.h with #define IMGUI_DISABLE_DEMO_WINDOWS and those functions will be empty.
  27. // In another situation, whenever you have Dear ImGui available you probably want this to be available for reference.
  28. // Thank you,
  29. // -Your beloved friend, imgui_demo.cpp (which you won't delete)
  30. //--------------------------------------------
  31. // ABOUT THE MEANING OF THE 'static' KEYWORD:
  32. //--------------------------------------------
  33. // In this demo code, we frequently use 'static' variables inside functions.
  34. // A static variable persists across calls. It is essentially a global variable but declared inside the scope of the function.
  35. // Think of "static int n = 0;" as "global int n = 0;" !
  36. // We do this IN THE DEMO because we want:
  37. // - to gather code and data in the same place.
  38. // - to make the demo source code faster to read, faster to change, smaller in size.
  39. // - it is also a convenient way of storing simple UI related information as long as your function
  40. // doesn't need to be reentrant or used in multiple threads.
  41. // This might be a pattern you will want to use in your code, but most of the data you would be working
  42. // with in a complex codebase is likely going to be stored outside your functions.
  43. //-----------------------------------------
  44. // ABOUT THE CODING STYLE OF OUR DEMO CODE
  45. //-----------------------------------------
  46. // The Demo code in this file is designed to be easy to copy-and-paste into your application!
  47. // Because of this:
  48. // - We never omit the ImGui:: prefix when calling functions, even though most code here is in the same namespace.
  49. // - We try to declare static variables in the local scope, as close as possible to the code using them.
  50. // - We never use any of the helpers/facilities used internally by Dear ImGui, unless available in the public API.
  51. // - We never use maths operators on ImVec2/ImVec4. For our other sources files we use them, and they are provided
  52. // by imgui.h using the IMGUI_DEFINE_MATH_OPERATORS define. For your own sources file they are optional
  53. // and require you either enable those, either provide your own via IM_VEC2_CLASS_EXTRA in imconfig.h.
  54. // Because we can't assume anything about your support of maths operators, we cannot use them in imgui_demo.cpp.
  55. // Navigating this file:
  56. // - In Visual Studio: CTRL+comma ("Edit.GoToAll") can follow symbols inside comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot.
  57. // - In Visual Studio w/ Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols inside comments.
  58. // - In VS Code, CLion, etc.: CTRL+click can follow symbols inside comments.
  59. /*
  60. Index of this file:
  61. // [SECTION] Forward Declarations
  62. // [SECTION] Helpers
  63. // [SECTION] Demo Window / ShowDemoWindow()
  64. // - ShowDemoWindow()
  65. // - sub section: ShowDemoWindowWidgets()
  66. // - sub section: ShowDemoWindowLayout()
  67. // - sub section: ShowDemoWindowPopups()
  68. // - sub section: ShowDemoWindowTables()
  69. // - sub section: ShowDemoWindowInputs()
  70. // [SECTION] About Window / ShowAboutWindow()
  71. // [SECTION] Style Editor / ShowStyleEditor()
  72. // [SECTION] User Guide / ShowUserGuide()
  73. // [SECTION] Example App: Main Menu Bar / ShowExampleAppMainMenuBar()
  74. // [SECTION] Example App: Debug Console / ShowExampleAppConsole()
  75. // [SECTION] Example App: Debug Log / ShowExampleAppLog()
  76. // [SECTION] Example App: Simple Layout / ShowExampleAppLayout()
  77. // [SECTION] Example App: Property Editor / ShowExampleAppPropertyEditor()
  78. // [SECTION] Example App: Long Text / ShowExampleAppLongText()
  79. // [SECTION] Example App: Auto Resize / ShowExampleAppAutoResize()
  80. // [SECTION] Example App: Constrained Resize / ShowExampleAppConstrainedResize()
  81. // [SECTION] Example App: Simple overlay / ShowExampleAppSimpleOverlay()
  82. // [SECTION] Example App: Fullscreen window / ShowExampleAppFullscreen()
  83. // [SECTION] Example App: Manipulating window titles / ShowExampleAppWindowTitles()
  84. // [SECTION] Example App: Custom Rendering using ImDrawList API / ShowExampleAppCustomRendering()
  85. // [SECTION] Example App: Documents Handling / ShowExampleAppDocuments()
  86. */
  87. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  88. #define _CRT_SECURE_NO_WARNINGS
  89. #endif
  90. #include "imgui.h"
  91. #ifndef IMGUI_DISABLE
  92. // System includes
  93. #include <ctype.h> // toupper
  94. #include <limits.h> // INT_MIN, INT_MAX
  95. #include <math.h> // sqrtf, powf, cosf, sinf, floorf, ceilf
  96. #include <stdio.h> // vsnprintf, sscanf, printf
  97. #include <stdlib.h> // NULL, malloc, free, atoi
  98. #include <stdint.h> // intptr_t
  99. #if !defined(_MSC_VER) || _MSC_VER >= 1800
  100. #include <inttypes.h> // PRId64/PRIu64, not avail in some MinGW headers.
  101. #endif
  102. // Visual Studio warnings
  103. #ifdef _MSC_VER
  104. #pragma warning (disable: 4127) // condition expression is constant
  105. #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen
  106. #pragma warning (disable: 26451) // [Static Analyzer] Arithmetic overflow : Using operator 'xxx' on a 4 byte value and then casting the result to an 8 byte value. Cast the value to the wider type before calling operator 'xxx' to avoid overflow(io.2).
  107. #endif
  108. // Clang/GCC warnings with -Weverything
  109. #if defined(__clang__)
  110. #if __has_warning("-Wunknown-warning-option")
  111. #pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' // not all warnings are known by all Clang versions and they tend to be rename-happy.. so ignoring warnings triggers new warnings on some configuration. Great!
  112. #endif
  113. #pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx'
  114. #pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse.
  115. #pragma clang diagnostic ignored "-Wdeprecated-declarations" // warning: 'xx' is deprecated: The POSIX name for this.. // for strdup used in demo code (so user can copy & paste the code)
  116. #pragma clang diagnostic ignored "-Wint-to-void-pointer-cast" // warning: cast to 'void *' from smaller integer type
  117. #pragma clang diagnostic ignored "-Wformat-security" // warning: format string is not a string literal
  118. #pragma clang diagnostic ignored "-Wexit-time-destructors" // warning: declaration requires an exit-time destructor // exit-time destruction order is undefined. if MemFree() leads to users code that has been disabled before exit it might cause problems. ImGui coding style welcomes static/globals.
  119. #pragma clang diagnostic ignored "-Wunused-macros" // warning: macro is not used // we define snprintf/vsnprintf on Windows so they are available, but not always used.
  120. #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning: zero as null pointer constant // some standard header variations use #define NULL 0
  121. #pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double.
  122. #pragma clang diagnostic ignored "-Wreserved-id-macro" // warning: macro name is a reserved identifier
  123. #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision
  124. #pragma clang diagnostic ignored "-Wunsafe-buffer-usage" // warning: 'xxx' is an unsafe pointer used for buffer access
  125. #elif defined(__GNUC__)
  126. #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind
  127. #pragma GCC diagnostic ignored "-Wint-to-pointer-cast" // warning: cast to pointer from integer of different size
  128. #pragma GCC diagnostic ignored "-Wformat-security" // warning: format string is not a string literal (potentially insecure)
  129. #pragma GCC diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function
  130. #pragma GCC diagnostic ignored "-Wconversion" // warning: conversion to 'xxxx' from 'xxxx' may alter its value
  131. #pragma GCC diagnostic ignored "-Wmisleading-indentation" // [__GNUC__ >= 6] warning: this 'if' clause does not guard this statement // GCC 6.0+ only. See #883 on GitHub.
  132. #endif
  133. // Play it nice with Windows users (Update: May 2018, Notepad now supports Unix-style carriage returns!)
  134. #ifdef _WIN32
  135. #define IM_NEWLINE "\r\n"
  136. #else
  137. #define IM_NEWLINE "\n"
  138. #endif
  139. // Helpers
  140. #if defined(_MSC_VER) && !defined(snprintf)
  141. #define snprintf _snprintf
  142. #endif
  143. #if defined(_MSC_VER) && !defined(vsnprintf)
  144. #define vsnprintf _vsnprintf
  145. #endif
  146. // Format specifiers for 64-bit values (hasn't been decently standardized before VS2013)
  147. #if !defined(PRId64) && defined(_MSC_VER)
  148. #define PRId64 "I64d"
  149. #define PRIu64 "I64u"
  150. #elif !defined(PRId64)
  151. #define PRId64 "lld"
  152. #define PRIu64 "llu"
  153. #endif
  154. // Helpers macros
  155. // We normally try to not use many helpers in imgui_demo.cpp in order to make code easier to copy and paste,
  156. // but making an exception here as those are largely simplifying code...
  157. // In other imgui sources we can use nicer internal functions from imgui_internal.h (ImMin/ImMax) but not in the demo.
  158. #define IM_MIN(A, B) (((A) < (B)) ? (A) : (B))
  159. #define IM_MAX(A, B) (((A) >= (B)) ? (A) : (B))
  160. #define IM_CLAMP(V, MN, MX) ((V) < (MN) ? (MN) : (V) > (MX) ? (MX) : (V))
  161. // Enforce cdecl calling convention for functions called by the standard library,
  162. // in case compilation settings changed the default to e.g. __vectorcall
  163. #ifndef IMGUI_CDECL
  164. #ifdef _MSC_VER
  165. #define IMGUI_CDECL __cdecl
  166. #else
  167. #define IMGUI_CDECL
  168. #endif
  169. #endif
  170. //-----------------------------------------------------------------------------
  171. // [SECTION] Forward Declarations, Helpers
  172. //-----------------------------------------------------------------------------
  173. #if !defined(IMGUI_DISABLE_DEMO_WINDOWS)
  174. // Forward Declarations
  175. static void ShowExampleAppMainMenuBar();
  176. static void ShowExampleAppConsole(bool* p_open);
  177. static void ShowExampleAppCustomRendering(bool* p_open);
  178. static void ShowExampleAppDocuments(bool* p_open);
  179. static void ShowExampleAppLog(bool* p_open);
  180. static void ShowExampleAppLayout(bool* p_open);
  181. static void ShowExampleAppPropertyEditor(bool* p_open);
  182. static void ShowExampleAppSimpleOverlay(bool* p_open);
  183. static void ShowExampleAppAutoResize(bool* p_open);
  184. static void ShowExampleAppConstrainedResize(bool* p_open);
  185. static void ShowExampleAppFullscreen(bool* p_open);
  186. static void ShowExampleAppLongText(bool* p_open);
  187. static void ShowExampleAppWindowTitles(bool* p_open);
  188. static void ShowExampleMenuFile();
  189. // We split the contents of the big ShowDemoWindow() function into smaller functions
  190. // (because the link time of very large functions grow non-linearly)
  191. static void ShowDemoWindowWidgets();
  192. static void ShowDemoWindowLayout();
  193. static void ShowDemoWindowPopups();
  194. static void ShowDemoWindowTables();
  195. static void ShowDemoWindowColumns();
  196. static void ShowDemoWindowInputs();
  197. //-----------------------------------------------------------------------------
  198. // [SECTION] Helpers
  199. //-----------------------------------------------------------------------------
  200. // Helper to display a little (?) mark which shows a tooltip when hovered.
  201. // In your own code you may want to display an actual icon if you are using a merged icon fonts (see docs/FONTS.md)
  202. static void HelpMarker(const char* desc)
  203. {
  204. ImGui::TextDisabled("(?)");
  205. if (ImGui::BeginItemTooltip())
  206. {
  207. ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
  208. ImGui::TextUnformatted(desc);
  209. ImGui::PopTextWrapPos();
  210. ImGui::EndTooltip();
  211. }
  212. }
  213. // Helper to wire demo markers located in code to an interactive browser
  214. typedef void (*ImGuiDemoMarkerCallback)(const char* file, int line, const char* section, void* user_data);
  215. extern ImGuiDemoMarkerCallback GImGuiDemoMarkerCallback;
  216. extern void* GImGuiDemoMarkerCallbackUserData;
  217. ImGuiDemoMarkerCallback GImGuiDemoMarkerCallback = NULL;
  218. void* GImGuiDemoMarkerCallbackUserData = NULL;
  219. #define IMGUI_DEMO_MARKER(section) do { if (GImGuiDemoMarkerCallback != NULL) GImGuiDemoMarkerCallback(__FILE__, __LINE__, section, GImGuiDemoMarkerCallbackUserData); } while (0)
  220. //-----------------------------------------------------------------------------
  221. // [SECTION] Demo Window / ShowDemoWindow()
  222. //-----------------------------------------------------------------------------
  223. // - ShowDemoWindow()
  224. // - ShowDemoWindowWidgets()
  225. // - ShowDemoWindowLayout()
  226. // - ShowDemoWindowPopups()
  227. // - ShowDemoWindowTables()
  228. // - ShowDemoWindowColumns()
  229. // - ShowDemoWindowInputs()
  230. //-----------------------------------------------------------------------------
  231. // Demonstrate most Dear ImGui features (this is big function!)
  232. // You may execute this function to experiment with the UI and understand what it does.
  233. // You may then search for keywords in the code when you are interested by a specific feature.
  234. void ImGui::ShowDemoWindow(bool* p_open)
  235. {
  236. // Exceptionally add an extra assert here for people confused about initial Dear ImGui setup
  237. // Most functions would normally just assert/crash if the context is missing.
  238. IM_ASSERT(ImGui::GetCurrentContext() != NULL && "Missing Dear ImGui context. Refer to examples app!");
  239. // Verify ABI compatibility between caller code and compiled version of Dear ImGui. This helps detects some build issues.
  240. IMGUI_CHECKVERSION();
  241. // Examples Apps (accessible from the "Examples" menu)
  242. static bool show_app_main_menu_bar = false;
  243. static bool show_app_console = false;
  244. static bool show_app_custom_rendering = false;
  245. static bool show_app_documents = false;
  246. static bool show_app_log = false;
  247. static bool show_app_layout = false;
  248. static bool show_app_property_editor = false;
  249. static bool show_app_simple_overlay = false;
  250. static bool show_app_auto_resize = false;
  251. static bool show_app_constrained_resize = false;
  252. static bool show_app_fullscreen = false;
  253. static bool show_app_long_text = false;
  254. static bool show_app_window_titles = false;
  255. if (show_app_main_menu_bar) ShowExampleAppMainMenuBar();
  256. if (show_app_documents) ShowExampleAppDocuments(&show_app_documents);
  257. if (show_app_console) ShowExampleAppConsole(&show_app_console);
  258. if (show_app_custom_rendering) ShowExampleAppCustomRendering(&show_app_custom_rendering);
  259. if (show_app_log) ShowExampleAppLog(&show_app_log);
  260. if (show_app_layout) ShowExampleAppLayout(&show_app_layout);
  261. if (show_app_property_editor) ShowExampleAppPropertyEditor(&show_app_property_editor);
  262. if (show_app_simple_overlay) ShowExampleAppSimpleOverlay(&show_app_simple_overlay);
  263. if (show_app_auto_resize) ShowExampleAppAutoResize(&show_app_auto_resize);
  264. if (show_app_constrained_resize) ShowExampleAppConstrainedResize(&show_app_constrained_resize);
  265. if (show_app_fullscreen) ShowExampleAppFullscreen(&show_app_fullscreen);
  266. if (show_app_long_text) ShowExampleAppLongText(&show_app_long_text);
  267. if (show_app_window_titles) ShowExampleAppWindowTitles(&show_app_window_titles);
  268. // Dear ImGui Tools (accessible from the "Tools" menu)
  269. static bool show_tool_metrics = false;
  270. static bool show_tool_debug_log = false;
  271. static bool show_tool_id_stack_tool = false;
  272. static bool show_tool_style_editor = false;
  273. static bool show_tool_about = false;
  274. if (show_tool_metrics)
  275. ImGui::ShowMetricsWindow(&show_tool_metrics);
  276. if (show_tool_debug_log)
  277. ImGui::ShowDebugLogWindow(&show_tool_debug_log);
  278. if (show_tool_id_stack_tool)
  279. ImGui::ShowIDStackToolWindow(&show_tool_id_stack_tool);
  280. if (show_tool_style_editor)
  281. {
  282. ImGui::Begin("Dear ImGui Style Editor", &show_tool_style_editor);
  283. ImGui::ShowStyleEditor();
  284. ImGui::End();
  285. }
  286. if (show_tool_about)
  287. ImGui::ShowAboutWindow(&show_tool_about);
  288. // Demonstrate the various window flags. Typically you would just use the default!
  289. static bool no_titlebar = false;
  290. static bool no_scrollbar = false;
  291. static bool no_menu = false;
  292. static bool no_move = false;
  293. static bool no_resize = false;
  294. static bool no_collapse = false;
  295. static bool no_close = false;
  296. static bool no_nav = false;
  297. static bool no_background = false;
  298. static bool no_bring_to_front = false;
  299. static bool unsaved_document = false;
  300. ImGuiWindowFlags window_flags = 0;
  301. if (no_titlebar) window_flags |= ImGuiWindowFlags_NoTitleBar;
  302. if (no_scrollbar) window_flags |= ImGuiWindowFlags_NoScrollbar;
  303. if (!no_menu) window_flags |= ImGuiWindowFlags_MenuBar;
  304. if (no_move) window_flags |= ImGuiWindowFlags_NoMove;
  305. if (no_resize) window_flags |= ImGuiWindowFlags_NoResize;
  306. if (no_collapse) window_flags |= ImGuiWindowFlags_NoCollapse;
  307. if (no_nav) window_flags |= ImGuiWindowFlags_NoNav;
  308. if (no_background) window_flags |= ImGuiWindowFlags_NoBackground;
  309. if (no_bring_to_front) window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus;
  310. if (unsaved_document) window_flags |= ImGuiWindowFlags_UnsavedDocument;
  311. if (no_close) p_open = NULL; // Don't pass our bool* to Begin
  312. // We specify a default position/size in case there's no data in the .ini file.
  313. // We only do it to make the demo applications a little more welcoming, but typically this isn't required.
  314. const ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  315. ImGui::SetNextWindowPos(ImVec2(main_viewport->WorkPos.x + 650, main_viewport->WorkPos.y + 20), ImGuiCond_FirstUseEver);
  316. ImGui::SetNextWindowSize(ImVec2(550, 680), ImGuiCond_FirstUseEver);
  317. // Main body of the Demo window starts here.
  318. if (!ImGui::Begin("Dear ImGui Demo", p_open, window_flags))
  319. {
  320. // Early out if the window is collapsed, as an optimization.
  321. ImGui::End();
  322. return;
  323. }
  324. // Most "big" widgets share a common width settings by default. See 'Demo->Layout->Widgets Width' for details.
  325. // e.g. Use 2/3 of the space for widgets and 1/3 for labels (right align)
  326. //ImGui::PushItemWidth(-ImGui::GetWindowWidth() * 0.35f);
  327. // e.g. Leave a fixed amount of width for labels (by passing a negative value), the rest goes to widgets.
  328. ImGui::PushItemWidth(ImGui::GetFontSize() * -12);
  329. // Menu Bar
  330. if (ImGui::BeginMenuBar())
  331. {
  332. if (ImGui::BeginMenu("Menu"))
  333. {
  334. IMGUI_DEMO_MARKER("Menu/File");
  335. ShowExampleMenuFile();
  336. ImGui::EndMenu();
  337. }
  338. if (ImGui::BeginMenu("Examples"))
  339. {
  340. IMGUI_DEMO_MARKER("Menu/Examples");
  341. ImGui::MenuItem("Main menu bar", NULL, &show_app_main_menu_bar);
  342. ImGui::SeparatorText("Mini apps");
  343. ImGui::MenuItem("Console", NULL, &show_app_console);
  344. ImGui::MenuItem("Custom rendering", NULL, &show_app_custom_rendering);
  345. ImGui::MenuItem("Documents", NULL, &show_app_documents);
  346. ImGui::MenuItem("Log", NULL, &show_app_log);
  347. ImGui::MenuItem("Property editor", NULL, &show_app_property_editor);
  348. ImGui::MenuItem("Simple layout", NULL, &show_app_layout);
  349. ImGui::MenuItem("Simple overlay", NULL, &show_app_simple_overlay);
  350. ImGui::SeparatorText("Concepts");
  351. ImGui::MenuItem("Auto-resizing window", NULL, &show_app_auto_resize);
  352. ImGui::MenuItem("Constrained-resizing window", NULL, &show_app_constrained_resize);
  353. ImGui::MenuItem("Fullscreen window", NULL, &show_app_fullscreen);
  354. ImGui::MenuItem("Long text display", NULL, &show_app_long_text);
  355. ImGui::MenuItem("Manipulating window titles", NULL, &show_app_window_titles);
  356. ImGui::EndMenu();
  357. }
  358. //if (ImGui::MenuItem("MenuItem")) {} // You can also use MenuItem() inside a menu bar!
  359. if (ImGui::BeginMenu("Tools"))
  360. {
  361. IMGUI_DEMO_MARKER("Menu/Tools");
  362. #ifndef IMGUI_DISABLE_DEBUG_TOOLS
  363. const bool has_debug_tools = true;
  364. #else
  365. const bool has_debug_tools = false;
  366. #endif
  367. ImGui::MenuItem("Metrics/Debugger", NULL, &show_tool_metrics, has_debug_tools);
  368. ImGui::MenuItem("Debug Log", NULL, &show_tool_debug_log, has_debug_tools);
  369. ImGui::MenuItem("ID Stack Tool", NULL, &show_tool_id_stack_tool, has_debug_tools);
  370. ImGui::MenuItem("Style Editor", NULL, &show_tool_style_editor);
  371. bool is_debugger_present = ImGui::GetIO().ConfigDebugIsDebuggerPresent;
  372. if (ImGui::MenuItem("Item Picker", NULL, false, has_debug_tools && is_debugger_present))
  373. ImGui::DebugStartItemPicker();
  374. if (!is_debugger_present)
  375. ImGui::SetItemTooltip("Requires io.ConfigDebugIsDebuggerPresent=true to be set.\n\nWe otherwise disable the menu option to avoid casual users crashing the application.\n\nYou can however always access the Item Picker in Metrics->Tools.");
  376. ImGui::Separator();
  377. ImGui::MenuItem("About Dear ImGui", NULL, &show_tool_about);
  378. ImGui::EndMenu();
  379. }
  380. ImGui::EndMenuBar();
  381. }
  382. ImGui::Text("dear imgui says hello! (%s) (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM);
  383. ImGui::Spacing();
  384. IMGUI_DEMO_MARKER("Help");
  385. if (ImGui::CollapsingHeader("Help"))
  386. {
  387. ImGui::SeparatorText("ABOUT THIS DEMO:");
  388. ImGui::BulletText("Sections below are demonstrating many aspects of the library.");
  389. ImGui::BulletText("The \"Examples\" menu above leads to more demo contents.");
  390. ImGui::BulletText("The \"Tools\" menu above gives access to: About Box, Style Editor,\n"
  391. "and Metrics/Debugger (general purpose Dear ImGui debugging tool).");
  392. ImGui::SeparatorText("PROGRAMMER GUIDE:");
  393. ImGui::BulletText("See the ShowDemoWindow() code in imgui_demo.cpp. <- you are here!");
  394. ImGui::BulletText("See comments in imgui.cpp.");
  395. ImGui::BulletText("See example applications in the examples/ folder.");
  396. ImGui::BulletText("Read the FAQ at https://www.dearimgui.com/faq/");
  397. ImGui::BulletText("Set 'io.ConfigFlags |= NavEnableKeyboard' for keyboard controls.");
  398. ImGui::BulletText("Set 'io.ConfigFlags |= NavEnableGamepad' for gamepad controls.");
  399. ImGui::SeparatorText("USER GUIDE:");
  400. ImGui::ShowUserGuide();
  401. }
  402. IMGUI_DEMO_MARKER("Configuration");
  403. if (ImGui::CollapsingHeader("Configuration"))
  404. {
  405. ImGuiIO& io = ImGui::GetIO();
  406. if (ImGui::TreeNode("Configuration##2"))
  407. {
  408. ImGui::SeparatorText("General");
  409. ImGui::CheckboxFlags("io.ConfigFlags: NavEnableKeyboard", &io.ConfigFlags, ImGuiConfigFlags_NavEnableKeyboard);
  410. ImGui::SameLine(); HelpMarker("Enable keyboard controls.");
  411. ImGui::CheckboxFlags("io.ConfigFlags: NavEnableGamepad", &io.ConfigFlags, ImGuiConfigFlags_NavEnableGamepad);
  412. ImGui::SameLine(); HelpMarker("Enable gamepad controls. Require backend to set io.BackendFlags |= ImGuiBackendFlags_HasGamepad.\n\nRead instructions in imgui.cpp for details.");
  413. ImGui::CheckboxFlags("io.ConfigFlags: NavEnableSetMousePos", &io.ConfigFlags, ImGuiConfigFlags_NavEnableSetMousePos);
  414. ImGui::SameLine(); HelpMarker("Instruct navigation to move the mouse cursor. See comment for ImGuiConfigFlags_NavEnableSetMousePos.");
  415. ImGui::CheckboxFlags("io.ConfigFlags: NoMouse", &io.ConfigFlags, ImGuiConfigFlags_NoMouse);
  416. if (io.ConfigFlags & ImGuiConfigFlags_NoMouse)
  417. {
  418. // The "NoMouse" option can get us stuck with a disabled mouse! Let's provide an alternative way to fix it:
  419. if (fmodf((float)ImGui::GetTime(), 0.40f) < 0.20f)
  420. {
  421. ImGui::SameLine();
  422. ImGui::Text("<<PRESS SPACE TO DISABLE>>");
  423. }
  424. if (ImGui::IsKeyPressed(ImGuiKey_Space))
  425. io.ConfigFlags &= ~ImGuiConfigFlags_NoMouse;
  426. }
  427. ImGui::CheckboxFlags("io.ConfigFlags: NoMouseCursorChange", &io.ConfigFlags, ImGuiConfigFlags_NoMouseCursorChange);
  428. ImGui::SameLine(); HelpMarker("Instruct backend to not alter mouse cursor shape and visibility.");
  429. ImGui::Checkbox("io.ConfigInputTrickleEventQueue", &io.ConfigInputTrickleEventQueue);
  430. ImGui::SameLine(); HelpMarker("Enable input queue trickling: some types of events submitted during the same frame (e.g. button down + up) will be spread over multiple frames, improving interactions with low framerates.");
  431. ImGui::Checkbox("io.MouseDrawCursor", &io.MouseDrawCursor);
  432. ImGui::SameLine(); HelpMarker("Instruct Dear ImGui to render a mouse cursor itself. Note that a mouse cursor rendered via your application GPU rendering path will feel more laggy than hardware cursor, but will be more in sync with your other visuals.\n\nSome desktop applications may use both kinds of cursors (e.g. enable software cursor only when resizing/dragging something).");
  433. ImGui::SeparatorText("Widgets");
  434. ImGui::Checkbox("io.ConfigInputTextCursorBlink", &io.ConfigInputTextCursorBlink);
  435. ImGui::SameLine(); HelpMarker("Enable blinking cursor (optional as some users consider it to be distracting).");
  436. ImGui::Checkbox("io.ConfigInputTextEnterKeepActive", &io.ConfigInputTextEnterKeepActive);
  437. ImGui::SameLine(); HelpMarker("Pressing Enter will keep item active and select contents (single-line only).");
  438. ImGui::Checkbox("io.ConfigDragClickToInputText", &io.ConfigDragClickToInputText);
  439. ImGui::SameLine(); HelpMarker("Enable turning DragXXX widgets into text input with a simple mouse click-release (without moving).");
  440. ImGui::Checkbox("io.ConfigWindowsResizeFromEdges", &io.ConfigWindowsResizeFromEdges);
  441. ImGui::SameLine(); HelpMarker("Enable resizing of windows from their edges and from the lower-left corner.\nThis requires (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) because it needs mouse cursor feedback.");
  442. ImGui::Checkbox("io.ConfigWindowsMoveFromTitleBarOnly", &io.ConfigWindowsMoveFromTitleBarOnly);
  443. ImGui::Checkbox("io.ConfigMacOSXBehaviors", &io.ConfigMacOSXBehaviors);
  444. ImGui::SameLine(); HelpMarker("Swap Cmd<>Ctrl keys, enable various MacOS style behaviors.");
  445. ImGui::Text("Also see Style->Rendering for rendering options.");
  446. ImGui::SeparatorText("Debug");
  447. ImGui::Checkbox("io.ConfigDebugIsDebuggerPresent", &io.ConfigDebugIsDebuggerPresent);
  448. ImGui::SameLine(); HelpMarker("Enable various tools calling IM_DEBUG_BREAK().\n\nRequires a debugger being attached, otherwise IM_DEBUG_BREAK() options will appear to crash your application.");
  449. ImGui::BeginDisabled();
  450. ImGui::Checkbox("io.ConfigDebugBeginReturnValueOnce", &io.ConfigDebugBeginReturnValueOnce); // .
  451. ImGui::EndDisabled();
  452. ImGui::SameLine(); HelpMarker("First calls to Begin()/BeginChild() will return false.\n\nTHIS OPTION IS DISABLED because it needs to be set at application boot-time to make sense. Showing the disabled option is a way to make this feature easier to discover.");
  453. ImGui::Checkbox("io.ConfigDebugBeginReturnValueLoop", &io.ConfigDebugBeginReturnValueLoop);
  454. ImGui::SameLine(); HelpMarker("Some calls to Begin()/BeginChild() will return false.\n\nWill cycle through window depths then repeat. Windows should be flickering while running.");
  455. ImGui::Checkbox("io.ConfigDebugIgnoreFocusLoss", &io.ConfigDebugIgnoreFocusLoss);
  456. ImGui::SameLine(); HelpMarker("Option to deactivate io.AddFocusEvent(false) handling. May facilitate interactions with a debugger when focus loss leads to clearing inputs data.");
  457. ImGui::Checkbox("io.ConfigDebugIniSettings", &io.ConfigDebugIniSettings);
  458. ImGui::SameLine(); HelpMarker("Option to save .ini data with extra comments (particularly helpful for Docking, but makes saving slower).");
  459. ImGui::TreePop();
  460. ImGui::Spacing();
  461. }
  462. IMGUI_DEMO_MARKER("Configuration/Backend Flags");
  463. if (ImGui::TreeNode("Backend Flags"))
  464. {
  465. HelpMarker(
  466. "Those flags are set by the backends (imgui_impl_xxx files) to specify their capabilities.\n"
  467. "Here we expose them as read-only fields to avoid breaking interactions with your backend.");
  468. // FIXME: Maybe we need a BeginReadonly() equivalent to keep label bright?
  469. ImGui::BeginDisabled();
  470. ImGui::CheckboxFlags("io.BackendFlags: HasGamepad", &io.BackendFlags, ImGuiBackendFlags_HasGamepad);
  471. ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", &io.BackendFlags, ImGuiBackendFlags_HasMouseCursors);
  472. ImGui::CheckboxFlags("io.BackendFlags: HasSetMousePos", &io.BackendFlags, ImGuiBackendFlags_HasSetMousePos);
  473. ImGui::CheckboxFlags("io.BackendFlags: RendererHasVtxOffset", &io.BackendFlags, ImGuiBackendFlags_RendererHasVtxOffset);
  474. ImGui::EndDisabled();
  475. ImGui::TreePop();
  476. ImGui::Spacing();
  477. }
  478. IMGUI_DEMO_MARKER("Configuration/Style");
  479. if (ImGui::TreeNode("Style"))
  480. {
  481. HelpMarker("The same contents can be accessed in 'Tools->Style Editor' or by calling the ShowStyleEditor() function.");
  482. ImGui::ShowStyleEditor();
  483. ImGui::TreePop();
  484. ImGui::Spacing();
  485. }
  486. IMGUI_DEMO_MARKER("Configuration/Capture, Logging");
  487. if (ImGui::TreeNode("Capture/Logging"))
  488. {
  489. HelpMarker(
  490. "The logging API redirects all text output so you can easily capture the content of "
  491. "a window or a block. Tree nodes can be automatically expanded.\n"
  492. "Try opening any of the contents below in this window and then click one of the \"Log To\" button.");
  493. ImGui::LogButtons();
  494. HelpMarker("You can also call ImGui::LogText() to output directly to the log without a visual output.");
  495. if (ImGui::Button("Copy \"Hello, world!\" to clipboard"))
  496. {
  497. ImGui::LogToClipboard();
  498. ImGui::LogText("Hello, world!");
  499. ImGui::LogFinish();
  500. }
  501. ImGui::TreePop();
  502. }
  503. }
  504. IMGUI_DEMO_MARKER("Window options");
  505. if (ImGui::CollapsingHeader("Window options"))
  506. {
  507. if (ImGui::BeginTable("split", 3))
  508. {
  509. ImGui::TableNextColumn(); ImGui::Checkbox("No titlebar", &no_titlebar);
  510. ImGui::TableNextColumn(); ImGui::Checkbox("No scrollbar", &no_scrollbar);
  511. ImGui::TableNextColumn(); ImGui::Checkbox("No menu", &no_menu);
  512. ImGui::TableNextColumn(); ImGui::Checkbox("No move", &no_move);
  513. ImGui::TableNextColumn(); ImGui::Checkbox("No resize", &no_resize);
  514. ImGui::TableNextColumn(); ImGui::Checkbox("No collapse", &no_collapse);
  515. ImGui::TableNextColumn(); ImGui::Checkbox("No close", &no_close);
  516. ImGui::TableNextColumn(); ImGui::Checkbox("No nav", &no_nav);
  517. ImGui::TableNextColumn(); ImGui::Checkbox("No background", &no_background);
  518. ImGui::TableNextColumn(); ImGui::Checkbox("No bring to front", &no_bring_to_front);
  519. ImGui::TableNextColumn(); ImGui::Checkbox("Unsaved document", &unsaved_document);
  520. ImGui::EndTable();
  521. }
  522. }
  523. // All demo contents
  524. ShowDemoWindowWidgets();
  525. ShowDemoWindowLayout();
  526. ShowDemoWindowPopups();
  527. ShowDemoWindowTables();
  528. ShowDemoWindowInputs();
  529. // End of ShowDemoWindow()
  530. ImGui::PopItemWidth();
  531. ImGui::End();
  532. }
  533. static void ShowDemoWindowWidgets()
  534. {
  535. IMGUI_DEMO_MARKER("Widgets");
  536. if (!ImGui::CollapsingHeader("Widgets"))
  537. return;
  538. static bool disable_all = false; // The Checkbox for that is inside the "Disabled" section at the bottom
  539. if (disable_all)
  540. ImGui::BeginDisabled();
  541. IMGUI_DEMO_MARKER("Widgets/Basic");
  542. if (ImGui::TreeNode("Basic"))
  543. {
  544. ImGui::SeparatorText("General");
  545. IMGUI_DEMO_MARKER("Widgets/Basic/Button");
  546. static int clicked = 0;
  547. if (ImGui::Button("Button"))
  548. clicked++;
  549. if (clicked & 1)
  550. {
  551. ImGui::SameLine();
  552. ImGui::Text("Thanks for clicking me!");
  553. }
  554. IMGUI_DEMO_MARKER("Widgets/Basic/Checkbox");
  555. static bool check = true;
  556. ImGui::Checkbox("checkbox", &check);
  557. IMGUI_DEMO_MARKER("Widgets/Basic/RadioButton");
  558. static int e = 0;
  559. ImGui::RadioButton("radio a", &e, 0); ImGui::SameLine();
  560. ImGui::RadioButton("radio b", &e, 1); ImGui::SameLine();
  561. ImGui::RadioButton("radio c", &e, 2);
  562. // Color buttons, demonstrate using PushID() to add unique identifier in the ID stack, and changing style.
  563. IMGUI_DEMO_MARKER("Widgets/Basic/Buttons (Colored)");
  564. for (int i = 0; i < 7; i++)
  565. {
  566. if (i > 0)
  567. ImGui::SameLine();
  568. ImGui::PushID(i);
  569. ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(i / 7.0f, 0.6f, 0.6f));
  570. ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV(i / 7.0f, 0.7f, 0.7f));
  571. ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(i / 7.0f, 0.8f, 0.8f));
  572. ImGui::Button("Click");
  573. ImGui::PopStyleColor(3);
  574. ImGui::PopID();
  575. }
  576. // Use AlignTextToFramePadding() to align text baseline to the baseline of framed widgets elements
  577. // (otherwise a Text+SameLine+Button sequence will have the text a little too high by default!)
  578. // See 'Demo->Layout->Text Baseline Alignment' for details.
  579. ImGui::AlignTextToFramePadding();
  580. ImGui::Text("Hold to repeat:");
  581. ImGui::SameLine();
  582. // Arrow buttons with Repeater
  583. IMGUI_DEMO_MARKER("Widgets/Basic/Buttons (Repeating)");
  584. static int counter = 0;
  585. float spacing = ImGui::GetStyle().ItemInnerSpacing.x;
  586. ImGui::PushButtonRepeat(true);
  587. if (ImGui::ArrowButton("##left", ImGuiDir_Left)) { counter--; }
  588. ImGui::SameLine(0.0f, spacing);
  589. if (ImGui::ArrowButton("##right", ImGuiDir_Right)) { counter++; }
  590. ImGui::PopButtonRepeat();
  591. ImGui::SameLine();
  592. ImGui::Text("%d", counter);
  593. ImGui::Button("Tooltip");
  594. ImGui::SetItemTooltip("I am a tooltip");
  595. ImGui::LabelText("label", "Value");
  596. ImGui::SeparatorText("Inputs");
  597. {
  598. // To wire InputText() with std::string or any other custom string type,
  599. // see the "Text Input > Resize Callback" section of this demo, and the misc/cpp/imgui_stdlib.h file.
  600. IMGUI_DEMO_MARKER("Widgets/Basic/InputText");
  601. static char str0[128] = "Hello, world!";
  602. ImGui::InputText("input text", str0, IM_ARRAYSIZE(str0));
  603. ImGui::SameLine(); HelpMarker(
  604. "USER:\n"
  605. "Hold SHIFT or use mouse to select text.\n"
  606. "CTRL+Left/Right to word jump.\n"
  607. "CTRL+A or Double-Click to select all.\n"
  608. "CTRL+X,CTRL+C,CTRL+V clipboard.\n"
  609. "CTRL+Z,CTRL+Y undo/redo.\n"
  610. "ESCAPE to revert.\n\n"
  611. "PROGRAMMER:\n"
  612. "You can use the ImGuiInputTextFlags_CallbackResize facility if you need to wire InputText() "
  613. "to a dynamic string type. See misc/cpp/imgui_stdlib.h for an example (this is not demonstrated "
  614. "in imgui_demo.cpp).");
  615. static char str1[128] = "";
  616. ImGui::InputTextWithHint("input text (w/ hint)", "enter text here", str1, IM_ARRAYSIZE(str1));
  617. IMGUI_DEMO_MARKER("Widgets/Basic/InputInt, InputFloat");
  618. static int i0 = 123;
  619. ImGui::InputInt("input int", &i0);
  620. static float f0 = 0.001f;
  621. ImGui::InputFloat("input float", &f0, 0.01f, 1.0f, "%.3f");
  622. static double d0 = 999999.00000001;
  623. ImGui::InputDouble("input double", &d0, 0.01f, 1.0f, "%.8f");
  624. static float f1 = 1.e10f;
  625. ImGui::InputFloat("input scientific", &f1, 0.0f, 0.0f, "%e");
  626. ImGui::SameLine(); HelpMarker(
  627. "You can input value using the scientific notation,\n"
  628. " e.g. \"1e+8\" becomes \"100000000\".");
  629. static float vec4a[4] = { 0.10f, 0.20f, 0.30f, 0.44f };
  630. ImGui::InputFloat3("input float3", vec4a);
  631. }
  632. ImGui::SeparatorText("Drags");
  633. {
  634. IMGUI_DEMO_MARKER("Widgets/Basic/DragInt, DragFloat");
  635. static int i1 = 50, i2 = 42;
  636. ImGui::DragInt("drag int", &i1, 1);
  637. ImGui::SameLine(); HelpMarker(
  638. "Click and drag to edit value.\n"
  639. "Hold SHIFT/ALT for faster/slower edit.\n"
  640. "Double-click or CTRL+click to input value.");
  641. ImGui::DragInt("drag int 0..100", &i2, 1, 0, 100, "%d%%", ImGuiSliderFlags_AlwaysClamp);
  642. static float f1 = 1.00f, f2 = 0.0067f;
  643. ImGui::DragFloat("drag float", &f1, 0.005f);
  644. ImGui::DragFloat("drag small float", &f2, 0.0001f, 0.0f, 0.0f, "%.06f ns");
  645. }
  646. ImGui::SeparatorText("Sliders");
  647. {
  648. IMGUI_DEMO_MARKER("Widgets/Basic/SliderInt, SliderFloat");
  649. static int i1 = 0;
  650. ImGui::SliderInt("slider int", &i1, -1, 3);
  651. ImGui::SameLine(); HelpMarker("CTRL+click to input value.");
  652. static float f1 = 0.123f, f2 = 0.0f;
  653. ImGui::SliderFloat("slider float", &f1, 0.0f, 1.0f, "ratio = %.3f");
  654. ImGui::SliderFloat("slider float (log)", &f2, -10.0f, 10.0f, "%.4f", ImGuiSliderFlags_Logarithmic);
  655. IMGUI_DEMO_MARKER("Widgets/Basic/SliderAngle");
  656. static float angle = 0.0f;
  657. ImGui::SliderAngle("slider angle", &angle);
  658. // Using the format string to display a name instead of an integer.
  659. // Here we completely omit '%d' from the format string, so it'll only display a name.
  660. // This technique can also be used with DragInt().
  661. IMGUI_DEMO_MARKER("Widgets/Basic/Slider (enum)");
  662. enum Element { Element_Fire, Element_Earth, Element_Air, Element_Water, Element_COUNT };
  663. static int elem = Element_Fire;
  664. const char* elems_names[Element_COUNT] = { "Fire", "Earth", "Air", "Water" };
  665. const char* elem_name = (elem >= 0 && elem < Element_COUNT) ? elems_names[elem] : "Unknown";
  666. ImGui::SliderInt("slider enum", &elem, 0, Element_COUNT - 1, elem_name); // Use ImGuiSliderFlags_NoInput flag to disable CTRL+Click here.
  667. ImGui::SameLine(); HelpMarker("Using the format string parameter to display a name instead of the underlying integer.");
  668. }
  669. ImGui::SeparatorText("Selectors/Pickers");
  670. {
  671. IMGUI_DEMO_MARKER("Widgets/Basic/ColorEdit3, ColorEdit4");
  672. static float col1[3] = { 1.0f, 0.0f, 0.2f };
  673. static float col2[4] = { 0.4f, 0.7f, 0.0f, 0.5f };
  674. ImGui::ColorEdit3("color 1", col1);
  675. ImGui::SameLine(); HelpMarker(
  676. "Click on the color square to open a color picker.\n"
  677. "Click and hold to use drag and drop.\n"
  678. "Right-click on the color square to show options.\n"
  679. "CTRL+click on individual component to input value.\n");
  680. ImGui::ColorEdit4("color 2", col2);
  681. }
  682. {
  683. // Using the _simplified_ one-liner Combo() api here
  684. // See "Combo" section for examples of how to use the more flexible BeginCombo()/EndCombo() api.
  685. IMGUI_DEMO_MARKER("Widgets/Basic/Combo");
  686. const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIIIIII", "JJJJ", "KKKKKKK" };
  687. static int item_current = 0;
  688. ImGui::Combo("combo", &item_current, items, IM_ARRAYSIZE(items));
  689. ImGui::SameLine(); HelpMarker(
  690. "Using the simplified one-liner Combo API here.\n"
  691. "Refer to the \"Combo\" section below for an explanation of how to use the more flexible and general BeginCombo/EndCombo API.");
  692. }
  693. {
  694. // Using the _simplified_ one-liner ListBox() api here
  695. // See "List boxes" section for examples of how to use the more flexible BeginListBox()/EndListBox() api.
  696. IMGUI_DEMO_MARKER("Widgets/Basic/ListBox");
  697. const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi", "Mango", "Orange", "Pineapple", "Strawberry", "Watermelon" };
  698. static int item_current = 1;
  699. ImGui::ListBox("listbox", &item_current, items, IM_ARRAYSIZE(items), 4);
  700. ImGui::SameLine(); HelpMarker(
  701. "Using the simplified one-liner ListBox API here.\n"
  702. "Refer to the \"List boxes\" section below for an explanation of how to use the more flexible and general BeginListBox/EndListBox API.");
  703. }
  704. ImGui::TreePop();
  705. }
  706. IMGUI_DEMO_MARKER("Widgets/Tooltips");
  707. if (ImGui::TreeNode("Tooltips"))
  708. {
  709. // Tooltips are windows following the mouse. They do not take focus away.
  710. ImGui::SeparatorText("General");
  711. // Typical use cases:
  712. // - Short-form (text only): SetItemTooltip("Hello");
  713. // - Short-form (any contents): if (BeginItemTooltip()) { Text("Hello"); EndTooltip(); }
  714. // - Full-form (text only): if (IsItemHovered(...)) { SetTooltip("Hello"); }
  715. // - Full-form (any contents): if (IsItemHovered(...) && BeginTooltip()) { Text("Hello"); EndTooltip(); }
  716. HelpMarker(
  717. "Tooltip are typically created by using a IsItemHovered() + SetTooltip() sequence.\n\n"
  718. "We provide a helper SetItemTooltip() function to perform the two with standards flags.");
  719. ImVec2 sz = ImVec2(-FLT_MIN, 0.0f);
  720. ImGui::Button("Basic", sz);
  721. ImGui::SetItemTooltip("I am a tooltip");
  722. ImGui::Button("Fancy", sz);
  723. if (ImGui::BeginItemTooltip())
  724. {
  725. ImGui::Text("I am a fancy tooltip");
  726. static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f };
  727. ImGui::PlotLines("Curve", arr, IM_ARRAYSIZE(arr));
  728. ImGui::Text("Sin(time) = %f", sinf((float)ImGui::GetTime()));
  729. ImGui::EndTooltip();
  730. }
  731. ImGui::SeparatorText("Always On");
  732. // Showcase NOT relying on a IsItemHovered() to emit a tooltip.
  733. // Here the tooltip is always emitted when 'always_on == true'.
  734. static int always_on = 0;
  735. ImGui::RadioButton("Off", &always_on, 0);
  736. ImGui::SameLine();
  737. ImGui::RadioButton("Always On (Simple)", &always_on, 1);
  738. ImGui::SameLine();
  739. ImGui::RadioButton("Always On (Advanced)", &always_on, 2);
  740. if (always_on == 1)
  741. ImGui::SetTooltip("I am following you around.");
  742. else if (always_on == 2 && ImGui::BeginTooltip())
  743. {
  744. ImGui::ProgressBar(sinf((float)ImGui::GetTime()) * 0.5f + 0.5f, ImVec2(ImGui::GetFontSize() * 25, 0.0f));
  745. ImGui::EndTooltip();
  746. }
  747. ImGui::SeparatorText("Custom");
  748. HelpMarker(
  749. "Passing ImGuiHoveredFlags_ForTooltip to IsItemHovered() is the preferred way to standardize"
  750. "tooltip activation details across your application. You may however decide to use custom"
  751. "flags for a specific tooltip instance.");
  752. // The following examples are passed for documentation purpose but may not be useful to most users.
  753. // Passing ImGuiHoveredFlags_ForTooltip to IsItemHovered() will pull ImGuiHoveredFlags flags values from
  754. // 'style.HoverFlagsForTooltipMouse' or 'style.HoverFlagsForTooltipNav' depending on whether mouse or gamepad/keyboard is being used.
  755. // With default settings, ImGuiHoveredFlags_ForTooltip is equivalent to ImGuiHoveredFlags_DelayShort + ImGuiHoveredFlags_Stationary.
  756. ImGui::Button("Manual", sz);
  757. if (ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip))
  758. ImGui::SetTooltip("I am a manually emitted tooltip.");
  759. ImGui::Button("DelayNone", sz);
  760. if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNone))
  761. ImGui::SetTooltip("I am a tooltip with no delay.");
  762. ImGui::Button("DelayShort", sz);
  763. if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort | ImGuiHoveredFlags_NoSharedDelay))
  764. ImGui::SetTooltip("I am a tooltip with a short delay (%0.2f sec).", ImGui::GetStyle().HoverDelayShort);
  765. ImGui::Button("DelayLong", sz);
  766. if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNormal | ImGuiHoveredFlags_NoSharedDelay))
  767. ImGui::SetTooltip("I am a tooltip with a long delay (%0.2f sec).", ImGui::GetStyle().HoverDelayNormal);
  768. ImGui::Button("Stationary", sz);
  769. if (ImGui::IsItemHovered(ImGuiHoveredFlags_Stationary))
  770. ImGui::SetTooltip("I am a tooltip requiring mouse to be stationary before activating.");
  771. // Using ImGuiHoveredFlags_ForTooltip will pull flags from 'style.HoverFlagsForTooltipMouse' or 'style.HoverFlagsForTooltipNav',
  772. // which default value include the ImGuiHoveredFlags_AllowWhenDisabled flag.
  773. // As a result, Set
  774. ImGui::BeginDisabled();
  775. ImGui::Button("Disabled item", sz);
  776. ImGui::EndDisabled();
  777. if (ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip))
  778. ImGui::SetTooltip("I am a a tooltip for a disabled item.");
  779. ImGui::TreePop();
  780. }
  781. // Testing ImGuiOnceUponAFrame helper.
  782. //static ImGuiOnceUponAFrame once;
  783. //for (int i = 0; i < 5; i++)
  784. // if (once)
  785. // ImGui::Text("This will be displayed only once.");
  786. IMGUI_DEMO_MARKER("Widgets/Tree Nodes");
  787. if (ImGui::TreeNode("Tree Nodes"))
  788. {
  789. IMGUI_DEMO_MARKER("Widgets/Tree Nodes/Basic trees");
  790. if (ImGui::TreeNode("Basic trees"))
  791. {
  792. for (int i = 0; i < 5; i++)
  793. {
  794. // Use SetNextItemOpen() so set the default state of a node to be open. We could
  795. // also use TreeNodeEx() with the ImGuiTreeNodeFlags_DefaultOpen flag to achieve the same thing!
  796. if (i == 0)
  797. ImGui::SetNextItemOpen(true, ImGuiCond_Once);
  798. // Here we use PushID() to generate a unique base ID, and then the "" used as TreeNode id won't conflict.
  799. // An alternative to using 'PushID() + TreeNode("", ...)' to generate a unique ID is to use 'TreeNode((void*)(intptr_t)i, ...)',
  800. // aka generate a dummy pointer-sized value to be hashed. The demo below uses that technique. Both are fine.
  801. ImGui::PushID(i);
  802. if (ImGui::TreeNode("", "Child %d", i))
  803. {
  804. ImGui::Text("blah blah");
  805. ImGui::SameLine();
  806. if (ImGui::SmallButton("button")) {}
  807. ImGui::TreePop();
  808. }
  809. ImGui::PopID();
  810. }
  811. ImGui::TreePop();
  812. }
  813. IMGUI_DEMO_MARKER("Widgets/Tree Nodes/Advanced, with Selectable nodes");
  814. if (ImGui::TreeNode("Advanced, with Selectable nodes"))
  815. {
  816. HelpMarker(
  817. "This is a more typical looking tree with selectable nodes.\n"
  818. "Click to select, CTRL+Click to toggle, click on arrows or double-click to open.");
  819. static ImGuiTreeNodeFlags base_flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_SpanAvailWidth;
  820. static bool align_label_with_current_x_position = false;
  821. static bool test_drag_and_drop = false;
  822. ImGui::CheckboxFlags("ImGuiTreeNodeFlags_OpenOnArrow", &base_flags, ImGuiTreeNodeFlags_OpenOnArrow);
  823. ImGui::CheckboxFlags("ImGuiTreeNodeFlags_OpenOnDoubleClick", &base_flags, ImGuiTreeNodeFlags_OpenOnDoubleClick);
  824. ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAvailWidth", &base_flags, ImGuiTreeNodeFlags_SpanAvailWidth); ImGui::SameLine(); HelpMarker("Extend hit area to all available width instead of allowing more items to be laid out after the node.");
  825. ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanFullWidth", &base_flags, ImGuiTreeNodeFlags_SpanFullWidth);
  826. ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanTextWidth", &base_flags, ImGuiTreeNodeFlags_SpanTextWidth); ImGui::SameLine(); HelpMarker("Reduce hit area to the text label and a bit of margin.");
  827. ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAllColumns", &base_flags, ImGuiTreeNodeFlags_SpanAllColumns); ImGui::SameLine(); HelpMarker("For use in Tables only.");
  828. ImGui::CheckboxFlags("ImGuiTreeNodeFlags_AllowOverlap", &base_flags, ImGuiTreeNodeFlags_AllowOverlap);
  829. ImGui::CheckboxFlags("ImGuiTreeNodeFlags_Framed", &base_flags, ImGuiTreeNodeFlags_Framed); ImGui::SameLine(); HelpMarker("Draw frame with background (e.g. for CollapsingHeader)");
  830. ImGui::Checkbox("Align label with current X position", &align_label_with_current_x_position);
  831. ImGui::Checkbox("Test tree node as drag source", &test_drag_and_drop);
  832. ImGui::Text("Hello!");
  833. if (align_label_with_current_x_position)
  834. ImGui::Unindent(ImGui::GetTreeNodeToLabelSpacing());
  835. // 'selection_mask' is dumb representation of what may be user-side selection state.
  836. // You may retain selection state inside or outside your objects in whatever format you see fit.
  837. // 'node_clicked' is temporary storage of what node we have clicked to process selection at the end
  838. /// of the loop. May be a pointer to your own node type, etc.
  839. static int selection_mask = (1 << 2);
  840. int node_clicked = -1;
  841. for (int i = 0; i < 6; i++)
  842. {
  843. // Disable the default "open on single-click behavior" + set Selected flag according to our selection.
  844. // To alter selection we use IsItemClicked() && !IsItemToggledOpen(), so clicking on an arrow doesn't alter selection.
  845. ImGuiTreeNodeFlags node_flags = base_flags;
  846. const bool is_selected = (selection_mask & (1 << i)) != 0;
  847. if (is_selected)
  848. node_flags |= ImGuiTreeNodeFlags_Selected;
  849. if (i < 3)
  850. {
  851. // Items 0..2 are Tree Node
  852. bool node_open = ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags, "Selectable Node %d", i);
  853. if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen())
  854. node_clicked = i;
  855. if (test_drag_and_drop && ImGui::BeginDragDropSource())
  856. {
  857. ImGui::SetDragDropPayload("_TREENODE", NULL, 0);
  858. ImGui::Text("This is a drag and drop source");
  859. ImGui::EndDragDropSource();
  860. }
  861. if (i == 2)
  862. {
  863. // Item 2 has an additional inline button to help demonstrate SpanTextWidth.
  864. ImGui::SameLine();
  865. if (ImGui::SmallButton("button")) {}
  866. }
  867. if (node_open)
  868. {
  869. ImGui::BulletText("Blah blah\nBlah Blah");
  870. ImGui::TreePop();
  871. }
  872. }
  873. else
  874. {
  875. // Items 3..5 are Tree Leaves
  876. // The only reason we use TreeNode at all is to allow selection of the leaf. Otherwise we can
  877. // use BulletText() or advance the cursor by GetTreeNodeToLabelSpacing() and call Text().
  878. node_flags |= ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen; // ImGuiTreeNodeFlags_Bullet
  879. ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags, "Selectable Leaf %d", i);
  880. if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen())
  881. node_clicked = i;
  882. if (test_drag_and_drop && ImGui::BeginDragDropSource())
  883. {
  884. ImGui::SetDragDropPayload("_TREENODE", NULL, 0);
  885. ImGui::Text("This is a drag and drop source");
  886. ImGui::EndDragDropSource();
  887. }
  888. }
  889. }
  890. if (node_clicked != -1)
  891. {
  892. // Update selection state
  893. // (process outside of tree loop to avoid visual inconsistencies during the clicking frame)
  894. if (ImGui::GetIO().KeyCtrl)
  895. selection_mask ^= (1 << node_clicked); // CTRL+click to toggle
  896. else //if (!(selection_mask & (1 << node_clicked))) // Depending on selection behavior you want, may want to preserve selection when clicking on item that is part of the selection
  897. selection_mask = (1 << node_clicked); // Click to single-select
  898. }
  899. if (align_label_with_current_x_position)
  900. ImGui::Indent(ImGui::GetTreeNodeToLabelSpacing());
  901. ImGui::TreePop();
  902. }
  903. ImGui::TreePop();
  904. }
  905. IMGUI_DEMO_MARKER("Widgets/Collapsing Headers");
  906. if (ImGui::TreeNode("Collapsing Headers"))
  907. {
  908. static bool closable_group = true;
  909. ImGui::Checkbox("Show 2nd header", &closable_group);
  910. if (ImGui::CollapsingHeader("Header", ImGuiTreeNodeFlags_None))
  911. {
  912. ImGui::Text("IsItemHovered: %d", ImGui::IsItemHovered());
  913. for (int i = 0; i < 5; i++)
  914. ImGui::Text("Some content %d", i);
  915. }
  916. if (ImGui::CollapsingHeader("Header with a close button", &closable_group))
  917. {
  918. ImGui::Text("IsItemHovered: %d", ImGui::IsItemHovered());
  919. for (int i = 0; i < 5; i++)
  920. ImGui::Text("More content %d", i);
  921. }
  922. /*
  923. if (ImGui::CollapsingHeader("Header with a bullet", ImGuiTreeNodeFlags_Bullet))
  924. ImGui::Text("IsItemHovered: %d", ImGui::IsItemHovered());
  925. */
  926. ImGui::TreePop();
  927. }
  928. IMGUI_DEMO_MARKER("Widgets/Bullets");
  929. if (ImGui::TreeNode("Bullets"))
  930. {
  931. ImGui::BulletText("Bullet point 1");
  932. ImGui::BulletText("Bullet point 2\nOn multiple lines");
  933. if (ImGui::TreeNode("Tree node"))
  934. {
  935. ImGui::BulletText("Another bullet point");
  936. ImGui::TreePop();
  937. }
  938. ImGui::Bullet(); ImGui::Text("Bullet point 3 (two calls)");
  939. ImGui::Bullet(); ImGui::SmallButton("Button");
  940. ImGui::TreePop();
  941. }
  942. IMGUI_DEMO_MARKER("Widgets/Text");
  943. if (ImGui::TreeNode("Text"))
  944. {
  945. IMGUI_DEMO_MARKER("Widgets/Text/Colored Text");
  946. if (ImGui::TreeNode("Colorful Text"))
  947. {
  948. // Using shortcut. You can use PushStyleColor()/PopStyleColor() for more flexibility.
  949. ImGui::TextColored(ImVec4(1.0f, 0.0f, 1.0f, 1.0f), "Pink");
  950. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "Yellow");
  951. ImGui::TextDisabled("Disabled");
  952. ImGui::SameLine(); HelpMarker("The TextDisabled color is stored in ImGuiStyle.");
  953. ImGui::TreePop();
  954. }
  955. IMGUI_DEMO_MARKER("Widgets/Text/Word Wrapping");
  956. if (ImGui::TreeNode("Word Wrapping"))
  957. {
  958. // Using shortcut. You can use PushTextWrapPos()/PopTextWrapPos() for more flexibility.
  959. ImGui::TextWrapped(
  960. "This text should automatically wrap on the edge of the window. The current implementation "
  961. "for text wrapping follows simple rules suitable for English and possibly other languages.");
  962. ImGui::Spacing();
  963. static float wrap_width = 200.0f;
  964. ImGui::SliderFloat("Wrap width", &wrap_width, -20, 600, "%.0f");
  965. ImDrawList* draw_list = ImGui::GetWindowDrawList();
  966. for (int n = 0; n < 2; n++)
  967. {
  968. ImGui::Text("Test paragraph %d:", n);
  969. ImVec2 pos = ImGui::GetCursorScreenPos();
  970. ImVec2 marker_min = ImVec2(pos.x + wrap_width, pos.y);
  971. ImVec2 marker_max = ImVec2(pos.x + wrap_width + 10, pos.y + ImGui::GetTextLineHeight());
  972. ImGui::PushTextWrapPos(ImGui::GetCursorPos().x + wrap_width);
  973. if (n == 0)
  974. ImGui::Text("The lazy dog is a good dog. This paragraph should fit within %.0f pixels. Testing a 1 character word. The quick brown fox jumps over the lazy dog.", wrap_width);
  975. else
  976. ImGui::Text("aaaaaaaa bbbbbbbb, c cccccccc,dddddddd. d eeeeeeee ffffffff. gggggggg!hhhhhhhh");
  977. // Draw actual text bounding box, following by marker of our expected limit (should not overlap!)
  978. draw_list->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(), IM_COL32(255, 255, 0, 255));
  979. draw_list->AddRectFilled(marker_min, marker_max, IM_COL32(255, 0, 255, 255));
  980. ImGui::PopTextWrapPos();
  981. }
  982. ImGui::TreePop();
  983. }
  984. IMGUI_DEMO_MARKER("Widgets/Text/UTF-8 Text");
  985. if (ImGui::TreeNode("UTF-8 Text"))
  986. {
  987. // UTF-8 test with Japanese characters
  988. // (Needs a suitable font? Try "Google Noto" or "Arial Unicode". See docs/FONTS.md for details.)
  989. // - From C++11 you can use the u8"my text" syntax to encode literal strings as UTF-8
  990. // - For earlier compiler, you may be able to encode your sources as UTF-8 (e.g. in Visual Studio, you
  991. // can save your source files as 'UTF-8 without signature').
  992. // - FOR THIS DEMO FILE ONLY, BECAUSE WE WANT TO SUPPORT OLD COMPILERS, WE ARE *NOT* INCLUDING RAW UTF-8
  993. // CHARACTERS IN THIS SOURCE FILE. Instead we are encoding a few strings with hexadecimal constants.
  994. // Don't do this in your application! Please use u8"text in any language" in your application!
  995. // Note that characters values are preserved even by InputText() if the font cannot be displayed,
  996. // so you can safely copy & paste garbled characters into another application.
  997. ImGui::TextWrapped(
  998. "CJK text will only appear if the font was loaded with the appropriate CJK character ranges. "
  999. "Call io.Fonts->AddFontFromFileTTF() manually to load extra character ranges. "
  1000. "Read docs/FONTS.md for details.");
  1001. ImGui::Text("Hiragana: \xe3\x81\x8b\xe3\x81\x8d\xe3\x81\x8f\xe3\x81\x91\xe3\x81\x93 (kakikukeko)");
  1002. ImGui::Text("Kanjis: \xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e (nihongo)");
  1003. static char buf[32] = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e";
  1004. //static char buf[32] = u8"NIHONGO"; // <- this is how you would write it with C++11, using real kanjis
  1005. ImGui::InputText("UTF-8 input", buf, IM_ARRAYSIZE(buf));
  1006. ImGui::TreePop();
  1007. }
  1008. ImGui::TreePop();
  1009. }
  1010. IMGUI_DEMO_MARKER("Widgets/Images");
  1011. if (ImGui::TreeNode("Images"))
  1012. {
  1013. ImGuiIO& io = ImGui::GetIO();
  1014. ImGui::TextWrapped(
  1015. "Below we are displaying the font texture (which is the only texture we have access to in this demo). "
  1016. "Use the 'ImTextureID' type as storage to pass pointers or identifier to your own texture data. "
  1017. "Hover the texture for a zoomed view!");
  1018. // Below we are displaying the font texture because it is the only texture we have access to inside the demo!
  1019. // Remember that ImTextureID is just storage for whatever you want it to be. It is essentially a value that
  1020. // will be passed to the rendering backend via the ImDrawCmd structure.
  1021. // If you use one of the default imgui_impl_XXXX.cpp rendering backend, they all have comments at the top
  1022. // of their respective source file to specify what they expect to be stored in ImTextureID, for example:
  1023. // - The imgui_impl_dx11.cpp renderer expect a 'ID3D11ShaderResourceView*' pointer
  1024. // - The imgui_impl_opengl3.cpp renderer expect a GLuint OpenGL texture identifier, etc.
  1025. // More:
  1026. // - If you decided that ImTextureID = MyEngineTexture*, then you can pass your MyEngineTexture* pointers
  1027. // to ImGui::Image(), and gather width/height through your own functions, etc.
  1028. // - You can use ShowMetricsWindow() to inspect the draw data that are being passed to your renderer,
  1029. // it will help you debug issues if you are confused about it.
  1030. // - Consider using the lower-level ImDrawList::AddImage() API, via ImGui::GetWindowDrawList()->AddImage().
  1031. // - Read https://github.com/ocornut/imgui/blob/master/docs/FAQ.md
  1032. // - Read https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples
  1033. ImTextureID my_tex_id = io.Fonts->TexID;
  1034. float my_tex_w = (float)io.Fonts->TexWidth;
  1035. float my_tex_h = (float)io.Fonts->TexHeight;
  1036. {
  1037. static bool use_text_color_for_tint = false;
  1038. ImGui::Checkbox("Use Text Color for Tint", &use_text_color_for_tint);
  1039. ImGui::Text("%.0fx%.0f", my_tex_w, my_tex_h);
  1040. ImVec2 pos = ImGui::GetCursorScreenPos();
  1041. ImVec2 uv_min = ImVec2(0.0f, 0.0f); // Top-left
  1042. ImVec2 uv_max = ImVec2(1.0f, 1.0f); // Lower-right
  1043. ImVec4 tint_col = use_text_color_for_tint ? ImGui::GetStyleColorVec4(ImGuiCol_Text) : ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // No tint
  1044. ImVec4 border_col = ImGui::GetStyleColorVec4(ImGuiCol_Border);
  1045. ImGui::Image(my_tex_id, ImVec2(my_tex_w, my_tex_h), uv_min, uv_max, tint_col, border_col);
  1046. if (ImGui::BeginItemTooltip())
  1047. {
  1048. float region_sz = 32.0f;
  1049. float region_x = io.MousePos.x - pos.x - region_sz * 0.5f;
  1050. float region_y = io.MousePos.y - pos.y - region_sz * 0.5f;
  1051. float zoom = 4.0f;
  1052. if (region_x < 0.0f) { region_x = 0.0f; }
  1053. else if (region_x > my_tex_w - region_sz) { region_x = my_tex_w - region_sz; }
  1054. if (region_y < 0.0f) { region_y = 0.0f; }
  1055. else if (region_y > my_tex_h - region_sz) { region_y = my_tex_h - region_sz; }
  1056. ImGui::Text("Min: (%.2f, %.2f)", region_x, region_y);
  1057. ImGui::Text("Max: (%.2f, %.2f)", region_x + region_sz, region_y + region_sz);
  1058. ImVec2 uv0 = ImVec2((region_x) / my_tex_w, (region_y) / my_tex_h);
  1059. ImVec2 uv1 = ImVec2((region_x + region_sz) / my_tex_w, (region_y + region_sz) / my_tex_h);
  1060. ImGui::Image(my_tex_id, ImVec2(region_sz * zoom, region_sz * zoom), uv0, uv1, tint_col, border_col);
  1061. ImGui::EndTooltip();
  1062. }
  1063. }
  1064. IMGUI_DEMO_MARKER("Widgets/Images/Textured buttons");
  1065. ImGui::TextWrapped("And now some textured buttons..");
  1066. static int pressed_count = 0;
  1067. for (int i = 0; i < 8; i++)
  1068. {
  1069. // UV coordinates are often (0.0f, 0.0f) and (1.0f, 1.0f) to display an entire textures.
  1070. // Here are trying to display only a 32x32 pixels area of the texture, hence the UV computation.
  1071. // Read about UV coordinates here: https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples
  1072. ImGui::PushID(i);
  1073. if (i > 0)
  1074. ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(i - 1.0f, i - 1.0f));
  1075. ImVec2 size = ImVec2(32.0f, 32.0f); // Size of the image we want to make visible
  1076. ImVec2 uv0 = ImVec2(0.0f, 0.0f); // UV coordinates for lower-left
  1077. ImVec2 uv1 = ImVec2(32.0f / my_tex_w, 32.0f / my_tex_h); // UV coordinates for (32,32) in our texture
  1078. ImVec4 bg_col = ImVec4(0.0f, 0.0f, 0.0f, 1.0f); // Black background
  1079. ImVec4 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // No tint
  1080. if (ImGui::ImageButton("", my_tex_id, size, uv0, uv1, bg_col, tint_col))
  1081. pressed_count += 1;
  1082. if (i > 0)
  1083. ImGui::PopStyleVar();
  1084. ImGui::PopID();
  1085. ImGui::SameLine();
  1086. }
  1087. ImGui::NewLine();
  1088. ImGui::Text("Pressed %d times.", pressed_count);
  1089. ImGui::TreePop();
  1090. }
  1091. IMGUI_DEMO_MARKER("Widgets/Combo");
  1092. if (ImGui::TreeNode("Combo"))
  1093. {
  1094. // Combo Boxes are also called "Dropdown" in other systems
  1095. // Expose flags as checkbox for the demo
  1096. static ImGuiComboFlags flags = 0;
  1097. ImGui::CheckboxFlags("ImGuiComboFlags_PopupAlignLeft", &flags, ImGuiComboFlags_PopupAlignLeft);
  1098. ImGui::SameLine(); HelpMarker("Only makes a difference if the popup is larger than the combo");
  1099. if (ImGui::CheckboxFlags("ImGuiComboFlags_NoArrowButton", &flags, ImGuiComboFlags_NoArrowButton))
  1100. flags &= ~ImGuiComboFlags_NoPreview; // Clear incompatible flags
  1101. if (ImGui::CheckboxFlags("ImGuiComboFlags_NoPreview", &flags, ImGuiComboFlags_NoPreview))
  1102. flags &= ~(ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_WidthFitPreview); // Clear incompatible flags
  1103. if (ImGui::CheckboxFlags("ImGuiComboFlags_WidthFitPreview", &flags, ImGuiComboFlags_WidthFitPreview))
  1104. flags &= ~ImGuiComboFlags_NoPreview;
  1105. // Override default popup height
  1106. if (ImGui::CheckboxFlags("ImGuiComboFlags_HeightSmall", &flags, ImGuiComboFlags_HeightSmall))
  1107. flags &= ~(ImGuiComboFlags_HeightMask_ & ~ImGuiComboFlags_HeightSmall);
  1108. if (ImGui::CheckboxFlags("ImGuiComboFlags_HeightRegular", &flags, ImGuiComboFlags_HeightRegular))
  1109. flags &= ~(ImGuiComboFlags_HeightMask_ & ~ImGuiComboFlags_HeightRegular);
  1110. if (ImGui::CheckboxFlags("ImGuiComboFlags_HeightLargest", &flags, ImGuiComboFlags_HeightLargest))
  1111. flags &= ~(ImGuiComboFlags_HeightMask_ & ~ImGuiComboFlags_HeightLargest);
  1112. // Using the generic BeginCombo() API, you have full control over how to display the combo contents.
  1113. // (your selection data could be an index, a pointer to the object, an id for the object, a flag intrusively
  1114. // stored in the object itself, etc.)
  1115. const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLLLLL", "MMMM", "OOOOOOO" };
  1116. static int item_current_idx = 0; // Here we store our selection data as an index.
  1117. // Pass in the preview value visible before opening the combo (it could technically be different contents or not pulled from items[])
  1118. const char* combo_preview_value = items[item_current_idx];
  1119. if (ImGui::BeginCombo("combo 1", combo_preview_value, flags))
  1120. {
  1121. for (int n = 0; n < IM_ARRAYSIZE(items); n++)
  1122. {
  1123. const bool is_selected = (item_current_idx == n);
  1124. if (ImGui::Selectable(items[n], is_selected))
  1125. item_current_idx = n;
  1126. // Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
  1127. if (is_selected)
  1128. ImGui::SetItemDefaultFocus();
  1129. }
  1130. ImGui::EndCombo();
  1131. }
  1132. ImGui::Spacing();
  1133. ImGui::SeparatorText("One-liner variants");
  1134. HelpMarker("Flags above don't apply to this section.");
  1135. // Simplified one-liner Combo() API, using values packed in a single constant string
  1136. // This is a convenience for when the selection set is small and known at compile-time.
  1137. static int item_current_2 = 0;
  1138. ImGui::Combo("combo 2 (one-liner)", &item_current_2, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0");
  1139. // Simplified one-liner Combo() using an array of const char*
  1140. // This is not very useful (may obsolete): prefer using BeginCombo()/EndCombo() for full control.
  1141. static int item_current_3 = -1; // If the selection isn't within 0..count, Combo won't display a preview
  1142. ImGui::Combo("combo 3 (array)", &item_current_3, items, IM_ARRAYSIZE(items));
  1143. // Simplified one-liner Combo() using an accessor function
  1144. static int item_current_4 = 0;
  1145. ImGui::Combo("combo 4 (function)", &item_current_4, [](void* data, int n) { return ((const char**)data)[n]; }, items, IM_ARRAYSIZE(items));
  1146. ImGui::TreePop();
  1147. }
  1148. IMGUI_DEMO_MARKER("Widgets/List Boxes");
  1149. if (ImGui::TreeNode("List boxes"))
  1150. {
  1151. // BeginListBox() is essentially a thin wrapper to using BeginChild()/EndChild()
  1152. // using the ImGuiChildFlags_FrameStyle flag for stylistic changes + displaying a label.
  1153. // You may be tempted to simply use BeginChild() directly. However note that BeginChild() requires EndChild()
  1154. // to always be called (inconsistent with BeginListBox()/EndListBox()).
  1155. // Using the generic BeginListBox() API, you have full control over how to display the combo contents.
  1156. // (your selection data could be an index, a pointer to the object, an id for the object, a flag intrusively
  1157. // stored in the object itself, etc.)
  1158. const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLLLLL", "MMMM", "OOOOOOO" };
  1159. static int item_current_idx = 0; // Here we store our selection data as an index.
  1160. if (ImGui::BeginListBox("listbox 1"))
  1161. {
  1162. for (int n = 0; n < IM_ARRAYSIZE(items); n++)
  1163. {
  1164. const bool is_selected = (item_current_idx == n);
  1165. if (ImGui::Selectable(items[n], is_selected))
  1166. item_current_idx = n;
  1167. // Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
  1168. if (is_selected)
  1169. ImGui::SetItemDefaultFocus();
  1170. }
  1171. ImGui::EndListBox();
  1172. }
  1173. ImGui::SameLine(); HelpMarker("Here we are sharing selection state between both boxes.");
  1174. // Custom size: use all width, 5 items tall
  1175. ImGui::Text("Full-width:");
  1176. if (ImGui::BeginListBox("##listbox 2", ImVec2(-FLT_MIN, 5 * ImGui::GetTextLineHeightWithSpacing())))
  1177. {
  1178. for (int n = 0; n < IM_ARRAYSIZE(items); n++)
  1179. {
  1180. const bool is_selected = (item_current_idx == n);
  1181. if (ImGui::Selectable(items[n], is_selected))
  1182. item_current_idx = n;
  1183. // Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
  1184. if (is_selected)
  1185. ImGui::SetItemDefaultFocus();
  1186. }
  1187. ImGui::EndListBox();
  1188. }
  1189. ImGui::TreePop();
  1190. }
  1191. IMGUI_DEMO_MARKER("Widgets/Selectables");
  1192. if (ImGui::TreeNode("Selectables"))
  1193. {
  1194. // Selectable() has 2 overloads:
  1195. // - The one taking "bool selected" as a read-only selection information.
  1196. // When Selectable() has been clicked it returns true and you can alter selection state accordingly.
  1197. // - The one taking "bool* p_selected" as a read-write selection information (convenient in some cases)
  1198. // The earlier is more flexible, as in real application your selection may be stored in many different ways
  1199. // and not necessarily inside a bool value (e.g. in flags within objects, as an external list, etc).
  1200. IMGUI_DEMO_MARKER("Widgets/Selectables/Basic");
  1201. if (ImGui::TreeNode("Basic"))
  1202. {
  1203. static bool selection[5] = { false, true, false, false };
  1204. ImGui::Selectable("1. I am selectable", &selection[0]);
  1205. ImGui::Selectable("2. I am selectable", &selection[1]);
  1206. ImGui::Selectable("3. I am selectable", &selection[2]);
  1207. if (ImGui::Selectable("4. I am double clickable", selection[3], ImGuiSelectableFlags_AllowDoubleClick))
  1208. if (ImGui::IsMouseDoubleClicked(0))
  1209. selection[3] = !selection[3];
  1210. ImGui::TreePop();
  1211. }
  1212. IMGUI_DEMO_MARKER("Widgets/Selectables/Single Selection");
  1213. if (ImGui::TreeNode("Selection State: Single Selection"))
  1214. {
  1215. static int selected = -1;
  1216. for (int n = 0; n < 5; n++)
  1217. {
  1218. char buf[32];
  1219. sprintf(buf, "Object %d", n);
  1220. if (ImGui::Selectable(buf, selected == n))
  1221. selected = n;
  1222. }
  1223. ImGui::TreePop();
  1224. }
  1225. IMGUI_DEMO_MARKER("Widgets/Selectables/Multiple Selection");
  1226. if (ImGui::TreeNode("Selection State: Multiple Selection"))
  1227. {
  1228. HelpMarker("Hold CTRL and click to select multiple items.");
  1229. static bool selection[5] = { false, false, false, false, false };
  1230. for (int n = 0; n < 5; n++)
  1231. {
  1232. char buf[32];
  1233. sprintf(buf, "Object %d", n);
  1234. if (ImGui::Selectable(buf, selection[n]))
  1235. {
  1236. if (!ImGui::GetIO().KeyCtrl) // Clear selection when CTRL is not held
  1237. memset(selection, 0, sizeof(selection));
  1238. selection[n] ^= 1;
  1239. }
  1240. }
  1241. ImGui::TreePop();
  1242. }
  1243. IMGUI_DEMO_MARKER("Widgets/Selectables/Rendering more items on the same line");
  1244. if (ImGui::TreeNode("Rendering more items on the same line"))
  1245. {
  1246. // (1) Using SetNextItemAllowOverlap()
  1247. // (2) Using the Selectable() override that takes "bool* p_selected" parameter, the bool value is toggled automatically.
  1248. static bool selected[3] = { false, false, false };
  1249. ImGui::SetNextItemAllowOverlap(); ImGui::Selectable("main.c", &selected[0]); ImGui::SameLine(); ImGui::SmallButton("Link 1");
  1250. ImGui::SetNextItemAllowOverlap(); ImGui::Selectable("Hello.cpp", &selected[1]); ImGui::SameLine(); ImGui::SmallButton("Link 2");
  1251. ImGui::SetNextItemAllowOverlap(); ImGui::Selectable("Hello.h", &selected[2]); ImGui::SameLine(); ImGui::SmallButton("Link 3");
  1252. ImGui::TreePop();
  1253. }
  1254. IMGUI_DEMO_MARKER("Widgets/Selectables/In columns");
  1255. if (ImGui::TreeNode("In columns"))
  1256. {
  1257. static bool selected[10] = {};
  1258. if (ImGui::BeginTable("split1", 3, ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings | ImGuiTableFlags_Borders))
  1259. {
  1260. for (int i = 0; i < 10; i++)
  1261. {
  1262. char label[32];
  1263. sprintf(label, "Item %d", i);
  1264. ImGui::TableNextColumn();
  1265. ImGui::Selectable(label, &selected[i]); // FIXME-TABLE: Selection overlap
  1266. }
  1267. ImGui::EndTable();
  1268. }
  1269. ImGui::Spacing();
  1270. if (ImGui::BeginTable("split2", 3, ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings | ImGuiTableFlags_Borders))
  1271. {
  1272. for (int i = 0; i < 10; i++)
  1273. {
  1274. char label[32];
  1275. sprintf(label, "Item %d", i);
  1276. ImGui::TableNextRow();
  1277. ImGui::TableNextColumn();
  1278. ImGui::Selectable(label, &selected[i], ImGuiSelectableFlags_SpanAllColumns);
  1279. ImGui::TableNextColumn();
  1280. ImGui::Text("Some other contents");
  1281. ImGui::TableNextColumn();
  1282. ImGui::Text("123456");
  1283. }
  1284. ImGui::EndTable();
  1285. }
  1286. ImGui::TreePop();
  1287. }
  1288. IMGUI_DEMO_MARKER("Widgets/Selectables/Grid");
  1289. if (ImGui::TreeNode("Grid"))
  1290. {
  1291. static char selected[4][4] = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } };
  1292. // Add in a bit of silly fun...
  1293. const float time = (float)ImGui::GetTime();
  1294. const bool winning_state = memchr(selected, 0, sizeof(selected)) == NULL; // If all cells are selected...
  1295. if (winning_state)
  1296. ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, ImVec2(0.5f + 0.5f * cosf(time * 2.0f), 0.5f + 0.5f * sinf(time * 3.0f)));
  1297. for (int y = 0; y < 4; y++)
  1298. for (int x = 0; x < 4; x++)
  1299. {
  1300. if (x > 0)
  1301. ImGui::SameLine();
  1302. ImGui::PushID(y * 4 + x);
  1303. if (ImGui::Selectable("Sailor", selected[y][x] != 0, 0, ImVec2(50, 50)))
  1304. {
  1305. // Toggle clicked cell + toggle neighbors
  1306. selected[y][x] ^= 1;
  1307. if (x > 0) { selected[y][x - 1] ^= 1; }
  1308. if (x < 3) { selected[y][x + 1] ^= 1; }
  1309. if (y > 0) { selected[y - 1][x] ^= 1; }
  1310. if (y < 3) { selected[y + 1][x] ^= 1; }
  1311. }
  1312. ImGui::PopID();
  1313. }
  1314. if (winning_state)
  1315. ImGui::PopStyleVar();
  1316. ImGui::TreePop();
  1317. }
  1318. IMGUI_DEMO_MARKER("Widgets/Selectables/Alignment");
  1319. if (ImGui::TreeNode("Alignment"))
  1320. {
  1321. HelpMarker(
  1322. "By default, Selectables uses style.SelectableTextAlign but it can be overridden on a per-item "
  1323. "basis using PushStyleVar(). You'll probably want to always keep your default situation to "
  1324. "left-align otherwise it becomes difficult to layout multiple items on a same line");
  1325. static bool selected[3 * 3] = { true, false, true, false, true, false, true, false, true };
  1326. for (int y = 0; y < 3; y++)
  1327. {
  1328. for (int x = 0; x < 3; x++)
  1329. {
  1330. ImVec2 alignment = ImVec2((float)x / 2.0f, (float)y / 2.0f);
  1331. char name[32];
  1332. sprintf(name, "(%.1f,%.1f)", alignment.x, alignment.y);
  1333. if (x > 0) ImGui::SameLine();
  1334. ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, alignment);
  1335. ImGui::Selectable(name, &selected[3 * y + x], ImGuiSelectableFlags_None, ImVec2(80, 80));
  1336. ImGui::PopStyleVar();
  1337. }
  1338. }
  1339. ImGui::TreePop();
  1340. }
  1341. ImGui::TreePop();
  1342. }
  1343. // To wire InputText() with std::string or any other custom string type,
  1344. // see the "Text Input > Resize Callback" section of this demo, and the misc/cpp/imgui_stdlib.h file.
  1345. IMGUI_DEMO_MARKER("Widgets/Text Input");
  1346. if (ImGui::TreeNode("Text Input"))
  1347. {
  1348. IMGUI_DEMO_MARKER("Widgets/Text Input/Multi-line Text Input");
  1349. if (ImGui::TreeNode("Multi-line Text Input"))
  1350. {
  1351. // Note: we are using a fixed-sized buffer for simplicity here. See ImGuiInputTextFlags_CallbackResize
  1352. // and the code in misc/cpp/imgui_stdlib.h for how to setup InputText() for dynamically resizing strings.
  1353. static char text[1024 * 16] =
  1354. "/*\n"
  1355. " The Pentium F00F bug, shorthand for F0 0F C7 C8,\n"
  1356. " the hexadecimal encoding of one offending instruction,\n"
  1357. " more formally, the invalid operand with locked CMPXCHG8B\n"
  1358. " instruction bug, is a design flaw in the majority of\n"
  1359. " Intel Pentium, Pentium MMX, and Pentium OverDrive\n"
  1360. " processors (all in the P5 microarchitecture).\n"
  1361. "*/\n\n"
  1362. "label:\n"
  1363. "\tlock cmpxchg8b eax\n";
  1364. static ImGuiInputTextFlags flags = ImGuiInputTextFlags_AllowTabInput;
  1365. HelpMarker("You can use the ImGuiInputTextFlags_CallbackResize facility if you need to wire InputTextMultiline() to a dynamic string type. See misc/cpp/imgui_stdlib.h for an example. (This is not demonstrated in imgui_demo.cpp because we don't want to include <string> in here)");
  1366. ImGui::CheckboxFlags("ImGuiInputTextFlags_ReadOnly", &flags, ImGuiInputTextFlags_ReadOnly);
  1367. ImGui::CheckboxFlags("ImGuiInputTextFlags_AllowTabInput", &flags, ImGuiInputTextFlags_AllowTabInput);
  1368. ImGui::SameLine(); HelpMarker("When _AllowTabInput is set, passing through the widget with Tabbing doesn't automatically activate it, in order to also cycling through subsequent widgets.");
  1369. ImGui::CheckboxFlags("ImGuiInputTextFlags_CtrlEnterForNewLine", &flags, ImGuiInputTextFlags_CtrlEnterForNewLine);
  1370. ImGui::InputTextMultiline("##source", text, IM_ARRAYSIZE(text), ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16), flags);
  1371. ImGui::TreePop();
  1372. }
  1373. IMGUI_DEMO_MARKER("Widgets/Text Input/Filtered Text Input");
  1374. if (ImGui::TreeNode("Filtered Text Input"))
  1375. {
  1376. struct TextFilters
  1377. {
  1378. // Modify character input by altering 'data->Eventchar' (ImGuiInputTextFlags_CallbackCharFilter callback)
  1379. static int FilterCasingSwap(ImGuiInputTextCallbackData* data)
  1380. {
  1381. if (data->EventChar >= 'a' && data->EventChar <= 'z') { data->EventChar -= 'a' - 'A'; } // Lowercase becomes uppercase
  1382. else if (data->EventChar >= 'A' && data->EventChar <= 'Z') { data->EventChar += 'a' - 'A'; } // Uppercase becomes lowercase
  1383. return 0;
  1384. }
  1385. // Return 0 (pass) if the character is 'i' or 'm' or 'g' or 'u' or 'i', otherwise return 1 (filter out)
  1386. static int FilterImGuiLetters(ImGuiInputTextCallbackData* data)
  1387. {
  1388. if (data->EventChar < 256 && strchr("imgui", (char)data->EventChar))
  1389. return 0;
  1390. return 1;
  1391. }
  1392. };
  1393. static char buf1[32] = ""; ImGui::InputText("default", buf1, 32);
  1394. static char buf2[32] = ""; ImGui::InputText("decimal", buf2, 32, ImGuiInputTextFlags_CharsDecimal);
  1395. static char buf3[32] = ""; ImGui::InputText("hexadecimal", buf3, 32, ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase);
  1396. static char buf4[32] = ""; ImGui::InputText("uppercase", buf4, 32, ImGuiInputTextFlags_CharsUppercase);
  1397. static char buf5[32] = ""; ImGui::InputText("no blank", buf5, 32, ImGuiInputTextFlags_CharsNoBlank);
  1398. static char buf6[32] = ""; ImGui::InputText("casing swap", buf6, 32, ImGuiInputTextFlags_CallbackCharFilter, TextFilters::FilterCasingSwap); // Use CharFilter callback to replace characters.
  1399. static char buf7[32] = ""; ImGui::InputText("\"imgui\"", buf7, 32, ImGuiInputTextFlags_CallbackCharFilter, TextFilters::FilterImGuiLetters); // Use CharFilter callback to disable some characters.
  1400. ImGui::TreePop();
  1401. }
  1402. IMGUI_DEMO_MARKER("Widgets/Text Input/Password input");
  1403. if (ImGui::TreeNode("Password Input"))
  1404. {
  1405. static char password[64] = "password123";
  1406. ImGui::InputText("password", password, IM_ARRAYSIZE(password), ImGuiInputTextFlags_Password);
  1407. ImGui::SameLine(); HelpMarker("Display all characters as '*'.\nDisable clipboard cut and copy.\nDisable logging.\n");
  1408. ImGui::InputTextWithHint("password (w/ hint)", "<password>", password, IM_ARRAYSIZE(password), ImGuiInputTextFlags_Password);
  1409. ImGui::InputText("password (clear)", password, IM_ARRAYSIZE(password));
  1410. ImGui::TreePop();
  1411. }
  1412. IMGUI_DEMO_MARKER("Widgets/Text Input/Completion, History, Edit Callbacks");
  1413. if (ImGui::TreeNode("Completion, History, Edit Callbacks"))
  1414. {
  1415. struct Funcs
  1416. {
  1417. static int MyCallback(ImGuiInputTextCallbackData* data)
  1418. {
  1419. if (data->EventFlag == ImGuiInputTextFlags_CallbackCompletion)
  1420. {
  1421. data->InsertChars(data->CursorPos, "..");
  1422. }
  1423. else if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory)
  1424. {
  1425. if (data->EventKey == ImGuiKey_UpArrow)
  1426. {
  1427. data->DeleteChars(0, data->BufTextLen);
  1428. data->InsertChars(0, "Pressed Up!");
  1429. data->SelectAll();
  1430. }
  1431. else if (data->EventKey == ImGuiKey_DownArrow)
  1432. {
  1433. data->DeleteChars(0, data->BufTextLen);
  1434. data->InsertChars(0, "Pressed Down!");
  1435. data->SelectAll();
  1436. }
  1437. }
  1438. else if (data->EventFlag == ImGuiInputTextFlags_CallbackEdit)
  1439. {
  1440. // Toggle casing of first character
  1441. char c = data->Buf[0];
  1442. if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) data->Buf[0] ^= 32;
  1443. data->BufDirty = true;
  1444. // Increment a counter
  1445. int* p_int = (int*)data->UserData;
  1446. *p_int = *p_int + 1;
  1447. }
  1448. return 0;
  1449. }
  1450. };
  1451. static char buf1[64];
  1452. ImGui::InputText("Completion", buf1, 64, ImGuiInputTextFlags_CallbackCompletion, Funcs::MyCallback);
  1453. ImGui::SameLine(); HelpMarker(
  1454. "Here we append \"..\" each time Tab is pressed. "
  1455. "See 'Examples>Console' for a more meaningful demonstration of using this callback.");
  1456. static char buf2[64];
  1457. ImGui::InputText("History", buf2, 64, ImGuiInputTextFlags_CallbackHistory, Funcs::MyCallback);
  1458. ImGui::SameLine(); HelpMarker(
  1459. "Here we replace and select text each time Up/Down are pressed. "
  1460. "See 'Examples>Console' for a more meaningful demonstration of using this callback.");
  1461. static char buf3[64];
  1462. static int edit_count = 0;
  1463. ImGui::InputText("Edit", buf3, 64, ImGuiInputTextFlags_CallbackEdit, Funcs::MyCallback, (void*)&edit_count);
  1464. ImGui::SameLine(); HelpMarker(
  1465. "Here we toggle the casing of the first character on every edit + count edits.");
  1466. ImGui::SameLine(); ImGui::Text("(%d)", edit_count);
  1467. ImGui::TreePop();
  1468. }
  1469. IMGUI_DEMO_MARKER("Widgets/Text Input/Resize Callback");
  1470. if (ImGui::TreeNode("Resize Callback"))
  1471. {
  1472. // To wire InputText() with std::string or any other custom string type,
  1473. // you can use the ImGuiInputTextFlags_CallbackResize flag + create a custom ImGui::InputText() wrapper
  1474. // using your preferred type. See misc/cpp/imgui_stdlib.h for an implementation of this using std::string.
  1475. HelpMarker(
  1476. "Using ImGuiInputTextFlags_CallbackResize to wire your custom string type to InputText().\n\n"
  1477. "See misc/cpp/imgui_stdlib.h for an implementation of this for std::string.");
  1478. struct Funcs
  1479. {
  1480. static int MyResizeCallback(ImGuiInputTextCallbackData* data)
  1481. {
  1482. if (data->EventFlag == ImGuiInputTextFlags_CallbackResize)
  1483. {
  1484. ImVector<char>* my_str = (ImVector<char>*)data->UserData;
  1485. IM_ASSERT(my_str->begin() == data->Buf);
  1486. my_str->resize(data->BufSize); // NB: On resizing calls, generally data->BufSize == data->BufTextLen + 1
  1487. data->Buf = my_str->begin();
  1488. }
  1489. return 0;
  1490. }
  1491. // Note: Because ImGui:: is a namespace you would typically add your own function into the namespace.
  1492. // For example, you code may declare a function 'ImGui::InputText(const char* label, MyString* my_str)'
  1493. static bool MyInputTextMultiline(const char* label, ImVector<char>* my_str, const ImVec2& size = ImVec2(0, 0), ImGuiInputTextFlags flags = 0)
  1494. {
  1495. IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
  1496. return ImGui::InputTextMultiline(label, my_str->begin(), (size_t)my_str->size(), size, flags | ImGuiInputTextFlags_CallbackResize, Funcs::MyResizeCallback, (void*)my_str);
  1497. }
  1498. };
  1499. // For this demo we are using ImVector as a string container.
  1500. // Note that because we need to store a terminating zero character, our size/capacity are 1 more
  1501. // than usually reported by a typical string class.
  1502. static ImVector<char> my_str;
  1503. if (my_str.empty())
  1504. my_str.push_back(0);
  1505. Funcs::MyInputTextMultiline("##MyStr", &my_str, ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16));
  1506. ImGui::Text("Data: %p\nSize: %d\nCapacity: %d", (void*)my_str.begin(), my_str.size(), my_str.capacity());
  1507. ImGui::TreePop();
  1508. }
  1509. IMGUI_DEMO_MARKER("Widgets/Text Input/Miscellaneous");
  1510. if (ImGui::TreeNode("Miscellaneous"))
  1511. {
  1512. static char buf1[16];
  1513. static ImGuiInputTextFlags flags = ImGuiInputTextFlags_EscapeClearsAll;
  1514. ImGui::CheckboxFlags("ImGuiInputTextFlags_EscapeClearsAll", &flags, ImGuiInputTextFlags_EscapeClearsAll);
  1515. ImGui::CheckboxFlags("ImGuiInputTextFlags_ReadOnly", &flags, ImGuiInputTextFlags_ReadOnly);
  1516. ImGui::CheckboxFlags("ImGuiInputTextFlags_NoUndoRedo", &flags, ImGuiInputTextFlags_NoUndoRedo);
  1517. ImGui::InputText("Hello", buf1, IM_ARRAYSIZE(buf1), flags);
  1518. ImGui::TreePop();
  1519. }
  1520. ImGui::TreePop();
  1521. }
  1522. // Tabs
  1523. IMGUI_DEMO_MARKER("Widgets/Tabs");
  1524. if (ImGui::TreeNode("Tabs"))
  1525. {
  1526. IMGUI_DEMO_MARKER("Widgets/Tabs/Basic");
  1527. if (ImGui::TreeNode("Basic"))
  1528. {
  1529. ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_None;
  1530. if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags))
  1531. {
  1532. if (ImGui::BeginTabItem("Avocado"))
  1533. {
  1534. ImGui::Text("This is the Avocado tab!\nblah blah blah blah blah");
  1535. ImGui::EndTabItem();
  1536. }
  1537. if (ImGui::BeginTabItem("Broccoli"))
  1538. {
  1539. ImGui::Text("This is the Broccoli tab!\nblah blah blah blah blah");
  1540. ImGui::EndTabItem();
  1541. }
  1542. if (ImGui::BeginTabItem("Cucumber"))
  1543. {
  1544. ImGui::Text("This is the Cucumber tab!\nblah blah blah blah blah");
  1545. ImGui::EndTabItem();
  1546. }
  1547. ImGui::EndTabBar();
  1548. }
  1549. ImGui::Separator();
  1550. ImGui::TreePop();
  1551. }
  1552. IMGUI_DEMO_MARKER("Widgets/Tabs/Advanced & Close Button");
  1553. if (ImGui::TreeNode("Advanced & Close Button"))
  1554. {
  1555. // Expose a couple of the available flags. In most cases you may just call BeginTabBar() with no flags (0).
  1556. static ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_Reorderable;
  1557. ImGui::CheckboxFlags("ImGuiTabBarFlags_Reorderable", &tab_bar_flags, ImGuiTabBarFlags_Reorderable);
  1558. ImGui::CheckboxFlags("ImGuiTabBarFlags_AutoSelectNewTabs", &tab_bar_flags, ImGuiTabBarFlags_AutoSelectNewTabs);
  1559. ImGui::CheckboxFlags("ImGuiTabBarFlags_TabListPopupButton", &tab_bar_flags, ImGuiTabBarFlags_TabListPopupButton);
  1560. ImGui::CheckboxFlags("ImGuiTabBarFlags_NoCloseWithMiddleMouseButton", &tab_bar_flags, ImGuiTabBarFlags_NoCloseWithMiddleMouseButton);
  1561. if ((tab_bar_flags & ImGuiTabBarFlags_FittingPolicyMask_) == 0)
  1562. tab_bar_flags |= ImGuiTabBarFlags_FittingPolicyDefault_;
  1563. if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyResizeDown))
  1564. tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyResizeDown);
  1565. if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyScroll))
  1566. tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll);
  1567. // Tab Bar
  1568. const char* names[4] = { "Artichoke", "Beetroot", "Celery", "Daikon" };
  1569. static bool opened[4] = { true, true, true, true }; // Persistent user state
  1570. for (int n = 0; n < IM_ARRAYSIZE(opened); n++)
  1571. {
  1572. if (n > 0) { ImGui::SameLine(); }
  1573. ImGui::Checkbox(names[n], &opened[n]);
  1574. }
  1575. // Passing a bool* to BeginTabItem() is similar to passing one to Begin():
  1576. // the underlying bool will be set to false when the tab is closed.
  1577. if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags))
  1578. {
  1579. for (int n = 0; n < IM_ARRAYSIZE(opened); n++)
  1580. if (opened[n] && ImGui::BeginTabItem(names[n], &opened[n], ImGuiTabItemFlags_None))
  1581. {
  1582. ImGui::Text("This is the %s tab!", names[n]);
  1583. if (n & 1)
  1584. ImGui::Text("I am an odd tab.");
  1585. ImGui::EndTabItem();
  1586. }
  1587. ImGui::EndTabBar();
  1588. }
  1589. ImGui::Separator();
  1590. ImGui::TreePop();
  1591. }
  1592. IMGUI_DEMO_MARKER("Widgets/Tabs/TabItemButton & Leading-Trailing flags");
  1593. if (ImGui::TreeNode("TabItemButton & Leading/Trailing flags"))
  1594. {
  1595. static ImVector<int> active_tabs;
  1596. static int next_tab_id = 0;
  1597. if (next_tab_id == 0) // Initialize with some default tabs
  1598. for (int i = 0; i < 3; i++)
  1599. active_tabs.push_back(next_tab_id++);
  1600. // TabItemButton() and Leading/Trailing flags are distinct features which we will demo together.
  1601. // (It is possible to submit regular tabs with Leading/Trailing flags, or TabItemButton tabs without Leading/Trailing flags...
  1602. // but they tend to make more sense together)
  1603. static bool show_leading_button = true;
  1604. static bool show_trailing_button = true;
  1605. ImGui::Checkbox("Show Leading TabItemButton()", &show_leading_button);
  1606. ImGui::Checkbox("Show Trailing TabItemButton()", &show_trailing_button);
  1607. // Expose some other flags which are useful to showcase how they interact with Leading/Trailing tabs
  1608. static ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_AutoSelectNewTabs | ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_FittingPolicyResizeDown;
  1609. ImGui::CheckboxFlags("ImGuiTabBarFlags_TabListPopupButton", &tab_bar_flags, ImGuiTabBarFlags_TabListPopupButton);
  1610. if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyResizeDown))
  1611. tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyResizeDown);
  1612. if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyScroll))
  1613. tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll);
  1614. if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags))
  1615. {
  1616. // Demo a Leading TabItemButton(): click the "?" button to open a menu
  1617. if (show_leading_button)
  1618. if (ImGui::TabItemButton("?", ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_NoTooltip))
  1619. ImGui::OpenPopup("MyHelpMenu");
  1620. if (ImGui::BeginPopup("MyHelpMenu"))
  1621. {
  1622. ImGui::Selectable("Hello!");
  1623. ImGui::EndPopup();
  1624. }
  1625. // Demo Trailing Tabs: click the "+" button to add a new tab.
  1626. // (In your app you may want to use a font icon instead of the "+")
  1627. // We submit it before the regular tabs, but thanks to the ImGuiTabItemFlags_Trailing flag it will always appear at the end.
  1628. if (show_trailing_button)
  1629. if (ImGui::TabItemButton("+", ImGuiTabItemFlags_Trailing | ImGuiTabItemFlags_NoTooltip))
  1630. active_tabs.push_back(next_tab_id++); // Add new tab
  1631. // Submit our regular tabs
  1632. for (int n = 0; n < active_tabs.Size; )
  1633. {
  1634. bool open = true;
  1635. char name[16];
  1636. snprintf(name, IM_ARRAYSIZE(name), "%04d", active_tabs[n]);
  1637. if (ImGui::BeginTabItem(name, &open, ImGuiTabItemFlags_None))
  1638. {
  1639. ImGui::Text("This is the %s tab!", name);
  1640. ImGui::EndTabItem();
  1641. }
  1642. if (!open)
  1643. active_tabs.erase(active_tabs.Data + n);
  1644. else
  1645. n++;
  1646. }
  1647. ImGui::EndTabBar();
  1648. }
  1649. ImGui::Separator();
  1650. ImGui::TreePop();
  1651. }
  1652. ImGui::TreePop();
  1653. }
  1654. // Plot/Graph widgets are not very good.
  1655. // Consider using a third-party library such as ImPlot: https://github.com/epezent/implot
  1656. // (see others https://github.com/ocornut/imgui/wiki/Useful-Extensions)
  1657. IMGUI_DEMO_MARKER("Widgets/Plotting");
  1658. if (ImGui::TreeNode("Plotting"))
  1659. {
  1660. static bool animate = true;
  1661. ImGui::Checkbox("Animate", &animate);
  1662. // Plot as lines and plot as histogram
  1663. static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f };
  1664. ImGui::PlotLines("Frame Times", arr, IM_ARRAYSIZE(arr));
  1665. ImGui::PlotHistogram("Histogram", arr, IM_ARRAYSIZE(arr), 0, NULL, 0.0f, 1.0f, ImVec2(0, 80.0f));
  1666. //ImGui::SameLine(); HelpMarker("Consider using ImPlot instead!");
  1667. // Fill an array of contiguous float values to plot
  1668. // Tip: If your float aren't contiguous but part of a structure, you can pass a pointer to your first float
  1669. // and the sizeof() of your structure in the "stride" parameter.
  1670. static float values[90] = {};
  1671. static int values_offset = 0;
  1672. static double refresh_time = 0.0;
  1673. if (!animate || refresh_time == 0.0)
  1674. refresh_time = ImGui::GetTime();
  1675. while (refresh_time < ImGui::GetTime()) // Create data at fixed 60 Hz rate for the demo
  1676. {
  1677. static float phase = 0.0f;
  1678. values[values_offset] = cosf(phase);
  1679. values_offset = (values_offset + 1) % IM_ARRAYSIZE(values);
  1680. phase += 0.10f * values_offset;
  1681. refresh_time += 1.0f / 60.0f;
  1682. }
  1683. // Plots can display overlay texts
  1684. // (in this example, we will display an average value)
  1685. {
  1686. float average = 0.0f;
  1687. for (int n = 0; n < IM_ARRAYSIZE(values); n++)
  1688. average += values[n];
  1689. average /= (float)IM_ARRAYSIZE(values);
  1690. char overlay[32];
  1691. sprintf(overlay, "avg %f", average);
  1692. ImGui::PlotLines("Lines", values, IM_ARRAYSIZE(values), values_offset, overlay, -1.0f, 1.0f, ImVec2(0, 80.0f));
  1693. }
  1694. // Use functions to generate output
  1695. // FIXME: This is actually VERY awkward because current plot API only pass in indices.
  1696. // We probably want an API passing floats and user provide sample rate/count.
  1697. struct Funcs
  1698. {
  1699. static float Sin(void*, int i) { return sinf(i * 0.1f); }
  1700. static float Saw(void*, int i) { return (i & 1) ? 1.0f : -1.0f; }
  1701. };
  1702. static int func_type = 0, display_count = 70;
  1703. ImGui::SeparatorText("Functions");
  1704. ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
  1705. ImGui::Combo("func", &func_type, "Sin\0Saw\0");
  1706. ImGui::SameLine();
  1707. ImGui::SliderInt("Sample count", &display_count, 1, 400);
  1708. float (*func)(void*, int) = (func_type == 0) ? Funcs::Sin : Funcs::Saw;
  1709. ImGui::PlotLines("Lines", func, NULL, display_count, 0, NULL, -1.0f, 1.0f, ImVec2(0, 80));
  1710. ImGui::PlotHistogram("Histogram", func, NULL, display_count, 0, NULL, -1.0f, 1.0f, ImVec2(0, 80));
  1711. ImGui::Separator();
  1712. ImGui::TreePop();
  1713. }
  1714. IMGUI_DEMO_MARKER("Widgets/Progress Bars");
  1715. if (ImGui::TreeNode("Progress Bars"))
  1716. {
  1717. // Animate a simple progress bar
  1718. static float progress = 0.0f, progress_dir = 1.0f;
  1719. progress += progress_dir * 0.4f * ImGui::GetIO().DeltaTime;
  1720. if (progress >= +1.1f) { progress = +1.1f; progress_dir *= -1.0f; }
  1721. if (progress <= -0.1f) { progress = -0.1f; progress_dir *= -1.0f; }
  1722. // Typically we would use ImVec2(-1.0f,0.0f) or ImVec2(-FLT_MIN,0.0f) to use all available width,
  1723. // or ImVec2(width,0.0f) for a specified width. ImVec2(0.0f,0.0f) uses ItemWidth.
  1724. ImGui::ProgressBar(progress, ImVec2(0.0f, 0.0f));
  1725. ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x);
  1726. ImGui::Text("Progress Bar");
  1727. float progress_saturated = IM_CLAMP(progress, 0.0f, 1.0f);
  1728. char buf[32];
  1729. sprintf(buf, "%d/%d", (int)(progress_saturated * 1753), 1753);
  1730. ImGui::ProgressBar(progress, ImVec2(0.f, 0.f), buf);
  1731. // Pass an animated negative value, e.g. -1.0f * (float)ImGui::GetTime() is the recommended value.
  1732. // Adjust the factor if you want to adjust the animation speed.
  1733. ImGui::ProgressBar(-1.0f * (float)ImGui::GetTime(), ImVec2(0.0f, 0.0f), "Searching..");
  1734. ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x);
  1735. ImGui::Text("Indeterminate");
  1736. ImGui::TreePop();
  1737. }
  1738. IMGUI_DEMO_MARKER("Widgets/Color");
  1739. if (ImGui::TreeNode("Color/Picker Widgets"))
  1740. {
  1741. static ImVec4 color = ImVec4(114.0f / 255.0f, 144.0f / 255.0f, 154.0f / 255.0f, 200.0f / 255.0f);
  1742. static bool alpha_preview = true;
  1743. static bool alpha_half_preview = false;
  1744. static bool drag_and_drop = true;
  1745. static bool options_menu = true;
  1746. static bool hdr = false;
  1747. ImGui::SeparatorText("Options");
  1748. ImGui::Checkbox("With Alpha Preview", &alpha_preview);
  1749. ImGui::Checkbox("With Half Alpha Preview", &alpha_half_preview);
  1750. ImGui::Checkbox("With Drag and Drop", &drag_and_drop);
  1751. ImGui::Checkbox("With Options Menu", &options_menu); ImGui::SameLine(); HelpMarker("Right-click on the individual color widget to show options.");
  1752. ImGui::Checkbox("With HDR", &hdr); ImGui::SameLine(); HelpMarker("Currently all this does is to lift the 0..1 limits on dragging widgets.");
  1753. ImGuiColorEditFlags misc_flags = (hdr ? ImGuiColorEditFlags_HDR : 0) | (drag_and_drop ? 0 : ImGuiColorEditFlags_NoDragDrop) | (alpha_half_preview ? ImGuiColorEditFlags_AlphaPreviewHalf : (alpha_preview ? ImGuiColorEditFlags_AlphaPreview : 0)) | (options_menu ? 0 : ImGuiColorEditFlags_NoOptions);
  1754. IMGUI_DEMO_MARKER("Widgets/Color/ColorEdit");
  1755. ImGui::SeparatorText("Inline color editor");
  1756. ImGui::Text("Color widget:");
  1757. ImGui::SameLine(); HelpMarker(
  1758. "Click on the color square to open a color picker.\n"
  1759. "CTRL+click on individual component to input value.\n");
  1760. ImGui::ColorEdit3("MyColor##1", (float*)&color, misc_flags);
  1761. IMGUI_DEMO_MARKER("Widgets/Color/ColorEdit (HSV, with Alpha)");
  1762. ImGui::Text("Color widget HSV with Alpha:");
  1763. ImGui::ColorEdit4("MyColor##2", (float*)&color, ImGuiColorEditFlags_DisplayHSV | misc_flags);
  1764. IMGUI_DEMO_MARKER("Widgets/Color/ColorEdit (float display)");
  1765. ImGui::Text("Color widget with Float Display:");
  1766. ImGui::ColorEdit4("MyColor##2f", (float*)&color, ImGuiColorEditFlags_Float | misc_flags);
  1767. IMGUI_DEMO_MARKER("Widgets/Color/ColorButton (with Picker)");
  1768. ImGui::Text("Color button with Picker:");
  1769. ImGui::SameLine(); HelpMarker(
  1770. "With the ImGuiColorEditFlags_NoInputs flag you can hide all the slider/text inputs.\n"
  1771. "With the ImGuiColorEditFlags_NoLabel flag you can pass a non-empty label which will only "
  1772. "be used for the tooltip and picker popup.");
  1773. ImGui::ColorEdit4("MyColor##3", (float*)&color, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel | misc_flags);
  1774. IMGUI_DEMO_MARKER("Widgets/Color/ColorButton (with custom Picker popup)");
  1775. ImGui::Text("Color button with Custom Picker Popup:");
  1776. // Generate a default palette. The palette will persist and can be edited.
  1777. static bool saved_palette_init = true;
  1778. static ImVec4 saved_palette[32] = {};
  1779. if (saved_palette_init)
  1780. {
  1781. for (int n = 0; n < IM_ARRAYSIZE(saved_palette); n++)
  1782. {
  1783. ImGui::ColorConvertHSVtoRGB(n / 31.0f, 0.8f, 0.8f,
  1784. saved_palette[n].x, saved_palette[n].y, saved_palette[n].z);
  1785. saved_palette[n].w = 1.0f; // Alpha
  1786. }
  1787. saved_palette_init = false;
  1788. }
  1789. static ImVec4 backup_color;
  1790. bool open_popup = ImGui::ColorButton("MyColor##3b", color, misc_flags);
  1791. ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
  1792. open_popup |= ImGui::Button("Palette");
  1793. if (open_popup)
  1794. {
  1795. ImGui::OpenPopup("mypicker");
  1796. backup_color = color;
  1797. }
  1798. if (ImGui::BeginPopup("mypicker"))
  1799. {
  1800. ImGui::Text("MY CUSTOM COLOR PICKER WITH AN AMAZING PALETTE!");
  1801. ImGui::Separator();
  1802. ImGui::ColorPicker4("##picker", (float*)&color, misc_flags | ImGuiColorEditFlags_NoSidePreview | ImGuiColorEditFlags_NoSmallPreview);
  1803. ImGui::SameLine();
  1804. ImGui::BeginGroup(); // Lock X position
  1805. ImGui::Text("Current");
  1806. ImGui::ColorButton("##current", color, ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_AlphaPreviewHalf, ImVec2(60, 40));
  1807. ImGui::Text("Previous");
  1808. if (ImGui::ColorButton("##previous", backup_color, ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_AlphaPreviewHalf, ImVec2(60, 40)))
  1809. color = backup_color;
  1810. ImGui::Separator();
  1811. ImGui::Text("Palette");
  1812. for (int n = 0; n < IM_ARRAYSIZE(saved_palette); n++)
  1813. {
  1814. ImGui::PushID(n);
  1815. if ((n % 8) != 0)
  1816. ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y);
  1817. ImGuiColorEditFlags palette_button_flags = ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_NoTooltip;
  1818. if (ImGui::ColorButton("##palette", saved_palette[n], palette_button_flags, ImVec2(20, 20)))
  1819. color = ImVec4(saved_palette[n].x, saved_palette[n].y, saved_palette[n].z, color.w); // Preserve alpha!
  1820. // Allow user to drop colors into each palette entry. Note that ColorButton() is already a
  1821. // drag source by default, unless specifying the ImGuiColorEditFlags_NoDragDrop flag.
  1822. if (ImGui::BeginDragDropTarget())
  1823. {
  1824. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F))
  1825. memcpy((float*)&saved_palette[n], payload->Data, sizeof(float) * 3);
  1826. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_4F))
  1827. memcpy((float*)&saved_palette[n], payload->Data, sizeof(float) * 4);
  1828. ImGui::EndDragDropTarget();
  1829. }
  1830. ImGui::PopID();
  1831. }
  1832. ImGui::EndGroup();
  1833. ImGui::EndPopup();
  1834. }
  1835. IMGUI_DEMO_MARKER("Widgets/Color/ColorButton (simple)");
  1836. ImGui::Text("Color button only:");
  1837. static bool no_border = false;
  1838. ImGui::Checkbox("ImGuiColorEditFlags_NoBorder", &no_border);
  1839. ImGui::ColorButton("MyColor##3c", *(ImVec4*)&color, misc_flags | (no_border ? ImGuiColorEditFlags_NoBorder : 0), ImVec2(80, 80));
  1840. IMGUI_DEMO_MARKER("Widgets/Color/ColorPicker");
  1841. ImGui::SeparatorText("Color picker");
  1842. static bool alpha = true;
  1843. static bool alpha_bar = true;
  1844. static bool side_preview = true;
  1845. static bool ref_color = false;
  1846. static ImVec4 ref_color_v(1.0f, 0.0f, 1.0f, 0.5f);
  1847. static int display_mode = 0;
  1848. static int picker_mode = 0;
  1849. ImGui::Checkbox("With Alpha", &alpha);
  1850. ImGui::Checkbox("With Alpha Bar", &alpha_bar);
  1851. ImGui::Checkbox("With Side Preview", &side_preview);
  1852. if (side_preview)
  1853. {
  1854. ImGui::SameLine();
  1855. ImGui::Checkbox("With Ref Color", &ref_color);
  1856. if (ref_color)
  1857. {
  1858. ImGui::SameLine();
  1859. ImGui::ColorEdit4("##RefColor", &ref_color_v.x, ImGuiColorEditFlags_NoInputs | misc_flags);
  1860. }
  1861. }
  1862. ImGui::Combo("Display Mode", &display_mode, "Auto/Current\0None\0RGB Only\0HSV Only\0Hex Only\0");
  1863. ImGui::SameLine(); HelpMarker(
  1864. "ColorEdit defaults to displaying RGB inputs if you don't specify a display mode, "
  1865. "but the user can change it with a right-click on those inputs.\n\nColorPicker defaults to displaying RGB+HSV+Hex "
  1866. "if you don't specify a display mode.\n\nYou can change the defaults using SetColorEditOptions().");
  1867. ImGui::SameLine(); HelpMarker("When not specified explicitly (Auto/Current mode), user can right-click the picker to change mode.");
  1868. ImGuiColorEditFlags flags = misc_flags;
  1869. if (!alpha) flags |= ImGuiColorEditFlags_NoAlpha; // This is by default if you call ColorPicker3() instead of ColorPicker4()
  1870. if (alpha_bar) flags |= ImGuiColorEditFlags_AlphaBar;
  1871. if (!side_preview) flags |= ImGuiColorEditFlags_NoSidePreview;
  1872. if (picker_mode == 1) flags |= ImGuiColorEditFlags_PickerHueBar;
  1873. if (picker_mode == 2) flags |= ImGuiColorEditFlags_PickerHueWheel;
  1874. if (display_mode == 1) flags |= ImGuiColorEditFlags_NoInputs; // Disable all RGB/HSV/Hex displays
  1875. if (display_mode == 2) flags |= ImGuiColorEditFlags_DisplayRGB; // Override display mode
  1876. if (display_mode == 3) flags |= ImGuiColorEditFlags_DisplayHSV;
  1877. if (display_mode == 4) flags |= ImGuiColorEditFlags_DisplayHex;
  1878. ImGui::ColorPicker4("MyColor##4", (float*)&color, flags, ref_color ? &ref_color_v.x : NULL);
  1879. ImGui::Text("Set defaults in code:");
  1880. ImGui::SameLine(); HelpMarker(
  1881. "SetColorEditOptions() is designed to allow you to set boot-time default.\n"
  1882. "We don't have Push/Pop functions because you can force options on a per-widget basis if needed,"
  1883. "and the user can change non-forced ones with the options menu.\nWe don't have a getter to avoid"
  1884. "encouraging you to persistently save values that aren't forward-compatible.");
  1885. if (ImGui::Button("Default: Uint8 + HSV + Hue Bar"))
  1886. ImGui::SetColorEditOptions(ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_DisplayHSV | ImGuiColorEditFlags_PickerHueBar);
  1887. if (ImGui::Button("Default: Float + HDR + Hue Wheel"))
  1888. ImGui::SetColorEditOptions(ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_PickerHueWheel);
  1889. // Always display a small version of both types of pickers
  1890. // (that's in order to make it more visible in the demo to people who are skimming quickly through it)
  1891. ImGui::Text("Both types:");
  1892. float w = (ImGui::GetContentRegionAvail().x - ImGui::GetStyle().ItemSpacing.y) * 0.40f;
  1893. ImGui::SetNextItemWidth(w);
  1894. ImGui::ColorPicker3("##MyColor##5", (float*)&color, ImGuiColorEditFlags_PickerHueBar | ImGuiColorEditFlags_NoSidePreview | ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoAlpha);
  1895. ImGui::SameLine();
  1896. ImGui::SetNextItemWidth(w);
  1897. ImGui::ColorPicker3("##MyColor##6", (float*)&color, ImGuiColorEditFlags_PickerHueWheel | ImGuiColorEditFlags_NoSidePreview | ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoAlpha);
  1898. // HSV encoded support (to avoid RGB<>HSV round trips and singularities when S==0 or V==0)
  1899. static ImVec4 color_hsv(0.23f, 1.0f, 1.0f, 1.0f); // Stored as HSV!
  1900. ImGui::Spacing();
  1901. ImGui::Text("HSV encoded colors");
  1902. ImGui::SameLine(); HelpMarker(
  1903. "By default, colors are given to ColorEdit and ColorPicker in RGB, but ImGuiColorEditFlags_InputHSV"
  1904. "allows you to store colors as HSV and pass them to ColorEdit and ColorPicker as HSV. This comes with the"
  1905. "added benefit that you can manipulate hue values with the picker even when saturation or value are zero.");
  1906. ImGui::Text("Color widget with InputHSV:");
  1907. ImGui::ColorEdit4("HSV shown as RGB##1", (float*)&color_hsv, ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_InputHSV | ImGuiColorEditFlags_Float);
  1908. ImGui::ColorEdit4("HSV shown as HSV##1", (float*)&color_hsv, ImGuiColorEditFlags_DisplayHSV | ImGuiColorEditFlags_InputHSV | ImGuiColorEditFlags_Float);
  1909. ImGui::DragFloat4("Raw HSV values", (float*)&color_hsv, 0.01f, 0.0f, 1.0f);
  1910. ImGui::TreePop();
  1911. }
  1912. IMGUI_DEMO_MARKER("Widgets/Drag and Slider Flags");
  1913. if (ImGui::TreeNode("Drag/Slider Flags"))
  1914. {
  1915. // Demonstrate using advanced flags for DragXXX and SliderXXX functions. Note that the flags are the same!
  1916. static ImGuiSliderFlags flags = ImGuiSliderFlags_None;
  1917. ImGui::CheckboxFlags("ImGuiSliderFlags_AlwaysClamp", &flags, ImGuiSliderFlags_AlwaysClamp);
  1918. ImGui::SameLine(); HelpMarker("Always clamp value to min/max bounds (if any) when input manually with CTRL+Click.");
  1919. ImGui::CheckboxFlags("ImGuiSliderFlags_Logarithmic", &flags, ImGuiSliderFlags_Logarithmic);
  1920. ImGui::SameLine(); HelpMarker("Enable logarithmic editing (more precision for small values).");
  1921. ImGui::CheckboxFlags("ImGuiSliderFlags_NoRoundToFormat", &flags, ImGuiSliderFlags_NoRoundToFormat);
  1922. ImGui::SameLine(); HelpMarker("Disable rounding underlying value to match precision of the format string (e.g. %.3f values are rounded to those 3 digits).");
  1923. ImGui::CheckboxFlags("ImGuiSliderFlags_NoInput", &flags, ImGuiSliderFlags_NoInput);
  1924. ImGui::SameLine(); HelpMarker("Disable CTRL+Click or Enter key allowing to input text directly into the widget.");
  1925. // Drags
  1926. static float drag_f = 0.5f;
  1927. static int drag_i = 50;
  1928. ImGui::Text("Underlying float value: %f", drag_f);
  1929. ImGui::DragFloat("DragFloat (0 -> 1)", &drag_f, 0.005f, 0.0f, 1.0f, "%.3f", flags);
  1930. ImGui::DragFloat("DragFloat (0 -> +inf)", &drag_f, 0.005f, 0.0f, FLT_MAX, "%.3f", flags);
  1931. ImGui::DragFloat("DragFloat (-inf -> 1)", &drag_f, 0.005f, -FLT_MAX, 1.0f, "%.3f", flags);
  1932. ImGui::DragFloat("DragFloat (-inf -> +inf)", &drag_f, 0.005f, -FLT_MAX, +FLT_MAX, "%.3f", flags);
  1933. ImGui::DragInt("DragInt (0 -> 100)", &drag_i, 0.5f, 0, 100, "%d", flags);
  1934. // Sliders
  1935. static float slider_f = 0.5f;
  1936. static int slider_i = 50;
  1937. ImGui::Text("Underlying float value: %f", slider_f);
  1938. ImGui::SliderFloat("SliderFloat (0 -> 1)", &slider_f, 0.0f, 1.0f, "%.3f", flags);
  1939. ImGui::SliderInt("SliderInt (0 -> 100)", &slider_i, 0, 100, "%d", flags);
  1940. ImGui::TreePop();
  1941. }
  1942. IMGUI_DEMO_MARKER("Widgets/Range Widgets");
  1943. if (ImGui::TreeNode("Range Widgets"))
  1944. {
  1945. static float begin = 10, end = 90;
  1946. static int begin_i = 100, end_i = 1000;
  1947. ImGui::DragFloatRange2("range float", &begin, &end, 0.25f, 0.0f, 100.0f, "Min: %.1f %%", "Max: %.1f %%", ImGuiSliderFlags_AlwaysClamp);
  1948. ImGui::DragIntRange2("range int", &begin_i, &end_i, 5, 0, 1000, "Min: %d units", "Max: %d units");
  1949. ImGui::DragIntRange2("range int (no bounds)", &begin_i, &end_i, 5, 0, 0, "Min: %d units", "Max: %d units");
  1950. ImGui::TreePop();
  1951. }
  1952. IMGUI_DEMO_MARKER("Widgets/Data Types");
  1953. if (ImGui::TreeNode("Data Types"))
  1954. {
  1955. // DragScalar/InputScalar/SliderScalar functions allow various data types
  1956. // - signed/unsigned
  1957. // - 8/16/32/64-bits
  1958. // - integer/float/double
  1959. // To avoid polluting the public API with all possible combinations, we use the ImGuiDataType enum
  1960. // to pass the type, and passing all arguments by pointer.
  1961. // This is the reason the test code below creates local variables to hold "zero" "one" etc. for each type.
  1962. // In practice, if you frequently use a given type that is not covered by the normal API entry points,
  1963. // you can wrap it yourself inside a 1 line function which can take typed argument as value instead of void*,
  1964. // and then pass their address to the generic function. For example:
  1965. // bool MySliderU64(const char *label, u64* value, u64 min = 0, u64 max = 0, const char* format = "%lld")
  1966. // {
  1967. // return SliderScalar(label, ImGuiDataType_U64, value, &min, &max, format);
  1968. // }
  1969. // Setup limits (as helper variables so we can take their address, as explained above)
  1970. // Note: SliderScalar() functions have a maximum usable range of half the natural type maximum, hence the /2.
  1971. #ifndef LLONG_MIN
  1972. ImS64 LLONG_MIN = -9223372036854775807LL - 1;
  1973. ImS64 LLONG_MAX = 9223372036854775807LL;
  1974. ImU64 ULLONG_MAX = (2ULL * 9223372036854775807LL + 1);
  1975. #endif
  1976. const char s8_zero = 0, s8_one = 1, s8_fifty = 50, s8_min = -128, s8_max = 127;
  1977. const ImU8 u8_zero = 0, u8_one = 1, u8_fifty = 50, u8_min = 0, u8_max = 255;
  1978. const short s16_zero = 0, s16_one = 1, s16_fifty = 50, s16_min = -32768, s16_max = 32767;
  1979. const ImU16 u16_zero = 0, u16_one = 1, u16_fifty = 50, u16_min = 0, u16_max = 65535;
  1980. const ImS32 s32_zero = 0, s32_one = 1, s32_fifty = 50, s32_min = INT_MIN/2, s32_max = INT_MAX/2, s32_hi_a = INT_MAX/2 - 100, s32_hi_b = INT_MAX/2;
  1981. const ImU32 u32_zero = 0, u32_one = 1, u32_fifty = 50, u32_min = 0, u32_max = UINT_MAX/2, u32_hi_a = UINT_MAX/2 - 100, u32_hi_b = UINT_MAX/2;
  1982. const ImS64 s64_zero = 0, s64_one = 1, s64_fifty = 50, s64_min = LLONG_MIN/2, s64_max = LLONG_MAX/2, s64_hi_a = LLONG_MAX/2 - 100, s64_hi_b = LLONG_MAX/2;
  1983. const ImU64 u64_zero = 0, u64_one = 1, u64_fifty = 50, u64_min = 0, u64_max = ULLONG_MAX/2, u64_hi_a = ULLONG_MAX/2 - 100, u64_hi_b = ULLONG_MAX/2;
  1984. const float f32_zero = 0.f, f32_one = 1.f, f32_lo_a = -10000000000.0f, f32_hi_a = +10000000000.0f;
  1985. const double f64_zero = 0., f64_one = 1., f64_lo_a = -1000000000000000.0, f64_hi_a = +1000000000000000.0;
  1986. // State
  1987. static char s8_v = 127;
  1988. static ImU8 u8_v = 255;
  1989. static short s16_v = 32767;
  1990. static ImU16 u16_v = 65535;
  1991. static ImS32 s32_v = -1;
  1992. static ImU32 u32_v = (ImU32)-1;
  1993. static ImS64 s64_v = -1;
  1994. static ImU64 u64_v = (ImU64)-1;
  1995. static float f32_v = 0.123f;
  1996. static double f64_v = 90000.01234567890123456789;
  1997. const float drag_speed = 0.2f;
  1998. static bool drag_clamp = false;
  1999. IMGUI_DEMO_MARKER("Widgets/Data Types/Drags");
  2000. ImGui::SeparatorText("Drags");
  2001. ImGui::Checkbox("Clamp integers to 0..50", &drag_clamp);
  2002. ImGui::SameLine(); HelpMarker(
  2003. "As with every widget in dear imgui, we never modify values unless there is a user interaction.\n"
  2004. "You can override the clamping limits by using CTRL+Click to input a value.");
  2005. ImGui::DragScalar("drag s8", ImGuiDataType_S8, &s8_v, drag_speed, drag_clamp ? &s8_zero : NULL, drag_clamp ? &s8_fifty : NULL);
  2006. ImGui::DragScalar("drag u8", ImGuiDataType_U8, &u8_v, drag_speed, drag_clamp ? &u8_zero : NULL, drag_clamp ? &u8_fifty : NULL, "%u ms");
  2007. ImGui::DragScalar("drag s16", ImGuiDataType_S16, &s16_v, drag_speed, drag_clamp ? &s16_zero : NULL, drag_clamp ? &s16_fifty : NULL);
  2008. ImGui::DragScalar("drag u16", ImGuiDataType_U16, &u16_v, drag_speed, drag_clamp ? &u16_zero : NULL, drag_clamp ? &u16_fifty : NULL, "%u ms");
  2009. ImGui::DragScalar("drag s32", ImGuiDataType_S32, &s32_v, drag_speed, drag_clamp ? &s32_zero : NULL, drag_clamp ? &s32_fifty : NULL);
  2010. ImGui::DragScalar("drag s32 hex", ImGuiDataType_S32, &s32_v, drag_speed, drag_clamp ? &s32_zero : NULL, drag_clamp ? &s32_fifty : NULL, "0x%08X");
  2011. ImGui::DragScalar("drag u32", ImGuiDataType_U32, &u32_v, drag_speed, drag_clamp ? &u32_zero : NULL, drag_clamp ? &u32_fifty : NULL, "%u ms");
  2012. ImGui::DragScalar("drag s64", ImGuiDataType_S64, &s64_v, drag_speed, drag_clamp ? &s64_zero : NULL, drag_clamp ? &s64_fifty : NULL);
  2013. ImGui::DragScalar("drag u64", ImGuiDataType_U64, &u64_v, drag_speed, drag_clamp ? &u64_zero : NULL, drag_clamp ? &u64_fifty : NULL);
  2014. ImGui::DragScalar("drag float", ImGuiDataType_Float, &f32_v, 0.005f, &f32_zero, &f32_one, "%f");
  2015. ImGui::DragScalar("drag float log", ImGuiDataType_Float, &f32_v, 0.005f, &f32_zero, &f32_one, "%f", ImGuiSliderFlags_Logarithmic);
  2016. ImGui::DragScalar("drag double", ImGuiDataType_Double, &f64_v, 0.0005f, &f64_zero, NULL, "%.10f grams");
  2017. ImGui::DragScalar("drag double log",ImGuiDataType_Double, &f64_v, 0.0005f, &f64_zero, &f64_one, "0 < %.10f < 1", ImGuiSliderFlags_Logarithmic);
  2018. IMGUI_DEMO_MARKER("Widgets/Data Types/Sliders");
  2019. ImGui::SeparatorText("Sliders");
  2020. ImGui::SliderScalar("slider s8 full", ImGuiDataType_S8, &s8_v, &s8_min, &s8_max, "%d");
  2021. ImGui::SliderScalar("slider u8 full", ImGuiDataType_U8, &u8_v, &u8_min, &u8_max, "%u");
  2022. ImGui::SliderScalar("slider s16 full", ImGuiDataType_S16, &s16_v, &s16_min, &s16_max, "%d");
  2023. ImGui::SliderScalar("slider u16 full", ImGuiDataType_U16, &u16_v, &u16_min, &u16_max, "%u");
  2024. ImGui::SliderScalar("slider s32 low", ImGuiDataType_S32, &s32_v, &s32_zero, &s32_fifty,"%d");
  2025. ImGui::SliderScalar("slider s32 high", ImGuiDataType_S32, &s32_v, &s32_hi_a, &s32_hi_b, "%d");
  2026. ImGui::SliderScalar("slider s32 full", ImGuiDataType_S32, &s32_v, &s32_min, &s32_max, "%d");
  2027. ImGui::SliderScalar("slider s32 hex", ImGuiDataType_S32, &s32_v, &s32_zero, &s32_fifty, "0x%04X");
  2028. ImGui::SliderScalar("slider u32 low", ImGuiDataType_U32, &u32_v, &u32_zero, &u32_fifty,"%u");
  2029. ImGui::SliderScalar("slider u32 high", ImGuiDataType_U32, &u32_v, &u32_hi_a, &u32_hi_b, "%u");
  2030. ImGui::SliderScalar("slider u32 full", ImGuiDataType_U32, &u32_v, &u32_min, &u32_max, "%u");
  2031. ImGui::SliderScalar("slider s64 low", ImGuiDataType_S64, &s64_v, &s64_zero, &s64_fifty,"%" PRId64);
  2032. ImGui::SliderScalar("slider s64 high", ImGuiDataType_S64, &s64_v, &s64_hi_a, &s64_hi_b, "%" PRId64);
  2033. ImGui::SliderScalar("slider s64 full", ImGuiDataType_S64, &s64_v, &s64_min, &s64_max, "%" PRId64);
  2034. ImGui::SliderScalar("slider u64 low", ImGuiDataType_U64, &u64_v, &u64_zero, &u64_fifty,"%" PRIu64 " ms");
  2035. ImGui::SliderScalar("slider u64 high", ImGuiDataType_U64, &u64_v, &u64_hi_a, &u64_hi_b, "%" PRIu64 " ms");
  2036. ImGui::SliderScalar("slider u64 full", ImGuiDataType_U64, &u64_v, &u64_min, &u64_max, "%" PRIu64 " ms");
  2037. ImGui::SliderScalar("slider float low", ImGuiDataType_Float, &f32_v, &f32_zero, &f32_one);
  2038. ImGui::SliderScalar("slider float low log", ImGuiDataType_Float, &f32_v, &f32_zero, &f32_one, "%.10f", ImGuiSliderFlags_Logarithmic);
  2039. ImGui::SliderScalar("slider float high", ImGuiDataType_Float, &f32_v, &f32_lo_a, &f32_hi_a, "%e");
  2040. ImGui::SliderScalar("slider double low", ImGuiDataType_Double, &f64_v, &f64_zero, &f64_one, "%.10f grams");
  2041. ImGui::SliderScalar("slider double low log",ImGuiDataType_Double, &f64_v, &f64_zero, &f64_one, "%.10f", ImGuiSliderFlags_Logarithmic);
  2042. ImGui::SliderScalar("slider double high", ImGuiDataType_Double, &f64_v, &f64_lo_a, &f64_hi_a, "%e grams");
  2043. ImGui::SeparatorText("Sliders (reverse)");
  2044. ImGui::SliderScalar("slider s8 reverse", ImGuiDataType_S8, &s8_v, &s8_max, &s8_min, "%d");
  2045. ImGui::SliderScalar("slider u8 reverse", ImGuiDataType_U8, &u8_v, &u8_max, &u8_min, "%u");
  2046. ImGui::SliderScalar("slider s32 reverse", ImGuiDataType_S32, &s32_v, &s32_fifty, &s32_zero, "%d");
  2047. ImGui::SliderScalar("slider u32 reverse", ImGuiDataType_U32, &u32_v, &u32_fifty, &u32_zero, "%u");
  2048. ImGui::SliderScalar("slider s64 reverse", ImGuiDataType_S64, &s64_v, &s64_fifty, &s64_zero, "%" PRId64);
  2049. ImGui::SliderScalar("slider u64 reverse", ImGuiDataType_U64, &u64_v, &u64_fifty, &u64_zero, "%" PRIu64 " ms");
  2050. IMGUI_DEMO_MARKER("Widgets/Data Types/Inputs");
  2051. static bool inputs_step = true;
  2052. static ImGuiInputTextFlags flags = ImGuiInputTextFlags_None;
  2053. ImGui::SeparatorText("Inputs");
  2054. ImGui::Checkbox("Show step buttons", &inputs_step);
  2055. ImGui::CheckboxFlags("ImGuiInputTextFlags_ReadOnly", &flags, ImGuiInputTextFlags_ReadOnly);
  2056. ImGui::CheckboxFlags("ImGuiInputTextFlags_ParseEmptyRefVal", &flags, ImGuiInputTextFlags_ParseEmptyRefVal);
  2057. ImGui::CheckboxFlags("ImGuiInputTextFlags_DisplayEmptyRefVal", &flags, ImGuiInputTextFlags_DisplayEmptyRefVal);
  2058. ImGui::InputScalar("input s8", ImGuiDataType_S8, &s8_v, inputs_step ? &s8_one : NULL, NULL, "%d", flags);
  2059. ImGui::InputScalar("input u8", ImGuiDataType_U8, &u8_v, inputs_step ? &u8_one : NULL, NULL, "%u", flags);
  2060. ImGui::InputScalar("input s16", ImGuiDataType_S16, &s16_v, inputs_step ? &s16_one : NULL, NULL, "%d", flags);
  2061. ImGui::InputScalar("input u16", ImGuiDataType_U16, &u16_v, inputs_step ? &u16_one : NULL, NULL, "%u", flags);
  2062. ImGui::InputScalar("input s32", ImGuiDataType_S32, &s32_v, inputs_step ? &s32_one : NULL, NULL, "%d", flags);
  2063. ImGui::InputScalar("input s32 hex", ImGuiDataType_S32, &s32_v, inputs_step ? &s32_one : NULL, NULL, "%04X", flags);
  2064. ImGui::InputScalar("input u32", ImGuiDataType_U32, &u32_v, inputs_step ? &u32_one : NULL, NULL, "%u", flags);
  2065. ImGui::InputScalar("input u32 hex", ImGuiDataType_U32, &u32_v, inputs_step ? &u32_one : NULL, NULL, "%08X", flags);
  2066. ImGui::InputScalar("input s64", ImGuiDataType_S64, &s64_v, inputs_step ? &s64_one : NULL, NULL, NULL, flags);
  2067. ImGui::InputScalar("input u64", ImGuiDataType_U64, &u64_v, inputs_step ? &u64_one : NULL, NULL, NULL, flags);
  2068. ImGui::InputScalar("input float", ImGuiDataType_Float, &f32_v, inputs_step ? &f32_one : NULL, NULL, NULL, flags);
  2069. ImGui::InputScalar("input double", ImGuiDataType_Double, &f64_v, inputs_step ? &f64_one : NULL, NULL, NULL, flags);
  2070. ImGui::TreePop();
  2071. }
  2072. IMGUI_DEMO_MARKER("Widgets/Multi-component Widgets");
  2073. if (ImGui::TreeNode("Multi-component Widgets"))
  2074. {
  2075. static float vec4f[4] = { 0.10f, 0.20f, 0.30f, 0.44f };
  2076. static int vec4i[4] = { 1, 5, 100, 255 };
  2077. ImGui::SeparatorText("2-wide");
  2078. ImGui::InputFloat2("input float2", vec4f);
  2079. ImGui::DragFloat2("drag float2", vec4f, 0.01f, 0.0f, 1.0f);
  2080. ImGui::SliderFloat2("slider float2", vec4f, 0.0f, 1.0f);
  2081. ImGui::InputInt2("input int2", vec4i);
  2082. ImGui::DragInt2("drag int2", vec4i, 1, 0, 255);
  2083. ImGui::SliderInt2("slider int2", vec4i, 0, 255);
  2084. ImGui::SeparatorText("3-wide");
  2085. ImGui::InputFloat3("input float3", vec4f);
  2086. ImGui::DragFloat3("drag float3", vec4f, 0.01f, 0.0f, 1.0f);
  2087. ImGui::SliderFloat3("slider float3", vec4f, 0.0f, 1.0f);
  2088. ImGui::InputInt3("input int3", vec4i);
  2089. ImGui::DragInt3("drag int3", vec4i, 1, 0, 255);
  2090. ImGui::SliderInt3("slider int3", vec4i, 0, 255);
  2091. ImGui::SeparatorText("4-wide");
  2092. ImGui::InputFloat4("input float4", vec4f);
  2093. ImGui::DragFloat4("drag float4", vec4f, 0.01f, 0.0f, 1.0f);
  2094. ImGui::SliderFloat4("slider float4", vec4f, 0.0f, 1.0f);
  2095. ImGui::InputInt4("input int4", vec4i);
  2096. ImGui::DragInt4("drag int4", vec4i, 1, 0, 255);
  2097. ImGui::SliderInt4("slider int4", vec4i, 0, 255);
  2098. ImGui::TreePop();
  2099. }
  2100. IMGUI_DEMO_MARKER("Widgets/Vertical Sliders");
  2101. if (ImGui::TreeNode("Vertical Sliders"))
  2102. {
  2103. const float spacing = 4;
  2104. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(spacing, spacing));
  2105. static int int_value = 0;
  2106. ImGui::VSliderInt("##int", ImVec2(18, 160), &int_value, 0, 5);
  2107. ImGui::SameLine();
  2108. static float values[7] = { 0.0f, 0.60f, 0.35f, 0.9f, 0.70f, 0.20f, 0.0f };
  2109. ImGui::PushID("set1");
  2110. for (int i = 0; i < 7; i++)
  2111. {
  2112. if (i > 0) ImGui::SameLine();
  2113. ImGui::PushID(i);
  2114. ImGui::PushStyleColor(ImGuiCol_FrameBg, (ImVec4)ImColor::HSV(i / 7.0f, 0.5f, 0.5f));
  2115. ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, (ImVec4)ImColor::HSV(i / 7.0f, 0.6f, 0.5f));
  2116. ImGui::PushStyleColor(ImGuiCol_FrameBgActive, (ImVec4)ImColor::HSV(i / 7.0f, 0.7f, 0.5f));
  2117. ImGui::PushStyleColor(ImGuiCol_SliderGrab, (ImVec4)ImColor::HSV(i / 7.0f, 0.9f, 0.9f));
  2118. ImGui::VSliderFloat("##v", ImVec2(18, 160), &values[i], 0.0f, 1.0f, "");
  2119. if (ImGui::IsItemActive() || ImGui::IsItemHovered())
  2120. ImGui::SetTooltip("%.3f", values[i]);
  2121. ImGui::PopStyleColor(4);
  2122. ImGui::PopID();
  2123. }
  2124. ImGui::PopID();
  2125. ImGui::SameLine();
  2126. ImGui::PushID("set2");
  2127. static float values2[4] = { 0.20f, 0.80f, 0.40f, 0.25f };
  2128. const int rows = 3;
  2129. const ImVec2 small_slider_size(18, (float)(int)((160.0f - (rows - 1) * spacing) / rows));
  2130. for (int nx = 0; nx < 4; nx++)
  2131. {
  2132. if (nx > 0) ImGui::SameLine();
  2133. ImGui::BeginGroup();
  2134. for (int ny = 0; ny < rows; ny++)
  2135. {
  2136. ImGui::PushID(nx * rows + ny);
  2137. ImGui::VSliderFloat("##v", small_slider_size, &values2[nx], 0.0f, 1.0f, "");
  2138. if (ImGui::IsItemActive() || ImGui::IsItemHovered())
  2139. ImGui::SetTooltip("%.3f", values2[nx]);
  2140. ImGui::PopID();
  2141. }
  2142. ImGui::EndGroup();
  2143. }
  2144. ImGui::PopID();
  2145. ImGui::SameLine();
  2146. ImGui::PushID("set3");
  2147. for (int i = 0; i < 4; i++)
  2148. {
  2149. if (i > 0) ImGui::SameLine();
  2150. ImGui::PushID(i);
  2151. ImGui::PushStyleVar(ImGuiStyleVar_GrabMinSize, 40);
  2152. ImGui::VSliderFloat("##v", ImVec2(40, 160), &values[i], 0.0f, 1.0f, "%.2f\nsec");
  2153. ImGui::PopStyleVar();
  2154. ImGui::PopID();
  2155. }
  2156. ImGui::PopID();
  2157. ImGui::PopStyleVar();
  2158. ImGui::TreePop();
  2159. }
  2160. IMGUI_DEMO_MARKER("Widgets/Drag and drop");
  2161. if (ImGui::TreeNode("Drag and Drop"))
  2162. {
  2163. IMGUI_DEMO_MARKER("Widgets/Drag and drop/Standard widgets");
  2164. if (ImGui::TreeNode("Drag and drop in standard widgets"))
  2165. {
  2166. // ColorEdit widgets automatically act as drag source and drag target.
  2167. // They are using standardized payload strings IMGUI_PAYLOAD_TYPE_COLOR_3F and IMGUI_PAYLOAD_TYPE_COLOR_4F
  2168. // to allow your own widgets to use colors in their drag and drop interaction.
  2169. // Also see 'Demo->Widgets->Color/Picker Widgets->Palette' demo.
  2170. HelpMarker("You can drag from the color squares.");
  2171. static float col1[3] = { 1.0f, 0.0f, 0.2f };
  2172. static float col2[4] = { 0.4f, 0.7f, 0.0f, 0.5f };
  2173. ImGui::ColorEdit3("color 1", col1);
  2174. ImGui::ColorEdit4("color 2", col2);
  2175. ImGui::TreePop();
  2176. }
  2177. IMGUI_DEMO_MARKER("Widgets/Drag and drop/Copy-swap items");
  2178. if (ImGui::TreeNode("Drag and drop to copy/swap items"))
  2179. {
  2180. enum Mode
  2181. {
  2182. Mode_Copy,
  2183. Mode_Move,
  2184. Mode_Swap
  2185. };
  2186. static int mode = 0;
  2187. if (ImGui::RadioButton("Copy", mode == Mode_Copy)) { mode = Mode_Copy; } ImGui::SameLine();
  2188. if (ImGui::RadioButton("Move", mode == Mode_Move)) { mode = Mode_Move; } ImGui::SameLine();
  2189. if (ImGui::RadioButton("Swap", mode == Mode_Swap)) { mode = Mode_Swap; }
  2190. static const char* names[9] =
  2191. {
  2192. "Bobby", "Beatrice", "Betty",
  2193. "Brianna", "Barry", "Bernard",
  2194. "Bibi", "Blaine", "Bryn"
  2195. };
  2196. for (int n = 0; n < IM_ARRAYSIZE(names); n++)
  2197. {
  2198. ImGui::PushID(n);
  2199. if ((n % 3) != 0)
  2200. ImGui::SameLine();
  2201. ImGui::Button(names[n], ImVec2(60, 60));
  2202. // Our buttons are both drag sources and drag targets here!
  2203. if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None))
  2204. {
  2205. // Set payload to carry the index of our item (could be anything)
  2206. ImGui::SetDragDropPayload("DND_DEMO_CELL", &n, sizeof(int));
  2207. // Display preview (could be anything, e.g. when dragging an image we could decide to display
  2208. // the filename and a small preview of the image, etc.)
  2209. if (mode == Mode_Copy) { ImGui::Text("Copy %s", names[n]); }
  2210. if (mode == Mode_Move) { ImGui::Text("Move %s", names[n]); }
  2211. if (mode == Mode_Swap) { ImGui::Text("Swap %s", names[n]); }
  2212. ImGui::EndDragDropSource();
  2213. }
  2214. if (ImGui::BeginDragDropTarget())
  2215. {
  2216. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("DND_DEMO_CELL"))
  2217. {
  2218. IM_ASSERT(payload->DataSize == sizeof(int));
  2219. int payload_n = *(const int*)payload->Data;
  2220. if (mode == Mode_Copy)
  2221. {
  2222. names[n] = names[payload_n];
  2223. }
  2224. if (mode == Mode_Move)
  2225. {
  2226. names[n] = names[payload_n];
  2227. names[payload_n] = "";
  2228. }
  2229. if (mode == Mode_Swap)
  2230. {
  2231. const char* tmp = names[n];
  2232. names[n] = names[payload_n];
  2233. names[payload_n] = tmp;
  2234. }
  2235. }
  2236. ImGui::EndDragDropTarget();
  2237. }
  2238. ImGui::PopID();
  2239. }
  2240. ImGui::TreePop();
  2241. }
  2242. IMGUI_DEMO_MARKER("Widgets/Drag and Drop/Drag to reorder items (simple)");
  2243. if (ImGui::TreeNode("Drag to reorder items (simple)"))
  2244. {
  2245. // Simple reordering
  2246. HelpMarker(
  2247. "We don't use the drag and drop api at all here! "
  2248. "Instead we query when the item is held but not hovered, and order items accordingly.");
  2249. static const char* item_names[] = { "Item One", "Item Two", "Item Three", "Item Four", "Item Five" };
  2250. for (int n = 0; n < IM_ARRAYSIZE(item_names); n++)
  2251. {
  2252. const char* item = item_names[n];
  2253. ImGui::Selectable(item);
  2254. if (ImGui::IsItemActive() && !ImGui::IsItemHovered())
  2255. {
  2256. int n_next = n + (ImGui::GetMouseDragDelta(0).y < 0.f ? -1 : 1);
  2257. if (n_next >= 0 && n_next < IM_ARRAYSIZE(item_names))
  2258. {
  2259. item_names[n] = item_names[n_next];
  2260. item_names[n_next] = item;
  2261. ImGui::ResetMouseDragDelta();
  2262. }
  2263. }
  2264. }
  2265. ImGui::TreePop();
  2266. }
  2267. IMGUI_DEMO_MARKER("Widgets/Drag and Drop/Tooltip at target location");
  2268. if (ImGui::TreeNode("Tooltip at target location"))
  2269. {
  2270. for (int n = 0; n < 2; n++)
  2271. {
  2272. // Drop targets
  2273. ImGui::Button(n ? "drop here##1" : "drop here##0");
  2274. if (ImGui::BeginDragDropTarget())
  2275. {
  2276. ImGuiDragDropFlags drop_target_flags = ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoPreviewTooltip;
  2277. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_4F, drop_target_flags))
  2278. {
  2279. IM_UNUSED(payload);
  2280. ImGui::SetMouseCursor(ImGuiMouseCursor_NotAllowed);
  2281. ImGui::SetTooltip("Cannot drop here!");
  2282. }
  2283. ImGui::EndDragDropTarget();
  2284. }
  2285. // Drop source
  2286. static ImVec4 col4 = { 1.0f, 0.0f, 0.2f, 1.0f };
  2287. if (n == 0)
  2288. ImGui::ColorButton("drag me", col4);
  2289. }
  2290. ImGui::TreePop();
  2291. }
  2292. ImGui::TreePop();
  2293. }
  2294. IMGUI_DEMO_MARKER("Widgets/Querying Item Status (Edited,Active,Hovered etc.)");
  2295. if (ImGui::TreeNode("Querying Item Status (Edited/Active/Hovered etc.)"))
  2296. {
  2297. // Select an item type
  2298. const char* item_names[] =
  2299. {
  2300. "Text", "Button", "Button (w/ repeat)", "Checkbox", "SliderFloat", "InputText", "InputTextMultiline", "InputFloat",
  2301. "InputFloat3", "ColorEdit4", "Selectable", "MenuItem", "TreeNode", "TreeNode (w/ double-click)", "Combo", "ListBox"
  2302. };
  2303. static int item_type = 4;
  2304. static bool item_disabled = false;
  2305. ImGui::Combo("Item Type", &item_type, item_names, IM_ARRAYSIZE(item_names), IM_ARRAYSIZE(item_names));
  2306. ImGui::SameLine();
  2307. HelpMarker("Testing how various types of items are interacting with the IsItemXXX functions. Note that the bool return value of most ImGui function is generally equivalent to calling ImGui::IsItemHovered().");
  2308. ImGui::Checkbox("Item Disabled", &item_disabled);
  2309. // Submit selected items so we can query their status in the code following it.
  2310. bool ret = false;
  2311. static bool b = false;
  2312. static float col4f[4] = { 1.0f, 0.5, 0.0f, 1.0f };
  2313. static char str[16] = {};
  2314. if (item_disabled)
  2315. ImGui::BeginDisabled(true);
  2316. if (item_type == 0) { ImGui::Text("ITEM: Text"); } // Testing text items with no identifier/interaction
  2317. if (item_type == 1) { ret = ImGui::Button("ITEM: Button"); } // Testing button
  2318. if (item_type == 2) { ImGui::PushButtonRepeat(true); ret = ImGui::Button("ITEM: Button"); ImGui::PopButtonRepeat(); } // Testing button (with repeater)
  2319. if (item_type == 3) { ret = ImGui::Checkbox("ITEM: Checkbox", &b); } // Testing checkbox
  2320. if (item_type == 4) { ret = ImGui::SliderFloat("ITEM: SliderFloat", &col4f[0], 0.0f, 1.0f); } // Testing basic item
  2321. if (item_type == 5) { ret = ImGui::InputText("ITEM: InputText", &str[0], IM_ARRAYSIZE(str)); } // Testing input text (which handles tabbing)
  2322. if (item_type == 6) { ret = ImGui::InputTextMultiline("ITEM: InputTextMultiline", &str[0], IM_ARRAYSIZE(str)); } // Testing input text (which uses a child window)
  2323. if (item_type == 7) { ret = ImGui::InputFloat("ITEM: InputFloat", col4f, 1.0f); } // Testing +/- buttons on scalar input
  2324. if (item_type == 8) { ret = ImGui::InputFloat3("ITEM: InputFloat3", col4f); } // Testing multi-component items (IsItemXXX flags are reported merged)
  2325. if (item_type == 9) { ret = ImGui::ColorEdit4("ITEM: ColorEdit4", col4f); } // Testing multi-component items (IsItemXXX flags are reported merged)
  2326. if (item_type == 10){ ret = ImGui::Selectable("ITEM: Selectable"); } // Testing selectable item
  2327. if (item_type == 11){ ret = ImGui::MenuItem("ITEM: MenuItem"); } // Testing menu item (they use ImGuiButtonFlags_PressedOnRelease button policy)
  2328. if (item_type == 12){ ret = ImGui::TreeNode("ITEM: TreeNode"); if (ret) ImGui::TreePop(); } // Testing tree node
  2329. if (item_type == 13){ ret = ImGui::TreeNodeEx("ITEM: TreeNode w/ ImGuiTreeNodeFlags_OpenOnDoubleClick", ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_NoTreePushOnOpen); } // Testing tree node with ImGuiButtonFlags_PressedOnDoubleClick button policy.
  2330. if (item_type == 14){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::Combo("ITEM: Combo", &current, items, IM_ARRAYSIZE(items)); }
  2331. if (item_type == 15){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::ListBox("ITEM: ListBox", &current, items, IM_ARRAYSIZE(items), IM_ARRAYSIZE(items)); }
  2332. bool hovered_delay_none = ImGui::IsItemHovered();
  2333. bool hovered_delay_stationary = ImGui::IsItemHovered(ImGuiHoveredFlags_Stationary);
  2334. bool hovered_delay_short = ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort);
  2335. bool hovered_delay_normal = ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNormal);
  2336. bool hovered_delay_tooltip = ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip); // = Normal + Stationary
  2337. // Display the values of IsItemHovered() and other common item state functions.
  2338. // Note that the ImGuiHoveredFlags_XXX flags can be combined.
  2339. // Because BulletText is an item itself and that would affect the output of IsItemXXX functions,
  2340. // we query every state in a single call to avoid storing them and to simplify the code.
  2341. ImGui::BulletText(
  2342. "Return value = %d\n"
  2343. "IsItemFocused() = %d\n"
  2344. "IsItemHovered() = %d\n"
  2345. "IsItemHovered(_AllowWhenBlockedByPopup) = %d\n"
  2346. "IsItemHovered(_AllowWhenBlockedByActiveItem) = %d\n"
  2347. "IsItemHovered(_AllowWhenOverlappedByItem) = %d\n"
  2348. "IsItemHovered(_AllowWhenOverlappedByWindow) = %d\n"
  2349. "IsItemHovered(_AllowWhenDisabled) = %d\n"
  2350. "IsItemHovered(_RectOnly) = %d\n"
  2351. "IsItemActive() = %d\n"
  2352. "IsItemEdited() = %d\n"
  2353. "IsItemActivated() = %d\n"
  2354. "IsItemDeactivated() = %d\n"
  2355. "IsItemDeactivatedAfterEdit() = %d\n"
  2356. "IsItemVisible() = %d\n"
  2357. "IsItemClicked() = %d\n"
  2358. "IsItemToggledOpen() = %d\n"
  2359. "GetItemRectMin() = (%.1f, %.1f)\n"
  2360. "GetItemRectMax() = (%.1f, %.1f)\n"
  2361. "GetItemRectSize() = (%.1f, %.1f)",
  2362. ret,
  2363. ImGui::IsItemFocused(),
  2364. ImGui::IsItemHovered(),
  2365. ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup),
  2366. ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem),
  2367. ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenOverlappedByItem),
  2368. ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenOverlappedByWindow),
  2369. ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled),
  2370. ImGui::IsItemHovered(ImGuiHoveredFlags_RectOnly),
  2371. ImGui::IsItemActive(),
  2372. ImGui::IsItemEdited(),
  2373. ImGui::IsItemActivated(),
  2374. ImGui::IsItemDeactivated(),
  2375. ImGui::IsItemDeactivatedAfterEdit(),
  2376. ImGui::IsItemVisible(),
  2377. ImGui::IsItemClicked(),
  2378. ImGui::IsItemToggledOpen(),
  2379. ImGui::GetItemRectMin().x, ImGui::GetItemRectMin().y,
  2380. ImGui::GetItemRectMax().x, ImGui::GetItemRectMax().y,
  2381. ImGui::GetItemRectSize().x, ImGui::GetItemRectSize().y
  2382. );
  2383. ImGui::BulletText(
  2384. "with Hovering Delay or Stationary test:\n"
  2385. "IsItemHovered() = = %d\n"
  2386. "IsItemHovered(_Stationary) = %d\n"
  2387. "IsItemHovered(_DelayShort) = %d\n"
  2388. "IsItemHovered(_DelayNormal) = %d\n"
  2389. "IsItemHovered(_Tooltip) = %d",
  2390. hovered_delay_none, hovered_delay_stationary, hovered_delay_short, hovered_delay_normal, hovered_delay_tooltip);
  2391. if (item_disabled)
  2392. ImGui::EndDisabled();
  2393. char buf[1] = "";
  2394. ImGui::InputText("unused", buf, IM_ARRAYSIZE(buf), ImGuiInputTextFlags_ReadOnly);
  2395. ImGui::SameLine();
  2396. HelpMarker("This widget is only here to be able to tab-out of the widgets above and see e.g. Deactivated() status.");
  2397. ImGui::TreePop();
  2398. }
  2399. IMGUI_DEMO_MARKER("Widgets/Querying Window Status (Focused,Hovered etc.)");
  2400. if (ImGui::TreeNode("Querying Window Status (Focused/Hovered etc.)"))
  2401. {
  2402. static bool embed_all_inside_a_child_window = false;
  2403. ImGui::Checkbox("Embed everything inside a child window for testing _RootWindow flag.", &embed_all_inside_a_child_window);
  2404. if (embed_all_inside_a_child_window)
  2405. ImGui::BeginChild("outer_child", ImVec2(0, ImGui::GetFontSize() * 20.0f), ImGuiChildFlags_Border);
  2406. // Testing IsWindowFocused() function with its various flags.
  2407. ImGui::BulletText(
  2408. "IsWindowFocused() = %d\n"
  2409. "IsWindowFocused(_ChildWindows) = %d\n"
  2410. "IsWindowFocused(_ChildWindows|_NoPopupHierarchy) = %d\n"
  2411. "IsWindowFocused(_ChildWindows|_RootWindow) = %d\n"
  2412. "IsWindowFocused(_ChildWindows|_RootWindow|_NoPopupHierarchy) = %d\n"
  2413. "IsWindowFocused(_RootWindow) = %d\n"
  2414. "IsWindowFocused(_RootWindow|_NoPopupHierarchy) = %d\n"
  2415. "IsWindowFocused(_AnyWindow) = %d\n",
  2416. ImGui::IsWindowFocused(),
  2417. ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows),
  2418. ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows | ImGuiFocusedFlags_NoPopupHierarchy),
  2419. ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows | ImGuiFocusedFlags_RootWindow),
  2420. ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows | ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_NoPopupHierarchy),
  2421. ImGui::IsWindowFocused(ImGuiFocusedFlags_RootWindow),
  2422. ImGui::IsWindowFocused(ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_NoPopupHierarchy),
  2423. ImGui::IsWindowFocused(ImGuiFocusedFlags_AnyWindow));
  2424. // Testing IsWindowHovered() function with its various flags.
  2425. ImGui::BulletText(
  2426. "IsWindowHovered() = %d\n"
  2427. "IsWindowHovered(_AllowWhenBlockedByPopup) = %d\n"
  2428. "IsWindowHovered(_AllowWhenBlockedByActiveItem) = %d\n"
  2429. "IsWindowHovered(_ChildWindows) = %d\n"
  2430. "IsWindowHovered(_ChildWindows|_NoPopupHierarchy) = %d\n"
  2431. "IsWindowHovered(_ChildWindows|_RootWindow) = %d\n"
  2432. "IsWindowHovered(_ChildWindows|_RootWindow|_NoPopupHierarchy) = %d\n"
  2433. "IsWindowHovered(_RootWindow) = %d\n"
  2434. "IsWindowHovered(_RootWindow|_NoPopupHierarchy) = %d\n"
  2435. "IsWindowHovered(_ChildWindows|_AllowWhenBlockedByPopup) = %d\n"
  2436. "IsWindowHovered(_AnyWindow) = %d\n"
  2437. "IsWindowHovered(_Stationary) = %d\n",
  2438. ImGui::IsWindowHovered(),
  2439. ImGui::IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup),
  2440. ImGui::IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem),
  2441. ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows),
  2442. ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_NoPopupHierarchy),
  2443. ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_RootWindow),
  2444. ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_NoPopupHierarchy),
  2445. ImGui::IsWindowHovered(ImGuiHoveredFlags_RootWindow),
  2446. ImGui::IsWindowHovered(ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_NoPopupHierarchy),
  2447. ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_AllowWhenBlockedByPopup),
  2448. ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow),
  2449. ImGui::IsWindowHovered(ImGuiHoveredFlags_Stationary));
  2450. ImGui::BeginChild("child", ImVec2(0, 50), ImGuiChildFlags_Border);
  2451. ImGui::Text("This is another child window for testing the _ChildWindows flag.");
  2452. ImGui::EndChild();
  2453. if (embed_all_inside_a_child_window)
  2454. ImGui::EndChild();
  2455. // Calling IsItemHovered() after begin returns the hovered status of the title bar.
  2456. // This is useful in particular if you want to create a context menu associated to the title bar of a window.
  2457. static bool test_window = false;
  2458. ImGui::Checkbox("Hovered/Active tests after Begin() for title bar testing", &test_window);
  2459. if (test_window)
  2460. {
  2461. ImGui::Begin("Title bar Hovered/Active tests", &test_window);
  2462. if (ImGui::BeginPopupContextItem()) // <-- This is using IsItemHovered()
  2463. {
  2464. if (ImGui::MenuItem("Close")) { test_window = false; }
  2465. ImGui::EndPopup();
  2466. }
  2467. ImGui::Text(
  2468. "IsItemHovered() after begin = %d (== is title bar hovered)\n"
  2469. "IsItemActive() after begin = %d (== is window being clicked/moved)\n",
  2470. ImGui::IsItemHovered(), ImGui::IsItemActive());
  2471. ImGui::End();
  2472. }
  2473. ImGui::TreePop();
  2474. }
  2475. // Demonstrate BeginDisabled/EndDisabled using a checkbox located at the bottom of the section (which is a bit odd:
  2476. // logically we'd have this checkbox at the top of the section, but we don't want this feature to steal that space)
  2477. if (disable_all)
  2478. ImGui::EndDisabled();
  2479. IMGUI_DEMO_MARKER("Widgets/Disable Block");
  2480. if (ImGui::TreeNode("Disable block"))
  2481. {
  2482. ImGui::Checkbox("Disable entire section above", &disable_all);
  2483. ImGui::SameLine(); HelpMarker("Demonstrate using BeginDisabled()/EndDisabled() across this section.");
  2484. ImGui::TreePop();
  2485. }
  2486. IMGUI_DEMO_MARKER("Widgets/Text Filter");
  2487. if (ImGui::TreeNode("Text Filter"))
  2488. {
  2489. // Helper class to easy setup a text filter.
  2490. // You may want to implement a more feature-full filtering scheme in your own application.
  2491. HelpMarker("Not a widget per-se, but ImGuiTextFilter is a helper to perform simple filtering on text strings.");
  2492. static ImGuiTextFilter filter;
  2493. ImGui::Text("Filter usage:\n"
  2494. " \"\" display all lines\n"
  2495. " \"xxx\" display lines containing \"xxx\"\n"
  2496. " \"xxx,yyy\" display lines containing \"xxx\" or \"yyy\"\n"
  2497. " \"-xxx\" hide lines containing \"xxx\"");
  2498. filter.Draw();
  2499. const char* lines[] = { "aaa1.c", "bbb1.c", "ccc1.c", "aaa2.cpp", "bbb2.cpp", "ccc2.cpp", "abc.h", "hello, world" };
  2500. for (int i = 0; i < IM_ARRAYSIZE(lines); i++)
  2501. if (filter.PassFilter(lines[i]))
  2502. ImGui::BulletText("%s", lines[i]);
  2503. ImGui::TreePop();
  2504. }
  2505. }
  2506. static void ShowDemoWindowLayout()
  2507. {
  2508. IMGUI_DEMO_MARKER("Layout");
  2509. if (!ImGui::CollapsingHeader("Layout & Scrolling"))
  2510. return;
  2511. IMGUI_DEMO_MARKER("Layout/Child windows");
  2512. if (ImGui::TreeNode("Child windows"))
  2513. {
  2514. ImGui::SeparatorText("Child windows");
  2515. HelpMarker("Use child windows to begin into a self-contained independent scrolling/clipping regions within a host window.");
  2516. static bool disable_mouse_wheel = false;
  2517. static bool disable_menu = false;
  2518. ImGui::Checkbox("Disable Mouse Wheel", &disable_mouse_wheel);
  2519. ImGui::Checkbox("Disable Menu", &disable_menu);
  2520. // Child 1: no border, enable horizontal scrollbar
  2521. {
  2522. ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar;
  2523. if (disable_mouse_wheel)
  2524. window_flags |= ImGuiWindowFlags_NoScrollWithMouse;
  2525. ImGui::BeginChild("ChildL", ImVec2(ImGui::GetContentRegionAvail().x * 0.5f, 260), ImGuiChildFlags_None, window_flags);
  2526. for (int i = 0; i < 100; i++)
  2527. ImGui::Text("%04d: scrollable region", i);
  2528. ImGui::EndChild();
  2529. }
  2530. ImGui::SameLine();
  2531. // Child 2: rounded border
  2532. {
  2533. ImGuiWindowFlags window_flags = ImGuiWindowFlags_None;
  2534. if (disable_mouse_wheel)
  2535. window_flags |= ImGuiWindowFlags_NoScrollWithMouse;
  2536. if (!disable_menu)
  2537. window_flags |= ImGuiWindowFlags_MenuBar;
  2538. ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 5.0f);
  2539. ImGui::BeginChild("ChildR", ImVec2(0, 260), ImGuiChildFlags_Border, window_flags);
  2540. if (!disable_menu && ImGui::BeginMenuBar())
  2541. {
  2542. if (ImGui::BeginMenu("Menu"))
  2543. {
  2544. ShowExampleMenuFile();
  2545. ImGui::EndMenu();
  2546. }
  2547. ImGui::EndMenuBar();
  2548. }
  2549. if (ImGui::BeginTable("split", 2, ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings))
  2550. {
  2551. for (int i = 0; i < 100; i++)
  2552. {
  2553. char buf[32];
  2554. sprintf(buf, "%03d", i);
  2555. ImGui::TableNextColumn();
  2556. ImGui::Button(buf, ImVec2(-FLT_MIN, 0.0f));
  2557. }
  2558. ImGui::EndTable();
  2559. }
  2560. ImGui::EndChild();
  2561. ImGui::PopStyleVar();
  2562. }
  2563. // Child 3: manual-resize
  2564. ImGui::SeparatorText("Manual-resize");
  2565. {
  2566. HelpMarker("Drag bottom border to resize. Double-click bottom border to auto-fit to vertical contents.");
  2567. ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::GetStyleColorVec4(ImGuiCol_FrameBg));
  2568. if (ImGui::BeginChild("ResizableChild", ImVec2(-FLT_MIN, ImGui::GetTextLineHeightWithSpacing() * 8), ImGuiChildFlags_Border | ImGuiChildFlags_ResizeY))
  2569. for (int n = 0; n < 10; n++)
  2570. ImGui::Text("Line %04d", n);
  2571. ImGui::PopStyleColor();
  2572. ImGui::EndChild();
  2573. }
  2574. // Child 4: auto-resizing height with a limit
  2575. ImGui::SeparatorText("Auto-resize with constraints");
  2576. {
  2577. static int draw_lines = 3;
  2578. static int max_height_in_lines = 10;
  2579. ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
  2580. ImGui::DragInt("Lines Count", &draw_lines, 0.2f);
  2581. ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
  2582. ImGui::DragInt("Max Height (in Lines)", &max_height_in_lines, 0.2f);
  2583. ImGui::SetNextWindowSizeConstraints(ImVec2(0.0f, ImGui::GetTextLineHeightWithSpacing() * 1), ImVec2(FLT_MAX, ImGui::GetTextLineHeightWithSpacing() * max_height_in_lines));
  2584. if (ImGui::BeginChild("ConstrainedChild", ImVec2(-FLT_MIN, 0.0f), ImGuiChildFlags_Border | ImGuiChildFlags_AutoResizeY))
  2585. for (int n = 0; n < draw_lines; n++)
  2586. ImGui::Text("Line %04d", n);
  2587. ImGui::EndChild();
  2588. }
  2589. ImGui::SeparatorText("Misc/Advanced");
  2590. // Demonstrate a few extra things
  2591. // - Changing ImGuiCol_ChildBg (which is transparent black in default styles)
  2592. // - Using SetCursorPos() to position child window (the child window is an item from the POV of parent window)
  2593. // You can also call SetNextWindowPos() to position the child window. The parent window will effectively
  2594. // layout from this position.
  2595. // - Using ImGui::GetItemRectMin/Max() to query the "item" state (because the child window is an item from
  2596. // the POV of the parent window). See 'Demo->Querying Status (Edited/Active/Hovered etc.)' for details.
  2597. {
  2598. static int offset_x = 0;
  2599. static bool override_bg_color = true;
  2600. static ImGuiChildFlags child_flags = ImGuiChildFlags_Border | ImGuiChildFlags_ResizeX | ImGuiChildFlags_ResizeY;
  2601. ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
  2602. ImGui::DragInt("Offset X", &offset_x, 1.0f, -1000, 1000);
  2603. ImGui::Checkbox("Override ChildBg color", &override_bg_color);
  2604. ImGui::CheckboxFlags("ImGuiChildFlags_Border", &child_flags, ImGuiChildFlags_Border);
  2605. ImGui::CheckboxFlags("ImGuiChildFlags_AlwaysUseWindowPadding", &child_flags, ImGuiChildFlags_AlwaysUseWindowPadding);
  2606. ImGui::CheckboxFlags("ImGuiChildFlags_ResizeX", &child_flags, ImGuiChildFlags_ResizeX);
  2607. ImGui::CheckboxFlags("ImGuiChildFlags_ResizeY", &child_flags, ImGuiChildFlags_ResizeY);
  2608. ImGui::CheckboxFlags("ImGuiChildFlags_FrameStyle", &child_flags, ImGuiChildFlags_FrameStyle);
  2609. ImGui::SameLine(); HelpMarker("Style the child window like a framed item: use FrameBg, FrameRounding, FrameBorderSize, FramePadding instead of ChildBg, ChildRounding, ChildBorderSize, WindowPadding.");
  2610. if (child_flags & ImGuiChildFlags_FrameStyle)
  2611. override_bg_color = false;
  2612. ImGui::SetCursorPosX(ImGui::GetCursorPosX() + (float)offset_x);
  2613. if (override_bg_color)
  2614. ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(255, 0, 0, 100));
  2615. ImGui::BeginChild("Red", ImVec2(200, 100), child_flags, ImGuiWindowFlags_None);
  2616. if (override_bg_color)
  2617. ImGui::PopStyleColor();
  2618. for (int n = 0; n < 50; n++)
  2619. ImGui::Text("Some test %d", n);
  2620. ImGui::EndChild();
  2621. bool child_is_hovered = ImGui::IsItemHovered();
  2622. ImVec2 child_rect_min = ImGui::GetItemRectMin();
  2623. ImVec2 child_rect_max = ImGui::GetItemRectMax();
  2624. ImGui::Text("Hovered: %d", child_is_hovered);
  2625. ImGui::Text("Rect of child window is: (%.0f,%.0f) (%.0f,%.0f)", child_rect_min.x, child_rect_min.y, child_rect_max.x, child_rect_max.y);
  2626. }
  2627. ImGui::TreePop();
  2628. }
  2629. IMGUI_DEMO_MARKER("Layout/Widgets Width");
  2630. if (ImGui::TreeNode("Widgets Width"))
  2631. {
  2632. static float f = 0.0f;
  2633. static bool show_indented_items = true;
  2634. ImGui::Checkbox("Show indented items", &show_indented_items);
  2635. // Use SetNextItemWidth() to set the width of a single upcoming item.
  2636. // Use PushItemWidth()/PopItemWidth() to set the width of a group of items.
  2637. // In real code use you'll probably want to choose width values that are proportional to your font size
  2638. // e.g. Using '20.0f * GetFontSize()' as width instead of '200.0f', etc.
  2639. ImGui::Text("SetNextItemWidth/PushItemWidth(100)");
  2640. ImGui::SameLine(); HelpMarker("Fixed width.");
  2641. ImGui::PushItemWidth(100);
  2642. ImGui::DragFloat("float##1b", &f);
  2643. if (show_indented_items)
  2644. {
  2645. ImGui::Indent();
  2646. ImGui::DragFloat("float (indented)##1b", &f);
  2647. ImGui::Unindent();
  2648. }
  2649. ImGui::PopItemWidth();
  2650. ImGui::Text("SetNextItemWidth/PushItemWidth(-100)");
  2651. ImGui::SameLine(); HelpMarker("Align to right edge minus 100");
  2652. ImGui::PushItemWidth(-100);
  2653. ImGui::DragFloat("float##2a", &f);
  2654. if (show_indented_items)
  2655. {
  2656. ImGui::Indent();
  2657. ImGui::DragFloat("float (indented)##2b", &f);
  2658. ImGui::Unindent();
  2659. }
  2660. ImGui::PopItemWidth();
  2661. ImGui::Text("SetNextItemWidth/PushItemWidth(GetContentRegionAvail().x * 0.5f)");
  2662. ImGui::SameLine(); HelpMarker("Half of available width.\n(~ right-cursor_pos)\n(works within a column set)");
  2663. ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x * 0.5f);
  2664. ImGui::DragFloat("float##3a", &f);
  2665. if (show_indented_items)
  2666. {
  2667. ImGui::Indent();
  2668. ImGui::DragFloat("float (indented)##3b", &f);
  2669. ImGui::Unindent();
  2670. }
  2671. ImGui::PopItemWidth();
  2672. ImGui::Text("SetNextItemWidth/PushItemWidth(-GetContentRegionAvail().x * 0.5f)");
  2673. ImGui::SameLine(); HelpMarker("Align to right edge minus half");
  2674. ImGui::PushItemWidth(-ImGui::GetContentRegionAvail().x * 0.5f);
  2675. ImGui::DragFloat("float##4a", &f);
  2676. if (show_indented_items)
  2677. {
  2678. ImGui::Indent();
  2679. ImGui::DragFloat("float (indented)##4b", &f);
  2680. ImGui::Unindent();
  2681. }
  2682. ImGui::PopItemWidth();
  2683. // Demonstrate using PushItemWidth to surround three items.
  2684. // Calling SetNextItemWidth() before each of them would have the same effect.
  2685. ImGui::Text("SetNextItemWidth/PushItemWidth(-FLT_MIN)");
  2686. ImGui::SameLine(); HelpMarker("Align to right edge");
  2687. ImGui::PushItemWidth(-FLT_MIN);
  2688. ImGui::DragFloat("##float5a", &f);
  2689. if (show_indented_items)
  2690. {
  2691. ImGui::Indent();
  2692. ImGui::DragFloat("float (indented)##5b", &f);
  2693. ImGui::Unindent();
  2694. }
  2695. ImGui::PopItemWidth();
  2696. ImGui::TreePop();
  2697. }
  2698. IMGUI_DEMO_MARKER("Layout/Basic Horizontal Layout");
  2699. if (ImGui::TreeNode("Basic Horizontal Layout"))
  2700. {
  2701. ImGui::TextWrapped("(Use ImGui::SameLine() to keep adding items to the right of the preceding item)");
  2702. // Text
  2703. IMGUI_DEMO_MARKER("Layout/Basic Horizontal Layout/SameLine");
  2704. ImGui::Text("Two items: Hello"); ImGui::SameLine();
  2705. ImGui::TextColored(ImVec4(1, 1, 0, 1), "Sailor");
  2706. // Adjust spacing
  2707. ImGui::Text("More spacing: Hello"); ImGui::SameLine(0, 20);
  2708. ImGui::TextColored(ImVec4(1, 1, 0, 1), "Sailor");
  2709. // Button
  2710. ImGui::AlignTextToFramePadding();
  2711. ImGui::Text("Normal buttons"); ImGui::SameLine();
  2712. ImGui::Button("Banana"); ImGui::SameLine();
  2713. ImGui::Button("Apple"); ImGui::SameLine();
  2714. ImGui::Button("Corniflower");
  2715. // Button
  2716. ImGui::Text("Small buttons"); ImGui::SameLine();
  2717. ImGui::SmallButton("Like this one"); ImGui::SameLine();
  2718. ImGui::Text("can fit within a text block.");
  2719. // Aligned to arbitrary position. Easy/cheap column.
  2720. IMGUI_DEMO_MARKER("Layout/Basic Horizontal Layout/SameLine (with offset)");
  2721. ImGui::Text("Aligned");
  2722. ImGui::SameLine(150); ImGui::Text("x=150");
  2723. ImGui::SameLine(300); ImGui::Text("x=300");
  2724. ImGui::Text("Aligned");
  2725. ImGui::SameLine(150); ImGui::SmallButton("x=150");
  2726. ImGui::SameLine(300); ImGui::SmallButton("x=300");
  2727. // Checkbox
  2728. IMGUI_DEMO_MARKER("Layout/Basic Horizontal Layout/SameLine (more)");
  2729. static bool c1 = false, c2 = false, c3 = false, c4 = false;
  2730. ImGui::Checkbox("My", &c1); ImGui::SameLine();
  2731. ImGui::Checkbox("Tailor", &c2); ImGui::SameLine();
  2732. ImGui::Checkbox("Is", &c3); ImGui::SameLine();
  2733. ImGui::Checkbox("Rich", &c4);
  2734. // Various
  2735. static float f0 = 1.0f, f1 = 2.0f, f2 = 3.0f;
  2736. ImGui::PushItemWidth(80);
  2737. const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD" };
  2738. static int item = -1;
  2739. ImGui::Combo("Combo", &item, items, IM_ARRAYSIZE(items)); ImGui::SameLine();
  2740. ImGui::SliderFloat("X", &f0, 0.0f, 5.0f); ImGui::SameLine();
  2741. ImGui::SliderFloat("Y", &f1, 0.0f, 5.0f); ImGui::SameLine();
  2742. ImGui::SliderFloat("Z", &f2, 0.0f, 5.0f);
  2743. ImGui::PopItemWidth();
  2744. ImGui::PushItemWidth(80);
  2745. ImGui::Text("Lists:");
  2746. static int selection[4] = { 0, 1, 2, 3 };
  2747. for (int i = 0; i < 4; i++)
  2748. {
  2749. if (i > 0) ImGui::SameLine();
  2750. ImGui::PushID(i);
  2751. ImGui::ListBox("", &selection[i], items, IM_ARRAYSIZE(items));
  2752. ImGui::PopID();
  2753. //ImGui::SetItemTooltip("ListBox %d hovered", i);
  2754. }
  2755. ImGui::PopItemWidth();
  2756. // Dummy
  2757. IMGUI_DEMO_MARKER("Layout/Basic Horizontal Layout/Dummy");
  2758. ImVec2 button_sz(40, 40);
  2759. ImGui::Button("A", button_sz); ImGui::SameLine();
  2760. ImGui::Dummy(button_sz); ImGui::SameLine();
  2761. ImGui::Button("B", button_sz);
  2762. // Manually wrapping
  2763. // (we should eventually provide this as an automatic layout feature, but for now you can do it manually)
  2764. IMGUI_DEMO_MARKER("Layout/Basic Horizontal Layout/Manual wrapping");
  2765. ImGui::Text("Manual wrapping:");
  2766. ImGuiStyle& style = ImGui::GetStyle();
  2767. int buttons_count = 20;
  2768. float window_visible_x2 = ImGui::GetWindowPos().x + ImGui::GetWindowContentRegionMax().x;
  2769. for (int n = 0; n < buttons_count; n++)
  2770. {
  2771. ImGui::PushID(n);
  2772. ImGui::Button("Box", button_sz);
  2773. float last_button_x2 = ImGui::GetItemRectMax().x;
  2774. float next_button_x2 = last_button_x2 + style.ItemSpacing.x + button_sz.x; // Expected position if next button was on same line
  2775. if (n + 1 < buttons_count && next_button_x2 < window_visible_x2)
  2776. ImGui::SameLine();
  2777. ImGui::PopID();
  2778. }
  2779. ImGui::TreePop();
  2780. }
  2781. IMGUI_DEMO_MARKER("Layout/Groups");
  2782. if (ImGui::TreeNode("Groups"))
  2783. {
  2784. HelpMarker(
  2785. "BeginGroup() basically locks the horizontal position for new line. "
  2786. "EndGroup() bundles the whole group so that you can use \"item\" functions such as "
  2787. "IsItemHovered()/IsItemActive() or SameLine() etc. on the whole group.");
  2788. ImGui::BeginGroup();
  2789. {
  2790. ImGui::BeginGroup();
  2791. ImGui::Button("AAA");
  2792. ImGui::SameLine();
  2793. ImGui::Button("BBB");
  2794. ImGui::SameLine();
  2795. ImGui::BeginGroup();
  2796. ImGui::Button("CCC");
  2797. ImGui::Button("DDD");
  2798. ImGui::EndGroup();
  2799. ImGui::SameLine();
  2800. ImGui::Button("EEE");
  2801. ImGui::EndGroup();
  2802. ImGui::SetItemTooltip("First group hovered");
  2803. }
  2804. // Capture the group size and create widgets using the same size
  2805. ImVec2 size = ImGui::GetItemRectSize();
  2806. const float values[5] = { 0.5f, 0.20f, 0.80f, 0.60f, 0.25f };
  2807. ImGui::PlotHistogram("##values", values, IM_ARRAYSIZE(values), 0, NULL, 0.0f, 1.0f, size);
  2808. ImGui::Button("ACTION", ImVec2((size.x - ImGui::GetStyle().ItemSpacing.x) * 0.5f, size.y));
  2809. ImGui::SameLine();
  2810. ImGui::Button("REACTION", ImVec2((size.x - ImGui::GetStyle().ItemSpacing.x) * 0.5f, size.y));
  2811. ImGui::EndGroup();
  2812. ImGui::SameLine();
  2813. ImGui::Button("LEVERAGE\nBUZZWORD", size);
  2814. ImGui::SameLine();
  2815. if (ImGui::BeginListBox("List", size))
  2816. {
  2817. ImGui::Selectable("Selected", true);
  2818. ImGui::Selectable("Not Selected", false);
  2819. ImGui::EndListBox();
  2820. }
  2821. ImGui::TreePop();
  2822. }
  2823. IMGUI_DEMO_MARKER("Layout/Text Baseline Alignment");
  2824. if (ImGui::TreeNode("Text Baseline Alignment"))
  2825. {
  2826. {
  2827. ImGui::BulletText("Text baseline:");
  2828. ImGui::SameLine(); HelpMarker(
  2829. "This is testing the vertical alignment that gets applied on text to keep it aligned with widgets. "
  2830. "Lines only composed of text or \"small\" widgets use less vertical space than lines with framed widgets.");
  2831. ImGui::Indent();
  2832. ImGui::Text("KO Blahblah"); ImGui::SameLine();
  2833. ImGui::Button("Some framed item"); ImGui::SameLine();
  2834. HelpMarker("Baseline of button will look misaligned with text..");
  2835. // If your line starts with text, call AlignTextToFramePadding() to align text to upcoming widgets.
  2836. // (because we don't know what's coming after the Text() statement, we need to move the text baseline
  2837. // down by FramePadding.y ahead of time)
  2838. ImGui::AlignTextToFramePadding();
  2839. ImGui::Text("OK Blahblah"); ImGui::SameLine();
  2840. ImGui::Button("Some framed item"); ImGui::SameLine();
  2841. HelpMarker("We call AlignTextToFramePadding() to vertically align the text baseline by +FramePadding.y");
  2842. // SmallButton() uses the same vertical padding as Text
  2843. ImGui::Button("TEST##1"); ImGui::SameLine();
  2844. ImGui::Text("TEST"); ImGui::SameLine();
  2845. ImGui::SmallButton("TEST##2");
  2846. // If your line starts with text, call AlignTextToFramePadding() to align text to upcoming widgets.
  2847. ImGui::AlignTextToFramePadding();
  2848. ImGui::Text("Text aligned to framed item"); ImGui::SameLine();
  2849. ImGui::Button("Item##1"); ImGui::SameLine();
  2850. ImGui::Text("Item"); ImGui::SameLine();
  2851. ImGui::SmallButton("Item##2"); ImGui::SameLine();
  2852. ImGui::Button("Item##3");
  2853. ImGui::Unindent();
  2854. }
  2855. ImGui::Spacing();
  2856. {
  2857. ImGui::BulletText("Multi-line text:");
  2858. ImGui::Indent();
  2859. ImGui::Text("One\nTwo\nThree"); ImGui::SameLine();
  2860. ImGui::Text("Hello\nWorld"); ImGui::SameLine();
  2861. ImGui::Text("Banana");
  2862. ImGui::Text("Banana"); ImGui::SameLine();
  2863. ImGui::Text("Hello\nWorld"); ImGui::SameLine();
  2864. ImGui::Text("One\nTwo\nThree");
  2865. ImGui::Button("HOP##1"); ImGui::SameLine();
  2866. ImGui::Text("Banana"); ImGui::SameLine();
  2867. ImGui::Text("Hello\nWorld"); ImGui::SameLine();
  2868. ImGui::Text("Banana");
  2869. ImGui::Button("HOP##2"); ImGui::SameLine();
  2870. ImGui::Text("Hello\nWorld"); ImGui::SameLine();
  2871. ImGui::Text("Banana");
  2872. ImGui::Unindent();
  2873. }
  2874. ImGui::Spacing();
  2875. {
  2876. ImGui::BulletText("Misc items:");
  2877. ImGui::Indent();
  2878. // SmallButton() sets FramePadding to zero. Text baseline is aligned to match baseline of previous Button.
  2879. ImGui::Button("80x80", ImVec2(80, 80));
  2880. ImGui::SameLine();
  2881. ImGui::Button("50x50", ImVec2(50, 50));
  2882. ImGui::SameLine();
  2883. ImGui::Button("Button()");
  2884. ImGui::SameLine();
  2885. ImGui::SmallButton("SmallButton()");
  2886. // Tree
  2887. const float spacing = ImGui::GetStyle().ItemInnerSpacing.x;
  2888. ImGui::Button("Button##1");
  2889. ImGui::SameLine(0.0f, spacing);
  2890. if (ImGui::TreeNode("Node##1"))
  2891. {
  2892. // Placeholder tree data
  2893. for (int i = 0; i < 6; i++)
  2894. ImGui::BulletText("Item %d..", i);
  2895. ImGui::TreePop();
  2896. }
  2897. // Vertically align text node a bit lower so it'll be vertically centered with upcoming widget.
  2898. // Otherwise you can use SmallButton() (smaller fit).
  2899. ImGui::AlignTextToFramePadding();
  2900. // Common mistake to avoid: if we want to SameLine after TreeNode we need to do it before we add
  2901. // other contents below the node.
  2902. bool node_open = ImGui::TreeNode("Node##2");
  2903. ImGui::SameLine(0.0f, spacing); ImGui::Button("Button##2");
  2904. if (node_open)
  2905. {
  2906. // Placeholder tree data
  2907. for (int i = 0; i < 6; i++)
  2908. ImGui::BulletText("Item %d..", i);
  2909. ImGui::TreePop();
  2910. }
  2911. // Bullet
  2912. ImGui::Button("Button##3");
  2913. ImGui::SameLine(0.0f, spacing);
  2914. ImGui::BulletText("Bullet text");
  2915. ImGui::AlignTextToFramePadding();
  2916. ImGui::BulletText("Node");
  2917. ImGui::SameLine(0.0f, spacing); ImGui::Button("Button##4");
  2918. ImGui::Unindent();
  2919. }
  2920. ImGui::TreePop();
  2921. }
  2922. IMGUI_DEMO_MARKER("Layout/Scrolling");
  2923. if (ImGui::TreeNode("Scrolling"))
  2924. {
  2925. // Vertical scroll functions
  2926. IMGUI_DEMO_MARKER("Layout/Scrolling/Vertical");
  2927. HelpMarker("Use SetScrollHereY() or SetScrollFromPosY() to scroll to a given vertical position.");
  2928. static int track_item = 50;
  2929. static bool enable_track = true;
  2930. static bool enable_extra_decorations = false;
  2931. static float scroll_to_off_px = 0.0f;
  2932. static float scroll_to_pos_px = 200.0f;
  2933. ImGui::Checkbox("Decoration", &enable_extra_decorations);
  2934. ImGui::Checkbox("Track", &enable_track);
  2935. ImGui::PushItemWidth(100);
  2936. ImGui::SameLine(140); enable_track |= ImGui::DragInt("##item", &track_item, 0.25f, 0, 99, "Item = %d");
  2937. bool scroll_to_off = ImGui::Button("Scroll Offset");
  2938. ImGui::SameLine(140); scroll_to_off |= ImGui::DragFloat("##off", &scroll_to_off_px, 1.00f, 0, FLT_MAX, "+%.0f px");
  2939. bool scroll_to_pos = ImGui::Button("Scroll To Pos");
  2940. ImGui::SameLine(140); scroll_to_pos |= ImGui::DragFloat("##pos", &scroll_to_pos_px, 1.00f, -10, FLT_MAX, "X/Y = %.0f px");
  2941. ImGui::PopItemWidth();
  2942. if (scroll_to_off || scroll_to_pos)
  2943. enable_track = false;
  2944. ImGuiStyle& style = ImGui::GetStyle();
  2945. float child_w = (ImGui::GetContentRegionAvail().x - 4 * style.ItemSpacing.x) / 5;
  2946. if (child_w < 1.0f)
  2947. child_w = 1.0f;
  2948. ImGui::PushID("##VerticalScrolling");
  2949. for (int i = 0; i < 5; i++)
  2950. {
  2951. if (i > 0) ImGui::SameLine();
  2952. ImGui::BeginGroup();
  2953. const char* names[] = { "Top", "25%", "Center", "75%", "Bottom" };
  2954. ImGui::TextUnformatted(names[i]);
  2955. const ImGuiWindowFlags child_flags = enable_extra_decorations ? ImGuiWindowFlags_MenuBar : 0;
  2956. const ImGuiID child_id = ImGui::GetID((void*)(intptr_t)i);
  2957. const bool child_is_visible = ImGui::BeginChild(child_id, ImVec2(child_w, 200.0f), ImGuiChildFlags_Border, child_flags);
  2958. if (ImGui::BeginMenuBar())
  2959. {
  2960. ImGui::TextUnformatted("abc");
  2961. ImGui::EndMenuBar();
  2962. }
  2963. if (scroll_to_off)
  2964. ImGui::SetScrollY(scroll_to_off_px);
  2965. if (scroll_to_pos)
  2966. ImGui::SetScrollFromPosY(ImGui::GetCursorStartPos().y + scroll_to_pos_px, i * 0.25f);
  2967. if (child_is_visible) // Avoid calling SetScrollHereY when running with culled items
  2968. {
  2969. for (int item = 0; item < 100; item++)
  2970. {
  2971. if (enable_track && item == track_item)
  2972. {
  2973. ImGui::TextColored(ImVec4(1, 1, 0, 1), "Item %d", item);
  2974. ImGui::SetScrollHereY(i * 0.25f); // 0.0f:top, 0.5f:center, 1.0f:bottom
  2975. }
  2976. else
  2977. {
  2978. ImGui::Text("Item %d", item);
  2979. }
  2980. }
  2981. }
  2982. float scroll_y = ImGui::GetScrollY();
  2983. float scroll_max_y = ImGui::GetScrollMaxY();
  2984. ImGui::EndChild();
  2985. ImGui::Text("%.0f/%.0f", scroll_y, scroll_max_y);
  2986. ImGui::EndGroup();
  2987. }
  2988. ImGui::PopID();
  2989. // Horizontal scroll functions
  2990. IMGUI_DEMO_MARKER("Layout/Scrolling/Horizontal");
  2991. ImGui::Spacing();
  2992. HelpMarker(
  2993. "Use SetScrollHereX() or SetScrollFromPosX() to scroll to a given horizontal position.\n\n"
  2994. "Because the clipping rectangle of most window hides half worth of WindowPadding on the "
  2995. "left/right, using SetScrollFromPosX(+1) will usually result in clipped text whereas the "
  2996. "equivalent SetScrollFromPosY(+1) wouldn't.");
  2997. ImGui::PushID("##HorizontalScrolling");
  2998. for (int i = 0; i < 5; i++)
  2999. {
  3000. float child_height = ImGui::GetTextLineHeight() + style.ScrollbarSize + style.WindowPadding.y * 2.0f;
  3001. ImGuiWindowFlags child_flags = ImGuiWindowFlags_HorizontalScrollbar | (enable_extra_decorations ? ImGuiWindowFlags_AlwaysVerticalScrollbar : 0);
  3002. ImGuiID child_id = ImGui::GetID((void*)(intptr_t)i);
  3003. bool child_is_visible = ImGui::BeginChild(child_id, ImVec2(-100, child_height), ImGuiChildFlags_Border, child_flags);
  3004. if (scroll_to_off)
  3005. ImGui::SetScrollX(scroll_to_off_px);
  3006. if (scroll_to_pos)
  3007. ImGui::SetScrollFromPosX(ImGui::GetCursorStartPos().x + scroll_to_pos_px, i * 0.25f);
  3008. if (child_is_visible) // Avoid calling SetScrollHereY when running with culled items
  3009. {
  3010. for (int item = 0; item < 100; item++)
  3011. {
  3012. if (item > 0)
  3013. ImGui::SameLine();
  3014. if (enable_track && item == track_item)
  3015. {
  3016. ImGui::TextColored(ImVec4(1, 1, 0, 1), "Item %d", item);
  3017. ImGui::SetScrollHereX(i * 0.25f); // 0.0f:left, 0.5f:center, 1.0f:right
  3018. }
  3019. else
  3020. {
  3021. ImGui::Text("Item %d", item);
  3022. }
  3023. }
  3024. }
  3025. float scroll_x = ImGui::GetScrollX();
  3026. float scroll_max_x = ImGui::GetScrollMaxX();
  3027. ImGui::EndChild();
  3028. ImGui::SameLine();
  3029. const char* names[] = { "Left", "25%", "Center", "75%", "Right" };
  3030. ImGui::Text("%s\n%.0f/%.0f", names[i], scroll_x, scroll_max_x);
  3031. ImGui::Spacing();
  3032. }
  3033. ImGui::PopID();
  3034. // Miscellaneous Horizontal Scrolling Demo
  3035. IMGUI_DEMO_MARKER("Layout/Scrolling/Horizontal (more)");
  3036. HelpMarker(
  3037. "Horizontal scrolling for a window is enabled via the ImGuiWindowFlags_HorizontalScrollbar flag.\n\n"
  3038. "You may want to also explicitly specify content width by using SetNextWindowContentWidth() before Begin().");
  3039. static int lines = 7;
  3040. ImGui::SliderInt("Lines", &lines, 1, 15);
  3041. ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
  3042. ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2.0f, 1.0f));
  3043. ImVec2 scrolling_child_size = ImVec2(0, ImGui::GetFrameHeightWithSpacing() * 7 + 30);
  3044. ImGui::BeginChild("scrolling", scrolling_child_size, ImGuiChildFlags_Border, ImGuiWindowFlags_HorizontalScrollbar);
  3045. for (int line = 0; line < lines; line++)
  3046. {
  3047. // Display random stuff. For the sake of this trivial demo we are using basic Button() + SameLine()
  3048. // If you want to create your own time line for a real application you may be better off manipulating
  3049. // the cursor position yourself, aka using SetCursorPos/SetCursorScreenPos to position the widgets
  3050. // yourself. You may also want to use the lower-level ImDrawList API.
  3051. int num_buttons = 10 + ((line & 1) ? line * 9 : line * 3);
  3052. for (int n = 0; n < num_buttons; n++)
  3053. {
  3054. if (n > 0) ImGui::SameLine();
  3055. ImGui::PushID(n + line * 1000);
  3056. char num_buf[16];
  3057. sprintf(num_buf, "%d", n);
  3058. const char* label = (!(n % 15)) ? "FizzBuzz" : (!(n % 3)) ? "Fizz" : (!(n % 5)) ? "Buzz" : num_buf;
  3059. float hue = n * 0.05f;
  3060. ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(hue, 0.6f, 0.6f));
  3061. ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV(hue, 0.7f, 0.7f));
  3062. ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(hue, 0.8f, 0.8f));
  3063. ImGui::Button(label, ImVec2(40.0f + sinf((float)(line + n)) * 20.0f, 0.0f));
  3064. ImGui::PopStyleColor(3);
  3065. ImGui::PopID();
  3066. }
  3067. }
  3068. float scroll_x = ImGui::GetScrollX();
  3069. float scroll_max_x = ImGui::GetScrollMaxX();
  3070. ImGui::EndChild();
  3071. ImGui::PopStyleVar(2);
  3072. float scroll_x_delta = 0.0f;
  3073. ImGui::SmallButton("<<");
  3074. if (ImGui::IsItemActive())
  3075. scroll_x_delta = -ImGui::GetIO().DeltaTime * 1000.0f;
  3076. ImGui::SameLine();
  3077. ImGui::Text("Scroll from code"); ImGui::SameLine();
  3078. ImGui::SmallButton(">>");
  3079. if (ImGui::IsItemActive())
  3080. scroll_x_delta = +ImGui::GetIO().DeltaTime * 1000.0f;
  3081. ImGui::SameLine();
  3082. ImGui::Text("%.0f/%.0f", scroll_x, scroll_max_x);
  3083. if (scroll_x_delta != 0.0f)
  3084. {
  3085. // Demonstrate a trick: you can use Begin to set yourself in the context of another window
  3086. // (here we are already out of your child window)
  3087. ImGui::BeginChild("scrolling");
  3088. ImGui::SetScrollX(ImGui::GetScrollX() + scroll_x_delta);
  3089. ImGui::EndChild();
  3090. }
  3091. ImGui::Spacing();
  3092. static bool show_horizontal_contents_size_demo_window = false;
  3093. ImGui::Checkbox("Show Horizontal contents size demo window", &show_horizontal_contents_size_demo_window);
  3094. if (show_horizontal_contents_size_demo_window)
  3095. {
  3096. static bool show_h_scrollbar = true;
  3097. static bool show_button = true;
  3098. static bool show_tree_nodes = true;
  3099. static bool show_text_wrapped = false;
  3100. static bool show_columns = true;
  3101. static bool show_tab_bar = true;
  3102. static bool show_child = false;
  3103. static bool explicit_content_size = false;
  3104. static float contents_size_x = 300.0f;
  3105. if (explicit_content_size)
  3106. ImGui::SetNextWindowContentSize(ImVec2(contents_size_x, 0.0f));
  3107. ImGui::Begin("Horizontal contents size demo window", &show_horizontal_contents_size_demo_window, show_h_scrollbar ? ImGuiWindowFlags_HorizontalScrollbar : 0);
  3108. IMGUI_DEMO_MARKER("Layout/Scrolling/Horizontal contents size demo window");
  3109. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(2, 0));
  3110. ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 0));
  3111. HelpMarker(
  3112. "Test how different widgets react and impact the work rectangle growing when horizontal scrolling is enabled.\n\n"
  3113. "Use 'Metrics->Tools->Show windows rectangles' to visualize rectangles.");
  3114. ImGui::Checkbox("H-scrollbar", &show_h_scrollbar);
  3115. ImGui::Checkbox("Button", &show_button); // Will grow contents size (unless explicitly overwritten)
  3116. ImGui::Checkbox("Tree nodes", &show_tree_nodes); // Will grow contents size and display highlight over full width
  3117. ImGui::Checkbox("Text wrapped", &show_text_wrapped);// Will grow and use contents size
  3118. ImGui::Checkbox("Columns", &show_columns); // Will use contents size
  3119. ImGui::Checkbox("Tab bar", &show_tab_bar); // Will use contents size
  3120. ImGui::Checkbox("Child", &show_child); // Will grow and use contents size
  3121. ImGui::Checkbox("Explicit content size", &explicit_content_size);
  3122. ImGui::Text("Scroll %.1f/%.1f %.1f/%.1f", ImGui::GetScrollX(), ImGui::GetScrollMaxX(), ImGui::GetScrollY(), ImGui::GetScrollMaxY());
  3123. if (explicit_content_size)
  3124. {
  3125. ImGui::SameLine();
  3126. ImGui::SetNextItemWidth(100);
  3127. ImGui::DragFloat("##csx", &contents_size_x);
  3128. ImVec2 p = ImGui::GetCursorScreenPos();
  3129. ImGui::GetWindowDrawList()->AddRectFilled(p, ImVec2(p.x + 10, p.y + 10), IM_COL32_WHITE);
  3130. ImGui::GetWindowDrawList()->AddRectFilled(ImVec2(p.x + contents_size_x - 10, p.y), ImVec2(p.x + contents_size_x, p.y + 10), IM_COL32_WHITE);
  3131. ImGui::Dummy(ImVec2(0, 10));
  3132. }
  3133. ImGui::PopStyleVar(2);
  3134. ImGui::Separator();
  3135. if (show_button)
  3136. {
  3137. ImGui::Button("this is a 300-wide button", ImVec2(300, 0));
  3138. }
  3139. if (show_tree_nodes)
  3140. {
  3141. bool open = true;
  3142. if (ImGui::TreeNode("this is a tree node"))
  3143. {
  3144. if (ImGui::TreeNode("another one of those tree node..."))
  3145. {
  3146. ImGui::Text("Some tree contents");
  3147. ImGui::TreePop();
  3148. }
  3149. ImGui::TreePop();
  3150. }
  3151. ImGui::CollapsingHeader("CollapsingHeader", &open);
  3152. }
  3153. if (show_text_wrapped)
  3154. {
  3155. ImGui::TextWrapped("This text should automatically wrap on the edge of the work rectangle.");
  3156. }
  3157. if (show_columns)
  3158. {
  3159. ImGui::Text("Tables:");
  3160. if (ImGui::BeginTable("table", 4, ImGuiTableFlags_Borders))
  3161. {
  3162. for (int n = 0; n < 4; n++)
  3163. {
  3164. ImGui::TableNextColumn();
  3165. ImGui::Text("Width %.2f", ImGui::GetContentRegionAvail().x);
  3166. }
  3167. ImGui::EndTable();
  3168. }
  3169. ImGui::Text("Columns:");
  3170. ImGui::Columns(4);
  3171. for (int n = 0; n < 4; n++)
  3172. {
  3173. ImGui::Text("Width %.2f", ImGui::GetColumnWidth());
  3174. ImGui::NextColumn();
  3175. }
  3176. ImGui::Columns(1);
  3177. }
  3178. if (show_tab_bar && ImGui::BeginTabBar("Hello"))
  3179. {
  3180. if (ImGui::BeginTabItem("OneOneOne")) { ImGui::EndTabItem(); }
  3181. if (ImGui::BeginTabItem("TwoTwoTwo")) { ImGui::EndTabItem(); }
  3182. if (ImGui::BeginTabItem("ThreeThreeThree")) { ImGui::EndTabItem(); }
  3183. if (ImGui::BeginTabItem("FourFourFour")) { ImGui::EndTabItem(); }
  3184. ImGui::EndTabBar();
  3185. }
  3186. if (show_child)
  3187. {
  3188. ImGui::BeginChild("child", ImVec2(0, 0), ImGuiChildFlags_Border);
  3189. ImGui::EndChild();
  3190. }
  3191. ImGui::End();
  3192. }
  3193. ImGui::TreePop();
  3194. }
  3195. IMGUI_DEMO_MARKER("Layout/Clipping");
  3196. if (ImGui::TreeNode("Clipping"))
  3197. {
  3198. static ImVec2 size(100.0f, 100.0f);
  3199. static ImVec2 offset(30.0f, 30.0f);
  3200. ImGui::DragFloat2("size", (float*)&size, 0.5f, 1.0f, 200.0f, "%.0f");
  3201. ImGui::TextWrapped("(Click and drag to scroll)");
  3202. HelpMarker(
  3203. "(Left) Using ImGui::PushClipRect():\n"
  3204. "Will alter ImGui hit-testing logic + ImDrawList rendering.\n"
  3205. "(use this if you want your clipping rectangle to affect interactions)\n\n"
  3206. "(Center) Using ImDrawList::PushClipRect():\n"
  3207. "Will alter ImDrawList rendering only.\n"
  3208. "(use this as a shortcut if you are only using ImDrawList calls)\n\n"
  3209. "(Right) Using ImDrawList::AddText() with a fine ClipRect:\n"
  3210. "Will alter only this specific ImDrawList::AddText() rendering.\n"
  3211. "This is often used internally to avoid altering the clipping rectangle and minimize draw calls.");
  3212. for (int n = 0; n < 3; n++)
  3213. {
  3214. if (n > 0)
  3215. ImGui::SameLine();
  3216. ImGui::PushID(n);
  3217. ImGui::InvisibleButton("##canvas", size);
  3218. if (ImGui::IsItemActive() && ImGui::IsMouseDragging(ImGuiMouseButton_Left))
  3219. {
  3220. offset.x += ImGui::GetIO().MouseDelta.x;
  3221. offset.y += ImGui::GetIO().MouseDelta.y;
  3222. }
  3223. ImGui::PopID();
  3224. if (!ImGui::IsItemVisible()) // Skip rendering as ImDrawList elements are not clipped.
  3225. continue;
  3226. const ImVec2 p0 = ImGui::GetItemRectMin();
  3227. const ImVec2 p1 = ImGui::GetItemRectMax();
  3228. const char* text_str = "Line 1 hello\nLine 2 clip me!";
  3229. const ImVec2 text_pos = ImVec2(p0.x + offset.x, p0.y + offset.y);
  3230. ImDrawList* draw_list = ImGui::GetWindowDrawList();
  3231. switch (n)
  3232. {
  3233. case 0:
  3234. ImGui::PushClipRect(p0, p1, true);
  3235. draw_list->AddRectFilled(p0, p1, IM_COL32(90, 90, 120, 255));
  3236. draw_list->AddText(text_pos, IM_COL32_WHITE, text_str);
  3237. ImGui::PopClipRect();
  3238. break;
  3239. case 1:
  3240. draw_list->PushClipRect(p0, p1, true);
  3241. draw_list->AddRectFilled(p0, p1, IM_COL32(90, 90, 120, 255));
  3242. draw_list->AddText(text_pos, IM_COL32_WHITE, text_str);
  3243. draw_list->PopClipRect();
  3244. break;
  3245. case 2:
  3246. ImVec4 clip_rect(p0.x, p0.y, p1.x, p1.y); // AddText() takes a ImVec4* here so let's convert.
  3247. draw_list->AddRectFilled(p0, p1, IM_COL32(90, 90, 120, 255));
  3248. draw_list->AddText(ImGui::GetFont(), ImGui::GetFontSize(), text_pos, IM_COL32_WHITE, text_str, NULL, 0.0f, &clip_rect);
  3249. break;
  3250. }
  3251. }
  3252. ImGui::TreePop();
  3253. }
  3254. IMGUI_DEMO_MARKER("Layout/Overlap Mode");
  3255. if (ImGui::TreeNode("Overlap Mode"))
  3256. {
  3257. static bool enable_allow_overlap = true;
  3258. HelpMarker(
  3259. "Hit-testing is by default performed in item submission order, which generally is perceived as 'back-to-front'.\n\n"
  3260. "By using SetNextItemAllowOverlap() you can notify that an item may be overlapped by another. "
  3261. "Doing so alters the hovering logic: items using AllowOverlap mode requires an extra frame to accept hovered state.");
  3262. ImGui::Checkbox("Enable AllowOverlap", &enable_allow_overlap);
  3263. ImVec2 button1_pos = ImGui::GetCursorScreenPos();
  3264. ImVec2 button2_pos = ImVec2(button1_pos.x + 50.0f, button1_pos.y + 50.0f);
  3265. if (enable_allow_overlap)
  3266. ImGui::SetNextItemAllowOverlap();
  3267. ImGui::Button("Button 1", ImVec2(80, 80));
  3268. ImGui::SetCursorScreenPos(button2_pos);
  3269. ImGui::Button("Button 2", ImVec2(80, 80));
  3270. // This is typically used with width-spanning items.
  3271. // (note that Selectable() has a dedicated flag ImGuiSelectableFlags_AllowOverlap, which is a shortcut
  3272. // for using SetNextItemAllowOverlap(). For demo purpose we use SetNextItemAllowOverlap() here.)
  3273. if (enable_allow_overlap)
  3274. ImGui::SetNextItemAllowOverlap();
  3275. ImGui::Selectable("Some Selectable", false);
  3276. ImGui::SameLine();
  3277. ImGui::SmallButton("++");
  3278. ImGui::TreePop();
  3279. }
  3280. }
  3281. static void ShowDemoWindowPopups()
  3282. {
  3283. IMGUI_DEMO_MARKER("Popups");
  3284. if (!ImGui::CollapsingHeader("Popups & Modal windows"))
  3285. return;
  3286. // The properties of popups windows are:
  3287. // - They block normal mouse hovering detection outside them. (*)
  3288. // - Unless modal, they can be closed by clicking anywhere outside them, or by pressing ESCAPE.
  3289. // - Their visibility state (~bool) is held internally by Dear ImGui instead of being held by the programmer as
  3290. // we are used to with regular Begin() calls. User can manipulate the visibility state by calling OpenPopup().
  3291. // (*) One can use IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup) to bypass it and detect hovering even
  3292. // when normally blocked by a popup.
  3293. // Those three properties are connected. The library needs to hold their visibility state BECAUSE it can close
  3294. // popups at any time.
  3295. // Typical use for regular windows:
  3296. // bool my_tool_is_active = false; if (ImGui::Button("Open")) my_tool_is_active = true; [...] if (my_tool_is_active) Begin("My Tool", &my_tool_is_active) { [...] } End();
  3297. // Typical use for popups:
  3298. // if (ImGui::Button("Open")) ImGui::OpenPopup("MyPopup"); if (ImGui::BeginPopup("MyPopup") { [...] EndPopup(); }
  3299. // With popups we have to go through a library call (here OpenPopup) to manipulate the visibility state.
  3300. // This may be a bit confusing at first but it should quickly make sense. Follow on the examples below.
  3301. IMGUI_DEMO_MARKER("Popups/Popups");
  3302. if (ImGui::TreeNode("Popups"))
  3303. {
  3304. ImGui::TextWrapped(
  3305. "When a popup is active, it inhibits interacting with windows that are behind the popup. "
  3306. "Clicking outside the popup closes it.");
  3307. static int selected_fish = -1;
  3308. const char* names[] = { "Bream", "Haddock", "Mackerel", "Pollock", "Tilefish" };
  3309. static bool toggles[] = { true, false, false, false, false };
  3310. // Simple selection popup (if you want to show the current selection inside the Button itself,
  3311. // you may want to build a string using the "###" operator to preserve a constant ID with a variable label)
  3312. if (ImGui::Button("Select.."))
  3313. ImGui::OpenPopup("my_select_popup");
  3314. ImGui::SameLine();
  3315. ImGui::TextUnformatted(selected_fish == -1 ? "<None>" : names[selected_fish]);
  3316. if (ImGui::BeginPopup("my_select_popup"))
  3317. {
  3318. ImGui::SeparatorText("Aquarium");
  3319. for (int i = 0; i < IM_ARRAYSIZE(names); i++)
  3320. if (ImGui::Selectable(names[i]))
  3321. selected_fish = i;
  3322. ImGui::EndPopup();
  3323. }
  3324. // Showing a menu with toggles
  3325. if (ImGui::Button("Toggle.."))
  3326. ImGui::OpenPopup("my_toggle_popup");
  3327. if (ImGui::BeginPopup("my_toggle_popup"))
  3328. {
  3329. for (int i = 0; i < IM_ARRAYSIZE(names); i++)
  3330. ImGui::MenuItem(names[i], "", &toggles[i]);
  3331. if (ImGui::BeginMenu("Sub-menu"))
  3332. {
  3333. ImGui::MenuItem("Click me");
  3334. ImGui::EndMenu();
  3335. }
  3336. ImGui::Separator();
  3337. ImGui::Text("Tooltip here");
  3338. ImGui::SetItemTooltip("I am a tooltip over a popup");
  3339. if (ImGui::Button("Stacked Popup"))
  3340. ImGui::OpenPopup("another popup");
  3341. if (ImGui::BeginPopup("another popup"))
  3342. {
  3343. for (int i = 0; i < IM_ARRAYSIZE(names); i++)
  3344. ImGui::MenuItem(names[i], "", &toggles[i]);
  3345. if (ImGui::BeginMenu("Sub-menu"))
  3346. {
  3347. ImGui::MenuItem("Click me");
  3348. if (ImGui::Button("Stacked Popup"))
  3349. ImGui::OpenPopup("another popup");
  3350. if (ImGui::BeginPopup("another popup"))
  3351. {
  3352. ImGui::Text("I am the last one here.");
  3353. ImGui::EndPopup();
  3354. }
  3355. ImGui::EndMenu();
  3356. }
  3357. ImGui::EndPopup();
  3358. }
  3359. ImGui::EndPopup();
  3360. }
  3361. // Call the more complete ShowExampleMenuFile which we use in various places of this demo
  3362. if (ImGui::Button("With a menu.."))
  3363. ImGui::OpenPopup("my_file_popup");
  3364. if (ImGui::BeginPopup("my_file_popup", ImGuiWindowFlags_MenuBar))
  3365. {
  3366. if (ImGui::BeginMenuBar())
  3367. {
  3368. if (ImGui::BeginMenu("File"))
  3369. {
  3370. ShowExampleMenuFile();
  3371. ImGui::EndMenu();
  3372. }
  3373. if (ImGui::BeginMenu("Edit"))
  3374. {
  3375. ImGui::MenuItem("Dummy");
  3376. ImGui::EndMenu();
  3377. }
  3378. ImGui::EndMenuBar();
  3379. }
  3380. ImGui::Text("Hello from popup!");
  3381. ImGui::Button("This is a dummy button..");
  3382. ImGui::EndPopup();
  3383. }
  3384. ImGui::TreePop();
  3385. }
  3386. IMGUI_DEMO_MARKER("Popups/Context menus");
  3387. if (ImGui::TreeNode("Context menus"))
  3388. {
  3389. HelpMarker("\"Context\" functions are simple helpers to associate a Popup to a given Item or Window identifier.");
  3390. // BeginPopupContextItem() is a helper to provide common/simple popup behavior of essentially doing:
  3391. // if (id == 0)
  3392. // id = GetItemID(); // Use last item id
  3393. // if (IsItemHovered() && IsMouseReleased(ImGuiMouseButton_Right))
  3394. // OpenPopup(id);
  3395. // return BeginPopup(id);
  3396. // For advanced uses you may want to replicate and customize this code.
  3397. // See more details in BeginPopupContextItem().
  3398. // Example 1
  3399. // When used after an item that has an ID (e.g. Button), we can skip providing an ID to BeginPopupContextItem(),
  3400. // and BeginPopupContextItem() will use the last item ID as the popup ID.
  3401. {
  3402. const char* names[5] = { "Label1", "Label2", "Label3", "Label4", "Label5" };
  3403. static int selected = -1;
  3404. for (int n = 0; n < 5; n++)
  3405. {
  3406. if (ImGui::Selectable(names[n], selected == n))
  3407. selected = n;
  3408. if (ImGui::BeginPopupContextItem()) // <-- use last item id as popup id
  3409. {
  3410. selected = n;
  3411. ImGui::Text("This a popup for \"%s\"!", names[n]);
  3412. if (ImGui::Button("Close"))
  3413. ImGui::CloseCurrentPopup();
  3414. ImGui::EndPopup();
  3415. }
  3416. ImGui::SetItemTooltip("Right-click to open popup");
  3417. }
  3418. }
  3419. // Example 2
  3420. // Popup on a Text() element which doesn't have an identifier: we need to provide an identifier to BeginPopupContextItem().
  3421. // Using an explicit identifier is also convenient if you want to activate the popups from different locations.
  3422. {
  3423. HelpMarker("Text() elements don't have stable identifiers so we need to provide one.");
  3424. static float value = 0.5f;
  3425. ImGui::Text("Value = %.3f <-- (1) right-click this text", value);
  3426. if (ImGui::BeginPopupContextItem("my popup"))
  3427. {
  3428. if (ImGui::Selectable("Set to zero")) value = 0.0f;
  3429. if (ImGui::Selectable("Set to PI")) value = 3.1415f;
  3430. ImGui::SetNextItemWidth(-FLT_MIN);
  3431. ImGui::DragFloat("##Value", &value, 0.1f, 0.0f, 0.0f);
  3432. ImGui::EndPopup();
  3433. }
  3434. // We can also use OpenPopupOnItemClick() to toggle the visibility of a given popup.
  3435. // Here we make it that right-clicking this other text element opens the same popup as above.
  3436. // The popup itself will be submitted by the code above.
  3437. ImGui::Text("(2) Or right-click this text");
  3438. ImGui::OpenPopupOnItemClick("my popup", ImGuiPopupFlags_MouseButtonRight);
  3439. // Back to square one: manually open the same popup.
  3440. if (ImGui::Button("(3) Or click this button"))
  3441. ImGui::OpenPopup("my popup");
  3442. }
  3443. // Example 3
  3444. // When using BeginPopupContextItem() with an implicit identifier (NULL == use last item ID),
  3445. // we need to make sure your item identifier is stable.
  3446. // In this example we showcase altering the item label while preserving its identifier, using the ### operator (see FAQ).
  3447. {
  3448. HelpMarker("Showcase using a popup ID linked to item ID, with the item having a changing label + stable ID using the ### operator.");
  3449. static char name[32] = "Label1";
  3450. char buf[64];
  3451. sprintf(buf, "Button: %s###Button", name); // ### operator override ID ignoring the preceding label
  3452. ImGui::Button(buf);
  3453. if (ImGui::BeginPopupContextItem())
  3454. {
  3455. ImGui::Text("Edit name:");
  3456. ImGui::InputText("##edit", name, IM_ARRAYSIZE(name));
  3457. if (ImGui::Button("Close"))
  3458. ImGui::CloseCurrentPopup();
  3459. ImGui::EndPopup();
  3460. }
  3461. ImGui::SameLine(); ImGui::Text("(<-- right-click here)");
  3462. }
  3463. ImGui::TreePop();
  3464. }
  3465. IMGUI_DEMO_MARKER("Popups/Modals");
  3466. if (ImGui::TreeNode("Modals"))
  3467. {
  3468. ImGui::TextWrapped("Modal windows are like popups but the user cannot close them by clicking outside.");
  3469. if (ImGui::Button("Delete.."))
  3470. ImGui::OpenPopup("Delete?");
  3471. // Always center this window when appearing
  3472. ImVec2 center = ImGui::GetMainViewport()->GetCenter();
  3473. ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
  3474. if (ImGui::BeginPopupModal("Delete?", NULL, ImGuiWindowFlags_AlwaysAutoResize))
  3475. {
  3476. ImGui::Text("All those beautiful files will be deleted.\nThis operation cannot be undone!");
  3477. ImGui::Separator();
  3478. //static int unused_i = 0;
  3479. //ImGui::Combo("Combo", &unused_i, "Delete\0Delete harder\0");
  3480. static bool dont_ask_me_next_time = false;
  3481. ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
  3482. ImGui::Checkbox("Don't ask me next time", &dont_ask_me_next_time);
  3483. ImGui::PopStyleVar();
  3484. if (ImGui::Button("OK", ImVec2(120, 0))) { ImGui::CloseCurrentPopup(); }
  3485. ImGui::SetItemDefaultFocus();
  3486. ImGui::SameLine();
  3487. if (ImGui::Button("Cancel", ImVec2(120, 0))) { ImGui::CloseCurrentPopup(); }
  3488. ImGui::EndPopup();
  3489. }
  3490. if (ImGui::Button("Stacked modals.."))
  3491. ImGui::OpenPopup("Stacked 1");
  3492. if (ImGui::BeginPopupModal("Stacked 1", NULL, ImGuiWindowFlags_MenuBar))
  3493. {
  3494. if (ImGui::BeginMenuBar())
  3495. {
  3496. if (ImGui::BeginMenu("File"))
  3497. {
  3498. if (ImGui::MenuItem("Some menu item")) {}
  3499. ImGui::EndMenu();
  3500. }
  3501. ImGui::EndMenuBar();
  3502. }
  3503. ImGui::Text("Hello from Stacked The First\nUsing style.Colors[ImGuiCol_ModalWindowDimBg] behind it.");
  3504. // Testing behavior of widgets stacking their own regular popups over the modal.
  3505. static int item = 1;
  3506. static float color[4] = { 0.4f, 0.7f, 0.0f, 0.5f };
  3507. ImGui::Combo("Combo", &item, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0");
  3508. ImGui::ColorEdit4("Color", color);
  3509. if (ImGui::Button("Add another modal.."))
  3510. ImGui::OpenPopup("Stacked 2");
  3511. // Also demonstrate passing a bool* to BeginPopupModal(), this will create a regular close button which
  3512. // will close the popup. Note that the visibility state of popups is owned by imgui, so the input value
  3513. // of the bool actually doesn't matter here.
  3514. bool unused_open = true;
  3515. if (ImGui::BeginPopupModal("Stacked 2", &unused_open))
  3516. {
  3517. ImGui::Text("Hello from Stacked The Second!");
  3518. ImGui::ColorEdit4("Color", color); // Allow opening another nested popup
  3519. if (ImGui::Button("Close"))
  3520. ImGui::CloseCurrentPopup();
  3521. ImGui::EndPopup();
  3522. }
  3523. if (ImGui::Button("Close"))
  3524. ImGui::CloseCurrentPopup();
  3525. ImGui::EndPopup();
  3526. }
  3527. ImGui::TreePop();
  3528. }
  3529. IMGUI_DEMO_MARKER("Popups/Menus inside a regular window");
  3530. if (ImGui::TreeNode("Menus inside a regular window"))
  3531. {
  3532. ImGui::TextWrapped("Below we are testing adding menu items to a regular window. It's rather unusual but should work!");
  3533. ImGui::Separator();
  3534. ImGui::MenuItem("Menu item", "CTRL+M");
  3535. if (ImGui::BeginMenu("Menu inside a regular window"))
  3536. {
  3537. ShowExampleMenuFile();
  3538. ImGui::EndMenu();
  3539. }
  3540. ImGui::Separator();
  3541. ImGui::TreePop();
  3542. }
  3543. }
  3544. // Dummy data structure that we use for the Table demo.
  3545. // (pre-C++11 doesn't allow us to instantiate ImVector<MyItem> template if this structure is defined inside the demo function)
  3546. namespace
  3547. {
  3548. // We are passing our own identifier to TableSetupColumn() to facilitate identifying columns in the sorting code.
  3549. // This identifier will be passed down into ImGuiTableSortSpec::ColumnUserID.
  3550. // But it is possible to omit the user id parameter of TableSetupColumn() and just use the column index instead! (ImGuiTableSortSpec::ColumnIndex)
  3551. // If you don't use sorting, you will generally never care about giving column an ID!
  3552. enum MyItemColumnID
  3553. {
  3554. MyItemColumnID_ID,
  3555. MyItemColumnID_Name,
  3556. MyItemColumnID_Action,
  3557. MyItemColumnID_Quantity,
  3558. MyItemColumnID_Description
  3559. };
  3560. struct MyItem
  3561. {
  3562. int ID;
  3563. const char* Name;
  3564. int Quantity;
  3565. // We have a problem which is affecting _only this demo_ and should not affect your code:
  3566. // As we don't rely on std:: or other third-party library to compile dear imgui, we only have reliable access to qsort(),
  3567. // however qsort doesn't allow passing user data to comparing function.
  3568. // As a workaround, we are storing the sort specs in a static/global for the comparing function to access.
  3569. // In your own use case you would probably pass the sort specs to your sorting/comparing functions directly and not use a global.
  3570. // We could technically call ImGui::TableGetSortSpecs() in CompareWithSortSpecs(), but considering that this function is called
  3571. // very often by the sorting algorithm it would be a little wasteful.
  3572. static const ImGuiTableSortSpecs* s_current_sort_specs;
  3573. static void SortWithSortSpecs(ImGuiTableSortSpecs* sort_specs, MyItem* items, int items_count)
  3574. {
  3575. s_current_sort_specs = sort_specs; // Store in variable accessible by the sort function.
  3576. if (items_count > 1)
  3577. qsort(items, (size_t)items_count, sizeof(items[0]), MyItem::CompareWithSortSpecs);
  3578. s_current_sort_specs = NULL;
  3579. }
  3580. // Compare function to be used by qsort()
  3581. static int IMGUI_CDECL CompareWithSortSpecs(const void* lhs, const void* rhs)
  3582. {
  3583. const MyItem* a = (const MyItem*)lhs;
  3584. const MyItem* b = (const MyItem*)rhs;
  3585. for (int n = 0; n < s_current_sort_specs->SpecsCount; n++)
  3586. {
  3587. // Here we identify columns using the ColumnUserID value that we ourselves passed to TableSetupColumn()
  3588. // We could also choose to identify columns based on their index (sort_spec->ColumnIndex), which is simpler!
  3589. const ImGuiTableColumnSortSpecs* sort_spec = &s_current_sort_specs->Specs[n];
  3590. int delta = 0;
  3591. switch (sort_spec->ColumnUserID)
  3592. {
  3593. case MyItemColumnID_ID: delta = (a->ID - b->ID); break;
  3594. case MyItemColumnID_Name: delta = (strcmp(a->Name, b->Name)); break;
  3595. case MyItemColumnID_Quantity: delta = (a->Quantity - b->Quantity); break;
  3596. case MyItemColumnID_Description: delta = (strcmp(a->Name, b->Name)); break;
  3597. default: IM_ASSERT(0); break;
  3598. }
  3599. if (delta > 0)
  3600. return (sort_spec->SortDirection == ImGuiSortDirection_Ascending) ? +1 : -1;
  3601. if (delta < 0)
  3602. return (sort_spec->SortDirection == ImGuiSortDirection_Ascending) ? -1 : +1;
  3603. }
  3604. // qsort() is instable so always return a way to differenciate items.
  3605. // Your own compare function may want to avoid fallback on implicit sort specs.
  3606. // e.g. a Name compare if it wasn't already part of the sort specs.
  3607. return (a->ID - b->ID);
  3608. }
  3609. };
  3610. const ImGuiTableSortSpecs* MyItem::s_current_sort_specs = NULL;
  3611. }
  3612. // Make the UI compact because there are so many fields
  3613. static void PushStyleCompact()
  3614. {
  3615. ImGuiStyle& style = ImGui::GetStyle();
  3616. ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(style.FramePadding.x, (float)(int)(style.FramePadding.y * 0.60f)));
  3617. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x, (float)(int)(style.ItemSpacing.y * 0.60f)));
  3618. }
  3619. static void PopStyleCompact()
  3620. {
  3621. ImGui::PopStyleVar(2);
  3622. }
  3623. // Show a combo box with a choice of sizing policies
  3624. static void EditTableSizingFlags(ImGuiTableFlags* p_flags)
  3625. {
  3626. struct EnumDesc { ImGuiTableFlags Value; const char* Name; const char* Tooltip; };
  3627. static const EnumDesc policies[] =
  3628. {
  3629. { ImGuiTableFlags_None, "Default", "Use default sizing policy:\n- ImGuiTableFlags_SizingFixedFit if ScrollX is on or if host window has ImGuiWindowFlags_AlwaysAutoResize.\n- ImGuiTableFlags_SizingStretchSame otherwise." },
  3630. { ImGuiTableFlags_SizingFixedFit, "ImGuiTableFlags_SizingFixedFit", "Columns default to _WidthFixed (if resizable) or _WidthAuto (if not resizable), matching contents width." },
  3631. { ImGuiTableFlags_SizingFixedSame, "ImGuiTableFlags_SizingFixedSame", "Columns are all the same width, matching the maximum contents width.\nImplicitly disable ImGuiTableFlags_Resizable and enable ImGuiTableFlags_NoKeepColumnsVisible." },
  3632. { ImGuiTableFlags_SizingStretchProp, "ImGuiTableFlags_SizingStretchProp", "Columns default to _WidthStretch with weights proportional to their widths." },
  3633. { ImGuiTableFlags_SizingStretchSame, "ImGuiTableFlags_SizingStretchSame", "Columns default to _WidthStretch with same weights." }
  3634. };
  3635. int idx;
  3636. for (idx = 0; idx < IM_ARRAYSIZE(policies); idx++)
  3637. if (policies[idx].Value == (*p_flags & ImGuiTableFlags_SizingMask_))
  3638. break;
  3639. const char* preview_text = (idx < IM_ARRAYSIZE(policies)) ? policies[idx].Name + (idx > 0 ? strlen("ImGuiTableFlags") : 0) : "";
  3640. if (ImGui::BeginCombo("Sizing Policy", preview_text))
  3641. {
  3642. for (int n = 0; n < IM_ARRAYSIZE(policies); n++)
  3643. if (ImGui::Selectable(policies[n].Name, idx == n))
  3644. *p_flags = (*p_flags & ~ImGuiTableFlags_SizingMask_) | policies[n].Value;
  3645. ImGui::EndCombo();
  3646. }
  3647. ImGui::SameLine();
  3648. ImGui::TextDisabled("(?)");
  3649. if (ImGui::BeginItemTooltip())
  3650. {
  3651. ImGui::PushTextWrapPos(ImGui::GetFontSize() * 50.0f);
  3652. for (int m = 0; m < IM_ARRAYSIZE(policies); m++)
  3653. {
  3654. ImGui::Separator();
  3655. ImGui::Text("%s:", policies[m].Name);
  3656. ImGui::Separator();
  3657. ImGui::SetCursorPosX(ImGui::GetCursorPosX() + ImGui::GetStyle().IndentSpacing * 0.5f);
  3658. ImGui::TextUnformatted(policies[m].Tooltip);
  3659. }
  3660. ImGui::PopTextWrapPos();
  3661. ImGui::EndTooltip();
  3662. }
  3663. }
  3664. static void EditTableColumnsFlags(ImGuiTableColumnFlags* p_flags)
  3665. {
  3666. ImGui::CheckboxFlags("_Disabled", p_flags, ImGuiTableColumnFlags_Disabled); ImGui::SameLine(); HelpMarker("Master disable flag (also hide from context menu)");
  3667. ImGui::CheckboxFlags("_DefaultHide", p_flags, ImGuiTableColumnFlags_DefaultHide);
  3668. ImGui::CheckboxFlags("_DefaultSort", p_flags, ImGuiTableColumnFlags_DefaultSort);
  3669. if (ImGui::CheckboxFlags("_WidthStretch", p_flags, ImGuiTableColumnFlags_WidthStretch))
  3670. *p_flags &= ~(ImGuiTableColumnFlags_WidthMask_ ^ ImGuiTableColumnFlags_WidthStretch);
  3671. if (ImGui::CheckboxFlags("_WidthFixed", p_flags, ImGuiTableColumnFlags_WidthFixed))
  3672. *p_flags &= ~(ImGuiTableColumnFlags_WidthMask_ ^ ImGuiTableColumnFlags_WidthFixed);
  3673. ImGui::CheckboxFlags("_NoResize", p_flags, ImGuiTableColumnFlags_NoResize);
  3674. ImGui::CheckboxFlags("_NoReorder", p_flags, ImGuiTableColumnFlags_NoReorder);
  3675. ImGui::CheckboxFlags("_NoHide", p_flags, ImGuiTableColumnFlags_NoHide);
  3676. ImGui::CheckboxFlags("_NoClip", p_flags, ImGuiTableColumnFlags_NoClip);
  3677. ImGui::CheckboxFlags("_NoSort", p_flags, ImGuiTableColumnFlags_NoSort);
  3678. ImGui::CheckboxFlags("_NoSortAscending", p_flags, ImGuiTableColumnFlags_NoSortAscending);
  3679. ImGui::CheckboxFlags("_NoSortDescending", p_flags, ImGuiTableColumnFlags_NoSortDescending);
  3680. ImGui::CheckboxFlags("_NoHeaderLabel", p_flags, ImGuiTableColumnFlags_NoHeaderLabel);
  3681. ImGui::CheckboxFlags("_NoHeaderWidth", p_flags, ImGuiTableColumnFlags_NoHeaderWidth);
  3682. ImGui::CheckboxFlags("_PreferSortAscending", p_flags, ImGuiTableColumnFlags_PreferSortAscending);
  3683. ImGui::CheckboxFlags("_PreferSortDescending", p_flags, ImGuiTableColumnFlags_PreferSortDescending);
  3684. ImGui::CheckboxFlags("_IndentEnable", p_flags, ImGuiTableColumnFlags_IndentEnable); ImGui::SameLine(); HelpMarker("Default for column 0");
  3685. ImGui::CheckboxFlags("_IndentDisable", p_flags, ImGuiTableColumnFlags_IndentDisable); ImGui::SameLine(); HelpMarker("Default for column >0");
  3686. ImGui::CheckboxFlags("_AngledHeader", p_flags, ImGuiTableColumnFlags_AngledHeader);
  3687. }
  3688. static void ShowTableColumnsStatusFlags(ImGuiTableColumnFlags flags)
  3689. {
  3690. ImGui::CheckboxFlags("_IsEnabled", &flags, ImGuiTableColumnFlags_IsEnabled);
  3691. ImGui::CheckboxFlags("_IsVisible", &flags, ImGuiTableColumnFlags_IsVisible);
  3692. ImGui::CheckboxFlags("_IsSorted", &flags, ImGuiTableColumnFlags_IsSorted);
  3693. ImGui::CheckboxFlags("_IsHovered", &flags, ImGuiTableColumnFlags_IsHovered);
  3694. }
  3695. static void ShowDemoWindowTables()
  3696. {
  3697. //ImGui::SetNextItemOpen(true, ImGuiCond_Once);
  3698. IMGUI_DEMO_MARKER("Tables");
  3699. if (!ImGui::CollapsingHeader("Tables & Columns"))
  3700. return;
  3701. // Using those as a base value to create width/height that are factor of the size of our font
  3702. const float TEXT_BASE_WIDTH = ImGui::CalcTextSize("A").x;
  3703. const float TEXT_BASE_HEIGHT = ImGui::GetTextLineHeightWithSpacing();
  3704. ImGui::PushID("Tables");
  3705. int open_action = -1;
  3706. if (ImGui::Button("Expand all"))
  3707. open_action = 1;
  3708. ImGui::SameLine();
  3709. if (ImGui::Button("Collapse all"))
  3710. open_action = 0;
  3711. ImGui::SameLine();
  3712. // Options
  3713. static bool disable_indent = false;
  3714. ImGui::Checkbox("Disable tree indentation", &disable_indent);
  3715. ImGui::SameLine();
  3716. HelpMarker("Disable the indenting of tree nodes so demo tables can use the full window width.");
  3717. ImGui::Separator();
  3718. if (disable_indent)
  3719. ImGui::PushStyleVar(ImGuiStyleVar_IndentSpacing, 0.0f);
  3720. // About Styling of tables
  3721. // Most settings are configured on a per-table basis via the flags passed to BeginTable() and TableSetupColumns APIs.
  3722. // There are however a few settings that a shared and part of the ImGuiStyle structure:
  3723. // style.CellPadding // Padding within each cell
  3724. // style.Colors[ImGuiCol_TableHeaderBg] // Table header background
  3725. // style.Colors[ImGuiCol_TableBorderStrong] // Table outer and header borders
  3726. // style.Colors[ImGuiCol_TableBorderLight] // Table inner borders
  3727. // style.Colors[ImGuiCol_TableRowBg] // Table row background when ImGuiTableFlags_RowBg is enabled (even rows)
  3728. // style.Colors[ImGuiCol_TableRowBgAlt] // Table row background when ImGuiTableFlags_RowBg is enabled (odds rows)
  3729. // Demos
  3730. if (open_action != -1)
  3731. ImGui::SetNextItemOpen(open_action != 0);
  3732. IMGUI_DEMO_MARKER("Tables/Basic");
  3733. if (ImGui::TreeNode("Basic"))
  3734. {
  3735. // Here we will showcase three different ways to output a table.
  3736. // They are very simple variations of a same thing!
  3737. // [Method 1] Using TableNextRow() to create a new row, and TableSetColumnIndex() to select the column.
  3738. // In many situations, this is the most flexible and easy to use pattern.
  3739. HelpMarker("Using TableNextRow() + calling TableSetColumnIndex() _before_ each cell, in a loop.");
  3740. if (ImGui::BeginTable("table1", 3))
  3741. {
  3742. for (int row = 0; row < 4; row++)
  3743. {
  3744. ImGui::TableNextRow();
  3745. for (int column = 0; column < 3; column++)
  3746. {
  3747. ImGui::TableSetColumnIndex(column);
  3748. ImGui::Text("Row %d Column %d", row, column);
  3749. }
  3750. }
  3751. ImGui::EndTable();
  3752. }
  3753. // [Method 2] Using TableNextColumn() called multiple times, instead of using a for loop + TableSetColumnIndex().
  3754. // This is generally more convenient when you have code manually submitting the contents of each column.
  3755. HelpMarker("Using TableNextRow() + calling TableNextColumn() _before_ each cell, manually.");
  3756. if (ImGui::BeginTable("table2", 3))
  3757. {
  3758. for (int row = 0; row < 4; row++)
  3759. {
  3760. ImGui::TableNextRow();
  3761. ImGui::TableNextColumn();
  3762. ImGui::Text("Row %d", row);
  3763. ImGui::TableNextColumn();
  3764. ImGui::Text("Some contents");
  3765. ImGui::TableNextColumn();
  3766. ImGui::Text("123.456");
  3767. }
  3768. ImGui::EndTable();
  3769. }
  3770. // [Method 3] We call TableNextColumn() _before_ each cell. We never call TableNextRow(),
  3771. // as TableNextColumn() will automatically wrap around and create new rows as needed.
  3772. // This is generally more convenient when your cells all contains the same type of data.
  3773. HelpMarker(
  3774. "Only using TableNextColumn(), which tends to be convenient for tables where every cell contains "
  3775. "the same type of contents.\n This is also more similar to the old NextColumn() function of the "
  3776. "Columns API, and provided to facilitate the Columns->Tables API transition.");
  3777. if (ImGui::BeginTable("table3", 3))
  3778. {
  3779. for (int item = 0; item < 14; item++)
  3780. {
  3781. ImGui::TableNextColumn();
  3782. ImGui::Text("Item %d", item);
  3783. }
  3784. ImGui::EndTable();
  3785. }
  3786. ImGui::TreePop();
  3787. }
  3788. if (open_action != -1)
  3789. ImGui::SetNextItemOpen(open_action != 0);
  3790. IMGUI_DEMO_MARKER("Tables/Borders, background");
  3791. if (ImGui::TreeNode("Borders, background"))
  3792. {
  3793. // Expose a few Borders related flags interactively
  3794. enum ContentsType { CT_Text, CT_FillButton };
  3795. static ImGuiTableFlags flags = ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg;
  3796. static bool display_headers = false;
  3797. static int contents_type = CT_Text;
  3798. PushStyleCompact();
  3799. ImGui::CheckboxFlags("ImGuiTableFlags_RowBg", &flags, ImGuiTableFlags_RowBg);
  3800. ImGui::CheckboxFlags("ImGuiTableFlags_Borders", &flags, ImGuiTableFlags_Borders);
  3801. ImGui::SameLine(); HelpMarker("ImGuiTableFlags_Borders\n = ImGuiTableFlags_BordersInnerV\n | ImGuiTableFlags_BordersOuterV\n | ImGuiTableFlags_BordersInnerV\n | ImGuiTableFlags_BordersOuterH");
  3802. ImGui::Indent();
  3803. ImGui::CheckboxFlags("ImGuiTableFlags_BordersH", &flags, ImGuiTableFlags_BordersH);
  3804. ImGui::Indent();
  3805. ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterH", &flags, ImGuiTableFlags_BordersOuterH);
  3806. ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerH", &flags, ImGuiTableFlags_BordersInnerH);
  3807. ImGui::Unindent();
  3808. ImGui::CheckboxFlags("ImGuiTableFlags_BordersV", &flags, ImGuiTableFlags_BordersV);
  3809. ImGui::Indent();
  3810. ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterV", &flags, ImGuiTableFlags_BordersOuterV);
  3811. ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerV", &flags, ImGuiTableFlags_BordersInnerV);
  3812. ImGui::Unindent();
  3813. ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuter", &flags, ImGuiTableFlags_BordersOuter);
  3814. ImGui::CheckboxFlags("ImGuiTableFlags_BordersInner", &flags, ImGuiTableFlags_BordersInner);
  3815. ImGui::Unindent();
  3816. ImGui::AlignTextToFramePadding(); ImGui::Text("Cell contents:");
  3817. ImGui::SameLine(); ImGui::RadioButton("Text", &contents_type, CT_Text);
  3818. ImGui::SameLine(); ImGui::RadioButton("FillButton", &contents_type, CT_FillButton);
  3819. ImGui::Checkbox("Display headers", &display_headers);
  3820. ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body (borders will always appear in Headers");
  3821. PopStyleCompact();
  3822. if (ImGui::BeginTable("table1", 3, flags))
  3823. {
  3824. // Display headers so we can inspect their interaction with borders
  3825. // (Headers are not the main purpose of this section of the demo, so we are not elaborating on them now. See other sections for details)
  3826. if (display_headers)
  3827. {
  3828. ImGui::TableSetupColumn("One");
  3829. ImGui::TableSetupColumn("Two");
  3830. ImGui::TableSetupColumn("Three");
  3831. ImGui::TableHeadersRow();
  3832. }
  3833. for (int row = 0; row < 5; row++)
  3834. {
  3835. ImGui::TableNextRow();
  3836. for (int column = 0; column < 3; column++)
  3837. {
  3838. ImGui::TableSetColumnIndex(column);
  3839. char buf[32];
  3840. sprintf(buf, "Hello %d,%d", column, row);
  3841. if (contents_type == CT_Text)
  3842. ImGui::TextUnformatted(buf);
  3843. else if (contents_type == CT_FillButton)
  3844. ImGui::Button(buf, ImVec2(-FLT_MIN, 0.0f));
  3845. }
  3846. }
  3847. ImGui::EndTable();
  3848. }
  3849. ImGui::TreePop();
  3850. }
  3851. if (open_action != -1)
  3852. ImGui::SetNextItemOpen(open_action != 0);
  3853. IMGUI_DEMO_MARKER("Tables/Resizable, stretch");
  3854. if (ImGui::TreeNode("Resizable, stretch"))
  3855. {
  3856. // By default, if we don't enable ScrollX the sizing policy for each column is "Stretch"
  3857. // All columns maintain a sizing weight, and they will occupy all available width.
  3858. static ImGuiTableFlags flags = ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_ContextMenuInBody;
  3859. PushStyleCompact();
  3860. ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable);
  3861. ImGui::CheckboxFlags("ImGuiTableFlags_BordersV", &flags, ImGuiTableFlags_BordersV);
  3862. ImGui::SameLine(); HelpMarker(
  3863. "Using the _Resizable flag automatically enables the _BordersInnerV flag as well, "
  3864. "this is why the resize borders are still showing when unchecking this.");
  3865. PopStyleCompact();
  3866. if (ImGui::BeginTable("table1", 3, flags))
  3867. {
  3868. for (int row = 0; row < 5; row++)
  3869. {
  3870. ImGui::TableNextRow();
  3871. for (int column = 0; column < 3; column++)
  3872. {
  3873. ImGui::TableSetColumnIndex(column);
  3874. ImGui::Text("Hello %d,%d", column, row);
  3875. }
  3876. }
  3877. ImGui::EndTable();
  3878. }
  3879. ImGui::TreePop();
  3880. }
  3881. if (open_action != -1)
  3882. ImGui::SetNextItemOpen(open_action != 0);
  3883. IMGUI_DEMO_MARKER("Tables/Resizable, fixed");
  3884. if (ImGui::TreeNode("Resizable, fixed"))
  3885. {
  3886. // Here we use ImGuiTableFlags_SizingFixedFit (even though _ScrollX is not set)
  3887. // So columns will adopt the "Fixed" policy and will maintain a fixed width regardless of the whole available width (unless table is small)
  3888. // If there is not enough available width to fit all columns, they will however be resized down.
  3889. // FIXME-TABLE: Providing a stretch-on-init would make sense especially for tables which don't have saved settings
  3890. HelpMarker(
  3891. "Using _Resizable + _SizingFixedFit flags.\n"
  3892. "Fixed-width columns generally makes more sense if you want to use horizontal scrolling.\n\n"
  3893. "Double-click a column border to auto-fit the column to its contents.");
  3894. PushStyleCompact();
  3895. static ImGuiTableFlags flags = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_ContextMenuInBody;
  3896. ImGui::CheckboxFlags("ImGuiTableFlags_NoHostExtendX", &flags, ImGuiTableFlags_NoHostExtendX);
  3897. PopStyleCompact();
  3898. if (ImGui::BeginTable("table1", 3, flags))
  3899. {
  3900. for (int row = 0; row < 5; row++)
  3901. {
  3902. ImGui::TableNextRow();
  3903. for (int column = 0; column < 3; column++)
  3904. {
  3905. ImGui::TableSetColumnIndex(column);
  3906. ImGui::Text("Hello %d,%d", column, row);
  3907. }
  3908. }
  3909. ImGui::EndTable();
  3910. }
  3911. ImGui::TreePop();
  3912. }
  3913. if (open_action != -1)
  3914. ImGui::SetNextItemOpen(open_action != 0);
  3915. IMGUI_DEMO_MARKER("Tables/Resizable, mixed");
  3916. if (ImGui::TreeNode("Resizable, mixed"))
  3917. {
  3918. HelpMarker(
  3919. "Using TableSetupColumn() to alter resizing policy on a per-column basis.\n\n"
  3920. "When combining Fixed and Stretch columns, generally you only want one, maybe two trailing columns to use _WidthStretch.");
  3921. static ImGuiTableFlags flags = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_RowBg | ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable;
  3922. if (ImGui::BeginTable("table1", 3, flags))
  3923. {
  3924. ImGui::TableSetupColumn("AAA", ImGuiTableColumnFlags_WidthFixed);
  3925. ImGui::TableSetupColumn("BBB", ImGuiTableColumnFlags_WidthFixed);
  3926. ImGui::TableSetupColumn("CCC", ImGuiTableColumnFlags_WidthStretch);
  3927. ImGui::TableHeadersRow();
  3928. for (int row = 0; row < 5; row++)
  3929. {
  3930. ImGui::TableNextRow();
  3931. for (int column = 0; column < 3; column++)
  3932. {
  3933. ImGui::TableSetColumnIndex(column);
  3934. ImGui::Text("%s %d,%d", (column == 2) ? "Stretch" : "Fixed", column, row);
  3935. }
  3936. }
  3937. ImGui::EndTable();
  3938. }
  3939. if (ImGui::BeginTable("table2", 6, flags))
  3940. {
  3941. ImGui::TableSetupColumn("AAA", ImGuiTableColumnFlags_WidthFixed);
  3942. ImGui::TableSetupColumn("BBB", ImGuiTableColumnFlags_WidthFixed);
  3943. ImGui::TableSetupColumn("CCC", ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_DefaultHide);
  3944. ImGui::TableSetupColumn("DDD", ImGuiTableColumnFlags_WidthStretch);
  3945. ImGui::TableSetupColumn("EEE", ImGuiTableColumnFlags_WidthStretch);
  3946. ImGui::TableSetupColumn("FFF", ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_DefaultHide);
  3947. ImGui::TableHeadersRow();
  3948. for (int row = 0; row < 5; row++)
  3949. {
  3950. ImGui::TableNextRow();
  3951. for (int column = 0; column < 6; column++)
  3952. {
  3953. ImGui::TableSetColumnIndex(column);
  3954. ImGui::Text("%s %d,%d", (column >= 3) ? "Stretch" : "Fixed", column, row);
  3955. }
  3956. }
  3957. ImGui::EndTable();
  3958. }
  3959. ImGui::TreePop();
  3960. }
  3961. if (open_action != -1)
  3962. ImGui::SetNextItemOpen(open_action != 0);
  3963. IMGUI_DEMO_MARKER("Tables/Reorderable, hideable, with headers");
  3964. if (ImGui::TreeNode("Reorderable, hideable, with headers"))
  3965. {
  3966. HelpMarker(
  3967. "Click and drag column headers to reorder columns.\n\n"
  3968. "Right-click on a header to open a context menu.");
  3969. static ImGuiTableFlags flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV;
  3970. PushStyleCompact();
  3971. ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable);
  3972. ImGui::CheckboxFlags("ImGuiTableFlags_Reorderable", &flags, ImGuiTableFlags_Reorderable);
  3973. ImGui::CheckboxFlags("ImGuiTableFlags_Hideable", &flags, ImGuiTableFlags_Hideable);
  3974. ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody);
  3975. ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBodyUntilResize", &flags, ImGuiTableFlags_NoBordersInBodyUntilResize); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body until hovered for resize (borders will always appear in Headers)");
  3976. ImGui::CheckboxFlags("ImGuiTableFlags_HighlightHoveredColumn", &flags, ImGuiTableFlags_HighlightHoveredColumn);
  3977. PopStyleCompact();
  3978. if (ImGui::BeginTable("table1", 3, flags))
  3979. {
  3980. // Submit columns name with TableSetupColumn() and call TableHeadersRow() to create a row with a header in each column.
  3981. // (Later we will show how TableSetupColumn() has other uses, optional flags, sizing weight etc.)
  3982. ImGui::TableSetupColumn("One");
  3983. ImGui::TableSetupColumn("Two");
  3984. ImGui::TableSetupColumn("Three");
  3985. ImGui::TableHeadersRow();
  3986. for (int row = 0; row < 6; row++)
  3987. {
  3988. ImGui::TableNextRow();
  3989. for (int column = 0; column < 3; column++)
  3990. {
  3991. ImGui::TableSetColumnIndex(column);
  3992. ImGui::Text("Hello %d,%d", column, row);
  3993. }
  3994. }
  3995. ImGui::EndTable();
  3996. }
  3997. // Use outer_size.x == 0.0f instead of default to make the table as tight as possible
  3998. // (only valid when no scrolling and no stretch column)
  3999. if (ImGui::BeginTable("table2", 3, flags | ImGuiTableFlags_SizingFixedFit, ImVec2(0.0f, 0.0f)))
  4000. {
  4001. ImGui::TableSetupColumn("One");
  4002. ImGui::TableSetupColumn("Two");
  4003. ImGui::TableSetupColumn("Three");
  4004. ImGui::TableHeadersRow();
  4005. for (int row = 0; row < 6; row++)
  4006. {
  4007. ImGui::TableNextRow();
  4008. for (int column = 0; column < 3; column++)
  4009. {
  4010. ImGui::TableSetColumnIndex(column);
  4011. ImGui::Text("Fixed %d,%d", column, row);
  4012. }
  4013. }
  4014. ImGui::EndTable();
  4015. }
  4016. ImGui::TreePop();
  4017. }
  4018. if (open_action != -1)
  4019. ImGui::SetNextItemOpen(open_action != 0);
  4020. IMGUI_DEMO_MARKER("Tables/Padding");
  4021. if (ImGui::TreeNode("Padding"))
  4022. {
  4023. // First example: showcase use of padding flags and effect of BorderOuterV/BorderInnerV on X padding.
  4024. // We don't expose BorderOuterH/BorderInnerH here because they have no effect on X padding.
  4025. HelpMarker(
  4026. "We often want outer padding activated when any using features which makes the edges of a column visible:\n"
  4027. "e.g.:\n"
  4028. "- BorderOuterV\n"
  4029. "- any form of row selection\n"
  4030. "Because of this, activating BorderOuterV sets the default to PadOuterX. "
  4031. "Using PadOuterX or NoPadOuterX you can override the default.\n\n"
  4032. "Actual padding values are using style.CellPadding.\n\n"
  4033. "In this demo we don't show horizontal borders to emphasize how they don't affect default horizontal padding.");
  4034. static ImGuiTableFlags flags1 = ImGuiTableFlags_BordersV;
  4035. PushStyleCompact();
  4036. ImGui::CheckboxFlags("ImGuiTableFlags_PadOuterX", &flags1, ImGuiTableFlags_PadOuterX);
  4037. ImGui::SameLine(); HelpMarker("Enable outer-most padding (default if ImGuiTableFlags_BordersOuterV is set)");
  4038. ImGui::CheckboxFlags("ImGuiTableFlags_NoPadOuterX", &flags1, ImGuiTableFlags_NoPadOuterX);
  4039. ImGui::SameLine(); HelpMarker("Disable outer-most padding (default if ImGuiTableFlags_BordersOuterV is not set)");
  4040. ImGui::CheckboxFlags("ImGuiTableFlags_NoPadInnerX", &flags1, ImGuiTableFlags_NoPadInnerX);
  4041. ImGui::SameLine(); HelpMarker("Disable inner padding between columns (double inner padding if BordersOuterV is on, single inner padding if BordersOuterV is off)");
  4042. ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterV", &flags1, ImGuiTableFlags_BordersOuterV);
  4043. ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerV", &flags1, ImGuiTableFlags_BordersInnerV);
  4044. static bool show_headers = false;
  4045. ImGui::Checkbox("show_headers", &show_headers);
  4046. PopStyleCompact();
  4047. if (ImGui::BeginTable("table_padding", 3, flags1))
  4048. {
  4049. if (show_headers)
  4050. {
  4051. ImGui::TableSetupColumn("One");
  4052. ImGui::TableSetupColumn("Two");
  4053. ImGui::TableSetupColumn("Three");
  4054. ImGui::TableHeadersRow();
  4055. }
  4056. for (int row = 0; row < 5; row++)
  4057. {
  4058. ImGui::TableNextRow();
  4059. for (int column = 0; column < 3; column++)
  4060. {
  4061. ImGui::TableSetColumnIndex(column);
  4062. if (row == 0)
  4063. {
  4064. ImGui::Text("Avail %.2f", ImGui::GetContentRegionAvail().x);
  4065. }
  4066. else
  4067. {
  4068. char buf[32];
  4069. sprintf(buf, "Hello %d,%d", column, row);
  4070. ImGui::Button(buf, ImVec2(-FLT_MIN, 0.0f));
  4071. }
  4072. //if (ImGui::TableGetColumnFlags() & ImGuiTableColumnFlags_IsHovered)
  4073. // ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, IM_COL32(0, 100, 0, 255));
  4074. }
  4075. }
  4076. ImGui::EndTable();
  4077. }
  4078. // Second example: set style.CellPadding to (0.0) or a custom value.
  4079. // FIXME-TABLE: Vertical border effectively not displayed the same way as horizontal one...
  4080. HelpMarker("Setting style.CellPadding to (0,0) or a custom value.");
  4081. static ImGuiTableFlags flags2 = ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg;
  4082. static ImVec2 cell_padding(0.0f, 0.0f);
  4083. static bool show_widget_frame_bg = true;
  4084. PushStyleCompact();
  4085. ImGui::CheckboxFlags("ImGuiTableFlags_Borders", &flags2, ImGuiTableFlags_Borders);
  4086. ImGui::CheckboxFlags("ImGuiTableFlags_BordersH", &flags2, ImGuiTableFlags_BordersH);
  4087. ImGui::CheckboxFlags("ImGuiTableFlags_BordersV", &flags2, ImGuiTableFlags_BordersV);
  4088. ImGui::CheckboxFlags("ImGuiTableFlags_BordersInner", &flags2, ImGuiTableFlags_BordersInner);
  4089. ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuter", &flags2, ImGuiTableFlags_BordersOuter);
  4090. ImGui::CheckboxFlags("ImGuiTableFlags_RowBg", &flags2, ImGuiTableFlags_RowBg);
  4091. ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags2, ImGuiTableFlags_Resizable);
  4092. ImGui::Checkbox("show_widget_frame_bg", &show_widget_frame_bg);
  4093. ImGui::SliderFloat2("CellPadding", &cell_padding.x, 0.0f, 10.0f, "%.0f");
  4094. PopStyleCompact();
  4095. ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, cell_padding);
  4096. if (ImGui::BeginTable("table_padding_2", 3, flags2))
  4097. {
  4098. static char text_bufs[3 * 5][16]; // Mini text storage for 3x5 cells
  4099. static bool init = true;
  4100. if (!show_widget_frame_bg)
  4101. ImGui::PushStyleColor(ImGuiCol_FrameBg, 0);
  4102. for (int cell = 0; cell < 3 * 5; cell++)
  4103. {
  4104. ImGui::TableNextColumn();
  4105. if (init)
  4106. strcpy(text_bufs[cell], "edit me");
  4107. ImGui::SetNextItemWidth(-FLT_MIN);
  4108. ImGui::PushID(cell);
  4109. ImGui::InputText("##cell", text_bufs[cell], IM_ARRAYSIZE(text_bufs[cell]));
  4110. ImGui::PopID();
  4111. }
  4112. if (!show_widget_frame_bg)
  4113. ImGui::PopStyleColor();
  4114. init = false;
  4115. ImGui::EndTable();
  4116. }
  4117. ImGui::PopStyleVar();
  4118. ImGui::TreePop();
  4119. }
  4120. if (open_action != -1)
  4121. ImGui::SetNextItemOpen(open_action != 0);
  4122. IMGUI_DEMO_MARKER("Tables/Explicit widths");
  4123. if (ImGui::TreeNode("Sizing policies"))
  4124. {
  4125. static ImGuiTableFlags flags1 = ImGuiTableFlags_BordersV | ImGuiTableFlags_BordersOuterH | ImGuiTableFlags_RowBg | ImGuiTableFlags_ContextMenuInBody;
  4126. PushStyleCompact();
  4127. ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags1, ImGuiTableFlags_Resizable);
  4128. ImGui::CheckboxFlags("ImGuiTableFlags_NoHostExtendX", &flags1, ImGuiTableFlags_NoHostExtendX);
  4129. PopStyleCompact();
  4130. static ImGuiTableFlags sizing_policy_flags[4] = { ImGuiTableFlags_SizingFixedFit, ImGuiTableFlags_SizingFixedSame, ImGuiTableFlags_SizingStretchProp, ImGuiTableFlags_SizingStretchSame };
  4131. for (int table_n = 0; table_n < 4; table_n++)
  4132. {
  4133. ImGui::PushID(table_n);
  4134. ImGui::SetNextItemWidth(TEXT_BASE_WIDTH * 30);
  4135. EditTableSizingFlags(&sizing_policy_flags[table_n]);
  4136. // To make it easier to understand the different sizing policy,
  4137. // For each policy: we display one table where the columns have equal contents width,
  4138. // and one where the columns have different contents width.
  4139. if (ImGui::BeginTable("table1", 3, sizing_policy_flags[table_n] | flags1))
  4140. {
  4141. for (int row = 0; row < 3; row++)
  4142. {
  4143. ImGui::TableNextRow();
  4144. ImGui::TableNextColumn(); ImGui::Text("Oh dear");
  4145. ImGui::TableNextColumn(); ImGui::Text("Oh dear");
  4146. ImGui::TableNextColumn(); ImGui::Text("Oh dear");
  4147. }
  4148. ImGui::EndTable();
  4149. }
  4150. if (ImGui::BeginTable("table2", 3, sizing_policy_flags[table_n] | flags1))
  4151. {
  4152. for (int row = 0; row < 3; row++)
  4153. {
  4154. ImGui::TableNextRow();
  4155. ImGui::TableNextColumn(); ImGui::Text("AAAA");
  4156. ImGui::TableNextColumn(); ImGui::Text("BBBBBBBB");
  4157. ImGui::TableNextColumn(); ImGui::Text("CCCCCCCCCCCC");
  4158. }
  4159. ImGui::EndTable();
  4160. }
  4161. ImGui::PopID();
  4162. }
  4163. ImGui::Spacing();
  4164. ImGui::TextUnformatted("Advanced");
  4165. ImGui::SameLine();
  4166. HelpMarker(
  4167. "This section allows you to interact and see the effect of various sizing policies "
  4168. "depending on whether Scroll is enabled and the contents of your columns.");
  4169. enum ContentsType { CT_ShowWidth, CT_ShortText, CT_LongText, CT_Button, CT_FillButton, CT_InputText };
  4170. static ImGuiTableFlags flags = ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_Resizable;
  4171. static int contents_type = CT_ShowWidth;
  4172. static int column_count = 3;
  4173. PushStyleCompact();
  4174. ImGui::PushID("Advanced");
  4175. ImGui::PushItemWidth(TEXT_BASE_WIDTH * 30);
  4176. EditTableSizingFlags(&flags);
  4177. ImGui::Combo("Contents", &contents_type, "Show width\0Short Text\0Long Text\0Button\0Fill Button\0InputText\0");
  4178. if (contents_type == CT_FillButton)
  4179. {
  4180. ImGui::SameLine();
  4181. HelpMarker(
  4182. "Be mindful that using right-alignment (e.g. size.x = -FLT_MIN) creates a feedback loop "
  4183. "where contents width can feed into auto-column width can feed into contents width.");
  4184. }
  4185. ImGui::DragInt("Columns", &column_count, 0.1f, 1, 64, "%d", ImGuiSliderFlags_AlwaysClamp);
  4186. ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable);
  4187. ImGui::CheckboxFlags("ImGuiTableFlags_PreciseWidths", &flags, ImGuiTableFlags_PreciseWidths);
  4188. ImGui::SameLine(); HelpMarker("Disable distributing remainder width to stretched columns (width allocation on a 100-wide table with 3 columns: Without this flag: 33,33,34. With this flag: 33,33,33). With larger number of columns, resizing will appear to be less smooth.");
  4189. ImGui::CheckboxFlags("ImGuiTableFlags_ScrollX", &flags, ImGuiTableFlags_ScrollX);
  4190. ImGui::CheckboxFlags("ImGuiTableFlags_ScrollY", &flags, ImGuiTableFlags_ScrollY);
  4191. ImGui::CheckboxFlags("ImGuiTableFlags_NoClip", &flags, ImGuiTableFlags_NoClip);
  4192. ImGui::PopItemWidth();
  4193. ImGui::PopID();
  4194. PopStyleCompact();
  4195. if (ImGui::BeginTable("table2", column_count, flags, ImVec2(0.0f, TEXT_BASE_HEIGHT * 7)))
  4196. {
  4197. for (int cell = 0; cell < 10 * column_count; cell++)
  4198. {
  4199. ImGui::TableNextColumn();
  4200. int column = ImGui::TableGetColumnIndex();
  4201. int row = ImGui::TableGetRowIndex();
  4202. ImGui::PushID(cell);
  4203. char label[32];
  4204. static char text_buf[32] = "";
  4205. sprintf(label, "Hello %d,%d", column, row);
  4206. switch (contents_type)
  4207. {
  4208. case CT_ShortText: ImGui::TextUnformatted(label); break;
  4209. case CT_LongText: ImGui::Text("Some %s text %d,%d\nOver two lines..", column == 0 ? "long" : "longeeer", column, row); break;
  4210. case CT_ShowWidth: ImGui::Text("W: %.1f", ImGui::GetContentRegionAvail().x); break;
  4211. case CT_Button: ImGui::Button(label); break;
  4212. case CT_FillButton: ImGui::Button(label, ImVec2(-FLT_MIN, 0.0f)); break;
  4213. case CT_InputText: ImGui::SetNextItemWidth(-FLT_MIN); ImGui::InputText("##", text_buf, IM_ARRAYSIZE(text_buf)); break;
  4214. }
  4215. ImGui::PopID();
  4216. }
  4217. ImGui::EndTable();
  4218. }
  4219. ImGui::TreePop();
  4220. }
  4221. if (open_action != -1)
  4222. ImGui::SetNextItemOpen(open_action != 0);
  4223. IMGUI_DEMO_MARKER("Tables/Vertical scrolling, with clipping");
  4224. if (ImGui::TreeNode("Vertical scrolling, with clipping"))
  4225. {
  4226. HelpMarker(
  4227. "Here we activate ScrollY, which will create a child window container to allow hosting scrollable contents.\n\n"
  4228. "We also demonstrate using ImGuiListClipper to virtualize the submission of many items.");
  4229. static ImGuiTableFlags flags = ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable;
  4230. PushStyleCompact();
  4231. ImGui::CheckboxFlags("ImGuiTableFlags_ScrollY", &flags, ImGuiTableFlags_ScrollY);
  4232. PopStyleCompact();
  4233. // When using ScrollX or ScrollY we need to specify a size for our table container!
  4234. // Otherwise by default the table will fit all available space, like a BeginChild() call.
  4235. ImVec2 outer_size = ImVec2(0.0f, TEXT_BASE_HEIGHT * 8);
  4236. if (ImGui::BeginTable("table_scrolly", 3, flags, outer_size))
  4237. {
  4238. ImGui::TableSetupScrollFreeze(0, 1); // Make top row always visible
  4239. ImGui::TableSetupColumn("One", ImGuiTableColumnFlags_None);
  4240. ImGui::TableSetupColumn("Two", ImGuiTableColumnFlags_None);
  4241. ImGui::TableSetupColumn("Three", ImGuiTableColumnFlags_None);
  4242. ImGui::TableHeadersRow();
  4243. // Demonstrate using clipper for large vertical lists
  4244. ImGuiListClipper clipper;
  4245. clipper.Begin(1000);
  4246. while (clipper.Step())
  4247. {
  4248. for (int row = clipper.DisplayStart; row < clipper.DisplayEnd; row++)
  4249. {
  4250. ImGui::TableNextRow();
  4251. for (int column = 0; column < 3; column++)
  4252. {
  4253. ImGui::TableSetColumnIndex(column);
  4254. ImGui::Text("Hello %d,%d", column, row);
  4255. }
  4256. }
  4257. }
  4258. ImGui::EndTable();
  4259. }
  4260. ImGui::TreePop();
  4261. }
  4262. if (open_action != -1)
  4263. ImGui::SetNextItemOpen(open_action != 0);
  4264. IMGUI_DEMO_MARKER("Tables/Horizontal scrolling");
  4265. if (ImGui::TreeNode("Horizontal scrolling"))
  4266. {
  4267. HelpMarker(
  4268. "When ScrollX is enabled, the default sizing policy becomes ImGuiTableFlags_SizingFixedFit, "
  4269. "as automatically stretching columns doesn't make much sense with horizontal scrolling.\n\n"
  4270. "Also note that as of the current version, you will almost always want to enable ScrollY along with ScrollX, "
  4271. "because the container window won't automatically extend vertically to fix contents "
  4272. "(this may be improved in future versions).");
  4273. static ImGuiTableFlags flags = ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable;
  4274. static int freeze_cols = 1;
  4275. static int freeze_rows = 1;
  4276. PushStyleCompact();
  4277. ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable);
  4278. ImGui::CheckboxFlags("ImGuiTableFlags_ScrollX", &flags, ImGuiTableFlags_ScrollX);
  4279. ImGui::CheckboxFlags("ImGuiTableFlags_ScrollY", &flags, ImGuiTableFlags_ScrollY);
  4280. ImGui::SetNextItemWidth(ImGui::GetFrameHeight());
  4281. ImGui::DragInt("freeze_cols", &freeze_cols, 0.2f, 0, 9, NULL, ImGuiSliderFlags_NoInput);
  4282. ImGui::SetNextItemWidth(ImGui::GetFrameHeight());
  4283. ImGui::DragInt("freeze_rows", &freeze_rows, 0.2f, 0, 9, NULL, ImGuiSliderFlags_NoInput);
  4284. PopStyleCompact();
  4285. // When using ScrollX or ScrollY we need to specify a size for our table container!
  4286. // Otherwise by default the table will fit all available space, like a BeginChild() call.
  4287. ImVec2 outer_size = ImVec2(0.0f, TEXT_BASE_HEIGHT * 8);
  4288. if (ImGui::BeginTable("table_scrollx", 7, flags, outer_size))
  4289. {
  4290. ImGui::TableSetupScrollFreeze(freeze_cols, freeze_rows);
  4291. ImGui::TableSetupColumn("Line #", ImGuiTableColumnFlags_NoHide); // Make the first column not hideable to match our use of TableSetupScrollFreeze()
  4292. ImGui::TableSetupColumn("One");
  4293. ImGui::TableSetupColumn("Two");
  4294. ImGui::TableSetupColumn("Three");
  4295. ImGui::TableSetupColumn("Four");
  4296. ImGui::TableSetupColumn("Five");
  4297. ImGui::TableSetupColumn("Six");
  4298. ImGui::TableHeadersRow();
  4299. for (int row = 0; row < 20; row++)
  4300. {
  4301. ImGui::TableNextRow();
  4302. for (int column = 0; column < 7; column++)
  4303. {
  4304. // Both TableNextColumn() and TableSetColumnIndex() return true when a column is visible or performing width measurement.
  4305. // Because here we know that:
  4306. // - A) all our columns are contributing the same to row height
  4307. // - B) column 0 is always visible,
  4308. // We only always submit this one column and can skip others.
  4309. // More advanced per-column clipping behaviors may benefit from polling the status flags via TableGetColumnFlags().
  4310. if (!ImGui::TableSetColumnIndex(column) && column > 0)
  4311. continue;
  4312. if (column == 0)
  4313. ImGui::Text("Line %d", row);
  4314. else
  4315. ImGui::Text("Hello world %d,%d", column, row);
  4316. }
  4317. }
  4318. ImGui::EndTable();
  4319. }
  4320. ImGui::Spacing();
  4321. ImGui::TextUnformatted("Stretch + ScrollX");
  4322. ImGui::SameLine();
  4323. HelpMarker(
  4324. "Showcase using Stretch columns + ScrollX together: "
  4325. "this is rather unusual and only makes sense when specifying an 'inner_width' for the table!\n"
  4326. "Without an explicit value, inner_width is == outer_size.x and therefore using Stretch columns "
  4327. "along with ScrollX doesn't make sense.");
  4328. static ImGuiTableFlags flags2 = ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_RowBg | ImGuiTableFlags_ContextMenuInBody;
  4329. static float inner_width = 1000.0f;
  4330. PushStyleCompact();
  4331. ImGui::PushID("flags3");
  4332. ImGui::PushItemWidth(TEXT_BASE_WIDTH * 30);
  4333. ImGui::CheckboxFlags("ImGuiTableFlags_ScrollX", &flags2, ImGuiTableFlags_ScrollX);
  4334. ImGui::DragFloat("inner_width", &inner_width, 1.0f, 0.0f, FLT_MAX, "%.1f");
  4335. ImGui::PopItemWidth();
  4336. ImGui::PopID();
  4337. PopStyleCompact();
  4338. if (ImGui::BeginTable("table2", 7, flags2, outer_size, inner_width))
  4339. {
  4340. for (int cell = 0; cell < 20 * 7; cell++)
  4341. {
  4342. ImGui::TableNextColumn();
  4343. ImGui::Text("Hello world %d,%d", ImGui::TableGetColumnIndex(), ImGui::TableGetRowIndex());
  4344. }
  4345. ImGui::EndTable();
  4346. }
  4347. ImGui::TreePop();
  4348. }
  4349. if (open_action != -1)
  4350. ImGui::SetNextItemOpen(open_action != 0);
  4351. IMGUI_DEMO_MARKER("Tables/Columns flags");
  4352. if (ImGui::TreeNode("Columns flags"))
  4353. {
  4354. // Create a first table just to show all the options/flags we want to make visible in our example!
  4355. const int column_count = 3;
  4356. const char* column_names[column_count] = { "One", "Two", "Three" };
  4357. static ImGuiTableColumnFlags column_flags[column_count] = { ImGuiTableColumnFlags_DefaultSort, ImGuiTableColumnFlags_None, ImGuiTableColumnFlags_DefaultHide };
  4358. static ImGuiTableColumnFlags column_flags_out[column_count] = { 0, 0, 0 }; // Output from TableGetColumnFlags()
  4359. if (ImGui::BeginTable("table_columns_flags_checkboxes", column_count, ImGuiTableFlags_None))
  4360. {
  4361. PushStyleCompact();
  4362. for (int column = 0; column < column_count; column++)
  4363. {
  4364. ImGui::TableNextColumn();
  4365. ImGui::PushID(column);
  4366. ImGui::AlignTextToFramePadding(); // FIXME-TABLE: Workaround for wrong text baseline propagation across columns
  4367. ImGui::Text("'%s'", column_names[column]);
  4368. ImGui::Spacing();
  4369. ImGui::Text("Input flags:");
  4370. EditTableColumnsFlags(&column_flags[column]);
  4371. ImGui::Spacing();
  4372. ImGui::Text("Output flags:");
  4373. ImGui::BeginDisabled();
  4374. ShowTableColumnsStatusFlags(column_flags_out[column]);
  4375. ImGui::EndDisabled();
  4376. ImGui::PopID();
  4377. }
  4378. PopStyleCompact();
  4379. ImGui::EndTable();
  4380. }
  4381. // Create the real table we care about for the example!
  4382. // We use a scrolling table to be able to showcase the difference between the _IsEnabled and _IsVisible flags above,
  4383. // otherwise in a non-scrolling table columns are always visible (unless using ImGuiTableFlags_NoKeepColumnsVisible
  4384. // + resizing the parent window down).
  4385. const ImGuiTableFlags flags
  4386. = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY
  4387. | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV
  4388. | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Sortable;
  4389. ImVec2 outer_size = ImVec2(0.0f, TEXT_BASE_HEIGHT * 9);
  4390. if (ImGui::BeginTable("table_columns_flags", column_count, flags, outer_size))
  4391. {
  4392. bool has_angled_header = false;
  4393. for (int column = 0; column < column_count; column++)
  4394. {
  4395. has_angled_header |= (column_flags[column] & ImGuiTableColumnFlags_AngledHeader) != 0;
  4396. ImGui::TableSetupColumn(column_names[column], column_flags[column]);
  4397. }
  4398. if (has_angled_header)
  4399. ImGui::TableAngledHeadersRow();
  4400. ImGui::TableHeadersRow();
  4401. for (int column = 0; column < column_count; column++)
  4402. column_flags_out[column] = ImGui::TableGetColumnFlags(column);
  4403. float indent_step = (float)((int)TEXT_BASE_WIDTH / 2);
  4404. for (int row = 0; row < 8; row++)
  4405. {
  4406. // Add some indentation to demonstrate usage of per-column IndentEnable/IndentDisable flags.
  4407. ImGui::Indent(indent_step);
  4408. ImGui::TableNextRow();
  4409. for (int column = 0; column < column_count; column++)
  4410. {
  4411. ImGui::TableSetColumnIndex(column);
  4412. ImGui::Text("%s %s", (column == 0) ? "Indented" : "Hello", ImGui::TableGetColumnName(column));
  4413. }
  4414. }
  4415. ImGui::Unindent(indent_step * 8.0f);
  4416. ImGui::EndTable();
  4417. }
  4418. ImGui::TreePop();
  4419. }
  4420. if (open_action != -1)
  4421. ImGui::SetNextItemOpen(open_action != 0);
  4422. IMGUI_DEMO_MARKER("Tables/Columns widths");
  4423. if (ImGui::TreeNode("Columns widths"))
  4424. {
  4425. HelpMarker("Using TableSetupColumn() to setup default width.");
  4426. static ImGuiTableFlags flags1 = ImGuiTableFlags_Borders | ImGuiTableFlags_NoBordersInBodyUntilResize;
  4427. PushStyleCompact();
  4428. ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags1, ImGuiTableFlags_Resizable);
  4429. ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBodyUntilResize", &flags1, ImGuiTableFlags_NoBordersInBodyUntilResize);
  4430. PopStyleCompact();
  4431. if (ImGui::BeginTable("table1", 3, flags1))
  4432. {
  4433. // We could also set ImGuiTableFlags_SizingFixedFit on the table and all columns will default to ImGuiTableColumnFlags_WidthFixed.
  4434. ImGui::TableSetupColumn("one", ImGuiTableColumnFlags_WidthFixed, 100.0f); // Default to 100.0f
  4435. ImGui::TableSetupColumn("two", ImGuiTableColumnFlags_WidthFixed, 200.0f); // Default to 200.0f
  4436. ImGui::TableSetupColumn("three", ImGuiTableColumnFlags_WidthFixed); // Default to auto
  4437. ImGui::TableHeadersRow();
  4438. for (int row = 0; row < 4; row++)
  4439. {
  4440. ImGui::TableNextRow();
  4441. for (int column = 0; column < 3; column++)
  4442. {
  4443. ImGui::TableSetColumnIndex(column);
  4444. if (row == 0)
  4445. ImGui::Text("(w: %5.1f)", ImGui::GetContentRegionAvail().x);
  4446. else
  4447. ImGui::Text("Hello %d,%d", column, row);
  4448. }
  4449. }
  4450. ImGui::EndTable();
  4451. }
  4452. HelpMarker(
  4453. "Using TableSetupColumn() to setup explicit width.\n\nUnless _NoKeepColumnsVisible is set, "
  4454. "fixed columns with set width may still be shrunk down if there's not enough space in the host.");
  4455. static ImGuiTableFlags flags2 = ImGuiTableFlags_None;
  4456. PushStyleCompact();
  4457. ImGui::CheckboxFlags("ImGuiTableFlags_NoKeepColumnsVisible", &flags2, ImGuiTableFlags_NoKeepColumnsVisible);
  4458. ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerV", &flags2, ImGuiTableFlags_BordersInnerV);
  4459. ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterV", &flags2, ImGuiTableFlags_BordersOuterV);
  4460. PopStyleCompact();
  4461. if (ImGui::BeginTable("table2", 4, flags2))
  4462. {
  4463. // We could also set ImGuiTableFlags_SizingFixedFit on the table and then all columns
  4464. // will default to ImGuiTableColumnFlags_WidthFixed.
  4465. ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 100.0f);
  4466. ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, TEXT_BASE_WIDTH * 15.0f);
  4467. ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, TEXT_BASE_WIDTH * 30.0f);
  4468. ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, TEXT_BASE_WIDTH * 15.0f);
  4469. for (int row = 0; row < 5; row++)
  4470. {
  4471. ImGui::TableNextRow();
  4472. for (int column = 0; column < 4; column++)
  4473. {
  4474. ImGui::TableSetColumnIndex(column);
  4475. if (row == 0)
  4476. ImGui::Text("(w: %5.1f)", ImGui::GetContentRegionAvail().x);
  4477. else
  4478. ImGui::Text("Hello %d,%d", column, row);
  4479. }
  4480. }
  4481. ImGui::EndTable();
  4482. }
  4483. ImGui::TreePop();
  4484. }
  4485. if (open_action != -1)
  4486. ImGui::SetNextItemOpen(open_action != 0);
  4487. IMGUI_DEMO_MARKER("Tables/Nested tables");
  4488. if (ImGui::TreeNode("Nested tables"))
  4489. {
  4490. HelpMarker("This demonstrates embedding a table into another table cell.");
  4491. if (ImGui::BeginTable("table_nested1", 2, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable))
  4492. {
  4493. ImGui::TableSetupColumn("A0");
  4494. ImGui::TableSetupColumn("A1");
  4495. ImGui::TableHeadersRow();
  4496. ImGui::TableNextColumn();
  4497. ImGui::Text("A0 Row 0");
  4498. {
  4499. float rows_height = TEXT_BASE_HEIGHT * 2;
  4500. if (ImGui::BeginTable("table_nested2", 2, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable))
  4501. {
  4502. ImGui::TableSetupColumn("B0");
  4503. ImGui::TableSetupColumn("B1");
  4504. ImGui::TableHeadersRow();
  4505. ImGui::TableNextRow(ImGuiTableRowFlags_None, rows_height);
  4506. ImGui::TableNextColumn();
  4507. ImGui::Text("B0 Row 0");
  4508. ImGui::TableNextColumn();
  4509. ImGui::Text("B1 Row 0");
  4510. ImGui::TableNextRow(ImGuiTableRowFlags_None, rows_height);
  4511. ImGui::TableNextColumn();
  4512. ImGui::Text("B0 Row 1");
  4513. ImGui::TableNextColumn();
  4514. ImGui::Text("B1 Row 1");
  4515. ImGui::EndTable();
  4516. }
  4517. }
  4518. ImGui::TableNextColumn(); ImGui::Text("A1 Row 0");
  4519. ImGui::TableNextColumn(); ImGui::Text("A0 Row 1");
  4520. ImGui::TableNextColumn(); ImGui::Text("A1 Row 1");
  4521. ImGui::EndTable();
  4522. }
  4523. ImGui::TreePop();
  4524. }
  4525. if (open_action != -1)
  4526. ImGui::SetNextItemOpen(open_action != 0);
  4527. IMGUI_DEMO_MARKER("Tables/Row height");
  4528. if (ImGui::TreeNode("Row height"))
  4529. {
  4530. HelpMarker(
  4531. "You can pass a 'min_row_height' to TableNextRow().\n\nRows are padded with 'style.CellPadding.y' on top and bottom, "
  4532. "so effectively the minimum row height will always be >= 'style.CellPadding.y * 2.0f'.\n\n"
  4533. "We cannot honor a _maximum_ row height as that would require a unique clipping rectangle per row.");
  4534. if (ImGui::BeginTable("table_row_height", 1, ImGuiTableFlags_Borders))
  4535. {
  4536. for (int row = 0; row < 8; row++)
  4537. {
  4538. float min_row_height = (float)(int)(TEXT_BASE_HEIGHT * 0.30f * row);
  4539. ImGui::TableNextRow(ImGuiTableRowFlags_None, min_row_height);
  4540. ImGui::TableNextColumn();
  4541. ImGui::Text("min_row_height = %.2f", min_row_height);
  4542. }
  4543. ImGui::EndTable();
  4544. }
  4545. HelpMarker(
  4546. "Showcase using SameLine(0,0) to share Current Line Height between cells.\n\n"
  4547. "Please note that Tables Row Height is not the same thing as Current Line Height, "
  4548. "as a table cell may contains multiple lines.");
  4549. if (ImGui::BeginTable("table_share_lineheight", 2, ImGuiTableFlags_Borders))
  4550. {
  4551. ImGui::TableNextRow();
  4552. ImGui::TableNextColumn();
  4553. ImGui::ColorButton("##1", ImVec4(0.13f, 0.26f, 0.40f, 1.0f), ImGuiColorEditFlags_None, ImVec2(40, 40));
  4554. ImGui::TableNextColumn();
  4555. ImGui::Text("Line 1");
  4556. ImGui::Text("Line 2");
  4557. ImGui::TableNextRow();
  4558. ImGui::TableNextColumn();
  4559. ImGui::ColorButton("##2", ImVec4(0.13f, 0.26f, 0.40f, 1.0f), ImGuiColorEditFlags_None, ImVec2(40, 40));
  4560. ImGui::TableNextColumn();
  4561. ImGui::SameLine(0.0f, 0.0f); // Reuse line height from previous column
  4562. ImGui::Text("Line 1, with SameLine(0,0)");
  4563. ImGui::Text("Line 2");
  4564. ImGui::EndTable();
  4565. }
  4566. HelpMarker("Showcase altering CellPadding.y between rows. Note that CellPadding.x is locked for the entire table.");
  4567. if (ImGui::BeginTable("table_changing_cellpadding_y", 1, ImGuiTableFlags_Borders))
  4568. {
  4569. ImGuiStyle& style = ImGui::GetStyle();
  4570. for (int row = 0; row < 8; row++)
  4571. {
  4572. if ((row % 3) == 2)
  4573. ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, ImVec2(style.CellPadding.x, 20.0f));
  4574. ImGui::TableNextRow(ImGuiTableRowFlags_None);
  4575. ImGui::TableNextColumn();
  4576. ImGui::Text("CellPadding.y = %.2f", style.CellPadding.y);
  4577. if ((row % 3) == 2)
  4578. ImGui::PopStyleVar();
  4579. }
  4580. ImGui::EndTable();
  4581. }
  4582. ImGui::TreePop();
  4583. }
  4584. if (open_action != -1)
  4585. ImGui::SetNextItemOpen(open_action != 0);
  4586. IMGUI_DEMO_MARKER("Tables/Outer size");
  4587. if (ImGui::TreeNode("Outer size"))
  4588. {
  4589. // Showcasing use of ImGuiTableFlags_NoHostExtendX and ImGuiTableFlags_NoHostExtendY
  4590. // Important to that note how the two flags have slightly different behaviors!
  4591. ImGui::Text("Using NoHostExtendX and NoHostExtendY:");
  4592. PushStyleCompact();
  4593. static ImGuiTableFlags flags = ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_ContextMenuInBody | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX;
  4594. ImGui::CheckboxFlags("ImGuiTableFlags_NoHostExtendX", &flags, ImGuiTableFlags_NoHostExtendX);
  4595. ImGui::SameLine(); HelpMarker("Make outer width auto-fit to columns, overriding outer_size.x value.\n\nOnly available when ScrollX/ScrollY are disabled and Stretch columns are not used.");
  4596. ImGui::CheckboxFlags("ImGuiTableFlags_NoHostExtendY", &flags, ImGuiTableFlags_NoHostExtendY);
  4597. ImGui::SameLine(); HelpMarker("Make outer height stop exactly at outer_size.y (prevent auto-extending table past the limit).\n\nOnly available when ScrollX/ScrollY are disabled. Data below the limit will be clipped and not visible.");
  4598. PopStyleCompact();
  4599. ImVec2 outer_size = ImVec2(0.0f, TEXT_BASE_HEIGHT * 5.5f);
  4600. if (ImGui::BeginTable("table1", 3, flags, outer_size))
  4601. {
  4602. for (int row = 0; row < 10; row++)
  4603. {
  4604. ImGui::TableNextRow();
  4605. for (int column = 0; column < 3; column++)
  4606. {
  4607. ImGui::TableNextColumn();
  4608. ImGui::Text("Cell %d,%d", column, row);
  4609. }
  4610. }
  4611. ImGui::EndTable();
  4612. }
  4613. ImGui::SameLine();
  4614. ImGui::Text("Hello!");
  4615. ImGui::Spacing();
  4616. ImGui::Text("Using explicit size:");
  4617. if (ImGui::BeginTable("table2", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg, ImVec2(TEXT_BASE_WIDTH * 30, 0.0f)))
  4618. {
  4619. for (int row = 0; row < 5; row++)
  4620. {
  4621. ImGui::TableNextRow();
  4622. for (int column = 0; column < 3; column++)
  4623. {
  4624. ImGui::TableNextColumn();
  4625. ImGui::Text("Cell %d,%d", column, row);
  4626. }
  4627. }
  4628. ImGui::EndTable();
  4629. }
  4630. ImGui::SameLine();
  4631. if (ImGui::BeginTable("table3", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg, ImVec2(TEXT_BASE_WIDTH * 30, 0.0f)))
  4632. {
  4633. for (int row = 0; row < 3; row++)
  4634. {
  4635. ImGui::TableNextRow(0, TEXT_BASE_HEIGHT * 1.5f);
  4636. for (int column = 0; column < 3; column++)
  4637. {
  4638. ImGui::TableNextColumn();
  4639. ImGui::Text("Cell %d,%d", column, row);
  4640. }
  4641. }
  4642. ImGui::EndTable();
  4643. }
  4644. ImGui::TreePop();
  4645. }
  4646. if (open_action != -1)
  4647. ImGui::SetNextItemOpen(open_action != 0);
  4648. IMGUI_DEMO_MARKER("Tables/Background color");
  4649. if (ImGui::TreeNode("Background color"))
  4650. {
  4651. static ImGuiTableFlags flags = ImGuiTableFlags_RowBg;
  4652. static int row_bg_type = 1;
  4653. static int row_bg_target = 1;
  4654. static int cell_bg_type = 1;
  4655. PushStyleCompact();
  4656. ImGui::CheckboxFlags("ImGuiTableFlags_Borders", &flags, ImGuiTableFlags_Borders);
  4657. ImGui::CheckboxFlags("ImGuiTableFlags_RowBg", &flags, ImGuiTableFlags_RowBg);
  4658. ImGui::SameLine(); HelpMarker("ImGuiTableFlags_RowBg automatically sets RowBg0 to alternative colors pulled from the Style.");
  4659. ImGui::Combo("row bg type", (int*)&row_bg_type, "None\0Red\0Gradient\0");
  4660. ImGui::Combo("row bg target", (int*)&row_bg_target, "RowBg0\0RowBg1\0"); ImGui::SameLine(); HelpMarker("Target RowBg0 to override the alternating odd/even colors,\nTarget RowBg1 to blend with them.");
  4661. ImGui::Combo("cell bg type", (int*)&cell_bg_type, "None\0Blue\0"); ImGui::SameLine(); HelpMarker("We are colorizing cells to B1->C2 here.");
  4662. IM_ASSERT(row_bg_type >= 0 && row_bg_type <= 2);
  4663. IM_ASSERT(row_bg_target >= 0 && row_bg_target <= 1);
  4664. IM_ASSERT(cell_bg_type >= 0 && cell_bg_type <= 1);
  4665. PopStyleCompact();
  4666. if (ImGui::BeginTable("table1", 5, flags))
  4667. {
  4668. for (int row = 0; row < 6; row++)
  4669. {
  4670. ImGui::TableNextRow();
  4671. // Demonstrate setting a row background color with 'ImGui::TableSetBgColor(ImGuiTableBgTarget_RowBgX, ...)'
  4672. // We use a transparent color so we can see the one behind in case our target is RowBg1 and RowBg0 was already targeted by the ImGuiTableFlags_RowBg flag.
  4673. if (row_bg_type != 0)
  4674. {
  4675. ImU32 row_bg_color = ImGui::GetColorU32(row_bg_type == 1 ? ImVec4(0.7f, 0.3f, 0.3f, 0.65f) : ImVec4(0.2f + row * 0.1f, 0.2f, 0.2f, 0.65f)); // Flat or Gradient?
  4676. ImGui::TableSetBgColor(ImGuiTableBgTarget_RowBg0 + row_bg_target, row_bg_color);
  4677. }
  4678. // Fill cells
  4679. for (int column = 0; column < 5; column++)
  4680. {
  4681. ImGui::TableSetColumnIndex(column);
  4682. ImGui::Text("%c%c", 'A' + row, '0' + column);
  4683. // Change background of Cells B1->C2
  4684. // Demonstrate setting a cell background color with 'ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, ...)'
  4685. // (the CellBg color will be blended over the RowBg and ColumnBg colors)
  4686. // We can also pass a column number as a third parameter to TableSetBgColor() and do this outside the column loop.
  4687. if (row >= 1 && row <= 2 && column >= 1 && column <= 2 && cell_bg_type == 1)
  4688. {
  4689. ImU32 cell_bg_color = ImGui::GetColorU32(ImVec4(0.3f, 0.3f, 0.7f, 0.65f));
  4690. ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color);
  4691. }
  4692. }
  4693. }
  4694. ImGui::EndTable();
  4695. }
  4696. ImGui::TreePop();
  4697. }
  4698. if (open_action != -1)
  4699. ImGui::SetNextItemOpen(open_action != 0);
  4700. IMGUI_DEMO_MARKER("Tables/Tree view");
  4701. if (ImGui::TreeNode("Tree view"))
  4702. {
  4703. static ImGuiTableFlags flags = ImGuiTableFlags_BordersV | ImGuiTableFlags_BordersOuterH | ImGuiTableFlags_Resizable | ImGuiTableFlags_RowBg | ImGuiTableFlags_NoBordersInBody;
  4704. static ImGuiTreeNodeFlags tree_node_flags = ImGuiTreeNodeFlags_SpanAllColumns;
  4705. ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanFullWidth", &tree_node_flags, ImGuiTreeNodeFlags_SpanFullWidth);
  4706. ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanTextWidth", &tree_node_flags, ImGuiTreeNodeFlags_SpanTextWidth);
  4707. ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAllColumns", &tree_node_flags, ImGuiTreeNodeFlags_SpanAllColumns);
  4708. HelpMarker("See \"Columns flags\" section to configure how indentation is applied to individual columns.");
  4709. if (ImGui::BeginTable("3ways", 3, flags))
  4710. {
  4711. // The first column will use the default _WidthStretch when ScrollX is Off and _WidthFixed when ScrollX is On
  4712. ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_NoHide);
  4713. ImGui::TableSetupColumn("Size", ImGuiTableColumnFlags_WidthFixed, TEXT_BASE_WIDTH * 12.0f);
  4714. ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthFixed, TEXT_BASE_WIDTH * 18.0f);
  4715. ImGui::TableHeadersRow();
  4716. // Simple storage to output a dummy file-system.
  4717. struct MyTreeNode
  4718. {
  4719. const char* Name;
  4720. const char* Type;
  4721. int Size;
  4722. int ChildIdx;
  4723. int ChildCount;
  4724. static void DisplayNode(const MyTreeNode* node, const MyTreeNode* all_nodes)
  4725. {
  4726. ImGui::TableNextRow();
  4727. ImGui::TableNextColumn();
  4728. const bool is_folder = (node->ChildCount > 0);
  4729. if (is_folder)
  4730. {
  4731. bool open = ImGui::TreeNodeEx(node->Name, tree_node_flags);
  4732. ImGui::TableNextColumn();
  4733. ImGui::TextDisabled("--");
  4734. ImGui::TableNextColumn();
  4735. ImGui::TextUnformatted(node->Type);
  4736. if (open)
  4737. {
  4738. for (int child_n = 0; child_n < node->ChildCount; child_n++)
  4739. DisplayNode(&all_nodes[node->ChildIdx + child_n], all_nodes);
  4740. ImGui::TreePop();
  4741. }
  4742. }
  4743. else
  4744. {
  4745. ImGui::TreeNodeEx(node->Name, tree_node_flags | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_Bullet | ImGuiTreeNodeFlags_NoTreePushOnOpen);
  4746. ImGui::TableNextColumn();
  4747. ImGui::Text("%d", node->Size);
  4748. ImGui::TableNextColumn();
  4749. ImGui::TextUnformatted(node->Type);
  4750. }
  4751. }
  4752. };
  4753. static const MyTreeNode nodes[] =
  4754. {
  4755. { "Root", "Folder", -1, 1, 3 }, // 0
  4756. { "Music", "Folder", -1, 4, 2 }, // 1
  4757. { "Textures", "Folder", -1, 6, 3 }, // 2
  4758. { "desktop.ini", "System file", 1024, -1,-1 }, // 3
  4759. { "File1_a.wav", "Audio file", 123000, -1,-1 }, // 4
  4760. { "File1_b.wav", "Audio file", 456000, -1,-1 }, // 5
  4761. { "Image001.png", "Image file", 203128, -1,-1 }, // 6
  4762. { "Copy of Image001.png", "Image file", 203256, -1,-1 }, // 7
  4763. { "Copy of Image001 (Final2).png","Image file", 203512, -1,-1 }, // 8
  4764. };
  4765. MyTreeNode::DisplayNode(&nodes[0], nodes);
  4766. ImGui::EndTable();
  4767. }
  4768. ImGui::TreePop();
  4769. }
  4770. if (open_action != -1)
  4771. ImGui::SetNextItemOpen(open_action != 0);
  4772. IMGUI_DEMO_MARKER("Tables/Item width");
  4773. if (ImGui::TreeNode("Item width"))
  4774. {
  4775. HelpMarker(
  4776. "Showcase using PushItemWidth() and how it is preserved on a per-column basis.\n\n"
  4777. "Note that on auto-resizing non-resizable fixed columns, querying the content width for "
  4778. "e.g. right-alignment doesn't make sense.");
  4779. if (ImGui::BeginTable("table_item_width", 3, ImGuiTableFlags_Borders))
  4780. {
  4781. ImGui::TableSetupColumn("small");
  4782. ImGui::TableSetupColumn("half");
  4783. ImGui::TableSetupColumn("right-align");
  4784. ImGui::TableHeadersRow();
  4785. for (int row = 0; row < 3; row++)
  4786. {
  4787. ImGui::TableNextRow();
  4788. if (row == 0)
  4789. {
  4790. // Setup ItemWidth once (instead of setting up every time, which is also possible but less efficient)
  4791. ImGui::TableSetColumnIndex(0);
  4792. ImGui::PushItemWidth(TEXT_BASE_WIDTH * 3.0f); // Small
  4793. ImGui::TableSetColumnIndex(1);
  4794. ImGui::PushItemWidth(-ImGui::GetContentRegionAvail().x * 0.5f);
  4795. ImGui::TableSetColumnIndex(2);
  4796. ImGui::PushItemWidth(-FLT_MIN); // Right-aligned
  4797. }
  4798. // Draw our contents
  4799. static float dummy_f = 0.0f;
  4800. ImGui::PushID(row);
  4801. ImGui::TableSetColumnIndex(0);
  4802. ImGui::SliderFloat("float0", &dummy_f, 0.0f, 1.0f);
  4803. ImGui::TableSetColumnIndex(1);
  4804. ImGui::SliderFloat("float1", &dummy_f, 0.0f, 1.0f);
  4805. ImGui::TableSetColumnIndex(2);
  4806. ImGui::SliderFloat("##float2", &dummy_f, 0.0f, 1.0f); // No visible label since right-aligned
  4807. ImGui::PopID();
  4808. }
  4809. ImGui::EndTable();
  4810. }
  4811. ImGui::TreePop();
  4812. }
  4813. // Demonstrate using TableHeader() calls instead of TableHeadersRow()
  4814. if (open_action != -1)
  4815. ImGui::SetNextItemOpen(open_action != 0);
  4816. IMGUI_DEMO_MARKER("Tables/Custom headers");
  4817. if (ImGui::TreeNode("Custom headers"))
  4818. {
  4819. const int COLUMNS_COUNT = 3;
  4820. if (ImGui::BeginTable("table_custom_headers", COLUMNS_COUNT, ImGuiTableFlags_Borders | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable))
  4821. {
  4822. ImGui::TableSetupColumn("Apricot");
  4823. ImGui::TableSetupColumn("Banana");
  4824. ImGui::TableSetupColumn("Cherry");
  4825. // Dummy entire-column selection storage
  4826. // FIXME: It would be nice to actually demonstrate full-featured selection using those checkbox.
  4827. static bool column_selected[3] = {};
  4828. // Instead of calling TableHeadersRow() we'll submit custom headers ourselves
  4829. ImGui::TableNextRow(ImGuiTableRowFlags_Headers);
  4830. for (int column = 0; column < COLUMNS_COUNT; column++)
  4831. {
  4832. ImGui::TableSetColumnIndex(column);
  4833. const char* column_name = ImGui::TableGetColumnName(column); // Retrieve name passed to TableSetupColumn()
  4834. ImGui::PushID(column);
  4835. ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
  4836. ImGui::Checkbox("##checkall", &column_selected[column]);
  4837. ImGui::PopStyleVar();
  4838. ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x);
  4839. ImGui::TableHeader(column_name);
  4840. ImGui::PopID();
  4841. }
  4842. for (int row = 0; row < 5; row++)
  4843. {
  4844. ImGui::TableNextRow();
  4845. for (int column = 0; column < 3; column++)
  4846. {
  4847. char buf[32];
  4848. sprintf(buf, "Cell %d,%d", column, row);
  4849. ImGui::TableSetColumnIndex(column);
  4850. ImGui::Selectable(buf, column_selected[column]);
  4851. }
  4852. }
  4853. ImGui::EndTable();
  4854. }
  4855. ImGui::TreePop();
  4856. }
  4857. // Demonstrate using ImGuiTableColumnFlags_AngledHeader flag to create angled headers
  4858. if (open_action != -1)
  4859. ImGui::SetNextItemOpen(open_action != 0);
  4860. IMGUI_DEMO_MARKER("Tables/Angled headers");
  4861. if (ImGui::TreeNode("Angled headers"))
  4862. {
  4863. const char* column_names[] = { "Track", "cabasa", "ride", "smash", "tom-hi", "tom-mid", "tom-low", "hihat-o", "hihat-c", "snare-s", "snare-c", "clap", "rim", "kick" };
  4864. const int columns_count = IM_ARRAYSIZE(column_names);
  4865. const int rows_count = 12;
  4866. static ImGuiTableFlags table_flags = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersInnerH | ImGuiTableFlags_Hideable | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_HighlightHoveredColumn;
  4867. static ImGuiTableColumnFlags column_flags = ImGuiTableColumnFlags_AngledHeader | ImGuiTableColumnFlags_WidthFixed;
  4868. static bool bools[columns_count * rows_count] = {}; // Dummy storage selection storage
  4869. static int frozen_cols = 1;
  4870. static int frozen_rows = 2;
  4871. ImGui::CheckboxFlags("_ScrollX", &table_flags, ImGuiTableFlags_ScrollX);
  4872. ImGui::CheckboxFlags("_ScrollY", &table_flags, ImGuiTableFlags_ScrollY);
  4873. ImGui::CheckboxFlags("_Resizable", &table_flags, ImGuiTableFlags_Resizable);
  4874. ImGui::CheckboxFlags("_NoBordersInBody", &table_flags, ImGuiTableFlags_NoBordersInBody);
  4875. ImGui::CheckboxFlags("_HighlightHoveredColumn", &table_flags, ImGuiTableFlags_HighlightHoveredColumn);
  4876. ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
  4877. ImGui::SliderInt("Frozen columns", &frozen_cols, 0, 2);
  4878. ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
  4879. ImGui::SliderInt("Frozen rows", &frozen_rows, 0, 2);
  4880. ImGui::CheckboxFlags("Disable header contributing to column width", &column_flags, ImGuiTableColumnFlags_NoHeaderWidth);
  4881. if (ImGui::TreeNode("Style settings"))
  4882. {
  4883. ImGui::SameLine();
  4884. HelpMarker("Giving access to some ImGuiStyle value in this demo for convenience.");
  4885. ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
  4886. ImGui::SliderAngle("style.TableAngledHeadersAngle", &ImGui::GetStyle().TableAngledHeadersAngle, -50.0f, +50.0f);
  4887. ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
  4888. ImGui::SliderFloat2("style.TableAngledHeadersTextAlign", (float*)&ImGui::GetStyle().TableAngledHeadersTextAlign, 0.0f, 1.0f, "%.2f");
  4889. ImGui::TreePop();
  4890. }
  4891. if (ImGui::BeginTable("table_angled_headers", columns_count, table_flags, ImVec2(0.0f, TEXT_BASE_HEIGHT * 12)))
  4892. {
  4893. ImGui::TableSetupColumn(column_names[0], ImGuiTableColumnFlags_NoHide | ImGuiTableColumnFlags_NoReorder);
  4894. for (int n = 1; n < columns_count; n++)
  4895. ImGui::TableSetupColumn(column_names[n], column_flags);
  4896. ImGui::TableSetupScrollFreeze(frozen_cols, frozen_rows);
  4897. ImGui::TableAngledHeadersRow(); // Draw angled headers for all columns with the ImGuiTableColumnFlags_AngledHeader flag.
  4898. ImGui::TableHeadersRow(); // Draw remaining headers and allow access to context-menu and other functions.
  4899. for (int row = 0; row < rows_count; row++)
  4900. {
  4901. ImGui::PushID(row);
  4902. ImGui::TableNextRow();
  4903. ImGui::TableSetColumnIndex(0);
  4904. ImGui::AlignTextToFramePadding();
  4905. ImGui::Text("Track %d", row);
  4906. for (int column = 1; column < columns_count; column++)
  4907. if (ImGui::TableSetColumnIndex(column))
  4908. {
  4909. ImGui::PushID(column);
  4910. ImGui::Checkbox("", &bools[row * columns_count + column]);
  4911. ImGui::PopID();
  4912. }
  4913. ImGui::PopID();
  4914. }
  4915. ImGui::EndTable();
  4916. }
  4917. ImGui::TreePop();
  4918. }
  4919. // Demonstrate creating custom context menus inside columns,
  4920. // while playing it nice with context menus provided by TableHeadersRow()/TableHeader()
  4921. if (open_action != -1)
  4922. ImGui::SetNextItemOpen(open_action != 0);
  4923. IMGUI_DEMO_MARKER("Tables/Context menus");
  4924. if (ImGui::TreeNode("Context menus"))
  4925. {
  4926. HelpMarker(
  4927. "By default, right-clicking over a TableHeadersRow()/TableHeader() line will open the default context-menu.\n"
  4928. "Using ImGuiTableFlags_ContextMenuInBody we also allow right-clicking over columns body.");
  4929. static ImGuiTableFlags flags1 = ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders | ImGuiTableFlags_ContextMenuInBody;
  4930. PushStyleCompact();
  4931. ImGui::CheckboxFlags("ImGuiTableFlags_ContextMenuInBody", &flags1, ImGuiTableFlags_ContextMenuInBody);
  4932. PopStyleCompact();
  4933. // Context Menus: first example
  4934. // [1.1] Right-click on the TableHeadersRow() line to open the default table context menu.
  4935. // [1.2] Right-click in columns also open the default table context menu (if ImGuiTableFlags_ContextMenuInBody is set)
  4936. const int COLUMNS_COUNT = 3;
  4937. if (ImGui::BeginTable("table_context_menu", COLUMNS_COUNT, flags1))
  4938. {
  4939. ImGui::TableSetupColumn("One");
  4940. ImGui::TableSetupColumn("Two");
  4941. ImGui::TableSetupColumn("Three");
  4942. // [1.1]] Right-click on the TableHeadersRow() line to open the default table context menu.
  4943. ImGui::TableHeadersRow();
  4944. // Submit dummy contents
  4945. for (int row = 0; row < 4; row++)
  4946. {
  4947. ImGui::TableNextRow();
  4948. for (int column = 0; column < COLUMNS_COUNT; column++)
  4949. {
  4950. ImGui::TableSetColumnIndex(column);
  4951. ImGui::Text("Cell %d,%d", column, row);
  4952. }
  4953. }
  4954. ImGui::EndTable();
  4955. }
  4956. // Context Menus: second example
  4957. // [2.1] Right-click on the TableHeadersRow() line to open the default table context menu.
  4958. // [2.2] Right-click on the ".." to open a custom popup
  4959. // [2.3] Right-click in columns to open another custom popup
  4960. HelpMarker(
  4961. "Demonstrate mixing table context menu (over header), item context button (over button) "
  4962. "and custom per-colunm context menu (over column body).");
  4963. ImGuiTableFlags flags2 = ImGuiTableFlags_Resizable | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders;
  4964. if (ImGui::BeginTable("table_context_menu_2", COLUMNS_COUNT, flags2))
  4965. {
  4966. ImGui::TableSetupColumn("One");
  4967. ImGui::TableSetupColumn("Two");
  4968. ImGui::TableSetupColumn("Three");
  4969. // [2.1] Right-click on the TableHeadersRow() line to open the default table context menu.
  4970. ImGui::TableHeadersRow();
  4971. for (int row = 0; row < 4; row++)
  4972. {
  4973. ImGui::TableNextRow();
  4974. for (int column = 0; column < COLUMNS_COUNT; column++)
  4975. {
  4976. // Submit dummy contents
  4977. ImGui::TableSetColumnIndex(column);
  4978. ImGui::Text("Cell %d,%d", column, row);
  4979. ImGui::SameLine();
  4980. // [2.2] Right-click on the ".." to open a custom popup
  4981. ImGui::PushID(row * COLUMNS_COUNT + column);
  4982. ImGui::SmallButton("..");
  4983. if (ImGui::BeginPopupContextItem())
  4984. {
  4985. ImGui::Text("This is the popup for Button(\"..\") in Cell %d,%d", column, row);
  4986. if (ImGui::Button("Close"))
  4987. ImGui::CloseCurrentPopup();
  4988. ImGui::EndPopup();
  4989. }
  4990. ImGui::PopID();
  4991. }
  4992. }
  4993. // [2.3] Right-click anywhere in columns to open another custom popup
  4994. // (instead of testing for !IsAnyItemHovered() we could also call OpenPopup() with ImGuiPopupFlags_NoOpenOverExistingPopup
  4995. // to manage popup priority as the popups triggers, here "are we hovering a column" are overlapping)
  4996. int hovered_column = -1;
  4997. for (int column = 0; column < COLUMNS_COUNT + 1; column++)
  4998. {
  4999. ImGui::PushID(column);
  5000. if (ImGui::TableGetColumnFlags(column) & ImGuiTableColumnFlags_IsHovered)
  5001. hovered_column = column;
  5002. if (hovered_column == column && !ImGui::IsAnyItemHovered() && ImGui::IsMouseReleased(1))
  5003. ImGui::OpenPopup("MyPopup");
  5004. if (ImGui::BeginPopup("MyPopup"))
  5005. {
  5006. if (column == COLUMNS_COUNT)
  5007. ImGui::Text("This is a custom popup for unused space after the last column.");
  5008. else
  5009. ImGui::Text("This is a custom popup for Column %d", column);
  5010. if (ImGui::Button("Close"))
  5011. ImGui::CloseCurrentPopup();
  5012. ImGui::EndPopup();
  5013. }
  5014. ImGui::PopID();
  5015. }
  5016. ImGui::EndTable();
  5017. ImGui::Text("Hovered column: %d", hovered_column);
  5018. }
  5019. ImGui::TreePop();
  5020. }
  5021. // Demonstrate creating multiple tables with the same ID
  5022. if (open_action != -1)
  5023. ImGui::SetNextItemOpen(open_action != 0);
  5024. IMGUI_DEMO_MARKER("Tables/Synced instances");
  5025. if (ImGui::TreeNode("Synced instances"))
  5026. {
  5027. HelpMarker("Multiple tables with the same identifier will share their settings, width, visibility, order etc.");
  5028. static ImGuiTableFlags flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoSavedSettings;
  5029. ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable);
  5030. ImGui::CheckboxFlags("ImGuiTableFlags_ScrollY", &flags, ImGuiTableFlags_ScrollY);
  5031. ImGui::CheckboxFlags("ImGuiTableFlags_SizingFixedFit", &flags, ImGuiTableFlags_SizingFixedFit);
  5032. ImGui::CheckboxFlags("ImGuiTableFlags_HighlightHoveredColumn", &flags, ImGuiTableFlags_HighlightHoveredColumn);
  5033. for (int n = 0; n < 3; n++)
  5034. {
  5035. char buf[32];
  5036. sprintf(buf, "Synced Table %d", n);
  5037. bool open = ImGui::CollapsingHeader(buf, ImGuiTreeNodeFlags_DefaultOpen);
  5038. if (open && ImGui::BeginTable("Table", 3, flags, ImVec2(0.0f, ImGui::GetTextLineHeightWithSpacing() * 5)))
  5039. {
  5040. ImGui::TableSetupColumn("One");
  5041. ImGui::TableSetupColumn("Two");
  5042. ImGui::TableSetupColumn("Three");
  5043. ImGui::TableHeadersRow();
  5044. const int cell_count = (n == 1) ? 27 : 9; // Make second table have a scrollbar to verify that additional decoration is not affecting column positions.
  5045. for (int cell = 0; cell < cell_count; cell++)
  5046. {
  5047. ImGui::TableNextColumn();
  5048. ImGui::Text("this cell %d", cell);
  5049. }
  5050. ImGui::EndTable();
  5051. }
  5052. }
  5053. ImGui::TreePop();
  5054. }
  5055. // Demonstrate using Sorting facilities
  5056. // This is a simplified version of the "Advanced" example, where we mostly focus on the code necessary to handle sorting.
  5057. // Note that the "Advanced" example also showcase manually triggering a sort (e.g. if item quantities have been modified)
  5058. static const char* template_items_names[] =
  5059. {
  5060. "Banana", "Apple", "Cherry", "Watermelon", "Grapefruit", "Strawberry", "Mango",
  5061. "Kiwi", "Orange", "Pineapple", "Blueberry", "Plum", "Coconut", "Pear", "Apricot"
  5062. };
  5063. if (open_action != -1)
  5064. ImGui::SetNextItemOpen(open_action != 0);
  5065. IMGUI_DEMO_MARKER("Tables/Sorting");
  5066. if (ImGui::TreeNode("Sorting"))
  5067. {
  5068. // Create item list
  5069. static ImVector<MyItem> items;
  5070. if (items.Size == 0)
  5071. {
  5072. items.resize(50, MyItem());
  5073. for (int n = 0; n < items.Size; n++)
  5074. {
  5075. const int template_n = n % IM_ARRAYSIZE(template_items_names);
  5076. MyItem& item = items[n];
  5077. item.ID = n;
  5078. item.Name = template_items_names[template_n];
  5079. item.Quantity = (n * n - n) % 20; // Assign default quantities
  5080. }
  5081. }
  5082. // Options
  5083. static ImGuiTableFlags flags =
  5084. ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Sortable | ImGuiTableFlags_SortMulti
  5085. | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_NoBordersInBody
  5086. | ImGuiTableFlags_ScrollY;
  5087. PushStyleCompact();
  5088. ImGui::CheckboxFlags("ImGuiTableFlags_SortMulti", &flags, ImGuiTableFlags_SortMulti);
  5089. ImGui::SameLine(); HelpMarker("When sorting is enabled: hold shift when clicking headers to sort on multiple column. TableGetSortSpecs() may return specs where (SpecsCount > 1).");
  5090. ImGui::CheckboxFlags("ImGuiTableFlags_SortTristate", &flags, ImGuiTableFlags_SortTristate);
  5091. ImGui::SameLine(); HelpMarker("When sorting is enabled: allow no sorting, disable default sorting. TableGetSortSpecs() may return specs where (SpecsCount == 0).");
  5092. PopStyleCompact();
  5093. if (ImGui::BeginTable("table_sorting", 4, flags, ImVec2(0.0f, TEXT_BASE_HEIGHT * 15), 0.0f))
  5094. {
  5095. // Declare columns
  5096. // We use the "user_id" parameter of TableSetupColumn() to specify a user id that will be stored in the sort specifications.
  5097. // This is so our sort function can identify a column given our own identifier. We could also identify them based on their index!
  5098. // Demonstrate using a mixture of flags among available sort-related flags:
  5099. // - ImGuiTableColumnFlags_DefaultSort
  5100. // - ImGuiTableColumnFlags_NoSort / ImGuiTableColumnFlags_NoSortAscending / ImGuiTableColumnFlags_NoSortDescending
  5101. // - ImGuiTableColumnFlags_PreferSortAscending / ImGuiTableColumnFlags_PreferSortDescending
  5102. ImGui::TableSetupColumn("ID", ImGuiTableColumnFlags_DefaultSort | ImGuiTableColumnFlags_WidthFixed, 0.0f, MyItemColumnID_ID);
  5103. ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthFixed, 0.0f, MyItemColumnID_Name);
  5104. ImGui::TableSetupColumn("Action", ImGuiTableColumnFlags_NoSort | ImGuiTableColumnFlags_WidthFixed, 0.0f, MyItemColumnID_Action);
  5105. ImGui::TableSetupColumn("Quantity", ImGuiTableColumnFlags_PreferSortDescending | ImGuiTableColumnFlags_WidthStretch, 0.0f, MyItemColumnID_Quantity);
  5106. ImGui::TableSetupScrollFreeze(0, 1); // Make row always visible
  5107. ImGui::TableHeadersRow();
  5108. // Sort our data if sort specs have been changed!
  5109. if (ImGuiTableSortSpecs* sort_specs = ImGui::TableGetSortSpecs())
  5110. if (sort_specs->SpecsDirty)
  5111. {
  5112. MyItem::SortWithSortSpecs(sort_specs, items.Data, items.Size);
  5113. sort_specs->SpecsDirty = false;
  5114. }
  5115. // Demonstrate using clipper for large vertical lists
  5116. ImGuiListClipper clipper;
  5117. clipper.Begin(items.Size);
  5118. while (clipper.Step())
  5119. for (int row_n = clipper.DisplayStart; row_n < clipper.DisplayEnd; row_n++)
  5120. {
  5121. // Display a data item
  5122. MyItem* item = &items[row_n];
  5123. ImGui::PushID(item->ID);
  5124. ImGui::TableNextRow();
  5125. ImGui::TableNextColumn();
  5126. ImGui::Text("%04d", item->ID);
  5127. ImGui::TableNextColumn();
  5128. ImGui::TextUnformatted(item->Name);
  5129. ImGui::TableNextColumn();
  5130. ImGui::SmallButton("None");
  5131. ImGui::TableNextColumn();
  5132. ImGui::Text("%d", item->Quantity);
  5133. ImGui::PopID();
  5134. }
  5135. ImGui::EndTable();
  5136. }
  5137. ImGui::TreePop();
  5138. }
  5139. // In this example we'll expose most table flags and settings.
  5140. // For specific flags and settings refer to the corresponding section for more detailed explanation.
  5141. // This section is mostly useful to experiment with combining certain flags or settings with each others.
  5142. //ImGui::SetNextItemOpen(true, ImGuiCond_Once); // [DEBUG]
  5143. if (open_action != -1)
  5144. ImGui::SetNextItemOpen(open_action != 0);
  5145. IMGUI_DEMO_MARKER("Tables/Advanced");
  5146. if (ImGui::TreeNode("Advanced"))
  5147. {
  5148. static ImGuiTableFlags flags =
  5149. ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable
  5150. | ImGuiTableFlags_Sortable | ImGuiTableFlags_SortMulti
  5151. | ImGuiTableFlags_RowBg | ImGuiTableFlags_Borders | ImGuiTableFlags_NoBordersInBody
  5152. | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY
  5153. | ImGuiTableFlags_SizingFixedFit;
  5154. static ImGuiTableColumnFlags columns_base_flags = ImGuiTableColumnFlags_None;
  5155. enum ContentsType { CT_Text, CT_Button, CT_SmallButton, CT_FillButton, CT_Selectable, CT_SelectableSpanRow };
  5156. static int contents_type = CT_SelectableSpanRow;
  5157. const char* contents_type_names[] = { "Text", "Button", "SmallButton", "FillButton", "Selectable", "Selectable (span row)" };
  5158. static int freeze_cols = 1;
  5159. static int freeze_rows = 1;
  5160. static int items_count = IM_ARRAYSIZE(template_items_names) * 2;
  5161. static ImVec2 outer_size_value = ImVec2(0.0f, TEXT_BASE_HEIGHT * 12);
  5162. static float row_min_height = 0.0f; // Auto
  5163. static float inner_width_with_scroll = 0.0f; // Auto-extend
  5164. static bool outer_size_enabled = true;
  5165. static bool show_headers = true;
  5166. static bool show_wrapped_text = false;
  5167. //static ImGuiTextFilter filter;
  5168. //ImGui::SetNextItemOpen(true, ImGuiCond_Once); // FIXME-TABLE: Enabling this results in initial clipped first pass on table which tend to affect column sizing
  5169. if (ImGui::TreeNode("Options"))
  5170. {
  5171. // Make the UI compact because there are so many fields
  5172. PushStyleCompact();
  5173. ImGui::PushItemWidth(TEXT_BASE_WIDTH * 28.0f);
  5174. if (ImGui::TreeNodeEx("Features:", ImGuiTreeNodeFlags_DefaultOpen))
  5175. {
  5176. ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable);
  5177. ImGui::CheckboxFlags("ImGuiTableFlags_Reorderable", &flags, ImGuiTableFlags_Reorderable);
  5178. ImGui::CheckboxFlags("ImGuiTableFlags_Hideable", &flags, ImGuiTableFlags_Hideable);
  5179. ImGui::CheckboxFlags("ImGuiTableFlags_Sortable", &flags, ImGuiTableFlags_Sortable);
  5180. ImGui::CheckboxFlags("ImGuiTableFlags_NoSavedSettings", &flags, ImGuiTableFlags_NoSavedSettings);
  5181. ImGui::CheckboxFlags("ImGuiTableFlags_ContextMenuInBody", &flags, ImGuiTableFlags_ContextMenuInBody);
  5182. ImGui::TreePop();
  5183. }
  5184. if (ImGui::TreeNodeEx("Decorations:", ImGuiTreeNodeFlags_DefaultOpen))
  5185. {
  5186. ImGui::CheckboxFlags("ImGuiTableFlags_RowBg", &flags, ImGuiTableFlags_RowBg);
  5187. ImGui::CheckboxFlags("ImGuiTableFlags_BordersV", &flags, ImGuiTableFlags_BordersV);
  5188. ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterV", &flags, ImGuiTableFlags_BordersOuterV);
  5189. ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerV", &flags, ImGuiTableFlags_BordersInnerV);
  5190. ImGui::CheckboxFlags("ImGuiTableFlags_BordersH", &flags, ImGuiTableFlags_BordersH);
  5191. ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterH", &flags, ImGuiTableFlags_BordersOuterH);
  5192. ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerH", &flags, ImGuiTableFlags_BordersInnerH);
  5193. ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body (borders will always appear in Headers");
  5194. ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBodyUntilResize", &flags, ImGuiTableFlags_NoBordersInBodyUntilResize); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body until hovered for resize (borders will always appear in Headers)");
  5195. ImGui::TreePop();
  5196. }
  5197. if (ImGui::TreeNodeEx("Sizing:", ImGuiTreeNodeFlags_DefaultOpen))
  5198. {
  5199. EditTableSizingFlags(&flags);
  5200. ImGui::SameLine(); HelpMarker("In the Advanced demo we override the policy of each column so those table-wide settings have less effect that typical.");
  5201. ImGui::CheckboxFlags("ImGuiTableFlags_NoHostExtendX", &flags, ImGuiTableFlags_NoHostExtendX);
  5202. ImGui::SameLine(); HelpMarker("Make outer width auto-fit to columns, overriding outer_size.x value.\n\nOnly available when ScrollX/ScrollY are disabled and Stretch columns are not used.");
  5203. ImGui::CheckboxFlags("ImGuiTableFlags_NoHostExtendY", &flags, ImGuiTableFlags_NoHostExtendY);
  5204. ImGui::SameLine(); HelpMarker("Make outer height stop exactly at outer_size.y (prevent auto-extending table past the limit).\n\nOnly available when ScrollX/ScrollY are disabled. Data below the limit will be clipped and not visible.");
  5205. ImGui::CheckboxFlags("ImGuiTableFlags_NoKeepColumnsVisible", &flags, ImGuiTableFlags_NoKeepColumnsVisible);
  5206. ImGui::SameLine(); HelpMarker("Only available if ScrollX is disabled.");
  5207. ImGui::CheckboxFlags("ImGuiTableFlags_PreciseWidths", &flags, ImGuiTableFlags_PreciseWidths);
  5208. ImGui::SameLine(); HelpMarker("Disable distributing remainder width to stretched columns (width allocation on a 100-wide table with 3 columns: Without this flag: 33,33,34. With this flag: 33,33,33). With larger number of columns, resizing will appear to be less smooth.");
  5209. ImGui::CheckboxFlags("ImGuiTableFlags_NoClip", &flags, ImGuiTableFlags_NoClip);
  5210. ImGui::SameLine(); HelpMarker("Disable clipping rectangle for every individual columns (reduce draw command count, items will be able to overflow into other columns). Generally incompatible with ScrollFreeze options.");
  5211. ImGui::TreePop();
  5212. }
  5213. if (ImGui::TreeNodeEx("Padding:", ImGuiTreeNodeFlags_DefaultOpen))
  5214. {
  5215. ImGui::CheckboxFlags("ImGuiTableFlags_PadOuterX", &flags, ImGuiTableFlags_PadOuterX);
  5216. ImGui::CheckboxFlags("ImGuiTableFlags_NoPadOuterX", &flags, ImGuiTableFlags_NoPadOuterX);
  5217. ImGui::CheckboxFlags("ImGuiTableFlags_NoPadInnerX", &flags, ImGuiTableFlags_NoPadInnerX);
  5218. ImGui::TreePop();
  5219. }
  5220. if (ImGui::TreeNodeEx("Scrolling:", ImGuiTreeNodeFlags_DefaultOpen))
  5221. {
  5222. ImGui::CheckboxFlags("ImGuiTableFlags_ScrollX", &flags, ImGuiTableFlags_ScrollX);
  5223. ImGui::SameLine();
  5224. ImGui::SetNextItemWidth(ImGui::GetFrameHeight());
  5225. ImGui::DragInt("freeze_cols", &freeze_cols, 0.2f, 0, 9, NULL, ImGuiSliderFlags_NoInput);
  5226. ImGui::CheckboxFlags("ImGuiTableFlags_ScrollY", &flags, ImGuiTableFlags_ScrollY);
  5227. ImGui::SameLine();
  5228. ImGui::SetNextItemWidth(ImGui::GetFrameHeight());
  5229. ImGui::DragInt("freeze_rows", &freeze_rows, 0.2f, 0, 9, NULL, ImGuiSliderFlags_NoInput);
  5230. ImGui::TreePop();
  5231. }
  5232. if (ImGui::TreeNodeEx("Sorting:", ImGuiTreeNodeFlags_DefaultOpen))
  5233. {
  5234. ImGui::CheckboxFlags("ImGuiTableFlags_SortMulti", &flags, ImGuiTableFlags_SortMulti);
  5235. ImGui::SameLine(); HelpMarker("When sorting is enabled: hold shift when clicking headers to sort on multiple column. TableGetSortSpecs() may return specs where (SpecsCount > 1).");
  5236. ImGui::CheckboxFlags("ImGuiTableFlags_SortTristate", &flags, ImGuiTableFlags_SortTristate);
  5237. ImGui::SameLine(); HelpMarker("When sorting is enabled: allow no sorting, disable default sorting. TableGetSortSpecs() may return specs where (SpecsCount == 0).");
  5238. ImGui::TreePop();
  5239. }
  5240. if (ImGui::TreeNodeEx("Headers:", ImGuiTreeNodeFlags_DefaultOpen))
  5241. {
  5242. ImGui::Checkbox("show_headers", &show_headers);
  5243. ImGui::CheckboxFlags("ImGuiTableFlags_HighlightHoveredColumn", &flags, ImGuiTableFlags_HighlightHoveredColumn);
  5244. ImGui::CheckboxFlags("ImGuiTableColumnFlags_AngledHeader", &columns_base_flags, ImGuiTableColumnFlags_AngledHeader);
  5245. ImGui::SameLine(); HelpMarker("Enable AngledHeader on all columns. Best enabled on selected narrow columns (see \"Angled headers\" section of the demo).");
  5246. ImGui::TreePop();
  5247. }
  5248. if (ImGui::TreeNodeEx("Other:", ImGuiTreeNodeFlags_DefaultOpen))
  5249. {
  5250. ImGui::Checkbox("show_wrapped_text", &show_wrapped_text);
  5251. ImGui::DragFloat2("##OuterSize", &outer_size_value.x);
  5252. ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x);
  5253. ImGui::Checkbox("outer_size", &outer_size_enabled);
  5254. ImGui::SameLine();
  5255. HelpMarker("If scrolling is disabled (ScrollX and ScrollY not set):\n"
  5256. "- The table is output directly in the parent window.\n"
  5257. "- OuterSize.x < 0.0f will right-align the table.\n"
  5258. "- OuterSize.x = 0.0f will narrow fit the table unless there are any Stretch columns.\n"
  5259. "- OuterSize.y then becomes the minimum size for the table, which will extend vertically if there are more rows (unless NoHostExtendY is set).");
  5260. // From a user point of view we will tend to use 'inner_width' differently depending on whether our table is embedding scrolling.
  5261. // To facilitate toying with this demo we will actually pass 0.0f to the BeginTable() when ScrollX is disabled.
  5262. ImGui::DragFloat("inner_width (when ScrollX active)", &inner_width_with_scroll, 1.0f, 0.0f, FLT_MAX);
  5263. ImGui::DragFloat("row_min_height", &row_min_height, 1.0f, 0.0f, FLT_MAX);
  5264. ImGui::SameLine(); HelpMarker("Specify height of the Selectable item.");
  5265. ImGui::DragInt("items_count", &items_count, 0.1f, 0, 9999);
  5266. ImGui::Combo("items_type (first column)", &contents_type, contents_type_names, IM_ARRAYSIZE(contents_type_names));
  5267. //filter.Draw("filter");
  5268. ImGui::TreePop();
  5269. }
  5270. ImGui::PopItemWidth();
  5271. PopStyleCompact();
  5272. ImGui::Spacing();
  5273. ImGui::TreePop();
  5274. }
  5275. // Update item list if we changed the number of items
  5276. static ImVector<MyItem> items;
  5277. static ImVector<int> selection;
  5278. static bool items_need_sort = false;
  5279. if (items.Size != items_count)
  5280. {
  5281. items.resize(items_count, MyItem());
  5282. for (int n = 0; n < items_count; n++)
  5283. {
  5284. const int template_n = n % IM_ARRAYSIZE(template_items_names);
  5285. MyItem& item = items[n];
  5286. item.ID = n;
  5287. item.Name = template_items_names[template_n];
  5288. item.Quantity = (template_n == 3) ? 10 : (template_n == 4) ? 20 : 0; // Assign default quantities
  5289. }
  5290. }
  5291. const ImDrawList* parent_draw_list = ImGui::GetWindowDrawList();
  5292. const int parent_draw_list_draw_cmd_count = parent_draw_list->CmdBuffer.Size;
  5293. ImVec2 table_scroll_cur, table_scroll_max; // For debug display
  5294. const ImDrawList* table_draw_list = NULL; // "
  5295. // Submit table
  5296. const float inner_width_to_use = (flags & ImGuiTableFlags_ScrollX) ? inner_width_with_scroll : 0.0f;
  5297. if (ImGui::BeginTable("table_advanced", 6, flags, outer_size_enabled ? outer_size_value : ImVec2(0, 0), inner_width_to_use))
  5298. {
  5299. // Declare columns
  5300. // We use the "user_id" parameter of TableSetupColumn() to specify a user id that will be stored in the sort specifications.
  5301. // This is so our sort function can identify a column given our own identifier. We could also identify them based on their index!
  5302. ImGui::TableSetupColumn("ID", columns_base_flags | ImGuiTableColumnFlags_DefaultSort | ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoHide, 0.0f, MyItemColumnID_ID);
  5303. ImGui::TableSetupColumn("Name", columns_base_flags | ImGuiTableColumnFlags_WidthFixed, 0.0f, MyItemColumnID_Name);
  5304. ImGui::TableSetupColumn("Action", columns_base_flags | ImGuiTableColumnFlags_NoSort | ImGuiTableColumnFlags_WidthFixed, 0.0f, MyItemColumnID_Action);
  5305. ImGui::TableSetupColumn("Quantity", columns_base_flags | ImGuiTableColumnFlags_PreferSortDescending, 0.0f, MyItemColumnID_Quantity);
  5306. ImGui::TableSetupColumn("Description", columns_base_flags | ((flags & ImGuiTableFlags_NoHostExtendX) ? 0 : ImGuiTableColumnFlags_WidthStretch), 0.0f, MyItemColumnID_Description);
  5307. ImGui::TableSetupColumn("Hidden", columns_base_flags | ImGuiTableColumnFlags_DefaultHide | ImGuiTableColumnFlags_NoSort);
  5308. ImGui::TableSetupScrollFreeze(freeze_cols, freeze_rows);
  5309. // Sort our data if sort specs have been changed!
  5310. ImGuiTableSortSpecs* sort_specs = ImGui::TableGetSortSpecs();
  5311. if (sort_specs && sort_specs->SpecsDirty)
  5312. items_need_sort = true;
  5313. if (sort_specs && items_need_sort && items.Size > 1)
  5314. {
  5315. MyItem::SortWithSortSpecs(sort_specs, items.Data, items.Size);
  5316. sort_specs->SpecsDirty = false;
  5317. }
  5318. items_need_sort = false;
  5319. // Take note of whether we are currently sorting based on the Quantity field,
  5320. // we will use this to trigger sorting when we know the data of this column has been modified.
  5321. const bool sorts_specs_using_quantity = (ImGui::TableGetColumnFlags(3) & ImGuiTableColumnFlags_IsSorted) != 0;
  5322. // Show headers
  5323. if (show_headers && (columns_base_flags & ImGuiTableColumnFlags_AngledHeader) != 0)
  5324. ImGui::TableAngledHeadersRow();
  5325. if (show_headers)
  5326. ImGui::TableHeadersRow();
  5327. // Show data
  5328. // FIXME-TABLE FIXME-NAV: How we can get decent up/down even though we have the buttons here?
  5329. ImGui::PushButtonRepeat(true);
  5330. #if 1
  5331. // Demonstrate using clipper for large vertical lists
  5332. ImGuiListClipper clipper;
  5333. clipper.Begin(items.Size);
  5334. while (clipper.Step())
  5335. {
  5336. for (int row_n = clipper.DisplayStart; row_n < clipper.DisplayEnd; row_n++)
  5337. #else
  5338. // Without clipper
  5339. {
  5340. for (int row_n = 0; row_n < items.Size; row_n++)
  5341. #endif
  5342. {
  5343. MyItem* item = &items[row_n];
  5344. //if (!filter.PassFilter(item->Name))
  5345. // continue;
  5346. const bool item_is_selected = selection.contains(item->ID);
  5347. ImGui::PushID(item->ID);
  5348. ImGui::TableNextRow(ImGuiTableRowFlags_None, row_min_height);
  5349. // For the demo purpose we can select among different type of items submitted in the first column
  5350. ImGui::TableSetColumnIndex(0);
  5351. char label[32];
  5352. sprintf(label, "%04d", item->ID);
  5353. if (contents_type == CT_Text)
  5354. ImGui::TextUnformatted(label);
  5355. else if (contents_type == CT_Button)
  5356. ImGui::Button(label);
  5357. else if (contents_type == CT_SmallButton)
  5358. ImGui::SmallButton(label);
  5359. else if (contents_type == CT_FillButton)
  5360. ImGui::Button(label, ImVec2(-FLT_MIN, 0.0f));
  5361. else if (contents_type == CT_Selectable || contents_type == CT_SelectableSpanRow)
  5362. {
  5363. ImGuiSelectableFlags selectable_flags = (contents_type == CT_SelectableSpanRow) ? ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowOverlap : ImGuiSelectableFlags_None;
  5364. if (ImGui::Selectable(label, item_is_selected, selectable_flags, ImVec2(0, row_min_height)))
  5365. {
  5366. if (ImGui::GetIO().KeyCtrl)
  5367. {
  5368. if (item_is_selected)
  5369. selection.find_erase_unsorted(item->ID);
  5370. else
  5371. selection.push_back(item->ID);
  5372. }
  5373. else
  5374. {
  5375. selection.clear();
  5376. selection.push_back(item->ID);
  5377. }
  5378. }
  5379. }
  5380. if (ImGui::TableSetColumnIndex(1))
  5381. ImGui::TextUnformatted(item->Name);
  5382. // Here we demonstrate marking our data set as needing to be sorted again if we modified a quantity,
  5383. // and we are currently sorting on the column showing the Quantity.
  5384. // To avoid triggering a sort while holding the button, we only trigger it when the button has been released.
  5385. // You will probably need some extra logic if you want to automatically sort when a specific entry changes.
  5386. if (ImGui::TableSetColumnIndex(2))
  5387. {
  5388. if (ImGui::SmallButton("Chop")) { item->Quantity += 1; }
  5389. if (sorts_specs_using_quantity && ImGui::IsItemDeactivated()) { items_need_sort = true; }
  5390. ImGui::SameLine();
  5391. if (ImGui::SmallButton("Eat")) { item->Quantity -= 1; }
  5392. if (sorts_specs_using_quantity && ImGui::IsItemDeactivated()) { items_need_sort = true; }
  5393. }
  5394. if (ImGui::TableSetColumnIndex(3))
  5395. ImGui::Text("%d", item->Quantity);
  5396. ImGui::TableSetColumnIndex(4);
  5397. if (show_wrapped_text)
  5398. ImGui::TextWrapped("Lorem ipsum dolor sit amet");
  5399. else
  5400. ImGui::Text("Lorem ipsum dolor sit amet");
  5401. if (ImGui::TableSetColumnIndex(5))
  5402. ImGui::Text("1234");
  5403. ImGui::PopID();
  5404. }
  5405. }
  5406. ImGui::PopButtonRepeat();
  5407. // Store some info to display debug details below
  5408. table_scroll_cur = ImVec2(ImGui::GetScrollX(), ImGui::GetScrollY());
  5409. table_scroll_max = ImVec2(ImGui::GetScrollMaxX(), ImGui::GetScrollMaxY());
  5410. table_draw_list = ImGui::GetWindowDrawList();
  5411. ImGui::EndTable();
  5412. }
  5413. static bool show_debug_details = false;
  5414. ImGui::Checkbox("Debug details", &show_debug_details);
  5415. if (show_debug_details && table_draw_list)
  5416. {
  5417. ImGui::SameLine(0.0f, 0.0f);
  5418. const int table_draw_list_draw_cmd_count = table_draw_list->CmdBuffer.Size;
  5419. if (table_draw_list == parent_draw_list)
  5420. ImGui::Text(": DrawCmd: +%d (in same window)",
  5421. table_draw_list_draw_cmd_count - parent_draw_list_draw_cmd_count);
  5422. else
  5423. ImGui::Text(": DrawCmd: +%d (in child window), Scroll: (%.f/%.f) (%.f/%.f)",
  5424. table_draw_list_draw_cmd_count - 1, table_scroll_cur.x, table_scroll_max.x, table_scroll_cur.y, table_scroll_max.y);
  5425. }
  5426. ImGui::TreePop();
  5427. }
  5428. ImGui::PopID();
  5429. ShowDemoWindowColumns();
  5430. if (disable_indent)
  5431. ImGui::PopStyleVar();
  5432. }
  5433. // Demonstrate old/legacy Columns API!
  5434. // [2020: Columns are under-featured and not maintained. Prefer using the more flexible and powerful BeginTable() API!]
  5435. static void ShowDemoWindowColumns()
  5436. {
  5437. IMGUI_DEMO_MARKER("Columns (legacy API)");
  5438. bool open = ImGui::TreeNode("Legacy Columns API");
  5439. ImGui::SameLine();
  5440. HelpMarker("Columns() is an old API! Prefer using the more flexible and powerful BeginTable() API!");
  5441. if (!open)
  5442. return;
  5443. // Basic columns
  5444. IMGUI_DEMO_MARKER("Columns (legacy API)/Basic");
  5445. if (ImGui::TreeNode("Basic"))
  5446. {
  5447. ImGui::Text("Without border:");
  5448. ImGui::Columns(3, "mycolumns3", false); // 3-ways, no border
  5449. ImGui::Separator();
  5450. for (int n = 0; n < 14; n++)
  5451. {
  5452. char label[32];
  5453. sprintf(label, "Item %d", n);
  5454. if (ImGui::Selectable(label)) {}
  5455. //if (ImGui::Button(label, ImVec2(-FLT_MIN,0.0f))) {}
  5456. ImGui::NextColumn();
  5457. }
  5458. ImGui::Columns(1);
  5459. ImGui::Separator();
  5460. ImGui::Text("With border:");
  5461. ImGui::Columns(4, "mycolumns"); // 4-ways, with border
  5462. ImGui::Separator();
  5463. ImGui::Text("ID"); ImGui::NextColumn();
  5464. ImGui::Text("Name"); ImGui::NextColumn();
  5465. ImGui::Text("Path"); ImGui::NextColumn();
  5466. ImGui::Text("Hovered"); ImGui::NextColumn();
  5467. ImGui::Separator();
  5468. const char* names[3] = { "One", "Two", "Three" };
  5469. const char* paths[3] = { "/path/one", "/path/two", "/path/three" };
  5470. static int selected = -1;
  5471. for (int i = 0; i < 3; i++)
  5472. {
  5473. char label[32];
  5474. sprintf(label, "%04d", i);
  5475. if (ImGui::Selectable(label, selected == i, ImGuiSelectableFlags_SpanAllColumns))
  5476. selected = i;
  5477. bool hovered = ImGui::IsItemHovered();
  5478. ImGui::NextColumn();
  5479. ImGui::Text(names[i]); ImGui::NextColumn();
  5480. ImGui::Text(paths[i]); ImGui::NextColumn();
  5481. ImGui::Text("%d", hovered); ImGui::NextColumn();
  5482. }
  5483. ImGui::Columns(1);
  5484. ImGui::Separator();
  5485. ImGui::TreePop();
  5486. }
  5487. IMGUI_DEMO_MARKER("Columns (legacy API)/Borders");
  5488. if (ImGui::TreeNode("Borders"))
  5489. {
  5490. // NB: Future columns API should allow automatic horizontal borders.
  5491. static bool h_borders = true;
  5492. static bool v_borders = true;
  5493. static int columns_count = 4;
  5494. const int lines_count = 3;
  5495. ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
  5496. ImGui::DragInt("##columns_count", &columns_count, 0.1f, 2, 10, "%d columns");
  5497. if (columns_count < 2)
  5498. columns_count = 2;
  5499. ImGui::SameLine();
  5500. ImGui::Checkbox("horizontal", &h_borders);
  5501. ImGui::SameLine();
  5502. ImGui::Checkbox("vertical", &v_borders);
  5503. ImGui::Columns(columns_count, NULL, v_borders);
  5504. for (int i = 0; i < columns_count * lines_count; i++)
  5505. {
  5506. if (h_borders && ImGui::GetColumnIndex() == 0)
  5507. ImGui::Separator();
  5508. ImGui::Text("%c%c%c", 'a' + i, 'a' + i, 'a' + i);
  5509. ImGui::Text("Width %.2f", ImGui::GetColumnWidth());
  5510. ImGui::Text("Avail %.2f", ImGui::GetContentRegionAvail().x);
  5511. ImGui::Text("Offset %.2f", ImGui::GetColumnOffset());
  5512. ImGui::Text("Long text that is likely to clip");
  5513. ImGui::Button("Button", ImVec2(-FLT_MIN, 0.0f));
  5514. ImGui::NextColumn();
  5515. }
  5516. ImGui::Columns(1);
  5517. if (h_borders)
  5518. ImGui::Separator();
  5519. ImGui::TreePop();
  5520. }
  5521. // Create multiple items in a same cell before switching to next column
  5522. IMGUI_DEMO_MARKER("Columns (legacy API)/Mixed items");
  5523. if (ImGui::TreeNode("Mixed items"))
  5524. {
  5525. ImGui::Columns(3, "mixed");
  5526. ImGui::Separator();
  5527. ImGui::Text("Hello");
  5528. ImGui::Button("Banana");
  5529. ImGui::NextColumn();
  5530. ImGui::Text("ImGui");
  5531. ImGui::Button("Apple");
  5532. static float foo = 1.0f;
  5533. ImGui::InputFloat("red", &foo, 0.05f, 0, "%.3f");
  5534. ImGui::Text("An extra line here.");
  5535. ImGui::NextColumn();
  5536. ImGui::Text("Sailor");
  5537. ImGui::Button("Corniflower");
  5538. static float bar = 1.0f;
  5539. ImGui::InputFloat("blue", &bar, 0.05f, 0, "%.3f");
  5540. ImGui::NextColumn();
  5541. if (ImGui::CollapsingHeader("Category A")) { ImGui::Text("Blah blah blah"); } ImGui::NextColumn();
  5542. if (ImGui::CollapsingHeader("Category B")) { ImGui::Text("Blah blah blah"); } ImGui::NextColumn();
  5543. if (ImGui::CollapsingHeader("Category C")) { ImGui::Text("Blah blah blah"); } ImGui::NextColumn();
  5544. ImGui::Columns(1);
  5545. ImGui::Separator();
  5546. ImGui::TreePop();
  5547. }
  5548. // Word wrapping
  5549. IMGUI_DEMO_MARKER("Columns (legacy API)/Word-wrapping");
  5550. if (ImGui::TreeNode("Word-wrapping"))
  5551. {
  5552. ImGui::Columns(2, "word-wrapping");
  5553. ImGui::Separator();
  5554. ImGui::TextWrapped("The quick brown fox jumps over the lazy dog.");
  5555. ImGui::TextWrapped("Hello Left");
  5556. ImGui::NextColumn();
  5557. ImGui::TextWrapped("The quick brown fox jumps over the lazy dog.");
  5558. ImGui::TextWrapped("Hello Right");
  5559. ImGui::Columns(1);
  5560. ImGui::Separator();
  5561. ImGui::TreePop();
  5562. }
  5563. IMGUI_DEMO_MARKER("Columns (legacy API)/Horizontal Scrolling");
  5564. if (ImGui::TreeNode("Horizontal Scrolling"))
  5565. {
  5566. ImGui::SetNextWindowContentSize(ImVec2(1500.0f, 0.0f));
  5567. ImVec2 child_size = ImVec2(0, ImGui::GetFontSize() * 20.0f);
  5568. ImGui::BeginChild("##ScrollingRegion", child_size, ImGuiChildFlags_None, ImGuiWindowFlags_HorizontalScrollbar);
  5569. ImGui::Columns(10);
  5570. // Also demonstrate using clipper for large vertical lists
  5571. int ITEMS_COUNT = 2000;
  5572. ImGuiListClipper clipper;
  5573. clipper.Begin(ITEMS_COUNT);
  5574. while (clipper.Step())
  5575. {
  5576. for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)
  5577. for (int j = 0; j < 10; j++)
  5578. {
  5579. ImGui::Text("Line %d Column %d...", i, j);
  5580. ImGui::NextColumn();
  5581. }
  5582. }
  5583. ImGui::Columns(1);
  5584. ImGui::EndChild();
  5585. ImGui::TreePop();
  5586. }
  5587. IMGUI_DEMO_MARKER("Columns (legacy API)/Tree");
  5588. if (ImGui::TreeNode("Tree"))
  5589. {
  5590. ImGui::Columns(2, "tree", true);
  5591. for (int x = 0; x < 3; x++)
  5592. {
  5593. bool open1 = ImGui::TreeNode((void*)(intptr_t)x, "Node%d", x);
  5594. ImGui::NextColumn();
  5595. ImGui::Text("Node contents");
  5596. ImGui::NextColumn();
  5597. if (open1)
  5598. {
  5599. for (int y = 0; y < 3; y++)
  5600. {
  5601. bool open2 = ImGui::TreeNode((void*)(intptr_t)y, "Node%d.%d", x, y);
  5602. ImGui::NextColumn();
  5603. ImGui::Text("Node contents");
  5604. if (open2)
  5605. {
  5606. ImGui::Text("Even more contents");
  5607. if (ImGui::TreeNode("Tree in column"))
  5608. {
  5609. ImGui::Text("The quick brown fox jumps over the lazy dog");
  5610. ImGui::TreePop();
  5611. }
  5612. }
  5613. ImGui::NextColumn();
  5614. if (open2)
  5615. ImGui::TreePop();
  5616. }
  5617. ImGui::TreePop();
  5618. }
  5619. }
  5620. ImGui::Columns(1);
  5621. ImGui::TreePop();
  5622. }
  5623. ImGui::TreePop();
  5624. }
  5625. static void ShowDemoWindowInputs()
  5626. {
  5627. IMGUI_DEMO_MARKER("Inputs & Focus");
  5628. if (ImGui::CollapsingHeader("Inputs & Focus"))
  5629. {
  5630. ImGuiIO& io = ImGui::GetIO();
  5631. // Display inputs submitted to ImGuiIO
  5632. IMGUI_DEMO_MARKER("Inputs & Focus/Inputs");
  5633. ImGui::SetNextItemOpen(true, ImGuiCond_Once);
  5634. bool inputs_opened = ImGui::TreeNode("Inputs");
  5635. ImGui::SameLine();
  5636. HelpMarker(
  5637. "This is a simplified view. See more detailed input state:\n"
  5638. "- in 'Tools->Metrics/Debugger->Inputs'.\n"
  5639. "- in 'Tools->Debug Log->IO'.");
  5640. if (inputs_opened)
  5641. {
  5642. if (ImGui::IsMousePosValid())
  5643. ImGui::Text("Mouse pos: (%g, %g)", io.MousePos.x, io.MousePos.y);
  5644. else
  5645. ImGui::Text("Mouse pos: <INVALID>");
  5646. ImGui::Text("Mouse delta: (%g, %g)", io.MouseDelta.x, io.MouseDelta.y);
  5647. ImGui::Text("Mouse down:");
  5648. for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseDown(i)) { ImGui::SameLine(); ImGui::Text("b%d (%.02f secs)", i, io.MouseDownDuration[i]); }
  5649. ImGui::Text("Mouse wheel: %.1f", io.MouseWheel);
  5650. // We iterate both legacy native range and named ImGuiKey ranges. This is a little unusual/odd but this allows
  5651. // displaying the data for old/new backends.
  5652. // User code should never have to go through such hoops!
  5653. // You can generally iterate between ImGuiKey_NamedKey_BEGIN and ImGuiKey_NamedKey_END.
  5654. #ifdef IMGUI_DISABLE_OBSOLETE_KEYIO
  5655. struct funcs { static bool IsLegacyNativeDupe(ImGuiKey) { return false; } };
  5656. ImGuiKey start_key = ImGuiKey_NamedKey_BEGIN;
  5657. #else
  5658. struct funcs { static bool IsLegacyNativeDupe(ImGuiKey key) { return key >= 0 && key < 512 && ImGui::GetIO().KeyMap[key] != -1; } }; // Hide Native<>ImGuiKey duplicates when both exists in the array
  5659. ImGuiKey start_key = (ImGuiKey)0;
  5660. #endif
  5661. ImGui::Text("Keys down:"); for (ImGuiKey key = start_key; key < ImGuiKey_NamedKey_END; key = (ImGuiKey)(key + 1)) { if (funcs::IsLegacyNativeDupe(key) || !ImGui::IsKeyDown(key)) continue; ImGui::SameLine(); ImGui::Text((key < ImGuiKey_NamedKey_BEGIN) ? "\"%s\"" : "\"%s\" %d", ImGui::GetKeyName(key), key); }
  5662. ImGui::Text("Keys mods: %s%s%s%s", io.KeyCtrl ? "CTRL " : "", io.KeyShift ? "SHIFT " : "", io.KeyAlt ? "ALT " : "", io.KeySuper ? "SUPER " : "");
  5663. ImGui::Text("Chars queue:"); for (int i = 0; i < io.InputQueueCharacters.Size; i++) { ImWchar c = io.InputQueueCharacters[i]; ImGui::SameLine(); ImGui::Text("\'%c\' (0x%04X)", (c > ' ' && c <= 255) ? (char)c : '?', c); } // FIXME: We should convert 'c' to UTF-8 here but the functions are not public.
  5664. ImGui::TreePop();
  5665. }
  5666. // Display ImGuiIO output flags
  5667. IMGUI_DEMO_MARKER("Inputs & Focus/Outputs");
  5668. ImGui::SetNextItemOpen(true, ImGuiCond_Once);
  5669. bool outputs_opened = ImGui::TreeNode("Outputs");
  5670. ImGui::SameLine();
  5671. HelpMarker(
  5672. "The value of io.WantCaptureMouse and io.WantCaptureKeyboard are normally set by Dear ImGui "
  5673. "to instruct your application of how to route inputs. Typically, when a value is true, it means "
  5674. "Dear ImGui wants the corresponding inputs and we expect the underlying application to ignore them.\n\n"
  5675. "The most typical case is: when hovering a window, Dear ImGui set io.WantCaptureMouse to true, "
  5676. "and underlying application should ignore mouse inputs (in practice there are many and more subtle "
  5677. "rules leading to how those flags are set).");
  5678. if (outputs_opened)
  5679. {
  5680. ImGui::Text("io.WantCaptureMouse: %d", io.WantCaptureMouse);
  5681. ImGui::Text("io.WantCaptureMouseUnlessPopupClose: %d", io.WantCaptureMouseUnlessPopupClose);
  5682. ImGui::Text("io.WantCaptureKeyboard: %d", io.WantCaptureKeyboard);
  5683. ImGui::Text("io.WantTextInput: %d", io.WantTextInput);
  5684. ImGui::Text("io.WantSetMousePos: %d", io.WantSetMousePos);
  5685. ImGui::Text("io.NavActive: %d, io.NavVisible: %d", io.NavActive, io.NavVisible);
  5686. IMGUI_DEMO_MARKER("Inputs & Focus/Outputs/WantCapture override");
  5687. if (ImGui::TreeNode("WantCapture override"))
  5688. {
  5689. HelpMarker(
  5690. "Hovering the colored canvas will override io.WantCaptureXXX fields.\n"
  5691. "Notice how normally (when set to none), the value of io.WantCaptureKeyboard would be false when hovering "
  5692. "and true when clicking.");
  5693. static int capture_override_mouse = -1;
  5694. static int capture_override_keyboard = -1;
  5695. const char* capture_override_desc[] = { "None", "Set to false", "Set to true" };
  5696. ImGui::SetNextItemWidth(ImGui::GetFontSize() * 15);
  5697. ImGui::SliderInt("SetNextFrameWantCaptureMouse() on hover", &capture_override_mouse, -1, +1, capture_override_desc[capture_override_mouse + 1], ImGuiSliderFlags_AlwaysClamp);
  5698. ImGui::SetNextItemWidth(ImGui::GetFontSize() * 15);
  5699. ImGui::SliderInt("SetNextFrameWantCaptureKeyboard() on hover", &capture_override_keyboard, -1, +1, capture_override_desc[capture_override_keyboard + 1], ImGuiSliderFlags_AlwaysClamp);
  5700. ImGui::ColorButton("##panel", ImVec4(0.7f, 0.1f, 0.7f, 1.0f), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoDragDrop, ImVec2(128.0f, 96.0f)); // Dummy item
  5701. if (ImGui::IsItemHovered() && capture_override_mouse != -1)
  5702. ImGui::SetNextFrameWantCaptureMouse(capture_override_mouse == 1);
  5703. if (ImGui::IsItemHovered() && capture_override_keyboard != -1)
  5704. ImGui::SetNextFrameWantCaptureKeyboard(capture_override_keyboard == 1);
  5705. ImGui::TreePop();
  5706. }
  5707. ImGui::TreePop();
  5708. }
  5709. // Demonstrate using Shortcut() and Routing Policies.
  5710. // The general flow is:
  5711. // - Code interested in a chord (e.g. "Ctrl+A") declares their intent.
  5712. // - Multiple locations may be interested in same chord! Routing helps find a winner.
  5713. // - Every frame, we resolve all claims and assign one owner if the modifiers are matching.
  5714. // - The lower-level function is 'bool SetShortcutRouting()', returns true when caller got the route.
  5715. // - Most of the times, SetShortcutRouting() is not called directly. User mostly calls Shortcut() with routing flags.
  5716. // - If you call Shortcut() WITHOUT any routing option, it uses ImGuiInputFlags_RouteFocused.
  5717. // TL;DR: Most uses will simply be:
  5718. // - Shortcut(ImGuiMod_Ctrl | ImGuiKey_A); // Use ImGuiInputFlags_RouteFocused policy.
  5719. IMGUI_DEMO_MARKER("Inputs & Focus/Shortcuts");
  5720. if (ImGui::TreeNode("Shortcuts"))
  5721. {
  5722. static ImGuiInputFlags route_options = ImGuiInputFlags_Repeat;
  5723. static ImGuiInputFlags route_type = ImGuiInputFlags_RouteFocused;
  5724. ImGui::CheckboxFlags("ImGuiInputFlags_Repeat", &route_options, ImGuiInputFlags_Repeat);
  5725. ImGui::RadioButton("ImGuiInputFlags_RouteActive", &route_type, ImGuiInputFlags_RouteActive);
  5726. ImGui::RadioButton("ImGuiInputFlags_RouteFocused (default)", &route_type, ImGuiInputFlags_RouteFocused);
  5727. ImGui::RadioButton("ImGuiInputFlags_RouteGlobal", &route_type, ImGuiInputFlags_RouteGlobal);
  5728. ImGui::Indent();
  5729. ImGui::BeginDisabled(route_type != ImGuiInputFlags_RouteGlobal);
  5730. ImGui::CheckboxFlags("ImGuiInputFlags_RouteOverFocused", &route_options, ImGuiInputFlags_RouteOverFocused);
  5731. ImGui::CheckboxFlags("ImGuiInputFlags_RouteOverActive", &route_options, ImGuiInputFlags_RouteOverActive);
  5732. ImGui::CheckboxFlags("ImGuiInputFlags_RouteUnlessBgFocused", &route_options, ImGuiInputFlags_RouteUnlessBgFocused);
  5733. ImGui::EndDisabled();
  5734. ImGui::Unindent();
  5735. ImGui::RadioButton("ImGuiInputFlags_RouteAlways", &route_type, ImGuiInputFlags_RouteAlways);
  5736. ImGuiInputFlags flags = route_type | route_options; // Merged flags
  5737. if (route_type != ImGuiInputFlags_RouteGlobal)
  5738. flags &= ~(ImGuiInputFlags_RouteOverFocused | ImGuiInputFlags_RouteOverActive | ImGuiInputFlags_RouteUnlessBgFocused);
  5739. ImGui::SeparatorText("Using SetNextItemShortcut()");
  5740. ImGui::Text("Ctrl+S");
  5741. ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_S, flags | ImGuiInputFlags_Tooltip);
  5742. ImGui::Button("Save");
  5743. ImGui::Text("Alt+F");
  5744. ImGui::SetNextItemShortcut(ImGuiMod_Alt | ImGuiKey_F, flags | ImGuiInputFlags_Tooltip);
  5745. static float f = 0.5f;
  5746. ImGui::SliderFloat("Factor", &f, 0.0f, 1.0f);
  5747. ImGui::SeparatorText("Using Shortcut()");
  5748. const float line_height = ImGui::GetTextLineHeightWithSpacing();
  5749. const ImGuiKeyChord key_chord = ImGuiMod_Ctrl | ImGuiKey_A;
  5750. ImGui::Text("Ctrl+A");
  5751. ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, flags) ? "PRESSED" : "...");
  5752. ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 0.0f, 1.0f, 0.1f));
  5753. ImGui::BeginChild("WindowA", ImVec2(-FLT_MIN, line_height * 14), true);
  5754. ImGui::Text("Press CTRL+A and see who receives it!");
  5755. ImGui::Separator();
  5756. // 1: Window polling for CTRL+A
  5757. ImGui::Text("(in WindowA)");
  5758. ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, flags) ? "PRESSED" : "...");
  5759. // 2: InputText also polling for CTRL+A: it always uses _RouteFocused internally (gets priority when active)
  5760. // (Commmented because the owner-aware version of Shortcut() is still in imgui_internal.h)
  5761. //char str[16] = "Press CTRL+A";
  5762. //ImGui::Spacing();
  5763. //ImGui::InputText("InputTextB", str, IM_ARRAYSIZE(str), ImGuiInputTextFlags_ReadOnly);
  5764. //ImGuiID item_id = ImGui::GetItemID();
  5765. //ImGui::SameLine(); HelpMarker("Internal widgets always use _RouteFocused");
  5766. //ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, flags, item_id) ? "PRESSED" : "...");
  5767. // 3: Dummy child is not claiming the route: focusing them shouldn't steal route away from WindowA
  5768. ImGui::BeginChild("ChildD", ImVec2(-FLT_MIN, line_height * 4), true);
  5769. ImGui::Text("(in ChildD: not using same Shortcut)");
  5770. ImGui::Text("IsWindowFocused: %d", ImGui::IsWindowFocused());
  5771. ImGui::EndChild();
  5772. // 4: Child window polling for CTRL+A. It is deeper than WindowA and gets priority when focused.
  5773. ImGui::BeginChild("ChildE", ImVec2(-FLT_MIN, line_height * 4), true);
  5774. ImGui::Text("(in ChildE: using same Shortcut)");
  5775. ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, flags) ? "PRESSED" : "...");
  5776. ImGui::EndChild();
  5777. // 5: In a popup
  5778. if (ImGui::Button("Open Popup"))
  5779. ImGui::OpenPopup("PopupF");
  5780. if (ImGui::BeginPopup("PopupF"))
  5781. {
  5782. ImGui::Text("(in PopupF)");
  5783. ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, flags) ? "PRESSED" : "...");
  5784. // (Commmented because the owner-aware version of Shortcut() is still in imgui_internal.h)
  5785. //ImGui::InputText("InputTextG", str, IM_ARRAYSIZE(str), ImGuiInputTextFlags_ReadOnly);
  5786. //ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, flags, ImGui::GetItemID()) ? "PRESSED" : "...");
  5787. ImGui::EndPopup();
  5788. }
  5789. ImGui::EndChild();
  5790. ImGui::PopStyleColor();
  5791. ImGui::TreePop();
  5792. }
  5793. // Display mouse cursors
  5794. IMGUI_DEMO_MARKER("Inputs & Focus/Mouse Cursors");
  5795. if (ImGui::TreeNode("Mouse Cursors"))
  5796. {
  5797. const char* mouse_cursors_names[] = { "Arrow", "TextInput", "ResizeAll", "ResizeNS", "ResizeEW", "ResizeNESW", "ResizeNWSE", "Hand", "NotAllowed" };
  5798. IM_ASSERT(IM_ARRAYSIZE(mouse_cursors_names) == ImGuiMouseCursor_COUNT);
  5799. ImGuiMouseCursor current = ImGui::GetMouseCursor();
  5800. ImGui::Text("Current mouse cursor = %d: %s", current, mouse_cursors_names[current]);
  5801. ImGui::BeginDisabled(true);
  5802. ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", &io.BackendFlags, ImGuiBackendFlags_HasMouseCursors);
  5803. ImGui::EndDisabled();
  5804. ImGui::Text("Hover to see mouse cursors:");
  5805. ImGui::SameLine(); HelpMarker(
  5806. "Your application can render a different mouse cursor based on what ImGui::GetMouseCursor() returns. "
  5807. "If software cursor rendering (io.MouseDrawCursor) is set ImGui will draw the right cursor for you, "
  5808. "otherwise your backend needs to handle it.");
  5809. for (int i = 0; i < ImGuiMouseCursor_COUNT; i++)
  5810. {
  5811. char label[32];
  5812. sprintf(label, "Mouse cursor %d: %s", i, mouse_cursors_names[i]);
  5813. ImGui::Bullet(); ImGui::Selectable(label, false);
  5814. if (ImGui::IsItemHovered())
  5815. ImGui::SetMouseCursor(i);
  5816. }
  5817. ImGui::TreePop();
  5818. }
  5819. IMGUI_DEMO_MARKER("Inputs & Focus/Tabbing");
  5820. if (ImGui::TreeNode("Tabbing"))
  5821. {
  5822. ImGui::Text("Use TAB/SHIFT+TAB to cycle through keyboard editable fields.");
  5823. static char buf[32] = "hello";
  5824. ImGui::InputText("1", buf, IM_ARRAYSIZE(buf));
  5825. ImGui::InputText("2", buf, IM_ARRAYSIZE(buf));
  5826. ImGui::InputText("3", buf, IM_ARRAYSIZE(buf));
  5827. ImGui::PushTabStop(false);
  5828. ImGui::InputText("4 (tab skip)", buf, IM_ARRAYSIZE(buf));
  5829. ImGui::SameLine(); HelpMarker("Item won't be cycled through when using TAB or Shift+Tab.");
  5830. ImGui::PopTabStop();
  5831. ImGui::InputText("5", buf, IM_ARRAYSIZE(buf));
  5832. ImGui::TreePop();
  5833. }
  5834. IMGUI_DEMO_MARKER("Inputs & Focus/Focus from code");
  5835. if (ImGui::TreeNode("Focus from code"))
  5836. {
  5837. bool focus_1 = ImGui::Button("Focus on 1"); ImGui::SameLine();
  5838. bool focus_2 = ImGui::Button("Focus on 2"); ImGui::SameLine();
  5839. bool focus_3 = ImGui::Button("Focus on 3");
  5840. int has_focus = 0;
  5841. static char buf[128] = "click on a button to set focus";
  5842. if (focus_1) ImGui::SetKeyboardFocusHere();
  5843. ImGui::InputText("1", buf, IM_ARRAYSIZE(buf));
  5844. if (ImGui::IsItemActive()) has_focus = 1;
  5845. if (focus_2) ImGui::SetKeyboardFocusHere();
  5846. ImGui::InputText("2", buf, IM_ARRAYSIZE(buf));
  5847. if (ImGui::IsItemActive()) has_focus = 2;
  5848. ImGui::PushTabStop(false);
  5849. if (focus_3) ImGui::SetKeyboardFocusHere();
  5850. ImGui::InputText("3 (tab skip)", buf, IM_ARRAYSIZE(buf));
  5851. if (ImGui::IsItemActive()) has_focus = 3;
  5852. ImGui::SameLine(); HelpMarker("Item won't be cycled through when using TAB or Shift+Tab.");
  5853. ImGui::PopTabStop();
  5854. if (has_focus)
  5855. ImGui::Text("Item with focus: %d", has_focus);
  5856. else
  5857. ImGui::Text("Item with focus: <none>");
  5858. // Use >= 0 parameter to SetKeyboardFocusHere() to focus an upcoming item
  5859. static float f3[3] = { 0.0f, 0.0f, 0.0f };
  5860. int focus_ahead = -1;
  5861. if (ImGui::Button("Focus on X")) { focus_ahead = 0; } ImGui::SameLine();
  5862. if (ImGui::Button("Focus on Y")) { focus_ahead = 1; } ImGui::SameLine();
  5863. if (ImGui::Button("Focus on Z")) { focus_ahead = 2; }
  5864. if (focus_ahead != -1) ImGui::SetKeyboardFocusHere(focus_ahead);
  5865. ImGui::SliderFloat3("Float3", &f3[0], 0.0f, 1.0f);
  5866. ImGui::TextWrapped("NB: Cursor & selection are preserved when refocusing last used item in code.");
  5867. ImGui::TreePop();
  5868. }
  5869. IMGUI_DEMO_MARKER("Inputs & Focus/Dragging");
  5870. if (ImGui::TreeNode("Dragging"))
  5871. {
  5872. ImGui::TextWrapped("You can use ImGui::GetMouseDragDelta(0) to query for the dragged amount on any widget.");
  5873. for (int button = 0; button < 3; button++)
  5874. {
  5875. ImGui::Text("IsMouseDragging(%d):", button);
  5876. ImGui::Text(" w/ default threshold: %d,", ImGui::IsMouseDragging(button));
  5877. ImGui::Text(" w/ zero threshold: %d,", ImGui::IsMouseDragging(button, 0.0f));
  5878. ImGui::Text(" w/ large threshold: %d,", ImGui::IsMouseDragging(button, 20.0f));
  5879. }
  5880. ImGui::Button("Drag Me");
  5881. if (ImGui::IsItemActive())
  5882. ImGui::GetForegroundDrawList()->AddLine(io.MouseClickedPos[0], io.MousePos, ImGui::GetColorU32(ImGuiCol_Button), 4.0f); // Draw a line between the button and the mouse cursor
  5883. // Drag operations gets "unlocked" when the mouse has moved past a certain threshold
  5884. // (the default threshold is stored in io.MouseDragThreshold). You can request a lower or higher
  5885. // threshold using the second parameter of IsMouseDragging() and GetMouseDragDelta().
  5886. ImVec2 value_raw = ImGui::GetMouseDragDelta(0, 0.0f);
  5887. ImVec2 value_with_lock_threshold = ImGui::GetMouseDragDelta(0);
  5888. ImVec2 mouse_delta = io.MouseDelta;
  5889. ImGui::Text("GetMouseDragDelta(0):");
  5890. ImGui::Text(" w/ default threshold: (%.1f, %.1f)", value_with_lock_threshold.x, value_with_lock_threshold.y);
  5891. ImGui::Text(" w/ zero threshold: (%.1f, %.1f)", value_raw.x, value_raw.y);
  5892. ImGui::Text("io.MouseDelta: (%.1f, %.1f)", mouse_delta.x, mouse_delta.y);
  5893. ImGui::TreePop();
  5894. }
  5895. }
  5896. }
  5897. //-----------------------------------------------------------------------------
  5898. // [SECTION] About Window / ShowAboutWindow()
  5899. // Access from Dear ImGui Demo -> Tools -> About
  5900. //-----------------------------------------------------------------------------
  5901. void ImGui::ShowAboutWindow(bool* p_open)
  5902. {
  5903. if (!ImGui::Begin("About Dear ImGui", p_open, ImGuiWindowFlags_AlwaysAutoResize))
  5904. {
  5905. ImGui::End();
  5906. return;
  5907. }
  5908. IMGUI_DEMO_MARKER("Tools/About Dear ImGui");
  5909. ImGui::Text("Dear ImGui %s (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM);
  5910. ImGui::Separator();
  5911. ImGui::Text("By Omar Cornut and all Dear ImGui contributors.");
  5912. ImGui::Text("Dear ImGui is licensed under the MIT License, see LICENSE for more information.");
  5913. ImGui::Text("If your company uses this, please consider funding the project.");
  5914. static bool show_config_info = false;
  5915. ImGui::Checkbox("Config/Build Information", &show_config_info);
  5916. if (show_config_info)
  5917. {
  5918. ImGuiIO& io = ImGui::GetIO();
  5919. ImGuiStyle& style = ImGui::GetStyle();
  5920. bool copy_to_clipboard = ImGui::Button("Copy to clipboard");
  5921. ImVec2 child_size = ImVec2(0, ImGui::GetTextLineHeightWithSpacing() * 18);
  5922. ImGui::BeginChild(ImGui::GetID("cfg_infos"), child_size, ImGuiChildFlags_FrameStyle);
  5923. if (copy_to_clipboard)
  5924. {
  5925. ImGui::LogToClipboard();
  5926. ImGui::LogText("```\n"); // Back quotes will make text appears without formatting when pasting on GitHub
  5927. }
  5928. ImGui::Text("Dear ImGui %s (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM);
  5929. ImGui::Separator();
  5930. ImGui::Text("sizeof(size_t): %d, sizeof(ImDrawIdx): %d, sizeof(ImDrawVert): %d", (int)sizeof(size_t), (int)sizeof(ImDrawIdx), (int)sizeof(ImDrawVert));
  5931. ImGui::Text("define: __cplusplus=%d", (int)__cplusplus);
  5932. #ifdef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
  5933. ImGui::Text("define: IMGUI_DISABLE_OBSOLETE_FUNCTIONS");
  5934. #endif
  5935. #ifdef IMGUI_DISABLE_OBSOLETE_KEYIO
  5936. ImGui::Text("define: IMGUI_DISABLE_OBSOLETE_KEYIO");
  5937. #endif
  5938. #ifdef IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS
  5939. ImGui::Text("define: IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS");
  5940. #endif
  5941. #ifdef IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS
  5942. ImGui::Text("define: IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS");
  5943. #endif
  5944. #ifdef IMGUI_DISABLE_WIN32_FUNCTIONS
  5945. ImGui::Text("define: IMGUI_DISABLE_WIN32_FUNCTIONS");
  5946. #endif
  5947. #ifdef IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS
  5948. ImGui::Text("define: IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS");
  5949. #endif
  5950. #ifdef IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS
  5951. ImGui::Text("define: IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS");
  5952. #endif
  5953. #ifdef IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS
  5954. ImGui::Text("define: IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS");
  5955. #endif
  5956. #ifdef IMGUI_DISABLE_FILE_FUNCTIONS
  5957. ImGui::Text("define: IMGUI_DISABLE_FILE_FUNCTIONS");
  5958. #endif
  5959. #ifdef IMGUI_DISABLE_DEFAULT_ALLOCATORS
  5960. ImGui::Text("define: IMGUI_DISABLE_DEFAULT_ALLOCATORS");
  5961. #endif
  5962. #ifdef IMGUI_USE_BGRA_PACKED_COLOR
  5963. ImGui::Text("define: IMGUI_USE_BGRA_PACKED_COLOR");
  5964. #endif
  5965. #ifdef _WIN32
  5966. ImGui::Text("define: _WIN32");
  5967. #endif
  5968. #ifdef _WIN64
  5969. ImGui::Text("define: _WIN64");
  5970. #endif
  5971. #ifdef __linux__
  5972. ImGui::Text("define: __linux__");
  5973. #endif
  5974. #ifdef __APPLE__
  5975. ImGui::Text("define: __APPLE__");
  5976. #endif
  5977. #ifdef _MSC_VER
  5978. ImGui::Text("define: _MSC_VER=%d", _MSC_VER);
  5979. #endif
  5980. #ifdef _MSVC_LANG
  5981. ImGui::Text("define: _MSVC_LANG=%d", (int)_MSVC_LANG);
  5982. #endif
  5983. #ifdef __MINGW32__
  5984. ImGui::Text("define: __MINGW32__");
  5985. #endif
  5986. #ifdef __MINGW64__
  5987. ImGui::Text("define: __MINGW64__");
  5988. #endif
  5989. #ifdef __GNUC__
  5990. ImGui::Text("define: __GNUC__=%d", (int)__GNUC__);
  5991. #endif
  5992. #ifdef __clang_version__
  5993. ImGui::Text("define: __clang_version__=%s", __clang_version__);
  5994. #endif
  5995. #ifdef __EMSCRIPTEN__
  5996. ImGui::Text("define: __EMSCRIPTEN__");
  5997. #endif
  5998. ImGui::Separator();
  5999. ImGui::Text("io.BackendPlatformName: %s", io.BackendPlatformName ? io.BackendPlatformName : "NULL");
  6000. ImGui::Text("io.BackendRendererName: %s", io.BackendRendererName ? io.BackendRendererName : "NULL");
  6001. ImGui::Text("io.ConfigFlags: 0x%08X", io.ConfigFlags);
  6002. if (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) ImGui::Text(" NavEnableKeyboard");
  6003. if (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) ImGui::Text(" NavEnableGamepad");
  6004. if (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) ImGui::Text(" NavEnableSetMousePos");
  6005. if (io.ConfigFlags & ImGuiConfigFlags_NavNoCaptureKeyboard) ImGui::Text(" NavNoCaptureKeyboard");
  6006. if (io.ConfigFlags & ImGuiConfigFlags_NoMouse) ImGui::Text(" NoMouse");
  6007. if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) ImGui::Text(" NoMouseCursorChange");
  6008. if (io.MouseDrawCursor) ImGui::Text("io.MouseDrawCursor");
  6009. if (io.ConfigMacOSXBehaviors) ImGui::Text("io.ConfigMacOSXBehaviors");
  6010. if (io.ConfigInputTextCursorBlink) ImGui::Text("io.ConfigInputTextCursorBlink");
  6011. if (io.ConfigWindowsResizeFromEdges) ImGui::Text("io.ConfigWindowsResizeFromEdges");
  6012. if (io.ConfigWindowsMoveFromTitleBarOnly) ImGui::Text("io.ConfigWindowsMoveFromTitleBarOnly");
  6013. if (io.ConfigMemoryCompactTimer >= 0.0f) ImGui::Text("io.ConfigMemoryCompactTimer = %.1f", io.ConfigMemoryCompactTimer);
  6014. ImGui::Text("io.BackendFlags: 0x%08X", io.BackendFlags);
  6015. if (io.BackendFlags & ImGuiBackendFlags_HasGamepad) ImGui::Text(" HasGamepad");
  6016. if (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) ImGui::Text(" HasMouseCursors");
  6017. if (io.BackendFlags & ImGuiBackendFlags_HasSetMousePos) ImGui::Text(" HasSetMousePos");
  6018. if (io.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) ImGui::Text(" RendererHasVtxOffset");
  6019. ImGui::Separator();
  6020. ImGui::Text("io.Fonts: %d fonts, Flags: 0x%08X, TexSize: %d,%d", io.Fonts->Fonts.Size, io.Fonts->Flags, io.Fonts->TexWidth, io.Fonts->TexHeight);
  6021. ImGui::Text("io.DisplaySize: %.2f,%.2f", io.DisplaySize.x, io.DisplaySize.y);
  6022. ImGui::Text("io.DisplayFramebufferScale: %.2f,%.2f", io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
  6023. ImGui::Separator();
  6024. ImGui::Text("style.WindowPadding: %.2f,%.2f", style.WindowPadding.x, style.WindowPadding.y);
  6025. ImGui::Text("style.WindowBorderSize: %.2f", style.WindowBorderSize);
  6026. ImGui::Text("style.FramePadding: %.2f,%.2f", style.FramePadding.x, style.FramePadding.y);
  6027. ImGui::Text("style.FrameRounding: %.2f", style.FrameRounding);
  6028. ImGui::Text("style.FrameBorderSize: %.2f", style.FrameBorderSize);
  6029. ImGui::Text("style.ItemSpacing: %.2f,%.2f", style.ItemSpacing.x, style.ItemSpacing.y);
  6030. ImGui::Text("style.ItemInnerSpacing: %.2f,%.2f", style.ItemInnerSpacing.x, style.ItemInnerSpacing.y);
  6031. if (copy_to_clipboard)
  6032. {
  6033. ImGui::LogText("\n```\n");
  6034. ImGui::LogFinish();
  6035. }
  6036. ImGui::EndChild();
  6037. }
  6038. ImGui::End();
  6039. }
  6040. //-----------------------------------------------------------------------------
  6041. // [SECTION] Style Editor / ShowStyleEditor()
  6042. //-----------------------------------------------------------------------------
  6043. // - ShowFontSelector()
  6044. // - ShowStyleSelector()
  6045. // - ShowStyleEditor()
  6046. //-----------------------------------------------------------------------------
  6047. // Forward declare ShowFontAtlas() which isn't worth putting in public API yet
  6048. namespace ImGui { IMGUI_API void ShowFontAtlas(ImFontAtlas* atlas); }
  6049. // Demo helper function to select among loaded fonts.
  6050. // Here we use the regular BeginCombo()/EndCombo() api which is the more flexible one.
  6051. void ImGui::ShowFontSelector(const char* label)
  6052. {
  6053. ImGuiIO& io = ImGui::GetIO();
  6054. ImFont* font_current = ImGui::GetFont();
  6055. if (ImGui::BeginCombo(label, font_current->GetDebugName()))
  6056. {
  6057. for (ImFont* font : io.Fonts->Fonts)
  6058. {
  6059. ImGui::PushID((void*)font);
  6060. if (ImGui::Selectable(font->GetDebugName(), font == font_current))
  6061. io.FontDefault = font;
  6062. ImGui::PopID();
  6063. }
  6064. ImGui::EndCombo();
  6065. }
  6066. ImGui::SameLine();
  6067. HelpMarker(
  6068. "- Load additional fonts with io.Fonts->AddFontFromFileTTF().\n"
  6069. "- The font atlas is built when calling io.Fonts->GetTexDataAsXXXX() or io.Fonts->Build().\n"
  6070. "- Read FAQ and docs/FONTS.md for more details.\n"
  6071. "- If you need to add/remove fonts at runtime (e.g. for DPI change), do it before calling NewFrame().");
  6072. }
  6073. // Demo helper function to select among default colors. See ShowStyleEditor() for more advanced options.
  6074. // Here we use the simplified Combo() api that packs items into a single literal string.
  6075. // Useful for quick combo boxes where the choices are known locally.
  6076. bool ImGui::ShowStyleSelector(const char* label)
  6077. {
  6078. static int style_idx = -1;
  6079. if (ImGui::Combo(label, &style_idx, "Dark\0Light\0Classic\0"))
  6080. {
  6081. switch (style_idx)
  6082. {
  6083. case 0: ImGui::StyleColorsDark(); break;
  6084. case 1: ImGui::StyleColorsLight(); break;
  6085. case 2: ImGui::StyleColorsClassic(); break;
  6086. }
  6087. return true;
  6088. }
  6089. return false;
  6090. }
  6091. void ImGui::ShowStyleEditor(ImGuiStyle* ref)
  6092. {
  6093. IMGUI_DEMO_MARKER("Tools/Style Editor");
  6094. // You can pass in a reference ImGuiStyle structure to compare to, revert to and save to
  6095. // (without a reference style pointer, we will use one compared locally as a reference)
  6096. ImGuiStyle& style = ImGui::GetStyle();
  6097. static ImGuiStyle ref_saved_style;
  6098. // Default to using internal storage as reference
  6099. static bool init = true;
  6100. if (init && ref == NULL)
  6101. ref_saved_style = style;
  6102. init = false;
  6103. if (ref == NULL)
  6104. ref = &ref_saved_style;
  6105. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.50f);
  6106. if (ImGui::ShowStyleSelector("Colors##Selector"))
  6107. ref_saved_style = style;
  6108. ImGui::ShowFontSelector("Fonts##Selector");
  6109. // Simplified Settings (expose floating-pointer border sizes as boolean representing 0.0f or 1.0f)
  6110. if (ImGui::SliderFloat("FrameRounding", &style.FrameRounding, 0.0f, 12.0f, "%.0f"))
  6111. style.GrabRounding = style.FrameRounding; // Make GrabRounding always the same value as FrameRounding
  6112. { bool border = (style.WindowBorderSize > 0.0f); if (ImGui::Checkbox("WindowBorder", &border)) { style.WindowBorderSize = border ? 1.0f : 0.0f; } }
  6113. ImGui::SameLine();
  6114. { bool border = (style.FrameBorderSize > 0.0f); if (ImGui::Checkbox("FrameBorder", &border)) { style.FrameBorderSize = border ? 1.0f : 0.0f; } }
  6115. ImGui::SameLine();
  6116. { bool border = (style.PopupBorderSize > 0.0f); if (ImGui::Checkbox("PopupBorder", &border)) { style.PopupBorderSize = border ? 1.0f : 0.0f; } }
  6117. // Save/Revert button
  6118. if (ImGui::Button("Save Ref"))
  6119. *ref = ref_saved_style = style;
  6120. ImGui::SameLine();
  6121. if (ImGui::Button("Revert Ref"))
  6122. style = *ref;
  6123. ImGui::SameLine();
  6124. HelpMarker(
  6125. "Save/Revert in local non-persistent storage. Default Colors definition are not affected. "
  6126. "Use \"Export\" below to save them somewhere.");
  6127. ImGui::Separator();
  6128. if (ImGui::BeginTabBar("##tabs", ImGuiTabBarFlags_None))
  6129. {
  6130. if (ImGui::BeginTabItem("Sizes"))
  6131. {
  6132. ImGui::SeparatorText("Main");
  6133. ImGui::SliderFloat2("WindowPadding", (float*)&style.WindowPadding, 0.0f, 20.0f, "%.0f");
  6134. ImGui::SliderFloat2("FramePadding", (float*)&style.FramePadding, 0.0f, 20.0f, "%.0f");
  6135. ImGui::SliderFloat2("ItemSpacing", (float*)&style.ItemSpacing, 0.0f, 20.0f, "%.0f");
  6136. ImGui::SliderFloat2("ItemInnerSpacing", (float*)&style.ItemInnerSpacing, 0.0f, 20.0f, "%.0f");
  6137. ImGui::SliderFloat2("TouchExtraPadding", (float*)&style.TouchExtraPadding, 0.0f, 10.0f, "%.0f");
  6138. ImGui::SliderFloat("IndentSpacing", &style.IndentSpacing, 0.0f, 30.0f, "%.0f");
  6139. ImGui::SliderFloat("ScrollbarSize", &style.ScrollbarSize, 1.0f, 20.0f, "%.0f");
  6140. ImGui::SliderFloat("GrabMinSize", &style.GrabMinSize, 1.0f, 20.0f, "%.0f");
  6141. ImGui::SeparatorText("Borders");
  6142. ImGui::SliderFloat("WindowBorderSize", &style.WindowBorderSize, 0.0f, 1.0f, "%.0f");
  6143. ImGui::SliderFloat("ChildBorderSize", &style.ChildBorderSize, 0.0f, 1.0f, "%.0f");
  6144. ImGui::SliderFloat("PopupBorderSize", &style.PopupBorderSize, 0.0f, 1.0f, "%.0f");
  6145. ImGui::SliderFloat("FrameBorderSize", &style.FrameBorderSize, 0.0f, 1.0f, "%.0f");
  6146. ImGui::SliderFloat("TabBorderSize", &style.TabBorderSize, 0.0f, 1.0f, "%.0f");
  6147. ImGui::SliderFloat("TabBarBorderSize", &style.TabBarBorderSize, 0.0f, 2.0f, "%.0f");
  6148. ImGui::SeparatorText("Rounding");
  6149. ImGui::SliderFloat("WindowRounding", &style.WindowRounding, 0.0f, 12.0f, "%.0f");
  6150. ImGui::SliderFloat("ChildRounding", &style.ChildRounding, 0.0f, 12.0f, "%.0f");
  6151. ImGui::SliderFloat("FrameRounding", &style.FrameRounding, 0.0f, 12.0f, "%.0f");
  6152. ImGui::SliderFloat("PopupRounding", &style.PopupRounding, 0.0f, 12.0f, "%.0f");
  6153. ImGui::SliderFloat("ScrollbarRounding", &style.ScrollbarRounding, 0.0f, 12.0f, "%.0f");
  6154. ImGui::SliderFloat("GrabRounding", &style.GrabRounding, 0.0f, 12.0f, "%.0f");
  6155. ImGui::SliderFloat("TabRounding", &style.TabRounding, 0.0f, 12.0f, "%.0f");
  6156. ImGui::SeparatorText("Tables");
  6157. ImGui::SliderFloat2("CellPadding", (float*)&style.CellPadding, 0.0f, 20.0f, "%.0f");
  6158. ImGui::SliderAngle("TableAngledHeadersAngle", &style.TableAngledHeadersAngle, -50.0f, +50.0f);
  6159. ImGui::SliderFloat2("TableAngledHeadersTextAlign", (float*)&style.TableAngledHeadersTextAlign, 0.0f, 1.0f, "%.2f");
  6160. ImGui::SeparatorText("Widgets");
  6161. ImGui::SliderFloat2("WindowTitleAlign", (float*)&style.WindowTitleAlign, 0.0f, 1.0f, "%.2f");
  6162. int window_menu_button_position = style.WindowMenuButtonPosition + 1;
  6163. if (ImGui::Combo("WindowMenuButtonPosition", (int*)&window_menu_button_position, "None\0Left\0Right\0"))
  6164. style.WindowMenuButtonPosition = (ImGuiDir)(window_menu_button_position - 1);
  6165. ImGui::Combo("ColorButtonPosition", (int*)&style.ColorButtonPosition, "Left\0Right\0");
  6166. ImGui::SliderFloat2("ButtonTextAlign", (float*)&style.ButtonTextAlign, 0.0f, 1.0f, "%.2f");
  6167. ImGui::SameLine(); HelpMarker("Alignment applies when a button is larger than its text content.");
  6168. ImGui::SliderFloat2("SelectableTextAlign", (float*)&style.SelectableTextAlign, 0.0f, 1.0f, "%.2f");
  6169. ImGui::SameLine(); HelpMarker("Alignment applies when a selectable is larger than its text content.");
  6170. ImGui::SliderFloat("SeparatorTextBorderSize", &style.SeparatorTextBorderSize, 0.0f, 10.0f, "%.0f");
  6171. ImGui::SliderFloat2("SeparatorTextAlign", (float*)&style.SeparatorTextAlign, 0.0f, 1.0f, "%.2f");
  6172. ImGui::SliderFloat2("SeparatorTextPadding", (float*)&style.SeparatorTextPadding, 0.0f, 40.0f, "%.0f");
  6173. ImGui::SliderFloat("LogSliderDeadzone", &style.LogSliderDeadzone, 0.0f, 12.0f, "%.0f");
  6174. ImGui::SeparatorText("Tooltips");
  6175. for (int n = 0; n < 2; n++)
  6176. if (ImGui::TreeNodeEx(n == 0 ? "HoverFlagsForTooltipMouse" : "HoverFlagsForTooltipNav"))
  6177. {
  6178. ImGuiHoveredFlags* p = (n == 0) ? &style.HoverFlagsForTooltipMouse : &style.HoverFlagsForTooltipNav;
  6179. ImGui::CheckboxFlags("ImGuiHoveredFlags_DelayNone", p, ImGuiHoveredFlags_DelayNone);
  6180. ImGui::CheckboxFlags("ImGuiHoveredFlags_DelayShort", p, ImGuiHoveredFlags_DelayShort);
  6181. ImGui::CheckboxFlags("ImGuiHoveredFlags_DelayNormal", p, ImGuiHoveredFlags_DelayNormal);
  6182. ImGui::CheckboxFlags("ImGuiHoveredFlags_Stationary", p, ImGuiHoveredFlags_Stationary);
  6183. ImGui::CheckboxFlags("ImGuiHoveredFlags_NoSharedDelay", p, ImGuiHoveredFlags_NoSharedDelay);
  6184. ImGui::TreePop();
  6185. }
  6186. ImGui::SeparatorText("Misc");
  6187. ImGui::SliderFloat2("DisplayWindowPadding", (float*)&style.DisplayWindowPadding, 0.0f, 30.0f, "%.0f"); ImGui::SameLine(); HelpMarker("Apply to regular windows: amount which we enforce to keep visible when moving near edges of your screen.");
  6188. ImGui::SliderFloat2("DisplaySafeAreaPadding", (float*)&style.DisplaySafeAreaPadding, 0.0f, 30.0f, "%.0f"); ImGui::SameLine(); HelpMarker("Apply to every windows, menus, popups, tooltips: amount where we avoid displaying contents. Adjust if you cannot see the edges of your screen (e.g. on a TV where scaling has not been configured).");
  6189. ImGui::EndTabItem();
  6190. }
  6191. if (ImGui::BeginTabItem("Colors"))
  6192. {
  6193. static int output_dest = 0;
  6194. static bool output_only_modified = true;
  6195. if (ImGui::Button("Export"))
  6196. {
  6197. if (output_dest == 0)
  6198. ImGui::LogToClipboard();
  6199. else
  6200. ImGui::LogToTTY();
  6201. ImGui::LogText("ImVec4* colors = ImGui::GetStyle().Colors;" IM_NEWLINE);
  6202. for (int i = 0; i < ImGuiCol_COUNT; i++)
  6203. {
  6204. const ImVec4& col = style.Colors[i];
  6205. const char* name = ImGui::GetStyleColorName(i);
  6206. if (!output_only_modified || memcmp(&col, &ref->Colors[i], sizeof(ImVec4)) != 0)
  6207. ImGui::LogText("colors[ImGuiCol_%s]%*s= ImVec4(%.2ff, %.2ff, %.2ff, %.2ff);" IM_NEWLINE,
  6208. name, 23 - (int)strlen(name), "", col.x, col.y, col.z, col.w);
  6209. }
  6210. ImGui::LogFinish();
  6211. }
  6212. ImGui::SameLine(); ImGui::SetNextItemWidth(120); ImGui::Combo("##output_type", &output_dest, "To Clipboard\0To TTY\0");
  6213. ImGui::SameLine(); ImGui::Checkbox("Only Modified Colors", &output_only_modified);
  6214. static ImGuiTextFilter filter;
  6215. filter.Draw("Filter colors", ImGui::GetFontSize() * 16);
  6216. static ImGuiColorEditFlags alpha_flags = 0;
  6217. if (ImGui::RadioButton("Opaque", alpha_flags == ImGuiColorEditFlags_None)) { alpha_flags = ImGuiColorEditFlags_None; } ImGui::SameLine();
  6218. if (ImGui::RadioButton("Alpha", alpha_flags == ImGuiColorEditFlags_AlphaPreview)) { alpha_flags = ImGuiColorEditFlags_AlphaPreview; } ImGui::SameLine();
  6219. if (ImGui::RadioButton("Both", alpha_flags == ImGuiColorEditFlags_AlphaPreviewHalf)) { alpha_flags = ImGuiColorEditFlags_AlphaPreviewHalf; } ImGui::SameLine();
  6220. HelpMarker(
  6221. "In the color list:\n"
  6222. "Left-click on color square to open color picker,\n"
  6223. "Right-click to open edit options menu.");
  6224. ImGui::SetNextWindowSizeConstraints(ImVec2(0.0f, ImGui::GetTextLineHeightWithSpacing() * 10), ImVec2(FLT_MAX, FLT_MAX));
  6225. ImGui::BeginChild("##colors", ImVec2(0, 0), ImGuiChildFlags_Border, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar | ImGuiWindowFlags_NavFlattened);
  6226. ImGui::PushItemWidth(ImGui::GetFontSize() * -12);
  6227. for (int i = 0; i < ImGuiCol_COUNT; i++)
  6228. {
  6229. const char* name = ImGui::GetStyleColorName(i);
  6230. if (!filter.PassFilter(name))
  6231. continue;
  6232. ImGui::PushID(i);
  6233. #ifndef IMGUI_DISABLE_DEBUG_TOOLS
  6234. if (ImGui::Button("?"))
  6235. ImGui::DebugFlashStyleColor((ImGuiCol)i);
  6236. ImGui::SetItemTooltip("Flash given color to identify places where it is used.");
  6237. ImGui::SameLine();
  6238. #endif
  6239. ImGui::ColorEdit4("##color", (float*)&style.Colors[i], ImGuiColorEditFlags_AlphaBar | alpha_flags);
  6240. if (memcmp(&style.Colors[i], &ref->Colors[i], sizeof(ImVec4)) != 0)
  6241. {
  6242. // Tips: in a real user application, you may want to merge and use an icon font into the main font,
  6243. // so instead of "Save"/"Revert" you'd use icons!
  6244. // Read the FAQ and docs/FONTS.md about using icon fonts. It's really easy and super convenient!
  6245. ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); if (ImGui::Button("Save")) { ref->Colors[i] = style.Colors[i]; }
  6246. ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); if (ImGui::Button("Revert")) { style.Colors[i] = ref->Colors[i]; }
  6247. }
  6248. ImGui::SameLine(0.0f, style.ItemInnerSpacing.x);
  6249. ImGui::TextUnformatted(name);
  6250. ImGui::PopID();
  6251. }
  6252. ImGui::PopItemWidth();
  6253. ImGui::EndChild();
  6254. ImGui::EndTabItem();
  6255. }
  6256. if (ImGui::BeginTabItem("Fonts"))
  6257. {
  6258. ImGuiIO& io = ImGui::GetIO();
  6259. ImFontAtlas* atlas = io.Fonts;
  6260. HelpMarker("Read FAQ and docs/FONTS.md for details on font loading.");
  6261. ImGui::ShowFontAtlas(atlas);
  6262. // Post-baking font scaling. Note that this is NOT the nice way of scaling fonts, read below.
  6263. // (we enforce hard clamping manually as by default DragFloat/SliderFloat allows CTRL+Click text to get out of bounds).
  6264. const float MIN_SCALE = 0.3f;
  6265. const float MAX_SCALE = 2.0f;
  6266. HelpMarker(
  6267. "Those are old settings provided for convenience.\n"
  6268. "However, the _correct_ way of scaling your UI is currently to reload your font at the designed size, "
  6269. "rebuild the font atlas, and call style.ScaleAllSizes() on a reference ImGuiStyle structure.\n"
  6270. "Using those settings here will give you poor quality results.");
  6271. static float window_scale = 1.0f;
  6272. ImGui::PushItemWidth(ImGui::GetFontSize() * 8);
  6273. if (ImGui::DragFloat("window scale", &window_scale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f", ImGuiSliderFlags_AlwaysClamp)) // Scale only this window
  6274. ImGui::SetWindowFontScale(window_scale);
  6275. ImGui::DragFloat("global scale", &io.FontGlobalScale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f", ImGuiSliderFlags_AlwaysClamp); // Scale everything
  6276. ImGui::PopItemWidth();
  6277. ImGui::EndTabItem();
  6278. }
  6279. if (ImGui::BeginTabItem("Rendering"))
  6280. {
  6281. ImGui::Checkbox("Anti-aliased lines", &style.AntiAliasedLines);
  6282. ImGui::SameLine();
  6283. HelpMarker("When disabling anti-aliasing lines, you'll probably want to disable borders in your style as well.");
  6284. ImGui::Checkbox("Anti-aliased lines use texture", &style.AntiAliasedLinesUseTex);
  6285. ImGui::SameLine();
  6286. HelpMarker("Faster lines using texture data. Require backend to render with bilinear filtering (not point/nearest filtering).");
  6287. ImGui::Checkbox("Anti-aliased fill", &style.AntiAliasedFill);
  6288. ImGui::PushItemWidth(ImGui::GetFontSize() * 8);
  6289. ImGui::DragFloat("Curve Tessellation Tolerance", &style.CurveTessellationTol, 0.02f, 0.10f, 10.0f, "%.2f");
  6290. if (style.CurveTessellationTol < 0.10f) style.CurveTessellationTol = 0.10f;
  6291. // When editing the "Circle Segment Max Error" value, draw a preview of its effect on auto-tessellated circles.
  6292. ImGui::DragFloat("Circle Tessellation Max Error", &style.CircleTessellationMaxError , 0.005f, 0.10f, 5.0f, "%.2f", ImGuiSliderFlags_AlwaysClamp);
  6293. const bool show_samples = ImGui::IsItemActive();
  6294. if (show_samples)
  6295. ImGui::SetNextWindowPos(ImGui::GetCursorScreenPos());
  6296. if (show_samples && ImGui::BeginTooltip())
  6297. {
  6298. ImGui::TextUnformatted("(R = radius, N = number of segments)");
  6299. ImGui::Spacing();
  6300. ImDrawList* draw_list = ImGui::GetWindowDrawList();
  6301. const float min_widget_width = ImGui::CalcTextSize("N: MMM\nR: MMM").x;
  6302. for (int n = 0; n < 8; n++)
  6303. {
  6304. const float RAD_MIN = 5.0f;
  6305. const float RAD_MAX = 70.0f;
  6306. const float rad = RAD_MIN + (RAD_MAX - RAD_MIN) * (float)n / (8.0f - 1.0f);
  6307. ImGui::BeginGroup();
  6308. ImGui::Text("R: %.f\nN: %d", rad, draw_list->_CalcCircleAutoSegmentCount(rad));
  6309. const float canvas_width = IM_MAX(min_widget_width, rad * 2.0f);
  6310. const float offset_x = floorf(canvas_width * 0.5f);
  6311. const float offset_y = floorf(RAD_MAX);
  6312. const ImVec2 p1 = ImGui::GetCursorScreenPos();
  6313. draw_list->AddCircle(ImVec2(p1.x + offset_x, p1.y + offset_y), rad, ImGui::GetColorU32(ImGuiCol_Text));
  6314. ImGui::Dummy(ImVec2(canvas_width, RAD_MAX * 2));
  6315. /*
  6316. const ImVec2 p2 = ImGui::GetCursorScreenPos();
  6317. draw_list->AddCircleFilled(ImVec2(p2.x + offset_x, p2.y + offset_y), rad, ImGui::GetColorU32(ImGuiCol_Text));
  6318. ImGui::Dummy(ImVec2(canvas_width, RAD_MAX * 2));
  6319. */
  6320. ImGui::EndGroup();
  6321. ImGui::SameLine();
  6322. }
  6323. ImGui::EndTooltip();
  6324. }
  6325. ImGui::SameLine();
  6326. HelpMarker("When drawing circle primitives with \"num_segments == 0\" tesselation will be calculated automatically.");
  6327. ImGui::DragFloat("Global Alpha", &style.Alpha, 0.005f, 0.20f, 1.0f, "%.2f"); // Not exposing zero here so user doesn't "lose" the UI (zero alpha clips all widgets). But application code could have a toggle to switch between zero and non-zero.
  6328. ImGui::DragFloat("Disabled Alpha", &style.DisabledAlpha, 0.005f, 0.0f, 1.0f, "%.2f"); ImGui::SameLine(); HelpMarker("Additional alpha multiplier for disabled items (multiply over current value of Alpha).");
  6329. ImGui::PopItemWidth();
  6330. ImGui::EndTabItem();
  6331. }
  6332. ImGui::EndTabBar();
  6333. }
  6334. ImGui::PopItemWidth();
  6335. }
  6336. //-----------------------------------------------------------------------------
  6337. // [SECTION] User Guide / ShowUserGuide()
  6338. //-----------------------------------------------------------------------------
  6339. void ImGui::ShowUserGuide()
  6340. {
  6341. ImGuiIO& io = ImGui::GetIO();
  6342. ImGui::BulletText("Double-click on title bar to collapse window.");
  6343. ImGui::BulletText(
  6344. "Click and drag on lower corner to resize window\n"
  6345. "(double-click to auto fit window to its contents).");
  6346. ImGui::BulletText("CTRL+Click on a slider or drag box to input value as text.");
  6347. ImGui::BulletText("TAB/SHIFT+TAB to cycle through keyboard editable fields.");
  6348. ImGui::BulletText("CTRL+Tab to select a window.");
  6349. if (io.FontAllowUserScaling)
  6350. ImGui::BulletText("CTRL+Mouse Wheel to zoom window contents.");
  6351. ImGui::BulletText("While inputing text:\n");
  6352. ImGui::Indent();
  6353. ImGui::BulletText("CTRL+Left/Right to word jump.");
  6354. ImGui::BulletText("CTRL+A or double-click to select all.");
  6355. ImGui::BulletText("CTRL+X/C/V to use clipboard cut/copy/paste.");
  6356. ImGui::BulletText("CTRL+Z,CTRL+Y to undo/redo.");
  6357. ImGui::BulletText("ESCAPE to revert.");
  6358. ImGui::Unindent();
  6359. ImGui::BulletText("With keyboard navigation enabled:");
  6360. ImGui::Indent();
  6361. ImGui::BulletText("Arrow keys to navigate.");
  6362. ImGui::BulletText("Space to activate a widget.");
  6363. ImGui::BulletText("Return to input text into a widget.");
  6364. ImGui::BulletText("Escape to deactivate a widget, close popup, exit child window.");
  6365. ImGui::BulletText("Alt to jump to the menu layer of a window.");
  6366. ImGui::Unindent();
  6367. }
  6368. //-----------------------------------------------------------------------------
  6369. // [SECTION] Example App: Main Menu Bar / ShowExampleAppMainMenuBar()
  6370. //-----------------------------------------------------------------------------
  6371. // - ShowExampleAppMainMenuBar()
  6372. // - ShowExampleMenuFile()
  6373. //-----------------------------------------------------------------------------
  6374. // Demonstrate creating a "main" fullscreen menu bar and populating it.
  6375. // Note the difference between BeginMainMenuBar() and BeginMenuBar():
  6376. // - BeginMenuBar() = menu-bar inside current window (which needs the ImGuiWindowFlags_MenuBar flag!)
  6377. // - BeginMainMenuBar() = helper to create menu-bar-sized window at the top of the main viewport + call BeginMenuBar() into it.
  6378. static void ShowExampleAppMainMenuBar()
  6379. {
  6380. if (ImGui::BeginMainMenuBar())
  6381. {
  6382. if (ImGui::BeginMenu("File"))
  6383. {
  6384. ShowExampleMenuFile();
  6385. ImGui::EndMenu();
  6386. }
  6387. if (ImGui::BeginMenu("Edit"))
  6388. {
  6389. if (ImGui::MenuItem("Undo", "CTRL+Z")) {}
  6390. if (ImGui::MenuItem("Redo", "CTRL+Y", false, false)) {} // Disabled item
  6391. ImGui::Separator();
  6392. if (ImGui::MenuItem("Cut", "CTRL+X")) {}
  6393. if (ImGui::MenuItem("Copy", "CTRL+C")) {}
  6394. if (ImGui::MenuItem("Paste", "CTRL+V")) {}
  6395. ImGui::EndMenu();
  6396. }
  6397. ImGui::EndMainMenuBar();
  6398. }
  6399. }
  6400. // Note that shortcuts are currently provided for display only
  6401. // (future version will add explicit flags to BeginMenu() to request processing shortcuts)
  6402. static void ShowExampleMenuFile()
  6403. {
  6404. IMGUI_DEMO_MARKER("Examples/Menu");
  6405. ImGui::MenuItem("(demo menu)", NULL, false, false);
  6406. if (ImGui::MenuItem("New")) {}
  6407. if (ImGui::MenuItem("Open", "Ctrl+O")) {}
  6408. if (ImGui::BeginMenu("Open Recent"))
  6409. {
  6410. ImGui::MenuItem("fish_hat.c");
  6411. ImGui::MenuItem("fish_hat.inl");
  6412. ImGui::MenuItem("fish_hat.h");
  6413. if (ImGui::BeginMenu("More.."))
  6414. {
  6415. ImGui::MenuItem("Hello");
  6416. ImGui::MenuItem("Sailor");
  6417. if (ImGui::BeginMenu("Recurse.."))
  6418. {
  6419. ShowExampleMenuFile();
  6420. ImGui::EndMenu();
  6421. }
  6422. ImGui::EndMenu();
  6423. }
  6424. ImGui::EndMenu();
  6425. }
  6426. if (ImGui::MenuItem("Save", "Ctrl+S")) {}
  6427. if (ImGui::MenuItem("Save As..")) {}
  6428. ImGui::Separator();
  6429. IMGUI_DEMO_MARKER("Examples/Menu/Options");
  6430. if (ImGui::BeginMenu("Options"))
  6431. {
  6432. static bool enabled = true;
  6433. ImGui::MenuItem("Enabled", "", &enabled);
  6434. ImGui::BeginChild("child", ImVec2(0, 60), ImGuiChildFlags_Border);
  6435. for (int i = 0; i < 10; i++)
  6436. ImGui::Text("Scrolling Text %d", i);
  6437. ImGui::EndChild();
  6438. static float f = 0.5f;
  6439. static int n = 0;
  6440. ImGui::SliderFloat("Value", &f, 0.0f, 1.0f);
  6441. ImGui::InputFloat("Input", &f, 0.1f);
  6442. ImGui::Combo("Combo", &n, "Yes\0No\0Maybe\0\0");
  6443. ImGui::EndMenu();
  6444. }
  6445. IMGUI_DEMO_MARKER("Examples/Menu/Colors");
  6446. if (ImGui::BeginMenu("Colors"))
  6447. {
  6448. float sz = ImGui::GetTextLineHeight();
  6449. for (int i = 0; i < ImGuiCol_COUNT; i++)
  6450. {
  6451. const char* name = ImGui::GetStyleColorName((ImGuiCol)i);
  6452. ImVec2 p = ImGui::GetCursorScreenPos();
  6453. ImGui::GetWindowDrawList()->AddRectFilled(p, ImVec2(p.x + sz, p.y + sz), ImGui::GetColorU32((ImGuiCol)i));
  6454. ImGui::Dummy(ImVec2(sz, sz));
  6455. ImGui::SameLine();
  6456. ImGui::MenuItem(name);
  6457. }
  6458. ImGui::EndMenu();
  6459. }
  6460. // Here we demonstrate appending again to the "Options" menu (which we already created above)
  6461. // Of course in this demo it is a little bit silly that this function calls BeginMenu("Options") twice.
  6462. // In a real code-base using it would make senses to use this feature from very different code locations.
  6463. if (ImGui::BeginMenu("Options")) // <-- Append!
  6464. {
  6465. IMGUI_DEMO_MARKER("Examples/Menu/Append to an existing menu");
  6466. static bool b = true;
  6467. ImGui::Checkbox("SomeOption", &b);
  6468. ImGui::EndMenu();
  6469. }
  6470. if (ImGui::BeginMenu("Disabled", false)) // Disabled
  6471. {
  6472. IM_ASSERT(0);
  6473. }
  6474. if (ImGui::MenuItem("Checked", NULL, true)) {}
  6475. ImGui::Separator();
  6476. if (ImGui::MenuItem("Quit", "Alt+F4")) {}
  6477. }
  6478. //-----------------------------------------------------------------------------
  6479. // [SECTION] Example App: Debug Console / ShowExampleAppConsole()
  6480. //-----------------------------------------------------------------------------
  6481. // Demonstrate creating a simple console window, with scrolling, filtering, completion and history.
  6482. // For the console example, we are using a more C++ like approach of declaring a class to hold both data and functions.
  6483. struct ExampleAppConsole
  6484. {
  6485. char InputBuf[256];
  6486. ImVector<char*> Items;
  6487. ImVector<const char*> Commands;
  6488. ImVector<char*> History;
  6489. int HistoryPos; // -1: new line, 0..History.Size-1 browsing history.
  6490. ImGuiTextFilter Filter;
  6491. bool AutoScroll;
  6492. bool ScrollToBottom;
  6493. ExampleAppConsole()
  6494. {
  6495. IMGUI_DEMO_MARKER("Examples/Console");
  6496. ClearLog();
  6497. memset(InputBuf, 0, sizeof(InputBuf));
  6498. HistoryPos = -1;
  6499. // "CLASSIFY" is here to provide the test case where "C"+[tab] completes to "CL" and display multiple matches.
  6500. Commands.push_back("HELP");
  6501. Commands.push_back("HISTORY");
  6502. Commands.push_back("CLEAR");
  6503. Commands.push_back("CLASSIFY");
  6504. AutoScroll = true;
  6505. ScrollToBottom = false;
  6506. AddLog("Welcome to Dear ImGui!");
  6507. }
  6508. ~ExampleAppConsole()
  6509. {
  6510. ClearLog();
  6511. for (int i = 0; i < History.Size; i++)
  6512. ImGui::MemFree(History[i]);
  6513. }
  6514. // Portable helpers
  6515. static int Stricmp(const char* s1, const char* s2) { int d; while ((d = toupper(*s2) - toupper(*s1)) == 0 && *s1) { s1++; s2++; } return d; }
  6516. static int Strnicmp(const char* s1, const char* s2, int n) { int d = 0; while (n > 0 && (d = toupper(*s2) - toupper(*s1)) == 0 && *s1) { s1++; s2++; n--; } return d; }
  6517. static char* Strdup(const char* s) { IM_ASSERT(s); size_t len = strlen(s) + 1; void* buf = ImGui::MemAlloc(len); IM_ASSERT(buf); return (char*)memcpy(buf, (const void*)s, len); }
  6518. static void Strtrim(char* s) { char* str_end = s + strlen(s); while (str_end > s && str_end[-1] == ' ') str_end--; *str_end = 0; }
  6519. void ClearLog()
  6520. {
  6521. for (int i = 0; i < Items.Size; i++)
  6522. ImGui::MemFree(Items[i]);
  6523. Items.clear();
  6524. }
  6525. void AddLog(const char* fmt, ...) IM_FMTARGS(2)
  6526. {
  6527. // FIXME-OPT
  6528. char buf[1024];
  6529. va_list args;
  6530. va_start(args, fmt);
  6531. vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args);
  6532. buf[IM_ARRAYSIZE(buf)-1] = 0;
  6533. va_end(args);
  6534. Items.push_back(Strdup(buf));
  6535. }
  6536. void Draw(const char* title, bool* p_open)
  6537. {
  6538. ImGui::SetNextWindowSize(ImVec2(520, 600), ImGuiCond_FirstUseEver);
  6539. if (!ImGui::Begin(title, p_open))
  6540. {
  6541. ImGui::End();
  6542. return;
  6543. }
  6544. // As a specific feature guaranteed by the library, after calling Begin() the last Item represent the title bar.
  6545. // So e.g. IsItemHovered() will return true when hovering the title bar.
  6546. // Here we create a context menu only available from the title bar.
  6547. if (ImGui::BeginPopupContextItem())
  6548. {
  6549. if (ImGui::MenuItem("Close Console"))
  6550. *p_open = false;
  6551. ImGui::EndPopup();
  6552. }
  6553. ImGui::TextWrapped(
  6554. "This example implements a console with basic coloring, completion (TAB key) and history (Up/Down keys). A more elaborate "
  6555. "implementation may want to store entries along with extra data such as timestamp, emitter, etc.");
  6556. ImGui::TextWrapped("Enter 'HELP' for help.");
  6557. // TODO: display items starting from the bottom
  6558. if (ImGui::SmallButton("Add Debug Text")) { AddLog("%d some text", Items.Size); AddLog("some more text"); AddLog("display very important message here!"); }
  6559. ImGui::SameLine();
  6560. if (ImGui::SmallButton("Add Debug Error")) { AddLog("[error] something went wrong"); }
  6561. ImGui::SameLine();
  6562. if (ImGui::SmallButton("Clear")) { ClearLog(); }
  6563. ImGui::SameLine();
  6564. bool copy_to_clipboard = ImGui::SmallButton("Copy");
  6565. //static float t = 0.0f; if (ImGui::GetTime() - t > 0.02f) { t = ImGui::GetTime(); AddLog("Spam %f", t); }
  6566. ImGui::Separator();
  6567. // Options menu
  6568. if (ImGui::BeginPopup("Options"))
  6569. {
  6570. ImGui::Checkbox("Auto-scroll", &AutoScroll);
  6571. ImGui::EndPopup();
  6572. }
  6573. // Options, Filter
  6574. ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_O, ImGuiInputFlags_Tooltip);
  6575. if (ImGui::Button("Options"))
  6576. ImGui::OpenPopup("Options");
  6577. ImGui::SameLine();
  6578. Filter.Draw("Filter (\"incl,-excl\") (\"error\")", 180);
  6579. ImGui::Separator();
  6580. // Reserve enough left-over height for 1 separator + 1 input text
  6581. const float footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing();
  6582. if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), ImGuiChildFlags_None, ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_NavFlattened))
  6583. {
  6584. if (ImGui::BeginPopupContextWindow())
  6585. {
  6586. if (ImGui::Selectable("Clear")) ClearLog();
  6587. ImGui::EndPopup();
  6588. }
  6589. // Display every line as a separate entry so we can change their color or add custom widgets.
  6590. // If you only want raw text you can use ImGui::TextUnformatted(log.begin(), log.end());
  6591. // NB- if you have thousands of entries this approach may be too inefficient and may require user-side clipping
  6592. // to only process visible items. The clipper will automatically measure the height of your first item and then
  6593. // "seek" to display only items in the visible area.
  6594. // To use the clipper we can replace your standard loop:
  6595. // for (int i = 0; i < Items.Size; i++)
  6596. // With:
  6597. // ImGuiListClipper clipper;
  6598. // clipper.Begin(Items.Size);
  6599. // while (clipper.Step())
  6600. // for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)
  6601. // - That your items are evenly spaced (same height)
  6602. // - That you have cheap random access to your elements (you can access them given their index,
  6603. // without processing all the ones before)
  6604. // You cannot this code as-is if a filter is active because it breaks the 'cheap random-access' property.
  6605. // We would need random-access on the post-filtered list.
  6606. // A typical application wanting coarse clipping and filtering may want to pre-compute an array of indices
  6607. // or offsets of items that passed the filtering test, recomputing this array when user changes the filter,
  6608. // and appending newly elements as they are inserted. This is left as a task to the user until we can manage
  6609. // to improve this example code!
  6610. // If your items are of variable height:
  6611. // - Split them into same height items would be simpler and facilitate random-seeking into your list.
  6612. // - Consider using manual call to IsRectVisible() and skipping extraneous decoration from your items.
  6613. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1)); // Tighten spacing
  6614. if (copy_to_clipboard)
  6615. ImGui::LogToClipboard();
  6616. for (const char* item : Items)
  6617. {
  6618. if (!Filter.PassFilter(item))
  6619. continue;
  6620. // Normally you would store more information in your item than just a string.
  6621. // (e.g. make Items[] an array of structure, store color/type etc.)
  6622. ImVec4 color;
  6623. bool has_color = false;
  6624. if (strstr(item, "[error]")) { color = ImVec4(1.0f, 0.4f, 0.4f, 1.0f); has_color = true; }
  6625. else if (strncmp(item, "# ", 2) == 0) { color = ImVec4(1.0f, 0.8f, 0.6f, 1.0f); has_color = true; }
  6626. if (has_color)
  6627. ImGui::PushStyleColor(ImGuiCol_Text, color);
  6628. ImGui::TextUnformatted(item);
  6629. if (has_color)
  6630. ImGui::PopStyleColor();
  6631. }
  6632. if (copy_to_clipboard)
  6633. ImGui::LogFinish();
  6634. // Keep up at the bottom of the scroll region if we were already at the bottom at the beginning of the frame.
  6635. // Using a scrollbar or mouse-wheel will take away from the bottom edge.
  6636. if (ScrollToBottom || (AutoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY()))
  6637. ImGui::SetScrollHereY(1.0f);
  6638. ScrollToBottom = false;
  6639. ImGui::PopStyleVar();
  6640. }
  6641. ImGui::EndChild();
  6642. ImGui::Separator();
  6643. // Command-line
  6644. bool reclaim_focus = false;
  6645. ImGuiInputTextFlags input_text_flags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_EscapeClearsAll | ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory;
  6646. if (ImGui::InputText("Input", InputBuf, IM_ARRAYSIZE(InputBuf), input_text_flags, &TextEditCallbackStub, (void*)this))
  6647. {
  6648. char* s = InputBuf;
  6649. Strtrim(s);
  6650. if (s[0])
  6651. ExecCommand(s);
  6652. strcpy(s, "");
  6653. reclaim_focus = true;
  6654. }
  6655. // Auto-focus on window apparition
  6656. ImGui::SetItemDefaultFocus();
  6657. if (reclaim_focus)
  6658. ImGui::SetKeyboardFocusHere(-1); // Auto focus previous widget
  6659. ImGui::End();
  6660. }
  6661. void ExecCommand(const char* command_line)
  6662. {
  6663. AddLog("# %s\n", command_line);
  6664. // Insert into history. First find match and delete it so it can be pushed to the back.
  6665. // This isn't trying to be smart or optimal.
  6666. HistoryPos = -1;
  6667. for (int i = History.Size - 1; i >= 0; i--)
  6668. if (Stricmp(History[i], command_line) == 0)
  6669. {
  6670. ImGui::MemFree(History[i]);
  6671. History.erase(History.begin() + i);
  6672. break;
  6673. }
  6674. History.push_back(Strdup(command_line));
  6675. // Process command
  6676. if (Stricmp(command_line, "CLEAR") == 0)
  6677. {
  6678. ClearLog();
  6679. }
  6680. else if (Stricmp(command_line, "HELP") == 0)
  6681. {
  6682. AddLog("Commands:");
  6683. for (int i = 0; i < Commands.Size; i++)
  6684. AddLog("- %s", Commands[i]);
  6685. }
  6686. else if (Stricmp(command_line, "HISTORY") == 0)
  6687. {
  6688. int first = History.Size - 10;
  6689. for (int i = first > 0 ? first : 0; i < History.Size; i++)
  6690. AddLog("%3d: %s\n", i, History[i]);
  6691. }
  6692. else
  6693. {
  6694. AddLog("Unknown command: '%s'\n", command_line);
  6695. }
  6696. // On command input, we scroll to bottom even if AutoScroll==false
  6697. ScrollToBottom = true;
  6698. }
  6699. // In C++11 you'd be better off using lambdas for this sort of forwarding callbacks
  6700. static int TextEditCallbackStub(ImGuiInputTextCallbackData* data)
  6701. {
  6702. ExampleAppConsole* console = (ExampleAppConsole*)data->UserData;
  6703. return console->TextEditCallback(data);
  6704. }
  6705. int TextEditCallback(ImGuiInputTextCallbackData* data)
  6706. {
  6707. //AddLog("cursor: %d, selection: %d-%d", data->CursorPos, data->SelectionStart, data->SelectionEnd);
  6708. switch (data->EventFlag)
  6709. {
  6710. case ImGuiInputTextFlags_CallbackCompletion:
  6711. {
  6712. // Example of TEXT COMPLETION
  6713. // Locate beginning of current word
  6714. const char* word_end = data->Buf + data->CursorPos;
  6715. const char* word_start = word_end;
  6716. while (word_start > data->Buf)
  6717. {
  6718. const char c = word_start[-1];
  6719. if (c == ' ' || c == '\t' || c == ',' || c == ';')
  6720. break;
  6721. word_start--;
  6722. }
  6723. // Build a list of candidates
  6724. ImVector<const char*> candidates;
  6725. for (int i = 0; i < Commands.Size; i++)
  6726. if (Strnicmp(Commands[i], word_start, (int)(word_end - word_start)) == 0)
  6727. candidates.push_back(Commands[i]);
  6728. if (candidates.Size == 0)
  6729. {
  6730. // No match
  6731. AddLog("No match for \"%.*s\"!\n", (int)(word_end - word_start), word_start);
  6732. }
  6733. else if (candidates.Size == 1)
  6734. {
  6735. // Single match. Delete the beginning of the word and replace it entirely so we've got nice casing.
  6736. data->DeleteChars((int)(word_start - data->Buf), (int)(word_end - word_start));
  6737. data->InsertChars(data->CursorPos, candidates[0]);
  6738. data->InsertChars(data->CursorPos, " ");
  6739. }
  6740. else
  6741. {
  6742. // Multiple matches. Complete as much as we can..
  6743. // So inputing "C"+Tab will complete to "CL" then display "CLEAR" and "CLASSIFY" as matches.
  6744. int match_len = (int)(word_end - word_start);
  6745. for (;;)
  6746. {
  6747. int c = 0;
  6748. bool all_candidates_matches = true;
  6749. for (int i = 0; i < candidates.Size && all_candidates_matches; i++)
  6750. if (i == 0)
  6751. c = toupper(candidates[i][match_len]);
  6752. else if (c == 0 || c != toupper(candidates[i][match_len]))
  6753. all_candidates_matches = false;
  6754. if (!all_candidates_matches)
  6755. break;
  6756. match_len++;
  6757. }
  6758. if (match_len > 0)
  6759. {
  6760. data->DeleteChars((int)(word_start - data->Buf), (int)(word_end - word_start));
  6761. data->InsertChars(data->CursorPos, candidates[0], candidates[0] + match_len);
  6762. }
  6763. // List matches
  6764. AddLog("Possible matches:\n");
  6765. for (int i = 0; i < candidates.Size; i++)
  6766. AddLog("- %s\n", candidates[i]);
  6767. }
  6768. break;
  6769. }
  6770. case ImGuiInputTextFlags_CallbackHistory:
  6771. {
  6772. // Example of HISTORY
  6773. const int prev_history_pos = HistoryPos;
  6774. if (data->EventKey == ImGuiKey_UpArrow)
  6775. {
  6776. if (HistoryPos == -1)
  6777. HistoryPos = History.Size - 1;
  6778. else if (HistoryPos > 0)
  6779. HistoryPos--;
  6780. }
  6781. else if (data->EventKey == ImGuiKey_DownArrow)
  6782. {
  6783. if (HistoryPos != -1)
  6784. if (++HistoryPos >= History.Size)
  6785. HistoryPos = -1;
  6786. }
  6787. // A better implementation would preserve the data on the current input line along with cursor position.
  6788. if (prev_history_pos != HistoryPos)
  6789. {
  6790. const char* history_str = (HistoryPos >= 0) ? History[HistoryPos] : "";
  6791. data->DeleteChars(0, data->BufTextLen);
  6792. data->InsertChars(0, history_str);
  6793. }
  6794. }
  6795. }
  6796. return 0;
  6797. }
  6798. };
  6799. static void ShowExampleAppConsole(bool* p_open)
  6800. {
  6801. static ExampleAppConsole console;
  6802. console.Draw("Example: Console", p_open);
  6803. }
  6804. //-----------------------------------------------------------------------------
  6805. // [SECTION] Example App: Debug Log / ShowExampleAppLog()
  6806. //-----------------------------------------------------------------------------
  6807. // Usage:
  6808. // static ExampleAppLog my_log;
  6809. // my_log.AddLog("Hello %d world\n", 123);
  6810. // my_log.Draw("title");
  6811. struct ExampleAppLog
  6812. {
  6813. ImGuiTextBuffer Buf;
  6814. ImGuiTextFilter Filter;
  6815. ImVector<int> LineOffsets; // Index to lines offset. We maintain this with AddLog() calls.
  6816. bool AutoScroll; // Keep scrolling if already at the bottom.
  6817. ExampleAppLog()
  6818. {
  6819. AutoScroll = true;
  6820. Clear();
  6821. }
  6822. void Clear()
  6823. {
  6824. Buf.clear();
  6825. LineOffsets.clear();
  6826. LineOffsets.push_back(0);
  6827. }
  6828. void AddLog(const char* fmt, ...) IM_FMTARGS(2)
  6829. {
  6830. int old_size = Buf.size();
  6831. va_list args;
  6832. va_start(args, fmt);
  6833. Buf.appendfv(fmt, args);
  6834. va_end(args);
  6835. for (int new_size = Buf.size(); old_size < new_size; old_size++)
  6836. if (Buf[old_size] == '\n')
  6837. LineOffsets.push_back(old_size + 1);
  6838. }
  6839. void Draw(const char* title, bool* p_open = NULL)
  6840. {
  6841. if (!ImGui::Begin(title, p_open))
  6842. {
  6843. ImGui::End();
  6844. return;
  6845. }
  6846. // Options menu
  6847. if (ImGui::BeginPopup("Options"))
  6848. {
  6849. ImGui::Checkbox("Auto-scroll", &AutoScroll);
  6850. ImGui::EndPopup();
  6851. }
  6852. // Main window
  6853. if (ImGui::Button("Options"))
  6854. ImGui::OpenPopup("Options");
  6855. ImGui::SameLine();
  6856. bool clear = ImGui::Button("Clear");
  6857. ImGui::SameLine();
  6858. bool copy = ImGui::Button("Copy");
  6859. ImGui::SameLine();
  6860. Filter.Draw("Filter", -100.0f);
  6861. ImGui::Separator();
  6862. if (ImGui::BeginChild("scrolling", ImVec2(0, 0), ImGuiChildFlags_None, ImGuiWindowFlags_HorizontalScrollbar))
  6863. {
  6864. if (clear)
  6865. Clear();
  6866. if (copy)
  6867. ImGui::LogToClipboard();
  6868. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
  6869. const char* buf = Buf.begin();
  6870. const char* buf_end = Buf.end();
  6871. if (Filter.IsActive())
  6872. {
  6873. // In this example we don't use the clipper when Filter is enabled.
  6874. // This is because we don't have random access to the result of our filter.
  6875. // A real application processing logs with ten of thousands of entries may want to store the result of
  6876. // search/filter.. especially if the filtering function is not trivial (e.g. reg-exp).
  6877. for (int line_no = 0; line_no < LineOffsets.Size; line_no++)
  6878. {
  6879. const char* line_start = buf + LineOffsets[line_no];
  6880. const char* line_end = (line_no + 1 < LineOffsets.Size) ? (buf + LineOffsets[line_no + 1] - 1) : buf_end;
  6881. if (Filter.PassFilter(line_start, line_end))
  6882. ImGui::TextUnformatted(line_start, line_end);
  6883. }
  6884. }
  6885. else
  6886. {
  6887. // The simplest and easy way to display the entire buffer:
  6888. // ImGui::TextUnformatted(buf_begin, buf_end);
  6889. // And it'll just work. TextUnformatted() has specialization for large blob of text and will fast-forward
  6890. // to skip non-visible lines. Here we instead demonstrate using the clipper to only process lines that are
  6891. // within the visible area.
  6892. // If you have tens of thousands of items and their processing cost is non-negligible, coarse clipping them
  6893. // on your side is recommended. Using ImGuiListClipper requires
  6894. // - A) random access into your data
  6895. // - B) items all being the same height,
  6896. // both of which we can handle since we have an array pointing to the beginning of each line of text.
  6897. // When using the filter (in the block of code above) we don't have random access into the data to display
  6898. // anymore, which is why we don't use the clipper. Storing or skimming through the search result would make
  6899. // it possible (and would be recommended if you want to search through tens of thousands of entries).
  6900. ImGuiListClipper clipper;
  6901. clipper.Begin(LineOffsets.Size);
  6902. while (clipper.Step())
  6903. {
  6904. for (int line_no = clipper.DisplayStart; line_no < clipper.DisplayEnd; line_no++)
  6905. {
  6906. const char* line_start = buf + LineOffsets[line_no];
  6907. const char* line_end = (line_no + 1 < LineOffsets.Size) ? (buf + LineOffsets[line_no + 1] - 1) : buf_end;
  6908. ImGui::TextUnformatted(line_start, line_end);
  6909. }
  6910. }
  6911. clipper.End();
  6912. }
  6913. ImGui::PopStyleVar();
  6914. // Keep up at the bottom of the scroll region if we were already at the bottom at the beginning of the frame.
  6915. // Using a scrollbar or mouse-wheel will take away from the bottom edge.
  6916. if (AutoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY())
  6917. ImGui::SetScrollHereY(1.0f);
  6918. }
  6919. ImGui::EndChild();
  6920. ImGui::End();
  6921. }
  6922. };
  6923. // Demonstrate creating a simple log window with basic filtering.
  6924. static void ShowExampleAppLog(bool* p_open)
  6925. {
  6926. static ExampleAppLog log;
  6927. // For the demo: add a debug button _BEFORE_ the normal log window contents
  6928. // We take advantage of a rarely used feature: multiple calls to Begin()/End() are appending to the _same_ window.
  6929. // Most of the contents of the window will be added by the log.Draw() call.
  6930. ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver);
  6931. ImGui::Begin("Example: Log", p_open);
  6932. IMGUI_DEMO_MARKER("Examples/Log");
  6933. if (ImGui::SmallButton("[Debug] Add 5 entries"))
  6934. {
  6935. static int counter = 0;
  6936. const char* categories[3] = { "info", "warn", "error" };
  6937. const char* words[] = { "Bumfuzzled", "Cattywampus", "Snickersnee", "Abibliophobia", "Absquatulate", "Nincompoop", "Pauciloquent" };
  6938. for (int n = 0; n < 5; n++)
  6939. {
  6940. const char* category = categories[counter % IM_ARRAYSIZE(categories)];
  6941. const char* word = words[counter % IM_ARRAYSIZE(words)];
  6942. log.AddLog("[%05d] [%s] Hello, current time is %.1f, here's a word: '%s'\n",
  6943. ImGui::GetFrameCount(), category, ImGui::GetTime(), word);
  6944. counter++;
  6945. }
  6946. }
  6947. ImGui::End();
  6948. // Actually call in the regular Log helper (which will Begin() into the same window as we just did)
  6949. log.Draw("Example: Log", p_open);
  6950. }
  6951. //-----------------------------------------------------------------------------
  6952. // [SECTION] Example App: Simple Layout / ShowExampleAppLayout()
  6953. //-----------------------------------------------------------------------------
  6954. // Demonstrate create a window with multiple child windows.
  6955. static void ShowExampleAppLayout(bool* p_open)
  6956. {
  6957. ImGui::SetNextWindowSize(ImVec2(500, 440), ImGuiCond_FirstUseEver);
  6958. if (ImGui::Begin("Example: Simple layout", p_open, ImGuiWindowFlags_MenuBar))
  6959. {
  6960. IMGUI_DEMO_MARKER("Examples/Simple layout");
  6961. if (ImGui::BeginMenuBar())
  6962. {
  6963. if (ImGui::BeginMenu("File"))
  6964. {
  6965. if (ImGui::MenuItem("Close", "Ctrl+W")) { *p_open = false; }
  6966. ImGui::EndMenu();
  6967. }
  6968. ImGui::EndMenuBar();
  6969. }
  6970. // Left
  6971. static int selected = 0;
  6972. {
  6973. ImGui::BeginChild("left pane", ImVec2(150, 0), ImGuiChildFlags_Border | ImGuiChildFlags_ResizeX);
  6974. for (int i = 0; i < 100; i++)
  6975. {
  6976. // FIXME: Good candidate to use ImGuiSelectableFlags_SelectOnNav
  6977. char label[128];
  6978. sprintf(label, "MyObject %d", i);
  6979. if (ImGui::Selectable(label, selected == i))
  6980. selected = i;
  6981. }
  6982. ImGui::EndChild();
  6983. }
  6984. ImGui::SameLine();
  6985. // Right
  6986. {
  6987. ImGui::BeginGroup();
  6988. ImGui::BeginChild("item view", ImVec2(0, -ImGui::GetFrameHeightWithSpacing())); // Leave room for 1 line below us
  6989. ImGui::Text("MyObject: %d", selected);
  6990. ImGui::Separator();
  6991. if (ImGui::BeginTabBar("##Tabs", ImGuiTabBarFlags_None))
  6992. {
  6993. if (ImGui::BeginTabItem("Description"))
  6994. {
  6995. ImGui::TextWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ");
  6996. ImGui::EndTabItem();
  6997. }
  6998. if (ImGui::BeginTabItem("Details"))
  6999. {
  7000. ImGui::Text("ID: 0123456789");
  7001. ImGui::EndTabItem();
  7002. }
  7003. ImGui::EndTabBar();
  7004. }
  7005. ImGui::EndChild();
  7006. if (ImGui::Button("Revert")) {}
  7007. ImGui::SameLine();
  7008. if (ImGui::Button("Save")) {}
  7009. ImGui::EndGroup();
  7010. }
  7011. }
  7012. ImGui::End();
  7013. }
  7014. //-----------------------------------------------------------------------------
  7015. // [SECTION] Example App: Property Editor / ShowExampleAppPropertyEditor()
  7016. //-----------------------------------------------------------------------------
  7017. static void ShowPlaceholderObject(const char* prefix, int uid)
  7018. {
  7019. // Use object uid as identifier. Most commonly you could also use the object pointer as a base ID.
  7020. ImGui::PushID(uid);
  7021. // Text and Tree nodes are less high than framed widgets, using AlignTextToFramePadding() we add vertical spacing to make the tree lines equal high.
  7022. ImGui::TableNextRow();
  7023. ImGui::TableSetColumnIndex(0);
  7024. ImGui::AlignTextToFramePadding();
  7025. bool node_open = ImGui::TreeNode("Object", "%s_%u", prefix, uid);
  7026. ImGui::TableSetColumnIndex(1);
  7027. ImGui::Text("my sailor is rich");
  7028. if (node_open)
  7029. {
  7030. static float placeholder_members[8] = { 0.0f, 0.0f, 1.0f, 3.1416f, 100.0f, 999.0f };
  7031. for (int i = 0; i < 8; i++)
  7032. {
  7033. ImGui::PushID(i); // Use field index as identifier.
  7034. if (i < 2)
  7035. {
  7036. ShowPlaceholderObject("Child", 424242);
  7037. }
  7038. else
  7039. {
  7040. // Here we use a TreeNode to highlight on hover (we could use e.g. Selectable as well)
  7041. ImGui::TableNextRow();
  7042. ImGui::TableSetColumnIndex(0);
  7043. ImGui::AlignTextToFramePadding();
  7044. ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_Bullet;
  7045. ImGui::TreeNodeEx("Field", flags, "Field_%d", i);
  7046. ImGui::TableSetColumnIndex(1);
  7047. ImGui::SetNextItemWidth(-FLT_MIN);
  7048. if (i >= 5)
  7049. ImGui::InputFloat("##value", &placeholder_members[i], 1.0f);
  7050. else
  7051. ImGui::DragFloat("##value", &placeholder_members[i], 0.01f);
  7052. ImGui::NextColumn();
  7053. }
  7054. ImGui::PopID();
  7055. }
  7056. ImGui::TreePop();
  7057. }
  7058. ImGui::PopID();
  7059. }
  7060. // Demonstrate create a simple property editor.
  7061. // This demo is a bit lackluster nowadays, would be nice to improve.
  7062. static void ShowExampleAppPropertyEditor(bool* p_open)
  7063. {
  7064. ImGui::SetNextWindowSize(ImVec2(430, 450), ImGuiCond_FirstUseEver);
  7065. if (!ImGui::Begin("Example: Property editor", p_open))
  7066. {
  7067. ImGui::End();
  7068. return;
  7069. }
  7070. IMGUI_DEMO_MARKER("Examples/Property Editor");
  7071. HelpMarker(
  7072. "This example shows how you may implement a property editor using two columns.\n"
  7073. "All objects/fields data are dummies here.\n");
  7074. ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2));
  7075. if (ImGui::BeginTable("##split", 2, ImGuiTableFlags_BordersOuter | ImGuiTableFlags_Resizable | ImGuiTableFlags_ScrollY))
  7076. {
  7077. ImGui::TableSetupScrollFreeze(0, 1);
  7078. ImGui::TableSetupColumn("Object");
  7079. ImGui::TableSetupColumn("Contents");
  7080. ImGui::TableHeadersRow();
  7081. // Iterate placeholder objects (all the same data)
  7082. for (int obj_i = 0; obj_i < 4; obj_i++)
  7083. ShowPlaceholderObject("Object", obj_i);
  7084. ImGui::EndTable();
  7085. }
  7086. ImGui::PopStyleVar();
  7087. ImGui::End();
  7088. }
  7089. //-----------------------------------------------------------------------------
  7090. // [SECTION] Example App: Long Text / ShowExampleAppLongText()
  7091. //-----------------------------------------------------------------------------
  7092. // Demonstrate/test rendering huge amount of text, and the incidence of clipping.
  7093. static void ShowExampleAppLongText(bool* p_open)
  7094. {
  7095. ImGui::SetNextWindowSize(ImVec2(520, 600), ImGuiCond_FirstUseEver);
  7096. if (!ImGui::Begin("Example: Long text display", p_open))
  7097. {
  7098. ImGui::End();
  7099. return;
  7100. }
  7101. IMGUI_DEMO_MARKER("Examples/Long text display");
  7102. static int test_type = 0;
  7103. static ImGuiTextBuffer log;
  7104. static int lines = 0;
  7105. ImGui::Text("Printing unusually long amount of text.");
  7106. ImGui::Combo("Test type", &test_type,
  7107. "Single call to TextUnformatted()\0"
  7108. "Multiple calls to Text(), clipped\0"
  7109. "Multiple calls to Text(), not clipped (slow)\0");
  7110. ImGui::Text("Buffer contents: %d lines, %d bytes", lines, log.size());
  7111. if (ImGui::Button("Clear")) { log.clear(); lines = 0; }
  7112. ImGui::SameLine();
  7113. if (ImGui::Button("Add 1000 lines"))
  7114. {
  7115. for (int i = 0; i < 1000; i++)
  7116. log.appendf("%i The quick brown fox jumps over the lazy dog\n", lines + i);
  7117. lines += 1000;
  7118. }
  7119. ImGui::BeginChild("Log");
  7120. switch (test_type)
  7121. {
  7122. case 0:
  7123. // Single call to TextUnformatted() with a big buffer
  7124. ImGui::TextUnformatted(log.begin(), log.end());
  7125. break;
  7126. case 1:
  7127. {
  7128. // Multiple calls to Text(), manually coarsely clipped - demonstrate how to use the ImGuiListClipper helper.
  7129. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
  7130. ImGuiListClipper clipper;
  7131. clipper.Begin(lines);
  7132. while (clipper.Step())
  7133. for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)
  7134. ImGui::Text("%i The quick brown fox jumps over the lazy dog", i);
  7135. ImGui::PopStyleVar();
  7136. break;
  7137. }
  7138. case 2:
  7139. // Multiple calls to Text(), not clipped (slow)
  7140. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
  7141. for (int i = 0; i < lines; i++)
  7142. ImGui::Text("%i The quick brown fox jumps over the lazy dog", i);
  7143. ImGui::PopStyleVar();
  7144. break;
  7145. }
  7146. ImGui::EndChild();
  7147. ImGui::End();
  7148. }
  7149. //-----------------------------------------------------------------------------
  7150. // [SECTION] Example App: Auto Resize / ShowExampleAppAutoResize()
  7151. //-----------------------------------------------------------------------------
  7152. // Demonstrate creating a window which gets auto-resized according to its content.
  7153. static void ShowExampleAppAutoResize(bool* p_open)
  7154. {
  7155. if (!ImGui::Begin("Example: Auto-resizing window", p_open, ImGuiWindowFlags_AlwaysAutoResize))
  7156. {
  7157. ImGui::End();
  7158. return;
  7159. }
  7160. IMGUI_DEMO_MARKER("Examples/Auto-resizing window");
  7161. static int lines = 10;
  7162. ImGui::TextUnformatted(
  7163. "Window will resize every-frame to the size of its content.\n"
  7164. "Note that you probably don't want to query the window size to\n"
  7165. "output your content because that would create a feedback loop.");
  7166. ImGui::SliderInt("Number of lines", &lines, 1, 20);
  7167. for (int i = 0; i < lines; i++)
  7168. ImGui::Text("%*sThis is line %d", i * 4, "", i); // Pad with space to extend size horizontally
  7169. ImGui::End();
  7170. }
  7171. //-----------------------------------------------------------------------------
  7172. // [SECTION] Example App: Constrained Resize / ShowExampleAppConstrainedResize()
  7173. //-----------------------------------------------------------------------------
  7174. // Demonstrate creating a window with custom resize constraints.
  7175. // Note that size constraints currently don't work on a docked window (when in 'docking' branch)
  7176. static void ShowExampleAppConstrainedResize(bool* p_open)
  7177. {
  7178. struct CustomConstraints
  7179. {
  7180. // Helper functions to demonstrate programmatic constraints
  7181. // FIXME: This doesn't take account of decoration size (e.g. title bar), library should make this easier.
  7182. // FIXME: None of the three demos works consistently when resizing from borders.
  7183. static void AspectRatio(ImGuiSizeCallbackData* data)
  7184. {
  7185. float aspect_ratio = *(float*)data->UserData;
  7186. data->DesiredSize.y = (float)(int)(data->DesiredSize.x / aspect_ratio);
  7187. }
  7188. static void Square(ImGuiSizeCallbackData* data)
  7189. {
  7190. data->DesiredSize.x = data->DesiredSize.y = IM_MAX(data->DesiredSize.x, data->DesiredSize.y);
  7191. }
  7192. static void Step(ImGuiSizeCallbackData* data)
  7193. {
  7194. float step = *(float*)data->UserData;
  7195. data->DesiredSize = ImVec2((int)(data->DesiredSize.x / step + 0.5f) * step, (int)(data->DesiredSize.y / step + 0.5f) * step);
  7196. }
  7197. };
  7198. const char* test_desc[] =
  7199. {
  7200. "Between 100x100 and 500x500",
  7201. "At least 100x100",
  7202. "Resize vertical + lock current width",
  7203. "Resize horizontal + lock current height",
  7204. "Width Between 400 and 500",
  7205. "Height at least 400",
  7206. "Custom: Aspect Ratio 16:9",
  7207. "Custom: Always Square",
  7208. "Custom: Fixed Steps (100)",
  7209. };
  7210. // Options
  7211. static bool auto_resize = false;
  7212. static bool window_padding = true;
  7213. static int type = 6; // Aspect Ratio
  7214. static int display_lines = 10;
  7215. // Submit constraint
  7216. float aspect_ratio = 16.0f / 9.0f;
  7217. float fixed_step = 100.0f;
  7218. if (type == 0) ImGui::SetNextWindowSizeConstraints(ImVec2(100, 100), ImVec2(500, 500)); // Between 100x100 and 500x500
  7219. if (type == 1) ImGui::SetNextWindowSizeConstraints(ImVec2(100, 100), ImVec2(FLT_MAX, FLT_MAX)); // Width > 100, Height > 100
  7220. if (type == 2) ImGui::SetNextWindowSizeConstraints(ImVec2(-1, 0), ImVec2(-1, FLT_MAX)); // Resize vertical + lock current width
  7221. if (type == 3) ImGui::SetNextWindowSizeConstraints(ImVec2(0, -1), ImVec2(FLT_MAX, -1)); // Resize horizontal + lock current height
  7222. if (type == 4) ImGui::SetNextWindowSizeConstraints(ImVec2(400, -1), ImVec2(500, -1)); // Width Between and 400 and 500
  7223. if (type == 5) ImGui::SetNextWindowSizeConstraints(ImVec2(-1, 400), ImVec2(-1, FLT_MAX)); // Height at least 400
  7224. if (type == 6) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::AspectRatio, (void*)&aspect_ratio); // Aspect ratio
  7225. if (type == 7) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Square); // Always Square
  7226. if (type == 8) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Step, (void*)&fixed_step); // Fixed Step
  7227. // Submit window
  7228. if (!window_padding)
  7229. ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
  7230. const ImGuiWindowFlags window_flags = auto_resize ? ImGuiWindowFlags_AlwaysAutoResize : 0;
  7231. const bool window_open = ImGui::Begin("Example: Constrained Resize", p_open, window_flags);
  7232. if (!window_padding)
  7233. ImGui::PopStyleVar();
  7234. if (window_open)
  7235. {
  7236. IMGUI_DEMO_MARKER("Examples/Constrained Resizing window");
  7237. if (ImGui::GetIO().KeyShift)
  7238. {
  7239. // Display a dummy viewport (in your real app you would likely use ImageButton() to display a texture.
  7240. ImVec2 avail_size = ImGui::GetContentRegionAvail();
  7241. ImVec2 pos = ImGui::GetCursorScreenPos();
  7242. ImGui::ColorButton("viewport", ImVec4(0.5f, 0.2f, 0.5f, 1.0f), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoDragDrop, avail_size);
  7243. ImGui::SetCursorScreenPos(ImVec2(pos.x + 10, pos.y + 10));
  7244. ImGui::Text("%.2f x %.2f", avail_size.x, avail_size.y);
  7245. }
  7246. else
  7247. {
  7248. ImGui::Text("(Hold SHIFT to display a dummy viewport)");
  7249. if (ImGui::Button("Set 200x200")) { ImGui::SetWindowSize(ImVec2(200, 200)); } ImGui::SameLine();
  7250. if (ImGui::Button("Set 500x500")) { ImGui::SetWindowSize(ImVec2(500, 500)); } ImGui::SameLine();
  7251. if (ImGui::Button("Set 800x200")) { ImGui::SetWindowSize(ImVec2(800, 200)); }
  7252. ImGui::SetNextItemWidth(ImGui::GetFontSize() * 20);
  7253. ImGui::Combo("Constraint", &type, test_desc, IM_ARRAYSIZE(test_desc));
  7254. ImGui::SetNextItemWidth(ImGui::GetFontSize() * 20);
  7255. ImGui::DragInt("Lines", &display_lines, 0.2f, 1, 100);
  7256. ImGui::Checkbox("Auto-resize", &auto_resize);
  7257. ImGui::Checkbox("Window padding", &window_padding);
  7258. for (int i = 0; i < display_lines; i++)
  7259. ImGui::Text("%*sHello, sailor! Making this line long enough for the example.", i * 4, "");
  7260. }
  7261. }
  7262. ImGui::End();
  7263. }
  7264. //-----------------------------------------------------------------------------
  7265. // [SECTION] Example App: Simple overlay / ShowExampleAppSimpleOverlay()
  7266. //-----------------------------------------------------------------------------
  7267. // Demonstrate creating a simple static window with no decoration
  7268. // + a context-menu to choose which corner of the screen to use.
  7269. static void ShowExampleAppSimpleOverlay(bool* p_open)
  7270. {
  7271. static int location = 0;
  7272. ImGuiIO& io = ImGui::GetIO();
  7273. ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav;
  7274. if (location >= 0)
  7275. {
  7276. const float PAD = 10.0f;
  7277. const ImGuiViewport* viewport = ImGui::GetMainViewport();
  7278. ImVec2 work_pos = viewport->WorkPos; // Use work area to avoid menu-bar/task-bar, if any!
  7279. ImVec2 work_size = viewport->WorkSize;
  7280. ImVec2 window_pos, window_pos_pivot;
  7281. window_pos.x = (location & 1) ? (work_pos.x + work_size.x - PAD) : (work_pos.x + PAD);
  7282. window_pos.y = (location & 2) ? (work_pos.y + work_size.y - PAD) : (work_pos.y + PAD);
  7283. window_pos_pivot.x = (location & 1) ? 1.0f : 0.0f;
  7284. window_pos_pivot.y = (location & 2) ? 1.0f : 0.0f;
  7285. ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always, window_pos_pivot);
  7286. window_flags |= ImGuiWindowFlags_NoMove;
  7287. }
  7288. else if (location == -2)
  7289. {
  7290. // Center window
  7291. ImGui::SetNextWindowPos(ImGui::GetMainViewport()->GetCenter(), ImGuiCond_Always, ImVec2(0.5f, 0.5f));
  7292. window_flags |= ImGuiWindowFlags_NoMove;
  7293. }
  7294. ImGui::SetNextWindowBgAlpha(0.35f); // Transparent background
  7295. if (ImGui::Begin("Example: Simple overlay", p_open, window_flags))
  7296. {
  7297. IMGUI_DEMO_MARKER("Examples/Simple Overlay");
  7298. ImGui::Text("Simple overlay\n" "(right-click to change position)");
  7299. ImGui::Separator();
  7300. if (ImGui::IsMousePosValid())
  7301. ImGui::Text("Mouse Position: (%.1f,%.1f)", io.MousePos.x, io.MousePos.y);
  7302. else
  7303. ImGui::Text("Mouse Position: <invalid>");
  7304. if (ImGui::BeginPopupContextWindow())
  7305. {
  7306. if (ImGui::MenuItem("Custom", NULL, location == -1)) location = -1;
  7307. if (ImGui::MenuItem("Center", NULL, location == -2)) location = -2;
  7308. if (ImGui::MenuItem("Top-left", NULL, location == 0)) location = 0;
  7309. if (ImGui::MenuItem("Top-right", NULL, location == 1)) location = 1;
  7310. if (ImGui::MenuItem("Bottom-left", NULL, location == 2)) location = 2;
  7311. if (ImGui::MenuItem("Bottom-right", NULL, location == 3)) location = 3;
  7312. if (p_open && ImGui::MenuItem("Close")) *p_open = false;
  7313. ImGui::EndPopup();
  7314. }
  7315. }
  7316. ImGui::End();
  7317. }
  7318. //-----------------------------------------------------------------------------
  7319. // [SECTION] Example App: Fullscreen window / ShowExampleAppFullscreen()
  7320. //-----------------------------------------------------------------------------
  7321. // Demonstrate creating a window covering the entire screen/viewport
  7322. static void ShowExampleAppFullscreen(bool* p_open)
  7323. {
  7324. static bool use_work_area = true;
  7325. static ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings;
  7326. // We demonstrate using the full viewport area or the work area (without menu-bars, task-bars etc.)
  7327. // Based on your use case you may want one or the other.
  7328. const ImGuiViewport* viewport = ImGui::GetMainViewport();
  7329. ImGui::SetNextWindowPos(use_work_area ? viewport->WorkPos : viewport->Pos);
  7330. ImGui::SetNextWindowSize(use_work_area ? viewport->WorkSize : viewport->Size);
  7331. if (ImGui::Begin("Example: Fullscreen window", p_open, flags))
  7332. {
  7333. ImGui::Checkbox("Use work area instead of main area", &use_work_area);
  7334. ImGui::SameLine();
  7335. HelpMarker("Main Area = entire viewport,\nWork Area = entire viewport minus sections used by the main menu bars, task bars etc.\n\nEnable the main-menu bar in Examples menu to see the difference.");
  7336. ImGui::CheckboxFlags("ImGuiWindowFlags_NoBackground", &flags, ImGuiWindowFlags_NoBackground);
  7337. ImGui::CheckboxFlags("ImGuiWindowFlags_NoDecoration", &flags, ImGuiWindowFlags_NoDecoration);
  7338. ImGui::Indent();
  7339. ImGui::CheckboxFlags("ImGuiWindowFlags_NoTitleBar", &flags, ImGuiWindowFlags_NoTitleBar);
  7340. ImGui::CheckboxFlags("ImGuiWindowFlags_NoCollapse", &flags, ImGuiWindowFlags_NoCollapse);
  7341. ImGui::CheckboxFlags("ImGuiWindowFlags_NoScrollbar", &flags, ImGuiWindowFlags_NoScrollbar);
  7342. ImGui::Unindent();
  7343. if (p_open && ImGui::Button("Close this window"))
  7344. *p_open = false;
  7345. }
  7346. ImGui::End();
  7347. }
  7348. //-----------------------------------------------------------------------------
  7349. // [SECTION] Example App: Manipulating Window Titles / ShowExampleAppWindowTitles()
  7350. //-----------------------------------------------------------------------------
  7351. // Demonstrate the use of "##" and "###" in identifiers to manipulate ID generation.
  7352. // This applies to all regular items as well.
  7353. // Read FAQ section "How can I have multiple widgets with the same label?" for details.
  7354. static void ShowExampleAppWindowTitles(bool*)
  7355. {
  7356. const ImGuiViewport* viewport = ImGui::GetMainViewport();
  7357. const ImVec2 base_pos = viewport->Pos;
  7358. // By default, Windows are uniquely identified by their title.
  7359. // You can use the "##" and "###" markers to manipulate the display/ID.
  7360. // Using "##" to display same title but have unique identifier.
  7361. ImGui::SetNextWindowPos(ImVec2(base_pos.x + 100, base_pos.y + 100), ImGuiCond_FirstUseEver);
  7362. ImGui::Begin("Same title as another window##1");
  7363. IMGUI_DEMO_MARKER("Examples/Manipulating window titles");
  7364. ImGui::Text("This is window 1.\nMy title is the same as window 2, but my identifier is unique.");
  7365. ImGui::End();
  7366. ImGui::SetNextWindowPos(ImVec2(base_pos.x + 100, base_pos.y + 200), ImGuiCond_FirstUseEver);
  7367. ImGui::Begin("Same title as another window##2");
  7368. ImGui::Text("This is window 2.\nMy title is the same as window 1, but my identifier is unique.");
  7369. ImGui::End();
  7370. // Using "###" to display a changing title but keep a static identifier "AnimatedTitle"
  7371. char buf[128];
  7372. sprintf(buf, "Animated title %c %d###AnimatedTitle", "|/-\\"[(int)(ImGui::GetTime() / 0.25f) & 3], ImGui::GetFrameCount());
  7373. ImGui::SetNextWindowPos(ImVec2(base_pos.x + 100, base_pos.y + 300), ImGuiCond_FirstUseEver);
  7374. ImGui::Begin(buf);
  7375. ImGui::Text("This window has a changing title.");
  7376. ImGui::End();
  7377. }
  7378. //-----------------------------------------------------------------------------
  7379. // [SECTION] Example App: Custom Rendering using ImDrawList API / ShowExampleAppCustomRendering()
  7380. //-----------------------------------------------------------------------------
  7381. // Add a |_| looking shape
  7382. static void PathConcaveShape(ImDrawList* draw_list, float x, float y, float sz)
  7383. {
  7384. const ImVec2 pos_norms[] = { { 0.0f, 0.0f }, { 0.3f, 0.0f }, { 0.3f, 0.7f }, { 0.7f, 0.7f }, { 0.7f, 0.0f }, { 1.0f, 0.0f }, { 1.0f, 1.0f }, { 0.0f, 1.0f } };
  7385. for (const ImVec2& p : pos_norms)
  7386. draw_list->PathLineTo(ImVec2(x + 0.5f + (int)(sz * p.x), y + 0.5f + (int)(sz * p.y)));
  7387. }
  7388. // Demonstrate using the low-level ImDrawList to draw custom shapes.
  7389. static void ShowExampleAppCustomRendering(bool* p_open)
  7390. {
  7391. if (!ImGui::Begin("Example: Custom rendering", p_open))
  7392. {
  7393. ImGui::End();
  7394. return;
  7395. }
  7396. IMGUI_DEMO_MARKER("Examples/Custom Rendering");
  7397. // Tip: If you do a lot of custom rendering, you probably want to use your own geometrical types and benefit of
  7398. // overloaded operators, etc. Define IM_VEC2_CLASS_EXTRA in imconfig.h to create implicit conversions between your
  7399. // types and ImVec2/ImVec4. Dear ImGui defines overloaded operators but they are internal to imgui.cpp and not
  7400. // exposed outside (to avoid messing with your types) In this example we are not using the maths operators!
  7401. if (ImGui::BeginTabBar("##TabBar"))
  7402. {
  7403. if (ImGui::BeginTabItem("Primitives"))
  7404. {
  7405. ImGui::PushItemWidth(-ImGui::GetFontSize() * 15);
  7406. ImDrawList* draw_list = ImGui::GetWindowDrawList();
  7407. // Draw gradients
  7408. // (note that those are currently exacerbating our sRGB/Linear issues)
  7409. // Calling ImGui::GetColorU32() multiplies the given colors by the current Style Alpha, but you may pass the IM_COL32() directly as well..
  7410. ImGui::Text("Gradients");
  7411. ImVec2 gradient_size = ImVec2(ImGui::CalcItemWidth(), ImGui::GetFrameHeight());
  7412. {
  7413. ImVec2 p0 = ImGui::GetCursorScreenPos();
  7414. ImVec2 p1 = ImVec2(p0.x + gradient_size.x, p0.y + gradient_size.y);
  7415. ImU32 col_a = ImGui::GetColorU32(IM_COL32(0, 0, 0, 255));
  7416. ImU32 col_b = ImGui::GetColorU32(IM_COL32(255, 255, 255, 255));
  7417. draw_list->AddRectFilledMultiColor(p0, p1, col_a, col_b, col_b, col_a);
  7418. ImGui::InvisibleButton("##gradient1", gradient_size);
  7419. }
  7420. {
  7421. ImVec2 p0 = ImGui::GetCursorScreenPos();
  7422. ImVec2 p1 = ImVec2(p0.x + gradient_size.x, p0.y + gradient_size.y);
  7423. ImU32 col_a = ImGui::GetColorU32(IM_COL32(0, 255, 0, 255));
  7424. ImU32 col_b = ImGui::GetColorU32(IM_COL32(255, 0, 0, 255));
  7425. draw_list->AddRectFilledMultiColor(p0, p1, col_a, col_b, col_b, col_a);
  7426. ImGui::InvisibleButton("##gradient2", gradient_size);
  7427. }
  7428. // Draw a bunch of primitives
  7429. ImGui::Text("All primitives");
  7430. static float sz = 36.0f;
  7431. static float thickness = 3.0f;
  7432. static int ngon_sides = 6;
  7433. static bool circle_segments_override = false;
  7434. static int circle_segments_override_v = 12;
  7435. static bool curve_segments_override = false;
  7436. static int curve_segments_override_v = 8;
  7437. static ImVec4 colf = ImVec4(1.0f, 1.0f, 0.4f, 1.0f);
  7438. ImGui::DragFloat("Size", &sz, 0.2f, 2.0f, 100.0f, "%.0f");
  7439. ImGui::DragFloat("Thickness", &thickness, 0.05f, 1.0f, 8.0f, "%.02f");
  7440. ImGui::SliderInt("N-gon sides", &ngon_sides, 3, 12);
  7441. ImGui::Checkbox("##circlesegmentoverride", &circle_segments_override);
  7442. ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x);
  7443. circle_segments_override |= ImGui::SliderInt("Circle segments override", &circle_segments_override_v, 3, 40);
  7444. ImGui::Checkbox("##curvessegmentoverride", &curve_segments_override);
  7445. ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x);
  7446. curve_segments_override |= ImGui::SliderInt("Curves segments override", &curve_segments_override_v, 3, 40);
  7447. ImGui::ColorEdit4("Color", &colf.x);
  7448. const ImVec2 p = ImGui::GetCursorScreenPos();
  7449. const ImU32 col = ImColor(colf);
  7450. const float spacing = 10.0f;
  7451. const ImDrawFlags corners_tl_br = ImDrawFlags_RoundCornersTopLeft | ImDrawFlags_RoundCornersBottomRight;
  7452. const float rounding = sz / 5.0f;
  7453. const int circle_segments = circle_segments_override ? circle_segments_override_v : 0;
  7454. const int curve_segments = curve_segments_override ? curve_segments_override_v : 0;
  7455. const ImVec2 cp3[3] = { ImVec2(0.0f, sz * 0.6f), ImVec2(sz * 0.5f, -sz * 0.4f), ImVec2(sz, sz) }; // Control points for curves
  7456. const ImVec2 cp4[4] = { ImVec2(0.0f, 0.0f), ImVec2(sz * 1.3f, sz * 0.3f), ImVec2(sz - sz * 1.3f, sz - sz * 0.3f), ImVec2(sz, sz) };
  7457. float x = p.x + 4.0f;
  7458. float y = p.y + 4.0f;
  7459. for (int n = 0; n < 2; n++)
  7460. {
  7461. // First line uses a thickness of 1.0f, second line uses the configurable thickness
  7462. float th = (n == 0) ? 1.0f : thickness;
  7463. draw_list->AddNgon(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, ngon_sides, th); x += sz + spacing; // N-gon
  7464. draw_list->AddCircle(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, circle_segments, th); x += sz + spacing; // Circle
  7465. draw_list->AddEllipse(ImVec2(x + sz*0.5f, y + sz*0.5f), ImVec2(sz*0.5f, sz*0.3f), col, -0.3f, circle_segments, th); x += sz + spacing; // Ellipse
  7466. draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 0.0f, ImDrawFlags_None, th); x += sz + spacing; // Square
  7467. draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, rounding, ImDrawFlags_None, th); x += sz + spacing; // Square with all rounded corners
  7468. draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, rounding, corners_tl_br, th); x += sz + spacing; // Square with two rounded corners
  7469. draw_list->AddTriangle(ImVec2(x+sz*0.5f,y), ImVec2(x+sz, y+sz-0.5f), ImVec2(x, y+sz-0.5f), col, th);x += sz + spacing; // Triangle
  7470. //draw_list->AddTriangle(ImVec2(x+sz*0.2f,y), ImVec2(x, y+sz-0.5f), ImVec2(x+sz*0.4f, y+sz-0.5f), col, th);x+= sz*0.4f + spacing; // Thin triangle
  7471. PathConcaveShape(draw_list, x, y, sz); draw_list->PathStroke(col, ImDrawFlags_Closed, th); x += sz + spacing; // Concave Shape
  7472. //draw_list->AddPolyline(concave_shape, IM_ARRAYSIZE(concave_shape), col, ImDrawFlags_Closed, th);
  7473. draw_list->AddLine(ImVec2(x, y), ImVec2(x + sz, y), col, th); x += sz + spacing; // Horizontal line (note: drawing a filled rectangle will be faster!)
  7474. draw_list->AddLine(ImVec2(x, y), ImVec2(x, y + sz), col, th); x += spacing; // Vertical line (note: drawing a filled rectangle will be faster!)
  7475. draw_list->AddLine(ImVec2(x, y), ImVec2(x + sz, y + sz), col, th); x += sz + spacing; // Diagonal line
  7476. // Path
  7477. draw_list->PathArcTo(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, 3.141592f, 3.141592f * -0.5f);
  7478. draw_list->PathStroke(col, ImDrawFlags_None, th);
  7479. x += sz + spacing;
  7480. // Quadratic Bezier Curve (3 control points)
  7481. draw_list->AddBezierQuadratic(ImVec2(x + cp3[0].x, y + cp3[0].y), ImVec2(x + cp3[1].x, y + cp3[1].y), ImVec2(x + cp3[2].x, y + cp3[2].y), col, th, curve_segments);
  7482. x += sz + spacing;
  7483. // Cubic Bezier Curve (4 control points)
  7484. draw_list->AddBezierCubic(ImVec2(x + cp4[0].x, y + cp4[0].y), ImVec2(x + cp4[1].x, y + cp4[1].y), ImVec2(x + cp4[2].x, y + cp4[2].y), ImVec2(x + cp4[3].x, y + cp4[3].y), col, th, curve_segments);
  7485. x = p.x + 4;
  7486. y += sz + spacing;
  7487. }
  7488. // Filled shapes
  7489. draw_list->AddNgonFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz * 0.5f, col, ngon_sides); x += sz + spacing; // N-gon
  7490. draw_list->AddCircleFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz * 0.5f, col, circle_segments); x += sz + spacing; // Circle
  7491. draw_list->AddEllipseFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), ImVec2(sz * 0.5f, sz * 0.3f), col, -0.3f, circle_segments); x += sz + spacing;// Ellipse
  7492. draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col); x += sz + spacing; // Square
  7493. draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f); x += sz + spacing; // Square with all rounded corners
  7494. draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_tl_br); x += sz + spacing; // Square with two rounded corners
  7495. draw_list->AddTriangleFilled(ImVec2(x+sz*0.5f,y), ImVec2(x+sz, y+sz-0.5f), ImVec2(x, y+sz-0.5f), col); x += sz + spacing; // Triangle
  7496. //draw_list->AddTriangleFilled(ImVec2(x+sz*0.2f,y), ImVec2(x, y+sz-0.5f), ImVec2(x+sz*0.4f, y+sz-0.5f), col); x += sz*0.4f + spacing; // Thin triangle
  7497. PathConcaveShape(draw_list, x, y, sz); draw_list->PathFillConcave(col); x += sz + spacing; // Concave shape
  7498. draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + thickness), col); x += sz + spacing; // Horizontal line (faster than AddLine, but only handle integer thickness)
  7499. draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + thickness, y + sz), col); x += spacing * 2.0f;// Vertical line (faster than AddLine, but only handle integer thickness)
  7500. draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + 1, y + 1), col); x += sz; // Pixel (faster than AddLine)
  7501. // Path
  7502. draw_list->PathArcTo(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz * 0.5f, 3.141592f * -0.5f, 3.141592f);
  7503. draw_list->PathFillConvex(col);
  7504. x += sz + spacing;
  7505. // Quadratic Bezier Curve (3 control points)
  7506. draw_list->PathLineTo(ImVec2(x + cp3[0].x, y + cp3[0].y));
  7507. draw_list->PathBezierQuadraticCurveTo(ImVec2(x + cp3[1].x, y + cp3[1].y), ImVec2(x + cp3[2].x, y + cp3[2].y), curve_segments);
  7508. draw_list->PathFillConvex(col);
  7509. x += sz + spacing;
  7510. draw_list->AddRectFilledMultiColor(ImVec2(x, y), ImVec2(x + sz, y + sz), IM_COL32(0, 0, 0, 255), IM_COL32(255, 0, 0, 255), IM_COL32(255, 255, 0, 255), IM_COL32(0, 255, 0, 255));
  7511. x += sz + spacing;
  7512. ImGui::Dummy(ImVec2((sz + spacing) * 13.2f, (sz + spacing) * 3.0f));
  7513. ImGui::PopItemWidth();
  7514. ImGui::EndTabItem();
  7515. }
  7516. if (ImGui::BeginTabItem("Canvas"))
  7517. {
  7518. static ImVector<ImVec2> points;
  7519. static ImVec2 scrolling(0.0f, 0.0f);
  7520. static bool opt_enable_grid = true;
  7521. static bool opt_enable_context_menu = true;
  7522. static bool adding_line = false;
  7523. ImGui::Checkbox("Enable grid", &opt_enable_grid);
  7524. ImGui::Checkbox("Enable context menu", &opt_enable_context_menu);
  7525. ImGui::Text("Mouse Left: drag to add lines,\nMouse Right: drag to scroll, click for context menu.");
  7526. // Typically you would use a BeginChild()/EndChild() pair to benefit from a clipping region + own scrolling.
  7527. // Here we demonstrate that this can be replaced by simple offsetting + custom drawing + PushClipRect/PopClipRect() calls.
  7528. // To use a child window instead we could use, e.g:
  7529. // ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); // Disable padding
  7530. // ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(50, 50, 50, 255)); // Set a background color
  7531. // ImGui::BeginChild("canvas", ImVec2(0.0f, 0.0f), ImGuiChildFlags_Border, ImGuiWindowFlags_NoMove);
  7532. // ImGui::PopStyleColor();
  7533. // ImGui::PopStyleVar();
  7534. // [...]
  7535. // ImGui::EndChild();
  7536. // Using InvisibleButton() as a convenience 1) it will advance the layout cursor and 2) allows us to use IsItemHovered()/IsItemActive()
  7537. ImVec2 canvas_p0 = ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates!
  7538. ImVec2 canvas_sz = ImGui::GetContentRegionAvail(); // Resize canvas to what's available
  7539. if (canvas_sz.x < 50.0f) canvas_sz.x = 50.0f;
  7540. if (canvas_sz.y < 50.0f) canvas_sz.y = 50.0f;
  7541. ImVec2 canvas_p1 = ImVec2(canvas_p0.x + canvas_sz.x, canvas_p0.y + canvas_sz.y);
  7542. // Draw border and background color
  7543. ImGuiIO& io = ImGui::GetIO();
  7544. ImDrawList* draw_list = ImGui::GetWindowDrawList();
  7545. draw_list->AddRectFilled(canvas_p0, canvas_p1, IM_COL32(50, 50, 50, 255));
  7546. draw_list->AddRect(canvas_p0, canvas_p1, IM_COL32(255, 255, 255, 255));
  7547. // This will catch our interactions
  7548. ImGui::InvisibleButton("canvas", canvas_sz, ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight);
  7549. const bool is_hovered = ImGui::IsItemHovered(); // Hovered
  7550. const bool is_active = ImGui::IsItemActive(); // Held
  7551. const ImVec2 origin(canvas_p0.x + scrolling.x, canvas_p0.y + scrolling.y); // Lock scrolled origin
  7552. const ImVec2 mouse_pos_in_canvas(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
  7553. // Add first and second point
  7554. if (is_hovered && !adding_line && ImGui::IsMouseClicked(ImGuiMouseButton_Left))
  7555. {
  7556. points.push_back(mouse_pos_in_canvas);
  7557. points.push_back(mouse_pos_in_canvas);
  7558. adding_line = true;
  7559. }
  7560. if (adding_line)
  7561. {
  7562. points.back() = mouse_pos_in_canvas;
  7563. if (!ImGui::IsMouseDown(ImGuiMouseButton_Left))
  7564. adding_line = false;
  7565. }
  7566. // Pan (we use a zero mouse threshold when there's no context menu)
  7567. // You may decide to make that threshold dynamic based on whether the mouse is hovering something etc.
  7568. const float mouse_threshold_for_pan = opt_enable_context_menu ? -1.0f : 0.0f;
  7569. if (is_active && ImGui::IsMouseDragging(ImGuiMouseButton_Right, mouse_threshold_for_pan))
  7570. {
  7571. scrolling.x += io.MouseDelta.x;
  7572. scrolling.y += io.MouseDelta.y;
  7573. }
  7574. // Context menu (under default mouse threshold)
  7575. ImVec2 drag_delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Right);
  7576. if (opt_enable_context_menu && drag_delta.x == 0.0f && drag_delta.y == 0.0f)
  7577. ImGui::OpenPopupOnItemClick("context", ImGuiPopupFlags_MouseButtonRight);
  7578. if (ImGui::BeginPopup("context"))
  7579. {
  7580. if (adding_line)
  7581. points.resize(points.size() - 2);
  7582. adding_line = false;
  7583. if (ImGui::MenuItem("Remove one", NULL, false, points.Size > 0)) { points.resize(points.size() - 2); }
  7584. if (ImGui::MenuItem("Remove all", NULL, false, points.Size > 0)) { points.clear(); }
  7585. ImGui::EndPopup();
  7586. }
  7587. // Draw grid + all lines in the canvas
  7588. draw_list->PushClipRect(canvas_p0, canvas_p1, true);
  7589. if (opt_enable_grid)
  7590. {
  7591. const float GRID_STEP = 64.0f;
  7592. for (float x = fmodf(scrolling.x, GRID_STEP); x < canvas_sz.x; x += GRID_STEP)
  7593. draw_list->AddLine(ImVec2(canvas_p0.x + x, canvas_p0.y), ImVec2(canvas_p0.x + x, canvas_p1.y), IM_COL32(200, 200, 200, 40));
  7594. for (float y = fmodf(scrolling.y, GRID_STEP); y < canvas_sz.y; y += GRID_STEP)
  7595. draw_list->AddLine(ImVec2(canvas_p0.x, canvas_p0.y + y), ImVec2(canvas_p1.x, canvas_p0.y + y), IM_COL32(200, 200, 200, 40));
  7596. }
  7597. for (int n = 0; n < points.Size; n += 2)
  7598. draw_list->AddLine(ImVec2(origin.x + points[n].x, origin.y + points[n].y), ImVec2(origin.x + points[n + 1].x, origin.y + points[n + 1].y), IM_COL32(255, 255, 0, 255), 2.0f);
  7599. draw_list->PopClipRect();
  7600. ImGui::EndTabItem();
  7601. }
  7602. if (ImGui::BeginTabItem("BG/FG draw lists"))
  7603. {
  7604. static bool draw_bg = true;
  7605. static bool draw_fg = true;
  7606. ImGui::Checkbox("Draw in Background draw list", &draw_bg);
  7607. ImGui::SameLine(); HelpMarker("The Background draw list will be rendered below every Dear ImGui windows.");
  7608. ImGui::Checkbox("Draw in Foreground draw list", &draw_fg);
  7609. ImGui::SameLine(); HelpMarker("The Foreground draw list will be rendered over every Dear ImGui windows.");
  7610. ImVec2 window_pos = ImGui::GetWindowPos();
  7611. ImVec2 window_size = ImGui::GetWindowSize();
  7612. ImVec2 window_center = ImVec2(window_pos.x + window_size.x * 0.5f, window_pos.y + window_size.y * 0.5f);
  7613. if (draw_bg)
  7614. ImGui::GetBackgroundDrawList()->AddCircle(window_center, window_size.x * 0.6f, IM_COL32(255, 0, 0, 200), 0, 10 + 4);
  7615. if (draw_fg)
  7616. ImGui::GetForegroundDrawList()->AddCircle(window_center, window_size.y * 0.6f, IM_COL32(0, 255, 0, 200), 0, 10);
  7617. ImGui::EndTabItem();
  7618. }
  7619. // Demonstrate out-of-order rendering via channels splitting
  7620. // We use functions in ImDrawList as each draw list contains a convenience splitter,
  7621. // but you can also instantiate your own ImDrawListSplitter if you need to nest them.
  7622. if (ImGui::BeginTabItem("Draw Channels"))
  7623. {
  7624. ImDrawList* draw_list = ImGui::GetWindowDrawList();
  7625. {
  7626. ImGui::Text("Blue shape is drawn first: appears in back");
  7627. ImGui::Text("Red shape is drawn after: appears in front");
  7628. ImVec2 p0 = ImGui::GetCursorScreenPos();
  7629. draw_list->AddRectFilled(ImVec2(p0.x, p0.y), ImVec2(p0.x + 50, p0.y + 50), IM_COL32(0, 0, 255, 255)); // Blue
  7630. draw_list->AddRectFilled(ImVec2(p0.x + 25, p0.y + 25), ImVec2(p0.x + 75, p0.y + 75), IM_COL32(255, 0, 0, 255)); // Red
  7631. ImGui::Dummy(ImVec2(75, 75));
  7632. }
  7633. ImGui::Separator();
  7634. {
  7635. ImGui::Text("Blue shape is drawn first, into channel 1: appears in front");
  7636. ImGui::Text("Red shape is drawn after, into channel 0: appears in back");
  7637. ImVec2 p1 = ImGui::GetCursorScreenPos();
  7638. // Create 2 channels and draw a Blue shape THEN a Red shape.
  7639. // You can create any number of channels. Tables API use 1 channel per column in order to better batch draw calls.
  7640. draw_list->ChannelsSplit(2);
  7641. draw_list->ChannelsSetCurrent(1);
  7642. draw_list->AddRectFilled(ImVec2(p1.x, p1.y), ImVec2(p1.x + 50, p1.y + 50), IM_COL32(0, 0, 255, 255)); // Blue
  7643. draw_list->ChannelsSetCurrent(0);
  7644. draw_list->AddRectFilled(ImVec2(p1.x + 25, p1.y + 25), ImVec2(p1.x + 75, p1.y + 75), IM_COL32(255, 0, 0, 255)); // Red
  7645. // Flatten/reorder channels. Red shape is in channel 0 and it appears below the Blue shape in channel 1.
  7646. // This works by copying draw indices only (vertices are not copied).
  7647. draw_list->ChannelsMerge();
  7648. ImGui::Dummy(ImVec2(75, 75));
  7649. ImGui::Text("After reordering, contents of channel 0 appears below channel 1.");
  7650. }
  7651. ImGui::EndTabItem();
  7652. }
  7653. ImGui::EndTabBar();
  7654. }
  7655. ImGui::End();
  7656. }
  7657. //-----------------------------------------------------------------------------
  7658. // [SECTION] Example App: Documents Handling / ShowExampleAppDocuments()
  7659. //-----------------------------------------------------------------------------
  7660. // Simplified structure to mimic a Document model
  7661. struct MyDocument
  7662. {
  7663. char Name[32]; // Document title
  7664. int UID; // Unique ID (necessary as we can change title)
  7665. bool Open; // Set when open (we keep an array of all available documents to simplify demo code!)
  7666. bool OpenPrev; // Copy of Open from last update.
  7667. bool Dirty; // Set when the document has been modified
  7668. ImVec4 Color; // An arbitrary variable associated to the document
  7669. MyDocument(int uid, const char* name, bool open = true, const ImVec4& color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f))
  7670. {
  7671. UID = uid;
  7672. snprintf(Name, sizeof(Name), "%s", name);
  7673. Open = OpenPrev = open;
  7674. Dirty = false;
  7675. Color = color;
  7676. }
  7677. void DoOpen() { Open = true; }
  7678. void DoForceClose() { Open = false; Dirty = false; }
  7679. void DoSave() { Dirty = false; }
  7680. };
  7681. struct ExampleAppDocuments
  7682. {
  7683. ImVector<MyDocument> Documents;
  7684. ImVector<MyDocument*> CloseQueue;
  7685. MyDocument* RenamingDoc = NULL;
  7686. bool RenamingStarted = false;
  7687. ExampleAppDocuments()
  7688. {
  7689. Documents.push_back(MyDocument(0, "Lettuce", true, ImVec4(0.4f, 0.8f, 0.4f, 1.0f)));
  7690. Documents.push_back(MyDocument(1, "Eggplant", true, ImVec4(0.8f, 0.5f, 1.0f, 1.0f)));
  7691. Documents.push_back(MyDocument(2, "Carrot", true, ImVec4(1.0f, 0.8f, 0.5f, 1.0f)));
  7692. Documents.push_back(MyDocument(3, "Tomato", false, ImVec4(1.0f, 0.3f, 0.4f, 1.0f)));
  7693. Documents.push_back(MyDocument(4, "A Rather Long Title", false, ImVec4(0.4f, 0.8f, 0.8f, 1.0f)));
  7694. Documents.push_back(MyDocument(5, "Some Document", false, ImVec4(0.8f, 0.8f, 1.0f, 1.0f)));
  7695. }
  7696. // As we allow to change document name, we append a never-changing document ID so tabs are stable
  7697. void GetTabName(MyDocument* doc, char* out_buf, size_t out_buf_size)
  7698. {
  7699. snprintf(out_buf, out_buf_size, "%s###doc%d", doc->Name, doc->UID);
  7700. }
  7701. // Display placeholder contents for the Document
  7702. void DisplayDocContents(MyDocument* doc)
  7703. {
  7704. ImGui::PushID(doc);
  7705. ImGui::Text("Document \"%s\"", doc->Name);
  7706. ImGui::PushStyleColor(ImGuiCol_Text, doc->Color);
  7707. ImGui::TextWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
  7708. ImGui::PopStyleColor();
  7709. ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_R, ImGuiInputFlags_Tooltip);
  7710. if (ImGui::Button("Rename.."))
  7711. {
  7712. RenamingDoc = doc;
  7713. RenamingStarted = true;
  7714. }
  7715. ImGui::SameLine();
  7716. ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_M, ImGuiInputFlags_Tooltip);
  7717. if (ImGui::Button("Modify"))
  7718. doc->Dirty = true;
  7719. ImGui::SameLine();
  7720. ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_S, ImGuiInputFlags_Tooltip);
  7721. if (ImGui::Button("Save"))
  7722. doc->DoSave();
  7723. ImGui::SameLine();
  7724. ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_W, ImGuiInputFlags_Tooltip);
  7725. if (ImGui::Button("Close"))
  7726. CloseQueue.push_back(doc);
  7727. ImGui::ColorEdit3("color", &doc->Color.x); // Useful to test drag and drop and hold-dragged-to-open-tab behavior.
  7728. ImGui::PopID();
  7729. }
  7730. // Display context menu for the Document
  7731. void DisplayDocContextMenu(MyDocument* doc)
  7732. {
  7733. if (!ImGui::BeginPopupContextItem())
  7734. return;
  7735. char buf[256];
  7736. sprintf(buf, "Save %s", doc->Name);
  7737. if (ImGui::MenuItem(buf, "Ctrl+S", false, doc->Open))
  7738. doc->DoSave();
  7739. if (ImGui::MenuItem("Rename...", "Ctrl+R", false, doc->Open))
  7740. RenamingDoc = doc;
  7741. if (ImGui::MenuItem("Close", "Ctrl+W", false, doc->Open))
  7742. CloseQueue.push_back(doc);
  7743. ImGui::EndPopup();
  7744. }
  7745. // [Optional] Notify the system of Tabs/Windows closure that happened outside the regular tab interface.
  7746. // If a tab has been closed programmatically (aka closed from another source such as the Checkbox() in the demo,
  7747. // as opposed to clicking on the regular tab closing button) and stops being submitted, it will take a frame for
  7748. // the tab bar to notice its absence. During this frame there will be a gap in the tab bar, and if the tab that has
  7749. // disappeared was the selected one, the tab bar will report no selected tab during the frame. This will effectively
  7750. // give the impression of a flicker for one frame.
  7751. // We call SetTabItemClosed() to manually notify the Tab Bar or Docking system of removed tabs to avoid this glitch.
  7752. // Note that this completely optional, and only affect tab bars with the ImGuiTabBarFlags_Reorderable flag.
  7753. void NotifyOfDocumentsClosedElsewhere()
  7754. {
  7755. for (MyDocument& doc : Documents)
  7756. {
  7757. if (!doc.Open && doc.OpenPrev)
  7758. ImGui::SetTabItemClosed(doc.Name);
  7759. doc.OpenPrev = doc.Open;
  7760. }
  7761. }
  7762. };
  7763. void ShowExampleAppDocuments(bool* p_open)
  7764. {
  7765. static ExampleAppDocuments app;
  7766. // Options
  7767. static bool opt_reorderable = true;
  7768. static ImGuiTabBarFlags opt_fitting_flags = ImGuiTabBarFlags_FittingPolicyDefault_;
  7769. bool window_contents_visible = ImGui::Begin("Example: Documents", p_open, ImGuiWindowFlags_MenuBar);
  7770. if (!window_contents_visible)
  7771. {
  7772. ImGui::End();
  7773. return;
  7774. }
  7775. // Menu
  7776. if (ImGui::BeginMenuBar())
  7777. {
  7778. if (ImGui::BeginMenu("File"))
  7779. {
  7780. int open_count = 0;
  7781. for (MyDocument& doc : app.Documents)
  7782. open_count += doc.Open ? 1 : 0;
  7783. if (ImGui::BeginMenu("Open", open_count < app.Documents.Size))
  7784. {
  7785. for (MyDocument& doc : app.Documents)
  7786. if (!doc.Open && ImGui::MenuItem(doc.Name))
  7787. doc.DoOpen();
  7788. ImGui::EndMenu();
  7789. }
  7790. if (ImGui::MenuItem("Close All Documents", NULL, false, open_count > 0))
  7791. for (MyDocument& doc : app.Documents)
  7792. app.CloseQueue.push_back(&doc);
  7793. if (ImGui::MenuItem("Exit") && p_open)
  7794. *p_open = false;
  7795. ImGui::EndMenu();
  7796. }
  7797. ImGui::EndMenuBar();
  7798. }
  7799. // [Debug] List documents with one checkbox for each
  7800. for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++)
  7801. {
  7802. MyDocument& doc = app.Documents[doc_n];
  7803. if (doc_n > 0)
  7804. ImGui::SameLine();
  7805. ImGui::PushID(&doc);
  7806. if (ImGui::Checkbox(doc.Name, &doc.Open))
  7807. if (!doc.Open)
  7808. doc.DoForceClose();
  7809. ImGui::PopID();
  7810. }
  7811. ImGui::Separator();
  7812. // About the ImGuiWindowFlags_UnsavedDocument / ImGuiTabItemFlags_UnsavedDocument flags.
  7813. // They have multiple effects:
  7814. // - Display a dot next to the title.
  7815. // - Tab is selected when clicking the X close button.
  7816. // - Closure is not assumed (will wait for user to stop submitting the tab).
  7817. // Otherwise closure is assumed when pressing the X, so if you keep submitting the tab may reappear at end of tab bar.
  7818. // We need to assume closure by default otherwise waiting for "lack of submission" on the next frame would leave an empty
  7819. // hole for one-frame, both in the tab-bar and in tab-contents when closing a tab/window.
  7820. // The rarely used SetTabItemClosed() function is a way to notify of programmatic closure to avoid the one-frame hole.
  7821. // Submit Tab Bar and Tabs
  7822. {
  7823. ImGuiTabBarFlags tab_bar_flags = (opt_fitting_flags) | (opt_reorderable ? ImGuiTabBarFlags_Reorderable : 0);
  7824. if (ImGui::BeginTabBar("##tabs", tab_bar_flags))
  7825. {
  7826. if (opt_reorderable)
  7827. app.NotifyOfDocumentsClosedElsewhere();
  7828. // [DEBUG] Stress tests
  7829. //if ((ImGui::GetFrameCount() % 30) == 0) docs[1].Open ^= 1; // [DEBUG] Automatically show/hide a tab. Test various interactions e.g. dragging with this on.
  7830. //if (ImGui::GetIO().KeyCtrl) ImGui::SetTabItemSelected(docs[1].Name); // [DEBUG] Test SetTabItemSelected(), probably not very useful as-is anyway..
  7831. // Submit Tabs
  7832. for (MyDocument& doc : app.Documents)
  7833. {
  7834. if (!doc.Open)
  7835. continue;
  7836. // As we allow to change document name, we append a never-changing document id so tabs are stable
  7837. char doc_name_buf[64];
  7838. app.GetTabName(&doc, doc_name_buf, sizeof(doc_name_buf));
  7839. ImGuiTabItemFlags tab_flags = (doc.Dirty ? ImGuiTabItemFlags_UnsavedDocument : 0);
  7840. bool visible = ImGui::BeginTabItem(doc_name_buf, &doc.Open, tab_flags);
  7841. // Cancel attempt to close when unsaved add to save queue so we can display a popup.
  7842. if (!doc.Open && doc.Dirty)
  7843. {
  7844. doc.Open = true;
  7845. app.CloseQueue.push_back(&doc);
  7846. }
  7847. app.DisplayDocContextMenu(&doc);
  7848. if (visible)
  7849. {
  7850. app.DisplayDocContents(&doc);
  7851. ImGui::EndTabItem();
  7852. }
  7853. }
  7854. ImGui::EndTabBar();
  7855. }
  7856. }
  7857. // Display renaming UI
  7858. if (app.RenamingDoc != NULL)
  7859. {
  7860. if (app.RenamingStarted)
  7861. ImGui::OpenPopup("Rename");
  7862. if (ImGui::BeginPopup("Rename"))
  7863. {
  7864. ImGui::SetNextItemWidth(ImGui::GetFontSize() * 30);
  7865. if (ImGui::InputText("###Name", app.RenamingDoc->Name, IM_ARRAYSIZE(app.RenamingDoc->Name), ImGuiInputTextFlags_EnterReturnsTrue))
  7866. {
  7867. ImGui::CloseCurrentPopup();
  7868. app.RenamingDoc = NULL;
  7869. }
  7870. if (app.RenamingStarted)
  7871. ImGui::SetKeyboardFocusHere(-1);
  7872. ImGui::EndPopup();
  7873. }
  7874. else
  7875. {
  7876. app.RenamingDoc = NULL;
  7877. }
  7878. app.RenamingStarted = false;
  7879. }
  7880. // Display closing confirmation UI
  7881. if (!app.CloseQueue.empty())
  7882. {
  7883. int close_queue_unsaved_documents = 0;
  7884. for (int n = 0; n < app.CloseQueue.Size; n++)
  7885. if (app.CloseQueue[n]->Dirty)
  7886. close_queue_unsaved_documents++;
  7887. if (close_queue_unsaved_documents == 0)
  7888. {
  7889. // Close documents when all are unsaved
  7890. for (int n = 0; n < app.CloseQueue.Size; n++)
  7891. app.CloseQueue[n]->DoForceClose();
  7892. app.CloseQueue.clear();
  7893. }
  7894. else
  7895. {
  7896. if (!ImGui::IsPopupOpen("Save?"))
  7897. ImGui::OpenPopup("Save?");
  7898. if (ImGui::BeginPopupModal("Save?", NULL, ImGuiWindowFlags_AlwaysAutoResize))
  7899. {
  7900. ImGui::Text("Save change to the following items?");
  7901. float item_height = ImGui::GetTextLineHeightWithSpacing();
  7902. if (ImGui::BeginChild(ImGui::GetID("frame"), ImVec2(-FLT_MIN, 6.25f * item_height), ImGuiChildFlags_FrameStyle))
  7903. for (MyDocument* doc : app.CloseQueue)
  7904. if (doc->Dirty)
  7905. ImGui::Text("%s", doc->Name);
  7906. ImGui::EndChild();
  7907. ImVec2 button_size(ImGui::GetFontSize() * 7.0f, 0.0f);
  7908. if (ImGui::Button("Yes", button_size))
  7909. {
  7910. for (MyDocument* doc : app.CloseQueue)
  7911. {
  7912. if (doc->Dirty)
  7913. doc->DoSave();
  7914. doc->DoForceClose();
  7915. }
  7916. app.CloseQueue.clear();
  7917. ImGui::CloseCurrentPopup();
  7918. }
  7919. ImGui::SameLine();
  7920. if (ImGui::Button("No", button_size))
  7921. {
  7922. for (MyDocument* doc : app.CloseQueue)
  7923. doc->DoForceClose();
  7924. app.CloseQueue.clear();
  7925. ImGui::CloseCurrentPopup();
  7926. }
  7927. ImGui::SameLine();
  7928. if (ImGui::Button("Cancel", button_size))
  7929. {
  7930. app.CloseQueue.clear();
  7931. ImGui::CloseCurrentPopup();
  7932. }
  7933. ImGui::EndPopup();
  7934. }
  7935. }
  7936. }
  7937. ImGui::End();
  7938. }
  7939. // End of Demo code
  7940. #else
  7941. void ImGui::ShowAboutWindow(bool*) {}
  7942. void ImGui::ShowDemoWindow(bool*) {}
  7943. void ImGui::ShowUserGuide() {}
  7944. void ImGui::ShowStyleEditor(ImGuiStyle*) {}
  7945. #endif
  7946. #endif // #ifndef IMGUI_DISABLE