Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

imgui.cpp 559KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425
  1. // ImGui library v1.44 WIP
  2. // See ImGui::ShowTestWindow() for sample code.
  3. // Read 'Programmer guide' below for notes on how to setup ImGui in your codebase.
  4. // Get latest version at https://github.com/ocornut/imgui
  5. // Developed by Omar Cornut and ImGui contributors.
  6. /*
  7. Index
  8. - MISSION STATEMENT
  9. - END-USER GUIDE
  10. - PROGRAMMER GUIDE (read me!)
  11. - API BREAKING CHANGES (read me when you update!)
  12. - FREQUENTLY ASKED QUESTIONS (FAQ), TIPS
  13. - How do I update to a newer version of ImGui?
  14. - Can I have multiple widgets with the same label? Can I have widget without a label? (Yes)
  15. - Why is my text output blurry?
  16. - How can I load a different font than the default?
  17. - How can I load multiple fonts?
  18. - How can I display and input non-latin characters such as Chinese, Japanese, Korean, Cyrillic?
  19. - ISSUES & TODO-LIST
  20. - CODE
  21. - SAMPLE CODE
  22. - DEFAULT FONT DATA
  23. MISSION STATEMENT
  24. =================
  25. - easy to use to create code-driven and data-driven tools
  26. - easy to use to create ad hoc short-lived tools and long-lived, more elaborate tools
  27. - easy to hack and improve
  28. - minimize screen real-estate usage
  29. - minimize setup and maintenance
  30. - minimize state storage on user side
  31. - portable, minimize dependencies, run on target (consoles, phones, etc.)
  32. - efficient runtime (NB- we do allocate when "growing" content - creating a window / opening a tree node for the first time, etc. - but a typical frame won't allocate anything)
  33. - read about immediate-mode gui principles @ http://mollyrocket.com/861, http://mollyrocket.com/forums/index.html
  34. Designed for developers and content-creators, not the typical end-user! Some of the weaknesses includes:
  35. - doesn't look fancy, doesn't animate
  36. - limited layout features, intricate layouts are typically crafted in code
  37. - occasionally uses statically sized buffers for string manipulations - won't crash, but some very long pieces of text may be clipped. functions like ImGui::TextUnformatted() don't have such restriction.
  38. END-USER GUIDE
  39. ==============
  40. - double-click title bar to collapse window
  41. - click upper right corner to close a window, available when 'bool* p_opened' is passed to ImGui::Begin()
  42. - click and drag on lower right corner to resize window
  43. - click and drag on any empty space to move window
  44. - double-click/double-tap on lower right corner grip to auto-fit to content
  45. - TAB/SHIFT+TAB to cycle through keyboard editable fields
  46. - use mouse wheel to scroll
  47. - use CTRL+mouse wheel to zoom window contents (if IO.FontAllowScaling is true)
  48. - CTRL+Click on a slider or drag box to input value as text
  49. - text editor:
  50. - Hold SHIFT or use mouse to select text.
  51. - CTRL+Left/Right to word jump
  52. - CTRL+Shift+Left/Right to select words
  53. - CTRL+A our Double-Click to select all
  54. - CTRL+X,CTRL+C,CTRL+V to use OS clipboard
  55. - CTRL+Z,CTRL+Y to undo/redo
  56. - ESCAPE to revert text to its original value
  57. - You can apply arithmetic operators +,*,/ on numerical values. Use +- to subtract (because - would set a negative value!)
  58. PROGRAMMER GUIDE
  59. ================
  60. - read the FAQ below this section!
  61. - your code creates the UI, if your code doesn't run the UI is gone! == very dynamic UI, no construction/destructions steps, less data retention on your side, no state duplication, less sync, less bugs.
  62. - call and read ImGui::ShowTestWindow() for sample code demonstrating most features.
  63. - see examples/ folder for standalone sample applications. e.g. examples/opengl_example/
  64. - customization: PushStyleColor()/PushStyleVar() or the style editor to tweak the look of the interface (e.g. if you want a more compact UI or a different color scheme).
  65. - getting started:
  66. - initialisation: call ImGui::GetIO() to retrieve the ImGuiIO structure and fill the 'Settings' data.
  67. - every frame:
  68. 1/ in your mainloop or right after you got your keyboard/mouse info, call ImGui::GetIO() and fill the 'Input' data, then call ImGui::NewFrame().
  69. 2/ use any ImGui function you want between NewFrame() and Render()
  70. 3/ ImGui::Render() to render all the accumulated command-lists. it will call your RenderDrawListFn handler that you set in the IO structure.
  71. - all rendering information are stored into command-lists until ImGui::Render() is called.
  72. - ImGui never touches or know about your GPU state. the only function that knows about GPU is the RenderDrawListFn handler that you must provide.
  73. - effectively it means you can create widgets at any time in your code, regardless of "update" vs "render" considerations.
  74. - refer to the examples applications in the examples/ folder for instruction on how to setup your code.
  75. - a typical application skeleton may be:
  76. // Application init
  77. ImGuiIO& io = ImGui::GetIO();
  78. io.DisplaySize.x = 1920.0f;
  79. io.DisplaySize.y = 1280.0f;
  80. io.DeltaTime = 1.0f/60.0f;
  81. io.IniFilename = "imgui.ini";
  82. // TODO: Fill others settings of the io structure
  83. // Load texture
  84. unsigned char* pixels;
  85. int width, height, bytes_per_pixels;
  86. io.Fonts->GetTexDataAsRGBA32(pixels, &width, &height, &bytes_per_pixels);
  87. // TODO: copy texture to graphics memory.
  88. // TODO: store your texture pointer/identifier in 'io.Fonts->TexID'
  89. // Application main loop
  90. while (true)
  91. {
  92. // 1) get low-level input
  93. // e.g. on Win32, GetKeyboardState(), or poll your events, etc.
  94. // 2) TODO: fill all fields of IO structure and call NewFrame
  95. ImGuiIO& io = ImGui::GetIO();
  96. io.MousePos = mouse_pos;
  97. io.MouseDown[0] = mouse_button_0;
  98. io.KeysDown[i] = ...
  99. ImGui::NewFrame();
  100. // 3) most of your application code here - you can use any of ImGui::* functions at any point in the frame
  101. ImGui::Begin("My window");
  102. ImGui::Text("Hello, world.");
  103. ImGui::End();
  104. GameUpdate();
  105. GameRender();
  106. // 4) render & swap video buffers
  107. ImGui::Render();
  108. // swap video buffer, etc.
  109. }
  110. - after calling ImGui::NewFrame() you can read back 'io.WantCaptureMouse' and 'io.WantCaptureKeyboard' to tell if ImGui
  111. wants to use your inputs. if it does you can discard/hide the inputs from the rest of your application.
  112. API BREAKING CHANGES
  113. ====================
  114. Occasionally introducing changes that are breaking the API. The breakage are generally minor and easy to fix.
  115. Here is a change-log of API breaking changes, if you are using one of the functions listed, expect to have to fix some code.
  116. - 2015/07/18 (1.44) - fixed angles in ImDrawList::PathArcTo(), PathArcToFast() (introduced in 1.43) being off by an extra PI for no justifiable reason
  117. - 2015/07/14 (1.43) - add new ImFontAtlas::AddFont() API. For the old AddFont***, moved the 'font_no' parameter of ImFontAtlas::AddFont** functions to the ImFontConfig structure.
  118. you need to render your textured triangles with bilinear filtering to benefit from sub-pixel positioning of text.
  119. - 2015/07/08 (1.43) - switched rendering data to use indexed rendering. this is saving a fair amount of CPU/GPU and enables us to get anti-aliasing for a marginal cost.
  120. this necessary change will break your rendering function! the fix should be very easy. sorry for that :(
  121. - if you are using a vanilla copy of one of the imgui_impl_XXXX.cpp provided in the example, you just need to update your copy and you can ignore the rest.
  122. - the signature of the io.RenderDrawListsFn handler has changed!
  123. ImGui_XXXX_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)
  124. became:
  125. ImGui_XXXX_RenderDrawLists(ImDrawData* draw_data).
  126. argument 'cmd_lists' -> 'draw_data->CmdLists'
  127. argument 'cmd_lists_count' -> 'draw_data->CmdListsCount'
  128. ImDrawList 'commands' -> 'CmdBuffer'
  129. ImDrawList 'vtx_buffer' -> 'VtxBuffer'
  130. ImDrawList n/a -> 'IdxBuffer' (new)
  131. ImDrawCmd 'vtx_count' -> 'ElemCount'
  132. ImDrawCmd 'clip_rect' -> 'ClipRect'
  133. ImDrawCmd 'user_callback' -> 'UserCallback'
  134. ImDrawCmd 'texture_id' -> 'TextureId'
  135. - each ImDrawList now contains both a vertex buffer and an index buffer. For each command, render ElemCount/3 triangles using indices from the index buffer.
  136. - if you REALLY cannot render indexed primitives, you can call the draw_data->DeIndexAllBuffers() method to de-index the buffers. This is slow and a waste of CPU/GPU. Prefer using indexed rendering!
  137. - refer to code in the examples/ folder or ask on the GitHub if you are unsure of how to upgrade. please upgrade!
  138. - 2015/07/10 (1.43) - changed SameLine() parameters from int to float.
  139. - 2015/07/02 (1.42) - renamed SetScrollPosHere() to SetScrollFromCursorPos(). Kept inline redirection function (will obsolete).
  140. - 2015/07/02 (1.42) - renamed GetScrollPosY() to GetScrollY(). Necessary to reduce confusion along with other scrolling functions, because positions (e.g. cursor position) are not equivalent to scrolling amount.
  141. - 2015/06/14 (1.41) - changed ImageButton() default bg_col parameter from (0,0,0,1) (black) to (0,0,0,0) (transparent) - makes a difference when texture have transparence
  142. - 2015/06/14 (1.41) - changed Selectable() API from (label, selected, size) to (label, selected, flags, size). Size override should have been rarely be used. Sorry!
  143. - 2015/05/31 (1.40) - renamed GetWindowCollapsed() to IsWindowCollapsed() for consistency. Kept inline redirection function (will obsolete).
  144. - 2015/05/31 (1.40) - renamed IsRectClipped() to IsRectVisible() for consistency. Note that return value is opposite! Kept inline redirection function (will obsolete).
  145. - 2015/05/27 (1.40) - removed the third 'repeat_if_held' parameter from Button() - sorry! it was rarely used and inconsistent. Use PushButtonRepeat(true) / PopButtonRepeat() to enable repeat on desired buttons.
  146. - 2015/05/11 (1.40) - changed BeginPopup() API, takes a string identifier instead of a bool. ImGui needs to manage the open/closed state of popups. Call OpenPopup() to actually set the "opened" state of a popup. BeginPopup() returns true if the popup is opened.
  147. - 2015/05/03 (1.40) - removed style.AutoFitPadding, using style.WindowPadding makes more sense (the default values were already the same).
  148. - 2015/04/13 (1.38) - renamed IsClipped() to IsRectClipped(). Kept inline redirection function (will obsolete).
  149. - 2015/04/09 (1.38) - renamed ImDrawList::AddArc() to ImDrawList::AddArcFast() for compatibility with future API
  150. - 2015/04/03 (1.38) - removed ImGuiCol_CheckHovered, ImGuiCol_CheckActive, replaced with the more general ImGuiCol_FrameBgHovered, ImGuiCol_FrameBgActive.
  151. - 2014/04/03 (1.38) - removed support for passing -FLT_MAX..+FLT_MAX as the range for a SliderFloat(). Use DragFloat() or Inputfloat() instead.
  152. - 2015/03/17 (1.36) - renamed GetItemBoxMin()/GetItemBoxMax()/IsMouseHoveringBox() to GetItemRectMin()/GetItemRectMax()/IsMouseHoveringRect(). Kept inline redirection function (will obsolete).
  153. - 2015/03/15 (1.36) - renamed style.TreeNodeSpacing to style.IndentSpacing, ImGuiStyleVar_TreeNodeSpacing to ImGuiStyleVar_IndentSpacing
  154. - 2015/03/13 (1.36) - renamed GetWindowIsFocused() to IsWindowFocused(). Kept inline redirection function (will obsolete).
  155. - 2015/03/08 (1.35) - renamed style.ScrollBarWidth to style.ScrollbarWidth
  156. - 2015/02/27 (1.34) - renamed OpenNextNode(bool) to SetNextTreeNodeOpened(bool, ImGuiSetCond). Kept inline redirection function (will obsolete).
  157. - 2015/02/27 (1.34) - renamed ImGuiSetCondition_*** to ImGuiSetCond_***, and _FirstUseThisSession becomes _Once.
  158. - 2015/02/11 (1.32) - changed text input callback ImGuiTextEditCallback return type from void-->int. reserved for future use, return 0 for now.
  159. - 2015/02/10 (1.32) - renamed GetItemWidth() to CalcItemWidth() to clarify its evolving behavior
  160. - 2015/02/08 (1.31) - renamed GetTextLineSpacing() to GetTextLineHeightWithSpacing()
  161. - 2015/02/01 (1.31) - removed IO.MemReallocFn (unused)
  162. - 2015/01/19 (1.30) - renamed ImGuiStorage::GetIntPtr()/GetFloatPtr() to GetIntRef()/GetIntRef() because Ptr was conflicting with actual pointer storage functions.
  163. - 2015/01/11 (1.30) - big font/image API change! now loads TTF file. allow for multiple fonts. no need for a PNG loader.
  164. (1.30) - removed GetDefaultFontData(). uses io.Fonts->GetTextureData*() API to retrieve uncompressed pixels.
  165. this sequence:
  166. const void* png_data;
  167. unsigned int png_size;
  168. ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
  169. // <Copy to GPU>
  170. became:
  171. unsigned char* pixels;
  172. int width, height;
  173. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  174. // <Copy to GPU>
  175. io.Fonts->TexID = (your_texture_identifier);
  176. you now have much more flexibility to load multiple TTF fonts and manage the texture buffer for internal needs.
  177. it is now recommended your sample the font texture with bilinear interpolation.
  178. (1.30) - added texture identifier in ImDrawCmd passed to your render function (we can now render images). make sure to set io.Fonts->TexID.
  179. (1.30) - removed IO.PixelCenterOffset (unnecessary, can be handled in user projection matrix)
  180. (1.30) - removed ImGui::IsItemFocused() in favor of ImGui::IsItemActive() which handles all widgets
  181. - 2014/12/10 (1.18) - removed SetNewWindowDefaultPos() in favor of new generic API SetNextWindowPos(pos, ImGuiSetCondition_FirstUseEver)
  182. - 2014/11/28 (1.17) - moved IO.Font*** options to inside the IO.Font-> structure (FontYOffset, FontTexUvForWhite, FontBaseScale, FontFallbackGlyph)
  183. - 2014/11/26 (1.17) - reworked syntax of IMGUI_ONCE_UPON_A_FRAME helper macro to increase compiler compatibility
  184. - 2014/11/07 (1.15) - renamed IsHovered() to IsItemHovered()
  185. - 2014/10/02 (1.14) - renamed IMGUI_INCLUDE_IMGUI_USER_CPP to IMGUI_INCLUDE_IMGUI_USER_INL and imgui_user.cpp to imgui_user.inl (more IDE friendly)
  186. - 2014/09/25 (1.13) - removed 'text_end' parameter from IO.SetClipboardTextFn (the string is now always zero-terminated for simplicity)
  187. - 2014/09/24 (1.12) - renamed SetFontScale() to SetWindowFontScale()
  188. - 2014/09/24 (1.12) - moved IM_MALLOC/IM_REALLOC/IM_FREE preprocessor defines to IO.MemAllocFn/IO.MemReallocFn/IO.MemFreeFn
  189. - 2014/08/30 (1.09) - removed IO.FontHeight (now computed automatically)
  190. - 2014/08/30 (1.09) - moved IMGUI_FONT_TEX_UV_FOR_WHITE preprocessor define to IO.FontTexUvForWhite
  191. - 2014/08/28 (1.09) - changed the behavior of IO.PixelCenterOffset following various rendering fixes
  192. FREQUENTLY ASKED QUESTIONS (FAQ), TIPS
  193. ======================================
  194. Q: How do I update to a newer version of ImGui?
  195. A: Overwrite the following files:
  196. imgui.cpp
  197. imgui.h
  198. stb_rect_pack.h
  199. stb_textedit.h
  200. stb_truetype.h
  201. Check the "API BREAKING CHANGES" sections for a list of occasional API breaking changes. If you have a problem with a function, search for its name
  202. in the code, there will likely be a comment about it. Please report any issue to the GitHub page!
  203. Q: Can I have multiple widgets with the same label? Can I have widget without a label? (Yes)
  204. A: Yes. A primer on the use of labels/IDs in ImGui..
  205. - Elements that are not clickable, such as Text() items don't need an ID.
  206. - Interactive widgets require state to be carried over multiple frames (most typically ImGui often needs to remember what is the "active" widget).
  207. to do so they need an unique ID. unique ID are typically derived from a string label, an integer index or a pointer.
  208. Button("OK"); // Label = "OK", ID = hash of "OK"
  209. Button("Cancel"); // Label = "Cancel", ID = hash of "Cancel"
  210. - ID are uniquely scoped within windows, tree nodes, etc. so no conflict can happen if you have two buttons called "OK" in two different windows
  211. or in two different locations of a tree.
  212. - if you have a same ID twice in the same location, you'll have a conflict:
  213. Button("OK");
  214. Button("OK"); // ID collision! Both buttons will be treated as the same.
  215. Fear not! this is easy to solve and there are many ways to solve it!
  216. - when passing a label you can optionally specify extra unique ID information within string itself. This helps solving the simpler collision cases.
  217. use "##" to pass a complement to the ID that won't be visible to the end-user:
  218. Button("Play##0"); // Label = "Play", ID = hash of "Play##0"
  219. Button("Play##1"); // Label = "Play", ID = hash of "Play##1" (different from above)
  220. - so if you want to hide the label but need an ID:
  221. Checkbox("##On", &b); // Label = "", ID = hash of "##On"
  222. - occasionally (rarely) you might want change a label while preserving a constant ID. This allows you to animate labels.
  223. use "###" to pass a label that isn't part of ID:
  224. Button("Hello###ID"; // Label = "Hello", ID = hash of "ID"
  225. Button("World###ID"; // Label = "World", ID = hash of "ID" (same as above)
  226. - use PushID() / PopID() to create scopes and avoid ID conflicts within the same Window.
  227. this is the most convenient way of distinguish ID if you are iterating and creating many UI elements.
  228. you can push a pointer, a string or an integer value. remember that ID are formed from the addition of everything in the ID stack!
  229. for (int i = 0; i < 100; i++)
  230. {
  231. PushID(i);
  232. Button("Click"); // Label = "Click", ID = hash of integer + "label" (unique)
  233. PopID();
  234. }
  235. for (int i = 0; i < 100; i++)
  236. {
  237. MyObject* obj = Objects[i];
  238. PushID(obj);
  239. Button("Click"); // Label = "Click", ID = hash of pointer + "label" (unique)
  240. PopID();
  241. }
  242. for (int i = 0; i < 100; i++)
  243. {
  244. MyObject* obj = Objects[i];
  245. PushID(obj->Name);
  246. Button("Click"); // Label = "Click", ID = hash of string + "label" (unique)
  247. PopID();
  248. }
  249. - more example showing that you can stack multiple prefixes into the ID stack:
  250. Button("Click"); // Label = "Click", ID = hash of "Click"
  251. PushID("node");
  252. Button("Click"); // Label = "Click", ID = hash of "node" + "Click"
  253. PushID(my_ptr);
  254. Button("Click"); // Label = "Click", ID = hash of "node" + ptr + "Click"
  255. PopID();
  256. PopID();
  257. - tree nodes implicitly creates a scope for you by calling PushID().
  258. Button("Click"); // Label = "Click", ID = hash of "Click"
  259. if (TreeNode("node"))
  260. {
  261. Button("Click"); // Label = "Click", ID = hash of "node" + "Click"
  262. TreePop();
  263. }
  264. - when working with trees, ID are used to preserve the opened/closed state of each tree node.
  265. depending on your use cases you may want to use strings, indices or pointers as ID.
  266. e.g. when displaying a single object that may change over time (1-1 relationship), using a static string as ID will preserve your node open/closed state when the targeted object change.
  267. e.g. when displaying a list of objects, using indices or pointers as ID will preserve the node open/closed state differently. experiment and see what makes more sense!
  268. Q: Why is my text output blurry?
  269. A: In your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
  270. Q: How can I load a different font than the default? (default is an embedded version of ProggyClean.ttf, rendered at size 13)
  271. A: Use the font atlas to load the TTF file you want:
  272. io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels);
  273. io.Fonts->GetTexDataAsRGBA32() or GetTexDataAsAlpha8()
  274. Q: How can I load multiple fonts?
  275. A: Use the font atlas to pack them into a single texture:
  276. ImFont* font0 = io.Fonts->AddFontDefault();
  277. ImFont* font1 = io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels);
  278. ImFont* font2 = io.Fonts->AddFontFromFileTTF("myfontfile2.ttf", size_in_pixels);
  279. io.Fonts->GetTexDataAsRGBA32() or GetTexDataAsAlpha8()
  280. // the first loaded font gets used by default
  281. // use ImGui::PushFont()/ImGui::PopFont() to change the font at runtime
  282. // Options
  283. ImFontConfig config;
  284. config.OversampleH = 3;
  285. config.OversampleV = 3;
  286. config.GlyphExtraSpacing.x = 1.0f;
  287. io.Fonts->LoadFromFileTTF("myfontfile.ttf", size_pixels, &config);
  288. // Merging input from different fonts into one
  289. ImWchar ranges[] = { 0xf000, 0xf3ff, 0 };
  290. ImFontConfig config;
  291. config.MergeMode = true;
  292. io.Fonts->AddFontDefault();
  293. io.Fonts->LoadFromFileTTF("fontawesome-webfont.ttf", 16.0f, &config, ranges);
  294. io.Fonts->LoadFromFileTTF("myfontfile.ttf", size_pixels, NULL, &config, io.Fonts->GetGlyphRangesJapanese());
  295. Q: How can I display and input non-latin characters such as Chinese, Japanese, Korean, Cyrillic?
  296. A: When loading a font, pass custom Unicode ranges to specify the glyphs to load. ImGui will support UTF-8 encoding across the board.
  297. Character input depends on you passing the right character code to io.AddInputCharacter(). The example applications do that.
  298. io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels, NULL, io.Fonts->GetGlyphRangesJapanese()); // Load Japanese characters
  299. io.Fonts->GetTexDataAsRGBA32() or GetTexDataAsAlpha8()
  300. io.ImeWindowHandle = MY_HWND; // To input using Microsoft IME, give ImGui the hwnd of your application
  301. - tip: the construct 'IMGUI_ONCE_UPON_A_FRAME { ... }' will run the block of code only once a frame. You can use it to quickly add custom UI in the middle of a deep nested inner loop in your code.
  302. - tip: you can create widgets without a Begin()/End() block, they will go in an implicit window called "Debug"
  303. - tip: you can call Begin() multiple times with the same name during the same frame, it will keep appending to the same window.
  304. - tip: you can call Render() multiple times (e.g for VR renders).
  305. - tip: call and read the ShowTestWindow() code for more example of how to use ImGui!
  306. ISSUES & TODO-LIST
  307. ==================
  308. Issue numbers (#) refer to github issues.
  309. - misc: merge or clarify ImVec4 vs ImRect?
  310. - window: autofit feedback loop when user relies on any dynamic layout (window width multiplier, column). maybe just clearly drop manual autofit?
  311. - window: add a way for very transient windows (non-saved, temporary overlay over hundreds of objects) to "clean" up from the global window list.
  312. - window: allow resizing of child windows (possibly given min/max for each axis?)
  313. - window: background options for child windows, border option (disable rounding)
  314. - window: resizing from any sides? + mouse cursor directives for app.
  315. - window: get size/pos helpers given names (see discussion in #249)
  316. - window: a collapsed window can be stuck behing the main menu bar?
  317. - scrolling: add horizontal scroll
  318. !- scrolling: allow immediately effective change of scroll if we haven't appended items yet
  319. - widgets: display mode: widget-label, label-widget (aligned on column or using fixed size), label-newline-tab-widget etc.
  320. - widgets: clean up widgets internal toward exposing everything.
  321. - widgets: add a disabled/read-only mode (#211)
  322. - main: considering adding EndFrame()/Init(). some constructs are awkward in the implementation because of the lack of them.
  323. - main: IsItemHovered() make it more consistent for various type of widgets, widgets with multiple components, etc. also effectively IsHovered() region sometimes differs from hot region, e.g tree nodes
  324. - main: IsItemHovered() info stored in a stack? so that 'if TreeNode() { Text; TreePop; } if IsHovered' return the hover state of the TreeNode?
  325. - input text: add ImGuiInputTextFlags_EnterToApply? (off #218)
  326. - input text multi-line: way to dynamically grow the buffer without forcing the user to initially allocate for worse case (follow up on #200)
  327. - input text multi-line: line numbers? status bar? (follow up on #200)
  328. !- input number: large int not reliably supported because of int<>float conversions.
  329. - input number: optional range min/max for Input*() functions
  330. - input number: holding [-]/[+] buttons could increase the step speed non-linearly (or user-controlled)
  331. - input number: use mouse wheel to step up/down
  332. - input number: non-decimal input.
  333. - text: proper alignment options
  334. - layout: horizontal layout helper (#97)
  335. - layout: more generic alignment state (left/right/centered) for single items?
  336. - layout: clean up the InputFloatN/SliderFloatN/ColorEdit4 layout code. item width should include frame padding.
  337. - columns: separator function or parameter that works within the column (currently Separator() bypass all columns) (#125)
  338. - columns: declare column set (each column: fixed size, %, fill, distribute default size among fills) (#125)
  339. - columns: columns header to act as button (~sort op) and allow resize/reorder (#125)
  340. - columns: user specify columns size (#125)
  341. - popup: border options. richer api like BeginChild() perhaps? (#197)
  342. - combo: sparse combo boxes (via function call?)
  343. - combo: turn child handling code into pop up helper
  344. - combo: contents should extends to fit label if combo widget is small
  345. - combo/listbox: keyboard control. need inputtext like non-active focus + key handling. considering keybord for custom listbox (pr #203)
  346. - listbox: multiple selection
  347. - listbox: user may want to initial scroll to focus on the one selected value?
  348. - listbox: keyboard navigation.
  349. - listbox: scrolling should track modified selection.
  350. - menus: local shortcuts, global shortcuts (#126)
  351. - menus: icons
  352. - menus: menubars: some sort of priority / effect of main menu-bar on desktop size?
  353. - tabs
  354. - separator: separator on the initial position of a window is not visible (cursorpos.y <= clippos.y)
  355. - gauge: various forms of gauge/loading bars widgets
  356. - color: add a better color picker (perhaps a popup).
  357. - plot: plotlines should use the polygon-stroke facilities (currently issues with averaging normals)
  358. - plot: make it easier for user to draw extra stuff into the graph (e.g: draw basis, highlight certain points, 2d plots, multiple plots)
  359. - plot: "smooth" automatic scale over time, user give an input 0.0(full user scale) 1.0(full derived from value)
  360. - plot: add a helper e.g. Plot(char* label, float value, float time_span=2.0f) that stores values and Plot them for you - probably another function name. and/or automatically allow to plot ANY displayed value (more reliance on stable ID)
  361. - file selection widget -> build the tool in our codebase to improve model-dialog idioms
  362. - inputfloat: applying arithmetics ops (+,-,*,/) messes up with text edit undo stack.
  363. - slider: allow using the [-]/[+] buttons used by InputFloat()/InputInt()
  364. - slider: initial absolute click is imprecise. change to relative movement slider (same as scrollbar).
  365. - slider: add dragging-based widgets to edit values with mouse (on 2 axises), saving screen real-estate.
  366. - slider: tint background based on value (e.g. v_min -> v_max, or use 0.0f either side of the sign)
  367. - dragfloat: up/down axis
  368. - text edit: clean up the mess caused by converting UTF-8 <> wchar. the code is rather inefficient right now.
  369. - text edit: centered text for slider as input text so it matches typical positioning.
  370. - text edit: flag to disable live update of the user buffer.
  371. - text edit: field resize behavior - field could stretch when being edited? hover tooltip shows more text?
  372. - tree: add treenode/treepush int variants? because (void*) cast from int warns on some platforms/settings
  373. - textwrapped: figure out better way to use TextWrapped() in an always auto-resize context (tooltip, etc.) (git issue #249)
  374. - settings: write more decent code to allow saving/loading new fields
  375. - settings: api for per-tool simple persistent data (bool,int,float,columns sizes,etc.) in .ini file
  376. - style: store rounded corners in texture to use 1 quad per corner (filled and wireframe). so rounding have minor cost.
  377. - style: checkbox: padding for "active" color should be a multiplier of the
  378. - style: colorbox not always square?
  379. - text: simple markup language for color change?
  380. - log: LogButtons() options for specifying depth and/or hiding depth slider
  381. - log: have more control over the log scope (e.g. stop logging when leaving current tree node scope)
  382. - log: be able to log anything (e.g. right-click on a window/tree-node, shows context menu? log into tty/file/clipboard)
  383. - log: let user copy any window content to clipboard easily (CTRL+C on windows? while moving it? context menu?). code is commented because it fails with multiple Begin/End pairs.
  384. - filters: set a current filter that tree node can automatically query to hide themselves
  385. - filters: handle wildcards (with implicit leading/trailing *), regexps
  386. - shortcuts: add a shortcut api, e.g. parse "&Save" and/or "Save (CTRL+S)", pass in to widgets or provide simple ways to use (button=activate, input=focus)
  387. !- keyboard: tooltip & combo boxes are messing up / not honoring keyboard tabbing
  388. - keyboard: full keyboard navigation and focus.
  389. - focus: SetKeyboardFocusHere() on with >= 0 offset could be done on same frame (else latch and modulate on beginning of next frame)
  390. - input: rework IO to be able to pass actual events to fix temporal aliasing issues.
  391. - input: support track pad style scrolling & slider edit.
  392. - portability: big-endian test/support (#81)
  393. - memory: add a way to discard allocs of unused/transient windows. with the current architecture new windows (including popup, opened combos, listbox) perform at least 3 allocs.
  394. - misc: mark printf compiler attributes on relevant functions
  395. - misc: provide a way to compile out the entire implementation while providing a dummy API (e.g. #define IMGUI_DUMMY_IMPL)
  396. - misc: double-clicking on title bar to minimize isn't consistent, perhaps move to single-click on left-most collapse icon?
  397. - style editor: have a more global HSV setter (e.g. alter hue on all elements). consider replacing active/hovered by offset in HSV space?
  398. - style editor: color child window height expressed in multiple of line height.
  399. - optimization/render: merge command-lists with same clip-rect into one even if they aren't sequential? (as long as in-between clip rectangle don't overlap)?
  400. - optimization: turn some the various stack vectors into statically-sized arrays
  401. - optimization: better clipping for multi-component widgets
  402. */
  403. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  404. #define _CRT_SECURE_NO_WARNINGS
  405. #endif
  406. #include "imgui.h"
  407. #include <ctype.h> // toupper, isprint
  408. #include <math.h> // sqrtf, fabsf, fmodf, powf, cosf, sinf, floorf, ceilf
  409. #include <stdio.h> // vsnprintf, sscanf, printf
  410. #include <new> // new (ptr)
  411. #if defined(_MSC_VER) || defined(__MINGW32__)
  412. #include <malloc.h> // alloca
  413. #else
  414. #include <alloca.h> // alloca
  415. #endif
  416. #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier
  417. #include <stddef.h> // intptr_t
  418. #else
  419. #include <stdint.h> // intptr_t
  420. #endif
  421. #ifdef _MSC_VER
  422. #pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff)
  423. #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen
  424. #define snprintf _snprintf
  425. #endif
  426. // Clang warnings with -Weverything
  427. #ifdef __clang__
  428. #pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse.
  429. #pragma clang diagnostic ignored "-Wfloat-equal" // warning : comparing floating point with == or != is unsafe // storing and comparing against same constants ok.
  430. #pragma clang diagnostic ignored "-Wformat-nonliteral" // warning : format string is not a string literal // passing non-literal to vsnformat(). yes, user passing incorrect format strings can crash the code.
  431. #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.
  432. #pragma clang diagnostic ignored "-Wglobal-constructors" // warning : declaration requires a global destructor // similar to above, not sure what the exact difference it.
  433. #pragma clang diagnostic ignored "-Wsign-conversion" // warning : implicit conversion changes signedness //
  434. #pragma clang diagnostic ignored "-Wmissing-noreturn" // warning : function xx could be declared with attribute 'noreturn' warning // GetDefaultFontData() asserts which some implementation makes it never return.
  435. #pragma clang diagnostic ignored "-Wdeprecated-declarations"// warning : 'xx' is deprecated: The POSIX name for this item.. // for strdup used in demo code (so user can copy & paste the code)
  436. #pragma clang diagnostic ignored "-Wint-to-void-pointer-cast" // warning : cast to 'void *' from smaller integer type 'int'
  437. #endif
  438. #ifdef __GNUC__
  439. #pragma GCC diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used
  440. #pragma GCC diagnostic ignored "-Wint-to-pointer-cast" // warning: cast to pointer from integer of different size
  441. #endif
  442. //-------------------------------------------------------------------------
  443. // STB libraries implementation
  444. //-------------------------------------------------------------------------
  445. struct ImGuiTextEditState;
  446. //#define IMGUI_STB_NAMESPACE ImStb
  447. //#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION
  448. //#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION
  449. #ifdef IMGUI_STB_NAMESPACE
  450. namespace IMGUI_STB_NAMESPACE
  451. {
  452. #endif
  453. #ifdef __clang__
  454. #pragma clang diagnostic push
  455. #pragma clang diagnostic ignored "-Wunused-function"
  456. #pragma clang diagnostic ignored "-Wmissing-prototypes"
  457. #endif
  458. #define STBRP_ASSERT(x) IM_ASSERT(x)
  459. #ifndef IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION
  460. #define STBRP_STATIC
  461. #define STB_RECT_PACK_IMPLEMENTATION
  462. #endif
  463. #include "stb_rect_pack.h"
  464. #define STBTT_malloc(x,u) ((void)(u), ImGui::MemAlloc(x))
  465. #define STBTT_free(x,u) ((void)(u), ImGui::MemFree(x))
  466. #define STBTT_assert(x) IM_ASSERT(x)
  467. #ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION
  468. #define STBTT_STATIC
  469. #define STB_TRUETYPE_IMPLEMENTATION
  470. #else
  471. #define STBTT_DEF extern
  472. #endif
  473. #include "stb_truetype.h"
  474. #undef STB_TEXTEDIT_STRING
  475. #undef STB_TEXTEDIT_CHARTYPE
  476. #define STB_TEXTEDIT_STRING ImGuiTextEditState
  477. #define STB_TEXTEDIT_CHARTYPE ImWchar
  478. #define STB_TEXTEDIT_GETWIDTH_NEWLINE -1.0f
  479. #include "stb_textedit.h"
  480. #ifdef __clang__
  481. #pragma clang diagnostic pop
  482. #endif
  483. #ifdef IMGUI_STB_NAMESPACE
  484. } // namespace ImStb
  485. using namespace IMGUI_STB_NAMESPACE;
  486. #endif
  487. //-------------------------------------------------------------------------
  488. // Forward Declarations
  489. //-------------------------------------------------------------------------
  490. struct ImRect;
  491. struct ImGuiColMod;
  492. struct ImGuiStyleMod;
  493. struct ImGuiDrawContext;
  494. struct ImGuiTextEditState;
  495. struct ImGuiIniData;
  496. struct ImGuiState;
  497. struct ImGuiWindow;
  498. typedef int ImGuiLayoutType; // enum ImGuiLayoutType_
  499. typedef int ImGuiButtonFlags; // enum ImGuiButtonFlags_
  500. static bool ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, bool allow_key_modifiers, ImGuiButtonFlags flags = 0);
  501. static void LogText(const ImVec2& ref_pos, const char* text, const char* text_end = NULL);
  502. static bool InputTextEx(const char* label, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiTextEditCallback callback = NULL, void* user_data = NULL);
  503. static void RenderText(ImVec2 pos, const char* text, const char* text_end = NULL, bool hide_text_after_hash = true);
  504. static void RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end, float wrap_width);
  505. static void RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, ImGuiAlign align = ImGuiAlign_Default, const ImVec2* clip_min = NULL, const ImVec2* clip_max = NULL);
  506. static void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border = true, float rounding = 0.0f);
  507. static void RenderCollapseTriangle(ImVec2 p_min, bool opened, float scale = 1.0f, bool shadow = false);
  508. static void RenderCheckMark(ImVec2 pos, ImU32 col);
  509. static void SetFont(ImFont* font);
  510. static bool ItemAdd(const ImRect& bb, const ImGuiID* id);
  511. static void ItemSize(ImVec2 size, float text_offset_y = 0.0f);
  512. static void ItemSize(const ImRect& bb, float text_offset_y = 0.0f);
  513. static void PushColumnClipRect(int column_index = -1);
  514. static bool IsClippedEx(const ImRect& bb, const ImGuiID* id, bool clip_even_when_logged);
  515. static bool IsMouseHoveringRect(const ImRect& bb);
  516. static bool IsKeyPressedMap(ImGuiKey key, bool repeat = true);
  517. static void Scrollbar(ImGuiWindow* window);
  518. static bool CloseWindowButton(bool* p_opened = NULL);
  519. static void FocusWindow(ImGuiWindow* window);
  520. static ImGuiWindow* FindHoveredWindow(ImVec2 pos, bool excluding_childs);
  521. static void CloseInactivePopups();
  522. static ImGuiWindow* GetFrontMostModalRootWindow();
  523. static void SetWindowScrollY(ImGuiWindow* window, float scroll_y);
  524. // Helpers: String
  525. static int ImStricmp(const char* str1, const char* str2);
  526. static int ImStrnicmp(const char* str1, const char* str2, int count);
  527. static char* ImStrdup(const char* str);
  528. static int ImStrlenW(const ImWchar* str);
  529. static const ImWchar* ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin); // Find beginning-of-line
  530. static const char* ImStristr(const char* haystack, const char* needle, const char* needle_end);
  531. static int ImFormatString(char* buf, int buf_size, const char* fmt, ...);
  532. static int ImFormatStringV(char* buf, int buf_size, const char* fmt, va_list args);
  533. // Helpers: Misc
  534. static ImU32 ImHash(const void* data, int data_size, ImU32 seed);
  535. static bool ImLoadFileToMemory(const char* filename, const char* file_open_mode, void** out_file_data, int* out_file_size, int padding_bytes = 0);
  536. static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; }
  537. static inline bool ImCharIsSpace(int c) { return c == ' ' || c == '\t' || c == 0x3000; }
  538. // Helpers: UTF-8 <> wchar
  539. static inline int ImTextCharToUtf8(char* buf, int buf_size, unsigned int in_char); // return output UTF-8 bytes count
  540. static int ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWchar* in_text_end); // return output UTF-8 bytes count
  541. static int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end); // return input UTF-8 bytes count
  542. static int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_remaining = NULL); // return input UTF-8 bytes count
  543. static int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end); // return number of UTF-8 code-points (NOT bytes count)
  544. static inline int ImTextCountUtf8BytesFromChar(unsigned int in_char); // return output UTF-8 bytes count
  545. static int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end); // return number of bytes to express string as UTF-8 code-points
  546. //-----------------------------------------------------------------------------
  547. // Platform dependent default implementations
  548. //-----------------------------------------------------------------------------
  549. static const char* GetClipboardTextFn_DefaultImpl();
  550. static void SetClipboardTextFn_DefaultImpl(const char* text);
  551. static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y);
  552. //-----------------------------------------------------------------------------
  553. // User facing structures
  554. //-----------------------------------------------------------------------------
  555. ImGuiStyle::ImGuiStyle()
  556. {
  557. Alpha = 1.0f; // Global alpha applies to everything in ImGui
  558. WindowPadding = ImVec2(8,8); // Padding within a window
  559. WindowMinSize = ImVec2(32,32); // Minimum window size
  560. WindowRounding = 9.0f; // Radius of window corners rounding. Set to 0.0f to have rectangular windows
  561. WindowTitleAlign = ImGuiAlign_Left; // Alignment for title bar text
  562. ChildWindowRounding = 0.0f; // Radius of child window corners rounding. Set to 0.0f to have rectangular windows
  563. FramePadding = ImVec2(4,3); // Padding within a framed rectangle (used by most widgets)
  564. FrameRounding = 0.0f; // Radius of frame corners rounding. Set to 0.0f to have rectangular frames (used by most widgets).
  565. ItemSpacing = ImVec2(8,4); // Horizontal and vertical spacing between widgets/lines
  566. ItemInnerSpacing = ImVec2(4,4); // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label)
  567. TouchExtraPadding = ImVec2(0,0); // Expand reactive bounding box for touch-based system where touch position is not accurate enough. Unfortunately we don't sort widgets so priority on overlap will always be given to the first widget. So don't grow this too much!
  568. WindowFillAlphaDefault = 0.70f; // Default alpha of window background, if not specified in ImGui::Begin()
  569. IndentSpacing = 22.0f; // Horizontal spacing when e.g. entering a tree node
  570. ColumnsMinSpacing = 6.0f; // Minimum horizontal spacing between two columns
  571. ScrollbarWidth = 16.0f; // Width of the vertical scrollbar
  572. ScrollbarRounding = 0.0f; // Radius of grab corners rounding for scrollbar
  573. GrabMinSize = 10.0f; // Minimum width/height of a grab box for slider/scrollbar
  574. GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
  575. DisplayWindowPadding = ImVec2(22,22); // Window positions are clamped to be visible within the display area by at least this amount. Only covers regular windows.
  576. DisplaySafeAreaPadding = ImVec2(4,4); // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows.
  577. AntiAliasedLines = true; // Enable anti-aliasing on lines/borders. Disable if you are really short on CPU/GPU.
  578. AntiAliasedShapes = true; // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.)
  579. Colors[ImGuiCol_Text] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
  580. Colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f);
  581. Colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  582. Colors[ImGuiCol_ChildWindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
  583. Colors[ImGuiCol_Border] = ImVec4(0.70f, 0.70f, 0.70f, 0.65f);
  584. Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
  585. Colors[ImGuiCol_FrameBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.30f); // Background of checkbox, radio button, plot, slider, text input
  586. Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.90f, 0.80f, 0.80f, 0.40f);
  587. Colors[ImGuiCol_FrameBgActive] = ImVec4(0.90f, 0.65f, 0.65f, 0.45f);
  588. Colors[ImGuiCol_TitleBg] = ImVec4(0.50f, 0.50f, 1.00f, 0.45f);
  589. Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.40f, 0.40f, 0.80f, 0.20f);
  590. Colors[ImGuiCol_TitleBgActive] = ImVec4(0.50f, 0.50f, 1.00f, 0.55f);
  591. Colors[ImGuiCol_MenuBarBg] = ImVec4(0.40f, 0.40f, 0.55f, 0.60f);
  592. Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.40f, 0.40f, 0.80f, 0.15f);
  593. Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.40f, 0.40f, 0.80f, 0.30f);
  594. Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.40f, 0.80f, 0.40f);
  595. Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.80f, 0.50f, 0.50f, 0.40f);
  596. Colors[ImGuiCol_ComboBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.99f);
  597. Colors[ImGuiCol_CheckMark] = ImVec4(0.90f, 0.90f, 0.90f, 0.50f);
  598. Colors[ImGuiCol_SliderGrab] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f);
  599. Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.80f, 0.50f, 0.50f, 1.00f);
  600. Colors[ImGuiCol_Button] = ImVec4(0.67f, 0.40f, 0.40f, 0.60f);
  601. Colors[ImGuiCol_ButtonHovered] = ImVec4(0.67f, 0.40f, 0.40f, 1.00f);
  602. Colors[ImGuiCol_ButtonActive] = ImVec4(0.80f, 0.50f, 0.50f, 1.00f);
  603. Colors[ImGuiCol_Header] = ImVec4(0.40f, 0.40f, 0.90f, 0.45f);
  604. Colors[ImGuiCol_HeaderHovered] = ImVec4(0.45f, 0.45f, 0.90f, 0.80f);
  605. Colors[ImGuiCol_HeaderActive] = ImVec4(0.53f, 0.53f, 0.87f, 0.80f);
  606. Colors[ImGuiCol_Column] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);
  607. Colors[ImGuiCol_ColumnHovered] = ImVec4(0.70f, 0.60f, 0.60f, 1.00f);
  608. Colors[ImGuiCol_ColumnActive] = ImVec4(0.90f, 0.70f, 0.70f, 1.00f);
  609. Colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f);
  610. Colors[ImGuiCol_ResizeGripHovered] = ImVec4(1.00f, 1.00f, 1.00f, 0.60f);
  611. Colors[ImGuiCol_ResizeGripActive] = ImVec4(1.00f, 1.00f, 1.00f, 0.90f);
  612. Colors[ImGuiCol_CloseButton] = ImVec4(0.50f, 0.50f, 0.90f, 0.50f);
  613. Colors[ImGuiCol_CloseButtonHovered] = ImVec4(0.70f, 0.70f, 0.90f, 0.60f);
  614. Colors[ImGuiCol_CloseButtonActive] = ImVec4(0.70f, 0.70f, 0.70f, 1.00f);
  615. Colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  616. Colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
  617. Colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
  618. Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);
  619. Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 0.00f, 1.00f, 0.35f);
  620. Colors[ImGuiCol_TooltipBg] = ImVec4(0.05f, 0.05f, 0.10f, 0.90f);
  621. Colors[ImGuiCol_ModalWindowDarkening] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f);
  622. }
  623. // Statically allocated font atlas. This is merely a maneuver to keep ImFontAtlas definition at the bottom of the .h file (otherwise it'd be inside ImGuiIO)
  624. // Also we wouldn't be able to new() one at this point, before users may define IO.MemAllocFn.
  625. static ImFontAtlas GDefaultFontAtlas;
  626. ImGuiIO::ImGuiIO()
  627. {
  628. // Most fields are initialized with zero
  629. memset(this, 0, sizeof(*this));
  630. DisplaySize = ImVec2(-1.0f, -1.0f);
  631. DeltaTime = 1.0f/60.0f;
  632. IniSavingRate = 5.0f;
  633. IniFilename = "imgui.ini";
  634. LogFilename = "imgui_log.txt";
  635. Fonts = &GDefaultFontAtlas;
  636. FontGlobalScale = 1.0f;
  637. MousePos = ImVec2(-1,-1);
  638. MousePosPrev = ImVec2(-1,-1);
  639. MouseDoubleClickTime = 0.30f;
  640. MouseDoubleClickMaxDist = 6.0f;
  641. MouseDragThreshold = 6.0f;
  642. for (int i = 0; i < ImGuiKey_COUNT; i++)
  643. KeyMap[i] = -1;
  644. KeyRepeatDelay = 0.250f;
  645. KeyRepeatRate = 0.050f;
  646. UserData = NULL;
  647. // User functions
  648. RenderDrawListsFn = NULL;
  649. MemAllocFn = malloc;
  650. MemFreeFn = free;
  651. GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations
  652. SetClipboardTextFn = SetClipboardTextFn_DefaultImpl;
  653. ImeSetInputScreenPosFn = ImeSetInputScreenPosFn_DefaultImpl;
  654. }
  655. //-----------------------------------------------------------------------------
  656. // HELPERS
  657. //-----------------------------------------------------------------------------
  658. #define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR)/sizeof(*_ARR)))
  659. const float IM_PI = 3.14159265358979323846f;
  660. #ifdef INT_MAX
  661. #define IM_INT_MIN INT_MIN
  662. #define IM_INT_MAX INT_MAX
  663. #else
  664. #define IM_INT_MIN (-2147483647-1)
  665. #define IM_INT_MAX (2147483647)
  666. #endif
  667. // Play it nice with Windows users. Notepad in 2015 still doesn't display text data with Unix-style \n.
  668. #ifdef _MSC_VER
  669. #define IM_NEWLINE "\r\n"
  670. #else
  671. #define IM_NEWLINE "\n"
  672. #endif
  673. // Pass in translated ASCII characters for text input.
  674. // - with glfw you can get those from the callback set in glfwSetCharCallback()
  675. // - on Windows you can get those using ToAscii+keyboard state, or via the WM_CHAR message
  676. void ImGuiIO::AddInputCharacter(ImWchar c)
  677. {
  678. const int n = ImStrlenW(InputCharacters);
  679. if (n + 1 < IM_ARRAYSIZE(InputCharacters))
  680. {
  681. InputCharacters[n] = c;
  682. InputCharacters[n+1] = '\0';
  683. }
  684. }
  685. void ImGuiIO::AddInputCharactersUTF8(const char* utf8_chars)
  686. {
  687. // We can't pass more wchars than ImGuiIO::InputCharacters[] can hold so don't convert more
  688. const int wchars_buf_len = sizeof(ImGuiIO::InputCharacters) / sizeof(ImWchar);
  689. ImWchar wchars[wchars_buf_len];
  690. ImTextStrFromUtf8(wchars, wchars_buf_len, utf8_chars, NULL);
  691. for (int i = 0; wchars[i] != 0 && i < wchars_buf_len; i++)
  692. AddInputCharacter(wchars[i]);
  693. }
  694. // Math bits
  695. // We are keeping those static in the .cpp file so as not to leak them outside, in the case the user has implicit cast operators between ImVec2 and its own types.
  696. static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x*rhs, lhs.y*rhs); }
  697. //static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x/rhs, lhs.y/rhs); }
  698. static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x+rhs.x, lhs.y+rhs.y); }
  699. static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x-rhs.x, lhs.y-rhs.y); }
  700. static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x*rhs.x, lhs.y*rhs.y); }
  701. static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x/rhs.x, lhs.y/rhs.y); }
  702. static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; }
  703. //static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; }
  704. static inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; }
  705. //static inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; }
  706. static inline ImVec4 operator-(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x-rhs.x, lhs.y-rhs.y, lhs.z-rhs.z, lhs.w-lhs.w); }
  707. static inline int ImMin(int lhs, int rhs) { return lhs < rhs ? lhs : rhs; }
  708. static inline int ImMax(int lhs, int rhs) { return lhs >= rhs ? lhs : rhs; }
  709. static inline float ImMin(float lhs, float rhs) { return lhs < rhs ? lhs : rhs; }
  710. static inline float ImMax(float lhs, float rhs) { return lhs >= rhs ? lhs : rhs; }
  711. static inline ImVec2 ImMin(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(ImMin(lhs.x,rhs.x), ImMin(lhs.y,rhs.y)); }
  712. static inline ImVec2 ImMax(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(ImMax(lhs.x,rhs.x), ImMax(lhs.y,rhs.y)); }
  713. static inline int ImClamp(int v, int mn, int mx) { return (v < mn) ? mn : (v > mx) ? mx : v; }
  714. static inline float ImClamp(float v, float mn, float mx) { return (v < mn) ? mn : (v > mx) ? mx : v; }
  715. static inline ImVec2 ImClamp(const ImVec2& f, const ImVec2& mn, ImVec2 mx) { return ImVec2(ImClamp(f.x,mn.x,mx.x), ImClamp(f.y,mn.y,mx.y)); }
  716. static inline float ImSaturate(float f) { return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f; }
  717. static inline float ImLerp(float a, float b, float t) { return a + (b - a) * t; }
  718. static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, const ImVec2& t) { return ImVec2(a.x + (b.x - a.x) * t.x, a.y + (b.y - a.y) * t.y); }
  719. static inline float ImLengthSqr(const ImVec2& lhs) { return lhs.x*lhs.x + lhs.y*lhs.y; }
  720. static inline float ImLengthSqr(const ImVec4& lhs) { return lhs.x*lhs.x + lhs.y*lhs.y + lhs.z*lhs.z + lhs.w*lhs.w; }
  721. static inline float ImInvLength(const ImVec2& lhs, float fail_value) { float d = lhs.x*lhs.x + lhs.y*lhs.y; if (d > 0.0f) return 1.0f / sqrtf(d); return fail_value; }
  722. static bool ImIsPointInTriangle(const ImVec2& p, const ImVec2& a, const ImVec2& b, const ImVec2& c)
  723. {
  724. bool b1 = ((p.x - b.x) * (a.y - b.y) - (p.y - b.y) * (a.x - b.x)) < 0.0f;
  725. bool b2 = ((p.x - c.x) * (b.y - c.y) - (p.y - c.y) * (b.x - c.x)) < 0.0f;
  726. bool b3 = ((p.x - a.x) * (c.y - a.y) - (p.y - a.y) * (c.x - a.x)) < 0.0f;
  727. return ((b1 == b2) && (b2 == b3));
  728. }
  729. static int ImStricmp(const char* str1, const char* str2)
  730. {
  731. int d;
  732. while ((d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; }
  733. return d;
  734. }
  735. static int ImStrnicmp(const char* str1, const char* str2, int count)
  736. {
  737. int d = 0;
  738. while (count > 0 && (d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; count--; }
  739. return d;
  740. }
  741. static char* ImStrdup(const char *str)
  742. {
  743. char *buff = (char*)ImGui::MemAlloc(strlen(str) + 1);
  744. IM_ASSERT(buff);
  745. strcpy(buff, str);
  746. return buff;
  747. }
  748. static int ImStrlenW(const ImWchar* str)
  749. {
  750. int n = 0;
  751. while (*str++) n++;
  752. return n;
  753. }
  754. static const ImWchar* ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin) // find beginning-of-line
  755. {
  756. while (buf_mid_line > buf_begin && buf_mid_line[-1] != '\n')
  757. buf_mid_line--;
  758. return buf_mid_line;
  759. }
  760. static const char* ImStristr(const char* haystack, const char* needle, const char* needle_end)
  761. {
  762. if (!needle_end)
  763. needle_end = needle + strlen(needle);
  764. const char un0 = (char)toupper(*needle);
  765. while (*haystack)
  766. {
  767. if (toupper(*haystack) == un0)
  768. {
  769. const char* b = needle + 1;
  770. for (const char* a = haystack + 1; b < needle_end; a++, b++)
  771. if (toupper(*a) != toupper(*b))
  772. break;
  773. if (b == needle_end)
  774. return haystack;
  775. }
  776. haystack++;
  777. }
  778. return NULL;
  779. }
  780. // Pass data_size==0 for zero-terminated string
  781. // Try to replace with FNV1a hash?
  782. static ImU32 ImHash(const void* data, int data_size, ImU32 seed = 0)
  783. {
  784. static ImU32 crc32_lut[256] = { 0 };
  785. if (!crc32_lut[1])
  786. {
  787. const ImU32 polynomial = 0xEDB88320;
  788. for (ImU32 i = 0; i < 256; i++)
  789. {
  790. ImU32 crc = i;
  791. for (ImU32 j = 0; j < 8; j++)
  792. crc = (crc >> 1) ^ (ImU32(-int(crc & 1)) & polynomial);
  793. crc32_lut[i] = crc;
  794. }
  795. }
  796. seed = ~seed;
  797. ImU32 crc = seed;
  798. const unsigned char* current = (const unsigned char*)data;
  799. if (data_size > 0)
  800. {
  801. // Known size
  802. while (data_size--)
  803. crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ *current++];
  804. }
  805. else
  806. {
  807. // Zero-terminated string
  808. while (unsigned char c = *current++)
  809. {
  810. // We support a syntax of "label###id" where only "###id" is included in the hash, and only "label" gets displayed.
  811. // Because this syntax is rarely used we are optimizing for the common case.
  812. // - If we reach ### in the string we discard the hash so far and reset to the seed.
  813. // - We don't do 'current += 2; continue;' after handling ### to keep the code smaller.
  814. if (c == '#' && current[0] == '#' && current[1] == '#')
  815. crc = seed;
  816. crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ c];
  817. }
  818. }
  819. return ~crc;
  820. }
  821. static int ImFormatString(char* buf, int buf_size, const char* fmt, ...)
  822. {
  823. va_list args;
  824. va_start(args, fmt);
  825. int w = vsnprintf(buf, buf_size, fmt, args);
  826. va_end(args);
  827. buf[buf_size-1] = 0;
  828. return (w == -1) ? buf_size : w;
  829. }
  830. static int ImFormatStringV(char* buf, int buf_size, const char* fmt, va_list args)
  831. {
  832. int w = vsnprintf(buf, buf_size, fmt, args);
  833. buf[buf_size-1] = 0;
  834. return (w == -1) ? buf_size : w;
  835. }
  836. ImU32 ImGui::ColorConvertFloat4ToU32(const ImVec4& in)
  837. {
  838. ImU32 out = ((ImU32)(ImSaturate(in.x)*255.f));
  839. out |= ((ImU32)(ImSaturate(in.y)*255.f) << 8);
  840. out |= ((ImU32)(ImSaturate(in.z)*255.f) << 16);
  841. out |= ((ImU32)(ImSaturate(in.w)*255.f) << 24);
  842. return out;
  843. }
  844. // Convert rgb floats ([0-1],[0-1],[0-1]) to hsv floats ([0-1],[0-1],[0-1]), from Foley & van Dam p592
  845. // Optimized http://lolengine.net/blog/2013/01/13/fast-rgb-to-hsv
  846. void ImGui::ColorConvertRGBtoHSV(float r, float g, float b, float& out_h, float& out_s, float& out_v)
  847. {
  848. float K = 0.f;
  849. if (g < b)
  850. {
  851. const float tmp = g; g = b; b = tmp;
  852. K = -1.f;
  853. }
  854. if (r < g)
  855. {
  856. const float tmp = r; r = g; g = tmp;
  857. K = -2.f / 6.f - K;
  858. }
  859. const float chroma = r - (g < b ? g : b);
  860. out_h = fabsf(K + (g - b) / (6.f * chroma + 1e-20f));
  861. out_s = chroma / (r + 1e-20f);
  862. out_v = r;
  863. }
  864. // Convert hsv floats ([0-1],[0-1],[0-1]) to rgb floats ([0-1],[0-1],[0-1]), from Foley & van Dam p593
  865. // also http://en.wikipedia.org/wiki/HSL_and_HSV
  866. void ImGui::ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b)
  867. {
  868. if (s == 0.0f)
  869. {
  870. // gray
  871. out_r = out_g = out_b = v;
  872. return;
  873. }
  874. h = fmodf(h, 1.0f) / (60.0f/360.0f);
  875. int i = (int)h;
  876. float f = h - (float)i;
  877. float p = v * (1.0f - s);
  878. float q = v * (1.0f - s * f);
  879. float t = v * (1.0f - s * (1.0f - f));
  880. switch (i)
  881. {
  882. case 0: out_r = v; out_g = t; out_b = p; break;
  883. case 1: out_r = q; out_g = v; out_b = p; break;
  884. case 2: out_r = p; out_g = v; out_b = t; break;
  885. case 3: out_r = p; out_g = q; out_b = v; break;
  886. case 4: out_r = t; out_g = p; out_b = v; break;
  887. case 5: default: out_r = v; out_g = p; out_b = q; break;
  888. }
  889. }
  890. // Load file content into memory
  891. // Memory allocated with ImGui::MemAlloc(), must be freed by user using ImGui::MemFree()
  892. static bool ImLoadFileToMemory(const char* filename, const char* file_open_mode, void** out_file_data, int* out_file_size, int padding_bytes)
  893. {
  894. IM_ASSERT(filename && file_open_mode && out_file_data && out_file_size);
  895. *out_file_data = NULL;
  896. *out_file_size = 0;
  897. FILE* f;
  898. if ((f = fopen(filename, file_open_mode)) == NULL)
  899. return false;
  900. long file_size_signed;
  901. if (fseek(f, 0, SEEK_END) || (file_size_signed = ftell(f)) == -1 || fseek(f, 0, SEEK_SET))
  902. {
  903. fclose(f);
  904. return false;
  905. }
  906. int file_size = (int)file_size_signed;
  907. void* file_data = ImGui::MemAlloc(file_size + padding_bytes);
  908. if (file_data == NULL)
  909. {
  910. fclose(f);
  911. return false;
  912. }
  913. if (fread(file_data, 1, (size_t)file_size, f) != (size_t)file_size)
  914. {
  915. fclose(f);
  916. ImGui::MemFree(file_data);
  917. return false;
  918. }
  919. if (padding_bytes > 0)
  920. memset((void *)(((char*)file_data) + file_size), 0, padding_bytes);
  921. fclose(f);
  922. *out_file_data = file_data;
  923. *out_file_size = file_size;
  924. return true;
  925. }
  926. //-----------------------------------------------------------------------------
  927. enum ImGuiLayoutType_
  928. {
  929. ImGuiLayoutType_Vertical,
  930. ImGuiLayoutType_Horizontal // FIXME: this is in development, not exposed/functional as a generic feature yet.
  931. };
  932. enum ImGuiButtonFlags_
  933. {
  934. ImGuiButtonFlags_Repeat = 1 << 0,
  935. ImGuiButtonFlags_PressedOnClick = 1 << 1, // return pressed on click only (default requires click+release)
  936. ImGuiButtonFlags_PressedOnRelease = 1 << 2, // return pressed on release only (default requires click+release)
  937. ImGuiButtonFlags_FlattenChilds = 1 << 3,
  938. ImGuiButtonFlags_DontClosePopups = 1 << 4,
  939. ImGuiButtonFlags_Disabled = 1 << 5,
  940. ImGuiButtonFlags_AlignTextBaseLine = 1 << 6
  941. };
  942. enum ImGuiSelectableFlagsPrivate_
  943. {
  944. // NB: need to be in sync with last value of ImGuiSelectableFlags_
  945. ImGuiSelectableFlags_Menu = 1 << 2,
  946. ImGuiSelectableFlags_MenuItem = 1 << 3,
  947. ImGuiSelectableFlags_Disabled = 1 << 4,
  948. ImGuiSelectableFlags_DrawFillAvailWidth = 1 << 5
  949. };
  950. struct ImGuiColMod // Color modifier, backup of modified data so we can restore it
  951. {
  952. ImGuiCol Col;
  953. ImVec4 PreviousValue;
  954. };
  955. struct ImGuiStyleMod // Style modifier, backup of modified data so we can restore it
  956. {
  957. ImGuiStyleVar Var;
  958. ImVec2 PreviousValue;
  959. };
  960. struct ImRect // 2D axis aligned bounding-box
  961. {
  962. ImVec2 Min;
  963. ImVec2 Max;
  964. ImRect() : Min(FLT_MAX,FLT_MAX), Max(-FLT_MAX,-FLT_MAX) {}
  965. ImRect(const ImVec2& min, const ImVec2& max) : Min(min), Max(max) {}
  966. ImRect(const ImVec4& v) : Min(v.x, v.y), Max(v.z, v.w) {}
  967. ImRect(float x1, float y1, float x2, float y2) : Min(x1, y1), Max(x2, y2) {}
  968. ImVec2 GetCenter() const { return ImVec2((Min.x+Max.x)*0.5f, (Min.y+Max.y)*0.5f); }
  969. ImVec2 GetSize() const { return ImVec2(Max.x-Min.x,Max.y-Min.y); }
  970. float GetWidth() const { return Max.x-Min.x; }
  971. float GetHeight() const { return Max.y-Min.y; }
  972. ImVec2 GetTL() const { return Min; }
  973. ImVec2 GetTR() const { return ImVec2(Max.x,Min.y); }
  974. ImVec2 GetBL() const { return ImVec2(Min.x,Max.y); }
  975. ImVec2 GetBR() const { return Max; }
  976. bool Contains(const ImVec2& p) const { return p.x >= Min.x && p.y >= Min.y && p.x < Max.x && p.y < Max.y; }
  977. bool Contains(const ImRect& r) const { return r.Min.x >= Min.x && r.Min.y >= Min.y && r.Max.x < Max.x && r.Max.y < Max.y; }
  978. bool Overlaps(const ImRect& r) const { return r.Min.y < Max.y && r.Max.y > Min.y && r.Min.x < Max.x && r.Max.x > Min.x; }
  979. void Add(const ImVec2& rhs) { Min.x = ImMin(Min.x, rhs.x); Min.y = ImMin(Min.y, rhs.y); Max.x = ImMax(Max.x, rhs.x); Max.y = ImMax(Max.y, rhs.y); }
  980. void Add(const ImRect& rhs) { Min.x = ImMin(Min.x, rhs.Min.x); Min.y = ImMin(Min.y, rhs.Min.y); Max.x = ImMax(Max.x, rhs.Max.x); Max.y = ImMax(Max.y, rhs.Max.y); }
  981. void Expand(const float amount) { Min.x -= amount; Min.y -= amount; Max.x += amount; Max.y += amount; }
  982. void Expand(const ImVec2& amount) { Min.x -= amount.x; Min.y -= amount.y; Max.x += amount.x; Max.y += amount.y; }
  983. void Reduce(const ImVec2& amount) { Min.x += amount.x; Min.y += amount.y; Max.x -= amount.x; Max.y -= amount.y; }
  984. void Clip(const ImRect& clip) { Min.x = ImMax(Min.x, clip.Min.x); Min.y = ImMax(Min.y, clip.Min.y); Max.x = ImMin(Max.x, clip.Max.x); Max.y = ImMin(Max.y, clip.Max.y); }
  985. void Round() { Min.x = (float)(int)Min.x; Min.y = (float)(int)Min.y; Max.x = (float)(int)Max.x; Max.y = (float)(int)Max.y; }
  986. ImVec2 GetClosestPoint(ImVec2 p, bool on_edge) const
  987. {
  988. if (!on_edge && Contains(p))
  989. return p;
  990. if (p.x > Max.x) p.x = Max.x;
  991. else if (p.x < Min.x) p.x = Min.x;
  992. if (p.y > Max.y) p.y = Max.y;
  993. else if (p.y < Min.y) p.y = Min.y;
  994. return p;
  995. }
  996. };
  997. struct ImGuiGroupData
  998. {
  999. ImVec2 BackupCursorPos;
  1000. ImVec2 BackupCursorMaxPos;
  1001. float BackupColumnsStartX;
  1002. float BackupCurrentLineHeight;
  1003. float BackupCurrentLineTextBaseOffset;
  1004. float BackupLogLinePosY;
  1005. bool AdvanceCursor;
  1006. };
  1007. // Simple column measurement currently used for menu items. This is very short-sighted for now.
  1008. struct ImGuiSimpleColumns
  1009. {
  1010. int Count;
  1011. float Spacing;
  1012. float Width, NextWidth;
  1013. float Pos[8], NextWidths[8];
  1014. ImGuiSimpleColumns() { Count = 0; Spacing = 0.0f; Width = 0.0f; NextWidth = 0.0f; memset(Pos, 0, sizeof(Pos)); memset(NextWidths, 0, sizeof(NextWidths)); }
  1015. void Update(int count, float spacing, bool clear)
  1016. {
  1017. IM_ASSERT(Count <= IM_ARRAYSIZE(Pos));
  1018. Count = count;
  1019. Width = NextWidth = 0.0f;
  1020. Spacing = spacing;
  1021. if (clear) memset(NextWidths, 0, sizeof(NextWidths));
  1022. for (int i = 0; i < Count; i++)
  1023. {
  1024. if (i > 0 && NextWidths[i] > 0.0f)
  1025. Width += Spacing;
  1026. Pos[i] = (float)(int)Width;
  1027. Width += NextWidths[i];
  1028. NextWidths[i] = 0.0f;
  1029. }
  1030. }
  1031. float DeclColumns(float w0, float w1, float w2) // not using va_arg because they promote float to double
  1032. {
  1033. NextWidth = 0.0f;
  1034. NextWidths[0] = ImMax(NextWidths[0], w0);
  1035. NextWidths[1] = ImMax(NextWidths[1], w1);
  1036. NextWidths[2] = ImMax(NextWidths[2], w2);
  1037. for (int i = 0; i < 3; i++)
  1038. NextWidth += NextWidths[i] + ((i > 0 && NextWidths[i] > 0.0f) ? Spacing : 0.0f);
  1039. return ImMax(Width, NextWidth);
  1040. }
  1041. float CalcExtraSpace(float avail_w)
  1042. {
  1043. return ImMax(0.0f, avail_w - Width);
  1044. }
  1045. };
  1046. // Temporary per-window data, reset at the beginning of the frame
  1047. struct ImGuiDrawContext
  1048. {
  1049. ImVec2 CursorPos;
  1050. ImVec2 CursorPosPrevLine;
  1051. ImVec2 CursorStartPos;
  1052. ImVec2 CursorMaxPos; // Implicitly calculate the size of our contents, always extending. Saved into window->SizeContents at the end of the frame
  1053. float CurrentLineHeight;
  1054. float CurrentLineTextBaseOffset;
  1055. float PrevLineHeight;
  1056. float PrevLineTextBaseOffset;
  1057. float LogLinePosY;
  1058. int TreeDepth;
  1059. ImGuiID LastItemID;
  1060. ImRect LastItemRect;
  1061. bool LastItemHoveredAndUsable;
  1062. bool LastItemHoveredRect;
  1063. bool MenuBarAppending;
  1064. float MenuBarOffsetX;
  1065. ImVector<ImGuiWindow*> ChildWindows;
  1066. ImGuiStorage* StateStorage;
  1067. ImGuiLayoutType LayoutType;
  1068. // We store the current settings outside of the vectors to increase memory locality (reduce cache misses). The vectors are rarely modified. Also it allows us to not heap allocate for short-lived windows which are not using those settings.
  1069. bool ButtonRepeat; // == ButtonRepeatStack.back() [empty == false]
  1070. bool AllowKeyboardFocus; // == AllowKeyboardFocusStack.back() [empty == true]
  1071. float ItemWidth; // == ItemWidthStack.back(). 0.0: default, >0.0: width in pixels, <0.0: align xx pixels to the right of window
  1072. float TextWrapPos; // == TextWrapPosStack.back() [empty == -1.0f]
  1073. ImVector<bool> ButtonRepeatStack;
  1074. ImVector<bool> AllowKeyboardFocusStack;
  1075. ImVector<float> ItemWidthStack;
  1076. ImVector<float> TextWrapPosStack;
  1077. ImVector<ImGuiGroupData>GroupStack;
  1078. ImGuiColorEditMode ColorEditMode;
  1079. int StackSizesBackup[6]; // Store size of various stacks for asserting
  1080. float ColumnsStartX; // Indentation / start position from left of window (increased by TreePush/TreePop, etc.)
  1081. float ColumnsOffsetX; // Offset to the current column (if ColumnsCurrent > 0). FIXME: This and the above should be a stack to allow use cases like Tree->Column->Tree. Need revamp columns API.
  1082. int ColumnsCurrent;
  1083. int ColumnsCount;
  1084. ImVec2 ColumnsStartPos;
  1085. float ColumnsCellMinY;
  1086. float ColumnsCellMaxY;
  1087. bool ColumnsShowBorders;
  1088. ImGuiID ColumnsSetID;
  1089. ImVector<float> ColumnsOffsetsT; // Columns offset normalized 0.0 (far left) -> 1.0 (far right)
  1090. ImGuiDrawContext()
  1091. {
  1092. CursorPos = CursorPosPrevLine = CursorStartPos = CursorMaxPos = ImVec2(0.0f, 0.0f);
  1093. CurrentLineHeight = PrevLineHeight = 0.0f;
  1094. CurrentLineTextBaseOffset = PrevLineTextBaseOffset = 0.0f;
  1095. LogLinePosY = -1.0f;
  1096. TreeDepth = 0;
  1097. LastItemID = 0;
  1098. LastItemRect = ImRect(0.0f,0.0f,0.0f,0.0f);
  1099. LastItemHoveredAndUsable = LastItemHoveredRect = false;
  1100. MenuBarAppending = false;
  1101. MenuBarOffsetX = 0.0f;
  1102. StateStorage = NULL;
  1103. LayoutType = ImGuiLayoutType_Vertical;
  1104. ItemWidth = 0.0f;
  1105. ButtonRepeat = false;
  1106. AllowKeyboardFocus = true;
  1107. TextWrapPos = -1.0f;
  1108. ColorEditMode = ImGuiColorEditMode_RGB;
  1109. memset(StackSizesBackup, 0, sizeof(StackSizesBackup));
  1110. ColumnsStartX = 0.0f;
  1111. ColumnsOffsetX = 0.0f;
  1112. ColumnsCurrent = 0;
  1113. ColumnsCount = 1;
  1114. ColumnsStartPos = ImVec2(0.0f, 0.0f);
  1115. ColumnsCellMinY = ColumnsCellMaxY = 0.0f;
  1116. ColumnsShowBorders = true;
  1117. ColumnsSetID = 0;
  1118. }
  1119. };
  1120. // Internal state of the currently focused/edited text input box
  1121. struct ImGuiTextEditState
  1122. {
  1123. ImGuiID Id; // widget id owning the text state
  1124. ImVector<ImWchar> Text; // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so we copy into own buffer.
  1125. ImVector<char> InitialText; // backup of end-user buffer at the time of focus (in UTF-8, unaltered)
  1126. ImVector<char> TempTextBuffer;
  1127. int CurLenA, CurLenW; // we need to maintain our buffer length in both UTF-8 and wchar format.
  1128. int BufSizeA; // end-user buffer size
  1129. float ScrollX;
  1130. STB_TexteditState StbState;
  1131. float CursorAnim;
  1132. bool CursorFollow;
  1133. ImVec2 InputCursorScreenPos; // Cursor position in screen space to be used by IME callback.
  1134. bool SelectedAllMouseLock;
  1135. ImGuiTextEditState() { memset(this, 0, sizeof(*this)); }
  1136. void CursorAnimReset() { CursorAnim = -0.30f; } // After a user-input the cursor stays on for a while without blinking
  1137. bool CursorIsVisible() const { return CursorAnim <= 0.0f || fmodf(CursorAnim, 1.20f) <= 0.80f; } // Blinking
  1138. bool HasSelection() const { return StbState.select_start != StbState.select_end; }
  1139. void ClearSelection() { StbState.select_start = StbState.select_end = StbState.cursor; }
  1140. void SelectAll() { StbState.select_start = 0; StbState.select_end = CurLenW; StbState.cursor = StbState.select_end; StbState.has_preferred_x = false; }
  1141. void OnKeyPressed(int key);
  1142. };
  1143. // Data saved in imgui.ini file
  1144. struct ImGuiIniData
  1145. {
  1146. char* Name;
  1147. ImGuiID ID;
  1148. ImVec2 Pos;
  1149. ImVec2 Size;
  1150. bool Collapsed;
  1151. };
  1152. struct ImGuiMouseCursorData
  1153. {
  1154. ImGuiMouseCursor Type;
  1155. ImVec2 Offset;
  1156. ImVec2 Size;
  1157. ImVec2 TexUvMin[2];
  1158. ImVec2 TexUvMax[2];
  1159. };
  1160. struct ImGuiPopupRef
  1161. {
  1162. ImGuiID PopupID; // Set on OpenPopup()
  1163. ImGuiWindow* Window; // Resolved on BeginPopup() - may stay unresolved if user never calls OpenPopup()
  1164. ImGuiWindow* ParentWindow; // Set on OpenPopup()
  1165. ImGuiID ParentMenuSet; // Set on OpenPopup()
  1166. ImGuiPopupRef(ImGuiID id, ImGuiWindow* parent_window, ImGuiID parent_menu_set) { PopupID = id; Window = NULL; ParentWindow = parent_window; ParentMenuSet = parent_menu_set; }
  1167. };
  1168. // Main state for ImGui
  1169. struct ImGuiState
  1170. {
  1171. bool Initialized;
  1172. ImGuiIO IO;
  1173. ImGuiStyle Style;
  1174. ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back()
  1175. float FontSize; // (Shortcut) == FontBaseSize * g.CurrentWindow->FontWindowScale == window->FontSize()
  1176. float FontBaseSize; // (Shortcut) == IO.FontGlobalScale * Font->Scale * Font->FontSize. Size of characters.
  1177. ImVec2 FontTexUvWhitePixel; // (Shortcut) == Font->TexUvForWhite
  1178. float Time;
  1179. int FrameCount;
  1180. int FrameCountRendered;
  1181. ImVector<ImGuiWindow*> Windows;
  1182. ImVector<ImGuiWindow*> WindowsSortBuffer;
  1183. ImGuiWindow* CurrentWindow; // Being drawn into
  1184. ImVector<ImGuiWindow*> CurrentWindowStack;
  1185. ImGuiWindow* FocusedWindow; // Will catch keyboard inputs
  1186. ImGuiWindow* HoveredWindow; // Will catch mouse inputs
  1187. ImGuiWindow* HoveredRootWindow; // Will catch mouse inputs (for focus/move only)
  1188. ImGuiID HoveredId; // Hovered widget
  1189. ImGuiID HoveredIdPreviousFrame;
  1190. ImGuiID ActiveId; // Active widget
  1191. ImGuiID ActiveIdPreviousFrame;
  1192. bool ActiveIdIsAlive;
  1193. bool ActiveIdIsJustActivated; // Set at the time of activation for one frame
  1194. bool ActiveIdIsFocusedOnly; // Set only by active widget. Denote focus but no active interaction
  1195. ImGuiWindow* ActiveIdWindow;
  1196. ImGuiWindow* MovedWindow; // Track the child window we clicked on to move a window. Pointer is only valid if ActiveID is the "#MOVE" identifier of a window.
  1197. float SettingsDirtyTimer;
  1198. ImVector<ImGuiIniData> Settings;
  1199. int DisableHideTextAfterDoubleHash;
  1200. ImVector<ImGuiColMod> ColorModifiers;
  1201. ImVector<ImGuiStyleMod> StyleModifiers;
  1202. ImVector<ImFont*> FontStack;
  1203. ImVector<ImGuiPopupRef> OpenedPopupStack; // Which popups are open
  1204. ImVector<ImGuiPopupRef> CurrentPopupStack; // Which level of BeginPopup() we are in (reset every frame)
  1205. ImVec2 SetNextWindowPosVal;
  1206. ImGuiSetCond SetNextWindowPosCond;
  1207. ImVec2 SetNextWindowSizeVal;
  1208. ImGuiSetCond SetNextWindowSizeCond;
  1209. bool SetNextWindowCollapsedVal;
  1210. ImGuiSetCond SetNextWindowCollapsedCond;
  1211. bool SetNextWindowFocus;
  1212. bool SetNextTreeNodeOpenedVal;
  1213. ImGuiSetCond SetNextTreeNodeOpenedCond;
  1214. // Render
  1215. ImVector<ImDrawList*> RenderDrawLists[3];
  1216. float ModalWindowDarkeningRatio;
  1217. ImDrawList OverlayDrawList; // Optional software render of mouse cursors, if io.MouseDrawCursor is set + a few debug overlays
  1218. ImGuiMouseCursor MouseCursor;
  1219. ImGuiMouseCursorData MouseCursorData[ImGuiMouseCursor_Count_];
  1220. // Widget state
  1221. ImGuiTextEditState InputTextState;
  1222. ImGuiID ScalarAsInputTextId; // Temporary text input when CTRL+clicking on a slider, etc.
  1223. ImGuiStorage ColorEditModeStorage; // Store user selection of color edit mode
  1224. ImVec2 ActiveClickDeltaToCenter;
  1225. float DragCurrentValue; // current dragged value, always float, not rounded by end-user precision settings
  1226. ImVec2 DragLastMouseDelta;
  1227. float DragSpeedDefaultRatio; // if speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio
  1228. float DragSpeedScaleSlow;
  1229. float DragSpeedScaleFast;
  1230. float ScrollbarClickDeltaToGrabCenter; // distance between mouse and center of grab box, normalized in parent space
  1231. char Tooltip[1024];
  1232. char* PrivateClipboard; // if no custom clipboard handler is defined
  1233. // Logging
  1234. bool LogEnabled;
  1235. FILE* LogFile;
  1236. ImGuiTextBuffer* LogClipboard; // pointer so our GImGui static constructor doesn't call heap allocators.
  1237. int LogStartDepth;
  1238. int LogAutoExpandMaxDepth;
  1239. // Misc
  1240. float FramerateSecPerFrame[120]; // calculate estimate of framerate for user
  1241. int FramerateSecPerFrameIdx;
  1242. float FramerateSecPerFrameAccum;
  1243. bool CaptureMouseNextFrame; // explicit capture via CaptureInputs() sets those flags
  1244. bool CaptureKeyboardNextFrame;
  1245. char TempBuffer[1024*3+1]; // temporary text buffer
  1246. ImGuiState()
  1247. {
  1248. Initialized = false;
  1249. Font = NULL;
  1250. FontSize = FontBaseSize = 0.0f;
  1251. FontTexUvWhitePixel = ImVec2(0.0f, 0.0f);
  1252. Time = 0.0f;
  1253. FrameCount = 0;
  1254. FrameCountRendered = -1;
  1255. CurrentWindow = NULL;
  1256. FocusedWindow = NULL;
  1257. HoveredWindow = NULL;
  1258. HoveredRootWindow = NULL;
  1259. HoveredId = 0;
  1260. HoveredIdPreviousFrame = 0;
  1261. ActiveId = 0;
  1262. ActiveIdPreviousFrame = 0;
  1263. ActiveIdIsAlive = false;
  1264. ActiveIdIsJustActivated = false;
  1265. ActiveIdIsFocusedOnly = false;
  1266. ActiveIdWindow = NULL;
  1267. MovedWindow = NULL;
  1268. SettingsDirtyTimer = 0.0f;
  1269. DisableHideTextAfterDoubleHash = 0;
  1270. SetNextWindowPosVal = ImVec2(0.0f, 0.0f);
  1271. SetNextWindowPosCond = 0;
  1272. SetNextWindowSizeVal = ImVec2(0.0f, 0.0f);
  1273. SetNextWindowSizeCond = 0;
  1274. SetNextWindowCollapsedVal = false;
  1275. SetNextWindowCollapsedCond = 0;
  1276. SetNextWindowFocus = false;
  1277. SetNextTreeNodeOpenedVal = false;
  1278. SetNextTreeNodeOpenedCond = 0;
  1279. ScalarAsInputTextId = 0;
  1280. ActiveClickDeltaToCenter = ImVec2(0.0f, 0.0f);
  1281. DragCurrentValue = 0.0f;
  1282. DragLastMouseDelta = ImVec2(0.0f, 0.0f);
  1283. DragSpeedDefaultRatio = 0.01f;
  1284. DragSpeedScaleSlow = 0.01f;
  1285. DragSpeedScaleFast = 10.0f;
  1286. ScrollbarClickDeltaToGrabCenter = 0.0f;
  1287. memset(Tooltip, 0, sizeof(Tooltip));
  1288. PrivateClipboard = NULL;
  1289. ModalWindowDarkeningRatio = 0.0f;
  1290. OverlayDrawList._OwnerName = "##Overlay"; // Give it a name for debugging
  1291. MouseCursor = ImGuiMouseCursor_Arrow;
  1292. LogEnabled = false;
  1293. LogFile = NULL;
  1294. LogClipboard = NULL;
  1295. LogStartDepth = 0;
  1296. LogAutoExpandMaxDepth = 2;
  1297. memset(FramerateSecPerFrame, 0, sizeof(FramerateSecPerFrame));
  1298. FramerateSecPerFrameIdx = 0;
  1299. FramerateSecPerFrameAccum = 0.0f;
  1300. CaptureMouseNextFrame = CaptureKeyboardNextFrame = false;
  1301. }
  1302. };
  1303. static ImGuiState GImDefaultState; // Internal state storage
  1304. static ImGuiState* GImGui = &GImDefaultState; // We access everything through this pointer. NB: this pointer is always assumed to be != NULL
  1305. struct ImGuiWindow
  1306. {
  1307. char* Name;
  1308. ImGuiID ID;
  1309. ImGuiWindowFlags Flags;
  1310. ImVec2 PosFloat;
  1311. ImVec2 Pos; // Position rounded-up to nearest pixel
  1312. ImVec2 Size; // Current size (==SizeFull or collapsed title bar size)
  1313. ImVec2 SizeFull; // Size when non collapsed
  1314. ImVec2 SizeContents; // Size of contents (== extents reach of the drawing cursor) from previous frame
  1315. ImVec2 WindowPadding; // Window padding at the time of begin. We need to lock it, in particular manipulation of the ShowBorder would have an effect
  1316. ImGuiID MoveID; // == window->GetID("#MOVE")
  1317. float ScrollY;
  1318. float ScrollTargetRelY; // target scroll position. stored as cursor position with scrolling canceled out, so the highest point is always 0.0f. (FLT_MAX for no change)
  1319. float ScrollTargetCenterRatioY; // 0.0f = scroll so that target position is at top, 0.5f = scroll so that target position is centered
  1320. bool ScrollbarY;
  1321. bool Active; // Set to true on Begin()
  1322. bool WasActive;
  1323. bool Accessed; // Set to true when any widget access the current window
  1324. bool Collapsed; // Set when collapsing window to become only title-bar
  1325. bool SkipItems; // == Visible && !Collapsed
  1326. int BeginCount; // Number of Begin() during the current frame (generally 0 or 1, 1+ if appending via multiple Begin/End pairs)
  1327. ImGuiID PopupID; // ID in the popup stack when this window is used as a popup/menu (because we use generic Name/ID for recycling)
  1328. int AutoFitFramesX, AutoFitFramesY;
  1329. bool AutoFitOnlyGrows;
  1330. int AutoPosLastDirection;
  1331. int HiddenFrames;
  1332. int SetWindowPosAllowFlags; // bit ImGuiSetCond_*** specify if SetWindowPos() call will succeed with this particular flag.
  1333. int SetWindowSizeAllowFlags; // bit ImGuiSetCond_*** specify if SetWindowSize() call will succeed with this particular flag.
  1334. int SetWindowCollapsedAllowFlags; // bit ImGuiSetCond_*** specify if SetWindowCollapsed() call will succeed with this particular flag.
  1335. bool SetWindowPosCenterWanted;
  1336. ImGuiDrawContext DC; // Temporary per-window data, reset at the beginning of the frame
  1337. ImVector<ImGuiID> IDStack; // ID stack. ID are hashes seeded with the value at the top of the stack
  1338. ImVec4 ClipRect; // = DrawList->clip_rect_stack.back(). Scissoring / clipping rectangle. x1, y1, x2, y2.
  1339. ImRect ClippedWindowRect; // = ClipRect just after setup in Begin()
  1340. int LastFrameDrawn;
  1341. float ItemWidthDefault;
  1342. ImGuiSimpleColumns MenuColumns; // Simplified columns storage for menu items
  1343. ImGuiStorage StateStorage;
  1344. float FontWindowScale; // Scale multiplier per-window
  1345. ImDrawList* DrawList;
  1346. ImGuiWindow* RootWindow;
  1347. ImGuiWindow* RootNonPopupWindow;
  1348. // Focus
  1349. int FocusIdxAllCounter; // Start at -1 and increase as assigned via FocusItemRegister()
  1350. int FocusIdxTabCounter; // (same, but only count widgets which you can Tab through)
  1351. int FocusIdxAllRequestCurrent; // Item being requested for focus
  1352. int FocusIdxTabRequestCurrent; // Tab-able item being requested for focus
  1353. int FocusIdxAllRequestNext; // Item being requested for focus, for next update (relies on layout to be stable between the frame pressing TAB and the next frame)
  1354. int FocusIdxTabRequestNext; // "
  1355. public:
  1356. ImGuiWindow(const char* name);
  1357. ~ImGuiWindow();
  1358. ImGuiID GetID(const char* str, const char* str_end = NULL);
  1359. ImGuiID GetID(const void* ptr);
  1360. bool FocusItemRegister(bool is_active, bool tab_stop = true); // Return true if focus is requested
  1361. void FocusItemUnregister();
  1362. ImRect Rect() const { return ImRect(Pos, Pos+Size); }
  1363. float CalcFontSize() const { return GImGui->FontBaseSize * FontWindowScale; }
  1364. float TitleBarHeight() const { return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : CalcFontSize() + GImGui->Style.FramePadding.y * 2.0f; }
  1365. ImRect TitleBarRect() const { return ImRect(Pos, Pos + ImVec2(SizeFull.x, TitleBarHeight())); }
  1366. float MenuBarHeight() const { return (Flags & ImGuiWindowFlags_MenuBar) ? CalcFontSize() + GImGui->Style.FramePadding.y * 2.0f : 0.0f; }
  1367. ImRect MenuBarRect() const { float y1 = Pos.y + TitleBarHeight(); return ImRect(Pos.x, y1, Pos.x + SizeFull.x, y1 + MenuBarHeight()); }
  1368. float ScrollbarWidth() const { return ScrollbarY ? GImGui->Style.ScrollbarWidth : 0.0f; }
  1369. ImU32 Color(ImGuiCol idx, float a=1.f) const { ImVec4 c = GImGui->Style.Colors[idx]; c.w *= GImGui->Style.Alpha * a; return ImGui::ColorConvertFloat4ToU32(c); }
  1370. ImU32 Color(const ImVec4& col) const { ImVec4 c = col; c.w *= GImGui->Style.Alpha; return ImGui::ColorConvertFloat4ToU32(c); }
  1371. };
  1372. static inline ImGuiWindow* GetCurrentWindow()
  1373. {
  1374. // If this ever crash it probably means that ImGui::NewFrame() hasn't been called. We should always have a CurrentWindow in the stack (there is an implicit "Debug" window)
  1375. ImGuiState& g = *GImGui;
  1376. g.CurrentWindow->Accessed = true;
  1377. return g.CurrentWindow;
  1378. }
  1379. static inline void SetCurrentWindow(ImGuiWindow* window)
  1380. {
  1381. ImGuiState& g = *GImGui;
  1382. g.CurrentWindow = window;
  1383. if (window)
  1384. g.FontSize = window->CalcFontSize();
  1385. }
  1386. static inline ImGuiWindow* GetParentWindow()
  1387. {
  1388. ImGuiState& g = *GImGui;
  1389. IM_ASSERT(g.CurrentWindowStack.Size >= 2);
  1390. return g.CurrentWindowStack[g.CurrentWindowStack.Size - 2];
  1391. }
  1392. static void SetActiveId(ImGuiID id, ImGuiWindow* window = NULL)
  1393. {
  1394. ImGuiState& g = *GImGui;
  1395. g.ActiveId = id;
  1396. g.ActiveIdIsFocusedOnly = false;
  1397. g.ActiveIdIsJustActivated = true;
  1398. g.ActiveIdWindow = window;
  1399. }
  1400. static void RegisterAliveId(ImGuiID id)
  1401. {
  1402. ImGuiState& g = *GImGui;
  1403. if (g.ActiveId == id)
  1404. g.ActiveIdIsAlive = true;
  1405. }
  1406. //-----------------------------------------------------------------------------
  1407. // Helper: Key->value storage
  1408. void ImGuiStorage::Clear()
  1409. {
  1410. Data.clear();
  1411. }
  1412. // std::lower_bound but without the bullshit
  1413. static ImVector<ImGuiStorage::Pair>::iterator LowerBound(ImVector<ImGuiStorage::Pair>& data, ImU32 key)
  1414. {
  1415. ImVector<ImGuiStorage::Pair>::iterator first = data.begin();
  1416. ImVector<ImGuiStorage::Pair>::iterator last = data.end();
  1417. int count = (int)(last - first);
  1418. while (count > 0)
  1419. {
  1420. int count2 = count / 2;
  1421. ImVector<ImGuiStorage::Pair>::iterator mid = first + count2;
  1422. if (mid->key < key)
  1423. {
  1424. first = ++mid;
  1425. count -= count2 + 1;
  1426. }
  1427. else
  1428. {
  1429. count = count2;
  1430. }
  1431. }
  1432. return first;
  1433. }
  1434. int ImGuiStorage::GetInt(ImU32 key, int default_val) const
  1435. {
  1436. ImVector<Pair>::iterator it = LowerBound(const_cast<ImVector<ImGuiStorage::Pair>&>(Data), key);
  1437. if (it == Data.end() || it->key != key)
  1438. return default_val;
  1439. return it->val_i;
  1440. }
  1441. float ImGuiStorage::GetFloat(ImU32 key, float default_val) const
  1442. {
  1443. ImVector<Pair>::iterator it = LowerBound(const_cast<ImVector<ImGuiStorage::Pair>&>(Data), key);
  1444. if (it == Data.end() || it->key != key)
  1445. return default_val;
  1446. return it->val_f;
  1447. }
  1448. void* ImGuiStorage::GetVoidPtr(ImGuiID key) const
  1449. {
  1450. ImVector<Pair>::iterator it = LowerBound(const_cast<ImVector<ImGuiStorage::Pair>&>(Data), key);
  1451. if (it == Data.end() || it->key != key)
  1452. return NULL;
  1453. return it->val_p;
  1454. }
  1455. // References are only valid until a new value is added to the storage. Calling a Set***() function or a Get***Ref() function invalidates the pointer.
  1456. int* ImGuiStorage::GetIntRef(ImGuiID key, int default_val)
  1457. {
  1458. ImVector<Pair>::iterator it = LowerBound(Data, key);
  1459. if (it == Data.end() || it->key != key)
  1460. it = Data.insert(it, Pair(key, default_val));
  1461. return &it->val_i;
  1462. }
  1463. float* ImGuiStorage::GetFloatRef(ImGuiID key, float default_val)
  1464. {
  1465. ImVector<Pair>::iterator it = LowerBound(Data, key);
  1466. if (it == Data.end() || it->key != key)
  1467. it = Data.insert(it, Pair(key, default_val));
  1468. return &it->val_f;
  1469. }
  1470. void** ImGuiStorage::GetVoidPtrRef(ImGuiID key, void* default_val)
  1471. {
  1472. ImVector<Pair>::iterator it = LowerBound(Data, key);
  1473. if (it == Data.end() || it->key != key)
  1474. it = Data.insert(it, Pair(key, default_val));
  1475. return &it->val_p;
  1476. }
  1477. // FIXME-OPT: Need a way to reuse the result of lower_bound when doing GetInt()/SetInt() - not too bad because it only happens on explicit interaction (maximum one a frame)
  1478. void ImGuiStorage::SetInt(ImU32 key, int val)
  1479. {
  1480. ImVector<Pair>::iterator it = LowerBound(Data, key);
  1481. if (it == Data.end() || it->key != key)
  1482. {
  1483. Data.insert(it, Pair(key, val));
  1484. return;
  1485. }
  1486. it->val_i = val;
  1487. }
  1488. void ImGuiStorage::SetFloat(ImU32 key, float val)
  1489. {
  1490. ImVector<Pair>::iterator it = LowerBound(Data, key);
  1491. if (it == Data.end() || it->key != key)
  1492. {
  1493. Data.insert(it, Pair(key, val));
  1494. return;
  1495. }
  1496. it->val_f = val;
  1497. }
  1498. void ImGuiStorage::SetVoidPtr(ImU32 key, void* val)
  1499. {
  1500. ImVector<Pair>::iterator it = LowerBound(Data, key);
  1501. if (it == Data.end() || it->key != key)
  1502. {
  1503. Data.insert(it, Pair(key, val));
  1504. return;
  1505. }
  1506. it->val_p = val;
  1507. }
  1508. void ImGuiStorage::SetAllInt(int v)
  1509. {
  1510. for (int i = 0; i < Data.Size; i++)
  1511. Data[i].val_i = v;
  1512. }
  1513. //-----------------------------------------------------------------------------
  1514. // Helper: Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]"
  1515. ImGuiTextFilter::ImGuiTextFilter(const char* default_filter)
  1516. {
  1517. if (default_filter)
  1518. {
  1519. ImFormatString(InputBuf, IM_ARRAYSIZE(InputBuf), "%s", default_filter);
  1520. Build();
  1521. }
  1522. else
  1523. {
  1524. InputBuf[0] = 0;
  1525. CountGrep = 0;
  1526. }
  1527. }
  1528. void ImGuiTextFilter::Draw(const char* label, float width)
  1529. {
  1530. if (width > 0.0f)
  1531. ImGui::PushItemWidth(width);
  1532. ImGui::InputText(label, InputBuf, IM_ARRAYSIZE(InputBuf));
  1533. if (width > 0.0f)
  1534. ImGui::PopItemWidth();
  1535. Build();
  1536. }
  1537. void ImGuiTextFilter::TextRange::split(char separator, ImVector<TextRange>& out)
  1538. {
  1539. out.resize(0);
  1540. const char* wb = b;
  1541. const char* we = wb;
  1542. while (we < e)
  1543. {
  1544. if (*we == separator)
  1545. {
  1546. out.push_back(TextRange(wb, we));
  1547. wb = we + 1;
  1548. }
  1549. we++;
  1550. }
  1551. if (wb != we)
  1552. out.push_back(TextRange(wb, we));
  1553. }
  1554. void ImGuiTextFilter::Build()
  1555. {
  1556. Filters.resize(0);
  1557. TextRange input_range(InputBuf, InputBuf+strlen(InputBuf));
  1558. input_range.split(',', Filters);
  1559. CountGrep = 0;
  1560. for (int i = 0; i != Filters.Size; i++)
  1561. {
  1562. Filters[i].trim_blanks();
  1563. if (Filters[i].empty())
  1564. continue;
  1565. if (Filters[i].front() != '-')
  1566. CountGrep += 1;
  1567. }
  1568. }
  1569. bool ImGuiTextFilter::PassFilter(const char* val) const
  1570. {
  1571. if (Filters.empty())
  1572. return true;
  1573. if (val == NULL)
  1574. val = "";
  1575. for (int i = 0; i != Filters.Size; i++)
  1576. {
  1577. const TextRange& f = Filters[i];
  1578. if (f.empty())
  1579. continue;
  1580. if (f.front() == '-')
  1581. {
  1582. // Subtract
  1583. if (ImStristr(val, f.begin()+1, f.end()) != NULL)
  1584. return false;
  1585. }
  1586. else
  1587. {
  1588. // Grep
  1589. if (ImStristr(val, f.begin(), f.end()) != NULL)
  1590. return true;
  1591. }
  1592. }
  1593. // Implicit * grep
  1594. if (CountGrep == 0)
  1595. return true;
  1596. return false;
  1597. }
  1598. // On some platform vsnprintf() takes va_list by reference and modifies it.
  1599. // va_copy is the 'correct' way to copy a va_list but Visual Studio prior to 2013 doesn't have it.
  1600. #ifndef va_copy
  1601. #define va_copy(dest, src) (dest = src)
  1602. #endif
  1603. // Helper: Text buffer for logging/accumulating text
  1604. void ImGuiTextBuffer::appendv(const char* fmt, va_list args)
  1605. {
  1606. va_list args_copy;
  1607. va_copy(args_copy, args);
  1608. int len = vsnprintf(NULL, 0, fmt, args); // FIXME-OPT: could do a first pass write attempt, likely successful on first pass.
  1609. if (len <= 0)
  1610. return;
  1611. const int write_off = Buf.Size;
  1612. const int needed_sz = write_off + len;
  1613. if (write_off + len >= Buf.Capacity)
  1614. {
  1615. int double_capacity = Buf.Capacity * 2;
  1616. Buf.reserve(needed_sz > double_capacity ? needed_sz : double_capacity);
  1617. }
  1618. Buf.resize(needed_sz);
  1619. ImFormatStringV(&Buf[write_off] - 1, len+1, fmt, args_copy);
  1620. }
  1621. void ImGuiTextBuffer::append(const char* fmt, ...)
  1622. {
  1623. va_list args;
  1624. va_start(args, fmt);
  1625. appendv(fmt, args);
  1626. va_end(args);
  1627. }
  1628. //-----------------------------------------------------------------------------
  1629. ImGuiWindow::ImGuiWindow(const char* name)
  1630. {
  1631. Name = ImStrdup(name);
  1632. ID = ImHash(name, 0);
  1633. IDStack.push_back(ID);
  1634. MoveID = GetID("#MOVE");
  1635. Flags = 0;
  1636. PosFloat = Pos = ImVec2(0.0f, 0.0f);
  1637. Size = SizeFull = ImVec2(0.0f, 0.0f);
  1638. SizeContents = ImVec2(0.0f, 0.0f);
  1639. WindowPadding = ImVec2(0.0f, 0.0f);
  1640. ScrollY = 0.0f;
  1641. ScrollTargetRelY = FLT_MAX;
  1642. ScrollTargetCenterRatioY = 0.5f;
  1643. ScrollbarY = false;
  1644. Active = WasActive = false;
  1645. Accessed = false;
  1646. Collapsed = false;
  1647. SkipItems = false;
  1648. BeginCount = 0;
  1649. PopupID = 0;
  1650. AutoFitFramesX = AutoFitFramesY = -1;
  1651. AutoFitOnlyGrows = false;
  1652. AutoPosLastDirection = -1;
  1653. HiddenFrames = 0;
  1654. SetWindowPosAllowFlags = SetWindowSizeAllowFlags = SetWindowCollapsedAllowFlags = ImGuiSetCond_Always | ImGuiSetCond_Once | ImGuiSetCond_FirstUseEver | ImGuiSetCond_Appearing;
  1655. SetWindowPosCenterWanted = false;
  1656. LastFrameDrawn = -1;
  1657. ItemWidthDefault = 0.0f;
  1658. FontWindowScale = 1.0f;
  1659. DrawList = (ImDrawList*)ImGui::MemAlloc(sizeof(ImDrawList));
  1660. new(DrawList) ImDrawList();
  1661. DrawList->_OwnerName = Name;
  1662. RootWindow = NULL;
  1663. RootNonPopupWindow = NULL;
  1664. FocusIdxAllCounter = FocusIdxTabCounter = -1;
  1665. FocusIdxAllRequestCurrent = FocusIdxTabRequestCurrent = IM_INT_MAX;
  1666. FocusIdxAllRequestNext = FocusIdxTabRequestNext = IM_INT_MAX;
  1667. }
  1668. ImGuiWindow::~ImGuiWindow()
  1669. {
  1670. DrawList->~ImDrawList();
  1671. ImGui::MemFree(DrawList);
  1672. DrawList = NULL;
  1673. ImGui::MemFree(Name);
  1674. Name = NULL;
  1675. }
  1676. ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end)
  1677. {
  1678. ImGuiID seed = IDStack.back();
  1679. ImGuiID id = ImHash(str, str_end ? (int)(str_end - str) : 0, seed);
  1680. RegisterAliveId(id);
  1681. return id;
  1682. }
  1683. ImGuiID ImGuiWindow::GetID(const void* ptr)
  1684. {
  1685. ImGuiID seed = IDStack.back();
  1686. ImGuiID id = ImHash(&ptr, sizeof(void*), seed);
  1687. RegisterAliveId(id);
  1688. return id;
  1689. }
  1690. bool ImGuiWindow::FocusItemRegister(bool is_active, bool tab_stop)
  1691. {
  1692. ImGuiState& g = *GImGui;
  1693. ImGuiWindow* window = GetCurrentWindow();
  1694. const bool allow_keyboard_focus = window->DC.AllowKeyboardFocus;
  1695. FocusIdxAllCounter++;
  1696. if (allow_keyboard_focus)
  1697. FocusIdxTabCounter++;
  1698. // Process keyboard input at this point: TAB, Shift-TAB switch focus
  1699. // We can always TAB out of a widget that doesn't allow tabbing in.
  1700. if (tab_stop && FocusIdxAllRequestNext == IM_INT_MAX && FocusIdxTabRequestNext == IM_INT_MAX && is_active && IsKeyPressedMap(ImGuiKey_Tab))
  1701. {
  1702. // Modulo on index will be applied at the end of frame once we've got the total counter of items.
  1703. FocusIdxTabRequestNext = FocusIdxTabCounter + (g.IO.KeyShift ? (allow_keyboard_focus ? -1 : 0) : +1);
  1704. }
  1705. if (FocusIdxAllCounter == FocusIdxAllRequestCurrent)
  1706. return true;
  1707. if (allow_keyboard_focus)
  1708. if (FocusIdxTabCounter == FocusIdxTabRequestCurrent)
  1709. return true;
  1710. return false;
  1711. }
  1712. void ImGuiWindow::FocusItemUnregister()
  1713. {
  1714. FocusIdxAllCounter--;
  1715. FocusIdxTabCounter--;
  1716. }
  1717. static inline void AddDrawListToRenderList(ImVector<ImDrawList*>& out_render_list, ImDrawList* draw_list)
  1718. {
  1719. if (!draw_list->CmdBuffer.empty() && !draw_list->VtxBuffer.empty())
  1720. {
  1721. if (draw_list->CmdBuffer.back().ElemCount == 0)
  1722. draw_list->CmdBuffer.pop_back();
  1723. out_render_list.push_back(draw_list);
  1724. GImGui->IO.MetricsRenderVertices += draw_list->VtxBuffer.Size;
  1725. GImGui->IO.MetricsRenderIndices += draw_list->IdxBuffer.Size;
  1726. }
  1727. }
  1728. static void AddWindowToRenderList(ImVector<ImDrawList*>& out_render_list, ImGuiWindow* window)
  1729. {
  1730. AddDrawListToRenderList(out_render_list, window->DrawList);
  1731. for (int i = 0; i < window->DC.ChildWindows.Size; i++)
  1732. {
  1733. ImGuiWindow* child = window->DC.ChildWindows[i];
  1734. if (!child->Active) // clipped children may have been marked not active
  1735. continue;
  1736. if ((child->Flags & ImGuiWindowFlags_Popup) && child->HiddenFrames > 0)
  1737. continue;
  1738. AddWindowToRenderList(out_render_list, child);
  1739. }
  1740. }
  1741. //-----------------------------------------------------------------------------
  1742. void* ImGui::MemAlloc(size_t sz)
  1743. {
  1744. GImGui->IO.MetricsAllocs++;
  1745. return GImGui->IO.MemAllocFn(sz);
  1746. }
  1747. void ImGui::MemFree(void* ptr)
  1748. {
  1749. if (ptr) GImGui->IO.MetricsAllocs--;
  1750. return GImGui->IO.MemFreeFn(ptr);
  1751. }
  1752. static ImGuiIniData* FindWindowSettings(const char* name)
  1753. {
  1754. ImGuiState& g = *GImGui;
  1755. ImGuiID id = ImHash(name, 0);
  1756. for (int i = 0; i != g.Settings.Size; i++)
  1757. {
  1758. ImGuiIniData* ini = &g.Settings[i];
  1759. if (ini->ID == id)
  1760. return ini;
  1761. }
  1762. return NULL;
  1763. }
  1764. static ImGuiIniData* AddWindowSettings(const char* name)
  1765. {
  1766. GImGui->Settings.resize(GImGui->Settings.Size + 1);
  1767. ImGuiIniData* ini = &GImGui->Settings.back();
  1768. ini->Name = ImStrdup(name);
  1769. ini->ID = ImHash(name, 0);
  1770. ini->Collapsed = false;
  1771. ini->Pos = ImVec2(FLT_MAX,FLT_MAX);
  1772. ini->Size = ImVec2(0,0);
  1773. return ini;
  1774. }
  1775. // Zero-tolerance, poor-man .ini parsing
  1776. // FIXME: Write something less rubbish
  1777. static void LoadSettings()
  1778. {
  1779. ImGuiState& g = *GImGui;
  1780. const char* filename = g.IO.IniFilename;
  1781. if (!filename)
  1782. return;
  1783. char* file_data;
  1784. int file_size;
  1785. if (!ImLoadFileToMemory(filename, "rb", (void**)&file_data, &file_size, 1))
  1786. return;
  1787. ImGuiIniData* settings = NULL;
  1788. const char* buf_end = file_data + file_size;
  1789. for (const char* line_start = file_data; line_start < buf_end; )
  1790. {
  1791. const char* line_end = line_start;
  1792. while (line_end < buf_end && *line_end != '\n' && *line_end != '\r')
  1793. line_end++;
  1794. if (line_start[0] == '[' && line_end > line_start && line_end[-1] == ']')
  1795. {
  1796. char name[64];
  1797. ImFormatString(name, IM_ARRAYSIZE(name), "%.*s", line_end-line_start-2, line_start+1);
  1798. settings = FindWindowSettings(name);
  1799. if (!settings)
  1800. settings = AddWindowSettings(name);
  1801. }
  1802. else if (settings)
  1803. {
  1804. float x, y;
  1805. int i;
  1806. if (sscanf(line_start, "Pos=%f,%f", &x, &y) == 2)
  1807. settings->Pos = ImVec2(x, y);
  1808. else if (sscanf(line_start, "Size=%f,%f", &x, &y) == 2)
  1809. settings->Size = ImMax(ImVec2(x, y), g.Style.WindowMinSize);
  1810. else if (sscanf(line_start, "Collapsed=%d", &i) == 1)
  1811. settings->Collapsed = (i != 0);
  1812. }
  1813. line_start = line_end+1;
  1814. }
  1815. ImGui::MemFree(file_data);
  1816. }
  1817. static void SaveSettings()
  1818. {
  1819. ImGuiState& g = *GImGui;
  1820. const char* filename = g.IO.IniFilename;
  1821. if (!filename)
  1822. return;
  1823. // Gather data from windows that were active during this session
  1824. for (int i = 0; i != g.Windows.Size; i++)
  1825. {
  1826. ImGuiWindow* window = g.Windows[i];
  1827. if (window->Flags & ImGuiWindowFlags_NoSavedSettings)
  1828. continue;
  1829. ImGuiIniData* settings = FindWindowSettings(window->Name);
  1830. settings->Pos = window->Pos;
  1831. settings->Size = window->SizeFull;
  1832. settings->Collapsed = window->Collapsed;
  1833. }
  1834. // Write .ini file
  1835. // If a window wasn't opened in this session we preserve its settings
  1836. FILE* f = fopen(filename, "wt");
  1837. if (!f)
  1838. return;
  1839. for (int i = 0; i != g.Settings.Size; i++)
  1840. {
  1841. const ImGuiIniData* settings = &g.Settings[i];
  1842. if (settings->Pos.x == FLT_MAX)
  1843. continue;
  1844. const char* name = settings->Name;
  1845. if (const char* p = strstr(name, "###")) // Skip to the "###" marker if any. We don't skip past to match the behavior of GetID()
  1846. name = p;
  1847. fprintf(f, "[%s]\n", name);
  1848. fprintf(f, "Pos=%d,%d\n", (int)settings->Pos.x, (int)settings->Pos.y);
  1849. fprintf(f, "Size=%d,%d\n", (int)settings->Size.x, (int)settings->Size.y);
  1850. fprintf(f, "Collapsed=%d\n", settings->Collapsed);
  1851. fprintf(f, "\n");
  1852. }
  1853. fclose(f);
  1854. }
  1855. static void MarkSettingsDirty()
  1856. {
  1857. ImGuiState& g = *GImGui;
  1858. if (g.SettingsDirtyTimer <= 0.0f)
  1859. g.SettingsDirtyTimer = g.IO.IniSavingRate;
  1860. }
  1861. const char* ImGui::GetVersion()
  1862. {
  1863. return IMGUI_VERSION;
  1864. }
  1865. // Internal state access - if you want to share ImGui state between modules (e.g. DLL) or allocate it yourself
  1866. // Note that we still point to some static data and members (such as GFontAtlas), so the state instance you end up using will point to the static data within its module
  1867. void* ImGui::GetInternalState()
  1868. {
  1869. return GImGui;
  1870. }
  1871. size_t ImGui::GetInternalStateSize()
  1872. {
  1873. return sizeof(ImGuiState);
  1874. }
  1875. void ImGui::SetInternalState(void* state, bool construct)
  1876. {
  1877. if (construct)
  1878. new (state) ImGuiState();
  1879. GImGui = (ImGuiState*)state;
  1880. }
  1881. ImGuiIO& ImGui::GetIO()
  1882. {
  1883. return GImGui->IO;
  1884. }
  1885. ImGuiStyle& ImGui::GetStyle()
  1886. {
  1887. return GImGui->Style;
  1888. }
  1889. void ImGui::NewFrame()
  1890. {
  1891. ImGuiState& g = *GImGui;
  1892. // Check user data
  1893. IM_ASSERT(g.IO.DeltaTime >= 0.0f);
  1894. IM_ASSERT(g.IO.DisplaySize.x >= 0.0f && g.IO.DisplaySize.y >= 0.0f);
  1895. IM_ASSERT(g.IO.RenderDrawListsFn != NULL); // Must be implemented
  1896. IM_ASSERT(g.IO.Fonts->Fonts.Size > 0); // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ?
  1897. IM_ASSERT(g.IO.Fonts->Fonts[0]->IsLoaded()); // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ?
  1898. if (!g.Initialized)
  1899. {
  1900. // Initialize on first frame
  1901. g.LogClipboard = (ImGuiTextBuffer*)ImGui::MemAlloc(sizeof(ImGuiTextBuffer));
  1902. new(g.LogClipboard) ImGuiTextBuffer();
  1903. IM_ASSERT(g.Settings.empty());
  1904. LoadSettings();
  1905. g.Initialized = true;
  1906. }
  1907. SetFont(g.IO.Fonts->Fonts[0]);
  1908. g.Time += g.IO.DeltaTime;
  1909. g.FrameCount += 1;
  1910. g.Tooltip[0] = '\0';
  1911. g.OverlayDrawList.Clear();
  1912. g.OverlayDrawList.PushTextureID(g.IO.Fonts->TexID);
  1913. g.OverlayDrawList.PushClipRectFullScreen();
  1914. g.OverlayDrawList.AddDrawCmd();
  1915. // Update inputs state
  1916. if (g.IO.MousePos.x < 0 && g.IO.MousePos.y < 0)
  1917. g.IO.MousePos = ImVec2(-9999.0f, -9999.0f);
  1918. if ((g.IO.MousePos.x < 0 && g.IO.MousePos.y < 0) || (g.IO.MousePosPrev.x < 0 && g.IO.MousePosPrev.y < 0)) // if mouse just appeared or disappeared (negative coordinate) we cancel out movement in MouseDelta
  1919. g.IO.MouseDelta = ImVec2(0.0f, 0.0f);
  1920. else
  1921. g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev;
  1922. g.IO.MousePosPrev = g.IO.MousePos;
  1923. for (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++)
  1924. {
  1925. g.IO.MouseDownDurationPrev[i] = g.IO.MouseDownDuration[i];
  1926. g.IO.MouseDownDuration[i] = g.IO.MouseDown[i] ? (g.IO.MouseDownDuration[i] < 0.0f ? 0.0f : g.IO.MouseDownDuration[i] + g.IO.DeltaTime) : -1.0f;
  1927. g.IO.MouseClicked[i] = g.IO.MouseDownDuration[i] == 0.0f;
  1928. g.IO.MouseReleased[i] = g.IO.MouseDownDurationPrev[i] >= 0.0f && !g.IO.MouseDown[i];
  1929. g.IO.MouseDoubleClicked[i] = false;
  1930. if (g.IO.MouseClicked[i])
  1931. {
  1932. if (g.Time - g.IO.MouseClickedTime[i] < g.IO.MouseDoubleClickTime)
  1933. {
  1934. if (ImLengthSqr(g.IO.MousePos - g.IO.MouseClickedPos[i]) < g.IO.MouseDoubleClickMaxDist * g.IO.MouseDoubleClickMaxDist)
  1935. g.IO.MouseDoubleClicked[i] = true;
  1936. g.IO.MouseClickedTime[i] = -FLT_MAX; // so the third click isn't turned into a double-click
  1937. }
  1938. else
  1939. {
  1940. g.IO.MouseClickedTime[i] = g.Time;
  1941. }
  1942. g.IO.MouseClickedPos[i] = g.IO.MousePos;
  1943. g.IO.MouseDragMaxDistanceSqr[i] = 0.0f;
  1944. }
  1945. else if (g.IO.MouseDown[i])
  1946. {
  1947. g.IO.MouseDragMaxDistanceSqr[i] = ImMax(g.IO.MouseDragMaxDistanceSqr[i], ImLengthSqr(g.IO.MousePos - g.IO.MouseClickedPos[i]));
  1948. }
  1949. }
  1950. memcpy(g.IO.KeysDownDurationPrev, g.IO.KeysDownDuration, sizeof(g.IO.KeysDownDuration));
  1951. for (int i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++)
  1952. g.IO.KeysDownDuration[i] = g.IO.KeysDown[i] ? (g.IO.KeysDownDuration[i] < 0.0f ? 0.0f : g.IO.KeysDownDuration[i] + g.IO.DeltaTime) : -1.0f;
  1953. // Calculate frame-rate for the user, as a purely luxurious feature
  1954. g.FramerateSecPerFrameAccum += g.IO.DeltaTime - g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx];
  1955. g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx] = g.IO.DeltaTime;
  1956. g.FramerateSecPerFrameIdx = (g.FramerateSecPerFrameIdx + 1) % IM_ARRAYSIZE(g.FramerateSecPerFrame);
  1957. g.IO.Framerate = 1.0f / (g.FramerateSecPerFrameAccum / (float)IM_ARRAYSIZE(g.FramerateSecPerFrame));
  1958. // Clear reference to active widget if the widget isn't alive anymore
  1959. g.HoveredIdPreviousFrame = g.HoveredId;
  1960. g.HoveredId = 0;
  1961. if (!g.ActiveIdIsAlive && g.ActiveIdPreviousFrame == g.ActiveId && g.ActiveId != 0)
  1962. SetActiveId(0);
  1963. g.ActiveIdPreviousFrame = g.ActiveId;
  1964. g.ActiveIdIsAlive = false;
  1965. g.ActiveIdIsJustActivated = false;
  1966. if (!g.ActiveId)
  1967. g.MovedWindow = NULL;
  1968. // Delay saving settings so we don't spam disk too much
  1969. if (g.SettingsDirtyTimer > 0.0f)
  1970. {
  1971. g.SettingsDirtyTimer -= g.IO.DeltaTime;
  1972. if (g.SettingsDirtyTimer <= 0.0f)
  1973. SaveSettings();
  1974. }
  1975. // Find the window we are hovering. Child windows can extend beyond the limit of their parent so we need to derive HoveredRootWindow from HoveredWindow
  1976. g.HoveredWindow = FindHoveredWindow(g.IO.MousePos, false);
  1977. if (g.HoveredWindow && (g.HoveredWindow->Flags & ImGuiWindowFlags_ChildWindow))
  1978. g.HoveredRootWindow = g.HoveredWindow->RootWindow;
  1979. else
  1980. g.HoveredRootWindow = FindHoveredWindow(g.IO.MousePos, true);
  1981. if (ImGuiWindow* modal_window = GetFrontMostModalRootWindow())
  1982. {
  1983. g.ModalWindowDarkeningRatio = ImMin(g.ModalWindowDarkeningRatio + g.IO.DeltaTime * 6.0f, 1.0f);
  1984. if (g.HoveredRootWindow != modal_window)
  1985. g.HoveredRootWindow = g.HoveredWindow = NULL;
  1986. }
  1987. else
  1988. {
  1989. g.ModalWindowDarkeningRatio = 0.0f;
  1990. }
  1991. // Are we using inputs? Tell user so they can capture/discard the inputs away from the rest of their application.
  1992. // When clicking outside of a window we assume the click is owned by the application and won't request capture.
  1993. int mouse_earliest_button_down = -1;
  1994. bool mouse_any_down = false;
  1995. for (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++)
  1996. {
  1997. if (g.IO.MouseClicked[i])
  1998. g.IO.MouseDownOwned[i] = (g.HoveredWindow != NULL) || (!g.OpenedPopupStack.empty());
  1999. mouse_any_down |= g.IO.MouseDown[i];
  2000. if (g.IO.MouseDown[i])
  2001. if (mouse_earliest_button_down == -1 || g.IO.MouseClickedTime[mouse_earliest_button_down] > g.IO.MouseClickedTime[i])
  2002. mouse_earliest_button_down = i;
  2003. }
  2004. bool mouse_owned_by_application = mouse_earliest_button_down != -1 && !g.IO.MouseDownOwned[mouse_earliest_button_down];
  2005. g.IO.WantCaptureMouse = (!mouse_owned_by_application && g.HoveredWindow != NULL) || (!mouse_owned_by_application && mouse_any_down) || (g.ActiveId != 0) || (!g.OpenedPopupStack.empty()) || (g.CaptureMouseNextFrame);
  2006. g.IO.WantCaptureKeyboard = (g.ActiveId != 0) || (g.CaptureKeyboardNextFrame);
  2007. g.MouseCursor = ImGuiMouseCursor_Arrow;
  2008. g.CaptureMouseNextFrame = g.CaptureKeyboardNextFrame = false;
  2009. // If mouse was first clicked outside of ImGui bounds we also cancel out hovering.
  2010. if (mouse_owned_by_application)
  2011. g.HoveredWindow = g.HoveredRootWindow = NULL;
  2012. // Scale & Scrolling
  2013. if (g.HoveredWindow && g.IO.MouseWheel != 0.0f && !g.HoveredWindow->Collapsed)
  2014. {
  2015. ImGuiWindow* window = g.HoveredWindow;
  2016. if (g.IO.KeyCtrl)
  2017. {
  2018. if (g.IO.FontAllowUserScaling)
  2019. {
  2020. // Zoom / Scale window
  2021. float new_font_scale = ImClamp(window->FontWindowScale + g.IO.MouseWheel * 0.10f, 0.50f, 2.50f);
  2022. float scale = new_font_scale / window->FontWindowScale;
  2023. window->FontWindowScale = new_font_scale;
  2024. const ImVec2 offset = window->Size * (1.0f - scale) * (g.IO.MousePos - window->Pos) / window->Size;
  2025. window->Pos += offset;
  2026. window->PosFloat += offset;
  2027. window->Size *= scale;
  2028. window->SizeFull *= scale;
  2029. }
  2030. }
  2031. else
  2032. {
  2033. // Scroll
  2034. if (!(window->Flags & ImGuiWindowFlags_NoScrollWithMouse))
  2035. {
  2036. const int scroll_lines = (window->Flags & ImGuiWindowFlags_ComboBox) ? 3 : 5;
  2037. SetWindowScrollY(window, window->ScrollY - g.IO.MouseWheel * window->CalcFontSize() * scroll_lines);
  2038. }
  2039. }
  2040. }
  2041. // Pressing TAB activate widget focus
  2042. // NB: Don't discard FocusedWindow if it isn't active, so that a window that go on/off programatically won't lose its keyboard focus.
  2043. if (g.ActiveId == 0 && g.FocusedWindow != NULL && g.FocusedWindow->Active && IsKeyPressedMap(ImGuiKey_Tab, false))
  2044. g.FocusedWindow->FocusIdxTabRequestNext = 0;
  2045. // Mark all windows as not visible
  2046. for (int i = 0; i != g.Windows.Size; i++)
  2047. {
  2048. ImGuiWindow* window = g.Windows[i];
  2049. window->WasActive = window->Active;
  2050. window->Active = false;
  2051. window->Accessed = false;
  2052. }
  2053. // No window should be open at the beginning of the frame.
  2054. // But in order to allow the user to call NewFrame() multiple times without calling Render(), we are doing an explicit clear.
  2055. g.CurrentWindowStack.resize(0);
  2056. g.CurrentPopupStack.resize(0);
  2057. CloseInactivePopups();
  2058. // Create implicit window - we will only render it if the user has added something to it.
  2059. ImGui::SetNextWindowSize(ImVec2(400,400), ImGuiSetCond_FirstUseEver);
  2060. ImGui::Begin("Debug");
  2061. }
  2062. // NB: behavior of ImGui after Shutdown() is not tested/guaranteed at the moment. This function is merely here to free heap allocations.
  2063. void ImGui::Shutdown()
  2064. {
  2065. ImGuiState& g = *GImGui;
  2066. if (!g.Initialized)
  2067. return;
  2068. SaveSettings();
  2069. for (int i = 0; i < g.Windows.Size; i++)
  2070. {
  2071. g.Windows[i]->~ImGuiWindow();
  2072. ImGui::MemFree(g.Windows[i]);
  2073. }
  2074. g.Windows.clear();
  2075. g.WindowsSortBuffer.clear();
  2076. g.CurrentWindowStack.clear();
  2077. g.FocusedWindow = NULL;
  2078. g.HoveredWindow = NULL;
  2079. g.HoveredRootWindow = NULL;
  2080. for (int i = 0; i < g.Settings.Size; i++)
  2081. ImGui::MemFree(g.Settings[i].Name);
  2082. g.Settings.clear();
  2083. g.ColorModifiers.clear();
  2084. g.StyleModifiers.clear();
  2085. g.FontStack.clear();
  2086. g.OpenedPopupStack.clear();
  2087. g.CurrentPopupStack.clear();
  2088. for (int i = 0; i < IM_ARRAYSIZE(g.RenderDrawLists); i++)
  2089. g.RenderDrawLists[i].clear();
  2090. g.OverlayDrawList.ClearFreeMemory();
  2091. g.ColorEditModeStorage.Clear();
  2092. if (g.PrivateClipboard)
  2093. {
  2094. ImGui::MemFree(g.PrivateClipboard);
  2095. g.PrivateClipboard = NULL;
  2096. }
  2097. g.InputTextState.Text.clear();
  2098. g.InputTextState.InitialText.clear();
  2099. g.InputTextState.TempTextBuffer.clear();
  2100. if (g.LogFile && g.LogFile != stdout)
  2101. {
  2102. fclose(g.LogFile);
  2103. g.LogFile = NULL;
  2104. }
  2105. if (g.LogClipboard)
  2106. {
  2107. g.LogClipboard->~ImGuiTextBuffer();
  2108. ImGui::MemFree(g.LogClipboard);
  2109. }
  2110. if (g.IO.Fonts) // Testing for NULL to allow user to NULLify in case of running Shutdown() on multiple contexts. Bit hacky.
  2111. g.IO.Fonts->Clear();
  2112. g.Initialized = false;
  2113. }
  2114. // FIXME: Add a more explicit sort order in the window structure.
  2115. static int ChildWindowComparer(const void* lhs, const void* rhs)
  2116. {
  2117. const ImGuiWindow* a = *(const ImGuiWindow**)lhs;
  2118. const ImGuiWindow* b = *(const ImGuiWindow**)rhs;
  2119. if (int d = (a->Flags & ImGuiWindowFlags_Popup) - (b->Flags & ImGuiWindowFlags_Popup))
  2120. return d;
  2121. if (int d = (a->Flags & ImGuiWindowFlags_Tooltip) - (b->Flags & ImGuiWindowFlags_Tooltip))
  2122. return d;
  2123. if (int d = (a->Flags & ImGuiWindowFlags_ComboBox) - (b->Flags & ImGuiWindowFlags_ComboBox))
  2124. return d;
  2125. return 0;
  2126. }
  2127. static void AddWindowToSortedBuffer(ImVector<ImGuiWindow*>& out_sorted_windows, ImGuiWindow* window)
  2128. {
  2129. out_sorted_windows.push_back(window);
  2130. if (window->Active)
  2131. {
  2132. int count = window->DC.ChildWindows.Size;
  2133. if (count > 1)
  2134. qsort(window->DC.ChildWindows.begin(), (size_t)count, sizeof(ImGuiWindow*), ChildWindowComparer);
  2135. for (int i = 0; i < count; i++)
  2136. {
  2137. ImGuiWindow* child = window->DC.ChildWindows[i];
  2138. if (child->Active)
  2139. AddWindowToSortedBuffer(out_sorted_windows, child);
  2140. }
  2141. }
  2142. }
  2143. static void PushClipRect(const ImVec4& clip_rect, bool clipped = true)
  2144. {
  2145. ImGuiWindow* window = GetCurrentWindow();
  2146. ImVec4 cr = clip_rect;
  2147. if (clipped)
  2148. {
  2149. // Clip with existing clip rect
  2150. const ImVec4 cur_cr = window->ClipRect;
  2151. cr = ImVec4(ImMax(cr.x, cur_cr.x), ImMax(cr.y, cur_cr.y), ImMin(cr.z, cur_cr.z), ImMin(cr.w, cur_cr.w));
  2152. }
  2153. cr.z = ImMax(cr.x, cr.z);
  2154. cr.w = ImMax(cr.y, cr.w);
  2155. IM_ASSERT(cr.x <= cr.z && cr.y <= cr.w);
  2156. window->ClipRect = cr;
  2157. window->DrawList->PushClipRect(cr);
  2158. }
  2159. static void PopClipRect()
  2160. {
  2161. ImGuiWindow* window = GetCurrentWindow();
  2162. window->DrawList->PopClipRect();
  2163. window->ClipRect = window->DrawList->_ClipRectStack.back();
  2164. }
  2165. void ImGui::Render()
  2166. {
  2167. ImGuiState& g = *GImGui;
  2168. IM_ASSERT(g.Initialized); // Forgot to call ImGui::NewFrame()
  2169. const bool first_render_of_the_frame = (g.FrameCountRendered != g.FrameCount);
  2170. g.FrameCountRendered = g.FrameCount;
  2171. if (first_render_of_the_frame)
  2172. {
  2173. // Hide implicit "Debug" window if it hasn't been used
  2174. IM_ASSERT(g.CurrentWindowStack.Size == 1); // Mismatched Begin/End
  2175. if (g.CurrentWindow && !g.CurrentWindow->Accessed)
  2176. g.CurrentWindow->Active = false;
  2177. ImGui::End();
  2178. // Click to focus window and start moving (after we're done with all our widgets)
  2179. if (!g.ActiveId)
  2180. g.MovedWindow = NULL;
  2181. if (g.ActiveId == 0 && g.HoveredId == 0 && g.IO.MouseClicked[0])
  2182. {
  2183. if (!(g.FocusedWindow && !g.FocusedWindow->WasActive && g.FocusedWindow->Active)) // Unless we just made a popup appear
  2184. {
  2185. if (g.HoveredRootWindow != NULL)
  2186. {
  2187. g.MovedWindow = g.HoveredWindow;
  2188. SetActiveId(g.HoveredRootWindow->MoveID, g.HoveredRootWindow);
  2189. }
  2190. else if (g.FocusedWindow != NULL && GetFrontMostModalRootWindow() == NULL)
  2191. {
  2192. // Clicking on void disable focus
  2193. FocusWindow(NULL);
  2194. }
  2195. }
  2196. }
  2197. // Sort the window list so that all child windows are after their parent
  2198. // We cannot do that on FocusWindow() because childs may not exist yet
  2199. g.WindowsSortBuffer.resize(0);
  2200. g.WindowsSortBuffer.reserve(g.Windows.Size);
  2201. for (int i = 0; i != g.Windows.Size; i++)
  2202. {
  2203. ImGuiWindow* window = g.Windows[i];
  2204. if (window->Flags & ImGuiWindowFlags_ChildWindow) // if a child is active its parent will add it
  2205. if (window->Active)
  2206. continue;
  2207. AddWindowToSortedBuffer(g.WindowsSortBuffer, window);
  2208. }
  2209. IM_ASSERT(g.Windows.Size == g.WindowsSortBuffer.Size); // we done something wrong
  2210. g.Windows.swap(g.WindowsSortBuffer);
  2211. // Clear Input data for next frame
  2212. g.IO.MouseWheel = 0.0f;
  2213. memset(g.IO.InputCharacters, 0, sizeof(g.IO.InputCharacters));
  2214. }
  2215. // Skip render altogether if alpha is 0.0
  2216. // Note that vertex buffers have been created and are wasted, so it is best practice that you don't create windows in the first place, or respond to Begin() returning false.
  2217. if (g.Style.Alpha > 0.0f)
  2218. {
  2219. // Render tooltip
  2220. if (g.Tooltip[0])
  2221. {
  2222. ImGui::BeginTooltip();
  2223. ImGui::TextUnformatted(g.Tooltip);
  2224. ImGui::EndTooltip();
  2225. }
  2226. // Gather windows to render
  2227. g.IO.MetricsRenderVertices = g.IO.MetricsRenderIndices = g.IO.MetricsActiveWindows = 0;
  2228. for (int i = 0; i < IM_ARRAYSIZE(g.RenderDrawLists); i++)
  2229. g.RenderDrawLists[i].resize(0);
  2230. for (int i = 0; i != g.Windows.Size; i++)
  2231. {
  2232. ImGuiWindow* window = g.Windows[i];
  2233. if (window->Active && window->HiddenFrames <= 0 && (window->Flags & (ImGuiWindowFlags_ChildWindow)) == 0)
  2234. {
  2235. // FIXME: Generalize this with a proper layering system so e.g. user can draw in specific layers, below text, ..
  2236. g.IO.MetricsActiveWindows++;
  2237. if (window->Flags & ImGuiWindowFlags_Popup)
  2238. AddWindowToRenderList(g.RenderDrawLists[1], window);
  2239. else if (window->Flags & ImGuiWindowFlags_Tooltip)
  2240. AddWindowToRenderList(g.RenderDrawLists[2], window);
  2241. else
  2242. AddWindowToRenderList(g.RenderDrawLists[0], window);
  2243. }
  2244. }
  2245. // Flatten layers
  2246. int n = g.RenderDrawLists[0].Size;
  2247. int flattened_size = n;
  2248. for (int i = 1; i < IM_ARRAYSIZE(g.RenderDrawLists); i++)
  2249. flattened_size += g.RenderDrawLists[i].Size;
  2250. g.RenderDrawLists[0].resize(flattened_size);
  2251. for (int i = 1; i < IM_ARRAYSIZE(g.RenderDrawLists); i++)
  2252. {
  2253. ImVector<ImDrawList*>& layer = g.RenderDrawLists[i];
  2254. if (!layer.empty())
  2255. {
  2256. memcpy(&g.RenderDrawLists[0][n], &layer[0], layer.Size * sizeof(ImDrawList*));
  2257. n += layer.Size;
  2258. }
  2259. }
  2260. if (g.IO.MouseDrawCursor)
  2261. {
  2262. const ImGuiMouseCursorData& cursor_data = g.MouseCursorData[g.MouseCursor];
  2263. const ImVec2 pos = g.IO.MousePos - cursor_data.Offset;
  2264. const ImVec2 size = cursor_data.Size;
  2265. const ImTextureID tex_id = g.IO.Fonts->TexID;
  2266. g.OverlayDrawList.PushTextureID(tex_id);
  2267. g.OverlayDrawList.AddImage(tex_id, pos+ImVec2(1,0), pos+ImVec2(1,0) + size, cursor_data.TexUvMin[1], cursor_data.TexUvMax[1], 0x30000000); // Shadow
  2268. g.OverlayDrawList.AddImage(tex_id, pos+ImVec2(2,0), pos+ImVec2(2,0) + size, cursor_data.TexUvMin[1], cursor_data.TexUvMax[1], 0x30000000); // Shadow
  2269. g.OverlayDrawList.AddImage(tex_id, pos, pos + size, cursor_data.TexUvMin[1], cursor_data.TexUvMax[1], 0xFF000000); // Black border
  2270. g.OverlayDrawList.AddImage(tex_id, pos, pos + size, cursor_data.TexUvMin[0], cursor_data.TexUvMax[0], 0xFFFFFFFF); // White fill
  2271. g.OverlayDrawList.PopTextureID();
  2272. }
  2273. if (!g.OverlayDrawList.VtxBuffer.empty())
  2274. AddDrawListToRenderList(g.RenderDrawLists[0], &g.OverlayDrawList);
  2275. // Render
  2276. if (!g.RenderDrawLists[0].empty())
  2277. {
  2278. ImDrawData data;
  2279. data.CmdLists = &g.RenderDrawLists[0][0];
  2280. data.CmdListsCount = g.RenderDrawLists[0].Size;
  2281. data.TotalVtxCount = g.IO.MetricsRenderVertices;
  2282. data.TotalIdxCount = g.IO.MetricsRenderIndices;
  2283. g.IO.RenderDrawListsFn(&data);
  2284. }
  2285. }
  2286. }
  2287. // Find the optional ## from which we stop displaying text.
  2288. static const char* FindTextDisplayEnd(const char* text, const char* text_end = NULL)
  2289. {
  2290. const char* text_display_end = text;
  2291. if (!text_end)
  2292. text_end = (const char*)-1;
  2293. ImGuiState& g = *GImGui;
  2294. if (g.DisableHideTextAfterDoubleHash > 0)
  2295. {
  2296. while (text_display_end < text_end && *text_display_end != '\0')
  2297. text_display_end++;
  2298. }
  2299. else
  2300. {
  2301. while (text_display_end < text_end && *text_display_end != '\0' && (text_display_end[0] != '#' || text_display_end[1] != '#'))
  2302. text_display_end++;
  2303. }
  2304. return text_display_end;
  2305. }
  2306. // Pass text data straight to log (without being displayed)
  2307. void ImGui::LogText(const char* fmt, ...)
  2308. {
  2309. ImGuiState& g = *GImGui;
  2310. if (!g.LogEnabled)
  2311. return;
  2312. va_list args;
  2313. va_start(args, fmt);
  2314. if (g.LogFile)
  2315. {
  2316. vfprintf(g.LogFile, fmt, args);
  2317. }
  2318. else
  2319. {
  2320. g.LogClipboard->appendv(fmt, args);
  2321. }
  2322. va_end(args);
  2323. }
  2324. // Internal version that takes a position to decide on newline placement and pad items according to their depth.
  2325. // We split text into individual lines to add current tree level padding
  2326. static void LogText(const ImVec2& ref_pos, const char* text, const char* text_end)
  2327. {
  2328. ImGuiState& g = *GImGui;
  2329. ImGuiWindow* window = GetCurrentWindow();
  2330. if (!text_end)
  2331. text_end = FindTextDisplayEnd(text, text_end);
  2332. const bool log_new_line = ref_pos.y > window->DC.LogLinePosY+1;
  2333. window->DC.LogLinePosY = ref_pos.y;
  2334. const char* text_remaining = text;
  2335. if (g.LogStartDepth > window->DC.TreeDepth) // Re-adjust padding if we have popped out of our starting depth
  2336. g.LogStartDepth = window->DC.TreeDepth;
  2337. const int tree_depth = (window->DC.TreeDepth - g.LogStartDepth);
  2338. for (;;)
  2339. {
  2340. // Split the string. Each new line (after a '\n') is followed by spacing corresponding to the current depth of our log entry.
  2341. const char* line_end = text_remaining;
  2342. while (line_end < text_end)
  2343. if (*line_end == '\n')
  2344. break;
  2345. else
  2346. line_end++;
  2347. if (line_end >= text_end)
  2348. line_end = NULL;
  2349. const bool is_first_line = (text == text_remaining);
  2350. bool is_last_line = false;
  2351. if (line_end == NULL)
  2352. {
  2353. is_last_line = true;
  2354. line_end = text_end;
  2355. }
  2356. if (line_end != NULL && !(is_last_line && (line_end - text_remaining)==0))
  2357. {
  2358. const int char_count = (int)(line_end - text_remaining);
  2359. if (log_new_line || !is_first_line)
  2360. ImGui::LogText(IM_NEWLINE "%*s%.*s", tree_depth*4, "", char_count, text_remaining);
  2361. else
  2362. ImGui::LogText(" %.*s", char_count, text_remaining);
  2363. }
  2364. if (is_last_line)
  2365. break;
  2366. text_remaining = line_end + 1;
  2367. }
  2368. }
  2369. static float CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x)
  2370. {
  2371. if (wrap_pos_x < 0.0f)
  2372. return 0.0f;
  2373. ImGuiWindow* window = GetCurrentWindow();
  2374. if (wrap_pos_x == 0.0f)
  2375. wrap_pos_x = ImGui::GetContentRegionMax().x;
  2376. if (wrap_pos_x > 0.0f)
  2377. wrap_pos_x += window->Pos.x; // wrap_pos_x is provided is window local space
  2378. const float wrap_width = wrap_pos_x > 0.0f ? ImMax(wrap_pos_x - pos.x, 0.00001f) : 0.0f;
  2379. return wrap_width;
  2380. }
  2381. // Internal ImGui functions to render text
  2382. // RenderText***() functions calls ImDrawList::AddText() calls ImBitmapFont::RenderText()
  2383. static void RenderText(ImVec2 pos, const char* text, const char* text_end, bool hide_text_after_hash)
  2384. {
  2385. ImGuiState& g = *GImGui;
  2386. ImGuiWindow* window = GetCurrentWindow();
  2387. // Hide anything after a '##' string
  2388. const char* text_display_end;
  2389. if (hide_text_after_hash)
  2390. {
  2391. text_display_end = FindTextDisplayEnd(text, text_end);
  2392. }
  2393. else
  2394. {
  2395. if (!text_end)
  2396. text_end = text + strlen(text); // FIXME-OPT
  2397. text_display_end = text_end;
  2398. }
  2399. const int text_len = (int)(text_display_end - text);
  2400. if (text_len > 0)
  2401. {
  2402. // Render
  2403. window->DrawList->AddText(g.Font, g.FontSize, pos, window->Color(ImGuiCol_Text), text, text_display_end);
  2404. // Log as text
  2405. if (g.LogEnabled)
  2406. LogText(pos, text, text_display_end);
  2407. }
  2408. }
  2409. static void RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end, float wrap_width)
  2410. {
  2411. ImGuiState& g = *GImGui;
  2412. ImGuiWindow* window = GetCurrentWindow();
  2413. if (!text_end)
  2414. text_end = text + strlen(text); // FIXME-OPT
  2415. const int text_len = (int)(text_end - text);
  2416. if (text_len > 0)
  2417. {
  2418. window->DrawList->AddText(g.Font, g.FontSize, pos, window->Color(ImGuiCol_Text), text, text_end, wrap_width);
  2419. if (g.LogEnabled)
  2420. LogText(pos, text, text_end);
  2421. }
  2422. }
  2423. static void RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, ImGuiAlign align, const ImVec2* clip_min, const ImVec2* clip_max)
  2424. {
  2425. // Hide anything after a '##' string
  2426. const char* text_display_end = FindTextDisplayEnd(text, text_end);
  2427. const int text_len = (int)(text_display_end - text);
  2428. if (text_len == 0)
  2429. return;
  2430. ImGuiState& g = *GImGui;
  2431. ImGuiWindow* window = GetCurrentWindow();
  2432. // Perform CPU side clipping for single clipped element to avoid using scissor state
  2433. ImVec2 pos = pos_min;
  2434. const ImVec2 text_size = text_size_if_known ? *text_size_if_known : ImGui::CalcTextSize(text, text_display_end, false, 0.0f);
  2435. if (!clip_max) clip_max = &pos_max;
  2436. bool need_clipping = (pos.x + text_size.x >= clip_max->x) || (pos.y + text_size.y >= clip_max->y);
  2437. if (!clip_min) clip_min = &pos_min; else need_clipping |= (pos.x < clip_min->x) || (pos.y < clip_min->y);
  2438. // Align
  2439. if (align & ImGuiAlign_Center) pos.x = ImMax(pos.x, (pos.x + pos_max.x - text_size.x) * 0.5f);
  2440. else if (align & ImGuiAlign_Right) pos.x = ImMax(pos.x, pos_max.x - text_size.x);
  2441. if (align & ImGuiAlign_VCenter) pos.y = ImMax(pos.y, (pos.y + pos_max.y - text_size.y) * 0.5f);
  2442. // Render
  2443. if (need_clipping)
  2444. {
  2445. ImVec4 fine_clip_rect(clip_min->x, clip_min->y, clip_max->x, clip_max->y);
  2446. window->DrawList->AddText(g.Font, g.FontSize, pos, window->Color(ImGuiCol_Text), text, text_display_end, 0.0f, &fine_clip_rect);
  2447. }
  2448. else
  2449. {
  2450. window->DrawList->AddText(g.Font, g.FontSize, pos, window->Color(ImGuiCol_Text), text, text_display_end, 0.0f, NULL);
  2451. }
  2452. if (g.LogEnabled)
  2453. LogText(pos, text, text_display_end);
  2454. }
  2455. // Render a rectangle shaped with optional rounding and borders
  2456. static void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border, float rounding)
  2457. {
  2458. ImGuiWindow* window = GetCurrentWindow();
  2459. window->DrawList->AddRectFilled(p_min, p_max, fill_col, rounding);
  2460. if (border && (window->Flags & ImGuiWindowFlags_ShowBorders))
  2461. {
  2462. window->DrawList->AddRect(p_min+ImVec2(1,1), p_max, window->Color(ImGuiCol_BorderShadow), rounding);
  2463. window->DrawList->AddRect(p_min, p_max-ImVec2(1,1), window->Color(ImGuiCol_Border), rounding);
  2464. }
  2465. }
  2466. // Render a triangle to denote expanded/collapsed state
  2467. static void RenderCollapseTriangle(ImVec2 p_min, bool opened, float scale, bool shadow)
  2468. {
  2469. ImGuiState& g = *GImGui;
  2470. ImGuiWindow* window = GetCurrentWindow();
  2471. const float h = g.FontSize * 1.00f;
  2472. const float r = h * 0.40f * scale;
  2473. ImVec2 center = p_min + ImVec2(h*0.50f, h*0.50f*scale);
  2474. ImVec2 a, b, c;
  2475. if (opened)
  2476. {
  2477. center.y -= r*0.25f;
  2478. a = center + ImVec2(0,1)*r;
  2479. b = center + ImVec2(-0.866f,-0.5f)*r;
  2480. c = center + ImVec2(0.866f,-0.5f)*r;
  2481. }
  2482. else
  2483. {
  2484. a = center + ImVec2(1,0)*r;
  2485. b = center + ImVec2(-0.500f,0.866f)*r;
  2486. c = center + ImVec2(-0.500f,-0.866f)*r;
  2487. }
  2488. if (shadow && (window->Flags & ImGuiWindowFlags_ShowBorders) != 0)
  2489. window->DrawList->AddTriangleFilled(a+ImVec2(2,2), b+ImVec2(2,2), c+ImVec2(2,2), window->Color(ImGuiCol_BorderShadow));
  2490. window->DrawList->AddTriangleFilled(a, b, c, window->Color(ImGuiCol_Text));
  2491. }
  2492. static void RenderCheckMark(ImVec2 pos, ImU32 col)
  2493. {
  2494. ImGuiState& g = *GImGui;
  2495. ImGuiWindow* window = GetCurrentWindow();
  2496. ImVec2 a, b, c;
  2497. float start_x = (float)(int)(g.FontSize * 0.307f + 0.5f);
  2498. float rem_third = (float)(int)((g.FontSize - start_x) / 3.0f);
  2499. a.x = pos.x + 0.5f + start_x;
  2500. b.x = a.x + rem_third;
  2501. c.x = a.x + rem_third * 3.0f;
  2502. b.y = pos.y - 1.0f + (float)(int)(g.Font->Ascent * (g.FontSize / g.Font->FontSize) + 0.5f) + (float)(int)(g.Font->DisplayOffset.y);
  2503. a.y = b.y - rem_third;
  2504. c.y = b.y - rem_third * 2.0f;
  2505. window->DrawList->PathLineTo(a);
  2506. window->DrawList->PathLineTo(b);
  2507. window->DrawList->PathLineTo(c);
  2508. window->DrawList->PathStroke(col, false);
  2509. }
  2510. // Calculate text size. Text can be multi-line. Optionally ignore text after a ## marker.
  2511. // CalcTextSize("") should return ImVec2(0.0f, GImGui->FontSize)
  2512. ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_text_after_double_hash, float wrap_width)
  2513. {
  2514. ImGuiState& g = *GImGui;
  2515. const char* text_display_end;
  2516. if (hide_text_after_double_hash)
  2517. text_display_end = FindTextDisplayEnd(text, text_end); // Hide anything after a '##' string
  2518. else
  2519. text_display_end = text_end;
  2520. ImFont* font = g.Font;
  2521. const float font_size = g.FontSize;
  2522. ImVec2 text_size = font->CalcTextSizeA(font_size, FLT_MAX, wrap_width, text, text_display_end, NULL);
  2523. // Cancel out character spacing for the last character of a line (it is baked into glyph->XAdvance field)
  2524. const float font_scale = font_size / font->FontSize;
  2525. const float character_spacing_x = 1.0f * font_scale;
  2526. if (text_size.x > 0.0f)
  2527. text_size.x -= character_spacing_x;
  2528. return text_size;
  2529. }
  2530. // Helper to calculate coarse clipping of large list of evenly sized items.
  2531. // NB: Prefer using the ImGuiListClipper higher-level helper if you can!
  2532. // If you are displaying thousands of items and you have a random access to the list, you can perform clipping yourself to save on CPU.
  2533. // {
  2534. // float item_height = ImGui::GetTextLineHeightWithSpacing();
  2535. // int display_start, display_end;
  2536. // ImGui::CalcListClipping(count, item_height, &display_start, &display_end); // calculate how many to clip/display
  2537. // ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (display_start) * item_height); // advance cursor
  2538. // for (int i = display_start; i < display_end; i++) // display only visible items
  2539. // // TODO: display visible item
  2540. // ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (count - display_end) * item_height); // advance cursor
  2541. // }
  2542. void ImGui::CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end)
  2543. {
  2544. ImGuiState& g = *GImGui;
  2545. ImGuiWindow* window = GetCurrentWindow();
  2546. if (g.LogEnabled)
  2547. {
  2548. // If logging is active, do not perform any clipping
  2549. *out_items_display_start = 0;
  2550. *out_items_display_end = items_count;
  2551. return;
  2552. }
  2553. const ImVec2 pos = window->DC.CursorPos;
  2554. const float clip_y1 = window->ClipRect.y;
  2555. const float clip_y2 = window->ClipRect.w;
  2556. int start = (int)((clip_y1 - pos.y) / items_height);
  2557. int end = (int)((clip_y2 - pos.y) / items_height);
  2558. start = ImClamp(start, 0, items_count);
  2559. end = ImClamp(end + 1, start, items_count);
  2560. *out_items_display_start = start;
  2561. *out_items_display_end = end;
  2562. }
  2563. // Find window given position, search front-to-back
  2564. static ImGuiWindow* FindHoveredWindow(ImVec2 pos, bool excluding_childs)
  2565. {
  2566. ImGuiState& g = *GImGui;
  2567. for (int i = g.Windows.Size-1; i >= 0; i--)
  2568. {
  2569. ImGuiWindow* window = g.Windows[i];
  2570. if (!window->Active)
  2571. continue;
  2572. if (window->Flags & ImGuiWindowFlags_NoInputs)
  2573. continue;
  2574. if (excluding_childs && (window->Flags & ImGuiWindowFlags_ChildWindow) != 0)
  2575. continue;
  2576. // Using the clipped AABB so a child window will typically be clipped by its parent.
  2577. ImRect bb(window->ClippedWindowRect.Min - g.Style.TouchExtraPadding, window->ClippedWindowRect.Max + g.Style.TouchExtraPadding);
  2578. if (bb.Contains(pos))
  2579. return window;
  2580. }
  2581. return NULL;
  2582. }
  2583. // Test if mouse cursor is hovering given rectangle
  2584. // NB- Rectangle is clipped by our current clip setting
  2585. // NB- Expand the rectangle to be generous on imprecise inputs systems (g.Style.TouchExtraPadding)
  2586. static bool IsMouseHoveringRect(const ImRect& rect)
  2587. {
  2588. ImGuiState& g = *GImGui;
  2589. ImGuiWindow* window = GetCurrentWindow();
  2590. // Clip
  2591. ImRect rect_clipped = rect;
  2592. rect_clipped.Clip(window->ClipRect);
  2593. // Expand for touch input
  2594. const ImRect rect_for_touch(rect_clipped.Min - g.Style.TouchExtraPadding, rect_clipped.Max + g.Style.TouchExtraPadding);
  2595. return rect_for_touch.Contains(g.IO.MousePos);
  2596. }
  2597. bool ImGui::IsMouseHoveringRect(const ImVec2& rect_min, const ImVec2& rect_max)
  2598. {
  2599. return IsMouseHoveringRect(ImRect(rect_min, rect_max));
  2600. }
  2601. bool ImGui::IsMouseHoveringWindow()
  2602. {
  2603. ImGuiState& g = *GImGui;
  2604. ImGuiWindow* window = GetCurrentWindow();
  2605. return g.HoveredWindow == window;
  2606. }
  2607. bool ImGui::IsMouseHoveringAnyWindow()
  2608. {
  2609. ImGuiState& g = *GImGui;
  2610. return g.HoveredWindow != NULL;
  2611. }
  2612. bool ImGui::IsPosHoveringAnyWindow(const ImVec2& pos)
  2613. {
  2614. return FindHoveredWindow(pos, false) != NULL;
  2615. }
  2616. static bool IsKeyPressedMap(ImGuiKey key, bool repeat)
  2617. {
  2618. const int key_index = GImGui->IO.KeyMap[key];
  2619. return ImGui::IsKeyPressed(key_index, repeat);
  2620. }
  2621. int ImGui::GetKeyIndex(ImGuiKey key)
  2622. {
  2623. IM_ASSERT(key >= 0 && key < ImGuiKey_COUNT);
  2624. return GImGui->IO.KeyMap[key];
  2625. }
  2626. bool ImGui::IsKeyDown(int key_index)
  2627. {
  2628. if (key_index < 0) return false;
  2629. IM_ASSERT(key_index >= 0 && key_index < IM_ARRAYSIZE(GImGui->IO.KeysDown));
  2630. return GImGui->IO.KeysDown[key_index];
  2631. }
  2632. bool ImGui::IsKeyPressed(int key_index, bool repeat)
  2633. {
  2634. ImGuiState& g = *GImGui;
  2635. if (key_index < 0) return false;
  2636. IM_ASSERT(key_index >= 0 && key_index < IM_ARRAYSIZE(g.IO.KeysDown));
  2637. const float t = g.IO.KeysDownDuration[key_index];
  2638. if (t == 0.0f)
  2639. return true;
  2640. if (repeat && t > g.IO.KeyRepeatDelay)
  2641. {
  2642. float delay = g.IO.KeyRepeatDelay, rate = g.IO.KeyRepeatRate;
  2643. if ((fmodf(t - delay, rate) > rate*0.5f) != (fmodf(t - delay - g.IO.DeltaTime, rate) > rate*0.5f))
  2644. return true;
  2645. }
  2646. return false;
  2647. }
  2648. bool ImGui::IsKeyReleased(int key_index)
  2649. {
  2650. ImGuiState& g = *GImGui;
  2651. if (key_index < 0) return false;
  2652. IM_ASSERT(key_index >= 0 && key_index < IM_ARRAYSIZE(g.IO.KeysDown));
  2653. if (g.IO.KeysDownDurationPrev[key_index] >= 0.0f && !g.IO.KeysDown[key_index])
  2654. return true;
  2655. return false;
  2656. }
  2657. bool ImGui::IsMouseDown(int button)
  2658. {
  2659. ImGuiState& g = *GImGui;
  2660. IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));
  2661. return g.IO.MouseDown[button];
  2662. }
  2663. bool ImGui::IsMouseClicked(int button, bool repeat)
  2664. {
  2665. ImGuiState& g = *GImGui;
  2666. IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));
  2667. const float t = g.IO.MouseDownDuration[button];
  2668. if (t == 0.0f)
  2669. return true;
  2670. if (repeat && t > g.IO.KeyRepeatDelay)
  2671. {
  2672. float delay = g.IO.KeyRepeatDelay, rate = g.IO.KeyRepeatRate;
  2673. if ((fmodf(t - delay, rate) > rate*0.5f) != (fmodf(t - delay - g.IO.DeltaTime, rate) > rate*0.5f))
  2674. return true;
  2675. }
  2676. return false;
  2677. }
  2678. bool ImGui::IsMouseReleased(int button)
  2679. {
  2680. ImGuiState& g = *GImGui;
  2681. IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));
  2682. return g.IO.MouseReleased[button];
  2683. }
  2684. bool ImGui::IsMouseDoubleClicked(int button)
  2685. {
  2686. ImGuiState& g = *GImGui;
  2687. IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));
  2688. return g.IO.MouseDoubleClicked[button];
  2689. }
  2690. bool ImGui::IsMouseDragging(int button, float lock_threshold)
  2691. {
  2692. ImGuiState& g = *GImGui;
  2693. IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));
  2694. if (!g.IO.MouseDown[button])
  2695. return false;
  2696. if (lock_threshold < 0.0f)
  2697. lock_threshold = g.IO.MouseDragThreshold;
  2698. return g.IO.MouseDragMaxDistanceSqr[button] >= lock_threshold * lock_threshold;
  2699. }
  2700. ImVec2 ImGui::GetMousePos()
  2701. {
  2702. return GImGui->IO.MousePos;
  2703. }
  2704. ImVec2 ImGui::GetMouseDragDelta(int button, float lock_threshold)
  2705. {
  2706. ImGuiState& g = *GImGui;
  2707. IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));
  2708. if (lock_threshold < 0.0f)
  2709. lock_threshold = g.IO.MouseDragThreshold;
  2710. if (g.IO.MouseDown[button])
  2711. if (g.IO.MouseDragMaxDistanceSqr[button] >= lock_threshold * lock_threshold)
  2712. return g.IO.MousePos - g.IO.MouseClickedPos[button]; // Assume we can only get active with left-mouse button (at the moment).
  2713. return ImVec2(0.0f, 0.0f);
  2714. }
  2715. void ImGui::ResetMouseDragDelta(int button)
  2716. {
  2717. ImGuiState& g = *GImGui;
  2718. IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));
  2719. // NB: We don't need to reset g.IO.MouseDragMaxDistanceSqr
  2720. g.IO.MouseClickedPos[button] = g.IO.MousePos;
  2721. }
  2722. ImGuiMouseCursor ImGui::GetMouseCursor()
  2723. {
  2724. return GImGui->MouseCursor;
  2725. }
  2726. void ImGui::SetMouseCursor(ImGuiMouseCursor cursor_type)
  2727. {
  2728. GImGui->MouseCursor = cursor_type;
  2729. }
  2730. void ImGui::CaptureKeyboardFromApp()
  2731. {
  2732. GImGui->CaptureKeyboardNextFrame = true;
  2733. }
  2734. void ImGui::CaptureMouseFromApp()
  2735. {
  2736. GImGui->CaptureMouseNextFrame = true;
  2737. }
  2738. bool ImGui::IsItemHovered()
  2739. {
  2740. ImGuiWindow* window = GetCurrentWindow();
  2741. return window->DC.LastItemHoveredAndUsable;
  2742. }
  2743. bool ImGui::IsItemHoveredRect()
  2744. {
  2745. ImGuiWindow* window = GetCurrentWindow();
  2746. return window->DC.LastItemHoveredRect;
  2747. }
  2748. bool ImGui::IsItemActive()
  2749. {
  2750. ImGuiState& g = *GImGui;
  2751. if (g.ActiveId)
  2752. {
  2753. ImGuiWindow* window = GetCurrentWindow();
  2754. return g.ActiveId == window->DC.LastItemID;
  2755. }
  2756. return false;
  2757. }
  2758. bool ImGui::IsAnyItemHovered()
  2759. {
  2760. return GImGui->HoveredId != 0 || GImGui->HoveredIdPreviousFrame != 0;
  2761. }
  2762. bool ImGui::IsAnyItemActive()
  2763. {
  2764. return GImGui->ActiveId != 0;
  2765. }
  2766. bool ImGui::IsItemVisible()
  2767. {
  2768. ImGuiWindow* window = GetCurrentWindow();
  2769. ImRect r(window->ClipRect);
  2770. return r.Overlaps(window->DC.LastItemRect);
  2771. }
  2772. ImVec2 ImGui::GetItemRectMin()
  2773. {
  2774. ImGuiWindow* window = GetCurrentWindow();
  2775. return window->DC.LastItemRect.Min;
  2776. }
  2777. ImVec2 ImGui::GetItemRectMax()
  2778. {
  2779. ImGuiWindow* window = GetCurrentWindow();
  2780. return window->DC.LastItemRect.Max;
  2781. }
  2782. ImVec2 ImGui::GetItemRectSize()
  2783. {
  2784. ImGuiWindow* window = GetCurrentWindow();
  2785. return window->DC.LastItemRect.GetSize();
  2786. }
  2787. ImVec2 ImGui::CalcItemRectClosestPoint(const ImVec2& pos, bool on_edge, float outward)
  2788. {
  2789. ImGuiWindow* window = GetCurrentWindow();
  2790. ImRect rect = window->DC.LastItemRect;
  2791. rect.Expand(outward);
  2792. return rect.GetClosestPoint(pos, on_edge);
  2793. }
  2794. // Tooltip is stored and turned into a BeginTooltip()/EndTooltip() sequence at the end of the frame. Each call override previous value.
  2795. void ImGui::SetTooltipV(const char* fmt, va_list args)
  2796. {
  2797. ImGuiState& g = *GImGui;
  2798. ImFormatStringV(g.Tooltip, IM_ARRAYSIZE(g.Tooltip), fmt, args);
  2799. }
  2800. void ImGui::SetTooltip(const char* fmt, ...)
  2801. {
  2802. va_list args;
  2803. va_start(args, fmt);
  2804. SetTooltipV(fmt, args);
  2805. va_end(args);
  2806. }
  2807. float ImGui::GetTime()
  2808. {
  2809. return GImGui->Time;
  2810. }
  2811. int ImGui::GetFrameCount()
  2812. {
  2813. return GImGui->FrameCount;
  2814. }
  2815. static ImVec4 GetVisibleRect()
  2816. {
  2817. ImGuiState& g = *GImGui;
  2818. if (g.IO.DisplayVisibleMin.x != g.IO.DisplayVisibleMax.x && g.IO.DisplayVisibleMin.y != g.IO.DisplayVisibleMax.y)
  2819. return ImVec4(g.IO.DisplayVisibleMin.x, g.IO.DisplayVisibleMin.y, g.IO.DisplayVisibleMax.x, g.IO.DisplayVisibleMax.y);
  2820. return ImVec4(0.0f, 0.0f, g.IO.DisplaySize.x, g.IO.DisplaySize.y);
  2821. }
  2822. void ImGui::BeginTooltip()
  2823. {
  2824. ImGuiState& g = *GImGui;
  2825. ImGuiWindowFlags flags = ImGuiWindowFlags_Tooltip|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_AlwaysAutoResize;
  2826. ImGui::Begin("##Tooltip", NULL, ImVec2(0,0), g.Style.Colors[ImGuiCol_TooltipBg].w, flags);
  2827. }
  2828. void ImGui::EndTooltip()
  2829. {
  2830. IM_ASSERT(GetCurrentWindow()->Flags & ImGuiWindowFlags_Tooltip);
  2831. ImGui::End();
  2832. }
  2833. static bool IsPopupOpen(ImGuiID id)
  2834. {
  2835. ImGuiState& g = *GImGui;
  2836. const bool opened = g.OpenedPopupStack.Size > g.CurrentPopupStack.Size && g.OpenedPopupStack[g.CurrentPopupStack.Size].PopupID == id;
  2837. return opened;
  2838. }
  2839. // One open popup per level of the popup hierarchy (NB: when assigning we reset the Window member of ImGuiPopupRef to NULL)
  2840. void ImGui::OpenPopup(const char* str_id)
  2841. {
  2842. ImGuiState& g = *GImGui;
  2843. ImGuiWindow* window = GetCurrentWindow();
  2844. ImGuiID id = window->GetID(str_id);
  2845. int current_stack_size = g.CurrentPopupStack.Size;
  2846. ImGuiPopupRef popup_ref = ImGuiPopupRef(id, window, window->GetID("##menus")); // Tagged as new ref because constructor sets Window to NULL (we are passing the ParentWindow info here)
  2847. if (g.OpenedPopupStack.Size < current_stack_size + 1)
  2848. g.OpenedPopupStack.push_back(popup_ref);
  2849. else if (g.OpenedPopupStack[current_stack_size].PopupID != id)
  2850. {
  2851. g.OpenedPopupStack.resize(current_stack_size+1);
  2852. g.OpenedPopupStack[current_stack_size] = popup_ref;
  2853. }
  2854. }
  2855. static void CloseInactivePopups()
  2856. {
  2857. ImGuiState& g = *GImGui;
  2858. if (g.OpenedPopupStack.empty())
  2859. return;
  2860. // When popups are stacked, clicking on a lower level popups puts focus back to it and close popups above it.
  2861. // Don't close our own child popup windows
  2862. int n = 0;
  2863. if (g.FocusedWindow)
  2864. {
  2865. for (n = 0; n < g.OpenedPopupStack.Size; n++)
  2866. {
  2867. ImGuiPopupRef& popup = g.OpenedPopupStack[n];
  2868. if (!popup.Window)
  2869. continue;
  2870. IM_ASSERT((popup.Window->Flags & ImGuiWindowFlags_Popup) != 0);
  2871. if (popup.Window->Flags & ImGuiWindowFlags_ChildWindow)
  2872. continue;
  2873. bool has_focus = false;
  2874. for (int m = n; m < g.OpenedPopupStack.Size && !has_focus; m++)
  2875. has_focus = (g.OpenedPopupStack[m].Window && g.OpenedPopupStack[m].Window->RootWindow == g.FocusedWindow->RootWindow);
  2876. if (!has_focus)
  2877. break;
  2878. }
  2879. }
  2880. if (n < g.OpenedPopupStack.Size) // This test is not required but it allows to set a useful breakpoint on the line below
  2881. g.OpenedPopupStack.resize(n);
  2882. }
  2883. static ImGuiWindow* GetFrontMostModalRootWindow()
  2884. {
  2885. ImGuiState& g = *GImGui;
  2886. if (!g.OpenedPopupStack.empty())
  2887. if (ImGuiWindow* front_most_popup = g.OpenedPopupStack.back().Window)
  2888. if (front_most_popup->Flags & ImGuiWindowFlags_Modal)
  2889. return front_most_popup;
  2890. return NULL;
  2891. }
  2892. static void ClosePopupToLevel(int remaining)
  2893. {
  2894. ImGuiState& g = *GImGui;
  2895. if (remaining > 0)
  2896. FocusWindow(g.OpenedPopupStack[remaining-1].Window);
  2897. else
  2898. FocusWindow(g.OpenedPopupStack[0].ParentWindow);
  2899. g.OpenedPopupStack.resize(remaining);
  2900. }
  2901. static void ClosePopup(ImGuiID id)
  2902. {
  2903. if (!IsPopupOpen(id))
  2904. return;
  2905. ImGuiState& g = *GImGui;
  2906. ClosePopupToLevel(g.OpenedPopupStack.Size - 1);
  2907. }
  2908. // Close the popup we have begin-ed into.
  2909. void ImGui::CloseCurrentPopup()
  2910. {
  2911. ImGuiState& g = *GImGui;
  2912. int popup_idx = g.CurrentPopupStack.Size - 1;
  2913. if (popup_idx < 0 || popup_idx > g.OpenedPopupStack.Size || g.CurrentPopupStack[popup_idx].PopupID != g.OpenedPopupStack[popup_idx].PopupID)
  2914. return;
  2915. while (popup_idx > 0 && g.OpenedPopupStack[popup_idx].Window && (g.OpenedPopupStack[popup_idx].Window->Flags & ImGuiWindowFlags_ChildMenu))
  2916. popup_idx--;
  2917. ClosePopupToLevel(popup_idx);
  2918. }
  2919. static void ClearSetNextWindowData()
  2920. {
  2921. ImGuiState& g = *GImGui;
  2922. g.SetNextWindowPosCond = g.SetNextWindowSizeCond = g.SetNextWindowCollapsedCond = g.SetNextWindowFocus = 0;
  2923. }
  2924. static bool BeginPopupEx(const char* str_id, ImGuiWindowFlags extra_flags)
  2925. {
  2926. ImGuiState& g = *GImGui;
  2927. ImGuiWindow* window = g.CurrentWindow;
  2928. const ImGuiID id = window->GetID(str_id);
  2929. if (!IsPopupOpen(id))
  2930. {
  2931. ClearSetNextWindowData(); // We behave like Begin() and need to consume those values
  2932. return false;
  2933. }
  2934. ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
  2935. ImGuiWindowFlags flags = extra_flags|ImGuiWindowFlags_Popup|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_AlwaysAutoResize;
  2936. char name[32];
  2937. if (flags & ImGuiWindowFlags_ChildMenu)
  2938. ImFormatString(name, 20, "##menu_%d", g.CurrentPopupStack.Size); // Recycle windows based on depth
  2939. else
  2940. ImFormatString(name, 20, "##popup_%08x", id); // Not recycling, so we can close/open during the same frame
  2941. float alpha = 1.0f;
  2942. bool opened = ImGui::Begin(name, NULL, ImVec2(0.0f, 0.0f), alpha, flags);
  2943. if (!(window->Flags & ImGuiWindowFlags_ShowBorders))
  2944. GetCurrentWindow()->Flags &= ~ImGuiWindowFlags_ShowBorders;
  2945. if (!opened) // opened can be 'false' when the popup is completely clipped (e.g. zero size display)
  2946. ImGui::EndPopup();
  2947. return opened;
  2948. }
  2949. bool ImGui::BeginPopup(const char* str_id)
  2950. {
  2951. return BeginPopupEx(str_id, ImGuiWindowFlags_ShowBorders);
  2952. }
  2953. bool ImGui::BeginPopupModal(const char* name, bool* p_opened, ImGuiWindowFlags extra_flags)
  2954. {
  2955. ImGuiState& g = *GImGui;
  2956. ImGuiWindow* window = g.CurrentWindow;
  2957. const ImGuiID id = window->GetID(name);
  2958. if (!IsPopupOpen(id))
  2959. {
  2960. ClearSetNextWindowData(); // We behave like Begin() and need to consume those values
  2961. return false;
  2962. }
  2963. ImGuiWindowFlags flags = extra_flags|ImGuiWindowFlags_Popup|ImGuiWindowFlags_Modal|ImGuiWindowFlags_NoCollapse|ImGuiWindowFlags_NoSavedSettings;
  2964. bool opened = ImGui::Begin(name, p_opened, ImVec2(0.0f, 0.0f), -1.0f, flags);
  2965. if (!opened || (p_opened && !*p_opened)) // Opened can be 'false' when the popup is completely clipped (e.g. zero size display)
  2966. {
  2967. ImGui::EndPopup();
  2968. if (opened)
  2969. ClosePopup(id);
  2970. return false;
  2971. }
  2972. return opened;
  2973. }
  2974. void ImGui::EndPopup()
  2975. {
  2976. ImGuiWindow* window = GetCurrentWindow();
  2977. IM_ASSERT(window->Flags & ImGuiWindowFlags_Popup);
  2978. IM_ASSERT(GImGui->CurrentPopupStack.Size > 0);
  2979. ImGui::End();
  2980. if (!(window->Flags & ImGuiWindowFlags_Modal))
  2981. ImGui::PopStyleVar();
  2982. }
  2983. bool ImGui::BeginPopupContextItem(const char* str_id, int mouse_button)
  2984. {
  2985. if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(mouse_button))
  2986. ImGui::OpenPopup(str_id);
  2987. return ImGui::BeginPopup(str_id);
  2988. }
  2989. bool ImGui::BeginPopupContextWindow(bool also_over_items, const char* str_id, int mouse_button)
  2990. {
  2991. if (!str_id) str_id = "window_context_menu";
  2992. if (ImGui::IsMouseHoveringWindow() && ImGui::IsMouseClicked(mouse_button))
  2993. if (also_over_items || !ImGui::IsAnyItemHovered())
  2994. ImGui::OpenPopup(str_id);
  2995. return ImGui::BeginPopup(str_id);
  2996. }
  2997. bool ImGui::BeginPopupContextVoid(const char* str_id, int mouse_button)
  2998. {
  2999. if (!str_id) str_id = "void_context_menu";
  3000. if (!ImGui::IsMouseHoveringAnyWindow() && ImGui::IsMouseClicked(mouse_button))
  3001. ImGui::OpenPopup(str_id);
  3002. return ImGui::BeginPopup(str_id);
  3003. }
  3004. bool ImGui::BeginChild(const char* str_id, const ImVec2& size_arg, bool border, ImGuiWindowFlags extra_flags)
  3005. {
  3006. ImGuiWindow* window = GetCurrentWindow();
  3007. ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_ChildWindow;
  3008. const ImVec2 content_max = window->Pos + ImGui::GetContentRegionMax();
  3009. const ImVec2 cursor_pos = window->Pos + ImGui::GetCursorPos();
  3010. ImVec2 size = size_arg;
  3011. if (size.x <= 0.0f)
  3012. {
  3013. if (size.x == 0.0f)
  3014. flags |= ImGuiWindowFlags_ChildWindowAutoFitX;
  3015. size.x = ImMax(content_max.x - cursor_pos.x, 4.0f) - fabsf(size.x); // Arbitrary minimum zeroish child size of 4.0f
  3016. }
  3017. if (size.y <= 0.0f)
  3018. {
  3019. if (size.y == 0.0f)
  3020. flags |= ImGuiWindowFlags_ChildWindowAutoFitY;
  3021. size.y = ImMax(content_max.y - cursor_pos.y, 4.0f) - fabsf(size.y);
  3022. }
  3023. if (border)
  3024. flags |= ImGuiWindowFlags_ShowBorders;
  3025. flags |= extra_flags;
  3026. char title[256];
  3027. ImFormatString(title, IM_ARRAYSIZE(title), "%s.%s", window->Name, str_id);
  3028. const float alpha = 1.0f;
  3029. bool ret = ImGui::Begin(title, NULL, size, alpha, flags);
  3030. if (!(window->Flags & ImGuiWindowFlags_ShowBorders))
  3031. GetCurrentWindow()->Flags &= ~ImGuiWindowFlags_ShowBorders;
  3032. return ret;
  3033. }
  3034. bool ImGui::BeginChild(ImGuiID id, const ImVec2& size, bool border, ImGuiWindowFlags extra_flags)
  3035. {
  3036. char str_id[32];
  3037. ImFormatString(str_id, IM_ARRAYSIZE(str_id), "child_%08x", id);
  3038. bool ret = ImGui::BeginChild(str_id, size, border, extra_flags);
  3039. return ret;
  3040. }
  3041. void ImGui::EndChild()
  3042. {
  3043. ImGuiWindow* window = GetCurrentWindow();
  3044. IM_ASSERT(window->Flags & ImGuiWindowFlags_ChildWindow);
  3045. if ((window->Flags & ImGuiWindowFlags_ComboBox) || window->BeginCount > 1)
  3046. {
  3047. ImGui::End();
  3048. }
  3049. else
  3050. {
  3051. // When using auto-filling child window, we don't provide full width/height to ItemSize so that it doesn't feed back into automatic size-fitting.
  3052. ImGuiState& g = *GImGui;
  3053. ImVec2 sz = ImGui::GetWindowSize();
  3054. if (window->Flags & ImGuiWindowFlags_ChildWindowAutoFitX) // Arbitrary minimum zeroish child size of 4.0f
  3055. sz.x = ImMax(4.0f, sz.x - g.Style.WindowPadding.x);
  3056. if (window->Flags & ImGuiWindowFlags_ChildWindowAutoFitY)
  3057. sz.y = ImMax(4.0f, sz.y - g.Style.WindowPadding.y);
  3058. ImGui::End();
  3059. window = GetCurrentWindow();
  3060. ImRect bb(window->DC.CursorPos, window->DC.CursorPos + sz);
  3061. ItemSize(sz);
  3062. ItemAdd(bb, NULL);
  3063. }
  3064. }
  3065. // Helper to create a child window / scrolling region that looks like a normal widget frame.
  3066. bool ImGui::BeginChildFrame(ImGuiID id, const ImVec2& size)
  3067. {
  3068. ImGuiState& g = *GImGui;
  3069. const ImGuiStyle& style = g.Style;
  3070. ImGui::PushStyleColor(ImGuiCol_ChildWindowBg, style.Colors[ImGuiCol_FrameBg]);
  3071. ImGui::PushStyleVar(ImGuiStyleVar_ChildWindowRounding, style.FrameRounding);
  3072. return ImGui::BeginChild(id, size);
  3073. }
  3074. void ImGui::EndChildFrame()
  3075. {
  3076. ImGui::EndChild();
  3077. ImGui::PopStyleVar();
  3078. ImGui::PopStyleColor();
  3079. }
  3080. // Save and compare stack sizes on Begin()/End() to detect usage errors
  3081. static void CheckStacksSize(ImGuiWindow* window, bool write)
  3082. {
  3083. // NOT checking: DC.ItemWidth, DC.AllowKeyboardFocus, DC.ButtonRepeat, DC.TextWrapPos (per window) to allow user to conveniently push once and not pop (they are cleared on Begin)
  3084. ImGuiState& g = *GImGui;
  3085. int* p_backup = &window->DC.StackSizesBackup[0];
  3086. { int current = window->IDStack.Size; if (write) *p_backup = current; else IM_ASSERT(*p_backup == current); p_backup++; } // User forgot PopID()
  3087. { int current = window->DC.GroupStack.Size; if (write) *p_backup = current; else IM_ASSERT(*p_backup == current); p_backup++; } // User forgot EndGroup()
  3088. { int current = g.CurrentPopupStack.Size; if (write) *p_backup = current; else IM_ASSERT(*p_backup == current); p_backup++; } // User forgot EndPopup()/EndMenu()
  3089. { int current = g.ColorModifiers.Size; if (write) *p_backup = current; else IM_ASSERT(*p_backup == current); p_backup++; } // User forgot PopStyleColor()
  3090. { int current = g.StyleModifiers.Size; if (write) *p_backup = current; else IM_ASSERT(*p_backup == current); p_backup++; } // User forgot PopStyleVar()
  3091. { int current = g.FontStack.Size; if (write) *p_backup = current; else IM_ASSERT(*p_backup == current); p_backup++; } // User forgot PopFont()
  3092. IM_ASSERT(p_backup == window->DC.StackSizesBackup + IM_ARRAYSIZE(window->DC.StackSizesBackup));
  3093. }
  3094. static ImVec2 FindBestPopupWindowPos(const ImVec2& base_pos, const ImVec2& size, ImGuiWindowFlags flags, int* last_dir, const ImRect& r_inner)
  3095. {
  3096. const ImGuiStyle& style = GImGui->Style;
  3097. // Clamp into visible area while not overlapping the cursor
  3098. ImRect r_outer(GetVisibleRect());
  3099. r_outer.Reduce(style.DisplaySafeAreaPadding);
  3100. ImVec2 base_pos_clamped = ImClamp(base_pos, r_outer.Min, r_outer.Max - size);
  3101. for (int n = (*last_dir != -1) ? -1 : 0; n < 4; n++) // Right, down, up, left. Favor last used direction.
  3102. {
  3103. const int dir = (n == -1) ? *last_dir : n;
  3104. ImRect rect(dir == 0 ? r_inner.Max.x : r_outer.Min.x, dir == 1 ? r_inner.Max.y : r_outer.Min.y, dir == 3 ? r_inner.Min.x : r_outer.Max.x, dir == 2 ? r_inner.Min.y : r_outer.Max.y);
  3105. if (rect.GetWidth() < size.x || rect.GetHeight() < size.y)
  3106. continue;
  3107. *last_dir = dir;
  3108. return ImVec2(dir == 0 ? r_inner.Max.x : dir == 3 ? r_inner.Min.x - size.x : base_pos_clamped.x, dir == 1 ? r_inner.Max.y : dir == 2 ? r_inner.Min.y - size.y : base_pos_clamped.y);
  3109. }
  3110. // Fallback
  3111. *last_dir = -1;
  3112. if (flags & ImGuiWindowFlags_Tooltip) // For tooltip we prefer avoiding the cursor at all cost even if it means that part of the tooltip won't be visible.
  3113. return base_pos + ImVec2(2,2);
  3114. // Otherwise try to keep within display
  3115. ImVec2 pos = base_pos;
  3116. pos.x = ImMax(ImMin(pos.x + size.x, r_outer.Max.x) - size.x, r_outer.Min.x);
  3117. pos.y = ImMax(ImMin(pos.y + size.y, r_outer.Max.y) - size.y, r_outer.Min.y);
  3118. return pos;
  3119. }
  3120. static ImGuiWindow* FindWindowByName(const char* name)
  3121. {
  3122. // FIXME-OPT: Store sorted hashes -> pointers.
  3123. ImGuiState& g = *GImGui;
  3124. ImGuiID id = ImHash(name, 0);
  3125. for (int i = 0; i < g.Windows.Size; i++)
  3126. if (g.Windows[i]->ID == id)
  3127. return g.Windows[i];
  3128. return NULL;
  3129. }
  3130. static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFlags flags)
  3131. {
  3132. ImGuiState& g = *GImGui;
  3133. // Create window the first time
  3134. ImGuiWindow* window = (ImGuiWindow*)ImGui::MemAlloc(sizeof(ImGuiWindow));
  3135. new(window) ImGuiWindow(name);
  3136. window->Flags = flags;
  3137. if (flags & ImGuiWindowFlags_NoSavedSettings)
  3138. {
  3139. // User can disable loading and saving of settings. Tooltip and child windows also don't store settings.
  3140. window->Size = window->SizeFull = size;
  3141. }
  3142. else
  3143. {
  3144. // Retrieve settings from .ini file
  3145. // Use SetWindowPos() or SetNextWindowPos() with the appropriate condition flag to change the initial position of a window.
  3146. window->PosFloat = ImVec2(60, 60);
  3147. window->Pos = ImVec2((float)(int)window->PosFloat.x, (float)(int)window->PosFloat.y);
  3148. ImGuiIniData* settings = FindWindowSettings(name);
  3149. if (!settings)
  3150. {
  3151. settings = AddWindowSettings(name);
  3152. }
  3153. else
  3154. {
  3155. window->SetWindowPosAllowFlags &= ~ImGuiSetCond_FirstUseEver;
  3156. window->SetWindowSizeAllowFlags &= ~ImGuiSetCond_FirstUseEver;
  3157. window->SetWindowCollapsedAllowFlags &= ~ImGuiSetCond_FirstUseEver;
  3158. }
  3159. if (settings->Pos.x != FLT_MAX)
  3160. {
  3161. window->PosFloat = settings->Pos;
  3162. window->Pos = ImVec2((float)(int)window->PosFloat.x, (float)(int)window->PosFloat.y);
  3163. window->Collapsed = settings->Collapsed;
  3164. }
  3165. if (ImLengthSqr(settings->Size) > 0.00001f && !(flags & ImGuiWindowFlags_NoResize))
  3166. size = settings->Size;
  3167. window->Size = window->SizeFull = size;
  3168. }
  3169. if ((flags & ImGuiWindowFlags_AlwaysAutoResize) != 0)
  3170. {
  3171. window->AutoFitFramesX = window->AutoFitFramesY = 2;
  3172. window->AutoFitOnlyGrows = false;
  3173. }
  3174. else
  3175. {
  3176. if (window->Size.x <= 0.0f)
  3177. window->AutoFitFramesX = 2;
  3178. if (window->Size.y <= 0.0f)
  3179. window->AutoFitFramesY = 2;
  3180. window->AutoFitOnlyGrows = (window->AutoFitFramesX > 0) || (window->AutoFitFramesY > 0);
  3181. }
  3182. g.Windows.push_back(window);
  3183. return window;
  3184. }
  3185. // Push a new ImGui window to add widgets to.
  3186. // - A default window called "Debug" is automatically stacked at the beginning of every frame so you can use widgets without explicitly calling a Begin/End pair.
  3187. // - Begin/End can be called multiple times during the frame with the same window name to append content.
  3188. // - 'size_on_first_use' for a regular window denote the initial size for first-time creation (no saved data) and isn't that useful. Use SetNextWindowSize() prior to calling Begin() for more flexible window manipulation.
  3189. // - The window name is used as a unique identifier to preserve window information across frames (and save rudimentary information to the .ini file).
  3190. // You can use the "##" or "###" markers to use the same label with different id, or same id with different label. See documentation at the top of this file.
  3191. // - Return false when window is collapsed, so you can early out in your code. You always need to call ImGui::End() even if false is returned.
  3192. // - Passing 'bool* p_opened' displays a Close button on the upper-right corner of the window, the pointed value will be set to false when the button is pressed.
  3193. // - Passing non-zero 'size' is roughly equivalent to calling SetNextWindowSize(size, ImGuiSetCond_FirstUseEver) prior to calling Begin().
  3194. bool ImGui::Begin(const char* name, bool* p_opened, ImGuiWindowFlags flags)
  3195. {
  3196. return ImGui::Begin(name, p_opened, ImVec2(0.f, 0.f), -1.0f, flags);
  3197. }
  3198. bool ImGui::Begin(const char* name, bool* p_opened, const ImVec2& size_on_first_use, float bg_alpha, ImGuiWindowFlags flags)
  3199. {
  3200. ImGuiState& g = *GImGui;
  3201. const ImGuiStyle& style = g.Style;
  3202. IM_ASSERT(g.Initialized); // Forgot to call ImGui::NewFrame()
  3203. IM_ASSERT(name != NULL); // Must pass a name
  3204. if (flags & ImGuiWindowFlags_NoInputs)
  3205. flags |= ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize;
  3206. // Find or create
  3207. bool window_is_new = false;
  3208. ImGuiWindow* window = FindWindowByName(name);
  3209. if (!window)
  3210. {
  3211. window = CreateNewWindow(name, size_on_first_use, flags);
  3212. window_is_new = true;
  3213. }
  3214. window->Flags = (ImGuiWindowFlags)flags;
  3215. // Add to stack
  3216. ImGuiWindow* parent_window = !g.CurrentWindowStack.empty() ? g.CurrentWindowStack.back() : NULL;
  3217. g.CurrentWindowStack.push_back(window);
  3218. SetCurrentWindow(window);
  3219. CheckStacksSize(window, true);
  3220. IM_ASSERT(parent_window != NULL || !(flags & ImGuiWindowFlags_ChildWindow));
  3221. const int current_frame = ImGui::GetFrameCount();
  3222. bool window_was_visible = (window->LastFrameDrawn == current_frame - 1); // Not using !WasActive because the implicit "Debug" window would always toggle off->on
  3223. if (flags & ImGuiWindowFlags_Popup)
  3224. {
  3225. ImGuiPopupRef& popup_ref = g.OpenedPopupStack[g.CurrentPopupStack.Size];
  3226. window_was_visible &= (window->PopupID == popup_ref.PopupID);
  3227. window_was_visible &= (window == popup_ref.Window);
  3228. popup_ref.Window = window;
  3229. g.CurrentPopupStack.push_back(popup_ref);
  3230. window->PopupID = popup_ref.PopupID;
  3231. }
  3232. // Process SetNextWindow***() calls
  3233. bool window_pos_set_by_api = false, window_size_set_by_api = false;
  3234. if (g.SetNextWindowPosCond)
  3235. {
  3236. const ImVec2 backup_cursor_pos = window->DC.CursorPos; // FIXME: not sure of the exact reason of this anymore :( need to look into that.
  3237. if (!window_was_visible) window->SetWindowPosAllowFlags |= ImGuiSetCond_Appearing;
  3238. window_pos_set_by_api = (window->SetWindowPosAllowFlags & g.SetNextWindowPosCond) != 0;
  3239. if (window_pos_set_by_api && ImLengthSqr(g.SetNextWindowPosVal - ImVec2(-FLT_MAX,-FLT_MAX)) < 0.001f)
  3240. {
  3241. window->SetWindowPosCenterWanted = true; // May be processed on the next frame if this is our first frame and we are measuring size
  3242. window->SetWindowPosAllowFlags &= ~(ImGuiSetCond_Once | ImGuiSetCond_FirstUseEver | ImGuiSetCond_Appearing);
  3243. }
  3244. else
  3245. {
  3246. ImGui::SetWindowPos(g.SetNextWindowPosVal, g.SetNextWindowPosCond);
  3247. }
  3248. window->DC.CursorPos = backup_cursor_pos;
  3249. g.SetNextWindowPosCond = 0;
  3250. }
  3251. if (g.SetNextWindowSizeCond)
  3252. {
  3253. if (!window_was_visible) window->SetWindowSizeAllowFlags |= ImGuiSetCond_Appearing;
  3254. window_size_set_by_api = (window->SetWindowSizeAllowFlags & g.SetNextWindowSizeCond) != 0;
  3255. ImGui::SetWindowSize(g.SetNextWindowSizeVal, g.SetNextWindowSizeCond);
  3256. g.SetNextWindowSizeCond = 0;
  3257. }
  3258. if (g.SetNextWindowCollapsedCond)
  3259. {
  3260. if (!window_was_visible) window->SetWindowCollapsedAllowFlags |= ImGuiSetCond_Appearing;
  3261. ImGui::SetWindowCollapsed(g.SetNextWindowCollapsedVal, g.SetNextWindowCollapsedCond);
  3262. g.SetNextWindowCollapsedCond = 0;
  3263. }
  3264. if (g.SetNextWindowFocus)
  3265. {
  3266. ImGui::SetWindowFocus();
  3267. g.SetNextWindowFocus = false;
  3268. }
  3269. // Update known root window (if we are a child window, otherwise window == window->RootWindow)
  3270. int root_idx, root_non_popup_idx;
  3271. for (root_idx = g.CurrentWindowStack.Size - 1; root_idx > 0; root_idx--)
  3272. if (!(g.CurrentWindowStack[root_idx]->Flags & ImGuiWindowFlags_ChildWindow))
  3273. break;
  3274. for (root_non_popup_idx = root_idx; root_non_popup_idx > 0; root_non_popup_idx--)
  3275. if (!(g.CurrentWindowStack[root_non_popup_idx]->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Popup)))
  3276. break;
  3277. window->RootWindow = g.CurrentWindowStack[root_idx];
  3278. window->RootNonPopupWindow = g.CurrentWindowStack[root_non_popup_idx]; // This is merely for displaying the TitleBgActive color.
  3279. // Default alpha
  3280. if (bg_alpha < 0.0f)
  3281. bg_alpha = style.WindowFillAlphaDefault;
  3282. // When reusing window again multiple times a frame, just append content (don't need to setup again)
  3283. const bool first_begin_of_the_frame = (window->LastFrameDrawn != current_frame);
  3284. if (first_begin_of_the_frame)
  3285. {
  3286. window->Active = true;
  3287. window->BeginCount = 0;
  3288. window->DrawList->Clear();
  3289. window->ClipRect = ImVec4(-FLT_MAX,-FLT_MAX,+FLT_MAX,+FLT_MAX);
  3290. window->LastFrameDrawn = current_frame;
  3291. window->IDStack.resize(1);
  3292. // Setup texture, outer clipping rectangle
  3293. window->DrawList->PushTextureID(g.Font->ContainerAtlas->TexID);
  3294. if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & (ImGuiWindowFlags_ComboBox|ImGuiWindowFlags_Popup)))
  3295. PushClipRect(parent_window->ClipRect);
  3296. else
  3297. PushClipRect(GetVisibleRect());
  3298. // New windows appears in front
  3299. if (!window_was_visible)
  3300. {
  3301. window->AutoPosLastDirection = -1;
  3302. if (!(flags & (ImGuiWindowFlags_ChildWindow|ImGuiWindowFlags_Tooltip)) || (flags & ImGuiWindowFlags_Popup))
  3303. FocusWindow(window);
  3304. // Popup first latch mouse position, will position itself when it appears next frame
  3305. if ((flags & ImGuiWindowFlags_Popup) != 0 && !window_pos_set_by_api)
  3306. window->PosFloat = g.IO.MousePos;
  3307. }
  3308. // Collapse window by double-clicking on title bar
  3309. // At this point we don't have a clipping rectangle setup yet, so we can use the title bar area for hit detection and drawing
  3310. if (!(flags & ImGuiWindowFlags_NoTitleBar) && !(flags & ImGuiWindowFlags_NoCollapse))
  3311. {
  3312. if (g.HoveredWindow == window && IsMouseHoveringRect(window->TitleBarRect()) && g.IO.MouseDoubleClicked[0])
  3313. {
  3314. window->Collapsed = !window->Collapsed;
  3315. if (!(flags & ImGuiWindowFlags_NoSavedSettings))
  3316. MarkSettingsDirty();
  3317. FocusWindow(window);
  3318. }
  3319. }
  3320. else
  3321. {
  3322. window->Collapsed = false;
  3323. }
  3324. const bool window_appearing_after_being_hidden = (window->HiddenFrames == 1);
  3325. if (window->HiddenFrames > 0)
  3326. window->HiddenFrames--;
  3327. // SIZE
  3328. // Save contents size from last frame for auto-fitting
  3329. window->SizeContents = window_is_new ? ImVec2(0.0f, 0.0f) : window->DC.CursorMaxPos - window->Pos;
  3330. window->SizeContents.y += window->ScrollY;
  3331. // Hide popup/tooltip window when first appearing while we measure size (because we recycle them)
  3332. if ((flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_Tooltip)) != 0 && !window_was_visible)
  3333. {
  3334. window->HiddenFrames = 1;
  3335. if (flags & ImGuiWindowFlags_AlwaysAutoResize)
  3336. {
  3337. if (!window_size_set_by_api)
  3338. window->Size = window->SizeFull = ImVec2(0.f, 0.f);
  3339. window->SizeContents = ImVec2(0.f, 0.f);
  3340. }
  3341. }
  3342. // Lock window padding so that altering the ShowBorders flag for childs doesn't have side-effects.
  3343. window->WindowPadding = ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & (ImGuiWindowFlags_ShowBorders | ImGuiWindowFlags_ComboBox | ImGuiWindowFlags_Popup))) ? ImVec2(0,0) : style.WindowPadding;
  3344. // Calculate auto-fit size
  3345. ImVec2 size_auto_fit;
  3346. if ((flags & ImGuiWindowFlags_Tooltip) != 0)
  3347. {
  3348. // Tooltip always resize. We keep the spacing symmetric on both axises for aesthetic purpose.
  3349. size_auto_fit = window->SizeContents + window->WindowPadding - ImVec2(0.0f, style.ItemSpacing.y);
  3350. }
  3351. else
  3352. {
  3353. size_auto_fit = ImClamp(window->SizeContents + window->WindowPadding, style.WindowMinSize, ImMax(style.WindowMinSize, g.IO.DisplaySize - window->WindowPadding));
  3354. if (size_auto_fit.y < window->SizeContents.y && !(flags & ImGuiWindowFlags_NoScrollbar))
  3355. size_auto_fit.x += style.ScrollbarWidth;
  3356. size_auto_fit.y = ImMax(size_auto_fit.y - style.ItemSpacing.y, 0.0f);
  3357. }
  3358. // Handle automatic resize
  3359. if (window->Collapsed)
  3360. {
  3361. // We still process initial auto-fit on collapsed windows to get a window width,
  3362. // But otherwise we don't honor ImGuiWindowFlags_AlwaysAutoResize when collapsed.
  3363. if (window->AutoFitFramesX > 0)
  3364. window->SizeFull.x = window->AutoFitOnlyGrows ? ImMax(window->SizeFull.x, size_auto_fit.x) : size_auto_fit.x;
  3365. if (window->AutoFitFramesY > 0)
  3366. window->SizeFull.y = window->AutoFitOnlyGrows ? ImMax(window->SizeFull.y, size_auto_fit.y) : size_auto_fit.y;
  3367. window->Size = window->TitleBarRect().GetSize();
  3368. }
  3369. else
  3370. {
  3371. if ((flags & ImGuiWindowFlags_AlwaysAutoResize) && !window_size_set_by_api)
  3372. {
  3373. window->SizeFull = size_auto_fit;
  3374. }
  3375. else if ((window->AutoFitFramesX > 0 || window->AutoFitFramesY > 0) && !window_size_set_by_api)
  3376. {
  3377. // Auto-fit only grows during the first few frames
  3378. if (window->AutoFitFramesX > 0)
  3379. window->SizeFull.x = window->AutoFitOnlyGrows ? ImMax(window->SizeFull.x, size_auto_fit.x) : size_auto_fit.x;
  3380. if (window->AutoFitFramesY > 0)
  3381. window->SizeFull.y = window->AutoFitOnlyGrows ? ImMax(window->SizeFull.y, size_auto_fit.y) : size_auto_fit.y;
  3382. if (!(flags & ImGuiWindowFlags_NoSavedSettings))
  3383. MarkSettingsDirty();
  3384. }
  3385. window->Size = window->SizeFull;
  3386. }
  3387. // Minimum window size
  3388. if (!(flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_AlwaysAutoResize)))
  3389. {
  3390. window->SizeFull = ImMax(window->SizeFull, style.WindowMinSize);
  3391. if (!window->Collapsed)
  3392. window->Size = window->SizeFull;
  3393. }
  3394. // POSITION
  3395. // Position child window
  3396. if (flags & ImGuiWindowFlags_ChildWindow)
  3397. parent_window->DC.ChildWindows.push_back(window);
  3398. if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Popup))
  3399. {
  3400. window->Pos = window->PosFloat = parent_window->DC.CursorPos;
  3401. window->Size = window->SizeFull = size_on_first_use; // NB: argument name 'size_on_first_use' misleading here, it's really just 'size' as provided by user.
  3402. }
  3403. bool window_pos_center = false;
  3404. window_pos_center |= (window->SetWindowPosCenterWanted && window->HiddenFrames == 0);
  3405. window_pos_center |= ((flags & ImGuiWindowFlags_Modal) && !window_pos_set_by_api && window_appearing_after_being_hidden);
  3406. if (window_pos_center)
  3407. {
  3408. // Center (any sort of window)
  3409. ImRect fullscreen_rect(GetVisibleRect());
  3410. SetWindowPos(ImMax(style.DisplaySafeAreaPadding, fullscreen_rect.GetCenter() - window->SizeFull * 0.5f));
  3411. }
  3412. else if (flags & ImGuiWindowFlags_ChildMenu)
  3413. {
  3414. IM_ASSERT(window_pos_set_by_api);
  3415. ImRect rect_to_avoid;
  3416. if (parent_window->DC.MenuBarAppending)
  3417. rect_to_avoid = ImRect(-FLT_MAX, parent_window->Pos.y + parent_window->TitleBarHeight(), FLT_MAX, parent_window->Pos.y + parent_window->TitleBarHeight() + parent_window->MenuBarHeight());
  3418. else
  3419. rect_to_avoid = ImRect(parent_window->Pos.x + style.ItemSpacing.x, -FLT_MAX, parent_window->Pos.x + parent_window->Size.x - style.ItemSpacing.x - parent_window->ScrollbarWidth(), FLT_MAX); // We want some overlap to convey the relative depth of each popup (here hard-coded to 4)
  3420. window->PosFloat = FindBestPopupWindowPos(window->PosFloat, window->Size, flags, &window->AutoPosLastDirection, rect_to_avoid);
  3421. }
  3422. else if ((flags & ImGuiWindowFlags_Popup) != 0 && !window_pos_set_by_api && window_appearing_after_being_hidden)
  3423. {
  3424. ImRect rect_to_avoid(window->PosFloat.x - 1, window->PosFloat.y - 1, window->PosFloat.x + 1, window->PosFloat.y + 1);
  3425. window->PosFloat = FindBestPopupWindowPos(window->PosFloat, window->Size, flags, &window->AutoPosLastDirection, rect_to_avoid);
  3426. }
  3427. // Position tooltip (always follows mouse)
  3428. if ((flags & ImGuiWindowFlags_Tooltip) != 0 && !window_pos_set_by_api)
  3429. {
  3430. ImRect rect_to_avoid(g.IO.MousePos.x - 16, g.IO.MousePos.y - 8, g.IO.MousePos.x + 24, g.IO.MousePos.y + 24); // FIXME: Completely hard-coded. Perhaps center on cursor hit-point instead?
  3431. window->PosFloat = FindBestPopupWindowPos(g.IO.MousePos, window->Size, flags, &window->AutoPosLastDirection, rect_to_avoid);
  3432. }
  3433. // User moving window (at the beginning of the frame to avoid input lag or sheering). Only valid for root windows.
  3434. RegisterAliveId(window->MoveID);
  3435. if (g.ActiveId == window->MoveID)
  3436. {
  3437. if (g.IO.MouseDown[0])
  3438. {
  3439. if (!(flags & ImGuiWindowFlags_NoMove))
  3440. {
  3441. window->PosFloat += g.IO.MouseDelta;
  3442. if (!(flags & ImGuiWindowFlags_NoSavedSettings))
  3443. MarkSettingsDirty();
  3444. }
  3445. IM_ASSERT(g.MovedWindow != NULL);
  3446. FocusWindow(g.MovedWindow);
  3447. }
  3448. else
  3449. {
  3450. SetActiveId(0);
  3451. g.MovedWindow = NULL; // Not strictly necessary but doing it for sanity.
  3452. }
  3453. }
  3454. // Clamp position so it stays visible
  3455. if (!(flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Tooltip))
  3456. {
  3457. if (!window_pos_set_by_api && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0 && g.IO.DisplaySize.x > 0.0f && g.IO.DisplaySize.y > 0.0f) // Ignore zero-sized display explicitly to avoid losing positions if a window manager reports zero-sized window when initializing or minimizing.
  3458. {
  3459. ImVec2 padding = ImMax(style.DisplayWindowPadding, style.DisplaySafeAreaPadding);
  3460. window->PosFloat = ImMax(window->PosFloat + window->Size, padding) - window->Size;
  3461. window->PosFloat = ImMin(window->PosFloat, g.IO.DisplaySize - padding);
  3462. }
  3463. }
  3464. window->Pos = ImVec2((float)(int)window->PosFloat.x, (float)(int)window->PosFloat.y);
  3465. // Default item width. Make it proportional to window size if window manually resizes
  3466. if (window->Size.x > 0.0f && !(flags & ImGuiWindowFlags_Tooltip) && !(flags & ImGuiWindowFlags_AlwaysAutoResize))
  3467. window->ItemWidthDefault = (float)(int)(window->Size.x * 0.65f);
  3468. else
  3469. window->ItemWidthDefault = (float)(int)(g.FontSize * 16.0f);
  3470. // Prepare for focus requests
  3471. window->FocusIdxAllRequestCurrent = (window->FocusIdxAllRequestNext == IM_INT_MAX || window->FocusIdxAllCounter == -1) ? IM_INT_MAX : (window->FocusIdxAllRequestNext + (window->FocusIdxAllCounter+1)) % (window->FocusIdxAllCounter+1);
  3472. window->FocusIdxTabRequestCurrent = (window->FocusIdxTabRequestNext == IM_INT_MAX || window->FocusIdxTabCounter == -1) ? IM_INT_MAX : (window->FocusIdxTabRequestNext + (window->FocusIdxTabCounter+1)) % (window->FocusIdxTabCounter+1);
  3473. window->FocusIdxAllCounter = window->FocusIdxTabCounter = -1;
  3474. window->FocusIdxAllRequestNext = window->FocusIdxTabRequestNext = IM_INT_MAX;
  3475. // Apply scrolling
  3476. if (window->ScrollTargetRelY < FLT_MAX)
  3477. {
  3478. float center_ratio_y = window->ScrollTargetCenterRatioY;
  3479. window->ScrollY = window->ScrollTargetRelY - ((1.0f - center_ratio_y) * window->TitleBarHeight()) - (center_ratio_y * window->SizeFull.y);
  3480. window->ScrollTargetRelY = FLT_MAX;
  3481. }
  3482. window->ScrollY = ImMax(window->ScrollY, 0.0f);
  3483. if (!window->Collapsed && !window->SkipItems)
  3484. window->ScrollY = ImMin(window->ScrollY, ImMax(0.0f, window->SizeContents.y - window->SizeFull.y));
  3485. // Modal window darkens what is behind them
  3486. if ((flags & ImGuiWindowFlags_Modal) != 0 && window == GetFrontMostModalRootWindow())
  3487. {
  3488. ImRect fullscreen_rect = GetVisibleRect();
  3489. window->DrawList->AddRectFilled(fullscreen_rect.Min, fullscreen_rect.Max, window->Color(ImGuiCol_ModalWindowDarkening, g.ModalWindowDarkeningRatio));
  3490. }
  3491. // Draw window + handle manual resize
  3492. ImRect title_bar_rect = window->TitleBarRect();
  3493. const float window_rounding = (flags & ImGuiWindowFlags_ChildWindow) ? style.ChildWindowRounding : style.WindowRounding;
  3494. if (window->Collapsed)
  3495. {
  3496. // Draw title bar only
  3497. window->DrawList->AddRectFilled(title_bar_rect.GetTL(), title_bar_rect.GetBR(), window->Color(ImGuiCol_TitleBgCollapsed), window_rounding);
  3498. if (flags & ImGuiWindowFlags_ShowBorders)
  3499. {
  3500. window->DrawList->AddRect(title_bar_rect.GetTL()+ImVec2(1,1), title_bar_rect.GetBR()+ImVec2(1,1), window->Color(ImGuiCol_BorderShadow), window_rounding);
  3501. window->DrawList->AddRect(title_bar_rect.GetTL(), title_bar_rect.GetBR(), window->Color(ImGuiCol_Border), window_rounding);
  3502. }
  3503. }
  3504. else
  3505. {
  3506. ImU32 resize_col = 0;
  3507. if (!(flags & ImGuiWindowFlags_AlwaysAutoResize) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0 && !(flags & ImGuiWindowFlags_NoResize))
  3508. {
  3509. // Manual resize
  3510. const ImRect resize_rect(window->Rect().GetBR()-ImVec2(14,14), window->Rect().GetBR());
  3511. const ImGuiID resize_id = window->GetID("#RESIZE");
  3512. bool hovered, held;
  3513. ButtonBehavior(resize_rect, resize_id, &hovered, &held, true, ImGuiButtonFlags_FlattenChilds);
  3514. resize_col = window->Color(held ? ImGuiCol_ResizeGripActive : hovered ? ImGuiCol_ResizeGripHovered : ImGuiCol_ResizeGrip);
  3515. if (hovered || held)
  3516. g.MouseCursor = ImGuiMouseCursor_ResizeNWSE;
  3517. if (g.HoveredWindow == window && held && g.IO.MouseDoubleClicked[0])
  3518. {
  3519. // Manual auto-fit when double-clicking
  3520. window->SizeFull = size_auto_fit;
  3521. if (!(flags & ImGuiWindowFlags_NoSavedSettings))
  3522. MarkSettingsDirty();
  3523. SetActiveId(0);
  3524. }
  3525. else if (held)
  3526. {
  3527. window->SizeFull = ImMax(window->SizeFull + g.IO.MouseDelta, style.WindowMinSize);
  3528. if (!(flags & ImGuiWindowFlags_NoSavedSettings))
  3529. MarkSettingsDirty();
  3530. }
  3531. window->Size = window->SizeFull;
  3532. title_bar_rect = window->TitleBarRect();
  3533. }
  3534. // Scrollbar
  3535. window->ScrollbarY = (window->SizeContents.y > window->Size.y + style.ItemSpacing.y) && !(flags & ImGuiWindowFlags_NoScrollbar);
  3536. // Window background
  3537. if (bg_alpha > 0.0f)
  3538. {
  3539. if ((flags & ImGuiWindowFlags_ComboBox) != 0)
  3540. window->DrawList->AddRectFilled(window->Pos, window->Pos+window->Size, window->Color(ImGuiCol_ComboBg, bg_alpha), window_rounding);
  3541. else if ((flags & ImGuiWindowFlags_Tooltip) != 0)
  3542. window->DrawList->AddRectFilled(window->Pos, window->Pos+window->Size, window->Color(ImGuiCol_TooltipBg, bg_alpha), window_rounding);
  3543. else if ((flags & ImGuiWindowFlags_Popup) != 0)
  3544. window->DrawList->AddRectFilled(window->Pos, window->Pos+window->Size, window->Color(ImGuiCol_WindowBg, bg_alpha), window_rounding);
  3545. else if ((flags & ImGuiWindowFlags_ChildWindow) != 0)
  3546. window->DrawList->AddRectFilled(window->Pos, window->Pos+window->Size-ImVec2(window->ScrollbarWidth(),0.0f), window->Color(ImGuiCol_ChildWindowBg, bg_alpha), window_rounding, window->ScrollbarY ? (1|8) : (0xF));
  3547. else
  3548. window->DrawList->AddRectFilled(window->Pos, window->Pos+window->Size, window->Color(ImGuiCol_WindowBg, bg_alpha), window_rounding);
  3549. }
  3550. // Title bar
  3551. if (!(flags & ImGuiWindowFlags_NoTitleBar))
  3552. window->DrawList->AddRectFilled(title_bar_rect.GetTL(), title_bar_rect.GetBR(), window->Color((g.FocusedWindow && window->RootNonPopupWindow == g.FocusedWindow->RootNonPopupWindow) ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBg), window_rounding, 1|2);
  3553. // Menu bar
  3554. if (flags & ImGuiWindowFlags_MenuBar)
  3555. {
  3556. ImRect menu_bar_rect = window->MenuBarRect();
  3557. window->DrawList->AddRectFilled(menu_bar_rect.GetTL(), menu_bar_rect.GetBR(), window->Color(ImGuiCol_MenuBarBg), (flags & ImGuiWindowFlags_NoTitleBar) ? window_rounding : 0.0f, 1|2);
  3558. }
  3559. // Borders
  3560. if (flags & ImGuiWindowFlags_ShowBorders)
  3561. {
  3562. window->DrawList->AddRect(window->Pos+ImVec2(1,1), window->Pos+window->Size+ImVec2(1,1), window->Color(ImGuiCol_BorderShadow), window_rounding);
  3563. window->DrawList->AddRect(window->Pos, window->Pos+window->Size, window->Color(ImGuiCol_Border), window_rounding);
  3564. if (!(flags & ImGuiWindowFlags_NoTitleBar))
  3565. window->DrawList->AddLine(title_bar_rect.GetBL(), title_bar_rect.GetBR(), window->Color(ImGuiCol_Border));
  3566. }
  3567. // Scrollbar
  3568. if (window->ScrollbarY)
  3569. Scrollbar(window);
  3570. // Render resize grip
  3571. // (after the input handling so we don't have a frame of latency)
  3572. if (!(flags & ImGuiWindowFlags_NoResize))
  3573. {
  3574. const float base_size = g.FontSize * 1.35f;
  3575. const float min_size = window_rounding + 1.0f + g.FontSize * 0.2f;
  3576. const float corner_size = ImMax(base_size, min_size);
  3577. const ImVec2 br = window->Rect().GetBR();
  3578. window->DrawList->PathLineTo(br + ImVec2(-corner_size, 0.0f));
  3579. window->DrawList->PathLineTo(br + ImVec2(0.0f, -corner_size));
  3580. window->DrawList->PathArcToFast(ImVec2(br.x - window_rounding, br.y - window_rounding), window_rounding, 0, 3);
  3581. window->DrawList->PathFill(resize_col);
  3582. }
  3583. }
  3584. // Setup drawing context
  3585. window->DC.ColumnsStartX = window->WindowPadding.x;
  3586. window->DC.ColumnsOffsetX = 0.0f;
  3587. window->DC.CursorStartPos = window->Pos + ImVec2(window->DC.ColumnsStartX + window->DC.ColumnsOffsetX, window->TitleBarHeight() + window->MenuBarHeight() + window->WindowPadding.y) - ImVec2(0.0f, window->ScrollY);
  3588. window->DC.CursorPos = window->DC.CursorStartPos;
  3589. window->DC.CursorPosPrevLine = window->DC.CursorPos;
  3590. window->DC.CursorMaxPos = window->DC.CursorStartPos;
  3591. window->DC.CurrentLineHeight = window->DC.PrevLineHeight = 0.0f;
  3592. window->DC.CurrentLineTextBaseOffset = window->DC.PrevLineTextBaseOffset = 0.0f;
  3593. window->DC.MenuBarAppending = false;
  3594. window->DC.MenuBarOffsetX = ImMax(window->DC.ColumnsStartX, style.ItemSpacing.x);
  3595. window->DC.LogLinePosY = window->DC.CursorPos.y - 9999.0f;
  3596. window->DC.ChildWindows.resize(0);
  3597. window->DC.LayoutType = ImGuiLayoutType_Vertical;
  3598. window->DC.ItemWidth = window->ItemWidthDefault;
  3599. window->DC.ItemWidthStack.resize(0);
  3600. window->DC.ButtonRepeat = false;
  3601. window->DC.ButtonRepeatStack.resize(0);
  3602. window->DC.AllowKeyboardFocus = true;
  3603. window->DC.AllowKeyboardFocusStack.resize(0);
  3604. window->DC.TextWrapPos = -1.0f; // disabled
  3605. window->DC.TextWrapPosStack.resize(0);
  3606. window->DC.ColorEditMode = ImGuiColorEditMode_UserSelect;
  3607. window->DC.ColumnsCurrent = 0;
  3608. window->DC.ColumnsCount = 1;
  3609. window->DC.ColumnsStartPos = window->DC.CursorPos;
  3610. window->DC.ColumnsCellMinY = window->DC.ColumnsCellMaxY = window->DC.ColumnsStartPos.y;
  3611. window->DC.TreeDepth = 0;
  3612. window->DC.StateStorage = &window->StateStorage;
  3613. window->DC.GroupStack.resize(0);
  3614. window->MenuColumns.Update(3, style.ItemSpacing.x, !window_was_visible);
  3615. if (window->AutoFitFramesX > 0)
  3616. window->AutoFitFramesX--;
  3617. if (window->AutoFitFramesY > 0)
  3618. window->AutoFitFramesY--;
  3619. // Title bar
  3620. if (!(flags & ImGuiWindowFlags_NoTitleBar))
  3621. {
  3622. if (p_opened != NULL)
  3623. CloseWindowButton(p_opened);
  3624. const ImVec2 text_size = CalcTextSize(name, NULL, true);
  3625. if (!(flags & ImGuiWindowFlags_NoCollapse))
  3626. RenderCollapseTriangle(window->Pos + style.FramePadding, !window->Collapsed, 1.0f, true);
  3627. ImVec2 text_min = window->Pos + style.FramePadding;
  3628. ImVec2 text_max = window->Pos + ImVec2(window->Size.x - style.FramePadding.x, style.FramePadding.y*2 + text_size.y);
  3629. ImVec2 clip_max = ImVec2(window->Pos.x + window->Size.x - (p_opened ? title_bar_rect.GetHeight() - 3 : style.FramePadding.x), text_max.y); // Match the size of CloseWindowButton()
  3630. bool pad_left = (flags & ImGuiWindowFlags_NoCollapse) == 0;
  3631. bool pad_right = (p_opened != NULL);
  3632. if (style.WindowTitleAlign & ImGuiAlign_Center) pad_right = pad_left;
  3633. if (pad_left) text_min.x += g.FontSize + style.ItemInnerSpacing.x;
  3634. if (pad_right) text_max.x -= g.FontSize + style.ItemInnerSpacing.x;
  3635. RenderTextClipped(text_min, text_max, name, NULL, &text_size, style.WindowTitleAlign, NULL, &clip_max);
  3636. }
  3637. // Save clipped aabb so we can access it in constant-time in FindHoveredWindow()
  3638. window->ClippedWindowRect = window->Rect();
  3639. window->ClippedWindowRect.Clip(window->ClipRect);
  3640. // Pressing CTRL+C while holding on a window copy its content to the clipboard
  3641. // This works but 1. doesn't handle multiple Begin/End pairs, 2. recursing into another Begin/End pair - so we need to work that out and add better logging scope.
  3642. // Maybe we can support CTRL+C on every element?
  3643. /*
  3644. if (g.ActiveId == move_id)
  3645. if (g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_C))
  3646. ImGui::LogToClipboard();
  3647. */
  3648. }
  3649. window->BeginCount++;
  3650. // Inner clipping rectangle
  3651. // We set this up after processing the resize grip so that our clip rectangle doesn't lag by a frame
  3652. // Note that if our window is collapsed we will end up with a null clipping rectangle which is the correct behavior.
  3653. const ImRect title_bar_rect = window->TitleBarRect();
  3654. ImVec4 clip_rect(title_bar_rect.Min.x+0.5f+window->WindowPadding.x*0.5f, title_bar_rect.Max.y+window->MenuBarHeight()+0.5f, window->Rect().Max.x+0.5f-window->WindowPadding.x*0.5f, window->Rect().Max.y-1.5f);
  3655. if (window->ScrollbarY)
  3656. clip_rect.z -= style.ScrollbarWidth;
  3657. PushClipRect(clip_rect);
  3658. // Clear 'accessed' flag last thing
  3659. if (first_begin_of_the_frame)
  3660. window->Accessed = false;
  3661. // Child window can be out of sight and have "negative" clip windows.
  3662. // Mark them as collapsed so commands are skipped earlier (we can't manually collapse because they have no title bar).
  3663. if (flags & ImGuiWindowFlags_ChildWindow)
  3664. {
  3665. IM_ASSERT((flags & ImGuiWindowFlags_NoTitleBar) != 0);
  3666. window->Collapsed = parent_window && parent_window->Collapsed;
  3667. if (!(flags & ImGuiWindowFlags_AlwaysAutoResize) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0)
  3668. {
  3669. const ImVec4 clip_rect_t = window->ClipRect;
  3670. window->Collapsed |= (clip_rect_t.x >= clip_rect_t.z || clip_rect_t.y >= clip_rect_t.w);
  3671. }
  3672. // We also hide the window from rendering because we've already added its border to the command list.
  3673. // (we could perform the check earlier in the function but it is simpler at this point)
  3674. if (window->Collapsed)
  3675. window->Active = false;
  3676. }
  3677. if (style.Alpha <= 0.0f)
  3678. window->Active = false;
  3679. // Return false if we don't intend to display anything to allow user to perform an early out optimization
  3680. window->SkipItems = (window->Collapsed || !window->Active) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0;
  3681. return !window->SkipItems;
  3682. }
  3683. void ImGui::End()
  3684. {
  3685. ImGuiState& g = *GImGui;
  3686. ImGuiWindow* window = g.CurrentWindow;
  3687. ImGui::Columns(1, "#CloseColumns");
  3688. PopClipRect(); // inner window clip rectangle
  3689. // Stop logging
  3690. if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) // FIXME: add more options for scope of logging
  3691. ImGui::LogFinish();
  3692. // Pop
  3693. // NB: we don't clear 'window->RootWindow'. The pointer is allowed to live until the next call to Begin().
  3694. g.CurrentWindowStack.pop_back();
  3695. if (window->Flags & ImGuiWindowFlags_Popup)
  3696. g.CurrentPopupStack.pop_back();
  3697. CheckStacksSize(window, false);
  3698. SetCurrentWindow(g.CurrentWindowStack.empty() ? NULL : g.CurrentWindowStack.back());
  3699. }
  3700. // Vertical scrollbar
  3701. // The entire piece of code below is rather confusing because:
  3702. // - We handle absolute seeking (when first clicking outside the grab) and relative manipulation (afterward or when clicking inside the grab)
  3703. // - We store values as ratio and in a form that allows the window content to change while we are holding on a scrollbar
  3704. static void Scrollbar(ImGuiWindow* window)
  3705. {
  3706. ImGuiState& g = *GImGui;
  3707. const ImGuiStyle& style = g.Style;
  3708. const ImGuiID id = window->GetID("#SCROLLY");
  3709. // Render background
  3710. ImRect bb(window->Rect().Max.x - style.ScrollbarWidth, window->Pos.y + window->TitleBarHeight()+1, window->Rect().Max.x, window->Rect().Max.y-1);
  3711. window->DrawList->AddRectFilled(bb.Min, bb.Max, window->Color(ImGuiCol_ScrollbarBg));
  3712. bb.Expand(-3.0f);
  3713. const float scrollbar_height = bb.GetHeight();
  3714. // The grabable box size generally represent the amount visible (vs the total scrollable amount)
  3715. // But we maintain a minimum size in pixel to allow for the user to still aim inside.
  3716. const float grab_h_pixels = ImMin(ImMax(scrollbar_height * ImSaturate(window->Size.y / ImMax(window->SizeContents.y, window->Size.y)), style.GrabMinSize), scrollbar_height);
  3717. const float grab_h_norm = grab_h_pixels / scrollbar_height;
  3718. // Handle input right away. None of the code of Begin() is relying on scrolling position before calling Scrollbar().
  3719. bool held = false;
  3720. bool hovered = false;
  3721. const bool previously_held = (g.ActiveId == id);
  3722. ButtonBehavior(bb, id, &hovered, &held, true);
  3723. const float scroll_max = ImMax(1.0f, window->SizeContents.y - window->Size.y);
  3724. float scroll_ratio = ImSaturate(window->ScrollY / scroll_max);
  3725. float grab_y_norm = scroll_ratio * (scrollbar_height - grab_h_pixels) / scrollbar_height;
  3726. if (held)
  3727. {
  3728. const float clicked_y_norm = ImSaturate((g.IO.MousePos.y - bb.Min.y) / scrollbar_height); // Click position in scrollbar space (0.0f->1.0f)
  3729. g.HoveredId = id;
  3730. bool seek_absolute = false;
  3731. if (!previously_held)
  3732. {
  3733. // On initial click calculate the distance between mouse and the center of the grab
  3734. if (clicked_y_norm >= grab_y_norm && clicked_y_norm <= grab_y_norm + grab_h_norm)
  3735. {
  3736. g.ScrollbarClickDeltaToGrabCenter = clicked_y_norm - grab_y_norm - grab_h_norm*0.5f;
  3737. }
  3738. else
  3739. {
  3740. seek_absolute = true;
  3741. g.ScrollbarClickDeltaToGrabCenter = 0;
  3742. }
  3743. }
  3744. // Apply scroll
  3745. // It is ok to modify ScrollY here because we are being called in Begin() after the calculation of SizeContents and before setting up our starting position
  3746. const float scroll_y_norm = ImSaturate((clicked_y_norm - g.ScrollbarClickDeltaToGrabCenter - grab_h_norm*0.5f) / (1.0f - grab_h_norm));
  3747. window->ScrollY = (float)(int)(0.5f + scroll_y_norm * (window->SizeContents.y - window->Size.y));
  3748. // Update values for rendering
  3749. scroll_ratio = ImSaturate(window->ScrollY / scroll_max);
  3750. grab_y_norm = scroll_ratio * (scrollbar_height - grab_h_pixels) / scrollbar_height;
  3751. // Update distance to grab now that we have seeked and saturated
  3752. if (seek_absolute)
  3753. g.ScrollbarClickDeltaToGrabCenter = clicked_y_norm - grab_y_norm - grab_h_norm*0.5f;
  3754. }
  3755. // Render
  3756. const ImU32 grab_col = window->Color(held ? ImGuiCol_ScrollbarGrabActive : hovered ? ImGuiCol_ScrollbarGrabHovered : ImGuiCol_ScrollbarGrab);
  3757. window->DrawList->AddRectFilled(ImVec2(bb.Min.x, ImLerp(bb.Min.y, bb.Max.y, grab_y_norm)), ImVec2(bb.Max.x, ImLerp(bb.Min.y, bb.Max.y, grab_y_norm) + grab_h_pixels), grab_col, style.ScrollbarRounding);
  3758. }
  3759. // Moving window to front of display (which happens to be back of our sorted list)
  3760. static void FocusWindow(ImGuiWindow* window)
  3761. {
  3762. ImGuiState& g = *GImGui;
  3763. // Always mark the window we passed as focused. This is used for keyboard interactions such as tabbing.
  3764. g.FocusedWindow = window;
  3765. // Passing NULL allow to disable keyboard focus
  3766. if (!window)
  3767. return;
  3768. // And move its root window to the top of the pile
  3769. if (window->RootWindow)
  3770. window = window->RootWindow;
  3771. // Steal focus on active widgets
  3772. if (window->Flags & ImGuiWindowFlags_Popup) // FIXME: This statement should be unnecessary. Need further testing before removing it..
  3773. if (g.ActiveId != 0 && g.ActiveIdWindow && g.ActiveIdWindow->RootWindow != window)
  3774. SetActiveId(0);
  3775. if (g.Windows.back() == window)
  3776. return;
  3777. for (int i = 0; i < g.Windows.Size; i++)
  3778. if (g.Windows[i] == window)
  3779. {
  3780. g.Windows.erase(g.Windows.begin() + i);
  3781. break;
  3782. }
  3783. g.Windows.push_back(window);
  3784. }
  3785. void ImGui::PushItemWidth(float item_width)
  3786. {
  3787. ImGuiWindow* window = GetCurrentWindow();
  3788. window->DC.ItemWidth = (item_width == 0.0f ? window->ItemWidthDefault : item_width);
  3789. window->DC.ItemWidthStack.push_back(window->DC.ItemWidth);
  3790. }
  3791. static void PushMultiItemsWidths(int components, float w_full = 0.0f)
  3792. {
  3793. ImGuiWindow* window = GetCurrentWindow();
  3794. const ImGuiStyle& style = GImGui->Style;
  3795. if (w_full <= 0.0f)
  3796. w_full = ImGui::CalcItemWidth();
  3797. const float w_item_one = ImMax(1.0f, (float)(int)((w_full - (style.FramePadding.x*2.0f + style.ItemInnerSpacing.x) * (components-1)) / (float)components));
  3798. const float w_item_last = ImMax(1.0f, (float)(int)(w_full - (w_item_one + style.FramePadding.x*2.0f + style.ItemInnerSpacing.x) * (components-1)));
  3799. window->DC.ItemWidthStack.push_back(w_item_last);
  3800. for (int i = 0; i < components-1; i++)
  3801. window->DC.ItemWidthStack.push_back(w_item_one);
  3802. window->DC.ItemWidth = window->DC.ItemWidthStack.back();
  3803. }
  3804. void ImGui::PopItemWidth()
  3805. {
  3806. ImGuiWindow* window = GetCurrentWindow();
  3807. window->DC.ItemWidthStack.pop_back();
  3808. window->DC.ItemWidth = window->DC.ItemWidthStack.empty() ? window->ItemWidthDefault : window->DC.ItemWidthStack.back();
  3809. }
  3810. float ImGui::CalcItemWidth()
  3811. {
  3812. ImGuiWindow* window = GetCurrentWindow();
  3813. float w = window->DC.ItemWidth;
  3814. if (w < 0.0f)
  3815. {
  3816. // Align to a right-side limit. We include 1 frame padding in the calculation because this is how the width is always used (we add 2 frame padding to it), but we could move that responsibility to the widget as well.
  3817. ImGuiState& g = *GImGui;
  3818. float width_to_right_edge = window->Pos.x + ImGui::GetContentRegionMax().x - window->DC.CursorPos.x;
  3819. w = ImMax(1.0f, width_to_right_edge + w - g.Style.FramePadding.x * 2.0f);
  3820. }
  3821. w = (float)(int)w;
  3822. return w;
  3823. }
  3824. static void SetFont(ImFont* font)
  3825. {
  3826. ImGuiState& g = *GImGui;
  3827. IM_ASSERT(font && font->IsLoaded());
  3828. IM_ASSERT(font->Scale > 0.0f);
  3829. g.Font = font;
  3830. g.FontBaseSize = g.IO.FontGlobalScale * g.Font->FontSize * g.Font->Scale;
  3831. g.FontSize = g.CurrentWindow ? g.CurrentWindow->CalcFontSize() : 0.0f;
  3832. g.FontTexUvWhitePixel = g.Font->ContainerAtlas->TexUvWhitePixel;
  3833. }
  3834. void ImGui::PushFont(ImFont* font)
  3835. {
  3836. ImGuiState& g = *GImGui;
  3837. if (!font)
  3838. font = g.IO.Fonts->Fonts[0];
  3839. SetFont(font);
  3840. g.FontStack.push_back(font);
  3841. g.CurrentWindow->DrawList->PushTextureID(font->ContainerAtlas->TexID);
  3842. }
  3843. void ImGui::PopFont()
  3844. {
  3845. ImGuiState& g = *GImGui;
  3846. g.CurrentWindow->DrawList->PopTextureID();
  3847. g.FontStack.pop_back();
  3848. SetFont(g.FontStack.empty() ? g.IO.Fonts->Fonts[0] : g.FontStack.back());
  3849. }
  3850. void ImGui::PushAllowKeyboardFocus(bool allow_keyboard_focus)
  3851. {
  3852. ImGuiWindow* window = GetCurrentWindow();
  3853. window->DC.AllowKeyboardFocus = allow_keyboard_focus;
  3854. window->DC.AllowKeyboardFocusStack.push_back(allow_keyboard_focus);
  3855. }
  3856. void ImGui::PopAllowKeyboardFocus()
  3857. {
  3858. ImGuiWindow* window = GetCurrentWindow();
  3859. window->DC.AllowKeyboardFocusStack.pop_back();
  3860. window->DC.AllowKeyboardFocus = window->DC.AllowKeyboardFocusStack.empty() ? true : window->DC.AllowKeyboardFocusStack.back();
  3861. }
  3862. void ImGui::PushButtonRepeat(bool repeat)
  3863. {
  3864. ImGuiWindow* window = GetCurrentWindow();
  3865. window->DC.ButtonRepeat = repeat;
  3866. window->DC.ButtonRepeatStack.push_back(repeat);
  3867. }
  3868. void ImGui::PopButtonRepeat()
  3869. {
  3870. ImGuiWindow* window = GetCurrentWindow();
  3871. window->DC.ButtonRepeatStack.pop_back();
  3872. window->DC.ButtonRepeat = window->DC.ButtonRepeatStack.empty() ? false : window->DC.ButtonRepeatStack.back();
  3873. }
  3874. void ImGui::PushTextWrapPos(float wrap_pos_x)
  3875. {
  3876. ImGuiWindow* window = GetCurrentWindow();
  3877. window->DC.TextWrapPos = wrap_pos_x;
  3878. window->DC.TextWrapPosStack.push_back(wrap_pos_x);
  3879. }
  3880. void ImGui::PopTextWrapPos()
  3881. {
  3882. ImGuiWindow* window = GetCurrentWindow();
  3883. window->DC.TextWrapPosStack.pop_back();
  3884. window->DC.TextWrapPos = window->DC.TextWrapPosStack.empty() ? -1.0f : window->DC.TextWrapPosStack.back();
  3885. }
  3886. void ImGui::PushStyleColor(ImGuiCol idx, const ImVec4& col)
  3887. {
  3888. ImGuiState& g = *GImGui;
  3889. ImGuiColMod backup;
  3890. backup.Col = idx;
  3891. backup.PreviousValue = g.Style.Colors[idx];
  3892. g.ColorModifiers.push_back(backup);
  3893. g.Style.Colors[idx] = col;
  3894. }
  3895. void ImGui::PopStyleColor(int count)
  3896. {
  3897. ImGuiState& g = *GImGui;
  3898. while (count > 0)
  3899. {
  3900. ImGuiColMod& backup = g.ColorModifiers.back();
  3901. g.Style.Colors[backup.Col] = backup.PreviousValue;
  3902. g.ColorModifiers.pop_back();
  3903. count--;
  3904. }
  3905. }
  3906. static float* GetStyleVarFloatAddr(ImGuiStyleVar idx)
  3907. {
  3908. ImGuiState& g = *GImGui;
  3909. switch (idx)
  3910. {
  3911. case ImGuiStyleVar_Alpha: return &g.Style.Alpha;
  3912. case ImGuiStyleVar_WindowRounding: return &g.Style.WindowRounding;
  3913. case ImGuiStyleVar_ChildWindowRounding: return &g.Style.ChildWindowRounding;
  3914. case ImGuiStyleVar_FrameRounding: return &g.Style.FrameRounding;
  3915. case ImGuiStyleVar_IndentSpacing: return &g.Style.IndentSpacing;
  3916. case ImGuiStyleVar_GrabMinSize: return &g.Style.GrabMinSize;
  3917. }
  3918. return NULL;
  3919. }
  3920. static ImVec2* GetStyleVarVec2Addr(ImGuiStyleVar idx)
  3921. {
  3922. ImGuiState& g = *GImGui;
  3923. switch (idx)
  3924. {
  3925. case ImGuiStyleVar_WindowPadding: return &g.Style.WindowPadding;
  3926. case ImGuiStyleVar_WindowMinSize: return &g.Style.WindowMinSize;
  3927. case ImGuiStyleVar_FramePadding: return &g.Style.FramePadding;
  3928. case ImGuiStyleVar_ItemSpacing: return &g.Style.ItemSpacing;
  3929. case ImGuiStyleVar_ItemInnerSpacing: return &g.Style.ItemInnerSpacing;
  3930. }
  3931. return NULL;
  3932. }
  3933. void ImGui::PushStyleVar(ImGuiStyleVar idx, float val)
  3934. {
  3935. ImGuiState& g = *GImGui;
  3936. float* pvar = GetStyleVarFloatAddr(idx);
  3937. IM_ASSERT(pvar != NULL); // Called function with wrong-type? Variable is not a float.
  3938. ImGuiStyleMod backup;
  3939. backup.Var = idx;
  3940. backup.PreviousValue = ImVec2(*pvar, 0.0f);
  3941. g.StyleModifiers.push_back(backup);
  3942. *pvar = val;
  3943. }
  3944. void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val)
  3945. {
  3946. ImGuiState& g = *GImGui;
  3947. ImVec2* pvar = GetStyleVarVec2Addr(idx);
  3948. IM_ASSERT(pvar != NULL); // Called function with wrong-type? Variable is not a ImVec2.
  3949. ImGuiStyleMod backup;
  3950. backup.Var = idx;
  3951. backup.PreviousValue = *pvar;
  3952. g.StyleModifiers.push_back(backup);
  3953. *pvar = val;
  3954. }
  3955. void ImGui::PopStyleVar(int count)
  3956. {
  3957. ImGuiState& g = *GImGui;
  3958. while (count > 0)
  3959. {
  3960. ImGuiStyleMod& backup = g.StyleModifiers.back();
  3961. if (float* pvar_f = GetStyleVarFloatAddr(backup.Var))
  3962. *pvar_f = backup.PreviousValue.x;
  3963. else if (ImVec2* pvar_v = GetStyleVarVec2Addr(backup.Var))
  3964. *pvar_v = backup.PreviousValue;
  3965. g.StyleModifiers.pop_back();
  3966. count--;
  3967. }
  3968. }
  3969. const char* ImGui::GetStyleColName(ImGuiCol idx)
  3970. {
  3971. // Create switch-case from enum with regexp: ImGuiCol_{.*}, --> case ImGuiCol_\1: return "\1";
  3972. switch (idx)
  3973. {
  3974. case ImGuiCol_Text: return "Text";
  3975. case ImGuiCol_TextDisabled: return "TextDisabled";
  3976. case ImGuiCol_WindowBg: return "WindowBg";
  3977. case ImGuiCol_ChildWindowBg: return "ChildWindowBg";
  3978. case ImGuiCol_Border: return "Border";
  3979. case ImGuiCol_BorderShadow: return "BorderShadow";
  3980. case ImGuiCol_FrameBg: return "FrameBg";
  3981. case ImGuiCol_FrameBgHovered: return "FrameBgHovered";
  3982. case ImGuiCol_FrameBgActive: return "FrameBgActive";
  3983. case ImGuiCol_TitleBg: return "TitleBg";
  3984. case ImGuiCol_TitleBgCollapsed: return "TitleBgCollapsed";
  3985. case ImGuiCol_TitleBgActive: return "TitleBgActive";
  3986. case ImGuiCol_MenuBarBg: return "MenuBarBg";
  3987. case ImGuiCol_ScrollbarBg: return "ScrollbarBg";
  3988. case ImGuiCol_ScrollbarGrab: return "ScrollbarGrab";
  3989. case ImGuiCol_ScrollbarGrabHovered: return "ScrollbarGrabHovered";
  3990. case ImGuiCol_ScrollbarGrabActive: return "ScrollbarGrabActive";
  3991. case ImGuiCol_ComboBg: return "ComboBg";
  3992. case ImGuiCol_CheckMark: return "CheckMark";
  3993. case ImGuiCol_SliderGrab: return "SliderGrab";
  3994. case ImGuiCol_SliderGrabActive: return "SliderGrabActive";
  3995. case ImGuiCol_Button: return "Button";
  3996. case ImGuiCol_ButtonHovered: return "ButtonHovered";
  3997. case ImGuiCol_ButtonActive: return "ButtonActive";
  3998. case ImGuiCol_Header: return "Header";
  3999. case ImGuiCol_HeaderHovered: return "HeaderHovered";
  4000. case ImGuiCol_HeaderActive: return "HeaderActive";
  4001. case ImGuiCol_Column: return "Column";
  4002. case ImGuiCol_ColumnHovered: return "ColumnHovered";
  4003. case ImGuiCol_ColumnActive: return "ColumnActive";
  4004. case ImGuiCol_ResizeGrip: return "ResizeGrip";
  4005. case ImGuiCol_ResizeGripHovered: return "ResizeGripHovered";
  4006. case ImGuiCol_ResizeGripActive: return "ResizeGripActive";
  4007. case ImGuiCol_CloseButton: return "CloseButton";
  4008. case ImGuiCol_CloseButtonHovered: return "CloseButtonHovered";
  4009. case ImGuiCol_CloseButtonActive: return "CloseButtonActive";
  4010. case ImGuiCol_PlotLines: return "PlotLines";
  4011. case ImGuiCol_PlotLinesHovered: return "PlotLinesHovered";
  4012. case ImGuiCol_PlotHistogram: return "PlotHistogram";
  4013. case ImGuiCol_PlotHistogramHovered: return "PlotHistogramHovered";
  4014. case ImGuiCol_TextSelectedBg: return "TextSelectedBg";
  4015. case ImGuiCol_TooltipBg: return "TooltipBg";
  4016. case ImGuiCol_ModalWindowDarkening: return "ModalWindowDarkening";
  4017. }
  4018. IM_ASSERT(0);
  4019. return "Unknown";
  4020. }
  4021. bool ImGui::IsWindowHovered()
  4022. {
  4023. ImGuiState& g = *GImGui;
  4024. return g.HoveredWindow == g.CurrentWindow;
  4025. }
  4026. bool ImGui::IsWindowFocused()
  4027. {
  4028. ImGuiState& g = *GImGui;
  4029. return g.FocusedWindow == g.CurrentWindow;
  4030. }
  4031. bool ImGui::IsRootWindowFocused()
  4032. {
  4033. ImGuiState& g = *GImGui;
  4034. ImGuiWindow* root_window = g.CurrentWindow->RootWindow;
  4035. return g.FocusedWindow == root_window;
  4036. }
  4037. bool ImGui::IsRootWindowOrAnyChildFocused()
  4038. {
  4039. ImGuiState& g = *GImGui;
  4040. ImGuiWindow* root_window = g.CurrentWindow->RootWindow;
  4041. return g.FocusedWindow && g.FocusedWindow->RootWindow == root_window;
  4042. }
  4043. float ImGui::GetWindowWidth()
  4044. {
  4045. ImGuiWindow* window = GImGui->CurrentWindow;
  4046. return window->Size.x;
  4047. }
  4048. ImVec2 ImGui::GetWindowPos()
  4049. {
  4050. ImGuiState& g = *GImGui;
  4051. ImGuiWindow* window = g.CurrentWindow;
  4052. return window->Pos;
  4053. }
  4054. static void SetWindowScrollY(ImGuiWindow* window, float new_scroll_y)
  4055. {
  4056. window->DC.CursorMaxPos.y += window->ScrollY;
  4057. window->ScrollY = new_scroll_y;
  4058. window->DC.CursorMaxPos.y -= window->ScrollY;
  4059. }
  4060. static void SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiSetCond cond)
  4061. {
  4062. // Test condition (NB: bit 0 is always true) and clear flags for next time
  4063. if (cond && (window->SetWindowPosAllowFlags & cond) == 0)
  4064. return;
  4065. window->SetWindowPosAllowFlags &= ~(ImGuiSetCond_Once | ImGuiSetCond_FirstUseEver | ImGuiSetCond_Appearing);
  4066. window->SetWindowPosCenterWanted = false;
  4067. // Set
  4068. const ImVec2 old_pos = window->Pos;
  4069. window->PosFloat = pos;
  4070. window->Pos = ImVec2((float)(int)window->PosFloat.x, (float)(int)window->PosFloat.y);
  4071. window->DC.CursorPos += (window->Pos - old_pos); // As we happen to move the window while it is being appended to (which is a bad idea - will smear) let's at least offset the cursor
  4072. window->DC.CursorMaxPos += (window->Pos - old_pos); // And more importantly we need to adjust this so size calculation doesn't get affected.
  4073. }
  4074. void ImGui::SetWindowPos(const ImVec2& pos, ImGuiSetCond cond)
  4075. {
  4076. ImGuiWindow* window = GetCurrentWindow();
  4077. SetWindowPos(window, pos, cond);
  4078. }
  4079. void ImGui::SetWindowPos(const char* name, const ImVec2& pos, ImGuiSetCond cond)
  4080. {
  4081. ImGuiWindow* window = FindWindowByName(name);
  4082. if (window)
  4083. SetWindowPos(window, pos, cond);
  4084. }
  4085. ImVec2 ImGui::GetWindowSize()
  4086. {
  4087. ImGuiWindow* window = GetCurrentWindow();
  4088. return window->Size;
  4089. }
  4090. static void SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiSetCond cond)
  4091. {
  4092. // Test condition (NB: bit 0 is always true) and clear flags for next time
  4093. if (cond && (window->SetWindowSizeAllowFlags & cond) == 0)
  4094. return;
  4095. window->SetWindowSizeAllowFlags &= ~(ImGuiSetCond_Once | ImGuiSetCond_FirstUseEver | ImGuiSetCond_Appearing);
  4096. // Set
  4097. if (size.x > 0.0f)
  4098. {
  4099. window->AutoFitFramesX = 0;
  4100. window->SizeFull.x = size.x;
  4101. }
  4102. else
  4103. {
  4104. window->AutoFitFramesX = 2;
  4105. window->AutoFitOnlyGrows = false;
  4106. }
  4107. if (size.y > 0.0f)
  4108. {
  4109. window->AutoFitFramesY = 0;
  4110. window->SizeFull.y = size.y;
  4111. }
  4112. else
  4113. {
  4114. window->AutoFitFramesY = 2;
  4115. window->AutoFitOnlyGrows = false;
  4116. }
  4117. }
  4118. void ImGui::SetWindowSize(const ImVec2& size, ImGuiSetCond cond)
  4119. {
  4120. ImGuiWindow* window = GetCurrentWindow();
  4121. SetWindowSize(window, size, cond);
  4122. }
  4123. void ImGui::SetWindowSize(const char* name, const ImVec2& size, ImGuiSetCond cond)
  4124. {
  4125. ImGuiWindow* window = FindWindowByName(name);
  4126. if (window)
  4127. SetWindowSize(window, size, cond);
  4128. }
  4129. static void SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiSetCond cond)
  4130. {
  4131. // Test condition (NB: bit 0 is always true) and clear flags for next time
  4132. if (cond && (window->SetWindowCollapsedAllowFlags & cond) == 0)
  4133. return;
  4134. window->SetWindowCollapsedAllowFlags &= ~(ImGuiSetCond_Once | ImGuiSetCond_FirstUseEver | ImGuiSetCond_Appearing);
  4135. // Set
  4136. window->Collapsed = collapsed;
  4137. }
  4138. void ImGui::SetWindowCollapsed(bool collapsed, ImGuiSetCond cond)
  4139. {
  4140. ImGuiWindow* window = GetCurrentWindow();
  4141. SetWindowCollapsed(window, collapsed, cond);
  4142. }
  4143. bool ImGui::IsWindowCollapsed()
  4144. {
  4145. return GImGui->CurrentWindow->Collapsed;
  4146. }
  4147. void ImGui::SetWindowCollapsed(const char* name, bool collapsed, ImGuiSetCond cond)
  4148. {
  4149. ImGuiWindow* window = FindWindowByName(name);
  4150. if (window)
  4151. SetWindowCollapsed(window, collapsed, cond);
  4152. }
  4153. void ImGui::SetWindowFocus()
  4154. {
  4155. ImGuiWindow* window = GetCurrentWindow();
  4156. FocusWindow(window);
  4157. }
  4158. void ImGui::SetWindowFocus(const char* name)
  4159. {
  4160. if (name)
  4161. {
  4162. ImGuiWindow* window = FindWindowByName(name);
  4163. if (window)
  4164. FocusWindow(window);
  4165. }
  4166. else
  4167. {
  4168. FocusWindow(NULL);
  4169. }
  4170. }
  4171. void ImGui::SetNextWindowPos(const ImVec2& pos, ImGuiSetCond cond)
  4172. {
  4173. ImGuiState& g = *GImGui;
  4174. g.SetNextWindowPosVal = pos;
  4175. g.SetNextWindowPosCond = cond ? cond : ImGuiSetCond_Always;
  4176. }
  4177. void ImGui::SetNextWindowPosCenter(ImGuiSetCond cond)
  4178. {
  4179. ImGuiState& g = *GImGui;
  4180. g.SetNextWindowPosVal = ImVec2(-FLT_MAX, -FLT_MAX);
  4181. g.SetNextWindowPosCond = cond ? cond : ImGuiSetCond_Always;
  4182. }
  4183. void ImGui::SetNextWindowSize(const ImVec2& size, ImGuiSetCond cond)
  4184. {
  4185. ImGuiState& g = *GImGui;
  4186. g.SetNextWindowSizeVal = size;
  4187. g.SetNextWindowSizeCond = cond ? cond : ImGuiSetCond_Always;
  4188. }
  4189. void ImGui::SetNextWindowCollapsed(bool collapsed, ImGuiSetCond cond)
  4190. {
  4191. ImGuiState& g = *GImGui;
  4192. g.SetNextWindowCollapsedVal = collapsed;
  4193. g.SetNextWindowCollapsedCond = cond ? cond : ImGuiSetCond_Always;
  4194. }
  4195. void ImGui::SetNextWindowFocus()
  4196. {
  4197. ImGuiState& g = *GImGui;
  4198. g.SetNextWindowFocus = true;
  4199. }
  4200. ImVec2 ImGui::GetContentRegionMax()
  4201. {
  4202. ImGuiWindow* window = GetCurrentWindow();
  4203. ImVec2 mx = window->Size - window->WindowPadding;
  4204. if (window->DC.ColumnsCount != 1)
  4205. mx.x = ImGui::GetColumnOffset(window->DC.ColumnsCurrent + 1) - window->WindowPadding.x;
  4206. else
  4207. mx.x -= window->ScrollbarWidth();
  4208. return mx;
  4209. }
  4210. ImVec2 ImGui::GetWindowContentRegionMin()
  4211. {
  4212. ImGuiWindow* window = GetCurrentWindow();
  4213. return ImVec2(0, window->TitleBarHeight() + window->MenuBarHeight()) + window->WindowPadding;
  4214. }
  4215. ImVec2 ImGui::GetWindowContentRegionMax()
  4216. {
  4217. ImGuiWindow* window = GetCurrentWindow();
  4218. ImVec2 m = window->Size - window->WindowPadding;
  4219. m.x -= window->ScrollbarWidth();
  4220. return m;
  4221. }
  4222. float ImGui::GetTextLineHeight()
  4223. {
  4224. ImGuiState& g = *GImGui;
  4225. return g.FontSize;
  4226. }
  4227. float ImGui::GetTextLineHeightWithSpacing()
  4228. {
  4229. ImGuiState& g = *GImGui;
  4230. return g.FontSize + g.Style.ItemSpacing.y;
  4231. }
  4232. float ImGui::GetItemsLineHeightWithSpacing()
  4233. {
  4234. ImGuiState& g = *GImGui;
  4235. return g.FontSize + g.Style.FramePadding.y * 2.0f + g.Style.ItemSpacing.y;
  4236. }
  4237. ImDrawList* ImGui::GetWindowDrawList()
  4238. {
  4239. ImGuiWindow* window = GetCurrentWindow();
  4240. return window->DrawList;
  4241. }
  4242. ImFont* ImGui::GetWindowFont()
  4243. {
  4244. ImGuiState& g = *GImGui;
  4245. return g.Font;
  4246. }
  4247. float ImGui::GetWindowFontSize()
  4248. {
  4249. ImGuiState& g = *GImGui;
  4250. return g.FontSize;
  4251. }
  4252. void ImGui::SetWindowFontScale(float scale)
  4253. {
  4254. ImGuiState& g = *GImGui;
  4255. ImGuiWindow* window = GetCurrentWindow();
  4256. window->FontWindowScale = scale;
  4257. g.FontSize = window->CalcFontSize();
  4258. }
  4259. // User generally sees positions in window coordinates. Internally we store CursorPos in absolute screen coordinates because it is more convenient.
  4260. // Conversion happens as we pass the value to user, but it makes our naming convention confusing because GetCursorPos() == (DC.CursorPos - window.Pos). May want to rename 'DC.CursorPos'.
  4261. ImVec2 ImGui::GetCursorPos()
  4262. {
  4263. ImGuiWindow* window = GetCurrentWindow();
  4264. return window->DC.CursorPos - window->Pos;
  4265. }
  4266. float ImGui::GetCursorPosX()
  4267. {
  4268. return ImGui::GetCursorPos().x;
  4269. }
  4270. float ImGui::GetCursorPosY()
  4271. {
  4272. return ImGui::GetCursorPos().y;
  4273. }
  4274. void ImGui::SetCursorPos(const ImVec2& pos)
  4275. {
  4276. ImGuiWindow* window = GetCurrentWindow();
  4277. window->DC.CursorPos = window->Pos + pos;
  4278. window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos);
  4279. }
  4280. void ImGui::SetCursorPosX(float x)
  4281. {
  4282. ImGuiWindow* window = GetCurrentWindow();
  4283. window->DC.CursorPos.x = window->Pos.x + x;
  4284. window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPos.x);
  4285. }
  4286. void ImGui::SetCursorPosY(float y)
  4287. {
  4288. ImGuiWindow* window = GetCurrentWindow();
  4289. window->DC.CursorPos.y = window->Pos.y + y;
  4290. window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y);
  4291. }
  4292. ImVec2 ImGui::GetCursorStartPos()
  4293. {
  4294. ImGuiWindow* window = GetCurrentWindow();
  4295. return window->DC.CursorStartPos - window->Pos;
  4296. }
  4297. ImVec2 ImGui::GetCursorScreenPos()
  4298. {
  4299. ImGuiWindow* window = GetCurrentWindow();
  4300. return window->DC.CursorPos;
  4301. }
  4302. void ImGui::SetCursorScreenPos(const ImVec2& screen_pos)
  4303. {
  4304. ImGuiWindow* window = GetCurrentWindow();
  4305. window->DC.CursorPos = screen_pos;
  4306. }
  4307. float ImGui::GetScrollY()
  4308. {
  4309. ImGuiWindow* window = GetCurrentWindow();
  4310. return window->ScrollY;
  4311. }
  4312. float ImGui::GetScrollMaxY()
  4313. {
  4314. ImGuiWindow* window = GetCurrentWindow();
  4315. return window->SizeContents.y - window->SizeFull.y;
  4316. }
  4317. void ImGui::SetScrollY(float scroll_y)
  4318. {
  4319. ImGuiWindow* window = GetCurrentWindow();
  4320. window->ScrollTargetRelY = scroll_y + window->TitleBarHeight(); // title bar height cancelled out when using ScrollTargetRelY
  4321. window->ScrollTargetCenterRatioY = 0.0f;
  4322. }
  4323. void ImGui::SetScrollFromPosY(float pos_y, float center_y_ratio)
  4324. {
  4325. // We store a target position so centering can occur on the next frame when we are guaranteed to have a known window size
  4326. ImGuiWindow* window = GetCurrentWindow();
  4327. IM_ASSERT(center_y_ratio >= 0.0f && center_y_ratio <= 1.0f);
  4328. window->ScrollTargetRelY = (float)(int)(pos_y + window->ScrollY);
  4329. if (center_y_ratio <= 0.0f && window->ScrollTargetRelY <= window->WindowPadding.y) // Minor hack to make "scroll to top" take account of WindowPadding, else it would scroll to (WindowPadding.y - ItemSpacing.y)
  4330. window->ScrollTargetRelY = 0.0f;
  4331. window->ScrollTargetCenterRatioY = center_y_ratio;
  4332. }
  4333. void ImGui::SetScrollHere(float center_y_ratio)
  4334. {
  4335. ImGuiWindow* window = GetCurrentWindow();
  4336. float target_y = window->DC.CursorPosPrevLine.y + (window->DC.PrevLineHeight * center_y_ratio) + (GImGui->Style.ItemSpacing.y * (center_y_ratio - 0.5f) * 2.0f); // Precisely aim above, in the middle or below the last line.
  4337. ImGui::SetScrollFromPosY(target_y - window->Pos.y, center_y_ratio);
  4338. }
  4339. void ImGui::SetKeyboardFocusHere(int offset)
  4340. {
  4341. ImGuiWindow* window = GetCurrentWindow();
  4342. window->FocusIdxAllRequestNext = window->FocusIdxAllCounter + 1 + offset;
  4343. window->FocusIdxTabRequestNext = IM_INT_MAX;
  4344. }
  4345. void ImGui::SetStateStorage(ImGuiStorage* tree)
  4346. {
  4347. ImGuiWindow* window = GetCurrentWindow();
  4348. window->DC.StateStorage = tree ? tree : &window->StateStorage;
  4349. }
  4350. ImGuiStorage* ImGui::GetStateStorage()
  4351. {
  4352. ImGuiWindow* window = GetCurrentWindow();
  4353. return window->DC.StateStorage;
  4354. }
  4355. void ImGui::TextV(const char* fmt, va_list args)
  4356. {
  4357. ImGuiState& g = *GImGui;
  4358. ImGuiWindow* window = GetCurrentWindow();
  4359. if (window->SkipItems)
  4360. return;
  4361. const char* text_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args);
  4362. TextUnformatted(g.TempBuffer, text_end);
  4363. }
  4364. void ImGui::Text(const char* fmt, ...)
  4365. {
  4366. va_list args;
  4367. va_start(args, fmt);
  4368. TextV(fmt, args);
  4369. va_end(args);
  4370. }
  4371. void ImGui::TextColoredV(const ImVec4& col, const char* fmt, va_list args)
  4372. {
  4373. ImGui::PushStyleColor(ImGuiCol_Text, col);
  4374. TextV(fmt, args);
  4375. ImGui::PopStyleColor();
  4376. }
  4377. void ImGui::TextColored(const ImVec4& col, const char* fmt, ...)
  4378. {
  4379. va_list args;
  4380. va_start(args, fmt);
  4381. TextColoredV(col, fmt, args);
  4382. va_end(args);
  4383. }
  4384. void ImGui::TextDisabledV(const char* fmt, va_list args)
  4385. {
  4386. ImGui::PushStyleColor(ImGuiCol_Text, GImGui->Style.Colors[ImGuiCol_TextDisabled]);
  4387. TextV(fmt, args);
  4388. ImGui::PopStyleColor();
  4389. }
  4390. void ImGui::TextDisabled(const char* fmt, ...)
  4391. {
  4392. va_list args;
  4393. va_start(args, fmt);
  4394. TextDisabledV(fmt, args);
  4395. va_end(args);
  4396. }
  4397. void ImGui::TextWrappedV(const char* fmt, va_list args)
  4398. {
  4399. ImGui::PushTextWrapPos(0.0f);
  4400. TextV(fmt, args);
  4401. ImGui::PopTextWrapPos();
  4402. }
  4403. void ImGui::TextWrapped(const char* fmt, ...)
  4404. {
  4405. va_list args;
  4406. va_start(args, fmt);
  4407. TextWrappedV(fmt, args);
  4408. va_end(args);
  4409. }
  4410. void ImGui::TextUnformatted(const char* text, const char* text_end)
  4411. {
  4412. ImGuiState& g = *GImGui;
  4413. ImGuiWindow* window = GetCurrentWindow();
  4414. if (window->SkipItems)
  4415. return;
  4416. IM_ASSERT(text != NULL);
  4417. const char* text_begin = text;
  4418. if (text_end == NULL)
  4419. text_end = text + strlen(text); // FIXME-OPT
  4420. const float wrap_pos_x = window->DC.TextWrapPos;
  4421. const bool wrap_enabled = wrap_pos_x >= 0.0f;
  4422. if (text_end - text > 2000 && !wrap_enabled)
  4423. {
  4424. // Long text!
  4425. // Perform manual coarse clipping to optimize for long multi-line text
  4426. // From this point we will only compute the width of lines that are visible. Optimization only available when word-wrapping is disabled.
  4427. // We also don't vertically center the text within the line full height, which is unlikely to matter because we are likely the biggest and only item on the line.
  4428. const char* line = text;
  4429. const float line_height = ImGui::GetTextLineHeight();
  4430. const ImVec2 text_pos = window->DC.CursorPos + ImVec2(0.0f, window->DC.CurrentLineTextBaseOffset);
  4431. const ImVec4 clip_rect = window->ClipRect;
  4432. ImVec2 text_size(0,0);
  4433. if (text_pos.y <= clip_rect.w)
  4434. {
  4435. ImVec2 pos = text_pos;
  4436. // Lines to skip (can't skip when logging text)
  4437. if (!g.LogEnabled)
  4438. {
  4439. int lines_skippable = (int)((clip_rect.y - text_pos.y) / line_height);
  4440. if (lines_skippable > 0)
  4441. {
  4442. int lines_skipped = 0;
  4443. while (line < text_end && lines_skipped < lines_skippable)
  4444. {
  4445. const char* line_end = strchr(line, '\n');
  4446. line = line_end + 1;
  4447. lines_skipped++;
  4448. }
  4449. pos.y += lines_skipped * line_height;
  4450. }
  4451. }
  4452. // Lines to render
  4453. if (line < text_end)
  4454. {
  4455. ImRect line_rect(pos, pos + ImVec2(ImGui::GetWindowWidth(), line_height));
  4456. while (line < text_end)
  4457. {
  4458. const char* line_end = strchr(line, '\n');
  4459. if (IsClippedEx(line_rect, NULL, false))
  4460. break;
  4461. const ImVec2 line_size = CalcTextSize(line, line_end, false);
  4462. text_size.x = ImMax(text_size.x, line_size.x);
  4463. RenderText(pos, line, line_end, false);
  4464. if (!line_end)
  4465. line_end = text_end;
  4466. line = line_end + 1;
  4467. line_rect.Min.y += line_height;
  4468. line_rect.Max.y += line_height;
  4469. pos.y += line_height;
  4470. }
  4471. // Count remaining lines
  4472. int lines_skipped = 0;
  4473. while (line < text_end)
  4474. {
  4475. const char* line_end = strchr(line, '\n');
  4476. if (!line_end)
  4477. line_end = text_end;
  4478. line = line_end + 1;
  4479. lines_skipped++;
  4480. }
  4481. pos.y += lines_skipped * line_height;
  4482. }
  4483. text_size.y += (pos - text_pos).y;
  4484. }
  4485. ImRect bb(text_pos, text_pos + text_size);
  4486. ItemSize(bb);
  4487. ItemAdd(bb, NULL);
  4488. }
  4489. else
  4490. {
  4491. const float wrap_width = wrap_enabled ? CalcWrapWidthForPos(window->DC.CursorPos, wrap_pos_x) : 0.0f;
  4492. const ImVec2 text_size = CalcTextSize(text_begin, text_end, false, wrap_width);
  4493. // Account of baseline offset
  4494. ImVec2 text_pos = window->DC.CursorPos;
  4495. text_pos.y += window->DC.CurrentLineTextBaseOffset;
  4496. ImRect bb(text_pos, text_pos + text_size);
  4497. ItemSize(bb.GetSize());
  4498. if (!ItemAdd(bb, NULL))
  4499. return;
  4500. // Render (we don't hide text after ## in this end-user function)
  4501. RenderTextWrapped(bb.Min, text_begin, text_end, wrap_width);
  4502. }
  4503. }
  4504. void ImGui::AlignFirstTextHeightToWidgets()
  4505. {
  4506. ImGuiState& g = *GImGui;
  4507. ImGuiWindow* window = GetCurrentWindow();
  4508. if (window->SkipItems)
  4509. return;
  4510. // Declare a dummy item size to that upcoming items that are smaller will center-align on the newly expanded line height.
  4511. ItemSize(ImVec2(0, g.FontSize + g.Style.FramePadding.y*2), g.Style.FramePadding.y);
  4512. ImGui::SameLine(0, 0);
  4513. }
  4514. // Add a label+text combo aligned to other label+value widgets
  4515. void ImGui::LabelTextV(const char* label, const char* fmt, va_list args)
  4516. {
  4517. ImGuiState& g = *GImGui;
  4518. ImGuiWindow* window = GetCurrentWindow();
  4519. if (window->SkipItems)
  4520. return;
  4521. const ImGuiStyle& style = g.Style;
  4522. const float w = ImGui::CalcItemWidth();
  4523. const ImVec2 label_size = CalcTextSize(label, NULL, true);
  4524. const ImRect value_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w + style.FramePadding.x*2, label_size.y + style.FramePadding.y*2));
  4525. const ImRect total_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w + style.FramePadding.x*2 + (label_size.x > 0.0f ? style.ItemInnerSpacing.x : 0.0f), style.FramePadding.y*2) + label_size);
  4526. ItemSize(total_bb, style.FramePadding.y);
  4527. if (!ItemAdd(total_bb, NULL))
  4528. return;
  4529. // Render
  4530. const char* value_text_begin = &g.TempBuffer[0];
  4531. const char* value_text_end = value_text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args);
  4532. RenderTextClipped(value_bb.Min, value_bb.Max, value_text_begin, value_text_end, NULL, ImGuiAlign_VCenter);
  4533. RenderText(ImVec2(value_bb.Max.x + style.ItemInnerSpacing.x, value_bb.Min.y + style.FramePadding.y), label);
  4534. }
  4535. void ImGui::LabelText(const char* label, const char* fmt, ...)
  4536. {
  4537. va_list args;
  4538. va_start(args, fmt);
  4539. LabelTextV(label, fmt, args);
  4540. va_end(args);
  4541. }
  4542. static inline bool IsWindowContentHoverable(ImGuiWindow* window)
  4543. {
  4544. // An active popup disable hovering on other windows (apart from its own children)
  4545. ImGuiState& g = *GImGui;
  4546. if (ImGuiWindow* focused_window = g.FocusedWindow)
  4547. if (ImGuiWindow* focused_root_window = focused_window->RootWindow)
  4548. if ((focused_root_window->Flags & ImGuiWindowFlags_Popup) != 0 && focused_root_window->WasActive && focused_root_window != window->RootWindow)
  4549. return false;
  4550. return true;
  4551. }
  4552. static bool IsHovered(const ImRect& bb, ImGuiID id, bool flatten_childs = false)
  4553. {
  4554. ImGuiState& g = *GImGui;
  4555. if (g.HoveredId == 0 || g.HoveredId == id)
  4556. {
  4557. ImGuiWindow* window = GetCurrentWindow();
  4558. if (g.HoveredWindow == window || (flatten_childs && g.HoveredRootWindow == window->RootWindow))
  4559. if ((g.ActiveId == 0 || g.ActiveId == id || g.ActiveIdIsFocusedOnly) && IsMouseHoveringRect(bb))
  4560. if (IsWindowContentHoverable(g.HoveredRootWindow))
  4561. return true;
  4562. }
  4563. return false;
  4564. }
  4565. static bool ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, bool allow_key_modifiers, ImGuiButtonFlags flags)
  4566. {
  4567. ImGuiState& g = *GImGui;
  4568. ImGuiWindow* window = GetCurrentWindow();
  4569. if (flags & ImGuiButtonFlags_Disabled)
  4570. {
  4571. if (out_hovered) *out_hovered = false;
  4572. if (out_held) *out_held = false;
  4573. if (g.ActiveId == id) SetActiveId(0);
  4574. return false;
  4575. }
  4576. bool pressed = false;
  4577. const bool hovered = IsHovered(bb, id, (flags & ImGuiButtonFlags_FlattenChilds) != 0);
  4578. if (hovered)
  4579. {
  4580. g.HoveredId = id;
  4581. if (allow_key_modifiers || (!g.IO.KeyCtrl && !g.IO.KeyShift && !g.IO.KeyAlt))
  4582. {
  4583. if (g.IO.MouseClicked[0])
  4584. {
  4585. if (flags & ImGuiButtonFlags_PressedOnClick)
  4586. {
  4587. pressed = true;
  4588. SetActiveId(0);
  4589. }
  4590. else
  4591. {
  4592. SetActiveId(id, window);
  4593. }
  4594. FocusWindow(window);
  4595. }
  4596. else if (g.IO.MouseReleased[0] && (flags & ImGuiButtonFlags_PressedOnRelease))
  4597. {
  4598. pressed = true;
  4599. SetActiveId(0);
  4600. }
  4601. else if ((flags & ImGuiButtonFlags_Repeat) && g.ActiveId == id && ImGui::IsMouseClicked(0, true))
  4602. {
  4603. pressed = true;
  4604. }
  4605. }
  4606. }
  4607. bool held = false;
  4608. if (g.ActiveId == id)
  4609. {
  4610. if (g.IO.MouseDown[0])
  4611. {
  4612. held = true;
  4613. }
  4614. else
  4615. {
  4616. if (hovered)
  4617. pressed = true;
  4618. SetActiveId(0);
  4619. }
  4620. }
  4621. if (out_hovered) *out_hovered = hovered;
  4622. if (out_held) *out_held = held;
  4623. return pressed;
  4624. }
  4625. static inline ImVec2 CalcItemSize(ImVec2 size, float default_x, float default_y)
  4626. {
  4627. ImGuiState& g = *GImGui;
  4628. ImVec2 content_max;
  4629. if (size.x < 0.0f || size.y < 0.0f)
  4630. content_max = g.CurrentWindow->Pos + ImGui::GetContentRegionMax();
  4631. if (size.x <= 0.0f)
  4632. size.x = (size.x == 0.0f) ? default_x : ImMax(content_max.x - g.CurrentWindow->DC.CursorPos.x, 4.0f) + size.x;
  4633. if (size.y <= 0.0f)
  4634. size.y = (size.y == 0.0f) ? default_y : ImMax(content_max.y - g.CurrentWindow->DC.CursorPos.y, 4.0f) + size.y;
  4635. return size;
  4636. }
  4637. static bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0,0), ImGuiButtonFlags flags = 0)
  4638. {
  4639. ImGuiWindow* window = GetCurrentWindow();
  4640. if (window->SkipItems)
  4641. return false;
  4642. ImGuiState& g = *GImGui;
  4643. const ImGuiStyle& style = g.Style;
  4644. const ImGuiID id = window->GetID(label);
  4645. const ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true);
  4646. ImVec2 pos = window->DC.CursorPos;
  4647. if ((flags & ImGuiButtonFlags_AlignTextBaseLine) && style.FramePadding.y < window->DC.CurrentLineTextBaseOffset)
  4648. pos.y += window->DC.CurrentLineTextBaseOffset - style.FramePadding.y;
  4649. ImVec2 size = CalcItemSize(size_arg, label_size.x + style.FramePadding.x * 2.0f, label_size.y + style.FramePadding.y * 2.0f);
  4650. const ImRect bb(pos, pos + size);
  4651. ItemSize(bb, style.FramePadding.y);
  4652. if (!ItemAdd(bb, &id))
  4653. return false;
  4654. if (window->DC.ButtonRepeat) flags |= ImGuiButtonFlags_Repeat;
  4655. bool hovered, held;
  4656. bool pressed = ButtonBehavior(bb, id, &hovered, &held, true, flags);
  4657. // Render
  4658. const ImU32 col = window->Color((hovered && held) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button);
  4659. RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding);
  4660. RenderTextClipped(bb.Min, bb.Max, label, NULL, &label_size, ImGuiAlign_Center | ImGuiAlign_VCenter);
  4661. // Automatically close popups
  4662. //if (pressed && !(flags & ImGuiButtonFlags_DontClosePopups) && (window->Flags & ImGuiWindowFlags_Popup))
  4663. // ImGui::CloseCurrentPopup();
  4664. return pressed;
  4665. }
  4666. bool ImGui::Button(const char* label, const ImVec2& size_arg)
  4667. {
  4668. return ButtonEx(label, size_arg, 0);
  4669. }
  4670. // Small buttons fits within text without additional vertical spacing.
  4671. bool ImGui::SmallButton(const char* label)
  4672. {
  4673. ImGuiState& g = *GImGui;
  4674. float backup_padding_y = g.Style.FramePadding.y;
  4675. g.Style.FramePadding.y = 0.0f;
  4676. bool pressed = ButtonEx(label, ImVec2(0,0), ImGuiButtonFlags_AlignTextBaseLine);
  4677. g.Style.FramePadding.y = backup_padding_y;
  4678. return pressed;
  4679. }
  4680. // Tip: use ImGui::PushID()/PopID() to push indices or pointers in the ID stack.
  4681. // Then you can keep 'str_id' empty or the same for all your buttons (instead of creating a string based on a non-string id)
  4682. bool ImGui::InvisibleButton(const char* str_id, const ImVec2& size)
  4683. {
  4684. ImGuiWindow* window = GetCurrentWindow();
  4685. if (window->SkipItems)
  4686. return false;
  4687. const ImGuiID id = window->GetID(str_id);
  4688. const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size);
  4689. ItemSize(bb);
  4690. if (!ItemAdd(bb, &id))
  4691. return false;
  4692. bool hovered, held;
  4693. bool pressed = ButtonBehavior(bb, id, &hovered, &held, true);
  4694. return pressed;
  4695. }
  4696. // Upper-right button to close a window.
  4697. static bool CloseWindowButton(bool* p_opened)
  4698. {
  4699. ImGuiWindow* window = GetCurrentWindow();
  4700. const ImGuiID id = window->GetID("#CLOSE");
  4701. const float size = window->TitleBarHeight() - 4.0f;
  4702. const ImRect bb(window->Rect().GetTR() + ImVec2(-3.0f-size,2.0f), window->Rect().GetTR() + ImVec2(-3.0f,2.0f+size));
  4703. bool hovered, held;
  4704. bool pressed = ButtonBehavior(bb, id, &hovered, &held, true);
  4705. // Render
  4706. const ImU32 col = window->Color((held && hovered) ? ImGuiCol_CloseButtonActive : hovered ? ImGuiCol_CloseButtonHovered : ImGuiCol_CloseButton);
  4707. const ImVec2 center = bb.GetCenter();
  4708. window->DrawList->AddCircleFilled(center, ImMax(2.0f,size*0.5f), col, 16);
  4709. const float cross_extent = (size * 0.5f * 0.7071f) - 1.0f;
  4710. if (hovered)
  4711. {
  4712. window->DrawList->AddLine(center + ImVec2(+cross_extent,+cross_extent), center + ImVec2(-cross_extent,-cross_extent), window->Color(ImGuiCol_Text));
  4713. window->DrawList->AddLine(center + ImVec2(+cross_extent,-cross_extent), center + ImVec2(-cross_extent,+cross_extent), window->Color(ImGuiCol_Text));
  4714. }
  4715. if (p_opened != NULL && pressed)
  4716. *p_opened = !*p_opened;
  4717. return pressed;
  4718. }
  4719. void ImGui::Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col)
  4720. {
  4721. ImGuiWindow* window = GetCurrentWindow();
  4722. if (window->SkipItems)
  4723. return;
  4724. ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size);
  4725. if (border_col.w > 0.0f)
  4726. bb.Max += ImVec2(2,2);
  4727. ItemSize(bb);
  4728. if (!ItemAdd(bb, NULL))
  4729. return;
  4730. if (border_col.w > 0.0f)
  4731. {
  4732. window->DrawList->AddRect(bb.Min, bb.Max, window->Color(border_col), 0.0f);
  4733. window->DrawList->AddImage(user_texture_id, bb.Min+ImVec2(1,1), bb.Max-ImVec2(1,1), uv0, uv1, window->Color(tint_col));
  4734. }
  4735. else
  4736. {
  4737. window->DrawList->AddImage(user_texture_id, bb.Min, bb.Max, uv0, uv1, window->Color(tint_col));
  4738. }
  4739. }
  4740. // frame_padding < 0: uses FramePadding from style (default)
  4741. // frame_padding = 0: no framing
  4742. // frame_padding > 0: set framing size
  4743. // The color used are the button colors.
  4744. bool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, int frame_padding, const ImVec4& bg_col, const ImVec4& tint_col)
  4745. {
  4746. ImGuiState& g = *GImGui;
  4747. ImGuiWindow* window = GetCurrentWindow();
  4748. if (window->SkipItems)
  4749. return false;
  4750. const ImGuiStyle& style = g.Style;
  4751. // Default to using texture ID as ID. User can still push string/integer prefixes.
  4752. // We could hash the size/uv to create a unique ID but that would prevent the user from animating UV.
  4753. ImGui::PushID((void *)user_texture_id);
  4754. const ImGuiID id = window->GetID("#image");
  4755. ImGui::PopID();
  4756. const ImVec2 padding = (frame_padding >= 0) ? ImVec2((float)frame_padding, (float)frame_padding) : style.FramePadding;
  4757. const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size + padding*2);
  4758. const ImRect image_bb(window->DC.CursorPos + padding, window->DC.CursorPos + padding + size);
  4759. ItemSize(bb);
  4760. if (!ItemAdd(bb, &id))
  4761. return false;
  4762. bool hovered, held;
  4763. bool pressed = ButtonBehavior(bb, id, &hovered, &held, true);
  4764. // Render
  4765. const ImU32 col = window->Color((hovered && held) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button);
  4766. RenderFrame(bb.Min, bb.Max, col);
  4767. if (bg_col.w > 0.0f)
  4768. window->DrawList->AddRectFilled(image_bb.Min, image_bb.Max, window->Color(bg_col));
  4769. window->DrawList->AddImage(user_texture_id, image_bb.Min, image_bb.Max, uv0, uv1, window->Color(tint_col));
  4770. return pressed;
  4771. }
  4772. // Start logging ImGui output to TTY
  4773. void ImGui::LogToTTY(int max_depth)
  4774. {
  4775. ImGuiState& g = *GImGui;
  4776. ImGuiWindow* window = GetCurrentWindow();
  4777. if (g.LogEnabled)
  4778. return;
  4779. g.LogEnabled = true;
  4780. g.LogFile = stdout;
  4781. g.LogStartDepth = window->DC.TreeDepth;
  4782. if (max_depth >= 0)
  4783. g.LogAutoExpandMaxDepth = max_depth;
  4784. }
  4785. // Start logging ImGui output to given file
  4786. void ImGui::LogToFile(int max_depth, const char* filename)
  4787. {
  4788. ImGuiState& g = *GImGui;
  4789. ImGuiWindow* window = GetCurrentWindow();
  4790. if (g.LogEnabled)
  4791. return;
  4792. if (!filename)
  4793. {
  4794. filename = g.IO.LogFilename;
  4795. if (!filename)
  4796. return;
  4797. }
  4798. g.LogFile = fopen(filename, "ab");
  4799. if (!g.LogFile)
  4800. {
  4801. IM_ASSERT(g.LogFile != NULL); // Consider this an error
  4802. return;
  4803. }
  4804. g.LogEnabled = true;
  4805. g.LogStartDepth = window->DC.TreeDepth;
  4806. if (max_depth >= 0)
  4807. g.LogAutoExpandMaxDepth = max_depth;
  4808. }
  4809. // Start logging ImGui output to clipboard
  4810. void ImGui::LogToClipboard(int max_depth)
  4811. {
  4812. ImGuiWindow* window = GetCurrentWindow();
  4813. ImGuiState& g = *GImGui;
  4814. if (g.LogEnabled)
  4815. return;
  4816. g.LogEnabled = true;
  4817. g.LogFile = NULL;
  4818. g.LogStartDepth = window->DC.TreeDepth;
  4819. if (max_depth >= 0)
  4820. g.LogAutoExpandMaxDepth = max_depth;
  4821. }
  4822. void ImGui::LogFinish()
  4823. {
  4824. ImGuiState& g = *GImGui;
  4825. if (!g.LogEnabled)
  4826. return;
  4827. ImGui::LogText(IM_NEWLINE);
  4828. g.LogEnabled = false;
  4829. if (g.LogFile != NULL)
  4830. {
  4831. if (g.LogFile == stdout)
  4832. fflush(g.LogFile);
  4833. else
  4834. fclose(g.LogFile);
  4835. g.LogFile = NULL;
  4836. }
  4837. if (g.LogClipboard->size() > 1)
  4838. {
  4839. if (g.IO.SetClipboardTextFn)
  4840. g.IO.SetClipboardTextFn(g.LogClipboard->begin());
  4841. g.LogClipboard->clear();
  4842. }
  4843. }
  4844. // Helper to display logging buttons
  4845. void ImGui::LogButtons()
  4846. {
  4847. ImGuiState& g = *GImGui;
  4848. ImGui::PushID("LogButtons");
  4849. const bool log_to_tty = ImGui::Button("Log To TTY");
  4850. ImGui::SameLine();
  4851. const bool log_to_file = ImGui::Button("Log To File");
  4852. ImGui::SameLine();
  4853. const bool log_to_clipboard = ImGui::Button("Log To Clipboard");
  4854. ImGui::SameLine();
  4855. ImGui::PushItemWidth(80.0f);
  4856. ImGui::PushAllowKeyboardFocus(false);
  4857. ImGui::SliderInt("Depth", &g.LogAutoExpandMaxDepth, 0, 9, NULL);
  4858. ImGui::PopAllowKeyboardFocus();
  4859. ImGui::PopItemWidth();
  4860. ImGui::PopID();
  4861. // Start logging at the end of the function so that the buttons don't appear in the log
  4862. if (log_to_tty)
  4863. LogToTTY(g.LogAutoExpandMaxDepth);
  4864. if (log_to_file)
  4865. LogToFile(g.LogAutoExpandMaxDepth, g.IO.LogFilename);
  4866. if (log_to_clipboard)
  4867. LogToClipboard(g.LogAutoExpandMaxDepth);
  4868. }
  4869. bool ImGui::CollapsingHeader(const char* label, const char* str_id, bool display_frame, bool default_open)
  4870. {
  4871. ImGuiState& g = *GImGui;
  4872. ImGuiWindow* window = GetCurrentWindow();
  4873. if (window->SkipItems)
  4874. return false;
  4875. const ImGuiStyle& style = g.Style;
  4876. IM_ASSERT(str_id != NULL || label != NULL);
  4877. if (str_id == NULL)
  4878. str_id = label;
  4879. if (label == NULL)
  4880. label = str_id;
  4881. const ImGuiID id = window->GetID(str_id);
  4882. // We only write to the tree storage if the user clicks (or explicitely use SetNextTreeNode*** functions)
  4883. ImGuiStorage* storage = window->DC.StateStorage;
  4884. bool opened;
  4885. if (g.SetNextTreeNodeOpenedCond != 0)
  4886. {
  4887. if (g.SetNextTreeNodeOpenedCond & ImGuiSetCond_Always)
  4888. {
  4889. opened = g.SetNextTreeNodeOpenedVal;
  4890. storage->SetInt(id, opened);
  4891. }
  4892. else
  4893. {
  4894. // We treat ImGuiSetCondition_Once and ImGuiSetCondition_FirstUseEver the same because tree node state are not saved persistently.
  4895. const int stored_value = storage->GetInt(id, -1);
  4896. if (stored_value == -1)
  4897. {
  4898. opened = g.SetNextTreeNodeOpenedVal;
  4899. storage->SetInt(id, opened);
  4900. }
  4901. else
  4902. {
  4903. opened = stored_value != 0;
  4904. }
  4905. }
  4906. g.SetNextTreeNodeOpenedCond = 0;
  4907. }
  4908. else
  4909. {
  4910. opened = storage->GetInt(id, default_open) != 0;
  4911. }
  4912. // Framed header expand a little outside the default padding
  4913. const ImVec2 window_padding = window->WindowPadding;
  4914. const ImVec2 label_size = CalcTextSize(label, NULL, true);
  4915. const ImVec2 pos_min = window->DC.CursorPos;
  4916. const ImVec2 pos_max = window->Pos + GetContentRegionMax();
  4917. ImRect bb = ImRect(pos_min, ImVec2(pos_max.x, pos_min.y + label_size.y));
  4918. if (display_frame)
  4919. {
  4920. bb.Min.x -= (float)(int)(window_padding.x*0.5f) - 1;
  4921. bb.Max.x += (float)(int)(window_padding.x*0.5f) - 1;
  4922. bb.Max.y += style.FramePadding.y * 2;
  4923. }
  4924. // FIXME: we don't provide our width so that it doesn't get feed back into AutoFit. Should manage that better so we can still hover without extending ContentsSize
  4925. const ImRect text_bb(bb.Min, bb.Min + ImVec2(g.FontSize + style.FramePadding.x*2*2,0) + label_size);
  4926. ItemSize(ImVec2(text_bb.GetSize().x, bb.GetSize().y), display_frame ? style.FramePadding.y : 0.0f);
  4927. // When logging is enabled, if automatically expand tree nodes (but *NOT* collapsing headers.. seems like sensible behavior).
  4928. // NB- If we are above max depth we still allow manually opened nodes to be logged.
  4929. if (g.LogEnabled && !display_frame && window->DC.TreeDepth < g.LogAutoExpandMaxDepth)
  4930. opened = true;
  4931. if (!ItemAdd(bb, &id))
  4932. return opened;
  4933. bool hovered, held;
  4934. bool pressed = ButtonBehavior(display_frame ? bb : text_bb, id, &hovered, &held, false);
  4935. if (pressed)
  4936. {
  4937. opened = !opened;
  4938. storage->SetInt(id, opened);
  4939. }
  4940. // Render
  4941. const ImU32 col = window->Color((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header);
  4942. if (display_frame)
  4943. {
  4944. // Framed type
  4945. RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding);
  4946. RenderCollapseTriangle(bb.Min + style.FramePadding, opened, 1.0f, true);
  4947. if (g.LogEnabled)
  4948. {
  4949. // NB: '##' is normally used to hide text (as a library-wide feature), so we need to specify the text range to make sure the ## aren't stripped out here.
  4950. const char log_prefix[] = "\n##";
  4951. LogText(bb.Min + style.FramePadding, log_prefix, log_prefix+3);
  4952. }
  4953. RenderTextClipped(bb.Min + style.FramePadding + ImVec2(g.FontSize + style.FramePadding.x*2,0), bb.Max, label, NULL, &label_size);
  4954. if (g.LogEnabled)
  4955. {
  4956. const char log_suffix[] = "##";
  4957. LogText(bb.Min + style.FramePadding, log_suffix, log_suffix+2);
  4958. }
  4959. }
  4960. else
  4961. {
  4962. // Unframed typed for tree nodes
  4963. if ((held && hovered) || hovered)
  4964. RenderFrame(bb.Min, bb.Max, col, false);
  4965. RenderCollapseTriangle(bb.Min + ImVec2(style.FramePadding.x, g.FontSize*0.15f), opened, 0.70f, false);
  4966. if (g.LogEnabled)
  4967. LogText(bb.Min, ">");
  4968. RenderText(bb.Min + ImVec2(g.FontSize + style.FramePadding.x*2,0), label);
  4969. }
  4970. return opened;
  4971. }
  4972. void ImGui::Bullet()
  4973. {
  4974. ImGuiState& g = *GImGui;
  4975. ImGuiWindow* window = GetCurrentWindow();
  4976. if (window->SkipItems)
  4977. return;
  4978. const ImGuiStyle& style = g.Style;
  4979. const float line_height = g.FontSize;
  4980. const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(line_height, line_height));
  4981. ItemSize(bb);
  4982. if (!ItemAdd(bb, NULL))
  4983. return;
  4984. // Render
  4985. const float bullet_size = line_height*0.15f;
  4986. window->DrawList->AddCircleFilled(bb.Min + ImVec2(style.FramePadding.x + line_height*0.5f, line_height*0.5f), bullet_size, window->Color(ImGuiCol_Text));
  4987. // Stay on same line
  4988. ImGui::SameLine(0, -1);
  4989. }
  4990. // Text with a little bullet aligned to the typical tree node.
  4991. void ImGui::BulletTextV(const char* fmt, va_list args)
  4992. {
  4993. ImGuiState& g = *GImGui;
  4994. ImGuiWindow* window = GetCurrentWindow();
  4995. if (window->SkipItems)
  4996. return;
  4997. const char* text_begin = g.TempBuffer;
  4998. const char* text_end = text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args);
  4999. const ImGuiStyle& style = g.Style;
  5000. const float line_height = g.FontSize;
  5001. const ImVec2 label_size = CalcTextSize(text_begin, text_end, true);
  5002. const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(line_height + (label_size.x > 0.0f ? (style.FramePadding.x*2) : 0.0f),0) + label_size); // Empty text doesn't add padding
  5003. ItemSize(bb);
  5004. if (!ItemAdd(bb, NULL))
  5005. return;
  5006. // Render
  5007. const float bullet_size = line_height*0.15f;
  5008. window->DrawList->AddCircleFilled(bb.Min + ImVec2(style.FramePadding.x + line_height*0.5f, line_height*0.5f), bullet_size, window->Color(ImGuiCol_Text));
  5009. RenderText(bb.Min+ImVec2(g.FontSize + style.FramePadding.x*2,0), text_begin, text_end);
  5010. }
  5011. void ImGui::BulletText(const char* fmt, ...)
  5012. {
  5013. va_list args;
  5014. va_start(args, fmt);
  5015. BulletTextV(fmt, args);
  5016. va_end(args);
  5017. }
  5018. // If returning 'true' the node is open and the user is responsible for calling TreePop
  5019. bool ImGui::TreeNodeV(const char* str_id, const char* fmt, va_list args)
  5020. {
  5021. ImGuiState& g = *GImGui;
  5022. ImGuiWindow* window = GetCurrentWindow();
  5023. if (window->SkipItems)
  5024. return false;
  5025. ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args);
  5026. if (!str_id || !str_id[0])
  5027. str_id = fmt;
  5028. ImGui::PushID(str_id);
  5029. const bool opened = ImGui::CollapsingHeader(g.TempBuffer, "", false);
  5030. ImGui::PopID();
  5031. if (opened)
  5032. ImGui::TreePush(str_id);
  5033. return opened;
  5034. }
  5035. bool ImGui::TreeNode(const char* str_id, const char* fmt, ...)
  5036. {
  5037. va_list args;
  5038. va_start(args, fmt);
  5039. bool s = TreeNodeV(str_id, fmt, args);
  5040. va_end(args);
  5041. return s;
  5042. }
  5043. // If returning 'true' the node is open and the user is responsible for calling TreePop
  5044. bool ImGui::TreeNodeV(const void* ptr_id, const char* fmt, va_list args)
  5045. {
  5046. ImGuiState& g = *GImGui;
  5047. ImGuiWindow* window = GetCurrentWindow();
  5048. if (window->SkipItems)
  5049. return false;
  5050. ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args);
  5051. if (!ptr_id)
  5052. ptr_id = fmt;
  5053. ImGui::PushID(ptr_id);
  5054. const bool opened = ImGui::CollapsingHeader(g.TempBuffer, "", false);
  5055. ImGui::PopID();
  5056. if (opened)
  5057. ImGui::TreePush(ptr_id);
  5058. return opened;
  5059. }
  5060. bool ImGui::TreeNode(const void* ptr_id, const char* fmt, ...)
  5061. {
  5062. va_list args;
  5063. va_start(args, fmt);
  5064. bool s = TreeNodeV(ptr_id, fmt, args);
  5065. va_end(args);
  5066. return s;
  5067. }
  5068. bool ImGui::TreeNode(const char* str_label_id)
  5069. {
  5070. return TreeNode(str_label_id, "%s", str_label_id);
  5071. }
  5072. void ImGui::SetNextTreeNodeOpened(bool opened, ImGuiSetCond cond)
  5073. {
  5074. ImGuiState& g = *GImGui;
  5075. g.SetNextTreeNodeOpenedVal = opened;
  5076. g.SetNextTreeNodeOpenedCond = cond ? cond : ImGuiSetCond_Always;
  5077. }
  5078. void ImGui::PushID(const char* str_id)
  5079. {
  5080. ImGuiWindow* window = GetCurrentWindow();
  5081. window->IDStack.push_back(window->GetID(str_id));
  5082. }
  5083. void ImGui::PushID(const char* str_id_begin, const char* str_id_end)
  5084. {
  5085. ImGuiWindow* window = GetCurrentWindow();
  5086. window->IDStack.push_back(window->GetID(str_id_begin, str_id_end));
  5087. }
  5088. void ImGui::PushID(const void* ptr_id)
  5089. {
  5090. ImGuiWindow* window = GetCurrentWindow();
  5091. window->IDStack.push_back(window->GetID(ptr_id));
  5092. }
  5093. void ImGui::PushID(int int_id)
  5094. {
  5095. const void* ptr_id = (void*)(intptr_t)int_id;
  5096. ImGuiWindow* window = GetCurrentWindow();
  5097. window->IDStack.push_back(window->GetID(ptr_id));
  5098. }
  5099. void ImGui::PopID()
  5100. {
  5101. ImGuiWindow* window = GetCurrentWindow();
  5102. window->IDStack.pop_back();
  5103. }
  5104. ImGuiID ImGui::GetID(const char* str_id)
  5105. {
  5106. return GImGui->CurrentWindow->GetID(str_id);
  5107. }
  5108. ImGuiID ImGui::GetID(const char* str_id_begin, const char* str_id_end)
  5109. {
  5110. return GImGui->CurrentWindow->GetID(str_id_begin, str_id_end);
  5111. }
  5112. ImGuiID ImGui::GetID(const void* ptr_id)
  5113. {
  5114. return GImGui->CurrentWindow->GetID(ptr_id);
  5115. }
  5116. // User can input math operators (e.g. +100) to edit a numerical values.
  5117. // NB: only call right after InputText because we are using its InitialValue storage
  5118. static void InputTextApplyArithmeticOp(const char* buf, float *v)
  5119. {
  5120. while (ImCharIsSpace(*buf))
  5121. buf++;
  5122. // We don't support '-' op because it would conflict with inputing negative value.
  5123. // Instead you can use +-100 to subtract from an existing value
  5124. char op = buf[0];
  5125. if (op == '+' || op == '*' || op == '/')
  5126. {
  5127. buf++;
  5128. while (ImCharIsSpace(*buf))
  5129. buf++;
  5130. }
  5131. else
  5132. {
  5133. op = 0;
  5134. }
  5135. if (!buf[0])
  5136. return;
  5137. float ref_v = *v;
  5138. if (op)
  5139. if (sscanf(GImGui->InputTextState.InitialText.begin(), "%f", &ref_v) < 1)
  5140. return;
  5141. float op_v = 0.0f;
  5142. if (sscanf(buf, "%f", &op_v) < 1)
  5143. return;
  5144. if (op == '+')
  5145. *v = ref_v + op_v; // add (uses "+-" to substract)
  5146. else if (op == '*')
  5147. *v = ref_v * op_v; // multiply
  5148. else if (op == '/')
  5149. {
  5150. if (op_v == 0.0f) // divide
  5151. return;
  5152. *v = ref_v / op_v;
  5153. }
  5154. else
  5155. *v = op_v; // Constant
  5156. }
  5157. // Create text input in place of a slider (when CTRL+Clicking on slider)
  5158. static bool InputFloatReplaceWidget(const ImRect& aabb, const char* label, float* v, ImGuiID id, int decimal_precision)
  5159. {
  5160. ImGuiState& g = *GImGui;
  5161. ImGuiWindow* window = GetCurrentWindow();
  5162. char text_buf[64];
  5163. ImFormatString(text_buf, IM_ARRAYSIZE(text_buf), "%.*f", decimal_precision, *v);
  5164. SetActiveId(g.ScalarAsInputTextId, window);
  5165. g.HoveredId = 0;
  5166. // Our replacement widget will override the focus ID (registered previously to allow for a TAB focus to happen)
  5167. window->FocusItemUnregister();
  5168. bool value_changed = InputTextEx(label, text_buf, (int)IM_ARRAYSIZE(text_buf), aabb.GetSize() - g.Style.FramePadding*2.0f, ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_AutoSelectAll);
  5169. if (g.ScalarAsInputTextId == 0)
  5170. {
  5171. // First frame
  5172. IM_ASSERT(g.ActiveId == id); // InputText ID expected to match the Slider ID (else we'd need to store them both, which is also possible)
  5173. g.ScalarAsInputTextId = g.ActiveId;
  5174. g.HoveredId = id;
  5175. }
  5176. else if (g.ActiveId != g.ScalarAsInputTextId)
  5177. {
  5178. // Release
  5179. g.ScalarAsInputTextId = 0;
  5180. }
  5181. if (value_changed)
  5182. {
  5183. InputTextApplyArithmeticOp(text_buf, v);
  5184. }
  5185. return value_changed;
  5186. }
  5187. // Parse display precision back from the display format string
  5188. static inline int ParseFormatPrecision(const char* fmt, int default_precision)
  5189. {
  5190. int precision = default_precision;
  5191. while ((fmt = strchr(fmt, '%')) != NULL)
  5192. {
  5193. fmt++;
  5194. if (fmt[0] == '%') { fmt++; continue; } // Ignore "%%"
  5195. while (*fmt >= '0' && *fmt <= '9')
  5196. fmt++;
  5197. if (*fmt == '.')
  5198. {
  5199. precision = atoi(fmt + 1);
  5200. if (precision < 0 || precision > 10)
  5201. precision = default_precision;
  5202. }
  5203. break;
  5204. }
  5205. return precision;
  5206. }
  5207. static inline float RoundScalar(float value, int decimal_precision)
  5208. {
  5209. // Round past decimal precision
  5210. // 0: 1, 1: 0.1, 2: 0.01, etc.
  5211. // So when our value is 1.99999 with a precision of 0.001 we'll end up rounding to 2.0
  5212. // FIXME: Investigate better rounding methods
  5213. float min_step = 1.0f / powf(10.0f, (float)decimal_precision);
  5214. bool negative = value < 0.0f;
  5215. value = fabsf(value);
  5216. float remainder = fmodf(value, min_step);
  5217. if (remainder <= min_step*0.5f)
  5218. value -= remainder;
  5219. else
  5220. value += (min_step - remainder);
  5221. return negative ? -value : value;
  5222. }
  5223. static bool SliderBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v_min, float v_max, float power, int decimal_precision, bool horizontal)
  5224. {
  5225. ImGuiState& g = *GImGui;
  5226. ImGuiWindow* window = GetCurrentWindow();
  5227. const ImGuiStyle& style = g.Style;
  5228. // Draw frame
  5229. RenderFrame(frame_bb.Min, frame_bb.Max, window->Color(ImGuiCol_FrameBg), true, style.FrameRounding);
  5230. const bool is_non_linear = fabsf(power - 1.0f) > 0.0001f;
  5231. const float padding = horizontal ? style.FramePadding.x : style.FramePadding.y;
  5232. const float slider_sz = horizontal ? (frame_bb.GetWidth() - padding * 2.0f) : (frame_bb.GetHeight() - padding * 2.0f);
  5233. float grab_sz;
  5234. if (decimal_precision > 0)
  5235. grab_sz = ImMin(style.GrabMinSize, slider_sz);
  5236. else
  5237. grab_sz = ImMin(ImMax(1.0f * (slider_sz / (v_max-v_min+1.0f)), style.GrabMinSize), slider_sz); // Integer sliders, if possible have the grab size represent 1 unit
  5238. const float slider_usable_sz = slider_sz - grab_sz;
  5239. const float slider_usable_pos_min = (horizontal ? frame_bb.Min.x : frame_bb.Min.y) + padding + grab_sz*0.5f;
  5240. const float slider_usable_pos_max = (horizontal ? frame_bb.Max.x : frame_bb.Max.y) - padding - grab_sz*0.5f;
  5241. bool value_changed = false;
  5242. // For logarithmic sliders that cross over sign boundary we want the exponential increase to be symmetric around 0.0f
  5243. float linear_zero_pos = 0.0f; // 0.0->1.0f
  5244. if (v_min * v_max < 0.0f)
  5245. {
  5246. // Different sign
  5247. const float linear_dist_min_to_0 = powf(fabsf(0.0f - v_min), 1.0f/power);
  5248. const float linear_dist_max_to_0 = powf(fabsf(v_max - 0.0f), 1.0f/power);
  5249. linear_zero_pos = linear_dist_min_to_0 / (linear_dist_min_to_0+linear_dist_max_to_0);
  5250. }
  5251. else
  5252. {
  5253. // Same sign
  5254. linear_zero_pos = v_min < 0.0f ? 1.0f : 0.0f;
  5255. }
  5256. // Process clicking on the slider
  5257. if (g.ActiveId == id)
  5258. {
  5259. if (g.IO.MouseDown[0])
  5260. {
  5261. const float mouse_abs_pos = horizontal ? g.IO.MousePos.x : g.IO.MousePos.y;
  5262. float normalized_pos = ImClamp((mouse_abs_pos - slider_usable_pos_min) / slider_usable_sz, 0.0f, 1.0f);
  5263. if (!horizontal)
  5264. normalized_pos = 1.0f - normalized_pos;
  5265. float new_value;
  5266. if (is_non_linear)
  5267. {
  5268. // Account for logarithmic scale on both sides of the zero
  5269. if (normalized_pos < linear_zero_pos)
  5270. {
  5271. // Negative: rescale to the negative range before powering
  5272. float a = 1.0f - (normalized_pos / linear_zero_pos);
  5273. a = powf(a, power);
  5274. new_value = ImLerp(ImMin(v_max,0.0f), v_min, a);
  5275. }
  5276. else
  5277. {
  5278. // Positive: rescale to the positive range before powering
  5279. float a;
  5280. if (fabsf(linear_zero_pos - 1.0f) > 1.e-6)
  5281. a = (normalized_pos - linear_zero_pos) / (1.0f - linear_zero_pos);
  5282. else
  5283. a = normalized_pos;
  5284. a = powf(a, power);
  5285. new_value = ImLerp(ImMax(v_min,0.0f), v_max, a);
  5286. }
  5287. }
  5288. else
  5289. {
  5290. // Linear slider
  5291. new_value = ImLerp(v_min, v_max, normalized_pos);
  5292. }
  5293. // Round past decimal precision
  5294. new_value = RoundScalar(new_value, decimal_precision);
  5295. if (*v != new_value)
  5296. {
  5297. *v = new_value;
  5298. value_changed = true;
  5299. }
  5300. }
  5301. else
  5302. {
  5303. SetActiveId(0);
  5304. }
  5305. }
  5306. // Calculate slider grab positioning
  5307. float grab_t;
  5308. if (is_non_linear)
  5309. {
  5310. float v_clamped = ImClamp(*v, v_min, v_max);
  5311. if (v_clamped < 0.0f)
  5312. {
  5313. const float f = 1.0f - (v_clamped - v_min) / (ImMin(0.0f,v_max) - v_min);
  5314. grab_t = (1.0f - powf(f, 1.0f/power)) * linear_zero_pos;
  5315. }
  5316. else
  5317. {
  5318. const float f = (v_clamped - ImMax(0.0f,v_min)) / (v_max - ImMax(0.0f,v_min));
  5319. grab_t = linear_zero_pos + powf(f, 1.0f/power) * (1.0f - linear_zero_pos);
  5320. }
  5321. }
  5322. else
  5323. {
  5324. // Linear slider
  5325. grab_t = (ImClamp(*v, v_min, v_max) - v_min) / (v_max - v_min);
  5326. }
  5327. // Draw
  5328. if (!horizontal)
  5329. grab_t = 1.0f - grab_t;
  5330. const float grab_pos = ImLerp(slider_usable_pos_min, slider_usable_pos_max, grab_t);
  5331. ImRect grab_bb;
  5332. if (horizontal)
  5333. grab_bb = ImRect(ImVec2(grab_pos - grab_sz*0.5f, frame_bb.Min.y + 2.0f), ImVec2(grab_pos + grab_sz*0.5f, frame_bb.Max.y - 2.0f));
  5334. else
  5335. grab_bb = ImRect(ImVec2(frame_bb.Min.x + 2.0f, grab_pos - grab_sz*0.5f), ImVec2(frame_bb.Max.x - 2.0f, grab_pos + grab_sz*0.5f));
  5336. window->DrawList->AddRectFilled(grab_bb.Min, grab_bb.Max, window->Color(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), style.GrabRounding);
  5337. return value_changed;
  5338. }
  5339. // Use power!=1.0 for logarithmic sliders.
  5340. // Adjust display_format to decorate the value with a prefix or a suffix.
  5341. // "%.3f" 1.234
  5342. // "%5.2f secs" 01.23 secs
  5343. // "Gold: %.0f" Gold: 1
  5344. bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, const char* display_format, float power)
  5345. {
  5346. ImGuiState& g = *GImGui;
  5347. ImGuiWindow* window = GetCurrentWindow();
  5348. if (window->SkipItems)
  5349. return false;
  5350. const ImGuiStyle& style = g.Style;
  5351. const ImGuiID id = window->GetID(label);
  5352. const float w = ImGui::CalcItemWidth();
  5353. const ImVec2 label_size = CalcTextSize(label, NULL, true);
  5354. const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y) + style.FramePadding*2.0f);
  5355. const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));
  5356. // NB- we don't call ItemSize() yet because we may turn into a text edit box below
  5357. if (!ItemAdd(total_bb, &id))
  5358. {
  5359. ItemSize(total_bb, style.FramePadding.y);
  5360. return false;
  5361. }
  5362. const bool hovered = IsHovered(frame_bb, id);
  5363. if (hovered)
  5364. g.HoveredId = id;
  5365. if (!display_format)
  5366. display_format = "%.3f";
  5367. int decimal_precision = ParseFormatPrecision(display_format, 3);
  5368. // Tabbing or CTRL-clicking on Slider turns it into an input box
  5369. bool start_text_input = false;
  5370. const bool tab_focus_requested = window->FocusItemRegister(g.ActiveId == id);
  5371. if (tab_focus_requested || (hovered && g.IO.MouseClicked[0]))
  5372. {
  5373. SetActiveId(id, window);
  5374. FocusWindow(window);
  5375. if (tab_focus_requested || g.IO.KeyCtrl)
  5376. {
  5377. start_text_input = true;
  5378. g.ScalarAsInputTextId = 0;
  5379. }
  5380. }
  5381. if (start_text_input || (g.ActiveId == id && g.ScalarAsInputTextId == id))
  5382. return InputFloatReplaceWidget(frame_bb, label, v, id, decimal_precision);
  5383. ItemSize(total_bb, style.FramePadding.y);
  5384. // Actual slider behavior + render grab
  5385. const bool value_changed = SliderBehavior(frame_bb, id, v, v_min, v_max, power, decimal_precision, true);
  5386. // Display value using user-provided display format so user can add prefix/suffix/decorations to the value.
  5387. char value_buf[64];
  5388. const char* value_buf_end = value_buf + ImFormatString(value_buf, IM_ARRAYSIZE(value_buf), display_format, *v);
  5389. RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImGuiAlign_Center|ImGuiAlign_VCenter);
  5390. if (label_size.x > 0.0f)
  5391. RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);
  5392. return value_changed;
  5393. }
  5394. bool ImGui::VSliderFloat(const char* label, const ImVec2& size, float* v, float v_min, float v_max, const char* display_format, float power)
  5395. {
  5396. ImGuiState& g = *GImGui;
  5397. ImGuiWindow* window = GetCurrentWindow();
  5398. if (window->SkipItems)
  5399. return false;
  5400. const ImGuiStyle& style = g.Style;
  5401. const ImGuiID id = window->GetID(label);
  5402. const ImVec2 label_size = CalcTextSize(label, NULL, true);
  5403. const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + size);
  5404. const ImRect bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));
  5405. ItemSize(bb, style.FramePadding.y);
  5406. if (!ItemAdd(frame_bb, &id))
  5407. return false;
  5408. const bool hovered = IsHovered(frame_bb, id);
  5409. if (hovered)
  5410. g.HoveredId = id;
  5411. if (!display_format)
  5412. display_format = "%.3f";
  5413. int decimal_precision = ParseFormatPrecision(display_format, 3);
  5414. if (hovered && g.IO.MouseClicked[0])
  5415. {
  5416. SetActiveId(id, window);
  5417. FocusWindow(window);
  5418. }
  5419. // Actual slider behavior + render grab
  5420. bool value_changed = SliderBehavior(frame_bb, id, v, v_min, v_max, power, decimal_precision, false);
  5421. // Display value using user-provided display format so user can add prefix/suffix/decorations to the value.
  5422. // For the vertical slider we allow centered text to overlap the frame padding
  5423. char value_buf[64];
  5424. char* value_buf_end = value_buf + ImFormatString(value_buf, IM_ARRAYSIZE(value_buf), display_format, *v);
  5425. RenderTextClipped(ImVec2(frame_bb.Min.x, frame_bb.Min.y + style.FramePadding.y), frame_bb.Max, value_buf, value_buf_end, NULL, ImGuiAlign_Center);
  5426. if (label_size.x > 0.0f)
  5427. RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);
  5428. return value_changed;
  5429. }
  5430. bool ImGui::SliderAngle(const char* label, float* v_rad, float v_degrees_min, float v_degrees_max)
  5431. {
  5432. float v_deg = (*v_rad) * 360.0f / (2*IM_PI);
  5433. bool value_changed = ImGui::SliderFloat(label, &v_deg, v_degrees_min, v_degrees_max, "%.0f deg", 1.0f);
  5434. *v_rad = v_deg * (2*IM_PI) / 360.0f;
  5435. return value_changed;
  5436. }
  5437. bool ImGui::SliderInt(const char* label, int* v, int v_min, int v_max, const char* display_format)
  5438. {
  5439. if (!display_format)
  5440. display_format = "%.0f";
  5441. float v_f = (float)*v;
  5442. bool value_changed = ImGui::SliderFloat(label, &v_f, (float)v_min, (float)v_max, display_format, 1.0f);
  5443. *v = (int)v_f;
  5444. return value_changed;
  5445. }
  5446. bool ImGui::VSliderInt(const char* label, const ImVec2& size, int* v, int v_min, int v_max, const char* display_format)
  5447. {
  5448. if (!display_format)
  5449. display_format = "%.0f";
  5450. float v_f = (float)*v;
  5451. bool value_changed = ImGui::VSliderFloat(label, size, &v_f, (float)v_min, (float)v_max, display_format, 1.0f);
  5452. *v = (int)v_f;
  5453. return value_changed;
  5454. }
  5455. // Add multiple sliders on 1 line for compact edition of multiple components
  5456. static bool SliderFloatN(const char* label, float* v, int components, float v_min, float v_max, const char* display_format, float power)
  5457. {
  5458. ImGuiState& g = *GImGui;
  5459. ImGuiWindow* window = GetCurrentWindow();
  5460. if (window->SkipItems)
  5461. return false;
  5462. bool value_changed = false;
  5463. ImGui::BeginGroup();
  5464. ImGui::PushID(label);
  5465. PushMultiItemsWidths(components);
  5466. for (int i = 0; i < components; i++)
  5467. {
  5468. ImGui::PushID(i);
  5469. value_changed |= ImGui::SliderFloat("##v", &v[i], v_min, v_max, display_format, power);
  5470. ImGui::SameLine(0, g.Style.ItemInnerSpacing.x);
  5471. ImGui::PopID();
  5472. ImGui::PopItemWidth();
  5473. }
  5474. ImGui::PopID();
  5475. ImGui::TextUnformatted(label, FindTextDisplayEnd(label));
  5476. ImGui::EndGroup();
  5477. return value_changed;
  5478. }
  5479. bool ImGui::SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* display_format, float power)
  5480. {
  5481. return SliderFloatN(label, v, 2, v_min, v_max, display_format, power);
  5482. }
  5483. bool ImGui::SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* display_format, float power)
  5484. {
  5485. return SliderFloatN(label, v, 3, v_min, v_max, display_format, power);
  5486. }
  5487. bool ImGui::SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* display_format, float power)
  5488. {
  5489. return SliderFloatN(label, v, 4, v_min, v_max, display_format, power);
  5490. }
  5491. static bool SliderIntN(const char* label, int* v, int components, int v_min, int v_max, const char* display_format)
  5492. {
  5493. ImGuiState& g = *GImGui;
  5494. ImGuiWindow* window = GetCurrentWindow();
  5495. if (window->SkipItems)
  5496. return false;
  5497. bool value_changed = false;
  5498. ImGui::BeginGroup();
  5499. ImGui::PushID(label);
  5500. PushMultiItemsWidths(components);
  5501. for (int i = 0; i < components; i++)
  5502. {
  5503. ImGui::PushID(i);
  5504. value_changed |= ImGui::SliderInt("##v", &v[i], v_min, v_max, display_format);
  5505. ImGui::SameLine(0, g.Style.ItemInnerSpacing.x);
  5506. ImGui::PopID();
  5507. ImGui::PopItemWidth();
  5508. }
  5509. ImGui::PopID();
  5510. ImGui::TextUnformatted(label, FindTextDisplayEnd(label));
  5511. ImGui::EndGroup();
  5512. return value_changed;
  5513. }
  5514. bool ImGui::SliderInt2(const char* label, int v[2], int v_min, int v_max, const char* display_format)
  5515. {
  5516. return SliderIntN(label, v, 2, v_min, v_max, display_format);
  5517. }
  5518. bool ImGui::SliderInt3(const char* label, int v[3], int v_min, int v_max, const char* display_format)
  5519. {
  5520. return SliderIntN(label, v, 3, v_min, v_max, display_format);
  5521. }
  5522. bool ImGui::SliderInt4(const char* label, int v[4], int v_min, int v_max, const char* display_format)
  5523. {
  5524. return SliderIntN(label, v, 4, v_min, v_max, display_format);
  5525. }
  5526. // FIXME-WIP: Work in progress. May change API / behavior.
  5527. static bool DragBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v_speed, float v_min, float v_max, int decimal_precision, float power)
  5528. {
  5529. ImGuiState& g = *GImGui;
  5530. ImGuiWindow* window = GetCurrentWindow();
  5531. const ImGuiStyle& style = g.Style;
  5532. // Draw frame
  5533. const ImU32 frame_col = window->Color(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
  5534. RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, style.FrameRounding);
  5535. bool value_changed = false;
  5536. // Process clicking on the drag
  5537. if (g.ActiveId == id)
  5538. {
  5539. if (g.IO.MouseDown[0])
  5540. {
  5541. if (g.ActiveIdIsJustActivated)
  5542. {
  5543. // Lock current value on click
  5544. g.DragCurrentValue = *v;
  5545. g.DragLastMouseDelta = ImVec2(0.f, 0.f);
  5546. }
  5547. const ImVec2 mouse_drag_delta = ImGui::GetMouseDragDelta(0, 1.0f);
  5548. if (fabsf(mouse_drag_delta.x - g.DragLastMouseDelta.x) > 0.0f)
  5549. {
  5550. float speed = v_speed;
  5551. if (speed == 0.0f && (v_max - v_min) != 0.0f && (v_max - v_min) < FLT_MAX)
  5552. speed = (v_max - v_min) * g.DragSpeedDefaultRatio;
  5553. if (g.IO.KeyShift && g.DragSpeedScaleFast >= 0.0f)
  5554. speed = speed * g.DragSpeedScaleFast;
  5555. if (g.IO.KeyAlt && g.DragSpeedScaleSlow >= 0.0f)
  5556. speed = speed * g.DragSpeedScaleSlow;
  5557. float v_cur = g.DragCurrentValue;
  5558. float delta = (mouse_drag_delta.x - g.DragLastMouseDelta.x) * speed;
  5559. if (fabsf(power - 1.0f) > 0.001f)
  5560. {
  5561. // Logarithmic curve on both side of 0.0
  5562. float v0_abs = v_cur >= 0.0f ? v_cur : -v_cur;
  5563. float v0_sign = v_cur >= 0.0f ? 1.0f : -1.0f;
  5564. float v1 = powf(v0_abs, 1.0f / power) + (delta * v0_sign);
  5565. float v1_abs = v1 >= 0.0f ? v1 : -v1;
  5566. float v1_sign = v1 >= 0.0f ? 1.0f : -1.0f; // Crossed sign line
  5567. v_cur = powf(v1_abs, power) * v0_sign * v1_sign; // Reapply sign
  5568. }
  5569. else
  5570. {
  5571. v_cur += delta;
  5572. }
  5573. g.DragLastMouseDelta.x = mouse_drag_delta.x;
  5574. // Clamp
  5575. if (v_min < v_max)
  5576. v_cur = ImClamp(v_cur, v_min, v_max);
  5577. g.DragCurrentValue = v_cur;
  5578. // Round to user desired precision, then apply
  5579. v_cur = RoundScalar(v_cur, decimal_precision);
  5580. if (*v != v_cur)
  5581. {
  5582. *v = v_cur;
  5583. value_changed = true;
  5584. }
  5585. }
  5586. }
  5587. else
  5588. {
  5589. SetActiveId(0);
  5590. }
  5591. }
  5592. return value_changed;
  5593. }
  5594. bool ImGui::DragFloat(const char* label, float *v, float v_speed, float v_min, float v_max, const char* display_format, float power)
  5595. {
  5596. ImGuiState& g = *GImGui;
  5597. ImGuiWindow* window = GetCurrentWindow();
  5598. if (window->SkipItems)
  5599. return false;
  5600. const ImGuiStyle& style = g.Style;
  5601. const ImGuiID id = window->GetID(label);
  5602. const float w = ImGui::CalcItemWidth();
  5603. const ImVec2 label_size = CalcTextSize(label, NULL, true);
  5604. const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y) + style.FramePadding*2.0f);
  5605. const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding);
  5606. const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));
  5607. // NB- we don't call ItemSize() yet because we may turn into a text edit box below
  5608. if (!ItemAdd(total_bb, &id))
  5609. {
  5610. ItemSize(total_bb, style.FramePadding.y);
  5611. return false;
  5612. }
  5613. const bool hovered = IsHovered(frame_bb, id);
  5614. if (hovered)
  5615. g.HoveredId = id;
  5616. if (!display_format)
  5617. display_format = "%.3f";
  5618. int decimal_precision = ParseFormatPrecision(display_format, 3);
  5619. // Tabbing or CTRL-clicking on Drag turns it into an input box
  5620. bool start_text_input = false;
  5621. const bool tab_focus_requested = window->FocusItemRegister(g.ActiveId == id);
  5622. if (tab_focus_requested || (hovered && (g.IO.MouseClicked[0] | g.IO.MouseDoubleClicked[0])))
  5623. {
  5624. SetActiveId(id, window);
  5625. FocusWindow(window);
  5626. if (tab_focus_requested || g.IO.KeyCtrl || g.IO.MouseDoubleClicked[0])
  5627. {
  5628. start_text_input = true;
  5629. g.ScalarAsInputTextId = 0;
  5630. }
  5631. }
  5632. if (start_text_input || (g.ActiveId == id && g.ScalarAsInputTextId == id))
  5633. return InputFloatReplaceWidget(frame_bb, label, v, id, decimal_precision);
  5634. ItemSize(total_bb, style.FramePadding.y);
  5635. // Actual drag behavior
  5636. const bool value_changed = DragBehavior(frame_bb, id, v, v_speed, v_min, v_max, decimal_precision, power);
  5637. // Display value using user-provided display format so user can add prefix/suffix/decorations to the value.
  5638. char value_buf[64];
  5639. const char* value_buf_end = value_buf + ImFormatString(value_buf, IM_ARRAYSIZE(value_buf), display_format, *v);
  5640. RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImGuiAlign_Center|ImGuiAlign_VCenter);
  5641. if (label_size.x > 0.0f)
  5642. RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Min.y), label);
  5643. return value_changed;
  5644. }
  5645. static bool DragFloatN(const char* label, float* v, int components, float v_speed, float v_min, float v_max, const char* display_format, float power)
  5646. {
  5647. ImGuiState& g = *GImGui;
  5648. ImGuiWindow* window = GetCurrentWindow();
  5649. if (window->SkipItems)
  5650. return false;
  5651. bool value_changed = false;
  5652. ImGui::BeginGroup();
  5653. ImGui::PushID(label);
  5654. PushMultiItemsWidths(components);
  5655. for (int i = 0; i < components; i++)
  5656. {
  5657. ImGui::PushID(i);
  5658. value_changed |= ImGui::DragFloat("##v", &v[i], v_speed, v_min, v_max, display_format, power);
  5659. ImGui::SameLine(0, g.Style.ItemInnerSpacing.x);
  5660. ImGui::PopID();
  5661. ImGui::PopItemWidth();
  5662. }
  5663. ImGui::PopID();
  5664. ImGui::TextUnformatted(label, FindTextDisplayEnd(label));
  5665. ImGui::EndGroup();
  5666. return value_changed;
  5667. }
  5668. bool ImGui::DragFloat2(const char* label, float v[2], float v_speed, float v_min, float v_max, const char* display_format, float power)
  5669. {
  5670. return DragFloatN(label, v, 2, v_speed, v_min, v_max, display_format, power);
  5671. }
  5672. bool ImGui::DragFloat3(const char* label, float v[3], float v_speed, float v_min, float v_max, const char* display_format, float power)
  5673. {
  5674. return DragFloatN(label, v, 3, v_speed, v_min, v_max, display_format, power);
  5675. }
  5676. bool ImGui::DragFloat4(const char* label, float v[4], float v_speed, float v_min, float v_max, const char* display_format, float power)
  5677. {
  5678. return DragFloatN(label, v, 4, v_speed, v_min, v_max, display_format, power);
  5679. }
  5680. bool ImGui::DragFloatRange2(const char* label, float* v_current_min, float* v_current_max, float v_speed, float v_min, float v_max, const char* display_format, const char* display_format_max, float power)
  5681. {
  5682. ImGuiState& g = *GImGui;
  5683. ImGuiWindow* window = GetCurrentWindow();
  5684. if (window->SkipItems)
  5685. return false;
  5686. ImGui::PushID(label);
  5687. ImGui::BeginGroup();
  5688. PushMultiItemsWidths(2);
  5689. bool value_changed = ImGui::DragFloat("##min", v_current_min, v_speed, (v_min >= v_max) ? -FLT_MAX : v_min, (v_min >= v_max) ? *v_current_max : ImMin(v_max, *v_current_max), display_format, power);
  5690. ImGui::PopItemWidth();
  5691. ImGui::SameLine(0, g.Style.ItemInnerSpacing.x);
  5692. value_changed |= ImGui::DragFloat("##max", v_current_max, v_speed, (v_min >= v_max) ? *v_current_min : ImMax(v_min, *v_current_min), (v_min >= v_max) ? FLT_MAX : v_max, display_format_max ? display_format_max : display_format, power);
  5693. ImGui::PopItemWidth();
  5694. ImGui::SameLine(0, g.Style.ItemInnerSpacing.x);
  5695. ImGui::TextUnformatted(label, FindTextDisplayEnd(label));
  5696. ImGui::EndGroup();
  5697. ImGui::PopID();
  5698. return value_changed;
  5699. }
  5700. // NB: v_speed is float to allow adjusting the drag speed with more precision
  5701. bool ImGui::DragInt(const char* label, int* v, float v_speed, int v_min, int v_max, const char* display_format)
  5702. {
  5703. if (!display_format)
  5704. display_format = "%.0f";
  5705. float v_f = (float)*v;
  5706. bool value_changed = ImGui::DragFloat(label, &v_f, v_speed, (float)v_min, (float)v_max, display_format);
  5707. *v = (int)v_f;
  5708. return value_changed;
  5709. }
  5710. static bool DragIntN(const char* label, int* v, int components, float v_speed, int v_min, int v_max, const char* display_format)
  5711. {
  5712. ImGuiState& g = *GImGui;
  5713. ImGuiWindow* window = GetCurrentWindow();
  5714. if (window->SkipItems)
  5715. return false;
  5716. bool value_changed = false;
  5717. ImGui::BeginGroup();
  5718. ImGui::PushID(label);
  5719. PushMultiItemsWidths(components);
  5720. for (int i = 0; i < components; i++)
  5721. {
  5722. ImGui::PushID(i);
  5723. value_changed |= ImGui::DragInt("##v", &v[i], v_speed, v_min, v_max, display_format);
  5724. ImGui::SameLine(0, g.Style.ItemInnerSpacing.x);
  5725. ImGui::PopID();
  5726. ImGui::PopItemWidth();
  5727. }
  5728. ImGui::PopID();
  5729. ImGui::TextUnformatted(label, FindTextDisplayEnd(label));
  5730. ImGui::EndGroup();
  5731. return value_changed;
  5732. }
  5733. bool ImGui::DragInt2(const char* label, int v[2], float v_speed, int v_min, int v_max, const char* display_format)
  5734. {
  5735. return DragIntN(label, v, 2, v_speed, v_min, v_max, display_format);
  5736. }
  5737. bool ImGui::DragInt3(const char* label, int v[3], float v_speed, int v_min, int v_max, const char* display_format)
  5738. {
  5739. return DragIntN(label, v, 3, v_speed, v_min, v_max, display_format);
  5740. }
  5741. bool ImGui::DragInt4(const char* label, int v[4], float v_speed, int v_min, int v_max, const char* display_format)
  5742. {
  5743. return DragIntN(label, v, 4, v_speed, v_min, v_max, display_format);
  5744. }
  5745. bool ImGui::DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed, int v_min, int v_max, const char* display_format, const char* display_format_max)
  5746. {
  5747. ImGuiState& g = *GImGui;
  5748. ImGuiWindow* window = GetCurrentWindow();
  5749. if (window->SkipItems)
  5750. return false;
  5751. ImGui::PushID(label);
  5752. ImGui::BeginGroup();
  5753. PushMultiItemsWidths(2);
  5754. bool value_changed = ImGui::DragInt("##min", v_current_min, v_speed, (v_min >= v_max) ? IM_INT_MIN : v_min, (v_min >= v_max) ? *v_current_max : ImMin(v_max, *v_current_max), display_format);
  5755. ImGui::PopItemWidth();
  5756. ImGui::SameLine(0, g.Style.ItemInnerSpacing.x);
  5757. value_changed |= ImGui::DragInt("##max", v_current_max, v_speed, (v_min >= v_max) ? *v_current_min : ImMax(v_min, *v_current_min), (v_min >= v_max) ? IM_INT_MAX : v_max, display_format_max ? display_format_max : display_format);
  5758. ImGui::PopItemWidth();
  5759. ImGui::SameLine(0, g.Style.ItemInnerSpacing.x);
  5760. ImGui::TextUnformatted(label, FindTextDisplayEnd(label));
  5761. ImGui::EndGroup();
  5762. ImGui::PopID();
  5763. return value_changed;
  5764. }
  5765. enum ImGuiPlotType
  5766. {
  5767. ImGuiPlotType_Lines,
  5768. ImGuiPlotType_Histogram
  5769. };
  5770. static void Plot(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size)
  5771. {
  5772. ImGuiState& g = *GImGui;
  5773. ImGuiWindow* window = GetCurrentWindow();
  5774. if (window->SkipItems)
  5775. return;
  5776. const ImGuiStyle& style = g.Style;
  5777. const ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true);
  5778. if (graph_size.x == 0.0f)
  5779. graph_size.x = ImGui::CalcItemWidth() + (style.FramePadding.x * 2);
  5780. if (graph_size.y == 0.0f)
  5781. graph_size.y = label_size.y + (style.FramePadding.y * 2);
  5782. const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(graph_size.x, graph_size.y));
  5783. const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding);
  5784. const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0));
  5785. ItemSize(total_bb, style.FramePadding.y);
  5786. if (!ItemAdd(total_bb, NULL))
  5787. return;
  5788. // Determine scale from values if not specified
  5789. if (scale_min == FLT_MAX || scale_max == FLT_MAX)
  5790. {
  5791. float v_min = FLT_MAX;
  5792. float v_max = -FLT_MAX;
  5793. for (int i = 0; i < values_count; i++)
  5794. {
  5795. const float v = values_getter(data, i);
  5796. v_min = ImMin(v_min, v);
  5797. v_max = ImMax(v_max, v);
  5798. }
  5799. if (scale_min == FLT_MAX)
  5800. scale_min = v_min;
  5801. if (scale_max == FLT_MAX)
  5802. scale_max = v_max;
  5803. }
  5804. RenderFrame(frame_bb.Min, frame_bb.Max, window->Color(ImGuiCol_FrameBg), true, style.FrameRounding);
  5805. int res_w = ImMin((int)graph_size.x, values_count);
  5806. if (plot_type == ImGuiPlotType_Lines)
  5807. res_w -= 1;
  5808. // Tooltip on hover
  5809. int v_hovered = -1;
  5810. if (IsHovered(inner_bb, 0))
  5811. {
  5812. const float t = ImClamp((g.IO.MousePos.x - inner_bb.Min.x) / (inner_bb.Max.x - inner_bb.Min.x), 0.0f, 0.9999f);
  5813. const int v_idx = (int)(t * (values_count + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0)));
  5814. IM_ASSERT(v_idx >= 0 && v_idx < values_count);
  5815. const float v0 = values_getter(data, (v_idx + values_offset) % values_count);
  5816. const float v1 = values_getter(data, (v_idx + 1 + values_offset) % values_count);
  5817. if (plot_type == ImGuiPlotType_Lines)
  5818. ImGui::SetTooltip("%d: %8.4g\n%d: %8.4g", v_idx, v0, v_idx+1, v1);
  5819. else if (plot_type == ImGuiPlotType_Histogram)
  5820. ImGui::SetTooltip("%d: %8.4g", v_idx, v0);
  5821. v_hovered = v_idx;
  5822. }
  5823. const float t_step = 1.0f / (float)res_w;
  5824. float v0 = values_getter(data, (0 + values_offset) % values_count);
  5825. float t0 = 0.0f;
  5826. ImVec2 p0 = ImVec2( t0, 1.0f - ImSaturate((v0 - scale_min) / (scale_max - scale_min)) );
  5827. const ImU32 col_base = window->Color((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLines : ImGuiCol_PlotHistogram);
  5828. const ImU32 col_hovered = window->Color((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLinesHovered : ImGuiCol_PlotHistogramHovered);
  5829. for (int n = 0; n < res_w; n++)
  5830. {
  5831. const float t1 = t0 + t_step;
  5832. const int v_idx = (int)(t0 * values_count);
  5833. IM_ASSERT(v_idx >= 0 && v_idx < values_count);
  5834. const float v1 = values_getter(data, (v_idx + values_offset + 1) % values_count);
  5835. const ImVec2 p1 = ImVec2( t1, 1.0f - ImSaturate((v1 - scale_min) / (scale_max - scale_min)) );
  5836. // NB- Draw calls are merged together by the DrawList system.
  5837. if (plot_type == ImGuiPlotType_Lines)
  5838. window->DrawList->AddLine(ImLerp(inner_bb.Min, inner_bb.Max, p0), ImLerp(inner_bb.Min, inner_bb.Max, p1), v_hovered == v_idx ? col_hovered : col_base);
  5839. else if (plot_type == ImGuiPlotType_Histogram)
  5840. window->DrawList->AddRectFilled(ImLerp(inner_bb.Min, inner_bb.Max, p0), ImLerp(inner_bb.Min, inner_bb.Max, ImVec2(p1.x, 1.0f))+ImVec2(-1,0), v_hovered == v_idx ? col_hovered : col_base);
  5841. t0 = t1;
  5842. p0 = p1;
  5843. }
  5844. // Text overlay
  5845. if (overlay_text)
  5846. RenderTextClipped(ImVec2(frame_bb.Min.x, frame_bb.Min.y + style.FramePadding.y), frame_bb.Max, overlay_text, NULL, NULL, ImGuiAlign_Center);
  5847. RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Min.y), label);
  5848. }
  5849. struct ImGuiPlotArrayGetterData
  5850. {
  5851. const float* Values;
  5852. int Stride;
  5853. ImGuiPlotArrayGetterData(const float* values, int stride) { Values = values; Stride = stride; }
  5854. };
  5855. static float Plot_ArrayGetter(void* data, int idx)
  5856. {
  5857. ImGuiPlotArrayGetterData* plot_data = (ImGuiPlotArrayGetterData*)data;
  5858. const float v = *(float*)(void*)((unsigned char*)plot_data->Values + (size_t)idx * plot_data->Stride);
  5859. return v;
  5860. }
  5861. void ImGui::PlotLines(const char* label, const float* values, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size, int stride)
  5862. {
  5863. ImGuiPlotArrayGetterData data(values, stride);
  5864. Plot(ImGuiPlotType_Lines, label, &Plot_ArrayGetter, (void*)&data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size);
  5865. }
  5866. void ImGui::PlotLines(const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size)
  5867. {
  5868. Plot(ImGuiPlotType_Lines, label, values_getter, data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size);
  5869. }
  5870. void ImGui::PlotHistogram(const char* label, const float* values, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size, int stride)
  5871. {
  5872. ImGuiPlotArrayGetterData data(values, stride);
  5873. Plot(ImGuiPlotType_Histogram, label, &Plot_ArrayGetter, (void*)&data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size);
  5874. }
  5875. void ImGui::PlotHistogram(const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size)
  5876. {
  5877. Plot(ImGuiPlotType_Histogram, label, values_getter, data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size);
  5878. }
  5879. bool ImGui::Checkbox(const char* label, bool* v)
  5880. {
  5881. ImGuiState& g = *GImGui;
  5882. ImGuiWindow* window = GetCurrentWindow();
  5883. if (window->SkipItems)
  5884. return false;
  5885. const ImGuiStyle& style = g.Style;
  5886. const ImGuiID id = window->GetID(label);
  5887. const ImVec2 label_size = CalcTextSize(label, NULL, true);
  5888. const ImRect check_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(label_size.y + style.FramePadding.y*2, label_size.y + style.FramePadding.y*2));
  5889. ItemSize(check_bb, style.FramePadding.y);
  5890. ImRect total_bb = check_bb;
  5891. if (label_size.x > 0)
  5892. SameLine(0, style.ItemInnerSpacing.x);
  5893. const ImRect text_bb(window->DC.CursorPos + ImVec2(0,style.FramePadding.y), window->DC.CursorPos + ImVec2(0,style.FramePadding.y) + label_size);
  5894. if (label_size.x > 0)
  5895. {
  5896. ItemSize(ImVec2(text_bb.GetWidth(), check_bb.GetHeight()), style.FramePadding.y);
  5897. total_bb = ImRect(ImMin(check_bb.Min, text_bb.Min), ImMax(check_bb.Max, text_bb.Max));
  5898. }
  5899. if (!ItemAdd(total_bb, &id))
  5900. return false;
  5901. bool hovered, held;
  5902. bool pressed = ButtonBehavior(total_bb, id, &hovered, &held, true);
  5903. if (pressed)
  5904. *v = !(*v);
  5905. RenderFrame(check_bb.Min, check_bb.Max, window->Color((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), true, style.FrameRounding);
  5906. if (*v)
  5907. {
  5908. const float check_sz = ImMin(check_bb.GetWidth(), check_bb.GetHeight());
  5909. const float pad = check_sz < 8.0f ? 1.0f : check_sz < 13.0f ? 2.0f : 3.0f;
  5910. window->DrawList->AddRectFilled(check_bb.Min+ImVec2(pad,pad), check_bb.Max-ImVec2(pad,pad), window->Color(ImGuiCol_CheckMark), style.FrameRounding);
  5911. }
  5912. if (g.LogEnabled)
  5913. LogText(text_bb.GetTL(), *v ? "[x]" : "[ ]");
  5914. RenderText(text_bb.GetTL(), label);
  5915. return pressed;
  5916. }
  5917. bool ImGui::CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value)
  5918. {
  5919. bool v = (*flags & flags_value) ? true : false;
  5920. bool pressed = ImGui::Checkbox(label, &v);
  5921. if (v)
  5922. *flags |= flags_value;
  5923. else
  5924. *flags &= ~flags_value;
  5925. return pressed;
  5926. }
  5927. bool ImGui::RadioButton(const char* label, bool active)
  5928. {
  5929. ImGuiState& g = *GImGui;
  5930. ImGuiWindow* window = GetCurrentWindow();
  5931. if (window->SkipItems)
  5932. return false;
  5933. const ImGuiStyle& style = g.Style;
  5934. const ImGuiID id = window->GetID(label);
  5935. const ImVec2 label_size = CalcTextSize(label, NULL, true);
  5936. const ImRect check_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(label_size.y + style.FramePadding.y*2-1, label_size.y + style.FramePadding.y*2-1));
  5937. ItemSize(check_bb, style.FramePadding.y);
  5938. ImRect total_bb = check_bb;
  5939. if (label_size.x > 0)
  5940. SameLine(0, style.ItemInnerSpacing.x);
  5941. const ImRect text_bb(window->DC.CursorPos + ImVec2(0, style.FramePadding.y), window->DC.CursorPos + ImVec2(0, style.FramePadding.y) + label_size);
  5942. if (label_size.x > 0)
  5943. {
  5944. ItemSize(ImVec2(text_bb.GetWidth(), check_bb.GetHeight()), style.FramePadding.y);
  5945. total_bb.Add(text_bb);
  5946. }
  5947. if (!ItemAdd(total_bb, &id))
  5948. return false;
  5949. ImVec2 center = check_bb.GetCenter();
  5950. center.x = (float)(int)center.x + 0.5f;
  5951. center.y = (float)(int)center.y + 0.5f;
  5952. const float radius = check_bb.GetHeight() * 0.5f;
  5953. bool hovered, held;
  5954. bool pressed = ButtonBehavior(total_bb, id, &hovered, &held, true);
  5955. window->DrawList->AddCircleFilled(center, radius, window->Color((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), 16);
  5956. if (active)
  5957. {
  5958. const float check_sz = ImMin(check_bb.GetWidth(), check_bb.GetHeight());
  5959. const float pad = check_sz < 8.0f ? 1.0f : check_sz < 13.0f ? 2.0f : 3.0f;
  5960. window->DrawList->AddCircleFilled(center, radius-pad, window->Color(ImGuiCol_CheckMark), 16);
  5961. }
  5962. if (window->Flags & ImGuiWindowFlags_ShowBorders)
  5963. {
  5964. window->DrawList->AddCircle(center+ImVec2(1,1), radius, window->Color(ImGuiCol_BorderShadow), 16);
  5965. window->DrawList->AddCircle(center, radius, window->Color(ImGuiCol_Border), 16);
  5966. }
  5967. if (g.LogEnabled)
  5968. LogText(text_bb.GetTL(), active ? "(x)" : "( )");
  5969. RenderText(text_bb.GetTL(), label);
  5970. return pressed;
  5971. }
  5972. bool ImGui::RadioButton(const char* label, int* v, int v_button)
  5973. {
  5974. const bool pressed = ImGui::RadioButton(label, *v == v_button);
  5975. if (pressed)
  5976. {
  5977. *v = v_button;
  5978. }
  5979. return pressed;
  5980. }
  5981. static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end)
  5982. {
  5983. int line_count = 0;
  5984. const char* s = text_begin;
  5985. while (char c = *s++) // We are only matching for \n so we can ignore UTF-8 decoding
  5986. if (c == '\n')
  5987. line_count++;
  5988. s--;
  5989. if (s[0] != '\n' && s[0] != '\r')
  5990. line_count++;
  5991. *out_text_end = s;
  5992. return line_count;
  5993. }
  5994. static ImVec2 InputTextCalcTextSizeW(const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false)
  5995. {
  5996. ImFont* font = GImGui->Font;
  5997. const float line_height = GImGui->FontSize;
  5998. const float scale = line_height / font->FontSize;
  5999. ImVec2 text_size = ImVec2(0,0);
  6000. float line_width = 0.0f;
  6001. const ImWchar* s = text_begin;
  6002. while (s < text_end)
  6003. {
  6004. unsigned int c = (unsigned int)(*s++);
  6005. if (c == '\n')
  6006. {
  6007. text_size.x = ImMax(text_size.x, line_width);
  6008. text_size.y += line_height;
  6009. line_width = 0.0f;
  6010. if (stop_on_new_line)
  6011. break;
  6012. continue;
  6013. }
  6014. if (c == '\r')
  6015. continue;
  6016. const float char_width = font->GetCharAdvance((unsigned short)c) * scale;
  6017. line_width += char_width;
  6018. }
  6019. if (text_size.x < line_width)
  6020. text_size.x = line_width;
  6021. if (out_offset)
  6022. *out_offset = ImVec2(line_width, text_size.y + line_height); // offset allow for the possibility of sitting after a trailing \n
  6023. if (line_width > 0 || text_size.y == 0.0f) // whereas size.y will ignore the trailing \n
  6024. text_size.y += line_height;
  6025. if (remaining)
  6026. *remaining = s;
  6027. return text_size;
  6028. }
  6029. // Wrapper for stb_textedit.h to edit text (our wrapper is for: statically sized buffer, single-line, wchar characters. InputText converts between UTF-8 and wchar)
  6030. static int STB_TEXTEDIT_STRINGLEN(const STB_TEXTEDIT_STRING* obj) { return obj->CurLenW; }
  6031. static ImWchar STB_TEXTEDIT_GETCHAR(const STB_TEXTEDIT_STRING* obj, int idx) { return obj->Text[idx]; }
  6032. static float STB_TEXTEDIT_GETWIDTH(STB_TEXTEDIT_STRING* obj, int line_start_idx, int char_idx) { ImWchar c = obj->Text[line_start_idx+char_idx]; if (c == '\n') return STB_TEXTEDIT_GETWIDTH_NEWLINE; return GImGui->Font->GetCharAdvance(c) * (GImGui->FontSize / GImGui->Font->FontSize); }
  6033. static int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x10000 ? 0 : key; }
  6034. static ImWchar STB_TEXTEDIT_NEWLINE = '\n';
  6035. static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, STB_TEXTEDIT_STRING* obj, int line_start_idx)
  6036. {
  6037. const ImWchar* text = obj->Text.Data;
  6038. const ImWchar* text_remaining = NULL;
  6039. const ImVec2 size = InputTextCalcTextSizeW(text + line_start_idx, text + obj->CurLenW, &text_remaining, NULL, true);
  6040. r->x0 = 0.0f;
  6041. r->x1 = size.x;
  6042. r->baseline_y_delta = size.y;
  6043. r->ymin = 0.0f;
  6044. r->ymax = size.y;
  6045. r->num_chars = (int)(text_remaining - (text + line_start_idx));
  6046. }
  6047. static bool is_separator(unsigned int c) { return c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|'; }
  6048. #define STB_TEXTEDIT_IS_SPACE(CH) ( ImCharIsSpace((unsigned int)CH) || is_separator((unsigned int)CH) )
  6049. static void STB_TEXTEDIT_DELETECHARS(STB_TEXTEDIT_STRING* obj, int pos, int n)
  6050. {
  6051. ImWchar* dst = obj->Text.Data + pos;
  6052. // We maintain our buffer length in both UTF-8 and wchar formats
  6053. obj->CurLenA -= ImTextCountUtf8BytesFromStr(dst, dst + n);
  6054. obj->CurLenW -= n;
  6055. // Offset remaining text
  6056. const ImWchar* src = obj->Text.Data + pos + n;
  6057. while (ImWchar c = *src++)
  6058. *dst++ = c;
  6059. *dst = '\0';
  6060. }
  6061. static bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING* obj, int pos, const ImWchar* new_text, int new_text_len)
  6062. {
  6063. const int text_len = obj->CurLenW;
  6064. if (new_text_len + text_len + 1 > obj->Text.Size)
  6065. return false;
  6066. const int new_text_len_utf8 = ImTextCountUtf8BytesFromStr(new_text, new_text + new_text_len);
  6067. if (new_text_len_utf8 + obj->CurLenA + 1 > obj->BufSizeA)
  6068. return false;
  6069. ImWchar* text = obj->Text.Data;
  6070. if (pos != text_len)
  6071. memmove(text + pos + new_text_len, text + pos, (size_t)(text_len - pos) * sizeof(ImWchar));
  6072. memcpy(text + pos, new_text, (size_t)new_text_len * sizeof(ImWchar));
  6073. obj->CurLenW += new_text_len;
  6074. obj->CurLenA += new_text_len_utf8;
  6075. obj->Text[obj->CurLenW] = '\0';
  6076. return true;
  6077. }
  6078. // We don't use an enum so we can build even with conflicting symbols (if another user of stb_textedit.h leak their STB_TEXTEDIT_K_* symbols)
  6079. #define STB_TEXTEDIT_K_LEFT 0x10000 // keyboard input to move cursor left
  6080. #define STB_TEXTEDIT_K_RIGHT 0x10001 // keyboard input to move cursor right
  6081. #define STB_TEXTEDIT_K_UP 0x10002 // keyboard input to move cursor up
  6082. #define STB_TEXTEDIT_K_DOWN 0x10003 // keyboard input to move cursor down
  6083. #define STB_TEXTEDIT_K_LINESTART 0x10004 // keyboard input to move cursor to start of line
  6084. #define STB_TEXTEDIT_K_LINEEND 0x10005 // keyboard input to move cursor to end of line
  6085. #define STB_TEXTEDIT_K_TEXTSTART 0x10006 // keyboard input to move cursor to start of text
  6086. #define STB_TEXTEDIT_K_TEXTEND 0x10007 // keyboard input to move cursor to end of text
  6087. #define STB_TEXTEDIT_K_DELETE 0x10008 // keyboard input to delete selection or character under cursor
  6088. #define STB_TEXTEDIT_K_BACKSPACE 0x10009 // keyboard input to delete selection or character left of cursor
  6089. #define STB_TEXTEDIT_K_UNDO 0x1000A // keyboard input to perform undo
  6090. #define STB_TEXTEDIT_K_REDO 0x1000B // keyboard input to perform redo
  6091. #define STB_TEXTEDIT_K_WORDLEFT 0x1000C // keyboard input to move cursor left one word
  6092. #define STB_TEXTEDIT_K_WORDRIGHT 0x1000D // keyboard input to move cursor right one word
  6093. #define STB_TEXTEDIT_K_SHIFT 0x20000
  6094. #ifdef IMGUI_STB_NAMESPACE
  6095. namespace IMGUI_STB_NAMESPACE
  6096. {
  6097. #endif
  6098. #define STB_TEXTEDIT_IMPLEMENTATION
  6099. #include "stb_textedit.h"
  6100. #ifdef IMGUI_STB_NAMESPACE
  6101. }
  6102. #endif
  6103. void ImGuiTextEditState::OnKeyPressed(int key)
  6104. {
  6105. stb_textedit_key(this, &StbState, key);
  6106. CursorFollow = true;
  6107. CursorAnimReset();
  6108. }
  6109. // Public API to manipulate UTF-8 text
  6110. // We expose UTF-8 to the user (unlike the STB_TEXTEDIT_* functions which are manipulating wchar)
  6111. void ImGuiTextEditCallbackData::DeleteChars(int pos, int bytes_count)
  6112. {
  6113. char* dst = Buf + pos;
  6114. const char* src = Buf + pos + bytes_count;
  6115. while (char c = *src++)
  6116. *dst++ = c;
  6117. *dst = '\0';
  6118. BufDirty = true;
  6119. if (CursorPos + bytes_count >= pos)
  6120. CursorPos -= bytes_count;
  6121. else if (CursorPos >= pos)
  6122. CursorPos = pos;
  6123. SelectionStart = SelectionEnd = CursorPos;
  6124. }
  6125. void ImGuiTextEditCallbackData::InsertChars(int pos, const char* new_text, const char* new_text_end)
  6126. {
  6127. const int text_len = (int)strlen(Buf);
  6128. if (!new_text_end)
  6129. new_text_end = new_text + strlen(new_text);
  6130. const int new_text_len = (int)(new_text_end - new_text);
  6131. if (new_text_len + text_len + 1 >= BufSize)
  6132. return;
  6133. if (text_len != pos)
  6134. memmove(Buf + pos + new_text_len, Buf + pos, (size_t)(text_len - pos));
  6135. memcpy(Buf + pos, new_text, (size_t)new_text_len * sizeof(char));
  6136. Buf[text_len + new_text_len] = '\0';
  6137. BufDirty = true;
  6138. if (CursorPos >= pos)
  6139. CursorPos += new_text_len;
  6140. SelectionStart = SelectionEnd = CursorPos;
  6141. }
  6142. // Return false to discard a character.
  6143. static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiTextEditCallback callback, void* user_data)
  6144. {
  6145. unsigned int c = *p_char;
  6146. if (c < 128 && c != ' ' && !isprint((int)(c & 0xFF)))
  6147. {
  6148. bool pass = false;
  6149. pass |= (c == '\n' && (flags & ImGuiInputTextFlags_Multiline));
  6150. pass |= (c == '\t' && (flags & ImGuiInputTextFlags_AllowTabInput));
  6151. if (!pass)
  6152. return false;
  6153. }
  6154. if (c >= 0xE000 && c <= 0xF8FF) // Filter private Unicode range. I don't imagine anybody would want to input them. GLFW on OSX seems to send private characters for special keys like arrow keys.
  6155. return false;
  6156. if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_CharsNoBlank))
  6157. {
  6158. if (flags & ImGuiInputTextFlags_CharsDecimal)
  6159. if (!(c >= '0' && c <= '9') && (c != '.') && (c != '-') && (c != '+') && (c != '*') && (c != '/'))
  6160. return false;
  6161. if (flags & ImGuiInputTextFlags_CharsHexadecimal)
  6162. if (!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F'))
  6163. return false;
  6164. if (flags & ImGuiInputTextFlags_CharsUppercase)
  6165. if (c >= 'a' && c <= 'z')
  6166. *p_char = (c += (unsigned int)('A'-'a'));
  6167. if (flags & ImGuiInputTextFlags_CharsNoBlank)
  6168. if (ImCharIsSpace(c))
  6169. return false;
  6170. }
  6171. if (flags & ImGuiInputTextFlags_CallbackCharFilter)
  6172. {
  6173. ImGuiTextEditCallbackData callback_data;
  6174. memset(&callback_data, 0, sizeof(ImGuiTextEditCallbackData));
  6175. callback_data.EventFlag = ImGuiInputTextFlags_CallbackCharFilter;
  6176. callback_data.EventChar = (ImWchar)c;
  6177. callback_data.Flags = flags;
  6178. callback_data.UserData = user_data;
  6179. if (callback(&callback_data) != 0)
  6180. return false;
  6181. *p_char = callback_data.EventChar;
  6182. if (!callback_data.EventChar)
  6183. return false;
  6184. }
  6185. return true;
  6186. }
  6187. // Edit a string of text
  6188. static bool InputTextEx(const char* label, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiTextEditCallback callback, void* user_data)
  6189. {
  6190. ImGuiWindow* window = GetCurrentWindow();
  6191. if (window->SkipItems)
  6192. return false;
  6193. IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackHistory) && (flags & ImGuiInputTextFlags_Multiline))); // Can't use both together (they both use up/down keys)
  6194. IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackCompletion) && (flags & ImGuiInputTextFlags_AllowTabInput))); // Can't use both together (they both use tab key)
  6195. ImGuiState& g = *GImGui;
  6196. const ImGuiIO& io = g.IO;
  6197. const ImGuiStyle& style = g.Style;
  6198. const ImGuiID id = window->GetID(label);
  6199. const bool is_multiline = (flags & ImGuiInputTextFlags_Multiline) != 0;
  6200. ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true);
  6201. ImVec2 size = CalcItemSize(size_arg, ImGui::CalcItemWidth(), is_multiline ? ImGui::GetTextLineHeight() * 8.0f : label_size.y); // Arbitrary default of 8 lines high for multi-line
  6202. const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + size + style.FramePadding*2.0f);
  6203. const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? (style.ItemInnerSpacing.x + label_size.x) : 0.0f, 0.0f));
  6204. ImGuiWindow* draw_window = window;
  6205. if (is_multiline)
  6206. {
  6207. ImGui::BeginGroup();
  6208. if (!ImGui::BeginChildFrame(id, frame_bb.GetSize()))
  6209. {
  6210. ImGui::EndChildFrame();
  6211. ImGui::EndGroup();
  6212. return false;
  6213. }
  6214. draw_window = GetCurrentWindow();
  6215. draw_window->DC.CursorPos += style.FramePadding;
  6216. size.x -= draw_window->ScrollbarWidth();
  6217. }
  6218. else
  6219. {
  6220. ItemSize(total_bb, style.FramePadding.y);
  6221. if (!ItemAdd(total_bb, &id))
  6222. return false;
  6223. }
  6224. // NB: we are only allowed to access 'edit_state' if we are the active widget.
  6225. ImGuiTextEditState& edit_state = g.InputTextState;
  6226. const bool is_ctrl_down = io.KeyCtrl;
  6227. const bool is_shift_down = io.KeyShift;
  6228. const bool is_alt_down = io.KeyAlt;
  6229. const bool focus_requested = window->FocusItemRegister(g.ActiveId == id, (flags & (ImGuiInputTextFlags_CallbackCompletion|ImGuiInputTextFlags_AllowTabInput)) == 0); // Using completion callback disable keyboard tabbing
  6230. const bool focus_requested_by_code = focus_requested && (window->FocusIdxAllCounter == window->FocusIdxAllRequestCurrent);
  6231. const bool focus_requested_by_tab = focus_requested && !focus_requested_by_code;
  6232. const bool hovered = IsHovered(frame_bb, id);
  6233. if (hovered)
  6234. {
  6235. g.HoveredId = id;
  6236. g.MouseCursor = ImGuiMouseCursor_TextInput;
  6237. }
  6238. const bool user_clicked = hovered && io.MouseClicked[0];
  6239. const bool user_scrolled = is_multiline && g.ActiveId == 0 && edit_state.Id == id && g.ActiveIdPreviousFrame == draw_window->GetID("#SCROLLY");
  6240. bool select_all = (g.ActiveId != id) && (flags & ImGuiInputTextFlags_AutoSelectAll) != 0;
  6241. if (focus_requested || user_clicked || user_scrolled)
  6242. {
  6243. if (g.ActiveId != id)
  6244. {
  6245. // Start edition
  6246. // Take a copy of the initial buffer value (both in original UTF-8 format and converted to wchar)
  6247. // From the moment we focused we are ignoring the content of 'buf'
  6248. edit_state.Text.resize(buf_size); // wchar count <= utf-8 count
  6249. edit_state.InitialText.resize(buf_size); // utf-8
  6250. ImFormatString(edit_state.InitialText.Data, edit_state.InitialText.Size, "%s", buf);
  6251. const char* buf_end = NULL;
  6252. edit_state.CurLenW = ImTextStrFromUtf8(edit_state.Text.Data, edit_state.Text.Size, buf, NULL, &buf_end);
  6253. edit_state.CurLenA = (int)(buf_end - buf); // We can't get the result from ImFormatString() above because it is not UTF-8 aware. Here we'll cut off malformed UTF-8.
  6254. edit_state.InputCursorScreenPos = ImVec2(-1.f, -1.f);
  6255. edit_state.CursorAnimReset();
  6256. if (edit_state.Id != id)
  6257. {
  6258. edit_state.Id = id;
  6259. edit_state.ScrollX = 0.f;
  6260. stb_textedit_initialize_state(&edit_state.StbState, !is_multiline);
  6261. if (!is_multiline && focus_requested_by_code)
  6262. select_all = true;
  6263. }
  6264. else
  6265. {
  6266. // Recycle existing cursor/selection/undo stack but clamp position
  6267. // Note a single mouse click will override the cursor/position immediately by calling stb_textedit_click handler.
  6268. edit_state.StbState.cursor = ImMin(edit_state.StbState.cursor, edit_state.CurLenW);
  6269. edit_state.StbState.select_start = ImMin(edit_state.StbState.select_start, edit_state.CurLenW);
  6270. edit_state.StbState.select_end = ImMin(edit_state.StbState.select_end, edit_state.CurLenW);
  6271. }
  6272. if (flags & ImGuiInputTextFlags_AlwaysInsertMode)
  6273. edit_state.StbState.insert_mode = true;
  6274. if (!is_multiline && (focus_requested_by_tab || (user_clicked && is_ctrl_down)))
  6275. select_all = true;
  6276. }
  6277. SetActiveId(id, window);
  6278. FocusWindow(window);
  6279. }
  6280. else if (io.MouseClicked[0])
  6281. {
  6282. // Release focus when we click outside
  6283. if (g.ActiveId == id)
  6284. SetActiveId(0);
  6285. }
  6286. bool value_changed = false;
  6287. bool cancel_edit = false;
  6288. bool enter_pressed = false;
  6289. if (g.ActiveId == id)
  6290. {
  6291. edit_state.BufSizeA = buf_size;
  6292. // Although we are active we don't prevent mouse from hovering other elements unless we are interacting right now with the widget.
  6293. // Down the line we should have a cleaner concept of focused vs active in the library.
  6294. g.ActiveIdIsFocusedOnly = !io.MouseDown[0];
  6295. // Edit in progress
  6296. const float mouse_x = (g.IO.MousePos.x - frame_bb.Min.x - style.FramePadding.x) + edit_state.ScrollX;
  6297. const float mouse_y = (is_multiline ? (g.IO.MousePos.y - draw_window->DC.CursorPos.y - style.FramePadding.y) : (g.FontSize*0.5f));
  6298. if (select_all || (hovered && io.MouseDoubleClicked[0]))
  6299. {
  6300. edit_state.SelectAll();
  6301. edit_state.SelectedAllMouseLock = true;
  6302. }
  6303. else if (io.MouseClicked[0] && !edit_state.SelectedAllMouseLock)
  6304. {
  6305. stb_textedit_click(&edit_state, &edit_state.StbState, mouse_x, mouse_y);
  6306. edit_state.CursorAnimReset();
  6307. }
  6308. else if (io.MouseDown[0] && !edit_state.SelectedAllMouseLock)
  6309. {
  6310. stb_textedit_drag(&edit_state, &edit_state.StbState, mouse_x, mouse_y);
  6311. edit_state.CursorAnimReset();
  6312. }
  6313. if (edit_state.SelectedAllMouseLock && !io.MouseDown[0])
  6314. edit_state.SelectedAllMouseLock = false;
  6315. if (g.IO.InputCharacters[0])
  6316. {
  6317. // Process text input (before we check for Return because using some IME will effectively send a Return?)
  6318. for (int n = 0; n < IM_ARRAYSIZE(g.IO.InputCharacters) && g.IO.InputCharacters[n]; n++)
  6319. {
  6320. if (unsigned int c = (unsigned int)g.IO.InputCharacters[n])
  6321. {
  6322. // Insert character if they pass filtering
  6323. if (!InputTextFilterCharacter(&c, flags, callback, user_data))
  6324. continue;
  6325. edit_state.OnKeyPressed((int)c);
  6326. }
  6327. }
  6328. // Consume characters
  6329. memset(g.IO.InputCharacters, 0, sizeof(g.IO.InputCharacters));
  6330. }
  6331. const int k_mask = (is_shift_down ? STB_TEXTEDIT_K_SHIFT : 0);
  6332. const bool is_ctrl_only = is_ctrl_down && !is_alt_down && !is_shift_down;
  6333. if (IsKeyPressedMap(ImGuiKey_LeftArrow)) { edit_state.OnKeyPressed(is_ctrl_down ? STB_TEXTEDIT_K_WORDLEFT | k_mask : STB_TEXTEDIT_K_LEFT | k_mask); }
  6334. else if (IsKeyPressedMap(ImGuiKey_RightArrow)) { edit_state.OnKeyPressed(is_ctrl_down ? STB_TEXTEDIT_K_WORDRIGHT | k_mask : STB_TEXTEDIT_K_RIGHT | k_mask); }
  6335. else if (is_multiline && IsKeyPressedMap(ImGuiKey_UpArrow)) { if (is_ctrl_down) SetWindowScrollY(draw_window, draw_window->ScrollY - g.FontSize); else edit_state.OnKeyPressed(STB_TEXTEDIT_K_UP | k_mask); }
  6336. else if (is_multiline && IsKeyPressedMap(ImGuiKey_DownArrow)) { if (is_ctrl_down) SetWindowScrollY(draw_window, draw_window->ScrollY + g.FontSize); else edit_state.OnKeyPressed(STB_TEXTEDIT_K_DOWN| k_mask); }
  6337. else if (IsKeyPressedMap(ImGuiKey_Home)) { edit_state.OnKeyPressed(is_ctrl_down ? STB_TEXTEDIT_K_TEXTSTART | k_mask : STB_TEXTEDIT_K_LINESTART | k_mask); }
  6338. else if (IsKeyPressedMap(ImGuiKey_End)) { edit_state.OnKeyPressed(is_ctrl_down ? STB_TEXTEDIT_K_TEXTEND | k_mask : STB_TEXTEDIT_K_LINEEND | k_mask); }
  6339. else if (IsKeyPressedMap(ImGuiKey_Delete)) { edit_state.OnKeyPressed(STB_TEXTEDIT_K_DELETE | k_mask); }
  6340. else if (IsKeyPressedMap(ImGuiKey_Backspace)) { edit_state.OnKeyPressed(STB_TEXTEDIT_K_BACKSPACE | k_mask); }
  6341. else if (IsKeyPressedMap(ImGuiKey_Enter))
  6342. {
  6343. bool ctrl_enter_for_new_line = (flags & ImGuiInputTextFlags_CtrlEnterForNewLine) != 0;
  6344. if (!is_multiline || (ctrl_enter_for_new_line && !is_ctrl_down) || (!ctrl_enter_for_new_line && is_ctrl_down))
  6345. {
  6346. SetActiveId(0);
  6347. enter_pressed = true;
  6348. }
  6349. else // New line
  6350. {
  6351. unsigned int c = '\n';
  6352. if (InputTextFilterCharacter(&c, flags, callback, user_data))
  6353. edit_state.OnKeyPressed((int)c);
  6354. }
  6355. }
  6356. else if ((flags & ImGuiInputTextFlags_AllowTabInput) && IsKeyPressedMap(ImGuiKey_Tab) && !is_ctrl_down && !is_shift_down && !is_alt_down)
  6357. {
  6358. unsigned int c = '\t';
  6359. if (InputTextFilterCharacter(&c, flags, callback, user_data))
  6360. edit_state.OnKeyPressed((int)c);
  6361. }
  6362. else if (IsKeyPressedMap(ImGuiKey_Escape)) { SetActiveId(0); cancel_edit = true; }
  6363. else if (is_ctrl_only && IsKeyPressedMap(ImGuiKey_Z)) { edit_state.OnKeyPressed(STB_TEXTEDIT_K_UNDO); edit_state.ClearSelection(); }
  6364. else if (is_ctrl_only && IsKeyPressedMap(ImGuiKey_Y)) { edit_state.OnKeyPressed(STB_TEXTEDIT_K_REDO); edit_state.ClearSelection(); }
  6365. else if (is_ctrl_only && IsKeyPressedMap(ImGuiKey_A)) { edit_state.SelectAll(); edit_state.CursorFollow = true; }
  6366. else if (is_ctrl_only && (IsKeyPressedMap(ImGuiKey_X) || IsKeyPressedMap(ImGuiKey_C)) && (!is_multiline || edit_state.HasSelection()))
  6367. {
  6368. // Cut, Copy
  6369. const bool cut = IsKeyPressedMap(ImGuiKey_X);
  6370. if (cut && !edit_state.HasSelection())
  6371. edit_state.SelectAll();
  6372. if (g.IO.SetClipboardTextFn)
  6373. {
  6374. const int ib = edit_state.HasSelection() ? ImMin(edit_state.StbState.select_start, edit_state.StbState.select_end) : 0;
  6375. const int ie = edit_state.HasSelection() ? ImMax(edit_state.StbState.select_start, edit_state.StbState.select_end) : edit_state.CurLenW;
  6376. edit_state.TempTextBuffer.resize((ie-ib) * 4 + 1);
  6377. ImTextStrToUtf8(edit_state.TempTextBuffer.Data, edit_state.TempTextBuffer.Size, edit_state.Text.Data+ib, edit_state.Text.Data+ie);
  6378. g.IO.SetClipboardTextFn(edit_state.TempTextBuffer.Data);
  6379. }
  6380. if (cut)
  6381. {
  6382. edit_state.CursorFollow = true;
  6383. stb_textedit_cut(&edit_state, &edit_state.StbState);
  6384. }
  6385. }
  6386. else if (is_ctrl_only && IsKeyPressedMap(ImGuiKey_V))
  6387. {
  6388. // Paste
  6389. if (g.IO.GetClipboardTextFn)
  6390. {
  6391. if (const char* clipboard = g.IO.GetClipboardTextFn())
  6392. {
  6393. // Remove new-line from pasted buffer
  6394. const int clipboard_len = (int)strlen(clipboard);
  6395. ImWchar* clipboard_filtered = (ImWchar*)ImGui::MemAlloc((clipboard_len+1) * sizeof(ImWchar));
  6396. int clipboard_filtered_len = 0;
  6397. for (const char* s = clipboard; *s; )
  6398. {
  6399. unsigned int c;
  6400. s += ImTextCharFromUtf8(&c, s, NULL);
  6401. if (c == 0)
  6402. break;
  6403. if (c >= 0x10000 || !InputTextFilterCharacter(&c, flags, callback, user_data))
  6404. continue;
  6405. clipboard_filtered[clipboard_filtered_len++] = (ImWchar)c;
  6406. }
  6407. clipboard_filtered[clipboard_filtered_len] = 0;
  6408. if (clipboard_filtered_len > 0) // If everything was filtered, ignore the pasting operation
  6409. {
  6410. stb_textedit_paste(&edit_state, &edit_state.StbState, clipboard_filtered, clipboard_filtered_len);
  6411. edit_state.CursorFollow = true;
  6412. }
  6413. ImGui::MemFree(clipboard_filtered);
  6414. }
  6415. }
  6416. }
  6417. if (cancel_edit)
  6418. {
  6419. // Restore initial value
  6420. ImFormatString(buf, buf_size, "%s", edit_state.InitialText.Data);
  6421. value_changed = true;
  6422. }
  6423. else
  6424. {
  6425. // Apply new value immediately - copy modified buffer back
  6426. // Note that as soon as we can focus into the input box, the in-widget value gets priority over any underlying modification of the input buffer
  6427. // FIXME: We actually always render 'buf' in RenderTextScrolledClipped
  6428. // FIXME-OPT: CPU waste to do this every time the widget is active, should mark dirty state from the stb_textedit callbacks
  6429. edit_state.TempTextBuffer.resize(edit_state.Text.Size * 4);
  6430. ImTextStrToUtf8(edit_state.TempTextBuffer.Data, edit_state.TempTextBuffer.Size, edit_state.Text.Data, NULL);
  6431. // User callback
  6432. if ((flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory | ImGuiInputTextFlags_CallbackAlways)) != 0)
  6433. {
  6434. IM_ASSERT(callback != NULL);
  6435. // The reason we specify the usage semantic (Completion/History) is that Completion needs to disable keyboard TABBING at the moment.
  6436. ImGuiInputTextFlags event_flag = 0;
  6437. ImGuiKey event_key = ImGuiKey_COUNT;
  6438. if ((flags & ImGuiInputTextFlags_CallbackCompletion) != 0 && IsKeyPressedMap(ImGuiKey_Tab))
  6439. {
  6440. event_flag = ImGuiInputTextFlags_CallbackCompletion;
  6441. event_key = ImGuiKey_Tab;
  6442. }
  6443. else if ((flags & ImGuiInputTextFlags_CallbackHistory) != 0 && IsKeyPressedMap(ImGuiKey_UpArrow))
  6444. {
  6445. event_flag = ImGuiInputTextFlags_CallbackHistory;
  6446. event_key = ImGuiKey_UpArrow;
  6447. }
  6448. else if ((flags & ImGuiInputTextFlags_CallbackHistory) != 0 && IsKeyPressedMap(ImGuiKey_DownArrow))
  6449. {
  6450. event_flag = ImGuiInputTextFlags_CallbackHistory;
  6451. event_key = ImGuiKey_DownArrow;
  6452. }
  6453. if (event_key != ImGuiKey_COUNT || (flags & ImGuiInputTextFlags_CallbackAlways) != 0)
  6454. {
  6455. ImGuiTextEditCallbackData callback_data;
  6456. callback_data.EventFlag = event_flag;
  6457. callback_data.EventKey = event_key;
  6458. callback_data.Buf = edit_state.TempTextBuffer.Data;
  6459. callback_data.BufSize = edit_state.BufSizeA;
  6460. callback_data.BufDirty = false;
  6461. callback_data.Flags = flags;
  6462. callback_data.UserData = user_data;
  6463. // We have to convert from position from wchar to UTF-8 positions
  6464. ImWchar* text = edit_state.Text.Data;
  6465. const int utf8_cursor_pos = callback_data.CursorPos = ImTextCountUtf8BytesFromStr(text, text + edit_state.StbState.cursor);
  6466. const int utf8_selection_start = callback_data.SelectionStart = ImTextCountUtf8BytesFromStr(text, text + edit_state.StbState.select_start);
  6467. const int utf8_selection_end = callback_data.SelectionEnd = ImTextCountUtf8BytesFromStr(text, text + edit_state.StbState.select_end);
  6468. // Call user code
  6469. callback(&callback_data);
  6470. // Read back what user may have modified
  6471. IM_ASSERT(callback_data.Buf == edit_state.TempTextBuffer.Data); // Invalid to modify those fields
  6472. IM_ASSERT(callback_data.BufSize == edit_state.BufSizeA);
  6473. IM_ASSERT(callback_data.Flags == flags);
  6474. if (callback_data.CursorPos != utf8_cursor_pos) edit_state.StbState.cursor = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.CursorPos);
  6475. if (callback_data.SelectionStart != utf8_selection_start) edit_state.StbState.select_start = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionStart);
  6476. if (callback_data.SelectionEnd != utf8_selection_end) edit_state.StbState.select_end = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionEnd);
  6477. if (callback_data.BufDirty)
  6478. {
  6479. edit_state.CurLenW = ImTextStrFromUtf8(text, edit_state.Text.Size, edit_state.TempTextBuffer.Data, NULL);
  6480. edit_state.CurLenA = (int)strlen(edit_state.TempTextBuffer.Data);
  6481. edit_state.CursorAnimReset();
  6482. }
  6483. }
  6484. }
  6485. if (strcmp(edit_state.TempTextBuffer.Data, buf) != 0)
  6486. {
  6487. ImFormatString(buf, buf_size, "%s", edit_state.TempTextBuffer.Data);
  6488. value_changed = true;
  6489. }
  6490. }
  6491. }
  6492. if (!is_multiline)
  6493. RenderFrame(frame_bb.Min, frame_bb.Max, window->Color(ImGuiCol_FrameBg), true, style.FrameRounding);
  6494. ImVec2 render_pos = is_multiline ? draw_window->DC.CursorPos : frame_bb.Min + style.FramePadding;
  6495. ImVec4 clip_rect(frame_bb.Min.x, frame_bb.Min.y, frame_bb.Min.x + size.x + style.FramePadding.x*2.0f, frame_bb.Min.y + size.y + style.FramePadding.y*2.0f);
  6496. ImVec2 text_size(0.f, 0.f);
  6497. if (g.ActiveId == id || (edit_state.Id == id && is_multiline && g.ActiveId == draw_window->GetID("#SCROLLY")))
  6498. {
  6499. edit_state.CursorAnim += g.IO.DeltaTime;
  6500. // We need to:
  6501. // - Display the text (this can be more easily clipped)
  6502. // - Handle scrolling, highlight selection, display cursor (those all requires some form of 1d->2d cursor position calculation)
  6503. // - Measure text height (for scrollbar)
  6504. // We are attempting to do most of that in one main pass to minimize the computation cost (non-negligible for large amount of text) + 2nd pass for selection rendering (we could merge them by an extra refactoring effort)
  6505. const ImWchar* text_begin = edit_state.Text.Data;
  6506. const ImWchar* text_end = text_begin + edit_state.CurLenW;
  6507. ImVec2 cursor_offset, select_start_offset;
  6508. {
  6509. // Count lines + find lines numbers of cursor and select_start
  6510. int matches_remaining = 0;
  6511. int matches_line_no[2] = { -1, -999 };
  6512. const ImWchar* matches_ptr[2] = { NULL, NULL };
  6513. matches_ptr[0] = text_begin + edit_state.StbState.cursor; matches_remaining++;
  6514. if (edit_state.StbState.select_start != edit_state.StbState.select_end)
  6515. {
  6516. matches_ptr[1] = text_begin + ImMin(edit_state.StbState.select_start, edit_state.StbState.select_end);
  6517. matches_line_no[1] = -1;
  6518. matches_remaining++;
  6519. }
  6520. matches_remaining += is_multiline ? 1 : 0; // So that we never exit the loop until all lines are counted.
  6521. int line_count = 0;
  6522. for (const ImWchar* s = text_begin; s < text_end+1; s++)
  6523. if ((*s) == '\n' || s == text_end)
  6524. {
  6525. line_count++;
  6526. if (matches_line_no[0] == -1 && s >= matches_ptr[0]) { matches_line_no[0] = line_count; if (--matches_remaining <= 0) break; }
  6527. if (matches_line_no[1] == -1 && s >= matches_ptr[1]) { matches_line_no[1] = line_count; if (--matches_remaining <= 0) break; }
  6528. }
  6529. // Calculate 2d position
  6530. IM_ASSERT(matches_line_no[0] != -1);
  6531. cursor_offset.x = InputTextCalcTextSizeW(ImStrbolW(matches_ptr[0], text_begin), matches_ptr[0]).x;
  6532. cursor_offset.y = matches_line_no[0] * g.FontSize;
  6533. if (matches_line_no[1] >= 0)
  6534. {
  6535. select_start_offset.x = InputTextCalcTextSizeW(ImStrbolW(matches_ptr[1], text_begin), matches_ptr[1]).x;
  6536. select_start_offset.y = matches_line_no[1] * g.FontSize;
  6537. }
  6538. // Calculate text height
  6539. if (is_multiline)
  6540. text_size = ImVec2(size.x, line_count * g.FontSize);
  6541. }
  6542. // Scroll
  6543. if (edit_state.CursorFollow)
  6544. {
  6545. // Horizontal scroll in chunks of quarter width
  6546. if (!(flags & ImGuiInputTextFlags_NoHorizontalScroll))
  6547. {
  6548. const float scroll_increment_x = size.x * 0.25f;
  6549. if (cursor_offset.x < edit_state.ScrollX)
  6550. edit_state.ScrollX = ImMax(0.0f, cursor_offset.x - scroll_increment_x);
  6551. else if (cursor_offset.x - size.x >= edit_state.ScrollX)
  6552. edit_state.ScrollX = cursor_offset.x - size.x + scroll_increment_x;
  6553. }
  6554. else
  6555. {
  6556. edit_state.ScrollX = 0.0f;
  6557. }
  6558. // Vertical scroll
  6559. if (is_multiline)
  6560. {
  6561. float scroll_y = draw_window->ScrollY;
  6562. if (cursor_offset.y - g.FontSize < scroll_y)
  6563. scroll_y = ImMax(0.0f, cursor_offset.y - g.FontSize);
  6564. else if (cursor_offset.y - size.y >= scroll_y)
  6565. scroll_y = cursor_offset.y - size.y;
  6566. draw_window->DC.CursorPos.y += (draw_window->ScrollY - scroll_y); // To avoid a frame of lag
  6567. draw_window->ScrollY = scroll_y;
  6568. render_pos.y = draw_window->DC.CursorPos.y;
  6569. }
  6570. }
  6571. edit_state.CursorFollow = false;
  6572. ImVec2 render_scroll = ImVec2(edit_state.ScrollX, 0.0f);
  6573. // Draw selection
  6574. if (edit_state.StbState.select_start != edit_state.StbState.select_end)
  6575. {
  6576. const ImWchar* text_selected_begin = text_begin + ImMin(edit_state.StbState.select_start, edit_state.StbState.select_end);
  6577. const ImWchar* text_selected_end = text_begin + ImMax(edit_state.StbState.select_start, edit_state.StbState.select_end);
  6578. float bg_offy_up = is_multiline ? 0.0f : -1.0f; // FIXME: those offsets should be part of the style? they don't play so well with multi-line selection.
  6579. float bg_offy_dn = is_multiline ? 0.0f : 2.0f;
  6580. ImU32 bg_color = draw_window->Color(ImGuiCol_TextSelectedBg);
  6581. ImVec2 rect_pos = render_pos + select_start_offset - render_scroll;
  6582. for (const ImWchar* p = text_selected_begin; p < text_selected_end; )
  6583. {
  6584. if (rect_pos.y > clip_rect.w + g.FontSize)
  6585. break;
  6586. if (rect_pos.y < clip_rect.y)
  6587. {
  6588. while (p < text_selected_end)
  6589. if (*p++ == '\n')
  6590. break;
  6591. }
  6592. else
  6593. {
  6594. ImVec2 rect_size = InputTextCalcTextSizeW(p, text_selected_end, &p, NULL, true);
  6595. if (rect_size.x <= 0.0f) rect_size.x = (float)(int)(g.Font->GetCharAdvance((unsigned short)' ') * 0.50f); // So we can see selected empty lines
  6596. ImRect rect(rect_pos + ImVec2(0.0f, bg_offy_up - g.FontSize), rect_pos +ImVec2(rect_size.x, bg_offy_dn));
  6597. rect.Clip(clip_rect);
  6598. if (rect.Overlaps(clip_rect))
  6599. draw_window->DrawList->AddRectFilled(rect.Min, rect.Max, bg_color);
  6600. }
  6601. rect_pos.x = render_pos.x - render_scroll.x;
  6602. rect_pos.y += g.FontSize;
  6603. }
  6604. }
  6605. draw_window->DrawList->AddText(g.Font, g.FontSize, render_pos - render_scroll, draw_window->Color(ImGuiCol_Text), buf, buf+edit_state.CurLenA, 0.0f, is_multiline ? NULL : &clip_rect);
  6606. // Draw blinking cursor
  6607. ImVec2 cursor_screen_pos = render_pos + cursor_offset - render_scroll;
  6608. if (g.InputTextState.CursorIsVisible())
  6609. draw_window->DrawList->AddLine(cursor_screen_pos + ImVec2(0.0f,-g.FontSize+0.5f), cursor_screen_pos + ImVec2(0.0f,-1.5f), window->Color(ImGuiCol_Text));
  6610. // Notify OS of text input position for advanced IME
  6611. if (io.ImeSetInputScreenPosFn && ImLengthSqr(edit_state.InputCursorScreenPos - cursor_screen_pos) > 0.0001f)
  6612. io.ImeSetInputScreenPosFn((int)cursor_screen_pos.x - 1, (int)(cursor_screen_pos.y - g.FontSize)); // -1 x offset so that Windows IME can cover our cursor. Bit of an extra nicety.
  6613. edit_state.InputCursorScreenPos = cursor_screen_pos;
  6614. }
  6615. else
  6616. {
  6617. // Render text only
  6618. const char* buf_end = NULL;
  6619. if (is_multiline)
  6620. text_size = ImVec2(size.x, InputTextCalcTextLenAndLineCount(buf, &buf_end) * g.FontSize); // We don't need width
  6621. draw_window->DrawList->AddText(g.Font, g.FontSize, render_pos, draw_window->Color(ImGuiCol_Text), buf, buf_end, 0.0f, is_multiline ? NULL : &clip_rect);
  6622. }
  6623. if (is_multiline)
  6624. {
  6625. ImGui::Dummy(text_size + ImVec2(0.0f, g.FontSize)); // Always add room to scroll an extra line
  6626. ImGui::EndChildFrame();
  6627. ImGui::EndGroup();
  6628. }
  6629. // Log as text
  6630. if (GImGui->LogEnabled)
  6631. LogText(render_pos, buf, NULL);
  6632. if (label_size.x > 0)
  6633. RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);
  6634. if ((flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0)
  6635. return enter_pressed;
  6636. else
  6637. return value_changed;
  6638. }
  6639. bool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags, ImGuiTextEditCallback callback, void* user_data)
  6640. {
  6641. IM_ASSERT(!(flags & ImGuiInputTextFlags_Multiline)); // call InputTextMultiline()
  6642. bool ret = InputTextEx(label, buf, (int)buf_size, ImVec2(0,0), flags, callback, user_data);
  6643. return ret;
  6644. }
  6645. bool ImGui::InputTextMultiline(const char* label, char* buf, size_t buf_size, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiTextEditCallback callback, void* user_data)
  6646. {
  6647. bool ret = InputTextEx(label, buf, (int)buf_size, size, flags | ImGuiInputTextFlags_Multiline, callback, user_data);
  6648. return ret;
  6649. }
  6650. bool ImGui::InputFloat(const char* label, float *v, float step, float step_fast, int decimal_precision, ImGuiInputTextFlags extra_flags)
  6651. {
  6652. ImGuiState& g = *GImGui;
  6653. ImGuiWindow* window = GetCurrentWindow();
  6654. if (window->SkipItems)
  6655. return false;
  6656. const ImGuiStyle& style = g.Style;
  6657. const float w = ImGui::CalcItemWidth();
  6658. const ImVec2 label_size = CalcTextSize(label, NULL, true);
  6659. const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y) + style.FramePadding*2.0f);
  6660. ImGui::BeginGroup();
  6661. ImGui::PushID(label);
  6662. const ImVec2 button_sz = ImVec2(g.FontSize, g.FontSize) + style.FramePadding * 2;
  6663. if (step > 0.0f)
  6664. ImGui::PushItemWidth(ImMax(1.0f, w - (button_sz.x + style.ItemInnerSpacing.x)*2));
  6665. char buf[64];
  6666. if (decimal_precision < 0)
  6667. ImFormatString(buf, IM_ARRAYSIZE(buf), "%f", *v); // Ideally we'd have a minimum decimal precision of 1 to visually denote that it is a float, while hiding non-significant digits?
  6668. else
  6669. ImFormatString(buf, IM_ARRAYSIZE(buf), "%.*f", decimal_precision, *v);
  6670. bool value_changed = false;
  6671. const ImGuiInputTextFlags flags = extra_flags | (ImGuiInputTextFlags_CharsDecimal|ImGuiInputTextFlags_AutoSelectAll);
  6672. if (ImGui::InputText("", buf, IM_ARRAYSIZE(buf), flags))
  6673. {
  6674. InputTextApplyArithmeticOp(buf, v);
  6675. value_changed = true;
  6676. }
  6677. // Step buttons
  6678. if (step > 0.0f)
  6679. {
  6680. ImGui::PopItemWidth();
  6681. ImGui::SameLine(0, style.ItemInnerSpacing.x);
  6682. if (ButtonEx("-", button_sz, ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups))
  6683. {
  6684. *v -= g.IO.KeyCtrl && step_fast > 0.0f ? step_fast : step;
  6685. value_changed = true;
  6686. }
  6687. ImGui::SameLine(0, style.ItemInnerSpacing.x);
  6688. if (ButtonEx("+", button_sz, ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups))
  6689. {
  6690. *v += g.IO.KeyCtrl && step_fast > 0.0f ? step_fast : step;
  6691. value_changed = true;
  6692. }
  6693. }
  6694. ImGui::PopID();
  6695. if (label_size.x > 0)
  6696. {
  6697. ImGui::SameLine(0, style.ItemInnerSpacing.x);
  6698. RenderText(ImVec2(window->DC.CursorPos.x, window->DC.CursorPos.y + style.FramePadding.y), label);
  6699. ItemSize(label_size, style.FramePadding.y);
  6700. }
  6701. ImGui::EndGroup();
  6702. return value_changed;
  6703. }
  6704. bool ImGui::InputInt(const char* label, int *v, int step, int step_fast, ImGuiInputTextFlags extra_flags)
  6705. {
  6706. float f = (float)*v;
  6707. const bool value_changed = ImGui::InputFloat(label, &f, (float)step, (float)step_fast, 0, extra_flags);
  6708. if (value_changed)
  6709. *v = (int)f;
  6710. return value_changed;
  6711. }
  6712. static bool InputFloatN(const char* label, float* v, int components, int decimal_precision, ImGuiInputTextFlags extra_flags)
  6713. {
  6714. ImGuiState& g = *GImGui;
  6715. ImGuiWindow* window = GetCurrentWindow();
  6716. if (window->SkipItems)
  6717. return false;
  6718. bool value_changed = false;
  6719. ImGui::BeginGroup();
  6720. ImGui::PushID(label);
  6721. PushMultiItemsWidths(components);
  6722. for (int i = 0; i < components; i++)
  6723. {
  6724. ImGui::PushID(i);
  6725. value_changed |= ImGui::InputFloat("##v", &v[i], 0, 0, decimal_precision, extra_flags);
  6726. ImGui::SameLine(0, g.Style.ItemInnerSpacing.x);
  6727. ImGui::PopID();
  6728. ImGui::PopItemWidth();
  6729. }
  6730. ImGui::PopID();
  6731. window->DC.CurrentLineTextBaseOffset = ImMax(window->DC.CurrentLineTextBaseOffset, g.Style.FramePadding.y);
  6732. ImGui::TextUnformatted(label, FindTextDisplayEnd(label));
  6733. ImGui::EndGroup();
  6734. return value_changed;
  6735. }
  6736. bool ImGui::InputFloat2(const char* label, float v[2], int decimal_precision, ImGuiInputTextFlags extra_flags)
  6737. {
  6738. return InputFloatN(label, v, 2, decimal_precision, extra_flags);
  6739. }
  6740. bool ImGui::InputFloat3(const char* label, float v[3], int decimal_precision, ImGuiInputTextFlags extra_flags)
  6741. {
  6742. return InputFloatN(label, v, 3, decimal_precision, extra_flags);
  6743. }
  6744. bool ImGui::InputFloat4(const char* label, float v[4], int decimal_precision, ImGuiInputTextFlags extra_flags)
  6745. {
  6746. return InputFloatN(label, v, 4, decimal_precision, extra_flags);
  6747. }
  6748. static bool InputIntN(const char* label, int* v, int components, ImGuiInputTextFlags extra_flags)
  6749. {
  6750. ImGuiState& g = *GImGui;
  6751. ImGuiWindow* window = GetCurrentWindow();
  6752. if (window->SkipItems)
  6753. return false;
  6754. bool value_changed = false;
  6755. ImGui::BeginGroup();
  6756. ImGui::PushID(label);
  6757. PushMultiItemsWidths(components);
  6758. for (int i = 0; i < components; i++)
  6759. {
  6760. ImGui::PushID(i);
  6761. value_changed |= ImGui::InputInt("##v", &v[i], 0, 0, extra_flags);
  6762. ImGui::SameLine(0, g.Style.ItemInnerSpacing.x);
  6763. ImGui::PopID();
  6764. ImGui::PopItemWidth();
  6765. }
  6766. ImGui::PopID();
  6767. window->DC.CurrentLineTextBaseOffset = ImMax(window->DC.CurrentLineTextBaseOffset, g.Style.FramePadding.y);
  6768. ImGui::TextUnformatted(label, FindTextDisplayEnd(label));
  6769. ImGui::EndGroup();
  6770. return value_changed;
  6771. }
  6772. bool ImGui::InputInt2(const char* label, int v[2], ImGuiInputTextFlags extra_flags)
  6773. {
  6774. return InputIntN(label, v, 2, extra_flags);
  6775. }
  6776. bool ImGui::InputInt3(const char* label, int v[3], ImGuiInputTextFlags extra_flags)
  6777. {
  6778. return InputIntN(label, v, 3, extra_flags);
  6779. }
  6780. bool ImGui::InputInt4(const char* label, int v[4], ImGuiInputTextFlags extra_flags)
  6781. {
  6782. return InputIntN(label, v, 4, extra_flags);
  6783. }
  6784. static bool Items_ArrayGetter(void* data, int idx, const char** out_text)
  6785. {
  6786. const char** items = (const char**)data;
  6787. if (out_text)
  6788. *out_text = items[idx];
  6789. return true;
  6790. }
  6791. static bool Items_SingleStringGetter(void* data, int idx, const char** out_text)
  6792. {
  6793. // FIXME-OPT: we could pre-compute the indices to fasten this. But only 1 active combo means the waste is limited.
  6794. const char* items_separated_by_zeros = (const char*)data;
  6795. int items_count = 0;
  6796. const char* p = items_separated_by_zeros;
  6797. while (*p)
  6798. {
  6799. if (idx == items_count)
  6800. break;
  6801. p += strlen(p) + 1;
  6802. items_count++;
  6803. }
  6804. if (!*p)
  6805. return false;
  6806. if (out_text)
  6807. *out_text = p;
  6808. return true;
  6809. }
  6810. // Combo box helper allowing to pass an array of strings.
  6811. bool ImGui::Combo(const char* label, int* current_item, const char** items, int items_count, int height_in_items)
  6812. {
  6813. const bool value_changed = Combo(label, current_item, Items_ArrayGetter, (void*)items, items_count, height_in_items);
  6814. return value_changed;
  6815. }
  6816. // Combo box helper allowing to pass all items in a single string.
  6817. bool ImGui::Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int height_in_items)
  6818. {
  6819. int items_count = 0;
  6820. const char* p = items_separated_by_zeros; // FIXME-OPT: Avoid computing this, or at least only when combo is open
  6821. while (*p)
  6822. {
  6823. p += strlen(p) + 1;
  6824. items_count++;
  6825. }
  6826. bool value_changed = Combo(label, current_item, Items_SingleStringGetter, (void*)items_separated_by_zeros, items_count, height_in_items);
  6827. return value_changed;
  6828. }
  6829. // Combo box function.
  6830. bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int height_in_items)
  6831. {
  6832. ImGuiState& g = *GImGui;
  6833. ImGuiWindow* window = GetCurrentWindow();
  6834. if (window->SkipItems)
  6835. return false;
  6836. const ImGuiStyle& style = g.Style;
  6837. const ImGuiID id = window->GetID(label);
  6838. const float w = ImGui::CalcItemWidth();
  6839. const ImVec2 label_size = CalcTextSize(label, NULL, true);
  6840. const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y) + style.FramePadding*2.0f);
  6841. const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));
  6842. ItemSize(total_bb, style.FramePadding.y);
  6843. if (!ItemAdd(total_bb, &id))
  6844. return false;
  6845. const float arrow_size = (g.FontSize + style.FramePadding.x * 2.0f);
  6846. const bool hovered = IsHovered(frame_bb, id);
  6847. const ImRect value_bb(frame_bb.Min, frame_bb.Max - ImVec2(arrow_size, 0.0f));
  6848. RenderFrame(frame_bb.Min, frame_bb.Max, window->Color(ImGuiCol_FrameBg), true, style.FrameRounding);
  6849. RenderFrame(ImVec2(frame_bb.Max.x-arrow_size, frame_bb.Min.y), frame_bb.Max, window->Color(hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button), true, style.FrameRounding); // FIXME-ROUNDING
  6850. RenderCollapseTriangle(ImVec2(frame_bb.Max.x-arrow_size, frame_bb.Min.y) + style.FramePadding, true);
  6851. if (*current_item >= 0 && *current_item < items_count)
  6852. {
  6853. const char* item_text;
  6854. if (items_getter(data, *current_item, &item_text))
  6855. RenderTextClipped(frame_bb.Min + style.FramePadding, value_bb.Max, item_text, NULL, NULL);
  6856. }
  6857. if (label_size.x > 0)
  6858. RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);
  6859. bool menu_toggled = false;
  6860. if (hovered)
  6861. {
  6862. g.HoveredId = id;
  6863. if (g.IO.MouseClicked[0])
  6864. {
  6865. SetActiveId(0);
  6866. if (IsPopupOpen(id))
  6867. {
  6868. ClosePopup(id);
  6869. }
  6870. else
  6871. {
  6872. FocusWindow(window);
  6873. ImGui::OpenPopup(label);
  6874. menu_toggled = true;
  6875. }
  6876. }
  6877. }
  6878. bool value_changed = false;
  6879. if (IsPopupOpen(id))
  6880. {
  6881. // Size default to hold ~7 items
  6882. if (height_in_items < 0)
  6883. height_in_items = 7;
  6884. float popup_height = (label_size.y + style.ItemSpacing.y) * ImMin(items_count, height_in_items) + (style.FramePadding.y * 3);
  6885. ImRect popup_rect(ImVec2(frame_bb.Min.x, frame_bb.Max.y), ImVec2(frame_bb.Max.x, frame_bb.Max.y + popup_height));
  6886. popup_rect.Max.y = ImMin(popup_rect.Max.y, g.IO.DisplaySize.y - style.DisplaySafeAreaPadding.y); // Adhoc height limit for Combo. Ideally should be handled in Begin() along with other popups size, we want to have the possibility of moving the popup above as well.
  6887. ImGui::SetNextWindowPos(popup_rect.Min);
  6888. ImGui::SetNextWindowSize(popup_rect.GetSize());
  6889. ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, style.FramePadding);
  6890. const ImGuiWindowFlags flags = ImGuiWindowFlags_ComboBox | ((window->Flags & ImGuiWindowFlags_ShowBorders) ? ImGuiWindowFlags_ShowBorders : 0);
  6891. if (BeginPopupEx(label, flags))
  6892. {
  6893. // Display items
  6894. ImGui::Spacing();
  6895. for (int i = 0; i < items_count; i++)
  6896. {
  6897. ImGui::PushID((void*)(intptr_t)i);
  6898. const bool item_selected = (i == *current_item);
  6899. const char* item_text;
  6900. if (!items_getter(data, i, &item_text))
  6901. item_text = "*Unknown item*";
  6902. if (ImGui::Selectable(item_text, item_selected))
  6903. {
  6904. SetActiveId(0);
  6905. value_changed = true;
  6906. *current_item = i;
  6907. }
  6908. if (item_selected && menu_toggled)
  6909. ImGui::SetScrollHere();
  6910. ImGui::PopID();
  6911. }
  6912. ImGui::EndPopup();
  6913. }
  6914. ImGui::PopStyleVar();
  6915. }
  6916. return value_changed;
  6917. }
  6918. // Tip: pass an empty label (e.g. "##dummy") then you can use the space to draw other text or image.
  6919. // But you need to make sure the ID is unique, e.g. enclose calls in PushID/PopID.
  6920. bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags flags, const ImVec2& size_arg)
  6921. {
  6922. ImGuiState& g = *GImGui;
  6923. ImGuiWindow* window = GetCurrentWindow();
  6924. if (window->SkipItems)
  6925. return false;
  6926. if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.ColumnsCount > 1)
  6927. PopClipRect();
  6928. const ImGuiStyle& style = g.Style;
  6929. ImGuiID id = window->GetID(label);
  6930. ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true);
  6931. ImVec2 size(size_arg.x != 0.0f ? size_arg.x : label_size.x, size_arg.y != 0.0f ? size_arg.y : label_size.y);
  6932. ImVec2 pos = window->DC.CursorPos;
  6933. pos.y += window->DC.CurrentLineTextBaseOffset;
  6934. ImRect bb(pos, pos + size);
  6935. ItemSize(bb);
  6936. // Fill horizontal space.
  6937. ImVec2 window_padding = window->WindowPadding;
  6938. float max_x = (flags & ImGuiSelectableFlags_SpanAllColumns) ? ImGui::GetWindowContentRegionMax().x : ImGui::GetContentRegionMax().x;
  6939. float w_draw = ImMax(label_size.x, window->Pos.x + max_x - window_padding.x - window->DC.CursorPos.x);
  6940. ImVec2 size_draw((size_arg.x != 0 && !(flags & ImGuiSelectableFlags_DrawFillAvailWidth)) ? size_arg.x : w_draw, size_arg.y != 0.0f ? size_arg.y : size.y);
  6941. ImRect bb_with_spacing(pos, pos + size_draw);
  6942. if (size_arg.x == 0.0f || (flags & ImGuiSelectableFlags_DrawFillAvailWidth))
  6943. bb_with_spacing.Max.x += window_padding.x;
  6944. // Selectables are tightly packed together, we extend the box to cover spacing between selectable.
  6945. float spacing_L = (float)(int)(style.ItemSpacing.x * 0.5f);
  6946. float spacing_U = (float)(int)(style.ItemSpacing.y * 0.5f);
  6947. float spacing_R = style.ItemSpacing.x - spacing_L;
  6948. float spacing_D = style.ItemSpacing.y - spacing_U;
  6949. bb_with_spacing.Min.x -= spacing_L;
  6950. bb_with_spacing.Min.y -= spacing_U;
  6951. bb_with_spacing.Max.x += spacing_R;
  6952. bb_with_spacing.Max.y += spacing_D;
  6953. if (!ItemAdd(bb_with_spacing, &id))
  6954. {
  6955. if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.ColumnsCount > 1)
  6956. PushColumnClipRect();
  6957. return false;
  6958. }
  6959. ImGuiButtonFlags button_flags = 0;
  6960. if (flags & ImGuiSelectableFlags_Menu) button_flags |= ImGuiButtonFlags_PressedOnClick;
  6961. if (flags & ImGuiSelectableFlags_MenuItem) button_flags |= ImGuiButtonFlags_PressedOnClick|ImGuiButtonFlags_PressedOnRelease;
  6962. if (flags & ImGuiSelectableFlags_Disabled) button_flags |= ImGuiButtonFlags_Disabled;
  6963. bool hovered, held;
  6964. bool pressed = ButtonBehavior(bb_with_spacing, id, &hovered, &held, true, button_flags);
  6965. if (flags & ImGuiSelectableFlags_Disabled)
  6966. selected = false;
  6967. // Render
  6968. if (hovered || selected)
  6969. {
  6970. const ImU32 col = window->Color((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header);
  6971. RenderFrame(bb_with_spacing.Min, bb_with_spacing.Max, col, false, 0.0f);
  6972. }
  6973. if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.ColumnsCount > 1)
  6974. {
  6975. PushColumnClipRect();
  6976. bb_with_spacing.Max.x -= (ImGui::GetContentRegionMax().x - max_x);
  6977. }
  6978. if (flags & ImGuiSelectableFlags_Disabled) ImGui::PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]);
  6979. RenderTextClipped(bb.Min, bb_with_spacing.Max, label, NULL, &label_size);
  6980. if (flags & ImGuiSelectableFlags_Disabled) ImGui::PopStyleColor();
  6981. // Automatically close popups
  6982. if (pressed && !(flags & ImGuiSelectableFlags_DontClosePopups) && (window->Flags & ImGuiWindowFlags_Popup))
  6983. ImGui::CloseCurrentPopup();
  6984. return pressed;
  6985. }
  6986. bool ImGui::Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags, const ImVec2& size_arg)
  6987. {
  6988. if (ImGui::Selectable(label, *p_selected, flags, size_arg))
  6989. {
  6990. *p_selected = !*p_selected;
  6991. return true;
  6992. }
  6993. return false;
  6994. }
  6995. // Helper to calculate the size of a listbox and display a label on the right.
  6996. // Tip: To have a list filling the entire window width, PushItemWidth(-1) and pass an empty label "##empty"
  6997. bool ImGui::ListBoxHeader(const char* label, const ImVec2& size_arg)
  6998. {
  6999. ImGuiWindow* window = GetCurrentWindow();
  7000. if (window->SkipItems)
  7001. return false;
  7002. const ImGuiStyle& style = ImGui::GetStyle();
  7003. const ImGuiID id = ImGui::GetID(label);
  7004. const ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true);
  7005. // Size default to hold ~7 items. Fractional number of items helps seeing that we can scroll down/up without looking at scrollbar.
  7006. ImVec2 size = CalcItemSize(size_arg, ImGui::CalcItemWidth() + style.FramePadding.x * 2.0f, ImGui::GetTextLineHeightWithSpacing() * 7.4f + style.ItemSpacing.y);
  7007. ImVec2 frame_size = ImVec2(size.x, ImMax(size.y, label_size.y));
  7008. ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size);
  7009. ImRect bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));
  7010. window->DC.LastItemRect = bb;
  7011. ImGui::BeginGroup();
  7012. if (label_size.x > 0)
  7013. RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);
  7014. ImGui::BeginChildFrame(id, frame_bb.GetSize());
  7015. return true;
  7016. }
  7017. bool ImGui::ListBoxHeader(const char* label, int items_count, int height_in_items)
  7018. {
  7019. // Size default to hold ~7 items. Fractional number of items helps seeing that we can scroll down/up without looking at scrollbar.
  7020. // However we don't add +0.40f if items_count <= height_in_items. It is slightly dodgy, because it means a dynamic list of items will make the widget resize occasionally when it crosses that size.
  7021. // I am expecting that someone will come and complain about this behavior in a remote future, then we can advise on a better solution.
  7022. if (height_in_items < 0)
  7023. height_in_items = ImMin(items_count, 7);
  7024. float height_in_items_f = height_in_items < items_count ? (height_in_items + 0.40f) : (height_in_items + 0.00f);
  7025. // We include ItemSpacing.y so that a list sized for the exact number of items doesn't make a scrollbar appears. We could also enforce that by passing a flag to BeginChild().
  7026. ImVec2 size;
  7027. size.x = 0.0f;
  7028. size.y = ImGui::GetTextLineHeightWithSpacing() * height_in_items_f + ImGui::GetStyle().ItemSpacing.y;
  7029. return ImGui::ListBoxHeader(label, size);
  7030. }
  7031. void ImGui::ListBoxFooter()
  7032. {
  7033. ImGuiWindow* parent_window = GetParentWindow();
  7034. const ImRect bb = parent_window->DC.LastItemRect;
  7035. const ImGuiStyle& style = ImGui::GetStyle();
  7036. ImGui::EndChildFrame();
  7037. // Redeclare item size so that it includes the label (we have stored the full size in LastItemRect)
  7038. // We call SameLine() to restore DC.CurrentLine* data
  7039. ImGui::SameLine();
  7040. parent_window->DC.CursorPos = bb.Min;
  7041. ItemSize(bb, style.FramePadding.y);
  7042. ImGui::EndGroup();
  7043. }
  7044. bool ImGui::ListBox(const char* label, int* current_item, const char** items, int items_count, int height_items)
  7045. {
  7046. const bool value_changed = ListBox(label, current_item, Items_ArrayGetter, (void*)items, items_count, height_items);
  7047. return value_changed;
  7048. }
  7049. bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int height_in_items)
  7050. {
  7051. if (!ImGui::ListBoxHeader(label, items_count, height_in_items))
  7052. return false;
  7053. // Assume all items have even height (= 1 line of text). If you need items of different or variable sizes you can create a custom version of ListBox() in your code without using the clipper.
  7054. bool value_changed = false;
  7055. ImGuiListClipper clipper(items_count, ImGui::GetTextLineHeightWithSpacing());
  7056. for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)
  7057. {
  7058. const bool item_selected = (i == *current_item);
  7059. const char* item_text;
  7060. if (!items_getter(data, i, &item_text))
  7061. item_text = "*Unknown item*";
  7062. ImGui::PushID(i);
  7063. if (ImGui::Selectable(item_text, item_selected))
  7064. {
  7065. *current_item = i;
  7066. value_changed = true;
  7067. }
  7068. ImGui::PopID();
  7069. }
  7070. clipper.End();
  7071. ImGui::ListBoxFooter();
  7072. return value_changed;
  7073. }
  7074. bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected, bool enabled)
  7075. {
  7076. ImGuiState& g = *GImGui;
  7077. ImGuiWindow* window = GetCurrentWindow();
  7078. if (window->SkipItems)
  7079. return false;
  7080. ImVec2 pos = ImGui::GetCursorScreenPos();
  7081. ImVec2 label_size = CalcTextSize(label, NULL, true);
  7082. ImVec2 shortcut_size = shortcut ? CalcTextSize(shortcut, NULL) : ImVec2(0.0f, 0.0f);
  7083. float w = window->MenuColumns.DeclColumns(label_size.x, shortcut_size.x, (float)(int)(g.FontSize * 1.20f)); // Feedback for next frame
  7084. float extra_w = ImMax(0.0f, window->Pos.x + ImGui::GetContentRegionMax().x - pos.x - w);
  7085. bool pressed = ImGui::Selectable(label, false, ImGuiSelectableFlags_MenuItem | ImGuiSelectableFlags_DrawFillAvailWidth | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(w, 0.0f));
  7086. if (shortcut_size.x > 0.0f)
  7087. {
  7088. ImGui::PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]);
  7089. RenderText(pos + ImVec2(window->MenuColumns.Pos[1] + extra_w, 0.0f), shortcut, NULL, false);
  7090. ImGui::PopStyleColor();
  7091. }
  7092. if (selected)
  7093. RenderCheckMark(pos + ImVec2(window->MenuColumns.Pos[2] + extra_w + g.FontSize * 0.20f, 0.0f), window->Color(ImGuiCol_Text));
  7094. return pressed;
  7095. }
  7096. bool ImGui::MenuItem(const char* label, const char* shortcut, bool* p_selected, bool enabled)
  7097. {
  7098. if (ImGui::MenuItem(label, shortcut, p_selected ? *p_selected : false, enabled))
  7099. {
  7100. if (p_selected)
  7101. *p_selected = !*p_selected;
  7102. return true;
  7103. }
  7104. return false;
  7105. }
  7106. bool ImGui::BeginMainMenuBar()
  7107. {
  7108. ImGuiState& g = *GImGui;
  7109. ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
  7110. ImGui::SetNextWindowSize(ImVec2(g.IO.DisplaySize.x, g.FontBaseSize + g.Style.FramePadding.y * 2.0f));
  7111. ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
  7112. ImGui::PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(0,0));
  7113. if (!ImGui::Begin("##MainMenuBar", NULL, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoScrollbar|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_MenuBar)
  7114. || !ImGui::BeginMenuBar())
  7115. {
  7116. ImGui::End();
  7117. ImGui::PopStyleVar(2);
  7118. return false;
  7119. }
  7120. g.CurrentWindow->DC.MenuBarOffsetX += g.Style.DisplaySafeAreaPadding.x;
  7121. return true;
  7122. }
  7123. void ImGui::EndMainMenuBar()
  7124. {
  7125. ImGui::EndMenuBar();
  7126. ImGui::End();
  7127. ImGui::PopStyleVar(2);
  7128. }
  7129. bool ImGui::BeginMenuBar()
  7130. {
  7131. ImGuiWindow* window = GetCurrentWindow();
  7132. if (window->SkipItems)
  7133. return false;
  7134. if (!(window->Flags & ImGuiWindowFlags_MenuBar))
  7135. return false;
  7136. IM_ASSERT(!window->DC.MenuBarAppending);
  7137. ImGui::BeginGroup(); // Save position
  7138. ImGui::PushID("##menubar");
  7139. ImRect rect = window->MenuBarRect();
  7140. PushClipRect(ImVec4(rect.Min.x+0.5f, rect.Min.y-0.5f, rect.Max.x+0.5f, rect.Max.y-1.5f), false);
  7141. window->DC.CursorPos = ImVec2(rect.Min.x + window->DC.MenuBarOffsetX, rect.Min.y);// + g.Style.FramePadding.y);
  7142. window->DC.LayoutType = ImGuiLayoutType_Horizontal;
  7143. window->DC.MenuBarAppending = true;
  7144. ImGui::AlignFirstTextHeightToWidgets();
  7145. return true;
  7146. }
  7147. void ImGui::EndMenuBar()
  7148. {
  7149. ImGuiWindow* window = GetCurrentWindow();
  7150. if (window->SkipItems)
  7151. return;
  7152. IM_ASSERT(window->Flags & ImGuiWindowFlags_MenuBar);
  7153. IM_ASSERT(window->DC.MenuBarAppending);
  7154. PopClipRect();
  7155. ImGui::PopID();
  7156. window->DC.MenuBarOffsetX = window->DC.CursorPos.x - window->MenuBarRect().Min.x;
  7157. window->DC.GroupStack.back().AdvanceCursor = false;
  7158. ImGui::EndGroup();
  7159. window->DC.LayoutType = ImGuiLayoutType_Vertical;
  7160. window->DC.MenuBarAppending = false;
  7161. }
  7162. bool ImGui::BeginMenu(const char* label, bool enabled)
  7163. {
  7164. ImGuiWindow* window = GetCurrentWindow();
  7165. if (window->SkipItems)
  7166. return false;
  7167. ImGuiState& g = *GImGui;
  7168. const ImGuiStyle& style = g.Style;
  7169. const ImGuiID id = window->GetID(label);
  7170. ImVec2 label_size = CalcTextSize(label, NULL, true);
  7171. ImGuiWindow* backed_focused_window = g.FocusedWindow;
  7172. bool pressed;
  7173. bool opened = IsPopupOpen(id);
  7174. bool menuset_opened = !(window->Flags & ImGuiWindowFlags_Popup) && (g.OpenedPopupStack.Size > g.CurrentPopupStack.Size && g.OpenedPopupStack[g.CurrentPopupStack.Size].ParentMenuSet == window->GetID("##menus"));
  7175. if (menuset_opened)
  7176. g.FocusedWindow = window;
  7177. ImVec2 popup_pos, pos = window->DC.CursorPos;
  7178. if (window->DC.LayoutType == ImGuiLayoutType_Horizontal)
  7179. {
  7180. popup_pos = ImVec2(pos.x - window->WindowPadding.x, pos.y - style.FramePadding.y + window->MenuBarHeight());
  7181. window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f);
  7182. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, style.ItemSpacing * 2.0f);
  7183. float w = label_size.x;
  7184. pressed = ImGui::Selectable(label, opened, ImGuiSelectableFlags_Menu | ImGuiSelectableFlags_DontClosePopups | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(w, 0.0f));
  7185. ImGui::PopStyleVar();
  7186. ImGui::SameLine();
  7187. window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f);
  7188. }
  7189. else
  7190. {
  7191. popup_pos = ImVec2(pos.x, pos.y - style.WindowPadding.y);
  7192. float w = window->MenuColumns.DeclColumns(label_size.x, 0.0f, (float)(int)(g.FontSize * 1.20f)); // Feedback to next frame
  7193. float extra_w = ImMax(0.0f, window->Pos.x + ImGui::GetContentRegionMax().x - pos.x - w);
  7194. pressed = ImGui::Selectable(label, opened, ImGuiSelectableFlags_Menu | ImGuiSelectableFlags_DontClosePopups | ImGuiSelectableFlags_DrawFillAvailWidth | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(w, 0.0f));
  7195. if (!enabled) ImGui::PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]);
  7196. RenderCollapseTriangle(pos + ImVec2(window->MenuColumns.Pos[2] + extra_w + g.FontSize * 0.20f, 0.0f), false);
  7197. if (!enabled) ImGui::PopStyleColor();
  7198. }
  7199. bool hovered = enabled && IsHovered(window->DC.LastItemRect, id);
  7200. if (menuset_opened)
  7201. g.FocusedWindow = backed_focused_window;
  7202. bool want_open = false, want_close = false;
  7203. if (window->Flags & (ImGuiWindowFlags_Popup|ImGuiWindowFlags_ChildMenu))
  7204. {
  7205. // Implement http://bjk5.com/post/44698559168/breaking-down-amazons-mega-dropdown to avoid using timers so menus feel more reactive.
  7206. bool moving_within_opened_triangle = false;
  7207. if (g.HoveredWindow == window && g.OpenedPopupStack.Size > g.CurrentPopupStack.Size && g.OpenedPopupStack[g.CurrentPopupStack.Size].ParentWindow == window)
  7208. {
  7209. if (ImGuiWindow* next_window = g.OpenedPopupStack[g.CurrentPopupStack.Size].Window)
  7210. {
  7211. ImRect next_window_rect = next_window->Rect();
  7212. ImVec2 ta = g.IO.MousePos - g.IO.MouseDelta;
  7213. ImVec2 tb = (window->Pos.x < next_window->Pos.x) ? next_window_rect.GetTL() : next_window_rect.GetTR();
  7214. ImVec2 tc = (window->Pos.x < next_window->Pos.x) ? next_window_rect.GetBL() : next_window_rect.GetBR();
  7215. float extra = ImClamp(fabsf(ta.x - tb.x) * 0.30f, 5.0f, 30.0f); // add a bit of extra slack.
  7216. ta.x += (window->Pos.x < next_window->Pos.x) ? -0.5f : +0.5f; // to avoid numerical issues
  7217. tb.y = ta.y + ImMax((tb.y - extra) - ta.y, -100.0f); // triangle is maximum 200 high to limit the slope and the bias toward large sub-menus
  7218. tc.y = ta.y + ImMin((tc.y + extra) - ta.y, +100.0f);
  7219. moving_within_opened_triangle = ImIsPointInTriangle(g.IO.MousePos, ta, tb, tc);
  7220. //window->DrawList->PushClipRectFullScreen(); window->DrawList->AddTriangleFilled(ta, tb, tc, moving_within_opened_triangle ? 0x80008000 : 0x80000080); window->DrawList->PopClipRect(); // Debug
  7221. }
  7222. }
  7223. want_close = (opened && !hovered && g.HoveredWindow == window && g.HoveredIdPreviousFrame != 0 && g.HoveredIdPreviousFrame != id && !moving_within_opened_triangle);
  7224. want_open = (!opened && hovered && !moving_within_opened_triangle) || (!opened && hovered && pressed);
  7225. }
  7226. else if (opened && pressed && menuset_opened) // menu-bar: click open menu to close
  7227. {
  7228. want_close = true;
  7229. want_open = opened = false;
  7230. }
  7231. else if (pressed || (hovered && menuset_opened && !opened)) // menu-bar: first click to open, then hover to open others
  7232. want_open = true;
  7233. if (want_close && IsPopupOpen(id))
  7234. ClosePopupToLevel(GImGui->CurrentPopupStack.Size);
  7235. if (!opened && want_open && g.OpenedPopupStack.Size > g.CurrentPopupStack.Size)
  7236. {
  7237. // Don't recycle same menu level in the same frame, first close the other menu and yield for a frame.
  7238. ImGui::OpenPopup(label);
  7239. return false;
  7240. }
  7241. opened |= want_open;
  7242. if (want_open)
  7243. ImGui::OpenPopup(label);
  7244. if (opened)
  7245. {
  7246. ImGui::SetNextWindowPos(popup_pos, ImGuiSetCond_Always);
  7247. ImGuiWindowFlags flags = ImGuiWindowFlags_ShowBorders | ((window->Flags & (ImGuiWindowFlags_Popup|ImGuiWindowFlags_ChildMenu)) ? ImGuiWindowFlags_ChildMenu|ImGuiWindowFlags_ChildWindow : ImGuiWindowFlags_ChildMenu);
  7248. opened = BeginPopupEx(label, flags); // opened can be 'false' when the popup is completely clipped (e.g. zero size display)
  7249. }
  7250. return opened;
  7251. }
  7252. void ImGui::EndMenu()
  7253. {
  7254. ImGui::EndPopup();
  7255. }
  7256. // A little colored square. Return true when clicked.
  7257. bool ImGui::ColorButton(const ImVec4& col, bool small_height, bool outline_border)
  7258. {
  7259. ImGuiState& g = *GImGui;
  7260. ImGuiWindow* window = GetCurrentWindow();
  7261. if (window->SkipItems)
  7262. return false;
  7263. const ImGuiStyle& style = g.Style;
  7264. const ImGuiID id = window->GetID("#colorbutton");
  7265. const float square_size = g.FontSize;
  7266. const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(square_size + style.FramePadding.y*2, square_size + (small_height ? 0 : style.FramePadding.y*2)));
  7267. ItemSize(bb, small_height ? 0.0f : style.FramePadding.y);
  7268. if (!ItemAdd(bb, &id))
  7269. return false;
  7270. bool hovered, held;
  7271. bool pressed = ButtonBehavior(bb, id, &hovered, &held, true);
  7272. RenderFrame(bb.Min, bb.Max, window->Color(col), outline_border, style.FrameRounding);
  7273. if (hovered)
  7274. {
  7275. int ix = (int)(col.x * 255.0f + 0.5f);
  7276. int iy = (int)(col.y * 255.0f + 0.5f);
  7277. int iz = (int)(col.z * 255.0f + 0.5f);
  7278. int iw = (int)(col.w * 255.0f + 0.5f);
  7279. ImGui::SetTooltip("Color:\n(%.2f,%.2f,%.2f,%.2f)\n#%02X%02X%02X%02X", col.x, col.y, col.z, col.w, ix, iy, iz, iw);
  7280. }
  7281. return pressed;
  7282. }
  7283. bool ImGui::ColorEdit3(const char* label, float col[3])
  7284. {
  7285. float col4[4];
  7286. col4[0] = col[0];
  7287. col4[1] = col[1];
  7288. col4[2] = col[2];
  7289. col4[3] = 1.0f;
  7290. const bool value_changed = ImGui::ColorEdit4(label, col4, false);
  7291. col[0] = col4[0];
  7292. col[1] = col4[1];
  7293. col[2] = col4[2];
  7294. return value_changed;
  7295. }
  7296. // Edit colors components (each component in 0.0f..1.0f range
  7297. // Use CTRL-Click to input value and TAB to go to next item.
  7298. bool ImGui::ColorEdit4(const char* label, float col[4], bool alpha)
  7299. {
  7300. ImGuiState& g = *GImGui;
  7301. ImGuiWindow* window = GetCurrentWindow();
  7302. if (window->SkipItems)
  7303. return false;
  7304. const ImGuiStyle& style = g.Style;
  7305. const ImGuiID id = window->GetID(label);
  7306. const float w_full = ImGui::CalcItemWidth();
  7307. const float square_sz = (g.FontSize + style.FramePadding.y * 2.0f);
  7308. ImGuiColorEditMode edit_mode = window->DC.ColorEditMode;
  7309. if (edit_mode == ImGuiColorEditMode_UserSelect || edit_mode == ImGuiColorEditMode_UserSelectShowButton)
  7310. edit_mode = g.ColorEditModeStorage.GetInt(id, 0) % 3;
  7311. float f[4] = { col[0], col[1], col[2], col[3] };
  7312. if (edit_mode == ImGuiColorEditMode_HSV)
  7313. ImGui::ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]);
  7314. int i[4] = { (int)(f[0] * 255.0f + 0.5f), (int)(f[1] * 255.0f + 0.5f), (int)(f[2] * 255.0f + 0.5f), (int)(f[3] * 255.0f + 0.5f) };
  7315. int components = alpha ? 4 : 3;
  7316. bool value_changed = false;
  7317. ImGui::BeginGroup();
  7318. ImGui::PushID(label);
  7319. const bool hsv = (edit_mode == 1);
  7320. switch (edit_mode)
  7321. {
  7322. case ImGuiColorEditMode_RGB:
  7323. case ImGuiColorEditMode_HSV:
  7324. {
  7325. // RGB/HSV 0..255 Sliders
  7326. const float w_items_all = w_full - (square_sz + style.ItemInnerSpacing.x);
  7327. const float w_item_one = ImMax(1.0f, (float)(int)((w_items_all - (style.FramePadding.x*2.0f + style.ItemInnerSpacing.x) * (components-1)) / (float)components));
  7328. const float w_item_last = ImMax(1.0f, (float)(int)(w_items_all - (w_item_one + style.FramePadding.x*2.0f + style.ItemInnerSpacing.x) * (components-1)));
  7329. const bool hide_prefix = (w_item_one <= CalcTextSize("M:999").x);
  7330. const char* ids[4] = { "##X", "##Y", "##Z", "##W" };
  7331. const char* fmt_table[3][4] =
  7332. {
  7333. { "%3.0f", "%3.0f", "%3.0f", "%3.0f" },
  7334. { "R:%3.0f", "G:%3.0f", "B:%3.0f", "A:%3.0f" },
  7335. { "H:%3.0f", "S:%3.0f", "V:%3.0f", "A:%3.0f" }
  7336. };
  7337. const char** fmt = hide_prefix ? fmt_table[0] : hsv ? fmt_table[2] : fmt_table[1];
  7338. ImGui::PushItemWidth(w_item_one);
  7339. for (int n = 0; n < components; n++)
  7340. {
  7341. if (n > 0)
  7342. ImGui::SameLine(0, style.ItemInnerSpacing.x);
  7343. if (n + 1 == components)
  7344. ImGui::PushItemWidth(w_item_last);
  7345. value_changed |= ImGui::DragInt(ids[n], &i[n], 1.0f, 0, 255, fmt[n]);
  7346. }
  7347. ImGui::PopItemWidth();
  7348. ImGui::PopItemWidth();
  7349. }
  7350. break;
  7351. case ImGuiColorEditMode_HEX:
  7352. {
  7353. // RGB Hexadecimal Input
  7354. const float w_slider_all = w_full - square_sz;
  7355. char buf[64];
  7356. if (alpha)
  7357. ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X%02X", i[0], i[1], i[2], i[3]);
  7358. else
  7359. ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X", i[0], i[1], i[2]);
  7360. ImGui::PushItemWidth(w_slider_all - style.ItemInnerSpacing.x);
  7361. value_changed |= ImGui::InputText("##Text", buf, IM_ARRAYSIZE(buf), ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase);
  7362. ImGui::PopItemWidth();
  7363. char* p = buf;
  7364. while (*p == '#' || ImCharIsSpace(*p))
  7365. p++;
  7366. // Treat at unsigned (%X is unsigned)
  7367. i[0] = i[1] = i[2] = i[3] = 0;
  7368. if (alpha)
  7369. sscanf(p, "%02X%02X%02X%02X", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2], (unsigned int*)&i[3]);
  7370. else
  7371. sscanf(p, "%02X%02X%02X", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2]);
  7372. }
  7373. break;
  7374. }
  7375. ImGui::SameLine(0, style.ItemInnerSpacing.x);
  7376. const ImVec4 col_display(col[0], col[1], col[2], 1.0f);
  7377. if (ImGui::ColorButton(col_display))
  7378. g.ColorEditModeStorage.SetInt(id, (edit_mode + 1) % 3); // Don't set local copy of 'edit_mode' right away!
  7379. if (window->DC.ColorEditMode == ImGuiColorEditMode_UserSelectShowButton)
  7380. {
  7381. ImGui::SameLine(0, style.ItemInnerSpacing.x);
  7382. const char* button_titles[3] = { "RGB", "HSV", "HEX" };
  7383. if (ButtonEx(button_titles[edit_mode], ImVec2(0,0), ImGuiButtonFlags_DontClosePopups))
  7384. g.ColorEditModeStorage.SetInt(id, (edit_mode + 1) % 3); // Don't set local copy of 'edit_mode' right away!
  7385. ImGui::SameLine();
  7386. }
  7387. else
  7388. {
  7389. ImGui::SameLine(0, style.ItemInnerSpacing.x);
  7390. }
  7391. ImGui::TextUnformatted(label, FindTextDisplayEnd(label));
  7392. // Convert back
  7393. for (int n = 0; n < 4; n++)
  7394. f[n] = i[n] / 255.0f;
  7395. if (edit_mode == 1)
  7396. ImGui::ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]);
  7397. if (value_changed)
  7398. {
  7399. col[0] = f[0];
  7400. col[1] = f[1];
  7401. col[2] = f[2];
  7402. if (alpha)
  7403. col[3] = f[3];
  7404. }
  7405. ImGui::PopID();
  7406. ImGui::EndGroup();
  7407. return value_changed;
  7408. }
  7409. void ImGui::ColorEditMode(ImGuiColorEditMode mode)
  7410. {
  7411. ImGuiWindow* window = GetCurrentWindow();
  7412. window->DC.ColorEditMode = mode;
  7413. }
  7414. // Horizontal separating line.
  7415. void ImGui::Separator()
  7416. {
  7417. ImGuiWindow* window = GetCurrentWindow();
  7418. if (window->SkipItems)
  7419. return;
  7420. if (window->DC.ColumnsCount > 1)
  7421. PopClipRect();
  7422. float x1 = window->Pos.x;
  7423. float x2 = window->Pos.x + window->Size.x;
  7424. if (!window->DC.GroupStack.empty())
  7425. x1 += window->DC.ColumnsStartX;
  7426. const ImRect bb(ImVec2(x1, window->DC.CursorPos.y), ImVec2(x2, window->DC.CursorPos.y));
  7427. ItemSize(ImVec2(0.0f, bb.GetSize().y)); // NB: we don't provide our width so that it doesn't get feed back into AutoFit
  7428. if (!ItemAdd(bb, NULL))
  7429. {
  7430. if (window->DC.ColumnsCount > 1)
  7431. PushColumnClipRect();
  7432. return;
  7433. }
  7434. window->DrawList->AddLine(bb.Min, bb.Max, window->Color(ImGuiCol_Border));
  7435. ImGuiState& g = *GImGui;
  7436. if (g.LogEnabled)
  7437. ImGui::LogText(IM_NEWLINE "--------------------------------");
  7438. if (window->DC.ColumnsCount > 1)
  7439. {
  7440. PushColumnClipRect();
  7441. window->DC.ColumnsCellMinY = window->DC.CursorPos.y;
  7442. }
  7443. }
  7444. void ImGui::Spacing()
  7445. {
  7446. ImGuiWindow* window = GetCurrentWindow();
  7447. if (window->SkipItems)
  7448. return;
  7449. ItemSize(ImVec2(0,0));
  7450. }
  7451. void ImGui::Dummy(const ImVec2& size)
  7452. {
  7453. ImGuiWindow* window = GetCurrentWindow();
  7454. if (window->SkipItems)
  7455. return;
  7456. ItemSize(size);
  7457. }
  7458. // Advance cursor given item size for layout.
  7459. static void ItemSize(ImVec2 size, float text_offset_y)
  7460. {
  7461. ImGuiState& g = *GImGui;
  7462. ImGuiWindow* window = GetCurrentWindow();
  7463. if (window->SkipItems)
  7464. return;
  7465. // Always align ourselves on pixel boundaries
  7466. const float line_height = ImMax(window->DC.CurrentLineHeight, size.y);
  7467. const float text_base_offset = ImMax(window->DC.CurrentLineTextBaseOffset, text_offset_y);
  7468. window->DC.CursorPosPrevLine = ImVec2(window->DC.CursorPos.x + size.x, window->DC.CursorPos.y);
  7469. window->DC.CursorPos = ImVec2((float)(int)(window->Pos.x + window->DC.ColumnsStartX + window->DC.ColumnsOffsetX), (float)(int)(window->DC.CursorPos.y + line_height + g.Style.ItemSpacing.y));
  7470. window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPosPrevLine.x);
  7471. window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y);
  7472. //window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, 0xFF0000FF, 4); // Debug
  7473. window->DC.PrevLineHeight = line_height;
  7474. window->DC.PrevLineTextBaseOffset = text_base_offset;
  7475. window->DC.CurrentLineHeight = window->DC.CurrentLineTextBaseOffset = 0.0f;
  7476. }
  7477. static inline void ItemSize(const ImRect& bb, float text_offset_y)
  7478. {
  7479. ItemSize(bb.GetSize(), text_offset_y);
  7480. }
  7481. static bool IsClippedEx(const ImRect& bb, const ImGuiID* id, bool clip_even_when_logged)
  7482. {
  7483. ImGuiState& g = *GImGui;
  7484. ImGuiWindow* window = GetCurrentWindow();
  7485. if (!bb.Overlaps(ImRect(window->ClipRect)))
  7486. {
  7487. if (!id || *id != GImGui->ActiveId)
  7488. if (clip_even_when_logged || !g.LogEnabled)
  7489. return true;
  7490. }
  7491. return false;
  7492. }
  7493. bool ImGui::IsRectVisible(const ImVec2& size)
  7494. {
  7495. ImGuiWindow* window = GetCurrentWindow();
  7496. ImRect r(window->ClipRect);
  7497. return r.Overlaps(ImRect(window->DC.CursorPos, window->DC.CursorPos + size));
  7498. }
  7499. // Declare item bounding box for clipping and interaction.
  7500. // Note that the size can be different than the one provided to ItemSize(). Typically, widgets that spread over available surface
  7501. // declares their minimum size requirement to ItemSize() and then use a larger region for drawing/interaction, which is passed to ItemAdd().
  7502. static bool ItemAdd(const ImRect& bb, const ImGuiID* id)
  7503. {
  7504. ImGuiWindow* window = GetCurrentWindow();
  7505. window->DC.LastItemID = id ? *id : 0;
  7506. window->DC.LastItemRect = bb;
  7507. if (IsClippedEx(bb, id, false))
  7508. {
  7509. window->DC.LastItemHoveredAndUsable = window->DC.LastItemHoveredRect = false;
  7510. return false;
  7511. }
  7512. // This is a sensible default, but widgets are free to override it after calling ItemAdd()
  7513. ImGuiState& g = *GImGui;
  7514. if (IsMouseHoveringRect(bb))
  7515. {
  7516. // Matching the behavior of IsHovered() but ignore if ActiveId==window->MoveID (we clicked on the window background)
  7517. // So that clicking on items with no active id such as Text() still returns true with IsItemHovered()
  7518. window->DC.LastItemHoveredRect = true;
  7519. window->DC.LastItemHoveredAndUsable = false;
  7520. if (g.HoveredRootWindow == window->RootWindow)
  7521. if (g.ActiveId == 0 || (id && g.ActiveId == *id) || g.ActiveIdIsFocusedOnly || (g.ActiveId == window->MoveID))
  7522. if (IsWindowContentHoverable(window))
  7523. window->DC.LastItemHoveredAndUsable = true;
  7524. }
  7525. else
  7526. {
  7527. window->DC.LastItemHoveredAndUsable = window->DC.LastItemHoveredRect = false;
  7528. }
  7529. return true;
  7530. }
  7531. void ImGui::BeginGroup()
  7532. {
  7533. ImGuiWindow* window = GetCurrentWindow();
  7534. window->DC.GroupStack.resize(window->DC.GroupStack.Size + 1);
  7535. ImGuiGroupData& group_data = window->DC.GroupStack.back();
  7536. group_data.BackupCursorPos = window->DC.CursorPos;
  7537. group_data.BackupCursorMaxPos = window->DC.CursorMaxPos;
  7538. group_data.BackupColumnsStartX = window->DC.ColumnsStartX;
  7539. group_data.BackupCurrentLineHeight = window->DC.CurrentLineHeight;
  7540. group_data.BackupCurrentLineTextBaseOffset = window->DC.CurrentLineTextBaseOffset;
  7541. group_data.BackupLogLinePosY = window->DC.LogLinePosY;
  7542. group_data.AdvanceCursor = true;
  7543. window->DC.ColumnsStartX = window->DC.CursorPos.x - window->Pos.x;
  7544. window->DC.CursorMaxPos = window->DC.CursorPos;
  7545. window->DC.CurrentLineHeight = 0.0f;
  7546. window->DC.LogLinePosY = window->DC.CursorPos.y - 9999.0f;
  7547. }
  7548. void ImGui::EndGroup()
  7549. {
  7550. ImGuiWindow* window = GetCurrentWindow();
  7551. ImGuiStyle& style = ImGui::GetStyle();
  7552. IM_ASSERT(!window->DC.GroupStack.empty());
  7553. ImGuiGroupData& group_data = window->DC.GroupStack.back();
  7554. ImRect group_bb(group_data.BackupCursorPos, window->DC.CursorMaxPos);
  7555. group_bb.Max.y -= style.ItemSpacing.y; // Cancel out last vertical spacing because we are adding one ourselves.
  7556. group_bb.Max = ImMax(group_bb.Min, group_bb.Max);
  7557. window->DC.CursorPos = group_data.BackupCursorPos;
  7558. window->DC.CursorMaxPos = ImMax(group_data.BackupCursorMaxPos, window->DC.CursorMaxPos);
  7559. window->DC.CurrentLineHeight = group_data.BackupCurrentLineHeight;
  7560. window->DC.CurrentLineTextBaseOffset = group_data.BackupCurrentLineTextBaseOffset;
  7561. window->DC.ColumnsStartX = group_data.BackupColumnsStartX;
  7562. window->DC.LogLinePosY = window->DC.CursorPos.y - 9999.0f;
  7563. if (group_data.AdvanceCursor)
  7564. {
  7565. window->DC.CurrentLineTextBaseOffset = ImMax(window->DC.PrevLineTextBaseOffset, group_data.BackupCurrentLineTextBaseOffset); // FIXME: Incorrect, we should grab the base offset from the *first line* of the group but it is hard to obtain now.
  7566. ItemSize(group_bb.GetSize(), group_data.BackupCurrentLineTextBaseOffset);
  7567. ItemAdd(group_bb, NULL);
  7568. }
  7569. window->DC.GroupStack.pop_back();
  7570. //window->DrawList->AddRect(group_bb.Min, group_bb.Max, 0xFFFF00FF); // Debug
  7571. }
  7572. // Gets back to previous line and continue with horizontal layout
  7573. // pos_x == 0 : follow on previous item
  7574. // pos_x != 0 : align to specified column
  7575. // spacing_w < 0 : use default spacing if column_x==0, no spacing if column_x!=0
  7576. // spacing_w >= 0 : enforce spacing
  7577. void ImGui::SameLine(float pos_x, float spacing_w)
  7578. {
  7579. ImGuiState& g = *GImGui;
  7580. ImGuiWindow* window = GetCurrentWindow();
  7581. if (window->SkipItems)
  7582. return;
  7583. float x, y;
  7584. if (pos_x != 0.0f)
  7585. {
  7586. if (spacing_w < 0.0f) spacing_w = 0.0f;
  7587. x = window->Pos.x + pos_x + spacing_w;
  7588. y = window->DC.CursorPosPrevLine.y;
  7589. }
  7590. else
  7591. {
  7592. if (spacing_w < 0.0f) spacing_w = g.Style.ItemSpacing.x;
  7593. x = window->DC.CursorPosPrevLine.x + spacing_w;
  7594. y = window->DC.CursorPosPrevLine.y;
  7595. }
  7596. window->DC.CurrentLineHeight = window->DC.PrevLineHeight;
  7597. window->DC.CurrentLineTextBaseOffset = window->DC.PrevLineTextBaseOffset;
  7598. window->DC.CursorPos = ImVec2(x, y);
  7599. }
  7600. void ImGui::NextColumn()
  7601. {
  7602. ImGuiState& g = *GImGui;
  7603. ImGuiWindow* window = GetCurrentWindow();
  7604. if (window->SkipItems)
  7605. return;
  7606. if (window->DC.ColumnsCount > 1)
  7607. {
  7608. ImGui::PopItemWidth();
  7609. PopClipRect();
  7610. window->DC.ColumnsCellMaxY = ImMax(window->DC.ColumnsCellMaxY, window->DC.CursorPos.y);
  7611. if (++window->DC.ColumnsCurrent < window->DC.ColumnsCount)
  7612. {
  7613. window->DC.ColumnsOffsetX = ImGui::GetColumnOffset(window->DC.ColumnsCurrent) - window->DC.ColumnsStartX + g.Style.ItemSpacing.x;
  7614. window->DrawList->ChannelsSetCurrent(window->DC.ColumnsCurrent);
  7615. }
  7616. else
  7617. {
  7618. window->DC.ColumnsCurrent = 0;
  7619. window->DC.ColumnsOffsetX = 0.0f;
  7620. window->DC.ColumnsCellMinY = window->DC.ColumnsCellMaxY;
  7621. window->DrawList->ChannelsSetCurrent(0);
  7622. }
  7623. window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.ColumnsStartX + window->DC.ColumnsOffsetX);
  7624. window->DC.CursorPos.y = window->DC.ColumnsCellMinY;
  7625. window->DC.CurrentLineHeight = 0.0f;
  7626. window->DC.CurrentLineTextBaseOffset = 0.0f;
  7627. PushColumnClipRect();
  7628. ImGui::PushItemWidth(ImGui::GetColumnWidth() * 0.65f); // FIXME
  7629. }
  7630. }
  7631. int ImGui::GetColumnIndex()
  7632. {
  7633. ImGuiWindow* window = GetCurrentWindow();
  7634. return window->DC.ColumnsCurrent;
  7635. }
  7636. int ImGui::GetColumnsCount()
  7637. {
  7638. ImGuiWindow* window = GetCurrentWindow();
  7639. return window->DC.ColumnsCount;
  7640. }
  7641. static float GetDraggedColumnOffset(int column_index)
  7642. {
  7643. // Active (dragged) column always follow mouse. The reason we need this is that dragging a column to the right edge of an auto-resizing
  7644. // window creates a feedback loop because we store normalized positions/ So while dragging we enforce absolute positioning
  7645. ImGuiState& g = *GImGui;
  7646. ImGuiWindow* window = GetCurrentWindow();
  7647. IM_ASSERT(column_index > 0); // We cannot drag column 0. If you get this assert you may have a conflict between the ID of your columns and another widgets.
  7648. IM_ASSERT(g.ActiveId == window->DC.ColumnsSetID + ImGuiID(column_index));
  7649. float x = g.IO.MousePos.x + g.ActiveClickDeltaToCenter.x - window->Pos.x;
  7650. x = ImClamp(x, ImGui::GetColumnOffset(column_index-1)+g.Style.ColumnsMinSpacing, ImGui::GetColumnOffset(column_index+1)-g.Style.ColumnsMinSpacing);
  7651. return (float)(int)x;
  7652. }
  7653. float ImGui::GetColumnOffset(int column_index)
  7654. {
  7655. ImGuiState& g = *GImGui;
  7656. ImGuiWindow* window = GetCurrentWindow();
  7657. if (column_index < 0)
  7658. column_index = window->DC.ColumnsCurrent;
  7659. if (g.ActiveId)
  7660. {
  7661. const ImGuiID column_id = window->DC.ColumnsSetID + ImGuiID(column_index);
  7662. if (g.ActiveId == column_id)
  7663. return GetDraggedColumnOffset(column_index);
  7664. }
  7665. // Read from cache
  7666. IM_ASSERT(column_index < window->DC.ColumnsOffsetsT.Size);
  7667. const float t = window->DC.ColumnsOffsetsT[column_index];
  7668. const float min_x = window->DC.ColumnsStartX;
  7669. const float max_x = window->Size.x - ((window->Flags & ImGuiWindowFlags_NoScrollbar) ? 0 : g.Style.ScrollbarWidth);// - window->WindowPadding().x;
  7670. const float x = min_x + t * (max_x - min_x);
  7671. return (float)(int)x;
  7672. }
  7673. void ImGui::SetColumnOffset(int column_index, float offset)
  7674. {
  7675. ImGuiState& g = *GImGui;
  7676. ImGuiWindow* window = GetCurrentWindow();
  7677. if (column_index < 0)
  7678. column_index = window->DC.ColumnsCurrent;
  7679. IM_ASSERT(column_index < window->DC.ColumnsOffsetsT.Size);
  7680. const ImGuiID column_id = window->DC.ColumnsSetID + ImGuiID(column_index);
  7681. const float min_x = window->DC.ColumnsStartX;
  7682. const float max_x = window->Size.x - ((window->Flags & ImGuiWindowFlags_NoScrollbar) ? 0 : g.Style.ScrollbarWidth);// - window->WindowPadding().x;
  7683. const float t = (offset - min_x) / (max_x - min_x);
  7684. window->DC.StateStorage->SetFloat(column_id, t);
  7685. window->DC.ColumnsOffsetsT[column_index] = t;
  7686. }
  7687. float ImGui::GetColumnWidth(int column_index)
  7688. {
  7689. ImGuiWindow* window = GetCurrentWindow();
  7690. if (column_index < 0)
  7691. column_index = window->DC.ColumnsCurrent;
  7692. const float w = GetColumnOffset(column_index+1) - GetColumnOffset(column_index);
  7693. return w;
  7694. }
  7695. static void PushColumnClipRect(int column_index)
  7696. {
  7697. ImGuiWindow* window = GetCurrentWindow();
  7698. if (column_index < 0)
  7699. column_index = window->DC.ColumnsCurrent;
  7700. const float x1 = window->Pos.x + ImGui::GetColumnOffset(column_index) - 1;
  7701. const float x2 = window->Pos.x + ImGui::GetColumnOffset(column_index+1) - 1;
  7702. PushClipRect(ImVec4(x1,-FLT_MAX,x2,+FLT_MAX));
  7703. }
  7704. void ImGui::Columns(int columns_count, const char* id, bool border)
  7705. {
  7706. ImGuiState& g = *GImGui;
  7707. ImGuiWindow* window = GetCurrentWindow();
  7708. if (window->DC.ColumnsCount != 1)
  7709. {
  7710. if (window->DC.ColumnsCurrent != 0)
  7711. ItemSize(ImVec2(0,0)); // Advance to column 0
  7712. ImGui::PopItemWidth();
  7713. PopClipRect();
  7714. window->DrawList->ChannelsMerge(window->DC.ColumnsCount);
  7715. window->DC.ColumnsCellMaxY = ImMax(window->DC.ColumnsCellMaxY, window->DC.CursorPos.y);
  7716. window->DC.CursorPos.y = window->DC.ColumnsCellMaxY;
  7717. }
  7718. // Draw columns borders and handle resize at the time of "closing" a columns set
  7719. if (window->DC.ColumnsCount != columns_count && window->DC.ColumnsCount != 1 && window->DC.ColumnsShowBorders && !window->SkipItems)
  7720. {
  7721. const float y1 = window->DC.ColumnsStartPos.y;
  7722. const float y2 = window->DC.CursorPos.y;
  7723. for (int i = 1; i < window->DC.ColumnsCount; i++)
  7724. {
  7725. float x = window->Pos.x + GetColumnOffset(i);
  7726. const ImGuiID column_id = window->DC.ColumnsSetID + ImGuiID(i);
  7727. const ImRect column_rect(ImVec2(x-4,y1),ImVec2(x+4,y2));
  7728. if (IsClippedEx(column_rect, &column_id, false))
  7729. continue;
  7730. bool hovered, held;
  7731. ButtonBehavior(column_rect, column_id, &hovered, &held, true);
  7732. if (hovered || held)
  7733. g.MouseCursor = ImGuiMouseCursor_ResizeEW;
  7734. // Draw before resize so our items positioning are in sync with the line being drawn
  7735. const ImU32 col = window->Color(held ? ImGuiCol_ColumnActive : hovered ? ImGuiCol_ColumnHovered : ImGuiCol_Column);
  7736. const float xi = (float)(int)x;
  7737. window->DrawList->AddLine(ImVec2(xi, y1+1.0f), ImVec2(xi, y2), col);
  7738. if (held)
  7739. {
  7740. if (g.ActiveIdIsJustActivated)
  7741. g.ActiveClickDeltaToCenter.x = x - g.IO.MousePos.x;
  7742. x = GetDraggedColumnOffset(i);
  7743. SetColumnOffset(i, x);
  7744. }
  7745. }
  7746. }
  7747. // Set state for first column
  7748. ImGui::PushID(0x11223344); // Differentiate column ID with an arbitrary/random prefix for cases where users name their columns set the same as another non-scope widget
  7749. window->DC.ColumnsSetID = window->GetID(id ? id : "");
  7750. ImGui::PopID();
  7751. window->DC.ColumnsCurrent = 0;
  7752. window->DC.ColumnsCount = columns_count;
  7753. window->DC.ColumnsShowBorders = border;
  7754. window->DC.ColumnsStartPos = window->DC.CursorPos;
  7755. window->DC.ColumnsCellMinY = window->DC.ColumnsCellMaxY = window->DC.CursorPos.y;
  7756. window->DC.ColumnsOffsetX = 0.0f;
  7757. window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.ColumnsStartX + window->DC.ColumnsOffsetX);
  7758. if (window->DC.ColumnsCount != 1)
  7759. {
  7760. // Cache column offsets
  7761. window->DC.ColumnsOffsetsT.resize(columns_count + 1);
  7762. for (int column_index = 0; column_index < columns_count + 1; column_index++)
  7763. {
  7764. const ImGuiID column_id = window->DC.ColumnsSetID + ImGuiID(column_index);
  7765. RegisterAliveId(column_id);
  7766. const float default_t = column_index / (float)window->DC.ColumnsCount;
  7767. const float t = window->DC.StateStorage->GetFloat(column_id, default_t); // Cheaply store our floating point value inside the integer (could store an union into the map?)
  7768. window->DC.ColumnsOffsetsT[column_index] = t;
  7769. }
  7770. window->DrawList->ChannelsSplit(window->DC.ColumnsCount);
  7771. PushColumnClipRect();
  7772. ImGui::PushItemWidth(ImGui::GetColumnWidth() * 0.65f);
  7773. }
  7774. else
  7775. {
  7776. window->DC.ColumnsOffsetsT.resize(2);
  7777. window->DC.ColumnsOffsetsT[0] = 0.0f;
  7778. window->DC.ColumnsOffsetsT[1] = 1.0f;
  7779. }
  7780. }
  7781. void ImGui::Indent()
  7782. {
  7783. ImGuiState& g = *GImGui;
  7784. ImGuiWindow* window = GetCurrentWindow();
  7785. window->DC.ColumnsStartX += g.Style.IndentSpacing;
  7786. window->DC.CursorPos.x = window->Pos.x + window->DC.ColumnsStartX + window->DC.ColumnsOffsetX;
  7787. }
  7788. void ImGui::Unindent()
  7789. {
  7790. ImGuiState& g = *GImGui;
  7791. ImGuiWindow* window = GetCurrentWindow();
  7792. window->DC.ColumnsStartX -= g.Style.IndentSpacing;
  7793. window->DC.CursorPos.x = window->Pos.x + window->DC.ColumnsStartX + window->DC.ColumnsOffsetX;
  7794. }
  7795. void ImGui::TreePush(const char* str_id)
  7796. {
  7797. ImGuiWindow* window = GetCurrentWindow();
  7798. ImGui::Indent();
  7799. window->DC.TreeDepth++;
  7800. PushID(str_id ? str_id : "#TreePush");
  7801. }
  7802. void ImGui::TreePush(const void* ptr_id)
  7803. {
  7804. ImGuiWindow* window = GetCurrentWindow();
  7805. ImGui::Indent();
  7806. window->DC.TreeDepth++;
  7807. PushID(ptr_id ? ptr_id : (const void*)"#TreePush");
  7808. }
  7809. void ImGui::TreePop()
  7810. {
  7811. ImGuiWindow* window = GetCurrentWindow();
  7812. ImGui::Unindent();
  7813. window->DC.TreeDepth--;
  7814. PopID();
  7815. }
  7816. void ImGui::Value(const char* prefix, bool b)
  7817. {
  7818. ImGui::Text("%s: %s", prefix, (b ? "true" : "false"));
  7819. }
  7820. void ImGui::Value(const char* prefix, int v)
  7821. {
  7822. ImGui::Text("%s: %d", prefix, v);
  7823. }
  7824. void ImGui::Value(const char* prefix, unsigned int v)
  7825. {
  7826. ImGui::Text("%s: %d", prefix, v);
  7827. }
  7828. void ImGui::Value(const char* prefix, float v, const char* float_format)
  7829. {
  7830. if (float_format)
  7831. {
  7832. char fmt[64];
  7833. ImFormatString(fmt, IM_ARRAYSIZE(fmt), "%%s: %s", float_format);
  7834. ImGui::Text(fmt, prefix, v);
  7835. }
  7836. else
  7837. {
  7838. ImGui::Text("%s: %.3f", prefix, v);
  7839. }
  7840. }
  7841. void ImGui::Color(const char* prefix, const ImVec4& v)
  7842. {
  7843. ImGui::Text("%s: (%.2f,%.2f,%.2f,%.2f)", prefix, v.x, v.y, v.z, v.w);
  7844. ImGui::SameLine();
  7845. ImGui::ColorButton(v, true);
  7846. }
  7847. void ImGui::Color(const char* prefix, unsigned int v)
  7848. {
  7849. ImGui::Text("%s: %08X", prefix, v);
  7850. ImGui::SameLine();
  7851. ImVec4 col;
  7852. col.x = (float)((v >> 0) & 0xFF) / 255.0f;
  7853. col.y = (float)((v >> 8) & 0xFF) / 255.0f;
  7854. col.z = (float)((v >> 16) & 0xFF) / 255.0f;
  7855. col.w = (float)((v >> 24) & 0xFF) / 255.0f;
  7856. ImGui::ColorButton(col, true);
  7857. }
  7858. //-----------------------------------------------------------------------------
  7859. // ImDrawList
  7860. //-----------------------------------------------------------------------------
  7861. static ImVec4 GNullClipRect(-9999.0f,-9999.0f, +9999.0f, +9999.0f);
  7862. void ImDrawList::Clear()
  7863. {
  7864. CmdBuffer.resize(0);
  7865. IdxBuffer.resize(0);
  7866. VtxBuffer.resize(0);
  7867. _VtxCurrentIdx = 0;
  7868. _VtxWritePtr = NULL;
  7869. _IdxWritePtr = NULL;
  7870. _ClipRectStack.resize(0);
  7871. _TextureIdStack.resize(0);
  7872. _Path.resize(0);
  7873. _ChannelCurrent = 0;
  7874. // NB: Do not clear channels so our allocations are re-used after the first frame.
  7875. }
  7876. void ImDrawList::ClearFreeMemory()
  7877. {
  7878. CmdBuffer.clear();
  7879. IdxBuffer.clear();
  7880. VtxBuffer.clear();
  7881. _VtxCurrentIdx = 0;
  7882. _VtxWritePtr = NULL;
  7883. _IdxWritePtr = NULL;
  7884. _ClipRectStack.clear();
  7885. _TextureIdStack.clear();
  7886. _Path.clear();
  7887. _ChannelCurrent = 0;
  7888. for (int i = 0; i < _Channels.Size; i++)
  7889. {
  7890. if (i == 0) memset(&_Channels[0], 0, sizeof(_Channels[0])); // channel 0 is a copy of CmdBuffer/IdxBuffer, don't destruct again
  7891. _Channels[i].CmdBuffer.clear();
  7892. _Channels[i].IdxBuffer.clear();
  7893. }
  7894. _Channels.clear();
  7895. }
  7896. void ImDrawList::AddDrawCmd()
  7897. {
  7898. ImDrawCmd draw_cmd;
  7899. draw_cmd.ElemCount = 0;
  7900. draw_cmd.ClipRect = _ClipRectStack.Size ? _ClipRectStack.back() : GNullClipRect;
  7901. draw_cmd.TextureId = _TextureIdStack.Size ? _TextureIdStack.back() : NULL;
  7902. draw_cmd.UserCallback = NULL;
  7903. draw_cmd.UserCallbackData = NULL;
  7904. IM_ASSERT(draw_cmd.ClipRect.x <= draw_cmd.ClipRect.z && draw_cmd.ClipRect.y <= draw_cmd.ClipRect.w);
  7905. CmdBuffer.push_back(draw_cmd);
  7906. }
  7907. void ImDrawList::AddCallback(ImDrawCallback callback, void* callback_data)
  7908. {
  7909. ImDrawCmd* current_cmd = CmdBuffer.Size ? &CmdBuffer.back() : NULL;
  7910. if (!current_cmd || current_cmd->ElemCount != 0 || current_cmd->UserCallback != NULL)
  7911. {
  7912. AddDrawCmd();
  7913. current_cmd = &CmdBuffer.back();
  7914. }
  7915. current_cmd->UserCallback = callback;
  7916. current_cmd->UserCallbackData = callback_data;
  7917. // Force a new command after us (we function this way so that the most common calls AddLine, AddRect, etc. always have a command to add to without doing any check).
  7918. AddDrawCmd();
  7919. }
  7920. void ImDrawList::ChannelsSplit(int channel_count)
  7921. {
  7922. IM_ASSERT(_ChannelCurrent == 0);
  7923. int old_channels_count = _Channels.Size;
  7924. if (old_channels_count < channel_count)
  7925. _Channels.resize(channel_count);
  7926. for (int i = 0; i < channel_count; i++)
  7927. if (i >= old_channels_count)
  7928. new(&_Channels[i]) ImDrawChannel();
  7929. else
  7930. _Channels[i].CmdBuffer.resize(0), _Channels[i].IdxBuffer.resize(0);
  7931. }
  7932. void ImDrawList::ChannelsMerge(int channel_count)
  7933. {
  7934. // Note that we never use or rely on channels.Size because it is merely a buffer that we never shrink back to 0 to keep all sub-buffers ready for use.
  7935. // This is why this function takes 'channel_count' as a parameter of how many channels to merge (the user knows)
  7936. if (channel_count < 2)
  7937. return;
  7938. ChannelsSetCurrent(0);
  7939. if (CmdBuffer.Size && CmdBuffer.back().ElemCount == 0)
  7940. CmdBuffer.pop_back();
  7941. int new_cmd_buffer_count = 0, new_idx_buffer_count = 0;
  7942. for (int i = 1; i < channel_count; i++)
  7943. {
  7944. ImDrawChannel& ch = _Channels[i];
  7945. if (ch.CmdBuffer.Size && ch.CmdBuffer.back().ElemCount == 0)
  7946. ch.CmdBuffer.pop_back();
  7947. new_cmd_buffer_count += ch.CmdBuffer.Size;
  7948. new_idx_buffer_count += ch.IdxBuffer.Size;
  7949. }
  7950. CmdBuffer.resize(CmdBuffer.Size + new_cmd_buffer_count);
  7951. IdxBuffer.resize(IdxBuffer.Size + new_idx_buffer_count);
  7952. ImDrawCmd* cmd_write = CmdBuffer.Data + CmdBuffer.Size - new_cmd_buffer_count;
  7953. _IdxWritePtr = IdxBuffer.Data + IdxBuffer.Size - new_idx_buffer_count;
  7954. for (int i = 1; i < channel_count; i++)
  7955. {
  7956. ImDrawChannel& ch = _Channels[i];
  7957. if (int sz = ch.CmdBuffer.Size) { memcpy(cmd_write, ch.CmdBuffer.Data, sz * sizeof(ImDrawCmd)); cmd_write += sz; }
  7958. if (int sz = ch.IdxBuffer.Size) { memcpy(_IdxWritePtr, ch.IdxBuffer.Data, sz * sizeof(ImDrawIdx)); _IdxWritePtr += sz; }
  7959. }
  7960. AddDrawCmd();
  7961. }
  7962. void ImDrawList::ChannelsSetCurrent(int idx)
  7963. {
  7964. if (_ChannelCurrent == idx) return;
  7965. memcpy(&_Channels.Data[_ChannelCurrent].CmdBuffer, &CmdBuffer, sizeof(CmdBuffer));
  7966. memcpy(&_Channels.Data[_ChannelCurrent].IdxBuffer, &IdxBuffer, sizeof(IdxBuffer));
  7967. _ChannelCurrent = idx;
  7968. memcpy(&CmdBuffer, &_Channels.Data[_ChannelCurrent].CmdBuffer, sizeof(CmdBuffer));
  7969. memcpy(&IdxBuffer, &_Channels.Data[_ChannelCurrent].IdxBuffer, sizeof(IdxBuffer));
  7970. _IdxWritePtr = IdxBuffer.Data + IdxBuffer.Size;
  7971. }
  7972. void ImDrawList::UpdateClipRect()
  7973. {
  7974. ImDrawCmd* current_cmd = CmdBuffer.Size ? &CmdBuffer.back() : NULL;
  7975. if (!current_cmd || (current_cmd->ElemCount != 0) || current_cmd->UserCallback != NULL)
  7976. {
  7977. AddDrawCmd();
  7978. }
  7979. else
  7980. {
  7981. ImVec4 current_clip_rect = _ClipRectStack.Size ? _ClipRectStack.back() : GNullClipRect;
  7982. if (CmdBuffer.Size >= 2 && ImLengthSqr(CmdBuffer.Data[CmdBuffer.Size-2].ClipRect - current_clip_rect) < 0.00001f)
  7983. CmdBuffer.pop_back();
  7984. else
  7985. current_cmd->ClipRect = current_clip_rect;
  7986. }
  7987. }
  7988. // Scissoring. The values in clip_rect are x1, y1, x2, y2.
  7989. void ImDrawList::PushClipRect(const ImVec4& clip_rect)
  7990. {
  7991. _ClipRectStack.push_back(clip_rect);
  7992. UpdateClipRect();
  7993. }
  7994. void ImDrawList::PushClipRectFullScreen()
  7995. {
  7996. PushClipRect(GNullClipRect);
  7997. // This would be more correct but we're not supposed to access ImGuiState from here?
  7998. //ImGuiState& g = *GImGui;
  7999. //PushClipRect(GetVisibleRect());
  8000. }
  8001. void ImDrawList::PopClipRect()
  8002. {
  8003. IM_ASSERT(_ClipRectStack.Size > 0);
  8004. _ClipRectStack.pop_back();
  8005. UpdateClipRect();
  8006. }
  8007. void ImDrawList::UpdateTextureID()
  8008. {
  8009. ImDrawCmd* current_cmd = CmdBuffer.Size ? &CmdBuffer.back() : NULL;
  8010. const ImTextureID texture_id = _TextureIdStack.Size ? _TextureIdStack.back() : NULL;
  8011. if (!current_cmd || (current_cmd->ElemCount != 0 && current_cmd->TextureId != texture_id) || current_cmd->UserCallback != NULL)
  8012. AddDrawCmd();
  8013. else
  8014. current_cmd->TextureId = texture_id;
  8015. }
  8016. void ImDrawList::PushTextureID(const ImTextureID& texture_id)
  8017. {
  8018. _TextureIdStack.push_back(texture_id);
  8019. UpdateTextureID();
  8020. }
  8021. void ImDrawList::PopTextureID()
  8022. {
  8023. IM_ASSERT(_TextureIdStack.Size > 0);
  8024. _TextureIdStack.pop_back();
  8025. UpdateTextureID();
  8026. }
  8027. // NB: this can be called with negative count for removing primitives (as long as the result does not underflow)
  8028. void ImDrawList::PrimReserve(int idx_count, int vtx_count)
  8029. {
  8030. ImDrawCmd& draw_cmd = CmdBuffer.Data[CmdBuffer.Size-1];
  8031. draw_cmd.ElemCount += idx_count;
  8032. int vtx_buffer_size = VtxBuffer.Size;
  8033. VtxBuffer.resize(vtx_buffer_size + vtx_count);
  8034. _VtxWritePtr = VtxBuffer.Data + vtx_buffer_size;
  8035. int idx_buffer_size = IdxBuffer.Size;
  8036. IdxBuffer.resize(idx_buffer_size + idx_count);
  8037. _IdxWritePtr = IdxBuffer.Data + idx_buffer_size;
  8038. }
  8039. void ImDrawList::PrimRect(const ImVec2& a, const ImVec2& c, ImU32 col)
  8040. {
  8041. const ImVec2 uv = GImGui->FontTexUvWhitePixel;
  8042. const ImVec2 b(c.x, a.y);
  8043. const ImVec2 d(a.x, c.y);
  8044. _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx+1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx+2);
  8045. _IdxWritePtr[3] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[4] = (ImDrawIdx)(_VtxCurrentIdx+2); _IdxWritePtr[5] = (ImDrawIdx)(_VtxCurrentIdx+3);
  8046. _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col;
  8047. _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col;
  8048. _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col;
  8049. _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col;
  8050. _VtxWritePtr += 4;
  8051. _VtxCurrentIdx += 4;
  8052. _IdxWritePtr += 6;
  8053. }
  8054. void ImDrawList::PrimRectUV(const ImVec2& a, const ImVec2& c, const ImVec2& uv_a, const ImVec2& uv_c, ImU32 col)
  8055. {
  8056. const ImVec2 b(c.x, a.y);
  8057. const ImVec2 d(a.x, c.y);
  8058. const ImVec2 uv_b(uv_c.x, uv_a.y);
  8059. const ImVec2 uv_d(uv_a.x, uv_c.y);
  8060. _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx+1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx+2);
  8061. _IdxWritePtr[3] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[4] = (ImDrawIdx)(_VtxCurrentIdx+2); _IdxWritePtr[5] = (ImDrawIdx)(_VtxCurrentIdx+3);
  8062. _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv_a; _VtxWritePtr[0].col = col;
  8063. _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv_b; _VtxWritePtr[1].col = col;
  8064. _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv_c; _VtxWritePtr[2].col = col;
  8065. _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv_d; _VtxWritePtr[3].col = col;
  8066. _VtxWritePtr += 4;
  8067. _VtxCurrentIdx += 4;
  8068. _IdxWritePtr += 6;
  8069. }
  8070. // TODO: Thickness anti-aliased lines cap are missing their AA fringe.
  8071. void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 col, bool closed, float thickness, bool anti_aliased)
  8072. {
  8073. if (points_count < 2)
  8074. return;
  8075. const ImVec2 uv = GImGui->FontTexUvWhitePixel;
  8076. anti_aliased &= GImGui->Style.AntiAliasedLines;
  8077. //if (ImGui::GetIO().KeyCtrl) anti_aliased = false;
  8078. int count = points_count;
  8079. if (!closed)
  8080. count = points_count-1;
  8081. const bool thick_line = thickness > 1.0f;
  8082. if (anti_aliased)
  8083. {
  8084. // Anti-aliased stroke
  8085. const float AA_SIZE = 1.0f;
  8086. const ImU32 col_trans = col & 0x00ffffff;
  8087. const int idx_count = thick_line ? count*18 : count*12;
  8088. const int vtx_count = thick_line ? points_count*4 : points_count*3;
  8089. PrimReserve(idx_count, vtx_count);
  8090. // Temporary buffer
  8091. ImVec2* temp_normals = (ImVec2*)alloca(points_count * (thick_line ? 5 : 3) * sizeof(ImVec2));
  8092. ImVec2* temp_points = temp_normals + points_count;
  8093. for (int i1 = 0; i1 < count; i1++)
  8094. {
  8095. const int i2 = (i1+1) == points_count ? 0 : i1+1;
  8096. ImVec2 diff = points[i2] - points[i1];
  8097. diff *= ImInvLength(diff, 1.0f);
  8098. temp_normals[i1].x = diff.y;
  8099. temp_normals[i1].y = -diff.x;
  8100. }
  8101. if (!closed)
  8102. temp_normals[points_count-1] = temp_normals[points_count-2];
  8103. if (!thick_line)
  8104. {
  8105. if (!closed)
  8106. {
  8107. temp_points[0] = points[0] + temp_normals[0] * AA_SIZE;
  8108. temp_points[1] = points[0] - temp_normals[0] * AA_SIZE;
  8109. temp_points[(points_count-1)*2+0] = points[points_count-1] + temp_normals[points_count-1] * AA_SIZE;
  8110. temp_points[(points_count-1)*2+1] = points[points_count-1] - temp_normals[points_count-1] * AA_SIZE;
  8111. }
  8112. // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer.
  8113. unsigned int idx1 = _VtxCurrentIdx;
  8114. for (int i1 = 0; i1 < count; i1++)
  8115. {
  8116. const int i2 = (i1+1) == points_count ? 0 : i1+1;
  8117. unsigned int idx2 = (i1+1) == points_count ? _VtxCurrentIdx : idx1+3;
  8118. // Average normals
  8119. ImVec2 dm = (temp_normals[i1] + temp_normals[i2]) * 0.5f;
  8120. float dmr2 = dm.x*dm.x + dm.y*dm.y;
  8121. if (dmr2 > 0.000001f)
  8122. {
  8123. float scale = 1.0f / dmr2;
  8124. if (scale > 100.0f) scale = 100.0f;
  8125. dm *= scale;
  8126. }
  8127. dm *= AA_SIZE;
  8128. temp_points[i2*2+0] = points[i2] + dm;
  8129. temp_points[i2*2+1] = points[i2] - dm;
  8130. // Add indexes
  8131. _IdxWritePtr[0] = (ImDrawIdx)(idx2+0); _IdxWritePtr[1] = (ImDrawIdx)(idx1+0); _IdxWritePtr[2] = (ImDrawIdx)(idx1+2);
  8132. _IdxWritePtr[3] = (ImDrawIdx)(idx1+2); _IdxWritePtr[4] = (ImDrawIdx)(idx2+2); _IdxWritePtr[5] = (ImDrawIdx)(idx2+0);
  8133. _IdxWritePtr[6] = (ImDrawIdx)(idx2+1); _IdxWritePtr[7] = (ImDrawIdx)(idx1+1); _IdxWritePtr[8] = (ImDrawIdx)(idx1+0);
  8134. _IdxWritePtr[9] = (ImDrawIdx)(idx1+0); _IdxWritePtr[10]= (ImDrawIdx)(idx2+0); _IdxWritePtr[11]= (ImDrawIdx)(idx2+1);
  8135. _IdxWritePtr += 12;
  8136. idx1 = idx2;
  8137. }
  8138. // Add vertexes
  8139. for (int i = 0; i < points_count; i++)
  8140. {
  8141. _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col;
  8142. _VtxWritePtr[1].pos = temp_points[i*2+0]; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col_trans;
  8143. _VtxWritePtr[2].pos = temp_points[i*2+1]; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col_trans;
  8144. _VtxWritePtr += 3;
  8145. }
  8146. }
  8147. else
  8148. {
  8149. const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f;
  8150. if (!closed)
  8151. {
  8152. temp_points[0] = points[0] + temp_normals[0] * (half_inner_thickness + AA_SIZE);
  8153. temp_points[1] = points[0] + temp_normals[0] * (half_inner_thickness);
  8154. temp_points[2] = points[0] - temp_normals[0] * (half_inner_thickness);
  8155. temp_points[3] = points[0] - temp_normals[0] * (half_inner_thickness + AA_SIZE);
  8156. temp_points[(points_count-1)*4+0] = points[points_count-1] + temp_normals[points_count-1] * (half_inner_thickness + AA_SIZE);
  8157. temp_points[(points_count-1)*4+1] = points[points_count-1] + temp_normals[points_count-1] * (half_inner_thickness);
  8158. temp_points[(points_count-1)*4+2] = points[points_count-1] - temp_normals[points_count-1] * (half_inner_thickness);
  8159. temp_points[(points_count-1)*4+3] = points[points_count-1] - temp_normals[points_count-1] * (half_inner_thickness + AA_SIZE);
  8160. }
  8161. // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer.
  8162. unsigned int idx1 = _VtxCurrentIdx;
  8163. for (int i1 = 0; i1 < count; i1++)
  8164. {
  8165. const int i2 = (i1+1) == points_count ? 0 : i1+1;
  8166. unsigned int idx2 = (i1+1) == points_count ? _VtxCurrentIdx : idx1+4;
  8167. // Average normals
  8168. ImVec2 dm = (temp_normals[i1] + temp_normals[i2]) * 0.5f;
  8169. float dmr2 = dm.x*dm.x + dm.y*dm.y;
  8170. if (dmr2 > 0.000001f)
  8171. {
  8172. float scale = 1.0f / dmr2;
  8173. if (scale > 100.0f) scale = 100.0f;
  8174. dm *= scale;
  8175. }
  8176. ImVec2 dm_out = dm * (half_inner_thickness + AA_SIZE);
  8177. ImVec2 dm_in = dm * half_inner_thickness;
  8178. temp_points[i2*4+0] = points[i2] + dm_out;
  8179. temp_points[i2*4+1] = points[i2] + dm_in;
  8180. temp_points[i2*4+2] = points[i2] - dm_in;
  8181. temp_points[i2*4+3] = points[i2] - dm_out;
  8182. // Add indexes
  8183. _IdxWritePtr[0] = (ImDrawIdx)(idx2+1); _IdxWritePtr[1] = (ImDrawIdx)(idx1+1); _IdxWritePtr[2] = (ImDrawIdx)(idx1+2);
  8184. _IdxWritePtr[3] = (ImDrawIdx)(idx1+2); _IdxWritePtr[4] = (ImDrawIdx)(idx2+2); _IdxWritePtr[5] = (ImDrawIdx)(idx2+1);
  8185. _IdxWritePtr[6] = (ImDrawIdx)(idx2+1); _IdxWritePtr[7] = (ImDrawIdx)(idx1+1); _IdxWritePtr[8] = (ImDrawIdx)(idx1+0);
  8186. _IdxWritePtr[9] = (ImDrawIdx)(idx1+0); _IdxWritePtr[10] = (ImDrawIdx)(idx2+0); _IdxWritePtr[11] = (ImDrawIdx)(idx2+1);
  8187. _IdxWritePtr[12] = (ImDrawIdx)(idx2+2); _IdxWritePtr[13] = (ImDrawIdx)(idx1+2); _IdxWritePtr[14] = (ImDrawIdx)(idx1+3);
  8188. _IdxWritePtr[15] = (ImDrawIdx)(idx1+3); _IdxWritePtr[16] = (ImDrawIdx)(idx2+3); _IdxWritePtr[17] = (ImDrawIdx)(idx2+2);
  8189. _IdxWritePtr += 18;
  8190. idx1 = idx2;
  8191. }
  8192. // Add vertexes
  8193. for (int i = 0; i < points_count; i++)
  8194. {
  8195. _VtxWritePtr[0].pos = temp_points[i*4+0]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col_trans;
  8196. _VtxWritePtr[1].pos = temp_points[i*4+1]; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col;
  8197. _VtxWritePtr[2].pos = temp_points[i*4+2]; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col;
  8198. _VtxWritePtr[3].pos = temp_points[i*4+3]; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col_trans;
  8199. _VtxWritePtr += 4;
  8200. }
  8201. }
  8202. _VtxCurrentIdx += (ImDrawIdx)vtx_count;
  8203. }
  8204. else
  8205. {
  8206. // Non Anti-aliased Stroke
  8207. const int idx_count = count*6;
  8208. const int vtx_count = count*4; // FIXME-OPT: Not sharing edges
  8209. PrimReserve(idx_count, vtx_count);
  8210. for (int i1 = 0; i1 < count; i1++)
  8211. {
  8212. const int i2 = (i1+1) == points_count ? 0 : i1+1;
  8213. const ImVec2& p1 = points[i1];
  8214. const ImVec2& p2 = points[i2];
  8215. ImVec2 diff = p2 - p1;
  8216. diff *= ImInvLength(diff, 1.0f);
  8217. const float dx = diff.x * (thickness * 0.5f);
  8218. const float dy = diff.y * (thickness * 0.5f);
  8219. _VtxWritePtr[0].pos.x = p1.x + dy; _VtxWritePtr[0].pos.y = p1.y - dx; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col;
  8220. _VtxWritePtr[1].pos.x = p2.x + dy; _VtxWritePtr[1].pos.y = p2.y - dx; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col;
  8221. _VtxWritePtr[2].pos.x = p2.x - dy; _VtxWritePtr[2].pos.y = p2.y + dx; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col;
  8222. _VtxWritePtr[3].pos.x = p1.x - dy; _VtxWritePtr[3].pos.y = p1.y + dx; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col;
  8223. _VtxWritePtr += 4;
  8224. _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx+1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx+2);
  8225. _IdxWritePtr[3] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[4] = (ImDrawIdx)(_VtxCurrentIdx+2); _IdxWritePtr[5] = (ImDrawIdx)(_VtxCurrentIdx+3);
  8226. _IdxWritePtr += 6;
  8227. _VtxCurrentIdx += 4;
  8228. }
  8229. }
  8230. }
  8231. void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_count, ImU32 col, bool anti_aliased)
  8232. {
  8233. const ImVec2 uv = GImGui->FontTexUvWhitePixel;
  8234. anti_aliased &= GImGui->Style.AntiAliasedShapes;
  8235. //if (ImGui::GetIO().KeyCtrl) anti_aliased = false;
  8236. if (anti_aliased)
  8237. {
  8238. // Anti-aliased Fill
  8239. const float AA_SIZE = 1.0f;
  8240. const ImU32 col_trans = col & 0x00ffffff;
  8241. const int idx_count = (points_count-2)*3 + points_count*6;
  8242. const int vtx_count = (points_count*2);
  8243. PrimReserve(idx_count, vtx_count);
  8244. // Add indexes for fill
  8245. unsigned int vtx_inner_idx = _VtxCurrentIdx;
  8246. unsigned int vtx_outer_idx = _VtxCurrentIdx+1;
  8247. for (int i = 2; i < points_count; i++)
  8248. {
  8249. _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx+((i-1)<<1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_inner_idx+(i<<1));
  8250. _IdxWritePtr += 3;
  8251. }
  8252. // Compute normals
  8253. ImVec2* temp_normals = (ImVec2*)alloca(points_count * sizeof(ImVec2));
  8254. for (int i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++)
  8255. {
  8256. const ImVec2& p0 = points[i0];
  8257. const ImVec2& p1 = points[i1];
  8258. ImVec2 diff = p1 - p0;
  8259. diff *= ImInvLength(diff, 1.0f);
  8260. temp_normals[i0].x = diff.y;
  8261. temp_normals[i0].y = -diff.x;
  8262. }
  8263. for (int i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++)
  8264. {
  8265. // Average normals
  8266. const ImVec2& n0 = temp_normals[i0];
  8267. const ImVec2& n1 = temp_normals[i1];
  8268. ImVec2 dm = (n0 + n1) * 0.5f;
  8269. float dmr2 = dm.x*dm.x + dm.y*dm.y;
  8270. if (dmr2 > 0.000001f)
  8271. {
  8272. float scale = 1.0f / dmr2;
  8273. if (scale > 100.0f) scale = 100.0f;
  8274. dm *= scale;
  8275. }
  8276. dm *= AA_SIZE * 0.5f;
  8277. // Add vertices
  8278. _VtxWritePtr[0].pos = (points[i1] - dm); _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; // Inner
  8279. _VtxWritePtr[1].pos = (points[i1] + dm); _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col_trans; // Outer
  8280. _VtxWritePtr += 2;
  8281. // Add indexes for fringes
  8282. _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx+(i1<<1)); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx+(i0<<1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_outer_idx+(i0<<1));
  8283. _IdxWritePtr[3] = (ImDrawIdx)(vtx_outer_idx+(i0<<1)); _IdxWritePtr[4] = (ImDrawIdx)(vtx_outer_idx+(i1<<1)); _IdxWritePtr[5] = (ImDrawIdx)(vtx_inner_idx+(i1<<1));
  8284. _IdxWritePtr += 6;
  8285. }
  8286. _VtxCurrentIdx += (ImDrawIdx)vtx_count;
  8287. }
  8288. else
  8289. {
  8290. // Non Anti-aliased Fill
  8291. const int idx_count = (points_count-2)*3;
  8292. const int vtx_count = points_count;
  8293. PrimReserve(idx_count, vtx_count);
  8294. for (int i = 0; i < vtx_count; i++)
  8295. {
  8296. _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col;
  8297. _VtxWritePtr++;
  8298. }
  8299. for (int i = 2; i < points_count; i++)
  8300. {
  8301. _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx+i-1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx+i);
  8302. _IdxWritePtr += 3;
  8303. }
  8304. _VtxCurrentIdx += (ImDrawIdx)vtx_count;
  8305. }
  8306. }
  8307. void ImDrawList::PathArcToFast(const ImVec2& centre, float radius, int amin, int amax)
  8308. {
  8309. static ImVec2 circle_vtx[12];
  8310. static bool circle_vtx_builds = false;
  8311. const int circle_vtx_count = IM_ARRAYSIZE(circle_vtx);
  8312. if (!circle_vtx_builds)
  8313. {
  8314. for (int i = 0; i < circle_vtx_count; i++)
  8315. {
  8316. const float a = ((float)i / (float)circle_vtx_count) * 2*IM_PI;
  8317. circle_vtx[i].x = cosf(a);
  8318. circle_vtx[i].y = sinf(a);
  8319. }
  8320. circle_vtx_builds = true;
  8321. }
  8322. if (amin > amax) return;
  8323. if (radius == 0.0f)
  8324. {
  8325. _Path.push_back(centre);
  8326. }
  8327. else
  8328. {
  8329. _Path.reserve(_Path.Size + (amax - amin + 1));
  8330. for (int a = amin; a <= amax; a++)
  8331. {
  8332. const ImVec2& c = circle_vtx[a % circle_vtx_count];
  8333. _Path.push_back(ImVec2(centre.x + c.x * radius, centre.y + c.y * radius));
  8334. }
  8335. }
  8336. }
  8337. void ImDrawList::PathArcTo(const ImVec2& centre, float radius, float amin, float amax, int num_segments)
  8338. {
  8339. if (radius == 0.0f)
  8340. _Path.push_back(centre);
  8341. _Path.reserve(_Path.Size + (num_segments + 1));
  8342. for (int i = 0; i <= num_segments; i++)
  8343. {
  8344. const float a = amin + ((float)i / (float)num_segments) * (amax - amin);
  8345. _Path.push_back(ImVec2(centre.x + cosf(a) * radius, centre.y + sinf(a) * radius));
  8346. }
  8347. }
  8348. void ImDrawList::PathRect(const ImVec2& a, const ImVec2& b, float rounding, int rounding_corners)
  8349. {
  8350. float r = rounding;
  8351. r = ImMin(r, fabsf(b.x-a.x) * ( ((rounding_corners&(1|2))==(1|2)) || ((rounding_corners&(4|8))==(4|8)) ? 0.5f : 1.0f ) - 1.0f);
  8352. r = ImMin(r, fabsf(b.y-a.y) * ( ((rounding_corners&(1|8))==(1|8)) || ((rounding_corners&(2|4))==(2|4)) ? 0.5f : 1.0f ) - 1.0f);
  8353. if (r == 0.0f || rounding_corners == 0)
  8354. {
  8355. PathLineTo(a);
  8356. PathLineTo(ImVec2(b.x,a.y));
  8357. PathLineTo(b);
  8358. PathLineTo(ImVec2(a.x,b.y));
  8359. }
  8360. else
  8361. {
  8362. const float r0 = (rounding_corners & 1) ? r : 0.0f;
  8363. const float r1 = (rounding_corners & 2) ? r : 0.0f;
  8364. const float r2 = (rounding_corners & 4) ? r : 0.0f;
  8365. const float r3 = (rounding_corners & 8) ? r : 0.0f;
  8366. PathArcToFast(ImVec2(a.x+r0,a.y+r0), r0, 6, 9);
  8367. PathArcToFast(ImVec2(b.x-r1,a.y+r1), r1, 9, 12);
  8368. PathArcToFast(ImVec2(b.x-r2,b.y-r2), r2, 0, 3);
  8369. PathArcToFast(ImVec2(a.x+r3,b.y-r3), r3, 3, 6);
  8370. }
  8371. }
  8372. void ImDrawList::AddLine(const ImVec2& a, const ImVec2& b, ImU32 col, float thickness)
  8373. {
  8374. if ((col >> 24) == 0)
  8375. return;
  8376. PathLineTo(a + ImVec2(0.5f,0.5f));
  8377. PathLineTo(b + ImVec2(0.5f,0.5f));
  8378. PathStroke(col, false, thickness);
  8379. }
  8380. void ImDrawList::AddRect(const ImVec2& a, const ImVec2& b, ImU32 col, float rounding, int rounding_corners)
  8381. {
  8382. if ((col >> 24) == 0)
  8383. return;
  8384. PathRect(a + ImVec2(0.5f,0.5f), b + ImVec2(0.5f,0.5f), rounding, rounding_corners);
  8385. PathStroke(col, true);
  8386. }
  8387. void ImDrawList::AddRectFilled(const ImVec2& a, const ImVec2& b, ImU32 col, float rounding, int rounding_corners)
  8388. {
  8389. if ((col >> 24) == 0)
  8390. return;
  8391. if (rounding > 0.0f)
  8392. {
  8393. PathRect(a, b, rounding, rounding_corners);
  8394. PathFill(col);
  8395. }
  8396. else
  8397. {
  8398. PrimReserve(6, 4);
  8399. PrimRect(a, b, col);
  8400. }
  8401. }
  8402. void ImDrawList::AddRectFilledMultiColor(const ImVec2& a, const ImVec2& c, ImU32 col_upr_left, ImU32 col_upr_right, ImU32 col_bot_right, ImU32 col_bot_left)
  8403. {
  8404. if (((col_upr_left | col_upr_right | col_bot_right | col_bot_left) >> 24) == 0)
  8405. return;
  8406. const ImVec2 uv = GImGui->FontTexUvWhitePixel;
  8407. PrimReserve(6, 4);
  8408. PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+1)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+2));
  8409. PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+2)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+3));
  8410. PrimWriteVtx(a, uv, col_upr_left);
  8411. PrimWriteVtx(ImVec2(c.x, a.y), uv, col_upr_right);
  8412. PrimWriteVtx(c, uv, col_bot_right);
  8413. PrimWriteVtx(ImVec2(a.x, c.y), uv, col_bot_left);
  8414. }
  8415. void ImDrawList::AddTriangleFilled(const ImVec2& a, const ImVec2& b, const ImVec2& c, ImU32 col)
  8416. {
  8417. if ((col >> 24) == 0)
  8418. return;
  8419. PathLineTo(a);
  8420. PathLineTo(b);
  8421. PathLineTo(c);
  8422. PathFill(col);
  8423. }
  8424. void ImDrawList::AddCircle(const ImVec2& centre, float radius, ImU32 col, int num_segments)
  8425. {
  8426. if ((col >> 24) == 0)
  8427. return;
  8428. const float a_max = IM_PI*2.0f * ((float)num_segments - 1.0f) / (float)num_segments;
  8429. PathArcTo(centre, radius, 0.0f, a_max, num_segments);
  8430. PathStroke(col, true);
  8431. }
  8432. void ImDrawList::AddCircleFilled(const ImVec2& centre, float radius, ImU32 col, int num_segments)
  8433. {
  8434. if ((col >> 24) == 0)
  8435. return;
  8436. const float a_max = IM_PI*2.0f * ((float)num_segments - 1.0f) / (float)num_segments;
  8437. PathArcTo(centre, radius, 0.0f, a_max, num_segments);
  8438. PathFill(col);
  8439. }
  8440. void ImDrawList::AddText(const ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end, float wrap_width, const ImVec4* cpu_fine_clip_rect)
  8441. {
  8442. if ((col >> 24) == 0)
  8443. return;
  8444. if (text_end == NULL)
  8445. text_end = text_begin + strlen(text_begin);
  8446. if (text_begin == text_end)
  8447. return;
  8448. IM_ASSERT(font->ContainerAtlas->TexID == _TextureIdStack.back()); // Use high-level ImGui::PushFont() or low-level ImDrawList::PushTextureId() to change font.
  8449. // reserve vertices for worse case
  8450. const int char_count = (int)(text_end - text_begin);
  8451. const int vtx_count_max = char_count * 4;
  8452. const int idx_count_max = char_count * 6;
  8453. const int vtx_begin = VtxBuffer.Size;
  8454. const int idx_begin = IdxBuffer.Size;
  8455. PrimReserve(idx_count_max, vtx_count_max);
  8456. ImVec4 clip_rect = _ClipRectStack.back();
  8457. if (cpu_fine_clip_rect)
  8458. {
  8459. clip_rect.x = ImMax(clip_rect.x, cpu_fine_clip_rect->x);
  8460. clip_rect.y = ImMax(clip_rect.y, cpu_fine_clip_rect->y);
  8461. clip_rect.z = ImMin(clip_rect.z, cpu_fine_clip_rect->z);
  8462. clip_rect.w = ImMin(clip_rect.w, cpu_fine_clip_rect->w);
  8463. }
  8464. font->RenderText(font_size, pos, col, clip_rect, text_begin, text_end, this, wrap_width, cpu_fine_clip_rect != NULL);
  8465. // give back unused vertices
  8466. // FIXME-OPT
  8467. VtxBuffer.resize((int)(_VtxWritePtr - VtxBuffer.Data));
  8468. IdxBuffer.resize((int)(_IdxWritePtr - IdxBuffer.Data));
  8469. int vtx_unused = vtx_count_max - (VtxBuffer.Size - vtx_begin);
  8470. int idx_unused = idx_count_max - (IdxBuffer.Size - idx_begin);
  8471. CmdBuffer.back().ElemCount -= idx_unused;
  8472. _VtxWritePtr -= vtx_unused;
  8473. _IdxWritePtr -= idx_unused;
  8474. }
  8475. // This is one of the few function breaking the encapsulation of ImDrawLst, but it is just so useful.
  8476. void ImDrawList::AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end)
  8477. {
  8478. if ((col >> 24) == 0)
  8479. return;
  8480. AddText(ImGui::GetWindowFont(), ImGui::GetWindowFontSize(), pos, col, text_begin, text_end);
  8481. }
  8482. void ImDrawList::AddImage(ImTextureID user_texture_id, const ImVec2& a, const ImVec2& b, const ImVec2& uv0, const ImVec2& uv1, ImU32 col)
  8483. {
  8484. if ((col >> 24) == 0)
  8485. return;
  8486. // FIXME-OPT: This is wasting draw calls.
  8487. const bool push_texture_id = _TextureIdStack.empty() || user_texture_id != _TextureIdStack.back();
  8488. if (push_texture_id)
  8489. PushTextureID(user_texture_id);
  8490. PrimReserve(6, 4);
  8491. PrimRectUV(a, b, uv0, uv1, col);
  8492. if (push_texture_id)
  8493. PopTextureID();
  8494. }
  8495. //-----------------------------------------------------------------------------
  8496. // ImDrawData
  8497. //-----------------------------------------------------------------------------
  8498. // For backward compatibility: convert all buffers from indexed to de-indexed, in case you cannot render indexed. Note: this is slow and most likely a waste of resources. Always prefer indexed rendering!
  8499. void ImDrawData::DeIndexAllBuffers()
  8500. {
  8501. ImVector<ImDrawVert> new_vtx_buffer;
  8502. TotalVtxCount = TotalIdxCount = 0;
  8503. for (int i = 0; i < CmdListsCount; i++)
  8504. {
  8505. ImDrawList* cmd_list = CmdLists[i];
  8506. if (cmd_list->IdxBuffer.empty())
  8507. continue;
  8508. new_vtx_buffer.resize(cmd_list->IdxBuffer.Size);
  8509. for (int j = 0; j < cmd_list->IdxBuffer.Size; j++)
  8510. new_vtx_buffer[j] = cmd_list->VtxBuffer[cmd_list->IdxBuffer[j]];
  8511. cmd_list->VtxBuffer.swap(new_vtx_buffer);
  8512. cmd_list->IdxBuffer.resize(0);
  8513. TotalVtxCount += cmd_list->VtxBuffer.Size;
  8514. }
  8515. }
  8516. //-----------------------------------------------------------------------------
  8517. // ImFontAtlias
  8518. //-----------------------------------------------------------------------------
  8519. ImFontConfig::ImFontConfig()
  8520. {
  8521. FontData = NULL;
  8522. FontDataSize = 0;
  8523. FontDataOwnedByAtlas = true;
  8524. FontNo = 0;
  8525. SizePixels = 0.0f;
  8526. OversampleH = 3;
  8527. OversampleV = 1;
  8528. PixelSnapH = false;
  8529. GlyphExtraSpacing = ImVec2(0.0f, 0.0f);
  8530. GlyphRanges = NULL;
  8531. MergeMode = false;
  8532. MergeGlyphCenterV = false;
  8533. DstFont = NULL;
  8534. memset(Name, 0, sizeof(Name));
  8535. }
  8536. ImFontAtlas::ImFontAtlas()
  8537. {
  8538. TexID = NULL;
  8539. TexPixelsAlpha8 = NULL;
  8540. TexPixelsRGBA32 = NULL;
  8541. TexWidth = TexHeight = 0;
  8542. TexUvWhitePixel = ImVec2(0, 0);
  8543. }
  8544. ImFontAtlas::~ImFontAtlas()
  8545. {
  8546. Clear();
  8547. }
  8548. void ImFontAtlas::ClearInputData()
  8549. {
  8550. for (int i = 0; i < ConfigData.Size; i++)
  8551. if (ConfigData[i].FontData && ConfigData[i].FontDataOwnedByAtlas)
  8552. {
  8553. ImGui::MemFree(ConfigData[i].FontData);
  8554. ConfigData[i].FontData = NULL;
  8555. }
  8556. // When clearing this we lose access to the font name and other information used to build the font.
  8557. for (int i = 0; i < Fonts.Size; i++)
  8558. if (Fonts[i]->ConfigData >= ConfigData.Data && Fonts[i]->ConfigData < ConfigData.Data + ConfigData.Size)
  8559. {
  8560. Fonts[i]->ConfigData = NULL;
  8561. Fonts[i]->ConfigDataCount = 0;
  8562. }
  8563. ConfigData.clear();
  8564. }
  8565. void ImFontAtlas::ClearTexData()
  8566. {
  8567. if (TexPixelsAlpha8)
  8568. ImGui::MemFree(TexPixelsAlpha8);
  8569. if (TexPixelsRGBA32)
  8570. ImGui::MemFree(TexPixelsRGBA32);
  8571. TexPixelsAlpha8 = NULL;
  8572. TexPixelsRGBA32 = NULL;
  8573. }
  8574. void ImFontAtlas::ClearFonts()
  8575. {
  8576. for (int i = 0; i < Fonts.Size; i++)
  8577. {
  8578. Fonts[i]->~ImFont();
  8579. ImGui::MemFree(Fonts[i]);
  8580. }
  8581. Fonts.clear();
  8582. }
  8583. void ImFontAtlas::Clear()
  8584. {
  8585. ClearInputData();
  8586. ClearTexData();
  8587. ClearFonts();
  8588. }
  8589. void ImFontAtlas::GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel)
  8590. {
  8591. // Lazily build
  8592. if (TexPixelsAlpha8 == NULL)
  8593. {
  8594. if (ConfigData.empty())
  8595. AddFontDefault();
  8596. Build();
  8597. }
  8598. *out_pixels = TexPixelsAlpha8;
  8599. if (out_width) *out_width = TexWidth;
  8600. if (out_height) *out_height = TexHeight;
  8601. if (out_bytes_per_pixel) *out_bytes_per_pixel = 1;
  8602. }
  8603. void ImFontAtlas::GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel)
  8604. {
  8605. // Lazily convert to RGBA32 format
  8606. // Although it is likely to be the most commonly used format, our font rendering is 8 bpp
  8607. if (!TexPixelsRGBA32)
  8608. {
  8609. unsigned char* pixels;
  8610. GetTexDataAsAlpha8(&pixels, NULL, NULL);
  8611. TexPixelsRGBA32 = (unsigned int*)ImGui::MemAlloc((size_t)(TexWidth * TexHeight * 4));
  8612. const unsigned char* src = pixels;
  8613. unsigned int* dst = TexPixelsRGBA32;
  8614. for (int n = TexWidth * TexHeight; n > 0; n--)
  8615. *dst++ = ((unsigned int)(*src++) << 24) | 0x00FFFFFF;
  8616. }
  8617. *out_pixels = (unsigned char*)TexPixelsRGBA32;
  8618. if (out_width) *out_width = TexWidth;
  8619. if (out_height) *out_height = TexHeight;
  8620. if (out_bytes_per_pixel) *out_bytes_per_pixel = 4;
  8621. }
  8622. ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg)
  8623. {
  8624. IM_ASSERT(font_cfg->FontData != NULL && font_cfg->FontDataSize > 0);
  8625. IM_ASSERT(font_cfg->SizePixels > 0.0f);
  8626. // Create new font
  8627. if (!font_cfg->MergeMode)
  8628. {
  8629. ImFont* font = (ImFont*)ImGui::MemAlloc(sizeof(ImFont));
  8630. new (font) ImFont();
  8631. Fonts.push_back(font);
  8632. }
  8633. ConfigData.push_back(*font_cfg);
  8634. ImFontConfig& new_font_cfg = ConfigData.back();
  8635. new_font_cfg.DstFont = Fonts.back();
  8636. if (!new_font_cfg.FontDataOwnedByAtlas)
  8637. {
  8638. new_font_cfg.FontData = ImGui::MemAlloc(new_font_cfg.FontDataSize);
  8639. new_font_cfg.FontDataOwnedByAtlas = true;
  8640. memcpy(new_font_cfg.FontData, font_cfg->FontData, (size_t)new_font_cfg.FontDataSize);
  8641. }
  8642. // Invalidate texture
  8643. ClearTexData();
  8644. return Fonts.back();
  8645. }
  8646. static unsigned int stb_decompress_length(unsigned char *input);
  8647. static unsigned int stb_decompress(unsigned char *output, unsigned char *i, unsigned int length);
  8648. // Default font ttf is compressed with stb_compress then base85 encoded (see extra_fonts/binary_to_compressed_c.cpp for encoder)
  8649. static const char* GetDefaultCompressedFontDataTTFBase85();
  8650. static unsigned int Decode85Byte(char c) { return c >= '\\' ? c-36 : c-35; }
  8651. static void Decode85(const unsigned char* src, unsigned int* dst) { for (; *src; src += 5) *dst++ = Decode85Byte(src[0]) + 85*(Decode85Byte(src[1]) + 85*(Decode85Byte(src[2]) + 85*(Decode85Byte(src[3]) + 85*Decode85Byte(src[4])))); }
  8652. // Load embedded ProggyClean.ttf at size 13
  8653. ImFont* ImFontAtlas::AddFontDefault(const ImFontConfig* font_cfg_template)
  8654. {
  8655. const char* ttf_compressed_base85 = GetDefaultCompressedFontDataTTFBase85();
  8656. int ttf_compressed_size = (((int)strlen(ttf_compressed_base85) + 4) / 5) * 4;
  8657. void* ttf_compressed = ImGui::MemAlloc(ttf_compressed_size);
  8658. Decode85((const unsigned char*)ttf_compressed_base85, (unsigned int*)ttf_compressed);
  8659. ImFontConfig font_cfg;
  8660. if (font_cfg_template)
  8661. font_cfg = *font_cfg_template;
  8662. else
  8663. {
  8664. font_cfg.OversampleH = font_cfg.OversampleV = 1;
  8665. font_cfg.PixelSnapH = true;
  8666. }
  8667. if (font_cfg.Name[0] == '\0') strcpy(font_cfg.Name, "<default>");
  8668. ImFont* font = AddFontFromMemoryCompressedTTF(ttf_compressed, ttf_compressed_size, 13.0f, &font_cfg, GetGlyphRangesDefault());
  8669. ImGui::MemFree(ttf_compressed);
  8670. return font;
  8671. }
  8672. ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges)
  8673. {
  8674. void* data = NULL;
  8675. int data_size = 0;
  8676. if (!ImLoadFileToMemory(filename, "rb", (void**)&data, &data_size))
  8677. {
  8678. IM_ASSERT(0); // Could not load file.
  8679. return NULL;
  8680. }
  8681. ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();
  8682. if (font_cfg.Name[0] == '\0')
  8683. {
  8684. const char* p;
  8685. for (p = filename + strlen(filename); p > filename && p[-1] != '/' && p[-1] != '\\'; p--) {}
  8686. ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%s", p);
  8687. }
  8688. return AddFontFromMemoryTTF(data, data_size, size_pixels, &font_cfg, glyph_ranges);
  8689. }
  8690. // Transfer ownership of 'ttf_data' to ImFontAtlas, will be deleted after Build()
  8691. ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* ttf_data, int ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges)
  8692. {
  8693. ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();
  8694. IM_ASSERT(font_cfg.FontData == NULL);
  8695. font_cfg.FontData = ttf_data;
  8696. font_cfg.FontDataSize = ttf_size;
  8697. font_cfg.SizePixels = size_pixels;
  8698. if (glyph_ranges)
  8699. font_cfg.GlyphRanges = glyph_ranges;
  8700. return AddFont(&font_cfg);
  8701. }
  8702. ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_data, int compressed_ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges)
  8703. {
  8704. const unsigned int buf_decompressed_size = stb_decompress_length((unsigned char*)compressed_ttf_data);
  8705. unsigned char* buf_decompressed_data = (unsigned char *)ImGui::MemAlloc(buf_decompressed_size);
  8706. stb_decompress(buf_decompressed_data, (unsigned char*)compressed_ttf_data, (unsigned int)compressed_ttf_size);
  8707. ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();
  8708. IM_ASSERT(font_cfg.FontData == NULL);
  8709. font_cfg.FontDataOwnedByAtlas = true;
  8710. return AddFontFromMemoryTTF(buf_decompressed_data, (int)buf_decompressed_size, size_pixels, font_cfg_template, glyph_ranges);
  8711. }
  8712. bool ImFontAtlas::Build()
  8713. {
  8714. IM_ASSERT(ConfigData.Size > 0);
  8715. TexID = NULL;
  8716. TexWidth = TexHeight = 0;
  8717. TexUvWhitePixel = ImVec2(0, 0);
  8718. ClearTexData();
  8719. struct ImFontTempBuildData
  8720. {
  8721. stbtt_fontinfo FontInfo;
  8722. stbrp_rect* Rects;
  8723. stbtt_pack_range* Ranges;
  8724. int RangesCount;
  8725. };
  8726. ImFontTempBuildData* tmp_array = (ImFontTempBuildData*)ImGui::MemAlloc((size_t)ConfigData.Size * sizeof(ImFontTempBuildData));
  8727. // Initialize font information early (so we can error without any cleanup) + count glyphs
  8728. int total_glyph_count = 0;
  8729. int total_glyph_range_count = 0;
  8730. for (int input_i = 0; input_i < ConfigData.Size; input_i++)
  8731. {
  8732. ImFontConfig& cfg = ConfigData[input_i];
  8733. ImFontTempBuildData& tmp = tmp_array[input_i];
  8734. IM_ASSERT(cfg.DstFont && (!cfg.DstFont->IsLoaded() || cfg.DstFont->ContainerAtlas == this));
  8735. const int font_offset = stbtt_GetFontOffsetForIndex((unsigned char*)cfg.FontData, cfg.FontNo);
  8736. IM_ASSERT(font_offset >= 0);
  8737. if (!stbtt_InitFont(&tmp.FontInfo, (unsigned char*)cfg.FontData, font_offset))
  8738. return false;
  8739. if (!cfg.GlyphRanges)
  8740. cfg.GlyphRanges = GetGlyphRangesDefault();
  8741. for (const ImWchar* in_range = cfg.GlyphRanges; in_range[0] && in_range[1]; in_range += 2)
  8742. {
  8743. total_glyph_count += (in_range[1] - in_range[0]) + 1;
  8744. total_glyph_range_count++;
  8745. }
  8746. }
  8747. // Start packing
  8748. TexWidth = (total_glyph_count > 1000) ? 1024 : 512; // Width doesn't actually matters.
  8749. TexHeight = 0;
  8750. const int max_tex_height = 1024*32;
  8751. stbtt_pack_context spc;
  8752. stbtt_PackBegin(&spc, NULL, TexWidth, max_tex_height, 0, 1, NULL);
  8753. // Pack our extra data rectangles first, so it will be on the upper-left corner of our texture (UV will have small values).
  8754. ImVector<stbrp_rect> extra_rects;
  8755. RenderCustomTexData(0, &extra_rects);
  8756. stbtt_PackSetOversampling(&spc, 1, 1);
  8757. stbrp_pack_rects((stbrp_context*)spc.pack_info, &extra_rects[0], extra_rects.Size);
  8758. for (int i = 0; i < extra_rects.Size; i++)
  8759. if (extra_rects[i].was_packed)
  8760. TexHeight = ImMax(TexHeight, extra_rects[i].y + extra_rects[i].h);
  8761. // Allocate packing character data and flag packed characters buffer as non-packed (x0=y0=x1=y1=0)
  8762. int buf_packedchars_n = 0, buf_rects_n = 0, buf_ranges_n = 0;
  8763. stbtt_packedchar* buf_packedchars = (stbtt_packedchar*)ImGui::MemAlloc(total_glyph_count * sizeof(stbtt_packedchar));
  8764. stbrp_rect* buf_rects = (stbrp_rect*)ImGui::MemAlloc(total_glyph_count * sizeof(stbrp_rect));
  8765. stbtt_pack_range* buf_ranges = (stbtt_pack_range*)ImGui::MemAlloc(total_glyph_range_count * sizeof(stbtt_pack_range));
  8766. memset(buf_packedchars, 0, total_glyph_count * sizeof(stbtt_packedchar));
  8767. memset(buf_rects, 0, total_glyph_count * sizeof(stbrp_rect)); // Unnecessary but let's clear this for the sake of sanity.
  8768. memset(buf_ranges, 0, total_glyph_range_count * sizeof(stbtt_pack_range));
  8769. // First font pass: pack all glyphs (no rendering at this point, we are working with glyph sizes only)
  8770. for (int input_i = 0; input_i < ConfigData.Size; input_i++)
  8771. {
  8772. ImFontConfig& cfg = ConfigData[input_i];
  8773. ImFontTempBuildData& tmp = tmp_array[input_i];
  8774. // Setup ranges
  8775. int glyph_count = 0;
  8776. int glyph_ranges_count = 0;
  8777. for (const ImWchar* in_range = cfg.GlyphRanges; in_range[0] && in_range[1]; in_range += 2)
  8778. {
  8779. glyph_count += (in_range[1] - in_range[0]) + 1;
  8780. glyph_ranges_count++;
  8781. }
  8782. tmp.Ranges = buf_ranges + buf_ranges_n;
  8783. tmp.RangesCount = glyph_ranges_count;
  8784. buf_ranges_n += glyph_ranges_count;
  8785. for (int i = 0; i < glyph_ranges_count; i++)
  8786. {
  8787. const ImWchar* in_range = &cfg.GlyphRanges[i * 2];
  8788. stbtt_pack_range& range = tmp.Ranges[i];
  8789. range.font_size = cfg.SizePixels;
  8790. range.first_unicode_codepoint_in_range = in_range[0];
  8791. range.num_chars = (in_range[1] - in_range[0]) + 1;
  8792. range.chardata_for_range = buf_packedchars + buf_packedchars_n;
  8793. buf_packedchars_n += range.num_chars;
  8794. }
  8795. // Pack
  8796. tmp.Rects = buf_rects + buf_rects_n;
  8797. buf_rects_n += glyph_count;
  8798. stbtt_PackSetOversampling(&spc, cfg.OversampleH, cfg.OversampleV);
  8799. int n = stbtt_PackFontRangesGatherRects(&spc, &tmp.FontInfo, tmp.Ranges, tmp.RangesCount, tmp.Rects);
  8800. stbrp_pack_rects((stbrp_context*)spc.pack_info, tmp.Rects, n);
  8801. // Extend texture height
  8802. for (int i = 0; i < n; i++)
  8803. if (tmp.Rects[i].was_packed)
  8804. TexHeight = ImMax(TexHeight, tmp.Rects[i].y + tmp.Rects[i].h);
  8805. }
  8806. IM_ASSERT(buf_rects_n == total_glyph_count);
  8807. IM_ASSERT(buf_packedchars_n == total_glyph_count);
  8808. IM_ASSERT(buf_ranges_n == total_glyph_range_count);
  8809. // Create texture
  8810. TexHeight = ImUpperPowerOfTwo(TexHeight);
  8811. TexPixelsAlpha8 = (unsigned char*)ImGui::MemAlloc(TexWidth * TexHeight);
  8812. memset(TexPixelsAlpha8, 0, TexWidth * TexHeight);
  8813. spc.pixels = TexPixelsAlpha8;
  8814. spc.height = TexHeight;
  8815. // Second pass: render characters
  8816. for (int input_i = 0; input_i < ConfigData.Size; input_i++)
  8817. {
  8818. ImFontConfig& cfg = ConfigData[input_i];
  8819. ImFontTempBuildData& tmp = tmp_array[input_i];
  8820. stbtt_PackSetOversampling(&spc, cfg.OversampleH, cfg.OversampleV);
  8821. stbtt_PackFontRangesRenderIntoRects(&spc, &tmp.FontInfo, tmp.Ranges, tmp.RangesCount, tmp.Rects);
  8822. tmp.Rects = NULL;
  8823. }
  8824. // End packing
  8825. stbtt_PackEnd(&spc);
  8826. ImGui::MemFree(buf_rects);
  8827. buf_rects = NULL;
  8828. // Third pass: setup ImFont and glyphs for runtime
  8829. for (int input_i = 0; input_i < ConfigData.Size; input_i++)
  8830. {
  8831. ImFontConfig& cfg = ConfigData[input_i];
  8832. ImFontTempBuildData& tmp = tmp_array[input_i];
  8833. ImFont* dst_font = cfg.DstFont;
  8834. float font_scale = stbtt_ScaleForPixelHeight(&tmp.FontInfo, cfg.SizePixels);
  8835. int unscaled_ascent, unscaled_descent, unscaled_line_gap;
  8836. stbtt_GetFontVMetrics(&tmp.FontInfo, &unscaled_ascent, &unscaled_descent, &unscaled_line_gap);
  8837. float ascent = unscaled_ascent * font_scale;
  8838. float descent = unscaled_descent * font_scale;
  8839. if (!cfg.MergeMode)
  8840. {
  8841. dst_font->ContainerAtlas = this;
  8842. dst_font->ConfigData = &cfg;
  8843. dst_font->ConfigDataCount = 0;
  8844. dst_font->FontSize = cfg.SizePixels;
  8845. dst_font->Ascent = ascent;
  8846. dst_font->Descent = descent;
  8847. dst_font->Glyphs.resize(0);
  8848. }
  8849. dst_font->ConfigDataCount++;
  8850. float off_y = (cfg.MergeMode && cfg.MergeGlyphCenterV) ? (ascent - dst_font->Ascent) * 0.5f : 0.0f;
  8851. dst_font->FallbackGlyph = NULL; // Always clear fallback so FindGlyph can return NULL. It will be set again in BuildLookupTable()
  8852. for (int i = 0; i < tmp.RangesCount; i++)
  8853. {
  8854. stbtt_pack_range& range = tmp.Ranges[i];
  8855. for (int char_idx = 0; char_idx < range.num_chars; char_idx += 1)
  8856. {
  8857. const stbtt_packedchar& pc = range.chardata_for_range[char_idx];
  8858. if (!pc.x0 && !pc.x1 && !pc.y0 && !pc.y1)
  8859. continue;
  8860. const int codepoint = range.first_unicode_codepoint_in_range + char_idx;
  8861. if (cfg.MergeMode && dst_font->FindGlyph((unsigned short)codepoint))
  8862. continue;
  8863. stbtt_aligned_quad q;
  8864. float dummy_x = 0.0f, dummy_y = 0.0f;
  8865. stbtt_GetPackedQuad(range.chardata_for_range, TexWidth, TexHeight, char_idx, &dummy_x, &dummy_y, &q, 0);
  8866. dst_font->Glyphs.resize(dst_font->Glyphs.Size + 1);
  8867. ImFont::Glyph& glyph = dst_font->Glyphs.back();
  8868. glyph.Codepoint = (ImWchar)codepoint;
  8869. glyph.X0 = q.x0; glyph.Y0 = q.y0; glyph.X1 = q.x1; glyph.Y1 = q.y1;
  8870. glyph.U0 = q.s0; glyph.V0 = q.t0; glyph.U1 = q.s1; glyph.V1 = q.t1;
  8871. glyph.Y0 += (float)(int)(dst_font->Ascent + off_y + 0.5f);
  8872. glyph.Y1 += (float)(int)(dst_font->Ascent + off_y + 0.5f);
  8873. glyph.XAdvance = (pc.xadvance + cfg.GlyphExtraSpacing.x); // Bake spacing into XAdvance
  8874. if (cfg.PixelSnapH)
  8875. glyph.XAdvance = (float)(int)(glyph.XAdvance + 0.5f);
  8876. }
  8877. }
  8878. cfg.DstFont->BuildLookupTable();
  8879. }
  8880. // Cleanup temporaries
  8881. ImGui::MemFree(buf_packedchars);
  8882. ImGui::MemFree(buf_ranges);
  8883. ImGui::MemFree(tmp_array);
  8884. // Render into our custom data block
  8885. RenderCustomTexData(1, &extra_rects);
  8886. return true;
  8887. }
  8888. void ImFontAtlas::RenderCustomTexData(int pass, void* p_rects)
  8889. {
  8890. // . = white layer, X = black layer, others are blank
  8891. const int TEX_DATA_W = 90;
  8892. const int TEX_DATA_H = 27;
  8893. const char texture_data[TEX_DATA_W*TEX_DATA_H+1] =
  8894. {
  8895. "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX"
  8896. "..- -X.....X- X.X - X.X -X.....X - X.....X"
  8897. "--- -XXX.XXX- X...X - X...X -X....X - X....X"
  8898. "X - X.X - X.....X - X.....X -X...X - X...X"
  8899. "XX - X.X -X.......X- X.......X -X..X.X - X.X..X"
  8900. "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X"
  8901. "X..X - X.X - X.X - X.X -XX X.X - X.X XX"
  8902. "X...X - X.X - X.X - XX X.X XX - X.X - X.X "
  8903. "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X "
  8904. "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X "
  8905. "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X "
  8906. "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X "
  8907. "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X "
  8908. "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X "
  8909. "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X "
  8910. "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X "
  8911. "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX "
  8912. "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------"
  8913. "X.X X..X - -X.......X- X.......X - XX XX - "
  8914. "XX X..X - - X.....X - X.....X - X.X X.X - "
  8915. " X..X - X...X - X...X - X..X X..X - "
  8916. " XX - X.X - X.X - X...XXXXXXXXXXXXX...X - "
  8917. "------------ - X - X -X.....................X- "
  8918. " ----------------------------------- X...XXXXXXXXXXXXX...X - "
  8919. " - X..X X..X - "
  8920. " - X.X X.X - "
  8921. " - XX XX - "
  8922. };
  8923. ImVector<stbrp_rect>& rects = *(ImVector<stbrp_rect>*)p_rects;
  8924. if (pass == 0)
  8925. {
  8926. stbrp_rect r;
  8927. memset(&r, 0, sizeof(r));
  8928. r.w = (TEX_DATA_W*2)+1;
  8929. r.h = TEX_DATA_H+1;
  8930. rects.push_back(r);
  8931. }
  8932. else if (pass == 1)
  8933. {
  8934. // Copy pixels
  8935. const stbrp_rect& r = rects[0];
  8936. for (int y = 0, n = 0; y < TEX_DATA_H; y++)
  8937. for (int x = 0; x < TEX_DATA_W; x++, n++)
  8938. {
  8939. const int offset0 = (int)(r.x + x) + (int)(r.y + y) * TexWidth;
  8940. const int offset1 = offset0 + 1 + TEX_DATA_W;
  8941. TexPixelsAlpha8[offset0] = texture_data[n] == '.' ? 0xFF : 0x00;
  8942. TexPixelsAlpha8[offset1] = texture_data[n] == 'X' ? 0xFF : 0x00;
  8943. }
  8944. const ImVec2 tex_uv_scale(1.0f / TexWidth, 1.0f / TexHeight);
  8945. TexUvWhitePixel = ImVec2(r.x + 0.5f, r.y + 0.5f) * tex_uv_scale;
  8946. const ImVec2 cursor_datas[ImGuiMouseCursor_Count_][3] =
  8947. {
  8948. // Pos ........ Size ......... Offset ......
  8949. { ImVec2(0,3), ImVec2(12,19), ImVec2( 0, 0) }, // ImGuiMouseCursor_Arrow
  8950. { ImVec2(13,0), ImVec2(7,16), ImVec2( 4, 8) }, // ImGuiMouseCursor_TextInput
  8951. { ImVec2(31,0), ImVec2(23,23), ImVec2(11,11) }, // ImGuiMouseCursor_Move
  8952. { ImVec2(21,0), ImVec2( 9,23), ImVec2( 5,11) }, // ImGuiMouseCursor_ResizeNS
  8953. { ImVec2(55,18),ImVec2(23, 9), ImVec2(11, 5) }, // ImGuiMouseCursor_ResizeEW
  8954. { ImVec2(73,0), ImVec2(17,17), ImVec2( 9, 9) }, // ImGuiMouseCursor_ResizeNESW
  8955. { ImVec2(55,0), ImVec2(17,17), ImVec2( 9, 9) }, // ImGuiMouseCursor_ResizeNWSE
  8956. };
  8957. for (int type = 0; type < ImGuiMouseCursor_Count_; type++)
  8958. {
  8959. ImGuiMouseCursorData& cursor_data = GImGui->MouseCursorData[type];
  8960. ImVec2 pos = cursor_datas[type][0] + ImVec2((float)r.x, (float)r.y);
  8961. const ImVec2 size = cursor_datas[type][1];
  8962. cursor_data.Type = type;
  8963. cursor_data.Size = size;
  8964. cursor_data.Offset = cursor_datas[type][2];
  8965. cursor_data.TexUvMin[0] = (pos) * tex_uv_scale;
  8966. cursor_data.TexUvMax[0] = (pos + size) * tex_uv_scale;
  8967. pos.x += TEX_DATA_W+1;
  8968. cursor_data.TexUvMin[1] = (pos) * tex_uv_scale;
  8969. cursor_data.TexUvMax[1] = (pos + size) * tex_uv_scale;
  8970. }
  8971. }
  8972. }
  8973. // Retrieve list of range (2 int per range, values are inclusive)
  8974. const ImWchar* ImFontAtlas::GetGlyphRangesDefault()
  8975. {
  8976. static const ImWchar ranges[] =
  8977. {
  8978. 0x0020, 0x00FF, // Basic Latin + Latin Supplement
  8979. 0,
  8980. };
  8981. return &ranges[0];
  8982. }
  8983. const ImWchar* ImFontAtlas::GetGlyphRangesChinese()
  8984. {
  8985. static const ImWchar ranges[] =
  8986. {
  8987. 0x0020, 0x00FF, // Basic Latin + Latin Supplement
  8988. 0x3000, 0x30FF, // Punctuations, Hiragana, Katakana
  8989. 0x31F0, 0x31FF, // Katakana Phonetic Extensions
  8990. 0xFF00, 0xFFEF, // Half-width characters
  8991. 0x4e00, 0x9FAF, // CJK Ideograms
  8992. 0,
  8993. };
  8994. return &ranges[0];
  8995. }
  8996. const ImWchar* ImFontAtlas::GetGlyphRangesJapanese()
  8997. {
  8998. // Store the 1946 ideograms code points as successive offsets from the initial unicode codepoint 0x4E00. Each offset has an implicit +1.
  8999. // This encoding helps us reduce the source code size.
  9000. static const short offsets_from_0x4E00[] =
  9001. {
  9002. -1,0,1,3,0,0,0,0,1,0,5,1,1,0,7,4,6,10,0,1,9,9,7,1,3,19,1,10,7,1,0,1,0,5,1,0,6,4,2,6,0,0,12,6,8,0,3,5,0,1,0,9,0,0,8,1,1,3,4,5,13,0,0,8,2,17,
  9003. 4,3,1,1,9,6,0,0,0,2,1,3,2,22,1,9,11,1,13,1,3,12,0,5,9,2,0,6,12,5,3,12,4,1,2,16,1,1,4,6,5,3,0,6,13,15,5,12,8,14,0,0,6,15,3,6,0,18,8,1,6,14,1,
  9004. 5,4,12,24,3,13,12,10,24,0,0,0,1,0,1,1,2,9,10,2,2,0,0,3,3,1,0,3,8,0,3,2,4,4,1,6,11,10,14,6,15,3,4,15,1,0,0,5,2,2,0,0,1,6,5,5,6,0,3,6,5,0,0,1,0,
  9005. 11,2,2,8,4,7,0,10,0,1,2,17,19,3,0,2,5,0,6,2,4,4,6,1,1,11,2,0,3,1,2,1,2,10,7,6,3,16,0,8,24,0,0,3,1,1,3,0,1,6,0,0,0,2,0,1,5,15,0,1,0,0,2,11,19,
  9006. 1,4,19,7,6,5,1,0,0,0,0,5,1,0,1,9,0,0,5,0,2,0,1,0,3,0,11,3,0,2,0,0,0,0,0,9,3,6,4,12,0,14,0,0,29,10,8,0,14,37,13,0,31,16,19,0,8,30,1,20,8,3,48,
  9007. 21,1,0,12,0,10,44,34,42,54,11,18,82,0,2,1,2,12,1,0,6,2,17,2,12,7,0,7,17,4,2,6,24,23,8,23,39,2,16,23,1,0,5,1,2,15,14,5,6,2,11,0,8,6,2,2,2,14,
  9008. 20,4,15,3,4,11,10,10,2,5,2,1,30,2,1,0,0,22,5,5,0,3,1,5,4,1,0,0,2,2,21,1,5,1,2,16,2,1,3,4,0,8,4,0,0,5,14,11,2,16,1,13,1,7,0,22,15,3,1,22,7,14,
  9009. 22,19,11,24,18,46,10,20,64,45,3,2,0,4,5,0,1,4,25,1,0,0,2,10,0,0,0,1,0,1,2,0,0,9,1,2,0,0,0,2,5,2,1,1,5,5,8,1,1,1,5,1,4,9,1,3,0,1,0,1,1,2,0,0,
  9010. 2,0,1,8,22,8,1,0,0,0,0,4,2,1,0,9,8,5,0,9,1,30,24,2,6,4,39,0,14,5,16,6,26,179,0,2,1,1,0,0,0,5,2,9,6,0,2,5,16,7,5,1,1,0,2,4,4,7,15,13,14,0,0,
  9011. 3,0,1,0,0,0,2,1,6,4,5,1,4,9,0,3,1,8,0,0,10,5,0,43,0,2,6,8,4,0,2,0,0,9,6,0,9,3,1,6,20,14,6,1,4,0,7,2,3,0,2,0,5,0,3,1,0,3,9,7,0,3,4,0,4,9,1,6,0,
  9012. 9,0,0,2,3,10,9,28,3,6,2,4,1,2,32,4,1,18,2,0,3,1,5,30,10,0,2,2,2,0,7,9,8,11,10,11,7,2,13,7,5,10,0,3,40,2,0,1,6,12,0,4,5,1,5,11,11,21,4,8,3,7,
  9013. 8,8,33,5,23,0,0,19,8,8,2,3,0,6,1,1,1,5,1,27,4,2,5,0,3,5,6,3,1,0,3,1,12,5,3,3,2,0,7,7,2,1,0,4,0,1,1,2,0,10,10,6,2,5,9,7,5,15,15,21,6,11,5,20,
  9014. 4,3,5,5,2,5,0,2,1,0,1,7,28,0,9,0,5,12,5,5,18,30,0,12,3,3,21,16,25,32,9,3,14,11,24,5,66,9,1,2,0,5,9,1,5,1,8,0,8,3,3,0,1,15,1,4,8,1,2,7,0,7,2,
  9015. 8,3,7,5,3,7,10,2,1,0,0,2,25,0,6,4,0,10,0,4,2,4,1,12,5,38,4,0,4,1,10,5,9,4,0,14,4,2,5,18,20,21,1,3,0,5,0,7,0,3,7,1,3,1,1,8,1,0,0,0,3,2,5,2,11,
  9016. 6,0,13,1,3,9,1,12,0,16,6,2,1,0,2,1,12,6,13,11,2,0,28,1,7,8,14,13,8,13,0,2,0,5,4,8,10,2,37,42,19,6,6,7,4,14,11,18,14,80,7,6,0,4,72,12,36,27,
  9017. 7,7,0,14,17,19,164,27,0,5,10,7,3,13,6,14,0,2,2,5,3,0,6,13,0,0,10,29,0,4,0,3,13,0,3,1,6,51,1,5,28,2,0,8,0,20,2,4,0,25,2,10,13,10,0,16,4,0,1,0,
  9018. 2,1,7,0,1,8,11,0,0,1,2,7,2,23,11,6,6,4,16,2,2,2,0,22,9,3,3,5,2,0,15,16,21,2,9,20,15,15,5,3,9,1,0,0,1,7,7,5,4,2,2,2,38,24,14,0,0,15,5,6,24,14,
  9019. 5,5,11,0,21,12,0,3,8,4,11,1,8,0,11,27,7,2,4,9,21,59,0,1,39,3,60,62,3,0,12,11,0,3,30,11,0,13,88,4,15,5,28,13,1,4,48,17,17,4,28,32,46,0,16,0,
  9020. 18,11,1,8,6,38,11,2,6,11,38,2,0,45,3,11,2,7,8,4,30,14,17,2,1,1,65,18,12,16,4,2,45,123,12,56,33,1,4,3,4,7,0,0,0,3,2,0,16,4,2,4,2,0,7,4,5,2,26,
  9021. 2,25,6,11,6,1,16,2,6,17,77,15,3,35,0,1,0,5,1,0,38,16,6,3,12,3,3,3,0,9,3,1,3,5,2,9,0,18,0,25,1,3,32,1,72,46,6,2,7,1,3,14,17,0,28,1,40,13,0,20,
  9022. 15,40,6,38,24,12,43,1,1,9,0,12,6,0,6,2,4,19,3,7,1,48,0,9,5,0,5,6,9,6,10,15,2,11,19,3,9,2,0,1,10,1,27,8,1,3,6,1,14,0,26,0,27,16,3,4,9,6,2,23,
  9023. 9,10,5,25,2,1,6,1,1,48,15,9,15,14,3,4,26,60,29,13,37,21,1,6,4,0,2,11,22,23,16,16,2,2,1,3,0,5,1,6,4,0,0,4,0,0,8,3,0,2,5,0,7,1,7,3,13,2,4,10,
  9024. 3,0,2,31,0,18,3,0,12,10,4,1,0,7,5,7,0,5,4,12,2,22,10,4,2,15,2,8,9,0,23,2,197,51,3,1,1,4,13,4,3,21,4,19,3,10,5,40,0,4,1,1,10,4,1,27,34,7,21,
  9025. 2,17,2,9,6,4,2,3,0,4,2,7,8,2,5,1,15,21,3,4,4,2,2,17,22,1,5,22,4,26,7,0,32,1,11,42,15,4,1,2,5,0,19,3,1,8,6,0,10,1,9,2,13,30,8,2,24,17,19,1,4,
  9026. 4,25,13,0,10,16,11,39,18,8,5,30,82,1,6,8,18,77,11,13,20,75,11,112,78,33,3,0,0,60,17,84,9,1,1,12,30,10,49,5,32,158,178,5,5,6,3,3,1,3,1,4,7,6,
  9027. 19,31,21,0,2,9,5,6,27,4,9,8,1,76,18,12,1,4,0,3,3,6,3,12,2,8,30,16,2,25,1,5,5,4,3,0,6,10,2,3,1,0,5,1,19,3,0,8,1,5,2,6,0,0,0,19,1,2,0,5,1,2,5,
  9028. 1,3,7,0,4,12,7,3,10,22,0,9,5,1,0,2,20,1,1,3,23,30,3,9,9,1,4,191,14,3,15,6,8,50,0,1,0,0,4,0,0,1,0,2,4,2,0,2,3,0,2,0,2,2,8,7,0,1,1,1,3,3,17,11,
  9029. 91,1,9,3,2,13,4,24,15,41,3,13,3,1,20,4,125,29,30,1,0,4,12,2,21,4,5,5,19,11,0,13,11,86,2,18,0,7,1,8,8,2,2,22,1,2,6,5,2,0,1,2,8,0,2,0,5,2,1,0,
  9030. 2,10,2,0,5,9,2,1,2,0,1,0,4,0,0,10,2,5,3,0,6,1,0,1,4,4,33,3,13,17,3,18,6,4,7,1,5,78,0,4,1,13,7,1,8,1,0,35,27,15,3,0,0,0,1,11,5,41,38,15,22,6,
  9031. 14,14,2,1,11,6,20,63,5,8,27,7,11,2,2,40,58,23,50,54,56,293,8,8,1,5,1,14,0,1,12,37,89,8,8,8,2,10,6,0,0,0,4,5,2,1,0,1,1,2,7,0,3,3,0,4,6,0,3,2,
  9032. 19,3,8,0,0,0,4,4,16,0,4,1,5,1,3,0,3,4,6,2,17,10,10,31,6,4,3,6,10,126,7,3,2,2,0,9,0,0,5,20,13,0,15,0,6,0,2,5,8,64,50,3,2,12,2,9,0,0,11,8,20,
  9033. 109,2,18,23,0,0,9,61,3,0,28,41,77,27,19,17,81,5,2,14,5,83,57,252,14,154,263,14,20,8,13,6,57,39,38,
  9034. };
  9035. static int ranges_unpacked = false;
  9036. static ImWchar ranges[8 + IM_ARRAYSIZE(offsets_from_0x4E00)*2 + 1] =
  9037. {
  9038. 0x0020, 0x00FF, // Basic Latin + Latin Supplement
  9039. 0x3000, 0x30FF, // Punctuations, Hiragana, Katakana
  9040. 0x31F0, 0x31FF, // Katakana Phonetic Extensions
  9041. 0xFF00, 0xFFEF, // Half-width characters
  9042. };
  9043. if (!ranges_unpacked)
  9044. {
  9045. // Unpack
  9046. int codepoint = 0x4e00;
  9047. ImWchar* dst = &ranges[8];
  9048. for (int n = 0; n < IM_ARRAYSIZE(offsets_from_0x4E00); n++, dst += 2)
  9049. dst[0] = dst[1] = (ImWchar)(codepoint += (offsets_from_0x4E00[n] + 1));
  9050. dst[0] = 0;
  9051. ranges_unpacked = true;
  9052. }
  9053. return &ranges[0];
  9054. }
  9055. const ImWchar* ImFontAtlas::GetGlyphRangesCyrillic()
  9056. {
  9057. static const ImWchar ranges[] =
  9058. {
  9059. 0x0020, 0x00FF, // Basic Latin + Latin Supplement
  9060. 0x0400, 0x052F, // Cyrillic + Cyrillic Supplement
  9061. 0x2DE0, 0x2DFF, // Cyrillic Extended-A
  9062. 0xA640, 0xA69F, // Cyrillic Extended-B
  9063. 0,
  9064. };
  9065. return &ranges[0];
  9066. }
  9067. //-----------------------------------------------------------------------------
  9068. // ImFont
  9069. //-----------------------------------------------------------------------------
  9070. ImFont::ImFont()
  9071. {
  9072. Scale = 1.0f;
  9073. FallbackChar = (ImWchar)'?';
  9074. Clear();
  9075. }
  9076. ImFont::~ImFont()
  9077. {
  9078. // Invalidate active font so that the user gets a clear crash instead of a dangling pointer.
  9079. // If you want to delete fonts you need to do it between Render() and NewFrame().
  9080. ImGuiState& g = *GImGui;
  9081. if (g.Font == this)
  9082. g.Font = NULL;
  9083. Clear();
  9084. }
  9085. void ImFont::Clear()
  9086. {
  9087. FontSize = 0.0f;
  9088. DisplayOffset = ImVec2(0.0f, 1.0f);
  9089. ConfigData = NULL;
  9090. ConfigDataCount = 0;
  9091. Ascent = Descent = 0.0f;
  9092. ContainerAtlas = NULL;
  9093. Glyphs.clear();
  9094. FallbackGlyph = NULL;
  9095. FallbackXAdvance = 0.0f;
  9096. IndexXAdvance.clear();
  9097. IndexLookup.clear();
  9098. }
  9099. void ImFont::BuildLookupTable()
  9100. {
  9101. int max_codepoint = 0;
  9102. for (int i = 0; i != Glyphs.Size; i++)
  9103. max_codepoint = ImMax(max_codepoint, (int)Glyphs[i].Codepoint);
  9104. IndexXAdvance.clear();
  9105. IndexXAdvance.resize(max_codepoint + 1);
  9106. IndexLookup.clear();
  9107. IndexLookup.resize(max_codepoint + 1);
  9108. for (int i = 0; i < max_codepoint + 1; i++)
  9109. {
  9110. IndexXAdvance[i] = -1.0f;
  9111. IndexLookup[i] = -1;
  9112. }
  9113. for (int i = 0; i < Glyphs.Size; i++)
  9114. {
  9115. int codepoint = (int)Glyphs[i].Codepoint;
  9116. IndexXAdvance[codepoint] = Glyphs[i].XAdvance;
  9117. IndexLookup[codepoint] = i;
  9118. }
  9119. // Create a glyph to handle TAB
  9120. // FIXME: Needs proper TAB handling but it needs to be contextualized (can arbitrary say that each string starts at "column 0"
  9121. if (FindGlyph((unsigned short)' '))
  9122. {
  9123. if (Glyphs.back().Codepoint != '\t') // So we can call this function multiple times
  9124. Glyphs.resize(Glyphs.Size + 1);
  9125. ImFont::Glyph& tab_glyph = Glyphs.back();
  9126. tab_glyph = *FindGlyph((unsigned short)' ');
  9127. tab_glyph.Codepoint = '\t';
  9128. tab_glyph.XAdvance *= 4;
  9129. IndexXAdvance[(int)tab_glyph.Codepoint] = (float)tab_glyph.XAdvance;
  9130. IndexLookup[(int)tab_glyph.Codepoint] = (int)(Glyphs.Size-1);
  9131. }
  9132. FallbackGlyph = NULL;
  9133. FallbackGlyph = FindGlyph(FallbackChar);
  9134. FallbackXAdvance = FallbackGlyph ? FallbackGlyph->XAdvance : 0.0f;
  9135. for (int i = 0; i < max_codepoint + 1; i++)
  9136. if (IndexXAdvance[i] < 0.0f)
  9137. IndexXAdvance[i] = FallbackXAdvance;
  9138. }
  9139. void ImFont::SetFallbackChar(ImWchar c)
  9140. {
  9141. FallbackChar = c;
  9142. BuildLookupTable();
  9143. }
  9144. const ImFont::Glyph* ImFont::FindGlyph(unsigned short c) const
  9145. {
  9146. if (c < IndexLookup.Size)
  9147. {
  9148. const int i = IndexLookup[c];
  9149. if (i != -1)
  9150. return &Glyphs[i];
  9151. }
  9152. return FallbackGlyph;
  9153. }
  9154. // Convert UTF-8 to 32-bits character, process single character input.
  9155. // Based on stb_from_utf8() from github.com/nothings/stb/
  9156. // We handle UTF-8 decoding error by skipping forward.
  9157. static int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end)
  9158. {
  9159. unsigned int c = (unsigned int)-1;
  9160. const unsigned char* str = (const unsigned char*)in_text;
  9161. if (!(*str & 0x80))
  9162. {
  9163. c = (unsigned int)(*str++);
  9164. *out_char = c;
  9165. return 1;
  9166. }
  9167. if ((*str & 0xe0) == 0xc0)
  9168. {
  9169. *out_char = 0;
  9170. if (in_text_end && in_text_end - (const char*)str < 2) return 0;
  9171. if (*str < 0xc2) return 0;
  9172. c = (unsigned int)((*str++ & 0x1f) << 6);
  9173. if ((*str & 0xc0) != 0x80) return 0;
  9174. c += (*str++ & 0x3f);
  9175. *out_char = c;
  9176. return 2;
  9177. }
  9178. if ((*str & 0xf0) == 0xe0)
  9179. {
  9180. *out_char = 0;
  9181. if (in_text_end && in_text_end - (const char*)str < 3) return 0;
  9182. if (*str == 0xe0 && (str[1] < 0xa0 || str[1] > 0xbf)) return 0;
  9183. if (*str == 0xed && str[1] > 0x9f) return 0; // str[1] < 0x80 is checked below
  9184. c = (unsigned int)((*str++ & 0x0f) << 12);
  9185. if ((*str & 0xc0) != 0x80) return 0;
  9186. c += (unsigned int)((*str++ & 0x3f) << 6);
  9187. if ((*str & 0xc0) != 0x80) return 0;
  9188. c += (*str++ & 0x3f);
  9189. *out_char = c;
  9190. return 3;
  9191. }
  9192. if ((*str & 0xf8) == 0xf0)
  9193. {
  9194. *out_char = 0;
  9195. if (in_text_end && in_text_end - (const char*)str < 4) return 0;
  9196. if (*str > 0xf4) return 0;
  9197. if (*str == 0xf0 && (str[1] < 0x90 || str[1] > 0xbf)) return 0;
  9198. if (*str == 0xf4 && str[1] > 0x8f) return 0; // str[1] < 0x80 is checked below
  9199. c = (unsigned int)((*str++ & 0x07) << 18);
  9200. if ((*str & 0xc0) != 0x80) return 0;
  9201. c += (unsigned int)((*str++ & 0x3f) << 12);
  9202. if ((*str & 0xc0) != 0x80) return 0;
  9203. c += (unsigned int)((*str++ & 0x3f) << 6);
  9204. if ((*str & 0xc0) != 0x80) return 0;
  9205. c += (*str++ & 0x3f);
  9206. // utf-8 encodings of values used in surrogate pairs are invalid
  9207. if ((c & 0xFFFFF800) == 0xD800) return 0;
  9208. *out_char = c;
  9209. return 4;
  9210. }
  9211. *out_char = 0;
  9212. return 0;
  9213. }
  9214. static int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_text_remaining)
  9215. {
  9216. ImWchar* buf_out = buf;
  9217. ImWchar* buf_end = buf + buf_size;
  9218. while (buf_out < buf_end-1 && (!in_text_end || in_text < in_text_end) && *in_text)
  9219. {
  9220. unsigned int c;
  9221. in_text += ImTextCharFromUtf8(&c, in_text, in_text_end);
  9222. if (c == 0)
  9223. break;
  9224. if (c < 0x10000) // FIXME: Losing characters that don't fit in 2 bytes
  9225. *buf_out++ = (ImWchar)c;
  9226. }
  9227. *buf_out = 0;
  9228. if (in_text_remaining)
  9229. *in_text_remaining = in_text;
  9230. return (int)(buf_out - buf);
  9231. }
  9232. static int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end)
  9233. {
  9234. int char_count = 0;
  9235. while ((!in_text_end || in_text < in_text_end) && *in_text)
  9236. {
  9237. unsigned int c;
  9238. in_text += ImTextCharFromUtf8(&c, in_text, in_text_end);
  9239. if (c == 0)
  9240. break;
  9241. if (c < 0x10000)
  9242. char_count++;
  9243. }
  9244. return char_count;
  9245. }
  9246. // Based on stb_to_utf8() from github.com/nothings/stb/
  9247. static inline int ImTextCharToUtf8(char* buf, int buf_size, unsigned int c)
  9248. {
  9249. if (c < 0x80)
  9250. {
  9251. buf[0] = (char)c;
  9252. return 1;
  9253. }
  9254. if (c < 0x800)
  9255. {
  9256. if (buf_size < 2) return 0;
  9257. buf[0] = (char)(0xc0 + (c >> 6));
  9258. buf[1] = (char)(0x80 + (c & 0x3f));
  9259. return 2;
  9260. }
  9261. if (c >= 0xdc00 && c < 0xe000)
  9262. {
  9263. return 0;
  9264. }
  9265. if (c >= 0xd800 && c < 0xdc00)
  9266. {
  9267. if (buf_size < 4) return 0;
  9268. buf[0] = (char)(0xf0 + (c >> 18));
  9269. buf[1] = (char)(0x80 + ((c >> 12) & 0x3f));
  9270. buf[2] = (char)(0x80 + ((c >> 6) & 0x3f));
  9271. buf[3] = (char)(0x80 + ((c ) & 0x3f));
  9272. return 4;
  9273. }
  9274. //else if (c < 0x10000)
  9275. {
  9276. if (buf_size < 3) return 0;
  9277. buf[0] = (char)(0xe0 + (c >> 12));
  9278. buf[1] = (char)(0x80 + ((c>> 6) & 0x3f));
  9279. buf[2] = (char)(0x80 + ((c ) & 0x3f));
  9280. return 3;
  9281. }
  9282. }
  9283. static inline int ImTextCountUtf8BytesFromChar(unsigned int c)
  9284. {
  9285. if (c < 0x80) return 1;
  9286. if (c < 0x800) return 2;
  9287. if (c >= 0xdc00 && c < 0xe000) return 0;
  9288. if (c >= 0xd800 && c < 0xdc00) return 4;
  9289. return 3;
  9290. }
  9291. static int ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWchar* in_text_end)
  9292. {
  9293. char* buf_out = buf;
  9294. const char* buf_end = buf + buf_size;
  9295. while (buf_out < buf_end-1 && (!in_text_end || in_text < in_text_end) && *in_text)
  9296. {
  9297. unsigned int c = (unsigned int)(*in_text++);
  9298. if (c < 0x80)
  9299. *buf_out++ = (char)c;
  9300. else
  9301. buf_out += ImTextCharToUtf8(buf_out, (int)(buf_end-buf_out-1), c);
  9302. }
  9303. *buf_out = 0;
  9304. return (int)(buf_out - buf);
  9305. }
  9306. static int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end)
  9307. {
  9308. int bytes_count = 0;
  9309. while ((!in_text_end || in_text < in_text_end) && *in_text)
  9310. {
  9311. unsigned int c = (unsigned int)(*in_text++);
  9312. if (c < 0x80)
  9313. bytes_count++;
  9314. else
  9315. bytes_count += ImTextCountUtf8BytesFromChar(c);
  9316. }
  9317. return bytes_count;
  9318. }
  9319. const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const
  9320. {
  9321. // Simple word-wrapping for English, not full-featured. Please submit failing cases!
  9322. // FIXME: Much possible improvements (don't cut things like "word !", "word!!!" but cut within "word,,,,", more sensible support for punctuations, support for Unicode punctuations, etc.)
  9323. // For references, possible wrap point marked with ^
  9324. // "aaa bbb, ccc,ddd. eee fff. ggg!"
  9325. // ^ ^ ^ ^ ^__ ^ ^
  9326. // List of hardcoded separators: .,;!?'"
  9327. // Skip extra blanks after a line returns (that includes not counting them in width computation)
  9328. // e.g. "Hello world" --> "Hello" "World"
  9329. // Cut words that cannot possibly fit within one line.
  9330. // e.g.: "The tropical fish" with ~5 characters worth of width --> "The tr" "opical" "fish"
  9331. float line_width = 0.0f;
  9332. float word_width = 0.0f;
  9333. float blank_width = 0.0f;
  9334. const char* word_end = text;
  9335. const char* prev_word_end = NULL;
  9336. bool inside_word = true;
  9337. const char* s = text;
  9338. while (s < text_end)
  9339. {
  9340. unsigned int c = (unsigned int)*s;
  9341. const char* next_s;
  9342. if (c < 0x80)
  9343. next_s = s + 1;
  9344. else
  9345. next_s = s + ImTextCharFromUtf8(&c, s, text_end);
  9346. if (c == 0)
  9347. break;
  9348. if (c < 32)
  9349. {
  9350. if (c == '\n')
  9351. {
  9352. line_width = word_width = blank_width = 0.0f;
  9353. inside_word = true;
  9354. s = next_s;
  9355. continue;
  9356. }
  9357. if (c == '\r')
  9358. {
  9359. s = next_s;
  9360. continue;
  9361. }
  9362. }
  9363. const float char_width = ((int)c < IndexXAdvance.Size) ? IndexXAdvance[(int)c] * scale : FallbackXAdvance;
  9364. if (ImCharIsSpace(c))
  9365. {
  9366. if (inside_word)
  9367. {
  9368. line_width += blank_width;
  9369. blank_width = 0.0f;
  9370. }
  9371. blank_width += char_width;
  9372. inside_word = false;
  9373. }
  9374. else
  9375. {
  9376. word_width += char_width;
  9377. if (inside_word)
  9378. {
  9379. word_end = next_s;
  9380. }
  9381. else
  9382. {
  9383. prev_word_end = word_end;
  9384. line_width += word_width + blank_width;
  9385. word_width = blank_width = 0.0f;
  9386. }
  9387. // Allow wrapping after punctuation.
  9388. inside_word = !(c == '.' || c == ',' || c == ';' || c == '!' || c == '?' || c == '\"');
  9389. }
  9390. // We ignore blank width at the end of the line (they can be skipped)
  9391. if (line_width + word_width >= wrap_width)
  9392. {
  9393. // Words that cannot possibly fit within an entire line will be cut anywhere.
  9394. if (word_width < wrap_width)
  9395. s = prev_word_end ? prev_word_end : word_end;
  9396. break;
  9397. }
  9398. s = next_s;
  9399. }
  9400. return s;
  9401. }
  9402. ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** remaining) const
  9403. {
  9404. if (!text_end)
  9405. text_end = text_begin + strlen(text_begin); // FIXME-OPT: Need to avoid this.
  9406. const float line_height = size;
  9407. const float scale = size / FontSize;
  9408. ImVec2 text_size = ImVec2(0,0);
  9409. float line_width = 0.0f;
  9410. const bool word_wrap_enabled = (wrap_width > 0.0f);
  9411. const char* word_wrap_eol = NULL;
  9412. const char* s = text_begin;
  9413. while (s < text_end)
  9414. {
  9415. if (word_wrap_enabled)
  9416. {
  9417. // Calculate how far we can render. Requires two passes on the string data but keeps the code simple and not intrusive for what's essentially an uncommon feature.
  9418. if (!word_wrap_eol)
  9419. {
  9420. word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - line_width);
  9421. if (word_wrap_eol == s) // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity.
  9422. word_wrap_eol++; // +1 may not be a character start point in UTF-8 but it's ok because we use s >= word_wrap_eol below
  9423. }
  9424. if (s >= word_wrap_eol)
  9425. {
  9426. if (text_size.x < line_width)
  9427. text_size.x = line_width;
  9428. text_size.y += line_height;
  9429. line_width = 0.0f;
  9430. word_wrap_eol = NULL;
  9431. // Wrapping skips upcoming blanks
  9432. while (s < text_end)
  9433. {
  9434. const char c = *s;
  9435. if (ImCharIsSpace(c)) { s++; } else if (c == '\n') { s++; break; } else { break; }
  9436. }
  9437. continue;
  9438. }
  9439. }
  9440. // Decode and advance source (handle unlikely UTF-8 decoding failure by skipping to the next byte)
  9441. const char* prev_s = s;
  9442. unsigned int c = (unsigned int)*s;
  9443. if (c < 0x80)
  9444. {
  9445. s += 1;
  9446. }
  9447. else
  9448. {
  9449. s += ImTextCharFromUtf8(&c, s, text_end);
  9450. if (c == 0)
  9451. break;
  9452. }
  9453. if (c < 32)
  9454. {
  9455. if (c == '\n')
  9456. {
  9457. text_size.x = ImMax(text_size.x, line_width);
  9458. text_size.y += line_height;
  9459. line_width = 0.0f;
  9460. continue;
  9461. }
  9462. if (c == '\r')
  9463. continue;
  9464. }
  9465. const float char_width = ((int)c < IndexXAdvance.Size ? IndexXAdvance[(int)c] : FallbackXAdvance) * scale;
  9466. if (line_width + char_width >= max_width)
  9467. {
  9468. s = prev_s;
  9469. break;
  9470. }
  9471. line_width += char_width;
  9472. }
  9473. if (text_size.x < line_width)
  9474. text_size.x = line_width;
  9475. if (line_width > 0 || text_size.y == 0.0f)
  9476. text_size.y += line_height;
  9477. if (remaining)
  9478. *remaining = s;
  9479. return text_size;
  9480. }
  9481. void ImFont::RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, ImDrawList* draw_list, float wrap_width, bool cpu_fine_clip) const
  9482. {
  9483. if (!text_end)
  9484. text_end = text_begin + strlen(text_begin);
  9485. // Align to be pixel perfect
  9486. pos.x = (float)(int)pos.x + DisplayOffset.x;
  9487. pos.y = (float)(int)pos.y + DisplayOffset.y;
  9488. float x = pos.x;
  9489. float y = pos.y;
  9490. if (y > clip_rect.w)
  9491. return;
  9492. const float scale = size / FontSize;
  9493. const float line_height = FontSize * scale;
  9494. const bool word_wrap_enabled = (wrap_width > 0.0f);
  9495. const char* word_wrap_eol = NULL;
  9496. ImDrawVert* vtx_write = draw_list->_VtxWritePtr;
  9497. ImDrawIdx* idx_write = draw_list->_IdxWritePtr;
  9498. unsigned int vtx_current_idx = draw_list->_VtxCurrentIdx;
  9499. const char* s = text_begin;
  9500. if (!word_wrap_enabled && y + line_height < clip_rect.y)
  9501. while (s < text_end && *s != '\n') // Fast-forward to next line
  9502. s++;
  9503. while (s < text_end)
  9504. {
  9505. if (word_wrap_enabled)
  9506. {
  9507. // Calculate how far we can render. Requires two passes on the string data but keeps the code simple and not intrusive for what's essentially an uncommon feature.
  9508. if (!word_wrap_eol)
  9509. {
  9510. word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - (x - pos.x));
  9511. if (word_wrap_eol == s) // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity.
  9512. word_wrap_eol++; // +1 may not be a character start point in UTF-8 but it's ok because we use s >= word_wrap_eol below
  9513. }
  9514. if (s >= word_wrap_eol)
  9515. {
  9516. x = pos.x;
  9517. y += line_height;
  9518. word_wrap_eol = NULL;
  9519. // Wrapping skips upcoming blanks
  9520. while (s < text_end)
  9521. {
  9522. const char c = *s;
  9523. if (ImCharIsSpace(c)) { s++; } else if (c == '\n') { s++; break; } else { break; }
  9524. }
  9525. continue;
  9526. }
  9527. }
  9528. // Decode and advance source (handle unlikely UTF-8 decoding failure by skipping to the next byte)
  9529. unsigned int c = (unsigned int)*s;
  9530. if (c < 0x80)
  9531. {
  9532. s += 1;
  9533. }
  9534. else
  9535. {
  9536. s += ImTextCharFromUtf8(&c, s, text_end);
  9537. if (c == 0)
  9538. break;
  9539. }
  9540. if (c < 32)
  9541. {
  9542. if (c == '\n')
  9543. {
  9544. x = pos.x;
  9545. y += line_height;
  9546. if (y > clip_rect.w)
  9547. break;
  9548. if (!word_wrap_enabled && y + line_height < clip_rect.y)
  9549. while (s < text_end && *s != '\n') // Fast-forward to next line
  9550. s++;
  9551. continue;
  9552. }
  9553. if (c == '\r')
  9554. continue;
  9555. }
  9556. float char_width = 0.0f;
  9557. if (const Glyph* glyph = FindGlyph((unsigned short)c))
  9558. {
  9559. char_width = glyph->XAdvance * scale;
  9560. // Clipping on Y is more likely
  9561. if (c != ' ' && c != '\t')
  9562. {
  9563. // We don't do a second finer clipping test on the Y axis (todo: do some measurement see if it is worth it, probably not)
  9564. float y1 = (float)(y + glyph->Y0 * scale);
  9565. float y2 = (float)(y + glyph->Y1 * scale);
  9566. float x1 = (float)(x + glyph->X0 * scale);
  9567. float x2 = (float)(x + glyph->X1 * scale);
  9568. if (x1 <= clip_rect.z && x2 >= clip_rect.x)
  9569. {
  9570. // Render a character
  9571. float u1 = glyph->U0;
  9572. float v1 = glyph->V0;
  9573. float u2 = glyph->U1;
  9574. float v2 = glyph->V1;
  9575. // CPU side clipping used to fit text in their frame when the frame is too small. Only does clipping for axis aligned quads
  9576. if (cpu_fine_clip)
  9577. {
  9578. if (x1 < clip_rect.x)
  9579. {
  9580. u1 = u1 + (1.0f - (x2 - clip_rect.x) / (x2 - x1)) * (u2 - u1);
  9581. x1 = clip_rect.x;
  9582. }
  9583. if (y1 < clip_rect.y)
  9584. {
  9585. v1 = v1 + (1.0f - (y2 - clip_rect.y) / (y2 - y1)) * (v2 - v1);
  9586. y1 = clip_rect.y;
  9587. }
  9588. if (x2 > clip_rect.z)
  9589. {
  9590. u2 = u1 + ((clip_rect.z - x1) / (x2 - x1)) * (u2 - u1);
  9591. x2 = clip_rect.z;
  9592. }
  9593. if (y2 > clip_rect.w)
  9594. {
  9595. v2 = v1 + ((clip_rect.w - y1) / (y2 - y1)) * (v2 - v1);
  9596. y2 = clip_rect.w;
  9597. }
  9598. if (y1 >= y2)
  9599. {
  9600. x += char_width;
  9601. continue;
  9602. }
  9603. }
  9604. // NB: we are not calling PrimRectUV() here because non-inlined causes too much overhead in a debug build.
  9605. // inlined:
  9606. {
  9607. idx_write[0] = (ImDrawIdx)(vtx_current_idx); idx_write[1] = (ImDrawIdx)(vtx_current_idx+1); idx_write[2] = (ImDrawIdx)(vtx_current_idx+2);
  9608. idx_write[3] = (ImDrawIdx)(vtx_current_idx); idx_write[4] = (ImDrawIdx)(vtx_current_idx+2); idx_write[5] = (ImDrawIdx)(vtx_current_idx+3);
  9609. vtx_write[0].pos.x = x1; vtx_write[0].pos.y = y1; vtx_write[0].col = col; vtx_write[0].uv.x = u1; vtx_write[0].uv.y = v1;
  9610. vtx_write[1].pos.x = x2; vtx_write[1].pos.y = y1; vtx_write[1].col = col; vtx_write[1].uv.x = u2; vtx_write[1].uv.y = v1;
  9611. vtx_write[2].pos.x = x2; vtx_write[2].pos.y = y2; vtx_write[2].col = col; vtx_write[2].uv.x = u2; vtx_write[2].uv.y = v2;
  9612. vtx_write[3].pos.x = x1; vtx_write[3].pos.y = y2; vtx_write[3].col = col; vtx_write[3].uv.x = u1; vtx_write[3].uv.y = v2;
  9613. vtx_write += 4;
  9614. vtx_current_idx += 4;
  9615. idx_write += 6;
  9616. }
  9617. }
  9618. }
  9619. }
  9620. x += char_width;
  9621. }
  9622. draw_list->_VtxWritePtr = vtx_write;
  9623. draw_list->_VtxCurrentIdx = vtx_current_idx;
  9624. draw_list->_IdxWritePtr = idx_write;
  9625. }
  9626. //-----------------------------------------------------------------------------
  9627. // PLATFORM DEPENDANT HELPERS
  9628. //-----------------------------------------------------------------------------
  9629. #if defined(_MSC_VER) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCS)
  9630. #ifndef _WINDOWS_
  9631. #define WIN32_LEAN_AND_MEAN
  9632. #include <windows.h>
  9633. #endif
  9634. #pragma comment(lib, "user32")
  9635. // Win32 API clipboard implementation
  9636. static const char* GetClipboardTextFn_DefaultImpl()
  9637. {
  9638. static char* buf_local = NULL;
  9639. if (buf_local)
  9640. {
  9641. ImGui::MemFree(buf_local);
  9642. buf_local = NULL;
  9643. }
  9644. if (!OpenClipboard(NULL))
  9645. return NULL;
  9646. HANDLE wbuf_handle = GetClipboardData(CF_UNICODETEXT);
  9647. if (wbuf_handle == NULL)
  9648. return NULL;
  9649. if (ImWchar* wbuf_global = (ImWchar*)GlobalLock(wbuf_handle))
  9650. {
  9651. int buf_len = ImTextCountUtf8BytesFromStr(wbuf_global, NULL) + 1;
  9652. buf_local = (char*)ImGui::MemAlloc(buf_len * sizeof(char));
  9653. ImTextStrToUtf8(buf_local, buf_len, wbuf_global, NULL);
  9654. }
  9655. GlobalUnlock(wbuf_handle);
  9656. CloseClipboard();
  9657. return buf_local;
  9658. }
  9659. // Win32 API clipboard implementation
  9660. static void SetClipboardTextFn_DefaultImpl(const char* text)
  9661. {
  9662. if (!OpenClipboard(NULL))
  9663. return;
  9664. const int wbuf_length = ImTextCountCharsFromUtf8(text, NULL) + 1;
  9665. HGLOBAL wbuf_handle = GlobalAlloc(GMEM_MOVEABLE, (SIZE_T)wbuf_length * sizeof(ImWchar));
  9666. if (wbuf_handle == NULL)
  9667. return;
  9668. ImWchar* wbuf_global = (ImWchar*)GlobalLock(wbuf_handle);
  9669. ImTextStrFromUtf8(wbuf_global, wbuf_length, text, NULL);
  9670. GlobalUnlock(wbuf_handle);
  9671. EmptyClipboard();
  9672. SetClipboardData(CF_UNICODETEXT, wbuf_handle);
  9673. CloseClipboard();
  9674. }
  9675. #else
  9676. // Local ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers
  9677. static const char* GetClipboardTextFn_DefaultImpl()
  9678. {
  9679. return GImGui->PrivateClipboard;
  9680. }
  9681. // Local ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers
  9682. static void SetClipboardTextFn_DefaultImpl(const char* text)
  9683. {
  9684. ImGuiState& g = *GImGui;
  9685. if (g.PrivateClipboard)
  9686. {
  9687. ImGui::MemFree(g.PrivateClipboard);
  9688. g.PrivateClipboard = NULL;
  9689. }
  9690. const char* text_end = text + strlen(text);
  9691. g.PrivateClipboard = (char*)ImGui::MemAlloc((size_t)(text_end - text) + 1);
  9692. memcpy(g.PrivateClipboard, text, (size_t)(text_end - text));
  9693. g.PrivateClipboard[(int)(text_end - text)] = 0;
  9694. }
  9695. #endif
  9696. #if defined(_MSC_VER) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCS)
  9697. #ifndef _WINDOWS_
  9698. #define WIN32_LEAN_AND_MEAN
  9699. #include <windows.h>
  9700. #endif
  9701. #include <Imm.h>
  9702. #pragma comment(lib, "imm32")
  9703. static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y)
  9704. {
  9705. // Notify OS Input Method Editor of text input position
  9706. if (HWND hwnd = (HWND)GImGui->IO.ImeWindowHandle)
  9707. if (HIMC himc = ImmGetContext(hwnd))
  9708. {
  9709. COMPOSITIONFORM cf;
  9710. cf.ptCurrentPos.x = x;
  9711. cf.ptCurrentPos.y = y;
  9712. cf.dwStyle = CFS_FORCE_POSITION;
  9713. ImmSetCompositionWindow(himc, &cf);
  9714. }
  9715. }
  9716. #else
  9717. static void ImeSetInputScreenPosFn_DefaultImpl(int, int)
  9718. {
  9719. }
  9720. #endif
  9721. //-----------------------------------------------------------------------------
  9722. // HELP
  9723. //-----------------------------------------------------------------------------
  9724. void ImGui::ShowMetricsWindow(bool* opened)
  9725. {
  9726. if (ImGui::Begin("ImGui Metrics", opened))
  9727. {
  9728. ImGui::Text("ImGui %s", ImGui::GetVersion());
  9729. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  9730. ImGui::Text("%d vertices, %d triangles", ImGui::GetIO().MetricsRenderVertices, ImGui::GetIO().MetricsRenderIndices / 3);
  9731. ImGui::Text("%d allocations", ImGui::GetIO().MetricsAllocs);
  9732. static bool show_clip_rects = true;
  9733. ImGui::Checkbox("Show clipping rectangles when hovering ImDrawList", &show_clip_rects);
  9734. ImGui::Separator();
  9735. struct Funcs
  9736. {
  9737. static void NodeDrawList(ImDrawList* draw_list, const char* label)
  9738. {
  9739. bool node_opened = ImGui::TreeNode(draw_list, "%s: '%s' %d vtx, %d indices, %d cmds", label, draw_list->_OwnerName ? draw_list->_OwnerName : "", draw_list->VtxBuffer.Size, draw_list->IdxBuffer.Size, draw_list->CmdBuffer.Size);
  9740. if (draw_list == ImGui::GetWindowDrawList())
  9741. {
  9742. ImGui::SameLine();
  9743. ImGui::TextColored(ImColor(255,100,100), "CURRENTLY APPENDING"); // Can't display stats for active draw list! (we don't have the data double-buffered)
  9744. }
  9745. if (!node_opened)
  9746. return;
  9747. int elem_offset = 0;
  9748. for (const ImDrawCmd* pcmd = draw_list->CmdBuffer.begin(); pcmd < draw_list->CmdBuffer.end(); elem_offset += pcmd->ElemCount, pcmd++)
  9749. {
  9750. if (pcmd->UserCallback)
  9751. ImGui::BulletText("Callback %p, user_data %p", pcmd->UserCallback, pcmd->UserCallbackData);
  9752. else
  9753. {
  9754. ImGui::BulletText("Draw %d indexed vtx, tex = %p, clip_rect = (%.0f,%.0f)..(%.0f,%.0f)", pcmd->ElemCount, pcmd->TextureId, pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w);
  9755. if (show_clip_rects && ImGui::IsItemHovered())
  9756. {
  9757. ImRect clip_rect = pcmd->ClipRect;
  9758. ImRect vtxs_rect;
  9759. for (int i = elem_offset; i < elem_offset + (int)pcmd->ElemCount; i++)
  9760. vtxs_rect.Add(draw_list->VtxBuffer[draw_list->IdxBuffer[i]].pos);
  9761. GImGui->OverlayDrawList.PushClipRectFullScreen();
  9762. clip_rect.Round(); GImGui->OverlayDrawList.AddRect(clip_rect.Min, clip_rect.Max, ImColor(255,255,0));
  9763. vtxs_rect.Round(); GImGui->OverlayDrawList.AddRect(vtxs_rect.Min, vtxs_rect.Max, ImColor(255,0,255));
  9764. GImGui->OverlayDrawList.PopClipRect();
  9765. }
  9766. }
  9767. }
  9768. ImGui::TreePop();
  9769. }
  9770. static void NodeWindows(ImVector<ImGuiWindow*>& windows, const char* label)
  9771. {
  9772. if (!ImGui::TreeNode(label, "%s (%d)", label, windows.Size))
  9773. return;
  9774. for (int i = 0; i < windows.Size; i++)
  9775. Funcs::NodeWindow(windows[i], "Window");
  9776. ImGui::TreePop();
  9777. }
  9778. static void NodeWindow(ImGuiWindow* window, const char* label)
  9779. {
  9780. if (!ImGui::TreeNode(window, "%s '%s', %d @ 0x%p", label, window->Name, window->Active || window->WasActive, window))
  9781. return;
  9782. NodeDrawList(window->DrawList, "DrawList");
  9783. if (window->RootWindow != window) NodeWindow(window->RootWindow, "RootWindow");
  9784. if (window->DC.ChildWindows.Size > 0) NodeWindows(window->DC.ChildWindows, "ChildWindows");
  9785. ImGui::BulletText("Storage: %d bytes", window->StateStorage.Data.Size * sizeof(ImGuiStorage::Pair));
  9786. ImGui::TreePop();
  9787. }
  9788. };
  9789. ImGuiState& g = *GImGui; // Access private state
  9790. g.DisableHideTextAfterDoubleHash++; // Not exposed (yet). Disable processing that hides text after '##' markers.
  9791. Funcs::NodeWindows(g.Windows, "Windows");
  9792. if (ImGui::TreeNode("DrawList", "Active DrawLists (%d)", g.RenderDrawLists[0].Size))
  9793. {
  9794. for (int i = 0; i < g.RenderDrawLists[0].Size; i++)
  9795. Funcs::NodeDrawList(g.RenderDrawLists[0][i], "DrawList");
  9796. ImGui::TreePop();
  9797. }
  9798. if (ImGui::TreeNode("Popups", "Opened Popups Stack (%d)", g.OpenedPopupStack.Size))
  9799. {
  9800. for (int i = 0; i < g.OpenedPopupStack.Size; i++)
  9801. {
  9802. ImGuiWindow* window = g.OpenedPopupStack[i].Window;
  9803. ImGui::BulletText("PopupID: %08x, Window: '%s'%s%s", g.OpenedPopupStack[i].PopupID, window ? window->Name : "NULL", window && (window->Flags & ImGuiWindowFlags_ChildWindow) ? " ChildWindow" : "", window && (window->Flags & ImGuiWindowFlags_ChildMenu) ? " ChildMenu" : "");
  9804. }
  9805. ImGui::TreePop();
  9806. }
  9807. g.DisableHideTextAfterDoubleHash--;
  9808. }
  9809. ImGui::End();
  9810. }
  9811. void ImGui::ShowUserGuide()
  9812. {
  9813. ImGui::BulletText("Double-click on title bar to collapse window.");
  9814. ImGui::BulletText("Click and drag on lower right corner to resize window.");
  9815. ImGui::BulletText("Click and drag on any empty space to move window.");
  9816. ImGui::BulletText("Mouse Wheel to scroll.");
  9817. if (ImGui::GetIO().FontAllowUserScaling)
  9818. ImGui::BulletText("CTRL+Mouse Wheel to zoom window contents.");
  9819. ImGui::BulletText("TAB/SHIFT+TAB to cycle through keyboard editable fields.");
  9820. ImGui::BulletText("CTRL+Click on a slider or drag box to input text.");
  9821. ImGui::BulletText(
  9822. "While editing text:\n"
  9823. "- Hold SHIFT or use mouse to select text\n"
  9824. "- CTRL+Left/Right to word jump\n"
  9825. "- CTRL+A or double-click to select all\n"
  9826. "- CTRL+X,CTRL+C,CTRL+V clipboard\n"
  9827. "- CTRL+Z,CTRL+Y undo/redo\n"
  9828. "- ESCAPE to revert\n"
  9829. "- You can apply arithmetic operators +,*,/ on numerical values.\n"
  9830. " Use +- to subtract.\n");
  9831. }
  9832. void ImGui::ShowStyleEditor(ImGuiStyle* ref)
  9833. {
  9834. ImGuiStyle& style = ImGui::GetStyle();
  9835. const ImGuiStyle def; // Default style
  9836. if (ImGui::Button("Revert Style"))
  9837. style = ref ? *ref : def;
  9838. if (ref)
  9839. {
  9840. ImGui::SameLine();
  9841. if (ImGui::Button("Save Style"))
  9842. *ref = style;
  9843. }
  9844. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.55f);
  9845. if (ImGui::TreeNode("Rendering"))
  9846. {
  9847. ImGui::Checkbox("Anti-aliased lines", &style.AntiAliasedLines);
  9848. ImGui::Checkbox("Anti-aliased shapes", &style.AntiAliasedShapes);
  9849. ImGui::TreePop();
  9850. }
  9851. if (ImGui::TreeNode("Sizes"))
  9852. {
  9853. ImGui::SliderFloat("Alpha", &style.Alpha, 0.20f, 1.0f, "%.2f"); // Not exposing zero here so user doesn't "lose" the UI. But application code could have a toggle to switch between zero and non-zero.
  9854. ImGui::SliderFloat2("WindowPadding", (float*)&style.WindowPadding, 0.0f, 20.0f, "%.0f");
  9855. ImGui::SliderFloat("WindowRounding", &style.WindowRounding, 0.0f, 16.0f, "%.0f");
  9856. ImGui::SliderFloat("ChildWindowRounding", &style.ChildWindowRounding, 0.0f, 16.0f, "%.0f");
  9857. ImGui::SliderFloat2("FramePadding", (float*)&style.FramePadding, 0.0f, 20.0f, "%.0f");
  9858. ImGui::SliderFloat("FrameRounding", &style.FrameRounding, 0.0f, 16.0f, "%.0f");
  9859. ImGui::SliderFloat2("ItemSpacing", (float*)&style.ItemSpacing, 0.0f, 20.0f, "%.0f");
  9860. ImGui::SliderFloat2("ItemInnerSpacing", (float*)&style.ItemInnerSpacing, 0.0f, 20.0f, "%.0f");
  9861. ImGui::SliderFloat2("TouchExtraPadding", (float*)&style.TouchExtraPadding, 0.0f, 10.0f, "%.0f");
  9862. ImGui::SliderFloat("IndentSpacing", &style.IndentSpacing, 0.0f, 30.0f, "%.0f");
  9863. ImGui::SliderFloat("ScrollbarWidth", &style.ScrollbarWidth, 1.0f, 20.0f, "%.0f");
  9864. ImGui::SliderFloat("ScrollbarRounding", &style.ScrollbarRounding, 0.0f, 16.0f, "%.0f");
  9865. ImGui::SliderFloat("GrabMinSize", &style.GrabMinSize, 1.0f, 20.0f, "%.0f");
  9866. ImGui::SliderFloat("GrabRounding", &style.GrabRounding, 0.0f, 16.0f, "%.0f");
  9867. ImGui::TreePop();
  9868. }
  9869. if (ImGui::TreeNode("Colors"))
  9870. {
  9871. static int output_dest = 0;
  9872. static bool output_only_modified = false;
  9873. if (ImGui::Button("Output Colors"))
  9874. {
  9875. if (output_dest == 0)
  9876. ImGui::LogToClipboard();
  9877. else
  9878. ImGui::LogToTTY();
  9879. ImGui::LogText("ImGuiStyle& style = ImGui::GetStyle();" IM_NEWLINE);
  9880. for (int i = 0; i < ImGuiCol_COUNT; i++)
  9881. {
  9882. const ImVec4& col = style.Colors[i];
  9883. const char* name = ImGui::GetStyleColName(i);
  9884. if (!output_only_modified || memcmp(&col, (ref ? &ref->Colors[i] : &def.Colors[i]), sizeof(ImVec4)) != 0)
  9885. ImGui::LogText("style.Colors[ImGuiCol_%s]%*s= ImVec4(%.2ff, %.2ff, %.2ff, %.2ff);" IM_NEWLINE, name, 22 - strlen(name), "", col.x, col.y, col.z, col.w);
  9886. }
  9887. ImGui::LogFinish();
  9888. }
  9889. ImGui::SameLine(); ImGui::PushItemWidth(120); ImGui::Combo("##output_type", &output_dest, "To Clipboard\0To TTY"); ImGui::PopItemWidth();
  9890. ImGui::SameLine(); ImGui::Checkbox("Only Modified Fields", &output_only_modified);
  9891. static ImGuiColorEditMode edit_mode = ImGuiColorEditMode_RGB;
  9892. ImGui::RadioButton("RGB", &edit_mode, ImGuiColorEditMode_RGB);
  9893. ImGui::SameLine();
  9894. ImGui::RadioButton("HSV", &edit_mode, ImGuiColorEditMode_HSV);
  9895. ImGui::SameLine();
  9896. ImGui::RadioButton("HEX", &edit_mode, ImGuiColorEditMode_HEX);
  9897. //ImGui::Text("Tip: Click on colored square to change edit mode.");
  9898. static ImGuiTextFilter filter;
  9899. filter.Draw("Filter colors", 200);
  9900. ImGui::BeginChild("#colors", ImVec2(0, 300), true);
  9901. ImGui::PushItemWidth(-160);
  9902. ImGui::ColorEditMode(edit_mode);
  9903. for (int i = 0; i < ImGuiCol_COUNT; i++)
  9904. {
  9905. const char* name = ImGui::GetStyleColName(i);
  9906. if (!filter.PassFilter(name))
  9907. continue;
  9908. ImGui::PushID(i);
  9909. ImGui::ColorEdit4(name, (float*)&style.Colors[i], true);
  9910. if (memcmp(&style.Colors[i], (ref ? &ref->Colors[i] : &def.Colors[i]), sizeof(ImVec4)) != 0)
  9911. {
  9912. ImGui::SameLine(); if (ImGui::Button("Revert")) style.Colors[i] = ref ? ref->Colors[i] : def.Colors[i];
  9913. if (ref) { ImGui::SameLine(); if (ImGui::Button("Save")) ref->Colors[i] = style.Colors[i]; }
  9914. }
  9915. ImGui::PopID();
  9916. }
  9917. ImGui::PopItemWidth();
  9918. ImGui::EndChild();
  9919. ImGui::TreePop();
  9920. }
  9921. ImGui::PopItemWidth();
  9922. }
  9923. //-----------------------------------------------------------------------------
  9924. // SAMPLE CODE
  9925. //-----------------------------------------------------------------------------
  9926. #ifndef IMGUI_DISABLE_TEST_WINDOWS
  9927. static void ShowExampleAppConsole(bool* opened);
  9928. static void ShowExampleAppLayout(bool* opened);
  9929. static void ShowExampleAppLongText(bool* opened);
  9930. static void ShowExampleAppAutoResize(bool* opened);
  9931. static void ShowExampleAppFixedOverlay(bool* opened);
  9932. static void ShowExampleAppManipulatingWindowTitle(bool* opened);
  9933. static void ShowExampleAppCustomRendering(bool* opened);
  9934. static void ShowExampleAppMainMenuBar();
  9935. static void ShowExampleMenuFile();
  9936. static void ShowHelpMarker(const char* desc)
  9937. {
  9938. ImGui::TextDisabled("(?)");
  9939. if (ImGui::IsItemHovered())
  9940. ImGui::SetTooltip(desc);
  9941. }
  9942. // Demonstrate most ImGui features (big function!)
  9943. void ImGui::ShowTestWindow(bool* opened)
  9944. {
  9945. // Examples apps
  9946. static bool show_app_metrics = false;
  9947. static bool show_app_main_menu_bar = false;
  9948. static bool show_app_console = false;
  9949. static bool show_app_layout = false;
  9950. static bool show_app_long_text = false;
  9951. static bool show_app_auto_resize = false;
  9952. static bool show_app_fixed_overlay = false;
  9953. static bool show_app_custom_rendering = false;
  9954. static bool show_app_manipulating_window_title = false;
  9955. static bool show_app_about = false;
  9956. if (show_app_metrics) ImGui::ShowMetricsWindow(&show_app_metrics);
  9957. if (show_app_main_menu_bar) ShowExampleAppMainMenuBar();
  9958. if (show_app_console) ShowExampleAppConsole(&show_app_console);
  9959. if (show_app_layout) ShowExampleAppLayout(&show_app_layout);
  9960. if (show_app_long_text) ShowExampleAppLongText(&show_app_long_text);
  9961. if (show_app_auto_resize) ShowExampleAppAutoResize(&show_app_auto_resize);
  9962. if (show_app_fixed_overlay) ShowExampleAppFixedOverlay(&show_app_fixed_overlay);
  9963. if (show_app_manipulating_window_title) ShowExampleAppManipulatingWindowTitle(&show_app_manipulating_window_title);
  9964. if (show_app_custom_rendering) ShowExampleAppCustomRendering(&show_app_custom_rendering);
  9965. if (show_app_about)
  9966. {
  9967. ImGui::Begin("About ImGui", &show_app_about, ImGuiWindowFlags_AlwaysAutoResize);
  9968. ImGui::Text("ImGui %s", ImGui::GetVersion());
  9969. ImGui::Separator();
  9970. ImGui::Text("By Omar Cornut and all github contributors.");
  9971. ImGui::Text("ImGui is licensed under the MIT License, see LICENSE for more information.");
  9972. ImGui::End();
  9973. }
  9974. static bool no_titlebar = false;
  9975. static bool no_border = true;
  9976. static bool no_resize = false;
  9977. static bool no_move = false;
  9978. static bool no_scrollbar = false;
  9979. static bool no_collapse = false;
  9980. static bool no_menu = false;
  9981. static float bg_alpha = 0.65f;
  9982. // Demonstrate the various window flags. Typically you would just use the default.
  9983. ImGuiWindowFlags window_flags = 0;
  9984. if (no_titlebar) window_flags |= ImGuiWindowFlags_NoTitleBar;
  9985. if (!no_border) window_flags |= ImGuiWindowFlags_ShowBorders;
  9986. if (no_resize) window_flags |= ImGuiWindowFlags_NoResize;
  9987. if (no_move) window_flags |= ImGuiWindowFlags_NoMove;
  9988. if (no_scrollbar) window_flags |= ImGuiWindowFlags_NoScrollbar;
  9989. if (no_collapse) window_flags |= ImGuiWindowFlags_NoCollapse;
  9990. if (!no_menu) window_flags |= ImGuiWindowFlags_MenuBar;
  9991. if (!ImGui::Begin("ImGui Test", opened, ImVec2(550,680), bg_alpha, window_flags))
  9992. {
  9993. // Early out if the window is collapsed, as an optimization.
  9994. ImGui::End();
  9995. return;
  9996. }
  9997. //ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.65f); // 2/3 of the space for widget and 1/3 for labels
  9998. ImGui::PushItemWidth(-140); // Right align, keep 140 pixels for labels
  9999. ImGui::Text("ImGui says hello.");
  10000. // Menu
  10001. if (ImGui::BeginMenuBar())
  10002. {
  10003. if (ImGui::BeginMenu("Menu"))
  10004. {
  10005. ShowExampleMenuFile();
  10006. ImGui::EndMenu();
  10007. }
  10008. if (ImGui::BeginMenu("Examples"))
  10009. {
  10010. ImGui::MenuItem("Main menu bar", NULL, &show_app_main_menu_bar);
  10011. ImGui::MenuItem("Console", NULL, &show_app_console);
  10012. ImGui::MenuItem("Simple layout", NULL, &show_app_layout);
  10013. ImGui::MenuItem("Long text display", NULL, &show_app_long_text);
  10014. ImGui::MenuItem("Auto-resizing window", NULL, &show_app_auto_resize);
  10015. ImGui::MenuItem("Simple overlay", NULL, &show_app_fixed_overlay);
  10016. ImGui::MenuItem("Manipulating window title", NULL, &show_app_manipulating_window_title);
  10017. ImGui::MenuItem("Custom rendering", NULL, &show_app_custom_rendering);
  10018. ImGui::EndMenu();
  10019. }
  10020. if (ImGui::BeginMenu("Help"))
  10021. {
  10022. ImGui::MenuItem("Metrics", NULL, &show_app_metrics);
  10023. ImGui::MenuItem("About ImGui", NULL, &show_app_about);
  10024. ImGui::EndMenu();
  10025. }
  10026. ImGui::EndMenuBar();
  10027. }
  10028. ImGui::Spacing();
  10029. if (ImGui::CollapsingHeader("Help"))
  10030. {
  10031. ImGui::TextWrapped("This window is being created by the ShowTestWindow() function. Please refer to the code for programming reference.\n\nUser Guide:");
  10032. ImGui::ShowUserGuide();
  10033. }
  10034. if (ImGui::CollapsingHeader("Window options"))
  10035. {
  10036. ImGui::Checkbox("no titlebar", &no_titlebar); ImGui::SameLine(150);
  10037. ImGui::Checkbox("no border", &no_border); ImGui::SameLine(300);
  10038. ImGui::Checkbox("no resize", &no_resize);
  10039. ImGui::Checkbox("no move", &no_move); ImGui::SameLine(150);
  10040. ImGui::Checkbox("no scrollbar", &no_scrollbar); ImGui::SameLine(300);
  10041. ImGui::Checkbox("no collapse", &no_collapse);
  10042. ImGui::Checkbox("no menu", &no_menu);
  10043. ImGui::SliderFloat("bg alpha", &bg_alpha, 0.0f, 1.0f);
  10044. if (ImGui::TreeNode("Style"))
  10045. {
  10046. ImGui::ShowStyleEditor();
  10047. ImGui::TreePop();
  10048. }
  10049. if (ImGui::TreeNode("Fonts", "Fonts (%d)", ImGui::GetIO().Fonts->Fonts.Size))
  10050. {
  10051. ImGui::TextWrapped("Tip: Load fonts with io.Fonts->AddFontFromFileTTF().");
  10052. ImFontAtlas* atlas = ImGui::GetIO().Fonts;
  10053. if (ImGui::TreeNode("Atlas texture"))
  10054. {
  10055. ImGui::Text("%dx%d pixels", atlas->TexWidth, atlas->TexHeight);
  10056. ImGui::Image(atlas->TexID, ImVec2((float)atlas->TexWidth, (float)atlas->TexHeight), ImVec2(0,0), ImVec2(1,1), ImColor(255,255,255,255), ImColor(255,255,255,128));
  10057. ImGui::TreePop();
  10058. }
  10059. ImGui::PushItemWidth(100);
  10060. for (int i = 0; i < atlas->Fonts.Size; i++)
  10061. {
  10062. ImFont* font = atlas->Fonts[i];
  10063. ImGui::BulletText("Font %d: \'%s\', %.2f px, %d glyphs", i, font->ConfigData ? font->ConfigData[0].Name : "", font->FontSize, font->Glyphs.Size);
  10064. ImGui::TreePush((void*)i);
  10065. if (i > 0) { ImGui::SameLine(); if (ImGui::SmallButton("Set as default")) { atlas->Fonts[i] = atlas->Fonts[0]; atlas->Fonts[0] = font; } }
  10066. ImGui::PushFont(font);
  10067. ImGui::Text("The quick brown fox jumps over the lazy dog");
  10068. ImGui::PopFont();
  10069. if (ImGui::TreeNode("Details"))
  10070. {
  10071. ImGui::DragFloat("font scale", &font->Scale, 0.005f, 0.3f, 2.0f, "%.1f"); // scale only this font
  10072. ImGui::Text("Ascent: %f, Descent: %f, Height: %f", font->Ascent, font->Descent, font->Ascent - font->Descent);
  10073. ImGui::Text("Fallback character: '%c' (%d)", font->FallbackChar, font->FallbackChar);
  10074. for (int config_i = 0; config_i < font->ConfigDataCount; config_i++)
  10075. ImGui::BulletText("Input %d: \'%s\'", config_i, font->ConfigData[config_i].Name);
  10076. ImGui::TreePop();
  10077. }
  10078. ImGui::TreePop();
  10079. }
  10080. static float window_scale = 1.0f;
  10081. ImGui::DragFloat("this window scale", &window_scale, 0.005f, 0.3f, 2.0f, "%.1f"); // scale only this window
  10082. ImGui::DragFloat("global scale", &ImGui::GetIO().FontGlobalScale, 0.005f, 0.3f, 2.0f, "%.1f"); // scale everything
  10083. ImGui::PopItemWidth();
  10084. ImGui::SetWindowFontScale(window_scale);
  10085. ImGui::TreePop();
  10086. }
  10087. if (ImGui::TreeNode("Logging"))
  10088. {
  10089. ImGui::TextWrapped("The logging API redirects all text output so you can easily capture the content of a window or a block. Tree nodes can be automatically expanded. You can also call ImGui::LogText() to output directly to the log without a visual output.");
  10090. ImGui::LogButtons();
  10091. ImGui::TreePop();
  10092. }
  10093. }
  10094. if (ImGui::CollapsingHeader("Widgets"))
  10095. {
  10096. if (ImGui::TreeNode("Tree"))
  10097. {
  10098. for (int i = 0; i < 5; i++)
  10099. {
  10100. if (ImGui::TreeNode((void*)i, "Child %d", i))
  10101. {
  10102. ImGui::Text("blah blah");
  10103. ImGui::SameLine();
  10104. if (ImGui::SmallButton("print"))
  10105. printf("Child %d pressed", i);
  10106. ImGui::TreePop();
  10107. }
  10108. }
  10109. ImGui::TreePop();
  10110. }
  10111. if (ImGui::TreeNode("Bullets"))
  10112. {
  10113. ImGui::BulletText("Bullet point 1");
  10114. ImGui::BulletText("Bullet point 2\nOn multiple lines");
  10115. ImGui::Bullet(); ImGui::Text("Bullet point 3 (two calls)");
  10116. ImGui::Bullet(); ImGui::SmallButton("Button");
  10117. ImGui::TreePop();
  10118. }
  10119. if (ImGui::TreeNode("Colored Text"))
  10120. {
  10121. // Using shortcut. You can use PushStyleColor()/PopStyleColor() for more flexibility.
  10122. ImGui::TextColored(ImVec4(1.0f,0.0f,1.0f,1.0f), "Pink");
  10123. ImGui::TextColored(ImVec4(1.0f,1.0f,0.0f,1.0f), "Yellow");
  10124. ImGui::TextDisabled("Disabled");
  10125. ImGui::TreePop();
  10126. }
  10127. if (ImGui::TreeNode("Word Wrapping"))
  10128. {
  10129. // Using shortcut. You can use PushTextWrapPos()/PopTextWrapPos() for more flexibility.
  10130. ImGui::TextWrapped("This text should automatically wrap on the edge of the window. The current implementation for text wrapping follows simple rules suitable for English and possibly other languages.");
  10131. ImGui::Spacing();
  10132. static float wrap_width = 200.0f;
  10133. ImGui::SliderFloat("Wrap width", &wrap_width, -20, 600, "%.0f");
  10134. ImGui::Text("Test paragraph 1:");
  10135. ImVec2 pos = ImGui::GetCursorScreenPos();
  10136. ImGui::GetWindowDrawList()->AddRectFilled(ImVec2(pos.x + wrap_width, pos.y), ImVec2(pos.x + wrap_width + 10, pos.y + ImGui::GetTextLineHeight()), 0xFFFF00FF);
  10137. ImGui::PushTextWrapPos(ImGui::GetCursorPos().x + wrap_width);
  10138. ImGui::Text("lazy dog. This paragraph is made to fit within %.0f pixels. The quick brown fox jumps over the lazy dog.", wrap_width);
  10139. ImGui::GetWindowDrawList()->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(), 0xFF00FFFF);
  10140. ImGui::PopTextWrapPos();
  10141. ImGui::Text("Test paragraph 2:");
  10142. pos = ImGui::GetCursorScreenPos();
  10143. ImGui::GetWindowDrawList()->AddRectFilled(ImVec2(pos.x + wrap_width, pos.y), ImVec2(pos.x + wrap_width + 10, pos.y + ImGui::GetTextLineHeight()), 0xFFFF00FF);
  10144. ImGui::PushTextWrapPos(ImGui::GetCursorPos().x + wrap_width);
  10145. ImGui::Text("aaaaaaaa bbbbbbbb, cccccccc,dddddddd. eeeeeeee ffffffff. gggggggg!hhhhhhhh");
  10146. ImGui::GetWindowDrawList()->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(), 0xFF00FFFF);
  10147. ImGui::PopTextWrapPos();
  10148. ImGui::TreePop();
  10149. }
  10150. if (ImGui::TreeNode("UTF-8 Text"))
  10151. {
  10152. // UTF-8 test with Japanese characters
  10153. // (needs a suitable font, try Arial Unicode or M+ fonts http://mplus-fonts.sourceforge.jp/mplus-outline-fonts/index-en.html)
  10154. // Most compiler appears to support UTF-8 in source code (with Visual Studio you need to save your file as 'UTF-8 without signature')
  10155. // However for the sake for maximum portability here we are *not* including raw UTF-8 character in this source file, instead we encode the string with hexadecimal constants.
  10156. // In your own application be reasonable and use UTF-8 in source or retrieve the data from file system!
  10157. // Note that characters values are preserved even if the font cannot be displayed, so you can safely copy & paste garbled characters into another application.
  10158. ImGui::TextWrapped("CJK text will only appears if the font was loaded with the appropriate CJK character ranges. Call io.Font->LoadFromFileTTF() manually to load extra character ranges.");
  10159. ImGui::Text("Hiragana: \xe3\x81\x8b\xe3\x81\x8d\xe3\x81\x8f\xe3\x81\x91\xe3\x81\x93 (kakikukeko)");
  10160. ImGui::Text("Kanjis: \xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e (nihongo)");
  10161. static char buf[32] = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e";
  10162. ImGui::InputText("UTF-8 input", buf, IM_ARRAYSIZE(buf));
  10163. ImGui::TreePop();
  10164. }
  10165. if (ImGui::TreeNode("Images"))
  10166. {
  10167. ImGui::TextWrapped("Below we are displaying the font texture (which is the only texture we have access to in this demo). Use the 'ImTextureID' type as storage to pass pointers or identifier to your own texture data. Hover the texture for a zoomed view!");
  10168. ImVec2 tex_screen_pos = ImGui::GetCursorScreenPos();
  10169. float tex_w = (float)ImGui::GetIO().Fonts->TexWidth;
  10170. float tex_h = (float)ImGui::GetIO().Fonts->TexHeight;
  10171. ImTextureID tex_id = ImGui::GetIO().Fonts->TexID;
  10172. ImGui::Text("%.0fx%.0f", tex_w, tex_h);
  10173. ImGui::Image(tex_id, ImVec2(tex_w, tex_h), ImVec2(0,0), ImVec2(1,1), ImColor(255,255,255,255), ImColor(255,255,255,128));
  10174. if (ImGui::IsItemHovered())
  10175. {
  10176. ImGui::BeginTooltip();
  10177. float focus_sz = 32.0f;
  10178. float focus_x = ImClamp(ImGui::GetMousePos().x - tex_screen_pos.x - focus_sz * 0.5f, 0.0f, tex_w - focus_sz);
  10179. float focus_y = ImClamp(ImGui::GetMousePos().y - tex_screen_pos.y - focus_sz * 0.5f, 0.0f, tex_h - focus_sz);
  10180. ImGui::Text("Min: (%.2f, %.2f)", focus_x, focus_y);
  10181. ImGui::Text("Max: (%.2f, %.2f)", focus_x + focus_sz, focus_y + focus_sz);
  10182. ImVec2 uv0 = ImVec2((focus_x) / tex_w, (focus_y) / tex_h);
  10183. ImVec2 uv1 = ImVec2((focus_x + focus_sz) / tex_w, (focus_y + focus_sz) / tex_h);
  10184. ImGui::Image(tex_id, ImVec2(128,128), uv0, uv1, ImColor(255,255,255,255), ImColor(255,255,255,128));
  10185. ImGui::EndTooltip();
  10186. }
  10187. ImGui::TextWrapped("And now some textured buttons..");
  10188. static int pressed_count = 0;
  10189. for (int i = 0; i < 8; i++)
  10190. {
  10191. if (i > 0)
  10192. ImGui::SameLine();
  10193. ImGui::PushID(i);
  10194. int frame_padding = -1 + i; // -1 = uses default padding
  10195. if (ImGui::ImageButton(tex_id, ImVec2(32,32), ImVec2(0,0), ImVec2(32.0f/tex_w,32/tex_h), frame_padding, ImColor(0,0,0,255)))
  10196. pressed_count += 1;
  10197. ImGui::PopID();
  10198. }
  10199. ImGui::Text("Pressed %d times.", pressed_count);
  10200. ImGui::TreePop();
  10201. }
  10202. if (ImGui::TreeNode("Selectables"))
  10203. {
  10204. if (ImGui::TreeNode("Basic"))
  10205. {
  10206. static bool selected[3] = { false, true, false };
  10207. ImGui::Selectable("1. I am selectable", &selected[0]);
  10208. ImGui::Selectable("2. I am selectable", &selected[1]);
  10209. ImGui::Text("3. I am not selectable");
  10210. ImGui::Selectable("4. I am selectable", &selected[2]);
  10211. ImGui::TreePop();
  10212. }
  10213. if (ImGui::TreeNode("Rendering more text into the same block"))
  10214. {
  10215. static bool selected[3] = { false, false, false };
  10216. ImGui::Selectable("main.c", &selected[0]); ImGui::SameLine(300); ImGui::Text(" 2,345 bytes");
  10217. ImGui::Selectable("Hello.cpp", &selected[1]); ImGui::SameLine(300); ImGui::Text("12,345 bytes");
  10218. ImGui::Selectable("Hello.h", &selected[2]); ImGui::SameLine(300); ImGui::Text(" 2,345 bytes");
  10219. ImGui::TreePop();
  10220. }
  10221. if (ImGui::TreeNode("Grid"))
  10222. {
  10223. static bool selected[16] = { true, false, false, false, false, true, false, false, false, false, true, false, false, false, false, true };
  10224. for (int i = 0; i < 16; i++)
  10225. {
  10226. ImGui::PushID(i);
  10227. if (ImGui::Selectable("Me", &selected[i], 0, ImVec2(50,50)))
  10228. {
  10229. int x = i % 4, y = i / 4;
  10230. if (x > 0) selected[i - 1] ^= 1;
  10231. if (x < 3) selected[i + 1] ^= 1;
  10232. if (y > 0) selected[i - 4] ^= 1;
  10233. if (y < 3) selected[i + 4] ^= 1;
  10234. }
  10235. if ((i % 4) < 3) ImGui::SameLine();
  10236. ImGui::PopID();
  10237. }
  10238. ImGui::TreePop();
  10239. }
  10240. ImGui::TreePop();
  10241. }
  10242. if (ImGui::TreeNode("Filtered Text Input"))
  10243. {
  10244. static char buf1[64] = ""; ImGui::InputText("default", buf1, 64);
  10245. static char buf2[64] = ""; ImGui::InputText("decimal", buf2, 64, ImGuiInputTextFlags_CharsDecimal);
  10246. static char buf3[64] = ""; ImGui::InputText("hexadecimal", buf3, 64, ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase);
  10247. static char buf4[64] = ""; ImGui::InputText("uppercase", buf4, 64, ImGuiInputTextFlags_CharsUppercase);
  10248. static char buf5[64] = ""; ImGui::InputText("no blank", buf5, 64, ImGuiInputTextFlags_CharsNoBlank);
  10249. struct TextFilters { static int FilterImGuiLetters(ImGuiTextEditCallbackData* data) { if (data->EventChar < 256 && strchr("imgui", (char)data->EventChar)) return 0; return 1; } };
  10250. static char buf6[64] = ""; ImGui::InputText("\"imgui\" letters", buf6, 64, ImGuiInputTextFlags_CallbackCharFilter, TextFilters::FilterImGuiLetters);
  10251. ImGui::TreePop();
  10252. }
  10253. if (ImGui::TreeNode("Multi-line Text Input"))
  10254. {
  10255. static char text[1024*16] = "// F00F bug\nlabel:\n\tlock cmpxchg8b eax\n";
  10256. ImGui::InputTextMultiline("##source", text, IM_ARRAYSIZE(text), ImVec2(-1.0f, ImGui::GetTextLineHeight() * 16), ImGuiInputTextFlags_AllowTabInput);
  10257. ImGui::TreePop();
  10258. }
  10259. static bool a=false;
  10260. if (ImGui::Button("Button")) { printf("Clicked\n"); a ^= 1; }
  10261. if (a)
  10262. {
  10263. ImGui::SameLine();
  10264. ImGui::Text("Thanks for clicking me!");
  10265. }
  10266. static bool check = true;
  10267. ImGui::Checkbox("checkbox", &check);
  10268. static int e = 0;
  10269. ImGui::RadioButton("radio a", &e, 0); ImGui::SameLine();
  10270. ImGui::RadioButton("radio b", &e, 1); ImGui::SameLine();
  10271. ImGui::RadioButton("radio c", &e, 2);
  10272. // Color buttons, demonstrate using PushID() to add unique identifier in the ID stack, and changing style.
  10273. for (int i = 0; i < 7; i++)
  10274. {
  10275. if (i > 0) ImGui::SameLine();
  10276. ImGui::PushID(i);
  10277. ImGui::PushStyleColor(ImGuiCol_Button, ImColor::HSV(i/7.0f, 0.6f, 0.6f));
  10278. ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImColor::HSV(i/7.0f, 0.7f, 0.7f));
  10279. ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImColor::HSV(i/7.0f, 0.8f, 0.8f));
  10280. ImGui::Button("Click");
  10281. ImGui::PopStyleColor(3);
  10282. ImGui::PopID();
  10283. }
  10284. ImGui::Text("Hover over me");
  10285. if (ImGui::IsItemHovered())
  10286. ImGui::SetTooltip("I am a tooltip");
  10287. ImGui::SameLine();
  10288. ImGui::Text("- or me");
  10289. if (ImGui::IsItemHovered())
  10290. {
  10291. ImGui::BeginTooltip();
  10292. ImGui::Text("I am a fancy tooltip");
  10293. static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f };
  10294. ImGui::PlotLines("Curve", arr, IM_ARRAYSIZE(arr));
  10295. ImGui::EndTooltip();
  10296. }
  10297. // Testing IMGUI_ONCE_UPON_A_FRAME macro
  10298. //for (int i = 0; i < 5; i++)
  10299. //{
  10300. // IMGUI_ONCE_UPON_A_FRAME
  10301. // {
  10302. // ImGui::Text("This will be displayed only once.");
  10303. // }
  10304. //}
  10305. ImGui::Separator();
  10306. ImGui::LabelText("label", "Value");
  10307. static int item = 1;
  10308. ImGui::Combo("combo", &item, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0"); // Combo using values packed in a single constant string (for really quick combo)
  10309. const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK" };
  10310. static int item2 = -1;
  10311. ImGui::Combo("combo scroll", &item2, items, IM_ARRAYSIZE(items)); // Combo using proper array. You can also pass a callback to retrieve array value, no need to create/copy an array just for that.
  10312. {
  10313. static char str0[128] = "Hello, world!";
  10314. static int i0=123;
  10315. static float f0=0.001f;
  10316. ImGui::InputText("input text", str0, IM_ARRAYSIZE(str0));
  10317. ImGui::SameLine(); ShowHelpMarker("Hold SHIFT or use mouse to select text.\n" "CTRL+Left/Right to word jump.\n" "CTRL+A or double-click to select all.\n" "CTRL+X,CTRL+C,CTRL+V clipboard.\n" "CTRL+Z,CTRL+Y undo/redo.\n" "ESCAPE to revert.\n");
  10318. ImGui::InputInt("input int", &i0);
  10319. ImGui::SameLine(); ShowHelpMarker("You can apply arithmetic operators +,*,/ on numerical values.\n e.g. [ 100 ], input \'*2\', result becomes [ 200 ]\nUse +- to subtract.\n");
  10320. ImGui::InputFloat("input float", &f0, 0.01f, 1.0f);
  10321. static float vec4a[4] = { 0.10f, 0.20f, 0.30f, 0.44f };
  10322. ImGui::InputFloat3("input float3", vec4a);
  10323. }
  10324. {
  10325. static int i1=50, i2=42;
  10326. ImGui::DragInt("drag int", &i1, 1);
  10327. ImGui::SameLine(); ShowHelpMarker("Click and drag to edit value.\nHold SHIFT/ALT for faster/slower edit.\nDouble-click or CTRL+click to input value.");
  10328. ImGui::DragInt("drag int 0..100", &i2, 1, 0, 100, "%.0f%%");
  10329. static float f1=1.00f, f2=0.0067f;
  10330. ImGui::DragFloat("drag float", &f1, 0.005f);
  10331. ImGui::DragFloat("drag small float", &f2, 0.0001f, 0.0f, 0.0f, "%.06f ns");
  10332. }
  10333. {
  10334. static int i1=0;
  10335. ImGui::SliderInt("slider int", &i1, -1, 3);
  10336. ImGui::SameLine(); ShowHelpMarker("CTRL+click to input value.");
  10337. static float f1=0.123f, f2=0.0f;
  10338. ImGui::SliderFloat("slider float", &f1, 0.0f, 1.0f, "ratio = %.3f");
  10339. ImGui::SliderFloat("slider log float", &f2, -10.0f, 10.0f, "%.4f", 3.0f);
  10340. static float angle = 0.0f;
  10341. ImGui::SliderAngle("slider angle", &angle);
  10342. }
  10343. static float col1[3] = { 1.0f,0.0f,0.2f };
  10344. static float col2[4] = { 0.4f,0.7f,0.0f,0.5f };
  10345. ImGui::ColorEdit3("color 1", col1);
  10346. ImGui::SameLine(); ShowHelpMarker("Click on the colored square to change edit mode.\nCTRL+click on individual component to input value.\n");
  10347. ImGui::ColorEdit4("color 2", col2);
  10348. const char* listbox_items[] = { "Apple", "Banana", "Cherry", "Kiwi", "Mango", "Orange", "Pineapple", "Strawberry", "Watermelon" };
  10349. static int listbox_item_current = 1;
  10350. ImGui::ListBox("listbox\n(single select)", &listbox_item_current, listbox_items, IM_ARRAYSIZE(listbox_items), 4);
  10351. //static int listbox_item_current2 = 2;
  10352. //ImGui::PushItemWidth(-1);
  10353. //ImGui::ListBox("##listbox2", &listbox_item_current2, listbox_items, IM_ARRAYSIZE(listbox_items), 4);
  10354. //ImGui::PopItemWidth();
  10355. if (ImGui::TreeNode("Range Widgets"))
  10356. {
  10357. ImGui::Unindent();
  10358. static float begin = 10, end = 90;
  10359. static int begin_i = 100, end_i = 1000;
  10360. ImGui::DragFloatRange2("range", &begin, &end, 0.25f, 0.0f, 100.0f, "Min: %.1f %%", "Max: %.1f %%");
  10361. ImGui::DragIntRange2("range int (no bounds)", &begin_i, &end_i, 5, 0, 0, "Min: %.0f units", "Max: %.0f units");
  10362. ImGui::Indent();
  10363. ImGui::TreePop();
  10364. }
  10365. if (ImGui::TreeNode("Multi-component Widgets"))
  10366. {
  10367. ImGui::Unindent();
  10368. static float vec4f[4] = { 0.10f, 0.20f, 0.30f, 0.44f };
  10369. static int vec4i[4] = { 1, 5, 100, 255 };
  10370. ImGui::InputFloat2("input float2", vec4f);
  10371. ImGui::DragFloat2("drag float2", vec4f, 0.01f, 0.0f, 1.0f);
  10372. ImGui::SliderFloat2("slider float2", vec4f, 0.0f, 1.0f);
  10373. ImGui::DragInt2("drag int2", vec4i, 1, 0, 255);
  10374. ImGui::InputInt2("input int2", vec4i);
  10375. ImGui::SliderInt2("slider int2", vec4i, 0, 255);
  10376. ImGui::Spacing();
  10377. ImGui::InputFloat3("input float3", vec4f);
  10378. ImGui::DragFloat3("drag float3", vec4f, 0.01f, 0.0f, 1.0f);
  10379. ImGui::SliderFloat3("slider float3", vec4f, 0.0f, 1.0f);
  10380. ImGui::DragInt3("drag int3", vec4i, 1, 0, 255);
  10381. ImGui::InputInt3("input int3", vec4i);
  10382. ImGui::SliderInt3("slider int3", vec4i, 0, 255);
  10383. ImGui::Spacing();
  10384. ImGui::InputFloat4("input float4", vec4f);
  10385. ImGui::DragFloat4("drag float4", vec4f, 0.01f, 0.0f, 1.0f);
  10386. ImGui::SliderFloat4("slider float4", vec4f, 0.0f, 1.0f);
  10387. ImGui::InputInt4("input int4", vec4i);
  10388. ImGui::DragInt4("drag int4", vec4i, 1, 0, 255);
  10389. ImGui::SliderInt4("slider int4", vec4i, 0, 255);
  10390. ImGui::Indent();
  10391. ImGui::TreePop();
  10392. }
  10393. if (ImGui::TreeNode("Vertical Sliders"))
  10394. {
  10395. ImGui::Unindent();
  10396. const float spacing = 4;
  10397. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(spacing, spacing));
  10398. static int int_value = 0;
  10399. ImGui::VSliderInt("##int", ImVec2(18,160), &int_value, 0, 5);
  10400. ImGui::SameLine();
  10401. static float values[7] = { 0.0f, 0.60f, 0.35f, 0.9f, 0.70f, 0.20f, 0.0f };
  10402. ImGui::PushID("set1");
  10403. for (int i = 0; i < 7; i++)
  10404. {
  10405. if (i > 0) ImGui::SameLine();
  10406. ImGui::PushID(i);
  10407. ImGui::PushStyleColor(ImGuiCol_FrameBg, ImColor::HSV(i/7.0f, 0.5f, 0.5f));
  10408. ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, ImColor::HSV(i/7.0f, 0.6f, 0.5f));
  10409. ImGui::PushStyleColor(ImGuiCol_FrameBgActive, ImColor::HSV(i/7.0f, 0.7f, 0.5f));
  10410. ImGui::PushStyleColor(ImGuiCol_SliderGrab, ImColor::HSV(i/7.0f, 0.9f, 0.9f));
  10411. ImGui::VSliderFloat("##v", ImVec2(18,160), &values[i], 0.0f, 1.0f, "");
  10412. if (ImGui::IsItemActive() || ImGui::IsItemHovered())
  10413. ImGui::SetTooltip("%.3f", values[i]);
  10414. ImGui::PopStyleColor(4);
  10415. ImGui::PopID();
  10416. }
  10417. ImGui::PopID();
  10418. ImGui::SameLine();
  10419. ImGui::PushID("set2");
  10420. static float values2[4] = { 0.20f, 0.80f, 0.40f, 0.25f };
  10421. const int rows = 3;
  10422. const ImVec2 small_slider_size(18, (160.0f-(rows-1)*spacing)/rows);
  10423. for (int nx = 0; nx < 4; nx++)
  10424. {
  10425. if (nx > 0) ImGui::SameLine();
  10426. ImGui::BeginGroup();
  10427. for (int ny = 0; ny < rows; ny++)
  10428. {
  10429. ImGui::PushID(nx*rows+ny);
  10430. ImGui::VSliderFloat("##v", small_slider_size, &values2[nx], 0.0f, 1.0f, "");
  10431. if (ImGui::IsItemActive() || ImGui::IsItemHovered())
  10432. ImGui::SetTooltip("%.3f", values2[nx]);
  10433. ImGui::PopID();
  10434. }
  10435. ImGui::EndGroup();
  10436. }
  10437. ImGui::PopID();
  10438. ImGui::SameLine();
  10439. ImGui::PushID("set3");
  10440. for (int i = 0; i < 4; i++)
  10441. {
  10442. if (i > 0) ImGui::SameLine();
  10443. ImGui::PushID(i);
  10444. ImGui::PushStyleVar(ImGuiStyleVar_GrabMinSize, 40);
  10445. ImGui::VSliderFloat("##v", ImVec2(40,160), &values[i], 0.0f, 1.0f, "%.2f");
  10446. ImGui::PopStyleVar();
  10447. ImGui::PopID();
  10448. }
  10449. ImGui::PopID();
  10450. ImGui::PopStyleVar();
  10451. ImGui::Indent();
  10452. ImGui::TreePop();
  10453. }
  10454. }
  10455. if (ImGui::CollapsingHeader("Graphs widgets"))
  10456. {
  10457. static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f };
  10458. ImGui::PlotLines("Frame Times", arr, IM_ARRAYSIZE(arr));
  10459. static bool pause;
  10460. static ImVector<float> values; if (values.empty()) { values.resize(90); memset(values.Data, 0, values.Size*sizeof(float)); }
  10461. static int values_offset = 0;
  10462. if (!pause)
  10463. {
  10464. static float refresh_time = ImGui::GetTime(); // Create dummy data at fixed 60 hz rate for the demo
  10465. for (; ImGui::GetTime() > refresh_time + 1.0f/60.0f; refresh_time += 1.0f/60.0f)
  10466. {
  10467. static float phase = 0.0f;
  10468. values[values_offset] = cosf(phase);
  10469. values_offset = (values_offset+1)%values.Size;
  10470. phase += 0.10f*values_offset;
  10471. }
  10472. }
  10473. ImGui::PlotLines("##Graph", values.Data, values.Size, values_offset, "avg 0.0", -1.0f, 1.0f, ImVec2(0,80));
  10474. ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
  10475. ImGui::BeginGroup();
  10476. ImGui::Text("Graph");
  10477. ImGui::Checkbox("pause", &pause);
  10478. ImGui::EndGroup();
  10479. ImGui::PlotHistogram("Histogram", arr, IM_ARRAYSIZE(arr), 0, NULL, 0.0f, 1.0f, ImVec2(0,80));
  10480. }
  10481. if (ImGui::CollapsingHeader("Layout"))
  10482. {
  10483. if (ImGui::TreeNode("Child regions"))
  10484. {
  10485. ImGui::Text("Without border");
  10486. static int line = 50;
  10487. bool goto_line = ImGui::Button("Goto");
  10488. ImGui::SameLine();
  10489. ImGui::PushItemWidth(100);
  10490. goto_line |= ImGui::InputInt("##Line", &line, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
  10491. ImGui::PopItemWidth();
  10492. ImGui::BeginChild("Sub1", ImVec2(ImGui::GetWindowWidth() * 0.5f,300));
  10493. for (int i = 0; i < 100; i++)
  10494. {
  10495. ImGui::Text("%04d: scrollable region", i);
  10496. if (goto_line && line == i)
  10497. ImGui::SetScrollHere();
  10498. }
  10499. if (goto_line && line >= 100)
  10500. ImGui::SetScrollHere();
  10501. ImGui::EndChild();
  10502. ImGui::SameLine();
  10503. ImGui::PushStyleVar(ImGuiStyleVar_ChildWindowRounding, 5.0f);
  10504. ImGui::BeginChild("Sub2", ImVec2(0,300), true);
  10505. ImGui::Text("With border");
  10506. ImGui::Columns(2);
  10507. for (int i = 0; i < 100; i++)
  10508. {
  10509. if (i == 50)
  10510. ImGui::NextColumn();
  10511. char buf[32];
  10512. sprintf(buf, "%08x", i*5731);
  10513. ImGui::Button(buf, ImVec2(ImGui::GetContentRegionMax().x - ImGui::GetCursorPosX(), 0.0f));
  10514. }
  10515. ImGui::EndChild();
  10516. ImGui::PopStyleVar();
  10517. ImGui::TreePop();
  10518. }
  10519. if (ImGui::TreeNode("Widgets Alignment"))
  10520. {
  10521. static float f = 0.0f;
  10522. ImGui::Text("Fixed: 100 pixels");
  10523. ImGui::PushItemWidth(100);
  10524. ImGui::InputFloat("float##1", &f);
  10525. ImGui::PopItemWidth();
  10526. ImGui::Text("Proportional: 50%% of window width");
  10527. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  10528. ImGui::InputFloat("float##2", &f);
  10529. ImGui::PopItemWidth();
  10530. ImGui::Text("Right-aligned: Leave 100 pixels for label");
  10531. ImGui::PushItemWidth(-100);
  10532. ImGui::InputFloat("float##3", &f);
  10533. ImGui::PopItemWidth();
  10534. ImGui::TreePop();
  10535. }
  10536. if (ImGui::TreeNode("Basic Horizontal Layout"))
  10537. {
  10538. ImGui::TextWrapped("(Use ImGui::SameLine() to keep adding items to the right of the preceeding item)");
  10539. // Text
  10540. ImGui::Text("Two items: Hello"); ImGui::SameLine();
  10541. ImGui::TextColored(ImVec4(1,1,0,1), "Sailor");
  10542. // Adjust spacing
  10543. ImGui::Text("More spacing: Hello"); ImGui::SameLine(0, 20);
  10544. ImGui::TextColored(ImVec4(1,1,0,1), "Sailor");
  10545. // Button
  10546. ImGui::AlignFirstTextHeightToWidgets();
  10547. ImGui::Text("Normal buttons"); ImGui::SameLine();
  10548. ImGui::Button("Banana"); ImGui::SameLine();
  10549. ImGui::Button("Apple"); ImGui::SameLine();
  10550. ImGui::Button("Corniflower");
  10551. // Button
  10552. ImGui::Text("Small buttons"); ImGui::SameLine();
  10553. ImGui::SmallButton("Like this one"); ImGui::SameLine();
  10554. ImGui::Text("can fit within a text block.");
  10555. // Aligned to arbitrary position. Easy/cheap column.
  10556. ImGui::Text("Aligned");
  10557. ImGui::SameLine(150); ImGui::Text("x=150");
  10558. ImGui::SameLine(300); ImGui::Text("x=300");
  10559. ImGui::Text("Aligned");
  10560. ImGui::SameLine(150); ImGui::SmallButton("x=150");
  10561. ImGui::SameLine(300); ImGui::SmallButton("x=300");
  10562. // Checkbox
  10563. static bool c1=false,c2=false,c3=false,c4=false;
  10564. ImGui::Checkbox("My", &c1); ImGui::SameLine();
  10565. ImGui::Checkbox("Tailor", &c2); ImGui::SameLine();
  10566. ImGui::Checkbox("Is", &c3); ImGui::SameLine();
  10567. ImGui::Checkbox("Rich", &c4);
  10568. // Various
  10569. static float f0=1.0f, f1=2.0f, f2=3.0f;
  10570. ImGui::PushItemWidth(80);
  10571. const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD" };
  10572. static int item = -1;
  10573. ImGui::Combo("Combo", &item, items, IM_ARRAYSIZE(items)); ImGui::SameLine();
  10574. ImGui::SliderFloat("X", &f0, 0.0f,5.0f); ImGui::SameLine();
  10575. ImGui::SliderFloat("Y", &f1, 0.0f,5.0f); ImGui::SameLine();
  10576. ImGui::SliderFloat("Z", &f2, 0.0f,5.0f);
  10577. ImGui::PopItemWidth();
  10578. ImGui::PushItemWidth(80);
  10579. ImGui::Text("Lists:");
  10580. static int selection[4] = { 0, 1, 2, 3 };
  10581. for (int i = 0; i < 4; i++)
  10582. {
  10583. if (i > 0) ImGui::SameLine();
  10584. ImGui::PushID(i);
  10585. ImGui::ListBox("", &selection[i], items, IM_ARRAYSIZE(items));
  10586. ImGui::PopID();
  10587. //if (ImGui::IsItemHovered()) ImGui::SetTooltip("ListBox %d hovered", i);
  10588. }
  10589. ImGui::PopItemWidth();
  10590. // Dummy
  10591. ImVec2 sz(30,30);
  10592. ImGui::Button("A", sz); ImGui::SameLine();
  10593. ImGui::Dummy(sz); ImGui::SameLine();
  10594. ImGui::Button("B", sz);
  10595. ImGui::TreePop();
  10596. }
  10597. if (ImGui::TreeNode("Groups"))
  10598. {
  10599. ImGui::TextWrapped("(Using ImGui::BeginGroup()/EndGroup() to layout items)");
  10600. ImGui::BeginGroup();
  10601. {
  10602. ImGui::BeginGroup();
  10603. ImGui::Button("AAA");
  10604. ImGui::SameLine();
  10605. ImGui::Button("BBB");
  10606. ImGui::SameLine();
  10607. ImGui::BeginGroup();
  10608. ImGui::Button("CCC");
  10609. ImGui::Button("DDD");
  10610. ImGui::EndGroup();
  10611. if (ImGui::IsItemHovered())
  10612. ImGui::SetTooltip("Group hovered");
  10613. ImGui::SameLine();
  10614. ImGui::Button("EEE");
  10615. ImGui::EndGroup();
  10616. }
  10617. // Capture the group size and create widgets using the same size
  10618. ImVec2 size = ImGui::GetItemRectSize();
  10619. const float values[5] = { 0.5f, 0.20f, 0.80f, 0.60f, 0.25f };
  10620. ImGui::PlotHistogram("##values", values, IM_ARRAYSIZE(values), 0, NULL, 0.0f, 1.0f, size);
  10621. ImGui::Button("ACTION", ImVec2((size.x - ImGui::GetStyle().ItemSpacing.x)*0.5f,size.y));
  10622. ImGui::SameLine();
  10623. ImGui::Button("REACTION", ImVec2((size.x - ImGui::GetStyle().ItemSpacing.x)*0.5f,size.y));
  10624. ImGui::EndGroup();
  10625. ImGui::SameLine();
  10626. ImGui::Button("LEVERAGE\nBUZZWORD", size);
  10627. ImGui::SameLine();
  10628. ImGui::ListBoxHeader("List", size);
  10629. ImGui::Selectable("Selected", true);
  10630. ImGui::Selectable("Not Selected", false);
  10631. ImGui::ListBoxFooter();
  10632. ImGui::TreePop();
  10633. }
  10634. if (ImGui::TreeNode("Text Baseline Alignment"))
  10635. {
  10636. ImGui::TextWrapped("(This is testing the vertical alignment that occurs on text to keep it at the same baseline as widgets. Lines only composed of text or \"small\" widgets fit in less vertical spaces than lines with normal widgets)");
  10637. ImGui::Text("One\nTwo\nThree"); ImGui::SameLine();
  10638. ImGui::Text("Hello\nWorld"); ImGui::SameLine();
  10639. ImGui::Text("Banana");
  10640. ImGui::Text("Banana"); ImGui::SameLine();
  10641. ImGui::Text("Hello\nWorld"); ImGui::SameLine();
  10642. ImGui::Text("One\nTwo\nThree");
  10643. ImGui::Button("HOP"); ImGui::SameLine();
  10644. ImGui::Text("Banana"); ImGui::SameLine();
  10645. ImGui::Text("Hello\nWorld"); ImGui::SameLine();
  10646. ImGui::Text("Banana");
  10647. ImGui::Button("HOP"); ImGui::SameLine();
  10648. ImGui::Text("Hello\nWorld"); ImGui::SameLine();
  10649. ImGui::Text("Banana");
  10650. ImGui::Button("TEST"); ImGui::SameLine();
  10651. ImGui::Text("TEST"); ImGui::SameLine();
  10652. ImGui::SmallButton("TEST");
  10653. ImGui::AlignFirstTextHeightToWidgets(); // If your line starts with text, call this to align it to upcoming widgets.
  10654. ImGui::Text("Text aligned to Widget"); ImGui::SameLine();
  10655. ImGui::Button("Widget"); ImGui::SameLine();
  10656. ImGui::Text("Widget"); ImGui::SameLine();
  10657. ImGui::SmallButton("Widget");
  10658. ImGui::TreePop();
  10659. }
  10660. if (ImGui::TreeNode("Scrolling"))
  10661. {
  10662. ImGui::TextWrapped("Use SetScrollHere() or SetScrollFromPosY() to scroll to a given position.");
  10663. static bool track = true;
  10664. static int track_line = 50, scroll_to_px = 200;
  10665. ImGui::Checkbox("Track", &track);
  10666. ImGui::SameLine(130); track |= ImGui::DragInt("##line", &track_line, 0.25f, 0, 9999, "Line %.0f");
  10667. bool scroll_to = ImGui::Button("Scroll To");
  10668. ImGui::SameLine(130); scroll_to |= ImGui::DragInt("##pos_y", &scroll_to_px, 1.00f, 0, 9999, "y = %.0f px");
  10669. if (scroll_to) track = false;
  10670. for (int i = 0; i < 5; i++)
  10671. {
  10672. if (i > 0) ImGui::SameLine();
  10673. ImGui::BeginGroup();
  10674. ImGui::Text("%s", i == 0 ? "Top" : i == 1 ? "25%" : i == 2 ? "Center" : i == 3 ? "75%" : "Bottom");
  10675. ImGui::BeginChild(ImGui::GetID((void *)(intptr_t)i), ImVec2(ImGui::GetWindowWidth() * 0.17f, 200.0f), true);
  10676. if (scroll_to)
  10677. ImGui::SetScrollFromPosY(ImGui::GetCursorStartPos().y + scroll_to_px, i * 0.25f);
  10678. for (int line = 0; line < 100; line++)
  10679. {
  10680. if (track && line == track_line)
  10681. {
  10682. ImGui::TextColored(ImColor(255,255,0), "Line %d", line);
  10683. ImGui::SetScrollHere(i * 0.25f); // 0.0f:top, 0.5f:center, 1.0f:bottom
  10684. }
  10685. else
  10686. {
  10687. ImGui::Text("Line %d", line);
  10688. }
  10689. }
  10690. ImGui::EndChild();
  10691. ImGui::EndGroup();
  10692. }
  10693. ImGui::TreePop();
  10694. }
  10695. if (ImGui::TreeNode("Clipping"))
  10696. {
  10697. static ImVec2 size(100, 100), offset(50, 20);
  10698. ImGui::TextWrapped("On a per-widget basis we are occasionally clipping text CPU-side if it won't fit in its frame. Otherwise we are doing coarser clipping + passing a scissor rectangle to the renderer. The system is designed to try minimizing both execution and CPU/GPU rendering cost.");
  10699. ImGui::DragFloat2("size", (float*)&size, 0.5f, 0.0f, 200.0f, "%.0f");
  10700. ImGui::DragFloat2("offset", (float*)&offset, 0.5f, -200, 200.0f, "%.0f");
  10701. ImVec2 pos = ImGui::GetCursorScreenPos();
  10702. ImVec4 clip_rect(pos.x, pos.y, pos.x+size.x, pos.y+size.y);
  10703. ImGui::GetWindowDrawList()->AddRectFilled(pos, ImVec2(pos.x+size.x,pos.y+size.y), ImColor(90,90,120,255));
  10704. ImGui::GetWindowDrawList()->AddText(ImGui::GetWindowFont(), ImGui::GetWindowFontSize()*2.0f, ImVec2(pos.x+offset.x,pos.y+offset.y), ImColor(255,255,255,255), "Line 1 hello\nLine 2 clip me!", NULL, 0.0f, &clip_rect);
  10705. ImGui::Dummy(size);
  10706. ImGui::TreePop();
  10707. }
  10708. }
  10709. if (ImGui::CollapsingHeader("Popups & Modal windows"))
  10710. {
  10711. if (ImGui::TreeNode("Popups"))
  10712. {
  10713. ImGui::TextWrapped("When a popup is active, it inhibits interacting with windows that are behind the popup. Clicking outside the popup closes it.");
  10714. static int selected_fish = -1;
  10715. const char* names[] = { "Bream", "Haddock", "Mackerel", "Pollock", "Tilefish" };
  10716. static bool toggles[] = { true, false, false, false, false };
  10717. if (ImGui::Button("Select.."))
  10718. ImGui::OpenPopup("select");
  10719. ImGui::SameLine();
  10720. ImGui::Text(selected_fish == -1 ? "<None>" : names[selected_fish]);
  10721. if (ImGui::BeginPopup("select"))
  10722. {
  10723. ImGui::Text("Aquarium");
  10724. ImGui::Separator();
  10725. for (int i = 0; i < IM_ARRAYSIZE(names); i++)
  10726. if (ImGui::Selectable(names[i]))
  10727. selected_fish = i;
  10728. ImGui::EndPopup();
  10729. }
  10730. if (ImGui::Button("Toggle.."))
  10731. ImGui::OpenPopup("toggle");
  10732. if (ImGui::BeginPopup("toggle"))
  10733. {
  10734. for (int i = 0; i < IM_ARRAYSIZE(names); i++)
  10735. ImGui::MenuItem(names[i], "", &toggles[i]);
  10736. if (ImGui::BeginMenu("Sub-menu"))
  10737. {
  10738. ImGui::MenuItem("Click me");
  10739. ImGui::EndMenu();
  10740. }
  10741. ImGui::Separator();
  10742. ImGui::Text("Tooltip here");
  10743. if (ImGui::IsItemHovered())
  10744. ImGui::SetTooltip("I am a tooltip over a popup");
  10745. if (ImGui::Button("Stacked Popup"))
  10746. ImGui::OpenPopup("another popup");
  10747. if (ImGui::BeginPopup("another popup"))
  10748. {
  10749. for (int i = 0; i < IM_ARRAYSIZE(names); i++)
  10750. ImGui::MenuItem(names[i], "", &toggles[i]);
  10751. if (ImGui::BeginMenu("Sub-menu"))
  10752. {
  10753. ImGui::MenuItem("Click me");
  10754. ImGui::EndMenu();
  10755. }
  10756. ImGui::EndPopup();
  10757. }
  10758. ImGui::EndPopup();
  10759. }
  10760. if (ImGui::Button("Popup Menu.."))
  10761. ImGui::OpenPopup("popup from button");
  10762. if (ImGui::BeginPopup("popup from button"))
  10763. {
  10764. ShowExampleMenuFile();
  10765. ImGui::EndPopup();
  10766. }
  10767. ImGui::TextWrapped("Below we are testing adding menu items to a regular window. It's rather unusual but should work!");
  10768. ImGui::Separator();
  10769. ImGui::MenuItem("Menu item", "CTRL+M");
  10770. if (ImGui::BeginMenu("Menu"))
  10771. {
  10772. ShowExampleMenuFile();
  10773. ImGui::EndMenu();
  10774. }
  10775. ImGui::Separator();
  10776. ImGui::TreePop();
  10777. }
  10778. if (ImGui::TreeNode("Context menus"))
  10779. {
  10780. static float value = 0.5f;
  10781. ImGui::Text("Value = %.3f (<-- right-click here)", value);
  10782. if (ImGui::BeginPopupContextItem("item context menu"))
  10783. {
  10784. if (ImGui::Selectable("Set to zero")) value = 0.0f;
  10785. if (ImGui::Selectable("Set to PI")) value = 3.1415f;
  10786. ImGui::EndPopup();
  10787. }
  10788. static ImVec4 color = ImColor(1.0f, 0.0f, 1.0f, 1.0f);
  10789. ImGui::ColorButton(color);
  10790. if (ImGui::BeginPopupContextItem("color context menu"))
  10791. {
  10792. ImGui::Text("Edit color");
  10793. ImGui::ColorEdit3("##edit", (float*)&color);
  10794. if (ImGui::Button("Close"))
  10795. ImGui::CloseCurrentPopup();
  10796. ImGui::EndPopup();
  10797. }
  10798. ImGui::SameLine(); ImGui::Text("(<-- right-click here)");
  10799. ImGui::TreePop();
  10800. }
  10801. if (ImGui::TreeNode("Modals"))
  10802. {
  10803. ImGui::TextWrapped("Modal windows are like popups but the user cannot close them by clicking outside the window.");
  10804. if (ImGui::Button("Delete.."))
  10805. ImGui::OpenPopup("Delete?");
  10806. if (ImGui::BeginPopupModal("Delete?", NULL, ImGuiWindowFlags_AlwaysAutoResize))
  10807. {
  10808. ImGui::Text("All those beautiful files will be deleted.\nThis operation cannot be undone!\n\n");
  10809. ImGui::Separator();
  10810. static bool dont_ask_me_next_time = false;
  10811. ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0,0));
  10812. ImGui::Checkbox("Don't ask me next time", &dont_ask_me_next_time);
  10813. ImGui::PopStyleVar();
  10814. if (ImGui::Button("OK", ImVec2(120,0))) { ImGui::CloseCurrentPopup(); }
  10815. ImGui::SameLine();
  10816. if (ImGui::Button("Cancel", ImVec2(120,0))) { ImGui::CloseCurrentPopup(); }
  10817. ImGui::EndPopup();
  10818. }
  10819. if (ImGui::Button("Stacked modals.."))
  10820. ImGui::OpenPopup("Stacked 1");
  10821. if (ImGui::BeginPopupModal("Stacked 1"))
  10822. {
  10823. ImGui::Text("Hello from Stacked The First");
  10824. if (ImGui::Button("Another one.."))
  10825. ImGui::OpenPopup("Stacked 2");
  10826. if (ImGui::BeginPopupModal("Stacked 2"))
  10827. {
  10828. ImGui::Text("Hello from Stacked The Second");
  10829. if (ImGui::Button("Close"))
  10830. ImGui::CloseCurrentPopup();
  10831. ImGui::EndPopup();
  10832. }
  10833. if (ImGui::Button("Close"))
  10834. ImGui::CloseCurrentPopup();
  10835. ImGui::EndPopup();
  10836. }
  10837. ImGui::TreePop();
  10838. }
  10839. }
  10840. if (ImGui::CollapsingHeader("Columns"))
  10841. {
  10842. // Basic columns
  10843. ImGui::Text("Basic:");
  10844. ImGui::Columns(4, "mycolumns");
  10845. ImGui::Separator();
  10846. ImGui::Text("ID"); ImGui::NextColumn();
  10847. ImGui::Text("Name"); ImGui::NextColumn();
  10848. ImGui::Text("Path"); ImGui::NextColumn();
  10849. ImGui::Text("Flags"); ImGui::NextColumn();
  10850. ImGui::Separator();
  10851. const char* names[3] = { "One", "Two", "Three" };
  10852. const char* paths[3] = { "/path/one", "/path/two", "/path/three" };
  10853. static int selected = -1;
  10854. for (int i = 0; i < 3; i++)
  10855. {
  10856. char label[32];
  10857. sprintf(label, "%04d", i);
  10858. if (ImGui::Selectable(label, selected == i, ImGuiSelectableFlags_SpanAllColumns))
  10859. selected = i;
  10860. ImGui::NextColumn();
  10861. ImGui::Text(names[i]); ImGui::NextColumn();
  10862. ImGui::Text(paths[i]); ImGui::NextColumn();
  10863. ImGui::Text("...."); ImGui::NextColumn();
  10864. }
  10865. ImGui::Columns(1);
  10866. ImGui::Separator();
  10867. ImGui::Spacing();
  10868. // Scrolling columns
  10869. /*
  10870. ImGui::Text("Scrolling:");
  10871. ImGui::BeginChild("##header", ImVec2(0, ImGui::GetTextLineHeightWithSpacing()+ImGui::GetStyle().ItemSpacing.y));
  10872. ImGui::Columns(3);
  10873. ImGui::Text("ID"); ImGui::NextColumn();
  10874. ImGui::Text("Name"); ImGui::NextColumn();
  10875. ImGui::Text("Path"); ImGui::NextColumn();
  10876. ImGui::Columns(1);
  10877. ImGui::Separator();
  10878. ImGui::EndChild();
  10879. ImGui::BeginChild("##scrollingregion", ImVec2(0, 60));
  10880. ImGui::Columns(3);
  10881. for (int i = 0; i < 10; i++)
  10882. {
  10883. ImGui::Text("%04d", i); ImGui::NextColumn();
  10884. ImGui::Text("Foobar"); ImGui::NextColumn();
  10885. ImGui::Text("/path/foobar/%04d/", i); ImGui::NextColumn();
  10886. }
  10887. ImGui::Columns(1);
  10888. ImGui::EndChild();
  10889. ImGui::Separator();
  10890. ImGui::Spacing();
  10891. */
  10892. // Create multiple items in a same cell before switching to next column
  10893. ImGui::Text("Mixed items:");
  10894. ImGui::Columns(3, "mixed");
  10895. ImGui::Separator();
  10896. static int e = 0;
  10897. ImGui::Text("Hello");
  10898. ImGui::Button("Banana");
  10899. ImGui::RadioButton("radio a", &e, 0);
  10900. ImGui::NextColumn();
  10901. ImGui::Text("ImGui");
  10902. ImGui::Button("Apple");
  10903. ImGui::RadioButton("radio b", &e, 1);
  10904. static float foo = 1.0f;
  10905. ImGui::InputFloat("red", &foo, 0.05f, 0, 3);
  10906. ImGui::Text("An extra line here.");
  10907. ImGui::NextColumn();
  10908. ImGui::Text("Sailor");
  10909. ImGui::Button("Corniflower");
  10910. ImGui::RadioButton("radio c", &e, 2);
  10911. static float bar = 1.0f;
  10912. ImGui::InputFloat("blue", &bar, 0.05f, 0, 3);
  10913. ImGui::NextColumn();
  10914. if (ImGui::CollapsingHeader("Category A")) ImGui::Text("Blah blah blah"); ImGui::NextColumn();
  10915. if (ImGui::CollapsingHeader("Category B")) ImGui::Text("Blah blah blah"); ImGui::NextColumn();
  10916. if (ImGui::CollapsingHeader("Category C")) ImGui::Text("Blah blah blah"); ImGui::NextColumn();
  10917. ImGui::Columns(1);
  10918. ImGui::Separator();
  10919. ImGui::Spacing();
  10920. // Tree items
  10921. ImGui::Text("Tree items:");
  10922. ImGui::Columns(2, "tree items");
  10923. ImGui::Separator();
  10924. if (ImGui::TreeNode("Hello")) { ImGui::BulletText("Sailor"); ImGui::TreePop(); } ImGui::NextColumn();
  10925. if (ImGui::TreeNode("Bonjour")) { ImGui::BulletText("Marin"); ImGui::TreePop(); } ImGui::NextColumn();
  10926. ImGui::Columns(1);
  10927. ImGui::Separator();
  10928. ImGui::Spacing();
  10929. // Word-wrapping
  10930. ImGui::Text("Word-wrapping:");
  10931. ImGui::Columns(2, "word-wrapping");
  10932. ImGui::Separator();
  10933. ImGui::TextWrapped("The quick brown fox jumps over the lazy dog.");
  10934. ImGui::Text("Hello Left");
  10935. ImGui::NextColumn();
  10936. ImGui::TextWrapped("The quick brown fox jumps over the lazy dog.");
  10937. ImGui::Text("Hello Right");
  10938. ImGui::Columns(1);
  10939. ImGui::Separator();
  10940. ImGui::Spacing();
  10941. if (ImGui::TreeNode("Inside a tree.."))
  10942. {
  10943. if (ImGui::TreeNode("node 1 (with borders)"))
  10944. {
  10945. ImGui::Columns(4);
  10946. for (int i = 0; i < 8; i++)
  10947. {
  10948. ImGui::Text("%c%c%c", 'a'+i, 'a'+i, 'a'+i);
  10949. ImGui::NextColumn();
  10950. }
  10951. ImGui::Columns(1);
  10952. ImGui::TreePop();
  10953. }
  10954. if (ImGui::TreeNode("node 2 (without borders)"))
  10955. {
  10956. ImGui::Columns(4, NULL, false);
  10957. for (int i = 0; i < 8; i++)
  10958. {
  10959. ImGui::Text("%c%c%c", 'a'+i, 'a'+i, 'a'+i);
  10960. ImGui::NextColumn();
  10961. }
  10962. ImGui::Columns(1);
  10963. ImGui::TreePop();
  10964. }
  10965. ImGui::TreePop();
  10966. }
  10967. }
  10968. if (ImGui::CollapsingHeader("Filtering"))
  10969. {
  10970. static ImGuiTextFilter filter;
  10971. ImGui::Text("Filter usage:\n"
  10972. " \"\" display all lines\n"
  10973. " \"xxx\" display lines containing \"xxx\"\n"
  10974. " \"xxx,yyy\" display lines containing \"xxx\" or \"yyy\"\n"
  10975. " \"-xxx\" hide lines containing \"xxx\"");
  10976. filter.Draw();
  10977. const char* lines[] = { "aaa1.c", "bbb1.c", "ccc1.c", "aaa2.cpp", "bbb2.cpp", "ccc2.cpp", "abc.h", "hello, world" };
  10978. for (int i = 0; i < IM_ARRAYSIZE(lines); i++)
  10979. if (filter.PassFilter(lines[i]))
  10980. ImGui::BulletText("%s", lines[i]);
  10981. }
  10982. if (ImGui::CollapsingHeader("Keyboard, Mouse & Focus"))
  10983. {
  10984. if (ImGui::TreeNode("Tabbing"))
  10985. {
  10986. ImGui::Text("Use TAB/SHIFT+TAB to cycle through keyboard editable fields.");
  10987. static char buf[32] = "dummy";
  10988. ImGui::InputText("1", buf, IM_ARRAYSIZE(buf));
  10989. ImGui::InputText("2", buf, IM_ARRAYSIZE(buf));
  10990. ImGui::InputText("3", buf, IM_ARRAYSIZE(buf));
  10991. ImGui::PushAllowKeyboardFocus(false);
  10992. ImGui::InputText("4 (tab skip)", buf, IM_ARRAYSIZE(buf));
  10993. //ImGui::SameLine(); ShowHelperMarker("Use ImGui::PushAllowKeyboardFocus(bool)\nto disable tabbing through certain widgets.");
  10994. ImGui::PopAllowKeyboardFocus();
  10995. ImGui::InputText("5", buf, IM_ARRAYSIZE(buf));
  10996. ImGui::TreePop();
  10997. }
  10998. if (ImGui::TreeNode("Focus from code"))
  10999. {
  11000. bool focus_1 = ImGui::Button("Focus on 1"); ImGui::SameLine();
  11001. bool focus_2 = ImGui::Button("Focus on 2"); ImGui::SameLine();
  11002. bool focus_3 = ImGui::Button("Focus on 3");
  11003. int has_focus = 0;
  11004. static char buf[128] = "click on a button to set focus";
  11005. if (focus_1) ImGui::SetKeyboardFocusHere();
  11006. ImGui::InputText("1", buf, IM_ARRAYSIZE(buf));
  11007. if (ImGui::IsItemActive()) has_focus = 1;
  11008. if (focus_2) ImGui::SetKeyboardFocusHere();
  11009. ImGui::InputText("2", buf, IM_ARRAYSIZE(buf));
  11010. if (ImGui::IsItemActive()) has_focus = 2;
  11011. ImGui::PushAllowKeyboardFocus(false);
  11012. if (focus_3) ImGui::SetKeyboardFocusHere();
  11013. ImGui::InputText("3 (tab skip)", buf, IM_ARRAYSIZE(buf));
  11014. if (ImGui::IsItemActive()) has_focus = 3;
  11015. ImGui::PopAllowKeyboardFocus();
  11016. if (has_focus)
  11017. ImGui::Text("Item with focus: %d", has_focus);
  11018. else
  11019. ImGui::Text("Item with focus: <none>");
  11020. ImGui::TextWrapped("Cursor & selection are preserved when refocusing last used item in code.");
  11021. ImGui::TreePop();
  11022. }
  11023. if (ImGui::TreeNode("Dragging"))
  11024. {
  11025. ImGui::TextWrapped("You can use ImGui::GetItemActiveDragDelta() to query for the dragged amount on any widget.");
  11026. ImGui::Button("Drag Me");
  11027. if (ImGui::IsItemActive())
  11028. {
  11029. // Draw a line between the button and the mouse cursor
  11030. ImDrawList* draw_list = ImGui::GetWindowDrawList();
  11031. draw_list->PushClipRectFullScreen();
  11032. draw_list->AddLine(ImGui::CalcItemRectClosestPoint(ImGui::GetIO().MousePos, true, -2.0f), ImGui::GetIO().MousePos, ImColor(ImGui::GetStyle().Colors[ImGuiCol_Button]), 4.0f);
  11033. draw_list->PopClipRect();
  11034. ImVec2 value_raw = ImGui::GetMouseDragDelta(0, 0.0f);
  11035. ImVec2 value_with_lock_threshold = ImGui::GetMouseDragDelta(0);
  11036. ImGui::SameLine(); ImGui::Text("Raw (%.1f, %.1f), WithLockThresold (%.1f, %.1f)", value_raw.x, value_raw.y, value_with_lock_threshold.x, value_with_lock_threshold.y);
  11037. }
  11038. ImGui::TreePop();
  11039. }
  11040. if (ImGui::TreeNode("Keyboard & Mouse State"))
  11041. {
  11042. ImGuiIO& io = ImGui::GetIO();
  11043. ImGui::Text("MousePos: (%g, %g)", io.MousePos.x, io.MousePos.y);
  11044. ImGui::Text("Mouse down:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (io.MouseDownDuration[i] >= 0.0f) { ImGui::SameLine(); ImGui::Text("%d (%.02f secs)", i, io.MouseDownDuration[i]); }
  11045. ImGui::Text("Mouse clicked:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseClicked(i)) { ImGui::SameLine(); ImGui::Text("%d", i); }
  11046. ImGui::Text("Mouse released:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseReleased(i)) { ImGui::SameLine(); ImGui::Text("%d", i); }
  11047. ImGui::Text("MouseWheel: %.1f", io.MouseWheel);
  11048. ImGui::Text("Keys down:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (io.KeysDownDuration[i] >= 0.0f) { ImGui::SameLine(); ImGui::Text("%d (%.02f secs)", i, io.KeysDownDuration[i]); }
  11049. ImGui::Text("Keys pressed:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (ImGui::IsKeyPressed(i)) { ImGui::SameLine(); ImGui::Text("%d", i); }
  11050. ImGui::Text("Keys release:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (ImGui::IsKeyReleased(i)) { ImGui::SameLine(); ImGui::Text("%d", i); }
  11051. ImGui::Text("KeyMods: %s%s%s", io.KeyCtrl ? "CTRL " : "", io.KeyShift ? "SHIFT " : "", io.KeyAlt ? "ALT " : "");
  11052. ImGui::Text("WantCaptureMouse: %s", io.WantCaptureMouse ? "true" : "false");
  11053. ImGui::Text("WantCaptureKeyboard: %s", io.WantCaptureKeyboard ? "true" : "false");
  11054. ImGui::Button("Hover me\nto enforce\ninputs capture");
  11055. if (ImGui::IsItemHovered())
  11056. ImGui::CaptureKeyboardFromApp();
  11057. ImGui::TreePop();
  11058. }
  11059. if (ImGui::TreeNode("Mouse cursors"))
  11060. {
  11061. ImGui::TextWrapped("Your application can render a different mouse cursor based on what ImGui::GetMouseCursor() returns. You can also set io.MouseDrawCursor to ask ImGui to render the cursor for you in software.");
  11062. ImGui::Checkbox("io.MouseDrawCursor", &ImGui::GetIO().MouseDrawCursor);
  11063. ImGui::Text("Hover to see mouse cursors:");
  11064. for (int i = 0; i < ImGuiMouseCursor_Count_; i++)
  11065. {
  11066. char label[32];
  11067. sprintf(label, "Mouse cursor %d", i);
  11068. ImGui::Bullet(); ImGui::Selectable(label, false);
  11069. if (ImGui::IsItemHovered())
  11070. ImGui::SetMouseCursor(i);
  11071. }
  11072. ImGui::TreePop();
  11073. }
  11074. }
  11075. ImGui::End();
  11076. }
  11077. static void ShowExampleAppMainMenuBar()
  11078. {
  11079. if (ImGui::BeginMainMenuBar())
  11080. {
  11081. if (ImGui::BeginMenu("File"))
  11082. {
  11083. ShowExampleMenuFile();
  11084. ImGui::EndMenu();
  11085. }
  11086. if (ImGui::BeginMenu("Edit"))
  11087. {
  11088. if (ImGui::MenuItem("Undo", "CTRL+Z")) {}
  11089. if (ImGui::MenuItem("Redo", "CTRL+Y", false, false)) {} // Disabled item
  11090. ImGui::Separator();
  11091. if (ImGui::MenuItem("Cut", "CTRL+X")) {}
  11092. if (ImGui::MenuItem("Copy", "CTRL+C")) {}
  11093. if (ImGui::MenuItem("Paste", "CTRL+V")) {}
  11094. ImGui::EndMenu();
  11095. }
  11096. ImGui::EndMainMenuBar();
  11097. }
  11098. }
  11099. static void ShowExampleMenuFile()
  11100. {
  11101. ImGui::MenuItem("(dummy menu)", NULL, false, false);
  11102. if (ImGui::MenuItem("New")) {}
  11103. if (ImGui::MenuItem("Open", "Ctrl+O")) {}
  11104. if (ImGui::BeginMenu("Open Recent"))
  11105. {
  11106. ImGui::MenuItem("fish_hat.c");
  11107. ImGui::MenuItem("fish_hat.inl");
  11108. ImGui::MenuItem("fish_hat.h");
  11109. if (ImGui::BeginMenu("More.."))
  11110. {
  11111. ImGui::MenuItem("Hello");
  11112. ImGui::MenuItem("Sailor");
  11113. if (ImGui::BeginMenu("Recurse.."))
  11114. {
  11115. ShowExampleMenuFile();
  11116. ImGui::EndMenu();
  11117. }
  11118. ImGui::EndMenu();
  11119. }
  11120. ImGui::EndMenu();
  11121. }
  11122. if (ImGui::MenuItem("Save", "Ctrl+S")) {}
  11123. if (ImGui::MenuItem("Save As..")) {}
  11124. ImGui::Separator();
  11125. if (ImGui::BeginMenu("Options"))
  11126. {
  11127. static bool enabled = true;
  11128. ImGui::MenuItem("Enabled", "", &enabled);
  11129. ImGui::BeginChild("child", ImVec2(0, 60), true);
  11130. for (int i = 0; i < 10; i++)
  11131. ImGui::Text("Scrolling Text %d", i);
  11132. ImGui::EndChild();
  11133. static float f = 0.5f;
  11134. static int n = 0;
  11135. ImGui::SliderFloat("Value", &f, 0.0f, 1.0f);
  11136. ImGui::InputFloat("Input", &f, 0.1f);
  11137. ImGui::Combo("Combo", &n, "Yes\0No\0Maybe\0\0");
  11138. ImGui::EndMenu();
  11139. }
  11140. if (ImGui::BeginMenu("Colors"))
  11141. {
  11142. for (int i = 0; i < ImGuiCol_COUNT; i++)
  11143. ImGui::MenuItem(ImGui::GetStyleColName((ImGuiCol)i));
  11144. ImGui::EndMenu();
  11145. }
  11146. if (ImGui::BeginMenu("Disabled", false)) // Disabled
  11147. {
  11148. IM_ASSERT(0);
  11149. }
  11150. if (ImGui::MenuItem("Checked", NULL, true)) {}
  11151. if (ImGui::MenuItem("Quit", "Alt+F4")) {}
  11152. }
  11153. static void ShowExampleAppAutoResize(bool* opened)
  11154. {
  11155. if (!ImGui::Begin("Example: Auto-resizing window", opened, ImGuiWindowFlags_AlwaysAutoResize))
  11156. {
  11157. ImGui::End();
  11158. return;
  11159. }
  11160. static int lines = 10;
  11161. ImGui::Text("Window will resize every-frame to the size of its content.\nNote that you probably don't want to query the window size to\noutput your content because that would create a feedback loop.");
  11162. ImGui::SliderInt("Number of lines", &lines, 1, 20);
  11163. for (int i = 0; i < lines; i++)
  11164. ImGui::Text("%*sThis is line %d", i*4, "", i); // Pad with space to extend size horizontally
  11165. ImGui::End();
  11166. }
  11167. static void ShowExampleAppFixedOverlay(bool* opened)
  11168. {
  11169. ImGui::SetNextWindowPos(ImVec2(10,10));
  11170. if (!ImGui::Begin("Example: Fixed Overlay", opened, ImVec2(0,0), 0.3f, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoSavedSettings))
  11171. {
  11172. ImGui::End();
  11173. return;
  11174. }
  11175. ImGui::Text("Simple overlay\non the top-left side of the screen.");
  11176. ImGui::Separator();
  11177. ImGui::Text("Mouse Position: (%.1f,%.1f)", ImGui::GetIO().MousePos.x, ImGui::GetIO().MousePos.y);
  11178. ImGui::End();
  11179. }
  11180. static void ShowExampleAppManipulatingWindowTitle(bool* opened)
  11181. {
  11182. (void)opened;
  11183. // By default, Windows are uniquely identified by their title.
  11184. // You can use the "##" and "###" markers to manipulate the display/ID. Read FAQ at the top of this file!
  11185. // Using "##" to display same title but have unique identifier.
  11186. ImGui::SetNextWindowPos(ImVec2(100,100), ImGuiSetCond_FirstUseEver);
  11187. ImGui::Begin("Same title as another window##1");
  11188. ImGui::Text("This is window 1.\nMy title is the same as window 2, but my identifier is unique.");
  11189. ImGui::End();
  11190. ImGui::SetNextWindowPos(ImVec2(100,200), ImGuiSetCond_FirstUseEver);
  11191. ImGui::Begin("Same title as another window##2");
  11192. ImGui::Text("This is window 2.\nMy title is the same as window 1, but my identifier is unique.");
  11193. ImGui::End();
  11194. // Using "###" to display a changing title but keep a static identifier "AnimatedTitle"
  11195. char buf[128];
  11196. sprintf(buf, "Animated title %c %d###AnimatedTitle", "|/-\\"[(int)(ImGui::GetTime()/0.25f)&3], rand());
  11197. ImGui::SetNextWindowPos(ImVec2(100,300), ImGuiSetCond_FirstUseEver);
  11198. ImGui::Begin(buf);
  11199. ImGui::Text("This window has a changing title.");
  11200. ImGui::End();
  11201. }
  11202. static void ShowExampleAppCustomRendering(bool* opened)
  11203. {
  11204. ImGui::SetNextWindowSize(ImVec2(300,350), ImGuiSetCond_FirstUseEver);
  11205. if (!ImGui::Begin("Example: Custom rendering", opened))
  11206. {
  11207. ImGui::End();
  11208. return;
  11209. }
  11210. // Tip: If you do a lot of custom rendering, you probably want to use your own geometrical types and benefit of overloaded operators, etc.
  11211. // Define IM_VEC2_CLASS_EXTRA in imconfig.h to create implicit conversions between your types and ImVec2/ImVec4.
  11212. // ImGui defines overloaded operators but they are internal to imgui.cpp and not exposed outside (to avoid messing with your types)
  11213. // In this example we aren't using the operators.
  11214. static ImVector<ImVec2> points;
  11215. static bool adding_line = false;
  11216. if (ImGui::Button("Clear")) points.clear();
  11217. if (points.Size >= 2) { ImGui::SameLine(); if (ImGui::Button("Undo")) { points.pop_back(); points.pop_back(); } }
  11218. ImGui::Text("Left-click and drag to add lines");
  11219. ImGui::Text("Right-click to undo");
  11220. ImDrawList* draw_list = ImGui::GetWindowDrawList();
  11221. // Here we are using InvisibleButton() as a convenience to 1) advance the cursor and 2) allows us to use IsItemHovered()
  11222. // However you can draw directly and poll mouse/keyboard by yourself. You can manipulate the cursor using GetCursorPos() and SetCursorPos().
  11223. // If you only use the ImDrawList API, you can notify the owner window of its extends by using SetCursorPos(max).
  11224. ImVec2 canvas_pos = ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates!
  11225. ImVec2 canvas_size = ImVec2(ImMax(50.0f,ImGui::GetWindowContentRegionMax().x-ImGui::GetCursorPos().x), ImMax(50.0f,ImGui::GetWindowContentRegionMax().y-ImGui::GetCursorPos().y)); // Resize canvas what's available
  11226. draw_list->AddRectFilledMultiColor(canvas_pos, ImVec2(canvas_pos.x + canvas_size.x, canvas_pos.y + canvas_size.y), ImColor(0,0,0), ImColor(255,0,0), ImColor(255,255,0), ImColor(0,255,0));
  11227. draw_list->AddRect(canvas_pos, ImVec2(canvas_pos.x + canvas_size.x, canvas_pos.y + canvas_size.y), ImColor(255,255,255));
  11228. bool adding_preview = false;
  11229. ImGui::InvisibleButton("canvas", canvas_size);
  11230. if (ImGui::IsItemHovered())
  11231. {
  11232. ImVec2 mouse_pos_in_canvas = ImVec2(ImGui::GetIO().MousePos.x - canvas_pos.x, ImGui::GetIO().MousePos.y - canvas_pos.y);
  11233. if (!adding_line && ImGui::GetIO().MouseClicked[0])
  11234. {
  11235. points.push_back(mouse_pos_in_canvas);
  11236. adding_line = true;
  11237. }
  11238. if (adding_line)
  11239. {
  11240. adding_preview = true;
  11241. points.push_back(mouse_pos_in_canvas);
  11242. if (!ImGui::GetIO().MouseDown[0])
  11243. adding_line = adding_preview = false;
  11244. }
  11245. if (ImGui::GetIO().MouseClicked[1] && !points.empty())
  11246. {
  11247. adding_line = false;
  11248. points.pop_back();
  11249. points.pop_back();
  11250. }
  11251. }
  11252. draw_list->PushClipRect(ImVec4(canvas_pos.x, canvas_pos.y, canvas_pos.x+canvas_size.x, canvas_pos.y+canvas_size.y)); // clip lines within the canvas (if we resize it, etc.)
  11253. for (int i = 0; i < points.Size - 1; i += 2)
  11254. draw_list->AddLine(ImVec2(canvas_pos.x + points[i].x, canvas_pos.y + points[i].y), ImVec2(canvas_pos.x + points[i+1].x, canvas_pos.y + points[i+1].y), 0xFF00FFFF);
  11255. draw_list->PopClipRect();
  11256. if (adding_preview)
  11257. points.pop_back();
  11258. ImGui::End();
  11259. }
  11260. // For the console example, here we are using a more C++ like approach of declaring a class to hold the data and the functions.
  11261. struct ExampleAppConsole
  11262. {
  11263. char InputBuf[256];
  11264. ImVector<char*> Items;
  11265. bool ScrollToBottom;
  11266. ImVector<char*> History;
  11267. int HistoryPos; // -1: new line, 0..History.Size-1 browsing history.
  11268. ImVector<const char*> Commands;
  11269. ExampleAppConsole()
  11270. {
  11271. ClearLog();
  11272. HistoryPos = -1;
  11273. Commands.push_back("HELP");
  11274. Commands.push_back("HISTORY");
  11275. Commands.push_back("CLEAR");
  11276. Commands.push_back("CLASSIFY"); // "classify" is here to provide an example of "C"+[tab] completing to "CL" and displaying matches.
  11277. }
  11278. ~ExampleAppConsole()
  11279. {
  11280. ClearLog();
  11281. for (int i = 0; i < Items.Size; i++)
  11282. free(History[i]);
  11283. }
  11284. void ClearLog()
  11285. {
  11286. for (int i = 0; i < Items.Size; i++)
  11287. free(Items[i]);
  11288. Items.clear();
  11289. ScrollToBottom = true;
  11290. }
  11291. void AddLog(const char* fmt, ...)
  11292. {
  11293. char buf[1024];
  11294. va_list args;
  11295. va_start(args, fmt);
  11296. vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args);
  11297. buf[IM_ARRAYSIZE(buf)-1] = 0;
  11298. va_end(args);
  11299. Items.push_back(strdup(buf));
  11300. ScrollToBottom = true;
  11301. }
  11302. void Run(const char* title, bool* opened)
  11303. {
  11304. ImGui::SetNextWindowSize(ImVec2(520,600), ImGuiSetCond_FirstUseEver);
  11305. if (!ImGui::Begin(title, opened))
  11306. {
  11307. ImGui::End();
  11308. return;
  11309. }
  11310. ImGui::TextWrapped("This example implements a console with basic coloring, completion and history. A more elaborate implementation may want to store entries along with extra data such as timestamp, emitter, etc.");
  11311. ImGui::TextWrapped("Enter 'HELP' for help, press TAB to use text completion.");
  11312. // TODO: display items starting from the bottom
  11313. if (ImGui::SmallButton("Add Dummy Text")) { AddLog("%d some text", Items.Size); AddLog("some more text"); AddLog("display very important message here!"); } ImGui::SameLine();
  11314. if (ImGui::SmallButton("Add Dummy Error")) AddLog("[error] something went wrong"); ImGui::SameLine();
  11315. if (ImGui::SmallButton("Clear")) ClearLog();
  11316. //static float t = 0.0f; if (ImGui::GetTime() - t > 0.02f) { t = ImGui::GetTime(); AddLog("Spam %f", t); }
  11317. ImGui::Separator();
  11318. ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0,0));
  11319. static ImGuiTextFilter filter;
  11320. filter.Draw("Filter (\"incl,-excl\") (\"error\")", 180);
  11321. ImGui::PopStyleVar();
  11322. ImGui::Separator();
  11323. // Display every line as a separate entry so we can change their color or add custom widgets. If you only want raw text you can use ImGui::TextUnformatted(log.begin(), log.end());
  11324. // NB- if you have thousands of entries this approach may be too inefficient. You can seek and display only the lines that are visible - CalcListClipping() is a helper to compute this information.
  11325. // If your items are of variable size you may want to implement code similar to what CalcListClipping() does. Or split your data into fixed height items to allow random-seeking into your list.
  11326. ImGui::BeginChild("ScrollingRegion", ImVec2(0,-ImGui::GetItemsLineHeightWithSpacing()));
  11327. if (ImGui::BeginPopupContextWindow())
  11328. {
  11329. if (ImGui::Selectable("Clear")) ClearLog();
  11330. ImGui::EndPopup();
  11331. }
  11332. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4,1)); // Tighten spacing
  11333. for (int i = 0; i < Items.Size; i++)
  11334. {
  11335. const char* item = Items[i];
  11336. if (!filter.PassFilter(item))
  11337. continue;
  11338. ImVec4 col = ImColor(255,255,255); // A better implementation may store a type per-item. For the sample let's just parse the text.
  11339. if (strstr(item, "[error]")) col = ImColor(255,100,100);
  11340. else if (strncmp(item, "# ", 2) == 0) col = ImColor(255,200,150);
  11341. ImGui::PushStyleColor(ImGuiCol_Text, col);
  11342. ImGui::TextUnformatted(item);
  11343. ImGui::PopStyleColor();
  11344. }
  11345. if (ScrollToBottom)
  11346. ImGui::SetScrollHere();
  11347. ScrollToBottom = false;
  11348. ImGui::PopStyleVar();
  11349. ImGui::EndChild();
  11350. ImGui::Separator();
  11351. // Command-line
  11352. if (ImGui::InputText("Input", InputBuf, IM_ARRAYSIZE(InputBuf), ImGuiInputTextFlags_EnterReturnsTrue|ImGuiInputTextFlags_CallbackCompletion|ImGuiInputTextFlags_CallbackHistory, &TextEditCallbackStub, (void*)this))
  11353. {
  11354. char* input_end = InputBuf+strlen(InputBuf);
  11355. while (input_end > InputBuf && input_end[-1] == ' ') input_end--; *input_end = 0;
  11356. if (InputBuf[0])
  11357. ExecCommand(InputBuf);
  11358. strcpy(InputBuf, "");
  11359. }
  11360. // Demonstrate keeping auto focus on the input box
  11361. if (ImGui::IsItemHovered() || (ImGui::IsRootWindowOrAnyChildFocused() && !ImGui::IsAnyItemActive() && !ImGui::IsMouseClicked(0)))
  11362. ImGui::SetKeyboardFocusHere(-1); // Auto focus
  11363. ImGui::End();
  11364. }
  11365. void ExecCommand(const char* command_line)
  11366. {
  11367. AddLog("# %s\n", command_line);
  11368. // Insert into history. First find match and delete it so it can be pushed to the back. This isn't trying to be smart or optimal.
  11369. HistoryPos = -1;
  11370. for (int i = History.Size-1; i >= 0; i--)
  11371. if (ImStricmp(History[i], command_line) == 0)
  11372. {
  11373. free(History[i]);
  11374. History.erase(History.begin() + i);
  11375. break;
  11376. }
  11377. History.push_back(strdup(command_line));
  11378. // Process command
  11379. if (ImStricmp(command_line, "CLEAR") == 0)
  11380. {
  11381. ClearLog();
  11382. }
  11383. else if (ImStricmp(command_line, "HELP") == 0)
  11384. {
  11385. AddLog("Commands:");
  11386. for (int i = 0; i < Commands.Size; i++)
  11387. AddLog("- %s", Commands[i]);
  11388. }
  11389. else if (ImStricmp(command_line, "HISTORY") == 0)
  11390. {
  11391. for (int i = History.Size >= 10 ? History.Size - 10 : 0; i < History.Size; i++)
  11392. AddLog("%3d: %s\n", i, History[i]);
  11393. }
  11394. else
  11395. {
  11396. AddLog("Unknown command: '%s'\n", command_line);
  11397. }
  11398. }
  11399. static int TextEditCallbackStub(ImGuiTextEditCallbackData* data) // In C++11 you are better off using lambdas for this sort of forwarding callbacks
  11400. {
  11401. ExampleAppConsole* console = (ExampleAppConsole*)data->UserData;
  11402. return console->TextEditCallback(data);
  11403. }
  11404. int TextEditCallback(ImGuiTextEditCallbackData* data)
  11405. {
  11406. //AddLog("cursor: %d, selection: %d-%d", data->CursorPos, data->SelectionStart, data->SelectionEnd);
  11407. switch (data->EventFlag)
  11408. {
  11409. case ImGuiInputTextFlags_CallbackCompletion:
  11410. {
  11411. // Example of TEXT COMPLETION
  11412. // Locate beginning of current word
  11413. const char* word_end = data->Buf + data->CursorPos;
  11414. const char* word_start = word_end;
  11415. while (word_start > data->Buf)
  11416. {
  11417. const char c = word_start[-1];
  11418. if (c == ' ' || c == '\t' || c == ',' || c == ';')
  11419. break;
  11420. word_start--;
  11421. }
  11422. // Build a list of candidates
  11423. ImVector<const char*> candidates;
  11424. for (int i = 0; i < Commands.Size; i++)
  11425. if (ImStrnicmp(Commands[i], word_start, (int)(word_end-word_start)) == 0)
  11426. candidates.push_back(Commands[i]);
  11427. if (candidates.Size == 0)
  11428. {
  11429. // No match
  11430. AddLog("No match for \"%.*s\"!\n", word_end-word_start, word_start);
  11431. }
  11432. else if (candidates.Size == 1)
  11433. {
  11434. // Single match. Delete the beginning of the word and replace it entirely so we've got nice casing
  11435. data->DeleteChars((int)(word_start-data->Buf), (int)(word_end-word_start));
  11436. data->InsertChars(data->CursorPos, candidates[0]);
  11437. data->InsertChars(data->CursorPos, " ");
  11438. }
  11439. else
  11440. {
  11441. // Multiple matches. Complete as much as we can, so inputing "C" will complete to "CL" and display "CLEAR" and "CLASSIFY"
  11442. int match_len = (int)(word_end - word_start);
  11443. for (;;)
  11444. {
  11445. int c = 0;
  11446. bool all_candidates_matches = true;
  11447. for (int i = 0; i < candidates.Size && all_candidates_matches; i++)
  11448. if (i == 0)
  11449. c = toupper(candidates[i][match_len]);
  11450. else if (c != toupper(candidates[i][match_len]))
  11451. all_candidates_matches = false;
  11452. if (!all_candidates_matches)
  11453. break;
  11454. match_len++;
  11455. }
  11456. if (match_len > 0)
  11457. {
  11458. data->DeleteChars((int)(word_start - data->Buf), (int)(word_end-word_start));
  11459. data->InsertChars(data->CursorPos, candidates[0], candidates[0] + match_len);
  11460. }
  11461. // List matches
  11462. AddLog("Possible matches:\n");
  11463. for (int i = 0; i < candidates.Size; i++)
  11464. AddLog("- %s\n", candidates[i]);
  11465. }
  11466. break;
  11467. }
  11468. case ImGuiInputTextFlags_CallbackHistory:
  11469. {
  11470. // Example of HISTORY
  11471. const int prev_history_pos = HistoryPos;
  11472. if (data->EventKey == ImGuiKey_UpArrow)
  11473. {
  11474. if (HistoryPos == -1)
  11475. HistoryPos = History.Size - 1;
  11476. else if (HistoryPos > 0)
  11477. HistoryPos--;
  11478. }
  11479. else if (data->EventKey == ImGuiKey_DownArrow)
  11480. {
  11481. if (HistoryPos != -1)
  11482. if (++HistoryPos >= History.Size)
  11483. HistoryPos = -1;
  11484. }
  11485. // A better implementation would preserve the data on the current input line along with cursor position.
  11486. if (prev_history_pos != HistoryPos)
  11487. {
  11488. snprintf(data->Buf, data->BufSize, "%s", (HistoryPos >= 0) ? History[HistoryPos] : "");
  11489. data->BufDirty = true;
  11490. data->CursorPos = data->SelectionStart = data->SelectionEnd = (int)strlen(data->Buf);
  11491. }
  11492. }
  11493. }
  11494. return 0;
  11495. }
  11496. };
  11497. static void ShowExampleAppConsole(bool* opened)
  11498. {
  11499. static ExampleAppConsole console;
  11500. console.Run("Example: Console", opened);
  11501. }
  11502. static void ShowExampleAppLayout(bool* opened)
  11503. {
  11504. ImGui::SetNextWindowSize(ImVec2(500, 440), ImGuiSetCond_FirstUseEver);
  11505. if (ImGui::Begin("Example: Layout", opened, ImGuiWindowFlags_MenuBar))
  11506. {
  11507. if (ImGui::BeginMenuBar())
  11508. {
  11509. if (ImGui::BeginMenu("File"))
  11510. {
  11511. if (ImGui::MenuItem("Close")) *opened = false;
  11512. ImGui::EndMenu();
  11513. }
  11514. ImGui::EndMenuBar();
  11515. }
  11516. // left
  11517. static int selected = 0;
  11518. ImGui::BeginChild("left pane", ImVec2(150, 0), true);
  11519. for (int i = 0; i < 100; i++)
  11520. {
  11521. char label[128];
  11522. sprintf(label, "MyObject %d", i);
  11523. if (ImGui::Selectable(label, selected == i))
  11524. selected = i;
  11525. }
  11526. ImGui::EndChild();
  11527. ImGui::SameLine();
  11528. // right
  11529. ImGui::BeginGroup();
  11530. ImGui::BeginChild("item view", ImVec2(0, -ImGui::GetItemsLineHeightWithSpacing())); // Leave room for 1 line below us
  11531. ImGui::Text("MyObject: %d", selected);
  11532. ImGui::Separator();
  11533. ImGui::TextWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ");
  11534. ImGui::EndChild();
  11535. ImGui::BeginChild("buttons");
  11536. if (ImGui::Button("Revert")) {}
  11537. ImGui::SameLine();
  11538. if (ImGui::Button("Save")) {}
  11539. ImGui::EndChild();
  11540. ImGui::EndGroup();
  11541. }
  11542. ImGui::End();
  11543. }
  11544. static void ShowExampleAppLongText(bool* opened)
  11545. {
  11546. ImGui::SetNextWindowSize(ImVec2(520,600), ImGuiSetCond_FirstUseEver);
  11547. if (!ImGui::Begin("Example: Long text display", opened))
  11548. {
  11549. ImGui::End();
  11550. return;
  11551. }
  11552. static int test_type = 0;
  11553. static ImGuiTextBuffer log;
  11554. static int lines = 0;
  11555. ImGui::Text("Printing unusually long amount of text.");
  11556. ImGui::Combo("Test type", &test_type, "Single call to TextUnformatted()\0Multiple calls to Text(), clipped manually\0Multiple calls to Text(), not clipped");
  11557. ImGui::Text("Buffer contents: %d lines, %d bytes", lines, log.size());
  11558. if (ImGui::Button("Clear")) { log.clear(); lines = 0; }
  11559. ImGui::SameLine();
  11560. if (ImGui::Button("Add 1000 lines"))
  11561. {
  11562. for (int i = 0; i < 1000; i++)
  11563. log.append("%i The quick brown fox jumps over the lazy dog\n", lines+i);
  11564. lines += 1000;
  11565. }
  11566. ImGui::BeginChild("Log");
  11567. switch (test_type)
  11568. {
  11569. case 0:
  11570. // Single call to TextUnformatted() with a big buffer
  11571. ImGui::TextUnformatted(log.begin(), log.end());
  11572. break;
  11573. case 1:
  11574. // Multiple calls to Text(), manually coarsely clipped - demonstrate how to use the CalcListClipping() helper.
  11575. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0,0));
  11576. int display_start, display_end;
  11577. ImGui::CalcListClipping(lines, ImGui::GetTextLineHeight(), &display_start, &display_end);
  11578. ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (display_start) * ImGui::GetTextLineHeight());
  11579. for (int i = display_start; i < display_end; i++)
  11580. ImGui::Text("%i The quick brown fox jumps over the lazy dog\n", i);
  11581. ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (lines - display_end) * ImGui::GetTextLineHeight());
  11582. ImGui::PopStyleVar();
  11583. break;
  11584. case 2:
  11585. // Multiple calls to Text(), not clipped (slow)
  11586. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0,0));
  11587. for (int i = 0; i < lines; i++)
  11588. ImGui::Text("%i The quick brown fox jumps over the lazy dog\n", i);
  11589. ImGui::PopStyleVar();
  11590. break;
  11591. }
  11592. ImGui::EndChild();
  11593. ImGui::End();
  11594. }
  11595. // End of Sample code
  11596. #else
  11597. void ImGui::ShowTestWindow(bool*) {}
  11598. #endif
  11599. //-----------------------------------------------------------------------------
  11600. // DEFAULT FONT DATA
  11601. //-----------------------------------------------------------------------------
  11602. // Compressed with stb_compress() then converted to a C array.
  11603. // Use the program in extra_fonts/binary_to_compressed_c.cpp to create the array from a TTF file.
  11604. // Decompression from stb.h (public domain) by Sean Barrett https://github.com/nothings/stb/blob/master/stb.h
  11605. //-----------------------------------------------------------------------------
  11606. static unsigned int stb_decompress_length(unsigned char *input)
  11607. {
  11608. return (input[8] << 24) + (input[9] << 16) + (input[10] << 8) + input[11];
  11609. }
  11610. static unsigned char *stb__barrier, *stb__barrier2, *stb__barrier3, *stb__barrier4;
  11611. static unsigned char *stb__dout;
  11612. static void stb__match(unsigned char *data, unsigned int length)
  11613. {
  11614. // INVERSE of memmove... write each byte before copying the next...
  11615. IM_ASSERT (stb__dout + length <= stb__barrier);
  11616. if (stb__dout + length > stb__barrier) { stb__dout += length; return; }
  11617. if (data < stb__barrier4) { stb__dout = stb__barrier+1; return; }
  11618. while (length--) *stb__dout++ = *data++;
  11619. }
  11620. static void stb__lit(unsigned char *data, unsigned int length)
  11621. {
  11622. IM_ASSERT (stb__dout + length <= stb__barrier);
  11623. if (stb__dout + length > stb__barrier) { stb__dout += length; return; }
  11624. if (data < stb__barrier2) { stb__dout = stb__barrier+1; return; }
  11625. memcpy(stb__dout, data, length);
  11626. stb__dout += length;
  11627. }
  11628. #define stb__in2(x) ((i[x] << 8) + i[(x)+1])
  11629. #define stb__in3(x) ((i[x] << 16) + stb__in2((x)+1))
  11630. #define stb__in4(x) ((i[x] << 24) + stb__in3((x)+1))
  11631. static unsigned char *stb_decompress_token(unsigned char *i)
  11632. {
  11633. if (*i >= 0x20) { // use fewer if's for cases that expand small
  11634. if (*i >= 0x80) stb__match(stb__dout-i[1]-1, i[0] - 0x80 + 1), i += 2;
  11635. else if (*i >= 0x40) stb__match(stb__dout-(stb__in2(0) - 0x4000 + 1), i[2]+1), i += 3;
  11636. else /* *i >= 0x20 */ stb__lit(i+1, i[0] - 0x20 + 1), i += 1 + (i[0] - 0x20 + 1);
  11637. } else { // more ifs for cases that expand large, since overhead is amortized
  11638. if (*i >= 0x18) stb__match(stb__dout-(stb__in3(0) - 0x180000 + 1), i[3]+1), i += 4;
  11639. else if (*i >= 0x10) stb__match(stb__dout-(stb__in3(0) - 0x100000 + 1), stb__in2(3)+1), i += 5;
  11640. else if (*i >= 0x08) stb__lit(i+2, stb__in2(0) - 0x0800 + 1), i += 2 + (stb__in2(0) - 0x0800 + 1);
  11641. else if (*i == 0x07) stb__lit(i+3, stb__in2(1) + 1), i += 3 + (stb__in2(1) + 1);
  11642. else if (*i == 0x06) stb__match(stb__dout-(stb__in3(1)+1), i[4]+1), i += 5;
  11643. else if (*i == 0x04) stb__match(stb__dout-(stb__in3(1)+1), stb__in2(4)+1), i += 6;
  11644. }
  11645. return i;
  11646. }
  11647. static unsigned int stb_adler32(unsigned int adler32, unsigned char *buffer, unsigned int buflen)
  11648. {
  11649. const unsigned long ADLER_MOD = 65521;
  11650. unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16;
  11651. unsigned long blocklen, i;
  11652. blocklen = buflen % 5552;
  11653. while (buflen) {
  11654. for (i=0; i + 7 < blocklen; i += 8) {
  11655. s1 += buffer[0], s2 += s1;
  11656. s1 += buffer[1], s2 += s1;
  11657. s1 += buffer[2], s2 += s1;
  11658. s1 += buffer[3], s2 += s1;
  11659. s1 += buffer[4], s2 += s1;
  11660. s1 += buffer[5], s2 += s1;
  11661. s1 += buffer[6], s2 += s1;
  11662. s1 += buffer[7], s2 += s1;
  11663. buffer += 8;
  11664. }
  11665. for (; i < blocklen; ++i)
  11666. s1 += *buffer++, s2 += s1;
  11667. s1 %= ADLER_MOD, s2 %= ADLER_MOD;
  11668. buflen -= blocklen;
  11669. blocklen = 5552;
  11670. }
  11671. return (unsigned int)(s2 << 16) + (unsigned int)s1;
  11672. }
  11673. static unsigned int stb_decompress(unsigned char *output, unsigned char *i, unsigned int length)
  11674. {
  11675. unsigned int olen;
  11676. if (stb__in4(0) != 0x57bC0000) return 0;
  11677. if (stb__in4(4) != 0) return 0; // error! stream is > 4GB
  11678. olen = stb_decompress_length(i);
  11679. stb__barrier2 = i;
  11680. stb__barrier3 = i+length;
  11681. stb__barrier = output + olen;
  11682. stb__barrier4 = output;
  11683. i += 16;
  11684. stb__dout = output;
  11685. for (;;) {
  11686. unsigned char *old_i = i;
  11687. i = stb_decompress_token(i);
  11688. if (i == old_i) {
  11689. if (*i == 0x05 && i[1] == 0xfa) {
  11690. IM_ASSERT(stb__dout == output + olen);
  11691. if (stb__dout != output + olen) return 0;
  11692. if (stb_adler32(1, output, olen) != (unsigned int) stb__in4(2))
  11693. return 0;
  11694. return olen;
  11695. } else {
  11696. IM_ASSERT(0); /* NOTREACHED */
  11697. return 0;
  11698. }
  11699. }
  11700. IM_ASSERT(stb__dout <= output + olen);
  11701. if (stb__dout > output + olen)
  11702. return 0;
  11703. }
  11704. }
  11705. //-----------------------------------------------------------------------------
  11706. // ProggyClean.ttf
  11707. // Copyright (c) 2004, 2005 Tristan Grimmer
  11708. // MIT license (see License.txt in http://www.upperbounds.net/download/ProggyClean.ttf.zip)
  11709. // Download and more information at http://upperbounds.net
  11710. //-----------------------------------------------------------------------------
  11711. // File: 'ProggyClean.ttf' (41208 bytes)
  11712. // Exported using binary_to_compressed_c.cpp
  11713. //-----------------------------------------------------------------------------
  11714. static const char proggy_clean_ttf_compressed_data_base85[11980+1] =
  11715. "7])#######hV0qs'/###[),##/l:$#Q6>##5[n42>c-TH`->>#/e>11NNV=Bv(*:.F?uu#(gRU.o0XGH`$vhLG1hxt9?W`#,5LsCp#-i>.r$<$6pD>Lb';9Crc6tgXmKVeU2cD4Eo3R/"
  11716. "2*>]b(MC;$jPfY.;h^`IWM9<Lh2TlS+f-s$o6Q<BWH`YiU.xfLq$N;$0iR/GX:U(jcW2p/W*q?-qmnUCI;jHSAiFWM.R*kU@C=GH?a9wp8f$e.-4^Qg1)Q-GL(lf(r/7GrRgwV%MS=C#"
  11717. "`8ND>Qo#t'X#(v#Y9w0#1D$CIf;W'#pWUPXOuxXuU(H9M(1<q-UE31#^-V'8IRUo7Qf./L>=Ke$$'5F%)]0^#0X@U.a<r:QLtFsLcL6##lOj)#.Y5<-R&KgLwqJfLgN&;Q?gI^#DY2uL"
  11718. "i@^rMl9t=cWq6##weg>$FBjVQTSDgEKnIS7EM9>ZY9w0#L;>>#Mx&4Mvt//L[MkA#W@lK.N'[0#7RL_&#w+F%HtG9M#XL`N&.,GM4Pg;-<nLENhvx>-VsM.M0rJfLH2eTM`*oJMHRC`N"
  11719. "kfimM2J,W-jXS:)r0wK#@Fge$U>`w'N7G#$#fB#$E^$#:9:hk+eOe--6x)F7*E%?76%^GMHePW-Z5l'&GiF#$956:rS?dA#fiK:)Yr+`&#0j@'DbG&#^$PG.Ll+DNa<XCMKEV*N)LN/N"
  11720. "*b=%Q6pia-Xg8I$<MR&,VdJe$<(7G;Ckl'&hF;;$<_=X(b.RS%%)###MPBuuE1V:v&cX&#2m#(&cV]`k9OhLMbn%s$G2,B$BfD3X*sp5#l,$R#]x_X1xKX%b5U*[r5iMfUo9U`N99hG)"
  11721. "tm+/Us9pG)XPu`<0s-)WTt(gCRxIg(%6sfh=ktMKn3j)<6<b5Sk_/0(^]AaN#(p/L>&VZ>1i%h1S9u5o@YaaW$e+b<TWFn/Z:Oh(Cx2$lNEoN^e)#CFY@@I;BOQ*sRwZtZxRcU7uW6CX"
  11722. "ow0i(?$Q[cjOd[P4d)]>ROPOpxTO7Stwi1::iB1q)C_=dV26J;2,]7op$]uQr@_V7$q^%lQwtuHY]=DX,n3L#0PHDO4f9>dC@O>HBuKPpP*E,N+b3L#lpR/MrTEH.IAQk.a>D[.e;mc."
  11723. "x]Ip.PH^'/aqUO/$1WxLoW0[iLA<QT;5HKD+@qQ'NQ(3_PLhE48R.qAPSwQ0/WK?Z,[x?-J;jQTWA0X@KJ(_Y8N-:/M74:/-ZpKrUss?d#dZq]DAbkU*JqkL+nwX@@47`5>w=4h(9.`G"
  11724. "CRUxHPeR`5Mjol(dUWxZa(>STrPkrJiWx`5U7F#.g*jrohGg`cg:lSTvEY/EV_7H4Q9[Z%cnv;JQYZ5q.l7Zeas:HOIZOB?G<Nald$qs]@]L<J7bR*>gv:[7MI2k).'2($5FNP&EQ(,)"
  11725. "U]W]+fh18.vsai00);D3@4ku5P?DP8aJt+;qUM]=+b'8@;mViBKx0DE[-auGl8:PJ&Dj+M6OC]O^((##]`0i)drT;-7X`=-H3[igUnPG-NZlo.#k@h#=Ork$m>a>$-?Tm$UV(?#P6YY#"
  11726. "'/###xe7q.73rI3*pP/$1>s9)W,JrM7SN]'/4C#v$U`0#V.[0>xQsH$fEmPMgY2u7Kh(G%siIfLSoS+MK2eTM$=5,M8p`A.;_R%#u[K#$x4AG8.kK/HSB==-'Ie/QTtG?-.*^N-4B/ZM"
  11727. "_3YlQC7(p7q)&](`6_c)$/*JL(L-^(]$wIM`dPtOdGA,U3:w2M-0<q-]L_?^)1vw'.,MRsqVr.L;aN&#/EgJ)PBc[-f>+WomX2u7lqM2iEumMTcsF?-aT=Z-97UEnXglEn1K-bnEO`gu"
  11728. "Ft(c%=;Am_Qs@jLooI&NX;]0#j4#F14;gl8-GQpgwhrq8'=l_f-b49'UOqkLu7-##oDY2L(te+Mch&gLYtJ,MEtJfLh'x'M=$CS-ZZ%P]8bZ>#S?YY#%Q&q'3^Fw&?D)UDNrocM3A76/"
  11729. "/oL?#h7gl85[qW/NDOk%16ij;+:1a'iNIdb-ou8.P*w,v5#EI$TWS>Pot-R*H'-SEpA:g)f+O$%%`kA#G=8RMmG1&O`>to8bC]T&$,n.LoO>29sp3dt-52U%VM#q7'DHpg+#Z9%H[K<L"
  11730. "%a2E-grWVM3@2=-k22tL]4$##6We'8UJCKE[d_=%wI;'6X-GsLX4j^SgJ$##R*w,vP3wK#iiW&#*h^D&R?jp7+/u&#(AP##XU8c$fSYW-J95_-Dp[g9wcO&#M-h1OcJlc-*vpw0xUX&#"
  11731. "OQFKNX@QI'IoPp7nb,QU//MQ&ZDkKP)X<WSVL(68uVl&#c'[0#(s1X&xm$Y%B7*K:eDA323j998GXbA#pwMs-jgD$9QISB-A_(aN4xoFM^@C58D0+Q+q3n0#3U1InDjF682-SjMXJK)("
  11732. "h$hxua_K]ul92%'BOU&#BRRh-slg8KDlr:%L71Ka:.A;%YULjDPmL<LYs8i#XwJOYaKPKc1h:'9Ke,g)b),78=I39B;xiY$bgGw-&.Zi9InXDuYa%G*f2Bq7mn9^#p1vv%#(Wi-;/Z5h"
  11733. "o;#2:;%d&#x9v68C5g?ntX0X)pT`;%pB3q7mgGN)3%(P8nTd5L7GeA-GL@+%J3u2:(Yf>et`e;)f#Km8&+DC$I46>#Kr]]u-[=99tts1.qb#q72g1WJO81q+eN'03'eM>&1XxY-caEnO"
  11734. "j%2n8)),?ILR5^.Ibn<-X-Mq7[a82Lq:F&#ce+S9wsCK*x`569E8ew'He]h:sI[2LM$[guka3ZRd6:t%IG:;$%YiJ:Nq=?eAw;/:nnDq0(CYcMpG)qLN4$##&J<j$UpK<Q4a1]MupW^-"
  11735. "sj_$%[HK%'F####QRZJ::Y3EGl4'@%FkiAOg#p[##O`gukTfBHagL<LHw%q&OV0##F=6/:chIm0@eCP8X]:kFI%hl8hgO@RcBhS-@Qb$%+m=hPDLg*%K8ln(wcf3/'DW-$.lR?n[nCH-"
  11736. "eXOONTJlh:.RYF%3'p6sq:UIMA945&^HFS87@$EP2iG<-lCO$%c`uKGD3rC$x0BL8aFn--`ke%#HMP'vh1/R&O_J9'um,.<tx[@%wsJk&bUT2`0uMv7gg#qp/ij.L56'hl;.s5CUrxjO"
  11737. "M7-##.l+Au'A&O:-T72L]P`&=;ctp'XScX*rU.>-XTt,%OVU4)S1+R-#dg0/Nn?Ku1^0f$B*P:Rowwm-`0PKjYDDM'3]d39VZHEl4,.j']Pk-M.h^&:0FACm$maq-&sgw0t7/6(^xtk%"
  11738. "LuH88Fj-ekm>GA#_>568x6(OFRl-IZp`&b,_P'$M<Jnq79VsJW/mWS*PUiq76;]/NM_>hLbxfc$mj`,O;&%W2m`Zh:/)Uetw:aJ%]K9h:TcF]u_-Sj9,VK3M.*'&0D[Ca]J9gp8,kAW]"
  11739. "%(?A%R$f<->Zts'^kn=-^@c4%-pY6qI%J%1IGxfLU9CP8cbPlXv);C=b),<2mOvP8up,UVf3839acAWAW-W?#ao/^#%KYo8fRULNd2.>%m]UK:n%r$'sw]J;5pAoO_#2mO3n,'=H5(et"
  11740. "Hg*`+RLgv>=4U8guD$I%D:W>-r5V*%j*W:Kvej.Lp$<M-SGZ':+Q_k+uvOSLiEo(<aD/K<CCc`'Lx>'?;++O'>()jLR-^u68PHm8ZFWe+ej8h:9r6L*0//c&iH&R8pRbA#Kjm%upV1g:"
  11741. "a_#Ur7FuA#(tRh#.Y5K+@?3<-8m0$PEn;J:rh6?I6uG<-`wMU'ircp0LaE_OtlMb&1#6T.#FDKu#1Lw%u%+GM+X'e?YLfjM[VO0MbuFp7;>Q&#WIo)0@F%q7c#4XAXN-U&VB<HFF*qL("
  11742. "$/V,;(kXZejWO`<[5??ewY(*9=%wDc;,u<'9t3W-(H1th3+G]ucQ]kLs7df($/*JL]@*t7Bu_G3_7mp7<iaQjO@.kLg;x3B0lqp7Hf,^Ze7-##@/c58Mo(3;knp0%)A7?-W+eI'o8)b<"
  11743. "nKnw'Ho8C=Y>pqB>0ie&jhZ[?iLR@@_AvA-iQC(=ksRZRVp7`.=+NpBC%rh&3]R:8XDmE5^V8O(x<<aG/1N$#FX$0V5Y6x'aErI3I$7x%E`v<-BY,)%-?Psf*l?%C3.mM(=/M0:JxG'?"
  11744. "7WhH%o'a<-80g0NBxoO(GH<dM]n.+%q@jH?f.UsJ2Ggs&4<-e47&Kl+f//9@`b+?.TeN_&B8Ss?v;^Trk;f#YvJkl&w$]>-+k?'(<S:68tq*WoDfZu';mM?8X[ma8W%*`-=;D.(nc7/;"
  11745. ")g:T1=^J$&BRV(-lTmNB6xqB[@0*o.erM*<SWF]u2=st-*(6v>^](H.aREZSi,#1:[IXaZFOm<-ui#qUq2$##Ri;u75OK#(RtaW-K-F`S+cF]uN`-KMQ%rP/Xri.LRcB##=YL3BgM/3M"
  11746. "D?@f&1'BW-)Ju<L25gl8uhVm1hL$##*8###'A3/LkKW+(^rWX?5W_8g)a(m&K8P>#bmmWCMkk&#TR`C,5d>g)F;t,4:@_l8G/5h4vUd%&%950:VXD'QdWoY-F$BtUwmfe$YqL'8(PWX("
  11747. "P?^@Po3$##`MSs?DWBZ/S>+4%>fX,VWv/w'KD`LP5IbH;rTV>n3cEK8U#bX]l-/V+^lj3;vlMb&[5YQ8#pekX9JP3XUC72L,,?+Ni&co7ApnO*5NK,((W-i:$,kp'UDAO(G0Sq7MVjJs"
  11748. "bIu)'Z,*[>br5fX^:FPAWr-m2KgL<LUN098kTF&#lvo58=/vjDo;.;)Ka*hLR#/k=rKbxuV`>Q_nN6'8uTG&#1T5g)uLv:873UpTLgH+#FgpH'_o1780Ph8KmxQJ8#H72L4@768@Tm&Q"
  11749. "h4CB/5OvmA&,Q&QbUoi$a_%3M01H)4x7I^&KQVgtFnV+;[Pc>[m4k//,]1?#`VY[Jr*3&&slRfLiVZJ:]?=K3Sw=[$=uRB?3xk48@aeg<Z'<$#4H)6,>e0jT6'N#(q%.O=?2S]u*(m<-"
  11750. "V8J'(1)G][68hW$5'q[GC&5j`TE?m'esFGNRM)j,ffZ?-qx8;->g4t*:CIP/[Qap7/9'#(1sao7w-.qNUdkJ)tCF&#B^;xGvn2r9FEPFFFcL@.iFNkTve$m%#QvQS8U@)2Z+3K:AKM5i"
  11751. "sZ88+dKQ)W6>J%CL<KE>`.d*(B`-n8D9oK<Up]c$X$(,)M8Zt7/[rdkqTgl-0cuGMv'?>-XV1q['-5k'cAZ69e;D_?$ZPP&s^+7])$*$#@QYi9,5P&#9r+$%CE=68>K8r0=dSC%%(@p7"
  11752. ".m7jilQ02'0-VWAg<a/''3u.=4L$Y)6k/K:_[3=&jvL<L0C/2'v:^;-DIBW,B4E68:kZ;%?8(Q8BH=kO65BW?xSG&#@uU,DS*,?.+(o(#1vCS8#CHF>TlGW'b)Tq7VT9q^*^$$.:&N@@"
  11753. "$&)WHtPm*5_rO0&e%K&#-30j(E4#'Zb.o/(Tpm$>K'f@[PvFl,hfINTNU6u'0pao7%XUp9]5.>%h`8_=VYbxuel.NTSsJfLacFu3B'lQSu/m6-Oqem8T+oE--$0a/k]uj9EwsG>%veR*"
  11754. "hv^BFpQj:K'#SJ,sB-'#](j.Lg92rTw-*n%@/;39rrJF,l#qV%OrtBeC6/,;qB3ebNW[?,Hqj2L.1NP&GjUR=1D8QaS3Up&@*9wP?+lo7b?@%'k4`p0Z$22%K3+iCZj?XJN4Nm&+YF]u"
  11755. "@-W$U%VEQ/,,>>#)D<h#`)h0:<Q6909ua+&VU%n2:cG3FJ-%@Bj-DgLr`Hw&HAKjKjseK</xKT*)B,N9X3]krc12t'pgTV(Lv-tL[xg_%=M_q7a^x?7Ubd>#%8cY#YZ?=,`Wdxu/ae&#"
  11756. "w6)R89tI#6@s'(6Bf7a&?S=^ZI_kS&ai`&=tE72L_D,;^R)7[$s<Eh#c&)q.MXI%#v9ROa5FZO%sF7q7Nwb&#ptUJ:aqJe$Sl68%.D###EC><?-aF&#RNQv>o8lKN%5/$(vdfq7+ebA#"
  11757. "u1p]ovUKW&Y%q]'>$1@-[xfn$7ZTp7mM,G,Ko7a&Gu%G[RMxJs[0MM%wci.LFDK)(<c`Q8N)jEIF*+?P2a8g%)$q]o2aH8C&<SibC/q,(e:v;-b#6[$NtDZ84Je2KNvB#$P5?tQ3nt(0"
  11758. "d=j.LQf./Ll33+(;q3L-w=8dX$#WF&uIJ@-bfI>%:_i2B5CsR8&9Z&#=mPEnm0f`<&c)QL5uJ#%u%lJj+D-r;BoF&#4DoS97h5g)E#o:&S4weDF,9^Hoe`h*L+_a*NrLW-1pG_&2UdB8"
  11759. "6e%B/:=>)N4xeW.*wft-;$'58-ESqr<b?UI(_%@[P46>#U`'6AQ]m&6/`Z>#S?YY#Vc;r7U2&326d=w&H####?TZ`*4?&.MK?LP8Vxg>$[QXc%QJv92.(Db*B)gb*BM9dM*hJMAo*c&#"
  11760. "b0v=Pjer]$gG&JXDf->'StvU7505l9$AFvgYRI^&<^b68?j#q9QX4SM'RO#&sL1IM.rJfLUAj221]d##DW=m83u5;'bYx,*Sl0hL(W;;$doB&O/TQ:(Z^xBdLjL<Lni;''X.`$#8+1GD"
  11761. ":k$YUWsbn8ogh6rxZ2Z9]%nd+>V#*8U_72Lh+2Q8Cj0i:6hp&$C/:p(HK>T8Y[gHQ4`4)'$Ab(Nof%V'8hL&#<NEdtg(n'=S1A(Q1/I&4([%dM`,Iu'1:_hL>SfD07&6D<fp8dHM7/g+"
  11762. "tlPN9J*rKaPct&?'uBCem^jn%9_K)<,C5K3s=5g&GmJb*[SYq7K;TRLGCsM-$$;S%:Y@r7AK0pprpL<Lrh,q7e/%KWK:50I^+m'vi`3?%Zp+<-d+$L-Sv:@.o19n$s0&39;kn;S%BSq*"
  11763. "$3WoJSCLweV[aZ'MQIjO<7;X-X;&+dMLvu#^UsGEC9WEc[X(wI7#2.(F0jV*eZf<-Qv3J-c+J5AlrB#$p(H68LvEA'q3n0#m,[`*8Ft)FcYgEud]CWfm68,(aLA$@EFTgLXoBq/UPlp7"
  11764. ":d[/;r_ix=:TF`S5H-b<LI&HY(K=h#)]Lk$K14lVfm:x$H<3^Ql<M`$OhapBnkup'D#L$Pb_`N*g]2e;X/Dtg,bsj&K#2[-:iYr'_wgH)NUIR8a1n#S?Yej'h8^58UbZd+^FKD*T@;6A"
  11765. "7aQC[K8d-(v6GI$x:T<&'Gp5Uf>@M.*J:;$-rv29'M]8qMv-tLp,'886iaC=Hb*YJoKJ,(j%K=H`K.v9HggqBIiZu'QvBT.#=)0ukruV&.)3=(^1`o*Pj4<-<aN((^7('#Z0wK#5GX@7"
  11766. "u][`*S^43933A4rl][`*O4CgLEl]v$1Q3AeF37dbXk,.)vj#x'd`;qgbQR%FW,2(?LO=s%Sc68%NP'##Aotl8x=BE#j1UD([3$M(]UI2LX3RpKN@;/#f'f/&_mt&F)XdF<9t4)Qa.*kT"
  11767. "LwQ'(TTB9.xH'>#MJ+gLq9-##@HuZPN0]u:h7.T..G:;$/Usj(T7`Q8tT72LnYl<-qx8;-HV7Q-&Xdx%1a,hC=0u+HlsV>nuIQL-5<N?)NBS)QN*_I,?&)2'IM%L3I)X((e/dl2&8'<M"
  11768. ":^#M*Q+[T.Xri.LYS3v%fF`68h;b-X[/En'CR.q7E)p'/kle2HM,u;^%OKC-N+Ll%F9CF<Nf'^#t2L,;27W:0O@6##U6W7:$rJfLWHj$#)woqBefIZ.PK<b*t7ed;p*_m;4ExK#h@&]>"
  11769. "_>@kXQtMacfD.m-VAb8;IReM3$wf0''hra*so568'Ip&vRs849'MRYSp%:t:h5qSgwpEr$B>Q,;s(C#$)`svQuF$##-D,##,g68@2[T;.XSdN9Qe)rpt._K-#5wF)sP'##p#C0c%-Gb%"
  11770. "hd+<-j'Ai*x&&HMkT]C'OSl##5RG[JXaHN;d'uA#x._U;.`PU@(Z3dt4r152@:v,'R.Sj'w#0<-;kPI)FfJ&#AYJ&#//)>-k=m=*XnK$>=)72L]0I%>.G690a:$##<,);?;72#?x9+d;"
  11771. "^V'9;jY@;)br#q^YQpx:X#Te$Z^'=-=bGhLf:D6&bNwZ9-ZD#n^9HhLMr5G;']d&6'wYmTFmL<LD)F^%[tC'8;+9E#C$g%#5Y>q9wI>P(9mI[>kC-ekLC/R&CH+s'B;K-M6$EB%is00:"
  11772. "+A4[7xks.LrNk0&E)wILYF@2L'0Nb$+pv<(2.768/FrY&h$^3i&@+G%JT'<-,v`3;_)I9M^AE]CN?Cl2AZg+%4iTpT3<n-&%H%b<FDj2M<hH=&Eh<2Len$b*aTX=-8QxN)k11IM1c^j%"
  11773. "9s<L<NFSo)B?+<-(GxsF,^-Eh@$4dXhN$+#rxK8'je'D7k`e;)2pYwPA'_p9&@^18ml1^[@g4t*[JOa*[=Qp7(qJ_oOL^('7fB&Hq-:sf,sNj8xq^>$U4O]GKx'm9)b@p7YsvK3w^YR-"
  11774. "CdQ*:Ir<($u&)#(&?L9Rg3H)4fiEp^iI9O8KnTj,]H?D*r7'M;PwZ9K0E^k&-cpI;.p/6_vwoFMV<->#%Xi.LxVnrU(4&8/P+:hLSKj$#U%]49t'I:rgMi'FL@a:0Y-uA[39',(vbma*"
  11775. "hU%<-SRF`Tt:542R_VV$p@[p8DV[A,?1839FWdF<TddF<9Ah-6&9tWoDlh]&1SpGMq>Ti1O*H&#(AL8[_P%.M>v^-))qOT*F5Cq0`Ye%+$B6i:7@0IX<N+T+0MlMBPQ*Vj>SsD<U4JHY"
  11776. "8kD2)2fU/M#$e.)T4,_=8hLim[&);?UkK'-x?'(:siIfL<$pFM`i<?%W(mGDHM%>iWP,##P`%/L<eXi:@Z9C.7o=@(pXdAO/NLQ8lPl+HPOQa8wD8=^GlPa8TKI1CjhsCTSLJM'/Wl>-"
  11777. "S(qw%sf/@%#B6;/U7K]uZbi^Oc^2n<bhPmUkMw>%t<)'mEVE''n`WnJra$^TKvX5B>;_aSEK',(hwa0:i4G?.Bci.(X[?b*($,=-n<.Q%`(X=?+@Am*Js0&=3bh8K]mL<LoNs'6,'85`"
  11778. "0?t/'_U59@]ddF<#LdF<eWdF<OuN/45rY<-L@&#+fm>69=Lb,OcZV/);TTm8VI;?%OtJ<(b4mq7M6:u?KRdF<gR@2L=FNU-<b[(9c/ML3m;Z[$oF3g)GAWqpARc=<ROu7cL5l;-[A]%/"
  11779. "+fsd;l#SafT/f*W]0=O'$(Tb<[)*@e775R-:Yob%g*>l*:xP?Yb.5)%w_I?7uk5JC+FS(m#i'k.'a0i)9<7b'fs'59hq$*5Uhv##pi^8+hIEBF`nvo`;'l0.^S1<-wUK2/Coh58KKhLj"
  11780. "M=SO*rfO`+qC`W-On.=AJ56>>i2@2LH6A:&5q`?9I3@@'04&p2/LVa*T-4<-i3;M9UvZd+N7>b*eIwg:CC)c<>nO&#<IGe;__.thjZl<%w(Wk2xmp4Q@I#I9,DF]u7-P=.-_:YJ]aS@V"
  11781. "?6*C()dOp7:WL,b&3Rg/.cmM9&r^>$(>.Z-I&J(Q0Hd5Q%7Co-b`-c<N(6r@ip+AurK<m86QIth*#v;-OBqi+L7wDE-Ir8K['m+DDSLwK&/.?-V%U_%3:qKNu$_b*B-kp7NaD'QdWQPK"
  11782. "Yq[@>P)hI;*_F]u`Rb[.j8_Q/<&>uu+VsH$sM9TA%?)(vmJ80),P7E>)tjD%2L=-t#fK[%`v=Q8<FfNkgg^oIbah*#8/Qt$F&:K*-(N/'+1vMB,u()-a.VUU*#[e%gAAO(S>WlA2);Sa"
  11783. ">gXm8YB`1d@K#n]76-a$U,mF<fX]idqd)<3,]J7JmW4`6]uks=4-72L(jEk+:bJ0M^q-8Dm_Z?0olP1C9Sa&H[d&c$ooQUj]Exd*3ZM@-WGW2%s',B-_M%>%Ul:#/'xoFM9QX-$.QN'>"
  11784. "[%$Z$uF6pA6Ki2O5:8w*vP1<-1`[G,)-m#>0`P&#eb#.3i)rtB61(o'$?X3B</R90;eZ]%Ncq;-Tl]#F>2Qft^ae_5tKL9MUe9b*sLEQ95C&`=G?@Mj=wh*'3E>=-<)Gt*Iw)'QG:`@I"
  11785. "wOf7&]1i'S01B+Ev/Nac#9S;=;YQpg_6U`*kVY39xK,[/6Aj7:'1Bm-_1EYfa1+o&o4hp7KN_Q(OlIo@S%;jVdn0'1<Vc52=u`3^o-n1'g4v58Hj&6_t7$##?M)c<$bgQ_'SY((-xkA#"
  11786. "Y(,p'H9rIVY-b,'%bCPF7.J<Up^,(dU1VY*5#WkTU>h19w,WQhLI)3S#f$2(eb,jr*b;3Vw]*7NH%$c4Vs,eD9>XW8?N]o+(*pgC%/72LV-u<Hp,3@e^9UB1J+ak9-TN/mhKPg+AJYd$"
  11787. "MlvAF_jCK*.O-^(63adMT->W%iewS8W6m2rtCpo'RS1R84=@paTKt)>=%&1[)*vp'u+x,VrwN;&]kuO9JDbg=pO$J*.jVe;u'm0dr9l,<*wMK*Oe=g8lV_KEBFkO'oU]^=[-792#ok,)"
  11788. "i]lR8qQ2oA8wcRCZ^7w/Njh;?.stX?Q1>S1q4Bn$)K1<-rGdO'$Wr.Lc.CG)$/*JL4tNR/,SVO3,aUw'DJN:)Ss;wGn9A32ijw%FL+Z0Fn.U9;reSq)bmI32U==5ALuG&#Vf1398/pVo"
  11789. "1*c-(aY168o<`JsSbk-,1N;$>0:OUas(3:8Z972LSfF8eb=c-;>SPw7.6hn3m`9^Xkn(r.qS[0;T%&Qc=+STRxX'q1BNk3&*eu2;&8q$&x>Q#Q7^Tf+6<(d%ZVmj2bDi%.3L2n+4W'$P"
  11790. "iDDG)g,r%+?,$@?uou5tSe2aN_AQU*<h`e-GI7)?OK2A.d7_c)?wQ5AS@DL3r#7fSkgl6-++D:'A,uq7SvlB$pcpH'q3n0#_%dY#xCpr-l<F0NR@-##FEV6NTF6##$l84N1w?AO>'IAO"
  11791. "URQ##V^Fv-XFbGM7Fl(N<3DhLGF%q.1rC$#:T__&Pi68%0xi_&[qFJ(77j_&JWoF.V735&T,[R*:xFR*K5>>#`bW-?4Ne_&6Ne_&6Ne_&n`kr-#GJcM6X;uM6X;uM(.a..^2TkL%oR(#"
  11792. ";u.T%fAr%4tJ8&><1=GHZ_+m9/#H1F^R#SC#*N=BA9(D?v[UiFY>>^8p,KKF.W]L29uLkLlu/+4T<XoIB&hx=T1PcDaB&;HH+-AFr?(m9HZV)FKS8JCw;SD=6[^/DZUL`EUDf]GGlG&>"
  11793. "w$)F./^n3+rlo+DB;5sIYGNk+i1t-69Jg--0pao7Sm#K)pdHW&;LuDNH@H>#/X-TI(;P>#,Gc>#0Su>#4`1?#8lC?#<xU?#@.i?#D:%@#HF7@#LRI@#P_[@#Tkn@#Xw*A#]-=A#a9OA#"
  11794. "d<F&#*;G##.GY##2Sl##6`($#:l:$#>xL$#B.`$#F:r$#JF.%#NR@%#R_R%#Vke%#Zww%#_-4&#3^Rh%Sflr-k'MS.o?.5/sWel/wpEM0%3'/1)K^f1-d>G21&v(35>V`39V7A4=onx4"
  11795. "A1OY5EI0;6Ibgr6M$HS7Q<)58C5w,;WoA*#[%T*#`1g*#d=#+#hI5+#lUG+#pbY+#tnl+#x$),#&1;,#*=M,#.I`,#2Ur,#6b.-#;w[H#iQtA#m^0B#qjBB#uvTB##-hB#'9$C#+E6C#"
  11796. "/QHC#3^ZC#7jmC#;v)D#?,<D#C8ND#GDaD#KPsD#O]/E#g1A5#KA*1#gC17#MGd;#8(02#L-d3#rWM4#Hga1#,<w0#T.j<#O#'2#CYN1#qa^:#_4m3#o@/=#eG8=#t8J5#`+78#4uI-#"
  11797. "m3B2#SB[8#Q0@8#i[*9#iOn8#1Nm;#^sN9#qh<9#:=x-#P;K2#$%X9#bC+.#Rg;<#mN=.#MTF.#RZO.#2?)4#Y#(/#[)1/#b;L/#dAU/#0Sv;#lY$0#n`-0#sf60#(F24#wrH0#%/e0#"
  11798. "TmD<#%JSMFove:CTBEXI:<eh2g)B,3h2^G3i;#d3jD>)4kMYD4lVu`4m`:&5niUA5@(A5BA1]PBB:xlBCC=2CDLXMCEUtiCf&0g2'tN?PGT4CPGT4CPGT4CPGT4CPGT4CPGT4CPGT4CP"
  11799. "GT4CPGT4CPGT4CPGT4CPGT4CPGT4CP-qekC`.9kEg^+F$kwViFJTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5o,^<-28ZI'O?;xp"
  11800. "O?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xp;7q-#lLYI:xvD=#";
  11801. static const char* GetDefaultCompressedFontDataTTFBase85()
  11802. {
  11803. return proggy_clean_ttf_compressed_data_base85;
  11804. }
  11805. //-----------------------------------------------------------------------------
  11806. //---- Include imgui_user.inl at the end of imgui.cpp
  11807. //---- So you can include code that extends ImGui using its private data/functions.
  11808. #ifdef IMGUI_INCLUDE_IMGUI_USER_INL
  11809. #include "imgui_user.inl"
  11810. #endif
  11811. //-----------------------------------------------------------------------------