imgui.cpp 297 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542
  1. // ImGui library v1.17 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 contributors.
  6. /*
  7. Index
  8. - MISSION STATEMENT
  9. - END-USER GUIDE
  10. - PROGRAMMER GUIDE
  11. - API BREAKING CHANGES
  12. - TROUBLESHOOTING & FREQUENTLY ASKED QUESTIONS
  13. - ISSUES & TODO-LIST
  14. - CODE
  15. - SAMPLE CODE
  16. - FONT DATA
  17. MISSION STATEMENT
  18. =================
  19. - easy to use to create code-driven and data-driven tools
  20. - easy to use to create ad hoc short-lived tools and long-lived, more elaborate tools
  21. - easy to hack and improve
  22. - minimize screen real-estate usage
  23. - minimize setup and maintenance
  24. - minimize state storage on user side
  25. - portable, minimize dependencies, run on target (consoles, etc.)
  26. - 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)
  27. - read about immediate-mode GUI principles @ http://mollyrocket.com/861, http://mollyrocket.com/forums/index.html
  28. Designed for developers and content-creators, not the typical end-user! Some of the weaknesses includes:
  29. - doesn't look fancy, doesn't animate
  30. - limited layout features, intricate layouts are typically crafted in code
  31. - occasionally use statically sized buffers for string manipulations - won't crash, but some long text may be clipped
  32. END-USER GUIDE
  33. ==============
  34. - double-click title bar to collapse window
  35. - click upper right corner to close a window, available when 'bool* open' is passed to ImGui::Begin()
  36. - click and drag on lower right corner to resize window
  37. - click and drag on any empty space to move window
  38. - double-click/double-tap on lower right corner grip to auto-fit to content
  39. - TAB/SHIFT+TAB to cycle through keyboard editable fields
  40. - use mouse wheel to scroll
  41. - use CTRL+mouse wheel to zoom window contents (if IO.FontAllowScaling is true)
  42. - CTRL+Click on a slider to input value as text
  43. - text editor:
  44. - Hold SHIFT or use mouse to select text.
  45. - CTRL+Left/Right to word jump
  46. - CTRL+Shift+Left/Right to select words
  47. - CTRL+A our Double-Click to select all
  48. - CTRL+X,CTRL+C,CTRL+V to use OS clipboard
  49. - CTRL+Z,CTRL+Y to undo/redo
  50. - ESCAPE to revert text to its original value
  51. - You can apply arithmetic operators +,*,/ on numerical values. Use +- to subtract (because - would set a negative value!)
  52. PROGRAMMER GUIDE
  53. ================
  54. - your code creates the UI, if your code doesn't run the UI is gone! == dynamic UI, no construction step, less data retention on your side, no state duplication, less sync, less errors.
  55. - see ImGui::ShowTestWindow() for user-side sample code
  56. - see examples/ folder for standalone sample applications.
  57. - getting started:
  58. - initialisation: call ImGui::GetIO() and fill the 'Settings' data.
  59. - every frame:
  60. 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().
  61. 2/ use any ImGui function you want between NewFrame() and Render()
  62. 3/ ImGui::Render() to render all the accumulated command-lists. it will call your RenderDrawListFn handler that you set in the IO structure.
  63. - all rendering information are stored into command-lists until ImGui::Render() is called.
  64. - effectively it means you can create widgets at any time in your code, regardless of "update" vs "render" considerations.
  65. - refer to the examples applications in the examples/ folder for instruction on how to setup your code.
  66. - a typical application skeleton may be:
  67. // Application init
  68. // TODO: Fill all settings fields of the io structure
  69. ImGuiIO& io = ImGui::GetIO();
  70. io.DisplaySize.x = 1920.0f;
  71. io.DisplaySize.y = 1280.0f;
  72. io.DeltaTime = 1.0f/60.0f;
  73. io.IniFilename = "imgui.ini";
  74. // Application main loop
  75. while (true)
  76. {
  77. // 1/ get low-level input
  78. // e.g. on Win32, GetKeyboardState(), or poll your events, etc.
  79. // 2/ TODO: Fill all 'Input' fields of io structure and call NewFrame
  80. ImGuiIO& io = ImGui::GetIO();
  81. io.MousePos = ...
  82. io.KeysDown[i] = ...
  83. ImGui::NewFrame();
  84. // 3/ most of your application code here - you can use any of ImGui::* functions between NewFrame() and Render() calls
  85. GameUpdate();
  86. GameRender();
  87. // 4/ render & swap video buffers
  88. ImGui::Render();
  89. // swap video buffer, etc.
  90. }
  91. API BREAKING CHANGES
  92. ====================
  93. Occasionally introducing changes that are breaking the API. The breakage are generally minor and easy to fix.
  94. 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.
  95. - 2014/11/28 (1.17) moved IO.Font*** options to inside the IO.Font-> structure.
  96. - 2014/11/26 (1.17) reworked syntax of IMGUI_ONCE_UPON_A_FRAME helper macro to increase compiler compatibility
  97. - 2014/11/07 (1.15) renamed IsHovered() to IsItemHovered()
  98. - 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)
  99. - 2014/09/25 (1.13) removed 'text_end' parameter from IO.SetClipboardTextFn (the string is now always zero-terminated for simplicity)
  100. - 2014/09/24 (1.12) renamed SetFontScale() to SetWindowFontScale()
  101. - 2014/09/24 (1.12) moved IM_MALLOC/IM_REALLOC/IM_FREE preprocessor defines to IO.MemAllocFn/IO.MemReallocFn/IO.MemFreeFn
  102. - 2014/08/30 (1.09) removed IO.FontHeight (now computed automatically)
  103. - 2014/08/30 (1.09) moved IMGUI_FONT_TEX_UV_FOR_WHITE preprocessor define to IO.FontTexUvForWhite
  104. - 2014/08/28 (1.09) changed the behavior of IO.PixelCenterOffset following various rendering fixes
  105. TROUBLESHOOTING & FREQUENTLY ASKED QUESTIONS
  106. ============================================
  107. If text or lines are blurry when integrating ImGui in your engine:
  108. - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
  109. - try adjusting ImGui::GetIO().PixelCenterOffset to 0.5f or 0.375f
  110. If you are confused about the meaning or use of ID in ImGui:
  111. - some widgets requires state to be carried over multiple frames (most typically ImGui often wants remember what is the "active" widget).
  112. to do so they need an unique ID. unique ID are typically derived from a string label, an indice or a pointer.
  113. when you call Button("OK") the button shows "OK" and also use "OK" as an ID.
  114. - ID are uniquely scoped within Windows so no conflict can happen if you have two buttons called "OK" in two different Windows.
  115. within a same Window, use PushID() / PopID() to easily create scopes and avoid ID conflicts.
  116. so if you have a loop creating "multiple" items, you can use PushID() / PopID() with the index of each item, or their pointer, etc.
  117. some functions like TreeNode() implicitly creates a scope for you by calling PushID()
  118. - when dealing with trees, ID are important because you want to preserve the opened/closed state of tree nodes.
  119. depending on your use cases you may want to use strings, indices or pointers as ID. experiment and see what makes more sense!
  120. e.g. When displaying a single object, using a static string as ID will preserve your node open/closed state when the targeted object change
  121. e.g. When displaying a list of objects, using indices or pointers as ID will preserve the node open/closed state per object
  122. - when passing a label you can optionally specify extra unique ID information within the same string using "##". This helps solving the simpler collision cases.
  123. e.g. "Label" display "Label" and uses "Label" as ID
  124. e.g. "Label##Foobar" display "Label" and uses "Label##Foobar" as ID
  125. e.g. "##Foobar" display an empty label and uses "##Foobar" as ID
  126. - read articles about the imgui principles (see web links) to understand the requirement and use of ID.
  127. If you want to use a different font than the default:
  128. - read extra_fonts/README.txt for instructions. Examples fonts are also provided.
  129. - if you can only see text but no solid shapes or lines, make sure io.Font->TexUvForWhite is set to the texture coordinates of a pure white pixel in your texture!
  130. If you want to input Japanese/Chinese/Korean in the text input widget:
  131. - make sure you are using a font that can display the glyphs you want (see above paragraph about fonts)
  132. - to have the Microsoft IME cursor appears at the right location in the screen, setup a handler for the io.ImeSetInputScreenPosFn function:
  133. #include <Windows.h>
  134. #include <Imm.h>
  135. static void ImImpl_ImeSetInputScreenPosFn(int x, int y)
  136. {
  137. // Notify OS Input Method Editor of text input position
  138. HWND hwnd = glfwGetWin32Window(window);
  139. if (HIMC himc = ImmGetContext(hwnd))
  140. {
  141. COMPOSITIONFORM cf;
  142. cf.ptCurrentPos.x = x;
  143. cf.ptCurrentPos.y = y;
  144. cf.dwStyle = CFS_FORCE_POSITION;
  145. ImmSetCompositionWindow(himc, &cf);
  146. }
  147. }
  148. // Set pointer to handler in ImGuiIO structure
  149. io.ImeSetInputScreenPosFn = ImImpl_ImeSetInputScreenPosFn;
  150. - tip: the construct 'IMGUI_ONCE_UPON_A_FRAME { ... }' will evaluate to a 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.
  151. - tip: you can call Render() multiple times (e.g for VR renders), up to you to communicate the extra state to your RenderDrawListFn function.
  152. - tip: you can create widgets without a Begin()/End() block, they will go in an implicit window called "Debug"
  153. - tip: read the ShowTestWindow() code for more example of how to use ImGui!
  154. ISSUES & TODO-LIST
  155. ==================
  156. - misc: merge or clarify ImVec4 / ImGuiAabb, they are essentially duplicate containers
  157. - window: autofit is losing its purpose when user relies on any dynamic layout (window width multiplier, column). maybe just clearly discard autofit?
  158. - window: add horizontal scroll
  159. - window: fix resize grip rendering scaling along with Rounding style setting
  160. - window: better helpers to set pos/size/collapsed with different options (first-run, session only, current value) (github issue #89)
  161. - widgets: switching from "widget-label" to "label-widget" would make it more convenient to integrate widgets in trees
  162. - widgets: clip text? hover clipped text shows it in a tooltip or in-place overlay
  163. - main: make IsHovered() 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
  164. - main: make IsHovered() info stored in a stack? so that 'if TreeNode() { Text; TreePop; } if IsHovered' return the hover state of the TreeNode?
  165. - scrollbar: use relative mouse movement when first-clicking inside of scroll grab box.
  166. - scrollbar: make the grab visible and a minimum size for long scroll regions
  167. - input number: optional range min/max
  168. - input number: holding [-]/[+] buttons should increase the step non-linearly
  169. - input number: use mouse wheel to step up/down
  170. - layout: clean up the InputFloatN/SliderFloatN/ColorEdit4 horrible layout code. item width should include frame padding, then we can have a generic horizontal layout helper.
  171. - columns: declare column set (each column: fixed size, %, fill, distribute default size among fills)
  172. - columns: columns header to act as button (~sort op) and allow resize/reorder
  173. - columns: user specify columns size
  174. - combo: turn child handling code into pop up helper
  175. - list selection, concept of a selectable "block" (that can be multiple widgets)
  176. - menubar, menus
  177. - plot: make it easier for user to draw into the graph (e.g: draw basis, highlight certain points, 2d plots, multiple plots)
  178. - plot: "smooth" automatic scale, user give an input 0.0(full user scale) 1.0(full derived from value)
  179. - 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)
  180. - file selection widget -> build the tool in our codebase to improve model-dialog idioms (may or not lead to ImGui changes)
  181. - slider: allow using the [-]/[+] buttons used by InputFloat()/InputInt()
  182. - slider: initial absolute click is imprecise. change to relative movement slider? hide mouse cursor, allow more precise input using less screen-space.
  183. - text edit: clean up the horrible mess caused by converting UTF-8 <> wchar
  184. - text edit: centered text for slider or input text to it matches typical positioning.
  185. - text edit: flag to disable live update of the user buffer.
  186. - text edit: field resize behavior - field could stretch when being edited? hover tooltip shows more text?
  187. - text edit: pasting text into a number box should filter the characters the same way direct input does
  188. - text edit: add multi-line text edit
  189. - settings: write more decent code to allow saving/loading new fields
  190. - settings: api for per-tool simple persistent data (bool,int,float) in .ini file
  191. - log: have more control over the log scope (e.g. stop logging when leaving current tree node scope)
  192. - log: be able to right-click and log a window or tree-node into tty/file/clipboard?
  193. - filters: set a current filter that tree node can automatically query to hide themselves
  194. - filters: handle wildcards (with implicit leading/trailing *), regexps
  195. - 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)
  196. ! keyboard: tooltip & combo boxes are messing up / not honoring keyboard tabbing
  197. - keyboard: full keyboard navigation and focus.
  198. - input: rework IO to be able to pass actual events to fix temporal aliasing issues.
  199. - input: support track pad style scrolling & slider edit.
  200. - tooltip: move to fit within screen (e.g. when mouse cursor is right of the screen).
  201. - clipboard: automatically transform \n into \n\r or equivalent for higher compatibility on windows
  202. - portability: big-endian test/support (github issue #81)
  203. - misc: provide a way to compile out the entire implementation while providing a dummy API (e.g. #define IMGUI_DUMMY_IMPL
  204. - misc: not thread-safe
  205. - misc: double-clicking on title bar to minimize isn't consistent, perhaps move to single-click on left-most collapse icon?
  206. - misc: CalcTextSize() could benefit from having 'hide_text_after_double_hash' false by default for external use?
  207. - style editor: add a button to output C code.
  208. - examples: integrate dx11 example.
  209. - examples: integrate opengl 3/4 programmable pipeline example.
  210. - optimization/render: use indexed rendering
  211. - optimization/render: move clip-rect to vertex data? would allow merging all commands
  212. - 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)?
  213. - optimization/render: font exported by bmfont is not tight fit on vertical axis, incur unneeded pixel-shading cost.
  214. - optimization: turn some the various stack vectors into statically-sized arrays
  215. - optimization: better clipping for multi-component widgets
  216. - optimization: specialize for height based clipping first (assume widgets never go up + height tests before width tests?)
  217. */
  218. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  219. #define _CRT_SECURE_NO_WARNINGS
  220. #endif
  221. #include "imgui.h"
  222. #include <ctype.h> // toupper
  223. #include <math.h> // sqrtf
  224. #include <stdint.h> // intptr_t
  225. #include <stdio.h> // vsnprintf
  226. #include <string.h> // memset
  227. #include <new> // new (ptr)
  228. #ifdef _MSC_VER
  229. #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen
  230. #endif
  231. #ifdef __clang__
  232. #pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse and not scary looking.
  233. #pragma clang diagnostic ignored "-Wfloat-equal" // warning : comparing floating point with == or != is unsafe // storing and comparing against same constants ok.
  234. #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, thank you.
  235. #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.
  236. #pragma clang diagnostic ignored "-Wglobal-constructors" // warning : declaration requires a global destructor // similar to above, not sure what the exact difference it.
  237. #endif
  238. //-------------------------------------------------------------------------
  239. // Forward Declarations
  240. //-------------------------------------------------------------------------
  241. static bool ButtonBehaviour(const ImGuiAabb& bb, const ImGuiID& id, bool* out_hovered, bool* out_held, bool allow_key_modifiers, bool repeat = false);
  242. static void LogText(const ImVec2& ref_pos, const char* text, const char* text_end = NULL);
  243. static void RenderText(ImVec2 pos, const char* text, const char* text_end = NULL, bool hide_text_after_hash = true, float wrap_width = 0.0f);
  244. static void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border = true, float rounding = 0.0f);
  245. static void RenderCollapseTriangle(ImVec2 p_min, bool open, float scale = 1.0f, bool shadow = false);
  246. static void ItemSize(ImVec2 size, ImVec2* adjust_start_offset = NULL);
  247. static void ItemSize(const ImGuiAabb& aabb, ImVec2* adjust_start_offset = NULL);
  248. static void PushColumnClipRect(int column_index = -1);
  249. static bool IsClipped(const ImGuiAabb& aabb);
  250. static bool ClipAdvance(const ImGuiAabb& aabb);
  251. static bool IsMouseHoveringBox(const ImGuiAabb& box);
  252. static bool IsKeyPressedMap(ImGuiKey key, bool repeat = true);
  253. static bool CloseWindowButton(bool* open = NULL);
  254. static void FocusWindow(ImGuiWindow* window);
  255. static ImGuiWindow* FindWindow(const char* name);
  256. static ImGuiWindow* FindHoveredWindow(ImVec2 pos, bool excluding_childs);
  257. //-----------------------------------------------------------------------------
  258. // Platform dependent default implementations
  259. //-----------------------------------------------------------------------------
  260. static const char* GetClipboardTextFn_DefaultImpl();
  261. static void SetClipboardTextFn_DefaultImpl(const char* text);
  262. //-----------------------------------------------------------------------------
  263. // User facing structures
  264. //-----------------------------------------------------------------------------
  265. ImGuiStyle::ImGuiStyle()
  266. {
  267. Alpha = 1.0f; // Global alpha applies to everything in ImGui
  268. WindowPadding = ImVec2(8,8); // Padding within a window
  269. WindowMinSize = ImVec2(48,48); // Minimum window size
  270. FramePadding = ImVec2(5,4); // Padding within a framed rectangle (used by most widgets)
  271. ItemSpacing = ImVec2(10,5); // Horizontal and vertical spacing between widgets/lines
  272. ItemInnerSpacing = ImVec2(5,5); // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label)
  273. TouchExtraPadding = ImVec2(0,0); // Expand bounding box for touch-based system where touch position is not accurate enough (unnecessary for mouse inputs). Unfortunately we don't sort widgets so priority on overlap will always be given to the first widget running. So dont grow this too much!
  274. AutoFitPadding = ImVec2(8,8); // Extra space after auto-fit (double-clicking on resize grip)
  275. WindowFillAlphaDefault = 0.70f; // Default alpha of window background, if not specified in ImGui::Begin()
  276. WindowRounding = 10.0f; // Radius of window corners rounding. Set to 0.0f to have rectangular windows
  277. TreeNodeSpacing = 22.0f; // Horizontal spacing when entering a tree node
  278. ColumnsMinSpacing = 6.0f; // Minimum horizontal spacing between two columns
  279. ScrollBarWidth = 16.0f; // Width of the vertical scroll bar
  280. Colors[ImGuiCol_Text] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
  281. Colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  282. Colors[ImGuiCol_Border] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  283. Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.60f);
  284. Colors[ImGuiCol_FrameBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.30f); // Background of checkbox, radio button, plot, slider, text input
  285. Colors[ImGuiCol_TitleBg] = ImVec4(0.50f, 0.50f, 1.00f, 0.45f);
  286. Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.40f, 0.40f, 0.80f, 0.20f);
  287. Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.40f, 0.40f, 0.80f, 0.15f);
  288. Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.40f, 0.40f, 0.80f, 0.30f);
  289. Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.40f, 0.80f, 0.40f);
  290. Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.80f, 0.50f, 0.50f, 0.40f);
  291. Colors[ImGuiCol_ComboBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.99f);
  292. Colors[ImGuiCol_CheckHovered] = ImVec4(0.60f, 0.40f, 0.40f, 0.45f);
  293. Colors[ImGuiCol_CheckActive] = ImVec4(0.90f, 0.90f, 0.90f, 0.50f);
  294. Colors[ImGuiCol_SliderGrab] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f);
  295. Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.80f, 0.50f, 0.50f, 1.00f);
  296. Colors[ImGuiCol_Button] = ImVec4(0.67f, 0.40f, 0.40f, 0.60f);
  297. Colors[ImGuiCol_ButtonHovered] = ImVec4(0.67f, 0.40f, 0.40f, 1.00f);
  298. Colors[ImGuiCol_ButtonActive] = ImVec4(0.80f, 0.50f, 0.50f, 1.00f);
  299. Colors[ImGuiCol_Header] = ImVec4(0.40f, 0.40f, 0.90f, 0.45f);
  300. Colors[ImGuiCol_HeaderHovered] = ImVec4(0.45f, 0.45f, 0.90f, 0.80f);
  301. Colors[ImGuiCol_HeaderActive] = ImVec4(0.60f, 0.60f, 0.80f, 1.00f);
  302. Colors[ImGuiCol_Column] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  303. Colors[ImGuiCol_ColumnHovered] = ImVec4(0.60f, 0.40f, 0.40f, 1.00f);
  304. Colors[ImGuiCol_ColumnActive] = ImVec4(0.80f, 0.50f, 0.50f, 1.00f);
  305. Colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f);
  306. Colors[ImGuiCol_ResizeGripHovered] = ImVec4(1.00f, 1.00f, 1.00f, 0.60f);
  307. Colors[ImGuiCol_ResizeGripActive] = ImVec4(1.00f, 1.00f, 1.00f, 0.90f);
  308. Colors[ImGuiCol_CloseButton] = ImVec4(0.50f, 0.50f, 0.90f, 0.50f);
  309. Colors[ImGuiCol_CloseButtonHovered] = ImVec4(0.70f, 0.70f, 0.90f, 0.60f);
  310. Colors[ImGuiCol_CloseButtonActive] = ImVec4(0.70f, 0.70f, 0.70f, 1.00f);
  311. Colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  312. Colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
  313. Colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
  314. Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);
  315. Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 0.00f, 1.00f, 0.35f);
  316. Colors[ImGuiCol_TooltipBg] = ImVec4(0.05f, 0.05f, 0.10f, 0.90f);
  317. }
  318. ImGuiIO::ImGuiIO()
  319. {
  320. memset(this, 0, sizeof(*this));
  321. DeltaTime = 1.0f/60.0f;
  322. IniSavingRate = 5.0f;
  323. IniFilename = "imgui.ini";
  324. LogFilename = "imgui_log.txt";
  325. Font = NULL;
  326. FontGlobalScale = 1.0f;
  327. FontAllowUserScaling = false;
  328. PixelCenterOffset = 0.0f;
  329. MousePos = ImVec2(-1,-1);
  330. MousePosPrev = ImVec2(-1,-1);
  331. MouseDoubleClickTime = 0.30f;
  332. MouseDoubleClickMaxDist = 6.0f;
  333. UserData = NULL;
  334. // User functions
  335. RenderDrawListsFn = NULL;
  336. MemAllocFn = malloc;
  337. MemReallocFn = realloc;
  338. MemFreeFn = free;
  339. GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependant default implementations
  340. SetClipboardTextFn = SetClipboardTextFn_DefaultImpl;
  341. ImeSetInputScreenPosFn = NULL;
  342. }
  343. // Pass in translated ASCII characters for text input.
  344. // - with glfw you can get those from the callback set in glfwSetCharCallback()
  345. // - on Windows you can get those using ToAscii+keyboard state, or via the VM_CHAR message
  346. static size_t ImStrlenW(const ImWchar* str);
  347. void ImGuiIO::AddInputCharacter(ImWchar c)
  348. {
  349. const size_t n = ImStrlenW(InputCharacters);
  350. if (n + 1 < sizeof(InputCharacters) / sizeof(InputCharacters[0]))
  351. {
  352. InputCharacters[n] = c;
  353. InputCharacters[n+1] = 0;
  354. }
  355. }
  356. //-----------------------------------------------------------------------------
  357. // Helpers
  358. //-----------------------------------------------------------------------------
  359. #define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR)/sizeof(*_ARR)))
  360. #undef PI
  361. const float PI = 3.14159265358979323846f;
  362. #ifdef INT_MAX
  363. #define IM_INT_MAX INT_MAX
  364. #else
  365. #define IM_INT_MAX 2147483647
  366. #endif
  367. // Math bits
  368. // 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.
  369. static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x*rhs, lhs.y*rhs); }
  370. //static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x/rhs, lhs.y/rhs); }
  371. static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x+rhs.x, lhs.y+rhs.y); }
  372. static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x-rhs.x, lhs.y-rhs.y); }
  373. static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2 rhs) { return ImVec2(lhs.x*rhs.x, lhs.y*rhs.y); }
  374. static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2 rhs) { return ImVec2(lhs.x/rhs.x, lhs.y/rhs.y); }
  375. static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; }
  376. static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; }
  377. static inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; }
  378. //static inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; }
  379. static inline int ImMin(int lhs, int rhs) { return lhs < rhs ? lhs : rhs; }
  380. static inline int ImMax(int lhs, int rhs) { return lhs >= rhs ? lhs : rhs; }
  381. static inline float ImMin(float lhs, float rhs) { return lhs < rhs ? lhs : rhs; }
  382. static inline float ImMax(float lhs, float rhs) { return lhs >= rhs ? lhs : rhs; }
  383. static inline ImVec2 ImMin(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(ImMin(lhs.x,rhs.x), ImMin(lhs.y,rhs.y)); }
  384. static inline ImVec2 ImMax(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(ImMax(lhs.x,rhs.x), ImMax(lhs.y,rhs.y)); }
  385. static inline float ImClamp(float f, float mn, float mx) { return (f < mn) ? mn : (f > mx) ? mx : f; }
  386. 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)); }
  387. static inline float ImSaturate(float f) { return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f; }
  388. static inline float ImLerp(float a, float b, float t) { return a + (b - a) * t; }
  389. //static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, float t) { return a + (b - a) * t; }
  390. 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); }
  391. static inline float ImLength(const ImVec2& lhs) { return sqrtf(lhs.x*lhs.x + lhs.y*lhs.y); }
  392. static int ImTextCharToUtf8(char* buf, size_t buf_size, unsigned int in_char); // return output UTF-8 bytes count
  393. static ptrdiff_t ImTextStrToUtf8(char* buf, size_t buf_size, const ImWchar* in_text, const ImWchar* in_text_end); // return output UTF-8 bytes count
  394. static int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end); // return input UTF-8 bytes count
  395. static ptrdiff_t ImTextStrFromUtf8(ImWchar* buf, size_t buf_size, const char* in_text, const char* in_text_end); // return input UTF-8 bytes count
  396. static int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end); // return number of UTF-8 code-points (NOT bytes count)
  397. static int ImTextCountUtf8BytesFromWchar(const ImWchar* in_text, const ImWchar* in_text_end); // return number of bytes to express string as UTF-8 code-points
  398. static int ImStricmp(const char* str1, const char* str2)
  399. {
  400. int d;
  401. while ((d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; }
  402. return d;
  403. }
  404. static int ImStrnicmp(const char* str1, const char* str2, int count)
  405. {
  406. int d = 0;
  407. while (count > 0 && (d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; count--; }
  408. return d;
  409. }
  410. static char* ImStrdup(const char *str)
  411. {
  412. char *buff = (char*)ImGui::MemAlloc(strlen(str) + 1);
  413. IM_ASSERT(buff);
  414. strcpy(buff, str);
  415. return buff;
  416. }
  417. static size_t ImStrlenW(const ImWchar* str)
  418. {
  419. size_t n = 0;
  420. while (*str++)
  421. n++;
  422. return n;
  423. }
  424. static const char* ImStristr(const char* haystack, const char* needle, const char* needle_end)
  425. {
  426. if (!needle_end)
  427. needle_end = needle + strlen(needle);
  428. const char un0 = (char)toupper(*needle);
  429. while (*haystack)
  430. {
  431. if (toupper(*haystack) == un0)
  432. {
  433. const char* b = needle + 1;
  434. for (const char* a = haystack + 1; b < needle_end; a++, b++)
  435. if (toupper(*a) != toupper(*b))
  436. break;
  437. if (b == needle_end)
  438. return haystack;
  439. }
  440. haystack++;
  441. }
  442. return NULL;
  443. }
  444. static ImU32 crc32(const void* data, size_t data_size, ImU32 seed = 0)
  445. {
  446. static ImU32 crc32_lut[256] = { 0 };
  447. if (!crc32_lut[1])
  448. {
  449. const ImU32 polynomial = 0xEDB88320;
  450. for (ImU32 i = 0; i < 256; i++)
  451. {
  452. ImU32 crc = i;
  453. for (ImU32 j = 0; j < 8; j++)
  454. crc = (crc >> 1) ^ (ImU32(-int(crc & 1)) & polynomial);
  455. crc32_lut[i] = crc;
  456. }
  457. }
  458. ImU32 crc = ~seed;
  459. const unsigned char* current = (const unsigned char*)data;
  460. while (data_size--)
  461. crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ *current++];
  462. return ~crc;
  463. }
  464. static size_t ImFormatString(char* buf, size_t buf_size, const char* fmt, ...)
  465. {
  466. va_list args;
  467. va_start(args, fmt);
  468. int w = vsnprintf(buf, buf_size, fmt, args);
  469. va_end(args);
  470. buf[buf_size-1] = 0;
  471. return (w == -1) ? buf_size : (size_t)w;
  472. }
  473. static size_t ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args)
  474. {
  475. int w = vsnprintf(buf, buf_size, fmt, args);
  476. buf[buf_size-1] = 0;
  477. return (w == -1) ? buf_size : (size_t)w;
  478. }
  479. static ImU32 ImConvertColorFloat4ToU32(const ImVec4& in)
  480. {
  481. ImU32 out = ((ImU32)(ImSaturate(in.x)*255.f));
  482. out |= ((ImU32)(ImSaturate(in.y)*255.f) << 8);
  483. out |= ((ImU32)(ImSaturate(in.z)*255.f) << 16);
  484. out |= ((ImU32)(ImSaturate(in.w)*255.f) << 24);
  485. return out;
  486. }
  487. // Convert rgb floats ([0-1],[0-1],[0-1]) to hsv floats ([0-1],[0-1],[0-1]), from Foley & van Dam p592
  488. // Optimized http://lolengine.net/blog/2013/01/13/fast-rgb-to-hsv
  489. static void ImConvertColorRGBtoHSV(float r, float g, float b, float& out_h, float& out_s, float& out_v)
  490. {
  491. float K = 0.f;
  492. if (g < b)
  493. {
  494. const float tmp = g; g = b; b = tmp;
  495. K = -1.f;
  496. }
  497. if (r < g)
  498. {
  499. const float tmp = r; r = g; g = tmp;
  500. K = -2.f / 6.f - K;
  501. }
  502. const float chroma = r - (g < b ? g : b);
  503. out_h = fabsf(K + (g - b) / (6.f * chroma + 1e-20f));
  504. out_s = chroma / (r + 1e-20f);
  505. out_v = r;
  506. }
  507. // Convert hsv floats ([0-1],[0-1],[0-1]) to rgb floats ([0-1],[0-1],[0-1]), from Foley & van Dam p593
  508. // also http://en.wikipedia.org/wiki/HSL_and_HSV
  509. static void ImConvertColorHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b)
  510. {
  511. if (s == 0.0f)
  512. {
  513. // gray
  514. out_r = out_g = out_b = v;
  515. return;
  516. }
  517. h = fmodf(h, 1.0f) / (60.0f/360.0f);
  518. int i = (int)h;
  519. float f = h - (float)i;
  520. float p = v * (1.0f - s);
  521. float q = v * (1.0f - s * f);
  522. float t = v * (1.0f - s * (1.0f - f));
  523. switch (i)
  524. {
  525. case 0: out_r = v; out_g = t; out_b = p; break;
  526. case 1: out_r = q; out_g = v; out_b = p; break;
  527. case 2: out_r = p; out_g = v; out_b = t; break;
  528. case 3: out_r = p; out_g = q; out_b = v; break;
  529. case 4: out_r = t; out_g = p; out_b = v; break;
  530. case 5: default: out_r = v; out_g = p; out_b = q; break;
  531. }
  532. }
  533. //-----------------------------------------------------------------------------
  534. struct ImGuiColMod // Color modifier, backup of modified data so we can restore it
  535. {
  536. ImGuiCol Col;
  537. ImVec4 PreviousValue;
  538. };
  539. struct ImGuiStyleMod // Style modifier, backup of modified data so we can restore it
  540. {
  541. ImGuiStyleVar Var;
  542. ImVec2 PreviousValue;
  543. };
  544. struct ImGuiAabb // 2D axis aligned bounding-box
  545. {
  546. ImVec2 Min;
  547. ImVec2 Max;
  548. ImGuiAabb() { Min = ImVec2(FLT_MAX,FLT_MAX); Max = ImVec2(-FLT_MAX,-FLT_MAX); }
  549. ImGuiAabb(const ImVec2& min, const ImVec2& max) { Min = min; Max = max; }
  550. ImGuiAabb(const ImVec4& v) { Min.x = v.x; Min.y = v.y; Max.x = v.z; Max.y = v.w; }
  551. ImGuiAabb(float x1, float y1, float x2, float y2) { Min.x = x1; Min.y = y1; Max.x = x2; Max.y = y2; }
  552. ImVec2 GetCenter() const { return Min + (Max-Min)*0.5f; }
  553. ImVec2 GetSize() const { return Max-Min; }
  554. float GetWidth() const { return (Max-Min).x; }
  555. float GetHeight() const { return (Max-Min).y; }
  556. ImVec2 GetTL() const { return Min; }
  557. ImVec2 GetTR() const { return ImVec2(Max.x,Min.y); }
  558. ImVec2 GetBL() const { return ImVec2(Min.x,Max.y); }
  559. ImVec2 GetBR() const { return Max; }
  560. bool Contains(ImVec2 p) const { return p.x >= Min.x && p.y >= Min.y && p.x <= Max.x && p.y <= Max.y; }
  561. bool Contains(const ImGuiAabb& r) const { return r.Min.x >= Min.x && r.Min.y >= Min.y && r.Max.x <= Max.x && r.Max.y <= Max.y; }
  562. bool Overlaps(const ImGuiAabb& r) const { return r.Min.y <= Max.y && r.Max.y >= Min.y && r.Min.x <= Max.x && r.Max.x >= Min.x; }
  563. void Expand(ImVec2 sz) { Min -= sz; Max += sz; }
  564. void Clip(const ImGuiAabb& 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); }
  565. };
  566. // Temporary per-window data, reset at the beginning of the frame
  567. struct ImGuiDrawContext
  568. {
  569. ImVec2 CursorPos;
  570. ImVec2 CursorPosPrevLine;
  571. ImVec2 CursorStartPos;
  572. float CurrentLineHeight;
  573. float PrevLineHeight;
  574. float LogLineHeight;
  575. int TreeDepth;
  576. ImGuiAabb LastItemAabb;
  577. bool LastItemHovered;
  578. bool LastItemFocused;
  579. ImVector<ImGuiWindow*> ChildWindows;
  580. ImVector<bool> AllowKeyboardFocus;
  581. ImVector<float> ItemWidth;
  582. ImVector<float> TextWrapPos;
  583. ImGuiColorEditMode ColorEditMode;
  584. ImGuiStorage* StateStorage;
  585. int OpenNextNode;
  586. float ColumnsStartX; // Start position from left of window (increased by TreePush/TreePop, etc.)
  587. 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.
  588. int ColumnsCurrent;
  589. int ColumnsCount;
  590. ImVec2 ColumnsStartPos;
  591. float ColumnsCellMinY;
  592. float ColumnsCellMaxY;
  593. bool ColumnsShowBorders;
  594. ImGuiID ColumnsSetID;
  595. ImGuiDrawContext()
  596. {
  597. CursorPos = CursorPosPrevLine = CursorStartPos = ImVec2(0.0f, 0.0f);
  598. CurrentLineHeight = PrevLineHeight = 0.0f;
  599. LogLineHeight = -1.0f;
  600. TreeDepth = 0;
  601. LastItemAabb = ImGuiAabb(0.0f,0.0f,0.0f,0.0f);
  602. LastItemHovered = false;
  603. LastItemFocused = true;
  604. StateStorage = NULL;
  605. OpenNextNode = -1;
  606. ColumnsStartX = 0.0f;
  607. ColumnsOffsetX = 0.0f;
  608. ColumnsCurrent = 0;
  609. ColumnsCount = 1;
  610. ColumnsStartPos = ImVec2(0.0f, 0.0f);
  611. ColumnsCellMinY = ColumnsCellMaxY = 0.0f;
  612. ColumnsShowBorders = true;
  613. ColumnsSetID = 0;
  614. }
  615. };
  616. struct ImGuiTextEditState;
  617. #define STB_TEXTEDIT_STRING ImGuiTextEditState
  618. #define STB_TEXTEDIT_CHARTYPE ImWchar
  619. #include "stb_textedit.h"
  620. // Internal state of the currently focused/edited text input box
  621. struct ImGuiTextEditState
  622. {
  623. ImWchar Text[1024]; // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so we copy into own buffer.
  624. char InitialText[1024*3+1]; // backup of end-user buffer at the time of focus (in UTF-8, unconverted)
  625. size_t BufSize; // end-user buffer size, <= 1024 (or increase above)
  626. float Width; // widget width
  627. float ScrollX;
  628. STB_TexteditState StbState;
  629. float CursorAnim;
  630. ImVec2 LastCursorPos; // Cursor position in screen space to be used by IME callback.
  631. bool SelectedAllMouseLock;
  632. ImFont* Font;
  633. float FontSize;
  634. ImGuiTextEditState() { memset(this, 0, sizeof(*this)); }
  635. void CursorAnimReset() { CursorAnim = -0.30f; } // After a user-input the cursor stays on for a while without blinking
  636. bool CursorIsVisible() const { return CursorAnim <= 0.0f || fmodf(CursorAnim, 1.20f) <= 0.80f; } // Blinking
  637. bool HasSelection() const { return StbState.select_start != StbState.select_end; }
  638. void SelectAll() { StbState.select_start = 0; StbState.select_end = (int)ImStrlenW(Text); StbState.cursor = StbState.select_end; StbState.has_preferred_x = false; }
  639. void OnKeyPressed(int key);
  640. void UpdateScrollOffset();
  641. ImVec2 CalcDisplayOffsetFromCharIdx(int i) const;
  642. // Static functions because they are used to render non-focused instances of a text input box
  643. static const char* GetTextPointerClippedA(ImFont* font, float font_size, const char* text, float width, ImVec2* out_text_size = NULL);
  644. static const ImWchar* GetTextPointerClippedW(ImFont* font, float font_size, const ImWchar* text, float width, ImVec2* out_text_size = NULL);
  645. static void RenderTextScrolledClipped(ImFont* font, float font_size, const char* text, ImVec2 pos_base, float width, float scroll_x);
  646. };
  647. struct ImGuiIniData
  648. {
  649. char* Name;
  650. ImVec2 Pos;
  651. ImVec2 Size;
  652. bool Collapsed;
  653. ImGuiIniData() { memset(this, 0, sizeof(*this)); }
  654. ~ImGuiIniData() { if (Name) { ImGui::MemFree(Name); Name = NULL; } }
  655. };
  656. struct ImGuiState
  657. {
  658. bool Initialized;
  659. ImGuiIO IO;
  660. ImGuiStyle Style;
  661. float FontSize; // == IO.FontGlobalScale * IO.Font->Scale * IO.Font->Info->FontSize. Vertical distance between two lines of text, aka == CalcTextSize(" ").y
  662. ImVec2 FontTexUvForWhite; // == IO.Font->FontTexUvForWhite (cached copy)
  663. float Time;
  664. int FrameCount;
  665. int FrameCountRendered;
  666. ImVector<ImGuiWindow*> Windows;
  667. ImGuiWindow* CurrentWindow; // Being drawn into
  668. ImVector<ImGuiWindow*> CurrentWindowStack;
  669. ImGuiWindow* FocusedWindow; // Will catch keyboard inputs
  670. ImGuiWindow* HoveredWindow; // Will catch mouse inputs
  671. ImGuiWindow* HoveredWindowExcludingChilds; // Will catch mouse inputs (for focus/move only)
  672. ImGuiID HoveredId;
  673. ImGuiID ActiveId;
  674. ImGuiID ActiveIdPreviousFrame;
  675. bool ActiveIdIsAlive;
  676. float SettingsDirtyTimer;
  677. ImVector<ImGuiIniData*> Settings;
  678. ImVec2 NewWindowDefaultPos;
  679. ImVector<ImGuiColMod> ColorModifiers;
  680. ImVector<ImGuiStyleMod> StyleModifiers;
  681. // Render
  682. ImVector<ImDrawList*> RenderDrawLists;
  683. // Widget state
  684. ImGuiTextEditState InputTextState;
  685. ImGuiID SliderAsInputTextId;
  686. ImGuiStorage ColorEditModeStorage; // for user selection
  687. ImGuiID ActiveComboID;
  688. char Tooltip[1024];
  689. char* PrivateClipboard; // if no custom clipboard handler is defined
  690. // Logging
  691. bool LogEnabled;
  692. FILE* LogFile;
  693. ImGuiTextBuffer* LogClipboard; // pointer so our GImGui static constructor doesn't call heap allocators.
  694. int LogAutoExpandMaxDepth;
  695. ImGuiState()
  696. {
  697. Initialized = false;
  698. Time = 0.0f;
  699. FrameCount = 0;
  700. FrameCountRendered = -1;
  701. CurrentWindow = NULL;
  702. FocusedWindow = NULL;
  703. HoveredWindow = NULL;
  704. HoveredWindowExcludingChilds = NULL;
  705. ActiveIdIsAlive = false;
  706. SettingsDirtyTimer = 0.0f;
  707. NewWindowDefaultPos = ImVec2(60, 60);
  708. SliderAsInputTextId = 0;
  709. ActiveComboID = 0;
  710. memset(Tooltip, 0, sizeof(Tooltip));
  711. PrivateClipboard = NULL;
  712. LogEnabled = false;
  713. LogFile = NULL;
  714. LogAutoExpandMaxDepth = 2;
  715. LogClipboard = NULL;
  716. }
  717. };
  718. static ImGuiState GImGui;
  719. struct ImGuiWindow
  720. {
  721. char* Name;
  722. ImGuiID ID;
  723. ImGuiWindowFlags Flags;
  724. ImVec2 PosFloat;
  725. ImVec2 Pos; // Position rounded-up to nearest pixel
  726. ImVec2 Size; // Current size (==SizeFull or collapsed title bar size)
  727. ImVec2 SizeFull; // Size when non collapsed
  728. ImVec2 SizeContentsFit; // Size of contents (extents reach by the drawing cursor) - may not fit within Size.
  729. float ScrollY;
  730. float NextScrollY;
  731. bool ScrollbarY;
  732. bool Visible; // Set to true on Begin()
  733. bool Accessed; // Set to true when any widget access the current window
  734. bool Collapsed; // Set when collapsing window to become only title-bar
  735. bool SkipItems; // == Visible && !Collapsed
  736. int AutoFitFrames;
  737. bool AutoFitOnlyGrows;
  738. ImGuiDrawContext DC;
  739. ImVector<ImGuiID> IDStack;
  740. ImVector<ImVec4> ClipRectStack;
  741. int LastFrameDrawn;
  742. float ItemWidthDefault;
  743. ImGuiStorage StateStorage;
  744. float FontWindowScale; // Scale multipler per-window
  745. ImDrawList* DrawList;
  746. // Focus
  747. int FocusIdxAllCounter; // Start at -1 and increase as assigned via FocusItemRegister()
  748. int FocusIdxTabCounter; // (same, but only count widgets which you can Tab through)
  749. int FocusIdxAllRequestCurrent; // Item being requested for focus
  750. int FocusIdxTabRequestCurrent; // Tab-able item being requested for focus
  751. 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)
  752. int FocusIdxTabRequestNext; // "
  753. public:
  754. ImGuiWindow(const char* name, ImVec2 default_pos, ImVec2 default_size);
  755. ~ImGuiWindow();
  756. ImGuiID GetID(const char* str);
  757. ImGuiID GetID(const void* ptr);
  758. void AddToRenderList();
  759. bool FocusItemRegister(bool is_active, bool tab_stop = true); // Return true if focus is requested
  760. void FocusItemUnregister();
  761. ImGuiAabb Aabb() const { return ImGuiAabb(Pos, Pos+Size); }
  762. ImFont* Font() const { return GImGui.IO.Font; }
  763. float FontSize() const { return GImGui.FontSize * FontWindowScale; }
  764. ImVec2 CursorPos() const { return DC.CursorPos; }
  765. float TitleBarHeight() const { return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0 : FontSize() + GImGui.Style.FramePadding.y * 2.0f; }
  766. ImGuiAabb TitleBarAabb() const { return ImGuiAabb(Pos, Pos + ImVec2(SizeFull.x, TitleBarHeight())); }
  767. ImVec2 WindowPadding() const { return ((Flags & ImGuiWindowFlags_ChildWindow) && !(Flags & ImGuiWindowFlags_ShowBorders)) ? ImVec2(1,1) : GImGui.Style.WindowPadding; }
  768. ImU32 Color(ImGuiCol idx, float a=1.f) const { ImVec4 c = GImGui.Style.Colors[idx]; c.w *= GImGui.Style.Alpha * a; return ImConvertColorFloat4ToU32(c); }
  769. ImU32 Color(const ImVec4& col) const { ImVec4 c = col; c.w *= GImGui.Style.Alpha; return ImConvertColorFloat4ToU32(c); }
  770. };
  771. static ImGuiWindow* GetCurrentWindow()
  772. {
  773. IM_ASSERT(GImGui.CurrentWindow != NULL); // ImGui::NewFrame() hasn't been called yet?
  774. GImGui.CurrentWindow->Accessed = true;
  775. return GImGui.CurrentWindow;
  776. }
  777. static void RegisterAliveId(const ImGuiID& id)
  778. {
  779. if (GImGui.ActiveId == id)
  780. GImGui.ActiveIdIsAlive = true;
  781. }
  782. //-----------------------------------------------------------------------------
  783. // Helper: Key->value storage
  784. void ImGuiStorage::Clear()
  785. {
  786. Data.clear();
  787. }
  788. // std::lower_bound but without the bullshit
  789. static ImVector<ImGuiStorage::Pair>::iterator LowerBound(ImVector<ImGuiStorage::Pair>& data, ImU32 key)
  790. {
  791. ImVector<ImGuiStorage::Pair>::iterator first = data.begin();
  792. ImVector<ImGuiStorage::Pair>::iterator last = data.end();
  793. int count = (int)(last - first);
  794. while (count > 0)
  795. {
  796. int count2 = count / 2;
  797. ImVector<ImGuiStorage::Pair>::iterator mid = first + count2;
  798. if (mid->key < key)
  799. {
  800. first = ++mid;
  801. count -= count2 + 1;
  802. }
  803. else
  804. {
  805. count = count2;
  806. }
  807. }
  808. return first;
  809. }
  810. int* ImGuiStorage::Find(ImU32 key)
  811. {
  812. ImVector<Pair>::iterator it = LowerBound(Data, key);
  813. if (it == Data.end())
  814. return NULL;
  815. if (it->key != key)
  816. return NULL;
  817. return &it->val;
  818. }
  819. int ImGuiStorage::GetInt(ImU32 key, int default_val)
  820. {
  821. int* pval = Find(key);
  822. if (!pval)
  823. return default_val;
  824. return *pval;
  825. }
  826. // FIXME-OPT: We are wasting time because all SetInt() are preceeded by GetInt() calls so we should have the result from lower_bound already in place.
  827. // However we only use SetInt() on explicit user action (so that's maximum once a frame) so the optimisation isn't much needed.
  828. void ImGuiStorage::SetInt(ImU32 key, int val)
  829. {
  830. ImVector<Pair>::iterator it = LowerBound(Data, key);
  831. if (it != Data.end() && it->key == key)
  832. {
  833. it->val = val;
  834. }
  835. else
  836. {
  837. Pair pair_key;
  838. pair_key.key = key;
  839. pair_key.val = val;
  840. Data.insert(it, pair_key);
  841. }
  842. }
  843. void ImGuiStorage::SetAllInt(int v)
  844. {
  845. for (size_t i = 0; i < Data.size(); i++)
  846. Data[i].val = v;
  847. }
  848. //-----------------------------------------------------------------------------
  849. // Helper: Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]"
  850. ImGuiTextFilter::ImGuiTextFilter()
  851. {
  852. InputBuf[0] = 0;
  853. CountGrep = 0;
  854. }
  855. void ImGuiTextFilter::Draw(const char* label, float width)
  856. {
  857. ImGuiWindow* window = GetCurrentWindow();
  858. if (width < 0.0f)
  859. {
  860. ImVec2 label_size = ImGui::CalcTextSize(label, NULL);
  861. width = ImMax(window->Pos.x + ImGui::GetContentRegionMax().x - window->DC.CursorPos.x - (label_size.x + GImGui.Style.ItemSpacing.x*4), 10.0f);
  862. }
  863. ImGui::PushItemWidth(width);
  864. ImGui::InputText(label, InputBuf, IM_ARRAYSIZE(InputBuf));
  865. ImGui::PopItemWidth();
  866. Build();
  867. }
  868. void ImGuiTextFilter::TextRange::split(char separator, ImVector<TextRange>& out)
  869. {
  870. out.resize(0);
  871. const char* wb = b;
  872. const char* we = wb;
  873. while (we < e)
  874. {
  875. if (*we == separator)
  876. {
  877. out.push_back(TextRange(wb, we));
  878. wb = we + 1;
  879. }
  880. we++;
  881. }
  882. if (wb != we)
  883. out.push_back(TextRange(wb, we));
  884. }
  885. void ImGuiTextFilter::Build()
  886. {
  887. Filters.resize(0);
  888. TextRange input_range(InputBuf, InputBuf+strlen(InputBuf));
  889. input_range.split(',', Filters);
  890. CountGrep = 0;
  891. for (size_t i = 0; i != Filters.size(); i++)
  892. {
  893. Filters[i].trim_blanks();
  894. if (Filters[i].empty())
  895. continue;
  896. if (Filters[i].front() != '-')
  897. CountGrep += 1;
  898. }
  899. }
  900. bool ImGuiTextFilter::PassFilter(const char* val) const
  901. {
  902. if (Filters.empty())
  903. return true;
  904. if (val == NULL)
  905. val = "";
  906. for (size_t i = 0; i != Filters.size(); i++)
  907. {
  908. const TextRange& f = Filters[i];
  909. if (f.empty())
  910. continue;
  911. if (f.front() == '-')
  912. {
  913. // Subtract
  914. if (ImStristr(val, f.begin()+1, f.end()) != NULL)
  915. return false;
  916. }
  917. else
  918. {
  919. // Grep
  920. if (ImStristr(val, f.begin(), f.end()) != NULL)
  921. return true;
  922. }
  923. }
  924. // Implicit * grep
  925. if (CountGrep == 0)
  926. return true;
  927. return false;
  928. }
  929. //-----------------------------------------------------------------------------
  930. // Helper: Text buffer for logging/accumulating text
  931. void ImGuiTextBuffer::append(const char* fmt, ...)
  932. {
  933. va_list args;
  934. va_start(args, fmt);
  935. int len = vsnprintf(NULL, 0, fmt, args);
  936. va_end(args);
  937. if (len <= 0)
  938. return;
  939. const size_t write_off = Buf.size();
  940. if (write_off + (size_t)len >= Buf.capacity())
  941. Buf.reserve(Buf.capacity() * 2);
  942. Buf.resize(write_off + (size_t)len);
  943. va_start(args, fmt);
  944. ImFormatStringV(&Buf[write_off] - 1, (size_t)len+1, fmt, args);
  945. va_end(args);
  946. }
  947. //-----------------------------------------------------------------------------
  948. ImGuiWindow::ImGuiWindow(const char* name, ImVec2 default_pos, ImVec2 default_size)
  949. {
  950. Name = ImStrdup(name);
  951. ID = GetID(name);
  952. IDStack.push_back(ID);
  953. PosFloat = default_pos;
  954. Pos = ImVec2((float)(int)PosFloat.x, (float)(int)PosFloat.y);
  955. Size = SizeFull = default_size;
  956. SizeContentsFit = ImVec2(0.0f, 0.0f);
  957. ScrollY = 0.0f;
  958. NextScrollY = 0.0f;
  959. ScrollbarY = false;
  960. Visible = false;
  961. Accessed = false;
  962. Collapsed = false;
  963. SkipItems = false;
  964. AutoFitFrames = -1;
  965. AutoFitOnlyGrows = false;
  966. LastFrameDrawn = -1;
  967. ItemWidthDefault = 0.0f;
  968. FontWindowScale = 1.0f;
  969. if (ImLength(Size) < 0.001f)
  970. {
  971. AutoFitFrames = 2;
  972. AutoFitOnlyGrows = true;
  973. }
  974. DrawList = (ImDrawList*)ImGui::MemAlloc(sizeof(ImDrawList));
  975. new(DrawList) ImDrawList();
  976. FocusIdxAllCounter = FocusIdxTabCounter = -1;
  977. FocusIdxAllRequestCurrent = FocusIdxTabRequestCurrent = IM_INT_MAX;
  978. FocusIdxAllRequestNext = FocusIdxTabRequestNext = IM_INT_MAX;
  979. }
  980. ImGuiWindow::~ImGuiWindow()
  981. {
  982. DrawList->~ImDrawList();
  983. ImGui::MemFree(DrawList);
  984. DrawList = NULL;
  985. ImGui::MemFree(Name);
  986. Name = NULL;
  987. }
  988. ImGuiID ImGuiWindow::GetID(const char* str)
  989. {
  990. const ImGuiID seed = IDStack.empty() ? 0 : IDStack.back();
  991. const ImGuiID id = crc32(str, strlen(str), seed); // FIXME-OPT: crc32 function/variant should handle zero-terminated strings
  992. RegisterAliveId(id);
  993. return id;
  994. }
  995. ImGuiID ImGuiWindow::GetID(const void* ptr)
  996. {
  997. const ImGuiID seed = IDStack.empty() ? 0 : IDStack.back();
  998. const ImGuiID id = crc32(&ptr, sizeof(void*), seed);
  999. RegisterAliveId(id);
  1000. return id;
  1001. }
  1002. bool ImGuiWindow::FocusItemRegister(bool is_active, bool tab_stop)
  1003. {
  1004. ImGuiState& g = GImGui;
  1005. ImGuiWindow* window = GetCurrentWindow();
  1006. const bool allow_keyboard_focus = window->DC.AllowKeyboardFocus.back();
  1007. FocusIdxAllCounter++;
  1008. if (allow_keyboard_focus)
  1009. FocusIdxTabCounter++;
  1010. if (is_active)
  1011. window->DC.LastItemFocused = true;
  1012. // Process keyboard input at this point: TAB, Shift-TAB switch focus
  1013. // We can always TAB out of a widget that doesn't allow tabbing in.
  1014. if (tab_stop && FocusIdxAllRequestNext == IM_INT_MAX && FocusIdxTabRequestNext == IM_INT_MAX && is_active && IsKeyPressedMap(ImGuiKey_Tab))
  1015. {
  1016. // Modulo on index will be applied at the end of frame once we've got the total counter of items.
  1017. FocusIdxTabRequestNext = FocusIdxTabCounter + (g.IO.KeyShift ? (allow_keyboard_focus ? -1 : 0) : +1);
  1018. }
  1019. if (FocusIdxAllCounter == FocusIdxAllRequestCurrent)
  1020. return true;
  1021. if (allow_keyboard_focus)
  1022. if (FocusIdxTabCounter == FocusIdxTabRequestCurrent)
  1023. return true;
  1024. return false;
  1025. }
  1026. void ImGuiWindow::FocusItemUnregister()
  1027. {
  1028. FocusIdxAllCounter--;
  1029. FocusIdxTabCounter--;
  1030. }
  1031. void ImGuiWindow::AddToRenderList()
  1032. {
  1033. ImGuiState& g = GImGui;
  1034. if (!DrawList->commands.empty() && !DrawList->vtx_buffer.empty())
  1035. {
  1036. if (DrawList->commands.back().vtx_count == 0)
  1037. DrawList->commands.pop_back();
  1038. g.RenderDrawLists.push_back(DrawList);
  1039. }
  1040. for (size_t i = 0; i < DC.ChildWindows.size(); i++)
  1041. {
  1042. ImGuiWindow* child = DC.ChildWindows[i];
  1043. if (child->Visible) // clipped childs may have been marked not Visible
  1044. child->AddToRenderList();
  1045. }
  1046. }
  1047. //-----------------------------------------------------------------------------
  1048. void* ImGui::MemAlloc(size_t sz)
  1049. {
  1050. return GImGui.IO.MemAllocFn(sz);
  1051. }
  1052. void ImGui::MemFree(void* ptr)
  1053. {
  1054. return GImGui.IO.MemFreeFn(ptr);
  1055. }
  1056. void* ImGui::MemRealloc(void* ptr, size_t sz)
  1057. {
  1058. return GImGui.IO.MemReallocFn(ptr, sz);
  1059. }
  1060. static ImGuiIniData* FindWindowSettings(const char* name)
  1061. {
  1062. ImGuiState& g = GImGui;
  1063. for (size_t i = 0; i != g.Settings.size(); i++)
  1064. {
  1065. ImGuiIniData* ini = g.Settings[i];
  1066. if (ImStricmp(ini->Name, name) == 0)
  1067. return ini;
  1068. }
  1069. ImGuiIniData* ini = (ImGuiIniData*)ImGui::MemAlloc(sizeof(ImGuiIniData));
  1070. new(ini) ImGuiIniData();
  1071. ini->Name = ImStrdup(name);
  1072. ini->Collapsed = false;
  1073. ini->Pos = ImVec2(FLT_MAX,FLT_MAX);
  1074. ini->Size = ImVec2(0,0);
  1075. g.Settings.push_back(ini);
  1076. return ini;
  1077. }
  1078. // Zero-tolerance, poor-man .ini parsing
  1079. // FIXME: Write something less rubbish
  1080. static void LoadSettings()
  1081. {
  1082. ImGuiState& g = GImGui;
  1083. const char* filename = g.IO.IniFilename;
  1084. if (!filename)
  1085. return;
  1086. // Load file
  1087. FILE* f;
  1088. if ((f = fopen(filename, "rt")) == NULL)
  1089. return;
  1090. if (fseek(f, 0, SEEK_END))
  1091. {
  1092. fclose(f);
  1093. return;
  1094. }
  1095. const long f_size_signed = ftell(f);
  1096. if (f_size_signed == -1)
  1097. {
  1098. fclose(f);
  1099. return;
  1100. }
  1101. size_t f_size = (size_t)f_size_signed;
  1102. if (fseek(f, 0, SEEK_SET))
  1103. {
  1104. fclose(f);
  1105. return;
  1106. }
  1107. char* f_data = (char*)ImGui::MemAlloc(f_size+1);
  1108. f_size = fread(f_data, 1, f_size, f); // Text conversion alter read size so let's not be fussy about return value
  1109. fclose(f);
  1110. if (f_size == 0)
  1111. {
  1112. ImGui::MemFree(f_data);
  1113. return;
  1114. }
  1115. f_data[f_size] = 0;
  1116. ImGuiIniData* settings = NULL;
  1117. const char* buf_end = f_data + f_size;
  1118. for (const char* line_start = f_data; line_start < buf_end; )
  1119. {
  1120. const char* line_end = line_start;
  1121. while (line_end < buf_end && *line_end != '\n' && *line_end != '\r')
  1122. line_end++;
  1123. if (line_start[0] == '[' && line_end > line_start && line_end[-1] == ']')
  1124. {
  1125. char name[64];
  1126. ImFormatString(name, IM_ARRAYSIZE(name), "%.*s", line_end-line_start-2, line_start+1);
  1127. settings = FindWindowSettings(name);
  1128. }
  1129. else if (settings)
  1130. {
  1131. float x, y;
  1132. int i;
  1133. if (sscanf(line_start, "Pos=%f,%f", &x, &y) == 2)
  1134. settings->Pos = ImVec2(x, y);
  1135. else if (sscanf(line_start, "Size=%f,%f", &x, &y) == 2)
  1136. settings->Size = ImMax(ImVec2(x, y), g.Style.WindowMinSize);
  1137. else if (sscanf(line_start, "Collapsed=%d", &i) == 1)
  1138. settings->Collapsed = (i != 0);
  1139. }
  1140. line_start = line_end+1;
  1141. }
  1142. ImGui::MemFree(f_data);
  1143. }
  1144. static void SaveSettings()
  1145. {
  1146. ImGuiState& g = GImGui;
  1147. const char* filename = g.IO.IniFilename;
  1148. if (!filename)
  1149. return;
  1150. // Gather data from windows that were active during this session
  1151. for (size_t i = 0; i != g.Windows.size(); i++)
  1152. {
  1153. ImGuiWindow* window = g.Windows[i];
  1154. if (window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Tooltip))
  1155. continue;
  1156. ImGuiIniData* settings = FindWindowSettings(window->Name);
  1157. settings->Pos = window->Pos;
  1158. settings->Size = window->SizeFull;
  1159. settings->Collapsed = window->Collapsed;
  1160. }
  1161. // Write .ini file
  1162. // If a window wasn't opened in this session we preserve its settings
  1163. FILE* f = fopen(filename, "wt");
  1164. if (!f)
  1165. return;
  1166. for (size_t i = 0; i != g.Settings.size(); i++)
  1167. {
  1168. const ImGuiIniData* settings = g.Settings[i];
  1169. if (settings->Pos.x == FLT_MAX)
  1170. continue;
  1171. fprintf(f, "[%s]\n", settings->Name);
  1172. fprintf(f, "Pos=%d,%d\n", (int)settings->Pos.x, (int)settings->Pos.y);
  1173. fprintf(f, "Size=%d,%d\n", (int)settings->Size.x, (int)settings->Size.y);
  1174. fprintf(f, "Collapsed=%d\n", settings->Collapsed);
  1175. fprintf(f, "\n");
  1176. }
  1177. fclose(f);
  1178. }
  1179. static void MarkSettingsDirty()
  1180. {
  1181. ImGuiState& g = GImGui;
  1182. if (g.SettingsDirtyTimer <= 0.0f)
  1183. g.SettingsDirtyTimer = g.IO.IniSavingRate;
  1184. }
  1185. ImGuiIO& ImGui::GetIO()
  1186. {
  1187. return GImGui.IO;
  1188. }
  1189. ImGuiStyle& ImGui::GetStyle()
  1190. {
  1191. return GImGui.Style;
  1192. }
  1193. void ImGui::NewFrame()
  1194. {
  1195. ImGuiState& g = GImGui;
  1196. // Check user inputs
  1197. IM_ASSERT(g.IO.DeltaTime > 0.0f);
  1198. IM_ASSERT(g.IO.DisplaySize.x > 0.0f && g.IO.DisplaySize.y > 0.0f);
  1199. IM_ASSERT(g.IO.RenderDrawListsFn != NULL); // Must be implemented
  1200. if (!g.Initialized)
  1201. {
  1202. // Initialize on first frame
  1203. g.LogClipboard = (ImGuiTextBuffer*)ImGui::MemAlloc(sizeof(ImGuiTextBuffer));
  1204. new(g.LogClipboard) ImGuiTextBuffer();
  1205. IM_ASSERT(g.Settings.empty());
  1206. LoadSettings();
  1207. if (!g.IO.Font)
  1208. {
  1209. // Default font
  1210. const void* fnt_data;
  1211. unsigned int fnt_size;
  1212. ImGui::GetDefaultFontData(&fnt_data, &fnt_size, NULL, NULL);
  1213. g.IO.Font = (ImFont*)ImGui::MemAlloc(sizeof(ImFont));
  1214. new(g.IO.Font) ImFont();
  1215. g.IO.Font->LoadFromMemory(fnt_data, fnt_size);
  1216. IM_ASSERT(g.IO.Font->IsLoaded()); // Font failed to load
  1217. g.IO.Font->DisplayOffset = ImVec2(0.0f, +1.0f);
  1218. }
  1219. g.Initialized = true;
  1220. }
  1221. IM_ASSERT(g.IO.Font && g.IO.Font->IsLoaded()); // Font not loaded
  1222. IM_ASSERT(g.IO.Font->Scale > 0.0f);
  1223. g.FontSize = g.IO.FontGlobalScale * (float)g.IO.Font->Info->FontSize * g.IO.Font->Scale;
  1224. g.FontTexUvForWhite = g.IO.Font->TexUvForWhite;
  1225. g.IO.Font->FallbackGlyph = g.IO.Font->FindGlyph(g.IO.Font->FallbackChar);
  1226. g.Time += g.IO.DeltaTime;
  1227. g.FrameCount += 1;
  1228. g.Tooltip[0] = '\0';
  1229. // Update inputs state
  1230. if (g.IO.MousePos.x < 0 && g.IO.MousePos.y < 0)
  1231. g.IO.MousePos = ImVec2(-9999.0f, -9999.0f);
  1232. 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
  1233. g.IO.MouseDelta = ImVec2(0.0f, 0.0f);
  1234. else
  1235. g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev;
  1236. g.IO.MousePosPrev = g.IO.MousePos;
  1237. for (size_t i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++)
  1238. {
  1239. g.IO.MouseDownTime[i] = g.IO.MouseDown[i] ? (g.IO.MouseDownTime[i] < 0.0f ? 0.0f : g.IO.MouseDownTime[i] + g.IO.DeltaTime) : -1.0f;
  1240. g.IO.MouseClicked[i] = (g.IO.MouseDownTime[i] == 0.0f);
  1241. g.IO.MouseDoubleClicked[i] = false;
  1242. if (g.IO.MouseClicked[i])
  1243. {
  1244. if (g.Time - g.IO.MouseClickedTime[i] < g.IO.MouseDoubleClickTime)
  1245. {
  1246. if (ImLength(g.IO.MousePos - g.IO.MouseClickedPos[i]) < g.IO.MouseDoubleClickMaxDist)
  1247. g.IO.MouseDoubleClicked[i] = true;
  1248. g.IO.MouseClickedTime[i] = -FLT_MAX; // so the third click isn't turned into a double-click
  1249. }
  1250. else
  1251. {
  1252. g.IO.MouseClickedTime[i] = g.Time;
  1253. g.IO.MouseClickedPos[i] = g.IO.MousePos;
  1254. }
  1255. }
  1256. }
  1257. for (size_t i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++)
  1258. g.IO.KeysDownTime[i] = g.IO.KeysDown[i] ? (g.IO.KeysDownTime[i] < 0.0f ? 0.0f : g.IO.KeysDownTime[i] + g.IO.DeltaTime) : -1.0f;
  1259. // Clear reference to active widget if the widget isn't alive anymore
  1260. g.HoveredId = 0;
  1261. if (!g.ActiveIdIsAlive && g.ActiveIdPreviousFrame == g.ActiveId && g.ActiveId != 0)
  1262. g.ActiveId = 0;
  1263. g.ActiveIdPreviousFrame = g.ActiveId;
  1264. g.ActiveIdIsAlive = false;
  1265. // Delay saving settings so we don't spam disk too much
  1266. if (g.SettingsDirtyTimer > 0.0f)
  1267. {
  1268. g.SettingsDirtyTimer -= g.IO.DeltaTime;
  1269. if (g.SettingsDirtyTimer <= 0.0f)
  1270. SaveSettings();
  1271. }
  1272. g.HoveredWindow = FindHoveredWindow(g.IO.MousePos, false);
  1273. g.HoveredWindowExcludingChilds = FindHoveredWindow(g.IO.MousePos, true);
  1274. // Are we using inputs? Tell user so they can capture/discard them.
  1275. g.IO.WantCaptureMouse = (g.HoveredWindow != NULL) || (g.ActiveId != 0);
  1276. g.IO.WantCaptureKeyboard = (g.ActiveId != 0);
  1277. // Scale & Scrolling
  1278. if (g.HoveredWindow && g.IO.MouseWheel != 0.0f)
  1279. {
  1280. ImGuiWindow* window = g.HoveredWindow;
  1281. if (g.IO.KeyCtrl)
  1282. {
  1283. if (g.IO.FontAllowUserScaling)
  1284. {
  1285. // Zoom / Scale window
  1286. float new_font_scale = ImClamp(window->FontWindowScale + g.IO.MouseWheel * 0.10f, 0.50f, 2.50f);
  1287. float scale = new_font_scale / window->FontWindowScale;
  1288. window->FontWindowScale = new_font_scale;
  1289. const ImVec2 offset = window->Size * (1.0f - scale) * (g.IO.MousePos - window->Pos) / window->Size;
  1290. window->Pos += offset;
  1291. window->PosFloat += offset;
  1292. window->Size *= scale;
  1293. window->SizeFull *= scale;
  1294. }
  1295. }
  1296. else
  1297. {
  1298. // Scroll
  1299. const int scroll_lines = (window->Flags & ImGuiWindowFlags_ComboBox) ? 3 : 5;
  1300. window->NextScrollY -= g.IO.MouseWheel * window->FontSize() * scroll_lines;
  1301. }
  1302. }
  1303. // Pressing TAB activate widget focus
  1304. // 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.
  1305. if (g.ActiveId == 0 && g.FocusedWindow != NULL && g.FocusedWindow->Visible && IsKeyPressedMap(ImGuiKey_Tab, false))
  1306. {
  1307. g.FocusedWindow->FocusIdxTabRequestNext = 0;
  1308. }
  1309. // Mark all windows as not visible
  1310. for (size_t i = 0; i != g.Windows.size(); i++)
  1311. {
  1312. ImGuiWindow* window = g.Windows[i];
  1313. window->Visible = false;
  1314. window->Accessed = false;
  1315. }
  1316. // No window should be open at the beginning of the frame.
  1317. // But in order to allow the user to call NewFrame() multiple times without calling Render(), we are doing an explicit clear.
  1318. g.CurrentWindowStack.clear();
  1319. // Create implicit window - we will only render it if the user has added something to it.
  1320. ImGui::Begin("Debug", NULL, ImVec2(400,400));
  1321. }
  1322. // NB: behaviour of ImGui after Shutdown() is not tested/guaranteed at the moment. This function is merely here to free heap allocations.
  1323. void ImGui::Shutdown()
  1324. {
  1325. ImGuiState& g = GImGui;
  1326. if (!g.Initialized)
  1327. return;
  1328. SaveSettings();
  1329. for (size_t i = 0; i < g.Windows.size(); i++)
  1330. {
  1331. g.Windows[i]->~ImGuiWindow();
  1332. ImGui::MemFree(g.Windows[i]);
  1333. }
  1334. g.Windows.clear();
  1335. g.CurrentWindowStack.clear();
  1336. g.RenderDrawLists.clear();
  1337. g.FocusedWindow = NULL;
  1338. g.HoveredWindow = NULL;
  1339. g.HoveredWindowExcludingChilds = NULL;
  1340. for (size_t i = 0; i < g.Settings.size(); i++)
  1341. {
  1342. g.Settings[i]->~ImGuiIniData();
  1343. ImGui::MemFree(g.Settings[i]);
  1344. }
  1345. g.Settings.clear();
  1346. g.ColorEditModeStorage.Clear();
  1347. if (g.LogFile && g.LogFile != stdout)
  1348. {
  1349. fclose(g.LogFile);
  1350. g.LogFile = NULL;
  1351. }
  1352. if (g.IO.Font)
  1353. {
  1354. g.IO.Font->~ImFont();
  1355. ImGui::MemFree(g.IO.Font);
  1356. g.IO.Font = NULL;
  1357. }
  1358. if (g.PrivateClipboard)
  1359. {
  1360. ImGui::MemFree(g.PrivateClipboard);
  1361. g.PrivateClipboard = NULL;
  1362. }
  1363. if (g.LogClipboard)
  1364. {
  1365. g.LogClipboard->~ImGuiTextBuffer();
  1366. ImGui::MemFree(g.LogClipboard);
  1367. }
  1368. g.Initialized = false;
  1369. }
  1370. static void AddWindowToSortedBuffer(ImGuiWindow* window, ImVector<ImGuiWindow*>& sorted_windows)
  1371. {
  1372. sorted_windows.push_back(window);
  1373. if (window->Visible)
  1374. {
  1375. for (size_t i = 0; i < window->DC.ChildWindows.size(); i++)
  1376. {
  1377. ImGuiWindow* child = window->DC.ChildWindows[i];
  1378. if (child->Visible)
  1379. AddWindowToSortedBuffer(child, sorted_windows);
  1380. }
  1381. }
  1382. }
  1383. static void PushClipRect(const ImVec4& clip_rect, bool clipped = true)
  1384. {
  1385. ImGuiWindow* window = GetCurrentWindow();
  1386. ImVec4 cr = clip_rect;
  1387. if (clipped && !window->ClipRectStack.empty())
  1388. {
  1389. // Clip to new clip rect
  1390. const ImVec4 cur_cr = window->ClipRectStack.back();
  1391. 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));
  1392. }
  1393. window->ClipRectStack.push_back(cr);
  1394. window->DrawList->PushClipRect(cr);
  1395. }
  1396. static void PopClipRect()
  1397. {
  1398. ImGuiWindow* window = GetCurrentWindow();
  1399. window->ClipRectStack.pop_back();
  1400. window->DrawList->PopClipRect();
  1401. }
  1402. void ImGui::Render()
  1403. {
  1404. ImGuiState& g = GImGui;
  1405. IM_ASSERT(g.Initialized); // Forgot to call ImGui::NewFrame()
  1406. const bool first_render_of_the_frame = (g.FrameCountRendered != g.FrameCount);
  1407. g.FrameCountRendered = g.FrameCount;
  1408. if (first_render_of_the_frame)
  1409. {
  1410. // Hide implicit window if it hasn't been used
  1411. IM_ASSERT(g.CurrentWindowStack.size() == 1); // Mismatched Begin/End
  1412. if (g.CurrentWindow && !g.CurrentWindow->Accessed)
  1413. g.CurrentWindow->Visible = false;
  1414. ImGui::End();
  1415. // Sort the window list so that all child windows are after their parent
  1416. // We cannot do that on FocusWindow() because childs may not exist yet
  1417. ImVector<ImGuiWindow*> sorted_windows;
  1418. sorted_windows.reserve(g.Windows.size());
  1419. for (size_t i = 0; i != g.Windows.size(); i++)
  1420. {
  1421. ImGuiWindow* window = g.Windows[i];
  1422. if (window->Flags & ImGuiWindowFlags_ChildWindow) // if a child is visible its parent will add it
  1423. if (window->Visible)
  1424. continue;
  1425. AddWindowToSortedBuffer(window, sorted_windows);
  1426. }
  1427. IM_ASSERT(g.Windows.size() == sorted_windows.size()); // We done something wrong
  1428. g.Windows.swap(sorted_windows);
  1429. // Clear data for next frame
  1430. g.IO.MouseWheel = 0.0f;
  1431. memset(g.IO.InputCharacters, 0, sizeof(g.IO.InputCharacters));
  1432. }
  1433. // Skip render altogether if alpha is 0.0
  1434. // Note that vertex buffers have been created, so it is best practice that you don't call Begin/End in the first place.
  1435. if (g.Style.Alpha > 0.0f)
  1436. {
  1437. // Render tooltip
  1438. if (g.Tooltip[0])
  1439. {
  1440. // Use a dummy window to render the tooltip
  1441. ImGui::BeginTooltip();
  1442. ImGui::TextUnformatted(g.Tooltip);
  1443. ImGui::EndTooltip();
  1444. }
  1445. // Gather windows to render
  1446. g.RenderDrawLists.resize(0);
  1447. for (size_t i = 0; i != g.Windows.size(); i++)
  1448. {
  1449. ImGuiWindow* window = g.Windows[i];
  1450. if (window->Visible && (window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Tooltip)) == 0)
  1451. window->AddToRenderList();
  1452. }
  1453. for (size_t i = 0; i != g.Windows.size(); i++)
  1454. {
  1455. ImGuiWindow* window = g.Windows[i];
  1456. if (window->Visible && (window->Flags & ImGuiWindowFlags_Tooltip))
  1457. window->AddToRenderList();
  1458. }
  1459. // Render
  1460. if (!g.RenderDrawLists.empty())
  1461. g.IO.RenderDrawListsFn(&g.RenderDrawLists[0], (int)g.RenderDrawLists.size());
  1462. g.RenderDrawLists.resize(0);
  1463. }
  1464. }
  1465. // Find the optional ## from which we stop displaying text.
  1466. static const char* FindTextDisplayEnd(const char* text, const char* text_end = NULL)
  1467. {
  1468. const char* text_display_end = text;
  1469. while ((!text_end || text_display_end < text_end) && *text_display_end != '\0' && (text_display_end[0] != '#' || text_display_end[1] != '#'))
  1470. text_display_end++;
  1471. return text_display_end;
  1472. }
  1473. // Log ImGui display into text output (tty or file or clipboard)
  1474. static void LogText(const ImVec2& ref_pos, const char* text, const char* text_end)
  1475. {
  1476. ImGuiState& g = GImGui;
  1477. ImGuiWindow* window = GetCurrentWindow();
  1478. if (!text_end)
  1479. text_end = FindTextDisplayEnd(text, text_end);
  1480. const bool log_new_line = ref_pos.y > window->DC.LogLineHeight+1;
  1481. window->DC.LogLineHeight = ref_pos.y;
  1482. const char* text_remaining = text;
  1483. const int tree_depth = window->DC.TreeDepth;
  1484. while (true)
  1485. {
  1486. const char* line_end = text_remaining;
  1487. while (line_end < text_end)
  1488. if (*line_end == '\n')
  1489. break;
  1490. else
  1491. line_end++;
  1492. if (line_end >= text_end)
  1493. line_end = NULL;
  1494. const bool is_first_line = (text == text_remaining);
  1495. bool is_last_line = false;
  1496. if (line_end == NULL)
  1497. {
  1498. is_last_line = true;
  1499. line_end = text_end;
  1500. }
  1501. if (line_end != NULL && !(is_last_line && (line_end - text_remaining)==0))
  1502. {
  1503. const int char_count = (int)(line_end - text_remaining);
  1504. if (g.LogFile)
  1505. {
  1506. if (log_new_line || !is_first_line)
  1507. fprintf(g.LogFile, "\n%*s%.*s", tree_depth*4, "", char_count, text_remaining);
  1508. else
  1509. fprintf(g.LogFile, " %.*s", char_count, text_remaining);
  1510. }
  1511. else
  1512. {
  1513. if (log_new_line || !is_first_line)
  1514. g.LogClipboard->append("\n%*s%.*s", tree_depth*4, "", char_count, text_remaining);
  1515. else
  1516. g.LogClipboard->append(" %.*s", char_count, text_remaining);
  1517. }
  1518. }
  1519. if (is_last_line)
  1520. break;
  1521. text_remaining = line_end + 1;
  1522. }
  1523. }
  1524. static float CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x)
  1525. {
  1526. if (wrap_pos_x < 0.0f)
  1527. return 0.0f;
  1528. ImGuiWindow* window = GetCurrentWindow();
  1529. if (wrap_pos_x == 0.0f)
  1530. wrap_pos_x = ImGui::GetContentRegionMax().x;
  1531. if (wrap_pos_x > 0.0f)
  1532. wrap_pos_x += window->Pos.x; // wrap_pos_x is provided is window local space
  1533. const float wrap_width = wrap_pos_x > 0.0f ? ImMax(wrap_pos_x - pos.x, 0.00001f) : 0.0f;
  1534. return wrap_width;
  1535. }
  1536. // Internal ImGui function to render text (called from ImGui::Text(), ImGui::TextUnformatted(), etc.)
  1537. // RenderText() calls ImDrawList::AddText() calls ImBitmapFont::RenderText()
  1538. static void RenderText(ImVec2 pos, const char* text, const char* text_end, bool hide_text_after_hash, float wrap_width)
  1539. {
  1540. ImGuiState& g = GImGui;
  1541. ImGuiWindow* window = GetCurrentWindow();
  1542. // Hide anything after a '##' string
  1543. const char* text_display_end;
  1544. if (hide_text_after_hash)
  1545. {
  1546. text_display_end = FindTextDisplayEnd(text, text_end);
  1547. }
  1548. else
  1549. {
  1550. if (!text_end)
  1551. text_end = text + strlen(text); // FIXME-OPT
  1552. text_display_end = text_end;
  1553. }
  1554. const int text_len = (int)(text_display_end - text);
  1555. if (text_len > 0)
  1556. {
  1557. // Render
  1558. window->DrawList->AddText(window->Font(), window->FontSize(), pos, window->Color(ImGuiCol_Text), text, text + text_len, wrap_width);
  1559. // Log as text. We split text into individual lines to add current tree level padding
  1560. if (g.LogEnabled)
  1561. LogText(pos, text, text_display_end);
  1562. }
  1563. }
  1564. // Render a rectangle shaped with optional rounding and borders
  1565. static void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border, float rounding)
  1566. {
  1567. ImGuiWindow* window = GetCurrentWindow();
  1568. window->DrawList->AddRectFilled(p_min, p_max, fill_col, rounding);
  1569. if (border && (window->Flags & ImGuiWindowFlags_ShowBorders))
  1570. {
  1571. // FIXME: This is the best I've found that works on multiple renderer/back ends. Rather dodgy.
  1572. const float offset = GImGui.IO.PixelCenterOffset;
  1573. window->DrawList->AddRect(p_min+ImVec2(1.5f-offset,1.5f-offset), p_max+ImVec2(1.0f-offset*2,1.0f-offset*2), window->Color(ImGuiCol_BorderShadow), rounding);
  1574. window->DrawList->AddRect(p_min+ImVec2(0.5f-offset,0.5f-offset), p_max+ImVec2(0.0f-offset*2,0.0f-offset*2), window->Color(ImGuiCol_Border), rounding);
  1575. }
  1576. }
  1577. // Render a triangle to denote expanded/collapsed state
  1578. static void RenderCollapseTriangle(ImVec2 p_min, bool open, float scale, bool shadow)
  1579. {
  1580. ImGuiWindow* window = GetCurrentWindow();
  1581. const float h = window->FontSize() * 1.00f;
  1582. const float r = h * 0.40f * scale;
  1583. ImVec2 center = p_min + ImVec2(h*0.50f, h*0.50f*scale);
  1584. ImVec2 a, b, c;
  1585. if (open)
  1586. {
  1587. center.y -= r*0.25f;
  1588. a = center + ImVec2(0,1)*r;
  1589. b = center + ImVec2(-0.866f,-0.5f)*r;
  1590. c = center + ImVec2(0.866f,-0.5f)*r;
  1591. }
  1592. else
  1593. {
  1594. a = center + ImVec2(1,0)*r;
  1595. b = center + ImVec2(-0.500f,0.866f)*r;
  1596. c = center + ImVec2(-0.500f,-0.866f)*r;
  1597. }
  1598. if (shadow && (window->Flags & ImGuiWindowFlags_ShowBorders) != 0)
  1599. window->DrawList->AddTriangleFilled(a+ImVec2(2,2), b+ImVec2(2,2), c+ImVec2(2,2), window->Color(ImGuiCol_BorderShadow));
  1600. window->DrawList->AddTriangleFilled(a, b, c, window->Color(ImGuiCol_Border));
  1601. }
  1602. // Calculate text size. Text can be multi-line. Optionally ignore text after a ## marker.
  1603. // CalcTextSize("") should return ImVec2(0.0f, GImGui.FontSize)
  1604. ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_text_after_double_hash, float wrap_width)
  1605. {
  1606. ImGuiWindow* window = GetCurrentWindow();
  1607. const char* text_display_end;
  1608. if (hide_text_after_double_hash)
  1609. text_display_end = FindTextDisplayEnd(text, text_end); // Hide anything after a '##' string
  1610. else
  1611. text_display_end = text_end;
  1612. const ImVec2 text_size = window->Font()->CalcTextSizeA(window->FontSize(), FLT_MAX, wrap_width, text, text_display_end, NULL);
  1613. return text_size;
  1614. }
  1615. // Find window given position, search front-to-back
  1616. static ImGuiWindow* FindHoveredWindow(ImVec2 pos, bool excluding_childs)
  1617. {
  1618. ImGuiState& g = GImGui;
  1619. for (int i = (int)g.Windows.size()-1; i >= 0; i--)
  1620. {
  1621. ImGuiWindow* window = g.Windows[(size_t)i];
  1622. if (!window->Visible)
  1623. continue;
  1624. if (excluding_childs && (window->Flags & ImGuiWindowFlags_ChildWindow) != 0)
  1625. continue;
  1626. ImGuiAabb bb(window->Pos - g.Style.TouchExtraPadding, window->Pos+window->Size + g.Style.TouchExtraPadding);
  1627. if (bb.Contains(pos))
  1628. return window;
  1629. }
  1630. return NULL;
  1631. }
  1632. // Test if mouse cursor is hovering given aabb
  1633. // NB- Box is clipped by our current clip setting
  1634. // NB- Expand the aabb to be generous on imprecise inputs systems (g.Style.TouchExtraPadding)
  1635. static bool IsMouseHoveringBox(const ImGuiAabb& box)
  1636. {
  1637. ImGuiState& g = GImGui;
  1638. ImGuiWindow* window = GetCurrentWindow();
  1639. // Clip
  1640. ImGuiAabb box_clipped = box;
  1641. if (!window->ClipRectStack.empty())
  1642. {
  1643. const ImVec4 clip_rect = window->ClipRectStack.back();
  1644. box_clipped.Clip(ImGuiAabb(ImVec2(clip_rect.x, clip_rect.y), ImVec2(clip_rect.z, clip_rect.w)));
  1645. }
  1646. // Expand for touch input
  1647. const ImGuiAabb box_for_touch(box_clipped.Min - g.Style.TouchExtraPadding, box_clipped.Max + g.Style.TouchExtraPadding);
  1648. return box_for_touch.Contains(g.IO.MousePos);
  1649. }
  1650. bool ImGui::IsMouseHoveringBox(const ImVec2& box_min, const ImVec2& box_max)
  1651. {
  1652. return IsMouseHoveringBox(ImGuiAabb(box_min, box_max));
  1653. }
  1654. bool ImGui::IsMouseHoveringWindow()
  1655. {
  1656. ImGuiState& g = GImGui;
  1657. ImGuiWindow* window = GetCurrentWindow();
  1658. return g.HoveredWindow == window;
  1659. }
  1660. bool ImGui::IsMouseHoveringAnyWindow()
  1661. {
  1662. ImGuiState& g = GImGui;
  1663. return g.HoveredWindow != NULL;
  1664. }
  1665. bool ImGui::IsPosHoveringAnyWindow(const ImVec2& pos)
  1666. {
  1667. return FindHoveredWindow(pos, false) != NULL;
  1668. }
  1669. static bool IsKeyPressedMap(ImGuiKey key, bool repeat)
  1670. {
  1671. ImGuiState& g = GImGui;
  1672. const int key_index = g.IO.KeyMap[key];
  1673. return ImGui::IsKeyPressed(key_index, repeat);
  1674. }
  1675. bool ImGui::IsKeyPressed(int key_index, bool repeat)
  1676. {
  1677. ImGuiState& g = GImGui;
  1678. IM_ASSERT(key_index >= 0 && key_index < IM_ARRAYSIZE(g.IO.KeysDown));
  1679. const float t = g.IO.KeysDownTime[key_index];
  1680. if (t == 0.0f)
  1681. return true;
  1682. // FIXME: Repeat rate should be provided elsewhere?
  1683. const float KEY_REPEAT_DELAY = 0.250f;
  1684. const float KEY_REPEAT_RATE = 0.020f;
  1685. if (repeat && t > KEY_REPEAT_DELAY)
  1686. if ((fmodf(t - KEY_REPEAT_DELAY, KEY_REPEAT_RATE) > KEY_REPEAT_RATE*0.5f) != (fmodf(t - KEY_REPEAT_DELAY - g.IO.DeltaTime, KEY_REPEAT_RATE) > KEY_REPEAT_RATE*0.5f))
  1687. return true;
  1688. return false;
  1689. }
  1690. bool ImGui::IsMouseClicked(int button, bool repeat)
  1691. {
  1692. ImGuiState& g = GImGui;
  1693. IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));
  1694. const float t = g.IO.MouseDownTime[button];
  1695. if (t == 0.0f)
  1696. return true;
  1697. // FIXME: Repeat rate should be provided elsewhere?
  1698. const float MOUSE_REPEAT_DELAY = 0.250f;
  1699. const float MOUSE_REPEAT_RATE = 0.020f;
  1700. if (repeat && t > MOUSE_REPEAT_DELAY)
  1701. if ((fmodf(t - MOUSE_REPEAT_DELAY, MOUSE_REPEAT_RATE) > MOUSE_REPEAT_RATE*0.5f) != (fmodf(t - MOUSE_REPEAT_DELAY - g.IO.DeltaTime, MOUSE_REPEAT_RATE) > MOUSE_REPEAT_RATE*0.5f))
  1702. return true;
  1703. return false;
  1704. }
  1705. bool ImGui::IsMouseDoubleClicked(int button)
  1706. {
  1707. ImGuiState& g = GImGui;
  1708. IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));
  1709. return g.IO.MouseDoubleClicked[button];
  1710. }
  1711. ImVec2 ImGui::GetMousePos()
  1712. {
  1713. return GImGui.IO.MousePos;
  1714. }
  1715. bool ImGui::IsItemHovered()
  1716. {
  1717. ImGuiWindow* window = GetCurrentWindow();
  1718. return window->DC.LastItemHovered;
  1719. }
  1720. bool ImGui::IsItemFocused()
  1721. {
  1722. ImGuiWindow* window = GetCurrentWindow();
  1723. return window->DC.LastItemFocused;
  1724. }
  1725. ImVec2 ImGui::GetItemBoxMin()
  1726. {
  1727. ImGuiWindow* window = GetCurrentWindow();
  1728. return window->DC.LastItemAabb.Min;
  1729. }
  1730. ImVec2 ImGui::GetItemBoxMax()
  1731. {
  1732. ImGuiWindow* window = GetCurrentWindow();
  1733. return window->DC.LastItemAabb.Max;
  1734. }
  1735. // Tooltip is stored and turned into a BeginTooltip()/EndTooltip() sequence at the end of the frame. Each call override previous value.
  1736. void ImGui::SetTooltipV(const char* fmt, va_list args)
  1737. {
  1738. ImGuiState& g = GImGui;
  1739. ImFormatStringV(g.Tooltip, IM_ARRAYSIZE(g.Tooltip), fmt, args);
  1740. }
  1741. void ImGui::SetTooltip(const char* fmt, ...)
  1742. {
  1743. va_list args;
  1744. va_start(args, fmt);
  1745. SetTooltipV(fmt, args);
  1746. va_end(args);
  1747. }
  1748. // Position new window if they don't have position setting in the .ini file. Rarely useful (used by the sample applications).
  1749. void ImGui::SetNewWindowDefaultPos(const ImVec2& pos)
  1750. {
  1751. ImGuiState& g = GImGui;
  1752. g.NewWindowDefaultPos = pos;
  1753. }
  1754. float ImGui::GetTime()
  1755. {
  1756. return GImGui.Time;
  1757. }
  1758. int ImGui::GetFrameCount()
  1759. {
  1760. return GImGui.FrameCount;
  1761. }
  1762. static ImGuiWindow* FindWindow(const char* name)
  1763. {
  1764. ImGuiState& g = GImGui;
  1765. for (size_t i = 0; i != g.Windows.size(); i++)
  1766. if (strcmp(g.Windows[i]->Name, name) == 0)
  1767. return g.Windows[i];
  1768. return NULL;
  1769. }
  1770. void ImGui::BeginTooltip()
  1771. {
  1772. ImGui::Begin("##Tooltip", NULL, ImVec2(0,0), 0.9f, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_Tooltip);
  1773. }
  1774. void ImGui::EndTooltip()
  1775. {
  1776. IM_ASSERT(GetCurrentWindow()->Flags & ImGuiWindowFlags_Tooltip);
  1777. ImGui::End();
  1778. }
  1779. void ImGui::BeginChild(const char* str_id, ImVec2 size, bool border, ImGuiWindowFlags extra_flags)
  1780. {
  1781. ImGuiState& g = GImGui;
  1782. ImGuiWindow* window = GetCurrentWindow();
  1783. ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_ChildWindow;
  1784. const ImVec2 content_max = window->Pos + ImGui::GetContentRegionMax();
  1785. const ImVec2 cursor_pos = window->Pos + ImGui::GetCursorPos();
  1786. if (size.x <= 0.0f)
  1787. {
  1788. if (size.x == 0.0f)
  1789. flags |= ImGuiWindowFlags_ChildWindowAutoFitX;
  1790. size.x = ImMax(content_max.x - cursor_pos.x, g.Style.WindowMinSize.x) - fabsf(size.x);
  1791. }
  1792. if (size.y <= 0.0f)
  1793. {
  1794. if (size.y == 0.0f)
  1795. flags |= ImGuiWindowFlags_ChildWindowAutoFitY;
  1796. size.y = ImMax(content_max.y - cursor_pos.y, g.Style.WindowMinSize.y) - fabsf(size.y);
  1797. }
  1798. if (border)
  1799. flags |= ImGuiWindowFlags_ShowBorders;
  1800. flags |= extra_flags;
  1801. char title[256];
  1802. ImFormatString(title, IM_ARRAYSIZE(title), "%s.%s", window->Name, str_id);
  1803. const float alpha = (flags & ImGuiWindowFlags_ComboBox) ? 1.0f : 0.0f;
  1804. ImGui::Begin(title, NULL, size, alpha, flags);
  1805. if (!(window->Flags & ImGuiWindowFlags_ShowBorders))
  1806. g.CurrentWindow->Flags &= ~ImGuiWindowFlags_ShowBorders;
  1807. }
  1808. void ImGui::EndChild()
  1809. {
  1810. ImGuiWindow* window = GetCurrentWindow();
  1811. if (window->Flags & ImGuiWindowFlags_ComboBox)
  1812. {
  1813. ImGui::End();
  1814. }
  1815. else
  1816. {
  1817. // When using auto-filling child window, we don't provide the width/height to ItemSize so that it doesn't feed back into automatic size-fitting.
  1818. ImVec2 sz = ImGui::GetWindowSize();
  1819. if (window->Flags & ImGuiWindowFlags_ChildWindowAutoFitX)
  1820. sz.x = 0;
  1821. if (window->Flags & ImGuiWindowFlags_ChildWindowAutoFitY)
  1822. sz.y = 0;
  1823. ImGui::End();
  1824. ItemSize(sz);
  1825. }
  1826. }
  1827. // Push a new ImGui window to add widgets to. This can be called multiple times with the same window to append contents
  1828. bool ImGui::Begin(const char* name, bool* open, ImVec2 size, float fill_alpha, ImGuiWindowFlags flags)
  1829. {
  1830. ImGuiState& g = GImGui;
  1831. const ImGuiStyle& style = g.Style;
  1832. IM_ASSERT(g.Initialized); // Forgot to call ImGui::NewFrame()
  1833. ImGuiWindow* window = FindWindow(name);
  1834. if (!window)
  1835. {
  1836. // Create window the first time, and load settings
  1837. if (flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Tooltip))
  1838. {
  1839. // Tooltip and child windows don't store settings
  1840. window = (ImGuiWindow*)ImGui::MemAlloc(sizeof(ImGuiWindow));
  1841. new(window) ImGuiWindow(name, ImVec2(0,0), size);
  1842. }
  1843. else
  1844. {
  1845. // Normal windows store settings in .ini file
  1846. ImGuiIniData* settings = FindWindowSettings(name);
  1847. if (settings && ImLength(settings->Size) > 0.0f && !(flags & ImGuiWindowFlags_NoResize))// && ImLengthsize) == 0.0f)
  1848. size = settings->Size;
  1849. window = (ImGuiWindow*)ImGui::MemAlloc(sizeof(ImGuiWindow));
  1850. new(window) ImGuiWindow(name, g.NewWindowDefaultPos, size);
  1851. if (settings->Pos.x != FLT_MAX)
  1852. {
  1853. window->PosFloat = settings->Pos;
  1854. window->Pos = ImVec2((float)(int)window->PosFloat.x, (float)(int)window->PosFloat.y);
  1855. window->Collapsed = settings->Collapsed;
  1856. }
  1857. }
  1858. g.Windows.push_back(window);
  1859. }
  1860. window->Flags = (ImGuiWindowFlags)flags;
  1861. g.CurrentWindowStack.push_back(window);
  1862. g.CurrentWindow = window;
  1863. // Default alpha
  1864. if (fill_alpha < 0.0f)
  1865. fill_alpha = style.WindowFillAlphaDefault;
  1866. // When reusing window again multiple times a frame, just append content (don't need to setup again)
  1867. const int current_frame = ImGui::GetFrameCount();
  1868. const bool first_begin_of_the_frame = (window->LastFrameDrawn != current_frame);
  1869. if (first_begin_of_the_frame)
  1870. {
  1871. window->DrawList->Clear();
  1872. window->Visible = true;
  1873. // New windows appears in front
  1874. if (window->LastFrameDrawn < current_frame - 1)
  1875. {
  1876. FocusWindow(window);
  1877. if ((window->Flags & ImGuiWindowFlags_Tooltip) != 0)
  1878. {
  1879. // Hide for 1 frame while resizing
  1880. window->AutoFitFrames = 2;
  1881. window->AutoFitOnlyGrows = false;
  1882. window->Visible = false;
  1883. }
  1884. }
  1885. window->LastFrameDrawn = current_frame;
  1886. window->ClipRectStack.resize(0);
  1887. if (flags & ImGuiWindowFlags_ChildWindow)
  1888. {
  1889. ImGuiWindow* parent_window = g.CurrentWindowStack[g.CurrentWindowStack.size()-2];
  1890. parent_window->DC.ChildWindows.push_back(window);
  1891. window->Pos = window->PosFloat = parent_window->DC.CursorPos;
  1892. window->SizeFull = size;
  1893. }
  1894. // Outer clipping rectangle
  1895. if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_ComboBox))
  1896. PushClipRect(g.CurrentWindowStack[g.CurrentWindowStack.size()-2]->ClipRectStack.back());
  1897. else
  1898. PushClipRect(ImVec4(0.0f, 0.0f, g.IO.DisplaySize.x, g.IO.DisplaySize.y));
  1899. // Seed ID stack with our window pointer
  1900. window->IDStack.resize(0);
  1901. ImGui::PushID(window);
  1902. // Move window (at the beginning of the frame to avoid lag)
  1903. const ImGuiID move_id = window->GetID("#MOVE");
  1904. RegisterAliveId(move_id);
  1905. if (g.ActiveId == move_id)
  1906. {
  1907. if (g.IO.MouseDown[0])
  1908. {
  1909. if (!(window->Flags & ImGuiWindowFlags_NoMove))
  1910. {
  1911. window->PosFloat += g.IO.MouseDelta;
  1912. MarkSettingsDirty();
  1913. }
  1914. FocusWindow(window);
  1915. }
  1916. else
  1917. {
  1918. g.ActiveId = 0;
  1919. }
  1920. }
  1921. // Tooltips always follow mouse
  1922. if ((window->Flags & ImGuiWindowFlags_Tooltip) != 0)
  1923. {
  1924. window->PosFloat = g.IO.MousePos + ImVec2(32,16) - g.Style.FramePadding*2;
  1925. }
  1926. // Clamp into view
  1927. if (!(window->Flags & ImGuiWindowFlags_ChildWindow))
  1928. {
  1929. const ImVec2 pad = ImVec2(window->FontSize()*2.0f, window->FontSize()*2.0f);
  1930. window->PosFloat = ImMax(window->PosFloat + window->Size, pad) - window->Size;
  1931. window->PosFloat = ImMin(window->PosFloat, ImVec2(g.IO.DisplaySize.x, g.IO.DisplaySize.y) - pad);
  1932. window->SizeFull = ImMax(window->SizeFull, pad);
  1933. }
  1934. window->Pos = ImVec2((float)(int)window->PosFloat.x, (float)(int)window->PosFloat.y);
  1935. // Default item width
  1936. if (window->Size.x > 0.0f && !(window->Flags & ImGuiWindowFlags_Tooltip) && !(window->Flags & ImGuiWindowFlags_AlwaysAutoResize))
  1937. window->ItemWidthDefault = (float)(int)(window->Size.x * 0.65f);
  1938. else
  1939. window->ItemWidthDefault = 200.0f;
  1940. // Prepare for focus requests
  1941. if (window->FocusIdxAllRequestNext == IM_INT_MAX || window->FocusIdxAllCounter == -1)
  1942. window->FocusIdxAllRequestCurrent = IM_INT_MAX;
  1943. else
  1944. window->FocusIdxAllRequestCurrent = (window->FocusIdxAllRequestNext + (window->FocusIdxAllCounter+1)) % (window->FocusIdxAllCounter+1);
  1945. if (window->FocusIdxTabRequestNext == IM_INT_MAX || window->FocusIdxTabCounter == -1)
  1946. window->FocusIdxTabRequestCurrent = IM_INT_MAX;
  1947. else
  1948. window->FocusIdxTabRequestCurrent = (window->FocusIdxTabRequestNext + (window->FocusIdxTabCounter+1)) % (window->FocusIdxTabCounter+1);
  1949. window->FocusIdxAllCounter = window->FocusIdxTabCounter = -1;
  1950. window->FocusIdxAllRequestNext = window->FocusIdxTabRequestNext = IM_INT_MAX;
  1951. ImGuiAabb title_bar_aabb = window->TitleBarAabb();
  1952. // Apply and ImClamp scrolling
  1953. window->ScrollY = window->NextScrollY;
  1954. window->ScrollY = ImMax(window->ScrollY, 0.0f);
  1955. if (!window->Collapsed && !window->SkipItems)
  1956. window->ScrollY = ImMin(window->ScrollY, ImMax(0.0f, (float)window->SizeContentsFit.y - window->SizeFull.y));
  1957. window->NextScrollY = window->ScrollY;
  1958. // At this point we don't have a clipping rectangle setup yet, so we can test and draw in title bar
  1959. // Collapse window by double-clicking on title bar
  1960. if (!(window->Flags & ImGuiWindowFlags_NoTitleBar))
  1961. {
  1962. if (g.HoveredWindow == window && IsMouseHoveringBox(title_bar_aabb) && g.IO.MouseDoubleClicked[0])
  1963. {
  1964. window->Collapsed = !window->Collapsed;
  1965. MarkSettingsDirty();
  1966. FocusWindow(window);
  1967. }
  1968. }
  1969. else
  1970. {
  1971. window->Collapsed = false;
  1972. }
  1973. if (window->Collapsed)
  1974. {
  1975. // Draw title bar only
  1976. window->Size = title_bar_aabb.GetSize();
  1977. window->DrawList->AddRectFilled(title_bar_aabb.GetTL(), title_bar_aabb.GetBR(), window->Color(ImGuiCol_TitleBgCollapsed), g.Style.WindowRounding);
  1978. if (window->Flags & ImGuiWindowFlags_ShowBorders)
  1979. {
  1980. window->DrawList->AddRect(title_bar_aabb.GetTL()+ImVec2(1,1), title_bar_aabb.GetBR()+ImVec2(1,1), window->Color(ImGuiCol_BorderShadow), g.Style.WindowRounding);
  1981. window->DrawList->AddRect(title_bar_aabb.GetTL(), title_bar_aabb.GetBR(), window->Color(ImGuiCol_Border), g.Style.WindowRounding);
  1982. }
  1983. }
  1984. else
  1985. {
  1986. window->Size = window->SizeFull;
  1987. ImU32 resize_col = 0;
  1988. if ((window->Flags & ImGuiWindowFlags_Tooltip) != 0)
  1989. {
  1990. // Tooltip always resize
  1991. if (window->AutoFitFrames > 0)
  1992. {
  1993. window->SizeFull = window->SizeContentsFit + g.Style.WindowPadding - ImVec2(0.0f, g.Style.ItemSpacing.y);
  1994. }
  1995. }
  1996. else
  1997. {
  1998. const ImVec2 size_auto_fit = ImClamp(window->SizeContentsFit + style.AutoFitPadding, style.WindowMinSize, g.IO.DisplaySize - style.AutoFitPadding);
  1999. if ((window->Flags & ImGuiWindowFlags_AlwaysAutoResize) != 0)
  2000. {
  2001. // Don't continously mark settings as dirty, the size of the window doesn't need to be stored.
  2002. window->SizeFull = size_auto_fit;
  2003. }
  2004. else if (window->AutoFitFrames > 0)
  2005. {
  2006. // Auto-fit only grows during the first few frames
  2007. if (window->AutoFitOnlyGrows)
  2008. window->SizeFull = ImMax(window->SizeFull, size_auto_fit);
  2009. else
  2010. window->SizeFull = size_auto_fit;
  2011. MarkSettingsDirty();
  2012. }
  2013. else if (!(window->Flags & ImGuiWindowFlags_NoResize))
  2014. {
  2015. // Resize grip
  2016. const ImGuiAabb resize_aabb(window->Aabb().GetBR()-ImVec2(18,18), window->Aabb().GetBR());
  2017. const ImGuiID resize_id = window->GetID("#RESIZE");
  2018. bool hovered, held;
  2019. ButtonBehaviour(resize_aabb, resize_id, &hovered, &held, true);
  2020. resize_col = window->Color(held ? ImGuiCol_ResizeGripActive : hovered ? ImGuiCol_ResizeGripHovered : ImGuiCol_ResizeGrip);
  2021. if (g.HoveredWindow == window && held && g.IO.MouseDoubleClicked[0])
  2022. {
  2023. // Manual auto-fit
  2024. window->SizeFull = size_auto_fit;
  2025. window->Size = window->SizeFull;
  2026. MarkSettingsDirty();
  2027. }
  2028. else if (held)
  2029. {
  2030. // Resize
  2031. window->SizeFull = ImMax(window->SizeFull + g.IO.MouseDelta, style.WindowMinSize);
  2032. window->Size = window->SizeFull;
  2033. MarkSettingsDirty();
  2034. }
  2035. }
  2036. // Update aabb immediately so that the rendering below isn't one frame late
  2037. title_bar_aabb = window->TitleBarAabb();
  2038. }
  2039. // Title bar + Window box
  2040. if (fill_alpha > 0.0f)
  2041. {
  2042. if ((window->Flags & ImGuiWindowFlags_ComboBox) != 0)
  2043. window->DrawList->AddRectFilled(window->Pos, window->Pos+window->Size, window->Color(ImGuiCol_ComboBg, fill_alpha), 0);
  2044. else
  2045. window->DrawList->AddRectFilled(window->Pos, window->Pos+window->Size, window->Color(ImGuiCol_WindowBg, fill_alpha), g.Style.WindowRounding);
  2046. }
  2047. if (!(window->Flags & ImGuiWindowFlags_NoTitleBar))
  2048. window->DrawList->AddRectFilled(title_bar_aabb.GetTL(), title_bar_aabb.GetBR(), window->Color(ImGuiCol_TitleBg), g.Style.WindowRounding, 1|2);
  2049. // Borders
  2050. if (window->Flags & ImGuiWindowFlags_ShowBorders)
  2051. {
  2052. const float rounding = (window->Flags & ImGuiWindowFlags_ComboBox) ? 0.0f : g.Style.WindowRounding;
  2053. window->DrawList->AddRect(window->Pos+ImVec2(1,1), window->Pos+window->Size+ImVec2(1,1), window->Color(ImGuiCol_BorderShadow), rounding);
  2054. window->DrawList->AddRect(window->Pos, window->Pos+window->Size, window->Color(ImGuiCol_Border), rounding);
  2055. if (!(window->Flags & ImGuiWindowFlags_NoTitleBar))
  2056. window->DrawList->AddLine(title_bar_aabb.GetBL(), title_bar_aabb.GetBR(), window->Color(ImGuiCol_Border));
  2057. }
  2058. // Scrollbar
  2059. window->ScrollbarY = (window->SizeContentsFit.y > window->Size.y) && !(window->Flags & ImGuiWindowFlags_NoScrollbar);
  2060. if (window->ScrollbarY)
  2061. {
  2062. ImGuiAabb scrollbar_bb(window->Aabb().Max.x - style.ScrollBarWidth, title_bar_aabb.Max.y+1, window->Aabb().Max.x, window->Aabb().Max.y-1);
  2063. //window->DrawList->AddLine(scrollbar_bb.GetTL(), scrollbar_bb.GetBL(), g.Colors[ImGuiCol_Border]);
  2064. window->DrawList->AddRectFilled(scrollbar_bb.Min, scrollbar_bb.Max, window->Color(ImGuiCol_ScrollbarBg));
  2065. scrollbar_bb.Expand(ImVec2(-3,-3));
  2066. const float grab_size_y_norm = ImSaturate(window->Size.y / ImMax(window->SizeContentsFit.y, window->Size.y));
  2067. const float grab_size_y = scrollbar_bb.GetHeight() * grab_size_y_norm;
  2068. // Handle input right away (none of the code above is relying on scrolling position)
  2069. bool held = false;
  2070. bool hovered = false;
  2071. if (grab_size_y_norm < 1.0f)
  2072. {
  2073. const ImGuiID scrollbar_id = window->GetID("#SCROLLY");
  2074. ButtonBehaviour(scrollbar_bb, scrollbar_id, &hovered, &held, true);
  2075. if (held)
  2076. {
  2077. g.HoveredId = scrollbar_id;
  2078. const float pos_y_norm = ImSaturate((g.IO.MousePos.y - (scrollbar_bb.Min.y + grab_size_y*0.5f)) / (scrollbar_bb.GetHeight() - grab_size_y)) * (1.0f - grab_size_y_norm);
  2079. window->ScrollY = pos_y_norm * window->SizeContentsFit.y;
  2080. window->NextScrollY = window->ScrollY;
  2081. }
  2082. }
  2083. // Normalized height of the grab
  2084. const float pos_y_norm = ImSaturate(window->ScrollY / ImMax(0.0f, window->SizeContentsFit.y));
  2085. const ImU32 grab_col = window->Color(held ? ImGuiCol_ScrollbarGrabActive : hovered ? ImGuiCol_ScrollbarGrabHovered : ImGuiCol_ScrollbarGrab);
  2086. window->DrawList->AddRectFilled(
  2087. ImVec2(scrollbar_bb.Min.x, ImLerp(scrollbar_bb.Min.y, scrollbar_bb.Max.y, pos_y_norm)),
  2088. ImVec2(scrollbar_bb.Max.x, ImLerp(scrollbar_bb.Min.y, scrollbar_bb.Max.y, pos_y_norm + grab_size_y_norm)), grab_col);
  2089. }
  2090. // Render resize grip
  2091. // (after the input handling so we don't have a frame of latency)
  2092. if (!(window->Flags & ImGuiWindowFlags_NoResize))
  2093. {
  2094. const float r = style.WindowRounding;
  2095. const ImVec2 br = window->Aabb().GetBR();
  2096. if (r == 0.0f)
  2097. {
  2098. window->DrawList->AddTriangleFilled(br, br-ImVec2(0,14), br-ImVec2(14,0), resize_col);
  2099. }
  2100. else
  2101. {
  2102. // FIXME: We should draw 4 triangles and decide on a size that's not dependant on the rounding size (previously used 18)
  2103. window->DrawList->AddArc(br - ImVec2(r,r), r, resize_col, 6, 9, true);
  2104. window->DrawList->AddTriangleFilled(br+ImVec2(0,-2*r),br+ImVec2(0,-r),br+ImVec2(-r,-r), resize_col);
  2105. window->DrawList->AddTriangleFilled(br+ImVec2(-r,-r), br+ImVec2(-r,0),br+ImVec2(-2*r,0), resize_col);
  2106. }
  2107. }
  2108. }
  2109. // Setup drawing context
  2110. window->DC.ColumnsStartX = window->WindowPadding().x;
  2111. window->DC.ColumnsOffsetX = 0.0f;
  2112. window->DC.CursorStartPos = window->Pos + ImVec2(window->DC.ColumnsStartX + window->DC.ColumnsOffsetX, window->TitleBarHeight() + window->WindowPadding().y) - ImVec2(0.0f, window->ScrollY);
  2113. window->DC.CursorPos = window->DC.CursorStartPos;
  2114. window->DC.CursorPosPrevLine = window->DC.CursorPos;
  2115. window->DC.CurrentLineHeight = window->DC.PrevLineHeight = 0.0f;
  2116. window->DC.LogLineHeight = window->DC.CursorPos.y - 9999.0f;
  2117. window->DC.ChildWindows.resize(0);
  2118. window->DC.ItemWidth.resize(0);
  2119. window->DC.ItemWidth.push_back(window->ItemWidthDefault);
  2120. window->DC.AllowKeyboardFocus.resize(0);
  2121. window->DC.AllowKeyboardFocus.push_back(true);
  2122. window->DC.TextWrapPos.resize(0);
  2123. window->DC.TextWrapPos.push_back(-1.0f); // disabled
  2124. window->DC.ColorEditMode = ImGuiColorEditMode_UserSelect;
  2125. window->DC.ColumnsCurrent = 0;
  2126. window->DC.ColumnsCount = 1;
  2127. window->DC.ColumnsStartPos = window->DC.CursorPos;
  2128. window->DC.ColumnsCellMinY = window->DC.ColumnsCellMaxY = window->DC.ColumnsStartPos.y;
  2129. window->DC.TreeDepth = 0;
  2130. window->DC.StateStorage = &window->StateStorage;
  2131. window->DC.OpenNextNode = -1;
  2132. // Reset contents size for auto-fitting
  2133. window->SizeContentsFit = ImVec2(0.0f, 0.0f);
  2134. if (window->AutoFitFrames > 0)
  2135. window->AutoFitFrames--;
  2136. // Title bar
  2137. if (!(window->Flags & ImGuiWindowFlags_NoTitleBar))
  2138. {
  2139. RenderCollapseTriangle(window->Pos + style.FramePadding, !window->Collapsed, 1.0f, true);
  2140. if (open)
  2141. CloseWindowButton(open);
  2142. const ImVec2 text_size = CalcTextSize(name);
  2143. const ImVec2 text_min = window->Pos + style.FramePadding + ImVec2(window->FontSize() + style.ItemInnerSpacing.x, 0.0f);
  2144. const ImVec2 text_max = window->Pos + ImVec2(window->Size.x - (open ? (title_bar_aabb.GetHeight()-3) : style.FramePadding.x), style.FramePadding.y + text_size.y);
  2145. const bool clip_title = text_size.x > (text_max.x - text_min.x); // only push a clip rectangle if we need to, because it may turn into a separate draw call
  2146. if (clip_title)
  2147. PushClipRect(ImVec4(text_min.x, text_min.y, text_max.x, text_max.y));
  2148. RenderText(text_min, name);
  2149. if (clip_title)
  2150. PopClipRect();
  2151. }
  2152. }
  2153. else
  2154. {
  2155. // Outer clipping rectangle
  2156. if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_ComboBox))
  2157. {
  2158. ImGuiWindow* parent_window = g.CurrentWindowStack[g.CurrentWindowStack.size()-2];
  2159. PushClipRect(parent_window->ClipRectStack.back());
  2160. }
  2161. else
  2162. {
  2163. PushClipRect(ImVec4(0.0f, 0.0f, g.IO.DisplaySize.x, g.IO.DisplaySize.y));
  2164. }
  2165. }
  2166. // Inner clipping rectangle
  2167. // We set this up after processing the resize grip so that our clip rectangle doesn't lag by a frame
  2168. const ImGuiAabb title_bar_aabb = window->TitleBarAabb();
  2169. ImVec4 clip_rect(title_bar_aabb.Min.x+0.5f+window->WindowPadding().x*0.5f, title_bar_aabb.Max.y+0.5f, window->Aabb().Max.x+0.5f-window->WindowPadding().x*0.5f, window->Aabb().Max.y-1.5f);
  2170. if (window->ScrollbarY)
  2171. clip_rect.z -= g.Style.ScrollBarWidth;
  2172. PushClipRect(clip_rect);
  2173. // Clear 'accessed' flag last thing
  2174. if (first_begin_of_the_frame)
  2175. window->Accessed = false;
  2176. // Child window can be out of sight and have "negative" clip windows.
  2177. // Mark them as collapsed so commands are skipped earlier (we can't manually collapse because they have no title bar).
  2178. if (flags & ImGuiWindowFlags_ChildWindow)
  2179. {
  2180. IM_ASSERT((flags & ImGuiWindowFlags_NoTitleBar) != 0);
  2181. const ImVec4 clip_rect_t = window->ClipRectStack.back();
  2182. window->Collapsed = (clip_rect_t.x >= clip_rect_t.z || clip_rect_t.y >= clip_rect_t.w);
  2183. // We also hide the window from rendering because we've already added its border to the command list.
  2184. // (we could perform the check earlier in the function but it is simpler at this point)
  2185. if (window->Collapsed)
  2186. window->Visible = false;
  2187. }
  2188. if (g.Style.Alpha <= 0.0f)
  2189. window->Visible = false;
  2190. // Return false if we don't intend to display anything to allow user to perform an early out optimisation
  2191. window->SkipItems = window->Collapsed || (!window->Visible && window->AutoFitFrames == 0);
  2192. return !window->SkipItems;
  2193. }
  2194. void ImGui::End()
  2195. {
  2196. ImGuiState& g = GImGui;
  2197. ImGuiWindow* window = g.CurrentWindow;
  2198. ImGui::Columns(1, "#CloseColumns");
  2199. PopClipRect(); // inner window clip rectangle
  2200. PopClipRect(); // outer window clip rectangle
  2201. // Select window for move/focus when we're done with all our widgets (we only consider non-childs windows here)
  2202. const ImGuiAabb bb(window->Pos, window->Pos+window->Size);
  2203. if (g.ActiveId == 0 && g.HoveredId == 0 && g.HoveredWindowExcludingChilds == window && IsMouseHoveringBox(bb) && g.IO.MouseClicked[0])
  2204. g.ActiveId = window->GetID("#MOVE");
  2205. // Stop logging
  2206. if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) // FIXME: add more options for scope of logging
  2207. {
  2208. g.LogEnabled = false;
  2209. if (g.LogFile != NULL)
  2210. {
  2211. fprintf(g.LogFile, "\n");
  2212. if (g.LogFile == stdout)
  2213. fflush(g.LogFile);
  2214. else
  2215. fclose(g.LogFile);
  2216. g.LogFile = NULL;
  2217. }
  2218. if (g.LogClipboard->size() > 1)
  2219. {
  2220. g.LogClipboard->append("\n");
  2221. if (g.IO.SetClipboardTextFn)
  2222. g.IO.SetClipboardTextFn(g.LogClipboard->begin());
  2223. g.LogClipboard->clear();
  2224. }
  2225. }
  2226. // Pop
  2227. g.CurrentWindowStack.pop_back();
  2228. g.CurrentWindow = g.CurrentWindowStack.empty() ? NULL : g.CurrentWindowStack.back();
  2229. }
  2230. // Moving window to front
  2231. static void FocusWindow(ImGuiWindow* window)
  2232. {
  2233. ImGuiState& g = GImGui;
  2234. g.FocusedWindow = window;
  2235. for (size_t i = 0; i < g.Windows.size(); i++)
  2236. if (g.Windows[i] == window)
  2237. {
  2238. g.Windows.erase(g.Windows.begin() + i);
  2239. break;
  2240. }
  2241. g.Windows.push_back(window);
  2242. }
  2243. void ImGui::PushItemWidth(float item_width)
  2244. {
  2245. ImGuiWindow* window = GetCurrentWindow();
  2246. item_width = (float)(int)item_width;
  2247. window->DC.ItemWidth.push_back(item_width > 0.0f ? item_width : window->ItemWidthDefault);
  2248. }
  2249. void ImGui::PopItemWidth()
  2250. {
  2251. ImGuiWindow* window = GetCurrentWindow();
  2252. window->DC.ItemWidth.pop_back();
  2253. }
  2254. float ImGui::GetItemWidth()
  2255. {
  2256. ImGuiWindow* window = GetCurrentWindow();
  2257. return window->DC.ItemWidth.back();
  2258. }
  2259. void ImGui::PushAllowKeyboardFocus(bool allow_keyboard_focus)
  2260. {
  2261. ImGuiWindow* window = GetCurrentWindow();
  2262. window->DC.AllowKeyboardFocus.push_back(allow_keyboard_focus);
  2263. }
  2264. void ImGui::PopAllowKeyboardFocus()
  2265. {
  2266. ImGuiWindow* window = GetCurrentWindow();
  2267. window->DC.AllowKeyboardFocus.pop_back();
  2268. }
  2269. void ImGui::PushTextWrapPos(float wrap_x)
  2270. {
  2271. ImGuiWindow* window = GetCurrentWindow();
  2272. window->DC.TextWrapPos.push_back(wrap_x);
  2273. }
  2274. void ImGui::PopTextWrapPos()
  2275. {
  2276. ImGuiWindow* window = GetCurrentWindow();
  2277. window->DC.TextWrapPos.pop_back();
  2278. }
  2279. void ImGui::PushStyleColor(ImGuiCol idx, const ImVec4& col)
  2280. {
  2281. ImGuiState& g = GImGui;
  2282. ImGuiColMod backup;
  2283. backup.Col = idx;
  2284. backup.PreviousValue = g.Style.Colors[idx];
  2285. g.ColorModifiers.push_back(backup);
  2286. g.Style.Colors[idx] = col;
  2287. }
  2288. void ImGui::PopStyleColor(int count)
  2289. {
  2290. ImGuiState& g = GImGui;
  2291. while (count > 0)
  2292. {
  2293. ImGuiColMod& backup = g.ColorModifiers.back();
  2294. g.Style.Colors[backup.Col] = backup.PreviousValue;
  2295. g.ColorModifiers.pop_back();
  2296. count--;
  2297. }
  2298. }
  2299. static float* GetStyleVarFloatAddr(ImGuiStyleVar idx)
  2300. {
  2301. ImGuiState& g = GImGui;
  2302. switch (idx)
  2303. {
  2304. case ImGuiStyleVar_Alpha: return &g.Style.Alpha;
  2305. case ImGuiStyleVar_TreeNodeSpacing: return &g.Style.TreeNodeSpacing;
  2306. case ImGuiStyleVar_ColumnsMinSpacing: return &g.Style.ColumnsMinSpacing;
  2307. }
  2308. return NULL;
  2309. }
  2310. static ImVec2* GetStyleVarVec2Addr(ImGuiStyleVar idx)
  2311. {
  2312. ImGuiState& g = GImGui;
  2313. switch (idx)
  2314. {
  2315. case ImGuiStyleVar_WindowPadding: return &g.Style.WindowPadding;
  2316. case ImGuiStyleVar_FramePadding: return &g.Style.FramePadding;
  2317. case ImGuiStyleVar_ItemSpacing: return &g.Style.ItemSpacing;
  2318. case ImGuiStyleVar_ItemInnerSpacing: return &g.Style.ItemInnerSpacing;
  2319. }
  2320. return NULL;
  2321. }
  2322. void ImGui::PushStyleVar(ImGuiStyleVar idx, float val)
  2323. {
  2324. ImGuiState& g = GImGui;
  2325. float* pvar = GetStyleVarFloatAddr(idx);
  2326. IM_ASSERT(pvar != NULL); // Called wrong function?
  2327. ImGuiStyleMod backup;
  2328. backup.Var = idx;
  2329. backup.PreviousValue = ImVec2(*pvar, 0.0f);
  2330. g.StyleModifiers.push_back(backup);
  2331. *pvar = val;
  2332. }
  2333. void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val)
  2334. {
  2335. ImGuiState& g = GImGui;
  2336. ImVec2* pvar = GetStyleVarVec2Addr(idx);
  2337. IM_ASSERT(pvar != NULL); // Called wrong function?
  2338. ImGuiStyleMod backup;
  2339. backup.Var = idx;
  2340. backup.PreviousValue = *pvar;
  2341. g.StyleModifiers.push_back(backup);
  2342. *pvar = val;
  2343. }
  2344. void ImGui::PopStyleVar(int count)
  2345. {
  2346. ImGuiState& g = GImGui;
  2347. while (count > 0)
  2348. {
  2349. ImGuiStyleMod& backup = g.StyleModifiers.back();
  2350. if (float* pvar_f = GetStyleVarFloatAddr(backup.Var))
  2351. *pvar_f = backup.PreviousValue.x;
  2352. else if (ImVec2* pvar_v = GetStyleVarVec2Addr(backup.Var))
  2353. *pvar_v = backup.PreviousValue;
  2354. g.StyleModifiers.pop_back();
  2355. count--;
  2356. }
  2357. }
  2358. const char* ImGui::GetStyleColorName(ImGuiCol idx)
  2359. {
  2360. // Create switch-case from enum with regexp: ImGuiCol_{.*}, --> case ImGuiCol_\1: return "\1";
  2361. switch (idx)
  2362. {
  2363. case ImGuiCol_Text: return "Text";
  2364. case ImGuiCol_WindowBg: return "WindowBg";
  2365. case ImGuiCol_Border: return "Border";
  2366. case ImGuiCol_BorderShadow: return "BorderShadow";
  2367. case ImGuiCol_FrameBg: return "FrameBg";
  2368. case ImGuiCol_TitleBg: return "TitleBg";
  2369. case ImGuiCol_TitleBgCollapsed: return "TitleBgCollapsed";
  2370. case ImGuiCol_ScrollbarBg: return "ScrollbarBg";
  2371. case ImGuiCol_ScrollbarGrab: return "ScrollbarGrab";
  2372. case ImGuiCol_ScrollbarGrabHovered: return "ScrollbarGrabHovered";
  2373. case ImGuiCol_ScrollbarGrabActive: return "ScrollbarGrabActive";
  2374. case ImGuiCol_ComboBg: return "ComboBg";
  2375. case ImGuiCol_CheckHovered: return "CheckHovered";
  2376. case ImGuiCol_CheckActive: return "CheckActive";
  2377. case ImGuiCol_SliderGrab: return "SliderGrab";
  2378. case ImGuiCol_SliderGrabActive: return "SliderGrabActive";
  2379. case ImGuiCol_Button: return "Button";
  2380. case ImGuiCol_ButtonHovered: return "ButtonHovered";
  2381. case ImGuiCol_ButtonActive: return "ButtonActive";
  2382. case ImGuiCol_Header: return "Header";
  2383. case ImGuiCol_HeaderHovered: return "HeaderHovered";
  2384. case ImGuiCol_HeaderActive: return "HeaderActive";
  2385. case ImGuiCol_Column: return "Column";
  2386. case ImGuiCol_ColumnHovered: return "ColumnHovered";
  2387. case ImGuiCol_ColumnActive: return "ColumnActive";
  2388. case ImGuiCol_ResizeGrip: return "ResizeGrip";
  2389. case ImGuiCol_ResizeGripHovered: return "ResizeGripHovered";
  2390. case ImGuiCol_ResizeGripActive: return "ResizeGripActive";
  2391. case ImGuiCol_CloseButton: return "CloseButton";
  2392. case ImGuiCol_CloseButtonHovered: return "CloseButtonHovered";
  2393. case ImGuiCol_CloseButtonActive: return "CloseButtonActive";
  2394. case ImGuiCol_PlotLines: return "PlotLines";
  2395. case ImGuiCol_PlotLinesHovered: return "PlotLinesHovered";
  2396. case ImGuiCol_PlotHistogram: return "PlotHistogram";
  2397. case ImGuiCol_PlotHistogramHovered: return "PlotHistogramHovered";
  2398. case ImGuiCol_TextSelectedBg: return "TextSelectedBg";
  2399. case ImGuiCol_TooltipBg: return "TooltipBg";
  2400. }
  2401. IM_ASSERT(0);
  2402. return "Unknown";
  2403. }
  2404. bool ImGui::GetWindowIsFocused()
  2405. {
  2406. ImGuiState& g = GImGui;
  2407. ImGuiWindow* window = GetCurrentWindow();
  2408. return g.FocusedWindow == window;
  2409. }
  2410. float ImGui::GetWindowWidth()
  2411. {
  2412. ImGuiWindow* window = GetCurrentWindow();
  2413. return window->Size.x;
  2414. }
  2415. ImVec2 ImGui::GetWindowPos()
  2416. {
  2417. ImGuiWindow* window = GetCurrentWindow();
  2418. return window->Pos;
  2419. }
  2420. void ImGui::SetWindowPos(const ImVec2& pos)
  2421. {
  2422. ImGuiWindow* window = GetCurrentWindow();
  2423. const ImVec2 old_pos = window->Pos;
  2424. window->PosFloat = pos;
  2425. window->Pos = ImVec2((float)(int)window->PosFloat.x, (float)(int)window->PosFloat.y);
  2426. // If 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
  2427. window->DC.CursorPos += (window->Pos - old_pos);
  2428. }
  2429. ImVec2 ImGui::GetWindowSize()
  2430. {
  2431. ImGuiWindow* window = GetCurrentWindow();
  2432. return window->Size;
  2433. }
  2434. void ImGui::SetWindowSize(const ImVec2& size)
  2435. {
  2436. ImGuiWindow* window = GetCurrentWindow();
  2437. if (ImLength(size) < 0.001f)
  2438. {
  2439. window->AutoFitFrames = 2;
  2440. window->AutoFitOnlyGrows = false;
  2441. }
  2442. else
  2443. {
  2444. window->SizeFull = size;
  2445. }
  2446. }
  2447. ImVec2 ImGui::GetContentRegionMax()
  2448. {
  2449. ImGuiWindow* window = GetCurrentWindow();
  2450. ImVec2 m = window->Size - window->WindowPadding();
  2451. if (window->DC.ColumnsCount != 1)
  2452. {
  2453. m.x = GetColumnOffset(window->DC.ColumnsCurrent + 1);
  2454. m.x -= GImGui.Style.WindowPadding.x;
  2455. }
  2456. else
  2457. {
  2458. if (window->ScrollbarY)
  2459. m.x -= GImGui.Style.ScrollBarWidth;
  2460. }
  2461. return m;
  2462. }
  2463. ImVec2 ImGui::GetWindowContentRegionMin()
  2464. {
  2465. ImGuiWindow* window = GetCurrentWindow();
  2466. return ImVec2(0, window->TitleBarHeight()) + window->WindowPadding();
  2467. }
  2468. ImVec2 ImGui::GetWindowContentRegionMax()
  2469. {
  2470. ImGuiWindow* window = GetCurrentWindow();
  2471. ImVec2 m = window->Size - window->WindowPadding();
  2472. if (window->ScrollbarY)
  2473. m.x -= GImGui.Style.ScrollBarWidth;
  2474. return m;
  2475. }
  2476. float ImGui::GetTextLineHeight()
  2477. {
  2478. ImGuiWindow* window = GetCurrentWindow();
  2479. return window->FontSize();
  2480. }
  2481. float ImGui::GetTextLineSpacing()
  2482. {
  2483. ImGuiState& g = GImGui;
  2484. ImGuiWindow* window = GetCurrentWindow();
  2485. return window->FontSize() + g.Style.ItemSpacing.y;
  2486. }
  2487. ImDrawList* ImGui::GetWindowDrawList()
  2488. {
  2489. ImGuiWindow* window = GetCurrentWindow();
  2490. return window->DrawList;
  2491. }
  2492. ImFont* ImGui::GetWindowFont()
  2493. {
  2494. ImGuiWindow* window = GetCurrentWindow();
  2495. return window->Font();
  2496. }
  2497. float ImGui::GetWindowFontSize()
  2498. {
  2499. ImGuiWindow* window = GetCurrentWindow();
  2500. return window->FontSize();
  2501. }
  2502. void ImGui::SetWindowFontScale(float scale)
  2503. {
  2504. ImGuiWindow* window = GetCurrentWindow();
  2505. window->FontWindowScale = scale;
  2506. }
  2507. ImVec2 ImGui::GetCursorPos()
  2508. {
  2509. ImGuiWindow* window = GetCurrentWindow();
  2510. return window->DC.CursorPos - window->Pos;
  2511. }
  2512. void ImGui::SetCursorPos(const ImVec2& pos)
  2513. {
  2514. ImGuiWindow* window = GetCurrentWindow();
  2515. window->DC.CursorPos = window->Pos + pos;
  2516. }
  2517. void ImGui::SetCursorPosX(float x)
  2518. {
  2519. ImGuiWindow* window = GetCurrentWindow();
  2520. window->DC.CursorPos.x = window->Pos.x + x;
  2521. }
  2522. void ImGui::SetCursorPosY(float y)
  2523. {
  2524. ImGuiWindow* window = GetCurrentWindow();
  2525. window->DC.CursorPos.y = window->Pos.y + y;
  2526. }
  2527. ImVec2 ImGui::GetCursorScreenPos()
  2528. {
  2529. ImGuiWindow* window = GetCurrentWindow();
  2530. return window->DC.CursorPos;
  2531. }
  2532. void ImGui::SetScrollPosHere()
  2533. {
  2534. ImGuiWindow* window = GetCurrentWindow();
  2535. window->NextScrollY = (window->DC.CursorPos.y + window->ScrollY) - (window->Pos.y + window->SizeFull.y * 0.5f) - (window->TitleBarHeight() + window->WindowPadding().y);
  2536. }
  2537. void ImGui::SetKeyboardFocusHere(int offset)
  2538. {
  2539. ImGuiWindow* window = GetCurrentWindow();
  2540. window->FocusIdxAllRequestNext = window->FocusIdxAllCounter + 1 + offset;
  2541. window->FocusIdxTabRequestNext = IM_INT_MAX;
  2542. }
  2543. void ImGui::SetTreeStateStorage(ImGuiStorage* tree)
  2544. {
  2545. ImGuiWindow* window = GetCurrentWindow();
  2546. window->DC.StateStorage = tree ? tree : &window->StateStorage;
  2547. }
  2548. ImGuiStorage* ImGui::GetTreeStateStorage()
  2549. {
  2550. ImGuiWindow* window = GetCurrentWindow();
  2551. return window->DC.StateStorage;
  2552. }
  2553. void ImGui::TextV(const char* fmt, va_list args)
  2554. {
  2555. ImGuiWindow* window = GetCurrentWindow();
  2556. if (window->SkipItems)
  2557. return;
  2558. static char buf[1024];
  2559. const char* text_end = buf + ImFormatStringV(buf, IM_ARRAYSIZE(buf), fmt, args);
  2560. TextUnformatted(buf, text_end);
  2561. }
  2562. void ImGui::Text(const char* fmt, ...)
  2563. {
  2564. va_list args;
  2565. va_start(args, fmt);
  2566. TextV(fmt, args);
  2567. va_end(args);
  2568. }
  2569. void ImGui::TextColoredV(const ImVec4& col, const char* fmt, va_list args)
  2570. {
  2571. ImGui::PushStyleColor(ImGuiCol_Text, col);
  2572. TextV(fmt, args);
  2573. ImGui::PopStyleColor();
  2574. }
  2575. void ImGui::TextColored(const ImVec4& col, const char* fmt, ...)
  2576. {
  2577. va_list args;
  2578. va_start(args, fmt);
  2579. TextColoredV(col, fmt, args);
  2580. va_end(args);
  2581. }
  2582. void ImGui::TextWrappedV(const char* fmt, va_list args)
  2583. {
  2584. ImGui::PushTextWrapPos(0.0f);
  2585. TextV(fmt, args);
  2586. ImGui::PopTextWrapPos();
  2587. }
  2588. void ImGui::TextWrapped(const char* fmt, ...)
  2589. {
  2590. va_list args;
  2591. va_start(args, fmt);
  2592. TextWrappedV(fmt, args);
  2593. va_end(args);
  2594. }
  2595. void ImGui::TextUnformatted(const char* text, const char* text_end)
  2596. {
  2597. ImGuiState& g = GImGui;
  2598. ImGuiWindow* window = GetCurrentWindow();
  2599. if (window->SkipItems)
  2600. return;
  2601. IM_ASSERT(text != NULL);
  2602. const char* text_begin = text;
  2603. if (text_end == NULL)
  2604. text_end = text + strlen(text); // FIXME-OPT
  2605. const float wrap_pos_x = window->DC.TextWrapPos.back();
  2606. const bool wrap_enabled = wrap_pos_x >= 0.0f;
  2607. if (text_end - text > 2000 && !wrap_enabled)
  2608. {
  2609. // Long text!
  2610. // Perform manual coarse clipping to optimize for long multi-line text
  2611. // From this point we will only compute the width of lines that are visible.
  2612. // Optimization only available when word-wrapping is disabled.
  2613. const char* line = text;
  2614. const float line_height = ImGui::GetTextLineHeight();
  2615. const ImVec2 start_pos = window->DC.CursorPos;
  2616. const ImVec4 clip_rect = window->ClipRectStack.back();
  2617. ImVec2 text_size(0,0);
  2618. if (start_pos.y <= clip_rect.w)
  2619. {
  2620. ImVec2 pos = start_pos;
  2621. // Lines to skip (can't skip when logging text)
  2622. if (!g.LogEnabled)
  2623. {
  2624. int lines_skippable = (int)((clip_rect.y - start_pos.y) / line_height) - 1;
  2625. if (lines_skippable > 0)
  2626. {
  2627. int lines_skipped = 0;
  2628. while (line < text_end && lines_skipped <= lines_skippable)
  2629. {
  2630. const char* line_end = strchr(line, '\n');
  2631. line = line_end + 1;
  2632. lines_skipped++;
  2633. }
  2634. pos.y += lines_skipped * line_height;
  2635. }
  2636. }
  2637. // Lines to render
  2638. if (line < text_end)
  2639. {
  2640. ImGuiAabb line_box(pos, pos + ImVec2(ImGui::GetWindowWidth(), line_height));
  2641. while (line < text_end)
  2642. {
  2643. const char* line_end = strchr(line, '\n');
  2644. if (IsClipped(line_box))
  2645. break;
  2646. const ImVec2 line_size = CalcTextSize(line, line_end, false);
  2647. text_size.x = ImMax(text_size.x, line_size.x);
  2648. RenderText(pos, line, line_end, false);
  2649. if (!line_end)
  2650. line_end = text_end;
  2651. line = line_end + 1;
  2652. line_box.Min.y += line_height;
  2653. line_box.Max.y += line_height;
  2654. pos.y += line_height;
  2655. }
  2656. // Count remaining lines
  2657. int lines_skipped = 0;
  2658. while (line < text_end)
  2659. {
  2660. const char* line_end = strchr(line, '\n');
  2661. if (!line_end)
  2662. line_end = text_end;
  2663. line = line_end + 1;
  2664. lines_skipped++;
  2665. }
  2666. pos.y += lines_skipped * line_height;
  2667. }
  2668. text_size.y += (pos - start_pos).y;
  2669. }
  2670. const ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos + text_size);
  2671. ItemSize(bb);
  2672. ClipAdvance(bb);
  2673. }
  2674. else
  2675. {
  2676. const float wrap_width = wrap_enabled ? CalcWrapWidthForPos(window->DC.CursorPos, wrap_pos_x) : 0.0f;
  2677. const ImVec2 text_size = CalcTextSize(text_begin, text_end, false, wrap_width);
  2678. ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos + text_size);
  2679. ItemSize(bb.GetSize(), &bb.Min);
  2680. if (ClipAdvance(bb))
  2681. return;
  2682. // Render
  2683. // We don't hide text after ## in this end-user function.
  2684. RenderText(bb.Min, text_begin, text_end, false, wrap_width);
  2685. }
  2686. }
  2687. void ImGui::AlignFirstTextHeightToWidgets()
  2688. {
  2689. ImGuiState& g = GImGui;
  2690. ImGuiWindow* window = GetCurrentWindow();
  2691. if (window->SkipItems)
  2692. return;
  2693. // Declare a dummy item size to that upcoming items that are smaller will center-align on the newly expanded line height.
  2694. ItemSize(ImVec2(0, window->FontSize() + g.Style.FramePadding.y*2));
  2695. ImGui::SameLine(0, 0);
  2696. }
  2697. // Add a label+text combo aligned to other label+value widgets
  2698. void ImGui::LabelTextV(const char* label, const char* fmt, va_list args)
  2699. {
  2700. ImGuiState& g = GImGui;
  2701. ImGuiWindow* window = GetCurrentWindow();
  2702. if (window->SkipItems)
  2703. return;
  2704. const ImGuiStyle& style = g.Style;
  2705. const float w = window->DC.ItemWidth.back();
  2706. static char buf[1024];
  2707. const char* text_begin = &buf[0];
  2708. const char* text_end = text_begin + ImFormatStringV(buf, IM_ARRAYSIZE(buf), fmt, args);
  2709. const ImVec2 text_size = CalcTextSize(label);
  2710. const ImGuiAabb value_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w + style.FramePadding.x*2, text_size.y));
  2711. const ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w + style.FramePadding.x*2 + style.ItemInnerSpacing.x, 0.0f) + text_size);
  2712. ItemSize(bb);
  2713. if (ClipAdvance(value_bb))
  2714. return;
  2715. // Render
  2716. RenderText(value_bb.Min, text_begin, text_end);
  2717. RenderText(ImVec2(value_bb.Max.x + style.ItemInnerSpacing.x, value_bb.Min.y), label);
  2718. }
  2719. void ImGui::LabelText(const char* label, const char* fmt, ...)
  2720. {
  2721. va_list args;
  2722. va_start(args, fmt);
  2723. LabelTextV(label, fmt, args);
  2724. va_end(args);
  2725. }
  2726. static bool ButtonBehaviour(const ImGuiAabb& bb, const ImGuiID& id, bool* out_hovered, bool* out_held, bool allow_key_modifiers, bool repeat)
  2727. {
  2728. ImGuiState& g = GImGui;
  2729. ImGuiWindow* window = GetCurrentWindow();
  2730. const bool hovered = (g.HoveredWindow == window) && (g.HoveredId == 0) && IsMouseHoveringBox(bb);
  2731. bool pressed = false;
  2732. if (hovered)
  2733. {
  2734. g.HoveredId = id;
  2735. if (allow_key_modifiers || (!g.IO.KeyCtrl && !g.IO.KeyShift))
  2736. {
  2737. if (g.IO.MouseClicked[0])
  2738. {
  2739. g.ActiveId = id;
  2740. }
  2741. else if (repeat && g.ActiveId && ImGui::IsMouseClicked(0, true))
  2742. {
  2743. pressed = true;
  2744. }
  2745. }
  2746. }
  2747. bool held = false;
  2748. if (g.ActiveId == id)
  2749. {
  2750. if (g.IO.MouseDown[0])
  2751. {
  2752. held = true;
  2753. }
  2754. else
  2755. {
  2756. if (hovered)
  2757. pressed = true;
  2758. g.ActiveId = 0;
  2759. }
  2760. }
  2761. if (out_hovered) *out_hovered = hovered;
  2762. if (out_held) *out_held = held;
  2763. return pressed;
  2764. }
  2765. bool ImGui::Button(const char* label, ImVec2 size, bool repeat_when_held)
  2766. {
  2767. ImGuiState& g = GImGui;
  2768. ImGuiWindow* window = GetCurrentWindow();
  2769. if (window->SkipItems)
  2770. return false;
  2771. const ImGuiStyle& style = g.Style;
  2772. const ImGuiID id = window->GetID(label);
  2773. const ImVec2 text_size = CalcTextSize(label);
  2774. if (size.x == 0.0f)
  2775. size.x = text_size.x;
  2776. if (size.y == 0.0f)
  2777. size.y = text_size.y;
  2778. const ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos+size + style.FramePadding*2.0f);
  2779. ItemSize(bb);
  2780. if (ClipAdvance(bb))
  2781. return false;
  2782. bool hovered, held;
  2783. bool pressed = ButtonBehaviour(bb, id, &hovered, &held, true, repeat_when_held);
  2784. // Render
  2785. const ImU32 col = window->Color((hovered && held) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button);
  2786. RenderFrame(bb.Min, bb.Max, col);
  2787. if (size.x < text_size.x || size.y < text_size.y)
  2788. PushClipRect(ImVec4(bb.Min.x+style.FramePadding.x, bb.Min.y+style.FramePadding.y, bb.Max.x, bb.Max.y-style.FramePadding.y)); // Allow extra to draw over the horizontal padding to make it visible that text doesn't fit
  2789. const ImVec2 off = ImVec2(ImMax(0.0f, size.x - text_size.x) * 0.5f, ImMax(0.0f, size.y - text_size.y) * 0.5f);
  2790. RenderText(bb.Min + style.FramePadding + off, label);
  2791. if (size.x < text_size.x || size.y < text_size.y)
  2792. PopClipRect();
  2793. return pressed;
  2794. }
  2795. // Small buttons fits within text without additional spacing.
  2796. bool ImGui::SmallButton(const char* label)
  2797. {
  2798. ImGuiState& g = GImGui;
  2799. ImGuiWindow* window = GetCurrentWindow();
  2800. if (window->SkipItems)
  2801. return false;
  2802. const ImGuiStyle& style = g.Style;
  2803. const ImGuiID id = window->GetID(label);
  2804. const ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos+CalcTextSize(label) + ImVec2(style.FramePadding.x*2,0));
  2805. ItemSize(bb);
  2806. if (ClipAdvance(bb))
  2807. return false;
  2808. bool hovered, held;
  2809. bool pressed = ButtonBehaviour(bb, id, &hovered, &held, true);
  2810. // Render
  2811. const ImU32 col = window->Color((hovered && held) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button);
  2812. RenderFrame(bb.Min, bb.Max, col);
  2813. RenderText(bb.Min + ImVec2(style.FramePadding.x,0), label);
  2814. return pressed;
  2815. }
  2816. // Upper-right button to close a window.
  2817. static bool CloseWindowButton(bool* open)
  2818. {
  2819. ImGuiWindow* window = GetCurrentWindow();
  2820. const ImGuiID id = window->GetID("##CLOSE");
  2821. const float size = window->TitleBarHeight() - 4.0f;
  2822. const ImGuiAabb bb(window->Aabb().GetTR() + ImVec2(-3.0f-size,2.0f), window->Aabb().GetTR() + ImVec2(-3.0f,2.0f+size));
  2823. bool hovered, held;
  2824. bool pressed = ButtonBehaviour(bb, id, &hovered, &held, true);
  2825. // Render
  2826. const ImU32 col = window->Color((held && hovered) ? ImGuiCol_CloseButtonActive : hovered ? ImGuiCol_CloseButtonHovered : ImGuiCol_CloseButton);
  2827. const ImVec2 center = bb.GetCenter();
  2828. window->DrawList->AddCircleFilled(center, ImMax(2.0f,size*0.5f), col, 16);
  2829. const float cross_extent = (size * 0.5f * 0.7071f) - 1.0f;
  2830. if (hovered)
  2831. {
  2832. window->DrawList->AddLine(center + ImVec2(+cross_extent,+cross_extent), center + ImVec2(-cross_extent,-cross_extent), window->Color(ImGuiCol_Text));
  2833. window->DrawList->AddLine(center + ImVec2(+cross_extent,-cross_extent), center + ImVec2(-cross_extent,+cross_extent), window->Color(ImGuiCol_Text));
  2834. }
  2835. if (open != NULL && pressed)
  2836. *open = !*open;
  2837. return pressed;
  2838. }
  2839. // Start logging ImGui output to TTY
  2840. void ImGui::LogToTTY(int max_depth)
  2841. {
  2842. ImGuiState& g = GImGui;
  2843. if (g.LogEnabled)
  2844. return;
  2845. g.LogEnabled = true;
  2846. g.LogFile = stdout;
  2847. if (max_depth >= 0)
  2848. g.LogAutoExpandMaxDepth = max_depth;
  2849. }
  2850. // Start logging ImGui output to given file
  2851. void ImGui::LogToFile(int max_depth, const char* filename)
  2852. {
  2853. ImGuiState& g = GImGui;
  2854. if (g.LogEnabled)
  2855. return;
  2856. if (!filename)
  2857. filename = g.IO.LogFilename;
  2858. g.LogEnabled = true;
  2859. g.LogFile = fopen(filename, "at");
  2860. if (max_depth >= 0)
  2861. g.LogAutoExpandMaxDepth = max_depth;
  2862. }
  2863. // Start logging ImGui output to clipboard
  2864. void ImGui::LogToClipboard(int max_depth)
  2865. {
  2866. ImGuiState& g = GImGui;
  2867. if (g.LogEnabled)
  2868. return;
  2869. g.LogEnabled = true;
  2870. g.LogFile = NULL;
  2871. if (max_depth >= 0)
  2872. g.LogAutoExpandMaxDepth = max_depth;
  2873. }
  2874. // Helper to display logging buttons
  2875. void ImGui::LogButtons()
  2876. {
  2877. ImGuiState& g = GImGui;
  2878. ImGui::PushID("LogButtons");
  2879. const bool log_to_tty = ImGui::Button("Log To TTY");
  2880. ImGui::SameLine();
  2881. const bool log_to_file = ImGui::Button("Log To File");
  2882. ImGui::SameLine();
  2883. const bool log_to_clipboard = ImGui::Button("Log To Clipboard");
  2884. ImGui::SameLine();
  2885. ImGui::PushItemWidth(80.0f);
  2886. ImGui::PushAllowKeyboardFocus(false);
  2887. ImGui::SliderInt("Depth", &g.LogAutoExpandMaxDepth, 0, 9, NULL);
  2888. ImGui::PopAllowKeyboardFocus();
  2889. ImGui::PopItemWidth();
  2890. ImGui::PopID();
  2891. // Start logging at the end of the function so that the buttons don't appear in the log
  2892. if (log_to_tty)
  2893. LogToTTY(g.LogAutoExpandMaxDepth);
  2894. if (log_to_file)
  2895. LogToFile(g.LogAutoExpandMaxDepth, g.IO.LogFilename);
  2896. if (log_to_clipboard)
  2897. LogToClipboard(g.LogAutoExpandMaxDepth);
  2898. }
  2899. bool ImGui::CollapsingHeader(const char* label, const char* str_id, const bool display_frame, const bool default_open)
  2900. {
  2901. ImGuiState& g = GImGui;
  2902. ImGuiWindow* window = GetCurrentWindow();
  2903. if (window->SkipItems)
  2904. return false;
  2905. const ImGuiStyle& style = g.Style;
  2906. IM_ASSERT(str_id != NULL || label != NULL);
  2907. if (str_id == NULL)
  2908. str_id = label;
  2909. if (label == NULL)
  2910. label = str_id;
  2911. const ImGuiID id = window->GetID(str_id);
  2912. // We only write to the tree storage if the user clicks
  2913. ImGuiStorage* tree = window->DC.StateStorage;
  2914. bool opened;
  2915. if (window->DC.OpenNextNode != -1)
  2916. {
  2917. opened = window->DC.OpenNextNode > 0;
  2918. tree->SetInt(id, opened);
  2919. window->DC.OpenNextNode = -1;
  2920. }
  2921. else
  2922. {
  2923. opened = tree->GetInt(id, default_open) != 0;
  2924. }
  2925. // Framed header expand a little outside the default padding
  2926. const ImVec2 window_padding = window->WindowPadding();
  2927. const ImVec2 text_size = CalcTextSize(label);
  2928. const ImVec2 pos_min = window->DC.CursorPos;
  2929. const ImVec2 pos_max = window->Pos + GetContentRegionMax();
  2930. ImGuiAabb bb = ImGuiAabb(pos_min, ImVec2(pos_max.x, pos_min.y + text_size.y));
  2931. if (display_frame)
  2932. {
  2933. bb.Min.x -= window_padding.x*0.5f - 1;
  2934. bb.Max.x += window_padding.x*0.5f - 1;
  2935. bb.Max.y += style.FramePadding.y * 2;
  2936. }
  2937. const ImGuiAabb text_bb(bb.Min, bb.Min + ImVec2(window->FontSize() + style.FramePadding.x*2*2,0) + text_size);
  2938. ItemSize(ImVec2(text_bb.GetSize().x, bb.GetSize().y)); // NB: we don't provide our width so that it doesn't get feed back into AutoFit
  2939. // When logging is enabled, if automatically expand tree nodes (but *NOT* collapsing headers.. seems like sensible behaviour).
  2940. // NB- If we are above max depth we still allow manually opened nodes to be logged.
  2941. if (!display_frame)
  2942. if (g.LogEnabled && window->DC.TreeDepth < g.LogAutoExpandMaxDepth)
  2943. opened = true;
  2944. if (ClipAdvance(bb))
  2945. return opened;
  2946. bool hovered, held;
  2947. bool pressed = ButtonBehaviour(display_frame ? bb : text_bb, id, &hovered, &held, false);
  2948. if (pressed)
  2949. {
  2950. opened = !opened;
  2951. tree->SetInt(id, opened);
  2952. }
  2953. // Render
  2954. const ImU32 col = window->Color((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header);
  2955. if (display_frame)
  2956. {
  2957. // Framed type
  2958. RenderFrame(bb.Min, bb.Max, col, true);
  2959. RenderCollapseTriangle(bb.Min + style.FramePadding, opened, 1.0f, true);
  2960. RenderText(bb.Min + style.FramePadding + ImVec2(window->FontSize() + style.FramePadding.x*2,0), label);
  2961. }
  2962. else
  2963. {
  2964. // Unframed typed for tree nodes
  2965. if ((held && hovered) || hovered)
  2966. RenderFrame(bb.Min, bb.Max, col, false);
  2967. RenderCollapseTriangle(bb.Min + ImVec2(style.FramePadding.x, window->FontSize()*0.15f), opened, 0.70f, false);
  2968. RenderText(bb.Min + ImVec2(window->FontSize() + style.FramePadding.x*2,0), label);
  2969. }
  2970. return opened;
  2971. }
  2972. // Text with a little bullet aligned to the typical tree node.
  2973. void ImGui::BulletTextV(const char* fmt, va_list args)
  2974. {
  2975. ImGuiState& g = GImGui;
  2976. ImGuiWindow* window = GetCurrentWindow();
  2977. if (window->SkipItems)
  2978. return;
  2979. static char buf[1024];
  2980. const char* text_begin = buf;
  2981. const char* text_end = text_begin + ImFormatStringV(buf, IM_ARRAYSIZE(buf), fmt, args);
  2982. const float line_height = window->FontSize();
  2983. const ImVec2 text_size = CalcTextSize(text_begin, text_end);
  2984. const ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(line_height + (text_size.x > 0.0f ? (g.Style.FramePadding.x*2) : 0.0f),0) + text_size); // Empty text doesn't add padding
  2985. ItemSize(bb);
  2986. if (ClipAdvance(bb))
  2987. return;
  2988. // Render
  2989. const float bullet_size = line_height*0.15f;
  2990. window->DrawList->AddCircleFilled(bb.Min + ImVec2(g.Style.FramePadding.x + line_height*0.5f, line_height*0.5f), bullet_size, window->Color(ImGuiCol_Text));
  2991. RenderText(bb.Min+ImVec2(window->FontSize()+g.Style.FramePadding.x*2,0), text_begin, text_end);
  2992. }
  2993. void ImGui::BulletText(const char* fmt, ...)
  2994. {
  2995. va_list args;
  2996. va_start(args, fmt);
  2997. BulletTextV(fmt, args);
  2998. va_end(args);
  2999. }
  3000. // If returning 'true' the node is open and the user is responsible for calling TreePop
  3001. bool ImGui::TreeNodeV(const char* str_id, const char* fmt, va_list args)
  3002. {
  3003. static char buf[1024];
  3004. ImFormatStringV(buf, IM_ARRAYSIZE(buf), fmt, args);
  3005. if (!str_id || !str_id[0])
  3006. str_id = fmt;
  3007. ImGui::PushID(str_id);
  3008. const bool opened = ImGui::CollapsingHeader(buf, "", false); // do not add to the ID so that TreeNodeSetOpen can access
  3009. ImGui::PopID();
  3010. if (opened)
  3011. ImGui::TreePush(str_id);
  3012. return opened;
  3013. }
  3014. bool ImGui::TreeNode(const char* str_id, const char* fmt, ...)
  3015. {
  3016. va_list args;
  3017. va_start(args, fmt);
  3018. bool s = TreeNodeV(str_id, fmt, args);
  3019. va_end(args);
  3020. return s;
  3021. }
  3022. // If returning 'true' the node is open and the user is responsible for calling TreePop
  3023. bool ImGui::TreeNodeV(const void* ptr_id, const char* fmt, va_list args)
  3024. {
  3025. static char buf[1024];
  3026. ImFormatStringV(buf, IM_ARRAYSIZE(buf), fmt, args);
  3027. if (!ptr_id)
  3028. ptr_id = fmt;
  3029. ImGui::PushID(ptr_id);
  3030. const bool opened = ImGui::CollapsingHeader(buf, "", false);
  3031. ImGui::PopID();
  3032. if (opened)
  3033. ImGui::TreePush(ptr_id);
  3034. return opened;
  3035. }
  3036. bool ImGui::TreeNode(const void* ptr_id, const char* fmt, ...)
  3037. {
  3038. va_list args;
  3039. va_start(args, fmt);
  3040. bool s = TreeNodeV(ptr_id, fmt, args);
  3041. va_end(args);
  3042. return s;
  3043. }
  3044. bool ImGui::TreeNode(const char* str_label_id)
  3045. {
  3046. return TreeNode(str_label_id, "%s", str_label_id);
  3047. }
  3048. void ImGui::OpenNextNode(bool open)
  3049. {
  3050. ImGuiWindow* window = GetCurrentWindow();
  3051. window->DC.OpenNextNode = open ? 1 : 0;
  3052. }
  3053. void ImGui::PushID(const char* str_id)
  3054. {
  3055. ImGuiWindow* window = GetCurrentWindow();
  3056. window->IDStack.push_back(window->GetID(str_id));
  3057. }
  3058. void ImGui::PushID(const void* ptr_id)
  3059. {
  3060. ImGuiWindow* window = GetCurrentWindow();
  3061. window->IDStack.push_back(window->GetID(ptr_id));
  3062. }
  3063. void ImGui::PushID(const int int_id)
  3064. {
  3065. const void* ptr_id = (void*)(intptr_t)int_id;
  3066. ImGuiWindow* window = GetCurrentWindow();
  3067. window->IDStack.push_back(window->GetID(ptr_id));
  3068. }
  3069. void ImGui::PopID()
  3070. {
  3071. ImGuiWindow* window = GetCurrentWindow();
  3072. window->IDStack.pop_back();
  3073. }
  3074. // User can input math operators (e.g. +100) to edit a numerical values.
  3075. // NB: only call right after InputText because we are using its InitialValue storage
  3076. static void ApplyNumericalTextInput(const char* buf, float *v)
  3077. {
  3078. while (*buf == ' ' || *buf == '\t')
  3079. buf++;
  3080. // We don't support '-' op because it would conflict with inputing negative value.
  3081. // Instead you can use +-100 to subtract from an existing value
  3082. char op = buf[0];
  3083. if (op == '+' || op == '*' || op == '/')
  3084. {
  3085. buf++;
  3086. while (*buf == ' ' || *buf == '\t')
  3087. buf++;
  3088. }
  3089. else
  3090. {
  3091. op = 0;
  3092. }
  3093. if (!buf[0])
  3094. return;
  3095. float ref_v = *v;
  3096. if (op)
  3097. if (sscanf(GImGui.InputTextState.InitialText, "%f", &ref_v) < 1)
  3098. return;
  3099. float op_v = 0.0f;
  3100. if (sscanf(buf, "%f", &op_v) < 1)
  3101. return;
  3102. if (op == '+')
  3103. *v = ref_v + op_v;
  3104. else if (op == '*')
  3105. *v = ref_v * op_v;
  3106. else if (op == '/')
  3107. {
  3108. if (op_v == 0.0f)
  3109. return;
  3110. *v = ref_v / op_v;
  3111. }
  3112. else
  3113. *v = op_v;
  3114. }
  3115. // Use power!=1.0 for logarithmic sliders.
  3116. // Adjust display_format to decorate the value with a prefix or a suffix.
  3117. bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, const char* display_format, float power)
  3118. {
  3119. ImGuiState& g = GImGui;
  3120. ImGuiWindow* window = GetCurrentWindow();
  3121. if (window->SkipItems)
  3122. return false;
  3123. const ImGuiStyle& style = g.Style;
  3124. const ImGuiID id = window->GetID(label);
  3125. const float w = window->DC.ItemWidth.back();
  3126. if (!display_format)
  3127. display_format = "%.3f";
  3128. // Parse display precision back from the display format string
  3129. int decimal_precision = 3;
  3130. if (const char* p = strchr(display_format, '%'))
  3131. {
  3132. p++;
  3133. while (*p >= '0' && *p <= '9')
  3134. p++;
  3135. if (*p == '.')
  3136. {
  3137. decimal_precision = atoi(p+1);
  3138. if (decimal_precision < 0 || decimal_precision > 10)
  3139. decimal_precision = 3;
  3140. }
  3141. }
  3142. const ImVec2 text_size = CalcTextSize(label);
  3143. const ImGuiAabb frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, text_size.y) + style.FramePadding*2.0f);
  3144. const ImGuiAabb slider_bb(frame_bb.Min+g.Style.FramePadding, frame_bb.Max-g.Style.FramePadding);
  3145. const ImGuiAabb bb(frame_bb.Min, frame_bb.Max + ImVec2(style.ItemInnerSpacing.x + text_size.x, 0.0f));
  3146. if (IsClipped(slider_bb))
  3147. {
  3148. // NB- we don't use ClipAdvance() in the if() statement because we don't want to submit ItemSize() because we may change into a text edit later which may submit an ItemSize itself
  3149. ItemSize(bb);
  3150. return false;
  3151. }
  3152. const bool tab_focus_requested = window->FocusItemRegister(g.ActiveId == id);
  3153. const bool is_unbound = v_min == -FLT_MAX || v_min == FLT_MAX || v_max == -FLT_MAX || v_max == FLT_MAX;
  3154. const float grab_size_in_units = 1.0f; // In 'v' units. Probably needs to be parametrized, based on a 'v_step' value? decimal precision?
  3155. float grab_size_in_pixels;
  3156. if (decimal_precision > 0 || is_unbound)
  3157. grab_size_in_pixels = 10.0f;
  3158. else
  3159. grab_size_in_pixels = ImMax(grab_size_in_units * (w / (v_max-v_min+1.0f)), 8.0f); // Integer sliders
  3160. const float slider_effective_w = slider_bb.GetWidth() - grab_size_in_pixels;
  3161. const float slider_effective_x1 = slider_bb.Min.x + grab_size_in_pixels*0.5f;
  3162. const float slider_effective_x2 = slider_bb.Max.x - grab_size_in_pixels*0.5f;
  3163. // For logarithmic sliders that cross over sign boundary we want the exponential increase to be symmetric around 0.0f
  3164. float linear_zero_pos = 0.0f; // 0.0->1.0f
  3165. if (!is_unbound)
  3166. {
  3167. if (v_min * v_max < 0.0f)
  3168. {
  3169. // Different sign
  3170. const float linear_dist_min_to_0 = powf(fabsf(0.0f - v_min), 1.0f/power);
  3171. const float linear_dist_max_to_0 = powf(fabsf(v_max - 0.0f), 1.0f/power);
  3172. linear_zero_pos = linear_dist_min_to_0 / (linear_dist_min_to_0+linear_dist_max_to_0);
  3173. }
  3174. else
  3175. {
  3176. // Same sign
  3177. linear_zero_pos = v_min < 0.0f ? 1.0f : 0.0f;
  3178. }
  3179. }
  3180. const bool hovered = (g.HoveredWindow == window) && (g.HoveredId == 0) && IsMouseHoveringBox(slider_bb);
  3181. if (hovered)
  3182. g.HoveredId = id;
  3183. bool start_text_input = false;
  3184. if (tab_focus_requested || (hovered && g.IO.MouseClicked[0]))
  3185. {
  3186. g.ActiveId = id;
  3187. const bool is_ctrl_down = g.IO.KeyCtrl;
  3188. if (tab_focus_requested || is_ctrl_down || is_unbound)
  3189. {
  3190. start_text_input = true;
  3191. g.SliderAsInputTextId = 0;
  3192. }
  3193. }
  3194. // Tabbing or CTRL-clicking through slider turns into an input box
  3195. bool value_changed = false;
  3196. if (start_text_input || (g.ActiveId == id && id == g.SliderAsInputTextId))
  3197. {
  3198. char text_buf[64];
  3199. ImFormatString(text_buf, IM_ARRAYSIZE(text_buf), "%.*f", decimal_precision, *v);
  3200. g.ActiveId = g.SliderAsInputTextId;
  3201. g.HoveredId = 0;
  3202. window->FocusItemUnregister(); // Our replacement slider will override the focus ID (registered previously to allow for a TAB focus to happen)
  3203. value_changed = ImGui::InputText(label, text_buf, IM_ARRAYSIZE(text_buf), ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_AutoSelectAll);
  3204. if (g.SliderAsInputTextId == 0)
  3205. {
  3206. // First frame
  3207. IM_ASSERT(g.ActiveId == id); // InputText ID should match the Slider ID (else we'd need to store them both which is also possible)
  3208. g.SliderAsInputTextId = g.ActiveId;
  3209. g.ActiveId = id;
  3210. g.HoveredId = id;
  3211. }
  3212. else
  3213. {
  3214. if (g.ActiveId == g.SliderAsInputTextId)
  3215. g.ActiveId = id;
  3216. else
  3217. g.ActiveId = g.SliderAsInputTextId = 0;
  3218. }
  3219. if (value_changed)
  3220. {
  3221. ApplyNumericalTextInput(text_buf, v);
  3222. }
  3223. return value_changed;
  3224. }
  3225. ItemSize(bb);
  3226. RenderFrame(frame_bb.Min, frame_bb.Max, window->Color(ImGuiCol_FrameBg));
  3227. // Process clicking on the slider
  3228. if (g.ActiveId == id)
  3229. {
  3230. if (g.IO.MouseDown[0])
  3231. {
  3232. if (!is_unbound)
  3233. {
  3234. const float normalized_pos = ImClamp((g.IO.MousePos.x - slider_effective_x1) / slider_effective_w, 0.0f, 1.0f);
  3235. // Linear slider
  3236. //float new_value = ImLerp(v_min, v_max, normalized_pos);
  3237. // Account for logarithmic scale on both sides of the zero
  3238. float new_value;
  3239. if (normalized_pos < linear_zero_pos)
  3240. {
  3241. // Negative: rescale to the negative range before powering
  3242. float a = 1.0f - (normalized_pos / linear_zero_pos);
  3243. a = powf(a, power);
  3244. new_value = ImLerp(ImMin(v_max,0.f), v_min, a);
  3245. }
  3246. else
  3247. {
  3248. // Positive: rescale to the positive range before powering
  3249. float a;
  3250. if (fabsf(linear_zero_pos - 1.0f) > 1.e-6)
  3251. a = (normalized_pos - linear_zero_pos) / (1.0f - linear_zero_pos);
  3252. else
  3253. a = normalized_pos;
  3254. a = powf(a, power);
  3255. new_value = ImLerp(ImMax(v_min,0.0f), v_max, a);
  3256. }
  3257. // Round past decimal precision
  3258. // 0->1, 1->0.1, 2->0.01, etc.
  3259. // So when our value is 1.99999 with a precision of 0.001 we'll end up rounding to 2.0
  3260. const float min_step = 1.0f / powf(10.0f, (float)decimal_precision);
  3261. const float remainder = fmodf(new_value, min_step);
  3262. if (remainder <= min_step*0.5f)
  3263. new_value -= remainder;
  3264. else
  3265. new_value += (min_step - remainder);
  3266. if (*v != new_value)
  3267. {
  3268. *v = new_value;
  3269. value_changed = true;
  3270. }
  3271. }
  3272. }
  3273. else
  3274. {
  3275. g.ActiveId = 0;
  3276. }
  3277. }
  3278. if (!is_unbound)
  3279. {
  3280. // Linear slider
  3281. // const float grab_t = (ImClamp(*v, v_min, v_max) - v_min) / (v_max - v_min);
  3282. // Calculate slider grab positioning
  3283. float grab_t;
  3284. float v_clamped = ImClamp(*v, v_min, v_max);
  3285. if (v_clamped < 0.0f)
  3286. {
  3287. const float f = 1.0f - (v_clamped - v_min) / (ImMin(0.0f,v_max) - v_min);
  3288. grab_t = (1.0f - powf(f, 1.0f/power)) * linear_zero_pos;
  3289. }
  3290. else
  3291. {
  3292. const float f = (v_clamped - ImMax(0.0f,v_min)) / (v_max - ImMax(0.0f,v_min));
  3293. grab_t = linear_zero_pos + powf(f, 1.0f/power) * (1.0f - linear_zero_pos);
  3294. }
  3295. // Draw
  3296. const float grab_x = ImLerp(slider_effective_x1, slider_effective_x2, grab_t);
  3297. const ImGuiAabb grab_bb(ImVec2(grab_x-grab_size_in_pixels*0.5f,frame_bb.Min.y+2.0f), ImVec2(grab_x+grab_size_in_pixels*0.5f,frame_bb.Max.y-2.0f));
  3298. window->DrawList->AddRectFilled(grab_bb.Min, grab_bb.Max, window->Color(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab));
  3299. }
  3300. // Draw value using user-provided display format so user can add prefix/suffix/decorations to the value.
  3301. char value_buf[64];
  3302. ImFormatString(value_buf, IM_ARRAYSIZE(value_buf), display_format, *v);
  3303. RenderText(ImVec2(slider_bb.GetCenter().x-CalcTextSize(value_buf).x*0.5f, frame_bb.Min.y + style.FramePadding.y), value_buf);
  3304. RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, slider_bb.Min.y), label);
  3305. return value_changed;
  3306. }
  3307. bool ImGui::SliderAngle(const char* label, float* v, float v_degrees_min, float v_degrees_max)
  3308. {
  3309. float v_deg = *v * 360.0f / (2*PI);
  3310. bool value_changed = ImGui::SliderFloat(label, &v_deg, v_degrees_min, v_degrees_max, "%.0f deg", 1.0f);
  3311. *v = v_deg * (2*PI) / 360.0f;
  3312. return value_changed;
  3313. }
  3314. bool ImGui::SliderInt(const char* label, int* v, int v_min, int v_max, const char* display_format)
  3315. {
  3316. if (!display_format)
  3317. display_format = "%.0f";
  3318. float v_f = (float)*v;
  3319. bool value_changed = ImGui::SliderFloat(label, &v_f, (float)v_min, (float)v_max, display_format, 1.0f);
  3320. *v = (int)v_f;
  3321. return value_changed;
  3322. }
  3323. // Add multiple sliders on 1 line for compact edition of multiple components
  3324. static bool SliderFloatN(const char* label, float v[3], int components, float v_min, float v_max, const char* display_format, float power)
  3325. {
  3326. ImGuiState& g = GImGui;
  3327. ImGuiWindow* window = GetCurrentWindow();
  3328. if (window->SkipItems)
  3329. return false;
  3330. const ImGuiStyle& style = g.Style;
  3331. const float w_full = window->DC.ItemWidth.back();
  3332. const float w_item_one = ImMax(1.0f, (float)(int)((w_full - (style.FramePadding.x*2.0f+style.ItemInnerSpacing.x)*(components-1)) / (float)components));
  3333. 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)));
  3334. bool value_changed = false;
  3335. ImGui::PushID(label);
  3336. ImGui::PushItemWidth(w_item_one);
  3337. for (int i = 0; i < components; i++)
  3338. {
  3339. ImGui::PushID(i);
  3340. if (i + 1 == components)
  3341. {
  3342. ImGui::PopItemWidth();
  3343. ImGui::PushItemWidth(w_item_last);
  3344. }
  3345. value_changed |= ImGui::SliderFloat("##v", &v[i], v_min, v_max, display_format, power);
  3346. ImGui::SameLine(0, 0);
  3347. ImGui::PopID();
  3348. }
  3349. ImGui::PopItemWidth();
  3350. ImGui::PopID();
  3351. ImGui::TextUnformatted(label, FindTextDisplayEnd(label));
  3352. return value_changed;
  3353. }
  3354. bool ImGui::SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* display_format, float power)
  3355. {
  3356. return SliderFloatN(label, v, 2, v_min, v_max, display_format, power);
  3357. }
  3358. bool ImGui::SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* display_format, float power)
  3359. {
  3360. return SliderFloatN(label, v, 3, v_min, v_max, display_format, power);
  3361. }
  3362. bool ImGui::SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* display_format, float power)
  3363. {
  3364. return SliderFloatN(label, v, 4, v_min, v_max, display_format, power);
  3365. }
  3366. enum ImGuiPlotType
  3367. {
  3368. ImGuiPlotType_Lines,
  3369. ImGuiPlotType_Histogram
  3370. };
  3371. 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)
  3372. {
  3373. ImGuiState& g = GImGui;
  3374. ImGuiWindow* window = GetCurrentWindow();
  3375. if (window->SkipItems)
  3376. return;
  3377. const ImGuiStyle& style = g.Style;
  3378. const ImVec2 text_size = ImGui::CalcTextSize(label);
  3379. if (graph_size.x == 0.0f)
  3380. graph_size.x = window->DC.ItemWidth.back();
  3381. if (graph_size.y == 0.0f)
  3382. graph_size.y = text_size.y;
  3383. const ImGuiAabb frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(graph_size.x, graph_size.y) + style.FramePadding*2.0f);
  3384. const ImGuiAabb graph_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding);
  3385. const ImGuiAabb bb(frame_bb.Min, frame_bb.Max + ImVec2(style.ItemInnerSpacing.x + text_size.x,0));
  3386. ItemSize(bb);
  3387. if (ClipAdvance(bb))
  3388. return;
  3389. // Determine scale from values if not specified
  3390. if (scale_min == FLT_MAX || scale_max == FLT_MAX)
  3391. {
  3392. float v_min = FLT_MAX;
  3393. float v_max = -FLT_MAX;
  3394. for (int i = 0; i < values_count; i++)
  3395. {
  3396. const float v = values_getter(data, i);
  3397. v_min = ImMin(v_min, v);
  3398. v_max = ImMax(v_max, v);
  3399. }
  3400. if (scale_min == FLT_MAX)
  3401. scale_min = v_min;
  3402. if (scale_max == FLT_MAX)
  3403. scale_max = v_max;
  3404. }
  3405. RenderFrame(frame_bb.Min, frame_bb.Max, window->Color(ImGuiCol_FrameBg));
  3406. int res_w = ImMin((int)graph_size.x, values_count);
  3407. if (plot_type == ImGuiPlotType_Lines)
  3408. res_w -= 1;
  3409. // Tooltip on hover
  3410. int v_hovered = -1;
  3411. if (IsMouseHoveringBox(graph_bb))
  3412. {
  3413. const float t = ImClamp((g.IO.MousePos.x - graph_bb.Min.x) / (graph_bb.Max.x - graph_bb.Min.x), 0.0f, 0.9999f);
  3414. const int v_idx = (int)(t * (values_count + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0)));
  3415. IM_ASSERT(v_idx >= 0 && v_idx < values_count);
  3416. const float v0 = values_getter(data, (v_idx + values_offset) % values_count);
  3417. const float v1 = values_getter(data, (v_idx + 1 + values_offset) % values_count);
  3418. if (plot_type == ImGuiPlotType_Lines)
  3419. ImGui::SetTooltip("%d: %8.4g\n%d: %8.4g", v_idx, v0, v_idx+1, v1);
  3420. else if (plot_type == ImGuiPlotType_Histogram)
  3421. ImGui::SetTooltip("%d: %8.4g", v_idx, v0);
  3422. v_hovered = v_idx;
  3423. }
  3424. const float t_step = 1.0f / (float)res_w;
  3425. float v0 = values_getter(data, (0 + values_offset) % values_count);
  3426. float t0 = 0.0f;
  3427. ImVec2 p0 = ImVec2( t0, 1.0f - ImSaturate((v0 - scale_min) / (scale_max - scale_min)) );
  3428. const ImU32 col_base = window->Color((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLines : ImGuiCol_PlotHistogram);
  3429. const ImU32 col_hovered = window->Color((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLinesHovered : ImGuiCol_PlotHistogramHovered);
  3430. for (int n = 0; n < res_w; n++)
  3431. {
  3432. const float t1 = t0 + t_step;
  3433. const int v_idx = (int)(t0 * values_count);
  3434. IM_ASSERT(v_idx >= 0 && v_idx < values_count);
  3435. const float v1 = values_getter(data, (v_idx + values_offset + 1) % values_count);
  3436. const ImVec2 p1 = ImVec2( t1, 1.0f - ImSaturate((v1 - scale_min) / (scale_max - scale_min)) );
  3437. // NB- Draw calls are merged together by the DrawList system.
  3438. if (plot_type == ImGuiPlotType_Lines)
  3439. window->DrawList->AddLine(ImLerp(graph_bb.Min, graph_bb.Max, p0), ImLerp(graph_bb.Min, graph_bb.Max, p1), v_hovered == v_idx ? col_hovered : col_base);
  3440. else if (plot_type == ImGuiPlotType_Histogram)
  3441. window->DrawList->AddRectFilled(ImLerp(graph_bb.Min, graph_bb.Max, p0), ImLerp(graph_bb.Min, graph_bb.Max, ImVec2(p1.x, 1.0f))+ImVec2(-1,0), v_hovered == v_idx ? col_hovered : col_base);
  3442. t0 = t1;
  3443. p0 = p1;
  3444. }
  3445. // Text overlay
  3446. if (overlay_text)
  3447. RenderText(ImVec2(graph_bb.GetCenter().x - ImGui::CalcTextSize(overlay_text).x*0.5f, frame_bb.Min.y + style.FramePadding.y), overlay_text);
  3448. RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, graph_bb.Min.y), label);
  3449. }
  3450. struct ImGuiPlotArrayGetterData
  3451. {
  3452. const float* Values;
  3453. size_t Stride;
  3454. ImGuiPlotArrayGetterData(const float* values, size_t stride) { Values = values; Stride = stride; }
  3455. };
  3456. static float Plot_ArrayGetter(void* data, int idx)
  3457. {
  3458. ImGuiPlotArrayGetterData* plot_data = (ImGuiPlotArrayGetterData*)data;
  3459. const float v = *(float*)(void*)((unsigned char*)plot_data->Values + (size_t)idx * plot_data->Stride);
  3460. return v;
  3461. }
  3462. 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, size_t stride)
  3463. {
  3464. ImGuiPlotArrayGetterData data(values, stride);
  3465. Plot(ImGuiPlotType_Lines, label, &Plot_ArrayGetter, (void*)&data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size);
  3466. }
  3467. 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)
  3468. {
  3469. Plot(ImGuiPlotType_Lines, label, values_getter, data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size);
  3470. }
  3471. 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, size_t stride)
  3472. {
  3473. ImGuiPlotArrayGetterData data(values, stride);
  3474. Plot(ImGuiPlotType_Histogram, label, &Plot_ArrayGetter, (void*)&data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size);
  3475. }
  3476. 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)
  3477. {
  3478. Plot(ImGuiPlotType_Histogram, label, values_getter, data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size);
  3479. }
  3480. bool ImGui::Checkbox(const char* label, bool* v)
  3481. {
  3482. ImGuiState& g = GImGui;
  3483. ImGuiWindow* window = GetCurrentWindow();
  3484. if (window->SkipItems)
  3485. return false;
  3486. const ImGuiStyle& style = g.Style;
  3487. const ImGuiID id = window->GetID(label);
  3488. const ImVec2 text_size = CalcTextSize(label);
  3489. const ImGuiAabb check_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(text_size.y + style.FramePadding.y*2, text_size.y + style.FramePadding.y*2));
  3490. ItemSize(check_bb);
  3491. SameLine(0, (int)g.Style.ItemInnerSpacing.x);
  3492. const ImGuiAabb text_bb(window->DC.CursorPos + ImVec2(0,style.FramePadding.y), window->DC.CursorPos + ImVec2(0,style.FramePadding.y) + text_size);
  3493. ItemSize(ImVec2(text_bb.GetWidth(), check_bb.GetHeight()));
  3494. const ImGuiAabb total_bb(ImMin(check_bb.Min, text_bb.Min), ImMax(check_bb.Max, text_bb.Max));
  3495. if (ClipAdvance(total_bb))
  3496. return false;
  3497. const bool hovered = (g.HoveredWindow == window) && (g.HoveredId == 0) && IsMouseHoveringBox(total_bb);
  3498. const bool pressed = hovered && g.IO.MouseClicked[0];
  3499. if (hovered)
  3500. g.HoveredId = id;
  3501. if (pressed)
  3502. {
  3503. *v = !(*v);
  3504. g.ActiveId = 0; // Clear focus
  3505. }
  3506. RenderFrame(check_bb.Min, check_bb.Max, window->Color(hovered ? ImGuiCol_CheckHovered : ImGuiCol_FrameBg));
  3507. if (*v)
  3508. {
  3509. const float check_sz = ImMin(check_bb.GetWidth(), check_bb.GetHeight());
  3510. const float pad = check_sz < 8.0f ? 1.0f : check_sz < 13.0f ? 2.0f : 3.0f;
  3511. window->DrawList->AddRectFilled(check_bb.Min+ImVec2(pad,pad), check_bb.Max-ImVec2(pad,pad), window->Color(ImGuiCol_CheckActive));
  3512. }
  3513. if (g.LogEnabled)
  3514. LogText(text_bb.GetTL(), *v ? "[x]" : "[ ]");
  3515. RenderText(text_bb.GetTL(), label);
  3516. return pressed;
  3517. }
  3518. bool ImGui::CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value)
  3519. {
  3520. bool v = (*flags & flags_value) ? true : false;
  3521. bool pressed = ImGui::Checkbox(label, &v);
  3522. if (v)
  3523. *flags |= flags_value;
  3524. else
  3525. *flags &= ~flags_value;
  3526. return pressed;
  3527. }
  3528. bool ImGui::RadioButton(const char* label, bool active)
  3529. {
  3530. ImGuiState& g = GImGui;
  3531. ImGuiWindow* window = GetCurrentWindow();
  3532. if (window->SkipItems)
  3533. return false;
  3534. const ImGuiStyle& style = g.Style;
  3535. const ImGuiID id = window->GetID(label);
  3536. const ImVec2 text_size = CalcTextSize(label);
  3537. const ImGuiAabb check_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(text_size.y + style.FramePadding.y*2-1, text_size.y + style.FramePadding.y*2-1));
  3538. ItemSize(check_bb);
  3539. SameLine(0, (int)style.ItemInnerSpacing.x);
  3540. const ImGuiAabb text_bb(window->DC.CursorPos + ImVec2(0, style.FramePadding.y), window->DC.CursorPos + ImVec2(0, style.FramePadding.y) + text_size);
  3541. ItemSize(ImVec2(text_bb.GetWidth(), check_bb.GetHeight()));
  3542. const ImGuiAabb total_bb(ImMin(check_bb.Min, text_bb.Min), ImMax(check_bb.Max, text_bb.Max));
  3543. if (ClipAdvance(total_bb))
  3544. return false;
  3545. ImVec2 center = check_bb.GetCenter();
  3546. center.x = (float)(int)center.x + 0.5f;
  3547. center.y = (float)(int)center.y + 0.5f;
  3548. const float radius = check_bb.GetHeight() * 0.5f;
  3549. const bool hovered = (g.HoveredWindow == window) && (g.HoveredId == 0) && IsMouseHoveringBox(total_bb);
  3550. const bool pressed = hovered && g.IO.MouseClicked[0];
  3551. if (hovered)
  3552. g.HoveredId = id;
  3553. window->DrawList->AddCircleFilled(center, radius, window->Color(hovered ? ImGuiCol_CheckHovered : ImGuiCol_FrameBg), 16);
  3554. if (active)
  3555. {
  3556. const float check_sz = ImMin(check_bb.GetWidth(), check_bb.GetHeight());
  3557. const float pad = check_sz < 8.0f ? 1.0f : check_sz < 13.0f ? 2.0f : 3.0f;
  3558. window->DrawList->AddCircleFilled(center, radius-pad, window->Color(ImGuiCol_CheckActive), 16);
  3559. }
  3560. if (window->Flags & ImGuiWindowFlags_ShowBorders)
  3561. {
  3562. window->DrawList->AddCircle(center+ImVec2(1,1), radius, window->Color(ImGuiCol_BorderShadow), 16);
  3563. window->DrawList->AddCircle(center, radius, window->Color(ImGuiCol_Border), 16);
  3564. }
  3565. if (g.LogEnabled)
  3566. LogText(text_bb.GetTL(), active ? "(x)" : "( )");
  3567. RenderText(text_bb.GetTL(), label);
  3568. return pressed;
  3569. }
  3570. bool ImGui::RadioButton(const char* label, int* v, int v_button)
  3571. {
  3572. const bool pressed = ImGui::RadioButton(label, *v == v_button);
  3573. if (pressed)
  3574. {
  3575. *v = v_button;
  3576. }
  3577. return pressed;
  3578. }
  3579. // 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)
  3580. static int STB_TEXTEDIT_STRINGLEN(const STB_TEXTEDIT_STRING* obj) { return (int)ImStrlenW(obj->Text); }
  3581. static ImWchar STB_TEXTEDIT_GETCHAR(const STB_TEXTEDIT_STRING* obj, int idx) { return obj->Text[idx]; }
  3582. static float STB_TEXTEDIT_GETWIDTH(STB_TEXTEDIT_STRING* obj, int line_start_idx, int char_idx) { (void)line_start_idx; return obj->Font->CalcTextSizeW(obj->FontSize, FLT_MAX, &obj->Text[char_idx], &obj->Text[char_idx]+1, NULL).x; }
  3583. static int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x10000 ? 0 : key; }
  3584. static ImWchar STB_TEXTEDIT_NEWLINE = '\n';
  3585. static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, STB_TEXTEDIT_STRING* obj, int line_start_idx)
  3586. {
  3587. const ImWchar* text_remaining = NULL;
  3588. const ImVec2 size = obj->Font->CalcTextSizeW(obj->FontSize, FLT_MAX, obj->Text + line_start_idx, NULL, &text_remaining);
  3589. r->x0 = 0.0f;
  3590. r->x1 = size.x;
  3591. r->baseline_y_delta = size.y;
  3592. r->ymin = 0.0f;
  3593. r->ymax = size.y;
  3594. r->num_chars = (int)(text_remaining - (obj->Text + line_start_idx));
  3595. }
  3596. static bool is_white(unsigned int c) { return c==0 || c==' ' || c=='\t' || c=='\r' || c=='\n'; }
  3597. static bool is_separator(unsigned int c) { return c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|'; }
  3598. #define STB_TEXTEDIT_IS_SPACE(CH) ( is_white((unsigned int)CH) || is_separator((unsigned int)CH) )
  3599. static void STB_TEXTEDIT_DELETECHARS(STB_TEXTEDIT_STRING* obj, int pos, int n) { ImWchar* dst = obj->Text+pos; const ImWchar* src = obj->Text+pos+n; while (ImWchar c = *src++) *dst++ = c; *dst = '\0'; }
  3600. static bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING* obj, int pos, const ImWchar* new_text, int new_text_len)
  3601. {
  3602. const size_t text_len = ImStrlenW(obj->Text);
  3603. if ((size_t)new_text_len + text_len + 1 >= obj->BufSize)
  3604. return false;
  3605. if (pos != (int)text_len)
  3606. memmove(obj->Text + (size_t)pos + new_text_len, obj->Text + (size_t)pos, (text_len - (size_t)pos) * sizeof(ImWchar));
  3607. memcpy(obj->Text + (size_t)pos, new_text, (size_t)new_text_len * sizeof(ImWchar));
  3608. obj->Text[text_len + (size_t)new_text_len] = '\0';
  3609. return true;
  3610. }
  3611. enum
  3612. {
  3613. STB_TEXTEDIT_K_LEFT = 1 << 16, // keyboard input to move cursor left
  3614. STB_TEXTEDIT_K_RIGHT, // keyboard input to move cursor right
  3615. STB_TEXTEDIT_K_UP, // keyboard input to move cursor up
  3616. STB_TEXTEDIT_K_DOWN, // keyboard input to move cursor down
  3617. STB_TEXTEDIT_K_LINESTART, // keyboard input to move cursor to start of line
  3618. STB_TEXTEDIT_K_LINEEND, // keyboard input to move cursor to end of line
  3619. STB_TEXTEDIT_K_TEXTSTART, // keyboard input to move cursor to start of text
  3620. STB_TEXTEDIT_K_TEXTEND, // keyboard input to move cursor to end of text
  3621. STB_TEXTEDIT_K_DELETE, // keyboard input to delete selection or character under cursor
  3622. STB_TEXTEDIT_K_BACKSPACE, // keyboard input to delete selection or character left of cursor
  3623. STB_TEXTEDIT_K_UNDO, // keyboard input to perform undo
  3624. STB_TEXTEDIT_K_REDO, // keyboard input to perform redo
  3625. STB_TEXTEDIT_K_WORDLEFT, // keyboard input to move cursor left one word
  3626. STB_TEXTEDIT_K_WORDRIGHT, // keyboard input to move cursor right one word
  3627. STB_TEXTEDIT_K_SHIFT = 1 << 17
  3628. };
  3629. #define STB_TEXTEDIT_IMPLEMENTATION
  3630. #include "stb_textedit.h"
  3631. void ImGuiTextEditState::OnKeyPressed(int key)
  3632. {
  3633. stb_textedit_key(this, &StbState, key);
  3634. CursorAnimReset();
  3635. }
  3636. void ImGuiTextEditState::UpdateScrollOffset()
  3637. {
  3638. // Scroll in chunks of quarter width
  3639. const float scroll_x_increment = Width * 0.25f;
  3640. const float cursor_offset_x = Font->CalcTextSizeW(FontSize, FLT_MAX, Text, Text+StbState.cursor, NULL).x;
  3641. if (ScrollX > cursor_offset_x)
  3642. ScrollX = ImMax(0.0f, cursor_offset_x - scroll_x_increment);
  3643. else if (ScrollX < cursor_offset_x - Width)
  3644. ScrollX = cursor_offset_x - Width + scroll_x_increment;
  3645. }
  3646. ImVec2 ImGuiTextEditState::CalcDisplayOffsetFromCharIdx(int i) const
  3647. {
  3648. const ImWchar* text_start = GetTextPointerClippedW(Font, FontSize, Text, ScrollX, NULL);
  3649. const ImWchar* text_end = (Text+i >= text_start) ? Text+i : text_start; // Clip if requested character is outside of display
  3650. IM_ASSERT(text_end >= text_start);
  3651. const ImVec2 offset = Font->CalcTextSizeW(FontSize, Width, text_start, text_end, NULL);
  3652. return offset;
  3653. }
  3654. // [Static]
  3655. const char* ImGuiTextEditState::GetTextPointerClippedA(ImFont* font, float font_size, const char* text, float width, ImVec2* out_text_size)
  3656. {
  3657. if (width <= 0.0f)
  3658. return text;
  3659. const char* text_clipped_end = NULL;
  3660. const ImVec2 text_size = font->CalcTextSizeA(font_size, width, 0.0f, text, NULL, &text_clipped_end);
  3661. if (out_text_size)
  3662. *out_text_size = text_size;
  3663. return text_clipped_end;
  3664. }
  3665. // [Static]
  3666. const ImWchar* ImGuiTextEditState::GetTextPointerClippedW(ImFont* font, float font_size, const ImWchar* text, float width, ImVec2* out_text_size)
  3667. {
  3668. if (width <= 0.0f)
  3669. return text;
  3670. const ImWchar* text_clipped_end = NULL;
  3671. const ImVec2 text_size = font->CalcTextSizeW(font_size, width, text, NULL, &text_clipped_end);
  3672. if (out_text_size)
  3673. *out_text_size = text_size;
  3674. return text_clipped_end;
  3675. }
  3676. // [Static]
  3677. void ImGuiTextEditState::RenderTextScrolledClipped(ImFont* font, float font_size, const char* buf, ImVec2 pos, float width, float scroll_x)
  3678. {
  3679. // NB- We start drawing at character boundary
  3680. ImVec2 text_size;
  3681. const char* text_start = GetTextPointerClippedA(font, font_size, buf, scroll_x, NULL);
  3682. const char* text_end = GetTextPointerClippedA(font, font_size, text_start, width, &text_size);
  3683. // Draw a little clip symbol if we've got text on either left or right of the box
  3684. const char symbol_c = '~';
  3685. const float symbol_w = font_size*0.40f; // FIXME: compute correct width
  3686. const float clip_begin = (text_start > buf && text_start < text_end) ? symbol_w : 0.0f;
  3687. const float clip_end = (text_end[0] != '\0' && text_end > text_start) ? symbol_w : 0.0f;
  3688. // Draw text
  3689. RenderText(pos+ImVec2(clip_begin,0), text_start+(clip_begin>0.0f?1:0), text_end-(clip_end>0.0f?1:0), false);//, &text_params_with_clipping);
  3690. // Draw the clip symbol
  3691. const char s[2] = {symbol_c,'\0'};
  3692. if (clip_begin > 0.0f)
  3693. RenderText(pos, s);
  3694. if (clip_end > 0.0f)
  3695. RenderText(pos+ImVec2(width-clip_end,0.0f), s);
  3696. }
  3697. bool ImGui::InputFloat(const char* label, float *v, float step, float step_fast, int decimal_precision, ImGuiInputTextFlags extra_flags)
  3698. {
  3699. ImGuiState& g = GImGui;
  3700. ImGuiWindow* window = GetCurrentWindow();
  3701. if (window->SkipItems)
  3702. return false;
  3703. const ImGuiStyle& style = g.Style;
  3704. const float w = window->DC.ItemWidth.back();
  3705. const ImVec2 text_size = CalcTextSize(label);
  3706. const ImGuiAabb frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, text_size.y) + style.FramePadding*2.0f);
  3707. ImGui::PushID(label);
  3708. const float button_sz = window->FontSize();
  3709. if (step > 0.0f)
  3710. ImGui::PushItemWidth(ImMax(1.0f, window->DC.ItemWidth.back() - (button_sz+g.Style.FramePadding.x*2.0f+g.Style.ItemInnerSpacing.x)*2));
  3711. char buf[64];
  3712. if (decimal_precision < 0)
  3713. 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?
  3714. else
  3715. ImFormatString(buf, IM_ARRAYSIZE(buf), "%.*f", decimal_precision, *v);
  3716. bool value_changed = false;
  3717. const ImGuiInputTextFlags flags = extra_flags | (ImGuiInputTextFlags_CharsDecimal|ImGuiInputTextFlags_AutoSelectAll);
  3718. if (ImGui::InputText("", buf, IM_ARRAYSIZE(buf), flags))
  3719. {
  3720. ApplyNumericalTextInput(buf, v);
  3721. value_changed = true;
  3722. }
  3723. // Step buttons
  3724. if (step > 0.0f)
  3725. {
  3726. ImGui::PopItemWidth();
  3727. ImGui::SameLine(0, 0);
  3728. if (ImGui::Button("-", ImVec2(button_sz,button_sz), true))
  3729. {
  3730. *v -= g.IO.KeyCtrl && step_fast > 0.0f ? step_fast : step;
  3731. value_changed = true;
  3732. }
  3733. ImGui::SameLine(0, (int)g.Style.ItemInnerSpacing.x);
  3734. if (ImGui::Button("+", ImVec2(button_sz,button_sz), true))
  3735. {
  3736. *v += g.IO.KeyCtrl && step_fast > 0.0f ? step_fast : step;
  3737. value_changed = true;
  3738. }
  3739. }
  3740. ImGui::PopID();
  3741. RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + g.Style.FramePadding.y), label);
  3742. return value_changed;
  3743. }
  3744. bool ImGui::InputInt(const char* label, int *v, int step, int step_fast, ImGuiInputTextFlags extra_flags)
  3745. {
  3746. float f = (float)*v;
  3747. const bool value_changed = ImGui::InputFloat(label, &f, (float)step, (float)step_fast, 0, extra_flags);
  3748. *v = (int)f;
  3749. return value_changed;
  3750. }
  3751. // Public API to manipulate UTF-8 text
  3752. // We expose UTF-8 to the user (unlike the STB_TEXTEDIT_* functions which are manipulating wchar)
  3753. void ImGuiTextEditCallbackData::DeleteChars(int pos, int bytes_count)
  3754. {
  3755. char* dst = Buf + pos;
  3756. const char* src = Buf + pos + bytes_count;
  3757. while (char c = *src++)
  3758. *dst++ = c;
  3759. *dst = '\0';
  3760. BufDirty = true;
  3761. if (CursorPos + bytes_count >= pos)
  3762. CursorPos -= bytes_count;
  3763. else if (CursorPos >= pos)
  3764. CursorPos = pos;
  3765. SelectionStart = SelectionEnd = CursorPos;
  3766. }
  3767. void ImGuiTextEditCallbackData::InsertChars(int pos, const char* new_text, const char* new_text_end)
  3768. {
  3769. const size_t text_len = strlen(Buf);
  3770. if (!new_text_end)
  3771. new_text_end = new_text + strlen(new_text);
  3772. const size_t new_text_len = (size_t)(new_text_end - new_text);
  3773. if (new_text_len + text_len + 1 >= BufSize)
  3774. return;
  3775. size_t upos = (size_t)pos;
  3776. if (text_len != upos)
  3777. memmove(Buf + upos + new_text_len, Buf + upos, text_len - upos);
  3778. memcpy(Buf + upos, new_text, new_text_len * sizeof(char));
  3779. Buf[text_len + new_text_len] = '\0';
  3780. BufDirty = true;
  3781. if (CursorPos >= pos)
  3782. CursorPos += (int)new_text_len;
  3783. SelectionStart = SelectionEnd = CursorPos;
  3784. }
  3785. // Edit a string of text
  3786. bool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags, void (*callback)(ImGuiTextEditCallbackData*), void* user_data)
  3787. {
  3788. ImGuiState& g = GImGui;
  3789. ImGuiWindow* window = GetCurrentWindow();
  3790. if (window->SkipItems)
  3791. return false;
  3792. const ImGuiIO& io = g.IO;
  3793. const ImGuiStyle& style = g.Style;
  3794. const ImGuiID id = window->GetID(label);
  3795. const float w = window->DC.ItemWidth.back();
  3796. const ImVec2 text_size = CalcTextSize(label);
  3797. const ImGuiAabb frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, text_size.y) + style.FramePadding*2.0f);
  3798. const ImGuiAabb bb(frame_bb.Min, frame_bb.Max + ImVec2(style.ItemInnerSpacing.x + text_size.x, 0.0f));
  3799. ItemSize(bb);
  3800. if (ClipAdvance(frame_bb))
  3801. return false;
  3802. // NB: we are only allowed to access it if we are the active widget.
  3803. ImGuiTextEditState& edit_state = g.InputTextState;
  3804. const bool is_ctrl_down = io.KeyCtrl;
  3805. const bool is_shift_down = io.KeyShift;
  3806. const bool tab_focus_requested = window->FocusItemRegister(g.ActiveId == id, (flags & ImGuiInputTextFlags_CallbackCompletion) == 0); // Using completion callback disable keyboard tabbing
  3807. //const bool align_center = (bool)(flags & ImGuiInputTextFlags_AlignCenter); // FIXME: Unsupported
  3808. const bool hovered = (g.HoveredWindow == window) && (g.HoveredId == 0) && IsMouseHoveringBox(frame_bb);
  3809. if (hovered)
  3810. g.HoveredId = id;
  3811. bool select_all = (g.ActiveId != id) && (flags & ImGuiInputTextFlags_AutoSelectAll) != 0;
  3812. if (tab_focus_requested || (hovered && io.MouseClicked[0]))
  3813. {
  3814. if (g.ActiveId != id)
  3815. {
  3816. // Start edition
  3817. // Take a copy of the initial buffer value (both in original UTF-8 format and converted to wchar)
  3818. ImFormatString(edit_state.InitialText, IM_ARRAYSIZE(edit_state.InitialText), "%s", buf);
  3819. ImTextStrFromUtf8(edit_state.Text, IM_ARRAYSIZE(edit_state.Text), buf, NULL);
  3820. edit_state.ScrollX = 0.0f;
  3821. edit_state.Width = w;
  3822. stb_textedit_initialize_state(&edit_state.StbState, true);
  3823. edit_state.CursorAnimReset();
  3824. edit_state.LastCursorPos = ImVec2(-1.f,-1.f);
  3825. if (tab_focus_requested || is_ctrl_down)
  3826. select_all = true;
  3827. }
  3828. g.ActiveId = id;
  3829. }
  3830. else if (io.MouseClicked[0])
  3831. {
  3832. // Release focus when we click outside
  3833. if (g.ActiveId == id)
  3834. {
  3835. g.ActiveId = 0;
  3836. }
  3837. }
  3838. bool value_changed = false;
  3839. bool cancel_edit = false;
  3840. bool enter_pressed = false;
  3841. static char text_tmp_utf8[IM_ARRAYSIZE(edit_state.InitialText)];
  3842. if (g.ActiveId == id)
  3843. {
  3844. // Edit in progress
  3845. edit_state.BufSize = buf_size < IM_ARRAYSIZE(edit_state.Text) ? buf_size : IM_ARRAYSIZE(edit_state.Text);
  3846. edit_state.Font = window->Font();
  3847. edit_state.FontSize = window->FontSize();
  3848. const float mx = g.IO.MousePos.x - frame_bb.Min.x - style.FramePadding.x;
  3849. const float my = window->FontSize()*0.5f; // Flatten mouse because we are doing a single-line edit
  3850. edit_state.UpdateScrollOffset();
  3851. if (select_all || (hovered && io.MouseDoubleClicked[0]))
  3852. {
  3853. edit_state.SelectAll();
  3854. edit_state.SelectedAllMouseLock = true;
  3855. }
  3856. else if (io.MouseClicked[0] && !edit_state.SelectedAllMouseLock)
  3857. {
  3858. stb_textedit_click(&edit_state, &edit_state.StbState, mx + edit_state.ScrollX, my);
  3859. edit_state.CursorAnimReset();
  3860. }
  3861. else if (io.MouseDown[0] && !edit_state.SelectedAllMouseLock)
  3862. {
  3863. stb_textedit_drag(&edit_state, &edit_state.StbState, mx + edit_state.ScrollX, my);
  3864. edit_state.CursorAnimReset();
  3865. }
  3866. if (edit_state.SelectedAllMouseLock && !io.MouseDown[0])
  3867. edit_state.SelectedAllMouseLock = false;
  3868. const int k_mask = (is_shift_down ? STB_TEXTEDIT_K_SHIFT : 0);
  3869. if (IsKeyPressedMap(ImGuiKey_LeftArrow)) { edit_state.OnKeyPressed(is_ctrl_down ? STB_TEXTEDIT_K_WORDLEFT | k_mask : STB_TEXTEDIT_K_LEFT | k_mask); }
  3870. else if (IsKeyPressedMap(ImGuiKey_RightArrow)) { edit_state.OnKeyPressed(is_ctrl_down ? STB_TEXTEDIT_K_WORDRIGHT | k_mask : STB_TEXTEDIT_K_RIGHT | k_mask); }
  3871. else if (IsKeyPressedMap(ImGuiKey_Home)) { edit_state.OnKeyPressed(is_ctrl_down ? STB_TEXTEDIT_K_TEXTSTART | k_mask : STB_TEXTEDIT_K_LINESTART | k_mask); }
  3872. else if (IsKeyPressedMap(ImGuiKey_End)) { edit_state.OnKeyPressed(is_ctrl_down ? STB_TEXTEDIT_K_TEXTEND | k_mask : STB_TEXTEDIT_K_LINEEND | k_mask); }
  3873. else if (IsKeyPressedMap(ImGuiKey_Delete)) { edit_state.OnKeyPressed(STB_TEXTEDIT_K_DELETE | k_mask); }
  3874. else if (IsKeyPressedMap(ImGuiKey_Backspace)) { edit_state.OnKeyPressed(STB_TEXTEDIT_K_BACKSPACE | k_mask); }
  3875. else if (IsKeyPressedMap(ImGuiKey_Enter)) { g.ActiveId = 0; enter_pressed = true; }
  3876. else if (IsKeyPressedMap(ImGuiKey_Escape)) { g.ActiveId = 0; cancel_edit = true; }
  3877. else if (is_ctrl_down && IsKeyPressedMap(ImGuiKey_Z)) { edit_state.OnKeyPressed(STB_TEXTEDIT_K_UNDO); }
  3878. else if (is_ctrl_down && IsKeyPressedMap(ImGuiKey_Y)) { edit_state.OnKeyPressed(STB_TEXTEDIT_K_REDO); }
  3879. else if (is_ctrl_down && IsKeyPressedMap(ImGuiKey_A)) { edit_state.SelectAll(); }
  3880. else if (is_ctrl_down && (IsKeyPressedMap(ImGuiKey_X) || IsKeyPressedMap(ImGuiKey_C)))
  3881. {
  3882. // Cut, Copy
  3883. const bool cut = IsKeyPressedMap(ImGuiKey_X);
  3884. if (cut && !edit_state.HasSelection())
  3885. edit_state.SelectAll();
  3886. if (g.IO.SetClipboardTextFn)
  3887. {
  3888. const int ib = edit_state.HasSelection() ? ImMin(edit_state.StbState.select_start, edit_state.StbState.select_end) : 0;
  3889. const int ie = edit_state.HasSelection() ? ImMax(edit_state.StbState.select_start, edit_state.StbState.select_end) : (int)ImStrlenW(edit_state.Text);
  3890. ImTextStrToUtf8(text_tmp_utf8, IM_ARRAYSIZE(text_tmp_utf8), edit_state.Text+ib, edit_state.Text+ie);
  3891. g.IO.SetClipboardTextFn(text_tmp_utf8);
  3892. }
  3893. if (cut)
  3894. stb_textedit_cut(&edit_state, &edit_state.StbState);
  3895. }
  3896. else if (is_ctrl_down && IsKeyPressedMap(ImGuiKey_V))
  3897. {
  3898. // Paste
  3899. if (g.IO.GetClipboardTextFn)
  3900. {
  3901. if (const char* clipboard = g.IO.GetClipboardTextFn())
  3902. {
  3903. // Remove new-line from pasted buffer
  3904. size_t clipboard_len = strlen(clipboard);
  3905. ImWchar* clipboard_filtered = (ImWchar*)ImGui::MemAlloc((clipboard_len+1) * sizeof(ImWchar));
  3906. int clipboard_filtered_len = 0;
  3907. for (const char* s = clipboard; *s; )
  3908. {
  3909. unsigned int c;
  3910. const int bytes_count = ImTextCharFromUtf8(&c, s, NULL);
  3911. if (bytes_count <= 0)
  3912. break;
  3913. s += bytes_count;
  3914. if (c == '\n' || c == '\r')
  3915. continue;
  3916. if (c >= 0x10000)
  3917. continue;
  3918. clipboard_filtered[clipboard_filtered_len++] = (ImWchar)c;
  3919. }
  3920. clipboard_filtered[clipboard_filtered_len] = 0;
  3921. stb_textedit_paste(&edit_state, &edit_state.StbState, clipboard_filtered, clipboard_filtered_len);
  3922. ImGui::MemFree(clipboard_filtered);
  3923. }
  3924. }
  3925. }
  3926. else if (g.IO.InputCharacters[0])
  3927. {
  3928. // Text input
  3929. for (int n = 0; n < IM_ARRAYSIZE(g.IO.InputCharacters) && g.IO.InputCharacters[n]; n++)
  3930. {
  3931. const ImWchar c = g.IO.InputCharacters[n];
  3932. if (c)
  3933. {
  3934. // Filter
  3935. if (c < 256 && !isprint((char)(c & 0xFF)) && c != ' ')
  3936. continue;
  3937. if (flags & ImGuiInputTextFlags_CharsDecimal)
  3938. if (!(c >= '0' && c <= '9') && (c != '.') && (c != '-') && (c != '+') && (c != '*') && (c != '/'))
  3939. continue;
  3940. if (flags & ImGuiInputTextFlags_CharsHexadecimal)
  3941. if (!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F'))
  3942. continue;
  3943. // Insert character!
  3944. edit_state.OnKeyPressed(c);
  3945. }
  3946. }
  3947. }
  3948. edit_state.CursorAnim += g.IO.DeltaTime;
  3949. edit_state.UpdateScrollOffset();
  3950. if (cancel_edit)
  3951. {
  3952. // Restore initial value
  3953. ImFormatString(buf, buf_size, "%s", edit_state.InitialText);
  3954. value_changed = true;
  3955. }
  3956. else
  3957. {
  3958. // Apply new value immediately - copy modified buffer back
  3959. // 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
  3960. // FIXME: We actually always render 'buf' in RenderTextScrolledClipped
  3961. // FIXME-OPT: CPU waste to do this every time the widget is active, should mark dirty state from the stb_textedit callbacks
  3962. ImTextStrToUtf8(text_tmp_utf8, IM_ARRAYSIZE(text_tmp_utf8), edit_state.Text, NULL);
  3963. // User callback
  3964. if ((flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory | ImGuiInputTextFlags_CallbackAlways)) != 0)
  3965. {
  3966. IM_ASSERT(callback != NULL);
  3967. // The reason we specify the usage semantic (Completion/History) is that Completion needs to disable keyboard TABBING at the moment.
  3968. ImGuiKey event_key = ImGuiKey_COUNT;
  3969. if ((flags & ImGuiInputTextFlags_CallbackCompletion) != 0 && IsKeyPressedMap(ImGuiKey_Tab))
  3970. event_key = ImGuiKey_Tab;
  3971. else if ((flags & ImGuiInputTextFlags_CallbackHistory) != 0 && IsKeyPressedMap(ImGuiKey_UpArrow))
  3972. event_key = ImGuiKey_UpArrow;
  3973. else if ((flags & ImGuiInputTextFlags_CallbackHistory) != 0 && IsKeyPressedMap(ImGuiKey_DownArrow))
  3974. event_key = ImGuiKey_DownArrow;
  3975. if (event_key != ImGuiKey_COUNT || (flags & ImGuiInputTextFlags_CallbackAlways) != 0)
  3976. {
  3977. ImGuiTextEditCallbackData callback_data;
  3978. callback_data.EventKey = event_key;
  3979. callback_data.Buf = text_tmp_utf8;
  3980. callback_data.BufSize = edit_state.BufSize;
  3981. callback_data.BufDirty = false;
  3982. callback_data.Flags = flags;
  3983. callback_data.UserData = user_data;
  3984. // We have to convert from position from wchar to UTF-8 positions
  3985. const int utf8_cursor_pos = callback_data.CursorPos = ImTextCountUtf8BytesFromWchar(edit_state.Text, edit_state.Text + edit_state.StbState.cursor);
  3986. const int utf8_selection_start = callback_data.SelectionStart = ImTextCountUtf8BytesFromWchar(edit_state.Text, edit_state.Text + edit_state.StbState.select_start);
  3987. const int utf8_selection_end = callback_data.SelectionEnd = ImTextCountUtf8BytesFromWchar(edit_state.Text, edit_state.Text + edit_state.StbState.select_end);
  3988. // Call user code
  3989. callback(&callback_data);
  3990. // Read back what user may have modified
  3991. IM_ASSERT(callback_data.Buf == text_tmp_utf8); // Invalid to modify those fields
  3992. IM_ASSERT(callback_data.BufSize == edit_state.BufSize);
  3993. IM_ASSERT(callback_data.Flags == flags);
  3994. if (callback_data.CursorPos != utf8_cursor_pos) edit_state.StbState.cursor = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.CursorPos);
  3995. if (callback_data.SelectionStart != utf8_selection_start) edit_state.StbState.select_start = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionStart);
  3996. if (callback_data.SelectionEnd != utf8_selection_end) edit_state.StbState.select_end = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionEnd);
  3997. if (callback_data.BufDirty)
  3998. {
  3999. ImTextStrFromUtf8(edit_state.Text, IM_ARRAYSIZE(edit_state.Text), text_tmp_utf8, NULL);
  4000. edit_state.CursorAnimReset();
  4001. }
  4002. }
  4003. }
  4004. if (strcmp(text_tmp_utf8, buf) != 0)
  4005. {
  4006. ImFormatString(buf, buf_size, "%s", text_tmp_utf8);
  4007. value_changed = true;
  4008. }
  4009. }
  4010. }
  4011. RenderFrame(frame_bb.Min, frame_bb.Max, window->Color(ImGuiCol_FrameBg), true);
  4012. const ImVec2 font_off_up = ImVec2(0.0f,window->FontSize()+1.0f); // FIXME: those offsets are part of the style or font API
  4013. const ImVec2 font_off_dn = ImVec2(0.0f,2.0f);
  4014. if (g.ActiveId == id)
  4015. {
  4016. // Draw selection
  4017. const int select_begin_idx = edit_state.StbState.select_start;
  4018. const int select_end_idx = edit_state.StbState.select_end;
  4019. if (select_begin_idx != select_end_idx)
  4020. {
  4021. const ImVec2 select_begin_pos = frame_bb.Min + style.FramePadding + edit_state.CalcDisplayOffsetFromCharIdx(ImMin(select_begin_idx,select_end_idx));
  4022. const ImVec2 select_end_pos = frame_bb.Min + style.FramePadding + edit_state.CalcDisplayOffsetFromCharIdx(ImMax(select_begin_idx,select_end_idx));
  4023. window->DrawList->AddRectFilled(select_begin_pos - font_off_up, select_end_pos + font_off_dn, window->Color(ImGuiCol_TextSelectedBg));
  4024. }
  4025. }
  4026. // FIXME: 'align_center' unsupported
  4027. ImGuiTextEditState::RenderTextScrolledClipped(window->Font(), window->FontSize(), buf, frame_bb.Min + style.FramePadding, w, (g.ActiveId == id) ? edit_state.ScrollX : 0.0f);
  4028. if (g.ActiveId == id)
  4029. {
  4030. const ImVec2 cursor_pos = frame_bb.Min + style.FramePadding + edit_state.CalcDisplayOffsetFromCharIdx(edit_state.StbState.cursor);
  4031. // Draw blinking cursor
  4032. if (g.InputTextState.CursorIsVisible())
  4033. window->DrawList->AddRect(cursor_pos - font_off_up + ImVec2(0,2), cursor_pos + font_off_dn - ImVec2(0,3), window->Color(ImGuiCol_Text));
  4034. // Notify OS of text input position
  4035. if (io.ImeSetInputScreenPosFn && ImLength(edit_state.LastCursorPos - cursor_pos) > 0.01f)
  4036. io.ImeSetInputScreenPosFn((int)cursor_pos.x - 1, (int)(cursor_pos.y - window->FontSize())); // -1 x offset so that Windows IME can cover our cursor. Bit of an extra nicety.
  4037. edit_state.LastCursorPos = cursor_pos;
  4038. }
  4039. RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);
  4040. if ((flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0)
  4041. return enter_pressed;
  4042. else
  4043. return value_changed;
  4044. }
  4045. static bool InputFloatN(const char* label, float* v, int components, int decimal_precision)
  4046. {
  4047. ImGuiState& g = GImGui;
  4048. ImGuiWindow* window = GetCurrentWindow();
  4049. if (window->SkipItems)
  4050. return false;
  4051. const ImGuiStyle& style = g.Style;
  4052. const float w_full = window->DC.ItemWidth.back();
  4053. const float w_item_one = ImMax(1.0f, (float)(int)((w_full - (style.FramePadding.x*2.0f+style.ItemInnerSpacing.x) * (components-1)) / (float)components));
  4054. 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)));
  4055. bool value_changed = false;
  4056. ImGui::PushID(label);
  4057. ImGui::PushItemWidth(w_item_one);
  4058. for (int i = 0; i < components; i++)
  4059. {
  4060. ImGui::PushID(i);
  4061. if (i + 1 == components)
  4062. {
  4063. ImGui::PopItemWidth();
  4064. ImGui::PushItemWidth(w_item_last);
  4065. }
  4066. value_changed |= ImGui::InputFloat("##v", &v[i], 0, 0, decimal_precision);
  4067. ImGui::SameLine(0, 0);
  4068. ImGui::PopID();
  4069. }
  4070. ImGui::PopItemWidth();
  4071. ImGui::PopID();
  4072. ImGui::TextUnformatted(label, FindTextDisplayEnd(label));
  4073. return value_changed;
  4074. }
  4075. bool ImGui::InputFloat2(const char* label, float v[2], int decimal_precision)
  4076. {
  4077. return InputFloatN(label, v, 2, decimal_precision);
  4078. }
  4079. bool ImGui::InputFloat3(const char* label, float v[3], int decimal_precision)
  4080. {
  4081. return InputFloatN(label, v, 3, decimal_precision);
  4082. }
  4083. bool ImGui::InputFloat4(const char* label, float v[4], int decimal_precision)
  4084. {
  4085. return InputFloatN(label, v, 4, decimal_precision);
  4086. }
  4087. static bool Combo_ArrayGetter(void* data, int idx, const char** out_text)
  4088. {
  4089. const char** items = (const char**)data;
  4090. if (out_text)
  4091. *out_text = items[idx];
  4092. return true;
  4093. }
  4094. // Combo box helper allowing to pass an array of strings.
  4095. bool ImGui::Combo(const char* label, int* current_item, const char** items, int items_count, int popup_height_items)
  4096. {
  4097. const bool value_changed = Combo(label, current_item, Combo_ArrayGetter, (void*)items, items_count, popup_height_items);
  4098. return value_changed;
  4099. }
  4100. static bool Combo_StringListGetter(void* data, int idx, const char** out_text)
  4101. {
  4102. // FIXME-OPT: we could precompute the indices to fasten this. But only 1 active combo means the waste is limited.
  4103. const char* items_separated_by_zeros = (const char*)data;
  4104. int items_count = 0;
  4105. const char* p = items_separated_by_zeros;
  4106. while (*p)
  4107. {
  4108. if (idx == items_count)
  4109. break;
  4110. p += strlen(p) + 1;
  4111. items_count++;
  4112. }
  4113. if (!*p)
  4114. return false;
  4115. if (out_text)
  4116. *out_text = p;
  4117. return true;
  4118. }
  4119. // Combo box helper allowing to pass all items in a single string.
  4120. bool ImGui::Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int popup_height_items)
  4121. {
  4122. int items_count = 0;
  4123. const char* p = items_separated_by_zeros;
  4124. while (*p)
  4125. {
  4126. p += strlen(p) + 1;
  4127. items_count++;
  4128. }
  4129. bool value_changed = Combo(label, current_item, Combo_StringListGetter, (void*)items_separated_by_zeros, items_count, popup_height_items);
  4130. return value_changed;
  4131. }
  4132. // Combo box function.
  4133. bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int popup_height_items)
  4134. {
  4135. ImGuiState& g = GImGui;
  4136. ImGuiWindow* window = GetCurrentWindow();
  4137. if (window->SkipItems)
  4138. return false;
  4139. const ImGuiStyle& style = g.Style;
  4140. const ImGuiID id = window->GetID(label);
  4141. const ImVec2 text_size = CalcTextSize(label);
  4142. const float arrow_size = (window->FontSize() + style.FramePadding.x * 2.0f);
  4143. const ImGuiAabb frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(window->DC.ItemWidth.back(), text_size.y) + style.FramePadding*2.0f);
  4144. const ImGuiAabb bb(frame_bb.Min, frame_bb.Max + ImVec2(style.ItemInnerSpacing.x + text_size.x,0));
  4145. if (ClipAdvance(frame_bb))
  4146. return false;
  4147. const bool hovered = (g.HoveredWindow == window) && (g.HoveredId == 0) && IsMouseHoveringBox(bb);
  4148. bool value_changed = false;
  4149. ItemSize(frame_bb);
  4150. RenderFrame(frame_bb.Min, frame_bb.Max, window->Color(ImGuiCol_FrameBg));
  4151. RenderFrame(ImVec2(frame_bb.Max.x-arrow_size, frame_bb.Min.y), frame_bb.Max, window->Color(hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button));
  4152. RenderCollapseTriangle(ImVec2(frame_bb.Max.x-arrow_size, frame_bb.Min.y) + style.FramePadding, true);
  4153. if (*current_item >= 0 && *current_item < items_count)
  4154. {
  4155. const char* item_text;
  4156. if (items_getter(data, *current_item, &item_text))
  4157. RenderText(frame_bb.Min + style.FramePadding, item_text, NULL, false);
  4158. }
  4159. ImGui::SameLine(0, (int)g.Style.ItemInnerSpacing.x);
  4160. ImGui::TextUnformatted(label, FindTextDisplayEnd(label));
  4161. ImGui::PushID((int)id);
  4162. bool menu_toggled = false;
  4163. if (hovered)
  4164. {
  4165. g.HoveredId = id;
  4166. if (g.IO.MouseClicked[0])
  4167. {
  4168. menu_toggled = true;
  4169. g.ActiveComboID = (g.ActiveComboID == id) ? 0 : id;
  4170. }
  4171. }
  4172. if (g.ActiveComboID == id)
  4173. {
  4174. const ImVec2 backup_pos = ImGui::GetCursorPos();
  4175. const float popup_off_x = 0.0f;//g.Style.ItemInnerSpacing.x;
  4176. const float popup_height = (text_size.y + g.Style.ItemSpacing.y) * ImMin(items_count, popup_height_items) + g.Style.WindowPadding.y;
  4177. const ImGuiAabb popup_aabb(ImVec2(frame_bb.Min.x+popup_off_x, frame_bb.Max.y), ImVec2(frame_bb.Max.x+popup_off_x, frame_bb.Max.y + popup_height));
  4178. ImGui::SetCursorPos(popup_aabb.Min - window->Pos);
  4179. const ImGuiWindowFlags flags = ImGuiWindowFlags_ComboBox | ((window->Flags & ImGuiWindowFlags_ShowBorders) ? ImGuiWindowFlags_ShowBorders : 0);
  4180. ImGui::BeginChild("#ComboBox", popup_aabb.GetSize(), false, flags);
  4181. ImGuiWindow* child_window = GetCurrentWindow();
  4182. ImGui::Spacing();
  4183. bool combo_item_active = false;
  4184. combo_item_active |= (g.ActiveId == child_window->GetID("#SCROLLY"));
  4185. // Display items
  4186. for (int item_idx = 0; item_idx < items_count; item_idx++)
  4187. {
  4188. const float item_h = child_window->FontSize();
  4189. const float spacing_up = (float)(int)(g.Style.ItemSpacing.y/2);
  4190. const float spacing_dn = g.Style.ItemSpacing.y - spacing_up;
  4191. const ImGuiAabb item_aabb(ImVec2(popup_aabb.Min.x, child_window->DC.CursorPos.y - spacing_up), ImVec2(popup_aabb.Max.x, child_window->DC.CursorPos.y + item_h + spacing_dn));
  4192. const ImGuiID item_id = child_window->GetID((void*)(intptr_t)item_idx);
  4193. bool item_hovered, item_held;
  4194. bool item_pressed = ButtonBehaviour(item_aabb, item_id, &item_hovered, &item_held, true);
  4195. bool item_selected = (item_idx == *current_item);
  4196. if (item_hovered || item_selected)
  4197. {
  4198. const ImU32 col = window->Color((item_held && item_hovered) ? ImGuiCol_HeaderActive : item_hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header);
  4199. RenderFrame(item_aabb.Min, item_aabb.Max, col, false);
  4200. }
  4201. const char* item_text;
  4202. if (!items_getter(data, item_idx, &item_text))
  4203. item_text = "*Unknown item*";
  4204. ImGui::Text("%s", item_text);
  4205. if (item_selected)
  4206. {
  4207. if (menu_toggled)
  4208. ImGui::SetScrollPosHere();
  4209. }
  4210. if (item_pressed)
  4211. {
  4212. g.ActiveId = 0;
  4213. g.ActiveComboID = 0;
  4214. value_changed = true;
  4215. *current_item = item_idx;
  4216. }
  4217. combo_item_active |= (g.ActiveId == item_id);
  4218. }
  4219. ImGui::EndChild();
  4220. ImGui::SetCursorPos(backup_pos);
  4221. if (!combo_item_active && g.ActiveId != 0)
  4222. g.ActiveComboID = 0;
  4223. }
  4224. ImGui::PopID();
  4225. return value_changed;
  4226. }
  4227. // A little colored square. Return true when clicked.
  4228. bool ImGui::ColorButton(const ImVec4& col, bool small_height, bool outline_border)
  4229. {
  4230. ImGuiState& g = GImGui;
  4231. ImGuiWindow* window = GetCurrentWindow();
  4232. if (window->SkipItems)
  4233. return false;
  4234. const ImGuiStyle& style = g.Style;
  4235. const float square_size = window->FontSize();
  4236. const ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(square_size + style.FramePadding.x*2, square_size + (small_height ? 0 : style.FramePadding.y*2)));
  4237. ItemSize(bb);
  4238. if (ClipAdvance(bb))
  4239. return false;
  4240. const bool hovered = (g.HoveredWindow == window) && (g.HoveredId == 0) && IsMouseHoveringBox(bb);
  4241. const bool pressed = hovered && g.IO.MouseClicked[0];
  4242. RenderFrame(bb.Min, bb.Max, window->Color(col), outline_border);
  4243. if (hovered)
  4244. {
  4245. int ix = (int)(col.x * 255.0f + 0.5f);
  4246. int iy = (int)(col.y * 255.0f + 0.5f);
  4247. int iz = (int)(col.z * 255.0f + 0.5f);
  4248. int iw = (int)(col.w * 255.0f + 0.5f);
  4249. 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);
  4250. }
  4251. return pressed;
  4252. }
  4253. bool ImGui::ColorEdit3(const char* label, float col[3])
  4254. {
  4255. float col4[4];
  4256. col4[0] = col[0];
  4257. col4[1] = col[1];
  4258. col4[2] = col[2];
  4259. col4[3] = 1.0f;
  4260. const bool value_changed = ImGui::ColorEdit4(label, col4, false);
  4261. col[0] = col4[0];
  4262. col[1] = col4[1];
  4263. col[2] = col4[2];
  4264. return value_changed;
  4265. }
  4266. // Edit colors components (each component in 0.0f..1.0f range
  4267. // Use CTRL-Click to input value and TAB to go to next item.
  4268. bool ImGui::ColorEdit4(const char* label, float col[4], bool alpha)
  4269. {
  4270. ImGuiState& g = GImGui;
  4271. ImGuiWindow* window = GetCurrentWindow();
  4272. if (window->SkipItems)
  4273. return false;
  4274. const ImGuiStyle& style = g.Style;
  4275. const ImGuiID id = window->GetID(label);
  4276. const float w_full = window->DC.ItemWidth.back();
  4277. const float square_sz = (window->FontSize() + style.FramePadding.x * 2.0f);
  4278. ImGuiColorEditMode edit_mode = window->DC.ColorEditMode;
  4279. if (edit_mode == ImGuiColorEditMode_UserSelect)
  4280. edit_mode = g.ColorEditModeStorage.GetInt(id, 0) % 3;
  4281. float fx = col[0];
  4282. float fy = col[1];
  4283. float fz = col[2];
  4284. float fw = col[3];
  4285. const ImVec4 col_display(fx, fy, fz, 1.0f);
  4286. if (edit_mode == ImGuiColorEditMode_HSV)
  4287. ImConvertColorRGBtoHSV(fx, fy, fz, fx, fy, fz);
  4288. int ix = (int)(fx * 255.0f + 0.5f);
  4289. int iy = (int)(fy * 255.0f + 0.5f);
  4290. int iz = (int)(fz * 255.0f + 0.5f);
  4291. int iw = (int)(fw * 255.0f + 0.5f);
  4292. int components = alpha ? 4 : 3;
  4293. bool value_changed = false;
  4294. ImGui::PushID(label);
  4295. bool hsv = (edit_mode == 1);
  4296. switch (edit_mode)
  4297. {
  4298. case ImGuiColorEditMode_RGB:
  4299. case ImGuiColorEditMode_HSV:
  4300. {
  4301. // 0: RGB 0..255
  4302. // 1: HSV 0.255 Sliders
  4303. const float w_items_all = w_full - (square_sz + style.ItemInnerSpacing.x);
  4304. 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));
  4305. 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)));
  4306. ImGui::PushItemWidth(w_item_one);
  4307. value_changed |= ImGui::SliderInt("##X", &ix, 0, 255, hsv ? "H:%3.0f" : "R:%3.0f");
  4308. ImGui::SameLine(0, 0);
  4309. value_changed |= ImGui::SliderInt("##Y", &iy, 0, 255, hsv ? "S:%3.0f" : "G:%3.0f");
  4310. ImGui::SameLine(0, 0);
  4311. if (alpha)
  4312. {
  4313. value_changed |= ImGui::SliderInt("##Z", &iz, 0, 255, hsv ? "V:%3.0f" : "B:%3.0f");
  4314. ImGui::SameLine(0, 0);
  4315. ImGui::PushItemWidth(w_item_last);
  4316. value_changed |= ImGui::SliderInt("##W", &iw, 0, 255, "A:%3.0f");
  4317. }
  4318. else
  4319. {
  4320. ImGui::PushItemWidth(w_item_last);
  4321. value_changed |= ImGui::SliderInt("##Z", &iz, 0, 255, hsv ? "V:%3.0f" : "B:%3.0f");
  4322. }
  4323. ImGui::PopItemWidth();
  4324. ImGui::PopItemWidth();
  4325. }
  4326. break;
  4327. case ImGuiColorEditMode_HEX:
  4328. {
  4329. // 2: RGB Hexadecimal
  4330. const float w_slider_all = w_full - square_sz;
  4331. char buf[64];
  4332. if (alpha)
  4333. sprintf(buf, "#%02X%02X%02X%02X", ix, iy, iz, iw);
  4334. else
  4335. sprintf(buf, "#%02X%02X%02X", ix, iy, iz);
  4336. ImGui::PushItemWidth(w_slider_all - g.Style.ItemInnerSpacing.x);
  4337. value_changed |= ImGui::InputText("##Text", buf, IM_ARRAYSIZE(buf), ImGuiInputTextFlags_CharsHexadecimal);
  4338. ImGui::PopItemWidth();
  4339. char* p = buf;
  4340. while (*p == '#' || *p == ' ' || *p == '\t')
  4341. p++;
  4342. // Treat at unsigned (%X is unsigned)
  4343. ix = iy = iz = iw = 0;
  4344. if (alpha)
  4345. sscanf(p, "%02X%02X%02X%02X", (unsigned int*)&ix, (unsigned int*)&iy, (unsigned int*)&iz, (unsigned int*)&iw);
  4346. else
  4347. sscanf(p, "%02X%02X%02X", (unsigned int*)&ix, (unsigned int*)&iy, (unsigned int*)&iz);
  4348. }
  4349. break;
  4350. }
  4351. ImGui::SameLine(0, 0);
  4352. ImGui::ColorButton(col_display);
  4353. if (window->DC.ColorEditMode == ImGuiColorEditMode_UserSelect)
  4354. {
  4355. ImGui::SameLine(0, (int)style.ItemInnerSpacing.x);
  4356. const char* button_titles[3] = { "RGB", "HSV", "HEX" };
  4357. if (ImGui::Button(button_titles[edit_mode]))
  4358. {
  4359. // Don't set local copy of 'edit_mode' right away!
  4360. g.ColorEditModeStorage.SetInt(id, (edit_mode + 1) % 3);
  4361. }
  4362. }
  4363. ImGui::SameLine();
  4364. ImGui::TextUnformatted(label, FindTextDisplayEnd(label));
  4365. // Convert back
  4366. fx = ix / 255.0f;
  4367. fy = iy / 255.0f;
  4368. fz = iz / 255.0f;
  4369. fw = iw / 255.0f;
  4370. if (edit_mode == 1)
  4371. ImConvertColorHSVtoRGB(fx, fy, fz, fx, fy, fz);
  4372. if (value_changed)
  4373. {
  4374. col[0] = fx;
  4375. col[1] = fy;
  4376. col[2] = fz;
  4377. if (alpha)
  4378. col[3] = fw;
  4379. }
  4380. ImGui::PopID();
  4381. return value_changed;
  4382. }
  4383. void ImGui::ColorEditMode(ImGuiColorEditMode mode)
  4384. {
  4385. ImGuiWindow* window = GetCurrentWindow();
  4386. window->DC.ColorEditMode = mode;
  4387. }
  4388. // Horizontal separating line.
  4389. void ImGui::Separator()
  4390. {
  4391. ImGuiWindow* window = GetCurrentWindow();
  4392. if (window->SkipItems)
  4393. return;
  4394. if (window->DC.ColumnsCount > 1)
  4395. PopClipRect();
  4396. const ImGuiAabb bb(ImVec2(window->Pos.x, window->DC.CursorPos.y), ImVec2(window->Pos.x + window->Size.x, window->DC.CursorPos.y));
  4397. ItemSize(ImVec2(0.0f, bb.GetSize().y)); // NB: we don't provide our width so that it doesn't get feed back into AutoFit
  4398. if (ClipAdvance(bb))
  4399. {
  4400. if (window->DC.ColumnsCount > 1)
  4401. PushColumnClipRect();
  4402. return;
  4403. }
  4404. window->DrawList->AddLine(bb.Min, bb.Max, window->Color(ImGuiCol_Border));
  4405. if (window->DC.ColumnsCount > 1)
  4406. PushColumnClipRect();
  4407. }
  4408. // A little vertical spacing.
  4409. void ImGui::Spacing()
  4410. {
  4411. ImGuiWindow* window = GetCurrentWindow();
  4412. if (window->SkipItems)
  4413. return;
  4414. ItemSize(ImVec2(0,0));
  4415. }
  4416. // Advance cursor given item size.
  4417. static void ItemSize(ImVec2 size, ImVec2* adjust_start_offset)
  4418. {
  4419. ImGuiState& g = GImGui;
  4420. ImGuiWindow* window = GetCurrentWindow();
  4421. if (window->SkipItems)
  4422. return;
  4423. const float line_height = ImMax(window->DC.CurrentLineHeight, size.y);
  4424. if (adjust_start_offset)
  4425. adjust_start_offset->y = adjust_start_offset->y + (line_height - size.y) * 0.5f;
  4426. // Always align ourselves on pixel boundaries
  4427. window->DC.CursorPosPrevLine = ImVec2(window->DC.CursorPos.x + size.x, window->DC.CursorPos.y);
  4428. 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));
  4429. window->SizeContentsFit = ImMax(window->SizeContentsFit, ImVec2(window->DC.CursorPosPrevLine.x, window->DC.CursorPos.y) - window->Pos + ImVec2(0.0f, window->ScrollY));
  4430. window->DC.PrevLineHeight = line_height;
  4431. window->DC.CurrentLineHeight = 0.0f;
  4432. }
  4433. static void ItemSize(const ImGuiAabb& aabb, ImVec2* adjust_start_offset)
  4434. {
  4435. ItemSize(aabb.GetSize(), adjust_start_offset);
  4436. }
  4437. void ImGui::NextColumn()
  4438. {
  4439. ImGuiState& g = GImGui;
  4440. ImGuiWindow* window = GetCurrentWindow();
  4441. if (window->SkipItems)
  4442. return;
  4443. if (window->DC.ColumnsCount > 1)
  4444. {
  4445. ImGui::PopItemWidth();
  4446. PopClipRect();
  4447. window->DC.ColumnsCellMaxY = ImMax(window->DC.ColumnsCellMaxY, window->DC.CursorPos.y);
  4448. if (++window->DC.ColumnsCurrent < window->DC.ColumnsCount)
  4449. {
  4450. window->DC.ColumnsOffsetX = ImGui::GetColumnOffset(window->DC.ColumnsCurrent) - window->DC.ColumnsStartX + g.Style.ItemSpacing.x;
  4451. }
  4452. else
  4453. {
  4454. window->DC.ColumnsCurrent = 0;
  4455. window->DC.ColumnsOffsetX = 0.0f;
  4456. window->DC.ColumnsCellMinY = window->DC.ColumnsCellMaxY;
  4457. }
  4458. window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.ColumnsStartX + window->DC.ColumnsOffsetX);
  4459. window->DC.CursorPos.y = window->DC.ColumnsCellMinY;
  4460. window->DC.CurrentLineHeight = 0.0f;
  4461. PushColumnClipRect();
  4462. ImGui::PushItemWidth(ImGui::GetColumnWidth() * 0.65f);
  4463. }
  4464. }
  4465. static bool IsClipped(const ImGuiAabb& bb)
  4466. {
  4467. ImGuiState& g = GImGui;
  4468. ImGuiWindow* window = GetCurrentWindow();
  4469. if (!bb.Overlaps(ImGuiAabb(window->ClipRectStack.back())) && !g.LogEnabled)
  4470. return true;
  4471. return false;
  4472. }
  4473. bool ImGui::IsClipped(const ImVec2& item_size)
  4474. {
  4475. ImGuiWindow* window = GetCurrentWindow();
  4476. return IsClipped(ImGuiAabb(window->DC.CursorPos, window->DC.CursorPos + item_size));
  4477. }
  4478. static bool ClipAdvance(const ImGuiAabb& bb)
  4479. {
  4480. ImGuiWindow* window = GetCurrentWindow();
  4481. window->DC.LastItemAabb = bb;
  4482. window->DC.LastItemFocused = false;
  4483. if (IsClipped(bb))
  4484. {
  4485. window->DC.LastItemHovered = false;
  4486. return true;
  4487. }
  4488. window->DC.LastItemHovered = IsMouseHoveringBox(bb); // this is a sensible default but widgets are free to override it after calling ClipAdvance
  4489. return false;
  4490. }
  4491. // Gets back to previous line and continue with horizontal layout
  4492. // column_x == 0 : follow on previous item
  4493. // columm_x != 0 : align to specified column
  4494. // spacing_w < 0 : use default spacing if column_x==0, no spacing if column_x!=0
  4495. // spacing_w >= 0 : enforce spacing
  4496. void ImGui::SameLine(int column_x, int spacing_w)
  4497. {
  4498. ImGuiState& g = GImGui;
  4499. ImGuiWindow* window = GetCurrentWindow();
  4500. if (window->SkipItems)
  4501. return;
  4502. float x, y;
  4503. if (column_x != 0)
  4504. {
  4505. if (spacing_w < 0) spacing_w = 0;
  4506. x = window->Pos.x + (float)column_x + (float)spacing_w;
  4507. y = window->DC.CursorPosPrevLine.y;
  4508. }
  4509. else
  4510. {
  4511. if (spacing_w < 0) spacing_w = (int)g.Style.ItemSpacing.x;
  4512. x = window->DC.CursorPosPrevLine.x + (float)spacing_w;
  4513. y = window->DC.CursorPosPrevLine.y;
  4514. }
  4515. window->DC.CurrentLineHeight = window->DC.PrevLineHeight;
  4516. window->DC.CursorPos = ImVec2(x, y);
  4517. }
  4518. float ImGui::GetColumnOffset(int column_index)
  4519. {
  4520. ImGuiState& g = GImGui;
  4521. ImGuiWindow* window = GetCurrentWindow();
  4522. if (column_index < 0)
  4523. column_index = window->DC.ColumnsCurrent;
  4524. const ImGuiID column_id = window->DC.ColumnsSetID + ImGuiID(column_index);
  4525. RegisterAliveId(column_id);
  4526. const float default_t = column_index / (float)window->DC.ColumnsCount;
  4527. const float t = (float)window->StateStorage.GetInt(column_id, (int)(default_t * 8192)) / 8192; // Cheaply store our floating point value inside the integer (could store an union into the map?)
  4528. const float offset = window->DC.ColumnsStartX + t * (window->Size.x - g.Style.ScrollBarWidth - window->DC.ColumnsStartX);
  4529. return offset;
  4530. }
  4531. void ImGui::SetColumnOffset(int column_index, float offset)
  4532. {
  4533. ImGuiState& g = GImGui;
  4534. ImGuiWindow* window = GetCurrentWindow();
  4535. if (column_index < 0)
  4536. column_index = window->DC.ColumnsCurrent;
  4537. const ImGuiID column_id = window->DC.ColumnsSetID + ImGuiID(column_index);
  4538. const float t = (offset - window->DC.ColumnsStartX) / (window->Size.x - g.Style.ScrollBarWidth - window->DC.ColumnsStartX);
  4539. window->StateStorage.SetInt(column_id, (int)(t*8192));
  4540. }
  4541. float ImGui::GetColumnWidth(int column_index)
  4542. {
  4543. ImGuiWindow* window = GetCurrentWindow();
  4544. if (column_index < 0)
  4545. column_index = window->DC.ColumnsCurrent;
  4546. const float w = GetColumnOffset(column_index+1) - GetColumnOffset(column_index);
  4547. return w;
  4548. }
  4549. static void PushColumnClipRect(int column_index)
  4550. {
  4551. ImGuiWindow* window = GetCurrentWindow();
  4552. if (column_index < 0)
  4553. column_index = window->DC.ColumnsCurrent;
  4554. const float x1 = window->Pos.x + ImGui::GetColumnOffset(column_index) - 1;
  4555. const float x2 = window->Pos.x + ImGui::GetColumnOffset(column_index+1) - 1;
  4556. PushClipRect(ImVec4(x1,-FLT_MAX,x2,+FLT_MAX));
  4557. }
  4558. void ImGui::Columns(int columns_count, const char* id, bool border)
  4559. {
  4560. ImGuiState& g = GImGui;
  4561. ImGuiWindow* window = GetCurrentWindow();
  4562. if (window->SkipItems)
  4563. return;
  4564. if (window->DC.ColumnsCount != 1)
  4565. {
  4566. if (window->DC.ColumnsCurrent != 0)
  4567. ItemSize(ImVec2(0,0)); // Advance to column 0
  4568. ImGui::PopItemWidth();
  4569. PopClipRect();
  4570. window->DC.ColumnsCellMaxY = ImMax(window->DC.ColumnsCellMaxY, window->DC.CursorPos.y);
  4571. window->DC.CursorPos.y = window->DC.ColumnsCellMaxY;
  4572. }
  4573. // Draw columns borders and handle resize at the time of "closing" a columns set
  4574. if (window->DC.ColumnsCount != columns_count && window->DC.ColumnsCount != 1 && window->DC.ColumnsShowBorders)
  4575. {
  4576. const float y1 = window->DC.ColumnsStartPos.y;
  4577. const float y2 = window->DC.CursorPos.y;
  4578. for (int i = 1; i < window->DC.ColumnsCount; i++)
  4579. {
  4580. float x = window->Pos.x + GetColumnOffset(i);
  4581. const ImGuiID column_id = window->DC.ColumnsSetID + ImGuiID(i);
  4582. const ImGuiAabb column_aabb(ImVec2(x-4,y1),ImVec2(x+4,y2));
  4583. if (IsClipped(column_aabb))
  4584. continue;
  4585. bool hovered, held;
  4586. ButtonBehaviour(column_aabb, column_id, &hovered, &held, true);
  4587. // Draw before resize so our items positioning are in sync with the line being drawn
  4588. const ImU32 col = window->Color(held ? ImGuiCol_ColumnActive : hovered ? ImGuiCol_ColumnHovered : ImGuiCol_Column);
  4589. const float xi = (float)(int)x;
  4590. window->DrawList->AddLine(ImVec2(xi, y1), ImVec2(xi, y2), col);
  4591. if (held)
  4592. {
  4593. x -= window->Pos.x;
  4594. x = ImClamp(x + g.IO.MouseDelta.x, ImGui::GetColumnOffset(i-1)+g.Style.ColumnsMinSpacing, ImGui::GetColumnOffset(i+1)-g.Style.ColumnsMinSpacing);
  4595. SetColumnOffset(i, x);
  4596. x += window->Pos.x;
  4597. }
  4598. }
  4599. }
  4600. // Set state for first column
  4601. window->DC.ColumnsSetID = window->GetID(id ? id : "");
  4602. window->DC.ColumnsCurrent = 0;
  4603. window->DC.ColumnsCount = columns_count;
  4604. window->DC.ColumnsShowBorders = border;
  4605. window->DC.ColumnsStartPos = window->DC.CursorPos;
  4606. window->DC.ColumnsCellMinY = window->DC.ColumnsCellMaxY = window->DC.CursorPos.y;
  4607. window->DC.ColumnsOffsetX = 0.0f;
  4608. window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.ColumnsStartX + window->DC.ColumnsOffsetX);
  4609. if (window->DC.ColumnsCount != 1)
  4610. {
  4611. PushColumnClipRect();
  4612. ImGui::PushItemWidth(ImGui::GetColumnWidth() * 0.65f);
  4613. }
  4614. }
  4615. void ImGui::TreePush(const char* str_id)
  4616. {
  4617. ImGuiState& g = GImGui;
  4618. ImGuiWindow* window = GetCurrentWindow();
  4619. window->DC.ColumnsStartX += g.Style.TreeNodeSpacing;
  4620. window->DC.CursorPos.x = window->Pos.x + window->DC.ColumnsStartX + window->DC.ColumnsOffsetX;
  4621. window->DC.TreeDepth++;
  4622. PushID(str_id ? str_id : "#TreePush");
  4623. }
  4624. void ImGui::TreePush(const void* ptr_id)
  4625. {
  4626. ImGuiState& g = GImGui;
  4627. ImGuiWindow* window = GetCurrentWindow();
  4628. window->DC.ColumnsStartX += g.Style.TreeNodeSpacing;
  4629. window->DC.CursorPos.x = window->Pos.x + window->DC.ColumnsStartX + window->DC.ColumnsOffsetX;
  4630. window->DC.TreeDepth++;
  4631. PushID(ptr_id ? ptr_id : (const void*)"#TreePush");
  4632. }
  4633. void ImGui::TreePop()
  4634. {
  4635. ImGuiState& g = GImGui;
  4636. ImGuiWindow* window = GetCurrentWindow();
  4637. window->DC.ColumnsStartX -= g.Style.TreeNodeSpacing;
  4638. window->DC.CursorPos.x = window->Pos.x + window->DC.ColumnsStartX + window->DC.ColumnsOffsetX;
  4639. window->DC.TreeDepth--;
  4640. PopID();
  4641. }
  4642. void ImGui::Value(const char* prefix, bool b)
  4643. {
  4644. ImGui::Text("%s: %s", prefix, (b ? "true" : "false"));
  4645. }
  4646. void ImGui::Value(const char* prefix, int v)
  4647. {
  4648. ImGui::Text("%s: %d", prefix, v);
  4649. }
  4650. void ImGui::Value(const char* prefix, unsigned int v)
  4651. {
  4652. ImGui::Text("%s: %d", prefix, v);
  4653. }
  4654. void ImGui::Value(const char* prefix, float v, const char* float_format)
  4655. {
  4656. if (float_format)
  4657. {
  4658. char fmt[64];
  4659. sprintf(fmt, "%%s: %s", float_format);
  4660. ImGui::Text(fmt, prefix, v);
  4661. }
  4662. else
  4663. {
  4664. ImGui::Text("%s: %.3f", prefix, v);
  4665. }
  4666. }
  4667. void ImGui::Color(const char* prefix, const ImVec4& v)
  4668. {
  4669. ImGui::Text("%s: (%.2f,%.2f,%.2f,%.2f)", prefix, v.x, v.y, v.z, v.w);
  4670. ImGui::SameLine();
  4671. ImGui::ColorButton(v, true);
  4672. }
  4673. void ImGui::Color(const char* prefix, unsigned int v)
  4674. {
  4675. ImGui::Text("%s: %08X", prefix, v);
  4676. ImGui::SameLine();
  4677. ImVec4 col;
  4678. col.x = (float)((v >> 0) & 0xFF) / 255.0f;
  4679. col.y = (float)((v >> 8) & 0xFF) / 255.0f;
  4680. col.z = (float)((v >> 16) & 0xFF) / 255.0f;
  4681. col.w = (float)((v >> 24) & 0xFF) / 255.0f;
  4682. ImGui::ColorButton(col, true);
  4683. }
  4684. //-----------------------------------------------------------------------------
  4685. // ImDrawList
  4686. //-----------------------------------------------------------------------------
  4687. void ImDrawList::Clear()
  4688. {
  4689. commands.resize(0);
  4690. vtx_buffer.resize(0);
  4691. vtx_write = NULL;
  4692. clip_rect_stack.resize(0);
  4693. }
  4694. void ImDrawList::PushClipRect(const ImVec4& clip_rect)
  4695. {
  4696. if (!commands.empty() && commands.back().vtx_count == 0)
  4697. {
  4698. // Reuse empty command because high-level clipping may have discarded the other vertices already
  4699. commands.back().clip_rect = clip_rect;
  4700. }
  4701. else
  4702. {
  4703. ImDrawCmd draw_cmd;
  4704. draw_cmd.vtx_count = 0;
  4705. draw_cmd.clip_rect = clip_rect;
  4706. commands.push_back(draw_cmd);
  4707. }
  4708. clip_rect_stack.push_back(clip_rect);
  4709. }
  4710. void ImDrawList::PopClipRect()
  4711. {
  4712. clip_rect_stack.pop_back();
  4713. const ImVec4 clip_rect = clip_rect_stack.empty() ? ImVec4(-9999.0f,-9999.0f, +9999.0f, +9999.0f) : clip_rect_stack.back();
  4714. if (!commands.empty() && commands.back().vtx_count == 0)
  4715. {
  4716. // Reuse empty command because high-level clipping may have discarded the other vertices already
  4717. commands.back().clip_rect = clip_rect;
  4718. }
  4719. else
  4720. {
  4721. ImDrawCmd draw_cmd;
  4722. draw_cmd.vtx_count = 0;
  4723. draw_cmd.clip_rect = clip_rect;
  4724. commands.push_back(draw_cmd);
  4725. }
  4726. }
  4727. void ImDrawList::ReserveVertices(unsigned int vtx_count)
  4728. {
  4729. if (vtx_count > 0)
  4730. {
  4731. ImDrawCmd& draw_cmd = commands.back();
  4732. draw_cmd.vtx_count += vtx_count;
  4733. vtx_buffer.resize(vtx_buffer.size() + vtx_count);
  4734. vtx_write = &vtx_buffer[vtx_buffer.size() - vtx_count];
  4735. }
  4736. }
  4737. void ImDrawList::AddVtx(const ImVec2& pos, ImU32 col)
  4738. {
  4739. vtx_write->pos = pos;
  4740. vtx_write->col = col;
  4741. vtx_write->uv = GImGui.FontTexUvForWhite;
  4742. vtx_write++;
  4743. }
  4744. void ImDrawList::AddVtxLine(const ImVec2& a, const ImVec2& b, ImU32 col)
  4745. {
  4746. const float offset = GImGui.IO.PixelCenterOffset;
  4747. const ImVec2 hn = (b - a) * (0.50f / ImLength(b - a)); // half normal
  4748. const ImVec2 hp0 = ImVec2(offset + hn.y, offset - hn.x); // half perpendiculars + user offset
  4749. const ImVec2 hp1 = ImVec2(offset - hn.y, offset + hn.x);
  4750. // Two triangles makes up one line. Using triangles allows us to reduce amount of draw calls.
  4751. AddVtx(a + hp0, col);
  4752. AddVtx(b + hp0, col);
  4753. AddVtx(a + hp1, col);
  4754. AddVtx(b + hp0, col);
  4755. AddVtx(b + hp1, col);
  4756. AddVtx(a + hp1, col);
  4757. }
  4758. void ImDrawList::AddLine(const ImVec2& a, const ImVec2& b, ImU32 col)
  4759. {
  4760. if ((col >> 24) == 0)
  4761. return;
  4762. ReserveVertices(6);
  4763. AddVtxLine(a, b, col);
  4764. }
  4765. void ImDrawList::AddArc(const ImVec2& center, float rad, ImU32 col, int a_min, int a_max, bool tris, const ImVec2& third_point_offset)
  4766. {
  4767. if ((col >> 24) == 0)
  4768. return;
  4769. static ImVec2 circle_vtx[12];
  4770. static bool circle_vtx_builds = false;
  4771. if (!circle_vtx_builds)
  4772. {
  4773. for (int i = 0; i < IM_ARRAYSIZE(circle_vtx); i++)
  4774. {
  4775. const float a = ((float)i / (float)IM_ARRAYSIZE(circle_vtx)) * 2*PI;
  4776. circle_vtx[i].x = cosf(a + PI);
  4777. circle_vtx[i].y = sinf(a + PI);
  4778. }
  4779. circle_vtx_builds = true;
  4780. }
  4781. if (tris)
  4782. {
  4783. ReserveVertices((unsigned int)(a_max-a_min) * 3);
  4784. for (int a = a_min; a < a_max; a++)
  4785. {
  4786. AddVtx(center + circle_vtx[a % IM_ARRAYSIZE(circle_vtx)] * rad, col);
  4787. AddVtx(center + circle_vtx[(a+1) % IM_ARRAYSIZE(circle_vtx)] * rad, col);
  4788. AddVtx(center + third_point_offset, col);
  4789. }
  4790. }
  4791. else
  4792. {
  4793. ReserveVertices((unsigned int)(a_max-a_min) * 6);
  4794. for (int a = a_min; a < a_max; a++)
  4795. AddVtxLine(center + circle_vtx[a % IM_ARRAYSIZE(circle_vtx)] * rad, center + circle_vtx[(a+1) % IM_ARRAYSIZE(circle_vtx)] * rad, col);
  4796. }
  4797. }
  4798. void ImDrawList::AddRect(const ImVec2& a, const ImVec2& b, ImU32 col, float rounding, int rounding_corners)
  4799. {
  4800. if ((col >> 24) == 0)
  4801. return;
  4802. float r = rounding;
  4803. r = ImMin(r, fabsf(b.x-a.x) * ( ((rounding_corners&(1|2))==(1|2)) || ((rounding_corners&(4|8))==(4|8)) ? 0.5f : 1.0f ));
  4804. r = ImMin(r, fabsf(b.y-a.y) * ( ((rounding_corners&(1|8))==(1|8)) || ((rounding_corners&(2|4))==(2|4)) ? 0.5f : 1.0f ));
  4805. if (r == 0.0f || rounding_corners == 0)
  4806. {
  4807. ReserveVertices(4*6);
  4808. AddVtxLine(ImVec2(a.x,a.y), ImVec2(b.x,a.y), col);
  4809. AddVtxLine(ImVec2(b.x,a.y), ImVec2(b.x,b.y), col);
  4810. AddVtxLine(ImVec2(b.x,b.y), ImVec2(a.x,b.y), col);
  4811. AddVtxLine(ImVec2(a.x,b.y), ImVec2(a.x,a.y), col);
  4812. }
  4813. else
  4814. {
  4815. ReserveVertices(4*6);
  4816. AddVtxLine(ImVec2(a.x + ((rounding_corners & 1)?r:0), a.y), ImVec2(b.x - ((rounding_corners & 2)?r:0), a.y), col);
  4817. AddVtxLine(ImVec2(b.x, a.y + ((rounding_corners & 2)?r:0)), ImVec2(b.x, b.y - ((rounding_corners & 4)?r:0)), col);
  4818. AddVtxLine(ImVec2(b.x - ((rounding_corners & 4)?r:0), b.y), ImVec2(a.x + ((rounding_corners & 8)?r:0), b.y), col);
  4819. AddVtxLine(ImVec2(a.x, b.y - ((rounding_corners & 8)?r:0)), ImVec2(a.x, a.y + ((rounding_corners & 1)?r:0)), col);
  4820. if (rounding_corners & 1) AddArc(ImVec2(a.x+r,a.y+r), r, col, 0, 3);
  4821. if (rounding_corners & 2) AddArc(ImVec2(b.x-r,a.y+r), r, col, 3, 6);
  4822. if (rounding_corners & 4) AddArc(ImVec2(b.x-r,b.y-r), r, col, 6, 9);
  4823. if (rounding_corners & 8) AddArc(ImVec2(a.x+r,b.y-r), r, col, 9, 12);
  4824. }
  4825. }
  4826. void ImDrawList::AddRectFilled(const ImVec2& a, const ImVec2& b, ImU32 col, float rounding, int rounding_corners)
  4827. {
  4828. if ((col >> 24) == 0)
  4829. return;
  4830. float r = rounding;
  4831. r = ImMin(r, fabsf(b.x-a.x) * ( ((rounding_corners&(1|2))==(1|2)) || ((rounding_corners&(4|8))==(4|8)) ? 0.5f : 1.0f ));
  4832. r = ImMin(r, fabsf(b.y-a.y) * ( ((rounding_corners&(1|8))==(1|8)) || ((rounding_corners&(2|4))==(2|4)) ? 0.5f : 1.0f ));
  4833. if (r == 0.0f || rounding_corners == 0)
  4834. {
  4835. // Use triangle so we can merge more draw calls together (at the cost of extra vertices)
  4836. ReserveVertices(6);
  4837. AddVtx(ImVec2(a.x,a.y), col);
  4838. AddVtx(ImVec2(b.x,a.y), col);
  4839. AddVtx(ImVec2(b.x,b.y), col);
  4840. AddVtx(ImVec2(a.x,a.y), col);
  4841. AddVtx(ImVec2(b.x,b.y), col);
  4842. AddVtx(ImVec2(a.x,b.y), col);
  4843. }
  4844. else
  4845. {
  4846. ReserveVertices(6+6*2);
  4847. AddVtx(ImVec2(a.x+r,a.y), col);
  4848. AddVtx(ImVec2(b.x-r,a.y), col);
  4849. AddVtx(ImVec2(b.x-r,b.y), col);
  4850. AddVtx(ImVec2(a.x+r,a.y), col);
  4851. AddVtx(ImVec2(b.x-r,b.y), col);
  4852. AddVtx(ImVec2(a.x+r,b.y), col);
  4853. float top_y = (rounding_corners & 1) ? a.y+r : a.y;
  4854. float bot_y = (rounding_corners & 8) ? b.y-r : b.y;
  4855. AddVtx(ImVec2(a.x,top_y), col);
  4856. AddVtx(ImVec2(a.x+r,top_y), col);
  4857. AddVtx(ImVec2(a.x+r,bot_y), col);
  4858. AddVtx(ImVec2(a.x,top_y), col);
  4859. AddVtx(ImVec2(a.x+r,bot_y), col);
  4860. AddVtx(ImVec2(a.x,bot_y), col);
  4861. top_y = (rounding_corners & 2) ? a.y+r : a.y;
  4862. bot_y = (rounding_corners & 4) ? b.y-r : b.y;
  4863. AddVtx(ImVec2(b.x-r,top_y), col);
  4864. AddVtx(ImVec2(b.x,top_y), col);
  4865. AddVtx(ImVec2(b.x,bot_y), col);
  4866. AddVtx(ImVec2(b.x-r,top_y), col);
  4867. AddVtx(ImVec2(b.x,bot_y), col);
  4868. AddVtx(ImVec2(b.x-r,bot_y), col);
  4869. if (rounding_corners & 1) AddArc(ImVec2(a.x+r,a.y+r), r, col, 0, 3, true);
  4870. if (rounding_corners & 2) AddArc(ImVec2(b.x-r,a.y+r), r, col, 3, 6, true);
  4871. if (rounding_corners & 4) AddArc(ImVec2(b.x-r,b.y-r), r, col, 6, 9, true);
  4872. if (rounding_corners & 8) AddArc(ImVec2(a.x+r,b.y-r), r, col, 9, 12,true);
  4873. }
  4874. }
  4875. void ImDrawList::AddTriangleFilled(const ImVec2& a, const ImVec2& b, const ImVec2& c, ImU32 col)
  4876. {
  4877. if ((col >> 24) == 0)
  4878. return;
  4879. const ImVec2 offset(GImGui.IO.PixelCenterOffset,GImGui.IO.PixelCenterOffset);
  4880. ReserveVertices(3);
  4881. AddVtx(a + offset, col);
  4882. AddVtx(b + offset, col);
  4883. AddVtx(c + offset, col);
  4884. }
  4885. void ImDrawList::AddCircle(const ImVec2& centre, float radius, ImU32 col, int num_segments)
  4886. {
  4887. if ((col >> 24) == 0)
  4888. return;
  4889. const ImVec2 offset(GImGui.IO.PixelCenterOffset,GImGui.IO.PixelCenterOffset);
  4890. ReserveVertices((unsigned int)num_segments*6);
  4891. const float a_step = 2*PI/(float)num_segments;
  4892. float a0 = 0.0f;
  4893. for (int i = 0; i < num_segments; i++)
  4894. {
  4895. const float a1 = (i + 1) == num_segments ? 0.0f : a0 + a_step;
  4896. AddVtxLine(centre + offset + ImVec2(cosf(a0), sinf(a0))*radius, centre + ImVec2(cosf(a1), sinf(a1))*radius, col);
  4897. a0 = a1;
  4898. }
  4899. }
  4900. void ImDrawList::AddCircleFilled(const ImVec2& centre, float radius, ImU32 col, int num_segments)
  4901. {
  4902. if ((col >> 24) == 0)
  4903. return;
  4904. const ImVec2 offset(GImGui.IO.PixelCenterOffset,GImGui.IO.PixelCenterOffset);
  4905. ReserveVertices((unsigned int)num_segments*3);
  4906. const float a_step = 2*PI/(float)num_segments;
  4907. float a0 = 0.0f;
  4908. for (int i = 0; i < num_segments; i++)
  4909. {
  4910. const float a1 = (i + 1) == num_segments ? 0.0f : a0 + a_step;
  4911. AddVtx(centre + offset + ImVec2(cosf(a0), sinf(a0))*radius, col);
  4912. AddVtx(centre + offset + ImVec2(cosf(a1), sinf(a1))*radius, col);
  4913. AddVtx(centre + offset, col);
  4914. a0 = a1;
  4915. }
  4916. }
  4917. void ImDrawList::AddText(ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end, float wrap_width)
  4918. {
  4919. if ((col >> 24) == 0)
  4920. return;
  4921. if (text_end == NULL)
  4922. text_end = text_begin + strlen(text_begin); // FIXME-OPT
  4923. // reserve vertices for worse case
  4924. const unsigned int char_count = (unsigned int)(text_end - text_begin);
  4925. const unsigned int vtx_count_max = char_count * 6;
  4926. const size_t vtx_begin = vtx_buffer.size();
  4927. ReserveVertices(vtx_count_max);
  4928. font->RenderText(font_size, pos, col, clip_rect_stack.back(), text_begin, text_end, vtx_write, wrap_width);
  4929. // give back unused vertices
  4930. vtx_buffer.resize((size_t)(vtx_write - &vtx_buffer.front()));
  4931. const size_t vtx_count = vtx_buffer.size() - vtx_begin;
  4932. commands.back().vtx_count -= (unsigned int)(vtx_count_max - vtx_count);
  4933. vtx_write -= (vtx_count_max - vtx_count);
  4934. }
  4935. //-----------------------------------------------------------------------------
  4936. // ImBitmapFont
  4937. //-----------------------------------------------------------------------------
  4938. ImFont::ImFont()
  4939. {
  4940. Scale = 1.0f;
  4941. DisplayOffset = ImVec2(0.0f,0.0f);
  4942. TexUvForWhite = ImVec2(0.0f,0.0f);
  4943. FallbackChar = (ImWchar)'?';
  4944. Data = NULL;
  4945. DataSize = 0;
  4946. DataOwned = false;
  4947. Info = NULL;
  4948. Common = NULL;
  4949. Glyphs = NULL;
  4950. GlyphsCount = 0;
  4951. Kerning = NULL;
  4952. KerningCount = 0;
  4953. }
  4954. void ImFont::Clear()
  4955. {
  4956. if (Data && DataOwned)
  4957. ImGui::MemFree(Data);
  4958. Data = NULL;
  4959. DataOwned = false;
  4960. Info = NULL;
  4961. Common = NULL;
  4962. Glyphs = NULL;
  4963. GlyphsCount = 0;
  4964. Filenames.clear();
  4965. IndexLookup.clear();
  4966. }
  4967. bool ImFont::LoadFromFile(const char* filename)
  4968. {
  4969. IM_ASSERT(!IsLoaded()); // Call Clear()
  4970. // Load file
  4971. FILE* f;
  4972. if ((f = fopen(filename, "rb")) == NULL)
  4973. return false;
  4974. if (fseek(f, 0, SEEK_END))
  4975. {
  4976. fclose(f);
  4977. return false;
  4978. }
  4979. const long f_size = ftell(f);
  4980. if (f_size == -1)
  4981. {
  4982. fclose(f);
  4983. return false;
  4984. }
  4985. DataSize = (size_t)f_size;
  4986. if (fseek(f, 0, SEEK_SET))
  4987. {
  4988. fclose(f);
  4989. return false;
  4990. }
  4991. if ((Data = (unsigned char*)ImGui::MemAlloc(DataSize)) == NULL)
  4992. {
  4993. fclose(f);
  4994. return false;
  4995. }
  4996. if (fread(Data, 1, DataSize, f) != DataSize)
  4997. {
  4998. fclose(f);
  4999. ImGui::MemFree(Data);
  5000. return false;
  5001. }
  5002. fclose(f);
  5003. DataOwned = true;
  5004. return LoadFromMemory(Data, DataSize);
  5005. }
  5006. bool ImFont::LoadFromMemory(const void* data, size_t data_size)
  5007. {
  5008. IM_ASSERT(!IsLoaded()); // Call Clear()
  5009. Data = (unsigned char*)data;
  5010. DataSize = data_size;
  5011. // Parse data
  5012. if (DataSize < 4 || Data[0] != 'B' || Data[1] != 'M' || Data[2] != 'F' || Data[3] != 0x03)
  5013. return false;
  5014. for (const unsigned char* p = Data+4; p < Data + DataSize; )
  5015. {
  5016. const unsigned char block_type = *(unsigned char*)p;
  5017. p += sizeof(unsigned char);
  5018. ImU32 block_size; // use memcpy to read 4-byte because they may be unaligned. This seems to break when compiling for Emscripten.
  5019. memcpy(&block_size, p, sizeof(ImU32));
  5020. p += sizeof(ImU32);
  5021. switch (block_type)
  5022. {
  5023. case 1:
  5024. IM_ASSERT(Info == NULL);
  5025. Info = (FntInfo*)p;
  5026. break;
  5027. case 2:
  5028. IM_ASSERT(Common == NULL);
  5029. Common = (FntCommon*)p;
  5030. break;
  5031. case 3:
  5032. for (const unsigned char* s = p; s < p+block_size && s < Data+DataSize; s = s + strlen((const char*)s) + 1)
  5033. Filenames.push_back((const char*)s);
  5034. break;
  5035. case 4:
  5036. IM_ASSERT(Glyphs == NULL && GlyphsCount == 0);
  5037. Glyphs = (FntGlyph*)p;
  5038. GlyphsCount = block_size / sizeof(FntGlyph);
  5039. break;
  5040. case 5:
  5041. IM_ASSERT(Kerning == NULL && KerningCount == 0);
  5042. Kerning = (FntKerning*)p;
  5043. KerningCount = block_size / sizeof(FntKerning);
  5044. break;
  5045. default:
  5046. break;
  5047. }
  5048. p += block_size;
  5049. }
  5050. BuildLookupTable();
  5051. return true;
  5052. }
  5053. void ImFont::BuildLookupTable()
  5054. {
  5055. ImU32 max_c = 0;
  5056. for (size_t i = 0; i != GlyphsCount; i++)
  5057. if (max_c < Glyphs[i].Id)
  5058. max_c = Glyphs[i].Id;
  5059. IndexLookup.clear();
  5060. IndexLookup.resize(max_c + 1);
  5061. for (size_t i = 0; i < IndexLookup.size(); i++)
  5062. IndexLookup[i] = -1;
  5063. for (size_t i = 0; i < GlyphsCount; i++)
  5064. IndexLookup[Glyphs[i].Id] = (int)i;
  5065. }
  5066. const ImFont::FntGlyph* ImFont::FindGlyph(unsigned short c) const
  5067. {
  5068. if (c < (int)IndexLookup.size())
  5069. {
  5070. const int i = IndexLookup[c];
  5071. if (i >= 0 && i < (int)GlyphsCount)
  5072. return &Glyphs[i];
  5073. }
  5074. return FallbackGlyph;
  5075. }
  5076. // Convert UTF-8 to 32-bits character, process single character input.
  5077. // Based on stb_from_utf8() from github.com/nothings/stb/
  5078. static int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end)
  5079. {
  5080. if (*in_text != 0)
  5081. {
  5082. unsigned int c = (unsigned int)-1;
  5083. const unsigned char* str = (const unsigned char*)in_text;
  5084. if (!(*str & 0x80))
  5085. {
  5086. c = (unsigned int)(*str++);
  5087. *out_char = c;
  5088. return 1;
  5089. }
  5090. if ((*str & 0xe0) == 0xc0)
  5091. {
  5092. if (in_text_end && in_text_end - (const char*)str < 2) return -1;
  5093. if (*str < 0xc2) return -1;
  5094. c = (unsigned int)((*str++ & 0x1f) << 6);
  5095. if ((*str & 0xc0) != 0x80) return -1;
  5096. c += (*str++ & 0x3f);
  5097. *out_char = c;
  5098. return 2;
  5099. }
  5100. if ((*str & 0xf0) == 0xe0)
  5101. {
  5102. if (in_text_end && in_text_end - (const char*)str < 3) return -1;
  5103. if (*str == 0xe0 && (str[1] < 0xa0 || str[1] > 0xbf)) return -1;
  5104. if (*str == 0xed && str[1] > 0x9f) return -1; // str[1] < 0x80 is checked below
  5105. c = (unsigned int)((*str++ & 0x0f) << 12);
  5106. if ((*str & 0xc0) != 0x80) return -1;
  5107. c += (unsigned int)((*str++ & 0x3f) << 6);
  5108. if ((*str & 0xc0) != 0x80) return -1;
  5109. c += (*str++ & 0x3f);
  5110. *out_char = c;
  5111. return 3;
  5112. }
  5113. if ((*str & 0xf8) == 0xf0)
  5114. {
  5115. if (in_text_end && in_text_end - (const char*)str < 4) return -1;
  5116. if (*str > 0xf4) return -1;
  5117. if (*str == 0xf0 && (str[1] < 0x90 || str[1] > 0xbf)) return -1;
  5118. if (*str == 0xf4 && str[1] > 0x8f) return -1; // str[1] < 0x80 is checked below
  5119. c = (unsigned int)((*str++ & 0x07) << 18);
  5120. if ((*str & 0xc0) != 0x80) return -1;
  5121. c += (unsigned int)((*str++ & 0x3f) << 12);
  5122. if ((*str & 0xc0) != 0x80) return -1;
  5123. c += (unsigned int)((*str++ & 0x3f) << 6);
  5124. if ((*str & 0xc0) != 0x80) return -1;
  5125. c += (*str++ & 0x3f);
  5126. // utf-8 encodings of values used in surrogate pairs are invalid
  5127. if ((c & 0xFFFFF800) == 0xD800) return -1;
  5128. *out_char = c;
  5129. return 4;
  5130. }
  5131. }
  5132. *out_char = 0;
  5133. return 0;
  5134. }
  5135. static ptrdiff_t ImTextStrFromUtf8(ImWchar* buf, size_t buf_size, const char* in_text, const char* in_text_end)
  5136. {
  5137. ImWchar* buf_out = buf;
  5138. ImWchar* buf_end = buf + buf_size;
  5139. while (buf_out < buf_end-1 && (!in_text_end || in_text < in_text_end) && *in_text)
  5140. {
  5141. unsigned int c;
  5142. in_text += ImTextCharFromUtf8(&c, in_text, in_text_end);
  5143. if (c < 0x10000) // FIXME: Losing characters that don't fit in 2 bytes
  5144. *buf_out++ = (ImWchar)c;
  5145. }
  5146. *buf_out = 0;
  5147. return buf_out - buf;
  5148. }
  5149. static int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end)
  5150. {
  5151. int char_count = 0;
  5152. while ((!in_text_end || in_text < in_text_end) && *in_text)
  5153. {
  5154. unsigned int c;
  5155. in_text += ImTextCharFromUtf8(&c, in_text, in_text_end);
  5156. if (c < 0x10000)
  5157. char_count++;
  5158. }
  5159. return char_count;
  5160. }
  5161. // Based on stb_to_utf8() from github.com/nothings/stb/
  5162. static int ImTextCharToUtf8(char* buf, size_t buf_size, unsigned int c)
  5163. {
  5164. if (c)
  5165. {
  5166. size_t i = 0;
  5167. size_t n = buf_size;
  5168. if (c < 0x80)
  5169. {
  5170. if (i+1 > n) return 0;
  5171. buf[i++] = (char)c;
  5172. return 1;
  5173. }
  5174. else if (c < 0x800)
  5175. {
  5176. if (i+2 > n) return 0;
  5177. buf[i++] = (char)(0xc0 + (c >> 6));
  5178. buf[i++] = (char)(0x80 + (c & 0x3f));
  5179. return 2;
  5180. }
  5181. else if (c >= 0xdc00 && c < 0xe000)
  5182. {
  5183. return 0;
  5184. }
  5185. else if (c >= 0xd800 && c < 0xdc00)
  5186. {
  5187. if (i+4 > n) return 0;
  5188. buf[i++] = (char)(0xf0 + (c >> 18));
  5189. buf[i++] = (char)(0x80 + ((c >> 12) & 0x3f));
  5190. buf[i++] = (char)(0x80 + ((c >> 6) & 0x3f));
  5191. buf[i++] = (char)(0x80 + ((c ) & 0x3f));
  5192. return 4;
  5193. }
  5194. //else if (c < 0x10000)
  5195. {
  5196. if (i+3 > n) return 0;
  5197. buf[i++] = (char)(0xe0 + (c >> 12));
  5198. buf[i++] = (char)(0x80 + ((c>> 6) & 0x3f));
  5199. buf[i++] = (char)(0x80 + ((c ) & 0x3f));
  5200. return 3;
  5201. }
  5202. }
  5203. return 0;
  5204. }
  5205. static ptrdiff_t ImTextStrToUtf8(char* buf, size_t buf_size, const ImWchar* in_text, const ImWchar* in_text_end)
  5206. {
  5207. char* buf_out = buf;
  5208. const char* buf_end = buf + buf_size;
  5209. while (buf_out < buf_end-1 && (!in_text_end || in_text < in_text_end) && *in_text)
  5210. {
  5211. buf_out += ImTextCharToUtf8(buf_out, (uintptr_t)(buf_end-buf_out-1), (unsigned int)*in_text);
  5212. in_text++;
  5213. }
  5214. *buf_out = 0;
  5215. return buf_out - buf;
  5216. }
  5217. static int ImTextCountUtf8BytesFromWchar(const ImWchar* in_text, const ImWchar* in_text_end)
  5218. {
  5219. int bytes_count = 0;
  5220. while ((!in_text_end || in_text < in_text_end) && *in_text)
  5221. {
  5222. char dummy[5]; // FIXME-OPT
  5223. bytes_count += ImTextCharToUtf8(dummy, 5, (unsigned int)*in_text);
  5224. in_text++;
  5225. }
  5226. return bytes_count;
  5227. }
  5228. const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const
  5229. {
  5230. // Simple word-wrapping for English, not full-featured. Please submit failing cases!
  5231. // 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.)
  5232. // For references, possible wrap point marked with ^
  5233. // "aaa bbb, ccc,ddd. eee fff. ggg!"
  5234. // ^ ^ ^ ^ ^__ ^ ^
  5235. // List of hardcoded separators: .,;!?'"
  5236. // Skip extra blanks after a line returns (that includes not counting them in width computation)
  5237. // e.g. "Hello world"
  5238. // -->
  5239. // "Hello"
  5240. // "world"
  5241. // Cut words that cannot possibly fit within one line.
  5242. // e.g.: "The tropical fish" with ~5 characters worth of width
  5243. // -->
  5244. // "The tr"
  5245. // "opical"
  5246. // "fish"
  5247. float line_width = 0.0f;
  5248. float word_width = 0.0f;
  5249. float blank_width = 0.0f;
  5250. const char* word_end = text;
  5251. const char* prev_word_end = NULL;
  5252. bool inside_word = true;
  5253. const char* s = text;
  5254. while (s < text_end)
  5255. {
  5256. unsigned int c;
  5257. const int bytes_count = ImTextCharFromUtf8(&c, s, text_end);
  5258. const char* next_s = s + (bytes_count > 0 ? bytes_count : 1);
  5259. if (c == '\n')
  5260. {
  5261. line_width = word_width = blank_width = 0.0f;
  5262. inside_word = true;
  5263. s = next_s;
  5264. continue;
  5265. }
  5266. float char_width = 0.0f;
  5267. if (c == '\t')
  5268. {
  5269. if (const FntGlyph* glyph = FindGlyph((unsigned short)' '))
  5270. char_width = (glyph->XAdvance + Info->SpacingHoriz) * 4 * scale;
  5271. }
  5272. else
  5273. {
  5274. if (const FntGlyph* glyph = FindGlyph((unsigned short)c))
  5275. char_width = (glyph->XAdvance + Info->SpacingHoriz) * scale;
  5276. }
  5277. if (c == ' ' || c == '\t')
  5278. {
  5279. if (inside_word)
  5280. {
  5281. line_width += blank_width;
  5282. blank_width = 0.0f;
  5283. }
  5284. blank_width += char_width;
  5285. inside_word = false;
  5286. }
  5287. else
  5288. {
  5289. word_width += char_width;
  5290. if (inside_word)
  5291. {
  5292. word_end = next_s;
  5293. }
  5294. else
  5295. {
  5296. prev_word_end = word_end;
  5297. line_width += word_width + blank_width;
  5298. word_width = blank_width = 0.0f;
  5299. }
  5300. // Allow wrapping after punctuation.
  5301. inside_word = !(c == '.' || c == ',' || c == ';' || c == '!' || c == '?' || c == '\"');
  5302. }
  5303. // We ignore blank width at the end of the line (they can be skipped)
  5304. if (line_width + word_width >= wrap_width)
  5305. {
  5306. // Words that cannot possibly fit within an entire line will be cut anywhere.
  5307. if (word_width < wrap_width)
  5308. s = prev_word_end ? prev_word_end : word_end;
  5309. break;
  5310. }
  5311. s = next_s;
  5312. }
  5313. return s;
  5314. }
  5315. ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** remaining) const
  5316. {
  5317. if (!text_end)
  5318. text_end = text_begin + strlen(text_begin); // FIXME-OPT
  5319. const float scale = size / (float)Info->FontSize;
  5320. const float line_height = (float)Info->FontSize * scale;
  5321. ImVec2 text_size = ImVec2(0,0);
  5322. float line_width = 0.0f;
  5323. const bool word_wrap_enabled = (wrap_width > 0.0f);
  5324. const char* word_wrap_eol = NULL;
  5325. const char* s = text_begin;
  5326. while (s < text_end)
  5327. {
  5328. if (word_wrap_enabled)
  5329. {
  5330. // 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.
  5331. if (!word_wrap_eol)
  5332. {
  5333. word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - line_width);
  5334. if (word_wrap_eol == s) // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity.
  5335. 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
  5336. }
  5337. if (s >= word_wrap_eol)
  5338. {
  5339. if (text_size.x < line_width)
  5340. text_size.x = line_width;
  5341. text_size.y += line_height;
  5342. line_width = 0.0f;
  5343. word_wrap_eol = NULL;
  5344. // Wrapping skips upcoming blanks
  5345. while (s < text_end)
  5346. {
  5347. const char c = *s;
  5348. if (c == ' ' || c == '\t') { s++; } else if (c == '\n') { s++; break; } else { break; }
  5349. }
  5350. continue;
  5351. }
  5352. }
  5353. // Decode and advance source (handle unlikely UTF-8 decoding failure by skipping to the next byte)
  5354. unsigned int c;
  5355. const int bytes_count = ImTextCharFromUtf8(&c, s, text_end);
  5356. s += bytes_count > 0 ? bytes_count : 1;
  5357. if (c == '\n')
  5358. {
  5359. if (text_size.x < line_width)
  5360. text_size.x = line_width;
  5361. text_size.y += line_height;
  5362. line_width = 0.0f;
  5363. continue;
  5364. }
  5365. float char_width = 0.0f;
  5366. if (c == '\t')
  5367. {
  5368. // FIXME: Better TAB handling
  5369. if (const FntGlyph* glyph = FindGlyph((unsigned short)' '))
  5370. char_width = (glyph->XAdvance + Info->SpacingHoriz) * 4 * scale;
  5371. }
  5372. else if (const FntGlyph* glyph = FindGlyph((unsigned short)c))
  5373. {
  5374. char_width = (glyph->XAdvance + Info->SpacingHoriz) * scale;
  5375. }
  5376. if (line_width + char_width >= max_width)
  5377. break;
  5378. line_width += char_width;
  5379. }
  5380. if (line_width > 0 || text_size.y == 0.0f)
  5381. {
  5382. if (text_size.x < line_width)
  5383. text_size.x = line_width;
  5384. text_size.y += line_height;
  5385. }
  5386. if (remaining)
  5387. *remaining = s;
  5388. return text_size;
  5389. }
  5390. ImVec2 ImFont::CalcTextSizeW(float size, float max_width, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining) const
  5391. {
  5392. if (!text_end)
  5393. text_end = text_begin + ImStrlenW(text_begin);
  5394. const float scale = size / (float)Info->FontSize;
  5395. const float line_height = (float)Info->FontSize * scale;
  5396. ImVec2 text_size = ImVec2(0,0);
  5397. float line_width = 0.0f;
  5398. const ImWchar* s = text_begin;
  5399. while (s < text_end)
  5400. {
  5401. const unsigned int c = (unsigned int)(*s++);
  5402. if (c == '\n')
  5403. {
  5404. if (text_size.x < line_width)
  5405. text_size.x = line_width;
  5406. text_size.y += line_height;
  5407. line_width = 0.0f;
  5408. continue;
  5409. }
  5410. float char_width = 0.0f;
  5411. if (c == '\t')
  5412. {
  5413. // FIXME: Better TAB handling
  5414. if (const FntGlyph* glyph = FindGlyph((unsigned short)' '))
  5415. char_width = (glyph->XAdvance + Info->SpacingHoriz) * 4 * scale;
  5416. }
  5417. else
  5418. {
  5419. if (const FntGlyph* glyph = FindGlyph((unsigned short)c))
  5420. char_width = (glyph->XAdvance + Info->SpacingHoriz) * scale;
  5421. }
  5422. if (line_width + char_width >= max_width)
  5423. break;
  5424. line_width += char_width;
  5425. }
  5426. if (line_width > 0 || text_size.y == 0.0f)
  5427. {
  5428. if (text_size.x < line_width)
  5429. text_size.x = line_width;
  5430. text_size.y += line_height;
  5431. }
  5432. if (remaining)
  5433. *remaining = s;
  5434. return text_size;
  5435. }
  5436. void ImFont::RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect_ref, const char* text_begin, const char* text_end, ImDrawVert*& out_vertices, float wrap_width) const
  5437. {
  5438. if (!text_end)
  5439. text_end = text_begin + strlen(text_begin);
  5440. const float line_height = (float)Info->FontSize;
  5441. const float scale = size / (float)Info->FontSize;
  5442. const float tex_scale_x = 1.0f / (float)Common->ScaleW;
  5443. const float tex_scale_y = 1.0f / (float)(Common->ScaleH);
  5444. const float outline = (float)Info->Outline;
  5445. // Align to be pixel perfect
  5446. pos.x = (float)(int)pos.x + DisplayOffset.x;
  5447. pos.y = (float)(int)pos.y + DisplayOffset.y;
  5448. const bool word_wrap_enabled = (wrap_width > 0.0f);
  5449. const char* word_wrap_eol = NULL;
  5450. const ImVec4 clip_rect = clip_rect_ref;
  5451. float x = pos.x;
  5452. float y = pos.y;
  5453. const char* s = text_begin;
  5454. while (s < text_end)
  5455. {
  5456. if (word_wrap_enabled)
  5457. {
  5458. // 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.
  5459. if (!word_wrap_eol)
  5460. {
  5461. word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - (x - pos.x));
  5462. if (word_wrap_eol == s) // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity.
  5463. 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
  5464. }
  5465. if (s >= word_wrap_eol)
  5466. {
  5467. x = pos.x;
  5468. y += line_height * scale;
  5469. word_wrap_eol = NULL;
  5470. // Wrapping skips upcoming blanks
  5471. while (s < text_end)
  5472. {
  5473. const char c = *s;
  5474. if (c == ' ' || c == '\t') { s++; } else if (c == '\n') { s++; break; } else { break; }
  5475. }
  5476. continue;
  5477. }
  5478. }
  5479. // Decode and advance source (handle unlikely UTF-8 decoding failure by skipping to the next byte)
  5480. unsigned int c;
  5481. const int bytes_count = ImTextCharFromUtf8(&c, s, text_end);
  5482. s += bytes_count > 0 ? bytes_count : 1;
  5483. if (c == '\n')
  5484. {
  5485. x = pos.x;
  5486. y += line_height * scale;
  5487. continue;
  5488. }
  5489. float char_width = 0.0f;
  5490. if (c == '\t')
  5491. {
  5492. // FIXME: Better TAB handling
  5493. if (const FntGlyph* glyph = FindGlyph((unsigned short)' '))
  5494. char_width += (glyph->XAdvance + Info->SpacingHoriz) * 4 * scale;
  5495. }
  5496. else if (const FntGlyph* glyph = FindGlyph((unsigned short)c))
  5497. {
  5498. char_width = (glyph->XAdvance + Info->SpacingHoriz) * scale;
  5499. if (c != ' ')
  5500. {
  5501. // Clipping on Y is more likely
  5502. const float y1 = (float)(y + (glyph->YOffset + outline*2) * scale);
  5503. const float y2 = (float)(y1 + glyph->Height * scale);
  5504. if (y1 <= clip_rect.w && y2 >= clip_rect.y)
  5505. {
  5506. const float x1 = (float)(x + (glyph->XOffset + outline) * scale);
  5507. const float x2 = (float)(x1 + glyph->Width * scale);
  5508. if (x1 <= clip_rect.z && x2 >= clip_rect.x)
  5509. {
  5510. // Render a character
  5511. const float s1 = (glyph->X) * tex_scale_x;
  5512. const float t1 = (glyph->Y) * tex_scale_y;
  5513. const float s2 = (glyph->X + glyph->Width) * tex_scale_x;
  5514. const float t2 = (glyph->Y + glyph->Height) * tex_scale_y;
  5515. out_vertices[0].pos = ImVec2(x1, y1);
  5516. out_vertices[0].uv = ImVec2(s1, t1);
  5517. out_vertices[0].col = col;
  5518. out_vertices[1].pos = ImVec2(x2, y1);
  5519. out_vertices[1].uv = ImVec2(s2, t1);
  5520. out_vertices[1].col = col;
  5521. out_vertices[2].pos = ImVec2(x2, y2);
  5522. out_vertices[2].uv = ImVec2(s2, t2);
  5523. out_vertices[2].col = col;
  5524. out_vertices[3] = out_vertices[0];
  5525. out_vertices[4] = out_vertices[2];
  5526. out_vertices[5].pos = ImVec2(x1, y2);
  5527. out_vertices[5].uv = ImVec2(s1, t2);
  5528. out_vertices[5].col = col;
  5529. out_vertices += 6;
  5530. }
  5531. }
  5532. }
  5533. }
  5534. x += char_width;
  5535. }
  5536. }
  5537. //-----------------------------------------------------------------------------
  5538. // PLATFORM DEPENDANT HELPERS
  5539. //-----------------------------------------------------------------------------
  5540. #if defined(_MSC_VER) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCS)
  5541. #define WIN32_LEAN_AND_MEAN
  5542. #include <windows.h>
  5543. // Win32 API clipboard implementation
  5544. static const char* GetClipboardTextFn_DefaultImpl()
  5545. {
  5546. static char* buf_local = NULL;
  5547. if (buf_local)
  5548. {
  5549. ImGui::MemFree(buf_local);
  5550. buf_local = NULL;
  5551. }
  5552. if (!OpenClipboard(NULL))
  5553. return NULL;
  5554. HANDLE buf_handle = GetClipboardData(CF_TEXT);
  5555. if (buf_handle == NULL)
  5556. return NULL;
  5557. if (char* buf_global = (char*)GlobalLock(buf_handle))
  5558. buf_local = ImStrdup(buf_global);
  5559. GlobalUnlock(buf_handle);
  5560. CloseClipboard();
  5561. return buf_local;
  5562. }
  5563. // Win32 API clipboard implementation
  5564. static void SetClipboardTextFn_DefaultImpl(const char* text)
  5565. {
  5566. if (!OpenClipboard(NULL))
  5567. return;
  5568. const char* text_end = text + strlen(text);
  5569. const int buf_length = (int)(text_end - text) + 1;
  5570. HGLOBAL buf_handle = GlobalAlloc(GMEM_MOVEABLE, (SIZE_T)buf_length * sizeof(char));
  5571. if (buf_handle == NULL)
  5572. return;
  5573. char* buf_global = (char *)GlobalLock(buf_handle);
  5574. memcpy(buf_global, text, (size_t)(text_end - text));
  5575. buf_global[text_end - text] = 0;
  5576. GlobalUnlock(buf_handle);
  5577. EmptyClipboard();
  5578. SetClipboardData(CF_TEXT, buf_handle);
  5579. CloseClipboard();
  5580. }
  5581. #else
  5582. // Local ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers
  5583. static const char* GetClipboardTextFn_DefaultImpl()
  5584. {
  5585. return GImGui.PrivateClipboard;
  5586. }
  5587. // Local ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers
  5588. static void SetClipboardTextFn_DefaultImpl(const char* text)
  5589. {
  5590. if (GImGui.PrivateClipboard)
  5591. {
  5592. ImGui::MemFree(GImGui.PrivateClipboard);
  5593. GImGui.PrivateClipboard = NULL;
  5594. }
  5595. const char* text_end = text + strlen(text);
  5596. GImGui.PrivateClipboard = (char*)ImGui::MemAlloc((size_t)(text_end - text) + 1);
  5597. memcpy(GImGui.PrivateClipboard, text, (size_t)(text_end - text));
  5598. GImGui.PrivateClipboard[(size_t)(text_end - text)] = 0;
  5599. }
  5600. #endif
  5601. //-----------------------------------------------------------------------------
  5602. // HELP
  5603. //-----------------------------------------------------------------------------
  5604. void ImGui::ShowUserGuide()
  5605. {
  5606. ImGuiState& g = GImGui;
  5607. ImGui::BulletText("Double-click on title bar to collapse window.");
  5608. ImGui::BulletText("Click and drag on lower right corner to resize window.");
  5609. ImGui::BulletText("Click and drag on any empty space to move window.");
  5610. ImGui::BulletText("Mouse Wheel to scroll.");
  5611. if (g.IO.FontAllowUserScaling)
  5612. ImGui::BulletText("CTRL+Mouse Wheel to zoom window contents.");
  5613. ImGui::BulletText("TAB/SHIFT+TAB to cycle through keyboard editable fields.");
  5614. ImGui::BulletText("CTRL+Click on a slider to input text.");
  5615. ImGui::BulletText(
  5616. "While editing text:\n"
  5617. "- Hold SHIFT or use mouse to select text\n"
  5618. "- CTRL+Left/Right to word jump\n"
  5619. "- CTRL+A select all\n"
  5620. "- CTRL+X,CTRL+C,CTRL+V clipboard\n"
  5621. "- CTRL+Z,CTRL+Y undo/redo\n"
  5622. "- ESCAPE to revert\n"
  5623. "- You can apply arithmetic operators +,*,/ on numerical values.\n"
  5624. " Use +- to subtract.\n");
  5625. }
  5626. void ImGui::ShowStyleEditor(ImGuiStyle* ref)
  5627. {
  5628. ImGuiState& g = GImGui;
  5629. ImGuiStyle& style = g.Style;
  5630. const ImGuiStyle def;
  5631. if (ImGui::Button("Revert Style"))
  5632. g.Style = ref ? *ref : def;
  5633. if (ref)
  5634. {
  5635. ImGui::SameLine();
  5636. if (ImGui::Button("Save Style"))
  5637. *ref = g.Style;
  5638. }
  5639. ImGui::PushItemWidth(ImGui::GetWindowWidth()*0.55f);
  5640. if (ImGui::TreeNode("Sizes"))
  5641. {
  5642. 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.
  5643. ImGui::SliderFloat2("WindowPadding", (float*)&style.WindowPadding, 0.0f, 20.0f, "%.0f");
  5644. ImGui::SliderFloat("WindowRounding", &style.WindowRounding, 0.0f, 16.0f, "%.0f");
  5645. ImGui::SliderFloat2("FramePadding", (float*)&style.FramePadding, 0.0f, 20.0f, "%.0f");
  5646. ImGui::SliderFloat2("ItemSpacing", (float*)&style.ItemSpacing, 0.0f, 20.0f, "%.0f");
  5647. ImGui::SliderFloat2("ItemInnerSpacing", (float*)&style.ItemInnerSpacing, 0.0f, 20.0f, "%.0f");
  5648. ImGui::SliderFloat2("TouchExtraPadding", (float*)&style.TouchExtraPadding, 0.0f, 20.0f, "%.0f");
  5649. ImGui::SliderFloat("TreeNodeSpacing", &style.TreeNodeSpacing, 0.0f, 20.0f, "%.0f");
  5650. ImGui::SliderFloat("ScrollBarWidth", &style.ScrollBarWidth, 0.0f, 20.0f, "%.0f");
  5651. ImGui::TreePop();
  5652. }
  5653. if (ImGui::TreeNode("Colors"))
  5654. {
  5655. static ImGuiColorEditMode edit_mode = ImGuiColorEditMode_RGB;
  5656. ImGui::RadioButton("RGB", &edit_mode, ImGuiColorEditMode_RGB);
  5657. ImGui::SameLine();
  5658. ImGui::RadioButton("HSV", &edit_mode, ImGuiColorEditMode_HSV);
  5659. ImGui::SameLine();
  5660. ImGui::RadioButton("HEX", &edit_mode, ImGuiColorEditMode_HEX);
  5661. static ImGuiTextFilter filter;
  5662. filter.Draw("Filter colors", 200);
  5663. ImGui::BeginChild("#colors", ImVec2(0, 300), true);
  5664. ImGui::ColorEditMode(edit_mode);
  5665. for (int i = 0; i < ImGuiCol_COUNT; i++)
  5666. {
  5667. const char* name = GetStyleColorName(i);
  5668. if (!filter.PassFilter(name))
  5669. continue;
  5670. ImGui::PushID(i);
  5671. ImGui::ColorEdit4(name, (float*)&style.Colors[i], true);
  5672. if (memcmp(&style.Colors[i], (ref ? &ref->Colors[i] : &def.Colors[i]), sizeof(ImVec4)) != 0)
  5673. {
  5674. ImGui::SameLine(); if (ImGui::Button("Revert")) style.Colors[i] = ref ? ref->Colors[i] : def.Colors[i];
  5675. if (ref) { ImGui::SameLine(); if (ImGui::Button("Save")) ref->Colors[i] = style.Colors[i]; }
  5676. }
  5677. ImGui::PopID();
  5678. }
  5679. ImGui::EndChild();
  5680. ImGui::TreePop();
  5681. }
  5682. /*
  5683. // Font scaling options
  5684. // Note that those are not actually part of the style.
  5685. if (ImGui::TreeNode("Font"))
  5686. {
  5687. static float window_scale = 1.0f;
  5688. ImGui::SliderFloat("window scale", &window_scale, 0.3f, 2.0f, "%.1f"); // scale only this window
  5689. ImGui::SliderFloat("font scale", &ImGui::GetIO().Font->Scale, 0.3f, 2.0f, "%.1f"); // scale only this font
  5690. ImGui::SliderFloat("global scale", &ImGui::GetIO().FontGlobalScale, 0.3f, 2.0f, "%.1f"); // scale everything
  5691. ImGui::SetWindowFontScale(window_scale);
  5692. ImGui::TreePop();
  5693. }
  5694. */
  5695. ImGui::PopItemWidth();
  5696. }
  5697. //-----------------------------------------------------------------------------
  5698. // SAMPLE CODE
  5699. //-----------------------------------------------------------------------------
  5700. static void ShowExampleAppConsole(bool* open);
  5701. static void ShowExampleAppLongText(bool* open);
  5702. static void ShowExampleAppAutoResize(bool* open);
  5703. // Demonstrate ImGui features (unfortunately this makes this function a little bloated!)
  5704. void ImGui::ShowTestWindow(bool* open)
  5705. {
  5706. static bool no_titlebar = false;
  5707. static bool no_border = true;
  5708. static bool no_resize = false;
  5709. static bool no_move = false;
  5710. static bool no_scrollbar = false;
  5711. static float fill_alpha = 0.65f;
  5712. const ImGuiWindowFlags layout_flags = (no_titlebar ? ImGuiWindowFlags_NoTitleBar : 0) | (no_border ? 0 : ImGuiWindowFlags_ShowBorders) | (no_resize ? ImGuiWindowFlags_NoResize : 0) | (no_move ? ImGuiWindowFlags_NoMove : 0) | (no_scrollbar ? ImGuiWindowFlags_NoScrollbar : 0);
  5713. ImGui::Begin("ImGui Test", open, ImVec2(550,680), fill_alpha, layout_flags);
  5714. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.65f);
  5715. ImGui::Text("ImGui says hello.");
  5716. //ImGui::Text("MousePos (%g, %g)", ImGui::GetIO().MousePos.x, ImGui::GetIO().MousePos.y);
  5717. //ImGui::Text("MouseWheel %d", ImGui::GetIO().MouseWheel);
  5718. ImGui::Spacing();
  5719. if (ImGui::CollapsingHeader("Help"))
  5720. {
  5721. ImGui::TextWrapped("This window is being created by the ShowTestWindow() function. Please refer to the code for programming reference.\n\nUser Guide:");
  5722. ImGui::ShowUserGuide();
  5723. }
  5724. if (ImGui::CollapsingHeader("Window options"))
  5725. {
  5726. ImGui::Checkbox("no titlebar", &no_titlebar); ImGui::SameLine(150);
  5727. ImGui::Checkbox("no border", &no_border); ImGui::SameLine(300);
  5728. ImGui::Checkbox("no resize", &no_resize);
  5729. ImGui::Checkbox("no move", &no_move); ImGui::SameLine(150);
  5730. ImGui::Checkbox("no scrollbar", &no_scrollbar);
  5731. ImGui::SliderFloat("fill alpha", &fill_alpha, 0.0f, 1.0f);
  5732. if (ImGui::TreeNode("Style Editor"))
  5733. {
  5734. ImGui::ShowStyleEditor();
  5735. ImGui::TreePop();
  5736. }
  5737. if (ImGui::TreeNode("Logging"))
  5738. {
  5739. ImGui::LogButtons();
  5740. ImGui::TreePop();
  5741. }
  5742. }
  5743. if (ImGui::CollapsingHeader("Widgets"))
  5744. {
  5745. static bool a=false;
  5746. if (ImGui::Button("Button")) { printf("Clicked\n"); a ^= 1; }
  5747. if (a)
  5748. {
  5749. ImGui::SameLine();
  5750. ImGui::Text("Thanks for clicking me!");
  5751. }
  5752. if (ImGui::TreeNode("Tree"))
  5753. {
  5754. for (size_t i = 0; i < 5; i++)
  5755. {
  5756. if (ImGui::TreeNode((void*)i, "Child %d", i))
  5757. {
  5758. ImGui::Text("blah blah");
  5759. ImGui::SameLine();
  5760. if (ImGui::SmallButton("print"))
  5761. printf("Child %d pressed", (int)i);
  5762. ImGui::TreePop();
  5763. }
  5764. }
  5765. ImGui::TreePop();
  5766. }
  5767. if (ImGui::TreeNode("Bullets"))
  5768. {
  5769. ImGui::BulletText("Bullet point 1");
  5770. ImGui::BulletText("Bullet point 2\nOn multiple lines");
  5771. ImGui::BulletText("Bullet point 3");
  5772. ImGui::TreePop();
  5773. }
  5774. if (ImGui::TreeNode("Colored Text"))
  5775. {
  5776. // Using shortcut. You can use PushStyleColor()/PopStyleColor() for more flexibility.
  5777. ImGui::TextColored(ImVec4(1.0f,0.0f,1.0f,1.0f), "Pink");
  5778. ImGui::TextColored(ImVec4(1.0f,1.0f,0.0f,1.0f), "Yellow");
  5779. ImGui::TreePop();
  5780. }
  5781. if (ImGui::TreeNode("Word Wrapping"))
  5782. {
  5783. // Using shortcut. You can use PushTextWrapPos()/PopTextWrapPos() for more flexibility.
  5784. ImGui::TextWrapped("This is a long paragraph. The text should automatically wrap on the edge of the window. The current implementation follows simple rules that works for English and possibly other languages.");
  5785. ImGui::Spacing();
  5786. static float wrap_width = 200.0f;
  5787. ImGui::SliderFloat("Wrap width", &wrap_width, -20, 600, "%.0f");
  5788. ImGui::Text("Test paragraph 1:");
  5789. ImGui::GetWindowDrawList()->AddRectFilled(ImGui::GetCursorScreenPos() + ImVec2(wrap_width, 0.0f), ImGui::GetCursorScreenPos() + ImVec2(wrap_width+10, ImGui::GetTextLineHeight()), 0xFFFF00FF);
  5790. ImGui::PushTextWrapPos(ImGui::GetCursorPos().x + wrap_width);
  5791. ImGui::Text("lazy dog. This paragraph is made to fit within %.0f pixels. The quick brown fox jumps over the lazy dog.", wrap_width);
  5792. ImGui::GetWindowDrawList()->AddRect(ImGui::GetItemBoxMin(), ImGui::GetItemBoxMax(), 0xFF00FFFF);
  5793. ImGui::PopTextWrapPos();
  5794. ImGui::Text("Test paragraph 2:");
  5795. ImGui::GetWindowDrawList()->AddRectFilled(ImGui::GetCursorScreenPos() + ImVec2(wrap_width, 0.0f), ImGui::GetCursorScreenPos() + ImVec2(wrap_width+10, ImGui::GetTextLineHeight()), 0xFFFF00FF);
  5796. ImGui::PushTextWrapPos(ImGui::GetCursorPos().x + wrap_width);
  5797. ImGui::Text("aaaaaaaa bbbbbbbb, cccccccc,dddddddd. eeeeeeee ffffffff. gggggggg!hhhhhhhh");
  5798. ImGui::GetWindowDrawList()->AddRect(ImGui::GetItemBoxMin(), ImGui::GetItemBoxMax(), 0xFF00FFFF);
  5799. ImGui::PopTextWrapPos();
  5800. ImGui::TreePop();
  5801. }
  5802. if (ImGui::TreeNode("UTF-8 Text"))
  5803. {
  5804. // UTF-8 test (need a suitable font, try extra_fonts/mplus* files for example)
  5805. // Most compiler appears to support UTF-8 in source code (with Visual Studio you need to save your file as 'UTF-8 without signature')
  5806. // 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.
  5807. // In your own application please be reasonable and use UTF-8 in the source or get the data from external files! :)
  5808. ImGui::TextWrapped("(CJK text will only appears if the font supports it. Please check in the extra_fonts/ folder if you intend to use non-ASCII characters. Note that characters values are preserved even if the font cannot be displayed, so you can safely copy & paste garbled characters.)");
  5809. ImGui::Text("Hiragana: \xe3\x81\x8b\xe3\x81\x8d\xe3\x81\x8f\xe3\x81\x91\xe3\x81\x93 (kakikukeko)");
  5810. ImGui::Text("Kanjis: \xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e (nihongo)");
  5811. static char buf[32] = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e";
  5812. ImGui::InputText("UTF-8 input", buf, IM_ARRAYSIZE(buf));
  5813. ImGui::TreePop();
  5814. }
  5815. static bool check = true;
  5816. ImGui::Checkbox("checkbox", &check);
  5817. static int e = 0;
  5818. ImGui::RadioButton("radio a", &e, 0); ImGui::SameLine();
  5819. ImGui::RadioButton("radio b", &e, 1); ImGui::SameLine();
  5820. ImGui::RadioButton("radio c", &e, 2);
  5821. ImGui::Text("Hover me");
  5822. if (ImGui::IsItemHovered())
  5823. ImGui::SetTooltip("I am a tooltip");
  5824. ImGui::SameLine();
  5825. ImGui::Text("- or me");
  5826. if (ImGui::IsItemHovered())
  5827. {
  5828. ImGui::BeginTooltip();
  5829. ImGui::Text("I am a fancy tooltip");
  5830. static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f };
  5831. ImGui::PlotLines("Curve", arr, IM_ARRAYSIZE(arr));
  5832. ImGui::EndTooltip();
  5833. }
  5834. // Testing IMGUI_ONCE_UPON_A_FRAME macro
  5835. //for (int i = 0; i < 5; i++)
  5836. //{
  5837. // IMGUI_ONCE_UPON_A_FRAME
  5838. // {
  5839. // ImGui::Text("This will be displayed only once.");
  5840. // }
  5841. //}
  5842. ImGui::Separator();
  5843. static int item = 1;
  5844. ImGui::Combo("combo", &item, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0");
  5845. const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK" };
  5846. static int item2 = -1;
  5847. ImGui::Combo("combo scroll", &item2, items, IM_ARRAYSIZE(items));
  5848. static char str0[128] = "Hello, world!";
  5849. static int i0=123;
  5850. static float f0=0.001f;
  5851. ImGui::InputText("string", str0, IM_ARRAYSIZE(str0));
  5852. ImGui::InputInt("input int", &i0);
  5853. ImGui::InputFloat("input float", &f0, 0.01f, 1.0f);
  5854. //static float vec2a[3] = { 0.10f, 0.20f };
  5855. //ImGui::InputFloat2("input float2", vec2a);
  5856. static float vec3a[3] = { 0.10f, 0.20f, 0.30f };
  5857. ImGui::InputFloat3("input float3", vec3a);
  5858. //static float vec4a[4] = { 0.10f, 0.20f, 0.30f, 0.44f };
  5859. //ImGui::InputFloat4("input float4", vec4a);
  5860. static int i1=0;
  5861. static int i2=42;
  5862. ImGui::SliderInt("int 0..3", &i1, 0, 3);
  5863. ImGui::SliderInt("int -100..100", &i2, -100, 100);
  5864. static float f1=1.123f;
  5865. static float f2=0;
  5866. static float f3=0;
  5867. static float f4=123456789.0f;
  5868. ImGui::SliderFloat("float", &f1, 0.0f, 2.0f);
  5869. ImGui::SliderFloat("log float", &f2, 0.0f, 10.0f, "%.4f", 2.0f);
  5870. ImGui::SliderFloat("signed log float", &f3, -10.0f, 10.0f, "%.4f", 3.0f);
  5871. ImGui::SliderFloat("unbound float", &f4, -FLT_MAX, FLT_MAX, "%.4f");
  5872. static float angle = 0.0f;
  5873. ImGui::SliderAngle("angle", &angle);
  5874. //static float vec2b[3] = { 0.10f, 0.20f };
  5875. //ImGui::SliderFloat2("slider float2", vec2b, 0.0f, 1.0f);
  5876. static float vec3b[3] = { 0.10f, 0.20f, 0.30f };
  5877. ImGui::SliderFloat3("slider float3", vec3b, 0.0f, 1.0f);
  5878. //static float vec4b[4] = { 0.10f, 0.20f, 0.30f, 0.40f };
  5879. //ImGui::SliderFloat4("slider float4", vec4b, 0.0f, 1.0f);
  5880. static float col1[3] = { 1.0f,0.0f,0.2f };
  5881. static float col2[4] = { 0.4f,0.7f,0.0f,0.5f };
  5882. ImGui::ColorEdit3("color 1", col1);
  5883. ImGui::ColorEdit4("color 2", col2);
  5884. }
  5885. if (ImGui::CollapsingHeader("Graphs widgets"))
  5886. {
  5887. static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f };
  5888. ImGui::PlotLines("Frame Times", arr, IM_ARRAYSIZE(arr));
  5889. static bool pause;
  5890. static ImVector<float> values; if (values.empty()) { values.resize(100); memset(&values.front(), 0, values.size()*sizeof(float)); }
  5891. static size_t values_offset = 0;
  5892. if (!pause)
  5893. {
  5894. // create dummy data at fixed 60 hz rate
  5895. static float refresh_time = -1.0f;
  5896. if (ImGui::GetTime() > refresh_time + 1.0f/60.0f)
  5897. {
  5898. refresh_time = ImGui::GetTime();
  5899. static float phase = 0.0f;
  5900. values[values_offset] = cosf(phase);
  5901. values_offset = (values_offset+1)%values.size();
  5902. phase += 0.10f*values_offset;
  5903. }
  5904. }
  5905. ImGui::PlotLines("Frame Times", &values.front(), (int)values.size(), (int)values_offset, "avg 0.0", -1.0f, 1.0f, ImVec2(0,70));
  5906. ImGui::SameLine(); ImGui::Checkbox("pause", &pause);
  5907. ImGui::PlotHistogram("Histogram", arr, IM_ARRAYSIZE(arr), 0, NULL, 0.0f, 1.0f, ImVec2(0,70));
  5908. }
  5909. if (ImGui::CollapsingHeader("Widgets on same line"))
  5910. {
  5911. // Text
  5912. ImGui::Text("Hello");
  5913. ImGui::SameLine();
  5914. ImGui::Text("World");
  5915. // Button
  5916. if (ImGui::Button("Banana")) printf("Pressed!\n");
  5917. ImGui::SameLine();
  5918. ImGui::Button("Apple");
  5919. ImGui::SameLine();
  5920. ImGui::Button("Corniflower");
  5921. // Button
  5922. ImGui::SmallButton("Banana");
  5923. ImGui::SameLine();
  5924. ImGui::SmallButton("Apple");
  5925. ImGui::SameLine();
  5926. ImGui::SmallButton("Corniflower");
  5927. ImGui::SameLine();
  5928. ImGui::Text("Small buttons fit in a text block");
  5929. // Checkbox
  5930. static bool c1=false,c2=false,c3=false,c4=false;
  5931. ImGui::Checkbox("My", &c1);
  5932. ImGui::SameLine();
  5933. ImGui::Checkbox("Tailor", &c2);
  5934. ImGui::SameLine();
  5935. ImGui::Checkbox("Is", &c3);
  5936. ImGui::SameLine();
  5937. ImGui::Checkbox("Rich", &c4);
  5938. // SliderFloat
  5939. static float f0=1.0f, f1=2.0f, f2=3.0f;
  5940. ImGui::PushItemWidth(80);
  5941. ImGui::SliderFloat("f0", &f0, 0.0f,5.0f);
  5942. ImGui::SameLine();
  5943. ImGui::SliderFloat("f1", &f1, 0.0f,5.0f);
  5944. ImGui::SameLine();
  5945. ImGui::SliderFloat("f2", &f2, 0.0f,5.0f);
  5946. // InputText
  5947. static char s0[128] = "one", s1[128] = "two", s2[128] = "three";
  5948. ImGui::InputText("s0", s0, 128);
  5949. ImGui::SameLine();
  5950. ImGui::InputText("s1", s1, 128);
  5951. ImGui::SameLine();
  5952. ImGui::InputText("s2", s2, 128);
  5953. // LabelText
  5954. ImGui::LabelText("l0", "one");
  5955. ImGui::SameLine();
  5956. ImGui::LabelText("l0", "two");
  5957. ImGui::SameLine();
  5958. ImGui::LabelText("l0", "three");
  5959. ImGui::PopItemWidth();
  5960. }
  5961. if (ImGui::CollapsingHeader("Child regions"))
  5962. {
  5963. ImGui::Text("Without border");
  5964. static int line = 50;
  5965. bool goto_line = ImGui::Button("Goto");
  5966. ImGui::SameLine();
  5967. ImGui::PushItemWidth(100);
  5968. goto_line |= ImGui::InputInt("##Line", &line, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
  5969. ImGui::PopItemWidth();
  5970. ImGui::BeginChild("Sub1", ImVec2(ImGui::GetWindowWidth()*0.5f,300));
  5971. for (int i = 0; i < 100; i++)
  5972. {
  5973. ImGui::Text("%04d: scrollable region", i);
  5974. if (goto_line && line == i)
  5975. ImGui::SetScrollPosHere();
  5976. }
  5977. if (goto_line && line >= 100)
  5978. ImGui::SetScrollPosHere();
  5979. ImGui::EndChild();
  5980. ImGui::SameLine();
  5981. ImGui::BeginChild("Sub2", ImVec2(0,300), true);
  5982. ImGui::Text("With border");
  5983. ImGui::Columns(2);
  5984. for (int i = 0; i < 100; i++)
  5985. {
  5986. char buf[32];
  5987. ImFormatString(buf, IM_ARRAYSIZE(buf), "%08x", i*5731);
  5988. ImGui::Button(buf);
  5989. ImGui::NextColumn();
  5990. }
  5991. ImGui::EndChild();
  5992. }
  5993. if (ImGui::CollapsingHeader("Columns"))
  5994. {
  5995. ImGui::Columns(4, "data", true);
  5996. ImGui::Text("ID"); ImGui::NextColumn();
  5997. ImGui::Text("Name"); ImGui::NextColumn();
  5998. ImGui::Text("Path"); ImGui::NextColumn();
  5999. ImGui::Text("Flags"); ImGui::NextColumn();
  6000. ImGui::Separator();
  6001. ImGui::Text("0000"); ImGui::NextColumn();
  6002. ImGui::Text("Robert"); ImGui::NextColumn();
  6003. ImGui::Text("/path/robert"); ImGui::NextColumn();
  6004. ImGui::Text("...."); ImGui::NextColumn();
  6005. ImGui::Text("0001"); ImGui::NextColumn();
  6006. ImGui::Text("Stephanie"); ImGui::NextColumn();
  6007. ImGui::Text("/path/stephanie"); ImGui::NextColumn();
  6008. ImGui::Text("line 1"); ImGui::Text("line 2"); ImGui::NextColumn(); // two lines, two items
  6009. ImGui::Text("0002"); ImGui::NextColumn();
  6010. ImGui::Text("C64"); ImGui::NextColumn();
  6011. ImGui::Text("/path/computer"); ImGui::NextColumn();
  6012. ImGui::Text("...."); ImGui::NextColumn();
  6013. ImGui::Columns(1);
  6014. ImGui::Separator();
  6015. ImGui::Columns(3, "mixed");
  6016. // Create multiple items in a same cell because switching to next column
  6017. static int e = 0;
  6018. ImGui::Text("Hello");
  6019. ImGui::Button("Banana");
  6020. ImGui::RadioButton("radio a", &e, 0);
  6021. ImGui::NextColumn();
  6022. ImGui::Text("ImGui");
  6023. ImGui::Button("Apple");
  6024. ImGui::RadioButton("radio b", &e, 1);
  6025. ImGui::Text("An extra line here.");
  6026. ImGui::NextColumn();
  6027. ImGui::Text("World!");
  6028. ImGui::Button("Corniflower");
  6029. ImGui::RadioButton("radio c", &e, 2);
  6030. ImGui::NextColumn();
  6031. if (ImGui::CollapsingHeader("Category A")) ImGui::Text("Blah blah blah"); ImGui::NextColumn();
  6032. if (ImGui::CollapsingHeader("Category B")) ImGui::Text("Blah blah blah"); ImGui::NextColumn();
  6033. if (ImGui::CollapsingHeader("Category C")) ImGui::Text("Blah blah blah"); ImGui::NextColumn();
  6034. ImGui::Columns(1);
  6035. ImGui::Separator();
  6036. ImGui::Columns(2, "multiple components");
  6037. static float foo = 1.0f;
  6038. ImGui::InputFloat("red", &foo, 0.05f, 0, 3); ImGui::NextColumn();
  6039. static float bar = 1.0f;
  6040. ImGui::InputFloat("blue", &bar, 0.05f, 0, 3); ImGui::NextColumn();
  6041. ImGui::Columns(1);
  6042. ImGui::Separator();
  6043. ImGui::Columns(2, "word wrapping");
  6044. ImGui::TextWrapped("The quick brown fox jumps over the lazy dog.");
  6045. ImGui::Text("Hello Left");
  6046. ImGui::NextColumn();
  6047. ImGui::TextWrapped("The quick brown fox jumps over the lazy dog.");
  6048. ImGui::Text("Hello Right");
  6049. ImGui::Columns(1);
  6050. ImGui::Separator();
  6051. if (ImGui::TreeNode("Inside a tree.."))
  6052. {
  6053. if (ImGui::TreeNode("node 1 (with borders)"))
  6054. {
  6055. ImGui::Columns(4);
  6056. ImGui::Text("aaa"); ImGui::NextColumn();
  6057. ImGui::Text("bbb"); ImGui::NextColumn();
  6058. ImGui::Text("ccc"); ImGui::NextColumn();
  6059. ImGui::Text("ddd"); ImGui::NextColumn();
  6060. ImGui::Text("eee"); ImGui::NextColumn();
  6061. ImGui::Text("fff"); ImGui::NextColumn();
  6062. ImGui::Text("ggg"); ImGui::NextColumn();
  6063. ImGui::Text("hhh"); ImGui::NextColumn();
  6064. ImGui::Columns(1);
  6065. ImGui::TreePop();
  6066. }
  6067. if (ImGui::TreeNode("node 2 (without borders)"))
  6068. {
  6069. ImGui::Columns(4, NULL, false);
  6070. ImGui::Text("aaa"); ImGui::NextColumn();
  6071. ImGui::Text("bbb"); ImGui::NextColumn();
  6072. ImGui::Text("ccc"); ImGui::NextColumn();
  6073. ImGui::Text("ddd"); ImGui::NextColumn();
  6074. ImGui::Text("eee"); ImGui::NextColumn();
  6075. ImGui::Text("fff"); ImGui::NextColumn();
  6076. ImGui::Text("ggg"); ImGui::NextColumn();
  6077. ImGui::Text("hhh"); ImGui::NextColumn();
  6078. ImGui::Columns(1);
  6079. ImGui::TreePop();
  6080. }
  6081. ImGui::TreePop();
  6082. }
  6083. }
  6084. if (ImGui::CollapsingHeader("Filtering"))
  6085. {
  6086. static ImGuiTextFilter filter;
  6087. ImGui::Text("Filter usage:\n"
  6088. " \"\" display all lines\n"
  6089. " \"xxx\" display lines containing \"xxx\"\n"
  6090. " \"xxx,yyy\" display lines containing \"xxx\" or \"yyy\"\n"
  6091. " \"-xxx\" hide lines containing \"xxx\"");
  6092. filter.Draw();
  6093. const char* lines[] = { "aaa1.c", "bbb1.c", "ccc1.c", "aaa2.cpp", "bbb2.cpp", "ccc2.cpp", "abc.h", "hello, world" };
  6094. for (size_t i = 0; i < IM_ARRAYSIZE(lines); i++)
  6095. if (filter.PassFilter(lines[i]))
  6096. ImGui::BulletText("%s", lines[i]);
  6097. }
  6098. if (ImGui::CollapsingHeader("Keyboard & Focus"))
  6099. {
  6100. if (ImGui::TreeNode("Tabbing"))
  6101. {
  6102. ImGui::Text("Use TAB/SHIFT+TAB to cycle thru keyboard editable fields.");
  6103. static char buf[32] = "dummy";
  6104. ImGui::InputText("1", buf, IM_ARRAYSIZE(buf));
  6105. ImGui::InputText("2", buf, IM_ARRAYSIZE(buf));
  6106. ImGui::InputText("3", buf, IM_ARRAYSIZE(buf));
  6107. ImGui::PushAllowKeyboardFocus(false);
  6108. ImGui::InputText("4 (tab skip)", buf, IM_ARRAYSIZE(buf));
  6109. //ImGui::SameLine(); ImGui::Text("(?)"); if (ImGui::IsHovered()) ImGui::SetTooltip("Use ImGui::PushAllowKeyboardFocus(bool)\nto disable tabbing through certain widgets.");
  6110. ImGui::PopAllowKeyboardFocus();
  6111. ImGui::InputText("5", buf, IM_ARRAYSIZE(buf));
  6112. ImGui::TreePop();
  6113. }
  6114. if (ImGui::TreeNode("Focus from code"))
  6115. {
  6116. bool focus_1 = ImGui::Button("Focus on 1"); ImGui::SameLine();
  6117. bool focus_2 = ImGui::Button("Focus on 2"); ImGui::SameLine();
  6118. bool focus_3 = ImGui::Button("Focus on 3");
  6119. int has_focus = 0;
  6120. static char buf[128] = "click on a button to set focus";
  6121. if (focus_1) ImGui::SetKeyboardFocusHere();
  6122. ImGui::InputText("1", buf, IM_ARRAYSIZE(buf));
  6123. if (ImGui::IsItemFocused()) has_focus = 1;
  6124. if (focus_2) ImGui::SetKeyboardFocusHere();
  6125. ImGui::InputText("2", buf, IM_ARRAYSIZE(buf));
  6126. if (ImGui::IsItemFocused()) has_focus = 2;
  6127. ImGui::PushAllowKeyboardFocus(false);
  6128. if (focus_3) ImGui::SetKeyboardFocusHere();
  6129. ImGui::InputText("3 (tab skip)", buf, IM_ARRAYSIZE(buf));
  6130. if (ImGui::IsItemFocused()) has_focus = 3;
  6131. ImGui::PopAllowKeyboardFocus();
  6132. if (has_focus)
  6133. ImGui::Text("Item with focus: %d", has_focus);
  6134. else
  6135. ImGui::Text("Item with focus: <none>");
  6136. ImGui::TreePop();
  6137. }
  6138. }
  6139. static bool show_app_console = false;
  6140. static bool show_app_long_text = false;
  6141. static bool show_app_auto_resize = false;
  6142. if (ImGui::CollapsingHeader("App Examples"))
  6143. {
  6144. ImGui::Checkbox("Console", &show_app_console);
  6145. ImGui::Checkbox("Long text display", &show_app_long_text);
  6146. ImGui::Checkbox("Auto-resizing window", &show_app_auto_resize);
  6147. }
  6148. if (show_app_console)
  6149. ShowExampleAppConsole(&show_app_console);
  6150. if (show_app_long_text)
  6151. ShowExampleAppLongText(&show_app_long_text);
  6152. if (show_app_auto_resize)
  6153. ShowExampleAppAutoResize(&show_app_auto_resize);
  6154. ImGui::End();
  6155. }
  6156. static void ShowExampleAppAutoResize(bool* open)
  6157. {
  6158. if (!ImGui::Begin("Example: Auto-Resizing Window", open, ImVec2(0,0), -1.0f, ImGuiWindowFlags_AlwaysAutoResize))
  6159. {
  6160. ImGui::End();
  6161. return;
  6162. }
  6163. static int lines = 10;
  6164. ImGui::TextWrapped("Window will resize every-frame to the size of its content. Note that you don't want to query the window size to output your content because that would create a feedback loop.");
  6165. ImGui::SliderInt("Number of lines", &lines, 1, 20);
  6166. for (int i = 0; i < lines; i++)
  6167. ImGui::Text("%*sThis is line %d", i*4, "", i); // Pad with space to extend size horizontally
  6168. ImGui::End();
  6169. }
  6170. struct ExampleAppConsole
  6171. {
  6172. ImVector<char*> Items;
  6173. bool NewItems;
  6174. void Clear()
  6175. {
  6176. for (size_t i = 0; i < Items.size(); i++)
  6177. ImGui::MemFree(Items[i]);
  6178. Items.clear();
  6179. NewItems = true;
  6180. }
  6181. void AddLog(const char* fmt, ...)
  6182. {
  6183. char buf[512];
  6184. va_list args;
  6185. va_start(args, fmt);
  6186. ImFormatStringV(buf, IM_ARRAYSIZE(buf), fmt, args);
  6187. va_end(args);
  6188. Items.push_back(ImStrdup(buf));
  6189. NewItems = true;
  6190. }
  6191. void TextEditCallback(ImGuiTextEditCallbackData* data)
  6192. {
  6193. //AddLog("cursor: %d, selection: %d-%d", data->CursorPos, data->SelectionStart, data->SelectionEnd);
  6194. switch (data->EventKey)
  6195. {
  6196. case ImGuiKey_Tab:
  6197. {
  6198. // Example of TEXT COMPLETION
  6199. // Locate beginning of current word
  6200. const char* word_end = data->Buf + data->CursorPos;
  6201. const char* word_start = word_end;
  6202. while (word_start > data->Buf)
  6203. {
  6204. const char c = word_start[-1];
  6205. if (c == ' ' || c == '\t' || c == ',' || c == ';')
  6206. break;
  6207. word_start--;
  6208. }
  6209. // Build a list of candidates
  6210. const char* commands[] = { "HELP", "CLEAR", "CLASSIFY" };
  6211. ImVector<const char*> candidates;
  6212. for (size_t i = 0; i < IM_ARRAYSIZE(commands); i++)
  6213. if (ImStrnicmp(commands[i], word_start, word_end-word_start) == 0)
  6214. candidates.push_back(commands[i]);
  6215. if (candidates.size() == 0)
  6216. {
  6217. // No match
  6218. AddLog("No match for \"%.*s\"!\n", word_end-word_start, word_start);
  6219. }
  6220. else if (candidates.size() == 1)
  6221. {
  6222. // Single match. Delete the beginning of the word and replace it entirely so we've got nice casing
  6223. data->DeleteChars(word_start-data->Buf, word_end-word_start);
  6224. data->InsertChars(data->CursorPos, candidates[0]);
  6225. data->InsertChars(data->CursorPos, " ");
  6226. }
  6227. else
  6228. {
  6229. // Multiple matches. Complete as much as we can, so inputing "C" will complete to "CL" and display "CLEAR" and "CLASSIFY"
  6230. int match_len = word_end - word_start;
  6231. while (true)
  6232. {
  6233. int c = 0;
  6234. bool all_candidates_matches = true;
  6235. for (size_t i = 0; i < candidates.size() && all_candidates_matches; i++)
  6236. {
  6237. if (i == 0)
  6238. c = toupper(candidates[i][match_len]);
  6239. else if (c != toupper(candidates[i][match_len]))
  6240. all_candidates_matches = false;
  6241. }
  6242. if (!all_candidates_matches)
  6243. break;
  6244. match_len++;
  6245. }
  6246. if (match_len > 0)
  6247. {
  6248. data->DeleteChars(word_start - data->Buf, word_end-word_start);
  6249. data->InsertChars(data->CursorPos, candidates[0], candidates[0] + match_len);
  6250. }
  6251. // List matches
  6252. AddLog("Possible matches:\n");
  6253. for (size_t i = 0; i < candidates.size(); i++)
  6254. AddLog("- %s\n", candidates[i]);
  6255. }
  6256. break;
  6257. }
  6258. }
  6259. }
  6260. };
  6261. static void ShowExampleAppConsole_TextEditCallback(ImGuiTextEditCallbackData* data)
  6262. {
  6263. ExampleAppConsole* console = (ExampleAppConsole*)data->UserData;
  6264. console->TextEditCallback(data);
  6265. }
  6266. static void ShowExampleAppConsole(bool* open)
  6267. {
  6268. if (!ImGui::Begin("Example: Console", open, ImVec2(520,600)))
  6269. {
  6270. ImGui::End();
  6271. return;
  6272. }
  6273. ImGui::TextWrapped("This example implement a simple console. A more elaborate implementation may want to store individual entries along with extra data such as timestamp, emitter, etc.");
  6274. ImGui::TextWrapped("Press TAB to use text completion.");
  6275. // TODO: display from bottom
  6276. // TODO: clip manually
  6277. // TODO: history
  6278. static ExampleAppConsole console;
  6279. static char input[256] = "";
  6280. if (ImGui::SmallButton("Add Dummy Text")) console.AddLog("some text\nsome more text");
  6281. ImGui::SameLine();
  6282. if (ImGui::SmallButton("Add Dummy Error")) console.AddLog("[error] something went wrong");
  6283. ImGui::SameLine();
  6284. if (ImGui::SmallButton("Clear all")) console.Clear();
  6285. ImGui::Separator();
  6286. ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0,0));
  6287. static ImGuiTextFilter filter;
  6288. filter.Draw("Filter (\"incl,-excl\") (\"error\")", 180);
  6289. if (ImGui::IsItemHovered()) ImGui::SetKeyboardFocusHere(-1); // Auto focus on hover
  6290. ImGui::PopStyleVar();
  6291. ImGui::Separator();
  6292. ImGui::BeginChild("ScrollingRegion", ImVec2(0,-ImGui::GetTextLineSpacing()*2));
  6293. // 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());
  6294. // NB- if you have lots of text this approach may be too inefficient. You can seek and display only the lines that are on display using a technique similar to what TextUnformatted() does,
  6295. // or faster if your entries are already stored into a table.
  6296. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4,1)); // tighten spacing
  6297. ImGui::GetStyle().ItemSpacing.y = 1; // tighten spacing
  6298. for (size_t i = 0; i < console.Items.size(); i++)
  6299. {
  6300. const char* item = console.Items[i];
  6301. if (!filter.PassFilter(item))
  6302. continue;
  6303. ImVec4 col(1,1,1,1);
  6304. if (strstr(item, "[error]")) col = ImVec4(1.0f,0.4f,0.4f,1.0f);
  6305. else if (strncmp(item, "# ", 2) == 0) col = ImVec4(1.0f,0.8f,0.6f,1.0f);
  6306. ImGui::PushStyleColor(ImGuiCol_Text, col);
  6307. ImGui::TextUnformatted(item);
  6308. ImGui::PopStyleColor();
  6309. }
  6310. ImGui::PopStyleVar();
  6311. if (console.NewItems)
  6312. {
  6313. ImGui::SetScrollPosHere();
  6314. console.NewItems = false;
  6315. }
  6316. ImGui::EndChild();
  6317. ImGui::Separator();
  6318. if (ImGui::InputText("Input", input, IM_ARRAYSIZE(input), ImGuiInputTextFlags_EnterReturnsTrue|ImGuiInputTextFlags_CallbackCompletion, &ShowExampleAppConsole_TextEditCallback, (void*)&console))
  6319. {
  6320. const char* input_trimmed_end = input+strlen(input);
  6321. while (input_trimmed_end > input && input_trimmed_end[-1] == ' ')
  6322. input_trimmed_end--;
  6323. if (input_trimmed_end > input)
  6324. {
  6325. console.AddLog("# %s\n", input);
  6326. console.AddLog("Unknown command: '%.*s'\n", input_trimmed_end-input, input); // NB: we don't actually handle any command in this sample code
  6327. }
  6328. strcpy(input, "");
  6329. }
  6330. if (ImGui::IsItemHovered()) ImGui::SetKeyboardFocusHere(-1); // Auto focus on hover
  6331. ImGui::End();
  6332. }
  6333. static void ShowExampleAppLongText(bool* open)
  6334. {
  6335. if (!ImGui::Begin("Example: Long text display", open, ImVec2(520,600)))
  6336. {
  6337. ImGui::End();
  6338. return;
  6339. }
  6340. static ImGuiTextBuffer log;
  6341. static int lines = 0;
  6342. ImGui::Text("Printing unusually long amount of text.");
  6343. ImGui::Text("Buffer contents: %d lines, %d bytes", lines, log.size());
  6344. if (ImGui::Button("Clear")) { log.clear(); lines = 0; }
  6345. ImGui::SameLine();
  6346. if (ImGui::Button("Add 1000 lines"))
  6347. {
  6348. for (int i = 0; i < 1000; i++)
  6349. log.append("%i The quick brown fox jumps over the lazy dog\n", lines+i);
  6350. lines += 1000;
  6351. }
  6352. ImGui::BeginChild("Log");
  6353. ImGui::TextUnformatted(log.begin(), log.end());
  6354. ImGui::EndChild();
  6355. ImGui::End();
  6356. }
  6357. // End of Sample code
  6358. //-----------------------------------------------------------------------------
  6359. // Font data
  6360. // Bitmap exported from proggy_clean.fon (c) by Tristan Grimmer http://upperbounds.net/
  6361. // Also available on unofficial ProggyFonts mirror http://www.proggyfonts.net
  6362. //-----------------------------------------------------------------------------
  6363. /*
  6364. // Copyright (c) 2004, 2005 Tristan Grimmer
  6365. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6366. // of this software and associated documentation files (the "Software"), to deal
  6367. // in the Software without restriction, including without limitation the rights
  6368. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6369. // copies of the Software, and to permit persons to whom the Software is
  6370. // furnished to do so, subject to the following conditions:
  6371. // The above copyright notice and this permission notice shall be included in all
  6372. // copies or substantial portions of the Software.
  6373. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  6374. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  6375. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  6376. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  6377. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  6378. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  6379. // SOFTWARE.
  6380. */
  6381. //-----------------------------------------------------------------------------
  6382. // Fonts exported with BMFont http://www.angelcode.com/products/bmfont
  6383. // We are using bmfont format and you can load your own font from a file by setting up ImGui::GetIO().Font
  6384. // PNG further compressed with pngout.exe http://advsys.net/ken/utils.htm
  6385. // Manually converted to C++ array using the following program:
  6386. /*
  6387. static void binary_to_c(const char* name_in, const char* symbol)
  6388. {
  6389. FILE* fi = fopen(name_in, "rb"); fseek(fi, 0, SEEK_END); long sz = ftell(fi); fseek(fi, 0, SEEK_SET);
  6390. fprintf(stdout, "static const unsigned int %s_size = %d;\n", symbol, sz);
  6391. fprintf(stdout, "static const unsigned int %s_data[%d/4] =\n{", symbol, ((sz+3)/4)*4);
  6392. int column = 0;
  6393. for (unsigned int data = 0; fread(&data, 1, 4, fi); data = 0)
  6394. if ((column++ % 12) == 0)
  6395. fprintf(stdout, "\n 0x%08x, ", data);
  6396. else
  6397. fprintf(stdout, "0x%08x, ", data);
  6398. fprintf(stdout, "\n};\n\n");
  6399. fclose(fi);
  6400. }
  6401. int main(int argc, char** argv)
  6402. {
  6403. binary_to_c("proggy_clean_13.fnt", "proggy_clean_13_fnt");
  6404. binary_to_c("proggy_clean_13.png", "proggy_clean_13_png");
  6405. return 1;
  6406. }
  6407. */
  6408. //-----------------------------------------------------------------------------
  6409. static const unsigned int proggy_clean_13_png_size = 1557;
  6410. static const unsigned int proggy_clean_13_png_data[1560/4] =
  6411. {
  6412. 0x474e5089, 0x0a1a0a0d, 0x0d000000, 0x52444849, 0x00010000, 0x80000000, 0x00000308, 0x476bd300, 0x00000038, 0x544c5006, 0x00000045, 0xa5ffffff,
  6413. 0x00dd9fd9, 0x74010000, 0x00534e52, 0x66d8e640, 0xbd050000, 0x54414449, 0x9bed5e78, 0x30e36e51, 0xeef5440c, 0x31fde97f, 0x584ec0f0, 0x681ace39,
  6414. 0xca120e6b, 0x1c5a28a6, 0xc5d98a89, 0x1a3d602e, 0x323c0043, 0xf6bc9e68, 0xbe3ad62c, 0x3d60260f, 0x82d60096, 0xe0bfc707, 0xfb9bf1d1, 0xbf0267ac,
  6415. 0x1600260f, 0x061229c0, 0x0000c183, 0x37162c58, 0xdfa088fc, 0xde7d5704, 0x77fcbb80, 0x48e5c3f1, 0x73d8b8f8, 0xc4af7802, 0x1ca111ad, 0x0001ed7a,
  6416. 0x76eda3ef, 0xb78d3e00, 0x801c7203, 0x0215c0b1, 0x0410b044, 0xa85100d4, 0x07627ec7, 0x0cf83fa8, 0x94001a22, 0xf87347f1, 0xdcb5cfc1, 0x1c3880cc,
  6417. 0xd4e034ca, 0xfa928d9d, 0xb0167e31, 0x325cc570, 0x4bbd584b, 0xbd4e6574, 0x70bae084, 0xf0c0008a, 0x3f601ddb, 0x0bba506a, 0xa58a0082, 0x5b46946e,
  6418. 0x720a4ccd, 0xdfaaed39, 0x25dc8042, 0x7ee403f4, 0x2ad69cc9, 0x6c4b3009, 0x429037ed, 0x0293f875, 0x1a69dced, 0xab120198, 0x61c01d88, 0xcf2e43dc,
  6419. 0xfc3c00ef, 0xc049a270, 0xdbbea582, 0x0d592601, 0xc3c9a8dd, 0x5013d143, 0x19a47bbb, 0xf89253dd, 0x0a9901dc, 0x38900ecd, 0xb2dec9d7, 0xc2b91230,
  6420. 0xb8e0106f, 0x976404cb, 0x5d83c3f3, 0x6e8086fd, 0x5c9ab007, 0xf50354f6, 0xe7e72002, 0x4bc870ca, 0xab49736f, 0xc137c6e0, 0xa9aa6ff3, 0xbff84f2f,
  6421. 0x673e6e20, 0xf6e3c7e0, 0x618fe05a, 0x39ca2a00, 0x93ca03b4, 0x3a9d2728, 0xbbebba41, 0xce0e3681, 0x6e29ec05, 0x111eca83, 0xfdfe7ec1, 0xa7c8a75b,
  6422. 0xac6bc3ab, 0x72a5bc25, 0x9f612c1c, 0x378ec05e, 0x7202b157, 0x789e5a82, 0x5256bc0e, 0xcb900996, 0x10721105, 0x00823ce0, 0x69ab59fb, 0x39c72084,
  6423. 0xf5e37b25, 0xd1794700, 0x538d0637, 0x9a2bff4f, 0xce0d43a4, 0xa6da7ed2, 0xd7095132, 0xf5ad6232, 0x9aaa8e9c, 0xd8d1d3ed, 0x058940a1, 0x21f00d64,
  6424. 0x89a5c9de, 0x021b3f24, 0x77a97aac, 0x714be65a, 0x5e2d57ae, 0x27e3610f, 0x28809288, 0x36b9559f, 0xd00e347a, 0x0094e385, 0x565d034d, 0x7f52d5f2,
  6425. 0x9aea81de, 0x5e804909, 0x010d7f0a, 0x8f0d3fb1, 0xbbce23bc, 0x375e85ac, 0x01fa03b9, 0xc0526c3a, 0xf7866870, 0x9d46d804, 0x158ebf64, 0x7bd534c5,
  6426. 0xd80cf202, 0x410ee80f, 0x79419915, 0x74a844ae, 0x94119881, 0xcbbcc0fc, 0xa263d471, 0x013d0269, 0x67f6a0f8, 0x3e4474d0, 0xd1e50cb5, 0x56fd0e60,
  6427. 0xc4c0fd4c, 0x940629ff, 0xe18a7a16, 0xcca0330f, 0xb8ed50b7, 0x6935778b, 0x3735c791, 0x3909eb94, 0x0be36620, 0x0ac0d7aa, 0xefe942c9, 0xf0092727,
  6428. 0x5c020ee2, 0x0246da53, 0xa24be8bc, 0xa891ab94, 0xd012c7e2, 0x9c115954, 0xde0dac8e, 0x555dc022, 0x59e84f77, 0xbed2cf80, 0xe9af2cda, 0x4b600716,
  6429. 0x8955bd80, 0x7098c3f3, 0x25a8466a, 0x4ddbf26a, 0x5f554753, 0xf4890f28, 0x886a27ab, 0x54a00413, 0x0a157ca9, 0x52909a80, 0x7122a312, 0x0024a75c,
  6430. 0xe6d72935, 0xecde29cf, 0x025de009, 0x7995a6aa, 0x4a180491, 0x013df0d8, 0xe009edba, 0xd40019dc, 0x45b36b2a, 0x0122eb0d, 0x6e80a79f, 0x746590f5,
  6431. 0xd1a6dd49, 0xc05954b6, 0x83d4b957, 0xa00fe5b1, 0x59695ad7, 0xcff8433d, 0x44a0f340, 0xdd226c73, 0x5537f08c, 0xe1e89c32, 0x431056af, 0x233eb000,
  6432. 0x60773f40, 0xed7e490a, 0xc160091f, 0x12829db5, 0x43fbe6cf, 0x0a6b26c2, 0xd5f0f35a, 0xfc09fda8, 0x73525f8c, 0x2ea38cf9, 0x32bc410b, 0x94a60a22,
  6433. 0x1f62a42b, 0x5f290034, 0x07beaa91, 0x1e8ccb40, 0x17d6b0f9, 0xa2a017c9, 0x4c79a610, 0xa1de6525, 0xe975029f, 0xe063585f, 0x6246cfbb, 0x04acad44,
  6434. 0xe6a05138, 0xd03d8434, 0xc9950013, 0x5d4c809e, 0xfd26932d, 0x739213ac, 0xe260d8ef, 0xe4164617, 0x16fc60aa, 0x1d0b21e7, 0x445004b4, 0x13fd1b59,
  6435. 0x56b0f804, 0xaa936a3a, 0x335459c1, 0xb37f8caa, 0x06b68e03, 0x14d5eb01, 0x8300c78c, 0x9674792a, 0x20ba791b, 0x4d88024d, 0xef747354, 0x451e673e,
  6436. 0xc4dafc9a, 0xe53b9cd1, 0x32b4011a, 0x3d702c0f, 0x09bc0b40, 0x220d277d, 0x47eb7809, 0x8a946500, 0x7a28c4bd, 0x96e00f99, 0xc04365da, 0x05edcf46,
  6437. 0x7dee2c27, 0xe6020b7f, 0x159ecedf, 0xcbdb00ff, 0x516bb9e3, 0xd0716161, 0xeba75956, 0xf17fc22b, 0x5c578beb, 0xfe474a09, 0xc1750a87, 0xe384c189,
  6438. 0x5df54e26, 0xa6f76b79, 0xd4b172be, 0x3e8d5ceb, 0x832d90ec, 0x180368e7, 0x354c724d, 0x1a8b1412, 0x8de07be9, 0xaf009efe, 0x4616c621, 0x2860eb01,
  6439. 0x244f1404, 0xc3de724b, 0x6497a802, 0xab2f4419, 0x4e02910d, 0xe3ecf410, 0x7a6404a8, 0x8c72b112, 0xde5bc706, 0xd4f8ffe9, 0x50176344, 0x7b49fe7d,
  6440. 0x02c1d88c, 0x25634a40, 0x194804f7, 0x03b76d84, 0x392bde58, 0xdeebad27, 0xc160c021, 0xa97a72db, 0xa8040b83, 0x78804f3e, 0x046b9433, 0x178cc824,
  6441. 0x62800897, 0x7010370b, 0x21cfe7e4, 0x8053ec40, 0xf9d60526, 0xae9d353f, 0x069b40c7, 0x80496f14, 0x57e682b3, 0x6e0273e0, 0x974e2e28, 0x60ab7c3d,
  6442. 0x2025ba33, 0x507b3a8c, 0x12b70173, 0xd095c400, 0xee012d96, 0x6e194c9a, 0xe5933f89, 0x43b70102, 0xf30306aa, 0xc5802189, 0x53c077c3, 0x86029009,
  6443. 0xa0c1e780, 0xa4c04c1f, 0x93dbd580, 0xf8149809, 0x06021893, 0x3060c183, 0x83060c18, 0x183060c1, 0xc183060c, 0x0c183060, 0x60c18306, 0xfe0c1830,
  6444. 0x0cb69501, 0x7a40d9df, 0x000000dd, 0x4e454900, 0x6042ae44, 0x00000082,
  6445. };
  6446. static const unsigned int proggy_clean_13_fnt_size = 4647;
  6447. static const unsigned int proggy_clean_13_fnt_data[4648/4] =
  6448. {
  6449. 0x03464d42, 0x00001a01, 0x40000d00, 0x01006400, 0x00000000, 0x50000101, 0x67676f72, 0x656c4379, 0x02006e61, 0x0000000f, 0x000a000d, 0x00800100,
  6450. 0x01000001, 0x03000000, 0x00000016, 0x676f7270, 0x635f7967, 0x6e61656c, 0x5f33315f, 0x6e702e30, 0xd0040067, 0x00000011, 0x2e000000, 0x07000e00,
  6451. 0x00000d00, 0x07000000, 0x010f0000, 0x36000000, 0x05003800, 0x01000d00, 0x07000000, 0x020f0000, 0x86000000, 0x07000e00, 0x00000d00, 0x07000000,
  6452. 0x030f0000, 0x07000000, 0x06001c00, 0x01000d00, 0x07000000, 0x040f0000, 0x15000000, 0x06001c00, 0x01000d00, 0x07000000, 0x050f0000, 0x23000000,
  6453. 0x06001c00, 0x01000d00, 0x07000000, 0x060f0000, 0x31000000, 0x06001c00, 0x01000d00, 0x07000000, 0x070f0000, 0xfc000000, 0x03003800, 0x02000d00,
  6454. 0x07000000, 0x080f0000, 0x54000000, 0x05003800, 0x01000d00, 0x07000000, 0x090f0000, 0x4d000000, 0x06001c00, 0x01000d00, 0x07000000, 0x0a0f0000,
  6455. 0xa8000000, 0x06001c00, 0x01000d00, 0x07000000, 0x0b0f0000, 0x6a000000, 0x04004600, 0x00000d00, 0x07000000, 0x0c0f0000, 0x74000000, 0x04004600,
  6456. 0x00000d00, 0x07000000, 0x0d0f0000, 0x88000000, 0x04004600, 0x03000d00, 0x07000000, 0x0e0f0000, 0x65000000, 0x04004600, 0x03000d00, 0x07000000,
  6457. 0x0f0f0000, 0x36000000, 0x07000e00, 0x00000d00, 0x07000000, 0x100f0000, 0x5a000000, 0x05003800, 0x00000d00, 0x07000000, 0x110f0000, 0x60000000,
  6458. 0x05003800, 0x00000d00, 0x07000000, 0x120f0000, 0xe4000000, 0x03004600, 0x01000d00, 0x07000000, 0x130f0000, 0xe0000000, 0x03004600, 0x01000d00,
  6459. 0x07000000, 0x140f0000, 0x66000000, 0x05003800, 0x00000d00, 0x07000000, 0x150f0000, 0x6c000000, 0x05003800, 0x00000d00, 0x07000000, 0x160f0000,
  6460. 0x72000000, 0x05003800, 0x00000d00, 0x07000000, 0x170f0000, 0xd8000000, 0x03004600, 0x00000d00, 0x07000000, 0x180f0000, 0xcc000000, 0x03004600,
  6461. 0x01000d00, 0x07000000, 0x190f0000, 0xc8000000, 0x03004600, 0x02000d00, 0x07000000, 0x1a0f0000, 0x78000000, 0x05003800, 0x00000d00, 0x07000000,
  6462. 0x1b0f0000, 0x84000000, 0x05003800, 0x00000d00, 0x07000000, 0x1c0f0000, 0x00000000, 0x15000000, 0xf9000d00, 0x070000ff, 0x1d0f0000, 0xb0000000,
  6463. 0x15000000, 0xf9000d00, 0x070000ff, 0x1e0f0000, 0x2c000000, 0x15000000, 0xf9000d00, 0x070000ff, 0x200f0000, 0x9a000000, 0x15000000, 0xf9000d00,
  6464. 0x070000ff, 0x210f0000, 0x0c000000, 0x01005400, 0x03000d00, 0x07000000, 0x220f0000, 0xbc000000, 0x03004600, 0x02000d00, 0x07000000, 0x230f0000,
  6465. 0x4e000000, 0x07000e00, 0x00000d00, 0x07000000, 0x240f0000, 0x8a000000, 0x05003800, 0x01000d00, 0x07000000, 0x250f0000, 0xa6000000, 0x07000e00,
  6466. 0x00000d00, 0x07000000, 0x260f0000, 0xf4000000, 0x06000e00, 0x01000d00, 0x07000000, 0x270f0000, 0x06000000, 0x01005400, 0x03000d00, 0x07000000,
  6467. 0x280f0000, 0xb8000000, 0x03004600, 0x02000d00, 0x07000000, 0x290f0000, 0xb4000000, 0x03004600, 0x02000d00, 0x07000000, 0x2a0f0000, 0x90000000,
  6468. 0x05003800, 0x01000d00, 0x07000000, 0x2b0f0000, 0x96000000, 0x05003800, 0x01000d00, 0x07000000, 0x2c0f0000, 0xe8000000, 0x02004600, 0x01000d00,
  6469. 0x07000000, 0x2d0f0000, 0x9c000000, 0x05003800, 0x01000d00, 0x07000000, 0x2e0f0000, 0x04000000, 0x01005400, 0x02000d00, 0x07000000, 0x2f0f0000,
  6470. 0xa2000000, 0x05003800, 0x01000d00, 0x07000000, 0x300f0000, 0xae000000, 0x05003800, 0x01000d00, 0x07000000, 0x310f0000, 0xd8000000, 0x05003800,
  6471. 0x01000d00, 0x07000000, 0x320f0000, 0xfa000000, 0x05000000, 0x01000d00, 0x07000000, 0x330f0000, 0x31000000, 0x05002a00, 0x01000d00, 0x07000000,
  6472. 0x340f0000, 0x3f000000, 0x06001c00, 0x01000d00, 0x07000000, 0x350f0000, 0x37000000, 0x05002a00, 0x01000d00, 0x07000000, 0x360f0000, 0x3d000000,
  6473. 0x05002a00, 0x01000d00, 0x07000000, 0x370f0000, 0x43000000, 0x05002a00, 0x01000d00, 0x07000000, 0x380f0000, 0x49000000, 0x05002a00, 0x01000d00,
  6474. 0x07000000, 0x390f0000, 0x4f000000, 0x05002a00, 0x01000d00, 0x07000000, 0x3a0f0000, 0x02000000, 0x01005400, 0x03000d00, 0x07000000, 0x3b0f0000,
  6475. 0xfa000000, 0x02004600, 0x01000d00, 0x07000000, 0x3c0f0000, 0x77000000, 0x06001c00, 0x00000d00, 0x07000000, 0x3d0f0000, 0x7e000000, 0x06001c00,
  6476. 0x01000d00, 0x07000000, 0x3e0f0000, 0x85000000, 0x06001c00, 0x01000d00, 0x07000000, 0x3f0f0000, 0x55000000, 0x05002a00, 0x01000d00, 0x07000000,
  6477. 0x400f0000, 0xae000000, 0x07000e00, 0x00000d00, 0x07000000, 0x410f0000, 0xe0000000, 0x06001c00, 0x01000d00, 0x07000000, 0x420f0000, 0xa1000000,
  6478. 0x06001c00, 0x01000d00, 0x07000000, 0x430f0000, 0x5b000000, 0x05002a00, 0x01000d00, 0x07000000, 0x440f0000, 0xaf000000, 0x06001c00, 0x01000d00,
  6479. 0x07000000, 0x450f0000, 0x61000000, 0x05002a00, 0x01000d00, 0x07000000, 0x460f0000, 0x67000000, 0x05002a00, 0x01000d00, 0x07000000, 0x470f0000,
  6480. 0x38000000, 0x06001c00, 0x01000d00, 0x07000000, 0x480f0000, 0x8c000000, 0x06001c00, 0x01000d00, 0x07000000, 0x490f0000, 0xa0000000, 0x03004600,
  6481. 0x02000d00, 0x07000000, 0x4a0f0000, 0x97000000, 0x04004600, 0x01000d00, 0x07000000, 0x4b0f0000, 0xb6000000, 0x06001c00, 0x01000d00, 0x07000000,
  6482. 0x4c0f0000, 0x6d000000, 0x05002a00, 0x01000d00, 0x07000000, 0x4d0f0000, 0x1e000000, 0x07000e00, 0x00000d00, 0x07000000, 0x4e0f0000, 0x23000000,
  6483. 0x06002a00, 0x01000d00, 0x07000000, 0x4f0f0000, 0xed000000, 0x06000e00, 0x01000d00, 0x07000000, 0x500f0000, 0x73000000, 0x05002a00, 0x01000d00,
  6484. 0x07000000, 0x510f0000, 0x00000000, 0x06001c00, 0x01000d00, 0x07000000, 0x520f0000, 0x0e000000, 0x06001c00, 0x01000d00, 0x07000000, 0x530f0000,
  6485. 0x1c000000, 0x06001c00, 0x01000d00, 0x07000000, 0x540f0000, 0x66000000, 0x07000e00, 0x00000d00, 0x07000000, 0x550f0000, 0x2a000000, 0x06001c00,
  6486. 0x01000d00, 0x07000000, 0x560f0000, 0x6e000000, 0x07000e00, 0x00000d00, 0x07000000, 0x570f0000, 0x76000000, 0x07000e00, 0x00000d00, 0x07000000,
  6487. 0x580f0000, 0x46000000, 0x06001c00, 0x01000d00, 0x07000000, 0x590f0000, 0x7e000000, 0x07000e00, 0x00000d00, 0x07000000, 0x5a0f0000, 0x54000000,
  6488. 0x06001c00, 0x01000d00, 0x07000000, 0x5b0f0000, 0x9c000000, 0x03004600, 0x02000d00, 0x07000000, 0x5c0f0000, 0x79000000, 0x05002a00, 0x01000d00,
  6489. 0x07000000, 0x5d0f0000, 0xdc000000, 0x03004600, 0x02000d00, 0x07000000, 0x5e0f0000, 0x7f000000, 0x05002a00, 0x01000d00, 0x07000000, 0x5f0f0000,
  6490. 0xc6000000, 0x07000e00, 0x00000d00, 0x07000000, 0x600f0000, 0xfd000000, 0x02004600, 0x02000d00, 0x07000000, 0x610f0000, 0x85000000, 0x05002a00,
  6491. 0x01000d00, 0x07000000, 0x620f0000, 0x8b000000, 0x05002a00, 0x01000d00, 0x07000000, 0x630f0000, 0x91000000, 0x05002a00, 0x01000d00, 0x07000000,
  6492. 0x640f0000, 0x97000000, 0x05002a00, 0x01000d00, 0x07000000, 0x650f0000, 0x9d000000, 0x05002a00, 0x01000d00, 0x07000000, 0x660f0000, 0xa3000000,
  6493. 0x05002a00, 0x01000d00, 0x07000000, 0x670f0000, 0xa9000000, 0x05002a00, 0x01000d00, 0x07000000, 0x680f0000, 0xaf000000, 0x05002a00, 0x01000d00,
  6494. 0x07000000, 0x690f0000, 0xee000000, 0x02004600, 0x02000d00, 0x07000000, 0x6a0f0000, 0x92000000, 0x04004600, 0x01000d00, 0x07000000, 0x6b0f0000,
  6495. 0xb5000000, 0x05002a00, 0x01000d00, 0x07000000, 0x6c0f0000, 0xfd000000, 0x02002a00, 0x02000d00, 0x07000000, 0x6d0f0000, 0x8e000000, 0x07000e00,
  6496. 0x00000d00, 0x07000000, 0x6e0f0000, 0xbb000000, 0x05002a00, 0x01000d00, 0x07000000, 0x6f0f0000, 0xc1000000, 0x05002a00, 0x01000d00, 0x07000000,
  6497. 0x700f0000, 0xc7000000, 0x05002a00, 0x01000d00, 0x07000000, 0x710f0000, 0xcd000000, 0x05002a00, 0x01000d00, 0x07000000, 0x720f0000, 0xd3000000,
  6498. 0x05002a00, 0x01000d00, 0x07000000, 0x730f0000, 0xd9000000, 0x05002a00, 0x01000d00, 0x07000000, 0x740f0000, 0x7e000000, 0x04004600, 0x02000d00,
  6499. 0x07000000, 0x750f0000, 0xdf000000, 0x05002a00, 0x01000d00, 0x07000000, 0x760f0000, 0xe5000000, 0x05002a00, 0x01000d00, 0x07000000, 0x770f0000,
  6500. 0xbe000000, 0x07000e00, 0x00000d00, 0x07000000, 0x780f0000, 0xeb000000, 0x05002a00, 0x01000d00, 0x07000000, 0x790f0000, 0xf1000000, 0x05002a00,
  6501. 0x01000d00, 0x07000000, 0x7a0f0000, 0xf7000000, 0x05002a00, 0x01000d00, 0x07000000, 0x7b0f0000, 0x00000000, 0x05003800, 0x01000d00, 0x07000000,
  6502. 0x7c0f0000, 0x00000000, 0x01005400, 0x03000d00, 0x07000000, 0x7d0f0000, 0x06000000, 0x05003800, 0x01000d00, 0x07000000, 0x7e0f0000, 0x16000000,
  6503. 0x07000e00, 0x00000d00, 0x07000000, 0x7f0f0000, 0x58000000, 0x15000000, 0xf9000d00, 0x070000ff, 0x810f0000, 0x16000000, 0x15000000, 0xf9000d00,
  6504. 0x070000ff, 0x8d0f0000, 0x00000000, 0x15000e00, 0xf9000d00, 0x070000ff, 0x8f0f0000, 0xc6000000, 0x15000000, 0xf9000d00, 0x070000ff, 0x900f0000,
  6505. 0x6e000000, 0x15000000, 0xf9000d00, 0x070000ff, 0x9d0f0000, 0x84000000, 0x15000000, 0xf9000d00, 0x070000ff, 0xa00f0000, 0xdc000000, 0x15000000,
  6506. 0xf9000d00, 0x070000ff, 0xa10f0000, 0x0a000000, 0x01005400, 0x03000d00, 0x07000000, 0xa20f0000, 0x0c000000, 0x05003800, 0x01000d00, 0x07000000,
  6507. 0xa30f0000, 0x12000000, 0x05003800, 0x01000d00, 0x07000000, 0xa40f0000, 0x96000000, 0x07000e00, 0x00000d00, 0x07000000, 0xa50f0000, 0x5e000000,
  6508. 0x07000e00, 0x00000d00, 0x07000000, 0xa60f0000, 0x08000000, 0x01005400, 0x03000d00, 0x07000000, 0xa70f0000, 0x18000000, 0x05003800, 0x01000d00,
  6509. 0x07000000, 0xa80f0000, 0xac000000, 0x03004600, 0x02000d00, 0x07000000, 0xa90f0000, 0x56000000, 0x07000e00, 0x00000d00, 0x07000000, 0xaa0f0000,
  6510. 0x8d000000, 0x04004600, 0x01000d00, 0x07000000, 0xab0f0000, 0x1e000000, 0x05003800, 0x01000d00, 0x07000000, 0xac0f0000, 0xfb000000, 0x04000e00,
  6511. 0x01000d00, 0x07000000, 0xad0f0000, 0x42000000, 0x15000000, 0xf9000d00, 0x070000ff, 0xae0f0000, 0x3e000000, 0x07000e00, 0x00000d00, 0x07000000,
  6512. 0xaf0f0000, 0x26000000, 0x07000e00, 0x00000d00, 0x07000000, 0xb00f0000, 0x6f000000, 0x04004600, 0x01000d00, 0x07000000, 0xb10f0000, 0x24000000,
  6513. 0x05003800, 0x01000d00, 0x07000000, 0xb20f0000, 0x79000000, 0x04004600, 0x01000d00, 0x07000000, 0xb30f0000, 0x83000000, 0x04004600, 0x01000d00,
  6514. 0x07000000, 0xb40f0000, 0xeb000000, 0x02004600, 0x03000d00, 0x07000000, 0xb50f0000, 0x46000000, 0x07000e00, 0x00000d00, 0x07000000, 0xb60f0000,
  6515. 0xe6000000, 0x06000e00, 0x01000d00, 0x07000000, 0xb70f0000, 0xc0000000, 0x03004600, 0x02000d00, 0x07000000, 0xb80f0000, 0xf7000000, 0x02004600,
  6516. 0x03000d00, 0x07000000, 0xb90f0000, 0xc4000000, 0x03004600, 0x01000d00, 0x07000000, 0xba0f0000, 0x60000000, 0x04004600, 0x01000d00, 0x07000000,
  6517. 0xbb0f0000, 0x2a000000, 0x05003800, 0x01000d00, 0x07000000, 0xbc0f0000, 0x1c000000, 0x06002a00, 0x01000d00, 0x07000000, 0xbd0f0000, 0xc4000000,
  6518. 0x06001c00, 0x01000d00, 0x07000000, 0xbe0f0000, 0x9e000000, 0x07000e00, 0x00000d00, 0x07000000, 0xbf0f0000, 0x30000000, 0x05003800, 0x01000d00,
  6519. 0x07000000, 0xc00f0000, 0x9a000000, 0x06001c00, 0x01000d00, 0x07000000, 0xc10f0000, 0x93000000, 0x06001c00, 0x01000d00, 0x07000000, 0xc20f0000,
  6520. 0x70000000, 0x06001c00, 0x01000d00, 0x07000000, 0xc30f0000, 0x69000000, 0x06001c00, 0x01000d00, 0x07000000, 0xc40f0000, 0x62000000, 0x06001c00,
  6521. 0x01000d00, 0x07000000, 0xc50f0000, 0x5b000000, 0x06001c00, 0x01000d00, 0x07000000, 0xc60f0000, 0xf2000000, 0x07000000, 0x00000d00, 0x07000000,
  6522. 0xc70f0000, 0xbd000000, 0x06001c00, 0x01000d00, 0x07000000, 0xc80f0000, 0x3c000000, 0x05003800, 0x01000d00, 0x07000000, 0xc90f0000, 0x42000000,
  6523. 0x05003800, 0x01000d00, 0x07000000, 0xca0f0000, 0x48000000, 0x05003800, 0x01000d00, 0x07000000, 0xcb0f0000, 0x4e000000, 0x05003800, 0x01000d00,
  6524. 0x07000000, 0xcc0f0000, 0xa4000000, 0x03004600, 0x02000d00, 0x07000000, 0xcd0f0000, 0xb0000000, 0x03004600, 0x02000d00, 0x07000000, 0xce0f0000,
  6525. 0xa8000000, 0x03004600, 0x02000d00, 0x07000000, 0xcf0f0000, 0xfc000000, 0x03001c00, 0x02000d00, 0x07000000, 0xd00f0000, 0xce000000, 0x07000e00,
  6526. 0x00000d00, 0x07000000, 0xd10f0000, 0xcb000000, 0x06001c00, 0x01000d00, 0x07000000, 0xd20f0000, 0xd2000000, 0x06001c00, 0x01000d00, 0x07000000,
  6527. 0xd30f0000, 0xd9000000, 0x06001c00, 0x01000d00, 0x07000000, 0xd40f0000, 0x2a000000, 0x06002a00, 0x01000d00, 0x07000000, 0xd50f0000, 0xe7000000,
  6528. 0x06001c00, 0x01000d00, 0x07000000, 0xd60f0000, 0xee000000, 0x06001c00, 0x01000d00, 0x07000000, 0xd70f0000, 0x7e000000, 0x05003800, 0x01000d00,
  6529. 0x07000000, 0xd80f0000, 0xf5000000, 0x06001c00, 0x01000d00, 0x07000000, 0xd90f0000, 0x00000000, 0x06002a00, 0x01000d00, 0x07000000, 0xda0f0000,
  6530. 0x07000000, 0x06002a00, 0x01000d00, 0x07000000, 0xdb0f0000, 0x0e000000, 0x06002a00, 0x01000d00, 0x07000000, 0xdc0f0000, 0x15000000, 0x06002a00,
  6531. 0x01000d00, 0x07000000, 0xdd0f0000, 0xd6000000, 0x07000e00, 0x00000d00, 0x07000000, 0xde0f0000, 0xa8000000, 0x05003800, 0x01000d00, 0x07000000,
  6532. 0xdf0f0000, 0xde000000, 0x07000e00, 0x00000d00, 0x07000000, 0xe00f0000, 0xb4000000, 0x05003800, 0x01000d00, 0x07000000, 0xe10f0000, 0xba000000,
  6533. 0x05003800, 0x01000d00, 0x07000000, 0xe20f0000, 0xc0000000, 0x05003800, 0x01000d00, 0x07000000, 0xe30f0000, 0xc6000000, 0x05003800, 0x01000d00,
  6534. 0x07000000, 0xe40f0000, 0xcc000000, 0x05003800, 0x01000d00, 0x07000000, 0xe50f0000, 0xd2000000, 0x05003800, 0x01000d00, 0x07000000, 0xe60f0000,
  6535. 0xb6000000, 0x07000e00, 0x00000d00, 0x07000000, 0xe70f0000, 0xde000000, 0x05003800, 0x01000d00, 0x07000000, 0xe80f0000, 0xe4000000, 0x05003800,
  6536. 0x01000d00, 0x07000000, 0xe90f0000, 0xea000000, 0x05003800, 0x01000d00, 0x07000000, 0xea0f0000, 0xf0000000, 0x05003800, 0x01000d00, 0x07000000,
  6537. 0xeb0f0000, 0xf6000000, 0x05003800, 0x01000d00, 0x07000000, 0xec0f0000, 0xf1000000, 0x02004600, 0x02000d00, 0x07000000, 0xed0f0000, 0xf4000000,
  6538. 0x02004600, 0x02000d00, 0x07000000, 0xee0f0000, 0xd0000000, 0x03004600, 0x02000d00, 0x07000000, 0xef0f0000, 0xd4000000, 0x03004600, 0x02000d00,
  6539. 0x07000000, 0xf00f0000, 0x00000000, 0x05004600, 0x01000d00, 0x07000000, 0xf10f0000, 0x06000000, 0x05004600, 0x01000d00, 0x07000000, 0xf20f0000,
  6540. 0x0c000000, 0x05004600, 0x01000d00, 0x07000000, 0xf30f0000, 0x12000000, 0x05004600, 0x01000d00, 0x07000000, 0xf40f0000, 0x18000000, 0x05004600,
  6541. 0x01000d00, 0x07000000, 0xf50f0000, 0x1e000000, 0x05004600, 0x01000d00, 0x07000000, 0xf60f0000, 0x24000000, 0x05004600, 0x01000d00, 0x07000000,
  6542. 0xf70f0000, 0x2a000000, 0x05004600, 0x01000d00, 0x07000000, 0xf80f0000, 0x30000000, 0x05004600, 0x01000d00, 0x07000000, 0xf90f0000, 0x36000000,
  6543. 0x05004600, 0x01000d00, 0x07000000, 0xfa0f0000, 0x3c000000, 0x05004600, 0x01000d00, 0x07000000, 0xfb0f0000, 0x42000000, 0x05004600, 0x01000d00,
  6544. 0x07000000, 0xfc0f0000, 0x48000000, 0x05004600, 0x01000d00, 0x07000000, 0xfd0f0000, 0x4e000000, 0x05004600, 0x01000d00, 0x07000000, 0xfe0f0000,
  6545. 0x54000000, 0x05004600, 0x01000d00, 0x07000000, 0xff0f0000, 0x5a000000, 0x05004600, 0x01000d00, 0x07000000, 0x000f0000,
  6546. };
  6547. void ImGui::GetDefaultFontData(const void** fnt_data, unsigned int* fnt_size, const void** png_data, unsigned int* png_size)
  6548. {
  6549. if (fnt_data) *fnt_data = (const void*)proggy_clean_13_fnt_data;
  6550. if (fnt_size) *fnt_size = proggy_clean_13_fnt_size;
  6551. if (png_data) *png_data = (const void*)proggy_clean_13_png_data;
  6552. if (png_size) *png_size = proggy_clean_13_png_size;
  6553. }
  6554. //-----------------------------------------------------------------------------
  6555. //---- Include imgui_user.inl at the end of imgui.cpp so you can include code that extends ImGui using its private data/functions.
  6556. #ifdef IMGUI_INCLUDE_IMGUI_USER_INL
  6557. #include "imgui_user.inl"
  6558. #endif
  6559. //-----------------------------------------------------------------------------