nuklear.h 958 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540
  1. /*
  2. /// # Nuklear
  3. /// ![](https://cloud.githubusercontent.com/assets/8057201/11761525/ae06f0ca-a0c6-11e5-819d-5610b25f6ef4.gif)
  4. ///
  5. /// ## Contents
  6. /// 1. About section
  7. /// 2. Highlights section
  8. /// 3. Features section
  9. /// 4. Usage section
  10. /// 1. Flags section
  11. /// 2. Constants section
  12. /// 3. Dependencies section
  13. /// 5. Example section
  14. /// 6. API section
  15. /// 1. Context section
  16. /// 2. Input section
  17. /// 3. Drawing section
  18. /// 4. Window section
  19. /// 5. Layouting section
  20. /// 6. Groups section
  21. /// 7. Tree section
  22. /// 8. Properties section
  23. /// 7. License section
  24. /// 8. Changelog section
  25. /// 9. Gallery section
  26. /// 10. Credits section
  27. ///
  28. /// ## About
  29. /// This is a minimal state immediate mode graphical user interface toolkit
  30. /// written in ANSI C and licensed under public domain. It was designed as a simple
  31. /// embeddable user interface for application and does not have any dependencies,
  32. /// a default renderbackend or OS window and input handling but instead provides a very modular
  33. /// library approach by using simple input state for input and draw
  34. /// commands describing primitive shapes as output. So instead of providing a
  35. /// layered library that tries to abstract over a number of platform and
  36. /// render backends it only focuses on the actual UI.
  37. ///
  38. /// ## Highlights
  39. /// - Graphical user interface toolkit
  40. /// - Single header library
  41. /// - Written in C89 (a.k.a. ANSI C or ISO C90)
  42. /// - Small codebase (~18kLOC)
  43. /// - Focus on portability, efficiency and simplicity
  44. /// - No dependencies (not even the standard library if not wanted)
  45. /// - Fully skinnable and customizable
  46. /// - Low memory footprint with total memory control if needed or wanted
  47. /// - UTF-8 support
  48. /// - No global or hidden state
  49. /// - Customizable library modules (you can compile and use only what you need)
  50. /// - Optional font baker and vertex buffer output
  51. ///
  52. /// ## Features
  53. /// - Absolutely no platform dependent code
  54. /// - Memory management control ranging from/to
  55. /// - Ease of use by allocating everything from standard library
  56. /// - Control every byte of memory inside the library
  57. /// - Font handling control ranging from/to
  58. /// - Use your own font implementation for everything
  59. /// - Use this libraries internal font baking and handling API
  60. /// - Drawing output control ranging from/to
  61. /// - Simple shapes for more high level APIs which already have drawing capabilities
  62. /// - Hardware accessible anti-aliased vertex buffer output
  63. /// - Customizable colors and properties ranging from/to
  64. /// - Simple changes to color by filling a simple color table
  65. /// - Complete control with ability to use skinning to decorate widgets
  66. /// - Bendable UI library with widget ranging from/to
  67. /// - Basic widgets like buttons, checkboxes, slider, ...
  68. /// - Advanced widget like abstract comboboxes, contextual menus,...
  69. /// - Compile time configuration to only compile what you need
  70. /// - Subset which can be used if you do not want to link or use the standard library
  71. /// - Can be easily modified to only update on user input instead of frame updates
  72. ///
  73. /// ## Usage
  74. /// This library is self contained in one single header file and can be used either
  75. /// in header only mode or in implementation mode. The header only mode is used
  76. /// by default when included and allows including this header in other headers
  77. /// and does not contain the actual implementation. <br /><br />
  78. ///
  79. /// The implementation mode requires to define the preprocessor macro
  80. /// NK_IMPLEMENTATION in *one* .c/.cpp file before #includeing this file, e.g.:
  81. ///
  82. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~C
  83. /// #define NK_IMPLEMENTATION
  84. /// #include "nuklear.h"
  85. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  86. ///
  87. /// Also optionally define the symbols listed in the section "OPTIONAL DEFINES"
  88. /// below in header and implementation mode if you want to use additional functionality
  89. /// or need more control over the library.
  90. ///
  91. /// !!! WARNING
  92. /// Every time nuklear is included define the same compiler flags. This very important not doing so could lead to compiler errors or even worse stack corruptions.
  93. ///
  94. /// ### Flags
  95. /// Flag | Description
  96. /// --------------------------------|------------------------------------------
  97. /// NK_PRIVATE | If defined declares all functions as static, so they can only be accessed inside the file that contains the implementation
  98. /// NK_INCLUDE_FIXED_TYPES | If defined it will include header `<stdint.h>` for fixed sized types otherwise nuklear tries to select the correct type. If that fails it will throw a compiler error and you have to select the correct types yourself.
  99. /// NK_INCLUDE_DEFAULT_ALLOCATOR | If defined it will include header `<stdlib.h>` and provide additional functions to use this library without caring for memory allocation control and therefore ease memory management.
  100. /// NK_INCLUDE_STANDARD_IO | If defined it will include header `<stdio.h>` and provide additional functions depending on file loading.
  101. /// NK_INCLUDE_STANDARD_VARARGS | If defined it will include header <stdio.h> and provide additional functions depending on file loading.
  102. /// NK_INCLUDE_VERTEX_BUFFER_OUTPUT | Defining this adds a vertex draw command list backend to this library, which allows you to convert queue commands into vertex draw commands. This is mainly if you need a hardware accessible format for OpenGL, DirectX, Vulkan, Metal,...
  103. /// NK_INCLUDE_FONT_BAKING | Defining this adds `stb_truetype` and `stb_rect_pack` implementation to this library and provides font baking and rendering. If you already have font handling or do not want to use this font handler you don't have to define it.
  104. /// NK_INCLUDE_DEFAULT_FONT | Defining this adds the default font: ProggyClean.ttf into this library which can be loaded into a font atlas and allows using this library without having a truetype font
  105. /// NK_INCLUDE_COMMAND_USERDATA | Defining this adds a userdata pointer into each command. Can be useful for example if you want to provide custom shaders depending on the used widget. Can be combined with the style structures.
  106. /// NK_BUTTON_TRIGGER_ON_RELEASE | Different platforms require button clicks occurring either on buttons being pressed (up to down) or released (down to up). By default this library will react on buttons being pressed, but if you define this it will only trigger if a button is released.
  107. /// NK_ZERO_COMMAND_MEMORY | Defining this will zero out memory for each drawing command added to a drawing queue (inside nk_command_buffer_push). Zeroing command memory is very useful for fast checking (using memcmp) if command buffers are equal and avoid drawing frames when nothing on screen has changed since previous frame.
  108. ///
  109. /// !!! WARNING
  110. /// The following flags will pull in the standard C library:
  111. /// - NK_INCLUDE_DEFAULT_ALLOCATOR
  112. /// - NK_INCLUDE_STANDARD_IO
  113. /// - NK_INCLUDE_STANDARD_VARARGS
  114. ///
  115. /// !!! WARNING
  116. /// The following flags if defined need to be defined for both header and implementation:
  117. /// - NK_INCLUDE_FIXED_TYPES
  118. /// - NK_INCLUDE_DEFAULT_ALLOCATOR
  119. /// - NK_INCLUDE_STANDARD_VARARGS
  120. /// - NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  121. /// - NK_INCLUDE_FONT_BAKING
  122. /// - NK_INCLUDE_DEFAULT_FONT
  123. /// - NK_INCLUDE_STANDARD_VARARGS
  124. /// - NK_INCLUDE_COMMAND_USERDATA
  125. ///
  126. /// ### Constants
  127. /// Define | Description
  128. /// --------------------------------|---------------------------------------
  129. /// NK_BUFFER_DEFAULT_INITIAL_SIZE | Initial buffer size allocated by all buffers while using the default allocator functions included by defining NK_INCLUDE_DEFAULT_ALLOCATOR. If you don't want to allocate the default 4k memory then redefine it.
  130. /// NK_MAX_NUMBER_BUFFER | Maximum buffer size for the conversion buffer between float and string Under normal circumstances this should be more than sufficient.
  131. /// NK_INPUT_MAX | Defines the max number of bytes which can be added as text input in one frame. Under normal circumstances this should be more than sufficient.
  132. ///
  133. /// !!! WARNING
  134. /// The following constants if defined need to be defined for both header and implementation:
  135. /// - NK_MAX_NUMBER_BUFFER
  136. /// - NK_BUFFER_DEFAULT_INITIAL_SIZE
  137. /// - NK_INPUT_MAX
  138. ///
  139. /// ### Dependencies
  140. /// Function | Description
  141. /// ------------|---------------------------------------------------------------
  142. /// NK_ASSERT | If you don't define this, nuklear will use <assert.h> with assert().
  143. /// NK_MEMSET | You can define this to 'memset' or your own memset implementation replacement. If not nuklear will use its own version.
  144. /// NK_MEMCPY | You can define this to 'memcpy' or your own memcpy implementation replacement. If not nuklear will use its own version.
  145. /// NK_SQRT | You can define this to 'sqrt' or your own sqrt implementation replacement. If not nuklear will use its own slow and not highly accurate version.
  146. /// NK_SIN | You can define this to 'sinf' or your own sine implementation replacement. If not nuklear will use its own approximation implementation.
  147. /// NK_COS | You can define this to 'cosf' or your own cosine implementation replacement. If not nuklear will use its own approximation implementation.
  148. /// NK_STRTOD | You can define this to `strtod` or your own string to double conversion implementation replacement. If not defined nuklear will use its own imprecise and possibly unsafe version (does not handle nan or infinity!).
  149. /// NK_DTOA | You can define this to `dtoa` or your own double to string conversion implementation replacement. If not defined nuklear will use its own imprecise and possibly unsafe version (does not handle nan or infinity!).
  150. /// NK_VSNPRINTF| If you define `NK_INCLUDE_STANDARD_VARARGS` as well as `NK_INCLUDE_STANDARD_IO` and want to be safe define this to `vsnprintf` on compilers supporting later versions of C or C++. By default nuklear will check for your stdlib version in C as well as compiler version in C++. if `vsnprintf` is available it will define it to `vsnprintf` directly. If not defined and if you have older versions of C or C++ it will be defined to `vsprintf` which is unsafe.
  151. ///
  152. /// !!! WARNING
  153. /// The following dependencies will pull in the standard C library if not redefined:
  154. /// - NK_ASSERT
  155. ///
  156. /// !!! WARNING
  157. /// The following dependencies if defined need to be defined for both header and implementation:
  158. /// - NK_ASSERT
  159. ///
  160. /// !!! WARNING
  161. /// The following dependencies if defined need to be defined only for the implementation part:
  162. /// - NK_MEMSET
  163. /// - NK_MEMCPY
  164. /// - NK_SQRT
  165. /// - NK_SIN
  166. /// - NK_COS
  167. /// - NK_STRTOD
  168. /// - NK_DTOA
  169. /// - NK_VSNPRINTF
  170. ///
  171. /// ## Example
  172. ///
  173. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  174. /// // init gui state
  175. /// enum {EASY, HARD};
  176. /// static int op = EASY;
  177. /// static float value = 0.6f;
  178. /// static int i = 20;
  179. /// struct nk_context ctx;
  180. ///
  181. /// nk_init_fixed(&ctx, calloc(1, MAX_MEMORY), MAX_MEMORY, &font);
  182. /// if (nk_begin(&ctx, "Show", nk_rect(50, 50, 220, 220),
  183. /// NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
  184. /// // fixed widget pixel width
  185. /// nk_layout_row_static(&ctx, 30, 80, 1);
  186. /// if (nk_button_label(&ctx, "button")) {
  187. /// // event handling
  188. /// }
  189. ///
  190. /// // fixed widget window ratio width
  191. /// nk_layout_row_dynamic(&ctx, 30, 2);
  192. /// if (nk_option_label(&ctx, "easy", op == EASY)) op = EASY;
  193. /// if (nk_option_label(&ctx, "hard", op == HARD)) op = HARD;
  194. ///
  195. /// // custom widget pixel width
  196. /// nk_layout_row_begin(&ctx, NK_STATIC, 30, 2);
  197. /// {
  198. /// nk_layout_row_push(&ctx, 50);
  199. /// nk_label(&ctx, "Volume:", NK_TEXT_LEFT);
  200. /// nk_layout_row_push(&ctx, 110);
  201. /// nk_slider_float(&ctx, 0, &value, 1.0f, 0.1f);
  202. /// }
  203. /// nk_layout_row_end(&ctx);
  204. /// }
  205. /// nk_end(&ctx);
  206. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  207. ///
  208. /// ![](https://cloud.githubusercontent.com/assets/8057201/10187981/584ecd68-675c-11e5-897c-822ef534a876.png)
  209. ///
  210. /// ## API
  211. ///
  212. */
  213. #ifndef NK_SINGLE_FILE
  214. #define NK_SINGLE_FILE
  215. #endif
  216. #ifndef NK_NUKLEAR_H_
  217. #define NK_NUKLEAR_H_
  218. #ifdef __cplusplus
  219. extern "C" {
  220. #endif
  221. /*
  222. * ==============================================================
  223. *
  224. * CONSTANTS
  225. *
  226. * ===============================================================
  227. */
  228. #define NK_UNDEFINED (-1.0f)
  229. #define NK_UTF_INVALID 0xFFFD /* internal invalid utf8 rune */
  230. #define NK_UTF_SIZE 4 /* describes the number of bytes a glyph consists of*/
  231. #ifndef NK_INPUT_MAX
  232. #define NK_INPUT_MAX 16
  233. #endif
  234. #ifndef NK_MAX_NUMBER_BUFFER
  235. #define NK_MAX_NUMBER_BUFFER 64
  236. #endif
  237. #ifndef NK_SCROLLBAR_HIDING_TIMEOUT
  238. #define NK_SCROLLBAR_HIDING_TIMEOUT 4.0f
  239. #endif
  240. /*
  241. * ==============================================================
  242. *
  243. * HELPER
  244. *
  245. * ===============================================================
  246. */
  247. #ifndef NK_API
  248. #ifdef NK_PRIVATE
  249. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199409L))
  250. #define NK_API static inline
  251. #elif defined(__cplusplus)
  252. #define NK_API static inline
  253. #else
  254. #define NK_API static
  255. #endif
  256. #else
  257. #define NK_API extern
  258. #endif
  259. #endif
  260. #ifndef NK_LIB
  261. #ifdef NK_SINGLE_FILE
  262. #define NK_LIB static
  263. #else
  264. #define NK_LIB extern
  265. #endif
  266. #endif
  267. #define NK_INTERN static
  268. #define NK_STORAGE static
  269. #define NK_GLOBAL static
  270. #define NK_FLAG(x) (1 << (x))
  271. #define NK_STRINGIFY(x) #x
  272. #define NK_MACRO_STRINGIFY(x) NK_STRINGIFY(x)
  273. #define NK_STRING_JOIN_IMMEDIATE(arg1, arg2) arg1 ## arg2
  274. #define NK_STRING_JOIN_DELAY(arg1, arg2) NK_STRING_JOIN_IMMEDIATE(arg1, arg2)
  275. #define NK_STRING_JOIN(arg1, arg2) NK_STRING_JOIN_DELAY(arg1, arg2)
  276. #ifdef _MSC_VER
  277. #define NK_UNIQUE_NAME(name) NK_STRING_JOIN(name,__COUNTER__)
  278. #else
  279. #define NK_UNIQUE_NAME(name) NK_STRING_JOIN(name,__LINE__)
  280. #endif
  281. #ifndef NK_STATIC_ASSERT
  282. #define NK_STATIC_ASSERT(exp) typedef char NK_UNIQUE_NAME(_dummy_array)[(exp)?1:-1]
  283. #endif
  284. #ifndef NK_FILE_LINE
  285. #ifdef _MSC_VER
  286. #define NK_FILE_LINE __FILE__ ":" NK_MACRO_STRINGIFY(__COUNTER__)
  287. #else
  288. #define NK_FILE_LINE __FILE__ ":" NK_MACRO_STRINGIFY(__LINE__)
  289. #endif
  290. #endif
  291. #define NK_MIN(a,b) ((a) < (b) ? (a) : (b))
  292. #define NK_MAX(a,b) ((a) < (b) ? (b) : (a))
  293. #define NK_CLAMP(i,v,x) (NK_MAX(NK_MIN(v,x), i))
  294. #ifdef NK_INCLUDE_STANDARD_VARARGS
  295. #if defined(_MSC_VER) && (_MSC_VER >= 1600) /* VS 2010 and above */
  296. #include <sal.h>
  297. #define NK_PRINTF_FORMAT_STRING _Printf_format_string_
  298. #else
  299. #define NK_PRINTF_FORMAT_STRING
  300. #endif
  301. #if defined(__GNUC__)
  302. #define NK_PRINTF_VARARG_FUNC(fmtargnumber) __attribute__((format(__printf__, fmtargnumber, fmtargnumber+1)))
  303. #define NK_PRINTF_VALIST_FUNC(fmtargnumber) __attribute__((format(__printf__, fmtargnumber, 0)))
  304. #else
  305. #define NK_PRINTF_VARARG_FUNC(fmtargnumber)
  306. #define NK_PRINTF_VALIST_FUNC(fmtargnumber)
  307. #endif
  308. #include <stdarg.h> /* valist, va_start, va_end, ... */
  309. #endif
  310. /*
  311. * ===============================================================
  312. *
  313. * BASIC
  314. *
  315. * ===============================================================
  316. */
  317. #ifdef NK_INCLUDE_FIXED_TYPES
  318. #include <stdint.h>
  319. #define NK_INT8 int8_t
  320. #define NK_UINT8 uint8_t
  321. #define NK_INT16 int16_t
  322. #define NK_UINT16 uint16_t
  323. #define NK_INT32 int32_t
  324. #define NK_UINT32 uint32_t
  325. #define NK_SIZE_TYPE uintptr_t
  326. #define NK_POINTER_TYPE uintptr_t
  327. #else
  328. #ifndef NK_INT8
  329. #define NK_INT8 char
  330. #endif
  331. #ifndef NK_UINT8
  332. #define NK_UINT8 unsigned char
  333. #endif
  334. #ifndef NK_INT16
  335. #define NK_INT16 signed short
  336. #endif
  337. #ifndef NK_UINT16
  338. #define NK_UINT16 unsigned short
  339. #endif
  340. #ifndef NK_INT32
  341. #if defined(_MSC_VER)
  342. #define NK_INT32 __int32
  343. #else
  344. #define NK_INT32 signed int
  345. #endif
  346. #endif
  347. #ifndef NK_UINT32
  348. #if defined(_MSC_VER)
  349. #define NK_UINT32 unsigned __int32
  350. #else
  351. #define NK_UINT32 unsigned int
  352. #endif
  353. #endif
  354. #ifndef NK_SIZE_TYPE
  355. #if defined(_WIN64) && defined(_MSC_VER)
  356. #define NK_SIZE_TYPE unsigned __int64
  357. #elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
  358. #define NK_SIZE_TYPE unsigned __int32
  359. #elif defined(__GNUC__) || defined(__clang__)
  360. #if defined(__x86_64__) || defined(__ppc64__)
  361. #define NK_SIZE_TYPE unsigned long
  362. #else
  363. #define NK_SIZE_TYPE unsigned int
  364. #endif
  365. #else
  366. #define NK_SIZE_TYPE unsigned long
  367. #endif
  368. #endif
  369. #ifndef NK_POINTER_TYPE
  370. #if defined(_WIN64) && defined(_MSC_VER)
  371. #define NK_POINTER_TYPE unsigned __int64
  372. #elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
  373. #define NK_POINTER_TYPE unsigned __int32
  374. #elif defined(__GNUC__) || defined(__clang__)
  375. #if defined(__x86_64__) || defined(__ppc64__)
  376. #define NK_POINTER_TYPE unsigned long
  377. #else
  378. #define NK_POINTER_TYPE unsigned int
  379. #endif
  380. #else
  381. #define NK_POINTER_TYPE unsigned long
  382. #endif
  383. #endif
  384. #endif
  385. typedef NK_INT8 nk_char;
  386. typedef NK_UINT8 nk_uchar;
  387. typedef NK_UINT8 nk_byte;
  388. typedef NK_INT16 nk_short;
  389. typedef NK_UINT16 nk_ushort;
  390. typedef NK_INT32 nk_int;
  391. typedef NK_UINT32 nk_uint;
  392. typedef NK_SIZE_TYPE nk_size;
  393. typedef NK_POINTER_TYPE nk_ptr;
  394. typedef nk_uint nk_hash;
  395. typedef nk_uint nk_flags;
  396. typedef nk_uint nk_rune;
  397. /* Make sure correct type size:
  398. * This will fire with a negative subscript error if the type sizes
  399. * are set incorrectly by the compiler, and compile out if not */
  400. NK_STATIC_ASSERT(sizeof(nk_short) == 2);
  401. NK_STATIC_ASSERT(sizeof(nk_ushort) == 2);
  402. NK_STATIC_ASSERT(sizeof(nk_uint) == 4);
  403. NK_STATIC_ASSERT(sizeof(nk_int) == 4);
  404. NK_STATIC_ASSERT(sizeof(nk_byte) == 1);
  405. NK_STATIC_ASSERT(sizeof(nk_flags) >= 4);
  406. NK_STATIC_ASSERT(sizeof(nk_rune) >= 4);
  407. NK_STATIC_ASSERT(sizeof(nk_size) >= sizeof(void*));
  408. NK_STATIC_ASSERT(sizeof(nk_ptr) >= sizeof(void*));
  409. /* ============================================================================
  410. *
  411. * API
  412. *
  413. * =========================================================================== */
  414. struct nk_buffer;
  415. struct nk_allocator;
  416. struct nk_command_buffer;
  417. struct nk_draw_command;
  418. struct nk_convert_config;
  419. struct nk_style_item;
  420. struct nk_text_edit;
  421. struct nk_draw_list;
  422. struct nk_user_font;
  423. struct nk_panel;
  424. struct nk_context;
  425. struct nk_draw_vertex_layout_element;
  426. struct nk_style_button;
  427. struct nk_style_toggle;
  428. struct nk_style_selectable;
  429. struct nk_style_slide;
  430. struct nk_style_progress;
  431. struct nk_style_scrollbar;
  432. struct nk_style_edit;
  433. struct nk_style_property;
  434. struct nk_style_chart;
  435. struct nk_style_combo;
  436. struct nk_style_tab;
  437. struct nk_style_window_header;
  438. struct nk_style_window;
  439. enum {nk_false, nk_true};
  440. struct nk_color {nk_byte r,g,b,a;};
  441. struct nk_colorf {float r,g,b,a;};
  442. struct nk_vec2 {float x,y;};
  443. struct nk_vec2i {short x, y;};
  444. struct nk_rect {float x,y,w,h;};
  445. struct nk_recti {short x,y,w,h;};
  446. typedef char nk_glyph[NK_UTF_SIZE];
  447. typedef union {void *ptr; int id;} nk_handle;
  448. struct nk_image {nk_handle handle;unsigned short w,h;unsigned short region[4];};
  449. struct nk_cursor {struct nk_image img; struct nk_vec2 size, offset;};
  450. struct nk_scroll {nk_uint x, y;};
  451. enum nk_heading {NK_UP, NK_RIGHT, NK_DOWN, NK_LEFT};
  452. enum nk_button_behavior {NK_BUTTON_DEFAULT, NK_BUTTON_REPEATER};
  453. enum nk_modify {NK_FIXED = nk_false, NK_MODIFIABLE = nk_true};
  454. enum nk_orientation {NK_VERTICAL, NK_HORIZONTAL};
  455. enum nk_collapse_states {NK_MINIMIZED = nk_false, NK_MAXIMIZED = nk_true};
  456. enum nk_show_states {NK_HIDDEN = nk_false, NK_SHOWN = nk_true};
  457. enum nk_chart_type {NK_CHART_LINES, NK_CHART_COLUMN, NK_CHART_MAX};
  458. enum nk_chart_event {NK_CHART_HOVERING = 0x01, NK_CHART_CLICKED = 0x02};
  459. enum nk_color_format {NK_RGB, NK_RGBA};
  460. enum nk_popup_type {NK_POPUP_STATIC, NK_POPUP_DYNAMIC};
  461. enum nk_layout_format {NK_DYNAMIC, NK_STATIC};
  462. enum nk_tree_type {NK_TREE_NODE, NK_TREE_TAB};
  463. typedef void*(*nk_plugin_alloc)(nk_handle, void *old, nk_size);
  464. typedef void (*nk_plugin_free)(nk_handle, void *old);
  465. typedef int(*nk_plugin_filter)(const struct nk_text_edit*, nk_rune unicode);
  466. typedef void(*nk_plugin_paste)(nk_handle, struct nk_text_edit*);
  467. typedef void(*nk_plugin_copy)(nk_handle, const char*, int len);
  468. struct nk_allocator {
  469. nk_handle userdata;
  470. nk_plugin_alloc alloc;
  471. nk_plugin_free free;
  472. };
  473. enum nk_symbol_type {
  474. NK_SYMBOL_NONE,
  475. NK_SYMBOL_X,
  476. NK_SYMBOL_UNDERSCORE,
  477. NK_SYMBOL_CIRCLE_SOLID,
  478. NK_SYMBOL_CIRCLE_OUTLINE,
  479. NK_SYMBOL_RECT_SOLID,
  480. NK_SYMBOL_RECT_OUTLINE,
  481. NK_SYMBOL_TRIANGLE_UP,
  482. NK_SYMBOL_TRIANGLE_DOWN,
  483. NK_SYMBOL_TRIANGLE_LEFT,
  484. NK_SYMBOL_TRIANGLE_RIGHT,
  485. NK_SYMBOL_PLUS,
  486. NK_SYMBOL_MINUS,
  487. NK_SYMBOL_MAX
  488. };
  489. /* =============================================================================
  490. *
  491. * CONTEXT
  492. *
  493. * =============================================================================*/
  494. /*/// ### Context
  495. /// Contexts are the main entry point and the majestro of nuklear and contain all required state.
  496. /// They are used for window, memory, input, style, stack, commands and time management and need
  497. /// to be passed into all nuklear GUI specific functions.
  498. ///
  499. /// #### Usage
  500. /// To use a context it first has to be initialized which can be achieved by calling
  501. /// one of either `nk_init_default`, `nk_init_fixed`, `nk_init`, `nk_init_custom`.
  502. /// Each takes in a font handle and a specific way of handling memory. Memory control
  503. /// hereby ranges from standard library to just specifying a fixed sized block of memory
  504. /// which nuklear has to manage itself from.
  505. ///
  506. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  507. /// struct nk_context ctx;
  508. /// nk_init_xxx(&ctx, ...);
  509. /// while (1) {
  510. /// // [...]
  511. /// nk_clear(&ctx);
  512. /// }
  513. /// nk_free(&ctx);
  514. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  515. ///
  516. /// #### Reference
  517. /// Function | Description
  518. /// --------------------|-------------------------------------------------------
  519. /// __nk_init_default__ | Initializes context with standard library memory allocation (malloc,free)
  520. /// __nk_init_fixed__ | Initializes context from single fixed size memory block
  521. /// __nk_init__ | Initializes context with memory allocator callbacks for alloc and free
  522. /// __nk_init_custom__ | Initializes context from two buffers. One for draw commands the other for window/panel/table allocations
  523. /// __nk_clear__ | Called at the end of the frame to reset and prepare the context for the next frame
  524. /// __nk_free__ | Shutdown and free all memory allocated inside the context
  525. /// __nk_set_user_data__| Utility function to pass user data to draw command
  526. */
  527. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  528. /*/// #### nk_init_default
  529. /// Initializes a `nk_context` struct with a default standard library allocator.
  530. /// Should be used if you don't want to be bothered with memory management in nuklear.
  531. ///
  532. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  533. /// int nk_init_default(struct nk_context *ctx, const struct nk_user_font *font);
  534. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  535. ///
  536. /// Parameter | Description
  537. /// ------------|---------------------------------------------------------------
  538. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  539. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  540. ///
  541. /// Returns either `false(0)` on failure or `true(1)` on success.
  542. ///
  543. */
  544. NK_API int nk_init_default(struct nk_context*, const struct nk_user_font*);
  545. #endif
  546. /*/// #### nk_init_fixed
  547. /// Initializes a `nk_context` struct from single fixed size memory block
  548. /// Should be used if you want complete control over nuklear's memory management.
  549. /// Especially recommended for system with little memory or systems with virtual memory.
  550. /// For the later case you can just allocate for example 16MB of virtual memory
  551. /// and only the required amount of memory will actually be committed.
  552. ///
  553. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  554. /// int nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size, const struct nk_user_font *font);
  555. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  556. ///
  557. /// !!! Warning
  558. /// make sure the passed memory block is aligned correctly for `nk_draw_commands`.
  559. ///
  560. /// Parameter | Description
  561. /// ------------|--------------------------------------------------------------
  562. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  563. /// __memory__ | Must point to a previously allocated memory block
  564. /// __size__ | Must contain the total size of __memory__
  565. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  566. ///
  567. /// Returns either `false(0)` on failure or `true(1)` on success.
  568. */
  569. NK_API int nk_init_fixed(struct nk_context*, void *memory, nk_size size, const struct nk_user_font*);
  570. /*/// #### nk_init
  571. /// Initializes a `nk_context` struct with memory allocation callbacks for nuklear to allocate
  572. /// memory from. Used internally for `nk_init_default` and provides a kitchen sink allocation
  573. /// interface to nuklear. Can be useful for cases like monitoring memory consumption.
  574. ///
  575. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  576. /// int nk_init(struct nk_context *ctx, struct nk_allocator *alloc, const struct nk_user_font *font);
  577. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  578. ///
  579. /// Parameter | Description
  580. /// ------------|---------------------------------------------------------------
  581. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  582. /// __alloc__ | Must point to a previously allocated memory allocator
  583. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  584. ///
  585. /// Returns either `false(0)` on failure or `true(1)` on success.
  586. */
  587. NK_API int nk_init(struct nk_context*, struct nk_allocator*, const struct nk_user_font*);
  588. /*/// #### nk_init_custom
  589. /// Initializes a `nk_context` struct from two different either fixed or growing
  590. /// buffers. The first buffer is for allocating draw commands while the second buffer is
  591. /// used for allocating windows, panels and state tables.
  592. ///
  593. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  594. /// int nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font *font);
  595. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  596. ///
  597. /// Parameter | Description
  598. /// ------------|---------------------------------------------------------------
  599. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  600. /// __cmds__ | Must point to a previously initialized memory buffer either fixed or dynamic to store draw commands into
  601. /// __pool__ | Must point to a previously initialized memory buffer either fixed or dynamic to store windows, panels and tables
  602. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  603. ///
  604. /// Returns either `false(0)` on failure or `true(1)` on success.
  605. */
  606. NK_API int nk_init_custom(struct nk_context*, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font*);
  607. /*/// #### nk_clear
  608. /// Resets the context state at the end of the frame. This includes mostly
  609. /// garbage collector tasks like removing windows or table not called and therefore
  610. /// used anymore.
  611. ///
  612. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  613. /// void nk_clear(struct nk_context *ctx);
  614. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  615. ///
  616. /// Parameter | Description
  617. /// ------------|-----------------------------------------------------------
  618. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  619. */
  620. NK_API void nk_clear(struct nk_context*);
  621. /*/// #### nk_free
  622. /// Frees all memory allocated by nuklear. Not needed if context was
  623. /// initialized with `nk_init_fixed`.
  624. ///
  625. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  626. /// void nk_free(struct nk_context *ctx);
  627. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  628. ///
  629. /// Parameter | Description
  630. /// ------------|-----------------------------------------------------------
  631. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  632. */
  633. NK_API void nk_free(struct nk_context*);
  634. #ifdef NK_INCLUDE_COMMAND_USERDATA
  635. /*/// #### nk_set_user_data
  636. /// Sets the currently passed userdata passed down into each draw command.
  637. ///
  638. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  639. /// void nk_set_user_data(struct nk_context *ctx, nk_handle data);
  640. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  641. ///
  642. /// Parameter | Description
  643. /// ------------|--------------------------------------------------------------
  644. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  645. /// __data__ | Handle with either pointer or index to be passed into every draw commands
  646. */
  647. NK_API void nk_set_user_data(struct nk_context*, nk_handle handle);
  648. #endif
  649. /* =============================================================================
  650. *
  651. * INPUT
  652. *
  653. * =============================================================================*/
  654. /*/// ### Input
  655. /// The input API is responsible for holding the current input state composed of
  656. /// mouse, key and text input states.
  657. /// It is worth noting that no direct OS or window handling is done in nuklear.
  658. /// Instead all input state has to be provided by platform specific code. This on one hand
  659. /// expects more work from the user and complicates usage but on the other hand
  660. /// provides simple abstraction over a big number of platforms, libraries and other
  661. /// already provided functionality.
  662. ///
  663. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  664. /// nk_input_begin(&ctx);
  665. /// while (GetEvent(&evt)) {
  666. /// if (evt.type == MOUSE_MOVE)
  667. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  668. /// else if (evt.type == [...]) {
  669. /// // [...]
  670. /// }
  671. /// } nk_input_end(&ctx);
  672. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  673. ///
  674. /// #### Usage
  675. /// Input state needs to be provided to nuklear by first calling `nk_input_begin`
  676. /// which resets internal state like delta mouse position and button transistions.
  677. /// After `nk_input_begin` all current input state needs to be provided. This includes
  678. /// mouse motion, button and key pressed and released, text input and scrolling.
  679. /// Both event- or state-based input handling are supported by this API
  680. /// and should work without problems. Finally after all input state has been
  681. /// mirrored `nk_input_end` needs to be called to finish input process.
  682. ///
  683. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  684. /// struct nk_context ctx;
  685. /// nk_init_xxx(&ctx, ...);
  686. /// while (1) {
  687. /// Event evt;
  688. /// nk_input_begin(&ctx);
  689. /// while (GetEvent(&evt)) {
  690. /// if (evt.type == MOUSE_MOVE)
  691. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  692. /// else if (evt.type == [...]) {
  693. /// // [...]
  694. /// }
  695. /// }
  696. /// nk_input_end(&ctx);
  697. /// // [...]
  698. /// nk_clear(&ctx);
  699. /// } nk_free(&ctx);
  700. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  701. ///
  702. /// #### Reference
  703. /// Function | Description
  704. /// --------------------|-------------------------------------------------------
  705. /// __nk_input_begin__ | Begins the input mirroring process. Needs to be called before all other `nk_input_xxx` calls
  706. /// __nk_input_motion__ | Mirrors mouse cursor position
  707. /// __nk_input_key__ | Mirrors key state with either pressed or released
  708. /// __nk_input_button__ | Mirrors mouse button state with either pressed or released
  709. /// __nk_input_scroll__ | Mirrors mouse scroll values
  710. /// __nk_input_char__ | Adds a single ASCII text character into an internal text buffer
  711. /// __nk_input_glyph__ | Adds a single multi-byte UTF-8 character into an internal text buffer
  712. /// __nk_input_unicode__| Adds a single unicode rune into an internal text buffer
  713. /// __nk_input_end__ | Ends the input mirroring process by calculating state changes. Don't call any `nk_input_xxx` function referenced above after this call
  714. */
  715. enum nk_keys {
  716. NK_KEY_NONE,
  717. NK_KEY_SHIFT,
  718. NK_KEY_CTRL,
  719. NK_KEY_DEL,
  720. NK_KEY_ENTER,
  721. NK_KEY_TAB,
  722. NK_KEY_BACKSPACE,
  723. NK_KEY_COPY,
  724. NK_KEY_CUT,
  725. NK_KEY_PASTE,
  726. NK_KEY_UP,
  727. NK_KEY_DOWN,
  728. NK_KEY_LEFT,
  729. NK_KEY_RIGHT,
  730. /* Shortcuts: text field */
  731. NK_KEY_TEXT_INSERT_MODE,
  732. NK_KEY_TEXT_REPLACE_MODE,
  733. NK_KEY_TEXT_RESET_MODE,
  734. NK_KEY_TEXT_LINE_START,
  735. NK_KEY_TEXT_LINE_END,
  736. NK_KEY_TEXT_START,
  737. NK_KEY_TEXT_END,
  738. NK_KEY_TEXT_UNDO,
  739. NK_KEY_TEXT_REDO,
  740. NK_KEY_TEXT_SELECT_ALL,
  741. NK_KEY_TEXT_WORD_LEFT,
  742. NK_KEY_TEXT_WORD_RIGHT,
  743. /* Shortcuts: scrollbar */
  744. NK_KEY_SCROLL_START,
  745. NK_KEY_SCROLL_END,
  746. NK_KEY_SCROLL_DOWN,
  747. NK_KEY_SCROLL_UP,
  748. NK_KEY_MAX
  749. };
  750. enum nk_buttons {
  751. NK_BUTTON_LEFT,
  752. NK_BUTTON_MIDDLE,
  753. NK_BUTTON_RIGHT,
  754. NK_BUTTON_DOUBLE,
  755. NK_BUTTON_MAX
  756. };
  757. /*/// #### nk_input_begin
  758. /// Begins the input mirroring process by resetting text, scroll
  759. /// mouse, previous mouse position and movement as well as key state transitions,
  760. ///
  761. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  762. /// void nk_input_begin(struct nk_context*);
  763. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  764. ///
  765. /// Parameter | Description
  766. /// ------------|-----------------------------------------------------------
  767. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  768. */
  769. NK_API void nk_input_begin(struct nk_context*);
  770. /*/// #### nk_input_motion
  771. /// Mirrors current mouse position to nuklear
  772. ///
  773. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  774. /// void nk_input_motion(struct nk_context *ctx, int x, int y);
  775. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  776. ///
  777. /// Parameter | Description
  778. /// ------------|-----------------------------------------------------------
  779. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  780. /// __x__ | Must hold an integer describing the current mouse cursor x-position
  781. /// __y__ | Must hold an integer describing the current mouse cursor y-position
  782. */
  783. NK_API void nk_input_motion(struct nk_context*, int x, int y);
  784. /*/// #### nk_input_key
  785. /// Mirrors the state of a specific key to nuklear
  786. ///
  787. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  788. /// void nk_input_key(struct nk_context*, enum nk_keys key, int down);
  789. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  790. ///
  791. /// Parameter | Description
  792. /// ------------|-----------------------------------------------------------
  793. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  794. /// __key__ | Must be any value specified in enum `nk_keys` that needs to be mirrored
  795. /// __down__ | Must be 0 for key is up and 1 for key is down
  796. */
  797. NK_API void nk_input_key(struct nk_context*, enum nk_keys, int down);
  798. /*/// #### nk_input_button
  799. /// Mirrors the state of a specific mouse button to nuklear
  800. ///
  801. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  802. /// void nk_input_button(struct nk_context *ctx, enum nk_buttons btn, int x, int y, int down);
  803. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  804. ///
  805. /// Parameter | Description
  806. /// ------------|-----------------------------------------------------------
  807. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  808. /// __btn__ | Must be any value specified in enum `nk_buttons` that needs to be mirrored
  809. /// __x__ | Must contain an integer describing mouse cursor x-position on click up/down
  810. /// __y__ | Must contain an integer describing mouse cursor y-position on click up/down
  811. /// __down__ | Must be 0 for key is up and 1 for key is down
  812. */
  813. NK_API void nk_input_button(struct nk_context*, enum nk_buttons, int x, int y, int down);
  814. /*/// #### nk_input_scroll
  815. /// Copies the last mouse scroll value to nuklear. Is generally
  816. /// a scroll value. So does not have to come from mouse and could also originate
  817. /// TODO finish this sentence
  818. ///
  819. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  820. /// void nk_input_scroll(struct nk_context *ctx, struct nk_vec2 val);
  821. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  822. ///
  823. /// Parameter | Description
  824. /// ------------|-----------------------------------------------------------
  825. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  826. /// __val__ | vector with both X- as well as Y-scroll value
  827. */
  828. NK_API void nk_input_scroll(struct nk_context*, struct nk_vec2 val);
  829. /*/// #### nk_input_char
  830. /// Copies a single ASCII character into an internal text buffer
  831. /// This is basically a helper function to quickly push ASCII characters into
  832. /// nuklear.
  833. ///
  834. /// !!! Note
  835. /// Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
  836. ///
  837. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  838. /// void nk_input_char(struct nk_context *ctx, char c);
  839. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  840. ///
  841. /// Parameter | Description
  842. /// ------------|-----------------------------------------------------------
  843. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  844. /// __c__ | Must be a single ASCII character preferable one that can be printed
  845. */
  846. NK_API void nk_input_char(struct nk_context*, char);
  847. /*/// #### nk_input_glyph
  848. /// Converts an encoded unicode rune into UTF-8 and copies the result into an
  849. /// internal text buffer.
  850. ///
  851. /// !!! Note
  852. /// Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
  853. ///
  854. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  855. /// void nk_input_glyph(struct nk_context *ctx, const nk_glyph g);
  856. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  857. ///
  858. /// Parameter | Description
  859. /// ------------|-----------------------------------------------------------
  860. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  861. /// __g__ | UTF-32 unicode codepoint
  862. */
  863. NK_API void nk_input_glyph(struct nk_context*, const nk_glyph);
  864. /*/// #### nk_input_unicode
  865. /// Converts a unicode rune into UTF-8 and copies the result
  866. /// into an internal text buffer.
  867. /// !!! Note
  868. /// Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
  869. ///
  870. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  871. /// void nk_input_unicode(struct nk_context*, nk_rune rune);
  872. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  873. ///
  874. /// Parameter | Description
  875. /// ------------|-----------------------------------------------------------
  876. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  877. /// __rune__ | UTF-32 unicode codepoint
  878. */
  879. NK_API void nk_input_unicode(struct nk_context*, nk_rune);
  880. /*/// #### nk_input_end
  881. /// End the input mirroring process by resetting mouse grabbing
  882. /// state to ensure the mouse cursor is not grabbed indefinitely.
  883. ///
  884. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  885. /// void nk_input_end(struct nk_context *ctx);
  886. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  887. ///
  888. /// Parameter | Description
  889. /// ------------|-----------------------------------------------------------
  890. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  891. */
  892. NK_API void nk_input_end(struct nk_context*);
  893. /* =============================================================================
  894. *
  895. * DRAWING
  896. *
  897. * =============================================================================*/
  898. /*/// ### Drawing
  899. /// This library was designed to be render backend agnostic so it does
  900. /// not draw anything to screen directly. Instead all drawn shapes, widgets
  901. /// are made of, are buffered into memory and make up a command queue.
  902. /// Each frame therefore fills the command buffer with draw commands
  903. /// that then need to be executed by the user and his own render backend.
  904. /// After that the command buffer needs to be cleared and a new frame can be
  905. /// started. It is probably important to note that the command buffer is the main
  906. /// drawing API and the optional vertex buffer API only takes this format and
  907. /// converts it into a hardware accessible format.
  908. ///
  909. /// #### Usage
  910. /// To draw all draw commands accumulated over a frame you need your own render
  911. /// backend able to draw a number of 2D primitives. This includes at least
  912. /// filled and stroked rectangles, circles, text, lines, triangles and scissors.
  913. /// As soon as this criterion is met you can iterate over each draw command
  914. /// and execute each draw command in a interpreter like fashion:
  915. ///
  916. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  917. /// const struct nk_command *cmd = 0;
  918. /// nk_foreach(cmd, &ctx) {
  919. /// switch (cmd->type) {
  920. /// case NK_COMMAND_LINE:
  921. /// your_draw_line_function(...)
  922. /// break;
  923. /// case NK_COMMAND_RECT
  924. /// your_draw_rect_function(...)
  925. /// break;
  926. /// case //...:
  927. /// //[...]
  928. /// }
  929. /// }
  930. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  931. ///
  932. /// In program flow context draw commands need to be executed after input has been
  933. /// gathered and the complete UI with windows and their contained widgets have
  934. /// been executed and before calling `nk_clear` which frees all previously
  935. /// allocated draw commands.
  936. ///
  937. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  938. /// struct nk_context ctx;
  939. /// nk_init_xxx(&ctx, ...);
  940. /// while (1) {
  941. /// Event evt;
  942. /// nk_input_begin(&ctx);
  943. /// while (GetEvent(&evt)) {
  944. /// if (evt.type == MOUSE_MOVE)
  945. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  946. /// else if (evt.type == [...]) {
  947. /// [...]
  948. /// }
  949. /// }
  950. /// nk_input_end(&ctx);
  951. /// //
  952. /// // [...]
  953. /// //
  954. /// const struct nk_command *cmd = 0;
  955. /// nk_foreach(cmd, &ctx) {
  956. /// switch (cmd->type) {
  957. /// case NK_COMMAND_LINE:
  958. /// your_draw_line_function(...)
  959. /// break;
  960. /// case NK_COMMAND_RECT
  961. /// your_draw_rect_function(...)
  962. /// break;
  963. /// case ...:
  964. /// // [...]
  965. /// }
  966. /// nk_clear(&ctx);
  967. /// }
  968. /// nk_free(&ctx);
  969. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  970. ///
  971. /// You probably noticed that you have to draw all of the UI each frame which is
  972. /// quite wasteful. While the actual UI updating loop is quite fast rendering
  973. /// without actually needing it is not. So there are multiple things you could do.
  974. ///
  975. /// First is only update on input. This of course is only an option if your
  976. /// application only depends on the UI and does not require any outside calculations.
  977. /// If you actually only update on input make sure to update the UI two times each
  978. /// frame and call `nk_clear` directly after the first pass and only draw in
  979. /// the second pass. In addition it is recommended to also add additional timers
  980. /// to make sure the UI is not drawn more than a fixed number of frames per second.
  981. ///
  982. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  983. /// struct nk_context ctx;
  984. /// nk_init_xxx(&ctx, ...);
  985. /// while (1) {
  986. /// // [...wait for input ]
  987. /// // [...do two UI passes ...]
  988. /// do_ui(...)
  989. /// nk_clear(&ctx);
  990. /// do_ui(...)
  991. /// //
  992. /// // draw
  993. /// const struct nk_command *cmd = 0;
  994. /// nk_foreach(cmd, &ctx) {
  995. /// switch (cmd->type) {
  996. /// case NK_COMMAND_LINE:
  997. /// your_draw_line_function(...)
  998. /// break;
  999. /// case NK_COMMAND_RECT
  1000. /// your_draw_rect_function(...)
  1001. /// break;
  1002. /// case ...:
  1003. /// //[...]
  1004. /// }
  1005. /// nk_clear(&ctx);
  1006. /// }
  1007. /// nk_free(&ctx);
  1008. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1009. ///
  1010. /// The second probably more applicable trick is to only draw if anything changed.
  1011. /// It is not really useful for applications with continuous draw loop but
  1012. /// quite useful for desktop applications. To actually get nuklear to only
  1013. /// draw on changes you first have to define `NK_ZERO_COMMAND_MEMORY` and
  1014. /// allocate a memory buffer that will store each unique drawing output.
  1015. /// After each frame you compare the draw command memory inside the library
  1016. /// with your allocated buffer by memcmp. If memcmp detects differences
  1017. /// you have to copy the command buffer into the allocated buffer
  1018. /// and then draw like usual (this example uses fixed memory but you could
  1019. /// use dynamically allocated memory).
  1020. ///
  1021. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1022. /// //[... other defines ...]
  1023. /// #define NK_ZERO_COMMAND_MEMORY
  1024. /// #include "nuklear.h"
  1025. /// //
  1026. /// // setup context
  1027. /// struct nk_context ctx;
  1028. /// void *last = calloc(1,64*1024);
  1029. /// void *buf = calloc(1,64*1024);
  1030. /// nk_init_fixed(&ctx, buf, 64*1024);
  1031. /// //
  1032. /// // loop
  1033. /// while (1) {
  1034. /// // [...input...]
  1035. /// // [...ui...]
  1036. /// void *cmds = nk_buffer_memory(&ctx.memory);
  1037. /// if (memcmp(cmds, last, ctx.memory.allocated)) {
  1038. /// memcpy(last,cmds,ctx.memory.allocated);
  1039. /// const struct nk_command *cmd = 0;
  1040. /// nk_foreach(cmd, &ctx) {
  1041. /// switch (cmd->type) {
  1042. /// case NK_COMMAND_LINE:
  1043. /// your_draw_line_function(...)
  1044. /// break;
  1045. /// case NK_COMMAND_RECT
  1046. /// your_draw_rect_function(...)
  1047. /// break;
  1048. /// case ...:
  1049. /// // [...]
  1050. /// }
  1051. /// }
  1052. /// }
  1053. /// nk_clear(&ctx);
  1054. /// }
  1055. /// nk_free(&ctx);
  1056. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1057. ///
  1058. /// Finally while using draw commands makes sense for higher abstracted platforms like
  1059. /// X11 and Win32 or drawing libraries it is often desirable to use graphics
  1060. /// hardware directly. Therefore it is possible to just define
  1061. /// `NK_INCLUDE_VERTEX_BUFFER_OUTPUT` which includes optional vertex output.
  1062. /// To access the vertex output you first have to convert all draw commands into
  1063. /// vertexes by calling `nk_convert` which takes in your preferred vertex format.
  1064. /// After successfully converting all draw commands just iterate over and execute all
  1065. /// vertex draw commands:
  1066. ///
  1067. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1068. /// // fill configuration
  1069. /// struct nk_convert_config cfg = {};
  1070. /// static const struct nk_draw_vertex_layout_element vertex_layout[] = {
  1071. /// {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct your_vertex, pos)},
  1072. /// {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct your_vertex, uv)},
  1073. /// {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct your_vertex, col)},
  1074. /// {NK_VERTEX_LAYOUT_END}
  1075. /// };
  1076. /// cfg.shape_AA = NK_ANTI_ALIASING_ON;
  1077. /// cfg.line_AA = NK_ANTI_ALIASING_ON;
  1078. /// cfg.vertex_layout = vertex_layout;
  1079. /// cfg.vertex_size = sizeof(struct your_vertex);
  1080. /// cfg.vertex_alignment = NK_ALIGNOF(struct your_vertex);
  1081. /// cfg.circle_segment_count = 22;
  1082. /// cfg.curve_segment_count = 22;
  1083. /// cfg.arc_segment_count = 22;
  1084. /// cfg.global_alpha = 1.0f;
  1085. /// cfg.null = dev->null;
  1086. /// //
  1087. /// // setup buffers and convert
  1088. /// struct nk_buffer cmds, verts, idx;
  1089. /// nk_buffer_init_default(&cmds);
  1090. /// nk_buffer_init_default(&verts);
  1091. /// nk_buffer_init_default(&idx);
  1092. /// nk_convert(&ctx, &cmds, &verts, &idx, &cfg);
  1093. /// //
  1094. /// // draw
  1095. /// nk_draw_foreach(cmd, &ctx, &cmds) {
  1096. /// if (!cmd->elem_count) continue;
  1097. /// //[...]
  1098. /// }
  1099. /// nk_buffer_free(&cms);
  1100. /// nk_buffer_free(&verts);
  1101. /// nk_buffer_free(&idx);
  1102. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1103. ///
  1104. /// #### Reference
  1105. /// Function | Description
  1106. /// --------------------|-------------------------------------------------------
  1107. /// __nk__begin__ | Returns the first draw command in the context draw command list to be drawn
  1108. /// __nk__next__ | Increments the draw command iterator to the next command inside the context draw command list
  1109. /// __nk_foreach__ | Iterates over each draw command inside the context draw command list
  1110. /// __nk_convert__ | Converts from the abstract draw commands list into a hardware accessible vertex format
  1111. /// __nk_draw_begin__ | Returns the first vertex command in the context vertex draw list to be executed
  1112. /// __nk__draw_next__ | Increments the vertex command iterator to the next command inside the context vertex command list
  1113. /// __nk__draw_end__ | Returns the end of the vertex draw list
  1114. /// __nk_draw_foreach__ | Iterates over each vertex draw command inside the vertex draw list
  1115. */
  1116. enum nk_anti_aliasing {NK_ANTI_ALIASING_OFF, NK_ANTI_ALIASING_ON};
  1117. enum nk_convert_result {
  1118. NK_CONVERT_SUCCESS = 0,
  1119. NK_CONVERT_INVALID_PARAM = 1,
  1120. NK_CONVERT_COMMAND_BUFFER_FULL = NK_FLAG(1),
  1121. NK_CONVERT_VERTEX_BUFFER_FULL = NK_FLAG(2),
  1122. NK_CONVERT_ELEMENT_BUFFER_FULL = NK_FLAG(3)
  1123. };
  1124. struct nk_draw_null_texture {
  1125. nk_handle texture; /* texture handle to a texture with a white pixel */
  1126. struct nk_vec2 uv; /* coordinates to a white pixel in the texture */
  1127. };
  1128. struct nk_convert_config {
  1129. float global_alpha; /* global alpha value */
  1130. enum nk_anti_aliasing line_AA; /* line anti-aliasing flag can be turned off if you are tight on memory */
  1131. enum nk_anti_aliasing shape_AA; /* shape anti-aliasing flag can be turned off if you are tight on memory */
  1132. unsigned circle_segment_count; /* number of segments used for circles: default to 22 */
  1133. unsigned arc_segment_count; /* number of segments used for arcs: default to 22 */
  1134. unsigned curve_segment_count; /* number of segments used for curves: default to 22 */
  1135. struct nk_draw_null_texture null; /* handle to texture with a white pixel for shape drawing */
  1136. const struct nk_draw_vertex_layout_element *vertex_layout; /* describes the vertex output format and packing */
  1137. nk_size vertex_size; /* sizeof one vertex for vertex packing */
  1138. nk_size vertex_alignment; /* vertex alignment: Can be obtained by NK_ALIGNOF */
  1139. };
  1140. /*/// #### nk__begin
  1141. /// Returns a draw command list iterator to iterate all draw
  1142. /// commands accumulated over one frame.
  1143. ///
  1144. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1145. /// const struct nk_command* nk__begin(struct nk_context*);
  1146. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1147. ///
  1148. /// Parameter | Description
  1149. /// ------------|-----------------------------------------------------------
  1150. /// __ctx__ | must point to an previously initialized `nk_context` struct at the end of a frame
  1151. ///
  1152. /// Returns draw command pointer pointing to the first command inside the draw command list
  1153. */
  1154. NK_API const struct nk_command* nk__begin(struct nk_context*);
  1155. /*/// #### nk__next
  1156. /// Returns draw command pointer pointing to the next command inside the draw command list
  1157. ///
  1158. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1159. /// const struct nk_command* nk__next(struct nk_context*, const struct nk_command*);
  1160. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1161. ///
  1162. /// Parameter | Description
  1163. /// ------------|-----------------------------------------------------------
  1164. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1165. /// __cmd__ | Must point to an previously a draw command either returned by `nk__begin` or `nk__next`
  1166. ///
  1167. /// Returns draw command pointer pointing to the next command inside the draw command list
  1168. */
  1169. NK_API const struct nk_command* nk__next(struct nk_context*, const struct nk_command*);
  1170. /*/// #### nk_foreach
  1171. /// Iterates over each draw command inside the context draw command list
  1172. ///
  1173. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1174. /// #define nk_foreach(c, ctx)
  1175. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1176. ///
  1177. /// Parameter | Description
  1178. /// ------------|-----------------------------------------------------------
  1179. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1180. /// __cmd__ | Command pointer initialized to NULL
  1181. ///
  1182. /// Iterates over each draw command inside the context draw command list
  1183. */
  1184. #define nk_foreach(c, ctx) for((c) = nk__begin(ctx); (c) != 0; (c) = nk__next(ctx,c))
  1185. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  1186. /*/// #### nk_convert
  1187. /// Converts all internal draw commands into vertex draw commands and fills
  1188. /// three buffers with vertexes, vertex draw commands and vertex indices. The vertex format
  1189. /// as well as some other configuration values have to be configured by filling out a
  1190. /// `nk_convert_config` struct.
  1191. ///
  1192. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1193. /// nk_flags nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
  1194. // struct nk_buffer *vertices, struct nk_buffer *elements, const struct nk_convert_config*);
  1195. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1196. ///
  1197. /// Parameter | Description
  1198. /// ------------|-----------------------------------------------------------
  1199. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1200. /// __cmds__ | Must point to a previously initialized buffer to hold converted vertex draw commands
  1201. /// __vertices__| Must point to a previously initialized buffer to hold all produced vertices
  1202. /// __elements__| Must point to a previously initialized buffer to hold all produced vertex indices
  1203. /// __config__ | Must point to a filled out `nk_config` struct to configure the conversion process
  1204. ///
  1205. /// Returns one of enum nk_convert_result error codes
  1206. ///
  1207. /// Parameter | Description
  1208. /// --------------------------------|-----------------------------------------------------------
  1209. /// NK_CONVERT_SUCCESS | Signals a successful draw command to vertex buffer conversion
  1210. /// NK_CONVERT_INVALID_PARAM | An invalid argument was passed in the function call
  1211. /// NK_CONVERT_COMMAND_BUFFER_FULL | The provided buffer for storing draw commands is full or failed to allocate more memory
  1212. /// NK_CONVERT_VERTEX_BUFFER_FULL | The provided buffer for storing vertices is full or failed to allocate more memory
  1213. /// NK_CONVERT_ELEMENT_BUFFER_FULL | The provided buffer for storing indicies is full or failed to allocate more memory
  1214. */
  1215. NK_API nk_flags nk_convert(struct nk_context*, struct nk_buffer *cmds, struct nk_buffer *vertices, struct nk_buffer *elements, const struct nk_convert_config*);
  1216. /*/// #### nk__draw_begin
  1217. /// Returns a draw vertex command buffer iterator to iterate over the vertex draw command buffer
  1218. ///
  1219. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1220. /// const struct nk_draw_command* nk__draw_begin(const struct nk_context*, const struct nk_buffer*);
  1221. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1222. ///
  1223. /// Parameter | Description
  1224. /// ------------|-----------------------------------------------------------
  1225. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1226. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1227. ///
  1228. /// Returns vertex draw command pointer pointing to the first command inside the vertex draw command buffer
  1229. */
  1230. NK_API const struct nk_draw_command* nk__draw_begin(const struct nk_context*, const struct nk_buffer*);
  1231. /*/// #### nk__draw_end
  1232. /// Returns the vertex draw command at the end of the vertex draw command buffer
  1233. ///
  1234. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1235. /// const struct nk_draw_command* nk__draw_end(const struct nk_context *ctx, const struct nk_buffer *buf);
  1236. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1237. ///
  1238. /// Parameter | Description
  1239. /// ------------|-----------------------------------------------------------
  1240. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1241. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1242. ///
  1243. /// Returns vertex draw command pointer pointing to the end of the last vertex draw command inside the vertex draw command buffer
  1244. */
  1245. NK_API const struct nk_draw_command* nk__draw_end(const struct nk_context*, const struct nk_buffer*);
  1246. /*/// #### nk__draw_next
  1247. /// Increments the vertex draw command buffer iterator
  1248. ///
  1249. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1250. /// const struct nk_draw_command* nk__draw_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_context*);
  1251. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1252. ///
  1253. /// Parameter | Description
  1254. /// ------------|-----------------------------------------------------------
  1255. /// __cmd__ | Must point to an previously either by `nk__draw_begin` or `nk__draw_next` returned vertex draw command
  1256. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1257. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1258. ///
  1259. /// Returns vertex draw command pointer pointing to the end of the last vertex draw command inside the vertex draw command buffer
  1260. */
  1261. NK_API const struct nk_draw_command* nk__draw_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_context*);
  1262. /*/// #### nk_draw_foreach
  1263. /// Iterates over each vertex draw command inside a vertex draw command buffer
  1264. ///
  1265. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1266. /// #define nk_draw_foreach(cmd,ctx, b)
  1267. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1268. ///
  1269. /// Parameter | Description
  1270. /// ------------|-----------------------------------------------------------
  1271. /// __cmd__ | `nk_draw_command`iterator set to NULL
  1272. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1273. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1274. */
  1275. #define nk_draw_foreach(cmd,ctx, b) for((cmd)=nk__draw_begin(ctx, b); (cmd)!=0; (cmd)=nk__draw_next(cmd, b, ctx))
  1276. #endif
  1277. /* =============================================================================
  1278. *
  1279. * WINDOW
  1280. *
  1281. * =============================================================================
  1282. /// ### Window
  1283. /// Windows are the main persistent state used inside nuklear and are life time
  1284. /// controlled by simply "retouching" (i.e. calling) each window each frame.
  1285. /// All widgets inside nuklear can only be added inside the function pair `nk_begin_xxx`
  1286. /// and `nk_end`. Calling any widgets outside these two functions will result in an
  1287. /// assert in debug or no state change in release mode.<br /><br />
  1288. ///
  1289. /// Each window holds frame persistent state like position, size, flags, state tables,
  1290. /// and some garbage collected internal persistent widget state. Each window
  1291. /// is linked into a window stack list which determines the drawing and overlapping
  1292. /// order. The topmost window thereby is the currently active window.<br /><br />
  1293. ///
  1294. /// To change window position inside the stack occurs either automatically by
  1295. /// user input by being clicked on or programmatically by calling `nk_window_focus`.
  1296. /// Windows by default are visible unless explicitly being defined with flag
  1297. /// `NK_WINDOW_HIDDEN`, the user clicked the close button on windows with flag
  1298. /// `NK_WINDOW_CLOSABLE` or if a window was explicitly hidden by calling
  1299. /// `nk_window_show`. To explicitly close and destroy a window call `nk_window_close`.<br /><br />
  1300. ///
  1301. /// #### Usage
  1302. /// To create and keep a window you have to call one of the two `nk_begin_xxx`
  1303. /// functions to start window declarations and `nk_end` at the end. Furthermore it
  1304. /// is recommended to check the return value of `nk_begin_xxx` and only process
  1305. /// widgets inside the window if the value is not 0. Either way you have to call
  1306. /// `nk_end` at the end of window declarations. Furthermore, do not attempt to
  1307. /// nest `nk_begin_xxx` calls which will hopefully result in an assert or if not
  1308. /// in a segmentation fault.
  1309. ///
  1310. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1311. /// if (nk_begin_xxx(...) {
  1312. /// // [... widgets ...]
  1313. /// }
  1314. /// nk_end(ctx);
  1315. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1316. ///
  1317. /// In the grand concept window and widget declarations need to occur after input
  1318. /// handling and before drawing to screen. Not doing so can result in higher
  1319. /// latency or at worst invalid behavior. Furthermore make sure that `nk_clear`
  1320. /// is called at the end of the frame. While nuklear's default platform backends
  1321. /// already call `nk_clear` for you if you write your own backend not calling
  1322. /// `nk_clear` can cause asserts or even worse undefined behavior.
  1323. ///
  1324. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1325. /// struct nk_context ctx;
  1326. /// nk_init_xxx(&ctx, ...);
  1327. /// while (1) {
  1328. /// Event evt;
  1329. /// nk_input_begin(&ctx);
  1330. /// while (GetEvent(&evt)) {
  1331. /// if (evt.type == MOUSE_MOVE)
  1332. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  1333. /// else if (evt.type == [...]) {
  1334. /// nk_input_xxx(...);
  1335. /// }
  1336. /// }
  1337. /// nk_input_end(&ctx);
  1338. ///
  1339. /// if (nk_begin_xxx(...) {
  1340. /// //[...]
  1341. /// }
  1342. /// nk_end(ctx);
  1343. ///
  1344. /// const struct nk_command *cmd = 0;
  1345. /// nk_foreach(cmd, &ctx) {
  1346. /// case NK_COMMAND_LINE:
  1347. /// your_draw_line_function(...)
  1348. /// break;
  1349. /// case NK_COMMAND_RECT
  1350. /// your_draw_rect_function(...)
  1351. /// break;
  1352. /// case //...:
  1353. /// //[...]
  1354. /// }
  1355. /// nk_clear(&ctx);
  1356. /// }
  1357. /// nk_free(&ctx);
  1358. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1359. ///
  1360. /// #### Reference
  1361. /// Function | Description
  1362. /// ------------------------------------|----------------------------------------
  1363. /// nk_begin | Starts a new window; needs to be called every frame for every window (unless hidden) or otherwise the window gets removed
  1364. /// nk_begin_titled | Extended window start with separated title and identifier to allow multiple windows with same name but not title
  1365. /// nk_end | Needs to be called at the end of the window building process to process scaling, scrollbars and general cleanup
  1366. //
  1367. /// nk_window_find | Finds and returns the window with give name
  1368. /// nk_window_get_bounds | Returns a rectangle with screen position and size of the currently processed window.
  1369. /// nk_window_get_position | Returns the position of the currently processed window
  1370. /// nk_window_get_size | Returns the size with width and height of the currently processed window
  1371. /// nk_window_get_width | Returns the width of the currently processed window
  1372. /// nk_window_get_height | Returns the height of the currently processed window
  1373. /// nk_window_get_panel | Returns the underlying panel which contains all processing state of the current window
  1374. /// nk_window_get_content_region | Returns the position and size of the currently visible and non-clipped space inside the currently processed window
  1375. /// nk_window_get_content_region_min | Returns the upper rectangle position of the currently visible and non-clipped space inside the currently processed window
  1376. /// nk_window_get_content_region_max | Returns the upper rectangle position of the currently visible and non-clipped space inside the currently processed window
  1377. /// nk_window_get_content_region_size | Returns the size of the currently visible and non-clipped space inside the currently processed window
  1378. /// nk_window_get_canvas | Returns the draw command buffer. Can be used to draw custom widgets
  1379. /// nk_window_has_focus | Returns if the currently processed window is currently active
  1380. /// nk_window_is_collapsed | Returns if the window with given name is currently minimized/collapsed
  1381. /// nk_window_is_closed | Returns if the currently processed window was closed
  1382. /// nk_window_is_hidden | Returns if the currently processed window was hidden
  1383. /// nk_window_is_active | Same as nk_window_has_focus for some reason
  1384. /// nk_window_is_hovered | Returns if the currently processed window is currently being hovered by mouse
  1385. /// nk_window_is_any_hovered | Return if any window currently hovered
  1386. /// nk_item_is_any_active | Returns if any window or widgets is currently hovered or active
  1387. //
  1388. /// nk_window_set_bounds | Updates position and size of the currently processed window
  1389. /// nk_window_set_position | Updates position of the currently process window
  1390. /// nk_window_set_size | Updates the size of the currently processed window
  1391. /// nk_window_set_focus | Set the currently processed window as active window
  1392. //
  1393. /// nk_window_close | Closes the window with given window name which deletes the window at the end of the frame
  1394. /// nk_window_collapse | Collapses the window with given window name
  1395. /// nk_window_collapse_if | Collapses the window with given window name if the given condition was met
  1396. /// nk_window_show | Hides a visible or reshows a hidden window
  1397. /// nk_window_show_if | Hides/shows a window depending on condition
  1398. */
  1399. /*
  1400. /// #### nk_panel_flags
  1401. /// Flag | Description
  1402. /// ----------------------------|----------------------------------------
  1403. /// NK_WINDOW_BORDER | Draws a border around the window to visually separate window from the background
  1404. /// NK_WINDOW_MOVABLE | The movable flag indicates that a window can be moved by user input or by dragging the window header
  1405. /// NK_WINDOW_SCALABLE | The scalable flag indicates that a window can be scaled by user input by dragging a scaler icon at the button of the window
  1406. /// NK_WINDOW_CLOSABLE | Adds a closable icon into the header
  1407. /// NK_WINDOW_MINIMIZABLE | Adds a minimize icon into the header
  1408. /// NK_WINDOW_NO_SCROLLBAR | Removes the scrollbar from the window
  1409. /// NK_WINDOW_TITLE | Forces a header at the top at the window showing the title
  1410. /// NK_WINDOW_SCROLL_AUTO_HIDE | Automatically hides the window scrollbar if no user interaction: also requires delta time in `nk_context` to be set each frame
  1411. /// NK_WINDOW_BACKGROUND | Always keep window in the background
  1412. /// NK_WINDOW_SCALE_LEFT | Puts window scaler in the left-ottom corner instead right-bottom
  1413. /// NK_WINDOW_NO_INPUT | Prevents window of scaling, moving or getting focus
  1414. ///
  1415. /// #### nk_collapse_states
  1416. /// State | Description
  1417. /// ----------------|-----------------------------------------------------------
  1418. /// __NK_MINIMIZED__| UI section is collased and not visibile until maximized
  1419. /// __NK_MAXIMIZED__| UI section is extended and visibile until minimized
  1420. /// <br /><br />
  1421. */
  1422. enum nk_panel_flags {
  1423. NK_WINDOW_BORDER = NK_FLAG(0),
  1424. NK_WINDOW_MOVABLE = NK_FLAG(1),
  1425. NK_WINDOW_SCALABLE = NK_FLAG(2),
  1426. NK_WINDOW_CLOSABLE = NK_FLAG(3),
  1427. NK_WINDOW_MINIMIZABLE = NK_FLAG(4),
  1428. NK_WINDOW_NO_SCROLLBAR = NK_FLAG(5),
  1429. NK_WINDOW_TITLE = NK_FLAG(6),
  1430. NK_WINDOW_SCROLL_AUTO_HIDE = NK_FLAG(7),
  1431. NK_WINDOW_BACKGROUND = NK_FLAG(8),
  1432. NK_WINDOW_SCALE_LEFT = NK_FLAG(9),
  1433. NK_WINDOW_NO_INPUT = NK_FLAG(10)
  1434. };
  1435. /*/// #### nk_begin
  1436. /// Starts a new window; needs to be called every frame for every
  1437. /// window (unless hidden) or otherwise the window gets removed
  1438. ///
  1439. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1440. /// int nk_begin(struct nk_context *ctx, const char *title, struct nk_rect bounds, nk_flags flags);
  1441. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1442. ///
  1443. /// Parameter | Description
  1444. /// ------------|-----------------------------------------------------------
  1445. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1446. /// __title__ | Window title and identifier. Needs to be persistent over frames to identify the window
  1447. /// __bounds__ | Initial position and window size. However if you do not define `NK_WINDOW_SCALABLE` or `NK_WINDOW_MOVABLE` you can set window position and size every frame
  1448. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different window behaviors
  1449. ///
  1450. /// Returns `true(1)` if the window can be filled up with widgets from this point
  1451. /// until `nk_end` or `false(0)` otherwise for example if minimized
  1452. */
  1453. NK_API int nk_begin(struct nk_context *ctx, const char *title, struct nk_rect bounds, nk_flags flags);
  1454. /*/// #### nk_begin_titled
  1455. /// Extended window start with separated title and identifier to allow multiple
  1456. /// windows with same title but not name
  1457. ///
  1458. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1459. /// int nk_begin_titled(struct nk_context *ctx, const char *name, const char *title, struct nk_rect bounds, nk_flags flags);
  1460. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1461. ///
  1462. /// Parameter | Description
  1463. /// ------------|-----------------------------------------------------------
  1464. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1465. /// __name__ | Window identifier. Needs to be persistent over frames to identify the window
  1466. /// __title__ | Window title displayed inside header if flag `NK_WINDOW_TITLE` or either `NK_WINDOW_CLOSABLE` or `NK_WINDOW_MINIMIZED` was set
  1467. /// __bounds__ | Initial position and window size. However if you do not define `NK_WINDOW_SCALABLE` or `NK_WINDOW_MOVABLE` you can set window position and size every frame
  1468. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different window behaviors
  1469. ///
  1470. /// Returns `true(1)` if the window can be filled up with widgets from this point
  1471. /// until `nk_end` or `false(0)` otherwise for example if minimized
  1472. */
  1473. NK_API int nk_begin_titled(struct nk_context *ctx, const char *name, const char *title, struct nk_rect bounds, nk_flags flags);
  1474. /*/// #### nk_end
  1475. /// Needs to be called at the end of the window building process to process scaling, scrollbars and general cleanup.
  1476. /// All widget calls after this functions will result in asserts or no state changes
  1477. ///
  1478. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1479. /// void nk_end(struct nk_context *ctx);
  1480. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1481. ///
  1482. /// Parameter | Description
  1483. /// ------------|-----------------------------------------------------------
  1484. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1485. */
  1486. NK_API void nk_end(struct nk_context *ctx);
  1487. /*/// #### nk_window_find
  1488. /// Finds and returns a window from passed name
  1489. ///
  1490. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1491. /// void nk_end(struct nk_context *ctx);
  1492. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1493. ///
  1494. /// Parameter | Description
  1495. /// ------------|-----------------------------------------------------------
  1496. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1497. /// __name__ | Window identifier
  1498. ///
  1499. /// Returns a `nk_window` struct pointing to the identified window or NULL if
  1500. /// no window with the given name was found
  1501. */
  1502. NK_API struct nk_window *nk_window_find(struct nk_context *ctx, const char *name);
  1503. /*/// #### nk_window_get_bounds
  1504. /// Returns a rectangle with screen position and size of the currently processed window
  1505. ///
  1506. /// !!! WARNING
  1507. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1508. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1509. /// struct nk_rect nk_window_get_bounds(const struct nk_context *ctx);
  1510. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1511. ///
  1512. /// Parameter | Description
  1513. /// ------------|-----------------------------------------------------------
  1514. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1515. ///
  1516. /// Returns a `nk_rect` struct with window upper left window position and size
  1517. */
  1518. NK_API struct nk_rect nk_window_get_bounds(const struct nk_context *ctx);
  1519. /*/// #### nk_window_get_position
  1520. /// Returns the position of the currently processed window.
  1521. ///
  1522. /// !!! WARNING
  1523. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1524. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1525. /// struct nk_vec2 nk_window_get_position(const struct nk_context *ctx);
  1526. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1527. ///
  1528. /// Parameter | Description
  1529. /// ------------|-----------------------------------------------------------
  1530. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1531. ///
  1532. /// Returns a `nk_vec2` struct with window upper left position
  1533. */
  1534. NK_API struct nk_vec2 nk_window_get_position(const struct nk_context *ctx);
  1535. /*/// #### nk_window_get_size
  1536. /// Returns the size with width and height of the currently processed window.
  1537. ///
  1538. /// !!! WARNING
  1539. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1540. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1541. /// struct nk_vec2 nk_window_get_size(const struct nk_context *ctx);
  1542. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1543. ///
  1544. /// Parameter | Description
  1545. /// ------------|-----------------------------------------------------------
  1546. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1547. ///
  1548. /// Returns a `nk_vec2` struct with window width and height
  1549. */
  1550. NK_API struct nk_vec2 nk_window_get_size(const struct nk_context*);
  1551. /*/// #### nk_window_get_width
  1552. /// Returns the width of the currently processed window.
  1553. ///
  1554. /// !!! WARNING
  1555. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1556. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1557. /// float nk_window_get_width(const struct nk_context *ctx);
  1558. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1559. ///
  1560. /// Parameter | Description
  1561. /// ------------|-----------------------------------------------------------
  1562. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1563. ///
  1564. /// Returns the current window width
  1565. */
  1566. NK_API float nk_window_get_width(const struct nk_context*);
  1567. /*/// #### nk_window_get_height
  1568. /// Returns the height of the currently processed window.
  1569. ///
  1570. /// !!! WARNING
  1571. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1572. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1573. /// float nk_window_get_height(const struct nk_context *ctx);
  1574. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1575. ///
  1576. /// Parameter | Description
  1577. /// ------------|-----------------------------------------------------------
  1578. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1579. ///
  1580. /// Returns the current window height
  1581. */
  1582. NK_API float nk_window_get_height(const struct nk_context*);
  1583. /*/// #### nk_window_get_panel
  1584. /// Returns the underlying panel which contains all processing state of the current window.
  1585. ///
  1586. /// !!! WARNING
  1587. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1588. /// !!! WARNING
  1589. /// Do not keep the returned panel pointer around, it is only valid until `nk_end`
  1590. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1591. /// struct nk_panel* nk_window_get_panel(struct nk_context *ctx);
  1592. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1593. ///
  1594. /// Parameter | Description
  1595. /// ------------|-----------------------------------------------------------
  1596. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1597. ///
  1598. /// Returns a pointer to window internal `nk_panel` state.
  1599. */
  1600. NK_API struct nk_panel* nk_window_get_panel(struct nk_context*);
  1601. /*/// #### nk_window_get_content_region
  1602. /// Returns the position and size of the currently visible and non-clipped space
  1603. /// inside the currently processed window.
  1604. ///
  1605. /// !!! WARNING
  1606. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1607. ///
  1608. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1609. /// struct nk_rect nk_window_get_content_region(struct nk_context *ctx);
  1610. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1611. ///
  1612. /// Parameter | Description
  1613. /// ------------|-----------------------------------------------------------
  1614. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1615. ///
  1616. /// Returns `nk_rect` struct with screen position and size (no scrollbar offset)
  1617. /// of the visible space inside the current window
  1618. */
  1619. NK_API struct nk_rect nk_window_get_content_region(struct nk_context*);
  1620. /*/// #### nk_window_get_content_region_min
  1621. /// Returns the upper left position of the currently visible and non-clipped
  1622. /// space inside the currently processed window.
  1623. ///
  1624. /// !!! WARNING
  1625. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1626. ///
  1627. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1628. /// struct nk_vec2 nk_window_get_content_region_min(struct nk_context *ctx);
  1629. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1630. ///
  1631. /// Parameter | Description
  1632. /// ------------|-----------------------------------------------------------
  1633. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1634. ///
  1635. /// returns `nk_vec2` struct with upper left screen position (no scrollbar offset)
  1636. /// of the visible space inside the current window
  1637. */
  1638. NK_API struct nk_vec2 nk_window_get_content_region_min(struct nk_context*);
  1639. /*/// #### nk_window_get_content_region_max
  1640. /// Returns the lower right screen position of the currently visible and
  1641. /// non-clipped space inside the currently processed window.
  1642. ///
  1643. /// !!! WARNING
  1644. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1645. ///
  1646. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1647. /// struct nk_vec2 nk_window_get_content_region_max(struct nk_context *ctx);
  1648. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1649. ///
  1650. /// Parameter | Description
  1651. /// ------------|-----------------------------------------------------------
  1652. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1653. ///
  1654. /// Returns `nk_vec2` struct with lower right screen position (no scrollbar offset)
  1655. /// of the visible space inside the current window
  1656. */
  1657. NK_API struct nk_vec2 nk_window_get_content_region_max(struct nk_context*);
  1658. /*/// #### nk_window_get_content_region_size
  1659. /// Returns the size of the currently visible and non-clipped space inside the
  1660. /// currently processed window
  1661. ///
  1662. /// !!! WARNING
  1663. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1664. ///
  1665. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1666. /// struct nk_vec2 nk_window_get_content_region_size(struct nk_context *ctx);
  1667. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1668. ///
  1669. /// Parameter | Description
  1670. /// ------------|-----------------------------------------------------------
  1671. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1672. ///
  1673. /// Returns `nk_vec2` struct with size the visible space inside the current window
  1674. */
  1675. NK_API struct nk_vec2 nk_window_get_content_region_size(struct nk_context*);
  1676. /*/// #### nk_window_get_canvas
  1677. /// Returns the draw command buffer. Can be used to draw custom widgets
  1678. /// !!! WARNING
  1679. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1680. /// !!! WARNING
  1681. /// Do not keep the returned command buffer pointer around it is only valid until `nk_end`
  1682. ///
  1683. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1684. /// struct nk_command_buffer* nk_window_get_canvas(struct nk_context *ctx);
  1685. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1686. ///
  1687. /// Parameter | Description
  1688. /// ------------|-----------------------------------------------------------
  1689. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1690. ///
  1691. /// Returns a pointer to window internal `nk_command_buffer` struct used as
  1692. /// drawing canvas. Can be used to do custom drawing.
  1693. */
  1694. NK_API struct nk_command_buffer* nk_window_get_canvas(struct nk_context*);
  1695. /*/// #### nk_window_has_focus
  1696. /// Returns if the currently processed window is currently active
  1697. /// !!! WARNING
  1698. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1699. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1700. /// int nk_window_has_focus(const struct nk_context *ctx);
  1701. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1702. ///
  1703. /// Parameter | Description
  1704. /// ------------|-----------------------------------------------------------
  1705. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1706. ///
  1707. /// Returns `false(0)` if current window is not active or `true(1)` if it is
  1708. */
  1709. NK_API int nk_window_has_focus(const struct nk_context*);
  1710. /*/// #### nk_window_is_hovered
  1711. /// Return if the current window is being hovered
  1712. /// !!! WARNING
  1713. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1714. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1715. /// int nk_window_is_hovered(struct nk_context *ctx);
  1716. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1717. ///
  1718. /// Parameter | Description
  1719. /// ------------|-----------------------------------------------------------
  1720. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1721. ///
  1722. /// Returns `true(1)` if current window is hovered or `false(0)` otherwise
  1723. */
  1724. NK_API int nk_window_is_hovered(struct nk_context*);
  1725. /*/// #### nk_window_is_collapsed
  1726. /// Returns if the window with given name is currently minimized/collapsed
  1727. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1728. /// int nk_window_is_collapsed(struct nk_context *ctx, const char *name);
  1729. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1730. ///
  1731. /// Parameter | Description
  1732. /// ------------|-----------------------------------------------------------
  1733. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1734. /// __name__ | Identifier of window you want to check if it is collapsed
  1735. ///
  1736. /// Returns `true(1)` if current window is minimized and `false(0)` if window not
  1737. /// found or is not minimized
  1738. */
  1739. NK_API int nk_window_is_collapsed(struct nk_context *ctx, const char *name);
  1740. /*/// #### nk_window_is_closed
  1741. /// Returns if the window with given name was closed by calling `nk_close`
  1742. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1743. /// int nk_window_is_closed(struct nk_context *ctx, const char *name);
  1744. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1745. ///
  1746. /// Parameter | Description
  1747. /// ------------|-----------------------------------------------------------
  1748. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1749. /// __name__ | Identifier of window you want to check if it is closed
  1750. ///
  1751. /// Returns `true(1)` if current window was closed or `false(0)` window not found or not closed
  1752. */
  1753. NK_API int nk_window_is_closed(struct nk_context*, const char*);
  1754. /*/// #### nk_window_is_hidden
  1755. /// Returns if the window with given name is hidden
  1756. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1757. /// int nk_window_is_hidden(struct nk_context *ctx, const char *name);
  1758. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1759. ///
  1760. /// Parameter | Description
  1761. /// ------------|-----------------------------------------------------------
  1762. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1763. /// __name__ | Identifier of window you want to check if it is hidden
  1764. ///
  1765. /// Returns `true(1)` if current window is hidden or `false(0)` window not found or visible
  1766. */
  1767. NK_API int nk_window_is_hidden(struct nk_context*, const char*);
  1768. /*/// #### nk_window_is_active
  1769. /// Same as nk_window_has_focus for some reason
  1770. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1771. /// int nk_window_is_active(struct nk_context *ctx, const char *name);
  1772. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1773. ///
  1774. /// Parameter | Description
  1775. /// ------------|-----------------------------------------------------------
  1776. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1777. /// __name__ | Identifier of window you want to check if it is active
  1778. ///
  1779. /// Returns `true(1)` if current window is active or `false(0)` window not found or not active
  1780. */
  1781. NK_API int nk_window_is_active(struct nk_context*, const char*);
  1782. /*/// #### nk_window_is_any_hovered
  1783. /// Returns if the any window is being hovered
  1784. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1785. /// int nk_window_is_any_hovered(struct nk_context*);
  1786. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1787. ///
  1788. /// Parameter | Description
  1789. /// ------------|-----------------------------------------------------------
  1790. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1791. ///
  1792. /// Returns `true(1)` if any window is hovered or `false(0)` otherwise
  1793. */
  1794. NK_API int nk_window_is_any_hovered(struct nk_context*);
  1795. /*/// #### nk_item_is_any_active
  1796. /// Returns if the any window is being hovered or any widget is currently active.
  1797. /// Can be used to decide if input should be processed by UI or your specific input handling.
  1798. /// Example could be UI and 3D camera to move inside a 3D space.
  1799. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1800. /// int nk_item_is_any_active(struct nk_context*);
  1801. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1802. ///
  1803. /// Parameter | Description
  1804. /// ------------|-----------------------------------------------------------
  1805. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1806. ///
  1807. /// Returns `true(1)` if any window is hovered or any item is active or `false(0)` otherwise
  1808. */
  1809. NK_API int nk_item_is_any_active(struct nk_context*);
  1810. /*/// #### nk_window_set_bounds
  1811. /// Updates position and size of window with passed in name
  1812. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1813. /// void nk_window_set_bounds(struct nk_context*, const char *name, struct nk_rect bounds);
  1814. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1815. ///
  1816. /// Parameter | Description
  1817. /// ------------|-----------------------------------------------------------
  1818. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1819. /// __name__ | Identifier of the window to modify both position and size
  1820. /// __bounds__ | Must point to a `nk_rect` struct with the new position and size
  1821. */
  1822. NK_API void nk_window_set_bounds(struct nk_context*, const char *name, struct nk_rect bounds);
  1823. /*/// #### nk_window_set_position
  1824. /// Updates position of window with passed name
  1825. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1826. /// void nk_window_set_position(struct nk_context*, const char *name, struct nk_vec2 pos);
  1827. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1828. ///
  1829. /// Parameter | Description
  1830. /// ------------|-----------------------------------------------------------
  1831. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1832. /// __name__ | Identifier of the window to modify both position
  1833. /// __pos__ | Must point to a `nk_vec2` struct with the new position
  1834. */
  1835. NK_API void nk_window_set_position(struct nk_context*, const char *name, struct nk_vec2 pos);
  1836. /*/// #### nk_window_set_size
  1837. /// Updates size of window with passed in name
  1838. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1839. /// void nk_window_set_size(struct nk_context*, const char *name, struct nk_vec2);
  1840. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1841. ///
  1842. /// Parameter | Description
  1843. /// ------------|-----------------------------------------------------------
  1844. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1845. /// __name__ | Identifier of the window to modify both window size
  1846. /// __size__ | Must point to a `nk_vec2` struct with new window size
  1847. */
  1848. NK_API void nk_window_set_size(struct nk_context*, const char *name, struct nk_vec2);
  1849. /*/// #### nk_window_set_focus
  1850. /// Sets the window with given name as active
  1851. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1852. /// void nk_window_set_focus(struct nk_context*, const char *name);
  1853. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1854. ///
  1855. /// Parameter | Description
  1856. /// ------------|-----------------------------------------------------------
  1857. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1858. /// __name__ | Identifier of the window to set focus on
  1859. */
  1860. NK_API void nk_window_set_focus(struct nk_context*, const char *name);
  1861. /*/// #### nk_window_close
  1862. /// Closes a window and marks it for being freed at the end of the frame
  1863. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1864. /// void nk_window_close(struct nk_context *ctx, const char *name);
  1865. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1866. ///
  1867. /// Parameter | Description
  1868. /// ------------|-----------------------------------------------------------
  1869. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1870. /// __name__ | Identifier of the window to close
  1871. */
  1872. NK_API void nk_window_close(struct nk_context *ctx, const char *name);
  1873. /*/// #### nk_window_collapse
  1874. /// Updates collapse state of a window with given name
  1875. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1876. /// void nk_window_collapse(struct nk_context*, const char *name, enum nk_collapse_states state);
  1877. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1878. ///
  1879. /// Parameter | Description
  1880. /// ------------|-----------------------------------------------------------
  1881. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1882. /// __name__ | Identifier of the window to close
  1883. /// __state__ | value out of nk_collapse_states section
  1884. */
  1885. NK_API void nk_window_collapse(struct nk_context*, const char *name, enum nk_collapse_states state);
  1886. /*/// #### nk_window_collapse_if
  1887. /// Updates collapse state of a window with given name if given condition is met
  1888. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1889. /// void nk_window_collapse_if(struct nk_context*, const char *name, enum nk_collapse_states, int cond);
  1890. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1891. ///
  1892. /// Parameter | Description
  1893. /// ------------|-----------------------------------------------------------
  1894. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1895. /// __name__ | Identifier of the window to either collapse or maximize
  1896. /// __state__ | value out of nk_collapse_states section the window should be put into
  1897. /// __cond__ | condition that has to be met to actually commit the collapse state change
  1898. */
  1899. NK_API void nk_window_collapse_if(struct nk_context*, const char *name, enum nk_collapse_states, int cond);
  1900. /*/// #### nk_window_show
  1901. /// updates visibility state of a window with given name
  1902. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1903. /// void nk_window_show(struct nk_context*, const char *name, enum nk_show_states);
  1904. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1905. ///
  1906. /// Parameter | Description
  1907. /// ------------|-----------------------------------------------------------
  1908. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1909. /// __name__ | Identifier of the window to either collapse or maximize
  1910. /// __state__ | state with either visible or hidden to modify the window with
  1911. */
  1912. NK_API void nk_window_show(struct nk_context*, const char *name, enum nk_show_states);
  1913. /*/// #### nk_window_show_if
  1914. /// Updates visibility state of a window with given name if a given condition is met
  1915. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1916. /// void nk_window_show_if(struct nk_context*, const char *name, enum nk_show_states, int cond);
  1917. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1918. ///
  1919. /// Parameter | Description
  1920. /// ------------|-----------------------------------------------------------
  1921. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1922. /// __name__ | Identifier of the window to either hide or show
  1923. /// __state__ | state with either visible or hidden to modify the window with
  1924. /// __cond__ | condition that has to be met to actually commit the visbility state change
  1925. */
  1926. NK_API void nk_window_show_if(struct nk_context*, const char *name, enum nk_show_states, int cond);
  1927. /* =============================================================================
  1928. *
  1929. * LAYOUT
  1930. *
  1931. * =============================================================================
  1932. /// ### Layouting
  1933. /// Layouting in general describes placing widget inside a window with position and size.
  1934. /// While in this particular implementation there are five different APIs for layouting
  1935. /// each with different trade offs between control and ease of use. <br /><br />
  1936. ///
  1937. /// All layouting methods in this library are based around the concept of a row.
  1938. /// A row has a height the window content grows by and a number of columns and each
  1939. /// layouting method specifies how each widget is placed inside the row.
  1940. /// After a row has been allocated by calling a layouting functions and then
  1941. /// filled with widgets will advance an internal pointer over the allocated row. <br /><br />
  1942. ///
  1943. /// To actually define a layout you just call the appropriate layouting function
  1944. /// and each subsequent widget call will place the widget as specified. Important
  1945. /// here is that if you define more widgets then columns defined inside the layout
  1946. /// functions it will allocate the next row without you having to make another layouting <br /><br />
  1947. /// call.
  1948. ///
  1949. /// Biggest limitation with using all these APIs outside the `nk_layout_space_xxx` API
  1950. /// is that you have to define the row height for each. However the row height
  1951. /// often depends on the height of the font. <br /><br />
  1952. ///
  1953. /// To fix that internally nuklear uses a minimum row height that is set to the
  1954. /// height plus padding of currently active font and overwrites the row height
  1955. /// value if zero. <br /><br />
  1956. ///
  1957. /// If you manually want to change the minimum row height then
  1958. /// use nk_layout_set_min_row_height, and use nk_layout_reset_min_row_height to
  1959. /// reset it back to be derived from font height. <br /><br />
  1960. ///
  1961. /// Also if you change the font in nuklear it will automatically change the minimum
  1962. /// row height for you and. This means if you change the font but still want
  1963. /// a minimum row height smaller than the font you have to repush your value. <br /><br />
  1964. ///
  1965. /// For actually more advanced UI I would even recommend using the `nk_layout_space_xxx`
  1966. /// layouting method in combination with a cassowary constraint solver (there are
  1967. /// some versions on github with permissive license model) to take over all control over widget
  1968. /// layouting yourself. However for quick and dirty layouting using all the other layouting
  1969. /// functions should be fine.
  1970. ///
  1971. /// #### Usage
  1972. /// 1. __nk_layout_row_dynamic__<br /><br />
  1973. /// The easiest layouting function is `nk_layout_row_dynamic`. It provides each
  1974. /// widgets with same horizontal space inside the row and dynamically grows
  1975. /// if the owning window grows in width. So the number of columns dictates
  1976. /// the size of each widget dynamically by formula:
  1977. ///
  1978. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1979. /// widget_width = (window_width - padding - spacing) * (1/colum_count)
  1980. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1981. ///
  1982. /// Just like all other layouting APIs if you define more widget than columns this
  1983. /// library will allocate a new row and keep all layouting parameters previously
  1984. /// defined.
  1985. ///
  1986. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1987. /// if (nk_begin_xxx(...) {
  1988. /// // first row with height: 30 composed of two widgets
  1989. /// nk_layout_row_dynamic(&ctx, 30, 2);
  1990. /// nk_widget(...);
  1991. /// nk_widget(...);
  1992. /// //
  1993. /// // second row with same parameter as defined above
  1994. /// nk_widget(...);
  1995. /// nk_widget(...);
  1996. /// //
  1997. /// // third row uses 0 for height which will use auto layouting
  1998. /// nk_layout_row_dynamic(&ctx, 0, 2);
  1999. /// nk_widget(...);
  2000. /// nk_widget(...);
  2001. /// }
  2002. /// nk_end(...);
  2003. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2004. ///
  2005. /// 2. __nk_layout_row_static__<br /><br />
  2006. /// Another easy layouting function is `nk_layout_row_static`. It provides each
  2007. /// widget with same horizontal pixel width inside the row and does not grow
  2008. /// if the owning window scales smaller or bigger.
  2009. ///
  2010. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2011. /// if (nk_begin_xxx(...) {
  2012. /// // first row with height: 30 composed of two widgets with width: 80
  2013. /// nk_layout_row_static(&ctx, 30, 80, 2);
  2014. /// nk_widget(...);
  2015. /// nk_widget(...);
  2016. /// //
  2017. /// // second row with same parameter as defined above
  2018. /// nk_widget(...);
  2019. /// nk_widget(...);
  2020. /// //
  2021. /// // third row uses 0 for height which will use auto layouting
  2022. /// nk_layout_row_static(&ctx, 0, 80, 2);
  2023. /// nk_widget(...);
  2024. /// nk_widget(...);
  2025. /// }
  2026. /// nk_end(...);
  2027. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2028. ///
  2029. /// 3. __nk_layout_row_xxx__<br /><br />
  2030. /// A little bit more advanced layouting API are functions `nk_layout_row_begin`,
  2031. /// `nk_layout_row_push` and `nk_layout_row_end`. They allow to directly
  2032. /// specify each column pixel or window ratio in a row. It supports either
  2033. /// directly setting per column pixel width or widget window ratio but not
  2034. /// both. Furthermore it is a immediate mode API so each value is directly
  2035. /// pushed before calling a widget. Therefore the layout is not automatically
  2036. /// repeating like the last two layouting functions.
  2037. ///
  2038. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2039. /// if (nk_begin_xxx(...) {
  2040. /// // first row with height: 25 composed of two widgets with width 60 and 40
  2041. /// nk_layout_row_begin(ctx, NK_STATIC, 25, 2);
  2042. /// nk_layout_row_push(ctx, 60);
  2043. /// nk_widget(...);
  2044. /// nk_layout_row_push(ctx, 40);
  2045. /// nk_widget(...);
  2046. /// nk_layout_row_end(ctx);
  2047. /// //
  2048. /// // second row with height: 25 composed of two widgets with window ratio 0.25 and 0.75
  2049. /// nk_layout_row_begin(ctx, NK_DYNAMIC, 25, 2);
  2050. /// nk_layout_row_push(ctx, 0.25f);
  2051. /// nk_widget(...);
  2052. /// nk_layout_row_push(ctx, 0.75f);
  2053. /// nk_widget(...);
  2054. /// nk_layout_row_end(ctx);
  2055. /// //
  2056. /// // third row with auto generated height: composed of two widgets with window ratio 0.25 and 0.75
  2057. /// nk_layout_row_begin(ctx, NK_DYNAMIC, 0, 2);
  2058. /// nk_layout_row_push(ctx, 0.25f);
  2059. /// nk_widget(...);
  2060. /// nk_layout_row_push(ctx, 0.75f);
  2061. /// nk_widget(...);
  2062. /// nk_layout_row_end(ctx);
  2063. /// }
  2064. /// nk_end(...);
  2065. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2066. ///
  2067. /// 4. __nk_layout_row__<br /><br />
  2068. /// The array counterpart to API nk_layout_row_xxx is the single nk_layout_row
  2069. /// functions. Instead of pushing either pixel or window ratio for every widget
  2070. /// it allows to define it by array. The trade of for less control is that
  2071. /// `nk_layout_row` is automatically repeating. Otherwise the behavior is the
  2072. /// same.
  2073. ///
  2074. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2075. /// if (nk_begin_xxx(...) {
  2076. /// // two rows with height: 30 composed of two widgets with width 60 and 40
  2077. /// const float size[] = {60,40};
  2078. /// nk_layout_row(ctx, NK_STATIC, 30, 2, ratio);
  2079. /// nk_widget(...);
  2080. /// nk_widget(...);
  2081. /// nk_widget(...);
  2082. /// nk_widget(...);
  2083. /// //
  2084. /// // two rows with height: 30 composed of two widgets with window ratio 0.25 and 0.75
  2085. /// const float ratio[] = {0.25, 0.75};
  2086. /// nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio);
  2087. /// nk_widget(...);
  2088. /// nk_widget(...);
  2089. /// nk_widget(...);
  2090. /// nk_widget(...);
  2091. /// //
  2092. /// // two rows with auto generated height composed of two widgets with window ratio 0.25 and 0.75
  2093. /// const float ratio[] = {0.25, 0.75};
  2094. /// nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio);
  2095. /// nk_widget(...);
  2096. /// nk_widget(...);
  2097. /// nk_widget(...);
  2098. /// nk_widget(...);
  2099. /// }
  2100. /// nk_end(...);
  2101. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2102. ///
  2103. /// 5. __nk_layout_row_template_xxx__<br /><br />
  2104. /// The most complex and second most flexible API is a simplified flexbox version without
  2105. /// line wrapping and weights for dynamic widgets. It is an immediate mode API but
  2106. /// unlike `nk_layout_row_xxx` it has auto repeat behavior and needs to be called
  2107. /// before calling the templated widgets.
  2108. /// The row template layout has three different per widget size specifier. The first
  2109. /// one is the `nk_layout_row_template_push_static` with fixed widget pixel width.
  2110. /// They do not grow if the row grows and will always stay the same.
  2111. /// The second size specifier is `nk_layout_row_template_push_variable`
  2112. /// which defines a minimum widget size but it also can grow if more space is available
  2113. /// not taken by other widgets.
  2114. /// Finally there are dynamic widgets with `nk_layout_row_template_push_dynamic`
  2115. /// which are completely flexible and unlike variable widgets can even shrink
  2116. /// to zero if not enough space is provided.
  2117. ///
  2118. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2119. /// if (nk_begin_xxx(...) {
  2120. /// // two rows with height: 30 composed of three widgets
  2121. /// nk_layout_row_template_begin(ctx, 30);
  2122. /// nk_layout_row_template_push_dynamic(ctx);
  2123. /// nk_layout_row_template_push_variable(ctx, 80);
  2124. /// nk_layout_row_template_push_static(ctx, 80);
  2125. /// nk_layout_row_template_end(ctx);
  2126. /// //
  2127. /// // first row
  2128. /// nk_widget(...); // dynamic widget can go to zero if not enough space
  2129. /// nk_widget(...); // variable widget with min 80 pixel but can grow bigger if enough space
  2130. /// nk_widget(...); // static widget with fixed 80 pixel width
  2131. /// //
  2132. /// // second row same layout
  2133. /// nk_widget(...);
  2134. /// nk_widget(...);
  2135. /// nk_widget(...);
  2136. /// }
  2137. /// nk_end(...);
  2138. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2139. ///
  2140. /// 6. __nk_layout_space_xxx__<br /><br />
  2141. /// Finally the most flexible API directly allows you to place widgets inside the
  2142. /// window. The space layout API is an immediate mode API which does not support
  2143. /// row auto repeat and directly sets position and size of a widget. Position
  2144. /// and size hereby can be either specified as ratio of allocated space or
  2145. /// allocated space local position and pixel size. Since this API is quite
  2146. /// powerful there are a number of utility functions to get the available space
  2147. /// and convert between local allocated space and screen space.
  2148. ///
  2149. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2150. /// if (nk_begin_xxx(...) {
  2151. /// // static row with height: 500 (you can set column count to INT_MAX if you don't want to be bothered)
  2152. /// nk_layout_space_begin(ctx, NK_STATIC, 500, INT_MAX);
  2153. /// nk_layout_space_push(ctx, nk_rect(0,0,150,200));
  2154. /// nk_widget(...);
  2155. /// nk_layout_space_push(ctx, nk_rect(200,200,100,200));
  2156. /// nk_widget(...);
  2157. /// nk_layout_space_end(ctx);
  2158. /// //
  2159. /// // dynamic row with height: 500 (you can set column count to INT_MAX if you don't want to be bothered)
  2160. /// nk_layout_space_begin(ctx, NK_DYNAMIC, 500, INT_MAX);
  2161. /// nk_layout_space_push(ctx, nk_rect(0.5,0.5,0.1,0.1));
  2162. /// nk_widget(...);
  2163. /// nk_layout_space_push(ctx, nk_rect(0.7,0.6,0.1,0.1));
  2164. /// nk_widget(...);
  2165. /// }
  2166. /// nk_end(...);
  2167. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2168. ///
  2169. /// #### Reference
  2170. /// Function | Description
  2171. /// ----------------------------------------|------------------------------------
  2172. /// nk_layout_set_min_row_height | Set the currently used minimum row height to a specified value
  2173. /// nk_layout_reset_min_row_height | Resets the currently used minimum row height to font height
  2174. /// nk_layout_widget_bounds | Calculates current width a static layout row can fit inside a window
  2175. /// nk_layout_ratio_from_pixel | Utility functions to calculate window ratio from pixel size
  2176. //
  2177. /// nk_layout_row_dynamic | Current layout is divided into n same sized growing columns
  2178. /// nk_layout_row_static | Current layout is divided into n same fixed sized columns
  2179. /// nk_layout_row_begin | Starts a new row with given height and number of columns
  2180. /// nk_layout_row_push | Pushes another column with given size or window ratio
  2181. /// nk_layout_row_end | Finished previously started row
  2182. /// nk_layout_row | Specifies row columns in array as either window ratio or size
  2183. //
  2184. /// nk_layout_row_template_begin | Begins the row template declaration
  2185. /// nk_layout_row_template_push_dynamic | Adds a dynamic column that dynamically grows and can go to zero if not enough space
  2186. /// nk_layout_row_template_push_variable | Adds a variable column that dynamically grows but does not shrink below specified pixel width
  2187. /// nk_layout_row_template_push_static | Adds a static column that does not grow and will always have the same size
  2188. /// nk_layout_row_template_end | Marks the end of the row template
  2189. //
  2190. /// nk_layout_space_begin | Begins a new layouting space that allows to specify each widgets position and size
  2191. /// nk_layout_space_push | Pushes position and size of the next widget in own coordinate space either as pixel or ratio
  2192. /// nk_layout_space_end | Marks the end of the layouting space
  2193. //
  2194. /// nk_layout_space_bounds | Callable after nk_layout_space_begin and returns total space allocated
  2195. /// nk_layout_space_to_screen | Converts vector from nk_layout_space coordinate space into screen space
  2196. /// nk_layout_space_to_local | Converts vector from screen space into nk_layout_space coordinates
  2197. /// nk_layout_space_rect_to_screen | Converts rectangle from nk_layout_space coordinate space into screen space
  2198. /// nk_layout_space_rect_to_local | Converts rectangle from screen space into nk_layout_space coordinates
  2199. */
  2200. /*/// #### nk_layout_set_min_row_height
  2201. /// Sets the currently used minimum row height.
  2202. /// !!! WARNING
  2203. /// The passed height needs to include both your preferred row height
  2204. /// as well as padding. No internal padding is added.
  2205. ///
  2206. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2207. /// void nk_layout_set_min_row_height(struct nk_context*, float height);
  2208. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2209. ///
  2210. /// Parameter | Description
  2211. /// ------------|-----------------------------------------------------------
  2212. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2213. /// __height__ | New minimum row height to be used for auto generating the row height
  2214. */
  2215. NK_API void nk_layout_set_min_row_height(struct nk_context*, float height);
  2216. /*/// #### nk_layout_reset_min_row_height
  2217. /// Reset the currently used minimum row height back to `font_height + text_padding + padding`
  2218. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2219. /// void nk_layout_reset_min_row_height(struct nk_context*);
  2220. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2221. ///
  2222. /// Parameter | Description
  2223. /// ------------|-----------------------------------------------------------
  2224. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2225. */
  2226. NK_API void nk_layout_reset_min_row_height(struct nk_context*);
  2227. /*/// #### nk_layout_widget_bounds
  2228. /// Returns the width of the next row allocate by one of the layouting functions
  2229. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2230. /// struct nk_rect nk_layout_widget_bounds(struct nk_context*);
  2231. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2232. ///
  2233. /// Parameter | Description
  2234. /// ------------|-----------------------------------------------------------
  2235. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2236. ///
  2237. /// Return `nk_rect` with both position and size of the next row
  2238. */
  2239. NK_API struct nk_rect nk_layout_widget_bounds(struct nk_context*);
  2240. /*/// #### nk_layout_ratio_from_pixel
  2241. /// Utility functions to calculate window ratio from pixel size
  2242. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2243. /// float nk_layout_ratio_from_pixel(struct nk_context*, float pixel_width);
  2244. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2245. ///
  2246. /// Parameter | Description
  2247. /// ------------|-----------------------------------------------------------
  2248. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2249. /// __pixel__ | Pixel_width to convert to window ratio
  2250. ///
  2251. /// Returns `nk_rect` with both position and size of the next row
  2252. */
  2253. NK_API float nk_layout_ratio_from_pixel(struct nk_context*, float pixel_width);
  2254. /*/// #### nk_layout_row_dynamic
  2255. /// Sets current row layout to share horizontal space
  2256. /// between @cols number of widgets evenly. Once called all subsequent widget
  2257. /// calls greater than @cols will allocate a new row with same layout.
  2258. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2259. /// void nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols);
  2260. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2261. ///
  2262. /// Parameter | Description
  2263. /// ------------|-----------------------------------------------------------
  2264. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2265. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2266. /// __columns__ | Number of widget inside row
  2267. */
  2268. NK_API void nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols);
  2269. /*/// #### nk_layout_row_static
  2270. /// Sets current row layout to fill @cols number of widgets
  2271. /// in row with same @item_width horizontal size. Once called all subsequent widget
  2272. /// calls greater than @cols will allocate a new row with same layout.
  2273. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2274. /// void nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols);
  2275. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2276. ///
  2277. /// Parameter | Description
  2278. /// ------------|-----------------------------------------------------------
  2279. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2280. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2281. /// __width__ | Holds pixel width of each widget in the row
  2282. /// __columns__ | Number of widget inside row
  2283. */
  2284. NK_API void nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols);
  2285. /*/// #### nk_layout_row_begin
  2286. /// Starts a new dynamic or fixed row with given height and columns.
  2287. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2288. /// void nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt, float row_height, int cols);
  2289. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2290. ///
  2291. /// Parameter | Description
  2292. /// ------------|-----------------------------------------------------------
  2293. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2294. /// __fmt__ | either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  2295. /// __height__ | holds height of each widget in row or zero for auto layouting
  2296. /// __columns__ | Number of widget inside row
  2297. */
  2298. NK_API void nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt, float row_height, int cols);
  2299. /*/// #### nk_layout_row_push
  2300. /// Specifies either window ratio or width of a single column
  2301. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2302. /// void nk_layout_row_push(struct nk_context*, float value);
  2303. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2304. ///
  2305. /// Parameter | Description
  2306. /// ------------|-----------------------------------------------------------
  2307. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2308. /// __value__ | either a window ratio or fixed width depending on @fmt in previous `nk_layout_row_begin` call
  2309. */
  2310. NK_API void nk_layout_row_push(struct nk_context*, float value);
  2311. /*/// #### nk_layout_row_end
  2312. /// Finished previously started row
  2313. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2314. /// void nk_layout_row_end(struct nk_context*);
  2315. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2316. ///
  2317. /// Parameter | Description
  2318. /// ------------|-----------------------------------------------------------
  2319. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2320. */
  2321. NK_API void nk_layout_row_end(struct nk_context*);
  2322. /*/// #### nk_layout_row
  2323. /// Specifies row columns in array as either window ratio or size
  2324. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2325. /// void nk_layout_row(struct nk_context*, enum nk_layout_format, float height, int cols, const float *ratio);
  2326. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2327. ///
  2328. /// Parameter | Description
  2329. /// ------------|-----------------------------------------------------------
  2330. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2331. /// __fmt__ | Either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  2332. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2333. /// __columns__ | Number of widget inside row
  2334. */
  2335. NK_API void nk_layout_row(struct nk_context*, enum nk_layout_format, float height, int cols, const float *ratio);
  2336. /*/// #### nk_layout_row_template_begin
  2337. /// Begins the row template declaration
  2338. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2339. /// void nk_layout_row_template_begin(struct nk_context*, float row_height);
  2340. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2341. ///
  2342. /// Parameter | Description
  2343. /// ------------|-----------------------------------------------------------
  2344. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2345. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2346. */
  2347. NK_API void nk_layout_row_template_begin(struct nk_context*, float row_height);
  2348. /*/// #### nk_layout_row_template_push_dynamic
  2349. /// Adds a dynamic column that dynamically grows and can go to zero if not enough space
  2350. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2351. /// void nk_layout_row_template_push_dynamic(struct nk_context*);
  2352. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2353. ///
  2354. /// Parameter | Description
  2355. /// ------------|-----------------------------------------------------------
  2356. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2357. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2358. */
  2359. NK_API void nk_layout_row_template_push_dynamic(struct nk_context*);
  2360. /*/// #### nk_layout_row_template_push_variable
  2361. /// Adds a variable column that dynamically grows but does not shrink below specified pixel width
  2362. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2363. /// void nk_layout_row_template_push_variable(struct nk_context*, float min_width);
  2364. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2365. ///
  2366. /// Parameter | Description
  2367. /// ------------|-----------------------------------------------------------
  2368. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2369. /// __width__ | Holds the minimum pixel width the next column must always be
  2370. */
  2371. NK_API void nk_layout_row_template_push_variable(struct nk_context*, float min_width);
  2372. /*/// #### nk_layout_row_template_push_static
  2373. /// Adds a static column that does not grow and will always have the same size
  2374. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2375. /// void nk_layout_row_template_push_static(struct nk_context*, float width);
  2376. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2377. ///
  2378. /// Parameter | Description
  2379. /// ------------|-----------------------------------------------------------
  2380. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2381. /// __width__ | Holds the absolute pixel width value the next column must be
  2382. */
  2383. NK_API void nk_layout_row_template_push_static(struct nk_context*, float width);
  2384. /*/// #### nk_layout_row_template_end
  2385. /// Marks the end of the row template
  2386. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2387. /// void nk_layout_row_template_end(struct nk_context*);
  2388. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2389. ///
  2390. /// Parameter | Description
  2391. /// ------------|-----------------------------------------------------------
  2392. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2393. */
  2394. NK_API void nk_layout_row_template_end(struct nk_context*);
  2395. /*/// #### nk_layout_space_begin
  2396. /// Begins a new layouting space that allows to specify each widgets position and size.
  2397. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2398. /// void nk_layout_space_begin(struct nk_context*, enum nk_layout_format, float height, int widget_count);
  2399. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2400. ///
  2401. /// Parameter | Description
  2402. /// ------------|-----------------------------------------------------------
  2403. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2404. /// __fmt__ | Either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  2405. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2406. /// __columns__ | Number of widgets inside row
  2407. */
  2408. NK_API void nk_layout_space_begin(struct nk_context*, enum nk_layout_format, float height, int widget_count);
  2409. /*/// #### nk_layout_space_push
  2410. /// Pushes position and size of the next widget in own coordinate space either as pixel or ratio
  2411. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2412. /// void nk_layout_space_push(struct nk_context *ctx, struct nk_rect bounds);
  2413. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2414. ///
  2415. /// Parameter | Description
  2416. /// ------------|-----------------------------------------------------------
  2417. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2418. /// __bounds__ | Position and size in laoyut space local coordinates
  2419. */
  2420. NK_API void nk_layout_space_push(struct nk_context*, struct nk_rect bounds);
  2421. /*/// #### nk_layout_space_end
  2422. /// Marks the end of the layout space
  2423. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2424. /// void nk_layout_space_end(struct nk_context*);
  2425. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2426. ///
  2427. /// Parameter | Description
  2428. /// ------------|-----------------------------------------------------------
  2429. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2430. */
  2431. NK_API void nk_layout_space_end(struct nk_context*);
  2432. /*/// #### nk_layout_space_bounds
  2433. /// Utility function to calculate total space allocated for `nk_layout_space`
  2434. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2435. /// struct nk_rect nk_layout_space_bounds(struct nk_context*);
  2436. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2437. ///
  2438. /// Parameter | Description
  2439. /// ------------|-----------------------------------------------------------
  2440. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2441. ///
  2442. /// Returns `nk_rect` holding the total space allocated
  2443. */
  2444. NK_API struct nk_rect nk_layout_space_bounds(struct nk_context*);
  2445. /*/// #### nk_layout_space_to_screen
  2446. /// Converts vector from nk_layout_space coordinate space into screen space
  2447. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2448. /// struct nk_vec2 nk_layout_space_to_screen(struct nk_context*, struct nk_vec2);
  2449. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2450. ///
  2451. /// Parameter | Description
  2452. /// ------------|-----------------------------------------------------------
  2453. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2454. /// __vec__ | Position to convert from layout space into screen coordinate space
  2455. ///
  2456. /// Returns transformed `nk_vec2` in screen space coordinates
  2457. */
  2458. NK_API struct nk_vec2 nk_layout_space_to_screen(struct nk_context*, struct nk_vec2);
  2459. /*/// #### nk_layout_space_to_local
  2460. /// Converts vector from layout space into screen space
  2461. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2462. /// struct nk_vec2 nk_layout_space_to_local(struct nk_context*, struct nk_vec2);
  2463. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2464. ///
  2465. /// Parameter | Description
  2466. /// ------------|-----------------------------------------------------------
  2467. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2468. /// __vec__ | Position to convert from screen space into layout coordinate space
  2469. ///
  2470. /// Returns transformed `nk_vec2` in layout space coordinates
  2471. */
  2472. NK_API struct nk_vec2 nk_layout_space_to_local(struct nk_context*, struct nk_vec2);
  2473. /*/// #### nk_layout_space_rect_to_screen
  2474. /// Converts rectangle from screen space into layout space
  2475. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2476. /// struct nk_rect nk_layout_space_rect_to_screen(struct nk_context*, struct nk_rect);
  2477. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2478. ///
  2479. /// Parameter | Description
  2480. /// ------------|-----------------------------------------------------------
  2481. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2482. /// __bounds__ | Rectangle to convert from layout space into screen space
  2483. ///
  2484. /// Returns transformed `nk_rect` in screen space coordinates
  2485. */
  2486. NK_API struct nk_rect nk_layout_space_rect_to_screen(struct nk_context*, struct nk_rect);
  2487. /*/// #### nk_layout_space_rect_to_local
  2488. /// Converts rectangle from layout space into screen space
  2489. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2490. /// struct nk_rect nk_layout_space_rect_to_local(struct nk_context*, struct nk_rect);
  2491. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2492. ///
  2493. /// Parameter | Description
  2494. /// ------------|-----------------------------------------------------------
  2495. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2496. /// __bounds__ | Rectangle to convert from layout space into screen space
  2497. ///
  2498. /// Returns transformed `nk_rect` in layout space coordinates
  2499. */
  2500. NK_API struct nk_rect nk_layout_space_rect_to_local(struct nk_context*, struct nk_rect);
  2501. /* =============================================================================
  2502. *
  2503. * GROUP
  2504. *
  2505. * =============================================================================
  2506. /// ### Groups
  2507. /// Groups are basically windows inside windows. They allow to subdivide space
  2508. /// in a window to layout widgets as a group. Almost all more complex widget
  2509. /// layouting requirements can be solved using groups and basic layouting
  2510. /// fuctionality. Groups just like windows are identified by an unique name and
  2511. /// internally keep track of scrollbar offsets by default. However additional
  2512. /// versions are provided to directly manage the scrollbar.
  2513. ///
  2514. /// #### Usage
  2515. /// To create a group you have to call one of the three `nk_group_begin_xxx`
  2516. /// functions to start group declarations and `nk_group_end` at the end. Furthermore it
  2517. /// is required to check the return value of `nk_group_begin_xxx` and only process
  2518. /// widgets inside the window if the value is not 0.
  2519. /// Nesting groups is possible and even encouraged since many layouting schemes
  2520. /// can only be achieved by nesting. Groups, unlike windows, need `nk_group_end`
  2521. /// to be only called if the corosponding `nk_group_begin_xxx` call does not return 0:
  2522. ///
  2523. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2524. /// if (nk_group_begin_xxx(ctx, ...) {
  2525. /// // [... widgets ...]
  2526. /// nk_group_end(ctx);
  2527. /// }
  2528. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2529. ///
  2530. /// In the grand concept groups can be called after starting a window
  2531. /// with `nk_begin_xxx` and before calling `nk_end`:
  2532. ///
  2533. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2534. /// struct nk_context ctx;
  2535. /// nk_init_xxx(&ctx, ...);
  2536. /// while (1) {
  2537. /// // Input
  2538. /// Event evt;
  2539. /// nk_input_begin(&ctx);
  2540. /// while (GetEvent(&evt)) {
  2541. /// if (evt.type == MOUSE_MOVE)
  2542. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  2543. /// else if (evt.type == [...]) {
  2544. /// nk_input_xxx(...);
  2545. /// }
  2546. /// }
  2547. /// nk_input_end(&ctx);
  2548. /// //
  2549. /// // Window
  2550. /// if (nk_begin_xxx(...) {
  2551. /// // [...widgets...]
  2552. /// nk_layout_row_dynamic(...);
  2553. /// if (nk_group_begin_xxx(ctx, ...) {
  2554. /// //[... widgets ...]
  2555. /// nk_group_end(ctx);
  2556. /// }
  2557. /// }
  2558. /// nk_end(ctx);
  2559. /// //
  2560. /// // Draw
  2561. /// const struct nk_command *cmd = 0;
  2562. /// nk_foreach(cmd, &ctx) {
  2563. /// switch (cmd->type) {
  2564. /// case NK_COMMAND_LINE:
  2565. /// your_draw_line_function(...)
  2566. /// break;
  2567. /// case NK_COMMAND_RECT
  2568. /// your_draw_rect_function(...)
  2569. /// break;
  2570. /// case ...:
  2571. /// // [...]
  2572. /// }
  2573. // nk_clear(&ctx);
  2574. /// }
  2575. /// nk_free(&ctx);
  2576. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2577. /// #### Reference
  2578. /// Function | Description
  2579. /// --------------------------------|-------------------------------------------
  2580. /// nk_group_begin | Start a new group with internal scrollbar handling
  2581. /// nk_group_begin_titled | Start a new group with separeted name and title and internal scrollbar handling
  2582. /// nk_group_end | Ends a group. Should only be called if nk_group_begin returned non-zero
  2583. /// nk_group_scrolled_offset_begin | Start a new group with manual separated handling of scrollbar x- and y-offset
  2584. /// nk_group_scrolled_begin | Start a new group with manual scrollbar handling
  2585. /// nk_group_scrolled_end | Ends a group with manual scrollbar handling. Should only be called if nk_group_begin returned non-zero
  2586. */
  2587. /*/// #### nk_group_begin
  2588. /// Starts a new widget group. Requires a previous layouting function to specify a pos/size.
  2589. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2590. /// int nk_group_begin(struct nk_context*, const char *title, nk_flags);
  2591. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2592. ///
  2593. /// Parameter | Description
  2594. /// ------------|-----------------------------------------------------------
  2595. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2596. /// __title__ | Must be an unique identifier for this group that is also used for the group header
  2597. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different group behaviors
  2598. ///
  2599. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2600. */
  2601. NK_API int nk_group_begin(struct nk_context*, const char *title, nk_flags);
  2602. /*/// #### nk_group_begin_titled
  2603. /// Starts a new widget group. Requires a previous layouting function to specify a pos/size.
  2604. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2605. /// int nk_group_begin_titled(struct nk_context*, const char *name, const char *title, nk_flags);
  2606. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2607. ///
  2608. /// Parameter | Description
  2609. /// ------------|-----------------------------------------------------------
  2610. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2611. /// __id__ | Must be an unique identifier for this group
  2612. /// __title__ | Group header title
  2613. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different group behaviors
  2614. ///
  2615. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2616. */
  2617. NK_API int nk_group_begin_titled(struct nk_context*, const char *name, const char *title, nk_flags);
  2618. /*/// #### nk_group_end
  2619. /// Ends a widget group
  2620. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2621. /// void nk_group_end(struct nk_context*);
  2622. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2623. ///
  2624. /// Parameter | Description
  2625. /// ------------|-----------------------------------------------------------
  2626. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2627. */
  2628. NK_API void nk_group_end(struct nk_context*);
  2629. /*/// #### nk_group_scrolled_offset_begin
  2630. /// starts a new widget group. requires a previous layouting function to specify
  2631. /// a size. Does not keep track of scrollbar.
  2632. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2633. /// int nk_group_scrolled_offset_begin(struct nk_context*, nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags);
  2634. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2635. ///
  2636. /// Parameter | Description
  2637. /// ------------|-----------------------------------------------------------
  2638. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2639. /// __x_offset__| Scrollbar x-offset to offset all widgets inside the group horizontally.
  2640. /// __y_offset__| Scrollbar y-offset to offset all widgets inside the group vertically
  2641. /// __title__ | Window unique group title used to both identify and display in the group header
  2642. /// __flags__ | Window flags from the nk_panel_flags section
  2643. ///
  2644. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2645. */
  2646. NK_API int nk_group_scrolled_offset_begin(struct nk_context*, nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags);
  2647. /*/// #### nk_group_scrolled_begin
  2648. /// Starts a new widget group. requires a previous
  2649. /// layouting function to specify a size. Does not keep track of scrollbar.
  2650. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2651. /// int nk_group_scrolled_begin(struct nk_context*, struct nk_scroll *off, const char *title, nk_flags);
  2652. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2653. ///
  2654. /// Parameter | Description
  2655. /// ------------|-----------------------------------------------------------
  2656. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2657. /// __off__ | Both x- and y- scroll offset. Allows for manual scrollbar control
  2658. /// __title__ | Window unique group title used to both identify and display in the group header
  2659. /// __flags__ | Window flags from nk_panel_flags section
  2660. ///
  2661. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2662. */
  2663. NK_API int nk_group_scrolled_begin(struct nk_context*, struct nk_scroll *off, const char *title, nk_flags);
  2664. /*/// #### nk_group_scrolled_end
  2665. /// Ends a widget group after calling nk_group_scrolled_offset_begin or nk_group_scrolled_begin.
  2666. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2667. /// void nk_group_scrolled_end(struct nk_context*);
  2668. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2669. ///
  2670. /// Parameter | Description
  2671. /// ------------|-----------------------------------------------------------
  2672. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2673. */
  2674. NK_API void nk_group_scrolled_end(struct nk_context*);
  2675. /* =============================================================================
  2676. *
  2677. * TREE
  2678. *
  2679. * =============================================================================
  2680. /// ### Tree
  2681. /// Trees represent two different concept. First the concept of a collapsable
  2682. /// UI section that can be either in a hidden or visibile state. They allow the UI
  2683. /// user to selectively minimize the current set of visible UI to comprehend.
  2684. /// The second concept are tree widgets for visual UI representation of trees.<br /><br />
  2685. ///
  2686. /// Trees thereby can be nested for tree representations and multiple nested
  2687. /// collapsable UI sections. All trees are started by calling of the
  2688. /// `nk_tree_xxx_push_tree` functions and ended by calling one of the
  2689. /// `nk_tree_xxx_pop_xxx()` functions. Each starting functions takes a title label
  2690. /// and optionally an image to be displayed and the initial collapse state from
  2691. /// the nk_collapse_states section.<br /><br />
  2692. ///
  2693. /// The runtime state of the tree is either stored outside the library by the caller
  2694. /// or inside which requires a unique ID. The unique ID can either be generated
  2695. /// automatically from `__FILE__` and `__LINE__` with function `nk_tree_push`,
  2696. /// by `__FILE__` and a user provided ID generated for example by loop index with
  2697. /// function `nk_tree_push_id` or completely provided from outside by user with
  2698. /// function `nk_tree_push_hashed`.
  2699. ///
  2700. /// #### Usage
  2701. /// To create a tree you have to call one of the seven `nk_tree_xxx_push_xxx`
  2702. /// functions to start a collapsable UI section and `nk_tree_xxx_pop` to mark the
  2703. /// end.
  2704. /// Each starting function will either return `false(0)` if the tree is collapsed
  2705. /// or hidden and therefore does not need to be filled with content or `true(1)`
  2706. /// if visible and required to be filled.
  2707. ///
  2708. /// !!! Note
  2709. /// The tree header does not require and layouting function and instead
  2710. /// calculates a auto height based on the currently used font size
  2711. ///
  2712. /// The tree ending functions only need to be called if the tree content is
  2713. /// actually visible. So make sure the tree push function is guarded by `if`
  2714. /// and the pop call is only taken if the tree is visible.
  2715. ///
  2716. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2717. /// if (nk_tree_push(ctx, NK_TREE_TAB, "Tree", NK_MINIMIZED)) {
  2718. /// nk_layout_row_dynamic(...);
  2719. /// nk_widget(...);
  2720. /// nk_tree_pop(ctx);
  2721. /// }
  2722. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2723. ///
  2724. /// #### Reference
  2725. /// Function | Description
  2726. /// ----------------------------|-------------------------------------------
  2727. /// nk_tree_push | Start a collapsable UI section with internal state management
  2728. /// nk_tree_push_id | Start a collapsable UI section with internal state management callable in a look
  2729. /// nk_tree_push_hashed | Start a collapsable UI section with internal state management with full control over internal unique ID use to store state
  2730. /// nk_tree_image_push | Start a collapsable UI section with image and label header
  2731. /// nk_tree_image_push_id | Start a collapsable UI section with image and label header and internal state management callable in a look
  2732. /// nk_tree_image_push_hashed | Start a collapsable UI section with image and label header and internal state management with full control over internal unique ID use to store state
  2733. /// nk_tree_pop | Ends a collapsable UI section
  2734. //
  2735. /// nk_tree_state_push | Start a collapsable UI section with external state management
  2736. /// nk_tree_state_image_push | Start a collapsable UI section with image and label header and external state management
  2737. /// nk_tree_state_pop | Ends a collapsabale UI section
  2738. ///
  2739. /// #### nk_tree_type
  2740. /// Flag | Description
  2741. /// ----------------|----------------------------------------
  2742. /// NK_TREE_NODE | Highlighted tree header to mark a collapsable UI section
  2743. /// NK_TREE_TAB | Non-highighted tree header closer to tree representations
  2744. */
  2745. /*/// #### nk_tree_push
  2746. /// Starts a collapsable UI section with internal state management
  2747. /// !!! WARNING
  2748. /// To keep track of the runtime tree collapsable state this function uses
  2749. /// defines `__FILE__` and `__LINE__` to generate a unique ID. If you want
  2750. /// to call this function in a loop please use `nk_tree_push_id` or
  2751. /// `nk_tree_push_hashed` instead.
  2752. ///
  2753. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2754. /// #define nk_tree_push(ctx, type, title, state)
  2755. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2756. ///
  2757. /// Parameter | Description
  2758. /// ------------|-----------------------------------------------------------
  2759. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2760. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2761. /// __title__ | Label printed in the tree header
  2762. /// __state__ | Initial tree state value out of nk_collapse_states
  2763. ///
  2764. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2765. */
  2766. #define nk_tree_push(ctx, type, title, state) nk_tree_push_hashed(ctx, type, title, state, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),__LINE__)
  2767. /*/// #### nk_tree_push_id
  2768. /// Starts a collapsable UI section with internal state management callable in a look
  2769. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2770. /// #define nk_tree_push_id(ctx, type, title, state, id)
  2771. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2772. ///
  2773. /// Parameter | Description
  2774. /// ------------|-----------------------------------------------------------
  2775. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2776. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2777. /// __title__ | Label printed in the tree header
  2778. /// __state__ | Initial tree state value out of nk_collapse_states
  2779. /// __id__ | Loop counter index if this function is called in a loop
  2780. ///
  2781. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2782. */
  2783. #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)
  2784. /*/// #### nk_tree_push_hashed
  2785. /// Start a collapsable UI section with internal state management with full
  2786. /// control over internal unique ID used to store state
  2787. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2788. /// 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);
  2789. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2790. ///
  2791. /// Parameter | Description
  2792. /// ------------|-----------------------------------------------------------
  2793. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2794. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2795. /// __title__ | Label printed in the tree header
  2796. /// __state__ | Initial tree state value out of nk_collapse_states
  2797. /// __hash__ | Memory block or string to generate the ID from
  2798. /// __len__ | Size of passed memory block or string in __hash__
  2799. /// __seed__ | Seeding value if this function is called in a loop or default to `0`
  2800. ///
  2801. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2802. */
  2803. 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);
  2804. /*/// #### nk_tree_image_push
  2805. /// Start a collapsable UI section with image and label header
  2806. /// !!! WARNING
  2807. /// To keep track of the runtime tree collapsable state this function uses
  2808. /// defines `__FILE__` and `__LINE__` to generate a unique ID. If you want
  2809. /// to call this function in a loop please use `nk_tree_image_push_id` or
  2810. /// `nk_tree_image_push_hashed` instead.
  2811. ///
  2812. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2813. /// #define nk_tree_image_push(ctx, type, img, title, state)
  2814. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2815. //
  2816. /// Parameter | Description
  2817. /// ------------|-----------------------------------------------------------
  2818. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2819. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2820. /// __img__ | Image to display inside the header on the left of the label
  2821. /// __title__ | Label printed in the tree header
  2822. /// __state__ | Initial tree state value out of nk_collapse_states
  2823. ///
  2824. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2825. */
  2826. #define nk_tree_image_push(ctx, type, img, title, state) nk_tree_image_push_hashed(ctx, type, img, title, state, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),__LINE__)
  2827. /*/// #### nk_tree_image_push_id
  2828. /// Start a collapsable UI section with image and label header and internal state
  2829. /// management callable in a look
  2830. ///
  2831. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2832. /// #define nk_tree_image_push_id(ctx, type, img, title, state, id)
  2833. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2834. ///
  2835. /// Parameter | Description
  2836. /// ------------|-----------------------------------------------------------
  2837. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2838. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2839. /// __img__ | Image to display inside the header on the left of the label
  2840. /// __title__ | Label printed in the tree header
  2841. /// __state__ | Initial tree state value out of nk_collapse_states
  2842. /// __id__ | Loop counter index if this function is called in a loop
  2843. ///
  2844. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2845. */
  2846. #define nk_tree_image_push_id(ctx, type, img, title, state, id) nk_tree_image_push_hashed(ctx, type, img, title, state, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),id)
  2847. /*/// #### nk_tree_image_push_hashed
  2848. /// Start a collapsable UI section with internal state management with full
  2849. /// control over internal unique ID used to store state
  2850. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2851. /// 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);
  2852. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2853. ///
  2854. /// Parameter | Description
  2855. /// ------------|-----------------------------------------------------------
  2856. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2857. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2858. /// __img__ | Image to display inside the header on the left of the label
  2859. /// __title__ | Label printed in the tree header
  2860. /// __state__ | Initial tree state value out of nk_collapse_states
  2861. /// __hash__ | Memory block or string to generate the ID from
  2862. /// __len__ | Size of passed memory block or string in __hash__
  2863. /// __seed__ | Seeding value if this function is called in a loop or default to `0`
  2864. ///
  2865. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2866. */
  2867. 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);
  2868. /*/// #### nk_tree_pop
  2869. /// Ends a collapsabale UI section
  2870. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2871. /// void nk_tree_pop(struct nk_context*);
  2872. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2873. ///
  2874. /// Parameter | Description
  2875. /// ------------|-----------------------------------------------------------
  2876. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  2877. */
  2878. NK_API void nk_tree_pop(struct nk_context*);
  2879. /*/// #### nk_tree_state_push
  2880. /// Start a collapsable UI section with external state management
  2881. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2882. /// int nk_tree_state_push(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states *state);
  2883. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2884. ///
  2885. /// Parameter | Description
  2886. /// ------------|-----------------------------------------------------------
  2887. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  2888. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2889. /// __title__ | Label printed in the tree header
  2890. /// __state__ | Persistent state to update
  2891. ///
  2892. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2893. */
  2894. NK_API int nk_tree_state_push(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states *state);
  2895. /*/// #### nk_tree_state_image_push
  2896. /// Start a collapsable UI section with image and label header and external state management
  2897. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2898. /// int nk_tree_state_image_push(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states *state);
  2899. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2900. ///
  2901. /// Parameter | Description
  2902. /// ------------|-----------------------------------------------------------
  2903. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  2904. /// __img__ | Image to display inside the header on the left of the label
  2905. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2906. /// __title__ | Label printed in the tree header
  2907. /// __state__ | Persistent state to update
  2908. ///
  2909. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2910. */
  2911. NK_API int nk_tree_state_image_push(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states *state);
  2912. /*/// #### nk_tree_state_pop
  2913. /// Ends a collapsabale UI section
  2914. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2915. /// void nk_tree_state_pop(struct nk_context*);
  2916. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2917. ///
  2918. /// Parameter | Description
  2919. /// ------------|-----------------------------------------------------------
  2920. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  2921. */
  2922. NK_API void nk_tree_state_pop(struct nk_context*);
  2923. #define nk_tree_element_push(ctx, type, title, state, sel) nk_tree_element_push_hashed(ctx, type, title, state, sel, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),__LINE__)
  2924. #define nk_tree_element_push_id(ctx, type, title, state, sel, id) nk_tree_element_push_hashed(ctx, type, title, state, sel, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),id)
  2925. NK_API int nk_tree_element_push_hashed(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states initial_state, int *selected, const char *hash, int len, int seed);
  2926. NK_API int nk_tree_element_image_push_hashed(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states initial_state, int *selected, const char *hash, int len,int seed);
  2927. NK_API void nk_tree_element_pop(struct nk_context*);
  2928. /* =============================================================================
  2929. *
  2930. * LIST VIEW
  2931. *
  2932. * ============================================================================= */
  2933. struct nk_list_view {
  2934. /* public: */
  2935. int begin, end, count;
  2936. /* private: */
  2937. int total_height;
  2938. struct nk_context *ctx;
  2939. nk_uint *scroll_pointer;
  2940. nk_uint scroll_value;
  2941. };
  2942. NK_API int nk_list_view_begin(struct nk_context*, struct nk_list_view *out, const char *id, nk_flags, int row_height, int row_count);
  2943. NK_API void nk_list_view_end(struct nk_list_view*);
  2944. /* =============================================================================
  2945. *
  2946. * WIDGET
  2947. *
  2948. * ============================================================================= */
  2949. enum nk_widget_layout_states {
  2950. NK_WIDGET_INVALID, /* The widget cannot be seen and is completely out of view */
  2951. NK_WIDGET_VALID, /* The widget is completely inside the window and can be updated and drawn */
  2952. NK_WIDGET_ROM /* The widget is partially visible and cannot be updated */
  2953. };
  2954. enum nk_widget_states {
  2955. NK_WIDGET_STATE_MODIFIED = NK_FLAG(1),
  2956. NK_WIDGET_STATE_INACTIVE = NK_FLAG(2), /* widget is neither active nor hovered */
  2957. NK_WIDGET_STATE_ENTERED = NK_FLAG(3), /* widget has been hovered on the current frame */
  2958. NK_WIDGET_STATE_HOVER = NK_FLAG(4), /* widget is being hovered */
  2959. NK_WIDGET_STATE_ACTIVED = NK_FLAG(5),/* widget is currently activated */
  2960. NK_WIDGET_STATE_LEFT = NK_FLAG(6), /* widget is from this frame on not hovered anymore */
  2961. NK_WIDGET_STATE_HOVERED = NK_WIDGET_STATE_HOVER|NK_WIDGET_STATE_MODIFIED, /* widget is being hovered */
  2962. NK_WIDGET_STATE_ACTIVE = NK_WIDGET_STATE_ACTIVED|NK_WIDGET_STATE_MODIFIED /* widget is currently activated */
  2963. };
  2964. NK_API enum nk_widget_layout_states nk_widget(struct nk_rect*, const struct nk_context*);
  2965. NK_API enum nk_widget_layout_states nk_widget_fitting(struct nk_rect*, struct nk_context*, struct nk_vec2);
  2966. NK_API struct nk_rect nk_widget_bounds(struct nk_context*);
  2967. NK_API struct nk_vec2 nk_widget_position(struct nk_context*);
  2968. NK_API struct nk_vec2 nk_widget_size(struct nk_context*);
  2969. NK_API float nk_widget_width(struct nk_context*);
  2970. NK_API float nk_widget_height(struct nk_context*);
  2971. NK_API int nk_widget_is_hovered(struct nk_context*);
  2972. NK_API int nk_widget_is_mouse_clicked(struct nk_context*, enum nk_buttons);
  2973. NK_API int nk_widget_has_mouse_click_down(struct nk_context*, enum nk_buttons, int down);
  2974. NK_API void nk_spacing(struct nk_context*, int cols);
  2975. /* =============================================================================
  2976. *
  2977. * TEXT
  2978. *
  2979. * ============================================================================= */
  2980. enum nk_text_align {
  2981. NK_TEXT_ALIGN_LEFT = 0x01,
  2982. NK_TEXT_ALIGN_CENTERED = 0x02,
  2983. NK_TEXT_ALIGN_RIGHT = 0x04,
  2984. NK_TEXT_ALIGN_TOP = 0x08,
  2985. NK_TEXT_ALIGN_MIDDLE = 0x10,
  2986. NK_TEXT_ALIGN_BOTTOM = 0x20
  2987. };
  2988. enum nk_text_alignment {
  2989. NK_TEXT_LEFT = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_LEFT,
  2990. NK_TEXT_CENTERED = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_CENTERED,
  2991. NK_TEXT_RIGHT = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_RIGHT
  2992. };
  2993. NK_API void nk_text(struct nk_context*, const char*, int, nk_flags);
  2994. NK_API void nk_text_colored(struct nk_context*, const char*, int, nk_flags, struct nk_color);
  2995. NK_API void nk_text_wrap(struct nk_context*, const char*, int);
  2996. NK_API void nk_text_wrap_colored(struct nk_context*, const char*, int, struct nk_color);
  2997. NK_API void nk_label(struct nk_context*, const char*, nk_flags align);
  2998. NK_API void nk_label_colored(struct nk_context*, const char*, nk_flags align, struct nk_color);
  2999. NK_API void nk_label_wrap(struct nk_context*, const char*);
  3000. NK_API void nk_label_colored_wrap(struct nk_context*, const char*, struct nk_color);
  3001. NK_API void nk_image(struct nk_context*, struct nk_image);
  3002. NK_API void nk_image_color(struct nk_context*, struct nk_image, struct nk_color);
  3003. #ifdef NK_INCLUDE_STANDARD_VARARGS
  3004. NK_API void nk_labelf(struct nk_context*, nk_flags, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(3);
  3005. NK_API void nk_labelf_colored(struct nk_context*, nk_flags, struct nk_color, NK_PRINTF_FORMAT_STRING const char*,...) NK_PRINTF_VARARG_FUNC(4);
  3006. NK_API void nk_labelf_wrap(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*,...) NK_PRINTF_VARARG_FUNC(2);
  3007. NK_API void nk_labelf_colored_wrap(struct nk_context*, struct nk_color, NK_PRINTF_FORMAT_STRING const char*,...) NK_PRINTF_VARARG_FUNC(3);
  3008. NK_API void nk_labelfv(struct nk_context*, nk_flags, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(3);
  3009. NK_API void nk_labelfv_colored(struct nk_context*, nk_flags, struct nk_color, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(4);
  3010. NK_API void nk_labelfv_wrap(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(2);
  3011. NK_API void nk_labelfv_colored_wrap(struct nk_context*, struct nk_color, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(3);
  3012. NK_API void nk_value_bool(struct nk_context*, const char *prefix, int);
  3013. NK_API void nk_value_int(struct nk_context*, const char *prefix, int);
  3014. NK_API void nk_value_uint(struct nk_context*, const char *prefix, unsigned int);
  3015. NK_API void nk_value_float(struct nk_context*, const char *prefix, float);
  3016. NK_API void nk_value_color_byte(struct nk_context*, const char *prefix, struct nk_color);
  3017. NK_API void nk_value_color_float(struct nk_context*, const char *prefix, struct nk_color);
  3018. NK_API void nk_value_color_hex(struct nk_context*, const char *prefix, struct nk_color);
  3019. #endif
  3020. /* =============================================================================
  3021. *
  3022. * BUTTON
  3023. *
  3024. * ============================================================================= */
  3025. NK_API int nk_button_text(struct nk_context*, const char *title, int len);
  3026. NK_API int nk_button_label(struct nk_context*, const char *title);
  3027. NK_API int nk_button_color(struct nk_context*, struct nk_color);
  3028. NK_API int nk_button_symbol(struct nk_context*, enum nk_symbol_type);
  3029. NK_API int nk_button_image(struct nk_context*, struct nk_image img);
  3030. NK_API int nk_button_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags text_alignment);
  3031. NK_API int nk_button_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3032. NK_API int nk_button_image_label(struct nk_context*, struct nk_image img, const char*, nk_flags text_alignment);
  3033. NK_API int nk_button_image_text(struct nk_context*, struct nk_image img, const char*, int, nk_flags alignment);
  3034. NK_API int nk_button_text_styled(struct nk_context*, const struct nk_style_button*, const char *title, int len);
  3035. NK_API int nk_button_label_styled(struct nk_context*, const struct nk_style_button*, const char *title);
  3036. NK_API int nk_button_symbol_styled(struct nk_context*, const struct nk_style_button*, enum nk_symbol_type);
  3037. NK_API int nk_button_image_styled(struct nk_context*, const struct nk_style_button*, struct nk_image img);
  3038. NK_API int nk_button_symbol_text_styled(struct nk_context*,const struct nk_style_button*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3039. NK_API int nk_button_symbol_label_styled(struct nk_context *ctx, const struct nk_style_button *style, enum nk_symbol_type symbol, const char *title, nk_flags align);
  3040. NK_API int nk_button_image_label_styled(struct nk_context*,const struct nk_style_button*, struct nk_image img, const char*, nk_flags text_alignment);
  3041. NK_API int nk_button_image_text_styled(struct nk_context*,const struct nk_style_button*, struct nk_image img, const char*, int, nk_flags alignment);
  3042. NK_API void nk_button_set_behavior(struct nk_context*, enum nk_button_behavior);
  3043. NK_API int nk_button_push_behavior(struct nk_context*, enum nk_button_behavior);
  3044. NK_API int nk_button_pop_behavior(struct nk_context*);
  3045. /* =============================================================================
  3046. *
  3047. * CHECKBOX
  3048. *
  3049. * ============================================================================= */
  3050. NK_API int nk_check_label(struct nk_context*, const char*, int active);
  3051. NK_API int nk_check_text(struct nk_context*, const char*, int,int active);
  3052. NK_API unsigned nk_check_flags_label(struct nk_context*, const char*, unsigned int flags, unsigned int value);
  3053. NK_API unsigned nk_check_flags_text(struct nk_context*, const char*, int, unsigned int flags, unsigned int value);
  3054. NK_API int nk_checkbox_label(struct nk_context*, const char*, int *active);
  3055. NK_API int nk_checkbox_text(struct nk_context*, const char*, int, int *active);
  3056. NK_API int nk_checkbox_flags_label(struct nk_context*, const char*, unsigned int *flags, unsigned int value);
  3057. NK_API int nk_checkbox_flags_text(struct nk_context*, const char*, int, unsigned int *flags, unsigned int value);
  3058. /* =============================================================================
  3059. *
  3060. * RADIO BUTTON
  3061. *
  3062. * ============================================================================= */
  3063. NK_API int nk_radio_label(struct nk_context*, const char*, int *active);
  3064. NK_API int nk_radio_text(struct nk_context*, const char*, int, int *active);
  3065. NK_API int nk_option_label(struct nk_context*, const char*, int active);
  3066. NK_API int nk_option_text(struct nk_context*, const char*, int, int active);
  3067. /* =============================================================================
  3068. *
  3069. * SELECTABLE
  3070. *
  3071. * ============================================================================= */
  3072. NK_API int nk_selectable_label(struct nk_context*, const char*, nk_flags align, int *value);
  3073. NK_API int nk_selectable_text(struct nk_context*, const char*, int, nk_flags align, int *value);
  3074. NK_API int nk_selectable_image_label(struct nk_context*,struct nk_image, const char*, nk_flags align, int *value);
  3075. NK_API int nk_selectable_image_text(struct nk_context*,struct nk_image, const char*, int, nk_flags align, int *value);
  3076. NK_API int nk_selectable_symbol_label(struct nk_context*,enum nk_symbol_type, const char*, nk_flags align, int *value);
  3077. NK_API int nk_selectable_symbol_text(struct nk_context*,enum nk_symbol_type, const char*, int, nk_flags align, int *value);
  3078. NK_API int nk_select_label(struct nk_context*, const char*, nk_flags align, int value);
  3079. NK_API int nk_select_text(struct nk_context*, const char*, int, nk_flags align, int value);
  3080. NK_API int nk_select_image_label(struct nk_context*, struct nk_image,const char*, nk_flags align, int value);
  3081. NK_API int nk_select_image_text(struct nk_context*, struct nk_image,const char*, int, nk_flags align, int value);
  3082. NK_API int nk_select_symbol_label(struct nk_context*,enum nk_symbol_type, const char*, nk_flags align, int value);
  3083. NK_API int nk_select_symbol_text(struct nk_context*,enum nk_symbol_type, const char*, int, nk_flags align, int value);
  3084. /* =============================================================================
  3085. *
  3086. * SLIDER
  3087. *
  3088. * ============================================================================= */
  3089. NK_API float nk_slide_float(struct nk_context*, float min, float val, float max, float step);
  3090. NK_API int nk_slide_int(struct nk_context*, int min, int val, int max, int step);
  3091. NK_API int nk_slider_float(struct nk_context*, float min, float *val, float max, float step);
  3092. NK_API int nk_slider_int(struct nk_context*, int min, int *val, int max, int step);
  3093. /* =============================================================================
  3094. *
  3095. * PROGRESSBAR
  3096. *
  3097. * ============================================================================= */
  3098. NK_API int nk_progress(struct nk_context*, nk_size *cur, nk_size max, int modifyable);
  3099. NK_API nk_size nk_prog(struct nk_context*, nk_size cur, nk_size max, int modifyable);
  3100. /* =============================================================================
  3101. *
  3102. * COLOR PICKER
  3103. *
  3104. * ============================================================================= */
  3105. NK_API struct nk_colorf nk_color_picker(struct nk_context*, struct nk_colorf, enum nk_color_format);
  3106. NK_API int nk_color_pick(struct nk_context*, struct nk_colorf*, enum nk_color_format);
  3107. /* =============================================================================
  3108. *
  3109. * PROPERTIES
  3110. *
  3111. * =============================================================================
  3112. /// ### Properties
  3113. /// Properties are the main value modification widgets in Nuklear. Changing a value
  3114. /// can be achieved by dragging, adding/removing incremental steps on button click
  3115. /// or by directly typing a number.
  3116. ///
  3117. /// #### Usage
  3118. /// Each property requires a unique name for identifaction that is also used for
  3119. /// displaying a label. If you want to use the same name multiple times make sure
  3120. /// add a '#' before your name. The '#' will not be shown but will generate a
  3121. /// unique ID. Each propery also takes in a minimum and maximum value. If you want
  3122. /// to make use of the complete number range of a type just use the provided
  3123. /// type limits from `limits.h`. For example `INT_MIN` and `INT_MAX` for
  3124. /// `nk_property_int` and `nk_propertyi`. In additional each property takes in
  3125. /// a increment value that will be added or subtracted if either the increment
  3126. /// decrement button is clicked. Finally there is a value for increment per pixel
  3127. /// dragged that is added or subtracted from the value.
  3128. ///
  3129. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3130. /// int value = 0;
  3131. /// struct nk_context ctx;
  3132. /// nk_init_xxx(&ctx, ...);
  3133. /// while (1) {
  3134. /// // Input
  3135. /// Event evt;
  3136. /// nk_input_begin(&ctx);
  3137. /// while (GetEvent(&evt)) {
  3138. /// if (evt.type == MOUSE_MOVE)
  3139. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  3140. /// else if (evt.type == [...]) {
  3141. /// nk_input_xxx(...);
  3142. /// }
  3143. /// }
  3144. /// nk_input_end(&ctx);
  3145. /// //
  3146. /// // Window
  3147. /// if (nk_begin_xxx(...) {
  3148. /// // Property
  3149. /// nk_layout_row_dynamic(...);
  3150. /// nk_property_int(ctx, "ID", INT_MIN, &value, INT_MAX, 1, 1);
  3151. /// }
  3152. /// nk_end(ctx);
  3153. /// //
  3154. /// // Draw
  3155. /// const struct nk_command *cmd = 0;
  3156. /// nk_foreach(cmd, &ctx) {
  3157. /// switch (cmd->type) {
  3158. /// case NK_COMMAND_LINE:
  3159. /// your_draw_line_function(...)
  3160. /// break;
  3161. /// case NK_COMMAND_RECT
  3162. /// your_draw_rect_function(...)
  3163. /// break;
  3164. /// case ...:
  3165. /// // [...]
  3166. /// }
  3167. // nk_clear(&ctx);
  3168. /// }
  3169. /// nk_free(&ctx);
  3170. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3171. ///
  3172. /// #### Reference
  3173. /// Function | Description
  3174. /// --------------------|-------------------------------------------
  3175. /// nk_property_int | Integer property directly modifing a passed in value
  3176. /// nk_property_float | Float property directly modifing a passed in value
  3177. /// nk_property_double | Double property directly modifing a passed in value
  3178. /// nk_propertyi | Integer property returning the modified int value
  3179. /// nk_propertyf | Float property returning the modified float value
  3180. /// nk_propertyd | Double property returning the modified double value
  3181. ///
  3182. */
  3183. /*/// #### nk_property_int
  3184. /// Integer property directly modifing a passed in value
  3185. /// !!! WARNING
  3186. /// To generate a unique property ID using the same label make sure to insert
  3187. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3188. ///
  3189. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3190. /// void nk_property_int(struct nk_context *ctx, const char *name, int min, int *val, int max, int step, float inc_per_pixel);
  3191. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3192. ///
  3193. /// Parameter | Description
  3194. /// --------------------|-----------------------------------------------------------
  3195. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3196. /// __name__ | String used both as a label as well as a unique identifier
  3197. /// __min__ | Minimum value not allowed to be underflown
  3198. /// __val__ | Integer pointer to be modified
  3199. /// __max__ | Maximum value not allowed to be overflown
  3200. /// __step__ | Increment added and subtracted on increment and decrement button
  3201. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3202. */
  3203. NK_API void nk_property_int(struct nk_context*, const char *name, int min, int *val, int max, int step, float inc_per_pixel);
  3204. /*/// #### nk_property_float
  3205. /// Float property directly modifing a passed in value
  3206. /// !!! WARNING
  3207. /// To generate a unique property ID using the same label make sure to insert
  3208. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3209. ///
  3210. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3211. /// void nk_property_float(struct nk_context *ctx, const char *name, float min, float *val, float max, float step, float inc_per_pixel);
  3212. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3213. ///
  3214. /// Parameter | Description
  3215. /// --------------------|-----------------------------------------------------------
  3216. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3217. /// __name__ | String used both as a label as well as a unique identifier
  3218. /// __min__ | Minimum value not allowed to be underflown
  3219. /// __val__ | Float pointer to be modified
  3220. /// __max__ | Maximum value not allowed to be overflown
  3221. /// __step__ | Increment added and subtracted on increment and decrement button
  3222. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3223. */
  3224. NK_API void nk_property_float(struct nk_context*, const char *name, float min, float *val, float max, float step, float inc_per_pixel);
  3225. /*/// #### nk_property_double
  3226. /// Double property directly modifing a passed in value
  3227. /// !!! WARNING
  3228. /// To generate a unique property ID using the same label make sure to insert
  3229. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3230. ///
  3231. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3232. /// void nk_property_double(struct nk_context *ctx, const char *name, double min, double *val, double max, double step, double inc_per_pixel);
  3233. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3234. ///
  3235. /// Parameter | Description
  3236. /// --------------------|-----------------------------------------------------------
  3237. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3238. /// __name__ | String used both as a label as well as a unique identifier
  3239. /// __min__ | Minimum value not allowed to be underflown
  3240. /// __val__ | Double pointer to be modified
  3241. /// __max__ | Maximum value not allowed to be overflown
  3242. /// __step__ | Increment added and subtracted on increment and decrement button
  3243. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3244. */
  3245. NK_API void nk_property_double(struct nk_context*, const char *name, double min, double *val, double max, double step, float inc_per_pixel);
  3246. /*/// #### nk_propertyi
  3247. /// Integer property modifing a passed in value and returning the new value
  3248. /// !!! WARNING
  3249. /// To generate a unique property ID using the same label make sure to insert
  3250. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3251. ///
  3252. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3253. /// int nk_propertyi(struct nk_context *ctx, const char *name, int min, int val, int max, int step, float inc_per_pixel);
  3254. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3255. ///
  3256. /// Parameter | Description
  3257. /// --------------------|-----------------------------------------------------------
  3258. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3259. /// __name__ | String used both as a label as well as a unique identifier
  3260. /// __min__ | Minimum value not allowed to be underflown
  3261. /// __val__ | Current integer value to be modified and returned
  3262. /// __max__ | Maximum value not allowed to be overflown
  3263. /// __step__ | Increment added and subtracted on increment and decrement button
  3264. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3265. ///
  3266. /// Returns the new modified integer value
  3267. */
  3268. NK_API int nk_propertyi(struct nk_context*, const char *name, int min, int val, int max, int step, float inc_per_pixel);
  3269. /*/// #### nk_propertyf
  3270. /// Float property modifing a passed in value and returning the new value
  3271. /// !!! WARNING
  3272. /// To generate a unique property ID using the same label make sure to insert
  3273. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3274. ///
  3275. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3276. /// float nk_propertyf(struct nk_context *ctx, const char *name, float min, float val, float max, float step, float inc_per_pixel);
  3277. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3278. ///
  3279. /// Parameter | Description
  3280. /// --------------------|-----------------------------------------------------------
  3281. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3282. /// __name__ | String used both as a label as well as a unique identifier
  3283. /// __min__ | Minimum value not allowed to be underflown
  3284. /// __val__ | Current float value to be modified and returned
  3285. /// __max__ | Maximum value not allowed to be overflown
  3286. /// __step__ | Increment added and subtracted on increment and decrement button
  3287. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3288. ///
  3289. /// Returns the new modified float value
  3290. */
  3291. NK_API float nk_propertyf(struct nk_context*, const char *name, float min, float val, float max, float step, float inc_per_pixel);
  3292. /*/// #### nk_propertyd
  3293. /// Float property modifing a passed in value and returning the new value
  3294. /// !!! WARNING
  3295. /// To generate a unique property ID using the same label make sure to insert
  3296. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3297. ///
  3298. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3299. /// float nk_propertyd(struct nk_context *ctx, const char *name, double min, double val, double max, double step, double inc_per_pixel);
  3300. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3301. ///
  3302. /// Parameter | Description
  3303. /// --------------------|-----------------------------------------------------------
  3304. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3305. /// __name__ | String used both as a label as well as a unique identifier
  3306. /// __min__ | Minimum value not allowed to be underflown
  3307. /// __val__ | Current double value to be modified and returned
  3308. /// __max__ | Maximum value not allowed to be overflown
  3309. /// __step__ | Increment added and subtracted on increment and decrement button
  3310. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3311. ///
  3312. /// Returns the new modified double value
  3313. */
  3314. NK_API double nk_propertyd(struct nk_context*, const char *name, double min, double val, double max, double step, float inc_per_pixel);
  3315. /* =============================================================================
  3316. *
  3317. * TEXT EDIT
  3318. *
  3319. * ============================================================================= */
  3320. enum nk_edit_flags {
  3321. NK_EDIT_DEFAULT = 0,
  3322. NK_EDIT_READ_ONLY = NK_FLAG(0),
  3323. NK_EDIT_AUTO_SELECT = NK_FLAG(1),
  3324. NK_EDIT_SIG_ENTER = NK_FLAG(2),
  3325. NK_EDIT_ALLOW_TAB = NK_FLAG(3),
  3326. NK_EDIT_NO_CURSOR = NK_FLAG(4),
  3327. NK_EDIT_SELECTABLE = NK_FLAG(5),
  3328. NK_EDIT_CLIPBOARD = NK_FLAG(6),
  3329. NK_EDIT_CTRL_ENTER_NEWLINE = NK_FLAG(7),
  3330. NK_EDIT_NO_HORIZONTAL_SCROLL = NK_FLAG(8),
  3331. NK_EDIT_ALWAYS_INSERT_MODE = NK_FLAG(9),
  3332. NK_EDIT_MULTILINE = NK_FLAG(10),
  3333. NK_EDIT_GOTO_END_ON_ACTIVATE = NK_FLAG(11)
  3334. };
  3335. enum nk_edit_types {
  3336. NK_EDIT_SIMPLE = NK_EDIT_ALWAYS_INSERT_MODE,
  3337. NK_EDIT_FIELD = NK_EDIT_SIMPLE|NK_EDIT_SELECTABLE|NK_EDIT_CLIPBOARD,
  3338. NK_EDIT_BOX = NK_EDIT_ALWAYS_INSERT_MODE| NK_EDIT_SELECTABLE| NK_EDIT_MULTILINE|NK_EDIT_ALLOW_TAB|NK_EDIT_CLIPBOARD,
  3339. NK_EDIT_EDITOR = NK_EDIT_SELECTABLE|NK_EDIT_MULTILINE|NK_EDIT_ALLOW_TAB| NK_EDIT_CLIPBOARD
  3340. };
  3341. enum nk_edit_events {
  3342. NK_EDIT_ACTIVE = NK_FLAG(0), /* edit widget is currently being modified */
  3343. NK_EDIT_INACTIVE = NK_FLAG(1), /* edit widget is not active and is not being modified */
  3344. NK_EDIT_ACTIVATED = NK_FLAG(2), /* edit widget went from state inactive to state active */
  3345. NK_EDIT_DEACTIVATED = NK_FLAG(3), /* edit widget went from state active to state inactive */
  3346. NK_EDIT_COMMITED = NK_FLAG(4) /* edit widget has received an enter and lost focus */
  3347. };
  3348. NK_API nk_flags nk_edit_string(struct nk_context*, nk_flags, char *buffer, int *len, int max, nk_plugin_filter);
  3349. NK_API nk_flags nk_edit_string_zero_terminated(struct nk_context*, nk_flags, char *buffer, int max, nk_plugin_filter);
  3350. NK_API nk_flags nk_edit_buffer(struct nk_context*, nk_flags, struct nk_text_edit*, nk_plugin_filter);
  3351. NK_API void nk_edit_focus(struct nk_context*, nk_flags flags);
  3352. NK_API void nk_edit_unfocus(struct nk_context*);
  3353. /* =============================================================================
  3354. *
  3355. * CHART
  3356. *
  3357. * ============================================================================= */
  3358. NK_API int nk_chart_begin(struct nk_context*, enum nk_chart_type, int num, float min, float max);
  3359. 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);
  3360. NK_API void nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type, int count, float min_value, float max_value);
  3361. 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);
  3362. NK_API nk_flags nk_chart_push(struct nk_context*, float);
  3363. NK_API nk_flags nk_chart_push_slot(struct nk_context*, float, int);
  3364. NK_API void nk_chart_end(struct nk_context*);
  3365. NK_API void nk_plot(struct nk_context*, enum nk_chart_type, const float *values, int count, int offset);
  3366. 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);
  3367. /* =============================================================================
  3368. *
  3369. * POPUP
  3370. *
  3371. * ============================================================================= */
  3372. NK_API int nk_popup_begin(struct nk_context*, enum nk_popup_type, const char*, nk_flags, struct nk_rect bounds);
  3373. NK_API void nk_popup_close(struct nk_context*);
  3374. NK_API void nk_popup_end(struct nk_context*);
  3375. /* =============================================================================
  3376. *
  3377. * COMBOBOX
  3378. *
  3379. * ============================================================================= */
  3380. NK_API int nk_combo(struct nk_context*, const char **items, int count, int selected, int item_height, struct nk_vec2 size);
  3381. NK_API int nk_combo_separator(struct nk_context*, const char *items_separated_by_separator, int separator, int selected, int count, int item_height, struct nk_vec2 size);
  3382. NK_API int nk_combo_string(struct nk_context*, const char *items_separated_by_zeros, int selected, int count, int item_height, struct nk_vec2 size);
  3383. 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, struct nk_vec2 size);
  3384. NK_API void nk_combobox(struct nk_context*, const char **items, int count, int *selected, int item_height, struct nk_vec2 size);
  3385. NK_API void nk_combobox_string(struct nk_context*, const char *items_separated_by_zeros, int *selected, int count, int item_height, struct nk_vec2 size);
  3386. NK_API void nk_combobox_separator(struct nk_context*, const char *items_separated_by_separator, int separator,int *selected, int count, int item_height, struct nk_vec2 size);
  3387. NK_API void nk_combobox_callback(struct nk_context*, void(*item_getter)(void*, int, const char**), void*, int *selected, int count, int item_height, struct nk_vec2 size);
  3388. /* =============================================================================
  3389. *
  3390. * ABSTRACT COMBOBOX
  3391. *
  3392. * ============================================================================= */
  3393. NK_API int nk_combo_begin_text(struct nk_context*, const char *selected, int, struct nk_vec2 size);
  3394. NK_API int nk_combo_begin_label(struct nk_context*, const char *selected, struct nk_vec2 size);
  3395. NK_API int nk_combo_begin_color(struct nk_context*, struct nk_color color, struct nk_vec2 size);
  3396. NK_API int nk_combo_begin_symbol(struct nk_context*, enum nk_symbol_type, struct nk_vec2 size);
  3397. NK_API int nk_combo_begin_symbol_label(struct nk_context*, const char *selected, enum nk_symbol_type, struct nk_vec2 size);
  3398. NK_API int nk_combo_begin_symbol_text(struct nk_context*, const char *selected, int, enum nk_symbol_type, struct nk_vec2 size);
  3399. NK_API int nk_combo_begin_image(struct nk_context*, struct nk_image img, struct nk_vec2 size);
  3400. NK_API int nk_combo_begin_image_label(struct nk_context*, const char *selected, struct nk_image, struct nk_vec2 size);
  3401. NK_API int nk_combo_begin_image_text(struct nk_context*, const char *selected, int, struct nk_image, struct nk_vec2 size);
  3402. NK_API int nk_combo_item_label(struct nk_context*, const char*, nk_flags alignment);
  3403. NK_API int nk_combo_item_text(struct nk_context*, const char*,int, nk_flags alignment);
  3404. NK_API int nk_combo_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  3405. NK_API int nk_combo_item_image_text(struct nk_context*, struct nk_image, const char*, int,nk_flags alignment);
  3406. NK_API int nk_combo_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  3407. NK_API int nk_combo_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3408. NK_API void nk_combo_close(struct nk_context*);
  3409. NK_API void nk_combo_end(struct nk_context*);
  3410. /* =============================================================================
  3411. *
  3412. * CONTEXTUAL
  3413. *
  3414. * ============================================================================= */
  3415. NK_API int nk_contextual_begin(struct nk_context*, nk_flags, struct nk_vec2, struct nk_rect trigger_bounds);
  3416. NK_API int nk_contextual_item_text(struct nk_context*, const char*, int,nk_flags align);
  3417. NK_API int nk_contextual_item_label(struct nk_context*, const char*, nk_flags align);
  3418. NK_API int nk_contextual_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  3419. NK_API int nk_contextual_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
  3420. NK_API int nk_contextual_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  3421. NK_API int nk_contextual_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3422. NK_API void nk_contextual_close(struct nk_context*);
  3423. NK_API void nk_contextual_end(struct nk_context*);
  3424. /* =============================================================================
  3425. *
  3426. * TOOLTIP
  3427. *
  3428. * ============================================================================= */
  3429. NK_API void nk_tooltip(struct nk_context*, const char*);
  3430. #ifdef NK_INCLUDE_STANDARD_VARARGS
  3431. NK_API void nk_tooltipf(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(2);
  3432. NK_API void nk_tooltipfv(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(2);
  3433. #endif
  3434. NK_API int nk_tooltip_begin(struct nk_context*, float width);
  3435. NK_API void nk_tooltip_end(struct nk_context*);
  3436. /* =============================================================================
  3437. *
  3438. * MENU
  3439. *
  3440. * ============================================================================= */
  3441. NK_API void nk_menubar_begin(struct nk_context*);
  3442. NK_API void nk_menubar_end(struct nk_context*);
  3443. NK_API int nk_menu_begin_text(struct nk_context*, const char* title, int title_len, nk_flags align, struct nk_vec2 size);
  3444. NK_API int nk_menu_begin_label(struct nk_context*, const char*, nk_flags align, struct nk_vec2 size);
  3445. NK_API int nk_menu_begin_image(struct nk_context*, const char*, struct nk_image, struct nk_vec2 size);
  3446. NK_API int nk_menu_begin_image_text(struct nk_context*, const char*, int,nk_flags align,struct nk_image, struct nk_vec2 size);
  3447. NK_API int nk_menu_begin_image_label(struct nk_context*, const char*, nk_flags align,struct nk_image, struct nk_vec2 size);
  3448. NK_API int nk_menu_begin_symbol(struct nk_context*, const char*, enum nk_symbol_type, struct nk_vec2 size);
  3449. NK_API int nk_menu_begin_symbol_text(struct nk_context*, const char*, int,nk_flags align,enum nk_symbol_type, struct nk_vec2 size);
  3450. NK_API int nk_menu_begin_symbol_label(struct nk_context*, const char*, nk_flags align,enum nk_symbol_type, struct nk_vec2 size);
  3451. NK_API int nk_menu_item_text(struct nk_context*, const char*, int,nk_flags align);
  3452. NK_API int nk_menu_item_label(struct nk_context*, const char*, nk_flags alignment);
  3453. NK_API int nk_menu_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  3454. NK_API int nk_menu_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
  3455. NK_API int nk_menu_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3456. NK_API int nk_menu_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  3457. NK_API void nk_menu_close(struct nk_context*);
  3458. NK_API void nk_menu_end(struct nk_context*);
  3459. /* =============================================================================
  3460. *
  3461. * STYLE
  3462. *
  3463. * ============================================================================= */
  3464. enum nk_style_colors {
  3465. NK_COLOR_TEXT,
  3466. NK_COLOR_WINDOW,
  3467. NK_COLOR_HEADER,
  3468. NK_COLOR_BORDER,
  3469. NK_COLOR_BUTTON,
  3470. NK_COLOR_BUTTON_HOVER,
  3471. NK_COLOR_BUTTON_ACTIVE,
  3472. NK_COLOR_TOGGLE,
  3473. NK_COLOR_TOGGLE_HOVER,
  3474. NK_COLOR_TOGGLE_CURSOR,
  3475. NK_COLOR_SELECT,
  3476. NK_COLOR_SELECT_ACTIVE,
  3477. NK_COLOR_SLIDER,
  3478. NK_COLOR_SLIDER_CURSOR,
  3479. NK_COLOR_SLIDER_CURSOR_HOVER,
  3480. NK_COLOR_SLIDER_CURSOR_ACTIVE,
  3481. NK_COLOR_PROPERTY,
  3482. NK_COLOR_EDIT,
  3483. NK_COLOR_EDIT_CURSOR,
  3484. NK_COLOR_COMBO,
  3485. NK_COLOR_CHART,
  3486. NK_COLOR_CHART_COLOR,
  3487. NK_COLOR_CHART_COLOR_HIGHLIGHT,
  3488. NK_COLOR_SCROLLBAR,
  3489. NK_COLOR_SCROLLBAR_CURSOR,
  3490. NK_COLOR_SCROLLBAR_CURSOR_HOVER,
  3491. NK_COLOR_SCROLLBAR_CURSOR_ACTIVE,
  3492. NK_COLOR_TAB_HEADER,
  3493. NK_COLOR_COUNT
  3494. };
  3495. enum nk_style_cursor {
  3496. NK_CURSOR_ARROW,
  3497. NK_CURSOR_TEXT,
  3498. NK_CURSOR_MOVE,
  3499. NK_CURSOR_RESIZE_VERTICAL,
  3500. NK_CURSOR_RESIZE_HORIZONTAL,
  3501. NK_CURSOR_RESIZE_TOP_LEFT_DOWN_RIGHT,
  3502. NK_CURSOR_RESIZE_TOP_RIGHT_DOWN_LEFT,
  3503. NK_CURSOR_COUNT
  3504. };
  3505. NK_API void nk_style_default(struct nk_context*);
  3506. NK_API void nk_style_from_table(struct nk_context*, const struct nk_color*);
  3507. NK_API void nk_style_load_cursor(struct nk_context*, enum nk_style_cursor, const struct nk_cursor*);
  3508. NK_API void nk_style_load_all_cursors(struct nk_context*, struct nk_cursor*);
  3509. NK_API const char* nk_style_get_color_by_name(enum nk_style_colors);
  3510. NK_API void nk_style_set_font(struct nk_context*, const struct nk_user_font*);
  3511. NK_API int nk_style_set_cursor(struct nk_context*, enum nk_style_cursor);
  3512. NK_API void nk_style_show_cursor(struct nk_context*);
  3513. NK_API void nk_style_hide_cursor(struct nk_context*);
  3514. NK_API int nk_style_push_font(struct nk_context*, const struct nk_user_font*);
  3515. NK_API int nk_style_push_float(struct nk_context*, float*, float);
  3516. NK_API int nk_style_push_vec2(struct nk_context*, struct nk_vec2*, struct nk_vec2);
  3517. NK_API int nk_style_push_style_item(struct nk_context*, struct nk_style_item*, struct nk_style_item);
  3518. NK_API int nk_style_push_flags(struct nk_context*, nk_flags*, nk_flags);
  3519. NK_API int nk_style_push_color(struct nk_context*, struct nk_color*, struct nk_color);
  3520. NK_API int nk_style_pop_font(struct nk_context*);
  3521. NK_API int nk_style_pop_float(struct nk_context*);
  3522. NK_API int nk_style_pop_vec2(struct nk_context*);
  3523. NK_API int nk_style_pop_style_item(struct nk_context*);
  3524. NK_API int nk_style_pop_flags(struct nk_context*);
  3525. NK_API int nk_style_pop_color(struct nk_context*);
  3526. /* =============================================================================
  3527. *
  3528. * COLOR
  3529. *
  3530. * ============================================================================= */
  3531. NK_API struct nk_color nk_rgb(int r, int g, int b);
  3532. NK_API struct nk_color nk_rgb_iv(const int *rgb);
  3533. NK_API struct nk_color nk_rgb_bv(const nk_byte* rgb);
  3534. NK_API struct nk_color nk_rgb_f(float r, float g, float b);
  3535. NK_API struct nk_color nk_rgb_fv(const float *rgb);
  3536. NK_API struct nk_color nk_rgb_cf(struct nk_colorf c);
  3537. NK_API struct nk_color nk_rgb_hex(const char *rgb);
  3538. NK_API struct nk_color nk_rgba(int r, int g, int b, int a);
  3539. NK_API struct nk_color nk_rgba_u32(nk_uint);
  3540. NK_API struct nk_color nk_rgba_iv(const int *rgba);
  3541. NK_API struct nk_color nk_rgba_bv(const nk_byte *rgba);
  3542. NK_API struct nk_color nk_rgba_f(float r, float g, float b, float a);
  3543. NK_API struct nk_color nk_rgba_fv(const float *rgba);
  3544. NK_API struct nk_color nk_rgba_cf(struct nk_colorf c);
  3545. NK_API struct nk_color nk_rgba_hex(const char *rgb);
  3546. NK_API struct nk_colorf nk_hsva_colorf(float h, float s, float v, float a);
  3547. NK_API struct nk_colorf nk_hsva_colorfv(float *c);
  3548. NK_API void nk_colorf_hsva_f(float *out_h, float *out_s, float *out_v, float *out_a, struct nk_colorf in);
  3549. NK_API void nk_colorf_hsva_fv(float *hsva, struct nk_colorf in);
  3550. NK_API struct nk_color nk_hsv(int h, int s, int v);
  3551. NK_API struct nk_color nk_hsv_iv(const int *hsv);
  3552. NK_API struct nk_color nk_hsv_bv(const nk_byte *hsv);
  3553. NK_API struct nk_color nk_hsv_f(float h, float s, float v);
  3554. NK_API struct nk_color nk_hsv_fv(const float *hsv);
  3555. NK_API struct nk_color nk_hsva(int h, int s, int v, int a);
  3556. NK_API struct nk_color nk_hsva_iv(const int *hsva);
  3557. NK_API struct nk_color nk_hsva_bv(const nk_byte *hsva);
  3558. NK_API struct nk_color nk_hsva_f(float h, float s, float v, float a);
  3559. NK_API struct nk_color nk_hsva_fv(const float *hsva);
  3560. /* color (conversion nuklear --> user) */
  3561. NK_API void nk_color_f(float *r, float *g, float *b, float *a, struct nk_color);
  3562. NK_API void nk_color_fv(float *rgba_out, struct nk_color);
  3563. NK_API struct nk_colorf nk_color_cf(struct nk_color);
  3564. NK_API void nk_color_d(double *r, double *g, double *b, double *a, struct nk_color);
  3565. NK_API void nk_color_dv(double *rgba_out, struct nk_color);
  3566. NK_API nk_uint nk_color_u32(struct nk_color);
  3567. NK_API void nk_color_hex_rgba(char *output, struct nk_color);
  3568. NK_API void nk_color_hex_rgb(char *output, struct nk_color);
  3569. NK_API void nk_color_hsv_i(int *out_h, int *out_s, int *out_v, struct nk_color);
  3570. NK_API void nk_color_hsv_b(nk_byte *out_h, nk_byte *out_s, nk_byte *out_v, struct nk_color);
  3571. NK_API void nk_color_hsv_iv(int *hsv_out, struct nk_color);
  3572. NK_API void nk_color_hsv_bv(nk_byte *hsv_out, struct nk_color);
  3573. NK_API void nk_color_hsv_f(float *out_h, float *out_s, float *out_v, struct nk_color);
  3574. NK_API void nk_color_hsv_fv(float *hsv_out, struct nk_color);
  3575. NK_API void nk_color_hsva_i(int *h, int *s, int *v, int *a, struct nk_color);
  3576. NK_API void nk_color_hsva_b(nk_byte *h, nk_byte *s, nk_byte *v, nk_byte *a, struct nk_color);
  3577. NK_API void nk_color_hsva_iv(int *hsva_out, struct nk_color);
  3578. NK_API void nk_color_hsva_bv(nk_byte *hsva_out, struct nk_color);
  3579. NK_API void nk_color_hsva_f(float *out_h, float *out_s, float *out_v, float *out_a, struct nk_color);
  3580. NK_API void nk_color_hsva_fv(float *hsva_out, struct nk_color);
  3581. /* =============================================================================
  3582. *
  3583. * IMAGE
  3584. *
  3585. * ============================================================================= */
  3586. NK_API nk_handle nk_handle_ptr(void*);
  3587. NK_API nk_handle nk_handle_id(int);
  3588. NK_API struct nk_image nk_image_handle(nk_handle);
  3589. NK_API struct nk_image nk_image_ptr(void*);
  3590. NK_API struct nk_image nk_image_id(int);
  3591. NK_API int nk_image_is_subimage(const struct nk_image* img);
  3592. NK_API struct nk_image nk_subimage_ptr(void*, unsigned short w, unsigned short h, struct nk_rect sub_region);
  3593. NK_API struct nk_image nk_subimage_id(int, unsigned short w, unsigned short h, struct nk_rect sub_region);
  3594. NK_API struct nk_image nk_subimage_handle(nk_handle, unsigned short w, unsigned short h, struct nk_rect sub_region);
  3595. /* =============================================================================
  3596. *
  3597. * MATH
  3598. *
  3599. * ============================================================================= */
  3600. NK_API nk_hash nk_murmur_hash(const void *key, int len, nk_hash seed);
  3601. NK_API void nk_triangle_from_direction(struct nk_vec2 *result, struct nk_rect r, float pad_x, float pad_y, enum nk_heading);
  3602. NK_API struct nk_vec2 nk_vec2(float x, float y);
  3603. NK_API struct nk_vec2 nk_vec2i(int x, int y);
  3604. NK_API struct nk_vec2 nk_vec2v(const float *xy);
  3605. NK_API struct nk_vec2 nk_vec2iv(const int *xy);
  3606. NK_API struct nk_rect nk_get_null_rect(void);
  3607. NK_API struct nk_rect nk_rect(float x, float y, float w, float h);
  3608. NK_API struct nk_rect nk_recti(int x, int y, int w, int h);
  3609. NK_API struct nk_rect nk_recta(struct nk_vec2 pos, struct nk_vec2 size);
  3610. NK_API struct nk_rect nk_rectv(const float *xywh);
  3611. NK_API struct nk_rect nk_rectiv(const int *xywh);
  3612. NK_API struct nk_vec2 nk_rect_pos(struct nk_rect);
  3613. NK_API struct nk_vec2 nk_rect_size(struct nk_rect);
  3614. /* =============================================================================
  3615. *
  3616. * STRING
  3617. *
  3618. * ============================================================================= */
  3619. NK_API int nk_strlen(const char *str);
  3620. NK_API int nk_stricmp(const char *s1, const char *s2);
  3621. NK_API int nk_stricmpn(const char *s1, const char *s2, int n);
  3622. NK_API int nk_strtoi(const char *str, const char **endptr);
  3623. NK_API float nk_strtof(const char *str, const char **endptr);
  3624. NK_API double nk_strtod(const char *str, const char **endptr);
  3625. NK_API int nk_strfilter(const char *text, const char *regexp);
  3626. NK_API int nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score);
  3627. NK_API int nk_strmatch_fuzzy_text(const char *txt, int txt_len, const char *pattern, int *out_score);
  3628. /* =============================================================================
  3629. *
  3630. * UTF-8
  3631. *
  3632. * ============================================================================= */
  3633. NK_API int nk_utf_decode(const char*, nk_rune*, int);
  3634. NK_API int nk_utf_encode(nk_rune, char*, int);
  3635. NK_API int nk_utf_len(const char*, int byte_len);
  3636. NK_API const char* nk_utf_at(const char *buffer, int length, int index, nk_rune *unicode, int *len);
  3637. /* ===============================================================
  3638. *
  3639. * FONT
  3640. *
  3641. * ===============================================================*/
  3642. /* Font handling in this library was designed to be quite customizable and lets
  3643. you decide what you want to use and what you want to provide. There are three
  3644. different ways to use the font atlas. The first two will use your font
  3645. handling scheme and only requires essential data to run nuklear. The next
  3646. slightly more advanced features is font handling with vertex buffer output.
  3647. Finally the most complex API wise is using nuklear's font baking API.
  3648. 1.) Using your own implementation without vertex buffer output
  3649. --------------------------------------------------------------
  3650. So first up the easiest way to do font handling is by just providing a
  3651. `nk_user_font` struct which only requires the height in pixel of the used
  3652. font and a callback to calculate the width of a string. This way of handling
  3653. fonts is best fitted for using the normal draw shape command API where you
  3654. do all the text drawing yourself and the library does not require any kind
  3655. of deeper knowledge about which font handling mechanism you use.
  3656. IMPORTANT: the `nk_user_font` pointer provided to nuklear has to persist
  3657. over the complete life time! I know this sucks but it is currently the only
  3658. way to switch between fonts.
  3659. float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
  3660. {
  3661. your_font_type *type = handle.ptr;
  3662. float text_width = ...;
  3663. return text_width;
  3664. }
  3665. struct nk_user_font font;
  3666. font.userdata.ptr = &your_font_class_or_struct;
  3667. font.height = your_font_height;
  3668. font.width = your_text_width_calculation;
  3669. struct nk_context ctx;
  3670. nk_init_default(&ctx, &font);
  3671. 2.) Using your own implementation with vertex buffer output
  3672. --------------------------------------------------------------
  3673. While the first approach works fine if you don't want to use the optional
  3674. vertex buffer output it is not enough if you do. To get font handling working
  3675. for these cases you have to provide two additional parameters inside the
  3676. `nk_user_font`. First a texture atlas handle used to draw text as subimages
  3677. of a bigger font atlas texture and a callback to query a character's glyph
  3678. information (offset, size, ...). So it is still possible to provide your own
  3679. font and use the vertex buffer output.
  3680. float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
  3681. {
  3682. your_font_type *type = handle.ptr;
  3683. float text_width = ...;
  3684. return text_width;
  3685. }
  3686. void query_your_font_glyph(nk_handle handle, float font_height, struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
  3687. {
  3688. your_font_type *type = handle.ptr;
  3689. glyph.width = ...;
  3690. glyph.height = ...;
  3691. glyph.xadvance = ...;
  3692. glyph.uv[0].x = ...;
  3693. glyph.uv[0].y = ...;
  3694. glyph.uv[1].x = ...;
  3695. glyph.uv[1].y = ...;
  3696. glyph.offset.x = ...;
  3697. glyph.offset.y = ...;
  3698. }
  3699. struct nk_user_font font;
  3700. font.userdata.ptr = &your_font_class_or_struct;
  3701. font.height = your_font_height;
  3702. font.width = your_text_width_calculation;
  3703. font.query = query_your_font_glyph;
  3704. font.texture.id = your_font_texture;
  3705. struct nk_context ctx;
  3706. nk_init_default(&ctx, &font);
  3707. 3.) Nuklear font baker
  3708. ------------------------------------
  3709. The final approach if you do not have a font handling functionality or don't
  3710. want to use it in this library is by using the optional font baker.
  3711. The font baker APIs can be used to create a font plus font atlas texture
  3712. and can be used with or without the vertex buffer output.
  3713. It still uses the `nk_user_font` struct and the two different approaches
  3714. previously stated still work. The font baker is not located inside
  3715. `nk_context` like all other systems since it can be understood as more of
  3716. an extension to nuklear and does not really depend on any `nk_context` state.
  3717. Font baker need to be initialized first by one of the nk_font_atlas_init_xxx
  3718. functions. If you don't care about memory just call the default version
  3719. `nk_font_atlas_init_default` which will allocate all memory from the standard library.
  3720. If you want to control memory allocation but you don't care if the allocated
  3721. memory is temporary and therefore can be freed directly after the baking process
  3722. is over or permanent you can call `nk_font_atlas_init`.
  3723. After successfully initializing the font baker you can add Truetype(.ttf) fonts from
  3724. different sources like memory or from file by calling one of the `nk_font_atlas_add_xxx`.
  3725. functions. Adding font will permanently store each font, font config and ttf memory block(!)
  3726. inside the font atlas and allows to reuse the font atlas. If you don't want to reuse
  3727. the font baker by for example adding additional fonts you can call
  3728. `nk_font_atlas_cleanup` after the baking process is over (after calling nk_font_atlas_end).
  3729. As soon as you added all fonts you wanted you can now start the baking process
  3730. for every selected glyph to image by calling `nk_font_atlas_bake`.
  3731. The baking process returns image memory, width and height which can be used to
  3732. either create your own image object or upload it to any graphics library.
  3733. No matter which case you finally have to call `nk_font_atlas_end` which
  3734. will free all temporary memory including the font atlas image so make sure
  3735. you created our texture beforehand. `nk_font_atlas_end` requires a handle
  3736. to your font texture or object and optionally fills a `struct nk_draw_null_texture`
  3737. which can be used for the optional vertex output. If you don't want it just
  3738. set the argument to `NULL`.
  3739. At this point you are done and if you don't want to reuse the font atlas you
  3740. can call `nk_font_atlas_cleanup` to free all truetype blobs and configuration
  3741. memory. Finally if you don't use the font atlas and any of it's fonts anymore
  3742. you need to call `nk_font_atlas_clear` to free all memory still being used.
  3743. struct nk_font_atlas atlas;
  3744. nk_font_atlas_init_default(&atlas);
  3745. nk_font_atlas_begin(&atlas);
  3746. nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, 0);
  3747. nk_font *font2 = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font2.ttf", 16, 0);
  3748. const void* img = nk_font_atlas_bake(&atlas, &img_width, &img_height, NK_FONT_ATLAS_RGBA32);
  3749. nk_font_atlas_end(&atlas, nk_handle_id(texture), 0);
  3750. struct nk_context ctx;
  3751. nk_init_default(&ctx, &font->handle);
  3752. while (1) {
  3753. }
  3754. nk_font_atlas_clear(&atlas);
  3755. The font baker API is probably the most complex API inside this library and
  3756. I would suggest reading some of my examples `example/` to get a grip on how
  3757. to use the font atlas. There are a number of details I left out. For example
  3758. how to merge fonts, configure a font with `nk_font_config` to use other languages,
  3759. use another texture coordinate format and a lot more:
  3760. struct nk_font_config cfg = nk_font_config(font_pixel_height);
  3761. cfg.merge_mode = nk_false or nk_true;
  3762. cfg.range = nk_font_korean_glyph_ranges();
  3763. cfg.coord_type = NK_COORD_PIXEL;
  3764. nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, &cfg);
  3765. */
  3766. struct nk_user_font_glyph;
  3767. typedef float(*nk_text_width_f)(nk_handle, float h, const char*, int len);
  3768. typedef void(*nk_query_font_glyph_f)(nk_handle handle, float font_height,
  3769. struct nk_user_font_glyph *glyph,
  3770. nk_rune codepoint, nk_rune next_codepoint);
  3771. #if defined(NK_INCLUDE_VERTEX_BUFFER_OUTPUT) || defined(NK_INCLUDE_SOFTWARE_FONT)
  3772. struct nk_user_font_glyph {
  3773. struct nk_vec2 uv[2];
  3774. /* texture coordinates */
  3775. struct nk_vec2 offset;
  3776. /* offset between top left and glyph */
  3777. float width, height;
  3778. /* size of the glyph */
  3779. float xadvance;
  3780. /* offset to the next glyph */
  3781. };
  3782. #endif
  3783. struct nk_user_font {
  3784. nk_handle userdata;
  3785. /* user provided font handle */
  3786. float height;
  3787. /* max height of the font */
  3788. nk_text_width_f width;
  3789. /* font string width in pixel callback */
  3790. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  3791. nk_query_font_glyph_f query;
  3792. /* font glyph callback to query drawing info */
  3793. nk_handle texture;
  3794. /* texture handle to the used font atlas or texture */
  3795. #endif
  3796. };
  3797. #ifdef NK_INCLUDE_FONT_BAKING
  3798. enum nk_font_coord_type {
  3799. NK_COORD_UV, /* texture coordinates inside font glyphs are clamped between 0-1 */
  3800. NK_COORD_PIXEL /* texture coordinates inside font glyphs are in absolute pixel */
  3801. };
  3802. struct nk_font;
  3803. struct nk_baked_font {
  3804. float height;
  3805. /* height of the font */
  3806. float ascent, descent;
  3807. /* font glyphs ascent and descent */
  3808. nk_rune glyph_offset;
  3809. /* glyph array offset inside the font glyph baking output array */
  3810. nk_rune glyph_count;
  3811. /* number of glyphs of this font inside the glyph baking array output */
  3812. const nk_rune *ranges;
  3813. /* font codepoint ranges as pairs of (from/to) and 0 as last element */
  3814. };
  3815. struct nk_font_config {
  3816. struct nk_font_config *next;
  3817. /* NOTE: only used internally */
  3818. void *ttf_blob;
  3819. /* pointer to loaded TTF file memory block.
  3820. * NOTE: not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
  3821. nk_size ttf_size;
  3822. /* size of the loaded TTF file memory block
  3823. * NOTE: not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
  3824. unsigned char ttf_data_owned_by_atlas;
  3825. /* used inside font atlas: default to: 0*/
  3826. unsigned char merge_mode;
  3827. /* merges this font into the last font */
  3828. unsigned char pixel_snap;
  3829. /* align every character to pixel boundary (if true set oversample (1,1)) */
  3830. unsigned char oversample_v, oversample_h;
  3831. /* rasterize at hight quality for sub-pixel position */
  3832. unsigned char padding[3];
  3833. float size;
  3834. /* baked pixel height of the font */
  3835. enum nk_font_coord_type coord_type;
  3836. /* texture coordinate format with either pixel or UV coordinates */
  3837. struct nk_vec2 spacing;
  3838. /* extra pixel spacing between glyphs */
  3839. const nk_rune *range;
  3840. /* list of unicode ranges (2 values per range, zero terminated) */
  3841. struct nk_baked_font *font;
  3842. /* font to setup in the baking process: NOTE: not needed for font atlas */
  3843. nk_rune fallback_glyph;
  3844. /* fallback glyph to use if a given rune is not found */
  3845. struct nk_font_config *n;
  3846. struct nk_font_config *p;
  3847. };
  3848. struct nk_font_glyph {
  3849. nk_rune codepoint;
  3850. float xadvance;
  3851. float x0, y0, x1, y1, w, h;
  3852. float u0, v0, u1, v1;
  3853. };
  3854. struct nk_font {
  3855. struct nk_font *next;
  3856. struct nk_user_font handle;
  3857. struct nk_baked_font info;
  3858. float scale;
  3859. struct nk_font_glyph *glyphs;
  3860. const struct nk_font_glyph *fallback;
  3861. nk_rune fallback_codepoint;
  3862. nk_handle texture;
  3863. struct nk_font_config *config;
  3864. };
  3865. enum nk_font_atlas_format {
  3866. NK_FONT_ATLAS_ALPHA8,
  3867. NK_FONT_ATLAS_RGBA32
  3868. };
  3869. struct nk_font_atlas {
  3870. void *pixel;
  3871. int tex_width;
  3872. int tex_height;
  3873. struct nk_allocator permanent;
  3874. struct nk_allocator temporary;
  3875. struct nk_recti custom;
  3876. struct nk_cursor cursors[NK_CURSOR_COUNT];
  3877. int glyph_count;
  3878. struct nk_font_glyph *glyphs;
  3879. struct nk_font *default_font;
  3880. struct nk_font *fonts;
  3881. struct nk_font_config *config;
  3882. int font_num;
  3883. };
  3884. /* some language glyph codepoint ranges */
  3885. NK_API const nk_rune *nk_font_default_glyph_ranges(void);
  3886. NK_API const nk_rune *nk_font_chinese_glyph_ranges(void);
  3887. NK_API const nk_rune *nk_font_cyrillic_glyph_ranges(void);
  3888. NK_API const nk_rune *nk_font_korean_glyph_ranges(void);
  3889. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  3890. NK_API void nk_font_atlas_init_default(struct nk_font_atlas*);
  3891. #endif
  3892. NK_API void nk_font_atlas_init(struct nk_font_atlas*, struct nk_allocator*);
  3893. NK_API void nk_font_atlas_init_custom(struct nk_font_atlas*, struct nk_allocator *persistent, struct nk_allocator *transient);
  3894. NK_API void nk_font_atlas_begin(struct nk_font_atlas*);
  3895. NK_API struct nk_font_config nk_font_config(float pixel_height);
  3896. NK_API struct nk_font *nk_font_atlas_add(struct nk_font_atlas*, const struct nk_font_config*);
  3897. #ifdef NK_INCLUDE_DEFAULT_FONT
  3898. NK_API struct nk_font* nk_font_atlas_add_default(struct nk_font_atlas*, float height, const struct nk_font_config*);
  3899. #endif
  3900. 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);
  3901. #ifdef NK_INCLUDE_STANDARD_IO
  3902. 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*);
  3903. #endif
  3904. 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*);
  3905. 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);
  3906. NK_API const void* nk_font_atlas_bake(struct nk_font_atlas*, int *width, int *height, enum nk_font_atlas_format);
  3907. NK_API void nk_font_atlas_end(struct nk_font_atlas*, nk_handle tex, struct nk_draw_null_texture*);
  3908. NK_API const struct nk_font_glyph* nk_font_find_glyph(struct nk_font*, nk_rune unicode);
  3909. NK_API void nk_font_atlas_cleanup(struct nk_font_atlas *atlas);
  3910. NK_API void nk_font_atlas_clear(struct nk_font_atlas*);
  3911. #endif
  3912. /* ==============================================================
  3913. *
  3914. * MEMORY BUFFER
  3915. *
  3916. * ===============================================================*/
  3917. /* A basic (double)-buffer with linear allocation and resetting as only
  3918. freeing policy. The buffer's main purpose is to control all memory management
  3919. inside the GUI toolkit and still leave memory control as much as possible in
  3920. the hand of the user while also making sure the library is easy to use if
  3921. not as much control is needed.
  3922. In general all memory inside this library can be provided from the user in
  3923. three different ways.
  3924. The first way and the one providing most control is by just passing a fixed
  3925. size memory block. In this case all control lies in the hand of the user
  3926. since he can exactly control where the memory comes from and how much memory
  3927. the library should consume. Of course using the fixed size API removes the
  3928. ability to automatically resize a buffer if not enough memory is provided so
  3929. you have to take over the resizing. While being a fixed sized buffer sounds
  3930. quite limiting, it is very effective in this library since the actual memory
  3931. consumption is quite stable and has a fixed upper bound for a lot of cases.
  3932. If you don't want to think about how much memory the library should allocate
  3933. at all time or have a very dynamic UI with unpredictable memory consumption
  3934. habits but still want control over memory allocation you can use the dynamic
  3935. allocator based API. The allocator consists of two callbacks for allocating
  3936. and freeing memory and optional userdata so you can plugin your own allocator.
  3937. The final and easiest way can be used by defining
  3938. NK_INCLUDE_DEFAULT_ALLOCATOR which uses the standard library memory
  3939. allocation functions malloc and free and takes over complete control over
  3940. memory in this library.
  3941. */
  3942. struct nk_memory_status {
  3943. void *memory;
  3944. unsigned int type;
  3945. nk_size size;
  3946. nk_size allocated;
  3947. nk_size needed;
  3948. nk_size calls;
  3949. };
  3950. enum nk_allocation_type {
  3951. NK_BUFFER_FIXED,
  3952. NK_BUFFER_DYNAMIC
  3953. };
  3954. enum nk_buffer_allocation_type {
  3955. NK_BUFFER_FRONT,
  3956. NK_BUFFER_BACK,
  3957. NK_BUFFER_MAX
  3958. };
  3959. struct nk_buffer_marker {
  3960. int active;
  3961. nk_size offset;
  3962. };
  3963. struct nk_memory {void *ptr;nk_size size;};
  3964. struct nk_buffer {
  3965. struct nk_buffer_marker marker[NK_BUFFER_MAX];
  3966. /* buffer marker to free a buffer to a certain offset */
  3967. struct nk_allocator pool;
  3968. /* allocator callback for dynamic buffers */
  3969. enum nk_allocation_type type;
  3970. /* memory management type */
  3971. struct nk_memory memory;
  3972. /* memory and size of the current memory block */
  3973. float grow_factor;
  3974. /* growing factor for dynamic memory management */
  3975. nk_size allocated;
  3976. /* total amount of memory allocated */
  3977. nk_size needed;
  3978. /* totally consumed memory given that enough memory is present */
  3979. nk_size calls;
  3980. /* number of allocation calls */
  3981. nk_size size;
  3982. /* current size of the buffer */
  3983. };
  3984. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  3985. NK_API void nk_buffer_init_default(struct nk_buffer*);
  3986. #endif
  3987. NK_API void nk_buffer_init(struct nk_buffer*, const struct nk_allocator*, nk_size size);
  3988. NK_API void nk_buffer_init_fixed(struct nk_buffer*, void *memory, nk_size size);
  3989. NK_API void nk_buffer_info(struct nk_memory_status*, struct nk_buffer*);
  3990. NK_API void nk_buffer_push(struct nk_buffer*, enum nk_buffer_allocation_type type, const void *memory, nk_size size, nk_size align);
  3991. NK_API void nk_buffer_mark(struct nk_buffer*, enum nk_buffer_allocation_type type);
  3992. NK_API void nk_buffer_reset(struct nk_buffer*, enum nk_buffer_allocation_type type);
  3993. NK_API void nk_buffer_clear(struct nk_buffer*);
  3994. NK_API void nk_buffer_free(struct nk_buffer*);
  3995. NK_API void *nk_buffer_memory(struct nk_buffer*);
  3996. NK_API const void *nk_buffer_memory_const(const struct nk_buffer*);
  3997. NK_API nk_size nk_buffer_total(struct nk_buffer*);
  3998. /* ==============================================================
  3999. *
  4000. * STRING
  4001. *
  4002. * ===============================================================*/
  4003. /* Basic string buffer which is only used in context with the text editor
  4004. * to manage and manipulate dynamic or fixed size string content. This is _NOT_
  4005. * the default string handling method. The only instance you should have any contact
  4006. * with this API is if you interact with an `nk_text_edit` object inside one of the
  4007. * copy and paste functions and even there only for more advanced cases. */
  4008. struct nk_str {
  4009. struct nk_buffer buffer;
  4010. int len; /* in codepoints/runes/glyphs */
  4011. };
  4012. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  4013. NK_API void nk_str_init_default(struct nk_str*);
  4014. #endif
  4015. NK_API void nk_str_init(struct nk_str*, const struct nk_allocator*, nk_size size);
  4016. NK_API void nk_str_init_fixed(struct nk_str*, void *memory, nk_size size);
  4017. NK_API void nk_str_clear(struct nk_str*);
  4018. NK_API void nk_str_free(struct nk_str*);
  4019. NK_API int nk_str_append_text_char(struct nk_str*, const char*, int);
  4020. NK_API int nk_str_append_str_char(struct nk_str*, const char*);
  4021. NK_API int nk_str_append_text_utf8(struct nk_str*, const char*, int);
  4022. NK_API int nk_str_append_str_utf8(struct nk_str*, const char*);
  4023. NK_API int nk_str_append_text_runes(struct nk_str*, const nk_rune*, int);
  4024. NK_API int nk_str_append_str_runes(struct nk_str*, const nk_rune*);
  4025. NK_API int nk_str_insert_at_char(struct nk_str*, int pos, const char*, int);
  4026. NK_API int nk_str_insert_at_rune(struct nk_str*, int pos, const char*, int);
  4027. NK_API int nk_str_insert_text_char(struct nk_str*, int pos, const char*, int);
  4028. NK_API int nk_str_insert_str_char(struct nk_str*, int pos, const char*);
  4029. NK_API int nk_str_insert_text_utf8(struct nk_str*, int pos, const char*, int);
  4030. NK_API int nk_str_insert_str_utf8(struct nk_str*, int pos, const char*);
  4031. NK_API int nk_str_insert_text_runes(struct nk_str*, int pos, const nk_rune*, int);
  4032. NK_API int nk_str_insert_str_runes(struct nk_str*, int pos, const nk_rune*);
  4033. NK_API void nk_str_remove_chars(struct nk_str*, int len);
  4034. NK_API void nk_str_remove_runes(struct nk_str *str, int len);
  4035. NK_API void nk_str_delete_chars(struct nk_str*, int pos, int len);
  4036. NK_API void nk_str_delete_runes(struct nk_str*, int pos, int len);
  4037. NK_API char *nk_str_at_char(struct nk_str*, int pos);
  4038. NK_API char *nk_str_at_rune(struct nk_str*, int pos, nk_rune *unicode, int *len);
  4039. NK_API nk_rune nk_str_rune_at(const struct nk_str*, int pos);
  4040. NK_API const char *nk_str_at_char_const(const struct nk_str*, int pos);
  4041. NK_API const char *nk_str_at_const(const struct nk_str*, int pos, nk_rune *unicode, int *len);
  4042. NK_API char *nk_str_get(struct nk_str*);
  4043. NK_API const char *nk_str_get_const(const struct nk_str*);
  4044. NK_API int nk_str_len(struct nk_str*);
  4045. NK_API int nk_str_len_char(struct nk_str*);
  4046. /*===============================================================
  4047. *
  4048. * TEXT EDITOR
  4049. *
  4050. * ===============================================================*/
  4051. /* Editing text in this library is handled by either `nk_edit_string` or
  4052. * `nk_edit_buffer`. But like almost everything in this library there are multiple
  4053. * ways of doing it and a balance between control and ease of use with memory
  4054. * as well as functionality controlled by flags.
  4055. *
  4056. * This library generally allows three different levels of memory control:
  4057. * First of is the most basic way of just providing a simple char array with
  4058. * string length. This method is probably the easiest way of handling simple
  4059. * user text input. Main upside is complete control over memory while the biggest
  4060. * downside in comparison with the other two approaches is missing undo/redo.
  4061. *
  4062. * For UIs that require undo/redo the second way was created. It is based on
  4063. * a fixed size nk_text_edit struct, which has an internal undo/redo stack.
  4064. * This is mainly useful if you want something more like a text editor but don't want
  4065. * to have a dynamically growing buffer.
  4066. *
  4067. * The final way is using a dynamically growing nk_text_edit struct, which
  4068. * has both a default version if you don't care where memory comes from and an
  4069. * allocator version if you do. While the text editor is quite powerful for its
  4070. * complexity I would not recommend editing gigabytes of data with it.
  4071. * It is rather designed for uses cases which make sense for a GUI library not for
  4072. * an full blown text editor.
  4073. */
  4074. #ifndef NK_TEXTEDIT_UNDOSTATECOUNT
  4075. #define NK_TEXTEDIT_UNDOSTATECOUNT 99
  4076. #endif
  4077. #ifndef NK_TEXTEDIT_UNDOCHARCOUNT
  4078. #define NK_TEXTEDIT_UNDOCHARCOUNT 999
  4079. #endif
  4080. struct nk_text_edit;
  4081. struct nk_clipboard {
  4082. nk_handle userdata;
  4083. nk_plugin_paste paste;
  4084. nk_plugin_copy copy;
  4085. };
  4086. struct nk_text_undo_record {
  4087. int where;
  4088. short insert_length;
  4089. short delete_length;
  4090. short char_storage;
  4091. };
  4092. struct nk_text_undo_state {
  4093. struct nk_text_undo_record undo_rec[NK_TEXTEDIT_UNDOSTATECOUNT];
  4094. nk_rune undo_char[NK_TEXTEDIT_UNDOCHARCOUNT];
  4095. short undo_point;
  4096. short redo_point;
  4097. short undo_char_point;
  4098. short redo_char_point;
  4099. };
  4100. enum nk_text_edit_type {
  4101. NK_TEXT_EDIT_SINGLE_LINE,
  4102. NK_TEXT_EDIT_MULTI_LINE
  4103. };
  4104. enum nk_text_edit_mode {
  4105. NK_TEXT_EDIT_MODE_VIEW,
  4106. NK_TEXT_EDIT_MODE_INSERT,
  4107. NK_TEXT_EDIT_MODE_REPLACE
  4108. };
  4109. struct nk_text_edit {
  4110. struct nk_clipboard clip;
  4111. struct nk_str string;
  4112. nk_plugin_filter filter;
  4113. struct nk_vec2 scrollbar;
  4114. int cursor;
  4115. int select_start;
  4116. int select_end;
  4117. unsigned char mode;
  4118. unsigned char cursor_at_end_of_line;
  4119. unsigned char initialized;
  4120. unsigned char has_preferred_x;
  4121. unsigned char single_line;
  4122. unsigned char active;
  4123. unsigned char padding1;
  4124. float preferred_x;
  4125. struct nk_text_undo_state undo;
  4126. };
  4127. /* filter function */
  4128. NK_API int nk_filter_default(const struct nk_text_edit*, nk_rune unicode);
  4129. NK_API int nk_filter_ascii(const struct nk_text_edit*, nk_rune unicode);
  4130. NK_API int nk_filter_float(const struct nk_text_edit*, nk_rune unicode);
  4131. NK_API int nk_filter_decimal(const struct nk_text_edit*, nk_rune unicode);
  4132. NK_API int nk_filter_hex(const struct nk_text_edit*, nk_rune unicode);
  4133. NK_API int nk_filter_oct(const struct nk_text_edit*, nk_rune unicode);
  4134. NK_API int nk_filter_binary(const struct nk_text_edit*, nk_rune unicode);
  4135. /* text editor */
  4136. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  4137. NK_API void nk_textedit_init_default(struct nk_text_edit*);
  4138. #endif
  4139. NK_API void nk_textedit_init(struct nk_text_edit*, struct nk_allocator*, nk_size size);
  4140. NK_API void nk_textedit_init_fixed(struct nk_text_edit*, void *memory, nk_size size);
  4141. NK_API void nk_textedit_free(struct nk_text_edit*);
  4142. NK_API void nk_textedit_text(struct nk_text_edit*, const char*, int total_len);
  4143. NK_API void nk_textedit_delete(struct nk_text_edit*, int where, int len);
  4144. NK_API void nk_textedit_delete_selection(struct nk_text_edit*);
  4145. NK_API void nk_textedit_select_all(struct nk_text_edit*);
  4146. NK_API int nk_textedit_cut(struct nk_text_edit*);
  4147. NK_API int nk_textedit_paste(struct nk_text_edit*, char const*, int len);
  4148. NK_API void nk_textedit_undo(struct nk_text_edit*);
  4149. NK_API void nk_textedit_redo(struct nk_text_edit*);
  4150. /* ===============================================================
  4151. *
  4152. * DRAWING
  4153. *
  4154. * ===============================================================*/
  4155. /* This library was designed to be render backend agnostic so it does
  4156. not draw anything to screen. Instead all drawn shapes, widgets
  4157. are made of, are buffered into memory and make up a command queue.
  4158. Each frame therefore fills the command buffer with draw commands
  4159. that then need to be executed by the user and his own render backend.
  4160. After that the command buffer needs to be cleared and a new frame can be
  4161. started. It is probably important to note that the command buffer is the main
  4162. drawing API and the optional vertex buffer API only takes this format and
  4163. converts it into a hardware accessible format.
  4164. To use the command queue to draw your own widgets you can access the
  4165. command buffer of each window by calling `nk_window_get_canvas` after
  4166. previously having called `nk_begin`:
  4167. void draw_red_rectangle_widget(struct nk_context *ctx)
  4168. {
  4169. struct nk_command_buffer *canvas;
  4170. struct nk_input *input = &ctx->input;
  4171. canvas = nk_window_get_canvas(ctx);
  4172. struct nk_rect space;
  4173. enum nk_widget_layout_states state;
  4174. state = nk_widget(&space, ctx);
  4175. if (!state) return;
  4176. if (state != NK_WIDGET_ROM)
  4177. update_your_widget_by_user_input(...);
  4178. nk_fill_rect(canvas, space, 0, nk_rgb(255,0,0));
  4179. }
  4180. if (nk_begin(...)) {
  4181. nk_layout_row_dynamic(ctx, 25, 1);
  4182. draw_red_rectangle_widget(ctx);
  4183. }
  4184. nk_end(..)
  4185. Important to know if you want to create your own widgets is the `nk_widget`
  4186. call. It allocates space on the panel reserved for this widget to be used,
  4187. but also returns the state of the widget space. If your widget is not seen and does
  4188. not have to be updated it is '0' and you can just return. If it only has
  4189. to be drawn the state will be `NK_WIDGET_ROM` otherwise you can do both
  4190. update and draw your widget. The reason for separating is to only draw and
  4191. update what is actually necessary which is crucial for performance.
  4192. */
  4193. enum nk_command_type {
  4194. NK_COMMAND_NOP,
  4195. NK_COMMAND_SCISSOR,
  4196. NK_COMMAND_LINE,
  4197. NK_COMMAND_CURVE,
  4198. NK_COMMAND_RECT,
  4199. NK_COMMAND_RECT_FILLED,
  4200. NK_COMMAND_RECT_MULTI_COLOR,
  4201. NK_COMMAND_CIRCLE,
  4202. NK_COMMAND_CIRCLE_FILLED,
  4203. NK_COMMAND_ARC,
  4204. NK_COMMAND_ARC_FILLED,
  4205. NK_COMMAND_TRIANGLE,
  4206. NK_COMMAND_TRIANGLE_FILLED,
  4207. NK_COMMAND_POLYGON,
  4208. NK_COMMAND_POLYGON_FILLED,
  4209. NK_COMMAND_POLYLINE,
  4210. NK_COMMAND_TEXT,
  4211. NK_COMMAND_IMAGE,
  4212. NK_COMMAND_CUSTOM
  4213. };
  4214. /* command base and header of every command inside the buffer */
  4215. struct nk_command {
  4216. enum nk_command_type type;
  4217. nk_size next;
  4218. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4219. nk_handle userdata;
  4220. #endif
  4221. };
  4222. struct nk_command_scissor {
  4223. struct nk_command header;
  4224. short x, y;
  4225. unsigned short w, h;
  4226. };
  4227. struct nk_command_line {
  4228. struct nk_command header;
  4229. unsigned short line_thickness;
  4230. struct nk_vec2i begin;
  4231. struct nk_vec2i end;
  4232. struct nk_color color;
  4233. };
  4234. struct nk_command_curve {
  4235. struct nk_command header;
  4236. unsigned short line_thickness;
  4237. struct nk_vec2i begin;
  4238. struct nk_vec2i end;
  4239. struct nk_vec2i ctrl[2];
  4240. struct nk_color color;
  4241. };
  4242. struct nk_command_rect {
  4243. struct nk_command header;
  4244. unsigned short rounding;
  4245. unsigned short line_thickness;
  4246. short x, y;
  4247. unsigned short w, h;
  4248. struct nk_color color;
  4249. };
  4250. struct nk_command_rect_filled {
  4251. struct nk_command header;
  4252. unsigned short rounding;
  4253. short x, y;
  4254. unsigned short w, h;
  4255. struct nk_color color;
  4256. };
  4257. struct nk_command_rect_multi_color {
  4258. struct nk_command header;
  4259. short x, y;
  4260. unsigned short w, h;
  4261. struct nk_color left;
  4262. struct nk_color top;
  4263. struct nk_color bottom;
  4264. struct nk_color right;
  4265. };
  4266. struct nk_command_triangle {
  4267. struct nk_command header;
  4268. unsigned short line_thickness;
  4269. struct nk_vec2i a;
  4270. struct nk_vec2i b;
  4271. struct nk_vec2i c;
  4272. struct nk_color color;
  4273. };
  4274. struct nk_command_triangle_filled {
  4275. struct nk_command header;
  4276. struct nk_vec2i a;
  4277. struct nk_vec2i b;
  4278. struct nk_vec2i c;
  4279. struct nk_color color;
  4280. };
  4281. struct nk_command_circle {
  4282. struct nk_command header;
  4283. short x, y;
  4284. unsigned short line_thickness;
  4285. unsigned short w, h;
  4286. struct nk_color color;
  4287. };
  4288. struct nk_command_circle_filled {
  4289. struct nk_command header;
  4290. short x, y;
  4291. unsigned short w, h;
  4292. struct nk_color color;
  4293. };
  4294. struct nk_command_arc {
  4295. struct nk_command header;
  4296. short cx, cy;
  4297. unsigned short r;
  4298. unsigned short line_thickness;
  4299. float a[2];
  4300. struct nk_color color;
  4301. };
  4302. struct nk_command_arc_filled {
  4303. struct nk_command header;
  4304. short cx, cy;
  4305. unsigned short r;
  4306. float a[2];
  4307. struct nk_color color;
  4308. };
  4309. struct nk_command_polygon {
  4310. struct nk_command header;
  4311. struct nk_color color;
  4312. unsigned short line_thickness;
  4313. unsigned short point_count;
  4314. struct nk_vec2i points[1];
  4315. };
  4316. struct nk_command_polygon_filled {
  4317. struct nk_command header;
  4318. struct nk_color color;
  4319. unsigned short point_count;
  4320. struct nk_vec2i points[1];
  4321. };
  4322. struct nk_command_polyline {
  4323. struct nk_command header;
  4324. struct nk_color color;
  4325. unsigned short line_thickness;
  4326. unsigned short point_count;
  4327. struct nk_vec2i points[1];
  4328. };
  4329. struct nk_command_image {
  4330. struct nk_command header;
  4331. short x, y;
  4332. unsigned short w, h;
  4333. struct nk_image img;
  4334. struct nk_color col;
  4335. };
  4336. typedef void (*nk_command_custom_callback)(void *canvas, short x,short y,
  4337. unsigned short w, unsigned short h, nk_handle callback_data);
  4338. struct nk_command_custom {
  4339. struct nk_command header;
  4340. short x, y;
  4341. unsigned short w, h;
  4342. nk_handle callback_data;
  4343. nk_command_custom_callback callback;
  4344. };
  4345. struct nk_command_text {
  4346. struct nk_command header;
  4347. const struct nk_user_font *font;
  4348. struct nk_color background;
  4349. struct nk_color foreground;
  4350. short x, y;
  4351. unsigned short w, h;
  4352. float height;
  4353. int length;
  4354. char string[1];
  4355. };
  4356. enum nk_command_clipping {
  4357. NK_CLIPPING_OFF = nk_false,
  4358. NK_CLIPPING_ON = nk_true
  4359. };
  4360. struct nk_command_buffer {
  4361. struct nk_buffer *base;
  4362. struct nk_rect clip;
  4363. int use_clipping;
  4364. nk_handle userdata;
  4365. nk_size begin, end, last;
  4366. };
  4367. /* shape outlines */
  4368. 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);
  4369. NK_API void nk_stroke_curve(struct nk_command_buffer*, float, float, float, float, float, float, float, float, float line_thickness, struct nk_color);
  4370. NK_API void nk_stroke_rect(struct nk_command_buffer*, struct nk_rect, float rounding, float line_thickness, struct nk_color);
  4371. NK_API void nk_stroke_circle(struct nk_command_buffer*, struct nk_rect, float line_thickness, struct nk_color);
  4372. 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);
  4373. NK_API void nk_stroke_triangle(struct nk_command_buffer*, float, float, float, float, float, float, float line_thichness, struct nk_color);
  4374. NK_API void nk_stroke_polyline(struct nk_command_buffer*, float *points, int point_count, float line_thickness, struct nk_color col);
  4375. NK_API void nk_stroke_polygon(struct nk_command_buffer*, float*, int point_count, float line_thickness, struct nk_color);
  4376. /* filled shades */
  4377. NK_API void nk_fill_rect(struct nk_command_buffer*, struct nk_rect, float rounding, struct nk_color);
  4378. 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);
  4379. NK_API void nk_fill_circle(struct nk_command_buffer*, struct nk_rect, struct nk_color);
  4380. 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);
  4381. 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);
  4382. NK_API void nk_fill_polygon(struct nk_command_buffer*, float*, int point_count, struct nk_color);
  4383. /* misc */
  4384. NK_API void nk_draw_image(struct nk_command_buffer*, struct nk_rect, const struct nk_image*, struct nk_color);
  4385. 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);
  4386. NK_API void nk_push_scissor(struct nk_command_buffer*, struct nk_rect);
  4387. NK_API void nk_push_custom(struct nk_command_buffer*, struct nk_rect, nk_command_custom_callback, nk_handle usr);
  4388. /* ===============================================================
  4389. *
  4390. * INPUT
  4391. *
  4392. * ===============================================================*/
  4393. struct nk_mouse_button {
  4394. int down;
  4395. unsigned int clicked;
  4396. struct nk_vec2 clicked_pos;
  4397. };
  4398. struct nk_mouse {
  4399. struct nk_mouse_button buttons[NK_BUTTON_MAX];
  4400. struct nk_vec2 pos;
  4401. struct nk_vec2 prev;
  4402. struct nk_vec2 delta;
  4403. struct nk_vec2 scroll_delta;
  4404. unsigned char grab;
  4405. unsigned char grabbed;
  4406. unsigned char ungrab;
  4407. };
  4408. struct nk_key {
  4409. int down;
  4410. unsigned int clicked;
  4411. };
  4412. struct nk_keyboard {
  4413. struct nk_key keys[NK_KEY_MAX];
  4414. char text[NK_INPUT_MAX];
  4415. int text_len;
  4416. };
  4417. struct nk_input {
  4418. struct nk_keyboard keyboard;
  4419. struct nk_mouse mouse;
  4420. };
  4421. NK_API int nk_input_has_mouse_click(const struct nk_input*, enum nk_buttons);
  4422. NK_API int nk_input_has_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4423. NK_API int nk_input_has_mouse_click_down_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect, int down);
  4424. NK_API int nk_input_is_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4425. 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);
  4426. NK_API int nk_input_any_mouse_click_in_rect(const struct nk_input*, struct nk_rect);
  4427. NK_API int nk_input_is_mouse_prev_hovering_rect(const struct nk_input*, struct nk_rect);
  4428. NK_API int nk_input_is_mouse_hovering_rect(const struct nk_input*, struct nk_rect);
  4429. NK_API int nk_input_mouse_clicked(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4430. NK_API int nk_input_is_mouse_down(const struct nk_input*, enum nk_buttons);
  4431. NK_API int nk_input_is_mouse_pressed(const struct nk_input*, enum nk_buttons);
  4432. NK_API int nk_input_is_mouse_released(const struct nk_input*, enum nk_buttons);
  4433. NK_API int nk_input_is_key_pressed(const struct nk_input*, enum nk_keys);
  4434. NK_API int nk_input_is_key_released(const struct nk_input*, enum nk_keys);
  4435. NK_API int nk_input_is_key_down(const struct nk_input*, enum nk_keys);
  4436. /* ===============================================================
  4437. *
  4438. * DRAW LIST
  4439. *
  4440. * ===============================================================*/
  4441. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  4442. /* The optional vertex buffer draw list provides a 2D drawing context
  4443. with antialiasing functionality which takes basic filled or outlined shapes
  4444. or a path and outputs vertexes, elements and draw commands.
  4445. The actual draw list API is not required to be used directly while using this
  4446. library since converting the default library draw command output is done by
  4447. just calling `nk_convert` but I decided to still make this library accessible
  4448. since it can be useful.
  4449. The draw list is based on a path buffering and polygon and polyline
  4450. rendering API which allows a lot of ways to draw 2D content to screen.
  4451. In fact it is probably more powerful than needed but allows even more crazy
  4452. things than this library provides by default.
  4453. */
  4454. typedef nk_ushort nk_draw_index;
  4455. enum nk_draw_list_stroke {
  4456. NK_STROKE_OPEN = nk_false,
  4457. /* build up path has no connection back to the beginning */
  4458. NK_STROKE_CLOSED = nk_true
  4459. /* build up path has a connection back to the beginning */
  4460. };
  4461. enum nk_draw_vertex_layout_attribute {
  4462. NK_VERTEX_POSITION,
  4463. NK_VERTEX_COLOR,
  4464. NK_VERTEX_TEXCOORD,
  4465. NK_VERTEX_ATTRIBUTE_COUNT
  4466. };
  4467. enum nk_draw_vertex_layout_format {
  4468. NK_FORMAT_SCHAR,
  4469. NK_FORMAT_SSHORT,
  4470. NK_FORMAT_SINT,
  4471. NK_FORMAT_UCHAR,
  4472. NK_FORMAT_USHORT,
  4473. NK_FORMAT_UINT,
  4474. NK_FORMAT_FLOAT,
  4475. NK_FORMAT_DOUBLE,
  4476. NK_FORMAT_COLOR_BEGIN,
  4477. NK_FORMAT_R8G8B8 = NK_FORMAT_COLOR_BEGIN,
  4478. NK_FORMAT_R16G15B16,
  4479. NK_FORMAT_R32G32B32,
  4480. NK_FORMAT_R8G8B8A8,
  4481. NK_FORMAT_B8G8R8A8,
  4482. NK_FORMAT_R16G15B16A16,
  4483. NK_FORMAT_R32G32B32A32,
  4484. NK_FORMAT_R32G32B32A32_FLOAT,
  4485. NK_FORMAT_R32G32B32A32_DOUBLE,
  4486. NK_FORMAT_RGB32,
  4487. NK_FORMAT_RGBA32,
  4488. NK_FORMAT_COLOR_END = NK_FORMAT_RGBA32,
  4489. NK_FORMAT_COUNT
  4490. };
  4491. #define NK_VERTEX_LAYOUT_END NK_VERTEX_ATTRIBUTE_COUNT,NK_FORMAT_COUNT,0
  4492. struct nk_draw_vertex_layout_element {
  4493. enum nk_draw_vertex_layout_attribute attribute;
  4494. enum nk_draw_vertex_layout_format format;
  4495. nk_size offset;
  4496. };
  4497. struct nk_draw_command {
  4498. unsigned int elem_count;
  4499. /* number of elements in the current draw batch */
  4500. struct nk_rect clip_rect;
  4501. /* current screen clipping rectangle */
  4502. nk_handle texture;
  4503. /* current texture to set */
  4504. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4505. nk_handle userdata;
  4506. #endif
  4507. };
  4508. struct nk_draw_list {
  4509. struct nk_rect clip_rect;
  4510. struct nk_vec2 circle_vtx[12];
  4511. struct nk_convert_config config;
  4512. struct nk_buffer *buffer;
  4513. struct nk_buffer *vertices;
  4514. struct nk_buffer *elements;
  4515. unsigned int element_count;
  4516. unsigned int vertex_count;
  4517. unsigned int cmd_count;
  4518. nk_size cmd_offset;
  4519. unsigned int path_count;
  4520. unsigned int path_offset;
  4521. enum nk_anti_aliasing line_AA;
  4522. enum nk_anti_aliasing shape_AA;
  4523. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4524. nk_handle userdata;
  4525. #endif
  4526. };
  4527. /* draw list */
  4528. NK_API void nk_draw_list_init(struct nk_draw_list*);
  4529. NK_API void nk_draw_list_setup(struct nk_draw_list*, const struct nk_convert_config*, struct nk_buffer *cmds, struct nk_buffer *vertices, struct nk_buffer *elements, enum nk_anti_aliasing line_aa,enum nk_anti_aliasing shape_aa);
  4530. /* drawing */
  4531. #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))
  4532. NK_API const struct nk_draw_command* nk__draw_list_begin(const struct nk_draw_list*, const struct nk_buffer*);
  4533. 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*);
  4534. NK_API const struct nk_draw_command* nk__draw_list_end(const struct nk_draw_list*, const struct nk_buffer*);
  4535. /* path */
  4536. NK_API void nk_draw_list_path_clear(struct nk_draw_list*);
  4537. NK_API void nk_draw_list_path_line_to(struct nk_draw_list*, struct nk_vec2 pos);
  4538. 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);
  4539. 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);
  4540. NK_API void nk_draw_list_path_rect_to(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, float rounding);
  4541. 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);
  4542. NK_API void nk_draw_list_path_fill(struct nk_draw_list*, struct nk_color);
  4543. NK_API void nk_draw_list_path_stroke(struct nk_draw_list*, struct nk_color, enum nk_draw_list_stroke closed, float thickness);
  4544. /* stroke */
  4545. 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);
  4546. NK_API void nk_draw_list_stroke_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding, float thickness);
  4547. 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);
  4548. 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);
  4549. 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);
  4550. 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);
  4551. /* fill */
  4552. NK_API void nk_draw_list_fill_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding);
  4553. NK_API void nk_draw_list_fill_rect_multi_color(struct nk_draw_list*, struct nk_rect rect, struct nk_color left, struct nk_color top, struct nk_color right, struct nk_color bottom);
  4554. 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);
  4555. 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);
  4556. 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);
  4557. /* misc */
  4558. NK_API void nk_draw_list_add_image(struct nk_draw_list*, struct nk_image texture, struct nk_rect rect, struct nk_color);
  4559. 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);
  4560. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4561. NK_API void nk_draw_list_push_userdata(struct nk_draw_list*, nk_handle userdata);
  4562. #endif
  4563. #endif
  4564. /* ===============================================================
  4565. *
  4566. * GUI
  4567. *
  4568. * ===============================================================*/
  4569. enum nk_style_item_type {
  4570. NK_STYLE_ITEM_COLOR,
  4571. NK_STYLE_ITEM_IMAGE
  4572. };
  4573. union nk_style_item_data {
  4574. struct nk_image image;
  4575. struct nk_color color;
  4576. };
  4577. struct nk_style_item {
  4578. enum nk_style_item_type type;
  4579. union nk_style_item_data data;
  4580. };
  4581. struct nk_style_text {
  4582. struct nk_color color;
  4583. struct nk_vec2 padding;
  4584. };
  4585. struct nk_style_button {
  4586. /* background */
  4587. struct nk_style_item normal;
  4588. struct nk_style_item hover;
  4589. struct nk_style_item active;
  4590. struct nk_color border_color;
  4591. /* text */
  4592. struct nk_color text_background;
  4593. struct nk_color text_normal;
  4594. struct nk_color text_hover;
  4595. struct nk_color text_active;
  4596. nk_flags text_alignment;
  4597. /* properties */
  4598. float border;
  4599. float rounding;
  4600. struct nk_vec2 padding;
  4601. struct nk_vec2 image_padding;
  4602. struct nk_vec2 touch_padding;
  4603. /* optional user callbacks */
  4604. nk_handle userdata;
  4605. void(*draw_begin)(struct nk_command_buffer*, nk_handle userdata);
  4606. void(*draw_end)(struct nk_command_buffer*, nk_handle userdata);
  4607. };
  4608. struct nk_style_toggle {
  4609. /* background */
  4610. struct nk_style_item normal;
  4611. struct nk_style_item hover;
  4612. struct nk_style_item active;
  4613. struct nk_color border_color;
  4614. /* cursor */
  4615. struct nk_style_item cursor_normal;
  4616. struct nk_style_item cursor_hover;
  4617. /* text */
  4618. struct nk_color text_normal;
  4619. struct nk_color text_hover;
  4620. struct nk_color text_active;
  4621. struct nk_color text_background;
  4622. nk_flags text_alignment;
  4623. /* properties */
  4624. struct nk_vec2 padding;
  4625. struct nk_vec2 touch_padding;
  4626. float spacing;
  4627. float border;
  4628. /* optional user callbacks */
  4629. nk_handle userdata;
  4630. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4631. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4632. };
  4633. struct nk_style_selectable {
  4634. /* background (inactive) */
  4635. struct nk_style_item normal;
  4636. struct nk_style_item hover;
  4637. struct nk_style_item pressed;
  4638. /* background (active) */
  4639. struct nk_style_item normal_active;
  4640. struct nk_style_item hover_active;
  4641. struct nk_style_item pressed_active;
  4642. /* text color (inactive) */
  4643. struct nk_color text_normal;
  4644. struct nk_color text_hover;
  4645. struct nk_color text_pressed;
  4646. /* text color (active) */
  4647. struct nk_color text_normal_active;
  4648. struct nk_color text_hover_active;
  4649. struct nk_color text_pressed_active;
  4650. struct nk_color text_background;
  4651. nk_flags text_alignment;
  4652. /* properties */
  4653. float rounding;
  4654. struct nk_vec2 padding;
  4655. struct nk_vec2 touch_padding;
  4656. struct nk_vec2 image_padding;
  4657. /* optional user callbacks */
  4658. nk_handle userdata;
  4659. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4660. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4661. };
  4662. struct nk_style_slider {
  4663. /* background */
  4664. struct nk_style_item normal;
  4665. struct nk_style_item hover;
  4666. struct nk_style_item active;
  4667. struct nk_color border_color;
  4668. /* background bar */
  4669. struct nk_color bar_normal;
  4670. struct nk_color bar_hover;
  4671. struct nk_color bar_active;
  4672. struct nk_color bar_filled;
  4673. /* cursor */
  4674. struct nk_style_item cursor_normal;
  4675. struct nk_style_item cursor_hover;
  4676. struct nk_style_item cursor_active;
  4677. /* properties */
  4678. float border;
  4679. float rounding;
  4680. float bar_height;
  4681. struct nk_vec2 padding;
  4682. struct nk_vec2 spacing;
  4683. struct nk_vec2 cursor_size;
  4684. /* optional buttons */
  4685. int show_buttons;
  4686. struct nk_style_button inc_button;
  4687. struct nk_style_button dec_button;
  4688. enum nk_symbol_type inc_symbol;
  4689. enum nk_symbol_type dec_symbol;
  4690. /* optional user callbacks */
  4691. nk_handle userdata;
  4692. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4693. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4694. };
  4695. struct nk_style_progress {
  4696. /* background */
  4697. struct nk_style_item normal;
  4698. struct nk_style_item hover;
  4699. struct nk_style_item active;
  4700. struct nk_color border_color;
  4701. /* cursor */
  4702. struct nk_style_item cursor_normal;
  4703. struct nk_style_item cursor_hover;
  4704. struct nk_style_item cursor_active;
  4705. struct nk_color cursor_border_color;
  4706. /* properties */
  4707. float rounding;
  4708. float border;
  4709. float cursor_border;
  4710. float cursor_rounding;
  4711. struct nk_vec2 padding;
  4712. /* optional user callbacks */
  4713. nk_handle userdata;
  4714. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4715. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4716. };
  4717. struct nk_style_scrollbar {
  4718. /* background */
  4719. struct nk_style_item normal;
  4720. struct nk_style_item hover;
  4721. struct nk_style_item active;
  4722. struct nk_color border_color;
  4723. /* cursor */
  4724. struct nk_style_item cursor_normal;
  4725. struct nk_style_item cursor_hover;
  4726. struct nk_style_item cursor_active;
  4727. struct nk_color cursor_border_color;
  4728. /* properties */
  4729. float border;
  4730. float rounding;
  4731. float border_cursor;
  4732. float rounding_cursor;
  4733. struct nk_vec2 padding;
  4734. /* optional buttons */
  4735. int show_buttons;
  4736. struct nk_style_button inc_button;
  4737. struct nk_style_button dec_button;
  4738. enum nk_symbol_type inc_symbol;
  4739. enum nk_symbol_type dec_symbol;
  4740. /* optional user callbacks */
  4741. nk_handle userdata;
  4742. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4743. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4744. };
  4745. struct nk_style_edit {
  4746. /* background */
  4747. struct nk_style_item normal;
  4748. struct nk_style_item hover;
  4749. struct nk_style_item active;
  4750. struct nk_color border_color;
  4751. struct nk_style_scrollbar scrollbar;
  4752. /* cursor */
  4753. struct nk_color cursor_normal;
  4754. struct nk_color cursor_hover;
  4755. struct nk_color cursor_text_normal;
  4756. struct nk_color cursor_text_hover;
  4757. /* text (unselected) */
  4758. struct nk_color text_normal;
  4759. struct nk_color text_hover;
  4760. struct nk_color text_active;
  4761. /* text (selected) */
  4762. struct nk_color selected_normal;
  4763. struct nk_color selected_hover;
  4764. struct nk_color selected_text_normal;
  4765. struct nk_color selected_text_hover;
  4766. /* properties */
  4767. float border;
  4768. float rounding;
  4769. float cursor_size;
  4770. struct nk_vec2 scrollbar_size;
  4771. struct nk_vec2 padding;
  4772. float row_padding;
  4773. };
  4774. struct nk_style_property {
  4775. /* background */
  4776. struct nk_style_item normal;
  4777. struct nk_style_item hover;
  4778. struct nk_style_item active;
  4779. struct nk_color border_color;
  4780. /* text */
  4781. struct nk_color label_normal;
  4782. struct nk_color label_hover;
  4783. struct nk_color label_active;
  4784. /* symbols */
  4785. enum nk_symbol_type sym_left;
  4786. enum nk_symbol_type sym_right;
  4787. /* properties */
  4788. float border;
  4789. float rounding;
  4790. struct nk_vec2 padding;
  4791. struct nk_style_edit edit;
  4792. struct nk_style_button inc_button;
  4793. struct nk_style_button dec_button;
  4794. /* optional user callbacks */
  4795. nk_handle userdata;
  4796. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4797. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4798. };
  4799. struct nk_style_chart {
  4800. /* colors */
  4801. struct nk_style_item background;
  4802. struct nk_color border_color;
  4803. struct nk_color selected_color;
  4804. struct nk_color color;
  4805. /* properties */
  4806. float border;
  4807. float rounding;
  4808. struct nk_vec2 padding;
  4809. };
  4810. struct nk_style_combo {
  4811. /* background */
  4812. struct nk_style_item normal;
  4813. struct nk_style_item hover;
  4814. struct nk_style_item active;
  4815. struct nk_color border_color;
  4816. /* label */
  4817. struct nk_color label_normal;
  4818. struct nk_color label_hover;
  4819. struct nk_color label_active;
  4820. /* symbol */
  4821. struct nk_color symbol_normal;
  4822. struct nk_color symbol_hover;
  4823. struct nk_color symbol_active;
  4824. /* button */
  4825. struct nk_style_button button;
  4826. enum nk_symbol_type sym_normal;
  4827. enum nk_symbol_type sym_hover;
  4828. enum nk_symbol_type sym_active;
  4829. /* properties */
  4830. float border;
  4831. float rounding;
  4832. struct nk_vec2 content_padding;
  4833. struct nk_vec2 button_padding;
  4834. struct nk_vec2 spacing;
  4835. };
  4836. struct nk_style_tab {
  4837. /* background */
  4838. struct nk_style_item background;
  4839. struct nk_color border_color;
  4840. struct nk_color text;
  4841. /* button */
  4842. struct nk_style_button tab_maximize_button;
  4843. struct nk_style_button tab_minimize_button;
  4844. struct nk_style_button node_maximize_button;
  4845. struct nk_style_button node_minimize_button;
  4846. enum nk_symbol_type sym_minimize;
  4847. enum nk_symbol_type sym_maximize;
  4848. /* properties */
  4849. float border;
  4850. float rounding;
  4851. float indent;
  4852. struct nk_vec2 padding;
  4853. struct nk_vec2 spacing;
  4854. };
  4855. enum nk_style_header_align {
  4856. NK_HEADER_LEFT,
  4857. NK_HEADER_RIGHT
  4858. };
  4859. struct nk_style_window_header {
  4860. /* background */
  4861. struct nk_style_item normal;
  4862. struct nk_style_item hover;
  4863. struct nk_style_item active;
  4864. /* button */
  4865. struct nk_style_button close_button;
  4866. struct nk_style_button minimize_button;
  4867. enum nk_symbol_type close_symbol;
  4868. enum nk_symbol_type minimize_symbol;
  4869. enum nk_symbol_type maximize_symbol;
  4870. /* title */
  4871. struct nk_color label_normal;
  4872. struct nk_color label_hover;
  4873. struct nk_color label_active;
  4874. /* properties */
  4875. enum nk_style_header_align align;
  4876. struct nk_vec2 padding;
  4877. struct nk_vec2 label_padding;
  4878. struct nk_vec2 spacing;
  4879. };
  4880. struct nk_style_window {
  4881. struct nk_style_window_header header;
  4882. struct nk_style_item fixed_background;
  4883. struct nk_color background;
  4884. struct nk_color border_color;
  4885. struct nk_color popup_border_color;
  4886. struct nk_color combo_border_color;
  4887. struct nk_color contextual_border_color;
  4888. struct nk_color menu_border_color;
  4889. struct nk_color group_border_color;
  4890. struct nk_color tooltip_border_color;
  4891. struct nk_style_item scaler;
  4892. float border;
  4893. float combo_border;
  4894. float contextual_border;
  4895. float menu_border;
  4896. float group_border;
  4897. float tooltip_border;
  4898. float popup_border;
  4899. float min_row_height_padding;
  4900. float rounding;
  4901. struct nk_vec2 spacing;
  4902. struct nk_vec2 scrollbar_size;
  4903. struct nk_vec2 min_size;
  4904. struct nk_vec2 padding;
  4905. struct nk_vec2 group_padding;
  4906. struct nk_vec2 popup_padding;
  4907. struct nk_vec2 combo_padding;
  4908. struct nk_vec2 contextual_padding;
  4909. struct nk_vec2 menu_padding;
  4910. struct nk_vec2 tooltip_padding;
  4911. };
  4912. struct nk_style {
  4913. const struct nk_user_font *font;
  4914. const struct nk_cursor *cursors[NK_CURSOR_COUNT];
  4915. const struct nk_cursor *cursor_active;
  4916. struct nk_cursor *cursor_last;
  4917. int cursor_visible;
  4918. struct nk_style_text text;
  4919. struct nk_style_button button;
  4920. struct nk_style_button contextual_button;
  4921. struct nk_style_button menu_button;
  4922. struct nk_style_toggle option;
  4923. struct nk_style_toggle checkbox;
  4924. struct nk_style_selectable selectable;
  4925. struct nk_style_slider slider;
  4926. struct nk_style_progress progress;
  4927. struct nk_style_property property;
  4928. struct nk_style_edit edit;
  4929. struct nk_style_chart chart;
  4930. struct nk_style_scrollbar scrollh;
  4931. struct nk_style_scrollbar scrollv;
  4932. struct nk_style_tab tab;
  4933. struct nk_style_combo combo;
  4934. struct nk_style_window window;
  4935. };
  4936. NK_API struct nk_style_item nk_style_item_image(struct nk_image img);
  4937. NK_API struct nk_style_item nk_style_item_color(struct nk_color);
  4938. NK_API struct nk_style_item nk_style_item_hide(void);
  4939. /*==============================================================
  4940. * PANEL
  4941. * =============================================================*/
  4942. #ifndef NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS
  4943. #define NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS 16
  4944. #endif
  4945. #ifndef NK_CHART_MAX_SLOT
  4946. #define NK_CHART_MAX_SLOT 4
  4947. #endif
  4948. enum nk_panel_type {
  4949. NK_PANEL_NONE = 0,
  4950. NK_PANEL_WINDOW = NK_FLAG(0),
  4951. NK_PANEL_GROUP = NK_FLAG(1),
  4952. NK_PANEL_POPUP = NK_FLAG(2),
  4953. NK_PANEL_CONTEXTUAL = NK_FLAG(4),
  4954. NK_PANEL_COMBO = NK_FLAG(5),
  4955. NK_PANEL_MENU = NK_FLAG(6),
  4956. NK_PANEL_TOOLTIP = NK_FLAG(7)
  4957. };
  4958. enum nk_panel_set {
  4959. NK_PANEL_SET_NONBLOCK = NK_PANEL_CONTEXTUAL|NK_PANEL_COMBO|NK_PANEL_MENU|NK_PANEL_TOOLTIP,
  4960. NK_PANEL_SET_POPUP = NK_PANEL_SET_NONBLOCK|NK_PANEL_POPUP,
  4961. NK_PANEL_SET_SUB = NK_PANEL_SET_POPUP|NK_PANEL_GROUP
  4962. };
  4963. struct nk_chart_slot {
  4964. enum nk_chart_type type;
  4965. struct nk_color color;
  4966. struct nk_color highlight;
  4967. float min, max, range;
  4968. int count;
  4969. struct nk_vec2 last;
  4970. int index;
  4971. };
  4972. struct nk_chart {
  4973. int slot;
  4974. float x, y, w, h;
  4975. struct nk_chart_slot slots[NK_CHART_MAX_SLOT];
  4976. };
  4977. enum nk_panel_row_layout_type {
  4978. NK_LAYOUT_DYNAMIC_FIXED = 0,
  4979. NK_LAYOUT_DYNAMIC_ROW,
  4980. NK_LAYOUT_DYNAMIC_FREE,
  4981. NK_LAYOUT_DYNAMIC,
  4982. NK_LAYOUT_STATIC_FIXED,
  4983. NK_LAYOUT_STATIC_ROW,
  4984. NK_LAYOUT_STATIC_FREE,
  4985. NK_LAYOUT_STATIC,
  4986. NK_LAYOUT_TEMPLATE,
  4987. NK_LAYOUT_COUNT
  4988. };
  4989. struct nk_row_layout {
  4990. enum nk_panel_row_layout_type type;
  4991. int index;
  4992. float height;
  4993. float min_height;
  4994. int columns;
  4995. const float *ratio;
  4996. float item_width;
  4997. float item_height;
  4998. float item_offset;
  4999. float filled;
  5000. struct nk_rect item;
  5001. int tree_depth;
  5002. float templates[NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS];
  5003. };
  5004. struct nk_popup_buffer {
  5005. nk_size begin;
  5006. nk_size parent;
  5007. nk_size last;
  5008. nk_size end;
  5009. int active;
  5010. };
  5011. struct nk_menu_state {
  5012. float x, y, w, h;
  5013. struct nk_scroll offset;
  5014. };
  5015. struct nk_panel {
  5016. enum nk_panel_type type;
  5017. nk_flags flags;
  5018. struct nk_rect bounds;
  5019. nk_uint *offset_x;
  5020. nk_uint *offset_y;
  5021. float at_x, at_y, max_x;
  5022. float footer_height;
  5023. float header_height;
  5024. float border;
  5025. unsigned int has_scrolling;
  5026. struct nk_rect clip;
  5027. struct nk_menu_state menu;
  5028. struct nk_row_layout row;
  5029. struct nk_chart chart;
  5030. struct nk_command_buffer *buffer;
  5031. struct nk_panel *parent;
  5032. };
  5033. /*==============================================================
  5034. * WINDOW
  5035. * =============================================================*/
  5036. #ifndef NK_WINDOW_MAX_NAME
  5037. #define NK_WINDOW_MAX_NAME 64
  5038. #endif
  5039. struct nk_table;
  5040. enum nk_window_flags {
  5041. NK_WINDOW_PRIVATE = NK_FLAG(11),
  5042. NK_WINDOW_DYNAMIC = NK_WINDOW_PRIVATE,
  5043. /* special window type growing up in height while being filled to a certain maximum height */
  5044. NK_WINDOW_ROM = NK_FLAG(12),
  5045. /* sets window widgets into a read only mode and does not allow input changes */
  5046. NK_WINDOW_NOT_INTERACTIVE = NK_WINDOW_ROM|NK_WINDOW_NO_INPUT,
  5047. /* prevents all interaction caused by input to either window or widgets inside */
  5048. NK_WINDOW_HIDDEN = NK_FLAG(13),
  5049. /* Hides window and stops any window interaction and drawing */
  5050. NK_WINDOW_CLOSED = NK_FLAG(14),
  5051. /* Directly closes and frees the window at the end of the frame */
  5052. NK_WINDOW_MINIMIZED = NK_FLAG(15),
  5053. /* marks the window as minimized */
  5054. NK_WINDOW_REMOVE_ROM = NK_FLAG(16)
  5055. /* Removes read only mode at the end of the window */
  5056. };
  5057. struct nk_popup_state {
  5058. struct nk_window *win;
  5059. enum nk_panel_type type;
  5060. struct nk_popup_buffer buf;
  5061. nk_hash name;
  5062. int active;
  5063. unsigned combo_count;
  5064. unsigned con_count, con_old;
  5065. unsigned active_con;
  5066. struct nk_rect header;
  5067. };
  5068. struct nk_edit_state {
  5069. nk_hash name;
  5070. unsigned int seq;
  5071. unsigned int old;
  5072. int active, prev;
  5073. int cursor;
  5074. int sel_start;
  5075. int sel_end;
  5076. struct nk_scroll scrollbar;
  5077. unsigned char mode;
  5078. unsigned char single_line;
  5079. };
  5080. struct nk_property_state {
  5081. int active, prev;
  5082. char buffer[NK_MAX_NUMBER_BUFFER];
  5083. int length;
  5084. int cursor;
  5085. int select_start;
  5086. int select_end;
  5087. nk_hash name;
  5088. unsigned int seq;
  5089. unsigned int old;
  5090. int state;
  5091. };
  5092. struct nk_window {
  5093. unsigned int seq;
  5094. nk_hash name;
  5095. char name_string[NK_WINDOW_MAX_NAME];
  5096. nk_flags flags;
  5097. struct nk_rect bounds;
  5098. struct nk_scroll scrollbar;
  5099. struct nk_command_buffer buffer;
  5100. struct nk_panel *layout;
  5101. float scrollbar_hiding_timer;
  5102. /* persistent widget state */
  5103. struct nk_property_state property;
  5104. struct nk_popup_state popup;
  5105. struct nk_edit_state edit;
  5106. unsigned int scrolled;
  5107. struct nk_table *tables;
  5108. unsigned int table_count;
  5109. /* window list hooks */
  5110. struct nk_window *next;
  5111. struct nk_window *prev;
  5112. struct nk_window *parent;
  5113. };
  5114. /*==============================================================
  5115. * STACK
  5116. * =============================================================*/
  5117. /* The style modifier stack can be used to temporarily change a
  5118. * property inside `nk_style`. For example if you want a special
  5119. * red button you can temporarily push the old button color onto a stack
  5120. * draw the button with a red color and then you just pop the old color
  5121. * back from the stack:
  5122. *
  5123. * nk_style_push_style_item(ctx, &ctx->style.button.normal, nk_style_item_color(nk_rgb(255,0,0)));
  5124. * nk_style_push_style_item(ctx, &ctx->style.button.hover, nk_style_item_color(nk_rgb(255,0,0)));
  5125. * nk_style_push_style_item(ctx, &ctx->style.button.active, nk_style_item_color(nk_rgb(255,0,0)));
  5126. * nk_style_push_vec2(ctx, &cx->style.button.padding, nk_vec2(2,2));
  5127. *
  5128. * nk_button(...);
  5129. *
  5130. * nk_style_pop_style_item(ctx);
  5131. * nk_style_pop_style_item(ctx);
  5132. * nk_style_pop_style_item(ctx);
  5133. * nk_style_pop_vec2(ctx);
  5134. *
  5135. * Nuklear has a stack for style_items, float properties, vector properties,
  5136. * flags, colors, fonts and for button_behavior. Each has it's own fixed size stack
  5137. * which can be changed at compile time.
  5138. */
  5139. #ifndef NK_BUTTON_BEHAVIOR_STACK_SIZE
  5140. #define NK_BUTTON_BEHAVIOR_STACK_SIZE 8
  5141. #endif
  5142. #ifndef NK_FONT_STACK_SIZE
  5143. #define NK_FONT_STACK_SIZE 8
  5144. #endif
  5145. #ifndef NK_STYLE_ITEM_STACK_SIZE
  5146. #define NK_STYLE_ITEM_STACK_SIZE 16
  5147. #endif
  5148. #ifndef NK_FLOAT_STACK_SIZE
  5149. #define NK_FLOAT_STACK_SIZE 32
  5150. #endif
  5151. #ifndef NK_VECTOR_STACK_SIZE
  5152. #define NK_VECTOR_STACK_SIZE 16
  5153. #endif
  5154. #ifndef NK_FLAGS_STACK_SIZE
  5155. #define NK_FLAGS_STACK_SIZE 32
  5156. #endif
  5157. #ifndef NK_COLOR_STACK_SIZE
  5158. #define NK_COLOR_STACK_SIZE 32
  5159. #endif
  5160. #define NK_CONFIGURATION_STACK_TYPE(prefix, name, type)\
  5161. struct nk_config_stack_##name##_element {\
  5162. prefix##_##type *address;\
  5163. prefix##_##type old_value;\
  5164. }
  5165. #define NK_CONFIG_STACK(type,size)\
  5166. struct nk_config_stack_##type {\
  5167. int head;\
  5168. struct nk_config_stack_##type##_element elements[size];\
  5169. }
  5170. #define nk_float float
  5171. NK_CONFIGURATION_STACK_TYPE(struct nk, style_item, style_item);
  5172. NK_CONFIGURATION_STACK_TYPE(nk ,float, float);
  5173. NK_CONFIGURATION_STACK_TYPE(struct nk, vec2, vec2);
  5174. NK_CONFIGURATION_STACK_TYPE(nk ,flags, flags);
  5175. NK_CONFIGURATION_STACK_TYPE(struct nk, color, color);
  5176. NK_CONFIGURATION_STACK_TYPE(const struct nk, user_font, user_font*);
  5177. NK_CONFIGURATION_STACK_TYPE(enum nk, button_behavior, button_behavior);
  5178. NK_CONFIG_STACK(style_item, NK_STYLE_ITEM_STACK_SIZE);
  5179. NK_CONFIG_STACK(float, NK_FLOAT_STACK_SIZE);
  5180. NK_CONFIG_STACK(vec2, NK_VECTOR_STACK_SIZE);
  5181. NK_CONFIG_STACK(flags, NK_FLAGS_STACK_SIZE);
  5182. NK_CONFIG_STACK(color, NK_COLOR_STACK_SIZE);
  5183. NK_CONFIG_STACK(user_font, NK_FONT_STACK_SIZE);
  5184. NK_CONFIG_STACK(button_behavior, NK_BUTTON_BEHAVIOR_STACK_SIZE);
  5185. struct nk_configuration_stacks {
  5186. struct nk_config_stack_style_item style_items;
  5187. struct nk_config_stack_float floats;
  5188. struct nk_config_stack_vec2 vectors;
  5189. struct nk_config_stack_flags flags;
  5190. struct nk_config_stack_color colors;
  5191. struct nk_config_stack_user_font fonts;
  5192. struct nk_config_stack_button_behavior button_behaviors;
  5193. };
  5194. /*==============================================================
  5195. * CONTEXT
  5196. * =============================================================*/
  5197. #define NK_VALUE_PAGE_CAPACITY \
  5198. (((NK_MAX(sizeof(struct nk_window),sizeof(struct nk_panel)) / sizeof(nk_uint))) / 2)
  5199. struct nk_table {
  5200. unsigned int seq;
  5201. unsigned int size;
  5202. nk_hash keys[NK_VALUE_PAGE_CAPACITY];
  5203. nk_uint values[NK_VALUE_PAGE_CAPACITY];
  5204. struct nk_table *next, *prev;
  5205. };
  5206. union nk_page_data {
  5207. struct nk_table tbl;
  5208. struct nk_panel pan;
  5209. struct nk_window win;
  5210. };
  5211. struct nk_page_element {
  5212. union nk_page_data data;
  5213. struct nk_page_element *next;
  5214. struct nk_page_element *prev;
  5215. };
  5216. struct nk_page {
  5217. unsigned int size;
  5218. struct nk_page *next;
  5219. struct nk_page_element win[1];
  5220. };
  5221. struct nk_pool {
  5222. struct nk_allocator alloc;
  5223. enum nk_allocation_type type;
  5224. unsigned int page_count;
  5225. struct nk_page *pages;
  5226. struct nk_page_element *freelist;
  5227. unsigned capacity;
  5228. nk_size size;
  5229. nk_size cap;
  5230. };
  5231. struct nk_context {
  5232. /* public: can be accessed freely */
  5233. struct nk_input input;
  5234. struct nk_style style;
  5235. struct nk_buffer memory;
  5236. struct nk_clipboard clip;
  5237. nk_flags last_widget_state;
  5238. enum nk_button_behavior button_behavior;
  5239. struct nk_configuration_stacks stacks;
  5240. float delta_time_seconds;
  5241. /* private:
  5242. should only be accessed if you
  5243. know what you are doing */
  5244. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  5245. struct nk_draw_list draw_list;
  5246. #endif
  5247. #ifdef NK_INCLUDE_COMMAND_USERDATA
  5248. nk_handle userdata;
  5249. #endif
  5250. /* text editor objects are quite big because of an internal
  5251. * undo/redo stack. Therefore it does not make sense to have one for
  5252. * each window for temporary use cases, so I only provide *one* instance
  5253. * for all windows. This works because the content is cleared anyway */
  5254. struct nk_text_edit text_edit;
  5255. /* draw buffer used for overlay drawing operation like cursor */
  5256. struct nk_command_buffer overlay;
  5257. /* windows */
  5258. int build;
  5259. int use_pool;
  5260. struct nk_pool pool;
  5261. struct nk_window *begin;
  5262. struct nk_window *end;
  5263. struct nk_window *active;
  5264. struct nk_window *current;
  5265. struct nk_page_element *freelist;
  5266. unsigned int count;
  5267. unsigned int seq;
  5268. };
  5269. /* ==============================================================
  5270. * MATH
  5271. * =============================================================== */
  5272. #define NK_PI 3.141592654f
  5273. #define NK_UTF_INVALID 0xFFFD
  5274. #define NK_MAX_FLOAT_PRECISION 2
  5275. #define NK_UNUSED(x) ((void)(x))
  5276. #define NK_SATURATE(x) (NK_MAX(0, NK_MIN(1.0f, x)))
  5277. #define NK_LEN(a) (sizeof(a)/sizeof(a)[0])
  5278. #define NK_ABS(a) (((a) < 0) ? -(a) : (a))
  5279. #define NK_BETWEEN(x, a, b) ((a) <= (x) && (x) < (b))
  5280. #define NK_INBOX(px, py, x, y, w, h)\
  5281. (NK_BETWEEN(px,x,x+w) && NK_BETWEEN(py,y,y+h))
  5282. #define NK_INTERSECT(x0, y0, w0, h0, x1, y1, w1, h1) \
  5283. (!(((x1 > (x0 + w0)) || ((x1 + w1) < x0) || (y1 > (y0 + h0)) || (y1 + h1) < y0)))
  5284. #define NK_CONTAINS(x, y, w, h, bx, by, bw, bh)\
  5285. (NK_INBOX(x,y, bx, by, bw, bh) && NK_INBOX(x+w,y+h, bx, by, bw, bh))
  5286. #define nk_vec2_sub(a, b) nk_vec2((a).x - (b).x, (a).y - (b).y)
  5287. #define nk_vec2_add(a, b) nk_vec2((a).x + (b).x, (a).y + (b).y)
  5288. #define nk_vec2_len_sqr(a) ((a).x*(a).x+(a).y*(a).y)
  5289. #define nk_vec2_muls(a, t) nk_vec2((a).x * (t), (a).y * (t))
  5290. #define nk_ptr_add(t, p, i) ((t*)((void*)((nk_byte*)(p) + (i))))
  5291. #define nk_ptr_add_const(t, p, i) ((const t*)((const void*)((const nk_byte*)(p) + (i))))
  5292. #define nk_zero_struct(s) nk_zero(&s, sizeof(s))
  5293. /* ==============================================================
  5294. * ALIGNMENT
  5295. * =============================================================== */
  5296. /* Pointer to Integer type conversion for pointer alignment */
  5297. #if defined(__PTRDIFF_TYPE__) /* This case should work for GCC*/
  5298. # define NK_UINT_TO_PTR(x) ((void*)(__PTRDIFF_TYPE__)(x))
  5299. # define NK_PTR_TO_UINT(x) ((nk_size)(__PTRDIFF_TYPE__)(x))
  5300. #elif !defined(__GNUC__) /* works for compilers other than LLVM */
  5301. # define NK_UINT_TO_PTR(x) ((void*)&((char*)0)[x])
  5302. # define NK_PTR_TO_UINT(x) ((nk_size)(((char*)x)-(char*)0))
  5303. #elif defined(NK_USE_FIXED_TYPES) /* used if we have <stdint.h> */
  5304. # define NK_UINT_TO_PTR(x) ((void*)(uintptr_t)(x))
  5305. # define NK_PTR_TO_UINT(x) ((uintptr_t)(x))
  5306. #else /* generates warning but works */
  5307. # define NK_UINT_TO_PTR(x) ((void*)(x))
  5308. # define NK_PTR_TO_UINT(x) ((nk_size)(x))
  5309. #endif
  5310. #define NK_ALIGN_PTR(x, mask)\
  5311. (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x) + (mask-1)) & ~(mask-1))))
  5312. #define NK_ALIGN_PTR_BACK(x, mask)\
  5313. (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x)) & ~(mask-1))))
  5314. #define NK_OFFSETOF(st,m) ((nk_ptr)&(((st*)0)->m))
  5315. #define NK_CONTAINER_OF(ptr,type,member)\
  5316. (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member)))
  5317. #ifdef __cplusplus
  5318. }
  5319. #endif
  5320. #ifdef __cplusplus
  5321. template<typename T> struct nk_alignof;
  5322. template<typename T, int size_diff> struct nk_helper{enum {value = size_diff};};
  5323. template<typename T> struct nk_helper<T,0>{enum {value = nk_alignof<T>::value};};
  5324. template<typename T> struct nk_alignof{struct Big {T x; char c;}; enum {
  5325. diff = sizeof(Big) - sizeof(T), value = nk_helper<Big, diff>::value};};
  5326. #define NK_ALIGNOF(t) (nk_alignof<t>::value)
  5327. #elif defined(_MSC_VER)
  5328. #define NK_ALIGNOF(t) (__alignof(t))
  5329. #else
  5330. #define NK_ALIGNOF(t) ((char*)(&((struct {char c; t _h;}*)0)->_h) - (char*)0)
  5331. #endif
  5332. #endif /* NK_NUKLEAR_H_ */
  5333. #ifdef NK_IMPLEMENTATION
  5334. #ifndef NK_INTERNAL_H
  5335. #define NK_INTERNAL_H
  5336. #ifndef NK_POOL_DEFAULT_CAPACITY
  5337. #define NK_POOL_DEFAULT_CAPACITY 16
  5338. #endif
  5339. #ifndef NK_DEFAULT_COMMAND_BUFFER_SIZE
  5340. #define NK_DEFAULT_COMMAND_BUFFER_SIZE (4*1024)
  5341. #endif
  5342. #ifndef NK_BUFFER_DEFAULT_INITIAL_SIZE
  5343. #define NK_BUFFER_DEFAULT_INITIAL_SIZE (4*1024)
  5344. #endif
  5345. /* standard library headers */
  5346. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  5347. #include <stdlib.h> /* malloc, free */
  5348. #endif
  5349. #ifdef NK_INCLUDE_STANDARD_IO
  5350. #include <stdio.h> /* fopen, fclose,... */
  5351. #endif
  5352. #ifndef NK_ASSERT
  5353. #include <assert.h>
  5354. #define NK_ASSERT(expr) assert(expr)
  5355. #endif
  5356. #ifndef NK_MEMSET
  5357. #define NK_MEMSET nk_memset
  5358. #endif
  5359. #ifndef NK_MEMCPY
  5360. #define NK_MEMCPY nk_memcopy
  5361. #endif
  5362. #ifndef NK_SQRT
  5363. #define NK_SQRT nk_sqrt
  5364. #endif
  5365. #ifndef NK_SIN
  5366. #define NK_SIN nk_sin
  5367. #endif
  5368. #ifndef NK_COS
  5369. #define NK_COS nk_cos
  5370. #endif
  5371. #ifndef NK_STRTOD
  5372. #define NK_STRTOD nk_strtod
  5373. #endif
  5374. #ifndef NK_DTOA
  5375. #define NK_DTOA nk_dtoa
  5376. #endif
  5377. #define NK_DEFAULT (-1)
  5378. #ifndef NK_VSNPRINTF
  5379. /* If your compiler does support `vsnprintf` I would highly recommend
  5380. * defining this to vsnprintf instead since `vsprintf` is basically
  5381. * unbelievable unsafe and should *NEVER* be used. But I have to support
  5382. * it since C89 only provides this unsafe version. */
  5383. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) ||\
  5384. (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  5385. (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L)) ||\
  5386. (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 500)) ||\
  5387. defined(_ISOC99_SOURCE) || defined(_BSD_SOURCE)
  5388. #define NK_VSNPRINTF(s,n,f,a) vsnprintf(s,n,f,a)
  5389. #else
  5390. #define NK_VSNPRINTF(s,n,f,a) vsprintf(s,f,a)
  5391. #endif
  5392. #endif
  5393. #define NK_SCHAR_MIN (-127)
  5394. #define NK_SCHAR_MAX 127
  5395. #define NK_UCHAR_MIN 0
  5396. #define NK_UCHAR_MAX 256
  5397. #define NK_SSHORT_MIN (-32767)
  5398. #define NK_SSHORT_MAX 32767
  5399. #define NK_USHORT_MIN 0
  5400. #define NK_USHORT_MAX 65535
  5401. #define NK_SINT_MIN (-2147483647)
  5402. #define NK_SINT_MAX 2147483647
  5403. #define NK_UINT_MIN 0
  5404. #define NK_UINT_MAX 4294967295u
  5405. /* Make sure correct type size:
  5406. * This will fire with a negative subscript error if the type sizes
  5407. * are set incorrectly by the compiler, and compile out if not */
  5408. NK_STATIC_ASSERT(sizeof(nk_size) >= sizeof(void*));
  5409. NK_STATIC_ASSERT(sizeof(nk_ptr) == sizeof(void*));
  5410. NK_STATIC_ASSERT(sizeof(nk_flags) >= 4);
  5411. NK_STATIC_ASSERT(sizeof(nk_rune) >= 4);
  5412. NK_STATIC_ASSERT(sizeof(nk_ushort) == 2);
  5413. NK_STATIC_ASSERT(sizeof(nk_short) == 2);
  5414. NK_STATIC_ASSERT(sizeof(nk_uint) == 4);
  5415. NK_STATIC_ASSERT(sizeof(nk_int) == 4);
  5416. NK_STATIC_ASSERT(sizeof(nk_byte) == 1);
  5417. NK_GLOBAL const struct nk_rect nk_null_rect = {-8192.0f, -8192.0f, 16384, 16384};
  5418. #define NK_FLOAT_PRECISION 0.00000000000001
  5419. NK_GLOBAL const struct nk_color nk_red = {255,0,0,255};
  5420. NK_GLOBAL const struct nk_color nk_green = {0,255,0,255};
  5421. NK_GLOBAL const struct nk_color nk_blue = {0,0,255,255};
  5422. NK_GLOBAL const struct nk_color nk_white = {255,255,255,255};
  5423. NK_GLOBAL const struct nk_color nk_black = {0,0,0,255};
  5424. NK_GLOBAL const struct nk_color nk_yellow = {255,255,0,255};
  5425. /* widget */
  5426. #define nk_widget_state_reset(s)\
  5427. if ((*(s)) & NK_WIDGET_STATE_MODIFIED)\
  5428. (*(s)) = NK_WIDGET_STATE_INACTIVE|NK_WIDGET_STATE_MODIFIED;\
  5429. else (*(s)) = NK_WIDGET_STATE_INACTIVE;
  5430. /* math */
  5431. NK_LIB float nk_inv_sqrt(float n);
  5432. NK_LIB float nk_sqrt(float x);
  5433. NK_LIB float nk_sin(float x);
  5434. NK_LIB float nk_cos(float x);
  5435. NK_LIB nk_uint nk_round_up_pow2(nk_uint v);
  5436. NK_LIB struct nk_rect nk_shrink_rect(struct nk_rect r, float amount);
  5437. NK_LIB struct nk_rect nk_pad_rect(struct nk_rect r, struct nk_vec2 pad);
  5438. NK_LIB void nk_unify(struct nk_rect *clip, const struct nk_rect *a, float x0, float y0, float x1, float y1);
  5439. NK_LIB double nk_pow(double x, int n);
  5440. NK_LIB int nk_ifloord(double x);
  5441. NK_LIB int nk_ifloorf(float x);
  5442. NK_LIB int nk_iceilf(float x);
  5443. NK_LIB int nk_log10(double n);
  5444. /* util */
  5445. enum {NK_DO_NOT_STOP_ON_NEW_LINE, NK_STOP_ON_NEW_LINE};
  5446. NK_LIB int nk_is_lower(int c);
  5447. NK_LIB int nk_is_upper(int c);
  5448. NK_LIB int nk_to_upper(int c);
  5449. NK_LIB int nk_to_lower(int c);
  5450. NK_LIB void* nk_memcopy(void *dst, const void *src, nk_size n);
  5451. NK_LIB void nk_memset(void *ptr, int c0, nk_size size);
  5452. NK_LIB void nk_zero(void *ptr, nk_size size);
  5453. NK_LIB char *nk_itoa(char *s, long n);
  5454. NK_LIB int nk_string_float_limit(char *string, int prec);
  5455. NK_LIB char *nk_dtoa(char *s, double n);
  5456. NK_LIB int nk_text_clamp(const struct nk_user_font *font, const char *text, int text_len, float space, int *glyphs, float *text_width, nk_rune *sep_list, int sep_count);
  5457. NK_LIB struct nk_vec2 nk_text_calculate_text_bounds(const struct nk_user_font *font, const char *begin, int byte_len, float row_height, const char **remaining, struct nk_vec2 *out_offset, int *glyphs, int op);
  5458. #ifdef NK_INCLUDE_STANDARD_VARARGS
  5459. NK_LIB int nk_strfmt(char *buf, int buf_size, const char *fmt, va_list args);
  5460. #endif
  5461. #ifdef NK_INCLUDE_STANDARD_IO
  5462. NK_LIB char *nk_file_load(const char* path, nk_size* siz, struct nk_allocator *alloc);
  5463. #endif
  5464. /* buffer */
  5465. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  5466. NK_LIB void* nk_malloc(nk_handle unused, void *old,nk_size size);
  5467. NK_LIB void nk_mfree(nk_handle unused, void *ptr);
  5468. #endif
  5469. NK_LIB void* nk_buffer_align(void *unaligned, nk_size align, nk_size *alignment, enum nk_buffer_allocation_type type);
  5470. NK_LIB void* nk_buffer_alloc(struct nk_buffer *b, enum nk_buffer_allocation_type type, nk_size size, nk_size align);
  5471. NK_LIB void* nk_buffer_realloc(struct nk_buffer *b, nk_size capacity, nk_size *size);
  5472. /* draw */
  5473. NK_LIB void nk_command_buffer_init(struct nk_command_buffer *cb, struct nk_buffer *b, enum nk_command_clipping clip);
  5474. NK_LIB void nk_command_buffer_reset(struct nk_command_buffer *b);
  5475. NK_LIB void* nk_command_buffer_push(struct nk_command_buffer* b, enum nk_command_type t, nk_size size);
  5476. NK_LIB void nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type, struct nk_rect content, struct nk_color background, struct nk_color foreground, float border_width, const struct nk_user_font *font);
  5477. /* buffering */
  5478. NK_LIB void nk_start_buffer(struct nk_context *ctx, struct nk_command_buffer *b);
  5479. NK_LIB void nk_start(struct nk_context *ctx, struct nk_window *win);
  5480. NK_LIB void nk_start_popup(struct nk_context *ctx, struct nk_window *win);
  5481. NK_LIB void nk_finish_popup(struct nk_context *ctx, struct nk_window*);
  5482. NK_LIB void nk_finish_buffer(struct nk_context *ctx, struct nk_command_buffer *b);
  5483. NK_LIB void nk_finish(struct nk_context *ctx, struct nk_window *w);
  5484. NK_LIB void nk_build(struct nk_context *ctx);
  5485. /* text editor */
  5486. NK_LIB void nk_textedit_clear_state(struct nk_text_edit *state, enum nk_text_edit_type type, nk_plugin_filter filter);
  5487. NK_LIB void nk_textedit_click(struct nk_text_edit *state, float x, float y, const struct nk_user_font *font, float row_height);
  5488. NK_LIB void nk_textedit_drag(struct nk_text_edit *state, float x, float y, const struct nk_user_font *font, float row_height);
  5489. NK_LIB void nk_textedit_key(struct nk_text_edit *state, enum nk_keys key, int shift_mod, const struct nk_user_font *font, float row_height);
  5490. /* window */
  5491. enum nk_window_insert_location {
  5492. NK_INSERT_BACK, /* inserts window into the back of list (front of screen) */
  5493. NK_INSERT_FRONT /* inserts window into the front of list (back of screen) */
  5494. };
  5495. NK_LIB void *nk_create_window(struct nk_context *ctx);
  5496. NK_LIB void nk_remove_window(struct nk_context*, struct nk_window*);
  5497. NK_LIB void nk_free_window(struct nk_context *ctx, struct nk_window *win);
  5498. NK_LIB struct nk_window *nk_find_window(struct nk_context *ctx, nk_hash hash, const char *name);
  5499. NK_LIB void nk_insert_window(struct nk_context *ctx, struct nk_window *win, enum nk_window_insert_location loc);
  5500. /* pool */
  5501. NK_LIB void nk_pool_init(struct nk_pool *pool, struct nk_allocator *alloc, unsigned int capacity);
  5502. NK_LIB void nk_pool_free(struct nk_pool *pool);
  5503. NK_LIB void nk_pool_init_fixed(struct nk_pool *pool, void *memory, nk_size size);
  5504. NK_LIB struct nk_page_element *nk_pool_alloc(struct nk_pool *pool);
  5505. /* page-element */
  5506. NK_LIB struct nk_page_element* nk_create_page_element(struct nk_context *ctx);
  5507. NK_LIB void nk_link_page_element_into_freelist(struct nk_context *ctx, struct nk_page_element *elem);
  5508. NK_LIB void nk_free_page_element(struct nk_context *ctx, struct nk_page_element *elem);
  5509. /* table */
  5510. NK_LIB struct nk_table* nk_create_table(struct nk_context *ctx);
  5511. NK_LIB void nk_remove_table(struct nk_window *win, struct nk_table *tbl);
  5512. NK_LIB void nk_free_table(struct nk_context *ctx, struct nk_table *tbl);
  5513. NK_LIB void nk_push_table(struct nk_window *win, struct nk_table *tbl);
  5514. NK_LIB nk_uint *nk_add_value(struct nk_context *ctx, struct nk_window *win, nk_hash name, nk_uint value);
  5515. NK_LIB nk_uint *nk_find_value(struct nk_window *win, nk_hash name);
  5516. /* panel */
  5517. NK_LIB void *nk_create_panel(struct nk_context *ctx);
  5518. NK_LIB void nk_free_panel(struct nk_context*, struct nk_panel *pan);
  5519. NK_LIB int nk_panel_has_header(nk_flags flags, const char *title);
  5520. NK_LIB struct nk_vec2 nk_panel_get_padding(const struct nk_style *style, enum nk_panel_type type);
  5521. NK_LIB float nk_panel_get_border(const struct nk_style *style, nk_flags flags, enum nk_panel_type type);
  5522. NK_LIB struct nk_color nk_panel_get_border_color(const struct nk_style *style, enum nk_panel_type type);
  5523. NK_LIB int nk_panel_is_sub(enum nk_panel_type type);
  5524. NK_LIB int nk_panel_is_nonblock(enum nk_panel_type type);
  5525. NK_LIB int nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type panel_type);
  5526. NK_LIB void nk_panel_end(struct nk_context *ctx);
  5527. /* layout */
  5528. NK_LIB float nk_layout_row_calculate_usable_space(const struct nk_style *style, enum nk_panel_type type, float total_space, int columns);
  5529. NK_LIB void nk_panel_layout(const struct nk_context *ctx, struct nk_window *win, float height, int cols);
  5530. NK_LIB void nk_row_layout(struct nk_context *ctx, enum nk_layout_format fmt, float height, int cols, int width);
  5531. NK_LIB void nk_panel_alloc_row(const struct nk_context *ctx, struct nk_window *win);
  5532. NK_LIB void nk_layout_widget_space(struct nk_rect *bounds, const struct nk_context *ctx, struct nk_window *win, int modify);
  5533. NK_LIB void nk_panel_alloc_space(struct nk_rect *bounds, const struct nk_context *ctx);
  5534. NK_LIB void nk_layout_peek(struct nk_rect *bounds, struct nk_context *ctx);
  5535. /* popup */
  5536. NK_LIB int nk_nonblock_begin(struct nk_context *ctx, nk_flags flags, struct nk_rect body, struct nk_rect header, enum nk_panel_type panel_type);
  5537. /* text */
  5538. struct nk_text {
  5539. struct nk_vec2 padding;
  5540. struct nk_color background;
  5541. struct nk_color text;
  5542. };
  5543. NK_LIB void nk_widget_text(struct nk_command_buffer *o, struct nk_rect b, const char *string, int len, const struct nk_text *t, nk_flags a, const struct nk_user_font *f);
  5544. NK_LIB void nk_widget_text_wrap(struct nk_command_buffer *o, struct nk_rect b, const char *string, int len, const struct nk_text *t, const struct nk_user_font *f);
  5545. /* button */
  5546. NK_LIB int nk_button_behavior(nk_flags *state, struct nk_rect r, const struct nk_input *i, enum nk_button_behavior behavior);
  5547. NK_LIB const struct nk_style_item* nk_draw_button(struct nk_command_buffer *out, const struct nk_rect *bounds, nk_flags state, const struct nk_style_button *style);
  5548. NK_LIB int nk_do_button(nk_flags *state, struct nk_command_buffer *out, struct nk_rect r, const struct nk_style_button *style, const struct nk_input *in, enum nk_button_behavior behavior, struct nk_rect *content);
  5549. NK_LIB void nk_draw_button_text(struct nk_command_buffer *out, const struct nk_rect *bounds, const struct nk_rect *content, nk_flags state, const struct nk_style_button *style, const char *txt, int len, nk_flags text_alignment, const struct nk_user_font *font);
  5550. NK_LIB int nk_do_button_text(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, const char *string, int len, nk_flags align, enum nk_button_behavior behavior, const struct nk_style_button *style, const struct nk_input *in, const struct nk_user_font *font);
  5551. NK_LIB void nk_draw_button_symbol(struct nk_command_buffer *out, const struct nk_rect *bounds, const struct nk_rect *content, nk_flags state, const struct nk_style_button *style, enum nk_symbol_type type, const struct nk_user_font *font);
  5552. NK_LIB int nk_do_button_symbol(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, enum nk_symbol_type symbol, enum nk_button_behavior behavior, const struct nk_style_button *style, const struct nk_input *in, const struct nk_user_font *font);
  5553. NK_LIB void nk_draw_button_image(struct nk_command_buffer *out, const struct nk_rect *bounds, const struct nk_rect *content, nk_flags state, const struct nk_style_button *style, const struct nk_image *img);
  5554. NK_LIB int nk_do_button_image(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, struct nk_image img, enum nk_button_behavior b, const struct nk_style_button *style, const struct nk_input *in);
  5555. NK_LIB void nk_draw_button_text_symbol(struct nk_command_buffer *out, const struct nk_rect *bounds, const struct nk_rect *label, const struct nk_rect *symbol, nk_flags state, const struct nk_style_button *style, const char *str, int len, enum nk_symbol_type type, const struct nk_user_font *font);
  5556. NK_LIB int nk_do_button_text_symbol(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, enum nk_symbol_type symbol, const char *str, int len, nk_flags align, enum nk_button_behavior behavior, const struct nk_style_button *style, const struct nk_user_font *font, const struct nk_input *in);
  5557. NK_LIB void nk_draw_button_text_image(struct nk_command_buffer *out, const struct nk_rect *bounds, const struct nk_rect *label, const struct nk_rect *image, nk_flags state, const struct nk_style_button *style, const char *str, int len, const struct nk_user_font *font, const struct nk_image *img);
  5558. NK_LIB int nk_do_button_text_image(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, struct nk_image img, const char* str, int len, nk_flags align, enum nk_button_behavior behavior, const struct nk_style_button *style, const struct nk_user_font *font, const struct nk_input *in);
  5559. /* toggle */
  5560. enum nk_toggle_type {
  5561. NK_TOGGLE_CHECK,
  5562. NK_TOGGLE_OPTION
  5563. };
  5564. NK_LIB int nk_toggle_behavior(const struct nk_input *in, struct nk_rect select, nk_flags *state, int active);
  5565. NK_LIB void nk_draw_checkbox(struct nk_command_buffer *out, nk_flags state, const struct nk_style_toggle *style, int active, const struct nk_rect *label, const struct nk_rect *selector, const struct nk_rect *cursors, const char *string, int len, const struct nk_user_font *font);
  5566. NK_LIB void nk_draw_option(struct nk_command_buffer *out, nk_flags state, const struct nk_style_toggle *style, int active, const struct nk_rect *label, const struct nk_rect *selector, const struct nk_rect *cursors, const char *string, int len, const struct nk_user_font *font);
  5567. NK_LIB int nk_do_toggle(nk_flags *state, struct nk_command_buffer *out, struct nk_rect r, int *active, const char *str, int len, enum nk_toggle_type type, const struct nk_style_toggle *style, const struct nk_input *in, const struct nk_user_font *font);
  5568. /* progress */
  5569. NK_LIB nk_size nk_progress_behavior(nk_flags *state, struct nk_input *in, struct nk_rect r, struct nk_rect cursor, nk_size max, nk_size value, int modifiable);
  5570. NK_LIB void nk_draw_progress(struct nk_command_buffer *out, nk_flags state, const struct nk_style_progress *style, const struct nk_rect *bounds, const struct nk_rect *scursor, nk_size value, nk_size max);
  5571. NK_LIB nk_size nk_do_progress(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, nk_size value, nk_size max, int modifiable, const struct nk_style_progress *style, struct nk_input *in);
  5572. /* slider */
  5573. NK_LIB float nk_slider_behavior(nk_flags *state, struct nk_rect *logical_cursor, struct nk_rect *visual_cursor, struct nk_input *in, struct nk_rect bounds, float slider_min, float slider_max, float slider_value, float slider_step, float slider_steps);
  5574. NK_LIB void nk_draw_slider(struct nk_command_buffer *out, nk_flags state, const struct nk_style_slider *style, const struct nk_rect *bounds, const struct nk_rect *visual_cursor, float min, float value, float max);
  5575. NK_LIB float nk_do_slider(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, float min, float val, float max, float step, const struct nk_style_slider *style, struct nk_input *in, const struct nk_user_font *font);
  5576. /* scrollbar */
  5577. NK_LIB float nk_scrollbar_behavior(nk_flags *state, struct nk_input *in, int has_scrolling, const struct nk_rect *scroll, const struct nk_rect *cursor, const struct nk_rect *empty0, const struct nk_rect *empty1, float scroll_offset, float target, float scroll_step, enum nk_orientation o);
  5578. NK_LIB void nk_draw_scrollbar(struct nk_command_buffer *out, nk_flags state, const struct nk_style_scrollbar *style, const struct nk_rect *bounds, const struct nk_rect *scroll);
  5579. NK_LIB float nk_do_scrollbarv(nk_flags *state, struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling, float offset, float target, float step, float button_pixel_inc, const struct nk_style_scrollbar *style, struct nk_input *in, const struct nk_user_font *font);
  5580. NK_LIB float nk_do_scrollbarh(nk_flags *state, struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling, float offset, float target, float step, float button_pixel_inc, const struct nk_style_scrollbar *style, struct nk_input *in, const struct nk_user_font *font);
  5581. /* selectable */
  5582. NK_LIB void nk_draw_selectable(struct nk_command_buffer *out, nk_flags state, const struct nk_style_selectable *style, int active, const struct nk_rect *bounds, const struct nk_rect *icon, const struct nk_image *img, enum nk_symbol_type sym, const char *string, int len, nk_flags align, const struct nk_user_font *font);
  5583. NK_LIB int nk_do_selectable(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, const char *str, int len, nk_flags align, int *value, const struct nk_style_selectable *style, const struct nk_input *in, const struct nk_user_font *font);
  5584. NK_LIB int nk_do_selectable_image(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, const char *str, int len, nk_flags align, int *value, const struct nk_image *img, const struct nk_style_selectable *style, const struct nk_input *in, const struct nk_user_font *font);
  5585. /* edit */
  5586. NK_LIB void nk_edit_draw_text(struct nk_command_buffer *out, const struct nk_style_edit *style, float pos_x, float pos_y, float x_offset, const char *text, int byte_len, float row_height, const struct nk_user_font *font, struct nk_color background, struct nk_color foreground, int is_selected);
  5587. NK_LIB nk_flags nk_do_edit(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, nk_flags flags, nk_plugin_filter filter, struct nk_text_edit *edit, const struct nk_style_edit *style, struct nk_input *in, const struct nk_user_font *font);
  5588. /* color-picker */
  5589. NK_LIB int nk_color_picker_behavior(nk_flags *state, const struct nk_rect *bounds, const struct nk_rect *matrix, const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar, struct nk_colorf *color, const struct nk_input *in);
  5590. NK_LIB void nk_draw_color_picker(struct nk_command_buffer *o, const struct nk_rect *matrix, const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar, struct nk_colorf col);
  5591. NK_LIB int nk_do_color_picker(nk_flags *state, struct nk_command_buffer *out, struct nk_colorf *col, enum nk_color_format fmt, struct nk_rect bounds, struct nk_vec2 padding, const struct nk_input *in, const struct nk_user_font *font);
  5592. /* property */
  5593. enum nk_property_status {
  5594. NK_PROPERTY_DEFAULT,
  5595. NK_PROPERTY_EDIT,
  5596. NK_PROPERTY_DRAG
  5597. };
  5598. enum nk_property_filter {
  5599. NK_FILTER_INT,
  5600. NK_FILTER_FLOAT
  5601. };
  5602. enum nk_property_kind {
  5603. NK_PROPERTY_INT,
  5604. NK_PROPERTY_FLOAT,
  5605. NK_PROPERTY_DOUBLE
  5606. };
  5607. union nk_property {
  5608. int i;
  5609. float f;
  5610. double d;
  5611. };
  5612. struct nk_property_variant {
  5613. enum nk_property_kind kind;
  5614. union nk_property value;
  5615. union nk_property min_value;
  5616. union nk_property max_value;
  5617. union nk_property step;
  5618. };
  5619. NK_LIB struct nk_property_variant nk_property_variant_int(int value, int min_value, int max_value, int step);
  5620. NK_LIB struct nk_property_variant nk_property_variant_float(float value, float min_value, float max_value, float step);
  5621. NK_LIB struct nk_property_variant nk_property_variant_double(double value, double min_value, double max_value, double step);
  5622. NK_LIB void nk_drag_behavior(nk_flags *state, const struct nk_input *in, struct nk_rect drag, struct nk_property_variant *variant, float inc_per_pixel);
  5623. NK_LIB void nk_property_behavior(nk_flags *ws, const struct nk_input *in, struct nk_rect property, struct nk_rect label, struct nk_rect edit, struct nk_rect empty, int *state, struct nk_property_variant *variant, float inc_per_pixel);
  5624. NK_LIB void nk_draw_property(struct nk_command_buffer *out, const struct nk_style_property *style, const struct nk_rect *bounds, const struct nk_rect *label, nk_flags state, const char *name, int len, const struct nk_user_font *font);
  5625. NK_LIB void nk_do_property(nk_flags *ws, struct nk_command_buffer *out, struct nk_rect property, const char *name, struct nk_property_variant *variant, float inc_per_pixel, char *buffer, int *len, int *state, int *cursor, int *select_begin, int *select_end, const struct nk_style_property *style, enum nk_property_filter filter, struct nk_input *in, const struct nk_user_font *font, struct nk_text_edit *text_edit, enum nk_button_behavior behavior);
  5626. NK_LIB void nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant *variant, float inc_per_pixel, const enum nk_property_filter filter);
  5627. #endif
  5628. /* ===============================================================
  5629. *
  5630. * MATH
  5631. *
  5632. * ===============================================================*/
  5633. /* Since nuklear is supposed to work on all systems providing floating point
  5634. math without any dependencies I also had to implement my own math functions
  5635. for sqrt, sin and cos. Since the actual highly accurate implementations for
  5636. the standard library functions are quite complex and I do not need high
  5637. precision for my use cases I use approximations.
  5638. Sqrt
  5639. ----
  5640. For square root nuklear uses the famous fast inverse square root:
  5641. https://en.wikipedia.org/wiki/Fast_inverse_square_root with
  5642. slightly tweaked magic constant. While on today's hardware it is
  5643. probably not faster it is still fast and accurate enough for
  5644. nuklear's use cases. IMPORTANT: this requires float format IEEE 754
  5645. Sine/Cosine
  5646. -----------
  5647. All constants inside both function are generated Remez's minimax
  5648. approximations for value range 0...2*PI. The reason why I decided to
  5649. approximate exactly that range is that nuklear only needs sine and
  5650. cosine to generate circles which only requires that exact range.
  5651. In addition I used Remez instead of Taylor for additional precision:
  5652. www.lolengine.net/blog/2011/12/21/better-function-approximations.
  5653. The tool I used to generate constants for both sine and cosine
  5654. (it can actually approximate a lot more functions) can be
  5655. found here: www.lolengine.net/wiki/oss/lolremez
  5656. */
  5657. NK_LIB float
  5658. nk_inv_sqrt(float n)
  5659. {
  5660. float x2;
  5661. const float threehalfs = 1.5f;
  5662. union {nk_uint i; float f;} conv = {0};
  5663. conv.f = n;
  5664. x2 = n * 0.5f;
  5665. conv.i = 0x5f375A84 - (conv.i >> 1);
  5666. conv.f = conv.f * (threehalfs - (x2 * conv.f * conv.f));
  5667. return conv.f;
  5668. }
  5669. NK_LIB float
  5670. nk_sqrt(float x)
  5671. {
  5672. return x * nk_inv_sqrt(x);
  5673. }
  5674. NK_LIB float
  5675. nk_sin(float x)
  5676. {
  5677. NK_STORAGE const float a0 = +1.91059300966915117e-31f;
  5678. NK_STORAGE const float a1 = +1.00086760103908896f;
  5679. NK_STORAGE const float a2 = -1.21276126894734565e-2f;
  5680. NK_STORAGE const float a3 = -1.38078780785773762e-1f;
  5681. NK_STORAGE const float a4 = -2.67353392911981221e-2f;
  5682. NK_STORAGE const float a5 = +2.08026600266304389e-2f;
  5683. NK_STORAGE const float a6 = -3.03996055049204407e-3f;
  5684. NK_STORAGE const float a7 = +1.38235642404333740e-4f;
  5685. return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*a7))))));
  5686. }
  5687. NK_LIB float
  5688. nk_cos(float x)
  5689. {
  5690. NK_STORAGE const float a0 = +1.00238601909309722f;
  5691. NK_STORAGE const float a1 = -3.81919947353040024e-2f;
  5692. NK_STORAGE const float a2 = -3.94382342128062756e-1f;
  5693. NK_STORAGE const float a3 = -1.18134036025221444e-1f;
  5694. NK_STORAGE const float a4 = +1.07123798512170878e-1f;
  5695. NK_STORAGE const float a5 = -1.86637164165180873e-2f;
  5696. NK_STORAGE const float a6 = +9.90140908664079833e-4f;
  5697. NK_STORAGE const float a7 = -5.23022132118824778e-14f;
  5698. return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*a7))))));
  5699. }
  5700. NK_LIB nk_uint
  5701. nk_round_up_pow2(nk_uint v)
  5702. {
  5703. v--;
  5704. v |= v >> 1;
  5705. v |= v >> 2;
  5706. v |= v >> 4;
  5707. v |= v >> 8;
  5708. v |= v >> 16;
  5709. v++;
  5710. return v;
  5711. }
  5712. NK_LIB double
  5713. nk_pow(double x, int n)
  5714. {
  5715. /* check the sign of n */
  5716. double r = 1;
  5717. int plus = n >= 0;
  5718. n = (plus) ? n : -n;
  5719. while (n > 0) {
  5720. if ((n & 1) == 1)
  5721. r *= x;
  5722. n /= 2;
  5723. x *= x;
  5724. }
  5725. return plus ? r : 1.0 / r;
  5726. }
  5727. NK_LIB int
  5728. nk_ifloord(double x)
  5729. {
  5730. x = (double)((int)x - ((x < 0.0) ? 1 : 0));
  5731. return (int)x;
  5732. }
  5733. NK_LIB int
  5734. nk_ifloorf(float x)
  5735. {
  5736. x = (float)((int)x - ((x < 0.0f) ? 1 : 0));
  5737. return (int)x;
  5738. }
  5739. NK_LIB int
  5740. nk_iceilf(float x)
  5741. {
  5742. if (x >= 0) {
  5743. int i = (int)x;
  5744. return (x > i) ? i+1: i;
  5745. } else {
  5746. int t = (int)x;
  5747. float r = x - (float)t;
  5748. return (r > 0.0f) ? t+1: t;
  5749. }
  5750. }
  5751. NK_LIB int
  5752. nk_log10(double n)
  5753. {
  5754. int neg;
  5755. int ret;
  5756. int exp = 0;
  5757. neg = (n < 0) ? 1 : 0;
  5758. ret = (neg) ? (int)-n : (int)n;
  5759. while ((ret / 10) > 0) {
  5760. ret /= 10;
  5761. exp++;
  5762. }
  5763. if (neg) exp = -exp;
  5764. return exp;
  5765. }
  5766. NK_API struct nk_rect
  5767. nk_get_null_rect(void)
  5768. {
  5769. return nk_null_rect;
  5770. }
  5771. NK_API struct nk_rect
  5772. nk_rect(float x, float y, float w, float h)
  5773. {
  5774. struct nk_rect r;
  5775. r.x = x; r.y = y;
  5776. r.w = w; r.h = h;
  5777. return r;
  5778. }
  5779. NK_API struct nk_rect
  5780. nk_recti(int x, int y, int w, int h)
  5781. {
  5782. struct nk_rect r;
  5783. r.x = (float)x;
  5784. r.y = (float)y;
  5785. r.w = (float)w;
  5786. r.h = (float)h;
  5787. return r;
  5788. }
  5789. NK_API struct nk_rect
  5790. nk_recta(struct nk_vec2 pos, struct nk_vec2 size)
  5791. {
  5792. return nk_rect(pos.x, pos.y, size.x, size.y);
  5793. }
  5794. NK_API struct nk_rect
  5795. nk_rectv(const float *r)
  5796. {
  5797. return nk_rect(r[0], r[1], r[2], r[3]);
  5798. }
  5799. NK_API struct nk_rect
  5800. nk_rectiv(const int *r)
  5801. {
  5802. return nk_recti(r[0], r[1], r[2], r[3]);
  5803. }
  5804. NK_API struct nk_vec2
  5805. nk_rect_pos(struct nk_rect r)
  5806. {
  5807. struct nk_vec2 ret;
  5808. ret.x = r.x; ret.y = r.y;
  5809. return ret;
  5810. }
  5811. NK_API struct nk_vec2
  5812. nk_rect_size(struct nk_rect r)
  5813. {
  5814. struct nk_vec2 ret;
  5815. ret.x = r.w; ret.y = r.h;
  5816. return ret;
  5817. }
  5818. NK_LIB struct nk_rect
  5819. nk_shrink_rect(struct nk_rect r, float amount)
  5820. {
  5821. struct nk_rect res;
  5822. r.w = NK_MAX(r.w, 2 * amount);
  5823. r.h = NK_MAX(r.h, 2 * amount);
  5824. res.x = r.x + amount;
  5825. res.y = r.y + amount;
  5826. res.w = r.w - 2 * amount;
  5827. res.h = r.h - 2 * amount;
  5828. return res;
  5829. }
  5830. NK_LIB struct nk_rect
  5831. nk_pad_rect(struct nk_rect r, struct nk_vec2 pad)
  5832. {
  5833. r.w = NK_MAX(r.w, 2 * pad.x);
  5834. r.h = NK_MAX(r.h, 2 * pad.y);
  5835. r.x += pad.x; r.y += pad.y;
  5836. r.w -= 2 * pad.x;
  5837. r.h -= 2 * pad.y;
  5838. return r;
  5839. }
  5840. NK_API struct nk_vec2
  5841. nk_vec2(float x, float y)
  5842. {
  5843. struct nk_vec2 ret;
  5844. ret.x = x; ret.y = y;
  5845. return ret;
  5846. }
  5847. NK_API struct nk_vec2
  5848. nk_vec2i(int x, int y)
  5849. {
  5850. struct nk_vec2 ret;
  5851. ret.x = (float)x;
  5852. ret.y = (float)y;
  5853. return ret;
  5854. }
  5855. NK_API struct nk_vec2
  5856. nk_vec2v(const float *v)
  5857. {
  5858. return nk_vec2(v[0], v[1]);
  5859. }
  5860. NK_API struct nk_vec2
  5861. nk_vec2iv(const int *v)
  5862. {
  5863. return nk_vec2i(v[0], v[1]);
  5864. }
  5865. NK_LIB void
  5866. nk_unify(struct nk_rect *clip, const struct nk_rect *a, float x0, float y0,
  5867. float x1, float y1)
  5868. {
  5869. NK_ASSERT(a);
  5870. NK_ASSERT(clip);
  5871. clip->x = NK_MAX(a->x, x0);
  5872. clip->y = NK_MAX(a->y, y0);
  5873. clip->w = NK_MIN(a->x + a->w, x1) - clip->x;
  5874. clip->h = NK_MIN(a->y + a->h, y1) - clip->y;
  5875. clip->w = NK_MAX(0, clip->w);
  5876. clip->h = NK_MAX(0, clip->h);
  5877. }
  5878. NK_API void
  5879. nk_triangle_from_direction(struct nk_vec2 *result, struct nk_rect r,
  5880. float pad_x, float pad_y, enum nk_heading direction)
  5881. {
  5882. float w_half, h_half;
  5883. NK_ASSERT(result);
  5884. r.w = NK_MAX(2 * pad_x, r.w);
  5885. r.h = NK_MAX(2 * pad_y, r.h);
  5886. r.w = r.w - 2 * pad_x;
  5887. r.h = r.h - 2 * pad_y;
  5888. r.x = r.x + pad_x;
  5889. r.y = r.y + pad_y;
  5890. w_half = r.w / 2.0f;
  5891. h_half = r.h / 2.0f;
  5892. if (direction == NK_UP) {
  5893. result[0] = nk_vec2(r.x + w_half, r.y);
  5894. result[1] = nk_vec2(r.x + r.w, r.y + r.h);
  5895. result[2] = nk_vec2(r.x, r.y + r.h);
  5896. } else if (direction == NK_RIGHT) {
  5897. result[0] = nk_vec2(r.x, r.y);
  5898. result[1] = nk_vec2(r.x + r.w, r.y + h_half);
  5899. result[2] = nk_vec2(r.x, r.y + r.h);
  5900. } else if (direction == NK_DOWN) {
  5901. result[0] = nk_vec2(r.x, r.y);
  5902. result[1] = nk_vec2(r.x + r.w, r.y);
  5903. result[2] = nk_vec2(r.x + w_half, r.y + r.h);
  5904. } else {
  5905. result[0] = nk_vec2(r.x, r.y + h_half);
  5906. result[1] = nk_vec2(r.x + r.w, r.y);
  5907. result[2] = nk_vec2(r.x + r.w, r.y + r.h);
  5908. }
  5909. }
  5910. /* ===============================================================
  5911. *
  5912. * UTIL
  5913. *
  5914. * ===============================================================*/
  5915. NK_INTERN int nk_str_match_here(const char *regexp, const char *text);
  5916. NK_INTERN int nk_str_match_star(int c, const char *regexp, const char *text);
  5917. NK_LIB int nk_is_lower(int c) {return (c >= 'a' && c <= 'z') || (c >= 0xE0 && c <= 0xFF);}
  5918. NK_LIB int nk_is_upper(int c){return (c >= 'A' && c <= 'Z') || (c >= 0xC0 && c <= 0xDF);}
  5919. NK_LIB int nk_to_upper(int c) {return (c >= 'a' && c <= 'z') ? (c - ('a' - 'A')) : c;}
  5920. NK_LIB int nk_to_lower(int c) {return (c >= 'A' && c <= 'Z') ? (c - ('a' + 'A')) : c;}
  5921. NK_LIB void*
  5922. nk_memcopy(void *dst0, const void *src0, nk_size length)
  5923. {
  5924. nk_ptr t;
  5925. char *dst = (char*)dst0;
  5926. const char *src = (const char*)src0;
  5927. if (length == 0 || dst == src)
  5928. goto done;
  5929. #define nk_word int
  5930. #define nk_wsize sizeof(nk_word)
  5931. #define nk_wmask (nk_wsize-1)
  5932. #define NK_TLOOP(s) if (t) NK_TLOOP1(s)
  5933. #define NK_TLOOP1(s) do { s; } while (--t)
  5934. if (dst < src) {
  5935. t = (nk_ptr)src; /* only need low bits */
  5936. if ((t | (nk_ptr)dst) & nk_wmask) {
  5937. if ((t ^ (nk_ptr)dst) & nk_wmask || length < nk_wsize)
  5938. t = length;
  5939. else
  5940. t = nk_wsize - (t & nk_wmask);
  5941. length -= t;
  5942. NK_TLOOP1(*dst++ = *src++);
  5943. }
  5944. t = length / nk_wsize;
  5945. NK_TLOOP(*(nk_word*)(void*)dst = *(const nk_word*)(const void*)src;
  5946. src += nk_wsize; dst += nk_wsize);
  5947. t = length & nk_wmask;
  5948. NK_TLOOP(*dst++ = *src++);
  5949. } else {
  5950. src += length;
  5951. dst += length;
  5952. t = (nk_ptr)src;
  5953. if ((t | (nk_ptr)dst) & nk_wmask) {
  5954. if ((t ^ (nk_ptr)dst) & nk_wmask || length <= nk_wsize)
  5955. t = length;
  5956. else
  5957. t &= nk_wmask;
  5958. length -= t;
  5959. NK_TLOOP1(*--dst = *--src);
  5960. }
  5961. t = length / nk_wsize;
  5962. NK_TLOOP(src -= nk_wsize; dst -= nk_wsize;
  5963. *(nk_word*)(void*)dst = *(const nk_word*)(const void*)src);
  5964. t = length & nk_wmask;
  5965. NK_TLOOP(*--dst = *--src);
  5966. }
  5967. #undef nk_word
  5968. #undef nk_wsize
  5969. #undef nk_wmask
  5970. #undef NK_TLOOP
  5971. #undef NK_TLOOP1
  5972. done:
  5973. return (dst0);
  5974. }
  5975. NK_LIB void
  5976. nk_memset(void *ptr, int c0, nk_size size)
  5977. {
  5978. #define nk_word unsigned
  5979. #define nk_wsize sizeof(nk_word)
  5980. #define nk_wmask (nk_wsize - 1)
  5981. nk_byte *dst = (nk_byte*)ptr;
  5982. unsigned c = 0;
  5983. nk_size t = 0;
  5984. if ((c = (nk_byte)c0) != 0) {
  5985. c = (c << 8) | c; /* at least 16-bits */
  5986. if (sizeof(unsigned int) > 2)
  5987. c = (c << 16) | c; /* at least 32-bits*/
  5988. }
  5989. /* too small of a word count */
  5990. dst = (nk_byte*)ptr;
  5991. if (size < 3 * nk_wsize) {
  5992. while (size--) *dst++ = (nk_byte)c0;
  5993. return;
  5994. }
  5995. /* align destination */
  5996. if ((t = NK_PTR_TO_UINT(dst) & nk_wmask) != 0) {
  5997. t = nk_wsize -t;
  5998. size -= t;
  5999. do {
  6000. *dst++ = (nk_byte)c0;
  6001. } while (--t != 0);
  6002. }
  6003. /* fill word */
  6004. t = size / nk_wsize;
  6005. do {
  6006. *(nk_word*)((void*)dst) = c;
  6007. dst += nk_wsize;
  6008. } while (--t != 0);
  6009. /* fill trailing bytes */
  6010. t = (size & nk_wmask);
  6011. if (t != 0) {
  6012. do {
  6013. *dst++ = (nk_byte)c0;
  6014. } while (--t != 0);
  6015. }
  6016. #undef nk_word
  6017. #undef nk_wsize
  6018. #undef nk_wmask
  6019. }
  6020. NK_LIB void
  6021. nk_zero(void *ptr, nk_size size)
  6022. {
  6023. NK_ASSERT(ptr);
  6024. NK_MEMSET(ptr, 0, size);
  6025. }
  6026. NK_API int
  6027. nk_strlen(const char *str)
  6028. {
  6029. int siz = 0;
  6030. NK_ASSERT(str);
  6031. while (str && *str++ != '\0') siz++;
  6032. return siz;
  6033. }
  6034. NK_API int
  6035. nk_strtoi(const char *str, const char **endptr)
  6036. {
  6037. int neg = 1;
  6038. const char *p = str;
  6039. int value = 0;
  6040. NK_ASSERT(str);
  6041. if (!str) return 0;
  6042. /* skip whitespace */
  6043. while (*p == ' ') p++;
  6044. if (*p == '-') {
  6045. neg = -1;
  6046. p++;
  6047. }
  6048. while (*p && *p >= '0' && *p <= '9') {
  6049. value = value * 10 + (int) (*p - '0');
  6050. p++;
  6051. }
  6052. if (endptr)
  6053. *endptr = p;
  6054. return neg*value;
  6055. }
  6056. NK_API double
  6057. nk_strtod(const char *str, const char **endptr)
  6058. {
  6059. double m;
  6060. double neg = 1.0;
  6061. const char *p = str;
  6062. double value = 0;
  6063. double number = 0;
  6064. NK_ASSERT(str);
  6065. if (!str) return 0;
  6066. /* skip whitespace */
  6067. while (*p == ' ') p++;
  6068. if (*p == '-') {
  6069. neg = -1.0;
  6070. p++;
  6071. }
  6072. while (*p && *p != '.' && *p != 'e') {
  6073. value = value * 10.0 + (double) (*p - '0');
  6074. p++;
  6075. }
  6076. if (*p == '.') {
  6077. p++;
  6078. for(m = 0.1; *p && *p != 'e'; p++ ) {
  6079. value = value + (double) (*p - '0') * m;
  6080. m *= 0.1;
  6081. }
  6082. }
  6083. if (*p == 'e') {
  6084. int i, pow, div;
  6085. p++;
  6086. if (*p == '-') {
  6087. div = nk_true;
  6088. p++;
  6089. } else if (*p == '+') {
  6090. div = nk_false;
  6091. p++;
  6092. } else div = nk_false;
  6093. for (pow = 0; *p; p++)
  6094. pow = pow * 10 + (int) (*p - '0');
  6095. for (m = 1.0, i = 0; i < pow; i++)
  6096. m *= 10.0;
  6097. if (div)
  6098. value /= m;
  6099. else value *= m;
  6100. }
  6101. number = value * neg;
  6102. if (endptr)
  6103. *endptr = p;
  6104. return number;
  6105. }
  6106. NK_API float
  6107. nk_strtof(const char *str, const char **endptr)
  6108. {
  6109. float float_value;
  6110. double double_value;
  6111. double_value = NK_STRTOD(str, endptr);
  6112. float_value = (float)double_value;
  6113. return float_value;
  6114. }
  6115. NK_API int
  6116. nk_stricmp(const char *s1, const char *s2)
  6117. {
  6118. nk_int c1,c2,d;
  6119. do {
  6120. c1 = *s1++;
  6121. c2 = *s2++;
  6122. d = c1 - c2;
  6123. while (d) {
  6124. if (c1 <= 'Z' && c1 >= 'A') {
  6125. d += ('a' - 'A');
  6126. if (!d) break;
  6127. }
  6128. if (c2 <= 'Z' && c2 >= 'A') {
  6129. d -= ('a' - 'A');
  6130. if (!d) break;
  6131. }
  6132. return ((d >= 0) << 1) - 1;
  6133. }
  6134. } while (c1);
  6135. return 0;
  6136. }
  6137. NK_API int
  6138. nk_stricmpn(const char *s1, const char *s2, int n)
  6139. {
  6140. int c1,c2,d;
  6141. NK_ASSERT(n >= 0);
  6142. do {
  6143. c1 = *s1++;
  6144. c2 = *s2++;
  6145. if (!n--) return 0;
  6146. d = c1 - c2;
  6147. while (d) {
  6148. if (c1 <= 'Z' && c1 >= 'A') {
  6149. d += ('a' - 'A');
  6150. if (!d) break;
  6151. }
  6152. if (c2 <= 'Z' && c2 >= 'A') {
  6153. d -= ('a' - 'A');
  6154. if (!d) break;
  6155. }
  6156. return ((d >= 0) << 1) - 1;
  6157. }
  6158. } while (c1);
  6159. return 0;
  6160. }
  6161. NK_INTERN int
  6162. nk_str_match_here(const char *regexp, const char *text)
  6163. {
  6164. if (regexp[0] == '\0')
  6165. return 1;
  6166. if (regexp[1] == '*')
  6167. return nk_str_match_star(regexp[0], regexp+2, text);
  6168. if (regexp[0] == '$' && regexp[1] == '\0')
  6169. return *text == '\0';
  6170. if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text))
  6171. return nk_str_match_here(regexp+1, text+1);
  6172. return 0;
  6173. }
  6174. NK_INTERN int
  6175. nk_str_match_star(int c, const char *regexp, const char *text)
  6176. {
  6177. do {/* a '* matches zero or more instances */
  6178. if (nk_str_match_here(regexp, text))
  6179. return 1;
  6180. } while (*text != '\0' && (*text++ == c || c == '.'));
  6181. return 0;
  6182. }
  6183. NK_API int
  6184. nk_strfilter(const char *text, const char *regexp)
  6185. {
  6186. /*
  6187. c matches any literal character c
  6188. . matches any single character
  6189. ^ matches the beginning of the input string
  6190. $ matches the end of the input string
  6191. * matches zero or more occurrences of the previous character*/
  6192. if (regexp[0] == '^')
  6193. return nk_str_match_here(regexp+1, text);
  6194. do { /* must look even if string is empty */
  6195. if (nk_str_match_here(regexp, text))
  6196. return 1;
  6197. } while (*text++ != '\0');
  6198. return 0;
  6199. }
  6200. NK_API int
  6201. nk_strmatch_fuzzy_text(const char *str, int str_len,
  6202. const char *pattern, int *out_score)
  6203. {
  6204. /* Returns true if each character in pattern is found sequentially within str
  6205. * if found then out_score is also set. Score value has no intrinsic meaning.
  6206. * Range varies with pattern. Can only compare scores with same search pattern. */
  6207. /* bonus for adjacent matches */
  6208. #define NK_ADJACENCY_BONUS 5
  6209. /* bonus if match occurs after a separator */
  6210. #define NK_SEPARATOR_BONUS 10
  6211. /* bonus if match is uppercase and prev is lower */
  6212. #define NK_CAMEL_BONUS 10
  6213. /* penalty applied for every letter in str before the first match */
  6214. #define NK_LEADING_LETTER_PENALTY (-3)
  6215. /* maximum penalty for leading letters */
  6216. #define NK_MAX_LEADING_LETTER_PENALTY (-9)
  6217. /* penalty for every letter that doesn't matter */
  6218. #define NK_UNMATCHED_LETTER_PENALTY (-1)
  6219. /* loop variables */
  6220. int score = 0;
  6221. char const * pattern_iter = pattern;
  6222. int str_iter = 0;
  6223. int prev_matched = nk_false;
  6224. int prev_lower = nk_false;
  6225. /* true so if first letter match gets separator bonus*/
  6226. int prev_separator = nk_true;
  6227. /* use "best" matched letter if multiple string letters match the pattern */
  6228. char const * best_letter = 0;
  6229. int best_letter_score = 0;
  6230. /* loop over strings */
  6231. NK_ASSERT(str);
  6232. NK_ASSERT(pattern);
  6233. if (!str || !str_len || !pattern) return 0;
  6234. while (str_iter < str_len)
  6235. {
  6236. const char pattern_letter = *pattern_iter;
  6237. const char str_letter = str[str_iter];
  6238. int next_match = *pattern_iter != '\0' &&
  6239. nk_to_lower(pattern_letter) == nk_to_lower(str_letter);
  6240. int rematch = best_letter && nk_to_upper(*best_letter) == nk_to_upper(str_letter);
  6241. int advanced = next_match && best_letter;
  6242. int pattern_repeat = best_letter && *pattern_iter != '\0';
  6243. pattern_repeat = pattern_repeat &&
  6244. nk_to_lower(*best_letter) == nk_to_lower(pattern_letter);
  6245. if (advanced || pattern_repeat) {
  6246. score += best_letter_score;
  6247. best_letter = 0;
  6248. best_letter_score = 0;
  6249. }
  6250. if (next_match || rematch)
  6251. {
  6252. int new_score = 0;
  6253. /* Apply penalty for each letter before the first pattern match */
  6254. if (pattern_iter == pattern) {
  6255. int count = (int)(&str[str_iter] - str);
  6256. int penalty = NK_LEADING_LETTER_PENALTY * count;
  6257. if (penalty < NK_MAX_LEADING_LETTER_PENALTY)
  6258. penalty = NK_MAX_LEADING_LETTER_PENALTY;
  6259. score += penalty;
  6260. }
  6261. /* apply bonus for consecutive bonuses */
  6262. if (prev_matched)
  6263. new_score += NK_ADJACENCY_BONUS;
  6264. /* apply bonus for matches after a separator */
  6265. if (prev_separator)
  6266. new_score += NK_SEPARATOR_BONUS;
  6267. /* apply bonus across camel case boundaries */
  6268. if (prev_lower && nk_is_upper(str_letter))
  6269. new_score += NK_CAMEL_BONUS;
  6270. /* update pattern iter IFF the next pattern letter was matched */
  6271. if (next_match)
  6272. ++pattern_iter;
  6273. /* update best letter in str which may be for a "next" letter or a rematch */
  6274. if (new_score >= best_letter_score) {
  6275. /* apply penalty for now skipped letter */
  6276. if (best_letter != 0)
  6277. score += NK_UNMATCHED_LETTER_PENALTY;
  6278. best_letter = &str[str_iter];
  6279. best_letter_score = new_score;
  6280. }
  6281. prev_matched = nk_true;
  6282. } else {
  6283. score += NK_UNMATCHED_LETTER_PENALTY;
  6284. prev_matched = nk_false;
  6285. }
  6286. /* separators should be more easily defined */
  6287. prev_lower = nk_is_lower(str_letter) != 0;
  6288. prev_separator = str_letter == '_' || str_letter == ' ';
  6289. ++str_iter;
  6290. }
  6291. /* apply score for last match */
  6292. if (best_letter)
  6293. score += best_letter_score;
  6294. /* did not match full pattern */
  6295. if (*pattern_iter != '\0')
  6296. return nk_false;
  6297. if (out_score)
  6298. *out_score = score;
  6299. return nk_true;
  6300. }
  6301. NK_API int
  6302. nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score)
  6303. {
  6304. return nk_strmatch_fuzzy_text(str, nk_strlen(str), pattern, out_score);
  6305. }
  6306. NK_LIB int
  6307. nk_string_float_limit(char *string, int prec)
  6308. {
  6309. int dot = 0;
  6310. char *c = string;
  6311. while (*c) {
  6312. if (*c == '.') {
  6313. dot = 1;
  6314. c++;
  6315. continue;
  6316. }
  6317. if (dot == (prec+1)) {
  6318. *c = 0;
  6319. break;
  6320. }
  6321. if (dot > 0) dot++;
  6322. c++;
  6323. }
  6324. return (int)(c - string);
  6325. }
  6326. NK_INTERN void
  6327. nk_strrev_ascii(char *s)
  6328. {
  6329. int len = nk_strlen(s);
  6330. int end = len / 2;
  6331. int i = 0;
  6332. char t;
  6333. for (; i < end; ++i) {
  6334. t = s[i];
  6335. s[i] = s[len - 1 - i];
  6336. s[len -1 - i] = t;
  6337. }
  6338. }
  6339. NK_LIB char*
  6340. nk_itoa(char *s, long n)
  6341. {
  6342. long i = 0;
  6343. if (n == 0) {
  6344. s[i++] = '0';
  6345. s[i] = 0;
  6346. return s;
  6347. }
  6348. if (n < 0) {
  6349. s[i++] = '-';
  6350. n = -n;
  6351. }
  6352. while (n > 0) {
  6353. s[i++] = (char)('0' + (n % 10));
  6354. n /= 10;
  6355. }
  6356. s[i] = 0;
  6357. if (s[0] == '-')
  6358. ++s;
  6359. nk_strrev_ascii(s);
  6360. return s;
  6361. }
  6362. NK_LIB char*
  6363. nk_dtoa(char *s, double n)
  6364. {
  6365. int useExp = 0;
  6366. int digit = 0, m = 0, m1 = 0;
  6367. char *c = s;
  6368. int neg = 0;
  6369. NK_ASSERT(s);
  6370. if (!s) return 0;
  6371. if (n == 0.0) {
  6372. s[0] = '0'; s[1] = '\0';
  6373. return s;
  6374. }
  6375. neg = (n < 0);
  6376. if (neg) n = -n;
  6377. /* calculate magnitude */
  6378. m = nk_log10(n);
  6379. useExp = (m >= 14 || (neg && m >= 9) || m <= -9);
  6380. if (neg) *(c++) = '-';
  6381. /* set up for scientific notation */
  6382. if (useExp) {
  6383. if (m < 0)
  6384. m -= 1;
  6385. n = n / (double)nk_pow(10.0, m);
  6386. m1 = m;
  6387. m = 0;
  6388. }
  6389. if (m < 1.0) {
  6390. m = 0;
  6391. }
  6392. /* convert the number */
  6393. while (n > NK_FLOAT_PRECISION || m >= 0) {
  6394. double weight = nk_pow(10.0, m);
  6395. if (weight > 0) {
  6396. double t = (double)n / weight;
  6397. digit = nk_ifloord(t);
  6398. n -= ((double)digit * weight);
  6399. *(c++) = (char)('0' + (char)digit);
  6400. }
  6401. if (m == 0 && n > 0)
  6402. *(c++) = '.';
  6403. m--;
  6404. }
  6405. if (useExp) {
  6406. /* convert the exponent */
  6407. int i, j;
  6408. *(c++) = 'e';
  6409. if (m1 > 0) {
  6410. *(c++) = '+';
  6411. } else {
  6412. *(c++) = '-';
  6413. m1 = -m1;
  6414. }
  6415. m = 0;
  6416. while (m1 > 0) {
  6417. *(c++) = (char)('0' + (char)(m1 % 10));
  6418. m1 /= 10;
  6419. m++;
  6420. }
  6421. c -= m;
  6422. for (i = 0, j = m-1; i<j; i++, j--) {
  6423. /* swap without temporary */
  6424. c[i] ^= c[j];
  6425. c[j] ^= c[i];
  6426. c[i] ^= c[j];
  6427. }
  6428. c += m;
  6429. }
  6430. *(c) = '\0';
  6431. return s;
  6432. }
  6433. #ifdef NK_INCLUDE_STANDARD_VARARGS
  6434. #ifndef NK_INCLUDE_STANDARD_IO
  6435. NK_INTERN int
  6436. nk_vsnprintf(char *buf, int buf_size, const char *fmt, va_list args)
  6437. {
  6438. enum nk_arg_type {
  6439. NK_ARG_TYPE_CHAR,
  6440. NK_ARG_TYPE_SHORT,
  6441. NK_ARG_TYPE_DEFAULT,
  6442. NK_ARG_TYPE_LONG
  6443. };
  6444. enum nk_arg_flags {
  6445. NK_ARG_FLAG_LEFT = 0x01,
  6446. NK_ARG_FLAG_PLUS = 0x02,
  6447. NK_ARG_FLAG_SPACE = 0x04,
  6448. NK_ARG_FLAG_NUM = 0x10,
  6449. NK_ARG_FLAG_ZERO = 0x20
  6450. };
  6451. char number_buffer[NK_MAX_NUMBER_BUFFER];
  6452. enum nk_arg_type arg_type = NK_ARG_TYPE_DEFAULT;
  6453. int precision = NK_DEFAULT;
  6454. int width = NK_DEFAULT;
  6455. nk_flags flag = 0;
  6456. int len = 0;
  6457. int result = -1;
  6458. const char *iter = fmt;
  6459. NK_ASSERT(buf);
  6460. NK_ASSERT(buf_size);
  6461. if (!buf || !buf_size || !fmt) return 0;
  6462. for (iter = fmt; *iter && len < buf_size; iter++) {
  6463. /* copy all non-format characters */
  6464. while (*iter && (*iter != '%') && (len < buf_size))
  6465. buf[len++] = *iter++;
  6466. if (!(*iter) || len >= buf_size) break;
  6467. iter++;
  6468. /* flag arguments */
  6469. while (*iter) {
  6470. if (*iter == '-') flag |= NK_ARG_FLAG_LEFT;
  6471. else if (*iter == '+') flag |= NK_ARG_FLAG_PLUS;
  6472. else if (*iter == ' ') flag |= NK_ARG_FLAG_SPACE;
  6473. else if (*iter == '#') flag |= NK_ARG_FLAG_NUM;
  6474. else if (*iter == '0') flag |= NK_ARG_FLAG_ZERO;
  6475. else break;
  6476. iter++;
  6477. }
  6478. /* width argument */
  6479. width = NK_DEFAULT;
  6480. if (*iter >= '1' && *iter <= '9') {
  6481. const char *end;
  6482. width = nk_strtoi(iter, &end);
  6483. if (end == iter)
  6484. width = -1;
  6485. else iter = end;
  6486. } else if (*iter == '*') {
  6487. width = va_arg(args, int);
  6488. iter++;
  6489. }
  6490. /* precision argument */
  6491. precision = NK_DEFAULT;
  6492. if (*iter == '.') {
  6493. iter++;
  6494. if (*iter == '*') {
  6495. precision = va_arg(args, int);
  6496. iter++;
  6497. } else {
  6498. const char *end;
  6499. precision = nk_strtoi(iter, &end);
  6500. if (end == iter)
  6501. precision = -1;
  6502. else iter = end;
  6503. }
  6504. }
  6505. /* length modifier */
  6506. if (*iter == 'h') {
  6507. if (*(iter+1) == 'h') {
  6508. arg_type = NK_ARG_TYPE_CHAR;
  6509. iter++;
  6510. } else arg_type = NK_ARG_TYPE_SHORT;
  6511. iter++;
  6512. } else if (*iter == 'l') {
  6513. arg_type = NK_ARG_TYPE_LONG;
  6514. iter++;
  6515. } else arg_type = NK_ARG_TYPE_DEFAULT;
  6516. /* specifier */
  6517. if (*iter == '%') {
  6518. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6519. NK_ASSERT(precision == NK_DEFAULT);
  6520. NK_ASSERT(width == NK_DEFAULT);
  6521. if (len < buf_size)
  6522. buf[len++] = '%';
  6523. } else if (*iter == 's') {
  6524. /* string */
  6525. const char *str = va_arg(args, const char*);
  6526. NK_ASSERT(str != buf && "buffer and argument are not allowed to overlap!");
  6527. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6528. NK_ASSERT(precision == NK_DEFAULT);
  6529. NK_ASSERT(width == NK_DEFAULT);
  6530. if (str == buf) return -1;
  6531. while (str && *str && len < buf_size)
  6532. buf[len++] = *str++;
  6533. } else if (*iter == 'n') {
  6534. /* current length callback */
  6535. signed int *n = va_arg(args, int*);
  6536. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6537. NK_ASSERT(precision == NK_DEFAULT);
  6538. NK_ASSERT(width == NK_DEFAULT);
  6539. if (n) *n = len;
  6540. } else if (*iter == 'c' || *iter == 'i' || *iter == 'd') {
  6541. /* signed integer */
  6542. long value = 0;
  6543. const char *num_iter;
  6544. int num_len, num_print, padding;
  6545. int cur_precision = NK_MAX(precision, 1);
  6546. int cur_width = NK_MAX(width, 0);
  6547. /* retrieve correct value type */
  6548. if (arg_type == NK_ARG_TYPE_CHAR)
  6549. value = (signed char)va_arg(args, int);
  6550. else if (arg_type == NK_ARG_TYPE_SHORT)
  6551. value = (signed short)va_arg(args, int);
  6552. else if (arg_type == NK_ARG_TYPE_LONG)
  6553. value = va_arg(args, signed long);
  6554. else if (*iter == 'c')
  6555. value = (unsigned char)va_arg(args, int);
  6556. else value = va_arg(args, signed int);
  6557. /* convert number to string */
  6558. nk_itoa(number_buffer, value);
  6559. num_len = nk_strlen(number_buffer);
  6560. padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 0);
  6561. if ((flag & NK_ARG_FLAG_PLUS) || (flag & NK_ARG_FLAG_SPACE))
  6562. padding = NK_MAX(padding-1, 0);
  6563. /* fill left padding up to a total of `width` characters */
  6564. if (!(flag & NK_ARG_FLAG_LEFT)) {
  6565. while (padding-- > 0 && (len < buf_size)) {
  6566. if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
  6567. buf[len++] = '0';
  6568. else buf[len++] = ' ';
  6569. }
  6570. }
  6571. /* copy string value representation into buffer */
  6572. if ((flag & NK_ARG_FLAG_PLUS) && value >= 0 && len < buf_size)
  6573. buf[len++] = '+';
  6574. else if ((flag & NK_ARG_FLAG_SPACE) && value >= 0 && len < buf_size)
  6575. buf[len++] = ' ';
  6576. /* fill up to precision number of digits with '0' */
  6577. num_print = NK_MAX(cur_precision, num_len);
  6578. while (precision && (num_print > num_len) && (len < buf_size)) {
  6579. buf[len++] = '0';
  6580. num_print--;
  6581. }
  6582. /* copy string value representation into buffer */
  6583. num_iter = number_buffer;
  6584. while (precision && *num_iter && len < buf_size)
  6585. buf[len++] = *num_iter++;
  6586. /* fill right padding up to width characters */
  6587. if (flag & NK_ARG_FLAG_LEFT) {
  6588. while ((padding-- > 0) && (len < buf_size))
  6589. buf[len++] = ' ';
  6590. }
  6591. } else if (*iter == 'o' || *iter == 'x' || *iter == 'X' || *iter == 'u') {
  6592. /* unsigned integer */
  6593. unsigned long value = 0;
  6594. int num_len = 0, num_print, padding = 0;
  6595. int cur_precision = NK_MAX(precision, 1);
  6596. int cur_width = NK_MAX(width, 0);
  6597. unsigned int base = (*iter == 'o') ? 8: (*iter == 'u')? 10: 16;
  6598. /* print oct/hex/dec value */
  6599. const char *upper_output_format = "0123456789ABCDEF";
  6600. const char *lower_output_format = "0123456789abcdef";
  6601. const char *output_format = (*iter == 'x') ?
  6602. lower_output_format: upper_output_format;
  6603. /* retrieve correct value type */
  6604. if (arg_type == NK_ARG_TYPE_CHAR)
  6605. value = (unsigned char)va_arg(args, int);
  6606. else if (arg_type == NK_ARG_TYPE_SHORT)
  6607. value = (unsigned short)va_arg(args, int);
  6608. else if (arg_type == NK_ARG_TYPE_LONG)
  6609. value = va_arg(args, unsigned long);
  6610. else value = va_arg(args, unsigned int);
  6611. do {
  6612. /* convert decimal number into hex/oct number */
  6613. int digit = output_format[value % base];
  6614. if (num_len < NK_MAX_NUMBER_BUFFER)
  6615. number_buffer[num_len++] = (char)digit;
  6616. value /= base;
  6617. } while (value > 0);
  6618. num_print = NK_MAX(cur_precision, num_len);
  6619. padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 0);
  6620. if (flag & NK_ARG_FLAG_NUM)
  6621. padding = NK_MAX(padding-1, 0);
  6622. /* fill left padding up to a total of `width` characters */
  6623. if (!(flag & NK_ARG_FLAG_LEFT)) {
  6624. while ((padding-- > 0) && (len < buf_size)) {
  6625. if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
  6626. buf[len++] = '0';
  6627. else buf[len++] = ' ';
  6628. }
  6629. }
  6630. /* fill up to precision number of digits */
  6631. if (num_print && (flag & NK_ARG_FLAG_NUM)) {
  6632. if ((*iter == 'o') && (len < buf_size)) {
  6633. buf[len++] = '0';
  6634. } else if ((*iter == 'x') && ((len+1) < buf_size)) {
  6635. buf[len++] = '0';
  6636. buf[len++] = 'x';
  6637. } else if ((*iter == 'X') && ((len+1) < buf_size)) {
  6638. buf[len++] = '0';
  6639. buf[len++] = 'X';
  6640. }
  6641. }
  6642. while (precision && (num_print > num_len) && (len < buf_size)) {
  6643. buf[len++] = '0';
  6644. num_print--;
  6645. }
  6646. /* reverse number direction */
  6647. while (num_len > 0) {
  6648. if (precision && (len < buf_size))
  6649. buf[len++] = number_buffer[num_len-1];
  6650. num_len--;
  6651. }
  6652. /* fill right padding up to width characters */
  6653. if (flag & NK_ARG_FLAG_LEFT) {
  6654. while ((padding-- > 0) && (len < buf_size))
  6655. buf[len++] = ' ';
  6656. }
  6657. } else if (*iter == 'f') {
  6658. /* floating point */
  6659. const char *num_iter;
  6660. int cur_precision = (precision < 0) ? 6: precision;
  6661. int prefix, cur_width = NK_MAX(width, 0);
  6662. double value = va_arg(args, double);
  6663. int num_len = 0, frac_len = 0, dot = 0;
  6664. int padding = 0;
  6665. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6666. NK_DTOA(number_buffer, value);
  6667. num_len = nk_strlen(number_buffer);
  6668. /* calculate padding */
  6669. num_iter = number_buffer;
  6670. while (*num_iter && *num_iter != '.')
  6671. num_iter++;
  6672. prefix = (*num_iter == '.')?(int)(num_iter - number_buffer)+1:0;
  6673. padding = NK_MAX(cur_width - (prefix + NK_MIN(cur_precision, num_len - prefix)) , 0);
  6674. if ((flag & NK_ARG_FLAG_PLUS) || (flag & NK_ARG_FLAG_SPACE))
  6675. padding = NK_MAX(padding-1, 0);
  6676. /* fill left padding up to a total of `width` characters */
  6677. if (!(flag & NK_ARG_FLAG_LEFT)) {
  6678. while (padding-- > 0 && (len < buf_size)) {
  6679. if (flag & NK_ARG_FLAG_ZERO)
  6680. buf[len++] = '0';
  6681. else buf[len++] = ' ';
  6682. }
  6683. }
  6684. /* copy string value representation into buffer */
  6685. num_iter = number_buffer;
  6686. if ((flag & NK_ARG_FLAG_PLUS) && (value >= 0) && (len < buf_size))
  6687. buf[len++] = '+';
  6688. else if ((flag & NK_ARG_FLAG_SPACE) && (value >= 0) && (len < buf_size))
  6689. buf[len++] = ' ';
  6690. while (*num_iter) {
  6691. if (dot) frac_len++;
  6692. if (len < buf_size)
  6693. buf[len++] = *num_iter;
  6694. if (*num_iter == '.') dot = 1;
  6695. if (frac_len >= cur_precision) break;
  6696. num_iter++;
  6697. }
  6698. /* fill number up to precision */
  6699. while (frac_len < cur_precision) {
  6700. if (!dot && len < buf_size) {
  6701. buf[len++] = '.';
  6702. dot = 1;
  6703. }
  6704. if (len < buf_size)
  6705. buf[len++] = '0';
  6706. frac_len++;
  6707. }
  6708. /* fill right padding up to width characters */
  6709. if (flag & NK_ARG_FLAG_LEFT) {
  6710. while ((padding-- > 0) && (len < buf_size))
  6711. buf[len++] = ' ';
  6712. }
  6713. } else {
  6714. /* Specifier not supported: g,G,e,E,p,z */
  6715. NK_ASSERT(0 && "specifier is not supported!");
  6716. return result;
  6717. }
  6718. }
  6719. buf[(len >= buf_size)?(buf_size-1):len] = 0;
  6720. result = (len >= buf_size)?-1:len;
  6721. return result;
  6722. }
  6723. #endif
  6724. NK_LIB int
  6725. nk_strfmt(char *buf, int buf_size, const char *fmt, va_list args)
  6726. {
  6727. int result = -1;
  6728. NK_ASSERT(buf);
  6729. NK_ASSERT(buf_size);
  6730. if (!buf || !buf_size || !fmt) return 0;
  6731. #ifdef NK_INCLUDE_STANDARD_IO
  6732. result = NK_VSNPRINTF(buf, (nk_size)buf_size, fmt, args);
  6733. result = (result >= buf_size) ? -1: result;
  6734. buf[buf_size-1] = 0;
  6735. #else
  6736. result = nk_vsnprintf(buf, buf_size, fmt, args);
  6737. #endif
  6738. return result;
  6739. }
  6740. #endif
  6741. NK_API nk_hash
  6742. nk_murmur_hash(const void * key, int len, nk_hash seed)
  6743. {
  6744. /* 32-Bit MurmurHash3: https://code.google.com/p/smhasher/wiki/MurmurHash3*/
  6745. #define NK_ROTL(x,r) ((x) << (r) | ((x) >> (32 - r)))
  6746. union {const nk_uint *i; const nk_byte *b;} conv = {0};
  6747. const nk_byte *data = (const nk_byte*)key;
  6748. const int nblocks = len/4;
  6749. nk_uint h1 = seed;
  6750. const nk_uint c1 = 0xcc9e2d51;
  6751. const nk_uint c2 = 0x1b873593;
  6752. const nk_byte *tail;
  6753. const nk_uint *blocks;
  6754. nk_uint k1;
  6755. int i;
  6756. /* body */
  6757. if (!key) return 0;
  6758. conv.b = (data + nblocks*4);
  6759. blocks = (const nk_uint*)conv.i;
  6760. for (i = -nblocks; i; ++i) {
  6761. k1 = blocks[i];
  6762. k1 *= c1;
  6763. k1 = NK_ROTL(k1,15);
  6764. k1 *= c2;
  6765. h1 ^= k1;
  6766. h1 = NK_ROTL(h1,13);
  6767. h1 = h1*5+0xe6546b64;
  6768. }
  6769. /* tail */
  6770. tail = (const nk_byte*)(data + nblocks*4);
  6771. k1 = 0;
  6772. switch (len & 3) {
  6773. case 3: k1 ^= (nk_uint)(tail[2] << 16); /* fallthrough */
  6774. case 2: k1 ^= (nk_uint)(tail[1] << 8u); /* fallthrough */
  6775. case 1: k1 ^= tail[0];
  6776. k1 *= c1;
  6777. k1 = NK_ROTL(k1,15);
  6778. k1 *= c2;
  6779. h1 ^= k1;
  6780. break;
  6781. default: break;
  6782. }
  6783. /* finalization */
  6784. h1 ^= (nk_uint)len;
  6785. /* fmix32 */
  6786. h1 ^= h1 >> 16;
  6787. h1 *= 0x85ebca6b;
  6788. h1 ^= h1 >> 13;
  6789. h1 *= 0xc2b2ae35;
  6790. h1 ^= h1 >> 16;
  6791. #undef NK_ROTL
  6792. return h1;
  6793. }
  6794. #ifdef NK_INCLUDE_STANDARD_IO
  6795. NK_LIB char*
  6796. nk_file_load(const char* path, nk_size* siz, struct nk_allocator *alloc)
  6797. {
  6798. char *buf;
  6799. FILE *fd;
  6800. long ret;
  6801. NK_ASSERT(path);
  6802. NK_ASSERT(siz);
  6803. NK_ASSERT(alloc);
  6804. if (!path || !siz || !alloc)
  6805. return 0;
  6806. fd = fopen(path, "rb");
  6807. if (!fd) return 0;
  6808. fseek(fd, 0, SEEK_END);
  6809. ret = ftell(fd);
  6810. if (ret < 0) {
  6811. fclose(fd);
  6812. return 0;
  6813. }
  6814. *siz = (nk_size)ret;
  6815. fseek(fd, 0, SEEK_SET);
  6816. buf = (char*)alloc->alloc(alloc->userdata,0, *siz);
  6817. NK_ASSERT(buf);
  6818. if (!buf) {
  6819. fclose(fd);
  6820. return 0;
  6821. }
  6822. *siz = (nk_size)fread(buf, 1,*siz, fd);
  6823. fclose(fd);
  6824. return buf;
  6825. }
  6826. #endif
  6827. NK_LIB int
  6828. nk_text_clamp(const struct nk_user_font *font, const char *text,
  6829. int text_len, float space, int *glyphs, float *text_width,
  6830. nk_rune *sep_list, int sep_count)
  6831. {
  6832. int i = 0;
  6833. int glyph_len = 0;
  6834. float last_width = 0;
  6835. nk_rune unicode = 0;
  6836. float width = 0;
  6837. int len = 0;
  6838. int g = 0;
  6839. float s;
  6840. int sep_len = 0;
  6841. int sep_g = 0;
  6842. float sep_width = 0;
  6843. sep_count = NK_MAX(sep_count,0);
  6844. glyph_len = nk_utf_decode(text, &unicode, text_len);
  6845. while (glyph_len && (width < space) && (len < text_len)) {
  6846. len += glyph_len;
  6847. s = font->width(font->userdata, font->height, text, len);
  6848. for (i = 0; i < sep_count; ++i) {
  6849. if (unicode != sep_list[i]) continue;
  6850. sep_width = last_width = width;
  6851. sep_g = g+1;
  6852. sep_len = len;
  6853. break;
  6854. }
  6855. if (i == sep_count){
  6856. last_width = sep_width = width;
  6857. sep_g = g+1;
  6858. }
  6859. width = s;
  6860. glyph_len = nk_utf_decode(&text[len], &unicode, text_len - len);
  6861. g++;
  6862. }
  6863. if (len >= text_len) {
  6864. *glyphs = g;
  6865. *text_width = last_width;
  6866. return len;
  6867. } else {
  6868. *glyphs = sep_g;
  6869. *text_width = sep_width;
  6870. return (!sep_len) ? len: sep_len;
  6871. }
  6872. }
  6873. NK_LIB struct nk_vec2
  6874. nk_text_calculate_text_bounds(const struct nk_user_font *font,
  6875. const char *begin, int byte_len, float row_height, const char **remaining,
  6876. struct nk_vec2 *out_offset, int *glyphs, int op)
  6877. {
  6878. float line_height = row_height;
  6879. struct nk_vec2 text_size = nk_vec2(0,0);
  6880. float line_width = 0.0f;
  6881. float glyph_width;
  6882. int glyph_len = 0;
  6883. nk_rune unicode = 0;
  6884. int text_len = 0;
  6885. if (!begin || byte_len <= 0 || !font)
  6886. return nk_vec2(0,row_height);
  6887. glyph_len = nk_utf_decode(begin, &unicode, byte_len);
  6888. if (!glyph_len) return text_size;
  6889. glyph_width = font->width(font->userdata, font->height, begin, glyph_len);
  6890. *glyphs = 0;
  6891. while ((text_len < byte_len) && glyph_len) {
  6892. if (unicode == '\n') {
  6893. text_size.x = NK_MAX(text_size.x, line_width);
  6894. text_size.y += line_height;
  6895. line_width = 0;
  6896. *glyphs+=1;
  6897. if (op == NK_STOP_ON_NEW_LINE)
  6898. break;
  6899. text_len++;
  6900. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  6901. continue;
  6902. }
  6903. if (unicode == '\r') {
  6904. text_len++;
  6905. *glyphs+=1;
  6906. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  6907. continue;
  6908. }
  6909. *glyphs = *glyphs + 1;
  6910. text_len += glyph_len;
  6911. line_width += (float)glyph_width;
  6912. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  6913. glyph_width = font->width(font->userdata, font->height, begin+text_len, glyph_len);
  6914. continue;
  6915. }
  6916. if (text_size.x < line_width)
  6917. text_size.x = line_width;
  6918. if (out_offset)
  6919. *out_offset = nk_vec2(line_width, text_size.y + line_height);
  6920. if (line_width > 0 || text_size.y == 0.0f)
  6921. text_size.y += line_height;
  6922. if (remaining)
  6923. *remaining = begin+text_len;
  6924. return text_size;
  6925. }
  6926. /* ==============================================================
  6927. *
  6928. * COLOR
  6929. *
  6930. * ===============================================================*/
  6931. NK_INTERN int
  6932. nk_parse_hex(const char *p, int length)
  6933. {
  6934. int i = 0;
  6935. int len = 0;
  6936. while (len < length) {
  6937. i <<= 4;
  6938. if (p[len] >= 'a' && p[len] <= 'f')
  6939. i += ((p[len] - 'a') + 10);
  6940. else if (p[len] >= 'A' && p[len] <= 'F')
  6941. i += ((p[len] - 'A') + 10);
  6942. else i += (p[len] - '0');
  6943. len++;
  6944. }
  6945. return i;
  6946. }
  6947. NK_API struct nk_color
  6948. nk_rgba(int r, int g, int b, int a)
  6949. {
  6950. struct nk_color ret;
  6951. ret.r = (nk_byte)NK_CLAMP(0, r, 255);
  6952. ret.g = (nk_byte)NK_CLAMP(0, g, 255);
  6953. ret.b = (nk_byte)NK_CLAMP(0, b, 255);
  6954. ret.a = (nk_byte)NK_CLAMP(0, a, 255);
  6955. return ret;
  6956. }
  6957. NK_API struct nk_color
  6958. nk_rgb_hex(const char *rgb)
  6959. {
  6960. struct nk_color col;
  6961. const char *c = rgb;
  6962. if (*c == '#') c++;
  6963. col.r = (nk_byte)nk_parse_hex(c, 2);
  6964. col.g = (nk_byte)nk_parse_hex(c+2, 2);
  6965. col.b = (nk_byte)nk_parse_hex(c+4, 2);
  6966. col.a = 255;
  6967. return col;
  6968. }
  6969. NK_API struct nk_color
  6970. nk_rgba_hex(const char *rgb)
  6971. {
  6972. struct nk_color col;
  6973. const char *c = rgb;
  6974. if (*c == '#') c++;
  6975. col.r = (nk_byte)nk_parse_hex(c, 2);
  6976. col.g = (nk_byte)nk_parse_hex(c+2, 2);
  6977. col.b = (nk_byte)nk_parse_hex(c+4, 2);
  6978. col.a = (nk_byte)nk_parse_hex(c+6, 2);
  6979. return col;
  6980. }
  6981. NK_API void
  6982. nk_color_hex_rgba(char *output, struct nk_color col)
  6983. {
  6984. #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
  6985. output[0] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
  6986. output[1] = (char)NK_TO_HEX((col.r & 0x0F));
  6987. output[2] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
  6988. output[3] = (char)NK_TO_HEX((col.g & 0x0F));
  6989. output[4] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
  6990. output[5] = (char)NK_TO_HEX((col.b & 0x0F));
  6991. output[6] = (char)NK_TO_HEX((col.a & 0xF0) >> 4);
  6992. output[7] = (char)NK_TO_HEX((col.a & 0x0F));
  6993. output[8] = '\0';
  6994. #undef NK_TO_HEX
  6995. }
  6996. NK_API void
  6997. nk_color_hex_rgb(char *output, struct nk_color col)
  6998. {
  6999. #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
  7000. output[0] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
  7001. output[1] = (char)NK_TO_HEX((col.r & 0x0F));
  7002. output[2] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
  7003. output[3] = (char)NK_TO_HEX((col.g & 0x0F));
  7004. output[4] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
  7005. output[5] = (char)NK_TO_HEX((col.b & 0x0F));
  7006. output[6] = '\0';
  7007. #undef NK_TO_HEX
  7008. }
  7009. NK_API struct nk_color
  7010. nk_rgba_iv(const int *c)
  7011. {
  7012. return nk_rgba(c[0], c[1], c[2], c[3]);
  7013. }
  7014. NK_API struct nk_color
  7015. nk_rgba_bv(const nk_byte *c)
  7016. {
  7017. return nk_rgba(c[0], c[1], c[2], c[3]);
  7018. }
  7019. NK_API struct nk_color
  7020. nk_rgb(int r, int g, int b)
  7021. {
  7022. struct nk_color ret;
  7023. ret.r = (nk_byte)NK_CLAMP(0, r, 255);
  7024. ret.g = (nk_byte)NK_CLAMP(0, g, 255);
  7025. ret.b = (nk_byte)NK_CLAMP(0, b, 255);
  7026. ret.a = (nk_byte)255;
  7027. return ret;
  7028. }
  7029. NK_API struct nk_color
  7030. nk_rgb_iv(const int *c)
  7031. {
  7032. return nk_rgb(c[0], c[1], c[2]);
  7033. }
  7034. NK_API struct nk_color
  7035. nk_rgb_bv(const nk_byte* c)
  7036. {
  7037. return nk_rgb(c[0], c[1], c[2]);
  7038. }
  7039. NK_API struct nk_color
  7040. nk_rgba_u32(nk_uint in)
  7041. {
  7042. struct nk_color ret;
  7043. ret.r = (in & 0xFF);
  7044. ret.g = ((in >> 8) & 0xFF);
  7045. ret.b = ((in >> 16) & 0xFF);
  7046. ret.a = (nk_byte)((in >> 24) & 0xFF);
  7047. return ret;
  7048. }
  7049. NK_API struct nk_color
  7050. nk_rgba_f(float r, float g, float b, float a)
  7051. {
  7052. struct nk_color ret;
  7053. ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
  7054. ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
  7055. ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
  7056. ret.a = (nk_byte)(NK_SATURATE(a) * 255.0f);
  7057. return ret;
  7058. }
  7059. NK_API struct nk_color
  7060. nk_rgba_fv(const float *c)
  7061. {
  7062. return nk_rgba_f(c[0], c[1], c[2], c[3]);
  7063. }
  7064. NK_API struct nk_color
  7065. nk_rgba_cf(struct nk_colorf c)
  7066. {
  7067. return nk_rgba_f(c.r, c.g, c.b, c.a);
  7068. }
  7069. NK_API struct nk_color
  7070. nk_rgb_f(float r, float g, float b)
  7071. {
  7072. struct nk_color ret;
  7073. ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
  7074. ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
  7075. ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
  7076. ret.a = 255;
  7077. return ret;
  7078. }
  7079. NK_API struct nk_color
  7080. nk_rgb_fv(const float *c)
  7081. {
  7082. return nk_rgb_f(c[0], c[1], c[2]);
  7083. }
  7084. NK_API struct nk_color
  7085. nk_rgb_cf(struct nk_colorf c)
  7086. {
  7087. return nk_rgb_f(c.r, c.g, c.b);
  7088. }
  7089. NK_API struct nk_color
  7090. nk_hsv(int h, int s, int v)
  7091. {
  7092. return nk_hsva(h, s, v, 255);
  7093. }
  7094. NK_API struct nk_color
  7095. nk_hsv_iv(const int *c)
  7096. {
  7097. return nk_hsv(c[0], c[1], c[2]);
  7098. }
  7099. NK_API struct nk_color
  7100. nk_hsv_bv(const nk_byte *c)
  7101. {
  7102. return nk_hsv(c[0], c[1], c[2]);
  7103. }
  7104. NK_API struct nk_color
  7105. nk_hsv_f(float h, float s, float v)
  7106. {
  7107. return nk_hsva_f(h, s, v, 1.0f);
  7108. }
  7109. NK_API struct nk_color
  7110. nk_hsv_fv(const float *c)
  7111. {
  7112. return nk_hsv_f(c[0], c[1], c[2]);
  7113. }
  7114. NK_API struct nk_color
  7115. nk_hsva(int h, int s, int v, int a)
  7116. {
  7117. float hf = ((float)NK_CLAMP(0, h, 255)) / 255.0f;
  7118. float sf = ((float)NK_CLAMP(0, s, 255)) / 255.0f;
  7119. float vf = ((float)NK_CLAMP(0, v, 255)) / 255.0f;
  7120. float af = ((float)NK_CLAMP(0, a, 255)) / 255.0f;
  7121. return nk_hsva_f(hf, sf, vf, af);
  7122. }
  7123. NK_API struct nk_color
  7124. nk_hsva_iv(const int *c)
  7125. {
  7126. return nk_hsva(c[0], c[1], c[2], c[3]);
  7127. }
  7128. NK_API struct nk_color
  7129. nk_hsva_bv(const nk_byte *c)
  7130. {
  7131. return nk_hsva(c[0], c[1], c[2], c[3]);
  7132. }
  7133. NK_API struct nk_colorf
  7134. nk_hsva_colorf(float h, float s, float v, float a)
  7135. {
  7136. int i;
  7137. float p, q, t, f;
  7138. struct nk_colorf out = {0,0,0,0};
  7139. if (s <= 0.0f) {
  7140. out.r = v; out.g = v; out.b = v; out.a = a;
  7141. return out;
  7142. }
  7143. h = h / (60.0f/360.0f);
  7144. i = (int)h;
  7145. f = h - (float)i;
  7146. p = v * (1.0f - s);
  7147. q = v * (1.0f - (s * f));
  7148. t = v * (1.0f - s * (1.0f - f));
  7149. switch (i) {
  7150. case 0: default: out.r = v; out.g = t; out.b = p; break;
  7151. case 1: out.r = q; out.g = v; out.b = p; break;
  7152. case 2: out.r = p; out.g = v; out.b = t; break;
  7153. case 3: out.r = p; out.g = q; out.b = v; break;
  7154. case 4: out.r = t; out.g = p; out.b = v; break;
  7155. case 5: out.r = v; out.g = p; out.b = q; break;}
  7156. out.a = a;
  7157. return out;
  7158. }
  7159. NK_API struct nk_colorf
  7160. nk_hsva_colorfv(float *c)
  7161. {
  7162. return nk_hsva_colorf(c[0], c[1], c[2], c[3]);
  7163. }
  7164. NK_API struct nk_color
  7165. nk_hsva_f(float h, float s, float v, float a)
  7166. {
  7167. struct nk_colorf c = nk_hsva_colorf(h, s, v, a);
  7168. return nk_rgba_f(c.r, c.g, c.b, c.a);
  7169. }
  7170. NK_API struct nk_color
  7171. nk_hsva_fv(const float *c)
  7172. {
  7173. return nk_hsva_f(c[0], c[1], c[2], c[3]);
  7174. }
  7175. NK_API nk_uint
  7176. nk_color_u32(struct nk_color in)
  7177. {
  7178. nk_uint out = (nk_uint)in.r;
  7179. out |= ((nk_uint)in.g << 8);
  7180. out |= ((nk_uint)in.b << 16);
  7181. out |= ((nk_uint)in.a << 24);
  7182. return out;
  7183. }
  7184. NK_API void
  7185. nk_color_f(float *r, float *g, float *b, float *a, struct nk_color in)
  7186. {
  7187. NK_STORAGE const float s = 1.0f/255.0f;
  7188. *r = (float)in.r * s;
  7189. *g = (float)in.g * s;
  7190. *b = (float)in.b * s;
  7191. *a = (float)in.a * s;
  7192. }
  7193. NK_API void
  7194. nk_color_fv(float *c, struct nk_color in)
  7195. {
  7196. nk_color_f(&c[0], &c[1], &c[2], &c[3], in);
  7197. }
  7198. NK_API struct nk_colorf
  7199. nk_color_cf(struct nk_color in)
  7200. {
  7201. struct nk_colorf o;
  7202. nk_color_f(&o.r, &o.g, &o.b, &o.a, in);
  7203. return o;
  7204. }
  7205. NK_API void
  7206. nk_color_d(double *r, double *g, double *b, double *a, struct nk_color in)
  7207. {
  7208. NK_STORAGE const double s = 1.0/255.0;
  7209. *r = (double)in.r * s;
  7210. *g = (double)in.g * s;
  7211. *b = (double)in.b * s;
  7212. *a = (double)in.a * s;
  7213. }
  7214. NK_API void
  7215. nk_color_dv(double *c, struct nk_color in)
  7216. {
  7217. nk_color_d(&c[0], &c[1], &c[2], &c[3], in);
  7218. }
  7219. NK_API void
  7220. nk_color_hsv_f(float *out_h, float *out_s, float *out_v, struct nk_color in)
  7221. {
  7222. float a;
  7223. nk_color_hsva_f(out_h, out_s, out_v, &a, in);
  7224. }
  7225. NK_API void
  7226. nk_color_hsv_fv(float *out, struct nk_color in)
  7227. {
  7228. float a;
  7229. nk_color_hsva_f(&out[0], &out[1], &out[2], &a, in);
  7230. }
  7231. NK_API void
  7232. nk_colorf_hsva_f(float *out_h, float *out_s,
  7233. float *out_v, float *out_a, struct nk_colorf in)
  7234. {
  7235. float chroma;
  7236. float K = 0.0f;
  7237. if (in.g < in.b) {
  7238. const float t = in.g; in.g = in.b; in.b = t;
  7239. K = -1.f;
  7240. }
  7241. if (in.r < in.g) {
  7242. const float t = in.r; in.r = in.g; in.g = t;
  7243. K = -2.f/6.0f - K;
  7244. }
  7245. chroma = in.r - ((in.g < in.b) ? in.g: in.b);
  7246. *out_h = NK_ABS(K + (in.g - in.b)/(6.0f * chroma + 1e-20f));
  7247. *out_s = chroma / (in.r + 1e-20f);
  7248. *out_v = in.r;
  7249. *out_a = in.a;
  7250. }
  7251. NK_API void
  7252. nk_colorf_hsva_fv(float *hsva, struct nk_colorf in)
  7253. {
  7254. nk_colorf_hsva_f(&hsva[0], &hsva[1], &hsva[2], &hsva[3], in);
  7255. }
  7256. NK_API void
  7257. nk_color_hsva_f(float *out_h, float *out_s,
  7258. float *out_v, float *out_a, struct nk_color in)
  7259. {
  7260. struct nk_colorf col;
  7261. nk_color_f(&col.r,&col.g,&col.b,&col.a, in);
  7262. nk_colorf_hsva_f(out_h, out_s, out_v, out_a, col);
  7263. }
  7264. NK_API void
  7265. nk_color_hsva_fv(float *out, struct nk_color in)
  7266. {
  7267. nk_color_hsva_f(&out[0], &out[1], &out[2], &out[3], in);
  7268. }
  7269. NK_API void
  7270. nk_color_hsva_i(int *out_h, int *out_s, int *out_v,
  7271. int *out_a, struct nk_color in)
  7272. {
  7273. float h,s,v,a;
  7274. nk_color_hsva_f(&h, &s, &v, &a, in);
  7275. *out_h = (nk_byte)(h * 255.0f);
  7276. *out_s = (nk_byte)(s * 255.0f);
  7277. *out_v = (nk_byte)(v * 255.0f);
  7278. *out_a = (nk_byte)(a * 255.0f);
  7279. }
  7280. NK_API void
  7281. nk_color_hsva_iv(int *out, struct nk_color in)
  7282. {
  7283. nk_color_hsva_i(&out[0], &out[1], &out[2], &out[3], in);
  7284. }
  7285. NK_API void
  7286. nk_color_hsva_bv(nk_byte *out, struct nk_color in)
  7287. {
  7288. int tmp[4];
  7289. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  7290. out[0] = (nk_byte)tmp[0];
  7291. out[1] = (nk_byte)tmp[1];
  7292. out[2] = (nk_byte)tmp[2];
  7293. out[3] = (nk_byte)tmp[3];
  7294. }
  7295. NK_API void
  7296. nk_color_hsva_b(nk_byte *h, nk_byte *s, nk_byte *v, nk_byte *a, struct nk_color in)
  7297. {
  7298. int tmp[4];
  7299. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  7300. *h = (nk_byte)tmp[0];
  7301. *s = (nk_byte)tmp[1];
  7302. *v = (nk_byte)tmp[2];
  7303. *a = (nk_byte)tmp[3];
  7304. }
  7305. NK_API void
  7306. nk_color_hsv_i(int *out_h, int *out_s, int *out_v, struct nk_color in)
  7307. {
  7308. int a;
  7309. nk_color_hsva_i(out_h, out_s, out_v, &a, in);
  7310. }
  7311. NK_API void
  7312. nk_color_hsv_b(nk_byte *out_h, nk_byte *out_s, nk_byte *out_v, struct nk_color in)
  7313. {
  7314. int tmp[4];
  7315. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  7316. *out_h = (nk_byte)tmp[0];
  7317. *out_s = (nk_byte)tmp[1];
  7318. *out_v = (nk_byte)tmp[2];
  7319. }
  7320. NK_API void
  7321. nk_color_hsv_iv(int *out, struct nk_color in)
  7322. {
  7323. nk_color_hsv_i(&out[0], &out[1], &out[2], in);
  7324. }
  7325. NK_API void
  7326. nk_color_hsv_bv(nk_byte *out, struct nk_color in)
  7327. {
  7328. int tmp[4];
  7329. nk_color_hsv_i(&tmp[0], &tmp[1], &tmp[2], in);
  7330. out[0] = (nk_byte)tmp[0];
  7331. out[1] = (nk_byte)tmp[1];
  7332. out[2] = (nk_byte)tmp[2];
  7333. }
  7334. /* ===============================================================
  7335. *
  7336. * UTF-8
  7337. *
  7338. * ===============================================================*/
  7339. NK_GLOBAL const nk_byte nk_utfbyte[NK_UTF_SIZE+1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
  7340. NK_GLOBAL const nk_byte nk_utfmask[NK_UTF_SIZE+1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
  7341. NK_GLOBAL const nk_uint nk_utfmin[NK_UTF_SIZE+1] = {0, 0, 0x80, 0x800, 0x10000};
  7342. NK_GLOBAL const nk_uint nk_utfmax[NK_UTF_SIZE+1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
  7343. NK_INTERN int
  7344. nk_utf_validate(nk_rune *u, int i)
  7345. {
  7346. NK_ASSERT(u);
  7347. if (!u) return 0;
  7348. if (!NK_BETWEEN(*u, nk_utfmin[i], nk_utfmax[i]) ||
  7349. NK_BETWEEN(*u, 0xD800, 0xDFFF))
  7350. *u = NK_UTF_INVALID;
  7351. for (i = 1; *u > nk_utfmax[i]; ++i);
  7352. return i;
  7353. }
  7354. NK_INTERN nk_rune
  7355. nk_utf_decode_byte(char c, int *i)
  7356. {
  7357. NK_ASSERT(i);
  7358. if (!i) return 0;
  7359. for(*i = 0; *i < (int)NK_LEN(nk_utfmask); ++(*i)) {
  7360. if (((nk_byte)c & nk_utfmask[*i]) == nk_utfbyte[*i])
  7361. return (nk_byte)(c & ~nk_utfmask[*i]);
  7362. }
  7363. return 0;
  7364. }
  7365. NK_API int
  7366. nk_utf_decode(const char *c, nk_rune *u, int clen)
  7367. {
  7368. int i, j, len, type=0;
  7369. nk_rune udecoded;
  7370. NK_ASSERT(c);
  7371. NK_ASSERT(u);
  7372. if (!c || !u) return 0;
  7373. if (!clen) return 0;
  7374. *u = NK_UTF_INVALID;
  7375. udecoded = nk_utf_decode_byte(c[0], &len);
  7376. if (!NK_BETWEEN(len, 1, NK_UTF_SIZE))
  7377. return 1;
  7378. for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
  7379. udecoded = (udecoded << 6) | nk_utf_decode_byte(c[i], &type);
  7380. if (type != 0)
  7381. return j;
  7382. }
  7383. if (j < len)
  7384. return 0;
  7385. *u = udecoded;
  7386. nk_utf_validate(u, len);
  7387. return len;
  7388. }
  7389. NK_INTERN char
  7390. nk_utf_encode_byte(nk_rune u, int i)
  7391. {
  7392. return (char)((nk_utfbyte[i]) | ((nk_byte)u & ~nk_utfmask[i]));
  7393. }
  7394. NK_API int
  7395. nk_utf_encode(nk_rune u, char *c, int clen)
  7396. {
  7397. int len, i;
  7398. len = nk_utf_validate(&u, 0);
  7399. if (clen < len || !len || len > NK_UTF_SIZE)
  7400. return 0;
  7401. for (i = len - 1; i != 0; --i) {
  7402. c[i] = nk_utf_encode_byte(u, 0);
  7403. u >>= 6;
  7404. }
  7405. c[0] = nk_utf_encode_byte(u, len);
  7406. return len;
  7407. }
  7408. NK_API int
  7409. nk_utf_len(const char *str, int len)
  7410. {
  7411. const char *text;
  7412. int glyphs = 0;
  7413. int text_len;
  7414. int glyph_len;
  7415. int src_len = 0;
  7416. nk_rune unicode;
  7417. NK_ASSERT(str);
  7418. if (!str || !len) return 0;
  7419. text = str;
  7420. text_len = len;
  7421. glyph_len = nk_utf_decode(text, &unicode, text_len);
  7422. while (glyph_len && src_len < len) {
  7423. glyphs++;
  7424. src_len = src_len + glyph_len;
  7425. glyph_len = nk_utf_decode(text + src_len, &unicode, text_len - src_len);
  7426. }
  7427. return glyphs;
  7428. }
  7429. NK_API const char*
  7430. nk_utf_at(const char *buffer, int length, int index,
  7431. nk_rune *unicode, int *len)
  7432. {
  7433. int i = 0;
  7434. int src_len = 0;
  7435. int glyph_len = 0;
  7436. const char *text;
  7437. int text_len;
  7438. NK_ASSERT(buffer);
  7439. NK_ASSERT(unicode);
  7440. NK_ASSERT(len);
  7441. if (!buffer || !unicode || !len) return 0;
  7442. if (index < 0) {
  7443. *unicode = NK_UTF_INVALID;
  7444. *len = 0;
  7445. return 0;
  7446. }
  7447. text = buffer;
  7448. text_len = length;
  7449. glyph_len = nk_utf_decode(text, unicode, text_len);
  7450. while (glyph_len) {
  7451. if (i == index) {
  7452. *len = glyph_len;
  7453. break;
  7454. }
  7455. i++;
  7456. src_len = src_len + glyph_len;
  7457. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  7458. }
  7459. if (i != index) return 0;
  7460. return buffer + src_len;
  7461. }
  7462. /* ==============================================================
  7463. *
  7464. * BUFFER
  7465. *
  7466. * ===============================================================*/
  7467. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  7468. NK_LIB void*
  7469. nk_malloc(nk_handle unused, void *old,nk_size size)
  7470. {
  7471. NK_UNUSED(unused);
  7472. NK_UNUSED(old);
  7473. return malloc(size);
  7474. }
  7475. NK_LIB void
  7476. nk_mfree(nk_handle unused, void *ptr)
  7477. {
  7478. NK_UNUSED(unused);
  7479. free(ptr);
  7480. }
  7481. NK_API void
  7482. nk_buffer_init_default(struct nk_buffer *buffer)
  7483. {
  7484. struct nk_allocator alloc;
  7485. alloc.userdata.ptr = 0;
  7486. alloc.alloc = nk_malloc;
  7487. alloc.free = nk_mfree;
  7488. nk_buffer_init(buffer, &alloc, NK_BUFFER_DEFAULT_INITIAL_SIZE);
  7489. }
  7490. #endif
  7491. NK_API void
  7492. nk_buffer_init(struct nk_buffer *b, const struct nk_allocator *a,
  7493. nk_size initial_size)
  7494. {
  7495. NK_ASSERT(b);
  7496. NK_ASSERT(a);
  7497. NK_ASSERT(initial_size);
  7498. if (!b || !a || !initial_size) return;
  7499. nk_zero(b, sizeof(*b));
  7500. b->type = NK_BUFFER_DYNAMIC;
  7501. b->memory.ptr = a->alloc(a->userdata,0, initial_size);
  7502. b->memory.size = initial_size;
  7503. b->size = initial_size;
  7504. b->grow_factor = 2.0f;
  7505. b->pool = *a;
  7506. }
  7507. NK_API void
  7508. nk_buffer_init_fixed(struct nk_buffer *b, void *m, nk_size size)
  7509. {
  7510. NK_ASSERT(b);
  7511. NK_ASSERT(m);
  7512. NK_ASSERT(size);
  7513. if (!b || !m || !size) return;
  7514. nk_zero(b, sizeof(*b));
  7515. b->type = NK_BUFFER_FIXED;
  7516. b->memory.ptr = m;
  7517. b->memory.size = size;
  7518. b->size = size;
  7519. }
  7520. NK_LIB void*
  7521. nk_buffer_align(void *unaligned,
  7522. nk_size align, nk_size *alignment,
  7523. enum nk_buffer_allocation_type type)
  7524. {
  7525. void *memory = 0;
  7526. switch (type) {
  7527. default:
  7528. case NK_BUFFER_MAX:
  7529. case NK_BUFFER_FRONT:
  7530. if (align) {
  7531. memory = NK_ALIGN_PTR(unaligned, align);
  7532. *alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
  7533. } else {
  7534. memory = unaligned;
  7535. *alignment = 0;
  7536. }
  7537. break;
  7538. case NK_BUFFER_BACK:
  7539. if (align) {
  7540. memory = NK_ALIGN_PTR_BACK(unaligned, align);
  7541. *alignment = (nk_size)((nk_byte*)unaligned - (nk_byte*)memory);
  7542. } else {
  7543. memory = unaligned;
  7544. *alignment = 0;
  7545. }
  7546. break;
  7547. }
  7548. return memory;
  7549. }
  7550. NK_LIB void*
  7551. nk_buffer_realloc(struct nk_buffer *b, nk_size capacity, nk_size *size)
  7552. {
  7553. void *temp;
  7554. nk_size buffer_size;
  7555. NK_ASSERT(b);
  7556. NK_ASSERT(size);
  7557. if (!b || !size || !b->pool.alloc || !b->pool.free)
  7558. return 0;
  7559. buffer_size = b->memory.size;
  7560. temp = b->pool.alloc(b->pool.userdata, b->memory.ptr, capacity);
  7561. NK_ASSERT(temp);
  7562. if (!temp) return 0;
  7563. *size = capacity;
  7564. if (temp != b->memory.ptr) {
  7565. NK_MEMCPY(temp, b->memory.ptr, buffer_size);
  7566. b->pool.free(b->pool.userdata, b->memory.ptr);
  7567. }
  7568. if (b->size == buffer_size) {
  7569. /* no back buffer so just set correct size */
  7570. b->size = capacity;
  7571. return temp;
  7572. } else {
  7573. /* copy back buffer to the end of the new buffer */
  7574. void *dst, *src;
  7575. nk_size back_size;
  7576. back_size = buffer_size - b->size;
  7577. dst = nk_ptr_add(void, temp, capacity - back_size);
  7578. src = nk_ptr_add(void, temp, b->size);
  7579. NK_MEMCPY(dst, src, back_size);
  7580. b->size = capacity - back_size;
  7581. }
  7582. return temp;
  7583. }
  7584. NK_LIB void*
  7585. nk_buffer_alloc(struct nk_buffer *b, enum nk_buffer_allocation_type type,
  7586. nk_size size, nk_size align)
  7587. {
  7588. int full;
  7589. nk_size alignment;
  7590. void *unaligned;
  7591. void *memory;
  7592. NK_ASSERT(b);
  7593. NK_ASSERT(size);
  7594. if (!b || !size) return 0;
  7595. b->needed += size;
  7596. /* calculate total size with needed alignment + size */
  7597. if (type == NK_BUFFER_FRONT)
  7598. unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
  7599. else unaligned = nk_ptr_add(void, b->memory.ptr, b->size - size);
  7600. memory = nk_buffer_align(unaligned, align, &alignment, type);
  7601. /* check if buffer has enough memory*/
  7602. if (type == NK_BUFFER_FRONT)
  7603. full = ((b->allocated + size + alignment) > b->size);
  7604. else full = ((b->size - NK_MIN(b->size,(size + alignment))) <= b->allocated);
  7605. if (full) {
  7606. nk_size capacity;
  7607. if (b->type != NK_BUFFER_DYNAMIC)
  7608. return 0;
  7609. NK_ASSERT(b->pool.alloc && b->pool.free);
  7610. if (b->type != NK_BUFFER_DYNAMIC || !b->pool.alloc || !b->pool.free)
  7611. return 0;
  7612. /* buffer is full so allocate bigger buffer if dynamic */
  7613. capacity = (nk_size)((float)b->memory.size * b->grow_factor);
  7614. capacity = NK_MAX(capacity, nk_round_up_pow2((nk_uint)(b->allocated + size)));
  7615. b->memory.ptr = nk_buffer_realloc(b, capacity, &b->memory.size);
  7616. if (!b->memory.ptr) return 0;
  7617. /* align newly allocated pointer */
  7618. if (type == NK_BUFFER_FRONT)
  7619. unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
  7620. else unaligned = nk_ptr_add(void, b->memory.ptr, b->size - size);
  7621. memory = nk_buffer_align(unaligned, align, &alignment, type);
  7622. }
  7623. if (type == NK_BUFFER_FRONT)
  7624. b->allocated += size + alignment;
  7625. else b->size -= (size + alignment);
  7626. b->needed += alignment;
  7627. b->calls++;
  7628. return memory;
  7629. }
  7630. NK_API void
  7631. nk_buffer_push(struct nk_buffer *b, enum nk_buffer_allocation_type type,
  7632. const void *memory, nk_size size, nk_size align)
  7633. {
  7634. void *mem = nk_buffer_alloc(b, type, size, align);
  7635. if (!mem) return;
  7636. NK_MEMCPY(mem, memory, size);
  7637. }
  7638. NK_API void
  7639. nk_buffer_mark(struct nk_buffer *buffer, enum nk_buffer_allocation_type type)
  7640. {
  7641. NK_ASSERT(buffer);
  7642. if (!buffer) return;
  7643. buffer->marker[type].active = nk_true;
  7644. if (type == NK_BUFFER_BACK)
  7645. buffer->marker[type].offset = buffer->size;
  7646. else buffer->marker[type].offset = buffer->allocated;
  7647. }
  7648. NK_API void
  7649. nk_buffer_reset(struct nk_buffer *buffer, enum nk_buffer_allocation_type type)
  7650. {
  7651. NK_ASSERT(buffer);
  7652. if (!buffer) return;
  7653. if (type == NK_BUFFER_BACK) {
  7654. /* reset back buffer either back to marker or empty */
  7655. buffer->needed -= (buffer->memory.size - buffer->marker[type].offset);
  7656. if (buffer->marker[type].active)
  7657. buffer->size = buffer->marker[type].offset;
  7658. else buffer->size = buffer->memory.size;
  7659. buffer->marker[type].active = nk_false;
  7660. } else {
  7661. /* reset front buffer either back to back marker or empty */
  7662. buffer->needed -= (buffer->allocated - buffer->marker[type].offset);
  7663. if (buffer->marker[type].active)
  7664. buffer->allocated = buffer->marker[type].offset;
  7665. else buffer->allocated = 0;
  7666. buffer->marker[type].active = nk_false;
  7667. }
  7668. }
  7669. NK_API void
  7670. nk_buffer_clear(struct nk_buffer *b)
  7671. {
  7672. NK_ASSERT(b);
  7673. if (!b) return;
  7674. b->allocated = 0;
  7675. b->size = b->memory.size;
  7676. b->calls = 0;
  7677. b->needed = 0;
  7678. }
  7679. NK_API void
  7680. nk_buffer_free(struct nk_buffer *b)
  7681. {
  7682. NK_ASSERT(b);
  7683. if (!b || !b->memory.ptr) return;
  7684. if (b->type == NK_BUFFER_FIXED) return;
  7685. if (!b->pool.free) return;
  7686. NK_ASSERT(b->pool.free);
  7687. b->pool.free(b->pool.userdata, b->memory.ptr);
  7688. }
  7689. NK_API void
  7690. nk_buffer_info(struct nk_memory_status *s, struct nk_buffer *b)
  7691. {
  7692. NK_ASSERT(b);
  7693. NK_ASSERT(s);
  7694. if (!s || !b) return;
  7695. s->allocated = b->allocated;
  7696. s->size = b->memory.size;
  7697. s->needed = b->needed;
  7698. s->memory = b->memory.ptr;
  7699. s->calls = b->calls;
  7700. }
  7701. NK_API void*
  7702. nk_buffer_memory(struct nk_buffer *buffer)
  7703. {
  7704. NK_ASSERT(buffer);
  7705. if (!buffer) return 0;
  7706. return buffer->memory.ptr;
  7707. }
  7708. NK_API const void*
  7709. nk_buffer_memory_const(const struct nk_buffer *buffer)
  7710. {
  7711. NK_ASSERT(buffer);
  7712. if (!buffer) return 0;
  7713. return buffer->memory.ptr;
  7714. }
  7715. NK_API nk_size
  7716. nk_buffer_total(struct nk_buffer *buffer)
  7717. {
  7718. NK_ASSERT(buffer);
  7719. if (!buffer) return 0;
  7720. return buffer->memory.size;
  7721. }
  7722. /* ===============================================================
  7723. *
  7724. * STRING
  7725. *
  7726. * ===============================================================*/
  7727. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  7728. NK_API void
  7729. nk_str_init_default(struct nk_str *str)
  7730. {
  7731. struct nk_allocator alloc;
  7732. alloc.userdata.ptr = 0;
  7733. alloc.alloc = nk_malloc;
  7734. alloc.free = nk_mfree;
  7735. nk_buffer_init(&str->buffer, &alloc, 32);
  7736. str->len = 0;
  7737. }
  7738. #endif
  7739. NK_API void
  7740. nk_str_init(struct nk_str *str, const struct nk_allocator *alloc, nk_size size)
  7741. {
  7742. nk_buffer_init(&str->buffer, alloc, size);
  7743. str->len = 0;
  7744. }
  7745. NK_API void
  7746. nk_str_init_fixed(struct nk_str *str, void *memory, nk_size size)
  7747. {
  7748. nk_buffer_init_fixed(&str->buffer, memory, size);
  7749. str->len = 0;
  7750. }
  7751. NK_API int
  7752. nk_str_append_text_char(struct nk_str *s, const char *str, int len)
  7753. {
  7754. char *mem;
  7755. NK_ASSERT(s);
  7756. NK_ASSERT(str);
  7757. if (!s || !str || !len) return 0;
  7758. mem = (char*)nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
  7759. if (!mem) return 0;
  7760. NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
  7761. s->len += nk_utf_len(str, len);
  7762. return len;
  7763. }
  7764. NK_API int
  7765. nk_str_append_str_char(struct nk_str *s, const char *str)
  7766. {
  7767. return nk_str_append_text_char(s, str, nk_strlen(str));
  7768. }
  7769. NK_API int
  7770. nk_str_append_text_utf8(struct nk_str *str, const char *text, int len)
  7771. {
  7772. int i = 0;
  7773. int byte_len = 0;
  7774. nk_rune unicode;
  7775. if (!str || !text || !len) return 0;
  7776. for (i = 0; i < len; ++i)
  7777. byte_len += nk_utf_decode(text+byte_len, &unicode, 4);
  7778. nk_str_append_text_char(str, text, byte_len);
  7779. return len;
  7780. }
  7781. NK_API int
  7782. nk_str_append_str_utf8(struct nk_str *str, const char *text)
  7783. {
  7784. int runes = 0;
  7785. int byte_len = 0;
  7786. int num_runes = 0;
  7787. int glyph_len = 0;
  7788. nk_rune unicode;
  7789. if (!str || !text) return 0;
  7790. glyph_len = byte_len = nk_utf_decode(text+byte_len, &unicode, 4);
  7791. while (unicode != '\0' && glyph_len) {
  7792. glyph_len = nk_utf_decode(text+byte_len, &unicode, 4);
  7793. byte_len += glyph_len;
  7794. num_runes++;
  7795. }
  7796. nk_str_append_text_char(str, text, byte_len);
  7797. return runes;
  7798. }
  7799. NK_API int
  7800. nk_str_append_text_runes(struct nk_str *str, const nk_rune *text, int len)
  7801. {
  7802. int i = 0;
  7803. int byte_len = 0;
  7804. nk_glyph glyph;
  7805. NK_ASSERT(str);
  7806. if (!str || !text || !len) return 0;
  7807. for (i = 0; i < len; ++i) {
  7808. byte_len = nk_utf_encode(text[i], glyph, NK_UTF_SIZE);
  7809. if (!byte_len) break;
  7810. nk_str_append_text_char(str, glyph, byte_len);
  7811. }
  7812. return len;
  7813. }
  7814. NK_API int
  7815. nk_str_append_str_runes(struct nk_str *str, const nk_rune *runes)
  7816. {
  7817. int i = 0;
  7818. nk_glyph glyph;
  7819. int byte_len;
  7820. NK_ASSERT(str);
  7821. if (!str || !runes) return 0;
  7822. while (runes[i] != '\0') {
  7823. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  7824. nk_str_append_text_char(str, glyph, byte_len);
  7825. i++;
  7826. }
  7827. return i;
  7828. }
  7829. NK_API int
  7830. nk_str_insert_at_char(struct nk_str *s, int pos, const char *str, int len)
  7831. {
  7832. int i;
  7833. void *mem;
  7834. char *src;
  7835. char *dst;
  7836. int copylen;
  7837. NK_ASSERT(s);
  7838. NK_ASSERT(str);
  7839. NK_ASSERT(len >= 0);
  7840. if (!s || !str || !len || (nk_size)pos > s->buffer.allocated) return 0;
  7841. if ((s->buffer.allocated + (nk_size)len >= s->buffer.memory.size) &&
  7842. (s->buffer.type == NK_BUFFER_FIXED)) return 0;
  7843. copylen = (int)s->buffer.allocated - pos;
  7844. if (!copylen) {
  7845. nk_str_append_text_char(s, str, len);
  7846. return 1;
  7847. }
  7848. mem = nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
  7849. if (!mem) return 0;
  7850. /* memmove */
  7851. NK_ASSERT(((int)pos + (int)len + ((int)copylen - 1)) >= 0);
  7852. NK_ASSERT(((int)pos + ((int)copylen - 1)) >= 0);
  7853. dst = nk_ptr_add(char, s->buffer.memory.ptr, pos + len + (copylen - 1));
  7854. src = nk_ptr_add(char, s->buffer.memory.ptr, pos + (copylen-1));
  7855. for (i = 0; i < copylen; ++i) *dst-- = *src--;
  7856. mem = nk_ptr_add(void, s->buffer.memory.ptr, pos);
  7857. NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
  7858. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  7859. return 1;
  7860. }
  7861. NK_API int
  7862. nk_str_insert_at_rune(struct nk_str *str, int pos, const char *cstr, int len)
  7863. {
  7864. int glyph_len;
  7865. nk_rune unicode;
  7866. const char *begin;
  7867. const char *buffer;
  7868. NK_ASSERT(str);
  7869. NK_ASSERT(cstr);
  7870. NK_ASSERT(len);
  7871. if (!str || !cstr || !len) return 0;
  7872. begin = nk_str_at_rune(str, pos, &unicode, &glyph_len);
  7873. if (!str->len)
  7874. return nk_str_append_text_char(str, cstr, len);
  7875. buffer = nk_str_get_const(str);
  7876. if (!begin) return 0;
  7877. return nk_str_insert_at_char(str, (int)(begin - buffer), cstr, len);
  7878. }
  7879. NK_API int
  7880. nk_str_insert_text_char(struct nk_str *str, int pos, const char *text, int len)
  7881. {
  7882. return nk_str_insert_text_utf8(str, pos, text, len);
  7883. }
  7884. NK_API int
  7885. nk_str_insert_str_char(struct nk_str *str, int pos, const char *text)
  7886. {
  7887. return nk_str_insert_text_utf8(str, pos, text, nk_strlen(text));
  7888. }
  7889. NK_API int
  7890. nk_str_insert_text_utf8(struct nk_str *str, int pos, const char *text, int len)
  7891. {
  7892. int i = 0;
  7893. int byte_len = 0;
  7894. nk_rune unicode;
  7895. NK_ASSERT(str);
  7896. NK_ASSERT(text);
  7897. if (!str || !text || !len) return 0;
  7898. for (i = 0; i < len; ++i)
  7899. byte_len += nk_utf_decode(text+byte_len, &unicode, 4);
  7900. nk_str_insert_at_rune(str, pos, text, byte_len);
  7901. return len;
  7902. }
  7903. NK_API int
  7904. nk_str_insert_str_utf8(struct nk_str *str, int pos, const char *text)
  7905. {
  7906. int runes = 0;
  7907. int byte_len = 0;
  7908. int num_runes = 0;
  7909. int glyph_len = 0;
  7910. nk_rune unicode;
  7911. if (!str || !text) return 0;
  7912. glyph_len = byte_len = nk_utf_decode(text+byte_len, &unicode, 4);
  7913. while (unicode != '\0' && glyph_len) {
  7914. glyph_len = nk_utf_decode(text+byte_len, &unicode, 4);
  7915. byte_len += glyph_len;
  7916. num_runes++;
  7917. }
  7918. nk_str_insert_at_rune(str, pos, text, byte_len);
  7919. return runes;
  7920. }
  7921. NK_API int
  7922. nk_str_insert_text_runes(struct nk_str *str, int pos, const nk_rune *runes, int len)
  7923. {
  7924. int i = 0;
  7925. int byte_len = 0;
  7926. nk_glyph glyph;
  7927. NK_ASSERT(str);
  7928. if (!str || !runes || !len) return 0;
  7929. for (i = 0; i < len; ++i) {
  7930. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  7931. if (!byte_len) break;
  7932. nk_str_insert_at_rune(str, pos+i, glyph, byte_len);
  7933. }
  7934. return len;
  7935. }
  7936. NK_API int
  7937. nk_str_insert_str_runes(struct nk_str *str, int pos, const nk_rune *runes)
  7938. {
  7939. int i = 0;
  7940. nk_glyph glyph;
  7941. int byte_len;
  7942. NK_ASSERT(str);
  7943. if (!str || !runes) return 0;
  7944. while (runes[i] != '\0') {
  7945. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  7946. nk_str_insert_at_rune(str, pos+i, glyph, byte_len);
  7947. i++;
  7948. }
  7949. return i;
  7950. }
  7951. NK_API void
  7952. nk_str_remove_chars(struct nk_str *s, int len)
  7953. {
  7954. NK_ASSERT(s);
  7955. NK_ASSERT(len >= 0);
  7956. if (!s || len < 0 || (nk_size)len > s->buffer.allocated) return;
  7957. NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
  7958. s->buffer.allocated -= (nk_size)len;
  7959. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  7960. }
  7961. NK_API void
  7962. nk_str_remove_runes(struct nk_str *str, int len)
  7963. {
  7964. int index;
  7965. const char *begin;
  7966. const char *end;
  7967. nk_rune unicode;
  7968. NK_ASSERT(str);
  7969. NK_ASSERT(len >= 0);
  7970. if (!str || len < 0) return;
  7971. if (len >= str->len) {
  7972. str->len = 0;
  7973. return;
  7974. }
  7975. index = str->len - len;
  7976. begin = nk_str_at_rune(str, index, &unicode, &len);
  7977. end = (const char*)str->buffer.memory.ptr + str->buffer.allocated;
  7978. nk_str_remove_chars(str, (int)(end-begin)+1);
  7979. }
  7980. NK_API void
  7981. nk_str_delete_chars(struct nk_str *s, int pos, int len)
  7982. {
  7983. NK_ASSERT(s);
  7984. if (!s || !len || (nk_size)pos > s->buffer.allocated ||
  7985. (nk_size)(pos + len) > s->buffer.allocated) return;
  7986. if ((nk_size)(pos + len) < s->buffer.allocated) {
  7987. /* memmove */
  7988. char *dst = nk_ptr_add(char, s->buffer.memory.ptr, pos);
  7989. char *src = nk_ptr_add(char, s->buffer.memory.ptr, pos + len);
  7990. NK_MEMCPY(dst, src, s->buffer.allocated - (nk_size)(pos + len));
  7991. NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
  7992. s->buffer.allocated -= (nk_size)len;
  7993. } else nk_str_remove_chars(s, len);
  7994. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  7995. }
  7996. NK_API void
  7997. nk_str_delete_runes(struct nk_str *s, int pos, int len)
  7998. {
  7999. char *temp;
  8000. nk_rune unicode;
  8001. char *begin;
  8002. char *end;
  8003. int unused;
  8004. NK_ASSERT(s);
  8005. NK_ASSERT(s->len >= pos + len);
  8006. if (s->len < pos + len)
  8007. len = NK_CLAMP(0, (s->len - pos), s->len);
  8008. if (!len) return;
  8009. temp = (char *)s->buffer.memory.ptr;
  8010. begin = nk_str_at_rune(s, pos, &unicode, &unused);
  8011. if (!begin) return;
  8012. s->buffer.memory.ptr = begin;
  8013. end = nk_str_at_rune(s, len, &unicode, &unused);
  8014. s->buffer.memory.ptr = temp;
  8015. if (!end) return;
  8016. nk_str_delete_chars(s, (int)(begin - temp), (int)(end - begin));
  8017. }
  8018. NK_API char*
  8019. nk_str_at_char(struct nk_str *s, int pos)
  8020. {
  8021. NK_ASSERT(s);
  8022. if (!s || pos > (int)s->buffer.allocated) return 0;
  8023. return nk_ptr_add(char, s->buffer.memory.ptr, pos);
  8024. }
  8025. NK_API char*
  8026. nk_str_at_rune(struct nk_str *str, int pos, nk_rune *unicode, int *len)
  8027. {
  8028. int i = 0;
  8029. int src_len = 0;
  8030. int glyph_len = 0;
  8031. char *text;
  8032. int text_len;
  8033. NK_ASSERT(str);
  8034. NK_ASSERT(unicode);
  8035. NK_ASSERT(len);
  8036. if (!str || !unicode || !len) return 0;
  8037. if (pos < 0) {
  8038. *unicode = 0;
  8039. *len = 0;
  8040. return 0;
  8041. }
  8042. text = (char*)str->buffer.memory.ptr;
  8043. text_len = (int)str->buffer.allocated;
  8044. glyph_len = nk_utf_decode(text, unicode, text_len);
  8045. while (glyph_len) {
  8046. if (i == pos) {
  8047. *len = glyph_len;
  8048. break;
  8049. }
  8050. i++;
  8051. src_len = src_len + glyph_len;
  8052. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  8053. }
  8054. if (i != pos) return 0;
  8055. return text + src_len;
  8056. }
  8057. NK_API const char*
  8058. nk_str_at_char_const(const struct nk_str *s, int pos)
  8059. {
  8060. NK_ASSERT(s);
  8061. if (!s || pos > (int)s->buffer.allocated) return 0;
  8062. return nk_ptr_add(char, s->buffer.memory.ptr, pos);
  8063. }
  8064. NK_API const char*
  8065. nk_str_at_const(const struct nk_str *str, int pos, nk_rune *unicode, int *len)
  8066. {
  8067. int i = 0;
  8068. int src_len = 0;
  8069. int glyph_len = 0;
  8070. char *text;
  8071. int text_len;
  8072. NK_ASSERT(str);
  8073. NK_ASSERT(unicode);
  8074. NK_ASSERT(len);
  8075. if (!str || !unicode || !len) return 0;
  8076. if (pos < 0) {
  8077. *unicode = 0;
  8078. *len = 0;
  8079. return 0;
  8080. }
  8081. text = (char*)str->buffer.memory.ptr;
  8082. text_len = (int)str->buffer.allocated;
  8083. glyph_len = nk_utf_decode(text, unicode, text_len);
  8084. while (glyph_len) {
  8085. if (i == pos) {
  8086. *len = glyph_len;
  8087. break;
  8088. }
  8089. i++;
  8090. src_len = src_len + glyph_len;
  8091. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  8092. }
  8093. if (i != pos) return 0;
  8094. return text + src_len;
  8095. }
  8096. NK_API nk_rune
  8097. nk_str_rune_at(const struct nk_str *str, int pos)
  8098. {
  8099. int len;
  8100. nk_rune unicode = 0;
  8101. nk_str_at_const(str, pos, &unicode, &len);
  8102. return unicode;
  8103. }
  8104. NK_API char*
  8105. nk_str_get(struct nk_str *s)
  8106. {
  8107. NK_ASSERT(s);
  8108. if (!s || !s->len || !s->buffer.allocated) return 0;
  8109. return (char*)s->buffer.memory.ptr;
  8110. }
  8111. NK_API const char*
  8112. nk_str_get_const(const struct nk_str *s)
  8113. {
  8114. NK_ASSERT(s);
  8115. if (!s || !s->len || !s->buffer.allocated) return 0;
  8116. return (const char*)s->buffer.memory.ptr;
  8117. }
  8118. NK_API int
  8119. nk_str_len(struct nk_str *s)
  8120. {
  8121. NK_ASSERT(s);
  8122. if (!s || !s->len || !s->buffer.allocated) return 0;
  8123. return s->len;
  8124. }
  8125. NK_API int
  8126. nk_str_len_char(struct nk_str *s)
  8127. {
  8128. NK_ASSERT(s);
  8129. if (!s || !s->len || !s->buffer.allocated) return 0;
  8130. return (int)s->buffer.allocated;
  8131. }
  8132. NK_API void
  8133. nk_str_clear(struct nk_str *str)
  8134. {
  8135. NK_ASSERT(str);
  8136. nk_buffer_clear(&str->buffer);
  8137. str->len = 0;
  8138. }
  8139. NK_API void
  8140. nk_str_free(struct nk_str *str)
  8141. {
  8142. NK_ASSERT(str);
  8143. nk_buffer_free(&str->buffer);
  8144. str->len = 0;
  8145. }
  8146. /* ==============================================================
  8147. *
  8148. * DRAW
  8149. *
  8150. * ===============================================================*/
  8151. NK_LIB void
  8152. nk_command_buffer_init(struct nk_command_buffer *cb,
  8153. struct nk_buffer *b, enum nk_command_clipping clip)
  8154. {
  8155. NK_ASSERT(cb);
  8156. NK_ASSERT(b);
  8157. if (!cb || !b) return;
  8158. cb->base = b;
  8159. cb->use_clipping = (int)clip;
  8160. cb->begin = b->allocated;
  8161. cb->end = b->allocated;
  8162. cb->last = b->allocated;
  8163. }
  8164. NK_LIB void
  8165. nk_command_buffer_reset(struct nk_command_buffer *b)
  8166. {
  8167. NK_ASSERT(b);
  8168. if (!b) return;
  8169. b->begin = 0;
  8170. b->end = 0;
  8171. b->last = 0;
  8172. b->clip = nk_null_rect;
  8173. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8174. b->userdata.ptr = 0;
  8175. #endif
  8176. }
  8177. NK_LIB void*
  8178. nk_command_buffer_push(struct nk_command_buffer* b,
  8179. enum nk_command_type t, nk_size size)
  8180. {
  8181. NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_command);
  8182. struct nk_command *cmd;
  8183. nk_size alignment;
  8184. void *unaligned;
  8185. void *memory;
  8186. NK_ASSERT(b);
  8187. NK_ASSERT(b->base);
  8188. if (!b) return 0;
  8189. cmd = (struct nk_command*)nk_buffer_alloc(b->base,NK_BUFFER_FRONT,size,align);
  8190. if (!cmd) return 0;
  8191. /* make sure the offset to the next command is aligned */
  8192. b->last = (nk_size)((nk_byte*)cmd - (nk_byte*)b->base->memory.ptr);
  8193. unaligned = (nk_byte*)cmd + size;
  8194. memory = NK_ALIGN_PTR(unaligned, align);
  8195. alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
  8196. #ifdef NK_ZERO_COMMAND_MEMORY
  8197. NK_MEMSET(cmd, 0, size + alignment);
  8198. #endif
  8199. cmd->type = t;
  8200. cmd->next = b->base->allocated + alignment;
  8201. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8202. cmd->userdata = b->userdata;
  8203. #endif
  8204. b->end = cmd->next;
  8205. return cmd;
  8206. }
  8207. NK_API void
  8208. nk_push_scissor(struct nk_command_buffer *b, struct nk_rect r)
  8209. {
  8210. struct nk_command_scissor *cmd;
  8211. NK_ASSERT(b);
  8212. if (!b) return;
  8213. b->clip.x = r.x;
  8214. b->clip.y = r.y;
  8215. b->clip.w = r.w;
  8216. b->clip.h = r.h;
  8217. cmd = (struct nk_command_scissor*)
  8218. nk_command_buffer_push(b, NK_COMMAND_SCISSOR, sizeof(*cmd));
  8219. if (!cmd) return;
  8220. cmd->x = (short)r.x;
  8221. cmd->y = (short)r.y;
  8222. cmd->w = (unsigned short)NK_MAX(0, r.w);
  8223. cmd->h = (unsigned short)NK_MAX(0, r.h);
  8224. }
  8225. NK_API void
  8226. nk_stroke_line(struct nk_command_buffer *b, float x0, float y0,
  8227. float x1, float y1, float line_thickness, struct nk_color c)
  8228. {
  8229. struct nk_command_line *cmd;
  8230. NK_ASSERT(b);
  8231. if (!b || line_thickness <= 0) return;
  8232. cmd = (struct nk_command_line*)
  8233. nk_command_buffer_push(b, NK_COMMAND_LINE, sizeof(*cmd));
  8234. if (!cmd) return;
  8235. cmd->line_thickness = (unsigned short)line_thickness;
  8236. cmd->begin.x = (short)x0;
  8237. cmd->begin.y = (short)y0;
  8238. cmd->end.x = (short)x1;
  8239. cmd->end.y = (short)y1;
  8240. cmd->color = c;
  8241. }
  8242. NK_API void
  8243. nk_stroke_curve(struct nk_command_buffer *b, float ax, float ay,
  8244. float ctrl0x, float ctrl0y, float ctrl1x, float ctrl1y,
  8245. float bx, float by, float line_thickness, struct nk_color col)
  8246. {
  8247. struct nk_command_curve *cmd;
  8248. NK_ASSERT(b);
  8249. if (!b || col.a == 0 || line_thickness <= 0) return;
  8250. cmd = (struct nk_command_curve*)
  8251. nk_command_buffer_push(b, NK_COMMAND_CURVE, sizeof(*cmd));
  8252. if (!cmd) return;
  8253. cmd->line_thickness = (unsigned short)line_thickness;
  8254. cmd->begin.x = (short)ax;
  8255. cmd->begin.y = (short)ay;
  8256. cmd->ctrl[0].x = (short)ctrl0x;
  8257. cmd->ctrl[0].y = (short)ctrl0y;
  8258. cmd->ctrl[1].x = (short)ctrl1x;
  8259. cmd->ctrl[1].y = (short)ctrl1y;
  8260. cmd->end.x = (short)bx;
  8261. cmd->end.y = (short)by;
  8262. cmd->color = col;
  8263. }
  8264. NK_API void
  8265. nk_stroke_rect(struct nk_command_buffer *b, struct nk_rect rect,
  8266. float rounding, float line_thickness, struct nk_color c)
  8267. {
  8268. struct nk_command_rect *cmd;
  8269. NK_ASSERT(b);
  8270. if (!b || c.a == 0 || rect.w == 0 || rect.h == 0 || line_thickness <= 0) return;
  8271. if (b->use_clipping) {
  8272. const struct nk_rect *clip = &b->clip;
  8273. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8274. clip->x, clip->y, clip->w, clip->h)) return;
  8275. }
  8276. cmd = (struct nk_command_rect*)
  8277. nk_command_buffer_push(b, NK_COMMAND_RECT, sizeof(*cmd));
  8278. if (!cmd) return;
  8279. cmd->rounding = (unsigned short)rounding;
  8280. cmd->line_thickness = (unsigned short)line_thickness;
  8281. cmd->x = (short)rect.x;
  8282. cmd->y = (short)rect.y;
  8283. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  8284. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  8285. cmd->color = c;
  8286. }
  8287. NK_API void
  8288. nk_fill_rect(struct nk_command_buffer *b, struct nk_rect rect,
  8289. float rounding, struct nk_color c)
  8290. {
  8291. struct nk_command_rect_filled *cmd;
  8292. NK_ASSERT(b);
  8293. if (!b || c.a == 0 || rect.w == 0 || rect.h == 0) return;
  8294. if (b->use_clipping) {
  8295. const struct nk_rect *clip = &b->clip;
  8296. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8297. clip->x, clip->y, clip->w, clip->h)) return;
  8298. }
  8299. cmd = (struct nk_command_rect_filled*)
  8300. nk_command_buffer_push(b, NK_COMMAND_RECT_FILLED, sizeof(*cmd));
  8301. if (!cmd) return;
  8302. cmd->rounding = (unsigned short)rounding;
  8303. cmd->x = (short)rect.x;
  8304. cmd->y = (short)rect.y;
  8305. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  8306. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  8307. cmd->color = c;
  8308. }
  8309. NK_API void
  8310. nk_fill_rect_multi_color(struct nk_command_buffer *b, struct nk_rect rect,
  8311. struct nk_color left, struct nk_color top, struct nk_color right,
  8312. struct nk_color bottom)
  8313. {
  8314. struct nk_command_rect_multi_color *cmd;
  8315. NK_ASSERT(b);
  8316. if (!b || rect.w == 0 || rect.h == 0) return;
  8317. if (b->use_clipping) {
  8318. const struct nk_rect *clip = &b->clip;
  8319. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8320. clip->x, clip->y, clip->w, clip->h)) return;
  8321. }
  8322. cmd = (struct nk_command_rect_multi_color*)
  8323. nk_command_buffer_push(b, NK_COMMAND_RECT_MULTI_COLOR, sizeof(*cmd));
  8324. if (!cmd) return;
  8325. cmd->x = (short)rect.x;
  8326. cmd->y = (short)rect.y;
  8327. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  8328. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  8329. cmd->left = left;
  8330. cmd->top = top;
  8331. cmd->right = right;
  8332. cmd->bottom = bottom;
  8333. }
  8334. NK_API void
  8335. nk_stroke_circle(struct nk_command_buffer *b, struct nk_rect r,
  8336. float line_thickness, struct nk_color c)
  8337. {
  8338. struct nk_command_circle *cmd;
  8339. if (!b || r.w == 0 || r.h == 0 || line_thickness <= 0) return;
  8340. if (b->use_clipping) {
  8341. const struct nk_rect *clip = &b->clip;
  8342. if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
  8343. return;
  8344. }
  8345. cmd = (struct nk_command_circle*)
  8346. nk_command_buffer_push(b, NK_COMMAND_CIRCLE, sizeof(*cmd));
  8347. if (!cmd) return;
  8348. cmd->line_thickness = (unsigned short)line_thickness;
  8349. cmd->x = (short)r.x;
  8350. cmd->y = (short)r.y;
  8351. cmd->w = (unsigned short)NK_MAX(r.w, 0);
  8352. cmd->h = (unsigned short)NK_MAX(r.h, 0);
  8353. cmd->color = c;
  8354. }
  8355. NK_API void
  8356. nk_fill_circle(struct nk_command_buffer *b, struct nk_rect r, struct nk_color c)
  8357. {
  8358. struct nk_command_circle_filled *cmd;
  8359. NK_ASSERT(b);
  8360. if (!b || c.a == 0 || r.w == 0 || r.h == 0) return;
  8361. if (b->use_clipping) {
  8362. const struct nk_rect *clip = &b->clip;
  8363. if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
  8364. return;
  8365. }
  8366. cmd = (struct nk_command_circle_filled*)
  8367. nk_command_buffer_push(b, NK_COMMAND_CIRCLE_FILLED, sizeof(*cmd));
  8368. if (!cmd) return;
  8369. cmd->x = (short)r.x;
  8370. cmd->y = (short)r.y;
  8371. cmd->w = (unsigned short)NK_MAX(r.w, 0);
  8372. cmd->h = (unsigned short)NK_MAX(r.h, 0);
  8373. cmd->color = c;
  8374. }
  8375. NK_API void
  8376. nk_stroke_arc(struct nk_command_buffer *b, float cx, float cy, float radius,
  8377. float a_min, float a_max, float line_thickness, struct nk_color c)
  8378. {
  8379. struct nk_command_arc *cmd;
  8380. if (!b || c.a == 0 || line_thickness <= 0) return;
  8381. cmd = (struct nk_command_arc*)
  8382. nk_command_buffer_push(b, NK_COMMAND_ARC, sizeof(*cmd));
  8383. if (!cmd) return;
  8384. cmd->line_thickness = (unsigned short)line_thickness;
  8385. cmd->cx = (short)cx;
  8386. cmd->cy = (short)cy;
  8387. cmd->r = (unsigned short)radius;
  8388. cmd->a[0] = a_min;
  8389. cmd->a[1] = a_max;
  8390. cmd->color = c;
  8391. }
  8392. NK_API void
  8393. nk_fill_arc(struct nk_command_buffer *b, float cx, float cy, float radius,
  8394. float a_min, float a_max, struct nk_color c)
  8395. {
  8396. struct nk_command_arc_filled *cmd;
  8397. NK_ASSERT(b);
  8398. if (!b || c.a == 0) return;
  8399. cmd = (struct nk_command_arc_filled*)
  8400. nk_command_buffer_push(b, NK_COMMAND_ARC_FILLED, sizeof(*cmd));
  8401. if (!cmd) return;
  8402. cmd->cx = (short)cx;
  8403. cmd->cy = (short)cy;
  8404. cmd->r = (unsigned short)radius;
  8405. cmd->a[0] = a_min;
  8406. cmd->a[1] = a_max;
  8407. cmd->color = c;
  8408. }
  8409. NK_API void
  8410. nk_stroke_triangle(struct nk_command_buffer *b, float x0, float y0, float x1,
  8411. float y1, float x2, float y2, float line_thickness, struct nk_color c)
  8412. {
  8413. struct nk_command_triangle *cmd;
  8414. NK_ASSERT(b);
  8415. if (!b || c.a == 0 || line_thickness <= 0) return;
  8416. if (b->use_clipping) {
  8417. const struct nk_rect *clip = &b->clip;
  8418. if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
  8419. !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
  8420. !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
  8421. return;
  8422. }
  8423. cmd = (struct nk_command_triangle*)
  8424. nk_command_buffer_push(b, NK_COMMAND_TRIANGLE, sizeof(*cmd));
  8425. if (!cmd) return;
  8426. cmd->line_thickness = (unsigned short)line_thickness;
  8427. cmd->a.x = (short)x0;
  8428. cmd->a.y = (short)y0;
  8429. cmd->b.x = (short)x1;
  8430. cmd->b.y = (short)y1;
  8431. cmd->c.x = (short)x2;
  8432. cmd->c.y = (short)y2;
  8433. cmd->color = c;
  8434. }
  8435. NK_API void
  8436. nk_fill_triangle(struct nk_command_buffer *b, float x0, float y0, float x1,
  8437. float y1, float x2, float y2, struct nk_color c)
  8438. {
  8439. struct nk_command_triangle_filled *cmd;
  8440. NK_ASSERT(b);
  8441. if (!b || c.a == 0) return;
  8442. if (!b) return;
  8443. if (b->use_clipping) {
  8444. const struct nk_rect *clip = &b->clip;
  8445. if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
  8446. !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
  8447. !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
  8448. return;
  8449. }
  8450. cmd = (struct nk_command_triangle_filled*)
  8451. nk_command_buffer_push(b, NK_COMMAND_TRIANGLE_FILLED, sizeof(*cmd));
  8452. if (!cmd) return;
  8453. cmd->a.x = (short)x0;
  8454. cmd->a.y = (short)y0;
  8455. cmd->b.x = (short)x1;
  8456. cmd->b.y = (short)y1;
  8457. cmd->c.x = (short)x2;
  8458. cmd->c.y = (short)y2;
  8459. cmd->color = c;
  8460. }
  8461. NK_API void
  8462. nk_stroke_polygon(struct nk_command_buffer *b, float *points, int point_count,
  8463. float line_thickness, struct nk_color col)
  8464. {
  8465. int i;
  8466. nk_size size = 0;
  8467. struct nk_command_polygon *cmd;
  8468. NK_ASSERT(b);
  8469. if (!b || col.a == 0 || line_thickness <= 0) return;
  8470. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  8471. cmd = (struct nk_command_polygon*) nk_command_buffer_push(b, NK_COMMAND_POLYGON, size);
  8472. if (!cmd) return;
  8473. cmd->color = col;
  8474. cmd->line_thickness = (unsigned short)line_thickness;
  8475. cmd->point_count = (unsigned short)point_count;
  8476. for (i = 0; i < point_count; ++i) {
  8477. cmd->points[i].x = (short)points[i*2];
  8478. cmd->points[i].y = (short)points[i*2+1];
  8479. }
  8480. }
  8481. NK_API void
  8482. nk_fill_polygon(struct nk_command_buffer *b, float *points, int point_count,
  8483. struct nk_color col)
  8484. {
  8485. int i;
  8486. nk_size size = 0;
  8487. struct nk_command_polygon_filled *cmd;
  8488. NK_ASSERT(b);
  8489. if (!b || col.a == 0) return;
  8490. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  8491. cmd = (struct nk_command_polygon_filled*)
  8492. nk_command_buffer_push(b, NK_COMMAND_POLYGON_FILLED, size);
  8493. if (!cmd) return;
  8494. cmd->color = col;
  8495. cmd->point_count = (unsigned short)point_count;
  8496. for (i = 0; i < point_count; ++i) {
  8497. cmd->points[i].x = (short)points[i*2+0];
  8498. cmd->points[i].y = (short)points[i*2+1];
  8499. }
  8500. }
  8501. NK_API void
  8502. nk_stroke_polyline(struct nk_command_buffer *b, float *points, int point_count,
  8503. float line_thickness, struct nk_color col)
  8504. {
  8505. int i;
  8506. nk_size size = 0;
  8507. struct nk_command_polyline *cmd;
  8508. NK_ASSERT(b);
  8509. if (!b || col.a == 0 || line_thickness <= 0) return;
  8510. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  8511. cmd = (struct nk_command_polyline*) nk_command_buffer_push(b, NK_COMMAND_POLYLINE, size);
  8512. if (!cmd) return;
  8513. cmd->color = col;
  8514. cmd->point_count = (unsigned short)point_count;
  8515. cmd->line_thickness = (unsigned short)line_thickness;
  8516. for (i = 0; i < point_count; ++i) {
  8517. cmd->points[i].x = (short)points[i*2];
  8518. cmd->points[i].y = (short)points[i*2+1];
  8519. }
  8520. }
  8521. NK_API void
  8522. nk_draw_image(struct nk_command_buffer *b, struct nk_rect r,
  8523. const struct nk_image *img, struct nk_color col)
  8524. {
  8525. struct nk_command_image *cmd;
  8526. NK_ASSERT(b);
  8527. if (!b) return;
  8528. if (b->use_clipping) {
  8529. const struct nk_rect *c = &b->clip;
  8530. if (c->w == 0 || c->h == 0 || !NK_INTERSECT(r.x, r.y, r.w, r.h, c->x, c->y, c->w, c->h))
  8531. return;
  8532. }
  8533. cmd = (struct nk_command_image*)
  8534. nk_command_buffer_push(b, NK_COMMAND_IMAGE, sizeof(*cmd));
  8535. if (!cmd) return;
  8536. cmd->x = (short)r.x;
  8537. cmd->y = (short)r.y;
  8538. cmd->w = (unsigned short)NK_MAX(0, r.w);
  8539. cmd->h = (unsigned short)NK_MAX(0, r.h);
  8540. cmd->img = *img;
  8541. cmd->col = col;
  8542. }
  8543. NK_API void
  8544. nk_push_custom(struct nk_command_buffer *b, struct nk_rect r,
  8545. nk_command_custom_callback cb, nk_handle usr)
  8546. {
  8547. struct nk_command_custom *cmd;
  8548. NK_ASSERT(b);
  8549. if (!b) return;
  8550. if (b->use_clipping) {
  8551. const struct nk_rect *c = &b->clip;
  8552. if (c->w == 0 || c->h == 0 || !NK_INTERSECT(r.x, r.y, r.w, r.h, c->x, c->y, c->w, c->h))
  8553. return;
  8554. }
  8555. cmd = (struct nk_command_custom*)
  8556. nk_command_buffer_push(b, NK_COMMAND_CUSTOM, sizeof(*cmd));
  8557. if (!cmd) return;
  8558. cmd->x = (short)r.x;
  8559. cmd->y = (short)r.y;
  8560. cmd->w = (unsigned short)NK_MAX(0, r.w);
  8561. cmd->h = (unsigned short)NK_MAX(0, r.h);
  8562. cmd->callback_data = usr;
  8563. cmd->callback = cb;
  8564. }
  8565. NK_API void
  8566. nk_draw_text(struct nk_command_buffer *b, struct nk_rect r,
  8567. const char *string, int length, const struct nk_user_font *font,
  8568. struct nk_color bg, struct nk_color fg)
  8569. {
  8570. float text_width = 0;
  8571. struct nk_command_text *cmd;
  8572. NK_ASSERT(b);
  8573. NK_ASSERT(font);
  8574. if (!b || !string || !length || (bg.a == 0 && fg.a == 0)) return;
  8575. if (b->use_clipping) {
  8576. const struct nk_rect *c = &b->clip;
  8577. if (c->w == 0 || c->h == 0 || !NK_INTERSECT(r.x, r.y, r.w, r.h, c->x, c->y, c->w, c->h))
  8578. return;
  8579. }
  8580. /* make sure text fits inside bounds */
  8581. text_width = font->width(font->userdata, font->height, string, length);
  8582. if (text_width > r.w){
  8583. int glyphs = 0;
  8584. float txt_width = (float)text_width;
  8585. length = nk_text_clamp(font, string, length, r.w, &glyphs, &txt_width, 0,0);
  8586. }
  8587. if (!length) return;
  8588. cmd = (struct nk_command_text*)
  8589. nk_command_buffer_push(b, NK_COMMAND_TEXT, sizeof(*cmd) + (nk_size)(length + 1));
  8590. if (!cmd) return;
  8591. cmd->x = (short)r.x;
  8592. cmd->y = (short)r.y;
  8593. cmd->w = (unsigned short)r.w;
  8594. cmd->h = (unsigned short)r.h;
  8595. cmd->background = bg;
  8596. cmd->foreground = fg;
  8597. cmd->font = font;
  8598. cmd->length = length;
  8599. cmd->height = font->height;
  8600. NK_MEMCPY(cmd->string, string, (nk_size)length);
  8601. cmd->string[length] = '\0';
  8602. }
  8603. /* ===============================================================
  8604. *
  8605. * VERTEX
  8606. *
  8607. * ===============================================================*/
  8608. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  8609. NK_API void
  8610. nk_draw_list_init(struct nk_draw_list *list)
  8611. {
  8612. nk_size i = 0;
  8613. NK_ASSERT(list);
  8614. if (!list) return;
  8615. nk_zero(list, sizeof(*list));
  8616. for (i = 0; i < NK_LEN(list->circle_vtx); ++i) {
  8617. const float a = ((float)i / (float)NK_LEN(list->circle_vtx)) * 2 * NK_PI;
  8618. list->circle_vtx[i].x = (float)NK_COS(a);
  8619. list->circle_vtx[i].y = (float)NK_SIN(a);
  8620. }
  8621. }
  8622. NK_API void
  8623. nk_draw_list_setup(struct nk_draw_list *canvas, const struct nk_convert_config *config,
  8624. struct nk_buffer *cmds, struct nk_buffer *vertices, struct nk_buffer *elements,
  8625. enum nk_anti_aliasing line_aa, enum nk_anti_aliasing shape_aa)
  8626. {
  8627. NK_ASSERT(canvas);
  8628. NK_ASSERT(config);
  8629. NK_ASSERT(cmds);
  8630. NK_ASSERT(vertices);
  8631. NK_ASSERT(elements);
  8632. if (!canvas || !config || !cmds || !vertices || !elements)
  8633. return;
  8634. canvas->buffer = cmds;
  8635. canvas->config = *config;
  8636. canvas->elements = elements;
  8637. canvas->vertices = vertices;
  8638. canvas->line_AA = line_aa;
  8639. canvas->shape_AA = shape_aa;
  8640. canvas->clip_rect = nk_null_rect;
  8641. canvas->cmd_offset = 0;
  8642. canvas->element_count = 0;
  8643. canvas->vertex_count = 0;
  8644. canvas->cmd_offset = 0;
  8645. canvas->cmd_count = 0;
  8646. canvas->path_count = 0;
  8647. }
  8648. NK_API const struct nk_draw_command*
  8649. nk__draw_list_begin(const struct nk_draw_list *canvas, const struct nk_buffer *buffer)
  8650. {
  8651. nk_byte *memory;
  8652. nk_size offset;
  8653. const struct nk_draw_command *cmd;
  8654. NK_ASSERT(buffer);
  8655. if (!buffer || !buffer->size || !canvas->cmd_count)
  8656. return 0;
  8657. memory = (nk_byte*)buffer->memory.ptr;
  8658. offset = buffer->memory.size - canvas->cmd_offset;
  8659. cmd = nk_ptr_add(const struct nk_draw_command, memory, offset);
  8660. return cmd;
  8661. }
  8662. NK_API const struct nk_draw_command*
  8663. nk__draw_list_end(const struct nk_draw_list *canvas, const struct nk_buffer *buffer)
  8664. {
  8665. nk_size size;
  8666. nk_size offset;
  8667. nk_byte *memory;
  8668. const struct nk_draw_command *end;
  8669. NK_ASSERT(buffer);
  8670. NK_ASSERT(canvas);
  8671. if (!buffer || !canvas)
  8672. return 0;
  8673. memory = (nk_byte*)buffer->memory.ptr;
  8674. size = buffer->memory.size;
  8675. offset = size - canvas->cmd_offset;
  8676. end = nk_ptr_add(const struct nk_draw_command, memory, offset);
  8677. end -= (canvas->cmd_count-1);
  8678. return end;
  8679. }
  8680. NK_API const struct nk_draw_command*
  8681. nk__draw_list_next(const struct nk_draw_command *cmd,
  8682. const struct nk_buffer *buffer, const struct nk_draw_list *canvas)
  8683. {
  8684. const struct nk_draw_command *end;
  8685. NK_ASSERT(buffer);
  8686. NK_ASSERT(canvas);
  8687. if (!cmd || !buffer || !canvas)
  8688. return 0;
  8689. end = nk__draw_list_end(canvas, buffer);
  8690. if (cmd <= end) return 0;
  8691. return (cmd-1);
  8692. }
  8693. NK_INTERN struct nk_vec2*
  8694. nk_draw_list_alloc_path(struct nk_draw_list *list, int count)
  8695. {
  8696. struct nk_vec2 *points;
  8697. NK_STORAGE const nk_size point_align = NK_ALIGNOF(struct nk_vec2);
  8698. NK_STORAGE const nk_size point_size = sizeof(struct nk_vec2);
  8699. points = (struct nk_vec2*)
  8700. nk_buffer_alloc(list->buffer, NK_BUFFER_FRONT,
  8701. point_size * (nk_size)count, point_align);
  8702. if (!points) return 0;
  8703. if (!list->path_offset) {
  8704. void *memory = nk_buffer_memory(list->buffer);
  8705. list->path_offset = (unsigned int)((nk_byte*)points - (nk_byte*)memory);
  8706. }
  8707. list->path_count += (unsigned int)count;
  8708. return points;
  8709. }
  8710. NK_INTERN struct nk_vec2
  8711. nk_draw_list_path_last(struct nk_draw_list *list)
  8712. {
  8713. void *memory;
  8714. struct nk_vec2 *point;
  8715. NK_ASSERT(list->path_count);
  8716. memory = nk_buffer_memory(list->buffer);
  8717. point = nk_ptr_add(struct nk_vec2, memory, list->path_offset);
  8718. point += (list->path_count-1);
  8719. return *point;
  8720. }
  8721. NK_INTERN struct nk_draw_command*
  8722. nk_draw_list_push_command(struct nk_draw_list *list, struct nk_rect clip,
  8723. nk_handle texture)
  8724. {
  8725. NK_STORAGE const nk_size cmd_align = NK_ALIGNOF(struct nk_draw_command);
  8726. NK_STORAGE const nk_size cmd_size = sizeof(struct nk_draw_command);
  8727. struct nk_draw_command *cmd;
  8728. NK_ASSERT(list);
  8729. cmd = (struct nk_draw_command*)
  8730. nk_buffer_alloc(list->buffer, NK_BUFFER_BACK, cmd_size, cmd_align);
  8731. if (!cmd) return 0;
  8732. if (!list->cmd_count) {
  8733. nk_byte *memory = (nk_byte*)nk_buffer_memory(list->buffer);
  8734. nk_size total = nk_buffer_total(list->buffer);
  8735. memory = nk_ptr_add(nk_byte, memory, total);
  8736. list->cmd_offset = (nk_size)(memory - (nk_byte*)cmd);
  8737. }
  8738. cmd->elem_count = 0;
  8739. cmd->clip_rect = clip;
  8740. cmd->texture = texture;
  8741. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8742. cmd->userdata = list->userdata;
  8743. #endif
  8744. list->cmd_count++;
  8745. list->clip_rect = clip;
  8746. return cmd;
  8747. }
  8748. NK_INTERN struct nk_draw_command*
  8749. nk_draw_list_command_last(struct nk_draw_list *list)
  8750. {
  8751. void *memory;
  8752. nk_size size;
  8753. struct nk_draw_command *cmd;
  8754. NK_ASSERT(list->cmd_count);
  8755. memory = nk_buffer_memory(list->buffer);
  8756. size = nk_buffer_total(list->buffer);
  8757. cmd = nk_ptr_add(struct nk_draw_command, memory, size - list->cmd_offset);
  8758. return (cmd - (list->cmd_count-1));
  8759. }
  8760. NK_INTERN void
  8761. nk_draw_list_add_clip(struct nk_draw_list *list, struct nk_rect rect)
  8762. {
  8763. NK_ASSERT(list);
  8764. if (!list) return;
  8765. if (!list->cmd_count) {
  8766. nk_draw_list_push_command(list, rect, list->config.null.texture);
  8767. } else {
  8768. struct nk_draw_command *prev = nk_draw_list_command_last(list);
  8769. if (prev->elem_count == 0)
  8770. prev->clip_rect = rect;
  8771. nk_draw_list_push_command(list, rect, prev->texture);
  8772. }
  8773. }
  8774. NK_INTERN void
  8775. nk_draw_list_push_image(struct nk_draw_list *list, nk_handle texture)
  8776. {
  8777. NK_ASSERT(list);
  8778. if (!list) return;
  8779. if (!list->cmd_count) {
  8780. nk_draw_list_push_command(list, nk_null_rect, texture);
  8781. } else {
  8782. struct nk_draw_command *prev = nk_draw_list_command_last(list);
  8783. if (prev->elem_count == 0) {
  8784. prev->texture = texture;
  8785. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8786. prev->userdata = list->userdata;
  8787. #endif
  8788. } else if (prev->texture.id != texture.id
  8789. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8790. || prev->userdata.id != list->userdata.id
  8791. #endif
  8792. ) nk_draw_list_push_command(list, prev->clip_rect, texture);
  8793. }
  8794. }
  8795. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8796. NK_API void
  8797. nk_draw_list_push_userdata(struct nk_draw_list *list, nk_handle userdata)
  8798. {
  8799. list->userdata = userdata;
  8800. }
  8801. #endif
  8802. NK_INTERN void*
  8803. nk_draw_list_alloc_vertices(struct nk_draw_list *list, nk_size count)
  8804. {
  8805. void *vtx;
  8806. NK_ASSERT(list);
  8807. if (!list) return 0;
  8808. vtx = nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT,
  8809. list->config.vertex_size*count, list->config.vertex_alignment);
  8810. if (!vtx) return 0;
  8811. list->vertex_count += (unsigned int)count;
  8812. /* This assert triggers because your are drawing a lot of stuff and nuklear
  8813. * defined `nk_draw_index` as `nk_ushort` to safe space be default.
  8814. *
  8815. * So you reached the maximum number of indicies or rather vertexes.
  8816. * To solve this issue please change typdef `nk_draw_index` to `nk_uint`
  8817. * and don't forget to specify the new element size in your drawing
  8818. * backend (OpenGL, DirectX, ...). For example in OpenGL for `glDrawElements`
  8819. * instead of specifing `GL_UNSIGNED_SHORT` you have to define `GL_UNSIGNED_INT`.
  8820. * Sorry for the inconvenience. */
  8821. NK_ASSERT((sizeof(nk_draw_index) == 2 && list->vertex_count < NK_USHORT_MAX &&
  8822. "To many verticies for 16-bit vertex indicies. Please read comment above on how to solve this problem"));
  8823. return vtx;
  8824. }
  8825. NK_INTERN nk_draw_index*
  8826. nk_draw_list_alloc_elements(struct nk_draw_list *list, nk_size count)
  8827. {
  8828. nk_draw_index *ids;
  8829. struct nk_draw_command *cmd;
  8830. NK_STORAGE const nk_size elem_align = NK_ALIGNOF(nk_draw_index);
  8831. NK_STORAGE const nk_size elem_size = sizeof(nk_draw_index);
  8832. NK_ASSERT(list);
  8833. if (!list) return 0;
  8834. ids = (nk_draw_index*)
  8835. nk_buffer_alloc(list->elements, NK_BUFFER_FRONT, elem_size*count, elem_align);
  8836. if (!ids) return 0;
  8837. cmd = nk_draw_list_command_last(list);
  8838. list->element_count += (unsigned int)count;
  8839. cmd->elem_count += (unsigned int)count;
  8840. return ids;
  8841. }
  8842. NK_INTERN int
  8843. nk_draw_vertex_layout_element_is_end_of_layout(
  8844. const struct nk_draw_vertex_layout_element *element)
  8845. {
  8846. return (element->attribute == NK_VERTEX_ATTRIBUTE_COUNT ||
  8847. element->format == NK_FORMAT_COUNT);
  8848. }
  8849. NK_INTERN void
  8850. nk_draw_vertex_color(void *attr, const float *vals,
  8851. enum nk_draw_vertex_layout_format format)
  8852. {
  8853. /* if this triggers you tried to provide a value format for a color */
  8854. float val[4];
  8855. NK_ASSERT(format >= NK_FORMAT_COLOR_BEGIN);
  8856. NK_ASSERT(format <= NK_FORMAT_COLOR_END);
  8857. if (format < NK_FORMAT_COLOR_BEGIN || format > NK_FORMAT_COLOR_END) return;
  8858. val[0] = NK_SATURATE(vals[0]);
  8859. val[1] = NK_SATURATE(vals[1]);
  8860. val[2] = NK_SATURATE(vals[2]);
  8861. val[3] = NK_SATURATE(vals[3]);
  8862. switch (format) {
  8863. default: NK_ASSERT(0 && "Invalid vertex layout color format"); break;
  8864. case NK_FORMAT_R8G8B8A8:
  8865. case NK_FORMAT_R8G8B8: {
  8866. struct nk_color col = nk_rgba_fv(val);
  8867. NK_MEMCPY(attr, &col.r, sizeof(col));
  8868. } break;
  8869. case NK_FORMAT_B8G8R8A8: {
  8870. struct nk_color col = nk_rgba_fv(val);
  8871. struct nk_color bgra = nk_rgba(col.b, col.g, col.r, col.a);
  8872. NK_MEMCPY(attr, &bgra, sizeof(bgra));
  8873. } break;
  8874. case NK_FORMAT_R16G15B16: {
  8875. nk_ushort col[3];
  8876. col[0] = (nk_ushort)(val[0]*(float)NK_USHORT_MAX);
  8877. col[1] = (nk_ushort)(val[1]*(float)NK_USHORT_MAX);
  8878. col[2] = (nk_ushort)(val[2]*(float)NK_USHORT_MAX);
  8879. NK_MEMCPY(attr, col, sizeof(col));
  8880. } break;
  8881. case NK_FORMAT_R16G15B16A16: {
  8882. nk_ushort col[4];
  8883. col[0] = (nk_ushort)(val[0]*(float)NK_USHORT_MAX);
  8884. col[1] = (nk_ushort)(val[1]*(float)NK_USHORT_MAX);
  8885. col[2] = (nk_ushort)(val[2]*(float)NK_USHORT_MAX);
  8886. col[3] = (nk_ushort)(val[3]*(float)NK_USHORT_MAX);
  8887. NK_MEMCPY(attr, col, sizeof(col));
  8888. } break;
  8889. case NK_FORMAT_R32G32B32: {
  8890. nk_uint col[3];
  8891. col[0] = (nk_uint)(val[0]*(float)NK_UINT_MAX);
  8892. col[1] = (nk_uint)(val[1]*(float)NK_UINT_MAX);
  8893. col[2] = (nk_uint)(val[2]*(float)NK_UINT_MAX);
  8894. NK_MEMCPY(attr, col, sizeof(col));
  8895. } break;
  8896. case NK_FORMAT_R32G32B32A32: {
  8897. nk_uint col[4];
  8898. col[0] = (nk_uint)(val[0]*(float)NK_UINT_MAX);
  8899. col[1] = (nk_uint)(val[1]*(float)NK_UINT_MAX);
  8900. col[2] = (nk_uint)(val[2]*(float)NK_UINT_MAX);
  8901. col[3] = (nk_uint)(val[3]*(float)NK_UINT_MAX);
  8902. NK_MEMCPY(attr, col, sizeof(col));
  8903. } break;
  8904. case NK_FORMAT_R32G32B32A32_FLOAT:
  8905. NK_MEMCPY(attr, val, sizeof(float)*4);
  8906. break;
  8907. case NK_FORMAT_R32G32B32A32_DOUBLE: {
  8908. double col[4];
  8909. col[0] = (double)val[0];
  8910. col[1] = (double)val[1];
  8911. col[2] = (double)val[2];
  8912. col[3] = (double)val[3];
  8913. NK_MEMCPY(attr, col, sizeof(col));
  8914. } break;
  8915. case NK_FORMAT_RGB32:
  8916. case NK_FORMAT_RGBA32: {
  8917. struct nk_color col = nk_rgba_fv(val);
  8918. nk_uint color = nk_color_u32(col);
  8919. NK_MEMCPY(attr, &color, sizeof(color));
  8920. } break; }
  8921. }
  8922. NK_INTERN void
  8923. nk_draw_vertex_element(void *dst, const float *values, int value_count,
  8924. enum nk_draw_vertex_layout_format format)
  8925. {
  8926. int value_index;
  8927. void *attribute = dst;
  8928. /* if this triggers you tried to provide a color format for a value */
  8929. NK_ASSERT(format < NK_FORMAT_COLOR_BEGIN);
  8930. if (format >= NK_FORMAT_COLOR_BEGIN && format <= NK_FORMAT_COLOR_END) return;
  8931. for (value_index = 0; value_index < value_count; ++value_index) {
  8932. switch (format) {
  8933. default: NK_ASSERT(0 && "invalid vertex layout format"); break;
  8934. case NK_FORMAT_SCHAR: {
  8935. char value = (char)NK_CLAMP((float)NK_SCHAR_MIN, values[value_index], (float)NK_SCHAR_MAX);
  8936. NK_MEMCPY(attribute, &value, sizeof(value));
  8937. attribute = (void*)((char*)attribute + sizeof(char));
  8938. } break;
  8939. case NK_FORMAT_SSHORT: {
  8940. nk_short value = (nk_short)NK_CLAMP((float)NK_SSHORT_MIN, values[value_index], (float)NK_SSHORT_MAX);
  8941. NK_MEMCPY(attribute, &value, sizeof(value));
  8942. attribute = (void*)((char*)attribute + sizeof(value));
  8943. } break;
  8944. case NK_FORMAT_SINT: {
  8945. nk_int value = (nk_int)NK_CLAMP((float)NK_SINT_MIN, values[value_index], (float)NK_SINT_MAX);
  8946. NK_MEMCPY(attribute, &value, sizeof(value));
  8947. attribute = (void*)((char*)attribute + sizeof(nk_int));
  8948. } break;
  8949. case NK_FORMAT_UCHAR: {
  8950. unsigned char value = (unsigned char)NK_CLAMP((float)NK_UCHAR_MIN, values[value_index], (float)NK_UCHAR_MAX);
  8951. NK_MEMCPY(attribute, &value, sizeof(value));
  8952. attribute = (void*)((char*)attribute + sizeof(unsigned char));
  8953. } break;
  8954. case NK_FORMAT_USHORT: {
  8955. nk_ushort value = (nk_ushort)NK_CLAMP((float)NK_USHORT_MIN, values[value_index], (float)NK_USHORT_MAX);
  8956. NK_MEMCPY(attribute, &value, sizeof(value));
  8957. attribute = (void*)((char*)attribute + sizeof(value));
  8958. } break;
  8959. case NK_FORMAT_UINT: {
  8960. nk_uint value = (nk_uint)NK_CLAMP((float)NK_UINT_MIN, values[value_index], (float)NK_UINT_MAX);
  8961. NK_MEMCPY(attribute, &value, sizeof(value));
  8962. attribute = (void*)((char*)attribute + sizeof(nk_uint));
  8963. } break;
  8964. case NK_FORMAT_FLOAT:
  8965. NK_MEMCPY(attribute, &values[value_index], sizeof(values[value_index]));
  8966. attribute = (void*)((char*)attribute + sizeof(float));
  8967. break;
  8968. case NK_FORMAT_DOUBLE: {
  8969. double value = (double)values[value_index];
  8970. NK_MEMCPY(attribute, &value, sizeof(value));
  8971. attribute = (void*)((char*)attribute + sizeof(double));
  8972. } break;
  8973. }
  8974. }
  8975. }
  8976. NK_INTERN void*
  8977. nk_draw_vertex(void *dst, const struct nk_convert_config *config,
  8978. struct nk_vec2 pos, struct nk_vec2 uv, struct nk_colorf color)
  8979. {
  8980. void *result = (void*)((char*)dst + config->vertex_size);
  8981. const struct nk_draw_vertex_layout_element *elem_iter = config->vertex_layout;
  8982. while (!nk_draw_vertex_layout_element_is_end_of_layout(elem_iter)) {
  8983. void *address = (void*)((char*)dst + elem_iter->offset);
  8984. switch (elem_iter->attribute) {
  8985. case NK_VERTEX_ATTRIBUTE_COUNT:
  8986. default: NK_ASSERT(0 && "wrong element attribute"); break;
  8987. case NK_VERTEX_POSITION: nk_draw_vertex_element(address, &pos.x, 2, elem_iter->format); break;
  8988. case NK_VERTEX_TEXCOORD: nk_draw_vertex_element(address, &uv.x, 2, elem_iter->format); break;
  8989. case NK_VERTEX_COLOR: nk_draw_vertex_color(address, &color.r, elem_iter->format); break;
  8990. }
  8991. elem_iter++;
  8992. }
  8993. return result;
  8994. }
  8995. NK_API void
  8996. nk_draw_list_stroke_poly_line(struct nk_draw_list *list, const struct nk_vec2 *points,
  8997. const unsigned int points_count, struct nk_color color, enum nk_draw_list_stroke closed,
  8998. float thickness, enum nk_anti_aliasing aliasing)
  8999. {
  9000. nk_size count;
  9001. int thick_line;
  9002. struct nk_colorf col;
  9003. struct nk_colorf col_trans;
  9004. NK_ASSERT(list);
  9005. if (!list || points_count < 2) return;
  9006. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  9007. count = points_count;
  9008. if (!closed) count = points_count-1;
  9009. thick_line = thickness > 1.0f;
  9010. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9011. nk_draw_list_push_userdata(list, list->userdata);
  9012. #endif
  9013. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  9014. nk_color_fv(&col.r, color);
  9015. col_trans = col;
  9016. col_trans.a = 0;
  9017. if (aliasing == NK_ANTI_ALIASING_ON) {
  9018. /* ANTI-ALIASED STROKE */
  9019. const float AA_SIZE = 1.0f;
  9020. NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
  9021. NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
  9022. /* allocate vertices and elements */
  9023. nk_size i1 = 0;
  9024. nk_size vertex_offset;
  9025. nk_size index = list->vertex_count;
  9026. const nk_size idx_count = (thick_line) ? (count * 18) : (count * 12);
  9027. const nk_size vtx_count = (thick_line) ? (points_count * 4): (points_count *3);
  9028. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9029. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  9030. nk_size size;
  9031. struct nk_vec2 *normals, *temp;
  9032. if (!vtx || !ids) return;
  9033. /* temporary allocate normals + points */
  9034. vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
  9035. nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
  9036. size = pnt_size * ((thick_line) ? 5 : 3) * points_count;
  9037. normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
  9038. if (!normals) return;
  9039. temp = normals + points_count;
  9040. /* make sure vertex pointer is still correct */
  9041. vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
  9042. /* calculate normals */
  9043. for (i1 = 0; i1 < count; ++i1) {
  9044. const nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
  9045. struct nk_vec2 diff = nk_vec2_sub(points[i2], points[i1]);
  9046. float len;
  9047. /* vec2 inverted length */
  9048. len = nk_vec2_len_sqr(diff);
  9049. if (len != 0.0f)
  9050. len = nk_inv_sqrt(len);
  9051. else len = 1.0f;
  9052. diff = nk_vec2_muls(diff, len);
  9053. normals[i1].x = diff.y;
  9054. normals[i1].y = -diff.x;
  9055. }
  9056. if (!closed)
  9057. normals[points_count-1] = normals[points_count-2];
  9058. if (!thick_line) {
  9059. nk_size idx1, i;
  9060. if (!closed) {
  9061. struct nk_vec2 d;
  9062. temp[0] = nk_vec2_add(points[0], nk_vec2_muls(normals[0], AA_SIZE));
  9063. temp[1] = nk_vec2_sub(points[0], nk_vec2_muls(normals[0], AA_SIZE));
  9064. d = nk_vec2_muls(normals[points_count-1], AA_SIZE);
  9065. temp[(points_count-1) * 2 + 0] = nk_vec2_add(points[points_count-1], d);
  9066. temp[(points_count-1) * 2 + 1] = nk_vec2_sub(points[points_count-1], d);
  9067. }
  9068. /* fill elements */
  9069. idx1 = index;
  9070. for (i1 = 0; i1 < count; i1++) {
  9071. struct nk_vec2 dm;
  9072. float dmr2;
  9073. nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
  9074. nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 3);
  9075. /* average normals */
  9076. dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
  9077. dmr2 = dm.x * dm.x + dm.y* dm.y;
  9078. if (dmr2 > 0.000001f) {
  9079. float scale = 1.0f/dmr2;
  9080. scale = NK_MIN(100.0f, scale);
  9081. dm = nk_vec2_muls(dm, scale);
  9082. }
  9083. dm = nk_vec2_muls(dm, AA_SIZE);
  9084. temp[i2*2+0] = nk_vec2_add(points[i2], dm);
  9085. temp[i2*2+1] = nk_vec2_sub(points[i2], dm);
  9086. ids[0] = (nk_draw_index)(idx2 + 0); ids[1] = (nk_draw_index)(idx1+0);
  9087. ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
  9088. ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+0);
  9089. ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
  9090. ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
  9091. ids[10]= (nk_draw_index)(idx2 + 0); ids[11]= (nk_draw_index)(idx2+1);
  9092. ids += 12;
  9093. idx1 = idx2;
  9094. }
  9095. /* fill vertices */
  9096. for (i = 0; i < points_count; ++i) {
  9097. const struct nk_vec2 uv = list->config.null.uv;
  9098. vtx = nk_draw_vertex(vtx, &list->config, points[i], uv, col);
  9099. vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+0], uv, col_trans);
  9100. vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+1], uv, col_trans);
  9101. }
  9102. } else {
  9103. nk_size idx1, i;
  9104. const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f;
  9105. if (!closed) {
  9106. struct nk_vec2 d1 = nk_vec2_muls(normals[0], half_inner_thickness + AA_SIZE);
  9107. struct nk_vec2 d2 = nk_vec2_muls(normals[0], half_inner_thickness);
  9108. temp[0] = nk_vec2_add(points[0], d1);
  9109. temp[1] = nk_vec2_add(points[0], d2);
  9110. temp[2] = nk_vec2_sub(points[0], d2);
  9111. temp[3] = nk_vec2_sub(points[0], d1);
  9112. d1 = nk_vec2_muls(normals[points_count-1], half_inner_thickness + AA_SIZE);
  9113. d2 = nk_vec2_muls(normals[points_count-1], half_inner_thickness);
  9114. temp[(points_count-1)*4+0] = nk_vec2_add(points[points_count-1], d1);
  9115. temp[(points_count-1)*4+1] = nk_vec2_add(points[points_count-1], d2);
  9116. temp[(points_count-1)*4+2] = nk_vec2_sub(points[points_count-1], d2);
  9117. temp[(points_count-1)*4+3] = nk_vec2_sub(points[points_count-1], d1);
  9118. }
  9119. /* add all elements */
  9120. idx1 = index;
  9121. for (i1 = 0; i1 < count; ++i1) {
  9122. struct nk_vec2 dm_out, dm_in;
  9123. const nk_size i2 = ((i1+1) == points_count) ? 0: (i1 + 1);
  9124. nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 4);
  9125. /* average normals */
  9126. struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
  9127. float dmr2 = dm.x * dm.x + dm.y* dm.y;
  9128. if (dmr2 > 0.000001f) {
  9129. float scale = 1.0f/dmr2;
  9130. scale = NK_MIN(100.0f, scale);
  9131. dm = nk_vec2_muls(dm, scale);
  9132. }
  9133. dm_out = nk_vec2_muls(dm, ((half_inner_thickness) + AA_SIZE));
  9134. dm_in = nk_vec2_muls(dm, half_inner_thickness);
  9135. temp[i2*4+0] = nk_vec2_add(points[i2], dm_out);
  9136. temp[i2*4+1] = nk_vec2_add(points[i2], dm_in);
  9137. temp[i2*4+2] = nk_vec2_sub(points[i2], dm_in);
  9138. temp[i2*4+3] = nk_vec2_sub(points[i2], dm_out);
  9139. /* add indexes */
  9140. ids[0] = (nk_draw_index)(idx2 + 1); ids[1] = (nk_draw_index)(idx1+1);
  9141. ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
  9142. ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+1);
  9143. ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
  9144. ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
  9145. ids[10]= (nk_draw_index)(idx2 + 0); ids[11] = (nk_draw_index)(idx2+1);
  9146. ids[12]= (nk_draw_index)(idx2 + 2); ids[13] = (nk_draw_index)(idx1+2);
  9147. ids[14]= (nk_draw_index)(idx1 + 3); ids[15] = (nk_draw_index)(idx1+3);
  9148. ids[16]= (nk_draw_index)(idx2 + 3); ids[17] = (nk_draw_index)(idx2+2);
  9149. ids += 18;
  9150. idx1 = idx2;
  9151. }
  9152. /* add vertices */
  9153. for (i = 0; i < points_count; ++i) {
  9154. const struct nk_vec2 uv = list->config.null.uv;
  9155. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+0], uv, col_trans);
  9156. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+1], uv, col);
  9157. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+2], uv, col);
  9158. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+3], uv, col_trans);
  9159. }
  9160. }
  9161. /* free temporary normals + points */
  9162. nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
  9163. } else {
  9164. /* NON ANTI-ALIASED STROKE */
  9165. nk_size i1 = 0;
  9166. nk_size idx = list->vertex_count;
  9167. const nk_size idx_count = count * 6;
  9168. const nk_size vtx_count = count * 4;
  9169. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9170. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  9171. if (!vtx || !ids) return;
  9172. for (i1 = 0; i1 < count; ++i1) {
  9173. float dx, dy;
  9174. const struct nk_vec2 uv = list->config.null.uv;
  9175. const nk_size i2 = ((i1+1) == points_count) ? 0 : i1 + 1;
  9176. const struct nk_vec2 p1 = points[i1];
  9177. const struct nk_vec2 p2 = points[i2];
  9178. struct nk_vec2 diff = nk_vec2_sub(p2, p1);
  9179. float len;
  9180. /* vec2 inverted length */
  9181. len = nk_vec2_len_sqr(diff);
  9182. if (len != 0.0f)
  9183. len = nk_inv_sqrt(len);
  9184. else len = 1.0f;
  9185. diff = nk_vec2_muls(diff, len);
  9186. /* add vertices */
  9187. dx = diff.x * (thickness * 0.5f);
  9188. dy = diff.y * (thickness * 0.5f);
  9189. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x + dy, p1.y - dx), uv, col);
  9190. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x + dy, p2.y - dx), uv, col);
  9191. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x - dy, p2.y + dx), uv, col);
  9192. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x - dy, p1.y + dx), uv, col);
  9193. ids[0] = (nk_draw_index)(idx+0); ids[1] = (nk_draw_index)(idx+1);
  9194. ids[2] = (nk_draw_index)(idx+2); ids[3] = (nk_draw_index)(idx+0);
  9195. ids[4] = (nk_draw_index)(idx+2); ids[5] = (nk_draw_index)(idx+3);
  9196. ids += 6;
  9197. idx += 4;
  9198. }
  9199. }
  9200. }
  9201. NK_API void
  9202. nk_draw_list_fill_poly_convex(struct nk_draw_list *list,
  9203. const struct nk_vec2 *points, const unsigned int points_count,
  9204. struct nk_color color, enum nk_anti_aliasing aliasing)
  9205. {
  9206. struct nk_colorf col;
  9207. struct nk_colorf col_trans;
  9208. NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
  9209. NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
  9210. NK_ASSERT(list);
  9211. if (!list || points_count < 3) return;
  9212. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9213. nk_draw_list_push_userdata(list, list->userdata);
  9214. #endif
  9215. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  9216. nk_color_fv(&col.r, color);
  9217. col_trans = col;
  9218. col_trans.a = 0;
  9219. if (aliasing == NK_ANTI_ALIASING_ON) {
  9220. nk_size i = 0;
  9221. nk_size i0 = 0;
  9222. nk_size i1 = 0;
  9223. const float AA_SIZE = 1.0f;
  9224. nk_size vertex_offset = 0;
  9225. nk_size index = list->vertex_count;
  9226. const nk_size idx_count = (points_count-2)*3 + points_count*6;
  9227. const nk_size vtx_count = (points_count*2);
  9228. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9229. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  9230. nk_size size = 0;
  9231. struct nk_vec2 *normals = 0;
  9232. unsigned int vtx_inner_idx = (unsigned int)(index + 0);
  9233. unsigned int vtx_outer_idx = (unsigned int)(index + 1);
  9234. if (!vtx || !ids) return;
  9235. /* temporary allocate normals */
  9236. vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
  9237. nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
  9238. size = pnt_size * points_count;
  9239. normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
  9240. if (!normals) return;
  9241. vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
  9242. /* add elements */
  9243. for (i = 2; i < points_count; i++) {
  9244. ids[0] = (nk_draw_index)(vtx_inner_idx);
  9245. ids[1] = (nk_draw_index)(vtx_inner_idx + ((i-1) << 1));
  9246. ids[2] = (nk_draw_index)(vtx_inner_idx + (i << 1));
  9247. ids += 3;
  9248. }
  9249. /* compute normals */
  9250. for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
  9251. struct nk_vec2 p0 = points[i0];
  9252. struct nk_vec2 p1 = points[i1];
  9253. struct nk_vec2 diff = nk_vec2_sub(p1, p0);
  9254. /* vec2 inverted length */
  9255. float len = nk_vec2_len_sqr(diff);
  9256. if (len != 0.0f)
  9257. len = nk_inv_sqrt(len);
  9258. else len = 1.0f;
  9259. diff = nk_vec2_muls(diff, len);
  9260. normals[i0].x = diff.y;
  9261. normals[i0].y = -diff.x;
  9262. }
  9263. /* add vertices + indexes */
  9264. for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
  9265. const struct nk_vec2 uv = list->config.null.uv;
  9266. struct nk_vec2 n0 = normals[i0];
  9267. struct nk_vec2 n1 = normals[i1];
  9268. struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(n0, n1), 0.5f);
  9269. float dmr2 = dm.x*dm.x + dm.y*dm.y;
  9270. if (dmr2 > 0.000001f) {
  9271. float scale = 1.0f / dmr2;
  9272. scale = NK_MIN(scale, 100.0f);
  9273. dm = nk_vec2_muls(dm, scale);
  9274. }
  9275. dm = nk_vec2_muls(dm, AA_SIZE * 0.5f);
  9276. /* add vertices */
  9277. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_sub(points[i1], dm), uv, col);
  9278. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_add(points[i1], dm), uv, col_trans);
  9279. /* add indexes */
  9280. ids[0] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
  9281. ids[1] = (nk_draw_index)(vtx_inner_idx+(i0<<1));
  9282. ids[2] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
  9283. ids[3] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
  9284. ids[4] = (nk_draw_index)(vtx_outer_idx+(i1<<1));
  9285. ids[5] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
  9286. ids += 6;
  9287. }
  9288. /* free temporary normals + points */
  9289. nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
  9290. } else {
  9291. nk_size i = 0;
  9292. nk_size index = list->vertex_count;
  9293. const nk_size idx_count = (points_count-2)*3;
  9294. const nk_size vtx_count = points_count;
  9295. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9296. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  9297. if (!vtx || !ids) return;
  9298. for (i = 0; i < vtx_count; ++i)
  9299. vtx = nk_draw_vertex(vtx, &list->config, points[i], list->config.null.uv, col);
  9300. for (i = 2; i < points_count; ++i) {
  9301. ids[0] = (nk_draw_index)index;
  9302. ids[1] = (nk_draw_index)(index+ i - 1);
  9303. ids[2] = (nk_draw_index)(index+i);
  9304. ids += 3;
  9305. }
  9306. }
  9307. }
  9308. NK_API void
  9309. nk_draw_list_path_clear(struct nk_draw_list *list)
  9310. {
  9311. NK_ASSERT(list);
  9312. if (!list) return;
  9313. nk_buffer_reset(list->buffer, NK_BUFFER_FRONT);
  9314. list->path_count = 0;
  9315. list->path_offset = 0;
  9316. }
  9317. NK_API void
  9318. nk_draw_list_path_line_to(struct nk_draw_list *list, struct nk_vec2 pos)
  9319. {
  9320. struct nk_vec2 *points = 0;
  9321. struct nk_draw_command *cmd = 0;
  9322. NK_ASSERT(list);
  9323. if (!list) return;
  9324. if (!list->cmd_count)
  9325. nk_draw_list_add_clip(list, nk_null_rect);
  9326. cmd = nk_draw_list_command_last(list);
  9327. if (cmd && cmd->texture.ptr != list->config.null.texture.ptr)
  9328. nk_draw_list_push_image(list, list->config.null.texture);
  9329. points = nk_draw_list_alloc_path(list, 1);
  9330. if (!points) return;
  9331. points[0] = pos;
  9332. }
  9333. NK_API void
  9334. nk_draw_list_path_arc_to_fast(struct nk_draw_list *list, struct nk_vec2 center,
  9335. float radius, int a_min, int a_max)
  9336. {
  9337. int a = 0;
  9338. NK_ASSERT(list);
  9339. if (!list) return;
  9340. if (a_min <= a_max) {
  9341. for (a = a_min; a <= a_max; a++) {
  9342. const struct nk_vec2 c = list->circle_vtx[(nk_size)a % NK_LEN(list->circle_vtx)];
  9343. const float x = center.x + c.x * radius;
  9344. const float y = center.y + c.y * radius;
  9345. nk_draw_list_path_line_to(list, nk_vec2(x, y));
  9346. }
  9347. }
  9348. }
  9349. NK_API void
  9350. nk_draw_list_path_arc_to(struct nk_draw_list *list, struct nk_vec2 center,
  9351. float radius, float a_min, float a_max, unsigned int segments)
  9352. {
  9353. unsigned int i = 0;
  9354. NK_ASSERT(list);
  9355. if (!list) return;
  9356. if (radius == 0.0f) return;
  9357. /* This algorithm for arc drawing relies on these two trigonometric identities[1]:
  9358. sin(a + b) = sin(a) * cos(b) + cos(a) * sin(b)
  9359. cos(a + b) = cos(a) * cos(b) - sin(a) * sin(b)
  9360. Two coordinates (x, y) of a point on a circle centered on
  9361. the origin can be written in polar form as:
  9362. x = r * cos(a)
  9363. y = r * sin(a)
  9364. where r is the radius of the circle,
  9365. a is the angle between (x, y) and the origin.
  9366. This allows us to rotate the coordinates around the
  9367. origin by an angle b using the following transformation:
  9368. x' = r * cos(a + b) = x * cos(b) - y * sin(b)
  9369. y' = r * sin(a + b) = y * cos(b) + x * sin(b)
  9370. [1] https://en.wikipedia.org/wiki/List_of_trigonometric_identities#Angle_sum_and_difference_identities
  9371. */
  9372. {const float d_angle = (a_max - a_min) / (float)segments;
  9373. const float sin_d = (float)NK_SIN(d_angle);
  9374. const float cos_d = (float)NK_COS(d_angle);
  9375. float cx = (float)NK_COS(a_min) * radius;
  9376. float cy = (float)NK_SIN(a_min) * radius;
  9377. for(i = 0; i <= segments; ++i) {
  9378. float new_cx, new_cy;
  9379. const float x = center.x + cx;
  9380. const float y = center.y + cy;
  9381. nk_draw_list_path_line_to(list, nk_vec2(x, y));
  9382. new_cx = cx * cos_d - cy * sin_d;
  9383. new_cy = cy * cos_d + cx * sin_d;
  9384. cx = new_cx;
  9385. cy = new_cy;
  9386. }}
  9387. }
  9388. NK_API void
  9389. nk_draw_list_path_rect_to(struct nk_draw_list *list, struct nk_vec2 a,
  9390. struct nk_vec2 b, float rounding)
  9391. {
  9392. float r;
  9393. NK_ASSERT(list);
  9394. if (!list) return;
  9395. r = rounding;
  9396. r = NK_MIN(r, ((b.x-a.x) < 0) ? -(b.x-a.x): (b.x-a.x));
  9397. r = NK_MIN(r, ((b.y-a.y) < 0) ? -(b.y-a.y): (b.y-a.y));
  9398. if (r == 0.0f) {
  9399. nk_draw_list_path_line_to(list, a);
  9400. nk_draw_list_path_line_to(list, nk_vec2(b.x,a.y));
  9401. nk_draw_list_path_line_to(list, b);
  9402. nk_draw_list_path_line_to(list, nk_vec2(a.x,b.y));
  9403. } else {
  9404. nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, a.y + r), r, 6, 9);
  9405. nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, a.y + r), r, 9, 12);
  9406. nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, b.y - r), r, 0, 3);
  9407. nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, b.y - r), r, 3, 6);
  9408. }
  9409. }
  9410. NK_API void
  9411. nk_draw_list_path_curve_to(struct nk_draw_list *list, struct nk_vec2 p2,
  9412. struct nk_vec2 p3, struct nk_vec2 p4, unsigned int num_segments)
  9413. {
  9414. float t_step;
  9415. unsigned int i_step;
  9416. struct nk_vec2 p1;
  9417. NK_ASSERT(list);
  9418. NK_ASSERT(list->path_count);
  9419. if (!list || !list->path_count) return;
  9420. num_segments = NK_MAX(num_segments, 1);
  9421. p1 = nk_draw_list_path_last(list);
  9422. t_step = 1.0f/(float)num_segments;
  9423. for (i_step = 1; i_step <= num_segments; ++i_step) {
  9424. float t = t_step * (float)i_step;
  9425. float u = 1.0f - t;
  9426. float w1 = u*u*u;
  9427. float w2 = 3*u*u*t;
  9428. float w3 = 3*u*t*t;
  9429. float w4 = t * t *t;
  9430. float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
  9431. float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
  9432. nk_draw_list_path_line_to(list, nk_vec2(x,y));
  9433. }
  9434. }
  9435. NK_API void
  9436. nk_draw_list_path_fill(struct nk_draw_list *list, struct nk_color color)
  9437. {
  9438. struct nk_vec2 *points;
  9439. NK_ASSERT(list);
  9440. if (!list) return;
  9441. points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
  9442. nk_draw_list_fill_poly_convex(list, points, list->path_count, color, list->config.shape_AA);
  9443. nk_draw_list_path_clear(list);
  9444. }
  9445. NK_API void
  9446. nk_draw_list_path_stroke(struct nk_draw_list *list, struct nk_color color,
  9447. enum nk_draw_list_stroke closed, float thickness)
  9448. {
  9449. struct nk_vec2 *points;
  9450. NK_ASSERT(list);
  9451. if (!list) return;
  9452. points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
  9453. nk_draw_list_stroke_poly_line(list, points, list->path_count, color,
  9454. closed, thickness, list->config.line_AA);
  9455. nk_draw_list_path_clear(list);
  9456. }
  9457. NK_API void
  9458. nk_draw_list_stroke_line(struct nk_draw_list *list, struct nk_vec2 a,
  9459. struct nk_vec2 b, struct nk_color col, float thickness)
  9460. {
  9461. NK_ASSERT(list);
  9462. if (!list || !col.a) return;
  9463. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  9464. nk_draw_list_path_line_to(list, a);
  9465. nk_draw_list_path_line_to(list, b);
  9466. } else {
  9467. nk_draw_list_path_line_to(list, nk_vec2_sub(a,nk_vec2(0.5f,0.5f)));
  9468. nk_draw_list_path_line_to(list, nk_vec2_sub(b,nk_vec2(0.5f,0.5f)));
  9469. }
  9470. nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
  9471. }
  9472. NK_API void
  9473. nk_draw_list_fill_rect(struct nk_draw_list *list, struct nk_rect rect,
  9474. struct nk_color col, float rounding)
  9475. {
  9476. NK_ASSERT(list);
  9477. if (!list || !col.a) return;
  9478. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  9479. nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
  9480. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9481. } else {
  9482. nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
  9483. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9484. } nk_draw_list_path_fill(list, col);
  9485. }
  9486. NK_API void
  9487. nk_draw_list_stroke_rect(struct nk_draw_list *list, struct nk_rect rect,
  9488. struct nk_color col, float rounding, float thickness)
  9489. {
  9490. NK_ASSERT(list);
  9491. if (!list || !col.a) return;
  9492. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  9493. nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
  9494. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9495. } else {
  9496. nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
  9497. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9498. } nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  9499. }
  9500. NK_API void
  9501. nk_draw_list_fill_rect_multi_color(struct nk_draw_list *list, struct nk_rect rect,
  9502. struct nk_color left, struct nk_color top, struct nk_color right,
  9503. struct nk_color bottom)
  9504. {
  9505. void *vtx;
  9506. struct nk_colorf col_left, col_top;
  9507. struct nk_colorf col_right, col_bottom;
  9508. nk_draw_index *idx;
  9509. nk_draw_index index;
  9510. nk_color_fv(&col_left.r, left);
  9511. nk_color_fv(&col_right.r, right);
  9512. nk_color_fv(&col_top.r, top);
  9513. nk_color_fv(&col_bottom.r, bottom);
  9514. NK_ASSERT(list);
  9515. if (!list) return;
  9516. nk_draw_list_push_image(list, list->config.null.texture);
  9517. index = (nk_draw_index)list->vertex_count;
  9518. vtx = nk_draw_list_alloc_vertices(list, 4);
  9519. idx = nk_draw_list_alloc_elements(list, 6);
  9520. if (!vtx || !idx) return;
  9521. idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
  9522. idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
  9523. idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
  9524. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y), list->config.null.uv, col_left);
  9525. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y), list->config.null.uv, col_top);
  9526. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y + rect.h), list->config.null.uv, col_right);
  9527. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y + rect.h), list->config.null.uv, col_bottom);
  9528. }
  9529. NK_API void
  9530. nk_draw_list_fill_triangle(struct nk_draw_list *list, struct nk_vec2 a,
  9531. struct nk_vec2 b, struct nk_vec2 c, struct nk_color col)
  9532. {
  9533. NK_ASSERT(list);
  9534. if (!list || !col.a) return;
  9535. nk_draw_list_path_line_to(list, a);
  9536. nk_draw_list_path_line_to(list, b);
  9537. nk_draw_list_path_line_to(list, c);
  9538. nk_draw_list_path_fill(list, col);
  9539. }
  9540. NK_API void
  9541. nk_draw_list_stroke_triangle(struct nk_draw_list *list, struct nk_vec2 a,
  9542. struct nk_vec2 b, struct nk_vec2 c, struct nk_color col, float thickness)
  9543. {
  9544. NK_ASSERT(list);
  9545. if (!list || !col.a) return;
  9546. nk_draw_list_path_line_to(list, a);
  9547. nk_draw_list_path_line_to(list, b);
  9548. nk_draw_list_path_line_to(list, c);
  9549. nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  9550. }
  9551. NK_API void
  9552. nk_draw_list_fill_circle(struct nk_draw_list *list, struct nk_vec2 center,
  9553. float radius, struct nk_color col, unsigned int segs)
  9554. {
  9555. float a_max;
  9556. NK_ASSERT(list);
  9557. if (!list || !col.a) return;
  9558. a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
  9559. nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
  9560. nk_draw_list_path_fill(list, col);
  9561. }
  9562. NK_API void
  9563. nk_draw_list_stroke_circle(struct nk_draw_list *list, struct nk_vec2 center,
  9564. float radius, struct nk_color col, unsigned int segs, float thickness)
  9565. {
  9566. float a_max;
  9567. NK_ASSERT(list);
  9568. if (!list || !col.a) return;
  9569. a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
  9570. nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
  9571. nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  9572. }
  9573. NK_API void
  9574. nk_draw_list_stroke_curve(struct nk_draw_list *list, struct nk_vec2 p0,
  9575. struct nk_vec2 cp0, struct nk_vec2 cp1, struct nk_vec2 p1,
  9576. struct nk_color col, unsigned int segments, float thickness)
  9577. {
  9578. NK_ASSERT(list);
  9579. if (!list || !col.a) return;
  9580. nk_draw_list_path_line_to(list, p0);
  9581. nk_draw_list_path_curve_to(list, cp0, cp1, p1, segments);
  9582. nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
  9583. }
  9584. NK_INTERN void
  9585. nk_draw_list_push_rect_uv(struct nk_draw_list *list, struct nk_vec2 a,
  9586. struct nk_vec2 c, struct nk_vec2 uva, struct nk_vec2 uvc,
  9587. struct nk_color color)
  9588. {
  9589. void *vtx;
  9590. struct nk_vec2 uvb;
  9591. struct nk_vec2 uvd;
  9592. struct nk_vec2 b;
  9593. struct nk_vec2 d;
  9594. struct nk_colorf col;
  9595. nk_draw_index *idx;
  9596. nk_draw_index index;
  9597. NK_ASSERT(list);
  9598. if (!list) return;
  9599. nk_color_fv(&col.r, color);
  9600. uvb = nk_vec2(uvc.x, uva.y);
  9601. uvd = nk_vec2(uva.x, uvc.y);
  9602. b = nk_vec2(c.x, a.y);
  9603. d = nk_vec2(a.x, c.y);
  9604. index = (nk_draw_index)list->vertex_count;
  9605. vtx = nk_draw_list_alloc_vertices(list, 4);
  9606. idx = nk_draw_list_alloc_elements(list, 6);
  9607. if (!vtx || !idx) return;
  9608. idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
  9609. idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
  9610. idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
  9611. vtx = nk_draw_vertex(vtx, &list->config, a, uva, col);
  9612. vtx = nk_draw_vertex(vtx, &list->config, b, uvb, col);
  9613. vtx = nk_draw_vertex(vtx, &list->config, c, uvc, col);
  9614. vtx = nk_draw_vertex(vtx, &list->config, d, uvd, col);
  9615. }
  9616. NK_API void
  9617. nk_draw_list_add_image(struct nk_draw_list *list, struct nk_image texture,
  9618. struct nk_rect rect, struct nk_color color)
  9619. {
  9620. NK_ASSERT(list);
  9621. if (!list) return;
  9622. /* push new command with given texture */
  9623. nk_draw_list_push_image(list, texture.handle);
  9624. if (nk_image_is_subimage(&texture)) {
  9625. /* add region inside of the texture */
  9626. struct nk_vec2 uv[2];
  9627. uv[0].x = (float)texture.region[0]/(float)texture.w;
  9628. uv[0].y = (float)texture.region[1]/(float)texture.h;
  9629. uv[1].x = (float)(texture.region[0] + texture.region[2])/(float)texture.w;
  9630. uv[1].y = (float)(texture.region[1] + texture.region[3])/(float)texture.h;
  9631. nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
  9632. nk_vec2(rect.x + rect.w, rect.y + rect.h), uv[0], uv[1], color);
  9633. } else nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
  9634. nk_vec2(rect.x + rect.w, rect.y + rect.h),
  9635. nk_vec2(0.0f, 0.0f), nk_vec2(1.0f, 1.0f),color);
  9636. }
  9637. NK_API void
  9638. nk_draw_list_add_text(struct nk_draw_list *list, const struct nk_user_font *font,
  9639. struct nk_rect rect, const char *text, int len, float font_height,
  9640. struct nk_color fg)
  9641. {
  9642. float x = 0;
  9643. int text_len = 0;
  9644. nk_rune unicode = 0;
  9645. nk_rune next = 0;
  9646. int glyph_len = 0;
  9647. int next_glyph_len = 0;
  9648. struct nk_user_font_glyph g;
  9649. NK_ASSERT(list);
  9650. if (!list || !len || !text) return;
  9651. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  9652. list->clip_rect.x, list->clip_rect.y, list->clip_rect.w, list->clip_rect.h)) return;
  9653. nk_draw_list_push_image(list, font->texture);
  9654. x = rect.x;
  9655. glyph_len = nk_utf_decode(text, &unicode, len);
  9656. if (!glyph_len) return;
  9657. /* draw every glyph image */
  9658. fg.a = (nk_byte)((float)fg.a * list->config.global_alpha);
  9659. while (text_len < len && glyph_len) {
  9660. float gx, gy, gh, gw;
  9661. float char_width = 0;
  9662. if (unicode == NK_UTF_INVALID) break;
  9663. /* query currently drawn glyph information */
  9664. next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
  9665. font->query(font->userdata, font_height, &g, unicode,
  9666. (next == NK_UTF_INVALID) ? '\0' : next);
  9667. /* calculate and draw glyph drawing rectangle and image */
  9668. gx = x + g.offset.x;
  9669. gy = rect.y + g.offset.y;
  9670. gw = g.width; gh = g.height;
  9671. char_width = g.xadvance;
  9672. nk_draw_list_push_rect_uv(list, nk_vec2(gx,gy), nk_vec2(gx + gw, gy+ gh),
  9673. g.uv[0], g.uv[1], fg);
  9674. /* offset next glyph */
  9675. text_len += glyph_len;
  9676. x += char_width;
  9677. glyph_len = next_glyph_len;
  9678. unicode = next;
  9679. }
  9680. }
  9681. NK_API nk_flags
  9682. nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
  9683. struct nk_buffer *vertices, struct nk_buffer *elements,
  9684. const struct nk_convert_config *config)
  9685. {
  9686. nk_flags res = NK_CONVERT_SUCCESS;
  9687. const struct nk_command *cmd;
  9688. NK_ASSERT(ctx);
  9689. NK_ASSERT(cmds);
  9690. NK_ASSERT(vertices);
  9691. NK_ASSERT(elements);
  9692. NK_ASSERT(config);
  9693. NK_ASSERT(config->vertex_layout);
  9694. NK_ASSERT(config->vertex_size);
  9695. if (!ctx || !cmds || !vertices || !elements || !config || !config->vertex_layout)
  9696. return NK_CONVERT_INVALID_PARAM;
  9697. nk_draw_list_setup(&ctx->draw_list, config, cmds, vertices, elements,
  9698. config->line_AA, config->shape_AA);
  9699. nk_foreach(cmd, ctx)
  9700. {
  9701. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9702. ctx->draw_list.userdata = cmd->userdata;
  9703. #endif
  9704. switch (cmd->type) {
  9705. case NK_COMMAND_NOP: break;
  9706. case NK_COMMAND_SCISSOR: {
  9707. const struct nk_command_scissor *s = (const struct nk_command_scissor*)cmd;
  9708. nk_draw_list_add_clip(&ctx->draw_list, nk_rect(s->x, s->y, s->w, s->h));
  9709. } break;
  9710. case NK_COMMAND_LINE: {
  9711. const struct nk_command_line *l = (const struct nk_command_line*)cmd;
  9712. nk_draw_list_stroke_line(&ctx->draw_list, nk_vec2(l->begin.x, l->begin.y),
  9713. nk_vec2(l->end.x, l->end.y), l->color, l->line_thickness);
  9714. } break;
  9715. case NK_COMMAND_CURVE: {
  9716. const struct nk_command_curve *q = (const struct nk_command_curve*)cmd;
  9717. nk_draw_list_stroke_curve(&ctx->draw_list, nk_vec2(q->begin.x, q->begin.y),
  9718. nk_vec2(q->ctrl[0].x, q->ctrl[0].y), nk_vec2(q->ctrl[1].x,
  9719. q->ctrl[1].y), nk_vec2(q->end.x, q->end.y), q->color,
  9720. config->curve_segment_count, q->line_thickness);
  9721. } break;
  9722. case NK_COMMAND_RECT: {
  9723. const struct nk_command_rect *r = (const struct nk_command_rect*)cmd;
  9724. nk_draw_list_stroke_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  9725. r->color, (float)r->rounding, r->line_thickness);
  9726. } break;
  9727. case NK_COMMAND_RECT_FILLED: {
  9728. const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled*)cmd;
  9729. nk_draw_list_fill_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  9730. r->color, (float)r->rounding);
  9731. } break;
  9732. case NK_COMMAND_RECT_MULTI_COLOR: {
  9733. const struct nk_command_rect_multi_color *r = (const struct nk_command_rect_multi_color*)cmd;
  9734. nk_draw_list_fill_rect_multi_color(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  9735. r->left, r->top, r->right, r->bottom);
  9736. } break;
  9737. case NK_COMMAND_CIRCLE: {
  9738. const struct nk_command_circle *c = (const struct nk_command_circle*)cmd;
  9739. nk_draw_list_stroke_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
  9740. (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
  9741. config->circle_segment_count, c->line_thickness);
  9742. } break;
  9743. case NK_COMMAND_CIRCLE_FILLED: {
  9744. const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
  9745. nk_draw_list_fill_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
  9746. (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
  9747. config->circle_segment_count);
  9748. } break;
  9749. case NK_COMMAND_ARC: {
  9750. const struct nk_command_arc *c = (const struct nk_command_arc*)cmd;
  9751. nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
  9752. nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
  9753. c->a[0], c->a[1], config->arc_segment_count);
  9754. nk_draw_list_path_stroke(&ctx->draw_list, c->color, NK_STROKE_CLOSED, c->line_thickness);
  9755. } break;
  9756. case NK_COMMAND_ARC_FILLED: {
  9757. const struct nk_command_arc_filled *c = (const struct nk_command_arc_filled*)cmd;
  9758. nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
  9759. nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
  9760. c->a[0], c->a[1], config->arc_segment_count);
  9761. nk_draw_list_path_fill(&ctx->draw_list, c->color);
  9762. } break;
  9763. case NK_COMMAND_TRIANGLE: {
  9764. const struct nk_command_triangle *t = (const struct nk_command_triangle*)cmd;
  9765. nk_draw_list_stroke_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
  9766. nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color,
  9767. t->line_thickness);
  9768. } break;
  9769. case NK_COMMAND_TRIANGLE_FILLED: {
  9770. const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled*)cmd;
  9771. nk_draw_list_fill_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
  9772. nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color);
  9773. } break;
  9774. case NK_COMMAND_POLYGON: {
  9775. int i;
  9776. const struct nk_command_polygon*p = (const struct nk_command_polygon*)cmd;
  9777. for (i = 0; i < p->point_count; ++i) {
  9778. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  9779. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  9780. }
  9781. nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_CLOSED, p->line_thickness);
  9782. } break;
  9783. case NK_COMMAND_POLYGON_FILLED: {
  9784. int i;
  9785. const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled*)cmd;
  9786. for (i = 0; i < p->point_count; ++i) {
  9787. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  9788. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  9789. }
  9790. nk_draw_list_path_fill(&ctx->draw_list, p->color);
  9791. } break;
  9792. case NK_COMMAND_POLYLINE: {
  9793. int i;
  9794. const struct nk_command_polyline *p = (const struct nk_command_polyline*)cmd;
  9795. for (i = 0; i < p->point_count; ++i) {
  9796. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  9797. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  9798. }
  9799. nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_OPEN, p->line_thickness);
  9800. } break;
  9801. case NK_COMMAND_TEXT: {
  9802. const struct nk_command_text *t = (const struct nk_command_text*)cmd;
  9803. nk_draw_list_add_text(&ctx->draw_list, t->font, nk_rect(t->x, t->y, t->w, t->h),
  9804. t->string, t->length, t->height, t->foreground);
  9805. } break;
  9806. case NK_COMMAND_IMAGE: {
  9807. const struct nk_command_image *i = (const struct nk_command_image*)cmd;
  9808. nk_draw_list_add_image(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
  9809. } break;
  9810. case NK_COMMAND_CUSTOM: {
  9811. const struct nk_command_custom *c = (const struct nk_command_custom*)cmd;
  9812. c->callback(&ctx->draw_list, c->x, c->y, c->w, c->h, c->callback_data);
  9813. } break;
  9814. default: break;
  9815. }
  9816. }
  9817. res |= (cmds->needed > cmds->allocated + (cmds->memory.size - cmds->size)) ? NK_CONVERT_COMMAND_BUFFER_FULL: 0;
  9818. res |= (vertices->needed > vertices->allocated) ? NK_CONVERT_VERTEX_BUFFER_FULL: 0;
  9819. res |= (elements->needed > elements->allocated) ? NK_CONVERT_ELEMENT_BUFFER_FULL: 0;
  9820. return res;
  9821. }
  9822. NK_API const struct nk_draw_command*
  9823. nk__draw_begin(const struct nk_context *ctx,
  9824. const struct nk_buffer *buffer)
  9825. {
  9826. return nk__draw_list_begin(&ctx->draw_list, buffer);
  9827. }
  9828. NK_API const struct nk_draw_command*
  9829. nk__draw_end(const struct nk_context *ctx, const struct nk_buffer *buffer)
  9830. {
  9831. return nk__draw_list_end(&ctx->draw_list, buffer);
  9832. }
  9833. NK_API const struct nk_draw_command*
  9834. nk__draw_next(const struct nk_draw_command *cmd,
  9835. const struct nk_buffer *buffer, const struct nk_context *ctx)
  9836. {
  9837. return nk__draw_list_next(cmd, buffer, &ctx->draw_list);
  9838. }
  9839. #endif
  9840. #ifdef NK_INCLUDE_FONT_BAKING
  9841. /* -------------------------------------------------------------
  9842. *
  9843. * RECT PACK
  9844. *
  9845. * --------------------------------------------------------------*/
  9846. /* stb_rect_pack.h - v0.05 - public domain - rectangle packing */
  9847. /* Sean Barrett 2014 */
  9848. #define NK_RP__MAXVAL 0xffff
  9849. typedef unsigned short nk_rp_coord;
  9850. struct nk_rp_rect {
  9851. /* reserved for your use: */
  9852. int id;
  9853. /* input: */
  9854. nk_rp_coord w, h;
  9855. /* output: */
  9856. nk_rp_coord x, y;
  9857. int was_packed;
  9858. /* non-zero if valid packing */
  9859. }; /* 16 bytes, nominally */
  9860. struct nk_rp_node {
  9861. nk_rp_coord x,y;
  9862. struct nk_rp_node *next;
  9863. };
  9864. struct nk_rp_context {
  9865. int width;
  9866. int height;
  9867. int align;
  9868. int init_mode;
  9869. int heuristic;
  9870. int num_nodes;
  9871. struct nk_rp_node *active_head;
  9872. struct nk_rp_node *free_head;
  9873. struct nk_rp_node extra[2];
  9874. /* we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2' */
  9875. };
  9876. struct nk_rp__findresult {
  9877. int x,y;
  9878. struct nk_rp_node **prev_link;
  9879. };
  9880. enum NK_RP_HEURISTIC {
  9881. NK_RP_HEURISTIC_Skyline_default=0,
  9882. NK_RP_HEURISTIC_Skyline_BL_sortHeight = NK_RP_HEURISTIC_Skyline_default,
  9883. NK_RP_HEURISTIC_Skyline_BF_sortHeight
  9884. };
  9885. enum NK_RP_INIT_STATE{NK_RP__INIT_skyline = 1};
  9886. NK_INTERN void
  9887. nk_rp_setup_allow_out_of_mem(struct nk_rp_context *context, int allow_out_of_mem)
  9888. {
  9889. if (allow_out_of_mem)
  9890. /* if it's ok to run out of memory, then don't bother aligning them; */
  9891. /* this gives better packing, but may fail due to OOM (even though */
  9892. /* the rectangles easily fit). @TODO a smarter approach would be to only */
  9893. /* quantize once we've hit OOM, then we could get rid of this parameter. */
  9894. context->align = 1;
  9895. else {
  9896. /* if it's not ok to run out of memory, then quantize the widths */
  9897. /* so that num_nodes is always enough nodes. */
  9898. /* */
  9899. /* I.e. num_nodes * align >= width */
  9900. /* align >= width / num_nodes */
  9901. /* align = ceil(width/num_nodes) */
  9902. context->align = (context->width + context->num_nodes-1) / context->num_nodes;
  9903. }
  9904. }
  9905. NK_INTERN void
  9906. nk_rp_init_target(struct nk_rp_context *context, int width, int height,
  9907. struct nk_rp_node *nodes, int num_nodes)
  9908. {
  9909. int i;
  9910. #ifndef STBRP_LARGE_RECTS
  9911. NK_ASSERT(width <= 0xffff && height <= 0xffff);
  9912. #endif
  9913. for (i=0; i < num_nodes-1; ++i)
  9914. nodes[i].next = &nodes[i+1];
  9915. nodes[i].next = 0;
  9916. context->init_mode = NK_RP__INIT_skyline;
  9917. context->heuristic = NK_RP_HEURISTIC_Skyline_default;
  9918. context->free_head = &nodes[0];
  9919. context->active_head = &context->extra[0];
  9920. context->width = width;
  9921. context->height = height;
  9922. context->num_nodes = num_nodes;
  9923. nk_rp_setup_allow_out_of_mem(context, 0);
  9924. /* node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly) */
  9925. context->extra[0].x = 0;
  9926. context->extra[0].y = 0;
  9927. context->extra[0].next = &context->extra[1];
  9928. context->extra[1].x = (nk_rp_coord) width;
  9929. context->extra[1].y = 65535;
  9930. context->extra[1].next = 0;
  9931. }
  9932. /* find minimum y position if it starts at x1 */
  9933. NK_INTERN int
  9934. nk_rp__skyline_find_min_y(struct nk_rp_context *c, struct nk_rp_node *first,
  9935. int x0, int width, int *pwaste)
  9936. {
  9937. struct nk_rp_node *node = first;
  9938. int x1 = x0 + width;
  9939. int min_y, visited_width, waste_area;
  9940. NK_ASSERT(first->x <= x0);
  9941. NK_UNUSED(c);
  9942. NK_ASSERT(node->next->x > x0);
  9943. /* we ended up handling this in the caller for efficiency */
  9944. NK_ASSERT(node->x <= x0);
  9945. min_y = 0;
  9946. waste_area = 0;
  9947. visited_width = 0;
  9948. while (node->x < x1)
  9949. {
  9950. if (node->y > min_y) {
  9951. /* raise min_y higher. */
  9952. /* we've accounted for all waste up to min_y, */
  9953. /* but we'll now add more waste for everything we've visited */
  9954. waste_area += visited_width * (node->y - min_y);
  9955. min_y = node->y;
  9956. /* the first time through, visited_width might be reduced */
  9957. if (node->x < x0)
  9958. visited_width += node->next->x - x0;
  9959. else
  9960. visited_width += node->next->x - node->x;
  9961. } else {
  9962. /* add waste area */
  9963. int under_width = node->next->x - node->x;
  9964. if (under_width + visited_width > width)
  9965. under_width = width - visited_width;
  9966. waste_area += under_width * (min_y - node->y);
  9967. visited_width += under_width;
  9968. }
  9969. node = node->next;
  9970. }
  9971. *pwaste = waste_area;
  9972. return min_y;
  9973. }
  9974. NK_INTERN struct nk_rp__findresult
  9975. nk_rp__skyline_find_best_pos(struct nk_rp_context *c, int width, int height)
  9976. {
  9977. int best_waste = (1<<30), best_x, best_y = (1 << 30);
  9978. struct nk_rp__findresult fr;
  9979. struct nk_rp_node **prev, *node, *tail, **best = 0;
  9980. /* align to multiple of c->align */
  9981. width = (width + c->align - 1);
  9982. width -= width % c->align;
  9983. NK_ASSERT(width % c->align == 0);
  9984. node = c->active_head;
  9985. prev = &c->active_head;
  9986. while (node->x + width <= c->width) {
  9987. int y,waste;
  9988. y = nk_rp__skyline_find_min_y(c, node, node->x, width, &waste);
  9989. /* actually just want to test BL */
  9990. if (c->heuristic == NK_RP_HEURISTIC_Skyline_BL_sortHeight) {
  9991. /* bottom left */
  9992. if (y < best_y) {
  9993. best_y = y;
  9994. best = prev;
  9995. }
  9996. } else {
  9997. /* best-fit */
  9998. if (y + height <= c->height) {
  9999. /* can only use it if it first vertically */
  10000. if (y < best_y || (y == best_y && waste < best_waste)) {
  10001. best_y = y;
  10002. best_waste = waste;
  10003. best = prev;
  10004. }
  10005. }
  10006. }
  10007. prev = &node->next;
  10008. node = node->next;
  10009. }
  10010. best_x = (best == 0) ? 0 : (*best)->x;
  10011. /* if doing best-fit (BF), we also have to try aligning right edge to each node position */
  10012. /* */
  10013. /* e.g, if fitting */
  10014. /* */
  10015. /* ____________________ */
  10016. /* |____________________| */
  10017. /* */
  10018. /* into */
  10019. /* */
  10020. /* | | */
  10021. /* | ____________| */
  10022. /* |____________| */
  10023. /* */
  10024. /* then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned */
  10025. /* */
  10026. /* This makes BF take about 2x the time */
  10027. if (c->heuristic == NK_RP_HEURISTIC_Skyline_BF_sortHeight)
  10028. {
  10029. tail = c->active_head;
  10030. node = c->active_head;
  10031. prev = &c->active_head;
  10032. /* find first node that's admissible */
  10033. while (tail->x < width)
  10034. tail = tail->next;
  10035. while (tail)
  10036. {
  10037. int xpos = tail->x - width;
  10038. int y,waste;
  10039. NK_ASSERT(xpos >= 0);
  10040. /* find the left position that matches this */
  10041. while (node->next->x <= xpos) {
  10042. prev = &node->next;
  10043. node = node->next;
  10044. }
  10045. NK_ASSERT(node->next->x > xpos && node->x <= xpos);
  10046. y = nk_rp__skyline_find_min_y(c, node, xpos, width, &waste);
  10047. if (y + height < c->height) {
  10048. if (y <= best_y) {
  10049. if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
  10050. best_x = xpos;
  10051. NK_ASSERT(y <= best_y);
  10052. best_y = y;
  10053. best_waste = waste;
  10054. best = prev;
  10055. }
  10056. }
  10057. }
  10058. tail = tail->next;
  10059. }
  10060. }
  10061. fr.prev_link = best;
  10062. fr.x = best_x;
  10063. fr.y = best_y;
  10064. return fr;
  10065. }
  10066. NK_INTERN struct nk_rp__findresult
  10067. nk_rp__skyline_pack_rectangle(struct nk_rp_context *context, int width, int height)
  10068. {
  10069. /* find best position according to heuristic */
  10070. struct nk_rp__findresult res = nk_rp__skyline_find_best_pos(context, width, height);
  10071. struct nk_rp_node *node, *cur;
  10072. /* bail if: */
  10073. /* 1. it failed */
  10074. /* 2. the best node doesn't fit (we don't always check this) */
  10075. /* 3. we're out of memory */
  10076. if (res.prev_link == 0 || res.y + height > context->height || context->free_head == 0) {
  10077. res.prev_link = 0;
  10078. return res;
  10079. }
  10080. /* on success, create new node */
  10081. node = context->free_head;
  10082. node->x = (nk_rp_coord) res.x;
  10083. node->y = (nk_rp_coord) (res.y + height);
  10084. context->free_head = node->next;
  10085. /* insert the new node into the right starting point, and */
  10086. /* let 'cur' point to the remaining nodes needing to be */
  10087. /* stitched back in */
  10088. cur = *res.prev_link;
  10089. if (cur->x < res.x) {
  10090. /* preserve the existing one, so start testing with the next one */
  10091. struct nk_rp_node *next = cur->next;
  10092. cur->next = node;
  10093. cur = next;
  10094. } else {
  10095. *res.prev_link = node;
  10096. }
  10097. /* from here, traverse cur and free the nodes, until we get to one */
  10098. /* that shouldn't be freed */
  10099. while (cur->next && cur->next->x <= res.x + width) {
  10100. struct nk_rp_node *next = cur->next;
  10101. /* move the current node to the free list */
  10102. cur->next = context->free_head;
  10103. context->free_head = cur;
  10104. cur = next;
  10105. }
  10106. /* stitch the list back in */
  10107. node->next = cur;
  10108. if (cur->x < res.x + width)
  10109. cur->x = (nk_rp_coord) (res.x + width);
  10110. return res;
  10111. }
  10112. NK_INTERN int
  10113. nk_rect_height_compare(const void *a, const void *b)
  10114. {
  10115. const struct nk_rp_rect *p = (const struct nk_rp_rect *) a;
  10116. const struct nk_rp_rect *q = (const struct nk_rp_rect *) b;
  10117. if (p->h > q->h)
  10118. return -1;
  10119. if (p->h < q->h)
  10120. return 1;
  10121. return (p->w > q->w) ? -1 : (p->w < q->w);
  10122. }
  10123. NK_INTERN int
  10124. nk_rect_original_order(const void *a, const void *b)
  10125. {
  10126. const struct nk_rp_rect *p = (const struct nk_rp_rect *) a;
  10127. const struct nk_rp_rect *q = (const struct nk_rp_rect *) b;
  10128. return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
  10129. }
  10130. NK_INTERN void
  10131. nk_rp_qsort(struct nk_rp_rect *array, unsigned int len, int(*cmp)(const void*,const void*))
  10132. {
  10133. /* iterative quick sort */
  10134. #define NK_MAX_SORT_STACK 64
  10135. unsigned right, left = 0, stack[NK_MAX_SORT_STACK], pos = 0;
  10136. unsigned seed = len/2 * 69069+1;
  10137. for (;;) {
  10138. for (; left+1 < len; len++) {
  10139. struct nk_rp_rect pivot, tmp;
  10140. if (pos == NK_MAX_SORT_STACK) len = stack[pos = 0];
  10141. pivot = array[left+seed%(len-left)];
  10142. seed = seed * 69069 + 1;
  10143. stack[pos++] = len;
  10144. for (right = left-1;;) {
  10145. while (cmp(&array[++right], &pivot) < 0);
  10146. while (cmp(&pivot, &array[--len]) < 0);
  10147. if (right >= len) break;
  10148. tmp = array[right];
  10149. array[right] = array[len];
  10150. array[len] = tmp;
  10151. }
  10152. }
  10153. if (pos == 0) break;
  10154. left = len;
  10155. len = stack[--pos];
  10156. }
  10157. #undef NK_MAX_SORT_STACK
  10158. }
  10159. NK_INTERN void
  10160. nk_rp_pack_rects(struct nk_rp_context *context, struct nk_rp_rect *rects, int num_rects)
  10161. {
  10162. int i;
  10163. /* we use the 'was_packed' field internally to allow sorting/unsorting */
  10164. for (i=0; i < num_rects; ++i) {
  10165. rects[i].was_packed = i;
  10166. }
  10167. /* sort according to heuristic */
  10168. nk_rp_qsort(rects, (unsigned)num_rects, nk_rect_height_compare);
  10169. for (i=0; i < num_rects; ++i) {
  10170. struct nk_rp__findresult fr = nk_rp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
  10171. if (fr.prev_link) {
  10172. rects[i].x = (nk_rp_coord) fr.x;
  10173. rects[i].y = (nk_rp_coord) fr.y;
  10174. } else {
  10175. rects[i].x = rects[i].y = NK_RP__MAXVAL;
  10176. }
  10177. }
  10178. /* unsort */
  10179. nk_rp_qsort(rects, (unsigned)num_rects, nk_rect_original_order);
  10180. /* set was_packed flags */
  10181. for (i=0; i < num_rects; ++i)
  10182. rects[i].was_packed = !(rects[i].x == NK_RP__MAXVAL && rects[i].y == NK_RP__MAXVAL);
  10183. }
  10184. /*
  10185. * ==============================================================
  10186. *
  10187. * TRUETYPE
  10188. *
  10189. * ===============================================================
  10190. */
  10191. /* stb_truetype.h - v1.07 - public domain */
  10192. #define NK_TT_MAX_OVERSAMPLE 8
  10193. #define NK_TT__OVER_MASK (NK_TT_MAX_OVERSAMPLE-1)
  10194. struct nk_tt_bakedchar {
  10195. unsigned short x0,y0,x1,y1;
  10196. /* coordinates of bbox in bitmap */
  10197. float xoff,yoff,xadvance;
  10198. };
  10199. struct nk_tt_aligned_quad{
  10200. float x0,y0,s0,t0; /* top-left */
  10201. float x1,y1,s1,t1; /* bottom-right */
  10202. };
  10203. struct nk_tt_packedchar {
  10204. unsigned short x0,y0,x1,y1;
  10205. /* coordinates of bbox in bitmap */
  10206. float xoff,yoff,xadvance;
  10207. float xoff2,yoff2;
  10208. };
  10209. struct nk_tt_pack_range {
  10210. float font_size;
  10211. int first_unicode_codepoint_in_range;
  10212. /* if non-zero, then the chars are continuous, and this is the first codepoint */
  10213. int *array_of_unicode_codepoints;
  10214. /* if non-zero, then this is an array of unicode codepoints */
  10215. int num_chars;
  10216. struct nk_tt_packedchar *chardata_for_range; /* output */
  10217. unsigned char h_oversample, v_oversample;
  10218. /* don't set these, they're used internally */
  10219. };
  10220. struct nk_tt_pack_context {
  10221. void *pack_info;
  10222. int width;
  10223. int height;
  10224. int stride_in_bytes;
  10225. int padding;
  10226. unsigned int h_oversample, v_oversample;
  10227. unsigned char *pixels;
  10228. void *nodes;
  10229. };
  10230. struct nk_tt_fontinfo {
  10231. const unsigned char* data; /* pointer to .ttf file */
  10232. int fontstart;/* offset of start of font */
  10233. int numGlyphs;/* number of glyphs, needed for range checking */
  10234. int loca,head,glyf,hhea,hmtx,kern; /* table locations as offset from start of .ttf */
  10235. int index_map; /* a cmap mapping for our chosen character encoding */
  10236. int indexToLocFormat; /* format needed to map from glyph index to glyph */
  10237. };
  10238. enum {
  10239. NK_TT_vmove=1,
  10240. NK_TT_vline,
  10241. NK_TT_vcurve
  10242. };
  10243. struct nk_tt_vertex {
  10244. short x,y,cx,cy;
  10245. unsigned char type,padding;
  10246. };
  10247. struct nk_tt__bitmap{
  10248. int w,h,stride;
  10249. unsigned char *pixels;
  10250. };
  10251. struct nk_tt__hheap_chunk {
  10252. struct nk_tt__hheap_chunk *next;
  10253. };
  10254. struct nk_tt__hheap {
  10255. struct nk_allocator alloc;
  10256. struct nk_tt__hheap_chunk *head;
  10257. void *first_free;
  10258. int num_remaining_in_head_chunk;
  10259. };
  10260. struct nk_tt__edge {
  10261. float x0,y0, x1,y1;
  10262. int invert;
  10263. };
  10264. struct nk_tt__active_edge {
  10265. struct nk_tt__active_edge *next;
  10266. float fx,fdx,fdy;
  10267. float direction;
  10268. float sy;
  10269. float ey;
  10270. };
  10271. struct nk_tt__point {float x,y;};
  10272. #define NK_TT_MACSTYLE_DONTCARE 0
  10273. #define NK_TT_MACSTYLE_BOLD 1
  10274. #define NK_TT_MACSTYLE_ITALIC 2
  10275. #define NK_TT_MACSTYLE_UNDERSCORE 4
  10276. #define NK_TT_MACSTYLE_NONE 8
  10277. /* <= not same as 0, this makes us check the bitfield is 0 */
  10278. enum { /* platformID */
  10279. NK_TT_PLATFORM_ID_UNICODE =0,
  10280. NK_TT_PLATFORM_ID_MAC =1,
  10281. NK_TT_PLATFORM_ID_ISO =2,
  10282. NK_TT_PLATFORM_ID_MICROSOFT =3
  10283. };
  10284. enum { /* encodingID for NK_TT_PLATFORM_ID_UNICODE */
  10285. NK_TT_UNICODE_EID_UNICODE_1_0 =0,
  10286. NK_TT_UNICODE_EID_UNICODE_1_1 =1,
  10287. NK_TT_UNICODE_EID_ISO_10646 =2,
  10288. NK_TT_UNICODE_EID_UNICODE_2_0_BMP=3,
  10289. NK_TT_UNICODE_EID_UNICODE_2_0_FULL=4
  10290. };
  10291. enum { /* encodingID for NK_TT_PLATFORM_ID_MICROSOFT */
  10292. NK_TT_MS_EID_SYMBOL =0,
  10293. NK_TT_MS_EID_UNICODE_BMP =1,
  10294. NK_TT_MS_EID_SHIFTJIS =2,
  10295. NK_TT_MS_EID_UNICODE_FULL =10
  10296. };
  10297. enum { /* encodingID for NK_TT_PLATFORM_ID_MAC; same as Script Manager codes */
  10298. NK_TT_MAC_EID_ROMAN =0, NK_TT_MAC_EID_ARABIC =4,
  10299. NK_TT_MAC_EID_JAPANESE =1, NK_TT_MAC_EID_HEBREW =5,
  10300. NK_TT_MAC_EID_CHINESE_TRAD =2, NK_TT_MAC_EID_GREEK =6,
  10301. NK_TT_MAC_EID_KOREAN =3, NK_TT_MAC_EID_RUSSIAN =7
  10302. };
  10303. enum { /* languageID for NK_TT_PLATFORM_ID_MICROSOFT; same as LCID... */
  10304. /* problematic because there are e.g. 16 english LCIDs and 16 arabic LCIDs */
  10305. NK_TT_MS_LANG_ENGLISH =0x0409, NK_TT_MS_LANG_ITALIAN =0x0410,
  10306. NK_TT_MS_LANG_CHINESE =0x0804, NK_TT_MS_LANG_JAPANESE =0x0411,
  10307. NK_TT_MS_LANG_DUTCH =0x0413, NK_TT_MS_LANG_KOREAN =0x0412,
  10308. NK_TT_MS_LANG_FRENCH =0x040c, NK_TT_MS_LANG_RUSSIAN =0x0419,
  10309. NK_TT_MS_LANG_GERMAN =0x0407, NK_TT_MS_LANG_SPANISH =0x0409,
  10310. NK_TT_MS_LANG_HEBREW =0x040d, NK_TT_MS_LANG_SWEDISH =0x041D
  10311. };
  10312. enum { /* languageID for NK_TT_PLATFORM_ID_MAC */
  10313. NK_TT_MAC_LANG_ENGLISH =0 , NK_TT_MAC_LANG_JAPANESE =11,
  10314. NK_TT_MAC_LANG_ARABIC =12, NK_TT_MAC_LANG_KOREAN =23,
  10315. NK_TT_MAC_LANG_DUTCH =4 , NK_TT_MAC_LANG_RUSSIAN =32,
  10316. NK_TT_MAC_LANG_FRENCH =1 , NK_TT_MAC_LANG_SPANISH =6 ,
  10317. NK_TT_MAC_LANG_GERMAN =2 , NK_TT_MAC_LANG_SWEDISH =5 ,
  10318. NK_TT_MAC_LANG_HEBREW =10, NK_TT_MAC_LANG_CHINESE_SIMPLIFIED =33,
  10319. NK_TT_MAC_LANG_ITALIAN =3 , NK_TT_MAC_LANG_CHINESE_TRAD =19
  10320. };
  10321. #define nk_ttBYTE(p) (* (const nk_byte *) (p))
  10322. #define nk_ttCHAR(p) (* (const char *) (p))
  10323. #if defined(NK_BIGENDIAN) && !defined(NK_ALLOW_UNALIGNED_TRUETYPE)
  10324. #define nk_ttUSHORT(p) (* (nk_ushort *) (p))
  10325. #define nk_ttSHORT(p) (* (nk_short *) (p))
  10326. #define nk_ttULONG(p) (* (nk_uint *) (p))
  10327. #define nk_ttLONG(p) (* (nk_int *) (p))
  10328. #else
  10329. static nk_ushort nk_ttUSHORT(const nk_byte *p) { return (nk_ushort)(p[0]*256 + p[1]); }
  10330. static nk_short nk_ttSHORT(const nk_byte *p) { return (nk_short)(p[0]*256 + p[1]); }
  10331. static nk_uint nk_ttULONG(const nk_byte *p) { return (nk_uint)((p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]); }
  10332. #endif
  10333. #define nk_tt_tag4(p,c0,c1,c2,c3)\
  10334. ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
  10335. #define nk_tt_tag(p,str) nk_tt_tag4(p,str[0],str[1],str[2],str[3])
  10336. NK_INTERN int nk_tt_GetGlyphShape(const struct nk_tt_fontinfo *info, struct nk_allocator *alloc,
  10337. int glyph_index, struct nk_tt_vertex **pvertices);
  10338. NK_INTERN nk_uint
  10339. nk_tt__find_table(const nk_byte *data, nk_uint fontstart, const char *tag)
  10340. {
  10341. /* @OPTIMIZE: binary search */
  10342. nk_int num_tables = nk_ttUSHORT(data+fontstart+4);
  10343. nk_uint tabledir = fontstart + 12;
  10344. nk_int i;
  10345. for (i = 0; i < num_tables; ++i) {
  10346. nk_uint loc = tabledir + (nk_uint)(16*i);
  10347. if (nk_tt_tag(data+loc+0, tag))
  10348. return nk_ttULONG(data+loc+8);
  10349. }
  10350. return 0;
  10351. }
  10352. NK_INTERN int
  10353. nk_tt_InitFont(struct nk_tt_fontinfo *info, const unsigned char *data2, int fontstart)
  10354. {
  10355. nk_uint cmap, t;
  10356. nk_int i,numTables;
  10357. const nk_byte *data = (const nk_byte *) data2;
  10358. info->data = data;
  10359. info->fontstart = fontstart;
  10360. cmap = nk_tt__find_table(data, (nk_uint)fontstart, "cmap"); /* required */
  10361. info->loca = (int)nk_tt__find_table(data, (nk_uint)fontstart, "loca"); /* required */
  10362. info->head = (int)nk_tt__find_table(data, (nk_uint)fontstart, "head"); /* required */
  10363. info->glyf = (int)nk_tt__find_table(data, (nk_uint)fontstart, "glyf"); /* required */
  10364. info->hhea = (int)nk_tt__find_table(data, (nk_uint)fontstart, "hhea"); /* required */
  10365. info->hmtx = (int)nk_tt__find_table(data, (nk_uint)fontstart, "hmtx"); /* required */
  10366. info->kern = (int)nk_tt__find_table(data, (nk_uint)fontstart, "kern"); /* not required */
  10367. if (!cmap || !info->loca || !info->head || !info->glyf || !info->hhea || !info->hmtx)
  10368. return 0;
  10369. t = nk_tt__find_table(data, (nk_uint)fontstart, "maxp");
  10370. if (t) info->numGlyphs = nk_ttUSHORT(data+t+4);
  10371. else info->numGlyphs = 0xffff;
  10372. /* find a cmap encoding table we understand *now* to avoid searching */
  10373. /* later. (todo: could make this installable) */
  10374. /* the same regardless of glyph. */
  10375. numTables = nk_ttUSHORT(data + cmap + 2);
  10376. info->index_map = 0;
  10377. for (i=0; i < numTables; ++i)
  10378. {
  10379. nk_uint encoding_record = cmap + 4 + 8 * (nk_uint)i;
  10380. /* find an encoding we understand: */
  10381. switch(nk_ttUSHORT(data+encoding_record)) {
  10382. case NK_TT_PLATFORM_ID_MICROSOFT:
  10383. switch (nk_ttUSHORT(data+encoding_record+2)) {
  10384. case NK_TT_MS_EID_UNICODE_BMP:
  10385. case NK_TT_MS_EID_UNICODE_FULL:
  10386. /* MS/Unicode */
  10387. info->index_map = (int)(cmap + nk_ttULONG(data+encoding_record+4));
  10388. break;
  10389. default: break;
  10390. } break;
  10391. case NK_TT_PLATFORM_ID_UNICODE:
  10392. /* Mac/iOS has these */
  10393. /* all the encodingIDs are unicode, so we don't bother to check it */
  10394. info->index_map = (int)(cmap + nk_ttULONG(data+encoding_record+4));
  10395. break;
  10396. default: break;
  10397. }
  10398. }
  10399. if (info->index_map == 0)
  10400. return 0;
  10401. info->indexToLocFormat = nk_ttUSHORT(data+info->head + 50);
  10402. return 1;
  10403. }
  10404. NK_INTERN int
  10405. nk_tt_FindGlyphIndex(const struct nk_tt_fontinfo *info, int unicode_codepoint)
  10406. {
  10407. const nk_byte *data = info->data;
  10408. nk_uint index_map = (nk_uint)info->index_map;
  10409. nk_ushort format = nk_ttUSHORT(data + index_map + 0);
  10410. if (format == 0) { /* apple byte encoding */
  10411. nk_int bytes = nk_ttUSHORT(data + index_map + 2);
  10412. if (unicode_codepoint < bytes-6)
  10413. return nk_ttBYTE(data + index_map + 6 + unicode_codepoint);
  10414. return 0;
  10415. } else if (format == 6) {
  10416. nk_uint first = nk_ttUSHORT(data + index_map + 6);
  10417. nk_uint count = nk_ttUSHORT(data + index_map + 8);
  10418. if ((nk_uint) unicode_codepoint >= first && (nk_uint) unicode_codepoint < first+count)
  10419. return nk_ttUSHORT(data + index_map + 10 + (unicode_codepoint - (int)first)*2);
  10420. return 0;
  10421. } else if (format == 2) {
  10422. NK_ASSERT(0); /* @TODO: high-byte mapping for japanese/chinese/korean */
  10423. return 0;
  10424. } else if (format == 4) { /* standard mapping for windows fonts: binary search collection of ranges */
  10425. nk_ushort segcount = nk_ttUSHORT(data+index_map+6) >> 1;
  10426. nk_ushort searchRange = nk_ttUSHORT(data+index_map+8) >> 1;
  10427. nk_ushort entrySelector = nk_ttUSHORT(data+index_map+10);
  10428. nk_ushort rangeShift = nk_ttUSHORT(data+index_map+12) >> 1;
  10429. /* do a binary search of the segments */
  10430. nk_uint endCount = index_map + 14;
  10431. nk_uint search = endCount;
  10432. if (unicode_codepoint > 0xffff)
  10433. return 0;
  10434. /* they lie from endCount .. endCount + segCount */
  10435. /* but searchRange is the nearest power of two, so... */
  10436. if (unicode_codepoint >= nk_ttUSHORT(data + search + rangeShift*2))
  10437. search += (nk_uint)(rangeShift*2);
  10438. /* now decrement to bias correctly to find smallest */
  10439. search -= 2;
  10440. while (entrySelector) {
  10441. nk_ushort end;
  10442. searchRange >>= 1;
  10443. end = nk_ttUSHORT(data + search + searchRange*2);
  10444. if (unicode_codepoint > end)
  10445. search += (nk_uint)(searchRange*2);
  10446. --entrySelector;
  10447. }
  10448. search += 2;
  10449. {
  10450. nk_ushort offset, start;
  10451. nk_ushort item = (nk_ushort) ((search - endCount) >> 1);
  10452. NK_ASSERT(unicode_codepoint <= nk_ttUSHORT(data + endCount + 2*item));
  10453. start = nk_ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);
  10454. if (unicode_codepoint < start)
  10455. return 0;
  10456. offset = nk_ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);
  10457. if (offset == 0)
  10458. return (nk_ushort) (unicode_codepoint + nk_ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));
  10459. return nk_ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
  10460. }
  10461. } else if (format == 12 || format == 13) {
  10462. nk_uint ngroups = nk_ttULONG(data+index_map+12);
  10463. nk_int low,high;
  10464. low = 0; high = (nk_int)ngroups;
  10465. /* Binary search the right group. */
  10466. while (low < high) {
  10467. nk_int mid = low + ((high-low) >> 1); /* rounds down, so low <= mid < high */
  10468. nk_uint start_char = nk_ttULONG(data+index_map+16+mid*12);
  10469. nk_uint end_char = nk_ttULONG(data+index_map+16+mid*12+4);
  10470. if ((nk_uint) unicode_codepoint < start_char)
  10471. high = mid;
  10472. else if ((nk_uint) unicode_codepoint > end_char)
  10473. low = mid+1;
  10474. else {
  10475. nk_uint start_glyph = nk_ttULONG(data+index_map+16+mid*12+8);
  10476. if (format == 12)
  10477. return (int)start_glyph + (int)unicode_codepoint - (int)start_char;
  10478. else /* format == 13 */
  10479. return (int)start_glyph;
  10480. }
  10481. }
  10482. return 0; /* not found */
  10483. }
  10484. /* @TODO */
  10485. NK_ASSERT(0);
  10486. return 0;
  10487. }
  10488. NK_INTERN void
  10489. nk_tt_setvertex(struct nk_tt_vertex *v, nk_byte type, nk_int x, nk_int y, nk_int cx, nk_int cy)
  10490. {
  10491. v->type = type;
  10492. v->x = (nk_short) x;
  10493. v->y = (nk_short) y;
  10494. v->cx = (nk_short) cx;
  10495. v->cy = (nk_short) cy;
  10496. }
  10497. NK_INTERN int
  10498. nk_tt__GetGlyfOffset(const struct nk_tt_fontinfo *info, int glyph_index)
  10499. {
  10500. int g1,g2;
  10501. if (glyph_index >= info->numGlyphs) return -1; /* glyph index out of range */
  10502. if (info->indexToLocFormat >= 2) return -1; /* unknown index->glyph map format */
  10503. if (info->indexToLocFormat == 0) {
  10504. g1 = info->glyf + nk_ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;
  10505. g2 = info->glyf + nk_ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;
  10506. } else {
  10507. g1 = info->glyf + (int)nk_ttULONG (info->data + info->loca + glyph_index * 4);
  10508. g2 = info->glyf + (int)nk_ttULONG (info->data + info->loca + glyph_index * 4 + 4);
  10509. }
  10510. return g1==g2 ? -1 : g1; /* if length is 0, return -1 */
  10511. }
  10512. NK_INTERN int
  10513. nk_tt_GetGlyphBox(const struct nk_tt_fontinfo *info, int glyph_index,
  10514. int *x0, int *y0, int *x1, int *y1)
  10515. {
  10516. int g = nk_tt__GetGlyfOffset(info, glyph_index);
  10517. if (g < 0) return 0;
  10518. if (x0) *x0 = nk_ttSHORT(info->data + g + 2);
  10519. if (y0) *y0 = nk_ttSHORT(info->data + g + 4);
  10520. if (x1) *x1 = nk_ttSHORT(info->data + g + 6);
  10521. if (y1) *y1 = nk_ttSHORT(info->data + g + 8);
  10522. return 1;
  10523. }
  10524. NK_INTERN int
  10525. nk_tt__close_shape(struct nk_tt_vertex *vertices, int num_vertices, int was_off,
  10526. int start_off, nk_int sx, nk_int sy, nk_int scx, nk_int scy, nk_int cx, nk_int cy)
  10527. {
  10528. if (start_off) {
  10529. if (was_off)
  10530. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy);
  10531. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, sx,sy,scx,scy);
  10532. } else {
  10533. if (was_off)
  10534. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve,sx,sy,cx,cy);
  10535. else
  10536. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vline,sx,sy,0,0);
  10537. }
  10538. return num_vertices;
  10539. }
  10540. NK_INTERN int
  10541. nk_tt_GetGlyphShape(const struct nk_tt_fontinfo *info, struct nk_allocator *alloc,
  10542. int glyph_index, struct nk_tt_vertex **pvertices)
  10543. {
  10544. nk_short numberOfContours;
  10545. const nk_byte *endPtsOfContours;
  10546. const nk_byte *data = info->data;
  10547. struct nk_tt_vertex *vertices=0;
  10548. int num_vertices=0;
  10549. int g = nk_tt__GetGlyfOffset(info, glyph_index);
  10550. *pvertices = 0;
  10551. if (g < 0) return 0;
  10552. numberOfContours = nk_ttSHORT(data + g);
  10553. if (numberOfContours > 0) {
  10554. nk_byte flags=0,flagcount;
  10555. nk_int ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0;
  10556. nk_int x,y,cx,cy,sx,sy, scx,scy;
  10557. const nk_byte *points;
  10558. endPtsOfContours = (data + g + 10);
  10559. ins = nk_ttUSHORT(data + g + 10 + numberOfContours * 2);
  10560. points = data + g + 10 + numberOfContours * 2 + 2 + ins;
  10561. n = 1+nk_ttUSHORT(endPtsOfContours + numberOfContours*2-2);
  10562. m = n + 2*numberOfContours; /* a loose bound on how many vertices we might need */
  10563. vertices = (struct nk_tt_vertex *)alloc->alloc(alloc->userdata, 0, (nk_size)m * sizeof(vertices[0]));
  10564. if (vertices == 0)
  10565. return 0;
  10566. next_move = 0;
  10567. flagcount=0;
  10568. /* in first pass, we load uninterpreted data into the allocated array */
  10569. /* above, shifted to the end of the array so we won't overwrite it when */
  10570. /* we create our final data starting from the front */
  10571. off = m - n; /* starting offset for uninterpreted data, regardless of how m ends up being calculated */
  10572. /* first load flags */
  10573. for (i=0; i < n; ++i) {
  10574. if (flagcount == 0) {
  10575. flags = *points++;
  10576. if (flags & 8)
  10577. flagcount = *points++;
  10578. } else --flagcount;
  10579. vertices[off+i].type = flags;
  10580. }
  10581. /* now load x coordinates */
  10582. x=0;
  10583. for (i=0; i < n; ++i) {
  10584. flags = vertices[off+i].type;
  10585. if (flags & 2) {
  10586. nk_short dx = *points++;
  10587. x += (flags & 16) ? dx : -dx; /* ??? */
  10588. } else {
  10589. if (!(flags & 16)) {
  10590. x = x + (nk_short) (points[0]*256 + points[1]);
  10591. points += 2;
  10592. }
  10593. }
  10594. vertices[off+i].x = (nk_short) x;
  10595. }
  10596. /* now load y coordinates */
  10597. y=0;
  10598. for (i=0; i < n; ++i) {
  10599. flags = vertices[off+i].type;
  10600. if (flags & 4) {
  10601. nk_short dy = *points++;
  10602. y += (flags & 32) ? dy : -dy; /* ??? */
  10603. } else {
  10604. if (!(flags & 32)) {
  10605. y = y + (nk_short) (points[0]*256 + points[1]);
  10606. points += 2;
  10607. }
  10608. }
  10609. vertices[off+i].y = (nk_short) y;
  10610. }
  10611. /* now convert them to our format */
  10612. num_vertices=0;
  10613. sx = sy = cx = cy = scx = scy = 0;
  10614. for (i=0; i < n; ++i)
  10615. {
  10616. flags = vertices[off+i].type;
  10617. x = (nk_short) vertices[off+i].x;
  10618. y = (nk_short) vertices[off+i].y;
  10619. if (next_move == i) {
  10620. if (i != 0)
  10621. num_vertices = nk_tt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
  10622. /* now start the new one */
  10623. start_off = !(flags & 1);
  10624. if (start_off) {
  10625. /* if we start off with an off-curve point, then when we need to find a point on the curve */
  10626. /* where we can start, and we need to save some state for when we wraparound. */
  10627. scx = x;
  10628. scy = y;
  10629. if (!(vertices[off+i+1].type & 1)) {
  10630. /* next point is also a curve point, so interpolate an on-point curve */
  10631. sx = (x + (nk_int) vertices[off+i+1].x) >> 1;
  10632. sy = (y + (nk_int) vertices[off+i+1].y) >> 1;
  10633. } else {
  10634. /* otherwise just use the next point as our start point */
  10635. sx = (nk_int) vertices[off+i+1].x;
  10636. sy = (nk_int) vertices[off+i+1].y;
  10637. ++i; /* we're using point i+1 as the starting point, so skip it */
  10638. }
  10639. } else {
  10640. sx = x;
  10641. sy = y;
  10642. }
  10643. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vmove,sx,sy,0,0);
  10644. was_off = 0;
  10645. next_move = 1 + nk_ttUSHORT(endPtsOfContours+j*2);
  10646. ++j;
  10647. } else {
  10648. if (!(flags & 1))
  10649. { /* if it's a curve */
  10650. if (was_off) /* two off-curve control points in a row means interpolate an on-curve midpoint */
  10651. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy);
  10652. cx = x;
  10653. cy = y;
  10654. was_off = 1;
  10655. } else {
  10656. if (was_off)
  10657. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, x,y, cx, cy);
  10658. else nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vline, x,y,0,0);
  10659. was_off = 0;
  10660. }
  10661. }
  10662. }
  10663. num_vertices = nk_tt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
  10664. } else if (numberOfContours == -1) {
  10665. /* Compound shapes. */
  10666. int more = 1;
  10667. const nk_byte *comp = data + g + 10;
  10668. num_vertices = 0;
  10669. vertices = 0;
  10670. while (more)
  10671. {
  10672. nk_ushort flags, gidx;
  10673. int comp_num_verts = 0, i;
  10674. struct nk_tt_vertex *comp_verts = 0, *tmp = 0;
  10675. float mtx[6] = {1,0,0,1,0,0}, m, n;
  10676. flags = (nk_ushort)nk_ttSHORT(comp); comp+=2;
  10677. gidx = (nk_ushort)nk_ttSHORT(comp); comp+=2;
  10678. if (flags & 2) { /* XY values */
  10679. if (flags & 1) { /* shorts */
  10680. mtx[4] = nk_ttSHORT(comp); comp+=2;
  10681. mtx[5] = nk_ttSHORT(comp); comp+=2;
  10682. } else {
  10683. mtx[4] = nk_ttCHAR(comp); comp+=1;
  10684. mtx[5] = nk_ttCHAR(comp); comp+=1;
  10685. }
  10686. } else {
  10687. /* @TODO handle matching point */
  10688. NK_ASSERT(0);
  10689. }
  10690. if (flags & (1<<3)) { /* WE_HAVE_A_SCALE */
  10691. mtx[0] = mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10692. mtx[1] = mtx[2] = 0;
  10693. } else if (flags & (1<<6)) { /* WE_HAVE_AN_X_AND_YSCALE */
  10694. mtx[0] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10695. mtx[1] = mtx[2] = 0;
  10696. mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10697. } else if (flags & (1<<7)) { /* WE_HAVE_A_TWO_BY_TWO */
  10698. mtx[0] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10699. mtx[1] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10700. mtx[2] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10701. mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10702. }
  10703. /* Find transformation scales. */
  10704. m = (float) NK_SQRT(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
  10705. n = (float) NK_SQRT(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
  10706. /* Get indexed glyph. */
  10707. comp_num_verts = nk_tt_GetGlyphShape(info, alloc, gidx, &comp_verts);
  10708. if (comp_num_verts > 0)
  10709. {
  10710. /* Transform vertices. */
  10711. for (i = 0; i < comp_num_verts; ++i) {
  10712. struct nk_tt_vertex* v = &comp_verts[i];
  10713. short x,y;
  10714. x=v->x; y=v->y;
  10715. v->x = (short)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
  10716. v->y = (short)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
  10717. x=v->cx; y=v->cy;
  10718. v->cx = (short)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
  10719. v->cy = (short)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
  10720. }
  10721. /* Append vertices. */
  10722. tmp = (struct nk_tt_vertex*)alloc->alloc(alloc->userdata, 0,
  10723. (nk_size)(num_vertices+comp_num_verts)*sizeof(struct nk_tt_vertex));
  10724. if (!tmp) {
  10725. if (vertices) alloc->free(alloc->userdata, vertices);
  10726. if (comp_verts) alloc->free(alloc->userdata, comp_verts);
  10727. return 0;
  10728. }
  10729. if (num_vertices > 0) NK_MEMCPY(tmp, vertices, (nk_size)num_vertices*sizeof(struct nk_tt_vertex));
  10730. NK_MEMCPY(tmp+num_vertices, comp_verts, (nk_size)comp_num_verts*sizeof(struct nk_tt_vertex));
  10731. if (vertices) alloc->free(alloc->userdata,vertices);
  10732. vertices = tmp;
  10733. alloc->free(alloc->userdata,comp_verts);
  10734. num_vertices += comp_num_verts;
  10735. }
  10736. /* More components ? */
  10737. more = flags & (1<<5);
  10738. }
  10739. } else if (numberOfContours < 0) {
  10740. /* @TODO other compound variations? */
  10741. NK_ASSERT(0);
  10742. } else {
  10743. /* numberOfCounters == 0, do nothing */
  10744. }
  10745. *pvertices = vertices;
  10746. return num_vertices;
  10747. }
  10748. NK_INTERN void
  10749. nk_tt_GetGlyphHMetrics(const struct nk_tt_fontinfo *info, int glyph_index,
  10750. int *advanceWidth, int *leftSideBearing)
  10751. {
  10752. nk_ushort numOfLongHorMetrics = nk_ttUSHORT(info->data+info->hhea + 34);
  10753. if (glyph_index < numOfLongHorMetrics) {
  10754. if (advanceWidth)
  10755. *advanceWidth = nk_ttSHORT(info->data + info->hmtx + 4*glyph_index);
  10756. if (leftSideBearing)
  10757. *leftSideBearing = nk_ttSHORT(info->data + info->hmtx + 4*glyph_index + 2);
  10758. } else {
  10759. if (advanceWidth)
  10760. *advanceWidth = nk_ttSHORT(info->data + info->hmtx + 4*(numOfLongHorMetrics-1));
  10761. if (leftSideBearing)
  10762. *leftSideBearing = nk_ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));
  10763. }
  10764. }
  10765. NK_INTERN void
  10766. nk_tt_GetFontVMetrics(const struct nk_tt_fontinfo *info,
  10767. int *ascent, int *descent, int *lineGap)
  10768. {
  10769. if (ascent ) *ascent = nk_ttSHORT(info->data+info->hhea + 4);
  10770. if (descent) *descent = nk_ttSHORT(info->data+info->hhea + 6);
  10771. if (lineGap) *lineGap = nk_ttSHORT(info->data+info->hhea + 8);
  10772. }
  10773. NK_INTERN float
  10774. nk_tt_ScaleForPixelHeight(const struct nk_tt_fontinfo *info, float height)
  10775. {
  10776. int fheight = nk_ttSHORT(info->data + info->hhea + 4) - nk_ttSHORT(info->data + info->hhea + 6);
  10777. return (float) height / (float)fheight;
  10778. }
  10779. NK_INTERN float
  10780. nk_tt_ScaleForMappingEmToPixels(const struct nk_tt_fontinfo *info, float pixels)
  10781. {
  10782. int unitsPerEm = nk_ttUSHORT(info->data + info->head + 18);
  10783. return pixels / (float)unitsPerEm;
  10784. }
  10785. /*-------------------------------------------------------------
  10786. * antialiasing software rasterizer
  10787. * --------------------------------------------------------------*/
  10788. NK_INTERN void
  10789. nk_tt_GetGlyphBitmapBoxSubpixel(const struct nk_tt_fontinfo *font,
  10790. int glyph, float scale_x, float scale_y,float shift_x, float shift_y,
  10791. int *ix0, int *iy0, int *ix1, int *iy1)
  10792. {
  10793. int x0,y0,x1,y1;
  10794. if (!nk_tt_GetGlyphBox(font, glyph, &x0,&y0,&x1,&y1)) {
  10795. /* e.g. space character */
  10796. if (ix0) *ix0 = 0;
  10797. if (iy0) *iy0 = 0;
  10798. if (ix1) *ix1 = 0;
  10799. if (iy1) *iy1 = 0;
  10800. } else {
  10801. /* move to integral bboxes (treating pixels as little squares, what pixels get touched)? */
  10802. if (ix0) *ix0 = nk_ifloorf((float)x0 * scale_x + shift_x);
  10803. if (iy0) *iy0 = nk_ifloorf((float)-y1 * scale_y + shift_y);
  10804. if (ix1) *ix1 = nk_iceilf ((float)x1 * scale_x + shift_x);
  10805. if (iy1) *iy1 = nk_iceilf ((float)-y0 * scale_y + shift_y);
  10806. }
  10807. }
  10808. NK_INTERN void
  10809. nk_tt_GetGlyphBitmapBox(const struct nk_tt_fontinfo *font, int glyph,
  10810. float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)
  10811. {
  10812. nk_tt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1);
  10813. }
  10814. /*-------------------------------------------------------------
  10815. * Rasterizer
  10816. * --------------------------------------------------------------*/
  10817. NK_INTERN void*
  10818. nk_tt__hheap_alloc(struct nk_tt__hheap *hh, nk_size size)
  10819. {
  10820. if (hh->first_free) {
  10821. void *p = hh->first_free;
  10822. hh->first_free = * (void **) p;
  10823. return p;
  10824. } else {
  10825. if (hh->num_remaining_in_head_chunk == 0) {
  10826. int count = (size < 32 ? 2000 : size < 128 ? 800 : 100);
  10827. struct nk_tt__hheap_chunk *c = (struct nk_tt__hheap_chunk *)
  10828. hh->alloc.alloc(hh->alloc.userdata, 0,
  10829. sizeof(struct nk_tt__hheap_chunk) + size * (nk_size)count);
  10830. if (c == 0) return 0;
  10831. c->next = hh->head;
  10832. hh->head = c;
  10833. hh->num_remaining_in_head_chunk = count;
  10834. }
  10835. --hh->num_remaining_in_head_chunk;
  10836. return (char *) (hh->head) + size * (nk_size)hh->num_remaining_in_head_chunk;
  10837. }
  10838. }
  10839. NK_INTERN void
  10840. nk_tt__hheap_free(struct nk_tt__hheap *hh, void *p)
  10841. {
  10842. *(void **) p = hh->first_free;
  10843. hh->first_free = p;
  10844. }
  10845. NK_INTERN void
  10846. nk_tt__hheap_cleanup(struct nk_tt__hheap *hh)
  10847. {
  10848. struct nk_tt__hheap_chunk *c = hh->head;
  10849. while (c) {
  10850. struct nk_tt__hheap_chunk *n = c->next;
  10851. hh->alloc.free(hh->alloc.userdata, c);
  10852. c = n;
  10853. }
  10854. }
  10855. NK_INTERN struct nk_tt__active_edge*
  10856. nk_tt__new_active(struct nk_tt__hheap *hh, struct nk_tt__edge *e,
  10857. int off_x, float start_point)
  10858. {
  10859. struct nk_tt__active_edge *z = (struct nk_tt__active_edge *)
  10860. nk_tt__hheap_alloc(hh, sizeof(*z));
  10861. float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
  10862. /*STBTT_assert(e->y0 <= start_point); */
  10863. if (!z) return z;
  10864. z->fdx = dxdy;
  10865. z->fdy = (dxdy != 0) ? (1/dxdy): 0;
  10866. z->fx = e->x0 + dxdy * (start_point - e->y0);
  10867. z->fx -= (float)off_x;
  10868. z->direction = e->invert ? 1.0f : -1.0f;
  10869. z->sy = e->y0;
  10870. z->ey = e->y1;
  10871. z->next = 0;
  10872. return z;
  10873. }
  10874. NK_INTERN void
  10875. nk_tt__handle_clipped_edge(float *scanline, int x, struct nk_tt__active_edge *e,
  10876. float x0, float y0, float x1, float y1)
  10877. {
  10878. if (y0 == y1) return;
  10879. NK_ASSERT(y0 < y1);
  10880. NK_ASSERT(e->sy <= e->ey);
  10881. if (y0 > e->ey) return;
  10882. if (y1 < e->sy) return;
  10883. if (y0 < e->sy) {
  10884. x0 += (x1-x0) * (e->sy - y0) / (y1-y0);
  10885. y0 = e->sy;
  10886. }
  10887. if (y1 > e->ey) {
  10888. x1 += (x1-x0) * (e->ey - y1) / (y1-y0);
  10889. y1 = e->ey;
  10890. }
  10891. if (x0 == x) NK_ASSERT(x1 <= x+1);
  10892. else if (x0 == x+1) NK_ASSERT(x1 >= x);
  10893. else if (x0 <= x) NK_ASSERT(x1 <= x);
  10894. else if (x0 >= x+1) NK_ASSERT(x1 >= x+1);
  10895. else NK_ASSERT(x1 >= x && x1 <= x+1);
  10896. if (x0 <= x && x1 <= x)
  10897. scanline[x] += e->direction * (y1-y0);
  10898. else if (x0 >= x+1 && x1 >= x+1);
  10899. else {
  10900. NK_ASSERT(x0 >= x && x0 <= x+1 && x1 >= x && x1 <= x+1);
  10901. /* coverage = 1 - average x position */
  10902. scanline[x] += (float)e->direction * (float)(y1-y0) * (1.0f-((x0-(float)x)+(x1-(float)x))/2.0f);
  10903. }
  10904. }
  10905. NK_INTERN void
  10906. nk_tt__fill_active_edges_new(float *scanline, float *scanline_fill, int len,
  10907. struct nk_tt__active_edge *e, float y_top)
  10908. {
  10909. float y_bottom = y_top+1;
  10910. while (e)
  10911. {
  10912. /* brute force every pixel */
  10913. /* compute intersection points with top & bottom */
  10914. NK_ASSERT(e->ey >= y_top);
  10915. if (e->fdx == 0) {
  10916. float x0 = e->fx;
  10917. if (x0 < len) {
  10918. if (x0 >= 0) {
  10919. nk_tt__handle_clipped_edge(scanline,(int) x0,e, x0,y_top, x0,y_bottom);
  10920. nk_tt__handle_clipped_edge(scanline_fill-1,(int) x0+1,e, x0,y_top, x0,y_bottom);
  10921. } else {
  10922. nk_tt__handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bottom);
  10923. }
  10924. }
  10925. } else {
  10926. float x0 = e->fx;
  10927. float dx = e->fdx;
  10928. float xb = x0 + dx;
  10929. float x_top, x_bottom;
  10930. float y0,y1;
  10931. float dy = e->fdy;
  10932. NK_ASSERT(e->sy <= y_bottom && e->ey >= y_top);
  10933. /* compute endpoints of line segment clipped to this scanline (if the */
  10934. /* line segment starts on this scanline. x0 is the intersection of the */
  10935. /* line with y_top, but that may be off the line segment. */
  10936. if (e->sy > y_top) {
  10937. x_top = x0 + dx * (e->sy - y_top);
  10938. y0 = e->sy;
  10939. } else {
  10940. x_top = x0;
  10941. y0 = y_top;
  10942. }
  10943. if (e->ey < y_bottom) {
  10944. x_bottom = x0 + dx * (e->ey - y_top);
  10945. y1 = e->ey;
  10946. } else {
  10947. x_bottom = xb;
  10948. y1 = y_bottom;
  10949. }
  10950. if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len)
  10951. {
  10952. /* from here on, we don't have to range check x values */
  10953. if ((int) x_top == (int) x_bottom) {
  10954. float height;
  10955. /* simple case, only spans one pixel */
  10956. int x = (int) x_top;
  10957. height = y1 - y0;
  10958. NK_ASSERT(x >= 0 && x < len);
  10959. scanline[x] += e->direction * (1.0f-(((float)x_top - (float)x) + ((float)x_bottom-(float)x))/2.0f) * (float)height;
  10960. scanline_fill[x] += e->direction * (float)height; /* everything right of this pixel is filled */
  10961. } else {
  10962. int x,x1,x2;
  10963. float y_crossing, step, sign, area;
  10964. /* covers 2+ pixels */
  10965. if (x_top > x_bottom)
  10966. {
  10967. /* flip scanline vertically; signed area is the same */
  10968. float t;
  10969. y0 = y_bottom - (y0 - y_top);
  10970. y1 = y_bottom - (y1 - y_top);
  10971. t = y0; y0 = y1; y1 = t;
  10972. t = x_bottom; x_bottom = x_top; x_top = t;
  10973. dx = -dx;
  10974. dy = -dy;
  10975. t = x0; x0 = xb; xb = t;
  10976. }
  10977. x1 = (int) x_top;
  10978. x2 = (int) x_bottom;
  10979. /* compute intersection with y axis at x1+1 */
  10980. y_crossing = ((float)x1+1 - (float)x0) * (float)dy + (float)y_top;
  10981. sign = e->direction;
  10982. /* area of the rectangle covered from y0..y_crossing */
  10983. area = sign * (y_crossing-y0);
  10984. /* area of the triangle (x_top,y0), (x+1,y0), (x+1,y_crossing) */
  10985. scanline[x1] += area * (1.0f-((float)((float)x_top - (float)x1)+(float)(x1+1-x1))/2.0f);
  10986. step = sign * dy;
  10987. for (x = x1+1; x < x2; ++x) {
  10988. scanline[x] += area + step/2;
  10989. area += step;
  10990. }
  10991. y_crossing += (float)dy * (float)(x2 - (x1+1));
  10992. scanline[x2] += area + sign * (1.0f-((float)(x2-x2)+((float)x_bottom-(float)x2))/2.0f) * (y1-y_crossing);
  10993. scanline_fill[x2] += sign * (y1-y0);
  10994. }
  10995. }
  10996. else
  10997. {
  10998. /* if edge goes outside of box we're drawing, we require */
  10999. /* clipping logic. since this does not match the intended use */
  11000. /* of this library, we use a different, very slow brute */
  11001. /* force implementation */
  11002. int x;
  11003. for (x=0; x < len; ++x)
  11004. {
  11005. /* cases: */
  11006. /* */
  11007. /* there can be up to two intersections with the pixel. any intersection */
  11008. /* with left or right edges can be handled by splitting into two (or three) */
  11009. /* regions. intersections with top & bottom do not necessitate case-wise logic. */
  11010. /* */
  11011. /* the old way of doing this found the intersections with the left & right edges, */
  11012. /* then used some simple logic to produce up to three segments in sorted order */
  11013. /* from top-to-bottom. however, this had a problem: if an x edge was epsilon */
  11014. /* across the x border, then the corresponding y position might not be distinct */
  11015. /* from the other y segment, and it might ignored as an empty segment. to avoid */
  11016. /* that, we need to explicitly produce segments based on x positions. */
  11017. /* rename variables to clear pairs */
  11018. float ya = y_top;
  11019. float x1 = (float) (x);
  11020. float x2 = (float) (x+1);
  11021. float x3 = xb;
  11022. float y3 = y_bottom;
  11023. float yb,y2;
  11024. yb = ((float)x - x0) / dx + y_top;
  11025. y2 = ((float)x+1 - x0) / dx + y_top;
  11026. if (x0 < x1 && x3 > x2) { /* three segments descending down-right */
  11027. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x1,yb);
  11028. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x2,y2);
  11029. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
  11030. } else if (x3 < x1 && x0 > x2) { /* three segments descending down-left */
  11031. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x2,y2);
  11032. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x1,yb);
  11033. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x3,y3);
  11034. } else if (x0 < x1 && x3 > x1) { /* two segments across x, down-right */
  11035. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x1,yb);
  11036. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x3,y3);
  11037. } else if (x3 < x1 && x0 > x1) { /* two segments across x, down-left */
  11038. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x1,yb);
  11039. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x3,y3);
  11040. } else if (x0 < x2 && x3 > x2) { /* two segments across x+1, down-right */
  11041. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x2,y2);
  11042. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
  11043. } else if (x3 < x2 && x0 > x2) { /* two segments across x+1, down-left */
  11044. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x2,y2);
  11045. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
  11046. } else { /* one segment */
  11047. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x3,y3);
  11048. }
  11049. }
  11050. }
  11051. }
  11052. e = e->next;
  11053. }
  11054. }
  11055. NK_INTERN void
  11056. nk_tt__rasterize_sorted_edges(struct nk_tt__bitmap *result, struct nk_tt__edge *e,
  11057. int n, int vsubsample, int off_x, int off_y, struct nk_allocator *alloc)
  11058. {
  11059. /* directly AA rasterize edges w/o supersampling */
  11060. struct nk_tt__hheap hh;
  11061. struct nk_tt__active_edge *active = 0;
  11062. int y,j=0, i;
  11063. float scanline_data[129], *scanline, *scanline2;
  11064. NK_UNUSED(vsubsample);
  11065. nk_zero_struct(hh);
  11066. hh.alloc = *alloc;
  11067. if (result->w > 64)
  11068. scanline = (float *) alloc->alloc(alloc->userdata,0, (nk_size)(result->w*2+1) * sizeof(float));
  11069. else scanline = scanline_data;
  11070. scanline2 = scanline + result->w;
  11071. y = off_y;
  11072. e[n].y0 = (float) (off_y + result->h) + 1;
  11073. while (j < result->h)
  11074. {
  11075. /* find center of pixel for this scanline */
  11076. float scan_y_top = (float)y + 0.0f;
  11077. float scan_y_bottom = (float)y + 1.0f;
  11078. struct nk_tt__active_edge **step = &active;
  11079. NK_MEMSET(scanline , 0, (nk_size)result->w*sizeof(scanline[0]));
  11080. NK_MEMSET(scanline2, 0, (nk_size)(result->w+1)*sizeof(scanline[0]));
  11081. /* update all active edges; */
  11082. /* remove all active edges that terminate before the top of this scanline */
  11083. while (*step) {
  11084. struct nk_tt__active_edge * z = *step;
  11085. if (z->ey <= scan_y_top) {
  11086. *step = z->next; /* delete from list */
  11087. NK_ASSERT(z->direction);
  11088. z->direction = 0;
  11089. nk_tt__hheap_free(&hh, z);
  11090. } else {
  11091. step = &((*step)->next); /* advance through list */
  11092. }
  11093. }
  11094. /* insert all edges that start before the bottom of this scanline */
  11095. while (e->y0 <= scan_y_bottom) {
  11096. if (e->y0 != e->y1) {
  11097. struct nk_tt__active_edge *z = nk_tt__new_active(&hh, e, off_x, scan_y_top);
  11098. if (z != 0) {
  11099. NK_ASSERT(z->ey >= scan_y_top);
  11100. /* insert at front */
  11101. z->next = active;
  11102. active = z;
  11103. }
  11104. }
  11105. ++e;
  11106. }
  11107. /* now process all active edges */
  11108. if (active)
  11109. nk_tt__fill_active_edges_new(scanline, scanline2+1, result->w, active, scan_y_top);
  11110. {
  11111. float sum = 0;
  11112. for (i=0; i < result->w; ++i) {
  11113. float k;
  11114. int m;
  11115. sum += scanline2[i];
  11116. k = scanline[i] + sum;
  11117. k = (float) NK_ABS(k) * 255.0f + 0.5f;
  11118. m = (int) k;
  11119. if (m > 255) m = 255;
  11120. result->pixels[j*result->stride + i] = (unsigned char) m;
  11121. }
  11122. }
  11123. /* advance all the edges */
  11124. step = &active;
  11125. while (*step) {
  11126. struct nk_tt__active_edge *z = *step;
  11127. z->fx += z->fdx; /* advance to position for current scanline */
  11128. step = &((*step)->next); /* advance through list */
  11129. }
  11130. ++y;
  11131. ++j;
  11132. }
  11133. nk_tt__hheap_cleanup(&hh);
  11134. if (scanline != scanline_data)
  11135. alloc->free(alloc->userdata, scanline);
  11136. }
  11137. NK_INTERN void
  11138. nk_tt__sort_edges_ins_sort(struct nk_tt__edge *p, int n)
  11139. {
  11140. int i,j;
  11141. #define NK_TT__COMPARE(a,b) ((a)->y0 < (b)->y0)
  11142. for (i=1; i < n; ++i) {
  11143. struct nk_tt__edge t = p[i], *a = &t;
  11144. j = i;
  11145. while (j > 0) {
  11146. struct nk_tt__edge *b = &p[j-1];
  11147. int c = NK_TT__COMPARE(a,b);
  11148. if (!c) break;
  11149. p[j] = p[j-1];
  11150. --j;
  11151. }
  11152. if (i != j)
  11153. p[j] = t;
  11154. }
  11155. }
  11156. NK_INTERN void
  11157. nk_tt__sort_edges_quicksort(struct nk_tt__edge *p, int n)
  11158. {
  11159. /* threshold for transitioning to insertion sort */
  11160. while (n > 12) {
  11161. struct nk_tt__edge t;
  11162. int c01,c12,c,m,i,j;
  11163. /* compute median of three */
  11164. m = n >> 1;
  11165. c01 = NK_TT__COMPARE(&p[0],&p[m]);
  11166. c12 = NK_TT__COMPARE(&p[m],&p[n-1]);
  11167. /* if 0 >= mid >= end, or 0 < mid < end, then use mid */
  11168. if (c01 != c12) {
  11169. /* otherwise, we'll need to swap something else to middle */
  11170. int z;
  11171. c = NK_TT__COMPARE(&p[0],&p[n-1]);
  11172. /* 0>mid && mid<n: 0>n => n; 0<n => 0 */
  11173. /* 0<mid && mid>n: 0>n => 0; 0<n => n */
  11174. z = (c == c12) ? 0 : n-1;
  11175. t = p[z];
  11176. p[z] = p[m];
  11177. p[m] = t;
  11178. }
  11179. /* now p[m] is the median-of-three */
  11180. /* swap it to the beginning so it won't move around */
  11181. t = p[0];
  11182. p[0] = p[m];
  11183. p[m] = t;
  11184. /* partition loop */
  11185. i=1;
  11186. j=n-1;
  11187. for(;;) {
  11188. /* handling of equality is crucial here */
  11189. /* for sentinels & efficiency with duplicates */
  11190. for (;;++i) {
  11191. if (!NK_TT__COMPARE(&p[i], &p[0])) break;
  11192. }
  11193. for (;;--j) {
  11194. if (!NK_TT__COMPARE(&p[0], &p[j])) break;
  11195. }
  11196. /* make sure we haven't crossed */
  11197. if (i >= j) break;
  11198. t = p[i];
  11199. p[i] = p[j];
  11200. p[j] = t;
  11201. ++i;
  11202. --j;
  11203. }
  11204. /* recurse on smaller side, iterate on larger */
  11205. if (j < (n-i)) {
  11206. nk_tt__sort_edges_quicksort(p,j);
  11207. p = p+i;
  11208. n = n-i;
  11209. } else {
  11210. nk_tt__sort_edges_quicksort(p+i, n-i);
  11211. n = j;
  11212. }
  11213. }
  11214. }
  11215. NK_INTERN void
  11216. nk_tt__sort_edges(struct nk_tt__edge *p, int n)
  11217. {
  11218. nk_tt__sort_edges_quicksort(p, n);
  11219. nk_tt__sort_edges_ins_sort(p, n);
  11220. }
  11221. NK_INTERN void
  11222. nk_tt__rasterize(struct nk_tt__bitmap *result, struct nk_tt__point *pts,
  11223. int *wcount, int windings, float scale_x, float scale_y,
  11224. float shift_x, float shift_y, int off_x, int off_y, int invert,
  11225. struct nk_allocator *alloc)
  11226. {
  11227. float y_scale_inv = invert ? -scale_y : scale_y;
  11228. struct nk_tt__edge *e;
  11229. int n,i,j,k,m;
  11230. int vsubsample = 1;
  11231. /* vsubsample should divide 255 evenly; otherwise we won't reach full opacity */
  11232. /* now we have to blow out the windings into explicit edge lists */
  11233. n = 0;
  11234. for (i=0; i < windings; ++i)
  11235. n += wcount[i];
  11236. e = (struct nk_tt__edge*)
  11237. alloc->alloc(alloc->userdata, 0,(sizeof(*e) * (nk_size)(n+1)));
  11238. if (e == 0) return;
  11239. n = 0;
  11240. m=0;
  11241. for (i=0; i < windings; ++i)
  11242. {
  11243. struct nk_tt__point *p = pts + m;
  11244. m += wcount[i];
  11245. j = wcount[i]-1;
  11246. for (k=0; k < wcount[i]; j=k++) {
  11247. int a=k,b=j;
  11248. /* skip the edge if horizontal */
  11249. if (p[j].y == p[k].y)
  11250. continue;
  11251. /* add edge from j to k to the list */
  11252. e[n].invert = 0;
  11253. if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) {
  11254. e[n].invert = 1;
  11255. a=j,b=k;
  11256. }
  11257. e[n].x0 = p[a].x * scale_x + shift_x;
  11258. e[n].y0 = (p[a].y * y_scale_inv + shift_y) * (float)vsubsample;
  11259. e[n].x1 = p[b].x * scale_x + shift_x;
  11260. e[n].y1 = (p[b].y * y_scale_inv + shift_y) * (float)vsubsample;
  11261. ++n;
  11262. }
  11263. }
  11264. /* now sort the edges by their highest point (should snap to integer, and then by x) */
  11265. /*STBTT_sort(e, n, sizeof(e[0]), nk_tt__edge_compare); */
  11266. nk_tt__sort_edges(e, n);
  11267. /* now, traverse the scanlines and find the intersections on each scanline, use xor winding rule */
  11268. nk_tt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, alloc);
  11269. alloc->free(alloc->userdata, e);
  11270. }
  11271. NK_INTERN void
  11272. nk_tt__add_point(struct nk_tt__point *points, int n, float x, float y)
  11273. {
  11274. if (!points) return; /* during first pass, it's unallocated */
  11275. points[n].x = x;
  11276. points[n].y = y;
  11277. }
  11278. NK_INTERN int
  11279. nk_tt__tesselate_curve(struct nk_tt__point *points, int *num_points,
  11280. float x0, float y0, float x1, float y1, float x2, float y2,
  11281. float objspace_flatness_squared, int n)
  11282. {
  11283. /* tesselate until threshold p is happy...
  11284. * @TODO warped to compensate for non-linear stretching */
  11285. /* midpoint */
  11286. float mx = (x0 + 2*x1 + x2)/4;
  11287. float my = (y0 + 2*y1 + y2)/4;
  11288. /* versus directly drawn line */
  11289. float dx = (x0+x2)/2 - mx;
  11290. float dy = (y0+y2)/2 - my;
  11291. if (n > 16) /* 65536 segments on one curve better be enough! */
  11292. return 1;
  11293. /* half-pixel error allowed... need to be smaller if AA */
  11294. if (dx*dx+dy*dy > objspace_flatness_squared) {
  11295. nk_tt__tesselate_curve(points, num_points, x0,y0,
  11296. (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1);
  11297. nk_tt__tesselate_curve(points, num_points, mx,my,
  11298. (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1);
  11299. } else {
  11300. nk_tt__add_point(points, *num_points,x2,y2);
  11301. *num_points = *num_points+1;
  11302. }
  11303. return 1;
  11304. }
  11305. NK_INTERN struct nk_tt__point*
  11306. nk_tt_FlattenCurves(struct nk_tt_vertex *vertices, int num_verts,
  11307. float objspace_flatness, int **contour_lengths, int *num_contours,
  11308. struct nk_allocator *alloc)
  11309. {
  11310. /* returns number of contours */
  11311. struct nk_tt__point *points=0;
  11312. int num_points=0;
  11313. float objspace_flatness_squared = objspace_flatness * objspace_flatness;
  11314. int i;
  11315. int n=0;
  11316. int start=0;
  11317. int pass;
  11318. /* count how many "moves" there are to get the contour count */
  11319. for (i=0; i < num_verts; ++i)
  11320. if (vertices[i].type == NK_TT_vmove) ++n;
  11321. *num_contours = n;
  11322. if (n == 0) return 0;
  11323. *contour_lengths = (int *)
  11324. alloc->alloc(alloc->userdata,0, (sizeof(**contour_lengths) * (nk_size)n));
  11325. if (*contour_lengths == 0) {
  11326. *num_contours = 0;
  11327. return 0;
  11328. }
  11329. /* make two passes through the points so we don't need to realloc */
  11330. for (pass=0; pass < 2; ++pass)
  11331. {
  11332. float x=0,y=0;
  11333. if (pass == 1) {
  11334. points = (struct nk_tt__point *)
  11335. alloc->alloc(alloc->userdata,0, (nk_size)num_points * sizeof(points[0]));
  11336. if (points == 0) goto error;
  11337. }
  11338. num_points = 0;
  11339. n= -1;
  11340. for (i=0; i < num_verts; ++i)
  11341. {
  11342. switch (vertices[i].type) {
  11343. case NK_TT_vmove:
  11344. /* start the next contour */
  11345. if (n >= 0)
  11346. (*contour_lengths)[n] = num_points - start;
  11347. ++n;
  11348. start = num_points;
  11349. x = vertices[i].x, y = vertices[i].y;
  11350. nk_tt__add_point(points, num_points++, x,y);
  11351. break;
  11352. case NK_TT_vline:
  11353. x = vertices[i].x, y = vertices[i].y;
  11354. nk_tt__add_point(points, num_points++, x, y);
  11355. break;
  11356. case NK_TT_vcurve:
  11357. nk_tt__tesselate_curve(points, &num_points, x,y,
  11358. vertices[i].cx, vertices[i].cy,
  11359. vertices[i].x, vertices[i].y,
  11360. objspace_flatness_squared, 0);
  11361. x = vertices[i].x, y = vertices[i].y;
  11362. break;
  11363. default: break;
  11364. }
  11365. }
  11366. (*contour_lengths)[n] = num_points - start;
  11367. }
  11368. return points;
  11369. error:
  11370. alloc->free(alloc->userdata, points);
  11371. alloc->free(alloc->userdata, *contour_lengths);
  11372. *contour_lengths = 0;
  11373. *num_contours = 0;
  11374. return 0;
  11375. }
  11376. NK_INTERN void
  11377. nk_tt_Rasterize(struct nk_tt__bitmap *result, float flatness_in_pixels,
  11378. struct nk_tt_vertex *vertices, int num_verts,
  11379. float scale_x, float scale_y, float shift_x, float shift_y,
  11380. int x_off, int y_off, int invert, struct nk_allocator *alloc)
  11381. {
  11382. float scale = scale_x > scale_y ? scale_y : scale_x;
  11383. int winding_count, *winding_lengths;
  11384. struct nk_tt__point *windings = nk_tt_FlattenCurves(vertices, num_verts,
  11385. flatness_in_pixels / scale, &winding_lengths, &winding_count, alloc);
  11386. NK_ASSERT(alloc);
  11387. if (windings) {
  11388. nk_tt__rasterize(result, windings, winding_lengths, winding_count,
  11389. scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, alloc);
  11390. alloc->free(alloc->userdata, winding_lengths);
  11391. alloc->free(alloc->userdata, windings);
  11392. }
  11393. }
  11394. NK_INTERN void
  11395. nk_tt_MakeGlyphBitmapSubpixel(const struct nk_tt_fontinfo *info, unsigned char *output,
  11396. int out_w, int out_h, int out_stride, float scale_x, float scale_y,
  11397. float shift_x, float shift_y, int glyph, struct nk_allocator *alloc)
  11398. {
  11399. int ix0,iy0;
  11400. struct nk_tt_vertex *vertices;
  11401. int num_verts = nk_tt_GetGlyphShape(info, alloc, glyph, &vertices);
  11402. struct nk_tt__bitmap gbm;
  11403. nk_tt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x,
  11404. shift_y, &ix0,&iy0,0,0);
  11405. gbm.pixels = output;
  11406. gbm.w = out_w;
  11407. gbm.h = out_h;
  11408. gbm.stride = out_stride;
  11409. if (gbm.w && gbm.h)
  11410. nk_tt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y,
  11411. shift_x, shift_y, ix0,iy0, 1, alloc);
  11412. alloc->free(alloc->userdata, vertices);
  11413. }
  11414. /*-------------------------------------------------------------
  11415. * Bitmap baking
  11416. * --------------------------------------------------------------*/
  11417. NK_INTERN int
  11418. nk_tt_PackBegin(struct nk_tt_pack_context *spc, unsigned char *pixels,
  11419. int pw, int ph, int stride_in_bytes, int padding, struct nk_allocator *alloc)
  11420. {
  11421. int num_nodes = pw - padding;
  11422. struct nk_rp_context *context = (struct nk_rp_context *)
  11423. alloc->alloc(alloc->userdata,0, sizeof(*context));
  11424. struct nk_rp_node *nodes = (struct nk_rp_node*)
  11425. alloc->alloc(alloc->userdata,0, (sizeof(*nodes ) * (nk_size)num_nodes));
  11426. if (context == 0 || nodes == 0) {
  11427. if (context != 0) alloc->free(alloc->userdata, context);
  11428. if (nodes != 0) alloc->free(alloc->userdata, nodes);
  11429. return 0;
  11430. }
  11431. spc->width = pw;
  11432. spc->height = ph;
  11433. spc->pixels = pixels;
  11434. spc->pack_info = context;
  11435. spc->nodes = nodes;
  11436. spc->padding = padding;
  11437. spc->stride_in_bytes = (stride_in_bytes != 0) ? stride_in_bytes : pw;
  11438. spc->h_oversample = 1;
  11439. spc->v_oversample = 1;
  11440. nk_rp_init_target(context, pw-padding, ph-padding, nodes, num_nodes);
  11441. if (pixels)
  11442. NK_MEMSET(pixels, 0, (nk_size)(pw*ph)); /* background of 0 around pixels */
  11443. return 1;
  11444. }
  11445. NK_INTERN void
  11446. nk_tt_PackEnd(struct nk_tt_pack_context *spc, struct nk_allocator *alloc)
  11447. {
  11448. alloc->free(alloc->userdata, spc->nodes);
  11449. alloc->free(alloc->userdata, spc->pack_info);
  11450. }
  11451. NK_INTERN void
  11452. nk_tt_PackSetOversampling(struct nk_tt_pack_context *spc,
  11453. unsigned int h_oversample, unsigned int v_oversample)
  11454. {
  11455. NK_ASSERT(h_oversample <= NK_TT_MAX_OVERSAMPLE);
  11456. NK_ASSERT(v_oversample <= NK_TT_MAX_OVERSAMPLE);
  11457. if (h_oversample <= NK_TT_MAX_OVERSAMPLE)
  11458. spc->h_oversample = h_oversample;
  11459. if (v_oversample <= NK_TT_MAX_OVERSAMPLE)
  11460. spc->v_oversample = v_oversample;
  11461. }
  11462. NK_INTERN void
  11463. nk_tt__h_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes,
  11464. int kernel_width)
  11465. {
  11466. unsigned char buffer[NK_TT_MAX_OVERSAMPLE];
  11467. int safe_w = w - kernel_width;
  11468. int j;
  11469. for (j=0; j < h; ++j)
  11470. {
  11471. int i;
  11472. unsigned int total;
  11473. NK_MEMSET(buffer, 0, (nk_size)kernel_width);
  11474. total = 0;
  11475. /* make kernel_width a constant in common cases so compiler can optimize out the divide */
  11476. switch (kernel_width) {
  11477. case 2:
  11478. for (i=0; i <= safe_w; ++i) {
  11479. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  11480. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  11481. pixels[i] = (unsigned char) (total / 2);
  11482. }
  11483. break;
  11484. case 3:
  11485. for (i=0; i <= safe_w; ++i) {
  11486. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  11487. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  11488. pixels[i] = (unsigned char) (total / 3);
  11489. }
  11490. break;
  11491. case 4:
  11492. for (i=0; i <= safe_w; ++i) {
  11493. total += (unsigned int)pixels[i] - buffer[i & NK_TT__OVER_MASK];
  11494. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  11495. pixels[i] = (unsigned char) (total / 4);
  11496. }
  11497. break;
  11498. case 5:
  11499. for (i=0; i <= safe_w; ++i) {
  11500. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  11501. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  11502. pixels[i] = (unsigned char) (total / 5);
  11503. }
  11504. break;
  11505. default:
  11506. for (i=0; i <= safe_w; ++i) {
  11507. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  11508. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  11509. pixels[i] = (unsigned char) (total / (unsigned int)kernel_width);
  11510. }
  11511. break;
  11512. }
  11513. for (; i < w; ++i) {
  11514. NK_ASSERT(pixels[i] == 0);
  11515. total -= (unsigned int)(buffer[i & NK_TT__OVER_MASK]);
  11516. pixels[i] = (unsigned char) (total / (unsigned int)kernel_width);
  11517. }
  11518. pixels += stride_in_bytes;
  11519. }
  11520. }
  11521. NK_INTERN void
  11522. nk_tt__v_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes,
  11523. int kernel_width)
  11524. {
  11525. unsigned char buffer[NK_TT_MAX_OVERSAMPLE];
  11526. int safe_h = h - kernel_width;
  11527. int j;
  11528. for (j=0; j < w; ++j)
  11529. {
  11530. int i;
  11531. unsigned int total;
  11532. NK_MEMSET(buffer, 0, (nk_size)kernel_width);
  11533. total = 0;
  11534. /* make kernel_width a constant in common cases so compiler can optimize out the divide */
  11535. switch (kernel_width) {
  11536. case 2:
  11537. for (i=0; i <= safe_h; ++i) {
  11538. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  11539. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  11540. pixels[i*stride_in_bytes] = (unsigned char) (total / 2);
  11541. }
  11542. break;
  11543. case 3:
  11544. for (i=0; i <= safe_h; ++i) {
  11545. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  11546. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  11547. pixels[i*stride_in_bytes] = (unsigned char) (total / 3);
  11548. }
  11549. break;
  11550. case 4:
  11551. for (i=0; i <= safe_h; ++i) {
  11552. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  11553. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  11554. pixels[i*stride_in_bytes] = (unsigned char) (total / 4);
  11555. }
  11556. break;
  11557. case 5:
  11558. for (i=0; i <= safe_h; ++i) {
  11559. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  11560. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  11561. pixels[i*stride_in_bytes] = (unsigned char) (total / 5);
  11562. }
  11563. break;
  11564. default:
  11565. for (i=0; i <= safe_h; ++i) {
  11566. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  11567. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  11568. pixels[i*stride_in_bytes] = (unsigned char) (total / (unsigned int)kernel_width);
  11569. }
  11570. break;
  11571. }
  11572. for (; i < h; ++i) {
  11573. NK_ASSERT(pixels[i*stride_in_bytes] == 0);
  11574. total -= (unsigned int)(buffer[i & NK_TT__OVER_MASK]);
  11575. pixels[i*stride_in_bytes] = (unsigned char) (total / (unsigned int)kernel_width);
  11576. }
  11577. pixels += 1;
  11578. }
  11579. }
  11580. NK_INTERN float
  11581. nk_tt__oversample_shift(int oversample)
  11582. {
  11583. if (!oversample)
  11584. return 0.0f;
  11585. /* The prefilter is a box filter of width "oversample", */
  11586. /* which shifts phase by (oversample - 1)/2 pixels in */
  11587. /* oversampled space. We want to shift in the opposite */
  11588. /* direction to counter this. */
  11589. return (float)-(oversample - 1) / (2.0f * (float)oversample);
  11590. }
  11591. NK_INTERN int
  11592. nk_tt_PackFontRangesGatherRects(struct nk_tt_pack_context *spc,
  11593. struct nk_tt_fontinfo *info, struct nk_tt_pack_range *ranges,
  11594. int num_ranges, struct nk_rp_rect *rects)
  11595. {
  11596. /* rects array must be big enough to accommodate all characters in the given ranges */
  11597. int i,j,k;
  11598. k = 0;
  11599. for (i=0; i < num_ranges; ++i) {
  11600. float fh = ranges[i].font_size;
  11601. float scale = (fh > 0) ? nk_tt_ScaleForPixelHeight(info, fh):
  11602. nk_tt_ScaleForMappingEmToPixels(info, -fh);
  11603. ranges[i].h_oversample = (unsigned char) spc->h_oversample;
  11604. ranges[i].v_oversample = (unsigned char) spc->v_oversample;
  11605. for (j=0; j < ranges[i].num_chars; ++j) {
  11606. int x0,y0,x1,y1;
  11607. int codepoint = ranges[i].first_unicode_codepoint_in_range ?
  11608. ranges[i].first_unicode_codepoint_in_range + j :
  11609. ranges[i].array_of_unicode_codepoints[j];
  11610. int glyph = nk_tt_FindGlyphIndex(info, codepoint);
  11611. nk_tt_GetGlyphBitmapBoxSubpixel(info,glyph, scale * (float)spc->h_oversample,
  11612. scale * (float)spc->v_oversample, 0,0, &x0,&y0,&x1,&y1);
  11613. rects[k].w = (nk_rp_coord) (x1-x0 + spc->padding + (int)spc->h_oversample-1);
  11614. rects[k].h = (nk_rp_coord) (y1-y0 + spc->padding + (int)spc->v_oversample-1);
  11615. ++k;
  11616. }
  11617. }
  11618. return k;
  11619. }
  11620. NK_INTERN int
  11621. nk_tt_PackFontRangesRenderIntoRects(struct nk_tt_pack_context *spc,
  11622. struct nk_tt_fontinfo *info, struct nk_tt_pack_range *ranges,
  11623. int num_ranges, struct nk_rp_rect *rects, struct nk_allocator *alloc)
  11624. {
  11625. int i,j,k, return_value = 1;
  11626. /* save current values */
  11627. int old_h_over = (int)spc->h_oversample;
  11628. int old_v_over = (int)spc->v_oversample;
  11629. /* rects array must be big enough to accommodate all characters in the given ranges */
  11630. k = 0;
  11631. for (i=0; i < num_ranges; ++i)
  11632. {
  11633. float fh = ranges[i].font_size;
  11634. float recip_h,recip_v,sub_x,sub_y;
  11635. float scale = fh > 0 ? nk_tt_ScaleForPixelHeight(info, fh):
  11636. nk_tt_ScaleForMappingEmToPixels(info, -fh);
  11637. spc->h_oversample = ranges[i].h_oversample;
  11638. spc->v_oversample = ranges[i].v_oversample;
  11639. recip_h = 1.0f / (float)spc->h_oversample;
  11640. recip_v = 1.0f / (float)spc->v_oversample;
  11641. sub_x = nk_tt__oversample_shift((int)spc->h_oversample);
  11642. sub_y = nk_tt__oversample_shift((int)spc->v_oversample);
  11643. for (j=0; j < ranges[i].num_chars; ++j)
  11644. {
  11645. struct nk_rp_rect *r = &rects[k];
  11646. if (r->was_packed)
  11647. {
  11648. struct nk_tt_packedchar *bc = &ranges[i].chardata_for_range[j];
  11649. int advance, lsb, x0,y0,x1,y1;
  11650. int codepoint = ranges[i].first_unicode_codepoint_in_range ?
  11651. ranges[i].first_unicode_codepoint_in_range + j :
  11652. ranges[i].array_of_unicode_codepoints[j];
  11653. int glyph = nk_tt_FindGlyphIndex(info, codepoint);
  11654. nk_rp_coord pad = (nk_rp_coord) spc->padding;
  11655. /* pad on left and top */
  11656. r->x = (nk_rp_coord)((int)r->x + (int)pad);
  11657. r->y = (nk_rp_coord)((int)r->y + (int)pad);
  11658. r->w = (nk_rp_coord)((int)r->w - (int)pad);
  11659. r->h = (nk_rp_coord)((int)r->h - (int)pad);
  11660. nk_tt_GetGlyphHMetrics(info, glyph, &advance, &lsb);
  11661. nk_tt_GetGlyphBitmapBox(info, glyph, scale * (float)spc->h_oversample,
  11662. (scale * (float)spc->v_oversample), &x0,&y0,&x1,&y1);
  11663. nk_tt_MakeGlyphBitmapSubpixel(info, spc->pixels + r->x + r->y*spc->stride_in_bytes,
  11664. (int)(r->w - spc->h_oversample+1), (int)(r->h - spc->v_oversample+1),
  11665. spc->stride_in_bytes, scale * (float)spc->h_oversample,
  11666. scale * (float)spc->v_oversample, 0,0, glyph, alloc);
  11667. if (spc->h_oversample > 1)
  11668. nk_tt__h_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
  11669. r->w, r->h, spc->stride_in_bytes, (int)spc->h_oversample);
  11670. if (spc->v_oversample > 1)
  11671. nk_tt__v_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
  11672. r->w, r->h, spc->stride_in_bytes, (int)spc->v_oversample);
  11673. bc->x0 = (nk_ushort) r->x;
  11674. bc->y0 = (nk_ushort) r->y;
  11675. bc->x1 = (nk_ushort) (r->x + r->w);
  11676. bc->y1 = (nk_ushort) (r->y + r->h);
  11677. bc->xadvance = scale * (float)advance;
  11678. bc->xoff = (float) x0 * recip_h + sub_x;
  11679. bc->yoff = (float) y0 * recip_v + sub_y;
  11680. bc->xoff2 = ((float)x0 + r->w) * recip_h + sub_x;
  11681. bc->yoff2 = ((float)y0 + r->h) * recip_v + sub_y;
  11682. } else {
  11683. return_value = 0; /* if any fail, report failure */
  11684. }
  11685. ++k;
  11686. }
  11687. }
  11688. /* restore original values */
  11689. spc->h_oversample = (unsigned int)old_h_over;
  11690. spc->v_oversample = (unsigned int)old_v_over;
  11691. return return_value;
  11692. }
  11693. NK_INTERN void
  11694. nk_tt_GetPackedQuad(struct nk_tt_packedchar *chardata, int pw, int ph,
  11695. int char_index, float *xpos, float *ypos, struct nk_tt_aligned_quad *q,
  11696. int align_to_integer)
  11697. {
  11698. float ipw = 1.0f / (float)pw, iph = 1.0f / (float)ph;
  11699. struct nk_tt_packedchar *b = (struct nk_tt_packedchar*)(chardata + char_index);
  11700. if (align_to_integer) {
  11701. int tx = nk_ifloorf((*xpos + b->xoff) + 0.5f);
  11702. int ty = nk_ifloorf((*ypos + b->yoff) + 0.5f);
  11703. float x = (float)tx;
  11704. float y = (float)ty;
  11705. q->x0 = x;
  11706. q->y0 = y;
  11707. q->x1 = x + b->xoff2 - b->xoff;
  11708. q->y1 = y + b->yoff2 - b->yoff;
  11709. } else {
  11710. q->x0 = *xpos + b->xoff;
  11711. q->y0 = *ypos + b->yoff;
  11712. q->x1 = *xpos + b->xoff2;
  11713. q->y1 = *ypos + b->yoff2;
  11714. }
  11715. q->s0 = b->x0 * ipw;
  11716. q->t0 = b->y0 * iph;
  11717. q->s1 = b->x1 * ipw;
  11718. q->t1 = b->y1 * iph;
  11719. *xpos += b->xadvance;
  11720. }
  11721. /* -------------------------------------------------------------
  11722. *
  11723. * FONT BAKING
  11724. *
  11725. * --------------------------------------------------------------*/
  11726. struct nk_font_bake_data {
  11727. struct nk_tt_fontinfo info;
  11728. struct nk_rp_rect *rects;
  11729. struct nk_tt_pack_range *ranges;
  11730. nk_rune range_count;
  11731. };
  11732. struct nk_font_baker {
  11733. struct nk_allocator alloc;
  11734. struct nk_tt_pack_context spc;
  11735. struct nk_font_bake_data *build;
  11736. struct nk_tt_packedchar *packed_chars;
  11737. struct nk_rp_rect *rects;
  11738. struct nk_tt_pack_range *ranges;
  11739. };
  11740. NK_GLOBAL const nk_size nk_rect_align = NK_ALIGNOF(struct nk_rp_rect);
  11741. NK_GLOBAL const nk_size nk_range_align = NK_ALIGNOF(struct nk_tt_pack_range);
  11742. NK_GLOBAL const nk_size nk_char_align = NK_ALIGNOF(struct nk_tt_packedchar);
  11743. NK_GLOBAL const nk_size nk_build_align = NK_ALIGNOF(struct nk_font_bake_data);
  11744. NK_GLOBAL const nk_size nk_baker_align = NK_ALIGNOF(struct nk_font_baker);
  11745. NK_INTERN int
  11746. nk_range_count(const nk_rune *range)
  11747. {
  11748. const nk_rune *iter = range;
  11749. NK_ASSERT(range);
  11750. if (!range) return 0;
  11751. while (*(iter++) != 0);
  11752. return (iter == range) ? 0 : (int)((iter - range)/2);
  11753. }
  11754. NK_INTERN int
  11755. nk_range_glyph_count(const nk_rune *range, int count)
  11756. {
  11757. int i = 0;
  11758. int total_glyphs = 0;
  11759. for (i = 0; i < count; ++i) {
  11760. int diff;
  11761. nk_rune f = range[(i*2)+0];
  11762. nk_rune t = range[(i*2)+1];
  11763. NK_ASSERT(t >= f);
  11764. diff = (int)((t - f) + 1);
  11765. total_glyphs += diff;
  11766. }
  11767. return total_glyphs;
  11768. }
  11769. NK_API const nk_rune*
  11770. nk_font_default_glyph_ranges(void)
  11771. {
  11772. NK_STORAGE const nk_rune ranges[] = {0x0020, 0x00FF, 0};
  11773. return ranges;
  11774. }
  11775. NK_API const nk_rune*
  11776. nk_font_chinese_glyph_ranges(void)
  11777. {
  11778. NK_STORAGE const nk_rune ranges[] = {
  11779. 0x0020, 0x00FF,
  11780. 0x3000, 0x30FF,
  11781. 0x31F0, 0x31FF,
  11782. 0xFF00, 0xFFEF,
  11783. 0x4e00, 0x9FAF,
  11784. 0
  11785. };
  11786. return ranges;
  11787. }
  11788. NK_API const nk_rune*
  11789. nk_font_cyrillic_glyph_ranges(void)
  11790. {
  11791. NK_STORAGE const nk_rune ranges[] = {
  11792. 0x0020, 0x00FF,
  11793. 0x0400, 0x052F,
  11794. 0x2DE0, 0x2DFF,
  11795. 0xA640, 0xA69F,
  11796. 0
  11797. };
  11798. return ranges;
  11799. }
  11800. NK_API const nk_rune*
  11801. nk_font_korean_glyph_ranges(void)
  11802. {
  11803. NK_STORAGE const nk_rune ranges[] = {
  11804. 0x0020, 0x00FF,
  11805. 0x3131, 0x3163,
  11806. 0xAC00, 0xD79D,
  11807. 0
  11808. };
  11809. return ranges;
  11810. }
  11811. NK_INTERN void
  11812. nk_font_baker_memory(nk_size *temp, int *glyph_count,
  11813. struct nk_font_config *config_list, int count)
  11814. {
  11815. int range_count = 0;
  11816. int total_range_count = 0;
  11817. struct nk_font_config *iter, *i;
  11818. NK_ASSERT(config_list);
  11819. NK_ASSERT(glyph_count);
  11820. if (!config_list) {
  11821. *temp = 0;
  11822. *glyph_count = 0;
  11823. return;
  11824. }
  11825. *glyph_count = 0;
  11826. for (iter = config_list; iter; iter = iter->next) {
  11827. i = iter;
  11828. do {if (!i->range) iter->range = nk_font_default_glyph_ranges();
  11829. range_count = nk_range_count(i->range);
  11830. total_range_count += range_count;
  11831. *glyph_count += nk_range_glyph_count(i->range, range_count);
  11832. } while ((i = i->n) != iter);
  11833. }
  11834. *temp = (nk_size)*glyph_count * sizeof(struct nk_rp_rect);
  11835. *temp += (nk_size)total_range_count * sizeof(struct nk_tt_pack_range);
  11836. *temp += (nk_size)*glyph_count * sizeof(struct nk_tt_packedchar);
  11837. *temp += (nk_size)count * sizeof(struct nk_font_bake_data);
  11838. *temp += sizeof(struct nk_font_baker);
  11839. *temp += nk_rect_align + nk_range_align + nk_char_align;
  11840. *temp += nk_build_align + nk_baker_align;
  11841. }
  11842. NK_INTERN struct nk_font_baker*
  11843. nk_font_baker(void *memory, int glyph_count, int count, struct nk_allocator *alloc)
  11844. {
  11845. struct nk_font_baker *baker;
  11846. if (!memory) return 0;
  11847. /* setup baker inside a memory block */
  11848. baker = (struct nk_font_baker*)NK_ALIGN_PTR(memory, nk_baker_align);
  11849. baker->build = (struct nk_font_bake_data*)NK_ALIGN_PTR((baker + 1), nk_build_align);
  11850. baker->packed_chars = (struct nk_tt_packedchar*)NK_ALIGN_PTR((baker->build + count), nk_char_align);
  11851. baker->rects = (struct nk_rp_rect*)NK_ALIGN_PTR((baker->packed_chars + glyph_count), nk_rect_align);
  11852. baker->ranges = (struct nk_tt_pack_range*)NK_ALIGN_PTR((baker->rects + glyph_count), nk_range_align);
  11853. baker->alloc = *alloc;
  11854. return baker;
  11855. }
  11856. NK_INTERN int
  11857. nk_font_bake_pack(struct nk_font_baker *baker,
  11858. nk_size *image_memory, int *width, int *height, struct nk_recti *custom,
  11859. const struct nk_font_config *config_list, int count,
  11860. struct nk_allocator *alloc)
  11861. {
  11862. NK_STORAGE const nk_size max_height = 1024 * 32;
  11863. const struct nk_font_config *config_iter, *it;
  11864. int total_glyph_count = 0;
  11865. int total_range_count = 0;
  11866. int range_count = 0;
  11867. int i = 0;
  11868. NK_ASSERT(image_memory);
  11869. NK_ASSERT(width);
  11870. NK_ASSERT(height);
  11871. NK_ASSERT(config_list);
  11872. NK_ASSERT(count);
  11873. NK_ASSERT(alloc);
  11874. if (!image_memory || !width || !height || !config_list || !count) return nk_false;
  11875. for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
  11876. it = config_iter;
  11877. do {range_count = nk_range_count(it->range);
  11878. total_range_count += range_count;
  11879. total_glyph_count += nk_range_glyph_count(it->range, range_count);
  11880. } while ((it = it->n) != config_iter);
  11881. }
  11882. /* setup font baker from temporary memory */
  11883. for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
  11884. it = config_iter;
  11885. do {if (!nk_tt_InitFont(&baker->build[i++].info, (const unsigned char*)it->ttf_blob, 0))
  11886. return nk_false;
  11887. } while ((it = it->n) != config_iter);
  11888. }
  11889. *height = 0;
  11890. *width = (total_glyph_count > 1000) ? 1024 : 512;
  11891. nk_tt_PackBegin(&baker->spc, 0, (int)*width, (int)max_height, 0, 1, alloc);
  11892. {
  11893. int input_i = 0;
  11894. int range_n = 0;
  11895. int rect_n = 0;
  11896. int char_n = 0;
  11897. if (custom) {
  11898. /* pack custom user data first so it will be in the upper left corner*/
  11899. struct nk_rp_rect custom_space;
  11900. nk_zero(&custom_space, sizeof(custom_space));
  11901. custom_space.w = (nk_rp_coord)(custom->w);
  11902. custom_space.h = (nk_rp_coord)(custom->h);
  11903. nk_tt_PackSetOversampling(&baker->spc, 1, 1);
  11904. nk_rp_pack_rects((struct nk_rp_context*)baker->spc.pack_info, &custom_space, 1);
  11905. *height = NK_MAX(*height, (int)(custom_space.y + custom_space.h));
  11906. custom->x = (short)custom_space.x;
  11907. custom->y = (short)custom_space.y;
  11908. custom->w = (short)custom_space.w;
  11909. custom->h = (short)custom_space.h;
  11910. }
  11911. /* first font pass: pack all glyphs */
  11912. for (input_i = 0, config_iter = config_list; input_i < count && config_iter;
  11913. config_iter = config_iter->next) {
  11914. it = config_iter;
  11915. do {int n = 0;
  11916. int glyph_count;
  11917. const nk_rune *in_range;
  11918. const struct nk_font_config *cfg = it;
  11919. struct nk_font_bake_data *tmp = &baker->build[input_i++];
  11920. /* count glyphs + ranges in current font */
  11921. glyph_count = 0; range_count = 0;
  11922. for (in_range = cfg->range; in_range[0] && in_range[1]; in_range += 2) {
  11923. glyph_count += (int)(in_range[1] - in_range[0]) + 1;
  11924. range_count++;
  11925. }
  11926. /* setup ranges */
  11927. tmp->ranges = baker->ranges + range_n;
  11928. tmp->range_count = (nk_rune)range_count;
  11929. range_n += range_count;
  11930. for (i = 0; i < range_count; ++i) {
  11931. in_range = &cfg->range[i * 2];
  11932. tmp->ranges[i].font_size = cfg->size;
  11933. tmp->ranges[i].first_unicode_codepoint_in_range = (int)in_range[0];
  11934. tmp->ranges[i].num_chars = (int)(in_range[1]- in_range[0]) + 1;
  11935. tmp->ranges[i].chardata_for_range = baker->packed_chars + char_n;
  11936. char_n += tmp->ranges[i].num_chars;
  11937. }
  11938. /* pack */
  11939. tmp->rects = baker->rects + rect_n;
  11940. rect_n += glyph_count;
  11941. nk_tt_PackSetOversampling(&baker->spc, cfg->oversample_h, cfg->oversample_v);
  11942. n = nk_tt_PackFontRangesGatherRects(&baker->spc, &tmp->info,
  11943. tmp->ranges, (int)tmp->range_count, tmp->rects);
  11944. nk_rp_pack_rects((struct nk_rp_context*)baker->spc.pack_info, tmp->rects, (int)n);
  11945. /* texture height */
  11946. for (i = 0; i < n; ++i) {
  11947. if (tmp->rects[i].was_packed)
  11948. *height = NK_MAX(*height, tmp->rects[i].y + tmp->rects[i].h);
  11949. }
  11950. } while ((it = it->n) != config_iter);
  11951. }
  11952. NK_ASSERT(rect_n == total_glyph_count);
  11953. NK_ASSERT(char_n == total_glyph_count);
  11954. NK_ASSERT(range_n == total_range_count);
  11955. }
  11956. *height = (int)nk_round_up_pow2((nk_uint)*height);
  11957. *image_memory = (nk_size)(*width) * (nk_size)(*height);
  11958. return nk_true;
  11959. }
  11960. NK_INTERN void
  11961. nk_font_bake(struct nk_font_baker *baker, void *image_memory, int width, int height,
  11962. struct nk_font_glyph *glyphs, int glyphs_count,
  11963. const struct nk_font_config *config_list, int font_count)
  11964. {
  11965. int input_i = 0;
  11966. nk_rune glyph_n = 0;
  11967. const struct nk_font_config *config_iter;
  11968. const struct nk_font_config *it;
  11969. NK_ASSERT(image_memory);
  11970. NK_ASSERT(width);
  11971. NK_ASSERT(height);
  11972. NK_ASSERT(config_list);
  11973. NK_ASSERT(baker);
  11974. NK_ASSERT(font_count);
  11975. NK_ASSERT(glyphs_count);
  11976. if (!image_memory || !width || !height || !config_list ||
  11977. !font_count || !glyphs || !glyphs_count)
  11978. return;
  11979. /* second font pass: render glyphs */
  11980. nk_zero(image_memory, (nk_size)((nk_size)width * (nk_size)height));
  11981. baker->spc.pixels = (unsigned char*)image_memory;
  11982. baker->spc.height = (int)height;
  11983. for (input_i = 0, config_iter = config_list; input_i < font_count && config_iter;
  11984. config_iter = config_iter->next) {
  11985. it = config_iter;
  11986. do {const struct nk_font_config *cfg = it;
  11987. struct nk_font_bake_data *tmp = &baker->build[input_i++];
  11988. nk_tt_PackSetOversampling(&baker->spc, cfg->oversample_h, cfg->oversample_v);
  11989. nk_tt_PackFontRangesRenderIntoRects(&baker->spc, &tmp->info, tmp->ranges,
  11990. (int)tmp->range_count, tmp->rects, &baker->alloc);
  11991. } while ((it = it->n) != config_iter);
  11992. } nk_tt_PackEnd(&baker->spc, &baker->alloc);
  11993. /* third pass: setup font and glyphs */
  11994. for (input_i = 0, config_iter = config_list; input_i < font_count && config_iter;
  11995. config_iter = config_iter->next) {
  11996. it = config_iter;
  11997. do {nk_size i = 0;
  11998. int char_idx = 0;
  11999. nk_rune glyph_count = 0;
  12000. const struct nk_font_config *cfg = it;
  12001. struct nk_font_bake_data *tmp = &baker->build[input_i++];
  12002. struct nk_baked_font *dst_font = cfg->font;
  12003. float font_scale = nk_tt_ScaleForPixelHeight(&tmp->info, cfg->size);
  12004. int unscaled_ascent, unscaled_descent, unscaled_line_gap;
  12005. nk_tt_GetFontVMetrics(&tmp->info, &unscaled_ascent, &unscaled_descent,
  12006. &unscaled_line_gap);
  12007. /* fill baked font */
  12008. if (!cfg->merge_mode) {
  12009. dst_font->ranges = cfg->range;
  12010. dst_font->height = cfg->size;
  12011. dst_font->ascent = ((float)unscaled_ascent * font_scale);
  12012. dst_font->descent = ((float)unscaled_descent * font_scale);
  12013. dst_font->glyph_offset = glyph_n;
  12014. }
  12015. /* fill own baked font glyph array */
  12016. for (i = 0; i < tmp->range_count; ++i) {
  12017. struct nk_tt_pack_range *range = &tmp->ranges[i];
  12018. for (char_idx = 0; char_idx < range->num_chars; char_idx++)
  12019. {
  12020. nk_rune codepoint = 0;
  12021. float dummy_x = 0, dummy_y = 0;
  12022. struct nk_tt_aligned_quad q;
  12023. struct nk_font_glyph *glyph;
  12024. /* query glyph bounds from stb_truetype */
  12025. const struct nk_tt_packedchar *pc = &range->chardata_for_range[char_idx];
  12026. if (!pc->x0 && !pc->x1 && !pc->y0 && !pc->y1) continue;
  12027. codepoint = (nk_rune)(range->first_unicode_codepoint_in_range + char_idx);
  12028. nk_tt_GetPackedQuad(range->chardata_for_range, (int)width,
  12029. (int)height, char_idx, &dummy_x, &dummy_y, &q, 0);
  12030. /* fill own glyph type with data */
  12031. glyph = &glyphs[dst_font->glyph_offset + dst_font->glyph_count + (unsigned int)glyph_count];
  12032. glyph->codepoint = codepoint;
  12033. glyph->x0 = q.x0; glyph->y0 = q.y0;
  12034. glyph->x1 = q.x1; glyph->y1 = q.y1;
  12035. glyph->y0 += (dst_font->ascent + 0.5f);
  12036. glyph->y1 += (dst_font->ascent + 0.5f);
  12037. glyph->w = glyph->x1 - glyph->x0 + 0.5f;
  12038. glyph->h = glyph->y1 - glyph->y0;
  12039. if (cfg->coord_type == NK_COORD_PIXEL) {
  12040. glyph->u0 = q.s0 * (float)width;
  12041. glyph->v0 = q.t0 * (float)height;
  12042. glyph->u1 = q.s1 * (float)width;
  12043. glyph->v1 = q.t1 * (float)height;
  12044. } else {
  12045. glyph->u0 = q.s0;
  12046. glyph->v0 = q.t0;
  12047. glyph->u1 = q.s1;
  12048. glyph->v1 = q.t1;
  12049. }
  12050. glyph->xadvance = (pc->xadvance + cfg->spacing.x);
  12051. if (cfg->pixel_snap)
  12052. glyph->xadvance = (float)(int)(glyph->xadvance + 0.5f);
  12053. glyph_count++;
  12054. }
  12055. }
  12056. dst_font->glyph_count += glyph_count;
  12057. glyph_n += glyph_count;
  12058. } while ((it = it->n) != config_iter);
  12059. }
  12060. }
  12061. NK_INTERN void
  12062. nk_font_bake_custom_data(void *img_memory, int img_width, int img_height,
  12063. struct nk_recti img_dst, const char *texture_data_mask, int tex_width,
  12064. int tex_height, char white, char black)
  12065. {
  12066. nk_byte *pixels;
  12067. int y = 0;
  12068. int x = 0;
  12069. int n = 0;
  12070. NK_ASSERT(img_memory);
  12071. NK_ASSERT(img_width);
  12072. NK_ASSERT(img_height);
  12073. NK_ASSERT(texture_data_mask);
  12074. NK_UNUSED(tex_height);
  12075. if (!img_memory || !img_width || !img_height || !texture_data_mask)
  12076. return;
  12077. pixels = (nk_byte*)img_memory;
  12078. for (y = 0, n = 0; y < tex_height; ++y) {
  12079. for (x = 0; x < tex_width; ++x, ++n) {
  12080. const int off0 = ((img_dst.x + x) + (img_dst.y + y) * img_width);
  12081. const int off1 = off0 + 1 + tex_width;
  12082. pixels[off0] = (texture_data_mask[n] == white) ? 0xFF : 0x00;
  12083. pixels[off1] = (texture_data_mask[n] == black) ? 0xFF : 0x00;
  12084. }
  12085. }
  12086. }
  12087. NK_INTERN void
  12088. nk_font_bake_convert(void *out_memory, int img_width, int img_height,
  12089. const void *in_memory)
  12090. {
  12091. int n = 0;
  12092. nk_rune *dst;
  12093. const nk_byte *src;
  12094. NK_ASSERT(out_memory);
  12095. NK_ASSERT(in_memory);
  12096. NK_ASSERT(img_width);
  12097. NK_ASSERT(img_height);
  12098. if (!out_memory || !in_memory || !img_height || !img_width) return;
  12099. dst = (nk_rune*)out_memory;
  12100. src = (const nk_byte*)in_memory;
  12101. for (n = (int)(img_width * img_height); n > 0; n--)
  12102. *dst++ = ((nk_rune)(*src++) << 24) | 0x00FFFFFF;
  12103. }
  12104. /* -------------------------------------------------------------
  12105. *
  12106. * FONT
  12107. *
  12108. * --------------------------------------------------------------*/
  12109. NK_INTERN float
  12110. nk_font_text_width(nk_handle handle, float height, const char *text, int len)
  12111. {
  12112. nk_rune unicode;
  12113. int text_len = 0;
  12114. float text_width = 0;
  12115. int glyph_len = 0;
  12116. float scale = 0;
  12117. struct nk_font *font = (struct nk_font*)handle.ptr;
  12118. NK_ASSERT(font);
  12119. NK_ASSERT(font->glyphs);
  12120. if (!font || !text || !len)
  12121. return 0;
  12122. scale = height/font->info.height;
  12123. glyph_len = text_len = nk_utf_decode(text, &unicode, (int)len);
  12124. if (!glyph_len) return 0;
  12125. while (text_len <= (int)len && glyph_len) {
  12126. const struct nk_font_glyph *g;
  12127. if (unicode == NK_UTF_INVALID) break;
  12128. /* query currently drawn glyph information */
  12129. g = nk_font_find_glyph(font, unicode);
  12130. text_width += g->xadvance * scale;
  12131. /* offset next glyph */
  12132. glyph_len = nk_utf_decode(text + text_len, &unicode, (int)len - text_len);
  12133. text_len += glyph_len;
  12134. }
  12135. return text_width;
  12136. }
  12137. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  12138. NK_INTERN void
  12139. nk_font_query_font_glyph(nk_handle handle, float height,
  12140. struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
  12141. {
  12142. float scale;
  12143. const struct nk_font_glyph *g;
  12144. struct nk_font *font;
  12145. NK_ASSERT(glyph);
  12146. NK_UNUSED(next_codepoint);
  12147. font = (struct nk_font*)handle.ptr;
  12148. NK_ASSERT(font);
  12149. NK_ASSERT(font->glyphs);
  12150. if (!font || !glyph)
  12151. return;
  12152. scale = height/font->info.height;
  12153. g = nk_font_find_glyph(font, codepoint);
  12154. glyph->width = (g->x1 - g->x0) * scale;
  12155. glyph->height = (g->y1 - g->y0) * scale;
  12156. glyph->offset = nk_vec2(g->x0 * scale, g->y0 * scale);
  12157. glyph->xadvance = (g->xadvance * scale);
  12158. glyph->uv[0] = nk_vec2(g->u0, g->v0);
  12159. glyph->uv[1] = nk_vec2(g->u1, g->v1);
  12160. }
  12161. #endif
  12162. NK_API const struct nk_font_glyph*
  12163. nk_font_find_glyph(struct nk_font *font, nk_rune unicode)
  12164. {
  12165. int i = 0;
  12166. int count;
  12167. int total_glyphs = 0;
  12168. const struct nk_font_glyph *glyph = 0;
  12169. const struct nk_font_config *iter = 0;
  12170. NK_ASSERT(font);
  12171. NK_ASSERT(font->glyphs);
  12172. NK_ASSERT(font->info.ranges);
  12173. if (!font || !font->glyphs) return 0;
  12174. glyph = font->fallback;
  12175. iter = font->config;
  12176. do {count = nk_range_count(iter->range);
  12177. for (i = 0; i < count; ++i) {
  12178. nk_rune f = iter->range[(i*2)+0];
  12179. nk_rune t = iter->range[(i*2)+1];
  12180. int diff = (int)((t - f) + 1);
  12181. if (unicode >= f && unicode <= t)
  12182. return &font->glyphs[((nk_rune)total_glyphs + (unicode - f))];
  12183. total_glyphs += diff;
  12184. }
  12185. } while ((iter = iter->n) != font->config);
  12186. return glyph;
  12187. }
  12188. NK_INTERN void
  12189. nk_font_init(struct nk_font *font, float pixel_height,
  12190. nk_rune fallback_codepoint, struct nk_font_glyph *glyphs,
  12191. const struct nk_baked_font *baked_font, nk_handle atlas)
  12192. {
  12193. struct nk_baked_font baked;
  12194. NK_ASSERT(font);
  12195. NK_ASSERT(glyphs);
  12196. NK_ASSERT(baked_font);
  12197. if (!font || !glyphs || !baked_font)
  12198. return;
  12199. baked = *baked_font;
  12200. font->fallback = 0;
  12201. font->info = baked;
  12202. font->scale = (float)pixel_height / (float)font->info.height;
  12203. font->glyphs = &glyphs[baked_font->glyph_offset];
  12204. font->texture = atlas;
  12205. font->fallback_codepoint = fallback_codepoint;
  12206. font->fallback = nk_font_find_glyph(font, fallback_codepoint);
  12207. font->handle.height = font->info.height * font->scale;
  12208. font->handle.width = nk_font_text_width;
  12209. font->handle.userdata.ptr = font;
  12210. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  12211. font->handle.query = nk_font_query_font_glyph;
  12212. font->handle.texture = font->texture;
  12213. #endif
  12214. }
  12215. /* ---------------------------------------------------------------------------
  12216. *
  12217. * DEFAULT FONT
  12218. *
  12219. * ProggyClean.ttf
  12220. * Copyright (c) 2004, 2005 Tristan Grimmer
  12221. * MIT license (see License.txt in http://www.upperbounds.net/download/ProggyClean.ttf.zip)
  12222. * Download and more information at http://upperbounds.net
  12223. *-----------------------------------------------------------------------------*/
  12224. #ifdef __clang__
  12225. #pragma clang diagnostic push
  12226. #pragma clang diagnostic ignored "-Woverlength-strings"
  12227. #elif defined(__GNUC__) || defined(__GNUG__)
  12228. #pragma GCC diagnostic push
  12229. #pragma GCC diagnostic ignored "-Woverlength-strings"
  12230. #endif
  12231. #ifdef NK_INCLUDE_DEFAULT_FONT
  12232. NK_GLOBAL const char nk_proggy_clean_ttf_compressed_data_base85[11980+1] =
  12233. "7])#######hV0qs'/###[),##/l:$#Q6>##5[n42>c-TH`->>#/e>11NNV=Bv(*:.F?uu#(gRU.o0XGH`$vhLG1hxt9?W`#,5LsCp#-i>.r$<$6pD>Lb';9Crc6tgXmKVeU2cD4Eo3R/"
  12234. "2*>]b(MC;$jPfY.;h^`IWM9<Lh2TlS+f-s$o6Q<BWH`YiU.xfLq$N;$0iR/GX:U(jcW2p/W*q?-qmnUCI;jHSAiFWM.R*kU@C=GH?a9wp8f$e.-4^Qg1)Q-GL(lf(r/7GrRgwV%MS=C#"
  12235. "`8ND>Qo#t'X#(v#Y9w0#1D$CIf;W'#pWUPXOuxXuU(H9M(1<q-UE31#^-V'8IRUo7Qf./L>=Ke$$'5F%)]0^#0X@U.a<r:QLtFsLcL6##lOj)#.Y5<-R&KgLwqJfLgN&;Q?gI^#DY2uL"
  12236. "i@^rMl9t=cWq6##weg>$FBjVQTSDgEKnIS7EM9>ZY9w0#L;>>#Mx&4Mvt//L[MkA#W@lK.N'[0#7RL_&#w+F%HtG9M#XL`N&.,GM4Pg;-<nLENhvx>-VsM.M0rJfLH2eTM`*oJMHRC`N"
  12237. "kfimM2J,W-jXS:)r0wK#@Fge$U>`w'N7G#$#fB#$E^$#:9:hk+eOe--6x)F7*E%?76%^GMHePW-Z5l'&GiF#$956:rS?dA#fiK:)Yr+`&#0j@'DbG&#^$PG.Ll+DNa<XCMKEV*N)LN/N"
  12238. "*b=%Q6pia-Xg8I$<MR&,VdJe$<(7G;Ckl'&hF;;$<_=X(b.RS%%)###MPBuuE1V:v&cX&#2m#(&cV]`k9OhLMbn%s$G2,B$BfD3X*sp5#l,$R#]x_X1xKX%b5U*[r5iMfUo9U`N99hG)"
  12239. "tm+/Us9pG)XPu`<0s-)WTt(gCRxIg(%6sfh=ktMKn3j)<6<b5Sk_/0(^]AaN#(p/L>&VZ>1i%h1S9u5o@YaaW$e+b<TWFn/Z:Oh(Cx2$lNEoN^e)#CFY@@I;BOQ*sRwZtZxRcU7uW6CX"
  12240. "ow0i(?$Q[cjOd[P4d)]>ROPOpxTO7Stwi1::iB1q)C_=dV26J;2,]7op$]uQr@_V7$q^%lQwtuHY]=DX,n3L#0PHDO4f9>dC@O>HBuKPpP*E,N+b3L#lpR/MrTEH.IAQk.a>D[.e;mc."
  12241. "x]Ip.PH^'/aqUO/$1WxLoW0[iLA<QT;5HKD+@qQ'NQ(3_PLhE48R.qAPSwQ0/WK?Z,[x?-J;jQTWA0X@KJ(_Y8N-:/M74:/-ZpKrUss?d#dZq]DAbkU*JqkL+nwX@@47`5>w=4h(9.`G"
  12242. "CRUxHPeR`5Mjol(dUWxZa(>STrPkrJiWx`5U7F#.g*jrohGg`cg:lSTvEY/EV_7H4Q9[Z%cnv;JQYZ5q.l7Zeas:HOIZOB?G<Nald$qs]@]L<J7bR*>gv:[7MI2k).'2($5FNP&EQ(,)"
  12243. "U]W]+fh18.vsai00);D3@4ku5P?DP8aJt+;qUM]=+b'8@;mViBKx0DE[-auGl8:PJ&Dj+M6OC]O^((##]`0i)drT;-7X`=-H3[igUnPG-NZlo.#k@h#=Ork$m>a>$-?Tm$UV(?#P6YY#"
  12244. "'/###xe7q.73rI3*pP/$1>s9)W,JrM7SN]'/4C#v$U`0#V.[0>xQsH$fEmPMgY2u7Kh(G%siIfLSoS+MK2eTM$=5,M8p`A.;_R%#u[K#$x4AG8.kK/HSB==-'Ie/QTtG?-.*^N-4B/ZM"
  12245. "_3YlQC7(p7q)&](`6_c)$/*JL(L-^(]$wIM`dPtOdGA,U3:w2M-0<q-]L_?^)1vw'.,MRsqVr.L;aN&#/EgJ)PBc[-f>+WomX2u7lqM2iEumMTcsF?-aT=Z-97UEnXglEn1K-bnEO`gu"
  12246. "Ft(c%=;Am_Qs@jLooI&NX;]0#j4#F14;gl8-GQpgwhrq8'=l_f-b49'UOqkLu7-##oDY2L(te+Mch&gLYtJ,MEtJfLh'x'M=$CS-ZZ%P]8bZ>#S?YY#%Q&q'3^Fw&?D)UDNrocM3A76/"
  12247. "/oL?#h7gl85[qW/NDOk%16ij;+:1a'iNIdb-ou8.P*w,v5#EI$TWS>Pot-R*H'-SEpA:g)f+O$%%`kA#G=8RMmG1&O`>to8bC]T&$,n.LoO>29sp3dt-52U%VM#q7'DHpg+#Z9%H[K<L"
  12248. "%a2E-grWVM3@2=-k22tL]4$##6We'8UJCKE[d_=%wI;'6X-GsLX4j^SgJ$##R*w,vP3wK#iiW&#*h^D&R?jp7+/u&#(AP##XU8c$fSYW-J95_-Dp[g9wcO&#M-h1OcJlc-*vpw0xUX&#"
  12249. "OQFKNX@QI'IoPp7nb,QU//MQ&ZDkKP)X<WSVL(68uVl&#c'[0#(s1X&xm$Y%B7*K:eDA323j998GXbA#pwMs-jgD$9QISB-A_(aN4xoFM^@C58D0+Q+q3n0#3U1InDjF682-SjMXJK)("
  12250. "h$hxua_K]ul92%'BOU&#BRRh-slg8KDlr:%L71Ka:.A;%YULjDPmL<LYs8i#XwJOYaKPKc1h:'9Ke,g)b),78=I39B;xiY$bgGw-&.Zi9InXDuYa%G*f2Bq7mn9^#p1vv%#(Wi-;/Z5h"
  12251. "o;#2:;%d&#x9v68C5g?ntX0X)pT`;%pB3q7mgGN)3%(P8nTd5L7GeA-GL@+%J3u2:(Yf>et`e;)f#Km8&+DC$I46>#Kr]]u-[=99tts1.qb#q72g1WJO81q+eN'03'eM>&1XxY-caEnO"
  12252. "j%2n8)),?ILR5^.Ibn<-X-Mq7[a82Lq:F&#ce+S9wsCK*x`569E8ew'He]h:sI[2LM$[guka3ZRd6:t%IG:;$%YiJ:Nq=?eAw;/:nnDq0(CYcMpG)qLN4$##&J<j$UpK<Q4a1]MupW^-"
  12253. "sj_$%[HK%'F####QRZJ::Y3EGl4'@%FkiAOg#p[##O`gukTfBHagL<LHw%q&OV0##F=6/:chIm0@eCP8X]:kFI%hl8hgO@RcBhS-@Qb$%+m=hPDLg*%K8ln(wcf3/'DW-$.lR?n[nCH-"
  12254. "eXOONTJlh:.RYF%3'p6sq:UIMA945&^HFS87@$EP2iG<-lCO$%c`uKGD3rC$x0BL8aFn--`ke%#HMP'vh1/R&O_J9'um,.<tx[@%wsJk&bUT2`0uMv7gg#qp/ij.L56'hl;.s5CUrxjO"
  12255. "M7-##.l+Au'A&O:-T72L]P`&=;ctp'XScX*rU.>-XTt,%OVU4)S1+R-#dg0/Nn?Ku1^0f$B*P:Rowwm-`0PKjYDDM'3]d39VZHEl4,.j']Pk-M.h^&:0FACm$maq-&sgw0t7/6(^xtk%"
  12256. "LuH88Fj-ekm>GA#_>568x6(OFRl-IZp`&b,_P'$M<Jnq79VsJW/mWS*PUiq76;]/NM_>hLbxfc$mj`,O;&%W2m`Zh:/)Uetw:aJ%]K9h:TcF]u_-Sj9,VK3M.*'&0D[Ca]J9gp8,kAW]"
  12257. "%(?A%R$f<->Zts'^kn=-^@c4%-pY6qI%J%1IGxfLU9CP8cbPlXv);C=b),<2mOvP8up,UVf3839acAWAW-W?#ao/^#%KYo8fRULNd2.>%m]UK:n%r$'sw]J;5pAoO_#2mO3n,'=H5(et"
  12258. "Hg*`+RLgv>=4U8guD$I%D:W>-r5V*%j*W:Kvej.Lp$<M-SGZ':+Q_k+uvOSLiEo(<aD/K<CCc`'Lx>'?;++O'>()jLR-^u68PHm8ZFWe+ej8h:9r6L*0//c&iH&R8pRbA#Kjm%upV1g:"
  12259. "a_#Ur7FuA#(tRh#.Y5K+@?3<-8m0$PEn;J:rh6?I6uG<-`wMU'ircp0LaE_OtlMb&1#6T.#FDKu#1Lw%u%+GM+X'e?YLfjM[VO0MbuFp7;>Q&#WIo)0@F%q7c#4XAXN-U&VB<HFF*qL("
  12260. "$/V,;(kXZejWO`<[5?\?ewY(*9=%wDc;,u<'9t3W-(H1th3+G]ucQ]kLs7df($/*JL]@*t7Bu_G3_7mp7<iaQjO@.kLg;x3B0lqp7Hf,^Ze7-##@/c58Mo(3;knp0%)A7?-W+eI'o8)b<"
  12261. "nKnw'Ho8C=Y>pqB>0ie&jhZ[?iLR@@_AvA-iQC(=ksRZRVp7`.=+NpBC%rh&3]R:8XDmE5^V8O(x<<aG/1N$#FX$0V5Y6x'aErI3I$7x%E`v<-BY,)%-?Psf*l?%C3.mM(=/M0:JxG'?"
  12262. "7WhH%o'a<-80g0NBxoO(GH<dM]n.+%q@jH?f.UsJ2Ggs&4<-e47&Kl+f//9@`b+?.TeN_&B8Ss?v;^Trk;f#YvJkl&w$]>-+k?'(<S:68tq*WoDfZu';mM?8X[ma8W%*`-=;D.(nc7/;"
  12263. ")g:T1=^J$&BRV(-lTmNB6xqB[@0*o.erM*<SWF]u2=st-*(6v>^](H.aREZSi,#1:[IXaZFOm<-ui#qUq2$##Ri;u75OK#(RtaW-K-F`S+cF]uN`-KMQ%rP/Xri.LRcB##=YL3BgM/3M"
  12264. "D?@f&1'BW-)Ju<L25gl8uhVm1hL$##*8###'A3/LkKW+(^rWX?5W_8g)a(m&K8P>#bmmWCMkk&#TR`C,5d>g)F;t,4:@_l8G/5h4vUd%&%950:VXD'QdWoY-F$BtUwmfe$YqL'8(PWX("
  12265. "P?^@Po3$##`MSs?DWBZ/S>+4%>fX,VWv/w'KD`LP5IbH;rTV>n3cEK8U#bX]l-/V+^lj3;vlMb&[5YQ8#pekX9JP3XUC72L,,?+Ni&co7ApnO*5NK,((W-i:$,kp'UDAO(G0Sq7MVjJs"
  12266. "bIu)'Z,*[>br5fX^:FPAWr-m2KgL<LUN098kTF&#lvo58=/vjDo;.;)Ka*hLR#/k=rKbxuV`>Q_nN6'8uTG&#1T5g)uLv:873UpTLgH+#FgpH'_o1780Ph8KmxQJ8#H72L4@768@Tm&Q"
  12267. "h4CB/5OvmA&,Q&QbUoi$a_%3M01H)4x7I^&KQVgtFnV+;[Pc>[m4k//,]1?#`VY[Jr*3&&slRfLiVZJ:]?=K3Sw=[$=uRB?3xk48@aeg<Z'<$#4H)6,>e0jT6'N#(q%.O=?2S]u*(m<-"
  12268. "V8J'(1)G][68hW$5'q[GC&5j`TE?m'esFGNRM)j,ffZ?-qx8;->g4t*:CIP/[Qap7/9'#(1sao7w-.qNUdkJ)tCF&#B^;xGvn2r9FEPFFFcL@.iFNkTve$m%#QvQS8U@)2Z+3K:AKM5i"
  12269. "sZ88+dKQ)W6>J%CL<KE>`.d*(B`-n8D9oK<Up]c$X$(,)M8Zt7/[rdkqTgl-0cuGMv'?>-XV1q['-5k'cAZ69e;D_?$ZPP&s^+7])$*$#@QYi9,5P&#9r+$%CE=68>K8r0=dSC%%(@p7"
  12270. ".m7jilQ02'0-VWAg<a/''3u.=4L$Y)6k/K:_[3=&jvL<L0C/2'v:^;-DIBW,B4E68:kZ;%?8(Q8BH=kO65BW?xSG&#@uU,DS*,?.+(o(#1vCS8#CHF>TlGW'b)Tq7VT9q^*^$$.:&N@@"
  12271. "$&)WHtPm*5_rO0&e%K&#-30j(E4#'Zb.o/(Tpm$>K'f@[PvFl,hfINTNU6u'0pao7%XUp9]5.>%h`8_=VYbxuel.NTSsJfLacFu3B'lQSu/m6-Oqem8T+oE--$0a/k]uj9EwsG>%veR*"
  12272. "hv^BFpQj:K'#SJ,sB-'#](j.Lg92rTw-*n%@/;39rrJF,l#qV%OrtBeC6/,;qB3ebNW[?,Hqj2L.1NP&GjUR=1D8QaS3Up&@*9wP?+lo7b?@%'k4`p0Z$22%K3+iCZj?XJN4Nm&+YF]u"
  12273. "@-W$U%VEQ/,,>>#)D<h#`)h0:<Q6909ua+&VU%n2:cG3FJ-%@Bj-DgLr`Hw&HAKjKjseK</xKT*)B,N9X3]krc12t'pgTV(Lv-tL[xg_%=M_q7a^x?7Ubd>#%8cY#YZ?=,`Wdxu/ae&#"
  12274. "w6)R89tI#6@s'(6Bf7a&?S=^ZI_kS&ai`&=tE72L_D,;^R)7[$s<Eh#c&)q.MXI%#v9ROa5FZO%sF7q7Nwb&#ptUJ:aqJe$Sl68%.D###EC><?-aF&#RNQv>o8lKN%5/$(vdfq7+ebA#"
  12275. "u1p]ovUKW&Y%q]'>$1@-[xfn$7ZTp7mM,G,Ko7a&Gu%G[RMxJs[0MM%wci.LFDK)(<c`Q8N)jEIF*+?P2a8g%)$q]o2aH8C&<SibC/q,(e:v;-b#6[$NtDZ84Je2KNvB#$P5?tQ3nt(0"
  12276. "d=j.LQf./Ll33+(;q3L-w=8dX$#WF&uIJ@-bfI>%:_i2B5CsR8&9Z&#=mPEnm0f`<&c)QL5uJ#%u%lJj+D-r;BoF&#4DoS97h5g)E#o:&S4weDF,9^Hoe`h*L+_a*NrLW-1pG_&2UdB8"
  12277. "6e%B/:=>)N4xeW.*wft-;$'58-ESqr<b?UI(_%@[P46>#U`'6AQ]m&6/`Z>#S?YY#Vc;r7U2&326d=w&H####?TZ`*4?&.MK?LP8Vxg>$[QXc%QJv92.(Db*B)gb*BM9dM*hJMAo*c&#"
  12278. "b0v=Pjer]$gG&JXDf->'StvU7505l9$AFvgYRI^&<^b68?j#q9QX4SM'RO#&sL1IM.rJfLUAj221]d##DW=m83u5;'bYx,*Sl0hL(W;;$doB&O/TQ:(Z^xBdLjL<Lni;''X.`$#8+1GD"
  12279. ":k$YUWsbn8ogh6rxZ2Z9]%nd+>V#*8U_72Lh+2Q8Cj0i:6hp&$C/:p(HK>T8Y[gHQ4`4)'$Ab(Nof%V'8hL&#<NEdtg(n'=S1A(Q1/I&4([%dM`,Iu'1:_hL>SfD07&6D<fp8dHM7/g+"
  12280. "tlPN9J*rKaPct&?'uBCem^jn%9_K)<,C5K3s=5g&GmJb*[SYq7K;TRLGCsM-$$;S%:Y@r7AK0pprpL<Lrh,q7e/%KWK:50I^+m'vi`3?%Zp+<-d+$L-Sv:@.o19n$s0&39;kn;S%BSq*"
  12281. "$3WoJSCLweV[aZ'MQIjO<7;X-X;&+dMLvu#^UsGEC9WEc[X(wI7#2.(F0jV*eZf<-Qv3J-c+J5AlrB#$p(H68LvEA'q3n0#m,[`*8Ft)FcYgEud]CWfm68,(aLA$@EFTgLXoBq/UPlp7"
  12282. ":d[/;r_ix=:TF`S5H-b<LI&HY(K=h#)]Lk$K14lVfm:x$H<3^Ql<M`$OhapBnkup'D#L$Pb_`N*g]2e;X/Dtg,bsj&K#2[-:iYr'_wgH)NUIR8a1n#S?Yej'h8^58UbZd+^FKD*T@;6A"
  12283. "7aQC[K8d-(v6GI$x:T<&'Gp5Uf>@M.*J:;$-rv29'M]8qMv-tLp,'886iaC=Hb*YJoKJ,(j%K=H`K.v9HggqBIiZu'QvBT.#=)0ukruV&.)3=(^1`o*Pj4<-<aN((^7('#Z0wK#5GX@7"
  12284. "u][`*S^43933A4rl][`*O4CgLEl]v$1Q3AeF37dbXk,.)vj#x'd`;qgbQR%FW,2(?LO=s%Sc68%NP'##Aotl8x=BE#j1UD([3$M(]UI2LX3RpKN@;/#f'f/&_mt&F)XdF<9t4)Qa.*kT"
  12285. "LwQ'(TTB9.xH'>#MJ+gLq9-##@HuZPN0]u:h7.T..G:;$/Usj(T7`Q8tT72LnYl<-qx8;-HV7Q-&Xdx%1a,hC=0u+HlsV>nuIQL-5<N?)NBS)QN*_I,?&)2'IM%L3I)X((e/dl2&8'<M"
  12286. ":^#M*Q+[T.Xri.LYS3v%fF`68h;b-X[/En'CR.q7E)p'/kle2HM,u;^%OKC-N+Ll%F9CF<Nf'^#t2L,;27W:0O@6##U6W7:$rJfLWHj$#)woqBefIZ.PK<b*t7ed;p*_m;4ExK#h@&]>"
  12287. "_>@kXQtMacfD.m-VAb8;IReM3$wf0''hra*so568'Ip&vRs849'MRYSp%:t:h5qSgwpEr$B>Q,;s(C#$)`svQuF$##-D,##,g68@2[T;.XSdN9Qe)rpt._K-#5wF)sP'##p#C0c%-Gb%"
  12288. "hd+<-j'Ai*x&&HMkT]C'OSl##5RG[JXaHN;d'uA#x._U;.`PU@(Z3dt4r152@:v,'R.Sj'w#0<-;kPI)FfJ&#AYJ&#//)>-k=m=*XnK$>=)72L]0I%>.G690a:$##<,);?;72#?x9+d;"
  12289. "^V'9;jY@;)br#q^YQpx:X#Te$Z^'=-=bGhLf:D6&bNwZ9-ZD#n^9HhLMr5G;']d&6'wYmTFmL<LD)F^%[tC'8;+9E#C$g%#5Y>q9wI>P(9mI[>kC-ekLC/R&CH+s'B;K-M6$EB%is00:"
  12290. "+A4[7xks.LrNk0&E)wILYF@2L'0Nb$+pv<(2.768/FrY&h$^3i&@+G%JT'<-,v`3;_)I9M^AE]CN?Cl2AZg+%4iTpT3<n-&%H%b<FDj2M<hH=&Eh<2Len$b*aTX=-8QxN)k11IM1c^j%"
  12291. "9s<L<NFSo)B?+<-(GxsF,^-Eh@$4dXhN$+#rxK8'je'D7k`e;)2pYwPA'_p9&@^18ml1^[@g4t*[JOa*[=Qp7(qJ_oOL^('7fB&Hq-:sf,sNj8xq^>$U4O]GKx'm9)b@p7YsvK3w^YR-"
  12292. "CdQ*:Ir<($u&)#(&?L9Rg3H)4fiEp^iI9O8KnTj,]H?D*r7'M;PwZ9K0E^k&-cpI;.p/6_vwoFMV<->#%Xi.LxVnrU(4&8/P+:hLSKj$#U%]49t'I:rgMi'FL@a:0Y-uA[39',(vbma*"
  12293. "hU%<-SRF`Tt:542R_VV$p@[p8DV[A,?1839FWdF<TddF<9Ah-6&9tWoDlh]&1SpGMq>Ti1O*H&#(AL8[_P%.M>v^-))qOT*F5Cq0`Ye%+$B6i:7@0IX<N+T+0MlMBPQ*Vj>SsD<U4JHY"
  12294. "8kD2)2fU/M#$e.)T4,_=8hLim[&);?UkK'-x?'(:siIfL<$pFM`i<?%W(mGDHM%>iWP,##P`%/L<eXi:@Z9C.7o=@(pXdAO/NLQ8lPl+HPOQa8wD8=^GlPa8TKI1CjhsCTSLJM'/Wl>-"
  12295. "S(qw%sf/@%#B6;/U7K]uZbi^Oc^2n<bhPmUkMw>%t<)'mEVE''n`WnJra$^TKvX5B>;_aSEK',(hwa0:i4G?.Bci.(X[?b*($,=-n<.Q%`(X=?+@Am*Js0&=3bh8K]mL<LoNs'6,'85`"
  12296. "0?t/'_U59@]ddF<#LdF<eWdF<OuN/45rY<-L@&#+fm>69=Lb,OcZV/);TTm8VI;?%OtJ<(b4mq7M6:u?KRdF<gR@2L=FNU-<b[(9c/ML3m;Z[$oF3g)GAWqpARc=<ROu7cL5l;-[A]%/"
  12297. "+fsd;l#SafT/f*W]0=O'$(Tb<[)*@e775R-:Yob%g*>l*:xP?Yb.5)%w_I?7uk5JC+FS(m#i'k.'a0i)9<7b'fs'59hq$*5Uhv##pi^8+hIEBF`nvo`;'l0.^S1<-wUK2/Coh58KKhLj"
  12298. "M=SO*rfO`+qC`W-On.=AJ56>>i2@2LH6A:&5q`?9I3@@'04&p2/LVa*T-4<-i3;M9UvZd+N7>b*eIwg:CC)c<>nO&#<IGe;__.thjZl<%w(Wk2xmp4Q@I#I9,DF]u7-P=.-_:YJ]aS@V"
  12299. "?6*C()dOp7:WL,b&3Rg/.cmM9&r^>$(>.Z-I&J(Q0Hd5Q%7Co-b`-c<N(6r@ip+AurK<m86QIth*#v;-OBqi+L7wDE-Ir8K['m+DDSLwK&/.?-V%U_%3:qKNu$_b*B-kp7NaD'QdWQPK"
  12300. "Yq[@>P)hI;*_F]u`Rb[.j8_Q/<&>uu+VsH$sM9TA%?)(vmJ80),P7E>)tjD%2L=-t#fK[%`v=Q8<FfNkgg^oIbah*#8/Qt$F&:K*-(N/'+1vMB,u()-a.VUU*#[e%gAAO(S>WlA2);Sa"
  12301. ">gXm8YB`1d@K#n]76-a$U,mF<fX]idqd)<3,]J7JmW4`6]uks=4-72L(jEk+:bJ0M^q-8Dm_Z?0olP1C9Sa&H[d&c$ooQUj]Exd*3ZM@-WGW2%s',B-_M%>%Ul:#/'xoFM9QX-$.QN'>"
  12302. "[%$Z$uF6pA6Ki2O5:8w*vP1<-1`[G,)-m#>0`P&#eb#.3i)rtB61(o'$?X3B</R90;eZ]%Ncq;-Tl]#F>2Qft^ae_5tKL9MUe9b*sLEQ95C&`=G?@Mj=wh*'3E>=-<)Gt*Iw)'QG:`@I"
  12303. "wOf7&]1i'S01B+Ev/Nac#9S;=;YQpg_6U`*kVY39xK,[/6Aj7:'1Bm-_1EYfa1+o&o4hp7KN_Q(OlIo@S%;jVdn0'1<Vc52=u`3^o-n1'g4v58Hj&6_t7$##?M)c<$bgQ_'SY((-xkA#"
  12304. "Y(,p'H9rIVY-b,'%bCPF7.J<Up^,(dU1VY*5#WkTU>h19w,WQhLI)3S#f$2(eb,jr*b;3Vw]*7NH%$c4Vs,eD9>XW8?N]o+(*pgC%/72LV-u<Hp,3@e^9UB1J+ak9-TN/mhKPg+AJYd$"
  12305. "MlvAF_jCK*.O-^(63adMT->W%iewS8W6m2rtCpo'RS1R84=@paTKt)>=%&1[)*vp'u+x,VrwN;&]kuO9JDbg=pO$J*.jVe;u'm0dr9l,<*wMK*Oe=g8lV_KEBFkO'oU]^=[-792#ok,)"
  12306. "i]lR8qQ2oA8wcRCZ^7w/Njh;?.stX?Q1>S1q4Bn$)K1<-rGdO'$Wr.Lc.CG)$/*JL4tNR/,SVO3,aUw'DJN:)Ss;wGn9A32ijw%FL+Z0Fn.U9;reSq)bmI32U==5ALuG&#Vf1398/pVo"
  12307. "1*c-(aY168o<`JsSbk-,1N;$>0:OUas(3:8Z972LSfF8eb=c-;>SPw7.6hn3m`9^Xkn(r.qS[0;T%&Qc=+STRxX'q1BNk3&*eu2;&8q$&x>Q#Q7^Tf+6<(d%ZVmj2bDi%.3L2n+4W'$P"
  12308. "iDDG)g,r%+?,$@?uou5tSe2aN_AQU*<h`e-GI7)?OK2A.d7_c)?wQ5AS@DL3r#7fSkgl6-++D:'A,uq7SvlB$pcpH'q3n0#_%dY#xCpr-l<F0NR@-##FEV6NTF6##$l84N1w?AO>'IAO"
  12309. "URQ##V^Fv-XFbGM7Fl(N<3DhLGF%q.1rC$#:T__&Pi68%0xi_&[qFJ(77j_&JWoF.V735&T,[R*:xFR*K5>>#`bW-?4Ne_&6Ne_&6Ne_&n`kr-#GJcM6X;uM6X;uM(.a..^2TkL%oR(#"
  12310. ";u.T%fAr%4tJ8&><1=GHZ_+m9/#H1F^R#SC#*N=BA9(D?v[UiFY>>^8p,KKF.W]L29uLkLlu/+4T<XoIB&hx=T1PcDaB&;HH+-AFr?(m9HZV)FKS8JCw;SD=6[^/DZUL`EUDf]GGlG&>"
  12311. "w$)F./^n3+rlo+DB;5sIYGNk+i1t-69Jg--0pao7Sm#K)pdHW&;LuDNH@H>#/X-TI(;P>#,Gc>#0Su>#4`1?#8lC?#<xU?#@.i?#D:%@#HF7@#LRI@#P_[@#Tkn@#Xw*A#]-=A#a9OA#"
  12312. "d<F&#*;G##.GY##2Sl##6`($#:l:$#>xL$#B.`$#F:r$#JF.%#NR@%#R_R%#Vke%#Zww%#_-4&#3^Rh%Sflr-k'MS.o?.5/sWel/wpEM0%3'/1)K^f1-d>G21&v(35>V`39V7A4=onx4"
  12313. "A1OY5EI0;6Ibgr6M$HS7Q<)58C5w,;WoA*#[%T*#`1g*#d=#+#hI5+#lUG+#pbY+#tnl+#x$),#&1;,#*=M,#.I`,#2Ur,#6b.-#;w[H#iQtA#m^0B#qjBB#uvTB##-hB#'9$C#+E6C#"
  12314. "/QHC#3^ZC#7jmC#;v)D#?,<D#C8ND#GDaD#KPsD#O]/E#g1A5#KA*1#gC17#MGd;#8(02#L-d3#rWM4#Hga1#,<w0#T.j<#O#'2#CYN1#qa^:#_4m3#o@/=#eG8=#t8J5#`+78#4uI-#"
  12315. "m3B2#SB[8#Q0@8#i[*9#iOn8#1Nm;#^sN9#qh<9#:=x-#P;K2#$%X9#bC+.#Rg;<#mN=.#MTF.#RZO.#2?)4#Y#(/#[)1/#b;L/#dAU/#0Sv;#lY$0#n`-0#sf60#(F24#wrH0#%/e0#"
  12316. "TmD<#%JSMFove:CTBEXI:<eh2g)B,3h2^G3i;#d3jD>)4kMYD4lVu`4m`:&5niUA5@(A5BA1]PBB:xlBCC=2CDLXMCEUtiCf&0g2'tN?PGT4CPGT4CPGT4CPGT4CPGT4CPGT4CPGT4CP"
  12317. "GT4CPGT4CPGT4CPGT4CPGT4CPGT4CP-qekC`.9kEg^+F$kwViFJTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5o,^<-28ZI'O?;xp"
  12318. "O?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xp;7q-#lLYI:xvD=#";
  12319. #endif /* NK_INCLUDE_DEFAULT_FONT */
  12320. #define NK_CURSOR_DATA_W 90
  12321. #define NK_CURSOR_DATA_H 27
  12322. NK_GLOBAL const char nk_custom_cursor_data[NK_CURSOR_DATA_W * NK_CURSOR_DATA_H + 1] =
  12323. {
  12324. "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX"
  12325. "..- -X.....X- X.X - X.X -X.....X - X.....X"
  12326. "--- -XXX.XXX- X...X - X...X -X....X - X....X"
  12327. "X - X.X - X.....X - X.....X -X...X - X...X"
  12328. "XX - X.X -X.......X- X.......X -X..X.X - X.X..X"
  12329. "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X"
  12330. "X..X - X.X - X.X - X.X -XX X.X - X.X XX"
  12331. "X...X - X.X - X.X - XX X.X XX - X.X - X.X "
  12332. "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X "
  12333. "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X "
  12334. "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X "
  12335. "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X "
  12336. "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X "
  12337. "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X "
  12338. "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X "
  12339. "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X "
  12340. "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX "
  12341. "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------"
  12342. "X.X X..X - -X.......X- X.......X - XX XX - "
  12343. "XX X..X - - X.....X - X.....X - X.X X.X - "
  12344. " X..X - X...X - X...X - X..X X..X - "
  12345. " XX - X.X - X.X - X...XXXXXXXXXXXXX...X - "
  12346. "------------ - X - X -X.....................X- "
  12347. " ----------------------------------- X...XXXXXXXXXXXXX...X - "
  12348. " - X..X X..X - "
  12349. " - X.X X.X - "
  12350. " - XX XX - "
  12351. };
  12352. #ifdef __clang__
  12353. #pragma clang diagnostic pop
  12354. #elif defined(__GNUC__) || defined(__GNUG__)
  12355. #pragma GCC diagnostic pop
  12356. #endif
  12357. NK_GLOBAL unsigned char *nk__barrier;
  12358. NK_GLOBAL unsigned char *nk__barrier2;
  12359. NK_GLOBAL unsigned char *nk__barrier3;
  12360. NK_GLOBAL unsigned char *nk__barrier4;
  12361. NK_GLOBAL unsigned char *nk__dout;
  12362. NK_INTERN unsigned int
  12363. nk_decompress_length(unsigned char *input)
  12364. {
  12365. return (unsigned int)((input[8] << 24) + (input[9] << 16) + (input[10] << 8) + input[11]);
  12366. }
  12367. NK_INTERN void
  12368. nk__match(unsigned char *data, unsigned int length)
  12369. {
  12370. /* INVERSE of memmove... write each byte before copying the next...*/
  12371. NK_ASSERT (nk__dout + length <= nk__barrier);
  12372. if (nk__dout + length > nk__barrier) { nk__dout += length; return; }
  12373. if (data < nk__barrier4) { nk__dout = nk__barrier+1; return; }
  12374. while (length--) *nk__dout++ = *data++;
  12375. }
  12376. NK_INTERN void
  12377. nk__lit(unsigned char *data, unsigned int length)
  12378. {
  12379. NK_ASSERT (nk__dout + length <= nk__barrier);
  12380. if (nk__dout + length > nk__barrier) { nk__dout += length; return; }
  12381. if (data < nk__barrier2) { nk__dout = nk__barrier+1; return; }
  12382. NK_MEMCPY(nk__dout, data, length);
  12383. nk__dout += length;
  12384. }
  12385. NK_INTERN unsigned char*
  12386. nk_decompress_token(unsigned char *i)
  12387. {
  12388. #define nk__in2(x) ((i[x] << 8) + i[(x)+1])
  12389. #define nk__in3(x) ((i[x] << 16) + nk__in2((x)+1))
  12390. #define nk__in4(x) ((i[x] << 24) + nk__in3((x)+1))
  12391. if (*i >= 0x20) { /* use fewer if's for cases that expand small */
  12392. if (*i >= 0x80) nk__match(nk__dout-i[1]-1, (unsigned int)i[0] - 0x80 + 1), i += 2;
  12393. else if (*i >= 0x40) nk__match(nk__dout-(nk__in2(0) - 0x4000 + 1), (unsigned int)i[2]+1), i += 3;
  12394. else /* *i >= 0x20 */ nk__lit(i+1, (unsigned int)i[0] - 0x20 + 1), i += 1 + (i[0] - 0x20 + 1);
  12395. } else { /* more ifs for cases that expand large, since overhead is amortized */
  12396. if (*i >= 0x18) nk__match(nk__dout-(unsigned int)(nk__in3(0) - 0x180000 + 1), (unsigned int)i[3]+1), i += 4;
  12397. else if (*i >= 0x10) nk__match(nk__dout-(unsigned int)(nk__in3(0) - 0x100000 + 1), (unsigned int)nk__in2(3)+1), i += 5;
  12398. else if (*i >= 0x08) nk__lit(i+2, (unsigned int)nk__in2(0) - 0x0800 + 1), i += 2 + (nk__in2(0) - 0x0800 + 1);
  12399. else if (*i == 0x07) nk__lit(i+3, (unsigned int)nk__in2(1) + 1), i += 3 + (nk__in2(1) + 1);
  12400. else if (*i == 0x06) nk__match(nk__dout-(unsigned int)(nk__in3(1)+1), i[4]+1u), i += 5;
  12401. else if (*i == 0x04) nk__match(nk__dout-(unsigned int)(nk__in3(1)+1), (unsigned int)nk__in2(4)+1u), i += 6;
  12402. }
  12403. return i;
  12404. }
  12405. NK_INTERN unsigned int
  12406. nk_adler32(unsigned int adler32, unsigned char *buffer, unsigned int buflen)
  12407. {
  12408. const unsigned long ADLER_MOD = 65521;
  12409. unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16;
  12410. unsigned long blocklen, i;
  12411. blocklen = buflen % 5552;
  12412. while (buflen) {
  12413. for (i=0; i + 7 < blocklen; i += 8) {
  12414. s1 += buffer[0]; s2 += s1;
  12415. s1 += buffer[1]; s2 += s1;
  12416. s1 += buffer[2]; s2 += s1;
  12417. s1 += buffer[3]; s2 += s1;
  12418. s1 += buffer[4]; s2 += s1;
  12419. s1 += buffer[5]; s2 += s1;
  12420. s1 += buffer[6]; s2 += s1;
  12421. s1 += buffer[7]; s2 += s1;
  12422. buffer += 8;
  12423. }
  12424. for (; i < blocklen; ++i) {
  12425. s1 += *buffer++; s2 += s1;
  12426. }
  12427. s1 %= ADLER_MOD; s2 %= ADLER_MOD;
  12428. buflen -= (unsigned int)blocklen;
  12429. blocklen = 5552;
  12430. }
  12431. return (unsigned int)(s2 << 16) + (unsigned int)s1;
  12432. }
  12433. NK_INTERN unsigned int
  12434. nk_decompress(unsigned char *output, unsigned char *i, unsigned int length)
  12435. {
  12436. unsigned int olen;
  12437. if (nk__in4(0) != 0x57bC0000) return 0;
  12438. if (nk__in4(4) != 0) return 0; /* error! stream is > 4GB */
  12439. olen = nk_decompress_length(i);
  12440. nk__barrier2 = i;
  12441. nk__barrier3 = i+length;
  12442. nk__barrier = output + olen;
  12443. nk__barrier4 = output;
  12444. i += 16;
  12445. nk__dout = output;
  12446. for (;;) {
  12447. unsigned char *old_i = i;
  12448. i = nk_decompress_token(i);
  12449. if (i == old_i) {
  12450. if (*i == 0x05 && i[1] == 0xfa) {
  12451. NK_ASSERT(nk__dout == output + olen);
  12452. if (nk__dout != output + olen) return 0;
  12453. if (nk_adler32(1, output, olen) != (unsigned int) nk__in4(2))
  12454. return 0;
  12455. return olen;
  12456. } else {
  12457. NK_ASSERT(0); /* NOTREACHED */
  12458. return 0;
  12459. }
  12460. }
  12461. NK_ASSERT(nk__dout <= output + olen);
  12462. if (nk__dout > output + olen)
  12463. return 0;
  12464. }
  12465. }
  12466. NK_INTERN unsigned int
  12467. nk_decode_85_byte(char c)
  12468. {
  12469. return (unsigned int)((c >= '\\') ? c-36 : c-35);
  12470. }
  12471. NK_INTERN void
  12472. nk_decode_85(unsigned char* dst, const unsigned char* src)
  12473. {
  12474. while (*src)
  12475. {
  12476. unsigned int tmp =
  12477. nk_decode_85_byte((char)src[0]) +
  12478. 85 * (nk_decode_85_byte((char)src[1]) +
  12479. 85 * (nk_decode_85_byte((char)src[2]) +
  12480. 85 * (nk_decode_85_byte((char)src[3]) +
  12481. 85 * nk_decode_85_byte((char)src[4]))));
  12482. /* we can't assume little-endianess. */
  12483. dst[0] = (unsigned char)((tmp >> 0) & 0xFF);
  12484. dst[1] = (unsigned char)((tmp >> 8) & 0xFF);
  12485. dst[2] = (unsigned char)((tmp >> 16) & 0xFF);
  12486. dst[3] = (unsigned char)((tmp >> 24) & 0xFF);
  12487. src += 5;
  12488. dst += 4;
  12489. }
  12490. }
  12491. /* -------------------------------------------------------------
  12492. *
  12493. * FONT ATLAS
  12494. *
  12495. * --------------------------------------------------------------*/
  12496. NK_API struct nk_font_config
  12497. nk_font_config(float pixel_height)
  12498. {
  12499. struct nk_font_config cfg;
  12500. nk_zero_struct(cfg);
  12501. cfg.ttf_blob = 0;
  12502. cfg.ttf_size = 0;
  12503. cfg.ttf_data_owned_by_atlas = 0;
  12504. cfg.size = pixel_height;
  12505. cfg.oversample_h = 3;
  12506. cfg.oversample_v = 1;
  12507. cfg.pixel_snap = 0;
  12508. cfg.coord_type = NK_COORD_UV;
  12509. cfg.spacing = nk_vec2(0,0);
  12510. cfg.range = nk_font_default_glyph_ranges();
  12511. cfg.merge_mode = 0;
  12512. cfg.fallback_glyph = '?';
  12513. cfg.font = 0;
  12514. cfg.n = 0;
  12515. return cfg;
  12516. }
  12517. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  12518. NK_API void
  12519. nk_font_atlas_init_default(struct nk_font_atlas *atlas)
  12520. {
  12521. NK_ASSERT(atlas);
  12522. if (!atlas) return;
  12523. nk_zero_struct(*atlas);
  12524. atlas->temporary.userdata.ptr = 0;
  12525. atlas->temporary.alloc = nk_malloc;
  12526. atlas->temporary.free = nk_mfree;
  12527. atlas->permanent.userdata.ptr = 0;
  12528. atlas->permanent.alloc = nk_malloc;
  12529. atlas->permanent.free = nk_mfree;
  12530. }
  12531. #endif
  12532. NK_API void
  12533. nk_font_atlas_init(struct nk_font_atlas *atlas, struct nk_allocator *alloc)
  12534. {
  12535. NK_ASSERT(atlas);
  12536. NK_ASSERT(alloc);
  12537. if (!atlas || !alloc) return;
  12538. nk_zero_struct(*atlas);
  12539. atlas->permanent = *alloc;
  12540. atlas->temporary = *alloc;
  12541. }
  12542. NK_API void
  12543. nk_font_atlas_init_custom(struct nk_font_atlas *atlas,
  12544. struct nk_allocator *permanent, struct nk_allocator *temporary)
  12545. {
  12546. NK_ASSERT(atlas);
  12547. NK_ASSERT(permanent);
  12548. NK_ASSERT(temporary);
  12549. if (!atlas || !permanent || !temporary) return;
  12550. nk_zero_struct(*atlas);
  12551. atlas->permanent = *permanent;
  12552. atlas->temporary = *temporary;
  12553. }
  12554. NK_API void
  12555. nk_font_atlas_begin(struct nk_font_atlas *atlas)
  12556. {
  12557. NK_ASSERT(atlas);
  12558. NK_ASSERT(atlas->temporary.alloc && atlas->temporary.free);
  12559. NK_ASSERT(atlas->permanent.alloc && atlas->permanent.free);
  12560. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free ||
  12561. !atlas->temporary.alloc || !atlas->temporary.free) return;
  12562. if (atlas->glyphs) {
  12563. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  12564. atlas->glyphs = 0;
  12565. }
  12566. if (atlas->pixel) {
  12567. atlas->permanent.free(atlas->permanent.userdata, atlas->pixel);
  12568. atlas->pixel = 0;
  12569. }
  12570. }
  12571. NK_API struct nk_font*
  12572. nk_font_atlas_add(struct nk_font_atlas *atlas, const struct nk_font_config *config)
  12573. {
  12574. struct nk_font *font = 0;
  12575. struct nk_font_config *cfg;
  12576. NK_ASSERT(atlas);
  12577. NK_ASSERT(atlas->permanent.alloc);
  12578. NK_ASSERT(atlas->permanent.free);
  12579. NK_ASSERT(atlas->temporary.alloc);
  12580. NK_ASSERT(atlas->temporary.free);
  12581. NK_ASSERT(config);
  12582. NK_ASSERT(config->ttf_blob);
  12583. NK_ASSERT(config->ttf_size);
  12584. NK_ASSERT(config->size > 0.0f);
  12585. if (!atlas || !config || !config->ttf_blob || !config->ttf_size || config->size <= 0.0f||
  12586. !atlas->permanent.alloc || !atlas->permanent.free ||
  12587. !atlas->temporary.alloc || !atlas->temporary.free)
  12588. return 0;
  12589. /* allocate font config */
  12590. cfg = (struct nk_font_config*)
  12591. atlas->permanent.alloc(atlas->permanent.userdata,0, sizeof(struct nk_font_config));
  12592. NK_MEMCPY(cfg, config, sizeof(*config));
  12593. cfg->n = cfg;
  12594. cfg->p = cfg;
  12595. if (!config->merge_mode) {
  12596. /* insert font config into list */
  12597. if (!atlas->config) {
  12598. atlas->config = cfg;
  12599. cfg->next = 0;
  12600. } else {
  12601. struct nk_font_config *i = atlas->config;
  12602. while (i->next) i = i->next;
  12603. i->next = cfg;
  12604. cfg->next = 0;
  12605. }
  12606. /* allocate new font */
  12607. font = (struct nk_font*)
  12608. atlas->permanent.alloc(atlas->permanent.userdata,0, sizeof(struct nk_font));
  12609. NK_ASSERT(font);
  12610. nk_zero(font, sizeof(*font));
  12611. if (!font) return 0;
  12612. font->config = cfg;
  12613. /* insert font into list */
  12614. if (!atlas->fonts) {
  12615. atlas->fonts = font;
  12616. font->next = 0;
  12617. } else {
  12618. struct nk_font *i = atlas->fonts;
  12619. while (i->next) i = i->next;
  12620. i->next = font;
  12621. font->next = 0;
  12622. }
  12623. cfg->font = &font->info;
  12624. } else {
  12625. /* extend previously added font */
  12626. struct nk_font *f = 0;
  12627. struct nk_font_config *c = 0;
  12628. NK_ASSERT(atlas->font_num);
  12629. f = atlas->fonts;
  12630. c = f->config;
  12631. cfg->font = &f->info;
  12632. cfg->n = c;
  12633. cfg->p = c->p;
  12634. c->p->n = cfg;
  12635. c->p = cfg;
  12636. }
  12637. /* create own copy of .TTF font blob */
  12638. if (!config->ttf_data_owned_by_atlas) {
  12639. cfg->ttf_blob = atlas->permanent.alloc(atlas->permanent.userdata,0, cfg->ttf_size);
  12640. NK_ASSERT(cfg->ttf_blob);
  12641. if (!cfg->ttf_blob) {
  12642. atlas->font_num++;
  12643. return 0;
  12644. }
  12645. NK_MEMCPY(cfg->ttf_blob, config->ttf_blob, cfg->ttf_size);
  12646. cfg->ttf_data_owned_by_atlas = 1;
  12647. }
  12648. atlas->font_num++;
  12649. return font;
  12650. }
  12651. NK_API struct nk_font*
  12652. nk_font_atlas_add_from_memory(struct nk_font_atlas *atlas, void *memory,
  12653. nk_size size, float height, const struct nk_font_config *config)
  12654. {
  12655. struct nk_font_config cfg;
  12656. NK_ASSERT(memory);
  12657. NK_ASSERT(size);
  12658. NK_ASSERT(atlas);
  12659. NK_ASSERT(atlas->temporary.alloc);
  12660. NK_ASSERT(atlas->temporary.free);
  12661. NK_ASSERT(atlas->permanent.alloc);
  12662. NK_ASSERT(atlas->permanent.free);
  12663. if (!atlas || !atlas->temporary.alloc || !atlas->temporary.free || !memory || !size ||
  12664. !atlas->permanent.alloc || !atlas->permanent.free)
  12665. return 0;
  12666. cfg = (config) ? *config: nk_font_config(height);
  12667. cfg.ttf_blob = memory;
  12668. cfg.ttf_size = size;
  12669. cfg.size = height;
  12670. cfg.ttf_data_owned_by_atlas = 0;
  12671. return nk_font_atlas_add(atlas, &cfg);
  12672. }
  12673. #ifdef NK_INCLUDE_STANDARD_IO
  12674. NK_API struct nk_font*
  12675. nk_font_atlas_add_from_file(struct nk_font_atlas *atlas, const char *file_path,
  12676. float height, const struct nk_font_config *config)
  12677. {
  12678. nk_size size;
  12679. char *memory;
  12680. struct nk_font_config cfg;
  12681. NK_ASSERT(atlas);
  12682. NK_ASSERT(atlas->temporary.alloc);
  12683. NK_ASSERT(atlas->temporary.free);
  12684. NK_ASSERT(atlas->permanent.alloc);
  12685. NK_ASSERT(atlas->permanent.free);
  12686. if (!atlas || !file_path) return 0;
  12687. memory = nk_file_load(file_path, &size, &atlas->permanent);
  12688. if (!memory) return 0;
  12689. cfg = (config) ? *config: nk_font_config(height);
  12690. cfg.ttf_blob = memory;
  12691. cfg.ttf_size = size;
  12692. cfg.size = height;
  12693. cfg.ttf_data_owned_by_atlas = 1;
  12694. return nk_font_atlas_add(atlas, &cfg);
  12695. }
  12696. #endif
  12697. NK_API struct nk_font*
  12698. nk_font_atlas_add_compressed(struct nk_font_atlas *atlas,
  12699. void *compressed_data, nk_size compressed_size, float height,
  12700. const struct nk_font_config *config)
  12701. {
  12702. unsigned int decompressed_size;
  12703. void *decompressed_data;
  12704. struct nk_font_config cfg;
  12705. NK_ASSERT(atlas);
  12706. NK_ASSERT(atlas->temporary.alloc);
  12707. NK_ASSERT(atlas->temporary.free);
  12708. NK_ASSERT(atlas->permanent.alloc);
  12709. NK_ASSERT(atlas->permanent.free);
  12710. NK_ASSERT(compressed_data);
  12711. NK_ASSERT(compressed_size);
  12712. if (!atlas || !compressed_data || !atlas->temporary.alloc || !atlas->temporary.free ||
  12713. !atlas->permanent.alloc || !atlas->permanent.free)
  12714. return 0;
  12715. decompressed_size = nk_decompress_length((unsigned char*)compressed_data);
  12716. decompressed_data = atlas->permanent.alloc(atlas->permanent.userdata,0,decompressed_size);
  12717. NK_ASSERT(decompressed_data);
  12718. if (!decompressed_data) return 0;
  12719. nk_decompress((unsigned char*)decompressed_data, (unsigned char*)compressed_data,
  12720. (unsigned int)compressed_size);
  12721. cfg = (config) ? *config: nk_font_config(height);
  12722. cfg.ttf_blob = decompressed_data;
  12723. cfg.ttf_size = decompressed_size;
  12724. cfg.size = height;
  12725. cfg.ttf_data_owned_by_atlas = 1;
  12726. return nk_font_atlas_add(atlas, &cfg);
  12727. }
  12728. NK_API struct nk_font*
  12729. nk_font_atlas_add_compressed_base85(struct nk_font_atlas *atlas,
  12730. const char *data_base85, float height, const struct nk_font_config *config)
  12731. {
  12732. int compressed_size;
  12733. void *compressed_data;
  12734. struct nk_font *font;
  12735. NK_ASSERT(atlas);
  12736. NK_ASSERT(atlas->temporary.alloc);
  12737. NK_ASSERT(atlas->temporary.free);
  12738. NK_ASSERT(atlas->permanent.alloc);
  12739. NK_ASSERT(atlas->permanent.free);
  12740. NK_ASSERT(data_base85);
  12741. if (!atlas || !data_base85 || !atlas->temporary.alloc || !atlas->temporary.free ||
  12742. !atlas->permanent.alloc || !atlas->permanent.free)
  12743. return 0;
  12744. compressed_size = (((int)nk_strlen(data_base85) + 4) / 5) * 4;
  12745. compressed_data = atlas->temporary.alloc(atlas->temporary.userdata,0, (nk_size)compressed_size);
  12746. NK_ASSERT(compressed_data);
  12747. if (!compressed_data) return 0;
  12748. nk_decode_85((unsigned char*)compressed_data, (const unsigned char*)data_base85);
  12749. font = nk_font_atlas_add_compressed(atlas, compressed_data,
  12750. (nk_size)compressed_size, height, config);
  12751. atlas->temporary.free(atlas->temporary.userdata, compressed_data);
  12752. return font;
  12753. }
  12754. #ifdef NK_INCLUDE_DEFAULT_FONT
  12755. NK_API struct nk_font*
  12756. nk_font_atlas_add_default(struct nk_font_atlas *atlas,
  12757. float pixel_height, const struct nk_font_config *config)
  12758. {
  12759. NK_ASSERT(atlas);
  12760. NK_ASSERT(atlas->temporary.alloc);
  12761. NK_ASSERT(atlas->temporary.free);
  12762. NK_ASSERT(atlas->permanent.alloc);
  12763. NK_ASSERT(atlas->permanent.free);
  12764. return nk_font_atlas_add_compressed_base85(atlas,
  12765. nk_proggy_clean_ttf_compressed_data_base85, pixel_height, config);
  12766. }
  12767. #endif
  12768. NK_API const void*
  12769. nk_font_atlas_bake(struct nk_font_atlas *atlas, int *width, int *height,
  12770. enum nk_font_atlas_format fmt)
  12771. {
  12772. int i = 0;
  12773. void *tmp = 0;
  12774. nk_size tmp_size, img_size;
  12775. struct nk_font *font_iter;
  12776. struct nk_font_baker *baker;
  12777. NK_ASSERT(atlas);
  12778. NK_ASSERT(atlas->temporary.alloc);
  12779. NK_ASSERT(atlas->temporary.free);
  12780. NK_ASSERT(atlas->permanent.alloc);
  12781. NK_ASSERT(atlas->permanent.free);
  12782. NK_ASSERT(width);
  12783. NK_ASSERT(height);
  12784. if (!atlas || !width || !height ||
  12785. !atlas->temporary.alloc || !atlas->temporary.free ||
  12786. !atlas->permanent.alloc || !atlas->permanent.free)
  12787. return 0;
  12788. #ifdef NK_INCLUDE_DEFAULT_FONT
  12789. /* no font added so just use default font */
  12790. if (!atlas->font_num)
  12791. atlas->default_font = nk_font_atlas_add_default(atlas, 13.0f, 0);
  12792. #endif
  12793. NK_ASSERT(atlas->font_num);
  12794. if (!atlas->font_num) return 0;
  12795. /* allocate temporary baker memory required for the baking process */
  12796. nk_font_baker_memory(&tmp_size, &atlas->glyph_count, atlas->config, atlas->font_num);
  12797. tmp = atlas->temporary.alloc(atlas->temporary.userdata,0, tmp_size);
  12798. NK_ASSERT(tmp);
  12799. if (!tmp) goto failed;
  12800. /* allocate glyph memory for all fonts */
  12801. baker = nk_font_baker(tmp, atlas->glyph_count, atlas->font_num, &atlas->temporary);
  12802. atlas->glyphs = (struct nk_font_glyph*)atlas->permanent.alloc(
  12803. atlas->permanent.userdata,0, sizeof(struct nk_font_glyph)*(nk_size)atlas->glyph_count);
  12804. NK_ASSERT(atlas->glyphs);
  12805. if (!atlas->glyphs)
  12806. goto failed;
  12807. /* pack all glyphs into a tight fit space */
  12808. atlas->custom.w = (NK_CURSOR_DATA_W*2)+1;
  12809. atlas->custom.h = NK_CURSOR_DATA_H + 1;
  12810. if (!nk_font_bake_pack(baker, &img_size, width, height, &atlas->custom,
  12811. atlas->config, atlas->font_num, &atlas->temporary))
  12812. goto failed;
  12813. /* allocate memory for the baked image font atlas */
  12814. atlas->pixel = atlas->temporary.alloc(atlas->temporary.userdata,0, img_size);
  12815. NK_ASSERT(atlas->pixel);
  12816. if (!atlas->pixel)
  12817. goto failed;
  12818. /* bake glyphs and custom white pixel into image */
  12819. nk_font_bake(baker, atlas->pixel, *width, *height,
  12820. atlas->glyphs, atlas->glyph_count, atlas->config, atlas->font_num);
  12821. nk_font_bake_custom_data(atlas->pixel, *width, *height, atlas->custom,
  12822. nk_custom_cursor_data, NK_CURSOR_DATA_W, NK_CURSOR_DATA_H, '.', 'X');
  12823. if (fmt == NK_FONT_ATLAS_RGBA32) {
  12824. /* convert alpha8 image into rgba32 image */
  12825. void *img_rgba = atlas->temporary.alloc(atlas->temporary.userdata,0,
  12826. (nk_size)(*width * *height * 4));
  12827. NK_ASSERT(img_rgba);
  12828. if (!img_rgba) goto failed;
  12829. nk_font_bake_convert(img_rgba, *width, *height, atlas->pixel);
  12830. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  12831. atlas->pixel = img_rgba;
  12832. }
  12833. atlas->tex_width = *width;
  12834. atlas->tex_height = *height;
  12835. /* initialize each font */
  12836. for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
  12837. struct nk_font *font = font_iter;
  12838. struct nk_font_config *config = font->config;
  12839. nk_font_init(font, config->size, config->fallback_glyph, atlas->glyphs,
  12840. config->font, nk_handle_ptr(0));
  12841. }
  12842. /* initialize each cursor */
  12843. {NK_STORAGE const struct nk_vec2 nk_cursor_data[NK_CURSOR_COUNT][3] = {
  12844. /* Pos Size Offset */
  12845. {{ 0, 3}, {12,19}, { 0, 0}},
  12846. {{13, 0}, { 7,16}, { 4, 8}},
  12847. {{31, 0}, {23,23}, {11,11}},
  12848. {{21, 0}, { 9, 23}, { 5,11}},
  12849. {{55,18}, {23, 9}, {11, 5}},
  12850. {{73, 0}, {17,17}, { 9, 9}},
  12851. {{55, 0}, {17,17}, { 9, 9}}
  12852. };
  12853. for (i = 0; i < NK_CURSOR_COUNT; ++i) {
  12854. struct nk_cursor *cursor = &atlas->cursors[i];
  12855. cursor->img.w = (unsigned short)*width;
  12856. cursor->img.h = (unsigned short)*height;
  12857. cursor->img.region[0] = (unsigned short)(atlas->custom.x + nk_cursor_data[i][0].x);
  12858. cursor->img.region[1] = (unsigned short)(atlas->custom.y + nk_cursor_data[i][0].y);
  12859. cursor->img.region[2] = (unsigned short)nk_cursor_data[i][1].x;
  12860. cursor->img.region[3] = (unsigned short)nk_cursor_data[i][1].y;
  12861. cursor->size = nk_cursor_data[i][1];
  12862. cursor->offset = nk_cursor_data[i][2];
  12863. }}
  12864. /* free temporary memory */
  12865. atlas->temporary.free(atlas->temporary.userdata, tmp);
  12866. return atlas->pixel;
  12867. failed:
  12868. /* error so cleanup all memory */
  12869. if (tmp) atlas->temporary.free(atlas->temporary.userdata, tmp);
  12870. if (atlas->glyphs) {
  12871. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  12872. atlas->glyphs = 0;
  12873. }
  12874. if (atlas->pixel) {
  12875. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  12876. atlas->pixel = 0;
  12877. }
  12878. return 0;
  12879. }
  12880. NK_API void
  12881. nk_font_atlas_end(struct nk_font_atlas *atlas, nk_handle texture,
  12882. struct nk_draw_null_texture *null)
  12883. {
  12884. int i = 0;
  12885. struct nk_font *font_iter;
  12886. NK_ASSERT(atlas);
  12887. if (!atlas) {
  12888. if (!null) return;
  12889. null->texture = texture;
  12890. null->uv = nk_vec2(0.5f,0.5f);
  12891. }
  12892. if (null) {
  12893. null->texture = texture;
  12894. null->uv.x = (atlas->custom.x + 0.5f)/(float)atlas->tex_width;
  12895. null->uv.y = (atlas->custom.y + 0.5f)/(float)atlas->tex_height;
  12896. }
  12897. for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
  12898. font_iter->texture = texture;
  12899. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  12900. font_iter->handle.texture = texture;
  12901. #endif
  12902. }
  12903. for (i = 0; i < NK_CURSOR_COUNT; ++i)
  12904. atlas->cursors[i].img.handle = texture;
  12905. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  12906. atlas->pixel = 0;
  12907. atlas->tex_width = 0;
  12908. atlas->tex_height = 0;
  12909. atlas->custom.x = 0;
  12910. atlas->custom.y = 0;
  12911. atlas->custom.w = 0;
  12912. atlas->custom.h = 0;
  12913. }
  12914. NK_API void
  12915. nk_font_atlas_cleanup(struct nk_font_atlas *atlas)
  12916. {
  12917. NK_ASSERT(atlas);
  12918. NK_ASSERT(atlas->temporary.alloc);
  12919. NK_ASSERT(atlas->temporary.free);
  12920. NK_ASSERT(atlas->permanent.alloc);
  12921. NK_ASSERT(atlas->permanent.free);
  12922. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free) return;
  12923. if (atlas->config) {
  12924. struct nk_font_config *iter;
  12925. for (iter = atlas->config; iter; iter = iter->next) {
  12926. struct nk_font_config *i;
  12927. for (i = iter->n; i != iter; i = i->n) {
  12928. atlas->permanent.free(atlas->permanent.userdata, i->ttf_blob);
  12929. i->ttf_blob = 0;
  12930. }
  12931. atlas->permanent.free(atlas->permanent.userdata, iter->ttf_blob);
  12932. iter->ttf_blob = 0;
  12933. }
  12934. }
  12935. }
  12936. NK_API void
  12937. nk_font_atlas_clear(struct nk_font_atlas *atlas)
  12938. {
  12939. NK_ASSERT(atlas);
  12940. NK_ASSERT(atlas->temporary.alloc);
  12941. NK_ASSERT(atlas->temporary.free);
  12942. NK_ASSERT(atlas->permanent.alloc);
  12943. NK_ASSERT(atlas->permanent.free);
  12944. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free) return;
  12945. if (atlas->config) {
  12946. struct nk_font_config *iter, *next;
  12947. for (iter = atlas->config; iter; iter = next) {
  12948. struct nk_font_config *i, *n;
  12949. for (i = iter->n; i != iter; i = n) {
  12950. n = i->n;
  12951. if (i->ttf_blob)
  12952. atlas->permanent.free(atlas->permanent.userdata, i->ttf_blob);
  12953. atlas->permanent.free(atlas->permanent.userdata, i);
  12954. }
  12955. next = iter->next;
  12956. if (i->ttf_blob)
  12957. atlas->permanent.free(atlas->permanent.userdata, iter->ttf_blob);
  12958. atlas->permanent.free(atlas->permanent.userdata, iter);
  12959. }
  12960. atlas->config = 0;
  12961. }
  12962. if (atlas->fonts) {
  12963. struct nk_font *iter, *next;
  12964. for (iter = atlas->fonts; iter; iter = next) {
  12965. next = iter->next;
  12966. atlas->permanent.free(atlas->permanent.userdata, iter);
  12967. }
  12968. atlas->fonts = 0;
  12969. }
  12970. if (atlas->glyphs)
  12971. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  12972. nk_zero_struct(*atlas);
  12973. }
  12974. #endif
  12975. /* ===============================================================
  12976. *
  12977. * INPUT
  12978. *
  12979. * ===============================================================*/
  12980. NK_API void
  12981. nk_input_begin(struct nk_context *ctx)
  12982. {
  12983. int i;
  12984. struct nk_input *in;
  12985. NK_ASSERT(ctx);
  12986. if (!ctx) return;
  12987. in = &ctx->input;
  12988. for (i = 0; i < NK_BUTTON_MAX; ++i)
  12989. in->mouse.buttons[i].clicked = 0;
  12990. in->keyboard.text_len = 0;
  12991. in->mouse.scroll_delta = nk_vec2(0,0);
  12992. in->mouse.prev.x = in->mouse.pos.x;
  12993. in->mouse.prev.y = in->mouse.pos.y;
  12994. in->mouse.delta.x = 0;
  12995. in->mouse.delta.y = 0;
  12996. for (i = 0; i < NK_KEY_MAX; i++)
  12997. in->keyboard.keys[i].clicked = 0;
  12998. }
  12999. NK_API void
  13000. nk_input_end(struct nk_context *ctx)
  13001. {
  13002. struct nk_input *in;
  13003. NK_ASSERT(ctx);
  13004. if (!ctx) return;
  13005. in = &ctx->input;
  13006. if (in->mouse.grab)
  13007. in->mouse.grab = 0;
  13008. if (in->mouse.ungrab) {
  13009. in->mouse.grabbed = 0;
  13010. in->mouse.ungrab = 0;
  13011. in->mouse.grab = 0;
  13012. }
  13013. }
  13014. NK_API void
  13015. nk_input_motion(struct nk_context *ctx, int x, int y)
  13016. {
  13017. struct nk_input *in;
  13018. NK_ASSERT(ctx);
  13019. if (!ctx) return;
  13020. in = &ctx->input;
  13021. in->mouse.pos.x = (float)x;
  13022. in->mouse.pos.y = (float)y;
  13023. in->mouse.delta.x = in->mouse.pos.x - in->mouse.prev.x;
  13024. in->mouse.delta.y = in->mouse.pos.y - in->mouse.prev.y;
  13025. }
  13026. NK_API void
  13027. nk_input_key(struct nk_context *ctx, enum nk_keys key, int down)
  13028. {
  13029. struct nk_input *in;
  13030. NK_ASSERT(ctx);
  13031. if (!ctx) return;
  13032. in = &ctx->input;
  13033. if (in->keyboard.keys[key].down != down)
  13034. in->keyboard.keys[key].clicked++;
  13035. in->keyboard.keys[key].down = down;
  13036. }
  13037. NK_API void
  13038. nk_input_button(struct nk_context *ctx, enum nk_buttons id, int x, int y, int down)
  13039. {
  13040. struct nk_mouse_button *btn;
  13041. struct nk_input *in;
  13042. NK_ASSERT(ctx);
  13043. if (!ctx) return;
  13044. in = &ctx->input;
  13045. if (in->mouse.buttons[id].down == down) return;
  13046. btn = &in->mouse.buttons[id];
  13047. btn->clicked_pos.x = (float)x;
  13048. btn->clicked_pos.y = (float)y;
  13049. btn->down = down;
  13050. btn->clicked++;
  13051. }
  13052. NK_API void
  13053. nk_input_scroll(struct nk_context *ctx, struct nk_vec2 val)
  13054. {
  13055. NK_ASSERT(ctx);
  13056. if (!ctx) return;
  13057. ctx->input.mouse.scroll_delta.x += val.x;
  13058. ctx->input.mouse.scroll_delta.y += val.y;
  13059. }
  13060. NK_API void
  13061. nk_input_glyph(struct nk_context *ctx, const nk_glyph glyph)
  13062. {
  13063. int len = 0;
  13064. nk_rune unicode;
  13065. struct nk_input *in;
  13066. NK_ASSERT(ctx);
  13067. if (!ctx) return;
  13068. in = &ctx->input;
  13069. len = nk_utf_decode(glyph, &unicode, NK_UTF_SIZE);
  13070. if (len && ((in->keyboard.text_len + len) < NK_INPUT_MAX)) {
  13071. nk_utf_encode(unicode, &in->keyboard.text[in->keyboard.text_len],
  13072. NK_INPUT_MAX - in->keyboard.text_len);
  13073. in->keyboard.text_len += len;
  13074. }
  13075. }
  13076. NK_API void
  13077. nk_input_char(struct nk_context *ctx, char c)
  13078. {
  13079. nk_glyph glyph;
  13080. NK_ASSERT(ctx);
  13081. if (!ctx) return;
  13082. glyph[0] = c;
  13083. nk_input_glyph(ctx, glyph);
  13084. }
  13085. NK_API void
  13086. nk_input_unicode(struct nk_context *ctx, nk_rune unicode)
  13087. {
  13088. nk_glyph rune;
  13089. NK_ASSERT(ctx);
  13090. if (!ctx) return;
  13091. nk_utf_encode(unicode, rune, NK_UTF_SIZE);
  13092. nk_input_glyph(ctx, rune);
  13093. }
  13094. NK_API int
  13095. nk_input_has_mouse_click(const struct nk_input *i, enum nk_buttons id)
  13096. {
  13097. const struct nk_mouse_button *btn;
  13098. if (!i) return nk_false;
  13099. btn = &i->mouse.buttons[id];
  13100. return (btn->clicked && btn->down == nk_false) ? nk_true : nk_false;
  13101. }
  13102. NK_API int
  13103. nk_input_has_mouse_click_in_rect(const struct nk_input *i, enum nk_buttons id,
  13104. struct nk_rect b)
  13105. {
  13106. const struct nk_mouse_button *btn;
  13107. if (!i) return nk_false;
  13108. btn = &i->mouse.buttons[id];
  13109. if (!NK_INBOX(btn->clicked_pos.x,btn->clicked_pos.y,b.x,b.y,b.w,b.h))
  13110. return nk_false;
  13111. return nk_true;
  13112. }
  13113. NK_API int
  13114. nk_input_has_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id,
  13115. struct nk_rect b, int down)
  13116. {
  13117. const struct nk_mouse_button *btn;
  13118. if (!i) return nk_false;
  13119. btn = &i->mouse.buttons[id];
  13120. return nk_input_has_mouse_click_in_rect(i, id, b) && (btn->down == down);
  13121. }
  13122. NK_API int
  13123. nk_input_is_mouse_click_in_rect(const struct nk_input *i, enum nk_buttons id,
  13124. struct nk_rect b)
  13125. {
  13126. const struct nk_mouse_button *btn;
  13127. if (!i) return nk_false;
  13128. btn = &i->mouse.buttons[id];
  13129. return (nk_input_has_mouse_click_down_in_rect(i, id, b, nk_false) &&
  13130. btn->clicked) ? nk_true : nk_false;
  13131. }
  13132. NK_API int
  13133. nk_input_is_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id,
  13134. struct nk_rect b, int down)
  13135. {
  13136. const struct nk_mouse_button *btn;
  13137. if (!i) return nk_false;
  13138. btn = &i->mouse.buttons[id];
  13139. return (nk_input_has_mouse_click_down_in_rect(i, id, b, down) &&
  13140. btn->clicked) ? nk_true : nk_false;
  13141. }
  13142. NK_API int
  13143. nk_input_any_mouse_click_in_rect(const struct nk_input *in, struct nk_rect b)
  13144. {
  13145. int i, down = 0;
  13146. for (i = 0; i < NK_BUTTON_MAX; ++i)
  13147. down = down || nk_input_is_mouse_click_in_rect(in, (enum nk_buttons)i, b);
  13148. return down;
  13149. }
  13150. NK_API int
  13151. nk_input_is_mouse_hovering_rect(const struct nk_input *i, struct nk_rect rect)
  13152. {
  13153. if (!i) return nk_false;
  13154. return NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h);
  13155. }
  13156. NK_API int
  13157. nk_input_is_mouse_prev_hovering_rect(const struct nk_input *i, struct nk_rect rect)
  13158. {
  13159. if (!i) return nk_false;
  13160. return NK_INBOX(i->mouse.prev.x, i->mouse.prev.y, rect.x, rect.y, rect.w, rect.h);
  13161. }
  13162. NK_API int
  13163. nk_input_mouse_clicked(const struct nk_input *i, enum nk_buttons id, struct nk_rect rect)
  13164. {
  13165. if (!i) return nk_false;
  13166. if (!nk_input_is_mouse_hovering_rect(i, rect)) return nk_false;
  13167. return nk_input_is_mouse_click_in_rect(i, id, rect);
  13168. }
  13169. NK_API int
  13170. nk_input_is_mouse_down(const struct nk_input *i, enum nk_buttons id)
  13171. {
  13172. if (!i) return nk_false;
  13173. return i->mouse.buttons[id].down;
  13174. }
  13175. NK_API int
  13176. nk_input_is_mouse_pressed(const struct nk_input *i, enum nk_buttons id)
  13177. {
  13178. const struct nk_mouse_button *b;
  13179. if (!i) return nk_false;
  13180. b = &i->mouse.buttons[id];
  13181. if (b->down && b->clicked)
  13182. return nk_true;
  13183. return nk_false;
  13184. }
  13185. NK_API int
  13186. nk_input_is_mouse_released(const struct nk_input *i, enum nk_buttons id)
  13187. {
  13188. if (!i) return nk_false;
  13189. return (!i->mouse.buttons[id].down && i->mouse.buttons[id].clicked);
  13190. }
  13191. NK_API int
  13192. nk_input_is_key_pressed(const struct nk_input *i, enum nk_keys key)
  13193. {
  13194. const struct nk_key *k;
  13195. if (!i) return nk_false;
  13196. k = &i->keyboard.keys[key];
  13197. if ((k->down && k->clicked) || (!k->down && k->clicked >= 2))
  13198. return nk_true;
  13199. return nk_false;
  13200. }
  13201. NK_API int
  13202. nk_input_is_key_released(const struct nk_input *i, enum nk_keys key)
  13203. {
  13204. const struct nk_key *k;
  13205. if (!i) return nk_false;
  13206. k = &i->keyboard.keys[key];
  13207. if ((!k->down && k->clicked) || (k->down && k->clicked >= 2))
  13208. return nk_true;
  13209. return nk_false;
  13210. }
  13211. NK_API int
  13212. nk_input_is_key_down(const struct nk_input *i, enum nk_keys key)
  13213. {
  13214. const struct nk_key *k;
  13215. if (!i) return nk_false;
  13216. k = &i->keyboard.keys[key];
  13217. if (k->down) return nk_true;
  13218. return nk_false;
  13219. }
  13220. /* ===============================================================
  13221. *
  13222. * STYLE
  13223. *
  13224. * ===============================================================*/
  13225. NK_API void nk_style_default(struct nk_context *ctx){nk_style_from_table(ctx, 0);}
  13226. #define NK_COLOR_MAP(NK_COLOR)\
  13227. NK_COLOR(NK_COLOR_TEXT, 175,175,175,255) \
  13228. NK_COLOR(NK_COLOR_WINDOW, 45, 45, 45, 255) \
  13229. NK_COLOR(NK_COLOR_HEADER, 40, 40, 40, 255) \
  13230. NK_COLOR(NK_COLOR_BORDER, 65, 65, 65, 255) \
  13231. NK_COLOR(NK_COLOR_BUTTON, 50, 50, 50, 255) \
  13232. NK_COLOR(NK_COLOR_BUTTON_HOVER, 40, 40, 40, 255) \
  13233. NK_COLOR(NK_COLOR_BUTTON_ACTIVE, 35, 35, 35, 255) \
  13234. NK_COLOR(NK_COLOR_TOGGLE, 100,100,100,255) \
  13235. NK_COLOR(NK_COLOR_TOGGLE_HOVER, 120,120,120,255) \
  13236. NK_COLOR(NK_COLOR_TOGGLE_CURSOR, 45, 45, 45, 255) \
  13237. NK_COLOR(NK_COLOR_SELECT, 45, 45, 45, 255) \
  13238. NK_COLOR(NK_COLOR_SELECT_ACTIVE, 35, 35, 35,255) \
  13239. NK_COLOR(NK_COLOR_SLIDER, 38, 38, 38, 255) \
  13240. NK_COLOR(NK_COLOR_SLIDER_CURSOR, 100,100,100,255) \
  13241. NK_COLOR(NK_COLOR_SLIDER_CURSOR_HOVER, 120,120,120,255) \
  13242. NK_COLOR(NK_COLOR_SLIDER_CURSOR_ACTIVE, 150,150,150,255) \
  13243. NK_COLOR(NK_COLOR_PROPERTY, 38, 38, 38, 255) \
  13244. NK_COLOR(NK_COLOR_EDIT, 38, 38, 38, 255) \
  13245. NK_COLOR(NK_COLOR_EDIT_CURSOR, 175,175,175,255) \
  13246. NK_COLOR(NK_COLOR_COMBO, 45, 45, 45, 255) \
  13247. NK_COLOR(NK_COLOR_CHART, 120,120,120,255) \
  13248. NK_COLOR(NK_COLOR_CHART_COLOR, 45, 45, 45, 255) \
  13249. NK_COLOR(NK_COLOR_CHART_COLOR_HIGHLIGHT, 255, 0, 0, 255) \
  13250. NK_COLOR(NK_COLOR_SCROLLBAR, 40, 40, 40, 255) \
  13251. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR, 100,100,100,255) \
  13252. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR_HOVER, 120,120,120,255) \
  13253. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR_ACTIVE, 150,150,150,255) \
  13254. NK_COLOR(NK_COLOR_TAB_HEADER, 40, 40, 40,255)
  13255. NK_GLOBAL const struct nk_color
  13256. nk_default_color_style[NK_COLOR_COUNT] = {
  13257. #define NK_COLOR(a,b,c,d,e) {b,c,d,e},
  13258. NK_COLOR_MAP(NK_COLOR)
  13259. #undef NK_COLOR
  13260. };
  13261. NK_GLOBAL const char *nk_color_names[NK_COLOR_COUNT] = {
  13262. #define NK_COLOR(a,b,c,d,e) #a,
  13263. NK_COLOR_MAP(NK_COLOR)
  13264. #undef NK_COLOR
  13265. };
  13266. NK_API const char*
  13267. nk_style_get_color_by_name(enum nk_style_colors c)
  13268. {
  13269. return nk_color_names[c];
  13270. }
  13271. NK_API struct nk_style_item
  13272. nk_style_item_image(struct nk_image img)
  13273. {
  13274. struct nk_style_item i;
  13275. i.type = NK_STYLE_ITEM_IMAGE;
  13276. i.data.image = img;
  13277. return i;
  13278. }
  13279. NK_API struct nk_style_item
  13280. nk_style_item_color(struct nk_color col)
  13281. {
  13282. struct nk_style_item i;
  13283. i.type = NK_STYLE_ITEM_COLOR;
  13284. i.data.color = col;
  13285. return i;
  13286. }
  13287. NK_API struct nk_style_item
  13288. nk_style_item_hide(void)
  13289. {
  13290. struct nk_style_item i;
  13291. i.type = NK_STYLE_ITEM_COLOR;
  13292. i.data.color = nk_rgba(0,0,0,0);
  13293. return i;
  13294. }
  13295. NK_API void
  13296. nk_style_from_table(struct nk_context *ctx, const struct nk_color *table)
  13297. {
  13298. struct nk_style *style;
  13299. struct nk_style_text *text;
  13300. struct nk_style_button *button;
  13301. struct nk_style_toggle *toggle;
  13302. struct nk_style_selectable *select;
  13303. struct nk_style_slider *slider;
  13304. struct nk_style_progress *prog;
  13305. struct nk_style_scrollbar *scroll;
  13306. struct nk_style_edit *edit;
  13307. struct nk_style_property *property;
  13308. struct nk_style_combo *combo;
  13309. struct nk_style_chart *chart;
  13310. struct nk_style_tab *tab;
  13311. struct nk_style_window *win;
  13312. NK_ASSERT(ctx);
  13313. if (!ctx) return;
  13314. style = &ctx->style;
  13315. table = (!table) ? nk_default_color_style: table;
  13316. /* default text */
  13317. text = &style->text;
  13318. text->color = table[NK_COLOR_TEXT];
  13319. text->padding = nk_vec2(0,0);
  13320. /* default button */
  13321. button = &style->button;
  13322. nk_zero_struct(*button);
  13323. button->normal = nk_style_item_color(table[NK_COLOR_BUTTON]);
  13324. button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]);
  13325. button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]);
  13326. button->border_color = table[NK_COLOR_BORDER];
  13327. button->text_background = table[NK_COLOR_BUTTON];
  13328. button->text_normal = table[NK_COLOR_TEXT];
  13329. button->text_hover = table[NK_COLOR_TEXT];
  13330. button->text_active = table[NK_COLOR_TEXT];
  13331. button->padding = nk_vec2(2.0f,2.0f);
  13332. button->image_padding = nk_vec2(0.0f,0.0f);
  13333. button->touch_padding = nk_vec2(0.0f, 0.0f);
  13334. button->userdata = nk_handle_ptr(0);
  13335. button->text_alignment = NK_TEXT_CENTERED;
  13336. button->border = 1.0f;
  13337. button->rounding = 4.0f;
  13338. button->draw_begin = 0;
  13339. button->draw_end = 0;
  13340. /* contextual button */
  13341. button = &style->contextual_button;
  13342. nk_zero_struct(*button);
  13343. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13344. button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]);
  13345. button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]);
  13346. button->border_color = table[NK_COLOR_WINDOW];
  13347. button->text_background = table[NK_COLOR_WINDOW];
  13348. button->text_normal = table[NK_COLOR_TEXT];
  13349. button->text_hover = table[NK_COLOR_TEXT];
  13350. button->text_active = table[NK_COLOR_TEXT];
  13351. button->padding = nk_vec2(2.0f,2.0f);
  13352. button->touch_padding = nk_vec2(0.0f,0.0f);
  13353. button->userdata = nk_handle_ptr(0);
  13354. button->text_alignment = NK_TEXT_CENTERED;
  13355. button->border = 0.0f;
  13356. button->rounding = 0.0f;
  13357. button->draw_begin = 0;
  13358. button->draw_end = 0;
  13359. /* menu button */
  13360. button = &style->menu_button;
  13361. nk_zero_struct(*button);
  13362. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13363. button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13364. button->active = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13365. button->border_color = table[NK_COLOR_WINDOW];
  13366. button->text_background = table[NK_COLOR_WINDOW];
  13367. button->text_normal = table[NK_COLOR_TEXT];
  13368. button->text_hover = table[NK_COLOR_TEXT];
  13369. button->text_active = table[NK_COLOR_TEXT];
  13370. button->padding = nk_vec2(2.0f,2.0f);
  13371. button->touch_padding = nk_vec2(0.0f,0.0f);
  13372. button->userdata = nk_handle_ptr(0);
  13373. button->text_alignment = NK_TEXT_CENTERED;
  13374. button->border = 0.0f;
  13375. button->rounding = 1.0f;
  13376. button->draw_begin = 0;
  13377. button->draw_end = 0;
  13378. /* checkbox toggle */
  13379. toggle = &style->checkbox;
  13380. nk_zero_struct(*toggle);
  13381. toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]);
  13382. toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  13383. toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  13384. toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  13385. toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  13386. toggle->userdata = nk_handle_ptr(0);
  13387. toggle->text_background = table[NK_COLOR_WINDOW];
  13388. toggle->text_normal = table[NK_COLOR_TEXT];
  13389. toggle->text_hover = table[NK_COLOR_TEXT];
  13390. toggle->text_active = table[NK_COLOR_TEXT];
  13391. toggle->padding = nk_vec2(2.0f, 2.0f);
  13392. toggle->touch_padding = nk_vec2(0,0);
  13393. toggle->border_color = nk_rgba(0,0,0,0);
  13394. toggle->border = 0.0f;
  13395. toggle->spacing = 4;
  13396. /* option toggle */
  13397. toggle = &style->option;
  13398. nk_zero_struct(*toggle);
  13399. toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]);
  13400. toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  13401. toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  13402. toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  13403. toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  13404. toggle->userdata = nk_handle_ptr(0);
  13405. toggle->text_background = table[NK_COLOR_WINDOW];
  13406. toggle->text_normal = table[NK_COLOR_TEXT];
  13407. toggle->text_hover = table[NK_COLOR_TEXT];
  13408. toggle->text_active = table[NK_COLOR_TEXT];
  13409. toggle->padding = nk_vec2(3.0f, 3.0f);
  13410. toggle->touch_padding = nk_vec2(0,0);
  13411. toggle->border_color = nk_rgba(0,0,0,0);
  13412. toggle->border = 0.0f;
  13413. toggle->spacing = 4;
  13414. /* selectable */
  13415. select = &style->selectable;
  13416. nk_zero_struct(*select);
  13417. select->normal = nk_style_item_color(table[NK_COLOR_SELECT]);
  13418. select->hover = nk_style_item_color(table[NK_COLOR_SELECT]);
  13419. select->pressed = nk_style_item_color(table[NK_COLOR_SELECT]);
  13420. select->normal_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  13421. select->hover_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  13422. select->pressed_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  13423. select->text_normal = table[NK_COLOR_TEXT];
  13424. select->text_hover = table[NK_COLOR_TEXT];
  13425. select->text_pressed = table[NK_COLOR_TEXT];
  13426. select->text_normal_active = table[NK_COLOR_TEXT];
  13427. select->text_hover_active = table[NK_COLOR_TEXT];
  13428. select->text_pressed_active = table[NK_COLOR_TEXT];
  13429. select->padding = nk_vec2(2.0f,2.0f);
  13430. select->image_padding = nk_vec2(2.0f,2.0f);
  13431. select->touch_padding = nk_vec2(0,0);
  13432. select->userdata = nk_handle_ptr(0);
  13433. select->rounding = 0.0f;
  13434. select->draw_begin = 0;
  13435. select->draw_end = 0;
  13436. /* slider */
  13437. slider = &style->slider;
  13438. nk_zero_struct(*slider);
  13439. slider->normal = nk_style_item_hide();
  13440. slider->hover = nk_style_item_hide();
  13441. slider->active = nk_style_item_hide();
  13442. slider->bar_normal = table[NK_COLOR_SLIDER];
  13443. slider->bar_hover = table[NK_COLOR_SLIDER];
  13444. slider->bar_active = table[NK_COLOR_SLIDER];
  13445. slider->bar_filled = table[NK_COLOR_SLIDER_CURSOR];
  13446. slider->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]);
  13447. slider->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]);
  13448. slider->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]);
  13449. slider->inc_symbol = NK_SYMBOL_TRIANGLE_RIGHT;
  13450. slider->dec_symbol = NK_SYMBOL_TRIANGLE_LEFT;
  13451. slider->cursor_size = nk_vec2(16,16);
  13452. slider->padding = nk_vec2(2,2);
  13453. slider->spacing = nk_vec2(2,2);
  13454. slider->userdata = nk_handle_ptr(0);
  13455. slider->show_buttons = nk_false;
  13456. slider->bar_height = 8;
  13457. slider->rounding = 0;
  13458. slider->draw_begin = 0;
  13459. slider->draw_end = 0;
  13460. /* slider buttons */
  13461. button = &style->slider.inc_button;
  13462. button->normal = nk_style_item_color(nk_rgb(40,40,40));
  13463. button->hover = nk_style_item_color(nk_rgb(42,42,42));
  13464. button->active = nk_style_item_color(nk_rgb(44,44,44));
  13465. button->border_color = nk_rgb(65,65,65);
  13466. button->text_background = nk_rgb(40,40,40);
  13467. button->text_normal = nk_rgb(175,175,175);
  13468. button->text_hover = nk_rgb(175,175,175);
  13469. button->text_active = nk_rgb(175,175,175);
  13470. button->padding = nk_vec2(8.0f,8.0f);
  13471. button->touch_padding = nk_vec2(0.0f,0.0f);
  13472. button->userdata = nk_handle_ptr(0);
  13473. button->text_alignment = NK_TEXT_CENTERED;
  13474. button->border = 1.0f;
  13475. button->rounding = 0.0f;
  13476. button->draw_begin = 0;
  13477. button->draw_end = 0;
  13478. style->slider.dec_button = style->slider.inc_button;
  13479. /* progressbar */
  13480. prog = &style->progress;
  13481. nk_zero_struct(*prog);
  13482. prog->normal = nk_style_item_color(table[NK_COLOR_SLIDER]);
  13483. prog->hover = nk_style_item_color(table[NK_COLOR_SLIDER]);
  13484. prog->active = nk_style_item_color(table[NK_COLOR_SLIDER]);
  13485. prog->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]);
  13486. prog->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]);
  13487. prog->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]);
  13488. prog->border_color = nk_rgba(0,0,0,0);
  13489. prog->cursor_border_color = nk_rgba(0,0,0,0);
  13490. prog->userdata = nk_handle_ptr(0);
  13491. prog->padding = nk_vec2(4,4);
  13492. prog->rounding = 0;
  13493. prog->border = 0;
  13494. prog->cursor_rounding = 0;
  13495. prog->cursor_border = 0;
  13496. prog->draw_begin = 0;
  13497. prog->draw_end = 0;
  13498. /* scrollbars */
  13499. scroll = &style->scrollh;
  13500. nk_zero_struct(*scroll);
  13501. scroll->normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  13502. scroll->hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  13503. scroll->active = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  13504. scroll->cursor_normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR]);
  13505. scroll->cursor_hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_HOVER]);
  13506. scroll->cursor_active = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE]);
  13507. scroll->dec_symbol = NK_SYMBOL_CIRCLE_SOLID;
  13508. scroll->inc_symbol = NK_SYMBOL_CIRCLE_SOLID;
  13509. scroll->userdata = nk_handle_ptr(0);
  13510. scroll->border_color = table[NK_COLOR_SCROLLBAR];
  13511. scroll->cursor_border_color = table[NK_COLOR_SCROLLBAR];
  13512. scroll->padding = nk_vec2(0,0);
  13513. scroll->show_buttons = nk_false;
  13514. scroll->border = 0;
  13515. scroll->rounding = 0;
  13516. scroll->border_cursor = 0;
  13517. scroll->rounding_cursor = 0;
  13518. scroll->draw_begin = 0;
  13519. scroll->draw_end = 0;
  13520. style->scrollv = style->scrollh;
  13521. /* scrollbars buttons */
  13522. button = &style->scrollh.inc_button;
  13523. button->normal = nk_style_item_color(nk_rgb(40,40,40));
  13524. button->hover = nk_style_item_color(nk_rgb(42,42,42));
  13525. button->active = nk_style_item_color(nk_rgb(44,44,44));
  13526. button->border_color = nk_rgb(65,65,65);
  13527. button->text_background = nk_rgb(40,40,40);
  13528. button->text_normal = nk_rgb(175,175,175);
  13529. button->text_hover = nk_rgb(175,175,175);
  13530. button->text_active = nk_rgb(175,175,175);
  13531. button->padding = nk_vec2(4.0f,4.0f);
  13532. button->touch_padding = nk_vec2(0.0f,0.0f);
  13533. button->userdata = nk_handle_ptr(0);
  13534. button->text_alignment = NK_TEXT_CENTERED;
  13535. button->border = 1.0f;
  13536. button->rounding = 0.0f;
  13537. button->draw_begin = 0;
  13538. button->draw_end = 0;
  13539. style->scrollh.dec_button = style->scrollh.inc_button;
  13540. style->scrollv.inc_button = style->scrollh.inc_button;
  13541. style->scrollv.dec_button = style->scrollh.inc_button;
  13542. /* edit */
  13543. edit = &style->edit;
  13544. nk_zero_struct(*edit);
  13545. edit->normal = nk_style_item_color(table[NK_COLOR_EDIT]);
  13546. edit->hover = nk_style_item_color(table[NK_COLOR_EDIT]);
  13547. edit->active = nk_style_item_color(table[NK_COLOR_EDIT]);
  13548. edit->cursor_normal = table[NK_COLOR_TEXT];
  13549. edit->cursor_hover = table[NK_COLOR_TEXT];
  13550. edit->cursor_text_normal= table[NK_COLOR_EDIT];
  13551. edit->cursor_text_hover = table[NK_COLOR_EDIT];
  13552. edit->border_color = table[NK_COLOR_BORDER];
  13553. edit->text_normal = table[NK_COLOR_TEXT];
  13554. edit->text_hover = table[NK_COLOR_TEXT];
  13555. edit->text_active = table[NK_COLOR_TEXT];
  13556. edit->selected_normal = table[NK_COLOR_TEXT];
  13557. edit->selected_hover = table[NK_COLOR_TEXT];
  13558. edit->selected_text_normal = table[NK_COLOR_EDIT];
  13559. edit->selected_text_hover = table[NK_COLOR_EDIT];
  13560. edit->scrollbar_size = nk_vec2(10,10);
  13561. edit->scrollbar = style->scrollv;
  13562. edit->padding = nk_vec2(4,4);
  13563. edit->row_padding = 2;
  13564. edit->cursor_size = 4;
  13565. edit->border = 1;
  13566. edit->rounding = 0;
  13567. /* property */
  13568. property = &style->property;
  13569. nk_zero_struct(*property);
  13570. property->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13571. property->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13572. property->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13573. property->border_color = table[NK_COLOR_BORDER];
  13574. property->label_normal = table[NK_COLOR_TEXT];
  13575. property->label_hover = table[NK_COLOR_TEXT];
  13576. property->label_active = table[NK_COLOR_TEXT];
  13577. property->sym_left = NK_SYMBOL_TRIANGLE_LEFT;
  13578. property->sym_right = NK_SYMBOL_TRIANGLE_RIGHT;
  13579. property->userdata = nk_handle_ptr(0);
  13580. property->padding = nk_vec2(4,4);
  13581. property->border = 1;
  13582. property->rounding = 10;
  13583. property->draw_begin = 0;
  13584. property->draw_end = 0;
  13585. /* property buttons */
  13586. button = &style->property.dec_button;
  13587. nk_zero_struct(*button);
  13588. button->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13589. button->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13590. button->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13591. button->border_color = nk_rgba(0,0,0,0);
  13592. button->text_background = table[NK_COLOR_PROPERTY];
  13593. button->text_normal = table[NK_COLOR_TEXT];
  13594. button->text_hover = table[NK_COLOR_TEXT];
  13595. button->text_active = table[NK_COLOR_TEXT];
  13596. button->padding = nk_vec2(0.0f,0.0f);
  13597. button->touch_padding = nk_vec2(0.0f,0.0f);
  13598. button->userdata = nk_handle_ptr(0);
  13599. button->text_alignment = NK_TEXT_CENTERED;
  13600. button->border = 0.0f;
  13601. button->rounding = 0.0f;
  13602. button->draw_begin = 0;
  13603. button->draw_end = 0;
  13604. style->property.inc_button = style->property.dec_button;
  13605. /* property edit */
  13606. edit = &style->property.edit;
  13607. nk_zero_struct(*edit);
  13608. edit->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13609. edit->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13610. edit->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13611. edit->border_color = nk_rgba(0,0,0,0);
  13612. edit->cursor_normal = table[NK_COLOR_TEXT];
  13613. edit->cursor_hover = table[NK_COLOR_TEXT];
  13614. edit->cursor_text_normal= table[NK_COLOR_EDIT];
  13615. edit->cursor_text_hover = table[NK_COLOR_EDIT];
  13616. edit->text_normal = table[NK_COLOR_TEXT];
  13617. edit->text_hover = table[NK_COLOR_TEXT];
  13618. edit->text_active = table[NK_COLOR_TEXT];
  13619. edit->selected_normal = table[NK_COLOR_TEXT];
  13620. edit->selected_hover = table[NK_COLOR_TEXT];
  13621. edit->selected_text_normal = table[NK_COLOR_EDIT];
  13622. edit->selected_text_hover = table[NK_COLOR_EDIT];
  13623. edit->padding = nk_vec2(0,0);
  13624. edit->cursor_size = 8;
  13625. edit->border = 0;
  13626. edit->rounding = 0;
  13627. /* chart */
  13628. chart = &style->chart;
  13629. nk_zero_struct(*chart);
  13630. chart->background = nk_style_item_color(table[NK_COLOR_CHART]);
  13631. chart->border_color = table[NK_COLOR_BORDER];
  13632. chart->selected_color = table[NK_COLOR_CHART_COLOR_HIGHLIGHT];
  13633. chart->color = table[NK_COLOR_CHART_COLOR];
  13634. chart->padding = nk_vec2(4,4);
  13635. chart->border = 0;
  13636. chart->rounding = 0;
  13637. /* combo */
  13638. combo = &style->combo;
  13639. combo->normal = nk_style_item_color(table[NK_COLOR_COMBO]);
  13640. combo->hover = nk_style_item_color(table[NK_COLOR_COMBO]);
  13641. combo->active = nk_style_item_color(table[NK_COLOR_COMBO]);
  13642. combo->border_color = table[NK_COLOR_BORDER];
  13643. combo->label_normal = table[NK_COLOR_TEXT];
  13644. combo->label_hover = table[NK_COLOR_TEXT];
  13645. combo->label_active = table[NK_COLOR_TEXT];
  13646. combo->sym_normal = NK_SYMBOL_TRIANGLE_DOWN;
  13647. combo->sym_hover = NK_SYMBOL_TRIANGLE_DOWN;
  13648. combo->sym_active = NK_SYMBOL_TRIANGLE_DOWN;
  13649. combo->content_padding = nk_vec2(4,4);
  13650. combo->button_padding = nk_vec2(0,4);
  13651. combo->spacing = nk_vec2(4,0);
  13652. combo->border = 1;
  13653. combo->rounding = 0;
  13654. /* combo button */
  13655. button = &style->combo.button;
  13656. nk_zero_struct(*button);
  13657. button->normal = nk_style_item_color(table[NK_COLOR_COMBO]);
  13658. button->hover = nk_style_item_color(table[NK_COLOR_COMBO]);
  13659. button->active = nk_style_item_color(table[NK_COLOR_COMBO]);
  13660. button->border_color = nk_rgba(0,0,0,0);
  13661. button->text_background = table[NK_COLOR_COMBO];
  13662. button->text_normal = table[NK_COLOR_TEXT];
  13663. button->text_hover = table[NK_COLOR_TEXT];
  13664. button->text_active = table[NK_COLOR_TEXT];
  13665. button->padding = nk_vec2(2.0f,2.0f);
  13666. button->touch_padding = nk_vec2(0.0f,0.0f);
  13667. button->userdata = nk_handle_ptr(0);
  13668. button->text_alignment = NK_TEXT_CENTERED;
  13669. button->border = 0.0f;
  13670. button->rounding = 0.0f;
  13671. button->draw_begin = 0;
  13672. button->draw_end = 0;
  13673. /* tab */
  13674. tab = &style->tab;
  13675. tab->background = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  13676. tab->border_color = table[NK_COLOR_BORDER];
  13677. tab->text = table[NK_COLOR_TEXT];
  13678. tab->sym_minimize = NK_SYMBOL_TRIANGLE_RIGHT;
  13679. tab->sym_maximize = NK_SYMBOL_TRIANGLE_DOWN;
  13680. tab->padding = nk_vec2(4,4);
  13681. tab->spacing = nk_vec2(4,4);
  13682. tab->indent = 10.0f;
  13683. tab->border = 1;
  13684. tab->rounding = 0;
  13685. /* tab button */
  13686. button = &style->tab.tab_minimize_button;
  13687. nk_zero_struct(*button);
  13688. button->normal = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  13689. button->hover = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  13690. button->active = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  13691. button->border_color = nk_rgba(0,0,0,0);
  13692. button->text_background = table[NK_COLOR_TAB_HEADER];
  13693. button->text_normal = table[NK_COLOR_TEXT];
  13694. button->text_hover = table[NK_COLOR_TEXT];
  13695. button->text_active = table[NK_COLOR_TEXT];
  13696. button->padding = nk_vec2(2.0f,2.0f);
  13697. button->touch_padding = nk_vec2(0.0f,0.0f);
  13698. button->userdata = nk_handle_ptr(0);
  13699. button->text_alignment = NK_TEXT_CENTERED;
  13700. button->border = 0.0f;
  13701. button->rounding = 0.0f;
  13702. button->draw_begin = 0;
  13703. button->draw_end = 0;
  13704. style->tab.tab_maximize_button =*button;
  13705. /* node button */
  13706. button = &style->tab.node_minimize_button;
  13707. nk_zero_struct(*button);
  13708. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13709. button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13710. button->active = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13711. button->border_color = nk_rgba(0,0,0,0);
  13712. button->text_background = table[NK_COLOR_TAB_HEADER];
  13713. button->text_normal = table[NK_COLOR_TEXT];
  13714. button->text_hover = table[NK_COLOR_TEXT];
  13715. button->text_active = table[NK_COLOR_TEXT];
  13716. button->padding = nk_vec2(2.0f,2.0f);
  13717. button->touch_padding = nk_vec2(0.0f,0.0f);
  13718. button->userdata = nk_handle_ptr(0);
  13719. button->text_alignment = NK_TEXT_CENTERED;
  13720. button->border = 0.0f;
  13721. button->rounding = 0.0f;
  13722. button->draw_begin = 0;
  13723. button->draw_end = 0;
  13724. style->tab.node_maximize_button =*button;
  13725. /* window header */
  13726. win = &style->window;
  13727. win->header.align = NK_HEADER_RIGHT;
  13728. win->header.close_symbol = NK_SYMBOL_X;
  13729. win->header.minimize_symbol = NK_SYMBOL_MINUS;
  13730. win->header.maximize_symbol = NK_SYMBOL_PLUS;
  13731. win->header.normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  13732. win->header.hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  13733. win->header.active = nk_style_item_color(table[NK_COLOR_HEADER]);
  13734. win->header.label_normal = table[NK_COLOR_TEXT];
  13735. win->header.label_hover = table[NK_COLOR_TEXT];
  13736. win->header.label_active = table[NK_COLOR_TEXT];
  13737. win->header.label_padding = nk_vec2(4,4);
  13738. win->header.padding = nk_vec2(4,4);
  13739. win->header.spacing = nk_vec2(0,0);
  13740. /* window header close button */
  13741. button = &style->window.header.close_button;
  13742. nk_zero_struct(*button);
  13743. button->normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  13744. button->hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  13745. button->active = nk_style_item_color(table[NK_COLOR_HEADER]);
  13746. button->border_color = nk_rgba(0,0,0,0);
  13747. button->text_background = table[NK_COLOR_HEADER];
  13748. button->text_normal = table[NK_COLOR_TEXT];
  13749. button->text_hover = table[NK_COLOR_TEXT];
  13750. button->text_active = table[NK_COLOR_TEXT];
  13751. button->padding = nk_vec2(0.0f,0.0f);
  13752. button->touch_padding = nk_vec2(0.0f,0.0f);
  13753. button->userdata = nk_handle_ptr(0);
  13754. button->text_alignment = NK_TEXT_CENTERED;
  13755. button->border = 0.0f;
  13756. button->rounding = 0.0f;
  13757. button->draw_begin = 0;
  13758. button->draw_end = 0;
  13759. /* window header minimize button */
  13760. button = &style->window.header.minimize_button;
  13761. nk_zero_struct(*button);
  13762. button->normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  13763. button->hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  13764. button->active = nk_style_item_color(table[NK_COLOR_HEADER]);
  13765. button->border_color = nk_rgba(0,0,0,0);
  13766. button->text_background = table[NK_COLOR_HEADER];
  13767. button->text_normal = table[NK_COLOR_TEXT];
  13768. button->text_hover = table[NK_COLOR_TEXT];
  13769. button->text_active = table[NK_COLOR_TEXT];
  13770. button->padding = nk_vec2(0.0f,0.0f);
  13771. button->touch_padding = nk_vec2(0.0f,0.0f);
  13772. button->userdata = nk_handle_ptr(0);
  13773. button->text_alignment = NK_TEXT_CENTERED;
  13774. button->border = 0.0f;
  13775. button->rounding = 0.0f;
  13776. button->draw_begin = 0;
  13777. button->draw_end = 0;
  13778. /* window */
  13779. win->background = table[NK_COLOR_WINDOW];
  13780. win->fixed_background = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13781. win->border_color = table[NK_COLOR_BORDER];
  13782. win->popup_border_color = table[NK_COLOR_BORDER];
  13783. win->combo_border_color = table[NK_COLOR_BORDER];
  13784. win->contextual_border_color = table[NK_COLOR_BORDER];
  13785. win->menu_border_color = table[NK_COLOR_BORDER];
  13786. win->group_border_color = table[NK_COLOR_BORDER];
  13787. win->tooltip_border_color = table[NK_COLOR_BORDER];
  13788. win->scaler = nk_style_item_color(table[NK_COLOR_TEXT]);
  13789. win->rounding = 0.0f;
  13790. win->spacing = nk_vec2(4,4);
  13791. win->scrollbar_size = nk_vec2(10,10);
  13792. win->min_size = nk_vec2(64,64);
  13793. win->combo_border = 1.0f;
  13794. win->contextual_border = 1.0f;
  13795. win->menu_border = 1.0f;
  13796. win->group_border = 1.0f;
  13797. win->tooltip_border = 1.0f;
  13798. win->popup_border = 1.0f;
  13799. win->border = 2.0f;
  13800. win->min_row_height_padding = 8;
  13801. win->padding = nk_vec2(4,4);
  13802. win->group_padding = nk_vec2(4,4);
  13803. win->popup_padding = nk_vec2(4,4);
  13804. win->combo_padding = nk_vec2(4,4);
  13805. win->contextual_padding = nk_vec2(4,4);
  13806. win->menu_padding = nk_vec2(4,4);
  13807. win->tooltip_padding = nk_vec2(4,4);
  13808. }
  13809. NK_API void
  13810. nk_style_set_font(struct nk_context *ctx, const struct nk_user_font *font)
  13811. {
  13812. struct nk_style *style;
  13813. NK_ASSERT(ctx);
  13814. if (!ctx) return;
  13815. style = &ctx->style;
  13816. style->font = font;
  13817. ctx->stacks.fonts.head = 0;
  13818. if (ctx->current)
  13819. nk_layout_reset_min_row_height(ctx);
  13820. }
  13821. NK_API int
  13822. nk_style_push_font(struct nk_context *ctx, const struct nk_user_font *font)
  13823. {
  13824. struct nk_config_stack_user_font *font_stack;
  13825. struct nk_config_stack_user_font_element *element;
  13826. NK_ASSERT(ctx);
  13827. if (!ctx) return 0;
  13828. font_stack = &ctx->stacks.fonts;
  13829. NK_ASSERT(font_stack->head < (int)NK_LEN(font_stack->elements));
  13830. if (font_stack->head >= (int)NK_LEN(font_stack->elements))
  13831. return 0;
  13832. element = &font_stack->elements[font_stack->head++];
  13833. element->address = &ctx->style.font;
  13834. element->old_value = ctx->style.font;
  13835. ctx->style.font = font;
  13836. return 1;
  13837. }
  13838. NK_API int
  13839. nk_style_pop_font(struct nk_context *ctx)
  13840. {
  13841. struct nk_config_stack_user_font *font_stack;
  13842. struct nk_config_stack_user_font_element *element;
  13843. NK_ASSERT(ctx);
  13844. if (!ctx) return 0;
  13845. font_stack = &ctx->stacks.fonts;
  13846. NK_ASSERT(font_stack->head > 0);
  13847. if (font_stack->head < 1)
  13848. return 0;
  13849. element = &font_stack->elements[--font_stack->head];
  13850. *element->address = element->old_value;
  13851. return 1;
  13852. }
  13853. #define NK_STYLE_PUSH_IMPLEMENATION(prefix, type, stack) \
  13854. nk_style_push_##type(struct nk_context *ctx, prefix##_##type *address, prefix##_##type value)\
  13855. {\
  13856. struct nk_config_stack_##type * type_stack;\
  13857. struct nk_config_stack_##type##_element *element;\
  13858. NK_ASSERT(ctx);\
  13859. if (!ctx) return 0;\
  13860. type_stack = &ctx->stacks.stack;\
  13861. NK_ASSERT(type_stack->head < (int)NK_LEN(type_stack->elements));\
  13862. if (type_stack->head >= (int)NK_LEN(type_stack->elements))\
  13863. return 0;\
  13864. element = &type_stack->elements[type_stack->head++];\
  13865. element->address = address;\
  13866. element->old_value = *address;\
  13867. *address = value;\
  13868. return 1;\
  13869. }
  13870. #define NK_STYLE_POP_IMPLEMENATION(type, stack) \
  13871. nk_style_pop_##type(struct nk_context *ctx)\
  13872. {\
  13873. struct nk_config_stack_##type *type_stack;\
  13874. struct nk_config_stack_##type##_element *element;\
  13875. NK_ASSERT(ctx);\
  13876. if (!ctx) return 0;\
  13877. type_stack = &ctx->stacks.stack;\
  13878. NK_ASSERT(type_stack->head > 0);\
  13879. if (type_stack->head < 1)\
  13880. return 0;\
  13881. element = &type_stack->elements[--type_stack->head];\
  13882. *element->address = element->old_value;\
  13883. return 1;\
  13884. }
  13885. NK_API int NK_STYLE_PUSH_IMPLEMENATION(struct nk, style_item, style_items)
  13886. NK_API int NK_STYLE_PUSH_IMPLEMENATION(nk,float, floats)
  13887. NK_API int NK_STYLE_PUSH_IMPLEMENATION(struct nk, vec2, vectors)
  13888. NK_API int NK_STYLE_PUSH_IMPLEMENATION(nk,flags, flags)
  13889. NK_API int NK_STYLE_PUSH_IMPLEMENATION(struct nk,color, colors)
  13890. NK_API int NK_STYLE_POP_IMPLEMENATION(style_item, style_items)
  13891. NK_API int NK_STYLE_POP_IMPLEMENATION(float,floats)
  13892. NK_API int NK_STYLE_POP_IMPLEMENATION(vec2, vectors)
  13893. NK_API int NK_STYLE_POP_IMPLEMENATION(flags,flags)
  13894. NK_API int NK_STYLE_POP_IMPLEMENATION(color,colors)
  13895. NK_API int
  13896. nk_style_set_cursor(struct nk_context *ctx, enum nk_style_cursor c)
  13897. {
  13898. struct nk_style *style;
  13899. NK_ASSERT(ctx);
  13900. if (!ctx) return 0;
  13901. style = &ctx->style;
  13902. if (style->cursors[c]) {
  13903. style->cursor_active = style->cursors[c];
  13904. return 1;
  13905. }
  13906. return 0;
  13907. }
  13908. NK_API void
  13909. nk_style_show_cursor(struct nk_context *ctx)
  13910. {
  13911. ctx->style.cursor_visible = nk_true;
  13912. }
  13913. NK_API void
  13914. nk_style_hide_cursor(struct nk_context *ctx)
  13915. {
  13916. ctx->style.cursor_visible = nk_false;
  13917. }
  13918. NK_API void
  13919. nk_style_load_cursor(struct nk_context *ctx, enum nk_style_cursor cursor,
  13920. const struct nk_cursor *c)
  13921. {
  13922. struct nk_style *style;
  13923. NK_ASSERT(ctx);
  13924. if (!ctx) return;
  13925. style = &ctx->style;
  13926. style->cursors[cursor] = c;
  13927. }
  13928. NK_API void
  13929. nk_style_load_all_cursors(struct nk_context *ctx, struct nk_cursor *cursors)
  13930. {
  13931. int i = 0;
  13932. struct nk_style *style;
  13933. NK_ASSERT(ctx);
  13934. if (!ctx) return;
  13935. style = &ctx->style;
  13936. for (i = 0; i < NK_CURSOR_COUNT; ++i)
  13937. style->cursors[i] = &cursors[i];
  13938. style->cursor_visible = nk_true;
  13939. }
  13940. /* ==============================================================
  13941. *
  13942. * CONTEXT
  13943. *
  13944. * ===============================================================*/
  13945. NK_INTERN void
  13946. nk_setup(struct nk_context *ctx, const struct nk_user_font *font)
  13947. {
  13948. NK_ASSERT(ctx);
  13949. if (!ctx) return;
  13950. nk_zero_struct(*ctx);
  13951. nk_style_default(ctx);
  13952. ctx->seq = 1;
  13953. if (font) ctx->style.font = font;
  13954. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  13955. nk_draw_list_init(&ctx->draw_list);
  13956. #endif
  13957. }
  13958. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  13959. NK_API int
  13960. nk_init_default(struct nk_context *ctx, const struct nk_user_font *font)
  13961. {
  13962. struct nk_allocator alloc;
  13963. alloc.userdata.ptr = 0;
  13964. alloc.alloc = nk_malloc;
  13965. alloc.free = nk_mfree;
  13966. return nk_init(ctx, &alloc, font);
  13967. }
  13968. #endif
  13969. NK_API int
  13970. nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size,
  13971. const struct nk_user_font *font)
  13972. {
  13973. NK_ASSERT(memory);
  13974. if (!memory) return 0;
  13975. nk_setup(ctx, font);
  13976. nk_buffer_init_fixed(&ctx->memory, memory, size);
  13977. ctx->use_pool = nk_false;
  13978. return 1;
  13979. }
  13980. NK_API int
  13981. nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds,
  13982. struct nk_buffer *pool, const struct nk_user_font *font)
  13983. {
  13984. NK_ASSERT(cmds);
  13985. NK_ASSERT(pool);
  13986. if (!cmds || !pool) return 0;
  13987. nk_setup(ctx, font);
  13988. ctx->memory = *cmds;
  13989. if (pool->type == NK_BUFFER_FIXED) {
  13990. /* take memory from buffer and alloc fixed pool */
  13991. nk_pool_init_fixed(&ctx->pool, pool->memory.ptr, pool->memory.size);
  13992. } else {
  13993. /* create dynamic pool from buffer allocator */
  13994. struct nk_allocator *alloc = &pool->pool;
  13995. nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
  13996. }
  13997. ctx->use_pool = nk_true;
  13998. return 1;
  13999. }
  14000. NK_API int
  14001. nk_init(struct nk_context *ctx, struct nk_allocator *alloc,
  14002. const struct nk_user_font *font)
  14003. {
  14004. NK_ASSERT(alloc);
  14005. if (!alloc) return 0;
  14006. nk_setup(ctx, font);
  14007. nk_buffer_init(&ctx->memory, alloc, NK_DEFAULT_COMMAND_BUFFER_SIZE);
  14008. nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
  14009. ctx->use_pool = nk_true;
  14010. return 1;
  14011. }
  14012. #ifdef NK_INCLUDE_COMMAND_USERDATA
  14013. NK_API void
  14014. nk_set_user_data(struct nk_context *ctx, nk_handle handle)
  14015. {
  14016. if (!ctx) return;
  14017. ctx->userdata = handle;
  14018. if (ctx->current)
  14019. ctx->current->buffer.userdata = handle;
  14020. }
  14021. #endif
  14022. NK_API void
  14023. nk_free(struct nk_context *ctx)
  14024. {
  14025. NK_ASSERT(ctx);
  14026. if (!ctx) return;
  14027. nk_buffer_free(&ctx->memory);
  14028. if (ctx->use_pool)
  14029. nk_pool_free(&ctx->pool);
  14030. nk_zero(&ctx->input, sizeof(ctx->input));
  14031. nk_zero(&ctx->style, sizeof(ctx->style));
  14032. nk_zero(&ctx->memory, sizeof(ctx->memory));
  14033. ctx->seq = 0;
  14034. ctx->build = 0;
  14035. ctx->begin = 0;
  14036. ctx->end = 0;
  14037. ctx->active = 0;
  14038. ctx->current = 0;
  14039. ctx->freelist = 0;
  14040. ctx->count = 0;
  14041. }
  14042. NK_API void
  14043. nk_clear(struct nk_context *ctx)
  14044. {
  14045. struct nk_window *iter;
  14046. struct nk_window *next;
  14047. NK_ASSERT(ctx);
  14048. if (!ctx) return;
  14049. if (ctx->use_pool)
  14050. nk_buffer_clear(&ctx->memory);
  14051. else nk_buffer_reset(&ctx->memory, NK_BUFFER_FRONT);
  14052. ctx->build = 0;
  14053. ctx->memory.calls = 0;
  14054. ctx->last_widget_state = 0;
  14055. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
  14056. NK_MEMSET(&ctx->overlay, 0, sizeof(ctx->overlay));
  14057. /* garbage collector */
  14058. iter = ctx->begin;
  14059. while (iter) {
  14060. /* make sure valid minimized windows do not get removed */
  14061. if ((iter->flags & NK_WINDOW_MINIMIZED) &&
  14062. !(iter->flags & NK_WINDOW_CLOSED) &&
  14063. iter->seq == ctx->seq) {
  14064. iter = iter->next;
  14065. continue;
  14066. }
  14067. /* remove hotness from hidden or closed windows*/
  14068. if (((iter->flags & NK_WINDOW_HIDDEN) ||
  14069. (iter->flags & NK_WINDOW_CLOSED)) &&
  14070. iter == ctx->active) {
  14071. ctx->active = iter->prev;
  14072. ctx->end = iter->prev;
  14073. if (!ctx->end)
  14074. ctx->begin = 0;
  14075. if (ctx->active)
  14076. ctx->active->flags &= ~(unsigned)NK_WINDOW_ROM;
  14077. }
  14078. /* free unused popup windows */
  14079. if (iter->popup.win && iter->popup.win->seq != ctx->seq) {
  14080. nk_free_window(ctx, iter->popup.win);
  14081. iter->popup.win = 0;
  14082. }
  14083. /* remove unused window state tables */
  14084. {struct nk_table *n, *it = iter->tables;
  14085. while (it) {
  14086. n = it->next;
  14087. if (it->seq != ctx->seq) {
  14088. nk_remove_table(iter, it);
  14089. nk_zero(it, sizeof(union nk_page_data));
  14090. nk_free_table(ctx, it);
  14091. if (it == iter->tables)
  14092. iter->tables = n;
  14093. } it = n;
  14094. }}
  14095. /* window itself is not used anymore so free */
  14096. if (iter->seq != ctx->seq || iter->flags & NK_WINDOW_CLOSED) {
  14097. next = iter->next;
  14098. nk_remove_window(ctx, iter);
  14099. nk_free_window(ctx, iter);
  14100. iter = next;
  14101. } else iter = iter->next;
  14102. }
  14103. ctx->seq++;
  14104. }
  14105. NK_LIB void
  14106. nk_start_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
  14107. {
  14108. NK_ASSERT(ctx);
  14109. NK_ASSERT(buffer);
  14110. if (!ctx || !buffer) return;
  14111. buffer->begin = ctx->memory.allocated;
  14112. buffer->end = buffer->begin;
  14113. buffer->last = buffer->begin;
  14114. buffer->clip = nk_null_rect;
  14115. }
  14116. NK_LIB void
  14117. nk_start(struct nk_context *ctx, struct nk_window *win)
  14118. {
  14119. NK_ASSERT(ctx);
  14120. NK_ASSERT(win);
  14121. nk_start_buffer(ctx, &win->buffer);
  14122. }
  14123. NK_LIB void
  14124. nk_start_popup(struct nk_context *ctx, struct nk_window *win)
  14125. {
  14126. struct nk_popup_buffer *buf;
  14127. NK_ASSERT(ctx);
  14128. NK_ASSERT(win);
  14129. if (!ctx || !win) return;
  14130. /* save buffer fill state for popup */
  14131. buf = &win->popup.buf;
  14132. buf->begin = win->buffer.end;
  14133. buf->end = win->buffer.end;
  14134. buf->parent = win->buffer.last;
  14135. buf->last = buf->begin;
  14136. buf->active = nk_true;
  14137. }
  14138. NK_LIB void
  14139. nk_finish_popup(struct nk_context *ctx, struct nk_window *win)
  14140. {
  14141. struct nk_popup_buffer *buf;
  14142. NK_ASSERT(ctx);
  14143. NK_ASSERT(win);
  14144. if (!ctx || !win) return;
  14145. buf = &win->popup.buf;
  14146. buf->last = win->buffer.last;
  14147. buf->end = win->buffer.end;
  14148. }
  14149. NK_LIB void
  14150. nk_finish_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
  14151. {
  14152. NK_ASSERT(ctx);
  14153. NK_ASSERT(buffer);
  14154. if (!ctx || !buffer) return;
  14155. buffer->end = ctx->memory.allocated;
  14156. }
  14157. NK_LIB void
  14158. nk_finish(struct nk_context *ctx, struct nk_window *win)
  14159. {
  14160. struct nk_popup_buffer *buf;
  14161. struct nk_command *parent_last;
  14162. void *memory;
  14163. NK_ASSERT(ctx);
  14164. NK_ASSERT(win);
  14165. if (!ctx || !win) return;
  14166. nk_finish_buffer(ctx, &win->buffer);
  14167. if (!win->popup.buf.active) return;
  14168. buf = &win->popup.buf;
  14169. memory = ctx->memory.memory.ptr;
  14170. parent_last = nk_ptr_add(struct nk_command, memory, buf->parent);
  14171. parent_last->next = buf->end;
  14172. }
  14173. NK_LIB void
  14174. nk_build(struct nk_context *ctx)
  14175. {
  14176. struct nk_window *it = 0;
  14177. struct nk_command *cmd = 0;
  14178. nk_byte *buffer = 0;
  14179. /* draw cursor overlay */
  14180. if (!ctx->style.cursor_active)
  14181. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
  14182. if (ctx->style.cursor_active && !ctx->input.mouse.grabbed && ctx->style.cursor_visible) {
  14183. struct nk_rect mouse_bounds;
  14184. const struct nk_cursor *cursor = ctx->style.cursor_active;
  14185. nk_command_buffer_init(&ctx->overlay, &ctx->memory, NK_CLIPPING_OFF);
  14186. nk_start_buffer(ctx, &ctx->overlay);
  14187. mouse_bounds.x = ctx->input.mouse.pos.x - cursor->offset.x;
  14188. mouse_bounds.y = ctx->input.mouse.pos.y - cursor->offset.y;
  14189. mouse_bounds.w = cursor->size.x;
  14190. mouse_bounds.h = cursor->size.y;
  14191. nk_draw_image(&ctx->overlay, mouse_bounds, &cursor->img, nk_white);
  14192. nk_finish_buffer(ctx, &ctx->overlay);
  14193. }
  14194. /* build one big draw command list out of all window buffers */
  14195. it = ctx->begin;
  14196. buffer = (nk_byte*)ctx->memory.memory.ptr;
  14197. while (it != 0) {
  14198. struct nk_window *next = it->next;
  14199. if (it->buffer.last == it->buffer.begin || (it->flags & NK_WINDOW_HIDDEN)||
  14200. it->seq != ctx->seq)
  14201. goto cont;
  14202. cmd = nk_ptr_add(struct nk_command, buffer, it->buffer.last);
  14203. while (next && ((next->buffer.last == next->buffer.begin) ||
  14204. (next->flags & NK_WINDOW_HIDDEN) || next->seq != ctx->seq))
  14205. next = next->next; /* skip empty command buffers */
  14206. if (next) cmd->next = next->buffer.begin;
  14207. cont: it = next;
  14208. }
  14209. /* append all popup draw commands into lists */
  14210. it = ctx->begin;
  14211. while (it != 0) {
  14212. struct nk_window *next = it->next;
  14213. struct nk_popup_buffer *buf;
  14214. if (!it->popup.buf.active)
  14215. goto skip;
  14216. buf = &it->popup.buf;
  14217. cmd->next = buf->begin;
  14218. cmd = nk_ptr_add(struct nk_command, buffer, buf->last);
  14219. buf->active = nk_false;
  14220. skip: it = next;
  14221. }
  14222. if (cmd) {
  14223. /* append overlay commands */
  14224. if (ctx->overlay.end != ctx->overlay.begin)
  14225. cmd->next = ctx->overlay.begin;
  14226. else cmd->next = ctx->memory.allocated;
  14227. }
  14228. }
  14229. NK_API const struct nk_command*
  14230. nk__begin(struct nk_context *ctx)
  14231. {
  14232. struct nk_window *iter;
  14233. nk_byte *buffer;
  14234. NK_ASSERT(ctx);
  14235. if (!ctx) return 0;
  14236. if (!ctx->count) return 0;
  14237. buffer = (nk_byte*)ctx->memory.memory.ptr;
  14238. if (!ctx->build) {
  14239. nk_build(ctx);
  14240. ctx->build = nk_true;
  14241. }
  14242. iter = ctx->begin;
  14243. while (iter && ((iter->buffer.begin == iter->buffer.end) ||
  14244. (iter->flags & NK_WINDOW_HIDDEN) || iter->seq != ctx->seq))
  14245. iter = iter->next;
  14246. if (!iter) return 0;
  14247. return nk_ptr_add_const(struct nk_command, buffer, iter->buffer.begin);
  14248. }
  14249. NK_API const struct nk_command*
  14250. nk__next(struct nk_context *ctx, const struct nk_command *cmd)
  14251. {
  14252. nk_byte *buffer;
  14253. const struct nk_command *next;
  14254. NK_ASSERT(ctx);
  14255. if (!ctx || !cmd || !ctx->count) return 0;
  14256. if (cmd->next >= ctx->memory.allocated) return 0;
  14257. buffer = (nk_byte*)ctx->memory.memory.ptr;
  14258. next = nk_ptr_add_const(struct nk_command, buffer, cmd->next);
  14259. return next;
  14260. }
  14261. /* ===============================================================
  14262. *
  14263. * POOL
  14264. *
  14265. * ===============================================================*/
  14266. NK_LIB void
  14267. nk_pool_init(struct nk_pool *pool, struct nk_allocator *alloc,
  14268. unsigned int capacity)
  14269. {
  14270. nk_zero(pool, sizeof(*pool));
  14271. pool->alloc = *alloc;
  14272. pool->capacity = capacity;
  14273. pool->type = NK_BUFFER_DYNAMIC;
  14274. pool->pages = 0;
  14275. }
  14276. NK_LIB void
  14277. nk_pool_free(struct nk_pool *pool)
  14278. {
  14279. struct nk_page *iter = pool->pages;
  14280. if (!pool) return;
  14281. if (pool->type == NK_BUFFER_FIXED) return;
  14282. while (iter) {
  14283. struct nk_page *next = iter->next;
  14284. pool->alloc.free(pool->alloc.userdata, iter);
  14285. iter = next;
  14286. }
  14287. }
  14288. NK_LIB void
  14289. nk_pool_init_fixed(struct nk_pool *pool, void *memory, nk_size size)
  14290. {
  14291. nk_zero(pool, sizeof(*pool));
  14292. NK_ASSERT(size >= sizeof(struct nk_page));
  14293. if (size < sizeof(struct nk_page)) return;
  14294. pool->capacity = (unsigned)(size - sizeof(struct nk_page)) / sizeof(struct nk_page_element);
  14295. pool->pages = (struct nk_page*)memory;
  14296. pool->type = NK_BUFFER_FIXED;
  14297. pool->size = size;
  14298. }
  14299. NK_LIB struct nk_page_element*
  14300. nk_pool_alloc(struct nk_pool *pool)
  14301. {
  14302. if (!pool->pages || pool->pages->size >= pool->capacity) {
  14303. /* allocate new page */
  14304. struct nk_page *page;
  14305. if (pool->type == NK_BUFFER_FIXED) {
  14306. NK_ASSERT(pool->pages);
  14307. if (!pool->pages) return 0;
  14308. NK_ASSERT(pool->pages->size < pool->capacity);
  14309. return 0;
  14310. } else {
  14311. nk_size size = sizeof(struct nk_page);
  14312. size += NK_POOL_DEFAULT_CAPACITY * sizeof(union nk_page_data);
  14313. page = (struct nk_page*)pool->alloc.alloc(pool->alloc.userdata,0, size);
  14314. page->next = pool->pages;
  14315. pool->pages = page;
  14316. page->size = 0;
  14317. }
  14318. } return &pool->pages->win[pool->pages->size++];
  14319. }
  14320. /* ===============================================================
  14321. *
  14322. * PAGE ELEMENT
  14323. *
  14324. * ===============================================================*/
  14325. NK_LIB struct nk_page_element*
  14326. nk_create_page_element(struct nk_context *ctx)
  14327. {
  14328. struct nk_page_element *elem;
  14329. if (ctx->freelist) {
  14330. /* unlink page element from free list */
  14331. elem = ctx->freelist;
  14332. ctx->freelist = elem->next;
  14333. } else if (ctx->use_pool) {
  14334. /* allocate page element from memory pool */
  14335. elem = nk_pool_alloc(&ctx->pool);
  14336. NK_ASSERT(elem);
  14337. if (!elem) return 0;
  14338. } else {
  14339. /* allocate new page element from back of fixed size memory buffer */
  14340. NK_STORAGE const nk_size size = sizeof(struct nk_page_element);
  14341. NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_page_element);
  14342. elem = (struct nk_page_element*)nk_buffer_alloc(&ctx->memory, NK_BUFFER_BACK, size, align);
  14343. NK_ASSERT(elem);
  14344. if (!elem) return 0;
  14345. }
  14346. nk_zero_struct(*elem);
  14347. elem->next = 0;
  14348. elem->prev = 0;
  14349. return elem;
  14350. }
  14351. NK_LIB void
  14352. nk_link_page_element_into_freelist(struct nk_context *ctx,
  14353. struct nk_page_element *elem)
  14354. {
  14355. /* link table into freelist */
  14356. if (!ctx->freelist) {
  14357. ctx->freelist = elem;
  14358. } else {
  14359. elem->next = ctx->freelist;
  14360. ctx->freelist = elem;
  14361. }
  14362. }
  14363. NK_LIB void
  14364. nk_free_page_element(struct nk_context *ctx, struct nk_page_element *elem)
  14365. {
  14366. /* we have a pool so just add to free list */
  14367. if (ctx->use_pool) {
  14368. nk_link_page_element_into_freelist(ctx, elem);
  14369. return;
  14370. }
  14371. /* if possible remove last element from back of fixed memory buffer */
  14372. {void *elem_end = (void*)(elem + 1);
  14373. void *buffer_end = (nk_byte*)ctx->memory.memory.ptr + ctx->memory.size;
  14374. if (elem_end == buffer_end)
  14375. ctx->memory.size -= sizeof(struct nk_page_element);
  14376. else nk_link_page_element_into_freelist(ctx, elem);}
  14377. }
  14378. /* ===============================================================
  14379. *
  14380. * TABLE
  14381. *
  14382. * ===============================================================*/
  14383. NK_LIB struct nk_table*
  14384. nk_create_table(struct nk_context *ctx)
  14385. {
  14386. struct nk_page_element *elem;
  14387. elem = nk_create_page_element(ctx);
  14388. if (!elem) return 0;
  14389. nk_zero_struct(*elem);
  14390. return &elem->data.tbl;
  14391. }
  14392. NK_LIB void
  14393. nk_free_table(struct nk_context *ctx, struct nk_table *tbl)
  14394. {
  14395. union nk_page_data *pd = NK_CONTAINER_OF(tbl, union nk_page_data, tbl);
  14396. struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  14397. nk_free_page_element(ctx, pe);
  14398. }
  14399. NK_LIB void
  14400. nk_push_table(struct nk_window *win, struct nk_table *tbl)
  14401. {
  14402. if (!win->tables) {
  14403. win->tables = tbl;
  14404. tbl->next = 0;
  14405. tbl->prev = 0;
  14406. tbl->size = 0;
  14407. win->table_count = 1;
  14408. return;
  14409. }
  14410. win->tables->prev = tbl;
  14411. tbl->next = win->tables;
  14412. tbl->prev = 0;
  14413. tbl->size = 0;
  14414. win->tables = tbl;
  14415. win->table_count++;
  14416. }
  14417. NK_LIB void
  14418. nk_remove_table(struct nk_window *win, struct nk_table *tbl)
  14419. {
  14420. if (win->tables == tbl)
  14421. win->tables = tbl->next;
  14422. if (tbl->next)
  14423. tbl->next->prev = tbl->prev;
  14424. if (tbl->prev)
  14425. tbl->prev->next = tbl->next;
  14426. tbl->next = 0;
  14427. tbl->prev = 0;
  14428. }
  14429. NK_LIB nk_uint*
  14430. nk_add_value(struct nk_context *ctx, struct nk_window *win,
  14431. nk_hash name, nk_uint value)
  14432. {
  14433. NK_ASSERT(ctx);
  14434. NK_ASSERT(win);
  14435. if (!win || !ctx) return 0;
  14436. if (!win->tables || win->tables->size >= NK_VALUE_PAGE_CAPACITY) {
  14437. struct nk_table *tbl = nk_create_table(ctx);
  14438. NK_ASSERT(tbl);
  14439. if (!tbl) return 0;
  14440. nk_push_table(win, tbl);
  14441. }
  14442. win->tables->seq = win->seq;
  14443. win->tables->keys[win->tables->size] = name;
  14444. win->tables->values[win->tables->size] = value;
  14445. return &win->tables->values[win->tables->size++];
  14446. }
  14447. NK_LIB nk_uint*
  14448. nk_find_value(struct nk_window *win, nk_hash name)
  14449. {
  14450. struct nk_table *iter = win->tables;
  14451. while (iter) {
  14452. unsigned int i = 0;
  14453. unsigned int size = iter->size;
  14454. for (i = 0; i < size; ++i) {
  14455. if (iter->keys[i] == name) {
  14456. iter->seq = win->seq;
  14457. return &iter->values[i];
  14458. }
  14459. } size = NK_VALUE_PAGE_CAPACITY;
  14460. iter = iter->next;
  14461. }
  14462. return 0;
  14463. }
  14464. /* ===============================================================
  14465. *
  14466. * PANEL
  14467. *
  14468. * ===============================================================*/
  14469. NK_LIB void*
  14470. nk_create_panel(struct nk_context *ctx)
  14471. {
  14472. struct nk_page_element *elem;
  14473. elem = nk_create_page_element(ctx);
  14474. if (!elem) return 0;
  14475. nk_zero_struct(*elem);
  14476. return &elem->data.pan;
  14477. }
  14478. NK_LIB void
  14479. nk_free_panel(struct nk_context *ctx, struct nk_panel *pan)
  14480. {
  14481. union nk_page_data *pd = NK_CONTAINER_OF(pan, union nk_page_data, pan);
  14482. struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  14483. nk_free_page_element(ctx, pe);
  14484. }
  14485. NK_LIB int
  14486. nk_panel_has_header(nk_flags flags, const char *title)
  14487. {
  14488. int active = 0;
  14489. active = (flags & (NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE));
  14490. active = active || (flags & NK_WINDOW_TITLE);
  14491. active = active && !(flags & NK_WINDOW_HIDDEN) && title;
  14492. return active;
  14493. }
  14494. NK_LIB struct nk_vec2
  14495. nk_panel_get_padding(const struct nk_style *style, enum nk_panel_type type)
  14496. {
  14497. switch (type) {
  14498. default:
  14499. case NK_PANEL_WINDOW: return style->window.padding;
  14500. case NK_PANEL_GROUP: return style->window.group_padding;
  14501. case NK_PANEL_POPUP: return style->window.popup_padding;
  14502. case NK_PANEL_CONTEXTUAL: return style->window.contextual_padding;
  14503. case NK_PANEL_COMBO: return style->window.combo_padding;
  14504. case NK_PANEL_MENU: return style->window.menu_padding;
  14505. case NK_PANEL_TOOLTIP: return style->window.menu_padding;}
  14506. }
  14507. NK_LIB float
  14508. nk_panel_get_border(const struct nk_style *style, nk_flags flags,
  14509. enum nk_panel_type type)
  14510. {
  14511. if (flags & NK_WINDOW_BORDER) {
  14512. switch (type) {
  14513. default:
  14514. case NK_PANEL_WINDOW: return style->window.border;
  14515. case NK_PANEL_GROUP: return style->window.group_border;
  14516. case NK_PANEL_POPUP: return style->window.popup_border;
  14517. case NK_PANEL_CONTEXTUAL: return style->window.contextual_border;
  14518. case NK_PANEL_COMBO: return style->window.combo_border;
  14519. case NK_PANEL_MENU: return style->window.menu_border;
  14520. case NK_PANEL_TOOLTIP: return style->window.menu_border;
  14521. }} else return 0;
  14522. }
  14523. NK_LIB struct nk_color
  14524. nk_panel_get_border_color(const struct nk_style *style, enum nk_panel_type type)
  14525. {
  14526. switch (type) {
  14527. default:
  14528. case NK_PANEL_WINDOW: return style->window.border_color;
  14529. case NK_PANEL_GROUP: return style->window.group_border_color;
  14530. case NK_PANEL_POPUP: return style->window.popup_border_color;
  14531. case NK_PANEL_CONTEXTUAL: return style->window.contextual_border_color;
  14532. case NK_PANEL_COMBO: return style->window.combo_border_color;
  14533. case NK_PANEL_MENU: return style->window.menu_border_color;
  14534. case NK_PANEL_TOOLTIP: return style->window.menu_border_color;}
  14535. }
  14536. NK_LIB int
  14537. nk_panel_is_sub(enum nk_panel_type type)
  14538. {
  14539. return (type & NK_PANEL_SET_SUB)?1:0;
  14540. }
  14541. NK_LIB int
  14542. nk_panel_is_nonblock(enum nk_panel_type type)
  14543. {
  14544. return (type & NK_PANEL_SET_NONBLOCK)?1:0;
  14545. }
  14546. NK_LIB int
  14547. nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type panel_type)
  14548. {
  14549. struct nk_input *in;
  14550. struct nk_window *win;
  14551. struct nk_panel *layout;
  14552. struct nk_command_buffer *out;
  14553. const struct nk_style *style;
  14554. const struct nk_user_font *font;
  14555. struct nk_vec2 scrollbar_size;
  14556. struct nk_vec2 panel_padding;
  14557. NK_ASSERT(ctx);
  14558. NK_ASSERT(ctx->current);
  14559. NK_ASSERT(ctx->current->layout);
  14560. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  14561. nk_zero(ctx->current->layout, sizeof(*ctx->current->layout));
  14562. if ((ctx->current->flags & NK_WINDOW_HIDDEN) || (ctx->current->flags & NK_WINDOW_CLOSED)) {
  14563. nk_zero(ctx->current->layout, sizeof(struct nk_panel));
  14564. ctx->current->layout->type = panel_type;
  14565. return 0;
  14566. }
  14567. /* pull state into local stack */
  14568. style = &ctx->style;
  14569. font = style->font;
  14570. win = ctx->current;
  14571. layout = win->layout;
  14572. out = &win->buffer;
  14573. in = (win->flags & NK_WINDOW_NO_INPUT) ? 0: &ctx->input;
  14574. #ifdef NK_INCLUDE_COMMAND_USERDATA
  14575. win->buffer.userdata = ctx->userdata;
  14576. #endif
  14577. /* pull style configuration into local stack */
  14578. scrollbar_size = style->window.scrollbar_size;
  14579. panel_padding = nk_panel_get_padding(style, panel_type);
  14580. /* window movement */
  14581. if ((win->flags & NK_WINDOW_MOVABLE) && !(win->flags & NK_WINDOW_ROM)) {
  14582. int left_mouse_down;
  14583. int left_mouse_clicked;
  14584. int left_mouse_click_in_cursor;
  14585. /* calculate draggable window space */
  14586. struct nk_rect header;
  14587. header.x = win->bounds.x;
  14588. header.y = win->bounds.y;
  14589. header.w = win->bounds.w;
  14590. if (nk_panel_has_header(win->flags, title)) {
  14591. header.h = font->height + 2.0f * style->window.header.padding.y;
  14592. header.h += 2.0f * style->window.header.label_padding.y;
  14593. } else header.h = panel_padding.y;
  14594. /* window movement by dragging */
  14595. left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  14596. left_mouse_clicked = (int)in->mouse.buttons[NK_BUTTON_LEFT].clicked;
  14597. left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
  14598. NK_BUTTON_LEFT, header, nk_true);
  14599. if (left_mouse_down && left_mouse_click_in_cursor && !left_mouse_clicked) {
  14600. win->bounds.x = win->bounds.x + in->mouse.delta.x;
  14601. win->bounds.y = win->bounds.y + in->mouse.delta.y;
  14602. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x += in->mouse.delta.x;
  14603. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y += in->mouse.delta.y;
  14604. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_MOVE];
  14605. }
  14606. }
  14607. /* setup panel */
  14608. layout->type = panel_type;
  14609. layout->flags = win->flags;
  14610. layout->bounds = win->bounds;
  14611. layout->bounds.x += panel_padding.x;
  14612. layout->bounds.w -= 2*panel_padding.x;
  14613. if (win->flags & NK_WINDOW_BORDER) {
  14614. layout->border = nk_panel_get_border(style, win->flags, panel_type);
  14615. layout->bounds = nk_shrink_rect(layout->bounds, layout->border);
  14616. } else layout->border = 0;
  14617. layout->at_y = layout->bounds.y;
  14618. layout->at_x = layout->bounds.x;
  14619. layout->max_x = 0;
  14620. layout->header_height = 0;
  14621. layout->footer_height = 0;
  14622. nk_layout_reset_min_row_height(ctx);
  14623. layout->row.index = 0;
  14624. layout->row.columns = 0;
  14625. layout->row.ratio = 0;
  14626. layout->row.item_width = 0;
  14627. layout->row.tree_depth = 0;
  14628. layout->row.height = panel_padding.y;
  14629. layout->has_scrolling = nk_true;
  14630. if (!(win->flags & NK_WINDOW_NO_SCROLLBAR))
  14631. layout->bounds.w -= scrollbar_size.x;
  14632. if (!nk_panel_is_nonblock(panel_type)) {
  14633. layout->footer_height = 0;
  14634. if (!(win->flags & NK_WINDOW_NO_SCROLLBAR) || win->flags & NK_WINDOW_SCALABLE)
  14635. layout->footer_height = scrollbar_size.y;
  14636. layout->bounds.h -= layout->footer_height;
  14637. }
  14638. /* panel header */
  14639. if (nk_panel_has_header(win->flags, title))
  14640. {
  14641. struct nk_text text;
  14642. struct nk_rect header;
  14643. const struct nk_style_item *background = 0;
  14644. /* calculate header bounds */
  14645. header.x = win->bounds.x;
  14646. header.y = win->bounds.y;
  14647. header.w = win->bounds.w;
  14648. header.h = font->height + 2.0f * style->window.header.padding.y;
  14649. header.h += (2.0f * style->window.header.label_padding.y);
  14650. /* shrink panel by header */
  14651. layout->header_height = header.h;
  14652. layout->bounds.y += header.h;
  14653. layout->bounds.h -= header.h;
  14654. layout->at_y += header.h;
  14655. /* select correct header background and text color */
  14656. if (ctx->active == win) {
  14657. background = &style->window.header.active;
  14658. text.text = style->window.header.label_active;
  14659. } else if (nk_input_is_mouse_hovering_rect(&ctx->input, header)) {
  14660. background = &style->window.header.hover;
  14661. text.text = style->window.header.label_hover;
  14662. } else {
  14663. background = &style->window.header.normal;
  14664. text.text = style->window.header.label_normal;
  14665. }
  14666. /* draw header background */
  14667. header.h += 1.0f;
  14668. if (background->type == NK_STYLE_ITEM_IMAGE) {
  14669. text.background = nk_rgba(0,0,0,0);
  14670. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  14671. } else {
  14672. text.background = background->data.color;
  14673. nk_fill_rect(out, header, 0, background->data.color);
  14674. }
  14675. /* window close button */
  14676. {struct nk_rect button;
  14677. button.y = header.y + style->window.header.padding.y;
  14678. button.h = header.h - 2 * style->window.header.padding.y;
  14679. button.w = button.h;
  14680. if (win->flags & NK_WINDOW_CLOSABLE) {
  14681. nk_flags ws = 0;
  14682. if (style->window.header.align == NK_HEADER_RIGHT) {
  14683. button.x = (header.w + header.x) - (button.w + style->window.header.padding.x);
  14684. header.w -= button.w + style->window.header.spacing.x + style->window.header.padding.x;
  14685. } else {
  14686. button.x = header.x + style->window.header.padding.x;
  14687. header.x += button.w + style->window.header.spacing.x + style->window.header.padding.x;
  14688. }
  14689. if (nk_do_button_symbol(&ws, &win->buffer, button,
  14690. style->window.header.close_symbol, NK_BUTTON_DEFAULT,
  14691. &style->window.header.close_button, in, style->font) && !(win->flags & NK_WINDOW_ROM))
  14692. {
  14693. layout->flags |= NK_WINDOW_HIDDEN;
  14694. layout->flags &= (nk_flags)~NK_WINDOW_MINIMIZED;
  14695. }
  14696. }
  14697. /* window minimize button */
  14698. if (win->flags & NK_WINDOW_MINIMIZABLE) {
  14699. nk_flags ws = 0;
  14700. if (style->window.header.align == NK_HEADER_RIGHT) {
  14701. button.x = (header.w + header.x) - button.w;
  14702. if (!(win->flags & NK_WINDOW_CLOSABLE)) {
  14703. button.x -= style->window.header.padding.x;
  14704. header.w -= style->window.header.padding.x;
  14705. }
  14706. header.w -= button.w + style->window.header.spacing.x;
  14707. } else {
  14708. button.x = header.x;
  14709. header.x += button.w + style->window.header.spacing.x + style->window.header.padding.x;
  14710. }
  14711. if (nk_do_button_symbol(&ws, &win->buffer, button, (layout->flags & NK_WINDOW_MINIMIZED)?
  14712. style->window.header.maximize_symbol: style->window.header.minimize_symbol,
  14713. NK_BUTTON_DEFAULT, &style->window.header.minimize_button, in, style->font) && !(win->flags & NK_WINDOW_ROM))
  14714. layout->flags = (layout->flags & NK_WINDOW_MINIMIZED) ?
  14715. layout->flags & (nk_flags)~NK_WINDOW_MINIMIZED:
  14716. layout->flags | NK_WINDOW_MINIMIZED;
  14717. }}
  14718. {/* window header title */
  14719. int text_len = nk_strlen(title);
  14720. struct nk_rect label = {0,0,0,0};
  14721. float t = font->width(font->userdata, font->height, title, text_len);
  14722. text.padding = nk_vec2(0,0);
  14723. label.x = header.x + style->window.header.padding.x;
  14724. label.x += style->window.header.label_padding.x;
  14725. label.y = header.y + style->window.header.label_padding.y;
  14726. label.h = font->height + 2 * style->window.header.label_padding.y;
  14727. label.w = t + 2 * style->window.header.spacing.x;
  14728. label.w = NK_CLAMP(0, label.w, header.x + header.w - label.x);
  14729. nk_widget_text(out, label,(const char*)title, text_len, &text, NK_TEXT_LEFT, font);}
  14730. }
  14731. /* draw window background */
  14732. if (!(layout->flags & NK_WINDOW_MINIMIZED) && !(layout->flags & NK_WINDOW_DYNAMIC)) {
  14733. struct nk_rect body;
  14734. body.x = win->bounds.x;
  14735. body.w = win->bounds.w;
  14736. body.y = (win->bounds.y + layout->header_height);
  14737. body.h = (win->bounds.h - layout->header_height);
  14738. if (style->window.fixed_background.type == NK_STYLE_ITEM_IMAGE)
  14739. nk_draw_image(out, body, &style->window.fixed_background.data.image, nk_white);
  14740. else nk_fill_rect(out, body, 0, style->window.fixed_background.data.color);
  14741. }
  14742. /* set clipping rectangle */
  14743. {struct nk_rect clip;
  14744. layout->clip = layout->bounds;
  14745. nk_unify(&clip, &win->buffer.clip, layout->clip.x, layout->clip.y,
  14746. layout->clip.x + layout->clip.w, layout->clip.y + layout->clip.h);
  14747. nk_push_scissor(out, clip);
  14748. layout->clip = clip;}
  14749. return !(layout->flags & NK_WINDOW_HIDDEN) && !(layout->flags & NK_WINDOW_MINIMIZED);
  14750. }
  14751. NK_LIB void
  14752. nk_panel_end(struct nk_context *ctx)
  14753. {
  14754. struct nk_input *in;
  14755. struct nk_window *window;
  14756. struct nk_panel *layout;
  14757. const struct nk_style *style;
  14758. struct nk_command_buffer *out;
  14759. struct nk_vec2 scrollbar_size;
  14760. struct nk_vec2 panel_padding;
  14761. NK_ASSERT(ctx);
  14762. NK_ASSERT(ctx->current);
  14763. NK_ASSERT(ctx->current->layout);
  14764. if (!ctx || !ctx->current || !ctx->current->layout)
  14765. return;
  14766. window = ctx->current;
  14767. layout = window->layout;
  14768. style = &ctx->style;
  14769. out = &window->buffer;
  14770. in = (layout->flags & NK_WINDOW_ROM || layout->flags & NK_WINDOW_NO_INPUT) ? 0 :&ctx->input;
  14771. if (!nk_panel_is_sub(layout->type))
  14772. nk_push_scissor(out, nk_null_rect);
  14773. /* cache configuration data */
  14774. scrollbar_size = style->window.scrollbar_size;
  14775. panel_padding = nk_panel_get_padding(style, layout->type);
  14776. /* update the current cursor Y-position to point over the last added widget */
  14777. layout->at_y += layout->row.height;
  14778. /* dynamic panels */
  14779. if (layout->flags & NK_WINDOW_DYNAMIC && !(layout->flags & NK_WINDOW_MINIMIZED))
  14780. {
  14781. /* update panel height to fit dynamic growth */
  14782. struct nk_rect empty_space;
  14783. if (layout->at_y < (layout->bounds.y + layout->bounds.h))
  14784. layout->bounds.h = layout->at_y - layout->bounds.y;
  14785. /* fill top empty space */
  14786. empty_space.x = window->bounds.x;
  14787. empty_space.y = layout->bounds.y;
  14788. empty_space.h = panel_padding.y;
  14789. empty_space.w = window->bounds.w;
  14790. nk_fill_rect(out, empty_space, 0, style->window.background);
  14791. /* fill left empty space */
  14792. empty_space.x = window->bounds.x;
  14793. empty_space.y = layout->bounds.y;
  14794. empty_space.w = panel_padding.x + layout->border;
  14795. empty_space.h = layout->bounds.h;
  14796. nk_fill_rect(out, empty_space, 0, style->window.background);
  14797. /* fill right empty space */
  14798. empty_space.x = layout->bounds.x + layout->bounds.w - layout->border;
  14799. empty_space.y = layout->bounds.y;
  14800. empty_space.w = panel_padding.x + layout->border;
  14801. empty_space.h = layout->bounds.h;
  14802. if (*layout->offset_y == 0 && !(layout->flags & NK_WINDOW_NO_SCROLLBAR))
  14803. empty_space.w += scrollbar_size.x;
  14804. nk_fill_rect(out, empty_space, 0, style->window.background);
  14805. /* fill bottom empty space */
  14806. if (*layout->offset_x != 0 && !(layout->flags & NK_WINDOW_NO_SCROLLBAR)) {
  14807. empty_space.x = window->bounds.x;
  14808. empty_space.y = layout->bounds.y + layout->bounds.h;
  14809. empty_space.w = window->bounds.w;
  14810. empty_space.h = scrollbar_size.y;
  14811. nk_fill_rect(out, empty_space, 0, style->window.background);
  14812. }
  14813. }
  14814. /* scrollbars */
  14815. if (!(layout->flags & NK_WINDOW_NO_SCROLLBAR) &&
  14816. !(layout->flags & NK_WINDOW_MINIMIZED) &&
  14817. window->scrollbar_hiding_timer < NK_SCROLLBAR_HIDING_TIMEOUT)
  14818. {
  14819. struct nk_rect scroll;
  14820. int scroll_has_scrolling;
  14821. float scroll_target;
  14822. float scroll_offset;
  14823. float scroll_step;
  14824. float scroll_inc;
  14825. /* mouse wheel scrolling */
  14826. if (nk_panel_is_sub(layout->type))
  14827. {
  14828. /* sub-window mouse wheel scrolling */
  14829. struct nk_window *root_window = window;
  14830. struct nk_panel *root_panel = window->layout;
  14831. while (root_panel->parent)
  14832. root_panel = root_panel->parent;
  14833. while (root_window->parent)
  14834. root_window = root_window->parent;
  14835. /* only allow scrolling if parent window is active */
  14836. scroll_has_scrolling = 0;
  14837. if ((root_window == ctx->active) && layout->has_scrolling) {
  14838. /* and panel is being hovered and inside clip rect*/
  14839. if (nk_input_is_mouse_hovering_rect(in, layout->bounds) &&
  14840. NK_INTERSECT(layout->bounds.x, layout->bounds.y, layout->bounds.w, layout->bounds.h,
  14841. root_panel->clip.x, root_panel->clip.y, root_panel->clip.w, root_panel->clip.h))
  14842. {
  14843. /* deactivate all parent scrolling */
  14844. root_panel = window->layout;
  14845. while (root_panel->parent) {
  14846. root_panel->has_scrolling = nk_false;
  14847. root_panel = root_panel->parent;
  14848. }
  14849. root_panel->has_scrolling = nk_false;
  14850. scroll_has_scrolling = nk_true;
  14851. }
  14852. }
  14853. } else if (!nk_panel_is_sub(layout->type)) {
  14854. /* window mouse wheel scrolling */
  14855. scroll_has_scrolling = (window == ctx->active) && layout->has_scrolling;
  14856. if (in && (in->mouse.scroll_delta.y > 0 || in->mouse.scroll_delta.x > 0) && scroll_has_scrolling)
  14857. window->scrolled = nk_true;
  14858. else window->scrolled = nk_false;
  14859. } else scroll_has_scrolling = nk_false;
  14860. {
  14861. /* vertical scrollbar */
  14862. nk_flags state = 0;
  14863. scroll.x = layout->bounds.x + layout->bounds.w + panel_padding.x;
  14864. scroll.y = layout->bounds.y;
  14865. scroll.w = scrollbar_size.x;
  14866. scroll.h = layout->bounds.h;
  14867. scroll_offset = (float)*layout->offset_y;
  14868. scroll_step = scroll.h * 0.10f;
  14869. scroll_inc = scroll.h * 0.01f;
  14870. scroll_target = (float)(int)(layout->at_y - scroll.y);
  14871. scroll_offset = nk_do_scrollbarv(&state, out, scroll, scroll_has_scrolling,
  14872. scroll_offset, scroll_target, scroll_step, scroll_inc,
  14873. &ctx->style.scrollv, in, style->font);
  14874. *layout->offset_y = (nk_uint)scroll_offset;
  14875. if (in && scroll_has_scrolling)
  14876. in->mouse.scroll_delta.y = 0;
  14877. }
  14878. {
  14879. /* horizontal scrollbar */
  14880. nk_flags state = 0;
  14881. scroll.x = layout->bounds.x;
  14882. scroll.y = layout->bounds.y + layout->bounds.h;
  14883. scroll.w = layout->bounds.w;
  14884. scroll.h = scrollbar_size.y;
  14885. scroll_offset = (float)*layout->offset_x;
  14886. scroll_target = (float)(int)(layout->max_x - scroll.x);
  14887. scroll_step = layout->max_x * 0.05f;
  14888. scroll_inc = layout->max_x * 0.005f;
  14889. scroll_offset = nk_do_scrollbarh(&state, out, scroll, scroll_has_scrolling,
  14890. scroll_offset, scroll_target, scroll_step, scroll_inc,
  14891. &ctx->style.scrollh, in, style->font);
  14892. *layout->offset_x = (nk_uint)scroll_offset;
  14893. }
  14894. }
  14895. /* hide scroll if no user input */
  14896. if (window->flags & NK_WINDOW_SCROLL_AUTO_HIDE) {
  14897. int has_input = ctx->input.mouse.delta.x != 0 || ctx->input.mouse.delta.y != 0 || ctx->input.mouse.scroll_delta.y != 0;
  14898. int is_window_hovered = nk_window_is_hovered(ctx);
  14899. int any_item_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
  14900. if ((!has_input && is_window_hovered) || (!is_window_hovered && !any_item_active))
  14901. window->scrollbar_hiding_timer += ctx->delta_time_seconds;
  14902. else window->scrollbar_hiding_timer = 0;
  14903. } else window->scrollbar_hiding_timer = 0;
  14904. /* window border */
  14905. if (layout->flags & NK_WINDOW_BORDER)
  14906. {
  14907. struct nk_color border_color = nk_panel_get_border_color(style, layout->type);
  14908. const float padding_y = (layout->flags & NK_WINDOW_MINIMIZED)
  14909. ? (style->window.border + window->bounds.y + layout->header_height)
  14910. : ((layout->flags & NK_WINDOW_DYNAMIC)
  14911. ? (layout->bounds.y + layout->bounds.h + layout->footer_height)
  14912. : (window->bounds.y + window->bounds.h));
  14913. struct nk_rect b = window->bounds;
  14914. b.h = padding_y - window->bounds.y;
  14915. nk_stroke_rect(out, b, 0, layout->border, border_color);
  14916. }
  14917. /* scaler */
  14918. if ((layout->flags & NK_WINDOW_SCALABLE) && in && !(layout->flags & NK_WINDOW_MINIMIZED))
  14919. {
  14920. /* calculate scaler bounds */
  14921. struct nk_rect scaler;
  14922. scaler.w = scrollbar_size.x;
  14923. scaler.h = scrollbar_size.y;
  14924. scaler.y = layout->bounds.y + layout->bounds.h;
  14925. if (layout->flags & NK_WINDOW_SCALE_LEFT)
  14926. scaler.x = layout->bounds.x - panel_padding.x * 0.5f;
  14927. else scaler.x = layout->bounds.x + layout->bounds.w + panel_padding.x;
  14928. if (layout->flags & NK_WINDOW_NO_SCROLLBAR)
  14929. scaler.x -= scaler.w;
  14930. /* draw scaler */
  14931. {const struct nk_style_item *item = &style->window.scaler;
  14932. if (item->type == NK_STYLE_ITEM_IMAGE)
  14933. nk_draw_image(out, scaler, &item->data.image, nk_white);
  14934. else {
  14935. if (layout->flags & NK_WINDOW_SCALE_LEFT) {
  14936. nk_fill_triangle(out, scaler.x, scaler.y, scaler.x,
  14937. scaler.y + scaler.h, scaler.x + scaler.w,
  14938. scaler.y + scaler.h, item->data.color);
  14939. } else {
  14940. nk_fill_triangle(out, scaler.x + scaler.w, scaler.y, scaler.x + scaler.w,
  14941. scaler.y + scaler.h, scaler.x, scaler.y + scaler.h, item->data.color);
  14942. }
  14943. }}
  14944. /* do window scaling */
  14945. if (!(window->flags & NK_WINDOW_ROM)) {
  14946. struct nk_vec2 window_size = style->window.min_size;
  14947. int left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  14948. int left_mouse_click_in_scaler = nk_input_has_mouse_click_down_in_rect(in,
  14949. NK_BUTTON_LEFT, scaler, nk_true);
  14950. if (left_mouse_down && left_mouse_click_in_scaler) {
  14951. float delta_x = in->mouse.delta.x;
  14952. if (layout->flags & NK_WINDOW_SCALE_LEFT) {
  14953. delta_x = -delta_x;
  14954. window->bounds.x += in->mouse.delta.x;
  14955. }
  14956. /* dragging in x-direction */
  14957. if (window->bounds.w + delta_x >= window_size.x) {
  14958. if ((delta_x < 0) || (delta_x > 0 && in->mouse.pos.x >= scaler.x)) {
  14959. window->bounds.w = window->bounds.w + delta_x;
  14960. scaler.x += in->mouse.delta.x;
  14961. }
  14962. }
  14963. /* dragging in y-direction (only possible if static window) */
  14964. if (!(layout->flags & NK_WINDOW_DYNAMIC)) {
  14965. if (window_size.y < window->bounds.h + in->mouse.delta.y) {
  14966. if ((in->mouse.delta.y < 0) || (in->mouse.delta.y > 0 && in->mouse.pos.y >= scaler.y)) {
  14967. window->bounds.h = window->bounds.h + in->mouse.delta.y;
  14968. scaler.y += in->mouse.delta.y;
  14969. }
  14970. }
  14971. }
  14972. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_RESIZE_TOP_RIGHT_DOWN_LEFT];
  14973. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = scaler.x + scaler.w/2.0f;
  14974. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = scaler.y + scaler.h/2.0f;
  14975. }
  14976. }
  14977. }
  14978. if (!nk_panel_is_sub(layout->type)) {
  14979. /* window is hidden so clear command buffer */
  14980. if (layout->flags & NK_WINDOW_HIDDEN)
  14981. nk_command_buffer_reset(&window->buffer);
  14982. /* window is visible and not tab */
  14983. else nk_finish(ctx, window);
  14984. }
  14985. /* NK_WINDOW_REMOVE_ROM flag was set so remove NK_WINDOW_ROM */
  14986. if (layout->flags & NK_WINDOW_REMOVE_ROM) {
  14987. layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
  14988. layout->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
  14989. }
  14990. window->flags = layout->flags;
  14991. /* property garbage collector */
  14992. if (window->property.active && window->property.old != window->property.seq &&
  14993. window->property.active == window->property.prev) {
  14994. nk_zero(&window->property, sizeof(window->property));
  14995. } else {
  14996. window->property.old = window->property.seq;
  14997. window->property.prev = window->property.active;
  14998. window->property.seq = 0;
  14999. }
  15000. /* edit garbage collector */
  15001. if (window->edit.active && window->edit.old != window->edit.seq &&
  15002. window->edit.active == window->edit.prev) {
  15003. nk_zero(&window->edit, sizeof(window->edit));
  15004. } else {
  15005. window->edit.old = window->edit.seq;
  15006. window->edit.prev = window->edit.active;
  15007. window->edit.seq = 0;
  15008. }
  15009. /* contextual garbage collector */
  15010. if (window->popup.active_con && window->popup.con_old != window->popup.con_count) {
  15011. window->popup.con_count = 0;
  15012. window->popup.con_old = 0;
  15013. window->popup.active_con = 0;
  15014. } else {
  15015. window->popup.con_old = window->popup.con_count;
  15016. window->popup.con_count = 0;
  15017. }
  15018. window->popup.combo_count = 0;
  15019. /* helper to make sure you have a 'nk_tree_push' for every 'nk_tree_pop' */
  15020. NK_ASSERT(!layout->row.tree_depth);
  15021. }
  15022. /* ===============================================================
  15023. *
  15024. * WINDOW
  15025. *
  15026. * ===============================================================*/
  15027. NK_LIB void*
  15028. nk_create_window(struct nk_context *ctx)
  15029. {
  15030. struct nk_page_element *elem;
  15031. elem = nk_create_page_element(ctx);
  15032. if (!elem) return 0;
  15033. elem->data.win.seq = ctx->seq;
  15034. return &elem->data.win;
  15035. }
  15036. NK_LIB void
  15037. nk_free_window(struct nk_context *ctx, struct nk_window *win)
  15038. {
  15039. /* unlink windows from list */
  15040. struct nk_table *it = win->tables;
  15041. if (win->popup.win) {
  15042. nk_free_window(ctx, win->popup.win);
  15043. win->popup.win = 0;
  15044. }
  15045. win->next = 0;
  15046. win->prev = 0;
  15047. while (it) {
  15048. /*free window state tables */
  15049. struct nk_table *n = it->next;
  15050. nk_remove_table(win, it);
  15051. nk_free_table(ctx, it);
  15052. if (it == win->tables)
  15053. win->tables = n;
  15054. it = n;
  15055. }
  15056. /* link windows into freelist */
  15057. {union nk_page_data *pd = NK_CONTAINER_OF(win, union nk_page_data, win);
  15058. struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  15059. nk_free_page_element(ctx, pe);}
  15060. }
  15061. NK_LIB struct nk_window*
  15062. nk_find_window(struct nk_context *ctx, nk_hash hash, const char *name)
  15063. {
  15064. struct nk_window *iter;
  15065. iter = ctx->begin;
  15066. while (iter) {
  15067. NK_ASSERT(iter != iter->next);
  15068. if (iter->name == hash) {
  15069. int max_len = nk_strlen(iter->name_string);
  15070. if (!nk_stricmpn(iter->name_string, name, max_len))
  15071. return iter;
  15072. }
  15073. iter = iter->next;
  15074. }
  15075. return 0;
  15076. }
  15077. NK_LIB void
  15078. nk_insert_window(struct nk_context *ctx, struct nk_window *win,
  15079. enum nk_window_insert_location loc)
  15080. {
  15081. const struct nk_window *iter;
  15082. NK_ASSERT(ctx);
  15083. NK_ASSERT(win);
  15084. if (!win || !ctx) return;
  15085. iter = ctx->begin;
  15086. while (iter) {
  15087. NK_ASSERT(iter != iter->next);
  15088. NK_ASSERT(iter != win);
  15089. if (iter == win) return;
  15090. iter = iter->next;
  15091. }
  15092. if (!ctx->begin) {
  15093. win->next = 0;
  15094. win->prev = 0;
  15095. ctx->begin = win;
  15096. ctx->end = win;
  15097. ctx->count = 1;
  15098. return;
  15099. }
  15100. if (loc == NK_INSERT_BACK) {
  15101. struct nk_window *end;
  15102. end = ctx->end;
  15103. end->flags |= NK_WINDOW_ROM;
  15104. end->next = win;
  15105. win->prev = ctx->end;
  15106. win->next = 0;
  15107. ctx->end = win;
  15108. ctx->active = ctx->end;
  15109. ctx->end->flags &= ~(nk_flags)NK_WINDOW_ROM;
  15110. } else {
  15111. /*ctx->end->flags |= NK_WINDOW_ROM;*/
  15112. ctx->begin->prev = win;
  15113. win->next = ctx->begin;
  15114. win->prev = 0;
  15115. ctx->begin = win;
  15116. ctx->begin->flags &= ~(nk_flags)NK_WINDOW_ROM;
  15117. }
  15118. ctx->count++;
  15119. }
  15120. NK_LIB void
  15121. nk_remove_window(struct nk_context *ctx, struct nk_window *win)
  15122. {
  15123. if (win == ctx->begin || win == ctx->end) {
  15124. if (win == ctx->begin) {
  15125. ctx->begin = win->next;
  15126. if (win->next)
  15127. win->next->prev = 0;
  15128. }
  15129. if (win == ctx->end) {
  15130. ctx->end = win->prev;
  15131. if (win->prev)
  15132. win->prev->next = 0;
  15133. }
  15134. } else {
  15135. if (win->next)
  15136. win->next->prev = win->prev;
  15137. if (win->prev)
  15138. win->prev->next = win->next;
  15139. }
  15140. if (win == ctx->active || !ctx->active) {
  15141. ctx->active = ctx->end;
  15142. if (ctx->end)
  15143. ctx->end->flags &= ~(nk_flags)NK_WINDOW_ROM;
  15144. }
  15145. win->next = 0;
  15146. win->prev = 0;
  15147. ctx->count--;
  15148. }
  15149. NK_API int
  15150. nk_begin(struct nk_context *ctx, const char *title,
  15151. struct nk_rect bounds, nk_flags flags)
  15152. {
  15153. return nk_begin_titled(ctx, title, title, bounds, flags);
  15154. }
  15155. NK_API int
  15156. nk_begin_titled(struct nk_context *ctx, const char *name, const char *title,
  15157. struct nk_rect bounds, nk_flags flags)
  15158. {
  15159. struct nk_window *win;
  15160. struct nk_style *style;
  15161. nk_hash title_hash;
  15162. int title_len;
  15163. int ret = 0;
  15164. NK_ASSERT(ctx);
  15165. NK_ASSERT(name);
  15166. NK_ASSERT(title);
  15167. NK_ASSERT(ctx->style.font && ctx->style.font->width && "if this triggers you forgot to add a font");
  15168. NK_ASSERT(!ctx->current && "if this triggers you missed a `nk_end` call");
  15169. if (!ctx || ctx->current || !title || !name)
  15170. return 0;
  15171. /* find or create window */
  15172. style = &ctx->style;
  15173. title_len = (int)nk_strlen(name);
  15174. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15175. win = nk_find_window(ctx, title_hash, name);
  15176. if (!win) {
  15177. /* create new window */
  15178. nk_size name_length = (nk_size)nk_strlen(name);
  15179. win = (struct nk_window*)nk_create_window(ctx);
  15180. NK_ASSERT(win);
  15181. if (!win) return 0;
  15182. if (flags & NK_WINDOW_BACKGROUND)
  15183. nk_insert_window(ctx, win, NK_INSERT_FRONT);
  15184. else nk_insert_window(ctx, win, NK_INSERT_BACK);
  15185. nk_command_buffer_init(&win->buffer, &ctx->memory, NK_CLIPPING_ON);
  15186. win->flags = flags;
  15187. win->bounds = bounds;
  15188. win->name = title_hash;
  15189. name_length = NK_MIN(name_length, NK_WINDOW_MAX_NAME-1);
  15190. NK_MEMCPY(win->name_string, name, name_length);
  15191. win->name_string[name_length] = 0;
  15192. win->popup.win = 0;
  15193. if (!ctx->active)
  15194. ctx->active = win;
  15195. } else {
  15196. /* update window */
  15197. win->flags &= ~(nk_flags)(NK_WINDOW_PRIVATE-1);
  15198. win->flags |= flags;
  15199. if (!(win->flags & (NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE)))
  15200. win->bounds = bounds;
  15201. /* If this assert triggers you either:
  15202. *
  15203. * I.) Have more than one window with the same name or
  15204. * II.) You forgot to actually draw the window.
  15205. * More specific you did not call `nk_clear` (nk_clear will be
  15206. * automatically called for you if you are using one of the
  15207. * provided demo backends). */
  15208. NK_ASSERT(win->seq != ctx->seq);
  15209. win->seq = ctx->seq;
  15210. if (!ctx->active && !(win->flags & NK_WINDOW_HIDDEN)) {
  15211. ctx->active = win;
  15212. ctx->end = win;
  15213. }
  15214. }
  15215. if (win->flags & NK_WINDOW_HIDDEN) {
  15216. ctx->current = win;
  15217. win->layout = 0;
  15218. return 0;
  15219. } else nk_start(ctx, win);
  15220. /* window overlapping */
  15221. if (!(win->flags & NK_WINDOW_HIDDEN) && !(win->flags & NK_WINDOW_NO_INPUT))
  15222. {
  15223. int inpanel, ishovered;
  15224. struct nk_window *iter = win;
  15225. float h = ctx->style.font->height + 2.0f * style->window.header.padding.y +
  15226. (2.0f * style->window.header.label_padding.y);
  15227. struct nk_rect win_bounds = (!(win->flags & NK_WINDOW_MINIMIZED))?
  15228. win->bounds: nk_rect(win->bounds.x, win->bounds.y, win->bounds.w, h);
  15229. /* activate window if hovered and no other window is overlapping this window */
  15230. inpanel = nk_input_has_mouse_click_down_in_rect(&ctx->input, NK_BUTTON_LEFT, win_bounds, nk_true);
  15231. inpanel = inpanel && ctx->input.mouse.buttons[NK_BUTTON_LEFT].clicked;
  15232. ishovered = nk_input_is_mouse_hovering_rect(&ctx->input, win_bounds);
  15233. if ((win != ctx->active) && ishovered && !ctx->input.mouse.buttons[NK_BUTTON_LEFT].down) {
  15234. iter = win->next;
  15235. while (iter) {
  15236. struct nk_rect iter_bounds = (!(iter->flags & NK_WINDOW_MINIMIZED))?
  15237. iter->bounds: nk_rect(iter->bounds.x, iter->bounds.y, iter->bounds.w, h);
  15238. if (NK_INTERSECT(win_bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  15239. iter_bounds.x, iter_bounds.y, iter_bounds.w, iter_bounds.h) &&
  15240. (!(iter->flags & NK_WINDOW_HIDDEN)))
  15241. break;
  15242. if (iter->popup.win && iter->popup.active && !(iter->flags & NK_WINDOW_HIDDEN) &&
  15243. NK_INTERSECT(win->bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  15244. iter->popup.win->bounds.x, iter->popup.win->bounds.y,
  15245. iter->popup.win->bounds.w, iter->popup.win->bounds.h))
  15246. break;
  15247. iter = iter->next;
  15248. }
  15249. }
  15250. /* activate window if clicked */
  15251. if (iter && inpanel && (win != ctx->end)) {
  15252. iter = win->next;
  15253. while (iter) {
  15254. /* try to find a panel with higher priority in the same position */
  15255. struct nk_rect iter_bounds = (!(iter->flags & NK_WINDOW_MINIMIZED))?
  15256. iter->bounds: nk_rect(iter->bounds.x, iter->bounds.y, iter->bounds.w, h);
  15257. if (NK_INBOX(ctx->input.mouse.pos.x, ctx->input.mouse.pos.y,
  15258. iter_bounds.x, iter_bounds.y, iter_bounds.w, iter_bounds.h) &&
  15259. !(iter->flags & NK_WINDOW_HIDDEN))
  15260. break;
  15261. if (iter->popup.win && iter->popup.active && !(iter->flags & NK_WINDOW_HIDDEN) &&
  15262. NK_INTERSECT(win_bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  15263. iter->popup.win->bounds.x, iter->popup.win->bounds.y,
  15264. iter->popup.win->bounds.w, iter->popup.win->bounds.h))
  15265. break;
  15266. iter = iter->next;
  15267. }
  15268. }
  15269. if (iter && !(win->flags & NK_WINDOW_ROM) && (win->flags & NK_WINDOW_BACKGROUND)) {
  15270. win->flags |= (nk_flags)NK_WINDOW_ROM;
  15271. iter->flags &= ~(nk_flags)NK_WINDOW_ROM;
  15272. ctx->active = iter;
  15273. if (!(iter->flags & NK_WINDOW_BACKGROUND)) {
  15274. /* current window is active in that position so transfer to top
  15275. * at the highest priority in stack */
  15276. nk_remove_window(ctx, iter);
  15277. nk_insert_window(ctx, iter, NK_INSERT_BACK);
  15278. }
  15279. } else {
  15280. if (!iter && ctx->end != win) {
  15281. if (!(win->flags & NK_WINDOW_BACKGROUND)) {
  15282. /* current window is active in that position so transfer to top
  15283. * at the highest priority in stack */
  15284. nk_remove_window(ctx, win);
  15285. nk_insert_window(ctx, win, NK_INSERT_BACK);
  15286. }
  15287. win->flags &= ~(nk_flags)NK_WINDOW_ROM;
  15288. ctx->active = win;
  15289. }
  15290. if (ctx->end != win && !(win->flags & NK_WINDOW_BACKGROUND))
  15291. win->flags |= NK_WINDOW_ROM;
  15292. }
  15293. }
  15294. win->layout = (struct nk_panel*)nk_create_panel(ctx);
  15295. ctx->current = win;
  15296. ret = nk_panel_begin(ctx, title, NK_PANEL_WINDOW);
  15297. win->layout->offset_x = &win->scrollbar.x;
  15298. win->layout->offset_y = &win->scrollbar.y;
  15299. return ret;
  15300. }
  15301. NK_API void
  15302. nk_end(struct nk_context *ctx)
  15303. {
  15304. struct nk_panel *layout;
  15305. NK_ASSERT(ctx);
  15306. NK_ASSERT(ctx->current && "if this triggers you forgot to call `nk_begin`");
  15307. if (!ctx || !ctx->current)
  15308. return;
  15309. layout = ctx->current->layout;
  15310. if (!layout || (layout->type == NK_PANEL_WINDOW && (ctx->current->flags & NK_WINDOW_HIDDEN))) {
  15311. ctx->current = 0;
  15312. return;
  15313. }
  15314. nk_panel_end(ctx);
  15315. nk_free_panel(ctx, ctx->current->layout);
  15316. ctx->current = 0;
  15317. }
  15318. NK_API struct nk_rect
  15319. nk_window_get_bounds(const struct nk_context *ctx)
  15320. {
  15321. NK_ASSERT(ctx);
  15322. NK_ASSERT(ctx->current);
  15323. if (!ctx || !ctx->current) return nk_rect(0,0,0,0);
  15324. return ctx->current->bounds;
  15325. }
  15326. NK_API struct nk_vec2
  15327. nk_window_get_position(const struct nk_context *ctx)
  15328. {
  15329. NK_ASSERT(ctx);
  15330. NK_ASSERT(ctx->current);
  15331. if (!ctx || !ctx->current) return nk_vec2(0,0);
  15332. return nk_vec2(ctx->current->bounds.x, ctx->current->bounds.y);
  15333. }
  15334. NK_API struct nk_vec2
  15335. nk_window_get_size(const struct nk_context *ctx)
  15336. {
  15337. NK_ASSERT(ctx);
  15338. NK_ASSERT(ctx->current);
  15339. if (!ctx || !ctx->current) return nk_vec2(0,0);
  15340. return nk_vec2(ctx->current->bounds.w, ctx->current->bounds.h);
  15341. }
  15342. NK_API float
  15343. nk_window_get_width(const struct nk_context *ctx)
  15344. {
  15345. NK_ASSERT(ctx);
  15346. NK_ASSERT(ctx->current);
  15347. if (!ctx || !ctx->current) return 0;
  15348. return ctx->current->bounds.w;
  15349. }
  15350. NK_API float
  15351. nk_window_get_height(const struct nk_context *ctx)
  15352. {
  15353. NK_ASSERT(ctx);
  15354. NK_ASSERT(ctx->current);
  15355. if (!ctx || !ctx->current) return 0;
  15356. return ctx->current->bounds.h;
  15357. }
  15358. NK_API struct nk_rect
  15359. nk_window_get_content_region(struct nk_context *ctx)
  15360. {
  15361. NK_ASSERT(ctx);
  15362. NK_ASSERT(ctx->current);
  15363. if (!ctx || !ctx->current) return nk_rect(0,0,0,0);
  15364. return ctx->current->layout->clip;
  15365. }
  15366. NK_API struct nk_vec2
  15367. nk_window_get_content_region_min(struct nk_context *ctx)
  15368. {
  15369. NK_ASSERT(ctx);
  15370. NK_ASSERT(ctx->current);
  15371. NK_ASSERT(ctx->current->layout);
  15372. if (!ctx || !ctx->current) return nk_vec2(0,0);
  15373. return nk_vec2(ctx->current->layout->clip.x, ctx->current->layout->clip.y);
  15374. }
  15375. NK_API struct nk_vec2
  15376. nk_window_get_content_region_max(struct nk_context *ctx)
  15377. {
  15378. NK_ASSERT(ctx);
  15379. NK_ASSERT(ctx->current);
  15380. NK_ASSERT(ctx->current->layout);
  15381. if (!ctx || !ctx->current) return nk_vec2(0,0);
  15382. return nk_vec2(ctx->current->layout->clip.x + ctx->current->layout->clip.w,
  15383. ctx->current->layout->clip.y + ctx->current->layout->clip.h);
  15384. }
  15385. NK_API struct nk_vec2
  15386. nk_window_get_content_region_size(struct nk_context *ctx)
  15387. {
  15388. NK_ASSERT(ctx);
  15389. NK_ASSERT(ctx->current);
  15390. NK_ASSERT(ctx->current->layout);
  15391. if (!ctx || !ctx->current) return nk_vec2(0,0);
  15392. return nk_vec2(ctx->current->layout->clip.w, ctx->current->layout->clip.h);
  15393. }
  15394. NK_API struct nk_command_buffer*
  15395. nk_window_get_canvas(struct nk_context *ctx)
  15396. {
  15397. NK_ASSERT(ctx);
  15398. NK_ASSERT(ctx->current);
  15399. NK_ASSERT(ctx->current->layout);
  15400. if (!ctx || !ctx->current) return 0;
  15401. return &ctx->current->buffer;
  15402. }
  15403. NK_API struct nk_panel*
  15404. nk_window_get_panel(struct nk_context *ctx)
  15405. {
  15406. NK_ASSERT(ctx);
  15407. NK_ASSERT(ctx->current);
  15408. if (!ctx || !ctx->current) return 0;
  15409. return ctx->current->layout;
  15410. }
  15411. NK_API int
  15412. nk_window_has_focus(const struct nk_context *ctx)
  15413. {
  15414. NK_ASSERT(ctx);
  15415. NK_ASSERT(ctx->current);
  15416. NK_ASSERT(ctx->current->layout);
  15417. if (!ctx || !ctx->current) return 0;
  15418. return ctx->current == ctx->active;
  15419. }
  15420. NK_API int
  15421. nk_window_is_hovered(struct nk_context *ctx)
  15422. {
  15423. NK_ASSERT(ctx);
  15424. NK_ASSERT(ctx->current);
  15425. if (!ctx || !ctx->current) return 0;
  15426. if(ctx->current->flags & NK_WINDOW_HIDDEN)
  15427. return 0;
  15428. return nk_input_is_mouse_hovering_rect(&ctx->input, ctx->current->bounds);
  15429. }
  15430. NK_API int
  15431. nk_window_is_any_hovered(struct nk_context *ctx)
  15432. {
  15433. struct nk_window *iter;
  15434. NK_ASSERT(ctx);
  15435. if (!ctx) return 0;
  15436. iter = ctx->begin;
  15437. while (iter) {
  15438. /* check if window is being hovered */
  15439. if(!(iter->flags & NK_WINDOW_HIDDEN)) {
  15440. /* check if window popup is being hovered */
  15441. if (iter->popup.active && iter->popup.win && nk_input_is_mouse_hovering_rect(&ctx->input, iter->popup.win->bounds))
  15442. return 1;
  15443. if (iter->flags & NK_WINDOW_MINIMIZED) {
  15444. struct nk_rect header = iter->bounds;
  15445. header.h = ctx->style.font->height + 2 * ctx->style.window.header.padding.y;
  15446. if (nk_input_is_mouse_hovering_rect(&ctx->input, header))
  15447. return 1;
  15448. } else if (nk_input_is_mouse_hovering_rect(&ctx->input, iter->bounds)) {
  15449. return 1;
  15450. }
  15451. }
  15452. iter = iter->next;
  15453. }
  15454. return 0;
  15455. }
  15456. NK_API int
  15457. nk_item_is_any_active(struct nk_context *ctx)
  15458. {
  15459. int any_hovered = nk_window_is_any_hovered(ctx);
  15460. int any_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
  15461. return any_hovered || any_active;
  15462. }
  15463. NK_API int
  15464. nk_window_is_collapsed(struct nk_context *ctx, const char *name)
  15465. {
  15466. int title_len;
  15467. nk_hash title_hash;
  15468. struct nk_window *win;
  15469. NK_ASSERT(ctx);
  15470. if (!ctx) return 0;
  15471. title_len = (int)nk_strlen(name);
  15472. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15473. win = nk_find_window(ctx, title_hash, name);
  15474. if (!win) return 0;
  15475. return win->flags & NK_WINDOW_MINIMIZED;
  15476. }
  15477. NK_API int
  15478. nk_window_is_closed(struct nk_context *ctx, const char *name)
  15479. {
  15480. int title_len;
  15481. nk_hash title_hash;
  15482. struct nk_window *win;
  15483. NK_ASSERT(ctx);
  15484. if (!ctx) return 1;
  15485. title_len = (int)nk_strlen(name);
  15486. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15487. win = nk_find_window(ctx, title_hash, name);
  15488. if (!win) return 1;
  15489. return (win->flags & NK_WINDOW_CLOSED);
  15490. }
  15491. NK_API int
  15492. nk_window_is_hidden(struct nk_context *ctx, const char *name)
  15493. {
  15494. int title_len;
  15495. nk_hash title_hash;
  15496. struct nk_window *win;
  15497. NK_ASSERT(ctx);
  15498. if (!ctx) return 1;
  15499. title_len = (int)nk_strlen(name);
  15500. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15501. win = nk_find_window(ctx, title_hash, name);
  15502. if (!win) return 1;
  15503. return (win->flags & NK_WINDOW_HIDDEN);
  15504. }
  15505. NK_API int
  15506. nk_window_is_active(struct nk_context *ctx, const char *name)
  15507. {
  15508. int title_len;
  15509. nk_hash title_hash;
  15510. struct nk_window *win;
  15511. NK_ASSERT(ctx);
  15512. if (!ctx) return 0;
  15513. title_len = (int)nk_strlen(name);
  15514. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15515. win = nk_find_window(ctx, title_hash, name);
  15516. if (!win) return 0;
  15517. return win == ctx->active;
  15518. }
  15519. NK_API struct nk_window*
  15520. nk_window_find(struct nk_context *ctx, const char *name)
  15521. {
  15522. int title_len;
  15523. nk_hash title_hash;
  15524. title_len = (int)nk_strlen(name);
  15525. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15526. return nk_find_window(ctx, title_hash, name);
  15527. }
  15528. NK_API void
  15529. nk_window_close(struct nk_context *ctx, const char *name)
  15530. {
  15531. struct nk_window *win;
  15532. NK_ASSERT(ctx);
  15533. if (!ctx) return;
  15534. win = nk_window_find(ctx, name);
  15535. if (!win) return;
  15536. NK_ASSERT(ctx->current != win && "You cannot close a currently active window");
  15537. if (ctx->current == win) return;
  15538. win->flags |= NK_WINDOW_HIDDEN;
  15539. win->flags |= NK_WINDOW_CLOSED;
  15540. }
  15541. NK_API void
  15542. nk_window_set_bounds(struct nk_context *ctx,
  15543. const char *name, struct nk_rect bounds)
  15544. {
  15545. struct nk_window *win;
  15546. NK_ASSERT(ctx);
  15547. if (!ctx) return;
  15548. win = nk_window_find(ctx, name);
  15549. if (!win) return;
  15550. NK_ASSERT(ctx->current != win && "You cannot update a currently in procecss window");
  15551. win->bounds = bounds;
  15552. }
  15553. NK_API void
  15554. nk_window_set_position(struct nk_context *ctx,
  15555. const char *name, struct nk_vec2 pos)
  15556. {
  15557. struct nk_window *win = nk_window_find(ctx, name);
  15558. if (!win) return;
  15559. win->bounds.x = pos.x;
  15560. win->bounds.y = pos.y;
  15561. }
  15562. NK_API void
  15563. nk_window_set_size(struct nk_context *ctx,
  15564. const char *name, struct nk_vec2 size)
  15565. {
  15566. struct nk_window *win = nk_window_find(ctx, name);
  15567. if (!win) return;
  15568. win->bounds.w = size.x;
  15569. win->bounds.h = size.y;
  15570. }
  15571. NK_API void
  15572. nk_window_collapse(struct nk_context *ctx, const char *name,
  15573. enum nk_collapse_states c)
  15574. {
  15575. int title_len;
  15576. nk_hash title_hash;
  15577. struct nk_window *win;
  15578. NK_ASSERT(ctx);
  15579. if (!ctx) return;
  15580. title_len = (int)nk_strlen(name);
  15581. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15582. win = nk_find_window(ctx, title_hash, name);
  15583. if (!win) return;
  15584. if (c == NK_MINIMIZED)
  15585. win->flags |= NK_WINDOW_MINIMIZED;
  15586. else win->flags &= ~(nk_flags)NK_WINDOW_MINIMIZED;
  15587. }
  15588. NK_API void
  15589. nk_window_collapse_if(struct nk_context *ctx, const char *name,
  15590. enum nk_collapse_states c, int cond)
  15591. {
  15592. NK_ASSERT(ctx);
  15593. if (!ctx || !cond) return;
  15594. nk_window_collapse(ctx, name, c);
  15595. }
  15596. NK_API void
  15597. nk_window_show(struct nk_context *ctx, const char *name, enum nk_show_states s)
  15598. {
  15599. int title_len;
  15600. nk_hash title_hash;
  15601. struct nk_window *win;
  15602. NK_ASSERT(ctx);
  15603. if (!ctx) return;
  15604. title_len = (int)nk_strlen(name);
  15605. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15606. win = nk_find_window(ctx, title_hash, name);
  15607. if (!win) return;
  15608. if (s == NK_HIDDEN) {
  15609. win->flags |= NK_WINDOW_HIDDEN;
  15610. } else win->flags &= ~(nk_flags)NK_WINDOW_HIDDEN;
  15611. }
  15612. NK_API void
  15613. nk_window_show_if(struct nk_context *ctx, const char *name,
  15614. enum nk_show_states s, int cond)
  15615. {
  15616. NK_ASSERT(ctx);
  15617. if (!ctx || !cond) return;
  15618. nk_window_show(ctx, name, s);
  15619. }
  15620. NK_API void
  15621. nk_window_set_focus(struct nk_context *ctx, const char *name)
  15622. {
  15623. int title_len;
  15624. nk_hash title_hash;
  15625. struct nk_window *win;
  15626. NK_ASSERT(ctx);
  15627. if (!ctx) return;
  15628. title_len = (int)nk_strlen(name);
  15629. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15630. win = nk_find_window(ctx, title_hash, name);
  15631. if (win && ctx->end != win) {
  15632. nk_remove_window(ctx, win);
  15633. nk_insert_window(ctx, win, NK_INSERT_BACK);
  15634. }
  15635. ctx->active = win;
  15636. }
  15637. /* ===============================================================
  15638. *
  15639. * POPUP
  15640. *
  15641. * ===============================================================*/
  15642. NK_API int
  15643. nk_popup_begin(struct nk_context *ctx, enum nk_popup_type type,
  15644. const char *title, nk_flags flags, struct nk_rect rect)
  15645. {
  15646. struct nk_window *popup;
  15647. struct nk_window *win;
  15648. struct nk_panel *panel;
  15649. int title_len;
  15650. nk_hash title_hash;
  15651. nk_size allocated;
  15652. NK_ASSERT(ctx);
  15653. NK_ASSERT(title);
  15654. NK_ASSERT(ctx->current);
  15655. NK_ASSERT(ctx->current->layout);
  15656. if (!ctx || !ctx->current || !ctx->current->layout)
  15657. return 0;
  15658. win = ctx->current;
  15659. panel = win->layout;
  15660. NK_ASSERT(!(panel->type & NK_PANEL_SET_POPUP) && "popups are not allowed to have popups");
  15661. (void)panel;
  15662. title_len = (int)nk_strlen(title);
  15663. title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_POPUP);
  15664. popup = win->popup.win;
  15665. if (!popup) {
  15666. popup = (struct nk_window*)nk_create_window(ctx);
  15667. popup->parent = win;
  15668. win->popup.win = popup;
  15669. win->popup.active = 0;
  15670. win->popup.type = NK_PANEL_POPUP;
  15671. }
  15672. /* make sure we have correct popup */
  15673. if (win->popup.name != title_hash) {
  15674. if (!win->popup.active) {
  15675. nk_zero(popup, sizeof(*popup));
  15676. win->popup.name = title_hash;
  15677. win->popup.active = 1;
  15678. win->popup.type = NK_PANEL_POPUP;
  15679. } else return 0;
  15680. }
  15681. /* popup position is local to window */
  15682. ctx->current = popup;
  15683. rect.x += win->layout->clip.x;
  15684. rect.y += win->layout->clip.y;
  15685. /* setup popup data */
  15686. popup->parent = win;
  15687. popup->bounds = rect;
  15688. popup->seq = ctx->seq;
  15689. popup->layout = (struct nk_panel*)nk_create_panel(ctx);
  15690. popup->flags = flags;
  15691. popup->flags |= NK_WINDOW_BORDER;
  15692. if (type == NK_POPUP_DYNAMIC)
  15693. popup->flags |= NK_WINDOW_DYNAMIC;
  15694. popup->buffer = win->buffer;
  15695. nk_start_popup(ctx, win);
  15696. allocated = ctx->memory.allocated;
  15697. nk_push_scissor(&popup->buffer, nk_null_rect);
  15698. if (nk_panel_begin(ctx, title, NK_PANEL_POPUP)) {
  15699. /* popup is running therefore invalidate parent panels */
  15700. struct nk_panel *root;
  15701. root = win->layout;
  15702. while (root) {
  15703. root->flags |= NK_WINDOW_ROM;
  15704. root->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
  15705. root = root->parent;
  15706. }
  15707. win->popup.active = 1;
  15708. popup->layout->offset_x = &popup->scrollbar.x;
  15709. popup->layout->offset_y = &popup->scrollbar.y;
  15710. popup->layout->parent = win->layout;
  15711. return 1;
  15712. } else {
  15713. /* popup was closed/is invalid so cleanup */
  15714. struct nk_panel *root;
  15715. root = win->layout;
  15716. while (root) {
  15717. root->flags |= NK_WINDOW_REMOVE_ROM;
  15718. root = root->parent;
  15719. }
  15720. win->popup.buf.active = 0;
  15721. win->popup.active = 0;
  15722. ctx->memory.allocated = allocated;
  15723. ctx->current = win;
  15724. nk_free_panel(ctx, popup->layout);
  15725. popup->layout = 0;
  15726. return 0;
  15727. }
  15728. }
  15729. NK_LIB int
  15730. nk_nonblock_begin(struct nk_context *ctx,
  15731. nk_flags flags, struct nk_rect body, struct nk_rect header,
  15732. enum nk_panel_type panel_type)
  15733. {
  15734. struct nk_window *popup;
  15735. struct nk_window *win;
  15736. struct nk_panel *panel;
  15737. int is_active = nk_true;
  15738. NK_ASSERT(ctx);
  15739. NK_ASSERT(ctx->current);
  15740. NK_ASSERT(ctx->current->layout);
  15741. if (!ctx || !ctx->current || !ctx->current->layout)
  15742. return 0;
  15743. /* popups cannot have popups */
  15744. win = ctx->current;
  15745. panel = win->layout;
  15746. NK_ASSERT(!(panel->type & NK_PANEL_SET_POPUP));
  15747. (void)panel;
  15748. popup = win->popup.win;
  15749. if (!popup) {
  15750. /* create window for nonblocking popup */
  15751. popup = (struct nk_window*)nk_create_window(ctx);
  15752. popup->parent = win;
  15753. win->popup.win = popup;
  15754. win->popup.type = panel_type;
  15755. nk_command_buffer_init(&popup->buffer, &ctx->memory, NK_CLIPPING_ON);
  15756. } else {
  15757. /* close the popup if user pressed outside or in the header */
  15758. int pressed, in_body, in_header;
  15759. pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
  15760. in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
  15761. in_header = nk_input_is_mouse_hovering_rect(&ctx->input, header);
  15762. if (pressed && (!in_body || in_header))
  15763. is_active = nk_false;
  15764. }
  15765. win->popup.header = header;
  15766. if (!is_active) {
  15767. /* remove read only mode from all parent panels */
  15768. struct nk_panel *root = win->layout;
  15769. while (root) {
  15770. root->flags |= NK_WINDOW_REMOVE_ROM;
  15771. root = root->parent;
  15772. }
  15773. return is_active;
  15774. }
  15775. popup->bounds = body;
  15776. popup->parent = win;
  15777. popup->layout = (struct nk_panel*)nk_create_panel(ctx);
  15778. popup->flags = flags;
  15779. popup->flags |= NK_WINDOW_BORDER;
  15780. popup->flags |= NK_WINDOW_DYNAMIC;
  15781. popup->seq = ctx->seq;
  15782. win->popup.active = 1;
  15783. NK_ASSERT(popup->layout);
  15784. nk_start_popup(ctx, win);
  15785. popup->buffer = win->buffer;
  15786. nk_push_scissor(&popup->buffer, nk_null_rect);
  15787. ctx->current = popup;
  15788. nk_panel_begin(ctx, 0, panel_type);
  15789. win->buffer = popup->buffer;
  15790. popup->layout->parent = win->layout;
  15791. popup->layout->offset_x = &popup->scrollbar.x;
  15792. popup->layout->offset_y = &popup->scrollbar.y;
  15793. /* set read only mode to all parent panels */
  15794. {struct nk_panel *root;
  15795. root = win->layout;
  15796. while (root) {
  15797. root->flags |= NK_WINDOW_ROM;
  15798. root = root->parent;
  15799. }}
  15800. return is_active;
  15801. }
  15802. NK_API void
  15803. nk_popup_close(struct nk_context *ctx)
  15804. {
  15805. struct nk_window *popup;
  15806. NK_ASSERT(ctx);
  15807. if (!ctx || !ctx->current) return;
  15808. popup = ctx->current;
  15809. NK_ASSERT(popup->parent);
  15810. NK_ASSERT(popup->layout->type & NK_PANEL_SET_POPUP);
  15811. popup->flags |= NK_WINDOW_HIDDEN;
  15812. }
  15813. NK_API void
  15814. nk_popup_end(struct nk_context *ctx)
  15815. {
  15816. struct nk_window *win;
  15817. struct nk_window *popup;
  15818. NK_ASSERT(ctx);
  15819. NK_ASSERT(ctx->current);
  15820. NK_ASSERT(ctx->current->layout);
  15821. if (!ctx || !ctx->current || !ctx->current->layout)
  15822. return;
  15823. popup = ctx->current;
  15824. if (!popup->parent) return;
  15825. win = popup->parent;
  15826. if (popup->flags & NK_WINDOW_HIDDEN) {
  15827. struct nk_panel *root;
  15828. root = win->layout;
  15829. while (root) {
  15830. root->flags |= NK_WINDOW_REMOVE_ROM;
  15831. root = root->parent;
  15832. }
  15833. win->popup.active = 0;
  15834. }
  15835. nk_push_scissor(&popup->buffer, nk_null_rect);
  15836. nk_end(ctx);
  15837. win->buffer = popup->buffer;
  15838. nk_finish_popup(ctx, win);
  15839. ctx->current = win;
  15840. nk_push_scissor(&win->buffer, win->layout->clip);
  15841. }
  15842. /* ==============================================================
  15843. *
  15844. * CONTEXTUAL
  15845. *
  15846. * ===============================================================*/
  15847. NK_API int
  15848. nk_contextual_begin(struct nk_context *ctx, nk_flags flags, struct nk_vec2 size,
  15849. struct nk_rect trigger_bounds)
  15850. {
  15851. struct nk_window *win;
  15852. struct nk_window *popup;
  15853. struct nk_rect body;
  15854. NK_STORAGE const struct nk_rect null_rect = {-1,-1,0,0};
  15855. int is_clicked = 0;
  15856. int is_open = 0;
  15857. int ret = 0;
  15858. NK_ASSERT(ctx);
  15859. NK_ASSERT(ctx->current);
  15860. NK_ASSERT(ctx->current->layout);
  15861. if (!ctx || !ctx->current || !ctx->current->layout)
  15862. return 0;
  15863. win = ctx->current;
  15864. ++win->popup.con_count;
  15865. if (ctx->current != ctx->active)
  15866. return 0;
  15867. /* check if currently active contextual is active */
  15868. popup = win->popup.win;
  15869. is_open = (popup && win->popup.type == NK_PANEL_CONTEXTUAL);
  15870. is_clicked = nk_input_mouse_clicked(&ctx->input, NK_BUTTON_RIGHT, trigger_bounds);
  15871. if (win->popup.active_con && win->popup.con_count != win->popup.active_con)
  15872. return 0;
  15873. if (!is_open && win->popup.active_con)
  15874. win->popup.active_con = 0;
  15875. if ((!is_open && !is_clicked))
  15876. return 0;
  15877. /* calculate contextual position on click */
  15878. win->popup.active_con = win->popup.con_count;
  15879. if (is_clicked) {
  15880. body.x = ctx->input.mouse.pos.x;
  15881. body.y = ctx->input.mouse.pos.y;
  15882. } else {
  15883. body.x = popup->bounds.x;
  15884. body.y = popup->bounds.y;
  15885. }
  15886. body.w = size.x;
  15887. body.h = size.y;
  15888. /* start nonblocking contextual popup */
  15889. ret = nk_nonblock_begin(ctx, flags|NK_WINDOW_NO_SCROLLBAR, body,
  15890. null_rect, NK_PANEL_CONTEXTUAL);
  15891. if (ret) win->popup.type = NK_PANEL_CONTEXTUAL;
  15892. else {
  15893. win->popup.active_con = 0;
  15894. win->popup.type = NK_PANEL_NONE;
  15895. if (win->popup.win)
  15896. win->popup.win->flags = 0;
  15897. }
  15898. return ret;
  15899. }
  15900. NK_API int
  15901. nk_contextual_item_text(struct nk_context *ctx, const char *text, int len,
  15902. nk_flags alignment)
  15903. {
  15904. struct nk_window *win;
  15905. const struct nk_input *in;
  15906. const struct nk_style *style;
  15907. struct nk_rect bounds;
  15908. enum nk_widget_layout_states state;
  15909. NK_ASSERT(ctx);
  15910. NK_ASSERT(ctx->current);
  15911. NK_ASSERT(ctx->current->layout);
  15912. if (!ctx || !ctx->current || !ctx->current->layout)
  15913. return 0;
  15914. win = ctx->current;
  15915. style = &ctx->style;
  15916. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  15917. if (!state) return nk_false;
  15918. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  15919. if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
  15920. text, len, alignment, NK_BUTTON_DEFAULT, &style->contextual_button, in, style->font)) {
  15921. nk_contextual_close(ctx);
  15922. return nk_true;
  15923. }
  15924. return nk_false;
  15925. }
  15926. NK_API int
  15927. nk_contextual_item_label(struct nk_context *ctx, const char *label, nk_flags align)
  15928. {
  15929. return nk_contextual_item_text(ctx, label, nk_strlen(label), align);
  15930. }
  15931. NK_API int
  15932. nk_contextual_item_image_text(struct nk_context *ctx, struct nk_image img,
  15933. const char *text, int len, nk_flags align)
  15934. {
  15935. struct nk_window *win;
  15936. const struct nk_input *in;
  15937. const struct nk_style *style;
  15938. struct nk_rect bounds;
  15939. enum nk_widget_layout_states state;
  15940. NK_ASSERT(ctx);
  15941. NK_ASSERT(ctx->current);
  15942. NK_ASSERT(ctx->current->layout);
  15943. if (!ctx || !ctx->current || !ctx->current->layout)
  15944. return 0;
  15945. win = ctx->current;
  15946. style = &ctx->style;
  15947. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  15948. if (!state) return nk_false;
  15949. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  15950. if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer, bounds,
  15951. img, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)){
  15952. nk_contextual_close(ctx);
  15953. return nk_true;
  15954. }
  15955. return nk_false;
  15956. }
  15957. NK_API int
  15958. nk_contextual_item_image_label(struct nk_context *ctx, struct nk_image img,
  15959. const char *label, nk_flags align)
  15960. {
  15961. return nk_contextual_item_image_text(ctx, img, label, nk_strlen(label), align);
  15962. }
  15963. NK_API int
  15964. nk_contextual_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type symbol,
  15965. const char *text, int len, nk_flags align)
  15966. {
  15967. struct nk_window *win;
  15968. const struct nk_input *in;
  15969. const struct nk_style *style;
  15970. struct nk_rect bounds;
  15971. enum nk_widget_layout_states state;
  15972. NK_ASSERT(ctx);
  15973. NK_ASSERT(ctx->current);
  15974. NK_ASSERT(ctx->current->layout);
  15975. if (!ctx || !ctx->current || !ctx->current->layout)
  15976. return 0;
  15977. win = ctx->current;
  15978. style = &ctx->style;
  15979. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  15980. if (!state) return nk_false;
  15981. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  15982. if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  15983. symbol, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)) {
  15984. nk_contextual_close(ctx);
  15985. return nk_true;
  15986. }
  15987. return nk_false;
  15988. }
  15989. NK_API int
  15990. nk_contextual_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type symbol,
  15991. const char *text, nk_flags align)
  15992. {
  15993. return nk_contextual_item_symbol_text(ctx, symbol, text, nk_strlen(text), align);
  15994. }
  15995. NK_API void
  15996. nk_contextual_close(struct nk_context *ctx)
  15997. {
  15998. NK_ASSERT(ctx);
  15999. NK_ASSERT(ctx->current);
  16000. NK_ASSERT(ctx->current->layout);
  16001. if (!ctx || !ctx->current || !ctx->current->layout) return;
  16002. nk_popup_close(ctx);
  16003. }
  16004. NK_API void
  16005. nk_contextual_end(struct nk_context *ctx)
  16006. {
  16007. struct nk_window *popup;
  16008. struct nk_panel *panel;
  16009. NK_ASSERT(ctx);
  16010. NK_ASSERT(ctx->current);
  16011. if (!ctx || !ctx->current) return;
  16012. popup = ctx->current;
  16013. panel = popup->layout;
  16014. NK_ASSERT(popup->parent);
  16015. NK_ASSERT(panel->type & NK_PANEL_SET_POPUP);
  16016. if (panel->flags & NK_WINDOW_DYNAMIC) {
  16017. /* Close behavior
  16018. This is a bit of a hack solution since we do not know before we end our popup
  16019. how big it will be. We therefore do not directly know when a
  16020. click outside the non-blocking popup must close it at that direct frame.
  16021. Instead it will be closed in the next frame.*/
  16022. struct nk_rect body = {0,0,0,0};
  16023. if (panel->at_y < (panel->bounds.y + panel->bounds.h)) {
  16024. struct nk_vec2 padding = nk_panel_get_padding(&ctx->style, panel->type);
  16025. body = panel->bounds;
  16026. body.y = (panel->at_y + panel->footer_height + panel->border + padding.y + panel->row.height);
  16027. body.h = (panel->bounds.y + panel->bounds.h) - body.y;
  16028. }
  16029. {int pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
  16030. int in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
  16031. if (pressed && in_body)
  16032. popup->flags |= NK_WINDOW_HIDDEN;
  16033. }
  16034. }
  16035. if (popup->flags & NK_WINDOW_HIDDEN)
  16036. popup->seq = 0;
  16037. nk_popup_end(ctx);
  16038. return;
  16039. }
  16040. /* ===============================================================
  16041. *
  16042. * MENU
  16043. *
  16044. * ===============================================================*/
  16045. NK_API void
  16046. nk_menubar_begin(struct nk_context *ctx)
  16047. {
  16048. struct nk_panel *layout;
  16049. NK_ASSERT(ctx);
  16050. NK_ASSERT(ctx->current);
  16051. NK_ASSERT(ctx->current->layout);
  16052. if (!ctx || !ctx->current || !ctx->current->layout)
  16053. return;
  16054. layout = ctx->current->layout;
  16055. NK_ASSERT(layout->at_y == layout->bounds.y);
  16056. /* if this assert triggers you allocated space between nk_begin and nk_menubar_begin.
  16057. If you want a menubar the first nuklear function after `nk_begin` has to be a
  16058. `nk_menubar_begin` call. Inside the menubar you then have to allocate space for
  16059. widgets (also supports multiple rows).
  16060. Example:
  16061. if (nk_begin(...)) {
  16062. nk_menubar_begin(...);
  16063. nk_layout_xxxx(...);
  16064. nk_button(...);
  16065. nk_layout_xxxx(...);
  16066. nk_button(...);
  16067. nk_menubar_end(...);
  16068. }
  16069. nk_end(...);
  16070. */
  16071. if (layout->flags & NK_WINDOW_HIDDEN || layout->flags & NK_WINDOW_MINIMIZED)
  16072. return;
  16073. layout->menu.x = layout->at_x;
  16074. layout->menu.y = layout->at_y + layout->row.height;
  16075. layout->menu.w = layout->bounds.w;
  16076. layout->menu.offset.x = *layout->offset_x;
  16077. layout->menu.offset.y = *layout->offset_y;
  16078. *layout->offset_y = 0;
  16079. }
  16080. NK_API void
  16081. nk_menubar_end(struct nk_context *ctx)
  16082. {
  16083. struct nk_window *win;
  16084. struct nk_panel *layout;
  16085. struct nk_command_buffer *out;
  16086. NK_ASSERT(ctx);
  16087. NK_ASSERT(ctx->current);
  16088. NK_ASSERT(ctx->current->layout);
  16089. if (!ctx || !ctx->current || !ctx->current->layout)
  16090. return;
  16091. win = ctx->current;
  16092. out = &win->buffer;
  16093. layout = win->layout;
  16094. if (layout->flags & NK_WINDOW_HIDDEN || layout->flags & NK_WINDOW_MINIMIZED)
  16095. return;
  16096. layout->menu.h = layout->at_y - layout->menu.y;
  16097. layout->bounds.y += layout->menu.h + ctx->style.window.spacing.y + layout->row.height;
  16098. layout->bounds.h -= layout->menu.h + ctx->style.window.spacing.y + layout->row.height;
  16099. *layout->offset_x = layout->menu.offset.x;
  16100. *layout->offset_y = layout->menu.offset.y;
  16101. layout->at_y = layout->bounds.y - layout->row.height;
  16102. layout->clip.y = layout->bounds.y;
  16103. layout->clip.h = layout->bounds.h;
  16104. nk_push_scissor(out, layout->clip);
  16105. }
  16106. NK_INTERN int
  16107. nk_menu_begin(struct nk_context *ctx, struct nk_window *win,
  16108. const char *id, int is_clicked, struct nk_rect header, struct nk_vec2 size)
  16109. {
  16110. int is_open = 0;
  16111. int is_active = 0;
  16112. struct nk_rect body;
  16113. struct nk_window *popup;
  16114. nk_hash hash = nk_murmur_hash(id, (int)nk_strlen(id), NK_PANEL_MENU);
  16115. NK_ASSERT(ctx);
  16116. NK_ASSERT(ctx->current);
  16117. NK_ASSERT(ctx->current->layout);
  16118. if (!ctx || !ctx->current || !ctx->current->layout)
  16119. return 0;
  16120. body.x = header.x;
  16121. body.w = size.x;
  16122. body.y = header.y + header.h;
  16123. body.h = size.y;
  16124. popup = win->popup.win;
  16125. is_open = popup ? nk_true : nk_false;
  16126. is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_MENU);
  16127. if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
  16128. (!is_open && !is_active && !is_clicked)) return 0;
  16129. if (!nk_nonblock_begin(ctx, NK_WINDOW_NO_SCROLLBAR, body, header, NK_PANEL_MENU))
  16130. return 0;
  16131. win->popup.type = NK_PANEL_MENU;
  16132. win->popup.name = hash;
  16133. return 1;
  16134. }
  16135. NK_API int
  16136. nk_menu_begin_text(struct nk_context *ctx, const char *title, int len,
  16137. nk_flags align, struct nk_vec2 size)
  16138. {
  16139. struct nk_window *win;
  16140. const struct nk_input *in;
  16141. struct nk_rect header;
  16142. int is_clicked = nk_false;
  16143. nk_flags state;
  16144. NK_ASSERT(ctx);
  16145. NK_ASSERT(ctx->current);
  16146. NK_ASSERT(ctx->current->layout);
  16147. if (!ctx || !ctx->current || !ctx->current->layout)
  16148. return 0;
  16149. win = ctx->current;
  16150. state = nk_widget(&header, ctx);
  16151. if (!state) return 0;
  16152. in = (state == NK_WIDGET_ROM || win->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  16153. if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, header,
  16154. title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
  16155. is_clicked = nk_true;
  16156. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  16157. }
  16158. NK_API int nk_menu_begin_label(struct nk_context *ctx,
  16159. const char *text, nk_flags align, struct nk_vec2 size)
  16160. {
  16161. return nk_menu_begin_text(ctx, text, nk_strlen(text), align, size);
  16162. }
  16163. NK_API int
  16164. nk_menu_begin_image(struct nk_context *ctx, const char *id, struct nk_image img,
  16165. struct nk_vec2 size)
  16166. {
  16167. struct nk_window *win;
  16168. struct nk_rect header;
  16169. const struct nk_input *in;
  16170. int is_clicked = nk_false;
  16171. nk_flags state;
  16172. NK_ASSERT(ctx);
  16173. NK_ASSERT(ctx->current);
  16174. NK_ASSERT(ctx->current->layout);
  16175. if (!ctx || !ctx->current || !ctx->current->layout)
  16176. return 0;
  16177. win = ctx->current;
  16178. state = nk_widget(&header, ctx);
  16179. if (!state) return 0;
  16180. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  16181. if (nk_do_button_image(&ctx->last_widget_state, &win->buffer, header,
  16182. img, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in))
  16183. is_clicked = nk_true;
  16184. return nk_menu_begin(ctx, win, id, is_clicked, header, size);
  16185. }
  16186. NK_API int
  16187. nk_menu_begin_symbol(struct nk_context *ctx, const char *id,
  16188. enum nk_symbol_type sym, struct nk_vec2 size)
  16189. {
  16190. struct nk_window *win;
  16191. const struct nk_input *in;
  16192. struct nk_rect header;
  16193. int is_clicked = nk_false;
  16194. nk_flags state;
  16195. NK_ASSERT(ctx);
  16196. NK_ASSERT(ctx->current);
  16197. NK_ASSERT(ctx->current->layout);
  16198. if (!ctx || !ctx->current || !ctx->current->layout)
  16199. return 0;
  16200. win = ctx->current;
  16201. state = nk_widget(&header, ctx);
  16202. if (!state) return 0;
  16203. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  16204. if (nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, header,
  16205. sym, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
  16206. is_clicked = nk_true;
  16207. return nk_menu_begin(ctx, win, id, is_clicked, header, size);
  16208. }
  16209. NK_API int
  16210. nk_menu_begin_image_text(struct nk_context *ctx, const char *title, int len,
  16211. nk_flags align, struct nk_image img, struct nk_vec2 size)
  16212. {
  16213. struct nk_window *win;
  16214. struct nk_rect header;
  16215. const struct nk_input *in;
  16216. int is_clicked = nk_false;
  16217. nk_flags state;
  16218. NK_ASSERT(ctx);
  16219. NK_ASSERT(ctx->current);
  16220. NK_ASSERT(ctx->current->layout);
  16221. if (!ctx || !ctx->current || !ctx->current->layout)
  16222. return 0;
  16223. win = ctx->current;
  16224. state = nk_widget(&header, ctx);
  16225. if (!state) return 0;
  16226. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  16227. if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
  16228. header, img, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
  16229. ctx->style.font, in))
  16230. is_clicked = nk_true;
  16231. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  16232. }
  16233. NK_API int
  16234. nk_menu_begin_image_label(struct nk_context *ctx,
  16235. const char *title, nk_flags align, struct nk_image img, struct nk_vec2 size)
  16236. {
  16237. return nk_menu_begin_image_text(ctx, title, nk_strlen(title), align, img, size);
  16238. }
  16239. NK_API int
  16240. nk_menu_begin_symbol_text(struct nk_context *ctx, const char *title, int len,
  16241. nk_flags align, enum nk_symbol_type sym, struct nk_vec2 size)
  16242. {
  16243. struct nk_window *win;
  16244. struct nk_rect header;
  16245. const struct nk_input *in;
  16246. int is_clicked = nk_false;
  16247. nk_flags state;
  16248. NK_ASSERT(ctx);
  16249. NK_ASSERT(ctx->current);
  16250. NK_ASSERT(ctx->current->layout);
  16251. if (!ctx || !ctx->current || !ctx->current->layout)
  16252. return 0;
  16253. win = ctx->current;
  16254. state = nk_widget(&header, ctx);
  16255. if (!state) return 0;
  16256. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  16257. if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer,
  16258. header, sym, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
  16259. ctx->style.font, in)) is_clicked = nk_true;
  16260. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  16261. }
  16262. NK_API int
  16263. nk_menu_begin_symbol_label(struct nk_context *ctx,
  16264. const char *title, nk_flags align, enum nk_symbol_type sym, struct nk_vec2 size )
  16265. {
  16266. return nk_menu_begin_symbol_text(ctx, title, nk_strlen(title), align,sym,size);
  16267. }
  16268. NK_API int
  16269. nk_menu_item_text(struct nk_context *ctx, const char *title, int len, nk_flags align)
  16270. {
  16271. return nk_contextual_item_text(ctx, title, len, align);
  16272. }
  16273. NK_API int
  16274. nk_menu_item_label(struct nk_context *ctx, const char *label, nk_flags align)
  16275. {
  16276. return nk_contextual_item_label(ctx, label, align);
  16277. }
  16278. NK_API int
  16279. nk_menu_item_image_label(struct nk_context *ctx, struct nk_image img,
  16280. const char *label, nk_flags align)
  16281. {
  16282. return nk_contextual_item_image_label(ctx, img, label, align);
  16283. }
  16284. NK_API int
  16285. nk_menu_item_image_text(struct nk_context *ctx, struct nk_image img,
  16286. const char *text, int len, nk_flags align)
  16287. {
  16288. return nk_contextual_item_image_text(ctx, img, text, len, align);
  16289. }
  16290. NK_API int nk_menu_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  16291. const char *text, int len, nk_flags align)
  16292. {
  16293. return nk_contextual_item_symbol_text(ctx, sym, text, len, align);
  16294. }
  16295. NK_API int nk_menu_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  16296. const char *label, nk_flags align)
  16297. {
  16298. return nk_contextual_item_symbol_label(ctx, sym, label, align);
  16299. }
  16300. NK_API void nk_menu_close(struct nk_context *ctx)
  16301. {
  16302. nk_contextual_close(ctx);
  16303. }
  16304. NK_API void
  16305. nk_menu_end(struct nk_context *ctx)
  16306. {
  16307. nk_contextual_end(ctx);
  16308. }
  16309. /* ===============================================================
  16310. *
  16311. * LAYOUT
  16312. *
  16313. * ===============================================================*/
  16314. NK_API void
  16315. nk_layout_set_min_row_height(struct nk_context *ctx, float height)
  16316. {
  16317. struct nk_window *win;
  16318. struct nk_panel *layout;
  16319. NK_ASSERT(ctx);
  16320. NK_ASSERT(ctx->current);
  16321. NK_ASSERT(ctx->current->layout);
  16322. if (!ctx || !ctx->current || !ctx->current->layout)
  16323. return;
  16324. win = ctx->current;
  16325. layout = win->layout;
  16326. layout->row.min_height = height;
  16327. }
  16328. NK_API void
  16329. nk_layout_reset_min_row_height(struct nk_context *ctx)
  16330. {
  16331. struct nk_window *win;
  16332. struct nk_panel *layout;
  16333. NK_ASSERT(ctx);
  16334. NK_ASSERT(ctx->current);
  16335. NK_ASSERT(ctx->current->layout);
  16336. if (!ctx || !ctx->current || !ctx->current->layout)
  16337. return;
  16338. win = ctx->current;
  16339. layout = win->layout;
  16340. layout->row.min_height = ctx->style.font->height;
  16341. layout->row.min_height += ctx->style.text.padding.y*2;
  16342. layout->row.min_height += ctx->style.window.min_row_height_padding*2;
  16343. }
  16344. NK_LIB float
  16345. nk_layout_row_calculate_usable_space(const struct nk_style *style, enum nk_panel_type type,
  16346. float total_space, int columns)
  16347. {
  16348. float panel_padding;
  16349. float panel_spacing;
  16350. float panel_space;
  16351. struct nk_vec2 spacing;
  16352. struct nk_vec2 padding;
  16353. spacing = style->window.spacing;
  16354. padding = nk_panel_get_padding(style, type);
  16355. /* calculate the usable panel space */
  16356. panel_padding = 2 * padding.x;
  16357. panel_spacing = (float)NK_MAX(columns - 1, 0) * spacing.x;
  16358. panel_space = total_space - panel_padding - panel_spacing;
  16359. return panel_space;
  16360. }
  16361. NK_LIB void
  16362. nk_panel_layout(const struct nk_context *ctx, struct nk_window *win,
  16363. float height, int cols)
  16364. {
  16365. struct nk_panel *layout;
  16366. const struct nk_style *style;
  16367. struct nk_command_buffer *out;
  16368. struct nk_vec2 item_spacing;
  16369. struct nk_color color;
  16370. NK_ASSERT(ctx);
  16371. NK_ASSERT(ctx->current);
  16372. NK_ASSERT(ctx->current->layout);
  16373. if (!ctx || !ctx->current || !ctx->current->layout)
  16374. return;
  16375. /* prefetch some configuration data */
  16376. layout = win->layout;
  16377. style = &ctx->style;
  16378. out = &win->buffer;
  16379. color = style->window.background;
  16380. item_spacing = style->window.spacing;
  16381. /* if one of these triggers you forgot to add an `if` condition around either
  16382. a window, group, popup, combobox or contextual menu `begin` and `end` block.
  16383. Example:
  16384. if (nk_begin(...) {...} nk_end(...); or
  16385. if (nk_group_begin(...) { nk_group_end(...);} */
  16386. NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
  16387. NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
  16388. NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
  16389. /* update the current row and set the current row layout */
  16390. layout->row.index = 0;
  16391. layout->at_y += layout->row.height;
  16392. layout->row.columns = cols;
  16393. if (height == 0.0f)
  16394. layout->row.height = NK_MAX(height, layout->row.min_height) + item_spacing.y;
  16395. else layout->row.height = height + item_spacing.y;
  16396. layout->row.item_offset = 0;
  16397. if (layout->flags & NK_WINDOW_DYNAMIC) {
  16398. /* draw background for dynamic panels */
  16399. struct nk_rect background;
  16400. background.x = win->bounds.x;
  16401. background.w = win->bounds.w;
  16402. background.y = layout->at_y - 1.0f;
  16403. background.h = layout->row.height + 1.0f;
  16404. nk_fill_rect(out, background, 0, color);
  16405. }
  16406. }
  16407. NK_LIB void
  16408. nk_row_layout(struct nk_context *ctx, enum nk_layout_format fmt,
  16409. float height, int cols, int width)
  16410. {
  16411. /* update the current row and set the current row layout */
  16412. struct nk_window *win;
  16413. NK_ASSERT(ctx);
  16414. NK_ASSERT(ctx->current);
  16415. NK_ASSERT(ctx->current->layout);
  16416. if (!ctx || !ctx->current || !ctx->current->layout)
  16417. return;
  16418. win = ctx->current;
  16419. nk_panel_layout(ctx, win, height, cols);
  16420. if (fmt == NK_DYNAMIC)
  16421. win->layout->row.type = NK_LAYOUT_DYNAMIC_FIXED;
  16422. else win->layout->row.type = NK_LAYOUT_STATIC_FIXED;
  16423. win->layout->row.ratio = 0;
  16424. win->layout->row.filled = 0;
  16425. win->layout->row.item_offset = 0;
  16426. win->layout->row.item_width = (float)width;
  16427. }
  16428. NK_API float
  16429. nk_layout_ratio_from_pixel(struct nk_context *ctx, float pixel_width)
  16430. {
  16431. struct nk_window *win;
  16432. NK_ASSERT(ctx);
  16433. NK_ASSERT(pixel_width);
  16434. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  16435. win = ctx->current;
  16436. return NK_CLAMP(0.0f, pixel_width/win->bounds.x, 1.0f);
  16437. }
  16438. NK_API void
  16439. nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols)
  16440. {
  16441. nk_row_layout(ctx, NK_DYNAMIC, height, cols, 0);
  16442. }
  16443. NK_API void
  16444. nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols)
  16445. {
  16446. nk_row_layout(ctx, NK_STATIC, height, cols, item_width);
  16447. }
  16448. NK_API void
  16449. nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt,
  16450. float row_height, int cols)
  16451. {
  16452. struct nk_window *win;
  16453. struct nk_panel *layout;
  16454. NK_ASSERT(ctx);
  16455. NK_ASSERT(ctx->current);
  16456. NK_ASSERT(ctx->current->layout);
  16457. if (!ctx || !ctx->current || !ctx->current->layout)
  16458. return;
  16459. win = ctx->current;
  16460. layout = win->layout;
  16461. nk_panel_layout(ctx, win, row_height, cols);
  16462. if (fmt == NK_DYNAMIC)
  16463. layout->row.type = NK_LAYOUT_DYNAMIC_ROW;
  16464. else layout->row.type = NK_LAYOUT_STATIC_ROW;
  16465. layout->row.ratio = 0;
  16466. layout->row.filled = 0;
  16467. layout->row.item_width = 0;
  16468. layout->row.item_offset = 0;
  16469. layout->row.columns = cols;
  16470. }
  16471. NK_API void
  16472. nk_layout_row_push(struct nk_context *ctx, float ratio_or_width)
  16473. {
  16474. struct nk_window *win;
  16475. struct nk_panel *layout;
  16476. NK_ASSERT(ctx);
  16477. NK_ASSERT(ctx->current);
  16478. NK_ASSERT(ctx->current->layout);
  16479. if (!ctx || !ctx->current || !ctx->current->layout)
  16480. return;
  16481. win = ctx->current;
  16482. layout = win->layout;
  16483. NK_ASSERT(layout->row.type == NK_LAYOUT_STATIC_ROW || layout->row.type == NK_LAYOUT_DYNAMIC_ROW);
  16484. if (layout->row.type != NK_LAYOUT_STATIC_ROW && layout->row.type != NK_LAYOUT_DYNAMIC_ROW)
  16485. return;
  16486. if (layout->row.type == NK_LAYOUT_DYNAMIC_ROW) {
  16487. float ratio = ratio_or_width;
  16488. if ((ratio + layout->row.filled) > 1.0f) return;
  16489. if (ratio > 0.0f)
  16490. layout->row.item_width = NK_SATURATE(ratio);
  16491. else layout->row.item_width = 1.0f - layout->row.filled;
  16492. } else layout->row.item_width = ratio_or_width;
  16493. }
  16494. NK_API void
  16495. nk_layout_row_end(struct nk_context *ctx)
  16496. {
  16497. struct nk_window *win;
  16498. struct nk_panel *layout;
  16499. NK_ASSERT(ctx);
  16500. NK_ASSERT(ctx->current);
  16501. NK_ASSERT(ctx->current->layout);
  16502. if (!ctx || !ctx->current || !ctx->current->layout)
  16503. return;
  16504. win = ctx->current;
  16505. layout = win->layout;
  16506. NK_ASSERT(layout->row.type == NK_LAYOUT_STATIC_ROW || layout->row.type == NK_LAYOUT_DYNAMIC_ROW);
  16507. if (layout->row.type != NK_LAYOUT_STATIC_ROW && layout->row.type != NK_LAYOUT_DYNAMIC_ROW)
  16508. return;
  16509. layout->row.item_width = 0;
  16510. layout->row.item_offset = 0;
  16511. }
  16512. NK_API void
  16513. nk_layout_row(struct nk_context *ctx, enum nk_layout_format fmt,
  16514. float height, int cols, const float *ratio)
  16515. {
  16516. int i;
  16517. int n_undef = 0;
  16518. struct nk_window *win;
  16519. struct nk_panel *layout;
  16520. NK_ASSERT(ctx);
  16521. NK_ASSERT(ctx->current);
  16522. NK_ASSERT(ctx->current->layout);
  16523. if (!ctx || !ctx->current || !ctx->current->layout)
  16524. return;
  16525. win = ctx->current;
  16526. layout = win->layout;
  16527. nk_panel_layout(ctx, win, height, cols);
  16528. if (fmt == NK_DYNAMIC) {
  16529. /* calculate width of undefined widget ratios */
  16530. float r = 0;
  16531. layout->row.ratio = ratio;
  16532. for (i = 0; i < cols; ++i) {
  16533. if (ratio[i] < 0.0f)
  16534. n_undef++;
  16535. else r += ratio[i];
  16536. }
  16537. r = NK_SATURATE(1.0f - r);
  16538. layout->row.type = NK_LAYOUT_DYNAMIC;
  16539. layout->row.item_width = (r > 0 && n_undef > 0) ? (r / (float)n_undef):0;
  16540. } else {
  16541. layout->row.ratio = ratio;
  16542. layout->row.type = NK_LAYOUT_STATIC;
  16543. layout->row.item_width = 0;
  16544. layout->row.item_offset = 0;
  16545. }
  16546. layout->row.item_offset = 0;
  16547. layout->row.filled = 0;
  16548. }
  16549. NK_API void
  16550. nk_layout_row_template_begin(struct nk_context *ctx, float height)
  16551. {
  16552. struct nk_window *win;
  16553. struct nk_panel *layout;
  16554. NK_ASSERT(ctx);
  16555. NK_ASSERT(ctx->current);
  16556. NK_ASSERT(ctx->current->layout);
  16557. if (!ctx || !ctx->current || !ctx->current->layout)
  16558. return;
  16559. win = ctx->current;
  16560. layout = win->layout;
  16561. nk_panel_layout(ctx, win, height, 1);
  16562. layout->row.type = NK_LAYOUT_TEMPLATE;
  16563. layout->row.columns = 0;
  16564. layout->row.ratio = 0;
  16565. layout->row.item_width = 0;
  16566. layout->row.item_height = 0;
  16567. layout->row.item_offset = 0;
  16568. layout->row.filled = 0;
  16569. layout->row.item.x = 0;
  16570. layout->row.item.y = 0;
  16571. layout->row.item.w = 0;
  16572. layout->row.item.h = 0;
  16573. }
  16574. NK_API void
  16575. nk_layout_row_template_push_dynamic(struct nk_context *ctx)
  16576. {
  16577. struct nk_window *win;
  16578. struct nk_panel *layout;
  16579. NK_ASSERT(ctx);
  16580. NK_ASSERT(ctx->current);
  16581. NK_ASSERT(ctx->current->layout);
  16582. if (!ctx || !ctx->current || !ctx->current->layout)
  16583. return;
  16584. win = ctx->current;
  16585. layout = win->layout;
  16586. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  16587. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  16588. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  16589. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  16590. layout->row.templates[layout->row.columns++] = -1.0f;
  16591. }
  16592. NK_API void
  16593. nk_layout_row_template_push_variable(struct nk_context *ctx, float min_width)
  16594. {
  16595. struct nk_window *win;
  16596. struct nk_panel *layout;
  16597. NK_ASSERT(ctx);
  16598. NK_ASSERT(ctx->current);
  16599. NK_ASSERT(ctx->current->layout);
  16600. if (!ctx || !ctx->current || !ctx->current->layout)
  16601. return;
  16602. win = ctx->current;
  16603. layout = win->layout;
  16604. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  16605. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  16606. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  16607. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  16608. layout->row.templates[layout->row.columns++] = -min_width;
  16609. }
  16610. NK_API void
  16611. nk_layout_row_template_push_static(struct nk_context *ctx, float width)
  16612. {
  16613. struct nk_window *win;
  16614. struct nk_panel *layout;
  16615. NK_ASSERT(ctx);
  16616. NK_ASSERT(ctx->current);
  16617. NK_ASSERT(ctx->current->layout);
  16618. if (!ctx || !ctx->current || !ctx->current->layout)
  16619. return;
  16620. win = ctx->current;
  16621. layout = win->layout;
  16622. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  16623. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  16624. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  16625. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  16626. layout->row.templates[layout->row.columns++] = width;
  16627. }
  16628. NK_API void
  16629. nk_layout_row_template_end(struct nk_context *ctx)
  16630. {
  16631. struct nk_window *win;
  16632. struct nk_panel *layout;
  16633. int i = 0;
  16634. int variable_count = 0;
  16635. int min_variable_count = 0;
  16636. float min_fixed_width = 0.0f;
  16637. float total_fixed_width = 0.0f;
  16638. float max_variable_width = 0.0f;
  16639. NK_ASSERT(ctx);
  16640. NK_ASSERT(ctx->current);
  16641. NK_ASSERT(ctx->current->layout);
  16642. if (!ctx || !ctx->current || !ctx->current->layout)
  16643. return;
  16644. win = ctx->current;
  16645. layout = win->layout;
  16646. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  16647. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  16648. for (i = 0; i < layout->row.columns; ++i) {
  16649. float width = layout->row.templates[i];
  16650. if (width >= 0.0f) {
  16651. total_fixed_width += width;
  16652. min_fixed_width += width;
  16653. } else if (width < -1.0f) {
  16654. width = -width;
  16655. total_fixed_width += width;
  16656. max_variable_width = NK_MAX(max_variable_width, width);
  16657. variable_count++;
  16658. } else {
  16659. min_variable_count++;
  16660. variable_count++;
  16661. }
  16662. }
  16663. if (variable_count) {
  16664. float space = nk_layout_row_calculate_usable_space(&ctx->style, layout->type,
  16665. layout->bounds.w, layout->row.columns);
  16666. float var_width = (NK_MAX(space-min_fixed_width,0.0f)) / (float)variable_count;
  16667. int enough_space = var_width >= max_variable_width;
  16668. if (!enough_space)
  16669. var_width = (NK_MAX(space-total_fixed_width,0)) / (float)min_variable_count;
  16670. for (i = 0; i < layout->row.columns; ++i) {
  16671. float *width = &layout->row.templates[i];
  16672. *width = (*width >= 0.0f)? *width: (*width < -1.0f && !enough_space)? -(*width): var_width;
  16673. }
  16674. }
  16675. }
  16676. NK_API void
  16677. nk_layout_space_begin(struct nk_context *ctx, enum nk_layout_format fmt,
  16678. float height, int widget_count)
  16679. {
  16680. struct nk_window *win;
  16681. struct nk_panel *layout;
  16682. NK_ASSERT(ctx);
  16683. NK_ASSERT(ctx->current);
  16684. NK_ASSERT(ctx->current->layout);
  16685. if (!ctx || !ctx->current || !ctx->current->layout)
  16686. return;
  16687. win = ctx->current;
  16688. layout = win->layout;
  16689. nk_panel_layout(ctx, win, height, widget_count);
  16690. if (fmt == NK_STATIC)
  16691. layout->row.type = NK_LAYOUT_STATIC_FREE;
  16692. else layout->row.type = NK_LAYOUT_DYNAMIC_FREE;
  16693. layout->row.ratio = 0;
  16694. layout->row.filled = 0;
  16695. layout->row.item_width = 0;
  16696. layout->row.item_offset = 0;
  16697. }
  16698. NK_API void
  16699. nk_layout_space_end(struct nk_context *ctx)
  16700. {
  16701. struct nk_window *win;
  16702. struct nk_panel *layout;
  16703. NK_ASSERT(ctx);
  16704. NK_ASSERT(ctx->current);
  16705. NK_ASSERT(ctx->current->layout);
  16706. if (!ctx || !ctx->current || !ctx->current->layout)
  16707. return;
  16708. win = ctx->current;
  16709. layout = win->layout;
  16710. layout->row.item_width = 0;
  16711. layout->row.item_height = 0;
  16712. layout->row.item_offset = 0;
  16713. nk_zero(&layout->row.item, sizeof(layout->row.item));
  16714. }
  16715. NK_API void
  16716. nk_layout_space_push(struct nk_context *ctx, struct nk_rect rect)
  16717. {
  16718. struct nk_window *win;
  16719. struct nk_panel *layout;
  16720. NK_ASSERT(ctx);
  16721. NK_ASSERT(ctx->current);
  16722. NK_ASSERT(ctx->current->layout);
  16723. if (!ctx || !ctx->current || !ctx->current->layout)
  16724. return;
  16725. win = ctx->current;
  16726. layout = win->layout;
  16727. layout->row.item = rect;
  16728. }
  16729. NK_API struct nk_rect
  16730. nk_layout_space_bounds(struct nk_context *ctx)
  16731. {
  16732. struct nk_rect ret;
  16733. struct nk_window *win;
  16734. struct nk_panel *layout;
  16735. NK_ASSERT(ctx);
  16736. NK_ASSERT(ctx->current);
  16737. NK_ASSERT(ctx->current->layout);
  16738. win = ctx->current;
  16739. layout = win->layout;
  16740. ret.x = layout->clip.x;
  16741. ret.y = layout->clip.y;
  16742. ret.w = layout->clip.w;
  16743. ret.h = layout->row.height;
  16744. return ret;
  16745. }
  16746. NK_API struct nk_rect
  16747. nk_layout_widget_bounds(struct nk_context *ctx)
  16748. {
  16749. struct nk_rect ret;
  16750. struct nk_window *win;
  16751. struct nk_panel *layout;
  16752. NK_ASSERT(ctx);
  16753. NK_ASSERT(ctx->current);
  16754. NK_ASSERT(ctx->current->layout);
  16755. win = ctx->current;
  16756. layout = win->layout;
  16757. ret.x = layout->at_x;
  16758. ret.y = layout->at_y;
  16759. ret.w = layout->bounds.w - NK_MAX(layout->at_x - layout->bounds.x,0);
  16760. ret.h = layout->row.height;
  16761. return ret;
  16762. }
  16763. NK_API struct nk_vec2
  16764. nk_layout_space_to_screen(struct nk_context *ctx, struct nk_vec2 ret)
  16765. {
  16766. struct nk_window *win;
  16767. struct nk_panel *layout;
  16768. NK_ASSERT(ctx);
  16769. NK_ASSERT(ctx->current);
  16770. NK_ASSERT(ctx->current->layout);
  16771. win = ctx->current;
  16772. layout = win->layout;
  16773. ret.x += layout->at_x - (float)*layout->offset_x;
  16774. ret.y += layout->at_y - (float)*layout->offset_y;
  16775. return ret;
  16776. }
  16777. NK_API struct nk_vec2
  16778. nk_layout_space_to_local(struct nk_context *ctx, struct nk_vec2 ret)
  16779. {
  16780. struct nk_window *win;
  16781. struct nk_panel *layout;
  16782. NK_ASSERT(ctx);
  16783. NK_ASSERT(ctx->current);
  16784. NK_ASSERT(ctx->current->layout);
  16785. win = ctx->current;
  16786. layout = win->layout;
  16787. ret.x += -layout->at_x + (float)*layout->offset_x;
  16788. ret.y += -layout->at_y + (float)*layout->offset_y;
  16789. return ret;
  16790. }
  16791. NK_API struct nk_rect
  16792. nk_layout_space_rect_to_screen(struct nk_context *ctx, struct nk_rect ret)
  16793. {
  16794. struct nk_window *win;
  16795. struct nk_panel *layout;
  16796. NK_ASSERT(ctx);
  16797. NK_ASSERT(ctx->current);
  16798. NK_ASSERT(ctx->current->layout);
  16799. win = ctx->current;
  16800. layout = win->layout;
  16801. ret.x += layout->at_x - (float)*layout->offset_x;
  16802. ret.y += layout->at_y - (float)*layout->offset_y;
  16803. return ret;
  16804. }
  16805. NK_API struct nk_rect
  16806. nk_layout_space_rect_to_local(struct nk_context *ctx, struct nk_rect ret)
  16807. {
  16808. struct nk_window *win;
  16809. struct nk_panel *layout;
  16810. NK_ASSERT(ctx);
  16811. NK_ASSERT(ctx->current);
  16812. NK_ASSERT(ctx->current->layout);
  16813. win = ctx->current;
  16814. layout = win->layout;
  16815. ret.x += -layout->at_x + (float)*layout->offset_x;
  16816. ret.y += -layout->at_y + (float)*layout->offset_y;
  16817. return ret;
  16818. }
  16819. NK_LIB void
  16820. nk_panel_alloc_row(const struct nk_context *ctx, struct nk_window *win)
  16821. {
  16822. struct nk_panel *layout = win->layout;
  16823. struct nk_vec2 spacing = ctx->style.window.spacing;
  16824. const float row_height = layout->row.height - spacing.y;
  16825. nk_panel_layout(ctx, win, row_height, layout->row.columns);
  16826. }
  16827. NK_LIB void
  16828. nk_layout_widget_space(struct nk_rect *bounds, const struct nk_context *ctx,
  16829. struct nk_window *win, int modify)
  16830. {
  16831. struct nk_panel *layout;
  16832. const struct nk_style *style;
  16833. struct nk_vec2 spacing;
  16834. struct nk_vec2 padding;
  16835. float item_offset = 0;
  16836. float item_width = 0;
  16837. float item_spacing = 0;
  16838. float panel_space = 0;
  16839. NK_ASSERT(ctx);
  16840. NK_ASSERT(ctx->current);
  16841. NK_ASSERT(ctx->current->layout);
  16842. if (!ctx || !ctx->current || !ctx->current->layout)
  16843. return;
  16844. win = ctx->current;
  16845. layout = win->layout;
  16846. style = &ctx->style;
  16847. NK_ASSERT(bounds);
  16848. spacing = style->window.spacing;
  16849. padding = nk_panel_get_padding(style, layout->type);
  16850. panel_space = nk_layout_row_calculate_usable_space(&ctx->style, layout->type,
  16851. layout->bounds.w, layout->row.columns);
  16852. /* calculate the width of one item inside the current layout space */
  16853. switch (layout->row.type) {
  16854. case NK_LAYOUT_DYNAMIC_FIXED: {
  16855. /* scaling fixed size widgets item width */
  16856. item_width = NK_MAX(1.0f,panel_space) / (float)layout->row.columns;
  16857. item_offset = (float)layout->row.index * item_width;
  16858. item_spacing = (float)layout->row.index * spacing.x;
  16859. } break;
  16860. case NK_LAYOUT_DYNAMIC_ROW: {
  16861. /* scaling single ratio widget width */
  16862. item_width = layout->row.item_width * panel_space;
  16863. item_offset = layout->row.item_offset;
  16864. item_spacing = 0;
  16865. if (modify) {
  16866. layout->row.item_offset += item_width + spacing.x;
  16867. layout->row.filled += layout->row.item_width;
  16868. layout->row.index = 0;
  16869. }
  16870. } break;
  16871. case NK_LAYOUT_DYNAMIC_FREE: {
  16872. /* panel width depended free widget placing */
  16873. bounds->x = layout->at_x + (layout->bounds.w * layout->row.item.x);
  16874. bounds->x -= (float)*layout->offset_x;
  16875. bounds->y = layout->at_y + (layout->row.height * layout->row.item.y);
  16876. bounds->y -= (float)*layout->offset_y;
  16877. bounds->w = layout->bounds.w * layout->row.item.w;
  16878. bounds->h = layout->row.height * layout->row.item.h;
  16879. return;
  16880. }
  16881. case NK_LAYOUT_DYNAMIC: {
  16882. /* scaling arrays of panel width ratios for every widget */
  16883. float ratio;
  16884. NK_ASSERT(layout->row.ratio);
  16885. ratio = (layout->row.ratio[layout->row.index] < 0) ?
  16886. layout->row.item_width : layout->row.ratio[layout->row.index];
  16887. item_spacing = (float)layout->row.index * spacing.x;
  16888. item_width = (ratio * panel_space);
  16889. item_offset = layout->row.item_offset;
  16890. if (modify) {
  16891. layout->row.item_offset += item_width;
  16892. layout->row.filled += ratio;
  16893. }
  16894. } break;
  16895. case NK_LAYOUT_STATIC_FIXED: {
  16896. /* non-scaling fixed widgets item width */
  16897. item_width = layout->row.item_width;
  16898. item_offset = (float)layout->row.index * item_width;
  16899. item_spacing = (float)layout->row.index * spacing.x;
  16900. } break;
  16901. case NK_LAYOUT_STATIC_ROW: {
  16902. /* scaling single ratio widget width */
  16903. item_width = layout->row.item_width;
  16904. item_offset = layout->row.item_offset;
  16905. item_spacing = (float)layout->row.index * spacing.x;
  16906. if (modify) layout->row.item_offset += item_width;
  16907. } break;
  16908. case NK_LAYOUT_STATIC_FREE: {
  16909. /* free widget placing */
  16910. bounds->x = layout->at_x + layout->row.item.x;
  16911. bounds->w = layout->row.item.w;
  16912. if (((bounds->x + bounds->w) > layout->max_x) && modify)
  16913. layout->max_x = (bounds->x + bounds->w);
  16914. bounds->x -= (float)*layout->offset_x;
  16915. bounds->y = layout->at_y + layout->row.item.y;
  16916. bounds->y -= (float)*layout->offset_y;
  16917. bounds->h = layout->row.item.h;
  16918. return;
  16919. }
  16920. case NK_LAYOUT_STATIC: {
  16921. /* non-scaling array of panel pixel width for every widget */
  16922. item_spacing = (float)layout->row.index * spacing.x;
  16923. item_width = layout->row.ratio[layout->row.index];
  16924. item_offset = layout->row.item_offset;
  16925. if (modify) layout->row.item_offset += item_width;
  16926. } break;
  16927. case NK_LAYOUT_TEMPLATE: {
  16928. /* stretchy row layout with combined dynamic/static widget width*/
  16929. NK_ASSERT(layout->row.index < layout->row.columns);
  16930. NK_ASSERT(layout->row.index < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  16931. item_width = layout->row.templates[layout->row.index];
  16932. item_offset = layout->row.item_offset;
  16933. item_spacing = (float)layout->row.index * spacing.x;
  16934. if (modify) layout->row.item_offset += item_width;
  16935. } break;
  16936. default: NK_ASSERT(0); break;
  16937. };
  16938. /* set the bounds of the newly allocated widget */
  16939. bounds->w = item_width;
  16940. bounds->h = layout->row.height - spacing.y;
  16941. bounds->y = layout->at_y - (float)*layout->offset_y;
  16942. bounds->x = layout->at_x + item_offset + item_spacing + padding.x;
  16943. if (((bounds->x + bounds->w) > layout->max_x) && modify)
  16944. layout->max_x = bounds->x + bounds->w;
  16945. bounds->x -= (float)*layout->offset_x;
  16946. }
  16947. NK_LIB void
  16948. nk_panel_alloc_space(struct nk_rect *bounds, const struct nk_context *ctx)
  16949. {
  16950. struct nk_window *win;
  16951. struct nk_panel *layout;
  16952. NK_ASSERT(ctx);
  16953. NK_ASSERT(ctx->current);
  16954. NK_ASSERT(ctx->current->layout);
  16955. if (!ctx || !ctx->current || !ctx->current->layout)
  16956. return;
  16957. /* check if the end of the row has been hit and begin new row if so */
  16958. win = ctx->current;
  16959. layout = win->layout;
  16960. if (layout->row.index >= layout->row.columns)
  16961. nk_panel_alloc_row(ctx, win);
  16962. /* calculate widget position and size */
  16963. nk_layout_widget_space(bounds, ctx, win, nk_true);
  16964. layout->row.index++;
  16965. }
  16966. NK_LIB void
  16967. nk_layout_peek(struct nk_rect *bounds, struct nk_context *ctx)
  16968. {
  16969. float y;
  16970. int index;
  16971. struct nk_window *win;
  16972. struct nk_panel *layout;
  16973. NK_ASSERT(ctx);
  16974. NK_ASSERT(ctx->current);
  16975. NK_ASSERT(ctx->current->layout);
  16976. if (!ctx || !ctx->current || !ctx->current->layout)
  16977. return;
  16978. win = ctx->current;
  16979. layout = win->layout;
  16980. y = layout->at_y;
  16981. index = layout->row.index;
  16982. if (layout->row.index >= layout->row.columns) {
  16983. layout->at_y += layout->row.height;
  16984. layout->row.index = 0;
  16985. }
  16986. nk_layout_widget_space(bounds, ctx, win, nk_false);
  16987. if (!layout->row.index) {
  16988. bounds->x -= layout->row.item_offset;
  16989. }
  16990. layout->at_y = y;
  16991. layout->row.index = index;
  16992. }
  16993. /* ===============================================================
  16994. *
  16995. * TREE
  16996. *
  16997. * ===============================================================*/
  16998. NK_INTERN int
  16999. nk_tree_state_base(struct nk_context *ctx, enum nk_tree_type type,
  17000. struct nk_image *img, const char *title, enum nk_collapse_states *state)
  17001. {
  17002. struct nk_window *win;
  17003. struct nk_panel *layout;
  17004. const struct nk_style *style;
  17005. struct nk_command_buffer *out;
  17006. const struct nk_input *in;
  17007. const struct nk_style_button *button;
  17008. enum nk_symbol_type symbol;
  17009. float row_height;
  17010. struct nk_vec2 item_spacing;
  17011. struct nk_rect header = {0,0,0,0};
  17012. struct nk_rect sym = {0,0,0,0};
  17013. struct nk_text text;
  17014. nk_flags ws = 0;
  17015. enum nk_widget_layout_states widget_state;
  17016. NK_ASSERT(ctx);
  17017. NK_ASSERT(ctx->current);
  17018. NK_ASSERT(ctx->current->layout);
  17019. if (!ctx || !ctx->current || !ctx->current->layout)
  17020. return 0;
  17021. /* cache some data */
  17022. win = ctx->current;
  17023. layout = win->layout;
  17024. out = &win->buffer;
  17025. style = &ctx->style;
  17026. item_spacing = style->window.spacing;
  17027. /* calculate header bounds and draw background */
  17028. row_height = style->font->height + 2 * style->tab.padding.y;
  17029. nk_layout_set_min_row_height(ctx, row_height);
  17030. nk_layout_row_dynamic(ctx, row_height, 1);
  17031. nk_layout_reset_min_row_height(ctx);
  17032. widget_state = nk_widget(&header, ctx);
  17033. if (type == NK_TREE_TAB) {
  17034. const struct nk_style_item *background = &style->tab.background;
  17035. if (background->type == NK_STYLE_ITEM_IMAGE) {
  17036. nk_draw_image(out, header, &background->data.image, nk_white);
  17037. text.background = nk_rgba(0,0,0,0);
  17038. } else {
  17039. text.background = background->data.color;
  17040. nk_fill_rect(out, header, 0, style->tab.border_color);
  17041. nk_fill_rect(out, nk_shrink_rect(header, style->tab.border),
  17042. style->tab.rounding, background->data.color);
  17043. }
  17044. } else text.background = style->window.background;
  17045. /* update node state */
  17046. in = (!(layout->flags & NK_WINDOW_ROM)) ? &ctx->input: 0;
  17047. in = (in && widget_state == NK_WIDGET_VALID) ? &ctx->input : 0;
  17048. if (nk_button_behavior(&ws, header, in, NK_BUTTON_DEFAULT))
  17049. *state = (*state == NK_MAXIMIZED) ? NK_MINIMIZED : NK_MAXIMIZED;
  17050. /* select correct button style */
  17051. if (*state == NK_MAXIMIZED) {
  17052. symbol = style->tab.sym_maximize;
  17053. if (type == NK_TREE_TAB)
  17054. button = &style->tab.tab_maximize_button;
  17055. else button = &style->tab.node_maximize_button;
  17056. } else {
  17057. symbol = style->tab.sym_minimize;
  17058. if (type == NK_TREE_TAB)
  17059. button = &style->tab.tab_minimize_button;
  17060. else button = &style->tab.node_minimize_button;
  17061. }
  17062. {/* draw triangle button */
  17063. sym.w = sym.h = style->font->height;
  17064. sym.y = header.y + style->tab.padding.y;
  17065. sym.x = header.x + style->tab.padding.x;
  17066. nk_do_button_symbol(&ws, &win->buffer, sym, symbol, NK_BUTTON_DEFAULT,
  17067. button, 0, style->font);
  17068. if (img) {
  17069. /* draw optional image icon */
  17070. sym.x = sym.x + sym.w + 4 * item_spacing.x;
  17071. nk_draw_image(&win->buffer, sym, img, nk_white);
  17072. sym.w = style->font->height + style->tab.spacing.x;}
  17073. }
  17074. {/* draw label */
  17075. struct nk_rect label;
  17076. header.w = NK_MAX(header.w, sym.w + item_spacing.x);
  17077. label.x = sym.x + sym.w + item_spacing.x;
  17078. label.y = sym.y;
  17079. label.w = header.w - (sym.w + item_spacing.y + style->tab.indent);
  17080. label.h = style->font->height;
  17081. text.text = style->tab.text;
  17082. text.padding = nk_vec2(0,0);
  17083. nk_widget_text(out, label, title, nk_strlen(title), &text,
  17084. NK_TEXT_LEFT, style->font);}
  17085. /* increase x-axis cursor widget position pointer */
  17086. if (*state == NK_MAXIMIZED) {
  17087. layout->at_x = header.x + (float)*layout->offset_x + style->tab.indent;
  17088. layout->bounds.w = NK_MAX(layout->bounds.w, style->tab.indent);
  17089. layout->bounds.w -= (style->tab.indent + style->window.padding.x);
  17090. layout->row.tree_depth++;
  17091. return nk_true;
  17092. } else return nk_false;
  17093. }
  17094. NK_INTERN int
  17095. nk_tree_base(struct nk_context *ctx, enum nk_tree_type type,
  17096. struct nk_image *img, const char *title, enum nk_collapse_states initial_state,
  17097. const char *hash, int len, int line)
  17098. {
  17099. struct nk_window *win = ctx->current;
  17100. int title_len = 0;
  17101. nk_hash tree_hash = 0;
  17102. nk_uint *state = 0;
  17103. /* retrieve tree state from internal widget state tables */
  17104. if (!hash) {
  17105. title_len = (int)nk_strlen(title);
  17106. tree_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)line);
  17107. } else tree_hash = nk_murmur_hash(hash, len, (nk_hash)line);
  17108. state = nk_find_value(win, tree_hash);
  17109. if (!state) {
  17110. state = nk_add_value(ctx, win, tree_hash, 0);
  17111. *state = initial_state;
  17112. }
  17113. return nk_tree_state_base(ctx, type, img, title, (enum nk_collapse_states*)state);
  17114. }
  17115. NK_API int
  17116. nk_tree_state_push(struct nk_context *ctx, enum nk_tree_type type,
  17117. const char *title, enum nk_collapse_states *state)
  17118. {
  17119. return nk_tree_state_base(ctx, type, 0, title, state);
  17120. }
  17121. NK_API int
  17122. nk_tree_state_image_push(struct nk_context *ctx, enum nk_tree_type type,
  17123. struct nk_image img, const char *title, enum nk_collapse_states *state)
  17124. {
  17125. return nk_tree_state_base(ctx, type, &img, title, state);
  17126. }
  17127. NK_API void
  17128. nk_tree_state_pop(struct nk_context *ctx)
  17129. {
  17130. struct nk_window *win = 0;
  17131. struct nk_panel *layout = 0;
  17132. NK_ASSERT(ctx);
  17133. NK_ASSERT(ctx->current);
  17134. NK_ASSERT(ctx->current->layout);
  17135. if (!ctx || !ctx->current || !ctx->current->layout)
  17136. return;
  17137. win = ctx->current;
  17138. layout = win->layout;
  17139. layout->at_x -= ctx->style.tab.indent + ctx->style.window.padding.x;
  17140. layout->bounds.w += ctx->style.tab.indent + ctx->style.window.padding.x;
  17141. NK_ASSERT(layout->row.tree_depth);
  17142. layout->row.tree_depth--;
  17143. }
  17144. NK_API int
  17145. nk_tree_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  17146. const char *title, enum nk_collapse_states initial_state,
  17147. const char *hash, int len, int line)
  17148. {
  17149. return nk_tree_base(ctx, type, 0, title, initial_state, hash, len, line);
  17150. }
  17151. NK_API int
  17152. nk_tree_image_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  17153. struct nk_image img, const char *title, enum nk_collapse_states initial_state,
  17154. const char *hash, int len,int seed)
  17155. {
  17156. return nk_tree_base(ctx, type, &img, title, initial_state, hash, len, seed);
  17157. }
  17158. NK_API void
  17159. nk_tree_pop(struct nk_context *ctx)
  17160. {
  17161. nk_tree_state_pop(ctx);
  17162. }
  17163. NK_INTERN int
  17164. nk_tree_element_image_push_hashed_base(struct nk_context *ctx, enum nk_tree_type type,
  17165. struct nk_image *img, const char *title, int title_len,
  17166. enum nk_collapse_states *state, int *selected)
  17167. {
  17168. struct nk_window *win;
  17169. struct nk_panel *layout;
  17170. const struct nk_style *style;
  17171. struct nk_command_buffer *out;
  17172. const struct nk_input *in;
  17173. const struct nk_style_button *button;
  17174. enum nk_symbol_type symbol;
  17175. float row_height;
  17176. struct nk_vec2 padding;
  17177. int text_len;
  17178. float text_width;
  17179. struct nk_vec2 item_spacing;
  17180. struct nk_rect header = {0,0,0,0};
  17181. struct nk_rect sym = {0,0,0,0};
  17182. struct nk_text text;
  17183. nk_flags ws = 0;
  17184. enum nk_widget_layout_states widget_state;
  17185. NK_ASSERT(ctx);
  17186. NK_ASSERT(ctx->current);
  17187. NK_ASSERT(ctx->current->layout);
  17188. if (!ctx || !ctx->current || !ctx->current->layout)
  17189. return 0;
  17190. /* cache some data */
  17191. win = ctx->current;
  17192. layout = win->layout;
  17193. out = &win->buffer;
  17194. style = &ctx->style;
  17195. item_spacing = style->window.spacing;
  17196. padding = style->selectable.padding;
  17197. /* calculate header bounds and draw background */
  17198. row_height = style->font->height + 2 * style->tab.padding.y;
  17199. nk_layout_set_min_row_height(ctx, row_height);
  17200. nk_layout_row_dynamic(ctx, row_height, 1);
  17201. nk_layout_reset_min_row_height(ctx);
  17202. widget_state = nk_widget(&header, ctx);
  17203. if (type == NK_TREE_TAB) {
  17204. const struct nk_style_item *background = &style->tab.background;
  17205. if (background->type == NK_STYLE_ITEM_IMAGE) {
  17206. nk_draw_image(out, header, &background->data.image, nk_white);
  17207. text.background = nk_rgba(0,0,0,0);
  17208. } else {
  17209. text.background = background->data.color;
  17210. nk_fill_rect(out, header, 0, style->tab.border_color);
  17211. nk_fill_rect(out, nk_shrink_rect(header, style->tab.border),
  17212. style->tab.rounding, background->data.color);
  17213. }
  17214. } else text.background = style->window.background;
  17215. in = (!(layout->flags & NK_WINDOW_ROM)) ? &ctx->input: 0;
  17216. in = (in && widget_state == NK_WIDGET_VALID) ? &ctx->input : 0;
  17217. /* select correct button style */
  17218. if (*state == NK_MAXIMIZED) {
  17219. symbol = style->tab.sym_maximize;
  17220. if (type == NK_TREE_TAB)
  17221. button = &style->tab.tab_maximize_button;
  17222. else button = &style->tab.node_maximize_button;
  17223. } else {
  17224. symbol = style->tab.sym_minimize;
  17225. if (type == NK_TREE_TAB)
  17226. button = &style->tab.tab_minimize_button;
  17227. else button = &style->tab.node_minimize_button;
  17228. }
  17229. {/* draw triangle button */
  17230. sym.w = sym.h = style->font->height;
  17231. sym.y = header.y + style->tab.padding.y;
  17232. sym.x = header.x + style->tab.padding.x;
  17233. if (nk_do_button_symbol(&ws, &win->buffer, sym, symbol, NK_BUTTON_DEFAULT, button, in, style->font))
  17234. *state = (*state == NK_MAXIMIZED) ? NK_MINIMIZED : NK_MAXIMIZED;}
  17235. /* draw label */
  17236. {nk_flags dummy = 0;
  17237. struct nk_rect label;
  17238. /* calculate size of the text and tooltip */
  17239. text_len = nk_strlen(title);
  17240. text_width = style->font->width(style->font->userdata, style->font->height, title, text_len);
  17241. text_width += (4 * padding.x);
  17242. header.w = NK_MAX(header.w, sym.w + item_spacing.x);
  17243. label.x = sym.x + sym.w + item_spacing.x;
  17244. label.y = sym.y;
  17245. label.w = NK_MIN(header.w - (sym.w + item_spacing.y + style->tab.indent), text_width);
  17246. label.h = style->font->height;
  17247. if (img) {
  17248. nk_do_selectable_image(&dummy, &win->buffer, label, title, title_len, NK_TEXT_LEFT,
  17249. selected, img, &style->selectable, in, style->font);
  17250. } else nk_do_selectable(&dummy, &win->buffer, label, title, title_len, NK_TEXT_LEFT,
  17251. selected, &style->selectable, in, style->font);
  17252. }
  17253. /* increase x-axis cursor widget position pointer */
  17254. if (*state == NK_MAXIMIZED) {
  17255. layout->at_x = header.x + (float)*layout->offset_x + style->tab.indent;
  17256. layout->bounds.w = NK_MAX(layout->bounds.w, style->tab.indent);
  17257. layout->bounds.w -= (style->tab.indent + style->window.padding.x);
  17258. layout->row.tree_depth++;
  17259. return nk_true;
  17260. } else return nk_false;
  17261. }
  17262. NK_INTERN int
  17263. nk_tree_element_base(struct nk_context *ctx, enum nk_tree_type type,
  17264. struct nk_image *img, const char *title, enum nk_collapse_states initial_state,
  17265. int *selected, const char *hash, int len, int line)
  17266. {
  17267. struct nk_window *win = ctx->current;
  17268. int title_len = 0;
  17269. nk_hash tree_hash = 0;
  17270. nk_uint *state = 0;
  17271. /* retrieve tree state from internal widget state tables */
  17272. if (!hash) {
  17273. title_len = (int)nk_strlen(title);
  17274. tree_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)line);
  17275. } else tree_hash = nk_murmur_hash(hash, len, (nk_hash)line);
  17276. state = nk_find_value(win, tree_hash);
  17277. if (!state) {
  17278. state = nk_add_value(ctx, win, tree_hash, 0);
  17279. *state = initial_state;
  17280. } return nk_tree_element_image_push_hashed_base(ctx, type, img, title,
  17281. nk_strlen(title), (enum nk_collapse_states*)state, selected);
  17282. }
  17283. NK_API int
  17284. nk_tree_element_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  17285. const char *title, enum nk_collapse_states initial_state,
  17286. int *selected, const char *hash, int len, int seed)
  17287. {
  17288. return nk_tree_element_base(ctx, type, 0, title, initial_state, selected, hash, len, seed);
  17289. }
  17290. NK_API int
  17291. nk_tree_element_image_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  17292. struct nk_image img, const char *title, enum nk_collapse_states initial_state,
  17293. int *selected, const char *hash, int len,int seed)
  17294. {
  17295. return nk_tree_element_base(ctx, type, &img, title, initial_state, selected, hash, len, seed);
  17296. }
  17297. NK_API void
  17298. nk_tree_element_pop(struct nk_context *ctx)
  17299. {
  17300. nk_tree_state_pop(ctx);
  17301. }
  17302. /* ===============================================================
  17303. *
  17304. * GROUP
  17305. *
  17306. * ===============================================================*/
  17307. NK_API int
  17308. nk_group_scrolled_offset_begin(struct nk_context *ctx,
  17309. nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags)
  17310. {
  17311. struct nk_rect bounds;
  17312. struct nk_window panel;
  17313. struct nk_window *win;
  17314. win = ctx->current;
  17315. nk_panel_alloc_space(&bounds, ctx);
  17316. {const struct nk_rect *c = &win->layout->clip;
  17317. if (!NK_INTERSECT(c->x, c->y, c->w, c->h, bounds.x, bounds.y, bounds.w, bounds.h) &&
  17318. !(flags & NK_WINDOW_MOVABLE)) {
  17319. return 0;
  17320. }}
  17321. if (win->flags & NK_WINDOW_ROM)
  17322. flags |= NK_WINDOW_ROM;
  17323. /* initialize a fake window to create the panel from */
  17324. nk_zero(&panel, sizeof(panel));
  17325. panel.bounds = bounds;
  17326. panel.flags = flags;
  17327. panel.scrollbar.x = *x_offset;
  17328. panel.scrollbar.y = *y_offset;
  17329. panel.buffer = win->buffer;
  17330. panel.layout = (struct nk_panel*)nk_create_panel(ctx);
  17331. ctx->current = &panel;
  17332. nk_panel_begin(ctx, (flags & NK_WINDOW_TITLE) ? title: 0, NK_PANEL_GROUP);
  17333. win->buffer = panel.buffer;
  17334. win->buffer.clip = panel.layout->clip;
  17335. panel.layout->offset_x = x_offset;
  17336. panel.layout->offset_y = y_offset;
  17337. panel.layout->parent = win->layout;
  17338. win->layout = panel.layout;
  17339. ctx->current = win;
  17340. if ((panel.layout->flags & NK_WINDOW_CLOSED) ||
  17341. (panel.layout->flags & NK_WINDOW_MINIMIZED))
  17342. {
  17343. nk_flags f = panel.layout->flags;
  17344. nk_group_scrolled_end(ctx);
  17345. if (f & NK_WINDOW_CLOSED)
  17346. return NK_WINDOW_CLOSED;
  17347. if (f & NK_WINDOW_MINIMIZED)
  17348. return NK_WINDOW_MINIMIZED;
  17349. }
  17350. return 1;
  17351. }
  17352. NK_API void
  17353. nk_group_scrolled_end(struct nk_context *ctx)
  17354. {
  17355. struct nk_window *win;
  17356. struct nk_panel *parent;
  17357. struct nk_panel *g;
  17358. struct nk_rect clip;
  17359. struct nk_window pan;
  17360. struct nk_vec2 panel_padding;
  17361. NK_ASSERT(ctx);
  17362. NK_ASSERT(ctx->current);
  17363. if (!ctx || !ctx->current)
  17364. return;
  17365. /* make sure nk_group_begin was called correctly */
  17366. NK_ASSERT(ctx->current);
  17367. win = ctx->current;
  17368. NK_ASSERT(win->layout);
  17369. g = win->layout;
  17370. NK_ASSERT(g->parent);
  17371. parent = g->parent;
  17372. /* dummy window */
  17373. nk_zero_struct(pan);
  17374. panel_padding = nk_panel_get_padding(&ctx->style, NK_PANEL_GROUP);
  17375. pan.bounds.y = g->bounds.y - (g->header_height + g->menu.h);
  17376. pan.bounds.x = g->bounds.x - panel_padding.x;
  17377. pan.bounds.w = g->bounds.w + 2 * panel_padding.x;
  17378. pan.bounds.h = g->bounds.h + g->header_height + g->menu.h;
  17379. if (g->flags & NK_WINDOW_BORDER) {
  17380. pan.bounds.x -= g->border;
  17381. pan.bounds.y -= g->border;
  17382. pan.bounds.w += 2*g->border;
  17383. pan.bounds.h += 2*g->border;
  17384. }
  17385. if (!(g->flags & NK_WINDOW_NO_SCROLLBAR)) {
  17386. pan.bounds.w += ctx->style.window.scrollbar_size.x;
  17387. pan.bounds.h += ctx->style.window.scrollbar_size.y;
  17388. }
  17389. pan.scrollbar.x = *g->offset_x;
  17390. pan.scrollbar.y = *g->offset_y;
  17391. pan.flags = g->flags;
  17392. pan.buffer = win->buffer;
  17393. pan.layout = g;
  17394. pan.parent = win;
  17395. ctx->current = &pan;
  17396. /* make sure group has correct clipping rectangle */
  17397. nk_unify(&clip, &parent->clip, pan.bounds.x, pan.bounds.y,
  17398. pan.bounds.x + pan.bounds.w, pan.bounds.y + pan.bounds.h + panel_padding.x);
  17399. nk_push_scissor(&pan.buffer, clip);
  17400. nk_end(ctx);
  17401. win->buffer = pan.buffer;
  17402. nk_push_scissor(&win->buffer, parent->clip);
  17403. ctx->current = win;
  17404. win->layout = parent;
  17405. g->bounds = pan.bounds;
  17406. return;
  17407. }
  17408. NK_API int
  17409. nk_group_scrolled_begin(struct nk_context *ctx,
  17410. struct nk_scroll *scroll, const char *title, nk_flags flags)
  17411. {
  17412. return nk_group_scrolled_offset_begin(ctx, &scroll->x, &scroll->y, title, flags);
  17413. }
  17414. NK_API int
  17415. nk_group_begin_titled(struct nk_context *ctx, const char *id,
  17416. const char *title, nk_flags flags)
  17417. {
  17418. int id_len;
  17419. nk_hash id_hash;
  17420. struct nk_window *win;
  17421. nk_uint *x_offset;
  17422. nk_uint *y_offset;
  17423. NK_ASSERT(ctx);
  17424. NK_ASSERT(id);
  17425. NK_ASSERT(ctx->current);
  17426. NK_ASSERT(ctx->current->layout);
  17427. if (!ctx || !ctx->current || !ctx->current->layout || !id)
  17428. return 0;
  17429. /* find persistent group scrollbar value */
  17430. win = ctx->current;
  17431. id_len = (int)nk_strlen(id);
  17432. id_hash = nk_murmur_hash(id, (int)id_len, NK_PANEL_GROUP);
  17433. x_offset = nk_find_value(win, id_hash);
  17434. if (!x_offset) {
  17435. x_offset = nk_add_value(ctx, win, id_hash, 0);
  17436. y_offset = nk_add_value(ctx, win, id_hash+1, 0);
  17437. NK_ASSERT(x_offset);
  17438. NK_ASSERT(y_offset);
  17439. if (!x_offset || !y_offset) return 0;
  17440. *x_offset = *y_offset = 0;
  17441. } else y_offset = nk_find_value(win, id_hash+1);
  17442. return nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
  17443. }
  17444. NK_API int
  17445. nk_group_begin(struct nk_context *ctx, const char *title, nk_flags flags)
  17446. {
  17447. return nk_group_begin_titled(ctx, title, title, flags);
  17448. }
  17449. NK_API void
  17450. nk_group_end(struct nk_context *ctx)
  17451. {
  17452. nk_group_scrolled_end(ctx);
  17453. }
  17454. /* ===============================================================
  17455. *
  17456. * LIST VIEW
  17457. *
  17458. * ===============================================================*/
  17459. NK_API int
  17460. nk_list_view_begin(struct nk_context *ctx, struct nk_list_view *view,
  17461. const char *title, nk_flags flags, int row_height, int row_count)
  17462. {
  17463. int title_len;
  17464. nk_hash title_hash;
  17465. nk_uint *x_offset;
  17466. nk_uint *y_offset;
  17467. int result;
  17468. struct nk_window *win;
  17469. struct nk_panel *layout;
  17470. const struct nk_style *style;
  17471. struct nk_vec2 item_spacing;
  17472. NK_ASSERT(ctx);
  17473. NK_ASSERT(view);
  17474. NK_ASSERT(title);
  17475. if (!ctx || !view || !title) return 0;
  17476. win = ctx->current;
  17477. style = &ctx->style;
  17478. item_spacing = style->window.spacing;
  17479. row_height += NK_MAX(0, (int)item_spacing.y);
  17480. /* find persistent list view scrollbar offset */
  17481. title_len = (int)nk_strlen(title);
  17482. title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_GROUP);
  17483. x_offset = nk_find_value(win, title_hash);
  17484. if (!x_offset) {
  17485. x_offset = nk_add_value(ctx, win, title_hash, 0);
  17486. y_offset = nk_add_value(ctx, win, title_hash+1, 0);
  17487. NK_ASSERT(x_offset);
  17488. NK_ASSERT(y_offset);
  17489. if (!x_offset || !y_offset) return 0;
  17490. *x_offset = *y_offset = 0;
  17491. } else y_offset = nk_find_value(win, title_hash+1);
  17492. view->scroll_value = *y_offset;
  17493. view->scroll_pointer = y_offset;
  17494. *y_offset = 0;
  17495. result = nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
  17496. win = ctx->current;
  17497. layout = win->layout;
  17498. view->total_height = row_height * NK_MAX(row_count,1);
  17499. view->begin = (int)NK_MAX(((float)view->scroll_value / (float)row_height), 0.0f);
  17500. view->count = (int)NK_MAX(nk_iceilf((layout->clip.h)/(float)row_height),0);
  17501. view->count = NK_MIN(view->count, row_count - view->begin);
  17502. view->end = view->begin + view->count;
  17503. view->ctx = ctx;
  17504. return result;
  17505. }
  17506. NK_API void
  17507. nk_list_view_end(struct nk_list_view *view)
  17508. {
  17509. struct nk_context *ctx;
  17510. struct nk_window *win;
  17511. struct nk_panel *layout;
  17512. NK_ASSERT(view);
  17513. NK_ASSERT(view->ctx);
  17514. NK_ASSERT(view->scroll_pointer);
  17515. if (!view || !view->ctx) return;
  17516. ctx = view->ctx;
  17517. win = ctx->current;
  17518. layout = win->layout;
  17519. layout->at_y = layout->bounds.y + (float)view->total_height;
  17520. *view->scroll_pointer = *view->scroll_pointer + view->scroll_value;
  17521. nk_group_end(view->ctx);
  17522. }
  17523. /* ===============================================================
  17524. *
  17525. * WIDGET
  17526. *
  17527. * ===============================================================*/
  17528. NK_API struct nk_rect
  17529. nk_widget_bounds(struct nk_context *ctx)
  17530. {
  17531. struct nk_rect bounds;
  17532. NK_ASSERT(ctx);
  17533. NK_ASSERT(ctx->current);
  17534. if (!ctx || !ctx->current)
  17535. return nk_rect(0,0,0,0);
  17536. nk_layout_peek(&bounds, ctx);
  17537. return bounds;
  17538. }
  17539. NK_API struct nk_vec2
  17540. nk_widget_position(struct nk_context *ctx)
  17541. {
  17542. struct nk_rect bounds;
  17543. NK_ASSERT(ctx);
  17544. NK_ASSERT(ctx->current);
  17545. if (!ctx || !ctx->current)
  17546. return nk_vec2(0,0);
  17547. nk_layout_peek(&bounds, ctx);
  17548. return nk_vec2(bounds.x, bounds.y);
  17549. }
  17550. NK_API struct nk_vec2
  17551. nk_widget_size(struct nk_context *ctx)
  17552. {
  17553. struct nk_rect bounds;
  17554. NK_ASSERT(ctx);
  17555. NK_ASSERT(ctx->current);
  17556. if (!ctx || !ctx->current)
  17557. return nk_vec2(0,0);
  17558. nk_layout_peek(&bounds, ctx);
  17559. return nk_vec2(bounds.w, bounds.h);
  17560. }
  17561. NK_API float
  17562. nk_widget_width(struct nk_context *ctx)
  17563. {
  17564. struct nk_rect bounds;
  17565. NK_ASSERT(ctx);
  17566. NK_ASSERT(ctx->current);
  17567. if (!ctx || !ctx->current)
  17568. return 0;
  17569. nk_layout_peek(&bounds, ctx);
  17570. return bounds.w;
  17571. }
  17572. NK_API float
  17573. nk_widget_height(struct nk_context *ctx)
  17574. {
  17575. struct nk_rect bounds;
  17576. NK_ASSERT(ctx);
  17577. NK_ASSERT(ctx->current);
  17578. if (!ctx || !ctx->current)
  17579. return 0;
  17580. nk_layout_peek(&bounds, ctx);
  17581. return bounds.h;
  17582. }
  17583. NK_API int
  17584. nk_widget_is_hovered(struct nk_context *ctx)
  17585. {
  17586. struct nk_rect c, v;
  17587. struct nk_rect bounds;
  17588. NK_ASSERT(ctx);
  17589. NK_ASSERT(ctx->current);
  17590. if (!ctx || !ctx->current || ctx->active != ctx->current)
  17591. return 0;
  17592. c = ctx->current->layout->clip;
  17593. c.x = (float)((int)c.x);
  17594. c.y = (float)((int)c.y);
  17595. c.w = (float)((int)c.w);
  17596. c.h = (float)((int)c.h);
  17597. nk_layout_peek(&bounds, ctx);
  17598. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  17599. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  17600. return 0;
  17601. return nk_input_is_mouse_hovering_rect(&ctx->input, bounds);
  17602. }
  17603. NK_API int
  17604. nk_widget_is_mouse_clicked(struct nk_context *ctx, enum nk_buttons btn)
  17605. {
  17606. struct nk_rect c, v;
  17607. struct nk_rect bounds;
  17608. NK_ASSERT(ctx);
  17609. NK_ASSERT(ctx->current);
  17610. if (!ctx || !ctx->current || ctx->active != ctx->current)
  17611. return 0;
  17612. c = ctx->current->layout->clip;
  17613. c.x = (float)((int)c.x);
  17614. c.y = (float)((int)c.y);
  17615. c.w = (float)((int)c.w);
  17616. c.h = (float)((int)c.h);
  17617. nk_layout_peek(&bounds, ctx);
  17618. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  17619. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  17620. return 0;
  17621. return nk_input_mouse_clicked(&ctx->input, btn, bounds);
  17622. }
  17623. NK_API int
  17624. nk_widget_has_mouse_click_down(struct nk_context *ctx, enum nk_buttons btn, int down)
  17625. {
  17626. struct nk_rect c, v;
  17627. struct nk_rect bounds;
  17628. NK_ASSERT(ctx);
  17629. NK_ASSERT(ctx->current);
  17630. if (!ctx || !ctx->current || ctx->active != ctx->current)
  17631. return 0;
  17632. c = ctx->current->layout->clip;
  17633. c.x = (float)((int)c.x);
  17634. c.y = (float)((int)c.y);
  17635. c.w = (float)((int)c.w);
  17636. c.h = (float)((int)c.h);
  17637. nk_layout_peek(&bounds, ctx);
  17638. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  17639. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  17640. return 0;
  17641. return nk_input_has_mouse_click_down_in_rect(&ctx->input, btn, bounds, down);
  17642. }
  17643. NK_API enum nk_widget_layout_states
  17644. nk_widget(struct nk_rect *bounds, const struct nk_context *ctx)
  17645. {
  17646. struct nk_rect c, v;
  17647. struct nk_window *win;
  17648. struct nk_panel *layout;
  17649. const struct nk_input *in;
  17650. NK_ASSERT(ctx);
  17651. NK_ASSERT(ctx->current);
  17652. NK_ASSERT(ctx->current->layout);
  17653. if (!ctx || !ctx->current || !ctx->current->layout)
  17654. return NK_WIDGET_INVALID;
  17655. /* allocate space and check if the widget needs to be updated and drawn */
  17656. nk_panel_alloc_space(bounds, ctx);
  17657. win = ctx->current;
  17658. layout = win->layout;
  17659. in = &ctx->input;
  17660. c = layout->clip;
  17661. /* if one of these triggers you forgot to add an `if` condition around either
  17662. a window, group, popup, combobox or contextual menu `begin` and `end` block.
  17663. Example:
  17664. if (nk_begin(...) {...} nk_end(...); or
  17665. if (nk_group_begin(...) { nk_group_end(...);} */
  17666. NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
  17667. NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
  17668. NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
  17669. /* need to convert to int here to remove floating point errors */
  17670. bounds->x = (float)((int)bounds->x);
  17671. bounds->y = (float)((int)bounds->y);
  17672. bounds->w = (float)((int)bounds->w);
  17673. bounds->h = (float)((int)bounds->h);
  17674. c.x = (float)((int)c.x);
  17675. c.y = (float)((int)c.y);
  17676. c.w = (float)((int)c.w);
  17677. c.h = (float)((int)c.h);
  17678. nk_unify(&v, &c, bounds->x, bounds->y, bounds->x + bounds->w, bounds->y + bounds->h);
  17679. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds->x, bounds->y, bounds->w, bounds->h))
  17680. return NK_WIDGET_INVALID;
  17681. if (!NK_INBOX(in->mouse.pos.x, in->mouse.pos.y, v.x, v.y, v.w, v.h))
  17682. return NK_WIDGET_ROM;
  17683. return NK_WIDGET_VALID;
  17684. }
  17685. NK_API enum nk_widget_layout_states
  17686. nk_widget_fitting(struct nk_rect *bounds, struct nk_context *ctx,
  17687. struct nk_vec2 item_padding)
  17688. {
  17689. /* update the bounds to stand without padding */
  17690. struct nk_window *win;
  17691. struct nk_style *style;
  17692. struct nk_panel *layout;
  17693. enum nk_widget_layout_states state;
  17694. struct nk_vec2 panel_padding;
  17695. NK_ASSERT(ctx);
  17696. NK_ASSERT(ctx->current);
  17697. NK_ASSERT(ctx->current->layout);
  17698. if (!ctx || !ctx->current || !ctx->current->layout)
  17699. return NK_WIDGET_INVALID;
  17700. win = ctx->current;
  17701. style = &ctx->style;
  17702. layout = win->layout;
  17703. state = nk_widget(bounds, ctx);
  17704. panel_padding = nk_panel_get_padding(style, layout->type);
  17705. if (layout->row.index == 1) {
  17706. bounds->w += panel_padding.x;
  17707. bounds->x -= panel_padding.x;
  17708. } else bounds->x -= item_padding.x;
  17709. if (layout->row.index == layout->row.columns)
  17710. bounds->w += panel_padding.x;
  17711. else bounds->w += item_padding.x;
  17712. return state;
  17713. }
  17714. NK_API void
  17715. nk_spacing(struct nk_context *ctx, int cols)
  17716. {
  17717. struct nk_window *win;
  17718. struct nk_panel *layout;
  17719. struct nk_rect none;
  17720. int i, index, rows;
  17721. NK_ASSERT(ctx);
  17722. NK_ASSERT(ctx->current);
  17723. NK_ASSERT(ctx->current->layout);
  17724. if (!ctx || !ctx->current || !ctx->current->layout)
  17725. return;
  17726. /* spacing over row boundaries */
  17727. win = ctx->current;
  17728. layout = win->layout;
  17729. index = (layout->row.index + cols) % layout->row.columns;
  17730. rows = (layout->row.index + cols) / layout->row.columns;
  17731. if (rows) {
  17732. for (i = 0; i < rows; ++i)
  17733. nk_panel_alloc_row(ctx, win);
  17734. cols = index;
  17735. }
  17736. /* non table layout need to allocate space */
  17737. if (layout->row.type != NK_LAYOUT_DYNAMIC_FIXED &&
  17738. layout->row.type != NK_LAYOUT_STATIC_FIXED) {
  17739. for (i = 0; i < cols; ++i)
  17740. nk_panel_alloc_space(&none, ctx);
  17741. } layout->row.index = index;
  17742. }
  17743. /* ===============================================================
  17744. *
  17745. * TEXT
  17746. *
  17747. * ===============================================================*/
  17748. NK_LIB void
  17749. nk_widget_text(struct nk_command_buffer *o, struct nk_rect b,
  17750. const char *string, int len, const struct nk_text *t,
  17751. nk_flags a, const struct nk_user_font *f)
  17752. {
  17753. struct nk_rect label;
  17754. float text_width;
  17755. NK_ASSERT(o);
  17756. NK_ASSERT(t);
  17757. if (!o || !t) return;
  17758. b.h = NK_MAX(b.h, 2 * t->padding.y);
  17759. label.x = 0; label.w = 0;
  17760. label.y = b.y + t->padding.y;
  17761. label.h = NK_MIN(f->height, b.h - 2 * t->padding.y);
  17762. text_width = f->width(f->userdata, f->height, (const char*)string, len);
  17763. text_width += (2.0f * t->padding.x);
  17764. /* align in x-axis */
  17765. if (a & NK_TEXT_ALIGN_LEFT) {
  17766. label.x = b.x + t->padding.x;
  17767. label.w = NK_MAX(0, b.w - 2 * t->padding.x);
  17768. } else if (a & NK_TEXT_ALIGN_CENTERED) {
  17769. label.w = NK_MAX(1, 2 * t->padding.x + (float)text_width);
  17770. label.x = (b.x + t->padding.x + ((b.w - 2 * t->padding.x) - label.w) / 2);
  17771. label.x = NK_MAX(b.x + t->padding.x, label.x);
  17772. label.w = NK_MIN(b.x + b.w, label.x + label.w);
  17773. if (label.w >= label.x) label.w -= label.x;
  17774. } else if (a & NK_TEXT_ALIGN_RIGHT) {
  17775. label.x = NK_MAX(b.x + t->padding.x, (b.x + b.w) - (2 * t->padding.x + (float)text_width));
  17776. label.w = (float)text_width + 2 * t->padding.x;
  17777. } else return;
  17778. /* align in y-axis */
  17779. if (a & NK_TEXT_ALIGN_MIDDLE) {
  17780. label.y = b.y + b.h/2.0f - (float)f->height/2.0f;
  17781. label.h = NK_MAX(b.h/2.0f, b.h - (b.h/2.0f + f->height/2.0f));
  17782. } else if (a & NK_TEXT_ALIGN_BOTTOM) {
  17783. label.y = b.y + b.h - f->height;
  17784. label.h = f->height;
  17785. }
  17786. nk_draw_text(o, label, (const char*)string, len, f, t->background, t->text);
  17787. }
  17788. NK_LIB void
  17789. nk_widget_text_wrap(struct nk_command_buffer *o, struct nk_rect b,
  17790. const char *string, int len, const struct nk_text *t,
  17791. const struct nk_user_font *f)
  17792. {
  17793. float width;
  17794. int glyphs = 0;
  17795. int fitting = 0;
  17796. int done = 0;
  17797. struct nk_rect line;
  17798. struct nk_text text;
  17799. NK_INTERN nk_rune seperator[] = {' '};
  17800. NK_ASSERT(o);
  17801. NK_ASSERT(t);
  17802. if (!o || !t) return;
  17803. text.padding = nk_vec2(0,0);
  17804. text.background = t->background;
  17805. text.text = t->text;
  17806. b.w = NK_MAX(b.w, 2 * t->padding.x);
  17807. b.h = NK_MAX(b.h, 2 * t->padding.y);
  17808. b.h = b.h - 2 * t->padding.y;
  17809. line.x = b.x + t->padding.x;
  17810. line.y = b.y + t->padding.y;
  17811. line.w = b.w - 2 * t->padding.x;
  17812. line.h = 2 * t->padding.y + f->height;
  17813. fitting = nk_text_clamp(f, string, len, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
  17814. while (done < len) {
  17815. if (!fitting || line.y + line.h >= (b.y + b.h)) break;
  17816. nk_widget_text(o, line, &string[done], fitting, &text, NK_TEXT_LEFT, f);
  17817. done += fitting;
  17818. line.y += f->height + 2 * t->padding.y;
  17819. fitting = nk_text_clamp(f, &string[done], len - done, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
  17820. }
  17821. }
  17822. NK_API void
  17823. nk_text_colored(struct nk_context *ctx, const char *str, int len,
  17824. nk_flags alignment, struct nk_color color)
  17825. {
  17826. struct nk_window *win;
  17827. const struct nk_style *style;
  17828. struct nk_vec2 item_padding;
  17829. struct nk_rect bounds;
  17830. struct nk_text text;
  17831. NK_ASSERT(ctx);
  17832. NK_ASSERT(ctx->current);
  17833. NK_ASSERT(ctx->current->layout);
  17834. if (!ctx || !ctx->current || !ctx->current->layout) return;
  17835. win = ctx->current;
  17836. style = &ctx->style;
  17837. nk_panel_alloc_space(&bounds, ctx);
  17838. item_padding = style->text.padding;
  17839. text.padding.x = item_padding.x;
  17840. text.padding.y = item_padding.y;
  17841. text.background = style->window.background;
  17842. text.text = color;
  17843. nk_widget_text(&win->buffer, bounds, str, len, &text, alignment, style->font);
  17844. }
  17845. NK_API void
  17846. nk_text_wrap_colored(struct nk_context *ctx, const char *str,
  17847. int len, struct nk_color color)
  17848. {
  17849. struct nk_window *win;
  17850. const struct nk_style *style;
  17851. struct nk_vec2 item_padding;
  17852. struct nk_rect bounds;
  17853. struct nk_text text;
  17854. NK_ASSERT(ctx);
  17855. NK_ASSERT(ctx->current);
  17856. NK_ASSERT(ctx->current->layout);
  17857. if (!ctx || !ctx->current || !ctx->current->layout) return;
  17858. win = ctx->current;
  17859. style = &ctx->style;
  17860. nk_panel_alloc_space(&bounds, ctx);
  17861. item_padding = style->text.padding;
  17862. text.padding.x = item_padding.x;
  17863. text.padding.y = item_padding.y;
  17864. text.background = style->window.background;
  17865. text.text = color;
  17866. nk_widget_text_wrap(&win->buffer, bounds, str, len, &text, style->font);
  17867. }
  17868. #ifdef NK_INCLUDE_STANDARD_VARARGS
  17869. NK_API void
  17870. nk_labelf_colored(struct nk_context *ctx, nk_flags flags,
  17871. struct nk_color color, const char *fmt, ...)
  17872. {
  17873. va_list args;
  17874. va_start(args, fmt);
  17875. nk_labelfv_colored(ctx, flags, color, fmt, args);
  17876. va_end(args);
  17877. }
  17878. NK_API void
  17879. nk_labelf_colored_wrap(struct nk_context *ctx, struct nk_color color,
  17880. const char *fmt, ...)
  17881. {
  17882. va_list args;
  17883. va_start(args, fmt);
  17884. nk_labelfv_colored_wrap(ctx, color, fmt, args);
  17885. va_end(args);
  17886. }
  17887. NK_API void
  17888. nk_labelf(struct nk_context *ctx, nk_flags flags, const char *fmt, ...)
  17889. {
  17890. va_list args;
  17891. va_start(args, fmt);
  17892. nk_labelfv(ctx, flags, fmt, args);
  17893. va_end(args);
  17894. }
  17895. NK_API void
  17896. nk_labelf_wrap(struct nk_context *ctx, const char *fmt,...)
  17897. {
  17898. va_list args;
  17899. va_start(args, fmt);
  17900. nk_labelfv_wrap(ctx, fmt, args);
  17901. va_end(args);
  17902. }
  17903. NK_API void
  17904. nk_labelfv_colored(struct nk_context *ctx, nk_flags flags,
  17905. struct nk_color color, const char *fmt, va_list args)
  17906. {
  17907. char buf[256];
  17908. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  17909. nk_label_colored(ctx, buf, flags, color);
  17910. }
  17911. NK_API void
  17912. nk_labelfv_colored_wrap(struct nk_context *ctx, struct nk_color color,
  17913. const char *fmt, va_list args)
  17914. {
  17915. char buf[256];
  17916. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  17917. nk_label_colored_wrap(ctx, buf, color);
  17918. }
  17919. NK_API void
  17920. nk_labelfv(struct nk_context *ctx, nk_flags flags, const char *fmt, va_list args)
  17921. {
  17922. char buf[256];
  17923. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  17924. nk_label(ctx, buf, flags);
  17925. }
  17926. NK_API void
  17927. nk_labelfv_wrap(struct nk_context *ctx, const char *fmt, va_list args)
  17928. {
  17929. char buf[256];
  17930. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  17931. nk_label_wrap(ctx, buf);
  17932. }
  17933. NK_API void
  17934. nk_value_bool(struct nk_context *ctx, const char *prefix, int value)
  17935. {
  17936. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, ((value) ? "true": "false"));
  17937. }
  17938. NK_API void
  17939. nk_value_int(struct nk_context *ctx, const char *prefix, int value)
  17940. {
  17941. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %d", prefix, value);
  17942. }
  17943. NK_API void
  17944. nk_value_uint(struct nk_context *ctx, const char *prefix, unsigned int value)
  17945. {
  17946. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %u", prefix, value);
  17947. }
  17948. NK_API void
  17949. nk_value_float(struct nk_context *ctx, const char *prefix, float value)
  17950. {
  17951. double double_value = (double)value;
  17952. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %.3f", prefix, double_value);
  17953. }
  17954. NK_API void
  17955. nk_value_color_byte(struct nk_context *ctx, const char *p, struct nk_color c)
  17956. {
  17957. nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%d, %d, %d, %d)", p, c.r, c.g, c.b, c.a);
  17958. }
  17959. NK_API void
  17960. nk_value_color_float(struct nk_context *ctx, const char *p, struct nk_color color)
  17961. {
  17962. double c[4]; nk_color_dv(c, color);
  17963. nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%.2f, %.2f, %.2f, %.2f)",
  17964. p, c[0], c[1], c[2], c[3]);
  17965. }
  17966. NK_API void
  17967. nk_value_color_hex(struct nk_context *ctx, const char *prefix, struct nk_color color)
  17968. {
  17969. char hex[16];
  17970. nk_color_hex_rgba(hex, color);
  17971. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, hex);
  17972. }
  17973. #endif
  17974. NK_API void
  17975. nk_text(struct nk_context *ctx, const char *str, int len, nk_flags alignment)
  17976. {
  17977. NK_ASSERT(ctx);
  17978. if (!ctx) return;
  17979. nk_text_colored(ctx, str, len, alignment, ctx->style.text.color);
  17980. }
  17981. NK_API void
  17982. nk_text_wrap(struct nk_context *ctx, const char *str, int len)
  17983. {
  17984. NK_ASSERT(ctx);
  17985. if (!ctx) return;
  17986. nk_text_wrap_colored(ctx, str, len, ctx->style.text.color);
  17987. }
  17988. NK_API void
  17989. nk_label(struct nk_context *ctx, const char *str, nk_flags alignment)
  17990. {
  17991. nk_text(ctx, str, nk_strlen(str), alignment);
  17992. }
  17993. NK_API void
  17994. nk_label_colored(struct nk_context *ctx, const char *str, nk_flags align,
  17995. struct nk_color color)
  17996. {
  17997. nk_text_colored(ctx, str, nk_strlen(str), align, color);
  17998. }
  17999. NK_API void
  18000. nk_label_wrap(struct nk_context *ctx, const char *str)
  18001. {
  18002. nk_text_wrap(ctx, str, nk_strlen(str));
  18003. }
  18004. NK_API void
  18005. nk_label_colored_wrap(struct nk_context *ctx, const char *str, struct nk_color color)
  18006. {
  18007. nk_text_wrap_colored(ctx, str, nk_strlen(str), color);
  18008. }
  18009. /* ===============================================================
  18010. *
  18011. * IMAGE
  18012. *
  18013. * ===============================================================*/
  18014. NK_API nk_handle
  18015. nk_handle_ptr(void *ptr)
  18016. {
  18017. nk_handle handle = {0};
  18018. handle.ptr = ptr;
  18019. return handle;
  18020. }
  18021. NK_API nk_handle
  18022. nk_handle_id(int id)
  18023. {
  18024. nk_handle handle;
  18025. nk_zero_struct(handle);
  18026. handle.id = id;
  18027. return handle;
  18028. }
  18029. NK_API struct nk_image
  18030. nk_subimage_ptr(void *ptr, unsigned short w, unsigned short h, struct nk_rect r)
  18031. {
  18032. struct nk_image s;
  18033. nk_zero(&s, sizeof(s));
  18034. s.handle.ptr = ptr;
  18035. s.w = w; s.h = h;
  18036. s.region[0] = (unsigned short)r.x;
  18037. s.region[1] = (unsigned short)r.y;
  18038. s.region[2] = (unsigned short)r.w;
  18039. s.region[3] = (unsigned short)r.h;
  18040. return s;
  18041. }
  18042. NK_API struct nk_image
  18043. nk_subimage_id(int id, unsigned short w, unsigned short h, struct nk_rect r)
  18044. {
  18045. struct nk_image s;
  18046. nk_zero(&s, sizeof(s));
  18047. s.handle.id = id;
  18048. s.w = w; s.h = h;
  18049. s.region[0] = (unsigned short)r.x;
  18050. s.region[1] = (unsigned short)r.y;
  18051. s.region[2] = (unsigned short)r.w;
  18052. s.region[3] = (unsigned short)r.h;
  18053. return s;
  18054. }
  18055. NK_API struct nk_image
  18056. nk_subimage_handle(nk_handle handle, unsigned short w, unsigned short h,
  18057. struct nk_rect r)
  18058. {
  18059. struct nk_image s;
  18060. nk_zero(&s, sizeof(s));
  18061. s.handle = handle;
  18062. s.w = w; s.h = h;
  18063. s.region[0] = (unsigned short)r.x;
  18064. s.region[1] = (unsigned short)r.y;
  18065. s.region[2] = (unsigned short)r.w;
  18066. s.region[3] = (unsigned short)r.h;
  18067. return s;
  18068. }
  18069. NK_API struct nk_image
  18070. nk_image_handle(nk_handle handle)
  18071. {
  18072. struct nk_image s;
  18073. nk_zero(&s, sizeof(s));
  18074. s.handle = handle;
  18075. s.w = 0; s.h = 0;
  18076. s.region[0] = 0;
  18077. s.region[1] = 0;
  18078. s.region[2] = 0;
  18079. s.region[3] = 0;
  18080. return s;
  18081. }
  18082. NK_API struct nk_image
  18083. nk_image_ptr(void *ptr)
  18084. {
  18085. struct nk_image s;
  18086. nk_zero(&s, sizeof(s));
  18087. NK_ASSERT(ptr);
  18088. s.handle.ptr = ptr;
  18089. s.w = 0; s.h = 0;
  18090. s.region[0] = 0;
  18091. s.region[1] = 0;
  18092. s.region[2] = 0;
  18093. s.region[3] = 0;
  18094. return s;
  18095. }
  18096. NK_API struct nk_image
  18097. nk_image_id(int id)
  18098. {
  18099. struct nk_image s;
  18100. nk_zero(&s, sizeof(s));
  18101. s.handle.id = id;
  18102. s.w = 0; s.h = 0;
  18103. s.region[0] = 0;
  18104. s.region[1] = 0;
  18105. s.region[2] = 0;
  18106. s.region[3] = 0;
  18107. return s;
  18108. }
  18109. NK_API int
  18110. nk_image_is_subimage(const struct nk_image* img)
  18111. {
  18112. NK_ASSERT(img);
  18113. return !(img->w == 0 && img->h == 0);
  18114. }
  18115. NK_API void
  18116. nk_image(struct nk_context *ctx, struct nk_image img)
  18117. {
  18118. struct nk_window *win;
  18119. struct nk_rect bounds;
  18120. NK_ASSERT(ctx);
  18121. NK_ASSERT(ctx->current);
  18122. NK_ASSERT(ctx->current->layout);
  18123. if (!ctx || !ctx->current || !ctx->current->layout) return;
  18124. win = ctx->current;
  18125. if (!nk_widget(&bounds, ctx)) return;
  18126. nk_draw_image(&win->buffer, bounds, &img, nk_white);
  18127. }
  18128. NK_API void
  18129. nk_image_color(struct nk_context *ctx, struct nk_image img, struct nk_color col)
  18130. {
  18131. struct nk_window *win;
  18132. struct nk_rect bounds;
  18133. NK_ASSERT(ctx);
  18134. NK_ASSERT(ctx->current);
  18135. NK_ASSERT(ctx->current->layout);
  18136. if (!ctx || !ctx->current || !ctx->current->layout) return;
  18137. win = ctx->current;
  18138. if (!nk_widget(&bounds, ctx)) return;
  18139. nk_draw_image(&win->buffer, bounds, &img, col);
  18140. }
  18141. /* ==============================================================
  18142. *
  18143. * BUTTON
  18144. *
  18145. * ===============================================================*/
  18146. NK_LIB void
  18147. nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type,
  18148. struct nk_rect content, struct nk_color background, struct nk_color foreground,
  18149. float border_width, const struct nk_user_font *font)
  18150. {
  18151. switch (type) {
  18152. case NK_SYMBOL_X:
  18153. case NK_SYMBOL_UNDERSCORE:
  18154. case NK_SYMBOL_PLUS:
  18155. case NK_SYMBOL_MINUS: {
  18156. /* single character text symbol */
  18157. const char *X = (type == NK_SYMBOL_X) ? "x":
  18158. (type == NK_SYMBOL_UNDERSCORE) ? "_":
  18159. (type == NK_SYMBOL_PLUS) ? "+": "-";
  18160. struct nk_text text;
  18161. text.padding = nk_vec2(0,0);
  18162. text.background = background;
  18163. text.text = foreground;
  18164. nk_widget_text(out, content, X, 1, &text, NK_TEXT_CENTERED, font);
  18165. } break;
  18166. case NK_SYMBOL_CIRCLE_SOLID:
  18167. case NK_SYMBOL_CIRCLE_OUTLINE:
  18168. case NK_SYMBOL_RECT_SOLID:
  18169. case NK_SYMBOL_RECT_OUTLINE: {
  18170. /* simple empty/filled shapes */
  18171. if (type == NK_SYMBOL_RECT_SOLID || type == NK_SYMBOL_RECT_OUTLINE) {
  18172. nk_fill_rect(out, content, 0, foreground);
  18173. if (type == NK_SYMBOL_RECT_OUTLINE)
  18174. nk_fill_rect(out, nk_shrink_rect(content, border_width), 0, background);
  18175. } else {
  18176. nk_fill_circle(out, content, foreground);
  18177. if (type == NK_SYMBOL_CIRCLE_OUTLINE)
  18178. nk_fill_circle(out, nk_shrink_rect(content, 1), background);
  18179. }
  18180. } break;
  18181. case NK_SYMBOL_TRIANGLE_UP:
  18182. case NK_SYMBOL_TRIANGLE_DOWN:
  18183. case NK_SYMBOL_TRIANGLE_LEFT:
  18184. case NK_SYMBOL_TRIANGLE_RIGHT: {
  18185. enum nk_heading heading;
  18186. struct nk_vec2 points[3];
  18187. heading = (type == NK_SYMBOL_TRIANGLE_RIGHT) ? NK_RIGHT :
  18188. (type == NK_SYMBOL_TRIANGLE_LEFT) ? NK_LEFT:
  18189. (type == NK_SYMBOL_TRIANGLE_UP) ? NK_UP: NK_DOWN;
  18190. nk_triangle_from_direction(points, content, 0, 0, heading);
  18191. nk_fill_triangle(out, points[0].x, points[0].y, points[1].x, points[1].y,
  18192. points[2].x, points[2].y, foreground);
  18193. } break;
  18194. default:
  18195. case NK_SYMBOL_NONE:
  18196. case NK_SYMBOL_MAX: break;
  18197. }
  18198. }
  18199. NK_LIB int
  18200. nk_button_behavior(nk_flags *state, struct nk_rect r,
  18201. const struct nk_input *i, enum nk_button_behavior behavior)
  18202. {
  18203. int ret = 0;
  18204. nk_widget_state_reset(state);
  18205. if (!i) return 0;
  18206. if (nk_input_is_mouse_hovering_rect(i, r)) {
  18207. *state = NK_WIDGET_STATE_HOVERED;
  18208. if (nk_input_is_mouse_down(i, NK_BUTTON_LEFT))
  18209. *state = NK_WIDGET_STATE_ACTIVE;
  18210. if (nk_input_has_mouse_click_in_rect(i, NK_BUTTON_LEFT, r)) {
  18211. ret = (behavior != NK_BUTTON_DEFAULT) ?
  18212. nk_input_is_mouse_down(i, NK_BUTTON_LEFT):
  18213. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  18214. nk_input_is_mouse_released(i, NK_BUTTON_LEFT);
  18215. #else
  18216. nk_input_is_mouse_pressed(i, NK_BUTTON_LEFT);
  18217. #endif
  18218. }
  18219. }
  18220. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(i, r))
  18221. *state |= NK_WIDGET_STATE_ENTERED;
  18222. else if (nk_input_is_mouse_prev_hovering_rect(i, r))
  18223. *state |= NK_WIDGET_STATE_LEFT;
  18224. return ret;
  18225. }
  18226. NK_LIB const struct nk_style_item*
  18227. nk_draw_button(struct nk_command_buffer *out,
  18228. const struct nk_rect *bounds, nk_flags state,
  18229. const struct nk_style_button *style)
  18230. {
  18231. const struct nk_style_item *background;
  18232. if (state & NK_WIDGET_STATE_HOVER)
  18233. background = &style->hover;
  18234. else if (state & NK_WIDGET_STATE_ACTIVED)
  18235. background = &style->active;
  18236. else background = &style->normal;
  18237. if (background->type == NK_STYLE_ITEM_IMAGE) {
  18238. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  18239. } else {
  18240. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  18241. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  18242. }
  18243. return background;
  18244. }
  18245. NK_LIB int
  18246. nk_do_button(nk_flags *state, struct nk_command_buffer *out, struct nk_rect r,
  18247. const struct nk_style_button *style, const struct nk_input *in,
  18248. enum nk_button_behavior behavior, struct nk_rect *content)
  18249. {
  18250. struct nk_rect bounds;
  18251. NK_ASSERT(style);
  18252. NK_ASSERT(state);
  18253. NK_ASSERT(out);
  18254. if (!out || !style)
  18255. return nk_false;
  18256. /* calculate button content space */
  18257. content->x = r.x + style->padding.x + style->border + style->rounding;
  18258. content->y = r.y + style->padding.y + style->border + style->rounding;
  18259. content->w = r.w - (2 * style->padding.x + style->border + style->rounding*2);
  18260. content->h = r.h - (2 * style->padding.y + style->border + style->rounding*2);
  18261. /* execute button behavior */
  18262. bounds.x = r.x - style->touch_padding.x;
  18263. bounds.y = r.y - style->touch_padding.y;
  18264. bounds.w = r.w + 2 * style->touch_padding.x;
  18265. bounds.h = r.h + 2 * style->touch_padding.y;
  18266. return nk_button_behavior(state, bounds, in, behavior);
  18267. }
  18268. NK_LIB void
  18269. nk_draw_button_text(struct nk_command_buffer *out,
  18270. const struct nk_rect *bounds, const struct nk_rect *content, nk_flags state,
  18271. const struct nk_style_button *style, const char *txt, int len,
  18272. nk_flags text_alignment, const struct nk_user_font *font)
  18273. {
  18274. struct nk_text text;
  18275. const struct nk_style_item *background;
  18276. background = nk_draw_button(out, bounds, state, style);
  18277. /* select correct colors/images */
  18278. if (background->type == NK_STYLE_ITEM_COLOR)
  18279. text.background = background->data.color;
  18280. else text.background = style->text_background;
  18281. if (state & NK_WIDGET_STATE_HOVER)
  18282. text.text = style->text_hover;
  18283. else if (state & NK_WIDGET_STATE_ACTIVED)
  18284. text.text = style->text_active;
  18285. else text.text = style->text_normal;
  18286. text.padding = nk_vec2(0,0);
  18287. nk_widget_text(out, *content, txt, len, &text, text_alignment, font);
  18288. }
  18289. NK_LIB int
  18290. nk_do_button_text(nk_flags *state,
  18291. struct nk_command_buffer *out, struct nk_rect bounds,
  18292. const char *string, int len, nk_flags align, enum nk_button_behavior behavior,
  18293. const struct nk_style_button *style, const struct nk_input *in,
  18294. const struct nk_user_font *font)
  18295. {
  18296. struct nk_rect content;
  18297. int ret = nk_false;
  18298. NK_ASSERT(state);
  18299. NK_ASSERT(style);
  18300. NK_ASSERT(out);
  18301. NK_ASSERT(string);
  18302. NK_ASSERT(font);
  18303. if (!out || !style || !font || !string)
  18304. return nk_false;
  18305. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  18306. if (style->draw_begin) style->draw_begin(out, style->userdata);
  18307. nk_draw_button_text(out, &bounds, &content, *state, style, string, len, align, font);
  18308. if (style->draw_end) style->draw_end(out, style->userdata);
  18309. return ret;
  18310. }
  18311. NK_LIB void
  18312. nk_draw_button_symbol(struct nk_command_buffer *out,
  18313. const struct nk_rect *bounds, const struct nk_rect *content,
  18314. nk_flags state, const struct nk_style_button *style,
  18315. enum nk_symbol_type type, const struct nk_user_font *font)
  18316. {
  18317. struct nk_color sym, bg;
  18318. const struct nk_style_item *background;
  18319. /* select correct colors/images */
  18320. background = nk_draw_button(out, bounds, state, style);
  18321. if (background->type == NK_STYLE_ITEM_COLOR)
  18322. bg = background->data.color;
  18323. else bg = style->text_background;
  18324. if (state & NK_WIDGET_STATE_HOVER)
  18325. sym = style->text_hover;
  18326. else if (state & NK_WIDGET_STATE_ACTIVED)
  18327. sym = style->text_active;
  18328. else sym = style->text_normal;
  18329. nk_draw_symbol(out, type, *content, bg, sym, 1, font);
  18330. }
  18331. NK_LIB int
  18332. nk_do_button_symbol(nk_flags *state,
  18333. struct nk_command_buffer *out, struct nk_rect bounds,
  18334. enum nk_symbol_type symbol, enum nk_button_behavior behavior,
  18335. const struct nk_style_button *style, const struct nk_input *in,
  18336. const struct nk_user_font *font)
  18337. {
  18338. int ret;
  18339. struct nk_rect content;
  18340. NK_ASSERT(state);
  18341. NK_ASSERT(style);
  18342. NK_ASSERT(font);
  18343. NK_ASSERT(out);
  18344. if (!out || !style || !font || !state)
  18345. return nk_false;
  18346. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  18347. if (style->draw_begin) style->draw_begin(out, style->userdata);
  18348. nk_draw_button_symbol(out, &bounds, &content, *state, style, symbol, font);
  18349. if (style->draw_end) style->draw_end(out, style->userdata);
  18350. return ret;
  18351. }
  18352. NK_LIB void
  18353. nk_draw_button_image(struct nk_command_buffer *out,
  18354. const struct nk_rect *bounds, const struct nk_rect *content,
  18355. nk_flags state, const struct nk_style_button *style, const struct nk_image *img)
  18356. {
  18357. nk_draw_button(out, bounds, state, style);
  18358. nk_draw_image(out, *content, img, nk_white);
  18359. }
  18360. NK_LIB int
  18361. nk_do_button_image(nk_flags *state,
  18362. struct nk_command_buffer *out, struct nk_rect bounds,
  18363. struct nk_image img, enum nk_button_behavior b,
  18364. const struct nk_style_button *style, const struct nk_input *in)
  18365. {
  18366. int ret;
  18367. struct nk_rect content;
  18368. NK_ASSERT(state);
  18369. NK_ASSERT(style);
  18370. NK_ASSERT(out);
  18371. if (!out || !style || !state)
  18372. return nk_false;
  18373. ret = nk_do_button(state, out, bounds, style, in, b, &content);
  18374. content.x += style->image_padding.x;
  18375. content.y += style->image_padding.y;
  18376. content.w -= 2 * style->image_padding.x;
  18377. content.h -= 2 * style->image_padding.y;
  18378. if (style->draw_begin) style->draw_begin(out, style->userdata);
  18379. nk_draw_button_image(out, &bounds, &content, *state, style, &img);
  18380. if (style->draw_end) style->draw_end(out, style->userdata);
  18381. return ret;
  18382. }
  18383. NK_LIB void
  18384. nk_draw_button_text_symbol(struct nk_command_buffer *out,
  18385. const struct nk_rect *bounds, const struct nk_rect *label,
  18386. const struct nk_rect *symbol, nk_flags state, const struct nk_style_button *style,
  18387. const char *str, int len, enum nk_symbol_type type,
  18388. const struct nk_user_font *font)
  18389. {
  18390. struct nk_color sym;
  18391. struct nk_text text;
  18392. const struct nk_style_item *background;
  18393. /* select correct background colors/images */
  18394. background = nk_draw_button(out, bounds, state, style);
  18395. if (background->type == NK_STYLE_ITEM_COLOR)
  18396. text.background = background->data.color;
  18397. else text.background = style->text_background;
  18398. /* select correct text colors */
  18399. if (state & NK_WIDGET_STATE_HOVER) {
  18400. sym = style->text_hover;
  18401. text.text = style->text_hover;
  18402. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  18403. sym = style->text_active;
  18404. text.text = style->text_active;
  18405. } else {
  18406. sym = style->text_normal;
  18407. text.text = style->text_normal;
  18408. }
  18409. text.padding = nk_vec2(0,0);
  18410. nk_draw_symbol(out, type, *symbol, style->text_background, sym, 0, font);
  18411. nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
  18412. }
  18413. NK_LIB int
  18414. nk_do_button_text_symbol(nk_flags *state,
  18415. struct nk_command_buffer *out, struct nk_rect bounds,
  18416. enum nk_symbol_type symbol, const char *str, int len, nk_flags align,
  18417. enum nk_button_behavior behavior, const struct nk_style_button *style,
  18418. const struct nk_user_font *font, const struct nk_input *in)
  18419. {
  18420. int ret;
  18421. struct nk_rect tri = {0,0,0,0};
  18422. struct nk_rect content;
  18423. NK_ASSERT(style);
  18424. NK_ASSERT(out);
  18425. NK_ASSERT(font);
  18426. if (!out || !style || !font)
  18427. return nk_false;
  18428. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  18429. tri.y = content.y + (content.h/2) - font->height/2;
  18430. tri.w = font->height; tri.h = font->height;
  18431. if (align & NK_TEXT_ALIGN_LEFT) {
  18432. tri.x = (content.x + content.w) - (2 * style->padding.x + tri.w);
  18433. tri.x = NK_MAX(tri.x, 0);
  18434. } else tri.x = content.x + 2 * style->padding.x;
  18435. /* draw button */
  18436. if (style->draw_begin) style->draw_begin(out, style->userdata);
  18437. nk_draw_button_text_symbol(out, &bounds, &content, &tri,
  18438. *state, style, str, len, symbol, font);
  18439. if (style->draw_end) style->draw_end(out, style->userdata);
  18440. return ret;
  18441. }
  18442. NK_LIB void
  18443. nk_draw_button_text_image(struct nk_command_buffer *out,
  18444. const struct nk_rect *bounds, const struct nk_rect *label,
  18445. const struct nk_rect *image, nk_flags state, const struct nk_style_button *style,
  18446. const char *str, int len, const struct nk_user_font *font,
  18447. const struct nk_image *img)
  18448. {
  18449. struct nk_text text;
  18450. const struct nk_style_item *background;
  18451. background = nk_draw_button(out, bounds, state, style);
  18452. /* select correct colors */
  18453. if (background->type == NK_STYLE_ITEM_COLOR)
  18454. text.background = background->data.color;
  18455. else text.background = style->text_background;
  18456. if (state & NK_WIDGET_STATE_HOVER)
  18457. text.text = style->text_hover;
  18458. else if (state & NK_WIDGET_STATE_ACTIVED)
  18459. text.text = style->text_active;
  18460. else text.text = style->text_normal;
  18461. text.padding = nk_vec2(0,0);
  18462. nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
  18463. nk_draw_image(out, *image, img, nk_white);
  18464. }
  18465. NK_LIB int
  18466. nk_do_button_text_image(nk_flags *state,
  18467. struct nk_command_buffer *out, struct nk_rect bounds,
  18468. struct nk_image img, const char* str, int len, nk_flags align,
  18469. enum nk_button_behavior behavior, const struct nk_style_button *style,
  18470. const struct nk_user_font *font, const struct nk_input *in)
  18471. {
  18472. int ret;
  18473. struct nk_rect icon;
  18474. struct nk_rect content;
  18475. NK_ASSERT(style);
  18476. NK_ASSERT(state);
  18477. NK_ASSERT(font);
  18478. NK_ASSERT(out);
  18479. if (!out || !font || !style || !str)
  18480. return nk_false;
  18481. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  18482. icon.y = bounds.y + style->padding.y;
  18483. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  18484. if (align & NK_TEXT_ALIGN_LEFT) {
  18485. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  18486. icon.x = NK_MAX(icon.x, 0);
  18487. } else icon.x = bounds.x + 2 * style->padding.x;
  18488. icon.x += style->image_padding.x;
  18489. icon.y += style->image_padding.y;
  18490. icon.w -= 2 * style->image_padding.x;
  18491. icon.h -= 2 * style->image_padding.y;
  18492. if (style->draw_begin) style->draw_begin(out, style->userdata);
  18493. nk_draw_button_text_image(out, &bounds, &content, &icon, *state, style, str, len, font, &img);
  18494. if (style->draw_end) style->draw_end(out, style->userdata);
  18495. return ret;
  18496. }
  18497. NK_API void
  18498. nk_button_set_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
  18499. {
  18500. NK_ASSERT(ctx);
  18501. if (!ctx) return;
  18502. ctx->button_behavior = behavior;
  18503. }
  18504. NK_API int
  18505. nk_button_push_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
  18506. {
  18507. struct nk_config_stack_button_behavior *button_stack;
  18508. struct nk_config_stack_button_behavior_element *element;
  18509. NK_ASSERT(ctx);
  18510. if (!ctx) return 0;
  18511. button_stack = &ctx->stacks.button_behaviors;
  18512. NK_ASSERT(button_stack->head < (int)NK_LEN(button_stack->elements));
  18513. if (button_stack->head >= (int)NK_LEN(button_stack->elements))
  18514. return 0;
  18515. element = &button_stack->elements[button_stack->head++];
  18516. element->address = &ctx->button_behavior;
  18517. element->old_value = ctx->button_behavior;
  18518. ctx->button_behavior = behavior;
  18519. return 1;
  18520. }
  18521. NK_API int
  18522. nk_button_pop_behavior(struct nk_context *ctx)
  18523. {
  18524. struct nk_config_stack_button_behavior *button_stack;
  18525. struct nk_config_stack_button_behavior_element *element;
  18526. NK_ASSERT(ctx);
  18527. if (!ctx) return 0;
  18528. button_stack = &ctx->stacks.button_behaviors;
  18529. NK_ASSERT(button_stack->head > 0);
  18530. if (button_stack->head < 1)
  18531. return 0;
  18532. element = &button_stack->elements[--button_stack->head];
  18533. *element->address = element->old_value;
  18534. return 1;
  18535. }
  18536. NK_API int
  18537. nk_button_text_styled(struct nk_context *ctx,
  18538. const struct nk_style_button *style, const char *title, int len)
  18539. {
  18540. struct nk_window *win;
  18541. struct nk_panel *layout;
  18542. const struct nk_input *in;
  18543. struct nk_rect bounds;
  18544. enum nk_widget_layout_states state;
  18545. NK_ASSERT(ctx);
  18546. NK_ASSERT(style);
  18547. NK_ASSERT(ctx->current);
  18548. NK_ASSERT(ctx->current->layout);
  18549. if (!style || !ctx || !ctx->current || !ctx->current->layout) return 0;
  18550. win = ctx->current;
  18551. layout = win->layout;
  18552. state = nk_widget(&bounds, ctx);
  18553. if (!state) return 0;
  18554. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18555. return nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
  18556. title, len, style->text_alignment, ctx->button_behavior,
  18557. style, in, ctx->style.font);
  18558. }
  18559. NK_API int
  18560. nk_button_text(struct nk_context *ctx, const char *title, int len)
  18561. {
  18562. NK_ASSERT(ctx);
  18563. if (!ctx) return 0;
  18564. return nk_button_text_styled(ctx, &ctx->style.button, title, len);
  18565. }
  18566. NK_API int nk_button_label_styled(struct nk_context *ctx,
  18567. const struct nk_style_button *style, const char *title)
  18568. {
  18569. return nk_button_text_styled(ctx, style, title, nk_strlen(title));
  18570. }
  18571. NK_API int nk_button_label(struct nk_context *ctx, const char *title)
  18572. {
  18573. return nk_button_text(ctx, title, nk_strlen(title));
  18574. }
  18575. NK_API int
  18576. nk_button_color(struct nk_context *ctx, struct nk_color color)
  18577. {
  18578. struct nk_window *win;
  18579. struct nk_panel *layout;
  18580. const struct nk_input *in;
  18581. struct nk_style_button button;
  18582. int ret = 0;
  18583. struct nk_rect bounds;
  18584. struct nk_rect content;
  18585. enum nk_widget_layout_states state;
  18586. NK_ASSERT(ctx);
  18587. NK_ASSERT(ctx->current);
  18588. NK_ASSERT(ctx->current->layout);
  18589. if (!ctx || !ctx->current || !ctx->current->layout)
  18590. return 0;
  18591. win = ctx->current;
  18592. layout = win->layout;
  18593. state = nk_widget(&bounds, ctx);
  18594. if (!state) return 0;
  18595. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18596. button = ctx->style.button;
  18597. button.normal = nk_style_item_color(color);
  18598. button.hover = nk_style_item_color(color);
  18599. button.active = nk_style_item_color(color);
  18600. ret = nk_do_button(&ctx->last_widget_state, &win->buffer, bounds,
  18601. &button, in, ctx->button_behavior, &content);
  18602. nk_draw_button(&win->buffer, &bounds, ctx->last_widget_state, &button);
  18603. return ret;
  18604. }
  18605. NK_API int
  18606. nk_button_symbol_styled(struct nk_context *ctx,
  18607. const struct nk_style_button *style, enum nk_symbol_type symbol)
  18608. {
  18609. struct nk_window *win;
  18610. struct nk_panel *layout;
  18611. const struct nk_input *in;
  18612. struct nk_rect bounds;
  18613. enum nk_widget_layout_states state;
  18614. NK_ASSERT(ctx);
  18615. NK_ASSERT(ctx->current);
  18616. NK_ASSERT(ctx->current->layout);
  18617. if (!ctx || !ctx->current || !ctx->current->layout)
  18618. return 0;
  18619. win = ctx->current;
  18620. layout = win->layout;
  18621. state = nk_widget(&bounds, ctx);
  18622. if (!state) return 0;
  18623. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18624. return nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  18625. symbol, ctx->button_behavior, style, in, ctx->style.font);
  18626. }
  18627. NK_API int
  18628. nk_button_symbol(struct nk_context *ctx, enum nk_symbol_type symbol)
  18629. {
  18630. NK_ASSERT(ctx);
  18631. if (!ctx) return 0;
  18632. return nk_button_symbol_styled(ctx, &ctx->style.button, symbol);
  18633. }
  18634. NK_API int
  18635. nk_button_image_styled(struct nk_context *ctx, const struct nk_style_button *style,
  18636. struct nk_image img)
  18637. {
  18638. struct nk_window *win;
  18639. struct nk_panel *layout;
  18640. const struct nk_input *in;
  18641. struct nk_rect bounds;
  18642. enum nk_widget_layout_states state;
  18643. NK_ASSERT(ctx);
  18644. NK_ASSERT(ctx->current);
  18645. NK_ASSERT(ctx->current->layout);
  18646. if (!ctx || !ctx->current || !ctx->current->layout)
  18647. return 0;
  18648. win = ctx->current;
  18649. layout = win->layout;
  18650. state = nk_widget(&bounds, ctx);
  18651. if (!state) return 0;
  18652. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18653. return nk_do_button_image(&ctx->last_widget_state, &win->buffer, bounds,
  18654. img, ctx->button_behavior, style, in);
  18655. }
  18656. NK_API int
  18657. nk_button_image(struct nk_context *ctx, struct nk_image img)
  18658. {
  18659. NK_ASSERT(ctx);
  18660. if (!ctx) return 0;
  18661. return nk_button_image_styled(ctx, &ctx->style.button, img);
  18662. }
  18663. NK_API int
  18664. nk_button_symbol_text_styled(struct nk_context *ctx,
  18665. const struct nk_style_button *style, enum nk_symbol_type symbol,
  18666. const char *text, int len, nk_flags align)
  18667. {
  18668. struct nk_window *win;
  18669. struct nk_panel *layout;
  18670. const struct nk_input *in;
  18671. struct nk_rect bounds;
  18672. enum nk_widget_layout_states state;
  18673. NK_ASSERT(ctx);
  18674. NK_ASSERT(ctx->current);
  18675. NK_ASSERT(ctx->current->layout);
  18676. if (!ctx || !ctx->current || !ctx->current->layout)
  18677. return 0;
  18678. win = ctx->current;
  18679. layout = win->layout;
  18680. state = nk_widget(&bounds, ctx);
  18681. if (!state) return 0;
  18682. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18683. return nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  18684. symbol, text, len, align, ctx->button_behavior,
  18685. style, ctx->style.font, in);
  18686. }
  18687. NK_API int
  18688. nk_button_symbol_text(struct nk_context *ctx, enum nk_symbol_type symbol,
  18689. const char* text, int len, nk_flags align)
  18690. {
  18691. NK_ASSERT(ctx);
  18692. if (!ctx) return 0;
  18693. return nk_button_symbol_text_styled(ctx, &ctx->style.button, symbol, text, len, align);
  18694. }
  18695. NK_API int nk_button_symbol_label(struct nk_context *ctx, enum nk_symbol_type symbol,
  18696. const char *label, nk_flags align)
  18697. {
  18698. return nk_button_symbol_text(ctx, symbol, label, nk_strlen(label), align);
  18699. }
  18700. NK_API int nk_button_symbol_label_styled(struct nk_context *ctx,
  18701. const struct nk_style_button *style, enum nk_symbol_type symbol,
  18702. const char *title, nk_flags align)
  18703. {
  18704. return nk_button_symbol_text_styled(ctx, style, symbol, title, nk_strlen(title), align);
  18705. }
  18706. NK_API int
  18707. nk_button_image_text_styled(struct nk_context *ctx,
  18708. const struct nk_style_button *style, struct nk_image img, const char *text,
  18709. int len, nk_flags align)
  18710. {
  18711. struct nk_window *win;
  18712. struct nk_panel *layout;
  18713. const struct nk_input *in;
  18714. struct nk_rect bounds;
  18715. enum nk_widget_layout_states state;
  18716. NK_ASSERT(ctx);
  18717. NK_ASSERT(ctx->current);
  18718. NK_ASSERT(ctx->current->layout);
  18719. if (!ctx || !ctx->current || !ctx->current->layout)
  18720. return 0;
  18721. win = ctx->current;
  18722. layout = win->layout;
  18723. state = nk_widget(&bounds, ctx);
  18724. if (!state) return 0;
  18725. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18726. return nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
  18727. bounds, img, text, len, align, ctx->button_behavior,
  18728. style, ctx->style.font, in);
  18729. }
  18730. NK_API int
  18731. nk_button_image_text(struct nk_context *ctx, struct nk_image img,
  18732. const char *text, int len, nk_flags align)
  18733. {
  18734. return nk_button_image_text_styled(ctx, &ctx->style.button,img, text, len, align);
  18735. }
  18736. NK_API int nk_button_image_label(struct nk_context *ctx, struct nk_image img,
  18737. const char *label, nk_flags align)
  18738. {
  18739. return nk_button_image_text(ctx, img, label, nk_strlen(label), align);
  18740. }
  18741. NK_API int nk_button_image_label_styled(struct nk_context *ctx,
  18742. const struct nk_style_button *style, struct nk_image img,
  18743. const char *label, nk_flags text_alignment)
  18744. {
  18745. return nk_button_image_text_styled(ctx, style, img, label, nk_strlen(label), text_alignment);
  18746. }
  18747. /* ===============================================================
  18748. *
  18749. * TOGGLE
  18750. *
  18751. * ===============================================================*/
  18752. NK_LIB int
  18753. nk_toggle_behavior(const struct nk_input *in, struct nk_rect select,
  18754. nk_flags *state, int active)
  18755. {
  18756. nk_widget_state_reset(state);
  18757. if (nk_button_behavior(state, select, in, NK_BUTTON_DEFAULT)) {
  18758. *state = NK_WIDGET_STATE_ACTIVE;
  18759. active = !active;
  18760. }
  18761. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, select))
  18762. *state |= NK_WIDGET_STATE_ENTERED;
  18763. else if (nk_input_is_mouse_prev_hovering_rect(in, select))
  18764. *state |= NK_WIDGET_STATE_LEFT;
  18765. return active;
  18766. }
  18767. NK_LIB void
  18768. nk_draw_checkbox(struct nk_command_buffer *out,
  18769. nk_flags state, const struct nk_style_toggle *style, int active,
  18770. const struct nk_rect *label, const struct nk_rect *selector,
  18771. const struct nk_rect *cursors, const char *string, int len,
  18772. const struct nk_user_font *font)
  18773. {
  18774. const struct nk_style_item *background;
  18775. const struct nk_style_item *cursor;
  18776. struct nk_text text;
  18777. /* select correct colors/images */
  18778. if (state & NK_WIDGET_STATE_HOVER) {
  18779. background = &style->hover;
  18780. cursor = &style->cursor_hover;
  18781. text.text = style->text_hover;
  18782. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  18783. background = &style->hover;
  18784. cursor = &style->cursor_hover;
  18785. text.text = style->text_active;
  18786. } else {
  18787. background = &style->normal;
  18788. cursor = &style->cursor_normal;
  18789. text.text = style->text_normal;
  18790. }
  18791. /* draw background and cursor */
  18792. if (background->type == NK_STYLE_ITEM_COLOR) {
  18793. nk_fill_rect(out, *selector, 0, style->border_color);
  18794. nk_fill_rect(out, nk_shrink_rect(*selector, style->border), 0, background->data.color);
  18795. } else nk_draw_image(out, *selector, &background->data.image, nk_white);
  18796. if (active) {
  18797. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  18798. nk_draw_image(out, *cursors, &cursor->data.image, nk_white);
  18799. else nk_fill_rect(out, *cursors, 0, cursor->data.color);
  18800. }
  18801. text.padding.x = 0;
  18802. text.padding.y = 0;
  18803. text.background = style->text_background;
  18804. nk_widget_text(out, *label, string, len, &text, NK_TEXT_LEFT, font);
  18805. }
  18806. NK_LIB void
  18807. nk_draw_option(struct nk_command_buffer *out,
  18808. nk_flags state, const struct nk_style_toggle *style, int active,
  18809. const struct nk_rect *label, const struct nk_rect *selector,
  18810. const struct nk_rect *cursors, const char *string, int len,
  18811. const struct nk_user_font *font)
  18812. {
  18813. const struct nk_style_item *background;
  18814. const struct nk_style_item *cursor;
  18815. struct nk_text text;
  18816. /* select correct colors/images */
  18817. if (state & NK_WIDGET_STATE_HOVER) {
  18818. background = &style->hover;
  18819. cursor = &style->cursor_hover;
  18820. text.text = style->text_hover;
  18821. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  18822. background = &style->hover;
  18823. cursor = &style->cursor_hover;
  18824. text.text = style->text_active;
  18825. } else {
  18826. background = &style->normal;
  18827. cursor = &style->cursor_normal;
  18828. text.text = style->text_normal;
  18829. }
  18830. /* draw background and cursor */
  18831. if (background->type == NK_STYLE_ITEM_COLOR) {
  18832. nk_fill_circle(out, *selector, style->border_color);
  18833. nk_fill_circle(out, nk_shrink_rect(*selector, style->border), background->data.color);
  18834. } else nk_draw_image(out, *selector, &background->data.image, nk_white);
  18835. if (active) {
  18836. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  18837. nk_draw_image(out, *cursors, &cursor->data.image, nk_white);
  18838. else nk_fill_circle(out, *cursors, cursor->data.color);
  18839. }
  18840. text.padding.x = 0;
  18841. text.padding.y = 0;
  18842. text.background = style->text_background;
  18843. nk_widget_text(out, *label, string, len, &text, NK_TEXT_LEFT, font);
  18844. }
  18845. NK_LIB int
  18846. nk_do_toggle(nk_flags *state,
  18847. struct nk_command_buffer *out, struct nk_rect r,
  18848. int *active, const char *str, int len, enum nk_toggle_type type,
  18849. const struct nk_style_toggle *style, const struct nk_input *in,
  18850. const struct nk_user_font *font)
  18851. {
  18852. int was_active;
  18853. struct nk_rect bounds;
  18854. struct nk_rect select;
  18855. struct nk_rect cursor;
  18856. struct nk_rect label;
  18857. NK_ASSERT(style);
  18858. NK_ASSERT(out);
  18859. NK_ASSERT(font);
  18860. if (!out || !style || !font || !active)
  18861. return 0;
  18862. r.w = NK_MAX(r.w, font->height + 2 * style->padding.x);
  18863. r.h = NK_MAX(r.h, font->height + 2 * style->padding.y);
  18864. /* add additional touch padding for touch screen devices */
  18865. bounds.x = r.x - style->touch_padding.x;
  18866. bounds.y = r.y - style->touch_padding.y;
  18867. bounds.w = r.w + 2 * style->touch_padding.x;
  18868. bounds.h = r.h + 2 * style->touch_padding.y;
  18869. /* calculate the selector space */
  18870. select.w = font->height;
  18871. select.h = select.w;
  18872. select.y = r.y + r.h/2.0f - select.h/2.0f;
  18873. select.x = r.x;
  18874. /* calculate the bounds of the cursor inside the selector */
  18875. cursor.x = select.x + style->padding.x + style->border;
  18876. cursor.y = select.y + style->padding.y + style->border;
  18877. cursor.w = select.w - (2 * style->padding.x + 2 * style->border);
  18878. cursor.h = select.h - (2 * style->padding.y + 2 * style->border);
  18879. /* label behind the selector */
  18880. label.x = select.x + select.w + style->spacing;
  18881. label.y = select.y;
  18882. label.w = NK_MAX(r.x + r.w, label.x) - label.x;
  18883. label.h = select.w;
  18884. /* update selector */
  18885. was_active = *active;
  18886. *active = nk_toggle_behavior(in, bounds, state, *active);
  18887. /* draw selector */
  18888. if (style->draw_begin)
  18889. style->draw_begin(out, style->userdata);
  18890. if (type == NK_TOGGLE_CHECK) {
  18891. nk_draw_checkbox(out, *state, style, *active, &label, &select, &cursor, str, len, font);
  18892. } else {
  18893. nk_draw_option(out, *state, style, *active, &label, &select, &cursor, str, len, font);
  18894. }
  18895. if (style->draw_end)
  18896. style->draw_end(out, style->userdata);
  18897. return (was_active != *active);
  18898. }
  18899. /*----------------------------------------------------------------
  18900. *
  18901. * CHECKBOX
  18902. *
  18903. * --------------------------------------------------------------*/
  18904. NK_API int
  18905. nk_check_text(struct nk_context *ctx, const char *text, int len, int active)
  18906. {
  18907. struct nk_window *win;
  18908. struct nk_panel *layout;
  18909. const struct nk_input *in;
  18910. const struct nk_style *style;
  18911. struct nk_rect bounds;
  18912. enum nk_widget_layout_states state;
  18913. NK_ASSERT(ctx);
  18914. NK_ASSERT(ctx->current);
  18915. NK_ASSERT(ctx->current->layout);
  18916. if (!ctx || !ctx->current || !ctx->current->layout)
  18917. return active;
  18918. win = ctx->current;
  18919. style = &ctx->style;
  18920. layout = win->layout;
  18921. state = nk_widget(&bounds, ctx);
  18922. if (!state) return active;
  18923. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18924. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &active,
  18925. text, len, NK_TOGGLE_CHECK, &style->checkbox, in, style->font);
  18926. return active;
  18927. }
  18928. NK_API unsigned int
  18929. nk_check_flags_text(struct nk_context *ctx, const char *text, int len,
  18930. unsigned int flags, unsigned int value)
  18931. {
  18932. int old_active;
  18933. NK_ASSERT(ctx);
  18934. NK_ASSERT(text);
  18935. if (!ctx || !text) return flags;
  18936. old_active = (int)((flags & value) & value);
  18937. if (nk_check_text(ctx, text, len, old_active))
  18938. flags |= value;
  18939. else flags &= ~value;
  18940. return flags;
  18941. }
  18942. NK_API int
  18943. nk_checkbox_text(struct nk_context *ctx, const char *text, int len, int *active)
  18944. {
  18945. int old_val;
  18946. NK_ASSERT(ctx);
  18947. NK_ASSERT(text);
  18948. NK_ASSERT(active);
  18949. if (!ctx || !text || !active) return 0;
  18950. old_val = *active;
  18951. *active = nk_check_text(ctx, text, len, *active);
  18952. return old_val != *active;
  18953. }
  18954. NK_API int
  18955. nk_checkbox_flags_text(struct nk_context *ctx, const char *text, int len,
  18956. unsigned int *flags, unsigned int value)
  18957. {
  18958. int active;
  18959. NK_ASSERT(ctx);
  18960. NK_ASSERT(text);
  18961. NK_ASSERT(flags);
  18962. if (!ctx || !text || !flags) return 0;
  18963. active = (int)((*flags & value) & value);
  18964. if (nk_checkbox_text(ctx, text, len, &active)) {
  18965. if (active) *flags |= value;
  18966. else *flags &= ~value;
  18967. return 1;
  18968. }
  18969. return 0;
  18970. }
  18971. NK_API int nk_check_label(struct nk_context *ctx, const char *label, int active)
  18972. {
  18973. return nk_check_text(ctx, label, nk_strlen(label), active);
  18974. }
  18975. NK_API unsigned int nk_check_flags_label(struct nk_context *ctx, const char *label,
  18976. unsigned int flags, unsigned int value)
  18977. {
  18978. return nk_check_flags_text(ctx, label, nk_strlen(label), flags, value);
  18979. }
  18980. NK_API int nk_checkbox_label(struct nk_context *ctx, const char *label, int *active)
  18981. {
  18982. return nk_checkbox_text(ctx, label, nk_strlen(label), active);
  18983. }
  18984. NK_API int nk_checkbox_flags_label(struct nk_context *ctx, const char *label,
  18985. unsigned int *flags, unsigned int value)
  18986. {
  18987. return nk_checkbox_flags_text(ctx, label, nk_strlen(label), flags, value);
  18988. }
  18989. /*----------------------------------------------------------------
  18990. *
  18991. * OPTION
  18992. *
  18993. * --------------------------------------------------------------*/
  18994. NK_API int
  18995. nk_option_text(struct nk_context *ctx, const char *text, int len, int is_active)
  18996. {
  18997. struct nk_window *win;
  18998. struct nk_panel *layout;
  18999. const struct nk_input *in;
  19000. const struct nk_style *style;
  19001. struct nk_rect bounds;
  19002. enum nk_widget_layout_states state;
  19003. NK_ASSERT(ctx);
  19004. NK_ASSERT(ctx->current);
  19005. NK_ASSERT(ctx->current->layout);
  19006. if (!ctx || !ctx->current || !ctx->current->layout)
  19007. return is_active;
  19008. win = ctx->current;
  19009. style = &ctx->style;
  19010. layout = win->layout;
  19011. state = nk_widget(&bounds, ctx);
  19012. if (!state) return (int)state;
  19013. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19014. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &is_active,
  19015. text, len, NK_TOGGLE_OPTION, &style->option, in, style->font);
  19016. return is_active;
  19017. }
  19018. NK_API int
  19019. nk_radio_text(struct nk_context *ctx, const char *text, int len, int *active)
  19020. {
  19021. int old_value;
  19022. NK_ASSERT(ctx);
  19023. NK_ASSERT(text);
  19024. NK_ASSERT(active);
  19025. if (!ctx || !text || !active) return 0;
  19026. old_value = *active;
  19027. *active = nk_option_text(ctx, text, len, old_value);
  19028. return old_value != *active;
  19029. }
  19030. NK_API int
  19031. nk_option_label(struct nk_context *ctx, const char *label, int active)
  19032. {
  19033. return nk_option_text(ctx, label, nk_strlen(label), active);
  19034. }
  19035. NK_API int
  19036. nk_radio_label(struct nk_context *ctx, const char *label, int *active)
  19037. {
  19038. return nk_radio_text(ctx, label, nk_strlen(label), active);
  19039. }
  19040. /* ===============================================================
  19041. *
  19042. * SELECTABLE
  19043. *
  19044. * ===============================================================*/
  19045. NK_LIB void
  19046. nk_draw_selectable(struct nk_command_buffer *out,
  19047. nk_flags state, const struct nk_style_selectable *style, int active,
  19048. const struct nk_rect *bounds,
  19049. const struct nk_rect *icon, const struct nk_image *img, enum nk_symbol_type sym,
  19050. const char *string, int len, nk_flags align, const struct nk_user_font *font)
  19051. {
  19052. const struct nk_style_item *background;
  19053. struct nk_text text;
  19054. text.padding = style->padding;
  19055. /* select correct colors/images */
  19056. if (!active) {
  19057. if (state & NK_WIDGET_STATE_ACTIVED) {
  19058. background = &style->pressed;
  19059. text.text = style->text_pressed;
  19060. } else if (state & NK_WIDGET_STATE_HOVER) {
  19061. background = &style->hover;
  19062. text.text = style->text_hover;
  19063. } else {
  19064. background = &style->normal;
  19065. text.text = style->text_normal;
  19066. }
  19067. } else {
  19068. if (state & NK_WIDGET_STATE_ACTIVED) {
  19069. background = &style->pressed_active;
  19070. text.text = style->text_pressed_active;
  19071. } else if (state & NK_WIDGET_STATE_HOVER) {
  19072. background = &style->hover_active;
  19073. text.text = style->text_hover_active;
  19074. } else {
  19075. background = &style->normal_active;
  19076. text.text = style->text_normal_active;
  19077. }
  19078. }
  19079. /* draw selectable background and text */
  19080. if (background->type == NK_STYLE_ITEM_IMAGE) {
  19081. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  19082. text.background = nk_rgba(0,0,0,0);
  19083. } else {
  19084. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  19085. text.background = background->data.color;
  19086. }
  19087. if (icon) {
  19088. if (img) nk_draw_image(out, *icon, img, nk_white);
  19089. else nk_draw_symbol(out, sym, *icon, text.background, text.text, 1, font);
  19090. }
  19091. nk_widget_text(out, *bounds, string, len, &text, align, font);
  19092. }
  19093. NK_LIB int
  19094. nk_do_selectable(nk_flags *state, struct nk_command_buffer *out,
  19095. struct nk_rect bounds, const char *str, int len, nk_flags align, int *value,
  19096. const struct nk_style_selectable *style, const struct nk_input *in,
  19097. const struct nk_user_font *font)
  19098. {
  19099. int old_value;
  19100. struct nk_rect touch;
  19101. NK_ASSERT(state);
  19102. NK_ASSERT(out);
  19103. NK_ASSERT(str);
  19104. NK_ASSERT(len);
  19105. NK_ASSERT(value);
  19106. NK_ASSERT(style);
  19107. NK_ASSERT(font);
  19108. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  19109. old_value = *value;
  19110. /* remove padding */
  19111. touch.x = bounds.x - style->touch_padding.x;
  19112. touch.y = bounds.y - style->touch_padding.y;
  19113. touch.w = bounds.w + style->touch_padding.x * 2;
  19114. touch.h = bounds.h + style->touch_padding.y * 2;
  19115. /* update button */
  19116. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  19117. *value = !(*value);
  19118. /* draw selectable */
  19119. if (style->draw_begin) style->draw_begin(out, style->userdata);
  19120. nk_draw_selectable(out, *state, style, *value, &bounds, 0,0,NK_SYMBOL_NONE, str, len, align, font);
  19121. if (style->draw_end) style->draw_end(out, style->userdata);
  19122. return old_value != *value;
  19123. }
  19124. NK_LIB int
  19125. nk_do_selectable_image(nk_flags *state, struct nk_command_buffer *out,
  19126. struct nk_rect bounds, const char *str, int len, nk_flags align, int *value,
  19127. const struct nk_image *img, const struct nk_style_selectable *style,
  19128. const struct nk_input *in, const struct nk_user_font *font)
  19129. {
  19130. int old_value;
  19131. struct nk_rect touch;
  19132. struct nk_rect icon;
  19133. NK_ASSERT(state);
  19134. NK_ASSERT(out);
  19135. NK_ASSERT(str);
  19136. NK_ASSERT(len);
  19137. NK_ASSERT(value);
  19138. NK_ASSERT(style);
  19139. NK_ASSERT(font);
  19140. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  19141. old_value = *value;
  19142. /* toggle behavior */
  19143. touch.x = bounds.x - style->touch_padding.x;
  19144. touch.y = bounds.y - style->touch_padding.y;
  19145. touch.w = bounds.w + style->touch_padding.x * 2;
  19146. touch.h = bounds.h + style->touch_padding.y * 2;
  19147. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  19148. *value = !(*value);
  19149. icon.y = bounds.y + style->padding.y;
  19150. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  19151. if (align & NK_TEXT_ALIGN_LEFT) {
  19152. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  19153. icon.x = NK_MAX(icon.x, 0);
  19154. } else icon.x = bounds.x + 2 * style->padding.x;
  19155. icon.x += style->image_padding.x;
  19156. icon.y += style->image_padding.y;
  19157. icon.w -= 2 * style->image_padding.x;
  19158. icon.h -= 2 * style->image_padding.y;
  19159. /* draw selectable */
  19160. if (style->draw_begin) style->draw_begin(out, style->userdata);
  19161. nk_draw_selectable(out, *state, style, *value, &bounds, &icon, img, NK_SYMBOL_NONE, str, len, align, font);
  19162. if (style->draw_end) style->draw_end(out, style->userdata);
  19163. return old_value != *value;
  19164. }
  19165. NK_LIB int
  19166. nk_do_selectable_symbol(nk_flags *state, struct nk_command_buffer *out,
  19167. struct nk_rect bounds, const char *str, int len, nk_flags align, int *value,
  19168. enum nk_symbol_type sym, const struct nk_style_selectable *style,
  19169. const struct nk_input *in, const struct nk_user_font *font)
  19170. {
  19171. int old_value;
  19172. struct nk_rect touch;
  19173. struct nk_rect icon;
  19174. NK_ASSERT(state);
  19175. NK_ASSERT(out);
  19176. NK_ASSERT(str);
  19177. NK_ASSERT(len);
  19178. NK_ASSERT(value);
  19179. NK_ASSERT(style);
  19180. NK_ASSERT(font);
  19181. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  19182. old_value = *value;
  19183. /* toggle behavior */
  19184. touch.x = bounds.x - style->touch_padding.x;
  19185. touch.y = bounds.y - style->touch_padding.y;
  19186. touch.w = bounds.w + style->touch_padding.x * 2;
  19187. touch.h = bounds.h + style->touch_padding.y * 2;
  19188. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  19189. *value = !(*value);
  19190. icon.y = bounds.y + style->padding.y;
  19191. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  19192. if (align & NK_TEXT_ALIGN_LEFT) {
  19193. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  19194. icon.x = NK_MAX(icon.x, 0);
  19195. } else icon.x = bounds.x + 2 * style->padding.x;
  19196. icon.x += style->image_padding.x;
  19197. icon.y += style->image_padding.y;
  19198. icon.w -= 2 * style->image_padding.x;
  19199. icon.h -= 2 * style->image_padding.y;
  19200. /* draw selectable */
  19201. if (style->draw_begin) style->draw_begin(out, style->userdata);
  19202. nk_draw_selectable(out, *state, style, *value, &bounds, &icon, 0, sym, str, len, align, font);
  19203. if (style->draw_end) style->draw_end(out, style->userdata);
  19204. return old_value != *value;
  19205. }
  19206. NK_API int
  19207. nk_selectable_text(struct nk_context *ctx, const char *str, int len,
  19208. nk_flags align, int *value)
  19209. {
  19210. struct nk_window *win;
  19211. struct nk_panel *layout;
  19212. const struct nk_input *in;
  19213. const struct nk_style *style;
  19214. enum nk_widget_layout_states state;
  19215. struct nk_rect bounds;
  19216. NK_ASSERT(ctx);
  19217. NK_ASSERT(value);
  19218. NK_ASSERT(ctx->current);
  19219. NK_ASSERT(ctx->current->layout);
  19220. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  19221. return 0;
  19222. win = ctx->current;
  19223. layout = win->layout;
  19224. style = &ctx->style;
  19225. state = nk_widget(&bounds, ctx);
  19226. if (!state) return 0;
  19227. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19228. return nk_do_selectable(&ctx->last_widget_state, &win->buffer, bounds,
  19229. str, len, align, value, &style->selectable, in, style->font);
  19230. }
  19231. NK_API int
  19232. nk_selectable_image_text(struct nk_context *ctx, struct nk_image img,
  19233. const char *str, int len, nk_flags align, int *value)
  19234. {
  19235. struct nk_window *win;
  19236. struct nk_panel *layout;
  19237. const struct nk_input *in;
  19238. const struct nk_style *style;
  19239. enum nk_widget_layout_states state;
  19240. struct nk_rect bounds;
  19241. NK_ASSERT(ctx);
  19242. NK_ASSERT(value);
  19243. NK_ASSERT(ctx->current);
  19244. NK_ASSERT(ctx->current->layout);
  19245. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  19246. return 0;
  19247. win = ctx->current;
  19248. layout = win->layout;
  19249. style = &ctx->style;
  19250. state = nk_widget(&bounds, ctx);
  19251. if (!state) return 0;
  19252. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19253. return nk_do_selectable_image(&ctx->last_widget_state, &win->buffer, bounds,
  19254. str, len, align, value, &img, &style->selectable, in, style->font);
  19255. }
  19256. NK_API int
  19257. nk_selectable_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  19258. const char *str, int len, nk_flags align, int *value)
  19259. {
  19260. struct nk_window *win;
  19261. struct nk_panel *layout;
  19262. const struct nk_input *in;
  19263. const struct nk_style *style;
  19264. enum nk_widget_layout_states state;
  19265. struct nk_rect bounds;
  19266. NK_ASSERT(ctx);
  19267. NK_ASSERT(value);
  19268. NK_ASSERT(ctx->current);
  19269. NK_ASSERT(ctx->current->layout);
  19270. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  19271. return 0;
  19272. win = ctx->current;
  19273. layout = win->layout;
  19274. style = &ctx->style;
  19275. state = nk_widget(&bounds, ctx);
  19276. if (!state) return 0;
  19277. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19278. return nk_do_selectable_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  19279. str, len, align, value, sym, &style->selectable, in, style->font);
  19280. }
  19281. NK_API int
  19282. nk_selectable_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  19283. const char *title, nk_flags align, int *value)
  19284. {
  19285. return nk_selectable_symbol_text(ctx, sym, title, nk_strlen(title), align, value);
  19286. }
  19287. NK_API int nk_select_text(struct nk_context *ctx, const char *str, int len,
  19288. nk_flags align, int value)
  19289. {
  19290. nk_selectable_text(ctx, str, len, align, &value);return value;
  19291. }
  19292. NK_API int nk_selectable_label(struct nk_context *ctx, const char *str, nk_flags align, int *value)
  19293. {
  19294. return nk_selectable_text(ctx, str, nk_strlen(str), align, value);
  19295. }
  19296. NK_API int nk_selectable_image_label(struct nk_context *ctx,struct nk_image img,
  19297. const char *str, nk_flags align, int *value)
  19298. {
  19299. return nk_selectable_image_text(ctx, img, str, nk_strlen(str), align, value);
  19300. }
  19301. NK_API int nk_select_label(struct nk_context *ctx, const char *str, nk_flags align, int value)
  19302. {
  19303. nk_selectable_text(ctx, str, nk_strlen(str), align, &value);return value;
  19304. }
  19305. NK_API int nk_select_image_label(struct nk_context *ctx, struct nk_image img,
  19306. const char *str, nk_flags align, int value)
  19307. {
  19308. nk_selectable_image_text(ctx, img, str, nk_strlen(str), align, &value);return value;
  19309. }
  19310. NK_API int nk_select_image_text(struct nk_context *ctx, struct nk_image img,
  19311. const char *str, int len, nk_flags align, int value)
  19312. {
  19313. nk_selectable_image_text(ctx, img, str, len, align, &value);return value;
  19314. }
  19315. NK_API int
  19316. nk_select_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  19317. const char *title, int title_len, nk_flags align, int value)
  19318. {
  19319. nk_selectable_symbol_text(ctx, sym, title, title_len, align, &value);return value;
  19320. }
  19321. NK_API int
  19322. nk_select_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  19323. const char *title, nk_flags align, int value)
  19324. {
  19325. return nk_select_symbol_text(ctx, sym, title, nk_strlen(title), align, value);
  19326. }
  19327. /* ===============================================================
  19328. *
  19329. * SLIDER
  19330. *
  19331. * ===============================================================*/
  19332. NK_LIB float
  19333. nk_slider_behavior(nk_flags *state, struct nk_rect *logical_cursor,
  19334. struct nk_rect *visual_cursor, struct nk_input *in,
  19335. struct nk_rect bounds, float slider_min, float slider_max, float slider_value,
  19336. float slider_step, float slider_steps)
  19337. {
  19338. int left_mouse_down;
  19339. int left_mouse_click_in_cursor;
  19340. /* check if visual cursor is being dragged */
  19341. nk_widget_state_reset(state);
  19342. left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  19343. left_mouse_click_in_cursor = in && nk_input_has_mouse_click_down_in_rect(in,
  19344. NK_BUTTON_LEFT, *visual_cursor, nk_true);
  19345. if (left_mouse_down && left_mouse_click_in_cursor) {
  19346. float ratio = 0;
  19347. const float d = in->mouse.pos.x - (visual_cursor->x+visual_cursor->w*0.5f);
  19348. const float pxstep = bounds.w / slider_steps;
  19349. /* only update value if the next slider step is reached */
  19350. *state = NK_WIDGET_STATE_ACTIVE;
  19351. if (NK_ABS(d) >= pxstep) {
  19352. const float steps = (float)((int)(NK_ABS(d) / pxstep));
  19353. slider_value += (d > 0) ? (slider_step*steps) : -(slider_step*steps);
  19354. slider_value = NK_CLAMP(slider_min, slider_value, slider_max);
  19355. ratio = (slider_value - slider_min)/slider_step;
  19356. logical_cursor->x = bounds.x + (logical_cursor->w * ratio);
  19357. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = logical_cursor->x;
  19358. }
  19359. }
  19360. /* slider widget state */
  19361. if (nk_input_is_mouse_hovering_rect(in, bounds))
  19362. *state = NK_WIDGET_STATE_HOVERED;
  19363. if (*state & NK_WIDGET_STATE_HOVER &&
  19364. !nk_input_is_mouse_prev_hovering_rect(in, bounds))
  19365. *state |= NK_WIDGET_STATE_ENTERED;
  19366. else if (nk_input_is_mouse_prev_hovering_rect(in, bounds))
  19367. *state |= NK_WIDGET_STATE_LEFT;
  19368. return slider_value;
  19369. }
  19370. NK_LIB void
  19371. nk_draw_slider(struct nk_command_buffer *out, nk_flags state,
  19372. const struct nk_style_slider *style, const struct nk_rect *bounds,
  19373. const struct nk_rect *visual_cursor, float min, float value, float max)
  19374. {
  19375. struct nk_rect fill;
  19376. struct nk_rect bar;
  19377. const struct nk_style_item *background;
  19378. /* select correct slider images/colors */
  19379. struct nk_color bar_color;
  19380. const struct nk_style_item *cursor;
  19381. NK_UNUSED(min);
  19382. NK_UNUSED(max);
  19383. NK_UNUSED(value);
  19384. if (state & NK_WIDGET_STATE_ACTIVED) {
  19385. background = &style->active;
  19386. bar_color = style->bar_active;
  19387. cursor = &style->cursor_active;
  19388. } else if (state & NK_WIDGET_STATE_HOVER) {
  19389. background = &style->hover;
  19390. bar_color = style->bar_hover;
  19391. cursor = &style->cursor_hover;
  19392. } else {
  19393. background = &style->normal;
  19394. bar_color = style->bar_normal;
  19395. cursor = &style->cursor_normal;
  19396. }
  19397. /* calculate slider background bar */
  19398. bar.x = bounds->x;
  19399. bar.y = (visual_cursor->y + visual_cursor->h/2) - bounds->h/12;
  19400. bar.w = bounds->w;
  19401. bar.h = bounds->h/6;
  19402. /* filled background bar style */
  19403. fill.w = (visual_cursor->x + (visual_cursor->w/2.0f)) - bar.x;
  19404. fill.x = bar.x;
  19405. fill.y = bar.y;
  19406. fill.h = bar.h;
  19407. /* draw background */
  19408. if (background->type == NK_STYLE_ITEM_IMAGE) {
  19409. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  19410. } else {
  19411. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  19412. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  19413. }
  19414. /* draw slider bar */
  19415. nk_fill_rect(out, bar, style->rounding, bar_color);
  19416. nk_fill_rect(out, fill, style->rounding, style->bar_filled);
  19417. /* draw cursor */
  19418. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  19419. nk_draw_image(out, *visual_cursor, &cursor->data.image, nk_white);
  19420. else nk_fill_circle(out, *visual_cursor, cursor->data.color);
  19421. }
  19422. NK_LIB float
  19423. nk_do_slider(nk_flags *state,
  19424. struct nk_command_buffer *out, struct nk_rect bounds,
  19425. float min, float val, float max, float step,
  19426. const struct nk_style_slider *style, struct nk_input *in,
  19427. const struct nk_user_font *font)
  19428. {
  19429. float slider_range;
  19430. float slider_min;
  19431. float slider_max;
  19432. float slider_value;
  19433. float slider_steps;
  19434. float cursor_offset;
  19435. struct nk_rect visual_cursor;
  19436. struct nk_rect logical_cursor;
  19437. NK_ASSERT(style);
  19438. NK_ASSERT(out);
  19439. if (!out || !style)
  19440. return 0;
  19441. /* remove padding from slider bounds */
  19442. bounds.x = bounds.x + style->padding.x;
  19443. bounds.y = bounds.y + style->padding.y;
  19444. bounds.h = NK_MAX(bounds.h, 2*style->padding.y);
  19445. bounds.w = NK_MAX(bounds.w, 2*style->padding.x + style->cursor_size.x);
  19446. bounds.w -= 2 * style->padding.x;
  19447. bounds.h -= 2 * style->padding.y;
  19448. /* optional buttons */
  19449. if (style->show_buttons) {
  19450. nk_flags ws;
  19451. struct nk_rect button;
  19452. button.y = bounds.y;
  19453. button.w = bounds.h;
  19454. button.h = bounds.h;
  19455. /* decrement button */
  19456. button.x = bounds.x;
  19457. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol, NK_BUTTON_DEFAULT,
  19458. &style->dec_button, in, font))
  19459. val -= step;
  19460. /* increment button */
  19461. button.x = (bounds.x + bounds.w) - button.w;
  19462. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol, NK_BUTTON_DEFAULT,
  19463. &style->inc_button, in, font))
  19464. val += step;
  19465. bounds.x = bounds.x + button.w + style->spacing.x;
  19466. bounds.w = bounds.w - (2*button.w + 2*style->spacing.x);
  19467. }
  19468. /* remove one cursor size to support visual cursor */
  19469. bounds.x += style->cursor_size.x*0.5f;
  19470. bounds.w -= style->cursor_size.x;
  19471. /* make sure the provided values are correct */
  19472. slider_max = NK_MAX(min, max);
  19473. slider_min = NK_MIN(min, max);
  19474. slider_value = NK_CLAMP(slider_min, val, slider_max);
  19475. slider_range = slider_max - slider_min;
  19476. slider_steps = slider_range / step;
  19477. cursor_offset = (slider_value - slider_min) / step;
  19478. /* calculate cursor
  19479. Basically you have two cursors. One for visual representation and interaction
  19480. and one for updating the actual cursor value. */
  19481. logical_cursor.h = bounds.h;
  19482. logical_cursor.w = bounds.w / slider_steps;
  19483. logical_cursor.x = bounds.x + (logical_cursor.w * cursor_offset);
  19484. logical_cursor.y = bounds.y;
  19485. visual_cursor.h = style->cursor_size.y;
  19486. visual_cursor.w = style->cursor_size.x;
  19487. visual_cursor.y = (bounds.y + bounds.h*0.5f) - visual_cursor.h*0.5f;
  19488. visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
  19489. slider_value = nk_slider_behavior(state, &logical_cursor, &visual_cursor,
  19490. in, bounds, slider_min, slider_max, slider_value, step, slider_steps);
  19491. visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
  19492. /* draw slider */
  19493. if (style->draw_begin) style->draw_begin(out, style->userdata);
  19494. nk_draw_slider(out, *state, style, &bounds, &visual_cursor, slider_min, slider_value, slider_max);
  19495. if (style->draw_end) style->draw_end(out, style->userdata);
  19496. return slider_value;
  19497. }
  19498. NK_API int
  19499. nk_slider_float(struct nk_context *ctx, float min_value, float *value, float max_value,
  19500. float value_step)
  19501. {
  19502. struct nk_window *win;
  19503. struct nk_panel *layout;
  19504. struct nk_input *in;
  19505. const struct nk_style *style;
  19506. int ret = 0;
  19507. float old_value;
  19508. struct nk_rect bounds;
  19509. enum nk_widget_layout_states state;
  19510. NK_ASSERT(ctx);
  19511. NK_ASSERT(ctx->current);
  19512. NK_ASSERT(ctx->current->layout);
  19513. NK_ASSERT(value);
  19514. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  19515. return ret;
  19516. win = ctx->current;
  19517. style = &ctx->style;
  19518. layout = win->layout;
  19519. state = nk_widget(&bounds, ctx);
  19520. if (!state) return ret;
  19521. in = (/*state == NK_WIDGET_ROM || */ layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19522. old_value = *value;
  19523. *value = nk_do_slider(&ctx->last_widget_state, &win->buffer, bounds, min_value,
  19524. old_value, max_value, value_step, &style->slider, in, style->font);
  19525. return (old_value > *value || old_value < *value);
  19526. }
  19527. NK_API float
  19528. nk_slide_float(struct nk_context *ctx, float min, float val, float max, float step)
  19529. {
  19530. nk_slider_float(ctx, min, &val, max, step); return val;
  19531. }
  19532. NK_API int
  19533. nk_slide_int(struct nk_context *ctx, int min, int val, int max, int step)
  19534. {
  19535. float value = (float)val;
  19536. nk_slider_float(ctx, (float)min, &value, (float)max, (float)step);
  19537. return (int)value;
  19538. }
  19539. NK_API int
  19540. nk_slider_int(struct nk_context *ctx, int min, int *val, int max, int step)
  19541. {
  19542. int ret;
  19543. float value = (float)*val;
  19544. ret = nk_slider_float(ctx, (float)min, &value, (float)max, (float)step);
  19545. *val = (int)value;
  19546. return ret;
  19547. }
  19548. /* ===============================================================
  19549. *
  19550. * PROGRESS
  19551. *
  19552. * ===============================================================*/
  19553. NK_LIB nk_size
  19554. nk_progress_behavior(nk_flags *state, struct nk_input *in,
  19555. struct nk_rect r, struct nk_rect cursor, nk_size max, nk_size value, int modifiable)
  19556. {
  19557. int left_mouse_down = 0;
  19558. int left_mouse_click_in_cursor = 0;
  19559. nk_widget_state_reset(state);
  19560. if (!in || !modifiable) return value;
  19561. left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  19562. left_mouse_click_in_cursor = in && nk_input_has_mouse_click_down_in_rect(in,
  19563. NK_BUTTON_LEFT, cursor, nk_true);
  19564. if (nk_input_is_mouse_hovering_rect(in, r))
  19565. *state = NK_WIDGET_STATE_HOVERED;
  19566. if (in && left_mouse_down && left_mouse_click_in_cursor) {
  19567. if (left_mouse_down && left_mouse_click_in_cursor) {
  19568. float ratio = NK_MAX(0, (float)(in->mouse.pos.x - cursor.x)) / (float)cursor.w;
  19569. value = (nk_size)NK_CLAMP(0, (float)max * ratio, (float)max);
  19570. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor.x + cursor.w/2.0f;
  19571. *state |= NK_WIDGET_STATE_ACTIVE;
  19572. }
  19573. }
  19574. /* set progressbar widget state */
  19575. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, r))
  19576. *state |= NK_WIDGET_STATE_ENTERED;
  19577. else if (nk_input_is_mouse_prev_hovering_rect(in, r))
  19578. *state |= NK_WIDGET_STATE_LEFT;
  19579. return value;
  19580. }
  19581. NK_LIB void
  19582. nk_draw_progress(struct nk_command_buffer *out, nk_flags state,
  19583. const struct nk_style_progress *style, const struct nk_rect *bounds,
  19584. const struct nk_rect *scursor, nk_size value, nk_size max)
  19585. {
  19586. const struct nk_style_item *background;
  19587. const struct nk_style_item *cursor;
  19588. NK_UNUSED(max);
  19589. NK_UNUSED(value);
  19590. /* select correct colors/images to draw */
  19591. if (state & NK_WIDGET_STATE_ACTIVED) {
  19592. background = &style->active;
  19593. cursor = &style->cursor_active;
  19594. } else if (state & NK_WIDGET_STATE_HOVER){
  19595. background = &style->hover;
  19596. cursor = &style->cursor_hover;
  19597. } else {
  19598. background = &style->normal;
  19599. cursor = &style->cursor_normal;
  19600. }
  19601. /* draw background */
  19602. if (background->type == NK_STYLE_ITEM_COLOR) {
  19603. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  19604. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  19605. } else nk_draw_image(out, *bounds, &background->data.image, nk_white);
  19606. /* draw cursor */
  19607. if (cursor->type == NK_STYLE_ITEM_COLOR) {
  19608. nk_fill_rect(out, *scursor, style->rounding, cursor->data.color);
  19609. nk_stroke_rect(out, *scursor, style->rounding, style->border, style->border_color);
  19610. } else nk_draw_image(out, *scursor, &cursor->data.image, nk_white);
  19611. }
  19612. NK_LIB nk_size
  19613. nk_do_progress(nk_flags *state,
  19614. struct nk_command_buffer *out, struct nk_rect bounds,
  19615. nk_size value, nk_size max, int modifiable,
  19616. const struct nk_style_progress *style, struct nk_input *in)
  19617. {
  19618. float prog_scale;
  19619. nk_size prog_value;
  19620. struct nk_rect cursor;
  19621. NK_ASSERT(style);
  19622. NK_ASSERT(out);
  19623. if (!out || !style) return 0;
  19624. /* calculate progressbar cursor */
  19625. cursor.w = NK_MAX(bounds.w, 2 * style->padding.x + 2 * style->border);
  19626. cursor.h = NK_MAX(bounds.h, 2 * style->padding.y + 2 * style->border);
  19627. cursor = nk_pad_rect(bounds, nk_vec2(style->padding.x + style->border, style->padding.y + style->border));
  19628. prog_scale = (float)value / (float)max;
  19629. /* update progressbar */
  19630. prog_value = NK_MIN(value, max);
  19631. prog_value = nk_progress_behavior(state, in, bounds, cursor,max, prog_value, modifiable);
  19632. cursor.w = cursor.w * prog_scale;
  19633. /* draw progressbar */
  19634. if (style->draw_begin) style->draw_begin(out, style->userdata);
  19635. nk_draw_progress(out, *state, style, &bounds, &cursor, value, max);
  19636. if (style->draw_end) style->draw_end(out, style->userdata);
  19637. return prog_value;
  19638. }
  19639. NK_API int
  19640. nk_progress(struct nk_context *ctx, nk_size *cur, nk_size max, int is_modifyable)
  19641. {
  19642. struct nk_window *win;
  19643. struct nk_panel *layout;
  19644. const struct nk_style *style;
  19645. struct nk_input *in;
  19646. struct nk_rect bounds;
  19647. enum nk_widget_layout_states state;
  19648. nk_size old_value;
  19649. NK_ASSERT(ctx);
  19650. NK_ASSERT(cur);
  19651. NK_ASSERT(ctx->current);
  19652. NK_ASSERT(ctx->current->layout);
  19653. if (!ctx || !ctx->current || !ctx->current->layout || !cur)
  19654. return 0;
  19655. win = ctx->current;
  19656. style = &ctx->style;
  19657. layout = win->layout;
  19658. state = nk_widget(&bounds, ctx);
  19659. if (!state) return 0;
  19660. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19661. old_value = *cur;
  19662. *cur = nk_do_progress(&ctx->last_widget_state, &win->buffer, bounds,
  19663. *cur, max, is_modifyable, &style->progress, in);
  19664. return (*cur != old_value);
  19665. }
  19666. NK_API nk_size
  19667. nk_prog(struct nk_context *ctx, nk_size cur, nk_size max, int modifyable)
  19668. {
  19669. nk_progress(ctx, &cur, max, modifyable);
  19670. return cur;
  19671. }
  19672. /* ===============================================================
  19673. *
  19674. * SCROLLBAR
  19675. *
  19676. * ===============================================================*/
  19677. NK_LIB float
  19678. nk_scrollbar_behavior(nk_flags *state, struct nk_input *in,
  19679. int has_scrolling, const struct nk_rect *scroll,
  19680. const struct nk_rect *cursor, const struct nk_rect *empty0,
  19681. const struct nk_rect *empty1, float scroll_offset,
  19682. float target, float scroll_step, enum nk_orientation o)
  19683. {
  19684. nk_flags ws = 0;
  19685. int left_mouse_down;
  19686. int left_mouse_click_in_cursor;
  19687. float scroll_delta;
  19688. nk_widget_state_reset(state);
  19689. if (!in) return scroll_offset;
  19690. left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  19691. left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
  19692. NK_BUTTON_LEFT, *cursor, nk_true);
  19693. if (nk_input_is_mouse_hovering_rect(in, *scroll))
  19694. *state = NK_WIDGET_STATE_HOVERED;
  19695. scroll_delta = (o == NK_VERTICAL) ? in->mouse.scroll_delta.y: in->mouse.scroll_delta.x;
  19696. if (left_mouse_down && left_mouse_click_in_cursor) {
  19697. /* update cursor by mouse dragging */
  19698. float pixel, delta;
  19699. *state = NK_WIDGET_STATE_ACTIVE;
  19700. if (o == NK_VERTICAL) {
  19701. float cursor_y;
  19702. pixel = in->mouse.delta.y;
  19703. delta = (pixel / scroll->h) * target;
  19704. scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->h);
  19705. cursor_y = scroll->y + ((scroll_offset/target) * scroll->h);
  19706. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = cursor_y + cursor->h/2.0f;
  19707. } else {
  19708. float cursor_x;
  19709. pixel = in->mouse.delta.x;
  19710. delta = (pixel / scroll->w) * target;
  19711. scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->w);
  19712. cursor_x = scroll->x + ((scroll_offset/target) * scroll->w);
  19713. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor_x + cursor->w/2.0f;
  19714. }
  19715. } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_UP) && o == NK_VERTICAL && has_scrolling)||
  19716. nk_button_behavior(&ws, *empty0, in, NK_BUTTON_DEFAULT)) {
  19717. /* scroll page up by click on empty space or shortcut */
  19718. if (o == NK_VERTICAL)
  19719. scroll_offset = NK_MAX(0, scroll_offset - scroll->h);
  19720. else scroll_offset = NK_MAX(0, scroll_offset - scroll->w);
  19721. } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_DOWN) && o == NK_VERTICAL && has_scrolling) ||
  19722. nk_button_behavior(&ws, *empty1, in, NK_BUTTON_DEFAULT)) {
  19723. /* scroll page down by click on empty space or shortcut */
  19724. if (o == NK_VERTICAL)
  19725. scroll_offset = NK_MIN(scroll_offset + scroll->h, target - scroll->h);
  19726. else scroll_offset = NK_MIN(scroll_offset + scroll->w, target - scroll->w);
  19727. } else if (has_scrolling) {
  19728. if ((scroll_delta < 0 || (scroll_delta > 0))) {
  19729. /* update cursor by mouse scrolling */
  19730. scroll_offset = scroll_offset + scroll_step * (-scroll_delta);
  19731. if (o == NK_VERTICAL)
  19732. scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->h);
  19733. else scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->w);
  19734. } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_START)) {
  19735. /* update cursor to the beginning */
  19736. if (o == NK_VERTICAL) scroll_offset = 0;
  19737. } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_END)) {
  19738. /* update cursor to the end */
  19739. if (o == NK_VERTICAL) scroll_offset = target - scroll->h;
  19740. }
  19741. }
  19742. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *scroll))
  19743. *state |= NK_WIDGET_STATE_ENTERED;
  19744. else if (nk_input_is_mouse_prev_hovering_rect(in, *scroll))
  19745. *state |= NK_WIDGET_STATE_LEFT;
  19746. return scroll_offset;
  19747. }
  19748. NK_LIB void
  19749. nk_draw_scrollbar(struct nk_command_buffer *out, nk_flags state,
  19750. const struct nk_style_scrollbar *style, const struct nk_rect *bounds,
  19751. const struct nk_rect *scroll)
  19752. {
  19753. const struct nk_style_item *background;
  19754. const struct nk_style_item *cursor;
  19755. /* select correct colors/images to draw */
  19756. if (state & NK_WIDGET_STATE_ACTIVED) {
  19757. background = &style->active;
  19758. cursor = &style->cursor_active;
  19759. } else if (state & NK_WIDGET_STATE_HOVER) {
  19760. background = &style->hover;
  19761. cursor = &style->cursor_hover;
  19762. } else {
  19763. background = &style->normal;
  19764. cursor = &style->cursor_normal;
  19765. }
  19766. /* draw background */
  19767. if (background->type == NK_STYLE_ITEM_COLOR) {
  19768. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  19769. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  19770. } else {
  19771. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  19772. }
  19773. /* draw cursor */
  19774. if (cursor->type == NK_STYLE_ITEM_COLOR) {
  19775. nk_fill_rect(out, *scroll, style->rounding_cursor, cursor->data.color);
  19776. nk_stroke_rect(out, *scroll, style->rounding_cursor, style->border_cursor, style->cursor_border_color);
  19777. } else nk_draw_image(out, *scroll, &cursor->data.image, nk_white);
  19778. }
  19779. NK_LIB float
  19780. nk_do_scrollbarv(nk_flags *state,
  19781. struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
  19782. float offset, float target, float step, float button_pixel_inc,
  19783. const struct nk_style_scrollbar *style, struct nk_input *in,
  19784. const struct nk_user_font *font)
  19785. {
  19786. struct nk_rect empty_north;
  19787. struct nk_rect empty_south;
  19788. struct nk_rect cursor;
  19789. float scroll_step;
  19790. float scroll_offset;
  19791. float scroll_off;
  19792. float scroll_ratio;
  19793. NK_ASSERT(out);
  19794. NK_ASSERT(style);
  19795. NK_ASSERT(state);
  19796. if (!out || !style) return 0;
  19797. scroll.w = NK_MAX(scroll.w, 1);
  19798. scroll.h = NK_MAX(scroll.h, 0);
  19799. if (target <= scroll.h) return 0;
  19800. /* optional scrollbar buttons */
  19801. if (style->show_buttons) {
  19802. nk_flags ws;
  19803. float scroll_h;
  19804. struct nk_rect button;
  19805. button.x = scroll.x;
  19806. button.w = scroll.w;
  19807. button.h = scroll.w;
  19808. scroll_h = NK_MAX(scroll.h - 2 * button.h,0);
  19809. scroll_step = NK_MIN(step, button_pixel_inc);
  19810. /* decrement button */
  19811. button.y = scroll.y;
  19812. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
  19813. NK_BUTTON_REPEATER, &style->dec_button, in, font))
  19814. offset = offset - scroll_step;
  19815. /* increment button */
  19816. button.y = scroll.y + scroll.h - button.h;
  19817. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
  19818. NK_BUTTON_REPEATER, &style->inc_button, in, font))
  19819. offset = offset + scroll_step;
  19820. scroll.y = scroll.y + button.h;
  19821. scroll.h = scroll_h;
  19822. }
  19823. /* calculate scrollbar constants */
  19824. scroll_step = NK_MIN(step, scroll.h);
  19825. scroll_offset = NK_CLAMP(0, offset, target - scroll.h);
  19826. scroll_ratio = scroll.h / target;
  19827. scroll_off = scroll_offset / target;
  19828. /* calculate scrollbar cursor bounds */
  19829. cursor.h = NK_MAX((scroll_ratio * scroll.h) - (2*style->border + 2*style->padding.y), 0);
  19830. cursor.y = scroll.y + (scroll_off * scroll.h) + style->border + style->padding.y;
  19831. cursor.w = scroll.w - (2 * style->border + 2 * style->padding.x);
  19832. cursor.x = scroll.x + style->border + style->padding.x;
  19833. /* calculate empty space around cursor */
  19834. empty_north.x = scroll.x;
  19835. empty_north.y = scroll.y;
  19836. empty_north.w = scroll.w;
  19837. empty_north.h = NK_MAX(cursor.y - scroll.y, 0);
  19838. empty_south.x = scroll.x;
  19839. empty_south.y = cursor.y + cursor.h;
  19840. empty_south.w = scroll.w;
  19841. empty_south.h = NK_MAX((scroll.y + scroll.h) - (cursor.y + cursor.h), 0);
  19842. /* update scrollbar */
  19843. scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
  19844. &empty_north, &empty_south, scroll_offset, target, scroll_step, NK_VERTICAL);
  19845. scroll_off = scroll_offset / target;
  19846. cursor.y = scroll.y + (scroll_off * scroll.h) + style->border_cursor + style->padding.y;
  19847. /* draw scrollbar */
  19848. if (style->draw_begin) style->draw_begin(out, style->userdata);
  19849. nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
  19850. if (style->draw_end) style->draw_end(out, style->userdata);
  19851. return scroll_offset;
  19852. }
  19853. NK_LIB float
  19854. nk_do_scrollbarh(nk_flags *state,
  19855. struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
  19856. float offset, float target, float step, float button_pixel_inc,
  19857. const struct nk_style_scrollbar *style, struct nk_input *in,
  19858. const struct nk_user_font *font)
  19859. {
  19860. struct nk_rect cursor;
  19861. struct nk_rect empty_west;
  19862. struct nk_rect empty_east;
  19863. float scroll_step;
  19864. float scroll_offset;
  19865. float scroll_off;
  19866. float scroll_ratio;
  19867. NK_ASSERT(out);
  19868. NK_ASSERT(style);
  19869. if (!out || !style) return 0;
  19870. /* scrollbar background */
  19871. scroll.h = NK_MAX(scroll.h, 1);
  19872. scroll.w = NK_MAX(scroll.w, 2 * scroll.h);
  19873. if (target <= scroll.w) return 0;
  19874. /* optional scrollbar buttons */
  19875. if (style->show_buttons) {
  19876. nk_flags ws;
  19877. float scroll_w;
  19878. struct nk_rect button;
  19879. button.y = scroll.y;
  19880. button.w = scroll.h;
  19881. button.h = scroll.h;
  19882. scroll_w = scroll.w - 2 * button.w;
  19883. scroll_step = NK_MIN(step, button_pixel_inc);
  19884. /* decrement button */
  19885. button.x = scroll.x;
  19886. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
  19887. NK_BUTTON_REPEATER, &style->dec_button, in, font))
  19888. offset = offset - scroll_step;
  19889. /* increment button */
  19890. button.x = scroll.x + scroll.w - button.w;
  19891. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
  19892. NK_BUTTON_REPEATER, &style->inc_button, in, font))
  19893. offset = offset + scroll_step;
  19894. scroll.x = scroll.x + button.w;
  19895. scroll.w = scroll_w;
  19896. }
  19897. /* calculate scrollbar constants */
  19898. scroll_step = NK_MIN(step, scroll.w);
  19899. scroll_offset = NK_CLAMP(0, offset, target - scroll.w);
  19900. scroll_ratio = scroll.w / target;
  19901. scroll_off = scroll_offset / target;
  19902. /* calculate cursor bounds */
  19903. cursor.w = (scroll_ratio * scroll.w) - (2*style->border + 2*style->padding.x);
  19904. cursor.x = scroll.x + (scroll_off * scroll.w) + style->border + style->padding.x;
  19905. cursor.h = scroll.h - (2 * style->border + 2 * style->padding.y);
  19906. cursor.y = scroll.y + style->border + style->padding.y;
  19907. /* calculate empty space around cursor */
  19908. empty_west.x = scroll.x;
  19909. empty_west.y = scroll.y;
  19910. empty_west.w = cursor.x - scroll.x;
  19911. empty_west.h = scroll.h;
  19912. empty_east.x = cursor.x + cursor.w;
  19913. empty_east.y = scroll.y;
  19914. empty_east.w = (scroll.x + scroll.w) - (cursor.x + cursor.w);
  19915. empty_east.h = scroll.h;
  19916. /* update scrollbar */
  19917. scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
  19918. &empty_west, &empty_east, scroll_offset, target, scroll_step, NK_HORIZONTAL);
  19919. scroll_off = scroll_offset / target;
  19920. cursor.x = scroll.x + (scroll_off * scroll.w);
  19921. /* draw scrollbar */
  19922. if (style->draw_begin) style->draw_begin(out, style->userdata);
  19923. nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
  19924. if (style->draw_end) style->draw_end(out, style->userdata);
  19925. return scroll_offset;
  19926. }
  19927. /* ===============================================================
  19928. *
  19929. * TEXT EDITOR
  19930. *
  19931. * ===============================================================*/
  19932. /* stb_textedit.h - v1.8 - public domain - Sean Barrett */
  19933. struct nk_text_find {
  19934. float x,y; /* position of n'th character */
  19935. float height; /* height of line */
  19936. int first_char, length; /* first char of row, and length */
  19937. int prev_first; /*_ first char of previous row */
  19938. };
  19939. struct nk_text_edit_row {
  19940. float x0,x1;
  19941. /* starting x location, end x location (allows for align=right, etc) */
  19942. float baseline_y_delta;
  19943. /* position of baseline relative to previous row's baseline*/
  19944. float ymin,ymax;
  19945. /* height of row above and below baseline */
  19946. int num_chars;
  19947. };
  19948. /* forward declarations */
  19949. NK_INTERN void nk_textedit_makeundo_delete(struct nk_text_edit*, int, int);
  19950. NK_INTERN void nk_textedit_makeundo_insert(struct nk_text_edit*, int, int);
  19951. NK_INTERN void nk_textedit_makeundo_replace(struct nk_text_edit*, int, int, int);
  19952. #define NK_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end)
  19953. NK_INTERN float
  19954. nk_textedit_get_width(const struct nk_text_edit *edit, int line_start, int char_id,
  19955. const struct nk_user_font *font)
  19956. {
  19957. int len = 0;
  19958. nk_rune unicode = 0;
  19959. const char *str = nk_str_at_const(&edit->string, line_start + char_id, &unicode, &len);
  19960. return font->width(font->userdata, font->height, str, len);
  19961. }
  19962. NK_INTERN void
  19963. nk_textedit_layout_row(struct nk_text_edit_row *r, struct nk_text_edit *edit,
  19964. int line_start_id, float row_height, const struct nk_user_font *font)
  19965. {
  19966. int l;
  19967. int glyphs = 0;
  19968. nk_rune unicode;
  19969. const char *remaining;
  19970. int len = nk_str_len_char(&edit->string);
  19971. const char *end = nk_str_get_const(&edit->string) + len;
  19972. const char *text = nk_str_at_const(&edit->string, line_start_id, &unicode, &l);
  19973. const struct nk_vec2 size = nk_text_calculate_text_bounds(font,
  19974. text, (int)(end - text), row_height, &remaining, 0, &glyphs, NK_STOP_ON_NEW_LINE);
  19975. r->x0 = 0.0f;
  19976. r->x1 = size.x;
  19977. r->baseline_y_delta = size.y;
  19978. r->ymin = 0.0f;
  19979. r->ymax = size.y;
  19980. r->num_chars = glyphs;
  19981. }
  19982. NK_INTERN int
  19983. nk_textedit_locate_coord(struct nk_text_edit *edit, float x, float y,
  19984. const struct nk_user_font *font, float row_height)
  19985. {
  19986. struct nk_text_edit_row r;
  19987. int n = edit->string.len;
  19988. float base_y = 0, prev_x;
  19989. int i=0, k;
  19990. r.x0 = r.x1 = 0;
  19991. r.ymin = r.ymax = 0;
  19992. r.num_chars = 0;
  19993. /* search rows to find one that straddles 'y' */
  19994. while (i < n) {
  19995. nk_textedit_layout_row(&r, edit, i, row_height, font);
  19996. if (r.num_chars <= 0)
  19997. return n;
  19998. if (i==0 && y < base_y + r.ymin)
  19999. return 0;
  20000. if (y < base_y + r.ymax)
  20001. break;
  20002. i += r.num_chars;
  20003. base_y += r.baseline_y_delta;
  20004. }
  20005. /* below all text, return 'after' last character */
  20006. if (i >= n)
  20007. return n;
  20008. /* check if it's before the beginning of the line */
  20009. if (x < r.x0)
  20010. return i;
  20011. /* check if it's before the end of the line */
  20012. if (x < r.x1) {
  20013. /* search characters in row for one that straddles 'x' */
  20014. k = i;
  20015. prev_x = r.x0;
  20016. for (i=0; i < r.num_chars; ++i) {
  20017. float w = nk_textedit_get_width(edit, k, i, font);
  20018. if (x < prev_x+w) {
  20019. if (x < prev_x+w/2)
  20020. return k+i;
  20021. else return k+i+1;
  20022. }
  20023. prev_x += w;
  20024. }
  20025. /* shouldn't happen, but if it does, fall through to end-of-line case */
  20026. }
  20027. /* if the last character is a newline, return that.
  20028. * otherwise return 'after' the last character */
  20029. if (nk_str_rune_at(&edit->string, i+r.num_chars-1) == '\n')
  20030. return i+r.num_chars-1;
  20031. else return i+r.num_chars;
  20032. }
  20033. NK_LIB void
  20034. nk_textedit_click(struct nk_text_edit *state, float x, float y,
  20035. const struct nk_user_font *font, float row_height)
  20036. {
  20037. /* API click: on mouse down, move the cursor to the clicked location,
  20038. * and reset the selection */
  20039. state->cursor = nk_textedit_locate_coord(state, x, y, font, row_height);
  20040. state->select_start = state->cursor;
  20041. state->select_end = state->cursor;
  20042. state->has_preferred_x = 0;
  20043. }
  20044. NK_LIB void
  20045. nk_textedit_drag(struct nk_text_edit *state, float x, float y,
  20046. const struct nk_user_font *font, float row_height)
  20047. {
  20048. /* API drag: on mouse drag, move the cursor and selection endpoint
  20049. * to the clicked location */
  20050. int p = nk_textedit_locate_coord(state, x, y, font, row_height);
  20051. if (state->select_start == state->select_end)
  20052. state->select_start = state->cursor;
  20053. state->cursor = state->select_end = p;
  20054. }
  20055. NK_INTERN void
  20056. nk_textedit_find_charpos(struct nk_text_find *find, struct nk_text_edit *state,
  20057. int n, int single_line, const struct nk_user_font *font, float row_height)
  20058. {
  20059. /* find the x/y location of a character, and remember info about the previous
  20060. * row in case we get a move-up event (for page up, we'll have to rescan) */
  20061. struct nk_text_edit_row r;
  20062. int prev_start = 0;
  20063. int z = state->string.len;
  20064. int i=0, first;
  20065. nk_zero_struct(r);
  20066. if (n == z) {
  20067. /* if it's at the end, then find the last line -- simpler than trying to
  20068. explicitly handle this case in the regular code */
  20069. nk_textedit_layout_row(&r, state, 0, row_height, font);
  20070. if (single_line) {
  20071. find->first_char = 0;
  20072. find->length = z;
  20073. } else {
  20074. while (i < z) {
  20075. prev_start = i;
  20076. i += r.num_chars;
  20077. nk_textedit_layout_row(&r, state, i, row_height, font);
  20078. }
  20079. find->first_char = i;
  20080. find->length = r.num_chars;
  20081. }
  20082. find->x = r.x1;
  20083. find->y = r.ymin;
  20084. find->height = r.ymax - r.ymin;
  20085. find->prev_first = prev_start;
  20086. return;
  20087. }
  20088. /* search rows to find the one that straddles character n */
  20089. find->y = 0;
  20090. for(;;) {
  20091. nk_textedit_layout_row(&r, state, i, row_height, font);
  20092. if (n < i + r.num_chars) break;
  20093. prev_start = i;
  20094. i += r.num_chars;
  20095. find->y += r.baseline_y_delta;
  20096. }
  20097. find->first_char = first = i;
  20098. find->length = r.num_chars;
  20099. find->height = r.ymax - r.ymin;
  20100. find->prev_first = prev_start;
  20101. /* now scan to find xpos */
  20102. find->x = r.x0;
  20103. for (i=0; first+i < n; ++i)
  20104. find->x += nk_textedit_get_width(state, first, i, font);
  20105. }
  20106. NK_INTERN void
  20107. nk_textedit_clamp(struct nk_text_edit *state)
  20108. {
  20109. /* make the selection/cursor state valid if client altered the string */
  20110. int n = state->string.len;
  20111. if (NK_TEXT_HAS_SELECTION(state)) {
  20112. if (state->select_start > n) state->select_start = n;
  20113. if (state->select_end > n) state->select_end = n;
  20114. /* if clamping forced them to be equal, move the cursor to match */
  20115. if (state->select_start == state->select_end)
  20116. state->cursor = state->select_start;
  20117. }
  20118. if (state->cursor > n) state->cursor = n;
  20119. }
  20120. NK_API void
  20121. nk_textedit_delete(struct nk_text_edit *state, int where, int len)
  20122. {
  20123. /* delete characters while updating undo */
  20124. nk_textedit_makeundo_delete(state, where, len);
  20125. nk_str_delete_runes(&state->string, where, len);
  20126. state->has_preferred_x = 0;
  20127. }
  20128. NK_API void
  20129. nk_textedit_delete_selection(struct nk_text_edit *state)
  20130. {
  20131. /* delete the section */
  20132. nk_textedit_clamp(state);
  20133. if (NK_TEXT_HAS_SELECTION(state)) {
  20134. if (state->select_start < state->select_end) {
  20135. nk_textedit_delete(state, state->select_start,
  20136. state->select_end - state->select_start);
  20137. state->select_end = state->cursor = state->select_start;
  20138. } else {
  20139. nk_textedit_delete(state, state->select_end,
  20140. state->select_start - state->select_end);
  20141. state->select_start = state->cursor = state->select_end;
  20142. }
  20143. state->has_preferred_x = 0;
  20144. }
  20145. }
  20146. NK_INTERN void
  20147. nk_textedit_sortselection(struct nk_text_edit *state)
  20148. {
  20149. /* canonicalize the selection so start <= end */
  20150. if (state->select_end < state->select_start) {
  20151. int temp = state->select_end;
  20152. state->select_end = state->select_start;
  20153. state->select_start = temp;
  20154. }
  20155. }
  20156. NK_INTERN void
  20157. nk_textedit_move_to_first(struct nk_text_edit *state)
  20158. {
  20159. /* move cursor to first character of selection */
  20160. if (NK_TEXT_HAS_SELECTION(state)) {
  20161. nk_textedit_sortselection(state);
  20162. state->cursor = state->select_start;
  20163. state->select_end = state->select_start;
  20164. state->has_preferred_x = 0;
  20165. }
  20166. }
  20167. NK_INTERN void
  20168. nk_textedit_move_to_last(struct nk_text_edit *state)
  20169. {
  20170. /* move cursor to last character of selection */
  20171. if (NK_TEXT_HAS_SELECTION(state)) {
  20172. nk_textedit_sortselection(state);
  20173. nk_textedit_clamp(state);
  20174. state->cursor = state->select_end;
  20175. state->select_start = state->select_end;
  20176. state->has_preferred_x = 0;
  20177. }
  20178. }
  20179. NK_INTERN int
  20180. nk_is_word_boundary( struct nk_text_edit *state, int idx)
  20181. {
  20182. int len;
  20183. nk_rune c;
  20184. if (idx <= 0) return 1;
  20185. if (!nk_str_at_rune(&state->string, idx, &c, &len)) return 1;
  20186. return (c == ' ' || c == '\t' ||c == 0x3000 || c == ',' || c == ';' ||
  20187. c == '(' || c == ')' || c == '{' || c == '}' || c == '[' || c == ']' ||
  20188. c == '|');
  20189. }
  20190. NK_INTERN int
  20191. nk_textedit_move_to_word_previous(struct nk_text_edit *state)
  20192. {
  20193. int c = state->cursor - 1;
  20194. while( c >= 0 && !nk_is_word_boundary(state, c))
  20195. --c;
  20196. if( c < 0 )
  20197. c = 0;
  20198. return c;
  20199. }
  20200. NK_INTERN int
  20201. nk_textedit_move_to_word_next(struct nk_text_edit *state)
  20202. {
  20203. const int len = state->string.len;
  20204. int c = state->cursor+1;
  20205. while( c < len && !nk_is_word_boundary(state, c))
  20206. ++c;
  20207. if( c > len )
  20208. c = len;
  20209. return c;
  20210. }
  20211. NK_INTERN void
  20212. nk_textedit_prep_selection_at_cursor(struct nk_text_edit *state)
  20213. {
  20214. /* update selection and cursor to match each other */
  20215. if (!NK_TEXT_HAS_SELECTION(state))
  20216. state->select_start = state->select_end = state->cursor;
  20217. else state->cursor = state->select_end;
  20218. }
  20219. NK_API int
  20220. nk_textedit_cut(struct nk_text_edit *state)
  20221. {
  20222. /* API cut: delete selection */
  20223. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  20224. return 0;
  20225. if (NK_TEXT_HAS_SELECTION(state)) {
  20226. nk_textedit_delete_selection(state); /* implicitly clamps */
  20227. state->has_preferred_x = 0;
  20228. return 1;
  20229. }
  20230. return 0;
  20231. }
  20232. NK_API int
  20233. nk_textedit_paste(struct nk_text_edit *state, char const *ctext, int len)
  20234. {
  20235. /* API paste: replace existing selection with passed-in text */
  20236. int glyphs;
  20237. const char *text = (const char *) ctext;
  20238. if (state->mode == NK_TEXT_EDIT_MODE_VIEW) return 0;
  20239. /* if there's a selection, the paste should delete it */
  20240. nk_textedit_clamp(state);
  20241. nk_textedit_delete_selection(state);
  20242. /* try to insert the characters */
  20243. glyphs = nk_utf_len(ctext, len);
  20244. if (nk_str_insert_text_char(&state->string, state->cursor, text, len)) {
  20245. nk_textedit_makeundo_insert(state, state->cursor, glyphs);
  20246. state->cursor += len;
  20247. state->has_preferred_x = 0;
  20248. return 1;
  20249. }
  20250. /* remove the undo since we didn't actually insert the characters */
  20251. if (state->undo.undo_point)
  20252. --state->undo.undo_point;
  20253. return 0;
  20254. }
  20255. NK_API void
  20256. nk_textedit_text(struct nk_text_edit *state, const char *text, int total_len)
  20257. {
  20258. nk_rune unicode;
  20259. int glyph_len;
  20260. int text_len = 0;
  20261. NK_ASSERT(state);
  20262. NK_ASSERT(text);
  20263. if (!text || !total_len || state->mode == NK_TEXT_EDIT_MODE_VIEW) return;
  20264. glyph_len = nk_utf_decode(text, &unicode, total_len);
  20265. while ((text_len < total_len) && glyph_len)
  20266. {
  20267. /* don't insert a backward delete, just process the event */
  20268. if (unicode == 127) goto next;
  20269. /* can't add newline in single-line mode */
  20270. if (unicode == '\n' && state->single_line) goto next;
  20271. /* filter incoming text */
  20272. if (state->filter && !state->filter(state, unicode)) goto next;
  20273. if (!NK_TEXT_HAS_SELECTION(state) &&
  20274. state->cursor < state->string.len)
  20275. {
  20276. if (state->mode == NK_TEXT_EDIT_MODE_REPLACE) {
  20277. nk_textedit_makeundo_replace(state, state->cursor, 1, 1);
  20278. nk_str_delete_runes(&state->string, state->cursor, 1);
  20279. }
  20280. if (nk_str_insert_text_utf8(&state->string, state->cursor,
  20281. text+text_len, 1))
  20282. {
  20283. ++state->cursor;
  20284. state->has_preferred_x = 0;
  20285. }
  20286. } else {
  20287. nk_textedit_delete_selection(state); /* implicitly clamps */
  20288. if (nk_str_insert_text_utf8(&state->string, state->cursor,
  20289. text+text_len, 1))
  20290. {
  20291. nk_textedit_makeundo_insert(state, state->cursor, 1);
  20292. ++state->cursor;
  20293. state->has_preferred_x = 0;
  20294. }
  20295. }
  20296. next:
  20297. text_len += glyph_len;
  20298. glyph_len = nk_utf_decode(text + text_len, &unicode, total_len-text_len);
  20299. }
  20300. }
  20301. NK_LIB void
  20302. nk_textedit_key(struct nk_text_edit *state, enum nk_keys key, int shift_mod,
  20303. const struct nk_user_font *font, float row_height)
  20304. {
  20305. retry:
  20306. switch (key)
  20307. {
  20308. case NK_KEY_NONE:
  20309. case NK_KEY_CTRL:
  20310. case NK_KEY_ENTER:
  20311. case NK_KEY_SHIFT:
  20312. case NK_KEY_TAB:
  20313. case NK_KEY_COPY:
  20314. case NK_KEY_CUT:
  20315. case NK_KEY_PASTE:
  20316. case NK_KEY_MAX:
  20317. default: break;
  20318. case NK_KEY_TEXT_UNDO:
  20319. nk_textedit_undo(state);
  20320. state->has_preferred_x = 0;
  20321. break;
  20322. case NK_KEY_TEXT_REDO:
  20323. nk_textedit_redo(state);
  20324. state->has_preferred_x = 0;
  20325. break;
  20326. case NK_KEY_TEXT_SELECT_ALL:
  20327. nk_textedit_select_all(state);
  20328. state->has_preferred_x = 0;
  20329. break;
  20330. case NK_KEY_TEXT_INSERT_MODE:
  20331. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  20332. state->mode = NK_TEXT_EDIT_MODE_INSERT;
  20333. break;
  20334. case NK_KEY_TEXT_REPLACE_MODE:
  20335. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  20336. state->mode = NK_TEXT_EDIT_MODE_REPLACE;
  20337. break;
  20338. case NK_KEY_TEXT_RESET_MODE:
  20339. if (state->mode == NK_TEXT_EDIT_MODE_INSERT ||
  20340. state->mode == NK_TEXT_EDIT_MODE_REPLACE)
  20341. state->mode = NK_TEXT_EDIT_MODE_VIEW;
  20342. break;
  20343. case NK_KEY_LEFT:
  20344. if (shift_mod) {
  20345. nk_textedit_clamp(state);
  20346. nk_textedit_prep_selection_at_cursor(state);
  20347. /* move selection left */
  20348. if (state->select_end > 0)
  20349. --state->select_end;
  20350. state->cursor = state->select_end;
  20351. state->has_preferred_x = 0;
  20352. } else {
  20353. /* if currently there's a selection,
  20354. * move cursor to start of selection */
  20355. if (NK_TEXT_HAS_SELECTION(state))
  20356. nk_textedit_move_to_first(state);
  20357. else if (state->cursor > 0)
  20358. --state->cursor;
  20359. state->has_preferred_x = 0;
  20360. } break;
  20361. case NK_KEY_RIGHT:
  20362. if (shift_mod) {
  20363. nk_textedit_prep_selection_at_cursor(state);
  20364. /* move selection right */
  20365. ++state->select_end;
  20366. nk_textedit_clamp(state);
  20367. state->cursor = state->select_end;
  20368. state->has_preferred_x = 0;
  20369. } else {
  20370. /* if currently there's a selection,
  20371. * move cursor to end of selection */
  20372. if (NK_TEXT_HAS_SELECTION(state))
  20373. nk_textedit_move_to_last(state);
  20374. else ++state->cursor;
  20375. nk_textedit_clamp(state);
  20376. state->has_preferred_x = 0;
  20377. } break;
  20378. case NK_KEY_TEXT_WORD_LEFT:
  20379. if (shift_mod) {
  20380. if( !NK_TEXT_HAS_SELECTION( state ) )
  20381. nk_textedit_prep_selection_at_cursor(state);
  20382. state->cursor = nk_textedit_move_to_word_previous(state);
  20383. state->select_end = state->cursor;
  20384. nk_textedit_clamp(state );
  20385. } else {
  20386. if (NK_TEXT_HAS_SELECTION(state))
  20387. nk_textedit_move_to_first(state);
  20388. else {
  20389. state->cursor = nk_textedit_move_to_word_previous(state);
  20390. nk_textedit_clamp(state );
  20391. }
  20392. } break;
  20393. case NK_KEY_TEXT_WORD_RIGHT:
  20394. if (shift_mod) {
  20395. if( !NK_TEXT_HAS_SELECTION( state ) )
  20396. nk_textedit_prep_selection_at_cursor(state);
  20397. state->cursor = nk_textedit_move_to_word_next(state);
  20398. state->select_end = state->cursor;
  20399. nk_textedit_clamp(state);
  20400. } else {
  20401. if (NK_TEXT_HAS_SELECTION(state))
  20402. nk_textedit_move_to_last(state);
  20403. else {
  20404. state->cursor = nk_textedit_move_to_word_next(state);
  20405. nk_textedit_clamp(state );
  20406. }
  20407. } break;
  20408. case NK_KEY_DOWN: {
  20409. struct nk_text_find find;
  20410. struct nk_text_edit_row row;
  20411. int i, sel = shift_mod;
  20412. if (state->single_line) {
  20413. /* on windows, up&down in single-line behave like left&right */
  20414. key = NK_KEY_RIGHT;
  20415. goto retry;
  20416. }
  20417. if (sel)
  20418. nk_textedit_prep_selection_at_cursor(state);
  20419. else if (NK_TEXT_HAS_SELECTION(state))
  20420. nk_textedit_move_to_last(state);
  20421. /* compute current position of cursor point */
  20422. nk_textedit_clamp(state);
  20423. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  20424. font, row_height);
  20425. /* now find character position down a row */
  20426. if (find.length)
  20427. {
  20428. float x;
  20429. float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
  20430. int start = find.first_char + find.length;
  20431. state->cursor = start;
  20432. nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
  20433. x = row.x0;
  20434. for (i=0; i < row.num_chars && x < row.x1; ++i) {
  20435. float dx = nk_textedit_get_width(state, start, i, font);
  20436. x += dx;
  20437. if (x > goal_x)
  20438. break;
  20439. ++state->cursor;
  20440. }
  20441. nk_textedit_clamp(state);
  20442. state->has_preferred_x = 1;
  20443. state->preferred_x = goal_x;
  20444. if (sel)
  20445. state->select_end = state->cursor;
  20446. }
  20447. } break;
  20448. case NK_KEY_UP: {
  20449. struct nk_text_find find;
  20450. struct nk_text_edit_row row;
  20451. int i, sel = shift_mod;
  20452. if (state->single_line) {
  20453. /* on windows, up&down become left&right */
  20454. key = NK_KEY_LEFT;
  20455. goto retry;
  20456. }
  20457. if (sel)
  20458. nk_textedit_prep_selection_at_cursor(state);
  20459. else if (NK_TEXT_HAS_SELECTION(state))
  20460. nk_textedit_move_to_first(state);
  20461. /* compute current position of cursor point */
  20462. nk_textedit_clamp(state);
  20463. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  20464. font, row_height);
  20465. /* can only go up if there's a previous row */
  20466. if (find.prev_first != find.first_char) {
  20467. /* now find character position up a row */
  20468. float x;
  20469. float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
  20470. state->cursor = find.prev_first;
  20471. nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
  20472. x = row.x0;
  20473. for (i=0; i < row.num_chars && x < row.x1; ++i) {
  20474. float dx = nk_textedit_get_width(state, find.prev_first, i, font);
  20475. x += dx;
  20476. if (x > goal_x)
  20477. break;
  20478. ++state->cursor;
  20479. }
  20480. nk_textedit_clamp(state);
  20481. state->has_preferred_x = 1;
  20482. state->preferred_x = goal_x;
  20483. if (sel) state->select_end = state->cursor;
  20484. }
  20485. } break;
  20486. case NK_KEY_DEL:
  20487. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  20488. break;
  20489. if (NK_TEXT_HAS_SELECTION(state))
  20490. nk_textedit_delete_selection(state);
  20491. else {
  20492. int n = state->string.len;
  20493. if (state->cursor < n)
  20494. nk_textedit_delete(state, state->cursor, 1);
  20495. }
  20496. state->has_preferred_x = 0;
  20497. break;
  20498. case NK_KEY_BACKSPACE:
  20499. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  20500. break;
  20501. if (NK_TEXT_HAS_SELECTION(state))
  20502. nk_textedit_delete_selection(state);
  20503. else {
  20504. nk_textedit_clamp(state);
  20505. if (state->cursor > 0) {
  20506. nk_textedit_delete(state, state->cursor-1, 1);
  20507. --state->cursor;
  20508. }
  20509. }
  20510. state->has_preferred_x = 0;
  20511. break;
  20512. case NK_KEY_TEXT_START:
  20513. if (shift_mod) {
  20514. nk_textedit_prep_selection_at_cursor(state);
  20515. state->cursor = state->select_end = 0;
  20516. state->has_preferred_x = 0;
  20517. } else {
  20518. state->cursor = state->select_start = state->select_end = 0;
  20519. state->has_preferred_x = 0;
  20520. }
  20521. break;
  20522. case NK_KEY_TEXT_END:
  20523. if (shift_mod) {
  20524. nk_textedit_prep_selection_at_cursor(state);
  20525. state->cursor = state->select_end = state->string.len;
  20526. state->has_preferred_x = 0;
  20527. } else {
  20528. state->cursor = state->string.len;
  20529. state->select_start = state->select_end = 0;
  20530. state->has_preferred_x = 0;
  20531. }
  20532. break;
  20533. case NK_KEY_TEXT_LINE_START: {
  20534. if (shift_mod) {
  20535. struct nk_text_find find;
  20536. nk_textedit_clamp(state);
  20537. nk_textedit_prep_selection_at_cursor(state);
  20538. if (state->string.len && state->cursor == state->string.len)
  20539. --state->cursor;
  20540. nk_textedit_find_charpos(&find, state,state->cursor, state->single_line,
  20541. font, row_height);
  20542. state->cursor = state->select_end = find.first_char;
  20543. state->has_preferred_x = 0;
  20544. } else {
  20545. struct nk_text_find find;
  20546. if (state->string.len && state->cursor == state->string.len)
  20547. --state->cursor;
  20548. nk_textedit_clamp(state);
  20549. nk_textedit_move_to_first(state);
  20550. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  20551. font, row_height);
  20552. state->cursor = find.first_char;
  20553. state->has_preferred_x = 0;
  20554. }
  20555. } break;
  20556. case NK_KEY_TEXT_LINE_END: {
  20557. if (shift_mod) {
  20558. struct nk_text_find find;
  20559. nk_textedit_clamp(state);
  20560. nk_textedit_prep_selection_at_cursor(state);
  20561. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  20562. font, row_height);
  20563. state->has_preferred_x = 0;
  20564. state->cursor = find.first_char + find.length;
  20565. if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor-1) == '\n')
  20566. --state->cursor;
  20567. state->select_end = state->cursor;
  20568. } else {
  20569. struct nk_text_find find;
  20570. nk_textedit_clamp(state);
  20571. nk_textedit_move_to_first(state);
  20572. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  20573. font, row_height);
  20574. state->has_preferred_x = 0;
  20575. state->cursor = find.first_char + find.length;
  20576. if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor-1) == '\n')
  20577. --state->cursor;
  20578. }} break;
  20579. }
  20580. }
  20581. NK_INTERN void
  20582. nk_textedit_flush_redo(struct nk_text_undo_state *state)
  20583. {
  20584. state->redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
  20585. state->redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
  20586. }
  20587. NK_INTERN void
  20588. nk_textedit_discard_undo(struct nk_text_undo_state *state)
  20589. {
  20590. /* discard the oldest entry in the undo list */
  20591. if (state->undo_point > 0) {
  20592. /* if the 0th undo state has characters, clean those up */
  20593. if (state->undo_rec[0].char_storage >= 0) {
  20594. int n = state->undo_rec[0].insert_length, i;
  20595. /* delete n characters from all other records */
  20596. state->undo_char_point = (short)(state->undo_char_point - n);
  20597. NK_MEMCPY(state->undo_char, state->undo_char + n,
  20598. (nk_size)state->undo_char_point*sizeof(nk_rune));
  20599. for (i=0; i < state->undo_point; ++i) {
  20600. if (state->undo_rec[i].char_storage >= 0)
  20601. state->undo_rec[i].char_storage = (short)
  20602. (state->undo_rec[i].char_storage - n);
  20603. }
  20604. }
  20605. --state->undo_point;
  20606. NK_MEMCPY(state->undo_rec, state->undo_rec+1,
  20607. (nk_size)((nk_size)state->undo_point * sizeof(state->undo_rec[0])));
  20608. }
  20609. }
  20610. NK_INTERN void
  20611. nk_textedit_discard_redo(struct nk_text_undo_state *state)
  20612. {
  20613. /* discard the oldest entry in the redo list--it's bad if this
  20614. ever happens, but because undo & redo have to store the actual
  20615. characters in different cases, the redo character buffer can
  20616. fill up even though the undo buffer didn't */
  20617. nk_size num;
  20618. int k = NK_TEXTEDIT_UNDOSTATECOUNT-1;
  20619. if (state->redo_point <= k) {
  20620. /* if the k'th undo state has characters, clean those up */
  20621. if (state->undo_rec[k].char_storage >= 0) {
  20622. int n = state->undo_rec[k].insert_length, i;
  20623. /* delete n characters from all other records */
  20624. state->redo_char_point = (short)(state->redo_char_point + n);
  20625. num = (nk_size)(NK_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point);
  20626. NK_MEMCPY(state->undo_char + state->redo_char_point,
  20627. state->undo_char + state->redo_char_point-n, num * sizeof(char));
  20628. for (i = state->redo_point; i < k; ++i) {
  20629. if (state->undo_rec[i].char_storage >= 0) {
  20630. state->undo_rec[i].char_storage = (short)
  20631. (state->undo_rec[i].char_storage + n);
  20632. }
  20633. }
  20634. }
  20635. ++state->redo_point;
  20636. num = (nk_size)(NK_TEXTEDIT_UNDOSTATECOUNT - state->redo_point);
  20637. if (num) NK_MEMCPY(state->undo_rec + state->redo_point-1,
  20638. state->undo_rec + state->redo_point, num * sizeof(state->undo_rec[0]));
  20639. }
  20640. }
  20641. NK_INTERN struct nk_text_undo_record*
  20642. nk_textedit_create_undo_record(struct nk_text_undo_state *state, int numchars)
  20643. {
  20644. /* any time we create a new undo record, we discard redo*/
  20645. nk_textedit_flush_redo(state);
  20646. /* if we have no free records, we have to make room,
  20647. * by sliding the existing records down */
  20648. if (state->undo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  20649. nk_textedit_discard_undo(state);
  20650. /* if the characters to store won't possibly fit in the buffer,
  20651. * we can't undo */
  20652. if (numchars > NK_TEXTEDIT_UNDOCHARCOUNT) {
  20653. state->undo_point = 0;
  20654. state->undo_char_point = 0;
  20655. return 0;
  20656. }
  20657. /* if we don't have enough free characters in the buffer,
  20658. * we have to make room */
  20659. while (state->undo_char_point + numchars > NK_TEXTEDIT_UNDOCHARCOUNT)
  20660. nk_textedit_discard_undo(state);
  20661. return &state->undo_rec[state->undo_point++];
  20662. }
  20663. NK_INTERN nk_rune*
  20664. nk_textedit_createundo(struct nk_text_undo_state *state, int pos,
  20665. int insert_len, int delete_len)
  20666. {
  20667. struct nk_text_undo_record *r = nk_textedit_create_undo_record(state, insert_len);
  20668. if (r == 0)
  20669. return 0;
  20670. r->where = pos;
  20671. r->insert_length = (short) insert_len;
  20672. r->delete_length = (short) delete_len;
  20673. if (insert_len == 0) {
  20674. r->char_storage = -1;
  20675. return 0;
  20676. } else {
  20677. r->char_storage = state->undo_char_point;
  20678. state->undo_char_point = (short)(state->undo_char_point + insert_len);
  20679. return &state->undo_char[r->char_storage];
  20680. }
  20681. }
  20682. NK_API void
  20683. nk_textedit_undo(struct nk_text_edit *state)
  20684. {
  20685. struct nk_text_undo_state *s = &state->undo;
  20686. struct nk_text_undo_record u, *r;
  20687. if (s->undo_point == 0)
  20688. return;
  20689. /* we need to do two things: apply the undo record, and create a redo record */
  20690. u = s->undo_rec[s->undo_point-1];
  20691. r = &s->undo_rec[s->redo_point-1];
  20692. r->char_storage = -1;
  20693. r->insert_length = u.delete_length;
  20694. r->delete_length = u.insert_length;
  20695. r->where = u.where;
  20696. if (u.delete_length)
  20697. {
  20698. /* if the undo record says to delete characters, then the redo record will
  20699. need to re-insert the characters that get deleted, so we need to store
  20700. them.
  20701. there are three cases:
  20702. - there's enough room to store the characters
  20703. - characters stored for *redoing* don't leave room for redo
  20704. - characters stored for *undoing* don't leave room for redo
  20705. if the last is true, we have to bail */
  20706. if (s->undo_char_point + u.delete_length >= NK_TEXTEDIT_UNDOCHARCOUNT) {
  20707. /* the undo records take up too much character space; there's no space
  20708. * to store the redo characters */
  20709. r->insert_length = 0;
  20710. } else {
  20711. int i;
  20712. /* there's definitely room to store the characters eventually */
  20713. while (s->undo_char_point + u.delete_length > s->redo_char_point) {
  20714. /* there's currently not enough room, so discard a redo record */
  20715. nk_textedit_discard_redo(s);
  20716. /* should never happen: */
  20717. if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  20718. return;
  20719. }
  20720. r = &s->undo_rec[s->redo_point-1];
  20721. r->char_storage = (short)(s->redo_char_point - u.delete_length);
  20722. s->redo_char_point = (short)(s->redo_char_point - u.delete_length);
  20723. /* now save the characters */
  20724. for (i=0; i < u.delete_length; ++i)
  20725. s->undo_char[r->char_storage + i] =
  20726. nk_str_rune_at(&state->string, u.where + i);
  20727. }
  20728. /* now we can carry out the deletion */
  20729. nk_str_delete_runes(&state->string, u.where, u.delete_length);
  20730. }
  20731. /* check type of recorded action: */
  20732. if (u.insert_length) {
  20733. /* easy case: was a deletion, so we need to insert n characters */
  20734. nk_str_insert_text_runes(&state->string, u.where,
  20735. &s->undo_char[u.char_storage], u.insert_length);
  20736. s->undo_char_point = (short)(s->undo_char_point - u.insert_length);
  20737. }
  20738. state->cursor = (short)(u.where + u.insert_length);
  20739. s->undo_point--;
  20740. s->redo_point--;
  20741. }
  20742. NK_API void
  20743. nk_textedit_redo(struct nk_text_edit *state)
  20744. {
  20745. struct nk_text_undo_state *s = &state->undo;
  20746. struct nk_text_undo_record *u, r;
  20747. if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  20748. return;
  20749. /* we need to do two things: apply the redo record, and create an undo record */
  20750. u = &s->undo_rec[s->undo_point];
  20751. r = s->undo_rec[s->redo_point];
  20752. /* we KNOW there must be room for the undo record, because the redo record
  20753. was derived from an undo record */
  20754. u->delete_length = r.insert_length;
  20755. u->insert_length = r.delete_length;
  20756. u->where = r.where;
  20757. u->char_storage = -1;
  20758. if (r.delete_length) {
  20759. /* the redo record requires us to delete characters, so the undo record
  20760. needs to store the characters */
  20761. if (s->undo_char_point + u->insert_length > s->redo_char_point) {
  20762. u->insert_length = 0;
  20763. u->delete_length = 0;
  20764. } else {
  20765. int i;
  20766. u->char_storage = s->undo_char_point;
  20767. s->undo_char_point = (short)(s->undo_char_point + u->insert_length);
  20768. /* now save the characters */
  20769. for (i=0; i < u->insert_length; ++i) {
  20770. s->undo_char[u->char_storage + i] =
  20771. nk_str_rune_at(&state->string, u->where + i);
  20772. }
  20773. }
  20774. nk_str_delete_runes(&state->string, r.where, r.delete_length);
  20775. }
  20776. if (r.insert_length) {
  20777. /* easy case: need to insert n characters */
  20778. nk_str_insert_text_runes(&state->string, r.where,
  20779. &s->undo_char[r.char_storage], r.insert_length);
  20780. }
  20781. state->cursor = r.where + r.insert_length;
  20782. s->undo_point++;
  20783. s->redo_point++;
  20784. }
  20785. NK_INTERN void
  20786. nk_textedit_makeundo_insert(struct nk_text_edit *state, int where, int length)
  20787. {
  20788. nk_textedit_createundo(&state->undo, where, 0, length);
  20789. }
  20790. NK_INTERN void
  20791. nk_textedit_makeundo_delete(struct nk_text_edit *state, int where, int length)
  20792. {
  20793. int i;
  20794. nk_rune *p = nk_textedit_createundo(&state->undo, where, length, 0);
  20795. if (p) {
  20796. for (i=0; i < length; ++i)
  20797. p[i] = nk_str_rune_at(&state->string, where+i);
  20798. }
  20799. }
  20800. NK_INTERN void
  20801. nk_textedit_makeundo_replace(struct nk_text_edit *state, int where,
  20802. int old_length, int new_length)
  20803. {
  20804. int i;
  20805. nk_rune *p = nk_textedit_createundo(&state->undo, where, old_length, new_length);
  20806. if (p) {
  20807. for (i=0; i < old_length; ++i)
  20808. p[i] = nk_str_rune_at(&state->string, where+i);
  20809. }
  20810. }
  20811. NK_LIB void
  20812. nk_textedit_clear_state(struct nk_text_edit *state, enum nk_text_edit_type type,
  20813. nk_plugin_filter filter)
  20814. {
  20815. /* reset the state to default */
  20816. state->undo.undo_point = 0;
  20817. state->undo.undo_char_point = 0;
  20818. state->undo.redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
  20819. state->undo.redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
  20820. state->select_end = state->select_start = 0;
  20821. state->cursor = 0;
  20822. state->has_preferred_x = 0;
  20823. state->preferred_x = 0;
  20824. state->cursor_at_end_of_line = 0;
  20825. state->initialized = 1;
  20826. state->single_line = (unsigned char)(type == NK_TEXT_EDIT_SINGLE_LINE);
  20827. state->mode = NK_TEXT_EDIT_MODE_VIEW;
  20828. state->filter = filter;
  20829. state->scrollbar = nk_vec2(0,0);
  20830. }
  20831. NK_API void
  20832. nk_textedit_init_fixed(struct nk_text_edit *state, void *memory, nk_size size)
  20833. {
  20834. NK_ASSERT(state);
  20835. NK_ASSERT(memory);
  20836. if (!state || !memory || !size) return;
  20837. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  20838. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  20839. nk_str_init_fixed(&state->string, memory, size);
  20840. }
  20841. NK_API void
  20842. nk_textedit_init(struct nk_text_edit *state, struct nk_allocator *alloc, nk_size size)
  20843. {
  20844. NK_ASSERT(state);
  20845. NK_ASSERT(alloc);
  20846. if (!state || !alloc) return;
  20847. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  20848. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  20849. nk_str_init(&state->string, alloc, size);
  20850. }
  20851. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  20852. NK_API void
  20853. nk_textedit_init_default(struct nk_text_edit *state)
  20854. {
  20855. NK_ASSERT(state);
  20856. if (!state) return;
  20857. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  20858. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  20859. nk_str_init_default(&state->string);
  20860. }
  20861. #endif
  20862. NK_API void
  20863. nk_textedit_select_all(struct nk_text_edit *state)
  20864. {
  20865. NK_ASSERT(state);
  20866. state->select_start = 0;
  20867. state->select_end = state->string.len;
  20868. }
  20869. NK_API void
  20870. nk_textedit_free(struct nk_text_edit *state)
  20871. {
  20872. NK_ASSERT(state);
  20873. if (!state) return;
  20874. nk_str_free(&state->string);
  20875. }
  20876. /* ===============================================================
  20877. *
  20878. * FILTER
  20879. *
  20880. * ===============================================================*/
  20881. NK_API int
  20882. nk_filter_default(const struct nk_text_edit *box, nk_rune unicode)
  20883. {
  20884. NK_UNUSED(unicode);
  20885. NK_UNUSED(box);
  20886. return nk_true;
  20887. }
  20888. NK_API int
  20889. nk_filter_ascii(const struct nk_text_edit *box, nk_rune unicode)
  20890. {
  20891. NK_UNUSED(box);
  20892. if (unicode > 128) return nk_false;
  20893. else return nk_true;
  20894. }
  20895. NK_API int
  20896. nk_filter_float(const struct nk_text_edit *box, nk_rune unicode)
  20897. {
  20898. NK_UNUSED(box);
  20899. if ((unicode < '0' || unicode > '9') && unicode != '.' && unicode != '-')
  20900. return nk_false;
  20901. else return nk_true;
  20902. }
  20903. NK_API int
  20904. nk_filter_decimal(const struct nk_text_edit *box, nk_rune unicode)
  20905. {
  20906. NK_UNUSED(box);
  20907. if ((unicode < '0' || unicode > '9') && unicode != '-')
  20908. return nk_false;
  20909. else return nk_true;
  20910. }
  20911. NK_API int
  20912. nk_filter_hex(const struct nk_text_edit *box, nk_rune unicode)
  20913. {
  20914. NK_UNUSED(box);
  20915. if ((unicode < '0' || unicode > '9') &&
  20916. (unicode < 'a' || unicode > 'f') &&
  20917. (unicode < 'A' || unicode > 'F'))
  20918. return nk_false;
  20919. else return nk_true;
  20920. }
  20921. NK_API int
  20922. nk_filter_oct(const struct nk_text_edit *box, nk_rune unicode)
  20923. {
  20924. NK_UNUSED(box);
  20925. if (unicode < '0' || unicode > '7')
  20926. return nk_false;
  20927. else return nk_true;
  20928. }
  20929. NK_API int
  20930. nk_filter_binary(const struct nk_text_edit *box, nk_rune unicode)
  20931. {
  20932. NK_UNUSED(box);
  20933. if (unicode != '0' && unicode != '1')
  20934. return nk_false;
  20935. else return nk_true;
  20936. }
  20937. /* ===============================================================
  20938. *
  20939. * EDIT
  20940. *
  20941. * ===============================================================*/
  20942. NK_LIB void
  20943. nk_edit_draw_text(struct nk_command_buffer *out,
  20944. const struct nk_style_edit *style, float pos_x, float pos_y,
  20945. float x_offset, const char *text, int byte_len, float row_height,
  20946. const struct nk_user_font *font, struct nk_color background,
  20947. struct nk_color foreground, int is_selected)
  20948. {
  20949. NK_ASSERT(out);
  20950. NK_ASSERT(font);
  20951. NK_ASSERT(style);
  20952. if (!text || !byte_len || !out || !style) return;
  20953. {int glyph_len = 0;
  20954. nk_rune unicode = 0;
  20955. int text_len = 0;
  20956. float line_width = 0;
  20957. float glyph_width;
  20958. const char *line = text;
  20959. float line_offset = 0;
  20960. int line_count = 0;
  20961. struct nk_text txt;
  20962. txt.padding = nk_vec2(0,0);
  20963. txt.background = background;
  20964. txt.text = foreground;
  20965. glyph_len = nk_utf_decode(text+text_len, &unicode, byte_len-text_len);
  20966. if (!glyph_len) return;
  20967. while ((text_len < byte_len) && glyph_len)
  20968. {
  20969. if (unicode == '\n') {
  20970. /* new line separator so draw previous line */
  20971. struct nk_rect label;
  20972. label.y = pos_y + line_offset;
  20973. label.h = row_height;
  20974. label.w = line_width;
  20975. label.x = pos_x;
  20976. if (!line_count)
  20977. label.x += x_offset;
  20978. if (is_selected) /* selection needs to draw different background color */
  20979. nk_fill_rect(out, label, 0, background);
  20980. nk_widget_text(out, label, line, (int)((text + text_len) - line),
  20981. &txt, NK_TEXT_CENTERED, font);
  20982. text_len++;
  20983. line_count++;
  20984. line_width = 0;
  20985. line = text + text_len;
  20986. line_offset += row_height;
  20987. glyph_len = nk_utf_decode(text + text_len, &unicode, (int)(byte_len-text_len));
  20988. continue;
  20989. }
  20990. if (unicode == '\r') {
  20991. text_len++;
  20992. glyph_len = nk_utf_decode(text + text_len, &unicode, byte_len-text_len);
  20993. continue;
  20994. }
  20995. glyph_width = font->width(font->userdata, font->height, text+text_len, glyph_len);
  20996. line_width += (float)glyph_width;
  20997. text_len += glyph_len;
  20998. glyph_len = nk_utf_decode(text + text_len, &unicode, byte_len-text_len);
  20999. continue;
  21000. }
  21001. if (line_width > 0) {
  21002. /* draw last line */
  21003. struct nk_rect label;
  21004. label.y = pos_y + line_offset;
  21005. label.h = row_height;
  21006. label.w = line_width;
  21007. label.x = pos_x;
  21008. if (!line_count)
  21009. label.x += x_offset;
  21010. if (is_selected)
  21011. nk_fill_rect(out, label, 0, background);
  21012. nk_widget_text(out, label, line, (int)((text + text_len) - line),
  21013. &txt, NK_TEXT_LEFT, font);
  21014. }}
  21015. }
  21016. NK_LIB nk_flags
  21017. nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
  21018. struct nk_rect bounds, nk_flags flags, nk_plugin_filter filter,
  21019. struct nk_text_edit *edit, const struct nk_style_edit *style,
  21020. struct nk_input *in, const struct nk_user_font *font)
  21021. {
  21022. struct nk_rect area;
  21023. nk_flags ret = 0;
  21024. float row_height;
  21025. char prev_state = 0;
  21026. char is_hovered = 0;
  21027. char select_all = 0;
  21028. char cursor_follow = 0;
  21029. struct nk_rect old_clip;
  21030. struct nk_rect clip;
  21031. NK_ASSERT(state);
  21032. NK_ASSERT(out);
  21033. NK_ASSERT(style);
  21034. if (!state || !out || !style)
  21035. return ret;
  21036. /* visible text area calculation */
  21037. area.x = bounds.x + style->padding.x + style->border;
  21038. area.y = bounds.y + style->padding.y + style->border;
  21039. area.w = bounds.w - (2.0f * style->padding.x + 2 * style->border);
  21040. area.h = bounds.h - (2.0f * style->padding.y + 2 * style->border);
  21041. if (flags & NK_EDIT_MULTILINE)
  21042. area.w = NK_MAX(0, area.w - style->scrollbar_size.x);
  21043. row_height = (flags & NK_EDIT_MULTILINE)? font->height + style->row_padding: area.h;
  21044. /* calculate clipping rectangle */
  21045. old_clip = out->clip;
  21046. nk_unify(&clip, &old_clip, area.x, area.y, area.x + area.w, area.y + area.h);
  21047. /* update edit state */
  21048. prev_state = (char)edit->active;
  21049. is_hovered = (char)nk_input_is_mouse_hovering_rect(in, bounds);
  21050. if (in && in->mouse.buttons[NK_BUTTON_LEFT].clicked && in->mouse.buttons[NK_BUTTON_LEFT].down) {
  21051. edit->active = NK_INBOX(in->mouse.pos.x, in->mouse.pos.y,
  21052. bounds.x, bounds.y, bounds.w, bounds.h);
  21053. }
  21054. /* (de)activate text editor */
  21055. if (!prev_state && edit->active) {
  21056. const enum nk_text_edit_type type = (flags & NK_EDIT_MULTILINE) ?
  21057. NK_TEXT_EDIT_MULTI_LINE: NK_TEXT_EDIT_SINGLE_LINE;
  21058. nk_textedit_clear_state(edit, type, filter);
  21059. if (flags & NK_EDIT_AUTO_SELECT)
  21060. select_all = nk_true;
  21061. if (flags & NK_EDIT_GOTO_END_ON_ACTIVATE) {
  21062. edit->cursor = edit->string.len;
  21063. in = 0;
  21064. }
  21065. } else if (!edit->active) edit->mode = NK_TEXT_EDIT_MODE_VIEW;
  21066. if (flags & NK_EDIT_READ_ONLY)
  21067. edit->mode = NK_TEXT_EDIT_MODE_VIEW;
  21068. else if (flags & NK_EDIT_ALWAYS_INSERT_MODE)
  21069. edit->mode = NK_TEXT_EDIT_MODE_INSERT;
  21070. ret = (edit->active) ? NK_EDIT_ACTIVE: NK_EDIT_INACTIVE;
  21071. if (prev_state != edit->active)
  21072. ret |= (edit->active) ? NK_EDIT_ACTIVATED: NK_EDIT_DEACTIVATED;
  21073. /* handle user input */
  21074. if (edit->active && in)
  21075. {
  21076. int shift_mod = in->keyboard.keys[NK_KEY_SHIFT].down;
  21077. const float mouse_x = (in->mouse.pos.x - area.x) + edit->scrollbar.x;
  21078. const float mouse_y = (in->mouse.pos.y - area.y) + edit->scrollbar.y;
  21079. /* mouse click handler */
  21080. is_hovered = (char)nk_input_is_mouse_hovering_rect(in, area);
  21081. if (select_all) {
  21082. nk_textedit_select_all(edit);
  21083. } else if (is_hovered && in->mouse.buttons[NK_BUTTON_LEFT].down &&
  21084. in->mouse.buttons[NK_BUTTON_LEFT].clicked) {
  21085. nk_textedit_click(edit, mouse_x, mouse_y, font, row_height);
  21086. } else if (is_hovered && in->mouse.buttons[NK_BUTTON_LEFT].down &&
  21087. (in->mouse.delta.x != 0.0f || in->mouse.delta.y != 0.0f)) {
  21088. nk_textedit_drag(edit, mouse_x, mouse_y, font, row_height);
  21089. cursor_follow = nk_true;
  21090. } else if (is_hovered && in->mouse.buttons[NK_BUTTON_RIGHT].clicked &&
  21091. in->mouse.buttons[NK_BUTTON_RIGHT].down) {
  21092. nk_textedit_key(edit, NK_KEY_TEXT_WORD_LEFT, nk_false, font, row_height);
  21093. nk_textedit_key(edit, NK_KEY_TEXT_WORD_RIGHT, nk_true, font, row_height);
  21094. cursor_follow = nk_true;
  21095. }
  21096. {int i; /* keyboard input */
  21097. int old_mode = edit->mode;
  21098. for (i = 0; i < NK_KEY_MAX; ++i) {
  21099. if (i == NK_KEY_ENTER || i == NK_KEY_TAB) continue; /* special case */
  21100. if (nk_input_is_key_pressed(in, (enum nk_keys)i)) {
  21101. nk_textedit_key(edit, (enum nk_keys)i, shift_mod, font, row_height);
  21102. cursor_follow = nk_true;
  21103. }
  21104. }
  21105. if (old_mode != edit->mode) {
  21106. in->keyboard.text_len = 0;
  21107. }}
  21108. /* text input */
  21109. edit->filter = filter;
  21110. if (in->keyboard.text_len) {
  21111. nk_textedit_text(edit, in->keyboard.text, in->keyboard.text_len);
  21112. cursor_follow = nk_true;
  21113. in->keyboard.text_len = 0;
  21114. }
  21115. /* enter key handler */
  21116. if (nk_input_is_key_pressed(in, NK_KEY_ENTER)) {
  21117. cursor_follow = nk_true;
  21118. if (flags & NK_EDIT_CTRL_ENTER_NEWLINE && shift_mod)
  21119. nk_textedit_text(edit, "\n", 1);
  21120. else if (flags & NK_EDIT_SIG_ENTER)
  21121. ret |= NK_EDIT_COMMITED;
  21122. else nk_textedit_text(edit, "\n", 1);
  21123. }
  21124. /* cut & copy handler */
  21125. {int copy= nk_input_is_key_pressed(in, NK_KEY_COPY);
  21126. int cut = nk_input_is_key_pressed(in, NK_KEY_CUT);
  21127. if ((copy || cut) && (flags & NK_EDIT_CLIPBOARD))
  21128. {
  21129. int glyph_len;
  21130. nk_rune unicode;
  21131. const char *text;
  21132. int b = edit->select_start;
  21133. int e = edit->select_end;
  21134. int begin = NK_MIN(b, e);
  21135. int end = NK_MAX(b, e);
  21136. text = nk_str_at_const(&edit->string, begin, &unicode, &glyph_len);
  21137. if (edit->clip.copy)
  21138. edit->clip.copy(edit->clip.userdata, text, end - begin);
  21139. if (cut && !(flags & NK_EDIT_READ_ONLY)){
  21140. nk_textedit_cut(edit);
  21141. cursor_follow = nk_true;
  21142. }
  21143. }}
  21144. /* paste handler */
  21145. {int paste = nk_input_is_key_pressed(in, NK_KEY_PASTE);
  21146. if (paste && (flags & NK_EDIT_CLIPBOARD) && edit->clip.paste) {
  21147. edit->clip.paste(edit->clip.userdata, edit);
  21148. cursor_follow = nk_true;
  21149. }}
  21150. /* tab handler */
  21151. {int tab = nk_input_is_key_pressed(in, NK_KEY_TAB);
  21152. if (tab && (flags & NK_EDIT_ALLOW_TAB)) {
  21153. nk_textedit_text(edit, " ", 4);
  21154. cursor_follow = nk_true;
  21155. }}
  21156. }
  21157. /* set widget state */
  21158. if (edit->active)
  21159. *state = NK_WIDGET_STATE_ACTIVE;
  21160. else nk_widget_state_reset(state);
  21161. if (is_hovered)
  21162. *state |= NK_WIDGET_STATE_HOVERED;
  21163. /* DRAW EDIT */
  21164. {const char *text = nk_str_get_const(&edit->string);
  21165. int len = nk_str_len_char(&edit->string);
  21166. {/* select background colors/images */
  21167. const struct nk_style_item *background;
  21168. if (*state & NK_WIDGET_STATE_ACTIVED)
  21169. background = &style->active;
  21170. else if (*state & NK_WIDGET_STATE_HOVER)
  21171. background = &style->hover;
  21172. else background = &style->normal;
  21173. /* draw background frame */
  21174. if (background->type == NK_STYLE_ITEM_COLOR) {
  21175. nk_stroke_rect(out, bounds, style->rounding, style->border, style->border_color);
  21176. nk_fill_rect(out, bounds, style->rounding, background->data.color);
  21177. } else nk_draw_image(out, bounds, &background->data.image, nk_white);}
  21178. area.w = NK_MAX(0, area.w - style->cursor_size);
  21179. if (edit->active)
  21180. {
  21181. int total_lines = 1;
  21182. struct nk_vec2 text_size = nk_vec2(0,0);
  21183. /* text pointer positions */
  21184. const char *cursor_ptr = 0;
  21185. const char *select_begin_ptr = 0;
  21186. const char *select_end_ptr = 0;
  21187. /* 2D pixel positions */
  21188. struct nk_vec2 cursor_pos = nk_vec2(0,0);
  21189. struct nk_vec2 selection_offset_start = nk_vec2(0,0);
  21190. struct nk_vec2 selection_offset_end = nk_vec2(0,0);
  21191. int selection_begin = NK_MIN(edit->select_start, edit->select_end);
  21192. int selection_end = NK_MAX(edit->select_start, edit->select_end);
  21193. /* calculate total line count + total space + cursor/selection position */
  21194. float line_width = 0.0f;
  21195. if (text && len)
  21196. {
  21197. /* utf8 encoding */
  21198. float glyph_width;
  21199. int glyph_len = 0;
  21200. nk_rune unicode = 0;
  21201. int text_len = 0;
  21202. int glyphs = 0;
  21203. int row_begin = 0;
  21204. glyph_len = nk_utf_decode(text, &unicode, len);
  21205. glyph_width = font->width(font->userdata, font->height, text, glyph_len);
  21206. line_width = 0;
  21207. /* iterate all lines */
  21208. while ((text_len < len) && glyph_len)
  21209. {
  21210. /* set cursor 2D position and line */
  21211. if (!cursor_ptr && glyphs == edit->cursor)
  21212. {
  21213. int glyph_offset;
  21214. struct nk_vec2 out_offset;
  21215. struct nk_vec2 row_size;
  21216. const char *remaining;
  21217. /* calculate 2d position */
  21218. cursor_pos.y = (float)(total_lines-1) * row_height;
  21219. row_size = nk_text_calculate_text_bounds(font, text+row_begin,
  21220. text_len-row_begin, row_height, &remaining,
  21221. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  21222. cursor_pos.x = row_size.x;
  21223. cursor_ptr = text + text_len;
  21224. }
  21225. /* set start selection 2D position and line */
  21226. if (!select_begin_ptr && edit->select_start != edit->select_end &&
  21227. glyphs == selection_begin)
  21228. {
  21229. int glyph_offset;
  21230. struct nk_vec2 out_offset;
  21231. struct nk_vec2 row_size;
  21232. const char *remaining;
  21233. /* calculate 2d position */
  21234. selection_offset_start.y = (float)(NK_MAX(total_lines-1,0)) * row_height;
  21235. row_size = nk_text_calculate_text_bounds(font, text+row_begin,
  21236. text_len-row_begin, row_height, &remaining,
  21237. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  21238. selection_offset_start.x = row_size.x;
  21239. select_begin_ptr = text + text_len;
  21240. }
  21241. /* set end selection 2D position and line */
  21242. if (!select_end_ptr && edit->select_start != edit->select_end &&
  21243. glyphs == selection_end)
  21244. {
  21245. int glyph_offset;
  21246. struct nk_vec2 out_offset;
  21247. struct nk_vec2 row_size;
  21248. const char *remaining;
  21249. /* calculate 2d position */
  21250. selection_offset_end.y = (float)(total_lines-1) * row_height;
  21251. row_size = nk_text_calculate_text_bounds(font, text+row_begin,
  21252. text_len-row_begin, row_height, &remaining,
  21253. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  21254. selection_offset_end.x = row_size.x;
  21255. select_end_ptr = text + text_len;
  21256. }
  21257. if (unicode == '\n') {
  21258. text_size.x = NK_MAX(text_size.x, line_width);
  21259. total_lines++;
  21260. line_width = 0;
  21261. text_len++;
  21262. glyphs++;
  21263. row_begin = text_len;
  21264. glyph_len = nk_utf_decode(text + text_len, &unicode, len-text_len);
  21265. glyph_width = font->width(font->userdata, font->height, text+text_len, glyph_len);
  21266. continue;
  21267. }
  21268. glyphs++;
  21269. text_len += glyph_len;
  21270. line_width += (float)glyph_width;
  21271. glyph_len = nk_utf_decode(text + text_len, &unicode, len-text_len);
  21272. glyph_width = font->width(font->userdata, font->height,
  21273. text+text_len, glyph_len);
  21274. continue;
  21275. }
  21276. text_size.y = (float)total_lines * row_height;
  21277. /* handle case when cursor is at end of text buffer */
  21278. if (!cursor_ptr && edit->cursor == edit->string.len) {
  21279. cursor_pos.x = line_width;
  21280. cursor_pos.y = text_size.y - row_height;
  21281. }
  21282. }
  21283. {
  21284. /* scrollbar */
  21285. if (cursor_follow)
  21286. {
  21287. /* update scrollbar to follow cursor */
  21288. if (!(flags & NK_EDIT_NO_HORIZONTAL_SCROLL)) {
  21289. /* horizontal scroll */
  21290. const float scroll_increment = area.w * 0.25f;
  21291. if (cursor_pos.x < edit->scrollbar.x)
  21292. edit->scrollbar.x = (float)(int)NK_MAX(0.0f, cursor_pos.x - scroll_increment);
  21293. if (cursor_pos.x >= edit->scrollbar.x + area.w)
  21294. edit->scrollbar.x = (float)(int)NK_MAX(0.0f, edit->scrollbar.x + scroll_increment);
  21295. } else edit->scrollbar.x = 0;
  21296. if (flags & NK_EDIT_MULTILINE) {
  21297. /* vertical scroll */
  21298. if (cursor_pos.y < edit->scrollbar.y)
  21299. edit->scrollbar.y = NK_MAX(0.0f, cursor_pos.y - row_height);
  21300. if (cursor_pos.y >= edit->scrollbar.y + area.h)
  21301. edit->scrollbar.y = edit->scrollbar.y + row_height;
  21302. } else edit->scrollbar.y = 0;
  21303. }
  21304. /* scrollbar widget */
  21305. if (flags & NK_EDIT_MULTILINE)
  21306. {
  21307. nk_flags ws;
  21308. struct nk_rect scroll;
  21309. float scroll_target;
  21310. float scroll_offset;
  21311. float scroll_step;
  21312. float scroll_inc;
  21313. scroll = area;
  21314. scroll.x = (bounds.x + bounds.w - style->border) - style->scrollbar_size.x;
  21315. scroll.w = style->scrollbar_size.x;
  21316. scroll_offset = edit->scrollbar.y;
  21317. scroll_step = scroll.h * 0.10f;
  21318. scroll_inc = scroll.h * 0.01f;
  21319. scroll_target = text_size.y;
  21320. edit->scrollbar.y = nk_do_scrollbarv(&ws, out, scroll, 0,
  21321. scroll_offset, scroll_target, scroll_step, scroll_inc,
  21322. &style->scrollbar, in, font);
  21323. }
  21324. }
  21325. /* draw text */
  21326. {struct nk_color background_color;
  21327. struct nk_color text_color;
  21328. struct nk_color sel_background_color;
  21329. struct nk_color sel_text_color;
  21330. struct nk_color cursor_color;
  21331. struct nk_color cursor_text_color;
  21332. const struct nk_style_item *background;
  21333. nk_push_scissor(out, clip);
  21334. /* select correct colors to draw */
  21335. if (*state & NK_WIDGET_STATE_ACTIVED) {
  21336. background = &style->active;
  21337. text_color = style->text_active;
  21338. sel_text_color = style->selected_text_hover;
  21339. sel_background_color = style->selected_hover;
  21340. cursor_color = style->cursor_hover;
  21341. cursor_text_color = style->cursor_text_hover;
  21342. } else if (*state & NK_WIDGET_STATE_HOVER) {
  21343. background = &style->hover;
  21344. text_color = style->text_hover;
  21345. sel_text_color = style->selected_text_hover;
  21346. sel_background_color = style->selected_hover;
  21347. cursor_text_color = style->cursor_text_hover;
  21348. cursor_color = style->cursor_hover;
  21349. } else {
  21350. background = &style->normal;
  21351. text_color = style->text_normal;
  21352. sel_text_color = style->selected_text_normal;
  21353. sel_background_color = style->selected_normal;
  21354. cursor_color = style->cursor_normal;
  21355. cursor_text_color = style->cursor_text_normal;
  21356. }
  21357. if (background->type == NK_STYLE_ITEM_IMAGE)
  21358. background_color = nk_rgba(0,0,0,0);
  21359. else background_color = background->data.color;
  21360. if (edit->select_start == edit->select_end) {
  21361. /* no selection so just draw the complete text */
  21362. const char *begin = nk_str_get_const(&edit->string);
  21363. int l = nk_str_len_char(&edit->string);
  21364. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  21365. area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
  21366. background_color, text_color, nk_false);
  21367. } else {
  21368. /* edit has selection so draw 1-3 text chunks */
  21369. if (edit->select_start != edit->select_end && selection_begin > 0){
  21370. /* draw unselected text before selection */
  21371. const char *begin = nk_str_get_const(&edit->string);
  21372. NK_ASSERT(select_begin_ptr);
  21373. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  21374. area.y - edit->scrollbar.y, 0, begin, (int)(select_begin_ptr - begin),
  21375. row_height, font, background_color, text_color, nk_false);
  21376. }
  21377. if (edit->select_start != edit->select_end) {
  21378. /* draw selected text */
  21379. NK_ASSERT(select_begin_ptr);
  21380. if (!select_end_ptr) {
  21381. const char *begin = nk_str_get_const(&edit->string);
  21382. select_end_ptr = begin + nk_str_len_char(&edit->string);
  21383. }
  21384. nk_edit_draw_text(out, style,
  21385. area.x - edit->scrollbar.x,
  21386. area.y + selection_offset_start.y - edit->scrollbar.y,
  21387. selection_offset_start.x,
  21388. select_begin_ptr, (int)(select_end_ptr - select_begin_ptr),
  21389. row_height, font, sel_background_color, sel_text_color, nk_true);
  21390. }
  21391. if ((edit->select_start != edit->select_end &&
  21392. selection_end < edit->string.len))
  21393. {
  21394. /* draw unselected text after selected text */
  21395. const char *begin = select_end_ptr;
  21396. const char *end = nk_str_get_const(&edit->string) +
  21397. nk_str_len_char(&edit->string);
  21398. NK_ASSERT(select_end_ptr);
  21399. nk_edit_draw_text(out, style,
  21400. area.x - edit->scrollbar.x,
  21401. area.y + selection_offset_end.y - edit->scrollbar.y,
  21402. selection_offset_end.x,
  21403. begin, (int)(end - begin), row_height, font,
  21404. background_color, text_color, nk_true);
  21405. }
  21406. }
  21407. /* cursor */
  21408. if (edit->select_start == edit->select_end)
  21409. {
  21410. if (edit->cursor >= nk_str_len(&edit->string) ||
  21411. (cursor_ptr && *cursor_ptr == '\n')) {
  21412. /* draw cursor at end of line */
  21413. struct nk_rect cursor;
  21414. cursor.w = style->cursor_size;
  21415. cursor.h = font->height;
  21416. cursor.x = area.x + cursor_pos.x - edit->scrollbar.x;
  21417. cursor.y = area.y + cursor_pos.y + row_height/2.0f - cursor.h/2.0f;
  21418. cursor.y -= edit->scrollbar.y;
  21419. nk_fill_rect(out, cursor, 0, cursor_color);
  21420. } else {
  21421. /* draw cursor inside text */
  21422. int glyph_len;
  21423. struct nk_rect label;
  21424. struct nk_text txt;
  21425. nk_rune unicode;
  21426. NK_ASSERT(cursor_ptr);
  21427. glyph_len = nk_utf_decode(cursor_ptr, &unicode, 4);
  21428. label.x = area.x + cursor_pos.x - edit->scrollbar.x;
  21429. label.y = area.y + cursor_pos.y - edit->scrollbar.y;
  21430. label.w = font->width(font->userdata, font->height, cursor_ptr, glyph_len);
  21431. label.h = row_height;
  21432. txt.padding = nk_vec2(0,0);
  21433. txt.background = cursor_color;;
  21434. txt.text = cursor_text_color;
  21435. nk_fill_rect(out, label, 0, cursor_color);
  21436. nk_widget_text(out, label, cursor_ptr, glyph_len, &txt, NK_TEXT_LEFT, font);
  21437. }
  21438. }}
  21439. } else {
  21440. /* not active so just draw text */
  21441. int l = nk_str_len_char(&edit->string);
  21442. const char *begin = nk_str_get_const(&edit->string);
  21443. const struct nk_style_item *background;
  21444. struct nk_color background_color;
  21445. struct nk_color text_color;
  21446. nk_push_scissor(out, clip);
  21447. if (*state & NK_WIDGET_STATE_ACTIVED) {
  21448. background = &style->active;
  21449. text_color = style->text_active;
  21450. } else if (*state & NK_WIDGET_STATE_HOVER) {
  21451. background = &style->hover;
  21452. text_color = style->text_hover;
  21453. } else {
  21454. background = &style->normal;
  21455. text_color = style->text_normal;
  21456. }
  21457. if (background->type == NK_STYLE_ITEM_IMAGE)
  21458. background_color = nk_rgba(0,0,0,0);
  21459. else background_color = background->data.color;
  21460. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  21461. area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
  21462. background_color, text_color, nk_false);
  21463. }
  21464. nk_push_scissor(out, old_clip);}
  21465. return ret;
  21466. }
  21467. NK_API void
  21468. nk_edit_focus(struct nk_context *ctx, nk_flags flags)
  21469. {
  21470. nk_hash hash;
  21471. struct nk_window *win;
  21472. NK_ASSERT(ctx);
  21473. NK_ASSERT(ctx->current);
  21474. if (!ctx || !ctx->current) return;
  21475. win = ctx->current;
  21476. hash = win->edit.seq;
  21477. win->edit.active = nk_true;
  21478. win->edit.name = hash;
  21479. if (flags & NK_EDIT_ALWAYS_INSERT_MODE)
  21480. win->edit.mode = NK_TEXT_EDIT_MODE_INSERT;
  21481. }
  21482. NK_API void
  21483. nk_edit_unfocus(struct nk_context *ctx)
  21484. {
  21485. struct nk_window *win;
  21486. NK_ASSERT(ctx);
  21487. NK_ASSERT(ctx->current);
  21488. if (!ctx || !ctx->current) return;
  21489. win = ctx->current;
  21490. win->edit.active = nk_false;
  21491. win->edit.name = 0;
  21492. }
  21493. NK_API nk_flags
  21494. nk_edit_string(struct nk_context *ctx, nk_flags flags,
  21495. char *memory, int *len, int max, nk_plugin_filter filter)
  21496. {
  21497. nk_hash hash;
  21498. nk_flags state;
  21499. struct nk_text_edit *edit;
  21500. struct nk_window *win;
  21501. NK_ASSERT(ctx);
  21502. NK_ASSERT(memory);
  21503. NK_ASSERT(len);
  21504. if (!ctx || !memory || !len)
  21505. return 0;
  21506. filter = (!filter) ? nk_filter_default: filter;
  21507. win = ctx->current;
  21508. hash = win->edit.seq;
  21509. edit = &ctx->text_edit;
  21510. nk_textedit_clear_state(&ctx->text_edit, (flags & NK_EDIT_MULTILINE)?
  21511. NK_TEXT_EDIT_MULTI_LINE: NK_TEXT_EDIT_SINGLE_LINE, filter);
  21512. if (win->edit.active && hash == win->edit.name) {
  21513. if (flags & NK_EDIT_NO_CURSOR)
  21514. edit->cursor = nk_utf_len(memory, *len);
  21515. else edit->cursor = win->edit.cursor;
  21516. if (!(flags & NK_EDIT_SELECTABLE)) {
  21517. edit->select_start = win->edit.cursor;
  21518. edit->select_end = win->edit.cursor;
  21519. } else {
  21520. edit->select_start = win->edit.sel_start;
  21521. edit->select_end = win->edit.sel_end;
  21522. }
  21523. edit->mode = win->edit.mode;
  21524. edit->scrollbar.x = (float)win->edit.scrollbar.x;
  21525. edit->scrollbar.y = (float)win->edit.scrollbar.y;
  21526. edit->active = nk_true;
  21527. } else edit->active = nk_false;
  21528. max = NK_MAX(1, max);
  21529. *len = NK_MIN(*len, max-1);
  21530. nk_str_init_fixed(&edit->string, memory, (nk_size)max);
  21531. edit->string.buffer.allocated = (nk_size)*len;
  21532. edit->string.len = nk_utf_len(memory, *len);
  21533. state = nk_edit_buffer(ctx, flags, edit, filter);
  21534. *len = (int)edit->string.buffer.allocated;
  21535. if (edit->active) {
  21536. win->edit.cursor = edit->cursor;
  21537. win->edit.sel_start = edit->select_start;
  21538. win->edit.sel_end = edit->select_end;
  21539. win->edit.mode = edit->mode;
  21540. win->edit.scrollbar.x = (nk_uint)edit->scrollbar.x;
  21541. win->edit.scrollbar.y = (nk_uint)edit->scrollbar.y;
  21542. } return state;
  21543. }
  21544. NK_API nk_flags
  21545. nk_edit_buffer(struct nk_context *ctx, nk_flags flags,
  21546. struct nk_text_edit *edit, nk_plugin_filter filter)
  21547. {
  21548. struct nk_window *win;
  21549. struct nk_style *style;
  21550. struct nk_input *in;
  21551. enum nk_widget_layout_states state;
  21552. struct nk_rect bounds;
  21553. nk_flags ret_flags = 0;
  21554. unsigned char prev_state;
  21555. nk_hash hash;
  21556. /* make sure correct values */
  21557. NK_ASSERT(ctx);
  21558. NK_ASSERT(edit);
  21559. NK_ASSERT(ctx->current);
  21560. NK_ASSERT(ctx->current->layout);
  21561. if (!ctx || !ctx->current || !ctx->current->layout)
  21562. return 0;
  21563. win = ctx->current;
  21564. style = &ctx->style;
  21565. state = nk_widget(&bounds, ctx);
  21566. if (!state) return state;
  21567. in = (win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  21568. /* check if edit is currently hot item */
  21569. hash = win->edit.seq++;
  21570. if (win->edit.active && hash == win->edit.name) {
  21571. if (flags & NK_EDIT_NO_CURSOR)
  21572. edit->cursor = edit->string.len;
  21573. if (!(flags & NK_EDIT_SELECTABLE)) {
  21574. edit->select_start = edit->cursor;
  21575. edit->select_end = edit->cursor;
  21576. }
  21577. if (flags & NK_EDIT_CLIPBOARD)
  21578. edit->clip = ctx->clip;
  21579. edit->active = (unsigned char)win->edit.active;
  21580. } else edit->active = nk_false;
  21581. edit->mode = win->edit.mode;
  21582. filter = (!filter) ? nk_filter_default: filter;
  21583. prev_state = (unsigned char)edit->active;
  21584. in = (flags & NK_EDIT_READ_ONLY) ? 0: in;
  21585. ret_flags = nk_do_edit(&ctx->last_widget_state, &win->buffer, bounds, flags,
  21586. filter, edit, &style->edit, in, style->font);
  21587. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  21588. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_TEXT];
  21589. if (edit->active && prev_state != edit->active) {
  21590. /* current edit is now hot */
  21591. win->edit.active = nk_true;
  21592. win->edit.name = hash;
  21593. } else if (prev_state && !edit->active) {
  21594. /* current edit is now cold */
  21595. win->edit.active = nk_false;
  21596. } return ret_flags;
  21597. }
  21598. NK_API nk_flags
  21599. nk_edit_string_zero_terminated(struct nk_context *ctx, nk_flags flags,
  21600. char *buffer, int max, nk_plugin_filter filter)
  21601. {
  21602. nk_flags result;
  21603. int len = nk_strlen(buffer);
  21604. result = nk_edit_string(ctx, flags, buffer, &len, max, filter);
  21605. buffer[NK_MIN(NK_MAX(max-1,0), len)] = '\0';
  21606. return result;
  21607. }
  21608. /* ===============================================================
  21609. *
  21610. * PROPERTY
  21611. *
  21612. * ===============================================================*/
  21613. NK_LIB void
  21614. nk_drag_behavior(nk_flags *state, const struct nk_input *in,
  21615. struct nk_rect drag, struct nk_property_variant *variant,
  21616. float inc_per_pixel)
  21617. {
  21618. int left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  21619. int left_mouse_click_in_cursor = in &&
  21620. nk_input_has_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, drag, nk_true);
  21621. nk_widget_state_reset(state);
  21622. if (nk_input_is_mouse_hovering_rect(in, drag))
  21623. *state = NK_WIDGET_STATE_HOVERED;
  21624. if (left_mouse_down && left_mouse_click_in_cursor) {
  21625. float delta, pixels;
  21626. pixels = in->mouse.delta.x;
  21627. delta = pixels * inc_per_pixel;
  21628. switch (variant->kind) {
  21629. default: break;
  21630. case NK_PROPERTY_INT:
  21631. variant->value.i = variant->value.i + (int)delta;
  21632. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
  21633. break;
  21634. case NK_PROPERTY_FLOAT:
  21635. variant->value.f = variant->value.f + (float)delta;
  21636. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
  21637. break;
  21638. case NK_PROPERTY_DOUBLE:
  21639. variant->value.d = variant->value.d + (double)delta;
  21640. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
  21641. break;
  21642. }
  21643. *state = NK_WIDGET_STATE_ACTIVE;
  21644. }
  21645. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, drag))
  21646. *state |= NK_WIDGET_STATE_ENTERED;
  21647. else if (nk_input_is_mouse_prev_hovering_rect(in, drag))
  21648. *state |= NK_WIDGET_STATE_LEFT;
  21649. }
  21650. NK_LIB void
  21651. nk_property_behavior(nk_flags *ws, const struct nk_input *in,
  21652. struct nk_rect property, struct nk_rect label, struct nk_rect edit,
  21653. struct nk_rect empty, int *state, struct nk_property_variant *variant,
  21654. float inc_per_pixel)
  21655. {
  21656. if (in && *state == NK_PROPERTY_DEFAULT) {
  21657. if (nk_button_behavior(ws, edit, in, NK_BUTTON_DEFAULT))
  21658. *state = NK_PROPERTY_EDIT;
  21659. else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, label, nk_true))
  21660. *state = NK_PROPERTY_DRAG;
  21661. else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, empty, nk_true))
  21662. *state = NK_PROPERTY_DRAG;
  21663. }
  21664. if (*state == NK_PROPERTY_DRAG) {
  21665. nk_drag_behavior(ws, in, property, variant, inc_per_pixel);
  21666. if (!(*ws & NK_WIDGET_STATE_ACTIVED)) *state = NK_PROPERTY_DEFAULT;
  21667. }
  21668. }
  21669. NK_LIB void
  21670. nk_draw_property(struct nk_command_buffer *out, const struct nk_style_property *style,
  21671. const struct nk_rect *bounds, const struct nk_rect *label, nk_flags state,
  21672. const char *name, int len, const struct nk_user_font *font)
  21673. {
  21674. struct nk_text text;
  21675. const struct nk_style_item *background;
  21676. /* select correct background and text color */
  21677. if (state & NK_WIDGET_STATE_ACTIVED) {
  21678. background = &style->active;
  21679. text.text = style->label_active;
  21680. } else if (state & NK_WIDGET_STATE_HOVER) {
  21681. background = &style->hover;
  21682. text.text = style->label_hover;
  21683. } else {
  21684. background = &style->normal;
  21685. text.text = style->label_normal;
  21686. }
  21687. /* draw background */
  21688. if (background->type == NK_STYLE_ITEM_IMAGE) {
  21689. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  21690. text.background = nk_rgba(0,0,0,0);
  21691. } else {
  21692. text.background = background->data.color;
  21693. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  21694. nk_stroke_rect(out, *bounds, style->rounding, style->border, background->data.color);
  21695. }
  21696. /* draw label */
  21697. text.padding = nk_vec2(0,0);
  21698. nk_widget_text(out, *label, name, len, &text, NK_TEXT_CENTERED, font);
  21699. }
  21700. NK_LIB void
  21701. nk_do_property(nk_flags *ws,
  21702. struct nk_command_buffer *out, struct nk_rect property,
  21703. const char *name, struct nk_property_variant *variant,
  21704. float inc_per_pixel, char *buffer, int *len,
  21705. int *state, int *cursor, int *select_begin, int *select_end,
  21706. const struct nk_style_property *style,
  21707. enum nk_property_filter filter, struct nk_input *in,
  21708. const struct nk_user_font *font, struct nk_text_edit *text_edit,
  21709. enum nk_button_behavior behavior)
  21710. {
  21711. const nk_plugin_filter filters[] = {
  21712. nk_filter_decimal,
  21713. nk_filter_float
  21714. };
  21715. int active, old;
  21716. int num_len, name_len;
  21717. char string[NK_MAX_NUMBER_BUFFER];
  21718. float size;
  21719. char *dst = 0;
  21720. int *length;
  21721. struct nk_rect left;
  21722. struct nk_rect right;
  21723. struct nk_rect label;
  21724. struct nk_rect edit;
  21725. struct nk_rect empty;
  21726. /* left decrement button */
  21727. left.h = font->height/2;
  21728. left.w = left.h;
  21729. left.x = property.x + style->border + style->padding.x;
  21730. left.y = property.y + style->border + property.h/2.0f - left.h/2;
  21731. /* text label */
  21732. name_len = nk_strlen(name);
  21733. size = font->width(font->userdata, font->height, name, name_len);
  21734. label.x = left.x + left.w + style->padding.x;
  21735. label.w = (float)size + 2 * style->padding.x;
  21736. label.y = property.y + style->border + style->padding.y;
  21737. label.h = property.h - (2 * style->border + 2 * style->padding.y);
  21738. /* right increment button */
  21739. right.y = left.y;
  21740. right.w = left.w;
  21741. right.h = left.h;
  21742. right.x = property.x + property.w - (right.w + style->padding.x);
  21743. /* edit */
  21744. if (*state == NK_PROPERTY_EDIT) {
  21745. size = font->width(font->userdata, font->height, buffer, *len);
  21746. size += style->edit.cursor_size;
  21747. length = len;
  21748. dst = buffer;
  21749. } else {
  21750. switch (variant->kind) {
  21751. default: break;
  21752. case NK_PROPERTY_INT:
  21753. nk_itoa(string, variant->value.i);
  21754. num_len = nk_strlen(string);
  21755. break;
  21756. case NK_PROPERTY_FLOAT:
  21757. NK_DTOA(string, (double)variant->value.f);
  21758. num_len = nk_string_float_limit(string, NK_MAX_FLOAT_PRECISION);
  21759. break;
  21760. case NK_PROPERTY_DOUBLE:
  21761. NK_DTOA(string, variant->value.d);
  21762. num_len = nk_string_float_limit(string, NK_MAX_FLOAT_PRECISION);
  21763. break;
  21764. }
  21765. size = font->width(font->userdata, font->height, string, num_len);
  21766. dst = string;
  21767. length = &num_len;
  21768. }
  21769. edit.w = (float)size + 2 * style->padding.x;
  21770. edit.w = NK_MIN(edit.w, right.x - (label.x + label.w));
  21771. edit.x = right.x - (edit.w + style->padding.x);
  21772. edit.y = property.y + style->border;
  21773. edit.h = property.h - (2 * style->border);
  21774. /* empty left space activator */
  21775. empty.w = edit.x - (label.x + label.w);
  21776. empty.x = label.x + label.w;
  21777. empty.y = property.y;
  21778. empty.h = property.h;
  21779. /* update property */
  21780. old = (*state == NK_PROPERTY_EDIT);
  21781. nk_property_behavior(ws, in, property, label, edit, empty, state, variant, inc_per_pixel);
  21782. /* draw property */
  21783. if (style->draw_begin) style->draw_begin(out, style->userdata);
  21784. nk_draw_property(out, style, &property, &label, *ws, name, name_len, font);
  21785. if (style->draw_end) style->draw_end(out, style->userdata);
  21786. /* execute right button */
  21787. if (nk_do_button_symbol(ws, out, left, style->sym_left, behavior, &style->dec_button, in, font)) {
  21788. switch (variant->kind) {
  21789. default: break;
  21790. case NK_PROPERTY_INT:
  21791. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i - variant->step.i, variant->max_value.i); break;
  21792. case NK_PROPERTY_FLOAT:
  21793. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f - variant->step.f, variant->max_value.f); break;
  21794. case NK_PROPERTY_DOUBLE:
  21795. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d - variant->step.d, variant->max_value.d); break;
  21796. }
  21797. }
  21798. /* execute left button */
  21799. if (nk_do_button_symbol(ws, out, right, style->sym_right, behavior, &style->inc_button, in, font)) {
  21800. switch (variant->kind) {
  21801. default: break;
  21802. case NK_PROPERTY_INT:
  21803. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i + variant->step.i, variant->max_value.i); break;
  21804. case NK_PROPERTY_FLOAT:
  21805. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f + variant->step.f, variant->max_value.f); break;
  21806. case NK_PROPERTY_DOUBLE:
  21807. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d + variant->step.d, variant->max_value.d); break;
  21808. }
  21809. }
  21810. if (old != NK_PROPERTY_EDIT && (*state == NK_PROPERTY_EDIT)) {
  21811. /* property has been activated so setup buffer */
  21812. NK_MEMCPY(buffer, dst, (nk_size)*length);
  21813. *cursor = nk_utf_len(buffer, *length);
  21814. *len = *length;
  21815. length = len;
  21816. dst = buffer;
  21817. active = 0;
  21818. } else active = (*state == NK_PROPERTY_EDIT);
  21819. /* execute and run text edit field */
  21820. nk_textedit_clear_state(text_edit, NK_TEXT_EDIT_SINGLE_LINE, filters[filter]);
  21821. text_edit->active = (unsigned char)active;
  21822. text_edit->string.len = *length;
  21823. text_edit->cursor = NK_CLAMP(0, *cursor, *length);
  21824. text_edit->select_start = NK_CLAMP(0,*select_begin, *length);
  21825. text_edit->select_end = NK_CLAMP(0,*select_end, *length);
  21826. text_edit->string.buffer.allocated = (nk_size)*length;
  21827. text_edit->string.buffer.memory.size = NK_MAX_NUMBER_BUFFER;
  21828. text_edit->string.buffer.memory.ptr = dst;
  21829. text_edit->string.buffer.size = NK_MAX_NUMBER_BUFFER;
  21830. text_edit->mode = NK_TEXT_EDIT_MODE_INSERT;
  21831. nk_do_edit(ws, out, edit, NK_EDIT_FIELD|NK_EDIT_AUTO_SELECT,
  21832. filters[filter], text_edit, &style->edit, (*state == NK_PROPERTY_EDIT) ? in: 0, font);
  21833. *length = text_edit->string.len;
  21834. *cursor = text_edit->cursor;
  21835. *select_begin = text_edit->select_start;
  21836. *select_end = text_edit->select_end;
  21837. if (text_edit->active && nk_input_is_key_pressed(in, NK_KEY_ENTER))
  21838. text_edit->active = nk_false;
  21839. if (active && !text_edit->active) {
  21840. /* property is now not active so convert edit text to value*/
  21841. *state = NK_PROPERTY_DEFAULT;
  21842. buffer[*len] = '\0';
  21843. switch (variant->kind) {
  21844. default: break;
  21845. case NK_PROPERTY_INT:
  21846. variant->value.i = nk_strtoi(buffer, 0);
  21847. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
  21848. break;
  21849. case NK_PROPERTY_FLOAT:
  21850. nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
  21851. variant->value.f = nk_strtof(buffer, 0);
  21852. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
  21853. break;
  21854. case NK_PROPERTY_DOUBLE:
  21855. nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
  21856. variant->value.d = nk_strtod(buffer, 0);
  21857. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
  21858. break;
  21859. }
  21860. }
  21861. }
  21862. NK_LIB struct nk_property_variant
  21863. nk_property_variant_int(int value, int min_value, int max_value, int step)
  21864. {
  21865. struct nk_property_variant result;
  21866. result.kind = NK_PROPERTY_INT;
  21867. result.value.i = value;
  21868. result.min_value.i = min_value;
  21869. result.max_value.i = max_value;
  21870. result.step.i = step;
  21871. return result;
  21872. }
  21873. NK_LIB struct nk_property_variant
  21874. nk_property_variant_float(float value, float min_value, float max_value, float step)
  21875. {
  21876. struct nk_property_variant result;
  21877. result.kind = NK_PROPERTY_FLOAT;
  21878. result.value.f = value;
  21879. result.min_value.f = min_value;
  21880. result.max_value.f = max_value;
  21881. result.step.f = step;
  21882. return result;
  21883. }
  21884. NK_LIB struct nk_property_variant
  21885. nk_property_variant_double(double value, double min_value, double max_value,
  21886. double step)
  21887. {
  21888. struct nk_property_variant result;
  21889. result.kind = NK_PROPERTY_DOUBLE;
  21890. result.value.d = value;
  21891. result.min_value.d = min_value;
  21892. result.max_value.d = max_value;
  21893. result.step.d = step;
  21894. return result;
  21895. }
  21896. NK_LIB void
  21897. nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant *variant,
  21898. float inc_per_pixel, const enum nk_property_filter filter)
  21899. {
  21900. struct nk_window *win;
  21901. struct nk_panel *layout;
  21902. struct nk_input *in;
  21903. const struct nk_style *style;
  21904. struct nk_rect bounds;
  21905. enum nk_widget_layout_states s;
  21906. int *state = 0;
  21907. nk_hash hash = 0;
  21908. char *buffer = 0;
  21909. int *len = 0;
  21910. int *cursor = 0;
  21911. int *select_begin = 0;
  21912. int *select_end = 0;
  21913. int old_state;
  21914. char dummy_buffer[NK_MAX_NUMBER_BUFFER];
  21915. int dummy_state = NK_PROPERTY_DEFAULT;
  21916. int dummy_length = 0;
  21917. int dummy_cursor = 0;
  21918. int dummy_select_begin = 0;
  21919. int dummy_select_end = 0;
  21920. NK_ASSERT(ctx);
  21921. NK_ASSERT(ctx->current);
  21922. NK_ASSERT(ctx->current->layout);
  21923. if (!ctx || !ctx->current || !ctx->current->layout)
  21924. return;
  21925. win = ctx->current;
  21926. layout = win->layout;
  21927. style = &ctx->style;
  21928. s = nk_widget(&bounds, ctx);
  21929. if (!s) return;
  21930. /* calculate hash from name */
  21931. if (name[0] == '#') {
  21932. hash = nk_murmur_hash(name, (int)nk_strlen(name), win->property.seq++);
  21933. name++; /* special number hash */
  21934. } else hash = nk_murmur_hash(name, (int)nk_strlen(name), 42);
  21935. /* check if property is currently hot item */
  21936. if (win->property.active && hash == win->property.name) {
  21937. buffer = win->property.buffer;
  21938. len = &win->property.length;
  21939. cursor = &win->property.cursor;
  21940. state = &win->property.state;
  21941. select_begin = &win->property.select_start;
  21942. select_end = &win->property.select_end;
  21943. } else {
  21944. buffer = dummy_buffer;
  21945. len = &dummy_length;
  21946. cursor = &dummy_cursor;
  21947. state = &dummy_state;
  21948. select_begin = &dummy_select_begin;
  21949. select_end = &dummy_select_end;
  21950. }
  21951. /* execute property widget */
  21952. old_state = *state;
  21953. ctx->text_edit.clip = ctx->clip;
  21954. in = ((s == NK_WIDGET_ROM && !win->property.active) ||
  21955. layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  21956. nk_do_property(&ctx->last_widget_state, &win->buffer, bounds, name,
  21957. variant, inc_per_pixel, buffer, len, state, cursor, select_begin,
  21958. select_end, &style->property, filter, in, style->font, &ctx->text_edit,
  21959. ctx->button_behavior);
  21960. if (in && *state != NK_PROPERTY_DEFAULT && !win->property.active) {
  21961. /* current property is now hot */
  21962. win->property.active = 1;
  21963. NK_MEMCPY(win->property.buffer, buffer, (nk_size)*len);
  21964. win->property.length = *len;
  21965. win->property.cursor = *cursor;
  21966. win->property.state = *state;
  21967. win->property.name = hash;
  21968. win->property.select_start = *select_begin;
  21969. win->property.select_end = *select_end;
  21970. if (*state == NK_PROPERTY_DRAG) {
  21971. ctx->input.mouse.grab = nk_true;
  21972. ctx->input.mouse.grabbed = nk_true;
  21973. }
  21974. }
  21975. /* check if previously active property is now inactive */
  21976. if (*state == NK_PROPERTY_DEFAULT && old_state != NK_PROPERTY_DEFAULT) {
  21977. if (old_state == NK_PROPERTY_DRAG) {
  21978. ctx->input.mouse.grab = nk_false;
  21979. ctx->input.mouse.grabbed = nk_false;
  21980. ctx->input.mouse.ungrab = nk_true;
  21981. }
  21982. win->property.select_start = 0;
  21983. win->property.select_end = 0;
  21984. win->property.active = 0;
  21985. }
  21986. }
  21987. NK_API void
  21988. nk_property_int(struct nk_context *ctx, const char *name,
  21989. int min, int *val, int max, int step, float inc_per_pixel)
  21990. {
  21991. struct nk_property_variant variant;
  21992. NK_ASSERT(ctx);
  21993. NK_ASSERT(name);
  21994. NK_ASSERT(val);
  21995. if (!ctx || !ctx->current || !name || !val) return;
  21996. variant = nk_property_variant_int(*val, min, max, step);
  21997. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
  21998. *val = variant.value.i;
  21999. }
  22000. NK_API void
  22001. nk_property_float(struct nk_context *ctx, const char *name,
  22002. float min, float *val, float max, float step, float inc_per_pixel)
  22003. {
  22004. struct nk_property_variant variant;
  22005. NK_ASSERT(ctx);
  22006. NK_ASSERT(name);
  22007. NK_ASSERT(val);
  22008. if (!ctx || !ctx->current || !name || !val) return;
  22009. variant = nk_property_variant_float(*val, min, max, step);
  22010. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  22011. *val = variant.value.f;
  22012. }
  22013. NK_API void
  22014. nk_property_double(struct nk_context *ctx, const char *name,
  22015. double min, double *val, double max, double step, float inc_per_pixel)
  22016. {
  22017. struct nk_property_variant variant;
  22018. NK_ASSERT(ctx);
  22019. NK_ASSERT(name);
  22020. NK_ASSERT(val);
  22021. if (!ctx || !ctx->current || !name || !val) return;
  22022. variant = nk_property_variant_double(*val, min, max, step);
  22023. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  22024. *val = variant.value.d;
  22025. }
  22026. NK_API int
  22027. nk_propertyi(struct nk_context *ctx, const char *name, int min, int val,
  22028. int max, int step, float inc_per_pixel)
  22029. {
  22030. struct nk_property_variant variant;
  22031. NK_ASSERT(ctx);
  22032. NK_ASSERT(name);
  22033. if (!ctx || !ctx->current || !name) return val;
  22034. variant = nk_property_variant_int(val, min, max, step);
  22035. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
  22036. val = variant.value.i;
  22037. return val;
  22038. }
  22039. NK_API float
  22040. nk_propertyf(struct nk_context *ctx, const char *name, float min,
  22041. float val, float max, float step, float inc_per_pixel)
  22042. {
  22043. struct nk_property_variant variant;
  22044. NK_ASSERT(ctx);
  22045. NK_ASSERT(name);
  22046. if (!ctx || !ctx->current || !name) return val;
  22047. variant = nk_property_variant_float(val, min, max, step);
  22048. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  22049. val = variant.value.f;
  22050. return val;
  22051. }
  22052. NK_API double
  22053. nk_propertyd(struct nk_context *ctx, const char *name, double min,
  22054. double val, double max, double step, float inc_per_pixel)
  22055. {
  22056. struct nk_property_variant variant;
  22057. NK_ASSERT(ctx);
  22058. NK_ASSERT(name);
  22059. if (!ctx || !ctx->current || !name) return val;
  22060. variant = nk_property_variant_double(val, min, max, step);
  22061. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  22062. val = variant.value.d;
  22063. return val;
  22064. }
  22065. /* ==============================================================
  22066. *
  22067. * CHART
  22068. *
  22069. * ===============================================================*/
  22070. NK_API int
  22071. nk_chart_begin_colored(struct nk_context *ctx, enum nk_chart_type type,
  22072. struct nk_color color, struct nk_color highlight,
  22073. int count, float min_value, float max_value)
  22074. {
  22075. struct nk_window *win;
  22076. struct nk_chart *chart;
  22077. const struct nk_style *config;
  22078. const struct nk_style_chart *style;
  22079. const struct nk_style_item *background;
  22080. struct nk_rect bounds = {0, 0, 0, 0};
  22081. NK_ASSERT(ctx);
  22082. NK_ASSERT(ctx->current);
  22083. NK_ASSERT(ctx->current->layout);
  22084. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  22085. if (!nk_widget(&bounds, ctx)) {
  22086. chart = &ctx->current->layout->chart;
  22087. nk_zero(chart, sizeof(*chart));
  22088. return 0;
  22089. }
  22090. win = ctx->current;
  22091. config = &ctx->style;
  22092. chart = &win->layout->chart;
  22093. style = &config->chart;
  22094. /* setup basic generic chart */
  22095. nk_zero(chart, sizeof(*chart));
  22096. chart->x = bounds.x + style->padding.x;
  22097. chart->y = bounds.y + style->padding.y;
  22098. chart->w = bounds.w - 2 * style->padding.x;
  22099. chart->h = bounds.h - 2 * style->padding.y;
  22100. chart->w = NK_MAX(chart->w, 2 * style->padding.x);
  22101. chart->h = NK_MAX(chart->h, 2 * style->padding.y);
  22102. /* add first slot into chart */
  22103. {struct nk_chart_slot *slot = &chart->slots[chart->slot++];
  22104. slot->type = type;
  22105. slot->count = count;
  22106. slot->color = color;
  22107. slot->highlight = highlight;
  22108. slot->min = NK_MIN(min_value, max_value);
  22109. slot->max = NK_MAX(min_value, max_value);
  22110. slot->range = slot->max - slot->min;}
  22111. /* draw chart background */
  22112. background = &style->background;
  22113. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22114. nk_draw_image(&win->buffer, bounds, &background->data.image, nk_white);
  22115. } else {
  22116. nk_fill_rect(&win->buffer, bounds, style->rounding, style->border_color);
  22117. nk_fill_rect(&win->buffer, nk_shrink_rect(bounds, style->border),
  22118. style->rounding, style->background.data.color);
  22119. }
  22120. return 1;
  22121. }
  22122. NK_API int
  22123. nk_chart_begin(struct nk_context *ctx, const enum nk_chart_type type,
  22124. int count, float min_value, float max_value)
  22125. {
  22126. return nk_chart_begin_colored(ctx, type, ctx->style.chart.color,
  22127. ctx->style.chart.selected_color, count, min_value, max_value);
  22128. }
  22129. NK_API void
  22130. nk_chart_add_slot_colored(struct nk_context *ctx, const enum nk_chart_type type,
  22131. struct nk_color color, struct nk_color highlight,
  22132. int count, float min_value, float max_value)
  22133. {
  22134. NK_ASSERT(ctx);
  22135. NK_ASSERT(ctx->current);
  22136. NK_ASSERT(ctx->current->layout);
  22137. NK_ASSERT(ctx->current->layout->chart.slot < NK_CHART_MAX_SLOT);
  22138. if (!ctx || !ctx->current || !ctx->current->layout) return;
  22139. if (ctx->current->layout->chart.slot >= NK_CHART_MAX_SLOT) return;
  22140. /* add another slot into the graph */
  22141. {struct nk_chart *chart = &ctx->current->layout->chart;
  22142. struct nk_chart_slot *slot = &chart->slots[chart->slot++];
  22143. slot->type = type;
  22144. slot->count = count;
  22145. slot->color = color;
  22146. slot->highlight = highlight;
  22147. slot->min = NK_MIN(min_value, max_value);
  22148. slot->max = NK_MAX(min_value, max_value);
  22149. slot->range = slot->max - slot->min;}
  22150. }
  22151. NK_API void
  22152. nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type type,
  22153. int count, float min_value, float max_value)
  22154. {
  22155. nk_chart_add_slot_colored(ctx, type, ctx->style.chart.color,
  22156. ctx->style.chart.selected_color, count, min_value, max_value);
  22157. }
  22158. NK_INTERN nk_flags
  22159. nk_chart_push_line(struct nk_context *ctx, struct nk_window *win,
  22160. struct nk_chart *g, float value, int slot)
  22161. {
  22162. struct nk_panel *layout = win->layout;
  22163. const struct nk_input *i = &ctx->input;
  22164. struct nk_command_buffer *out = &win->buffer;
  22165. nk_flags ret = 0;
  22166. struct nk_vec2 cur;
  22167. struct nk_rect bounds;
  22168. struct nk_color color;
  22169. float step;
  22170. float range;
  22171. float ratio;
  22172. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  22173. step = g->w / (float)g->slots[slot].count;
  22174. range = g->slots[slot].max - g->slots[slot].min;
  22175. ratio = (value - g->slots[slot].min) / range;
  22176. if (g->slots[slot].index == 0) {
  22177. /* first data point does not have a connection */
  22178. g->slots[slot].last.x = g->x;
  22179. g->slots[slot].last.y = (g->y + g->h) - ratio * (float)g->h;
  22180. bounds.x = g->slots[slot].last.x - 2;
  22181. bounds.y = g->slots[slot].last.y - 2;
  22182. bounds.w = bounds.h = 4;
  22183. color = g->slots[slot].color;
  22184. if (!(layout->flags & NK_WINDOW_ROM) &&
  22185. NK_INBOX(i->mouse.pos.x,i->mouse.pos.y, g->slots[slot].last.x-3, g->slots[slot].last.y-3, 6, 6)){
  22186. ret = nk_input_is_mouse_hovering_rect(i, bounds) ? NK_CHART_HOVERING : 0;
  22187. ret |= (i->mouse.buttons[NK_BUTTON_LEFT].down &&
  22188. i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
  22189. color = g->slots[slot].highlight;
  22190. }
  22191. nk_fill_rect(out, bounds, 0, color);
  22192. g->slots[slot].index += 1;
  22193. return ret;
  22194. }
  22195. /* draw a line between the last data point and the new one */
  22196. color = g->slots[slot].color;
  22197. cur.x = g->x + (float)(step * (float)g->slots[slot].index);
  22198. cur.y = (g->y + g->h) - (ratio * (float)g->h);
  22199. nk_stroke_line(out, g->slots[slot].last.x, g->slots[slot].last.y, cur.x, cur.y, 1.0f, color);
  22200. bounds.x = cur.x - 3;
  22201. bounds.y = cur.y - 3;
  22202. bounds.w = bounds.h = 6;
  22203. /* user selection of current data point */
  22204. if (!(layout->flags & NK_WINDOW_ROM)) {
  22205. if (nk_input_is_mouse_hovering_rect(i, bounds)) {
  22206. ret = NK_CHART_HOVERING;
  22207. ret |= (!i->mouse.buttons[NK_BUTTON_LEFT].down &&
  22208. i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
  22209. color = g->slots[slot].highlight;
  22210. }
  22211. }
  22212. nk_fill_rect(out, nk_rect(cur.x - 2, cur.y - 2, 4, 4), 0, color);
  22213. /* save current data point position */
  22214. g->slots[slot].last.x = cur.x;
  22215. g->slots[slot].last.y = cur.y;
  22216. g->slots[slot].index += 1;
  22217. return ret;
  22218. }
  22219. NK_INTERN nk_flags
  22220. nk_chart_push_column(const struct nk_context *ctx, struct nk_window *win,
  22221. struct nk_chart *chart, float value, int slot)
  22222. {
  22223. struct nk_command_buffer *out = &win->buffer;
  22224. const struct nk_input *in = &ctx->input;
  22225. struct nk_panel *layout = win->layout;
  22226. float ratio;
  22227. nk_flags ret = 0;
  22228. struct nk_color color;
  22229. struct nk_rect item = {0,0,0,0};
  22230. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  22231. if (chart->slots[slot].index >= chart->slots[slot].count)
  22232. return nk_false;
  22233. if (chart->slots[slot].count) {
  22234. float padding = (float)(chart->slots[slot].count-1);
  22235. item.w = (chart->w - padding) / (float)(chart->slots[slot].count);
  22236. }
  22237. /* calculate bounds of current bar chart entry */
  22238. color = chart->slots[slot].color;;
  22239. item.h = chart->h * NK_ABS((value/chart->slots[slot].range));
  22240. if (value >= 0) {
  22241. ratio = (value + NK_ABS(chart->slots[slot].min)) / NK_ABS(chart->slots[slot].range);
  22242. item.y = (chart->y + chart->h) - chart->h * ratio;
  22243. } else {
  22244. ratio = (value - chart->slots[slot].max) / chart->slots[slot].range;
  22245. item.y = chart->y + (chart->h * NK_ABS(ratio)) - item.h;
  22246. }
  22247. item.x = chart->x + ((float)chart->slots[slot].index * item.w);
  22248. item.x = item.x + ((float)chart->slots[slot].index);
  22249. /* user chart bar selection */
  22250. if (!(layout->flags & NK_WINDOW_ROM) &&
  22251. NK_INBOX(in->mouse.pos.x,in->mouse.pos.y,item.x,item.y,item.w,item.h)) {
  22252. ret = NK_CHART_HOVERING;
  22253. ret |= (!in->mouse.buttons[NK_BUTTON_LEFT].down &&
  22254. in->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
  22255. color = chart->slots[slot].highlight;
  22256. }
  22257. nk_fill_rect(out, item, 0, color);
  22258. chart->slots[slot].index += 1;
  22259. return ret;
  22260. }
  22261. NK_API nk_flags
  22262. nk_chart_push_slot(struct nk_context *ctx, float value, int slot)
  22263. {
  22264. nk_flags flags;
  22265. struct nk_window *win;
  22266. NK_ASSERT(ctx);
  22267. NK_ASSERT(ctx->current);
  22268. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  22269. NK_ASSERT(slot < ctx->current->layout->chart.slot);
  22270. if (!ctx || !ctx->current || slot >= NK_CHART_MAX_SLOT) return nk_false;
  22271. if (slot >= ctx->current->layout->chart.slot) return nk_false;
  22272. win = ctx->current;
  22273. if (win->layout->chart.slot < slot) return nk_false;
  22274. switch (win->layout->chart.slots[slot].type) {
  22275. case NK_CHART_LINES:
  22276. flags = nk_chart_push_line(ctx, win, &win->layout->chart, value, slot); break;
  22277. case NK_CHART_COLUMN:
  22278. flags = nk_chart_push_column(ctx, win, &win->layout->chart, value, slot); break;
  22279. default:
  22280. case NK_CHART_MAX:
  22281. flags = 0;
  22282. }
  22283. return flags;
  22284. }
  22285. NK_API nk_flags
  22286. nk_chart_push(struct nk_context *ctx, float value)
  22287. {
  22288. return nk_chart_push_slot(ctx, value, 0);
  22289. }
  22290. NK_API void
  22291. nk_chart_end(struct nk_context *ctx)
  22292. {
  22293. struct nk_window *win;
  22294. struct nk_chart *chart;
  22295. NK_ASSERT(ctx);
  22296. NK_ASSERT(ctx->current);
  22297. if (!ctx || !ctx->current)
  22298. return;
  22299. win = ctx->current;
  22300. chart = &win->layout->chart;
  22301. NK_MEMSET(chart, 0, sizeof(*chart));
  22302. return;
  22303. }
  22304. NK_API void
  22305. nk_plot(struct nk_context *ctx, enum nk_chart_type type, const float *values,
  22306. int count, int offset)
  22307. {
  22308. int i = 0;
  22309. float min_value;
  22310. float max_value;
  22311. NK_ASSERT(ctx);
  22312. NK_ASSERT(values);
  22313. if (!ctx || !values || !count) return;
  22314. min_value = values[offset];
  22315. max_value = values[offset];
  22316. for (i = 0; i < count; ++i) {
  22317. min_value = NK_MIN(values[i + offset], min_value);
  22318. max_value = NK_MAX(values[i + offset], max_value);
  22319. }
  22320. if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
  22321. for (i = 0; i < count; ++i)
  22322. nk_chart_push(ctx, values[i + offset]);
  22323. nk_chart_end(ctx);
  22324. }
  22325. }
  22326. NK_API void
  22327. nk_plot_function(struct nk_context *ctx, enum nk_chart_type type, void *userdata,
  22328. float(*value_getter)(void* user, int index), int count, int offset)
  22329. {
  22330. int i = 0;
  22331. float min_value;
  22332. float max_value;
  22333. NK_ASSERT(ctx);
  22334. NK_ASSERT(value_getter);
  22335. if (!ctx || !value_getter || !count) return;
  22336. max_value = min_value = value_getter(userdata, offset);
  22337. for (i = 0; i < count; ++i) {
  22338. float value = value_getter(userdata, i + offset);
  22339. min_value = NK_MIN(value, min_value);
  22340. max_value = NK_MAX(value, max_value);
  22341. }
  22342. if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
  22343. for (i = 0; i < count; ++i)
  22344. nk_chart_push(ctx, value_getter(userdata, i + offset));
  22345. nk_chart_end(ctx);
  22346. }
  22347. }
  22348. /* ==============================================================
  22349. *
  22350. * COLOR PICKER
  22351. *
  22352. * ===============================================================*/
  22353. NK_LIB int
  22354. nk_color_picker_behavior(nk_flags *state,
  22355. const struct nk_rect *bounds, const struct nk_rect *matrix,
  22356. const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
  22357. struct nk_colorf *color, const struct nk_input *in)
  22358. {
  22359. float hsva[4];
  22360. int value_changed = 0;
  22361. int hsv_changed = 0;
  22362. NK_ASSERT(state);
  22363. NK_ASSERT(matrix);
  22364. NK_ASSERT(hue_bar);
  22365. NK_ASSERT(color);
  22366. /* color matrix */
  22367. nk_colorf_hsva_fv(hsva, *color);
  22368. if (nk_button_behavior(state, *matrix, in, NK_BUTTON_REPEATER)) {
  22369. hsva[1] = NK_SATURATE((in->mouse.pos.x - matrix->x) / (matrix->w-1));
  22370. hsva[2] = 1.0f - NK_SATURATE((in->mouse.pos.y - matrix->y) / (matrix->h-1));
  22371. value_changed = hsv_changed = 1;
  22372. }
  22373. /* hue bar */
  22374. if (nk_button_behavior(state, *hue_bar, in, NK_BUTTON_REPEATER)) {
  22375. hsva[0] = NK_SATURATE((in->mouse.pos.y - hue_bar->y) / (hue_bar->h-1));
  22376. value_changed = hsv_changed = 1;
  22377. }
  22378. /* alpha bar */
  22379. if (alpha_bar) {
  22380. if (nk_button_behavior(state, *alpha_bar, in, NK_BUTTON_REPEATER)) {
  22381. hsva[3] = 1.0f - NK_SATURATE((in->mouse.pos.y - alpha_bar->y) / (alpha_bar->h-1));
  22382. value_changed = 1;
  22383. }
  22384. }
  22385. nk_widget_state_reset(state);
  22386. if (hsv_changed) {
  22387. *color = nk_hsva_colorfv(hsva);
  22388. *state = NK_WIDGET_STATE_ACTIVE;
  22389. }
  22390. if (value_changed) {
  22391. color->a = hsva[3];
  22392. *state = NK_WIDGET_STATE_ACTIVE;
  22393. }
  22394. /* set color picker widget state */
  22395. if (nk_input_is_mouse_hovering_rect(in, *bounds))
  22396. *state = NK_WIDGET_STATE_HOVERED;
  22397. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *bounds))
  22398. *state |= NK_WIDGET_STATE_ENTERED;
  22399. else if (nk_input_is_mouse_prev_hovering_rect(in, *bounds))
  22400. *state |= NK_WIDGET_STATE_LEFT;
  22401. return value_changed;
  22402. }
  22403. NK_LIB void
  22404. nk_draw_color_picker(struct nk_command_buffer *o, const struct nk_rect *matrix,
  22405. const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
  22406. struct nk_colorf col)
  22407. {
  22408. NK_STORAGE const struct nk_color black = {0,0,0,255};
  22409. NK_STORAGE const struct nk_color white = {255, 255, 255, 255};
  22410. NK_STORAGE const struct nk_color black_trans = {0,0,0,0};
  22411. const float crosshair_size = 7.0f;
  22412. struct nk_color temp;
  22413. float hsva[4];
  22414. float line_y;
  22415. int i;
  22416. NK_ASSERT(o);
  22417. NK_ASSERT(matrix);
  22418. NK_ASSERT(hue_bar);
  22419. /* draw hue bar */
  22420. nk_colorf_hsva_fv(hsva, col);
  22421. for (i = 0; i < 6; ++i) {
  22422. NK_GLOBAL const struct nk_color hue_colors[] = {
  22423. {255, 0, 0, 255}, {255,255,0,255}, {0,255,0,255}, {0, 255,255,255},
  22424. {0,0,255,255}, {255, 0, 255, 255}, {255, 0, 0, 255}
  22425. };
  22426. nk_fill_rect_multi_color(o,
  22427. nk_rect(hue_bar->x, hue_bar->y + (float)i * (hue_bar->h/6.0f) + 0.5f,
  22428. hue_bar->w, (hue_bar->h/6.0f) + 0.5f), hue_colors[i], hue_colors[i],
  22429. hue_colors[i+1], hue_colors[i+1]);
  22430. }
  22431. line_y = (float)(int)(hue_bar->y + hsva[0] * matrix->h + 0.5f);
  22432. nk_stroke_line(o, hue_bar->x-1, line_y, hue_bar->x + hue_bar->w + 2,
  22433. line_y, 1, nk_rgb(255,255,255));
  22434. /* draw alpha bar */
  22435. if (alpha_bar) {
  22436. float alpha = NK_SATURATE(col.a);
  22437. line_y = (float)(int)(alpha_bar->y + (1.0f - alpha) * matrix->h + 0.5f);
  22438. nk_fill_rect_multi_color(o, *alpha_bar, white, white, black, black);
  22439. nk_stroke_line(o, alpha_bar->x-1, line_y, alpha_bar->x + alpha_bar->w + 2,
  22440. line_y, 1, nk_rgb(255,255,255));
  22441. }
  22442. /* draw color matrix */
  22443. temp = nk_hsv_f(hsva[0], 1.0f, 1.0f);
  22444. nk_fill_rect_multi_color(o, *matrix, white, temp, temp, white);
  22445. nk_fill_rect_multi_color(o, *matrix, black_trans, black_trans, black, black);
  22446. /* draw cross-hair */
  22447. {struct nk_vec2 p; float S = hsva[1]; float V = hsva[2];
  22448. p.x = (float)(int)(matrix->x + S * matrix->w);
  22449. p.y = (float)(int)(matrix->y + (1.0f - V) * matrix->h);
  22450. nk_stroke_line(o, p.x - crosshair_size, p.y, p.x-2, p.y, 1.0f, white);
  22451. nk_stroke_line(o, p.x + crosshair_size + 1, p.y, p.x+3, p.y, 1.0f, white);
  22452. nk_stroke_line(o, p.x, p.y + crosshair_size + 1, p.x, p.y+3, 1.0f, white);
  22453. nk_stroke_line(o, p.x, p.y - crosshair_size, p.x, p.y-2, 1.0f, white);}
  22454. }
  22455. NK_LIB int
  22456. nk_do_color_picker(nk_flags *state,
  22457. struct nk_command_buffer *out, struct nk_colorf *col,
  22458. enum nk_color_format fmt, struct nk_rect bounds,
  22459. struct nk_vec2 padding, const struct nk_input *in,
  22460. const struct nk_user_font *font)
  22461. {
  22462. int ret = 0;
  22463. struct nk_rect matrix;
  22464. struct nk_rect hue_bar;
  22465. struct nk_rect alpha_bar;
  22466. float bar_w;
  22467. NK_ASSERT(out);
  22468. NK_ASSERT(col);
  22469. NK_ASSERT(state);
  22470. NK_ASSERT(font);
  22471. if (!out || !col || !state || !font)
  22472. return ret;
  22473. bar_w = font->height;
  22474. bounds.x += padding.x;
  22475. bounds.y += padding.x;
  22476. bounds.w -= 2 * padding.x;
  22477. bounds.h -= 2 * padding.y;
  22478. matrix.x = bounds.x;
  22479. matrix.y = bounds.y;
  22480. matrix.h = bounds.h;
  22481. matrix.w = bounds.w - (3 * padding.x + 2 * bar_w);
  22482. hue_bar.w = bar_w;
  22483. hue_bar.y = bounds.y;
  22484. hue_bar.h = matrix.h;
  22485. hue_bar.x = matrix.x + matrix.w + padding.x;
  22486. alpha_bar.x = hue_bar.x + hue_bar.w + padding.x;
  22487. alpha_bar.y = bounds.y;
  22488. alpha_bar.w = bar_w;
  22489. alpha_bar.h = matrix.h;
  22490. ret = nk_color_picker_behavior(state, &bounds, &matrix, &hue_bar,
  22491. (fmt == NK_RGBA) ? &alpha_bar:0, col, in);
  22492. nk_draw_color_picker(out, &matrix, &hue_bar, (fmt == NK_RGBA) ? &alpha_bar:0, *col);
  22493. return ret;
  22494. }
  22495. NK_API int
  22496. nk_color_pick(struct nk_context * ctx, struct nk_colorf *color,
  22497. enum nk_color_format fmt)
  22498. {
  22499. struct nk_window *win;
  22500. struct nk_panel *layout;
  22501. const struct nk_style *config;
  22502. const struct nk_input *in;
  22503. enum nk_widget_layout_states state;
  22504. struct nk_rect bounds;
  22505. NK_ASSERT(ctx);
  22506. NK_ASSERT(color);
  22507. NK_ASSERT(ctx->current);
  22508. NK_ASSERT(ctx->current->layout);
  22509. if (!ctx || !ctx->current || !ctx->current->layout || !color)
  22510. return 0;
  22511. win = ctx->current;
  22512. config = &ctx->style;
  22513. layout = win->layout;
  22514. state = nk_widget(&bounds, ctx);
  22515. if (!state) return 0;
  22516. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22517. return nk_do_color_picker(&ctx->last_widget_state, &win->buffer, color, fmt, bounds,
  22518. nk_vec2(0,0), in, config->font);
  22519. }
  22520. NK_API struct nk_colorf
  22521. nk_color_picker(struct nk_context *ctx, struct nk_colorf color,
  22522. enum nk_color_format fmt)
  22523. {
  22524. nk_color_pick(ctx, &color, fmt);
  22525. return color;
  22526. }
  22527. /* ==============================================================
  22528. *
  22529. * COMBO
  22530. *
  22531. * ===============================================================*/
  22532. NK_INTERN int
  22533. nk_combo_begin(struct nk_context *ctx, struct nk_window *win,
  22534. struct nk_vec2 size, int is_clicked, struct nk_rect header)
  22535. {
  22536. struct nk_window *popup;
  22537. int is_open = 0;
  22538. int is_active = 0;
  22539. struct nk_rect body;
  22540. nk_hash hash;
  22541. NK_ASSERT(ctx);
  22542. NK_ASSERT(ctx->current);
  22543. NK_ASSERT(ctx->current->layout);
  22544. if (!ctx || !ctx->current || !ctx->current->layout)
  22545. return 0;
  22546. popup = win->popup.win;
  22547. body.x = header.x;
  22548. body.w = size.x;
  22549. body.y = header.y + header.h-ctx->style.window.combo_border;
  22550. body.h = size.y;
  22551. hash = win->popup.combo_count++;
  22552. is_open = (popup) ? nk_true:nk_false;
  22553. is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_COMBO);
  22554. if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
  22555. (!is_open && !is_active && !is_clicked)) return 0;
  22556. if (!nk_nonblock_begin(ctx, 0, body,
  22557. (is_clicked && is_open)?nk_rect(0,0,0,0):header, NK_PANEL_COMBO)) return 0;
  22558. win->popup.type = NK_PANEL_COMBO;
  22559. win->popup.name = hash;
  22560. return 1;
  22561. }
  22562. NK_API int
  22563. nk_combo_begin_text(struct nk_context *ctx, const char *selected, int len,
  22564. struct nk_vec2 size)
  22565. {
  22566. const struct nk_input *in;
  22567. struct nk_window *win;
  22568. struct nk_style *style;
  22569. enum nk_widget_layout_states s;
  22570. int is_clicked = nk_false;
  22571. struct nk_rect header;
  22572. const struct nk_style_item *background;
  22573. struct nk_text text;
  22574. NK_ASSERT(ctx);
  22575. NK_ASSERT(selected);
  22576. NK_ASSERT(ctx->current);
  22577. NK_ASSERT(ctx->current->layout);
  22578. if (!ctx || !ctx->current || !ctx->current->layout || !selected)
  22579. return 0;
  22580. win = ctx->current;
  22581. style = &ctx->style;
  22582. s = nk_widget(&header, ctx);
  22583. if (s == NK_WIDGET_INVALID)
  22584. return 0;
  22585. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  22586. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  22587. is_clicked = nk_true;
  22588. /* draw combo box header background and border */
  22589. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  22590. background = &style->combo.active;
  22591. text.text = style->combo.label_active;
  22592. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  22593. background = &style->combo.hover;
  22594. text.text = style->combo.label_hover;
  22595. } else {
  22596. background = &style->combo.normal;
  22597. text.text = style->combo.label_normal;
  22598. }
  22599. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22600. text.background = nk_rgba(0,0,0,0);
  22601. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  22602. } else {
  22603. text.background = background->data.color;
  22604. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  22605. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  22606. }
  22607. {
  22608. /* print currently selected text item */
  22609. struct nk_rect label;
  22610. struct nk_rect button;
  22611. struct nk_rect content;
  22612. enum nk_symbol_type sym;
  22613. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22614. sym = style->combo.sym_hover;
  22615. else if (is_clicked)
  22616. sym = style->combo.sym_active;
  22617. else sym = style->combo.sym_normal;
  22618. /* calculate button */
  22619. button.w = header.h - 2 * style->combo.button_padding.y;
  22620. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  22621. button.y = header.y + style->combo.button_padding.y;
  22622. button.h = button.w;
  22623. content.x = button.x + style->combo.button.padding.x;
  22624. content.y = button.y + style->combo.button.padding.y;
  22625. content.w = button.w - 2 * style->combo.button.padding.x;
  22626. content.h = button.h - 2 * style->combo.button.padding.y;
  22627. /* draw selected label */
  22628. text.padding = nk_vec2(0,0);
  22629. label.x = header.x + style->combo.content_padding.x;
  22630. label.y = header.y + style->combo.content_padding.y;
  22631. label.w = button.x - (style->combo.content_padding.x + style->combo.spacing.x) - label.x;;
  22632. label.h = header.h - 2 * style->combo.content_padding.y;
  22633. nk_widget_text(&win->buffer, label, selected, len, &text,
  22634. NK_TEXT_LEFT, ctx->style.font);
  22635. /* draw open/close button */
  22636. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  22637. &ctx->style.combo.button, sym, style->font);
  22638. }
  22639. return nk_combo_begin(ctx, win, size, is_clicked, header);
  22640. }
  22641. NK_API int
  22642. nk_combo_begin_label(struct nk_context *ctx, const char *selected, struct nk_vec2 size)
  22643. {
  22644. return nk_combo_begin_text(ctx, selected, nk_strlen(selected), size);
  22645. }
  22646. NK_API int
  22647. nk_combo_begin_color(struct nk_context *ctx, struct nk_color color, struct nk_vec2 size)
  22648. {
  22649. struct nk_window *win;
  22650. struct nk_style *style;
  22651. const struct nk_input *in;
  22652. struct nk_rect header;
  22653. int is_clicked = nk_false;
  22654. enum nk_widget_layout_states s;
  22655. const struct nk_style_item *background;
  22656. NK_ASSERT(ctx);
  22657. NK_ASSERT(ctx->current);
  22658. NK_ASSERT(ctx->current->layout);
  22659. if (!ctx || !ctx->current || !ctx->current->layout)
  22660. return 0;
  22661. win = ctx->current;
  22662. style = &ctx->style;
  22663. s = nk_widget(&header, ctx);
  22664. if (s == NK_WIDGET_INVALID)
  22665. return 0;
  22666. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  22667. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  22668. is_clicked = nk_true;
  22669. /* draw combo box header background and border */
  22670. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
  22671. background = &style->combo.active;
  22672. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22673. background = &style->combo.hover;
  22674. else background = &style->combo.normal;
  22675. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22676. nk_draw_image(&win->buffer, header, &background->data.image,nk_white);
  22677. } else {
  22678. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  22679. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  22680. }
  22681. {
  22682. struct nk_rect content;
  22683. struct nk_rect button;
  22684. struct nk_rect bounds;
  22685. enum nk_symbol_type sym;
  22686. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22687. sym = style->combo.sym_hover;
  22688. else if (is_clicked)
  22689. sym = style->combo.sym_active;
  22690. else sym = style->combo.sym_normal;
  22691. /* calculate button */
  22692. button.w = header.h - 2 * style->combo.button_padding.y;
  22693. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  22694. button.y = header.y + style->combo.button_padding.y;
  22695. button.h = button.w;
  22696. content.x = button.x + style->combo.button.padding.x;
  22697. content.y = button.y + style->combo.button.padding.y;
  22698. content.w = button.w - 2 * style->combo.button.padding.x;
  22699. content.h = button.h - 2 * style->combo.button.padding.y;
  22700. /* draw color */
  22701. bounds.h = header.h - 4 * style->combo.content_padding.y;
  22702. bounds.y = header.y + 2 * style->combo.content_padding.y;
  22703. bounds.x = header.x + 2 * style->combo.content_padding.x;
  22704. bounds.w = (button.x - (style->combo.content_padding.x + style->combo.spacing.x)) - bounds.x;
  22705. nk_fill_rect(&win->buffer, bounds, 0, color);
  22706. /* draw open/close button */
  22707. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  22708. &ctx->style.combo.button, sym, style->font);
  22709. }
  22710. return nk_combo_begin(ctx, win, size, is_clicked, header);
  22711. }
  22712. NK_API int
  22713. nk_combo_begin_symbol(struct nk_context *ctx, enum nk_symbol_type symbol, struct nk_vec2 size)
  22714. {
  22715. struct nk_window *win;
  22716. struct nk_style *style;
  22717. const struct nk_input *in;
  22718. struct nk_rect header;
  22719. int is_clicked = nk_false;
  22720. enum nk_widget_layout_states s;
  22721. const struct nk_style_item *background;
  22722. struct nk_color sym_background;
  22723. struct nk_color symbol_color;
  22724. NK_ASSERT(ctx);
  22725. NK_ASSERT(ctx->current);
  22726. NK_ASSERT(ctx->current->layout);
  22727. if (!ctx || !ctx->current || !ctx->current->layout)
  22728. return 0;
  22729. win = ctx->current;
  22730. style = &ctx->style;
  22731. s = nk_widget(&header, ctx);
  22732. if (s == NK_WIDGET_INVALID)
  22733. return 0;
  22734. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  22735. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  22736. is_clicked = nk_true;
  22737. /* draw combo box header background and border */
  22738. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  22739. background = &style->combo.active;
  22740. symbol_color = style->combo.symbol_active;
  22741. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  22742. background = &style->combo.hover;
  22743. symbol_color = style->combo.symbol_hover;
  22744. } else {
  22745. background = &style->combo.normal;
  22746. symbol_color = style->combo.symbol_hover;
  22747. }
  22748. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22749. sym_background = nk_rgba(0,0,0,0);
  22750. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  22751. } else {
  22752. sym_background = background->data.color;
  22753. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  22754. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  22755. }
  22756. {
  22757. struct nk_rect bounds = {0,0,0,0};
  22758. struct nk_rect content;
  22759. struct nk_rect button;
  22760. enum nk_symbol_type sym;
  22761. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22762. sym = style->combo.sym_hover;
  22763. else if (is_clicked)
  22764. sym = style->combo.sym_active;
  22765. else sym = style->combo.sym_normal;
  22766. /* calculate button */
  22767. button.w = header.h - 2 * style->combo.button_padding.y;
  22768. button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
  22769. button.y = header.y + style->combo.button_padding.y;
  22770. button.h = button.w;
  22771. content.x = button.x + style->combo.button.padding.x;
  22772. content.y = button.y + style->combo.button.padding.y;
  22773. content.w = button.w - 2 * style->combo.button.padding.x;
  22774. content.h = button.h - 2 * style->combo.button.padding.y;
  22775. /* draw symbol */
  22776. bounds.h = header.h - 2 * style->combo.content_padding.y;
  22777. bounds.y = header.y + style->combo.content_padding.y;
  22778. bounds.x = header.x + style->combo.content_padding.x;
  22779. bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
  22780. nk_draw_symbol(&win->buffer, symbol, bounds, sym_background, symbol_color,
  22781. 1.0f, style->font);
  22782. /* draw open/close button */
  22783. nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
  22784. &ctx->style.combo.button, sym, style->font);
  22785. }
  22786. return nk_combo_begin(ctx, win, size, is_clicked, header);
  22787. }
  22788. NK_API int
  22789. nk_combo_begin_symbol_text(struct nk_context *ctx, const char *selected, int len,
  22790. enum nk_symbol_type symbol, struct nk_vec2 size)
  22791. {
  22792. struct nk_window *win;
  22793. struct nk_style *style;
  22794. struct nk_input *in;
  22795. struct nk_rect header;
  22796. int is_clicked = nk_false;
  22797. enum nk_widget_layout_states s;
  22798. const struct nk_style_item *background;
  22799. struct nk_color symbol_color;
  22800. struct nk_text text;
  22801. NK_ASSERT(ctx);
  22802. NK_ASSERT(ctx->current);
  22803. NK_ASSERT(ctx->current->layout);
  22804. if (!ctx || !ctx->current || !ctx->current->layout)
  22805. return 0;
  22806. win = ctx->current;
  22807. style = &ctx->style;
  22808. s = nk_widget(&header, ctx);
  22809. if (!s) return 0;
  22810. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  22811. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  22812. is_clicked = nk_true;
  22813. /* draw combo box header background and border */
  22814. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  22815. background = &style->combo.active;
  22816. symbol_color = style->combo.symbol_active;
  22817. text.text = style->combo.label_active;
  22818. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  22819. background = &style->combo.hover;
  22820. symbol_color = style->combo.symbol_hover;
  22821. text.text = style->combo.label_hover;
  22822. } else {
  22823. background = &style->combo.normal;
  22824. symbol_color = style->combo.symbol_normal;
  22825. text.text = style->combo.label_normal;
  22826. }
  22827. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22828. text.background = nk_rgba(0,0,0,0);
  22829. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  22830. } else {
  22831. text.background = background->data.color;
  22832. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  22833. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  22834. }
  22835. {
  22836. struct nk_rect content;
  22837. struct nk_rect button;
  22838. struct nk_rect label;
  22839. struct nk_rect image;
  22840. enum nk_symbol_type sym;
  22841. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22842. sym = style->combo.sym_hover;
  22843. else if (is_clicked)
  22844. sym = style->combo.sym_active;
  22845. else sym = style->combo.sym_normal;
  22846. /* calculate button */
  22847. button.w = header.h - 2 * style->combo.button_padding.y;
  22848. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  22849. button.y = header.y + style->combo.button_padding.y;
  22850. button.h = button.w;
  22851. content.x = button.x + style->combo.button.padding.x;
  22852. content.y = button.y + style->combo.button.padding.y;
  22853. content.w = button.w - 2 * style->combo.button.padding.x;
  22854. content.h = button.h - 2 * style->combo.button.padding.y;
  22855. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  22856. &ctx->style.combo.button, sym, style->font);
  22857. /* draw symbol */
  22858. image.x = header.x + style->combo.content_padding.x;
  22859. image.y = header.y + style->combo.content_padding.y;
  22860. image.h = header.h - 2 * style->combo.content_padding.y;
  22861. image.w = image.h;
  22862. nk_draw_symbol(&win->buffer, symbol, image, text.background, symbol_color,
  22863. 1.0f, style->font);
  22864. /* draw label */
  22865. text.padding = nk_vec2(0,0);
  22866. label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
  22867. label.y = header.y + style->combo.content_padding.y;
  22868. label.w = (button.x - style->combo.content_padding.x) - label.x;
  22869. label.h = header.h - 2 * style->combo.content_padding.y;
  22870. nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
  22871. }
  22872. return nk_combo_begin(ctx, win, size, is_clicked, header);
  22873. }
  22874. NK_API int
  22875. nk_combo_begin_image(struct nk_context *ctx, struct nk_image img, struct nk_vec2 size)
  22876. {
  22877. struct nk_window *win;
  22878. struct nk_style *style;
  22879. const struct nk_input *in;
  22880. struct nk_rect header;
  22881. int is_clicked = nk_false;
  22882. enum nk_widget_layout_states s;
  22883. const struct nk_style_item *background;
  22884. NK_ASSERT(ctx);
  22885. NK_ASSERT(ctx->current);
  22886. NK_ASSERT(ctx->current->layout);
  22887. if (!ctx || !ctx->current || !ctx->current->layout)
  22888. return 0;
  22889. win = ctx->current;
  22890. style = &ctx->style;
  22891. s = nk_widget(&header, ctx);
  22892. if (s == NK_WIDGET_INVALID)
  22893. return 0;
  22894. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  22895. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  22896. is_clicked = nk_true;
  22897. /* draw combo box header background and border */
  22898. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
  22899. background = &style->combo.active;
  22900. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22901. background = &style->combo.hover;
  22902. else background = &style->combo.normal;
  22903. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22904. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  22905. } else {
  22906. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  22907. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  22908. }
  22909. {
  22910. struct nk_rect bounds = {0,0,0,0};
  22911. struct nk_rect content;
  22912. struct nk_rect button;
  22913. enum nk_symbol_type sym;
  22914. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22915. sym = style->combo.sym_hover;
  22916. else if (is_clicked)
  22917. sym = style->combo.sym_active;
  22918. else sym = style->combo.sym_normal;
  22919. /* calculate button */
  22920. button.w = header.h - 2 * style->combo.button_padding.y;
  22921. button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
  22922. button.y = header.y + style->combo.button_padding.y;
  22923. button.h = button.w;
  22924. content.x = button.x + style->combo.button.padding.x;
  22925. content.y = button.y + style->combo.button.padding.y;
  22926. content.w = button.w - 2 * style->combo.button.padding.x;
  22927. content.h = button.h - 2 * style->combo.button.padding.y;
  22928. /* draw image */
  22929. bounds.h = header.h - 2 * style->combo.content_padding.y;
  22930. bounds.y = header.y + style->combo.content_padding.y;
  22931. bounds.x = header.x + style->combo.content_padding.x;
  22932. bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
  22933. nk_draw_image(&win->buffer, bounds, &img, nk_white);
  22934. /* draw open/close button */
  22935. nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
  22936. &ctx->style.combo.button, sym, style->font);
  22937. }
  22938. return nk_combo_begin(ctx, win, size, is_clicked, header);
  22939. }
  22940. NK_API int
  22941. nk_combo_begin_image_text(struct nk_context *ctx, const char *selected, int len,
  22942. struct nk_image img, struct nk_vec2 size)
  22943. {
  22944. struct nk_window *win;
  22945. struct nk_style *style;
  22946. struct nk_input *in;
  22947. struct nk_rect header;
  22948. int is_clicked = nk_false;
  22949. enum nk_widget_layout_states s;
  22950. const struct nk_style_item *background;
  22951. struct nk_text text;
  22952. NK_ASSERT(ctx);
  22953. NK_ASSERT(ctx->current);
  22954. NK_ASSERT(ctx->current->layout);
  22955. if (!ctx || !ctx->current || !ctx->current->layout)
  22956. return 0;
  22957. win = ctx->current;
  22958. style = &ctx->style;
  22959. s = nk_widget(&header, ctx);
  22960. if (!s) return 0;
  22961. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  22962. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  22963. is_clicked = nk_true;
  22964. /* draw combo box header background and border */
  22965. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  22966. background = &style->combo.active;
  22967. text.text = style->combo.label_active;
  22968. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  22969. background = &style->combo.hover;
  22970. text.text = style->combo.label_hover;
  22971. } else {
  22972. background = &style->combo.normal;
  22973. text.text = style->combo.label_normal;
  22974. }
  22975. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22976. text.background = nk_rgba(0,0,0,0);
  22977. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  22978. } else {
  22979. text.background = background->data.color;
  22980. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  22981. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  22982. }
  22983. {
  22984. struct nk_rect content;
  22985. struct nk_rect button;
  22986. struct nk_rect label;
  22987. struct nk_rect image;
  22988. enum nk_symbol_type sym;
  22989. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22990. sym = style->combo.sym_hover;
  22991. else if (is_clicked)
  22992. sym = style->combo.sym_active;
  22993. else sym = style->combo.sym_normal;
  22994. /* calculate button */
  22995. button.w = header.h - 2 * style->combo.button_padding.y;
  22996. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  22997. button.y = header.y + style->combo.button_padding.y;
  22998. button.h = button.w;
  22999. content.x = button.x + style->combo.button.padding.x;
  23000. content.y = button.y + style->combo.button.padding.y;
  23001. content.w = button.w - 2 * style->combo.button.padding.x;
  23002. content.h = button.h - 2 * style->combo.button.padding.y;
  23003. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  23004. &ctx->style.combo.button, sym, style->font);
  23005. /* draw image */
  23006. image.x = header.x + style->combo.content_padding.x;
  23007. image.y = header.y + style->combo.content_padding.y;
  23008. image.h = header.h - 2 * style->combo.content_padding.y;
  23009. image.w = image.h;
  23010. nk_draw_image(&win->buffer, image, &img, nk_white);
  23011. /* draw label */
  23012. text.padding = nk_vec2(0,0);
  23013. label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
  23014. label.y = header.y + style->combo.content_padding.y;
  23015. label.w = (button.x - style->combo.content_padding.x) - label.x;
  23016. label.h = header.h - 2 * style->combo.content_padding.y;
  23017. nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
  23018. }
  23019. return nk_combo_begin(ctx, win, size, is_clicked, header);
  23020. }
  23021. NK_API int
  23022. nk_combo_begin_symbol_label(struct nk_context *ctx,
  23023. const char *selected, enum nk_symbol_type type, struct nk_vec2 size)
  23024. {
  23025. return nk_combo_begin_symbol_text(ctx, selected, nk_strlen(selected), type, size);
  23026. }
  23027. NK_API int
  23028. nk_combo_begin_image_label(struct nk_context *ctx,
  23029. const char *selected, struct nk_image img, struct nk_vec2 size)
  23030. {
  23031. return nk_combo_begin_image_text(ctx, selected, nk_strlen(selected), img, size);
  23032. }
  23033. NK_API int
  23034. nk_combo_item_text(struct nk_context *ctx, const char *text, int len,nk_flags align)
  23035. {
  23036. return nk_contextual_item_text(ctx, text, len, align);
  23037. }
  23038. NK_API int
  23039. nk_combo_item_label(struct nk_context *ctx, const char *label, nk_flags align)
  23040. {
  23041. return nk_contextual_item_label(ctx, label, align);
  23042. }
  23043. NK_API int
  23044. nk_combo_item_image_text(struct nk_context *ctx, struct nk_image img, const char *text,
  23045. int len, nk_flags alignment)
  23046. {
  23047. return nk_contextual_item_image_text(ctx, img, text, len, alignment);
  23048. }
  23049. NK_API int
  23050. nk_combo_item_image_label(struct nk_context *ctx, struct nk_image img,
  23051. const char *text, nk_flags alignment)
  23052. {
  23053. return nk_contextual_item_image_label(ctx, img, text, alignment);
  23054. }
  23055. NK_API int
  23056. nk_combo_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  23057. const char *text, int len, nk_flags alignment)
  23058. {
  23059. return nk_contextual_item_symbol_text(ctx, sym, text, len, alignment);
  23060. }
  23061. NK_API int
  23062. nk_combo_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  23063. const char *label, nk_flags alignment)
  23064. {
  23065. return nk_contextual_item_symbol_label(ctx, sym, label, alignment);
  23066. }
  23067. NK_API void nk_combo_end(struct nk_context *ctx)
  23068. {
  23069. nk_contextual_end(ctx);
  23070. }
  23071. NK_API void nk_combo_close(struct nk_context *ctx)
  23072. {
  23073. nk_contextual_close(ctx);
  23074. }
  23075. NK_API int
  23076. nk_combo(struct nk_context *ctx, const char **items, int count,
  23077. int selected, int item_height, struct nk_vec2 size)
  23078. {
  23079. int i = 0;
  23080. int max_height;
  23081. struct nk_vec2 item_spacing;
  23082. struct nk_vec2 window_padding;
  23083. NK_ASSERT(ctx);
  23084. NK_ASSERT(items);
  23085. NK_ASSERT(ctx->current);
  23086. if (!ctx || !items ||!count)
  23087. return selected;
  23088. item_spacing = ctx->style.window.spacing;
  23089. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  23090. max_height = count * item_height + count * (int)item_spacing.y;
  23091. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  23092. size.y = NK_MIN(size.y, (float)max_height);
  23093. if (nk_combo_begin_label(ctx, items[selected], size)) {
  23094. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  23095. for (i = 0; i < count; ++i) {
  23096. if (nk_combo_item_label(ctx, items[i], NK_TEXT_LEFT))
  23097. selected = i;
  23098. }
  23099. nk_combo_end(ctx);
  23100. }
  23101. return selected;
  23102. }
  23103. NK_API int
  23104. nk_combo_separator(struct nk_context *ctx, const char *items_separated_by_separator,
  23105. int separator, int selected, int count, int item_height, struct nk_vec2 size)
  23106. {
  23107. int i;
  23108. int max_height;
  23109. struct nk_vec2 item_spacing;
  23110. struct nk_vec2 window_padding;
  23111. const char *current_item;
  23112. const char *iter;
  23113. int length = 0;
  23114. NK_ASSERT(ctx);
  23115. NK_ASSERT(items_separated_by_separator);
  23116. if (!ctx || !items_separated_by_separator)
  23117. return selected;
  23118. /* calculate popup window */
  23119. item_spacing = ctx->style.window.spacing;
  23120. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  23121. max_height = count * item_height + count * (int)item_spacing.y;
  23122. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  23123. size.y = NK_MIN(size.y, (float)max_height);
  23124. /* find selected item */
  23125. current_item = items_separated_by_separator;
  23126. for (i = 0; i < count; ++i) {
  23127. iter = current_item;
  23128. while (*iter && *iter != separator) iter++;
  23129. length = (int)(iter - current_item);
  23130. if (i == selected) break;
  23131. current_item = iter + 1;
  23132. }
  23133. if (nk_combo_begin_text(ctx, current_item, length, size)) {
  23134. current_item = items_separated_by_separator;
  23135. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  23136. for (i = 0; i < count; ++i) {
  23137. iter = current_item;
  23138. while (*iter && *iter != separator) iter++;
  23139. length = (int)(iter - current_item);
  23140. if (nk_combo_item_text(ctx, current_item, length, NK_TEXT_LEFT))
  23141. selected = i;
  23142. current_item = current_item + length + 1;
  23143. }
  23144. nk_combo_end(ctx);
  23145. }
  23146. return selected;
  23147. }
  23148. NK_API int
  23149. nk_combo_string(struct nk_context *ctx, const char *items_separated_by_zeros,
  23150. int selected, int count, int item_height, struct nk_vec2 size)
  23151. {
  23152. return nk_combo_separator(ctx, items_separated_by_zeros, '\0', selected, count, item_height, size);
  23153. }
  23154. NK_API int
  23155. nk_combo_callback(struct nk_context *ctx, void(*item_getter)(void*, int, const char**),
  23156. void *userdata, int selected, int count, int item_height, struct nk_vec2 size)
  23157. {
  23158. int i;
  23159. int max_height;
  23160. struct nk_vec2 item_spacing;
  23161. struct nk_vec2 window_padding;
  23162. const char *item;
  23163. NK_ASSERT(ctx);
  23164. NK_ASSERT(item_getter);
  23165. if (!ctx || !item_getter)
  23166. return selected;
  23167. /* calculate popup window */
  23168. item_spacing = ctx->style.window.spacing;
  23169. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  23170. max_height = count * item_height + count * (int)item_spacing.y;
  23171. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  23172. size.y = NK_MIN(size.y, (float)max_height);
  23173. item_getter(userdata, selected, &item);
  23174. if (nk_combo_begin_label(ctx, item, size)) {
  23175. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  23176. for (i = 0; i < count; ++i) {
  23177. item_getter(userdata, i, &item);
  23178. if (nk_combo_item_label(ctx, item, NK_TEXT_LEFT))
  23179. selected = i;
  23180. }
  23181. nk_combo_end(ctx);
  23182. } return selected;
  23183. }
  23184. NK_API void
  23185. nk_combobox(struct nk_context *ctx, const char **items, int count,
  23186. int *selected, int item_height, struct nk_vec2 size)
  23187. {
  23188. *selected = nk_combo(ctx, items, count, *selected, item_height, size);
  23189. }
  23190. NK_API void
  23191. nk_combobox_string(struct nk_context *ctx, const char *items_separated_by_zeros,
  23192. int *selected, int count, int item_height, struct nk_vec2 size)
  23193. {
  23194. *selected = nk_combo_string(ctx, items_separated_by_zeros, *selected, count, item_height, size);
  23195. }
  23196. NK_API void
  23197. nk_combobox_separator(struct nk_context *ctx, const char *items_separated_by_separator,
  23198. int separator,int *selected, int count, int item_height, struct nk_vec2 size)
  23199. {
  23200. *selected = nk_combo_separator(ctx, items_separated_by_separator, separator,
  23201. *selected, count, item_height, size);
  23202. }
  23203. NK_API void
  23204. nk_combobox_callback(struct nk_context *ctx,
  23205. void(*item_getter)(void* data, int id, const char **out_text),
  23206. void *userdata, int *selected, int count, int item_height, struct nk_vec2 size)
  23207. {
  23208. *selected = nk_combo_callback(ctx, item_getter, userdata, *selected, count, item_height, size);
  23209. }
  23210. /* ===============================================================
  23211. *
  23212. * TOOLTIP
  23213. *
  23214. * ===============================================================*/
  23215. NK_API int
  23216. nk_tooltip_begin(struct nk_context *ctx, float width)
  23217. {
  23218. int x,y,w,h;
  23219. struct nk_window *win;
  23220. const struct nk_input *in;
  23221. struct nk_rect bounds;
  23222. int ret;
  23223. NK_ASSERT(ctx);
  23224. NK_ASSERT(ctx->current);
  23225. NK_ASSERT(ctx->current->layout);
  23226. if (!ctx || !ctx->current || !ctx->current->layout)
  23227. return 0;
  23228. /* make sure that no nonblocking popup is currently active */
  23229. win = ctx->current;
  23230. in = &ctx->input;
  23231. if (win->popup.win && (win->popup.type & NK_PANEL_SET_NONBLOCK))
  23232. return 0;
  23233. w = nk_iceilf(width);
  23234. h = nk_iceilf(nk_null_rect.h);
  23235. x = nk_ifloorf(in->mouse.pos.x + 1) - (int)win->layout->clip.x;
  23236. y = nk_ifloorf(in->mouse.pos.y + 1) - (int)win->layout->clip.y;
  23237. bounds.x = (float)x;
  23238. bounds.y = (float)y;
  23239. bounds.w = (float)w;
  23240. bounds.h = (float)h;
  23241. ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
  23242. "__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER, bounds);
  23243. if (ret) win->layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
  23244. win->popup.type = NK_PANEL_TOOLTIP;
  23245. ctx->current->layout->type = NK_PANEL_TOOLTIP;
  23246. return ret;
  23247. }
  23248. NK_API void
  23249. nk_tooltip_end(struct nk_context *ctx)
  23250. {
  23251. NK_ASSERT(ctx);
  23252. NK_ASSERT(ctx->current);
  23253. if (!ctx || !ctx->current) return;
  23254. ctx->current->seq--;
  23255. nk_popup_close(ctx);
  23256. nk_popup_end(ctx);
  23257. }
  23258. NK_API void
  23259. nk_tooltip(struct nk_context *ctx, const char *text)
  23260. {
  23261. const struct nk_style *style;
  23262. struct nk_vec2 padding;
  23263. int text_len;
  23264. float text_width;
  23265. float text_height;
  23266. NK_ASSERT(ctx);
  23267. NK_ASSERT(ctx->current);
  23268. NK_ASSERT(ctx->current->layout);
  23269. NK_ASSERT(text);
  23270. if (!ctx || !ctx->current || !ctx->current->layout || !text)
  23271. return;
  23272. /* fetch configuration data */
  23273. style = &ctx->style;
  23274. padding = style->window.padding;
  23275. /* calculate size of the text and tooltip */
  23276. text_len = nk_strlen(text);
  23277. text_width = style->font->width(style->font->userdata,
  23278. style->font->height, text, text_len);
  23279. text_width += (4 * padding.x);
  23280. text_height = (style->font->height + 2 * padding.y);
  23281. /* execute tooltip and fill with text */
  23282. if (nk_tooltip_begin(ctx, (float)text_width)) {
  23283. nk_layout_row_dynamic(ctx, (float)text_height, 1);
  23284. nk_text(ctx, text, text_len, NK_TEXT_LEFT);
  23285. nk_tooltip_end(ctx);
  23286. }
  23287. }
  23288. #ifdef NK_INCLUDE_STANDARD_VARARGS
  23289. NK_API void
  23290. nk_tooltipf(struct nk_context *ctx, const char *fmt, ...)
  23291. {
  23292. va_list args;
  23293. va_start(args, fmt);
  23294. nk_tooltipfv(ctx, fmt, args);
  23295. va_end(args);
  23296. }
  23297. NK_API void
  23298. nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
  23299. {
  23300. char buf[256];
  23301. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  23302. nk_tooltip(ctx, buf);
  23303. }
  23304. #endif
  23305. #endif /* NK_IMPLEMENTATION */
  23306. /*
  23307. /// ## License
  23308. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~none
  23309. /// ------------------------------------------------------------------------------
  23310. /// This software is available under 2 licenses -- choose whichever you prefer.
  23311. /// ------------------------------------------------------------------------------
  23312. /// ALTERNATIVE A - MIT License
  23313. /// Copyright (c) 2016-2018 Micha Mettke
  23314. /// Permission is hereby granted, free of charge, to any person obtaining a copy of
  23315. /// this software and associated documentation files (the "Software"), to deal in
  23316. /// the Software without restriction, including without limitation the rights to
  23317. /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  23318. /// of the Software, and to permit persons to whom the Software is furnished to do
  23319. /// so, subject to the following conditions:
  23320. /// The above copyright notice and this permission notice shall be included in all
  23321. /// copies or substantial portions of the Software.
  23322. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23323. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23324. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23325. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23326. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23327. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23328. /// SOFTWARE.
  23329. /// ------------------------------------------------------------------------------
  23330. /// ALTERNATIVE B - Public Domain (www.unlicense.org)
  23331. /// This is free and unencumbered software released into the public domain.
  23332. /// Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  23333. /// software, either in source code form or as a compiled binary, for any purpose,
  23334. /// commercial or non-commercial, and by any means.
  23335. /// In jurisdictions that recognize copyright laws, the author or authors of this
  23336. /// software dedicate any and all copyright interest in the software to the public
  23337. /// domain. We make this dedication for the benefit of the public at large and to
  23338. /// the detriment of our heirs and successors. We intend this dedication to be an
  23339. /// overt act of relinquishment in perpetuity of all present and future rights to
  23340. /// this software under copyright law.
  23341. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23342. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23343. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23344. /// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  23345. /// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23346. /// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23347. /// ------------------------------------------------------------------------------
  23348. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23349. /// ## Changelog
  23350. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~none
  23351. /// [date][x.yy.zz]-[description]
  23352. /// -[date]: date on which the change has been pushed
  23353. /// -[x.yy.zz]: Numerical version string representation. Each version number on the right
  23354. /// resets back to zero if version on the left is incremented.
  23355. /// - [x]: Major version with API and library breaking changes
  23356. /// - [yy]: Minor version with non-breaking API and library changes
  23357. /// - [zz]: Bug fix version with no direct changes to API
  23358. ///
  23359. /// - 2018/04/01 (4.00.1) - Fixed calling `nk_convert` multiple time per single frame
  23360. /// - 2018/04/01 (4.00.0) - BREAKING CHANGE: nk_draw_list_clear no longer tries to
  23361. /// clear provided buffers. So make sure to either free
  23362. /// or clear each passed buffer after calling nk_convert.
  23363. /// - 2018/02/23 (3.00.6) - Fixed slider dragging behavior
  23364. /// - 2018/01/31 (3.00.5) - Fixed overcalculation of cursor data in font baking process
  23365. /// - 2018/01/31 (3.00.4) - Removed name collision with stb_truetype
  23366. /// - 2018/01/28 (3.00.3) - Fixed panel window border drawing bug
  23367. /// - 2018/01/12 (3.00.2) - Added `nk_group_begin_titled` for separed group identifier and title
  23368. /// - 2018/01/07 (3.00.1) - Started to change documentation style
  23369. /// - 2018/01/05 (3.00.0) - BREAKING CHANGE: The previous color picker API was broken
  23370. /// because of conversions between float and byte color representation.
  23371. /// Color pickers now use floating point values to represent
  23372. /// HSV values. To get back the old behavior I added some additional
  23373. /// color conversion functions to cast between nk_color and
  23374. /// nk_colorf.
  23375. /// - 2017/12/23 (2.00.7) - Fixed small warning
  23376. /// - 2017/12/23 (2.00.7) - Fixed nk_edit_buffer behavior if activated to allow input
  23377. /// - 2017/12/23 (2.00.7) - Fixed modifyable progressbar dragging visuals and input behavior
  23378. /// - 2017/12/04 (2.00.6) - Added formated string tooltip widget
  23379. /// - 2017/11/18 (2.00.5) - Fixed window becoming hidden with flag NK_WINDOW_NO_INPUT
  23380. /// - 2017/11/15 (2.00.4) - Fixed font merging
  23381. /// - 2017/11/07 (2.00.3) - Fixed window size and position modifier functions
  23382. /// - 2017/09/14 (2.00.2) - Fixed nk_edit_buffer and nk_edit_focus behavior
  23383. /// - 2017/09/14 (2.00.1) - Fixed window closing behavior
  23384. /// - 2017/09/14 (2.00.0) - BREAKING CHANGE: Modifing window position and size funtions now
  23385. /// require the name of the window and must happen outside the window
  23386. /// building process (between function call nk_begin and nk_end).
  23387. /// - 2017/09/11 (1.40.9) - Fixed window background flag if background window is declared last
  23388. /// - 2017/08/27 (1.40.8) - Fixed `nk_item_is_any_active` for hidden windows
  23389. /// - 2017/08/27 (1.40.7) - Fixed window background flag
  23390. /// - 2017/07/07 (1.40.6) - Fixed missing clipping rect check for hovering/clicked
  23391. /// query for widgets
  23392. /// - 2017/07/07 (1.40.5) - Fixed drawing bug for vertex output for lines and stroked
  23393. /// and filled rectangles
  23394. /// - 2017/07/07 (1.40.4) - Fixed bug in nk_convert trying to add windows that are in
  23395. /// process of being destroyed.
  23396. /// - 2017/07/07 (1.40.3) - Fixed table internal bug caused by storing table size in
  23397. /// window instead of directly in table.
  23398. /// - 2017/06/30 (1.40.2) - Removed unneeded semicolon in C++ NK_ALIGNOF macro
  23399. /// - 2017/06/30 (1.40.1) - Fixed drawing lines smaller or equal zero
  23400. /// - 2017/06/08 (1.40.0) - Removed the breaking part of last commit. Auto layout now only
  23401. /// comes in effect if you pass in zero was row height argument
  23402. /// - 2017/06/08 (1.40.0) - BREAKING CHANGE: while not directly API breaking it will change
  23403. /// how layouting works. From now there will be an internal minimum
  23404. /// row height derived from font height. If you need a row smaller than
  23405. /// that you can directly set it by `nk_layout_set_min_row_height` and
  23406. /// reset the value back by calling `nk_layout_reset_min_row_height.
  23407. /// - 2017/06/08 (1.39.1) - Fixed property text edit handling bug caused by past `nk_widget` fix
  23408. /// - 2017/06/08 (1.39.0) - Added function to retrieve window space without calling a nk_layout_xxx function
  23409. /// - 2017/06/06 (1.38.5) - Fixed `nk_convert` return flag for command buffer
  23410. /// - 2017/05/23 (1.38.4) - Fixed activation behavior for widgets partially clipped
  23411. /// - 2017/05/10 (1.38.3) - Fixed wrong min window size mouse scaling over boundries
  23412. /// - 2017/05/09 (1.38.2) - Fixed vertical scrollbar drawing with not enough space
  23413. /// - 2017/05/09 (1.38.1) - Fixed scaler dragging behavior if window size hits minimum size
  23414. /// - 2017/05/06 (1.38.0) - Added platform double-click support
  23415. /// - 2017/04/20 (1.37.1) - Fixed key repeat found inside glfw demo backends
  23416. /// - 2017/04/20 (1.37.0) - Extended properties with selection and clipbard support
  23417. /// - 2017/04/20 (1.36.2) - Fixed #405 overlapping rows with zero padding and spacing
  23418. /// - 2017/04/09 (1.36.1) - Fixed #403 with another widget float error
  23419. /// - 2017/04/09 (1.36.0) - Added window `NK_WINDOW_NO_INPUT` and `NK_WINDOW_NOT_INTERACTIVE` flags
  23420. /// - 2017/04/09 (1.35.3) - Fixed buffer heap corruption
  23421. /// - 2017/03/25 (1.35.2) - Fixed popup overlapping for `NK_WINDOW_BACKGROUND` windows
  23422. /// - 2017/03/25 (1.35.1) - Fixed windows closing behavior
  23423. /// - 2017/03/18 (1.35.0) - Added horizontal scroll requested in #377
  23424. /// - 2017/03/18 (1.34.3) - Fixed long window header titles
  23425. /// - 2017/03/04 (1.34.2) - Fixed text edit filtering
  23426. /// - 2017/03/04 (1.34.1) - Fixed group closable flag
  23427. /// - 2017/02/25 (1.34.0) - Added custom draw command for better language binding support
  23428. /// - 2017/01/24 (1.33.0) - Added programatic way of remove edit focus
  23429. /// - 2017/01/24 (1.32.3) - Fixed wrong define for basic type definitions for windows
  23430. /// - 2017/01/21 (1.32.2) - Fixed input capture from hidden or closed windows
  23431. /// - 2017/01/21 (1.32.1) - Fixed slider behavior and drawing
  23432. /// - 2017/01/13 (1.32.0) - Added flag to put scaler into the bottom left corner
  23433. /// - 2017/01/13 (1.31.0) - Added additional row layouting method to combine both
  23434. /// dynamic and static widgets.
  23435. /// - 2016/12/31 (1.30.0) - Extended scrollbar offset from 16-bit to 32-bit
  23436. /// - 2016/12/31 (1.29.2)- Fixed closing window bug of minimized windows
  23437. /// - 2016/12/03 (1.29.1)- Fixed wrapped text with no seperator and C89 error
  23438. /// - 2016/12/03 (1.29.0) - Changed text wrapping to process words not characters
  23439. /// - 2016/11/22 (1.28.6)- Fixed window minimized closing bug
  23440. /// - 2016/11/19 (1.28.5)- Fixed abstract combo box closing behavior
  23441. /// - 2016/11/19 (1.28.4)- Fixed tooltip flickering
  23442. /// - 2016/11/19 (1.28.3)- Fixed memory leak caused by popup repeated closing
  23443. /// - 2016/11/18 (1.28.2)- Fixed memory leak caused by popup panel allocation
  23444. /// - 2016/11/10 (1.28.1)- Fixed some warnings and C++ error
  23445. /// - 2016/11/10 (1.28.0)- Added additional `nk_button` versions which allows to directly
  23446. /// pass in a style struct to change buttons visual.
  23447. /// - 2016/11/10 (1.27.0)- Added additional 'nk_tree' versions to support external state
  23448. /// storage. Just like last the `nk_group` commit the main
  23449. /// advantage is that you optionally can minimize nuklears runtime
  23450. /// memory consumption or handle hash collisions.
  23451. /// - 2016/11/09 (1.26.0)- Added additional `nk_group` version to support external scrollbar
  23452. /// offset storage. Main advantage is that you can externalize
  23453. /// the memory management for the offset. It could also be helpful
  23454. /// if you have a hash collision in `nk_group_begin` but really
  23455. /// want the name. In addition I added `nk_list_view` which allows
  23456. /// to draw big lists inside a group without actually having to
  23457. /// commit the whole list to nuklear (issue #269).
  23458. /// - 2016/10/30 (1.25.1)- Fixed clipping rectangle bug inside `nk_draw_list`
  23459. /// - 2016/10/29 (1.25.0)- Pulled `nk_panel` memory management into nuklear and out of
  23460. /// the hands of the user. From now on users don't have to care
  23461. /// about panels unless they care about some information. If you
  23462. /// still need the panel just call `nk_window_get_panel`.
  23463. /// - 2016/10/21 (1.24.0)- Changed widget border drawing to stroked rectangle from filled
  23464. /// rectangle for less overdraw and widget background transparency.
  23465. /// - 2016/10/18 (1.23.0)- Added `nk_edit_focus` for manually edit widget focus control
  23466. /// - 2016/09/29 (1.22.7)- Fixed deduction of basic type in non `<stdint.h>` compilation
  23467. /// - 2016/09/29 (1.22.6)- Fixed edit widget UTF-8 text cursor drawing bug
  23468. /// - 2016/09/28 (1.22.5)- Fixed edit widget UTF-8 text appending/inserting/removing
  23469. /// - 2016/09/28 (1.22.4)- Fixed drawing bug inside edit widgets which offset all text
  23470. /// text in every edit widget if one of them is scrolled.
  23471. /// - 2016/09/28 (1.22.3)- Fixed small bug in edit widgets if not active. The wrong
  23472. /// text length is passed. It should have been in bytes but
  23473. /// was passed as glyphes.
  23474. /// - 2016/09/20 (1.22.2)- Fixed color button size calculation
  23475. /// - 2016/09/20 (1.22.1)- Fixed some `nk_vsnprintf` behavior bugs and removed
  23476. /// `<stdio.h>` again from `NK_INCLUDE_STANDARD_VARARGS`.
  23477. /// - 2016/09/18 (1.22.0)- C89 does not support vsnprintf only C99 and newer as well
  23478. /// as C++11 and newer. In addition to use vsnprintf you have
  23479. /// to include <stdio.h>. So just defining `NK_INCLUDE_STD_VAR_ARGS`
  23480. /// is not enough. That behavior is now fixed. By default if
  23481. /// both varargs as well as stdio is selected I try to use
  23482. /// vsnprintf if not possible I will revert to vsprintf. If
  23483. /// varargs but not stdio was defined I will use my own function.
  23484. /// - 2016/09/15 (1.21.2)- Fixed panel `close` behavior for deeper panel levels
  23485. /// - 2016/09/15 (1.21.1)- Fixed C++ errors and wrong argument to `nk_panel_get_xxxx`
  23486. /// - 2016/09/13 (1.21.0) - !BREAKING! Fixed nonblocking popup behavior in menu, combo,
  23487. /// and contextual which prevented closing in y-direction if
  23488. /// popup did not reach max height.
  23489. /// In addition the height parameter was changed into vec2
  23490. /// for width and height to have more control over the popup size.
  23491. /// - 2016/09/13 (1.20.3) - Cleaned up and extended type selection
  23492. /// - 2016/09/13 (1.20.2)- Fixed slider behavior hopefully for the last time. This time
  23493. /// all calculation are correct so no more hackery.
  23494. /// - 2016/09/13 (1.20.1)- Internal change to divide window/panel flags into panel flags and types.
  23495. /// Suprisinly spend years in C and still happened to confuse types
  23496. /// with flags. Probably something to take note.
  23497. /// - 2016/09/08 (1.20.0)- Added additional helper function to make it easier to just
  23498. /// take the produced buffers from `nk_convert` and unplug the
  23499. /// iteration process from `nk_context`. So now you can
  23500. /// just use the vertex,element and command buffer + two pointer
  23501. /// inside the command buffer retrieved by calls `nk__draw_begin`
  23502. /// and `nk__draw_end` and macro `nk_draw_foreach_bounded`.
  23503. /// - 2016/09/08 (1.19.0)- Added additional asserts to make sure every `nk_xxx_begin` call
  23504. /// for windows, popups, combobox, menu and contextual is guarded by
  23505. /// `if` condition and does not produce false drawing output.
  23506. /// - 2016/09/08 (1.18.0)- Changed confusing name for `NK_SYMBOL_RECT_FILLED`, `NK_SYMBOL_RECT`
  23507. /// to hopefully easier to understand `NK_SYMBOL_RECT_FILLED` and
  23508. /// `NK_SYMBOL_RECT_OUTLINE`.
  23509. /// - 2016/09/08 (1.17.0)- Changed confusing name for `NK_SYMBOL_CIRLCE_FILLED`, `NK_SYMBOL_CIRCLE`
  23510. /// to hopefully easier to understand `NK_SYMBOL_CIRCLE_FILLED` and
  23511. /// `NK_SYMBOL_CIRCLE_OUTLINE`.
  23512. /// - 2016/09/08 (1.16.0)- Added additional checks to select correct types if `NK_INCLUDE_FIXED_TYPES`
  23513. /// is not defined by supporting the biggest compiler GCC, clang and MSVC.
  23514. /// - 2016/09/07 (1.15.3)- Fixed `NK_INCLUDE_COMMAND_USERDATA` define to not cause an error
  23515. /// - 2016/09/04 (1.15.2)- Fixed wrong combobox height calculation
  23516. /// - 2016/09/03 (1.15.1)- Fixed gaps inside combo boxes in OpenGL
  23517. /// - 2016/09/02 (1.15.0) - Changed nuklear to not have any default vertex layout and
  23518. /// instead made it user provided. The range of types to convert
  23519. /// to is quite limited at the moment, but I would be more than
  23520. /// happy to accept PRs to add additional.
  23521. /// - 2016/08/30 (1.14.2) - Removed unused variables
  23522. /// - 2016/08/30 (1.14.1) - Fixed C++ build errors
  23523. /// - 2016/08/30 (1.14.0) - Removed mouse dragging from SDL demo since it does not work correctly
  23524. /// - 2016/08/30 (1.13.4) - Tweaked some default styling variables
  23525. /// - 2016/08/30 (1.13.3) - Hopefully fixed drawing bug in slider, in general I would
  23526. /// refrain from using slider with a big number of steps.
  23527. /// - 2016/08/30 (1.13.2) - Fixed close and minimize button which would fire even if the
  23528. /// window was in Read Only Mode.
  23529. /// - 2016/08/30 (1.13.1) - Fixed popup panel padding handling which was previously just
  23530. /// a hack for combo box and menu.
  23531. /// - 2016/08/30 (1.13.0) - Removed `NK_WINDOW_DYNAMIC` flag from public API since
  23532. /// it is bugged and causes issues in window selection.
  23533. /// - 2016/08/30 (1.12.0) - Removed scaler size. The size of the scaler is now
  23534. /// determined by the scrollbar size
  23535. /// - 2016/08/30 (1.11.2) - Fixed some drawing bugs caused by changes from 1.11
  23536. /// - 2016/08/30 (1.11.1) - Fixed overlapping minimized window selection
  23537. /// - 2016/08/30 (1.11.0) - Removed some internal complexity and overly complex code
  23538. /// handling panel padding and panel border.
  23539. /// - 2016/08/29 (1.10.0) - Added additional height parameter to `nk_combobox_xxx`
  23540. /// - 2016/08/29 (1.10.0) - Fixed drawing bug in dynamic popups
  23541. /// - 2016/08/29 (1.10.0) - Added experimental mouse scrolling to popups, menus and comboboxes
  23542. /// - 2016/08/26 (1.10.0) - Added window name string prepresentation to account for
  23543. /// hash collisions. Currently limited to NK_WINDOW_MAX_NAME
  23544. /// which in term can be redefined if not big enough.
  23545. /// - 2016/08/26 (1.10.0) - Added stacks for temporary style/UI changes in code
  23546. /// - 2016/08/25 (1.10.0) - Changed `nk_input_is_key_pressed` and 'nk_input_is_key_released'
  23547. /// to account for key press and release happening in one frame.
  23548. /// - 2016/08/25 (1.10.0) - Added additional nk_edit flag to directly jump to the end on activate
  23549. /// - 2016/08/17 (1.09.6)- Removed invalid check for value zero in nk_propertyx
  23550. /// - 2016/08/16 (1.09.5)- Fixed ROM mode for deeper levels of popup windows parents.
  23551. /// - 2016/08/15 (1.09.4)- Editbox are now still active if enter was pressed with flag
  23552. /// `NK_EDIT_SIG_ENTER`. Main reasoning is to be able to keep
  23553. /// typing after commiting.
  23554. /// - 2016/08/15 (1.09.4)- Removed redundant code
  23555. /// - 2016/08/15 (1.09.4)- Fixed negative numbers in `nk_strtoi` and remove unused variable
  23556. /// - 2016/08/15 (1.09.3)- Fixed `NK_WINDOW_BACKGROUND` flag behavior to select a background
  23557. /// window only as selected by hovering and not by clicking.
  23558. /// - 2016/08/14 (1.09.2)- Fixed a bug in font atlas which caused wrong loading
  23559. /// of glyphes for font with multiple ranges.
  23560. /// - 2016/08/12 (1.09.1)- Added additional function to check if window is currently
  23561. /// hidden and therefore not visible.
  23562. /// - 2016/08/12 (1.09.1)- nk_window_is_closed now queries the correct flag `NK_WINDOW_CLOSED`
  23563. /// instead of the old flag `NK_WINDOW_HIDDEN`
  23564. /// - 2016/08/09 (1.09.0) - Added additional double version to nk_property and changed
  23565. /// the underlying implementation to not cast to float and instead
  23566. /// work directly on the given values.
  23567. /// - 2016/08/09 (1.08.0) - Added additional define to overwrite library internal
  23568. /// floating pointer number to string conversion for additional
  23569. /// precision.
  23570. /// - 2016/08/09 (1.08.0) - Added additional define to overwrite library internal
  23571. /// string to floating point number conversion for additional
  23572. /// precision.
  23573. /// - 2016/08/08 (1.07.2)- Fixed compiling error without define NK_INCLUDE_FIXED_TYPE
  23574. /// - 2016/08/08 (1.07.1)- Fixed possible floating point error inside `nk_widget` leading
  23575. /// to wrong wiget width calculation which results in widgets falsly
  23576. /// becomming tagged as not inside window and cannot be accessed.
  23577. /// - 2016/08/08 (1.07.0) - Nuklear now differentiates between hiding a window (NK_WINDOW_HIDDEN) and
  23578. /// closing a window (NK_WINDOW_CLOSED). A window can be hidden/shown
  23579. /// by using `nk_window_show` and closed by either clicking the close
  23580. /// icon in a window or by calling `nk_window_close`. Only closed
  23581. /// windows get removed at the end of the frame while hidden windows
  23582. /// remain.
  23583. /// - 2016/08/08 (1.06.0) - Added `nk_edit_string_zero_terminated` as a second option to
  23584. /// `nk_edit_string` which takes, edits and outputs a '\0' terminated string.
  23585. /// - 2016/08/08 (1.05.4)- Fixed scrollbar auto hiding behavior
  23586. /// - 2016/08/08 (1.05.3)- Fixed wrong panel padding selection in `nk_layout_widget_space`
  23587. /// - 2016/08/07 (1.05.2)- Fixed old bug in dynamic immediate mode layout API, calculating
  23588. /// wrong item spacing and panel width.
  23589. ///- 2016/08/07 (1.05.1)- Hopefully finally fixed combobox popup drawing bug
  23590. ///- 2016/08/07 (1.05.0) - Split varargs away from NK_INCLUDE_STANDARD_IO into own
  23591. /// define NK_INCLUDE_STANDARD_VARARGS to allow more fine
  23592. /// grained controlled over library includes.
  23593. /// - 2016/08/06 (1.04.5)- Changed memset calls to NK_MEMSET
  23594. /// - 2016/08/04 (1.04.4)- Fixed fast window scaling behavior
  23595. /// - 2016/08/04 (1.04.3)- Fixed window scaling, movement bug which appears if you
  23596. /// move/scale a window and another window is behind it.
  23597. /// If you are fast enough then the window behind gets activated
  23598. /// and the operation is blocked. I now require activating
  23599. /// by hovering only if mouse is not pressed.
  23600. /// - 2016/08/04 (1.04.2)- Fixed changing fonts
  23601. /// - 2016/08/03 (1.04.1)- Fixed `NK_WINDOW_BACKGROUND` behavior
  23602. /// - 2016/08/03 (1.04.0) - Added color parameter to `nk_draw_image`
  23603. /// - 2016/08/03 (1.04.0) - Added additional window padding style attributes for
  23604. /// sub windows (combo, menu, ...)
  23605. /// - 2016/08/03 (1.04.0) - Added functions to show/hide software cursor
  23606. /// - 2016/08/03 (1.04.0) - Added `NK_WINDOW_BACKGROUND` flag to force a window
  23607. /// to be always in the background of the screen
  23608. /// - 2016/08/03 (1.03.2)- Removed invalid assert macro for NK_RGB color picker
  23609. /// - 2016/08/01 (1.03.1)- Added helper macros into header include guard
  23610. /// - 2016/07/29 (1.03.0) - Moved the window/table pool into the header part to
  23611. /// simplify memory management by removing the need to
  23612. /// allocate the pool.
  23613. /// - 2016/07/29 (1.02.0) - Added auto scrollbar hiding window flag which if enabled
  23614. /// will hide the window scrollbar after NK_SCROLLBAR_HIDING_TIMEOUT
  23615. /// seconds without window interaction. To make it work
  23616. /// you have to also set a delta time inside the `nk_context`.
  23617. /// - 2016/07/25 (1.01.1) - Fixed small panel and panel border drawing bugs
  23618. /// - 2016/07/15 (1.01.0) - Added software cursor to `nk_style` and `nk_context`
  23619. /// - 2016/07/15 (1.01.0) - Added const correctness to `nk_buffer_push' data argument
  23620. /// - 2016/07/15 (1.01.0) - Removed internal font baking API and simplified
  23621. /// font atlas memory management by converting pointer
  23622. /// arrays for fonts and font configurations to lists.
  23623. /// - 2016/07/15 (1.00.0) - Changed button API to use context dependend button
  23624. /// behavior instead of passing it for every function call.
  23625. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23626. /// ## Gallery
  23627. /// ![Figure [blue]: Feature overview with blue color styling](https://cloud.githubusercontent.com/assets/8057201/13538240/acd96876-e249-11e5-9547-5ac0b19667a0.png)
  23628. /// ![Figure [red]: Feature overview with red color styling](https://cloud.githubusercontent.com/assets/8057201/13538243/b04acd4c-e249-11e5-8fd2-ad7744a5b446.png)
  23629. /// ![Figure [widgets]: Widget overview](https://cloud.githubusercontent.com/assets/8057201/11282359/3325e3c6-8eff-11e5-86cb-cf02b0596087.png)
  23630. /// ![Figure [blackwhite]: Black and white](https://cloud.githubusercontent.com/assets/8057201/11033668/59ab5d04-86e5-11e5-8091-c56f16411565.png)
  23631. /// ![Figure [filexp]: File explorer](https://cloud.githubusercontent.com/assets/8057201/10718115/02a9ba08-7b6b-11e5-950f-adacdd637739.png)
  23632. /// ![Figure [opengl]: OpenGL Editor](https://cloud.githubusercontent.com/assets/8057201/12779619/2a20d72c-ca69-11e5-95fe-4edecf820d5c.png)
  23633. /// ![Figure [nodedit]: Node Editor](https://cloud.githubusercontent.com/assets/8057201/9976995/e81ac04a-5ef7-11e5-872b-acd54fbeee03.gif)
  23634. /// ![Figure [skinning]: Using skinning in Nuklear](https://cloud.githubusercontent.com/assets/8057201/15991632/76494854-30b8-11e6-9555-a69840d0d50b.png)
  23635. /// ![Figure [bf]: Heavy modified version](https://cloud.githubusercontent.com/assets/8057201/14902576/339926a8-0d9c-11e6-9fee-a8b73af04473.png)
  23636. ///
  23637. /// ## Credits
  23638. /// Developed by Micha Mettke and every direct or indirect github contributor. <br /><br />
  23639. ///
  23640. /// Embeds [stb_texedit](https://github.com/nothings/stb/blob/master/stb_textedit.h), [stb_truetype](https://github.com/nothings/stb/blob/master/stb_truetype.h) and [stb_rectpack](https://github.com/nothings/stb/blob/master/stb_rect_pack.h) by Sean Barret (public domain) <br />
  23641. /// Uses [stddoc.c](https://github.com/r-lyeh/stddoc.c) from r-lyeh@github.com for documentation generation <br /><br />
  23642. /// Embeds ProggyClean.ttf font by Tristan Grimmer (MIT license). <br />
  23643. ///
  23644. /// Big thank you to Omar Cornut (ocornut@github) for his [imgui library](https://github.com/ocornut/imgui) and
  23645. /// giving me the inspiration for this library, Casey Muratori for handmade hero
  23646. /// and his original immediate mode graphical user interface idea and Sean
  23647. /// Barret for his amazing single header libraries which restored my faith
  23648. /// in libraries and brought me to create some of my own. Finally Apoorva Joshi
  23649. /// for his single header file packer.
  23650. */