CHICKEN Scheme bindings to nuklear

nuklear.h 677KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771
  1. /* -*- mode: c; -*-
  2. Nuklear - v1.00 - public domain
  3. no warrenty implied; use at your own risk.
  4. authored from 2015-2016 by Micha Mettke
  5. ABOUT:
  6. This is a minimal state immediate mode graphical user interface single header
  7. toolkit written in ANSI C and licensed under public domain.
  8. It was designed as a simple embeddable user interface for application and does
  9. not have any dependencies, a default renderbackend or OS window and input handling
  10. but instead provides a very modular library approach by using simple input state
  11. for input and draw commands describing primitive shapes as output.
  12. So instead of providing a layered library that tries to abstract over a number
  13. of platform and render backends it only focuses on the actual UI.
  14. VALUES:
  15. - Immediate mode graphical user interface toolkit
  16. - Single header library
  17. - Written in C89 (ANSI C)
  18. - Small codebase (~15kLOC)
  19. - Focus on portability, efficiency and simplicity
  20. - No dependencies (not even the standard library if not wanted)
  21. - Fully skinnable and customizable
  22. - Low memory footprint with total memory control if needed or wanted
  23. - UTF-8 support
  24. - No global or hidden state
  25. - Customizable library modules (you can compile and use only what you need)
  26. - Optional font baker and vertex buffer output
  27. USAGE:
  28. This library is self contained in one single header file and can be used either
  29. in header only mode or in implementation mode. The header only mode is used
  30. by default when included and allows including this header in other headers
  31. and does not contain the actual implementation.
  32. The implementation mode requires to define the preprocessor macro
  33. NK_IMPLEMENTATION in *one* .c/.cpp file before #includeing this file, e.g.:
  34. #define NK_IMPLEMENTATION
  35. #include "nuklear.h"
  36. Also optionally define the symbols listed in the section "OPTIONAL DEFINES"
  37. below in implementation mode if you want to use additional functionality
  38. or need more control over the library.
  39. FEATURES:
  40. - Absolutely no platform dependent code
  41. - Memory management control ranging from/to
  42. - Ease of use by allocating everything from the standard library
  43. - Control every byte of memory inside the library
  44. - Font handling control ranging from/to
  45. - Use your own font implementation to draw shapes/vertexes
  46. - Use this libraries internal font baking and handling API
  47. - Drawing output control ranging from/to
  48. - Simple shapes for more high level APIs which already having drawing capabilities
  49. - Hardware accessible anti-aliased vertex buffer output
  50. - Customizable colors and properties ranging from/to
  51. - Simple changes to color by filling a simple color table
  52. - Complete control with ability to use skinning to decorate widgets
  53. - Bendable UI library with widget ranging from/to
  54. - Basic widgets like buttons, checkboxes, slider, ...
  55. - Advanced widget like abstract comboboxes, contextual menus,...
  56. - Compile time configuration to only compile what you need
  57. - Subset which can be used if you do not want to link or use the standard library
  58. - Can be easily modified only update on user input instead of frame updates
  59. OPTIONAL DEFINES:
  60. NK_PRIVATE
  61. If defined declares all functions as static, so they can only be accessed
  62. for the file that creates the implementation
  63. NK_INCLUDE_FIXED_TYPES
  64. If defined it will include header <stdint.h> for fixed sized types
  65. otherwise you have to select the correct types.
  66. NK_INCLUDE_DEFAULT_ALLOCATOR
  67. if defined it will include header <stdlib.h> and provide additional functions
  68. to use this library without caring for memory allocation control and therefore
  69. ease memory management.
  70. IMPORTANT: this adds the standard library with malloc and free so don't define
  71. if you don't want to link to the standard library!
  72. NK_INCLUDE_STANDARD_IO
  73. if defined it will include header <stdio.h> and <stdarg.h> and provide
  74. additional functions depending on file loading and variable arguments
  75. IMPORTANT: this adds the standard library with fopen, fclose,...
  76. as well as va_list,... so don't define this
  77. if you don't want to link to the standard library!
  78. NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  79. Defining this adds a vertex draw command list backend to this
  80. library, which allows you to convert queue commands into vertex draw commands.
  81. This is mainly if you need a hardware accessible format for OpenGL, DirectX,
  82. Vulkan, Metal,...
  83. NK_INCLUDE_FONT_BAKING
  84. Defining this adds the `stb_truetype` and `stb_rect_pack` implementation
  85. to this library and provides a default font for font loading and rendering.
  86. If you already have font handling or do not want to use this font handler
  87. you don't have to define it.
  88. NK_INCLUDE_DEFAULT_FONT
  89. Defining this adds the default font: ProggyClean.ttf font into this library
  90. which can be loaded into a font atlas and allows using this library without
  91. having a truetype font
  92. IMPORTANT: enabling this adds ~12kb to global stack memory
  93. NK_INCLUDE_COMMAND_USERDATA
  94. Defining this adds a userdata pointer into each command. Can be useful for
  95. example if you want to provide custom shader depending on the used widget.
  96. Can be combined with the style structures.
  97. NK_BUTTON_TRIGGER_ON_RELEASE
  98. Different platforms require button clicks occuring either on buttons being
  99. pressed (up to down) or released (down to up).
  100. By default this library will react on buttons being pressed, but if you
  101. define this it will only trigger if a button is released.
  102. NK_ASSERT
  103. If you don't define this, nuklear will use <assert.h> with assert().
  104. IMPORTANT: it also adds the standard library so define to nothing of not wanted!
  105. NK_BUFFER_DEFAULT_INITIAL_SIZE
  106. Initial buffer size allocated by all buffers while using the default allocator
  107. functions included by defining NK_INCLUDE_DEFAULT_ALLOCATOR. If you don't
  108. want to allocate the default 4k memory then redefine it.
  109. NK_MAX_NUMBER_BUFFER
  110. Maximum buffer size for the conversion buffer between float and string
  111. Under normal circumstances this should be more than sufficient.
  112. NK_INPUT_MAX
  113. Defines the max number of bytes which can be added as text input in one frame.
  114. Under normal circumstances this should be more than sufficient.
  115. NK_MEMSET
  116. You can define this to 'memset' or your own memset implementation
  117. replacement. If not nuklear will use its own version.
  118. NK_MEMCOPY
  119. You can define this to 'memcpy' or your own memcpy implementation
  120. replacement. If not nuklear will use its own version.
  121. NK_SQRT
  122. You can define this to 'sqrt' or your own sqrt implementation
  123. replacement. If not nuklear will use its own slow and not highly
  124. accurate version.
  125. NK_SIN
  126. You can define this to 'sinf' or your own sine implementation
  127. replacement. If not nuklear will use its own approximation implementation.
  128. NK_COS
  129. You can define this to 'cosf' or your own cosine implementation
  130. replacement. If not nuklear will use its own approximation implementation.
  131. NK_BYTE
  132. NK_INT16
  133. NK_UINT16
  134. NK_INT32
  135. NK_UINT32
  136. NK_SIZE_TYPE
  137. NK_POINTER_TYPE
  138. If you compile without NK_USE_FIXED_TYPE then a number of standard types
  139. will be selected and compile time validated. If they are incorrect you can
  140. define the correct types.
  141. CREDITS:
  142. Developed by Micha Mettke and every direct or indirect contributor to the GitHub.
  143. Embeds stb_texedit, stb_truetype and stb_rectpack by Sean Barret (public domain)
  144. Embeds ProggyClean.ttf font by Tristan Grimmer (MIT license).
  145. Big thank you to Omar Cornut (ocornut@github) for his imgui library and
  146. giving me the inspiration for this library, Casey Muratori for handmade hero
  147. and his original immediate mode graphical user interface idea and Sean
  148. Barret for his amazing single header libraries which restored by faith
  149. in libraries and brought me to create some of my own.
  150. LICENSE:
  151. This software is dual-licensed to the public domain and under the following
  152. license: you are granted a perpetual, irrevocable license to copy, modify,
  153. publish and distribute this file as you see fit.
  154. */
  155. #ifndef NK_H_
  156. #define NK_H_
  157. #ifdef __cplusplus
  158. extern "C" {
  159. #endif
  160. /*
  161. * ==============================================================
  162. *
  163. * CONSTANTS
  164. *
  165. * ===============================================================
  166. */
  167. #define NK_UTF_INVALID 0xFFFD /* internal invalid utf8 rune */
  168. #define NK_UTF_SIZE 4 /* describes the number of bytes a glyph consists of*/
  169. #ifndef NK_INPUT_MAX
  170. #define NK_INPUT_MAX 16
  171. #endif
  172. #ifndef NK_MAX_NUMBER_BUFFER
  173. #define NK_MAX_NUMBER_BUFFER 64
  174. #endif
  175. /*
  176. * ===============================================================
  177. *
  178. * BASIC
  179. *
  180. * ===============================================================
  181. */
  182. #ifdef NK_INCLUDE_FIXED_TYPES
  183. #include <stdint.h>
  184. typedef int16_t nk_short;
  185. typedef uint16_t nk_ushort;
  186. typedef int32_t nk_int;
  187. typedef uint32_t nk_uint;
  188. typedef uint32_t nk_hash;
  189. typedef uintptr_t nk_size;
  190. typedef uintptr_t nk_ptr;
  191. typedef uint32_t nk_flags;
  192. typedef uint32_t nk_rune;
  193. typedef uint8_t nk_byte;
  194. #else
  195. #ifndef NK_BYTE
  196. typedef unsigned char nk_byte;
  197. #else
  198. typedef NK_BYTE nk_byte;
  199. #endif
  200. #ifndef NK_INT16
  201. typedef short nk_short;
  202. #else
  203. typedef NK_INT16 nk_short;
  204. #endif
  205. #ifndef NK_UINT16
  206. typedef unsigned short nk_ushort;
  207. #else
  208. typedef NK_UINT16 nk_ushort;
  209. #endif
  210. #ifndef NK_INT32
  211. typedef int nk_int;
  212. #else
  213. typedef NK_INT32 nk_int;
  214. #endif
  215. #ifndef NK_UINT32
  216. typedef unsigned int nk_uint;
  217. #else
  218. typedef NK_UINT32 nk_uint;
  219. #endif
  220. #ifndef NK_SIZE_TYPE
  221. typedef unsigned long nk_size;
  222. #else
  223. typedef NK_SIZE_TYPE nk_size;
  224. #endif
  225. #ifndef NK_POINTER_TYPE
  226. typedef unsigned long nk_ptr;
  227. #else
  228. typedef NK_POINTER_TYPE nk_ptr;
  229. #endif
  230. typedef NK_UINT32 nk_hash;
  231. typedef unsigned int nk_flags;
  232. typedef nk_uint nk_rune;
  233. typedef unsigned char nk_byte;
  234. #endif
  235. #ifdef NK_PRIVATE
  236. #define NK_API static
  237. #else
  238. #define NK_API extern
  239. #endif
  240. #define NK_INTERN static
  241. #define NK_STORAGE static
  242. #define NK_GLOBAL static
  243. /* ============================================================================
  244. *
  245. * API
  246. *
  247. * =========================================================================== */
  248. #define NK_UNDEFINED (-1.0f)
  249. #define NK_FLAG(x) (1 << (x))
  250. #define NK_STRINGIFY(x) #x
  251. #define NK_LINE_STR(x) NK_STRINGIFY(x)
  252. #define NK_FILE_LINE __FILE__":"NK_LINE_STR(__LINE__)
  253. struct nk_buffer;
  254. struct nk_allocator;
  255. struct nk_command_buffer;
  256. struct nk_draw_command;
  257. struct nk_convert_config;
  258. struct nk_text_edit;
  259. struct nk_draw_list;
  260. struct nk_user_font;
  261. struct nk_panel;
  262. struct nk_context;
  263. enum {nk_false, nk_true};
  264. struct nk_color {nk_byte r,g,b,a;};
  265. struct nk_vec2 {float x,y;};
  266. struct nk_vec2i {short x, y;};
  267. struct nk_rect {float x,y,w,h;};
  268. struct nk_recti {short x,y,w,h;};
  269. typedef char nk_glyph[NK_UTF_SIZE];
  270. typedef union {void *ptr; int id;} nk_handle;
  271. struct nk_image {nk_handle handle;unsigned short w,h;unsigned short region[4];};
  272. struct nk_scroll {unsigned short x, y;};
  273. enum nk_heading {NK_UP, NK_RIGHT, NK_DOWN, NK_LEFT};
  274. typedef int(*nk_filter)(const struct nk_text_edit*, nk_rune unicode);
  275. typedef void(*nk_paste_f)(nk_handle, struct nk_text_edit*);
  276. typedef void(*nk_copy_f)(nk_handle, const char*, int len);
  277. enum nk_button_behavior {NK_BUTTON_DEFAULT, NK_BUTTON_REPEATER};
  278. enum nk_modify {NK_FIXED=nk_false, NK_MODIFIABLE=nk_true};
  279. enum nk_orientation {NK_VERTICAL, NK_HORIZONTAL};
  280. enum nk_collapse_states {NK_MINIMIZED=nk_false, NK_MAXIMIZED = nk_true};
  281. enum nk_show_states {NK_HIDDEN=nk_false, NK_SHOWN=nk_true};
  282. enum nk_chart_type {NK_CHART_LINES, NK_CHART_COLUMN, NK_CHART_MAX};
  283. enum nk_chart_event {NK_CHART_HOVERING = 0x01, NK_CHART_CLICKED = 0x02};
  284. enum nk_color_format {NK_RGB, NK_RGBA};
  285. enum nk_popup_type {NK_POPUP_STATIC, NK_POPUP_DYNAMIC};
  286. enum nk_layout_format {NK_DYNAMIC, NK_STATIC};
  287. enum nk_tree_type {NK_TREE_NODE, NK_TREE_TAB};
  288. enum nk_anti_aliasing {NK_ANTI_ALIASING_OFF, NK_ANTI_ALIASING_ON};
  289. struct nk_allocator {
  290. nk_handle userdata;
  291. void*(*alloc)(nk_handle, void *old, nk_size);
  292. void(*free)(nk_handle, void*);
  293. };
  294. struct nk_draw_null_texture {
  295. nk_handle texture;/* texture handle to a texture with a white pixel */
  296. struct nk_vec2 uv; /* coordinates to a white pixel in the texture */
  297. };
  298. struct nk_convert_config {
  299. float global_alpha; /* global alpha value */
  300. enum nk_anti_aliasing line_AA; /* line anti-aliasing flag can be turned off if you are tight on memory */
  301. enum nk_anti_aliasing shape_AA; /* shape anti-aliasing flag can be turned off if you are tight on memory */
  302. unsigned int circle_segment_count; /* number of segments used for circles: default to 22 */
  303. unsigned int arc_segment_count; /* number of segments used for arcs: default to 22 */
  304. unsigned int curve_segment_count; /* number of segments used for curves: default to 22 */
  305. struct nk_draw_null_texture null; /* handle to texture with a white pixel for shape drawing */
  306. };
  307. enum nk_symbol_type {
  308. NK_SYMBOL_NONE,
  309. NK_SYMBOL_X,
  310. NK_SYMBOL_UNDERSCORE,
  311. NK_SYMBOL_CIRCLE,
  312. NK_SYMBOL_CIRCLE_FILLED,
  313. NK_SYMBOL_RECT,
  314. NK_SYMBOL_RECT_FILLED,
  315. NK_SYMBOL_TRIANGLE_UP,
  316. NK_SYMBOL_TRIANGLE_DOWN,
  317. NK_SYMBOL_TRIANGLE_LEFT,
  318. NK_SYMBOL_TRIANGLE_RIGHT,
  319. NK_SYMBOL_PLUS,
  320. NK_SYMBOL_MINUS,
  321. NK_SYMBOL_MAX
  322. };
  323. enum nk_keys {
  324. NK_KEY_NONE,
  325. NK_KEY_SHIFT,
  326. NK_KEY_CTRL,
  327. NK_KEY_DEL,
  328. NK_KEY_ENTER,
  329. NK_KEY_TAB,
  330. NK_KEY_BACKSPACE,
  331. NK_KEY_COPY,
  332. NK_KEY_CUT,
  333. NK_KEY_PASTE,
  334. NK_KEY_UP,
  335. NK_KEY_DOWN,
  336. NK_KEY_LEFT,
  337. NK_KEY_RIGHT,
  338. NK_KEY_TEXT_INSERT_MODE,
  339. NK_KEY_TEXT_REPLACE_MODE,
  340. NK_KEY_TEXT_RESET_MODE,
  341. NK_KEY_TEXT_LINE_START,
  342. NK_KEY_TEXT_LINE_END,
  343. NK_KEY_TEXT_START,
  344. NK_KEY_TEXT_END,
  345. NK_KEY_TEXT_UNDO,
  346. NK_KEY_TEXT_REDO,
  347. NK_KEY_TEXT_WORD_LEFT,
  348. NK_KEY_TEXT_WORD_RIGHT,
  349. NK_KEY_MAX
  350. };
  351. enum nk_buttons {
  352. NK_BUTTON_LEFT,
  353. NK_BUTTON_MIDDLE,
  354. NK_BUTTON_RIGHT,
  355. NK_BUTTON_MAX
  356. };
  357. enum nk_style_colors {
  358. NK_COLOR_TEXT,
  359. NK_COLOR_WINDOW,
  360. NK_COLOR_HEADER,
  361. NK_COLOR_BORDER,
  362. NK_COLOR_BUTTON,
  363. NK_COLOR_BUTTON_HOVER,
  364. NK_COLOR_BUTTON_ACTIVE,
  365. NK_COLOR_TOGGLE,
  366. NK_COLOR_TOGGLE_HOVER,
  367. NK_COLOR_TOGGLE_CURSOR,
  368. NK_COLOR_SELECT,
  369. NK_COLOR_SELECT_ACTIVE,
  370. NK_COLOR_SLIDER,
  371. NK_COLOR_SLIDER_CURSOR,
  372. NK_COLOR_SLIDER_CURSOR_HOVER,
  373. NK_COLOR_SLIDER_CURSOR_ACTIVE,
  374. NK_COLOR_PROPERTY,
  375. NK_COLOR_EDIT,
  376. NK_COLOR_EDIT_CURSOR,
  377. NK_COLOR_COMBO,
  378. NK_COLOR_CHART,
  379. NK_COLOR_CHART_COLOR,
  380. NK_COLOR_CHART_COLOR_HIGHLIGHT,
  381. NK_COLOR_SCROLLBAR,
  382. NK_COLOR_SCROLLBAR_CURSOR,
  383. NK_COLOR_SCROLLBAR_CURSOR_HOVER,
  384. NK_COLOR_SCROLLBAR_CURSOR_ACTIVE,
  385. NK_COLOR_TAB_HEADER,
  386. NK_COLOR_COUNT
  387. };
  388. enum nk_widget_layout_states {
  389. NK_WIDGET_INVALID, /* The widget cannot be seen and is completely out of view */
  390. NK_WIDGET_VALID, /* The widget is completely inside the window and can be updated and drawn */
  391. NK_WIDGET_ROM /* The widget is partially visible and cannot be updated */
  392. };
  393. /* widget states */
  394. enum nk_widget_states {
  395. NK_WIDGET_STATE_MODIFIED = NK_FLAG(1),
  396. NK_WIDGET_STATE_INACTIVE = NK_FLAG(2), /* widget is neither active nor hovered */
  397. NK_WIDGET_STATE_ENTERED = NK_FLAG(3), /* widget has been hovered on the current frame */
  398. NK_WIDGET_STATE_HOVER = NK_FLAG(4), /* widget is being hovered */
  399. NK_WIDGET_STATE_ACTIVED = NK_FLAG(5),/* widget is currently activated */
  400. NK_WIDGET_STATE_LEFT = NK_FLAG(6), /* widget is from this frame on not hovered anymore */
  401. NK_WIDGET_STATE_HOVERED = NK_WIDGET_STATE_HOVER|NK_WIDGET_STATE_MODIFIED, /* widget is being hovered */
  402. NK_WIDGET_STATE_ACTIVE = NK_WIDGET_STATE_ACTIVED|NK_WIDGET_STATE_MODIFIED /* widget is currently activated */
  403. };
  404. /* text alignment */
  405. enum nk_text_align {
  406. NK_TEXT_ALIGN_LEFT = 0x01,
  407. NK_TEXT_ALIGN_CENTERED = 0x02,
  408. NK_TEXT_ALIGN_RIGHT = 0x04,
  409. NK_TEXT_ALIGN_TOP = 0x08,
  410. NK_TEXT_ALIGN_MIDDLE = 0x10,
  411. NK_TEXT_ALIGN_BOTTOM = 0x20
  412. };
  413. enum nk_text_alignment {
  414. NK_TEXT_LEFT = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_LEFT,
  415. NK_TEXT_CENTERED = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_CENTERED,
  416. NK_TEXT_RIGHT = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_RIGHT
  417. };
  418. enum nk_edit_flags {
  419. NK_EDIT_DEFAULT = 0,
  420. NK_EDIT_READ_ONLY = NK_FLAG(0),
  421. NK_EDIT_AUTO_SELECT = NK_FLAG(1),
  422. NK_EDIT_SIG_ENTER = NK_FLAG(2),
  423. NK_EDIT_ALLOW_TAB = NK_FLAG(3),
  424. NK_EDIT_NO_CURSOR = NK_FLAG(4),
  425. NK_EDIT_SELECTABLE = NK_FLAG(5),
  426. NK_EDIT_CLIPBOARD = NK_FLAG(6),
  427. NK_EDIT_CTRL_ENTER_NEWLINE = NK_FLAG(7),
  428. NK_EDIT_NO_HORIZONTAL_SCROLL = NK_FLAG(8),
  429. NK_EDIT_ALWAYS_INSERT_MODE = NK_FLAG(9),
  430. NK_EDIT_MULTILINE = NK_FLAG(11)
  431. };
  432. enum nk_edit_types {
  433. NK_EDIT_SIMPLE = NK_EDIT_ALWAYS_INSERT_MODE,
  434. NK_EDIT_FIELD = NK_EDIT_SIMPLE|NK_EDIT_SELECTABLE,
  435. NK_EDIT_BOX = NK_EDIT_ALWAYS_INSERT_MODE| NK_EDIT_SELECTABLE|
  436. NK_EDIT_MULTILINE|NK_EDIT_ALLOW_TAB,
  437. NK_EDIT_EDITOR = NK_EDIT_SELECTABLE|NK_EDIT_MULTILINE|NK_EDIT_ALLOW_TAB|
  438. NK_EDIT_CLIPBOARD
  439. };
  440. enum nk_edit_events {
  441. NK_EDIT_ACTIVE = NK_FLAG(0), /* edit widget is currently being modified */
  442. NK_EDIT_INACTIVE = NK_FLAG(1), /* edit widget is not active and is not being modified */
  443. NK_EDIT_ACTIVATED = NK_FLAG(2), /* edit widget went from state inactive to state active */
  444. NK_EDIT_DEACTIVATED = NK_FLAG(3), /* edit widget went from state active to state inactive */
  445. NK_EDIT_COMMITED = NK_FLAG(4) /* edit widget has received an enter and lost focus */
  446. };
  447. enum nk_panel_flags {
  448. NK_WINDOW_BORDER = NK_FLAG(0), /* Draws a border around the window to visually separate the window * from the background */
  449. NK_WINDOW_BORDER_HEADER = NK_FLAG(1), /* Draws a border between window header and body */
  450. NK_WINDOW_MOVABLE = NK_FLAG(2), /* The movable flag indicates that a window can be moved by user input or * by dragging the window header */
  451. NK_WINDOW_SCALABLE = NK_FLAG(3), /* The scalable flag indicates that a window can be scaled by user input * by dragging a scaler icon at the button of the window */
  452. NK_WINDOW_CLOSABLE = NK_FLAG(4), /* adds a closable icon into the header */
  453. NK_WINDOW_MINIMIZABLE = NK_FLAG(5), /* adds a minimize icon into the header */
  454. NK_WINDOW_DYNAMIC = NK_FLAG(6), /* special window type growing up in height while being filled to a * certain maximum height */
  455. NK_WINDOW_NO_SCROLLBAR = NK_FLAG(7), /* Removes the scrollbar from the window */
  456. NK_WINDOW_TITLE = NK_FLAG(8) /* Forces a header at the top at the window showing the title */
  457. };
  458. /* context */
  459. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  460. NK_API int nk_init_default(struct nk_context*, const struct nk_user_font*);
  461. #endif
  462. NK_API int nk_init_fixed(struct nk_context*, void *memory, nk_size size, const struct nk_user_font*);
  463. NK_API int nk_init_custom(struct nk_context*, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font*);
  464. NK_API int nk_init(struct nk_context*, struct nk_allocator*, const struct nk_user_font*);
  465. NK_API void nk_clear(struct nk_context*);
  466. NK_API void nk_free(struct nk_context*);
  467. #ifdef NK_INCLUDE_COMMAND_USERDATA
  468. NK_API void nk_set_user_data(struct nk_context*, nk_handle handle);
  469. #endif
  470. /* window */
  471. NK_API int nk_begin(struct nk_context*, struct nk_panel*, const char *title, struct nk_rect bounds, nk_flags flags);
  472. NK_API void nk_end(struct nk_context*);
  473. NK_API struct nk_window* nk_window_find(struct nk_context *ctx, const char *name);
  474. NK_API struct nk_rect nk_window_get_bounds(const struct nk_context*);
  475. NK_API struct nk_vec2 nk_window_get_position(const struct nk_context*);
  476. NK_API struct nk_vec2 nk_window_get_size(const struct nk_context*);
  477. NK_API float nk_window_get_width(const struct nk_context*);
  478. NK_API float nk_window_get_height(const struct nk_context*);
  479. NK_API struct nk_panel* nk_window_get_panel(struct nk_context*);
  480. NK_API struct nk_rect nk_window_get_content_region(struct nk_context*);
  481. NK_API struct nk_vec2 nk_window_get_content_region_min(struct nk_context*);
  482. NK_API struct nk_vec2 nk_window_get_content_region_max(struct nk_context*);
  483. NK_API struct nk_vec2 nk_window_get_content_region_size(struct nk_context*);
  484. NK_API struct nk_command_buffer* nk_window_get_canvas(struct nk_context*);
  485. NK_API int nk_window_has_focus(const struct nk_context*);
  486. NK_API int nk_window_is_collapsed(struct nk_context*, const char*);
  487. NK_API int nk_window_is_closed(struct nk_context*, const char*);
  488. NK_API int nk_window_is_active(struct nk_context*, const char*);
  489. NK_API int nk_window_is_hovered(struct nk_context*);
  490. NK_API int nk_window_is_any_hovered(struct nk_context*);
  491. NK_API int nk_item_is_any_active(struct nk_context*);
  492. NK_API void nk_window_set_bounds(struct nk_context*, struct nk_rect);
  493. NK_API void nk_window_set_position(struct nk_context*, struct nk_vec2);
  494. NK_API void nk_window_set_size(struct nk_context*, struct nk_vec2);
  495. NK_API void nk_window_set_focus(struct nk_context*, const char *name);
  496. NK_API void nk_window_close(struct nk_context *ctx, const char *name);
  497. NK_API void nk_window_collapse(struct nk_context*, const char *name, enum nk_collapse_states);
  498. NK_API void nk_window_collapse_if(struct nk_context*, const char *name, enum nk_collapse_states, int cond);
  499. NK_API void nk_window_show(struct nk_context*, const char *name, enum nk_show_states);
  500. NK_API void nk_window_show_if(struct nk_context*, const char *name, enum nk_show_states, int cond);
  501. /* Layout */
  502. NK_API void nk_layout_row_dynamic(struct nk_context*, float height, int cols);
  503. NK_API void nk_layout_row_static(struct nk_context*, float height, int item_width, int cols);
  504. NK_API void nk_layout_row_begin(struct nk_context*, enum nk_layout_format, float row_height, int cols);
  505. NK_API void nk_layout_row_push(struct nk_context*, float value);
  506. NK_API void nk_layout_row_end(struct nk_context*);
  507. NK_API void nk_layout_row(struct nk_context*, enum nk_layout_format, float height, int cols, const float *ratio);
  508. NK_API void nk_layout_space_begin(struct nk_context*, enum nk_layout_format, float height, int widget_count);
  509. NK_API void nk_layout_space_push(struct nk_context*, struct nk_rect);
  510. NK_API void nk_layout_space_end(struct nk_context*);
  511. NK_API struct nk_rect nk_layout_space_bounds(struct nk_context*);
  512. NK_API struct nk_vec2 nk_layout_space_to_screen(struct nk_context*, struct nk_vec2);
  513. NK_API struct nk_vec2 nk_layout_space_to_local(struct nk_context*, struct nk_vec2);
  514. NK_API struct nk_rect nk_layout_space_rect_to_screen(struct nk_context*, struct nk_rect);
  515. NK_API struct nk_rect nk_layout_space_rect_to_local(struct nk_context*, struct nk_rect);
  516. /* Layout: Group */
  517. NK_API int nk_group_begin(struct nk_context*, struct nk_panel*, const char *title, nk_flags);
  518. NK_API void nk_group_end(struct nk_context*);
  519. /* Layout: Tree */
  520. #define nk_tree_push(ctx, type, title, state) nk_tree_push_hashed(ctx, type, title, state, __FILE__,nk_strlen(__FILE__),__LINE__)
  521. #define nk_tree_push_id(ctx, type, title, state, id) nk_tree_push_hashed(ctx, type, title, state, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),id)
  522. NK_API int nk_tree_push_hashed(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states initial_state, const char *hash, int len,int seed);
  523. #define nk_tree_image_push(ctx, type, img, title, state) nk_tree_image_push_hashed(ctx, type, img, title, state, __FILE__,nk_strlen(__FILE__),__LINE__)
  524. #define nk_tree_image_push_id(ctx, type, img, title, state, id) nk_tree_image_push_hashed(ctx, img, type, title, state, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),id)
  525. NK_API int nk_tree_image_push_hashed(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states initial_state, const char *hash, int len,int seed);
  526. NK_API void nk_tree_pop(struct nk_context*);
  527. /* Widgets */
  528. NK_API void nk_text(struct nk_context*, const char*, int, nk_flags);
  529. NK_API void nk_text_colored(struct nk_context*, const char*, int, nk_flags, struct nk_color);
  530. NK_API void nk_text_wrap(struct nk_context*, const char*, int);
  531. NK_API void nk_text_wrap_colored(struct nk_context*, const char*, int, struct nk_color);
  532. NK_API void nk_label(struct nk_context*, const char*, nk_flags align);
  533. NK_API void nk_label_colored(struct nk_context*, const char*, nk_flags align, struct nk_color);
  534. NK_API void nk_label_wrap(struct nk_context*, const char*);
  535. NK_API void nk_label_colored_wrap(struct nk_context*, const char*, struct nk_color);
  536. NK_API void nk_image(struct nk_context*, struct nk_image);
  537. #ifdef NK_INCLUDE_STANDARD_IO
  538. NK_API void nk_labelf(struct nk_context*, nk_flags, const char*, ...);
  539. NK_API void nk_labelf_colored(struct nk_context*, nk_flags align, struct nk_color, const char*,...);
  540. NK_API void nk_labelf_wrap(struct nk_context*, const char*,...);
  541. NK_API void nk_labelf_colored_wrap(struct nk_context*, struct nk_color, const char*,...);
  542. NK_API void nk_value_bool(struct nk_context*, const char *prefix, int);
  543. NK_API void nk_value_int(struct nk_context*, const char *prefix, int);
  544. NK_API void nk_value_uint(struct nk_context*, const char *prefix, unsigned int);
  545. NK_API void nk_value_float(struct nk_context*, const char *prefix, float);
  546. NK_API void nk_value_color_byte(struct nk_context*, const char *prefix, struct nk_color);
  547. NK_API void nk_value_color_float(struct nk_context*, const char *prefix, struct nk_color);
  548. NK_API void nk_value_color_hex(struct nk_context*, const char *prefix, struct nk_color);
  549. #endif
  550. /* Widgets: Buttons */
  551. NK_API int nk_button_text(struct nk_context *ctx, const char *title, int len, enum nk_button_behavior);
  552. NK_API int nk_button_label(struct nk_context *ctx, const char *title, enum nk_button_behavior);
  553. NK_API int nk_button_color(struct nk_context*, struct nk_color, enum nk_button_behavior);
  554. NK_API int nk_button_symbol(struct nk_context*, enum nk_symbol_type, enum nk_button_behavior);
  555. NK_API int nk_button_image(struct nk_context*, struct nk_image img, enum nk_button_behavior);
  556. NK_API int nk_button_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags text_alignment, enum nk_button_behavior);
  557. NK_API int nk_button_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment, enum nk_button_behavior);
  558. NK_API int nk_button_image_label(struct nk_context*, struct nk_image img, const char*, nk_flags text_alignment, enum nk_button_behavior);
  559. NK_API int nk_button_image_text(struct nk_context*, struct nk_image img, const char*, int, nk_flags alignment, enum nk_button_behavior);
  560. /* Widgets: Checkbox */
  561. NK_API int nk_check_label(struct nk_context*, const char*, int active);
  562. NK_API int nk_check_text(struct nk_context*, const char*, int,int active);
  563. NK_API unsigned nk_check_flags_label(struct nk_context*, const char*, unsigned int flags, unsigned int value);
  564. NK_API unsigned nk_check_flags_text(struct nk_context*, const char*, int, unsigned int flags, unsigned int value);
  565. NK_API int nk_checkbox_label(struct nk_context*, const char*, int *active);
  566. NK_API int nk_checkbox_text(struct nk_context*, const char*, int, int *active);
  567. NK_API int nk_checkbox_flags_label(struct nk_context*, const char*, unsigned int *flags, unsigned int value);
  568. NK_API int nk_checkbox_flags_text(struct nk_context*, const char*, int, unsigned int *flags, unsigned int value);
  569. /* Widgets: Radio */
  570. NK_API int nk_radio_label(struct nk_context*, const char*, int *active);
  571. NK_API int nk_radio_text(struct nk_context*, const char*, int, int *active);
  572. NK_API int nk_option_label(struct nk_context*, const char*, int active);
  573. NK_API int nk_option_text(struct nk_context*, const char*, int, int active);
  574. /* Widgets: Selectable */
  575. NK_API int nk_selectable_label(struct nk_context*, const char*, nk_flags align, int *value);
  576. NK_API int nk_selectable_text(struct nk_context*, const char*, int, nk_flags align, int *value);
  577. NK_API int nk_selectable_image_label(struct nk_context*,struct nk_image, const char*, nk_flags align, int *value);
  578. NK_API int nk_selectable_image_text(struct nk_context*,struct nk_image, const char*, int, nk_flags align, int *value);
  579. NK_API int nk_select_label(struct nk_context*, const char*, nk_flags align, int value);
  580. NK_API int nk_select_text(struct nk_context*, const char*, int, nk_flags align, int value);
  581. NK_API int nk_select_image_label(struct nk_context*, struct nk_image,const char*, nk_flags align, int value);
  582. NK_API int nk_select_image_text(struct nk_context*, struct nk_image,const char*, int, nk_flags align, int value);
  583. /* Widgets: Slider */
  584. NK_API float nk_slide_float(struct nk_context*, float min, float val, float max, float step);
  585. NK_API int nk_slide_int(struct nk_context*, int min, int val, int max, int step);
  586. NK_API int nk_slider_float(struct nk_context*, float min, float *val, float max, float step);
  587. NK_API int nk_slider_int(struct nk_context*, int min, int *val, int max, int step);
  588. /* Widgets: Progressbar */
  589. NK_API int nk_progress(struct nk_context*, nk_size *cur, nk_size max, int modifyable);
  590. NK_API nk_size nk_prog(struct nk_context*, nk_size cur, nk_size max, int modifyable);
  591. /* Widgets: Color picker */
  592. NK_API struct nk_color nk_color_picker(struct nk_context*, struct nk_color, enum nk_color_format);
  593. NK_API int nk_color_pick(struct nk_context*, struct nk_color*, enum nk_color_format);
  594. /* Widgets: Property */
  595. NK_API void nk_property_float(struct nk_context *layout, const char *name, float min, float *val, float max, float step, float inc_per_pixel);
  596. NK_API void nk_property_int(struct nk_context *layout, const char *name, int min, int *val, int max, int step, int inc_per_pixel);
  597. NK_API float nk_propertyf(struct nk_context *layout, const char *name, float min, float val, float max, float step, float inc_per_pixel);
  598. NK_API int nk_propertyi(struct nk_context *layout, const char *name, int min, int val, int max, int step, int inc_per_pixel);
  599. /* Widgets: TextEdit */
  600. NK_API nk_flags nk_edit_string(struct nk_context*, nk_flags, char *buffer, int *len, int max, nk_filter);
  601. NK_API nk_flags nk_edit_buffer(struct nk_context*, nk_flags, struct nk_text_edit*, nk_filter);
  602. /* Chart */
  603. NK_API int nk_chart_begin(struct nk_context*, enum nk_chart_type, int num, float min, float max);
  604. NK_API int nk_chart_begin_colored(struct nk_context*, enum nk_chart_type, struct nk_color, struct nk_color active, int num, float min, float max);
  605. NK_API void nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type, int count, float min_value, float max_value);
  606. NK_API void nk_chart_add_slot_colored(struct nk_context *ctx, const enum nk_chart_type, struct nk_color, struct nk_color active, int count, float min_value, float max_value);
  607. NK_API nk_flags nk_chart_push(struct nk_context*, float);
  608. NK_API nk_flags nk_chart_push_slot(struct nk_context*, float, int);
  609. NK_API void nk_chart_end(struct nk_context*);
  610. NK_API void nk_plot(struct nk_context*, enum nk_chart_type, const float *values, int count, int offset);
  611. NK_API void nk_plot_function(struct nk_context*, enum nk_chart_type, void *userdata, float(*value_getter)(void* user, int index), int count, int offset);
  612. /* Popups */
  613. NK_API int nk_popup_begin(struct nk_context*, struct nk_panel*, enum nk_popup_type, const char*, nk_flags, struct nk_rect bounds);
  614. NK_API void nk_popup_close(struct nk_context*);
  615. NK_API void nk_popup_end(struct nk_context*);
  616. /* Combobox */
  617. NK_API int nk_combo(struct nk_context*, const char **items, int count, int selected, int item_height);
  618. NK_API int nk_combo_separator(struct nk_context*, const char *items_separated_by_separator, int separator, int selected, int count, int item_height);
  619. NK_API int nk_combo_string(struct nk_context*, const char *items_separated_by_zeros, int selected, int count, int item_height);
  620. NK_API int nk_combo_callback(struct nk_context*, void(*item_getter)(void*, int, const char**), void *userdata, int selected, int count, int item_height);
  621. NK_API void nk_combobox(struct nk_context*, const char **items, int count, int *selected, int item_height);
  622. NK_API void nk_combobox_string(struct nk_context*, const char *items_separated_by_zeros, int *selected, int count, int item_height);
  623. NK_API void nk_combobox_separator(struct nk_context*, const char *items_separated_by_separator, int separator,int *selected, int count, int item_height);
  624. NK_API void nk_combobox_callback(struct nk_context*, void(*item_getter)(void*, int, const char**), void*, int *selected, int count, int item_height);
  625. /* Combobox: abstract */
  626. NK_API int nk_combo_begin_text(struct nk_context*, struct nk_panel*, const char *selected, int, int max_height);
  627. NK_API int nk_combo_begin_label(struct nk_context*, struct nk_panel*, const char *selected, int max_height);
  628. NK_API int nk_combo_begin_color(struct nk_context*, struct nk_panel*, struct nk_color color, int max_height);
  629. NK_API int nk_combo_begin_symbol(struct nk_context*, struct nk_panel*, enum nk_symbol_type, int max_height);
  630. NK_API int nk_combo_begin_symbol_label(struct nk_context*, struct nk_panel*, const char *selected, enum nk_symbol_type, int height);
  631. NK_API int nk_combo_begin_symbol_text(struct nk_context*, struct nk_panel*, const char *selected, int, enum nk_symbol_type, int height);
  632. NK_API int nk_combo_begin_image(struct nk_context*, struct nk_panel*, struct nk_image img, int max_height);
  633. NK_API int nk_combo_begin_image_label(struct nk_context*, struct nk_panel*, const char *selected, struct nk_image, int height);
  634. NK_API int nk_combo_begin_image_text(struct nk_context*, struct nk_panel*, const char *selected, int, struct nk_image, int height);
  635. NK_API int nk_combo_item_label(struct nk_context*, const char*, nk_flags alignment);
  636. NK_API int nk_combo_item_text(struct nk_context*, const char*,int, nk_flags alignment);
  637. NK_API int nk_combo_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  638. NK_API int nk_combo_item_image_text(struct nk_context*, struct nk_image, const char*, int,nk_flags alignment);
  639. NK_API int nk_combo_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  640. NK_API int nk_combo_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  641. NK_API void nk_combo_close(struct nk_context*);
  642. NK_API void nk_combo_end(struct nk_context*);
  643. /* Contextual */
  644. NK_API int nk_contextual_begin(struct nk_context*, struct nk_panel*, nk_flags, struct nk_vec2, struct nk_rect trigger_bounds);
  645. NK_API int nk_contextual_item_text(struct nk_context*, const char*, int,nk_flags align);
  646. NK_API int nk_contextual_item_label(struct nk_context*, const char*, nk_flags align);
  647. NK_API int nk_contextual_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  648. NK_API int nk_contextual_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
  649. NK_API int nk_contextual_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  650. NK_API int nk_contextual_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  651. NK_API void nk_contextual_close(struct nk_context*);
  652. NK_API void nk_contextual_end(struct nk_context*);
  653. /* Tooltip */
  654. NK_API void nk_tooltip(struct nk_context*, const char*);
  655. NK_API int nk_tooltip_begin(struct nk_context*, struct nk_panel*, float width);
  656. NK_API void nk_tooltip_end(struct nk_context*);
  657. /* Menu */
  658. NK_API void nk_menubar_begin(struct nk_context*);
  659. NK_API void nk_menubar_end(struct nk_context*);
  660. NK_API int nk_menu_begin_text(struct nk_context*, struct nk_panel*, const char*, int, nk_flags align, float width);
  661. NK_API int nk_menu_begin_label(struct nk_context*, struct nk_panel*, const char*, nk_flags align, float width);
  662. NK_API int nk_menu_begin_image(struct nk_context*, struct nk_panel*, const char*, struct nk_image, float width);
  663. NK_API int nk_menu_begin_image_text(struct nk_context*, struct nk_panel*, const char*, int,nk_flags align,struct nk_image, float width);
  664. NK_API int nk_menu_begin_image_label(struct nk_context*, struct nk_panel*, const char*, nk_flags align,struct nk_image, float width);
  665. NK_API int nk_menu_begin_symbol(struct nk_context*, struct nk_panel*, const char*, enum nk_symbol_type, float width);
  666. NK_API int nk_menu_begin_symbol_text(struct nk_context*, struct nk_panel*, const char*, int,nk_flags align,enum nk_symbol_type, float width);
  667. NK_API int nk_menu_begin_symbol_label(struct nk_context*, struct nk_panel*, const char*, nk_flags align,enum nk_symbol_type, float width);
  668. NK_API int nk_menu_item_text(struct nk_context*, const char*, int,nk_flags align);
  669. NK_API int nk_menu_item_label(struct nk_context*, const char*, nk_flags alignment);
  670. NK_API int nk_menu_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  671. NK_API int nk_menu_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
  672. NK_API int nk_menu_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  673. NK_API int nk_menu_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  674. NK_API void nk_menu_close(struct nk_context*);
  675. NK_API void nk_menu_end(struct nk_context*);
  676. /* Drawing*/
  677. #define nk_foreach(c, ctx)for((c)=nk__begin(ctx); (c)!=0; (c)=nk__next(ctx, c))
  678. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  679. NK_API void nk_convert(struct nk_context*, struct nk_buffer *cmds, struct nk_buffer *vertices, struct nk_buffer *elements, const struct nk_convert_config*);
  680. #define nk_draw_foreach(cmd,ctx, b) for((cmd)=nk__draw_begin(ctx, b); (cmd)!=0; (cmd)=nk__draw_next(cmd, b, ctx))
  681. #endif
  682. /* User Input */
  683. NK_API void nk_input_begin(struct nk_context*);
  684. NK_API void nk_input_motion(struct nk_context*, int x, int y);
  685. NK_API void nk_input_key(struct nk_context*, enum nk_keys, int down);
  686. NK_API void nk_input_button(struct nk_context*, enum nk_buttons, int x, int y, int down);
  687. NK_API void nk_input_scroll(struct nk_context*, float y);
  688. NK_API void nk_input_char(struct nk_context*, char);
  689. NK_API void nk_input_glyph(struct nk_context*, const nk_glyph);
  690. NK_API void nk_input_unicode(struct nk_context*, nk_rune);
  691. NK_API void nk_input_end(struct nk_context*);
  692. /* Style */
  693. NK_API void nk_style_default(struct nk_context*);
  694. NK_API void nk_style_from_table(struct nk_context*, const struct nk_color*);
  695. NK_API const char* nk_style_color_name(enum nk_style_colors);
  696. NK_API void nk_style_set_font(struct nk_context*, const struct nk_user_font*);
  697. /* Utilities */
  698. NK_API struct nk_rect nk_widget_bounds(struct nk_context*);
  699. NK_API struct nk_vec2 nk_widget_position(struct nk_context*);
  700. NK_API struct nk_vec2 nk_widget_size(struct nk_context*);
  701. NK_API int nk_widget_is_hovered(struct nk_context*);
  702. NK_API int nk_widget_is_mouse_clicked(struct nk_context*, enum nk_buttons);
  703. NK_API int nk_widget_has_mouse_click_down(struct nk_context*, enum nk_buttons, int down);
  704. NK_API void nk_spacing(struct nk_context*, int cols);
  705. /* base widget function */
  706. NK_API enum nk_widget_layout_states nk_widget(struct nk_rect*, const struct nk_context*);
  707. NK_API enum nk_widget_layout_states nk_widget_fitting(struct nk_rect*, struct nk_context*, struct nk_vec2);
  708. /* color (conversion user --> nuklear) */
  709. NK_API struct nk_color nk_rgb(int r, int g, int b);
  710. NK_API struct nk_color nk_rgb_iv(const int *rgb);
  711. NK_API struct nk_color nk_rgb_bv(const nk_byte* rgb);
  712. NK_API struct nk_color nk_rgb_f(float r, float g, float b);
  713. NK_API struct nk_color nk_rgb_fv(const float *rgb);
  714. NK_API struct nk_color nk_rgb_hex(const char *rgb);
  715. NK_API struct nk_color nk_rgba(int r, int g, int b, int a);
  716. NK_API struct nk_color nk_rgba_u32(nk_uint);
  717. NK_API struct nk_color nk_rgba_iv(const int *rgba);
  718. NK_API struct nk_color nk_rgba_bv(const nk_byte *rgba);
  719. NK_API struct nk_color nk_rgba_f(float r, float g, float b, float a);
  720. NK_API struct nk_color nk_rgba_fv(const float *rgba);
  721. NK_API struct nk_color nk_rgba_hex(const char *rgb);
  722. NK_API struct nk_color nk_hsv(int h, int s, int v);
  723. NK_API struct nk_color nk_hsv_iv(const int *hsv);
  724. NK_API struct nk_color nk_hsv_bv(const nk_byte *hsv);
  725. NK_API struct nk_color nk_hsv_f(float h, float s, float v);
  726. NK_API struct nk_color nk_hsv_fv(const float *hsv);
  727. NK_API struct nk_color nk_hsva(int h, int s, int v, int a);
  728. NK_API struct nk_color nk_hsva_iv(const int *hsva);
  729. NK_API struct nk_color nk_hsva_bv(const nk_byte *hsva);
  730. NK_API struct nk_color nk_hsva_f(float h, float s, float v, float a);
  731. NK_API struct nk_color nk_hsva_fv(const float *hsva);
  732. /* color (conversion nuklear --> user) */
  733. NK_API void nk_color_f(float *r, float *g, float *b, float *a, struct nk_color);
  734. NK_API void nk_color_fv(float *rgba_out, struct nk_color);
  735. NK_API nk_uint nk_color_u32(struct nk_color);
  736. NK_API void nk_color_hex_rgba(char *output, struct nk_color);
  737. NK_API void nk_color_hex_rgb(char *output, struct nk_color);
  738. NK_API void nk_color_hsv_i(int *out_h, int *out_s, int *out_v, struct nk_color);
  739. NK_API void nk_color_hsv_b(nk_byte *out_h, nk_byte *out_s, nk_byte *out_v, struct nk_color);
  740. NK_API void nk_color_hsv_iv(int *hsv_out, struct nk_color);
  741. NK_API void nk_color_hsv_bv(nk_byte *hsv_out, struct nk_color);
  742. NK_API void nk_color_hsv_f(float *out_h, float *out_s, float *out_v, struct nk_color);
  743. NK_API void nk_color_hsv_fv(float *hsv_out, struct nk_color);
  744. NK_API void nk_color_hsva_i(int *h, int *s, int *v, int *a, struct nk_color);
  745. NK_API void nk_color_hsva_b(nk_byte *h, nk_byte *s, nk_byte *v, nk_byte *a, struct nk_color);
  746. NK_API void nk_color_hsva_iv(int *hsva_out, struct nk_color);
  747. NK_API void nk_color_hsva_bv(nk_byte *hsva_out, struct nk_color);
  748. NK_API void nk_color_hsva_f(float *out_h, float *out_s, float *out_v, float *out_a, struct nk_color);
  749. NK_API void nk_color_hsva_fv(float *hsva_out, struct nk_color);
  750. /* image */
  751. NK_API nk_handle nk_handle_ptr(void*);
  752. NK_API nk_handle nk_handle_id(int);
  753. NK_API struct nk_image nk_image_ptr(void*);
  754. NK_API struct nk_image nk_image_id(int);
  755. NK_API int nk_image_is_subimage(const struct nk_image* img);
  756. NK_API struct nk_image nk_subimage_ptr(void*, unsigned short w, unsigned short h, struct nk_rect sub_region);
  757. NK_API struct nk_image nk_subimage_id(int, unsigned short w, unsigned short h, struct nk_rect sub_region);
  758. /* math */
  759. NK_API nk_hash nk_murmur_hash(const void *key, int len, nk_hash seed);
  760. NK_API void nk_triangle_from_direction(struct nk_vec2 *result, struct nk_rect r, float pad_x, float pad_y, enum nk_heading);
  761. NK_API struct nk_vec2 nk_vec2(float x, float y);
  762. NK_API struct nk_vec2 nk_vec2i(int x, int y);
  763. NK_API struct nk_vec2 nk_vec2v(const float *xy);
  764. NK_API struct nk_vec2 nk_vec2iv(const int *xy);
  765. NK_API struct nk_rect nk_get_null_rect(void);
  766. NK_API struct nk_rect nk_rect(float x, float y, float w, float h);
  767. NK_API struct nk_rect nk_recti(int x, int y, int w, int h);
  768. NK_API struct nk_rect nk_recta(struct nk_vec2 pos, struct nk_vec2 size);
  769. NK_API struct nk_rect nk_rectv(const float *xywh);
  770. NK_API struct nk_rect nk_rectiv(const int *xywh);
  771. NK_API struct nk_vec2 nk_rect_pos(struct nk_rect);
  772. NK_API struct nk_vec2 nk_rect_size(struct nk_rect);
  773. /* string*/
  774. NK_API int nk_strlen(const char *str);
  775. NK_API int nk_stricmp(const char *s1, const char *s2);
  776. NK_API int nk_stricmpn(const char *s1, const char *s2, int n);
  777. NK_API int nk_strtof(float *number, const char *buffer);
  778. NK_API int nk_strfilter(const char *text, const char *regexp);
  779. NK_API int nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score);
  780. NK_API int nk_strmatch_fuzzy_text(const char *txt, int txt_len, const char *pattern, int *out_score);
  781. #ifdef NK_INCLUDE_STANDARD_IO
  782. NK_API int nk_strfmt(char *buf, int len, const char *fmt,...);
  783. #endif
  784. /* UTF-8 */
  785. NK_API int nk_utf_decode(const char*, nk_rune*, int);
  786. NK_API int nk_utf_encode(nk_rune, char*, int);
  787. NK_API int nk_utf_len(const char*, int byte_len);
  788. NK_API const char* nk_utf_at(const char *buffer, int length, int index, nk_rune *unicode, int *len);
  789. /* ==============================================================
  790. *
  791. * MEMORY BUFFER
  792. *
  793. * ===============================================================*/
  794. /* A basic (double)-buffer with linear allocation and resetting as only
  795. freeing policy. The buffers main purpose is to control all memory management
  796. inside the GUI toolkit and still leave memory control as much as possible in
  797. the hand of the user while also making sure the library is easy to use if
  798. not as much control is needed.
  799. In general all memory inside this library can be provided from the user in
  800. three different ways.
  801. The first way and the one providing most control is by just passing a fixed
  802. size memory block. In this case all control lies in the hand of the user
  803. since he can exactly control where the memory comes from and how much memory
  804. the library should consume. Of course using the fixed size API removes the
  805. ability to automatically resize a buffer if not enough memory is provided so
  806. you have to take over the resizing. While being a fixed sized buffer sounds
  807. quite limiting, it is very effective in this library since the actual memory
  808. consumption is quite stable and has a fixed upper bound for a lot of cases.
  809. If you don't want to think about how much memory the library should allocate
  810. at all time or have a very dynamic UI with unpredictable memory consumption
  811. habits but still want control over memory allocation you can use the dynamic
  812. allocator based API. The allocator consists of two callbacks for allocating
  813. and freeing memory and optional userdata so you can plugin your own allocator.
  814. The final and easiest way can be used by defining
  815. NK_INCLUDE_DEFAULT_ALLOCATOR which uses the standard library memory
  816. allocation functions malloc and free and takes over complete control over
  817. memory in this library.
  818. */
  819. struct nk_memory_status {
  820. void *memory;
  821. unsigned int type;
  822. nk_size size;
  823. nk_size allocated;
  824. nk_size needed;
  825. nk_size calls;
  826. };
  827. enum nk_allocation_type {
  828. NK_BUFFER_FIXED,
  829. NK_BUFFER_DYNAMIC
  830. };
  831. enum nk_buffer_allocation_type {
  832. NK_BUFFER_FRONT,
  833. NK_BUFFER_BACK,
  834. NK_BUFFER_MAX
  835. };
  836. struct nk_buffer_marker {
  837. int active;
  838. nk_size offset;
  839. };
  840. struct nk_memory {void *ptr;nk_size size;};
  841. struct nk_buffer {
  842. struct nk_buffer_marker marker[NK_BUFFER_MAX];
  843. /* buffer marker to free a buffer to a certain offset */
  844. struct nk_allocator pool;
  845. /* allocator callback for dynamic buffers */
  846. enum nk_allocation_type type;
  847. /* memory management type */
  848. struct nk_memory memory;
  849. /* memory and size of the current memory block */
  850. float grow_factor;
  851. /* growing factor for dynamic memory management */
  852. nk_size allocated;
  853. /* total amount of memory allocated */
  854. nk_size needed;
  855. /* totally consumed memory given that enough memory is present */
  856. nk_size calls;
  857. /* number of allocation calls */
  858. nk_size size;
  859. /* current size of the buffer */
  860. };
  861. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  862. NK_API void nk_buffer_init_default(struct nk_buffer*);
  863. #endif
  864. NK_API void nk_buffer_init(struct nk_buffer*, const struct nk_allocator*, nk_size size);
  865. NK_API void nk_buffer_init_fixed(struct nk_buffer*, void *memory, nk_size size);
  866. NK_API void nk_buffer_info(struct nk_memory_status*, struct nk_buffer*);
  867. NK_API void nk_buffer_push(struct nk_buffer*, enum nk_buffer_allocation_type type, void *memory, nk_size size, nk_size align);
  868. NK_API void nk_buffer_mark(struct nk_buffer*, enum nk_buffer_allocation_type type);
  869. NK_API void nk_buffer_reset(struct nk_buffer*, enum nk_buffer_allocation_type type);
  870. NK_API void nk_buffer_clear(struct nk_buffer*);
  871. NK_API void nk_buffer_free(struct nk_buffer*);
  872. NK_API void *nk_buffer_memory(struct nk_buffer*);
  873. NK_API const void *nk_buffer_memory_const(const struct nk_buffer*);
  874. NK_API nk_size nk_buffer_total(struct nk_buffer*);
  875. /* ==============================================================
  876. *
  877. * STRING
  878. *
  879. * ===============================================================*/
  880. /* Basic string buffer which is only used in context with the text editor
  881. * to manage and manipulate dynamic or fixed size string content. This is _NOT_
  882. * the default string handling method.*/
  883. struct nk_str {
  884. struct nk_buffer buffer;
  885. int len; /* in codepoints/runes/glyphs */
  886. };
  887. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  888. NK_API void nk_str_init_default(struct nk_str*);
  889. #endif
  890. NK_API void nk_str_init(struct nk_str*, const struct nk_allocator*, nk_size size);
  891. NK_API void nk_str_init_fixed(struct nk_str*, void *memory, nk_size size);
  892. NK_API void nk_str_clear(struct nk_str*);
  893. NK_API void nk_str_free(struct nk_str*);
  894. NK_API int nk_str_append_text_char(struct nk_str*, const char*, int);
  895. NK_API int nk_str_append_str_char(struct nk_str*, const char*);
  896. NK_API int nk_str_append_text_utf8(struct nk_str*, const char*, int);
  897. NK_API int nk_str_append_str_utf8(struct nk_str*, const char*);
  898. NK_API int nk_str_append_text_runes(struct nk_str*, const nk_rune*, int);
  899. NK_API int nk_str_append_str_runes(struct nk_str*, const nk_rune*);
  900. NK_API int nk_str_insert_at_char(struct nk_str*, int pos, const char*, int);
  901. NK_API int nk_str_insert_at_rune(struct nk_str*, int pos, const char*, int);
  902. NK_API int nk_str_insert_text_char(struct nk_str*, int pos, const char*, int);
  903. NK_API int nk_str_insert_str_char(struct nk_str*, int pos, const char*);
  904. NK_API int nk_str_insert_text_utf8(struct nk_str*, int pos, const char*, int);
  905. NK_API int nk_str_insert_str_utf8(struct nk_str*, int pos, const char*);
  906. NK_API int nk_str_insert_text_runes(struct nk_str*, int pos, const nk_rune*, int);
  907. NK_API int nk_str_insert_str_runes(struct nk_str*, int pos, const nk_rune*);
  908. NK_API void nk_str_remove_chars(struct nk_str*, int len);
  909. NK_API void nk_str_remove_runes(struct nk_str *str, int len);
  910. NK_API void nk_str_delete_chars(struct nk_str*, int pos, int len);
  911. NK_API void nk_str_delete_runes(struct nk_str*, int pos, int len);
  912. NK_API char *nk_str_at_char(struct nk_str*, int pos);
  913. NK_API char *nk_str_at_rune(struct nk_str*, int pos, nk_rune *unicode, int *len);
  914. NK_API nk_rune nk_str_rune_at(const struct nk_str*, int pos);
  915. NK_API const char *nk_str_at_char_const(const struct nk_str*, int pos);
  916. NK_API const char *nk_str_at_const(const struct nk_str*, int pos, nk_rune *unicode, int *len);
  917. NK_API char *nk_str_get(struct nk_str*);
  918. NK_API const char *nk_str_get_const(const struct nk_str*);
  919. NK_API int nk_str_len(struct nk_str*);
  920. NK_API int nk_str_len_char(struct nk_str*);
  921. /*===============================================================
  922. *
  923. * TEXT EDITOR
  924. *
  925. * ===============================================================*/
  926. /* Editing text in this library is handled by either `nk_edit_string` or
  927. * `nk_edit_buffer`. But like almost everything in this library there are multiple
  928. * ways of doing it and a balance between control and ease of use with memory
  929. * as well as functionality controlled by flags.
  930. *
  931. * This library generally allows three different levels of memory control:
  932. * First of is the most basic way of just providing a simple char array with
  933. * string length. This method is probably the easiest way of handling simple
  934. * user text input. Main upside is complete control over memory while the biggest
  935. * downside in comparsion with the other two approaches is missing undo/redo.
  936. *
  937. * For UIs that require undo/redo the second way was created. It is based on
  938. * a fixed size nk_text_edit struct, which has an internal undo/redo stack.
  939. * This is mainly useful if you want something more like a text editor but don't want
  940. * to have a dynamically growing buffer.
  941. *
  942. * The final ways is using a dynamically growing nk_text_edit struct, which
  943. * has both a default version if you don't care were memory comes from and a
  944. * allocator version if you do. While the text editor is quite powerful for its
  945. * complexity I would not recommend editing gigabytes of data with it.
  946. * It is rather designed for uses cases which make sense for a GUI library not for
  947. * an full blown text editor.
  948. */
  949. #ifndef NK_TEXTEDIT_UNDOSTATECOUNT
  950. #define NK_TEXTEDIT_UNDOSTATECOUNT 99
  951. #endif
  952. #ifndef NK_TEXTEDIT_UNDOCHARCOUNT
  953. #define NK_TEXTEDIT_UNDOCHARCOUNT 999
  954. #endif
  955. struct nk_text_edit;
  956. struct nk_clipboard {
  957. nk_handle userdata;
  958. nk_paste_f paste;
  959. nk_copy_f copy;
  960. };
  961. struct nk_text_undo_record {
  962. int where;
  963. short insert_length;
  964. short delete_length;
  965. short char_storage;
  966. };
  967. struct nk_text_undo_state {
  968. struct nk_text_undo_record undo_rec[NK_TEXTEDIT_UNDOSTATECOUNT];
  969. nk_rune undo_char[NK_TEXTEDIT_UNDOCHARCOUNT];
  970. short undo_point;
  971. short redo_point;
  972. short undo_char_point;
  973. short redo_char_point;
  974. };
  975. enum nk_text_edit_type {
  976. NK_TEXT_EDIT_SINGLE_LINE,
  977. NK_TEXT_EDIT_MULTI_LINE
  978. };
  979. enum nk_text_edit_mode {
  980. NK_TEXT_EDIT_MODE_VIEW,
  981. NK_TEXT_EDIT_MODE_INSERT,
  982. NK_TEXT_EDIT_MODE_REPLACE
  983. };
  984. struct nk_text_edit {
  985. struct nk_clipboard clip;
  986. struct nk_str string;
  987. nk_filter filter;
  988. struct nk_vec2 scrollbar;
  989. int cursor;
  990. int select_start;
  991. int select_end;
  992. unsigned char mode;
  993. unsigned char cursor_at_end_of_line;
  994. unsigned char initialized;
  995. unsigned char has_preferred_x;
  996. unsigned char single_line;
  997. unsigned char active;
  998. unsigned char padding1;
  999. float preferred_x;
  1000. struct nk_text_undo_state undo;
  1001. };
  1002. /* filter function */
  1003. NK_API int nk_filter_default(const struct nk_text_edit*, nk_rune unicode);
  1004. NK_API int nk_filter_ascii(const struct nk_text_edit*, nk_rune unicode);
  1005. NK_API int nk_filter_float(const struct nk_text_edit*, nk_rune unicode);
  1006. NK_API int nk_filter_decimal(const struct nk_text_edit*, nk_rune unicode);
  1007. NK_API int nk_filter_hex(const struct nk_text_edit*, nk_rune unicode);
  1008. NK_API int nk_filter_oct(const struct nk_text_edit*, nk_rune unicode);
  1009. NK_API int nk_filter_binary(const struct nk_text_edit*, nk_rune unicode);
  1010. /* text editor */
  1011. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  1012. NK_API void nk_textedit_init_default(struct nk_text_edit*);
  1013. #endif
  1014. NK_API void nk_textedit_init(struct nk_text_edit*, struct nk_allocator*, nk_size size);
  1015. NK_API void nk_textedit_init_fixed(struct nk_text_edit*, void *memory, nk_size size);
  1016. NK_API void nk_textedit_free(struct nk_text_edit*);
  1017. NK_API void nk_textedit_text(struct nk_text_edit*, const char*, int total_len);
  1018. NK_API void nk_textedit_delete(struct nk_text_edit*, int where, int len);
  1019. NK_API void nk_textedit_delete_selection(struct nk_text_edit*);
  1020. NK_API void nk_textedit_select_all(struct nk_text_edit*);
  1021. NK_API int nk_textedit_cut(struct nk_text_edit*);
  1022. NK_API int nk_textedit_paste(struct nk_text_edit*, char const*, int len);
  1023. NK_API void nk_textedit_undo(struct nk_text_edit*);
  1024. NK_API void nk_textedit_redo(struct nk_text_edit*);
  1025. /* ===============================================================
  1026. *
  1027. * FONT
  1028. *
  1029. * ===============================================================*/
  1030. /* Font handling in this library was designed to be quite customizable and lets
  1031. you decide what you want to use and what you want to provide. In this sense
  1032. there are four different degrees between control and ease of use and two
  1033. different drawing APIs to provide for.
  1034. So first of the easiest way to do font handling is by just providing a
  1035. `nk_user_font` struct which only requires the height in pixel of the used
  1036. font and a callback to calculate the width of a string. This way of handling
  1037. fonts is best fitted for using the normal draw shape command API were you
  1038. do all the text drawing yourself and the library does not require any kind
  1039. of deeper knowledge about which font handling mechanism you use.
  1040. While the first approach works fine if you don't want to use the optional
  1041. vertex buffer output it is not enough if you do. To get font handling working
  1042. for these cases you have to provide two additional parameter inside the
  1043. `nk_user_font`. First a texture atlas handle used to draw text as subimages
  1044. of a bigger font atlas texture and a callback to query a characters glyph
  1045. information (offset, size, ...). So it is still possible to provide your own
  1046. font and use the vertex buffer output.
  1047. The final approach if you do not have a font handling functionality or don't
  1048. want to use it in this library is by using the optional font baker. This API
  1049. is divided into a high- and low-level API with different priorities between
  1050. ease of use and control. Both API's can be used to create a font and
  1051. font atlas texture and can even be used with or without the vertex buffer
  1052. output. So it still uses the `nk_user_font` struct and the two different
  1053. approaches previously stated still work.
  1054. Now to the difference between the low level API and the high level API. The low
  1055. level API provides a lot of control over the baking process of the font and
  1056. provides total control over memory. It consists of a number of functions that
  1057. need to be called from begin to end and each step requires some additional
  1058. configuration, so it is a lot more complex than the high-level API.
  1059. If you don't want to do all the work required for using the low-level API
  1060. you can use the font atlas API. It provides the same functionality as the
  1061. low-level API but takes away some configuration and all of memory control and
  1062. in term provides a easier to use API.
  1063. */
  1064. struct nk_user_font_glyph;
  1065. typedef float(*nk_text_width_f)(nk_handle, float h, const char*, int len);
  1066. typedef void(*nk_query_font_glyph_f)(nk_handle handle, float font_height,
  1067. struct nk_user_font_glyph *glyph,
  1068. nk_rune codepoint, nk_rune next_codepoint);
  1069. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  1070. struct nk_user_font_glyph {
  1071. struct nk_vec2 uv[2];
  1072. /* texture coordinates */
  1073. struct nk_vec2 offset;
  1074. /* offset between top left and glyph */
  1075. float width, height;
  1076. /* size of the glyph */
  1077. float xadvance;
  1078. /* offset to the next glyph */
  1079. };
  1080. #endif
  1081. struct nk_user_font {
  1082. nk_handle userdata;
  1083. /* user provided font handle */
  1084. float height;
  1085. /* max height of the font */
  1086. nk_text_width_f width;
  1087. /* font string width in pixel callback */
  1088. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  1089. nk_query_font_glyph_f query;
  1090. /* font glyph callback to query drawing info */
  1091. nk_handle texture;
  1092. /* texture handle to the used font atlas or texture */
  1093. #endif
  1094. };
  1095. #ifdef NK_INCLUDE_FONT_BAKING
  1096. enum nk_font_coord_type {
  1097. NK_COORD_UV,
  1098. /* texture coordinates inside font glyphs are clamped between 0-1 */
  1099. NK_COORD_PIXEL
  1100. /* texture coordinates inside font glyphs are in absolute pixel */
  1101. };
  1102. struct nk_baked_font {
  1103. float height;
  1104. /* height of the font */
  1105. float ascent, descent;
  1106. /* font glyphs ascent and descent */
  1107. nk_rune glyph_offset;
  1108. /* glyph array offset inside the font glyph baking output array */
  1109. nk_rune glyph_count;
  1110. /* number of glyphs of this font inside the glyph baking array output */
  1111. const nk_rune *ranges;
  1112. /* font codepoint ranges as pairs of (from/to) and 0 as last element */
  1113. };
  1114. struct nk_font_config {
  1115. void *ttf_blob;
  1116. /* pointer to loaded TTF file memory block.
  1117. * NOTE: not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
  1118. nk_size ttf_size;
  1119. /* size of the loaded TTF file memory block
  1120. * NOTE: not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
  1121. unsigned char ttf_data_owned_by_atlas;
  1122. /* used inside font atlas: default to: 0*/
  1123. unsigned char merge_mode;
  1124. /* merges this font into the last font */
  1125. unsigned char pixel_snap;
  1126. /* align very character to pixel boundary (if true set oversample (1,1)) */
  1127. unsigned char oversample_v, oversample_h;
  1128. /* rasterize at hight quality for sub-pixel position */
  1129. unsigned char padding[3];
  1130. float size;
  1131. /* baked pixel height of the font */
  1132. enum nk_font_coord_type coord_type;
  1133. /* texture coordinate format with either pixel or UV coordinates */
  1134. struct nk_vec2 spacing;
  1135. /* extra pixel spacing between glyphs */
  1136. const nk_rune *range;
  1137. /* list of unicode ranges (2 values per range, zero terminated) */
  1138. struct nk_baked_font *font;
  1139. /* font to setup in the baking process: NOTE: not needed for font atlas */
  1140. nk_rune fallback_glyph;
  1141. /* fallback glyph to use if a given rune is not found */
  1142. };
  1143. struct nk_font_glyph {
  1144. nk_rune codepoint;
  1145. float xadvance;
  1146. float x0, y0, x1, y1, w, h;
  1147. float u0, v0, u1, v1;
  1148. };
  1149. struct nk_font {
  1150. struct nk_user_font handle;
  1151. struct nk_baked_font info;
  1152. float scale;
  1153. struct nk_font_glyph *glyphs;
  1154. const struct nk_font_glyph *fallback;
  1155. nk_rune fallback_codepoint;
  1156. nk_handle texture;
  1157. int config;
  1158. };
  1159. enum nk_font_atlas_format {
  1160. NK_FONT_ATLAS_ALPHA8,
  1161. NK_FONT_ATLAS_RGBA32
  1162. };
  1163. struct nk_font_atlas {
  1164. void *pixel;
  1165. int tex_width;
  1166. int tex_height;
  1167. struct nk_allocator alloc;
  1168. struct nk_recti custom;
  1169. int glyph_count;
  1170. struct nk_font *default_font;
  1171. struct nk_font_glyph *glyphs;
  1172. struct nk_font **fonts;
  1173. struct nk_font_config *config;
  1174. int font_num, font_cap;
  1175. };
  1176. /* some language glyph codepoint ranges */
  1177. NK_API const nk_rune *nk_font_default_glyph_ranges(void);
  1178. NK_API const nk_rune *nk_font_chinese_glyph_ranges(void);
  1179. NK_API const nk_rune *nk_font_cyrillic_glyph_ranges(void);
  1180. NK_API const nk_rune *nk_font_korean_glyph_ranges(void);
  1181. /* Font Atlas
  1182. * ---------------------------------------------------------------
  1183. * This is the high level font baking and handling API to generate an image
  1184. * out of font glyphs used to draw text onto the screen. This API takes away
  1185. * some control over the baking process like fine grained memory control and
  1186. * custom baking data but provides additional functionality and easier to
  1187. * use and manage data structures and functions. */
  1188. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  1189. NK_API void nk_font_atlas_init_default(struct nk_font_atlas*);
  1190. #endif
  1191. NK_API void nk_font_atlas_init(struct nk_font_atlas*, struct nk_allocator*);
  1192. NK_API void nk_font_atlas_begin(struct nk_font_atlas*);
  1193. NK_API struct nk_font_config nk_font_config(float pixel_height);
  1194. NK_API struct nk_font *nk_font_atlas_add(struct nk_font_atlas*, const struct nk_font_config*);
  1195. #ifdef NK_INCLUDE_DEFAULT_FONT
  1196. NK_API struct nk_font* nk_font_atlas_add_default(struct nk_font_atlas*, float height, const struct nk_font_config*);
  1197. #endif
  1198. NK_API struct nk_font* nk_font_atlas_add_from_memory(struct nk_font_atlas *atlas, void *memory, nk_size size, float height, const struct nk_font_config *config);
  1199. #ifdef NK_INCLUDE_STANDARD_IO
  1200. NK_API struct nk_font* nk_font_atlas_add_from_file(struct nk_font_atlas *atlas, const char *file_path, float height, const struct nk_font_config*);
  1201. #endif
  1202. NK_API struct nk_font *nk_font_atlas_add_compressed(struct nk_font_atlas*, void *memory, nk_size size, float height, const struct nk_font_config*);
  1203. NK_API struct nk_font* nk_font_atlas_add_compressed_base85(struct nk_font_atlas*, const char *data, float height, const struct nk_font_config *config);
  1204. NK_API const void* nk_font_atlas_bake(struct nk_font_atlas*, int *width, int *height, enum nk_font_atlas_format);
  1205. NK_API void nk_font_atlas_end(struct nk_font_atlas*, nk_handle tex, struct nk_draw_null_texture*);
  1206. NK_API void nk_font_atlas_clear(struct nk_font_atlas*);
  1207. /* Font
  1208. * -----------------------------------------------------------------
  1209. * The font structure is just a simple container to hold the output of a baking
  1210. * process in the low level API. */
  1211. NK_API void nk_font_init(struct nk_font*, float pixel_height, nk_rune fallback_codepoint, struct nk_font_glyph*, const struct nk_baked_font*, nk_handle atlas);
  1212. NK_API const struct nk_font_glyph* nk_font_find_glyph(struct nk_font*, nk_rune unicode);
  1213. /* Font baking (needs to be called sequentially top to bottom)
  1214. * --------------------------------------------------------------------
  1215. * This is a low level API to bake font glyphs into an image and is more
  1216. * complex than the atlas API but provides more control over the baking
  1217. * process with custom bake data and memory management. */
  1218. NK_API void nk_font_bake_memory(nk_size *temporary_memory, int *glyph_count, struct nk_font_config*, int count);
  1219. NK_API int nk_font_bake_pack(nk_size *img_memory, int *img_width, int *img_height, struct nk_recti *custom_space, void *temporary_memory, nk_size temporary_size, const struct nk_font_config*, int font_count, struct nk_allocator *alloc);
  1220. NK_API void nk_font_bake(void *image_memory, int image_width, int image_height, void *temporary_memory, nk_size temporary_memory_size, struct nk_font_glyph*, int glyphs_count, const struct nk_font_config*, int font_count);
  1221. NK_API void nk_font_bake_custom_data(void *img_memory, int img_width, int img_height, struct nk_recti img_dst, const char *image_data_mask, int tex_width, int tex_height,char white,char black);
  1222. NK_API void nk_font_bake_convert(void *out_memory, int image_width, int image_height, const void *in_memory);
  1223. #endif
  1224. /* ===============================================================
  1225. *
  1226. * DRAWING
  1227. *
  1228. * ===============================================================*/
  1229. /* This library was designed to be render backend agnostic so it does
  1230. not draw anything to screen. Instead all drawn shapes, widgets
  1231. are made of, are buffered into memory and make up a command queue.
  1232. Each frame therefore fills the command buffer with draw commands
  1233. that then need to be executed by the user and his own render backend.
  1234. After that the command buffer needs to be cleared and a new frame can be
  1235. started. It is probably important to note that the command buffer is the main
  1236. drawing API and the optional vertex buffer API only takes this format and
  1237. converts it into a hardware accessible format.
  1238. Draw commands are divided into filled shapes and shape outlines but only
  1239. filled shapes as well as line, curves and scissor are required to be provided.
  1240. All other shape drawing commands can be used but are not required. This was
  1241. done to allow the maximum number of render backends to be able to use this
  1242. library without you having to do additional work.
  1243. */
  1244. enum nk_command_type {
  1245. NK_COMMAND_NOP,
  1246. NK_COMMAND_SCISSOR,
  1247. NK_COMMAND_LINE,
  1248. NK_COMMAND_CURVE,
  1249. NK_COMMAND_RECT,
  1250. NK_COMMAND_RECT_FILLED,
  1251. NK_COMMAND_RECT_MULTI_COLOR,
  1252. NK_COMMAND_CIRCLE,
  1253. NK_COMMAND_CIRCLE_FILLED,
  1254. NK_COMMAND_ARC,
  1255. NK_COMMAND_ARC_FILLED,
  1256. NK_COMMAND_TRIANGLE,
  1257. NK_COMMAND_TRIANGLE_FILLED,
  1258. NK_COMMAND_POLYGON,
  1259. NK_COMMAND_POLYGON_FILLED,
  1260. NK_COMMAND_POLYLINE,
  1261. NK_COMMAND_TEXT,
  1262. NK_COMMAND_IMAGE
  1263. };
  1264. /* command base and header of every command inside the buffer */
  1265. struct nk_command {
  1266. enum nk_command_type type;
  1267. nk_size next;
  1268. #ifdef NK_INCLUDE_COMMAND_USERDATA
  1269. nk_handle userdata;
  1270. #endif
  1271. };
  1272. struct nk_command_scissor {
  1273. struct nk_command header;
  1274. short x, y;
  1275. unsigned short w, h;
  1276. };
  1277. struct nk_command_line {
  1278. struct nk_command header;
  1279. unsigned short line_thickness;
  1280. struct nk_vec2i begin;
  1281. struct nk_vec2i end;
  1282. struct nk_color color;
  1283. };
  1284. struct nk_command_curve {
  1285. struct nk_command header;
  1286. unsigned short line_thickness;
  1287. struct nk_vec2i begin;
  1288. struct nk_vec2i end;
  1289. struct nk_vec2i ctrl[2];
  1290. struct nk_color color;
  1291. };
  1292. struct nk_command_rect {
  1293. struct nk_command header;
  1294. unsigned short rounding;
  1295. unsigned short line_thickness;
  1296. short x, y;
  1297. unsigned short w, h;
  1298. struct nk_color color;
  1299. };
  1300. struct nk_command_rect_filled {
  1301. struct nk_command header;
  1302. unsigned short rounding;
  1303. short x, y;
  1304. unsigned short w, h;
  1305. struct nk_color color;
  1306. };
  1307. struct nk_command_rect_multi_color {
  1308. struct nk_command header;
  1309. short x, y;
  1310. unsigned short w, h;
  1311. struct nk_color left;
  1312. struct nk_color top;
  1313. struct nk_color bottom;
  1314. struct nk_color right;
  1315. };
  1316. struct nk_command_triangle {
  1317. struct nk_command header;
  1318. unsigned short line_thickness;
  1319. struct nk_vec2i a;
  1320. struct nk_vec2i b;
  1321. struct nk_vec2i c;
  1322. struct nk_color color;
  1323. };
  1324. struct nk_command_triangle_filled {
  1325. struct nk_command header;
  1326. struct nk_vec2i a;
  1327. struct nk_vec2i b;
  1328. struct nk_vec2i c;
  1329. struct nk_color color;
  1330. };
  1331. struct nk_command_circle {
  1332. struct nk_command header;
  1333. short x, y;
  1334. unsigned short line_thickness;
  1335. unsigned short w, h;
  1336. struct nk_color color;
  1337. };
  1338. struct nk_command_circle_filled {
  1339. struct nk_command header;
  1340. short x, y;
  1341. unsigned short w, h;
  1342. struct nk_color color;
  1343. };
  1344. struct nk_command_arc {
  1345. struct nk_command header;
  1346. short cx, cy;
  1347. unsigned short r;
  1348. unsigned short line_thickness;
  1349. float a[2];
  1350. struct nk_color color;
  1351. };
  1352. struct nk_command_arc_filled {
  1353. struct nk_command header;
  1354. short cx, cy;
  1355. unsigned short r;
  1356. float a[2];
  1357. struct nk_color color;
  1358. };
  1359. struct nk_command_polygon {
  1360. struct nk_command header;
  1361. struct nk_color color;
  1362. unsigned short line_thickness;
  1363. unsigned short point_count;
  1364. struct nk_vec2i points[1];
  1365. };
  1366. struct nk_command_polygon_filled {
  1367. struct nk_command header;
  1368. struct nk_color color;
  1369. unsigned short point_count;
  1370. struct nk_vec2i points[1];
  1371. };
  1372. struct nk_command_polyline {
  1373. struct nk_command header;
  1374. struct nk_color color;
  1375. unsigned short line_thickness;
  1376. unsigned short point_count;
  1377. struct nk_vec2i points[1];
  1378. };
  1379. struct nk_command_image {
  1380. struct nk_command header;
  1381. short x, y;
  1382. unsigned short w, h;
  1383. struct nk_image img;
  1384. };
  1385. struct nk_command_text {
  1386. struct nk_command header;
  1387. const struct nk_user_font *font;
  1388. struct nk_color background;
  1389. struct nk_color foreground;
  1390. short x, y;
  1391. unsigned short w, h;
  1392. float height;
  1393. int length;
  1394. char string[1];
  1395. };
  1396. enum nk_command_clipping {
  1397. NK_CLIPPING_OFF = nk_false,
  1398. NK_CLIPPING_ON = nk_true
  1399. };
  1400. struct nk_command_buffer {
  1401. struct nk_buffer *base;
  1402. struct nk_rect clip;
  1403. int use_clipping;
  1404. nk_handle userdata;
  1405. nk_size begin, end, last;
  1406. };
  1407. /* shape outlines */
  1408. NK_API void nk_stroke_line(struct nk_command_buffer *b, float x0, float y0, float x1, float y1, float line_thickness, struct nk_color);
  1409. NK_API void nk_stroke_curve(struct nk_command_buffer*, float, float, float, float, float, float, float, float, float line_thickness, struct nk_color);
  1410. NK_API void nk_stroke_rect(struct nk_command_buffer*, struct nk_rect, float rounding, float line_thickness, struct nk_color);
  1411. NK_API void nk_stroke_circle(struct nk_command_buffer*, struct nk_rect, float line_thickness, struct nk_color);
  1412. NK_API void nk_stroke_arc(struct nk_command_buffer*, float cx, float cy, float radius, float a_min, float a_max, float line_thickness, struct nk_color);
  1413. NK_API void nk_stroke_triangle(struct nk_command_buffer*, float, float, float, float, float, float, float line_thichness, struct nk_color);
  1414. NK_API void nk_stroke_polyline(struct nk_command_buffer*, float *points, int point_count, float line_thickness, struct nk_color col);
  1415. NK_API void nk_stroke_polygon(struct nk_command_buffer*, float*, int point_count, float line_thickness, struct nk_color);
  1416. /* filled shades */
  1417. NK_API void nk_fill_rect(struct nk_command_buffer*, struct nk_rect, float rounding, struct nk_color);
  1418. NK_API void nk_fill_rect_multi_color(struct nk_command_buffer*, struct nk_rect, struct nk_color left, struct nk_color top, struct nk_color right, struct nk_color bottom);
  1419. NK_API void nk_fill_circle(struct nk_command_buffer*, struct nk_rect, struct nk_color);
  1420. NK_API void nk_fill_arc(struct nk_command_buffer*, float cx, float cy, float radius, float a_min, float a_max, struct nk_color);
  1421. NK_API void nk_fill_triangle(struct nk_command_buffer*, float x0, float y0, float x1, float y1, float x2, float y2, struct nk_color);
  1422. NK_API void nk_fill_polygon(struct nk_command_buffer*, float*, int point_count, struct nk_color);
  1423. /* misc */
  1424. NK_API void nk_push_scissor(struct nk_command_buffer*, struct nk_rect);
  1425. NK_API void nk_draw_image(struct nk_command_buffer*, struct nk_rect, const struct nk_image*);
  1426. NK_API void nk_draw_text(struct nk_command_buffer*, struct nk_rect, const char *text, int len, const struct nk_user_font*, struct nk_color, struct nk_color);
  1427. NK_API const struct nk_command* nk__next(struct nk_context*, const struct nk_command*);
  1428. NK_API const struct nk_command* nk__begin(struct nk_context*);
  1429. /* ===============================================================
  1430. *
  1431. * INPUT
  1432. *
  1433. * ===============================================================*/
  1434. struct nk_mouse_button {
  1435. int down;
  1436. unsigned int clicked;
  1437. struct nk_vec2 clicked_pos;
  1438. };
  1439. struct nk_mouse {
  1440. struct nk_mouse_button buttons[NK_BUTTON_MAX];
  1441. struct nk_vec2 pos;
  1442. struct nk_vec2 prev;
  1443. struct nk_vec2 delta;
  1444. float scroll_delta;
  1445. unsigned char grab;
  1446. unsigned char grabbed;
  1447. unsigned char ungrab;
  1448. };
  1449. struct nk_key {
  1450. int down;
  1451. unsigned int clicked;
  1452. };
  1453. struct nk_keyboard {
  1454. struct nk_key keys[NK_KEY_MAX];
  1455. char text[NK_INPUT_MAX];
  1456. int text_len;
  1457. };
  1458. struct nk_input {
  1459. struct nk_keyboard keyboard;
  1460. struct nk_mouse mouse;
  1461. };
  1462. NK_API int nk_input_has_mouse_click(const struct nk_input*, enum nk_buttons);
  1463. NK_API int nk_input_has_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  1464. NK_API int nk_input_has_mouse_click_down_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect, int down);
  1465. NK_API int nk_input_is_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  1466. NK_API int nk_input_is_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id, struct nk_rect b, int down);
  1467. NK_API int nk_input_any_mouse_click_in_rect(const struct nk_input*, struct nk_rect);
  1468. NK_API int nk_input_is_mouse_prev_hovering_rect(const struct nk_input*, struct nk_rect);
  1469. NK_API int nk_input_is_mouse_hovering_rect(const struct nk_input*, struct nk_rect);
  1470. NK_API int nk_input_mouse_clicked(const struct nk_input*, enum nk_buttons, struct nk_rect);
  1471. NK_API int nk_input_is_mouse_down(const struct nk_input*, enum nk_buttons);
  1472. NK_API int nk_input_is_mouse_pressed(const struct nk_input*, enum nk_buttons);
  1473. NK_API int nk_input_is_mouse_released(const struct nk_input*, enum nk_buttons);
  1474. NK_API int nk_input_is_key_pressed(const struct nk_input*, enum nk_keys);
  1475. NK_API int nk_input_is_key_released(const struct nk_input*, enum nk_keys);
  1476. NK_API int nk_input_is_key_down(const struct nk_input*, enum nk_keys);
  1477. /* ===============================================================
  1478. *
  1479. * DRAW LIST
  1480. *
  1481. * ===============================================================*/
  1482. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  1483. /* The optional vertex buffer draw list provides a 2D drawing context
  1484. with antialiasing functionality which takes basic filled or outlined shapes
  1485. or a path and outputs vertexes, elements and draw commands.
  1486. The actual draw list API is not required to be used directly while using this
  1487. library since converting the default library draw command output is done by
  1488. just calling `nk_convert` but I decided to still make this library accessible
  1489. since it can be useful.
  1490. The draw list is based on a path buffering and polygon and polyline
  1491. rendering API which allows a lot of ways to draw 2D content to screen.
  1492. In fact it is probably more powerful than needed but allows even more crazy
  1493. things than this library provides by default.
  1494. */
  1495. typedef unsigned short nk_draw_index;
  1496. typedef nk_uint nk_draw_vertex_color;
  1497. enum nk_draw_list_stroke {
  1498. NK_STROKE_OPEN = nk_false,
  1499. /* build up path has no connection back to the beginning */
  1500. NK_STROKE_CLOSED = nk_true
  1501. /* build up path has a connection back to the beginning */
  1502. };
  1503. struct nk_draw_vertex {
  1504. struct nk_vec2 position;
  1505. struct nk_vec2 uv;
  1506. nk_draw_vertex_color col;
  1507. };
  1508. struct nk_draw_command {
  1509. unsigned int elem_count;
  1510. /* number of elements in the current draw batch */
  1511. struct nk_rect clip_rect;
  1512. /* current screen clipping rectangle */
  1513. nk_handle texture;
  1514. /* current texture to set */
  1515. #ifdef NK_INCLUDE_COMMAND_USERDATA
  1516. nk_handle userdata;
  1517. #endif
  1518. };
  1519. struct nk_draw_list {
  1520. float global_alpha;
  1521. enum nk_anti_aliasing shape_AA;
  1522. enum nk_anti_aliasing line_AA;
  1523. struct nk_draw_null_texture null;
  1524. struct nk_rect clip_rect;
  1525. struct nk_buffer *buffer;
  1526. struct nk_buffer *vertices;
  1527. struct nk_buffer *elements;
  1528. unsigned int element_count;
  1529. unsigned int vertex_count;
  1530. nk_size cmd_offset;
  1531. unsigned int cmd_count;
  1532. unsigned int path_count;
  1533. unsigned int path_offset;
  1534. struct nk_vec2 circle_vtx[12];
  1535. #ifdef NK_INCLUDE_COMMAND_USERDATA
  1536. nk_handle userdata;
  1537. #endif
  1538. };
  1539. /* draw list */
  1540. NK_API void nk_draw_list_init(struct nk_draw_list*);
  1541. NK_API void nk_draw_list_setup(struct nk_draw_list*, float global_alpha, enum nk_anti_aliasing, enum nk_anti_aliasing, struct nk_draw_null_texture, struct nk_buffer *cmds, struct nk_buffer *vert, struct nk_buffer *elem);
  1542. NK_API void nk_draw_list_clear(struct nk_draw_list*);
  1543. /* drawing */
  1544. #define nk_draw_list_foreach(cmd, can, b) for((cmd)=nk__draw_list_begin(can, b); (cmd)!=0; (cmd)=nk__draw_list_next(cmd, b, can))
  1545. NK_API const struct nk_draw_command* nk__draw_list_begin(const struct nk_draw_list*, const struct nk_buffer*);
  1546. NK_API const struct nk_draw_command* nk__draw_list_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_draw_list*);
  1547. NK_API const struct nk_draw_command* nk__draw_begin(const struct nk_context*, const struct nk_buffer*);
  1548. NK_API const struct nk_draw_command* nk__draw_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_context*);
  1549. /* path */
  1550. NK_API void nk_draw_list_path_clear(struct nk_draw_list*);
  1551. NK_API void nk_draw_list_path_line_to(struct nk_draw_list *list, struct nk_vec2 pos);
  1552. NK_API void nk_draw_list_path_arc_to_fast(struct nk_draw_list*, struct nk_vec2 center, float radius, int a_min, int a_max);
  1553. NK_API void nk_draw_list_path_arc_to(struct nk_draw_list*, struct nk_vec2 center, float radius, float a_min, float a_max, unsigned int segments);
  1554. NK_API void nk_draw_list_path_rect_to(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, float rounding);
  1555. NK_API void nk_draw_list_path_curve_to(struct nk_draw_list*, struct nk_vec2 p2, struct nk_vec2 p3, struct nk_vec2 p4, unsigned int num_segments);
  1556. NK_API void nk_draw_list_path_fill(struct nk_draw_list*, struct nk_color);
  1557. NK_API void nk_draw_list_path_stroke(struct nk_draw_list*, struct nk_color, enum nk_draw_list_stroke closed, float thickness);
  1558. /* stroke */
  1559. NK_API void nk_draw_list_stroke_line(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, struct nk_color, float thickness);
  1560. NK_API void nk_draw_list_stroke_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding, float thickness);
  1561. NK_API void nk_draw_list_stroke_triangle(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, struct nk_vec2 c, struct nk_color, float thickness);
  1562. NK_API void nk_draw_list_stroke_circle(struct nk_draw_list*, struct nk_vec2 center, float radius, struct nk_color, unsigned int segs, float thickness);
  1563. NK_API void nk_draw_list_stroke_curve(struct nk_draw_list*, struct nk_vec2 p0, struct nk_vec2 cp0, struct nk_vec2 cp1, struct nk_vec2 p1, struct nk_color, unsigned int segments, float thickness);
  1564. NK_API void nk_draw_list_stroke_poly_line(struct nk_draw_list*, const struct nk_vec2 *pnts, const unsigned int cnt, struct nk_color, enum nk_draw_list_stroke, float thickness, enum nk_anti_aliasing);
  1565. /* fill */
  1566. NK_API void nk_draw_list_fill_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding);
  1567. NK_API void nk_draw_list_fill_rect_multi_color(struct nk_draw_list *list, struct nk_rect rect, struct nk_color left, struct nk_color top, struct nk_color right, struct nk_color bottom);
  1568. NK_API void nk_draw_list_fill_triangle(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, struct nk_vec2 c, struct nk_color);
  1569. NK_API void nk_draw_list_fill_circle(struct nk_draw_list*, struct nk_vec2 center, float radius, struct nk_color col, unsigned int segs);
  1570. NK_API void nk_draw_list_fill_poly_convex(struct nk_draw_list*, const struct nk_vec2 *points, const unsigned int count, struct nk_color, enum nk_anti_aliasing);
  1571. /* misc */
  1572. NK_API void nk_draw_list_add_image(struct nk_draw_list*, struct nk_image texture, struct nk_rect rect, struct nk_color);
  1573. NK_API void nk_draw_list_add_text(struct nk_draw_list*, const struct nk_user_font*, struct nk_rect, const char *text, int len, float font_height, struct nk_color);
  1574. #ifdef NK_INCLUDE_COMMAND_USERDATA
  1575. NK_API void nk_draw_list_push_userdata(struct nk_draw_list*, nk_handle userdata);
  1576. #endif
  1577. #endif
  1578. /* ===============================================================
  1579. *
  1580. * GUI
  1581. *
  1582. * ===============================================================*/
  1583. enum nk_style_item_type {
  1584. NK_STYLE_ITEM_COLOR,
  1585. NK_STYLE_ITEM_IMAGE
  1586. };
  1587. union nk_style_item_data {
  1588. struct nk_image image;
  1589. struct nk_color color;
  1590. };
  1591. struct nk_style_item {
  1592. enum nk_style_item_type type;
  1593. union nk_style_item_data data;
  1594. };
  1595. struct nk_style_text {
  1596. struct nk_color color;
  1597. struct nk_vec2 padding;
  1598. };
  1599. struct nk_style_button {
  1600. /* background */
  1601. struct nk_style_item normal;
  1602. struct nk_style_item hover;
  1603. struct nk_style_item active;
  1604. struct nk_color border_color;
  1605. /* text */
  1606. struct nk_color text_background;
  1607. struct nk_color text_normal;
  1608. struct nk_color text_hover;
  1609. struct nk_color text_active;
  1610. nk_flags text_alignment;
  1611. /* properties */
  1612. float border;
  1613. float rounding;
  1614. struct nk_vec2 padding;
  1615. struct nk_vec2 image_padding;
  1616. struct nk_vec2 touch_padding;
  1617. /* optional user callbacks */
  1618. nk_handle userdata;
  1619. void(*draw_begin)(struct nk_command_buffer*, nk_handle userdata);
  1620. void(*draw_end)(struct nk_command_buffer*, nk_handle userdata);
  1621. };
  1622. struct nk_style_toggle {
  1623. /* background */
  1624. struct nk_style_item normal;
  1625. struct nk_style_item hover;
  1626. struct nk_style_item active;
  1627. /* cursor */
  1628. struct nk_style_item cursor_normal;
  1629. struct nk_style_item cursor_hover;
  1630. /* text */
  1631. struct nk_color text_normal;
  1632. struct nk_color text_hover;
  1633. struct nk_color text_active;
  1634. struct nk_color text_background;
  1635. nk_flags text_alignment;
  1636. /* properties */
  1637. struct nk_vec2 padding;
  1638. struct nk_vec2 touch_padding;
  1639. /* optional user callbacks */
  1640. nk_handle userdata;
  1641. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  1642. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  1643. };
  1644. struct nk_style_selectable {
  1645. /* background (inactive) */
  1646. struct nk_style_item normal;
  1647. struct nk_style_item hover;
  1648. struct nk_style_item pressed;
  1649. /* background (active) */
  1650. struct nk_style_item normal_active;
  1651. struct nk_style_item hover_active;
  1652. struct nk_style_item pressed_active;
  1653. /* text color (inactive) */
  1654. struct nk_color text_normal;
  1655. struct nk_color text_hover;
  1656. struct nk_color text_pressed;
  1657. /* text color (active) */
  1658. struct nk_color text_normal_active;
  1659. struct nk_color text_hover_active;
  1660. struct nk_color text_pressed_active;
  1661. struct nk_color text_background;
  1662. nk_flags text_alignment;
  1663. /* properties */
  1664. float rounding;
  1665. struct nk_vec2 padding;
  1666. struct nk_vec2 touch_padding;
  1667. struct nk_vec2 image_padding;
  1668. /* optional user callbacks */
  1669. nk_handle userdata;
  1670. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  1671. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  1672. };
  1673. struct nk_style_slider {
  1674. /* background */
  1675. struct nk_style_item normal;
  1676. struct nk_style_item hover;
  1677. struct nk_style_item active;
  1678. struct nk_color border_color;
  1679. /* background bar */
  1680. struct nk_color bar_normal;
  1681. struct nk_color bar_hover;
  1682. struct nk_color bar_active;
  1683. struct nk_color bar_filled;
  1684. /* cursor */
  1685. struct nk_style_item cursor_normal;
  1686. struct nk_style_item cursor_hover;
  1687. struct nk_style_item cursor_active;
  1688. /* properties */
  1689. float border;
  1690. float rounding;
  1691. float bar_height;
  1692. struct nk_vec2 padding;
  1693. struct nk_vec2 spacing;
  1694. struct nk_vec2 cursor_size;
  1695. /* optional buttons */
  1696. int show_buttons;
  1697. struct nk_style_button inc_button;
  1698. struct nk_style_button dec_button;
  1699. enum nk_symbol_type inc_symbol;
  1700. enum nk_symbol_type dec_symbol;
  1701. /* optional user callbacks */
  1702. nk_handle userdata;
  1703. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  1704. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  1705. };
  1706. struct nk_style_progress {
  1707. /* background */
  1708. struct nk_style_item normal;
  1709. struct nk_style_item hover;
  1710. struct nk_style_item active;
  1711. /* cursor */
  1712. struct nk_style_item cursor_normal;
  1713. struct nk_style_item cursor_hover;
  1714. struct nk_style_item cursor_active;
  1715. /* properties */
  1716. float rounding;
  1717. struct nk_vec2 padding;
  1718. /* optional user callbacks */
  1719. nk_handle userdata;
  1720. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  1721. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  1722. };
  1723. struct nk_style_scrollbar {
  1724. /* background */
  1725. struct nk_style_item normal;
  1726. struct nk_style_item hover;
  1727. struct nk_style_item active;
  1728. struct nk_color border_color;
  1729. /* cursor */
  1730. struct nk_style_item cursor_normal;
  1731. struct nk_style_item cursor_hover;
  1732. struct nk_style_item cursor_active;
  1733. /* properties */
  1734. float border;
  1735. float rounding;
  1736. struct nk_vec2 padding;
  1737. /* optional buttons */
  1738. int show_buttons;
  1739. struct nk_style_button inc_button;
  1740. struct nk_style_button dec_button;
  1741. enum nk_symbol_type inc_symbol;
  1742. enum nk_symbol_type dec_symbol;
  1743. /* optional user callbacks */
  1744. nk_handle userdata;
  1745. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  1746. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  1747. };
  1748. struct nk_style_edit {
  1749. /* background */
  1750. struct nk_style_item normal;
  1751. struct nk_style_item hover;
  1752. struct nk_style_item active;
  1753. struct nk_color border_color;
  1754. struct nk_style_scrollbar scrollbar;
  1755. /* cursor */
  1756. struct nk_color cursor_normal;
  1757. struct nk_color cursor_hover;
  1758. struct nk_color cursor_text_normal;
  1759. struct nk_color cursor_text_hover;
  1760. /* text (unselected) */
  1761. struct nk_color text_normal;
  1762. struct nk_color text_hover;
  1763. struct nk_color text_active;
  1764. /* text (selected) */
  1765. struct nk_color selected_normal;
  1766. struct nk_color selected_hover;
  1767. struct nk_color selected_text_normal;
  1768. struct nk_color selected_text_hover;
  1769. /* properties */
  1770. float border;
  1771. float rounding;
  1772. float cursor_size;
  1773. struct nk_vec2 scrollbar_size;
  1774. struct nk_vec2 padding;
  1775. float row_padding;
  1776. };
  1777. struct nk_style_property {
  1778. /* background */
  1779. struct nk_style_item normal;
  1780. struct nk_style_item hover;
  1781. struct nk_style_item active;
  1782. struct nk_color border_color;
  1783. /* text */
  1784. struct nk_color label_normal;
  1785. struct nk_color label_hover;
  1786. struct nk_color label_active;
  1787. /* symbols */
  1788. enum nk_symbol_type sym_left;
  1789. enum nk_symbol_type sym_right;
  1790. /* properties */
  1791. float border;
  1792. float rounding;
  1793. struct nk_vec2 padding;
  1794. struct nk_style_edit edit;
  1795. struct nk_style_button inc_button;
  1796. struct nk_style_button dec_button;
  1797. /* optional user callbacks */
  1798. nk_handle userdata;
  1799. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  1800. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  1801. };
  1802. struct nk_style_chart {
  1803. /* colors */
  1804. struct nk_style_item background;
  1805. struct nk_color border_color;
  1806. struct nk_color selected_color;
  1807. struct nk_color color;
  1808. /* properties */
  1809. float border;
  1810. float rounding;
  1811. struct nk_vec2 padding;
  1812. };
  1813. struct nk_style_combo {
  1814. /* background */
  1815. struct nk_style_item normal;
  1816. struct nk_style_item hover;
  1817. struct nk_style_item active;
  1818. struct nk_color border_color;
  1819. /* label */
  1820. struct nk_color label_normal;
  1821. struct nk_color label_hover;
  1822. struct nk_color label_active;
  1823. /* symbol */
  1824. struct nk_color symbol_normal;
  1825. struct nk_color symbol_hover;
  1826. struct nk_color symbol_active;
  1827. /* button */
  1828. struct nk_style_button button;
  1829. enum nk_symbol_type sym_normal;
  1830. enum nk_symbol_type sym_hover;
  1831. enum nk_symbol_type sym_active;
  1832. /* properties */
  1833. float border;
  1834. float rounding;
  1835. struct nk_vec2 content_padding;
  1836. struct nk_vec2 button_padding;
  1837. struct nk_vec2 spacing;
  1838. };
  1839. struct nk_style_tab {
  1840. /* background */
  1841. struct nk_style_item background;
  1842. struct nk_color border_color;
  1843. struct nk_color text;
  1844. /* button */
  1845. struct nk_style_button tab_button;
  1846. struct nk_style_button node_button;
  1847. enum nk_symbol_type sym_minimize;
  1848. enum nk_symbol_type sym_maximize;
  1849. /* properties */
  1850. float border;
  1851. float rounding;
  1852. struct nk_vec2 padding;
  1853. struct nk_vec2 spacing;
  1854. };
  1855. enum nk_style_header_align {
  1856. NK_HEADER_LEFT,
  1857. NK_HEADER_RIGHT
  1858. };
  1859. struct nk_style_window_header {
  1860. /* background */
  1861. struct nk_style_item normal;
  1862. struct nk_style_item hover;
  1863. struct nk_style_item active;
  1864. /* button */
  1865. struct nk_style_button close_button;
  1866. struct nk_style_button minimize_button;
  1867. enum nk_symbol_type close_symbol;
  1868. enum nk_symbol_type minimize_symbol;
  1869. enum nk_symbol_type maximize_symbol;
  1870. /* title */
  1871. struct nk_color label_normal;
  1872. struct nk_color label_hover;
  1873. struct nk_color label_active;
  1874. /* properties */
  1875. enum nk_style_header_align align;
  1876. struct nk_vec2 padding;
  1877. struct nk_vec2 label_padding;
  1878. struct nk_vec2 spacing;
  1879. };
  1880. struct nk_style_window {
  1881. struct nk_style_window_header header;
  1882. struct nk_style_item fixed_background;
  1883. struct nk_color background;
  1884. struct nk_color border_color;
  1885. struct nk_color combo_border_color;
  1886. struct nk_color contextual_border_color;
  1887. struct nk_color menu_border_color;
  1888. struct nk_color group_border_color;
  1889. struct nk_color tooltip_border_color;
  1890. struct nk_style_item scaler;
  1891. struct nk_vec2 footer_padding;
  1892. float border;
  1893. float combo_border;
  1894. float contextual_border;
  1895. float menu_border;
  1896. float group_border;
  1897. float tooltip_border;
  1898. float rounding;
  1899. struct nk_vec2 scaler_size;
  1900. struct nk_vec2 padding;
  1901. struct nk_vec2 spacing;
  1902. struct nk_vec2 scrollbar_size;
  1903. struct nk_vec2 min_size;
  1904. };
  1905. struct nk_style {
  1906. struct nk_user_font font;
  1907. struct nk_style_text text;
  1908. struct nk_style_button button;
  1909. struct nk_style_button contextual_button;
  1910. struct nk_style_button menu_button;
  1911. struct nk_style_toggle option;
  1912. struct nk_style_toggle checkbox;
  1913. struct nk_style_selectable selectable;
  1914. struct nk_style_slider slider;
  1915. struct nk_style_progress progress;
  1916. struct nk_style_property property;
  1917. struct nk_style_edit edit;
  1918. struct nk_style_chart chart;
  1919. struct nk_style_scrollbar scrollh;
  1920. struct nk_style_scrollbar scrollv;
  1921. struct nk_style_tab tab;
  1922. struct nk_style_combo combo;
  1923. struct nk_style_window window;
  1924. };
  1925. NK_API struct nk_style_item nk_style_item_image(struct nk_image img);
  1926. NK_API struct nk_style_item nk_style_item_color(struct nk_color);
  1927. NK_API struct nk_style_item nk_style_item_hide(void);
  1928. /*==============================================================
  1929. * PANEL
  1930. * =============================================================*/
  1931. #ifndef NK_CHART_MAX_SLOT
  1932. #define NK_CHART_MAX_SLOT 4
  1933. #endif
  1934. struct nk_chart_slot {
  1935. enum nk_chart_type type;
  1936. struct nk_color color;
  1937. struct nk_color highlight;
  1938. float min, max, range;
  1939. int count;
  1940. struct nk_vec2 last;
  1941. int index;
  1942. };
  1943. struct nk_chart {
  1944. struct nk_chart_slot slots[NK_CHART_MAX_SLOT];
  1945. int slot;
  1946. float x, y, w, h;
  1947. };
  1948. struct nk_row_layout {
  1949. int type;
  1950. int index;
  1951. float height;
  1952. int columns;
  1953. const float *ratio;
  1954. float item_width, item_height;
  1955. float item_offset;
  1956. float filled;
  1957. struct nk_rect item;
  1958. int tree_depth;
  1959. };
  1960. struct nk_popup_buffer {
  1961. nk_size begin;
  1962. nk_size parent;
  1963. nk_size last;
  1964. nk_size end;
  1965. int active;
  1966. };
  1967. struct nk_menu_state {
  1968. float x, y, w, h;
  1969. struct nk_scroll offset;
  1970. };
  1971. struct nk_panel {
  1972. nk_flags flags;
  1973. struct nk_rect bounds;
  1974. struct nk_scroll *offset;
  1975. float at_x, at_y, max_x;
  1976. float width, height;
  1977. float footer_h;
  1978. float header_h;
  1979. float border;
  1980. struct nk_rect clip;
  1981. struct nk_menu_state menu;
  1982. struct nk_row_layout row;
  1983. struct nk_chart chart;
  1984. struct nk_popup_buffer popup_buffer;
  1985. struct nk_command_buffer *buffer;
  1986. struct nk_panel *parent;
  1987. };
  1988. /*==============================================================
  1989. * WINDOW
  1990. * =============================================================*/
  1991. struct nk_table;
  1992. enum nk_window_flags {
  1993. NK_WINDOW_PRIVATE = NK_FLAG(9),
  1994. /* dummy flag which mark the beginning of the private window flag part */
  1995. NK_WINDOW_ROM = NK_FLAG(10),
  1996. /* sets the window into a read only mode and does not allow input changes */
  1997. NK_WINDOW_HIDDEN = NK_FLAG(11),
  1998. /* Hides the window and stops any window interaction and drawing can be set
  1999. * by user input or by closing the window */
  2000. NK_WINDOW_MINIMIZED = NK_FLAG(12),
  2001. /* marks the window as minimized */
  2002. NK_WINDOW_SUB = NK_FLAG(13),
  2003. /* Marks the window as subwindow of another window*/
  2004. NK_WINDOW_GROUP = NK_FLAG(14),
  2005. /* Marks the window as window widget group */
  2006. NK_WINDOW_POPUP = NK_FLAG(15),
  2007. /* Marks the window as a popup window */
  2008. NK_WINDOW_NONBLOCK = NK_FLAG(16),
  2009. /* Marks the window as a nonblock popup window */
  2010. NK_WINDOW_CONTEXTUAL = NK_FLAG(17),
  2011. /* Marks the window as a combo box or menu */
  2012. NK_WINDOW_COMBO = NK_FLAG(18),
  2013. /* Marks the window as a combo box */
  2014. NK_WINDOW_MENU = NK_FLAG(19),
  2015. /* Marks the window as a menu */
  2016. NK_WINDOW_TOOLTIP = NK_FLAG(20),
  2017. /* Marks the window as a menu */
  2018. NK_WINDOW_REMOVE_ROM = NK_FLAG(21)
  2019. /* Removes the read only mode at the end of the window */
  2020. };
  2021. struct nk_popup_state {
  2022. struct nk_window *win;
  2023. enum nk_window_flags type;
  2024. nk_hash name;
  2025. int active;
  2026. unsigned combo_count;
  2027. unsigned con_count, con_old;
  2028. unsigned active_con;
  2029. };
  2030. struct nk_edit_state {
  2031. nk_hash name;
  2032. unsigned int seq;
  2033. unsigned int old;
  2034. int active, prev;
  2035. int cursor;
  2036. int sel_start;
  2037. int sel_end;
  2038. struct nk_scroll scrollbar;
  2039. unsigned char mode;
  2040. unsigned char single_line;
  2041. };
  2042. struct nk_property_state {
  2043. int active, prev;
  2044. char buffer[NK_MAX_NUMBER_BUFFER];
  2045. int length;
  2046. int cursor;
  2047. nk_hash name;
  2048. unsigned int seq;
  2049. unsigned int old;
  2050. int state;
  2051. };
  2052. struct nk_scaling_state {
  2053. struct nk_vec2 start_size;
  2054. int active;
  2055. };
  2056. struct nk_window {
  2057. unsigned int seq;
  2058. nk_hash name;
  2059. nk_flags flags;
  2060. struct nk_rect bounds;
  2061. struct nk_scroll scrollbar;
  2062. struct nk_command_buffer buffer;
  2063. struct nk_panel *layout;
  2064. /* persistent widget state */
  2065. struct nk_property_state property;
  2066. struct nk_popup_state popup;
  2067. struct nk_edit_state edit;
  2068. struct nk_scaling_state scaling;
  2069. struct nk_table *tables;
  2070. unsigned short table_count;
  2071. unsigned short table_size;
  2072. /* window list hooks */
  2073. struct nk_window *next;
  2074. struct nk_window *prev;
  2075. struct nk_window *parent;
  2076. };
  2077. /*==============================================================
  2078. * CONTEXT
  2079. * =============================================================*/
  2080. struct nk_page_element;
  2081. struct nk_context {
  2082. /* public: can be accessed freely */
  2083. struct nk_input input;
  2084. struct nk_style style;
  2085. struct nk_buffer memory;
  2086. struct nk_clipboard clip;
  2087. nk_flags last_widget_state;
  2088. /* private:
  2089. should only be accessed if you
  2090. know what you are doing */
  2091. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  2092. struct nk_draw_list draw_list;
  2093. #endif
  2094. #ifdef NK_INCLUDE_COMMAND_USERDATA
  2095. nk_handle userdata;
  2096. #endif
  2097. /* text editor objects are quite big because of an internal
  2098. * undo/redo stack. Therefore does not make sense to have one for
  2099. * each window for temporary use cases, so I only provide *one* instance
  2100. * for all windows. This works because the content is cleared anyway */
  2101. struct nk_text_edit text_edit;
  2102. /* windows */
  2103. int build;
  2104. void *pool;
  2105. struct nk_window *begin;
  2106. struct nk_window *end;
  2107. struct nk_window *active;
  2108. struct nk_window *current;
  2109. struct nk_page_element *freelist;
  2110. unsigned int count;
  2111. unsigned int seq;
  2112. };
  2113. #ifdef __cplusplus
  2114. }
  2115. #endif
  2116. #endif /* NK_H_ */
  2117. /*
  2118. * ==============================================================
  2119. *
  2120. * IMPLEMENTATION
  2121. *
  2122. * ===============================================================
  2123. */
  2124. #ifdef NK_IMPLEMENTATION
  2125. #ifndef NK_POOL_DEFAULT_CAPACITY
  2126. #define NK_POOL_DEFAULT_CAPACITY 16
  2127. #endif
  2128. #ifndef NK_DEFAULT_COMMAND_BUFFER_SIZE
  2129. #define NK_DEFAULT_COMMAND_BUFFER_SIZE (4*1024)
  2130. #endif
  2131. #ifndef NK_BUFFER_DEFAULT_INITIAL_SIZE
  2132. #define NK_BUFFER_DEFAULT_INITIAL_SIZE (4*1024)
  2133. #endif
  2134. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  2135. #include <stdlib.h> /* malloc, free */
  2136. #endif
  2137. #ifdef NK_INCLUDE_STANDARD_IO
  2138. #include <stdio.h> /* fopen, fclose,... */
  2139. #include <stdarg.h>
  2140. #endif
  2141. #ifndef NK_ASSERT
  2142. #include <assert.h>
  2143. #define NK_ASSERT(expr) assert(expr)
  2144. #endif
  2145. #ifndef NK_MEMSET
  2146. #define NK_MEMSET nk_memset
  2147. #endif
  2148. #ifndef NK_MEMCPY
  2149. #define NK_MEMCPY nk_memcopy
  2150. #endif
  2151. #ifndef NK_SQRT
  2152. #define NK_SQRT nk_sqrt
  2153. #endif
  2154. #ifndef NK_SIN
  2155. #define NK_SIN nk_sin
  2156. #endif
  2157. #ifndef NK_COS
  2158. #define NK_COS nk_cos
  2159. #endif
  2160. /* ==============================================================
  2161. * MATH
  2162. * =============================================================== */
  2163. #define NK_MIN(a,b) ((a) < (b) ? (a) : (b))
  2164. #define NK_MAX(a,b) ((a) < (b) ? (b) : (a))
  2165. #define NK_CLAMP(i,v,x) (NK_MAX(NK_MIN(v,x), i))
  2166. #define NK_PI 3.141592654f
  2167. #define NK_UTF_INVALID 0xFFFD
  2168. #define NK_MAX_FLOAT_PRECISION 2
  2169. #define NK_UNUSED(x) ((void)(x))
  2170. #define NK_SATURATE(x) (NK_MAX(0, NK_MIN(1.0f, x)))
  2171. #define NK_LEN(a) (sizeof(a)/sizeof(a)[0])
  2172. #define NK_ABS(a) (((a) < 0) ? -(a) : (a))
  2173. #define NK_BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  2174. #define NK_INBOX(px, py, x, y, w, h)\
  2175. (NK_BETWEEN(px,x,x+w) && NK_BETWEEN(py,y,y+h))
  2176. #define NK_INTERSECT(x0, y0, w0, h0, x1, y1, w1, h1) \
  2177. (!(((x1 > (x0 + w0)) || ((x1 + w1) < x0) || (y1 > (y0 + h0)) || (y1 + h1) < y0)))
  2178. #define NK_CONTAINS(x, y, w, h, bx, by, bw, bh)\
  2179. (NK_INBOX(x,y, bx, by, bw, bh) && NK_INBOX(x+w,y+h, bx, by, bw, bh))
  2180. #define nk_vec2_sub(a, b) nk_vec2((a).x - (b).x, (a).y - (b).y)
  2181. #define nk_vec2_add(a, b) nk_vec2((a).x + (b).x, (a).y + (b).y)
  2182. #define nk_vec2_len_sqr(a) ((a).x*(a).x+(a).y*(a).y)
  2183. #define nk_vec2_muls(a, t) nk_vec2((a).x * (t), (a).y * (t))
  2184. #define nk_ptr_add(t, p, i) ((t*)((void*)((nk_byte*)(p) + (i))))
  2185. #define nk_ptr_add_const(t, p, i) ((const t*)((const void*)((const nk_byte*)(p) + (i))))
  2186. #define nk_zero_struct(s) nk_zero(&s, sizeof(s))
  2187. /* ==============================================================
  2188. * ALIGNMENT
  2189. * =============================================================== */
  2190. /* Pointer to Integer type conversion for pointer alignment */
  2191. #if defined(__PTRDIFF_TYPE__) /* This case should work for GCC*/
  2192. # define NK_UINT_TO_PTR(x) ((void*)(__PTRDIFF_TYPE__)(x))
  2193. # define NK_PTR_TO_UINT(x) ((nk_size)(__PTRDIFF_TYPE__)(x))
  2194. #elif !defined(__GNUC__) /* works for compilers other than LLVM */
  2195. # define NK_UINT_TO_PTR(x) ((void*)&((char*)0)[x])
  2196. # define NK_PTR_TO_UINT(x) ((nk_size)(((char*)x)-(char*)0))
  2197. #elif defined(NK_USE_FIXED_TYPES) /* used if we have <stdint.h> */
  2198. # define NK_UINT_TO_PTR(x) ((void*)(uintptr_t)(x))
  2199. # define NK_PTR_TO_UINT(x) ((uintptr_t)(x))
  2200. #else /* generates warning but works */
  2201. # define NK_UINT_TO_PTR(x) ((void*)(x))
  2202. # define NK_PTR_TO_UINT(x) ((nk_size)(x))
  2203. #endif
  2204. #define NK_ALIGN_PTR(x, mask)\
  2205. (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x) + (mask-1)) & ~(mask-1))))
  2206. #define NK_ALIGN_PTR_BACK(x, mask)\
  2207. (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x)) & ~(mask-1))))
  2208. #define NK_OFFSETOF(st,m) ((nk_ptr)&(((st*)0)->m))
  2209. #define NK_CONTAINER_OF(ptr,type,member)\
  2210. (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member)))
  2211. #ifdef __cplusplus
  2212. template<typename T> struct nk_alignof;
  2213. template<typename T, int size_diff> struct nk_helper{enum {value = size_diff};};
  2214. template<typename T> struct nk_helper<T,0>{enum {value = nk_alignof<T>::value};};
  2215. template<typename T> struct nk_alignof{struct Big {T x; char c;}; enum {
  2216. diff = sizeof(Big) - sizeof(T), value = nk_helper<Big, diff>::value};};
  2217. #define NK_ALIGNOF(t) (nk_alignof<t>::value);
  2218. #else
  2219. #define NK_ALIGNOF(t) ((char*)(&((struct {char c; t _h;}*)0)->_h) - (char*)0)
  2220. #endif
  2221. /* make sure correct type size */
  2222. typedef int nk__check_size[(sizeof(nk_size) >= sizeof(void*)) ? 1 : -1];
  2223. typedef int nk__check_ptr[(sizeof(nk_ptr) == sizeof(void*)) ? 1 : -1];
  2224. typedef int nk__check_flags[(sizeof(nk_flags) >= 4) ? 1 : -1];
  2225. typedef int nk__check_rune[(sizeof(nk_rune) >= 4) ? 1 : -1];
  2226. typedef int nk__check_ushort[(sizeof(nk_ushort) == 2) ? 1 : -1];
  2227. typedef int nk__check_short[(sizeof(nk_short) == 2) ? 1 : -1];
  2228. typedef int nk__check_uint[(sizeof(nk_uint) == 4) ? 1 : -1];
  2229. typedef int nk__check_int[(sizeof(nk_int) == 4) ? 1 : -1];
  2230. typedef int nk__check_byte[(sizeof(nk_byte) == 1) ? 1 : -1];
  2231. NK_GLOBAL const struct nk_rect nk_null_rect = {-8192.0f, -8192.0f, 16384, 16384};
  2232. NK_GLOBAL const float NK_FLOAT_PRECISION = 0.00000000000001f;
  2233. /*
  2234. * ==============================================================
  2235. *
  2236. * MATH
  2237. *
  2238. * ===============================================================
  2239. */
  2240. /* Since nuklear is supposed to work on all systems providing floating point
  2241. math without any dependencies I also had to implement my own math functions
  2242. for sqrt, sin and cos. Since the actual highly accurate implementations for
  2243. the standard library functions are quite complex and I do not need high
  2244. precision for my use cases I use approximations.
  2245. Sqrt
  2246. ----
  2247. For square root nuklear uses the famous fast inverse square root:
  2248. https://en.wikipedia.org/wiki/Fast_inverse_square_root with
  2249. slightly tweaked magic constant. While on todays hardware it is
  2250. probably not faster it is still fast and accurate enough for
  2251. nuklear's use cases. IMPORTANT: this requires float format IEEE 754
  2252. Sine/Cosine
  2253. -----------
  2254. All constants inside both function are generated Remez's minimax
  2255. approximations for value range 0...2*PI. The reason why I decided to
  2256. approximate exactly that range is that nuklear only needs sine and
  2257. cosine to generate circles which only requires that exact range.
  2258. In addition I used Remez instead of Taylor for additional precision:
  2259. www.lolengine.net/blog/2011/12/21/better-function-approximatations.
  2260. The tool I used to generate constants for both sine and cosine
  2261. (it can actually approximate a lot more functions) can be
  2262. found here: www.lolengine.net/wiki/oss/lolremez
  2263. */
  2264. NK_INTERN float
  2265. nk_inv_sqrt(float number)
  2266. {
  2267. float x2;
  2268. const float threehalfs = 1.5f;
  2269. union {nk_uint i; float f;} conv = {0};
  2270. conv.f = number;
  2271. x2 = number * 0.5f;
  2272. conv.i = 0x5f375A84 - (conv.i >> 1);
  2273. conv.f = conv.f * (threehalfs - (x2 * conv.f * conv.f));
  2274. return conv.f;
  2275. }
  2276. NK_INTERN float
  2277. nk_sqrt(float x)
  2278. {
  2279. return x * nk_inv_sqrt(x);
  2280. }
  2281. NK_INTERN float
  2282. nk_sin(float x)
  2283. {
  2284. NK_STORAGE const float a0 = +1.91059300966915117e-31f;
  2285. NK_STORAGE const float a1 = +1.00086760103908896f;
  2286. NK_STORAGE const float a2 = -1.21276126894734565e-2f;
  2287. NK_STORAGE const float a3 = -1.38078780785773762e-1f;
  2288. NK_STORAGE const float a4 = -2.67353392911981221e-2f;
  2289. NK_STORAGE const float a5 = +2.08026600266304389e-2f;
  2290. NK_STORAGE const float a6 = -3.03996055049204407e-3f;
  2291. NK_STORAGE const float a7 = +1.38235642404333740e-4f;
  2292. return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*a7))))));
  2293. }
  2294. NK_INTERN float
  2295. nk_cos(float x)
  2296. {
  2297. NK_STORAGE const float a0 = +1.00238601909309722f;
  2298. NK_STORAGE const float a1 = -3.81919947353040024e-2f;
  2299. NK_STORAGE const float a2 = -3.94382342128062756e-1f;
  2300. NK_STORAGE const float a3 = -1.18134036025221444e-1f;
  2301. NK_STORAGE const float a4 = +1.07123798512170878e-1f;
  2302. NK_STORAGE const float a5 = -1.86637164165180873e-2f;
  2303. NK_STORAGE const float a6 = +9.90140908664079833e-4f;
  2304. NK_STORAGE const float a7 = -5.23022132118824778e-14f;
  2305. return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*a7))))));
  2306. }
  2307. NK_INTERN nk_uint
  2308. nk_round_up_pow2(nk_uint v)
  2309. {
  2310. v--;
  2311. v |= v >> 1;
  2312. v |= v >> 2;
  2313. v |= v >> 4;
  2314. v |= v >> 8;
  2315. v |= v >> 16;
  2316. v++;
  2317. return v;
  2318. }
  2319. NK_API struct nk_rect
  2320. nk_get_null_rect(void)
  2321. {
  2322. return nk_null_rect;
  2323. }
  2324. NK_API struct nk_rect
  2325. nk_rect(float x, float y, float w, float h)
  2326. {
  2327. struct nk_rect r;
  2328. r.x = x, r.y = y;
  2329. r.w = w, r.h = h;
  2330. return r;
  2331. }
  2332. NK_API struct nk_rect
  2333. nk_recti(int x, int y, int w, int h)
  2334. {
  2335. struct nk_rect r;
  2336. r.x = (float)x;
  2337. r.y = (float)y;
  2338. r.w = (float)w;
  2339. r.h = (float)h;
  2340. return r;
  2341. }
  2342. NK_API struct nk_rect
  2343. nk_recta(struct nk_vec2 pos, struct nk_vec2 size)
  2344. {
  2345. return nk_rect(pos.x, pos.y, size.x, size.y);
  2346. }
  2347. NK_API struct nk_rect
  2348. nk_rectv(const float *r)
  2349. {
  2350. return nk_rect(r[0], r[1], r[2], r[3]);
  2351. }
  2352. NK_API struct nk_rect
  2353. nk_rectiv(const int *r)
  2354. {
  2355. return nk_recti(r[0], r[1], r[2], r[3]);
  2356. }
  2357. NK_API struct nk_vec2
  2358. nk_rect_pos(struct nk_rect r)
  2359. {
  2360. struct nk_vec2 ret;
  2361. ret.x = r.x; ret.y = r.y;
  2362. return ret;
  2363. }
  2364. NK_API struct nk_vec2
  2365. nk_rect_size(struct nk_rect r)
  2366. {
  2367. struct nk_vec2 ret;
  2368. ret.x = r.w; ret.y = r.h;
  2369. return ret;
  2370. }
  2371. NK_INTERN struct nk_rect
  2372. nk_shrink_rect(struct nk_rect r, float amount)
  2373. {
  2374. struct nk_rect res;
  2375. r.w = NK_MAX(r.w, 2 * amount);
  2376. r.h = NK_MAX(r.h, 2 * amount);
  2377. res.x = r.x + amount;
  2378. res.y = r.y + amount;
  2379. res.w = r.w - 2 * amount;
  2380. res.h = r.h - 2 * amount;
  2381. return res;
  2382. }
  2383. NK_INTERN struct nk_rect
  2384. nk_pad_rect(struct nk_rect r, struct nk_vec2 pad)
  2385. {
  2386. r.w = NK_MAX(r.w, 2 * pad.x);
  2387. r.h = NK_MAX(r.h, 2 * pad.y);
  2388. r.x += pad.x; r.y += pad.y;
  2389. r.w -= 2 * pad.x;
  2390. r.h -= 2 * pad.y;
  2391. return r;
  2392. }
  2393. NK_API struct nk_vec2
  2394. nk_vec2(float x, float y)
  2395. {
  2396. struct nk_vec2 ret;
  2397. ret.x = x; ret.y = y;
  2398. return ret;
  2399. }
  2400. NK_API struct nk_vec2
  2401. nk_vec2i(int x, int y)
  2402. {
  2403. struct nk_vec2 ret;
  2404. ret.x = (float)x;
  2405. ret.y = (float)y;
  2406. return ret;
  2407. }
  2408. NK_API struct nk_vec2
  2409. nk_vec2v(const float *v)
  2410. {
  2411. return nk_vec2(v[0], v[1]);
  2412. }
  2413. NK_API struct nk_vec2
  2414. nk_vec2iv(const int *v)
  2415. {
  2416. return nk_vec2i(v[0], v[1]);
  2417. }
  2418. /*
  2419. * ==============================================================
  2420. *
  2421. * UTIL
  2422. *
  2423. * ===============================================================
  2424. */
  2425. NK_INTERN int nk_str_match_here(const char *regexp, const char *text);
  2426. NK_INTERN int nk_str_match_star(int c, const char *regexp, const char *text);
  2427. NK_INTERN int nk_is_lower(int c) {return (c >= 'a' && c <= 'z') || (c >= 0xE0 && c <= 0xFF);}
  2428. NK_INTERN int nk_is_upper(int c){return (c >= 'A' && c <= 'Z') || (c >= 0xC0 && c <= 0xDF);}
  2429. NK_INTERN int nk_to_upper(int c) {return (c >= 'a' && c <= 'z') ? (c - ('a' - 'A')) : c;}
  2430. NK_INTERN int nk_to_lower(int c) {return (c >= 'A' && c <= 'Z') ? (c - ('a' + 'A')) : c;}
  2431. NK_INTERN void*
  2432. nk_memcopy(void *dst0, const void *src0, nk_size length)
  2433. {
  2434. nk_ptr t;
  2435. char *dst = (char*)dst0;
  2436. const char *src = (const char*)src0;
  2437. if (length == 0 || dst == src)
  2438. goto done;
  2439. #define nk_word int
  2440. #define nk_wsize sizeof(nk_word)
  2441. #define nk_wmask (nk_wsize-1)
  2442. #define NK_TLOOP(s) if (t) NK_TLOOP1(s)
  2443. #define NK_TLOOP1(s) do { s; } while (--t)
  2444. if (dst < src) {
  2445. t = (nk_ptr)src; /* only need low bits */
  2446. if ((t | (nk_ptr)dst) & nk_wmask) {
  2447. if ((t ^ (nk_ptr)dst) & nk_wmask || length < nk_wsize)
  2448. t = length;
  2449. else
  2450. t = nk_wsize - (t & nk_wmask);
  2451. length -= t;
  2452. NK_TLOOP1(*dst++ = *src++);
  2453. }
  2454. t = length / nk_wsize;
  2455. NK_TLOOP(*(nk_word*)(void*)dst = *(const nk_word*)(const void*)src;
  2456. src += nk_wsize; dst += nk_wsize);
  2457. t = length & nk_wmask;
  2458. NK_TLOOP(*dst++ = *src++);
  2459. } else {
  2460. src += length;
  2461. dst += length;
  2462. t = (nk_ptr)src;
  2463. if ((t | (nk_ptr)dst) & nk_wmask) {
  2464. if ((t ^ (nk_ptr)dst) & nk_wmask || length <= nk_wsize)
  2465. t = length;
  2466. else
  2467. t &= nk_wmask;
  2468. length -= t;
  2469. NK_TLOOP1(*--dst = *--src);
  2470. }
  2471. t = length / nk_wsize;
  2472. NK_TLOOP(src -= nk_wsize; dst -= nk_wsize;
  2473. *(nk_word*)(void*)dst = *(const nk_word*)(const void*)src);
  2474. t = length & nk_wmask;
  2475. NK_TLOOP(*--dst = *--src);
  2476. }
  2477. #undef nk_word
  2478. #undef nk_wsize
  2479. #undef nk_wmask
  2480. #undef NK_TLOOP
  2481. #undef NK_TLOOP1
  2482. done:
  2483. return (dst0);
  2484. }
  2485. NK_INTERN void
  2486. nk_memset(void *ptr, int c0, nk_size size)
  2487. {
  2488. #define nk_word unsigned
  2489. #define nk_wsize sizeof(nk_word)
  2490. #define nk_wmask (nk_wsize - 1)
  2491. nk_byte *dst = (nk_byte*)ptr;
  2492. unsigned c = 0;
  2493. nk_size t = 0;
  2494. if ((c = (nk_byte)c0) != 0) {
  2495. c = (c << 8) | c; /* at least 16-bits */
  2496. if (sizeof(unsigned int) > 2)
  2497. c = (c << 16) | c; /* at least 32-bits*/
  2498. }
  2499. /* too small of a word count */
  2500. dst = (nk_byte*)ptr;
  2501. if (size < 3 * nk_wsize) {
  2502. while (size--) *dst++ = (nk_byte)c0;
  2503. return;
  2504. }
  2505. /* align destination */
  2506. if ((t = NK_PTR_TO_UINT(dst) & nk_wmask) != 0) {
  2507. t = nk_wsize -t;
  2508. size -= t;
  2509. do {
  2510. *dst++ = (nk_byte)c0;
  2511. } while (--t != 0);
  2512. }
  2513. /* fill word */
  2514. t = size / nk_wsize;
  2515. do {
  2516. *(nk_word*)((void*)dst) = c;
  2517. dst += nk_wsize;
  2518. } while (--t != 0);
  2519. /* fill trailing bytes */
  2520. t = (size & nk_wmask);
  2521. if (t != 0) {
  2522. do {
  2523. *dst++ = (nk_byte)c0;
  2524. } while (--t != 0);
  2525. }
  2526. #undef nk_word
  2527. #undef nk_wsize
  2528. #undef nk_wmask
  2529. }
  2530. NK_INTERN void
  2531. nk_zero(void *ptr, nk_size size)
  2532. {
  2533. NK_ASSERT(ptr);
  2534. NK_MEMSET(ptr, 0, size);
  2535. }
  2536. NK_API int
  2537. nk_strlen(const char *str)
  2538. {
  2539. int siz = 0;
  2540. NK_ASSERT(str);
  2541. while (str && *str++ != '\0') siz++;
  2542. return siz;
  2543. }
  2544. NK_API int
  2545. nk_strtof(float *number, const char *buffer)
  2546. {
  2547. float m;
  2548. float neg = 1.0f;
  2549. const char *p = buffer;
  2550. float floatvalue = 0;
  2551. NK_ASSERT(number);
  2552. NK_ASSERT(buffer);
  2553. if (!number || !buffer) return 0;
  2554. *number = 0;
  2555. /* skip whitespace */
  2556. while (*p && *p == ' ') p++;
  2557. if (*p == '-') {
  2558. neg = -1.0f;
  2559. p++;
  2560. }
  2561. while( *p && *p != '.' && *p != 'e' ) {
  2562. floatvalue = floatvalue * 10.0f + (float) (*p - '0');
  2563. p++;
  2564. }
  2565. if ( *p == '.' ) {
  2566. p++;
  2567. for(m = 0.1f; *p && *p != 'e'; p++ ) {
  2568. floatvalue = floatvalue + (float) (*p - '0') * m;
  2569. m *= 0.1f;
  2570. }
  2571. }
  2572. if ( *p == 'e' ) {
  2573. int i, pow, div;
  2574. p++;
  2575. if ( *p == '-' ) {
  2576. div = nk_true;
  2577. p++;
  2578. } else if ( *p == '+' ) {
  2579. div = nk_false;
  2580. p++;
  2581. } else div = nk_false;
  2582. for ( pow = 0; *p; p++ )
  2583. pow = pow * 10 + (int) (*p - '0');
  2584. for ( m = 1.0, i = 0; i < pow; i++ )
  2585. m *= 10.0f;
  2586. if ( div )
  2587. floatvalue /= m;
  2588. else floatvalue *= m;
  2589. }
  2590. *number = floatvalue * neg;
  2591. return 1;
  2592. }
  2593. NK_API int
  2594. nk_stricmp(const char *s1, const char *s2)
  2595. {
  2596. nk_int c1,c2,d;
  2597. do {
  2598. c1 = *s1++;
  2599. c2 = *s2++;
  2600. d = c1 - c2;
  2601. while (d) {
  2602. if (c1 <= 'Z' && c1 >= 'A') {
  2603. d += ('a' - 'A');
  2604. if (!d) break;
  2605. }
  2606. if (c2 <= 'Z' && c2 >= 'A') {
  2607. d -= ('a' - 'A');
  2608. if (!d) break;
  2609. }
  2610. return ((d >= 0) << 1) - 1;
  2611. }
  2612. } while (c1);
  2613. return 0;
  2614. }
  2615. NK_API int
  2616. nk_stricmpn(const char *s1, const char *s2, int n)
  2617. {
  2618. int c1,c2,d;
  2619. NK_ASSERT(n >= 0);
  2620. do {
  2621. c1 = *s1++;
  2622. c2 = *s2++;
  2623. if (!n--) return 0;
  2624. d = c1 - c2;
  2625. while (d) {
  2626. if (c1 <= 'Z' && c1 >= 'A') {
  2627. d += ('a' - 'A');
  2628. if (!d) break;
  2629. }
  2630. if (c2 <= 'Z' && c2 >= 'A') {
  2631. d -= ('a' - 'A');
  2632. if (!d) break;
  2633. }
  2634. return ((d >= 0) << 1) - 1;
  2635. }
  2636. } while (c1);
  2637. return 0;
  2638. }
  2639. NK_INTERN int
  2640. nk_str_match_here(const char *regexp, const char *text)
  2641. {
  2642. if (regexp[0] == '\0')
  2643. return 1;
  2644. if (regexp[1] == '*')
  2645. return nk_str_match_star(regexp[0], regexp+2, text);
  2646. if (regexp[0] == '$' && regexp[1] == '\0')
  2647. return *text == '\0';
  2648. if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text))
  2649. return nk_str_match_here(regexp+1, text+1);
  2650. return 0;
  2651. }
  2652. NK_INTERN int
  2653. nk_str_match_star(int c, const char *regexp, const char *text)
  2654. {
  2655. do {/* a '* matches zero or more instances */
  2656. if (nk_str_match_here(regexp, text))
  2657. return 1;
  2658. } while (*text != '\0' && (*text++ == c || c == '.'));
  2659. return 0;
  2660. }
  2661. NK_API int
  2662. nk_strfilter(const char *text, const char *regexp)
  2663. {
  2664. /*
  2665. c matches any literal character c
  2666. . matches any single character
  2667. ^ matches the beginning of the input string
  2668. $ matches the end of the input string
  2669. * matches zero or more occurrences of the previous character*/
  2670. if (regexp[0] == '^')
  2671. return nk_str_match_here(regexp+1, text);
  2672. do { /* must look even if string is empty */
  2673. if (nk_str_match_here(regexp, text))
  2674. return 1;
  2675. } while (*text++ != '\0');
  2676. return 0;
  2677. }
  2678. NK_API int
  2679. nk_strmatch_fuzzy_text(const char *str, int str_len,
  2680. const char *pattern, int *out_score)
  2681. {
  2682. /* Returns true if each character in pattern is found sequentially within str
  2683. * if found then outScore is also set. Score value has no intrinsic meaning.
  2684. * Range varies with pattern. Can only compare scores with same search pattern. */
  2685. /* ------- scores --------- */
  2686. /* bonus for adjacent matches */
  2687. #define NK_ADJACENCY_BONUS 5
  2688. /* bonus if match occurs after a separator */
  2689. #define NK_SEPARATOR_BONUS 10
  2690. /* bonus if match is uppercase and prev is lower */
  2691. #define NK_CAMEL_BONUS 10
  2692. /* penalty applied for every letter in str before the first match */
  2693. #define NK_LEADING_LETTER_PENALTY (-3)
  2694. /* maximum penalty for leading letters */
  2695. #define NK_MAX_LEADING_LETTER_PENALTY (-9)
  2696. /* penalty for every letter that doesn't matter */
  2697. #define NK_UNMATCHED_LETTER_PENALTY (-1)
  2698. /* loop variables */
  2699. int score = 0;
  2700. char const * pattern_iter = pattern;
  2701. int str_iter = 0;
  2702. int prev_matched = nk_false;
  2703. int prev_lower = nk_false;
  2704. /* true so if first letter match gets separator bonus*/
  2705. int prev_separator = nk_true;
  2706. /* use "best" matched letter if multiple string letters match the pattern */
  2707. char const * best_letter = 0;
  2708. int best_letter_score = 0;
  2709. /* loop over strings */
  2710. NK_ASSERT(str);
  2711. NK_ASSERT(pattern);
  2712. if (!str || !str_len || !pattern) return 0;
  2713. while (str_iter < str_len)
  2714. {
  2715. const char pattern_letter = *pattern_iter;
  2716. const char str_letter = str[str_iter];
  2717. int next_match = *pattern_iter != '\0' &&
  2718. nk_to_lower(pattern_letter) == nk_to_lower(str_letter);
  2719. int rematch = best_letter && nk_to_lower(*best_letter) == nk_to_lower(str_letter);
  2720. int advanced = next_match && best_letter;
  2721. int pattern_repeat = best_letter && *pattern_iter != '\0';
  2722. pattern_repeat = pattern_repeat &&
  2723. nk_to_lower(*best_letter) == nk_to_lower(pattern_letter);
  2724. if (advanced || pattern_repeat) {
  2725. score += best_letter_score;
  2726. best_letter = 0;
  2727. best_letter_score = 0;
  2728. }
  2729. if (next_match || rematch)
  2730. {
  2731. int new_score = 0;
  2732. /* Apply penalty for each letter before the first pattern match */
  2733. if (pattern_iter == pattern)
  2734. {
  2735. int count = (int)(&str[str_iter] - str);
  2736. int penalty = NK_LEADING_LETTER_PENALTY * count;
  2737. if (penalty < NK_MAX_LEADING_LETTER_PENALTY)
  2738. penalty = NK_MAX_LEADING_LETTER_PENALTY;
  2739. score += penalty;
  2740. }
  2741. /* apply bonus for consecutive bonuses */
  2742. if (prev_matched)
  2743. new_score += NK_ADJACENCY_BONUS;
  2744. /* apply bonus for matches after a separator */
  2745. if (prev_separator)
  2746. new_score += NK_SEPARATOR_BONUS;
  2747. /* apply bonus across camel case boundaries */
  2748. if (prev_lower && nk_is_upper(str_letter))
  2749. new_score += NK_CAMEL_BONUS;
  2750. /* update pattern iter IFF the next pattern letter was matched */
  2751. if (next_match)
  2752. ++pattern_iter;
  2753. /* update best letter in str which may be for a "next" letter or a rematch */
  2754. if (new_score >= best_letter_score)
  2755. {
  2756. /* apply penalty for now skipped letter */
  2757. if (best_letter != 0)
  2758. score += NK_UNMATCHED_LETTER_PENALTY;
  2759. best_letter = &str[str_iter];
  2760. best_letter_score = new_score;
  2761. }
  2762. prev_matched = nk_true;
  2763. }
  2764. else
  2765. {
  2766. score += NK_UNMATCHED_LETTER_PENALTY;
  2767. prev_matched = nk_false;
  2768. }
  2769. /* separators should be more easily defined */
  2770. prev_lower = nk_is_lower(str_letter) != 0;
  2771. prev_separator = str_letter == '_' || str_letter == ' ';
  2772. ++str_iter;
  2773. }
  2774. /* apply score for last match */
  2775. if (best_letter)
  2776. score += best_letter_score;
  2777. /* did not match full pattern */
  2778. if (*pattern_iter != '\0')
  2779. return nk_false;
  2780. if (out_score)
  2781. *out_score = score;
  2782. return nk_true;
  2783. }
  2784. NK_API int
  2785. nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score)
  2786. {return nk_strmatch_fuzzy_text(str, nk_strlen(str), pattern, out_score);}
  2787. #ifdef NK_INCLUDE_STANDARD_IO
  2788. NK_API int
  2789. nk_strfmt(char *buf, int buf_size, const char *fmt,...)
  2790. {
  2791. int w;
  2792. va_list args;
  2793. va_start(args, fmt);
  2794. w = vsnprintf(buf, (nk_size)buf_size, fmt, args);
  2795. va_end(args);
  2796. buf[buf_size-1] = 0;
  2797. return (w == -1) ?(int)buf_size:w;
  2798. }
  2799. NK_INTERN int
  2800. nk_strfmtv(char *buf, int buf_size, const char *fmt, va_list args)
  2801. {
  2802. int w = vsnprintf(buf, (nk_size)buf_size, fmt, args);
  2803. buf[buf_size-1] = 0;
  2804. return (w == -1) ? (int)buf_size:w;
  2805. }
  2806. #endif
  2807. NK_INTERN int
  2808. nk_string_float_limit(char *string, int prec)
  2809. {
  2810. int dot = 0;
  2811. char *c = string;
  2812. while (*c) {
  2813. if (*c == '.') {
  2814. dot = 1;
  2815. c++;
  2816. continue;
  2817. }
  2818. if (dot == (prec+1)) {
  2819. *c = 0;
  2820. break;
  2821. }
  2822. if (dot > 0) dot++;
  2823. c++;
  2824. }
  2825. return (int)(c - string);
  2826. }
  2827. NK_INTERN float
  2828. nk_pow(float x, int n)
  2829. {
  2830. /* check the sign of n */
  2831. float r = 1;
  2832. int plus = n >= 0;
  2833. n = (plus) ? n : -n;
  2834. while (n > 0) {
  2835. if ((n & 1) == 1)
  2836. r *= x;
  2837. n /= 2;
  2838. x *= x;
  2839. }
  2840. return plus ? r : 1.0f / r;
  2841. }
  2842. NK_INTERN int
  2843. nk_ifloor(float x)
  2844. {
  2845. x = (float)((int)x - ((x < 0.0) ? 1 : 0));
  2846. return (int)x;
  2847. }
  2848. NK_INTERN int
  2849. nk_iceil(float x)
  2850. {
  2851. if (x >= 0) {
  2852. int i = (int)x;
  2853. return i;
  2854. } else {
  2855. int t = (int)x;
  2856. float r = x - (float)t;
  2857. return (r > 0.0f) ? t+1: t;
  2858. }
  2859. }
  2860. NK_INTERN int
  2861. nk_log10(float n)
  2862. {
  2863. int neg;
  2864. int ret;
  2865. int exp = 0;
  2866. neg = (n < 0) ? 1 : 0;
  2867. ret = (neg) ? (int)-n : (int)n;
  2868. while ((ret / 10) > 0) {
  2869. ret /= 10;
  2870. exp++;
  2871. }
  2872. if (neg) exp = -exp;
  2873. return exp;
  2874. }
  2875. NK_INTERN int
  2876. nk_ftos(char *s, float n)
  2877. {
  2878. int useExp = 0;
  2879. int digit = 0, m = 0, m1 = 0;
  2880. char *c = s;
  2881. int neg = 0;
  2882. if (n == 0.0f) {
  2883. s[0] = '0'; s[1] = '\0';
  2884. return 1;
  2885. }
  2886. neg = (n < 0);
  2887. if (neg) n = -n;
  2888. /* calculate magnitude */
  2889. m = nk_log10(n);
  2890. useExp = (m >= 14 || (neg && m >= 9) || m <= -9);
  2891. if (neg) *(c++) = '-';
  2892. /* set up for scientific notation */
  2893. if (useExp) {
  2894. if (m < 0)
  2895. m -= 1;
  2896. n = n / nk_pow(10.0, m);
  2897. m1 = m;
  2898. m = 0;
  2899. }
  2900. if (m < 1.0) {
  2901. m = 0;
  2902. }
  2903. /* convert the number */
  2904. while (n > NK_FLOAT_PRECISION || m >= 0) {
  2905. float weight = nk_pow(10.0, m);
  2906. if (weight > 0) {
  2907. float t = (float)n / weight;
  2908. digit = nk_ifloor(t);
  2909. n -= ((float)digit * weight);
  2910. *(c++) = (char)('0' + (char)digit);
  2911. }
  2912. if (m == 0 && n > 0)
  2913. *(c++) = '.';
  2914. m--;
  2915. }
  2916. if (useExp) {
  2917. /* convert the exponent */
  2918. int i, j;
  2919. *(c++) = 'e';
  2920. if (m1 > 0) {
  2921. *(c++) = '+';
  2922. } else {
  2923. *(c++) = '-';
  2924. m1 = -m1;
  2925. }
  2926. m = 0;
  2927. while (m1 > 0) {
  2928. *(c++) = (char)('0' + (char)(m1 % 10));
  2929. m1 /= 10;
  2930. m++;
  2931. }
  2932. c -= m;
  2933. for (i = 0, j = m-1; i<j; i++, j--) {
  2934. /* swap without temporary */
  2935. c[i] ^= c[j];
  2936. c[j] ^= c[i];
  2937. c[i] ^= c[j];
  2938. }
  2939. c += m;
  2940. }
  2941. *(c) = '\0';
  2942. return (int)(c - s);
  2943. }
  2944. NK_API nk_hash
  2945. nk_murmur_hash(const void * key, int len, nk_hash seed)
  2946. {
  2947. /* 32-Bit MurmurHash3: https://code.google.com/p/smhasher/wiki/MurmurHash3*/
  2948. #define NK_ROTL(x,r) ((x) << (r) | ((x) >> (32 - r)))
  2949. union {const nk_uint *i; const nk_byte *b;} conv = {0};
  2950. const nk_byte *data = (const nk_byte*)key;
  2951. const int nblocks = len/4;
  2952. nk_uint h1 = seed;
  2953. const nk_uint c1 = 0xcc9e2d51;
  2954. const nk_uint c2 = 0x1b873593;
  2955. const nk_byte *tail;
  2956. const nk_uint *blocks;
  2957. nk_uint k1;
  2958. int i;
  2959. /* body */
  2960. if (!key) return 0;
  2961. conv.b = (data + nblocks*4);
  2962. blocks = (const nk_uint*)conv.i;
  2963. for (i = -nblocks; i; ++i) {
  2964. k1 = blocks[i];
  2965. k1 *= c1;
  2966. k1 = NK_ROTL(k1,15);
  2967. k1 *= c2;
  2968. h1 ^= k1;
  2969. h1 = NK_ROTL(h1,13);
  2970. h1 = h1*5+0xe6546b64;
  2971. }
  2972. /* tail */
  2973. tail = (const nk_byte*)(data + nblocks*4);
  2974. k1 = 0;
  2975. switch (len & 3) {
  2976. case 3: k1 ^= (nk_uint)(tail[2] << 16);
  2977. case 2: k1 ^= (nk_uint)(tail[1] << 8u);
  2978. case 1: k1 ^= tail[0];
  2979. k1 *= c1;
  2980. k1 = NK_ROTL(k1,15);
  2981. k1 *= c2;
  2982. h1 ^= k1;
  2983. default: break;
  2984. }
  2985. /* finalization */
  2986. h1 ^= (nk_uint)len;
  2987. /* fmix32 */
  2988. h1 ^= h1 >> 16;
  2989. h1 *= 0x85ebca6b;
  2990. h1 ^= h1 >> 13;
  2991. h1 *= 0xc2b2ae35;
  2992. h1 ^= h1 >> 16;
  2993. #undef NK_ROTL
  2994. return h1;
  2995. }
  2996. #ifdef NK_INCLUDE_STANDARD_IO
  2997. NK_INTERN char*
  2998. nk_file_load(const char* path, nk_size* siz, struct nk_allocator *alloc)
  2999. {
  3000. char *buf;
  3001. FILE *fd;
  3002. long ret;
  3003. NK_ASSERT(path);
  3004. NK_ASSERT(siz);
  3005. NK_ASSERT(alloc);
  3006. if (!path || !siz || !alloc)
  3007. return 0;
  3008. fd = fopen(path, "rb");
  3009. if (!fd) return 0;
  3010. fseek(fd, 0, SEEK_END);
  3011. ret = ftell(fd);
  3012. if (ret < 0) {
  3013. fclose(fd);
  3014. return 0;
  3015. }
  3016. *siz = (nk_size)ret;
  3017. fseek(fd, 0, SEEK_SET);
  3018. buf = (char*)alloc->alloc(alloc->userdata,0, *siz);
  3019. NK_ASSERT(buf);
  3020. if (!buf) {
  3021. fclose(fd);
  3022. return 0;
  3023. }
  3024. *siz = (nk_size)fread(buf, *siz, 1, fd);
  3025. fclose(fd);
  3026. return buf;
  3027. }
  3028. #endif
  3029. /*
  3030. * ==============================================================
  3031. *
  3032. * COLOR
  3033. *
  3034. * ===============================================================
  3035. */
  3036. NK_INTERN int
  3037. nk_parse_hex(const char *p, int length)
  3038. {
  3039. int i = 0;
  3040. int len = 0;
  3041. while (len < length) {
  3042. i <<= 4;
  3043. if (p[len] >= 'a' && p[len] <= 'f')
  3044. i += ((p[len] - 'a') + 10);
  3045. else if (p[len] >= 'A' && p[len] <= 'F') {
  3046. i += ((p[len] - 'A') + 10);
  3047. } else i += (p[len] - '0');
  3048. len++;
  3049. }
  3050. return i;
  3051. }
  3052. NK_API struct nk_color
  3053. nk_rgba(int r, int g, int b, int a)
  3054. {
  3055. struct nk_color ret;
  3056. ret.r = (nk_byte)NK_CLAMP(0, r, 255);
  3057. ret.g = (nk_byte)NK_CLAMP(0, g, 255);
  3058. ret.b = (nk_byte)NK_CLAMP(0, b, 255);
  3059. ret.a = (nk_byte)NK_CLAMP(0, a, 255);
  3060. return ret;
  3061. }
  3062. NK_API struct nk_color
  3063. nk_rgb_hex(const char *rgb)
  3064. {
  3065. struct nk_color col;
  3066. const char *c = rgb;
  3067. if (*c == '#') c++;
  3068. col.r = (nk_byte)nk_parse_hex(c, 2);
  3069. col.g = (nk_byte)nk_parse_hex(c+2, 2);
  3070. col.b = (nk_byte)nk_parse_hex(c+4, 2);
  3071. col.a = 255;
  3072. return col;
  3073. }
  3074. NK_API struct nk_color
  3075. nk_rgba_hex(const char *rgb)
  3076. {
  3077. struct nk_color col;
  3078. const char *c = rgb;
  3079. if (*c == '#') c++;
  3080. col.r = (nk_byte)nk_parse_hex(c, 2);
  3081. col.g = (nk_byte)nk_parse_hex(c+2, 2);
  3082. col.b = (nk_byte)nk_parse_hex(c+4, 2);
  3083. col.a = (nk_byte)nk_parse_hex(c+6, 2);
  3084. return col;
  3085. }
  3086. NK_API void
  3087. nk_color_hex_rgba(char *output, struct nk_color col)
  3088. {
  3089. #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
  3090. output[0] = (char)NK_TO_HEX((col.r & 0x0F));
  3091. output[1] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
  3092. output[2] = (char)NK_TO_HEX((col.g & 0x0F));
  3093. output[3] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
  3094. output[4] = (char)NK_TO_HEX((col.b & 0x0F));
  3095. output[5] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
  3096. output[6] = (char)NK_TO_HEX((col.a & 0x0F));
  3097. output[7] = (char)NK_TO_HEX((col.a & 0xF0) >> 4);
  3098. output[8] = '\0';
  3099. #undef NK_TO_HEX
  3100. }
  3101. NK_API void
  3102. nk_color_hex_rgb(char *output, struct nk_color col)
  3103. {
  3104. #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
  3105. output[0] = (char)NK_TO_HEX((col.r & 0x0F));
  3106. output[1] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
  3107. output[2] = (char)NK_TO_HEX((col.g & 0x0F));
  3108. output[3] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
  3109. output[4] = (char)NK_TO_HEX((col.b & 0x0F));
  3110. output[5] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
  3111. output[6] = '\0';
  3112. #undef NK_TO_HEX
  3113. }
  3114. NK_API struct nk_color
  3115. nk_rgba_iv(const int *c)
  3116. {
  3117. return nk_rgba(c[0], c[1], c[2], c[3]);
  3118. }
  3119. NK_API struct nk_color
  3120. nk_rgba_bv(const nk_byte *c)
  3121. {
  3122. return nk_rgba(c[0], c[1], c[2], c[3]);
  3123. }
  3124. NK_API struct nk_color
  3125. nk_rgb(int r, int g, int b)
  3126. {
  3127. struct nk_color ret;
  3128. ret.r =(nk_byte)NK_CLAMP(0, r, 255);
  3129. ret.g =(nk_byte)NK_CLAMP(0, g, 255);
  3130. ret.b =(nk_byte)NK_CLAMP(0, b, 255);
  3131. ret.a =(nk_byte)255;
  3132. return ret;
  3133. }
  3134. NK_API struct nk_color
  3135. nk_rgb_iv(const int *c)
  3136. {
  3137. return nk_rgb(c[0], c[1], c[2]);
  3138. }
  3139. NK_API struct nk_color
  3140. nk_rgb_bv(const nk_byte* c)
  3141. {
  3142. return nk_rgb(c[0], c[1], c[2]);
  3143. }
  3144. NK_API struct nk_color
  3145. nk_rgba_u32(nk_uint in)
  3146. {
  3147. struct nk_color ret;
  3148. ret.r = (in & 0xFF);
  3149. ret.g = ((in >> 8) & 0xFF);
  3150. ret.b = ((in >> 16) & 0xFF);
  3151. ret.a = (nk_byte)((in >> 24) & 0xFF);
  3152. return ret;
  3153. }
  3154. NK_API struct nk_color
  3155. nk_rgba_f(float r, float g, float b, float a)
  3156. {
  3157. struct nk_color ret;
  3158. ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
  3159. ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
  3160. ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
  3161. ret.a = (nk_byte)(NK_SATURATE(a) * 255.0f);
  3162. return ret;
  3163. }
  3164. NK_API struct nk_color
  3165. nk_rgba_fv(const float *c)
  3166. {
  3167. return nk_rgba_f(c[0], c[1], c[2], c[3]);
  3168. }
  3169. NK_API struct nk_color
  3170. nk_rgb_f(float r, float g, float b)
  3171. {
  3172. struct nk_color ret;
  3173. ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
  3174. ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
  3175. ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
  3176. ret.a = 255;
  3177. return ret;
  3178. }
  3179. NK_API struct nk_color
  3180. nk_rgb_fv(const float *c)
  3181. {
  3182. return nk_rgb_f(c[0], c[1], c[2]);
  3183. }
  3184. NK_API struct nk_color
  3185. nk_hsv(int h, int s, int v)
  3186. {
  3187. return nk_hsva(h, s, v, 255);
  3188. }
  3189. NK_API struct nk_color
  3190. nk_hsv_iv(const int *c)
  3191. {
  3192. return nk_hsv(c[0], c[1], c[2]);
  3193. }
  3194. NK_API struct nk_color
  3195. nk_hsv_bv(const nk_byte *c)
  3196. {
  3197. return nk_hsv(c[0], c[1], c[2]);
  3198. }
  3199. NK_API struct nk_color
  3200. nk_hsv_f(float h, float s, float v)
  3201. {
  3202. return nk_hsva_f(h, s, v, 1.0f);
  3203. }
  3204. NK_API struct nk_color
  3205. nk_hsv_fv(const float *c)
  3206. {
  3207. return nk_hsv_f(c[0], c[1], c[2]);
  3208. }
  3209. NK_API struct nk_color
  3210. nk_hsva(int h, int s, int v, int a)
  3211. {
  3212. float hf = ((float)NK_CLAMP(0, h, 255)) / 255.0f;
  3213. float sf = ((float)NK_CLAMP(0, s, 255)) / 255.0f;
  3214. float vf = ((float)NK_CLAMP(0, v, 255)) / 255.0f;
  3215. float af = ((float)NK_CLAMP(0, a, 255)) / 255.0f;
  3216. return nk_hsva_f(hf, sf, vf, af);
  3217. }
  3218. NK_API struct nk_color
  3219. nk_hsva_iv(const int *c)
  3220. {
  3221. return nk_hsva(c[0], c[1], c[2], c[3]);
  3222. }
  3223. NK_API struct nk_color
  3224. nk_hsva_bv(const nk_byte *c)
  3225. {
  3226. return nk_hsva(c[0], c[1], c[2], c[3]);
  3227. }
  3228. NK_API struct nk_color
  3229. nk_hsva_f(float h, float s, float v, float a)
  3230. {
  3231. struct nk_colorf {float r,g,b;} out = {0,0,0};
  3232. float p, q, t, f;
  3233. int i;
  3234. if (s <= 0.0f) {
  3235. out.r = v; out.g = v; out.b = v;
  3236. return nk_rgb_f(out.r, out.g, out.b);
  3237. }
  3238. h = h / (60.0f/360.0f);
  3239. i = (int)h;
  3240. f = h - (float)i;
  3241. p = v * (1.0f - s);
  3242. q = v * (1.0f - (s * f));
  3243. t = v * (1.0f - s * (1.0f - f));
  3244. switch (i) {
  3245. case 0: out.r = v; out.g = t; out.b = p; break;
  3246. case 1: out.r = q; out.g = v; out.b = p; break;
  3247. case 2: out.r = p; out.g = v; out.b = t; break;
  3248. case 3: out.r = p; out.g = q; out.b = v; break;
  3249. case 4: out.r = t; out.g = p; out.b = v; break;
  3250. case 5: default: out.r = v; out.g = p; out.b = q; break;
  3251. }
  3252. return nk_rgba_f(out.r, out.g, out.b, a);
  3253. }
  3254. NK_API struct nk_color
  3255. nk_hsva_fv(const float *c)
  3256. {
  3257. return nk_hsva_f(c[0], c[1], c[2], c[3]);
  3258. }
  3259. NK_API nk_uint
  3260. nk_color_u32(struct nk_color in)
  3261. {
  3262. nk_uint out = (nk_uint)in.r;
  3263. out |= ((nk_uint)in.g << 8);
  3264. out |= ((nk_uint)in.b << 16);
  3265. out |= ((nk_uint)in.a << 24);
  3266. return out;
  3267. }
  3268. NK_API void
  3269. nk_color_f(float *r, float *g, float *b, float *a, struct nk_color in)
  3270. {
  3271. NK_STORAGE const float s = 1.0f/255.0f;
  3272. *r = (float)in.r * s;
  3273. *g = (float)in.g * s;
  3274. *b = (float)in.b * s;
  3275. *a = (float)in.a * s;
  3276. }
  3277. NK_API void
  3278. nk_color_fv(float *c, struct nk_color in)
  3279. {
  3280. nk_color_f(&c[0], &c[1], &c[2], &c[3], in);
  3281. }
  3282. NK_API void
  3283. nk_color_hsv_f(float *out_h, float *out_s, float *out_v, struct nk_color in)
  3284. {
  3285. float a;
  3286. nk_color_hsva_f(out_h, out_s, out_v, &a, in);
  3287. }
  3288. NK_API void
  3289. nk_color_hsv_fv(float *out, struct nk_color in)
  3290. {
  3291. float a;
  3292. nk_color_hsva_f(&out[0], &out[1], &out[2], &a, in);
  3293. }
  3294. NK_API void
  3295. nk_color_hsva_f(float *out_h, float *out_s,
  3296. float *out_v, float *out_a, struct nk_color in)
  3297. {
  3298. float chroma;
  3299. float K = 0.0f;
  3300. float r,g,b,a;
  3301. nk_color_f(&r,&g,&b,&a, in);
  3302. if (g < b) {
  3303. const float t = g; g = b; b = t;
  3304. K = -1.f;
  3305. }
  3306. if (r < g) {
  3307. const float t = r; r = g; g = t;
  3308. K = -2.f/6.0f - K;
  3309. }
  3310. chroma = r - ((g < b) ? g: b);
  3311. *out_h = NK_ABS(K + (g - b)/(6.0f * chroma + 1e-20f));
  3312. *out_s = chroma / (r + 1e-20f);
  3313. *out_v = r;
  3314. *out_a = (float)in.a / 255.0f;
  3315. }
  3316. NK_API void
  3317. nk_color_hsva_fv(float *out, struct nk_color in)
  3318. {
  3319. nk_color_hsva_f(&out[0], &out[1], &out[2], &out[3], in);
  3320. }
  3321. NK_API void
  3322. nk_color_hsva_i(int *out_h, int *out_s, int *out_v,
  3323. int *out_a, struct nk_color in)
  3324. {
  3325. float h,s,v,a;
  3326. nk_color_hsva_f(&h, &s, &v, &a, in);
  3327. *out_h = (nk_byte)(h * 255.0f);
  3328. *out_s = (nk_byte)(s * 255.0f);
  3329. *out_v = (nk_byte)(v * 255.0f);
  3330. *out_a = (nk_byte)(a * 255.0f);
  3331. }
  3332. NK_API void
  3333. nk_color_hsva_iv(int *out, struct nk_color in)
  3334. {
  3335. nk_color_hsva_i(&out[0], &out[1], &out[2], &out[3], in);
  3336. }
  3337. NK_API void
  3338. nk_color_hsva_bv(nk_byte *out, struct nk_color in)
  3339. {
  3340. int tmp[4];
  3341. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  3342. out[0] = (nk_byte)tmp[0];
  3343. out[1] = (nk_byte)tmp[1];
  3344. out[2] = (nk_byte)tmp[2];
  3345. out[3] = (nk_byte)tmp[3];
  3346. }
  3347. NK_API void
  3348. nk_color_hsva_b(nk_byte *h, nk_byte *s, nk_byte *v, nk_byte *a, struct nk_color in)
  3349. {
  3350. int tmp[4];
  3351. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  3352. *h = (nk_byte)tmp[0];
  3353. *s = (nk_byte)tmp[1];
  3354. *v = (nk_byte)tmp[2];
  3355. *a = (nk_byte)tmp[3];
  3356. }
  3357. NK_API void
  3358. nk_color_hsv_i(int *out_h, int *out_s, int *out_v, struct nk_color in)
  3359. {
  3360. int a;
  3361. nk_color_hsva_i(out_h, out_s, out_v, &a, in);
  3362. }
  3363. NK_API void
  3364. nk_color_hsv_b(nk_byte *out_h, nk_byte *out_s, nk_byte *out_v, struct nk_color in)
  3365. {
  3366. int tmp[4];
  3367. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  3368. *out_h = (nk_byte)tmp[0];
  3369. *out_s = (nk_byte)tmp[1];
  3370. *out_v = (nk_byte)tmp[2];
  3371. }
  3372. NK_API void
  3373. nk_color_hsv_iv(int *out, struct nk_color in)
  3374. {
  3375. nk_color_hsv_i(&out[0], &out[1], &out[2], in);
  3376. }
  3377. NK_API void
  3378. nk_color_hsv_bv(nk_byte *out, struct nk_color in)
  3379. {
  3380. int tmp[4];
  3381. nk_color_hsv_i(&tmp[0], &tmp[1], &tmp[2], in);
  3382. out[0] = (nk_byte)tmp[0];
  3383. out[1] = (nk_byte)tmp[1];
  3384. out[2] = (nk_byte)tmp[2];
  3385. }
  3386. /*
  3387. * ==============================================================
  3388. *
  3389. * IMAGE
  3390. *
  3391. * ===============================================================
  3392. */
  3393. NK_API nk_handle
  3394. nk_handle_ptr(void *ptr)
  3395. {
  3396. nk_handle handle = {0};
  3397. handle.ptr = ptr;
  3398. return handle;
  3399. }
  3400. NK_API nk_handle
  3401. nk_handle_id(int id)
  3402. {
  3403. nk_handle handle;
  3404. nk_zero_struct(handle);
  3405. handle.id = id;
  3406. return handle;
  3407. }
  3408. NK_API struct nk_image
  3409. nk_subimage_ptr(void *ptr, unsigned short w, unsigned short h, struct nk_rect r)
  3410. {
  3411. struct nk_image s;
  3412. nk_zero(&s, sizeof(s));
  3413. s.handle.ptr = ptr;
  3414. s.w = w; s.h = h;
  3415. s.region[0] = (unsigned short)r.x;
  3416. s.region[1] = (unsigned short)r.y;
  3417. s.region[2] = (unsigned short)r.w;
  3418. s.region[3] = (unsigned short)r.h;
  3419. return s;
  3420. }
  3421. NK_API struct nk_image
  3422. nk_subimage_id(int id, unsigned short w, unsigned short h, struct nk_rect r)
  3423. {
  3424. struct nk_image s;
  3425. nk_zero(&s, sizeof(s));
  3426. s.handle.id = id;
  3427. s.w = w; s.h = h;
  3428. s.region[0] = (unsigned short)r.x;
  3429. s.region[1] = (unsigned short)r.y;
  3430. s.region[2] = (unsigned short)r.w;
  3431. s.region[3] = (unsigned short)r.h;
  3432. return s;
  3433. }
  3434. NK_API struct nk_image
  3435. nk_image_ptr(void *ptr)
  3436. {
  3437. struct nk_image s;
  3438. nk_zero(&s, sizeof(s));
  3439. NK_ASSERT(ptr);
  3440. s.handle.ptr = ptr;
  3441. s.w = 0; s.h = 0;
  3442. s.region[0] = 0;
  3443. s.region[1] = 0;
  3444. s.region[2] = 0;
  3445. s.region[3] = 0;
  3446. return s;
  3447. }
  3448. NK_API struct nk_image
  3449. nk_image_id(int id)
  3450. {
  3451. struct nk_image s;
  3452. nk_zero(&s, sizeof(s));
  3453. s.handle.id = id;
  3454. s.w = 0; s.h = 0;
  3455. s.region[0] = 0;
  3456. s.region[1] = 0;
  3457. s.region[2] = 0;
  3458. s.region[3] = 0;
  3459. return s;
  3460. }
  3461. NK_API int
  3462. nk_image_is_subimage(const struct nk_image* img)
  3463. {
  3464. NK_ASSERT(img);
  3465. return !(img->w == 0 && img->h == 0);
  3466. }
  3467. NK_INTERN void
  3468. nk_unify(struct nk_rect *clip, const struct nk_rect *a, float x0, float y0,
  3469. float x1, float y1)
  3470. {
  3471. NK_ASSERT(a);
  3472. NK_ASSERT(clip);
  3473. clip->x = NK_MAX(a->x, x0);
  3474. clip->y = NK_MAX(a->y, y0);
  3475. clip->w = NK_MIN(a->x + a->w, x1) - clip->x;
  3476. clip->h = NK_MIN(a->y + a->h, y1) - clip->y;
  3477. clip->w = NK_MAX(0, clip->w);
  3478. clip->h = NK_MAX(0, clip->h);
  3479. }
  3480. NK_API void
  3481. nk_triangle_from_direction(struct nk_vec2 *result, struct nk_rect r,
  3482. float pad_x, float pad_y, enum nk_heading direction)
  3483. {
  3484. float w_half, h_half;
  3485. NK_ASSERT(result);
  3486. r.w = NK_MAX(2 * pad_x, r.w);
  3487. r.h = NK_MAX(2 * pad_y, r.h);
  3488. r.w = r.w - 2 * pad_x;
  3489. r.h = r.h - 2 * pad_y;
  3490. r.x = r.x + pad_x;
  3491. r.y = r.y + pad_y;
  3492. w_half = r.w / 2.0f;
  3493. h_half = r.h / 2.0f;
  3494. if (direction == NK_UP) {
  3495. result[0] = nk_vec2(r.x + w_half, r.y);
  3496. result[1] = nk_vec2(r.x + r.w, r.y + r.h);
  3497. result[2] = nk_vec2(r.x, r.y + r.h);
  3498. } else if (direction == NK_RIGHT) {
  3499. result[0] = nk_vec2(r.x, r.y);
  3500. result[1] = nk_vec2(r.x + r.w, r.y + h_half);
  3501. result[2] = nk_vec2(r.x, r.y + r.h);
  3502. } else if (direction == NK_DOWN) {
  3503. result[0] = nk_vec2(r.x, r.y);
  3504. result[1] = nk_vec2(r.x + r.w, r.y);
  3505. result[2] = nk_vec2(r.x + w_half, r.y + r.h);
  3506. } else {
  3507. result[0] = nk_vec2(r.x, r.y + h_half);
  3508. result[1] = nk_vec2(r.x + r.w, r.y);
  3509. result[2] = nk_vec2(r.x + r.w, r.y + r.h);
  3510. }
  3511. }
  3512. NK_INTERN int
  3513. nk_text_clamp(const struct nk_user_font *font, const char *text,
  3514. int text_len, float space, int *glyphs, float *text_width)
  3515. {
  3516. int glyph_len = 0;
  3517. float last_width = 0;
  3518. nk_rune unicode = 0;
  3519. float width = 0;
  3520. int len = 0;
  3521. int g = 0;
  3522. glyph_len = nk_utf_decode(text, &unicode, text_len);
  3523. while (glyph_len && (width < space) && (len < text_len)) {
  3524. float s;
  3525. len += glyph_len;
  3526. s = font->width(font->userdata, font->height, text, len);
  3527. last_width = width;
  3528. width = s;
  3529. glyph_len = nk_utf_decode(&text[len], &unicode, text_len - len);
  3530. g++;
  3531. }
  3532. *glyphs = g;
  3533. *text_width = last_width;
  3534. return len;
  3535. }
  3536. enum {NK_DO_NOT_STOP_ON_NEW_LINE, NK_STOP_ON_NEW_LINE};
  3537. NK_INTERN struct nk_vec2
  3538. nk_text_calculate_text_bounds(const struct nk_user_font *font,
  3539. const char *begin, int byte_len, float row_height, const char **remaining,
  3540. struct nk_vec2 *out_offset, int *glyphs, int op)
  3541. {
  3542. float line_height = row_height;
  3543. struct nk_vec2 text_size = nk_vec2(0,0);
  3544. float line_width = 0.0f;
  3545. float glyph_width;
  3546. int glyph_len = 0;
  3547. nk_rune unicode = 0;
  3548. int text_len = 0;
  3549. if (!begin || byte_len <= 0 || !font)
  3550. return nk_vec2(0,row_height);
  3551. glyph_len = nk_utf_decode(begin, &unicode, byte_len);
  3552. if (!glyph_len) return text_size;
  3553. glyph_width = font->width(font->userdata, font->height, begin, glyph_len);
  3554. *glyphs = 0;
  3555. while ((text_len < byte_len) && glyph_len) {
  3556. if (unicode == '\n') {
  3557. text_size.x = NK_MAX(text_size.x, line_width);
  3558. text_size.y += line_height;
  3559. line_width = 0;
  3560. *glyphs+=1;
  3561. if (op == NK_STOP_ON_NEW_LINE)
  3562. break;
  3563. text_len++;
  3564. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  3565. continue;
  3566. }
  3567. if (unicode == '\r') {
  3568. text_len++;
  3569. *glyphs+=1;
  3570. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  3571. continue;
  3572. }
  3573. *glyphs = *glyphs + 1;
  3574. text_len += glyph_len;
  3575. line_width += (float)glyph_width;
  3576. glyph_width = font->width(font->userdata, font->height, begin+text_len, glyph_len);
  3577. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  3578. continue;
  3579. }
  3580. if (text_size.x < line_width)
  3581. text_size.x = line_width;
  3582. if (out_offset)
  3583. *out_offset = nk_vec2(line_width, text_size.y + line_height);
  3584. if (line_width > 0 || text_size.y == 0.0f)
  3585. text_size.y += line_height;
  3586. if (remaining)
  3587. *remaining = begin+text_len;
  3588. return text_size;
  3589. }
  3590. /* ==============================================================
  3591. *
  3592. * UTF-8
  3593. *
  3594. * ===============================================================*/
  3595. NK_GLOBAL const nk_byte nk_utfbyte[NK_UTF_SIZE+1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
  3596. NK_GLOBAL const nk_byte nk_utfmask[NK_UTF_SIZE+1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
  3597. NK_GLOBAL const nk_uint nk_utfmin[NK_UTF_SIZE+1] = {0, 0, 0x80, 0x800, 0x10000};
  3598. NK_GLOBAL const nk_uint nk_utfmax[NK_UTF_SIZE+1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
  3599. NK_INTERN int
  3600. nk_utf_validate(nk_rune *u, int i)
  3601. {
  3602. NK_ASSERT(u);
  3603. if (!u) return 0;
  3604. if (!NK_BETWEEN(*u, nk_utfmin[i], nk_utfmax[i]) ||
  3605. NK_BETWEEN(*u, 0xD800, 0xDFFF))
  3606. *u = NK_UTF_INVALID;
  3607. for (i = 1; *u > nk_utfmax[i]; ++i);
  3608. return i;
  3609. }
  3610. NK_INTERN nk_rune
  3611. nk_utf_decode_byte(char c, int *i)
  3612. {
  3613. NK_ASSERT(i);
  3614. if (!i) return 0;
  3615. for(*i = 0; *i < (int)NK_LEN(nk_utfmask); ++(*i)) {
  3616. if (((nk_byte)c & nk_utfmask[*i]) == nk_utfbyte[*i])
  3617. return (nk_byte)(c & ~nk_utfmask[*i]);
  3618. }
  3619. return 0;
  3620. }
  3621. NK_API int
  3622. nk_utf_decode(const char *c, nk_rune *u, int clen)
  3623. {
  3624. int i, j, len, type=0;
  3625. nk_rune udecoded;
  3626. NK_ASSERT(c);
  3627. NK_ASSERT(u);
  3628. if (!c || !u) return 0;
  3629. if (!clen) return 0;
  3630. *u = NK_UTF_INVALID;
  3631. udecoded = nk_utf_decode_byte(c[0], &len);
  3632. if (!NK_BETWEEN(len, 1, NK_UTF_SIZE))
  3633. return 1;
  3634. for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
  3635. udecoded = (udecoded << 6) | nk_utf_decode_byte(c[i], &type);
  3636. if (type != 0)
  3637. return j;
  3638. }
  3639. if (j < len)
  3640. return 0;
  3641. *u = udecoded;
  3642. nk_utf_validate(u, len);
  3643. return len;
  3644. }
  3645. NK_INTERN char
  3646. nk_utf_encode_byte(nk_rune u, int i)
  3647. {
  3648. return (char)((nk_utfbyte[i]) | ((nk_byte)u & ~nk_utfmask[i]));
  3649. }
  3650. NK_API int
  3651. nk_utf_encode(nk_rune u, char *c, int clen)
  3652. {
  3653. int len, i;
  3654. len = nk_utf_validate(&u, 0);
  3655. if (clen < len || !len || len > NK_UTF_SIZE)
  3656. return 0;
  3657. for (i = len - 1; i != 0; --i) {
  3658. c[i] = nk_utf_encode_byte(u, 0);
  3659. u >>= 6;
  3660. }
  3661. c[0] = nk_utf_encode_byte(u, len);
  3662. return len;
  3663. }
  3664. NK_API int
  3665. nk_utf_len(const char *str, int len)
  3666. {
  3667. const char *text;
  3668. int glyphs = 0;
  3669. int text_len;
  3670. int glyph_len;
  3671. int src_len = 0;
  3672. nk_rune unicode;
  3673. NK_ASSERT(str);
  3674. if (!str || !len) return 0;
  3675. text = str;
  3676. text_len = len;
  3677. glyph_len = nk_utf_decode(text, &unicode, text_len);
  3678. while (glyph_len && src_len < len) {
  3679. glyphs++;
  3680. src_len = src_len + glyph_len;
  3681. glyph_len = nk_utf_decode(text + src_len, &unicode, text_len - src_len);
  3682. }
  3683. return glyphs;
  3684. }
  3685. NK_API const char*
  3686. nk_utf_at(const char *buffer, int length, int index,
  3687. nk_rune *unicode, int *len)
  3688. {
  3689. int i = 0;
  3690. int src_len = 0;
  3691. int glyph_len = 0;
  3692. const char *text;
  3693. int text_len;
  3694. NK_ASSERT(buffer);
  3695. NK_ASSERT(unicode);
  3696. NK_ASSERT(len);
  3697. if (!buffer || !unicode || !len) return 0;
  3698. if (index < 0) {
  3699. *unicode = NK_UTF_INVALID;
  3700. *len = 0;
  3701. return 0;
  3702. }
  3703. text = buffer;
  3704. text_len = length;
  3705. glyph_len = nk_utf_decode(text, unicode, text_len);
  3706. while (glyph_len) {
  3707. if (i == index) {
  3708. *len = glyph_len;
  3709. break;
  3710. }
  3711. i++;
  3712. src_len = src_len + glyph_len;
  3713. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  3714. }
  3715. if (i != index) return 0;
  3716. return buffer + src_len;
  3717. }
  3718. /* ==============================================================
  3719. *
  3720. * BUFFER
  3721. *
  3722. * ===============================================================*/
  3723. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  3724. NK_INTERN void* nk_malloc(nk_handle unused, void *old,nk_size size)
  3725. {NK_UNUSED(unused); NK_UNUSED(old); return malloc(size);}
  3726. NK_INTERN void nk_mfree(nk_handle unused, void *ptr)
  3727. {NK_UNUSED(unused); free(ptr);}
  3728. NK_API void
  3729. nk_buffer_init_default(struct nk_buffer *buffer)
  3730. {
  3731. struct nk_allocator alloc;
  3732. alloc.userdata.ptr = 0;
  3733. alloc.alloc = nk_malloc;
  3734. alloc.free = nk_mfree;
  3735. nk_buffer_init(buffer, &alloc, NK_BUFFER_DEFAULT_INITIAL_SIZE);
  3736. }
  3737. #endif
  3738. NK_API void
  3739. nk_buffer_init(struct nk_buffer *b, const struct nk_allocator *a,
  3740. nk_size initial_size)
  3741. {
  3742. NK_ASSERT(b);
  3743. NK_ASSERT(a);
  3744. NK_ASSERT(initial_size);
  3745. if (!b || !a || !initial_size) return;
  3746. nk_zero(b, sizeof(*b));
  3747. b->type = NK_BUFFER_DYNAMIC;
  3748. b->memory.ptr = a->alloc(a->userdata,0, initial_size);
  3749. b->memory.size = initial_size;
  3750. b->size = initial_size;
  3751. b->grow_factor = 2.0f;
  3752. b->pool = *a;
  3753. }
  3754. NK_API void
  3755. nk_buffer_init_fixed(struct nk_buffer *b, void *m, nk_size size)
  3756. {
  3757. NK_ASSERT(b);
  3758. NK_ASSERT(m);
  3759. NK_ASSERT(size);
  3760. if (!b || !m || !size) return;
  3761. nk_zero(b, sizeof(*b));
  3762. b->type = NK_BUFFER_FIXED;
  3763. b->memory.ptr = m;
  3764. b->memory.size = size;
  3765. b->size = size;
  3766. }
  3767. NK_INTERN void*
  3768. nk_buffer_align(void *unaligned, nk_size align, nk_size *alignment,
  3769. enum nk_buffer_allocation_type type)
  3770. {
  3771. void *memory = 0;
  3772. switch (type) {
  3773. default:
  3774. case NK_BUFFER_MAX:
  3775. case NK_BUFFER_FRONT:
  3776. if (align) {
  3777. memory = NK_ALIGN_PTR(unaligned, align);
  3778. *alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
  3779. } else {
  3780. memory = unaligned;
  3781. *alignment = 0;
  3782. }
  3783. break;
  3784. case NK_BUFFER_BACK:
  3785. if (align) {
  3786. memory = NK_ALIGN_PTR_BACK(unaligned, align);
  3787. *alignment = (nk_size)((nk_byte*)unaligned - (nk_byte*)memory);
  3788. } else {
  3789. memory = unaligned;
  3790. *alignment = 0;
  3791. }
  3792. break;
  3793. }
  3794. return memory;
  3795. }
  3796. NK_INTERN void*
  3797. nk_buffer_realloc(struct nk_buffer *b, nk_size capacity, nk_size *size)
  3798. {
  3799. void *temp;
  3800. nk_size buffer_size;
  3801. NK_ASSERT(b);
  3802. NK_ASSERT(size);
  3803. if (!b || !size || !b->pool.alloc || !b->pool.free)
  3804. return 0;
  3805. buffer_size = b->memory.size;
  3806. temp = b->pool.alloc(b->pool.userdata, b->memory.ptr, capacity);
  3807. NK_ASSERT(temp);
  3808. if (!temp) return 0;
  3809. *size = capacity;
  3810. if (temp != b->memory.ptr) {
  3811. NK_MEMCPY(temp, b->memory.ptr, buffer_size);
  3812. b->pool.free(b->pool.userdata, b->memory.ptr);
  3813. }
  3814. if (b->size == buffer_size) {
  3815. /* no back buffer so just set correct size */
  3816. b->size = capacity;
  3817. return temp;
  3818. } else {
  3819. /* copy back buffer to the end of the new buffer */
  3820. void *dst, *src;
  3821. nk_size back_size;
  3822. back_size = buffer_size - b->size;
  3823. dst = nk_ptr_add(void, temp, capacity - back_size);
  3824. src = nk_ptr_add(void, temp, b->size);
  3825. NK_MEMCPY(dst, src, back_size);
  3826. b->size = capacity - back_size;
  3827. }
  3828. return temp;
  3829. }
  3830. NK_INTERN void*
  3831. nk_buffer_alloc(struct nk_buffer *b, enum nk_buffer_allocation_type type,
  3832. nk_size size, nk_size align)
  3833. {
  3834. int full;
  3835. nk_size alignment;
  3836. void *unaligned;
  3837. void *memory;
  3838. NK_ASSERT(b);
  3839. NK_ASSERT(size);
  3840. if (!b || !size) return 0;
  3841. b->needed += size;
  3842. /* calculate total size with needed alignment + size */
  3843. if (type == NK_BUFFER_FRONT)
  3844. unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
  3845. else unaligned = nk_ptr_add(void, b->memory.ptr, b->size - size);
  3846. memory = nk_buffer_align(unaligned, align, &alignment, type);
  3847. /* check if buffer has enough memory*/
  3848. if (type == NK_BUFFER_FRONT)
  3849. full = ((b->allocated + size + alignment) > b->size);
  3850. else full = ((b->size - (size + alignment)) <= b->allocated);
  3851. if (full) {
  3852. nk_size capacity;
  3853. NK_ASSERT(b->type == NK_BUFFER_DYNAMIC);
  3854. NK_ASSERT(b->pool.alloc && b->pool.free);
  3855. if (b->type != NK_BUFFER_DYNAMIC || !b->pool.alloc || !b->pool.free)
  3856. return 0;
  3857. /* buffer is full so allocate bigger buffer if dynamic */
  3858. capacity = (nk_size)((float)b->memory.size * b->grow_factor);
  3859. capacity = NK_MAX(capacity, nk_round_up_pow2((nk_uint)(b->allocated + size)));
  3860. b->memory.ptr = nk_buffer_realloc(b, capacity, &b->memory.size);
  3861. if (!b->memory.ptr) return 0;
  3862. /* align newly allocated pointer */
  3863. if (type == NK_BUFFER_FRONT)
  3864. unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
  3865. else unaligned = nk_ptr_add(void, b->memory.ptr, b->size);
  3866. memory = nk_buffer_align(unaligned, align, &alignment, type);
  3867. }
  3868. if (type == NK_BUFFER_FRONT)
  3869. b->allocated += size + alignment;
  3870. else b->size -= (size + alignment);
  3871. b->needed += alignment;
  3872. b->calls++;
  3873. return memory;
  3874. }
  3875. NK_API void
  3876. nk_buffer_push(struct nk_buffer *b, enum nk_buffer_allocation_type type,
  3877. void *memory, nk_size size, nk_size align)
  3878. {
  3879. void *mem = nk_buffer_alloc(b, type, size, align);
  3880. if (!mem) return;
  3881. NK_MEMCPY(mem, memory, size);
  3882. }
  3883. NK_API void
  3884. nk_buffer_mark(struct nk_buffer *buffer, enum nk_buffer_allocation_type type)
  3885. {
  3886. NK_ASSERT(buffer);
  3887. if (!buffer) return;
  3888. buffer->marker[type].active = nk_true;
  3889. if (type == NK_BUFFER_BACK)
  3890. buffer->marker[type].offset = buffer->size;
  3891. else buffer->marker[type].offset = buffer->allocated;
  3892. }
  3893. NK_API void
  3894. nk_buffer_reset(struct nk_buffer *buffer, enum nk_buffer_allocation_type type)
  3895. {
  3896. NK_ASSERT(buffer);
  3897. if (!buffer) return;
  3898. if (type == NK_BUFFER_BACK) {
  3899. /* reset back buffer either back to marker or empty */
  3900. buffer->needed -= (buffer->memory.size - buffer->marker[type].offset);
  3901. if (buffer->marker[type].active)
  3902. buffer->size = buffer->marker[type].offset;
  3903. else buffer->size = buffer->memory.size;
  3904. buffer->marker[type].active = nk_false;
  3905. } else {
  3906. /* reset front buffer either back to back marker or empty */
  3907. buffer->needed -= (buffer->allocated - buffer->marker[type].offset);
  3908. if (buffer->marker[type].active)
  3909. buffer->allocated = buffer->marker[type].offset;
  3910. else buffer->allocated = 0;
  3911. buffer->marker[type].active = nk_false;
  3912. }
  3913. }
  3914. NK_API void
  3915. nk_buffer_clear(struct nk_buffer *b)
  3916. {
  3917. NK_ASSERT(b);
  3918. if (!b) return;
  3919. b->allocated = 0;
  3920. b->size = b->memory.size;
  3921. b->calls = 0;
  3922. b->needed = 0;
  3923. }
  3924. NK_API void
  3925. nk_buffer_free(struct nk_buffer *b)
  3926. {
  3927. NK_ASSERT(b);
  3928. if (!b || !b->memory.ptr) return;
  3929. if (b->type == NK_BUFFER_FIXED) return;
  3930. if (!b->pool.free) return;
  3931. NK_ASSERT(b->pool.free);
  3932. b->pool.free(b->pool.userdata, b->memory.ptr);
  3933. }
  3934. NK_API void
  3935. nk_buffer_info(struct nk_memory_status *s, struct nk_buffer *b)
  3936. {
  3937. NK_ASSERT(b);
  3938. NK_ASSERT(s);
  3939. if (!s || !b) return;
  3940. s->allocated = b->allocated;
  3941. s->size = b->memory.size;
  3942. s->needed = b->needed;
  3943. s->memory = b->memory.ptr;
  3944. s->calls = b->calls;
  3945. }
  3946. NK_API void*
  3947. nk_buffer_memory(struct nk_buffer *buffer)
  3948. {
  3949. NK_ASSERT(buffer);
  3950. if (!buffer) return 0;
  3951. return buffer->memory.ptr;
  3952. }
  3953. NK_API const void*
  3954. nk_buffer_memory_const(const struct nk_buffer *buffer)
  3955. {
  3956. NK_ASSERT(buffer);
  3957. if (!buffer) return 0;
  3958. return buffer->memory.ptr;
  3959. }
  3960. NK_API nk_size
  3961. nk_buffer_total(struct nk_buffer *buffer)
  3962. {
  3963. NK_ASSERT(buffer);
  3964. if (!buffer) return 0;
  3965. return buffer->memory.size;
  3966. }
  3967. /*
  3968. * ==============================================================
  3969. *
  3970. * STRING
  3971. *
  3972. * ===============================================================
  3973. */
  3974. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  3975. NK_API void
  3976. nk_str_init_default(struct nk_str *str)
  3977. {
  3978. struct nk_allocator alloc;
  3979. alloc.userdata.ptr = 0;
  3980. alloc.alloc = nk_malloc;
  3981. alloc.free = nk_mfree;
  3982. nk_buffer_init(&str->buffer, &alloc, 32);
  3983. str->len = 0;
  3984. }
  3985. #endif
  3986. NK_API void
  3987. nk_str_init(struct nk_str *str, const struct nk_allocator *alloc, nk_size size)
  3988. {
  3989. nk_buffer_init(&str->buffer, alloc, size);
  3990. str->len = 0;
  3991. }
  3992. NK_API void
  3993. nk_str_init_fixed(struct nk_str *str, void *memory, nk_size size)
  3994. {
  3995. nk_buffer_init_fixed(&str->buffer, memory, size);
  3996. str->len = 0;
  3997. }
  3998. NK_API int
  3999. nk_str_append_text_char(struct nk_str *s, const char *str, int len)
  4000. {
  4001. char *mem;
  4002. NK_ASSERT(s);
  4003. NK_ASSERT(str);
  4004. if (!s || !str || !len) return 0;
  4005. mem = (char*)nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
  4006. if (!mem) return 0;
  4007. NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
  4008. s->len += nk_utf_len(str, len);
  4009. return len;
  4010. }
  4011. NK_API int
  4012. nk_str_append_str_char(struct nk_str *s, const char *str)
  4013. {
  4014. return nk_str_append_text_char(s, str, nk_strlen(str));
  4015. }
  4016. NK_API int
  4017. nk_str_append_text_utf8(struct nk_str *str, const char *text, int len)
  4018. {
  4019. int i = 0;
  4020. int byte_len = 0;
  4021. nk_rune unicode;
  4022. if (!str || !text || !len) return 0;
  4023. for (i = 0; i < len; ++i)
  4024. byte_len += nk_utf_decode(text+byte_len, &unicode, 4);
  4025. nk_str_append_text_char(str, text, byte_len);
  4026. return len;
  4027. }
  4028. NK_API int
  4029. nk_str_append_str_utf8(struct nk_str *str, const char *text)
  4030. {
  4031. int runes = 0;
  4032. int byte_len = 0;
  4033. int num_runes = 0;
  4034. int glyph_len = 0;
  4035. nk_rune unicode;
  4036. if (!str || !text) return 0;
  4037. glyph_len = byte_len = nk_utf_decode(text+byte_len, &unicode, 4);
  4038. while (unicode != '\0' && glyph_len) {
  4039. glyph_len = nk_utf_decode(text+byte_len, &unicode, 4);
  4040. byte_len += glyph_len;
  4041. num_runes++;
  4042. }
  4043. nk_str_append_text_char(str, text, byte_len);
  4044. return runes;
  4045. }
  4046. NK_API int
  4047. nk_str_append_text_runes(struct nk_str *str, const nk_rune *text, int len)
  4048. {
  4049. int i = 0;
  4050. int byte_len = 0;
  4051. nk_glyph glyph;
  4052. NK_ASSERT(str);
  4053. if (!str || !text || !len) return 0;
  4054. for (i = 0; i < len; ++i) {
  4055. byte_len = nk_utf_encode(text[i], glyph, NK_UTF_SIZE);
  4056. if (!byte_len) break;
  4057. nk_str_append_text_char(str, glyph, byte_len);
  4058. }
  4059. return len;
  4060. }
  4061. NK_API int
  4062. nk_str_append_str_runes(struct nk_str *str, const nk_rune *runes)
  4063. {
  4064. int i = 0;
  4065. nk_glyph glyph;
  4066. int byte_len;
  4067. NK_ASSERT(str);
  4068. if (!str || !runes) return 0;
  4069. while (runes[i] != '\0') {
  4070. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  4071. nk_str_append_text_char(str, glyph, byte_len);
  4072. i++;
  4073. }
  4074. return i;
  4075. }
  4076. NK_API int
  4077. nk_str_insert_at_char(struct nk_str *s, int pos, const char *str, int len)
  4078. {
  4079. int i;
  4080. void *mem;
  4081. char *src;
  4082. char *dst;
  4083. int copylen;
  4084. NK_ASSERT(s);
  4085. NK_ASSERT(str);
  4086. NK_ASSERT(len >= 0);
  4087. if (!s || !str || !len || (nk_size)pos > s->buffer.allocated) return 0;
  4088. if ((s->buffer.allocated + (nk_size)len >= s->buffer.memory.size) &&
  4089. (s->buffer.type == NK_BUFFER_FIXED)) return 0;
  4090. copylen = (int)s->buffer.allocated - pos;
  4091. if (!copylen) {
  4092. nk_str_append_text_char(s, str, len);
  4093. return 1;
  4094. }
  4095. mem = nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
  4096. if (!mem) return 0;
  4097. /* memmove */
  4098. NK_ASSERT(((int)pos + (int)len + ((int)copylen - 1)) >= 0);
  4099. NK_ASSERT(((int)pos + ((int)copylen - 1)) >= 0);
  4100. dst = nk_ptr_add(char, s->buffer.memory.ptr, pos + len + (copylen - 1));
  4101. src = nk_ptr_add(char, s->buffer.memory.ptr, pos + (copylen-1));
  4102. for (i = 0; i < copylen; ++i) *dst-- = *src--;
  4103. mem = nk_ptr_add(void, s->buffer.memory.ptr, pos);
  4104. NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
  4105. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  4106. return 1;
  4107. }
  4108. NK_API int
  4109. nk_str_insert_at_rune(struct nk_str *str, int pos, const char *cstr, int len)
  4110. {
  4111. int glyph_len;
  4112. nk_rune unicode;
  4113. const char *begin;
  4114. const char *buffer;
  4115. NK_ASSERT(str);
  4116. NK_ASSERT(cstr);
  4117. NK_ASSERT(len);
  4118. if (!str || !cstr || !len) return 0;
  4119. begin = nk_str_at_rune(str, pos, &unicode, &glyph_len);
  4120. buffer = nk_str_get_const(str);
  4121. if (!begin) return 0;
  4122. return nk_str_insert_text_char(str, (int)(begin - buffer), cstr, len);
  4123. }
  4124. NK_API int nk_str_insert_text_char(struct nk_str *str, int pos, const char *text, int len)
  4125. {return nk_str_insert_at_char(str, pos, text, len);}
  4126. NK_API int nk_str_insert_str_char(struct nk_str *str, int pos, const char *text)
  4127. {return nk_str_insert_at_char(str, pos, text, nk_strlen(text));}
  4128. NK_API int
  4129. nk_str_insert_text_utf8(struct nk_str *str, int pos, const char *text, int len)
  4130. {
  4131. int i = 0;
  4132. int byte_len = 0;
  4133. nk_rune unicode;
  4134. NK_ASSERT(str);
  4135. NK_ASSERT(text);
  4136. if (!str || !text || !len) return 0;
  4137. for (i = 0; i < len; ++i)
  4138. byte_len += nk_utf_decode(text+byte_len, &unicode, 4);
  4139. nk_str_insert_at_rune(str, pos, text, byte_len);
  4140. return len;
  4141. }
  4142. NK_API int
  4143. nk_str_insert_str_utf8(struct nk_str *str, int pos, const char *text)
  4144. {
  4145. int runes = 0;
  4146. int byte_len = 0;
  4147. int num_runes = 0;
  4148. int glyph_len = 0;
  4149. nk_rune unicode;
  4150. if (!str || !text) return 0;
  4151. glyph_len = byte_len = nk_utf_decode(text+byte_len, &unicode, 4);
  4152. while (unicode != '\0' && glyph_len) {
  4153. glyph_len = nk_utf_decode(text+byte_len, &unicode, 4);
  4154. byte_len += glyph_len;
  4155. num_runes++;
  4156. }
  4157. nk_str_insert_at_rune(str, pos, text, byte_len);
  4158. return runes;
  4159. }
  4160. NK_API int
  4161. nk_str_insert_text_runes(struct nk_str *str, int pos, const nk_rune *runes, int len)
  4162. {
  4163. int i = 0;
  4164. int byte_len = 0;
  4165. nk_glyph glyph;
  4166. NK_ASSERT(str);
  4167. if (!str || !runes || !len) return 0;
  4168. for (i = 0; i < len; ++i) {
  4169. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  4170. if (!byte_len) break;
  4171. nk_str_insert_at_rune(str, pos+i, glyph, byte_len);
  4172. }
  4173. return len;
  4174. }
  4175. NK_API int
  4176. nk_str_insert_str_runes(struct nk_str *str, int pos, const nk_rune *runes)
  4177. {
  4178. int i = 0;
  4179. nk_glyph glyph;
  4180. int byte_len;
  4181. NK_ASSERT(str);
  4182. if (!str || !runes) return 0;
  4183. while (runes[i] != '\0') {
  4184. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  4185. nk_str_insert_at_rune(str, pos+i, glyph, byte_len);
  4186. i++;
  4187. }
  4188. return i;
  4189. }
  4190. NK_API void
  4191. nk_str_remove_chars(struct nk_str *s, int len)
  4192. {
  4193. NK_ASSERT(s);
  4194. NK_ASSERT(len >= 0);
  4195. if (!s || len < 0 || (nk_size)len > s->buffer.allocated) return;
  4196. NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
  4197. s->buffer.allocated -= (nk_size)len;
  4198. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  4199. }
  4200. NK_API void
  4201. nk_str_remove_runes(struct nk_str *str, int len)
  4202. {
  4203. int index;
  4204. const char *begin;
  4205. const char *end;
  4206. nk_rune unicode;
  4207. NK_ASSERT(str);
  4208. NK_ASSERT(len >= 0);
  4209. if (!str || len < 0) return;
  4210. if (len >= str->len) {
  4211. str->len = 0;
  4212. return;
  4213. }
  4214. index = str->len - len;
  4215. begin = nk_str_at_rune(str, index, &unicode, &len);
  4216. end = (const char*)str->buffer.memory.ptr + str->buffer.allocated;
  4217. nk_str_remove_chars(str, (int)(end-begin)+1);
  4218. }
  4219. NK_API void
  4220. nk_str_delete_chars(struct nk_str *s, int pos, int len)
  4221. {
  4222. NK_ASSERT(s);
  4223. if (!s || !len || (nk_size)pos > s->buffer.allocated ||
  4224. (nk_size)(pos + len) > s->buffer.allocated) return;
  4225. if ((nk_size)(pos + len) < s->buffer.allocated) {
  4226. /* memmove */
  4227. char *dst = nk_ptr_add(char, s->buffer.memory.ptr, pos);
  4228. char *src = nk_ptr_add(char, s->buffer.memory.ptr, pos + len);
  4229. NK_MEMCPY(dst, src, s->buffer.allocated - (nk_size)(pos + len));
  4230. NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
  4231. s->buffer.allocated -= (nk_size)len;
  4232. } else nk_str_remove_chars(s, len);
  4233. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  4234. }
  4235. NK_API void
  4236. nk_str_delete_runes(struct nk_str *s, int pos, int len)
  4237. {
  4238. char *temp;
  4239. nk_rune unicode;
  4240. char *begin;
  4241. char *end;
  4242. int unused;
  4243. NK_ASSERT(s);
  4244. NK_ASSERT(s->len >= pos + len);
  4245. if (s->len < pos + len)
  4246. len = NK_CLAMP(0, (s->len - pos), s->len);
  4247. if (!len) return;
  4248. temp = (char *)s->buffer.memory.ptr;
  4249. begin = nk_str_at_rune(s, pos, &unicode, &unused);
  4250. if (!begin) return;
  4251. s->buffer.memory.ptr = begin;
  4252. end = nk_str_at_rune(s, len, &unicode, &unused);
  4253. s->buffer.memory.ptr = temp;
  4254. if (!end) return;
  4255. nk_str_delete_chars(s, (int)(begin - temp), (int)(end - begin));
  4256. }
  4257. NK_API char*
  4258. nk_str_at_char(struct nk_str *s, int pos)
  4259. {
  4260. NK_ASSERT(s);
  4261. if (!s || pos > (int)s->buffer.allocated) return 0;
  4262. return nk_ptr_add(char, s->buffer.memory.ptr, pos);
  4263. }
  4264. NK_API char*
  4265. nk_str_at_rune(struct nk_str *str, int pos, nk_rune *unicode, int *len)
  4266. {
  4267. int i = 0;
  4268. int src_len = 0;
  4269. int glyph_len = 0;
  4270. char *text;
  4271. int text_len;
  4272. NK_ASSERT(str);
  4273. NK_ASSERT(unicode);
  4274. NK_ASSERT(len);
  4275. if (!str || !unicode || !len) return 0;
  4276. if (pos < 0) {
  4277. *unicode = 0;
  4278. *len = 0;
  4279. return 0;
  4280. }
  4281. text = (char*)str->buffer.memory.ptr;
  4282. text_len = (int)str->buffer.allocated;
  4283. glyph_len = nk_utf_decode(text, unicode, text_len);
  4284. while (glyph_len) {
  4285. if (i == pos) {
  4286. *len = glyph_len;
  4287. break;
  4288. }
  4289. i+= glyph_len;
  4290. src_len = src_len + glyph_len;
  4291. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  4292. }
  4293. if (i != pos) return 0;
  4294. return text + src_len;
  4295. }
  4296. NK_API const char*
  4297. nk_str_at_char_const(const struct nk_str *s, int pos)
  4298. {
  4299. NK_ASSERT(s);
  4300. if (!s || pos > (int)s->buffer.allocated) return 0;
  4301. return nk_ptr_add(char, s->buffer.memory.ptr, pos);
  4302. }
  4303. NK_API const char*
  4304. nk_str_at_const(const struct nk_str *str, int pos, nk_rune *unicode, int *len)
  4305. {
  4306. int i = 0;
  4307. int src_len = 0;
  4308. int glyph_len = 0;
  4309. char *text;
  4310. int text_len;
  4311. NK_ASSERT(str);
  4312. NK_ASSERT(unicode);
  4313. NK_ASSERT(len);
  4314. if (!str || !unicode || !len) return 0;
  4315. if (pos < 0) {
  4316. *unicode = 0;
  4317. *len = 0;
  4318. return 0;
  4319. }
  4320. text = (char*)str->buffer.memory.ptr;
  4321. text_len = (int)str->buffer.allocated;
  4322. glyph_len = nk_utf_decode(text, unicode, text_len);
  4323. while (glyph_len) {
  4324. if (i == pos) {
  4325. *len = glyph_len;
  4326. break;
  4327. }
  4328. i++;
  4329. src_len = src_len + glyph_len;
  4330. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  4331. }
  4332. if (i != pos) return 0;
  4333. return text + src_len;
  4334. }
  4335. NK_API nk_rune
  4336. nk_str_rune_at(const struct nk_str *str, int pos)
  4337. {
  4338. int len;
  4339. nk_rune unicode = 0;
  4340. nk_str_at_const(str, pos, &unicode, &len);
  4341. return unicode;
  4342. }
  4343. NK_API char*
  4344. nk_str_get(struct nk_str *s)
  4345. {
  4346. NK_ASSERT(s);
  4347. if (!s || !s->len || !s->buffer.allocated) return 0;
  4348. return (char*)s->buffer.memory.ptr;
  4349. }
  4350. NK_API const char*
  4351. nk_str_get_const(const struct nk_str *s)
  4352. {
  4353. NK_ASSERT(s);
  4354. if (!s || !s->len || !s->buffer.allocated) return 0;
  4355. return (const char*)s->buffer.memory.ptr;
  4356. }
  4357. NK_API int
  4358. nk_str_len(struct nk_str *s)
  4359. {
  4360. NK_ASSERT(s);
  4361. if (!s || !s->len || !s->buffer.allocated) return 0;
  4362. return s->len;
  4363. }
  4364. NK_API int
  4365. nk_str_len_char(struct nk_str *s)
  4366. {
  4367. NK_ASSERT(s);
  4368. if (!s || !s->len || !s->buffer.allocated) return 0;
  4369. return (int)s->buffer.allocated;
  4370. }
  4371. NK_API void
  4372. nk_str_clear(struct nk_str *str)
  4373. {
  4374. NK_ASSERT(str);
  4375. nk_buffer_clear(&str->buffer);
  4376. str->len = 0;
  4377. }
  4378. NK_API void
  4379. nk_str_free(struct nk_str *str)
  4380. {
  4381. NK_ASSERT(str);
  4382. nk_buffer_free(&str->buffer);
  4383. str->len = 0;
  4384. }
  4385. /*
  4386. * ==============================================================
  4387. *
  4388. * Command buffer
  4389. *
  4390. * ===============================================================
  4391. */
  4392. NK_INTERN void
  4393. nk_command_buffer_init(struct nk_command_buffer *cmdbuf,
  4394. struct nk_buffer *buffer, enum nk_command_clipping clip)
  4395. {
  4396. NK_ASSERT(cmdbuf);
  4397. NK_ASSERT(buffer);
  4398. if (!cmdbuf || !buffer) return;
  4399. cmdbuf->base = buffer;
  4400. cmdbuf->use_clipping = clip;
  4401. cmdbuf->begin = buffer->allocated;
  4402. cmdbuf->end = buffer->allocated;
  4403. cmdbuf->last = buffer->allocated;
  4404. }
  4405. NK_INTERN void
  4406. nk_command_buffer_reset(struct nk_command_buffer *buffer)
  4407. {
  4408. NK_ASSERT(buffer);
  4409. if (!buffer) return;
  4410. buffer->begin = 0;
  4411. buffer->end = 0;
  4412. buffer->last = 0;
  4413. buffer->clip = nk_null_rect;
  4414. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4415. buffer->userdata.ptr = 0;
  4416. #endif
  4417. }
  4418. NK_INTERN void*
  4419. nk_command_buffer_push(struct nk_command_buffer* b,
  4420. enum nk_command_type t, nk_size size)
  4421. {
  4422. NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_command);
  4423. struct nk_command *cmd;
  4424. nk_size alignment;
  4425. void *unaligned;
  4426. void *memory;
  4427. NK_ASSERT(b);
  4428. NK_ASSERT(b->base);
  4429. if (!b) return 0;
  4430. cmd = (struct nk_command*)nk_buffer_alloc(b->base,NK_BUFFER_FRONT,size,align);
  4431. if (!cmd) return 0;
  4432. /* make sure the offset to the next command is aligned */
  4433. b->last = (nk_size)((nk_byte*)cmd - (nk_byte*)b->base->memory.ptr);
  4434. unaligned = (nk_byte*)cmd + size;
  4435. memory = NK_ALIGN_PTR(unaligned, align);
  4436. alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
  4437. cmd->type = t;
  4438. cmd->next = b->base->allocated + alignment;
  4439. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4440. cmd->userdata = b->userdata;
  4441. #endif
  4442. b->end = cmd->next;
  4443. return cmd;
  4444. }
  4445. NK_API void
  4446. nk_push_scissor(struct nk_command_buffer *b, struct nk_rect r)
  4447. {
  4448. struct nk_command_scissor *cmd;
  4449. NK_ASSERT(b);
  4450. if (!b) return;
  4451. b->clip.x = r.x;
  4452. b->clip.y = r.y;
  4453. b->clip.w = r.w;
  4454. b->clip.h = r.h;
  4455. cmd = (struct nk_command_scissor*)
  4456. nk_command_buffer_push(b, NK_COMMAND_SCISSOR, sizeof(*cmd));
  4457. if (!cmd) return;
  4458. cmd->x = (short)r.x;
  4459. cmd->y = (short)r.y;
  4460. cmd->w = (unsigned short)NK_MAX(0, r.w);
  4461. cmd->h = (unsigned short)NK_MAX(0, r.h);
  4462. }
  4463. NK_API void
  4464. nk_stroke_line(struct nk_command_buffer *b, float x0, float y0,
  4465. float x1, float y1, float line_thickness, struct nk_color c)
  4466. {
  4467. struct nk_command_line *cmd;
  4468. NK_ASSERT(b);
  4469. if (!b) return;
  4470. cmd = (struct nk_command_line*)
  4471. nk_command_buffer_push(b, NK_COMMAND_LINE, sizeof(*cmd));
  4472. if (!cmd) return;
  4473. cmd->line_thickness = (unsigned short)line_thickness;
  4474. cmd->begin.x = (short)x0;
  4475. cmd->begin.y = (short)y0;
  4476. cmd->end.x = (short)x1;
  4477. cmd->end.y = (short)y1;
  4478. cmd->color = c;
  4479. }
  4480. NK_API void
  4481. nk_stroke_curve(struct nk_command_buffer *b, float ax, float ay,
  4482. float ctrl0x, float ctrl0y, float ctrl1x, float ctrl1y,
  4483. float bx, float by, float line_thickness, struct nk_color col)
  4484. {
  4485. struct nk_command_curve *cmd;
  4486. NK_ASSERT(b);
  4487. if (!b || col.a == 0) return;
  4488. cmd = (struct nk_command_curve*)
  4489. nk_command_buffer_push(b, NK_COMMAND_CURVE, sizeof(*cmd));
  4490. if (!cmd) return;
  4491. cmd->line_thickness = (unsigned short)line_thickness;
  4492. cmd->begin.x = (short)ax;
  4493. cmd->begin.y = (short)ay;
  4494. cmd->ctrl[0].x = (short)ctrl0x;
  4495. cmd->ctrl[0].y = (short)ctrl0y;
  4496. cmd->ctrl[1].x = (short)ctrl1x;
  4497. cmd->ctrl[1].y = (short)ctrl1y;
  4498. cmd->end.x = (short)bx;
  4499. cmd->end.y = (short)by;
  4500. cmd->color = col;
  4501. }
  4502. NK_API void
  4503. nk_stroke_rect(struct nk_command_buffer *b, struct nk_rect rect,
  4504. float rounding, float line_thickness, struct nk_color c)
  4505. {
  4506. struct nk_command_rect *cmd;
  4507. NK_ASSERT(b);
  4508. if (!b || c.a == 0) return;
  4509. if (b->use_clipping) {
  4510. const struct nk_rect *clip = &b->clip;
  4511. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  4512. clip->x, clip->y, clip->w, clip->h)) return;
  4513. }
  4514. cmd = (struct nk_command_rect*)
  4515. nk_command_buffer_push(b, NK_COMMAND_RECT, sizeof(*cmd));
  4516. if (!cmd) return;
  4517. cmd->rounding = (unsigned short)rounding;
  4518. cmd->line_thickness = (unsigned short)line_thickness;
  4519. cmd->x = (short)rect.x;
  4520. cmd->y = (short)rect.y;
  4521. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  4522. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  4523. cmd->color = c;
  4524. }
  4525. NK_API void
  4526. nk_fill_rect(struct nk_command_buffer *b, struct nk_rect rect,
  4527. float rounding, struct nk_color c)
  4528. {
  4529. struct nk_command_rect_filled *cmd;
  4530. NK_ASSERT(b);
  4531. if (!b || c.a == 0) return;
  4532. if (b->use_clipping) {
  4533. const struct nk_rect *clip = &b->clip;
  4534. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  4535. clip->x, clip->y, clip->w, clip->h)) return;
  4536. }
  4537. cmd = (struct nk_command_rect_filled*)
  4538. nk_command_buffer_push(b, NK_COMMAND_RECT_FILLED, sizeof(*cmd));
  4539. if (!cmd) return;
  4540. cmd->rounding = (unsigned short)rounding;
  4541. cmd->x = (short)rect.x;
  4542. cmd->y = (short)rect.y;
  4543. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  4544. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  4545. cmd->color = c;
  4546. }
  4547. NK_API void
  4548. nk_fill_rect_multi_color(struct nk_command_buffer *b, struct nk_rect rect,
  4549. struct nk_color left, struct nk_color top, struct nk_color right,
  4550. struct nk_color bottom)
  4551. {
  4552. struct nk_command_rect_multi_color *cmd;
  4553. NK_ASSERT(b);
  4554. if (!b) return;
  4555. if (b->use_clipping) {
  4556. const struct nk_rect *clip = &b->clip;
  4557. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  4558. clip->x, clip->y, clip->w, clip->h)) return;
  4559. }
  4560. cmd = (struct nk_command_rect_multi_color*)
  4561. nk_command_buffer_push(b, NK_COMMAND_RECT_MULTI_COLOR, sizeof(*cmd));
  4562. if (!cmd) return;
  4563. cmd->x = (short)rect.x;
  4564. cmd->y = (short)rect.y;
  4565. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  4566. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  4567. cmd->left = left;
  4568. cmd->top = top;
  4569. cmd->right = right;
  4570. cmd->bottom = bottom;
  4571. }
  4572. NK_API void
  4573. nk_stroke_circle(struct nk_command_buffer *b, struct nk_rect r,
  4574. float line_thickness, struct nk_color c)
  4575. {
  4576. struct nk_command_circle *cmd;
  4577. if (!b || c.a == 0) return;
  4578. if (b->use_clipping) {
  4579. const struct nk_rect *clip = &b->clip;
  4580. if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
  4581. return;
  4582. }
  4583. cmd = (struct nk_command_circle*)
  4584. nk_command_buffer_push(b, NK_COMMAND_CIRCLE, sizeof(*cmd));
  4585. if (!cmd) return;
  4586. cmd->line_thickness = (unsigned short)line_thickness;
  4587. cmd->x = (short)r.x;
  4588. cmd->y = (short)r.y;
  4589. cmd->w = (unsigned short)NK_MAX(r.w, 0);
  4590. cmd->h = (unsigned short)NK_MAX(r.h, 0);
  4591. cmd->color = c;
  4592. }
  4593. NK_API void
  4594. nk_fill_circle(struct nk_command_buffer *b, struct nk_rect r, struct nk_color c)
  4595. {
  4596. struct nk_command_circle_filled *cmd;
  4597. NK_ASSERT(b);
  4598. if (!b || c.a == 0) return;
  4599. if (b->use_clipping) {
  4600. const struct nk_rect *clip = &b->clip;
  4601. if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
  4602. return;
  4603. }
  4604. cmd = (struct nk_command_circle_filled*)
  4605. nk_command_buffer_push(b, NK_COMMAND_CIRCLE_FILLED, sizeof(*cmd));
  4606. if (!cmd) return;
  4607. cmd->x = (short)r.x;
  4608. cmd->y = (short)r.y;
  4609. cmd->w = (unsigned short)NK_MAX(r.w, 0);
  4610. cmd->h = (unsigned short)NK_MAX(r.h, 0);
  4611. cmd->color = c;
  4612. }
  4613. NK_API void
  4614. nk_stroke_arc(struct nk_command_buffer *b, float cx, float cy, float radius,
  4615. float a_min, float a_max, float line_thickness, struct nk_color c)
  4616. {
  4617. struct nk_command_arc *cmd;
  4618. if (!b || c.a == 0) return;
  4619. cmd = (struct nk_command_arc*)
  4620. nk_command_buffer_push(b, NK_COMMAND_ARC, sizeof(*cmd));
  4621. if (!cmd) return;
  4622. cmd->line_thickness = (unsigned short)line_thickness;
  4623. cmd->cx = (short)cx;
  4624. cmd->cy = (short)cy;
  4625. cmd->r = (unsigned short)radius;
  4626. cmd->a[0] = a_min;
  4627. cmd->a[1] = a_max;
  4628. cmd->color = c;
  4629. }
  4630. NK_API void
  4631. nk_fill_arc(struct nk_command_buffer *b, float cx, float cy, float radius,
  4632. float a_min, float a_max, struct nk_color c)
  4633. {
  4634. struct nk_command_arc_filled *cmd;
  4635. NK_ASSERT(b);
  4636. if (!b || c.a == 0) return;
  4637. cmd = (struct nk_command_arc_filled*)
  4638. nk_command_buffer_push(b, NK_COMMAND_ARC_FILLED, sizeof(*cmd));
  4639. if (!cmd) return;
  4640. cmd->cx = (short)cx;
  4641. cmd->cy = (short)cy;
  4642. cmd->r = (unsigned short)radius;
  4643. cmd->a[0] = a_min;
  4644. cmd->a[1] = a_max;
  4645. cmd->color = c;
  4646. }
  4647. NK_API void
  4648. nk_stroke_triangle(struct nk_command_buffer *b, float x0, float y0, float x1,
  4649. float y1, float x2, float y2, float line_thickness, struct nk_color c)
  4650. {
  4651. struct nk_command_triangle *cmd;
  4652. NK_ASSERT(b);
  4653. if (!b || c.a == 0) return;
  4654. if (b->use_clipping) {
  4655. const struct nk_rect *clip = &b->clip;
  4656. if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
  4657. !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
  4658. !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
  4659. return;
  4660. }
  4661. cmd = (struct nk_command_triangle*)
  4662. nk_command_buffer_push(b, NK_COMMAND_TRIANGLE, sizeof(*cmd));
  4663. if (!cmd) return;
  4664. cmd->line_thickness = (unsigned short)line_thickness;
  4665. cmd->a.x = (short)x0;
  4666. cmd->a.y = (short)y0;
  4667. cmd->b.x = (short)x1;
  4668. cmd->b.y = (short)y1;
  4669. cmd->c.x = (short)x2;
  4670. cmd->c.y = (short)y2;
  4671. cmd->color = c;
  4672. }
  4673. NK_API void
  4674. nk_fill_triangle(struct nk_command_buffer *b, float x0, float y0, float x1,
  4675. float y1, float x2, float y2, struct nk_color c)
  4676. {
  4677. struct nk_command_triangle_filled *cmd;
  4678. NK_ASSERT(b);
  4679. if (!b || c.a == 0) return;
  4680. if (!b) return;
  4681. if (b->use_clipping) {
  4682. const struct nk_rect *clip = &b->clip;
  4683. if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
  4684. !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
  4685. !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
  4686. return;
  4687. }
  4688. cmd = (struct nk_command_triangle_filled*)
  4689. nk_command_buffer_push(b, NK_COMMAND_TRIANGLE_FILLED, sizeof(*cmd));
  4690. if (!cmd) return;
  4691. cmd->a.x = (short)x0;
  4692. cmd->a.y = (short)y0;
  4693. cmd->b.x = (short)x1;
  4694. cmd->b.y = (short)y1;
  4695. cmd->c.x = (short)x2;
  4696. cmd->c.y = (short)y2;
  4697. cmd->color = c;
  4698. }
  4699. NK_API void
  4700. nk_stroke_polygon(struct nk_command_buffer *b, float *points, int point_count,
  4701. float line_thickness, struct nk_color col)
  4702. {
  4703. int i;
  4704. nk_size size = 0;
  4705. struct nk_command_polygon *cmd;
  4706. NK_ASSERT(b);
  4707. if (!b || col.a == 0) return;
  4708. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  4709. cmd = (struct nk_command_polygon*) nk_command_buffer_push(b, NK_COMMAND_POLYGON, size);
  4710. if (!cmd) return;
  4711. cmd->line_thickness = (unsigned short)line_thickness;
  4712. cmd->color = col;
  4713. cmd->point_count = (unsigned short)point_count;
  4714. for (i = 0; i < point_count; ++i) {
  4715. cmd->points[i].x = (short)points[i*2];
  4716. cmd->points[i].y = (short)points[i*2+1];
  4717. }
  4718. }
  4719. NK_API void
  4720. nk_fill_polygon(struct nk_command_buffer *b, float *points, int point_count,
  4721. struct nk_color col)
  4722. {
  4723. int i;
  4724. nk_size size = 0;
  4725. struct nk_command_polygon_filled *cmd;
  4726. NK_ASSERT(b);
  4727. if (!b || col.a == 0) return;
  4728. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  4729. cmd = (struct nk_command_polygon_filled*)
  4730. nk_command_buffer_push(b, NK_COMMAND_POLYGON_FILLED, size);
  4731. if (!cmd) return;
  4732. cmd->color = col;
  4733. cmd->point_count = (unsigned short)point_count;
  4734. for (i = 0; i < point_count; ++i) {
  4735. cmd->points[i].x = (short)points[i*2+0];
  4736. cmd->points[i].y = (short)points[i*2+1];
  4737. }
  4738. }
  4739. NK_API void
  4740. nk_stroke_polyline(struct nk_command_buffer *b, float *points, int point_count,
  4741. float line_thickness, struct nk_color col)
  4742. {
  4743. int i;
  4744. nk_size size = 0;
  4745. struct nk_command_polyline *cmd;
  4746. NK_ASSERT(b);
  4747. if (!b || col.a == 0) return;
  4748. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  4749. cmd = (struct nk_command_polyline*) nk_command_buffer_push(b, NK_COMMAND_POLYLINE, size);
  4750. if (!cmd) return;
  4751. cmd->color = col;
  4752. cmd->point_count = (unsigned short)point_count;
  4753. cmd->line_thickness = (unsigned short)line_thickness;
  4754. for (i = 0; i < point_count; ++i) {
  4755. cmd->points[i].x = (short)points[i*2];
  4756. cmd->points[i].y = (short)points[i*2+1];
  4757. }
  4758. }
  4759. NK_API void
  4760. nk_draw_image(struct nk_command_buffer *b, struct nk_rect r,
  4761. const struct nk_image *img)
  4762. {
  4763. struct nk_command_image *cmd;
  4764. NK_ASSERT(b);
  4765. if (!b) return;
  4766. if (b->use_clipping) {
  4767. const struct nk_rect *c = &b->clip;
  4768. if (!c->w || !c->h || !NK_INTERSECT(r.x, r.y, r.w, r.h, c->x, c->y, c->w, c->h))
  4769. return;
  4770. }
  4771. cmd = (struct nk_command_image*)
  4772. nk_command_buffer_push(b, NK_COMMAND_IMAGE, sizeof(*cmd));
  4773. if (!cmd) return;
  4774. cmd->x = (short)r.x;
  4775. cmd->y = (short)r.y;
  4776. cmd->w = (unsigned short)NK_MAX(0, r.w);
  4777. cmd->h = (unsigned short)NK_MAX(0, r.h);
  4778. cmd->img = *img;
  4779. }
  4780. NK_API void
  4781. nk_draw_text(struct nk_command_buffer *b, struct nk_rect r,
  4782. const char *string, int length, const struct nk_user_font *font,
  4783. struct nk_color bg, struct nk_color fg)
  4784. {
  4785. float text_width = 0;
  4786. struct nk_command_text *cmd;
  4787. NK_ASSERT(b);
  4788. NK_ASSERT(font);
  4789. if (!b || !string || !length || (bg.a == 0 && fg.a == 0)) return;
  4790. if (b->use_clipping) {
  4791. const struct nk_rect *c = &b->clip;
  4792. if (!c->w || !c->h || !NK_INTERSECT(r.x, r.y, r.w, r.h, c->x, c->y, c->w, c->h))
  4793. return;
  4794. }
  4795. /* make sure text fits inside bounds */
  4796. text_width = font->width(font->userdata, font->height, string, length);
  4797. if (text_width > r.w){
  4798. int glyphs = 0;
  4799. float txt_width = (float)text_width;
  4800. length = nk_text_clamp(font, string, length, r.w, &glyphs, &txt_width);
  4801. }
  4802. if (!length) return;
  4803. cmd = (struct nk_command_text*)
  4804. nk_command_buffer_push(b, NK_COMMAND_TEXT, sizeof(*cmd) + (nk_size)(length + 1));
  4805. if (!cmd) return;
  4806. cmd->x = (short)r.x;
  4807. cmd->y = (short)r.y;
  4808. cmd->w = (unsigned short)r.w;
  4809. cmd->h = (unsigned short)r.h;
  4810. cmd->background = bg;
  4811. cmd->foreground = fg;
  4812. cmd->font = font;
  4813. cmd->length = length;
  4814. cmd->height = font->height;
  4815. NK_MEMCPY(cmd->string, string, (nk_size)length);
  4816. cmd->string[length] = '\0';
  4817. }
  4818. /* ==============================================================
  4819. *
  4820. * DRAW LIST
  4821. *
  4822. * ===============================================================*/
  4823. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  4824. NK_API void
  4825. nk_draw_list_init(struct nk_draw_list *list)
  4826. {
  4827. nk_size i = 0;
  4828. nk_zero(list, sizeof(*list));
  4829. for (i = 0; i < NK_LEN(list->circle_vtx); ++i) {
  4830. const float a = ((float)i / (float)NK_LEN(list->circle_vtx)) * 2 * NK_PI;
  4831. list->circle_vtx[i].x = (float)NK_COS(a);
  4832. list->circle_vtx[i].y = (float)NK_SIN(a);
  4833. }
  4834. }
  4835. NK_API void
  4836. nk_draw_list_setup(struct nk_draw_list *canvas, float global_alpha,
  4837. enum nk_anti_aliasing line_AA, enum nk_anti_aliasing shape_AA,
  4838. struct nk_draw_null_texture null, struct nk_buffer *cmds,
  4839. struct nk_buffer *vertices, struct nk_buffer *elements)
  4840. {
  4841. canvas->null = null;
  4842. canvas->clip_rect = nk_null_rect;
  4843. canvas->vertices = vertices;
  4844. canvas->elements = elements;
  4845. canvas->buffer = cmds;
  4846. canvas->line_AA = line_AA;
  4847. canvas->shape_AA = shape_AA;
  4848. canvas->global_alpha = global_alpha;
  4849. }
  4850. NK_API const struct nk_draw_command*
  4851. nk__draw_list_begin(const struct nk_draw_list *canvas, const struct nk_buffer *buffer)
  4852. {
  4853. nk_byte *memory;
  4854. nk_size offset;
  4855. const struct nk_draw_command *cmd;
  4856. NK_ASSERT(buffer);
  4857. if (!buffer || !buffer->size || !canvas->cmd_count)
  4858. return 0;
  4859. memory = (nk_byte*)buffer->memory.ptr;
  4860. offset = buffer->memory.size - canvas->cmd_offset;
  4861. cmd = nk_ptr_add(const struct nk_draw_command, memory, offset);
  4862. return cmd;
  4863. }
  4864. NK_API const struct nk_draw_command*
  4865. nk__draw_list_next(const struct nk_draw_command *cmd,
  4866. const struct nk_buffer *buffer, const struct nk_draw_list *canvas)
  4867. {
  4868. nk_byte *memory;
  4869. nk_size size;
  4870. nk_size offset;
  4871. const struct nk_draw_command *end;
  4872. NK_ASSERT(buffer);
  4873. NK_ASSERT(canvas);
  4874. if (!cmd || !buffer || !canvas)
  4875. return 0;
  4876. memory = (nk_byte*)buffer->memory.ptr;
  4877. size = buffer->memory.size;
  4878. offset = size - canvas->cmd_offset;
  4879. end = nk_ptr_add(const struct nk_draw_command, memory, offset);
  4880. end -= (canvas->cmd_count-1);
  4881. if (cmd <= end) return 0;
  4882. return (cmd-1);
  4883. }
  4884. NK_API void
  4885. nk_draw_list_clear(struct nk_draw_list *list)
  4886. {
  4887. NK_ASSERT(list);
  4888. if (!list) return;
  4889. if (list->buffer)
  4890. nk_buffer_clear(list->buffer);
  4891. if (list->elements)
  4892. nk_buffer_clear(list->vertices);
  4893. if (list->vertices)
  4894. nk_buffer_clear(list->elements);
  4895. list->element_count = 0;
  4896. list->vertex_count = 0;
  4897. list->cmd_offset = 0;
  4898. list->cmd_count = 0;
  4899. list->path_count = 0;
  4900. list->vertices = 0;
  4901. list->elements = 0;
  4902. list->clip_rect = nk_null_rect;
  4903. }
  4904. NK_INTERN struct nk_vec2*
  4905. nk_draw_list_alloc_path(struct nk_draw_list *list, int count)
  4906. {
  4907. struct nk_vec2 *points;
  4908. NK_STORAGE const nk_size point_align = NK_ALIGNOF(struct nk_vec2);
  4909. NK_STORAGE const nk_size point_size = sizeof(struct nk_vec2);
  4910. points = (struct nk_vec2*)
  4911. nk_buffer_alloc(list->buffer, NK_BUFFER_FRONT,
  4912. point_size * (nk_size)count, point_align);
  4913. if (!points) return 0;
  4914. if (!list->path_offset) {
  4915. void *memory = nk_buffer_memory(list->buffer);
  4916. list->path_offset = (unsigned int)((nk_byte*)points - (nk_byte*)memory);
  4917. }
  4918. list->path_count += (unsigned int)count;
  4919. return points;
  4920. }
  4921. NK_INTERN struct nk_vec2
  4922. nk_draw_list_path_last(struct nk_draw_list *list)
  4923. {
  4924. void *memory;
  4925. struct nk_vec2 *point;
  4926. NK_ASSERT(list->path_count);
  4927. memory = nk_buffer_memory(list->buffer);
  4928. point = nk_ptr_add(struct nk_vec2, memory, list->path_offset);
  4929. point += (list->path_count-1);
  4930. return *point;
  4931. }
  4932. NK_INTERN struct nk_draw_command*
  4933. nk_draw_list_push_command(struct nk_draw_list *list, struct nk_rect clip,
  4934. nk_handle texture)
  4935. {
  4936. NK_STORAGE const nk_size cmd_align = NK_ALIGNOF(struct nk_draw_command);
  4937. NK_STORAGE const nk_size cmd_size = sizeof(struct nk_draw_command);
  4938. struct nk_draw_command *cmd;
  4939. NK_ASSERT(list);
  4940. cmd = (struct nk_draw_command*)
  4941. nk_buffer_alloc(list->buffer, NK_BUFFER_BACK, cmd_size, cmd_align);
  4942. if (!cmd) return 0;
  4943. if (!list->cmd_count) {
  4944. nk_byte *memory = (nk_byte*)nk_buffer_memory(list->buffer);
  4945. nk_size total = nk_buffer_total(list->buffer);
  4946. memory = nk_ptr_add(nk_byte, memory, total);
  4947. list->cmd_offset = (nk_size)(memory - (nk_byte*)cmd);
  4948. }
  4949. cmd->elem_count = 0;
  4950. cmd->clip_rect = clip;
  4951. cmd->texture = texture;
  4952. list->cmd_count++;
  4953. list->clip_rect = clip;
  4954. return cmd;
  4955. }
  4956. NK_INTERN struct nk_draw_command*
  4957. nk_draw_list_command_last(struct nk_draw_list *list)
  4958. {
  4959. void *memory;
  4960. nk_size size;
  4961. struct nk_draw_command *cmd;
  4962. NK_ASSERT(list->cmd_count);
  4963. memory = nk_buffer_memory(list->buffer);
  4964. size = nk_buffer_total(list->buffer);
  4965. cmd = nk_ptr_add(struct nk_draw_command, memory, size - list->cmd_offset);
  4966. return (cmd - (list->cmd_count-1));
  4967. }
  4968. NK_INTERN void
  4969. nk_draw_list_add_clip(struct nk_draw_list *list, struct nk_rect rect)
  4970. {
  4971. NK_ASSERT(list);
  4972. if (!list) return;
  4973. if (!list->cmd_count) {
  4974. nk_draw_list_push_command(list, rect, list->null.texture);
  4975. } else {
  4976. struct nk_draw_command *prev = nk_draw_list_command_last(list);
  4977. if (prev->elem_count == 0)
  4978. prev->clip_rect = rect;
  4979. nk_draw_list_push_command(list, rect, prev->texture);
  4980. }
  4981. }
  4982. NK_INTERN void
  4983. nk_draw_list_push_image(struct nk_draw_list *list, nk_handle texture)
  4984. {
  4985. NK_ASSERT(list);
  4986. if (!list) return;
  4987. if (!list->cmd_count) {
  4988. nk_draw_list_push_command(list, nk_null_rect, texture);
  4989. } else {
  4990. struct nk_draw_command *prev = nk_draw_list_command_last(list);
  4991. if (prev->elem_count == 0)
  4992. prev->texture = texture;
  4993. else if (prev->texture.id != texture.id)
  4994. nk_draw_list_push_command(list, prev->clip_rect, texture);
  4995. }
  4996. }
  4997. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4998. NK_API void
  4999. nk_draw_list_push_userdata(struct nk_draw_list *list, nk_handle userdata)
  5000. {
  5001. NK_ASSERT(list);
  5002. if (!list) return;
  5003. if (!list->cmd_count) {
  5004. struct nk_draw_command *prev;
  5005. nk_draw_list_push_command(list, nk_null_rect, list->null.texture);
  5006. prev = nk_draw_list_command_last(list);
  5007. prev->userdata = userdata;
  5008. } else {
  5009. struct nk_draw_command *prev = nk_draw_list_command_last(list);
  5010. if (prev->elem_count == 0) {
  5011. prev->userdata = userdata;
  5012. } else if (prev->userdata.ptr != userdata.ptr) {
  5013. nk_draw_list_push_command(list, prev->clip_rect, prev->texture);
  5014. prev = nk_draw_list_command_last(list);
  5015. prev->userdata = userdata;
  5016. }
  5017. }
  5018. }
  5019. #endif
  5020. NK_INTERN struct nk_draw_vertex*
  5021. nk_draw_list_alloc_vertices(struct nk_draw_list *list, nk_size count)
  5022. {
  5023. struct nk_draw_vertex *vtx;
  5024. NK_STORAGE const nk_size vtx_align = NK_ALIGNOF(struct nk_draw_vertex);
  5025. NK_STORAGE const nk_size vtx_size = sizeof(struct nk_draw_vertex);
  5026. NK_ASSERT(list);
  5027. if (!list) return 0;
  5028. vtx = (struct nk_draw_vertex*)
  5029. nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, vtx_size*count, vtx_align);
  5030. if (!vtx) return 0;
  5031. list->vertex_count += (unsigned int)count;
  5032. return vtx;
  5033. }
  5034. NK_INTERN nk_draw_index*
  5035. nk_draw_list_alloc_elements(struct nk_draw_list *list, nk_size count)
  5036. {
  5037. nk_draw_index *ids;
  5038. struct nk_draw_command *cmd;
  5039. NK_STORAGE const nk_size elem_align = NK_ALIGNOF(nk_draw_index);
  5040. NK_STORAGE const nk_size elem_size = sizeof(nk_draw_index);
  5041. NK_ASSERT(list);
  5042. if (!list) return 0;
  5043. ids = (nk_draw_index*)
  5044. nk_buffer_alloc(list->elements, NK_BUFFER_FRONT, elem_size*count, elem_align);
  5045. if (!ids) return 0;
  5046. cmd = nk_draw_list_command_last(list);
  5047. list->element_count += (unsigned int)count;
  5048. cmd->elem_count += (unsigned int)count;
  5049. return ids;
  5050. }
  5051. NK_INTERN struct nk_draw_vertex
  5052. nk_draw_vertex(struct nk_vec2 pos, struct nk_vec2 uv, nk_draw_vertex_color col)
  5053. {
  5054. struct nk_draw_vertex out;
  5055. out.position = pos;
  5056. out.uv = uv;
  5057. out.col = col;
  5058. return out;
  5059. }
  5060. NK_API void
  5061. nk_draw_list_stroke_poly_line(struct nk_draw_list *list, const struct nk_vec2 *points,
  5062. const unsigned int points_count, struct nk_color color, enum nk_draw_list_stroke closed,
  5063. float thickness, enum nk_anti_aliasing aliasing)
  5064. {
  5065. nk_size count;
  5066. int thick_line;
  5067. nk_draw_vertex_color col;
  5068. NK_ASSERT(list);
  5069. if (!list || points_count < 2) return;
  5070. color.a = (nk_byte)((float)color.a * list->global_alpha);
  5071. col = nk_color_u32(color);
  5072. count = points_count;
  5073. if (!closed) count = points_count-1;
  5074. thick_line = thickness > 1.0f;
  5075. #ifdef NK_INCLUDE_COMMAND_USERDATA
  5076. nk_draw_list_push_userdata(list, list->userdata);
  5077. #endif
  5078. if (aliasing == NK_ANTI_ALIASING_ON) {
  5079. /* ANTI-ALIASED STROKE */
  5080. const float AA_SIZE = 1.0f;
  5081. NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
  5082. NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
  5083. const nk_draw_vertex_color col_trans = col & 0x00ffffff;
  5084. /* allocate vertices and elements */
  5085. nk_size i1 = 0;
  5086. nk_size vertex_offset;
  5087. nk_size index = list->vertex_count;
  5088. const nk_size idx_count = (thick_line) ? (count * 18) : (count * 12);
  5089. const nk_size vtx_count = (thick_line) ? (points_count * 4): (points_count *3);
  5090. struct nk_draw_vertex *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  5091. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  5092. nk_size size;
  5093. struct nk_vec2 *normals, *temp;
  5094. NK_ASSERT(vtx && ids);
  5095. if (!vtx || !ids) return;
  5096. /* temporary allocate normals + points */
  5097. vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
  5098. nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
  5099. size = pnt_size * ((thick_line) ? 5 : 3) * points_count;
  5100. normals = (struct nk_vec2*)
  5101. nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
  5102. NK_ASSERT(normals);
  5103. if (!normals) return;
  5104. vtx = (struct nk_draw_vertex*)(void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
  5105. temp = normals + points_count;
  5106. /* calculate normals */
  5107. for (i1 = 0; i1 < count; ++i1) {
  5108. const nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
  5109. struct nk_vec2 diff = nk_vec2_sub(points[i2], points[i1]);
  5110. float len;
  5111. /* vec2 inverted length */
  5112. len = nk_vec2_len_sqr(diff);
  5113. if (len != 0.0f)
  5114. len = nk_inv_sqrt(len);
  5115. else len = 1.0f;
  5116. diff = nk_vec2_muls(diff, len);
  5117. normals[i1].x = diff.y;
  5118. normals[i1].y = -diff.x;
  5119. }
  5120. if (!closed)
  5121. normals[points_count-1] = normals[points_count-2];
  5122. if (!thick_line) {
  5123. nk_size idx1, i;
  5124. if (!closed) {
  5125. struct nk_vec2 d;
  5126. temp[0] = nk_vec2_add(points[0], nk_vec2_muls(normals[0], AA_SIZE));
  5127. temp[1] = nk_vec2_sub(points[0], nk_vec2_muls(normals[0], AA_SIZE));
  5128. d = nk_vec2_muls(normals[points_count-1], AA_SIZE);
  5129. temp[(points_count-1) * 2 + 0] = nk_vec2_add(points[points_count-1], d);
  5130. temp[(points_count-1) * 2 + 1] = nk_vec2_sub(points[points_count-1], d);
  5131. }
  5132. /* fill elements */
  5133. idx1 = index;
  5134. for (i1 = 0; i1 < count; i1++) {
  5135. struct nk_vec2 dm;
  5136. float dmr2;
  5137. nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
  5138. nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 3);
  5139. /* average normals */
  5140. dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
  5141. dmr2 = dm.x * dm.x + dm.y* dm.y;
  5142. if (dmr2 > 0.000001f) {
  5143. float scale = 1.0f/dmr2;
  5144. scale = NK_MIN(100.0f, scale);
  5145. dm = nk_vec2_muls(dm, scale);
  5146. }
  5147. dm = nk_vec2_muls(dm, AA_SIZE);
  5148. temp[i2*2+0] = nk_vec2_add(points[i2], dm);
  5149. temp[i2*2+1] = nk_vec2_sub(points[i2], dm);
  5150. ids[0] = (nk_draw_index)(idx2 + 0); ids[1] = (nk_draw_index)(idx1+0);
  5151. ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
  5152. ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+0);
  5153. ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
  5154. ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
  5155. ids[10]= (nk_draw_index)(idx2 + 0); ids[11]= (nk_draw_index)(idx2+1);
  5156. ids += 12;
  5157. idx1 = idx2;
  5158. }
  5159. /* fill vertices */
  5160. for (i = 0; i < points_count; ++i) {
  5161. const struct nk_vec2 uv = list->null.uv;
  5162. vtx[0] = nk_draw_vertex(points[i], uv, col);
  5163. vtx[1] = nk_draw_vertex(temp[i*2+0], uv, col_trans);
  5164. vtx[2] = nk_draw_vertex(temp[i*2+1], uv, col_trans);
  5165. vtx += 3;
  5166. }
  5167. } else {
  5168. nk_size idx1, i;
  5169. const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f;
  5170. if (!closed) {
  5171. struct nk_vec2 d1 = nk_vec2_muls(normals[0], half_inner_thickness + AA_SIZE);
  5172. struct nk_vec2 d2 = nk_vec2_muls(normals[0], half_inner_thickness);
  5173. temp[0] = nk_vec2_add(points[0], d1);
  5174. temp[1] = nk_vec2_add(points[0], d2);
  5175. temp[2] = nk_vec2_sub(points[0], d2);
  5176. temp[3] = nk_vec2_sub(points[0], d1);
  5177. d1 = nk_vec2_muls(normals[points_count-1], half_inner_thickness + AA_SIZE);
  5178. d2 = nk_vec2_muls(normals[points_count-1], half_inner_thickness);
  5179. temp[(points_count-1)*4+0] = nk_vec2_add(points[points_count-1], d1);
  5180. temp[(points_count-1)*4+1] = nk_vec2_add(points[points_count-1], d2);
  5181. temp[(points_count-1)*4+2] = nk_vec2_sub(points[points_count-1], d2);
  5182. temp[(points_count-1)*4+3] = nk_vec2_sub(points[points_count-1], d1);
  5183. }
  5184. /* add all elements */
  5185. idx1 = index;
  5186. for (i1 = 0; i1 < count; ++i1) {
  5187. struct nk_vec2 dm_out, dm_in;
  5188. const nk_size i2 = ((i1+1) == points_count) ? 0: (i1 + 1);
  5189. nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 4);
  5190. /* average normals */
  5191. struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
  5192. float dmr2 = dm.x * dm.x + dm.y* dm.y;
  5193. if (dmr2 > 0.000001f) {
  5194. float scale = 1.0f/dmr2;
  5195. scale = NK_MIN(100.0f, scale);
  5196. dm = nk_vec2_muls(dm, scale);
  5197. }
  5198. dm_out = nk_vec2_muls(dm, ((half_inner_thickness) + AA_SIZE));
  5199. dm_in = nk_vec2_muls(dm, half_inner_thickness);
  5200. temp[i2*4+0] = nk_vec2_add(points[i2], dm_out);
  5201. temp[i2*4+1] = nk_vec2_add(points[i2], dm_in);
  5202. temp[i2*4+2] = nk_vec2_sub(points[i2], dm_in);
  5203. temp[i2*4+3] = nk_vec2_sub(points[i2], dm_out);
  5204. /* add indexes */
  5205. ids[0] = (nk_draw_index)(idx2 + 1); ids[1] = (nk_draw_index)(idx1+1);
  5206. ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
  5207. ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+1);
  5208. ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
  5209. ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
  5210. ids[10]= (nk_draw_index)(idx2 + 0); ids[11] = (nk_draw_index)(idx2+1);
  5211. ids[12]= (nk_draw_index)(idx2 + 2); ids[13] = (nk_draw_index)(idx1+2);
  5212. ids[14]= (nk_draw_index)(idx1 + 3); ids[15] = (nk_draw_index)(idx1+3);
  5213. ids[16]= (nk_draw_index)(idx2 + 3); ids[17] = (nk_draw_index)(idx2+2);
  5214. ids += 18;
  5215. idx1 = idx2;
  5216. }
  5217. /* add vertices */
  5218. for (i = 0; i < points_count; ++i) {
  5219. const struct nk_vec2 uv = list->null.uv;
  5220. vtx[0] = nk_draw_vertex(temp[i*4+0], uv, col_trans);
  5221. vtx[1] = nk_draw_vertex(temp[i*4+1], uv, col);
  5222. vtx[2] = nk_draw_vertex(temp[i*4+2], uv, col);
  5223. vtx[3] = nk_draw_vertex(temp[i*4+3], uv, col_trans);
  5224. vtx += 4;
  5225. }
  5226. }
  5227. /* free temporary normals + points */
  5228. nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
  5229. } else {
  5230. /* NON ANTI-ALIASED STROKE */
  5231. nk_size i1 = 0;
  5232. nk_size idx = list->vertex_count;
  5233. const nk_size idx_count = count * 6;
  5234. const nk_size vtx_count = count * 4;
  5235. struct nk_draw_vertex *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  5236. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  5237. if (!vtx || !ids) return;
  5238. for (i1 = 0; i1 < count; ++i1) {
  5239. float dx, dy;
  5240. const struct nk_vec2 uv = list->null.uv;
  5241. const nk_size i2 = ((i1+1) == points_count) ? 0 : i1 + 1;
  5242. const struct nk_vec2 p1 = points[i1];
  5243. const struct nk_vec2 p2 = points[i2];
  5244. struct nk_vec2 diff = nk_vec2_sub(p2, p1);
  5245. float len;
  5246. /* vec2 inverted length */
  5247. len = nk_vec2_len_sqr(diff);
  5248. if (len != 0.0f)
  5249. len = nk_inv_sqrt(len);
  5250. else len = 1.0f;
  5251. diff = nk_vec2_muls(diff, len);
  5252. /* add vertices */
  5253. dx = diff.x * (thickness * 0.5f);
  5254. dy = diff.y * (thickness * 0.5f);
  5255. vtx[0] = nk_draw_vertex(nk_vec2(p1.x + dy, p1.y - dx), uv, col);
  5256. vtx[1] = nk_draw_vertex(nk_vec2(p2.x + dy, p2.y - dx), uv, col);
  5257. vtx[2] = nk_draw_vertex(nk_vec2(p2.x - dy, p2.y + dx), uv, col);
  5258. vtx[3] = nk_draw_vertex(nk_vec2(p1.x - dy, p1.y + dx), uv, col);
  5259. vtx += 4;
  5260. ids[0] = (nk_draw_index)(idx+0); ids[1] = (nk_draw_index)(idx+1);
  5261. ids[2] = (nk_draw_index)(idx+2); ids[3] = (nk_draw_index)(idx+0);
  5262. ids[4] = (nk_draw_index)(idx+2); ids[5] = (nk_draw_index)(idx+3);
  5263. ids += 6;
  5264. idx += 4;
  5265. }
  5266. }
  5267. }
  5268. NK_API void
  5269. nk_draw_list_fill_poly_convex(struct nk_draw_list *list,
  5270. const struct nk_vec2 *points, const unsigned int points_count,
  5271. struct nk_color color, enum nk_anti_aliasing aliasing)
  5272. {
  5273. NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
  5274. NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
  5275. nk_draw_vertex_color col;
  5276. NK_ASSERT(list);
  5277. if (!list || points_count < 3) return;
  5278. #ifdef NK_INCLUDE_COMMAND_USERDATA
  5279. nk_draw_list_push_userdata(list, list->userdata);
  5280. #endif
  5281. color.a = (nk_byte)((float)color.a * list->global_alpha);
  5282. col = nk_color_u32(color);
  5283. if (aliasing == NK_ANTI_ALIASING_ON) {
  5284. nk_size i = 0;
  5285. nk_size i0 = 0;
  5286. nk_size i1 = 0;
  5287. const float AA_SIZE = 1.0f;
  5288. nk_size vertex_offset = 0;
  5289. const nk_draw_vertex_color col_trans = col & 0x00ffffff;
  5290. nk_size index = list->vertex_count;
  5291. const nk_size idx_count = (points_count-2)*3 + points_count*6;
  5292. const nk_size vtx_count = (points_count*2);
  5293. struct nk_draw_vertex *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  5294. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  5295. unsigned int vtx_inner_idx = (unsigned int)(index + 0);
  5296. unsigned int vtx_outer_idx = (unsigned int)(index + 1);
  5297. struct nk_vec2 *normals = 0;
  5298. nk_size size = 0;
  5299. if (!vtx || !ids) return;
  5300. /* temporary allocate normals */
  5301. vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
  5302. nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
  5303. size = pnt_size * points_count;
  5304. normals = (struct nk_vec2*)
  5305. nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
  5306. NK_ASSERT(normals);
  5307. if (!normals) return;
  5308. vtx = (struct nk_draw_vertex*)(void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
  5309. /* add elements */
  5310. for (i = 2; i < points_count; i++) {
  5311. ids[0] = (nk_draw_index)(vtx_inner_idx);
  5312. ids[1] = (nk_draw_index)(vtx_inner_idx + ((i-1) << 1));
  5313. ids[2] = (nk_draw_index)(vtx_inner_idx + (i << 1));
  5314. ids += 3;
  5315. }
  5316. /* compute normals */
  5317. for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
  5318. struct nk_vec2 p0 = points[i0];
  5319. struct nk_vec2 p1 = points[i1];
  5320. struct nk_vec2 diff = nk_vec2_sub(p1, p0);
  5321. /* vec2 inverted length */
  5322. float len = nk_vec2_len_sqr(diff);
  5323. if (len != 0.0f)
  5324. len = nk_inv_sqrt(len);
  5325. else len = 1.0f;
  5326. diff = nk_vec2_muls(diff, len);
  5327. normals[i0].x = diff.y;
  5328. normals[i0].y = -diff.x;
  5329. }
  5330. /* add vertices + indexes */
  5331. for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
  5332. const struct nk_vec2 uv = list->null.uv;
  5333. struct nk_vec2 n0 = normals[i0];
  5334. struct nk_vec2 n1 = normals[i1];
  5335. struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(n0, n1), 0.5f);
  5336. float dmr2 = dm.x*dm.x + dm.y*dm.y;
  5337. if (dmr2 > 0.000001f) {
  5338. float scale = 1.0f / dmr2;
  5339. scale = NK_MIN(scale, 100.0f);
  5340. dm = nk_vec2_muls(dm, scale);
  5341. }
  5342. dm = nk_vec2_muls(dm, AA_SIZE * 0.5f);
  5343. /* add vertices */
  5344. vtx[0] = nk_draw_vertex(nk_vec2_sub(points[i1], dm), uv, col);
  5345. vtx[1] = nk_draw_vertex(nk_vec2_add(points[i1], dm), uv, col_trans);
  5346. vtx += 2;
  5347. /* add indexes */
  5348. ids[0] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
  5349. ids[1] = (nk_draw_index)(vtx_inner_idx+(i0<<1));
  5350. ids[2] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
  5351. ids[3] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
  5352. ids[4] = (nk_draw_index)(vtx_outer_idx+(i1<<1));
  5353. ids[5] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
  5354. ids += 6;
  5355. }
  5356. /* free temporary normals + points */
  5357. nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
  5358. } else {
  5359. nk_size i = 0;
  5360. nk_size index = list->vertex_count;
  5361. const nk_size idx_count = (points_count-2)*3;
  5362. const nk_size vtx_count = points_count;
  5363. struct nk_draw_vertex *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  5364. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  5365. if (!vtx || !ids) return;
  5366. for (i = 0; i < vtx_count; ++i) {
  5367. vtx[0] = nk_draw_vertex(points[i], list->null.uv, col);
  5368. vtx++;
  5369. }
  5370. for (i = 2; i < points_count; ++i) {
  5371. ids[0] = (nk_draw_index)index;
  5372. ids[1] = (nk_draw_index)(index+ i - 1);
  5373. ids[2] = (nk_draw_index)(index+i);
  5374. ids += 3;
  5375. }
  5376. }
  5377. }
  5378. NK_API void
  5379. nk_draw_list_path_clear(struct nk_draw_list *list)
  5380. {
  5381. NK_ASSERT(list);
  5382. if (!list) return;
  5383. nk_buffer_reset(list->buffer, NK_BUFFER_FRONT);
  5384. list->path_count = 0;
  5385. list->path_offset = 0;
  5386. }
  5387. NK_API void
  5388. nk_draw_list_path_line_to(struct nk_draw_list *list, struct nk_vec2 pos)
  5389. {
  5390. struct nk_vec2 *points = 0;
  5391. struct nk_draw_command *cmd = 0;
  5392. NK_ASSERT(list);
  5393. if (!list) return;
  5394. if (!list->cmd_count)
  5395. nk_draw_list_add_clip(list, nk_null_rect);
  5396. cmd = nk_draw_list_command_last(list);
  5397. if (cmd && cmd->texture.ptr != list->null.texture.ptr)
  5398. nk_draw_list_push_image(list, list->null.texture);
  5399. points = nk_draw_list_alloc_path(list, 1);
  5400. if (!points) return;
  5401. points[0] = pos;
  5402. }
  5403. NK_API void
  5404. nk_draw_list_path_arc_to_fast(struct nk_draw_list *list, struct nk_vec2 center,
  5405. float radius, int a_min, int a_max)
  5406. {
  5407. NK_ASSERT(list);
  5408. if (!list) return;
  5409. if (a_min <= a_max) {
  5410. int a = 0;
  5411. for (a = a_min; a <= a_max; a++) {
  5412. const struct nk_vec2 c = list->circle_vtx[(nk_size)a % NK_LEN(list->circle_vtx)];
  5413. const float x = center.x + c.x * radius;
  5414. const float y = center.y + c.y * radius;
  5415. nk_draw_list_path_line_to(list, nk_vec2(x, y));
  5416. }
  5417. }
  5418. }
  5419. NK_API void
  5420. nk_draw_list_path_arc_to(struct nk_draw_list *list, struct nk_vec2 center,
  5421. float radius, float a_min, float a_max, unsigned int segments)
  5422. {
  5423. unsigned int i = 0;
  5424. NK_ASSERT(list);
  5425. if (!list) return;
  5426. if (radius == 0.0f) return;
  5427. for (i = 0; i <= segments; ++i) {
  5428. const float a = a_min + ((float)i / ((float)segments) * (a_max - a_min));
  5429. const float x = center.x + (float)NK_COS(a) * radius;
  5430. const float y = center.y + (float)NK_SIN(a) * radius;
  5431. nk_draw_list_path_line_to(list, nk_vec2(x, y));
  5432. }
  5433. }
  5434. NK_API void
  5435. nk_draw_list_path_rect_to(struct nk_draw_list *list, struct nk_vec2 a,
  5436. struct nk_vec2 b, float rounding)
  5437. {
  5438. float r;
  5439. NK_ASSERT(list);
  5440. if (!list) return;
  5441. r = rounding;
  5442. r = NK_MIN(r, ((b.x-a.x) < 0) ? -(b.x-a.x): (b.x-a.x));
  5443. r = NK_MIN(r, ((b.y-a.y) < 0) ? -(b.y-a.y): (b.y-a.y));
  5444. if (r == 0.0f) {
  5445. nk_draw_list_path_line_to(list, a);
  5446. nk_draw_list_path_line_to(list, nk_vec2(b.x,a.y));
  5447. nk_draw_list_path_line_to(list, b);
  5448. nk_draw_list_path_line_to(list, nk_vec2(a.x,b.y));
  5449. } else {
  5450. nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, a.y + r), r, 6, 9);
  5451. nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, a.y + r), r, 9, 12);
  5452. nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, b.y - r), r, 0, 3);
  5453. nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, b.y - r), r, 3, 6);
  5454. }
  5455. }
  5456. NK_API void
  5457. nk_draw_list_path_curve_to(struct nk_draw_list *list, struct nk_vec2 p2,
  5458. struct nk_vec2 p3, struct nk_vec2 p4, unsigned int num_segments)
  5459. {
  5460. unsigned int i_step;
  5461. float t_step;
  5462. struct nk_vec2 p1;
  5463. NK_ASSERT(list);
  5464. NK_ASSERT(list->path_count);
  5465. if (!list || !list->path_count) return;
  5466. num_segments = NK_MAX(num_segments, 1);
  5467. p1 = nk_draw_list_path_last(list);
  5468. t_step = 1.0f/(float)num_segments;
  5469. for (i_step = 1; i_step <= num_segments; ++i_step) {
  5470. float t = t_step * (float)i_step;
  5471. float u = 1.0f - t;
  5472. float w1 = u*u*u;
  5473. float w2 = 3*u*u*t;
  5474. float w3 = 3*u*t*t;
  5475. float w4 = t * t *t;
  5476. float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
  5477. float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
  5478. nk_draw_list_path_line_to(list, nk_vec2(x,y));
  5479. }
  5480. }
  5481. NK_API void
  5482. nk_draw_list_path_fill(struct nk_draw_list *list, struct nk_color color)
  5483. {
  5484. struct nk_vec2 *points;
  5485. NK_ASSERT(list);
  5486. if (!list) return;
  5487. points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
  5488. nk_draw_list_fill_poly_convex(list, points, list->path_count, color, list->shape_AA);
  5489. nk_draw_list_path_clear(list);
  5490. }
  5491. NK_API void
  5492. nk_draw_list_path_stroke(struct nk_draw_list *list, struct nk_color color,
  5493. enum nk_draw_list_stroke closed, float thickness)
  5494. {
  5495. struct nk_vec2 *points;
  5496. NK_ASSERT(list);
  5497. if (!list) return;
  5498. points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
  5499. nk_draw_list_stroke_poly_line(list, points, list->path_count, color,
  5500. closed, thickness, list->line_AA);
  5501. nk_draw_list_path_clear(list);
  5502. }
  5503. NK_API void
  5504. nk_draw_list_stroke_line(struct nk_draw_list *list, struct nk_vec2 a,
  5505. struct nk_vec2 b, struct nk_color col, float thickness)
  5506. {
  5507. NK_ASSERT(list);
  5508. if (!list || !col.a) return;
  5509. nk_draw_list_path_line_to(list, nk_vec2_add(a, nk_vec2(0.5f, 0.5f)));
  5510. nk_draw_list_path_line_to(list, nk_vec2_add(b, nk_vec2(0.5f, 0.5f)));
  5511. nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
  5512. }
  5513. NK_API void
  5514. nk_draw_list_fill_rect(struct nk_draw_list *list, struct nk_rect rect,
  5515. struct nk_color col, float rounding)
  5516. {
  5517. NK_ASSERT(list);
  5518. if (!list || !col.a) return;
  5519. nk_draw_list_path_rect_to(list, nk_vec2(rect.x + 0.5f, rect.y + 0.5f),
  5520. nk_vec2(rect.x + rect.w + 0.5f, rect.y + rect.h + 0.5f), rounding);
  5521. nk_draw_list_path_fill(list, col);
  5522. }
  5523. NK_API void
  5524. nk_draw_list_stroke_rect(struct nk_draw_list *list, struct nk_rect rect,
  5525. struct nk_color col, float rounding, float thickness)
  5526. {
  5527. NK_ASSERT(list);
  5528. if (!list || !col.a) return;
  5529. nk_draw_list_path_rect_to(list, nk_vec2(rect.x + 0.5f, rect.y + 0.5f),
  5530. nk_vec2(rect.x + rect.w + 0.5f, rect.y + rect.h + 0.5f), rounding);
  5531. nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  5532. }
  5533. NK_API void
  5534. nk_draw_list_fill_rect_multi_color(struct nk_draw_list *list, struct nk_rect rect,
  5535. struct nk_color left, struct nk_color top, struct nk_color right,
  5536. struct nk_color bottom)
  5537. {
  5538. nk_draw_vertex_color col_left = nk_color_u32(left);
  5539. nk_draw_vertex_color col_top = nk_color_u32(top);
  5540. nk_draw_vertex_color col_right = nk_color_u32(right);
  5541. nk_draw_vertex_color col_bottom = nk_color_u32(bottom);
  5542. struct nk_draw_vertex *vtx;
  5543. nk_draw_index *idx;
  5544. nk_draw_index index;
  5545. NK_ASSERT(list);
  5546. if (!list) return;
  5547. nk_draw_list_push_image(list, list->null.texture);
  5548. index = (nk_draw_index)list->vertex_count;
  5549. vtx = nk_draw_list_alloc_vertices(list, 4);
  5550. idx = nk_draw_list_alloc_elements(list, 6);
  5551. if (!vtx || !idx) return;
  5552. idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
  5553. idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
  5554. idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
  5555. vtx[0] = nk_draw_vertex(nk_vec2(rect.x, rect.y), list->null.uv, col_left);
  5556. vtx[1] = nk_draw_vertex(nk_vec2(rect.x + rect.w, rect.y), list->null.uv, col_top);
  5557. vtx[2] = nk_draw_vertex(nk_vec2(rect.x + rect.w, rect.y + rect.h), list->null.uv, col_right);
  5558. vtx[3] = nk_draw_vertex(nk_vec2(rect.x, rect.y + rect.h), list->null.uv, col_bottom);
  5559. }
  5560. NK_API void
  5561. nk_draw_list_fill_triangle(struct nk_draw_list *list, struct nk_vec2 a,
  5562. struct nk_vec2 b, struct nk_vec2 c, struct nk_color col)
  5563. {
  5564. NK_ASSERT(list);
  5565. if (!list || !col.a) return;
  5566. nk_draw_list_path_line_to(list, a);
  5567. nk_draw_list_path_line_to(list, b);
  5568. nk_draw_list_path_line_to(list, c);
  5569. nk_draw_list_path_fill(list, col);
  5570. }
  5571. NK_API void
  5572. nk_draw_list_stroke_triangle(struct nk_draw_list *list, struct nk_vec2 a,
  5573. struct nk_vec2 b, struct nk_vec2 c, struct nk_color col, float thickness)
  5574. {
  5575. NK_ASSERT(list);
  5576. if (!list || !col.a) return;
  5577. nk_draw_list_path_line_to(list, a);
  5578. nk_draw_list_path_line_to(list, b);
  5579. nk_draw_list_path_line_to(list, c);
  5580. nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  5581. }
  5582. NK_API void
  5583. nk_draw_list_fill_circle(struct nk_draw_list *list, struct nk_vec2 center,
  5584. float radius, struct nk_color col, unsigned int segs)
  5585. {
  5586. float a_max;
  5587. NK_ASSERT(list);
  5588. if (!list || !col.a) return;
  5589. a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
  5590. nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
  5591. nk_draw_list_path_fill(list, col);
  5592. }
  5593. NK_API void
  5594. nk_draw_list_stroke_circle(struct nk_draw_list *list, struct nk_vec2 center,
  5595. float radius, struct nk_color col, unsigned int segs, float thickness)
  5596. {
  5597. float a_max;
  5598. NK_ASSERT(list);
  5599. if (!list || !col.a) return;
  5600. a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
  5601. nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
  5602. nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  5603. }
  5604. NK_API void
  5605. nk_draw_list_stroke_curve(struct nk_draw_list *list, struct nk_vec2 p0,
  5606. struct nk_vec2 cp0, struct nk_vec2 cp1, struct nk_vec2 p1,
  5607. struct nk_color col, unsigned int segments, float thickness)
  5608. {
  5609. NK_ASSERT(list);
  5610. if (!list || !col.a) return;
  5611. nk_draw_list_path_line_to(list, p0);
  5612. nk_draw_list_path_curve_to(list, cp0, cp1, p1, segments);
  5613. nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
  5614. }
  5615. NK_INTERN void
  5616. nk_draw_list_push_rect_uv(struct nk_draw_list *list, struct nk_vec2 a,
  5617. struct nk_vec2 c, struct nk_vec2 uva, struct nk_vec2 uvc,
  5618. struct nk_color color)
  5619. {
  5620. nk_draw_vertex_color col = nk_color_u32(color);
  5621. struct nk_draw_vertex *vtx;
  5622. struct nk_vec2 uvb;
  5623. struct nk_vec2 uvd;
  5624. struct nk_vec2 b;
  5625. struct nk_vec2 d;
  5626. nk_draw_index *idx;
  5627. nk_draw_index index;
  5628. NK_ASSERT(list);
  5629. if (!list) return;
  5630. uvb = nk_vec2(uvc.x, uva.y);
  5631. uvd = nk_vec2(uva.x, uvc.y);
  5632. b = nk_vec2(c.x, a.y);
  5633. d = nk_vec2(a.x, c.y);
  5634. index = (nk_draw_index)list->vertex_count;
  5635. vtx = nk_draw_list_alloc_vertices(list, 4);
  5636. idx = nk_draw_list_alloc_elements(list, 6);
  5637. if (!vtx || !idx) return;
  5638. idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
  5639. idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
  5640. idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
  5641. vtx[0] = nk_draw_vertex(a, uva, col);
  5642. vtx[1] = nk_draw_vertex(b, uvb, col);
  5643. vtx[2] = nk_draw_vertex(c, uvc, col);
  5644. vtx[3] = nk_draw_vertex(d, uvd, col);
  5645. }
  5646. NK_API void
  5647. nk_draw_list_add_image(struct nk_draw_list *list, struct nk_image texture,
  5648. struct nk_rect rect, struct nk_color color)
  5649. {
  5650. NK_ASSERT(list);
  5651. if (!list) return;
  5652. /* push new command with given texture */
  5653. nk_draw_list_push_image(list, texture.handle);
  5654. if (nk_image_is_subimage(&texture)) {
  5655. /* add region inside of the texture */
  5656. struct nk_vec2 uv[2];
  5657. uv[0].x = (float)texture.region[0]/(float)texture.w;
  5658. uv[0].y = (float)texture.region[1]/(float)texture.h;
  5659. uv[1].x = (float)(texture.region[0] + texture.region[2])/(float)texture.w;
  5660. uv[1].y = (float)(texture.region[1] + texture.region[3])/(float)texture.h;
  5661. nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
  5662. nk_vec2(rect.x + rect.w, rect.y + rect.h), uv[0], uv[1], color);
  5663. } else nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
  5664. nk_vec2(rect.x + rect.w, rect.y + rect.h),
  5665. nk_vec2(0.0f, 0.0f), nk_vec2(1.0f, 1.0f),color);
  5666. }
  5667. NK_API void
  5668. nk_draw_list_add_text(struct nk_draw_list *list, const struct nk_user_font *font,
  5669. struct nk_rect rect, const char *text, int len, float font_height,
  5670. struct nk_color fg)
  5671. {
  5672. float x = 0;
  5673. int text_len = 0;
  5674. nk_rune unicode = 0;
  5675. nk_rune next = 0;
  5676. int glyph_len = 0;
  5677. int next_glyph_len = 0;
  5678. struct nk_user_font_glyph g;
  5679. NK_ASSERT(list);
  5680. if (!list || !len || !text) return;
  5681. if (rect.x > (list->clip_rect.x + list->clip_rect.w) ||
  5682. rect.y > (list->clip_rect.y + list->clip_rect.h) ||
  5683. rect.x < list->clip_rect.x || rect.y < list->clip_rect.y)
  5684. return;
  5685. nk_draw_list_push_image(list, font->texture);
  5686. x = rect.x;
  5687. glyph_len = text_len = nk_utf_decode(text, &unicode, len);
  5688. if (!glyph_len) return;
  5689. /* draw every glyph image */
  5690. while (text_len <= len && glyph_len) {
  5691. float gx, gy, gh, gw;
  5692. float char_width = 0;
  5693. if (unicode == NK_UTF_INVALID) break;
  5694. /* query currently drawn glyph information */
  5695. next_glyph_len = nk_utf_decode(text + text_len, &next, (int)len - text_len);
  5696. font->query(font->userdata, font_height, &g, unicode,
  5697. (next == NK_UTF_INVALID) ? '\0' : next);
  5698. /* calculate and draw glyph drawing rectangle and image */
  5699. gx = x + g.offset.x;
  5700. /*gy = rect.y + (rect.h/2) - (font->height/2) + g.offset.y;*/
  5701. gy = rect.y + g.offset.y;
  5702. gw = g.width; gh = g.height;
  5703. char_width = g.xadvance;
  5704. fg.a = (nk_byte)((float)fg.a * list->global_alpha);
  5705. nk_draw_list_push_rect_uv(list, nk_vec2(gx,gy), nk_vec2(gx + gw, gy+ gh),
  5706. g.uv[0], g.uv[1], fg);
  5707. /* offset next glyph */
  5708. text_len += glyph_len;
  5709. x += char_width;
  5710. glyph_len = next_glyph_len;
  5711. unicode = next;
  5712. }
  5713. }
  5714. NK_API void
  5715. nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
  5716. struct nk_buffer *vertices, struct nk_buffer *elements,
  5717. const struct nk_convert_config *config)
  5718. {
  5719. const struct nk_command *cmd;
  5720. NK_ASSERT(ctx);
  5721. NK_ASSERT(cmds);
  5722. NK_ASSERT(vertices);
  5723. NK_ASSERT(elements);
  5724. if (!ctx || !cmds || !vertices || !elements)
  5725. return;
  5726. nk_draw_list_setup(&ctx->draw_list, config->global_alpha, config->line_AA,
  5727. config->shape_AA, config->null, cmds, vertices, elements);
  5728. nk_foreach(cmd, ctx)
  5729. {
  5730. #ifdef NK_INCLUDE_COMMAND_USERDATA
  5731. list->userdata = cmd->userdata;
  5732. #endif
  5733. switch (cmd->type) {
  5734. case NK_COMMAND_NOP: break;
  5735. case NK_COMMAND_SCISSOR: {
  5736. const struct nk_command_scissor *s = (const struct nk_command_scissor*)cmd;
  5737. nk_draw_list_add_clip(&ctx->draw_list, nk_rect(s->x, s->y, s->w, s->h));
  5738. } break;
  5739. case NK_COMMAND_LINE: {
  5740. const struct nk_command_line *l = (const struct nk_command_line*)cmd;
  5741. nk_draw_list_stroke_line(&ctx->draw_list, nk_vec2(l->begin.x, l->begin.y),
  5742. nk_vec2(l->end.x, l->end.y), l->color, l->line_thickness);
  5743. } break;
  5744. case NK_COMMAND_CURVE: {
  5745. const struct nk_command_curve *q = (const struct nk_command_curve*)cmd;
  5746. nk_draw_list_stroke_curve(&ctx->draw_list, nk_vec2(q->begin.x, q->begin.y),
  5747. nk_vec2(q->ctrl[0].x, q->ctrl[0].y), nk_vec2(q->ctrl[1].x,
  5748. q->ctrl[1].y), nk_vec2(q->end.x, q->end.y), q->color,
  5749. config->curve_segment_count, q->line_thickness);
  5750. } break;
  5751. case NK_COMMAND_RECT: {
  5752. const struct nk_command_rect *r = (const struct nk_command_rect*)cmd;
  5753. nk_draw_list_stroke_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  5754. r->color, (float)r->rounding, r->line_thickness);
  5755. } break;
  5756. case NK_COMMAND_RECT_FILLED: {
  5757. const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled*)cmd;
  5758. nk_draw_list_fill_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  5759. r->color, (float)r->rounding);
  5760. } break;
  5761. case NK_COMMAND_RECT_MULTI_COLOR: {
  5762. const struct nk_command_rect_multi_color *r = (const struct nk_command_rect_multi_color*)cmd;
  5763. nk_draw_list_fill_rect_multi_color(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  5764. r->left, r->top, r->right, r->bottom);
  5765. } break;
  5766. case NK_COMMAND_CIRCLE: {
  5767. const struct nk_command_circle *c = (const struct nk_command_circle*)cmd;
  5768. nk_draw_list_stroke_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
  5769. (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
  5770. config->circle_segment_count, c->line_thickness);
  5771. } break;
  5772. case NK_COMMAND_CIRCLE_FILLED: {
  5773. const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
  5774. nk_draw_list_fill_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
  5775. (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
  5776. config->circle_segment_count);
  5777. } break;
  5778. case NK_COMMAND_ARC: {
  5779. const struct nk_command_arc *c = (const struct nk_command_arc*)cmd;
  5780. nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
  5781. nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
  5782. c->a[0], c->a[1], config->arc_segment_count);
  5783. nk_draw_list_path_stroke(&ctx->draw_list, c->color, NK_STROKE_CLOSED, c->line_thickness);
  5784. } break;
  5785. case NK_COMMAND_ARC_FILLED: {
  5786. const struct nk_command_arc_filled *c = (const struct nk_command_arc_filled*)cmd;
  5787. nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
  5788. nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
  5789. c->a[0], c->a[1], config->arc_segment_count);
  5790. nk_draw_list_path_fill(&ctx->draw_list, c->color);
  5791. } break;
  5792. case NK_COMMAND_TRIANGLE: {
  5793. const struct nk_command_triangle *t = (const struct nk_command_triangle*)cmd;
  5794. nk_draw_list_stroke_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
  5795. nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color,
  5796. t->line_thickness);
  5797. } break;
  5798. case NK_COMMAND_TRIANGLE_FILLED: {
  5799. const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled*)cmd;
  5800. nk_draw_list_fill_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
  5801. nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color);
  5802. } break;
  5803. case NK_COMMAND_POLYGON: {
  5804. int i;
  5805. const struct nk_command_polygon*p = (const struct nk_command_polygon*)cmd;
  5806. for (i = 0; i < p->point_count; ++i) {
  5807. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  5808. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  5809. }
  5810. nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_CLOSED, p->line_thickness);
  5811. } break;
  5812. case NK_COMMAND_POLYGON_FILLED: {
  5813. int i;
  5814. const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled*)cmd;
  5815. for (i = 0; i < p->point_count; ++i) {
  5816. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  5817. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  5818. }
  5819. nk_draw_list_path_fill(&ctx->draw_list, p->color);
  5820. } break;
  5821. case NK_COMMAND_POLYLINE: {
  5822. int i;
  5823. const struct nk_command_polyline *p = (const struct nk_command_polyline*)cmd;
  5824. for (i = 0; i < p->point_count; ++i) {
  5825. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  5826. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  5827. }
  5828. nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_OPEN, p->line_thickness);
  5829. } break;
  5830. case NK_COMMAND_TEXT: {
  5831. const struct nk_command_text *t = (const struct nk_command_text*)cmd;
  5832. nk_draw_list_add_text(&ctx->draw_list, t->font, nk_rect(t->x, t->y, t->w, t->h),
  5833. t->string, t->length, t->height, t->foreground);
  5834. } break;
  5835. case NK_COMMAND_IMAGE: {
  5836. const struct nk_command_image *i = (const struct nk_command_image*)cmd;
  5837. nk_draw_list_add_image(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h),
  5838. nk_rgb(255, 255, 255));
  5839. } break;
  5840. default: break;
  5841. }
  5842. }
  5843. }
  5844. NK_API const struct nk_draw_command*
  5845. nk__draw_begin(const struct nk_context *ctx,
  5846. const struct nk_buffer *buffer)
  5847. {return nk__draw_list_begin(&ctx->draw_list, buffer);}
  5848. NK_API const struct nk_draw_command*
  5849. nk__draw_next(const struct nk_draw_command *cmd,
  5850. const struct nk_buffer *buffer, const struct nk_context *ctx)
  5851. {return nk__draw_list_next(cmd, buffer, &ctx->draw_list);}
  5852. #endif
  5853. /*
  5854. * ==============================================================
  5855. *
  5856. * FONT HANDLING
  5857. *
  5858. * ===============================================================
  5859. */
  5860. #ifdef NK_INCLUDE_FONT_BAKING
  5861. /* -------------------------------------------------------------
  5862. *
  5863. * RECT PACK
  5864. *
  5865. * --------------------------------------------------------------*/
  5866. /* stb_rect_pack.h - v0.05 - public domain - rectangle packing */
  5867. /* Sean Barrett 2014 */
  5868. #define NK_RP__MAXVAL 0xffff
  5869. typedef unsigned short nk_rp_coord;
  5870. struct nk_rp_rect {
  5871. /* reserved for your use: */
  5872. int id;
  5873. /* input: */
  5874. nk_rp_coord w, h;
  5875. /* output: */
  5876. nk_rp_coord x, y;
  5877. int was_packed;
  5878. /* non-zero if valid packing */
  5879. }; /* 16 bytes, nominally */
  5880. struct nk_rp_node {
  5881. nk_rp_coord x,y;
  5882. struct nk_rp_node *next;
  5883. };
  5884. struct nk_rp_context {
  5885. int width;
  5886. int height;
  5887. int align;
  5888. int init_mode;
  5889. int heuristic;
  5890. int num_nodes;
  5891. struct nk_rp_node *active_head;
  5892. struct nk_rp_node *free_head;
  5893. struct nk_rp_node extra[2];
  5894. /* we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2' */
  5895. };
  5896. struct nk_rp__findresult {
  5897. int x,y;
  5898. struct nk_rp_node **prev_link;
  5899. };
  5900. enum NK_RP_HEURISTIC {
  5901. NK_RP_HEURISTIC_Skyline_default=0,
  5902. NK_RP_HEURISTIC_Skyline_BL_sortHeight = NK_RP_HEURISTIC_Skyline_default,
  5903. NK_RP_HEURISTIC_Skyline_BF_sortHeight
  5904. };
  5905. enum NK_RP_INIT_STATE{NK_RP__INIT_skyline = 1};
  5906. NK_INTERN void
  5907. nk_rp_setup_allow_out_of_mem(struct nk_rp_context *context, int allow_out_of_mem)
  5908. {
  5909. if (allow_out_of_mem)
  5910. /* if it's ok to run out of memory, then don't bother aligning them; */
  5911. /* this gives better packing, but may fail due to OOM (even though */
  5912. /* the rectangles easily fit). @TODO a smarter approach would be to only */
  5913. /* quantize once we've hit OOM, then we could get rid of this parameter. */
  5914. context->align = 1;
  5915. else {
  5916. /* if it's not ok to run out of memory, then quantize the widths */
  5917. /* so that num_nodes is always enough nodes. */
  5918. /* */
  5919. /* I.e. num_nodes * align >= width */
  5920. /* align >= width / num_nodes */
  5921. /* align = ceil(width/num_nodes) */
  5922. context->align = (context->width + context->num_nodes-1) / context->num_nodes;
  5923. }
  5924. }
  5925. NK_INTERN void
  5926. nk_rp_init_target(struct nk_rp_context *context, int width, int height,
  5927. struct nk_rp_node *nodes, int num_nodes)
  5928. {
  5929. int i;
  5930. #ifndef STBRP_LARGE_RECTS
  5931. NK_ASSERT(width <= 0xffff && height <= 0xffff);
  5932. #endi