nuklear.h 968 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783
  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. /// NK_UINT_DRAW_INDEX | Defining this will set the size of vertex index elements when using NK_VERTEX_BUFFER_OUTPUT to 32bit instead of the default of 16bit
  109. /// NK_KEYSTATE_BASED_INPUT | Define this if your backend uses key state for each frame rather than key press/release events
  110. ///
  111. /// !!! WARNING
  112. /// The following flags will pull in the standard C library:
  113. /// - NK_INCLUDE_DEFAULT_ALLOCATOR
  114. /// - NK_INCLUDE_STANDARD_IO
  115. /// - NK_INCLUDE_STANDARD_VARARGS
  116. ///
  117. /// !!! WARNING
  118. /// The following flags if defined need to be defined for both header and implementation:
  119. /// - NK_INCLUDE_FIXED_TYPES
  120. /// - NK_INCLUDE_DEFAULT_ALLOCATOR
  121. /// - NK_INCLUDE_STANDARD_VARARGS
  122. /// - NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  123. /// - NK_INCLUDE_FONT_BAKING
  124. /// - NK_INCLUDE_DEFAULT_FONT
  125. /// - NK_INCLUDE_STANDARD_VARARGS
  126. /// - NK_INCLUDE_COMMAND_USERDATA
  127. /// - NK_UINT_DRAW_INDEX
  128. ///
  129. /// ### Constants
  130. /// Define | Description
  131. /// --------------------------------|---------------------------------------
  132. /// 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.
  133. /// NK_MAX_NUMBER_BUFFER | Maximum buffer size for the conversion buffer between float and string Under normal circumstances this should be more than sufficient.
  134. /// 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.
  135. ///
  136. /// !!! WARNING
  137. /// The following constants if defined need to be defined for both header and implementation:
  138. /// - NK_MAX_NUMBER_BUFFER
  139. /// - NK_BUFFER_DEFAULT_INITIAL_SIZE
  140. /// - NK_INPUT_MAX
  141. ///
  142. /// ### Dependencies
  143. /// Function | Description
  144. /// ------------|---------------------------------------------------------------
  145. /// NK_ASSERT | If you don't define this, nuklear will use <assert.h> with assert().
  146. /// NK_MEMSET | You can define this to 'memset' or your own memset implementation replacement. If not nuklear will use its own version.
  147. /// NK_MEMCPY | You can define this to 'memcpy' or your own memcpy implementation replacement. If not nuklear will use its own version.
  148. /// 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.
  149. /// NK_SIN | You can define this to 'sinf' or your own sine implementation replacement. If not nuklear will use its own approximation implementation.
  150. /// NK_COS | You can define this to 'cosf' or your own cosine implementation replacement. If not nuklear will use its own approximation implementation.
  151. /// 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!).
  152. /// 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!).
  153. /// 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.
  154. ///
  155. /// !!! WARNING
  156. /// The following dependencies will pull in the standard C library if not redefined:
  157. /// - NK_ASSERT
  158. ///
  159. /// !!! WARNING
  160. /// The following dependencies if defined need to be defined for both header and implementation:
  161. /// - NK_ASSERT
  162. ///
  163. /// !!! WARNING
  164. /// The following dependencies if defined need to be defined only for the implementation part:
  165. /// - NK_MEMSET
  166. /// - NK_MEMCPY
  167. /// - NK_SQRT
  168. /// - NK_SIN
  169. /// - NK_COS
  170. /// - NK_STRTOD
  171. /// - NK_DTOA
  172. /// - NK_VSNPRINTF
  173. ///
  174. /// ## Example
  175. ///
  176. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  177. /// // init gui state
  178. /// enum {EASY, HARD};
  179. /// static int op = EASY;
  180. /// static float value = 0.6f;
  181. /// static int i = 20;
  182. /// struct nk_context ctx;
  183. ///
  184. /// nk_init_fixed(&ctx, calloc(1, MAX_MEMORY), MAX_MEMORY, &font);
  185. /// if (nk_begin(&ctx, "Show", nk_rect(50, 50, 220, 220),
  186. /// NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
  187. /// // fixed widget pixel width
  188. /// nk_layout_row_static(&ctx, 30, 80, 1);
  189. /// if (nk_button_label(&ctx, "button")) {
  190. /// // event handling
  191. /// }
  192. ///
  193. /// // fixed widget window ratio width
  194. /// nk_layout_row_dynamic(&ctx, 30, 2);
  195. /// if (nk_option_label(&ctx, "easy", op == EASY)) op = EASY;
  196. /// if (nk_option_label(&ctx, "hard", op == HARD)) op = HARD;
  197. ///
  198. /// // custom widget pixel width
  199. /// nk_layout_row_begin(&ctx, NK_STATIC, 30, 2);
  200. /// {
  201. /// nk_layout_row_push(&ctx, 50);
  202. /// nk_label(&ctx, "Volume:", NK_TEXT_LEFT);
  203. /// nk_layout_row_push(&ctx, 110);
  204. /// nk_slider_float(&ctx, 0, &value, 1.0f, 0.1f);
  205. /// }
  206. /// nk_layout_row_end(&ctx);
  207. /// }
  208. /// nk_end(&ctx);
  209. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  210. ///
  211. /// ![](https://cloud.githubusercontent.com/assets/8057201/10187981/584ecd68-675c-11e5-897c-822ef534a876.png)
  212. ///
  213. /// ## API
  214. ///
  215. */
  216. #ifndef NK_SINGLE_FILE
  217. #define NK_SINGLE_FILE
  218. #endif
  219. #ifndef NK_NUKLEAR_H_
  220. #define NK_NUKLEAR_H_
  221. #ifdef __cplusplus
  222. extern "C" {
  223. #endif
  224. /*
  225. * ==============================================================
  226. *
  227. * CONSTANTS
  228. *
  229. * ===============================================================
  230. */
  231. #define NK_UNDEFINED (-1.0f)
  232. #define NK_UTF_INVALID 0xFFFD /* internal invalid utf8 rune */
  233. #define NK_UTF_SIZE 4 /* describes the number of bytes a glyph consists of*/
  234. #ifndef NK_INPUT_MAX
  235. #define NK_INPUT_MAX 16
  236. #endif
  237. #ifndef NK_MAX_NUMBER_BUFFER
  238. #define NK_MAX_NUMBER_BUFFER 64
  239. #endif
  240. #ifndef NK_SCROLLBAR_HIDING_TIMEOUT
  241. #define NK_SCROLLBAR_HIDING_TIMEOUT 4.0f
  242. #endif
  243. /*
  244. * ==============================================================
  245. *
  246. * HELPER
  247. *
  248. * ===============================================================
  249. */
  250. #ifndef NK_API
  251. #ifdef NK_PRIVATE
  252. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199409L))
  253. #define NK_API static inline
  254. #elif defined(__cplusplus)
  255. #define NK_API static inline
  256. #else
  257. #define NK_API static
  258. #endif
  259. #else
  260. #define NK_API extern
  261. #endif
  262. #endif
  263. #ifndef NK_LIB
  264. #ifdef NK_SINGLE_FILE
  265. #define NK_LIB static
  266. #else
  267. #define NK_LIB extern
  268. #endif
  269. #endif
  270. #define NK_INTERN static
  271. #define NK_STORAGE static
  272. #define NK_GLOBAL static
  273. #define NK_FLAG(x) (1 << (x))
  274. #define NK_STRINGIFY(x) #x
  275. #define NK_MACRO_STRINGIFY(x) NK_STRINGIFY(x)
  276. #define NK_STRING_JOIN_IMMEDIATE(arg1, arg2) arg1 ## arg2
  277. #define NK_STRING_JOIN_DELAY(arg1, arg2) NK_STRING_JOIN_IMMEDIATE(arg1, arg2)
  278. #define NK_STRING_JOIN(arg1, arg2) NK_STRING_JOIN_DELAY(arg1, arg2)
  279. #ifdef _MSC_VER
  280. #define NK_UNIQUE_NAME(name) NK_STRING_JOIN(name,__COUNTER__)
  281. #else
  282. #define NK_UNIQUE_NAME(name) NK_STRING_JOIN(name,__LINE__)
  283. #endif
  284. #ifndef NK_STATIC_ASSERT
  285. #define NK_STATIC_ASSERT(exp) typedef char NK_UNIQUE_NAME(_dummy_array)[(exp)?1:-1]
  286. #endif
  287. #ifndef NK_FILE_LINE
  288. #ifdef _MSC_VER
  289. #define NK_FILE_LINE __FILE__ ":" NK_MACRO_STRINGIFY(__COUNTER__)
  290. #else
  291. #define NK_FILE_LINE __FILE__ ":" NK_MACRO_STRINGIFY(__LINE__)
  292. #endif
  293. #endif
  294. #define NK_MIN(a,b) ((a) < (b) ? (a) : (b))
  295. #define NK_MAX(a,b) ((a) < (b) ? (b) : (a))
  296. #define NK_CLAMP(i,v,x) (NK_MAX(NK_MIN(v,x), i))
  297. #ifdef NK_INCLUDE_STANDARD_VARARGS
  298. #include <stdarg.h> /* valist, va_start, va_end, ... */
  299. #if defined(_MSC_VER) && (_MSC_VER >= 1600) /* VS 2010 and above */
  300. #include <sal.h>
  301. #define NK_PRINTF_FORMAT_STRING _Printf_format_string_
  302. #else
  303. #define NK_PRINTF_FORMAT_STRING
  304. #endif
  305. #if defined(__GNUC__)
  306. #define NK_PRINTF_VARARG_FUNC(fmtargnumber) __attribute__((format(__printf__, fmtargnumber, fmtargnumber+1)))
  307. #define NK_PRINTF_VALIST_FUNC(fmtargnumber) __attribute__((format(__printf__, fmtargnumber, 0)))
  308. #else
  309. #define NK_PRINTF_VARARG_FUNC(fmtargnumber)
  310. #define NK_PRINTF_VALIST_FUNC(fmtargnumber)
  311. #endif
  312. #endif
  313. /*
  314. * ===============================================================
  315. *
  316. * BASIC
  317. *
  318. * ===============================================================
  319. */
  320. #ifdef NK_INCLUDE_FIXED_TYPES
  321. #include <stdint.h>
  322. #define NK_INT8 int8_t
  323. #define NK_UINT8 uint8_t
  324. #define NK_INT16 int16_t
  325. #define NK_UINT16 uint16_t
  326. #define NK_INT32 int32_t
  327. #define NK_UINT32 uint32_t
  328. #define NK_SIZE_TYPE uintptr_t
  329. #define NK_POINTER_TYPE uintptr_t
  330. #else
  331. #ifndef NK_INT8
  332. #define NK_INT8 signed char
  333. #endif
  334. #ifndef NK_UINT8
  335. #define NK_UINT8 unsigned char
  336. #endif
  337. #ifndef NK_INT16
  338. #define NK_INT16 signed short
  339. #endif
  340. #ifndef NK_UINT16
  341. #define NK_UINT16 unsigned short
  342. #endif
  343. #ifndef NK_INT32
  344. #if defined(_MSC_VER)
  345. #define NK_INT32 __int32
  346. #else
  347. #define NK_INT32 signed int
  348. #endif
  349. #endif
  350. #ifndef NK_UINT32
  351. #if defined(_MSC_VER)
  352. #define NK_UINT32 unsigned __int32
  353. #else
  354. #define NK_UINT32 unsigned int
  355. #endif
  356. #endif
  357. #ifndef NK_SIZE_TYPE
  358. #if defined(_WIN64) && defined(_MSC_VER)
  359. #define NK_SIZE_TYPE unsigned __int64
  360. #elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
  361. #define NK_SIZE_TYPE unsigned __int32
  362. #elif defined(__GNUC__) || defined(__clang__)
  363. #if defined(__x86_64__) || defined(__ppc64__)
  364. #define NK_SIZE_TYPE unsigned long
  365. #else
  366. #define NK_SIZE_TYPE unsigned int
  367. #endif
  368. #else
  369. #define NK_SIZE_TYPE unsigned long
  370. #endif
  371. #endif
  372. #ifndef NK_POINTER_TYPE
  373. #if defined(_WIN64) && defined(_MSC_VER)
  374. #define NK_POINTER_TYPE unsigned __int64
  375. #elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
  376. #define NK_POINTER_TYPE unsigned __int32
  377. #elif defined(__GNUC__) || defined(__clang__)
  378. #if defined(__x86_64__) || defined(__ppc64__)
  379. #define NK_POINTER_TYPE unsigned long
  380. #else
  381. #define NK_POINTER_TYPE unsigned int
  382. #endif
  383. #else
  384. #define NK_POINTER_TYPE unsigned long
  385. #endif
  386. #endif
  387. #endif
  388. typedef NK_INT8 nk_char;
  389. typedef NK_UINT8 nk_uchar;
  390. typedef NK_UINT8 nk_byte;
  391. typedef NK_INT16 nk_short;
  392. typedef NK_UINT16 nk_ushort;
  393. typedef NK_INT32 nk_int;
  394. typedef NK_UINT32 nk_uint;
  395. typedef NK_SIZE_TYPE nk_size;
  396. typedef NK_POINTER_TYPE nk_ptr;
  397. typedef nk_uint nk_hash;
  398. typedef nk_uint nk_flags;
  399. typedef nk_uint nk_rune;
  400. /* Make sure correct type size:
  401. * This will fire with a negative subscript error if the type sizes
  402. * are set incorrectly by the compiler, and compile out if not */
  403. NK_STATIC_ASSERT(sizeof(nk_short) == 2);
  404. NK_STATIC_ASSERT(sizeof(nk_ushort) == 2);
  405. NK_STATIC_ASSERT(sizeof(nk_uint) == 4);
  406. NK_STATIC_ASSERT(sizeof(nk_int) == 4);
  407. NK_STATIC_ASSERT(sizeof(nk_byte) == 1);
  408. NK_STATIC_ASSERT(sizeof(nk_flags) >= 4);
  409. NK_STATIC_ASSERT(sizeof(nk_rune) >= 4);
  410. NK_STATIC_ASSERT(sizeof(nk_size) >= sizeof(void*));
  411. NK_STATIC_ASSERT(sizeof(nk_ptr) >= sizeof(void*));
  412. #if defined(_MSC_VER)
  413. /* disable `operands are different enum types` warning on MSVC */
  414. #pragma warning( disable: 5287 )
  415. #endif
  416. /* ============================================================================
  417. *
  418. * API
  419. *
  420. * =========================================================================== */
  421. struct nk_buffer;
  422. struct nk_allocator;
  423. struct nk_command_buffer;
  424. struct nk_draw_command;
  425. struct nk_convert_config;
  426. struct nk_style_item;
  427. struct nk_text_edit;
  428. struct nk_draw_list;
  429. struct nk_user_font;
  430. struct nk_panel;
  431. struct nk_context;
  432. struct nk_draw_vertex_layout_element;
  433. struct nk_style_button;
  434. struct nk_style_toggle;
  435. struct nk_style_selectable;
  436. struct nk_style_slide;
  437. struct nk_style_progress;
  438. struct nk_style_scrollbar;
  439. struct nk_style_edit;
  440. struct nk_style_property;
  441. struct nk_style_chart;
  442. struct nk_style_combo;
  443. struct nk_style_tab;
  444. struct nk_style_window_header;
  445. struct nk_style_window;
  446. enum {nk_false, nk_true};
  447. struct nk_color {nk_byte r,g,b,a;};
  448. struct nk_colorf {float r,g,b,a;};
  449. struct nk_vec2 {float x,y;};
  450. struct nk_vec2i {short x, y;};
  451. struct nk_rect {float x,y,w,h;};
  452. struct nk_recti {short x,y,w,h;};
  453. typedef char nk_glyph[NK_UTF_SIZE];
  454. typedef union {void *ptr; int id;} nk_handle;
  455. struct nk_image {nk_handle handle;unsigned short w,h;unsigned short region[4];};
  456. struct nk_cursor {struct nk_image img; struct nk_vec2 size, offset;};
  457. struct nk_scroll {nk_uint x, y;};
  458. enum nk_heading {NK_UP, NK_RIGHT, NK_DOWN, NK_LEFT};
  459. enum nk_button_behavior {NK_BUTTON_DEFAULT, NK_BUTTON_REPEATER};
  460. enum nk_modify {NK_FIXED = nk_false, NK_MODIFIABLE = nk_true};
  461. enum nk_orientation {NK_VERTICAL, NK_HORIZONTAL};
  462. enum nk_collapse_states {NK_MINIMIZED = nk_false, NK_MAXIMIZED = nk_true};
  463. enum nk_show_states {NK_HIDDEN = nk_false, NK_SHOWN = nk_true};
  464. enum nk_chart_type {NK_CHART_LINES, NK_CHART_COLUMN, NK_CHART_MAX};
  465. enum nk_chart_event {NK_CHART_HOVERING = 0x01, NK_CHART_CLICKED = 0x02};
  466. enum nk_color_format {NK_RGB, NK_RGBA};
  467. enum nk_popup_type {NK_POPUP_STATIC, NK_POPUP_DYNAMIC};
  468. enum nk_layout_format {NK_DYNAMIC, NK_STATIC};
  469. enum nk_tree_type {NK_TREE_NODE, NK_TREE_TAB};
  470. typedef void*(*nk_plugin_alloc)(nk_handle, void *old, nk_size);
  471. typedef void (*nk_plugin_free)(nk_handle, void *old);
  472. typedef int(*nk_plugin_filter)(const struct nk_text_edit*, nk_rune unicode);
  473. typedef void(*nk_plugin_paste)(nk_handle, struct nk_text_edit*);
  474. typedef void(*nk_plugin_copy)(nk_handle, const char*, int len);
  475. struct nk_allocator {
  476. nk_handle userdata;
  477. nk_plugin_alloc alloc;
  478. nk_plugin_free free;
  479. };
  480. enum nk_symbol_type {
  481. NK_SYMBOL_NONE,
  482. NK_SYMBOL_X,
  483. NK_SYMBOL_UNDERSCORE,
  484. NK_SYMBOL_CIRCLE_SOLID,
  485. NK_SYMBOL_CIRCLE_OUTLINE,
  486. NK_SYMBOL_RECT_SOLID,
  487. NK_SYMBOL_RECT_OUTLINE,
  488. NK_SYMBOL_TRIANGLE_UP,
  489. NK_SYMBOL_TRIANGLE_DOWN,
  490. NK_SYMBOL_TRIANGLE_LEFT,
  491. NK_SYMBOL_TRIANGLE_RIGHT,
  492. NK_SYMBOL_PLUS,
  493. NK_SYMBOL_MINUS,
  494. NK_SYMBOL_MAX
  495. };
  496. /* =============================================================================
  497. *
  498. * CONTEXT
  499. *
  500. * =============================================================================*/
  501. /*/// ### Context
  502. /// Contexts are the main entry point and the majestro of nuklear and contain all required state.
  503. /// They are used for window, memory, input, style, stack, commands and time management and need
  504. /// to be passed into all nuklear GUI specific functions.
  505. ///
  506. /// #### Usage
  507. /// To use a context it first has to be initialized which can be achieved by calling
  508. /// one of either `nk_init_default`, `nk_init_fixed`, `nk_init`, `nk_init_custom`.
  509. /// Each takes in a font handle and a specific way of handling memory. Memory control
  510. /// hereby ranges from standard library to just specifying a fixed sized block of memory
  511. /// which nuklear has to manage itself from.
  512. ///
  513. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  514. /// struct nk_context ctx;
  515. /// nk_init_xxx(&ctx, ...);
  516. /// while (1) {
  517. /// // [...]
  518. /// nk_clear(&ctx);
  519. /// }
  520. /// nk_free(&ctx);
  521. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  522. ///
  523. /// #### Reference
  524. /// Function | Description
  525. /// --------------------|-------------------------------------------------------
  526. /// __nk_init_default__ | Initializes context with standard library memory allocation (malloc,free)
  527. /// __nk_init_fixed__ | Initializes context from single fixed size memory block
  528. /// __nk_init__ | Initializes context with memory allocator callbacks for alloc and free
  529. /// __nk_init_custom__ | Initializes context from two buffers. One for draw commands the other for window/panel/table allocations
  530. /// __nk_clear__ | Called at the end of the frame to reset and prepare the context for the next frame
  531. /// __nk_free__ | Shutdown and free all memory allocated inside the context
  532. /// __nk_set_user_data__| Utility function to pass user data to draw command
  533. */
  534. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  535. /*/// #### nk_init_default
  536. /// Initializes a `nk_context` struct with a default standard library allocator.
  537. /// Should be used if you don't want to be bothered with memory management in nuklear.
  538. ///
  539. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  540. /// int nk_init_default(struct nk_context *ctx, const struct nk_user_font *font);
  541. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  542. ///
  543. /// Parameter | Description
  544. /// ------------|---------------------------------------------------------------
  545. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  546. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  547. ///
  548. /// Returns either `false(0)` on failure or `true(1)` on success.
  549. ///
  550. */
  551. NK_API int nk_init_default(struct nk_context*, const struct nk_user_font*);
  552. #endif
  553. /*/// #### nk_init_fixed
  554. /// Initializes a `nk_context` struct from single fixed size memory block
  555. /// Should be used if you want complete control over nuklear's memory management.
  556. /// Especially recommended for system with little memory or systems with virtual memory.
  557. /// For the later case you can just allocate for example 16MB of virtual memory
  558. /// and only the required amount of memory will actually be committed.
  559. ///
  560. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  561. /// int nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size, const struct nk_user_font *font);
  562. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  563. ///
  564. /// !!! Warning
  565. /// make sure the passed memory block is aligned correctly for `nk_draw_commands`.
  566. ///
  567. /// Parameter | Description
  568. /// ------------|--------------------------------------------------------------
  569. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  570. /// __memory__ | Must point to a previously allocated memory block
  571. /// __size__ | Must contain the total size of __memory__
  572. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  573. ///
  574. /// Returns either `false(0)` on failure or `true(1)` on success.
  575. */
  576. NK_API int nk_init_fixed(struct nk_context*, void *memory, nk_size size, const struct nk_user_font*);
  577. /*/// #### nk_init
  578. /// Initializes a `nk_context` struct with memory allocation callbacks for nuklear to allocate
  579. /// memory from. Used internally for `nk_init_default` and provides a kitchen sink allocation
  580. /// interface to nuklear. Can be useful for cases like monitoring memory consumption.
  581. ///
  582. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  583. /// int nk_init(struct nk_context *ctx, struct nk_allocator *alloc, const struct nk_user_font *font);
  584. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  585. ///
  586. /// Parameter | Description
  587. /// ------------|---------------------------------------------------------------
  588. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  589. /// __alloc__ | Must point to a previously allocated memory allocator
  590. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  591. ///
  592. /// Returns either `false(0)` on failure or `true(1)` on success.
  593. */
  594. NK_API int nk_init(struct nk_context*, struct nk_allocator*, const struct nk_user_font*);
  595. /*/// #### nk_init_custom
  596. /// Initializes a `nk_context` struct from two different either fixed or growing
  597. /// buffers. The first buffer is for allocating draw commands while the second buffer is
  598. /// used for allocating windows, panels and state tables.
  599. ///
  600. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  601. /// int nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font *font);
  602. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  603. ///
  604. /// Parameter | Description
  605. /// ------------|---------------------------------------------------------------
  606. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  607. /// __cmds__ | Must point to a previously initialized memory buffer either fixed or dynamic to store draw commands into
  608. /// __pool__ | Must point to a previously initialized memory buffer either fixed or dynamic to store windows, panels and tables
  609. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  610. ///
  611. /// Returns either `false(0)` on failure or `true(1)` on success.
  612. */
  613. NK_API int nk_init_custom(struct nk_context*, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font*);
  614. /*/// #### nk_clear
  615. /// Resets the context state at the end of the frame. This includes mostly
  616. /// garbage collector tasks like removing windows or table not called and therefore
  617. /// used anymore.
  618. ///
  619. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  620. /// void nk_clear(struct nk_context *ctx);
  621. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  622. ///
  623. /// Parameter | Description
  624. /// ------------|-----------------------------------------------------------
  625. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  626. */
  627. NK_API void nk_clear(struct nk_context*);
  628. /*/// #### nk_free
  629. /// Frees all memory allocated by nuklear. Not needed if context was
  630. /// initialized with `nk_init_fixed`.
  631. ///
  632. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  633. /// void nk_free(struct nk_context *ctx);
  634. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  635. ///
  636. /// Parameter | Description
  637. /// ------------|-----------------------------------------------------------
  638. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  639. */
  640. NK_API void nk_free(struct nk_context*);
  641. #ifdef NK_INCLUDE_COMMAND_USERDATA
  642. /*/// #### nk_set_user_data
  643. /// Sets the currently passed userdata passed down into each draw command.
  644. ///
  645. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  646. /// void nk_set_user_data(struct nk_context *ctx, nk_handle data);
  647. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  648. ///
  649. /// Parameter | Description
  650. /// ------------|--------------------------------------------------------------
  651. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  652. /// __data__ | Handle with either pointer or index to be passed into every draw commands
  653. */
  654. NK_API void nk_set_user_data(struct nk_context*, nk_handle handle);
  655. #endif
  656. /* =============================================================================
  657. *
  658. * INPUT
  659. *
  660. * =============================================================================*/
  661. /*/// ### Input
  662. /// The input API is responsible for holding the current input state composed of
  663. /// mouse, key and text input states.
  664. /// It is worth noting that no direct OS or window handling is done in nuklear.
  665. /// Instead all input state has to be provided by platform specific code. This on one hand
  666. /// expects more work from the user and complicates usage but on the other hand
  667. /// provides simple abstraction over a big number of platforms, libraries and other
  668. /// already provided functionality.
  669. ///
  670. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  671. /// nk_input_begin(&ctx);
  672. /// while (GetEvent(&evt)) {
  673. /// if (evt.type == MOUSE_MOVE)
  674. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  675. /// else if (evt.type == [...]) {
  676. /// // [...]
  677. /// }
  678. /// } nk_input_end(&ctx);
  679. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  680. ///
  681. /// #### Usage
  682. /// Input state needs to be provided to nuklear by first calling `nk_input_begin`
  683. /// which resets internal state like delta mouse position and button transistions.
  684. /// After `nk_input_begin` all current input state needs to be provided. This includes
  685. /// mouse motion, button and key pressed and released, text input and scrolling.
  686. /// Both event- or state-based input handling are supported by this API
  687. /// and should work without problems. Finally after all input state has been
  688. /// mirrored `nk_input_end` needs to be called to finish input process.
  689. ///
  690. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  691. /// struct nk_context ctx;
  692. /// nk_init_xxx(&ctx, ...);
  693. /// while (1) {
  694. /// Event evt;
  695. /// nk_input_begin(&ctx);
  696. /// while (GetEvent(&evt)) {
  697. /// if (evt.type == MOUSE_MOVE)
  698. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  699. /// else if (evt.type == [...]) {
  700. /// // [...]
  701. /// }
  702. /// }
  703. /// nk_input_end(&ctx);
  704. /// // [...]
  705. /// nk_clear(&ctx);
  706. /// } nk_free(&ctx);
  707. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  708. ///
  709. /// #### Reference
  710. /// Function | Description
  711. /// --------------------|-------------------------------------------------------
  712. /// __nk_input_begin__ | Begins the input mirroring process. Needs to be called before all other `nk_input_xxx` calls
  713. /// __nk_input_motion__ | Mirrors mouse cursor position
  714. /// __nk_input_key__ | Mirrors key state with either pressed or released
  715. /// __nk_input_button__ | Mirrors mouse button state with either pressed or released
  716. /// __nk_input_scroll__ | Mirrors mouse scroll values
  717. /// __nk_input_char__ | Adds a single ASCII text character into an internal text buffer
  718. /// __nk_input_glyph__ | Adds a single multi-byte UTF-8 character into an internal text buffer
  719. /// __nk_input_unicode__| Adds a single unicode rune into an internal text buffer
  720. /// __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
  721. */
  722. enum nk_keys {
  723. NK_KEY_NONE,
  724. NK_KEY_SHIFT,
  725. NK_KEY_CTRL,
  726. NK_KEY_DEL,
  727. NK_KEY_ENTER,
  728. NK_KEY_TAB,
  729. NK_KEY_BACKSPACE,
  730. NK_KEY_COPY,
  731. NK_KEY_CUT,
  732. NK_KEY_PASTE,
  733. NK_KEY_UP,
  734. NK_KEY_DOWN,
  735. NK_KEY_LEFT,
  736. NK_KEY_RIGHT,
  737. /* Shortcuts: text field */
  738. NK_KEY_TEXT_INSERT_MODE,
  739. NK_KEY_TEXT_REPLACE_MODE,
  740. NK_KEY_TEXT_RESET_MODE,
  741. NK_KEY_TEXT_LINE_START,
  742. NK_KEY_TEXT_LINE_END,
  743. NK_KEY_TEXT_START,
  744. NK_KEY_TEXT_END,
  745. NK_KEY_TEXT_UNDO,
  746. NK_KEY_TEXT_REDO,
  747. NK_KEY_TEXT_SELECT_ALL,
  748. NK_KEY_TEXT_WORD_LEFT,
  749. NK_KEY_TEXT_WORD_RIGHT,
  750. /* Shortcuts: scrollbar */
  751. NK_KEY_SCROLL_START,
  752. NK_KEY_SCROLL_END,
  753. NK_KEY_SCROLL_DOWN,
  754. NK_KEY_SCROLL_UP,
  755. NK_KEY_MAX
  756. };
  757. enum nk_buttons {
  758. NK_BUTTON_LEFT,
  759. NK_BUTTON_MIDDLE,
  760. NK_BUTTON_RIGHT,
  761. NK_BUTTON_DOUBLE,
  762. NK_BUTTON_MAX
  763. };
  764. /*/// #### nk_input_begin
  765. /// Begins the input mirroring process by resetting text, scroll
  766. /// mouse, previous mouse position and movement as well as key state transitions,
  767. ///
  768. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  769. /// void nk_input_begin(struct nk_context*);
  770. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  771. ///
  772. /// Parameter | Description
  773. /// ------------|-----------------------------------------------------------
  774. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  775. */
  776. NK_API void nk_input_begin(struct nk_context*);
  777. /*/// #### nk_input_motion
  778. /// Mirrors current mouse position to nuklear
  779. ///
  780. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  781. /// void nk_input_motion(struct nk_context *ctx, int x, int y);
  782. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  783. ///
  784. /// Parameter | Description
  785. /// ------------|-----------------------------------------------------------
  786. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  787. /// __x__ | Must hold an integer describing the current mouse cursor x-position
  788. /// __y__ | Must hold an integer describing the current mouse cursor y-position
  789. */
  790. NK_API void nk_input_motion(struct nk_context*, int x, int y);
  791. /*/// #### nk_input_key
  792. /// Mirrors the state of a specific key to nuklear
  793. ///
  794. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  795. /// void nk_input_key(struct nk_context*, enum nk_keys key, int down);
  796. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  797. ///
  798. /// Parameter | Description
  799. /// ------------|-----------------------------------------------------------
  800. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  801. /// __key__ | Must be any value specified in enum `nk_keys` that needs to be mirrored
  802. /// __down__ | Must be 0 for key is up and 1 for key is down
  803. */
  804. NK_API void nk_input_key(struct nk_context*, enum nk_keys, int down);
  805. /*/// #### nk_input_button
  806. /// Mirrors the state of a specific mouse button to nuklear
  807. ///
  808. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  809. /// void nk_input_button(struct nk_context *ctx, enum nk_buttons btn, int x, int y, int down);
  810. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  811. ///
  812. /// Parameter | Description
  813. /// ------------|-----------------------------------------------------------
  814. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  815. /// __btn__ | Must be any value specified in enum `nk_buttons` that needs to be mirrored
  816. /// __x__ | Must contain an integer describing mouse cursor x-position on click up/down
  817. /// __y__ | Must contain an integer describing mouse cursor y-position on click up/down
  818. /// __down__ | Must be 0 for key is up and 1 for key is down
  819. */
  820. NK_API void nk_input_button(struct nk_context*, enum nk_buttons, int x, int y, int down);
  821. /*/// #### nk_input_scroll
  822. /// Copies the last mouse scroll value to nuklear. Is generally
  823. /// a scroll value. So does not have to come from mouse and could also originate
  824. /// TODO finish this sentence
  825. ///
  826. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  827. /// void nk_input_scroll(struct nk_context *ctx, struct nk_vec2 val);
  828. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  829. ///
  830. /// Parameter | Description
  831. /// ------------|-----------------------------------------------------------
  832. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  833. /// __val__ | vector with both X- as well as Y-scroll value
  834. */
  835. NK_API void nk_input_scroll(struct nk_context*, struct nk_vec2 val);
  836. /*/// #### nk_input_char
  837. /// Copies a single ASCII character into an internal text buffer
  838. /// This is basically a helper function to quickly push ASCII characters into
  839. /// nuklear.
  840. ///
  841. /// !!! Note
  842. /// Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
  843. ///
  844. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  845. /// void nk_input_char(struct nk_context *ctx, char c);
  846. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  847. ///
  848. /// Parameter | Description
  849. /// ------------|-----------------------------------------------------------
  850. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  851. /// __c__ | Must be a single ASCII character preferable one that can be printed
  852. */
  853. NK_API void nk_input_char(struct nk_context*, char);
  854. /*/// #### nk_input_glyph
  855. /// Converts an encoded unicode rune into UTF-8 and copies the result into an
  856. /// internal text buffer.
  857. ///
  858. /// !!! Note
  859. /// Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
  860. ///
  861. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  862. /// void nk_input_glyph(struct nk_context *ctx, const nk_glyph g);
  863. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  864. ///
  865. /// Parameter | Description
  866. /// ------------|-----------------------------------------------------------
  867. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  868. /// __g__ | UTF-32 unicode codepoint
  869. */
  870. NK_API void nk_input_glyph(struct nk_context*, const nk_glyph);
  871. /*/// #### nk_input_unicode
  872. /// Converts a unicode rune into UTF-8 and copies the result
  873. /// into an internal text buffer.
  874. /// !!! Note
  875. /// Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
  876. ///
  877. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  878. /// void nk_input_unicode(struct nk_context*, nk_rune rune);
  879. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  880. ///
  881. /// Parameter | Description
  882. /// ------------|-----------------------------------------------------------
  883. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  884. /// __rune__ | UTF-32 unicode codepoint
  885. */
  886. NK_API void nk_input_unicode(struct nk_context*, nk_rune);
  887. /*/// #### nk_input_end
  888. /// End the input mirroring process by resetting mouse grabbing
  889. /// state to ensure the mouse cursor is not grabbed indefinitely.
  890. ///
  891. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  892. /// void nk_input_end(struct nk_context *ctx);
  893. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  894. ///
  895. /// Parameter | Description
  896. /// ------------|-----------------------------------------------------------
  897. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  898. */
  899. NK_API void nk_input_end(struct nk_context*);
  900. /* =============================================================================
  901. *
  902. * DRAWING
  903. *
  904. * =============================================================================*/
  905. /*/// ### Drawing
  906. /// This library was designed to be render backend agnostic so it does
  907. /// not draw anything to screen directly. Instead all drawn shapes, widgets
  908. /// are made of, are buffered into memory and make up a command queue.
  909. /// Each frame therefore fills the command buffer with draw commands
  910. /// that then need to be executed by the user and his own render backend.
  911. /// After that the command buffer needs to be cleared and a new frame can be
  912. /// started. It is probably important to note that the command buffer is the main
  913. /// drawing API and the optional vertex buffer API only takes this format and
  914. /// converts it into a hardware accessible format.
  915. ///
  916. /// #### Usage
  917. /// To draw all draw commands accumulated over a frame you need your own render
  918. /// backend able to draw a number of 2D primitives. This includes at least
  919. /// filled and stroked rectangles, circles, text, lines, triangles and scissors.
  920. /// As soon as this criterion is met you can iterate over each draw command
  921. /// and execute each draw command in a interpreter like fashion:
  922. ///
  923. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  924. /// const struct nk_command *cmd = 0;
  925. /// nk_foreach(cmd, &ctx) {
  926. /// switch (cmd->type) {
  927. /// case NK_COMMAND_LINE:
  928. /// your_draw_line_function(...)
  929. /// break;
  930. /// case NK_COMMAND_RECT
  931. /// your_draw_rect_function(...)
  932. /// break;
  933. /// case //...:
  934. /// //[...]
  935. /// }
  936. /// }
  937. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  938. ///
  939. /// In program flow context draw commands need to be executed after input has been
  940. /// gathered and the complete UI with windows and their contained widgets have
  941. /// been executed and before calling `nk_clear` which frees all previously
  942. /// allocated draw commands.
  943. ///
  944. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  945. /// struct nk_context ctx;
  946. /// nk_init_xxx(&ctx, ...);
  947. /// while (1) {
  948. /// Event evt;
  949. /// nk_input_begin(&ctx);
  950. /// while (GetEvent(&evt)) {
  951. /// if (evt.type == MOUSE_MOVE)
  952. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  953. /// else if (evt.type == [...]) {
  954. /// [...]
  955. /// }
  956. /// }
  957. /// nk_input_end(&ctx);
  958. /// //
  959. /// // [...]
  960. /// //
  961. /// const struct nk_command *cmd = 0;
  962. /// nk_foreach(cmd, &ctx) {
  963. /// switch (cmd->type) {
  964. /// case NK_COMMAND_LINE:
  965. /// your_draw_line_function(...)
  966. /// break;
  967. /// case NK_COMMAND_RECT
  968. /// your_draw_rect_function(...)
  969. /// break;
  970. /// case ...:
  971. /// // [...]
  972. /// }
  973. /// nk_clear(&ctx);
  974. /// }
  975. /// nk_free(&ctx);
  976. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  977. ///
  978. /// You probably noticed that you have to draw all of the UI each frame which is
  979. /// quite wasteful. While the actual UI updating loop is quite fast rendering
  980. /// without actually needing it is not. So there are multiple things you could do.
  981. ///
  982. /// First is only update on input. This of course is only an option if your
  983. /// application only depends on the UI and does not require any outside calculations.
  984. /// If you actually only update on input make sure to update the UI two times each
  985. /// frame and call `nk_clear` directly after the first pass and only draw in
  986. /// the second pass. In addition it is recommended to also add additional timers
  987. /// to make sure the UI is not drawn more than a fixed number of frames per second.
  988. ///
  989. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  990. /// struct nk_context ctx;
  991. /// nk_init_xxx(&ctx, ...);
  992. /// while (1) {
  993. /// // [...wait for input ]
  994. /// // [...do two UI passes ...]
  995. /// do_ui(...)
  996. /// nk_clear(&ctx);
  997. /// do_ui(...)
  998. /// //
  999. /// // draw
  1000. /// const struct nk_command *cmd = 0;
  1001. /// nk_foreach(cmd, &ctx) {
  1002. /// switch (cmd->type) {
  1003. /// case NK_COMMAND_LINE:
  1004. /// your_draw_line_function(...)
  1005. /// break;
  1006. /// case NK_COMMAND_RECT
  1007. /// your_draw_rect_function(...)
  1008. /// break;
  1009. /// case ...:
  1010. /// //[...]
  1011. /// }
  1012. /// nk_clear(&ctx);
  1013. /// }
  1014. /// nk_free(&ctx);
  1015. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1016. ///
  1017. /// The second probably more applicable trick is to only draw if anything changed.
  1018. /// It is not really useful for applications with continuous draw loop but
  1019. /// quite useful for desktop applications. To actually get nuklear to only
  1020. /// draw on changes you first have to define `NK_ZERO_COMMAND_MEMORY` and
  1021. /// allocate a memory buffer that will store each unique drawing output.
  1022. /// After each frame you compare the draw command memory inside the library
  1023. /// with your allocated buffer by memcmp. If memcmp detects differences
  1024. /// you have to copy the command buffer into the allocated buffer
  1025. /// and then draw like usual (this example uses fixed memory but you could
  1026. /// use dynamically allocated memory).
  1027. ///
  1028. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1029. /// //[... other defines ...]
  1030. /// #define NK_ZERO_COMMAND_MEMORY
  1031. /// #include "nuklear.h"
  1032. /// //
  1033. /// // setup context
  1034. /// struct nk_context ctx;
  1035. /// void *last = calloc(1,64*1024);
  1036. /// void *buf = calloc(1,64*1024);
  1037. /// nk_init_fixed(&ctx, buf, 64*1024);
  1038. /// //
  1039. /// // loop
  1040. /// while (1) {
  1041. /// // [...input...]
  1042. /// // [...ui...]
  1043. /// void *cmds = nk_buffer_memory(&ctx.memory);
  1044. /// if (memcmp(cmds, last, ctx.memory.allocated)) {
  1045. /// memcpy(last,cmds,ctx.memory.allocated);
  1046. /// const struct nk_command *cmd = 0;
  1047. /// nk_foreach(cmd, &ctx) {
  1048. /// switch (cmd->type) {
  1049. /// case NK_COMMAND_LINE:
  1050. /// your_draw_line_function(...)
  1051. /// break;
  1052. /// case NK_COMMAND_RECT
  1053. /// your_draw_rect_function(...)
  1054. /// break;
  1055. /// case ...:
  1056. /// // [...]
  1057. /// }
  1058. /// }
  1059. /// }
  1060. /// nk_clear(&ctx);
  1061. /// }
  1062. /// nk_free(&ctx);
  1063. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1064. ///
  1065. /// Finally while using draw commands makes sense for higher abstracted platforms like
  1066. /// X11 and Win32 or drawing libraries it is often desirable to use graphics
  1067. /// hardware directly. Therefore it is possible to just define
  1068. /// `NK_INCLUDE_VERTEX_BUFFER_OUTPUT` which includes optional vertex output.
  1069. /// To access the vertex output you first have to convert all draw commands into
  1070. /// vertexes by calling `nk_convert` which takes in your preferred vertex format.
  1071. /// After successfully converting all draw commands just iterate over and execute all
  1072. /// vertex draw commands:
  1073. ///
  1074. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1075. /// // fill configuration
  1076. /// struct your_vertex
  1077. /// {
  1078. /// float pos[2]; // important to keep it to 2 floats
  1079. /// float uv[2];
  1080. /// unsigned char col[4];
  1081. /// };
  1082. /// struct nk_convert_config cfg = {};
  1083. /// static const struct nk_draw_vertex_layout_element vertex_layout[] = {
  1084. /// {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct your_vertex, pos)},
  1085. /// {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct your_vertex, uv)},
  1086. /// {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct your_vertex, col)},
  1087. /// {NK_VERTEX_LAYOUT_END}
  1088. /// };
  1089. /// cfg.shape_AA = NK_ANTI_ALIASING_ON;
  1090. /// cfg.line_AA = NK_ANTI_ALIASING_ON;
  1091. /// cfg.vertex_layout = vertex_layout;
  1092. /// cfg.vertex_size = sizeof(struct your_vertex);
  1093. /// cfg.vertex_alignment = NK_ALIGNOF(struct your_vertex);
  1094. /// cfg.circle_segment_count = 22;
  1095. /// cfg.curve_segment_count = 22;
  1096. /// cfg.arc_segment_count = 22;
  1097. /// cfg.global_alpha = 1.0f;
  1098. /// cfg.null = dev->null;
  1099. /// //
  1100. /// // setup buffers and convert
  1101. /// struct nk_buffer cmds, verts, idx;
  1102. /// nk_buffer_init_default(&cmds);
  1103. /// nk_buffer_init_default(&verts);
  1104. /// nk_buffer_init_default(&idx);
  1105. /// nk_convert(&ctx, &cmds, &verts, &idx, &cfg);
  1106. /// //
  1107. /// // draw
  1108. /// nk_draw_foreach(cmd, &ctx, &cmds) {
  1109. /// if (!cmd->elem_count) continue;
  1110. /// //[...]
  1111. /// }
  1112. /// nk_buffer_free(&cms);
  1113. /// nk_buffer_free(&verts);
  1114. /// nk_buffer_free(&idx);
  1115. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1116. ///
  1117. /// #### Reference
  1118. /// Function | Description
  1119. /// --------------------|-------------------------------------------------------
  1120. /// __nk__begin__ | Returns the first draw command in the context draw command list to be drawn
  1121. /// __nk__next__ | Increments the draw command iterator to the next command inside the context draw command list
  1122. /// __nk_foreach__ | Iterates over each draw command inside the context draw command list
  1123. /// __nk_convert__ | Converts from the abstract draw commands list into a hardware accessible vertex format
  1124. /// __nk_draw_begin__ | Returns the first vertex command in the context vertex draw list to be executed
  1125. /// __nk__draw_next__ | Increments the vertex command iterator to the next command inside the context vertex command list
  1126. /// __nk__draw_end__ | Returns the end of the vertex draw list
  1127. /// __nk_draw_foreach__ | Iterates over each vertex draw command inside the vertex draw list
  1128. */
  1129. enum nk_anti_aliasing {NK_ANTI_ALIASING_OFF, NK_ANTI_ALIASING_ON};
  1130. enum nk_convert_result {
  1131. NK_CONVERT_SUCCESS = 0,
  1132. NK_CONVERT_INVALID_PARAM = 1,
  1133. NK_CONVERT_COMMAND_BUFFER_FULL = NK_FLAG(1),
  1134. NK_CONVERT_VERTEX_BUFFER_FULL = NK_FLAG(2),
  1135. NK_CONVERT_ELEMENT_BUFFER_FULL = NK_FLAG(3)
  1136. };
  1137. struct nk_draw_null_texture {
  1138. nk_handle texture; /* texture handle to a texture with a white pixel */
  1139. struct nk_vec2 uv; /* coordinates to a white pixel in the texture */
  1140. };
  1141. struct nk_convert_config {
  1142. float global_alpha; /* global alpha value */
  1143. enum nk_anti_aliasing line_AA; /* line anti-aliasing flag can be turned off if you are tight on memory */
  1144. enum nk_anti_aliasing shape_AA; /* shape anti-aliasing flag can be turned off if you are tight on memory */
  1145. unsigned circle_segment_count; /* number of segments used for circles: default to 22 */
  1146. unsigned arc_segment_count; /* number of segments used for arcs: default to 22 */
  1147. unsigned curve_segment_count; /* number of segments used for curves: default to 22 */
  1148. struct nk_draw_null_texture null; /* handle to texture with a white pixel for shape drawing */
  1149. const struct nk_draw_vertex_layout_element *vertex_layout; /* describes the vertex output format and packing */
  1150. nk_size vertex_size; /* sizeof one vertex for vertex packing */
  1151. nk_size vertex_alignment; /* vertex alignment: Can be obtained by NK_ALIGNOF */
  1152. };
  1153. /*/// #### nk__begin
  1154. /// Returns a draw command list iterator to iterate all draw
  1155. /// commands accumulated over one frame.
  1156. ///
  1157. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1158. /// const struct nk_command* nk__begin(struct nk_context*);
  1159. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1160. ///
  1161. /// Parameter | Description
  1162. /// ------------|-----------------------------------------------------------
  1163. /// __ctx__ | must point to an previously initialized `nk_context` struct at the end of a frame
  1164. ///
  1165. /// Returns draw command pointer pointing to the first command inside the draw command list
  1166. */
  1167. NK_API const struct nk_command* nk__begin(struct nk_context*);
  1168. /*/// #### nk__next
  1169. /// Returns draw command pointer pointing to the next command inside the draw command list
  1170. ///
  1171. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1172. /// const struct nk_command* nk__next(struct nk_context*, const struct nk_command*);
  1173. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1174. ///
  1175. /// Parameter | Description
  1176. /// ------------|-----------------------------------------------------------
  1177. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1178. /// __cmd__ | Must point to an previously a draw command either returned by `nk__begin` or `nk__next`
  1179. ///
  1180. /// Returns draw command pointer pointing to the next command inside the draw command list
  1181. */
  1182. NK_API const struct nk_command* nk__next(struct nk_context*, const struct nk_command*);
  1183. /*/// #### nk_foreach
  1184. /// Iterates over each draw command inside the context draw command list
  1185. ///
  1186. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1187. /// #define nk_foreach(c, ctx)
  1188. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1189. ///
  1190. /// Parameter | Description
  1191. /// ------------|-----------------------------------------------------------
  1192. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1193. /// __cmd__ | Command pointer initialized to NULL
  1194. ///
  1195. /// Iterates over each draw command inside the context draw command list
  1196. */
  1197. #define nk_foreach(c, ctx) for((c) = nk__begin(ctx); (c) != 0; (c) = nk__next(ctx,c))
  1198. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  1199. /*/// #### nk_convert
  1200. /// Converts all internal draw commands into vertex draw commands and fills
  1201. /// three buffers with vertexes, vertex draw commands and vertex indices. The vertex format
  1202. /// as well as some other configuration values have to be configured by filling out a
  1203. /// `nk_convert_config` struct.
  1204. ///
  1205. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1206. /// nk_flags nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
  1207. /// struct nk_buffer *vertices, struct nk_buffer *elements, const struct nk_convert_config*);
  1208. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1209. ///
  1210. /// Parameter | Description
  1211. /// ------------|-----------------------------------------------------------
  1212. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1213. /// __cmds__ | Must point to a previously initialized buffer to hold converted vertex draw commands
  1214. /// __vertices__| Must point to a previously initialized buffer to hold all produced vertices
  1215. /// __elements__| Must point to a previously initialized buffer to hold all produced vertex indices
  1216. /// __config__ | Must point to a filled out `nk_config` struct to configure the conversion process
  1217. ///
  1218. /// Returns one of enum nk_convert_result error codes
  1219. ///
  1220. /// Parameter | Description
  1221. /// --------------------------------|-----------------------------------------------------------
  1222. /// NK_CONVERT_SUCCESS | Signals a successful draw command to vertex buffer conversion
  1223. /// NK_CONVERT_INVALID_PARAM | An invalid argument was passed in the function call
  1224. /// NK_CONVERT_COMMAND_BUFFER_FULL | The provided buffer for storing draw commands is full or failed to allocate more memory
  1225. /// NK_CONVERT_VERTEX_BUFFER_FULL | The provided buffer for storing vertices is full or failed to allocate more memory
  1226. /// NK_CONVERT_ELEMENT_BUFFER_FULL | The provided buffer for storing indicies is full or failed to allocate more memory
  1227. */
  1228. 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*);
  1229. /*/// #### nk__draw_begin
  1230. /// Returns a draw vertex command buffer iterator to iterate over the vertex draw command buffer
  1231. ///
  1232. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1233. /// const struct nk_draw_command* nk__draw_begin(const struct nk_context*, const struct nk_buffer*);
  1234. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1235. ///
  1236. /// Parameter | Description
  1237. /// ------------|-----------------------------------------------------------
  1238. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1239. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1240. ///
  1241. /// Returns vertex draw command pointer pointing to the first command inside the vertex draw command buffer
  1242. */
  1243. NK_API const struct nk_draw_command* nk__draw_begin(const struct nk_context*, const struct nk_buffer*);
  1244. /*/// #### nk__draw_end
  1245. /// Returns the vertex draw command at the end of the vertex draw command buffer
  1246. ///
  1247. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1248. /// const struct nk_draw_command* nk__draw_end(const struct nk_context *ctx, const struct nk_buffer *buf);
  1249. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1250. ///
  1251. /// Parameter | Description
  1252. /// ------------|-----------------------------------------------------------
  1253. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1254. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1255. ///
  1256. /// Returns vertex draw command pointer pointing to the end of the last vertex draw command inside the vertex draw command buffer
  1257. */
  1258. NK_API const struct nk_draw_command* nk__draw_end(const struct nk_context*, const struct nk_buffer*);
  1259. /*/// #### nk__draw_next
  1260. /// Increments the vertex draw command buffer iterator
  1261. ///
  1262. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1263. /// const struct nk_draw_command* nk__draw_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_context*);
  1264. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1265. ///
  1266. /// Parameter | Description
  1267. /// ------------|-----------------------------------------------------------
  1268. /// __cmd__ | Must point to an previously either by `nk__draw_begin` or `nk__draw_next` returned vertex draw command
  1269. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1270. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1271. ///
  1272. /// Returns vertex draw command pointer pointing to the end of the last vertex draw command inside the vertex draw command buffer
  1273. */
  1274. NK_API const struct nk_draw_command* nk__draw_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_context*);
  1275. /*/// #### nk_draw_foreach
  1276. /// Iterates over each vertex draw command inside a vertex draw command buffer
  1277. ///
  1278. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1279. /// #define nk_draw_foreach(cmd,ctx, b)
  1280. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1281. ///
  1282. /// Parameter | Description
  1283. /// ------------|-----------------------------------------------------------
  1284. /// __cmd__ | `nk_draw_command`iterator set to NULL
  1285. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1286. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1287. */
  1288. #define nk_draw_foreach(cmd,ctx, b) for((cmd)=nk__draw_begin(ctx, b); (cmd)!=0; (cmd)=nk__draw_next(cmd, b, ctx))
  1289. #endif
  1290. /* =============================================================================
  1291. *
  1292. * WINDOW
  1293. *
  1294. * =============================================================================
  1295. /// ### Window
  1296. /// Windows are the main persistent state used inside nuklear and are life time
  1297. /// controlled by simply "retouching" (i.e. calling) each window each frame.
  1298. /// All widgets inside nuklear can only be added inside the function pair `nk_begin_xxx`
  1299. /// and `nk_end`. Calling any widgets outside these two functions will result in an
  1300. /// assert in debug or no state change in release mode.<br /><br />
  1301. ///
  1302. /// Each window holds frame persistent state like position, size, flags, state tables,
  1303. /// and some garbage collected internal persistent widget state. Each window
  1304. /// is linked into a window stack list which determines the drawing and overlapping
  1305. /// order. The topmost window thereby is the currently active window.<br /><br />
  1306. ///
  1307. /// To change window position inside the stack occurs either automatically by
  1308. /// user input by being clicked on or programmatically by calling `nk_window_focus`.
  1309. /// Windows by default are visible unless explicitly being defined with flag
  1310. /// `NK_WINDOW_HIDDEN`, the user clicked the close button on windows with flag
  1311. /// `NK_WINDOW_CLOSABLE` or if a window was explicitly hidden by calling
  1312. /// `nk_window_show`. To explicitly close and destroy a window call `nk_window_close`.<br /><br />
  1313. ///
  1314. /// #### Usage
  1315. /// To create and keep a window you have to call one of the two `nk_begin_xxx`
  1316. /// functions to start window declarations and `nk_end` at the end. Furthermore it
  1317. /// is recommended to check the return value of `nk_begin_xxx` and only process
  1318. /// widgets inside the window if the value is not 0. Either way you have to call
  1319. /// `nk_end` at the end of window declarations. Furthermore, do not attempt to
  1320. /// nest `nk_begin_xxx` calls which will hopefully result in an assert or if not
  1321. /// in a segmentation fault.
  1322. ///
  1323. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1324. /// if (nk_begin_xxx(...) {
  1325. /// // [... widgets ...]
  1326. /// }
  1327. /// nk_end(ctx);
  1328. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1329. ///
  1330. /// In the grand concept window and widget declarations need to occur after input
  1331. /// handling and before drawing to screen. Not doing so can result in higher
  1332. /// latency or at worst invalid behavior. Furthermore make sure that `nk_clear`
  1333. /// is called at the end of the frame. While nuklear's default platform backends
  1334. /// already call `nk_clear` for you if you write your own backend not calling
  1335. /// `nk_clear` can cause asserts or even worse undefined behavior.
  1336. ///
  1337. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1338. /// struct nk_context ctx;
  1339. /// nk_init_xxx(&ctx, ...);
  1340. /// while (1) {
  1341. /// Event evt;
  1342. /// nk_input_begin(&ctx);
  1343. /// while (GetEvent(&evt)) {
  1344. /// if (evt.type == MOUSE_MOVE)
  1345. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  1346. /// else if (evt.type == [...]) {
  1347. /// nk_input_xxx(...);
  1348. /// }
  1349. /// }
  1350. /// nk_input_end(&ctx);
  1351. ///
  1352. /// if (nk_begin_xxx(...) {
  1353. /// //[...]
  1354. /// }
  1355. /// nk_end(ctx);
  1356. ///
  1357. /// const struct nk_command *cmd = 0;
  1358. /// nk_foreach(cmd, &ctx) {
  1359. /// case NK_COMMAND_LINE:
  1360. /// your_draw_line_function(...)
  1361. /// break;
  1362. /// case NK_COMMAND_RECT
  1363. /// your_draw_rect_function(...)
  1364. /// break;
  1365. /// case //...:
  1366. /// //[...]
  1367. /// }
  1368. /// nk_clear(&ctx);
  1369. /// }
  1370. /// nk_free(&ctx);
  1371. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1372. ///
  1373. /// #### Reference
  1374. /// Function | Description
  1375. /// ------------------------------------|----------------------------------------
  1376. /// nk_begin | Starts a new window; needs to be called every frame for every window (unless hidden) or otherwise the window gets removed
  1377. /// nk_begin_titled | Extended window start with separated title and identifier to allow multiple windows with same name but not title
  1378. /// nk_end | Needs to be called at the end of the window building process to process scaling, scrollbars and general cleanup
  1379. //
  1380. /// nk_window_find | Finds and returns the window with give name
  1381. /// nk_window_get_bounds | Returns a rectangle with screen position and size of the currently processed window.
  1382. /// nk_window_get_position | Returns the position of the currently processed window
  1383. /// nk_window_get_size | Returns the size with width and height of the currently processed window
  1384. /// nk_window_get_width | Returns the width of the currently processed window
  1385. /// nk_window_get_height | Returns the height of the currently processed window
  1386. /// nk_window_get_panel | Returns the underlying panel which contains all processing state of the current window
  1387. /// nk_window_get_content_region | Returns the position and size of the currently visible and non-clipped space inside the currently processed window
  1388. /// nk_window_get_content_region_min | Returns the upper rectangle position of the currently visible and non-clipped space inside the currently processed window
  1389. /// nk_window_get_content_region_max | Returns the upper rectangle position of the currently visible and non-clipped space inside the currently processed window
  1390. /// nk_window_get_content_region_size | Returns the size of the currently visible and non-clipped space inside the currently processed window
  1391. /// nk_window_get_canvas | Returns the draw command buffer. Can be used to draw custom widgets
  1392. /// nk_window_get_scroll | Gets the scroll offset of the current window
  1393. /// nk_window_has_focus | Returns if the currently processed window is currently active
  1394. /// nk_window_is_collapsed | Returns if the window with given name is currently minimized/collapsed
  1395. /// nk_window_is_closed | Returns if the currently processed window was closed
  1396. /// nk_window_is_hidden | Returns if the currently processed window was hidden
  1397. /// nk_window_is_active | Same as nk_window_has_focus for some reason
  1398. /// nk_window_is_hovered | Returns if the currently processed window is currently being hovered by mouse
  1399. /// nk_window_is_any_hovered | Return if any window currently hovered
  1400. /// nk_item_is_any_active | Returns if any window or widgets is currently hovered or active
  1401. //
  1402. /// nk_window_set_bounds | Updates position and size of the currently processed window
  1403. /// nk_window_set_position | Updates position of the currently process window
  1404. /// nk_window_set_size | Updates the size of the currently processed window
  1405. /// nk_window_set_focus | Set the currently processed window as active window
  1406. /// nk_window_set_scroll | Sets the scroll offset of the current window
  1407. //
  1408. /// nk_window_close | Closes the window with given window name which deletes the window at the end of the frame
  1409. /// nk_window_collapse | Collapses the window with given window name
  1410. /// nk_window_collapse_if | Collapses the window with given window name if the given condition was met
  1411. /// nk_window_show | Hides a visible or reshows a hidden window
  1412. /// nk_window_show_if | Hides/shows a window depending on condition
  1413. */
  1414. /*
  1415. /// #### nk_panel_flags
  1416. /// Flag | Description
  1417. /// ----------------------------|----------------------------------------
  1418. /// NK_WINDOW_BORDER | Draws a border around the window to visually separate window from the background
  1419. /// NK_WINDOW_MOVABLE | The movable flag indicates that a window can be moved by user input or by dragging the window header
  1420. /// 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
  1421. /// NK_WINDOW_CLOSABLE | Adds a closable icon into the header
  1422. /// NK_WINDOW_MINIMIZABLE | Adds a minimize icon into the header
  1423. /// NK_WINDOW_NO_SCROLLBAR | Removes the scrollbar from the window
  1424. /// NK_WINDOW_TITLE | Forces a header at the top at the window showing the title
  1425. /// 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
  1426. /// NK_WINDOW_BACKGROUND | Always keep window in the background
  1427. /// NK_WINDOW_SCALE_LEFT | Puts window scaler in the left-bottom corner instead right-bottom
  1428. /// NK_WINDOW_NO_INPUT | Prevents window of scaling, moving or getting focus
  1429. ///
  1430. /// #### nk_collapse_states
  1431. /// State | Description
  1432. /// ----------------|-----------------------------------------------------------
  1433. /// __NK_MINIMIZED__| UI section is collased and not visibile until maximized
  1434. /// __NK_MAXIMIZED__| UI section is extended and visibile until minimized
  1435. /// <br /><br />
  1436. */
  1437. enum nk_panel_flags {
  1438. NK_WINDOW_BORDER = NK_FLAG(0),
  1439. NK_WINDOW_MOVABLE = NK_FLAG(1),
  1440. NK_WINDOW_SCALABLE = NK_FLAG(2),
  1441. NK_WINDOW_CLOSABLE = NK_FLAG(3),
  1442. NK_WINDOW_MINIMIZABLE = NK_FLAG(4),
  1443. NK_WINDOW_NO_SCROLLBAR = NK_FLAG(5),
  1444. NK_WINDOW_TITLE = NK_FLAG(6),
  1445. NK_WINDOW_SCROLL_AUTO_HIDE = NK_FLAG(7),
  1446. NK_WINDOW_BACKGROUND = NK_FLAG(8),
  1447. NK_WINDOW_SCALE_LEFT = NK_FLAG(9),
  1448. NK_WINDOW_NO_INPUT = NK_FLAG(10)
  1449. };
  1450. /*/// #### nk_begin
  1451. /// Starts a new window; needs to be called every frame for every
  1452. /// window (unless hidden) or otherwise the window gets removed
  1453. ///
  1454. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1455. /// int nk_begin(struct nk_context *ctx, const char *title, struct nk_rect bounds, nk_flags flags);
  1456. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1457. ///
  1458. /// Parameter | Description
  1459. /// ------------|-----------------------------------------------------------
  1460. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1461. /// __title__ | Window title and identifier. Needs to be persistent over frames to identify the window
  1462. /// __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
  1463. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different window behaviors
  1464. ///
  1465. /// Returns `true(1)` if the window can be filled up with widgets from this point
  1466. /// until `nk_end` or `false(0)` otherwise for example if minimized
  1467. */
  1468. NK_API int nk_begin(struct nk_context *ctx, const char *title, struct nk_rect bounds, nk_flags flags);
  1469. /*/// #### nk_begin_titled
  1470. /// Extended window start with separated title and identifier to allow multiple
  1471. /// windows with same title but not name
  1472. ///
  1473. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1474. /// int nk_begin_titled(struct nk_context *ctx, const char *name, const char *title, struct nk_rect bounds, nk_flags flags);
  1475. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1476. ///
  1477. /// Parameter | Description
  1478. /// ------------|-----------------------------------------------------------
  1479. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1480. /// __name__ | Window identifier. Needs to be persistent over frames to identify the window
  1481. /// __title__ | Window title displayed inside header if flag `NK_WINDOW_TITLE` or either `NK_WINDOW_CLOSABLE` or `NK_WINDOW_MINIMIZED` was set
  1482. /// __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
  1483. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different window behaviors
  1484. ///
  1485. /// Returns `true(1)` if the window can be filled up with widgets from this point
  1486. /// until `nk_end` or `false(0)` otherwise for example if minimized
  1487. */
  1488. NK_API int nk_begin_titled(struct nk_context *ctx, const char *name, const char *title, struct nk_rect bounds, nk_flags flags);
  1489. /*/// #### nk_end
  1490. /// Needs to be called at the end of the window building process to process scaling, scrollbars and general cleanup.
  1491. /// All widget calls after this functions will result in asserts or no state changes
  1492. ///
  1493. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1494. /// void nk_end(struct nk_context *ctx);
  1495. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1496. ///
  1497. /// Parameter | Description
  1498. /// ------------|-----------------------------------------------------------
  1499. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1500. */
  1501. NK_API void nk_end(struct nk_context *ctx);
  1502. /*/// #### nk_window_find
  1503. /// Finds and returns a window from passed name
  1504. ///
  1505. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1506. /// struct nk_window *nk_window_find(struct nk_context *ctx, const char *name);
  1507. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1508. ///
  1509. /// Parameter | Description
  1510. /// ------------|-----------------------------------------------------------
  1511. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1512. /// __name__ | Window identifier
  1513. ///
  1514. /// Returns a `nk_window` struct pointing to the identified window or NULL if
  1515. /// no window with the given name was found
  1516. */
  1517. NK_API struct nk_window *nk_window_find(struct nk_context *ctx, const char *name);
  1518. /*/// #### nk_window_get_bounds
  1519. /// Returns a rectangle with screen position and size of the currently processed window
  1520. ///
  1521. /// !!! WARNING
  1522. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1523. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1524. /// struct nk_rect nk_window_get_bounds(const struct nk_context *ctx);
  1525. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1526. ///
  1527. /// Parameter | Description
  1528. /// ------------|-----------------------------------------------------------
  1529. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1530. ///
  1531. /// Returns a `nk_rect` struct with window upper left window position and size
  1532. */
  1533. NK_API struct nk_rect nk_window_get_bounds(const struct nk_context *ctx);
  1534. /*/// #### nk_window_get_position
  1535. /// Returns the position of the currently processed window.
  1536. ///
  1537. /// !!! WARNING
  1538. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1539. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1540. /// struct nk_vec2 nk_window_get_position(const struct nk_context *ctx);
  1541. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1542. ///
  1543. /// Parameter | Description
  1544. /// ------------|-----------------------------------------------------------
  1545. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1546. ///
  1547. /// Returns a `nk_vec2` struct with window upper left position
  1548. */
  1549. NK_API struct nk_vec2 nk_window_get_position(const struct nk_context *ctx);
  1550. /*/// #### nk_window_get_size
  1551. /// Returns the size with width and height of the currently processed window.
  1552. ///
  1553. /// !!! WARNING
  1554. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1555. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1556. /// struct nk_vec2 nk_window_get_size(const struct nk_context *ctx);
  1557. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1558. ///
  1559. /// Parameter | Description
  1560. /// ------------|-----------------------------------------------------------
  1561. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1562. ///
  1563. /// Returns a `nk_vec2` struct with window width and height
  1564. */
  1565. NK_API struct nk_vec2 nk_window_get_size(const struct nk_context*);
  1566. /*/// #### nk_window_get_width
  1567. /// Returns the width of the currently processed window.
  1568. ///
  1569. /// !!! WARNING
  1570. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1571. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1572. /// float nk_window_get_width(const struct nk_context *ctx);
  1573. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1574. ///
  1575. /// Parameter | Description
  1576. /// ------------|-----------------------------------------------------------
  1577. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1578. ///
  1579. /// Returns the current window width
  1580. */
  1581. NK_API float nk_window_get_width(const struct nk_context*);
  1582. /*/// #### nk_window_get_height
  1583. /// Returns the height of the currently processed window.
  1584. ///
  1585. /// !!! WARNING
  1586. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1587. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1588. /// float nk_window_get_height(const struct nk_context *ctx);
  1589. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1590. ///
  1591. /// Parameter | Description
  1592. /// ------------|-----------------------------------------------------------
  1593. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1594. ///
  1595. /// Returns the current window height
  1596. */
  1597. NK_API float nk_window_get_height(const struct nk_context*);
  1598. /*/// #### nk_window_get_panel
  1599. /// Returns the underlying panel which contains all processing state of the current window.
  1600. ///
  1601. /// !!! WARNING
  1602. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1603. /// !!! WARNING
  1604. /// Do not keep the returned panel pointer around, it is only valid until `nk_end`
  1605. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1606. /// struct nk_panel* nk_window_get_panel(struct nk_context *ctx);
  1607. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1608. ///
  1609. /// Parameter | Description
  1610. /// ------------|-----------------------------------------------------------
  1611. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1612. ///
  1613. /// Returns a pointer to window internal `nk_panel` state.
  1614. */
  1615. NK_API struct nk_panel* nk_window_get_panel(struct nk_context*);
  1616. /*/// #### nk_window_get_content_region
  1617. /// Returns the position and size of the currently visible and non-clipped space
  1618. /// inside the currently processed window.
  1619. ///
  1620. /// !!! WARNING
  1621. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1622. ///
  1623. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1624. /// struct nk_rect nk_window_get_content_region(struct nk_context *ctx);
  1625. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1626. ///
  1627. /// Parameter | Description
  1628. /// ------------|-----------------------------------------------------------
  1629. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1630. ///
  1631. /// Returns `nk_rect` struct with screen position and size (no scrollbar offset)
  1632. /// of the visible space inside the current window
  1633. */
  1634. NK_API struct nk_rect nk_window_get_content_region(struct nk_context*);
  1635. /*/// #### nk_window_get_content_region_min
  1636. /// Returns the upper left position of the currently visible and non-clipped
  1637. /// space inside the currently processed window.
  1638. ///
  1639. /// !!! WARNING
  1640. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1641. ///
  1642. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1643. /// struct nk_vec2 nk_window_get_content_region_min(struct nk_context *ctx);
  1644. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1645. ///
  1646. /// Parameter | Description
  1647. /// ------------|-----------------------------------------------------------
  1648. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1649. ///
  1650. /// returns `nk_vec2` struct with upper left screen position (no scrollbar offset)
  1651. /// of the visible space inside the current window
  1652. */
  1653. NK_API struct nk_vec2 nk_window_get_content_region_min(struct nk_context*);
  1654. /*/// #### nk_window_get_content_region_max
  1655. /// Returns the lower right screen position of the currently visible and
  1656. /// non-clipped space inside the currently processed window.
  1657. ///
  1658. /// !!! WARNING
  1659. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1660. ///
  1661. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1662. /// struct nk_vec2 nk_window_get_content_region_max(struct nk_context *ctx);
  1663. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1664. ///
  1665. /// Parameter | Description
  1666. /// ------------|-----------------------------------------------------------
  1667. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1668. ///
  1669. /// Returns `nk_vec2` struct with lower right screen position (no scrollbar offset)
  1670. /// of the visible space inside the current window
  1671. */
  1672. NK_API struct nk_vec2 nk_window_get_content_region_max(struct nk_context*);
  1673. /*/// #### nk_window_get_content_region_size
  1674. /// Returns the size of the currently visible and non-clipped space inside the
  1675. /// currently processed window
  1676. ///
  1677. /// !!! WARNING
  1678. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1679. ///
  1680. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1681. /// struct nk_vec2 nk_window_get_content_region_size(struct nk_context *ctx);
  1682. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1683. ///
  1684. /// Parameter | Description
  1685. /// ------------|-----------------------------------------------------------
  1686. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1687. ///
  1688. /// Returns `nk_vec2` struct with size the visible space inside the current window
  1689. */
  1690. NK_API struct nk_vec2 nk_window_get_content_region_size(struct nk_context*);
  1691. /*/// #### nk_window_get_canvas
  1692. /// Returns the draw command buffer. Can be used to draw custom widgets
  1693. /// !!! WARNING
  1694. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1695. /// !!! WARNING
  1696. /// Do not keep the returned command buffer pointer around it is only valid until `nk_end`
  1697. ///
  1698. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1699. /// struct nk_command_buffer* nk_window_get_canvas(struct nk_context *ctx);
  1700. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1701. ///
  1702. /// Parameter | Description
  1703. /// ------------|-----------------------------------------------------------
  1704. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1705. ///
  1706. /// Returns a pointer to window internal `nk_command_buffer` struct used as
  1707. /// drawing canvas. Can be used to do custom drawing.
  1708. */
  1709. NK_API struct nk_command_buffer* nk_window_get_canvas(struct nk_context*);
  1710. /*/// #### nk_window_get_scroll
  1711. /// Gets the scroll offset for the current window
  1712. /// !!! WARNING
  1713. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1714. ///
  1715. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1716. /// void nk_window_get_scroll(struct nk_context *ctx, nk_uint *offset_x, nk_uint *offset_y);
  1717. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1718. ///
  1719. /// Parameter | Description
  1720. /// -------------|-----------------------------------------------------------
  1721. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1722. /// __offset_x__ | A pointer to the x offset output (or NULL to ignore)
  1723. /// __offset_y__ | A pointer to the y offset output (or NULL to ignore)
  1724. */
  1725. NK_API void nk_window_get_scroll(struct nk_context*, nk_uint *offset_x, nk_uint *offset_y);
  1726. /*/// #### nk_window_has_focus
  1727. /// Returns if the currently processed window is currently active
  1728. /// !!! WARNING
  1729. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1730. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1731. /// int nk_window_has_focus(const struct nk_context *ctx);
  1732. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1733. ///
  1734. /// Parameter | Description
  1735. /// ------------|-----------------------------------------------------------
  1736. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1737. ///
  1738. /// Returns `false(0)` if current window is not active or `true(1)` if it is
  1739. */
  1740. NK_API int nk_window_has_focus(const struct nk_context*);
  1741. /*/// #### nk_window_is_hovered
  1742. /// Return if the current window is being hovered
  1743. /// !!! WARNING
  1744. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1745. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1746. /// int nk_window_is_hovered(struct nk_context *ctx);
  1747. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1748. ///
  1749. /// Parameter | Description
  1750. /// ------------|-----------------------------------------------------------
  1751. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1752. ///
  1753. /// Returns `true(1)` if current window is hovered or `false(0)` otherwise
  1754. */
  1755. NK_API int nk_window_is_hovered(struct nk_context*);
  1756. /*/// #### nk_window_is_collapsed
  1757. /// Returns if the window with given name is currently minimized/collapsed
  1758. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1759. /// int nk_window_is_collapsed(struct nk_context *ctx, const char *name);
  1760. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1761. ///
  1762. /// Parameter | Description
  1763. /// ------------|-----------------------------------------------------------
  1764. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1765. /// __name__ | Identifier of window you want to check if it is collapsed
  1766. ///
  1767. /// Returns `true(1)` if current window is minimized and `false(0)` if window not
  1768. /// found or is not minimized
  1769. */
  1770. NK_API int nk_window_is_collapsed(struct nk_context *ctx, const char *name);
  1771. /*/// #### nk_window_is_closed
  1772. /// Returns if the window with given name was closed by calling `nk_close`
  1773. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1774. /// int nk_window_is_closed(struct nk_context *ctx, const char *name);
  1775. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1776. ///
  1777. /// Parameter | Description
  1778. /// ------------|-----------------------------------------------------------
  1779. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1780. /// __name__ | Identifier of window you want to check if it is closed
  1781. ///
  1782. /// Returns `true(1)` if current window was closed or `false(0)` window not found or not closed
  1783. */
  1784. NK_API int nk_window_is_closed(struct nk_context*, const char*);
  1785. /*/// #### nk_window_is_hidden
  1786. /// Returns if the window with given name is hidden
  1787. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1788. /// int nk_window_is_hidden(struct nk_context *ctx, const char *name);
  1789. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1790. ///
  1791. /// Parameter | Description
  1792. /// ------------|-----------------------------------------------------------
  1793. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1794. /// __name__ | Identifier of window you want to check if it is hidden
  1795. ///
  1796. /// Returns `true(1)` if current window is hidden or `false(0)` window not found or visible
  1797. */
  1798. NK_API int nk_window_is_hidden(struct nk_context*, const char*);
  1799. /*/// #### nk_window_is_active
  1800. /// Same as nk_window_has_focus for some reason
  1801. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1802. /// int nk_window_is_active(struct nk_context *ctx, const char *name);
  1803. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1804. ///
  1805. /// Parameter | Description
  1806. /// ------------|-----------------------------------------------------------
  1807. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1808. /// __name__ | Identifier of window you want to check if it is active
  1809. ///
  1810. /// Returns `true(1)` if current window is active or `false(0)` window not found or not active
  1811. */
  1812. NK_API int nk_window_is_active(struct nk_context*, const char*);
  1813. /*/// #### nk_window_is_any_hovered
  1814. /// Returns if the any window is being hovered
  1815. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1816. /// int nk_window_is_any_hovered(struct nk_context*);
  1817. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1818. ///
  1819. /// Parameter | Description
  1820. /// ------------|-----------------------------------------------------------
  1821. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1822. ///
  1823. /// Returns `true(1)` if any window is hovered or `false(0)` otherwise
  1824. */
  1825. NK_API int nk_window_is_any_hovered(struct nk_context*);
  1826. /*/// #### nk_item_is_any_active
  1827. /// Returns if the any window is being hovered or any widget is currently active.
  1828. /// Can be used to decide if input should be processed by UI or your specific input handling.
  1829. /// Example could be UI and 3D camera to move inside a 3D space.
  1830. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1831. /// int nk_item_is_any_active(struct nk_context*);
  1832. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1833. ///
  1834. /// Parameter | Description
  1835. /// ------------|-----------------------------------------------------------
  1836. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1837. ///
  1838. /// Returns `true(1)` if any window is hovered or any item is active or `false(0)` otherwise
  1839. */
  1840. NK_API int nk_item_is_any_active(struct nk_context*);
  1841. /*/// #### nk_window_set_bounds
  1842. /// Updates position and size of window with passed in name
  1843. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1844. /// void nk_window_set_bounds(struct nk_context*, const char *name, struct nk_rect bounds);
  1845. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1846. ///
  1847. /// Parameter | Description
  1848. /// ------------|-----------------------------------------------------------
  1849. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1850. /// __name__ | Identifier of the window to modify both position and size
  1851. /// __bounds__ | Must point to a `nk_rect` struct with the new position and size
  1852. */
  1853. NK_API void nk_window_set_bounds(struct nk_context*, const char *name, struct nk_rect bounds);
  1854. /*/// #### nk_window_set_position
  1855. /// Updates position of window with passed name
  1856. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1857. /// void nk_window_set_position(struct nk_context*, const char *name, struct nk_vec2 pos);
  1858. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1859. ///
  1860. /// Parameter | Description
  1861. /// ------------|-----------------------------------------------------------
  1862. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1863. /// __name__ | Identifier of the window to modify both position
  1864. /// __pos__ | Must point to a `nk_vec2` struct with the new position
  1865. */
  1866. NK_API void nk_window_set_position(struct nk_context*, const char *name, struct nk_vec2 pos);
  1867. /*/// #### nk_window_set_size
  1868. /// Updates size of window with passed in name
  1869. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1870. /// void nk_window_set_size(struct nk_context*, const char *name, struct nk_vec2);
  1871. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1872. ///
  1873. /// Parameter | Description
  1874. /// ------------|-----------------------------------------------------------
  1875. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1876. /// __name__ | Identifier of the window to modify both window size
  1877. /// __size__ | Must point to a `nk_vec2` struct with new window size
  1878. */
  1879. NK_API void nk_window_set_size(struct nk_context*, const char *name, struct nk_vec2);
  1880. /*/// #### nk_window_set_focus
  1881. /// Sets the window with given name as active
  1882. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1883. /// void nk_window_set_focus(struct nk_context*, const char *name);
  1884. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1885. ///
  1886. /// Parameter | Description
  1887. /// ------------|-----------------------------------------------------------
  1888. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1889. /// __name__ | Identifier of the window to set focus on
  1890. */
  1891. NK_API void nk_window_set_focus(struct nk_context*, const char *name);
  1892. /*/// #### nk_window_set_scroll
  1893. /// Sets the scroll offset for the current window
  1894. /// !!! WARNING
  1895. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1896. ///
  1897. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1898. /// void nk_window_set_scroll(struct nk_context *ctx, nk_uint offset_x, nk_uint offset_y);
  1899. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1900. ///
  1901. /// Parameter | Description
  1902. /// -------------|-----------------------------------------------------------
  1903. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1904. /// __offset_x__ | The x offset to scroll to
  1905. /// __offset_y__ | The y offset to scroll to
  1906. */
  1907. NK_API void nk_window_set_scroll(struct nk_context*, nk_uint offset_x, nk_uint offset_y);
  1908. /*/// #### nk_window_close
  1909. /// Closes a window and marks it for being freed at the end of the frame
  1910. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1911. /// void nk_window_close(struct nk_context *ctx, const char *name);
  1912. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1913. ///
  1914. /// Parameter | Description
  1915. /// ------------|-----------------------------------------------------------
  1916. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1917. /// __name__ | Identifier of the window to close
  1918. */
  1919. NK_API void nk_window_close(struct nk_context *ctx, const char *name);
  1920. /*/// #### nk_window_collapse
  1921. /// Updates collapse state of a window with given name
  1922. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1923. /// void nk_window_collapse(struct nk_context*, const char *name, enum nk_collapse_states state);
  1924. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1925. ///
  1926. /// Parameter | Description
  1927. /// ------------|-----------------------------------------------------------
  1928. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1929. /// __name__ | Identifier of the window to close
  1930. /// __state__ | value out of nk_collapse_states section
  1931. */
  1932. NK_API void nk_window_collapse(struct nk_context*, const char *name, enum nk_collapse_states state);
  1933. /*/// #### nk_window_collapse_if
  1934. /// Updates collapse state of a window with given name if given condition is met
  1935. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1936. /// void nk_window_collapse_if(struct nk_context*, const char *name, enum nk_collapse_states, int cond);
  1937. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1938. ///
  1939. /// Parameter | Description
  1940. /// ------------|-----------------------------------------------------------
  1941. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1942. /// __name__ | Identifier of the window to either collapse or maximize
  1943. /// __state__ | value out of nk_collapse_states section the window should be put into
  1944. /// __cond__ | condition that has to be met to actually commit the collapse state change
  1945. */
  1946. NK_API void nk_window_collapse_if(struct nk_context*, const char *name, enum nk_collapse_states, int cond);
  1947. /*/// #### nk_window_show
  1948. /// updates visibility state of a window with given name
  1949. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1950. /// void nk_window_show(struct nk_context*, const char *name, enum nk_show_states);
  1951. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1952. ///
  1953. /// Parameter | Description
  1954. /// ------------|-----------------------------------------------------------
  1955. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1956. /// __name__ | Identifier of the window to either collapse or maximize
  1957. /// __state__ | state with either visible or hidden to modify the window with
  1958. */
  1959. NK_API void nk_window_show(struct nk_context*, const char *name, enum nk_show_states);
  1960. /*/// #### nk_window_show_if
  1961. /// Updates visibility state of a window with given name if a given condition is met
  1962. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1963. /// void nk_window_show_if(struct nk_context*, const char *name, enum nk_show_states, int cond);
  1964. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1965. ///
  1966. /// Parameter | Description
  1967. /// ------------|-----------------------------------------------------------
  1968. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1969. /// __name__ | Identifier of the window to either hide or show
  1970. /// __state__ | state with either visible or hidden to modify the window with
  1971. /// __cond__ | condition that has to be met to actually commit the visbility state change
  1972. */
  1973. NK_API void nk_window_show_if(struct nk_context*, const char *name, enum nk_show_states, int cond);
  1974. /* =============================================================================
  1975. *
  1976. * LAYOUT
  1977. *
  1978. * =============================================================================
  1979. /// ### Layouting
  1980. /// Layouting in general describes placing widget inside a window with position and size.
  1981. /// While in this particular implementation there are five different APIs for layouting
  1982. /// each with different trade offs between control and ease of use. <br /><br />
  1983. ///
  1984. /// All layouting methods in this library are based around the concept of a row.
  1985. /// A row has a height the window content grows by and a number of columns and each
  1986. /// layouting method specifies how each widget is placed inside the row.
  1987. /// After a row has been allocated by calling a layouting functions and then
  1988. /// filled with widgets will advance an internal pointer over the allocated row. <br /><br />
  1989. ///
  1990. /// To actually define a layout you just call the appropriate layouting function
  1991. /// and each subsequent widget call will place the widget as specified. Important
  1992. /// here is that if you define more widgets then columns defined inside the layout
  1993. /// functions it will allocate the next row without you having to make another layouting <br /><br />
  1994. /// call.
  1995. ///
  1996. /// Biggest limitation with using all these APIs outside the `nk_layout_space_xxx` API
  1997. /// is that you have to define the row height for each. However the row height
  1998. /// often depends on the height of the font. <br /><br />
  1999. ///
  2000. /// To fix that internally nuklear uses a minimum row height that is set to the
  2001. /// height plus padding of currently active font and overwrites the row height
  2002. /// value if zero. <br /><br />
  2003. ///
  2004. /// If you manually want to change the minimum row height then
  2005. /// use nk_layout_set_min_row_height, and use nk_layout_reset_min_row_height to
  2006. /// reset it back to be derived from font height. <br /><br />
  2007. ///
  2008. /// Also if you change the font in nuklear it will automatically change the minimum
  2009. /// row height for you and. This means if you change the font but still want
  2010. /// a minimum row height smaller than the font you have to repush your value. <br /><br />
  2011. ///
  2012. /// For actually more advanced UI I would even recommend using the `nk_layout_space_xxx`
  2013. /// layouting method in combination with a cassowary constraint solver (there are
  2014. /// some versions on github with permissive license model) to take over all control over widget
  2015. /// layouting yourself. However for quick and dirty layouting using all the other layouting
  2016. /// functions should be fine.
  2017. ///
  2018. /// #### Usage
  2019. /// 1. __nk_layout_row_dynamic__<br /><br />
  2020. /// The easiest layouting function is `nk_layout_row_dynamic`. It provides each
  2021. /// widgets with same horizontal space inside the row and dynamically grows
  2022. /// if the owning window grows in width. So the number of columns dictates
  2023. /// the size of each widget dynamically by formula:
  2024. ///
  2025. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2026. /// widget_width = (window_width - padding - spacing) * (1/colum_count)
  2027. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2028. ///
  2029. /// Just like all other layouting APIs if you define more widget than columns this
  2030. /// library will allocate a new row and keep all layouting parameters previously
  2031. /// defined.
  2032. ///
  2033. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2034. /// if (nk_begin_xxx(...) {
  2035. /// // first row with height: 30 composed of two widgets
  2036. /// nk_layout_row_dynamic(&ctx, 30, 2);
  2037. /// nk_widget(...);
  2038. /// nk_widget(...);
  2039. /// //
  2040. /// // second row with same parameter as defined above
  2041. /// nk_widget(...);
  2042. /// nk_widget(...);
  2043. /// //
  2044. /// // third row uses 0 for height which will use auto layouting
  2045. /// nk_layout_row_dynamic(&ctx, 0, 2);
  2046. /// nk_widget(...);
  2047. /// nk_widget(...);
  2048. /// }
  2049. /// nk_end(...);
  2050. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2051. ///
  2052. /// 2. __nk_layout_row_static__<br /><br />
  2053. /// Another easy layouting function is `nk_layout_row_static`. It provides each
  2054. /// widget with same horizontal pixel width inside the row and does not grow
  2055. /// if the owning window scales smaller or bigger.
  2056. ///
  2057. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2058. /// if (nk_begin_xxx(...) {
  2059. /// // first row with height: 30 composed of two widgets with width: 80
  2060. /// nk_layout_row_static(&ctx, 30, 80, 2);
  2061. /// nk_widget(...);
  2062. /// nk_widget(...);
  2063. /// //
  2064. /// // second row with same parameter as defined above
  2065. /// nk_widget(...);
  2066. /// nk_widget(...);
  2067. /// //
  2068. /// // third row uses 0 for height which will use auto layouting
  2069. /// nk_layout_row_static(&ctx, 0, 80, 2);
  2070. /// nk_widget(...);
  2071. /// nk_widget(...);
  2072. /// }
  2073. /// nk_end(...);
  2074. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2075. ///
  2076. /// 3. __nk_layout_row_xxx__<br /><br />
  2077. /// A little bit more advanced layouting API are functions `nk_layout_row_begin`,
  2078. /// `nk_layout_row_push` and `nk_layout_row_end`. They allow to directly
  2079. /// specify each column pixel or window ratio in a row. It supports either
  2080. /// directly setting per column pixel width or widget window ratio but not
  2081. /// both. Furthermore it is a immediate mode API so each value is directly
  2082. /// pushed before calling a widget. Therefore the layout is not automatically
  2083. /// repeating like the last two layouting functions.
  2084. ///
  2085. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2086. /// if (nk_begin_xxx(...) {
  2087. /// // first row with height: 25 composed of two widgets with width 60 and 40
  2088. /// nk_layout_row_begin(ctx, NK_STATIC, 25, 2);
  2089. /// nk_layout_row_push(ctx, 60);
  2090. /// nk_widget(...);
  2091. /// nk_layout_row_push(ctx, 40);
  2092. /// nk_widget(...);
  2093. /// nk_layout_row_end(ctx);
  2094. /// //
  2095. /// // second row with height: 25 composed of two widgets with window ratio 0.25 and 0.75
  2096. /// nk_layout_row_begin(ctx, NK_DYNAMIC, 25, 2);
  2097. /// nk_layout_row_push(ctx, 0.25f);
  2098. /// nk_widget(...);
  2099. /// nk_layout_row_push(ctx, 0.75f);
  2100. /// nk_widget(...);
  2101. /// nk_layout_row_end(ctx);
  2102. /// //
  2103. /// // third row with auto generated height: composed of two widgets with window ratio 0.25 and 0.75
  2104. /// nk_layout_row_begin(ctx, NK_DYNAMIC, 0, 2);
  2105. /// nk_layout_row_push(ctx, 0.25f);
  2106. /// nk_widget(...);
  2107. /// nk_layout_row_push(ctx, 0.75f);
  2108. /// nk_widget(...);
  2109. /// nk_layout_row_end(ctx);
  2110. /// }
  2111. /// nk_end(...);
  2112. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2113. ///
  2114. /// 4. __nk_layout_row__<br /><br />
  2115. /// The array counterpart to API nk_layout_row_xxx is the single nk_layout_row
  2116. /// functions. Instead of pushing either pixel or window ratio for every widget
  2117. /// it allows to define it by array. The trade of for less control is that
  2118. /// `nk_layout_row` is automatically repeating. Otherwise the behavior is the
  2119. /// same.
  2120. ///
  2121. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2122. /// if (nk_begin_xxx(...) {
  2123. /// // two rows with height: 30 composed of two widgets with width 60 and 40
  2124. /// const float size[] = {60,40};
  2125. /// nk_layout_row(ctx, NK_STATIC, 30, 2, ratio);
  2126. /// nk_widget(...);
  2127. /// nk_widget(...);
  2128. /// nk_widget(...);
  2129. /// nk_widget(...);
  2130. /// //
  2131. /// // two rows with height: 30 composed of two widgets with window ratio 0.25 and 0.75
  2132. /// const float ratio[] = {0.25, 0.75};
  2133. /// nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio);
  2134. /// nk_widget(...);
  2135. /// nk_widget(...);
  2136. /// nk_widget(...);
  2137. /// nk_widget(...);
  2138. /// //
  2139. /// // two rows with auto generated height composed of two widgets with window ratio 0.25 and 0.75
  2140. /// const float ratio[] = {0.25, 0.75};
  2141. /// nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio);
  2142. /// nk_widget(...);
  2143. /// nk_widget(...);
  2144. /// nk_widget(...);
  2145. /// nk_widget(...);
  2146. /// }
  2147. /// nk_end(...);
  2148. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2149. ///
  2150. /// 5. __nk_layout_row_template_xxx__<br /><br />
  2151. /// The most complex and second most flexible API is a simplified flexbox version without
  2152. /// line wrapping and weights for dynamic widgets. It is an immediate mode API but
  2153. /// unlike `nk_layout_row_xxx` it has auto repeat behavior and needs to be called
  2154. /// before calling the templated widgets.
  2155. /// The row template layout has three different per widget size specifier. The first
  2156. /// one is the `nk_layout_row_template_push_static` with fixed widget pixel width.
  2157. /// They do not grow if the row grows and will always stay the same.
  2158. /// The second size specifier is `nk_layout_row_template_push_variable`
  2159. /// which defines a minimum widget size but it also can grow if more space is available
  2160. /// not taken by other widgets.
  2161. /// Finally there are dynamic widgets with `nk_layout_row_template_push_dynamic`
  2162. /// which are completely flexible and unlike variable widgets can even shrink
  2163. /// to zero if not enough space is provided.
  2164. ///
  2165. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2166. /// if (nk_begin_xxx(...) {
  2167. /// // two rows with height: 30 composed of three widgets
  2168. /// nk_layout_row_template_begin(ctx, 30);
  2169. /// nk_layout_row_template_push_dynamic(ctx);
  2170. /// nk_layout_row_template_push_variable(ctx, 80);
  2171. /// nk_layout_row_template_push_static(ctx, 80);
  2172. /// nk_layout_row_template_end(ctx);
  2173. /// //
  2174. /// // first row
  2175. /// nk_widget(...); // dynamic widget can go to zero if not enough space
  2176. /// nk_widget(...); // variable widget with min 80 pixel but can grow bigger if enough space
  2177. /// nk_widget(...); // static widget with fixed 80 pixel width
  2178. /// //
  2179. /// // second row same layout
  2180. /// nk_widget(...);
  2181. /// nk_widget(...);
  2182. /// nk_widget(...);
  2183. /// }
  2184. /// nk_end(...);
  2185. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2186. ///
  2187. /// 6. __nk_layout_space_xxx__<br /><br />
  2188. /// Finally the most flexible API directly allows you to place widgets inside the
  2189. /// window. The space layout API is an immediate mode API which does not support
  2190. /// row auto repeat and directly sets position and size of a widget. Position
  2191. /// and size hereby can be either specified as ratio of allocated space or
  2192. /// allocated space local position and pixel size. Since this API is quite
  2193. /// powerful there are a number of utility functions to get the available space
  2194. /// and convert between local allocated space and screen space.
  2195. ///
  2196. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2197. /// if (nk_begin_xxx(...) {
  2198. /// // static row with height: 500 (you can set column count to INT_MAX if you don't want to be bothered)
  2199. /// nk_layout_space_begin(ctx, NK_STATIC, 500, INT_MAX);
  2200. /// nk_layout_space_push(ctx, nk_rect(0,0,150,200));
  2201. /// nk_widget(...);
  2202. /// nk_layout_space_push(ctx, nk_rect(200,200,100,200));
  2203. /// nk_widget(...);
  2204. /// nk_layout_space_end(ctx);
  2205. /// //
  2206. /// // dynamic row with height: 500 (you can set column count to INT_MAX if you don't want to be bothered)
  2207. /// nk_layout_space_begin(ctx, NK_DYNAMIC, 500, INT_MAX);
  2208. /// nk_layout_space_push(ctx, nk_rect(0.5,0.5,0.1,0.1));
  2209. /// nk_widget(...);
  2210. /// nk_layout_space_push(ctx, nk_rect(0.7,0.6,0.1,0.1));
  2211. /// nk_widget(...);
  2212. /// }
  2213. /// nk_end(...);
  2214. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2215. ///
  2216. /// #### Reference
  2217. /// Function | Description
  2218. /// ----------------------------------------|------------------------------------
  2219. /// nk_layout_set_min_row_height | Set the currently used minimum row height to a specified value
  2220. /// nk_layout_reset_min_row_height | Resets the currently used minimum row height to font height
  2221. /// nk_layout_widget_bounds | Calculates current width a static layout row can fit inside a window
  2222. /// nk_layout_ratio_from_pixel | Utility functions to calculate window ratio from pixel size
  2223. //
  2224. /// nk_layout_row_dynamic | Current layout is divided into n same sized growing columns
  2225. /// nk_layout_row_static | Current layout is divided into n same fixed sized columns
  2226. /// nk_layout_row_begin | Starts a new row with given height and number of columns
  2227. /// nk_layout_row_push | Pushes another column with given size or window ratio
  2228. /// nk_layout_row_end | Finished previously started row
  2229. /// nk_layout_row | Specifies row columns in array as either window ratio or size
  2230. //
  2231. /// nk_layout_row_template_begin | Begins the row template declaration
  2232. /// nk_layout_row_template_push_dynamic | Adds a dynamic column that dynamically grows and can go to zero if not enough space
  2233. /// nk_layout_row_template_push_variable | Adds a variable column that dynamically grows but does not shrink below specified pixel width
  2234. /// nk_layout_row_template_push_static | Adds a static column that does not grow and will always have the same size
  2235. /// nk_layout_row_template_end | Marks the end of the row template
  2236. //
  2237. /// nk_layout_space_begin | Begins a new layouting space that allows to specify each widgets position and size
  2238. /// nk_layout_space_push | Pushes position and size of the next widget in own coordinate space either as pixel or ratio
  2239. /// nk_layout_space_end | Marks the end of the layouting space
  2240. //
  2241. /// nk_layout_space_bounds | Callable after nk_layout_space_begin and returns total space allocated
  2242. /// nk_layout_space_to_screen | Converts vector from nk_layout_space coordinate space into screen space
  2243. /// nk_layout_space_to_local | Converts vector from screen space into nk_layout_space coordinates
  2244. /// nk_layout_space_rect_to_screen | Converts rectangle from nk_layout_space coordinate space into screen space
  2245. /// nk_layout_space_rect_to_local | Converts rectangle from screen space into nk_layout_space coordinates
  2246. */
  2247. /*/// #### nk_layout_set_min_row_height
  2248. /// Sets the currently used minimum row height.
  2249. /// !!! WARNING
  2250. /// The passed height needs to include both your preferred row height
  2251. /// as well as padding. No internal padding is added.
  2252. ///
  2253. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2254. /// void nk_layout_set_min_row_height(struct nk_context*, float height);
  2255. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2256. ///
  2257. /// Parameter | Description
  2258. /// ------------|-----------------------------------------------------------
  2259. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2260. /// __height__ | New minimum row height to be used for auto generating the row height
  2261. */
  2262. NK_API void nk_layout_set_min_row_height(struct nk_context*, float height);
  2263. /*/// #### nk_layout_reset_min_row_height
  2264. /// Reset the currently used minimum row height back to `font_height + text_padding + padding`
  2265. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2266. /// void nk_layout_reset_min_row_height(struct nk_context*);
  2267. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2268. ///
  2269. /// Parameter | Description
  2270. /// ------------|-----------------------------------------------------------
  2271. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2272. */
  2273. NK_API void nk_layout_reset_min_row_height(struct nk_context*);
  2274. /*/// #### nk_layout_widget_bounds
  2275. /// Returns the width of the next row allocate by one of the layouting functions
  2276. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2277. /// struct nk_rect nk_layout_widget_bounds(struct nk_context*);
  2278. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2279. ///
  2280. /// Parameter | Description
  2281. /// ------------|-----------------------------------------------------------
  2282. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2283. ///
  2284. /// Return `nk_rect` with both position and size of the next row
  2285. */
  2286. NK_API struct nk_rect nk_layout_widget_bounds(struct nk_context*);
  2287. /*/// #### nk_layout_ratio_from_pixel
  2288. /// Utility functions to calculate window ratio from pixel size
  2289. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2290. /// float nk_layout_ratio_from_pixel(struct nk_context*, float pixel_width);
  2291. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2292. ///
  2293. /// Parameter | Description
  2294. /// ------------|-----------------------------------------------------------
  2295. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2296. /// __pixel__ | Pixel_width to convert to window ratio
  2297. ///
  2298. /// Returns `nk_rect` with both position and size of the next row
  2299. */
  2300. NK_API float nk_layout_ratio_from_pixel(struct nk_context*, float pixel_width);
  2301. /*/// #### nk_layout_row_dynamic
  2302. /// Sets current row layout to share horizontal space
  2303. /// between @cols number of widgets evenly. Once called all subsequent widget
  2304. /// calls greater than @cols will allocate a new row with same layout.
  2305. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2306. /// void nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols);
  2307. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2308. ///
  2309. /// Parameter | Description
  2310. /// ------------|-----------------------------------------------------------
  2311. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2312. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2313. /// __columns__ | Number of widget inside row
  2314. */
  2315. NK_API void nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols);
  2316. /*/// #### nk_layout_row_static
  2317. /// Sets current row layout to fill @cols number of widgets
  2318. /// in row with same @item_width horizontal size. Once called all subsequent widget
  2319. /// calls greater than @cols will allocate a new row with same layout.
  2320. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2321. /// void nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols);
  2322. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2323. ///
  2324. /// Parameter | Description
  2325. /// ------------|-----------------------------------------------------------
  2326. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2327. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2328. /// __width__ | Holds pixel width of each widget in the row
  2329. /// __columns__ | Number of widget inside row
  2330. */
  2331. NK_API void nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols);
  2332. /*/// #### nk_layout_row_begin
  2333. /// Starts a new dynamic or fixed row with given height and columns.
  2334. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2335. /// void nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt, float row_height, int cols);
  2336. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2337. ///
  2338. /// Parameter | Description
  2339. /// ------------|-----------------------------------------------------------
  2340. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2341. /// __fmt__ | either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  2342. /// __height__ | holds height of each widget in row or zero for auto layouting
  2343. /// __columns__ | Number of widget inside row
  2344. */
  2345. NK_API void nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt, float row_height, int cols);
  2346. /*/// #### nk_layout_row_push
  2347. /// Specifies either window ratio or width of a single column
  2348. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2349. /// void nk_layout_row_push(struct nk_context*, float value);
  2350. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2351. ///
  2352. /// Parameter | Description
  2353. /// ------------|-----------------------------------------------------------
  2354. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2355. /// __value__ | either a window ratio or fixed width depending on @fmt in previous `nk_layout_row_begin` call
  2356. */
  2357. NK_API void nk_layout_row_push(struct nk_context*, float value);
  2358. /*/// #### nk_layout_row_end
  2359. /// Finished previously started row
  2360. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2361. /// void nk_layout_row_end(struct nk_context*);
  2362. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2363. ///
  2364. /// Parameter | Description
  2365. /// ------------|-----------------------------------------------------------
  2366. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2367. */
  2368. NK_API void nk_layout_row_end(struct nk_context*);
  2369. /*/// #### nk_layout_row
  2370. /// Specifies row columns in array as either window ratio or size
  2371. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2372. /// void nk_layout_row(struct nk_context*, enum nk_layout_format, float height, int cols, const float *ratio);
  2373. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2374. ///
  2375. /// Parameter | Description
  2376. /// ------------|-----------------------------------------------------------
  2377. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2378. /// __fmt__ | Either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  2379. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2380. /// __columns__ | Number of widget inside row
  2381. */
  2382. NK_API void nk_layout_row(struct nk_context*, enum nk_layout_format, float height, int cols, const float *ratio);
  2383. /*/// #### nk_layout_row_template_begin
  2384. /// Begins the row template declaration
  2385. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2386. /// void nk_layout_row_template_begin(struct nk_context*, float row_height);
  2387. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2388. ///
  2389. /// Parameter | Description
  2390. /// ------------|-----------------------------------------------------------
  2391. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2392. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2393. */
  2394. NK_API void nk_layout_row_template_begin(struct nk_context*, float row_height);
  2395. /*/// #### nk_layout_row_template_push_dynamic
  2396. /// Adds a dynamic column that dynamically grows and can go to zero if not enough space
  2397. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2398. /// void nk_layout_row_template_push_dynamic(struct nk_context*);
  2399. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2400. ///
  2401. /// Parameter | Description
  2402. /// ------------|-----------------------------------------------------------
  2403. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2404. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2405. */
  2406. NK_API void nk_layout_row_template_push_dynamic(struct nk_context*);
  2407. /*/// #### nk_layout_row_template_push_variable
  2408. /// Adds a variable column that dynamically grows but does not shrink below specified pixel width
  2409. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2410. /// void nk_layout_row_template_push_variable(struct nk_context*, float min_width);
  2411. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2412. ///
  2413. /// Parameter | Description
  2414. /// ------------|-----------------------------------------------------------
  2415. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2416. /// __width__ | Holds the minimum pixel width the next column must always be
  2417. */
  2418. NK_API void nk_layout_row_template_push_variable(struct nk_context*, float min_width);
  2419. /*/// #### nk_layout_row_template_push_static
  2420. /// Adds a static column that does not grow and will always have the same size
  2421. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2422. /// void nk_layout_row_template_push_static(struct nk_context*, float width);
  2423. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2424. ///
  2425. /// Parameter | Description
  2426. /// ------------|-----------------------------------------------------------
  2427. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2428. /// __width__ | Holds the absolute pixel width value the next column must be
  2429. */
  2430. NK_API void nk_layout_row_template_push_static(struct nk_context*, float width);
  2431. /*/// #### nk_layout_row_template_end
  2432. /// Marks the end of the row template
  2433. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2434. /// void nk_layout_row_template_end(struct nk_context*);
  2435. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2436. ///
  2437. /// Parameter | Description
  2438. /// ------------|-----------------------------------------------------------
  2439. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2440. */
  2441. NK_API void nk_layout_row_template_end(struct nk_context*);
  2442. /*/// #### nk_layout_space_begin
  2443. /// Begins a new layouting space that allows to specify each widgets position and size.
  2444. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2445. /// void nk_layout_space_begin(struct nk_context*, enum nk_layout_format, float height, int widget_count);
  2446. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2447. ///
  2448. /// Parameter | Description
  2449. /// ------------|-----------------------------------------------------------
  2450. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2451. /// __fmt__ | Either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  2452. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2453. /// __columns__ | Number of widgets inside row
  2454. */
  2455. NK_API void nk_layout_space_begin(struct nk_context*, enum nk_layout_format, float height, int widget_count);
  2456. /*/// #### nk_layout_space_push
  2457. /// Pushes position and size of the next widget in own coordinate space either as pixel or ratio
  2458. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2459. /// void nk_layout_space_push(struct nk_context *ctx, struct nk_rect bounds);
  2460. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2461. ///
  2462. /// Parameter | Description
  2463. /// ------------|-----------------------------------------------------------
  2464. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2465. /// __bounds__ | Position and size in laoyut space local coordinates
  2466. */
  2467. NK_API void nk_layout_space_push(struct nk_context*, struct nk_rect bounds);
  2468. /*/// #### nk_layout_space_end
  2469. /// Marks the end of the layout space
  2470. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2471. /// void nk_layout_space_end(struct nk_context*);
  2472. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2473. ///
  2474. /// Parameter | Description
  2475. /// ------------|-----------------------------------------------------------
  2476. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2477. */
  2478. NK_API void nk_layout_space_end(struct nk_context*);
  2479. /*/// #### nk_layout_space_bounds
  2480. /// Utility function to calculate total space allocated for `nk_layout_space`
  2481. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2482. /// struct nk_rect nk_layout_space_bounds(struct nk_context*);
  2483. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2484. ///
  2485. /// Parameter | Description
  2486. /// ------------|-----------------------------------------------------------
  2487. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2488. ///
  2489. /// Returns `nk_rect` holding the total space allocated
  2490. */
  2491. NK_API struct nk_rect nk_layout_space_bounds(struct nk_context*);
  2492. /*/// #### nk_layout_space_to_screen
  2493. /// Converts vector from nk_layout_space coordinate space into screen space
  2494. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2495. /// struct nk_vec2 nk_layout_space_to_screen(struct nk_context*, struct nk_vec2);
  2496. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2497. ///
  2498. /// Parameter | Description
  2499. /// ------------|-----------------------------------------------------------
  2500. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2501. /// __vec__ | Position to convert from layout space into screen coordinate space
  2502. ///
  2503. /// Returns transformed `nk_vec2` in screen space coordinates
  2504. */
  2505. NK_API struct nk_vec2 nk_layout_space_to_screen(struct nk_context*, struct nk_vec2);
  2506. /*/// #### nk_layout_space_to_local
  2507. /// Converts vector from layout space into screen space
  2508. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2509. /// struct nk_vec2 nk_layout_space_to_local(struct nk_context*, struct nk_vec2);
  2510. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2511. ///
  2512. /// Parameter | Description
  2513. /// ------------|-----------------------------------------------------------
  2514. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2515. /// __vec__ | Position to convert from screen space into layout coordinate space
  2516. ///
  2517. /// Returns transformed `nk_vec2` in layout space coordinates
  2518. */
  2519. NK_API struct nk_vec2 nk_layout_space_to_local(struct nk_context*, struct nk_vec2);
  2520. /*/// #### nk_layout_space_rect_to_screen
  2521. /// Converts rectangle from screen space into layout space
  2522. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2523. /// struct nk_rect nk_layout_space_rect_to_screen(struct nk_context*, struct nk_rect);
  2524. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2525. ///
  2526. /// Parameter | Description
  2527. /// ------------|-----------------------------------------------------------
  2528. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2529. /// __bounds__ | Rectangle to convert from layout space into screen space
  2530. ///
  2531. /// Returns transformed `nk_rect` in screen space coordinates
  2532. */
  2533. NK_API struct nk_rect nk_layout_space_rect_to_screen(struct nk_context*, struct nk_rect);
  2534. /*/// #### nk_layout_space_rect_to_local
  2535. /// Converts rectangle from layout space into screen space
  2536. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2537. /// struct nk_rect nk_layout_space_rect_to_local(struct nk_context*, struct nk_rect);
  2538. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2539. ///
  2540. /// Parameter | Description
  2541. /// ------------|-----------------------------------------------------------
  2542. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2543. /// __bounds__ | Rectangle to convert from layout space into screen space
  2544. ///
  2545. /// Returns transformed `nk_rect` in layout space coordinates
  2546. */
  2547. NK_API struct nk_rect nk_layout_space_rect_to_local(struct nk_context*, struct nk_rect);
  2548. /* =============================================================================
  2549. *
  2550. * GROUP
  2551. *
  2552. * =============================================================================
  2553. /// ### Groups
  2554. /// Groups are basically windows inside windows. They allow to subdivide space
  2555. /// in a window to layout widgets as a group. Almost all more complex widget
  2556. /// layouting requirements can be solved using groups and basic layouting
  2557. /// fuctionality. Groups just like windows are identified by an unique name and
  2558. /// internally keep track of scrollbar offsets by default. However additional
  2559. /// versions are provided to directly manage the scrollbar.
  2560. ///
  2561. /// #### Usage
  2562. /// To create a group you have to call one of the three `nk_group_begin_xxx`
  2563. /// functions to start group declarations and `nk_group_end` at the end. Furthermore it
  2564. /// is required to check the return value of `nk_group_begin_xxx` and only process
  2565. /// widgets inside the window if the value is not 0.
  2566. /// Nesting groups is possible and even encouraged since many layouting schemes
  2567. /// can only be achieved by nesting. Groups, unlike windows, need `nk_group_end`
  2568. /// to be only called if the corosponding `nk_group_begin_xxx` call does not return 0:
  2569. ///
  2570. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2571. /// if (nk_group_begin_xxx(ctx, ...) {
  2572. /// // [... widgets ...]
  2573. /// nk_group_end(ctx);
  2574. /// }
  2575. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2576. ///
  2577. /// In the grand concept groups can be called after starting a window
  2578. /// with `nk_begin_xxx` and before calling `nk_end`:
  2579. ///
  2580. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2581. /// struct nk_context ctx;
  2582. /// nk_init_xxx(&ctx, ...);
  2583. /// while (1) {
  2584. /// // Input
  2585. /// Event evt;
  2586. /// nk_input_begin(&ctx);
  2587. /// while (GetEvent(&evt)) {
  2588. /// if (evt.type == MOUSE_MOVE)
  2589. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  2590. /// else if (evt.type == [...]) {
  2591. /// nk_input_xxx(...);
  2592. /// }
  2593. /// }
  2594. /// nk_input_end(&ctx);
  2595. /// //
  2596. /// // Window
  2597. /// if (nk_begin_xxx(...) {
  2598. /// // [...widgets...]
  2599. /// nk_layout_row_dynamic(...);
  2600. /// if (nk_group_begin_xxx(ctx, ...) {
  2601. /// //[... widgets ...]
  2602. /// nk_group_end(ctx);
  2603. /// }
  2604. /// }
  2605. /// nk_end(ctx);
  2606. /// //
  2607. /// // Draw
  2608. /// const struct nk_command *cmd = 0;
  2609. /// nk_foreach(cmd, &ctx) {
  2610. /// switch (cmd->type) {
  2611. /// case NK_COMMAND_LINE:
  2612. /// your_draw_line_function(...)
  2613. /// break;
  2614. /// case NK_COMMAND_RECT
  2615. /// your_draw_rect_function(...)
  2616. /// break;
  2617. /// case ...:
  2618. /// // [...]
  2619. /// }
  2620. /// nk_clear(&ctx);
  2621. /// }
  2622. /// nk_free(&ctx);
  2623. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2624. /// #### Reference
  2625. /// Function | Description
  2626. /// --------------------------------|-------------------------------------------
  2627. /// nk_group_begin | Start a new group with internal scrollbar handling
  2628. /// nk_group_begin_titled | Start a new group with separeted name and title and internal scrollbar handling
  2629. /// nk_group_end | Ends a group. Should only be called if nk_group_begin returned non-zero
  2630. /// nk_group_scrolled_offset_begin | Start a new group with manual separated handling of scrollbar x- and y-offset
  2631. /// nk_group_scrolled_begin | Start a new group with manual scrollbar handling
  2632. /// nk_group_scrolled_end | Ends a group with manual scrollbar handling. Should only be called if nk_group_begin returned non-zero
  2633. /// nk_group_get_scroll | Gets the scroll offset for the given group
  2634. /// nk_group_set_scroll | Sets the scroll offset for the given group
  2635. */
  2636. /*/// #### nk_group_begin
  2637. /// Starts a new widget group. Requires a previous layouting function to specify a pos/size.
  2638. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2639. /// int nk_group_begin(struct nk_context*, const char *title, nk_flags);
  2640. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2641. ///
  2642. /// Parameter | Description
  2643. /// ------------|-----------------------------------------------------------
  2644. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2645. /// __title__ | Must be an unique identifier for this group that is also used for the group header
  2646. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different group behaviors
  2647. ///
  2648. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2649. */
  2650. NK_API int nk_group_begin(struct nk_context*, const char *title, nk_flags);
  2651. /*/// #### nk_group_begin_titled
  2652. /// Starts a new widget group. Requires a previous layouting function to specify a pos/size.
  2653. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2654. /// int nk_group_begin_titled(struct nk_context*, const char *name, const char *title, nk_flags);
  2655. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2656. ///
  2657. /// Parameter | Description
  2658. /// ------------|-----------------------------------------------------------
  2659. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2660. /// __id__ | Must be an unique identifier for this group
  2661. /// __title__ | Group header title
  2662. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different group behaviors
  2663. ///
  2664. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2665. */
  2666. NK_API int nk_group_begin_titled(struct nk_context*, const char *name, const char *title, nk_flags);
  2667. /*/// #### nk_group_end
  2668. /// Ends a widget group
  2669. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2670. /// void nk_group_end(struct nk_context*);
  2671. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2672. ///
  2673. /// Parameter | Description
  2674. /// ------------|-----------------------------------------------------------
  2675. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2676. */
  2677. NK_API void nk_group_end(struct nk_context*);
  2678. /*/// #### nk_group_scrolled_offset_begin
  2679. /// starts a new widget group. requires a previous layouting function to specify
  2680. /// a size. Does not keep track of scrollbar.
  2681. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2682. /// int nk_group_scrolled_offset_begin(struct nk_context*, nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags);
  2683. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2684. ///
  2685. /// Parameter | Description
  2686. /// ------------|-----------------------------------------------------------
  2687. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2688. /// __x_offset__| Scrollbar x-offset to offset all widgets inside the group horizontally.
  2689. /// __y_offset__| Scrollbar y-offset to offset all widgets inside the group vertically
  2690. /// __title__ | Window unique group title used to both identify and display in the group header
  2691. /// __flags__ | Window flags from the nk_panel_flags section
  2692. ///
  2693. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2694. */
  2695. 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);
  2696. /*/// #### nk_group_scrolled_begin
  2697. /// Starts a new widget group. requires a previous
  2698. /// layouting function to specify a size. Does not keep track of scrollbar.
  2699. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2700. /// int nk_group_scrolled_begin(struct nk_context*, struct nk_scroll *off, const char *title, nk_flags);
  2701. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2702. ///
  2703. /// Parameter | Description
  2704. /// ------------|-----------------------------------------------------------
  2705. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2706. /// __off__ | Both x- and y- scroll offset. Allows for manual scrollbar control
  2707. /// __title__ | Window unique group title used to both identify and display in the group header
  2708. /// __flags__ | Window flags from nk_panel_flags section
  2709. ///
  2710. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2711. */
  2712. NK_API int nk_group_scrolled_begin(struct nk_context*, struct nk_scroll *off, const char *title, nk_flags);
  2713. /*/// #### nk_group_scrolled_end
  2714. /// Ends a widget group after calling nk_group_scrolled_offset_begin or nk_group_scrolled_begin.
  2715. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2716. /// void nk_group_scrolled_end(struct nk_context*);
  2717. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2718. ///
  2719. /// Parameter | Description
  2720. /// ------------|-----------------------------------------------------------
  2721. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2722. */
  2723. NK_API void nk_group_scrolled_end(struct nk_context*);
  2724. /*/// #### nk_group_get_scroll
  2725. /// Gets the scroll position of the given group.
  2726. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2727. /// void nk_group_get_scroll(struct nk_context*, const char *id, nk_uint *x_offset, nk_uint *y_offset);
  2728. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2729. ///
  2730. /// Parameter | Description
  2731. /// -------------|-----------------------------------------------------------
  2732. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2733. /// __id__ | The id of the group to get the scroll position of
  2734. /// __x_offset__ | A pointer to the x offset output (or NULL to ignore)
  2735. /// __y_offset__ | A pointer to the y offset output (or NULL to ignore)
  2736. */
  2737. NK_API void nk_group_get_scroll(struct nk_context*, const char *id, nk_uint *x_offset, nk_uint *y_offset);
  2738. /*/// #### nk_group_set_scroll
  2739. /// Sets the scroll position of the given group.
  2740. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2741. /// void nk_group_set_scroll(struct nk_context*, const char *id, nk_uint x_offset, nk_uint y_offset);
  2742. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2743. ///
  2744. /// Parameter | Description
  2745. /// -------------|-----------------------------------------------------------
  2746. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2747. /// __id__ | The id of the group to scroll
  2748. /// __x_offset__ | The x offset to scroll to
  2749. /// __y_offset__ | The y offset to scroll to
  2750. */
  2751. NK_API void nk_group_set_scroll(struct nk_context*, const char *id, nk_uint x_offset, nk_uint y_offset);
  2752. /* =============================================================================
  2753. *
  2754. * TREE
  2755. *
  2756. * =============================================================================
  2757. /// ### Tree
  2758. /// Trees represent two different concept. First the concept of a collapsable
  2759. /// UI section that can be either in a hidden or visibile state. They allow the UI
  2760. /// user to selectively minimize the current set of visible UI to comprehend.
  2761. /// The second concept are tree widgets for visual UI representation of trees.<br /><br />
  2762. ///
  2763. /// Trees thereby can be nested for tree representations and multiple nested
  2764. /// collapsable UI sections. All trees are started by calling of the
  2765. /// `nk_tree_xxx_push_tree` functions and ended by calling one of the
  2766. /// `nk_tree_xxx_pop_xxx()` functions. Each starting functions takes a title label
  2767. /// and optionally an image to be displayed and the initial collapse state from
  2768. /// the nk_collapse_states section.<br /><br />
  2769. ///
  2770. /// The runtime state of the tree is either stored outside the library by the caller
  2771. /// or inside which requires a unique ID. The unique ID can either be generated
  2772. /// automatically from `__FILE__` and `__LINE__` with function `nk_tree_push`,
  2773. /// by `__FILE__` and a user provided ID generated for example by loop index with
  2774. /// function `nk_tree_push_id` or completely provided from outside by user with
  2775. /// function `nk_tree_push_hashed`.
  2776. ///
  2777. /// #### Usage
  2778. /// To create a tree you have to call one of the seven `nk_tree_xxx_push_xxx`
  2779. /// functions to start a collapsable UI section and `nk_tree_xxx_pop` to mark the
  2780. /// end.
  2781. /// Each starting function will either return `false(0)` if the tree is collapsed
  2782. /// or hidden and therefore does not need to be filled with content or `true(1)`
  2783. /// if visible and required to be filled.
  2784. ///
  2785. /// !!! Note
  2786. /// The tree header does not require and layouting function and instead
  2787. /// calculates a auto height based on the currently used font size
  2788. ///
  2789. /// The tree ending functions only need to be called if the tree content is
  2790. /// actually visible. So make sure the tree push function is guarded by `if`
  2791. /// and the pop call is only taken if the tree is visible.
  2792. ///
  2793. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2794. /// if (nk_tree_push(ctx, NK_TREE_TAB, "Tree", NK_MINIMIZED)) {
  2795. /// nk_layout_row_dynamic(...);
  2796. /// nk_widget(...);
  2797. /// nk_tree_pop(ctx);
  2798. /// }
  2799. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2800. ///
  2801. /// #### Reference
  2802. /// Function | Description
  2803. /// ----------------------------|-------------------------------------------
  2804. /// nk_tree_push | Start a collapsable UI section with internal state management
  2805. /// nk_tree_push_id | Start a collapsable UI section with internal state management callable in a look
  2806. /// nk_tree_push_hashed | Start a collapsable UI section with internal state management with full control over internal unique ID use to store state
  2807. /// nk_tree_image_push | Start a collapsable UI section with image and label header
  2808. /// nk_tree_image_push_id | Start a collapsable UI section with image and label header and internal state management callable in a look
  2809. /// 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
  2810. /// nk_tree_pop | Ends a collapsable UI section
  2811. //
  2812. /// nk_tree_state_push | Start a collapsable UI section with external state management
  2813. /// nk_tree_state_image_push | Start a collapsable UI section with image and label header and external state management
  2814. /// nk_tree_state_pop | Ends a collapsabale UI section
  2815. ///
  2816. /// #### nk_tree_type
  2817. /// Flag | Description
  2818. /// ----------------|----------------------------------------
  2819. /// NK_TREE_NODE | Highlighted tree header to mark a collapsable UI section
  2820. /// NK_TREE_TAB | Non-highighted tree header closer to tree representations
  2821. */
  2822. /*/// #### nk_tree_push
  2823. /// Starts a collapsable UI section with internal state management
  2824. /// !!! WARNING
  2825. /// To keep track of the runtime tree collapsable state this function uses
  2826. /// defines `__FILE__` and `__LINE__` to generate a unique ID. If you want
  2827. /// to call this function in a loop please use `nk_tree_push_id` or
  2828. /// `nk_tree_push_hashed` instead.
  2829. ///
  2830. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2831. /// #define nk_tree_push(ctx, type, title, state)
  2832. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2833. ///
  2834. /// Parameter | Description
  2835. /// ------------|-----------------------------------------------------------
  2836. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2837. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2838. /// __title__ | Label printed in the tree header
  2839. /// __state__ | Initial tree state value out of nk_collapse_states
  2840. ///
  2841. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2842. */
  2843. #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__)
  2844. /*/// #### nk_tree_push_id
  2845. /// Starts a collapsable UI section with internal state management callable in a look
  2846. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2847. /// #define nk_tree_push_id(ctx, type, title, state, id)
  2848. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2849. ///
  2850. /// Parameter | Description
  2851. /// ------------|-----------------------------------------------------------
  2852. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2853. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2854. /// __title__ | Label printed in the tree header
  2855. /// __state__ | Initial tree state value out of nk_collapse_states
  2856. /// __id__ | Loop counter index if this function is called in a loop
  2857. ///
  2858. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2859. */
  2860. #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)
  2861. /*/// #### nk_tree_push_hashed
  2862. /// Start a collapsable UI section with internal state management with full
  2863. /// control over internal unique ID used to store state
  2864. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2865. /// 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);
  2866. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2867. ///
  2868. /// Parameter | Description
  2869. /// ------------|-----------------------------------------------------------
  2870. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2871. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2872. /// __title__ | Label printed in the tree header
  2873. /// __state__ | Initial tree state value out of nk_collapse_states
  2874. /// __hash__ | Memory block or string to generate the ID from
  2875. /// __len__ | Size of passed memory block or string in __hash__
  2876. /// __seed__ | Seeding value if this function is called in a loop or default to `0`
  2877. ///
  2878. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2879. */
  2880. 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);
  2881. /*/// #### nk_tree_image_push
  2882. /// Start a collapsable UI section with image and label header
  2883. /// !!! WARNING
  2884. /// To keep track of the runtime tree collapsable state this function uses
  2885. /// defines `__FILE__` and `__LINE__` to generate a unique ID. If you want
  2886. /// to call this function in a loop please use `nk_tree_image_push_id` or
  2887. /// `nk_tree_image_push_hashed` instead.
  2888. ///
  2889. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2890. /// #define nk_tree_image_push(ctx, type, img, title, state)
  2891. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2892. //
  2893. /// Parameter | Description
  2894. /// ------------|-----------------------------------------------------------
  2895. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2896. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2897. /// __img__ | Image to display inside the header on the left of the label
  2898. /// __title__ | Label printed in the tree header
  2899. /// __state__ | Initial tree state value out of nk_collapse_states
  2900. ///
  2901. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2902. */
  2903. #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__)
  2904. /*/// #### nk_tree_image_push_id
  2905. /// Start a collapsable UI section with image and label header and internal state
  2906. /// management callable in a look
  2907. ///
  2908. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2909. /// #define nk_tree_image_push_id(ctx, type, img, title, state, id)
  2910. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2911. ///
  2912. /// Parameter | Description
  2913. /// ------------|-----------------------------------------------------------
  2914. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2915. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2916. /// __img__ | Image to display inside the header on the left of the label
  2917. /// __title__ | Label printed in the tree header
  2918. /// __state__ | Initial tree state value out of nk_collapse_states
  2919. /// __id__ | Loop counter index if this function is called in a loop
  2920. ///
  2921. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2922. */
  2923. #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)
  2924. /*/// #### nk_tree_image_push_hashed
  2925. /// Start a collapsable UI section with internal state management with full
  2926. /// control over internal unique ID used to store state
  2927. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2928. /// 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);
  2929. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2930. ///
  2931. /// Parameter | Description
  2932. /// ------------|-----------------------------------------------------------
  2933. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2934. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2935. /// __img__ | Image to display inside the header on the left of the label
  2936. /// __title__ | Label printed in the tree header
  2937. /// __state__ | Initial tree state value out of nk_collapse_states
  2938. /// __hash__ | Memory block or string to generate the ID from
  2939. /// __len__ | Size of passed memory block or string in __hash__
  2940. /// __seed__ | Seeding value if this function is called in a loop or default to `0`
  2941. ///
  2942. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2943. */
  2944. 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);
  2945. /*/// #### nk_tree_pop
  2946. /// Ends a collapsabale UI section
  2947. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2948. /// void nk_tree_pop(struct nk_context*);
  2949. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2950. ///
  2951. /// Parameter | Description
  2952. /// ------------|-----------------------------------------------------------
  2953. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  2954. */
  2955. NK_API void nk_tree_pop(struct nk_context*);
  2956. /*/// #### nk_tree_state_push
  2957. /// Start a collapsable UI section with external state management
  2958. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2959. /// int nk_tree_state_push(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states *state);
  2960. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2961. ///
  2962. /// Parameter | Description
  2963. /// ------------|-----------------------------------------------------------
  2964. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  2965. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2966. /// __title__ | Label printed in the tree header
  2967. /// __state__ | Persistent state to update
  2968. ///
  2969. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2970. */
  2971. NK_API int nk_tree_state_push(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states *state);
  2972. /*/// #### nk_tree_state_image_push
  2973. /// Start a collapsable UI section with image and label header and external state management
  2974. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2975. /// int nk_tree_state_image_push(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states *state);
  2976. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2977. ///
  2978. /// Parameter | Description
  2979. /// ------------|-----------------------------------------------------------
  2980. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  2981. /// __img__ | Image to display inside the header on the left of the label
  2982. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2983. /// __title__ | Label printed in the tree header
  2984. /// __state__ | Persistent state to update
  2985. ///
  2986. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2987. */
  2988. 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);
  2989. /*/// #### nk_tree_state_pop
  2990. /// Ends a collapsabale UI section
  2991. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2992. /// void nk_tree_state_pop(struct nk_context*);
  2993. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2994. ///
  2995. /// Parameter | Description
  2996. /// ------------|-----------------------------------------------------------
  2997. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  2998. */
  2999. NK_API void nk_tree_state_pop(struct nk_context*);
  3000. #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__)
  3001. #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)
  3002. 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);
  3003. 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);
  3004. NK_API void nk_tree_element_pop(struct nk_context*);
  3005. /* =============================================================================
  3006. *
  3007. * LIST VIEW
  3008. *
  3009. * ============================================================================= */
  3010. struct nk_list_view {
  3011. /* public: */
  3012. int begin, end, count;
  3013. /* private: */
  3014. int total_height;
  3015. struct nk_context *ctx;
  3016. nk_uint *scroll_pointer;
  3017. nk_uint scroll_value;
  3018. };
  3019. 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);
  3020. NK_API void nk_list_view_end(struct nk_list_view*);
  3021. /* =============================================================================
  3022. *
  3023. * WIDGET
  3024. *
  3025. * ============================================================================= */
  3026. enum nk_widget_layout_states {
  3027. NK_WIDGET_INVALID, /* The widget cannot be seen and is completely out of view */
  3028. NK_WIDGET_VALID, /* The widget is completely inside the window and can be updated and drawn */
  3029. NK_WIDGET_ROM /* The widget is partially visible and cannot be updated */
  3030. };
  3031. enum nk_widget_states {
  3032. NK_WIDGET_STATE_MODIFIED = NK_FLAG(1),
  3033. NK_WIDGET_STATE_INACTIVE = NK_FLAG(2), /* widget is neither active nor hovered */
  3034. NK_WIDGET_STATE_ENTERED = NK_FLAG(3), /* widget has been hovered on the current frame */
  3035. NK_WIDGET_STATE_HOVER = NK_FLAG(4), /* widget is being hovered */
  3036. NK_WIDGET_STATE_ACTIVED = NK_FLAG(5),/* widget is currently activated */
  3037. NK_WIDGET_STATE_LEFT = NK_FLAG(6), /* widget is from this frame on not hovered anymore */
  3038. NK_WIDGET_STATE_HOVERED = NK_WIDGET_STATE_HOVER|NK_WIDGET_STATE_MODIFIED, /* widget is being hovered */
  3039. NK_WIDGET_STATE_ACTIVE = NK_WIDGET_STATE_ACTIVED|NK_WIDGET_STATE_MODIFIED /* widget is currently activated */
  3040. };
  3041. NK_API enum nk_widget_layout_states nk_widget(struct nk_rect*, const struct nk_context*);
  3042. NK_API enum nk_widget_layout_states nk_widget_fitting(struct nk_rect*, struct nk_context*, struct nk_vec2);
  3043. NK_API struct nk_rect nk_widget_bounds(struct nk_context*);
  3044. NK_API struct nk_vec2 nk_widget_position(struct nk_context*);
  3045. NK_API struct nk_vec2 nk_widget_size(struct nk_context*);
  3046. NK_API float nk_widget_width(struct nk_context*);
  3047. NK_API float nk_widget_height(struct nk_context*);
  3048. NK_API int nk_widget_is_hovered(struct nk_context*);
  3049. NK_API int nk_widget_is_mouse_clicked(struct nk_context*, enum nk_buttons);
  3050. NK_API int nk_widget_has_mouse_click_down(struct nk_context*, enum nk_buttons, int down);
  3051. NK_API void nk_spacing(struct nk_context*, int cols);
  3052. /* =============================================================================
  3053. *
  3054. * TEXT
  3055. *
  3056. * ============================================================================= */
  3057. enum nk_text_align {
  3058. NK_TEXT_ALIGN_LEFT = 0x01,
  3059. NK_TEXT_ALIGN_CENTERED = 0x02,
  3060. NK_TEXT_ALIGN_RIGHT = 0x04,
  3061. NK_TEXT_ALIGN_TOP = 0x08,
  3062. NK_TEXT_ALIGN_MIDDLE = 0x10,
  3063. NK_TEXT_ALIGN_BOTTOM = 0x20
  3064. };
  3065. enum nk_text_alignment {
  3066. NK_TEXT_LEFT = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_LEFT,
  3067. NK_TEXT_CENTERED = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_CENTERED,
  3068. NK_TEXT_RIGHT = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_RIGHT
  3069. };
  3070. NK_API void nk_text(struct nk_context*, const char*, int, nk_flags);
  3071. NK_API void nk_text_colored(struct nk_context*, const char*, int, nk_flags, struct nk_color);
  3072. NK_API void nk_text_wrap(struct nk_context*, const char*, int);
  3073. NK_API void nk_text_wrap_colored(struct nk_context*, const char*, int, struct nk_color);
  3074. NK_API void nk_label(struct nk_context*, const char*, nk_flags align);
  3075. NK_API void nk_label_colored(struct nk_context*, const char*, nk_flags align, struct nk_color);
  3076. NK_API void nk_label_wrap(struct nk_context*, const char*);
  3077. NK_API void nk_label_colored_wrap(struct nk_context*, const char*, struct nk_color);
  3078. NK_API void nk_image(struct nk_context*, struct nk_image);
  3079. NK_API void nk_image_color(struct nk_context*, struct nk_image, struct nk_color);
  3080. #ifdef NK_INCLUDE_STANDARD_VARARGS
  3081. NK_API void nk_labelf(struct nk_context*, nk_flags, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(3);
  3082. 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);
  3083. NK_API void nk_labelf_wrap(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*,...) NK_PRINTF_VARARG_FUNC(2);
  3084. NK_API void nk_labelf_colored_wrap(struct nk_context*, struct nk_color, NK_PRINTF_FORMAT_STRING const char*,...) NK_PRINTF_VARARG_FUNC(3);
  3085. NK_API void nk_labelfv(struct nk_context*, nk_flags, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(3);
  3086. 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);
  3087. NK_API void nk_labelfv_wrap(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(2);
  3088. 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);
  3089. NK_API void nk_value_bool(struct nk_context*, const char *prefix, int);
  3090. NK_API void nk_value_int(struct nk_context*, const char *prefix, int);
  3091. NK_API void nk_value_uint(struct nk_context*, const char *prefix, unsigned int);
  3092. NK_API void nk_value_float(struct nk_context*, const char *prefix, float);
  3093. NK_API void nk_value_color_byte(struct nk_context*, const char *prefix, struct nk_color);
  3094. NK_API void nk_value_color_float(struct nk_context*, const char *prefix, struct nk_color);
  3095. NK_API void nk_value_color_hex(struct nk_context*, const char *prefix, struct nk_color);
  3096. #endif
  3097. /* =============================================================================
  3098. *
  3099. * BUTTON
  3100. *
  3101. * ============================================================================= */
  3102. NK_API int nk_button_text(struct nk_context*, const char *title, int len);
  3103. NK_API int nk_button_label(struct nk_context*, const char *title);
  3104. NK_API int nk_button_color(struct nk_context*, struct nk_color);
  3105. NK_API int nk_button_symbol(struct nk_context*, enum nk_symbol_type);
  3106. NK_API int nk_button_image(struct nk_context*, struct nk_image img);
  3107. NK_API int nk_button_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags text_alignment);
  3108. NK_API int nk_button_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3109. NK_API int nk_button_image_label(struct nk_context*, struct nk_image img, const char*, nk_flags text_alignment);
  3110. NK_API int nk_button_image_text(struct nk_context*, struct nk_image img, const char*, int, nk_flags alignment);
  3111. NK_API int nk_button_text_styled(struct nk_context*, const struct nk_style_button*, const char *title, int len);
  3112. NK_API int nk_button_label_styled(struct nk_context*, const struct nk_style_button*, const char *title);
  3113. NK_API int nk_button_symbol_styled(struct nk_context*, const struct nk_style_button*, enum nk_symbol_type);
  3114. NK_API int nk_button_image_styled(struct nk_context*, const struct nk_style_button*, struct nk_image img);
  3115. 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);
  3116. 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);
  3117. 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);
  3118. 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);
  3119. NK_API void nk_button_set_behavior(struct nk_context*, enum nk_button_behavior);
  3120. NK_API int nk_button_push_behavior(struct nk_context*, enum nk_button_behavior);
  3121. NK_API int nk_button_pop_behavior(struct nk_context*);
  3122. /* =============================================================================
  3123. *
  3124. * CHECKBOX
  3125. *
  3126. * ============================================================================= */
  3127. NK_API int nk_check_label(struct nk_context*, const char*, int active);
  3128. NK_API int nk_check_text(struct nk_context*, const char*, int,int active);
  3129. NK_API unsigned nk_check_flags_label(struct nk_context*, const char*, unsigned int flags, unsigned int value);
  3130. NK_API unsigned nk_check_flags_text(struct nk_context*, const char*, int, unsigned int flags, unsigned int value);
  3131. NK_API int nk_checkbox_label(struct nk_context*, const char*, int *active);
  3132. NK_API int nk_checkbox_text(struct nk_context*, const char*, int, int *active);
  3133. NK_API int nk_checkbox_flags_label(struct nk_context*, const char*, unsigned int *flags, unsigned int value);
  3134. NK_API int nk_checkbox_flags_text(struct nk_context*, const char*, int, unsigned int *flags, unsigned int value);
  3135. /* =============================================================================
  3136. *
  3137. * RADIO BUTTON
  3138. *
  3139. * ============================================================================= */
  3140. NK_API int nk_radio_label(struct nk_context*, const char*, int *active);
  3141. NK_API int nk_radio_text(struct nk_context*, const char*, int, int *active);
  3142. NK_API int nk_option_label(struct nk_context*, const char*, int active);
  3143. NK_API int nk_option_text(struct nk_context*, const char*, int, int active);
  3144. /* =============================================================================
  3145. *
  3146. * SELECTABLE
  3147. *
  3148. * ============================================================================= */
  3149. NK_API int nk_selectable_label(struct nk_context*, const char*, nk_flags align, int *value);
  3150. NK_API int nk_selectable_text(struct nk_context*, const char*, int, nk_flags align, int *value);
  3151. NK_API int nk_selectable_image_label(struct nk_context*,struct nk_image, const char*, nk_flags align, int *value);
  3152. NK_API int nk_selectable_image_text(struct nk_context*,struct nk_image, const char*, int, nk_flags align, int *value);
  3153. NK_API int nk_selectable_symbol_label(struct nk_context*,enum nk_symbol_type, const char*, nk_flags align, int *value);
  3154. NK_API int nk_selectable_symbol_text(struct nk_context*,enum nk_symbol_type, const char*, int, nk_flags align, int *value);
  3155. NK_API int nk_select_label(struct nk_context*, const char*, nk_flags align, int value);
  3156. NK_API int nk_select_text(struct nk_context*, const char*, int, nk_flags align, int value);
  3157. NK_API int nk_select_image_label(struct nk_context*, struct nk_image,const char*, nk_flags align, int value);
  3158. NK_API int nk_select_image_text(struct nk_context*, struct nk_image,const char*, int, nk_flags align, int value);
  3159. NK_API int nk_select_symbol_label(struct nk_context*,enum nk_symbol_type, const char*, nk_flags align, int value);
  3160. NK_API int nk_select_symbol_text(struct nk_context*,enum nk_symbol_type, const char*, int, nk_flags align, int value);
  3161. /* =============================================================================
  3162. *
  3163. * SLIDER
  3164. *
  3165. * ============================================================================= */
  3166. NK_API float nk_slide_float(struct nk_context*, float min, float val, float max, float step);
  3167. NK_API int nk_slide_int(struct nk_context*, int min, int val, int max, int step);
  3168. NK_API int nk_slider_float(struct nk_context*, float min, float *val, float max, float step);
  3169. NK_API int nk_slider_int(struct nk_context*, int min, int *val, int max, int step);
  3170. /* =============================================================================
  3171. *
  3172. * PROGRESSBAR
  3173. *
  3174. * ============================================================================= */
  3175. NK_API int nk_progress(struct nk_context*, nk_size *cur, nk_size max, int modifyable);
  3176. NK_API nk_size nk_prog(struct nk_context*, nk_size cur, nk_size max, int modifyable);
  3177. /* =============================================================================
  3178. *
  3179. * COLOR PICKER
  3180. *
  3181. * ============================================================================= */
  3182. NK_API struct nk_colorf nk_color_picker(struct nk_context*, struct nk_colorf, enum nk_color_format);
  3183. NK_API int nk_color_pick(struct nk_context*, struct nk_colorf*, enum nk_color_format);
  3184. /* =============================================================================
  3185. *
  3186. * PROPERTIES
  3187. *
  3188. * =============================================================================
  3189. /// ### Properties
  3190. /// Properties are the main value modification widgets in Nuklear. Changing a value
  3191. /// can be achieved by dragging, adding/removing incremental steps on button click
  3192. /// or by directly typing a number.
  3193. ///
  3194. /// #### Usage
  3195. /// Each property requires a unique name for identifaction that is also used for
  3196. /// displaying a label. If you want to use the same name multiple times make sure
  3197. /// add a '#' before your name. The '#' will not be shown but will generate a
  3198. /// unique ID. Each propery also takes in a minimum and maximum value. If you want
  3199. /// to make use of the complete number range of a type just use the provided
  3200. /// type limits from `limits.h`. For example `INT_MIN` and `INT_MAX` for
  3201. /// `nk_property_int` and `nk_propertyi`. In additional each property takes in
  3202. /// a increment value that will be added or subtracted if either the increment
  3203. /// decrement button is clicked. Finally there is a value for increment per pixel
  3204. /// dragged that is added or subtracted from the value.
  3205. ///
  3206. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3207. /// int value = 0;
  3208. /// struct nk_context ctx;
  3209. /// nk_init_xxx(&ctx, ...);
  3210. /// while (1) {
  3211. /// // Input
  3212. /// Event evt;
  3213. /// nk_input_begin(&ctx);
  3214. /// while (GetEvent(&evt)) {
  3215. /// if (evt.type == MOUSE_MOVE)
  3216. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  3217. /// else if (evt.type == [...]) {
  3218. /// nk_input_xxx(...);
  3219. /// }
  3220. /// }
  3221. /// nk_input_end(&ctx);
  3222. /// //
  3223. /// // Window
  3224. /// if (nk_begin_xxx(...) {
  3225. /// // Property
  3226. /// nk_layout_row_dynamic(...);
  3227. /// nk_property_int(ctx, "ID", INT_MIN, &value, INT_MAX, 1, 1);
  3228. /// }
  3229. /// nk_end(ctx);
  3230. /// //
  3231. /// // Draw
  3232. /// const struct nk_command *cmd = 0;
  3233. /// nk_foreach(cmd, &ctx) {
  3234. /// switch (cmd->type) {
  3235. /// case NK_COMMAND_LINE:
  3236. /// your_draw_line_function(...)
  3237. /// break;
  3238. /// case NK_COMMAND_RECT
  3239. /// your_draw_rect_function(...)
  3240. /// break;
  3241. /// case ...:
  3242. /// // [...]
  3243. /// }
  3244. /// nk_clear(&ctx);
  3245. /// }
  3246. /// nk_free(&ctx);
  3247. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3248. ///
  3249. /// #### Reference
  3250. /// Function | Description
  3251. /// --------------------|-------------------------------------------
  3252. /// nk_property_int | Integer property directly modifing a passed in value
  3253. /// nk_property_float | Float property directly modifing a passed in value
  3254. /// nk_property_double | Double property directly modifing a passed in value
  3255. /// nk_propertyi | Integer property returning the modified int value
  3256. /// nk_propertyf | Float property returning the modified float value
  3257. /// nk_propertyd | Double property returning the modified double value
  3258. ///
  3259. */
  3260. /*/// #### nk_property_int
  3261. /// Integer property directly modifing a passed in value
  3262. /// !!! WARNING
  3263. /// To generate a unique property ID using the same label make sure to insert
  3264. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3265. ///
  3266. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3267. /// void nk_property_int(struct nk_context *ctx, const char *name, int min, int *val, int max, int step, float inc_per_pixel);
  3268. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3269. ///
  3270. /// Parameter | Description
  3271. /// --------------------|-----------------------------------------------------------
  3272. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3273. /// __name__ | String used both as a label as well as a unique identifier
  3274. /// __min__ | Minimum value not allowed to be underflown
  3275. /// __val__ | Integer pointer to be modified
  3276. /// __max__ | Maximum value not allowed to be overflown
  3277. /// __step__ | Increment added and subtracted on increment and decrement button
  3278. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3279. */
  3280. NK_API void nk_property_int(struct nk_context*, const char *name, int min, int *val, int max, int step, float inc_per_pixel);
  3281. /*/// #### nk_property_float
  3282. /// Float property directly modifing a passed in value
  3283. /// !!! WARNING
  3284. /// To generate a unique property ID using the same label make sure to insert
  3285. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3286. ///
  3287. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3288. /// void nk_property_float(struct nk_context *ctx, const char *name, float min, float *val, float max, float step, float inc_per_pixel);
  3289. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3290. ///
  3291. /// Parameter | Description
  3292. /// --------------------|-----------------------------------------------------------
  3293. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3294. /// __name__ | String used both as a label as well as a unique identifier
  3295. /// __min__ | Minimum value not allowed to be underflown
  3296. /// __val__ | Float pointer to be modified
  3297. /// __max__ | Maximum value not allowed to be overflown
  3298. /// __step__ | Increment added and subtracted on increment and decrement button
  3299. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3300. */
  3301. NK_API void nk_property_float(struct nk_context*, const char *name, float min, float *val, float max, float step, float inc_per_pixel);
  3302. /*/// #### nk_property_double
  3303. /// Double property directly modifing a passed in value
  3304. /// !!! WARNING
  3305. /// To generate a unique property ID using the same label make sure to insert
  3306. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3307. ///
  3308. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3309. /// void nk_property_double(struct nk_context *ctx, const char *name, double min, double *val, double max, double step, double inc_per_pixel);
  3310. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3311. ///
  3312. /// Parameter | Description
  3313. /// --------------------|-----------------------------------------------------------
  3314. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3315. /// __name__ | String used both as a label as well as a unique identifier
  3316. /// __min__ | Minimum value not allowed to be underflown
  3317. /// __val__ | Double pointer to be modified
  3318. /// __max__ | Maximum value not allowed to be overflown
  3319. /// __step__ | Increment added and subtracted on increment and decrement button
  3320. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3321. */
  3322. NK_API void nk_property_double(struct nk_context*, const char *name, double min, double *val, double max, double step, float inc_per_pixel);
  3323. /*/// #### nk_propertyi
  3324. /// Integer property modifing a passed in value and returning the new value
  3325. /// !!! WARNING
  3326. /// To generate a unique property ID using the same label make sure to insert
  3327. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3328. ///
  3329. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3330. /// int nk_propertyi(struct nk_context *ctx, const char *name, int min, int val, int max, int step, float inc_per_pixel);
  3331. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3332. ///
  3333. /// Parameter | Description
  3334. /// --------------------|-----------------------------------------------------------
  3335. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3336. /// __name__ | String used both as a label as well as a unique identifier
  3337. /// __min__ | Minimum value not allowed to be underflown
  3338. /// __val__ | Current integer value to be modified and returned
  3339. /// __max__ | Maximum value not allowed to be overflown
  3340. /// __step__ | Increment added and subtracted on increment and decrement button
  3341. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3342. ///
  3343. /// Returns the new modified integer value
  3344. */
  3345. NK_API int nk_propertyi(struct nk_context*, const char *name, int min, int val, int max, int step, float inc_per_pixel);
  3346. /*/// #### nk_propertyf
  3347. /// Float property modifing a passed in value and returning the new value
  3348. /// !!! WARNING
  3349. /// To generate a unique property ID using the same label make sure to insert
  3350. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3351. ///
  3352. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3353. /// float nk_propertyf(struct nk_context *ctx, const char *name, float min, float val, float max, float step, float inc_per_pixel);
  3354. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3355. ///
  3356. /// Parameter | Description
  3357. /// --------------------|-----------------------------------------------------------
  3358. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3359. /// __name__ | String used both as a label as well as a unique identifier
  3360. /// __min__ | Minimum value not allowed to be underflown
  3361. /// __val__ | Current float value to be modified and returned
  3362. /// __max__ | Maximum value not allowed to be overflown
  3363. /// __step__ | Increment added and subtracted on increment and decrement button
  3364. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3365. ///
  3366. /// Returns the new modified float value
  3367. */
  3368. NK_API float nk_propertyf(struct nk_context*, const char *name, float min, float val, float max, float step, float inc_per_pixel);
  3369. /*/// #### nk_propertyd
  3370. /// Float property modifing a passed in value and returning the new value
  3371. /// !!! WARNING
  3372. /// To generate a unique property ID using the same label make sure to insert
  3373. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3374. ///
  3375. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3376. /// float nk_propertyd(struct nk_context *ctx, const char *name, double min, double val, double max, double step, double inc_per_pixel);
  3377. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3378. ///
  3379. /// Parameter | Description
  3380. /// --------------------|-----------------------------------------------------------
  3381. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3382. /// __name__ | String used both as a label as well as a unique identifier
  3383. /// __min__ | Minimum value not allowed to be underflown
  3384. /// __val__ | Current double value to be modified and returned
  3385. /// __max__ | Maximum value not allowed to be overflown
  3386. /// __step__ | Increment added and subtracted on increment and decrement button
  3387. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3388. ///
  3389. /// Returns the new modified double value
  3390. */
  3391. NK_API double nk_propertyd(struct nk_context*, const char *name, double min, double val, double max, double step, float inc_per_pixel);
  3392. /* =============================================================================
  3393. *
  3394. * TEXT EDIT
  3395. *
  3396. * ============================================================================= */
  3397. enum nk_edit_flags {
  3398. NK_EDIT_DEFAULT = 0,
  3399. NK_EDIT_READ_ONLY = NK_FLAG(0),
  3400. NK_EDIT_AUTO_SELECT = NK_FLAG(1),
  3401. NK_EDIT_SIG_ENTER = NK_FLAG(2),
  3402. NK_EDIT_ALLOW_TAB = NK_FLAG(3),
  3403. NK_EDIT_NO_CURSOR = NK_FLAG(4),
  3404. NK_EDIT_SELECTABLE = NK_FLAG(5),
  3405. NK_EDIT_CLIPBOARD = NK_FLAG(6),
  3406. NK_EDIT_CTRL_ENTER_NEWLINE = NK_FLAG(7),
  3407. NK_EDIT_NO_HORIZONTAL_SCROLL = NK_FLAG(8),
  3408. NK_EDIT_ALWAYS_INSERT_MODE = NK_FLAG(9),
  3409. NK_EDIT_MULTILINE = NK_FLAG(10),
  3410. NK_EDIT_GOTO_END_ON_ACTIVATE = NK_FLAG(11)
  3411. };
  3412. enum nk_edit_types {
  3413. NK_EDIT_SIMPLE = NK_EDIT_ALWAYS_INSERT_MODE,
  3414. NK_EDIT_FIELD = NK_EDIT_SIMPLE|NK_EDIT_SELECTABLE|NK_EDIT_CLIPBOARD,
  3415. NK_EDIT_BOX = NK_EDIT_ALWAYS_INSERT_MODE| NK_EDIT_SELECTABLE| NK_EDIT_MULTILINE|NK_EDIT_ALLOW_TAB|NK_EDIT_CLIPBOARD,
  3416. NK_EDIT_EDITOR = NK_EDIT_SELECTABLE|NK_EDIT_MULTILINE|NK_EDIT_ALLOW_TAB| NK_EDIT_CLIPBOARD
  3417. };
  3418. enum nk_edit_events {
  3419. NK_EDIT_ACTIVE = NK_FLAG(0), /* edit widget is currently being modified */
  3420. NK_EDIT_INACTIVE = NK_FLAG(1), /* edit widget is not active and is not being modified */
  3421. NK_EDIT_ACTIVATED = NK_FLAG(2), /* edit widget went from state inactive to state active */
  3422. NK_EDIT_DEACTIVATED = NK_FLAG(3), /* edit widget went from state active to state inactive */
  3423. NK_EDIT_COMMITED = NK_FLAG(4) /* edit widget has received an enter and lost focus */
  3424. };
  3425. NK_API nk_flags nk_edit_string(struct nk_context*, nk_flags, char *buffer, int *len, int max, nk_plugin_filter);
  3426. NK_API nk_flags nk_edit_string_zero_terminated(struct nk_context*, nk_flags, char *buffer, int max, nk_plugin_filter);
  3427. NK_API nk_flags nk_edit_buffer(struct nk_context*, nk_flags, struct nk_text_edit*, nk_plugin_filter);
  3428. NK_API void nk_edit_focus(struct nk_context*, nk_flags flags);
  3429. NK_API void nk_edit_unfocus(struct nk_context*);
  3430. /* =============================================================================
  3431. *
  3432. * CHART
  3433. *
  3434. * ============================================================================= */
  3435. NK_API int nk_chart_begin(struct nk_context*, enum nk_chart_type, int num, float min, float max);
  3436. 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);
  3437. NK_API void nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type, int count, float min_value, float max_value);
  3438. 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);
  3439. NK_API nk_flags nk_chart_push(struct nk_context*, float);
  3440. NK_API nk_flags nk_chart_push_slot(struct nk_context*, float, int);
  3441. NK_API void nk_chart_end(struct nk_context*);
  3442. NK_API void nk_plot(struct nk_context*, enum nk_chart_type, const float *values, int count, int offset);
  3443. 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);
  3444. /* =============================================================================
  3445. *
  3446. * POPUP
  3447. *
  3448. * ============================================================================= */
  3449. NK_API int nk_popup_begin(struct nk_context*, enum nk_popup_type, const char*, nk_flags, struct nk_rect bounds);
  3450. NK_API void nk_popup_close(struct nk_context*);
  3451. NK_API void nk_popup_end(struct nk_context*);
  3452. NK_API void nk_popup_get_scroll(struct nk_context*, nk_uint *offset_x, nk_uint *offset_y);
  3453. NK_API void nk_popup_set_scroll(struct nk_context*, nk_uint offset_x, nk_uint offset_y);
  3454. /* =============================================================================
  3455. *
  3456. * COMBOBOX
  3457. *
  3458. * ============================================================================= */
  3459. NK_API int nk_combo(struct nk_context*, const char **items, int count, int selected, int item_height, struct nk_vec2 size);
  3460. 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);
  3461. 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);
  3462. 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);
  3463. NK_API void nk_combobox(struct nk_context*, const char **items, int count, int *selected, int item_height, struct nk_vec2 size);
  3464. 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);
  3465. 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);
  3466. 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);
  3467. /* =============================================================================
  3468. *
  3469. * ABSTRACT COMBOBOX
  3470. *
  3471. * ============================================================================= */
  3472. NK_API int nk_combo_begin_text(struct nk_context*, const char *selected, int, struct nk_vec2 size);
  3473. NK_API int nk_combo_begin_label(struct nk_context*, const char *selected, struct nk_vec2 size);
  3474. NK_API int nk_combo_begin_color(struct nk_context*, struct nk_color color, struct nk_vec2 size);
  3475. NK_API int nk_combo_begin_symbol(struct nk_context*, enum nk_symbol_type, struct nk_vec2 size);
  3476. NK_API int nk_combo_begin_symbol_label(struct nk_context*, const char *selected, enum nk_symbol_type, struct nk_vec2 size);
  3477. NK_API int nk_combo_begin_symbol_text(struct nk_context*, const char *selected, int, enum nk_symbol_type, struct nk_vec2 size);
  3478. NK_API int nk_combo_begin_image(struct nk_context*, struct nk_image img, struct nk_vec2 size);
  3479. NK_API int nk_combo_begin_image_label(struct nk_context*, const char *selected, struct nk_image, struct nk_vec2 size);
  3480. NK_API int nk_combo_begin_image_text(struct nk_context*, const char *selected, int, struct nk_image, struct nk_vec2 size);
  3481. NK_API int nk_combo_item_label(struct nk_context*, const char*, nk_flags alignment);
  3482. NK_API int nk_combo_item_text(struct nk_context*, const char*,int, nk_flags alignment);
  3483. NK_API int nk_combo_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  3484. NK_API int nk_combo_item_image_text(struct nk_context*, struct nk_image, const char*, int,nk_flags alignment);
  3485. NK_API int nk_combo_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  3486. NK_API int nk_combo_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3487. NK_API void nk_combo_close(struct nk_context*);
  3488. NK_API void nk_combo_end(struct nk_context*);
  3489. /* =============================================================================
  3490. *
  3491. * CONTEXTUAL
  3492. *
  3493. * ============================================================================= */
  3494. NK_API int nk_contextual_begin(struct nk_context*, nk_flags, struct nk_vec2, struct nk_rect trigger_bounds);
  3495. NK_API int nk_contextual_item_text(struct nk_context*, const char*, int,nk_flags align);
  3496. NK_API int nk_contextual_item_label(struct nk_context*, const char*, nk_flags align);
  3497. NK_API int nk_contextual_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  3498. NK_API int nk_contextual_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
  3499. NK_API int nk_contextual_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  3500. NK_API int nk_contextual_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3501. NK_API void nk_contextual_close(struct nk_context*);
  3502. NK_API void nk_contextual_end(struct nk_context*);
  3503. /* =============================================================================
  3504. *
  3505. * TOOLTIP
  3506. *
  3507. * ============================================================================= */
  3508. NK_API void nk_tooltip(struct nk_context*, const char*);
  3509. #ifdef NK_INCLUDE_STANDARD_VARARGS
  3510. NK_API void nk_tooltipf(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(2);
  3511. NK_API void nk_tooltipfv(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(2);
  3512. #endif
  3513. NK_API int nk_tooltip_begin(struct nk_context*, float width);
  3514. NK_API void nk_tooltip_end(struct nk_context*);
  3515. /* =============================================================================
  3516. *
  3517. * MENU
  3518. *
  3519. * ============================================================================= */
  3520. NK_API void nk_menubar_begin(struct nk_context*);
  3521. NK_API void nk_menubar_end(struct nk_context*);
  3522. NK_API int nk_menu_begin_text(struct nk_context*, const char* title, int title_len, nk_flags align, struct nk_vec2 size);
  3523. NK_API int nk_menu_begin_label(struct nk_context*, const char*, nk_flags align, struct nk_vec2 size);
  3524. NK_API int nk_menu_begin_image(struct nk_context*, const char*, struct nk_image, struct nk_vec2 size);
  3525. NK_API int nk_menu_begin_image_text(struct nk_context*, const char*, int,nk_flags align,struct nk_image, struct nk_vec2 size);
  3526. NK_API int nk_menu_begin_image_label(struct nk_context*, const char*, nk_flags align,struct nk_image, struct nk_vec2 size);
  3527. NK_API int nk_menu_begin_symbol(struct nk_context*, const char*, enum nk_symbol_type, struct nk_vec2 size);
  3528. 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);
  3529. NK_API int nk_menu_begin_symbol_label(struct nk_context*, const char*, nk_flags align,enum nk_symbol_type, struct nk_vec2 size);
  3530. NK_API int nk_menu_item_text(struct nk_context*, const char*, int,nk_flags align);
  3531. NK_API int nk_menu_item_label(struct nk_context*, const char*, nk_flags alignment);
  3532. NK_API int nk_menu_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  3533. NK_API int nk_menu_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
  3534. NK_API int nk_menu_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3535. NK_API int nk_menu_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  3536. NK_API void nk_menu_close(struct nk_context*);
  3537. NK_API void nk_menu_end(struct nk_context*);
  3538. /* =============================================================================
  3539. *
  3540. * STYLE
  3541. *
  3542. * ============================================================================= */
  3543. enum nk_style_colors {
  3544. NK_COLOR_TEXT,
  3545. NK_COLOR_WINDOW,
  3546. NK_COLOR_HEADER,
  3547. NK_COLOR_BORDER,
  3548. NK_COLOR_BUTTON,
  3549. NK_COLOR_BUTTON_HOVER,
  3550. NK_COLOR_BUTTON_ACTIVE,
  3551. NK_COLOR_TOGGLE,
  3552. NK_COLOR_TOGGLE_HOVER,
  3553. NK_COLOR_TOGGLE_CURSOR,
  3554. NK_COLOR_SELECT,
  3555. NK_COLOR_SELECT_ACTIVE,
  3556. NK_COLOR_SLIDER,
  3557. NK_COLOR_SLIDER_CURSOR,
  3558. NK_COLOR_SLIDER_CURSOR_HOVER,
  3559. NK_COLOR_SLIDER_CURSOR_ACTIVE,
  3560. NK_COLOR_PROPERTY,
  3561. NK_COLOR_EDIT,
  3562. NK_COLOR_EDIT_CURSOR,
  3563. NK_COLOR_COMBO,
  3564. NK_COLOR_CHART,
  3565. NK_COLOR_CHART_COLOR,
  3566. NK_COLOR_CHART_COLOR_HIGHLIGHT,
  3567. NK_COLOR_SCROLLBAR,
  3568. NK_COLOR_SCROLLBAR_CURSOR,
  3569. NK_COLOR_SCROLLBAR_CURSOR_HOVER,
  3570. NK_COLOR_SCROLLBAR_CURSOR_ACTIVE,
  3571. NK_COLOR_TAB_HEADER,
  3572. NK_COLOR_COUNT
  3573. };
  3574. enum nk_style_cursor {
  3575. NK_CURSOR_ARROW,
  3576. NK_CURSOR_TEXT,
  3577. NK_CURSOR_MOVE,
  3578. NK_CURSOR_RESIZE_VERTICAL,
  3579. NK_CURSOR_RESIZE_HORIZONTAL,
  3580. NK_CURSOR_RESIZE_TOP_LEFT_DOWN_RIGHT,
  3581. NK_CURSOR_RESIZE_TOP_RIGHT_DOWN_LEFT,
  3582. NK_CURSOR_COUNT
  3583. };
  3584. NK_API void nk_style_default(struct nk_context*);
  3585. NK_API void nk_style_from_table(struct nk_context*, const struct nk_color*);
  3586. NK_API void nk_style_load_cursor(struct nk_context*, enum nk_style_cursor, const struct nk_cursor*);
  3587. NK_API void nk_style_load_all_cursors(struct nk_context*, struct nk_cursor*);
  3588. NK_API const char* nk_style_get_color_by_name(enum nk_style_colors);
  3589. NK_API void nk_style_set_font(struct nk_context*, const struct nk_user_font*);
  3590. NK_API int nk_style_set_cursor(struct nk_context*, enum nk_style_cursor);
  3591. NK_API void nk_style_show_cursor(struct nk_context*);
  3592. NK_API void nk_style_hide_cursor(struct nk_context*);
  3593. NK_API int nk_style_push_font(struct nk_context*, const struct nk_user_font*);
  3594. NK_API int nk_style_push_float(struct nk_context*, float*, float);
  3595. NK_API int nk_style_push_vec2(struct nk_context*, struct nk_vec2*, struct nk_vec2);
  3596. NK_API int nk_style_push_style_item(struct nk_context*, struct nk_style_item*, struct nk_style_item);
  3597. NK_API int nk_style_push_flags(struct nk_context*, nk_flags*, nk_flags);
  3598. NK_API int nk_style_push_color(struct nk_context*, struct nk_color*, struct nk_color);
  3599. NK_API int nk_style_pop_font(struct nk_context*);
  3600. NK_API int nk_style_pop_float(struct nk_context*);
  3601. NK_API int nk_style_pop_vec2(struct nk_context*);
  3602. NK_API int nk_style_pop_style_item(struct nk_context*);
  3603. NK_API int nk_style_pop_flags(struct nk_context*);
  3604. NK_API int nk_style_pop_color(struct nk_context*);
  3605. /* =============================================================================
  3606. *
  3607. * COLOR
  3608. *
  3609. * ============================================================================= */
  3610. NK_API struct nk_color nk_rgb(int r, int g, int b);
  3611. NK_API struct nk_color nk_rgb_iv(const int *rgb);
  3612. NK_API struct nk_color nk_rgb_bv(const nk_byte* rgb);
  3613. NK_API struct nk_color nk_rgb_f(float r, float g, float b);
  3614. NK_API struct nk_color nk_rgb_fv(const float *rgb);
  3615. NK_API struct nk_color nk_rgb_cf(struct nk_colorf c);
  3616. NK_API struct nk_color nk_rgb_hex(const char *rgb);
  3617. NK_API struct nk_color nk_rgba(int r, int g, int b, int a);
  3618. NK_API struct nk_color nk_rgba_u32(nk_uint);
  3619. NK_API struct nk_color nk_rgba_iv(const int *rgba);
  3620. NK_API struct nk_color nk_rgba_bv(const nk_byte *rgba);
  3621. NK_API struct nk_color nk_rgba_f(float r, float g, float b, float a);
  3622. NK_API struct nk_color nk_rgba_fv(const float *rgba);
  3623. NK_API struct nk_color nk_rgba_cf(struct nk_colorf c);
  3624. NK_API struct nk_color nk_rgba_hex(const char *rgb);
  3625. NK_API struct nk_colorf nk_hsva_colorf(float h, float s, float v, float a);
  3626. NK_API struct nk_colorf nk_hsva_colorfv(float *c);
  3627. NK_API void nk_colorf_hsva_f(float *out_h, float *out_s, float *out_v, float *out_a, struct nk_colorf in);
  3628. NK_API void nk_colorf_hsva_fv(float *hsva, struct nk_colorf in);
  3629. NK_API struct nk_color nk_hsv(int h, int s, int v);
  3630. NK_API struct nk_color nk_hsv_iv(const int *hsv);
  3631. NK_API struct nk_color nk_hsv_bv(const nk_byte *hsv);
  3632. NK_API struct nk_color nk_hsv_f(float h, float s, float v);
  3633. NK_API struct nk_color nk_hsv_fv(const float *hsv);
  3634. NK_API struct nk_color nk_hsva(int h, int s, int v, int a);
  3635. NK_API struct nk_color nk_hsva_iv(const int *hsva);
  3636. NK_API struct nk_color nk_hsva_bv(const nk_byte *hsva);
  3637. NK_API struct nk_color nk_hsva_f(float h, float s, float v, float a);
  3638. NK_API struct nk_color nk_hsva_fv(const float *hsva);
  3639. /* color (conversion nuklear --> user) */
  3640. NK_API void nk_color_f(float *r, float *g, float *b, float *a, struct nk_color);
  3641. NK_API void nk_color_fv(float *rgba_out, struct nk_color);
  3642. NK_API struct nk_colorf nk_color_cf(struct nk_color);
  3643. NK_API void nk_color_d(double *r, double *g, double *b, double *a, struct nk_color);
  3644. NK_API void nk_color_dv(double *rgba_out, struct nk_color);
  3645. NK_API nk_uint nk_color_u32(struct nk_color);
  3646. NK_API void nk_color_hex_rgba(char *output, struct nk_color);
  3647. NK_API void nk_color_hex_rgb(char *output, struct nk_color);
  3648. NK_API void nk_color_hsv_i(int *out_h, int *out_s, int *out_v, struct nk_color);
  3649. NK_API void nk_color_hsv_b(nk_byte *out_h, nk_byte *out_s, nk_byte *out_v, struct nk_color);
  3650. NK_API void nk_color_hsv_iv(int *hsv_out, struct nk_color);
  3651. NK_API void nk_color_hsv_bv(nk_byte *hsv_out, struct nk_color);
  3652. NK_API void nk_color_hsv_f(float *out_h, float *out_s, float *out_v, struct nk_color);
  3653. NK_API void nk_color_hsv_fv(float *hsv_out, struct nk_color);
  3654. NK_API void nk_color_hsva_i(int *h, int *s, int *v, int *a, struct nk_color);
  3655. NK_API void nk_color_hsva_b(nk_byte *h, nk_byte *s, nk_byte *v, nk_byte *a, struct nk_color);
  3656. NK_API void nk_color_hsva_iv(int *hsva_out, struct nk_color);
  3657. NK_API void nk_color_hsva_bv(nk_byte *hsva_out, struct nk_color);
  3658. NK_API void nk_color_hsva_f(float *out_h, float *out_s, float *out_v, float *out_a, struct nk_color);
  3659. NK_API void nk_color_hsva_fv(float *hsva_out, struct nk_color);
  3660. /* =============================================================================
  3661. *
  3662. * IMAGE
  3663. *
  3664. * ============================================================================= */
  3665. NK_API nk_handle nk_handle_ptr(void*);
  3666. NK_API nk_handle nk_handle_id(int);
  3667. NK_API struct nk_image nk_image_handle(nk_handle);
  3668. NK_API struct nk_image nk_image_ptr(void*);
  3669. NK_API struct nk_image nk_image_id(int);
  3670. NK_API int nk_image_is_subimage(const struct nk_image* img);
  3671. NK_API struct nk_image nk_subimage_ptr(void*, unsigned short w, unsigned short h, struct nk_rect sub_region);
  3672. NK_API struct nk_image nk_subimage_id(int, unsigned short w, unsigned short h, struct nk_rect sub_region);
  3673. NK_API struct nk_image nk_subimage_handle(nk_handle, unsigned short w, unsigned short h, struct nk_rect sub_region);
  3674. /* =============================================================================
  3675. *
  3676. * MATH
  3677. *
  3678. * ============================================================================= */
  3679. NK_API nk_hash nk_murmur_hash(const void *key, int len, nk_hash seed);
  3680. NK_API void nk_triangle_from_direction(struct nk_vec2 *result, struct nk_rect r, float pad_x, float pad_y, enum nk_heading);
  3681. NK_API struct nk_vec2 nk_vec2(float x, float y);
  3682. NK_API struct nk_vec2 nk_vec2i(int x, int y);
  3683. NK_API struct nk_vec2 nk_vec2v(const float *xy);
  3684. NK_API struct nk_vec2 nk_vec2iv(const int *xy);
  3685. NK_API struct nk_rect nk_get_null_rect(void);
  3686. NK_API struct nk_rect nk_rect(float x, float y, float w, float h);
  3687. NK_API struct nk_rect nk_recti(int x, int y, int w, int h);
  3688. NK_API struct nk_rect nk_recta(struct nk_vec2 pos, struct nk_vec2 size);
  3689. NK_API struct nk_rect nk_rectv(const float *xywh);
  3690. NK_API struct nk_rect nk_rectiv(const int *xywh);
  3691. NK_API struct nk_vec2 nk_rect_pos(struct nk_rect);
  3692. NK_API struct nk_vec2 nk_rect_size(struct nk_rect);
  3693. /* =============================================================================
  3694. *
  3695. * STRING
  3696. *
  3697. * ============================================================================= */
  3698. NK_API int nk_strlen(const char *str);
  3699. NK_API int nk_stricmp(const char *s1, const char *s2);
  3700. NK_API int nk_stricmpn(const char *s1, const char *s2, int n);
  3701. NK_API int nk_strtoi(const char *str, const char **endptr);
  3702. NK_API float nk_strtof(const char *str, const char **endptr);
  3703. NK_API double nk_strtod(const char *str, const char **endptr);
  3704. NK_API int nk_strfilter(const char *text, const char *regexp);
  3705. NK_API int nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score);
  3706. NK_API int nk_strmatch_fuzzy_text(const char *txt, int txt_len, const char *pattern, int *out_score);
  3707. /* =============================================================================
  3708. *
  3709. * UTF-8
  3710. *
  3711. * ============================================================================= */
  3712. NK_API int nk_utf_decode(const char*, nk_rune*, int);
  3713. NK_API int nk_utf_encode(nk_rune, char*, int);
  3714. NK_API int nk_utf_len(const char*, int byte_len);
  3715. NK_API const char* nk_utf_at(const char *buffer, int length, int index, nk_rune *unicode, int *len);
  3716. /* ===============================================================
  3717. *
  3718. * FONT
  3719. *
  3720. * ===============================================================*/
  3721. /* Font handling in this library was designed to be quite customizable and lets
  3722. you decide what you want to use and what you want to provide. There are three
  3723. different ways to use the font atlas. The first two will use your font
  3724. handling scheme and only requires essential data to run nuklear. The next
  3725. slightly more advanced features is font handling with vertex buffer output.
  3726. Finally the most complex API wise is using nuklear's font baking API.
  3727. 1.) Using your own implementation without vertex buffer output
  3728. --------------------------------------------------------------
  3729. So first up the easiest way to do font handling is by just providing a
  3730. `nk_user_font` struct which only requires the height in pixel of the used
  3731. font and a callback to calculate the width of a string. This way of handling
  3732. fonts is best fitted for using the normal draw shape command API where you
  3733. do all the text drawing yourself and the library does not require any kind
  3734. of deeper knowledge about which font handling mechanism you use.
  3735. IMPORTANT: the `nk_user_font` pointer provided to nuklear has to persist
  3736. over the complete life time! I know this sucks but it is currently the only
  3737. way to switch between fonts.
  3738. float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
  3739. {
  3740. your_font_type *type = handle.ptr;
  3741. float text_width = ...;
  3742. return text_width;
  3743. }
  3744. struct nk_user_font font;
  3745. font.userdata.ptr = &your_font_class_or_struct;
  3746. font.height = your_font_height;
  3747. font.width = your_text_width_calculation;
  3748. struct nk_context ctx;
  3749. nk_init_default(&ctx, &font);
  3750. 2.) Using your own implementation with vertex buffer output
  3751. --------------------------------------------------------------
  3752. While the first approach works fine if you don't want to use the optional
  3753. vertex buffer output it is not enough if you do. To get font handling working
  3754. for these cases you have to provide two additional parameters inside the
  3755. `nk_user_font`. First a texture atlas handle used to draw text as subimages
  3756. of a bigger font atlas texture and a callback to query a character's glyph
  3757. information (offset, size, ...). So it is still possible to provide your own
  3758. font and use the vertex buffer output.
  3759. float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
  3760. {
  3761. your_font_type *type = handle.ptr;
  3762. float text_width = ...;
  3763. return text_width;
  3764. }
  3765. void query_your_font_glyph(nk_handle handle, float font_height, struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
  3766. {
  3767. your_font_type *type = handle.ptr;
  3768. glyph.width = ...;
  3769. glyph.height = ...;
  3770. glyph.xadvance = ...;
  3771. glyph.uv[0].x = ...;
  3772. glyph.uv[0].y = ...;
  3773. glyph.uv[1].x = ...;
  3774. glyph.uv[1].y = ...;
  3775. glyph.offset.x = ...;
  3776. glyph.offset.y = ...;
  3777. }
  3778. struct nk_user_font font;
  3779. font.userdata.ptr = &your_font_class_or_struct;
  3780. font.height = your_font_height;
  3781. font.width = your_text_width_calculation;
  3782. font.query = query_your_font_glyph;
  3783. font.texture.id = your_font_texture;
  3784. struct nk_context ctx;
  3785. nk_init_default(&ctx, &font);
  3786. 3.) Nuklear font baker
  3787. ------------------------------------
  3788. The final approach if you do not have a font handling functionality or don't
  3789. want to use it in this library is by using the optional font baker.
  3790. The font baker APIs can be used to create a font plus font atlas texture
  3791. and can be used with or without the vertex buffer output.
  3792. It still uses the `nk_user_font` struct and the two different approaches
  3793. previously stated still work. The font baker is not located inside
  3794. `nk_context` like all other systems since it can be understood as more of
  3795. an extension to nuklear and does not really depend on any `nk_context` state.
  3796. Font baker need to be initialized first by one of the nk_font_atlas_init_xxx
  3797. functions. If you don't care about memory just call the default version
  3798. `nk_font_atlas_init_default` which will allocate all memory from the standard library.
  3799. If you want to control memory allocation but you don't care if the allocated
  3800. memory is temporary and therefore can be freed directly after the baking process
  3801. is over or permanent you can call `nk_font_atlas_init`.
  3802. After successfully initializing the font baker you can add Truetype(.ttf) fonts from
  3803. different sources like memory or from file by calling one of the `nk_font_atlas_add_xxx`.
  3804. functions. Adding font will permanently store each font, font config and ttf memory block(!)
  3805. inside the font atlas and allows to reuse the font atlas. If you don't want to reuse
  3806. the font baker by for example adding additional fonts you can call
  3807. `nk_font_atlas_cleanup` after the baking process is over (after calling nk_font_atlas_end).
  3808. As soon as you added all fonts you wanted you can now start the baking process
  3809. for every selected glyph to image by calling `nk_font_atlas_bake`.
  3810. The baking process returns image memory, width and height which can be used to
  3811. either create your own image object or upload it to any graphics library.
  3812. No matter which case you finally have to call `nk_font_atlas_end` which
  3813. will free all temporary memory including the font atlas image so make sure
  3814. you created our texture beforehand. `nk_font_atlas_end` requires a handle
  3815. to your font texture or object and optionally fills a `struct nk_draw_null_texture`
  3816. which can be used for the optional vertex output. If you don't want it just
  3817. set the argument to `NULL`.
  3818. At this point you are done and if you don't want to reuse the font atlas you
  3819. can call `nk_font_atlas_cleanup` to free all truetype blobs and configuration
  3820. memory. Finally if you don't use the font atlas and any of it's fonts anymore
  3821. you need to call `nk_font_atlas_clear` to free all memory still being used.
  3822. struct nk_font_atlas atlas;
  3823. nk_font_atlas_init_default(&atlas);
  3824. nk_font_atlas_begin(&atlas);
  3825. nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, 0);
  3826. nk_font *font2 = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font2.ttf", 16, 0);
  3827. const void* img = nk_font_atlas_bake(&atlas, &img_width, &img_height, NK_FONT_ATLAS_RGBA32);
  3828. nk_font_atlas_end(&atlas, nk_handle_id(texture), 0);
  3829. struct nk_context ctx;
  3830. nk_init_default(&ctx, &font->handle);
  3831. while (1) {
  3832. }
  3833. nk_font_atlas_clear(&atlas);
  3834. The font baker API is probably the most complex API inside this library and
  3835. I would suggest reading some of my examples `example/` to get a grip on how
  3836. to use the font atlas. There are a number of details I left out. For example
  3837. how to merge fonts, configure a font with `nk_font_config` to use other languages,
  3838. use another texture coordinate format and a lot more:
  3839. struct nk_font_config cfg = nk_font_config(font_pixel_height);
  3840. cfg.merge_mode = nk_false or nk_true;
  3841. cfg.range = nk_font_korean_glyph_ranges();
  3842. cfg.coord_type = NK_COORD_PIXEL;
  3843. nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, &cfg);
  3844. */
  3845. struct nk_user_font_glyph;
  3846. typedef float(*nk_text_width_f)(nk_handle, float h, const char*, int len);
  3847. typedef void(*nk_query_font_glyph_f)(nk_handle handle, float font_height,
  3848. struct nk_user_font_glyph *glyph,
  3849. nk_rune codepoint, nk_rune next_codepoint);
  3850. #if defined(NK_INCLUDE_VERTEX_BUFFER_OUTPUT) || defined(NK_INCLUDE_SOFTWARE_FONT)
  3851. struct nk_user_font_glyph {
  3852. struct nk_vec2 uv[2];
  3853. /* texture coordinates */
  3854. struct nk_vec2 offset;
  3855. /* offset between top left and glyph */
  3856. float width, height;
  3857. /* size of the glyph */
  3858. float xadvance;
  3859. /* offset to the next glyph */
  3860. };
  3861. #endif
  3862. struct nk_user_font {
  3863. nk_handle userdata;
  3864. /* user provided font handle */
  3865. float height;
  3866. /* max height of the font */
  3867. nk_text_width_f width;
  3868. /* font string width in pixel callback */
  3869. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  3870. nk_query_font_glyph_f query;
  3871. /* font glyph callback to query drawing info */
  3872. nk_handle texture;
  3873. /* texture handle to the used font atlas or texture */
  3874. #endif
  3875. };
  3876. #ifdef NK_INCLUDE_FONT_BAKING
  3877. enum nk_font_coord_type {
  3878. NK_COORD_UV, /* texture coordinates inside font glyphs are clamped between 0-1 */
  3879. NK_COORD_PIXEL /* texture coordinates inside font glyphs are in absolute pixel */
  3880. };
  3881. struct nk_font;
  3882. struct nk_baked_font {
  3883. float height;
  3884. /* height of the font */
  3885. float ascent, descent;
  3886. /* font glyphs ascent and descent */
  3887. nk_rune glyph_offset;
  3888. /* glyph array offset inside the font glyph baking output array */
  3889. nk_rune glyph_count;
  3890. /* number of glyphs of this font inside the glyph baking array output */
  3891. const nk_rune *ranges;
  3892. /* font codepoint ranges as pairs of (from/to) and 0 as last element */
  3893. };
  3894. struct nk_font_config {
  3895. struct nk_font_config *next;
  3896. /* NOTE: only used internally */
  3897. void *ttf_blob;
  3898. /* pointer to loaded TTF file memory block.
  3899. * NOTE: not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
  3900. nk_size ttf_size;
  3901. /* size of the loaded TTF file memory block
  3902. * NOTE: not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
  3903. unsigned char ttf_data_owned_by_atlas;
  3904. /* used inside font atlas: default to: 0*/
  3905. unsigned char merge_mode;
  3906. /* merges this font into the last font */
  3907. unsigned char pixel_snap;
  3908. /* align every character to pixel boundary (if true set oversample (1,1)) */
  3909. unsigned char oversample_v, oversample_h;
  3910. /* rasterize at hight quality for sub-pixel position */
  3911. unsigned char padding[3];
  3912. float size;
  3913. /* baked pixel height of the font */
  3914. enum nk_font_coord_type coord_type;
  3915. /* texture coordinate format with either pixel or UV coordinates */
  3916. struct nk_vec2 spacing;
  3917. /* extra pixel spacing between glyphs */
  3918. const nk_rune *range;
  3919. /* list of unicode ranges (2 values per range, zero terminated) */
  3920. struct nk_baked_font *font;
  3921. /* font to setup in the baking process: NOTE: not needed for font atlas */
  3922. nk_rune fallback_glyph;
  3923. /* fallback glyph to use if a given rune is not found */
  3924. struct nk_font_config *n;
  3925. struct nk_font_config *p;
  3926. };
  3927. struct nk_font_glyph {
  3928. nk_rune codepoint;
  3929. float xadvance;
  3930. float x0, y0, x1, y1, w, h;
  3931. float u0, v0, u1, v1;
  3932. };
  3933. struct nk_font {
  3934. struct nk_font *next;
  3935. struct nk_user_font handle;
  3936. struct nk_baked_font info;
  3937. float scale;
  3938. struct nk_font_glyph *glyphs;
  3939. const struct nk_font_glyph *fallback;
  3940. nk_rune fallback_codepoint;
  3941. nk_handle texture;
  3942. struct nk_font_config *config;
  3943. };
  3944. enum nk_font_atlas_format {
  3945. NK_FONT_ATLAS_ALPHA8,
  3946. NK_FONT_ATLAS_RGBA32
  3947. };
  3948. struct nk_font_atlas {
  3949. void *pixel;
  3950. int tex_width;
  3951. int tex_height;
  3952. struct nk_allocator permanent;
  3953. struct nk_allocator temporary;
  3954. struct nk_recti custom;
  3955. struct nk_cursor cursors[NK_CURSOR_COUNT];
  3956. int glyph_count;
  3957. struct nk_font_glyph *glyphs;
  3958. struct nk_font *default_font;
  3959. struct nk_font *fonts;
  3960. struct nk_font_config *config;
  3961. int font_num;
  3962. };
  3963. /* some language glyph codepoint ranges */
  3964. NK_API const nk_rune *nk_font_default_glyph_ranges(void);
  3965. NK_API const nk_rune *nk_font_chinese_glyph_ranges(void);
  3966. NK_API const nk_rune *nk_font_cyrillic_glyph_ranges(void);
  3967. NK_API const nk_rune *nk_font_korean_glyph_ranges(void);
  3968. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  3969. NK_API void nk_font_atlas_init_default(struct nk_font_atlas*);
  3970. #endif
  3971. NK_API void nk_font_atlas_init(struct nk_font_atlas*, struct nk_allocator*);
  3972. NK_API void nk_font_atlas_init_custom(struct nk_font_atlas*, struct nk_allocator *persistent, struct nk_allocator *transient);
  3973. NK_API void nk_font_atlas_begin(struct nk_font_atlas*);
  3974. NK_API struct nk_font_config nk_font_config(float pixel_height);
  3975. NK_API struct nk_font *nk_font_atlas_add(struct nk_font_atlas*, const struct nk_font_config*);
  3976. #ifdef NK_INCLUDE_DEFAULT_FONT
  3977. NK_API struct nk_font* nk_font_atlas_add_default(struct nk_font_atlas*, float height, const struct nk_font_config*);
  3978. #endif
  3979. 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);
  3980. #ifdef NK_INCLUDE_STANDARD_IO
  3981. 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*);
  3982. #endif
  3983. 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*);
  3984. 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);
  3985. NK_API const void* nk_font_atlas_bake(struct nk_font_atlas*, int *width, int *height, enum nk_font_atlas_format);
  3986. NK_API void nk_font_atlas_end(struct nk_font_atlas*, nk_handle tex, struct nk_draw_null_texture*);
  3987. NK_API const struct nk_font_glyph* nk_font_find_glyph(struct nk_font*, nk_rune unicode);
  3988. NK_API void nk_font_atlas_cleanup(struct nk_font_atlas *atlas);
  3989. NK_API void nk_font_atlas_clear(struct nk_font_atlas*);
  3990. #endif
  3991. /* ==============================================================
  3992. *
  3993. * MEMORY BUFFER
  3994. *
  3995. * ===============================================================*/
  3996. /* A basic (double)-buffer with linear allocation and resetting as only
  3997. freeing policy. The buffer's main purpose is to control all memory management
  3998. inside the GUI toolkit and still leave memory control as much as possible in
  3999. the hand of the user while also making sure the library is easy to use if
  4000. not as much control is needed.
  4001. In general all memory inside this library can be provided from the user in
  4002. three different ways.
  4003. The first way and the one providing most control is by just passing a fixed
  4004. size memory block. In this case all control lies in the hand of the user
  4005. since he can exactly control where the memory comes from and how much memory
  4006. the library should consume. Of course using the fixed size API removes the
  4007. ability to automatically resize a buffer if not enough memory is provided so
  4008. you have to take over the resizing. While being a fixed sized buffer sounds
  4009. quite limiting, it is very effective in this library since the actual memory
  4010. consumption is quite stable and has a fixed upper bound for a lot of cases.
  4011. If you don't want to think about how much memory the library should allocate
  4012. at all time or have a very dynamic UI with unpredictable memory consumption
  4013. habits but still want control over memory allocation you can use the dynamic
  4014. allocator based API. The allocator consists of two callbacks for allocating
  4015. and freeing memory and optional userdata so you can plugin your own allocator.
  4016. The final and easiest way can be used by defining
  4017. NK_INCLUDE_DEFAULT_ALLOCATOR which uses the standard library memory
  4018. allocation functions malloc and free and takes over complete control over
  4019. memory in this library.
  4020. */
  4021. struct nk_memory_status {
  4022. void *memory;
  4023. unsigned int type;
  4024. nk_size size;
  4025. nk_size allocated;
  4026. nk_size needed;
  4027. nk_size calls;
  4028. };
  4029. enum nk_allocation_type {
  4030. NK_BUFFER_FIXED,
  4031. NK_BUFFER_DYNAMIC
  4032. };
  4033. enum nk_buffer_allocation_type {
  4034. NK_BUFFER_FRONT,
  4035. NK_BUFFER_BACK,
  4036. NK_BUFFER_MAX
  4037. };
  4038. struct nk_buffer_marker {
  4039. int active;
  4040. nk_size offset;
  4041. };
  4042. struct nk_memory {void *ptr;nk_size size;};
  4043. struct nk_buffer {
  4044. struct nk_buffer_marker marker[NK_BUFFER_MAX];
  4045. /* buffer marker to free a buffer to a certain offset */
  4046. struct nk_allocator pool;
  4047. /* allocator callback for dynamic buffers */
  4048. enum nk_allocation_type type;
  4049. /* memory management type */
  4050. struct nk_memory memory;
  4051. /* memory and size of the current memory block */
  4052. float grow_factor;
  4053. /* growing factor for dynamic memory management */
  4054. nk_size allocated;
  4055. /* total amount of memory allocated */
  4056. nk_size needed;
  4057. /* totally consumed memory given that enough memory is present */
  4058. nk_size calls;
  4059. /* number of allocation calls */
  4060. nk_size size;
  4061. /* current size of the buffer */
  4062. };
  4063. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  4064. NK_API void nk_buffer_init_default(struct nk_buffer*);
  4065. #endif
  4066. NK_API void nk_buffer_init(struct nk_buffer*, const struct nk_allocator*, nk_size size);
  4067. NK_API void nk_buffer_init_fixed(struct nk_buffer*, void *memory, nk_size size);
  4068. NK_API void nk_buffer_info(struct nk_memory_status*, struct nk_buffer*);
  4069. NK_API void nk_buffer_push(struct nk_buffer*, enum nk_buffer_allocation_type type, const void *memory, nk_size size, nk_size align);
  4070. NK_API void nk_buffer_mark(struct nk_buffer*, enum nk_buffer_allocation_type type);
  4071. NK_API void nk_buffer_reset(struct nk_buffer*, enum nk_buffer_allocation_type type);
  4072. NK_API void nk_buffer_clear(struct nk_buffer*);
  4073. NK_API void nk_buffer_free(struct nk_buffer*);
  4074. NK_API void *nk_buffer_memory(struct nk_buffer*);
  4075. NK_API const void *nk_buffer_memory_const(const struct nk_buffer*);
  4076. NK_API nk_size nk_buffer_total(struct nk_buffer*);
  4077. /* ==============================================================
  4078. *
  4079. * STRING
  4080. *
  4081. * ===============================================================*/
  4082. /* Basic string buffer which is only used in context with the text editor
  4083. * to manage and manipulate dynamic or fixed size string content. This is _NOT_
  4084. * the default string handling method. The only instance you should have any contact
  4085. * with this API is if you interact with an `nk_text_edit` object inside one of the
  4086. * copy and paste functions and even there only for more advanced cases. */
  4087. struct nk_str {
  4088. struct nk_buffer buffer;
  4089. int len; /* in codepoints/runes/glyphs */
  4090. };
  4091. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  4092. NK_API void nk_str_init_default(struct nk_str*);
  4093. #endif
  4094. NK_API void nk_str_init(struct nk_str*, const struct nk_allocator*, nk_size size);
  4095. NK_API void nk_str_init_fixed(struct nk_str*, void *memory, nk_size size);
  4096. NK_API void nk_str_clear(struct nk_str*);
  4097. NK_API void nk_str_free(struct nk_str*);
  4098. NK_API int nk_str_append_text_char(struct nk_str*, const char*, int);
  4099. NK_API int nk_str_append_str_char(struct nk_str*, const char*);
  4100. NK_API int nk_str_append_text_utf8(struct nk_str*, const char*, int);
  4101. NK_API int nk_str_append_str_utf8(struct nk_str*, const char*);
  4102. NK_API int nk_str_append_text_runes(struct nk_str*, const nk_rune*, int);
  4103. NK_API int nk_str_append_str_runes(struct nk_str*, const nk_rune*);
  4104. NK_API int nk_str_insert_at_char(struct nk_str*, int pos, const char*, int);
  4105. NK_API int nk_str_insert_at_rune(struct nk_str*, int pos, const char*, int);
  4106. NK_API int nk_str_insert_text_char(struct nk_str*, int pos, const char*, int);
  4107. NK_API int nk_str_insert_str_char(struct nk_str*, int pos, const char*);
  4108. NK_API int nk_str_insert_text_utf8(struct nk_str*, int pos, const char*, int);
  4109. NK_API int nk_str_insert_str_utf8(struct nk_str*, int pos, const char*);
  4110. NK_API int nk_str_insert_text_runes(struct nk_str*, int pos, const nk_rune*, int);
  4111. NK_API int nk_str_insert_str_runes(struct nk_str*, int pos, const nk_rune*);
  4112. NK_API void nk_str_remove_chars(struct nk_str*, int len);
  4113. NK_API void nk_str_remove_runes(struct nk_str *str, int len);
  4114. NK_API void nk_str_delete_chars(struct nk_str*, int pos, int len);
  4115. NK_API void nk_str_delete_runes(struct nk_str*, int pos, int len);
  4116. NK_API char *nk_str_at_char(struct nk_str*, int pos);
  4117. NK_API char *nk_str_at_rune(struct nk_str*, int pos, nk_rune *unicode, int *len);
  4118. NK_API nk_rune nk_str_rune_at(const struct nk_str*, int pos);
  4119. NK_API const char *nk_str_at_char_const(const struct nk_str*, int pos);
  4120. NK_API const char *nk_str_at_const(const struct nk_str*, int pos, nk_rune *unicode, int *len);
  4121. NK_API char *nk_str_get(struct nk_str*);
  4122. NK_API const char *nk_str_get_const(const struct nk_str*);
  4123. NK_API int nk_str_len(struct nk_str*);
  4124. NK_API int nk_str_len_char(struct nk_str*);
  4125. /*===============================================================
  4126. *
  4127. * TEXT EDITOR
  4128. *
  4129. * ===============================================================*/
  4130. /* Editing text in this library is handled by either `nk_edit_string` or
  4131. * `nk_edit_buffer`. But like almost everything in this library there are multiple
  4132. * ways of doing it and a balance between control and ease of use with memory
  4133. * as well as functionality controlled by flags.
  4134. *
  4135. * This library generally allows three different levels of memory control:
  4136. * First of is the most basic way of just providing a simple char array with
  4137. * string length. This method is probably the easiest way of handling simple
  4138. * user text input. Main upside is complete control over memory while the biggest
  4139. * downside in comparison with the other two approaches is missing undo/redo.
  4140. *
  4141. * For UIs that require undo/redo the second way was created. It is based on
  4142. * a fixed size nk_text_edit struct, which has an internal undo/redo stack.
  4143. * This is mainly useful if you want something more like a text editor but don't want
  4144. * to have a dynamically growing buffer.
  4145. *
  4146. * The final way is using a dynamically growing nk_text_edit struct, which
  4147. * has both a default version if you don't care where memory comes from and an
  4148. * allocator version if you do. While the text editor is quite powerful for its
  4149. * complexity I would not recommend editing gigabytes of data with it.
  4150. * It is rather designed for uses cases which make sense for a GUI library not for
  4151. * an full blown text editor.
  4152. */
  4153. #ifndef NK_TEXTEDIT_UNDOSTATECOUNT
  4154. #define NK_TEXTEDIT_UNDOSTATECOUNT 99
  4155. #endif
  4156. #ifndef NK_TEXTEDIT_UNDOCHARCOUNT
  4157. #define NK_TEXTEDIT_UNDOCHARCOUNT 999
  4158. #endif
  4159. struct nk_text_edit;
  4160. struct nk_clipboard {
  4161. nk_handle userdata;
  4162. nk_plugin_paste paste;
  4163. nk_plugin_copy copy;
  4164. };
  4165. struct nk_text_undo_record {
  4166. int where;
  4167. short insert_length;
  4168. short delete_length;
  4169. short char_storage;
  4170. };
  4171. struct nk_text_undo_state {
  4172. struct nk_text_undo_record undo_rec[NK_TEXTEDIT_UNDOSTATECOUNT];
  4173. nk_rune undo_char[NK_TEXTEDIT_UNDOCHARCOUNT];
  4174. short undo_point;
  4175. short redo_point;
  4176. short undo_char_point;
  4177. short redo_char_point;
  4178. };
  4179. enum nk_text_edit_type {
  4180. NK_TEXT_EDIT_SINGLE_LINE,
  4181. NK_TEXT_EDIT_MULTI_LINE
  4182. };
  4183. enum nk_text_edit_mode {
  4184. NK_TEXT_EDIT_MODE_VIEW,
  4185. NK_TEXT_EDIT_MODE_INSERT,
  4186. NK_TEXT_EDIT_MODE_REPLACE
  4187. };
  4188. struct nk_text_edit {
  4189. struct nk_clipboard clip;
  4190. struct nk_str string;
  4191. nk_plugin_filter filter;
  4192. struct nk_vec2 scrollbar;
  4193. int cursor;
  4194. int select_start;
  4195. int select_end;
  4196. unsigned char mode;
  4197. unsigned char cursor_at_end_of_line;
  4198. unsigned char initialized;
  4199. unsigned char has_preferred_x;
  4200. unsigned char single_line;
  4201. unsigned char active;
  4202. unsigned char padding1;
  4203. float preferred_x;
  4204. struct nk_text_undo_state undo;
  4205. };
  4206. /* filter function */
  4207. NK_API int nk_filter_default(const struct nk_text_edit*, nk_rune unicode);
  4208. NK_API int nk_filter_ascii(const struct nk_text_edit*, nk_rune unicode);
  4209. NK_API int nk_filter_float(const struct nk_text_edit*, nk_rune unicode);
  4210. NK_API int nk_filter_decimal(const struct nk_text_edit*, nk_rune unicode);
  4211. NK_API int nk_filter_hex(const struct nk_text_edit*, nk_rune unicode);
  4212. NK_API int nk_filter_oct(const struct nk_text_edit*, nk_rune unicode);
  4213. NK_API int nk_filter_binary(const struct nk_text_edit*, nk_rune unicode);
  4214. /* text editor */
  4215. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  4216. NK_API void nk_textedit_init_default(struct nk_text_edit*);
  4217. #endif
  4218. NK_API void nk_textedit_init(struct nk_text_edit*, struct nk_allocator*, nk_size size);
  4219. NK_API void nk_textedit_init_fixed(struct nk_text_edit*, void *memory, nk_size size);
  4220. NK_API void nk_textedit_free(struct nk_text_edit*);
  4221. NK_API void nk_textedit_text(struct nk_text_edit*, const char*, int total_len);
  4222. NK_API void nk_textedit_delete(struct nk_text_edit*, int where, int len);
  4223. NK_API void nk_textedit_delete_selection(struct nk_text_edit*);
  4224. NK_API void nk_textedit_select_all(struct nk_text_edit*);
  4225. NK_API int nk_textedit_cut(struct nk_text_edit*);
  4226. NK_API int nk_textedit_paste(struct nk_text_edit*, char const*, int len);
  4227. NK_API void nk_textedit_undo(struct nk_text_edit*);
  4228. NK_API void nk_textedit_redo(struct nk_text_edit*);
  4229. /* ===============================================================
  4230. *
  4231. * DRAWING
  4232. *
  4233. * ===============================================================*/
  4234. /* This library was designed to be render backend agnostic so it does
  4235. not draw anything to screen. Instead all drawn shapes, widgets
  4236. are made of, are buffered into memory and make up a command queue.
  4237. Each frame therefore fills the command buffer with draw commands
  4238. that then need to be executed by the user and his own render backend.
  4239. After that the command buffer needs to be cleared and a new frame can be
  4240. started. It is probably important to note that the command buffer is the main
  4241. drawing API and the optional vertex buffer API only takes this format and
  4242. converts it into a hardware accessible format.
  4243. To use the command queue to draw your own widgets you can access the
  4244. command buffer of each window by calling `nk_window_get_canvas` after
  4245. previously having called `nk_begin`:
  4246. void draw_red_rectangle_widget(struct nk_context *ctx)
  4247. {
  4248. struct nk_command_buffer *canvas;
  4249. struct nk_input *input = &ctx->input;
  4250. canvas = nk_window_get_canvas(ctx);
  4251. struct nk_rect space;
  4252. enum nk_widget_layout_states state;
  4253. state = nk_widget(&space, ctx);
  4254. if (!state) return;
  4255. if (state != NK_WIDGET_ROM)
  4256. update_your_widget_by_user_input(...);
  4257. nk_fill_rect(canvas, space, 0, nk_rgb(255,0,0));
  4258. }
  4259. if (nk_begin(...)) {
  4260. nk_layout_row_dynamic(ctx, 25, 1);
  4261. draw_red_rectangle_widget(ctx);
  4262. }
  4263. nk_end(..)
  4264. Important to know if you want to create your own widgets is the `nk_widget`
  4265. call. It allocates space on the panel reserved for this widget to be used,
  4266. but also returns the state of the widget space. If your widget is not seen and does
  4267. not have to be updated it is '0' and you can just return. If it only has
  4268. to be drawn the state will be `NK_WIDGET_ROM` otherwise you can do both
  4269. update and draw your widget. The reason for separating is to only draw and
  4270. update what is actually necessary which is crucial for performance.
  4271. */
  4272. enum nk_command_type {
  4273. NK_COMMAND_NOP,
  4274. NK_COMMAND_SCISSOR,
  4275. NK_COMMAND_LINE,
  4276. NK_COMMAND_CURVE,
  4277. NK_COMMAND_RECT,
  4278. NK_COMMAND_RECT_FILLED,
  4279. NK_COMMAND_RECT_MULTI_COLOR,
  4280. NK_COMMAND_CIRCLE,
  4281. NK_COMMAND_CIRCLE_FILLED,
  4282. NK_COMMAND_ARC,
  4283. NK_COMMAND_ARC_FILLED,
  4284. NK_COMMAND_TRIANGLE,
  4285. NK_COMMAND_TRIANGLE_FILLED,
  4286. NK_COMMAND_POLYGON,
  4287. NK_COMMAND_POLYGON_FILLED,
  4288. NK_COMMAND_POLYLINE,
  4289. NK_COMMAND_TEXT,
  4290. NK_COMMAND_IMAGE,
  4291. NK_COMMAND_CUSTOM
  4292. };
  4293. /* command base and header of every command inside the buffer */
  4294. struct nk_command {
  4295. enum nk_command_type type;
  4296. nk_size next;
  4297. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4298. nk_handle userdata;
  4299. #endif
  4300. };
  4301. struct nk_command_scissor {
  4302. struct nk_command header;
  4303. short x, y;
  4304. unsigned short w, h;
  4305. };
  4306. struct nk_command_line {
  4307. struct nk_command header;
  4308. unsigned short line_thickness;
  4309. struct nk_vec2i begin;
  4310. struct nk_vec2i end;
  4311. struct nk_color color;
  4312. };
  4313. struct nk_command_curve {
  4314. struct nk_command header;
  4315. unsigned short line_thickness;
  4316. struct nk_vec2i begin;
  4317. struct nk_vec2i end;
  4318. struct nk_vec2i ctrl[2];
  4319. struct nk_color color;
  4320. };
  4321. struct nk_command_rect {
  4322. struct nk_command header;
  4323. unsigned short rounding;
  4324. unsigned short line_thickness;
  4325. short x, y;
  4326. unsigned short w, h;
  4327. struct nk_color color;
  4328. };
  4329. struct nk_command_rect_filled {
  4330. struct nk_command header;
  4331. unsigned short rounding;
  4332. short x, y;
  4333. unsigned short w, h;
  4334. struct nk_color color;
  4335. };
  4336. struct nk_command_rect_multi_color {
  4337. struct nk_command header;
  4338. short x, y;
  4339. unsigned short w, h;
  4340. struct nk_color left;
  4341. struct nk_color top;
  4342. struct nk_color bottom;
  4343. struct nk_color right;
  4344. };
  4345. struct nk_command_triangle {
  4346. struct nk_command header;
  4347. unsigned short line_thickness;
  4348. struct nk_vec2i a;
  4349. struct nk_vec2i b;
  4350. struct nk_vec2i c;
  4351. struct nk_color color;
  4352. };
  4353. struct nk_command_triangle_filled {
  4354. struct nk_command header;
  4355. struct nk_vec2i a;
  4356. struct nk_vec2i b;
  4357. struct nk_vec2i c;
  4358. struct nk_color color;
  4359. };
  4360. struct nk_command_circle {
  4361. struct nk_command header;
  4362. short x, y;
  4363. unsigned short line_thickness;
  4364. unsigned short w, h;
  4365. struct nk_color color;
  4366. };
  4367. struct nk_command_circle_filled {
  4368. struct nk_command header;
  4369. short x, y;
  4370. unsigned short w, h;
  4371. struct nk_color color;
  4372. };
  4373. struct nk_command_arc {
  4374. struct nk_command header;
  4375. short cx, cy;
  4376. unsigned short r;
  4377. unsigned short line_thickness;
  4378. float a[2];
  4379. struct nk_color color;
  4380. };
  4381. struct nk_command_arc_filled {
  4382. struct nk_command header;
  4383. short cx, cy;
  4384. unsigned short r;
  4385. float a[2];
  4386. struct nk_color color;
  4387. };
  4388. struct nk_command_polygon {
  4389. struct nk_command header;
  4390. struct nk_color color;
  4391. unsigned short line_thickness;
  4392. unsigned short point_count;
  4393. struct nk_vec2i points[1];
  4394. };
  4395. struct nk_command_polygon_filled {
  4396. struct nk_command header;
  4397. struct nk_color color;
  4398. unsigned short point_count;
  4399. struct nk_vec2i points[1];
  4400. };
  4401. struct nk_command_polyline {
  4402. struct nk_command header;
  4403. struct nk_color color;
  4404. unsigned short line_thickness;
  4405. unsigned short point_count;
  4406. struct nk_vec2i points[1];
  4407. };
  4408. struct nk_command_image {
  4409. struct nk_command header;
  4410. short x, y;
  4411. unsigned short w, h;
  4412. struct nk_image img;
  4413. struct nk_color col;
  4414. };
  4415. typedef void (*nk_command_custom_callback)(void *canvas, short x,short y,
  4416. unsigned short w, unsigned short h, nk_handle callback_data);
  4417. struct nk_command_custom {
  4418. struct nk_command header;
  4419. short x, y;
  4420. unsigned short w, h;
  4421. nk_handle callback_data;
  4422. nk_command_custom_callback callback;
  4423. };
  4424. struct nk_command_text {
  4425. struct nk_command header;
  4426. const struct nk_user_font *font;
  4427. struct nk_color background;
  4428. struct nk_color foreground;
  4429. short x, y;
  4430. unsigned short w, h;
  4431. float height;
  4432. int length;
  4433. char string[1];
  4434. };
  4435. enum nk_command_clipping {
  4436. NK_CLIPPING_OFF = nk_false,
  4437. NK_CLIPPING_ON = nk_true
  4438. };
  4439. struct nk_command_buffer {
  4440. struct nk_buffer *base;
  4441. struct nk_rect clip;
  4442. int use_clipping;
  4443. nk_handle userdata;
  4444. nk_size begin, end, last;
  4445. };
  4446. /* shape outlines */
  4447. 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);
  4448. NK_API void nk_stroke_curve(struct nk_command_buffer*, float, float, float, float, float, float, float, float, float line_thickness, struct nk_color);
  4449. NK_API void nk_stroke_rect(struct nk_command_buffer*, struct nk_rect, float rounding, float line_thickness, struct nk_color);
  4450. NK_API void nk_stroke_circle(struct nk_command_buffer*, struct nk_rect, float line_thickness, struct nk_color);
  4451. 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);
  4452. NK_API void nk_stroke_triangle(struct nk_command_buffer*, float, float, float, float, float, float, float line_thichness, struct nk_color);
  4453. NK_API void nk_stroke_polyline(struct nk_command_buffer*, float *points, int point_count, float line_thickness, struct nk_color col);
  4454. NK_API void nk_stroke_polygon(struct nk_command_buffer*, float*, int point_count, float line_thickness, struct nk_color);
  4455. /* filled shades */
  4456. NK_API void nk_fill_rect(struct nk_command_buffer*, struct nk_rect, float rounding, struct nk_color);
  4457. 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);
  4458. NK_API void nk_fill_circle(struct nk_command_buffer*, struct nk_rect, struct nk_color);
  4459. 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);
  4460. 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);
  4461. NK_API void nk_fill_polygon(struct nk_command_buffer*, float*, int point_count, struct nk_color);
  4462. /* misc */
  4463. NK_API void nk_draw_image(struct nk_command_buffer*, struct nk_rect, const struct nk_image*, struct nk_color);
  4464. 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);
  4465. NK_API void nk_push_scissor(struct nk_command_buffer*, struct nk_rect);
  4466. NK_API void nk_push_custom(struct nk_command_buffer*, struct nk_rect, nk_command_custom_callback, nk_handle usr);
  4467. /* ===============================================================
  4468. *
  4469. * INPUT
  4470. *
  4471. * ===============================================================*/
  4472. struct nk_mouse_button {
  4473. int down;
  4474. unsigned int clicked;
  4475. struct nk_vec2 clicked_pos;
  4476. };
  4477. struct nk_mouse {
  4478. struct nk_mouse_button buttons[NK_BUTTON_MAX];
  4479. struct nk_vec2 pos;
  4480. struct nk_vec2 prev;
  4481. struct nk_vec2 delta;
  4482. struct nk_vec2 scroll_delta;
  4483. unsigned char grab;
  4484. unsigned char grabbed;
  4485. unsigned char ungrab;
  4486. };
  4487. struct nk_key {
  4488. int down;
  4489. unsigned int clicked;
  4490. };
  4491. struct nk_keyboard {
  4492. struct nk_key keys[NK_KEY_MAX];
  4493. char text[NK_INPUT_MAX];
  4494. int text_len;
  4495. };
  4496. struct nk_input {
  4497. struct nk_keyboard keyboard;
  4498. struct nk_mouse mouse;
  4499. };
  4500. NK_API int nk_input_has_mouse_click(const struct nk_input*, enum nk_buttons);
  4501. NK_API int nk_input_has_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4502. NK_API int nk_input_has_mouse_click_down_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect, int down);
  4503. NK_API int nk_input_is_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4504. 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);
  4505. NK_API int nk_input_any_mouse_click_in_rect(const struct nk_input*, struct nk_rect);
  4506. NK_API int nk_input_is_mouse_prev_hovering_rect(const struct nk_input*, struct nk_rect);
  4507. NK_API int nk_input_is_mouse_hovering_rect(const struct nk_input*, struct nk_rect);
  4508. NK_API int nk_input_mouse_clicked(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4509. NK_API int nk_input_is_mouse_down(const struct nk_input*, enum nk_buttons);
  4510. NK_API int nk_input_is_mouse_pressed(const struct nk_input*, enum nk_buttons);
  4511. NK_API int nk_input_is_mouse_released(const struct nk_input*, enum nk_buttons);
  4512. NK_API int nk_input_is_key_pressed(const struct nk_input*, enum nk_keys);
  4513. NK_API int nk_input_is_key_released(const struct nk_input*, enum nk_keys);
  4514. NK_API int nk_input_is_key_down(const struct nk_input*, enum nk_keys);
  4515. /* ===============================================================
  4516. *
  4517. * DRAW LIST
  4518. *
  4519. * ===============================================================*/
  4520. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  4521. /* The optional vertex buffer draw list provides a 2D drawing context
  4522. with antialiasing functionality which takes basic filled or outlined shapes
  4523. or a path and outputs vertexes, elements and draw commands.
  4524. The actual draw list API is not required to be used directly while using this
  4525. library since converting the default library draw command output is done by
  4526. just calling `nk_convert` but I decided to still make this library accessible
  4527. since it can be useful.
  4528. The draw list is based on a path buffering and polygon and polyline
  4529. rendering API which allows a lot of ways to draw 2D content to screen.
  4530. In fact it is probably more powerful than needed but allows even more crazy
  4531. things than this library provides by default.
  4532. */
  4533. #ifdef NK_UINT_DRAW_INDEX
  4534. typedef nk_uint nk_draw_index;
  4535. #else
  4536. typedef nk_ushort nk_draw_index;
  4537. #endif
  4538. enum nk_draw_list_stroke {
  4539. NK_STROKE_OPEN = nk_false,
  4540. /* build up path has no connection back to the beginning */
  4541. NK_STROKE_CLOSED = nk_true
  4542. /* build up path has a connection back to the beginning */
  4543. };
  4544. enum nk_draw_vertex_layout_attribute {
  4545. NK_VERTEX_POSITION,
  4546. NK_VERTEX_COLOR,
  4547. NK_VERTEX_TEXCOORD,
  4548. NK_VERTEX_ATTRIBUTE_COUNT
  4549. };
  4550. enum nk_draw_vertex_layout_format {
  4551. NK_FORMAT_SCHAR,
  4552. NK_FORMAT_SSHORT,
  4553. NK_FORMAT_SINT,
  4554. NK_FORMAT_UCHAR,
  4555. NK_FORMAT_USHORT,
  4556. NK_FORMAT_UINT,
  4557. NK_FORMAT_FLOAT,
  4558. NK_FORMAT_DOUBLE,
  4559. NK_FORMAT_COLOR_BEGIN,
  4560. NK_FORMAT_R8G8B8 = NK_FORMAT_COLOR_BEGIN,
  4561. NK_FORMAT_R16G15B16,
  4562. NK_FORMAT_R32G32B32,
  4563. NK_FORMAT_R8G8B8A8,
  4564. NK_FORMAT_B8G8R8A8,
  4565. NK_FORMAT_R16G15B16A16,
  4566. NK_FORMAT_R32G32B32A32,
  4567. NK_FORMAT_R32G32B32A32_FLOAT,
  4568. NK_FORMAT_R32G32B32A32_DOUBLE,
  4569. NK_FORMAT_RGB32,
  4570. NK_FORMAT_RGBA32,
  4571. NK_FORMAT_COLOR_END = NK_FORMAT_RGBA32,
  4572. NK_FORMAT_COUNT
  4573. };
  4574. #define NK_VERTEX_LAYOUT_END NK_VERTEX_ATTRIBUTE_COUNT,NK_FORMAT_COUNT,0
  4575. struct nk_draw_vertex_layout_element {
  4576. enum nk_draw_vertex_layout_attribute attribute;
  4577. enum nk_draw_vertex_layout_format format;
  4578. nk_size offset;
  4579. };
  4580. struct nk_draw_command {
  4581. unsigned int elem_count;
  4582. /* number of elements in the current draw batch */
  4583. struct nk_rect clip_rect;
  4584. /* current screen clipping rectangle */
  4585. nk_handle texture;
  4586. /* current texture to set */
  4587. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4588. nk_handle userdata;
  4589. #endif
  4590. };
  4591. struct nk_draw_list {
  4592. struct nk_rect clip_rect;
  4593. struct nk_vec2 circle_vtx[12];
  4594. struct nk_convert_config config;
  4595. struct nk_buffer *buffer;
  4596. struct nk_buffer *vertices;
  4597. struct nk_buffer *elements;
  4598. unsigned int element_count;
  4599. unsigned int vertex_count;
  4600. unsigned int cmd_count;
  4601. nk_size cmd_offset;
  4602. unsigned int path_count;
  4603. unsigned int path_offset;
  4604. enum nk_anti_aliasing line_AA;
  4605. enum nk_anti_aliasing shape_AA;
  4606. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4607. nk_handle userdata;
  4608. #endif
  4609. };
  4610. /* draw list */
  4611. NK_API void nk_draw_list_init(struct nk_draw_list*);
  4612. 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);
  4613. /* drawing */
  4614. #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))
  4615. NK_API const struct nk_draw_command* nk__draw_list_begin(const struct nk_draw_list*, const struct nk_buffer*);
  4616. 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*);
  4617. NK_API const struct nk_draw_command* nk__draw_list_end(const struct nk_draw_list*, const struct nk_buffer*);
  4618. /* path */
  4619. NK_API void nk_draw_list_path_clear(struct nk_draw_list*);
  4620. NK_API void nk_draw_list_path_line_to(struct nk_draw_list*, struct nk_vec2 pos);
  4621. 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);
  4622. 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);
  4623. NK_API void nk_draw_list_path_rect_to(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, float rounding);
  4624. 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);
  4625. NK_API void nk_draw_list_path_fill(struct nk_draw_list*, struct nk_color);
  4626. NK_API void nk_draw_list_path_stroke(struct nk_draw_list*, struct nk_color, enum nk_draw_list_stroke closed, float thickness);
  4627. /* stroke */
  4628. 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);
  4629. NK_API void nk_draw_list_stroke_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding, float thickness);
  4630. 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);
  4631. 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);
  4632. 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);
  4633. 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);
  4634. /* fill */
  4635. NK_API void nk_draw_list_fill_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding);
  4636. 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);
  4637. 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);
  4638. 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);
  4639. 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);
  4640. /* misc */
  4641. NK_API void nk_draw_list_add_image(struct nk_draw_list*, struct nk_image texture, struct nk_rect rect, struct nk_color);
  4642. 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);
  4643. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4644. NK_API void nk_draw_list_push_userdata(struct nk_draw_list*, nk_handle userdata);
  4645. #endif
  4646. #endif
  4647. /* ===============================================================
  4648. *
  4649. * GUI
  4650. *
  4651. * ===============================================================*/
  4652. enum nk_style_item_type {
  4653. NK_STYLE_ITEM_COLOR,
  4654. NK_STYLE_ITEM_IMAGE
  4655. };
  4656. union nk_style_item_data {
  4657. struct nk_image image;
  4658. struct nk_color color;
  4659. };
  4660. struct nk_style_item {
  4661. enum nk_style_item_type type;
  4662. union nk_style_item_data data;
  4663. };
  4664. struct nk_style_text {
  4665. struct nk_color color;
  4666. struct nk_vec2 padding;
  4667. };
  4668. struct nk_style_button {
  4669. /* background */
  4670. struct nk_style_item normal;
  4671. struct nk_style_item hover;
  4672. struct nk_style_item active;
  4673. struct nk_color border_color;
  4674. /* text */
  4675. struct nk_color text_background;
  4676. struct nk_color text_normal;
  4677. struct nk_color text_hover;
  4678. struct nk_color text_active;
  4679. nk_flags text_alignment;
  4680. /* properties */
  4681. float border;
  4682. float rounding;
  4683. struct nk_vec2 padding;
  4684. struct nk_vec2 image_padding;
  4685. struct nk_vec2 touch_padding;
  4686. /* optional user callbacks */
  4687. nk_handle userdata;
  4688. void(*draw_begin)(struct nk_command_buffer*, nk_handle userdata);
  4689. void(*draw_end)(struct nk_command_buffer*, nk_handle userdata);
  4690. };
  4691. struct nk_style_toggle {
  4692. /* background */
  4693. struct nk_style_item normal;
  4694. struct nk_style_item hover;
  4695. struct nk_style_item active;
  4696. struct nk_color border_color;
  4697. /* cursor */
  4698. struct nk_style_item cursor_normal;
  4699. struct nk_style_item cursor_hover;
  4700. /* text */
  4701. struct nk_color text_normal;
  4702. struct nk_color text_hover;
  4703. struct nk_color text_active;
  4704. struct nk_color text_background;
  4705. nk_flags text_alignment;
  4706. /* properties */
  4707. struct nk_vec2 padding;
  4708. struct nk_vec2 touch_padding;
  4709. float spacing;
  4710. float border;
  4711. /* optional user callbacks */
  4712. nk_handle userdata;
  4713. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4714. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4715. };
  4716. struct nk_style_selectable {
  4717. /* background (inactive) */
  4718. struct nk_style_item normal;
  4719. struct nk_style_item hover;
  4720. struct nk_style_item pressed;
  4721. /* background (active) */
  4722. struct nk_style_item normal_active;
  4723. struct nk_style_item hover_active;
  4724. struct nk_style_item pressed_active;
  4725. /* text color (inactive) */
  4726. struct nk_color text_normal;
  4727. struct nk_color text_hover;
  4728. struct nk_color text_pressed;
  4729. /* text color (active) */
  4730. struct nk_color text_normal_active;
  4731. struct nk_color text_hover_active;
  4732. struct nk_color text_pressed_active;
  4733. struct nk_color text_background;
  4734. nk_flags text_alignment;
  4735. /* properties */
  4736. float rounding;
  4737. struct nk_vec2 padding;
  4738. struct nk_vec2 touch_padding;
  4739. struct nk_vec2 image_padding;
  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_slider {
  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. /* background bar */
  4752. struct nk_color bar_normal;
  4753. struct nk_color bar_hover;
  4754. struct nk_color bar_active;
  4755. struct nk_color bar_filled;
  4756. /* cursor */
  4757. struct nk_style_item cursor_normal;
  4758. struct nk_style_item cursor_hover;
  4759. struct nk_style_item cursor_active;
  4760. /* properties */
  4761. float border;
  4762. float rounding;
  4763. float bar_height;
  4764. struct nk_vec2 padding;
  4765. struct nk_vec2 spacing;
  4766. struct nk_vec2 cursor_size;
  4767. /* optional buttons */
  4768. int show_buttons;
  4769. struct nk_style_button inc_button;
  4770. struct nk_style_button dec_button;
  4771. enum nk_symbol_type inc_symbol;
  4772. enum nk_symbol_type dec_symbol;
  4773. /* optional user callbacks */
  4774. nk_handle userdata;
  4775. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4776. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4777. };
  4778. struct nk_style_progress {
  4779. /* background */
  4780. struct nk_style_item normal;
  4781. struct nk_style_item hover;
  4782. struct nk_style_item active;
  4783. struct nk_color border_color;
  4784. /* cursor */
  4785. struct nk_style_item cursor_normal;
  4786. struct nk_style_item cursor_hover;
  4787. struct nk_style_item cursor_active;
  4788. struct nk_color cursor_border_color;
  4789. /* properties */
  4790. float rounding;
  4791. float border;
  4792. float cursor_border;
  4793. float cursor_rounding;
  4794. struct nk_vec2 padding;
  4795. /* optional user callbacks */
  4796. nk_handle userdata;
  4797. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4798. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4799. };
  4800. struct nk_style_scrollbar {
  4801. /* background */
  4802. struct nk_style_item normal;
  4803. struct nk_style_item hover;
  4804. struct nk_style_item active;
  4805. struct nk_color border_color;
  4806. /* cursor */
  4807. struct nk_style_item cursor_normal;
  4808. struct nk_style_item cursor_hover;
  4809. struct nk_style_item cursor_active;
  4810. struct nk_color cursor_border_color;
  4811. /* properties */
  4812. float border;
  4813. float rounding;
  4814. float border_cursor;
  4815. float rounding_cursor;
  4816. struct nk_vec2 padding;
  4817. /* optional buttons */
  4818. int show_buttons;
  4819. struct nk_style_button inc_button;
  4820. struct nk_style_button dec_button;
  4821. enum nk_symbol_type inc_symbol;
  4822. enum nk_symbol_type dec_symbol;
  4823. /* optional user callbacks */
  4824. nk_handle userdata;
  4825. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4826. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4827. };
  4828. struct nk_style_edit {
  4829. /* background */
  4830. struct nk_style_item normal;
  4831. struct nk_style_item hover;
  4832. struct nk_style_item active;
  4833. struct nk_color border_color;
  4834. struct nk_style_scrollbar scrollbar;
  4835. /* cursor */
  4836. struct nk_color cursor_normal;
  4837. struct nk_color cursor_hover;
  4838. struct nk_color cursor_text_normal;
  4839. struct nk_color cursor_text_hover;
  4840. /* text (unselected) */
  4841. struct nk_color text_normal;
  4842. struct nk_color text_hover;
  4843. struct nk_color text_active;
  4844. /* text (selected) */
  4845. struct nk_color selected_normal;
  4846. struct nk_color selected_hover;
  4847. struct nk_color selected_text_normal;
  4848. struct nk_color selected_text_hover;
  4849. /* properties */
  4850. float border;
  4851. float rounding;
  4852. float cursor_size;
  4853. struct nk_vec2 scrollbar_size;
  4854. struct nk_vec2 padding;
  4855. float row_padding;
  4856. };
  4857. struct nk_style_property {
  4858. /* background */
  4859. struct nk_style_item normal;
  4860. struct nk_style_item hover;
  4861. struct nk_style_item active;
  4862. struct nk_color border_color;
  4863. /* text */
  4864. struct nk_color label_normal;
  4865. struct nk_color label_hover;
  4866. struct nk_color label_active;
  4867. /* symbols */
  4868. enum nk_symbol_type sym_left;
  4869. enum nk_symbol_type sym_right;
  4870. /* properties */
  4871. float border;
  4872. float rounding;
  4873. struct nk_vec2 padding;
  4874. struct nk_style_edit edit;
  4875. struct nk_style_button inc_button;
  4876. struct nk_style_button dec_button;
  4877. /* optional user callbacks */
  4878. nk_handle userdata;
  4879. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4880. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4881. };
  4882. struct nk_style_chart {
  4883. /* colors */
  4884. struct nk_style_item background;
  4885. struct nk_color border_color;
  4886. struct nk_color selected_color;
  4887. struct nk_color color;
  4888. /* properties */
  4889. float border;
  4890. float rounding;
  4891. struct nk_vec2 padding;
  4892. };
  4893. struct nk_style_combo {
  4894. /* background */
  4895. struct nk_style_item normal;
  4896. struct nk_style_item hover;
  4897. struct nk_style_item active;
  4898. struct nk_color border_color;
  4899. /* label */
  4900. struct nk_color label_normal;
  4901. struct nk_color label_hover;
  4902. struct nk_color label_active;
  4903. /* symbol */
  4904. struct nk_color symbol_normal;
  4905. struct nk_color symbol_hover;
  4906. struct nk_color symbol_active;
  4907. /* button */
  4908. struct nk_style_button button;
  4909. enum nk_symbol_type sym_normal;
  4910. enum nk_symbol_type sym_hover;
  4911. enum nk_symbol_type sym_active;
  4912. /* properties */
  4913. float border;
  4914. float rounding;
  4915. struct nk_vec2 content_padding;
  4916. struct nk_vec2 button_padding;
  4917. struct nk_vec2 spacing;
  4918. };
  4919. struct nk_style_tab {
  4920. /* background */
  4921. struct nk_style_item background;
  4922. struct nk_color border_color;
  4923. struct nk_color text;
  4924. /* button */
  4925. struct nk_style_button tab_maximize_button;
  4926. struct nk_style_button tab_minimize_button;
  4927. struct nk_style_button node_maximize_button;
  4928. struct nk_style_button node_minimize_button;
  4929. enum nk_symbol_type sym_minimize;
  4930. enum nk_symbol_type sym_maximize;
  4931. /* properties */
  4932. float border;
  4933. float rounding;
  4934. float indent;
  4935. struct nk_vec2 padding;
  4936. struct nk_vec2 spacing;
  4937. };
  4938. enum nk_style_header_align {
  4939. NK_HEADER_LEFT,
  4940. NK_HEADER_RIGHT
  4941. };
  4942. struct nk_style_window_header {
  4943. /* background */
  4944. struct nk_style_item normal;
  4945. struct nk_style_item hover;
  4946. struct nk_style_item active;
  4947. /* button */
  4948. struct nk_style_button close_button;
  4949. struct nk_style_button minimize_button;
  4950. enum nk_symbol_type close_symbol;
  4951. enum nk_symbol_type minimize_symbol;
  4952. enum nk_symbol_type maximize_symbol;
  4953. /* title */
  4954. struct nk_color label_normal;
  4955. struct nk_color label_hover;
  4956. struct nk_color label_active;
  4957. /* properties */
  4958. enum nk_style_header_align align;
  4959. struct nk_vec2 padding;
  4960. struct nk_vec2 label_padding;
  4961. struct nk_vec2 spacing;
  4962. };
  4963. struct nk_style_window {
  4964. struct nk_style_window_header header;
  4965. struct nk_style_item fixed_background;
  4966. struct nk_color background;
  4967. struct nk_color border_color;
  4968. struct nk_color popup_border_color;
  4969. struct nk_color combo_border_color;
  4970. struct nk_color contextual_border_color;
  4971. struct nk_color menu_border_color;
  4972. struct nk_color group_border_color;
  4973. struct nk_color tooltip_border_color;
  4974. struct nk_style_item scaler;
  4975. float border;
  4976. float combo_border;
  4977. float contextual_border;
  4978. float menu_border;
  4979. float group_border;
  4980. float tooltip_border;
  4981. float popup_border;
  4982. float min_row_height_padding;
  4983. float rounding;
  4984. struct nk_vec2 spacing;
  4985. struct nk_vec2 scrollbar_size;
  4986. struct nk_vec2 min_size;
  4987. struct nk_vec2 padding;
  4988. struct nk_vec2 group_padding;
  4989. struct nk_vec2 popup_padding;
  4990. struct nk_vec2 combo_padding;
  4991. struct nk_vec2 contextual_padding;
  4992. struct nk_vec2 menu_padding;
  4993. struct nk_vec2 tooltip_padding;
  4994. };
  4995. struct nk_style {
  4996. const struct nk_user_font *font;
  4997. const struct nk_cursor *cursors[NK_CURSOR_COUNT];
  4998. const struct nk_cursor *cursor_active;
  4999. struct nk_cursor *cursor_last;
  5000. int cursor_visible;
  5001. struct nk_style_text text;
  5002. struct nk_style_button button;
  5003. struct nk_style_button contextual_button;
  5004. struct nk_style_button menu_button;
  5005. struct nk_style_toggle option;
  5006. struct nk_style_toggle checkbox;
  5007. struct nk_style_selectable selectable;
  5008. struct nk_style_slider slider;
  5009. struct nk_style_progress progress;
  5010. struct nk_style_property property;
  5011. struct nk_style_edit edit;
  5012. struct nk_style_chart chart;
  5013. struct nk_style_scrollbar scrollh;
  5014. struct nk_style_scrollbar scrollv;
  5015. struct nk_style_tab tab;
  5016. struct nk_style_combo combo;
  5017. struct nk_style_window window;
  5018. };
  5019. NK_API struct nk_style_item nk_style_item_image(struct nk_image img);
  5020. NK_API struct nk_style_item nk_style_item_color(struct nk_color);
  5021. NK_API struct nk_style_item nk_style_item_hide(void);
  5022. /*==============================================================
  5023. * PANEL
  5024. * =============================================================*/
  5025. #ifndef NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS
  5026. #define NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS 16
  5027. #endif
  5028. #ifndef NK_CHART_MAX_SLOT
  5029. #define NK_CHART_MAX_SLOT 4
  5030. #endif
  5031. enum nk_panel_type {
  5032. NK_PANEL_NONE = 0,
  5033. NK_PANEL_WINDOW = NK_FLAG(0),
  5034. NK_PANEL_GROUP = NK_FLAG(1),
  5035. NK_PANEL_POPUP = NK_FLAG(2),
  5036. NK_PANEL_CONTEXTUAL = NK_FLAG(4),
  5037. NK_PANEL_COMBO = NK_FLAG(5),
  5038. NK_PANEL_MENU = NK_FLAG(6),
  5039. NK_PANEL_TOOLTIP = NK_FLAG(7)
  5040. };
  5041. enum nk_panel_set {
  5042. NK_PANEL_SET_NONBLOCK = NK_PANEL_CONTEXTUAL|NK_PANEL_COMBO|NK_PANEL_MENU|NK_PANEL_TOOLTIP,
  5043. NK_PANEL_SET_POPUP = NK_PANEL_SET_NONBLOCK|NK_PANEL_POPUP,
  5044. NK_PANEL_SET_SUB = NK_PANEL_SET_POPUP|NK_PANEL_GROUP
  5045. };
  5046. struct nk_chart_slot {
  5047. enum nk_chart_type type;
  5048. struct nk_color color;
  5049. struct nk_color highlight;
  5050. float min, max, range;
  5051. int count;
  5052. struct nk_vec2 last;
  5053. int index;
  5054. };
  5055. struct nk_chart {
  5056. int slot;
  5057. float x, y, w, h;
  5058. struct nk_chart_slot slots[NK_CHART_MAX_SLOT];
  5059. };
  5060. enum nk_panel_row_layout_type {
  5061. NK_LAYOUT_DYNAMIC_FIXED = 0,
  5062. NK_LAYOUT_DYNAMIC_ROW,
  5063. NK_LAYOUT_DYNAMIC_FREE,
  5064. NK_LAYOUT_DYNAMIC,
  5065. NK_LAYOUT_STATIC_FIXED,
  5066. NK_LAYOUT_STATIC_ROW,
  5067. NK_LAYOUT_STATIC_FREE,
  5068. NK_LAYOUT_STATIC,
  5069. NK_LAYOUT_TEMPLATE,
  5070. NK_LAYOUT_COUNT
  5071. };
  5072. struct nk_row_layout {
  5073. enum nk_panel_row_layout_type type;
  5074. int index;
  5075. float height;
  5076. float min_height;
  5077. int columns;
  5078. const float *ratio;
  5079. float item_width;
  5080. float item_height;
  5081. float item_offset;
  5082. float filled;
  5083. struct nk_rect item;
  5084. int tree_depth;
  5085. float templates[NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS];
  5086. };
  5087. struct nk_popup_buffer {
  5088. nk_size begin;
  5089. nk_size parent;
  5090. nk_size last;
  5091. nk_size end;
  5092. int active;
  5093. };
  5094. struct nk_menu_state {
  5095. float x, y, w, h;
  5096. struct nk_scroll offset;
  5097. };
  5098. struct nk_panel {
  5099. enum nk_panel_type type;
  5100. nk_flags flags;
  5101. struct nk_rect bounds;
  5102. nk_uint *offset_x;
  5103. nk_uint *offset_y;
  5104. float at_x, at_y, max_x;
  5105. float footer_height;
  5106. float header_height;
  5107. float border;
  5108. unsigned int has_scrolling;
  5109. struct nk_rect clip;
  5110. struct nk_menu_state menu;
  5111. struct nk_row_layout row;
  5112. struct nk_chart chart;
  5113. struct nk_command_buffer *buffer;
  5114. struct nk_panel *parent;
  5115. };
  5116. /*==============================================================
  5117. * WINDOW
  5118. * =============================================================*/
  5119. #ifndef NK_WINDOW_MAX_NAME
  5120. #define NK_WINDOW_MAX_NAME 64
  5121. #endif
  5122. struct nk_table;
  5123. enum nk_window_flags {
  5124. NK_WINDOW_PRIVATE = NK_FLAG(11),
  5125. NK_WINDOW_DYNAMIC = NK_WINDOW_PRIVATE,
  5126. /* special window type growing up in height while being filled to a certain maximum height */
  5127. NK_WINDOW_ROM = NK_FLAG(12),
  5128. /* sets window widgets into a read only mode and does not allow input changes */
  5129. NK_WINDOW_NOT_INTERACTIVE = NK_WINDOW_ROM|NK_WINDOW_NO_INPUT,
  5130. /* prevents all interaction caused by input to either window or widgets inside */
  5131. NK_WINDOW_HIDDEN = NK_FLAG(13),
  5132. /* Hides window and stops any window interaction and drawing */
  5133. NK_WINDOW_CLOSED = NK_FLAG(14),
  5134. /* Directly closes and frees the window at the end of the frame */
  5135. NK_WINDOW_MINIMIZED = NK_FLAG(15),
  5136. /* marks the window as minimized */
  5137. NK_WINDOW_REMOVE_ROM = NK_FLAG(16)
  5138. /* Removes read only mode at the end of the window */
  5139. };
  5140. struct nk_popup_state {
  5141. struct nk_window *win;
  5142. enum nk_panel_type type;
  5143. struct nk_popup_buffer buf;
  5144. nk_hash name;
  5145. int active;
  5146. unsigned combo_count;
  5147. unsigned con_count, con_old;
  5148. unsigned active_con;
  5149. struct nk_rect header;
  5150. };
  5151. struct nk_edit_state {
  5152. nk_hash name;
  5153. unsigned int seq;
  5154. unsigned int old;
  5155. int active, prev;
  5156. int cursor;
  5157. int sel_start;
  5158. int sel_end;
  5159. struct nk_scroll scrollbar;
  5160. unsigned char mode;
  5161. unsigned char single_line;
  5162. };
  5163. struct nk_property_state {
  5164. int active, prev;
  5165. char buffer[NK_MAX_NUMBER_BUFFER];
  5166. int length;
  5167. int cursor;
  5168. int select_start;
  5169. int select_end;
  5170. nk_hash name;
  5171. unsigned int seq;
  5172. unsigned int old;
  5173. int state;
  5174. };
  5175. struct nk_window {
  5176. unsigned int seq;
  5177. nk_hash name;
  5178. char name_string[NK_WINDOW_MAX_NAME];
  5179. nk_flags flags;
  5180. struct nk_rect bounds;
  5181. struct nk_scroll scrollbar;
  5182. struct nk_command_buffer buffer;
  5183. struct nk_panel *layout;
  5184. float scrollbar_hiding_timer;
  5185. /* persistent widget state */
  5186. struct nk_property_state property;
  5187. struct nk_popup_state popup;
  5188. struct nk_edit_state edit;
  5189. unsigned int scrolled;
  5190. struct nk_table *tables;
  5191. unsigned int table_count;
  5192. /* window list hooks */
  5193. struct nk_window *next;
  5194. struct nk_window *prev;
  5195. struct nk_window *parent;
  5196. };
  5197. /*==============================================================
  5198. * STACK
  5199. * =============================================================*/
  5200. /* The style modifier stack can be used to temporarily change a
  5201. * property inside `nk_style`. For example if you want a special
  5202. * red button you can temporarily push the old button color onto a stack
  5203. * draw the button with a red color and then you just pop the old color
  5204. * back from the stack:
  5205. *
  5206. * nk_style_push_style_item(ctx, &ctx->style.button.normal, nk_style_item_color(nk_rgb(255,0,0)));
  5207. * nk_style_push_style_item(ctx, &ctx->style.button.hover, nk_style_item_color(nk_rgb(255,0,0)));
  5208. * nk_style_push_style_item(ctx, &ctx->style.button.active, nk_style_item_color(nk_rgb(255,0,0)));
  5209. * nk_style_push_vec2(ctx, &cx->style.button.padding, nk_vec2(2,2));
  5210. *
  5211. * nk_button(...);
  5212. *
  5213. * nk_style_pop_style_item(ctx);
  5214. * nk_style_pop_style_item(ctx);
  5215. * nk_style_pop_style_item(ctx);
  5216. * nk_style_pop_vec2(ctx);
  5217. *
  5218. * Nuklear has a stack for style_items, float properties, vector properties,
  5219. * flags, colors, fonts and for button_behavior. Each has it's own fixed size stack
  5220. * which can be changed at compile time.
  5221. */
  5222. #ifndef NK_BUTTON_BEHAVIOR_STACK_SIZE
  5223. #define NK_BUTTON_BEHAVIOR_STACK_SIZE 8
  5224. #endif
  5225. #ifndef NK_FONT_STACK_SIZE
  5226. #define NK_FONT_STACK_SIZE 8
  5227. #endif
  5228. #ifndef NK_STYLE_ITEM_STACK_SIZE
  5229. #define NK_STYLE_ITEM_STACK_SIZE 16
  5230. #endif
  5231. #ifndef NK_FLOAT_STACK_SIZE
  5232. #define NK_FLOAT_STACK_SIZE 32
  5233. #endif
  5234. #ifndef NK_VECTOR_STACK_SIZE
  5235. #define NK_VECTOR_STACK_SIZE 16
  5236. #endif
  5237. #ifndef NK_FLAGS_STACK_SIZE
  5238. #define NK_FLAGS_STACK_SIZE 32
  5239. #endif
  5240. #ifndef NK_COLOR_STACK_SIZE
  5241. #define NK_COLOR_STACK_SIZE 32
  5242. #endif
  5243. #define NK_CONFIGURATION_STACK_TYPE(prefix, name, type)\
  5244. struct nk_config_stack_##name##_element {\
  5245. prefix##_##type *address;\
  5246. prefix##_##type old_value;\
  5247. }
  5248. #define NK_CONFIG_STACK(type,size)\
  5249. struct nk_config_stack_##type {\
  5250. int head;\
  5251. struct nk_config_stack_##type##_element elements[size];\
  5252. }
  5253. #define nk_float float
  5254. NK_CONFIGURATION_STACK_TYPE(struct nk, style_item, style_item);
  5255. NK_CONFIGURATION_STACK_TYPE(nk ,float, float);
  5256. NK_CONFIGURATION_STACK_TYPE(struct nk, vec2, vec2);
  5257. NK_CONFIGURATION_STACK_TYPE(nk ,flags, flags);
  5258. NK_CONFIGURATION_STACK_TYPE(struct nk, color, color);
  5259. NK_CONFIGURATION_STACK_TYPE(const struct nk, user_font, user_font*);
  5260. NK_CONFIGURATION_STACK_TYPE(enum nk, button_behavior, button_behavior);
  5261. NK_CONFIG_STACK(style_item, NK_STYLE_ITEM_STACK_SIZE);
  5262. NK_CONFIG_STACK(float, NK_FLOAT_STACK_SIZE);
  5263. NK_CONFIG_STACK(vec2, NK_VECTOR_STACK_SIZE);
  5264. NK_CONFIG_STACK(flags, NK_FLAGS_STACK_SIZE);
  5265. NK_CONFIG_STACK(color, NK_COLOR_STACK_SIZE);
  5266. NK_CONFIG_STACK(user_font, NK_FONT_STACK_SIZE);
  5267. NK_CONFIG_STACK(button_behavior, NK_BUTTON_BEHAVIOR_STACK_SIZE);
  5268. struct nk_configuration_stacks {
  5269. struct nk_config_stack_style_item style_items;
  5270. struct nk_config_stack_float floats;
  5271. struct nk_config_stack_vec2 vectors;
  5272. struct nk_config_stack_flags flags;
  5273. struct nk_config_stack_color colors;
  5274. struct nk_config_stack_user_font fonts;
  5275. struct nk_config_stack_button_behavior button_behaviors;
  5276. };
  5277. /*==============================================================
  5278. * CONTEXT
  5279. * =============================================================*/
  5280. #define NK_VALUE_PAGE_CAPACITY \
  5281. (((NK_MAX(sizeof(struct nk_window),sizeof(struct nk_panel)) / sizeof(nk_uint))) / 2)
  5282. struct nk_table {
  5283. unsigned int seq;
  5284. unsigned int size;
  5285. nk_hash keys[NK_VALUE_PAGE_CAPACITY];
  5286. nk_uint values[NK_VALUE_PAGE_CAPACITY];
  5287. struct nk_table *next, *prev;
  5288. };
  5289. union nk_page_data {
  5290. struct nk_table tbl;
  5291. struct nk_panel pan;
  5292. struct nk_window win;
  5293. };
  5294. struct nk_page_element {
  5295. union nk_page_data data;
  5296. struct nk_page_element *next;
  5297. struct nk_page_element *prev;
  5298. };
  5299. struct nk_page {
  5300. unsigned int size;
  5301. struct nk_page *next;
  5302. struct nk_page_element win[1];
  5303. };
  5304. struct nk_pool {
  5305. struct nk_allocator alloc;
  5306. enum nk_allocation_type type;
  5307. unsigned int page_count;
  5308. struct nk_page *pages;
  5309. struct nk_page_element *freelist;
  5310. unsigned capacity;
  5311. nk_size size;
  5312. nk_size cap;
  5313. };
  5314. struct nk_context {
  5315. /* public: can be accessed freely */
  5316. struct nk_input input;
  5317. struct nk_style style;
  5318. struct nk_buffer memory;
  5319. struct nk_clipboard clip;
  5320. nk_flags last_widget_state;
  5321. enum nk_button_behavior button_behavior;
  5322. struct nk_configuration_stacks stacks;
  5323. float delta_time_seconds;
  5324. /* private:
  5325. should only be accessed if you
  5326. know what you are doing */
  5327. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  5328. struct nk_draw_list draw_list;
  5329. #endif
  5330. #ifdef NK_INCLUDE_COMMAND_USERDATA
  5331. nk_handle userdata;
  5332. #endif
  5333. /* text editor objects are quite big because of an internal
  5334. * undo/redo stack. Therefore it does not make sense to have one for
  5335. * each window for temporary use cases, so I only provide *one* instance
  5336. * for all windows. This works because the content is cleared anyway */
  5337. struct nk_text_edit text_edit;
  5338. /* draw buffer used for overlay drawing operation like cursor */
  5339. struct nk_command_buffer overlay;
  5340. /* windows */
  5341. int build;
  5342. int use_pool;
  5343. struct nk_pool pool;
  5344. struct nk_window *begin;
  5345. struct nk_window *end;
  5346. struct nk_window *active;
  5347. struct nk_window *current;
  5348. struct nk_page_element *freelist;
  5349. unsigned int count;
  5350. unsigned int seq;
  5351. };
  5352. /* ==============================================================
  5353. * MATH
  5354. * =============================================================== */
  5355. #define NK_PI 3.141592654f
  5356. #define NK_UTF_INVALID 0xFFFD
  5357. #define NK_MAX_FLOAT_PRECISION 2
  5358. #define NK_UNUSED(x) ((void)(x))
  5359. #define NK_SATURATE(x) (NK_MAX(0, NK_MIN(1.0f, x)))
  5360. #define NK_LEN(a) (sizeof(a)/sizeof(a)[0])
  5361. #define NK_ABS(a) (((a) < 0) ? -(a) : (a))
  5362. #define NK_BETWEEN(x, a, b) ((a) <= (x) && (x) < (b))
  5363. #define NK_INBOX(px, py, x, y, w, h)\
  5364. (NK_BETWEEN(px,x,x+w) && NK_BETWEEN(py,y,y+h))
  5365. #define NK_INTERSECT(x0, y0, w0, h0, x1, y1, w1, h1) \
  5366. (!(((x1 > (x0 + w0)) || ((x1 + w1) < x0) || (y1 > (y0 + h0)) || (y1 + h1) < y0)))
  5367. #define NK_CONTAINS(x, y, w, h, bx, by, bw, bh)\
  5368. (NK_INBOX(x,y, bx, by, bw, bh) && NK_INBOX(x+w,y+h, bx, by, bw, bh))
  5369. #define nk_vec2_sub(a, b) nk_vec2((a).x - (b).x, (a).y - (b).y)
  5370. #define nk_vec2_add(a, b) nk_vec2((a).x + (b).x, (a).y + (b).y)
  5371. #define nk_vec2_len_sqr(a) ((a).x*(a).x+(a).y*(a).y)
  5372. #define nk_vec2_muls(a, t) nk_vec2((a).x * (t), (a).y * (t))
  5373. #define nk_ptr_add(t, p, i) ((t*)((void*)((nk_byte*)(p) + (i))))
  5374. #define nk_ptr_add_const(t, p, i) ((const t*)((const void*)((const nk_byte*)(p) + (i))))
  5375. #define nk_zero_struct(s) nk_zero(&s, sizeof(s))
  5376. /* ==============================================================
  5377. * ALIGNMENT
  5378. * =============================================================== */
  5379. /* Pointer to Integer type conversion for pointer alignment */
  5380. #if defined(__PTRDIFF_TYPE__) /* This case should work for GCC*/
  5381. # define NK_UINT_TO_PTR(x) ((void*)(__PTRDIFF_TYPE__)(x))
  5382. # define NK_PTR_TO_UINT(x) ((nk_size)(__PTRDIFF_TYPE__)(x))
  5383. #elif !defined(__GNUC__) /* works for compilers other than LLVM */
  5384. # define NK_UINT_TO_PTR(x) ((void*)&((char*)0)[x])
  5385. # define NK_PTR_TO_UINT(x) ((nk_size)(((char*)x)-(char*)0))
  5386. #elif defined(NK_USE_FIXED_TYPES) /* used if we have <stdint.h> */
  5387. # define NK_UINT_TO_PTR(x) ((void*)(uintptr_t)(x))
  5388. # define NK_PTR_TO_UINT(x) ((uintptr_t)(x))
  5389. #else /* generates warning but works */
  5390. # define NK_UINT_TO_PTR(x) ((void*)(x))
  5391. # define NK_PTR_TO_UINT(x) ((nk_size)(x))
  5392. #endif
  5393. #define NK_ALIGN_PTR(x, mask)\
  5394. (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x) + (mask-1)) & ~(mask-1))))
  5395. #define NK_ALIGN_PTR_BACK(x, mask)\
  5396. (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x)) & ~(mask-1))))
  5397. #define NK_OFFSETOF(st,m) ((nk_ptr)&(((st*)0)->m))
  5398. #define NK_CONTAINER_OF(ptr,type,member)\
  5399. (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member)))
  5400. #ifdef __cplusplus
  5401. }
  5402. #endif
  5403. #ifdef __cplusplus
  5404. template<typename T> struct nk_alignof;
  5405. template<typename T, int size_diff> struct nk_helper{enum {value = size_diff};};
  5406. template<typename T> struct nk_helper<T,0>{enum {value = nk_alignof<T>::value};};
  5407. template<typename T> struct nk_alignof{struct Big {T x; char c;}; enum {
  5408. diff = sizeof(Big) - sizeof(T), value = nk_helper<Big, diff>::value};};
  5409. #define NK_ALIGNOF(t) (nk_alignof<t>::value)
  5410. #elif defined(_MSC_VER)
  5411. #define NK_ALIGNOF(t) (__alignof(t))
  5412. #else
  5413. #define NK_ALIGNOF(t) ((char*)(&((struct {char c; t _h;}*)0)->_h) - (char*)0)
  5414. #endif
  5415. #endif /* NK_NUKLEAR_H_ */
  5416. #ifdef NK_IMPLEMENTATION
  5417. #ifndef NK_INTERNAL_H
  5418. #define NK_INTERNAL_H
  5419. #ifndef NK_POOL_DEFAULT_CAPACITY
  5420. #define NK_POOL_DEFAULT_CAPACITY 16
  5421. #endif
  5422. #ifndef NK_DEFAULT_COMMAND_BUFFER_SIZE
  5423. #define NK_DEFAULT_COMMAND_BUFFER_SIZE (4*1024)
  5424. #endif
  5425. #ifndef NK_BUFFER_DEFAULT_INITIAL_SIZE
  5426. #define NK_BUFFER_DEFAULT_INITIAL_SIZE (4*1024)
  5427. #endif
  5428. /* standard library headers */
  5429. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  5430. #include <stdlib.h> /* malloc, free */
  5431. #endif
  5432. #ifdef NK_INCLUDE_STANDARD_IO
  5433. #include <stdio.h> /* fopen, fclose,... */
  5434. #endif
  5435. #ifndef NK_ASSERT
  5436. #include <assert.h>
  5437. #define NK_ASSERT(expr) assert(expr)
  5438. #endif
  5439. #ifndef NK_MEMSET
  5440. #define NK_MEMSET nk_memset
  5441. #endif
  5442. #ifndef NK_MEMCPY
  5443. #define NK_MEMCPY nk_memcopy
  5444. #endif
  5445. #ifndef NK_SQRT
  5446. #define NK_SQRT nk_sqrt
  5447. #endif
  5448. #ifndef NK_SIN
  5449. #define NK_SIN nk_sin
  5450. #endif
  5451. #ifndef NK_COS
  5452. #define NK_COS nk_cos
  5453. #endif
  5454. #ifndef NK_STRTOD
  5455. #define NK_STRTOD nk_strtod
  5456. #endif
  5457. #ifndef NK_DTOA
  5458. #define NK_DTOA nk_dtoa
  5459. #endif
  5460. #define NK_DEFAULT (-1)
  5461. #ifndef NK_VSNPRINTF
  5462. /* If your compiler does support `vsnprintf` I would highly recommend
  5463. * defining this to vsnprintf instead since `vsprintf` is basically
  5464. * unbelievable unsafe and should *NEVER* be used. But I have to support
  5465. * it since C89 only provides this unsafe version. */
  5466. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) ||\
  5467. (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  5468. (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L)) ||\
  5469. (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 500)) ||\
  5470. defined(_ISOC99_SOURCE) || defined(_BSD_SOURCE)
  5471. #define NK_VSNPRINTF(s,n,f,a) vsnprintf(s,n,f,a)
  5472. #else
  5473. #define NK_VSNPRINTF(s,n,f,a) vsprintf(s,f,a)
  5474. #endif
  5475. #endif
  5476. #define NK_SCHAR_MIN (-127)
  5477. #define NK_SCHAR_MAX 127
  5478. #define NK_UCHAR_MIN 0
  5479. #define NK_UCHAR_MAX 256
  5480. #define NK_SSHORT_MIN (-32767)
  5481. #define NK_SSHORT_MAX 32767
  5482. #define NK_USHORT_MIN 0
  5483. #define NK_USHORT_MAX 65535
  5484. #define NK_SINT_MIN (-2147483647)
  5485. #define NK_SINT_MAX 2147483647
  5486. #define NK_UINT_MIN 0
  5487. #define NK_UINT_MAX 4294967295u
  5488. /* Make sure correct type size:
  5489. * This will fire with a negative subscript error if the type sizes
  5490. * are set incorrectly by the compiler, and compile out if not */
  5491. NK_STATIC_ASSERT(sizeof(nk_size) >= sizeof(void*));
  5492. NK_STATIC_ASSERT(sizeof(nk_ptr) == sizeof(void*));
  5493. NK_STATIC_ASSERT(sizeof(nk_flags) >= 4);
  5494. NK_STATIC_ASSERT(sizeof(nk_rune) >= 4);
  5495. NK_STATIC_ASSERT(sizeof(nk_ushort) == 2);
  5496. NK_STATIC_ASSERT(sizeof(nk_short) == 2);
  5497. NK_STATIC_ASSERT(sizeof(nk_uint) == 4);
  5498. NK_STATIC_ASSERT(sizeof(nk_int) == 4);
  5499. NK_STATIC_ASSERT(sizeof(nk_byte) == 1);
  5500. NK_GLOBAL const struct nk_rect nk_null_rect = {-8192.0f, -8192.0f, 16384, 16384};
  5501. #define NK_FLOAT_PRECISION 0.00000000000001
  5502. NK_GLOBAL const struct nk_color nk_red = {255,0,0,255};
  5503. NK_GLOBAL const struct nk_color nk_green = {0,255,0,255};
  5504. NK_GLOBAL const struct nk_color nk_blue = {0,0,255,255};
  5505. NK_GLOBAL const struct nk_color nk_white = {255,255,255,255};
  5506. NK_GLOBAL const struct nk_color nk_black = {0,0,0,255};
  5507. NK_GLOBAL const struct nk_color nk_yellow = {255,255,0,255};
  5508. /* widget */
  5509. #define nk_widget_state_reset(s)\
  5510. if ((*(s)) & NK_WIDGET_STATE_MODIFIED)\
  5511. (*(s)) = NK_WIDGET_STATE_INACTIVE|NK_WIDGET_STATE_MODIFIED;\
  5512. else (*(s)) = NK_WIDGET_STATE_INACTIVE;
  5513. /* math */
  5514. NK_LIB float nk_inv_sqrt(float n);
  5515. NK_LIB float nk_sqrt(float x);
  5516. NK_LIB float nk_sin(float x);
  5517. NK_LIB float nk_cos(float x);
  5518. NK_LIB nk_uint nk_round_up_pow2(nk_uint v);
  5519. NK_LIB struct nk_rect nk_shrink_rect(struct nk_rect r, float amount);
  5520. NK_LIB struct nk_rect nk_pad_rect(struct nk_rect r, struct nk_vec2 pad);
  5521. NK_LIB void nk_unify(struct nk_rect *clip, const struct nk_rect *a, float x0, float y0, float x1, float y1);
  5522. NK_LIB double nk_pow(double x, int n);
  5523. NK_LIB int nk_ifloord(double x);
  5524. NK_LIB int nk_ifloorf(float x);
  5525. NK_LIB int nk_iceilf(float x);
  5526. NK_LIB int nk_log10(double n);
  5527. /* util */
  5528. enum {NK_DO_NOT_STOP_ON_NEW_LINE, NK_STOP_ON_NEW_LINE};
  5529. NK_LIB int nk_is_lower(int c);
  5530. NK_LIB int nk_is_upper(int c);
  5531. NK_LIB int nk_to_upper(int c);
  5532. NK_LIB int nk_to_lower(int c);
  5533. NK_LIB void* nk_memcopy(void *dst, const void *src, nk_size n);
  5534. NK_LIB void nk_memset(void *ptr, int c0, nk_size size);
  5535. NK_LIB void nk_zero(void *ptr, nk_size size);
  5536. NK_LIB char *nk_itoa(char *s, long n);
  5537. NK_LIB int nk_string_float_limit(char *string, int prec);
  5538. NK_LIB char *nk_dtoa(char *s, double n);
  5539. 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);
  5540. 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);
  5541. #ifdef NK_INCLUDE_STANDARD_VARARGS
  5542. NK_LIB int nk_strfmt(char *buf, int buf_size, const char *fmt, va_list args);
  5543. #endif
  5544. #ifdef NK_INCLUDE_STANDARD_IO
  5545. NK_LIB char *nk_file_load(const char* path, nk_size* siz, struct nk_allocator *alloc);
  5546. #endif
  5547. /* buffer */
  5548. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  5549. NK_LIB void* nk_malloc(nk_handle unused, void *old,nk_size size);
  5550. NK_LIB void nk_mfree(nk_handle unused, void *ptr);
  5551. #endif
  5552. NK_LIB void* nk_buffer_align(void *unaligned, nk_size align, nk_size *alignment, enum nk_buffer_allocation_type type);
  5553. NK_LIB void* nk_buffer_alloc(struct nk_buffer *b, enum nk_buffer_allocation_type type, nk_size size, nk_size align);
  5554. NK_LIB void* nk_buffer_realloc(struct nk_buffer *b, nk_size capacity, nk_size *size);
  5555. /* draw */
  5556. NK_LIB void nk_command_buffer_init(struct nk_command_buffer *cb, struct nk_buffer *b, enum nk_command_clipping clip);
  5557. NK_LIB void nk_command_buffer_reset(struct nk_command_buffer *b);
  5558. NK_LIB void* nk_command_buffer_push(struct nk_command_buffer* b, enum nk_command_type t, nk_size size);
  5559. 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);
  5560. /* buffering */
  5561. NK_LIB void nk_start_buffer(struct nk_context *ctx, struct nk_command_buffer *b);
  5562. NK_LIB void nk_start(struct nk_context *ctx, struct nk_window *win);
  5563. NK_LIB void nk_start_popup(struct nk_context *ctx, struct nk_window *win);
  5564. NK_LIB void nk_finish_popup(struct nk_context *ctx, struct nk_window*);
  5565. NK_LIB void nk_finish_buffer(struct nk_context *ctx, struct nk_command_buffer *b);
  5566. NK_LIB void nk_finish(struct nk_context *ctx, struct nk_window *w);
  5567. NK_LIB void nk_build(struct nk_context *ctx);
  5568. /* text editor */
  5569. NK_LIB void nk_textedit_clear_state(struct nk_text_edit *state, enum nk_text_edit_type type, nk_plugin_filter filter);
  5570. NK_LIB void nk_textedit_click(struct nk_text_edit *state, float x, float y, const struct nk_user_font *font, float row_height);
  5571. NK_LIB void nk_textedit_drag(struct nk_text_edit *state, float x, float y, const struct nk_user_font *font, float row_height);
  5572. 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);
  5573. /* window */
  5574. enum nk_window_insert_location {
  5575. NK_INSERT_BACK, /* inserts window into the back of list (front of screen) */
  5576. NK_INSERT_FRONT /* inserts window into the front of list (back of screen) */
  5577. };
  5578. NK_LIB void *nk_create_window(struct nk_context *ctx);
  5579. NK_LIB void nk_remove_window(struct nk_context*, struct nk_window*);
  5580. NK_LIB void nk_free_window(struct nk_context *ctx, struct nk_window *win);
  5581. NK_LIB struct nk_window *nk_find_window(struct nk_context *ctx, nk_hash hash, const char *name);
  5582. NK_LIB void nk_insert_window(struct nk_context *ctx, struct nk_window *win, enum nk_window_insert_location loc);
  5583. /* pool */
  5584. NK_LIB void nk_pool_init(struct nk_pool *pool, struct nk_allocator *alloc, unsigned int capacity);
  5585. NK_LIB void nk_pool_free(struct nk_pool *pool);
  5586. NK_LIB void nk_pool_init_fixed(struct nk_pool *pool, void *memory, nk_size size);
  5587. NK_LIB struct nk_page_element *nk_pool_alloc(struct nk_pool *pool);
  5588. /* page-element */
  5589. NK_LIB struct nk_page_element* nk_create_page_element(struct nk_context *ctx);
  5590. NK_LIB void nk_link_page_element_into_freelist(struct nk_context *ctx, struct nk_page_element *elem);
  5591. NK_LIB void nk_free_page_element(struct nk_context *ctx, struct nk_page_element *elem);
  5592. /* table */
  5593. NK_LIB struct nk_table* nk_create_table(struct nk_context *ctx);
  5594. NK_LIB void nk_remove_table(struct nk_window *win, struct nk_table *tbl);
  5595. NK_LIB void nk_free_table(struct nk_context *ctx, struct nk_table *tbl);
  5596. NK_LIB void nk_push_table(struct nk_window *win, struct nk_table *tbl);
  5597. NK_LIB nk_uint *nk_add_value(struct nk_context *ctx, struct nk_window *win, nk_hash name, nk_uint value);
  5598. NK_LIB nk_uint *nk_find_value(struct nk_window *win, nk_hash name);
  5599. /* panel */
  5600. NK_LIB void *nk_create_panel(struct nk_context *ctx);
  5601. NK_LIB void nk_free_panel(struct nk_context*, struct nk_panel *pan);
  5602. NK_LIB int nk_panel_has_header(nk_flags flags, const char *title);
  5603. NK_LIB struct nk_vec2 nk_panel_get_padding(const struct nk_style *style, enum nk_panel_type type);
  5604. NK_LIB float nk_panel_get_border(const struct nk_style *style, nk_flags flags, enum nk_panel_type type);
  5605. NK_LIB struct nk_color nk_panel_get_border_color(const struct nk_style *style, enum nk_panel_type type);
  5606. NK_LIB int nk_panel_is_sub(enum nk_panel_type type);
  5607. NK_LIB int nk_panel_is_nonblock(enum nk_panel_type type);
  5608. NK_LIB int nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type panel_type);
  5609. NK_LIB void nk_panel_end(struct nk_context *ctx);
  5610. /* layout */
  5611. NK_LIB float nk_layout_row_calculate_usable_space(const struct nk_style *style, enum nk_panel_type type, float total_space, int columns);
  5612. NK_LIB void nk_panel_layout(const struct nk_context *ctx, struct nk_window *win, float height, int cols);
  5613. NK_LIB void nk_row_layout(struct nk_context *ctx, enum nk_layout_format fmt, float height, int cols, int width);
  5614. NK_LIB void nk_panel_alloc_row(const struct nk_context *ctx, struct nk_window *win);
  5615. NK_LIB void nk_layout_widget_space(struct nk_rect *bounds, const struct nk_context *ctx, struct nk_window *win, int modify);
  5616. NK_LIB void nk_panel_alloc_space(struct nk_rect *bounds, const struct nk_context *ctx);
  5617. NK_LIB void nk_layout_peek(struct nk_rect *bounds, struct nk_context *ctx);
  5618. /* popup */
  5619. 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);
  5620. /* text */
  5621. struct nk_text {
  5622. struct nk_vec2 padding;
  5623. struct nk_color background;
  5624. struct nk_color text;
  5625. };
  5626. 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);
  5627. 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);
  5628. /* button */
  5629. NK_LIB int nk_button_behavior(nk_flags *state, struct nk_rect r, const struct nk_input *i, enum nk_button_behavior behavior);
  5630. 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);
  5631. 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);
  5632. 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);
  5633. 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);
  5634. 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);
  5635. 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);
  5636. 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);
  5637. 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);
  5638. 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);
  5639. 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);
  5640. 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);
  5641. 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);
  5642. /* toggle */
  5643. enum nk_toggle_type {
  5644. NK_TOGGLE_CHECK,
  5645. NK_TOGGLE_OPTION
  5646. };
  5647. NK_LIB int nk_toggle_behavior(const struct nk_input *in, struct nk_rect select, nk_flags *state, int active);
  5648. 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);
  5649. 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);
  5650. 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);
  5651. /* progress */
  5652. 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);
  5653. 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);
  5654. 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);
  5655. /* slider */
  5656. 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);
  5657. 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);
  5658. 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);
  5659. /* scrollbar */
  5660. 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);
  5661. 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);
  5662. 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);
  5663. 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);
  5664. /* selectable */
  5665. 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);
  5666. 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);
  5667. 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);
  5668. /* edit */
  5669. 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);
  5670. 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);
  5671. /* color-picker */
  5672. 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);
  5673. 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);
  5674. 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);
  5675. /* property */
  5676. enum nk_property_status {
  5677. NK_PROPERTY_DEFAULT,
  5678. NK_PROPERTY_EDIT,
  5679. NK_PROPERTY_DRAG
  5680. };
  5681. enum nk_property_filter {
  5682. NK_FILTER_INT,
  5683. NK_FILTER_FLOAT
  5684. };
  5685. enum nk_property_kind {
  5686. NK_PROPERTY_INT,
  5687. NK_PROPERTY_FLOAT,
  5688. NK_PROPERTY_DOUBLE
  5689. };
  5690. union nk_property {
  5691. int i;
  5692. float f;
  5693. double d;
  5694. };
  5695. struct nk_property_variant {
  5696. enum nk_property_kind kind;
  5697. union nk_property value;
  5698. union nk_property min_value;
  5699. union nk_property max_value;
  5700. union nk_property step;
  5701. };
  5702. NK_LIB struct nk_property_variant nk_property_variant_int(int value, int min_value, int max_value, int step);
  5703. NK_LIB struct nk_property_variant nk_property_variant_float(float value, float min_value, float max_value, float step);
  5704. NK_LIB struct nk_property_variant nk_property_variant_double(double value, double min_value, double max_value, double step);
  5705. 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);
  5706. 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);
  5707. 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);
  5708. 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);
  5709. 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);
  5710. #endif
  5711. /* ===============================================================
  5712. *
  5713. * MATH
  5714. *
  5715. * ===============================================================*/
  5716. /* Since nuklear is supposed to work on all systems providing floating point
  5717. math without any dependencies I also had to implement my own math functions
  5718. for sqrt, sin and cos. Since the actual highly accurate implementations for
  5719. the standard library functions are quite complex and I do not need high
  5720. precision for my use cases I use approximations.
  5721. Sqrt
  5722. ----
  5723. For square root nuklear uses the famous fast inverse square root:
  5724. https://en.wikipedia.org/wiki/Fast_inverse_square_root with
  5725. slightly tweaked magic constant. While on today's hardware it is
  5726. probably not faster it is still fast and accurate enough for
  5727. nuklear's use cases. IMPORTANT: this requires float format IEEE 754
  5728. Sine/Cosine
  5729. -----------
  5730. All constants inside both function are generated Remez's minimax
  5731. approximations for value range 0...2*PI. The reason why I decided to
  5732. approximate exactly that range is that nuklear only needs sine and
  5733. cosine to generate circles which only requires that exact range.
  5734. In addition I used Remez instead of Taylor for additional precision:
  5735. www.lolengine.net/blog/2011/12/21/better-function-approximations.
  5736. The tool I used to generate constants for both sine and cosine
  5737. (it can actually approximate a lot more functions) can be
  5738. found here: www.lolengine.net/wiki/oss/lolremez
  5739. */
  5740. NK_LIB float
  5741. nk_inv_sqrt(float n)
  5742. {
  5743. float x2;
  5744. const float threehalfs = 1.5f;
  5745. union {nk_uint i; float f;} conv = {0};
  5746. conv.f = n;
  5747. x2 = n * 0.5f;
  5748. conv.i = 0x5f375A84 - (conv.i >> 1);
  5749. conv.f = conv.f * (threehalfs - (x2 * conv.f * conv.f));
  5750. return conv.f;
  5751. }
  5752. NK_LIB float
  5753. nk_sqrt(float x)
  5754. {
  5755. return x * nk_inv_sqrt(x);
  5756. }
  5757. NK_LIB float
  5758. nk_sin(float x)
  5759. {
  5760. NK_STORAGE const float a0 = +1.91059300966915117e-31f;
  5761. NK_STORAGE const float a1 = +1.00086760103908896f;
  5762. NK_STORAGE const float a2 = -1.21276126894734565e-2f;
  5763. NK_STORAGE const float a3 = -1.38078780785773762e-1f;
  5764. NK_STORAGE const float a4 = -2.67353392911981221e-2f;
  5765. NK_STORAGE const float a5 = +2.08026600266304389e-2f;
  5766. NK_STORAGE const float a6 = -3.03996055049204407e-3f;
  5767. NK_STORAGE const float a7 = +1.38235642404333740e-4f;
  5768. return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*a7))))));
  5769. }
  5770. NK_LIB float
  5771. nk_cos(float x)
  5772. {
  5773. /* New implementation. Also generated using lolremez. */
  5774. /* Old version significantly deviated from expected results. */
  5775. NK_STORAGE const float a0 = 9.9995999154986614e-1f;
  5776. NK_STORAGE const float a1 = 1.2548995793001028e-3f;
  5777. NK_STORAGE const float a2 = -5.0648546280678015e-1f;
  5778. NK_STORAGE const float a3 = 1.2942246466519995e-2f;
  5779. NK_STORAGE const float a4 = 2.8668384702547972e-2f;
  5780. NK_STORAGE const float a5 = 7.3726485210586547e-3f;
  5781. NK_STORAGE const float a6 = -3.8510875386947414e-3f;
  5782. NK_STORAGE const float a7 = 4.7196604604366623e-4f;
  5783. NK_STORAGE const float a8 = -1.8776444013090451e-5f;
  5784. return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*(a7 + x*a8)))))));
  5785. }
  5786. NK_LIB nk_uint
  5787. nk_round_up_pow2(nk_uint v)
  5788. {
  5789. v--;
  5790. v |= v >> 1;
  5791. v |= v >> 2;
  5792. v |= v >> 4;
  5793. v |= v >> 8;
  5794. v |= v >> 16;
  5795. v++;
  5796. return v;
  5797. }
  5798. NK_LIB double
  5799. nk_pow(double x, int n)
  5800. {
  5801. /* check the sign of n */
  5802. double r = 1;
  5803. int plus = n >= 0;
  5804. n = (plus) ? n : -n;
  5805. while (n > 0) {
  5806. if ((n & 1) == 1)
  5807. r *= x;
  5808. n /= 2;
  5809. x *= x;
  5810. }
  5811. return plus ? r : 1.0 / r;
  5812. }
  5813. NK_LIB int
  5814. nk_ifloord(double x)
  5815. {
  5816. x = (double)((int)x - ((x < 0.0) ? 1 : 0));
  5817. return (int)x;
  5818. }
  5819. NK_LIB int
  5820. nk_ifloorf(float x)
  5821. {
  5822. x = (float)((int)x - ((x < 0.0f) ? 1 : 0));
  5823. return (int)x;
  5824. }
  5825. NK_LIB int
  5826. nk_iceilf(float x)
  5827. {
  5828. if (x >= 0) {
  5829. int i = (int)x;
  5830. return (x > i) ? i+1: i;
  5831. } else {
  5832. int t = (int)x;
  5833. float r = x - (float)t;
  5834. return (r > 0.0f) ? t+1: t;
  5835. }
  5836. }
  5837. NK_LIB int
  5838. nk_log10(double n)
  5839. {
  5840. int neg;
  5841. int ret;
  5842. int exp = 0;
  5843. neg = (n < 0) ? 1 : 0;
  5844. ret = (neg) ? (int)-n : (int)n;
  5845. while ((ret / 10) > 0) {
  5846. ret /= 10;
  5847. exp++;
  5848. }
  5849. if (neg) exp = -exp;
  5850. return exp;
  5851. }
  5852. NK_API struct nk_rect
  5853. nk_get_null_rect(void)
  5854. {
  5855. return nk_null_rect;
  5856. }
  5857. NK_API struct nk_rect
  5858. nk_rect(float x, float y, float w, float h)
  5859. {
  5860. struct nk_rect r;
  5861. r.x = x; r.y = y;
  5862. r.w = w; r.h = h;
  5863. return r;
  5864. }
  5865. NK_API struct nk_rect
  5866. nk_recti(int x, int y, int w, int h)
  5867. {
  5868. struct nk_rect r;
  5869. r.x = (float)x;
  5870. r.y = (float)y;
  5871. r.w = (float)w;
  5872. r.h = (float)h;
  5873. return r;
  5874. }
  5875. NK_API struct nk_rect
  5876. nk_recta(struct nk_vec2 pos, struct nk_vec2 size)
  5877. {
  5878. return nk_rect(pos.x, pos.y, size.x, size.y);
  5879. }
  5880. NK_API struct nk_rect
  5881. nk_rectv(const float *r)
  5882. {
  5883. return nk_rect(r[0], r[1], r[2], r[3]);
  5884. }
  5885. NK_API struct nk_rect
  5886. nk_rectiv(const int *r)
  5887. {
  5888. return nk_recti(r[0], r[1], r[2], r[3]);
  5889. }
  5890. NK_API struct nk_vec2
  5891. nk_rect_pos(struct nk_rect r)
  5892. {
  5893. struct nk_vec2 ret;
  5894. ret.x = r.x; ret.y = r.y;
  5895. return ret;
  5896. }
  5897. NK_API struct nk_vec2
  5898. nk_rect_size(struct nk_rect r)
  5899. {
  5900. struct nk_vec2 ret;
  5901. ret.x = r.w; ret.y = r.h;
  5902. return ret;
  5903. }
  5904. NK_LIB struct nk_rect
  5905. nk_shrink_rect(struct nk_rect r, float amount)
  5906. {
  5907. struct nk_rect res;
  5908. r.w = NK_MAX(r.w, 2 * amount);
  5909. r.h = NK_MAX(r.h, 2 * amount);
  5910. res.x = r.x + amount;
  5911. res.y = r.y + amount;
  5912. res.w = r.w - 2 * amount;
  5913. res.h = r.h - 2 * amount;
  5914. return res;
  5915. }
  5916. NK_LIB struct nk_rect
  5917. nk_pad_rect(struct nk_rect r, struct nk_vec2 pad)
  5918. {
  5919. r.w = NK_MAX(r.w, 2 * pad.x);
  5920. r.h = NK_MAX(r.h, 2 * pad.y);
  5921. r.x += pad.x; r.y += pad.y;
  5922. r.w -= 2 * pad.x;
  5923. r.h -= 2 * pad.y;
  5924. return r;
  5925. }
  5926. NK_API struct nk_vec2
  5927. nk_vec2(float x, float y)
  5928. {
  5929. struct nk_vec2 ret;
  5930. ret.x = x; ret.y = y;
  5931. return ret;
  5932. }
  5933. NK_API struct nk_vec2
  5934. nk_vec2i(int x, int y)
  5935. {
  5936. struct nk_vec2 ret;
  5937. ret.x = (float)x;
  5938. ret.y = (float)y;
  5939. return ret;
  5940. }
  5941. NK_API struct nk_vec2
  5942. nk_vec2v(const float *v)
  5943. {
  5944. return nk_vec2(v[0], v[1]);
  5945. }
  5946. NK_API struct nk_vec2
  5947. nk_vec2iv(const int *v)
  5948. {
  5949. return nk_vec2i(v[0], v[1]);
  5950. }
  5951. NK_LIB void
  5952. nk_unify(struct nk_rect *clip, const struct nk_rect *a, float x0, float y0,
  5953. float x1, float y1)
  5954. {
  5955. NK_ASSERT(a);
  5956. NK_ASSERT(clip);
  5957. clip->x = NK_MAX(a->x, x0);
  5958. clip->y = NK_MAX(a->y, y0);
  5959. clip->w = NK_MIN(a->x + a->w, x1) - clip->x;
  5960. clip->h = NK_MIN(a->y + a->h, y1) - clip->y;
  5961. clip->w = NK_MAX(0, clip->w);
  5962. clip->h = NK_MAX(0, clip->h);
  5963. }
  5964. NK_API void
  5965. nk_triangle_from_direction(struct nk_vec2 *result, struct nk_rect r,
  5966. float pad_x, float pad_y, enum nk_heading direction)
  5967. {
  5968. float w_half, h_half;
  5969. NK_ASSERT(result);
  5970. r.w = NK_MAX(2 * pad_x, r.w);
  5971. r.h = NK_MAX(2 * pad_y, r.h);
  5972. r.w = r.w - 2 * pad_x;
  5973. r.h = r.h - 2 * pad_y;
  5974. r.x = r.x + pad_x;
  5975. r.y = r.y + pad_y;
  5976. w_half = r.w / 2.0f;
  5977. h_half = r.h / 2.0f;
  5978. if (direction == NK_UP) {
  5979. result[0] = nk_vec2(r.x + w_half, r.y);
  5980. result[1] = nk_vec2(r.x + r.w, r.y + r.h);
  5981. result[2] = nk_vec2(r.x, r.y + r.h);
  5982. } else if (direction == NK_RIGHT) {
  5983. result[0] = nk_vec2(r.x, r.y);
  5984. result[1] = nk_vec2(r.x + r.w, r.y + h_half);
  5985. result[2] = nk_vec2(r.x, r.y + r.h);
  5986. } else if (direction == NK_DOWN) {
  5987. result[0] = nk_vec2(r.x, r.y);
  5988. result[1] = nk_vec2(r.x + r.w, r.y);
  5989. result[2] = nk_vec2(r.x + w_half, r.y + r.h);
  5990. } else {
  5991. result[0] = nk_vec2(r.x, r.y + h_half);
  5992. result[1] = nk_vec2(r.x + r.w, r.y);
  5993. result[2] = nk_vec2(r.x + r.w, r.y + r.h);
  5994. }
  5995. }
  5996. /* ===============================================================
  5997. *
  5998. * UTIL
  5999. *
  6000. * ===============================================================*/
  6001. NK_INTERN int nk_str_match_here(const char *regexp, const char *text);
  6002. NK_INTERN int nk_str_match_star(int c, const char *regexp, const char *text);
  6003. NK_LIB int nk_is_lower(int c) {return (c >= 'a' && c <= 'z') || (c >= 0xE0 && c <= 0xFF);}
  6004. NK_LIB int nk_is_upper(int c){return (c >= 'A' && c <= 'Z') || (c >= 0xC0 && c <= 0xDF);}
  6005. NK_LIB int nk_to_upper(int c) {return (c >= 'a' && c <= 'z') ? (c - ('a' - 'A')) : c;}
  6006. NK_LIB int nk_to_lower(int c) {return (c >= 'A' && c <= 'Z') ? (c - ('a' + 'A')) : c;}
  6007. NK_LIB void*
  6008. nk_memcopy(void *dst0, const void *src0, nk_size length)
  6009. {
  6010. nk_ptr t;
  6011. char *dst = (char*)dst0;
  6012. const char *src = (const char*)src0;
  6013. if (length == 0 || dst == src)
  6014. goto done;
  6015. #define nk_word int
  6016. #define nk_wsize sizeof(nk_word)
  6017. #define nk_wmask (nk_wsize-1)
  6018. #define NK_TLOOP(s) if (t) NK_TLOOP1(s)
  6019. #define NK_TLOOP1(s) do { s; } while (--t)
  6020. if (dst < src) {
  6021. t = (nk_ptr)src; /* only need low bits */
  6022. if ((t | (nk_ptr)dst) & nk_wmask) {
  6023. if ((t ^ (nk_ptr)dst) & nk_wmask || length < nk_wsize)
  6024. t = length;
  6025. else
  6026. t = nk_wsize - (t & nk_wmask);
  6027. length -= t;
  6028. NK_TLOOP1(*dst++ = *src++);
  6029. }
  6030. t = length / nk_wsize;
  6031. NK_TLOOP(*(nk_word*)(void*)dst = *(const nk_word*)(const void*)src;
  6032. src += nk_wsize; dst += nk_wsize);
  6033. t = length & nk_wmask;
  6034. NK_TLOOP(*dst++ = *src++);
  6035. } else {
  6036. src += length;
  6037. dst += length;
  6038. t = (nk_ptr)src;
  6039. if ((t | (nk_ptr)dst) & nk_wmask) {
  6040. if ((t ^ (nk_ptr)dst) & nk_wmask || length <= nk_wsize)
  6041. t = length;
  6042. else
  6043. t &= nk_wmask;
  6044. length -= t;
  6045. NK_TLOOP1(*--dst = *--src);
  6046. }
  6047. t = length / nk_wsize;
  6048. NK_TLOOP(src -= nk_wsize; dst -= nk_wsize;
  6049. *(nk_word*)(void*)dst = *(const nk_word*)(const void*)src);
  6050. t = length & nk_wmask;
  6051. NK_TLOOP(*--dst = *--src);
  6052. }
  6053. #undef nk_word
  6054. #undef nk_wsize
  6055. #undef nk_wmask
  6056. #undef NK_TLOOP
  6057. #undef NK_TLOOP1
  6058. done:
  6059. return (dst0);
  6060. }
  6061. NK_LIB void
  6062. nk_memset(void *ptr, int c0, nk_size size)
  6063. {
  6064. #define nk_word unsigned
  6065. #define nk_wsize sizeof(nk_word)
  6066. #define nk_wmask (nk_wsize - 1)
  6067. nk_byte *dst = (nk_byte*)ptr;
  6068. unsigned c = 0;
  6069. nk_size t = 0;
  6070. if ((c = (nk_byte)c0) != 0) {
  6071. c = (c << 8) | c; /* at least 16-bits */
  6072. if (sizeof(unsigned int) > 2)
  6073. c = (c << 16) | c; /* at least 32-bits*/
  6074. }
  6075. /* too small of a word count */
  6076. dst = (nk_byte*)ptr;
  6077. if (size < 3 * nk_wsize) {
  6078. while (size--) *dst++ = (nk_byte)c0;
  6079. return;
  6080. }
  6081. /* align destination */
  6082. if ((t = NK_PTR_TO_UINT(dst) & nk_wmask) != 0) {
  6083. t = nk_wsize -t;
  6084. size -= t;
  6085. do {
  6086. *dst++ = (nk_byte)c0;
  6087. } while (--t != 0);
  6088. }
  6089. /* fill word */
  6090. t = size / nk_wsize;
  6091. do {
  6092. *(nk_word*)((void*)dst) = c;
  6093. dst += nk_wsize;
  6094. } while (--t != 0);
  6095. /* fill trailing bytes */
  6096. t = (size & nk_wmask);
  6097. if (t != 0) {
  6098. do {
  6099. *dst++ = (nk_byte)c0;
  6100. } while (--t != 0);
  6101. }
  6102. #undef nk_word
  6103. #undef nk_wsize
  6104. #undef nk_wmask
  6105. }
  6106. NK_LIB void
  6107. nk_zero(void *ptr, nk_size size)
  6108. {
  6109. NK_ASSERT(ptr);
  6110. NK_MEMSET(ptr, 0, size);
  6111. }
  6112. NK_API int
  6113. nk_strlen(const char *str)
  6114. {
  6115. int siz = 0;
  6116. NK_ASSERT(str);
  6117. while (str && *str++ != '\0') siz++;
  6118. return siz;
  6119. }
  6120. NK_API int
  6121. nk_strtoi(const char *str, const char **endptr)
  6122. {
  6123. int neg = 1;
  6124. const char *p = str;
  6125. int value = 0;
  6126. NK_ASSERT(str);
  6127. if (!str) return 0;
  6128. /* skip whitespace */
  6129. while (*p == ' ') p++;
  6130. if (*p == '-') {
  6131. neg = -1;
  6132. p++;
  6133. }
  6134. while (*p && *p >= '0' && *p <= '9') {
  6135. value = value * 10 + (int) (*p - '0');
  6136. p++;
  6137. }
  6138. if (endptr)
  6139. *endptr = p;
  6140. return neg*value;
  6141. }
  6142. NK_API double
  6143. nk_strtod(const char *str, const char **endptr)
  6144. {
  6145. double m;
  6146. double neg = 1.0;
  6147. const char *p = str;
  6148. double value = 0;
  6149. double number = 0;
  6150. NK_ASSERT(str);
  6151. if (!str) return 0;
  6152. /* skip whitespace */
  6153. while (*p == ' ') p++;
  6154. if (*p == '-') {
  6155. neg = -1.0;
  6156. p++;
  6157. }
  6158. while (*p && *p != '.' && *p != 'e') {
  6159. value = value * 10.0 + (double) (*p - '0');
  6160. p++;
  6161. }
  6162. if (*p == '.') {
  6163. p++;
  6164. for(m = 0.1; *p && *p != 'e'; p++ ) {
  6165. value = value + (double) (*p - '0') * m;
  6166. m *= 0.1;
  6167. }
  6168. }
  6169. if (*p == 'e') {
  6170. int i, pow, div;
  6171. p++;
  6172. if (*p == '-') {
  6173. div = nk_true;
  6174. p++;
  6175. } else if (*p == '+') {
  6176. div = nk_false;
  6177. p++;
  6178. } else div = nk_false;
  6179. for (pow = 0; *p; p++)
  6180. pow = pow * 10 + (int) (*p - '0');
  6181. for (m = 1.0, i = 0; i < pow; i++)
  6182. m *= 10.0;
  6183. if (div)
  6184. value /= m;
  6185. else value *= m;
  6186. }
  6187. number = value * neg;
  6188. if (endptr)
  6189. *endptr = p;
  6190. return number;
  6191. }
  6192. NK_API float
  6193. nk_strtof(const char *str, const char **endptr)
  6194. {
  6195. float float_value;
  6196. double double_value;
  6197. double_value = NK_STRTOD(str, endptr);
  6198. float_value = (float)double_value;
  6199. return float_value;
  6200. }
  6201. NK_API int
  6202. nk_stricmp(const char *s1, const char *s2)
  6203. {
  6204. nk_int c1,c2,d;
  6205. do {
  6206. c1 = *s1++;
  6207. c2 = *s2++;
  6208. d = c1 - c2;
  6209. while (d) {
  6210. if (c1 <= 'Z' && c1 >= 'A') {
  6211. d += ('a' - 'A');
  6212. if (!d) break;
  6213. }
  6214. if (c2 <= 'Z' && c2 >= 'A') {
  6215. d -= ('a' - 'A');
  6216. if (!d) break;
  6217. }
  6218. return ((d >= 0) << 1) - 1;
  6219. }
  6220. } while (c1);
  6221. return 0;
  6222. }
  6223. NK_API int
  6224. nk_stricmpn(const char *s1, const char *s2, int n)
  6225. {
  6226. int c1,c2,d;
  6227. NK_ASSERT(n >= 0);
  6228. do {
  6229. c1 = *s1++;
  6230. c2 = *s2++;
  6231. if (!n--) return 0;
  6232. d = c1 - c2;
  6233. while (d) {
  6234. if (c1 <= 'Z' && c1 >= 'A') {
  6235. d += ('a' - 'A');
  6236. if (!d) break;
  6237. }
  6238. if (c2 <= 'Z' && c2 >= 'A') {
  6239. d -= ('a' - 'A');
  6240. if (!d) break;
  6241. }
  6242. return ((d >= 0) << 1) - 1;
  6243. }
  6244. } while (c1);
  6245. return 0;
  6246. }
  6247. NK_INTERN int
  6248. nk_str_match_here(const char *regexp, const char *text)
  6249. {
  6250. if (regexp[0] == '\0')
  6251. return 1;
  6252. if (regexp[1] == '*')
  6253. return nk_str_match_star(regexp[0], regexp+2, text);
  6254. if (regexp[0] == '$' && regexp[1] == '\0')
  6255. return *text == '\0';
  6256. if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text))
  6257. return nk_str_match_here(regexp+1, text+1);
  6258. return 0;
  6259. }
  6260. NK_INTERN int
  6261. nk_str_match_star(int c, const char *regexp, const char *text)
  6262. {
  6263. do {/* a '* matches zero or more instances */
  6264. if (nk_str_match_here(regexp, text))
  6265. return 1;
  6266. } while (*text != '\0' && (*text++ == c || c == '.'));
  6267. return 0;
  6268. }
  6269. NK_API int
  6270. nk_strfilter(const char *text, const char *regexp)
  6271. {
  6272. /*
  6273. c matches any literal character c
  6274. . matches any single character
  6275. ^ matches the beginning of the input string
  6276. $ matches the end of the input string
  6277. * matches zero or more occurrences of the previous character*/
  6278. if (regexp[0] == '^')
  6279. return nk_str_match_here(regexp+1, text);
  6280. do { /* must look even if string is empty */
  6281. if (nk_str_match_here(regexp, text))
  6282. return 1;
  6283. } while (*text++ != '\0');
  6284. return 0;
  6285. }
  6286. NK_API int
  6287. nk_strmatch_fuzzy_text(const char *str, int str_len,
  6288. const char *pattern, int *out_score)
  6289. {
  6290. /* Returns true if each character in pattern is found sequentially within str
  6291. * if found then out_score is also set. Score value has no intrinsic meaning.
  6292. * Range varies with pattern. Can only compare scores with same search pattern. */
  6293. /* bonus for adjacent matches */
  6294. #define NK_ADJACENCY_BONUS 5
  6295. /* bonus if match occurs after a separator */
  6296. #define NK_SEPARATOR_BONUS 10
  6297. /* bonus if match is uppercase and prev is lower */
  6298. #define NK_CAMEL_BONUS 10
  6299. /* penalty applied for every letter in str before the first match */
  6300. #define NK_LEADING_LETTER_PENALTY (-3)
  6301. /* maximum penalty for leading letters */
  6302. #define NK_MAX_LEADING_LETTER_PENALTY (-9)
  6303. /* penalty for every letter that doesn't matter */
  6304. #define NK_UNMATCHED_LETTER_PENALTY (-1)
  6305. /* loop variables */
  6306. int score = 0;
  6307. char const * pattern_iter = pattern;
  6308. int str_iter = 0;
  6309. int prev_matched = nk_false;
  6310. int prev_lower = nk_false;
  6311. /* true so if first letter match gets separator bonus*/
  6312. int prev_separator = nk_true;
  6313. /* use "best" matched letter if multiple string letters match the pattern */
  6314. char const * best_letter = 0;
  6315. int best_letter_score = 0;
  6316. /* loop over strings */
  6317. NK_ASSERT(str);
  6318. NK_ASSERT(pattern);
  6319. if (!str || !str_len || !pattern) return 0;
  6320. while (str_iter < str_len)
  6321. {
  6322. const char pattern_letter = *pattern_iter;
  6323. const char str_letter = str[str_iter];
  6324. int next_match = *pattern_iter != '\0' &&
  6325. nk_to_lower(pattern_letter) == nk_to_lower(str_letter);
  6326. int rematch = best_letter && nk_to_upper(*best_letter) == nk_to_upper(str_letter);
  6327. int advanced = next_match && best_letter;
  6328. int pattern_repeat = best_letter && *pattern_iter != '\0';
  6329. pattern_repeat = pattern_repeat &&
  6330. nk_to_lower(*best_letter) == nk_to_lower(pattern_letter);
  6331. if (advanced || pattern_repeat) {
  6332. score += best_letter_score;
  6333. best_letter = 0;
  6334. best_letter_score = 0;
  6335. }
  6336. if (next_match || rematch)
  6337. {
  6338. int new_score = 0;
  6339. /* Apply penalty for each letter before the first pattern match */
  6340. if (pattern_iter == pattern) {
  6341. int count = (int)(&str[str_iter] - str);
  6342. int penalty = NK_LEADING_LETTER_PENALTY * count;
  6343. if (penalty < NK_MAX_LEADING_LETTER_PENALTY)
  6344. penalty = NK_MAX_LEADING_LETTER_PENALTY;
  6345. score += penalty;
  6346. }
  6347. /* apply bonus for consecutive bonuses */
  6348. if (prev_matched)
  6349. new_score += NK_ADJACENCY_BONUS;
  6350. /* apply bonus for matches after a separator */
  6351. if (prev_separator)
  6352. new_score += NK_SEPARATOR_BONUS;
  6353. /* apply bonus across camel case boundaries */
  6354. if (prev_lower && nk_is_upper(str_letter))
  6355. new_score += NK_CAMEL_BONUS;
  6356. /* update pattern iter IFF the next pattern letter was matched */
  6357. if (next_match)
  6358. ++pattern_iter;
  6359. /* update best letter in str which may be for a "next" letter or a rematch */
  6360. if (new_score >= best_letter_score) {
  6361. /* apply penalty for now skipped letter */
  6362. if (best_letter != 0)
  6363. score += NK_UNMATCHED_LETTER_PENALTY;
  6364. best_letter = &str[str_iter];
  6365. best_letter_score = new_score;
  6366. }
  6367. prev_matched = nk_true;
  6368. } else {
  6369. score += NK_UNMATCHED_LETTER_PENALTY;
  6370. prev_matched = nk_false;
  6371. }
  6372. /* separators should be more easily defined */
  6373. prev_lower = nk_is_lower(str_letter) != 0;
  6374. prev_separator = str_letter == '_' || str_letter == ' ';
  6375. ++str_iter;
  6376. }
  6377. /* apply score for last match */
  6378. if (best_letter)
  6379. score += best_letter_score;
  6380. /* did not match full pattern */
  6381. if (*pattern_iter != '\0')
  6382. return nk_false;
  6383. if (out_score)
  6384. *out_score = score;
  6385. return nk_true;
  6386. }
  6387. NK_API int
  6388. nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score)
  6389. {
  6390. return nk_strmatch_fuzzy_text(str, nk_strlen(str), pattern, out_score);
  6391. }
  6392. NK_LIB int
  6393. nk_string_float_limit(char *string, int prec)
  6394. {
  6395. int dot = 0;
  6396. char *c = string;
  6397. while (*c) {
  6398. if (*c == '.') {
  6399. dot = 1;
  6400. c++;
  6401. continue;
  6402. }
  6403. if (dot == (prec+1)) {
  6404. *c = 0;
  6405. break;
  6406. }
  6407. if (dot > 0) dot++;
  6408. c++;
  6409. }
  6410. return (int)(c - string);
  6411. }
  6412. NK_INTERN void
  6413. nk_strrev_ascii(char *s)
  6414. {
  6415. int len = nk_strlen(s);
  6416. int end = len / 2;
  6417. int i = 0;
  6418. char t;
  6419. for (; i < end; ++i) {
  6420. t = s[i];
  6421. s[i] = s[len - 1 - i];
  6422. s[len -1 - i] = t;
  6423. }
  6424. }
  6425. NK_LIB char*
  6426. nk_itoa(char *s, long n)
  6427. {
  6428. long i = 0;
  6429. if (n == 0) {
  6430. s[i++] = '0';
  6431. s[i] = 0;
  6432. return s;
  6433. }
  6434. if (n < 0) {
  6435. s[i++] = '-';
  6436. n = -n;
  6437. }
  6438. while (n > 0) {
  6439. s[i++] = (char)('0' + (n % 10));
  6440. n /= 10;
  6441. }
  6442. s[i] = 0;
  6443. if (s[0] == '-')
  6444. ++s;
  6445. nk_strrev_ascii(s);
  6446. return s;
  6447. }
  6448. NK_LIB char*
  6449. nk_dtoa(char *s, double n)
  6450. {
  6451. int useExp = 0;
  6452. int digit = 0, m = 0, m1 = 0;
  6453. char *c = s;
  6454. int neg = 0;
  6455. NK_ASSERT(s);
  6456. if (!s) return 0;
  6457. if (n == 0.0) {
  6458. s[0] = '0'; s[1] = '\0';
  6459. return s;
  6460. }
  6461. neg = (n < 0);
  6462. if (neg) n = -n;
  6463. /* calculate magnitude */
  6464. m = nk_log10(n);
  6465. useExp = (m >= 14 || (neg && m >= 9) || m <= -9);
  6466. if (neg) *(c++) = '-';
  6467. /* set up for scientific notation */
  6468. if (useExp) {
  6469. if (m < 0)
  6470. m -= 1;
  6471. n = n / (double)nk_pow(10.0, m);
  6472. m1 = m;
  6473. m = 0;
  6474. }
  6475. if (m < 1.0) {
  6476. m = 0;
  6477. }
  6478. /* convert the number */
  6479. while (n > NK_FLOAT_PRECISION || m >= 0) {
  6480. double weight = nk_pow(10.0, m);
  6481. if (weight > 0) {
  6482. double t = (double)n / weight;
  6483. digit = nk_ifloord(t);
  6484. n -= ((double)digit * weight);
  6485. *(c++) = (char)('0' + (char)digit);
  6486. }
  6487. if (m == 0 && n > 0)
  6488. *(c++) = '.';
  6489. m--;
  6490. }
  6491. if (useExp) {
  6492. /* convert the exponent */
  6493. int i, j;
  6494. *(c++) = 'e';
  6495. if (m1 > 0) {
  6496. *(c++) = '+';
  6497. } else {
  6498. *(c++) = '-';
  6499. m1 = -m1;
  6500. }
  6501. m = 0;
  6502. while (m1 > 0) {
  6503. *(c++) = (char)('0' + (char)(m1 % 10));
  6504. m1 /= 10;
  6505. m++;
  6506. }
  6507. c -= m;
  6508. for (i = 0, j = m-1; i<j; i++, j--) {
  6509. /* swap without temporary */
  6510. c[i] ^= c[j];
  6511. c[j] ^= c[i];
  6512. c[i] ^= c[j];
  6513. }
  6514. c += m;
  6515. }
  6516. *(c) = '\0';
  6517. return s;
  6518. }
  6519. #ifdef NK_INCLUDE_STANDARD_VARARGS
  6520. #ifndef NK_INCLUDE_STANDARD_IO
  6521. NK_INTERN int
  6522. nk_vsnprintf(char *buf, int buf_size, const char *fmt, va_list args)
  6523. {
  6524. enum nk_arg_type {
  6525. NK_ARG_TYPE_CHAR,
  6526. NK_ARG_TYPE_SHORT,
  6527. NK_ARG_TYPE_DEFAULT,
  6528. NK_ARG_TYPE_LONG
  6529. };
  6530. enum nk_arg_flags {
  6531. NK_ARG_FLAG_LEFT = 0x01,
  6532. NK_ARG_FLAG_PLUS = 0x02,
  6533. NK_ARG_FLAG_SPACE = 0x04,
  6534. NK_ARG_FLAG_NUM = 0x10,
  6535. NK_ARG_FLAG_ZERO = 0x20
  6536. };
  6537. char number_buffer[NK_MAX_NUMBER_BUFFER];
  6538. enum nk_arg_type arg_type = NK_ARG_TYPE_DEFAULT;
  6539. int precision = NK_DEFAULT;
  6540. int width = NK_DEFAULT;
  6541. nk_flags flag = 0;
  6542. int len = 0;
  6543. int result = -1;
  6544. const char *iter = fmt;
  6545. NK_ASSERT(buf);
  6546. NK_ASSERT(buf_size);
  6547. if (!buf || !buf_size || !fmt) return 0;
  6548. for (iter = fmt; *iter && len < buf_size; iter++) {
  6549. /* copy all non-format characters */
  6550. while (*iter && (*iter != '%') && (len < buf_size))
  6551. buf[len++] = *iter++;
  6552. if (!(*iter) || len >= buf_size) break;
  6553. iter++;
  6554. /* flag arguments */
  6555. while (*iter) {
  6556. if (*iter == '-') flag |= NK_ARG_FLAG_LEFT;
  6557. else if (*iter == '+') flag |= NK_ARG_FLAG_PLUS;
  6558. else if (*iter == ' ') flag |= NK_ARG_FLAG_SPACE;
  6559. else if (*iter == '#') flag |= NK_ARG_FLAG_NUM;
  6560. else if (*iter == '0') flag |= NK_ARG_FLAG_ZERO;
  6561. else break;
  6562. iter++;
  6563. }
  6564. /* width argument */
  6565. width = NK_DEFAULT;
  6566. if (*iter >= '1' && *iter <= '9') {
  6567. const char *end;
  6568. width = nk_strtoi(iter, &end);
  6569. if (end == iter)
  6570. width = -1;
  6571. else iter = end;
  6572. } else if (*iter == '*') {
  6573. width = va_arg(args, int);
  6574. iter++;
  6575. }
  6576. /* precision argument */
  6577. precision = NK_DEFAULT;
  6578. if (*iter == '.') {
  6579. iter++;
  6580. if (*iter == '*') {
  6581. precision = va_arg(args, int);
  6582. iter++;
  6583. } else {
  6584. const char *end;
  6585. precision = nk_strtoi(iter, &end);
  6586. if (end == iter)
  6587. precision = -1;
  6588. else iter = end;
  6589. }
  6590. }
  6591. /* length modifier */
  6592. if (*iter == 'h') {
  6593. if (*(iter+1) == 'h') {
  6594. arg_type = NK_ARG_TYPE_CHAR;
  6595. iter++;
  6596. } else arg_type = NK_ARG_TYPE_SHORT;
  6597. iter++;
  6598. } else if (*iter == 'l') {
  6599. arg_type = NK_ARG_TYPE_LONG;
  6600. iter++;
  6601. } else arg_type = NK_ARG_TYPE_DEFAULT;
  6602. /* specifier */
  6603. if (*iter == '%') {
  6604. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6605. NK_ASSERT(precision == NK_DEFAULT);
  6606. NK_ASSERT(width == NK_DEFAULT);
  6607. if (len < buf_size)
  6608. buf[len++] = '%';
  6609. } else if (*iter == 's') {
  6610. /* string */
  6611. const char *str = va_arg(args, const char*);
  6612. NK_ASSERT(str != buf && "buffer and argument are not allowed to overlap!");
  6613. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6614. NK_ASSERT(precision == NK_DEFAULT);
  6615. NK_ASSERT(width == NK_DEFAULT);
  6616. if (str == buf) return -1;
  6617. while (str && *str && len < buf_size)
  6618. buf[len++] = *str++;
  6619. } else if (*iter == 'n') {
  6620. /* current length callback */
  6621. signed int *n = va_arg(args, int*);
  6622. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6623. NK_ASSERT(precision == NK_DEFAULT);
  6624. NK_ASSERT(width == NK_DEFAULT);
  6625. if (n) *n = len;
  6626. } else if (*iter == 'c' || *iter == 'i' || *iter == 'd') {
  6627. /* signed integer */
  6628. long value = 0;
  6629. const char *num_iter;
  6630. int num_len, num_print, padding;
  6631. int cur_precision = NK_MAX(precision, 1);
  6632. int cur_width = NK_MAX(width, 0);
  6633. /* retrieve correct value type */
  6634. if (arg_type == NK_ARG_TYPE_CHAR)
  6635. value = (signed char)va_arg(args, int);
  6636. else if (arg_type == NK_ARG_TYPE_SHORT)
  6637. value = (signed short)va_arg(args, int);
  6638. else if (arg_type == NK_ARG_TYPE_LONG)
  6639. value = va_arg(args, signed long);
  6640. else if (*iter == 'c')
  6641. value = (unsigned char)va_arg(args, int);
  6642. else value = va_arg(args, signed int);
  6643. /* convert number to string */
  6644. nk_itoa(number_buffer, value);
  6645. num_len = nk_strlen(number_buffer);
  6646. padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 0);
  6647. if ((flag & NK_ARG_FLAG_PLUS) || (flag & NK_ARG_FLAG_SPACE))
  6648. padding = NK_MAX(padding-1, 0);
  6649. /* fill left padding up to a total of `width` characters */
  6650. if (!(flag & NK_ARG_FLAG_LEFT)) {
  6651. while (padding-- > 0 && (len < buf_size)) {
  6652. if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
  6653. buf[len++] = '0';
  6654. else buf[len++] = ' ';
  6655. }
  6656. }
  6657. /* copy string value representation into buffer */
  6658. if ((flag & NK_ARG_FLAG_PLUS) && value >= 0 && len < buf_size)
  6659. buf[len++] = '+';
  6660. else if ((flag & NK_ARG_FLAG_SPACE) && value >= 0 && len < buf_size)
  6661. buf[len++] = ' ';
  6662. /* fill up to precision number of digits with '0' */
  6663. num_print = NK_MAX(cur_precision, num_len);
  6664. while (precision && (num_print > num_len) && (len < buf_size)) {
  6665. buf[len++] = '0';
  6666. num_print--;
  6667. }
  6668. /* copy string value representation into buffer */
  6669. num_iter = number_buffer;
  6670. while (precision && *num_iter && len < buf_size)
  6671. buf[len++] = *num_iter++;
  6672. /* fill right padding up to width characters */
  6673. if (flag & NK_ARG_FLAG_LEFT) {
  6674. while ((padding-- > 0) && (len < buf_size))
  6675. buf[len++] = ' ';
  6676. }
  6677. } else if (*iter == 'o' || *iter == 'x' || *iter == 'X' || *iter == 'u') {
  6678. /* unsigned integer */
  6679. unsigned long value = 0;
  6680. int num_len = 0, num_print, padding = 0;
  6681. int cur_precision = NK_MAX(precision, 1);
  6682. int cur_width = NK_MAX(width, 0);
  6683. unsigned int base = (*iter == 'o') ? 8: (*iter == 'u')? 10: 16;
  6684. /* print oct/hex/dec value */
  6685. const char *upper_output_format = "0123456789ABCDEF";
  6686. const char *lower_output_format = "0123456789abcdef";
  6687. const char *output_format = (*iter == 'x') ?
  6688. lower_output_format: upper_output_format;
  6689. /* retrieve correct value type */
  6690. if (arg_type == NK_ARG_TYPE_CHAR)
  6691. value = (unsigned char)va_arg(args, int);
  6692. else if (arg_type == NK_ARG_TYPE_SHORT)
  6693. value = (unsigned short)va_arg(args, int);
  6694. else if (arg_type == NK_ARG_TYPE_LONG)
  6695. value = va_arg(args, unsigned long);
  6696. else value = va_arg(args, unsigned int);
  6697. do {
  6698. /* convert decimal number into hex/oct number */
  6699. int digit = output_format[value % base];
  6700. if (num_len < NK_MAX_NUMBER_BUFFER)
  6701. number_buffer[num_len++] = (char)digit;
  6702. value /= base;
  6703. } while (value > 0);
  6704. num_print = NK_MAX(cur_precision, num_len);
  6705. padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 0);
  6706. if (flag & NK_ARG_FLAG_NUM)
  6707. padding = NK_MAX(padding-1, 0);
  6708. /* fill left padding up to a total of `width` characters */
  6709. if (!(flag & NK_ARG_FLAG_LEFT)) {
  6710. while ((padding-- > 0) && (len < buf_size)) {
  6711. if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
  6712. buf[len++] = '0';
  6713. else buf[len++] = ' ';
  6714. }
  6715. }
  6716. /* fill up to precision number of digits */
  6717. if (num_print && (flag & NK_ARG_FLAG_NUM)) {
  6718. if ((*iter == 'o') && (len < buf_size)) {
  6719. buf[len++] = '0';
  6720. } else if ((*iter == 'x') && ((len+1) < buf_size)) {
  6721. buf[len++] = '0';
  6722. buf[len++] = 'x';
  6723. } else if ((*iter == 'X') && ((len+1) < buf_size)) {
  6724. buf[len++] = '0';
  6725. buf[len++] = 'X';
  6726. }
  6727. }
  6728. while (precision && (num_print > num_len) && (len < buf_size)) {
  6729. buf[len++] = '0';
  6730. num_print--;
  6731. }
  6732. /* reverse number direction */
  6733. while (num_len > 0) {
  6734. if (precision && (len < buf_size))
  6735. buf[len++] = number_buffer[num_len-1];
  6736. num_len--;
  6737. }
  6738. /* fill right padding up to width characters */
  6739. if (flag & NK_ARG_FLAG_LEFT) {
  6740. while ((padding-- > 0) && (len < buf_size))
  6741. buf[len++] = ' ';
  6742. }
  6743. } else if (*iter == 'f') {
  6744. /* floating point */
  6745. const char *num_iter;
  6746. int cur_precision = (precision < 0) ? 6: precision;
  6747. int prefix, cur_width = NK_MAX(width, 0);
  6748. double value = va_arg(args, double);
  6749. int num_len = 0, frac_len = 0, dot = 0;
  6750. int padding = 0;
  6751. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6752. NK_DTOA(number_buffer, value);
  6753. num_len = nk_strlen(number_buffer);
  6754. /* calculate padding */
  6755. num_iter = number_buffer;
  6756. while (*num_iter && *num_iter != '.')
  6757. num_iter++;
  6758. prefix = (*num_iter == '.')?(int)(num_iter - number_buffer)+1:0;
  6759. padding = NK_MAX(cur_width - (prefix + NK_MIN(cur_precision, num_len - prefix)) , 0);
  6760. if ((flag & NK_ARG_FLAG_PLUS) || (flag & NK_ARG_FLAG_SPACE))
  6761. padding = NK_MAX(padding-1, 0);
  6762. /* fill left padding up to a total of `width` characters */
  6763. if (!(flag & NK_ARG_FLAG_LEFT)) {
  6764. while (padding-- > 0 && (len < buf_size)) {
  6765. if (flag & NK_ARG_FLAG_ZERO)
  6766. buf[len++] = '0';
  6767. else buf[len++] = ' ';
  6768. }
  6769. }
  6770. /* copy string value representation into buffer */
  6771. num_iter = number_buffer;
  6772. if ((flag & NK_ARG_FLAG_PLUS) && (value >= 0) && (len < buf_size))
  6773. buf[len++] = '+';
  6774. else if ((flag & NK_ARG_FLAG_SPACE) && (value >= 0) && (len < buf_size))
  6775. buf[len++] = ' ';
  6776. while (*num_iter) {
  6777. if (dot) frac_len++;
  6778. if (len < buf_size)
  6779. buf[len++] = *num_iter;
  6780. if (*num_iter == '.') dot = 1;
  6781. if (frac_len >= cur_precision) break;
  6782. num_iter++;
  6783. }
  6784. /* fill number up to precision */
  6785. while (frac_len < cur_precision) {
  6786. if (!dot && len < buf_size) {
  6787. buf[len++] = '.';
  6788. dot = 1;
  6789. }
  6790. if (len < buf_size)
  6791. buf[len++] = '0';
  6792. frac_len++;
  6793. }
  6794. /* fill right padding up to width characters */
  6795. if (flag & NK_ARG_FLAG_LEFT) {
  6796. while ((padding-- > 0) && (len < buf_size))
  6797. buf[len++] = ' ';
  6798. }
  6799. } else {
  6800. /* Specifier not supported: g,G,e,E,p,z */
  6801. NK_ASSERT(0 && "specifier is not supported!");
  6802. return result;
  6803. }
  6804. }
  6805. buf[(len >= buf_size)?(buf_size-1):len] = 0;
  6806. result = (len >= buf_size)?-1:len;
  6807. return result;
  6808. }
  6809. #endif
  6810. NK_LIB int
  6811. nk_strfmt(char *buf, int buf_size, const char *fmt, va_list args)
  6812. {
  6813. int result = -1;
  6814. NK_ASSERT(buf);
  6815. NK_ASSERT(buf_size);
  6816. if (!buf || !buf_size || !fmt) return 0;
  6817. #ifdef NK_INCLUDE_STANDARD_IO
  6818. result = NK_VSNPRINTF(buf, (nk_size)buf_size, fmt, args);
  6819. result = (result >= buf_size) ? -1: result;
  6820. buf[buf_size-1] = 0;
  6821. #else
  6822. result = nk_vsnprintf(buf, buf_size, fmt, args);
  6823. #endif
  6824. return result;
  6825. }
  6826. #endif
  6827. NK_API nk_hash
  6828. nk_murmur_hash(const void * key, int len, nk_hash seed)
  6829. {
  6830. /* 32-Bit MurmurHash3: https://code.google.com/p/smhasher/wiki/MurmurHash3*/
  6831. #define NK_ROTL(x,r) ((x) << (r) | ((x) >> (32 - r)))
  6832. nk_uint h1 = seed;
  6833. nk_uint k1;
  6834. const nk_byte *data = (const nk_byte*)key;
  6835. const nk_byte *keyptr = data;
  6836. nk_byte *k1ptr;
  6837. const int bsize = sizeof(k1);
  6838. const int nblocks = len/4;
  6839. const nk_uint c1 = 0xcc9e2d51;
  6840. const nk_uint c2 = 0x1b873593;
  6841. const nk_byte *tail;
  6842. int i;
  6843. /* body */
  6844. if (!key) return 0;
  6845. for (i = 0; i < nblocks; ++i, keyptr += bsize) {
  6846. k1ptr = (nk_byte*)&k1;
  6847. k1ptr[0] = keyptr[0];
  6848. k1ptr[1] = keyptr[1];
  6849. k1ptr[2] = keyptr[2];
  6850. k1ptr[3] = keyptr[3];
  6851. k1 *= c1;
  6852. k1 = NK_ROTL(k1,15);
  6853. k1 *= c2;
  6854. h1 ^= k1;
  6855. h1 = NK_ROTL(h1,13);
  6856. h1 = h1*5+0xe6546b64;
  6857. }
  6858. /* tail */
  6859. tail = (const nk_byte*)(data + nblocks*4);
  6860. k1 = 0;
  6861. switch (len & 3) {
  6862. case 3: k1 ^= (nk_uint)(tail[2] << 16); /* fallthrough */
  6863. case 2: k1 ^= (nk_uint)(tail[1] << 8u); /* fallthrough */
  6864. case 1: k1 ^= tail[0];
  6865. k1 *= c1;
  6866. k1 = NK_ROTL(k1,15);
  6867. k1 *= c2;
  6868. h1 ^= k1;
  6869. break;
  6870. default: break;
  6871. }
  6872. /* finalization */
  6873. h1 ^= (nk_uint)len;
  6874. /* fmix32 */
  6875. h1 ^= h1 >> 16;
  6876. h1 *= 0x85ebca6b;
  6877. h1 ^= h1 >> 13;
  6878. h1 *= 0xc2b2ae35;
  6879. h1 ^= h1 >> 16;
  6880. #undef NK_ROTL
  6881. return h1;
  6882. }
  6883. #ifdef NK_INCLUDE_STANDARD_IO
  6884. NK_LIB char*
  6885. nk_file_load(const char* path, nk_size* siz, struct nk_allocator *alloc)
  6886. {
  6887. char *buf;
  6888. FILE *fd;
  6889. long ret;
  6890. NK_ASSERT(path);
  6891. NK_ASSERT(siz);
  6892. NK_ASSERT(alloc);
  6893. if (!path || !siz || !alloc)
  6894. return 0;
  6895. fd = fopen(path, "rb");
  6896. if (!fd) return 0;
  6897. fseek(fd, 0, SEEK_END);
  6898. ret = ftell(fd);
  6899. if (ret < 0) {
  6900. fclose(fd);
  6901. return 0;
  6902. }
  6903. *siz = (nk_size)ret;
  6904. fseek(fd, 0, SEEK_SET);
  6905. buf = (char*)alloc->alloc(alloc->userdata,0, *siz);
  6906. NK_ASSERT(buf);
  6907. if (!buf) {
  6908. fclose(fd);
  6909. return 0;
  6910. }
  6911. *siz = (nk_size)fread(buf, 1,*siz, fd);
  6912. fclose(fd);
  6913. return buf;
  6914. }
  6915. #endif
  6916. NK_LIB int
  6917. nk_text_clamp(const struct nk_user_font *font, const char *text,
  6918. int text_len, float space, int *glyphs, float *text_width,
  6919. nk_rune *sep_list, int sep_count)
  6920. {
  6921. int i = 0;
  6922. int glyph_len = 0;
  6923. float last_width = 0;
  6924. nk_rune unicode = 0;
  6925. float width = 0;
  6926. int len = 0;
  6927. int g = 0;
  6928. float s;
  6929. int sep_len = 0;
  6930. int sep_g = 0;
  6931. float sep_width = 0;
  6932. sep_count = NK_MAX(sep_count,0);
  6933. glyph_len = nk_utf_decode(text, &unicode, text_len);
  6934. while (glyph_len && (width < space) && (len < text_len)) {
  6935. len += glyph_len;
  6936. s = font->width(font->userdata, font->height, text, len);
  6937. for (i = 0; i < sep_count; ++i) {
  6938. if (unicode != sep_list[i]) continue;
  6939. sep_width = last_width = width;
  6940. sep_g = g+1;
  6941. sep_len = len;
  6942. break;
  6943. }
  6944. if (i == sep_count){
  6945. last_width = sep_width = width;
  6946. sep_g = g+1;
  6947. }
  6948. width = s;
  6949. glyph_len = nk_utf_decode(&text[len], &unicode, text_len - len);
  6950. g++;
  6951. }
  6952. if (len >= text_len) {
  6953. *glyphs = g;
  6954. *text_width = last_width;
  6955. return len;
  6956. } else {
  6957. *glyphs = sep_g;
  6958. *text_width = sep_width;
  6959. return (!sep_len) ? len: sep_len;
  6960. }
  6961. }
  6962. NK_LIB struct nk_vec2
  6963. nk_text_calculate_text_bounds(const struct nk_user_font *font,
  6964. const char *begin, int byte_len, float row_height, const char **remaining,
  6965. struct nk_vec2 *out_offset, int *glyphs, int op)
  6966. {
  6967. float line_height = row_height;
  6968. struct nk_vec2 text_size = nk_vec2(0,0);
  6969. float line_width = 0.0f;
  6970. float glyph_width;
  6971. int glyph_len = 0;
  6972. nk_rune unicode = 0;
  6973. int text_len = 0;
  6974. if (!begin || byte_len <= 0 || !font)
  6975. return nk_vec2(0,row_height);
  6976. glyph_len = nk_utf_decode(begin, &unicode, byte_len);
  6977. if (!glyph_len) return text_size;
  6978. glyph_width = font->width(font->userdata, font->height, begin, glyph_len);
  6979. *glyphs = 0;
  6980. while ((text_len < byte_len) && glyph_len) {
  6981. if (unicode == '\n') {
  6982. text_size.x = NK_MAX(text_size.x, line_width);
  6983. text_size.y += line_height;
  6984. line_width = 0;
  6985. *glyphs+=1;
  6986. if (op == NK_STOP_ON_NEW_LINE)
  6987. break;
  6988. text_len++;
  6989. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  6990. continue;
  6991. }
  6992. if (unicode == '\r') {
  6993. text_len++;
  6994. *glyphs+=1;
  6995. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  6996. continue;
  6997. }
  6998. *glyphs = *glyphs + 1;
  6999. text_len += glyph_len;
  7000. line_width += (float)glyph_width;
  7001. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  7002. glyph_width = font->width(font->userdata, font->height, begin+text_len, glyph_len);
  7003. continue;
  7004. }
  7005. if (text_size.x < line_width)
  7006. text_size.x = line_width;
  7007. if (out_offset)
  7008. *out_offset = nk_vec2(line_width, text_size.y + line_height);
  7009. if (line_width > 0 || text_size.y == 0.0f)
  7010. text_size.y += line_height;
  7011. if (remaining)
  7012. *remaining = begin+text_len;
  7013. return text_size;
  7014. }
  7015. /* ==============================================================
  7016. *
  7017. * COLOR
  7018. *
  7019. * ===============================================================*/
  7020. NK_INTERN int
  7021. nk_parse_hex(const char *p, int length)
  7022. {
  7023. int i = 0;
  7024. int len = 0;
  7025. while (len < length) {
  7026. i <<= 4;
  7027. if (p[len] >= 'a' && p[len] <= 'f')
  7028. i += ((p[len] - 'a') + 10);
  7029. else if (p[len] >= 'A' && p[len] <= 'F')
  7030. i += ((p[len] - 'A') + 10);
  7031. else i += (p[len] - '0');
  7032. len++;
  7033. }
  7034. return i;
  7035. }
  7036. NK_API struct nk_color
  7037. nk_rgba(int r, int g, int b, int a)
  7038. {
  7039. struct nk_color ret;
  7040. ret.r = (nk_byte)NK_CLAMP(0, r, 255);
  7041. ret.g = (nk_byte)NK_CLAMP(0, g, 255);
  7042. ret.b = (nk_byte)NK_CLAMP(0, b, 255);
  7043. ret.a = (nk_byte)NK_CLAMP(0, a, 255);
  7044. return ret;
  7045. }
  7046. NK_API struct nk_color
  7047. nk_rgb_hex(const char *rgb)
  7048. {
  7049. struct nk_color col;
  7050. const char *c = rgb;
  7051. if (*c == '#') c++;
  7052. col.r = (nk_byte)nk_parse_hex(c, 2);
  7053. col.g = (nk_byte)nk_parse_hex(c+2, 2);
  7054. col.b = (nk_byte)nk_parse_hex(c+4, 2);
  7055. col.a = 255;
  7056. return col;
  7057. }
  7058. NK_API struct nk_color
  7059. nk_rgba_hex(const char *rgb)
  7060. {
  7061. struct nk_color col;
  7062. const char *c = rgb;
  7063. if (*c == '#') c++;
  7064. col.r = (nk_byte)nk_parse_hex(c, 2);
  7065. col.g = (nk_byte)nk_parse_hex(c+2, 2);
  7066. col.b = (nk_byte)nk_parse_hex(c+4, 2);
  7067. col.a = (nk_byte)nk_parse_hex(c+6, 2);
  7068. return col;
  7069. }
  7070. NK_API void
  7071. nk_color_hex_rgba(char *output, struct nk_color col)
  7072. {
  7073. #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
  7074. output[0] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
  7075. output[1] = (char)NK_TO_HEX((col.r & 0x0F));
  7076. output[2] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
  7077. output[3] = (char)NK_TO_HEX((col.g & 0x0F));
  7078. output[4] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
  7079. output[5] = (char)NK_TO_HEX((col.b & 0x0F));
  7080. output[6] = (char)NK_TO_HEX((col.a & 0xF0) >> 4);
  7081. output[7] = (char)NK_TO_HEX((col.a & 0x0F));
  7082. output[8] = '\0';
  7083. #undef NK_TO_HEX
  7084. }
  7085. NK_API void
  7086. nk_color_hex_rgb(char *output, struct nk_color col)
  7087. {
  7088. #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
  7089. output[0] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
  7090. output[1] = (char)NK_TO_HEX((col.r & 0x0F));
  7091. output[2] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
  7092. output[3] = (char)NK_TO_HEX((col.g & 0x0F));
  7093. output[4] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
  7094. output[5] = (char)NK_TO_HEX((col.b & 0x0F));
  7095. output[6] = '\0';
  7096. #undef NK_TO_HEX
  7097. }
  7098. NK_API struct nk_color
  7099. nk_rgba_iv(const int *c)
  7100. {
  7101. return nk_rgba(c[0], c[1], c[2], c[3]);
  7102. }
  7103. NK_API struct nk_color
  7104. nk_rgba_bv(const nk_byte *c)
  7105. {
  7106. return nk_rgba(c[0], c[1], c[2], c[3]);
  7107. }
  7108. NK_API struct nk_color
  7109. nk_rgb(int r, int g, int b)
  7110. {
  7111. struct nk_color ret;
  7112. ret.r = (nk_byte)NK_CLAMP(0, r, 255);
  7113. ret.g = (nk_byte)NK_CLAMP(0, g, 255);
  7114. ret.b = (nk_byte)NK_CLAMP(0, b, 255);
  7115. ret.a = (nk_byte)255;
  7116. return ret;
  7117. }
  7118. NK_API struct nk_color
  7119. nk_rgb_iv(const int *c)
  7120. {
  7121. return nk_rgb(c[0], c[1], c[2]);
  7122. }
  7123. NK_API struct nk_color
  7124. nk_rgb_bv(const nk_byte* c)
  7125. {
  7126. return nk_rgb(c[0], c[1], c[2]);
  7127. }
  7128. NK_API struct nk_color
  7129. nk_rgba_u32(nk_uint in)
  7130. {
  7131. struct nk_color ret;
  7132. ret.r = (in & 0xFF);
  7133. ret.g = ((in >> 8) & 0xFF);
  7134. ret.b = ((in >> 16) & 0xFF);
  7135. ret.a = (nk_byte)((in >> 24) & 0xFF);
  7136. return ret;
  7137. }
  7138. NK_API struct nk_color
  7139. nk_rgba_f(float r, float g, float b, float a)
  7140. {
  7141. struct nk_color ret;
  7142. ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
  7143. ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
  7144. ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
  7145. ret.a = (nk_byte)(NK_SATURATE(a) * 255.0f);
  7146. return ret;
  7147. }
  7148. NK_API struct nk_color
  7149. nk_rgba_fv(const float *c)
  7150. {
  7151. return nk_rgba_f(c[0], c[1], c[2], c[3]);
  7152. }
  7153. NK_API struct nk_color
  7154. nk_rgba_cf(struct nk_colorf c)
  7155. {
  7156. return nk_rgba_f(c.r, c.g, c.b, c.a);
  7157. }
  7158. NK_API struct nk_color
  7159. nk_rgb_f(float r, float g, float b)
  7160. {
  7161. struct nk_color ret;
  7162. ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
  7163. ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
  7164. ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
  7165. ret.a = 255;
  7166. return ret;
  7167. }
  7168. NK_API struct nk_color
  7169. nk_rgb_fv(const float *c)
  7170. {
  7171. return nk_rgb_f(c[0], c[1], c[2]);
  7172. }
  7173. NK_API struct nk_color
  7174. nk_rgb_cf(struct nk_colorf c)
  7175. {
  7176. return nk_rgb_f(c.r, c.g, c.b);
  7177. }
  7178. NK_API struct nk_color
  7179. nk_hsv(int h, int s, int v)
  7180. {
  7181. return nk_hsva(h, s, v, 255);
  7182. }
  7183. NK_API struct nk_color
  7184. nk_hsv_iv(const int *c)
  7185. {
  7186. return nk_hsv(c[0], c[1], c[2]);
  7187. }
  7188. NK_API struct nk_color
  7189. nk_hsv_bv(const nk_byte *c)
  7190. {
  7191. return nk_hsv(c[0], c[1], c[2]);
  7192. }
  7193. NK_API struct nk_color
  7194. nk_hsv_f(float h, float s, float v)
  7195. {
  7196. return nk_hsva_f(h, s, v, 1.0f);
  7197. }
  7198. NK_API struct nk_color
  7199. nk_hsv_fv(const float *c)
  7200. {
  7201. return nk_hsv_f(c[0], c[1], c[2]);
  7202. }
  7203. NK_API struct nk_color
  7204. nk_hsva(int h, int s, int v, int a)
  7205. {
  7206. float hf = ((float)NK_CLAMP(0, h, 255)) / 255.0f;
  7207. float sf = ((float)NK_CLAMP(0, s, 255)) / 255.0f;
  7208. float vf = ((float)NK_CLAMP(0, v, 255)) / 255.0f;
  7209. float af = ((float)NK_CLAMP(0, a, 255)) / 255.0f;
  7210. return nk_hsva_f(hf, sf, vf, af);
  7211. }
  7212. NK_API struct nk_color
  7213. nk_hsva_iv(const int *c)
  7214. {
  7215. return nk_hsva(c[0], c[1], c[2], c[3]);
  7216. }
  7217. NK_API struct nk_color
  7218. nk_hsva_bv(const nk_byte *c)
  7219. {
  7220. return nk_hsva(c[0], c[1], c[2], c[3]);
  7221. }
  7222. NK_API struct nk_colorf
  7223. nk_hsva_colorf(float h, float s, float v, float a)
  7224. {
  7225. int i;
  7226. float p, q, t, f;
  7227. struct nk_colorf out = {0,0,0,0};
  7228. if (s <= 0.0f) {
  7229. out.r = v; out.g = v; out.b = v; out.a = a;
  7230. return out;
  7231. }
  7232. h = h / (60.0f/360.0f);
  7233. i = (int)h;
  7234. f = h - (float)i;
  7235. p = v * (1.0f - s);
  7236. q = v * (1.0f - (s * f));
  7237. t = v * (1.0f - s * (1.0f - f));
  7238. switch (i) {
  7239. case 0: default: out.r = v; out.g = t; out.b = p; break;
  7240. case 1: out.r = q; out.g = v; out.b = p; break;
  7241. case 2: out.r = p; out.g = v; out.b = t; break;
  7242. case 3: out.r = p; out.g = q; out.b = v; break;
  7243. case 4: out.r = t; out.g = p; out.b = v; break;
  7244. case 5: out.r = v; out.g = p; out.b = q; break;}
  7245. out.a = a;
  7246. return out;
  7247. }
  7248. NK_API struct nk_colorf
  7249. nk_hsva_colorfv(float *c)
  7250. {
  7251. return nk_hsva_colorf(c[0], c[1], c[2], c[3]);
  7252. }
  7253. NK_API struct nk_color
  7254. nk_hsva_f(float h, float s, float v, float a)
  7255. {
  7256. struct nk_colorf c = nk_hsva_colorf(h, s, v, a);
  7257. return nk_rgba_f(c.r, c.g, c.b, c.a);
  7258. }
  7259. NK_API struct nk_color
  7260. nk_hsva_fv(const float *c)
  7261. {
  7262. return nk_hsva_f(c[0], c[1], c[2], c[3]);
  7263. }
  7264. NK_API nk_uint
  7265. nk_color_u32(struct nk_color in)
  7266. {
  7267. nk_uint out = (nk_uint)in.r;
  7268. out |= ((nk_uint)in.g << 8);
  7269. out |= ((nk_uint)in.b << 16);
  7270. out |= ((nk_uint)in.a << 24);
  7271. return out;
  7272. }
  7273. NK_API void
  7274. nk_color_f(float *r, float *g, float *b, float *a, struct nk_color in)
  7275. {
  7276. NK_STORAGE const float s = 1.0f/255.0f;
  7277. *r = (float)in.r * s;
  7278. *g = (float)in.g * s;
  7279. *b = (float)in.b * s;
  7280. *a = (float)in.a * s;
  7281. }
  7282. NK_API void
  7283. nk_color_fv(float *c, struct nk_color in)
  7284. {
  7285. nk_color_f(&c[0], &c[1], &c[2], &c[3], in);
  7286. }
  7287. NK_API struct nk_colorf
  7288. nk_color_cf(struct nk_color in)
  7289. {
  7290. struct nk_colorf o;
  7291. nk_color_f(&o.r, &o.g, &o.b, &o.a, in);
  7292. return o;
  7293. }
  7294. NK_API void
  7295. nk_color_d(double *r, double *g, double *b, double *a, struct nk_color in)
  7296. {
  7297. NK_STORAGE const double s = 1.0/255.0;
  7298. *r = (double)in.r * s;
  7299. *g = (double)in.g * s;
  7300. *b = (double)in.b * s;
  7301. *a = (double)in.a * s;
  7302. }
  7303. NK_API void
  7304. nk_color_dv(double *c, struct nk_color in)
  7305. {
  7306. nk_color_d(&c[0], &c[1], &c[2], &c[3], in);
  7307. }
  7308. NK_API void
  7309. nk_color_hsv_f(float *out_h, float *out_s, float *out_v, struct nk_color in)
  7310. {
  7311. float a;
  7312. nk_color_hsva_f(out_h, out_s, out_v, &a, in);
  7313. }
  7314. NK_API void
  7315. nk_color_hsv_fv(float *out, struct nk_color in)
  7316. {
  7317. float a;
  7318. nk_color_hsva_f(&out[0], &out[1], &out[2], &a, in);
  7319. }
  7320. NK_API void
  7321. nk_colorf_hsva_f(float *out_h, float *out_s,
  7322. float *out_v, float *out_a, struct nk_colorf in)
  7323. {
  7324. float chroma;
  7325. float K = 0.0f;
  7326. if (in.g < in.b) {
  7327. const float t = in.g; in.g = in.b; in.b = t;
  7328. K = -1.f;
  7329. }
  7330. if (in.r < in.g) {
  7331. const float t = in.r; in.r = in.g; in.g = t;
  7332. K = -2.f/6.0f - K;
  7333. }
  7334. chroma = in.r - ((in.g < in.b) ? in.g: in.b);
  7335. *out_h = NK_ABS(K + (in.g - in.b)/(6.0f * chroma + 1e-20f));
  7336. *out_s = chroma / (in.r + 1e-20f);
  7337. *out_v = in.r;
  7338. *out_a = in.a;
  7339. }
  7340. NK_API void
  7341. nk_colorf_hsva_fv(float *hsva, struct nk_colorf in)
  7342. {
  7343. nk_colorf_hsva_f(&hsva[0], &hsva[1], &hsva[2], &hsva[3], in);
  7344. }
  7345. NK_API void
  7346. nk_color_hsva_f(float *out_h, float *out_s,
  7347. float *out_v, float *out_a, struct nk_color in)
  7348. {
  7349. struct nk_colorf col;
  7350. nk_color_f(&col.r,&col.g,&col.b,&col.a, in);
  7351. nk_colorf_hsva_f(out_h, out_s, out_v, out_a, col);
  7352. }
  7353. NK_API void
  7354. nk_color_hsva_fv(float *out, struct nk_color in)
  7355. {
  7356. nk_color_hsva_f(&out[0], &out[1], &out[2], &out[3], in);
  7357. }
  7358. NK_API void
  7359. nk_color_hsva_i(int *out_h, int *out_s, int *out_v,
  7360. int *out_a, struct nk_color in)
  7361. {
  7362. float h,s,v,a;
  7363. nk_color_hsva_f(&h, &s, &v, &a, in);
  7364. *out_h = (nk_byte)(h * 255.0f);
  7365. *out_s = (nk_byte)(s * 255.0f);
  7366. *out_v = (nk_byte)(v * 255.0f);
  7367. *out_a = (nk_byte)(a * 255.0f);
  7368. }
  7369. NK_API void
  7370. nk_color_hsva_iv(int *out, struct nk_color in)
  7371. {
  7372. nk_color_hsva_i(&out[0], &out[1], &out[2], &out[3], in);
  7373. }
  7374. NK_API void
  7375. nk_color_hsva_bv(nk_byte *out, struct nk_color in)
  7376. {
  7377. int tmp[4];
  7378. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  7379. out[0] = (nk_byte)tmp[0];
  7380. out[1] = (nk_byte)tmp[1];
  7381. out[2] = (nk_byte)tmp[2];
  7382. out[3] = (nk_byte)tmp[3];
  7383. }
  7384. NK_API void
  7385. nk_color_hsva_b(nk_byte *h, nk_byte *s, nk_byte *v, nk_byte *a, struct nk_color in)
  7386. {
  7387. int tmp[4];
  7388. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  7389. *h = (nk_byte)tmp[0];
  7390. *s = (nk_byte)tmp[1];
  7391. *v = (nk_byte)tmp[2];
  7392. *a = (nk_byte)tmp[3];
  7393. }
  7394. NK_API void
  7395. nk_color_hsv_i(int *out_h, int *out_s, int *out_v, struct nk_color in)
  7396. {
  7397. int a;
  7398. nk_color_hsva_i(out_h, out_s, out_v, &a, in);
  7399. }
  7400. NK_API void
  7401. nk_color_hsv_b(nk_byte *out_h, nk_byte *out_s, nk_byte *out_v, struct nk_color in)
  7402. {
  7403. int tmp[4];
  7404. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  7405. *out_h = (nk_byte)tmp[0];
  7406. *out_s = (nk_byte)tmp[1];
  7407. *out_v = (nk_byte)tmp[2];
  7408. }
  7409. NK_API void
  7410. nk_color_hsv_iv(int *out, struct nk_color in)
  7411. {
  7412. nk_color_hsv_i(&out[0], &out[1], &out[2], in);
  7413. }
  7414. NK_API void
  7415. nk_color_hsv_bv(nk_byte *out, struct nk_color in)
  7416. {
  7417. int tmp[4];
  7418. nk_color_hsv_i(&tmp[0], &tmp[1], &tmp[2], in);
  7419. out[0] = (nk_byte)tmp[0];
  7420. out[1] = (nk_byte)tmp[1];
  7421. out[2] = (nk_byte)tmp[2];
  7422. }
  7423. /* ===============================================================
  7424. *
  7425. * UTF-8
  7426. *
  7427. * ===============================================================*/
  7428. NK_GLOBAL const nk_byte nk_utfbyte[NK_UTF_SIZE+1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
  7429. NK_GLOBAL const nk_byte nk_utfmask[NK_UTF_SIZE+1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
  7430. NK_GLOBAL const nk_uint nk_utfmin[NK_UTF_SIZE+1] = {0, 0, 0x80, 0x800, 0x10000};
  7431. NK_GLOBAL const nk_uint nk_utfmax[NK_UTF_SIZE+1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
  7432. NK_INTERN int
  7433. nk_utf_validate(nk_rune *u, int i)
  7434. {
  7435. NK_ASSERT(u);
  7436. if (!u) return 0;
  7437. if (!NK_BETWEEN(*u, nk_utfmin[i], nk_utfmax[i]) ||
  7438. NK_BETWEEN(*u, 0xD800, 0xDFFF))
  7439. *u = NK_UTF_INVALID;
  7440. for (i = 1; *u > nk_utfmax[i]; ++i);
  7441. return i;
  7442. }
  7443. NK_INTERN nk_rune
  7444. nk_utf_decode_byte(char c, int *i)
  7445. {
  7446. NK_ASSERT(i);
  7447. if (!i) return 0;
  7448. for(*i = 0; *i < (int)NK_LEN(nk_utfmask); ++(*i)) {
  7449. if (((nk_byte)c & nk_utfmask[*i]) == nk_utfbyte[*i])
  7450. return (nk_byte)(c & ~nk_utfmask[*i]);
  7451. }
  7452. return 0;
  7453. }
  7454. NK_API int
  7455. nk_utf_decode(const char *c, nk_rune *u, int clen)
  7456. {
  7457. int i, j, len, type=0;
  7458. nk_rune udecoded;
  7459. NK_ASSERT(c);
  7460. NK_ASSERT(u);
  7461. if (!c || !u) return 0;
  7462. if (!clen) return 0;
  7463. *u = NK_UTF_INVALID;
  7464. udecoded = nk_utf_decode_byte(c[0], &len);
  7465. if (!NK_BETWEEN(len, 1, NK_UTF_SIZE))
  7466. return 1;
  7467. for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
  7468. udecoded = (udecoded << 6) | nk_utf_decode_byte(c[i], &type);
  7469. if (type != 0)
  7470. return j;
  7471. }
  7472. if (j < len)
  7473. return 0;
  7474. *u = udecoded;
  7475. nk_utf_validate(u, len);
  7476. return len;
  7477. }
  7478. NK_INTERN char
  7479. nk_utf_encode_byte(nk_rune u, int i)
  7480. {
  7481. return (char)((nk_utfbyte[i]) | ((nk_byte)u & ~nk_utfmask[i]));
  7482. }
  7483. NK_API int
  7484. nk_utf_encode(nk_rune u, char *c, int clen)
  7485. {
  7486. int len, i;
  7487. len = nk_utf_validate(&u, 0);
  7488. if (clen < len || !len || len > NK_UTF_SIZE)
  7489. return 0;
  7490. for (i = len - 1; i != 0; --i) {
  7491. c[i] = nk_utf_encode_byte(u, 0);
  7492. u >>= 6;
  7493. }
  7494. c[0] = nk_utf_encode_byte(u, len);
  7495. return len;
  7496. }
  7497. NK_API int
  7498. nk_utf_len(const char *str, int len)
  7499. {
  7500. const char *text;
  7501. int glyphs = 0;
  7502. int text_len;
  7503. int glyph_len;
  7504. int src_len = 0;
  7505. nk_rune unicode;
  7506. NK_ASSERT(str);
  7507. if (!str || !len) return 0;
  7508. text = str;
  7509. text_len = len;
  7510. glyph_len = nk_utf_decode(text, &unicode, text_len);
  7511. while (glyph_len && src_len < len) {
  7512. glyphs++;
  7513. src_len = src_len + glyph_len;
  7514. glyph_len = nk_utf_decode(text + src_len, &unicode, text_len - src_len);
  7515. }
  7516. return glyphs;
  7517. }
  7518. NK_API const char*
  7519. nk_utf_at(const char *buffer, int length, int index,
  7520. nk_rune *unicode, int *len)
  7521. {
  7522. int i = 0;
  7523. int src_len = 0;
  7524. int glyph_len = 0;
  7525. const char *text;
  7526. int text_len;
  7527. NK_ASSERT(buffer);
  7528. NK_ASSERT(unicode);
  7529. NK_ASSERT(len);
  7530. if (!buffer || !unicode || !len) return 0;
  7531. if (index < 0) {
  7532. *unicode = NK_UTF_INVALID;
  7533. *len = 0;
  7534. return 0;
  7535. }
  7536. text = buffer;
  7537. text_len = length;
  7538. glyph_len = nk_utf_decode(text, unicode, text_len);
  7539. while (glyph_len) {
  7540. if (i == index) {
  7541. *len = glyph_len;
  7542. break;
  7543. }
  7544. i++;
  7545. src_len = src_len + glyph_len;
  7546. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  7547. }
  7548. if (i != index) return 0;
  7549. return buffer + src_len;
  7550. }
  7551. /* ==============================================================
  7552. *
  7553. * BUFFER
  7554. *
  7555. * ===============================================================*/
  7556. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  7557. NK_LIB void*
  7558. nk_malloc(nk_handle unused, void *old,nk_size size)
  7559. {
  7560. NK_UNUSED(unused);
  7561. NK_UNUSED(old);
  7562. return malloc(size);
  7563. }
  7564. NK_LIB void
  7565. nk_mfree(nk_handle unused, void *ptr)
  7566. {
  7567. NK_UNUSED(unused);
  7568. free(ptr);
  7569. }
  7570. NK_API void
  7571. nk_buffer_init_default(struct nk_buffer *buffer)
  7572. {
  7573. struct nk_allocator alloc;
  7574. alloc.userdata.ptr = 0;
  7575. alloc.alloc = nk_malloc;
  7576. alloc.free = nk_mfree;
  7577. nk_buffer_init(buffer, &alloc, NK_BUFFER_DEFAULT_INITIAL_SIZE);
  7578. }
  7579. #endif
  7580. NK_API void
  7581. nk_buffer_init(struct nk_buffer *b, const struct nk_allocator *a,
  7582. nk_size initial_size)
  7583. {
  7584. NK_ASSERT(b);
  7585. NK_ASSERT(a);
  7586. NK_ASSERT(initial_size);
  7587. if (!b || !a || !initial_size) return;
  7588. nk_zero(b, sizeof(*b));
  7589. b->type = NK_BUFFER_DYNAMIC;
  7590. b->memory.ptr = a->alloc(a->userdata,0, initial_size);
  7591. b->memory.size = initial_size;
  7592. b->size = initial_size;
  7593. b->grow_factor = 2.0f;
  7594. b->pool = *a;
  7595. }
  7596. NK_API void
  7597. nk_buffer_init_fixed(struct nk_buffer *b, void *m, nk_size size)
  7598. {
  7599. NK_ASSERT(b);
  7600. NK_ASSERT(m);
  7601. NK_ASSERT(size);
  7602. if (!b || !m || !size) return;
  7603. nk_zero(b, sizeof(*b));
  7604. b->type = NK_BUFFER_FIXED;
  7605. b->memory.ptr = m;
  7606. b->memory.size = size;
  7607. b->size = size;
  7608. }
  7609. NK_LIB void*
  7610. nk_buffer_align(void *unaligned,
  7611. nk_size align, nk_size *alignment,
  7612. enum nk_buffer_allocation_type type)
  7613. {
  7614. void *memory = 0;
  7615. switch (type) {
  7616. default:
  7617. case NK_BUFFER_MAX:
  7618. case NK_BUFFER_FRONT:
  7619. if (align) {
  7620. memory = NK_ALIGN_PTR(unaligned, align);
  7621. *alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
  7622. } else {
  7623. memory = unaligned;
  7624. *alignment = 0;
  7625. }
  7626. break;
  7627. case NK_BUFFER_BACK:
  7628. if (align) {
  7629. memory = NK_ALIGN_PTR_BACK(unaligned, align);
  7630. *alignment = (nk_size)((nk_byte*)unaligned - (nk_byte*)memory);
  7631. } else {
  7632. memory = unaligned;
  7633. *alignment = 0;
  7634. }
  7635. break;
  7636. }
  7637. return memory;
  7638. }
  7639. NK_LIB void*
  7640. nk_buffer_realloc(struct nk_buffer *b, nk_size capacity, nk_size *size)
  7641. {
  7642. void *temp;
  7643. nk_size buffer_size;
  7644. NK_ASSERT(b);
  7645. NK_ASSERT(size);
  7646. if (!b || !size || !b->pool.alloc || !b->pool.free)
  7647. return 0;
  7648. buffer_size = b->memory.size;
  7649. temp = b->pool.alloc(b->pool.userdata, b->memory.ptr, capacity);
  7650. NK_ASSERT(temp);
  7651. if (!temp) return 0;
  7652. *size = capacity;
  7653. if (temp != b->memory.ptr) {
  7654. NK_MEMCPY(temp, b->memory.ptr, buffer_size);
  7655. b->pool.free(b->pool.userdata, b->memory.ptr);
  7656. }
  7657. if (b->size == buffer_size) {
  7658. /* no back buffer so just set correct size */
  7659. b->size = capacity;
  7660. return temp;
  7661. } else {
  7662. /* copy back buffer to the end of the new buffer */
  7663. void *dst, *src;
  7664. nk_size back_size;
  7665. back_size = buffer_size - b->size;
  7666. dst = nk_ptr_add(void, temp, capacity - back_size);
  7667. src = nk_ptr_add(void, temp, b->size);
  7668. NK_MEMCPY(dst, src, back_size);
  7669. b->size = capacity - back_size;
  7670. }
  7671. return temp;
  7672. }
  7673. NK_LIB void*
  7674. nk_buffer_alloc(struct nk_buffer *b, enum nk_buffer_allocation_type type,
  7675. nk_size size, nk_size align)
  7676. {
  7677. int full;
  7678. nk_size alignment;
  7679. void *unaligned;
  7680. void *memory;
  7681. NK_ASSERT(b);
  7682. NK_ASSERT(size);
  7683. if (!b || !size) return 0;
  7684. b->needed += size;
  7685. /* calculate total size with needed alignment + size */
  7686. if (type == NK_BUFFER_FRONT)
  7687. unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
  7688. else unaligned = nk_ptr_add(void, b->memory.ptr, b->size - size);
  7689. memory = nk_buffer_align(unaligned, align, &alignment, type);
  7690. /* check if buffer has enough memory*/
  7691. if (type == NK_BUFFER_FRONT)
  7692. full = ((b->allocated + size + alignment) > b->size);
  7693. else full = ((b->size - NK_MIN(b->size,(size + alignment))) <= b->allocated);
  7694. if (full) {
  7695. nk_size capacity;
  7696. if (b->type != NK_BUFFER_DYNAMIC)
  7697. return 0;
  7698. NK_ASSERT(b->pool.alloc && b->pool.free);
  7699. if (b->type != NK_BUFFER_DYNAMIC || !b->pool.alloc || !b->pool.free)
  7700. return 0;
  7701. /* buffer is full so allocate bigger buffer if dynamic */
  7702. capacity = (nk_size)((float)b->memory.size * b->grow_factor);
  7703. capacity = NK_MAX(capacity, nk_round_up_pow2((nk_uint)(b->allocated + size)));
  7704. b->memory.ptr = nk_buffer_realloc(b, capacity, &b->memory.size);
  7705. if (!b->memory.ptr) return 0;
  7706. /* align newly allocated pointer */
  7707. if (type == NK_BUFFER_FRONT)
  7708. unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
  7709. else unaligned = nk_ptr_add(void, b->memory.ptr, b->size - size);
  7710. memory = nk_buffer_align(unaligned, align, &alignment, type);
  7711. }
  7712. if (type == NK_BUFFER_FRONT)
  7713. b->allocated += size + alignment;
  7714. else b->size -= (size + alignment);
  7715. b->needed += alignment;
  7716. b->calls++;
  7717. return memory;
  7718. }
  7719. NK_API void
  7720. nk_buffer_push(struct nk_buffer *b, enum nk_buffer_allocation_type type,
  7721. const void *memory, nk_size size, nk_size align)
  7722. {
  7723. void *mem = nk_buffer_alloc(b, type, size, align);
  7724. if (!mem) return;
  7725. NK_MEMCPY(mem, memory, size);
  7726. }
  7727. NK_API void
  7728. nk_buffer_mark(struct nk_buffer *buffer, enum nk_buffer_allocation_type type)
  7729. {
  7730. NK_ASSERT(buffer);
  7731. if (!buffer) return;
  7732. buffer->marker[type].active = nk_true;
  7733. if (type == NK_BUFFER_BACK)
  7734. buffer->marker[type].offset = buffer->size;
  7735. else buffer->marker[type].offset = buffer->allocated;
  7736. }
  7737. NK_API void
  7738. nk_buffer_reset(struct nk_buffer *buffer, enum nk_buffer_allocation_type type)
  7739. {
  7740. NK_ASSERT(buffer);
  7741. if (!buffer) return;
  7742. if (type == NK_BUFFER_BACK) {
  7743. /* reset back buffer either back to marker or empty */
  7744. buffer->needed -= (buffer->memory.size - buffer->marker[type].offset);
  7745. if (buffer->marker[type].active)
  7746. buffer->size = buffer->marker[type].offset;
  7747. else buffer->size = buffer->memory.size;
  7748. buffer->marker[type].active = nk_false;
  7749. } else {
  7750. /* reset front buffer either back to back marker or empty */
  7751. buffer->needed -= (buffer->allocated - buffer->marker[type].offset);
  7752. if (buffer->marker[type].active)
  7753. buffer->allocated = buffer->marker[type].offset;
  7754. else buffer->allocated = 0;
  7755. buffer->marker[type].active = nk_false;
  7756. }
  7757. }
  7758. NK_API void
  7759. nk_buffer_clear(struct nk_buffer *b)
  7760. {
  7761. NK_ASSERT(b);
  7762. if (!b) return;
  7763. b->allocated = 0;
  7764. b->size = b->memory.size;
  7765. b->calls = 0;
  7766. b->needed = 0;
  7767. }
  7768. NK_API void
  7769. nk_buffer_free(struct nk_buffer *b)
  7770. {
  7771. NK_ASSERT(b);
  7772. if (!b || !b->memory.ptr) return;
  7773. if (b->type == NK_BUFFER_FIXED) return;
  7774. if (!b->pool.free) return;
  7775. NK_ASSERT(b->pool.free);
  7776. b->pool.free(b->pool.userdata, b->memory.ptr);
  7777. }
  7778. NK_API void
  7779. nk_buffer_info(struct nk_memory_status *s, struct nk_buffer *b)
  7780. {
  7781. NK_ASSERT(b);
  7782. NK_ASSERT(s);
  7783. if (!s || !b) return;
  7784. s->allocated = b->allocated;
  7785. s->size = b->memory.size;
  7786. s->needed = b->needed;
  7787. s->memory = b->memory.ptr;
  7788. s->calls = b->calls;
  7789. }
  7790. NK_API void*
  7791. nk_buffer_memory(struct nk_buffer *buffer)
  7792. {
  7793. NK_ASSERT(buffer);
  7794. if (!buffer) return 0;
  7795. return buffer->memory.ptr;
  7796. }
  7797. NK_API const void*
  7798. nk_buffer_memory_const(const struct nk_buffer *buffer)
  7799. {
  7800. NK_ASSERT(buffer);
  7801. if (!buffer) return 0;
  7802. return buffer->memory.ptr;
  7803. }
  7804. NK_API nk_size
  7805. nk_buffer_total(struct nk_buffer *buffer)
  7806. {
  7807. NK_ASSERT(buffer);
  7808. if (!buffer) return 0;
  7809. return buffer->memory.size;
  7810. }
  7811. /* ===============================================================
  7812. *
  7813. * STRING
  7814. *
  7815. * ===============================================================*/
  7816. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  7817. NK_API void
  7818. nk_str_init_default(struct nk_str *str)
  7819. {
  7820. struct nk_allocator alloc;
  7821. alloc.userdata.ptr = 0;
  7822. alloc.alloc = nk_malloc;
  7823. alloc.free = nk_mfree;
  7824. nk_buffer_init(&str->buffer, &alloc, 32);
  7825. str->len = 0;
  7826. }
  7827. #endif
  7828. NK_API void
  7829. nk_str_init(struct nk_str *str, const struct nk_allocator *alloc, nk_size size)
  7830. {
  7831. nk_buffer_init(&str->buffer, alloc, size);
  7832. str->len = 0;
  7833. }
  7834. NK_API void
  7835. nk_str_init_fixed(struct nk_str *str, void *memory, nk_size size)
  7836. {
  7837. nk_buffer_init_fixed(&str->buffer, memory, size);
  7838. str->len = 0;
  7839. }
  7840. NK_API int
  7841. nk_str_append_text_char(struct nk_str *s, const char *str, int len)
  7842. {
  7843. char *mem;
  7844. NK_ASSERT(s);
  7845. NK_ASSERT(str);
  7846. if (!s || !str || !len) return 0;
  7847. mem = (char*)nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
  7848. if (!mem) return 0;
  7849. NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
  7850. s->len += nk_utf_len(str, len);
  7851. return len;
  7852. }
  7853. NK_API int
  7854. nk_str_append_str_char(struct nk_str *s, const char *str)
  7855. {
  7856. return nk_str_append_text_char(s, str, nk_strlen(str));
  7857. }
  7858. NK_API int
  7859. nk_str_append_text_utf8(struct nk_str *str, const char *text, int len)
  7860. {
  7861. int i = 0;
  7862. int byte_len = 0;
  7863. nk_rune unicode;
  7864. if (!str || !text || !len) return 0;
  7865. for (i = 0; i < len; ++i)
  7866. byte_len += nk_utf_decode(text+byte_len, &unicode, 4);
  7867. nk_str_append_text_char(str, text, byte_len);
  7868. return len;
  7869. }
  7870. NK_API int
  7871. nk_str_append_str_utf8(struct nk_str *str, const char *text)
  7872. {
  7873. int runes = 0;
  7874. int byte_len = 0;
  7875. int num_runes = 0;
  7876. int glyph_len = 0;
  7877. nk_rune unicode;
  7878. if (!str || !text) return 0;
  7879. glyph_len = byte_len = nk_utf_decode(text+byte_len, &unicode, 4);
  7880. while (unicode != '\0' && glyph_len) {
  7881. glyph_len = nk_utf_decode(text+byte_len, &unicode, 4);
  7882. byte_len += glyph_len;
  7883. num_runes++;
  7884. }
  7885. nk_str_append_text_char(str, text, byte_len);
  7886. return runes;
  7887. }
  7888. NK_API int
  7889. nk_str_append_text_runes(struct nk_str *str, const nk_rune *text, int len)
  7890. {
  7891. int i = 0;
  7892. int byte_len = 0;
  7893. nk_glyph glyph;
  7894. NK_ASSERT(str);
  7895. if (!str || !text || !len) return 0;
  7896. for (i = 0; i < len; ++i) {
  7897. byte_len = nk_utf_encode(text[i], glyph, NK_UTF_SIZE);
  7898. if (!byte_len) break;
  7899. nk_str_append_text_char(str, glyph, byte_len);
  7900. }
  7901. return len;
  7902. }
  7903. NK_API int
  7904. nk_str_append_str_runes(struct nk_str *str, const nk_rune *runes)
  7905. {
  7906. int i = 0;
  7907. nk_glyph glyph;
  7908. int byte_len;
  7909. NK_ASSERT(str);
  7910. if (!str || !runes) return 0;
  7911. while (runes[i] != '\0') {
  7912. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  7913. nk_str_append_text_char(str, glyph, byte_len);
  7914. i++;
  7915. }
  7916. return i;
  7917. }
  7918. NK_API int
  7919. nk_str_insert_at_char(struct nk_str *s, int pos, const char *str, int len)
  7920. {
  7921. int i;
  7922. void *mem;
  7923. char *src;
  7924. char *dst;
  7925. int copylen;
  7926. NK_ASSERT(s);
  7927. NK_ASSERT(str);
  7928. NK_ASSERT(len >= 0);
  7929. if (!s || !str || !len || (nk_size)pos > s->buffer.allocated) return 0;
  7930. if ((s->buffer.allocated + (nk_size)len >= s->buffer.memory.size) &&
  7931. (s->buffer.type == NK_BUFFER_FIXED)) return 0;
  7932. copylen = (int)s->buffer.allocated - pos;
  7933. if (!copylen) {
  7934. nk_str_append_text_char(s, str, len);
  7935. return 1;
  7936. }
  7937. mem = nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
  7938. if (!mem) return 0;
  7939. /* memmove */
  7940. NK_ASSERT(((int)pos + (int)len + ((int)copylen - 1)) >= 0);
  7941. NK_ASSERT(((int)pos + ((int)copylen - 1)) >= 0);
  7942. dst = nk_ptr_add(char, s->buffer.memory.ptr, pos + len + (copylen - 1));
  7943. src = nk_ptr_add(char, s->buffer.memory.ptr, pos + (copylen-1));
  7944. for (i = 0; i < copylen; ++i) *dst-- = *src--;
  7945. mem = nk_ptr_add(void, s->buffer.memory.ptr, pos);
  7946. NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
  7947. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  7948. return 1;
  7949. }
  7950. NK_API int
  7951. nk_str_insert_at_rune(struct nk_str *str, int pos, const char *cstr, int len)
  7952. {
  7953. int glyph_len;
  7954. nk_rune unicode;
  7955. const char *begin;
  7956. const char *buffer;
  7957. NK_ASSERT(str);
  7958. NK_ASSERT(cstr);
  7959. NK_ASSERT(len);
  7960. if (!str || !cstr || !len) return 0;
  7961. begin = nk_str_at_rune(str, pos, &unicode, &glyph_len);
  7962. if (!str->len)
  7963. return nk_str_append_text_char(str, cstr, len);
  7964. buffer = nk_str_get_const(str);
  7965. if (!begin) return 0;
  7966. return nk_str_insert_at_char(str, (int)(begin - buffer), cstr, len);
  7967. }
  7968. NK_API int
  7969. nk_str_insert_text_char(struct nk_str *str, int pos, const char *text, int len)
  7970. {
  7971. return nk_str_insert_text_utf8(str, pos, text, len);
  7972. }
  7973. NK_API int
  7974. nk_str_insert_str_char(struct nk_str *str, int pos, const char *text)
  7975. {
  7976. return nk_str_insert_text_utf8(str, pos, text, nk_strlen(text));
  7977. }
  7978. NK_API int
  7979. nk_str_insert_text_utf8(struct nk_str *str, int pos, const char *text, int len)
  7980. {
  7981. int i = 0;
  7982. int byte_len = 0;
  7983. nk_rune unicode;
  7984. NK_ASSERT(str);
  7985. NK_ASSERT(text);
  7986. if (!str || !text || !len) return 0;
  7987. for (i = 0; i < len; ++i)
  7988. byte_len += nk_utf_decode(text+byte_len, &unicode, 4);
  7989. nk_str_insert_at_rune(str, pos, text, byte_len);
  7990. return len;
  7991. }
  7992. NK_API int
  7993. nk_str_insert_str_utf8(struct nk_str *str, int pos, const char *text)
  7994. {
  7995. int runes = 0;
  7996. int byte_len = 0;
  7997. int num_runes = 0;
  7998. int glyph_len = 0;
  7999. nk_rune unicode;
  8000. if (!str || !text) return 0;
  8001. glyph_len = byte_len = nk_utf_decode(text+byte_len, &unicode, 4);
  8002. while (unicode != '\0' && glyph_len) {
  8003. glyph_len = nk_utf_decode(text+byte_len, &unicode, 4);
  8004. byte_len += glyph_len;
  8005. num_runes++;
  8006. }
  8007. nk_str_insert_at_rune(str, pos, text, byte_len);
  8008. return runes;
  8009. }
  8010. NK_API int
  8011. nk_str_insert_text_runes(struct nk_str *str, int pos, const nk_rune *runes, int len)
  8012. {
  8013. int i = 0;
  8014. int byte_len = 0;
  8015. nk_glyph glyph;
  8016. NK_ASSERT(str);
  8017. if (!str || !runes || !len) return 0;
  8018. for (i = 0; i < len; ++i) {
  8019. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  8020. if (!byte_len) break;
  8021. nk_str_insert_at_rune(str, pos+i, glyph, byte_len);
  8022. }
  8023. return len;
  8024. }
  8025. NK_API int
  8026. nk_str_insert_str_runes(struct nk_str *str, int pos, const nk_rune *runes)
  8027. {
  8028. int i = 0;
  8029. nk_glyph glyph;
  8030. int byte_len;
  8031. NK_ASSERT(str);
  8032. if (!str || !runes) return 0;
  8033. while (runes[i] != '\0') {
  8034. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  8035. nk_str_insert_at_rune(str, pos+i, glyph, byte_len);
  8036. i++;
  8037. }
  8038. return i;
  8039. }
  8040. NK_API void
  8041. nk_str_remove_chars(struct nk_str *s, int len)
  8042. {
  8043. NK_ASSERT(s);
  8044. NK_ASSERT(len >= 0);
  8045. if (!s || len < 0 || (nk_size)len > s->buffer.allocated) return;
  8046. NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
  8047. s->buffer.allocated -= (nk_size)len;
  8048. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  8049. }
  8050. NK_API void
  8051. nk_str_remove_runes(struct nk_str *str, int len)
  8052. {
  8053. int index;
  8054. const char *begin;
  8055. const char *end;
  8056. nk_rune unicode;
  8057. NK_ASSERT(str);
  8058. NK_ASSERT(len >= 0);
  8059. if (!str || len < 0) return;
  8060. if (len >= str->len) {
  8061. str->len = 0;
  8062. return;
  8063. }
  8064. index = str->len - len;
  8065. begin = nk_str_at_rune(str, index, &unicode, &len);
  8066. end = (const char*)str->buffer.memory.ptr + str->buffer.allocated;
  8067. nk_str_remove_chars(str, (int)(end-begin)+1);
  8068. }
  8069. NK_API void
  8070. nk_str_delete_chars(struct nk_str *s, int pos, int len)
  8071. {
  8072. NK_ASSERT(s);
  8073. if (!s || !len || (nk_size)pos > s->buffer.allocated ||
  8074. (nk_size)(pos + len) > s->buffer.allocated) return;
  8075. if ((nk_size)(pos + len) < s->buffer.allocated) {
  8076. /* memmove */
  8077. char *dst = nk_ptr_add(char, s->buffer.memory.ptr, pos);
  8078. char *src = nk_ptr_add(char, s->buffer.memory.ptr, pos + len);
  8079. NK_MEMCPY(dst, src, s->buffer.allocated - (nk_size)(pos + len));
  8080. NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
  8081. s->buffer.allocated -= (nk_size)len;
  8082. } else nk_str_remove_chars(s, len);
  8083. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  8084. }
  8085. NK_API void
  8086. nk_str_delete_runes(struct nk_str *s, int pos, int len)
  8087. {
  8088. char *temp;
  8089. nk_rune unicode;
  8090. char *begin;
  8091. char *end;
  8092. int unused;
  8093. NK_ASSERT(s);
  8094. NK_ASSERT(s->len >= pos + len);
  8095. if (s->len < pos + len)
  8096. len = NK_CLAMP(0, (s->len - pos), s->len);
  8097. if (!len) return;
  8098. temp = (char *)s->buffer.memory.ptr;
  8099. begin = nk_str_at_rune(s, pos, &unicode, &unused);
  8100. if (!begin) return;
  8101. s->buffer.memory.ptr = begin;
  8102. end = nk_str_at_rune(s, len, &unicode, &unused);
  8103. s->buffer.memory.ptr = temp;
  8104. if (!end) return;
  8105. nk_str_delete_chars(s, (int)(begin - temp), (int)(end - begin));
  8106. }
  8107. NK_API char*
  8108. nk_str_at_char(struct nk_str *s, int pos)
  8109. {
  8110. NK_ASSERT(s);
  8111. if (!s || pos > (int)s->buffer.allocated) return 0;
  8112. return nk_ptr_add(char, s->buffer.memory.ptr, pos);
  8113. }
  8114. NK_API char*
  8115. nk_str_at_rune(struct nk_str *str, int pos, nk_rune *unicode, int *len)
  8116. {
  8117. int i = 0;
  8118. int src_len = 0;
  8119. int glyph_len = 0;
  8120. char *text;
  8121. int text_len;
  8122. NK_ASSERT(str);
  8123. NK_ASSERT(unicode);
  8124. NK_ASSERT(len);
  8125. if (!str || !unicode || !len) return 0;
  8126. if (pos < 0) {
  8127. *unicode = 0;
  8128. *len = 0;
  8129. return 0;
  8130. }
  8131. text = (char*)str->buffer.memory.ptr;
  8132. text_len = (int)str->buffer.allocated;
  8133. glyph_len = nk_utf_decode(text, unicode, text_len);
  8134. while (glyph_len) {
  8135. if (i == pos) {
  8136. *len = glyph_len;
  8137. break;
  8138. }
  8139. i++;
  8140. src_len = src_len + glyph_len;
  8141. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  8142. }
  8143. if (i != pos) return 0;
  8144. return text + src_len;
  8145. }
  8146. NK_API const char*
  8147. nk_str_at_char_const(const struct nk_str *s, int pos)
  8148. {
  8149. NK_ASSERT(s);
  8150. if (!s || pos > (int)s->buffer.allocated) return 0;
  8151. return nk_ptr_add(char, s->buffer.memory.ptr, pos);
  8152. }
  8153. NK_API const char*
  8154. nk_str_at_const(const struct nk_str *str, int pos, nk_rune *unicode, int *len)
  8155. {
  8156. int i = 0;
  8157. int src_len = 0;
  8158. int glyph_len = 0;
  8159. char *text;
  8160. int text_len;
  8161. NK_ASSERT(str);
  8162. NK_ASSERT(unicode);
  8163. NK_ASSERT(len);
  8164. if (!str || !unicode || !len) return 0;
  8165. if (pos < 0) {
  8166. *unicode = 0;
  8167. *len = 0;
  8168. return 0;
  8169. }
  8170. text = (char*)str->buffer.memory.ptr;
  8171. text_len = (int)str->buffer.allocated;
  8172. glyph_len = nk_utf_decode(text, unicode, text_len);
  8173. while (glyph_len) {
  8174. if (i == pos) {
  8175. *len = glyph_len;
  8176. break;
  8177. }
  8178. i++;
  8179. src_len = src_len + glyph_len;
  8180. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  8181. }
  8182. if (i != pos) return 0;
  8183. return text + src_len;
  8184. }
  8185. NK_API nk_rune
  8186. nk_str_rune_at(const struct nk_str *str, int pos)
  8187. {
  8188. int len;
  8189. nk_rune unicode = 0;
  8190. nk_str_at_const(str, pos, &unicode, &len);
  8191. return unicode;
  8192. }
  8193. NK_API char*
  8194. nk_str_get(struct nk_str *s)
  8195. {
  8196. NK_ASSERT(s);
  8197. if (!s || !s->len || !s->buffer.allocated) return 0;
  8198. return (char*)s->buffer.memory.ptr;
  8199. }
  8200. NK_API const char*
  8201. nk_str_get_const(const struct nk_str *s)
  8202. {
  8203. NK_ASSERT(s);
  8204. if (!s || !s->len || !s->buffer.allocated) return 0;
  8205. return (const char*)s->buffer.memory.ptr;
  8206. }
  8207. NK_API int
  8208. nk_str_len(struct nk_str *s)
  8209. {
  8210. NK_ASSERT(s);
  8211. if (!s || !s->len || !s->buffer.allocated) return 0;
  8212. return s->len;
  8213. }
  8214. NK_API int
  8215. nk_str_len_char(struct nk_str *s)
  8216. {
  8217. NK_ASSERT(s);
  8218. if (!s || !s->len || !s->buffer.allocated) return 0;
  8219. return (int)s->buffer.allocated;
  8220. }
  8221. NK_API void
  8222. nk_str_clear(struct nk_str *str)
  8223. {
  8224. NK_ASSERT(str);
  8225. nk_buffer_clear(&str->buffer);
  8226. str->len = 0;
  8227. }
  8228. NK_API void
  8229. nk_str_free(struct nk_str *str)
  8230. {
  8231. NK_ASSERT(str);
  8232. nk_buffer_free(&str->buffer);
  8233. str->len = 0;
  8234. }
  8235. /* ==============================================================
  8236. *
  8237. * DRAW
  8238. *
  8239. * ===============================================================*/
  8240. NK_LIB void
  8241. nk_command_buffer_init(struct nk_command_buffer *cb,
  8242. struct nk_buffer *b, enum nk_command_clipping clip)
  8243. {
  8244. NK_ASSERT(cb);
  8245. NK_ASSERT(b);
  8246. if (!cb || !b) return;
  8247. cb->base = b;
  8248. cb->use_clipping = (int)clip;
  8249. cb->begin = b->allocated;
  8250. cb->end = b->allocated;
  8251. cb->last = b->allocated;
  8252. }
  8253. NK_LIB void
  8254. nk_command_buffer_reset(struct nk_command_buffer *b)
  8255. {
  8256. NK_ASSERT(b);
  8257. if (!b) return;
  8258. b->begin = 0;
  8259. b->end = 0;
  8260. b->last = 0;
  8261. b->clip = nk_null_rect;
  8262. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8263. b->userdata.ptr = 0;
  8264. #endif
  8265. }
  8266. NK_LIB void*
  8267. nk_command_buffer_push(struct nk_command_buffer* b,
  8268. enum nk_command_type t, nk_size size)
  8269. {
  8270. NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_command);
  8271. struct nk_command *cmd;
  8272. nk_size alignment;
  8273. void *unaligned;
  8274. void *memory;
  8275. NK_ASSERT(b);
  8276. NK_ASSERT(b->base);
  8277. if (!b) return 0;
  8278. cmd = (struct nk_command*)nk_buffer_alloc(b->base,NK_BUFFER_FRONT,size,align);
  8279. if (!cmd) return 0;
  8280. /* make sure the offset to the next command is aligned */
  8281. b->last = (nk_size)((nk_byte*)cmd - (nk_byte*)b->base->memory.ptr);
  8282. unaligned = (nk_byte*)cmd + size;
  8283. memory = NK_ALIGN_PTR(unaligned, align);
  8284. alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
  8285. #ifdef NK_ZERO_COMMAND_MEMORY
  8286. NK_MEMSET(cmd, 0, size + alignment);
  8287. #endif
  8288. cmd->type = t;
  8289. cmd->next = b->base->allocated + alignment;
  8290. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8291. cmd->userdata = b->userdata;
  8292. #endif
  8293. b->end = cmd->next;
  8294. return cmd;
  8295. }
  8296. NK_API void
  8297. nk_push_scissor(struct nk_command_buffer *b, struct nk_rect r)
  8298. {
  8299. struct nk_command_scissor *cmd;
  8300. NK_ASSERT(b);
  8301. if (!b) return;
  8302. b->clip.x = r.x;
  8303. b->clip.y = r.y;
  8304. b->clip.w = r.w;
  8305. b->clip.h = r.h;
  8306. cmd = (struct nk_command_scissor*)
  8307. nk_command_buffer_push(b, NK_COMMAND_SCISSOR, sizeof(*cmd));
  8308. if (!cmd) return;
  8309. cmd->x = (short)r.x;
  8310. cmd->y = (short)r.y;
  8311. cmd->w = (unsigned short)NK_MAX(0, r.w);
  8312. cmd->h = (unsigned short)NK_MAX(0, r.h);
  8313. }
  8314. NK_API void
  8315. nk_stroke_line(struct nk_command_buffer *b, float x0, float y0,
  8316. float x1, float y1, float line_thickness, struct nk_color c)
  8317. {
  8318. struct nk_command_line *cmd;
  8319. NK_ASSERT(b);
  8320. if (!b || line_thickness <= 0) return;
  8321. cmd = (struct nk_command_line*)
  8322. nk_command_buffer_push(b, NK_COMMAND_LINE, sizeof(*cmd));
  8323. if (!cmd) return;
  8324. cmd->line_thickness = (unsigned short)line_thickness;
  8325. cmd->begin.x = (short)x0;
  8326. cmd->begin.y = (short)y0;
  8327. cmd->end.x = (short)x1;
  8328. cmd->end.y = (short)y1;
  8329. cmd->color = c;
  8330. }
  8331. NK_API void
  8332. nk_stroke_curve(struct nk_command_buffer *b, float ax, float ay,
  8333. float ctrl0x, float ctrl0y, float ctrl1x, float ctrl1y,
  8334. float bx, float by, float line_thickness, struct nk_color col)
  8335. {
  8336. struct nk_command_curve *cmd;
  8337. NK_ASSERT(b);
  8338. if (!b || col.a == 0 || line_thickness <= 0) return;
  8339. cmd = (struct nk_command_curve*)
  8340. nk_command_buffer_push(b, NK_COMMAND_CURVE, sizeof(*cmd));
  8341. if (!cmd) return;
  8342. cmd->line_thickness = (unsigned short)line_thickness;
  8343. cmd->begin.x = (short)ax;
  8344. cmd->begin.y = (short)ay;
  8345. cmd->ctrl[0].x = (short)ctrl0x;
  8346. cmd->ctrl[0].y = (short)ctrl0y;
  8347. cmd->ctrl[1].x = (short)ctrl1x;
  8348. cmd->ctrl[1].y = (short)ctrl1y;
  8349. cmd->end.x = (short)bx;
  8350. cmd->end.y = (short)by;
  8351. cmd->color = col;
  8352. }
  8353. NK_API void
  8354. nk_stroke_rect(struct nk_command_buffer *b, struct nk_rect rect,
  8355. float rounding, float line_thickness, struct nk_color c)
  8356. {
  8357. struct nk_command_rect *cmd;
  8358. NK_ASSERT(b);
  8359. if (!b || c.a == 0 || rect.w == 0 || rect.h == 0 || line_thickness <= 0) return;
  8360. if (b->use_clipping) {
  8361. const struct nk_rect *clip = &b->clip;
  8362. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8363. clip->x, clip->y, clip->w, clip->h)) return;
  8364. }
  8365. cmd = (struct nk_command_rect*)
  8366. nk_command_buffer_push(b, NK_COMMAND_RECT, sizeof(*cmd));
  8367. if (!cmd) return;
  8368. cmd->rounding = (unsigned short)rounding;
  8369. cmd->line_thickness = (unsigned short)line_thickness;
  8370. cmd->x = (short)rect.x;
  8371. cmd->y = (short)rect.y;
  8372. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  8373. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  8374. cmd->color = c;
  8375. }
  8376. NK_API void
  8377. nk_fill_rect(struct nk_command_buffer *b, struct nk_rect rect,
  8378. float rounding, struct nk_color c)
  8379. {
  8380. struct nk_command_rect_filled *cmd;
  8381. NK_ASSERT(b);
  8382. if (!b || c.a == 0 || rect.w == 0 || rect.h == 0) return;
  8383. if (b->use_clipping) {
  8384. const struct nk_rect *clip = &b->clip;
  8385. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8386. clip->x, clip->y, clip->w, clip->h)) return;
  8387. }
  8388. cmd = (struct nk_command_rect_filled*)
  8389. nk_command_buffer_push(b, NK_COMMAND_RECT_FILLED, sizeof(*cmd));
  8390. if (!cmd) return;
  8391. cmd->rounding = (unsigned short)rounding;
  8392. cmd->x = (short)rect.x;
  8393. cmd->y = (short)rect.y;
  8394. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  8395. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  8396. cmd->color = c;
  8397. }
  8398. NK_API void
  8399. nk_fill_rect_multi_color(struct nk_command_buffer *b, struct nk_rect rect,
  8400. struct nk_color left, struct nk_color top, struct nk_color right,
  8401. struct nk_color bottom)
  8402. {
  8403. struct nk_command_rect_multi_color *cmd;
  8404. NK_ASSERT(b);
  8405. if (!b || rect.w == 0 || rect.h == 0) return;
  8406. if (b->use_clipping) {
  8407. const struct nk_rect *clip = &b->clip;
  8408. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8409. clip->x, clip->y, clip->w, clip->h)) return;
  8410. }
  8411. cmd = (struct nk_command_rect_multi_color*)
  8412. nk_command_buffer_push(b, NK_COMMAND_RECT_MULTI_COLOR, sizeof(*cmd));
  8413. if (!cmd) return;
  8414. cmd->x = (short)rect.x;
  8415. cmd->y = (short)rect.y;
  8416. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  8417. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  8418. cmd->left = left;
  8419. cmd->top = top;
  8420. cmd->right = right;
  8421. cmd->bottom = bottom;
  8422. }
  8423. NK_API void
  8424. nk_stroke_circle(struct nk_command_buffer *b, struct nk_rect r,
  8425. float line_thickness, struct nk_color c)
  8426. {
  8427. struct nk_command_circle *cmd;
  8428. if (!b || r.w == 0 || r.h == 0 || line_thickness <= 0) return;
  8429. if (b->use_clipping) {
  8430. const struct nk_rect *clip = &b->clip;
  8431. if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
  8432. return;
  8433. }
  8434. cmd = (struct nk_command_circle*)
  8435. nk_command_buffer_push(b, NK_COMMAND_CIRCLE, sizeof(*cmd));
  8436. if (!cmd) return;
  8437. cmd->line_thickness = (unsigned short)line_thickness;
  8438. cmd->x = (short)r.x;
  8439. cmd->y = (short)r.y;
  8440. cmd->w = (unsigned short)NK_MAX(r.w, 0);
  8441. cmd->h = (unsigned short)NK_MAX(r.h, 0);
  8442. cmd->color = c;
  8443. }
  8444. NK_API void
  8445. nk_fill_circle(struct nk_command_buffer *b, struct nk_rect r, struct nk_color c)
  8446. {
  8447. struct nk_command_circle_filled *cmd;
  8448. NK_ASSERT(b);
  8449. if (!b || c.a == 0 || r.w == 0 || r.h == 0) return;
  8450. if (b->use_clipping) {
  8451. const struct nk_rect *clip = &b->clip;
  8452. if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
  8453. return;
  8454. }
  8455. cmd = (struct nk_command_circle_filled*)
  8456. nk_command_buffer_push(b, NK_COMMAND_CIRCLE_FILLED, sizeof(*cmd));
  8457. if (!cmd) return;
  8458. cmd->x = (short)r.x;
  8459. cmd->y = (short)r.y;
  8460. cmd->w = (unsigned short)NK_MAX(r.w, 0);
  8461. cmd->h = (unsigned short)NK_MAX(r.h, 0);
  8462. cmd->color = c;
  8463. }
  8464. NK_API void
  8465. nk_stroke_arc(struct nk_command_buffer *b, float cx, float cy, float radius,
  8466. float a_min, float a_max, float line_thickness, struct nk_color c)
  8467. {
  8468. struct nk_command_arc *cmd;
  8469. if (!b || c.a == 0 || line_thickness <= 0) return;
  8470. cmd = (struct nk_command_arc*)
  8471. nk_command_buffer_push(b, NK_COMMAND_ARC, sizeof(*cmd));
  8472. if (!cmd) return;
  8473. cmd->line_thickness = (unsigned short)line_thickness;
  8474. cmd->cx = (short)cx;
  8475. cmd->cy = (short)cy;
  8476. cmd->r = (unsigned short)radius;
  8477. cmd->a[0] = a_min;
  8478. cmd->a[1] = a_max;
  8479. cmd->color = c;
  8480. }
  8481. NK_API void
  8482. nk_fill_arc(struct nk_command_buffer *b, float cx, float cy, float radius,
  8483. float a_min, float a_max, struct nk_color c)
  8484. {
  8485. struct nk_command_arc_filled *cmd;
  8486. NK_ASSERT(b);
  8487. if (!b || c.a == 0) return;
  8488. cmd = (struct nk_command_arc_filled*)
  8489. nk_command_buffer_push(b, NK_COMMAND_ARC_FILLED, sizeof(*cmd));
  8490. if (!cmd) return;
  8491. cmd->cx = (short)cx;
  8492. cmd->cy = (short)cy;
  8493. cmd->r = (unsigned short)radius;
  8494. cmd->a[0] = a_min;
  8495. cmd->a[1] = a_max;
  8496. cmd->color = c;
  8497. }
  8498. NK_API void
  8499. nk_stroke_triangle(struct nk_command_buffer *b, float x0, float y0, float x1,
  8500. float y1, float x2, float y2, float line_thickness, struct nk_color c)
  8501. {
  8502. struct nk_command_triangle *cmd;
  8503. NK_ASSERT(b);
  8504. if (!b || c.a == 0 || line_thickness <= 0) return;
  8505. if (b->use_clipping) {
  8506. const struct nk_rect *clip = &b->clip;
  8507. if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
  8508. !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
  8509. !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
  8510. return;
  8511. }
  8512. cmd = (struct nk_command_triangle*)
  8513. nk_command_buffer_push(b, NK_COMMAND_TRIANGLE, sizeof(*cmd));
  8514. if (!cmd) return;
  8515. cmd->line_thickness = (unsigned short)line_thickness;
  8516. cmd->a.x = (short)x0;
  8517. cmd->a.y = (short)y0;
  8518. cmd->b.x = (short)x1;
  8519. cmd->b.y = (short)y1;
  8520. cmd->c.x = (short)x2;
  8521. cmd->c.y = (short)y2;
  8522. cmd->color = c;
  8523. }
  8524. NK_API void
  8525. nk_fill_triangle(struct nk_command_buffer *b, float x0, float y0, float x1,
  8526. float y1, float x2, float y2, struct nk_color c)
  8527. {
  8528. struct nk_command_triangle_filled *cmd;
  8529. NK_ASSERT(b);
  8530. if (!b || c.a == 0) return;
  8531. if (!b) return;
  8532. if (b->use_clipping) {
  8533. const struct nk_rect *clip = &b->clip;
  8534. if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
  8535. !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
  8536. !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
  8537. return;
  8538. }
  8539. cmd = (struct nk_command_triangle_filled*)
  8540. nk_command_buffer_push(b, NK_COMMAND_TRIANGLE_FILLED, sizeof(*cmd));
  8541. if (!cmd) return;
  8542. cmd->a.x = (short)x0;
  8543. cmd->a.y = (short)y0;
  8544. cmd->b.x = (short)x1;
  8545. cmd->b.y = (short)y1;
  8546. cmd->c.x = (short)x2;
  8547. cmd->c.y = (short)y2;
  8548. cmd->color = c;
  8549. }
  8550. NK_API void
  8551. nk_stroke_polygon(struct nk_command_buffer *b, float *points, int point_count,
  8552. float line_thickness, struct nk_color col)
  8553. {
  8554. int i;
  8555. nk_size size = 0;
  8556. struct nk_command_polygon *cmd;
  8557. NK_ASSERT(b);
  8558. if (!b || col.a == 0 || line_thickness <= 0) return;
  8559. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  8560. cmd = (struct nk_command_polygon*) nk_command_buffer_push(b, NK_COMMAND_POLYGON, size);
  8561. if (!cmd) return;
  8562. cmd->color = col;
  8563. cmd->line_thickness = (unsigned short)line_thickness;
  8564. cmd->point_count = (unsigned short)point_count;
  8565. for (i = 0; i < point_count; ++i) {
  8566. cmd->points[i].x = (short)points[i*2];
  8567. cmd->points[i].y = (short)points[i*2+1];
  8568. }
  8569. }
  8570. NK_API void
  8571. nk_fill_polygon(struct nk_command_buffer *b, float *points, int point_count,
  8572. struct nk_color col)
  8573. {
  8574. int i;
  8575. nk_size size = 0;
  8576. struct nk_command_polygon_filled *cmd;
  8577. NK_ASSERT(b);
  8578. if (!b || col.a == 0) return;
  8579. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  8580. cmd = (struct nk_command_polygon_filled*)
  8581. nk_command_buffer_push(b, NK_COMMAND_POLYGON_FILLED, size);
  8582. if (!cmd) return;
  8583. cmd->color = col;
  8584. cmd->point_count = (unsigned short)point_count;
  8585. for (i = 0; i < point_count; ++i) {
  8586. cmd->points[i].x = (short)points[i*2+0];
  8587. cmd->points[i].y = (short)points[i*2+1];
  8588. }
  8589. }
  8590. NK_API void
  8591. nk_stroke_polyline(struct nk_command_buffer *b, float *points, int point_count,
  8592. float line_thickness, struct nk_color col)
  8593. {
  8594. int i;
  8595. nk_size size = 0;
  8596. struct nk_command_polyline *cmd;
  8597. NK_ASSERT(b);
  8598. if (!b || col.a == 0 || line_thickness <= 0) return;
  8599. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  8600. cmd = (struct nk_command_polyline*) nk_command_buffer_push(b, NK_COMMAND_POLYLINE, size);
  8601. if (!cmd) return;
  8602. cmd->color = col;
  8603. cmd->point_count = (unsigned short)point_count;
  8604. cmd->line_thickness = (unsigned short)line_thickness;
  8605. for (i = 0; i < point_count; ++i) {
  8606. cmd->points[i].x = (short)points[i*2];
  8607. cmd->points[i].y = (short)points[i*2+1];
  8608. }
  8609. }
  8610. NK_API void
  8611. nk_draw_image(struct nk_command_buffer *b, struct nk_rect r,
  8612. const struct nk_image *img, struct nk_color col)
  8613. {
  8614. struct nk_command_image *cmd;
  8615. NK_ASSERT(b);
  8616. if (!b) return;
  8617. if (b->use_clipping) {
  8618. const struct nk_rect *c = &b->clip;
  8619. 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))
  8620. return;
  8621. }
  8622. cmd = (struct nk_command_image*)
  8623. nk_command_buffer_push(b, NK_COMMAND_IMAGE, sizeof(*cmd));
  8624. if (!cmd) return;
  8625. cmd->x = (short)r.x;
  8626. cmd->y = (short)r.y;
  8627. cmd->w = (unsigned short)NK_MAX(0, r.w);
  8628. cmd->h = (unsigned short)NK_MAX(0, r.h);
  8629. cmd->img = *img;
  8630. cmd->col = col;
  8631. }
  8632. NK_API void
  8633. nk_push_custom(struct nk_command_buffer *b, struct nk_rect r,
  8634. nk_command_custom_callback cb, nk_handle usr)
  8635. {
  8636. struct nk_command_custom *cmd;
  8637. NK_ASSERT(b);
  8638. if (!b) return;
  8639. if (b->use_clipping) {
  8640. const struct nk_rect *c = &b->clip;
  8641. 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))
  8642. return;
  8643. }
  8644. cmd = (struct nk_command_custom*)
  8645. nk_command_buffer_push(b, NK_COMMAND_CUSTOM, sizeof(*cmd));
  8646. if (!cmd) return;
  8647. cmd->x = (short)r.x;
  8648. cmd->y = (short)r.y;
  8649. cmd->w = (unsigned short)NK_MAX(0, r.w);
  8650. cmd->h = (unsigned short)NK_MAX(0, r.h);
  8651. cmd->callback_data = usr;
  8652. cmd->callback = cb;
  8653. }
  8654. NK_API void
  8655. nk_draw_text(struct nk_command_buffer *b, struct nk_rect r,
  8656. const char *string, int length, const struct nk_user_font *font,
  8657. struct nk_color bg, struct nk_color fg)
  8658. {
  8659. float text_width = 0;
  8660. struct nk_command_text *cmd;
  8661. NK_ASSERT(b);
  8662. NK_ASSERT(font);
  8663. if (!b || !string || !length || (bg.a == 0 && fg.a == 0)) return;
  8664. if (b->use_clipping) {
  8665. const struct nk_rect *c = &b->clip;
  8666. 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))
  8667. return;
  8668. }
  8669. /* make sure text fits inside bounds */
  8670. text_width = font->width(font->userdata, font->height, string, length);
  8671. if (text_width > r.w){
  8672. int glyphs = 0;
  8673. float txt_width = (float)text_width;
  8674. length = nk_text_clamp(font, string, length, r.w, &glyphs, &txt_width, 0,0);
  8675. }
  8676. if (!length) return;
  8677. cmd = (struct nk_command_text*)
  8678. nk_command_buffer_push(b, NK_COMMAND_TEXT, sizeof(*cmd) + (nk_size)(length + 1));
  8679. if (!cmd) return;
  8680. cmd->x = (short)r.x;
  8681. cmd->y = (short)r.y;
  8682. cmd->w = (unsigned short)r.w;
  8683. cmd->h = (unsigned short)r.h;
  8684. cmd->background = bg;
  8685. cmd->foreground = fg;
  8686. cmd->font = font;
  8687. cmd->length = length;
  8688. cmd->height = font->height;
  8689. NK_MEMCPY(cmd->string, string, (nk_size)length);
  8690. cmd->string[length] = '\0';
  8691. }
  8692. /* ===============================================================
  8693. *
  8694. * VERTEX
  8695. *
  8696. * ===============================================================*/
  8697. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  8698. NK_API void
  8699. nk_draw_list_init(struct nk_draw_list *list)
  8700. {
  8701. nk_size i = 0;
  8702. NK_ASSERT(list);
  8703. if (!list) return;
  8704. nk_zero(list, sizeof(*list));
  8705. for (i = 0; i < NK_LEN(list->circle_vtx); ++i) {
  8706. const float a = ((float)i / (float)NK_LEN(list->circle_vtx)) * 2 * NK_PI;
  8707. list->circle_vtx[i].x = (float)NK_COS(a);
  8708. list->circle_vtx[i].y = (float)NK_SIN(a);
  8709. }
  8710. }
  8711. NK_API void
  8712. nk_draw_list_setup(struct nk_draw_list *canvas, const struct nk_convert_config *config,
  8713. struct nk_buffer *cmds, struct nk_buffer *vertices, struct nk_buffer *elements,
  8714. enum nk_anti_aliasing line_aa, enum nk_anti_aliasing shape_aa)
  8715. {
  8716. NK_ASSERT(canvas);
  8717. NK_ASSERT(config);
  8718. NK_ASSERT(cmds);
  8719. NK_ASSERT(vertices);
  8720. NK_ASSERT(elements);
  8721. if (!canvas || !config || !cmds || !vertices || !elements)
  8722. return;
  8723. canvas->buffer = cmds;
  8724. canvas->config = *config;
  8725. canvas->elements = elements;
  8726. canvas->vertices = vertices;
  8727. canvas->line_AA = line_aa;
  8728. canvas->shape_AA = shape_aa;
  8729. canvas->clip_rect = nk_null_rect;
  8730. canvas->cmd_offset = 0;
  8731. canvas->element_count = 0;
  8732. canvas->vertex_count = 0;
  8733. canvas->cmd_offset = 0;
  8734. canvas->cmd_count = 0;
  8735. canvas->path_count = 0;
  8736. }
  8737. NK_API const struct nk_draw_command*
  8738. nk__draw_list_begin(const struct nk_draw_list *canvas, const struct nk_buffer *buffer)
  8739. {
  8740. nk_byte *memory;
  8741. nk_size offset;
  8742. const struct nk_draw_command *cmd;
  8743. NK_ASSERT(buffer);
  8744. if (!buffer || !buffer->size || !canvas->cmd_count)
  8745. return 0;
  8746. memory = (nk_byte*)buffer->memory.ptr;
  8747. offset = buffer->memory.size - canvas->cmd_offset;
  8748. cmd = nk_ptr_add(const struct nk_draw_command, memory, offset);
  8749. return cmd;
  8750. }
  8751. NK_API const struct nk_draw_command*
  8752. nk__draw_list_end(const struct nk_draw_list *canvas, const struct nk_buffer *buffer)
  8753. {
  8754. nk_size size;
  8755. nk_size offset;
  8756. nk_byte *memory;
  8757. const struct nk_draw_command *end;
  8758. NK_ASSERT(buffer);
  8759. NK_ASSERT(canvas);
  8760. if (!buffer || !canvas)
  8761. return 0;
  8762. memory = (nk_byte*)buffer->memory.ptr;
  8763. size = buffer->memory.size;
  8764. offset = size - canvas->cmd_offset;
  8765. end = nk_ptr_add(const struct nk_draw_command, memory, offset);
  8766. end -= (canvas->cmd_count-1);
  8767. return end;
  8768. }
  8769. NK_API const struct nk_draw_command*
  8770. nk__draw_list_next(const struct nk_draw_command *cmd,
  8771. const struct nk_buffer *buffer, const struct nk_draw_list *canvas)
  8772. {
  8773. const struct nk_draw_command *end;
  8774. NK_ASSERT(buffer);
  8775. NK_ASSERT(canvas);
  8776. if (!cmd || !buffer || !canvas)
  8777. return 0;
  8778. end = nk__draw_list_end(canvas, buffer);
  8779. if (cmd <= end) return 0;
  8780. return (cmd-1);
  8781. }
  8782. NK_INTERN struct nk_vec2*
  8783. nk_draw_list_alloc_path(struct nk_draw_list *list, int count)
  8784. {
  8785. struct nk_vec2 *points;
  8786. NK_STORAGE const nk_size point_align = NK_ALIGNOF(struct nk_vec2);
  8787. NK_STORAGE const nk_size point_size = sizeof(struct nk_vec2);
  8788. points = (struct nk_vec2*)
  8789. nk_buffer_alloc(list->buffer, NK_BUFFER_FRONT,
  8790. point_size * (nk_size)count, point_align);
  8791. if (!points) return 0;
  8792. if (!list->path_offset) {
  8793. void *memory = nk_buffer_memory(list->buffer);
  8794. list->path_offset = (unsigned int)((nk_byte*)points - (nk_byte*)memory);
  8795. }
  8796. list->path_count += (unsigned int)count;
  8797. return points;
  8798. }
  8799. NK_INTERN struct nk_vec2
  8800. nk_draw_list_path_last(struct nk_draw_list *list)
  8801. {
  8802. void *memory;
  8803. struct nk_vec2 *point;
  8804. NK_ASSERT(list->path_count);
  8805. memory = nk_buffer_memory(list->buffer);
  8806. point = nk_ptr_add(struct nk_vec2, memory, list->path_offset);
  8807. point += (list->path_count-1);
  8808. return *point;
  8809. }
  8810. NK_INTERN struct nk_draw_command*
  8811. nk_draw_list_push_command(struct nk_draw_list *list, struct nk_rect clip,
  8812. nk_handle texture)
  8813. {
  8814. NK_STORAGE const nk_size cmd_align = NK_ALIGNOF(struct nk_draw_command);
  8815. NK_STORAGE const nk_size cmd_size = sizeof(struct nk_draw_command);
  8816. struct nk_draw_command *cmd;
  8817. NK_ASSERT(list);
  8818. cmd = (struct nk_draw_command*)
  8819. nk_buffer_alloc(list->buffer, NK_BUFFER_BACK, cmd_size, cmd_align);
  8820. if (!cmd) return 0;
  8821. if (!list->cmd_count) {
  8822. nk_byte *memory = (nk_byte*)nk_buffer_memory(list->buffer);
  8823. nk_size total = nk_buffer_total(list->buffer);
  8824. memory = nk_ptr_add(nk_byte, memory, total);
  8825. list->cmd_offset = (nk_size)(memory - (nk_byte*)cmd);
  8826. }
  8827. cmd->elem_count = 0;
  8828. cmd->clip_rect = clip;
  8829. cmd->texture = texture;
  8830. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8831. cmd->userdata = list->userdata;
  8832. #endif
  8833. list->cmd_count++;
  8834. list->clip_rect = clip;
  8835. return cmd;
  8836. }
  8837. NK_INTERN struct nk_draw_command*
  8838. nk_draw_list_command_last(struct nk_draw_list *list)
  8839. {
  8840. void *memory;
  8841. nk_size size;
  8842. struct nk_draw_command *cmd;
  8843. NK_ASSERT(list->cmd_count);
  8844. memory = nk_buffer_memory(list->buffer);
  8845. size = nk_buffer_total(list->buffer);
  8846. cmd = nk_ptr_add(struct nk_draw_command, memory, size - list->cmd_offset);
  8847. return (cmd - (list->cmd_count-1));
  8848. }
  8849. NK_INTERN void
  8850. nk_draw_list_add_clip(struct nk_draw_list *list, struct nk_rect rect)
  8851. {
  8852. NK_ASSERT(list);
  8853. if (!list) return;
  8854. if (!list->cmd_count) {
  8855. nk_draw_list_push_command(list, rect, list->config.null.texture);
  8856. } else {
  8857. struct nk_draw_command *prev = nk_draw_list_command_last(list);
  8858. if (prev->elem_count == 0)
  8859. prev->clip_rect = rect;
  8860. nk_draw_list_push_command(list, rect, prev->texture);
  8861. }
  8862. }
  8863. NK_INTERN void
  8864. nk_draw_list_push_image(struct nk_draw_list *list, nk_handle texture)
  8865. {
  8866. NK_ASSERT(list);
  8867. if (!list) return;
  8868. if (!list->cmd_count) {
  8869. nk_draw_list_push_command(list, nk_null_rect, texture);
  8870. } else {
  8871. struct nk_draw_command *prev = nk_draw_list_command_last(list);
  8872. if (prev->elem_count == 0) {
  8873. prev->texture = texture;
  8874. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8875. prev->userdata = list->userdata;
  8876. #endif
  8877. } else if (prev->texture.id != texture.id
  8878. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8879. || prev->userdata.id != list->userdata.id
  8880. #endif
  8881. ) nk_draw_list_push_command(list, prev->clip_rect, texture);
  8882. }
  8883. }
  8884. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8885. NK_API void
  8886. nk_draw_list_push_userdata(struct nk_draw_list *list, nk_handle userdata)
  8887. {
  8888. list->userdata = userdata;
  8889. }
  8890. #endif
  8891. NK_INTERN void*
  8892. nk_draw_list_alloc_vertices(struct nk_draw_list *list, nk_size count)
  8893. {
  8894. void *vtx;
  8895. NK_ASSERT(list);
  8896. if (!list) return 0;
  8897. vtx = nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT,
  8898. list->config.vertex_size*count, list->config.vertex_alignment);
  8899. if (!vtx) return 0;
  8900. list->vertex_count += (unsigned int)count;
  8901. /* This assert triggers because your are drawing a lot of stuff and nuklear
  8902. * defined `nk_draw_index` as `nk_ushort` to safe space be default.
  8903. *
  8904. * So you reached the maximum number of indicies or rather vertexes.
  8905. * To solve this issue please change typdef `nk_draw_index` to `nk_uint`
  8906. * and don't forget to specify the new element size in your drawing
  8907. * backend (OpenGL, DirectX, ...). For example in OpenGL for `glDrawElements`
  8908. * instead of specifing `GL_UNSIGNED_SHORT` you have to define `GL_UNSIGNED_INT`.
  8909. * Sorry for the inconvenience. */
  8910. if(sizeof(nk_draw_index)==2) NK_ASSERT((list->vertex_count < NK_USHORT_MAX &&
  8911. "To many verticies for 16-bit vertex indicies. Please read comment above on how to solve this problem"));
  8912. return vtx;
  8913. }
  8914. NK_INTERN nk_draw_index*
  8915. nk_draw_list_alloc_elements(struct nk_draw_list *list, nk_size count)
  8916. {
  8917. nk_draw_index *ids;
  8918. struct nk_draw_command *cmd;
  8919. NK_STORAGE const nk_size elem_align = NK_ALIGNOF(nk_draw_index);
  8920. NK_STORAGE const nk_size elem_size = sizeof(nk_draw_index);
  8921. NK_ASSERT(list);
  8922. if (!list) return 0;
  8923. ids = (nk_draw_index*)
  8924. nk_buffer_alloc(list->elements, NK_BUFFER_FRONT, elem_size*count, elem_align);
  8925. if (!ids) return 0;
  8926. cmd = nk_draw_list_command_last(list);
  8927. list->element_count += (unsigned int)count;
  8928. cmd->elem_count += (unsigned int)count;
  8929. return ids;
  8930. }
  8931. NK_INTERN int
  8932. nk_draw_vertex_layout_element_is_end_of_layout(
  8933. const struct nk_draw_vertex_layout_element *element)
  8934. {
  8935. return (element->attribute == NK_VERTEX_ATTRIBUTE_COUNT ||
  8936. element->format == NK_FORMAT_COUNT);
  8937. }
  8938. NK_INTERN void
  8939. nk_draw_vertex_color(void *attr, const float *vals,
  8940. enum nk_draw_vertex_layout_format format)
  8941. {
  8942. /* if this triggers you tried to provide a value format for a color */
  8943. float val[4];
  8944. NK_ASSERT(format >= NK_FORMAT_COLOR_BEGIN);
  8945. NK_ASSERT(format <= NK_FORMAT_COLOR_END);
  8946. if (format < NK_FORMAT_COLOR_BEGIN || format > NK_FORMAT_COLOR_END) return;
  8947. val[0] = NK_SATURATE(vals[0]);
  8948. val[1] = NK_SATURATE(vals[1]);
  8949. val[2] = NK_SATURATE(vals[2]);
  8950. val[3] = NK_SATURATE(vals[3]);
  8951. switch (format) {
  8952. default: NK_ASSERT(0 && "Invalid vertex layout color format"); break;
  8953. case NK_FORMAT_R8G8B8A8:
  8954. case NK_FORMAT_R8G8B8: {
  8955. struct nk_color col = nk_rgba_fv(val);
  8956. NK_MEMCPY(attr, &col.r, sizeof(col));
  8957. } break;
  8958. case NK_FORMAT_B8G8R8A8: {
  8959. struct nk_color col = nk_rgba_fv(val);
  8960. struct nk_color bgra = nk_rgba(col.b, col.g, col.r, col.a);
  8961. NK_MEMCPY(attr, &bgra, sizeof(bgra));
  8962. } break;
  8963. case NK_FORMAT_R16G15B16: {
  8964. nk_ushort col[3];
  8965. col[0] = (nk_ushort)(val[0]*(float)NK_USHORT_MAX);
  8966. col[1] = (nk_ushort)(val[1]*(float)NK_USHORT_MAX);
  8967. col[2] = (nk_ushort)(val[2]*(float)NK_USHORT_MAX);
  8968. NK_MEMCPY(attr, col, sizeof(col));
  8969. } break;
  8970. case NK_FORMAT_R16G15B16A16: {
  8971. nk_ushort col[4];
  8972. col[0] = (nk_ushort)(val[0]*(float)NK_USHORT_MAX);
  8973. col[1] = (nk_ushort)(val[1]*(float)NK_USHORT_MAX);
  8974. col[2] = (nk_ushort)(val[2]*(float)NK_USHORT_MAX);
  8975. col[3] = (nk_ushort)(val[3]*(float)NK_USHORT_MAX);
  8976. NK_MEMCPY(attr, col, sizeof(col));
  8977. } break;
  8978. case NK_FORMAT_R32G32B32: {
  8979. nk_uint col[3];
  8980. col[0] = (nk_uint)(val[0]*(float)NK_UINT_MAX);
  8981. col[1] = (nk_uint)(val[1]*(float)NK_UINT_MAX);
  8982. col[2] = (nk_uint)(val[2]*(float)NK_UINT_MAX);
  8983. NK_MEMCPY(attr, col, sizeof(col));
  8984. } break;
  8985. case NK_FORMAT_R32G32B32A32: {
  8986. nk_uint col[4];
  8987. col[0] = (nk_uint)(val[0]*(float)NK_UINT_MAX);
  8988. col[1] = (nk_uint)(val[1]*(float)NK_UINT_MAX);
  8989. col[2] = (nk_uint)(val[2]*(float)NK_UINT_MAX);
  8990. col[3] = (nk_uint)(val[3]*(float)NK_UINT_MAX);
  8991. NK_MEMCPY(attr, col, sizeof(col));
  8992. } break;
  8993. case NK_FORMAT_R32G32B32A32_FLOAT:
  8994. NK_MEMCPY(attr, val, sizeof(float)*4);
  8995. break;
  8996. case NK_FORMAT_R32G32B32A32_DOUBLE: {
  8997. double col[4];
  8998. col[0] = (double)val[0];
  8999. col[1] = (double)val[1];
  9000. col[2] = (double)val[2];
  9001. col[3] = (double)val[3];
  9002. NK_MEMCPY(attr, col, sizeof(col));
  9003. } break;
  9004. case NK_FORMAT_RGB32:
  9005. case NK_FORMAT_RGBA32: {
  9006. struct nk_color col = nk_rgba_fv(val);
  9007. nk_uint color = nk_color_u32(col);
  9008. NK_MEMCPY(attr, &color, sizeof(color));
  9009. } break; }
  9010. }
  9011. NK_INTERN void
  9012. nk_draw_vertex_element(void *dst, const float *values, int value_count,
  9013. enum nk_draw_vertex_layout_format format)
  9014. {
  9015. int value_index;
  9016. void *attribute = dst;
  9017. /* if this triggers you tried to provide a color format for a value */
  9018. NK_ASSERT(format < NK_FORMAT_COLOR_BEGIN);
  9019. if (format >= NK_FORMAT_COLOR_BEGIN && format <= NK_FORMAT_COLOR_END) return;
  9020. for (value_index = 0; value_index < value_count; ++value_index) {
  9021. switch (format) {
  9022. default: NK_ASSERT(0 && "invalid vertex layout format"); break;
  9023. case NK_FORMAT_SCHAR: {
  9024. char value = (char)NK_CLAMP((float)NK_SCHAR_MIN, values[value_index], (float)NK_SCHAR_MAX);
  9025. NK_MEMCPY(attribute, &value, sizeof(value));
  9026. attribute = (void*)((char*)attribute + sizeof(char));
  9027. } break;
  9028. case NK_FORMAT_SSHORT: {
  9029. nk_short value = (nk_short)NK_CLAMP((float)NK_SSHORT_MIN, values[value_index], (float)NK_SSHORT_MAX);
  9030. NK_MEMCPY(attribute, &value, sizeof(value));
  9031. attribute = (void*)((char*)attribute + sizeof(value));
  9032. } break;
  9033. case NK_FORMAT_SINT: {
  9034. nk_int value = (nk_int)NK_CLAMP((float)NK_SINT_MIN, values[value_index], (float)NK_SINT_MAX);
  9035. NK_MEMCPY(attribute, &value, sizeof(value));
  9036. attribute = (void*)((char*)attribute + sizeof(nk_int));
  9037. } break;
  9038. case NK_FORMAT_UCHAR: {
  9039. unsigned char value = (unsigned char)NK_CLAMP((float)NK_UCHAR_MIN, values[value_index], (float)NK_UCHAR_MAX);
  9040. NK_MEMCPY(attribute, &value, sizeof(value));
  9041. attribute = (void*)((char*)attribute + sizeof(unsigned char));
  9042. } break;
  9043. case NK_FORMAT_USHORT: {
  9044. nk_ushort value = (nk_ushort)NK_CLAMP((float)NK_USHORT_MIN, values[value_index], (float)NK_USHORT_MAX);
  9045. NK_MEMCPY(attribute, &value, sizeof(value));
  9046. attribute = (void*)((char*)attribute + sizeof(value));
  9047. } break;
  9048. case NK_FORMAT_UINT: {
  9049. nk_uint value = (nk_uint)NK_CLAMP((float)NK_UINT_MIN, values[value_index], (float)NK_UINT_MAX);
  9050. NK_MEMCPY(attribute, &value, sizeof(value));
  9051. attribute = (void*)((char*)attribute + sizeof(nk_uint));
  9052. } break;
  9053. case NK_FORMAT_FLOAT:
  9054. NK_MEMCPY(attribute, &values[value_index], sizeof(values[value_index]));
  9055. attribute = (void*)((char*)attribute + sizeof(float));
  9056. break;
  9057. case NK_FORMAT_DOUBLE: {
  9058. double value = (double)values[value_index];
  9059. NK_MEMCPY(attribute, &value, sizeof(value));
  9060. attribute = (void*)((char*)attribute + sizeof(double));
  9061. } break;
  9062. }
  9063. }
  9064. }
  9065. NK_INTERN void*
  9066. nk_draw_vertex(void *dst, const struct nk_convert_config *config,
  9067. struct nk_vec2 pos, struct nk_vec2 uv, struct nk_colorf color)
  9068. {
  9069. void *result = (void*)((char*)dst + config->vertex_size);
  9070. const struct nk_draw_vertex_layout_element *elem_iter = config->vertex_layout;
  9071. while (!nk_draw_vertex_layout_element_is_end_of_layout(elem_iter)) {
  9072. void *address = (void*)((char*)dst + elem_iter->offset);
  9073. switch (elem_iter->attribute) {
  9074. case NK_VERTEX_ATTRIBUTE_COUNT:
  9075. default: NK_ASSERT(0 && "wrong element attribute"); break;
  9076. case NK_VERTEX_POSITION: nk_draw_vertex_element(address, &pos.x, 2, elem_iter->format); break;
  9077. case NK_VERTEX_TEXCOORD: nk_draw_vertex_element(address, &uv.x, 2, elem_iter->format); break;
  9078. case NK_VERTEX_COLOR: nk_draw_vertex_color(address, &color.r, elem_iter->format); break;
  9079. }
  9080. elem_iter++;
  9081. }
  9082. return result;
  9083. }
  9084. NK_API void
  9085. nk_draw_list_stroke_poly_line(struct nk_draw_list *list, const struct nk_vec2 *points,
  9086. const unsigned int points_count, struct nk_color color, enum nk_draw_list_stroke closed,
  9087. float thickness, enum nk_anti_aliasing aliasing)
  9088. {
  9089. nk_size count;
  9090. int thick_line;
  9091. struct nk_colorf col;
  9092. struct nk_colorf col_trans;
  9093. NK_ASSERT(list);
  9094. if (!list || points_count < 2) return;
  9095. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  9096. count = points_count;
  9097. if (!closed) count = points_count-1;
  9098. thick_line = thickness > 1.0f;
  9099. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9100. nk_draw_list_push_userdata(list, list->userdata);
  9101. #endif
  9102. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  9103. nk_color_fv(&col.r, color);
  9104. col_trans = col;
  9105. col_trans.a = 0;
  9106. if (aliasing == NK_ANTI_ALIASING_ON) {
  9107. /* ANTI-ALIASED STROKE */
  9108. const float AA_SIZE = 1.0f;
  9109. NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
  9110. NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
  9111. /* allocate vertices and elements */
  9112. nk_size i1 = 0;
  9113. nk_size vertex_offset;
  9114. nk_size index = list->vertex_count;
  9115. const nk_size idx_count = (thick_line) ? (count * 18) : (count * 12);
  9116. const nk_size vtx_count = (thick_line) ? (points_count * 4): (points_count *3);
  9117. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9118. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  9119. nk_size size;
  9120. struct nk_vec2 *normals, *temp;
  9121. if (!vtx || !ids) return;
  9122. /* temporary allocate normals + points */
  9123. vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
  9124. nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
  9125. size = pnt_size * ((thick_line) ? 5 : 3) * points_count;
  9126. normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
  9127. if (!normals) return;
  9128. temp = normals + points_count;
  9129. /* make sure vertex pointer is still correct */
  9130. vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
  9131. /* calculate normals */
  9132. for (i1 = 0; i1 < count; ++i1) {
  9133. const nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
  9134. struct nk_vec2 diff = nk_vec2_sub(points[i2], points[i1]);
  9135. float len;
  9136. /* vec2 inverted length */
  9137. len = nk_vec2_len_sqr(diff);
  9138. if (len != 0.0f)
  9139. len = nk_inv_sqrt(len);
  9140. else len = 1.0f;
  9141. diff = nk_vec2_muls(diff, len);
  9142. normals[i1].x = diff.y;
  9143. normals[i1].y = -diff.x;
  9144. }
  9145. if (!closed)
  9146. normals[points_count-1] = normals[points_count-2];
  9147. if (!thick_line) {
  9148. nk_size idx1, i;
  9149. if (!closed) {
  9150. struct nk_vec2 d;
  9151. temp[0] = nk_vec2_add(points[0], nk_vec2_muls(normals[0], AA_SIZE));
  9152. temp[1] = nk_vec2_sub(points[0], nk_vec2_muls(normals[0], AA_SIZE));
  9153. d = nk_vec2_muls(normals[points_count-1], AA_SIZE);
  9154. temp[(points_count-1) * 2 + 0] = nk_vec2_add(points[points_count-1], d);
  9155. temp[(points_count-1) * 2 + 1] = nk_vec2_sub(points[points_count-1], d);
  9156. }
  9157. /* fill elements */
  9158. idx1 = index;
  9159. for (i1 = 0; i1 < count; i1++) {
  9160. struct nk_vec2 dm;
  9161. float dmr2;
  9162. nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
  9163. nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 3);
  9164. /* average normals */
  9165. dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
  9166. dmr2 = dm.x * dm.x + dm.y* dm.y;
  9167. if (dmr2 > 0.000001f) {
  9168. float scale = 1.0f/dmr2;
  9169. scale = NK_MIN(100.0f, scale);
  9170. dm = nk_vec2_muls(dm, scale);
  9171. }
  9172. dm = nk_vec2_muls(dm, AA_SIZE);
  9173. temp[i2*2+0] = nk_vec2_add(points[i2], dm);
  9174. temp[i2*2+1] = nk_vec2_sub(points[i2], dm);
  9175. ids[0] = (nk_draw_index)(idx2 + 0); ids[1] = (nk_draw_index)(idx1+0);
  9176. ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
  9177. ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+0);
  9178. ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
  9179. ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
  9180. ids[10]= (nk_draw_index)(idx2 + 0); ids[11]= (nk_draw_index)(idx2+1);
  9181. ids += 12;
  9182. idx1 = idx2;
  9183. }
  9184. /* fill vertices */
  9185. for (i = 0; i < points_count; ++i) {
  9186. const struct nk_vec2 uv = list->config.null.uv;
  9187. vtx = nk_draw_vertex(vtx, &list->config, points[i], uv, col);
  9188. vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+0], uv, col_trans);
  9189. vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+1], uv, col_trans);
  9190. }
  9191. } else {
  9192. nk_size idx1, i;
  9193. const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f;
  9194. if (!closed) {
  9195. struct nk_vec2 d1 = nk_vec2_muls(normals[0], half_inner_thickness + AA_SIZE);
  9196. struct nk_vec2 d2 = nk_vec2_muls(normals[0], half_inner_thickness);
  9197. temp[0] = nk_vec2_add(points[0], d1);
  9198. temp[1] = nk_vec2_add(points[0], d2);
  9199. temp[2] = nk_vec2_sub(points[0], d2);
  9200. temp[3] = nk_vec2_sub(points[0], d1);
  9201. d1 = nk_vec2_muls(normals[points_count-1], half_inner_thickness + AA_SIZE);
  9202. d2 = nk_vec2_muls(normals[points_count-1], half_inner_thickness);
  9203. temp[(points_count-1)*4+0] = nk_vec2_add(points[points_count-1], d1);
  9204. temp[(points_count-1)*4+1] = nk_vec2_add(points[points_count-1], d2);
  9205. temp[(points_count-1)*4+2] = nk_vec2_sub(points[points_count-1], d2);
  9206. temp[(points_count-1)*4+3] = nk_vec2_sub(points[points_count-1], d1);
  9207. }
  9208. /* add all elements */
  9209. idx1 = index;
  9210. for (i1 = 0; i1 < count; ++i1) {
  9211. struct nk_vec2 dm_out, dm_in;
  9212. const nk_size i2 = ((i1+1) == points_count) ? 0: (i1 + 1);
  9213. nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 4);
  9214. /* average normals */
  9215. struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
  9216. float dmr2 = dm.x * dm.x + dm.y* dm.y;
  9217. if (dmr2 > 0.000001f) {
  9218. float scale = 1.0f/dmr2;
  9219. scale = NK_MIN(100.0f, scale);
  9220. dm = nk_vec2_muls(dm, scale);
  9221. }
  9222. dm_out = nk_vec2_muls(dm, ((half_inner_thickness) + AA_SIZE));
  9223. dm_in = nk_vec2_muls(dm, half_inner_thickness);
  9224. temp[i2*4+0] = nk_vec2_add(points[i2], dm_out);
  9225. temp[i2*4+1] = nk_vec2_add(points[i2], dm_in);
  9226. temp[i2*4+2] = nk_vec2_sub(points[i2], dm_in);
  9227. temp[i2*4+3] = nk_vec2_sub(points[i2], dm_out);
  9228. /* add indexes */
  9229. ids[0] = (nk_draw_index)(idx2 + 1); ids[1] = (nk_draw_index)(idx1+1);
  9230. ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
  9231. ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+1);
  9232. ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
  9233. ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
  9234. ids[10]= (nk_draw_index)(idx2 + 0); ids[11] = (nk_draw_index)(idx2+1);
  9235. ids[12]= (nk_draw_index)(idx2 + 2); ids[13] = (nk_draw_index)(idx1+2);
  9236. ids[14]= (nk_draw_index)(idx1 + 3); ids[15] = (nk_draw_index)(idx1+3);
  9237. ids[16]= (nk_draw_index)(idx2 + 3); ids[17] = (nk_draw_index)(idx2+2);
  9238. ids += 18;
  9239. idx1 = idx2;
  9240. }
  9241. /* add vertices */
  9242. for (i = 0; i < points_count; ++i) {
  9243. const struct nk_vec2 uv = list->config.null.uv;
  9244. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+0], uv, col_trans);
  9245. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+1], uv, col);
  9246. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+2], uv, col);
  9247. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+3], uv, col_trans);
  9248. }
  9249. }
  9250. /* free temporary normals + points */
  9251. nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
  9252. } else {
  9253. /* NON ANTI-ALIASED STROKE */
  9254. nk_size i1 = 0;
  9255. nk_size idx = list->vertex_count;
  9256. const nk_size idx_count = count * 6;
  9257. const nk_size vtx_count = count * 4;
  9258. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9259. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  9260. if (!vtx || !ids) return;
  9261. for (i1 = 0; i1 < count; ++i1) {
  9262. float dx, dy;
  9263. const struct nk_vec2 uv = list->config.null.uv;
  9264. const nk_size i2 = ((i1+1) == points_count) ? 0 : i1 + 1;
  9265. const struct nk_vec2 p1 = points[i1];
  9266. const struct nk_vec2 p2 = points[i2];
  9267. struct nk_vec2 diff = nk_vec2_sub(p2, p1);
  9268. float len;
  9269. /* vec2 inverted length */
  9270. len = nk_vec2_len_sqr(diff);
  9271. if (len != 0.0f)
  9272. len = nk_inv_sqrt(len);
  9273. else len = 1.0f;
  9274. diff = nk_vec2_muls(diff, len);
  9275. /* add vertices */
  9276. dx = diff.x * (thickness * 0.5f);
  9277. dy = diff.y * (thickness * 0.5f);
  9278. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x + dy, p1.y - dx), uv, col);
  9279. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x + dy, p2.y - dx), uv, col);
  9280. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x - dy, p2.y + dx), uv, col);
  9281. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x - dy, p1.y + dx), uv, col);
  9282. ids[0] = (nk_draw_index)(idx+0); ids[1] = (nk_draw_index)(idx+1);
  9283. ids[2] = (nk_draw_index)(idx+2); ids[3] = (nk_draw_index)(idx+0);
  9284. ids[4] = (nk_draw_index)(idx+2); ids[5] = (nk_draw_index)(idx+3);
  9285. ids += 6;
  9286. idx += 4;
  9287. }
  9288. }
  9289. }
  9290. NK_API void
  9291. nk_draw_list_fill_poly_convex(struct nk_draw_list *list,
  9292. const struct nk_vec2 *points, const unsigned int points_count,
  9293. struct nk_color color, enum nk_anti_aliasing aliasing)
  9294. {
  9295. struct nk_colorf col;
  9296. struct nk_colorf col_trans;
  9297. NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
  9298. NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
  9299. NK_ASSERT(list);
  9300. if (!list || points_count < 3) return;
  9301. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9302. nk_draw_list_push_userdata(list, list->userdata);
  9303. #endif
  9304. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  9305. nk_color_fv(&col.r, color);
  9306. col_trans = col;
  9307. col_trans.a = 0;
  9308. if (aliasing == NK_ANTI_ALIASING_ON) {
  9309. nk_size i = 0;
  9310. nk_size i0 = 0;
  9311. nk_size i1 = 0;
  9312. const float AA_SIZE = 1.0f;
  9313. nk_size vertex_offset = 0;
  9314. nk_size index = list->vertex_count;
  9315. const nk_size idx_count = (points_count-2)*3 + points_count*6;
  9316. const nk_size vtx_count = (points_count*2);
  9317. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9318. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  9319. nk_size size = 0;
  9320. struct nk_vec2 *normals = 0;
  9321. unsigned int vtx_inner_idx = (unsigned int)(index + 0);
  9322. unsigned int vtx_outer_idx = (unsigned int)(index + 1);
  9323. if (!vtx || !ids) return;
  9324. /* temporary allocate normals */
  9325. vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
  9326. nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
  9327. size = pnt_size * points_count;
  9328. normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
  9329. if (!normals) return;
  9330. vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
  9331. /* add elements */
  9332. for (i = 2; i < points_count; i++) {
  9333. ids[0] = (nk_draw_index)(vtx_inner_idx);
  9334. ids[1] = (nk_draw_index)(vtx_inner_idx + ((i-1) << 1));
  9335. ids[2] = (nk_draw_index)(vtx_inner_idx + (i << 1));
  9336. ids += 3;
  9337. }
  9338. /* compute normals */
  9339. for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
  9340. struct nk_vec2 p0 = points[i0];
  9341. struct nk_vec2 p1 = points[i1];
  9342. struct nk_vec2 diff = nk_vec2_sub(p1, p0);
  9343. /* vec2 inverted length */
  9344. float len = nk_vec2_len_sqr(diff);
  9345. if (len != 0.0f)
  9346. len = nk_inv_sqrt(len);
  9347. else len = 1.0f;
  9348. diff = nk_vec2_muls(diff, len);
  9349. normals[i0].x = diff.y;
  9350. normals[i0].y = -diff.x;
  9351. }
  9352. /* add vertices + indexes */
  9353. for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
  9354. const struct nk_vec2 uv = list->config.null.uv;
  9355. struct nk_vec2 n0 = normals[i0];
  9356. struct nk_vec2 n1 = normals[i1];
  9357. struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(n0, n1), 0.5f);
  9358. float dmr2 = dm.x*dm.x + dm.y*dm.y;
  9359. if (dmr2 > 0.000001f) {
  9360. float scale = 1.0f / dmr2;
  9361. scale = NK_MIN(scale, 100.0f);
  9362. dm = nk_vec2_muls(dm, scale);
  9363. }
  9364. dm = nk_vec2_muls(dm, AA_SIZE * 0.5f);
  9365. /* add vertices */
  9366. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_sub(points[i1], dm), uv, col);
  9367. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_add(points[i1], dm), uv, col_trans);
  9368. /* add indexes */
  9369. ids[0] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
  9370. ids[1] = (nk_draw_index)(vtx_inner_idx+(i0<<1));
  9371. ids[2] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
  9372. ids[3] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
  9373. ids[4] = (nk_draw_index)(vtx_outer_idx+(i1<<1));
  9374. ids[5] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
  9375. ids += 6;
  9376. }
  9377. /* free temporary normals + points */
  9378. nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
  9379. } else {
  9380. nk_size i = 0;
  9381. nk_size index = list->vertex_count;
  9382. const nk_size idx_count = (points_count-2)*3;
  9383. const nk_size vtx_count = points_count;
  9384. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9385. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  9386. if (!vtx || !ids) return;
  9387. for (i = 0; i < vtx_count; ++i)
  9388. vtx = nk_draw_vertex(vtx, &list->config, points[i], list->config.null.uv, col);
  9389. for (i = 2; i < points_count; ++i) {
  9390. ids[0] = (nk_draw_index)index;
  9391. ids[1] = (nk_draw_index)(index+ i - 1);
  9392. ids[2] = (nk_draw_index)(index+i);
  9393. ids += 3;
  9394. }
  9395. }
  9396. }
  9397. NK_API void
  9398. nk_draw_list_path_clear(struct nk_draw_list *list)
  9399. {
  9400. NK_ASSERT(list);
  9401. if (!list) return;
  9402. nk_buffer_reset(list->buffer, NK_BUFFER_FRONT);
  9403. list->path_count = 0;
  9404. list->path_offset = 0;
  9405. }
  9406. NK_API void
  9407. nk_draw_list_path_line_to(struct nk_draw_list *list, struct nk_vec2 pos)
  9408. {
  9409. struct nk_vec2 *points = 0;
  9410. struct nk_draw_command *cmd = 0;
  9411. NK_ASSERT(list);
  9412. if (!list) return;
  9413. if (!list->cmd_count)
  9414. nk_draw_list_add_clip(list, nk_null_rect);
  9415. cmd = nk_draw_list_command_last(list);
  9416. if (cmd && cmd->texture.ptr != list->config.null.texture.ptr)
  9417. nk_draw_list_push_image(list, list->config.null.texture);
  9418. points = nk_draw_list_alloc_path(list, 1);
  9419. if (!points) return;
  9420. points[0] = pos;
  9421. }
  9422. NK_API void
  9423. nk_draw_list_path_arc_to_fast(struct nk_draw_list *list, struct nk_vec2 center,
  9424. float radius, int a_min, int a_max)
  9425. {
  9426. int a = 0;
  9427. NK_ASSERT(list);
  9428. if (!list) return;
  9429. if (a_min <= a_max) {
  9430. for (a = a_min; a <= a_max; a++) {
  9431. const struct nk_vec2 c = list->circle_vtx[(nk_size)a % NK_LEN(list->circle_vtx)];
  9432. const float x = center.x + c.x * radius;
  9433. const float y = center.y + c.y * radius;
  9434. nk_draw_list_path_line_to(list, nk_vec2(x, y));
  9435. }
  9436. }
  9437. }
  9438. NK_API void
  9439. nk_draw_list_path_arc_to(struct nk_draw_list *list, struct nk_vec2 center,
  9440. float radius, float a_min, float a_max, unsigned int segments)
  9441. {
  9442. unsigned int i = 0;
  9443. NK_ASSERT(list);
  9444. if (!list) return;
  9445. if (radius == 0.0f) return;
  9446. /* This algorithm for arc drawing relies on these two trigonometric identities[1]:
  9447. sin(a + b) = sin(a) * cos(b) + cos(a) * sin(b)
  9448. cos(a + b) = cos(a) * cos(b) - sin(a) * sin(b)
  9449. Two coordinates (x, y) of a point on a circle centered on
  9450. the origin can be written in polar form as:
  9451. x = r * cos(a)
  9452. y = r * sin(a)
  9453. where r is the radius of the circle,
  9454. a is the angle between (x, y) and the origin.
  9455. This allows us to rotate the coordinates around the
  9456. origin by an angle b using the following transformation:
  9457. x' = r * cos(a + b) = x * cos(b) - y * sin(b)
  9458. y' = r * sin(a + b) = y * cos(b) + x * sin(b)
  9459. [1] https://en.wikipedia.org/wiki/List_of_trigonometric_identities#Angle_sum_and_difference_identities
  9460. */
  9461. {const float d_angle = (a_max - a_min) / (float)segments;
  9462. const float sin_d = (float)NK_SIN(d_angle);
  9463. const float cos_d = (float)NK_COS(d_angle);
  9464. float cx = (float)NK_COS(a_min) * radius;
  9465. float cy = (float)NK_SIN(a_min) * radius;
  9466. for(i = 0; i <= segments; ++i) {
  9467. float new_cx, new_cy;
  9468. const float x = center.x + cx;
  9469. const float y = center.y + cy;
  9470. nk_draw_list_path_line_to(list, nk_vec2(x, y));
  9471. new_cx = cx * cos_d - cy * sin_d;
  9472. new_cy = cy * cos_d + cx * sin_d;
  9473. cx = new_cx;
  9474. cy = new_cy;
  9475. }}
  9476. }
  9477. NK_API void
  9478. nk_draw_list_path_rect_to(struct nk_draw_list *list, struct nk_vec2 a,
  9479. struct nk_vec2 b, float rounding)
  9480. {
  9481. float r;
  9482. NK_ASSERT(list);
  9483. if (!list) return;
  9484. r = rounding;
  9485. r = NK_MIN(r, ((b.x-a.x) < 0) ? -(b.x-a.x): (b.x-a.x));
  9486. r = NK_MIN(r, ((b.y-a.y) < 0) ? -(b.y-a.y): (b.y-a.y));
  9487. if (r == 0.0f) {
  9488. nk_draw_list_path_line_to(list, a);
  9489. nk_draw_list_path_line_to(list, nk_vec2(b.x,a.y));
  9490. nk_draw_list_path_line_to(list, b);
  9491. nk_draw_list_path_line_to(list, nk_vec2(a.x,b.y));
  9492. } else {
  9493. nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, a.y + r), r, 6, 9);
  9494. nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, a.y + r), r, 9, 12);
  9495. nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, b.y - r), r, 0, 3);
  9496. nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, b.y - r), r, 3, 6);
  9497. }
  9498. }
  9499. NK_API void
  9500. nk_draw_list_path_curve_to(struct nk_draw_list *list, struct nk_vec2 p2,
  9501. struct nk_vec2 p3, struct nk_vec2 p4, unsigned int num_segments)
  9502. {
  9503. float t_step;
  9504. unsigned int i_step;
  9505. struct nk_vec2 p1;
  9506. NK_ASSERT(list);
  9507. NK_ASSERT(list->path_count);
  9508. if (!list || !list->path_count) return;
  9509. num_segments = NK_MAX(num_segments, 1);
  9510. p1 = nk_draw_list_path_last(list);
  9511. t_step = 1.0f/(float)num_segments;
  9512. for (i_step = 1; i_step <= num_segments; ++i_step) {
  9513. float t = t_step * (float)i_step;
  9514. float u = 1.0f - t;
  9515. float w1 = u*u*u;
  9516. float w2 = 3*u*u*t;
  9517. float w3 = 3*u*t*t;
  9518. float w4 = t * t *t;
  9519. float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
  9520. float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
  9521. nk_draw_list_path_line_to(list, nk_vec2(x,y));
  9522. }
  9523. }
  9524. NK_API void
  9525. nk_draw_list_path_fill(struct nk_draw_list *list, struct nk_color color)
  9526. {
  9527. struct nk_vec2 *points;
  9528. NK_ASSERT(list);
  9529. if (!list) return;
  9530. points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
  9531. nk_draw_list_fill_poly_convex(list, points, list->path_count, color, list->config.shape_AA);
  9532. nk_draw_list_path_clear(list);
  9533. }
  9534. NK_API void
  9535. nk_draw_list_path_stroke(struct nk_draw_list *list, struct nk_color color,
  9536. enum nk_draw_list_stroke closed, float thickness)
  9537. {
  9538. struct nk_vec2 *points;
  9539. NK_ASSERT(list);
  9540. if (!list) return;
  9541. points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
  9542. nk_draw_list_stroke_poly_line(list, points, list->path_count, color,
  9543. closed, thickness, list->config.line_AA);
  9544. nk_draw_list_path_clear(list);
  9545. }
  9546. NK_API void
  9547. nk_draw_list_stroke_line(struct nk_draw_list *list, struct nk_vec2 a,
  9548. struct nk_vec2 b, struct nk_color col, float thickness)
  9549. {
  9550. NK_ASSERT(list);
  9551. if (!list || !col.a) return;
  9552. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  9553. nk_draw_list_path_line_to(list, a);
  9554. nk_draw_list_path_line_to(list, b);
  9555. } else {
  9556. nk_draw_list_path_line_to(list, nk_vec2_sub(a,nk_vec2(0.5f,0.5f)));
  9557. nk_draw_list_path_line_to(list, nk_vec2_sub(b,nk_vec2(0.5f,0.5f)));
  9558. }
  9559. nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
  9560. }
  9561. NK_API void
  9562. nk_draw_list_fill_rect(struct nk_draw_list *list, struct nk_rect rect,
  9563. struct nk_color col, float rounding)
  9564. {
  9565. NK_ASSERT(list);
  9566. if (!list || !col.a) return;
  9567. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  9568. nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
  9569. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9570. } else {
  9571. nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
  9572. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9573. } nk_draw_list_path_fill(list, col);
  9574. }
  9575. NK_API void
  9576. nk_draw_list_stroke_rect(struct nk_draw_list *list, struct nk_rect rect,
  9577. struct nk_color col, float rounding, float thickness)
  9578. {
  9579. NK_ASSERT(list);
  9580. if (!list || !col.a) return;
  9581. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  9582. nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
  9583. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9584. } else {
  9585. nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
  9586. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9587. } nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  9588. }
  9589. NK_API void
  9590. nk_draw_list_fill_rect_multi_color(struct nk_draw_list *list, struct nk_rect rect,
  9591. struct nk_color left, struct nk_color top, struct nk_color right,
  9592. struct nk_color bottom)
  9593. {
  9594. void *vtx;
  9595. struct nk_colorf col_left, col_top;
  9596. struct nk_colorf col_right, col_bottom;
  9597. nk_draw_index *idx;
  9598. nk_draw_index index;
  9599. nk_color_fv(&col_left.r, left);
  9600. nk_color_fv(&col_right.r, right);
  9601. nk_color_fv(&col_top.r, top);
  9602. nk_color_fv(&col_bottom.r, bottom);
  9603. NK_ASSERT(list);
  9604. if (!list) return;
  9605. nk_draw_list_push_image(list, list->config.null.texture);
  9606. index = (nk_draw_index)list->vertex_count;
  9607. vtx = nk_draw_list_alloc_vertices(list, 4);
  9608. idx = nk_draw_list_alloc_elements(list, 6);
  9609. if (!vtx || !idx) return;
  9610. idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
  9611. idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
  9612. idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
  9613. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y), list->config.null.uv, col_left);
  9614. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y), list->config.null.uv, col_top);
  9615. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y + rect.h), list->config.null.uv, col_right);
  9616. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y + rect.h), list->config.null.uv, col_bottom);
  9617. }
  9618. NK_API void
  9619. nk_draw_list_fill_triangle(struct nk_draw_list *list, struct nk_vec2 a,
  9620. struct nk_vec2 b, struct nk_vec2 c, struct nk_color col)
  9621. {
  9622. NK_ASSERT(list);
  9623. if (!list || !col.a) return;
  9624. nk_draw_list_path_line_to(list, a);
  9625. nk_draw_list_path_line_to(list, b);
  9626. nk_draw_list_path_line_to(list, c);
  9627. nk_draw_list_path_fill(list, col);
  9628. }
  9629. NK_API void
  9630. nk_draw_list_stroke_triangle(struct nk_draw_list *list, struct nk_vec2 a,
  9631. struct nk_vec2 b, struct nk_vec2 c, struct nk_color col, float thickness)
  9632. {
  9633. NK_ASSERT(list);
  9634. if (!list || !col.a) return;
  9635. nk_draw_list_path_line_to(list, a);
  9636. nk_draw_list_path_line_to(list, b);
  9637. nk_draw_list_path_line_to(list, c);
  9638. nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  9639. }
  9640. NK_API void
  9641. nk_draw_list_fill_circle(struct nk_draw_list *list, struct nk_vec2 center,
  9642. float radius, struct nk_color col, unsigned int segs)
  9643. {
  9644. float a_max;
  9645. NK_ASSERT(list);
  9646. if (!list || !col.a) return;
  9647. a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
  9648. nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
  9649. nk_draw_list_path_fill(list, col);
  9650. }
  9651. NK_API void
  9652. nk_draw_list_stroke_circle(struct nk_draw_list *list, struct nk_vec2 center,
  9653. float radius, struct nk_color col, unsigned int segs, float thickness)
  9654. {
  9655. float a_max;
  9656. NK_ASSERT(list);
  9657. if (!list || !col.a) return;
  9658. a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
  9659. nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
  9660. nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  9661. }
  9662. NK_API void
  9663. nk_draw_list_stroke_curve(struct nk_draw_list *list, struct nk_vec2 p0,
  9664. struct nk_vec2 cp0, struct nk_vec2 cp1, struct nk_vec2 p1,
  9665. struct nk_color col, unsigned int segments, float thickness)
  9666. {
  9667. NK_ASSERT(list);
  9668. if (!list || !col.a) return;
  9669. nk_draw_list_path_line_to(list, p0);
  9670. nk_draw_list_path_curve_to(list, cp0, cp1, p1, segments);
  9671. nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
  9672. }
  9673. NK_INTERN void
  9674. nk_draw_list_push_rect_uv(struct nk_draw_list *list, struct nk_vec2 a,
  9675. struct nk_vec2 c, struct nk_vec2 uva, struct nk_vec2 uvc,
  9676. struct nk_color color)
  9677. {
  9678. void *vtx;
  9679. struct nk_vec2 uvb;
  9680. struct nk_vec2 uvd;
  9681. struct nk_vec2 b;
  9682. struct nk_vec2 d;
  9683. struct nk_colorf col;
  9684. nk_draw_index *idx;
  9685. nk_draw_index index;
  9686. NK_ASSERT(list);
  9687. if (!list) return;
  9688. nk_color_fv(&col.r, color);
  9689. uvb = nk_vec2(uvc.x, uva.y);
  9690. uvd = nk_vec2(uva.x, uvc.y);
  9691. b = nk_vec2(c.x, a.y);
  9692. d = nk_vec2(a.x, c.y);
  9693. index = (nk_draw_index)list->vertex_count;
  9694. vtx = nk_draw_list_alloc_vertices(list, 4);
  9695. idx = nk_draw_list_alloc_elements(list, 6);
  9696. if (!vtx || !idx) return;
  9697. idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
  9698. idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
  9699. idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
  9700. vtx = nk_draw_vertex(vtx, &list->config, a, uva, col);
  9701. vtx = nk_draw_vertex(vtx, &list->config, b, uvb, col);
  9702. vtx = nk_draw_vertex(vtx, &list->config, c, uvc, col);
  9703. vtx = nk_draw_vertex(vtx, &list->config, d, uvd, col);
  9704. }
  9705. NK_API void
  9706. nk_draw_list_add_image(struct nk_draw_list *list, struct nk_image texture,
  9707. struct nk_rect rect, struct nk_color color)
  9708. {
  9709. NK_ASSERT(list);
  9710. if (!list) return;
  9711. /* push new command with given texture */
  9712. nk_draw_list_push_image(list, texture.handle);
  9713. if (nk_image_is_subimage(&texture)) {
  9714. /* add region inside of the texture */
  9715. struct nk_vec2 uv[2];
  9716. uv[0].x = (float)texture.region[0]/(float)texture.w;
  9717. uv[0].y = (float)texture.region[1]/(float)texture.h;
  9718. uv[1].x = (float)(texture.region[0] + texture.region[2])/(float)texture.w;
  9719. uv[1].y = (float)(texture.region[1] + texture.region[3])/(float)texture.h;
  9720. nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
  9721. nk_vec2(rect.x + rect.w, rect.y + rect.h), uv[0], uv[1], color);
  9722. } else nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
  9723. nk_vec2(rect.x + rect.w, rect.y + rect.h),
  9724. nk_vec2(0.0f, 0.0f), nk_vec2(1.0f, 1.0f),color);
  9725. }
  9726. NK_API void
  9727. nk_draw_list_add_text(struct nk_draw_list *list, const struct nk_user_font *font,
  9728. struct nk_rect rect, const char *text, int len, float font_height,
  9729. struct nk_color fg)
  9730. {
  9731. float x = 0;
  9732. int text_len = 0;
  9733. nk_rune unicode = 0;
  9734. nk_rune next = 0;
  9735. int glyph_len = 0;
  9736. int next_glyph_len = 0;
  9737. struct nk_user_font_glyph g;
  9738. NK_ASSERT(list);
  9739. if (!list || !len || !text) return;
  9740. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  9741. list->clip_rect.x, list->clip_rect.y, list->clip_rect.w, list->clip_rect.h)) return;
  9742. nk_draw_list_push_image(list, font->texture);
  9743. x = rect.x;
  9744. glyph_len = nk_utf_decode(text, &unicode, len);
  9745. if (!glyph_len) return;
  9746. /* draw every glyph image */
  9747. fg.a = (nk_byte)((float)fg.a * list->config.global_alpha);
  9748. while (text_len < len && glyph_len) {
  9749. float gx, gy, gh, gw;
  9750. float char_width = 0;
  9751. if (unicode == NK_UTF_INVALID) break;
  9752. /* query currently drawn glyph information */
  9753. next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
  9754. font->query(font->userdata, font_height, &g, unicode,
  9755. (next == NK_UTF_INVALID) ? '\0' : next);
  9756. /* calculate and draw glyph drawing rectangle and image */
  9757. gx = x + g.offset.x;
  9758. gy = rect.y + g.offset.y;
  9759. gw = g.width; gh = g.height;
  9760. char_width = g.xadvance;
  9761. nk_draw_list_push_rect_uv(list, nk_vec2(gx,gy), nk_vec2(gx + gw, gy+ gh),
  9762. g.uv[0], g.uv[1], fg);
  9763. /* offset next glyph */
  9764. text_len += glyph_len;
  9765. x += char_width;
  9766. glyph_len = next_glyph_len;
  9767. unicode = next;
  9768. }
  9769. }
  9770. NK_API nk_flags
  9771. nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
  9772. struct nk_buffer *vertices, struct nk_buffer *elements,
  9773. const struct nk_convert_config *config)
  9774. {
  9775. nk_flags res = NK_CONVERT_SUCCESS;
  9776. const struct nk_command *cmd;
  9777. NK_ASSERT(ctx);
  9778. NK_ASSERT(cmds);
  9779. NK_ASSERT(vertices);
  9780. NK_ASSERT(elements);
  9781. NK_ASSERT(config);
  9782. NK_ASSERT(config->vertex_layout);
  9783. NK_ASSERT(config->vertex_size);
  9784. if (!ctx || !cmds || !vertices || !elements || !config || !config->vertex_layout)
  9785. return NK_CONVERT_INVALID_PARAM;
  9786. nk_draw_list_setup(&ctx->draw_list, config, cmds, vertices, elements,
  9787. config->line_AA, config->shape_AA);
  9788. nk_foreach(cmd, ctx)
  9789. {
  9790. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9791. ctx->draw_list.userdata = cmd->userdata;
  9792. #endif
  9793. switch (cmd->type) {
  9794. case NK_COMMAND_NOP: break;
  9795. case NK_COMMAND_SCISSOR: {
  9796. const struct nk_command_scissor *s = (const struct nk_command_scissor*)cmd;
  9797. nk_draw_list_add_clip(&ctx->draw_list, nk_rect(s->x, s->y, s->w, s->h));
  9798. } break;
  9799. case NK_COMMAND_LINE: {
  9800. const struct nk_command_line *l = (const struct nk_command_line*)cmd;
  9801. nk_draw_list_stroke_line(&ctx->draw_list, nk_vec2(l->begin.x, l->begin.y),
  9802. nk_vec2(l->end.x, l->end.y), l->color, l->line_thickness);
  9803. } break;
  9804. case NK_COMMAND_CURVE: {
  9805. const struct nk_command_curve *q = (const struct nk_command_curve*)cmd;
  9806. nk_draw_list_stroke_curve(&ctx->draw_list, nk_vec2(q->begin.x, q->begin.y),
  9807. nk_vec2(q->ctrl[0].x, q->ctrl[0].y), nk_vec2(q->ctrl[1].x,
  9808. q->ctrl[1].y), nk_vec2(q->end.x, q->end.y), q->color,
  9809. config->curve_segment_count, q->line_thickness);
  9810. } break;
  9811. case NK_COMMAND_RECT: {
  9812. const struct nk_command_rect *r = (const struct nk_command_rect*)cmd;
  9813. nk_draw_list_stroke_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  9814. r->color, (float)r->rounding, r->line_thickness);
  9815. } break;
  9816. case NK_COMMAND_RECT_FILLED: {
  9817. const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled*)cmd;
  9818. nk_draw_list_fill_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  9819. r->color, (float)r->rounding);
  9820. } break;
  9821. case NK_COMMAND_RECT_MULTI_COLOR: {
  9822. const struct nk_command_rect_multi_color *r = (const struct nk_command_rect_multi_color*)cmd;
  9823. nk_draw_list_fill_rect_multi_color(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  9824. r->left, r->top, r->right, r->bottom);
  9825. } break;
  9826. case NK_COMMAND_CIRCLE: {
  9827. const struct nk_command_circle *c = (const struct nk_command_circle*)cmd;
  9828. nk_draw_list_stroke_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
  9829. (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
  9830. config->circle_segment_count, c->line_thickness);
  9831. } break;
  9832. case NK_COMMAND_CIRCLE_FILLED: {
  9833. const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
  9834. nk_draw_list_fill_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
  9835. (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
  9836. config->circle_segment_count);
  9837. } break;
  9838. case NK_COMMAND_ARC: {
  9839. const struct nk_command_arc *c = (const struct nk_command_arc*)cmd;
  9840. nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
  9841. nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
  9842. c->a[0], c->a[1], config->arc_segment_count);
  9843. nk_draw_list_path_stroke(&ctx->draw_list, c->color, NK_STROKE_CLOSED, c->line_thickness);
  9844. } break;
  9845. case NK_COMMAND_ARC_FILLED: {
  9846. const struct nk_command_arc_filled *c = (const struct nk_command_arc_filled*)cmd;
  9847. nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
  9848. nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
  9849. c->a[0], c->a[1], config->arc_segment_count);
  9850. nk_draw_list_path_fill(&ctx->draw_list, c->color);
  9851. } break;
  9852. case NK_COMMAND_TRIANGLE: {
  9853. const struct nk_command_triangle *t = (const struct nk_command_triangle*)cmd;
  9854. nk_draw_list_stroke_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
  9855. nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color,
  9856. t->line_thickness);
  9857. } break;
  9858. case NK_COMMAND_TRIANGLE_FILLED: {
  9859. const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled*)cmd;
  9860. nk_draw_list_fill_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
  9861. nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color);
  9862. } break;
  9863. case NK_COMMAND_POLYGON: {
  9864. int i;
  9865. const struct nk_command_polygon*p = (const struct nk_command_polygon*)cmd;
  9866. for (i = 0; i < p->point_count; ++i) {
  9867. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  9868. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  9869. }
  9870. nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_CLOSED, p->line_thickness);
  9871. } break;
  9872. case NK_COMMAND_POLYGON_FILLED: {
  9873. int i;
  9874. const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled*)cmd;
  9875. for (i = 0; i < p->point_count; ++i) {
  9876. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  9877. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  9878. }
  9879. nk_draw_list_path_fill(&ctx->draw_list, p->color);
  9880. } break;
  9881. case NK_COMMAND_POLYLINE: {
  9882. int i;
  9883. const struct nk_command_polyline *p = (const struct nk_command_polyline*)cmd;
  9884. for (i = 0; i < p->point_count; ++i) {
  9885. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  9886. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  9887. }
  9888. nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_OPEN, p->line_thickness);
  9889. } break;
  9890. case NK_COMMAND_TEXT: {
  9891. const struct nk_command_text *t = (const struct nk_command_text*)cmd;
  9892. nk_draw_list_add_text(&ctx->draw_list, t->font, nk_rect(t->x, t->y, t->w, t->h),
  9893. t->string, t->length, t->height, t->foreground);
  9894. } break;
  9895. case NK_COMMAND_IMAGE: {
  9896. const struct nk_command_image *i = (const struct nk_command_image*)cmd;
  9897. nk_draw_list_add_image(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
  9898. } break;
  9899. case NK_COMMAND_CUSTOM: {
  9900. const struct nk_command_custom *c = (const struct nk_command_custom*)cmd;
  9901. c->callback(&ctx->draw_list, c->x, c->y, c->w, c->h, c->callback_data);
  9902. } break;
  9903. default: break;
  9904. }
  9905. }
  9906. res |= (cmds->needed > cmds->allocated + (cmds->memory.size - cmds->size)) ? NK_CONVERT_COMMAND_BUFFER_FULL: 0;
  9907. res |= (vertices->needed > vertices->allocated) ? NK_CONVERT_VERTEX_BUFFER_FULL: 0;
  9908. res |= (elements->needed > elements->allocated) ? NK_CONVERT_ELEMENT_BUFFER_FULL: 0;
  9909. return res;
  9910. }
  9911. NK_API const struct nk_draw_command*
  9912. nk__draw_begin(const struct nk_context *ctx,
  9913. const struct nk_buffer *buffer)
  9914. {
  9915. return nk__draw_list_begin(&ctx->draw_list, buffer);
  9916. }
  9917. NK_API const struct nk_draw_command*
  9918. nk__draw_end(const struct nk_context *ctx, const struct nk_buffer *buffer)
  9919. {
  9920. return nk__draw_list_end(&ctx->draw_list, buffer);
  9921. }
  9922. NK_API const struct nk_draw_command*
  9923. nk__draw_next(const struct nk_draw_command *cmd,
  9924. const struct nk_buffer *buffer, const struct nk_context *ctx)
  9925. {
  9926. return nk__draw_list_next(cmd, buffer, &ctx->draw_list);
  9927. }
  9928. #endif
  9929. #ifdef NK_INCLUDE_FONT_BAKING
  9930. /* -------------------------------------------------------------
  9931. *
  9932. * RECT PACK
  9933. *
  9934. * --------------------------------------------------------------*/
  9935. /* stb_rect_pack.h - v0.05 - public domain - rectangle packing */
  9936. /* Sean Barrett 2014 */
  9937. #define NK_RP__MAXVAL 0xffff
  9938. typedef unsigned short nk_rp_coord;
  9939. struct nk_rp_rect {
  9940. /* reserved for your use: */
  9941. int id;
  9942. /* input: */
  9943. nk_rp_coord w, h;
  9944. /* output: */
  9945. nk_rp_coord x, y;
  9946. int was_packed;
  9947. /* non-zero if valid packing */
  9948. }; /* 16 bytes, nominally */
  9949. struct nk_rp_node {
  9950. nk_rp_coord x,y;
  9951. struct nk_rp_node *next;
  9952. };
  9953. struct nk_rp_context {
  9954. int width;
  9955. int height;
  9956. int align;
  9957. int init_mode;
  9958. int heuristic;
  9959. int num_nodes;
  9960. struct nk_rp_node *active_head;
  9961. struct nk_rp_node *free_head;
  9962. struct nk_rp_node extra[2];
  9963. /* we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2' */
  9964. };
  9965. struct nk_rp__findresult {
  9966. int x,y;
  9967. struct nk_rp_node **prev_link;
  9968. };
  9969. enum NK_RP_HEURISTIC {
  9970. NK_RP_HEURISTIC_Skyline_default=0,
  9971. NK_RP_HEURISTIC_Skyline_BL_sortHeight = NK_RP_HEURISTIC_Skyline_default,
  9972. NK_RP_HEURISTIC_Skyline_BF_sortHeight
  9973. };
  9974. enum NK_RP_INIT_STATE{NK_RP__INIT_skyline = 1};
  9975. NK_INTERN void
  9976. nk_rp_setup_allow_out_of_mem(struct nk_rp_context *context, int allow_out_of_mem)
  9977. {
  9978. if (allow_out_of_mem)
  9979. /* if it's ok to run out of memory, then don't bother aligning them; */
  9980. /* this gives better packing, but may fail due to OOM (even though */
  9981. /* the rectangles easily fit). @TODO a smarter approach would be to only */
  9982. /* quantize once we've hit OOM, then we could get rid of this parameter. */
  9983. context->align = 1;
  9984. else {
  9985. /* if it's not ok to run out of memory, then quantize the widths */
  9986. /* so that num_nodes is always enough nodes. */
  9987. /* */
  9988. /* I.e. num_nodes * align >= width */
  9989. /* align >= width / num_nodes */
  9990. /* align = ceil(width/num_nodes) */
  9991. context->align = (context->width + context->num_nodes-1) / context->num_nodes;
  9992. }
  9993. }
  9994. NK_INTERN void
  9995. nk_rp_init_target(struct nk_rp_context *context, int width, int height,
  9996. struct nk_rp_node *nodes, int num_nodes)
  9997. {
  9998. int i;
  9999. #ifndef STBRP_LARGE_RECTS
  10000. NK_ASSERT(width <= 0xffff && height <= 0xffff);
  10001. #endif
  10002. for (i=0; i < num_nodes-1; ++i)
  10003. nodes[i].next = &nodes[i+1];
  10004. nodes[i].next = 0;
  10005. context->init_mode = NK_RP__INIT_skyline;
  10006. context->heuristic = NK_RP_HEURISTIC_Skyline_default;
  10007. context->free_head = &nodes[0];
  10008. context->active_head = &context->extra[0];
  10009. context->width = width;
  10010. context->height = height;
  10011. context->num_nodes = num_nodes;
  10012. nk_rp_setup_allow_out_of_mem(context, 0);
  10013. /* node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly) */
  10014. context->extra[0].x = 0;
  10015. context->extra[0].y = 0;
  10016. context->extra[0].next = &context->extra[1];
  10017. context->extra[1].x = (nk_rp_coord) width;
  10018. context->extra[1].y = 65535;
  10019. context->extra[1].next = 0;
  10020. }
  10021. /* find minimum y position if it starts at x1 */
  10022. NK_INTERN int
  10023. nk_rp__skyline_find_min_y(struct nk_rp_context *c, struct nk_rp_node *first,
  10024. int x0, int width, int *pwaste)
  10025. {
  10026. struct nk_rp_node *node = first;
  10027. int x1 = x0 + width;
  10028. int min_y, visited_width, waste_area;
  10029. NK_ASSERT(first->x <= x0);
  10030. NK_UNUSED(c);
  10031. NK_ASSERT(node->next->x > x0);
  10032. /* we ended up handling this in the caller for efficiency */
  10033. NK_ASSERT(node->x <= x0);
  10034. min_y = 0;
  10035. waste_area = 0;
  10036. visited_width = 0;
  10037. while (node->x < x1)
  10038. {
  10039. if (node->y > min_y) {
  10040. /* raise min_y higher. */
  10041. /* we've accounted for all waste up to min_y, */
  10042. /* but we'll now add more waste for everything we've visited */
  10043. waste_area += visited_width * (node->y - min_y);
  10044. min_y = node->y;
  10045. /* the first time through, visited_width might be reduced */
  10046. if (node->x < x0)
  10047. visited_width += node->next->x - x0;
  10048. else
  10049. visited_width += node->next->x - node->x;
  10050. } else {
  10051. /* add waste area */
  10052. int under_width = node->next->x - node->x;
  10053. if (under_width + visited_width > width)
  10054. under_width = width - visited_width;
  10055. waste_area += under_width * (min_y - node->y);
  10056. visited_width += under_width;
  10057. }
  10058. node = node->next;
  10059. }
  10060. *pwaste = waste_area;
  10061. return min_y;
  10062. }
  10063. NK_INTERN struct nk_rp__findresult
  10064. nk_rp__skyline_find_best_pos(struct nk_rp_context *c, int width, int height)
  10065. {
  10066. int best_waste = (1<<30), best_x, best_y = (1 << 30);
  10067. struct nk_rp__findresult fr;
  10068. struct nk_rp_node **prev, *node, *tail, **best = 0;
  10069. /* align to multiple of c->align */
  10070. width = (width + c->align - 1);
  10071. width -= width % c->align;
  10072. NK_ASSERT(width % c->align == 0);
  10073. node = c->active_head;
  10074. prev = &c->active_head;
  10075. while (node->x + width <= c->width) {
  10076. int y,waste;
  10077. y = nk_rp__skyline_find_min_y(c, node, node->x, width, &waste);
  10078. /* actually just want to test BL */
  10079. if (c->heuristic == NK_RP_HEURISTIC_Skyline_BL_sortHeight) {
  10080. /* bottom left */
  10081. if (y < best_y) {
  10082. best_y = y;
  10083. best = prev;
  10084. }
  10085. } else {
  10086. /* best-fit */
  10087. if (y + height <= c->height) {
  10088. /* can only use it if it first vertically */
  10089. if (y < best_y || (y == best_y && waste < best_waste)) {
  10090. best_y = y;
  10091. best_waste = waste;
  10092. best = prev;
  10093. }
  10094. }
  10095. }
  10096. prev = &node->next;
  10097. node = node->next;
  10098. }
  10099. best_x = (best == 0) ? 0 : (*best)->x;
  10100. /* if doing best-fit (BF), we also have to try aligning right edge to each node position */
  10101. /* */
  10102. /* e.g, if fitting */
  10103. /* */
  10104. /* ____________________ */
  10105. /* |____________________| */
  10106. /* */
  10107. /* into */
  10108. /* */
  10109. /* | | */
  10110. /* | ____________| */
  10111. /* |____________| */
  10112. /* */
  10113. /* then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned */
  10114. /* */
  10115. /* This makes BF take about 2x the time */
  10116. if (c->heuristic == NK_RP_HEURISTIC_Skyline_BF_sortHeight)
  10117. {
  10118. tail = c->active_head;
  10119. node = c->active_head;
  10120. prev = &c->active_head;
  10121. /* find first node that's admissible */
  10122. while (tail->x < width)
  10123. tail = tail->next;
  10124. while (tail)
  10125. {
  10126. int xpos = tail->x - width;
  10127. int y,waste;
  10128. NK_ASSERT(xpos >= 0);
  10129. /* find the left position that matches this */
  10130. while (node->next->x <= xpos) {
  10131. prev = &node->next;
  10132. node = node->next;
  10133. }
  10134. NK_ASSERT(node->next->x > xpos && node->x <= xpos);
  10135. y = nk_rp__skyline_find_min_y(c, node, xpos, width, &waste);
  10136. if (y + height < c->height) {
  10137. if (y <= best_y) {
  10138. if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
  10139. best_x = xpos;
  10140. NK_ASSERT(y <= best_y);
  10141. best_y = y;
  10142. best_waste = waste;
  10143. best = prev;
  10144. }
  10145. }
  10146. }
  10147. tail = tail->next;
  10148. }
  10149. }
  10150. fr.prev_link = best;
  10151. fr.x = best_x;
  10152. fr.y = best_y;
  10153. return fr;
  10154. }
  10155. NK_INTERN struct nk_rp__findresult
  10156. nk_rp__skyline_pack_rectangle(struct nk_rp_context *context, int width, int height)
  10157. {
  10158. /* find best position according to heuristic */
  10159. struct nk_rp__findresult res = nk_rp__skyline_find_best_pos(context, width, height);
  10160. struct nk_rp_node *node, *cur;
  10161. /* bail if: */
  10162. /* 1. it failed */
  10163. /* 2. the best node doesn't fit (we don't always check this) */
  10164. /* 3. we're out of memory */
  10165. if (res.prev_link == 0 || res.y + height > context->height || context->free_head == 0) {
  10166. res.prev_link = 0;
  10167. return res;
  10168. }
  10169. /* on success, create new node */
  10170. node = context->free_head;
  10171. node->x = (nk_rp_coord) res.x;
  10172. node->y = (nk_rp_coord) (res.y + height);
  10173. context->free_head = node->next;
  10174. /* insert the new node into the right starting point, and */
  10175. /* let 'cur' point to the remaining nodes needing to be */
  10176. /* stitched back in */
  10177. cur = *res.prev_link;
  10178. if (cur->x < res.x) {
  10179. /* preserve the existing one, so start testing with the next one */
  10180. struct nk_rp_node *next = cur->next;
  10181. cur->next = node;
  10182. cur = next;
  10183. } else {
  10184. *res.prev_link = node;
  10185. }
  10186. /* from here, traverse cur and free the nodes, until we get to one */
  10187. /* that shouldn't be freed */
  10188. while (cur->next && cur->next->x <= res.x + width) {
  10189. struct nk_rp_node *next = cur->next;
  10190. /* move the current node to the free list */
  10191. cur->next = context->free_head;
  10192. context->free_head = cur;
  10193. cur = next;
  10194. }
  10195. /* stitch the list back in */
  10196. node->next = cur;
  10197. if (cur->x < res.x + width)
  10198. cur->x = (nk_rp_coord) (res.x + width);
  10199. return res;
  10200. }
  10201. NK_INTERN int
  10202. nk_rect_height_compare(const void *a, const void *b)
  10203. {
  10204. const struct nk_rp_rect *p = (const struct nk_rp_rect *) a;
  10205. const struct nk_rp_rect *q = (const struct nk_rp_rect *) b;
  10206. if (p->h > q->h)
  10207. return -1;
  10208. if (p->h < q->h)
  10209. return 1;
  10210. return (p->w > q->w) ? -1 : (p->w < q->w);
  10211. }
  10212. NK_INTERN int
  10213. nk_rect_original_order(const void *a, const void *b)
  10214. {
  10215. const struct nk_rp_rect *p = (const struct nk_rp_rect *) a;
  10216. const struct nk_rp_rect *q = (const struct nk_rp_rect *) b;
  10217. return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
  10218. }
  10219. NK_INTERN void
  10220. nk_rp_qsort(struct nk_rp_rect *array, unsigned int len, int(*cmp)(const void*,const void*))
  10221. {
  10222. /* iterative quick sort */
  10223. #define NK_MAX_SORT_STACK 64
  10224. unsigned right, left = 0, stack[NK_MAX_SORT_STACK], pos = 0;
  10225. unsigned seed = len/2 * 69069+1;
  10226. for (;;) {
  10227. for (; left+1 < len; len++) {
  10228. struct nk_rp_rect pivot, tmp;
  10229. if (pos == NK_MAX_SORT_STACK) len = stack[pos = 0];
  10230. pivot = array[left+seed%(len-left)];
  10231. seed = seed * 69069 + 1;
  10232. stack[pos++] = len;
  10233. for (right = left-1;;) {
  10234. while (cmp(&array[++right], &pivot) < 0);
  10235. while (cmp(&pivot, &array[--len]) < 0);
  10236. if (right >= len) break;
  10237. tmp = array[right];
  10238. array[right] = array[len];
  10239. array[len] = tmp;
  10240. }
  10241. }
  10242. if (pos == 0) break;
  10243. left = len;
  10244. len = stack[--pos];
  10245. }
  10246. #undef NK_MAX_SORT_STACK
  10247. }
  10248. NK_INTERN void
  10249. nk_rp_pack_rects(struct nk_rp_context *context, struct nk_rp_rect *rects, int num_rects)
  10250. {
  10251. int i;
  10252. /* we use the 'was_packed' field internally to allow sorting/unsorting */
  10253. for (i=0; i < num_rects; ++i) {
  10254. rects[i].was_packed = i;
  10255. }
  10256. /* sort according to heuristic */
  10257. nk_rp_qsort(rects, (unsigned)num_rects, nk_rect_height_compare);
  10258. for (i=0; i < num_rects; ++i) {
  10259. struct nk_rp__findresult fr = nk_rp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
  10260. if (fr.prev_link) {
  10261. rects[i].x = (nk_rp_coord) fr.x;
  10262. rects[i].y = (nk_rp_coord) fr.y;
  10263. } else {
  10264. rects[i].x = rects[i].y = NK_RP__MAXVAL;
  10265. }
  10266. }
  10267. /* unsort */
  10268. nk_rp_qsort(rects, (unsigned)num_rects, nk_rect_original_order);
  10269. /* set was_packed flags */
  10270. for (i=0; i < num_rects; ++i)
  10271. rects[i].was_packed = !(rects[i].x == NK_RP__MAXVAL && rects[i].y == NK_RP__MAXVAL);
  10272. }
  10273. /*
  10274. * ==============================================================
  10275. *
  10276. * TRUETYPE
  10277. *
  10278. * ===============================================================
  10279. */
  10280. /* stb_truetype.h - v1.07 - public domain */
  10281. #define NK_TT_MAX_OVERSAMPLE 8
  10282. #define NK_TT__OVER_MASK (NK_TT_MAX_OVERSAMPLE-1)
  10283. struct nk_tt_bakedchar {
  10284. unsigned short x0,y0,x1,y1;
  10285. /* coordinates of bbox in bitmap */
  10286. float xoff,yoff,xadvance;
  10287. };
  10288. struct nk_tt_aligned_quad{
  10289. float x0,y0,s0,t0; /* top-left */
  10290. float x1,y1,s1,t1; /* bottom-right */
  10291. };
  10292. struct nk_tt_packedchar {
  10293. unsigned short x0,y0,x1,y1;
  10294. /* coordinates of bbox in bitmap */
  10295. float xoff,yoff,xadvance;
  10296. float xoff2,yoff2;
  10297. };
  10298. struct nk_tt_pack_range {
  10299. float font_size;
  10300. int first_unicode_codepoint_in_range;
  10301. /* if non-zero, then the chars are continuous, and this is the first codepoint */
  10302. int *array_of_unicode_codepoints;
  10303. /* if non-zero, then this is an array of unicode codepoints */
  10304. int num_chars;
  10305. struct nk_tt_packedchar *chardata_for_range; /* output */
  10306. unsigned char h_oversample, v_oversample;
  10307. /* don't set these, they're used internally */
  10308. };
  10309. struct nk_tt_pack_context {
  10310. void *pack_info;
  10311. int width;
  10312. int height;
  10313. int stride_in_bytes;
  10314. int padding;
  10315. unsigned int h_oversample, v_oversample;
  10316. unsigned char *pixels;
  10317. void *nodes;
  10318. };
  10319. struct nk_tt_fontinfo {
  10320. const unsigned char* data; /* pointer to .ttf file */
  10321. int fontstart;/* offset of start of font */
  10322. int numGlyphs;/* number of glyphs, needed for range checking */
  10323. int loca,head,glyf,hhea,hmtx,kern; /* table locations as offset from start of .ttf */
  10324. int index_map; /* a cmap mapping for our chosen character encoding */
  10325. int indexToLocFormat; /* format needed to map from glyph index to glyph */
  10326. };
  10327. enum {
  10328. NK_TT_vmove=1,
  10329. NK_TT_vline,
  10330. NK_TT_vcurve
  10331. };
  10332. struct nk_tt_vertex {
  10333. short x,y,cx,cy;
  10334. unsigned char type,padding;
  10335. };
  10336. struct nk_tt__bitmap{
  10337. int w,h,stride;
  10338. unsigned char *pixels;
  10339. };
  10340. struct nk_tt__hheap_chunk {
  10341. struct nk_tt__hheap_chunk *next;
  10342. };
  10343. struct nk_tt__hheap {
  10344. struct nk_allocator alloc;
  10345. struct nk_tt__hheap_chunk *head;
  10346. void *first_free;
  10347. int num_remaining_in_head_chunk;
  10348. };
  10349. struct nk_tt__edge {
  10350. float x0,y0, x1,y1;
  10351. int invert;
  10352. };
  10353. struct nk_tt__active_edge {
  10354. struct nk_tt__active_edge *next;
  10355. float fx,fdx,fdy;
  10356. float direction;
  10357. float sy;
  10358. float ey;
  10359. };
  10360. struct nk_tt__point {float x,y;};
  10361. #define NK_TT_MACSTYLE_DONTCARE 0
  10362. #define NK_TT_MACSTYLE_BOLD 1
  10363. #define NK_TT_MACSTYLE_ITALIC 2
  10364. #define NK_TT_MACSTYLE_UNDERSCORE 4
  10365. #define NK_TT_MACSTYLE_NONE 8
  10366. /* <= not same as 0, this makes us check the bitfield is 0 */
  10367. enum { /* platformID */
  10368. NK_TT_PLATFORM_ID_UNICODE =0,
  10369. NK_TT_PLATFORM_ID_MAC =1,
  10370. NK_TT_PLATFORM_ID_ISO =2,
  10371. NK_TT_PLATFORM_ID_MICROSOFT =3
  10372. };
  10373. enum { /* encodingID for NK_TT_PLATFORM_ID_UNICODE */
  10374. NK_TT_UNICODE_EID_UNICODE_1_0 =0,
  10375. NK_TT_UNICODE_EID_UNICODE_1_1 =1,
  10376. NK_TT_UNICODE_EID_ISO_10646 =2,
  10377. NK_TT_UNICODE_EID_UNICODE_2_0_BMP=3,
  10378. NK_TT_UNICODE_EID_UNICODE_2_0_FULL=4
  10379. };
  10380. enum { /* encodingID for NK_TT_PLATFORM_ID_MICROSOFT */
  10381. NK_TT_MS_EID_SYMBOL =0,
  10382. NK_TT_MS_EID_UNICODE_BMP =1,
  10383. NK_TT_MS_EID_SHIFTJIS =2,
  10384. NK_TT_MS_EID_UNICODE_FULL =10
  10385. };
  10386. enum { /* encodingID for NK_TT_PLATFORM_ID_MAC; same as Script Manager codes */
  10387. NK_TT_MAC_EID_ROMAN =0, NK_TT_MAC_EID_ARABIC =4,
  10388. NK_TT_MAC_EID_JAPANESE =1, NK_TT_MAC_EID_HEBREW =5,
  10389. NK_TT_MAC_EID_CHINESE_TRAD =2, NK_TT_MAC_EID_GREEK =6,
  10390. NK_TT_MAC_EID_KOREAN =3, NK_TT_MAC_EID_RUSSIAN =7
  10391. };
  10392. enum { /* languageID for NK_TT_PLATFORM_ID_MICROSOFT; same as LCID... */
  10393. /* problematic because there are e.g. 16 english LCIDs and 16 arabic LCIDs */
  10394. NK_TT_MS_LANG_ENGLISH =0x0409, NK_TT_MS_LANG_ITALIAN =0x0410,
  10395. NK_TT_MS_LANG_CHINESE =0x0804, NK_TT_MS_LANG_JAPANESE =0x0411,
  10396. NK_TT_MS_LANG_DUTCH =0x0413, NK_TT_MS_LANG_KOREAN =0x0412,
  10397. NK_TT_MS_LANG_FRENCH =0x040c, NK_TT_MS_LANG_RUSSIAN =0x0419,
  10398. NK_TT_MS_LANG_GERMAN =0x0407, NK_TT_MS_LANG_SPANISH =0x0409,
  10399. NK_TT_MS_LANG_HEBREW =0x040d, NK_TT_MS_LANG_SWEDISH =0x041D
  10400. };
  10401. enum { /* languageID for NK_TT_PLATFORM_ID_MAC */
  10402. NK_TT_MAC_LANG_ENGLISH =0 , NK_TT_MAC_LANG_JAPANESE =11,
  10403. NK_TT_MAC_LANG_ARABIC =12, NK_TT_MAC_LANG_KOREAN =23,
  10404. NK_TT_MAC_LANG_DUTCH =4 , NK_TT_MAC_LANG_RUSSIAN =32,
  10405. NK_TT_MAC_LANG_FRENCH =1 , NK_TT_MAC_LANG_SPANISH =6 ,
  10406. NK_TT_MAC_LANG_GERMAN =2 , NK_TT_MAC_LANG_SWEDISH =5 ,
  10407. NK_TT_MAC_LANG_HEBREW =10, NK_TT_MAC_LANG_CHINESE_SIMPLIFIED =33,
  10408. NK_TT_MAC_LANG_ITALIAN =3 , NK_TT_MAC_LANG_CHINESE_TRAD =19
  10409. };
  10410. #define nk_ttBYTE(p) (* (const nk_byte *) (p))
  10411. #define nk_ttCHAR(p) (* (const char *) (p))
  10412. #if defined(NK_BIGENDIAN) && !defined(NK_ALLOW_UNALIGNED_TRUETYPE)
  10413. #define nk_ttUSHORT(p) (* (nk_ushort *) (p))
  10414. #define nk_ttSHORT(p) (* (nk_short *) (p))
  10415. #define nk_ttULONG(p) (* (nk_uint *) (p))
  10416. #define nk_ttLONG(p) (* (nk_int *) (p))
  10417. #else
  10418. static nk_ushort nk_ttUSHORT(const nk_byte *p) { return (nk_ushort)(p[0]*256 + p[1]); }
  10419. static nk_short nk_ttSHORT(const nk_byte *p) { return (nk_short)(p[0]*256 + p[1]); }
  10420. static nk_uint nk_ttULONG(const nk_byte *p) { return (nk_uint)((p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]); }
  10421. #endif
  10422. #define nk_tt_tag4(p,c0,c1,c2,c3)\
  10423. ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
  10424. #define nk_tt_tag(p,str) nk_tt_tag4(p,str[0],str[1],str[2],str[3])
  10425. NK_INTERN int nk_tt_GetGlyphShape(const struct nk_tt_fontinfo *info, struct nk_allocator *alloc,
  10426. int glyph_index, struct nk_tt_vertex **pvertices);
  10427. NK_INTERN nk_uint
  10428. nk_tt__find_table(const nk_byte *data, nk_uint fontstart, const char *tag)
  10429. {
  10430. /* @OPTIMIZE: binary search */
  10431. nk_int num_tables = nk_ttUSHORT(data+fontstart+4);
  10432. nk_uint tabledir = fontstart + 12;
  10433. nk_int i;
  10434. for (i = 0; i < num_tables; ++i) {
  10435. nk_uint loc = tabledir + (nk_uint)(16*i);
  10436. if (nk_tt_tag(data+loc+0, tag))
  10437. return nk_ttULONG(data+loc+8);
  10438. }
  10439. return 0;
  10440. }
  10441. NK_INTERN int
  10442. nk_tt_InitFont(struct nk_tt_fontinfo *info, const unsigned char *data2, int fontstart)
  10443. {
  10444. nk_uint cmap, t;
  10445. nk_int i,numTables;
  10446. const nk_byte *data = (const nk_byte *) data2;
  10447. info->data = data;
  10448. info->fontstart = fontstart;
  10449. cmap = nk_tt__find_table(data, (nk_uint)fontstart, "cmap"); /* required */
  10450. info->loca = (int)nk_tt__find_table(data, (nk_uint)fontstart, "loca"); /* required */
  10451. info->head = (int)nk_tt__find_table(data, (nk_uint)fontstart, "head"); /* required */
  10452. info->glyf = (int)nk_tt__find_table(data, (nk_uint)fontstart, "glyf"); /* required */
  10453. info->hhea = (int)nk_tt__find_table(data, (nk_uint)fontstart, "hhea"); /* required */
  10454. info->hmtx = (int)nk_tt__find_table(data, (nk_uint)fontstart, "hmtx"); /* required */
  10455. info->kern = (int)nk_tt__find_table(data, (nk_uint)fontstart, "kern"); /* not required */
  10456. if (!cmap || !info->loca || !info->head || !info->glyf || !info->hhea || !info->hmtx)
  10457. return 0;
  10458. t = nk_tt__find_table(data, (nk_uint)fontstart, "maxp");
  10459. if (t) info->numGlyphs = nk_ttUSHORT(data+t+4);
  10460. else info->numGlyphs = 0xffff;
  10461. /* find a cmap encoding table we understand *now* to avoid searching */
  10462. /* later. (todo: could make this installable) */
  10463. /* the same regardless of glyph. */
  10464. numTables = nk_ttUSHORT(data + cmap + 2);
  10465. info->index_map = 0;
  10466. for (i=0; i < numTables; ++i)
  10467. {
  10468. nk_uint encoding_record = cmap + 4 + 8 * (nk_uint)i;
  10469. /* find an encoding we understand: */
  10470. switch(nk_ttUSHORT(data+encoding_record)) {
  10471. case NK_TT_PLATFORM_ID_MICROSOFT:
  10472. switch (nk_ttUSHORT(data+encoding_record+2)) {
  10473. case NK_TT_MS_EID_UNICODE_BMP:
  10474. case NK_TT_MS_EID_UNICODE_FULL:
  10475. /* MS/Unicode */
  10476. info->index_map = (int)(cmap + nk_ttULONG(data+encoding_record+4));
  10477. break;
  10478. default: break;
  10479. } break;
  10480. case NK_TT_PLATFORM_ID_UNICODE:
  10481. /* Mac/iOS has these */
  10482. /* all the encodingIDs are unicode, so we don't bother to check it */
  10483. info->index_map = (int)(cmap + nk_ttULONG(data+encoding_record+4));
  10484. break;
  10485. default: break;
  10486. }
  10487. }
  10488. if (info->index_map == 0)
  10489. return 0;
  10490. info->indexToLocFormat = nk_ttUSHORT(data+info->head + 50);
  10491. return 1;
  10492. }
  10493. NK_INTERN int
  10494. nk_tt_FindGlyphIndex(const struct nk_tt_fontinfo *info, int unicode_codepoint)
  10495. {
  10496. const nk_byte *data = info->data;
  10497. nk_uint index_map = (nk_uint)info->index_map;
  10498. nk_ushort format = nk_ttUSHORT(data + index_map + 0);
  10499. if (format == 0) { /* apple byte encoding */
  10500. nk_int bytes = nk_ttUSHORT(data + index_map + 2);
  10501. if (unicode_codepoint < bytes-6)
  10502. return nk_ttBYTE(data + index_map + 6 + unicode_codepoint);
  10503. return 0;
  10504. } else if (format == 6) {
  10505. nk_uint first = nk_ttUSHORT(data + index_map + 6);
  10506. nk_uint count = nk_ttUSHORT(data + index_map + 8);
  10507. if ((nk_uint) unicode_codepoint >= first && (nk_uint) unicode_codepoint < first+count)
  10508. return nk_ttUSHORT(data + index_map + 10 + (unicode_codepoint - (int)first)*2);
  10509. return 0;
  10510. } else if (format == 2) {
  10511. NK_ASSERT(0); /* @TODO: high-byte mapping for japanese/chinese/korean */
  10512. return 0;
  10513. } else if (format == 4) { /* standard mapping for windows fonts: binary search collection of ranges */
  10514. nk_ushort segcount = nk_ttUSHORT(data+index_map+6) >> 1;
  10515. nk_ushort searchRange = nk_ttUSHORT(data+index_map+8) >> 1;
  10516. nk_ushort entrySelector = nk_ttUSHORT(data+index_map+10);
  10517. nk_ushort rangeShift = nk_ttUSHORT(data+index_map+12) >> 1;
  10518. /* do a binary search of the segments */
  10519. nk_uint endCount = index_map + 14;
  10520. nk_uint search = endCount;
  10521. if (unicode_codepoint > 0xffff)
  10522. return 0;
  10523. /* they lie from endCount .. endCount + segCount */
  10524. /* but searchRange is the nearest power of two, so... */
  10525. if (unicode_codepoint >= nk_ttUSHORT(data + search + rangeShift*2))
  10526. search += (nk_uint)(rangeShift*2);
  10527. /* now decrement to bias correctly to find smallest */
  10528. search -= 2;
  10529. while (entrySelector) {
  10530. nk_ushort end;
  10531. searchRange >>= 1;
  10532. end = nk_ttUSHORT(data + search + searchRange*2);
  10533. if (unicode_codepoint > end)
  10534. search += (nk_uint)(searchRange*2);
  10535. --entrySelector;
  10536. }
  10537. search += 2;
  10538. {
  10539. nk_ushort offset, start;
  10540. nk_ushort item = (nk_ushort) ((search - endCount) >> 1);
  10541. NK_ASSERT(unicode_codepoint <= nk_ttUSHORT(data + endCount + 2*item));
  10542. start = nk_ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);
  10543. if (unicode_codepoint < start)
  10544. return 0;
  10545. offset = nk_ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);
  10546. if (offset == 0)
  10547. return (nk_ushort) (unicode_codepoint + nk_ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));
  10548. return nk_ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
  10549. }
  10550. } else if (format == 12 || format == 13) {
  10551. nk_uint ngroups = nk_ttULONG(data+index_map+12);
  10552. nk_int low,high;
  10553. low = 0; high = (nk_int)ngroups;
  10554. /* Binary search the right group. */
  10555. while (low < high) {
  10556. nk_int mid = low + ((high-low) >> 1); /* rounds down, so low <= mid < high */
  10557. nk_uint start_char = nk_ttULONG(data+index_map+16+mid*12);
  10558. nk_uint end_char = nk_ttULONG(data+index_map+16+mid*12+4);
  10559. if ((nk_uint) unicode_codepoint < start_char)
  10560. high = mid;
  10561. else if ((nk_uint) unicode_codepoint > end_char)
  10562. low = mid+1;
  10563. else {
  10564. nk_uint start_glyph = nk_ttULONG(data+index_map+16+mid*12+8);
  10565. if (format == 12)
  10566. return (int)start_glyph + (int)unicode_codepoint - (int)start_char;
  10567. else /* format == 13 */
  10568. return (int)start_glyph;
  10569. }
  10570. }
  10571. return 0; /* not found */
  10572. }
  10573. /* @TODO */
  10574. NK_ASSERT(0);
  10575. return 0;
  10576. }
  10577. NK_INTERN void
  10578. nk_tt_setvertex(struct nk_tt_vertex *v, nk_byte type, nk_int x, nk_int y, nk_int cx, nk_int cy)
  10579. {
  10580. v->type = type;
  10581. v->x = (nk_short) x;
  10582. v->y = (nk_short) y;
  10583. v->cx = (nk_short) cx;
  10584. v->cy = (nk_short) cy;
  10585. }
  10586. NK_INTERN int
  10587. nk_tt__GetGlyfOffset(const struct nk_tt_fontinfo *info, int glyph_index)
  10588. {
  10589. int g1,g2;
  10590. if (glyph_index >= info->numGlyphs) return -1; /* glyph index out of range */
  10591. if (info->indexToLocFormat >= 2) return -1; /* unknown index->glyph map format */
  10592. if (info->indexToLocFormat == 0) {
  10593. g1 = info->glyf + nk_ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;
  10594. g2 = info->glyf + nk_ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;
  10595. } else {
  10596. g1 = info->glyf + (int)nk_ttULONG (info->data + info->loca + glyph_index * 4);
  10597. g2 = info->glyf + (int)nk_ttULONG (info->data + info->loca + glyph_index * 4 + 4);
  10598. }
  10599. return g1==g2 ? -1 : g1; /* if length is 0, return -1 */
  10600. }
  10601. NK_INTERN int
  10602. nk_tt_GetGlyphBox(const struct nk_tt_fontinfo *info, int glyph_index,
  10603. int *x0, int *y0, int *x1, int *y1)
  10604. {
  10605. int g = nk_tt__GetGlyfOffset(info, glyph_index);
  10606. if (g < 0) return 0;
  10607. if (x0) *x0 = nk_ttSHORT(info->data + g + 2);
  10608. if (y0) *y0 = nk_ttSHORT(info->data + g + 4);
  10609. if (x1) *x1 = nk_ttSHORT(info->data + g + 6);
  10610. if (y1) *y1 = nk_ttSHORT(info->data + g + 8);
  10611. return 1;
  10612. }
  10613. NK_INTERN int
  10614. nk_tt__close_shape(struct nk_tt_vertex *vertices, int num_vertices, int was_off,
  10615. int start_off, nk_int sx, nk_int sy, nk_int scx, nk_int scy, nk_int cx, nk_int cy)
  10616. {
  10617. if (start_off) {
  10618. if (was_off)
  10619. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy);
  10620. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, sx,sy,scx,scy);
  10621. } else {
  10622. if (was_off)
  10623. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve,sx,sy,cx,cy);
  10624. else
  10625. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vline,sx,sy,0,0);
  10626. }
  10627. return num_vertices;
  10628. }
  10629. NK_INTERN int
  10630. nk_tt_GetGlyphShape(const struct nk_tt_fontinfo *info, struct nk_allocator *alloc,
  10631. int glyph_index, struct nk_tt_vertex **pvertices)
  10632. {
  10633. nk_short numberOfContours;
  10634. const nk_byte *endPtsOfContours;
  10635. const nk_byte *data = info->data;
  10636. struct nk_tt_vertex *vertices=0;
  10637. int num_vertices=0;
  10638. int g = nk_tt__GetGlyfOffset(info, glyph_index);
  10639. *pvertices = 0;
  10640. if (g < 0) return 0;
  10641. numberOfContours = nk_ttSHORT(data + g);
  10642. if (numberOfContours > 0) {
  10643. nk_byte flags=0,flagcount;
  10644. nk_int ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0;
  10645. nk_int x,y,cx,cy,sx,sy, scx,scy;
  10646. const nk_byte *points;
  10647. endPtsOfContours = (data + g + 10);
  10648. ins = nk_ttUSHORT(data + g + 10 + numberOfContours * 2);
  10649. points = data + g + 10 + numberOfContours * 2 + 2 + ins;
  10650. n = 1+nk_ttUSHORT(endPtsOfContours + numberOfContours*2-2);
  10651. m = n + 2*numberOfContours; /* a loose bound on how many vertices we might need */
  10652. vertices = (struct nk_tt_vertex *)alloc->alloc(alloc->userdata, 0, (nk_size)m * sizeof(vertices[0]));
  10653. if (vertices == 0)
  10654. return 0;
  10655. next_move = 0;
  10656. flagcount=0;
  10657. /* in first pass, we load uninterpreted data into the allocated array */
  10658. /* above, shifted to the end of the array so we won't overwrite it when */
  10659. /* we create our final data starting from the front */
  10660. off = m - n; /* starting offset for uninterpreted data, regardless of how m ends up being calculated */
  10661. /* first load flags */
  10662. for (i=0; i < n; ++i) {
  10663. if (flagcount == 0) {
  10664. flags = *points++;
  10665. if (flags & 8)
  10666. flagcount = *points++;
  10667. } else --flagcount;
  10668. vertices[off+i].type = flags;
  10669. }
  10670. /* now load x coordinates */
  10671. x=0;
  10672. for (i=0; i < n; ++i) {
  10673. flags = vertices[off+i].type;
  10674. if (flags & 2) {
  10675. nk_short dx = *points++;
  10676. x += (flags & 16) ? dx : -dx; /* ??? */
  10677. } else {
  10678. if (!(flags & 16)) {
  10679. x = x + (nk_short) (points[0]*256 + points[1]);
  10680. points += 2;
  10681. }
  10682. }
  10683. vertices[off+i].x = (nk_short) x;
  10684. }
  10685. /* now load y coordinates */
  10686. y=0;
  10687. for (i=0; i < n; ++i) {
  10688. flags = vertices[off+i].type;
  10689. if (flags & 4) {
  10690. nk_short dy = *points++;
  10691. y += (flags & 32) ? dy : -dy; /* ??? */
  10692. } else {
  10693. if (!(flags & 32)) {
  10694. y = y + (nk_short) (points[0]*256 + points[1]);
  10695. points += 2;
  10696. }
  10697. }
  10698. vertices[off+i].y = (nk_short) y;
  10699. }
  10700. /* now convert them to our format */
  10701. num_vertices=0;
  10702. sx = sy = cx = cy = scx = scy = 0;
  10703. for (i=0; i < n; ++i)
  10704. {
  10705. flags = vertices[off+i].type;
  10706. x = (nk_short) vertices[off+i].x;
  10707. y = (nk_short) vertices[off+i].y;
  10708. if (next_move == i) {
  10709. if (i != 0)
  10710. num_vertices = nk_tt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
  10711. /* now start the new one */
  10712. start_off = !(flags & 1);
  10713. if (start_off) {
  10714. /* if we start off with an off-curve point, then when we need to find a point on the curve */
  10715. /* where we can start, and we need to save some state for when we wraparound. */
  10716. scx = x;
  10717. scy = y;
  10718. if (!(vertices[off+i+1].type & 1)) {
  10719. /* next point is also a curve point, so interpolate an on-point curve */
  10720. sx = (x + (nk_int) vertices[off+i+1].x) >> 1;
  10721. sy = (y + (nk_int) vertices[off+i+1].y) >> 1;
  10722. } else {
  10723. /* otherwise just use the next point as our start point */
  10724. sx = (nk_int) vertices[off+i+1].x;
  10725. sy = (nk_int) vertices[off+i+1].y;
  10726. ++i; /* we're using point i+1 as the starting point, so skip it */
  10727. }
  10728. } else {
  10729. sx = x;
  10730. sy = y;
  10731. }
  10732. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vmove,sx,sy,0,0);
  10733. was_off = 0;
  10734. next_move = 1 + nk_ttUSHORT(endPtsOfContours+j*2);
  10735. ++j;
  10736. } else {
  10737. if (!(flags & 1))
  10738. { /* if it's a curve */
  10739. if (was_off) /* two off-curve control points in a row means interpolate an on-curve midpoint */
  10740. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy);
  10741. cx = x;
  10742. cy = y;
  10743. was_off = 1;
  10744. } else {
  10745. if (was_off)
  10746. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, x,y, cx, cy);
  10747. else nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vline, x,y,0,0);
  10748. was_off = 0;
  10749. }
  10750. }
  10751. }
  10752. num_vertices = nk_tt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
  10753. } else if (numberOfContours == -1) {
  10754. /* Compound shapes. */
  10755. int more = 1;
  10756. const nk_byte *comp = data + g + 10;
  10757. num_vertices = 0;
  10758. vertices = 0;
  10759. while (more)
  10760. {
  10761. nk_ushort flags, gidx;
  10762. int comp_num_verts = 0, i;
  10763. struct nk_tt_vertex *comp_verts = 0, *tmp = 0;
  10764. float mtx[6] = {1,0,0,1,0,0}, m, n;
  10765. flags = (nk_ushort)nk_ttSHORT(comp); comp+=2;
  10766. gidx = (nk_ushort)nk_ttSHORT(comp); comp+=2;
  10767. if (flags & 2) { /* XY values */
  10768. if (flags & 1) { /* shorts */
  10769. mtx[4] = nk_ttSHORT(comp); comp+=2;
  10770. mtx[5] = nk_ttSHORT(comp); comp+=2;
  10771. } else {
  10772. mtx[4] = nk_ttCHAR(comp); comp+=1;
  10773. mtx[5] = nk_ttCHAR(comp); comp+=1;
  10774. }
  10775. } else {
  10776. /* @TODO handle matching point */
  10777. NK_ASSERT(0);
  10778. }
  10779. if (flags & (1<<3)) { /* WE_HAVE_A_SCALE */
  10780. mtx[0] = mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10781. mtx[1] = mtx[2] = 0;
  10782. } else if (flags & (1<<6)) { /* WE_HAVE_AN_X_AND_YSCALE */
  10783. mtx[0] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10784. mtx[1] = mtx[2] = 0;
  10785. mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10786. } else if (flags & (1<<7)) { /* WE_HAVE_A_TWO_BY_TWO */
  10787. mtx[0] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10788. mtx[1] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10789. mtx[2] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10790. mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10791. }
  10792. /* Find transformation scales. */
  10793. m = (float) NK_SQRT(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
  10794. n = (float) NK_SQRT(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
  10795. /* Get indexed glyph. */
  10796. comp_num_verts = nk_tt_GetGlyphShape(info, alloc, gidx, &comp_verts);
  10797. if (comp_num_verts > 0)
  10798. {
  10799. /* Transform vertices. */
  10800. for (i = 0; i < comp_num_verts; ++i) {
  10801. struct nk_tt_vertex* v = &comp_verts[i];
  10802. short x,y;
  10803. x=v->x; y=v->y;
  10804. v->x = (short)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
  10805. v->y = (short)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
  10806. x=v->cx; y=v->cy;
  10807. v->cx = (short)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
  10808. v->cy = (short)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
  10809. }
  10810. /* Append vertices. */
  10811. tmp = (struct nk_tt_vertex*)alloc->alloc(alloc->userdata, 0,
  10812. (nk_size)(num_vertices+comp_num_verts)*sizeof(struct nk_tt_vertex));
  10813. if (!tmp) {
  10814. if (vertices) alloc->free(alloc->userdata, vertices);
  10815. if (comp_verts) alloc->free(alloc->userdata, comp_verts);
  10816. return 0;
  10817. }
  10818. if (num_vertices > 0) NK_MEMCPY(tmp, vertices, (nk_size)num_vertices*sizeof(struct nk_tt_vertex));
  10819. NK_MEMCPY(tmp+num_vertices, comp_verts, (nk_size)comp_num_verts*sizeof(struct nk_tt_vertex));
  10820. if (vertices) alloc->free(alloc->userdata,vertices);
  10821. vertices = tmp;
  10822. alloc->free(alloc->userdata,comp_verts);
  10823. num_vertices += comp_num_verts;
  10824. }
  10825. /* More components ? */
  10826. more = flags & (1<<5);
  10827. }
  10828. } else if (numberOfContours < 0) {
  10829. /* @TODO other compound variations? */
  10830. NK_ASSERT(0);
  10831. } else {
  10832. /* numberOfCounters == 0, do nothing */
  10833. }
  10834. *pvertices = vertices;
  10835. return num_vertices;
  10836. }
  10837. NK_INTERN void
  10838. nk_tt_GetGlyphHMetrics(const struct nk_tt_fontinfo *info, int glyph_index,
  10839. int *advanceWidth, int *leftSideBearing)
  10840. {
  10841. nk_ushort numOfLongHorMetrics = nk_ttUSHORT(info->data+info->hhea + 34);
  10842. if (glyph_index < numOfLongHorMetrics) {
  10843. if (advanceWidth)
  10844. *advanceWidth = nk_ttSHORT(info->data + info->hmtx + 4*glyph_index);
  10845. if (leftSideBearing)
  10846. *leftSideBearing = nk_ttSHORT(info->data + info->hmtx + 4*glyph_index + 2);
  10847. } else {
  10848. if (advanceWidth)
  10849. *advanceWidth = nk_ttSHORT(info->data + info->hmtx + 4*(numOfLongHorMetrics-1));
  10850. if (leftSideBearing)
  10851. *leftSideBearing = nk_ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));
  10852. }
  10853. }
  10854. NK_INTERN void
  10855. nk_tt_GetFontVMetrics(const struct nk_tt_fontinfo *info,
  10856. int *ascent, int *descent, int *lineGap)
  10857. {
  10858. if (ascent ) *ascent = nk_ttSHORT(info->data+info->hhea + 4);
  10859. if (descent) *descent = nk_ttSHORT(info->data+info->hhea + 6);
  10860. if (lineGap) *lineGap = nk_ttSHORT(info->data+info->hhea + 8);
  10861. }
  10862. NK_INTERN float
  10863. nk_tt_ScaleForPixelHeight(const struct nk_tt_fontinfo *info, float height)
  10864. {
  10865. int fheight = nk_ttSHORT(info->data + info->hhea + 4) - nk_ttSHORT(info->data + info->hhea + 6);
  10866. return (float) height / (float)fheight;
  10867. }
  10868. NK_INTERN float
  10869. nk_tt_ScaleForMappingEmToPixels(const struct nk_tt_fontinfo *info, float pixels)
  10870. {
  10871. int unitsPerEm = nk_ttUSHORT(info->data + info->head + 18);
  10872. return pixels / (float)unitsPerEm;
  10873. }
  10874. /*-------------------------------------------------------------
  10875. * antialiasing software rasterizer
  10876. * --------------------------------------------------------------*/
  10877. NK_INTERN void
  10878. nk_tt_GetGlyphBitmapBoxSubpixel(const struct nk_tt_fontinfo *font,
  10879. int glyph, float scale_x, float scale_y,float shift_x, float shift_y,
  10880. int *ix0, int *iy0, int *ix1, int *iy1)
  10881. {
  10882. int x0,y0,x1,y1;
  10883. if (!nk_tt_GetGlyphBox(font, glyph, &x0,&y0,&x1,&y1)) {
  10884. /* e.g. space character */
  10885. if (ix0) *ix0 = 0;
  10886. if (iy0) *iy0 = 0;
  10887. if (ix1) *ix1 = 0;
  10888. if (iy1) *iy1 = 0;
  10889. } else {
  10890. /* move to integral bboxes (treating pixels as little squares, what pixels get touched)? */
  10891. if (ix0) *ix0 = nk_ifloorf((float)x0 * scale_x + shift_x);
  10892. if (iy0) *iy0 = nk_ifloorf((float)-y1 * scale_y + shift_y);
  10893. if (ix1) *ix1 = nk_iceilf ((float)x1 * scale_x + shift_x);
  10894. if (iy1) *iy1 = nk_iceilf ((float)-y0 * scale_y + shift_y);
  10895. }
  10896. }
  10897. NK_INTERN void
  10898. nk_tt_GetGlyphBitmapBox(const struct nk_tt_fontinfo *font, int glyph,
  10899. float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)
  10900. {
  10901. nk_tt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1);
  10902. }
  10903. /*-------------------------------------------------------------
  10904. * Rasterizer
  10905. * --------------------------------------------------------------*/
  10906. NK_INTERN void*
  10907. nk_tt__hheap_alloc(struct nk_tt__hheap *hh, nk_size size)
  10908. {
  10909. if (hh->first_free) {
  10910. void *p = hh->first_free;
  10911. hh->first_free = * (void **) p;
  10912. return p;
  10913. } else {
  10914. if (hh->num_remaining_in_head_chunk == 0) {
  10915. int count = (size < 32 ? 2000 : size < 128 ? 800 : 100);
  10916. struct nk_tt__hheap_chunk *c = (struct nk_tt__hheap_chunk *)
  10917. hh->alloc.alloc(hh->alloc.userdata, 0,
  10918. sizeof(struct nk_tt__hheap_chunk) + size * (nk_size)count);
  10919. if (c == 0) return 0;
  10920. c->next = hh->head;
  10921. hh->head = c;
  10922. hh->num_remaining_in_head_chunk = count;
  10923. }
  10924. --hh->num_remaining_in_head_chunk;
  10925. return (char *) (hh->head) + size * (nk_size)hh->num_remaining_in_head_chunk;
  10926. }
  10927. }
  10928. NK_INTERN void
  10929. nk_tt__hheap_free(struct nk_tt__hheap *hh, void *p)
  10930. {
  10931. *(void **) p = hh->first_free;
  10932. hh->first_free = p;
  10933. }
  10934. NK_INTERN void
  10935. nk_tt__hheap_cleanup(struct nk_tt__hheap *hh)
  10936. {
  10937. struct nk_tt__hheap_chunk *c = hh->head;
  10938. while (c) {
  10939. struct nk_tt__hheap_chunk *n = c->next;
  10940. hh->alloc.free(hh->alloc.userdata, c);
  10941. c = n;
  10942. }
  10943. }
  10944. NK_INTERN struct nk_tt__active_edge*
  10945. nk_tt__new_active(struct nk_tt__hheap *hh, struct nk_tt__edge *e,
  10946. int off_x, float start_point)
  10947. {
  10948. struct nk_tt__active_edge *z = (struct nk_tt__active_edge *)
  10949. nk_tt__hheap_alloc(hh, sizeof(*z));
  10950. float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
  10951. /*STBTT_assert(e->y0 <= start_point); */
  10952. if (!z) return z;
  10953. z->fdx = dxdy;
  10954. z->fdy = (dxdy != 0) ? (1/dxdy): 0;
  10955. z->fx = e->x0 + dxdy * (start_point - e->y0);
  10956. z->fx -= (float)off_x;
  10957. z->direction = e->invert ? 1.0f : -1.0f;
  10958. z->sy = e->y0;
  10959. z->ey = e->y1;
  10960. z->next = 0;
  10961. return z;
  10962. }
  10963. NK_INTERN void
  10964. nk_tt__handle_clipped_edge(float *scanline, int x, struct nk_tt__active_edge *e,
  10965. float x0, float y0, float x1, float y1)
  10966. {
  10967. if (y0 == y1) return;
  10968. NK_ASSERT(y0 < y1);
  10969. NK_ASSERT(e->sy <= e->ey);
  10970. if (y0 > e->ey) return;
  10971. if (y1 < e->sy) return;
  10972. if (y0 < e->sy) {
  10973. x0 += (x1-x0) * (e->sy - y0) / (y1-y0);
  10974. y0 = e->sy;
  10975. }
  10976. if (y1 > e->ey) {
  10977. x1 += (x1-x0) * (e->ey - y1) / (y1-y0);
  10978. y1 = e->ey;
  10979. }
  10980. if (x0 == x) NK_ASSERT(x1 <= x+1);
  10981. else if (x0 == x+1) NK_ASSERT(x1 >= x);
  10982. else if (x0 <= x) NK_ASSERT(x1 <= x);
  10983. else if (x0 >= x+1) NK_ASSERT(x1 >= x+1);
  10984. else NK_ASSERT(x1 >= x && x1 <= x+1);
  10985. if (x0 <= x && x1 <= x)
  10986. scanline[x] += e->direction * (y1-y0);
  10987. else if (x0 >= x+1 && x1 >= x+1);
  10988. else {
  10989. NK_ASSERT(x0 >= x && x0 <= x+1 && x1 >= x && x1 <= x+1);
  10990. /* coverage = 1 - average x position */
  10991. scanline[x] += (float)e->direction * (float)(y1-y0) * (1.0f-((x0-(float)x)+(x1-(float)x))/2.0f);
  10992. }
  10993. }
  10994. NK_INTERN void
  10995. nk_tt__fill_active_edges_new(float *scanline, float *scanline_fill, int len,
  10996. struct nk_tt__active_edge *e, float y_top)
  10997. {
  10998. float y_bottom = y_top+1;
  10999. while (e)
  11000. {
  11001. /* brute force every pixel */
  11002. /* compute intersection points with top & bottom */
  11003. NK_ASSERT(e->ey >= y_top);
  11004. if (e->fdx == 0) {
  11005. float x0 = e->fx;
  11006. if (x0 < len) {
  11007. if (x0 >= 0) {
  11008. nk_tt__handle_clipped_edge(scanline,(int) x0,e, x0,y_top, x0,y_bottom);
  11009. nk_tt__handle_clipped_edge(scanline_fill-1,(int) x0+1,e, x0,y_top, x0,y_bottom);
  11010. } else {
  11011. nk_tt__handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bottom);
  11012. }
  11013. }
  11014. } else {
  11015. float x0 = e->fx;
  11016. float dx = e->fdx;
  11017. float xb = x0 + dx;
  11018. float x_top, x_bottom;
  11019. float y0,y1;
  11020. float dy = e->fdy;
  11021. NK_ASSERT(e->sy <= y_bottom && e->ey >= y_top);
  11022. /* compute endpoints of line segment clipped to this scanline (if the */
  11023. /* line segment starts on this scanline. x0 is the intersection of the */
  11024. /* line with y_top, but that may be off the line segment. */
  11025. if (e->sy > y_top) {
  11026. x_top = x0 + dx * (e->sy - y_top);
  11027. y0 = e->sy;
  11028. } else {
  11029. x_top = x0;
  11030. y0 = y_top;
  11031. }
  11032. if (e->ey < y_bottom) {
  11033. x_bottom = x0 + dx * (e->ey - y_top);
  11034. y1 = e->ey;
  11035. } else {
  11036. x_bottom = xb;
  11037. y1 = y_bottom;
  11038. }
  11039. if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len)
  11040. {
  11041. /* from here on, we don't have to range check x values */
  11042. if ((int) x_top == (int) x_bottom) {
  11043. float height;
  11044. /* simple case, only spans one pixel */
  11045. int x = (int) x_top;
  11046. height = y1 - y0;
  11047. NK_ASSERT(x >= 0 && x < len);
  11048. scanline[x] += e->direction * (1.0f-(((float)x_top - (float)x) + ((float)x_bottom-(float)x))/2.0f) * (float)height;
  11049. scanline_fill[x] += e->direction * (float)height; /* everything right of this pixel is filled */
  11050. } else {
  11051. int x,x1,x2;
  11052. float y_crossing, step, sign, area;
  11053. /* covers 2+ pixels */
  11054. if (x_top > x_bottom)
  11055. {
  11056. /* flip scanline vertically; signed area is the same */
  11057. float t;
  11058. y0 = y_bottom - (y0 - y_top);
  11059. y1 = y_bottom - (y1 - y_top);
  11060. t = y0; y0 = y1; y1 = t;
  11061. t = x_bottom; x_bottom = x_top; x_top = t;
  11062. dx = -dx;
  11063. dy = -dy;
  11064. t = x0; x0 = xb; xb = t;
  11065. }
  11066. x1 = (int) x_top;
  11067. x2 = (int) x_bottom;
  11068. /* compute intersection with y axis at x1+1 */
  11069. y_crossing = ((float)x1+1 - (float)x0) * (float)dy + (float)y_top;
  11070. sign = e->direction;
  11071. /* area of the rectangle covered from y0..y_crossing */
  11072. area = sign * (y_crossing-y0);
  11073. /* area of the triangle (x_top,y0), (x+1,y0), (x+1,y_crossing) */
  11074. scanline[x1] += area * (1.0f-((float)((float)x_top - (float)x1)+(float)(x1+1-x1))/2.0f);
  11075. step = sign * dy;
  11076. for (x = x1+1; x < x2; ++x) {
  11077. scanline[x] += area + step/2;
  11078. area += step;
  11079. }
  11080. y_crossing += (float)dy * (float)(x2 - (x1+1));
  11081. scanline[x2] += area + sign * (1.0f-((float)(x2-x2)+((float)x_bottom-(float)x2))/2.0f) * (y1-y_crossing);
  11082. scanline_fill[x2] += sign * (y1-y0);
  11083. }
  11084. }
  11085. else
  11086. {
  11087. /* if edge goes outside of box we're drawing, we require */
  11088. /* clipping logic. since this does not match the intended use */
  11089. /* of this library, we use a different, very slow brute */
  11090. /* force implementation */
  11091. int x;
  11092. for (x=0; x < len; ++x)
  11093. {
  11094. /* cases: */
  11095. /* */
  11096. /* there can be up to two intersections with the pixel. any intersection */
  11097. /* with left or right edges can be handled by splitting into two (or three) */
  11098. /* regions. intersections with top & bottom do not necessitate case-wise logic. */
  11099. /* */
  11100. /* the old way of doing this found the intersections with the left & right edges, */
  11101. /* then used some simple logic to produce up to three segments in sorted order */
  11102. /* from top-to-bottom. however, this had a problem: if an x edge was epsilon */
  11103. /* across the x border, then the corresponding y position might not be distinct */
  11104. /* from the other y segment, and it might ignored as an empty segment. to avoid */
  11105. /* that, we need to explicitly produce segments based on x positions. */
  11106. /* rename variables to clear pairs */
  11107. float ya = y_top;
  11108. float x1 = (float) (x);
  11109. float x2 = (float) (x+1);
  11110. float x3 = xb;
  11111. float y3 = y_bottom;
  11112. float yb,y2;
  11113. yb = ((float)x - x0) / dx + y_top;
  11114. y2 = ((float)x+1 - x0) / dx + y_top;
  11115. if (x0 < x1 && x3 > x2) { /* three segments descending down-right */
  11116. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x1,yb);
  11117. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x2,y2);
  11118. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
  11119. } else if (x3 < x1 && x0 > x2) { /* three segments descending down-left */
  11120. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x2,y2);
  11121. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x1,yb);
  11122. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x3,y3);
  11123. } else if (x0 < x1 && x3 > x1) { /* two segments across x, down-right */
  11124. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x1,yb);
  11125. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x3,y3);
  11126. } else if (x3 < x1 && x0 > x1) { /* two segments across x, down-left */
  11127. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x1,yb);
  11128. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x3,y3);
  11129. } else if (x0 < x2 && x3 > x2) { /* two segments across x+1, down-right */
  11130. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x2,y2);
  11131. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
  11132. } else if (x3 < x2 && x0 > x2) { /* two segments across x+1, down-left */
  11133. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x2,y2);
  11134. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
  11135. } else { /* one segment */
  11136. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x3,y3);
  11137. }
  11138. }
  11139. }
  11140. }
  11141. e = e->next;
  11142. }
  11143. }
  11144. NK_INTERN void
  11145. nk_tt__rasterize_sorted_edges(struct nk_tt__bitmap *result, struct nk_tt__edge *e,
  11146. int n, int vsubsample, int off_x, int off_y, struct nk_allocator *alloc)
  11147. {
  11148. /* directly AA rasterize edges w/o supersampling */
  11149. struct nk_tt__hheap hh;
  11150. struct nk_tt__active_edge *active = 0;
  11151. int y,j=0, i;
  11152. float scanline_data[129], *scanline, *scanline2;
  11153. NK_UNUSED(vsubsample);
  11154. nk_zero_struct(hh);
  11155. hh.alloc = *alloc;
  11156. if (result->w > 64)
  11157. scanline = (float *) alloc->alloc(alloc->userdata,0, (nk_size)(result->w*2+1) * sizeof(float));
  11158. else scanline = scanline_data;
  11159. scanline2 = scanline + result->w;
  11160. y = off_y;
  11161. e[n].y0 = (float) (off_y + result->h) + 1;
  11162. while (j < result->h)
  11163. {
  11164. /* find center of pixel for this scanline */
  11165. float scan_y_top = (float)y + 0.0f;
  11166. float scan_y_bottom = (float)y + 1.0f;
  11167. struct nk_tt__active_edge **step = &active;
  11168. NK_MEMSET(scanline , 0, (nk_size)result->w*sizeof(scanline[0]));
  11169. NK_MEMSET(scanline2, 0, (nk_size)(result->w+1)*sizeof(scanline[0]));
  11170. /* update all active edges; */
  11171. /* remove all active edges that terminate before the top of this scanline */
  11172. while (*step) {
  11173. struct nk_tt__active_edge * z = *step;
  11174. if (z->ey <= scan_y_top) {
  11175. *step = z->next; /* delete from list */
  11176. NK_ASSERT(z->direction);
  11177. z->direction = 0;
  11178. nk_tt__hheap_free(&hh, z);
  11179. } else {
  11180. step = &((*step)->next); /* advance through list */
  11181. }
  11182. }
  11183. /* insert all edges that start before the bottom of this scanline */
  11184. while (e->y0 <= scan_y_bottom) {
  11185. if (e->y0 != e->y1) {
  11186. struct nk_tt__active_edge *z = nk_tt__new_active(&hh, e, off_x, scan_y_top);
  11187. if (z != 0) {
  11188. NK_ASSERT(z->ey >= scan_y_top);
  11189. /* insert at front */
  11190. z->next = active;
  11191. active = z;
  11192. }
  11193. }
  11194. ++e;
  11195. }
  11196. /* now process all active edges */
  11197. if (active)
  11198. nk_tt__fill_active_edges_new(scanline, scanline2+1, result->w, active, scan_y_top);
  11199. {
  11200. float sum = 0;
  11201. for (i=0; i < result->w; ++i) {
  11202. float k;
  11203. int m;
  11204. sum += scanline2[i];
  11205. k = scanline[i] + sum;
  11206. k = (float) NK_ABS(k) * 255.0f + 0.5f;
  11207. m = (int) k;
  11208. if (m > 255) m = 255;
  11209. result->pixels[j*result->stride + i] = (unsigned char) m;
  11210. }
  11211. }
  11212. /* advance all the edges */
  11213. step = &active;
  11214. while (*step) {
  11215. struct nk_tt__active_edge *z = *step;
  11216. z->fx += z->fdx; /* advance to position for current scanline */
  11217. step = &((*step)->next); /* advance through list */
  11218. }
  11219. ++y;
  11220. ++j;
  11221. }
  11222. nk_tt__hheap_cleanup(&hh);
  11223. if (scanline != scanline_data)
  11224. alloc->free(alloc->userdata, scanline);
  11225. }
  11226. NK_INTERN void
  11227. nk_tt__sort_edges_ins_sort(struct nk_tt__edge *p, int n)
  11228. {
  11229. int i,j;
  11230. #define NK_TT__COMPARE(a,b) ((a)->y0 < (b)->y0)
  11231. for (i=1; i < n; ++i) {
  11232. struct nk_tt__edge t = p[i], *a = &t;
  11233. j = i;
  11234. while (j > 0) {
  11235. struct nk_tt__edge *b = &p[j-1];
  11236. int c = NK_TT__COMPARE(a,b);
  11237. if (!c) break;
  11238. p[j] = p[j-1];
  11239. --j;
  11240. }
  11241. if (i != j)
  11242. p[j] = t;
  11243. }
  11244. }
  11245. NK_INTERN void
  11246. nk_tt__sort_edges_quicksort(struct nk_tt__edge *p, int n)
  11247. {
  11248. /* threshold for transitioning to insertion sort */
  11249. while (n > 12) {
  11250. struct nk_tt__edge t;
  11251. int c01,c12,c,m,i,j;
  11252. /* compute median of three */
  11253. m = n >> 1;
  11254. c01 = NK_TT__COMPARE(&p[0],&p[m]);
  11255. c12 = NK_TT__COMPARE(&p[m],&p[n-1]);
  11256. /* if 0 >= mid >= end, or 0 < mid < end, then use mid */
  11257. if (c01 != c12) {
  11258. /* otherwise, we'll need to swap something else to middle */
  11259. int z;
  11260. c = NK_TT__COMPARE(&p[0],&p[n-1]);
  11261. /* 0>mid && mid<n: 0>n => n; 0<n => 0 */
  11262. /* 0<mid && mid>n: 0>n => 0; 0<n => n */
  11263. z = (c == c12) ? 0 : n-1;
  11264. t = p[z];
  11265. p[z] = p[m];
  11266. p[m] = t;
  11267. }
  11268. /* now p[m] is the median-of-three */
  11269. /* swap it to the beginning so it won't move around */
  11270. t = p[0];
  11271. p[0] = p[m];
  11272. p[m] = t;
  11273. /* partition loop */
  11274. i=1;
  11275. j=n-1;
  11276. for(;;) {
  11277. /* handling of equality is crucial here */
  11278. /* for sentinels & efficiency with duplicates */
  11279. for (;;++i) {
  11280. if (!NK_TT__COMPARE(&p[i], &p[0])) break;
  11281. }
  11282. for (;;--j) {
  11283. if (!NK_TT__COMPARE(&p[0], &p[j])) break;
  11284. }
  11285. /* make sure we haven't crossed */
  11286. if (i >= j) break;
  11287. t = p[i];
  11288. p[i] = p[j];
  11289. p[j] = t;
  11290. ++i;
  11291. --j;
  11292. }
  11293. /* recurse on smaller side, iterate on larger */
  11294. if (j < (n-i)) {
  11295. nk_tt__sort_edges_quicksort(p,j);
  11296. p = p+i;
  11297. n = n-i;
  11298. } else {
  11299. nk_tt__sort_edges_quicksort(p+i, n-i);
  11300. n = j;
  11301. }
  11302. }
  11303. }
  11304. NK_INTERN void
  11305. nk_tt__sort_edges(struct nk_tt__edge *p, int n)
  11306. {
  11307. nk_tt__sort_edges_quicksort(p, n);
  11308. nk_tt__sort_edges_ins_sort(p, n);
  11309. }
  11310. NK_INTERN void
  11311. nk_tt__rasterize(struct nk_tt__bitmap *result, struct nk_tt__point *pts,
  11312. int *wcount, int windings, float scale_x, float scale_y,
  11313. float shift_x, float shift_y, int off_x, int off_y, int invert,
  11314. struct nk_allocator *alloc)
  11315. {
  11316. float y_scale_inv = invert ? -scale_y : scale_y;
  11317. struct nk_tt__edge *e;
  11318. int n,i,j,k,m;
  11319. int vsubsample = 1;
  11320. /* vsubsample should divide 255 evenly; otherwise we won't reach full opacity */
  11321. /* now we have to blow out the windings into explicit edge lists */
  11322. n = 0;
  11323. for (i=0; i < windings; ++i)
  11324. n += wcount[i];
  11325. e = (struct nk_tt__edge*)
  11326. alloc->alloc(alloc->userdata, 0,(sizeof(*e) * (nk_size)(n+1)));
  11327. if (e == 0) return;
  11328. n = 0;
  11329. m=0;
  11330. for (i=0; i < windings; ++i)
  11331. {
  11332. struct nk_tt__point *p = pts + m;
  11333. m += wcount[i];
  11334. j = wcount[i]-1;
  11335. for (k=0; k < wcount[i]; j=k++) {
  11336. int a=k,b=j;
  11337. /* skip the edge if horizontal */
  11338. if (p[j].y == p[k].y)
  11339. continue;
  11340. /* add edge from j to k to the list */
  11341. e[n].invert = 0;
  11342. if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) {
  11343. e[n].invert = 1;
  11344. a=j,b=k;
  11345. }
  11346. e[n].x0 = p[a].x * scale_x + shift_x;
  11347. e[n].y0 = (p[a].y * y_scale_inv + shift_y) * (float)vsubsample;
  11348. e[n].x1 = p[b].x * scale_x + shift_x;
  11349. e[n].y1 = (p[b].y * y_scale_inv + shift_y) * (float)vsubsample;
  11350. ++n;
  11351. }
  11352. }
  11353. /* now sort the edges by their highest point (should snap to integer, and then by x) */
  11354. /*STBTT_sort(e, n, sizeof(e[0]), nk_tt__edge_compare); */
  11355. nk_tt__sort_edges(e, n);
  11356. /* now, traverse the scanlines and find the intersections on each scanline, use xor winding rule */
  11357. nk_tt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, alloc);
  11358. alloc->free(alloc->userdata, e);
  11359. }
  11360. NK_INTERN void
  11361. nk_tt__add_point(struct nk_tt__point *points, int n, float x, float y)
  11362. {
  11363. if (!points) return; /* during first pass, it's unallocated */
  11364. points[n].x = x;
  11365. points[n].y = y;
  11366. }
  11367. NK_INTERN int
  11368. nk_tt__tesselate_curve(struct nk_tt__point *points, int *num_points,
  11369. float x0, float y0, float x1, float y1, float x2, float y2,
  11370. float objspace_flatness_squared, int n)
  11371. {
  11372. /* tesselate until threshold p is happy...
  11373. * @TODO warped to compensate for non-linear stretching */
  11374. /* midpoint */
  11375. float mx = (x0 + 2*x1 + x2)/4;
  11376. float my = (y0 + 2*y1 + y2)/4;
  11377. /* versus directly drawn line */
  11378. float dx = (x0+x2)/2 - mx;
  11379. float dy = (y0+y2)/2 - my;
  11380. if (n > 16) /* 65536 segments on one curve better be enough! */
  11381. return 1;
  11382. /* half-pixel error allowed... need to be smaller if AA */
  11383. if (dx*dx+dy*dy > objspace_flatness_squared) {
  11384. nk_tt__tesselate_curve(points, num_points, x0,y0,
  11385. (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1);
  11386. nk_tt__tesselate_curve(points, num_points, mx,my,
  11387. (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1);
  11388. } else {
  11389. nk_tt__add_point(points, *num_points,x2,y2);
  11390. *num_points = *num_points+1;
  11391. }
  11392. return 1;
  11393. }
  11394. NK_INTERN struct nk_tt__point*
  11395. nk_tt_FlattenCurves(struct nk_tt_vertex *vertices, int num_verts,
  11396. float objspace_flatness, int **contour_lengths, int *num_contours,
  11397. struct nk_allocator *alloc)
  11398. {
  11399. /* returns number of contours */
  11400. struct nk_tt__point *points=0;
  11401. int num_points=0;
  11402. float objspace_flatness_squared = objspace_flatness * objspace_flatness;
  11403. int i;
  11404. int n=0;
  11405. int start=0;
  11406. int pass;
  11407. /* count how many "moves" there are to get the contour count */
  11408. for (i=0; i < num_verts; ++i)
  11409. if (vertices[i].type == NK_TT_vmove) ++n;
  11410. *num_contours = n;
  11411. if (n == 0) return 0;
  11412. *contour_lengths = (int *)
  11413. alloc->alloc(alloc->userdata,0, (sizeof(**contour_lengths) * (nk_size)n));
  11414. if (*contour_lengths == 0) {
  11415. *num_contours = 0;
  11416. return 0;
  11417. }
  11418. /* make two passes through the points so we don't need to realloc */
  11419. for (pass=0; pass < 2; ++pass)
  11420. {
  11421. float x=0,y=0;
  11422. if (pass == 1) {
  11423. points = (struct nk_tt__point *)
  11424. alloc->alloc(alloc->userdata,0, (nk_size)num_points * sizeof(points[0]));
  11425. if (points == 0) goto error;
  11426. }
  11427. num_points = 0;
  11428. n= -1;
  11429. for (i=0; i < num_verts; ++i)
  11430. {
  11431. switch (vertices[i].type) {
  11432. case NK_TT_vmove:
  11433. /* start the next contour */
  11434. if (n >= 0)
  11435. (*contour_lengths)[n] = num_points - start;
  11436. ++n;
  11437. start = num_points;
  11438. x = vertices[i].x, y = vertices[i].y;
  11439. nk_tt__add_point(points, num_points++, x,y);
  11440. break;
  11441. case NK_TT_vline:
  11442. x = vertices[i].x, y = vertices[i].y;
  11443. nk_tt__add_point(points, num_points++, x, y);
  11444. break;
  11445. case NK_TT_vcurve:
  11446. nk_tt__tesselate_curve(points, &num_points, x,y,
  11447. vertices[i].cx, vertices[i].cy,
  11448. vertices[i].x, vertices[i].y,
  11449. objspace_flatness_squared, 0);
  11450. x = vertices[i].x, y = vertices[i].y;
  11451. break;
  11452. default: break;
  11453. }
  11454. }
  11455. (*contour_lengths)[n] = num_points - start;
  11456. }
  11457. return points;
  11458. error:
  11459. alloc->free(alloc->userdata, points);
  11460. alloc->free(alloc->userdata, *contour_lengths);
  11461. *contour_lengths = 0;
  11462. *num_contours = 0;
  11463. return 0;
  11464. }
  11465. NK_INTERN void
  11466. nk_tt_Rasterize(struct nk_tt__bitmap *result, float flatness_in_pixels,
  11467. struct nk_tt_vertex *vertices, int num_verts,
  11468. float scale_x, float scale_y, float shift_x, float shift_y,
  11469. int x_off, int y_off, int invert, struct nk_allocator *alloc)
  11470. {
  11471. float scale = scale_x > scale_y ? scale_y : scale_x;
  11472. int winding_count, *winding_lengths;
  11473. struct nk_tt__point *windings = nk_tt_FlattenCurves(vertices, num_verts,
  11474. flatness_in_pixels / scale, &winding_lengths, &winding_count, alloc);
  11475. NK_ASSERT(alloc);
  11476. if (windings) {
  11477. nk_tt__rasterize(result, windings, winding_lengths, winding_count,
  11478. scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, alloc);
  11479. alloc->free(alloc->userdata, winding_lengths);
  11480. alloc->free(alloc->userdata, windings);
  11481. }
  11482. }
  11483. NK_INTERN void
  11484. nk_tt_MakeGlyphBitmapSubpixel(const struct nk_tt_fontinfo *info, unsigned char *output,
  11485. int out_w, int out_h, int out_stride, float scale_x, float scale_y,
  11486. float shift_x, float shift_y, int glyph, struct nk_allocator *alloc)
  11487. {
  11488. int ix0,iy0;
  11489. struct nk_tt_vertex *vertices;
  11490. int num_verts = nk_tt_GetGlyphShape(info, alloc, glyph, &vertices);
  11491. struct nk_tt__bitmap gbm;
  11492. nk_tt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x,
  11493. shift_y, &ix0,&iy0,0,0);
  11494. gbm.pixels = output;
  11495. gbm.w = out_w;
  11496. gbm.h = out_h;
  11497. gbm.stride = out_stride;
  11498. if (gbm.w && gbm.h)
  11499. nk_tt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y,
  11500. shift_x, shift_y, ix0,iy0, 1, alloc);
  11501. alloc->free(alloc->userdata, vertices);
  11502. }
  11503. /*-------------------------------------------------------------
  11504. * Bitmap baking
  11505. * --------------------------------------------------------------*/
  11506. NK_INTERN int
  11507. nk_tt_PackBegin(struct nk_tt_pack_context *spc, unsigned char *pixels,
  11508. int pw, int ph, int stride_in_bytes, int padding, struct nk_allocator *alloc)
  11509. {
  11510. int num_nodes = pw - padding;
  11511. struct nk_rp_context *context = (struct nk_rp_context *)
  11512. alloc->alloc(alloc->userdata,0, sizeof(*context));
  11513. struct nk_rp_node *nodes = (struct nk_rp_node*)
  11514. alloc->alloc(alloc->userdata,0, (sizeof(*nodes ) * (nk_size)num_nodes));
  11515. if (context == 0 || nodes == 0) {
  11516. if (context != 0) alloc->free(alloc->userdata, context);
  11517. if (nodes != 0) alloc->free(alloc->userdata, nodes);
  11518. return 0;
  11519. }
  11520. spc->width = pw;
  11521. spc->height = ph;
  11522. spc->pixels = pixels;
  11523. spc->pack_info = context;
  11524. spc->nodes = nodes;
  11525. spc->padding = padding;
  11526. spc->stride_in_bytes = (stride_in_bytes != 0) ? stride_in_bytes : pw;
  11527. spc->h_oversample = 1;
  11528. spc->v_oversample = 1;
  11529. nk_rp_init_target(context, pw-padding, ph-padding, nodes, num_nodes);
  11530. if (pixels)
  11531. NK_MEMSET(pixels, 0, (nk_size)(pw*ph)); /* background of 0 around pixels */
  11532. return 1;
  11533. }
  11534. NK_INTERN void
  11535. nk_tt_PackEnd(struct nk_tt_pack_context *spc, struct nk_allocator *alloc)
  11536. {
  11537. alloc->free(alloc->userdata, spc->nodes);
  11538. alloc->free(alloc->userdata, spc->pack_info);
  11539. }
  11540. NK_INTERN void
  11541. nk_tt_PackSetOversampling(struct nk_tt_pack_context *spc,
  11542. unsigned int h_oversample, unsigned int v_oversample)
  11543. {
  11544. NK_ASSERT(h_oversample <= NK_TT_MAX_OVERSAMPLE);
  11545. NK_ASSERT(v_oversample <= NK_TT_MAX_OVERSAMPLE);
  11546. if (h_oversample <= NK_TT_MAX_OVERSAMPLE)
  11547. spc->h_oversample = h_oversample;
  11548. if (v_oversample <= NK_TT_MAX_OVERSAMPLE)
  11549. spc->v_oversample = v_oversample;
  11550. }
  11551. NK_INTERN void
  11552. nk_tt__h_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes,
  11553. int kernel_width)
  11554. {
  11555. unsigned char buffer[NK_TT_MAX_OVERSAMPLE];
  11556. int safe_w = w - kernel_width;
  11557. int j;
  11558. for (j=0; j < h; ++j)
  11559. {
  11560. int i;
  11561. unsigned int total;
  11562. NK_MEMSET(buffer, 0, (nk_size)kernel_width);
  11563. total = 0;
  11564. /* make kernel_width a constant in common cases so compiler can optimize out the divide */
  11565. switch (kernel_width) {
  11566. case 2:
  11567. for (i=0; i <= safe_w; ++i) {
  11568. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  11569. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  11570. pixels[i] = (unsigned char) (total / 2);
  11571. }
  11572. break;
  11573. case 3:
  11574. for (i=0; i <= safe_w; ++i) {
  11575. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  11576. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  11577. pixels[i] = (unsigned char) (total / 3);
  11578. }
  11579. break;
  11580. case 4:
  11581. for (i=0; i <= safe_w; ++i) {
  11582. total += (unsigned int)pixels[i] - buffer[i & NK_TT__OVER_MASK];
  11583. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  11584. pixels[i] = (unsigned char) (total / 4);
  11585. }
  11586. break;
  11587. case 5:
  11588. for (i=0; i <= safe_w; ++i) {
  11589. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  11590. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  11591. pixels[i] = (unsigned char) (total / 5);
  11592. }
  11593. break;
  11594. default:
  11595. for (i=0; i <= safe_w; ++i) {
  11596. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  11597. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  11598. pixels[i] = (unsigned char) (total / (unsigned int)kernel_width);
  11599. }
  11600. break;
  11601. }
  11602. for (; i < w; ++i) {
  11603. NK_ASSERT(pixels[i] == 0);
  11604. total -= (unsigned int)(buffer[i & NK_TT__OVER_MASK]);
  11605. pixels[i] = (unsigned char) (total / (unsigned int)kernel_width);
  11606. }
  11607. pixels += stride_in_bytes;
  11608. }
  11609. }
  11610. NK_INTERN void
  11611. nk_tt__v_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes,
  11612. int kernel_width)
  11613. {
  11614. unsigned char buffer[NK_TT_MAX_OVERSAMPLE];
  11615. int safe_h = h - kernel_width;
  11616. int j;
  11617. for (j=0; j < w; ++j)
  11618. {
  11619. int i;
  11620. unsigned int total;
  11621. NK_MEMSET(buffer, 0, (nk_size)kernel_width);
  11622. total = 0;
  11623. /* make kernel_width a constant in common cases so compiler can optimize out the divide */
  11624. switch (kernel_width) {
  11625. case 2:
  11626. for (i=0; i <= safe_h; ++i) {
  11627. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  11628. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  11629. pixels[i*stride_in_bytes] = (unsigned char) (total / 2);
  11630. }
  11631. break;
  11632. case 3:
  11633. for (i=0; i <= safe_h; ++i) {
  11634. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  11635. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  11636. pixels[i*stride_in_bytes] = (unsigned char) (total / 3);
  11637. }
  11638. break;
  11639. case 4:
  11640. for (i=0; i <= safe_h; ++i) {
  11641. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  11642. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  11643. pixels[i*stride_in_bytes] = (unsigned char) (total / 4);
  11644. }
  11645. break;
  11646. case 5:
  11647. for (i=0; i <= safe_h; ++i) {
  11648. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  11649. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  11650. pixels[i*stride_in_bytes] = (unsigned char) (total / 5);
  11651. }
  11652. break;
  11653. default:
  11654. for (i=0; i <= safe_h; ++i) {
  11655. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  11656. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  11657. pixels[i*stride_in_bytes] = (unsigned char) (total / (unsigned int)kernel_width);
  11658. }
  11659. break;
  11660. }
  11661. for (; i < h; ++i) {
  11662. NK_ASSERT(pixels[i*stride_in_bytes] == 0);
  11663. total -= (unsigned int)(buffer[i & NK_TT__OVER_MASK]);
  11664. pixels[i*stride_in_bytes] = (unsigned char) (total / (unsigned int)kernel_width);
  11665. }
  11666. pixels += 1;
  11667. }
  11668. }
  11669. NK_INTERN float
  11670. nk_tt__oversample_shift(int oversample)
  11671. {
  11672. if (!oversample)
  11673. return 0.0f;
  11674. /* The prefilter is a box filter of width "oversample", */
  11675. /* which shifts phase by (oversample - 1)/2 pixels in */
  11676. /* oversampled space. We want to shift in the opposite */
  11677. /* direction to counter this. */
  11678. return (float)-(oversample - 1) / (2.0f * (float)oversample);
  11679. }
  11680. NK_INTERN int
  11681. nk_tt_PackFontRangesGatherRects(struct nk_tt_pack_context *spc,
  11682. struct nk_tt_fontinfo *info, struct nk_tt_pack_range *ranges,
  11683. int num_ranges, struct nk_rp_rect *rects)
  11684. {
  11685. /* rects array must be big enough to accommodate all characters in the given ranges */
  11686. int i,j,k;
  11687. k = 0;
  11688. for (i=0; i < num_ranges; ++i) {
  11689. float fh = ranges[i].font_size;
  11690. float scale = (fh > 0) ? nk_tt_ScaleForPixelHeight(info, fh):
  11691. nk_tt_ScaleForMappingEmToPixels(info, -fh);
  11692. ranges[i].h_oversample = (unsigned char) spc->h_oversample;
  11693. ranges[i].v_oversample = (unsigned char) spc->v_oversample;
  11694. for (j=0; j < ranges[i].num_chars; ++j) {
  11695. int x0,y0,x1,y1;
  11696. int codepoint = ranges[i].first_unicode_codepoint_in_range ?
  11697. ranges[i].first_unicode_codepoint_in_range + j :
  11698. ranges[i].array_of_unicode_codepoints[j];
  11699. int glyph = nk_tt_FindGlyphIndex(info, codepoint);
  11700. nk_tt_GetGlyphBitmapBoxSubpixel(info,glyph, scale * (float)spc->h_oversample,
  11701. scale * (float)spc->v_oversample, 0,0, &x0,&y0,&x1,&y1);
  11702. rects[k].w = (nk_rp_coord) (x1-x0 + spc->padding + (int)spc->h_oversample-1);
  11703. rects[k].h = (nk_rp_coord) (y1-y0 + spc->padding + (int)spc->v_oversample-1);
  11704. ++k;
  11705. }
  11706. }
  11707. return k;
  11708. }
  11709. NK_INTERN int
  11710. nk_tt_PackFontRangesRenderIntoRects(struct nk_tt_pack_context *spc,
  11711. struct nk_tt_fontinfo *info, struct nk_tt_pack_range *ranges,
  11712. int num_ranges, struct nk_rp_rect *rects, struct nk_allocator *alloc)
  11713. {
  11714. int i,j,k, return_value = 1;
  11715. /* save current values */
  11716. int old_h_over = (int)spc->h_oversample;
  11717. int old_v_over = (int)spc->v_oversample;
  11718. /* rects array must be big enough to accommodate all characters in the given ranges */
  11719. k = 0;
  11720. for (i=0; i < num_ranges; ++i)
  11721. {
  11722. float fh = ranges[i].font_size;
  11723. float recip_h,recip_v,sub_x,sub_y;
  11724. float scale = fh > 0 ? nk_tt_ScaleForPixelHeight(info, fh):
  11725. nk_tt_ScaleForMappingEmToPixels(info, -fh);
  11726. spc->h_oversample = ranges[i].h_oversample;
  11727. spc->v_oversample = ranges[i].v_oversample;
  11728. recip_h = 1.0f / (float)spc->h_oversample;
  11729. recip_v = 1.0f / (float)spc->v_oversample;
  11730. sub_x = nk_tt__oversample_shift((int)spc->h_oversample);
  11731. sub_y = nk_tt__oversample_shift((int)spc->v_oversample);
  11732. for (j=0; j < ranges[i].num_chars; ++j)
  11733. {
  11734. struct nk_rp_rect *r = &rects[k];
  11735. if (r->was_packed)
  11736. {
  11737. struct nk_tt_packedchar *bc = &ranges[i].chardata_for_range[j];
  11738. int advance, lsb, x0,y0,x1,y1;
  11739. int codepoint = ranges[i].first_unicode_codepoint_in_range ?
  11740. ranges[i].first_unicode_codepoint_in_range + j :
  11741. ranges[i].array_of_unicode_codepoints[j];
  11742. int glyph = nk_tt_FindGlyphIndex(info, codepoint);
  11743. nk_rp_coord pad = (nk_rp_coord) spc->padding;
  11744. /* pad on left and top */
  11745. r->x = (nk_rp_coord)((int)r->x + (int)pad);
  11746. r->y = (nk_rp_coord)((int)r->y + (int)pad);
  11747. r->w = (nk_rp_coord)((int)r->w - (int)pad);
  11748. r->h = (nk_rp_coord)((int)r->h - (int)pad);
  11749. nk_tt_GetGlyphHMetrics(info, glyph, &advance, &lsb);
  11750. nk_tt_GetGlyphBitmapBox(info, glyph, scale * (float)spc->h_oversample,
  11751. (scale * (float)spc->v_oversample), &x0,&y0,&x1,&y1);
  11752. nk_tt_MakeGlyphBitmapSubpixel(info, spc->pixels + r->x + r->y*spc->stride_in_bytes,
  11753. (int)(r->w - spc->h_oversample+1), (int)(r->h - spc->v_oversample+1),
  11754. spc->stride_in_bytes, scale * (float)spc->h_oversample,
  11755. scale * (float)spc->v_oversample, 0,0, glyph, alloc);
  11756. if (spc->h_oversample > 1)
  11757. nk_tt__h_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
  11758. r->w, r->h, spc->stride_in_bytes, (int)spc->h_oversample);
  11759. if (spc->v_oversample > 1)
  11760. nk_tt__v_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
  11761. r->w, r->h, spc->stride_in_bytes, (int)spc->v_oversample);
  11762. bc->x0 = (nk_ushort) r->x;
  11763. bc->y0 = (nk_ushort) r->y;
  11764. bc->x1 = (nk_ushort) (r->x + r->w);
  11765. bc->y1 = (nk_ushort) (r->y + r->h);
  11766. bc->xadvance = scale * (float)advance;
  11767. bc->xoff = (float) x0 * recip_h + sub_x;
  11768. bc->yoff = (float) y0 * recip_v + sub_y;
  11769. bc->xoff2 = ((float)x0 + r->w) * recip_h + sub_x;
  11770. bc->yoff2 = ((float)y0 + r->h) * recip_v + sub_y;
  11771. } else {
  11772. return_value = 0; /* if any fail, report failure */
  11773. }
  11774. ++k;
  11775. }
  11776. }
  11777. /* restore original values */
  11778. spc->h_oversample = (unsigned int)old_h_over;
  11779. spc->v_oversample = (unsigned int)old_v_over;
  11780. return return_value;
  11781. }
  11782. NK_INTERN void
  11783. nk_tt_GetPackedQuad(struct nk_tt_packedchar *chardata, int pw, int ph,
  11784. int char_index, float *xpos, float *ypos, struct nk_tt_aligned_quad *q,
  11785. int align_to_integer)
  11786. {
  11787. float ipw = 1.0f / (float)pw, iph = 1.0f / (float)ph;
  11788. struct nk_tt_packedchar *b = (struct nk_tt_packedchar*)(chardata + char_index);
  11789. if (align_to_integer) {
  11790. int tx = nk_ifloorf((*xpos + b->xoff) + 0.5f);
  11791. int ty = nk_ifloorf((*ypos + b->yoff) + 0.5f);
  11792. float x = (float)tx;
  11793. float y = (float)ty;
  11794. q->x0 = x;
  11795. q->y0 = y;
  11796. q->x1 = x + b->xoff2 - b->xoff;
  11797. q->y1 = y + b->yoff2 - b->yoff;
  11798. } else {
  11799. q->x0 = *xpos + b->xoff;
  11800. q->y0 = *ypos + b->yoff;
  11801. q->x1 = *xpos + b->xoff2;
  11802. q->y1 = *ypos + b->yoff2;
  11803. }
  11804. q->s0 = b->x0 * ipw;
  11805. q->t0 = b->y0 * iph;
  11806. q->s1 = b->x1 * ipw;
  11807. q->t1 = b->y1 * iph;
  11808. *xpos += b->xadvance;
  11809. }
  11810. /* -------------------------------------------------------------
  11811. *
  11812. * FONT BAKING
  11813. *
  11814. * --------------------------------------------------------------*/
  11815. struct nk_font_bake_data {
  11816. struct nk_tt_fontinfo info;
  11817. struct nk_rp_rect *rects;
  11818. struct nk_tt_pack_range *ranges;
  11819. nk_rune range_count;
  11820. };
  11821. struct nk_font_baker {
  11822. struct nk_allocator alloc;
  11823. struct nk_tt_pack_context spc;
  11824. struct nk_font_bake_data *build;
  11825. struct nk_tt_packedchar *packed_chars;
  11826. struct nk_rp_rect *rects;
  11827. struct nk_tt_pack_range *ranges;
  11828. };
  11829. NK_GLOBAL const nk_size nk_rect_align = NK_ALIGNOF(struct nk_rp_rect);
  11830. NK_GLOBAL const nk_size nk_range_align = NK_ALIGNOF(struct nk_tt_pack_range);
  11831. NK_GLOBAL const nk_size nk_char_align = NK_ALIGNOF(struct nk_tt_packedchar);
  11832. NK_GLOBAL const nk_size nk_build_align = NK_ALIGNOF(struct nk_font_bake_data);
  11833. NK_GLOBAL const nk_size nk_baker_align = NK_ALIGNOF(struct nk_font_baker);
  11834. NK_INTERN int
  11835. nk_range_count(const nk_rune *range)
  11836. {
  11837. const nk_rune *iter = range;
  11838. NK_ASSERT(range);
  11839. if (!range) return 0;
  11840. while (*(iter++) != 0);
  11841. return (iter == range) ? 0 : (int)((iter - range)/2);
  11842. }
  11843. NK_INTERN int
  11844. nk_range_glyph_count(const nk_rune *range, int count)
  11845. {
  11846. int i = 0;
  11847. int total_glyphs = 0;
  11848. for (i = 0; i < count; ++i) {
  11849. int diff;
  11850. nk_rune f = range[(i*2)+0];
  11851. nk_rune t = range[(i*2)+1];
  11852. NK_ASSERT(t >= f);
  11853. diff = (int)((t - f) + 1);
  11854. total_glyphs += diff;
  11855. }
  11856. return total_glyphs;
  11857. }
  11858. NK_API const nk_rune*
  11859. nk_font_default_glyph_ranges(void)
  11860. {
  11861. NK_STORAGE const nk_rune ranges[] = {0x0020, 0x00FF, 0};
  11862. return ranges;
  11863. }
  11864. NK_API const nk_rune*
  11865. nk_font_chinese_glyph_ranges(void)
  11866. {
  11867. NK_STORAGE const nk_rune ranges[] = {
  11868. 0x0020, 0x00FF,
  11869. 0x3000, 0x30FF,
  11870. 0x31F0, 0x31FF,
  11871. 0xFF00, 0xFFEF,
  11872. 0x4e00, 0x9FAF,
  11873. 0
  11874. };
  11875. return ranges;
  11876. }
  11877. NK_API const nk_rune*
  11878. nk_font_cyrillic_glyph_ranges(void)
  11879. {
  11880. NK_STORAGE const nk_rune ranges[] = {
  11881. 0x0020, 0x00FF,
  11882. 0x0400, 0x052F,
  11883. 0x2DE0, 0x2DFF,
  11884. 0xA640, 0xA69F,
  11885. 0
  11886. };
  11887. return ranges;
  11888. }
  11889. NK_API const nk_rune*
  11890. nk_font_korean_glyph_ranges(void)
  11891. {
  11892. NK_STORAGE const nk_rune ranges[] = {
  11893. 0x0020, 0x00FF,
  11894. 0x3131, 0x3163,
  11895. 0xAC00, 0xD79D,
  11896. 0
  11897. };
  11898. return ranges;
  11899. }
  11900. NK_INTERN void
  11901. nk_font_baker_memory(nk_size *temp, int *glyph_count,
  11902. struct nk_font_config *config_list, int count)
  11903. {
  11904. int range_count = 0;
  11905. int total_range_count = 0;
  11906. struct nk_font_config *iter, *i;
  11907. NK_ASSERT(config_list);
  11908. NK_ASSERT(glyph_count);
  11909. if (!config_list) {
  11910. *temp = 0;
  11911. *glyph_count = 0;
  11912. return;
  11913. }
  11914. *glyph_count = 0;
  11915. for (iter = config_list; iter; iter = iter->next) {
  11916. i = iter;
  11917. do {if (!i->range) iter->range = nk_font_default_glyph_ranges();
  11918. range_count = nk_range_count(i->range);
  11919. total_range_count += range_count;
  11920. *glyph_count += nk_range_glyph_count(i->range, range_count);
  11921. } while ((i = i->n) != iter);
  11922. }
  11923. *temp = (nk_size)*glyph_count * sizeof(struct nk_rp_rect);
  11924. *temp += (nk_size)total_range_count * sizeof(struct nk_tt_pack_range);
  11925. *temp += (nk_size)*glyph_count * sizeof(struct nk_tt_packedchar);
  11926. *temp += (nk_size)count * sizeof(struct nk_font_bake_data);
  11927. *temp += sizeof(struct nk_font_baker);
  11928. *temp += nk_rect_align + nk_range_align + nk_char_align;
  11929. *temp += nk_build_align + nk_baker_align;
  11930. }
  11931. NK_INTERN struct nk_font_baker*
  11932. nk_font_baker(void *memory, int glyph_count, int count, struct nk_allocator *alloc)
  11933. {
  11934. struct nk_font_baker *baker;
  11935. if (!memory) return 0;
  11936. /* setup baker inside a memory block */
  11937. baker = (struct nk_font_baker*)NK_ALIGN_PTR(memory, nk_baker_align);
  11938. baker->build = (struct nk_font_bake_data*)NK_ALIGN_PTR((baker + 1), nk_build_align);
  11939. baker->packed_chars = (struct nk_tt_packedchar*)NK_ALIGN_PTR((baker->build + count), nk_char_align);
  11940. baker->rects = (struct nk_rp_rect*)NK_ALIGN_PTR((baker->packed_chars + glyph_count), nk_rect_align);
  11941. baker->ranges = (struct nk_tt_pack_range*)NK_ALIGN_PTR((baker->rects + glyph_count), nk_range_align);
  11942. baker->alloc = *alloc;
  11943. return baker;
  11944. }
  11945. NK_INTERN int
  11946. nk_font_bake_pack(struct nk_font_baker *baker,
  11947. nk_size *image_memory, int *width, int *height, struct nk_recti *custom,
  11948. const struct nk_font_config *config_list, int count,
  11949. struct nk_allocator *alloc)
  11950. {
  11951. NK_STORAGE const nk_size max_height = 1024 * 32;
  11952. const struct nk_font_config *config_iter, *it;
  11953. int total_glyph_count = 0;
  11954. int total_range_count = 0;
  11955. int range_count = 0;
  11956. int i = 0;
  11957. NK_ASSERT(image_memory);
  11958. NK_ASSERT(width);
  11959. NK_ASSERT(height);
  11960. NK_ASSERT(config_list);
  11961. NK_ASSERT(count);
  11962. NK_ASSERT(alloc);
  11963. if (!image_memory || !width || !height || !config_list || !count) return nk_false;
  11964. for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
  11965. it = config_iter;
  11966. do {range_count = nk_range_count(it->range);
  11967. total_range_count += range_count;
  11968. total_glyph_count += nk_range_glyph_count(it->range, range_count);
  11969. } while ((it = it->n) != config_iter);
  11970. }
  11971. /* setup font baker from temporary memory */
  11972. for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
  11973. it = config_iter;
  11974. do {if (!nk_tt_InitFont(&baker->build[i++].info, (const unsigned char*)it->ttf_blob, 0))
  11975. return nk_false;
  11976. } while ((it = it->n) != config_iter);
  11977. }
  11978. *height = 0;
  11979. *width = (total_glyph_count > 1000) ? 1024 : 512;
  11980. nk_tt_PackBegin(&baker->spc, 0, (int)*width, (int)max_height, 0, 1, alloc);
  11981. {
  11982. int input_i = 0;
  11983. int range_n = 0;
  11984. int rect_n = 0;
  11985. int char_n = 0;
  11986. if (custom) {
  11987. /* pack custom user data first so it will be in the upper left corner*/
  11988. struct nk_rp_rect custom_space;
  11989. nk_zero(&custom_space, sizeof(custom_space));
  11990. custom_space.w = (nk_rp_coord)(custom->w);
  11991. custom_space.h = (nk_rp_coord)(custom->h);
  11992. nk_tt_PackSetOversampling(&baker->spc, 1, 1);
  11993. nk_rp_pack_rects((struct nk_rp_context*)baker->spc.pack_info, &custom_space, 1);
  11994. *height = NK_MAX(*height, (int)(custom_space.y + custom_space.h));
  11995. custom->x = (short)custom_space.x;
  11996. custom->y = (short)custom_space.y;
  11997. custom->w = (short)custom_space.w;
  11998. custom->h = (short)custom_space.h;
  11999. }
  12000. /* first font pass: pack all glyphs */
  12001. for (input_i = 0, config_iter = config_list; input_i < count && config_iter;
  12002. config_iter = config_iter->next) {
  12003. it = config_iter;
  12004. do {int n = 0;
  12005. int glyph_count;
  12006. const nk_rune *in_range;
  12007. const struct nk_font_config *cfg = it;
  12008. struct nk_font_bake_data *tmp = &baker->build[input_i++];
  12009. /* count glyphs + ranges in current font */
  12010. glyph_count = 0; range_count = 0;
  12011. for (in_range = cfg->range; in_range[0] && in_range[1]; in_range += 2) {
  12012. glyph_count += (int)(in_range[1] - in_range[0]) + 1;
  12013. range_count++;
  12014. }
  12015. /* setup ranges */
  12016. tmp->ranges = baker->ranges + range_n;
  12017. tmp->range_count = (nk_rune)range_count;
  12018. range_n += range_count;
  12019. for (i = 0; i < range_count; ++i) {
  12020. in_range = &cfg->range[i * 2];
  12021. tmp->ranges[i].font_size = cfg->size;
  12022. tmp->ranges[i].first_unicode_codepoint_in_range = (int)in_range[0];
  12023. tmp->ranges[i].num_chars = (int)(in_range[1]- in_range[0]) + 1;
  12024. tmp->ranges[i].chardata_for_range = baker->packed_chars + char_n;
  12025. char_n += tmp->ranges[i].num_chars;
  12026. }
  12027. /* pack */
  12028. tmp->rects = baker->rects + rect_n;
  12029. rect_n += glyph_count;
  12030. nk_tt_PackSetOversampling(&baker->spc, cfg->oversample_h, cfg->oversample_v);
  12031. n = nk_tt_PackFontRangesGatherRects(&baker->spc, &tmp->info,
  12032. tmp->ranges, (int)tmp->range_count, tmp->rects);
  12033. nk_rp_pack_rects((struct nk_rp_context*)baker->spc.pack_info, tmp->rects, (int)n);
  12034. /* texture height */
  12035. for (i = 0; i < n; ++i) {
  12036. if (tmp->rects[i].was_packed)
  12037. *height = NK_MAX(*height, tmp->rects[i].y + tmp->rects[i].h);
  12038. }
  12039. } while ((it = it->n) != config_iter);
  12040. }
  12041. NK_ASSERT(rect_n == total_glyph_count);
  12042. NK_ASSERT(char_n == total_glyph_count);
  12043. NK_ASSERT(range_n == total_range_count);
  12044. }
  12045. *height = (int)nk_round_up_pow2((nk_uint)*height);
  12046. *image_memory = (nk_size)(*width) * (nk_size)(*height);
  12047. return nk_true;
  12048. }
  12049. NK_INTERN void
  12050. nk_font_bake(struct nk_font_baker *baker, void *image_memory, int width, int height,
  12051. struct nk_font_glyph *glyphs, int glyphs_count,
  12052. const struct nk_font_config *config_list, int font_count)
  12053. {
  12054. int input_i = 0;
  12055. nk_rune glyph_n = 0;
  12056. const struct nk_font_config *config_iter;
  12057. const struct nk_font_config *it;
  12058. NK_ASSERT(image_memory);
  12059. NK_ASSERT(width);
  12060. NK_ASSERT(height);
  12061. NK_ASSERT(config_list);
  12062. NK_ASSERT(baker);
  12063. NK_ASSERT(font_count);
  12064. NK_ASSERT(glyphs_count);
  12065. if (!image_memory || !width || !height || !config_list ||
  12066. !font_count || !glyphs || !glyphs_count)
  12067. return;
  12068. /* second font pass: render glyphs */
  12069. nk_zero(image_memory, (nk_size)((nk_size)width * (nk_size)height));
  12070. baker->spc.pixels = (unsigned char*)image_memory;
  12071. baker->spc.height = (int)height;
  12072. for (input_i = 0, config_iter = config_list; input_i < font_count && config_iter;
  12073. config_iter = config_iter->next) {
  12074. it = config_iter;
  12075. do {const struct nk_font_config *cfg = it;
  12076. struct nk_font_bake_data *tmp = &baker->build[input_i++];
  12077. nk_tt_PackSetOversampling(&baker->spc, cfg->oversample_h, cfg->oversample_v);
  12078. nk_tt_PackFontRangesRenderIntoRects(&baker->spc, &tmp->info, tmp->ranges,
  12079. (int)tmp->range_count, tmp->rects, &baker->alloc);
  12080. } while ((it = it->n) != config_iter);
  12081. } nk_tt_PackEnd(&baker->spc, &baker->alloc);
  12082. /* third pass: setup font and glyphs */
  12083. for (input_i = 0, config_iter = config_list; input_i < font_count && config_iter;
  12084. config_iter = config_iter->next) {
  12085. it = config_iter;
  12086. do {nk_size i = 0;
  12087. int char_idx = 0;
  12088. nk_rune glyph_count = 0;
  12089. const struct nk_font_config *cfg = it;
  12090. struct nk_font_bake_data *tmp = &baker->build[input_i++];
  12091. struct nk_baked_font *dst_font = cfg->font;
  12092. float font_scale = nk_tt_ScaleForPixelHeight(&tmp->info, cfg->size);
  12093. int unscaled_ascent, unscaled_descent, unscaled_line_gap;
  12094. nk_tt_GetFontVMetrics(&tmp->info, &unscaled_ascent, &unscaled_descent,
  12095. &unscaled_line_gap);
  12096. /* fill baked font */
  12097. if (!cfg->merge_mode) {
  12098. dst_font->ranges = cfg->range;
  12099. dst_font->height = cfg->size;
  12100. dst_font->ascent = ((float)unscaled_ascent * font_scale);
  12101. dst_font->descent = ((float)unscaled_descent * font_scale);
  12102. dst_font->glyph_offset = glyph_n;
  12103. // Need to zero this, or it will carry over from a previous
  12104. // bake, and cause a segfault when accessing glyphs[].
  12105. dst_font->glyph_count = 0;
  12106. }
  12107. /* fill own baked font glyph array */
  12108. for (i = 0; i < tmp->range_count; ++i) {
  12109. struct nk_tt_pack_range *range = &tmp->ranges[i];
  12110. for (char_idx = 0; char_idx < range->num_chars; char_idx++)
  12111. {
  12112. nk_rune codepoint = 0;
  12113. float dummy_x = 0, dummy_y = 0;
  12114. struct nk_tt_aligned_quad q;
  12115. struct nk_font_glyph *glyph;
  12116. /* query glyph bounds from stb_truetype */
  12117. const struct nk_tt_packedchar *pc = &range->chardata_for_range[char_idx];
  12118. if (!pc->x0 && !pc->x1 && !pc->y0 && !pc->y1) continue;
  12119. codepoint = (nk_rune)(range->first_unicode_codepoint_in_range + char_idx);
  12120. nk_tt_GetPackedQuad(range->chardata_for_range, (int)width,
  12121. (int)height, char_idx, &dummy_x, &dummy_y, &q, 0);
  12122. /* fill own glyph type with data */
  12123. glyph = &glyphs[dst_font->glyph_offset + dst_font->glyph_count + (unsigned int)glyph_count];
  12124. glyph->codepoint = codepoint;
  12125. glyph->x0 = q.x0; glyph->y0 = q.y0;
  12126. glyph->x1 = q.x1; glyph->y1 = q.y1;
  12127. glyph->y0 += (dst_font->ascent + 0.5f);
  12128. glyph->y1 += (dst_font->ascent + 0.5f);
  12129. glyph->w = glyph->x1 - glyph->x0 + 0.5f;
  12130. glyph->h = glyph->y1 - glyph->y0;
  12131. if (cfg->coord_type == NK_COORD_PIXEL) {
  12132. glyph->u0 = q.s0 * (float)width;
  12133. glyph->v0 = q.t0 * (float)height;
  12134. glyph->u1 = q.s1 * (float)width;
  12135. glyph->v1 = q.t1 * (float)height;
  12136. } else {
  12137. glyph->u0 = q.s0;
  12138. glyph->v0 = q.t0;
  12139. glyph->u1 = q.s1;
  12140. glyph->v1 = q.t1;
  12141. }
  12142. glyph->xadvance = (pc->xadvance + cfg->spacing.x);
  12143. if (cfg->pixel_snap)
  12144. glyph->xadvance = (float)(int)(glyph->xadvance + 0.5f);
  12145. glyph_count++;
  12146. }
  12147. }
  12148. dst_font->glyph_count += glyph_count;
  12149. glyph_n += glyph_count;
  12150. } while ((it = it->n) != config_iter);
  12151. }
  12152. }
  12153. NK_INTERN void
  12154. nk_font_bake_custom_data(void *img_memory, int img_width, int img_height,
  12155. struct nk_recti img_dst, const char *texture_data_mask, int tex_width,
  12156. int tex_height, char white, char black)
  12157. {
  12158. nk_byte *pixels;
  12159. int y = 0;
  12160. int x = 0;
  12161. int n = 0;
  12162. NK_ASSERT(img_memory);
  12163. NK_ASSERT(img_width);
  12164. NK_ASSERT(img_height);
  12165. NK_ASSERT(texture_data_mask);
  12166. NK_UNUSED(tex_height);
  12167. if (!img_memory || !img_width || !img_height || !texture_data_mask)
  12168. return;
  12169. pixels = (nk_byte*)img_memory;
  12170. for (y = 0, n = 0; y < tex_height; ++y) {
  12171. for (x = 0; x < tex_width; ++x, ++n) {
  12172. const int off0 = ((img_dst.x + x) + (img_dst.y + y) * img_width);
  12173. const int off1 = off0 + 1 + tex_width;
  12174. pixels[off0] = (texture_data_mask[n] == white) ? 0xFF : 0x00;
  12175. pixels[off1] = (texture_data_mask[n] == black) ? 0xFF : 0x00;
  12176. }
  12177. }
  12178. }
  12179. NK_INTERN void
  12180. nk_font_bake_convert(void *out_memory, int img_width, int img_height,
  12181. const void *in_memory)
  12182. {
  12183. int n = 0;
  12184. nk_rune *dst;
  12185. const nk_byte *src;
  12186. NK_ASSERT(out_memory);
  12187. NK_ASSERT(in_memory);
  12188. NK_ASSERT(img_width);
  12189. NK_ASSERT(img_height);
  12190. if (!out_memory || !in_memory || !img_height || !img_width) return;
  12191. dst = (nk_rune*)out_memory;
  12192. src = (const nk_byte*)in_memory;
  12193. for (n = (int)(img_width * img_height); n > 0; n--)
  12194. *dst++ = ((nk_rune)(*src++) << 24) | 0x00FFFFFF;
  12195. }
  12196. /* -------------------------------------------------------------
  12197. *
  12198. * FONT
  12199. *
  12200. * --------------------------------------------------------------*/
  12201. NK_INTERN float
  12202. nk_font_text_width(nk_handle handle, float height, const char *text, int len)
  12203. {
  12204. nk_rune unicode;
  12205. int text_len = 0;
  12206. float text_width = 0;
  12207. int glyph_len = 0;
  12208. float scale = 0;
  12209. struct nk_font *font = (struct nk_font*)handle.ptr;
  12210. NK_ASSERT(font);
  12211. NK_ASSERT(font->glyphs);
  12212. if (!font || !text || !len)
  12213. return 0;
  12214. scale = height/font->info.height;
  12215. glyph_len = text_len = nk_utf_decode(text, &unicode, (int)len);
  12216. if (!glyph_len) return 0;
  12217. while (text_len <= (int)len && glyph_len) {
  12218. const struct nk_font_glyph *g;
  12219. if (unicode == NK_UTF_INVALID) break;
  12220. /* query currently drawn glyph information */
  12221. g = nk_font_find_glyph(font, unicode);
  12222. text_width += g->xadvance * scale;
  12223. /* offset next glyph */
  12224. glyph_len = nk_utf_decode(text + text_len, &unicode, (int)len - text_len);
  12225. text_len += glyph_len;
  12226. }
  12227. return text_width;
  12228. }
  12229. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  12230. NK_INTERN void
  12231. nk_font_query_font_glyph(nk_handle handle, float height,
  12232. struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
  12233. {
  12234. float scale;
  12235. const struct nk_font_glyph *g;
  12236. struct nk_font *font;
  12237. NK_ASSERT(glyph);
  12238. NK_UNUSED(next_codepoint);
  12239. font = (struct nk_font*)handle.ptr;
  12240. NK_ASSERT(font);
  12241. NK_ASSERT(font->glyphs);
  12242. if (!font || !glyph)
  12243. return;
  12244. scale = height/font->info.height;
  12245. g = nk_font_find_glyph(font, codepoint);
  12246. glyph->width = (g->x1 - g->x0) * scale;
  12247. glyph->height = (g->y1 - g->y0) * scale;
  12248. glyph->offset = nk_vec2(g->x0 * scale, g->y0 * scale);
  12249. glyph->xadvance = (g->xadvance * scale);
  12250. glyph->uv[0] = nk_vec2(g->u0, g->v0);
  12251. glyph->uv[1] = nk_vec2(g->u1, g->v1);
  12252. }
  12253. #endif
  12254. NK_API const struct nk_font_glyph*
  12255. nk_font_find_glyph(struct nk_font *font, nk_rune unicode)
  12256. {
  12257. int i = 0;
  12258. int count;
  12259. int total_glyphs = 0;
  12260. const struct nk_font_glyph *glyph = 0;
  12261. const struct nk_font_config *iter = 0;
  12262. NK_ASSERT(font);
  12263. NK_ASSERT(font->glyphs);
  12264. NK_ASSERT(font->info.ranges);
  12265. if (!font || !font->glyphs) return 0;
  12266. glyph = font->fallback;
  12267. iter = font->config;
  12268. do {count = nk_range_count(iter->range);
  12269. for (i = 0; i < count; ++i) {
  12270. nk_rune f = iter->range[(i*2)+0];
  12271. nk_rune t = iter->range[(i*2)+1];
  12272. int diff = (int)((t - f) + 1);
  12273. if (unicode >= f && unicode <= t)
  12274. return &font->glyphs[((nk_rune)total_glyphs + (unicode - f))];
  12275. total_glyphs += diff;
  12276. }
  12277. } while ((iter = iter->n) != font->config);
  12278. return glyph;
  12279. }
  12280. NK_INTERN void
  12281. nk_font_init(struct nk_font *font, float pixel_height,
  12282. nk_rune fallback_codepoint, struct nk_font_glyph *glyphs,
  12283. const struct nk_baked_font *baked_font, nk_handle atlas)
  12284. {
  12285. struct nk_baked_font baked;
  12286. NK_ASSERT(font);
  12287. NK_ASSERT(glyphs);
  12288. NK_ASSERT(baked_font);
  12289. if (!font || !glyphs || !baked_font)
  12290. return;
  12291. baked = *baked_font;
  12292. font->fallback = 0;
  12293. font->info = baked;
  12294. font->scale = (float)pixel_height / (float)font->info.height;
  12295. font->glyphs = &glyphs[baked_font->glyph_offset];
  12296. font->texture = atlas;
  12297. font->fallback_codepoint = fallback_codepoint;
  12298. font->fallback = nk_font_find_glyph(font, fallback_codepoint);
  12299. font->handle.height = font->info.height * font->scale;
  12300. font->handle.width = nk_font_text_width;
  12301. font->handle.userdata.ptr = font;
  12302. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  12303. font->handle.query = nk_font_query_font_glyph;
  12304. font->handle.texture = font->texture;
  12305. #endif
  12306. }
  12307. /* ---------------------------------------------------------------------------
  12308. *
  12309. * DEFAULT FONT
  12310. *
  12311. * ProggyClean.ttf
  12312. * Copyright (c) 2004, 2005 Tristan Grimmer
  12313. * MIT license (see License.txt in http://www.upperbounds.net/download/ProggyClean.ttf.zip)
  12314. * Download and more information at http://upperbounds.net
  12315. *-----------------------------------------------------------------------------*/
  12316. #ifdef __clang__
  12317. #pragma clang diagnostic push
  12318. #pragma clang diagnostic ignored "-Woverlength-strings"
  12319. #elif defined(__GNUC__) || defined(__GNUG__)
  12320. #pragma GCC diagnostic push
  12321. #pragma GCC diagnostic ignored "-Woverlength-strings"
  12322. #endif
  12323. #ifdef NK_INCLUDE_DEFAULT_FONT
  12324. NK_GLOBAL const char nk_proggy_clean_ttf_compressed_data_base85[11980+1] =
  12325. "7])#######hV0qs'/###[),##/l:$#Q6>##5[n42>c-TH`->>#/e>11NNV=Bv(*:.F?uu#(gRU.o0XGH`$vhLG1hxt9?W`#,5LsCp#-i>.r$<$6pD>Lb';9Crc6tgXmKVeU2cD4Eo3R/"
  12326. "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#"
  12327. "`8ND>Qo#t'X#(v#Y9w0#1D$CIf;W'#pWUPXOuxXuU(H9M(1<q-UE31#^-V'8IRUo7Qf./L>=Ke$$'5F%)]0^#[email protected]<r:QLtFsLcL6##lOj)#.Y5<-R&KgLwqJfLgN&;Q?gI^#DY2uL"
  12328. "i@^rMl9t=cWq6##weg>$FBjVQTSDgEKnIS7EM9>ZY9w0#L;>>#Mx&4Mvt//L[MkA#[email protected]'[0#7RL_&#w+F%HtG9M#XL`N&.,GM4Pg;-<nLENhvx>-VsM.M0rJfLH2eTM`*oJMHRC`N"
  12329. "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"
  12330. "*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)"
  12331. "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"
  12332. "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."
  12333. "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"
  12334. "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(,)"
  12335. "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#"
  12336. "'/###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"
  12337. "_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"
  12338. "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/"
  12339. "/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"
  12340. "%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&#"
  12341. "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)("
  12342. "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"
  12343. "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"
  12344. "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^-"
  12345. "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-"
  12346. "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"
  12347. "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%"
  12348. "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]"
  12349. "%(?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"
  12350. "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:"
  12351. "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("
  12352. "$/V,;(kXZejWO`<[5?\?ewY(*9=%wDc;,u<'9t3W-(H1th3+G]ucQ]kLs7df($/*JL]@*t7Bu_G3_7mp7<[email protected];x3B0lqp7Hf,^Ze7-##@/c58Mo(3;knp0%)A7?-W+eI'o8)b<"
  12353. "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'?"
  12354. "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/;"
  12355. ")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"
  12356. "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("
  12357. "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"
  12358. "bIu)'Z,*[>br5fX^:FPAWr-m2KgL<LUN098kTF&#lvo58=/vjDo;.;)Ka*hLR#/k=rKbxuV`>Q_nN6'8uTG&#1T5g)uLv:873UpTLgH+#FgpH'_o1780Ph8KmxQJ8#H72L4@768@Tm&Q"
  12359. "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<-"
  12360. "V8J'(1)G][68hW$5'q[GC&5j`TE?m'esFGNRM)j,ffZ?-qx8;->g4t*:CIP/[Qap7/9'#(1sao7w-.qNUdkJ)tCF&#B^;[email protected]$m%#QvQS8U@)2Z+3K:AKM5i"
  12361. "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"
  12362. ".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@@"
  12363. "$&)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*"
  12364. "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"
  12365. "@-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&#"
  12366. "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#"
  12367. "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"
  12368. "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"
  12369. "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&#"
  12370. "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"
  12371. ":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+"
  12372. "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*"
  12373. "$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"
  12374. ":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"
  12375. "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"
  12376. "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"
  12377. "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"
  12378. ":^#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@&]>"
  12379. "_>@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%"
  12380. "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;"
  12381. "^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:"
  12382. "+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%"
  12383. "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-"
  12384. "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*"
  12385. "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"
  12386. "8kD2)2fU/M#$e.)T4,_=8hLim[&);?UkK'-x?'(:siIfL<$pFM`i<?%W(mGDHM%>iWP,##P`%/L<eXi:@Z9C.7o=@(pXdAO/NLQ8lPl+HPOQa8wD8=^GlPa8TKI1CjhsCTSLJM'/Wl>-"
  12387. "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`"
  12388. "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]%/"
  12389. "+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"
  12390. "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"
  12391. "?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"
  12392. "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"
  12393. ">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'>"
  12394. "[%$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"
  12395. "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#"
  12396. "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$"
  12397. "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,)"
  12398. "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"
  12399. "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"
  12400. "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"
  12401. "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(#"
  12402. ";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&>"
  12403. "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#"
  12404. "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"
  12405. "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#"
  12406. "/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-#"
  12407. "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#"
  12408. "TmD<#%JSMFove:CTBEXI:<eh2g)B,3h2^G3i;#d3jD>)4kMYD4lVu`4m`:&5niUA5@(A5BA1]PBB:xlBCC=2CDLXMCEUtiCf&0g2'tN?PGT4CPGT4CPGT4CPGT4CPGT4CPGT4CPGT4CP"
  12409. "GT4CPGT4CPGT4CPGT4CPGT4CPGT4CP-qekC`.9kEg^+F$kwViFJTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5o,^<-28ZI'O?;xp"
  12410. "O?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xp;7q-#lLYI:xvD=#";
  12411. #endif /* NK_INCLUDE_DEFAULT_FONT */
  12412. #define NK_CURSOR_DATA_W 90
  12413. #define NK_CURSOR_DATA_H 27
  12414. NK_GLOBAL const char nk_custom_cursor_data[NK_CURSOR_DATA_W * NK_CURSOR_DATA_H + 1] =
  12415. {
  12416. "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX"
  12417. "..- -X.....X- X.X - X.X -X.....X - X.....X"
  12418. "--- -XXX.XXX- X...X - X...X -X....X - X....X"
  12419. "X - X.X - X.....X - X.....X -X...X - X...X"
  12420. "XX - X.X -X.......X- X.......X -X..X.X - X.X..X"
  12421. "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X"
  12422. "X..X - X.X - X.X - X.X -XX X.X - X.X XX"
  12423. "X...X - X.X - X.X - XX X.X XX - X.X - X.X "
  12424. "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X "
  12425. "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X "
  12426. "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X "
  12427. "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X "
  12428. "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X "
  12429. "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X "
  12430. "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X "
  12431. "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X "
  12432. "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX "
  12433. "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------"
  12434. "X.X X..X - -X.......X- X.......X - XX XX - "
  12435. "XX X..X - - X.....X - X.....X - X.X X.X - "
  12436. " X..X - X...X - X...X - X..X X..X - "
  12437. " XX - X.X - X.X - X...XXXXXXXXXXXXX...X - "
  12438. "------------ - X - X -X.....................X- "
  12439. " ----------------------------------- X...XXXXXXXXXXXXX...X - "
  12440. " - X..X X..X - "
  12441. " - X.X X.X - "
  12442. " - XX XX - "
  12443. };
  12444. #ifdef __clang__
  12445. #pragma clang diagnostic pop
  12446. #elif defined(__GNUC__) || defined(__GNUG__)
  12447. #pragma GCC diagnostic pop
  12448. #endif
  12449. NK_GLOBAL unsigned char *nk__barrier;
  12450. NK_GLOBAL unsigned char *nk__barrier2;
  12451. NK_GLOBAL unsigned char *nk__barrier3;
  12452. NK_GLOBAL unsigned char *nk__barrier4;
  12453. NK_GLOBAL unsigned char *nk__dout;
  12454. NK_INTERN unsigned int
  12455. nk_decompress_length(unsigned char *input)
  12456. {
  12457. return (unsigned int)((input[8] << 24) + (input[9] << 16) + (input[10] << 8) + input[11]);
  12458. }
  12459. NK_INTERN void
  12460. nk__match(unsigned char *data, unsigned int length)
  12461. {
  12462. /* INVERSE of memmove... write each byte before copying the next...*/
  12463. NK_ASSERT (nk__dout + length <= nk__barrier);
  12464. if (nk__dout + length > nk__barrier) { nk__dout += length; return; }
  12465. if (data < nk__barrier4) { nk__dout = nk__barrier+1; return; }
  12466. while (length--) *nk__dout++ = *data++;
  12467. }
  12468. NK_INTERN void
  12469. nk__lit(unsigned char *data, unsigned int length)
  12470. {
  12471. NK_ASSERT (nk__dout + length <= nk__barrier);
  12472. if (nk__dout + length > nk__barrier) { nk__dout += length; return; }
  12473. if (data < nk__barrier2) { nk__dout = nk__barrier+1; return; }
  12474. NK_MEMCPY(nk__dout, data, length);
  12475. nk__dout += length;
  12476. }
  12477. NK_INTERN unsigned char*
  12478. nk_decompress_token(unsigned char *i)
  12479. {
  12480. #define nk__in2(x) ((i[x] << 8) + i[(x)+1])
  12481. #define nk__in3(x) ((i[x] << 16) + nk__in2((x)+1))
  12482. #define nk__in4(x) ((i[x] << 24) + nk__in3((x)+1))
  12483. if (*i >= 0x20) { /* use fewer if's for cases that expand small */
  12484. if (*i >= 0x80) nk__match(nk__dout-i[1]-1, (unsigned int)i[0] - 0x80 + 1), i += 2;
  12485. else if (*i >= 0x40) nk__match(nk__dout-(nk__in2(0) - 0x4000 + 1), (unsigned int)i[2]+1), i += 3;
  12486. else /* *i >= 0x20 */ nk__lit(i+1, (unsigned int)i[0] - 0x20 + 1), i += 1 + (i[0] - 0x20 + 1);
  12487. } else { /* more ifs for cases that expand large, since overhead is amortized */
  12488. if (*i >= 0x18) nk__match(nk__dout-(unsigned int)(nk__in3(0) - 0x180000 + 1), (unsigned int)i[3]+1), i += 4;
  12489. else if (*i >= 0x10) nk__match(nk__dout-(unsigned int)(nk__in3(0) - 0x100000 + 1), (unsigned int)nk__in2(3)+1), i += 5;
  12490. else if (*i >= 0x08) nk__lit(i+2, (unsigned int)nk__in2(0) - 0x0800 + 1), i += 2 + (nk__in2(0) - 0x0800 + 1);
  12491. else if (*i == 0x07) nk__lit(i+3, (unsigned int)nk__in2(1) + 1), i += 3 + (nk__in2(1) + 1);
  12492. else if (*i == 0x06) nk__match(nk__dout-(unsigned int)(nk__in3(1)+1), i[4]+1u), i += 5;
  12493. else if (*i == 0x04) nk__match(nk__dout-(unsigned int)(nk__in3(1)+1), (unsigned int)nk__in2(4)+1u), i += 6;
  12494. }
  12495. return i;
  12496. }
  12497. NK_INTERN unsigned int
  12498. nk_adler32(unsigned int adler32, unsigned char *buffer, unsigned int buflen)
  12499. {
  12500. const unsigned long ADLER_MOD = 65521;
  12501. unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16;
  12502. unsigned long blocklen, i;
  12503. blocklen = buflen % 5552;
  12504. while (buflen) {
  12505. for (i=0; i + 7 < blocklen; i += 8) {
  12506. s1 += buffer[0]; s2 += s1;
  12507. s1 += buffer[1]; s2 += s1;
  12508. s1 += buffer[2]; s2 += s1;
  12509. s1 += buffer[3]; s2 += s1;
  12510. s1 += buffer[4]; s2 += s1;
  12511. s1 += buffer[5]; s2 += s1;
  12512. s1 += buffer[6]; s2 += s1;
  12513. s1 += buffer[7]; s2 += s1;
  12514. buffer += 8;
  12515. }
  12516. for (; i < blocklen; ++i) {
  12517. s1 += *buffer++; s2 += s1;
  12518. }
  12519. s1 %= ADLER_MOD; s2 %= ADLER_MOD;
  12520. buflen -= (unsigned int)blocklen;
  12521. blocklen = 5552;
  12522. }
  12523. return (unsigned int)(s2 << 16) + (unsigned int)s1;
  12524. }
  12525. NK_INTERN unsigned int
  12526. nk_decompress(unsigned char *output, unsigned char *i, unsigned int length)
  12527. {
  12528. unsigned int olen;
  12529. if (nk__in4(0) != 0x57bC0000) return 0;
  12530. if (nk__in4(4) != 0) return 0; /* error! stream is > 4GB */
  12531. olen = nk_decompress_length(i);
  12532. nk__barrier2 = i;
  12533. nk__barrier3 = i+length;
  12534. nk__barrier = output + olen;
  12535. nk__barrier4 = output;
  12536. i += 16;
  12537. nk__dout = output;
  12538. for (;;) {
  12539. unsigned char *old_i = i;
  12540. i = nk_decompress_token(i);
  12541. if (i == old_i) {
  12542. if (*i == 0x05 && i[1] == 0xfa) {
  12543. NK_ASSERT(nk__dout == output + olen);
  12544. if (nk__dout != output + olen) return 0;
  12545. if (nk_adler32(1, output, olen) != (unsigned int) nk__in4(2))
  12546. return 0;
  12547. return olen;
  12548. } else {
  12549. NK_ASSERT(0); /* NOTREACHED */
  12550. return 0;
  12551. }
  12552. }
  12553. NK_ASSERT(nk__dout <= output + olen);
  12554. if (nk__dout > output + olen)
  12555. return 0;
  12556. }
  12557. }
  12558. NK_INTERN unsigned int
  12559. nk_decode_85_byte(char c)
  12560. {
  12561. return (unsigned int)((c >= '\\') ? c-36 : c-35);
  12562. }
  12563. NK_INTERN void
  12564. nk_decode_85(unsigned char* dst, const unsigned char* src)
  12565. {
  12566. while (*src)
  12567. {
  12568. unsigned int tmp =
  12569. nk_decode_85_byte((char)src[0]) +
  12570. 85 * (nk_decode_85_byte((char)src[1]) +
  12571. 85 * (nk_decode_85_byte((char)src[2]) +
  12572. 85 * (nk_decode_85_byte((char)src[3]) +
  12573. 85 * nk_decode_85_byte((char)src[4]))));
  12574. /* we can't assume little-endianess. */
  12575. dst[0] = (unsigned char)((tmp >> 0) & 0xFF);
  12576. dst[1] = (unsigned char)((tmp >> 8) & 0xFF);
  12577. dst[2] = (unsigned char)((tmp >> 16) & 0xFF);
  12578. dst[3] = (unsigned char)((tmp >> 24) & 0xFF);
  12579. src += 5;
  12580. dst += 4;
  12581. }
  12582. }
  12583. /* -------------------------------------------------------------
  12584. *
  12585. * FONT ATLAS
  12586. *
  12587. * --------------------------------------------------------------*/
  12588. NK_API struct nk_font_config
  12589. nk_font_config(float pixel_height)
  12590. {
  12591. struct nk_font_config cfg;
  12592. nk_zero_struct(cfg);
  12593. cfg.ttf_blob = 0;
  12594. cfg.ttf_size = 0;
  12595. cfg.ttf_data_owned_by_atlas = 0;
  12596. cfg.size = pixel_height;
  12597. cfg.oversample_h = 3;
  12598. cfg.oversample_v = 1;
  12599. cfg.pixel_snap = 0;
  12600. cfg.coord_type = NK_COORD_UV;
  12601. cfg.spacing = nk_vec2(0,0);
  12602. cfg.range = nk_font_default_glyph_ranges();
  12603. cfg.merge_mode = 0;
  12604. cfg.fallback_glyph = '?';
  12605. cfg.font = 0;
  12606. cfg.n = 0;
  12607. return cfg;
  12608. }
  12609. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  12610. NK_API void
  12611. nk_font_atlas_init_default(struct nk_font_atlas *atlas)
  12612. {
  12613. NK_ASSERT(atlas);
  12614. if (!atlas) return;
  12615. nk_zero_struct(*atlas);
  12616. atlas->temporary.userdata.ptr = 0;
  12617. atlas->temporary.alloc = nk_malloc;
  12618. atlas->temporary.free = nk_mfree;
  12619. atlas->permanent.userdata.ptr = 0;
  12620. atlas->permanent.alloc = nk_malloc;
  12621. atlas->permanent.free = nk_mfree;
  12622. }
  12623. #endif
  12624. NK_API void
  12625. nk_font_atlas_init(struct nk_font_atlas *atlas, struct nk_allocator *alloc)
  12626. {
  12627. NK_ASSERT(atlas);
  12628. NK_ASSERT(alloc);
  12629. if (!atlas || !alloc) return;
  12630. nk_zero_struct(*atlas);
  12631. atlas->permanent = *alloc;
  12632. atlas->temporary = *alloc;
  12633. }
  12634. NK_API void
  12635. nk_font_atlas_init_custom(struct nk_font_atlas *atlas,
  12636. struct nk_allocator *permanent, struct nk_allocator *temporary)
  12637. {
  12638. NK_ASSERT(atlas);
  12639. NK_ASSERT(permanent);
  12640. NK_ASSERT(temporary);
  12641. if (!atlas || !permanent || !temporary) return;
  12642. nk_zero_struct(*atlas);
  12643. atlas->permanent = *permanent;
  12644. atlas->temporary = *temporary;
  12645. }
  12646. NK_API void
  12647. nk_font_atlas_begin(struct nk_font_atlas *atlas)
  12648. {
  12649. NK_ASSERT(atlas);
  12650. NK_ASSERT(atlas->temporary.alloc && atlas->temporary.free);
  12651. NK_ASSERT(atlas->permanent.alloc && atlas->permanent.free);
  12652. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free ||
  12653. !atlas->temporary.alloc || !atlas->temporary.free) return;
  12654. if (atlas->glyphs) {
  12655. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  12656. atlas->glyphs = 0;
  12657. }
  12658. if (atlas->pixel) {
  12659. atlas->permanent.free(atlas->permanent.userdata, atlas->pixel);
  12660. atlas->pixel = 0;
  12661. }
  12662. }
  12663. NK_API struct nk_font*
  12664. nk_font_atlas_add(struct nk_font_atlas *atlas, const struct nk_font_config *config)
  12665. {
  12666. struct nk_font *font = 0;
  12667. struct nk_font_config *cfg;
  12668. NK_ASSERT(atlas);
  12669. NK_ASSERT(atlas->permanent.alloc);
  12670. NK_ASSERT(atlas->permanent.free);
  12671. NK_ASSERT(atlas->temporary.alloc);
  12672. NK_ASSERT(atlas->temporary.free);
  12673. NK_ASSERT(config);
  12674. NK_ASSERT(config->ttf_blob);
  12675. NK_ASSERT(config->ttf_size);
  12676. NK_ASSERT(config->size > 0.0f);
  12677. if (!atlas || !config || !config->ttf_blob || !config->ttf_size || config->size <= 0.0f||
  12678. !atlas->permanent.alloc || !atlas->permanent.free ||
  12679. !atlas->temporary.alloc || !atlas->temporary.free)
  12680. return 0;
  12681. /* allocate font config */
  12682. cfg = (struct nk_font_config*)
  12683. atlas->permanent.alloc(atlas->permanent.userdata,0, sizeof(struct nk_font_config));
  12684. NK_MEMCPY(cfg, config, sizeof(*config));
  12685. cfg->n = cfg;
  12686. cfg->p = cfg;
  12687. if (!config->merge_mode) {
  12688. /* insert font config into list */
  12689. if (!atlas->config) {
  12690. atlas->config = cfg;
  12691. cfg->next = 0;
  12692. } else {
  12693. struct nk_font_config *i = atlas->config;
  12694. while (i->next) i = i->next;
  12695. i->next = cfg;
  12696. cfg->next = 0;
  12697. }
  12698. /* allocate new font */
  12699. font = (struct nk_font*)
  12700. atlas->permanent.alloc(atlas->permanent.userdata,0, sizeof(struct nk_font));
  12701. NK_ASSERT(font);
  12702. nk_zero(font, sizeof(*font));
  12703. if (!font) return 0;
  12704. font->config = cfg;
  12705. /* insert font into list */
  12706. if (!atlas->fonts) {
  12707. atlas->fonts = font;
  12708. font->next = 0;
  12709. } else {
  12710. struct nk_font *i = atlas->fonts;
  12711. while (i->next) i = i->next;
  12712. i->next = font;
  12713. font->next = 0;
  12714. }
  12715. cfg->font = &font->info;
  12716. } else {
  12717. /* extend previously added font */
  12718. struct nk_font *f = 0;
  12719. struct nk_font_config *c = 0;
  12720. NK_ASSERT(atlas->font_num);
  12721. f = atlas->fonts;
  12722. c = f->config;
  12723. cfg->font = &f->info;
  12724. cfg->n = c;
  12725. cfg->p = c->p;
  12726. c->p->n = cfg;
  12727. c->p = cfg;
  12728. }
  12729. /* create own copy of .TTF font blob */
  12730. if (!config->ttf_data_owned_by_atlas) {
  12731. cfg->ttf_blob = atlas->permanent.alloc(atlas->permanent.userdata,0, cfg->ttf_size);
  12732. NK_ASSERT(cfg->ttf_blob);
  12733. if (!cfg->ttf_blob) {
  12734. atlas->font_num++;
  12735. return 0;
  12736. }
  12737. NK_MEMCPY(cfg->ttf_blob, config->ttf_blob, cfg->ttf_size);
  12738. cfg->ttf_data_owned_by_atlas = 1;
  12739. }
  12740. atlas->font_num++;
  12741. return font;
  12742. }
  12743. NK_API struct nk_font*
  12744. nk_font_atlas_add_from_memory(struct nk_font_atlas *atlas, void *memory,
  12745. nk_size size, float height, const struct nk_font_config *config)
  12746. {
  12747. struct nk_font_config cfg;
  12748. NK_ASSERT(memory);
  12749. NK_ASSERT(size);
  12750. NK_ASSERT(atlas);
  12751. NK_ASSERT(atlas->temporary.alloc);
  12752. NK_ASSERT(atlas->temporary.free);
  12753. NK_ASSERT(atlas->permanent.alloc);
  12754. NK_ASSERT(atlas->permanent.free);
  12755. if (!atlas || !atlas->temporary.alloc || !atlas->temporary.free || !memory || !size ||
  12756. !atlas->permanent.alloc || !atlas->permanent.free)
  12757. return 0;
  12758. cfg = (config) ? *config: nk_font_config(height);
  12759. cfg.ttf_blob = memory;
  12760. cfg.ttf_size = size;
  12761. cfg.size = height;
  12762. cfg.ttf_data_owned_by_atlas = 0;
  12763. return nk_font_atlas_add(atlas, &cfg);
  12764. }
  12765. #ifdef NK_INCLUDE_STANDARD_IO
  12766. NK_API struct nk_font*
  12767. nk_font_atlas_add_from_file(struct nk_font_atlas *atlas, const char *file_path,
  12768. float height, const struct nk_font_config *config)
  12769. {
  12770. nk_size size;
  12771. char *memory;
  12772. struct nk_font_config cfg;
  12773. NK_ASSERT(atlas);
  12774. NK_ASSERT(atlas->temporary.alloc);
  12775. NK_ASSERT(atlas->temporary.free);
  12776. NK_ASSERT(atlas->permanent.alloc);
  12777. NK_ASSERT(atlas->permanent.free);
  12778. if (!atlas || !file_path) return 0;
  12779. memory = nk_file_load(file_path, &size, &atlas->permanent);
  12780. if (!memory) return 0;
  12781. cfg = (config) ? *config: nk_font_config(height);
  12782. cfg.ttf_blob = memory;
  12783. cfg.ttf_size = size;
  12784. cfg.size = height;
  12785. cfg.ttf_data_owned_by_atlas = 1;
  12786. return nk_font_atlas_add(atlas, &cfg);
  12787. }
  12788. #endif
  12789. NK_API struct nk_font*
  12790. nk_font_atlas_add_compressed(struct nk_font_atlas *atlas,
  12791. void *compressed_data, nk_size compressed_size, float height,
  12792. const struct nk_font_config *config)
  12793. {
  12794. unsigned int decompressed_size;
  12795. void *decompressed_data;
  12796. struct nk_font_config cfg;
  12797. NK_ASSERT(atlas);
  12798. NK_ASSERT(atlas->temporary.alloc);
  12799. NK_ASSERT(atlas->temporary.free);
  12800. NK_ASSERT(atlas->permanent.alloc);
  12801. NK_ASSERT(atlas->permanent.free);
  12802. NK_ASSERT(compressed_data);
  12803. NK_ASSERT(compressed_size);
  12804. if (!atlas || !compressed_data || !atlas->temporary.alloc || !atlas->temporary.free ||
  12805. !atlas->permanent.alloc || !atlas->permanent.free)
  12806. return 0;
  12807. decompressed_size = nk_decompress_length((unsigned char*)compressed_data);
  12808. decompressed_data = atlas->permanent.alloc(atlas->permanent.userdata,0,decompressed_size);
  12809. NK_ASSERT(decompressed_data);
  12810. if (!decompressed_data) return 0;
  12811. nk_decompress((unsigned char*)decompressed_data, (unsigned char*)compressed_data,
  12812. (unsigned int)compressed_size);
  12813. cfg = (config) ? *config: nk_font_config(height);
  12814. cfg.ttf_blob = decompressed_data;
  12815. cfg.ttf_size = decompressed_size;
  12816. cfg.size = height;
  12817. cfg.ttf_data_owned_by_atlas = 1;
  12818. return nk_font_atlas_add(atlas, &cfg);
  12819. }
  12820. NK_API struct nk_font*
  12821. nk_font_atlas_add_compressed_base85(struct nk_font_atlas *atlas,
  12822. const char *data_base85, float height, const struct nk_font_config *config)
  12823. {
  12824. int compressed_size;
  12825. void *compressed_data;
  12826. struct nk_font *font;
  12827. NK_ASSERT(atlas);
  12828. NK_ASSERT(atlas->temporary.alloc);
  12829. NK_ASSERT(atlas->temporary.free);
  12830. NK_ASSERT(atlas->permanent.alloc);
  12831. NK_ASSERT(atlas->permanent.free);
  12832. NK_ASSERT(data_base85);
  12833. if (!atlas || !data_base85 || !atlas->temporary.alloc || !atlas->temporary.free ||
  12834. !atlas->permanent.alloc || !atlas->permanent.free)
  12835. return 0;
  12836. compressed_size = (((int)nk_strlen(data_base85) + 4) / 5) * 4;
  12837. compressed_data = atlas->temporary.alloc(atlas->temporary.userdata,0, (nk_size)compressed_size);
  12838. NK_ASSERT(compressed_data);
  12839. if (!compressed_data) return 0;
  12840. nk_decode_85((unsigned char*)compressed_data, (const unsigned char*)data_base85);
  12841. font = nk_font_atlas_add_compressed(atlas, compressed_data,
  12842. (nk_size)compressed_size, height, config);
  12843. atlas->temporary.free(atlas->temporary.userdata, compressed_data);
  12844. return font;
  12845. }
  12846. #ifdef NK_INCLUDE_DEFAULT_FONT
  12847. NK_API struct nk_font*
  12848. nk_font_atlas_add_default(struct nk_font_atlas *atlas,
  12849. float pixel_height, const struct nk_font_config *config)
  12850. {
  12851. NK_ASSERT(atlas);
  12852. NK_ASSERT(atlas->temporary.alloc);
  12853. NK_ASSERT(atlas->temporary.free);
  12854. NK_ASSERT(atlas->permanent.alloc);
  12855. NK_ASSERT(atlas->permanent.free);
  12856. return nk_font_atlas_add_compressed_base85(atlas,
  12857. nk_proggy_clean_ttf_compressed_data_base85, pixel_height, config);
  12858. }
  12859. #endif
  12860. NK_API const void*
  12861. nk_font_atlas_bake(struct nk_font_atlas *atlas, int *width, int *height,
  12862. enum nk_font_atlas_format fmt)
  12863. {
  12864. int i = 0;
  12865. void *tmp = 0;
  12866. nk_size tmp_size, img_size;
  12867. struct nk_font *font_iter;
  12868. struct nk_font_baker *baker;
  12869. NK_ASSERT(atlas);
  12870. NK_ASSERT(atlas->temporary.alloc);
  12871. NK_ASSERT(atlas->temporary.free);
  12872. NK_ASSERT(atlas->permanent.alloc);
  12873. NK_ASSERT(atlas->permanent.free);
  12874. NK_ASSERT(width);
  12875. NK_ASSERT(height);
  12876. if (!atlas || !width || !height ||
  12877. !atlas->temporary.alloc || !atlas->temporary.free ||
  12878. !atlas->permanent.alloc || !atlas->permanent.free)
  12879. return 0;
  12880. #ifdef NK_INCLUDE_DEFAULT_FONT
  12881. /* no font added so just use default font */
  12882. if (!atlas->font_num)
  12883. atlas->default_font = nk_font_atlas_add_default(atlas, 13.0f, 0);
  12884. #endif
  12885. NK_ASSERT(atlas->font_num);
  12886. if (!atlas->font_num) return 0;
  12887. /* allocate temporary baker memory required for the baking process */
  12888. nk_font_baker_memory(&tmp_size, &atlas->glyph_count, atlas->config, atlas->font_num);
  12889. tmp = atlas->temporary.alloc(atlas->temporary.userdata,0, tmp_size);
  12890. NK_ASSERT(tmp);
  12891. if (!tmp) goto failed;
  12892. /* allocate glyph memory for all fonts */
  12893. baker = nk_font_baker(tmp, atlas->glyph_count, atlas->font_num, &atlas->temporary);
  12894. atlas->glyphs = (struct nk_font_glyph*)atlas->permanent.alloc(
  12895. atlas->permanent.userdata,0, sizeof(struct nk_font_glyph)*(nk_size)atlas->glyph_count);
  12896. NK_ASSERT(atlas->glyphs);
  12897. if (!atlas->glyphs)
  12898. goto failed;
  12899. /* pack all glyphs into a tight fit space */
  12900. atlas->custom.w = (NK_CURSOR_DATA_W*2)+1;
  12901. atlas->custom.h = NK_CURSOR_DATA_H + 1;
  12902. if (!nk_font_bake_pack(baker, &img_size, width, height, &atlas->custom,
  12903. atlas->config, atlas->font_num, &atlas->temporary))
  12904. goto failed;
  12905. /* allocate memory for the baked image font atlas */
  12906. atlas->pixel = atlas->temporary.alloc(atlas->temporary.userdata,0, img_size);
  12907. NK_ASSERT(atlas->pixel);
  12908. if (!atlas->pixel)
  12909. goto failed;
  12910. /* bake glyphs and custom white pixel into image */
  12911. nk_font_bake(baker, atlas->pixel, *width, *height,
  12912. atlas->glyphs, atlas->glyph_count, atlas->config, atlas->font_num);
  12913. nk_font_bake_custom_data(atlas->pixel, *width, *height, atlas->custom,
  12914. nk_custom_cursor_data, NK_CURSOR_DATA_W, NK_CURSOR_DATA_H, '.', 'X');
  12915. if (fmt == NK_FONT_ATLAS_RGBA32) {
  12916. /* convert alpha8 image into rgba32 image */
  12917. void *img_rgba = atlas->temporary.alloc(atlas->temporary.userdata,0,
  12918. (nk_size)(*width * *height * 4));
  12919. NK_ASSERT(img_rgba);
  12920. if (!img_rgba) goto failed;
  12921. nk_font_bake_convert(img_rgba, *width, *height, atlas->pixel);
  12922. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  12923. atlas->pixel = img_rgba;
  12924. }
  12925. atlas->tex_width = *width;
  12926. atlas->tex_height = *height;
  12927. /* initialize each font */
  12928. for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
  12929. struct nk_font *font = font_iter;
  12930. struct nk_font_config *config = font->config;
  12931. nk_font_init(font, config->size, config->fallback_glyph, atlas->glyphs,
  12932. config->font, nk_handle_ptr(0));
  12933. }
  12934. /* initialize each cursor */
  12935. {NK_STORAGE const struct nk_vec2 nk_cursor_data[NK_CURSOR_COUNT][3] = {
  12936. /* Pos Size Offset */
  12937. {{ 0, 3}, {12,19}, { 0, 0}},
  12938. {{13, 0}, { 7,16}, { 4, 8}},
  12939. {{31, 0}, {23,23}, {11,11}},
  12940. {{21, 0}, { 9, 23}, { 5,11}},
  12941. {{55,18}, {23, 9}, {11, 5}},
  12942. {{73, 0}, {17,17}, { 9, 9}},
  12943. {{55, 0}, {17,17}, { 9, 9}}
  12944. };
  12945. for (i = 0; i < NK_CURSOR_COUNT; ++i) {
  12946. struct nk_cursor *cursor = &atlas->cursors[i];
  12947. cursor->img.w = (unsigned short)*width;
  12948. cursor->img.h = (unsigned short)*height;
  12949. cursor->img.region[0] = (unsigned short)(atlas->custom.x + nk_cursor_data[i][0].x);
  12950. cursor->img.region[1] = (unsigned short)(atlas->custom.y + nk_cursor_data[i][0].y);
  12951. cursor->img.region[2] = (unsigned short)nk_cursor_data[i][1].x;
  12952. cursor->img.region[3] = (unsigned short)nk_cursor_data[i][1].y;
  12953. cursor->size = nk_cursor_data[i][1];
  12954. cursor->offset = nk_cursor_data[i][2];
  12955. }}
  12956. /* free temporary memory */
  12957. atlas->temporary.free(atlas->temporary.userdata, tmp);
  12958. return atlas->pixel;
  12959. failed:
  12960. /* error so cleanup all memory */
  12961. if (tmp) atlas->temporary.free(atlas->temporary.userdata, tmp);
  12962. if (atlas->glyphs) {
  12963. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  12964. atlas->glyphs = 0;
  12965. }
  12966. if (atlas->pixel) {
  12967. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  12968. atlas->pixel = 0;
  12969. }
  12970. return 0;
  12971. }
  12972. NK_API void
  12973. nk_font_atlas_end(struct nk_font_atlas *atlas, nk_handle texture,
  12974. struct nk_draw_null_texture *null)
  12975. {
  12976. int i = 0;
  12977. struct nk_font *font_iter;
  12978. NK_ASSERT(atlas);
  12979. if (!atlas) {
  12980. if (!null) return;
  12981. null->texture = texture;
  12982. null->uv = nk_vec2(0.5f,0.5f);
  12983. }
  12984. if (null) {
  12985. null->texture = texture;
  12986. null->uv.x = (atlas->custom.x + 0.5f)/(float)atlas->tex_width;
  12987. null->uv.y = (atlas->custom.y + 0.5f)/(float)atlas->tex_height;
  12988. }
  12989. for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
  12990. font_iter->texture = texture;
  12991. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  12992. font_iter->handle.texture = texture;
  12993. #endif
  12994. }
  12995. for (i = 0; i < NK_CURSOR_COUNT; ++i)
  12996. atlas->cursors[i].img.handle = texture;
  12997. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  12998. atlas->pixel = 0;
  12999. atlas->tex_width = 0;
  13000. atlas->tex_height = 0;
  13001. atlas->custom.x = 0;
  13002. atlas->custom.y = 0;
  13003. atlas->custom.w = 0;
  13004. atlas->custom.h = 0;
  13005. }
  13006. NK_API void
  13007. nk_font_atlas_cleanup(struct nk_font_atlas *atlas)
  13008. {
  13009. NK_ASSERT(atlas);
  13010. NK_ASSERT(atlas->temporary.alloc);
  13011. NK_ASSERT(atlas->temporary.free);
  13012. NK_ASSERT(atlas->permanent.alloc);
  13013. NK_ASSERT(atlas->permanent.free);
  13014. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free) return;
  13015. if (atlas->config) {
  13016. struct nk_font_config *iter;
  13017. for (iter = atlas->config; iter; iter = iter->next) {
  13018. struct nk_font_config *i;
  13019. for (i = iter->n; i != iter; i = i->n) {
  13020. atlas->permanent.free(atlas->permanent.userdata, i->ttf_blob);
  13021. i->ttf_blob = 0;
  13022. }
  13023. atlas->permanent.free(atlas->permanent.userdata, iter->ttf_blob);
  13024. iter->ttf_blob = 0;
  13025. }
  13026. }
  13027. }
  13028. NK_API void
  13029. nk_font_atlas_clear(struct nk_font_atlas *atlas)
  13030. {
  13031. NK_ASSERT(atlas);
  13032. NK_ASSERT(atlas->temporary.alloc);
  13033. NK_ASSERT(atlas->temporary.free);
  13034. NK_ASSERT(atlas->permanent.alloc);
  13035. NK_ASSERT(atlas->permanent.free);
  13036. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free) return;
  13037. if (atlas->config) {
  13038. struct nk_font_config *iter, *next;
  13039. for (iter = atlas->config; iter; iter = next) {
  13040. struct nk_font_config *i, *n;
  13041. for (i = iter->n; i != iter; i = n) {
  13042. n = i->n;
  13043. if (i->ttf_blob)
  13044. atlas->permanent.free(atlas->permanent.userdata, i->ttf_blob);
  13045. atlas->permanent.free(atlas->permanent.userdata, i);
  13046. }
  13047. next = iter->next;
  13048. if (i->ttf_blob)
  13049. atlas->permanent.free(atlas->permanent.userdata, iter->ttf_blob);
  13050. atlas->permanent.free(atlas->permanent.userdata, iter);
  13051. }
  13052. atlas->config = 0;
  13053. }
  13054. if (atlas->fonts) {
  13055. struct nk_font *iter, *next;
  13056. for (iter = atlas->fonts; iter; iter = next) {
  13057. next = iter->next;
  13058. atlas->permanent.free(atlas->permanent.userdata, iter);
  13059. }
  13060. atlas->fonts = 0;
  13061. }
  13062. if (atlas->glyphs)
  13063. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  13064. nk_zero_struct(*atlas);
  13065. }
  13066. #endif
  13067. /* ===============================================================
  13068. *
  13069. * INPUT
  13070. *
  13071. * ===============================================================*/
  13072. NK_API void
  13073. nk_input_begin(struct nk_context *ctx)
  13074. {
  13075. int i;
  13076. struct nk_input *in;
  13077. NK_ASSERT(ctx);
  13078. if (!ctx) return;
  13079. in = &ctx->input;
  13080. for (i = 0; i < NK_BUTTON_MAX; ++i)
  13081. in->mouse.buttons[i].clicked = 0;
  13082. in->keyboard.text_len = 0;
  13083. in->mouse.scroll_delta = nk_vec2(0,0);
  13084. in->mouse.prev.x = in->mouse.pos.x;
  13085. in->mouse.prev.y = in->mouse.pos.y;
  13086. in->mouse.delta.x = 0;
  13087. in->mouse.delta.y = 0;
  13088. for (i = 0; i < NK_KEY_MAX; i++)
  13089. in->keyboard.keys[i].clicked = 0;
  13090. }
  13091. NK_API void
  13092. nk_input_end(struct nk_context *ctx)
  13093. {
  13094. struct nk_input *in;
  13095. NK_ASSERT(ctx);
  13096. if (!ctx) return;
  13097. in = &ctx->input;
  13098. if (in->mouse.grab)
  13099. in->mouse.grab = 0;
  13100. if (in->mouse.ungrab) {
  13101. in->mouse.grabbed = 0;
  13102. in->mouse.ungrab = 0;
  13103. in->mouse.grab = 0;
  13104. }
  13105. }
  13106. NK_API void
  13107. nk_input_motion(struct nk_context *ctx, int x, int y)
  13108. {
  13109. struct nk_input *in;
  13110. NK_ASSERT(ctx);
  13111. if (!ctx) return;
  13112. in = &ctx->input;
  13113. in->mouse.pos.x = (float)x;
  13114. in->mouse.pos.y = (float)y;
  13115. in->mouse.delta.x = in->mouse.pos.x - in->mouse.prev.x;
  13116. in->mouse.delta.y = in->mouse.pos.y - in->mouse.prev.y;
  13117. }
  13118. NK_API void
  13119. nk_input_key(struct nk_context *ctx, enum nk_keys key, int down)
  13120. {
  13121. struct nk_input *in;
  13122. NK_ASSERT(ctx);
  13123. if (!ctx) return;
  13124. in = &ctx->input;
  13125. #ifdef NK_KEYSTATE_BASED_INPUT
  13126. if (in->keyboard.keys[key].down != down)
  13127. in->keyboard.keys[key].clicked++;
  13128. #else
  13129. in->keyboard.keys[key].clicked++;
  13130. #endif
  13131. in->keyboard.keys[key].down = down;
  13132. }
  13133. NK_API void
  13134. nk_input_button(struct nk_context *ctx, enum nk_buttons id, int x, int y, int down)
  13135. {
  13136. struct nk_mouse_button *btn;
  13137. struct nk_input *in;
  13138. NK_ASSERT(ctx);
  13139. if (!ctx) return;
  13140. in = &ctx->input;
  13141. if (in->mouse.buttons[id].down == down) return;
  13142. btn = &in->mouse.buttons[id];
  13143. btn->clicked_pos.x = (float)x;
  13144. btn->clicked_pos.y = (float)y;
  13145. btn->down = down;
  13146. btn->clicked++;
  13147. }
  13148. NK_API void
  13149. nk_input_scroll(struct nk_context *ctx, struct nk_vec2 val)
  13150. {
  13151. NK_ASSERT(ctx);
  13152. if (!ctx) return;
  13153. ctx->input.mouse.scroll_delta.x += val.x;
  13154. ctx->input.mouse.scroll_delta.y += val.y;
  13155. }
  13156. NK_API void
  13157. nk_input_glyph(struct nk_context *ctx, const nk_glyph glyph)
  13158. {
  13159. int len = 0;
  13160. nk_rune unicode;
  13161. struct nk_input *in;
  13162. NK_ASSERT(ctx);
  13163. if (!ctx) return;
  13164. in = &ctx->input;
  13165. len = nk_utf_decode(glyph, &unicode, NK_UTF_SIZE);
  13166. if (len && ((in->keyboard.text_len + len) < NK_INPUT_MAX)) {
  13167. nk_utf_encode(unicode, &in->keyboard.text[in->keyboard.text_len],
  13168. NK_INPUT_MAX - in->keyboard.text_len);
  13169. in->keyboard.text_len += len;
  13170. }
  13171. }
  13172. NK_API void
  13173. nk_input_char(struct nk_context *ctx, char c)
  13174. {
  13175. nk_glyph glyph;
  13176. NK_ASSERT(ctx);
  13177. if (!ctx) return;
  13178. glyph[0] = c;
  13179. nk_input_glyph(ctx, glyph);
  13180. }
  13181. NK_API void
  13182. nk_input_unicode(struct nk_context *ctx, nk_rune unicode)
  13183. {
  13184. nk_glyph rune;
  13185. NK_ASSERT(ctx);
  13186. if (!ctx) return;
  13187. nk_utf_encode(unicode, rune, NK_UTF_SIZE);
  13188. nk_input_glyph(ctx, rune);
  13189. }
  13190. NK_API int
  13191. nk_input_has_mouse_click(const struct nk_input *i, enum nk_buttons id)
  13192. {
  13193. const struct nk_mouse_button *btn;
  13194. if (!i) return nk_false;
  13195. btn = &i->mouse.buttons[id];
  13196. return (btn->clicked && btn->down == nk_false) ? nk_true : nk_false;
  13197. }
  13198. NK_API int
  13199. nk_input_has_mouse_click_in_rect(const struct nk_input *i, enum nk_buttons id,
  13200. struct nk_rect b)
  13201. {
  13202. const struct nk_mouse_button *btn;
  13203. if (!i) return nk_false;
  13204. btn = &i->mouse.buttons[id];
  13205. if (!NK_INBOX(btn->clicked_pos.x,btn->clicked_pos.y,b.x,b.y,b.w,b.h))
  13206. return nk_false;
  13207. return nk_true;
  13208. }
  13209. NK_API int
  13210. nk_input_has_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id,
  13211. struct nk_rect b, int down)
  13212. {
  13213. const struct nk_mouse_button *btn;
  13214. if (!i) return nk_false;
  13215. btn = &i->mouse.buttons[id];
  13216. return nk_input_has_mouse_click_in_rect(i, id, b) && (btn->down == down);
  13217. }
  13218. NK_API int
  13219. nk_input_is_mouse_click_in_rect(const struct nk_input *i, enum nk_buttons id,
  13220. struct nk_rect b)
  13221. {
  13222. const struct nk_mouse_button *btn;
  13223. if (!i) return nk_false;
  13224. btn = &i->mouse.buttons[id];
  13225. return (nk_input_has_mouse_click_down_in_rect(i, id, b, nk_false) &&
  13226. btn->clicked) ? nk_true : nk_false;
  13227. }
  13228. NK_API int
  13229. nk_input_is_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id,
  13230. struct nk_rect b, int down)
  13231. {
  13232. const struct nk_mouse_button *btn;
  13233. if (!i) return nk_false;
  13234. btn = &i->mouse.buttons[id];
  13235. return (nk_input_has_mouse_click_down_in_rect(i, id, b, down) &&
  13236. btn->clicked) ? nk_true : nk_false;
  13237. }
  13238. NK_API int
  13239. nk_input_any_mouse_click_in_rect(const struct nk_input *in, struct nk_rect b)
  13240. {
  13241. int i, down = 0;
  13242. for (i = 0; i < NK_BUTTON_MAX; ++i)
  13243. down = down || nk_input_is_mouse_click_in_rect(in, (enum nk_buttons)i, b);
  13244. return down;
  13245. }
  13246. NK_API int
  13247. nk_input_is_mouse_hovering_rect(const struct nk_input *i, struct nk_rect rect)
  13248. {
  13249. if (!i) return nk_false;
  13250. return NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h);
  13251. }
  13252. NK_API int
  13253. nk_input_is_mouse_prev_hovering_rect(const struct nk_input *i, struct nk_rect rect)
  13254. {
  13255. if (!i) return nk_false;
  13256. return NK_INBOX(i->mouse.prev.x, i->mouse.prev.y, rect.x, rect.y, rect.w, rect.h);
  13257. }
  13258. NK_API int
  13259. nk_input_mouse_clicked(const struct nk_input *i, enum nk_buttons id, struct nk_rect rect)
  13260. {
  13261. if (!i) return nk_false;
  13262. if (!nk_input_is_mouse_hovering_rect(i, rect)) return nk_false;
  13263. return nk_input_is_mouse_click_in_rect(i, id, rect);
  13264. }
  13265. NK_API int
  13266. nk_input_is_mouse_down(const struct nk_input *i, enum nk_buttons id)
  13267. {
  13268. if (!i) return nk_false;
  13269. return i->mouse.buttons[id].down;
  13270. }
  13271. NK_API int
  13272. nk_input_is_mouse_pressed(const struct nk_input *i, enum nk_buttons id)
  13273. {
  13274. const struct nk_mouse_button *b;
  13275. if (!i) return nk_false;
  13276. b = &i->mouse.buttons[id];
  13277. if (b->down && b->clicked)
  13278. return nk_true;
  13279. return nk_false;
  13280. }
  13281. NK_API int
  13282. nk_input_is_mouse_released(const struct nk_input *i, enum nk_buttons id)
  13283. {
  13284. if (!i) return nk_false;
  13285. return (!i->mouse.buttons[id].down && i->mouse.buttons[id].clicked);
  13286. }
  13287. NK_API int
  13288. nk_input_is_key_pressed(const struct nk_input *i, enum nk_keys key)
  13289. {
  13290. const struct nk_key *k;
  13291. if (!i) return nk_false;
  13292. k = &i->keyboard.keys[key];
  13293. if ((k->down && k->clicked) || (!k->down && k->clicked >= 2))
  13294. return nk_true;
  13295. return nk_false;
  13296. }
  13297. NK_API int
  13298. nk_input_is_key_released(const struct nk_input *i, enum nk_keys key)
  13299. {
  13300. const struct nk_key *k;
  13301. if (!i) return nk_false;
  13302. k = &i->keyboard.keys[key];
  13303. if ((!k->down && k->clicked) || (k->down && k->clicked >= 2))
  13304. return nk_true;
  13305. return nk_false;
  13306. }
  13307. NK_API int
  13308. nk_input_is_key_down(const struct nk_input *i, enum nk_keys key)
  13309. {
  13310. const struct nk_key *k;
  13311. if (!i) return nk_false;
  13312. k = &i->keyboard.keys[key];
  13313. if (k->down) return nk_true;
  13314. return nk_false;
  13315. }
  13316. /* ===============================================================
  13317. *
  13318. * STYLE
  13319. *
  13320. * ===============================================================*/
  13321. NK_API void nk_style_default(struct nk_context *ctx){nk_style_from_table(ctx, 0);}
  13322. #define NK_COLOR_MAP(NK_COLOR)\
  13323. NK_COLOR(NK_COLOR_TEXT, 175,175,175,255) \
  13324. NK_COLOR(NK_COLOR_WINDOW, 45, 45, 45, 255) \
  13325. NK_COLOR(NK_COLOR_HEADER, 40, 40, 40, 255) \
  13326. NK_COLOR(NK_COLOR_BORDER, 65, 65, 65, 255) \
  13327. NK_COLOR(NK_COLOR_BUTTON, 50, 50, 50, 255) \
  13328. NK_COLOR(NK_COLOR_BUTTON_HOVER, 40, 40, 40, 255) \
  13329. NK_COLOR(NK_COLOR_BUTTON_ACTIVE, 35, 35, 35, 255) \
  13330. NK_COLOR(NK_COLOR_TOGGLE, 100,100,100,255) \
  13331. NK_COLOR(NK_COLOR_TOGGLE_HOVER, 120,120,120,255) \
  13332. NK_COLOR(NK_COLOR_TOGGLE_CURSOR, 45, 45, 45, 255) \
  13333. NK_COLOR(NK_COLOR_SELECT, 45, 45, 45, 255) \
  13334. NK_COLOR(NK_COLOR_SELECT_ACTIVE, 35, 35, 35,255) \
  13335. NK_COLOR(NK_COLOR_SLIDER, 38, 38, 38, 255) \
  13336. NK_COLOR(NK_COLOR_SLIDER_CURSOR, 100,100,100,255) \
  13337. NK_COLOR(NK_COLOR_SLIDER_CURSOR_HOVER, 120,120,120,255) \
  13338. NK_COLOR(NK_COLOR_SLIDER_CURSOR_ACTIVE, 150,150,150,255) \
  13339. NK_COLOR(NK_COLOR_PROPERTY, 38, 38, 38, 255) \
  13340. NK_COLOR(NK_COLOR_EDIT, 38, 38, 38, 255) \
  13341. NK_COLOR(NK_COLOR_EDIT_CURSOR, 175,175,175,255) \
  13342. NK_COLOR(NK_COLOR_COMBO, 45, 45, 45, 255) \
  13343. NK_COLOR(NK_COLOR_CHART, 120,120,120,255) \
  13344. NK_COLOR(NK_COLOR_CHART_COLOR, 45, 45, 45, 255) \
  13345. NK_COLOR(NK_COLOR_CHART_COLOR_HIGHLIGHT, 255, 0, 0, 255) \
  13346. NK_COLOR(NK_COLOR_SCROLLBAR, 40, 40, 40, 255) \
  13347. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR, 100,100,100,255) \
  13348. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR_HOVER, 120,120,120,255) \
  13349. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR_ACTIVE, 150,150,150,255) \
  13350. NK_COLOR(NK_COLOR_TAB_HEADER, 40, 40, 40,255)
  13351. NK_GLOBAL const struct nk_color
  13352. nk_default_color_style[NK_COLOR_COUNT] = {
  13353. #define NK_COLOR(a,b,c,d,e) {b,c,d,e},
  13354. NK_COLOR_MAP(NK_COLOR)
  13355. #undef NK_COLOR
  13356. };
  13357. NK_GLOBAL const char *nk_color_names[NK_COLOR_COUNT] = {
  13358. #define NK_COLOR(a,b,c,d,e) #a,
  13359. NK_COLOR_MAP(NK_COLOR)
  13360. #undef NK_COLOR
  13361. };
  13362. NK_API const char*
  13363. nk_style_get_color_by_name(enum nk_style_colors c)
  13364. {
  13365. return nk_color_names[c];
  13366. }
  13367. NK_API struct nk_style_item
  13368. nk_style_item_image(struct nk_image img)
  13369. {
  13370. struct nk_style_item i;
  13371. i.type = NK_STYLE_ITEM_IMAGE;
  13372. i.data.image = img;
  13373. return i;
  13374. }
  13375. NK_API struct nk_style_item
  13376. nk_style_item_color(struct nk_color col)
  13377. {
  13378. struct nk_style_item i;
  13379. i.type = NK_STYLE_ITEM_COLOR;
  13380. i.data.color = col;
  13381. return i;
  13382. }
  13383. NK_API struct nk_style_item
  13384. nk_style_item_hide(void)
  13385. {
  13386. struct nk_style_item i;
  13387. i.type = NK_STYLE_ITEM_COLOR;
  13388. i.data.color = nk_rgba(0,0,0,0);
  13389. return i;
  13390. }
  13391. NK_API void
  13392. nk_style_from_table(struct nk_context *ctx, const struct nk_color *table)
  13393. {
  13394. struct nk_style *style;
  13395. struct nk_style_text *text;
  13396. struct nk_style_button *button;
  13397. struct nk_style_toggle *toggle;
  13398. struct nk_style_selectable *select;
  13399. struct nk_style_slider *slider;
  13400. struct nk_style_progress *prog;
  13401. struct nk_style_scrollbar *scroll;
  13402. struct nk_style_edit *edit;
  13403. struct nk_style_property *property;
  13404. struct nk_style_combo *combo;
  13405. struct nk_style_chart *chart;
  13406. struct nk_style_tab *tab;
  13407. struct nk_style_window *win;
  13408. NK_ASSERT(ctx);
  13409. if (!ctx) return;
  13410. style = &ctx->style;
  13411. table = (!table) ? nk_default_color_style: table;
  13412. /* default text */
  13413. text = &style->text;
  13414. text->color = table[NK_COLOR_TEXT];
  13415. text->padding = nk_vec2(0,0);
  13416. /* default button */
  13417. button = &style->button;
  13418. nk_zero_struct(*button);
  13419. button->normal = nk_style_item_color(table[NK_COLOR_BUTTON]);
  13420. button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]);
  13421. button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]);
  13422. button->border_color = table[NK_COLOR_BORDER];
  13423. button->text_background = table[NK_COLOR_BUTTON];
  13424. button->text_normal = table[NK_COLOR_TEXT];
  13425. button->text_hover = table[NK_COLOR_TEXT];
  13426. button->text_active = table[NK_COLOR_TEXT];
  13427. button->padding = nk_vec2(2.0f,2.0f);
  13428. button->image_padding = nk_vec2(0.0f,0.0f);
  13429. button->touch_padding = nk_vec2(0.0f, 0.0f);
  13430. button->userdata = nk_handle_ptr(0);
  13431. button->text_alignment = NK_TEXT_CENTERED;
  13432. button->border = 1.0f;
  13433. button->rounding = 4.0f;
  13434. button->draw_begin = 0;
  13435. button->draw_end = 0;
  13436. /* contextual button */
  13437. button = &style->contextual_button;
  13438. nk_zero_struct(*button);
  13439. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13440. button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]);
  13441. button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]);
  13442. button->border_color = table[NK_COLOR_WINDOW];
  13443. button->text_background = table[NK_COLOR_WINDOW];
  13444. button->text_normal = table[NK_COLOR_TEXT];
  13445. button->text_hover = table[NK_COLOR_TEXT];
  13446. button->text_active = table[NK_COLOR_TEXT];
  13447. button->padding = nk_vec2(2.0f,2.0f);
  13448. button->touch_padding = nk_vec2(0.0f,0.0f);
  13449. button->userdata = nk_handle_ptr(0);
  13450. button->text_alignment = NK_TEXT_CENTERED;
  13451. button->border = 0.0f;
  13452. button->rounding = 0.0f;
  13453. button->draw_begin = 0;
  13454. button->draw_end = 0;
  13455. /* menu button */
  13456. button = &style->menu_button;
  13457. nk_zero_struct(*button);
  13458. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13459. button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13460. button->active = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13461. button->border_color = table[NK_COLOR_WINDOW];
  13462. button->text_background = table[NK_COLOR_WINDOW];
  13463. button->text_normal = table[NK_COLOR_TEXT];
  13464. button->text_hover = table[NK_COLOR_TEXT];
  13465. button->text_active = table[NK_COLOR_TEXT];
  13466. button->padding = nk_vec2(2.0f,2.0f);
  13467. button->touch_padding = nk_vec2(0.0f,0.0f);
  13468. button->userdata = nk_handle_ptr(0);
  13469. button->text_alignment = NK_TEXT_CENTERED;
  13470. button->border = 0.0f;
  13471. button->rounding = 1.0f;
  13472. button->draw_begin = 0;
  13473. button->draw_end = 0;
  13474. /* checkbox toggle */
  13475. toggle = &style->checkbox;
  13476. nk_zero_struct(*toggle);
  13477. toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]);
  13478. toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  13479. toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  13480. toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  13481. toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  13482. toggle->userdata = nk_handle_ptr(0);
  13483. toggle->text_background = table[NK_COLOR_WINDOW];
  13484. toggle->text_normal = table[NK_COLOR_TEXT];
  13485. toggle->text_hover = table[NK_COLOR_TEXT];
  13486. toggle->text_active = table[NK_COLOR_TEXT];
  13487. toggle->padding = nk_vec2(2.0f, 2.0f);
  13488. toggle->touch_padding = nk_vec2(0,0);
  13489. toggle->border_color = nk_rgba(0,0,0,0);
  13490. toggle->border = 0.0f;
  13491. toggle->spacing = 4;
  13492. /* option toggle */
  13493. toggle = &style->option;
  13494. nk_zero_struct(*toggle);
  13495. toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]);
  13496. toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  13497. toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  13498. toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  13499. toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  13500. toggle->userdata = nk_handle_ptr(0);
  13501. toggle->text_background = table[NK_COLOR_WINDOW];
  13502. toggle->text_normal = table[NK_COLOR_TEXT];
  13503. toggle->text_hover = table[NK_COLOR_TEXT];
  13504. toggle->text_active = table[NK_COLOR_TEXT];
  13505. toggle->padding = nk_vec2(3.0f, 3.0f);
  13506. toggle->touch_padding = nk_vec2(0,0);
  13507. toggle->border_color = nk_rgba(0,0,0,0);
  13508. toggle->border = 0.0f;
  13509. toggle->spacing = 4;
  13510. /* selectable */
  13511. select = &style->selectable;
  13512. nk_zero_struct(*select);
  13513. select->normal = nk_style_item_color(table[NK_COLOR_SELECT]);
  13514. select->hover = nk_style_item_color(table[NK_COLOR_SELECT]);
  13515. select->pressed = nk_style_item_color(table[NK_COLOR_SELECT]);
  13516. select->normal_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  13517. select->hover_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  13518. select->pressed_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  13519. select->text_normal = table[NK_COLOR_TEXT];
  13520. select->text_hover = table[NK_COLOR_TEXT];
  13521. select->text_pressed = table[NK_COLOR_TEXT];
  13522. select->text_normal_active = table[NK_COLOR_TEXT];
  13523. select->text_hover_active = table[NK_COLOR_TEXT];
  13524. select->text_pressed_active = table[NK_COLOR_TEXT];
  13525. select->padding = nk_vec2(2.0f,2.0f);
  13526. select->image_padding = nk_vec2(2.0f,2.0f);
  13527. select->touch_padding = nk_vec2(0,0);
  13528. select->userdata = nk_handle_ptr(0);
  13529. select->rounding = 0.0f;
  13530. select->draw_begin = 0;
  13531. select->draw_end = 0;
  13532. /* slider */
  13533. slider = &style->slider;
  13534. nk_zero_struct(*slider);
  13535. slider->normal = nk_style_item_hide();
  13536. slider->hover = nk_style_item_hide();
  13537. slider->active = nk_style_item_hide();
  13538. slider->bar_normal = table[NK_COLOR_SLIDER];
  13539. slider->bar_hover = table[NK_COLOR_SLIDER];
  13540. slider->bar_active = table[NK_COLOR_SLIDER];
  13541. slider->bar_filled = table[NK_COLOR_SLIDER_CURSOR];
  13542. slider->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]);
  13543. slider->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]);
  13544. slider->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]);
  13545. slider->inc_symbol = NK_SYMBOL_TRIANGLE_RIGHT;
  13546. slider->dec_symbol = NK_SYMBOL_TRIANGLE_LEFT;
  13547. slider->cursor_size = nk_vec2(16,16);
  13548. slider->padding = nk_vec2(2,2);
  13549. slider->spacing = nk_vec2(2,2);
  13550. slider->userdata = nk_handle_ptr(0);
  13551. slider->show_buttons = nk_false;
  13552. slider->bar_height = 8;
  13553. slider->rounding = 0;
  13554. slider->draw_begin = 0;
  13555. slider->draw_end = 0;
  13556. /* slider buttons */
  13557. button = &style->slider.inc_button;
  13558. button->normal = nk_style_item_color(nk_rgb(40,40,40));
  13559. button->hover = nk_style_item_color(nk_rgb(42,42,42));
  13560. button->active = nk_style_item_color(nk_rgb(44,44,44));
  13561. button->border_color = nk_rgb(65,65,65);
  13562. button->text_background = nk_rgb(40,40,40);
  13563. button->text_normal = nk_rgb(175,175,175);
  13564. button->text_hover = nk_rgb(175,175,175);
  13565. button->text_active = nk_rgb(175,175,175);
  13566. button->padding = nk_vec2(8.0f,8.0f);
  13567. button->touch_padding = nk_vec2(0.0f,0.0f);
  13568. button->userdata = nk_handle_ptr(0);
  13569. button->text_alignment = NK_TEXT_CENTERED;
  13570. button->border = 1.0f;
  13571. button->rounding = 0.0f;
  13572. button->draw_begin = 0;
  13573. button->draw_end = 0;
  13574. style->slider.dec_button = style->slider.inc_button;
  13575. /* progressbar */
  13576. prog = &style->progress;
  13577. nk_zero_struct(*prog);
  13578. prog->normal = nk_style_item_color(table[NK_COLOR_SLIDER]);
  13579. prog->hover = nk_style_item_color(table[NK_COLOR_SLIDER]);
  13580. prog->active = nk_style_item_color(table[NK_COLOR_SLIDER]);
  13581. prog->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]);
  13582. prog->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]);
  13583. prog->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]);
  13584. prog->border_color = nk_rgba(0,0,0,0);
  13585. prog->cursor_border_color = nk_rgba(0,0,0,0);
  13586. prog->userdata = nk_handle_ptr(0);
  13587. prog->padding = nk_vec2(4,4);
  13588. prog->rounding = 0;
  13589. prog->border = 0;
  13590. prog->cursor_rounding = 0;
  13591. prog->cursor_border = 0;
  13592. prog->draw_begin = 0;
  13593. prog->draw_end = 0;
  13594. /* scrollbars */
  13595. scroll = &style->scrollh;
  13596. nk_zero_struct(*scroll);
  13597. scroll->normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  13598. scroll->hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  13599. scroll->active = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  13600. scroll->cursor_normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR]);
  13601. scroll->cursor_hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_HOVER]);
  13602. scroll->cursor_active = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE]);
  13603. scroll->dec_symbol = NK_SYMBOL_CIRCLE_SOLID;
  13604. scroll->inc_symbol = NK_SYMBOL_CIRCLE_SOLID;
  13605. scroll->userdata = nk_handle_ptr(0);
  13606. scroll->border_color = table[NK_COLOR_SCROLLBAR];
  13607. scroll->cursor_border_color = table[NK_COLOR_SCROLLBAR];
  13608. scroll->padding = nk_vec2(0,0);
  13609. scroll->show_buttons = nk_false;
  13610. scroll->border = 0;
  13611. scroll->rounding = 0;
  13612. scroll->border_cursor = 0;
  13613. scroll->rounding_cursor = 0;
  13614. scroll->draw_begin = 0;
  13615. scroll->draw_end = 0;
  13616. style->scrollv = style->scrollh;
  13617. /* scrollbars buttons */
  13618. button = &style->scrollh.inc_button;
  13619. button->normal = nk_style_item_color(nk_rgb(40,40,40));
  13620. button->hover = nk_style_item_color(nk_rgb(42,42,42));
  13621. button->active = nk_style_item_color(nk_rgb(44,44,44));
  13622. button->border_color = nk_rgb(65,65,65);
  13623. button->text_background = nk_rgb(40,40,40);
  13624. button->text_normal = nk_rgb(175,175,175);
  13625. button->text_hover = nk_rgb(175,175,175);
  13626. button->text_active = nk_rgb(175,175,175);
  13627. button->padding = nk_vec2(4.0f,4.0f);
  13628. button->touch_padding = nk_vec2(0.0f,0.0f);
  13629. button->userdata = nk_handle_ptr(0);
  13630. button->text_alignment = NK_TEXT_CENTERED;
  13631. button->border = 1.0f;
  13632. button->rounding = 0.0f;
  13633. button->draw_begin = 0;
  13634. button->draw_end = 0;
  13635. style->scrollh.dec_button = style->scrollh.inc_button;
  13636. style->scrollv.inc_button = style->scrollh.inc_button;
  13637. style->scrollv.dec_button = style->scrollh.inc_button;
  13638. /* edit */
  13639. edit = &style->edit;
  13640. nk_zero_struct(*edit);
  13641. edit->normal = nk_style_item_color(table[NK_COLOR_EDIT]);
  13642. edit->hover = nk_style_item_color(table[NK_COLOR_EDIT]);
  13643. edit->active = nk_style_item_color(table[NK_COLOR_EDIT]);
  13644. edit->cursor_normal = table[NK_COLOR_TEXT];
  13645. edit->cursor_hover = table[NK_COLOR_TEXT];
  13646. edit->cursor_text_normal= table[NK_COLOR_EDIT];
  13647. edit->cursor_text_hover = table[NK_COLOR_EDIT];
  13648. edit->border_color = table[NK_COLOR_BORDER];
  13649. edit->text_normal = table[NK_COLOR_TEXT];
  13650. edit->text_hover = table[NK_COLOR_TEXT];
  13651. edit->text_active = table[NK_COLOR_TEXT];
  13652. edit->selected_normal = table[NK_COLOR_TEXT];
  13653. edit->selected_hover = table[NK_COLOR_TEXT];
  13654. edit->selected_text_normal = table[NK_COLOR_EDIT];
  13655. edit->selected_text_hover = table[NK_COLOR_EDIT];
  13656. edit->scrollbar_size = nk_vec2(10,10);
  13657. edit->scrollbar = style->scrollv;
  13658. edit->padding = nk_vec2(4,4);
  13659. edit->row_padding = 2;
  13660. edit->cursor_size = 4;
  13661. edit->border = 1;
  13662. edit->rounding = 0;
  13663. /* property */
  13664. property = &style->property;
  13665. nk_zero_struct(*property);
  13666. property->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13667. property->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13668. property->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13669. property->border_color = table[NK_COLOR_BORDER];
  13670. property->label_normal = table[NK_COLOR_TEXT];
  13671. property->label_hover = table[NK_COLOR_TEXT];
  13672. property->label_active = table[NK_COLOR_TEXT];
  13673. property->sym_left = NK_SYMBOL_TRIANGLE_LEFT;
  13674. property->sym_right = NK_SYMBOL_TRIANGLE_RIGHT;
  13675. property->userdata = nk_handle_ptr(0);
  13676. property->padding = nk_vec2(4,4);
  13677. property->border = 1;
  13678. property->rounding = 10;
  13679. property->draw_begin = 0;
  13680. property->draw_end = 0;
  13681. /* property buttons */
  13682. button = &style->property.dec_button;
  13683. nk_zero_struct(*button);
  13684. button->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13685. button->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13686. button->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13687. button->border_color = nk_rgba(0,0,0,0);
  13688. button->text_background = table[NK_COLOR_PROPERTY];
  13689. button->text_normal = table[NK_COLOR_TEXT];
  13690. button->text_hover = table[NK_COLOR_TEXT];
  13691. button->text_active = table[NK_COLOR_TEXT];
  13692. button->padding = nk_vec2(0.0f,0.0f);
  13693. button->touch_padding = nk_vec2(0.0f,0.0f);
  13694. button->userdata = nk_handle_ptr(0);
  13695. button->text_alignment = NK_TEXT_CENTERED;
  13696. button->border = 0.0f;
  13697. button->rounding = 0.0f;
  13698. button->draw_begin = 0;
  13699. button->draw_end = 0;
  13700. style->property.inc_button = style->property.dec_button;
  13701. /* property edit */
  13702. edit = &style->property.edit;
  13703. nk_zero_struct(*edit);
  13704. edit->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13705. edit->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13706. edit->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  13707. edit->border_color = nk_rgba(0,0,0,0);
  13708. edit->cursor_normal = table[NK_COLOR_TEXT];
  13709. edit->cursor_hover = table[NK_COLOR_TEXT];
  13710. edit->cursor_text_normal= table[NK_COLOR_EDIT];
  13711. edit->cursor_text_hover = table[NK_COLOR_EDIT];
  13712. edit->text_normal = table[NK_COLOR_TEXT];
  13713. edit->text_hover = table[NK_COLOR_TEXT];
  13714. edit->text_active = table[NK_COLOR_TEXT];
  13715. edit->selected_normal = table[NK_COLOR_TEXT];
  13716. edit->selected_hover = table[NK_COLOR_TEXT];
  13717. edit->selected_text_normal = table[NK_COLOR_EDIT];
  13718. edit->selected_text_hover = table[NK_COLOR_EDIT];
  13719. edit->padding = nk_vec2(0,0);
  13720. edit->cursor_size = 8;
  13721. edit->border = 0;
  13722. edit->rounding = 0;
  13723. /* chart */
  13724. chart = &style->chart;
  13725. nk_zero_struct(*chart);
  13726. chart->background = nk_style_item_color(table[NK_COLOR_CHART]);
  13727. chart->border_color = table[NK_COLOR_BORDER];
  13728. chart->selected_color = table[NK_COLOR_CHART_COLOR_HIGHLIGHT];
  13729. chart->color = table[NK_COLOR_CHART_COLOR];
  13730. chart->padding = nk_vec2(4,4);
  13731. chart->border = 0;
  13732. chart->rounding = 0;
  13733. /* combo */
  13734. combo = &style->combo;
  13735. combo->normal = nk_style_item_color(table[NK_COLOR_COMBO]);
  13736. combo->hover = nk_style_item_color(table[NK_COLOR_COMBO]);
  13737. combo->active = nk_style_item_color(table[NK_COLOR_COMBO]);
  13738. combo->border_color = table[NK_COLOR_BORDER];
  13739. combo->label_normal = table[NK_COLOR_TEXT];
  13740. combo->label_hover = table[NK_COLOR_TEXT];
  13741. combo->label_active = table[NK_COLOR_TEXT];
  13742. combo->sym_normal = NK_SYMBOL_TRIANGLE_DOWN;
  13743. combo->sym_hover = NK_SYMBOL_TRIANGLE_DOWN;
  13744. combo->sym_active = NK_SYMBOL_TRIANGLE_DOWN;
  13745. combo->content_padding = nk_vec2(4,4);
  13746. combo->button_padding = nk_vec2(0,4);
  13747. combo->spacing = nk_vec2(4,0);
  13748. combo->border = 1;
  13749. combo->rounding = 0;
  13750. /* combo button */
  13751. button = &style->combo.button;
  13752. nk_zero_struct(*button);
  13753. button->normal = nk_style_item_color(table[NK_COLOR_COMBO]);
  13754. button->hover = nk_style_item_color(table[NK_COLOR_COMBO]);
  13755. button->active = nk_style_item_color(table[NK_COLOR_COMBO]);
  13756. button->border_color = nk_rgba(0,0,0,0);
  13757. button->text_background = table[NK_COLOR_COMBO];
  13758. button->text_normal = table[NK_COLOR_TEXT];
  13759. button->text_hover = table[NK_COLOR_TEXT];
  13760. button->text_active = table[NK_COLOR_TEXT];
  13761. button->padding = nk_vec2(2.0f,2.0f);
  13762. button->touch_padding = nk_vec2(0.0f,0.0f);
  13763. button->userdata = nk_handle_ptr(0);
  13764. button->text_alignment = NK_TEXT_CENTERED;
  13765. button->border = 0.0f;
  13766. button->rounding = 0.0f;
  13767. button->draw_begin = 0;
  13768. button->draw_end = 0;
  13769. /* tab */
  13770. tab = &style->tab;
  13771. tab->background = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  13772. tab->border_color = table[NK_COLOR_BORDER];
  13773. tab->text = table[NK_COLOR_TEXT];
  13774. tab->sym_minimize = NK_SYMBOL_TRIANGLE_RIGHT;
  13775. tab->sym_maximize = NK_SYMBOL_TRIANGLE_DOWN;
  13776. tab->padding = nk_vec2(4,4);
  13777. tab->spacing = nk_vec2(4,4);
  13778. tab->indent = 10.0f;
  13779. tab->border = 1;
  13780. tab->rounding = 0;
  13781. /* tab button */
  13782. button = &style->tab.tab_minimize_button;
  13783. nk_zero_struct(*button);
  13784. button->normal = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  13785. button->hover = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  13786. button->active = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  13787. button->border_color = nk_rgba(0,0,0,0);
  13788. button->text_background = table[NK_COLOR_TAB_HEADER];
  13789. button->text_normal = table[NK_COLOR_TEXT];
  13790. button->text_hover = table[NK_COLOR_TEXT];
  13791. button->text_active = table[NK_COLOR_TEXT];
  13792. button->padding = nk_vec2(2.0f,2.0f);
  13793. button->touch_padding = nk_vec2(0.0f,0.0f);
  13794. button->userdata = nk_handle_ptr(0);
  13795. button->text_alignment = NK_TEXT_CENTERED;
  13796. button->border = 0.0f;
  13797. button->rounding = 0.0f;
  13798. button->draw_begin = 0;
  13799. button->draw_end = 0;
  13800. style->tab.tab_maximize_button =*button;
  13801. /* node button */
  13802. button = &style->tab.node_minimize_button;
  13803. nk_zero_struct(*button);
  13804. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13805. button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13806. button->active = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13807. button->border_color = nk_rgba(0,0,0,0);
  13808. button->text_background = table[NK_COLOR_TAB_HEADER];
  13809. button->text_normal = table[NK_COLOR_TEXT];
  13810. button->text_hover = table[NK_COLOR_TEXT];
  13811. button->text_active = table[NK_COLOR_TEXT];
  13812. button->padding = nk_vec2(2.0f,2.0f);
  13813. button->touch_padding = nk_vec2(0.0f,0.0f);
  13814. button->userdata = nk_handle_ptr(0);
  13815. button->text_alignment = NK_TEXT_CENTERED;
  13816. button->border = 0.0f;
  13817. button->rounding = 0.0f;
  13818. button->draw_begin = 0;
  13819. button->draw_end = 0;
  13820. style->tab.node_maximize_button =*button;
  13821. /* window header */
  13822. win = &style->window;
  13823. win->header.align = NK_HEADER_RIGHT;
  13824. win->header.close_symbol = NK_SYMBOL_X;
  13825. win->header.minimize_symbol = NK_SYMBOL_MINUS;
  13826. win->header.maximize_symbol = NK_SYMBOL_PLUS;
  13827. win->header.normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  13828. win->header.hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  13829. win->header.active = nk_style_item_color(table[NK_COLOR_HEADER]);
  13830. win->header.label_normal = table[NK_COLOR_TEXT];
  13831. win->header.label_hover = table[NK_COLOR_TEXT];
  13832. win->header.label_active = table[NK_COLOR_TEXT];
  13833. win->header.label_padding = nk_vec2(4,4);
  13834. win->header.padding = nk_vec2(4,4);
  13835. win->header.spacing = nk_vec2(0,0);
  13836. /* window header close button */
  13837. button = &style->window.header.close_button;
  13838. nk_zero_struct(*button);
  13839. button->normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  13840. button->hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  13841. button->active = nk_style_item_color(table[NK_COLOR_HEADER]);
  13842. button->border_color = nk_rgba(0,0,0,0);
  13843. button->text_background = table[NK_COLOR_HEADER];
  13844. button->text_normal = table[NK_COLOR_TEXT];
  13845. button->text_hover = table[NK_COLOR_TEXT];
  13846. button->text_active = table[NK_COLOR_TEXT];
  13847. button->padding = nk_vec2(0.0f,0.0f);
  13848. button->touch_padding = nk_vec2(0.0f,0.0f);
  13849. button->userdata = nk_handle_ptr(0);
  13850. button->text_alignment = NK_TEXT_CENTERED;
  13851. button->border = 0.0f;
  13852. button->rounding = 0.0f;
  13853. button->draw_begin = 0;
  13854. button->draw_end = 0;
  13855. /* window header minimize button */
  13856. button = &style->window.header.minimize_button;
  13857. nk_zero_struct(*button);
  13858. button->normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  13859. button->hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  13860. button->active = nk_style_item_color(table[NK_COLOR_HEADER]);
  13861. button->border_color = nk_rgba(0,0,0,0);
  13862. button->text_background = table[NK_COLOR_HEADER];
  13863. button->text_normal = table[NK_COLOR_TEXT];
  13864. button->text_hover = table[NK_COLOR_TEXT];
  13865. button->text_active = table[NK_COLOR_TEXT];
  13866. button->padding = nk_vec2(0.0f,0.0f);
  13867. button->touch_padding = nk_vec2(0.0f,0.0f);
  13868. button->userdata = nk_handle_ptr(0);
  13869. button->text_alignment = NK_TEXT_CENTERED;
  13870. button->border = 0.0f;
  13871. button->rounding = 0.0f;
  13872. button->draw_begin = 0;
  13873. button->draw_end = 0;
  13874. /* window */
  13875. win->background = table[NK_COLOR_WINDOW];
  13876. win->fixed_background = nk_style_item_color(table[NK_COLOR_WINDOW]);
  13877. win->border_color = table[NK_COLOR_BORDER];
  13878. win->popup_border_color = table[NK_COLOR_BORDER];
  13879. win->combo_border_color = table[NK_COLOR_BORDER];
  13880. win->contextual_border_color = table[NK_COLOR_BORDER];
  13881. win->menu_border_color = table[NK_COLOR_BORDER];
  13882. win->group_border_color = table[NK_COLOR_BORDER];
  13883. win->tooltip_border_color = table[NK_COLOR_BORDER];
  13884. win->scaler = nk_style_item_color(table[NK_COLOR_TEXT]);
  13885. win->rounding = 0.0f;
  13886. win->spacing = nk_vec2(4,4);
  13887. win->scrollbar_size = nk_vec2(10,10);
  13888. win->min_size = nk_vec2(64,64);
  13889. win->combo_border = 1.0f;
  13890. win->contextual_border = 1.0f;
  13891. win->menu_border = 1.0f;
  13892. win->group_border = 1.0f;
  13893. win->tooltip_border = 1.0f;
  13894. win->popup_border = 1.0f;
  13895. win->border = 2.0f;
  13896. win->min_row_height_padding = 8;
  13897. win->padding = nk_vec2(4,4);
  13898. win->group_padding = nk_vec2(4,4);
  13899. win->popup_padding = nk_vec2(4,4);
  13900. win->combo_padding = nk_vec2(4,4);
  13901. win->contextual_padding = nk_vec2(4,4);
  13902. win->menu_padding = nk_vec2(4,4);
  13903. win->tooltip_padding = nk_vec2(4,4);
  13904. }
  13905. NK_API void
  13906. nk_style_set_font(struct nk_context *ctx, const struct nk_user_font *font)
  13907. {
  13908. struct nk_style *style;
  13909. NK_ASSERT(ctx);
  13910. if (!ctx) return;
  13911. style = &ctx->style;
  13912. style->font = font;
  13913. ctx->stacks.fonts.head = 0;
  13914. if (ctx->current)
  13915. nk_layout_reset_min_row_height(ctx);
  13916. }
  13917. NK_API int
  13918. nk_style_push_font(struct nk_context *ctx, const struct nk_user_font *font)
  13919. {
  13920. struct nk_config_stack_user_font *font_stack;
  13921. struct nk_config_stack_user_font_element *element;
  13922. NK_ASSERT(ctx);
  13923. if (!ctx) return 0;
  13924. font_stack = &ctx->stacks.fonts;
  13925. NK_ASSERT(font_stack->head < (int)NK_LEN(font_stack->elements));
  13926. if (font_stack->head >= (int)NK_LEN(font_stack->elements))
  13927. return 0;
  13928. element = &font_stack->elements[font_stack->head++];
  13929. element->address = &ctx->style.font;
  13930. element->old_value = ctx->style.font;
  13931. ctx->style.font = font;
  13932. return 1;
  13933. }
  13934. NK_API int
  13935. nk_style_pop_font(struct nk_context *ctx)
  13936. {
  13937. struct nk_config_stack_user_font *font_stack;
  13938. struct nk_config_stack_user_font_element *element;
  13939. NK_ASSERT(ctx);
  13940. if (!ctx) return 0;
  13941. font_stack = &ctx->stacks.fonts;
  13942. NK_ASSERT(font_stack->head > 0);
  13943. if (font_stack->head < 1)
  13944. return 0;
  13945. element = &font_stack->elements[--font_stack->head];
  13946. *element->address = element->old_value;
  13947. return 1;
  13948. }
  13949. #define NK_STYLE_PUSH_IMPLEMENATION(prefix, type, stack) \
  13950. nk_style_push_##type(struct nk_context *ctx, prefix##_##type *address, prefix##_##type value)\
  13951. {\
  13952. struct nk_config_stack_##type * type_stack;\
  13953. struct nk_config_stack_##type##_element *element;\
  13954. NK_ASSERT(ctx);\
  13955. if (!ctx) return 0;\
  13956. type_stack = &ctx->stacks.stack;\
  13957. NK_ASSERT(type_stack->head < (int)NK_LEN(type_stack->elements));\
  13958. if (type_stack->head >= (int)NK_LEN(type_stack->elements))\
  13959. return 0;\
  13960. element = &type_stack->elements[type_stack->head++];\
  13961. element->address = address;\
  13962. element->old_value = *address;\
  13963. *address = value;\
  13964. return 1;\
  13965. }
  13966. #define NK_STYLE_POP_IMPLEMENATION(type, stack) \
  13967. nk_style_pop_##type(struct nk_context *ctx)\
  13968. {\
  13969. struct nk_config_stack_##type *type_stack;\
  13970. struct nk_config_stack_##type##_element *element;\
  13971. NK_ASSERT(ctx);\
  13972. if (!ctx) return 0;\
  13973. type_stack = &ctx->stacks.stack;\
  13974. NK_ASSERT(type_stack->head > 0);\
  13975. if (type_stack->head < 1)\
  13976. return 0;\
  13977. element = &type_stack->elements[--type_stack->head];\
  13978. *element->address = element->old_value;\
  13979. return 1;\
  13980. }
  13981. NK_API int NK_STYLE_PUSH_IMPLEMENATION(struct nk, style_item, style_items)
  13982. NK_API int NK_STYLE_PUSH_IMPLEMENATION(nk,float, floats)
  13983. NK_API int NK_STYLE_PUSH_IMPLEMENATION(struct nk, vec2, vectors)
  13984. NK_API int NK_STYLE_PUSH_IMPLEMENATION(nk,flags, flags)
  13985. NK_API int NK_STYLE_PUSH_IMPLEMENATION(struct nk,color, colors)
  13986. NK_API int NK_STYLE_POP_IMPLEMENATION(style_item, style_items)
  13987. NK_API int NK_STYLE_POP_IMPLEMENATION(float,floats)
  13988. NK_API int NK_STYLE_POP_IMPLEMENATION(vec2, vectors)
  13989. NK_API int NK_STYLE_POP_IMPLEMENATION(flags,flags)
  13990. NK_API int NK_STYLE_POP_IMPLEMENATION(color,colors)
  13991. NK_API int
  13992. nk_style_set_cursor(struct nk_context *ctx, enum nk_style_cursor c)
  13993. {
  13994. struct nk_style *style;
  13995. NK_ASSERT(ctx);
  13996. if (!ctx) return 0;
  13997. style = &ctx->style;
  13998. if (style->cursors[c]) {
  13999. style->cursor_active = style->cursors[c];
  14000. return 1;
  14001. }
  14002. return 0;
  14003. }
  14004. NK_API void
  14005. nk_style_show_cursor(struct nk_context *ctx)
  14006. {
  14007. ctx->style.cursor_visible = nk_true;
  14008. }
  14009. NK_API void
  14010. nk_style_hide_cursor(struct nk_context *ctx)
  14011. {
  14012. ctx->style.cursor_visible = nk_false;
  14013. }
  14014. NK_API void
  14015. nk_style_load_cursor(struct nk_context *ctx, enum nk_style_cursor cursor,
  14016. const struct nk_cursor *c)
  14017. {
  14018. struct nk_style *style;
  14019. NK_ASSERT(ctx);
  14020. if (!ctx) return;
  14021. style = &ctx->style;
  14022. style->cursors[cursor] = c;
  14023. }
  14024. NK_API void
  14025. nk_style_load_all_cursors(struct nk_context *ctx, struct nk_cursor *cursors)
  14026. {
  14027. int i = 0;
  14028. struct nk_style *style;
  14029. NK_ASSERT(ctx);
  14030. if (!ctx) return;
  14031. style = &ctx->style;
  14032. for (i = 0; i < NK_CURSOR_COUNT; ++i)
  14033. style->cursors[i] = &cursors[i];
  14034. style->cursor_visible = nk_true;
  14035. }
  14036. /* ==============================================================
  14037. *
  14038. * CONTEXT
  14039. *
  14040. * ===============================================================*/
  14041. NK_INTERN void
  14042. nk_setup(struct nk_context *ctx, const struct nk_user_font *font)
  14043. {
  14044. NK_ASSERT(ctx);
  14045. if (!ctx) return;
  14046. nk_zero_struct(*ctx);
  14047. nk_style_default(ctx);
  14048. ctx->seq = 1;
  14049. if (font) ctx->style.font = font;
  14050. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  14051. nk_draw_list_init(&ctx->draw_list);
  14052. #endif
  14053. }
  14054. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  14055. NK_API int
  14056. nk_init_default(struct nk_context *ctx, const struct nk_user_font *font)
  14057. {
  14058. struct nk_allocator alloc;
  14059. alloc.userdata.ptr = 0;
  14060. alloc.alloc = nk_malloc;
  14061. alloc.free = nk_mfree;
  14062. return nk_init(ctx, &alloc, font);
  14063. }
  14064. #endif
  14065. NK_API int
  14066. nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size,
  14067. const struct nk_user_font *font)
  14068. {
  14069. NK_ASSERT(memory);
  14070. if (!memory) return 0;
  14071. nk_setup(ctx, font);
  14072. nk_buffer_init_fixed(&ctx->memory, memory, size);
  14073. ctx->use_pool = nk_false;
  14074. return 1;
  14075. }
  14076. NK_API int
  14077. nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds,
  14078. struct nk_buffer *pool, const struct nk_user_font *font)
  14079. {
  14080. NK_ASSERT(cmds);
  14081. NK_ASSERT(pool);
  14082. if (!cmds || !pool) return 0;
  14083. nk_setup(ctx, font);
  14084. ctx->memory = *cmds;
  14085. if (pool->type == NK_BUFFER_FIXED) {
  14086. /* take memory from buffer and alloc fixed pool */
  14087. nk_pool_init_fixed(&ctx->pool, pool->memory.ptr, pool->memory.size);
  14088. } else {
  14089. /* create dynamic pool from buffer allocator */
  14090. struct nk_allocator *alloc = &pool->pool;
  14091. nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
  14092. }
  14093. ctx->use_pool = nk_true;
  14094. return 1;
  14095. }
  14096. NK_API int
  14097. nk_init(struct nk_context *ctx, struct nk_allocator *alloc,
  14098. const struct nk_user_font *font)
  14099. {
  14100. NK_ASSERT(alloc);
  14101. if (!alloc) return 0;
  14102. nk_setup(ctx, font);
  14103. nk_buffer_init(&ctx->memory, alloc, NK_DEFAULT_COMMAND_BUFFER_SIZE);
  14104. nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
  14105. ctx->use_pool = nk_true;
  14106. return 1;
  14107. }
  14108. #ifdef NK_INCLUDE_COMMAND_USERDATA
  14109. NK_API void
  14110. nk_set_user_data(struct nk_context *ctx, nk_handle handle)
  14111. {
  14112. if (!ctx) return;
  14113. ctx->userdata = handle;
  14114. if (ctx->current)
  14115. ctx->current->buffer.userdata = handle;
  14116. }
  14117. #endif
  14118. NK_API void
  14119. nk_free(struct nk_context *ctx)
  14120. {
  14121. NK_ASSERT(ctx);
  14122. if (!ctx) return;
  14123. nk_buffer_free(&ctx->memory);
  14124. if (ctx->use_pool)
  14125. nk_pool_free(&ctx->pool);
  14126. nk_zero(&ctx->input, sizeof(ctx->input));
  14127. nk_zero(&ctx->style, sizeof(ctx->style));
  14128. nk_zero(&ctx->memory, sizeof(ctx->memory));
  14129. ctx->seq = 0;
  14130. ctx->build = 0;
  14131. ctx->begin = 0;
  14132. ctx->end = 0;
  14133. ctx->active = 0;
  14134. ctx->current = 0;
  14135. ctx->freelist = 0;
  14136. ctx->count = 0;
  14137. }
  14138. NK_API void
  14139. nk_clear(struct nk_context *ctx)
  14140. {
  14141. struct nk_window *iter;
  14142. struct nk_window *next;
  14143. NK_ASSERT(ctx);
  14144. if (!ctx) return;
  14145. if (ctx->use_pool)
  14146. nk_buffer_clear(&ctx->memory);
  14147. else nk_buffer_reset(&ctx->memory, NK_BUFFER_FRONT);
  14148. ctx->build = 0;
  14149. ctx->memory.calls = 0;
  14150. ctx->last_widget_state = 0;
  14151. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
  14152. NK_MEMSET(&ctx->overlay, 0, sizeof(ctx->overlay));
  14153. /* garbage collector */
  14154. iter = ctx->begin;
  14155. while (iter) {
  14156. /* make sure valid minimized windows do not get removed */
  14157. if ((iter->flags & NK_WINDOW_MINIMIZED) &&
  14158. !(iter->flags & NK_WINDOW_CLOSED) &&
  14159. iter->seq == ctx->seq) {
  14160. iter = iter->next;
  14161. continue;
  14162. }
  14163. /* remove hotness from hidden or closed windows*/
  14164. if (((iter->flags & NK_WINDOW_HIDDEN) ||
  14165. (iter->flags & NK_WINDOW_CLOSED)) &&
  14166. iter == ctx->active) {
  14167. ctx->active = iter->prev;
  14168. ctx->end = iter->prev;
  14169. if (!ctx->end)
  14170. ctx->begin = 0;
  14171. if (ctx->active)
  14172. ctx->active->flags &= ~(unsigned)NK_WINDOW_ROM;
  14173. }
  14174. /* free unused popup windows */
  14175. if (iter->popup.win && iter->popup.win->seq != ctx->seq) {
  14176. nk_free_window(ctx, iter->popup.win);
  14177. iter->popup.win = 0;
  14178. }
  14179. /* remove unused window state tables */
  14180. {struct nk_table *n, *it = iter->tables;
  14181. while (it) {
  14182. n = it->next;
  14183. if (it->seq != ctx->seq) {
  14184. nk_remove_table(iter, it);
  14185. nk_zero(it, sizeof(union nk_page_data));
  14186. nk_free_table(ctx, it);
  14187. if (it == iter->tables)
  14188. iter->tables = n;
  14189. } it = n;
  14190. }}
  14191. /* window itself is not used anymore so free */
  14192. if (iter->seq != ctx->seq || iter->flags & NK_WINDOW_CLOSED) {
  14193. next = iter->next;
  14194. nk_remove_window(ctx, iter);
  14195. nk_free_window(ctx, iter);
  14196. iter = next;
  14197. } else iter = iter->next;
  14198. }
  14199. ctx->seq++;
  14200. }
  14201. NK_LIB void
  14202. nk_start_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
  14203. {
  14204. NK_ASSERT(ctx);
  14205. NK_ASSERT(buffer);
  14206. if (!ctx || !buffer) return;
  14207. buffer->begin = ctx->memory.allocated;
  14208. buffer->end = buffer->begin;
  14209. buffer->last = buffer->begin;
  14210. buffer->clip = nk_null_rect;
  14211. }
  14212. NK_LIB void
  14213. nk_start(struct nk_context *ctx, struct nk_window *win)
  14214. {
  14215. NK_ASSERT(ctx);
  14216. NK_ASSERT(win);
  14217. nk_start_buffer(ctx, &win->buffer);
  14218. }
  14219. NK_LIB void
  14220. nk_start_popup(struct nk_context *ctx, struct nk_window *win)
  14221. {
  14222. struct nk_popup_buffer *buf;
  14223. NK_ASSERT(ctx);
  14224. NK_ASSERT(win);
  14225. if (!ctx || !win) return;
  14226. /* save buffer fill state for popup */
  14227. buf = &win->popup.buf;
  14228. buf->begin = win->buffer.end;
  14229. buf->end = win->buffer.end;
  14230. buf->parent = win->buffer.last;
  14231. buf->last = buf->begin;
  14232. buf->active = nk_true;
  14233. }
  14234. NK_LIB void
  14235. nk_finish_popup(struct nk_context *ctx, struct nk_window *win)
  14236. {
  14237. struct nk_popup_buffer *buf;
  14238. NK_ASSERT(ctx);
  14239. NK_ASSERT(win);
  14240. if (!ctx || !win) return;
  14241. buf = &win->popup.buf;
  14242. buf->last = win->buffer.last;
  14243. buf->end = win->buffer.end;
  14244. }
  14245. NK_LIB void
  14246. nk_finish_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
  14247. {
  14248. NK_ASSERT(ctx);
  14249. NK_ASSERT(buffer);
  14250. if (!ctx || !buffer) return;
  14251. buffer->end = ctx->memory.allocated;
  14252. }
  14253. NK_LIB void
  14254. nk_finish(struct nk_context *ctx, struct nk_window *win)
  14255. {
  14256. struct nk_popup_buffer *buf;
  14257. struct nk_command *parent_last;
  14258. void *memory;
  14259. NK_ASSERT(ctx);
  14260. NK_ASSERT(win);
  14261. if (!ctx || !win) return;
  14262. nk_finish_buffer(ctx, &win->buffer);
  14263. if (!win->popup.buf.active) return;
  14264. buf = &win->popup.buf;
  14265. memory = ctx->memory.memory.ptr;
  14266. parent_last = nk_ptr_add(struct nk_command, memory, buf->parent);
  14267. parent_last->next = buf->end;
  14268. }
  14269. NK_LIB void
  14270. nk_build(struct nk_context *ctx)
  14271. {
  14272. struct nk_window *it = 0;
  14273. struct nk_command *cmd = 0;
  14274. nk_byte *buffer = 0;
  14275. /* draw cursor overlay */
  14276. if (!ctx->style.cursor_active)
  14277. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
  14278. if (ctx->style.cursor_active && !ctx->input.mouse.grabbed && ctx->style.cursor_visible) {
  14279. struct nk_rect mouse_bounds;
  14280. const struct nk_cursor *cursor = ctx->style.cursor_active;
  14281. nk_command_buffer_init(&ctx->overlay, &ctx->memory, NK_CLIPPING_OFF);
  14282. nk_start_buffer(ctx, &ctx->overlay);
  14283. mouse_bounds.x = ctx->input.mouse.pos.x - cursor->offset.x;
  14284. mouse_bounds.y = ctx->input.mouse.pos.y - cursor->offset.y;
  14285. mouse_bounds.w = cursor->size.x;
  14286. mouse_bounds.h = cursor->size.y;
  14287. nk_draw_image(&ctx->overlay, mouse_bounds, &cursor->img, nk_white);
  14288. nk_finish_buffer(ctx, &ctx->overlay);
  14289. }
  14290. /* build one big draw command list out of all window buffers */
  14291. it = ctx->begin;
  14292. buffer = (nk_byte*)ctx->memory.memory.ptr;
  14293. while (it != 0) {
  14294. struct nk_window *next = it->next;
  14295. if (it->buffer.last == it->buffer.begin || (it->flags & NK_WINDOW_HIDDEN)||
  14296. it->seq != ctx->seq)
  14297. goto cont;
  14298. cmd = nk_ptr_add(struct nk_command, buffer, it->buffer.last);
  14299. while (next && ((next->buffer.last == next->buffer.begin) ||
  14300. (next->flags & NK_WINDOW_HIDDEN) || next->seq != ctx->seq))
  14301. next = next->next; /* skip empty command buffers */
  14302. if (next) cmd->next = next->buffer.begin;
  14303. cont: it = next;
  14304. }
  14305. /* append all popup draw commands into lists */
  14306. it = ctx->begin;
  14307. while (it != 0) {
  14308. struct nk_window *next = it->next;
  14309. struct nk_popup_buffer *buf;
  14310. if (!it->popup.buf.active)
  14311. goto skip;
  14312. buf = &it->popup.buf;
  14313. cmd->next = buf->begin;
  14314. cmd = nk_ptr_add(struct nk_command, buffer, buf->last);
  14315. buf->active = nk_false;
  14316. skip: it = next;
  14317. }
  14318. if (cmd) {
  14319. /* append overlay commands */
  14320. if (ctx->overlay.end != ctx->overlay.begin)
  14321. cmd->next = ctx->overlay.begin;
  14322. else cmd->next = ctx->memory.allocated;
  14323. }
  14324. }
  14325. NK_API const struct nk_command*
  14326. nk__begin(struct nk_context *ctx)
  14327. {
  14328. struct nk_window *iter;
  14329. nk_byte *buffer;
  14330. NK_ASSERT(ctx);
  14331. if (!ctx) return 0;
  14332. if (!ctx->count) return 0;
  14333. buffer = (nk_byte*)ctx->memory.memory.ptr;
  14334. if (!ctx->build) {
  14335. nk_build(ctx);
  14336. ctx->build = nk_true;
  14337. }
  14338. iter = ctx->begin;
  14339. while (iter && ((iter->buffer.begin == iter->buffer.end) ||
  14340. (iter->flags & NK_WINDOW_HIDDEN) || iter->seq != ctx->seq))
  14341. iter = iter->next;
  14342. if (!iter) return 0;
  14343. return nk_ptr_add_const(struct nk_command, buffer, iter->buffer.begin);
  14344. }
  14345. NK_API const struct nk_command*
  14346. nk__next(struct nk_context *ctx, const struct nk_command *cmd)
  14347. {
  14348. nk_byte *buffer;
  14349. const struct nk_command *next;
  14350. NK_ASSERT(ctx);
  14351. if (!ctx || !cmd || !ctx->count) return 0;
  14352. if (cmd->next >= ctx->memory.allocated) return 0;
  14353. buffer = (nk_byte*)ctx->memory.memory.ptr;
  14354. next = nk_ptr_add_const(struct nk_command, buffer, cmd->next);
  14355. return next;
  14356. }
  14357. /* ===============================================================
  14358. *
  14359. * POOL
  14360. *
  14361. * ===============================================================*/
  14362. NK_LIB void
  14363. nk_pool_init(struct nk_pool *pool, struct nk_allocator *alloc,
  14364. unsigned int capacity)
  14365. {
  14366. nk_zero(pool, sizeof(*pool));
  14367. pool->alloc = *alloc;
  14368. pool->capacity = capacity;
  14369. pool->type = NK_BUFFER_DYNAMIC;
  14370. pool->pages = 0;
  14371. }
  14372. NK_LIB void
  14373. nk_pool_free(struct nk_pool *pool)
  14374. {
  14375. struct nk_page *iter = pool->pages;
  14376. if (!pool) return;
  14377. if (pool->type == NK_BUFFER_FIXED) return;
  14378. while (iter) {
  14379. struct nk_page *next = iter->next;
  14380. pool->alloc.free(pool->alloc.userdata, iter);
  14381. iter = next;
  14382. }
  14383. }
  14384. NK_LIB void
  14385. nk_pool_init_fixed(struct nk_pool *pool, void *memory, nk_size size)
  14386. {
  14387. nk_zero(pool, sizeof(*pool));
  14388. NK_ASSERT(size >= sizeof(struct nk_page));
  14389. if (size < sizeof(struct nk_page)) return;
  14390. pool->capacity = (unsigned)(size - sizeof(struct nk_page)) / sizeof(struct nk_page_element);
  14391. pool->pages = (struct nk_page*)memory;
  14392. pool->type = NK_BUFFER_FIXED;
  14393. pool->size = size;
  14394. }
  14395. NK_LIB struct nk_page_element*
  14396. nk_pool_alloc(struct nk_pool *pool)
  14397. {
  14398. if (!pool->pages || pool->pages->size >= pool->capacity) {
  14399. /* allocate new page */
  14400. struct nk_page *page;
  14401. if (pool->type == NK_BUFFER_FIXED) {
  14402. NK_ASSERT(pool->pages);
  14403. if (!pool->pages) return 0;
  14404. NK_ASSERT(pool->pages->size < pool->capacity);
  14405. return 0;
  14406. } else {
  14407. nk_size size = sizeof(struct nk_page);
  14408. size += NK_POOL_DEFAULT_CAPACITY * sizeof(union nk_page_data);
  14409. page = (struct nk_page*)pool->alloc.alloc(pool->alloc.userdata,0, size);
  14410. page->next = pool->pages;
  14411. pool->pages = page;
  14412. page->size = 0;
  14413. }
  14414. } return &pool->pages->win[pool->pages->size++];
  14415. }
  14416. /* ===============================================================
  14417. *
  14418. * PAGE ELEMENT
  14419. *
  14420. * ===============================================================*/
  14421. NK_LIB struct nk_page_element*
  14422. nk_create_page_element(struct nk_context *ctx)
  14423. {
  14424. struct nk_page_element *elem;
  14425. if (ctx->freelist) {
  14426. /* unlink page element from free list */
  14427. elem = ctx->freelist;
  14428. ctx->freelist = elem->next;
  14429. } else if (ctx->use_pool) {
  14430. /* allocate page element from memory pool */
  14431. elem = nk_pool_alloc(&ctx->pool);
  14432. NK_ASSERT(elem);
  14433. if (!elem) return 0;
  14434. } else {
  14435. /* allocate new page element from back of fixed size memory buffer */
  14436. NK_STORAGE const nk_size size = sizeof(struct nk_page_element);
  14437. NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_page_element);
  14438. elem = (struct nk_page_element*)nk_buffer_alloc(&ctx->memory, NK_BUFFER_BACK, size, align);
  14439. NK_ASSERT(elem);
  14440. if (!elem) return 0;
  14441. }
  14442. nk_zero_struct(*elem);
  14443. elem->next = 0;
  14444. elem->prev = 0;
  14445. return elem;
  14446. }
  14447. NK_LIB void
  14448. nk_link_page_element_into_freelist(struct nk_context *ctx,
  14449. struct nk_page_element *elem)
  14450. {
  14451. /* link table into freelist */
  14452. if (!ctx->freelist) {
  14453. ctx->freelist = elem;
  14454. } else {
  14455. elem->next = ctx->freelist;
  14456. ctx->freelist = elem;
  14457. }
  14458. }
  14459. NK_LIB void
  14460. nk_free_page_element(struct nk_context *ctx, struct nk_page_element *elem)
  14461. {
  14462. /* we have a pool so just add to free list */
  14463. if (ctx->use_pool) {
  14464. nk_link_page_element_into_freelist(ctx, elem);
  14465. return;
  14466. }
  14467. /* if possible remove last element from back of fixed memory buffer */
  14468. {void *elem_end = (void*)(elem + 1);
  14469. void *buffer_end = (nk_byte*)ctx->memory.memory.ptr + ctx->memory.size;
  14470. if (elem_end == buffer_end)
  14471. ctx->memory.size -= sizeof(struct nk_page_element);
  14472. else nk_link_page_element_into_freelist(ctx, elem);}
  14473. }
  14474. /* ===============================================================
  14475. *
  14476. * TABLE
  14477. *
  14478. * ===============================================================*/
  14479. NK_LIB struct nk_table*
  14480. nk_create_table(struct nk_context *ctx)
  14481. {
  14482. struct nk_page_element *elem;
  14483. elem = nk_create_page_element(ctx);
  14484. if (!elem) return 0;
  14485. nk_zero_struct(*elem);
  14486. return &elem->data.tbl;
  14487. }
  14488. NK_LIB void
  14489. nk_free_table(struct nk_context *ctx, struct nk_table *tbl)
  14490. {
  14491. union nk_page_data *pd = NK_CONTAINER_OF(tbl, union nk_page_data, tbl);
  14492. struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  14493. nk_free_page_element(ctx, pe);
  14494. }
  14495. NK_LIB void
  14496. nk_push_table(struct nk_window *win, struct nk_table *tbl)
  14497. {
  14498. if (!win->tables) {
  14499. win->tables = tbl;
  14500. tbl->next = 0;
  14501. tbl->prev = 0;
  14502. tbl->size = 0;
  14503. win->table_count = 1;
  14504. return;
  14505. }
  14506. win->tables->prev = tbl;
  14507. tbl->next = win->tables;
  14508. tbl->prev = 0;
  14509. tbl->size = 0;
  14510. win->tables = tbl;
  14511. win->table_count++;
  14512. }
  14513. NK_LIB void
  14514. nk_remove_table(struct nk_window *win, struct nk_table *tbl)
  14515. {
  14516. if (win->tables == tbl)
  14517. win->tables = tbl->next;
  14518. if (tbl->next)
  14519. tbl->next->prev = tbl->prev;
  14520. if (tbl->prev)
  14521. tbl->prev->next = tbl->next;
  14522. tbl->next = 0;
  14523. tbl->prev = 0;
  14524. }
  14525. NK_LIB nk_uint*
  14526. nk_add_value(struct nk_context *ctx, struct nk_window *win,
  14527. nk_hash name, nk_uint value)
  14528. {
  14529. NK_ASSERT(ctx);
  14530. NK_ASSERT(win);
  14531. if (!win || !ctx) return 0;
  14532. if (!win->tables || win->tables->size >= NK_VALUE_PAGE_CAPACITY) {
  14533. struct nk_table *tbl = nk_create_table(ctx);
  14534. NK_ASSERT(tbl);
  14535. if (!tbl) return 0;
  14536. nk_push_table(win, tbl);
  14537. }
  14538. win->tables->seq = win->seq;
  14539. win->tables->keys[win->tables->size] = name;
  14540. win->tables->values[win->tables->size] = value;
  14541. return &win->tables->values[win->tables->size++];
  14542. }
  14543. NK_LIB nk_uint*
  14544. nk_find_value(struct nk_window *win, nk_hash name)
  14545. {
  14546. struct nk_table *iter = win->tables;
  14547. while (iter) {
  14548. unsigned int i = 0;
  14549. unsigned int size = iter->size;
  14550. for (i = 0; i < size; ++i) {
  14551. if (iter->keys[i] == name) {
  14552. iter->seq = win->seq;
  14553. return &iter->values[i];
  14554. }
  14555. } size = NK_VALUE_PAGE_CAPACITY;
  14556. iter = iter->next;
  14557. }
  14558. return 0;
  14559. }
  14560. /* ===============================================================
  14561. *
  14562. * PANEL
  14563. *
  14564. * ===============================================================*/
  14565. NK_LIB void*
  14566. nk_create_panel(struct nk_context *ctx)
  14567. {
  14568. struct nk_page_element *elem;
  14569. elem = nk_create_page_element(ctx);
  14570. if (!elem) return 0;
  14571. nk_zero_struct(*elem);
  14572. return &elem->data.pan;
  14573. }
  14574. NK_LIB void
  14575. nk_free_panel(struct nk_context *ctx, struct nk_panel *pan)
  14576. {
  14577. union nk_page_data *pd = NK_CONTAINER_OF(pan, union nk_page_data, pan);
  14578. struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  14579. nk_free_page_element(ctx, pe);
  14580. }
  14581. NK_LIB int
  14582. nk_panel_has_header(nk_flags flags, const char *title)
  14583. {
  14584. int active = 0;
  14585. active = (flags & (NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE));
  14586. active = active || (flags & NK_WINDOW_TITLE);
  14587. active = active && !(flags & NK_WINDOW_HIDDEN) && title;
  14588. return active;
  14589. }
  14590. NK_LIB struct nk_vec2
  14591. nk_panel_get_padding(const struct nk_style *style, enum nk_panel_type type)
  14592. {
  14593. switch (type) {
  14594. default:
  14595. case NK_PANEL_WINDOW: return style->window.padding;
  14596. case NK_PANEL_GROUP: return style->window.group_padding;
  14597. case NK_PANEL_POPUP: return style->window.popup_padding;
  14598. case NK_PANEL_CONTEXTUAL: return style->window.contextual_padding;
  14599. case NK_PANEL_COMBO: return style->window.combo_padding;
  14600. case NK_PANEL_MENU: return style->window.menu_padding;
  14601. case NK_PANEL_TOOLTIP: return style->window.menu_padding;}
  14602. }
  14603. NK_LIB float
  14604. nk_panel_get_border(const struct nk_style *style, nk_flags flags,
  14605. enum nk_panel_type type)
  14606. {
  14607. if (flags & NK_WINDOW_BORDER) {
  14608. switch (type) {
  14609. default:
  14610. case NK_PANEL_WINDOW: return style->window.border;
  14611. case NK_PANEL_GROUP: return style->window.group_border;
  14612. case NK_PANEL_POPUP: return style->window.popup_border;
  14613. case NK_PANEL_CONTEXTUAL: return style->window.contextual_border;
  14614. case NK_PANEL_COMBO: return style->window.combo_border;
  14615. case NK_PANEL_MENU: return style->window.menu_border;
  14616. case NK_PANEL_TOOLTIP: return style->window.menu_border;
  14617. }} else return 0;
  14618. }
  14619. NK_LIB struct nk_color
  14620. nk_panel_get_border_color(const struct nk_style *style, enum nk_panel_type type)
  14621. {
  14622. switch (type) {
  14623. default:
  14624. case NK_PANEL_WINDOW: return style->window.border_color;
  14625. case NK_PANEL_GROUP: return style->window.group_border_color;
  14626. case NK_PANEL_POPUP: return style->window.popup_border_color;
  14627. case NK_PANEL_CONTEXTUAL: return style->window.contextual_border_color;
  14628. case NK_PANEL_COMBO: return style->window.combo_border_color;
  14629. case NK_PANEL_MENU: return style->window.menu_border_color;
  14630. case NK_PANEL_TOOLTIP: return style->window.menu_border_color;}
  14631. }
  14632. NK_LIB int
  14633. nk_panel_is_sub(enum nk_panel_type type)
  14634. {
  14635. return (type & NK_PANEL_SET_SUB)?1:0;
  14636. }
  14637. NK_LIB int
  14638. nk_panel_is_nonblock(enum nk_panel_type type)
  14639. {
  14640. return (type & NK_PANEL_SET_NONBLOCK)?1:0;
  14641. }
  14642. NK_LIB int
  14643. nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type panel_type)
  14644. {
  14645. struct nk_input *in;
  14646. struct nk_window *win;
  14647. struct nk_panel *layout;
  14648. struct nk_command_buffer *out;
  14649. const struct nk_style *style;
  14650. const struct nk_user_font *font;
  14651. struct nk_vec2 scrollbar_size;
  14652. struct nk_vec2 panel_padding;
  14653. NK_ASSERT(ctx);
  14654. NK_ASSERT(ctx->current);
  14655. NK_ASSERT(ctx->current->layout);
  14656. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  14657. nk_zero(ctx->current->layout, sizeof(*ctx->current->layout));
  14658. if ((ctx->current->flags & NK_WINDOW_HIDDEN) || (ctx->current->flags & NK_WINDOW_CLOSED)) {
  14659. nk_zero(ctx->current->layout, sizeof(struct nk_panel));
  14660. ctx->current->layout->type = panel_type;
  14661. return 0;
  14662. }
  14663. /* pull state into local stack */
  14664. style = &ctx->style;
  14665. font = style->font;
  14666. win = ctx->current;
  14667. layout = win->layout;
  14668. out = &win->buffer;
  14669. in = (win->flags & NK_WINDOW_NO_INPUT) ? 0: &ctx->input;
  14670. #ifdef NK_INCLUDE_COMMAND_USERDATA
  14671. win->buffer.userdata = ctx->userdata;
  14672. #endif
  14673. /* pull style configuration into local stack */
  14674. scrollbar_size = style->window.scrollbar_size;
  14675. panel_padding = nk_panel_get_padding(style, panel_type);
  14676. /* window movement */
  14677. if ((win->flags & NK_WINDOW_MOVABLE) && !(win->flags & NK_WINDOW_ROM)) {
  14678. int left_mouse_down;
  14679. int left_mouse_clicked;
  14680. int left_mouse_click_in_cursor;
  14681. /* calculate draggable window space */
  14682. struct nk_rect header;
  14683. header.x = win->bounds.x;
  14684. header.y = win->bounds.y;
  14685. header.w = win->bounds.w;
  14686. if (nk_panel_has_header(win->flags, title)) {
  14687. header.h = font->height + 2.0f * style->window.header.padding.y;
  14688. header.h += 2.0f * style->window.header.label_padding.y;
  14689. } else header.h = panel_padding.y;
  14690. /* window movement by dragging */
  14691. left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  14692. left_mouse_clicked = (int)in->mouse.buttons[NK_BUTTON_LEFT].clicked;
  14693. left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
  14694. NK_BUTTON_LEFT, header, nk_true);
  14695. if (left_mouse_down && left_mouse_click_in_cursor && !left_mouse_clicked) {
  14696. win->bounds.x = win->bounds.x + in->mouse.delta.x;
  14697. win->bounds.y = win->bounds.y + in->mouse.delta.y;
  14698. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x += in->mouse.delta.x;
  14699. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y += in->mouse.delta.y;
  14700. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_MOVE];
  14701. }
  14702. }
  14703. /* setup panel */
  14704. layout->type = panel_type;
  14705. layout->flags = win->flags;
  14706. layout->bounds = win->bounds;
  14707. layout->bounds.x += panel_padding.x;
  14708. layout->bounds.w -= 2*panel_padding.x;
  14709. if (win->flags & NK_WINDOW_BORDER) {
  14710. layout->border = nk_panel_get_border(style, win->flags, panel_type);
  14711. layout->bounds = nk_shrink_rect(layout->bounds, layout->border);
  14712. } else layout->border = 0;
  14713. layout->at_y = layout->bounds.y;
  14714. layout->at_x = layout->bounds.x;
  14715. layout->max_x = 0;
  14716. layout->header_height = 0;
  14717. layout->footer_height = 0;
  14718. nk_layout_reset_min_row_height(ctx);
  14719. layout->row.index = 0;
  14720. layout->row.columns = 0;
  14721. layout->row.ratio = 0;
  14722. layout->row.item_width = 0;
  14723. layout->row.tree_depth = 0;
  14724. layout->row.height = panel_padding.y;
  14725. layout->has_scrolling = nk_true;
  14726. if (!(win->flags & NK_WINDOW_NO_SCROLLBAR))
  14727. layout->bounds.w -= scrollbar_size.x;
  14728. if (!nk_panel_is_nonblock(panel_type)) {
  14729. layout->footer_height = 0;
  14730. if (!(win->flags & NK_WINDOW_NO_SCROLLBAR) || win->flags & NK_WINDOW_SCALABLE)
  14731. layout->footer_height = scrollbar_size.y;
  14732. layout->bounds.h -= layout->footer_height;
  14733. }
  14734. /* panel header */
  14735. if (nk_panel_has_header(win->flags, title))
  14736. {
  14737. struct nk_text text;
  14738. struct nk_rect header;
  14739. const struct nk_style_item *background = 0;
  14740. /* calculate header bounds */
  14741. header.x = win->bounds.x;
  14742. header.y = win->bounds.y;
  14743. header.w = win->bounds.w;
  14744. header.h = font->height + 2.0f * style->window.header.padding.y;
  14745. header.h += (2.0f * style->window.header.label_padding.y);
  14746. /* shrink panel by header */
  14747. layout->header_height = header.h;
  14748. layout->bounds.y += header.h;
  14749. layout->bounds.h -= header.h;
  14750. layout->at_y += header.h;
  14751. /* select correct header background and text color */
  14752. if (ctx->active == win) {
  14753. background = &style->window.header.active;
  14754. text.text = style->window.header.label_active;
  14755. } else if (nk_input_is_mouse_hovering_rect(&ctx->input, header)) {
  14756. background = &style->window.header.hover;
  14757. text.text = style->window.header.label_hover;
  14758. } else {
  14759. background = &style->window.header.normal;
  14760. text.text = style->window.header.label_normal;
  14761. }
  14762. /* draw header background */
  14763. header.h += 1.0f;
  14764. if (background->type == NK_STYLE_ITEM_IMAGE) {
  14765. text.background = nk_rgba(0,0,0,0);
  14766. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  14767. } else {
  14768. text.background = background->data.color;
  14769. nk_fill_rect(out, header, 0, background->data.color);
  14770. }
  14771. /* window close button */
  14772. {struct nk_rect button;
  14773. button.y = header.y + style->window.header.padding.y;
  14774. button.h = header.h - 2 * style->window.header.padding.y;
  14775. button.w = button.h;
  14776. if (win->flags & NK_WINDOW_CLOSABLE) {
  14777. nk_flags ws = 0;
  14778. if (style->window.header.align == NK_HEADER_RIGHT) {
  14779. button.x = (header.w + header.x) - (button.w + style->window.header.padding.x);
  14780. header.w -= button.w + style->window.header.spacing.x + style->window.header.padding.x;
  14781. } else {
  14782. button.x = header.x + style->window.header.padding.x;
  14783. header.x += button.w + style->window.header.spacing.x + style->window.header.padding.x;
  14784. }
  14785. if (nk_do_button_symbol(&ws, &win->buffer, button,
  14786. style->window.header.close_symbol, NK_BUTTON_DEFAULT,
  14787. &style->window.header.close_button, in, style->font) && !(win->flags & NK_WINDOW_ROM))
  14788. {
  14789. layout->flags |= NK_WINDOW_HIDDEN;
  14790. layout->flags &= (nk_flags)~NK_WINDOW_MINIMIZED;
  14791. }
  14792. }
  14793. /* window minimize button */
  14794. if (win->flags & NK_WINDOW_MINIMIZABLE) {
  14795. nk_flags ws = 0;
  14796. if (style->window.header.align == NK_HEADER_RIGHT) {
  14797. button.x = (header.w + header.x) - button.w;
  14798. if (!(win->flags & NK_WINDOW_CLOSABLE)) {
  14799. button.x -= style->window.header.padding.x;
  14800. header.w -= style->window.header.padding.x;
  14801. }
  14802. header.w -= button.w + style->window.header.spacing.x;
  14803. } else {
  14804. button.x = header.x;
  14805. header.x += button.w + style->window.header.spacing.x + style->window.header.padding.x;
  14806. }
  14807. if (nk_do_button_symbol(&ws, &win->buffer, button, (layout->flags & NK_WINDOW_MINIMIZED)?
  14808. style->window.header.maximize_symbol: style->window.header.minimize_symbol,
  14809. NK_BUTTON_DEFAULT, &style->window.header.minimize_button, in, style->font) && !(win->flags & NK_WINDOW_ROM))
  14810. layout->flags = (layout->flags & NK_WINDOW_MINIMIZED) ?
  14811. layout->flags & (nk_flags)~NK_WINDOW_MINIMIZED:
  14812. layout->flags | NK_WINDOW_MINIMIZED;
  14813. }}
  14814. {/* window header title */
  14815. int text_len = nk_strlen(title);
  14816. struct nk_rect label = {0,0,0,0};
  14817. float t = font->width(font->userdata, font->height, title, text_len);
  14818. text.padding = nk_vec2(0,0);
  14819. label.x = header.x + style->window.header.padding.x;
  14820. label.x += style->window.header.label_padding.x;
  14821. label.y = header.y + style->window.header.label_padding.y;
  14822. label.h = font->height + 2 * style->window.header.label_padding.y;
  14823. label.w = t + 2 * style->window.header.spacing.x;
  14824. label.w = NK_CLAMP(0, label.w, header.x + header.w - label.x);
  14825. nk_widget_text(out, label,(const char*)title, text_len, &text, NK_TEXT_LEFT, font);}
  14826. }
  14827. /* draw window background */
  14828. if (!(layout->flags & NK_WINDOW_MINIMIZED) && !(layout->flags & NK_WINDOW_DYNAMIC)) {
  14829. struct nk_rect body;
  14830. body.x = win->bounds.x;
  14831. body.w = win->bounds.w;
  14832. body.y = (win->bounds.y + layout->header_height);
  14833. body.h = (win->bounds.h - layout->header_height);
  14834. if (style->window.fixed_background.type == NK_STYLE_ITEM_IMAGE)
  14835. nk_draw_image(out, body, &style->window.fixed_background.data.image, nk_white);
  14836. else nk_fill_rect(out, body, 0, style->window.fixed_background.data.color);
  14837. }
  14838. /* set clipping rectangle */
  14839. {struct nk_rect clip;
  14840. layout->clip = layout->bounds;
  14841. nk_unify(&clip, &win->buffer.clip, layout->clip.x, layout->clip.y,
  14842. layout->clip.x + layout->clip.w, layout->clip.y + layout->clip.h);
  14843. nk_push_scissor(out, clip);
  14844. layout->clip = clip;}
  14845. return !(layout->flags & NK_WINDOW_HIDDEN) && !(layout->flags & NK_WINDOW_MINIMIZED);
  14846. }
  14847. NK_LIB void
  14848. nk_panel_end(struct nk_context *ctx)
  14849. {
  14850. struct nk_input *in;
  14851. struct nk_window *window;
  14852. struct nk_panel *layout;
  14853. const struct nk_style *style;
  14854. struct nk_command_buffer *out;
  14855. struct nk_vec2 scrollbar_size;
  14856. struct nk_vec2 panel_padding;
  14857. NK_ASSERT(ctx);
  14858. NK_ASSERT(ctx->current);
  14859. NK_ASSERT(ctx->current->layout);
  14860. if (!ctx || !ctx->current || !ctx->current->layout)
  14861. return;
  14862. window = ctx->current;
  14863. layout = window->layout;
  14864. style = &ctx->style;
  14865. out = &window->buffer;
  14866. in = (layout->flags & NK_WINDOW_ROM || layout->flags & NK_WINDOW_NO_INPUT) ? 0 :&ctx->input;
  14867. if (!nk_panel_is_sub(layout->type))
  14868. nk_push_scissor(out, nk_null_rect);
  14869. /* cache configuration data */
  14870. scrollbar_size = style->window.scrollbar_size;
  14871. panel_padding = nk_panel_get_padding(style, layout->type);
  14872. /* update the current cursor Y-position to point over the last added widget */
  14873. layout->at_y += layout->row.height;
  14874. /* dynamic panels */
  14875. if (layout->flags & NK_WINDOW_DYNAMIC && !(layout->flags & NK_WINDOW_MINIMIZED))
  14876. {
  14877. /* update panel height to fit dynamic growth */
  14878. struct nk_rect empty_space;
  14879. if (layout->at_y < (layout->bounds.y + layout->bounds.h))
  14880. layout->bounds.h = layout->at_y - layout->bounds.y;
  14881. /* fill top empty space */
  14882. empty_space.x = window->bounds.x;
  14883. empty_space.y = layout->bounds.y;
  14884. empty_space.h = panel_padding.y;
  14885. empty_space.w = window->bounds.w;
  14886. nk_fill_rect(out, empty_space, 0, style->window.background);
  14887. /* fill left empty space */
  14888. empty_space.x = window->bounds.x;
  14889. empty_space.y = layout->bounds.y;
  14890. empty_space.w = panel_padding.x + layout->border;
  14891. empty_space.h = layout->bounds.h;
  14892. nk_fill_rect(out, empty_space, 0, style->window.background);
  14893. /* fill right empty space */
  14894. empty_space.x = layout->bounds.x + layout->bounds.w;
  14895. empty_space.y = layout->bounds.y;
  14896. empty_space.w = panel_padding.x + layout->border;
  14897. empty_space.h = layout->bounds.h;
  14898. if (*layout->offset_y == 0 && !(layout->flags & NK_WINDOW_NO_SCROLLBAR))
  14899. empty_space.w += scrollbar_size.x;
  14900. nk_fill_rect(out, empty_space, 0, style->window.background);
  14901. /* fill bottom empty space */
  14902. if (layout->footer_height > 0) {
  14903. empty_space.x = window->bounds.x;
  14904. empty_space.y = layout->bounds.y + layout->bounds.h;
  14905. empty_space.w = window->bounds.w;
  14906. empty_space.h = layout->footer_height;
  14907. nk_fill_rect(out, empty_space, 0, style->window.background);
  14908. }
  14909. }
  14910. /* scrollbars */
  14911. if (!(layout->flags & NK_WINDOW_NO_SCROLLBAR) &&
  14912. !(layout->flags & NK_WINDOW_MINIMIZED) &&
  14913. window->scrollbar_hiding_timer < NK_SCROLLBAR_HIDING_TIMEOUT)
  14914. {
  14915. struct nk_rect scroll;
  14916. int scroll_has_scrolling;
  14917. float scroll_target;
  14918. float scroll_offset;
  14919. float scroll_step;
  14920. float scroll_inc;
  14921. /* mouse wheel scrolling */
  14922. if (nk_panel_is_sub(layout->type))
  14923. {
  14924. /* sub-window mouse wheel scrolling */
  14925. struct nk_window *root_window = window;
  14926. struct nk_panel *root_panel = window->layout;
  14927. while (root_panel->parent)
  14928. root_panel = root_panel->parent;
  14929. while (root_window->parent)
  14930. root_window = root_window->parent;
  14931. /* only allow scrolling if parent window is active */
  14932. scroll_has_scrolling = 0;
  14933. if ((root_window == ctx->active) && layout->has_scrolling) {
  14934. /* and panel is being hovered and inside clip rect*/
  14935. if (nk_input_is_mouse_hovering_rect(in, layout->bounds) &&
  14936. NK_INTERSECT(layout->bounds.x, layout->bounds.y, layout->bounds.w, layout->bounds.h,
  14937. root_panel->clip.x, root_panel->clip.y, root_panel->clip.w, root_panel->clip.h))
  14938. {
  14939. /* deactivate all parent scrolling */
  14940. root_panel = window->layout;
  14941. while (root_panel->parent) {
  14942. root_panel->has_scrolling = nk_false;
  14943. root_panel = root_panel->parent;
  14944. }
  14945. root_panel->has_scrolling = nk_false;
  14946. scroll_has_scrolling = nk_true;
  14947. }
  14948. }
  14949. } else if (!nk_panel_is_sub(layout->type)) {
  14950. /* window mouse wheel scrolling */
  14951. scroll_has_scrolling = (window == ctx->active) && layout->has_scrolling;
  14952. if (in && (in->mouse.scroll_delta.y > 0 || in->mouse.scroll_delta.x > 0) && scroll_has_scrolling)
  14953. window->scrolled = nk_true;
  14954. else window->scrolled = nk_false;
  14955. } else scroll_has_scrolling = nk_false;
  14956. {
  14957. /* vertical scrollbar */
  14958. nk_flags state = 0;
  14959. scroll.x = layout->bounds.x + layout->bounds.w + panel_padding.x;
  14960. scroll.y = layout->bounds.y;
  14961. scroll.w = scrollbar_size.x;
  14962. scroll.h = layout->bounds.h;
  14963. scroll_offset = (float)*layout->offset_y;
  14964. scroll_step = scroll.h * 0.10f;
  14965. scroll_inc = scroll.h * 0.01f;
  14966. scroll_target = (float)(int)(layout->at_y - scroll.y);
  14967. scroll_offset = nk_do_scrollbarv(&state, out, scroll, scroll_has_scrolling,
  14968. scroll_offset, scroll_target, scroll_step, scroll_inc,
  14969. &ctx->style.scrollv, in, style->font);
  14970. *layout->offset_y = (nk_uint)scroll_offset;
  14971. if (in && scroll_has_scrolling)
  14972. in->mouse.scroll_delta.y = 0;
  14973. }
  14974. {
  14975. /* horizontal scrollbar */
  14976. nk_flags state = 0;
  14977. scroll.x = layout->bounds.x;
  14978. scroll.y = layout->bounds.y + layout->bounds.h;
  14979. scroll.w = layout->bounds.w;
  14980. scroll.h = scrollbar_size.y;
  14981. scroll_offset = (float)*layout->offset_x;
  14982. scroll_target = (float)(int)(layout->max_x - scroll.x);
  14983. scroll_step = layout->max_x * 0.05f;
  14984. scroll_inc = layout->max_x * 0.005f;
  14985. scroll_offset = nk_do_scrollbarh(&state, out, scroll, scroll_has_scrolling,
  14986. scroll_offset, scroll_target, scroll_step, scroll_inc,
  14987. &ctx->style.scrollh, in, style->font);
  14988. *layout->offset_x = (nk_uint)scroll_offset;
  14989. }
  14990. }
  14991. /* hide scroll if no user input */
  14992. if (window->flags & NK_WINDOW_SCROLL_AUTO_HIDE) {
  14993. int has_input = ctx->input.mouse.delta.x != 0 || ctx->input.mouse.delta.y != 0 || ctx->input.mouse.scroll_delta.y != 0;
  14994. int is_window_hovered = nk_window_is_hovered(ctx);
  14995. int any_item_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
  14996. if ((!has_input && is_window_hovered) || (!is_window_hovered && !any_item_active))
  14997. window->scrollbar_hiding_timer += ctx->delta_time_seconds;
  14998. else window->scrollbar_hiding_timer = 0;
  14999. } else window->scrollbar_hiding_timer = 0;
  15000. /* window border */
  15001. if (layout->flags & NK_WINDOW_BORDER)
  15002. {
  15003. struct nk_color border_color = nk_panel_get_border_color(style, layout->type);
  15004. const float padding_y = (layout->flags & NK_WINDOW_MINIMIZED)
  15005. ? (style->window.border + window->bounds.y + layout->header_height)
  15006. : ((layout->flags & NK_WINDOW_DYNAMIC)
  15007. ? (layout->bounds.y + layout->bounds.h + layout->footer_height)
  15008. : (window->bounds.y + window->bounds.h));
  15009. struct nk_rect b = window->bounds;
  15010. b.h = padding_y - window->bounds.y;
  15011. nk_stroke_rect(out, b, 0, layout->border, border_color);
  15012. }
  15013. /* scaler */
  15014. if ((layout->flags & NK_WINDOW_SCALABLE) && in && !(layout->flags & NK_WINDOW_MINIMIZED))
  15015. {
  15016. /* calculate scaler bounds */
  15017. struct nk_rect scaler;
  15018. scaler.w = scrollbar_size.x;
  15019. scaler.h = scrollbar_size.y;
  15020. scaler.y = layout->bounds.y + layout->bounds.h;
  15021. if (layout->flags & NK_WINDOW_SCALE_LEFT)
  15022. scaler.x = layout->bounds.x - panel_padding.x * 0.5f;
  15023. else scaler.x = layout->bounds.x + layout->bounds.w + panel_padding.x;
  15024. if (layout->flags & NK_WINDOW_NO_SCROLLBAR)
  15025. scaler.x -= scaler.w;
  15026. /* draw scaler */
  15027. {const struct nk_style_item *item = &style->window.scaler;
  15028. if (item->type == NK_STYLE_ITEM_IMAGE)
  15029. nk_draw_image(out, scaler, &item->data.image, nk_white);
  15030. else {
  15031. if (layout->flags & NK_WINDOW_SCALE_LEFT) {
  15032. nk_fill_triangle(out, scaler.x, scaler.y, scaler.x,
  15033. scaler.y + scaler.h, scaler.x + scaler.w,
  15034. scaler.y + scaler.h, item->data.color);
  15035. } else {
  15036. nk_fill_triangle(out, scaler.x + scaler.w, scaler.y, scaler.x + scaler.w,
  15037. scaler.y + scaler.h, scaler.x, scaler.y + scaler.h, item->data.color);
  15038. }
  15039. }}
  15040. /* do window scaling */
  15041. if (!(window->flags & NK_WINDOW_ROM)) {
  15042. struct nk_vec2 window_size = style->window.min_size;
  15043. int left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  15044. int left_mouse_click_in_scaler = nk_input_has_mouse_click_down_in_rect(in,
  15045. NK_BUTTON_LEFT, scaler, nk_true);
  15046. if (left_mouse_down && left_mouse_click_in_scaler) {
  15047. float delta_x = in->mouse.delta.x;
  15048. if (layout->flags & NK_WINDOW_SCALE_LEFT) {
  15049. delta_x = -delta_x;
  15050. window->bounds.x += in->mouse.delta.x;
  15051. }
  15052. /* dragging in x-direction */
  15053. if (window->bounds.w + delta_x >= window_size.x) {
  15054. if ((delta_x < 0) || (delta_x > 0 && in->mouse.pos.x >= scaler.x)) {
  15055. window->bounds.w = window->bounds.w + delta_x;
  15056. scaler.x += in->mouse.delta.x;
  15057. }
  15058. }
  15059. /* dragging in y-direction (only possible if static window) */
  15060. if (!(layout->flags & NK_WINDOW_DYNAMIC)) {
  15061. if (window_size.y < window->bounds.h + in->mouse.delta.y) {
  15062. if ((in->mouse.delta.y < 0) || (in->mouse.delta.y > 0 && in->mouse.pos.y >= scaler.y)) {
  15063. window->bounds.h = window->bounds.h + in->mouse.delta.y;
  15064. scaler.y += in->mouse.delta.y;
  15065. }
  15066. }
  15067. }
  15068. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_RESIZE_TOP_RIGHT_DOWN_LEFT];
  15069. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = scaler.x + scaler.w/2.0f;
  15070. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = scaler.y + scaler.h/2.0f;
  15071. }
  15072. }
  15073. }
  15074. if (!nk_panel_is_sub(layout->type)) {
  15075. /* window is hidden so clear command buffer */
  15076. if (layout->flags & NK_WINDOW_HIDDEN)
  15077. nk_command_buffer_reset(&window->buffer);
  15078. /* window is visible and not tab */
  15079. else nk_finish(ctx, window);
  15080. }
  15081. /* NK_WINDOW_REMOVE_ROM flag was set so remove NK_WINDOW_ROM */
  15082. if (layout->flags & NK_WINDOW_REMOVE_ROM) {
  15083. layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
  15084. layout->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
  15085. }
  15086. window->flags = layout->flags;
  15087. /* property garbage collector */
  15088. if (window->property.active && window->property.old != window->property.seq &&
  15089. window->property.active == window->property.prev) {
  15090. nk_zero(&window->property, sizeof(window->property));
  15091. } else {
  15092. window->property.old = window->property.seq;
  15093. window->property.prev = window->property.active;
  15094. window->property.seq = 0;
  15095. }
  15096. /* edit garbage collector */
  15097. if (window->edit.active && window->edit.old != window->edit.seq &&
  15098. window->edit.active == window->edit.prev) {
  15099. nk_zero(&window->edit, sizeof(window->edit));
  15100. } else {
  15101. window->edit.old = window->edit.seq;
  15102. window->edit.prev = window->edit.active;
  15103. window->edit.seq = 0;
  15104. }
  15105. /* contextual garbage collector */
  15106. if (window->popup.active_con && window->popup.con_old != window->popup.con_count) {
  15107. window->popup.con_count = 0;
  15108. window->popup.con_old = 0;
  15109. window->popup.active_con = 0;
  15110. } else {
  15111. window->popup.con_old = window->popup.con_count;
  15112. window->popup.con_count = 0;
  15113. }
  15114. window->popup.combo_count = 0;
  15115. /* helper to make sure you have a 'nk_tree_push' for every 'nk_tree_pop' */
  15116. NK_ASSERT(!layout->row.tree_depth);
  15117. }
  15118. /* ===============================================================
  15119. *
  15120. * WINDOW
  15121. *
  15122. * ===============================================================*/
  15123. NK_LIB void*
  15124. nk_create_window(struct nk_context *ctx)
  15125. {
  15126. struct nk_page_element *elem;
  15127. elem = nk_create_page_element(ctx);
  15128. if (!elem) return 0;
  15129. elem->data.win.seq = ctx->seq;
  15130. return &elem->data.win;
  15131. }
  15132. NK_LIB void
  15133. nk_free_window(struct nk_context *ctx, struct nk_window *win)
  15134. {
  15135. /* unlink windows from list */
  15136. struct nk_table *it = win->tables;
  15137. if (win->popup.win) {
  15138. nk_free_window(ctx, win->popup.win);
  15139. win->popup.win = 0;
  15140. }
  15141. win->next = 0;
  15142. win->prev = 0;
  15143. while (it) {
  15144. /*free window state tables */
  15145. struct nk_table *n = it->next;
  15146. nk_remove_table(win, it);
  15147. nk_free_table(ctx, it);
  15148. if (it == win->tables)
  15149. win->tables = n;
  15150. it = n;
  15151. }
  15152. /* link windows into freelist */
  15153. {union nk_page_data *pd = NK_CONTAINER_OF(win, union nk_page_data, win);
  15154. struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  15155. nk_free_page_element(ctx, pe);}
  15156. }
  15157. NK_LIB struct nk_window*
  15158. nk_find_window(struct nk_context *ctx, nk_hash hash, const char *name)
  15159. {
  15160. struct nk_window *iter;
  15161. iter = ctx->begin;
  15162. while (iter) {
  15163. NK_ASSERT(iter != iter->next);
  15164. if (iter->name == hash) {
  15165. int max_len = nk_strlen(iter->name_string);
  15166. if (!nk_stricmpn(iter->name_string, name, max_len))
  15167. return iter;
  15168. }
  15169. iter = iter->next;
  15170. }
  15171. return 0;
  15172. }
  15173. NK_LIB void
  15174. nk_insert_window(struct nk_context *ctx, struct nk_window *win,
  15175. enum nk_window_insert_location loc)
  15176. {
  15177. const struct nk_window *iter;
  15178. NK_ASSERT(ctx);
  15179. NK_ASSERT(win);
  15180. if (!win || !ctx) return;
  15181. iter = ctx->begin;
  15182. while (iter) {
  15183. NK_ASSERT(iter != iter->next);
  15184. NK_ASSERT(iter != win);
  15185. if (iter == win) return;
  15186. iter = iter->next;
  15187. }
  15188. if (!ctx->begin) {
  15189. win->next = 0;
  15190. win->prev = 0;
  15191. ctx->begin = win;
  15192. ctx->end = win;
  15193. ctx->count = 1;
  15194. return;
  15195. }
  15196. if (loc == NK_INSERT_BACK) {
  15197. struct nk_window *end;
  15198. end = ctx->end;
  15199. end->flags |= NK_WINDOW_ROM;
  15200. end->next = win;
  15201. win->prev = ctx->end;
  15202. win->next = 0;
  15203. ctx->end = win;
  15204. ctx->active = ctx->end;
  15205. ctx->end->flags &= ~(nk_flags)NK_WINDOW_ROM;
  15206. } else {
  15207. /*ctx->end->flags |= NK_WINDOW_ROM;*/
  15208. ctx->begin->prev = win;
  15209. win->next = ctx->begin;
  15210. win->prev = 0;
  15211. ctx->begin = win;
  15212. ctx->begin->flags &= ~(nk_flags)NK_WINDOW_ROM;
  15213. }
  15214. ctx->count++;
  15215. }
  15216. NK_LIB void
  15217. nk_remove_window(struct nk_context *ctx, struct nk_window *win)
  15218. {
  15219. if (win == ctx->begin || win == ctx->end) {
  15220. if (win == ctx->begin) {
  15221. ctx->begin = win->next;
  15222. if (win->next)
  15223. win->next->prev = 0;
  15224. }
  15225. if (win == ctx->end) {
  15226. ctx->end = win->prev;
  15227. if (win->prev)
  15228. win->prev->next = 0;
  15229. }
  15230. } else {
  15231. if (win->next)
  15232. win->next->prev = win->prev;
  15233. if (win->prev)
  15234. win->prev->next = win->next;
  15235. }
  15236. if (win == ctx->active || !ctx->active) {
  15237. ctx->active = ctx->end;
  15238. if (ctx->end)
  15239. ctx->end->flags &= ~(nk_flags)NK_WINDOW_ROM;
  15240. }
  15241. win->next = 0;
  15242. win->prev = 0;
  15243. ctx->count--;
  15244. }
  15245. NK_API int
  15246. nk_begin(struct nk_context *ctx, const char *title,
  15247. struct nk_rect bounds, nk_flags flags)
  15248. {
  15249. return nk_begin_titled(ctx, title, title, bounds, flags);
  15250. }
  15251. NK_API int
  15252. nk_begin_titled(struct nk_context *ctx, const char *name, const char *title,
  15253. struct nk_rect bounds, nk_flags flags)
  15254. {
  15255. struct nk_window *win;
  15256. struct nk_style *style;
  15257. nk_hash name_hash;
  15258. int name_len;
  15259. int ret = 0;
  15260. NK_ASSERT(ctx);
  15261. NK_ASSERT(name);
  15262. NK_ASSERT(title);
  15263. NK_ASSERT(ctx->style.font && ctx->style.font->width && "if this triggers you forgot to add a font");
  15264. NK_ASSERT(!ctx->current && "if this triggers you missed a `nk_end` call");
  15265. if (!ctx || ctx->current || !title || !name)
  15266. return 0;
  15267. /* find or create window */
  15268. style = &ctx->style;
  15269. name_len = (int)nk_strlen(name);
  15270. name_hash = nk_murmur_hash(name, (int)name_len, NK_WINDOW_TITLE);
  15271. win = nk_find_window(ctx, name_hash, name);
  15272. if (!win) {
  15273. /* create new window */
  15274. nk_size name_length = (nk_size)name_len;
  15275. win = (struct nk_window*)nk_create_window(ctx);
  15276. NK_ASSERT(win);
  15277. if (!win) return 0;
  15278. if (flags & NK_WINDOW_BACKGROUND)
  15279. nk_insert_window(ctx, win, NK_INSERT_FRONT);
  15280. else nk_insert_window(ctx, win, NK_INSERT_BACK);
  15281. nk_command_buffer_init(&win->buffer, &ctx->memory, NK_CLIPPING_ON);
  15282. win->flags = flags;
  15283. win->bounds = bounds;
  15284. win->name = name_hash;
  15285. name_length = NK_MIN(name_length, NK_WINDOW_MAX_NAME-1);
  15286. NK_MEMCPY(win->name_string, name, name_length);
  15287. win->name_string[name_length] = 0;
  15288. win->popup.win = 0;
  15289. if (!ctx->active)
  15290. ctx->active = win;
  15291. } else {
  15292. /* update window */
  15293. win->flags &= ~(nk_flags)(NK_WINDOW_PRIVATE-1);
  15294. win->flags |= flags;
  15295. if (!(win->flags & (NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE)))
  15296. win->bounds = bounds;
  15297. /* If this assert triggers you either:
  15298. *
  15299. * I.) Have more than one window with the same name or
  15300. * II.) You forgot to actually draw the window.
  15301. * More specific you did not call `nk_clear` (nk_clear will be
  15302. * automatically called for you if you are using one of the
  15303. * provided demo backends). */
  15304. NK_ASSERT(win->seq != ctx->seq);
  15305. win->seq = ctx->seq;
  15306. if (!ctx->active && !(win->flags & NK_WINDOW_HIDDEN)) {
  15307. ctx->active = win;
  15308. ctx->end = win;
  15309. }
  15310. }
  15311. if (win->flags & NK_WINDOW_HIDDEN) {
  15312. ctx->current = win;
  15313. win->layout = 0;
  15314. return 0;
  15315. } else nk_start(ctx, win);
  15316. /* window overlapping */
  15317. if (!(win->flags & NK_WINDOW_HIDDEN) && !(win->flags & NK_WINDOW_NO_INPUT))
  15318. {
  15319. int inpanel, ishovered;
  15320. struct nk_window *iter = win;
  15321. float h = ctx->style.font->height + 2.0f * style->window.header.padding.y +
  15322. (2.0f * style->window.header.label_padding.y);
  15323. struct nk_rect win_bounds = (!(win->flags & NK_WINDOW_MINIMIZED))?
  15324. win->bounds: nk_rect(win->bounds.x, win->bounds.y, win->bounds.w, h);
  15325. /* activate window if hovered and no other window is overlapping this window */
  15326. inpanel = nk_input_has_mouse_click_down_in_rect(&ctx->input, NK_BUTTON_LEFT, win_bounds, nk_true);
  15327. inpanel = inpanel && ctx->input.mouse.buttons[NK_BUTTON_LEFT].clicked;
  15328. ishovered = nk_input_is_mouse_hovering_rect(&ctx->input, win_bounds);
  15329. if ((win != ctx->active) && ishovered && !ctx->input.mouse.buttons[NK_BUTTON_LEFT].down) {
  15330. iter = win->next;
  15331. while (iter) {
  15332. struct nk_rect iter_bounds = (!(iter->flags & NK_WINDOW_MINIMIZED))?
  15333. iter->bounds: nk_rect(iter->bounds.x, iter->bounds.y, iter->bounds.w, h);
  15334. if (NK_INTERSECT(win_bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  15335. iter_bounds.x, iter_bounds.y, iter_bounds.w, iter_bounds.h) &&
  15336. (!(iter->flags & NK_WINDOW_HIDDEN)))
  15337. break;
  15338. if (iter->popup.win && iter->popup.active && !(iter->flags & NK_WINDOW_HIDDEN) &&
  15339. NK_INTERSECT(win->bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  15340. iter->popup.win->bounds.x, iter->popup.win->bounds.y,
  15341. iter->popup.win->bounds.w, iter->popup.win->bounds.h))
  15342. break;
  15343. iter = iter->next;
  15344. }
  15345. }
  15346. /* activate window if clicked */
  15347. if (iter && inpanel && (win != ctx->end)) {
  15348. iter = win->next;
  15349. while (iter) {
  15350. /* try to find a panel with higher priority in the same position */
  15351. struct nk_rect iter_bounds = (!(iter->flags & NK_WINDOW_MINIMIZED))?
  15352. iter->bounds: nk_rect(iter->bounds.x, iter->bounds.y, iter->bounds.w, h);
  15353. if (NK_INBOX(ctx->input.mouse.pos.x, ctx->input.mouse.pos.y,
  15354. iter_bounds.x, iter_bounds.y, iter_bounds.w, iter_bounds.h) &&
  15355. !(iter->flags & NK_WINDOW_HIDDEN))
  15356. break;
  15357. if (iter->popup.win && iter->popup.active && !(iter->flags & NK_WINDOW_HIDDEN) &&
  15358. NK_INTERSECT(win_bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  15359. iter->popup.win->bounds.x, iter->popup.win->bounds.y,
  15360. iter->popup.win->bounds.w, iter->popup.win->bounds.h))
  15361. break;
  15362. iter = iter->next;
  15363. }
  15364. }
  15365. if (iter && !(win->flags & NK_WINDOW_ROM) && (win->flags & NK_WINDOW_BACKGROUND)) {
  15366. win->flags |= (nk_flags)NK_WINDOW_ROM;
  15367. iter->flags &= ~(nk_flags)NK_WINDOW_ROM;
  15368. ctx->active = iter;
  15369. if (!(iter->flags & NK_WINDOW_BACKGROUND)) {
  15370. /* current window is active in that position so transfer to top
  15371. * at the highest priority in stack */
  15372. nk_remove_window(ctx, iter);
  15373. nk_insert_window(ctx, iter, NK_INSERT_BACK);
  15374. }
  15375. } else {
  15376. if (!iter && ctx->end != win) {
  15377. if (!(win->flags & NK_WINDOW_BACKGROUND)) {
  15378. /* current window is active in that position so transfer to top
  15379. * at the highest priority in stack */
  15380. nk_remove_window(ctx, win);
  15381. nk_insert_window(ctx, win, NK_INSERT_BACK);
  15382. }
  15383. win->flags &= ~(nk_flags)NK_WINDOW_ROM;
  15384. ctx->active = win;
  15385. }
  15386. if (ctx->end != win && !(win->flags & NK_WINDOW_BACKGROUND))
  15387. win->flags |= NK_WINDOW_ROM;
  15388. }
  15389. }
  15390. win->layout = (struct nk_panel*)nk_create_panel(ctx);
  15391. ctx->current = win;
  15392. ret = nk_panel_begin(ctx, title, NK_PANEL_WINDOW);
  15393. win->layout->offset_x = &win->scrollbar.x;
  15394. win->layout->offset_y = &win->scrollbar.y;
  15395. return ret;
  15396. }
  15397. NK_API void
  15398. nk_end(struct nk_context *ctx)
  15399. {
  15400. struct nk_panel *layout;
  15401. NK_ASSERT(ctx);
  15402. NK_ASSERT(ctx->current && "if this triggers you forgot to call `nk_begin`");
  15403. if (!ctx || !ctx->current)
  15404. return;
  15405. layout = ctx->current->layout;
  15406. if (!layout || (layout->type == NK_PANEL_WINDOW && (ctx->current->flags & NK_WINDOW_HIDDEN))) {
  15407. ctx->current = 0;
  15408. return;
  15409. }
  15410. nk_panel_end(ctx);
  15411. nk_free_panel(ctx, ctx->current->layout);
  15412. ctx->current = 0;
  15413. }
  15414. NK_API struct nk_rect
  15415. nk_window_get_bounds(const struct nk_context *ctx)
  15416. {
  15417. NK_ASSERT(ctx);
  15418. NK_ASSERT(ctx->current);
  15419. if (!ctx || !ctx->current) return nk_rect(0,0,0,0);
  15420. return ctx->current->bounds;
  15421. }
  15422. NK_API struct nk_vec2
  15423. nk_window_get_position(const struct nk_context *ctx)
  15424. {
  15425. NK_ASSERT(ctx);
  15426. NK_ASSERT(ctx->current);
  15427. if (!ctx || !ctx->current) return nk_vec2(0,0);
  15428. return nk_vec2(ctx->current->bounds.x, ctx->current->bounds.y);
  15429. }
  15430. NK_API struct nk_vec2
  15431. nk_window_get_size(const struct nk_context *ctx)
  15432. {
  15433. NK_ASSERT(ctx);
  15434. NK_ASSERT(ctx->current);
  15435. if (!ctx || !ctx->current) return nk_vec2(0,0);
  15436. return nk_vec2(ctx->current->bounds.w, ctx->current->bounds.h);
  15437. }
  15438. NK_API float
  15439. nk_window_get_width(const struct nk_context *ctx)
  15440. {
  15441. NK_ASSERT(ctx);
  15442. NK_ASSERT(ctx->current);
  15443. if (!ctx || !ctx->current) return 0;
  15444. return ctx->current->bounds.w;
  15445. }
  15446. NK_API float
  15447. nk_window_get_height(const struct nk_context *ctx)
  15448. {
  15449. NK_ASSERT(ctx);
  15450. NK_ASSERT(ctx->current);
  15451. if (!ctx || !ctx->current) return 0;
  15452. return ctx->current->bounds.h;
  15453. }
  15454. NK_API struct nk_rect
  15455. nk_window_get_content_region(struct nk_context *ctx)
  15456. {
  15457. NK_ASSERT(ctx);
  15458. NK_ASSERT(ctx->current);
  15459. if (!ctx || !ctx->current) return nk_rect(0,0,0,0);
  15460. return ctx->current->layout->clip;
  15461. }
  15462. NK_API struct nk_vec2
  15463. nk_window_get_content_region_min(struct nk_context *ctx)
  15464. {
  15465. NK_ASSERT(ctx);
  15466. NK_ASSERT(ctx->current);
  15467. NK_ASSERT(ctx->current->layout);
  15468. if (!ctx || !ctx->current) return nk_vec2(0,0);
  15469. return nk_vec2(ctx->current->layout->clip.x, ctx->current->layout->clip.y);
  15470. }
  15471. NK_API struct nk_vec2
  15472. nk_window_get_content_region_max(struct nk_context *ctx)
  15473. {
  15474. NK_ASSERT(ctx);
  15475. NK_ASSERT(ctx->current);
  15476. NK_ASSERT(ctx->current->layout);
  15477. if (!ctx || !ctx->current) return nk_vec2(0,0);
  15478. return nk_vec2(ctx->current->layout->clip.x + ctx->current->layout->clip.w,
  15479. ctx->current->layout->clip.y + ctx->current->layout->clip.h);
  15480. }
  15481. NK_API struct nk_vec2
  15482. nk_window_get_content_region_size(struct nk_context *ctx)
  15483. {
  15484. NK_ASSERT(ctx);
  15485. NK_ASSERT(ctx->current);
  15486. NK_ASSERT(ctx->current->layout);
  15487. if (!ctx || !ctx->current) return nk_vec2(0,0);
  15488. return nk_vec2(ctx->current->layout->clip.w, ctx->current->layout->clip.h);
  15489. }
  15490. NK_API struct nk_command_buffer*
  15491. nk_window_get_canvas(struct nk_context *ctx)
  15492. {
  15493. NK_ASSERT(ctx);
  15494. NK_ASSERT(ctx->current);
  15495. NK_ASSERT(ctx->current->layout);
  15496. if (!ctx || !ctx->current) return 0;
  15497. return &ctx->current->buffer;
  15498. }
  15499. NK_API struct nk_panel*
  15500. nk_window_get_panel(struct nk_context *ctx)
  15501. {
  15502. NK_ASSERT(ctx);
  15503. NK_ASSERT(ctx->current);
  15504. if (!ctx || !ctx->current) return 0;
  15505. return ctx->current->layout;
  15506. }
  15507. NK_API void
  15508. nk_window_get_scroll(struct nk_context *ctx, nk_uint *offset_x, nk_uint *offset_y)
  15509. {
  15510. struct nk_window *win;
  15511. NK_ASSERT(ctx);
  15512. NK_ASSERT(ctx->current);
  15513. if (!ctx || !ctx->current)
  15514. return ;
  15515. win = ctx->current;
  15516. if (offset_x)
  15517. *offset_x = win->scrollbar.x;
  15518. if (offset_y)
  15519. *offset_y = win->scrollbar.y;
  15520. }
  15521. NK_API int
  15522. nk_window_has_focus(const struct nk_context *ctx)
  15523. {
  15524. NK_ASSERT(ctx);
  15525. NK_ASSERT(ctx->current);
  15526. NK_ASSERT(ctx->current->layout);
  15527. if (!ctx || !ctx->current) return 0;
  15528. return ctx->current == ctx->active;
  15529. }
  15530. NK_API int
  15531. nk_window_is_hovered(struct nk_context *ctx)
  15532. {
  15533. NK_ASSERT(ctx);
  15534. NK_ASSERT(ctx->current);
  15535. if (!ctx || !ctx->current) return 0;
  15536. if(ctx->current->flags & NK_WINDOW_HIDDEN)
  15537. return 0;
  15538. return nk_input_is_mouse_hovering_rect(&ctx->input, ctx->current->bounds);
  15539. }
  15540. NK_API int
  15541. nk_window_is_any_hovered(struct nk_context *ctx)
  15542. {
  15543. struct nk_window *iter;
  15544. NK_ASSERT(ctx);
  15545. if (!ctx) return 0;
  15546. iter = ctx->begin;
  15547. while (iter) {
  15548. /* check if window is being hovered */
  15549. if(!(iter->flags & NK_WINDOW_HIDDEN)) {
  15550. /* check if window popup is being hovered */
  15551. if (iter->popup.active && iter->popup.win && nk_input_is_mouse_hovering_rect(&ctx->input, iter->popup.win->bounds))
  15552. return 1;
  15553. if (iter->flags & NK_WINDOW_MINIMIZED) {
  15554. struct nk_rect header = iter->bounds;
  15555. header.h = ctx->style.font->height + 2 * ctx->style.window.header.padding.y;
  15556. if (nk_input_is_mouse_hovering_rect(&ctx->input, header))
  15557. return 1;
  15558. } else if (nk_input_is_mouse_hovering_rect(&ctx->input, iter->bounds)) {
  15559. return 1;
  15560. }
  15561. }
  15562. iter = iter->next;
  15563. }
  15564. return 0;
  15565. }
  15566. NK_API int
  15567. nk_item_is_any_active(struct nk_context *ctx)
  15568. {
  15569. int any_hovered = nk_window_is_any_hovered(ctx);
  15570. int any_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
  15571. return any_hovered || any_active;
  15572. }
  15573. NK_API int
  15574. nk_window_is_collapsed(struct nk_context *ctx, const char *name)
  15575. {
  15576. int title_len;
  15577. nk_hash title_hash;
  15578. struct nk_window *win;
  15579. NK_ASSERT(ctx);
  15580. if (!ctx) return 0;
  15581. title_len = (int)nk_strlen(name);
  15582. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15583. win = nk_find_window(ctx, title_hash, name);
  15584. if (!win) return 0;
  15585. return win->flags & NK_WINDOW_MINIMIZED;
  15586. }
  15587. NK_API int
  15588. nk_window_is_closed(struct nk_context *ctx, const char *name)
  15589. {
  15590. int title_len;
  15591. nk_hash title_hash;
  15592. struct nk_window *win;
  15593. NK_ASSERT(ctx);
  15594. if (!ctx) return 1;
  15595. title_len = (int)nk_strlen(name);
  15596. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15597. win = nk_find_window(ctx, title_hash, name);
  15598. if (!win) return 1;
  15599. return (win->flags & NK_WINDOW_CLOSED);
  15600. }
  15601. NK_API int
  15602. nk_window_is_hidden(struct nk_context *ctx, const char *name)
  15603. {
  15604. int title_len;
  15605. nk_hash title_hash;
  15606. struct nk_window *win;
  15607. NK_ASSERT(ctx);
  15608. if (!ctx) return 1;
  15609. title_len = (int)nk_strlen(name);
  15610. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15611. win = nk_find_window(ctx, title_hash, name);
  15612. if (!win) return 1;
  15613. return (win->flags & NK_WINDOW_HIDDEN);
  15614. }
  15615. NK_API int
  15616. nk_window_is_active(struct nk_context *ctx, const char *name)
  15617. {
  15618. int title_len;
  15619. nk_hash title_hash;
  15620. struct nk_window *win;
  15621. NK_ASSERT(ctx);
  15622. if (!ctx) return 0;
  15623. title_len = (int)nk_strlen(name);
  15624. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15625. win = nk_find_window(ctx, title_hash, name);
  15626. if (!win) return 0;
  15627. return win == ctx->active;
  15628. }
  15629. NK_API struct nk_window*
  15630. nk_window_find(struct nk_context *ctx, const char *name)
  15631. {
  15632. int title_len;
  15633. nk_hash title_hash;
  15634. title_len = (int)nk_strlen(name);
  15635. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15636. return nk_find_window(ctx, title_hash, name);
  15637. }
  15638. NK_API void
  15639. nk_window_close(struct nk_context *ctx, const char *name)
  15640. {
  15641. struct nk_window *win;
  15642. NK_ASSERT(ctx);
  15643. if (!ctx) return;
  15644. win = nk_window_find(ctx, name);
  15645. if (!win) return;
  15646. NK_ASSERT(ctx->current != win && "You cannot close a currently active window");
  15647. if (ctx->current == win) return;
  15648. win->flags |= NK_WINDOW_HIDDEN;
  15649. win->flags |= NK_WINDOW_CLOSED;
  15650. }
  15651. NK_API void
  15652. nk_window_set_bounds(struct nk_context *ctx,
  15653. const char *name, struct nk_rect bounds)
  15654. {
  15655. struct nk_window *win;
  15656. NK_ASSERT(ctx);
  15657. if (!ctx) return;
  15658. win = nk_window_find(ctx, name);
  15659. if (!win) return;
  15660. NK_ASSERT(ctx->current != win && "You cannot update a currently in procecss window");
  15661. win->bounds = bounds;
  15662. }
  15663. NK_API void
  15664. nk_window_set_position(struct nk_context *ctx,
  15665. const char *name, struct nk_vec2 pos)
  15666. {
  15667. struct nk_window *win = nk_window_find(ctx, name);
  15668. if (!win) return;
  15669. win->bounds.x = pos.x;
  15670. win->bounds.y = pos.y;
  15671. }
  15672. NK_API void
  15673. nk_window_set_size(struct nk_context *ctx,
  15674. const char *name, struct nk_vec2 size)
  15675. {
  15676. struct nk_window *win = nk_window_find(ctx, name);
  15677. if (!win) return;
  15678. win->bounds.w = size.x;
  15679. win->bounds.h = size.y;
  15680. }
  15681. NK_API void
  15682. nk_window_set_scroll(struct nk_context *ctx, nk_uint offset_x, nk_uint offset_y)
  15683. {
  15684. struct nk_window *win;
  15685. NK_ASSERT(ctx);
  15686. NK_ASSERT(ctx->current);
  15687. if (!ctx || !ctx->current)
  15688. return;
  15689. win = ctx->current;
  15690. win->scrollbar.x = offset_x;
  15691. win->scrollbar.y = offset_y;
  15692. }
  15693. NK_API void
  15694. nk_window_collapse(struct nk_context *ctx, const char *name,
  15695. enum nk_collapse_states c)
  15696. {
  15697. int title_len;
  15698. nk_hash title_hash;
  15699. struct nk_window *win;
  15700. NK_ASSERT(ctx);
  15701. if (!ctx) return;
  15702. title_len = (int)nk_strlen(name);
  15703. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15704. win = nk_find_window(ctx, title_hash, name);
  15705. if (!win) return;
  15706. if (c == NK_MINIMIZED)
  15707. win->flags |= NK_WINDOW_MINIMIZED;
  15708. else win->flags &= ~(nk_flags)NK_WINDOW_MINIMIZED;
  15709. }
  15710. NK_API void
  15711. nk_window_collapse_if(struct nk_context *ctx, const char *name,
  15712. enum nk_collapse_states c, int cond)
  15713. {
  15714. NK_ASSERT(ctx);
  15715. if (!ctx || !cond) return;
  15716. nk_window_collapse(ctx, name, c);
  15717. }
  15718. NK_API void
  15719. nk_window_show(struct nk_context *ctx, const char *name, enum nk_show_states s)
  15720. {
  15721. int title_len;
  15722. nk_hash title_hash;
  15723. struct nk_window *win;
  15724. NK_ASSERT(ctx);
  15725. if (!ctx) return;
  15726. title_len = (int)nk_strlen(name);
  15727. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15728. win = nk_find_window(ctx, title_hash, name);
  15729. if (!win) return;
  15730. if (s == NK_HIDDEN) {
  15731. win->flags |= NK_WINDOW_HIDDEN;
  15732. } else win->flags &= ~(nk_flags)NK_WINDOW_HIDDEN;
  15733. }
  15734. NK_API void
  15735. nk_window_show_if(struct nk_context *ctx, const char *name,
  15736. enum nk_show_states s, int cond)
  15737. {
  15738. NK_ASSERT(ctx);
  15739. if (!ctx || !cond) return;
  15740. nk_window_show(ctx, name, s);
  15741. }
  15742. NK_API void
  15743. nk_window_set_focus(struct nk_context *ctx, const char *name)
  15744. {
  15745. int title_len;
  15746. nk_hash title_hash;
  15747. struct nk_window *win;
  15748. NK_ASSERT(ctx);
  15749. if (!ctx) return;
  15750. title_len = (int)nk_strlen(name);
  15751. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  15752. win = nk_find_window(ctx, title_hash, name);
  15753. if (win && ctx->end != win) {
  15754. nk_remove_window(ctx, win);
  15755. nk_insert_window(ctx, win, NK_INSERT_BACK);
  15756. }
  15757. ctx->active = win;
  15758. }
  15759. /* ===============================================================
  15760. *
  15761. * POPUP
  15762. *
  15763. * ===============================================================*/
  15764. NK_API int
  15765. nk_popup_begin(struct nk_context *ctx, enum nk_popup_type type,
  15766. const char *title, nk_flags flags, struct nk_rect rect)
  15767. {
  15768. struct nk_window *popup;
  15769. struct nk_window *win;
  15770. struct nk_panel *panel;
  15771. int title_len;
  15772. nk_hash title_hash;
  15773. nk_size allocated;
  15774. NK_ASSERT(ctx);
  15775. NK_ASSERT(title);
  15776. NK_ASSERT(ctx->current);
  15777. NK_ASSERT(ctx->current->layout);
  15778. if (!ctx || !ctx->current || !ctx->current->layout)
  15779. return 0;
  15780. win = ctx->current;
  15781. panel = win->layout;
  15782. NK_ASSERT(!(panel->type & NK_PANEL_SET_POPUP) && "popups are not allowed to have popups");
  15783. (void)panel;
  15784. title_len = (int)nk_strlen(title);
  15785. title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_POPUP);
  15786. popup = win->popup.win;
  15787. if (!popup) {
  15788. popup = (struct nk_window*)nk_create_window(ctx);
  15789. popup->parent = win;
  15790. win->popup.win = popup;
  15791. win->popup.active = 0;
  15792. win->popup.type = NK_PANEL_POPUP;
  15793. }
  15794. /* make sure we have correct popup */
  15795. if (win->popup.name != title_hash) {
  15796. if (!win->popup.active) {
  15797. nk_zero(popup, sizeof(*popup));
  15798. win->popup.name = title_hash;
  15799. win->popup.active = 1;
  15800. win->popup.type = NK_PANEL_POPUP;
  15801. } else return 0;
  15802. }
  15803. /* popup position is local to window */
  15804. ctx->current = popup;
  15805. rect.x += win->layout->clip.x;
  15806. rect.y += win->layout->clip.y;
  15807. /* setup popup data */
  15808. popup->parent = win;
  15809. popup->bounds = rect;
  15810. popup->seq = ctx->seq;
  15811. popup->layout = (struct nk_panel*)nk_create_panel(ctx);
  15812. popup->flags = flags;
  15813. popup->flags |= NK_WINDOW_BORDER;
  15814. if (type == NK_POPUP_DYNAMIC)
  15815. popup->flags |= NK_WINDOW_DYNAMIC;
  15816. popup->buffer = win->buffer;
  15817. nk_start_popup(ctx, win);
  15818. allocated = ctx->memory.allocated;
  15819. nk_push_scissor(&popup->buffer, nk_null_rect);
  15820. if (nk_panel_begin(ctx, title, NK_PANEL_POPUP)) {
  15821. /* popup is running therefore invalidate parent panels */
  15822. struct nk_panel *root;
  15823. root = win->layout;
  15824. while (root) {
  15825. root->flags |= NK_WINDOW_ROM;
  15826. root->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
  15827. root = root->parent;
  15828. }
  15829. win->popup.active = 1;
  15830. popup->layout->offset_x = &popup->scrollbar.x;
  15831. popup->layout->offset_y = &popup->scrollbar.y;
  15832. popup->layout->parent = win->layout;
  15833. return 1;
  15834. } else {
  15835. /* popup was closed/is invalid so cleanup */
  15836. struct nk_panel *root;
  15837. root = win->layout;
  15838. while (root) {
  15839. root->flags |= NK_WINDOW_REMOVE_ROM;
  15840. root = root->parent;
  15841. }
  15842. win->popup.buf.active = 0;
  15843. win->popup.active = 0;
  15844. ctx->memory.allocated = allocated;
  15845. ctx->current = win;
  15846. nk_free_panel(ctx, popup->layout);
  15847. popup->layout = 0;
  15848. return 0;
  15849. }
  15850. }
  15851. NK_LIB int
  15852. nk_nonblock_begin(struct nk_context *ctx,
  15853. nk_flags flags, struct nk_rect body, struct nk_rect header,
  15854. enum nk_panel_type panel_type)
  15855. {
  15856. struct nk_window *popup;
  15857. struct nk_window *win;
  15858. struct nk_panel *panel;
  15859. int is_active = nk_true;
  15860. NK_ASSERT(ctx);
  15861. NK_ASSERT(ctx->current);
  15862. NK_ASSERT(ctx->current->layout);
  15863. if (!ctx || !ctx->current || !ctx->current->layout)
  15864. return 0;
  15865. /* popups cannot have popups */
  15866. win = ctx->current;
  15867. panel = win->layout;
  15868. NK_ASSERT(!(panel->type & NK_PANEL_SET_POPUP));
  15869. (void)panel;
  15870. popup = win->popup.win;
  15871. if (!popup) {
  15872. /* create window for nonblocking popup */
  15873. popup = (struct nk_window*)nk_create_window(ctx);
  15874. popup->parent = win;
  15875. win->popup.win = popup;
  15876. win->popup.type = panel_type;
  15877. nk_command_buffer_init(&popup->buffer, &ctx->memory, NK_CLIPPING_ON);
  15878. } else {
  15879. /* close the popup if user pressed outside or in the header */
  15880. int pressed, in_body, in_header;
  15881. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  15882. pressed = nk_input_is_mouse_released(&ctx->input, NK_BUTTON_LEFT);
  15883. #else
  15884. pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
  15885. #endif
  15886. in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
  15887. in_header = nk_input_is_mouse_hovering_rect(&ctx->input, header);
  15888. if (pressed && (!in_body || in_header))
  15889. is_active = nk_false;
  15890. }
  15891. win->popup.header = header;
  15892. if (!is_active) {
  15893. /* remove read only mode from all parent panels */
  15894. struct nk_panel *root = win->layout;
  15895. while (root) {
  15896. root->flags |= NK_WINDOW_REMOVE_ROM;
  15897. root = root->parent;
  15898. }
  15899. return is_active;
  15900. }
  15901. popup->bounds = body;
  15902. popup->parent = win;
  15903. popup->layout = (struct nk_panel*)nk_create_panel(ctx);
  15904. popup->flags = flags;
  15905. popup->flags |= NK_WINDOW_BORDER;
  15906. popup->flags |= NK_WINDOW_DYNAMIC;
  15907. popup->seq = ctx->seq;
  15908. win->popup.active = 1;
  15909. NK_ASSERT(popup->layout);
  15910. nk_start_popup(ctx, win);
  15911. popup->buffer = win->buffer;
  15912. nk_push_scissor(&popup->buffer, nk_null_rect);
  15913. ctx->current = popup;
  15914. nk_panel_begin(ctx, 0, panel_type);
  15915. win->buffer = popup->buffer;
  15916. popup->layout->parent = win->layout;
  15917. popup->layout->offset_x = &popup->scrollbar.x;
  15918. popup->layout->offset_y = &popup->scrollbar.y;
  15919. /* set read only mode to all parent panels */
  15920. {struct nk_panel *root;
  15921. root = win->layout;
  15922. while (root) {
  15923. root->flags |= NK_WINDOW_ROM;
  15924. root = root->parent;
  15925. }}
  15926. return is_active;
  15927. }
  15928. NK_API void
  15929. nk_popup_close(struct nk_context *ctx)
  15930. {
  15931. struct nk_window *popup;
  15932. NK_ASSERT(ctx);
  15933. if (!ctx || !ctx->current) return;
  15934. popup = ctx->current;
  15935. NK_ASSERT(popup->parent);
  15936. NK_ASSERT(popup->layout->type & NK_PANEL_SET_POPUP);
  15937. popup->flags |= NK_WINDOW_HIDDEN;
  15938. }
  15939. NK_API void
  15940. nk_popup_end(struct nk_context *ctx)
  15941. {
  15942. struct nk_window *win;
  15943. struct nk_window *popup;
  15944. NK_ASSERT(ctx);
  15945. NK_ASSERT(ctx->current);
  15946. NK_ASSERT(ctx->current->layout);
  15947. if (!ctx || !ctx->current || !ctx->current->layout)
  15948. return;
  15949. popup = ctx->current;
  15950. if (!popup->parent) return;
  15951. win = popup->parent;
  15952. if (popup->flags & NK_WINDOW_HIDDEN) {
  15953. struct nk_panel *root;
  15954. root = win->layout;
  15955. while (root) {
  15956. root->flags |= NK_WINDOW_REMOVE_ROM;
  15957. root = root->parent;
  15958. }
  15959. win->popup.active = 0;
  15960. }
  15961. nk_push_scissor(&popup->buffer, nk_null_rect);
  15962. nk_end(ctx);
  15963. win->buffer = popup->buffer;
  15964. nk_finish_popup(ctx, win);
  15965. ctx->current = win;
  15966. nk_push_scissor(&win->buffer, win->layout->clip);
  15967. }
  15968. NK_API void
  15969. nk_popup_get_scroll(struct nk_context *ctx, nk_uint *offset_x, nk_uint *offset_y)
  15970. {
  15971. struct nk_window *popup;
  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;
  15977. popup = ctx->current;
  15978. if (offset_x)
  15979. *offset_x = popup->scrollbar.x;
  15980. if (offset_y)
  15981. *offset_y = popup->scrollbar.y;
  15982. }
  15983. NK_API void
  15984. nk_popup_set_scroll(struct nk_context *ctx, nk_uint offset_x, nk_uint offset_y)
  15985. {
  15986. struct nk_window *popup;
  15987. NK_ASSERT(ctx);
  15988. NK_ASSERT(ctx->current);
  15989. NK_ASSERT(ctx->current->layout);
  15990. if (!ctx || !ctx->current || !ctx->current->layout)
  15991. return;
  15992. popup = ctx->current;
  15993. popup->scrollbar.x = offset_x;
  15994. popup->scrollbar.y = offset_y;
  15995. }
  15996. /* ==============================================================
  15997. *
  15998. * CONTEXTUAL
  15999. *
  16000. * ===============================================================*/
  16001. NK_API int
  16002. nk_contextual_begin(struct nk_context *ctx, nk_flags flags, struct nk_vec2 size,
  16003. struct nk_rect trigger_bounds)
  16004. {
  16005. struct nk_window *win;
  16006. struct nk_window *popup;
  16007. struct nk_rect body;
  16008. NK_STORAGE const struct nk_rect null_rect = {-1,-1,0,0};
  16009. int is_clicked = 0;
  16010. int is_open = 0;
  16011. int ret = 0;
  16012. NK_ASSERT(ctx);
  16013. NK_ASSERT(ctx->current);
  16014. NK_ASSERT(ctx->current->layout);
  16015. if (!ctx || !ctx->current || !ctx->current->layout)
  16016. return 0;
  16017. win = ctx->current;
  16018. ++win->popup.con_count;
  16019. if (ctx->current != ctx->active)
  16020. return 0;
  16021. /* check if currently active contextual is active */
  16022. popup = win->popup.win;
  16023. is_open = (popup && win->popup.type == NK_PANEL_CONTEXTUAL);
  16024. is_clicked = nk_input_mouse_clicked(&ctx->input, NK_BUTTON_RIGHT, trigger_bounds);
  16025. if (win->popup.active_con && win->popup.con_count != win->popup.active_con)
  16026. return 0;
  16027. if (!is_open && win->popup.active_con)
  16028. win->popup.active_con = 0;
  16029. if ((!is_open && !is_clicked))
  16030. return 0;
  16031. /* calculate contextual position on click */
  16032. win->popup.active_con = win->popup.con_count;
  16033. if (is_clicked) {
  16034. body.x = ctx->input.mouse.pos.x;
  16035. body.y = ctx->input.mouse.pos.y;
  16036. } else {
  16037. body.x = popup->bounds.x;
  16038. body.y = popup->bounds.y;
  16039. }
  16040. body.w = size.x;
  16041. body.h = size.y;
  16042. /* start nonblocking contextual popup */
  16043. ret = nk_nonblock_begin(ctx, flags|NK_WINDOW_NO_SCROLLBAR, body,
  16044. null_rect, NK_PANEL_CONTEXTUAL);
  16045. if (ret) win->popup.type = NK_PANEL_CONTEXTUAL;
  16046. else {
  16047. win->popup.active_con = 0;
  16048. win->popup.type = NK_PANEL_NONE;
  16049. if (win->popup.win)
  16050. win->popup.win->flags = 0;
  16051. }
  16052. return ret;
  16053. }
  16054. NK_API int
  16055. nk_contextual_item_text(struct nk_context *ctx, const char *text, int len,
  16056. nk_flags alignment)
  16057. {
  16058. struct nk_window *win;
  16059. const struct nk_input *in;
  16060. const struct nk_style *style;
  16061. struct nk_rect bounds;
  16062. enum nk_widget_layout_states state;
  16063. NK_ASSERT(ctx);
  16064. NK_ASSERT(ctx->current);
  16065. NK_ASSERT(ctx->current->layout);
  16066. if (!ctx || !ctx->current || !ctx->current->layout)
  16067. return 0;
  16068. win = ctx->current;
  16069. style = &ctx->style;
  16070. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  16071. if (!state) return nk_false;
  16072. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  16073. if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
  16074. text, len, alignment, NK_BUTTON_DEFAULT, &style->contextual_button, in, style->font)) {
  16075. nk_contextual_close(ctx);
  16076. return nk_true;
  16077. }
  16078. return nk_false;
  16079. }
  16080. NK_API int
  16081. nk_contextual_item_label(struct nk_context *ctx, const char *label, nk_flags align)
  16082. {
  16083. return nk_contextual_item_text(ctx, label, nk_strlen(label), align);
  16084. }
  16085. NK_API int
  16086. nk_contextual_item_image_text(struct nk_context *ctx, struct nk_image img,
  16087. const char *text, int len, nk_flags align)
  16088. {
  16089. struct nk_window *win;
  16090. const struct nk_input *in;
  16091. const struct nk_style *style;
  16092. struct nk_rect bounds;
  16093. enum nk_widget_layout_states state;
  16094. NK_ASSERT(ctx);
  16095. NK_ASSERT(ctx->current);
  16096. NK_ASSERT(ctx->current->layout);
  16097. if (!ctx || !ctx->current || !ctx->current->layout)
  16098. return 0;
  16099. win = ctx->current;
  16100. style = &ctx->style;
  16101. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  16102. if (!state) return nk_false;
  16103. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  16104. if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer, bounds,
  16105. img, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)){
  16106. nk_contextual_close(ctx);
  16107. return nk_true;
  16108. }
  16109. return nk_false;
  16110. }
  16111. NK_API int
  16112. nk_contextual_item_image_label(struct nk_context *ctx, struct nk_image img,
  16113. const char *label, nk_flags align)
  16114. {
  16115. return nk_contextual_item_image_text(ctx, img, label, nk_strlen(label), align);
  16116. }
  16117. NK_API int
  16118. nk_contextual_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type symbol,
  16119. const char *text, int len, nk_flags align)
  16120. {
  16121. struct nk_window *win;
  16122. const struct nk_input *in;
  16123. const struct nk_style *style;
  16124. struct nk_rect bounds;
  16125. enum nk_widget_layout_states state;
  16126. NK_ASSERT(ctx);
  16127. NK_ASSERT(ctx->current);
  16128. NK_ASSERT(ctx->current->layout);
  16129. if (!ctx || !ctx->current || !ctx->current->layout)
  16130. return 0;
  16131. win = ctx->current;
  16132. style = &ctx->style;
  16133. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  16134. if (!state) return nk_false;
  16135. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  16136. if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  16137. symbol, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)) {
  16138. nk_contextual_close(ctx);
  16139. return nk_true;
  16140. }
  16141. return nk_false;
  16142. }
  16143. NK_API int
  16144. nk_contextual_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type symbol,
  16145. const char *text, nk_flags align)
  16146. {
  16147. return nk_contextual_item_symbol_text(ctx, symbol, text, nk_strlen(text), align);
  16148. }
  16149. NK_API void
  16150. nk_contextual_close(struct nk_context *ctx)
  16151. {
  16152. NK_ASSERT(ctx);
  16153. NK_ASSERT(ctx->current);
  16154. NK_ASSERT(ctx->current->layout);
  16155. if (!ctx || !ctx->current || !ctx->current->layout) return;
  16156. nk_popup_close(ctx);
  16157. }
  16158. NK_API void
  16159. nk_contextual_end(struct nk_context *ctx)
  16160. {
  16161. struct nk_window *popup;
  16162. struct nk_panel *panel;
  16163. NK_ASSERT(ctx);
  16164. NK_ASSERT(ctx->current);
  16165. if (!ctx || !ctx->current) return;
  16166. popup = ctx->current;
  16167. panel = popup->layout;
  16168. NK_ASSERT(popup->parent);
  16169. NK_ASSERT(panel->type & NK_PANEL_SET_POPUP);
  16170. if (panel->flags & NK_WINDOW_DYNAMIC) {
  16171. /* Close behavior
  16172. This is a bit of a hack solution since we do not know before we end our popup
  16173. how big it will be. We therefore do not directly know when a
  16174. click outside the non-blocking popup must close it at that direct frame.
  16175. Instead it will be closed in the next frame.*/
  16176. struct nk_rect body = {0,0,0,0};
  16177. if (panel->at_y < (panel->bounds.y + panel->bounds.h)) {
  16178. struct nk_vec2 padding = nk_panel_get_padding(&ctx->style, panel->type);
  16179. body = panel->bounds;
  16180. body.y = (panel->at_y + panel->footer_height + panel->border + padding.y + panel->row.height);
  16181. body.h = (panel->bounds.y + panel->bounds.h) - body.y;
  16182. }
  16183. {int pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
  16184. int in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
  16185. if (pressed && in_body)
  16186. popup->flags |= NK_WINDOW_HIDDEN;
  16187. }
  16188. }
  16189. if (popup->flags & NK_WINDOW_HIDDEN)
  16190. popup->seq = 0;
  16191. nk_popup_end(ctx);
  16192. return;
  16193. }
  16194. /* ===============================================================
  16195. *
  16196. * MENU
  16197. *
  16198. * ===============================================================*/
  16199. NK_API void
  16200. nk_menubar_begin(struct nk_context *ctx)
  16201. {
  16202. struct nk_panel *layout;
  16203. NK_ASSERT(ctx);
  16204. NK_ASSERT(ctx->current);
  16205. NK_ASSERT(ctx->current->layout);
  16206. if (!ctx || !ctx->current || !ctx->current->layout)
  16207. return;
  16208. layout = ctx->current->layout;
  16209. NK_ASSERT(layout->at_y == layout->bounds.y);
  16210. /* if this assert triggers you allocated space between nk_begin and nk_menubar_begin.
  16211. If you want a menubar the first nuklear function after `nk_begin` has to be a
  16212. `nk_menubar_begin` call. Inside the menubar you then have to allocate space for
  16213. widgets (also supports multiple rows).
  16214. Example:
  16215. if (nk_begin(...)) {
  16216. nk_menubar_begin(...);
  16217. nk_layout_xxxx(...);
  16218. nk_button(...);
  16219. nk_layout_xxxx(...);
  16220. nk_button(...);
  16221. nk_menubar_end(...);
  16222. }
  16223. nk_end(...);
  16224. */
  16225. if (layout->flags & NK_WINDOW_HIDDEN || layout->flags & NK_WINDOW_MINIMIZED)
  16226. return;
  16227. layout->menu.x = layout->at_x;
  16228. layout->menu.y = layout->at_y + layout->row.height;
  16229. layout->menu.w = layout->bounds.w;
  16230. layout->menu.offset.x = *layout->offset_x;
  16231. layout->menu.offset.y = *layout->offset_y;
  16232. *layout->offset_y = 0;
  16233. }
  16234. NK_API void
  16235. nk_menubar_end(struct nk_context *ctx)
  16236. {
  16237. struct nk_window *win;
  16238. struct nk_panel *layout;
  16239. struct nk_command_buffer *out;
  16240. NK_ASSERT(ctx);
  16241. NK_ASSERT(ctx->current);
  16242. NK_ASSERT(ctx->current->layout);
  16243. if (!ctx || !ctx->current || !ctx->current->layout)
  16244. return;
  16245. win = ctx->current;
  16246. out = &win->buffer;
  16247. layout = win->layout;
  16248. if (layout->flags & NK_WINDOW_HIDDEN || layout->flags & NK_WINDOW_MINIMIZED)
  16249. return;
  16250. layout->menu.h = layout->at_y - layout->menu.y;
  16251. layout->bounds.y += layout->menu.h + ctx->style.window.spacing.y + layout->row.height;
  16252. layout->bounds.h -= layout->menu.h + ctx->style.window.spacing.y + layout->row.height;
  16253. *layout->offset_x = layout->menu.offset.x;
  16254. *layout->offset_y = layout->menu.offset.y;
  16255. layout->at_y = layout->bounds.y - layout->row.height;
  16256. layout->clip.y = layout->bounds.y;
  16257. layout->clip.h = layout->bounds.h;
  16258. nk_push_scissor(out, layout->clip);
  16259. }
  16260. NK_INTERN int
  16261. nk_menu_begin(struct nk_context *ctx, struct nk_window *win,
  16262. const char *id, int is_clicked, struct nk_rect header, struct nk_vec2 size)
  16263. {
  16264. int is_open = 0;
  16265. int is_active = 0;
  16266. struct nk_rect body;
  16267. struct nk_window *popup;
  16268. nk_hash hash = nk_murmur_hash(id, (int)nk_strlen(id), NK_PANEL_MENU);
  16269. NK_ASSERT(ctx);
  16270. NK_ASSERT(ctx->current);
  16271. NK_ASSERT(ctx->current->layout);
  16272. if (!ctx || !ctx->current || !ctx->current->layout)
  16273. return 0;
  16274. body.x = header.x;
  16275. body.w = size.x;
  16276. body.y = header.y + header.h;
  16277. body.h = size.y;
  16278. popup = win->popup.win;
  16279. is_open = popup ? nk_true : nk_false;
  16280. is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_MENU);
  16281. if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
  16282. (!is_open && !is_active && !is_clicked)) return 0;
  16283. if (!nk_nonblock_begin(ctx, NK_WINDOW_NO_SCROLLBAR, body, header, NK_PANEL_MENU))
  16284. return 0;
  16285. win->popup.type = NK_PANEL_MENU;
  16286. win->popup.name = hash;
  16287. return 1;
  16288. }
  16289. NK_API int
  16290. nk_menu_begin_text(struct nk_context *ctx, const char *title, int len,
  16291. nk_flags align, struct nk_vec2 size)
  16292. {
  16293. struct nk_window *win;
  16294. const struct nk_input *in;
  16295. struct nk_rect header;
  16296. int is_clicked = nk_false;
  16297. nk_flags state;
  16298. NK_ASSERT(ctx);
  16299. NK_ASSERT(ctx->current);
  16300. NK_ASSERT(ctx->current->layout);
  16301. if (!ctx || !ctx->current || !ctx->current->layout)
  16302. return 0;
  16303. win = ctx->current;
  16304. state = nk_widget(&header, ctx);
  16305. if (!state) return 0;
  16306. in = (state == NK_WIDGET_ROM || win->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  16307. if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, header,
  16308. title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
  16309. is_clicked = nk_true;
  16310. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  16311. }
  16312. NK_API int nk_menu_begin_label(struct nk_context *ctx,
  16313. const char *text, nk_flags align, struct nk_vec2 size)
  16314. {
  16315. return nk_menu_begin_text(ctx, text, nk_strlen(text), align, size);
  16316. }
  16317. NK_API int
  16318. nk_menu_begin_image(struct nk_context *ctx, const char *id, struct nk_image img,
  16319. struct nk_vec2 size)
  16320. {
  16321. struct nk_window *win;
  16322. struct nk_rect header;
  16323. const struct nk_input *in;
  16324. int is_clicked = nk_false;
  16325. nk_flags state;
  16326. NK_ASSERT(ctx);
  16327. NK_ASSERT(ctx->current);
  16328. NK_ASSERT(ctx->current->layout);
  16329. if (!ctx || !ctx->current || !ctx->current->layout)
  16330. return 0;
  16331. win = ctx->current;
  16332. state = nk_widget(&header, ctx);
  16333. if (!state) return 0;
  16334. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  16335. if (nk_do_button_image(&ctx->last_widget_state, &win->buffer, header,
  16336. img, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in))
  16337. is_clicked = nk_true;
  16338. return nk_menu_begin(ctx, win, id, is_clicked, header, size);
  16339. }
  16340. NK_API int
  16341. nk_menu_begin_symbol(struct nk_context *ctx, const char *id,
  16342. enum nk_symbol_type sym, struct nk_vec2 size)
  16343. {
  16344. struct nk_window *win;
  16345. const struct nk_input *in;
  16346. struct nk_rect header;
  16347. int is_clicked = nk_false;
  16348. nk_flags state;
  16349. NK_ASSERT(ctx);
  16350. NK_ASSERT(ctx->current);
  16351. NK_ASSERT(ctx->current->layout);
  16352. if (!ctx || !ctx->current || !ctx->current->layout)
  16353. return 0;
  16354. win = ctx->current;
  16355. state = nk_widget(&header, ctx);
  16356. if (!state) return 0;
  16357. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  16358. if (nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, header,
  16359. sym, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
  16360. is_clicked = nk_true;
  16361. return nk_menu_begin(ctx, win, id, is_clicked, header, size);
  16362. }
  16363. NK_API int
  16364. nk_menu_begin_image_text(struct nk_context *ctx, const char *title, int len,
  16365. nk_flags align, struct nk_image img, struct nk_vec2 size)
  16366. {
  16367. struct nk_window *win;
  16368. struct nk_rect header;
  16369. const struct nk_input *in;
  16370. int is_clicked = nk_false;
  16371. nk_flags state;
  16372. NK_ASSERT(ctx);
  16373. NK_ASSERT(ctx->current);
  16374. NK_ASSERT(ctx->current->layout);
  16375. if (!ctx || !ctx->current || !ctx->current->layout)
  16376. return 0;
  16377. win = ctx->current;
  16378. state = nk_widget(&header, ctx);
  16379. if (!state) return 0;
  16380. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  16381. if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
  16382. header, img, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
  16383. ctx->style.font, in))
  16384. is_clicked = nk_true;
  16385. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  16386. }
  16387. NK_API int
  16388. nk_menu_begin_image_label(struct nk_context *ctx,
  16389. const char *title, nk_flags align, struct nk_image img, struct nk_vec2 size)
  16390. {
  16391. return nk_menu_begin_image_text(ctx, title, nk_strlen(title), align, img, size);
  16392. }
  16393. NK_API int
  16394. nk_menu_begin_symbol_text(struct nk_context *ctx, const char *title, int len,
  16395. nk_flags align, enum nk_symbol_type sym, struct nk_vec2 size)
  16396. {
  16397. struct nk_window *win;
  16398. struct nk_rect header;
  16399. const struct nk_input *in;
  16400. int is_clicked = nk_false;
  16401. nk_flags state;
  16402. NK_ASSERT(ctx);
  16403. NK_ASSERT(ctx->current);
  16404. NK_ASSERT(ctx->current->layout);
  16405. if (!ctx || !ctx->current || !ctx->current->layout)
  16406. return 0;
  16407. win = ctx->current;
  16408. state = nk_widget(&header, ctx);
  16409. if (!state) return 0;
  16410. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  16411. if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer,
  16412. header, sym, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
  16413. ctx->style.font, in)) is_clicked = nk_true;
  16414. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  16415. }
  16416. NK_API int
  16417. nk_menu_begin_symbol_label(struct nk_context *ctx,
  16418. const char *title, nk_flags align, enum nk_symbol_type sym, struct nk_vec2 size )
  16419. {
  16420. return nk_menu_begin_symbol_text(ctx, title, nk_strlen(title), align,sym,size);
  16421. }
  16422. NK_API int
  16423. nk_menu_item_text(struct nk_context *ctx, const char *title, int len, nk_flags align)
  16424. {
  16425. return nk_contextual_item_text(ctx, title, len, align);
  16426. }
  16427. NK_API int
  16428. nk_menu_item_label(struct nk_context *ctx, const char *label, nk_flags align)
  16429. {
  16430. return nk_contextual_item_label(ctx, label, align);
  16431. }
  16432. NK_API int
  16433. nk_menu_item_image_label(struct nk_context *ctx, struct nk_image img,
  16434. const char *label, nk_flags align)
  16435. {
  16436. return nk_contextual_item_image_label(ctx, img, label, align);
  16437. }
  16438. NK_API int
  16439. nk_menu_item_image_text(struct nk_context *ctx, struct nk_image img,
  16440. const char *text, int len, nk_flags align)
  16441. {
  16442. return nk_contextual_item_image_text(ctx, img, text, len, align);
  16443. }
  16444. NK_API int nk_menu_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  16445. const char *text, int len, nk_flags align)
  16446. {
  16447. return nk_contextual_item_symbol_text(ctx, sym, text, len, align);
  16448. }
  16449. NK_API int nk_menu_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  16450. const char *label, nk_flags align)
  16451. {
  16452. return nk_contextual_item_symbol_label(ctx, sym, label, align);
  16453. }
  16454. NK_API void nk_menu_close(struct nk_context *ctx)
  16455. {
  16456. nk_contextual_close(ctx);
  16457. }
  16458. NK_API void
  16459. nk_menu_end(struct nk_context *ctx)
  16460. {
  16461. nk_contextual_end(ctx);
  16462. }
  16463. /* ===============================================================
  16464. *
  16465. * LAYOUT
  16466. *
  16467. * ===============================================================*/
  16468. NK_API void
  16469. nk_layout_set_min_row_height(struct nk_context *ctx, float height)
  16470. {
  16471. struct nk_window *win;
  16472. struct nk_panel *layout;
  16473. NK_ASSERT(ctx);
  16474. NK_ASSERT(ctx->current);
  16475. NK_ASSERT(ctx->current->layout);
  16476. if (!ctx || !ctx->current || !ctx->current->layout)
  16477. return;
  16478. win = ctx->current;
  16479. layout = win->layout;
  16480. layout->row.min_height = height;
  16481. }
  16482. NK_API void
  16483. nk_layout_reset_min_row_height(struct nk_context *ctx)
  16484. {
  16485. struct nk_window *win;
  16486. struct nk_panel *layout;
  16487. NK_ASSERT(ctx);
  16488. NK_ASSERT(ctx->current);
  16489. NK_ASSERT(ctx->current->layout);
  16490. if (!ctx || !ctx->current || !ctx->current->layout)
  16491. return;
  16492. win = ctx->current;
  16493. layout = win->layout;
  16494. layout->row.min_height = ctx->style.font->height;
  16495. layout->row.min_height += ctx->style.text.padding.y*2;
  16496. layout->row.min_height += ctx->style.window.min_row_height_padding*2;
  16497. }
  16498. NK_LIB float
  16499. nk_layout_row_calculate_usable_space(const struct nk_style *style, enum nk_panel_type type,
  16500. float total_space, int columns)
  16501. {
  16502. float panel_padding;
  16503. float panel_spacing;
  16504. float panel_space;
  16505. struct nk_vec2 spacing;
  16506. struct nk_vec2 padding;
  16507. spacing = style->window.spacing;
  16508. padding = nk_panel_get_padding(style, type);
  16509. /* calculate the usable panel space */
  16510. panel_padding = 2 * padding.x;
  16511. panel_spacing = (float)NK_MAX(columns - 1, 0) * spacing.x;
  16512. panel_space = total_space - panel_padding - panel_spacing;
  16513. return panel_space;
  16514. }
  16515. NK_LIB void
  16516. nk_panel_layout(const struct nk_context *ctx, struct nk_window *win,
  16517. float height, int cols)
  16518. {
  16519. struct nk_panel *layout;
  16520. const struct nk_style *style;
  16521. struct nk_command_buffer *out;
  16522. struct nk_vec2 item_spacing;
  16523. struct nk_color color;
  16524. NK_ASSERT(ctx);
  16525. NK_ASSERT(ctx->current);
  16526. NK_ASSERT(ctx->current->layout);
  16527. if (!ctx || !ctx->current || !ctx->current->layout)
  16528. return;
  16529. /* prefetch some configuration data */
  16530. layout = win->layout;
  16531. style = &ctx->style;
  16532. out = &win->buffer;
  16533. color = style->window.background;
  16534. item_spacing = style->window.spacing;
  16535. /* if one of these triggers you forgot to add an `if` condition around either
  16536. a window, group, popup, combobox or contextual menu `begin` and `end` block.
  16537. Example:
  16538. if (nk_begin(...) {...} nk_end(...); or
  16539. if (nk_group_begin(...) { nk_group_end(...);} */
  16540. NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
  16541. NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
  16542. NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
  16543. /* update the current row and set the current row layout */
  16544. layout->row.index = 0;
  16545. layout->at_y += layout->row.height;
  16546. layout->row.columns = cols;
  16547. if (height == 0.0f)
  16548. layout->row.height = NK_MAX(height, layout->row.min_height) + item_spacing.y;
  16549. else layout->row.height = height + item_spacing.y;
  16550. layout->row.item_offset = 0;
  16551. if (layout->flags & NK_WINDOW_DYNAMIC) {
  16552. /* draw background for dynamic panels */
  16553. struct nk_rect background;
  16554. background.x = win->bounds.x;
  16555. background.w = win->bounds.w;
  16556. background.y = layout->at_y - 1.0f;
  16557. background.h = layout->row.height + 1.0f;
  16558. nk_fill_rect(out, background, 0, color);
  16559. }
  16560. }
  16561. NK_LIB void
  16562. nk_row_layout(struct nk_context *ctx, enum nk_layout_format fmt,
  16563. float height, int cols, int width)
  16564. {
  16565. /* update the current row and set the current row layout */
  16566. struct nk_window *win;
  16567. NK_ASSERT(ctx);
  16568. NK_ASSERT(ctx->current);
  16569. NK_ASSERT(ctx->current->layout);
  16570. if (!ctx || !ctx->current || !ctx->current->layout)
  16571. return;
  16572. win = ctx->current;
  16573. nk_panel_layout(ctx, win, height, cols);
  16574. if (fmt == NK_DYNAMIC)
  16575. win->layout->row.type = NK_LAYOUT_DYNAMIC_FIXED;
  16576. else win->layout->row.type = NK_LAYOUT_STATIC_FIXED;
  16577. win->layout->row.ratio = 0;
  16578. win->layout->row.filled = 0;
  16579. win->layout->row.item_offset = 0;
  16580. win->layout->row.item_width = (float)width;
  16581. }
  16582. NK_API float
  16583. nk_layout_ratio_from_pixel(struct nk_context *ctx, float pixel_width)
  16584. {
  16585. struct nk_window *win;
  16586. NK_ASSERT(ctx);
  16587. NK_ASSERT(pixel_width);
  16588. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  16589. win = ctx->current;
  16590. return NK_CLAMP(0.0f, pixel_width/win->bounds.x, 1.0f);
  16591. }
  16592. NK_API void
  16593. nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols)
  16594. {
  16595. nk_row_layout(ctx, NK_DYNAMIC, height, cols, 0);
  16596. }
  16597. NK_API void
  16598. nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols)
  16599. {
  16600. nk_row_layout(ctx, NK_STATIC, height, cols, item_width);
  16601. }
  16602. NK_API void
  16603. nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt,
  16604. float row_height, int cols)
  16605. {
  16606. struct nk_window *win;
  16607. struct nk_panel *layout;
  16608. NK_ASSERT(ctx);
  16609. NK_ASSERT(ctx->current);
  16610. NK_ASSERT(ctx->current->layout);
  16611. if (!ctx || !ctx->current || !ctx->current->layout)
  16612. return;
  16613. win = ctx->current;
  16614. layout = win->layout;
  16615. nk_panel_layout(ctx, win, row_height, cols);
  16616. if (fmt == NK_DYNAMIC)
  16617. layout->row.type = NK_LAYOUT_DYNAMIC_ROW;
  16618. else layout->row.type = NK_LAYOUT_STATIC_ROW;
  16619. layout->row.ratio = 0;
  16620. layout->row.filled = 0;
  16621. layout->row.item_width = 0;
  16622. layout->row.item_offset = 0;
  16623. layout->row.columns = cols;
  16624. }
  16625. NK_API void
  16626. nk_layout_row_push(struct nk_context *ctx, float ratio_or_width)
  16627. {
  16628. struct nk_window *win;
  16629. struct nk_panel *layout;
  16630. NK_ASSERT(ctx);
  16631. NK_ASSERT(ctx->current);
  16632. NK_ASSERT(ctx->current->layout);
  16633. if (!ctx || !ctx->current || !ctx->current->layout)
  16634. return;
  16635. win = ctx->current;
  16636. layout = win->layout;
  16637. NK_ASSERT(layout->row.type == NK_LAYOUT_STATIC_ROW || layout->row.type == NK_LAYOUT_DYNAMIC_ROW);
  16638. if (layout->row.type != NK_LAYOUT_STATIC_ROW && layout->row.type != NK_LAYOUT_DYNAMIC_ROW)
  16639. return;
  16640. if (layout->row.type == NK_LAYOUT_DYNAMIC_ROW) {
  16641. float ratio = ratio_or_width;
  16642. if ((ratio + layout->row.filled) > 1.0f) return;
  16643. if (ratio > 0.0f)
  16644. layout->row.item_width = NK_SATURATE(ratio);
  16645. else layout->row.item_width = 1.0f - layout->row.filled;
  16646. } else layout->row.item_width = ratio_or_width;
  16647. }
  16648. NK_API void
  16649. nk_layout_row_end(struct nk_context *ctx)
  16650. {
  16651. struct nk_window *win;
  16652. struct nk_panel *layout;
  16653. NK_ASSERT(ctx);
  16654. NK_ASSERT(ctx->current);
  16655. NK_ASSERT(ctx->current->layout);
  16656. if (!ctx || !ctx->current || !ctx->current->layout)
  16657. return;
  16658. win = ctx->current;
  16659. layout = win->layout;
  16660. NK_ASSERT(layout->row.type == NK_LAYOUT_STATIC_ROW || layout->row.type == NK_LAYOUT_DYNAMIC_ROW);
  16661. if (layout->row.type != NK_LAYOUT_STATIC_ROW && layout->row.type != NK_LAYOUT_DYNAMIC_ROW)
  16662. return;
  16663. layout->row.item_width = 0;
  16664. layout->row.item_offset = 0;
  16665. }
  16666. NK_API void
  16667. nk_layout_row(struct nk_context *ctx, enum nk_layout_format fmt,
  16668. float height, int cols, const float *ratio)
  16669. {
  16670. int i;
  16671. int n_undef = 0;
  16672. struct nk_window *win;
  16673. struct nk_panel *layout;
  16674. NK_ASSERT(ctx);
  16675. NK_ASSERT(ctx->current);
  16676. NK_ASSERT(ctx->current->layout);
  16677. if (!ctx || !ctx->current || !ctx->current->layout)
  16678. return;
  16679. win = ctx->current;
  16680. layout = win->layout;
  16681. nk_panel_layout(ctx, win, height, cols);
  16682. if (fmt == NK_DYNAMIC) {
  16683. /* calculate width of undefined widget ratios */
  16684. float r = 0;
  16685. layout->row.ratio = ratio;
  16686. for (i = 0; i < cols; ++i) {
  16687. if (ratio[i] < 0.0f)
  16688. n_undef++;
  16689. else r += ratio[i];
  16690. }
  16691. r = NK_SATURATE(1.0f - r);
  16692. layout->row.type = NK_LAYOUT_DYNAMIC;
  16693. layout->row.item_width = (r > 0 && n_undef > 0) ? (r / (float)n_undef):0;
  16694. } else {
  16695. layout->row.ratio = ratio;
  16696. layout->row.type = NK_LAYOUT_STATIC;
  16697. layout->row.item_width = 0;
  16698. layout->row.item_offset = 0;
  16699. }
  16700. layout->row.item_offset = 0;
  16701. layout->row.filled = 0;
  16702. }
  16703. NK_API void
  16704. nk_layout_row_template_begin(struct nk_context *ctx, float height)
  16705. {
  16706. struct nk_window *win;
  16707. struct nk_panel *layout;
  16708. NK_ASSERT(ctx);
  16709. NK_ASSERT(ctx->current);
  16710. NK_ASSERT(ctx->current->layout);
  16711. if (!ctx || !ctx->current || !ctx->current->layout)
  16712. return;
  16713. win = ctx->current;
  16714. layout = win->layout;
  16715. nk_panel_layout(ctx, win, height, 1);
  16716. layout->row.type = NK_LAYOUT_TEMPLATE;
  16717. layout->row.columns = 0;
  16718. layout->row.ratio = 0;
  16719. layout->row.item_width = 0;
  16720. layout->row.item_height = 0;
  16721. layout->row.item_offset = 0;
  16722. layout->row.filled = 0;
  16723. layout->row.item.x = 0;
  16724. layout->row.item.y = 0;
  16725. layout->row.item.w = 0;
  16726. layout->row.item.h = 0;
  16727. }
  16728. NK_API void
  16729. nk_layout_row_template_push_dynamic(struct nk_context *ctx)
  16730. {
  16731. struct nk_window *win;
  16732. struct nk_panel *layout;
  16733. NK_ASSERT(ctx);
  16734. NK_ASSERT(ctx->current);
  16735. NK_ASSERT(ctx->current->layout);
  16736. if (!ctx || !ctx->current || !ctx->current->layout)
  16737. return;
  16738. win = ctx->current;
  16739. layout = win->layout;
  16740. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  16741. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  16742. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  16743. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  16744. layout->row.templates[layout->row.columns++] = -1.0f;
  16745. }
  16746. NK_API void
  16747. nk_layout_row_template_push_variable(struct nk_context *ctx, float min_width)
  16748. {
  16749. struct nk_window *win;
  16750. struct nk_panel *layout;
  16751. NK_ASSERT(ctx);
  16752. NK_ASSERT(ctx->current);
  16753. NK_ASSERT(ctx->current->layout);
  16754. if (!ctx || !ctx->current || !ctx->current->layout)
  16755. return;
  16756. win = ctx->current;
  16757. layout = win->layout;
  16758. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  16759. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  16760. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  16761. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  16762. layout->row.templates[layout->row.columns++] = -min_width;
  16763. }
  16764. NK_API void
  16765. nk_layout_row_template_push_static(struct nk_context *ctx, float width)
  16766. {
  16767. struct nk_window *win;
  16768. struct nk_panel *layout;
  16769. NK_ASSERT(ctx);
  16770. NK_ASSERT(ctx->current);
  16771. NK_ASSERT(ctx->current->layout);
  16772. if (!ctx || !ctx->current || !ctx->current->layout)
  16773. return;
  16774. win = ctx->current;
  16775. layout = win->layout;
  16776. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  16777. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  16778. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  16779. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  16780. layout->row.templates[layout->row.columns++] = width;
  16781. }
  16782. NK_API void
  16783. nk_layout_row_template_end(struct nk_context *ctx)
  16784. {
  16785. struct nk_window *win;
  16786. struct nk_panel *layout;
  16787. int i = 0;
  16788. int variable_count = 0;
  16789. int min_variable_count = 0;
  16790. float min_fixed_width = 0.0f;
  16791. float total_fixed_width = 0.0f;
  16792. float max_variable_width = 0.0f;
  16793. NK_ASSERT(ctx);
  16794. NK_ASSERT(ctx->current);
  16795. NK_ASSERT(ctx->current->layout);
  16796. if (!ctx || !ctx->current || !ctx->current->layout)
  16797. return;
  16798. win = ctx->current;
  16799. layout = win->layout;
  16800. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  16801. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  16802. for (i = 0; i < layout->row.columns; ++i) {
  16803. float width = layout->row.templates[i];
  16804. if (width >= 0.0f) {
  16805. total_fixed_width += width;
  16806. min_fixed_width += width;
  16807. } else if (width < -1.0f) {
  16808. width = -width;
  16809. total_fixed_width += width;
  16810. max_variable_width = NK_MAX(max_variable_width, width);
  16811. variable_count++;
  16812. } else {
  16813. min_variable_count++;
  16814. variable_count++;
  16815. }
  16816. }
  16817. if (variable_count) {
  16818. float space = nk_layout_row_calculate_usable_space(&ctx->style, layout->type,
  16819. layout->bounds.w, layout->row.columns);
  16820. float var_width = (NK_MAX(space-min_fixed_width,0.0f)) / (float)variable_count;
  16821. int enough_space = var_width >= max_variable_width;
  16822. if (!enough_space)
  16823. var_width = (NK_MAX(space-total_fixed_width,0)) / (float)min_variable_count;
  16824. for (i = 0; i < layout->row.columns; ++i) {
  16825. float *width = &layout->row.templates[i];
  16826. *width = (*width >= 0.0f)? *width: (*width < -1.0f && !enough_space)? -(*width): var_width;
  16827. }
  16828. }
  16829. }
  16830. NK_API void
  16831. nk_layout_space_begin(struct nk_context *ctx, enum nk_layout_format fmt,
  16832. float height, int widget_count)
  16833. {
  16834. struct nk_window *win;
  16835. struct nk_panel *layout;
  16836. NK_ASSERT(ctx);
  16837. NK_ASSERT(ctx->current);
  16838. NK_ASSERT(ctx->current->layout);
  16839. if (!ctx || !ctx->current || !ctx->current->layout)
  16840. return;
  16841. win = ctx->current;
  16842. layout = win->layout;
  16843. nk_panel_layout(ctx, win, height, widget_count);
  16844. if (fmt == NK_STATIC)
  16845. layout->row.type = NK_LAYOUT_STATIC_FREE;
  16846. else layout->row.type = NK_LAYOUT_DYNAMIC_FREE;
  16847. layout->row.ratio = 0;
  16848. layout->row.filled = 0;
  16849. layout->row.item_width = 0;
  16850. layout->row.item_offset = 0;
  16851. }
  16852. NK_API void
  16853. nk_layout_space_end(struct nk_context *ctx)
  16854. {
  16855. struct nk_window *win;
  16856. struct nk_panel *layout;
  16857. NK_ASSERT(ctx);
  16858. NK_ASSERT(ctx->current);
  16859. NK_ASSERT(ctx->current->layout);
  16860. if (!ctx || !ctx->current || !ctx->current->layout)
  16861. return;
  16862. win = ctx->current;
  16863. layout = win->layout;
  16864. layout->row.item_width = 0;
  16865. layout->row.item_height = 0;
  16866. layout->row.item_offset = 0;
  16867. nk_zero(&layout->row.item, sizeof(layout->row.item));
  16868. }
  16869. NK_API void
  16870. nk_layout_space_push(struct nk_context *ctx, struct nk_rect rect)
  16871. {
  16872. struct nk_window *win;
  16873. struct nk_panel *layout;
  16874. NK_ASSERT(ctx);
  16875. NK_ASSERT(ctx->current);
  16876. NK_ASSERT(ctx->current->layout);
  16877. if (!ctx || !ctx->current || !ctx->current->layout)
  16878. return;
  16879. win = ctx->current;
  16880. layout = win->layout;
  16881. layout->row.item = rect;
  16882. }
  16883. NK_API struct nk_rect
  16884. nk_layout_space_bounds(struct nk_context *ctx)
  16885. {
  16886. struct nk_rect ret;
  16887. struct nk_window *win;
  16888. struct nk_panel *layout;
  16889. NK_ASSERT(ctx);
  16890. NK_ASSERT(ctx->current);
  16891. NK_ASSERT(ctx->current->layout);
  16892. win = ctx->current;
  16893. layout = win->layout;
  16894. ret.x = layout->clip.x;
  16895. ret.y = layout->clip.y;
  16896. ret.w = layout->clip.w;
  16897. ret.h = layout->row.height;
  16898. return ret;
  16899. }
  16900. NK_API struct nk_rect
  16901. nk_layout_widget_bounds(struct nk_context *ctx)
  16902. {
  16903. struct nk_rect ret;
  16904. struct nk_window *win;
  16905. struct nk_panel *layout;
  16906. NK_ASSERT(ctx);
  16907. NK_ASSERT(ctx->current);
  16908. NK_ASSERT(ctx->current->layout);
  16909. win = ctx->current;
  16910. layout = win->layout;
  16911. ret.x = layout->at_x;
  16912. ret.y = layout->at_y;
  16913. ret.w = layout->bounds.w - NK_MAX(layout->at_x - layout->bounds.x,0);
  16914. ret.h = layout->row.height;
  16915. return ret;
  16916. }
  16917. NK_API struct nk_vec2
  16918. nk_layout_space_to_screen(struct nk_context *ctx, struct nk_vec2 ret)
  16919. {
  16920. struct nk_window *win;
  16921. struct nk_panel *layout;
  16922. NK_ASSERT(ctx);
  16923. NK_ASSERT(ctx->current);
  16924. NK_ASSERT(ctx->current->layout);
  16925. win = ctx->current;
  16926. layout = win->layout;
  16927. ret.x += layout->at_x - (float)*layout->offset_x;
  16928. ret.y += layout->at_y - (float)*layout->offset_y;
  16929. return ret;
  16930. }
  16931. NK_API struct nk_vec2
  16932. nk_layout_space_to_local(struct nk_context *ctx, struct nk_vec2 ret)
  16933. {
  16934. struct nk_window *win;
  16935. struct nk_panel *layout;
  16936. NK_ASSERT(ctx);
  16937. NK_ASSERT(ctx->current);
  16938. NK_ASSERT(ctx->current->layout);
  16939. win = ctx->current;
  16940. layout = win->layout;
  16941. ret.x += -layout->at_x + (float)*layout->offset_x;
  16942. ret.y += -layout->at_y + (float)*layout->offset_y;
  16943. return ret;
  16944. }
  16945. NK_API struct nk_rect
  16946. nk_layout_space_rect_to_screen(struct nk_context *ctx, struct nk_rect ret)
  16947. {
  16948. struct nk_window *win;
  16949. struct nk_panel *layout;
  16950. NK_ASSERT(ctx);
  16951. NK_ASSERT(ctx->current);
  16952. NK_ASSERT(ctx->current->layout);
  16953. win = ctx->current;
  16954. layout = win->layout;
  16955. ret.x += layout->at_x - (float)*layout->offset_x;
  16956. ret.y += layout->at_y - (float)*layout->offset_y;
  16957. return ret;
  16958. }
  16959. NK_API struct nk_rect
  16960. nk_layout_space_rect_to_local(struct nk_context *ctx, struct nk_rect ret)
  16961. {
  16962. struct nk_window *win;
  16963. struct nk_panel *layout;
  16964. NK_ASSERT(ctx);
  16965. NK_ASSERT(ctx->current);
  16966. NK_ASSERT(ctx->current->layout);
  16967. win = ctx->current;
  16968. layout = win->layout;
  16969. ret.x += -layout->at_x + (float)*layout->offset_x;
  16970. ret.y += -layout->at_y + (float)*layout->offset_y;
  16971. return ret;
  16972. }
  16973. NK_LIB void
  16974. nk_panel_alloc_row(const struct nk_context *ctx, struct nk_window *win)
  16975. {
  16976. struct nk_panel *layout = win->layout;
  16977. struct nk_vec2 spacing = ctx->style.window.spacing;
  16978. const float row_height = layout->row.height - spacing.y;
  16979. nk_panel_layout(ctx, win, row_height, layout->row.columns);
  16980. }
  16981. NK_LIB void
  16982. nk_layout_widget_space(struct nk_rect *bounds, const struct nk_context *ctx,
  16983. struct nk_window *win, int modify)
  16984. {
  16985. struct nk_panel *layout;
  16986. const struct nk_style *style;
  16987. struct nk_vec2 spacing;
  16988. struct nk_vec2 padding;
  16989. float item_offset = 0;
  16990. float item_width = 0;
  16991. float item_spacing = 0;
  16992. float panel_space = 0;
  16993. NK_ASSERT(ctx);
  16994. NK_ASSERT(ctx->current);
  16995. NK_ASSERT(ctx->current->layout);
  16996. if (!ctx || !ctx->current || !ctx->current->layout)
  16997. return;
  16998. win = ctx->current;
  16999. layout = win->layout;
  17000. style = &ctx->style;
  17001. NK_ASSERT(bounds);
  17002. spacing = style->window.spacing;
  17003. padding = nk_panel_get_padding(style, layout->type);
  17004. panel_space = nk_layout_row_calculate_usable_space(&ctx->style, layout->type,
  17005. layout->bounds.w, layout->row.columns);
  17006. #define NK_FRAC(x) (x - (int)x) /* will be used to remove fookin gaps */
  17007. /* calculate the width of one item inside the current layout space */
  17008. switch (layout->row.type) {
  17009. case NK_LAYOUT_DYNAMIC_FIXED: {
  17010. /* scaling fixed size widgets item width */
  17011. float w = NK_MAX(1.0f,panel_space) / (float)layout->row.columns;
  17012. item_offset = (float)layout->row.index * w;
  17013. item_width = w + NK_FRAC(item_offset);
  17014. item_spacing = (float)layout->row.index * spacing.x;
  17015. } break;
  17016. case NK_LAYOUT_DYNAMIC_ROW: {
  17017. /* scaling single ratio widget width */
  17018. float w = layout->row.item_width * panel_space;
  17019. item_offset = layout->row.item_offset;
  17020. item_width = w + NK_FRAC(item_offset);
  17021. item_spacing = 0;
  17022. if (modify) {
  17023. layout->row.item_offset += w + spacing.x;
  17024. layout->row.filled += layout->row.item_width;
  17025. layout->row.index = 0;
  17026. }
  17027. } break;
  17028. case NK_LAYOUT_DYNAMIC_FREE: {
  17029. /* panel width depended free widget placing */
  17030. bounds->x = layout->at_x + (layout->bounds.w * layout->row.item.x);
  17031. bounds->x -= (float)*layout->offset_x;
  17032. bounds->y = layout->at_y + (layout->row.height * layout->row.item.y);
  17033. bounds->y -= (float)*layout->offset_y;
  17034. bounds->w = layout->bounds.w * layout->row.item.w + NK_FRAC(bounds->x);
  17035. bounds->h = layout->row.height * layout->row.item.h + NK_FRAC(bounds->y);
  17036. return;
  17037. }
  17038. case NK_LAYOUT_DYNAMIC: {
  17039. /* scaling arrays of panel width ratios for every widget */
  17040. float ratio, w;
  17041. NK_ASSERT(layout->row.ratio);
  17042. ratio = (layout->row.ratio[layout->row.index] < 0) ?
  17043. layout->row.item_width : layout->row.ratio[layout->row.index];
  17044. w = (ratio * panel_space);
  17045. item_spacing = (float)layout->row.index * spacing.x;
  17046. item_offset = layout->row.item_offset;
  17047. item_width = w + NK_FRAC(item_offset);
  17048. if (modify) {
  17049. layout->row.item_offset += w;
  17050. layout->row.filled += ratio;
  17051. }
  17052. } break;
  17053. case NK_LAYOUT_STATIC_FIXED: {
  17054. /* non-scaling fixed widgets item width */
  17055. item_width = layout->row.item_width;
  17056. item_offset = (float)layout->row.index * item_width;
  17057. item_spacing = (float)layout->row.index * spacing.x;
  17058. } break;
  17059. case NK_LAYOUT_STATIC_ROW: {
  17060. /* scaling single ratio widget width */
  17061. item_width = layout->row.item_width;
  17062. item_offset = layout->row.item_offset;
  17063. item_spacing = (float)layout->row.index * spacing.x;
  17064. if (modify) layout->row.item_offset += item_width;
  17065. } break;
  17066. case NK_LAYOUT_STATIC_FREE: {
  17067. /* free widget placing */
  17068. bounds->x = layout->at_x + layout->row.item.x;
  17069. bounds->w = layout->row.item.w;
  17070. if (((bounds->x + bounds->w) > layout->max_x) && modify)
  17071. layout->max_x = (bounds->x + bounds->w);
  17072. bounds->x -= (float)*layout->offset_x;
  17073. bounds->y = layout->at_y + layout->row.item.y;
  17074. bounds->y -= (float)*layout->offset_y;
  17075. bounds->h = layout->row.item.h;
  17076. return;
  17077. }
  17078. case NK_LAYOUT_STATIC: {
  17079. /* non-scaling array of panel pixel width for every widget */
  17080. item_spacing = (float)layout->row.index * spacing.x;
  17081. item_width = layout->row.ratio[layout->row.index];
  17082. item_offset = layout->row.item_offset;
  17083. if (modify) layout->row.item_offset += item_width;
  17084. } break;
  17085. case NK_LAYOUT_TEMPLATE: {
  17086. /* stretchy row layout with combined dynamic/static widget width*/
  17087. float w;
  17088. NK_ASSERT(layout->row.index < layout->row.columns);
  17089. NK_ASSERT(layout->row.index < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  17090. w = layout->row.templates[layout->row.index];
  17091. item_offset = layout->row.item_offset;
  17092. item_width = w + NK_FRAC(item_offset);
  17093. item_spacing = (float)layout->row.index * spacing.x;
  17094. if (modify) layout->row.item_offset += w;
  17095. } break;
  17096. #undef NK_FRAC
  17097. default: NK_ASSERT(0); break;
  17098. };
  17099. /* set the bounds of the newly allocated widget */
  17100. bounds->w = item_width;
  17101. bounds->h = layout->row.height - spacing.y;
  17102. bounds->y = layout->at_y - (float)*layout->offset_y;
  17103. bounds->x = layout->at_x + item_offset + item_spacing + padding.x;
  17104. if (((bounds->x + bounds->w) > layout->max_x) && modify)
  17105. layout->max_x = bounds->x + bounds->w;
  17106. bounds->x -= (float)*layout->offset_x;
  17107. }
  17108. NK_LIB void
  17109. nk_panel_alloc_space(struct nk_rect *bounds, const struct nk_context *ctx)
  17110. {
  17111. struct nk_window *win;
  17112. struct nk_panel *layout;
  17113. NK_ASSERT(ctx);
  17114. NK_ASSERT(ctx->current);
  17115. NK_ASSERT(ctx->current->layout);
  17116. if (!ctx || !ctx->current || !ctx->current->layout)
  17117. return;
  17118. /* check if the end of the row has been hit and begin new row if so */
  17119. win = ctx->current;
  17120. layout = win->layout;
  17121. if (layout->row.index >= layout->row.columns)
  17122. nk_panel_alloc_row(ctx, win);
  17123. /* calculate widget position and size */
  17124. nk_layout_widget_space(bounds, ctx, win, nk_true);
  17125. layout->row.index++;
  17126. }
  17127. NK_LIB void
  17128. nk_layout_peek(struct nk_rect *bounds, struct nk_context *ctx)
  17129. {
  17130. float y;
  17131. int index;
  17132. struct nk_window *win;
  17133. struct nk_panel *layout;
  17134. NK_ASSERT(ctx);
  17135. NK_ASSERT(ctx->current);
  17136. NK_ASSERT(ctx->current->layout);
  17137. if (!ctx || !ctx->current || !ctx->current->layout)
  17138. return;
  17139. win = ctx->current;
  17140. layout = win->layout;
  17141. y = layout->at_y;
  17142. index = layout->row.index;
  17143. if (layout->row.index >= layout->row.columns) {
  17144. layout->at_y += layout->row.height;
  17145. layout->row.index = 0;
  17146. }
  17147. nk_layout_widget_space(bounds, ctx, win, nk_false);
  17148. if (!layout->row.index) {
  17149. bounds->x -= layout->row.item_offset;
  17150. }
  17151. layout->at_y = y;
  17152. layout->row.index = index;
  17153. }
  17154. /* ===============================================================
  17155. *
  17156. * TREE
  17157. *
  17158. * ===============================================================*/
  17159. NK_INTERN int
  17160. nk_tree_state_base(struct nk_context *ctx, enum nk_tree_type type,
  17161. struct nk_image *img, const char *title, enum nk_collapse_states *state)
  17162. {
  17163. struct nk_window *win;
  17164. struct nk_panel *layout;
  17165. const struct nk_style *style;
  17166. struct nk_command_buffer *out;
  17167. const struct nk_input *in;
  17168. const struct nk_style_button *button;
  17169. enum nk_symbol_type symbol;
  17170. float row_height;
  17171. struct nk_vec2 item_spacing;
  17172. struct nk_rect header = {0,0,0,0};
  17173. struct nk_rect sym = {0,0,0,0};
  17174. struct nk_text text;
  17175. nk_flags ws = 0;
  17176. enum nk_widget_layout_states widget_state;
  17177. NK_ASSERT(ctx);
  17178. NK_ASSERT(ctx->current);
  17179. NK_ASSERT(ctx->current->layout);
  17180. if (!ctx || !ctx->current || !ctx->current->layout)
  17181. return 0;
  17182. /* cache some data */
  17183. win = ctx->current;
  17184. layout = win->layout;
  17185. out = &win->buffer;
  17186. style = &ctx->style;
  17187. item_spacing = style->window.spacing;
  17188. /* calculate header bounds and draw background */
  17189. row_height = style->font->height + 2 * style->tab.padding.y;
  17190. nk_layout_set_min_row_height(ctx, row_height);
  17191. nk_layout_row_dynamic(ctx, row_height, 1);
  17192. nk_layout_reset_min_row_height(ctx);
  17193. widget_state = nk_widget(&header, ctx);
  17194. if (type == NK_TREE_TAB) {
  17195. const struct nk_style_item *background = &style->tab.background;
  17196. if (background->type == NK_STYLE_ITEM_IMAGE) {
  17197. nk_draw_image(out, header, &background->data.image, nk_white);
  17198. text.background = nk_rgba(0,0,0,0);
  17199. } else {
  17200. text.background = background->data.color;
  17201. nk_fill_rect(out, header, 0, style->tab.border_color);
  17202. nk_fill_rect(out, nk_shrink_rect(header, style->tab.border),
  17203. style->tab.rounding, background->data.color);
  17204. }
  17205. } else text.background = style->window.background;
  17206. /* update node state */
  17207. in = (!(layout->flags & NK_WINDOW_ROM)) ? &ctx->input: 0;
  17208. in = (in && widget_state == NK_WIDGET_VALID) ? &ctx->input : 0;
  17209. if (nk_button_behavior(&ws, header, in, NK_BUTTON_DEFAULT))
  17210. *state = (*state == NK_MAXIMIZED) ? NK_MINIMIZED : NK_MAXIMIZED;
  17211. /* select correct button style */
  17212. if (*state == NK_MAXIMIZED) {
  17213. symbol = style->tab.sym_maximize;
  17214. if (type == NK_TREE_TAB)
  17215. button = &style->tab.tab_maximize_button;
  17216. else button = &style->tab.node_maximize_button;
  17217. } else {
  17218. symbol = style->tab.sym_minimize;
  17219. if (type == NK_TREE_TAB)
  17220. button = &style->tab.tab_minimize_button;
  17221. else button = &style->tab.node_minimize_button;
  17222. }
  17223. {/* draw triangle button */
  17224. sym.w = sym.h = style->font->height;
  17225. sym.y = header.y + style->tab.padding.y;
  17226. sym.x = header.x + style->tab.padding.x;
  17227. nk_do_button_symbol(&ws, &win->buffer, sym, symbol, NK_BUTTON_DEFAULT,
  17228. button, 0, style->font);
  17229. if (img) {
  17230. /* draw optional image icon */
  17231. sym.x = sym.x + sym.w + 4 * item_spacing.x;
  17232. nk_draw_image(&win->buffer, sym, img, nk_white);
  17233. sym.w = style->font->height + style->tab.spacing.x;}
  17234. }
  17235. {/* draw label */
  17236. struct nk_rect label;
  17237. header.w = NK_MAX(header.w, sym.w + item_spacing.x);
  17238. label.x = sym.x + sym.w + item_spacing.x;
  17239. label.y = sym.y;
  17240. label.w = header.w - (sym.w + item_spacing.y + style->tab.indent);
  17241. label.h = style->font->height;
  17242. text.text = style->tab.text;
  17243. text.padding = nk_vec2(0,0);
  17244. nk_widget_text(out, label, title, nk_strlen(title), &text,
  17245. NK_TEXT_LEFT, style->font);}
  17246. /* increase x-axis cursor widget position pointer */
  17247. if (*state == NK_MAXIMIZED) {
  17248. layout->at_x = header.x + (float)*layout->offset_x + style->tab.indent;
  17249. layout->bounds.w = NK_MAX(layout->bounds.w, style->tab.indent);
  17250. layout->bounds.w -= (style->tab.indent + style->window.padding.x);
  17251. layout->row.tree_depth++;
  17252. return nk_true;
  17253. } else return nk_false;
  17254. }
  17255. NK_INTERN int
  17256. nk_tree_base(struct nk_context *ctx, enum nk_tree_type type,
  17257. struct nk_image *img, const char *title, enum nk_collapse_states initial_state,
  17258. const char *hash, int len, int line)
  17259. {
  17260. struct nk_window *win = ctx->current;
  17261. int title_len = 0;
  17262. nk_hash tree_hash = 0;
  17263. nk_uint *state = 0;
  17264. /* retrieve tree state from internal widget state tables */
  17265. if (!hash) {
  17266. title_len = (int)nk_strlen(title);
  17267. tree_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)line);
  17268. } else tree_hash = nk_murmur_hash(hash, len, (nk_hash)line);
  17269. state = nk_find_value(win, tree_hash);
  17270. if (!state) {
  17271. state = nk_add_value(ctx, win, tree_hash, 0);
  17272. *state = initial_state;
  17273. }
  17274. return nk_tree_state_base(ctx, type, img, title, (enum nk_collapse_states*)state);
  17275. }
  17276. NK_API int
  17277. nk_tree_state_push(struct nk_context *ctx, enum nk_tree_type type,
  17278. const char *title, enum nk_collapse_states *state)
  17279. {
  17280. return nk_tree_state_base(ctx, type, 0, title, state);
  17281. }
  17282. NK_API int
  17283. nk_tree_state_image_push(struct nk_context *ctx, enum nk_tree_type type,
  17284. struct nk_image img, const char *title, enum nk_collapse_states *state)
  17285. {
  17286. return nk_tree_state_base(ctx, type, &img, title, state);
  17287. }
  17288. NK_API void
  17289. nk_tree_state_pop(struct nk_context *ctx)
  17290. {
  17291. struct nk_window *win = 0;
  17292. struct nk_panel *layout = 0;
  17293. NK_ASSERT(ctx);
  17294. NK_ASSERT(ctx->current);
  17295. NK_ASSERT(ctx->current->layout);
  17296. if (!ctx || !ctx->current || !ctx->current->layout)
  17297. return;
  17298. win = ctx->current;
  17299. layout = win->layout;
  17300. layout->at_x -= ctx->style.tab.indent + ctx->style.window.padding.x;
  17301. layout->bounds.w += ctx->style.tab.indent + ctx->style.window.padding.x;
  17302. NK_ASSERT(layout->row.tree_depth);
  17303. layout->row.tree_depth--;
  17304. }
  17305. NK_API int
  17306. nk_tree_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  17307. const char *title, enum nk_collapse_states initial_state,
  17308. const char *hash, int len, int line)
  17309. {
  17310. return nk_tree_base(ctx, type, 0, title, initial_state, hash, len, line);
  17311. }
  17312. NK_API int
  17313. nk_tree_image_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  17314. struct nk_image img, const char *title, enum nk_collapse_states initial_state,
  17315. const char *hash, int len,int seed)
  17316. {
  17317. return nk_tree_base(ctx, type, &img, title, initial_state, hash, len, seed);
  17318. }
  17319. NK_API void
  17320. nk_tree_pop(struct nk_context *ctx)
  17321. {
  17322. nk_tree_state_pop(ctx);
  17323. }
  17324. NK_INTERN int
  17325. nk_tree_element_image_push_hashed_base(struct nk_context *ctx, enum nk_tree_type type,
  17326. struct nk_image *img, const char *title, int title_len,
  17327. enum nk_collapse_states *state, int *selected)
  17328. {
  17329. struct nk_window *win;
  17330. struct nk_panel *layout;
  17331. const struct nk_style *style;
  17332. struct nk_command_buffer *out;
  17333. const struct nk_input *in;
  17334. const struct nk_style_button *button;
  17335. enum nk_symbol_type symbol;
  17336. float row_height;
  17337. struct nk_vec2 padding;
  17338. int text_len;
  17339. float text_width;
  17340. struct nk_vec2 item_spacing;
  17341. struct nk_rect header = {0,0,0,0};
  17342. struct nk_rect sym = {0,0,0,0};
  17343. struct nk_text text;
  17344. nk_flags ws = 0;
  17345. enum nk_widget_layout_states widget_state;
  17346. NK_ASSERT(ctx);
  17347. NK_ASSERT(ctx->current);
  17348. NK_ASSERT(ctx->current->layout);
  17349. if (!ctx || !ctx->current || !ctx->current->layout)
  17350. return 0;
  17351. /* cache some data */
  17352. win = ctx->current;
  17353. layout = win->layout;
  17354. out = &win->buffer;
  17355. style = &ctx->style;
  17356. item_spacing = style->window.spacing;
  17357. padding = style->selectable.padding;
  17358. /* calculate header bounds and draw background */
  17359. row_height = style->font->height + 2 * style->tab.padding.y;
  17360. nk_layout_set_min_row_height(ctx, row_height);
  17361. nk_layout_row_dynamic(ctx, row_height, 1);
  17362. nk_layout_reset_min_row_height(ctx);
  17363. widget_state = nk_widget(&header, ctx);
  17364. if (type == NK_TREE_TAB) {
  17365. const struct nk_style_item *background = &style->tab.background;
  17366. if (background->type == NK_STYLE_ITEM_IMAGE) {
  17367. nk_draw_image(out, header, &background->data.image, nk_white);
  17368. text.background = nk_rgba(0,0,0,0);
  17369. } else {
  17370. text.background = background->data.color;
  17371. nk_fill_rect(out, header, 0, style->tab.border_color);
  17372. nk_fill_rect(out, nk_shrink_rect(header, style->tab.border),
  17373. style->tab.rounding, background->data.color);
  17374. }
  17375. } else text.background = style->window.background;
  17376. in = (!(layout->flags & NK_WINDOW_ROM)) ? &ctx->input: 0;
  17377. in = (in && widget_state == NK_WIDGET_VALID) ? &ctx->input : 0;
  17378. /* select correct button style */
  17379. if (*state == NK_MAXIMIZED) {
  17380. symbol = style->tab.sym_maximize;
  17381. if (type == NK_TREE_TAB)
  17382. button = &style->tab.tab_maximize_button;
  17383. else button = &style->tab.node_maximize_button;
  17384. } else {
  17385. symbol = style->tab.sym_minimize;
  17386. if (type == NK_TREE_TAB)
  17387. button = &style->tab.tab_minimize_button;
  17388. else button = &style->tab.node_minimize_button;
  17389. }
  17390. {/* draw triangle button */
  17391. sym.w = sym.h = style->font->height;
  17392. sym.y = header.y + style->tab.padding.y;
  17393. sym.x = header.x + style->tab.padding.x;
  17394. if (nk_do_button_symbol(&ws, &win->buffer, sym, symbol, NK_BUTTON_DEFAULT, button, in, style->font))
  17395. *state = (*state == NK_MAXIMIZED) ? NK_MINIMIZED : NK_MAXIMIZED;}
  17396. /* draw label */
  17397. {nk_flags dummy = 0;
  17398. struct nk_rect label;
  17399. /* calculate size of the text and tooltip */
  17400. text_len = nk_strlen(title);
  17401. text_width = style->font->width(style->font->userdata, style->font->height, title, text_len);
  17402. text_width += (4 * padding.x);
  17403. header.w = NK_MAX(header.w, sym.w + item_spacing.x);
  17404. label.x = sym.x + sym.w + item_spacing.x;
  17405. label.y = sym.y;
  17406. label.w = NK_MIN(header.w - (sym.w + item_spacing.y + style->tab.indent), text_width);
  17407. label.h = style->font->height;
  17408. if (img) {
  17409. nk_do_selectable_image(&dummy, &win->buffer, label, title, title_len, NK_TEXT_LEFT,
  17410. selected, img, &style->selectable, in, style->font);
  17411. } else nk_do_selectable(&dummy, &win->buffer, label, title, title_len, NK_TEXT_LEFT,
  17412. selected, &style->selectable, in, style->font);
  17413. }
  17414. /* increase x-axis cursor widget position pointer */
  17415. if (*state == NK_MAXIMIZED) {
  17416. layout->at_x = header.x + (float)*layout->offset_x + style->tab.indent;
  17417. layout->bounds.w = NK_MAX(layout->bounds.w, style->tab.indent);
  17418. layout->bounds.w -= (style->tab.indent + style->window.padding.x);
  17419. layout->row.tree_depth++;
  17420. return nk_true;
  17421. } else return nk_false;
  17422. }
  17423. NK_INTERN int
  17424. nk_tree_element_base(struct nk_context *ctx, enum nk_tree_type type,
  17425. struct nk_image *img, const char *title, enum nk_collapse_states initial_state,
  17426. int *selected, const char *hash, int len, int line)
  17427. {
  17428. struct nk_window *win = ctx->current;
  17429. int title_len = 0;
  17430. nk_hash tree_hash = 0;
  17431. nk_uint *state = 0;
  17432. /* retrieve tree state from internal widget state tables */
  17433. if (!hash) {
  17434. title_len = (int)nk_strlen(title);
  17435. tree_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)line);
  17436. } else tree_hash = nk_murmur_hash(hash, len, (nk_hash)line);
  17437. state = nk_find_value(win, tree_hash);
  17438. if (!state) {
  17439. state = nk_add_value(ctx, win, tree_hash, 0);
  17440. *state = initial_state;
  17441. } return nk_tree_element_image_push_hashed_base(ctx, type, img, title,
  17442. nk_strlen(title), (enum nk_collapse_states*)state, selected);
  17443. }
  17444. NK_API int
  17445. nk_tree_element_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  17446. const char *title, enum nk_collapse_states initial_state,
  17447. int *selected, const char *hash, int len, int seed)
  17448. {
  17449. return nk_tree_element_base(ctx, type, 0, title, initial_state, selected, hash, len, seed);
  17450. }
  17451. NK_API int
  17452. nk_tree_element_image_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  17453. struct nk_image img, const char *title, enum nk_collapse_states initial_state,
  17454. int *selected, const char *hash, int len,int seed)
  17455. {
  17456. return nk_tree_element_base(ctx, type, &img, title, initial_state, selected, hash, len, seed);
  17457. }
  17458. NK_API void
  17459. nk_tree_element_pop(struct nk_context *ctx)
  17460. {
  17461. nk_tree_state_pop(ctx);
  17462. }
  17463. /* ===============================================================
  17464. *
  17465. * GROUP
  17466. *
  17467. * ===============================================================*/
  17468. NK_API int
  17469. nk_group_scrolled_offset_begin(struct nk_context *ctx,
  17470. nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags)
  17471. {
  17472. struct nk_rect bounds;
  17473. struct nk_window panel;
  17474. struct nk_window *win;
  17475. win = ctx->current;
  17476. nk_panel_alloc_space(&bounds, ctx);
  17477. {const struct nk_rect *c = &win->layout->clip;
  17478. if (!NK_INTERSECT(c->x, c->y, c->w, c->h, bounds.x, bounds.y, bounds.w, bounds.h) &&
  17479. !(flags & NK_WINDOW_MOVABLE)) {
  17480. return 0;
  17481. }}
  17482. if (win->flags & NK_WINDOW_ROM)
  17483. flags |= NK_WINDOW_ROM;
  17484. /* initialize a fake window to create the panel from */
  17485. nk_zero(&panel, sizeof(panel));
  17486. panel.bounds = bounds;
  17487. panel.flags = flags;
  17488. panel.scrollbar.x = *x_offset;
  17489. panel.scrollbar.y = *y_offset;
  17490. panel.buffer = win->buffer;
  17491. panel.layout = (struct nk_panel*)nk_create_panel(ctx);
  17492. ctx->current = &panel;
  17493. nk_panel_begin(ctx, (flags & NK_WINDOW_TITLE) ? title: 0, NK_PANEL_GROUP);
  17494. win->buffer = panel.buffer;
  17495. win->buffer.clip = panel.layout->clip;
  17496. panel.layout->offset_x = x_offset;
  17497. panel.layout->offset_y = y_offset;
  17498. panel.layout->parent = win->layout;
  17499. win->layout = panel.layout;
  17500. ctx->current = win;
  17501. if ((panel.layout->flags & NK_WINDOW_CLOSED) ||
  17502. (panel.layout->flags & NK_WINDOW_MINIMIZED))
  17503. {
  17504. nk_flags f = panel.layout->flags;
  17505. nk_group_scrolled_end(ctx);
  17506. if (f & NK_WINDOW_CLOSED)
  17507. return NK_WINDOW_CLOSED;
  17508. if (f & NK_WINDOW_MINIMIZED)
  17509. return NK_WINDOW_MINIMIZED;
  17510. }
  17511. return 1;
  17512. }
  17513. NK_API void
  17514. nk_group_scrolled_end(struct nk_context *ctx)
  17515. {
  17516. struct nk_window *win;
  17517. struct nk_panel *parent;
  17518. struct nk_panel *g;
  17519. struct nk_rect clip;
  17520. struct nk_window pan;
  17521. struct nk_vec2 panel_padding;
  17522. NK_ASSERT(ctx);
  17523. NK_ASSERT(ctx->current);
  17524. if (!ctx || !ctx->current)
  17525. return;
  17526. /* make sure nk_group_begin was called correctly */
  17527. NK_ASSERT(ctx->current);
  17528. win = ctx->current;
  17529. NK_ASSERT(win->layout);
  17530. g = win->layout;
  17531. NK_ASSERT(g->parent);
  17532. parent = g->parent;
  17533. /* dummy window */
  17534. nk_zero_struct(pan);
  17535. panel_padding = nk_panel_get_padding(&ctx->style, NK_PANEL_GROUP);
  17536. pan.bounds.y = g->bounds.y - (g->header_height + g->menu.h);
  17537. pan.bounds.x = g->bounds.x - panel_padding.x;
  17538. pan.bounds.w = g->bounds.w + 2 * panel_padding.x;
  17539. pan.bounds.h = g->bounds.h + g->header_height + g->menu.h;
  17540. if (g->flags & NK_WINDOW_BORDER) {
  17541. pan.bounds.x -= g->border;
  17542. pan.bounds.y -= g->border;
  17543. pan.bounds.w += 2*g->border;
  17544. pan.bounds.h += 2*g->border;
  17545. }
  17546. if (!(g->flags & NK_WINDOW_NO_SCROLLBAR)) {
  17547. pan.bounds.w += ctx->style.window.scrollbar_size.x;
  17548. pan.bounds.h += ctx->style.window.scrollbar_size.y;
  17549. }
  17550. pan.scrollbar.x = *g->offset_x;
  17551. pan.scrollbar.y = *g->offset_y;
  17552. pan.flags = g->flags;
  17553. pan.buffer = win->buffer;
  17554. pan.layout = g;
  17555. pan.parent = win;
  17556. ctx->current = &pan;
  17557. /* make sure group has correct clipping rectangle */
  17558. nk_unify(&clip, &parent->clip, pan.bounds.x, pan.bounds.y,
  17559. pan.bounds.x + pan.bounds.w, pan.bounds.y + pan.bounds.h + panel_padding.x);
  17560. nk_push_scissor(&pan.buffer, clip);
  17561. nk_end(ctx);
  17562. win->buffer = pan.buffer;
  17563. nk_push_scissor(&win->buffer, parent->clip);
  17564. ctx->current = win;
  17565. win->layout = parent;
  17566. g->bounds = pan.bounds;
  17567. return;
  17568. }
  17569. NK_API int
  17570. nk_group_scrolled_begin(struct nk_context *ctx,
  17571. struct nk_scroll *scroll, const char *title, nk_flags flags)
  17572. {
  17573. return nk_group_scrolled_offset_begin(ctx, &scroll->x, &scroll->y, title, flags);
  17574. }
  17575. NK_API int
  17576. nk_group_begin_titled(struct nk_context *ctx, const char *id,
  17577. const char *title, nk_flags flags)
  17578. {
  17579. int id_len;
  17580. nk_hash id_hash;
  17581. struct nk_window *win;
  17582. nk_uint *x_offset;
  17583. nk_uint *y_offset;
  17584. NK_ASSERT(ctx);
  17585. NK_ASSERT(id);
  17586. NK_ASSERT(ctx->current);
  17587. NK_ASSERT(ctx->current->layout);
  17588. if (!ctx || !ctx->current || !ctx->current->layout || !id)
  17589. return 0;
  17590. /* find persistent group scrollbar value */
  17591. win = ctx->current;
  17592. id_len = (int)nk_strlen(id);
  17593. id_hash = nk_murmur_hash(id, (int)id_len, NK_PANEL_GROUP);
  17594. x_offset = nk_find_value(win, id_hash);
  17595. if (!x_offset) {
  17596. x_offset = nk_add_value(ctx, win, id_hash, 0);
  17597. y_offset = nk_add_value(ctx, win, id_hash+1, 0);
  17598. NK_ASSERT(x_offset);
  17599. NK_ASSERT(y_offset);
  17600. if (!x_offset || !y_offset) return 0;
  17601. *x_offset = *y_offset = 0;
  17602. } else y_offset = nk_find_value(win, id_hash+1);
  17603. return nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
  17604. }
  17605. NK_API int
  17606. nk_group_begin(struct nk_context *ctx, const char *title, nk_flags flags)
  17607. {
  17608. return nk_group_begin_titled(ctx, title, title, flags);
  17609. }
  17610. NK_API void
  17611. nk_group_end(struct nk_context *ctx)
  17612. {
  17613. nk_group_scrolled_end(ctx);
  17614. }
  17615. NK_API void
  17616. nk_group_get_scroll(struct nk_context *ctx, const char *id, nk_uint *x_offset, nk_uint *y_offset)
  17617. {
  17618. int id_len;
  17619. nk_hash id_hash;
  17620. struct nk_window *win;
  17621. nk_uint *x_offset_ptr;
  17622. nk_uint *y_offset_ptr;
  17623. NK_ASSERT(ctx);
  17624. NK_ASSERT(id);
  17625. NK_ASSERT(ctx->current);
  17626. NK_ASSERT(ctx->current->layout);
  17627. if (!ctx || !ctx->current || !ctx->current->layout || !id)
  17628. return;
  17629. /* find persistent group scrollbar value */
  17630. win = ctx->current;
  17631. id_len = (int)nk_strlen(id);
  17632. id_hash = nk_murmur_hash(id, (int)id_len, NK_PANEL_GROUP);
  17633. x_offset_ptr = nk_find_value(win, id_hash);
  17634. if (!x_offset_ptr) {
  17635. x_offset_ptr = nk_add_value(ctx, win, id_hash, 0);
  17636. y_offset_ptr = nk_add_value(ctx, win, id_hash+1, 0);
  17637. NK_ASSERT(x_offset_ptr);
  17638. NK_ASSERT(y_offset_ptr);
  17639. if (!x_offset_ptr || !y_offset_ptr) return;
  17640. *x_offset_ptr = *y_offset_ptr = 0;
  17641. } else y_offset_ptr = nk_find_value(win, id_hash+1);
  17642. if (x_offset)
  17643. *x_offset = *x_offset_ptr;
  17644. if (y_offset)
  17645. *y_offset = *y_offset_ptr;
  17646. }
  17647. NK_API void
  17648. nk_group_set_scroll(struct nk_context *ctx, const char *id, nk_uint x_offset, nk_uint y_offset)
  17649. {
  17650. int id_len;
  17651. nk_hash id_hash;
  17652. struct nk_window *win;
  17653. nk_uint *x_offset_ptr;
  17654. nk_uint *y_offset_ptr;
  17655. NK_ASSERT(ctx);
  17656. NK_ASSERT(id);
  17657. NK_ASSERT(ctx->current);
  17658. NK_ASSERT(ctx->current->layout);
  17659. if (!ctx || !ctx->current || !ctx->current->layout || !id)
  17660. return;
  17661. /* find persistent group scrollbar value */
  17662. win = ctx->current;
  17663. id_len = (int)nk_strlen(id);
  17664. id_hash = nk_murmur_hash(id, (int)id_len, NK_PANEL_GROUP);
  17665. x_offset_ptr = nk_find_value(win, id_hash);
  17666. if (!x_offset_ptr) {
  17667. x_offset_ptr = nk_add_value(ctx, win, id_hash, 0);
  17668. y_offset_ptr = nk_add_value(ctx, win, id_hash+1, 0);
  17669. NK_ASSERT(x_offset_ptr);
  17670. NK_ASSERT(y_offset_ptr);
  17671. if (!x_offset_ptr || !y_offset_ptr) return;
  17672. *x_offset_ptr = *y_offset_ptr = 0;
  17673. } else y_offset_ptr = nk_find_value(win, id_hash+1);
  17674. *x_offset_ptr = x_offset;
  17675. *y_offset_ptr = y_offset;
  17676. }
  17677. /* ===============================================================
  17678. *
  17679. * LIST VIEW
  17680. *
  17681. * ===============================================================*/
  17682. NK_API int
  17683. nk_list_view_begin(struct nk_context *ctx, struct nk_list_view *view,
  17684. const char *title, nk_flags flags, int row_height, int row_count)
  17685. {
  17686. int title_len;
  17687. nk_hash title_hash;
  17688. nk_uint *x_offset;
  17689. nk_uint *y_offset;
  17690. int result;
  17691. struct nk_window *win;
  17692. struct nk_panel *layout;
  17693. const struct nk_style *style;
  17694. struct nk_vec2 item_spacing;
  17695. NK_ASSERT(ctx);
  17696. NK_ASSERT(view);
  17697. NK_ASSERT(title);
  17698. if (!ctx || !view || !title) return 0;
  17699. win = ctx->current;
  17700. style = &ctx->style;
  17701. item_spacing = style->window.spacing;
  17702. row_height += NK_MAX(0, (int)item_spacing.y);
  17703. /* find persistent list view scrollbar offset */
  17704. title_len = (int)nk_strlen(title);
  17705. title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_GROUP);
  17706. x_offset = nk_find_value(win, title_hash);
  17707. if (!x_offset) {
  17708. x_offset = nk_add_value(ctx, win, title_hash, 0);
  17709. y_offset = nk_add_value(ctx, win, title_hash+1, 0);
  17710. NK_ASSERT(x_offset);
  17711. NK_ASSERT(y_offset);
  17712. if (!x_offset || !y_offset) return 0;
  17713. *x_offset = *y_offset = 0;
  17714. } else y_offset = nk_find_value(win, title_hash+1);
  17715. view->scroll_value = *y_offset;
  17716. view->scroll_pointer = y_offset;
  17717. *y_offset = 0;
  17718. result = nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
  17719. win = ctx->current;
  17720. layout = win->layout;
  17721. view->total_height = row_height * NK_MAX(row_count,1);
  17722. view->begin = (int)NK_MAX(((float)view->scroll_value / (float)row_height), 0.0f);
  17723. view->count = (int)NK_MAX(nk_iceilf((layout->clip.h)/(float)row_height),0);
  17724. view->count = NK_MIN(view->count, row_count - view->begin);
  17725. view->end = view->begin + view->count;
  17726. view->ctx = ctx;
  17727. return result;
  17728. }
  17729. NK_API void
  17730. nk_list_view_end(struct nk_list_view *view)
  17731. {
  17732. struct nk_context *ctx;
  17733. struct nk_window *win;
  17734. struct nk_panel *layout;
  17735. NK_ASSERT(view);
  17736. NK_ASSERT(view->ctx);
  17737. NK_ASSERT(view->scroll_pointer);
  17738. if (!view || !view->ctx) return;
  17739. ctx = view->ctx;
  17740. win = ctx->current;
  17741. layout = win->layout;
  17742. layout->at_y = layout->bounds.y + (float)view->total_height;
  17743. *view->scroll_pointer = *view->scroll_pointer + view->scroll_value;
  17744. nk_group_end(view->ctx);
  17745. }
  17746. /* ===============================================================
  17747. *
  17748. * WIDGET
  17749. *
  17750. * ===============================================================*/
  17751. NK_API struct nk_rect
  17752. nk_widget_bounds(struct nk_context *ctx)
  17753. {
  17754. struct nk_rect bounds;
  17755. NK_ASSERT(ctx);
  17756. NK_ASSERT(ctx->current);
  17757. if (!ctx || !ctx->current)
  17758. return nk_rect(0,0,0,0);
  17759. nk_layout_peek(&bounds, ctx);
  17760. return bounds;
  17761. }
  17762. NK_API struct nk_vec2
  17763. nk_widget_position(struct nk_context *ctx)
  17764. {
  17765. struct nk_rect bounds;
  17766. NK_ASSERT(ctx);
  17767. NK_ASSERT(ctx->current);
  17768. if (!ctx || !ctx->current)
  17769. return nk_vec2(0,0);
  17770. nk_layout_peek(&bounds, ctx);
  17771. return nk_vec2(bounds.x, bounds.y);
  17772. }
  17773. NK_API struct nk_vec2
  17774. nk_widget_size(struct nk_context *ctx)
  17775. {
  17776. struct nk_rect bounds;
  17777. NK_ASSERT(ctx);
  17778. NK_ASSERT(ctx->current);
  17779. if (!ctx || !ctx->current)
  17780. return nk_vec2(0,0);
  17781. nk_layout_peek(&bounds, ctx);
  17782. return nk_vec2(bounds.w, bounds.h);
  17783. }
  17784. NK_API float
  17785. nk_widget_width(struct nk_context *ctx)
  17786. {
  17787. struct nk_rect bounds;
  17788. NK_ASSERT(ctx);
  17789. NK_ASSERT(ctx->current);
  17790. if (!ctx || !ctx->current)
  17791. return 0;
  17792. nk_layout_peek(&bounds, ctx);
  17793. return bounds.w;
  17794. }
  17795. NK_API float
  17796. nk_widget_height(struct nk_context *ctx)
  17797. {
  17798. struct nk_rect bounds;
  17799. NK_ASSERT(ctx);
  17800. NK_ASSERT(ctx->current);
  17801. if (!ctx || !ctx->current)
  17802. return 0;
  17803. nk_layout_peek(&bounds, ctx);
  17804. return bounds.h;
  17805. }
  17806. NK_API int
  17807. nk_widget_is_hovered(struct nk_context *ctx)
  17808. {
  17809. struct nk_rect c, v;
  17810. struct nk_rect bounds;
  17811. NK_ASSERT(ctx);
  17812. NK_ASSERT(ctx->current);
  17813. if (!ctx || !ctx->current || ctx->active != ctx->current)
  17814. return 0;
  17815. c = ctx->current->layout->clip;
  17816. c.x = (float)((int)c.x);
  17817. c.y = (float)((int)c.y);
  17818. c.w = (float)((int)c.w);
  17819. c.h = (float)((int)c.h);
  17820. nk_layout_peek(&bounds, ctx);
  17821. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  17822. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  17823. return 0;
  17824. return nk_input_is_mouse_hovering_rect(&ctx->input, bounds);
  17825. }
  17826. NK_API int
  17827. nk_widget_is_mouse_clicked(struct nk_context *ctx, enum nk_buttons btn)
  17828. {
  17829. struct nk_rect c, v;
  17830. struct nk_rect bounds;
  17831. NK_ASSERT(ctx);
  17832. NK_ASSERT(ctx->current);
  17833. if (!ctx || !ctx->current || ctx->active != ctx->current)
  17834. return 0;
  17835. c = ctx->current->layout->clip;
  17836. c.x = (float)((int)c.x);
  17837. c.y = (float)((int)c.y);
  17838. c.w = (float)((int)c.w);
  17839. c.h = (float)((int)c.h);
  17840. nk_layout_peek(&bounds, ctx);
  17841. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  17842. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  17843. return 0;
  17844. return nk_input_mouse_clicked(&ctx->input, btn, bounds);
  17845. }
  17846. NK_API int
  17847. nk_widget_has_mouse_click_down(struct nk_context *ctx, enum nk_buttons btn, int down)
  17848. {
  17849. struct nk_rect c, v;
  17850. struct nk_rect bounds;
  17851. NK_ASSERT(ctx);
  17852. NK_ASSERT(ctx->current);
  17853. if (!ctx || !ctx->current || ctx->active != ctx->current)
  17854. return 0;
  17855. c = ctx->current->layout->clip;
  17856. c.x = (float)((int)c.x);
  17857. c.y = (float)((int)c.y);
  17858. c.w = (float)((int)c.w);
  17859. c.h = (float)((int)c.h);
  17860. nk_layout_peek(&bounds, ctx);
  17861. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  17862. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  17863. return 0;
  17864. return nk_input_has_mouse_click_down_in_rect(&ctx->input, btn, bounds, down);
  17865. }
  17866. NK_API enum nk_widget_layout_states
  17867. nk_widget(struct nk_rect *bounds, const struct nk_context *ctx)
  17868. {
  17869. struct nk_rect c, v;
  17870. struct nk_window *win;
  17871. struct nk_panel *layout;
  17872. const struct nk_input *in;
  17873. NK_ASSERT(ctx);
  17874. NK_ASSERT(ctx->current);
  17875. NK_ASSERT(ctx->current->layout);
  17876. if (!ctx || !ctx->current || !ctx->current->layout)
  17877. return NK_WIDGET_INVALID;
  17878. /* allocate space and check if the widget needs to be updated and drawn */
  17879. nk_panel_alloc_space(bounds, ctx);
  17880. win = ctx->current;
  17881. layout = win->layout;
  17882. in = &ctx->input;
  17883. c = layout->clip;
  17884. /* if one of these triggers you forgot to add an `if` condition around either
  17885. a window, group, popup, combobox or contextual menu `begin` and `end` block.
  17886. Example:
  17887. if (nk_begin(...) {...} nk_end(...); or
  17888. if (nk_group_begin(...) { nk_group_end(...);} */
  17889. NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
  17890. NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
  17891. NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
  17892. /* need to convert to int here to remove floating point errors */
  17893. bounds->x = (float)((int)bounds->x);
  17894. bounds->y = (float)((int)bounds->y);
  17895. bounds->w = (float)((int)bounds->w);
  17896. bounds->h = (float)((int)bounds->h);
  17897. c.x = (float)((int)c.x);
  17898. c.y = (float)((int)c.y);
  17899. c.w = (float)((int)c.w);
  17900. c.h = (float)((int)c.h);
  17901. nk_unify(&v, &c, bounds->x, bounds->y, bounds->x + bounds->w, bounds->y + bounds->h);
  17902. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds->x, bounds->y, bounds->w, bounds->h))
  17903. return NK_WIDGET_INVALID;
  17904. if (!NK_INBOX(in->mouse.pos.x, in->mouse.pos.y, v.x, v.y, v.w, v.h))
  17905. return NK_WIDGET_ROM;
  17906. return NK_WIDGET_VALID;
  17907. }
  17908. NK_API enum nk_widget_layout_states
  17909. nk_widget_fitting(struct nk_rect *bounds, struct nk_context *ctx,
  17910. struct nk_vec2 item_padding)
  17911. {
  17912. /* update the bounds to stand without padding */
  17913. struct nk_window *win;
  17914. struct nk_style *style;
  17915. struct nk_panel *layout;
  17916. enum nk_widget_layout_states state;
  17917. struct nk_vec2 panel_padding;
  17918. NK_ASSERT(ctx);
  17919. NK_ASSERT(ctx->current);
  17920. NK_ASSERT(ctx->current->layout);
  17921. if (!ctx || !ctx->current || !ctx->current->layout)
  17922. return NK_WIDGET_INVALID;
  17923. win = ctx->current;
  17924. style = &ctx->style;
  17925. layout = win->layout;
  17926. state = nk_widget(bounds, ctx);
  17927. panel_padding = nk_panel_get_padding(style, layout->type);
  17928. if (layout->row.index == 1) {
  17929. bounds->w += panel_padding.x;
  17930. bounds->x -= panel_padding.x;
  17931. } else bounds->x -= item_padding.x;
  17932. if (layout->row.index == layout->row.columns)
  17933. bounds->w += panel_padding.x;
  17934. else bounds->w += item_padding.x;
  17935. return state;
  17936. }
  17937. NK_API void
  17938. nk_spacing(struct nk_context *ctx, int cols)
  17939. {
  17940. struct nk_window *win;
  17941. struct nk_panel *layout;
  17942. struct nk_rect none;
  17943. int i, index, rows;
  17944. NK_ASSERT(ctx);
  17945. NK_ASSERT(ctx->current);
  17946. NK_ASSERT(ctx->current->layout);
  17947. if (!ctx || !ctx->current || !ctx->current->layout)
  17948. return;
  17949. /* spacing over row boundaries */
  17950. win = ctx->current;
  17951. layout = win->layout;
  17952. index = (layout->row.index + cols) % layout->row.columns;
  17953. rows = (layout->row.index + cols) / layout->row.columns;
  17954. if (rows) {
  17955. for (i = 0; i < rows; ++i)
  17956. nk_panel_alloc_row(ctx, win);
  17957. cols = index;
  17958. }
  17959. /* non table layout need to allocate space */
  17960. if (layout->row.type != NK_LAYOUT_DYNAMIC_FIXED &&
  17961. layout->row.type != NK_LAYOUT_STATIC_FIXED) {
  17962. for (i = 0; i < cols; ++i)
  17963. nk_panel_alloc_space(&none, ctx);
  17964. } layout->row.index = index;
  17965. }
  17966. /* ===============================================================
  17967. *
  17968. * TEXT
  17969. *
  17970. * ===============================================================*/
  17971. NK_LIB void
  17972. nk_widget_text(struct nk_command_buffer *o, struct nk_rect b,
  17973. const char *string, int len, const struct nk_text *t,
  17974. nk_flags a, const struct nk_user_font *f)
  17975. {
  17976. struct nk_rect label;
  17977. float text_width;
  17978. NK_ASSERT(o);
  17979. NK_ASSERT(t);
  17980. if (!o || !t) return;
  17981. b.h = NK_MAX(b.h, 2 * t->padding.y);
  17982. label.x = 0; label.w = 0;
  17983. label.y = b.y + t->padding.y;
  17984. label.h = NK_MIN(f->height, b.h - 2 * t->padding.y);
  17985. text_width = f->width(f->userdata, f->height, (const char*)string, len);
  17986. text_width += (2.0f * t->padding.x);
  17987. /* align in x-axis */
  17988. if (a & NK_TEXT_ALIGN_LEFT) {
  17989. label.x = b.x + t->padding.x;
  17990. label.w = NK_MAX(0, b.w - 2 * t->padding.x);
  17991. } else if (a & NK_TEXT_ALIGN_CENTERED) {
  17992. label.w = NK_MAX(1, 2 * t->padding.x + (float)text_width);
  17993. label.x = (b.x + t->padding.x + ((b.w - 2 * t->padding.x) - label.w) / 2);
  17994. label.x = NK_MAX(b.x + t->padding.x, label.x);
  17995. label.w = NK_MIN(b.x + b.w, label.x + label.w);
  17996. if (label.w >= label.x) label.w -= label.x;
  17997. } else if (a & NK_TEXT_ALIGN_RIGHT) {
  17998. label.x = NK_MAX(b.x + t->padding.x, (b.x + b.w) - (2 * t->padding.x + (float)text_width));
  17999. label.w = (float)text_width + 2 * t->padding.x;
  18000. } else return;
  18001. /* align in y-axis */
  18002. if (a & NK_TEXT_ALIGN_MIDDLE) {
  18003. label.y = b.y + b.h/2.0f - (float)f->height/2.0f;
  18004. label.h = NK_MAX(b.h/2.0f, b.h - (b.h/2.0f + f->height/2.0f));
  18005. } else if (a & NK_TEXT_ALIGN_BOTTOM) {
  18006. label.y = b.y + b.h - f->height;
  18007. label.h = f->height;
  18008. }
  18009. nk_draw_text(o, label, (const char*)string, len, f, t->background, t->text);
  18010. }
  18011. NK_LIB void
  18012. nk_widget_text_wrap(struct nk_command_buffer *o, struct nk_rect b,
  18013. const char *string, int len, const struct nk_text *t,
  18014. const struct nk_user_font *f)
  18015. {
  18016. float width;
  18017. int glyphs = 0;
  18018. int fitting = 0;
  18019. int done = 0;
  18020. struct nk_rect line;
  18021. struct nk_text text;
  18022. NK_INTERN nk_rune seperator[] = {' '};
  18023. NK_ASSERT(o);
  18024. NK_ASSERT(t);
  18025. if (!o || !t) return;
  18026. text.padding = nk_vec2(0,0);
  18027. text.background = t->background;
  18028. text.text = t->text;
  18029. b.w = NK_MAX(b.w, 2 * t->padding.x);
  18030. b.h = NK_MAX(b.h, 2 * t->padding.y);
  18031. b.h = b.h - 2 * t->padding.y;
  18032. line.x = b.x + t->padding.x;
  18033. line.y = b.y + t->padding.y;
  18034. line.w = b.w - 2 * t->padding.x;
  18035. line.h = 2 * t->padding.y + f->height;
  18036. fitting = nk_text_clamp(f, string, len, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
  18037. while (done < len) {
  18038. if (!fitting || line.y + line.h >= (b.y + b.h)) break;
  18039. nk_widget_text(o, line, &string[done], fitting, &text, NK_TEXT_LEFT, f);
  18040. done += fitting;
  18041. line.y += f->height + 2 * t->padding.y;
  18042. fitting = nk_text_clamp(f, &string[done], len - done, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
  18043. }
  18044. }
  18045. NK_API void
  18046. nk_text_colored(struct nk_context *ctx, const char *str, int len,
  18047. nk_flags alignment, struct nk_color color)
  18048. {
  18049. struct nk_window *win;
  18050. const struct nk_style *style;
  18051. struct nk_vec2 item_padding;
  18052. struct nk_rect bounds;
  18053. struct nk_text text;
  18054. NK_ASSERT(ctx);
  18055. NK_ASSERT(ctx->current);
  18056. NK_ASSERT(ctx->current->layout);
  18057. if (!ctx || !ctx->current || !ctx->current->layout) return;
  18058. win = ctx->current;
  18059. style = &ctx->style;
  18060. nk_panel_alloc_space(&bounds, ctx);
  18061. item_padding = style->text.padding;
  18062. text.padding.x = item_padding.x;
  18063. text.padding.y = item_padding.y;
  18064. text.background = style->window.background;
  18065. text.text = color;
  18066. nk_widget_text(&win->buffer, bounds, str, len, &text, alignment, style->font);
  18067. }
  18068. NK_API void
  18069. nk_text_wrap_colored(struct nk_context *ctx, const char *str,
  18070. int len, struct nk_color color)
  18071. {
  18072. struct nk_window *win;
  18073. const struct nk_style *style;
  18074. struct nk_vec2 item_padding;
  18075. struct nk_rect bounds;
  18076. struct nk_text text;
  18077. NK_ASSERT(ctx);
  18078. NK_ASSERT(ctx->current);
  18079. NK_ASSERT(ctx->current->layout);
  18080. if (!ctx || !ctx->current || !ctx->current->layout) return;
  18081. win = ctx->current;
  18082. style = &ctx->style;
  18083. nk_panel_alloc_space(&bounds, ctx);
  18084. item_padding = style->text.padding;
  18085. text.padding.x = item_padding.x;
  18086. text.padding.y = item_padding.y;
  18087. text.background = style->window.background;
  18088. text.text = color;
  18089. nk_widget_text_wrap(&win->buffer, bounds, str, len, &text, style->font);
  18090. }
  18091. #ifdef NK_INCLUDE_STANDARD_VARARGS
  18092. NK_API void
  18093. nk_labelf_colored(struct nk_context *ctx, nk_flags flags,
  18094. struct nk_color color, const char *fmt, ...)
  18095. {
  18096. va_list args;
  18097. va_start(args, fmt);
  18098. nk_labelfv_colored(ctx, flags, color, fmt, args);
  18099. va_end(args);
  18100. }
  18101. NK_API void
  18102. nk_labelf_colored_wrap(struct nk_context *ctx, struct nk_color color,
  18103. const char *fmt, ...)
  18104. {
  18105. va_list args;
  18106. va_start(args, fmt);
  18107. nk_labelfv_colored_wrap(ctx, color, fmt, args);
  18108. va_end(args);
  18109. }
  18110. NK_API void
  18111. nk_labelf(struct nk_context *ctx, nk_flags flags, const char *fmt, ...)
  18112. {
  18113. va_list args;
  18114. va_start(args, fmt);
  18115. nk_labelfv(ctx, flags, fmt, args);
  18116. va_end(args);
  18117. }
  18118. NK_API void
  18119. nk_labelf_wrap(struct nk_context *ctx, const char *fmt,...)
  18120. {
  18121. va_list args;
  18122. va_start(args, fmt);
  18123. nk_labelfv_wrap(ctx, fmt, args);
  18124. va_end(args);
  18125. }
  18126. NK_API void
  18127. nk_labelfv_colored(struct nk_context *ctx, nk_flags flags,
  18128. struct nk_color color, const char *fmt, va_list args)
  18129. {
  18130. char buf[256];
  18131. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  18132. nk_label_colored(ctx, buf, flags, color);
  18133. }
  18134. NK_API void
  18135. nk_labelfv_colored_wrap(struct nk_context *ctx, struct nk_color color,
  18136. const char *fmt, va_list args)
  18137. {
  18138. char buf[256];
  18139. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  18140. nk_label_colored_wrap(ctx, buf, color);
  18141. }
  18142. NK_API void
  18143. nk_labelfv(struct nk_context *ctx, nk_flags flags, const char *fmt, va_list args)
  18144. {
  18145. char buf[256];
  18146. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  18147. nk_label(ctx, buf, flags);
  18148. }
  18149. NK_API void
  18150. nk_labelfv_wrap(struct nk_context *ctx, const char *fmt, va_list args)
  18151. {
  18152. char buf[256];
  18153. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  18154. nk_label_wrap(ctx, buf);
  18155. }
  18156. NK_API void
  18157. nk_value_bool(struct nk_context *ctx, const char *prefix, int value)
  18158. {
  18159. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, ((value) ? "true": "false"));
  18160. }
  18161. NK_API void
  18162. nk_value_int(struct nk_context *ctx, const char *prefix, int value)
  18163. {
  18164. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %d", prefix, value);
  18165. }
  18166. NK_API void
  18167. nk_value_uint(struct nk_context *ctx, const char *prefix, unsigned int value)
  18168. {
  18169. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %u", prefix, value);
  18170. }
  18171. NK_API void
  18172. nk_value_float(struct nk_context *ctx, const char *prefix, float value)
  18173. {
  18174. double double_value = (double)value;
  18175. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %.3f", prefix, double_value);
  18176. }
  18177. NK_API void
  18178. nk_value_color_byte(struct nk_context *ctx, const char *p, struct nk_color c)
  18179. {
  18180. nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%d, %d, %d, %d)", p, c.r, c.g, c.b, c.a);
  18181. }
  18182. NK_API void
  18183. nk_value_color_float(struct nk_context *ctx, const char *p, struct nk_color color)
  18184. {
  18185. double c[4]; nk_color_dv(c, color);
  18186. nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%.2f, %.2f, %.2f, %.2f)",
  18187. p, c[0], c[1], c[2], c[3]);
  18188. }
  18189. NK_API void
  18190. nk_value_color_hex(struct nk_context *ctx, const char *prefix, struct nk_color color)
  18191. {
  18192. char hex[16];
  18193. nk_color_hex_rgba(hex, color);
  18194. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, hex);
  18195. }
  18196. #endif
  18197. NK_API void
  18198. nk_text(struct nk_context *ctx, const char *str, int len, nk_flags alignment)
  18199. {
  18200. NK_ASSERT(ctx);
  18201. if (!ctx) return;
  18202. nk_text_colored(ctx, str, len, alignment, ctx->style.text.color);
  18203. }
  18204. NK_API void
  18205. nk_text_wrap(struct nk_context *ctx, const char *str, int len)
  18206. {
  18207. NK_ASSERT(ctx);
  18208. if (!ctx) return;
  18209. nk_text_wrap_colored(ctx, str, len, ctx->style.text.color);
  18210. }
  18211. NK_API void
  18212. nk_label(struct nk_context *ctx, const char *str, nk_flags alignment)
  18213. {
  18214. nk_text(ctx, str, nk_strlen(str), alignment);
  18215. }
  18216. NK_API void
  18217. nk_label_colored(struct nk_context *ctx, const char *str, nk_flags align,
  18218. struct nk_color color)
  18219. {
  18220. nk_text_colored(ctx, str, nk_strlen(str), align, color);
  18221. }
  18222. NK_API void
  18223. nk_label_wrap(struct nk_context *ctx, const char *str)
  18224. {
  18225. nk_text_wrap(ctx, str, nk_strlen(str));
  18226. }
  18227. NK_API void
  18228. nk_label_colored_wrap(struct nk_context *ctx, const char *str, struct nk_color color)
  18229. {
  18230. nk_text_wrap_colored(ctx, str, nk_strlen(str), color);
  18231. }
  18232. /* ===============================================================
  18233. *
  18234. * IMAGE
  18235. *
  18236. * ===============================================================*/
  18237. NK_API nk_handle
  18238. nk_handle_ptr(void *ptr)
  18239. {
  18240. nk_handle handle = {0};
  18241. handle.ptr = ptr;
  18242. return handle;
  18243. }
  18244. NK_API nk_handle
  18245. nk_handle_id(int id)
  18246. {
  18247. nk_handle handle;
  18248. nk_zero_struct(handle);
  18249. handle.id = id;
  18250. return handle;
  18251. }
  18252. NK_API struct nk_image
  18253. nk_subimage_ptr(void *ptr, unsigned short w, unsigned short h, struct nk_rect r)
  18254. {
  18255. struct nk_image s;
  18256. nk_zero(&s, sizeof(s));
  18257. s.handle.ptr = ptr;
  18258. s.w = w; s.h = h;
  18259. s.region[0] = (unsigned short)r.x;
  18260. s.region[1] = (unsigned short)r.y;
  18261. s.region[2] = (unsigned short)r.w;
  18262. s.region[3] = (unsigned short)r.h;
  18263. return s;
  18264. }
  18265. NK_API struct nk_image
  18266. nk_subimage_id(int id, unsigned short w, unsigned short h, struct nk_rect r)
  18267. {
  18268. struct nk_image s;
  18269. nk_zero(&s, sizeof(s));
  18270. s.handle.id = id;
  18271. s.w = w; s.h = h;
  18272. s.region[0] = (unsigned short)r.x;
  18273. s.region[1] = (unsigned short)r.y;
  18274. s.region[2] = (unsigned short)r.w;
  18275. s.region[3] = (unsigned short)r.h;
  18276. return s;
  18277. }
  18278. NK_API struct nk_image
  18279. nk_subimage_handle(nk_handle handle, unsigned short w, unsigned short h,
  18280. struct nk_rect r)
  18281. {
  18282. struct nk_image s;
  18283. nk_zero(&s, sizeof(s));
  18284. s.handle = handle;
  18285. s.w = w; s.h = h;
  18286. s.region[0] = (unsigned short)r.x;
  18287. s.region[1] = (unsigned short)r.y;
  18288. s.region[2] = (unsigned short)r.w;
  18289. s.region[3] = (unsigned short)r.h;
  18290. return s;
  18291. }
  18292. NK_API struct nk_image
  18293. nk_image_handle(nk_handle handle)
  18294. {
  18295. struct nk_image s;
  18296. nk_zero(&s, sizeof(s));
  18297. s.handle = handle;
  18298. s.w = 0; s.h = 0;
  18299. s.region[0] = 0;
  18300. s.region[1] = 0;
  18301. s.region[2] = 0;
  18302. s.region[3] = 0;
  18303. return s;
  18304. }
  18305. NK_API struct nk_image
  18306. nk_image_ptr(void *ptr)
  18307. {
  18308. struct nk_image s;
  18309. nk_zero(&s, sizeof(s));
  18310. NK_ASSERT(ptr);
  18311. s.handle.ptr = ptr;
  18312. s.w = 0; s.h = 0;
  18313. s.region[0] = 0;
  18314. s.region[1] = 0;
  18315. s.region[2] = 0;
  18316. s.region[3] = 0;
  18317. return s;
  18318. }
  18319. NK_API struct nk_image
  18320. nk_image_id(int id)
  18321. {
  18322. struct nk_image s;
  18323. nk_zero(&s, sizeof(s));
  18324. s.handle.id = id;
  18325. s.w = 0; s.h = 0;
  18326. s.region[0] = 0;
  18327. s.region[1] = 0;
  18328. s.region[2] = 0;
  18329. s.region[3] = 0;
  18330. return s;
  18331. }
  18332. NK_API int
  18333. nk_image_is_subimage(const struct nk_image* img)
  18334. {
  18335. NK_ASSERT(img);
  18336. return !(img->w == 0 && img->h == 0);
  18337. }
  18338. NK_API void
  18339. nk_image(struct nk_context *ctx, struct nk_image img)
  18340. {
  18341. struct nk_window *win;
  18342. struct nk_rect bounds;
  18343. NK_ASSERT(ctx);
  18344. NK_ASSERT(ctx->current);
  18345. NK_ASSERT(ctx->current->layout);
  18346. if (!ctx || !ctx->current || !ctx->current->layout) return;
  18347. win = ctx->current;
  18348. if (!nk_widget(&bounds, ctx)) return;
  18349. nk_draw_image(&win->buffer, bounds, &img, nk_white);
  18350. }
  18351. NK_API void
  18352. nk_image_color(struct nk_context *ctx, struct nk_image img, struct nk_color col)
  18353. {
  18354. struct nk_window *win;
  18355. struct nk_rect bounds;
  18356. NK_ASSERT(ctx);
  18357. NK_ASSERT(ctx->current);
  18358. NK_ASSERT(ctx->current->layout);
  18359. if (!ctx || !ctx->current || !ctx->current->layout) return;
  18360. win = ctx->current;
  18361. if (!nk_widget(&bounds, ctx)) return;
  18362. nk_draw_image(&win->buffer, bounds, &img, col);
  18363. }
  18364. /* ==============================================================
  18365. *
  18366. * BUTTON
  18367. *
  18368. * ===============================================================*/
  18369. NK_LIB void
  18370. nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type,
  18371. struct nk_rect content, struct nk_color background, struct nk_color foreground,
  18372. float border_width, const struct nk_user_font *font)
  18373. {
  18374. switch (type) {
  18375. case NK_SYMBOL_X:
  18376. case NK_SYMBOL_UNDERSCORE:
  18377. case NK_SYMBOL_PLUS:
  18378. case NK_SYMBOL_MINUS: {
  18379. /* single character text symbol */
  18380. const char *X = (type == NK_SYMBOL_X) ? "x":
  18381. (type == NK_SYMBOL_UNDERSCORE) ? "_":
  18382. (type == NK_SYMBOL_PLUS) ? "+": "-";
  18383. struct nk_text text;
  18384. text.padding = nk_vec2(0,0);
  18385. text.background = background;
  18386. text.text = foreground;
  18387. nk_widget_text(out, content, X, 1, &text, NK_TEXT_CENTERED, font);
  18388. } break;
  18389. case NK_SYMBOL_CIRCLE_SOLID:
  18390. case NK_SYMBOL_CIRCLE_OUTLINE:
  18391. case NK_SYMBOL_RECT_SOLID:
  18392. case NK_SYMBOL_RECT_OUTLINE: {
  18393. /* simple empty/filled shapes */
  18394. if (type == NK_SYMBOL_RECT_SOLID || type == NK_SYMBOL_RECT_OUTLINE) {
  18395. nk_fill_rect(out, content, 0, foreground);
  18396. if (type == NK_SYMBOL_RECT_OUTLINE)
  18397. nk_fill_rect(out, nk_shrink_rect(content, border_width), 0, background);
  18398. } else {
  18399. nk_fill_circle(out, content, foreground);
  18400. if (type == NK_SYMBOL_CIRCLE_OUTLINE)
  18401. nk_fill_circle(out, nk_shrink_rect(content, 1), background);
  18402. }
  18403. } break;
  18404. case NK_SYMBOL_TRIANGLE_UP:
  18405. case NK_SYMBOL_TRIANGLE_DOWN:
  18406. case NK_SYMBOL_TRIANGLE_LEFT:
  18407. case NK_SYMBOL_TRIANGLE_RIGHT: {
  18408. enum nk_heading heading;
  18409. struct nk_vec2 points[3];
  18410. heading = (type == NK_SYMBOL_TRIANGLE_RIGHT) ? NK_RIGHT :
  18411. (type == NK_SYMBOL_TRIANGLE_LEFT) ? NK_LEFT:
  18412. (type == NK_SYMBOL_TRIANGLE_UP) ? NK_UP: NK_DOWN;
  18413. nk_triangle_from_direction(points, content, 0, 0, heading);
  18414. nk_fill_triangle(out, points[0].x, points[0].y, points[1].x, points[1].y,
  18415. points[2].x, points[2].y, foreground);
  18416. } break;
  18417. default:
  18418. case NK_SYMBOL_NONE:
  18419. case NK_SYMBOL_MAX: break;
  18420. }
  18421. }
  18422. NK_LIB int
  18423. nk_button_behavior(nk_flags *state, struct nk_rect r,
  18424. const struct nk_input *i, enum nk_button_behavior behavior)
  18425. {
  18426. int ret = 0;
  18427. nk_widget_state_reset(state);
  18428. if (!i) return 0;
  18429. if (nk_input_is_mouse_hovering_rect(i, r)) {
  18430. *state = NK_WIDGET_STATE_HOVERED;
  18431. if (nk_input_is_mouse_down(i, NK_BUTTON_LEFT))
  18432. *state = NK_WIDGET_STATE_ACTIVE;
  18433. if (nk_input_has_mouse_click_in_rect(i, NK_BUTTON_LEFT, r)) {
  18434. ret = (behavior != NK_BUTTON_DEFAULT) ?
  18435. nk_input_is_mouse_down(i, NK_BUTTON_LEFT):
  18436. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  18437. nk_input_is_mouse_released(i, NK_BUTTON_LEFT);
  18438. #else
  18439. nk_input_is_mouse_pressed(i, NK_BUTTON_LEFT);
  18440. #endif
  18441. }
  18442. }
  18443. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(i, r))
  18444. *state |= NK_WIDGET_STATE_ENTERED;
  18445. else if (nk_input_is_mouse_prev_hovering_rect(i, r))
  18446. *state |= NK_WIDGET_STATE_LEFT;
  18447. return ret;
  18448. }
  18449. NK_LIB const struct nk_style_item*
  18450. nk_draw_button(struct nk_command_buffer *out,
  18451. const struct nk_rect *bounds, nk_flags state,
  18452. const struct nk_style_button *style)
  18453. {
  18454. const struct nk_style_item *background;
  18455. if (state & NK_WIDGET_STATE_HOVER)
  18456. background = &style->hover;
  18457. else if (state & NK_WIDGET_STATE_ACTIVED)
  18458. background = &style->active;
  18459. else background = &style->normal;
  18460. if (background->type == NK_STYLE_ITEM_IMAGE) {
  18461. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  18462. } else {
  18463. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  18464. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  18465. }
  18466. return background;
  18467. }
  18468. NK_LIB int
  18469. nk_do_button(nk_flags *state, struct nk_command_buffer *out, struct nk_rect r,
  18470. const struct nk_style_button *style, const struct nk_input *in,
  18471. enum nk_button_behavior behavior, struct nk_rect *content)
  18472. {
  18473. struct nk_rect bounds;
  18474. NK_ASSERT(style);
  18475. NK_ASSERT(state);
  18476. NK_ASSERT(out);
  18477. if (!out || !style)
  18478. return nk_false;
  18479. /* calculate button content space */
  18480. content->x = r.x + style->padding.x + style->border + style->rounding;
  18481. content->y = r.y + style->padding.y + style->border + style->rounding;
  18482. content->w = r.w - (2 * style->padding.x + style->border + style->rounding*2);
  18483. content->h = r.h - (2 * style->padding.y + style->border + style->rounding*2);
  18484. /* execute button behavior */
  18485. bounds.x = r.x - style->touch_padding.x;
  18486. bounds.y = r.y - style->touch_padding.y;
  18487. bounds.w = r.w + 2 * style->touch_padding.x;
  18488. bounds.h = r.h + 2 * style->touch_padding.y;
  18489. return nk_button_behavior(state, bounds, in, behavior);
  18490. }
  18491. NK_LIB void
  18492. nk_draw_button_text(struct nk_command_buffer *out,
  18493. const struct nk_rect *bounds, const struct nk_rect *content, nk_flags state,
  18494. const struct nk_style_button *style, const char *txt, int len,
  18495. nk_flags text_alignment, const struct nk_user_font *font)
  18496. {
  18497. struct nk_text text;
  18498. const struct nk_style_item *background;
  18499. background = nk_draw_button(out, bounds, state, style);
  18500. /* select correct colors/images */
  18501. if (background->type == NK_STYLE_ITEM_COLOR)
  18502. text.background = background->data.color;
  18503. else text.background = style->text_background;
  18504. if (state & NK_WIDGET_STATE_HOVER)
  18505. text.text = style->text_hover;
  18506. else if (state & NK_WIDGET_STATE_ACTIVED)
  18507. text.text = style->text_active;
  18508. else text.text = style->text_normal;
  18509. text.padding = nk_vec2(0,0);
  18510. nk_widget_text(out, *content, txt, len, &text, text_alignment, font);
  18511. }
  18512. NK_LIB int
  18513. nk_do_button_text(nk_flags *state,
  18514. struct nk_command_buffer *out, struct nk_rect bounds,
  18515. const char *string, int len, nk_flags align, enum nk_button_behavior behavior,
  18516. const struct nk_style_button *style, const struct nk_input *in,
  18517. const struct nk_user_font *font)
  18518. {
  18519. struct nk_rect content;
  18520. int ret = nk_false;
  18521. NK_ASSERT(state);
  18522. NK_ASSERT(style);
  18523. NK_ASSERT(out);
  18524. NK_ASSERT(string);
  18525. NK_ASSERT(font);
  18526. if (!out || !style || !font || !string)
  18527. return nk_false;
  18528. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  18529. if (style->draw_begin) style->draw_begin(out, style->userdata);
  18530. nk_draw_button_text(out, &bounds, &content, *state, style, string, len, align, font);
  18531. if (style->draw_end) style->draw_end(out, style->userdata);
  18532. return ret;
  18533. }
  18534. NK_LIB void
  18535. nk_draw_button_symbol(struct nk_command_buffer *out,
  18536. const struct nk_rect *bounds, const struct nk_rect *content,
  18537. nk_flags state, const struct nk_style_button *style,
  18538. enum nk_symbol_type type, const struct nk_user_font *font)
  18539. {
  18540. struct nk_color sym, bg;
  18541. const struct nk_style_item *background;
  18542. /* select correct colors/images */
  18543. background = nk_draw_button(out, bounds, state, style);
  18544. if (background->type == NK_STYLE_ITEM_COLOR)
  18545. bg = background->data.color;
  18546. else bg = style->text_background;
  18547. if (state & NK_WIDGET_STATE_HOVER)
  18548. sym = style->text_hover;
  18549. else if (state & NK_WIDGET_STATE_ACTIVED)
  18550. sym = style->text_active;
  18551. else sym = style->text_normal;
  18552. nk_draw_symbol(out, type, *content, bg, sym, 1, font);
  18553. }
  18554. NK_LIB int
  18555. nk_do_button_symbol(nk_flags *state,
  18556. struct nk_command_buffer *out, struct nk_rect bounds,
  18557. enum nk_symbol_type symbol, enum nk_button_behavior behavior,
  18558. const struct nk_style_button *style, const struct nk_input *in,
  18559. const struct nk_user_font *font)
  18560. {
  18561. int ret;
  18562. struct nk_rect content;
  18563. NK_ASSERT(state);
  18564. NK_ASSERT(style);
  18565. NK_ASSERT(font);
  18566. NK_ASSERT(out);
  18567. if (!out || !style || !font || !state)
  18568. return nk_false;
  18569. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  18570. if (style->draw_begin) style->draw_begin(out, style->userdata);
  18571. nk_draw_button_symbol(out, &bounds, &content, *state, style, symbol, font);
  18572. if (style->draw_end) style->draw_end(out, style->userdata);
  18573. return ret;
  18574. }
  18575. NK_LIB void
  18576. nk_draw_button_image(struct nk_command_buffer *out,
  18577. const struct nk_rect *bounds, const struct nk_rect *content,
  18578. nk_flags state, const struct nk_style_button *style, const struct nk_image *img)
  18579. {
  18580. nk_draw_button(out, bounds, state, style);
  18581. nk_draw_image(out, *content, img, nk_white);
  18582. }
  18583. NK_LIB int
  18584. nk_do_button_image(nk_flags *state,
  18585. struct nk_command_buffer *out, struct nk_rect bounds,
  18586. struct nk_image img, enum nk_button_behavior b,
  18587. const struct nk_style_button *style, const struct nk_input *in)
  18588. {
  18589. int ret;
  18590. struct nk_rect content;
  18591. NK_ASSERT(state);
  18592. NK_ASSERT(style);
  18593. NK_ASSERT(out);
  18594. if (!out || !style || !state)
  18595. return nk_false;
  18596. ret = nk_do_button(state, out, bounds, style, in, b, &content);
  18597. content.x += style->image_padding.x;
  18598. content.y += style->image_padding.y;
  18599. content.w -= 2 * style->image_padding.x;
  18600. content.h -= 2 * style->image_padding.y;
  18601. if (style->draw_begin) style->draw_begin(out, style->userdata);
  18602. nk_draw_button_image(out, &bounds, &content, *state, style, &img);
  18603. if (style->draw_end) style->draw_end(out, style->userdata);
  18604. return ret;
  18605. }
  18606. NK_LIB void
  18607. nk_draw_button_text_symbol(struct nk_command_buffer *out,
  18608. const struct nk_rect *bounds, const struct nk_rect *label,
  18609. const struct nk_rect *symbol, nk_flags state, const struct nk_style_button *style,
  18610. const char *str, int len, enum nk_symbol_type type,
  18611. const struct nk_user_font *font)
  18612. {
  18613. struct nk_color sym;
  18614. struct nk_text text;
  18615. const struct nk_style_item *background;
  18616. /* select correct background colors/images */
  18617. background = nk_draw_button(out, bounds, state, style);
  18618. if (background->type == NK_STYLE_ITEM_COLOR)
  18619. text.background = background->data.color;
  18620. else text.background = style->text_background;
  18621. /* select correct text colors */
  18622. if (state & NK_WIDGET_STATE_HOVER) {
  18623. sym = style->text_hover;
  18624. text.text = style->text_hover;
  18625. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  18626. sym = style->text_active;
  18627. text.text = style->text_active;
  18628. } else {
  18629. sym = style->text_normal;
  18630. text.text = style->text_normal;
  18631. }
  18632. text.padding = nk_vec2(0,0);
  18633. nk_draw_symbol(out, type, *symbol, style->text_background, sym, 0, font);
  18634. nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
  18635. }
  18636. NK_LIB int
  18637. nk_do_button_text_symbol(nk_flags *state,
  18638. struct nk_command_buffer *out, struct nk_rect bounds,
  18639. enum nk_symbol_type symbol, const char *str, int len, nk_flags align,
  18640. enum nk_button_behavior behavior, const struct nk_style_button *style,
  18641. const struct nk_user_font *font, const struct nk_input *in)
  18642. {
  18643. int ret;
  18644. struct nk_rect tri = {0,0,0,0};
  18645. struct nk_rect content;
  18646. NK_ASSERT(style);
  18647. NK_ASSERT(out);
  18648. NK_ASSERT(font);
  18649. if (!out || !style || !font)
  18650. return nk_false;
  18651. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  18652. tri.y = content.y + (content.h/2) - font->height/2;
  18653. tri.w = font->height; tri.h = font->height;
  18654. if (align & NK_TEXT_ALIGN_LEFT) {
  18655. tri.x = (content.x + content.w) - (2 * style->padding.x + tri.w);
  18656. tri.x = NK_MAX(tri.x, 0);
  18657. } else tri.x = content.x + 2 * style->padding.x;
  18658. /* draw button */
  18659. if (style->draw_begin) style->draw_begin(out, style->userdata);
  18660. nk_draw_button_text_symbol(out, &bounds, &content, &tri,
  18661. *state, style, str, len, symbol, font);
  18662. if (style->draw_end) style->draw_end(out, style->userdata);
  18663. return ret;
  18664. }
  18665. NK_LIB void
  18666. nk_draw_button_text_image(struct nk_command_buffer *out,
  18667. const struct nk_rect *bounds, const struct nk_rect *label,
  18668. const struct nk_rect *image, nk_flags state, const struct nk_style_button *style,
  18669. const char *str, int len, const struct nk_user_font *font,
  18670. const struct nk_image *img)
  18671. {
  18672. struct nk_text text;
  18673. const struct nk_style_item *background;
  18674. background = nk_draw_button(out, bounds, state, style);
  18675. /* select correct colors */
  18676. if (background->type == NK_STYLE_ITEM_COLOR)
  18677. text.background = background->data.color;
  18678. else text.background = style->text_background;
  18679. if (state & NK_WIDGET_STATE_HOVER)
  18680. text.text = style->text_hover;
  18681. else if (state & NK_WIDGET_STATE_ACTIVED)
  18682. text.text = style->text_active;
  18683. else text.text = style->text_normal;
  18684. text.padding = nk_vec2(0,0);
  18685. nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
  18686. nk_draw_image(out, *image, img, nk_white);
  18687. }
  18688. NK_LIB int
  18689. nk_do_button_text_image(nk_flags *state,
  18690. struct nk_command_buffer *out, struct nk_rect bounds,
  18691. struct nk_image img, const char* str, int len, nk_flags align,
  18692. enum nk_button_behavior behavior, const struct nk_style_button *style,
  18693. const struct nk_user_font *font, const struct nk_input *in)
  18694. {
  18695. int ret;
  18696. struct nk_rect icon;
  18697. struct nk_rect content;
  18698. NK_ASSERT(style);
  18699. NK_ASSERT(state);
  18700. NK_ASSERT(font);
  18701. NK_ASSERT(out);
  18702. if (!out || !font || !style || !str)
  18703. return nk_false;
  18704. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  18705. icon.y = bounds.y + style->padding.y;
  18706. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  18707. if (align & NK_TEXT_ALIGN_LEFT) {
  18708. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  18709. icon.x = NK_MAX(icon.x, 0);
  18710. } else icon.x = bounds.x + 2 * style->padding.x;
  18711. icon.x += style->image_padding.x;
  18712. icon.y += style->image_padding.y;
  18713. icon.w -= 2 * style->image_padding.x;
  18714. icon.h -= 2 * style->image_padding.y;
  18715. if (style->draw_begin) style->draw_begin(out, style->userdata);
  18716. nk_draw_button_text_image(out, &bounds, &content, &icon, *state, style, str, len, font, &img);
  18717. if (style->draw_end) style->draw_end(out, style->userdata);
  18718. return ret;
  18719. }
  18720. NK_API void
  18721. nk_button_set_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
  18722. {
  18723. NK_ASSERT(ctx);
  18724. if (!ctx) return;
  18725. ctx->button_behavior = behavior;
  18726. }
  18727. NK_API int
  18728. nk_button_push_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
  18729. {
  18730. struct nk_config_stack_button_behavior *button_stack;
  18731. struct nk_config_stack_button_behavior_element *element;
  18732. NK_ASSERT(ctx);
  18733. if (!ctx) return 0;
  18734. button_stack = &ctx->stacks.button_behaviors;
  18735. NK_ASSERT(button_stack->head < (int)NK_LEN(button_stack->elements));
  18736. if (button_stack->head >= (int)NK_LEN(button_stack->elements))
  18737. return 0;
  18738. element = &button_stack->elements[button_stack->head++];
  18739. element->address = &ctx->button_behavior;
  18740. element->old_value = ctx->button_behavior;
  18741. ctx->button_behavior = behavior;
  18742. return 1;
  18743. }
  18744. NK_API int
  18745. nk_button_pop_behavior(struct nk_context *ctx)
  18746. {
  18747. struct nk_config_stack_button_behavior *button_stack;
  18748. struct nk_config_stack_button_behavior_element *element;
  18749. NK_ASSERT(ctx);
  18750. if (!ctx) return 0;
  18751. button_stack = &ctx->stacks.button_behaviors;
  18752. NK_ASSERT(button_stack->head > 0);
  18753. if (button_stack->head < 1)
  18754. return 0;
  18755. element = &button_stack->elements[--button_stack->head];
  18756. *element->address = element->old_value;
  18757. return 1;
  18758. }
  18759. NK_API int
  18760. nk_button_text_styled(struct nk_context *ctx,
  18761. const struct nk_style_button *style, const char *title, int len)
  18762. {
  18763. struct nk_window *win;
  18764. struct nk_panel *layout;
  18765. const struct nk_input *in;
  18766. struct nk_rect bounds;
  18767. enum nk_widget_layout_states state;
  18768. NK_ASSERT(ctx);
  18769. NK_ASSERT(style);
  18770. NK_ASSERT(ctx->current);
  18771. NK_ASSERT(ctx->current->layout);
  18772. if (!style || !ctx || !ctx->current || !ctx->current->layout) return 0;
  18773. win = ctx->current;
  18774. layout = win->layout;
  18775. state = nk_widget(&bounds, ctx);
  18776. if (!state) return 0;
  18777. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18778. return nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
  18779. title, len, style->text_alignment, ctx->button_behavior,
  18780. style, in, ctx->style.font);
  18781. }
  18782. NK_API int
  18783. nk_button_text(struct nk_context *ctx, const char *title, int len)
  18784. {
  18785. NK_ASSERT(ctx);
  18786. if (!ctx) return 0;
  18787. return nk_button_text_styled(ctx, &ctx->style.button, title, len);
  18788. }
  18789. NK_API int nk_button_label_styled(struct nk_context *ctx,
  18790. const struct nk_style_button *style, const char *title)
  18791. {
  18792. return nk_button_text_styled(ctx, style, title, nk_strlen(title));
  18793. }
  18794. NK_API int nk_button_label(struct nk_context *ctx, const char *title)
  18795. {
  18796. return nk_button_text(ctx, title, nk_strlen(title));
  18797. }
  18798. NK_API int
  18799. nk_button_color(struct nk_context *ctx, struct nk_color color)
  18800. {
  18801. struct nk_window *win;
  18802. struct nk_panel *layout;
  18803. const struct nk_input *in;
  18804. struct nk_style_button button;
  18805. int ret = 0;
  18806. struct nk_rect bounds;
  18807. struct nk_rect content;
  18808. enum nk_widget_layout_states state;
  18809. NK_ASSERT(ctx);
  18810. NK_ASSERT(ctx->current);
  18811. NK_ASSERT(ctx->current->layout);
  18812. if (!ctx || !ctx->current || !ctx->current->layout)
  18813. return 0;
  18814. win = ctx->current;
  18815. layout = win->layout;
  18816. state = nk_widget(&bounds, ctx);
  18817. if (!state) return 0;
  18818. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18819. button = ctx->style.button;
  18820. button.normal = nk_style_item_color(color);
  18821. button.hover = nk_style_item_color(color);
  18822. button.active = nk_style_item_color(color);
  18823. ret = nk_do_button(&ctx->last_widget_state, &win->buffer, bounds,
  18824. &button, in, ctx->button_behavior, &content);
  18825. nk_draw_button(&win->buffer, &bounds, ctx->last_widget_state, &button);
  18826. return ret;
  18827. }
  18828. NK_API int
  18829. nk_button_symbol_styled(struct nk_context *ctx,
  18830. const struct nk_style_button *style, enum nk_symbol_type symbol)
  18831. {
  18832. struct nk_window *win;
  18833. struct nk_panel *layout;
  18834. const struct nk_input *in;
  18835. struct nk_rect bounds;
  18836. enum nk_widget_layout_states state;
  18837. NK_ASSERT(ctx);
  18838. NK_ASSERT(ctx->current);
  18839. NK_ASSERT(ctx->current->layout);
  18840. if (!ctx || !ctx->current || !ctx->current->layout)
  18841. return 0;
  18842. win = ctx->current;
  18843. layout = win->layout;
  18844. state = nk_widget(&bounds, ctx);
  18845. if (!state) return 0;
  18846. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18847. return nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  18848. symbol, ctx->button_behavior, style, in, ctx->style.font);
  18849. }
  18850. NK_API int
  18851. nk_button_symbol(struct nk_context *ctx, enum nk_symbol_type symbol)
  18852. {
  18853. NK_ASSERT(ctx);
  18854. if (!ctx) return 0;
  18855. return nk_button_symbol_styled(ctx, &ctx->style.button, symbol);
  18856. }
  18857. NK_API int
  18858. nk_button_image_styled(struct nk_context *ctx, const struct nk_style_button *style,
  18859. struct nk_image img)
  18860. {
  18861. struct nk_window *win;
  18862. struct nk_panel *layout;
  18863. const struct nk_input *in;
  18864. struct nk_rect bounds;
  18865. enum nk_widget_layout_states state;
  18866. NK_ASSERT(ctx);
  18867. NK_ASSERT(ctx->current);
  18868. NK_ASSERT(ctx->current->layout);
  18869. if (!ctx || !ctx->current || !ctx->current->layout)
  18870. return 0;
  18871. win = ctx->current;
  18872. layout = win->layout;
  18873. state = nk_widget(&bounds, ctx);
  18874. if (!state) return 0;
  18875. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18876. return nk_do_button_image(&ctx->last_widget_state, &win->buffer, bounds,
  18877. img, ctx->button_behavior, style, in);
  18878. }
  18879. NK_API int
  18880. nk_button_image(struct nk_context *ctx, struct nk_image img)
  18881. {
  18882. NK_ASSERT(ctx);
  18883. if (!ctx) return 0;
  18884. return nk_button_image_styled(ctx, &ctx->style.button, img);
  18885. }
  18886. NK_API int
  18887. nk_button_symbol_text_styled(struct nk_context *ctx,
  18888. const struct nk_style_button *style, enum nk_symbol_type symbol,
  18889. const char *text, int len, nk_flags align)
  18890. {
  18891. struct nk_window *win;
  18892. struct nk_panel *layout;
  18893. const struct nk_input *in;
  18894. struct nk_rect bounds;
  18895. enum nk_widget_layout_states state;
  18896. NK_ASSERT(ctx);
  18897. NK_ASSERT(ctx->current);
  18898. NK_ASSERT(ctx->current->layout);
  18899. if (!ctx || !ctx->current || !ctx->current->layout)
  18900. return 0;
  18901. win = ctx->current;
  18902. layout = win->layout;
  18903. state = nk_widget(&bounds, ctx);
  18904. if (!state) return 0;
  18905. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18906. return nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  18907. symbol, text, len, align, ctx->button_behavior,
  18908. style, ctx->style.font, in);
  18909. }
  18910. NK_API int
  18911. nk_button_symbol_text(struct nk_context *ctx, enum nk_symbol_type symbol,
  18912. const char* text, int len, nk_flags align)
  18913. {
  18914. NK_ASSERT(ctx);
  18915. if (!ctx) return 0;
  18916. return nk_button_symbol_text_styled(ctx, &ctx->style.button, symbol, text, len, align);
  18917. }
  18918. NK_API int nk_button_symbol_label(struct nk_context *ctx, enum nk_symbol_type symbol,
  18919. const char *label, nk_flags align)
  18920. {
  18921. return nk_button_symbol_text(ctx, symbol, label, nk_strlen(label), align);
  18922. }
  18923. NK_API int nk_button_symbol_label_styled(struct nk_context *ctx,
  18924. const struct nk_style_button *style, enum nk_symbol_type symbol,
  18925. const char *title, nk_flags align)
  18926. {
  18927. return nk_button_symbol_text_styled(ctx, style, symbol, title, nk_strlen(title), align);
  18928. }
  18929. NK_API int
  18930. nk_button_image_text_styled(struct nk_context *ctx,
  18931. const struct nk_style_button *style, struct nk_image img, const char *text,
  18932. int len, nk_flags align)
  18933. {
  18934. struct nk_window *win;
  18935. struct nk_panel *layout;
  18936. const struct nk_input *in;
  18937. struct nk_rect bounds;
  18938. enum nk_widget_layout_states state;
  18939. NK_ASSERT(ctx);
  18940. NK_ASSERT(ctx->current);
  18941. NK_ASSERT(ctx->current->layout);
  18942. if (!ctx || !ctx->current || !ctx->current->layout)
  18943. return 0;
  18944. win = ctx->current;
  18945. layout = win->layout;
  18946. state = nk_widget(&bounds, ctx);
  18947. if (!state) return 0;
  18948. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  18949. return nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
  18950. bounds, img, text, len, align, ctx->button_behavior,
  18951. style, ctx->style.font, in);
  18952. }
  18953. NK_API int
  18954. nk_button_image_text(struct nk_context *ctx, struct nk_image img,
  18955. const char *text, int len, nk_flags align)
  18956. {
  18957. return nk_button_image_text_styled(ctx, &ctx->style.button,img, text, len, align);
  18958. }
  18959. NK_API int nk_button_image_label(struct nk_context *ctx, struct nk_image img,
  18960. const char *label, nk_flags align)
  18961. {
  18962. return nk_button_image_text(ctx, img, label, nk_strlen(label), align);
  18963. }
  18964. NK_API int nk_button_image_label_styled(struct nk_context *ctx,
  18965. const struct nk_style_button *style, struct nk_image img,
  18966. const char *label, nk_flags text_alignment)
  18967. {
  18968. return nk_button_image_text_styled(ctx, style, img, label, nk_strlen(label), text_alignment);
  18969. }
  18970. /* ===============================================================
  18971. *
  18972. * TOGGLE
  18973. *
  18974. * ===============================================================*/
  18975. NK_LIB int
  18976. nk_toggle_behavior(const struct nk_input *in, struct nk_rect select,
  18977. nk_flags *state, int active)
  18978. {
  18979. nk_widget_state_reset(state);
  18980. if (nk_button_behavior(state, select, in, NK_BUTTON_DEFAULT)) {
  18981. *state = NK_WIDGET_STATE_ACTIVE;
  18982. active = !active;
  18983. }
  18984. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, select))
  18985. *state |= NK_WIDGET_STATE_ENTERED;
  18986. else if (nk_input_is_mouse_prev_hovering_rect(in, select))
  18987. *state |= NK_WIDGET_STATE_LEFT;
  18988. return active;
  18989. }
  18990. NK_LIB void
  18991. nk_draw_checkbox(struct nk_command_buffer *out,
  18992. nk_flags state, const struct nk_style_toggle *style, int active,
  18993. const struct nk_rect *label, const struct nk_rect *selector,
  18994. const struct nk_rect *cursors, const char *string, int len,
  18995. const struct nk_user_font *font)
  18996. {
  18997. const struct nk_style_item *background;
  18998. const struct nk_style_item *cursor;
  18999. struct nk_text text;
  19000. /* select correct colors/images */
  19001. if (state & NK_WIDGET_STATE_HOVER) {
  19002. background = &style->hover;
  19003. cursor = &style->cursor_hover;
  19004. text.text = style->text_hover;
  19005. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  19006. background = &style->hover;
  19007. cursor = &style->cursor_hover;
  19008. text.text = style->text_active;
  19009. } else {
  19010. background = &style->normal;
  19011. cursor = &style->cursor_normal;
  19012. text.text = style->text_normal;
  19013. }
  19014. /* draw background and cursor */
  19015. if (background->type == NK_STYLE_ITEM_COLOR) {
  19016. nk_fill_rect(out, *selector, 0, style->border_color);
  19017. nk_fill_rect(out, nk_shrink_rect(*selector, style->border), 0, background->data.color);
  19018. } else nk_draw_image(out, *selector, &background->data.image, nk_white);
  19019. if (active) {
  19020. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  19021. nk_draw_image(out, *cursors, &cursor->data.image, nk_white);
  19022. else nk_fill_rect(out, *cursors, 0, cursor->data.color);
  19023. }
  19024. text.padding.x = 0;
  19025. text.padding.y = 0;
  19026. text.background = style->text_background;
  19027. nk_widget_text(out, *label, string, len, &text, NK_TEXT_LEFT, font);
  19028. }
  19029. NK_LIB void
  19030. nk_draw_option(struct nk_command_buffer *out,
  19031. nk_flags state, const struct nk_style_toggle *style, int active,
  19032. const struct nk_rect *label, const struct nk_rect *selector,
  19033. const struct nk_rect *cursors, const char *string, int len,
  19034. const struct nk_user_font *font)
  19035. {
  19036. const struct nk_style_item *background;
  19037. const struct nk_style_item *cursor;
  19038. struct nk_text text;
  19039. /* select correct colors/images */
  19040. if (state & NK_WIDGET_STATE_HOVER) {
  19041. background = &style->hover;
  19042. cursor = &style->cursor_hover;
  19043. text.text = style->text_hover;
  19044. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  19045. background = &style->hover;
  19046. cursor = &style->cursor_hover;
  19047. text.text = style->text_active;
  19048. } else {
  19049. background = &style->normal;
  19050. cursor = &style->cursor_normal;
  19051. text.text = style->text_normal;
  19052. }
  19053. /* draw background and cursor */
  19054. if (background->type == NK_STYLE_ITEM_COLOR) {
  19055. nk_fill_circle(out, *selector, style->border_color);
  19056. nk_fill_circle(out, nk_shrink_rect(*selector, style->border), background->data.color);
  19057. } else nk_draw_image(out, *selector, &background->data.image, nk_white);
  19058. if (active) {
  19059. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  19060. nk_draw_image(out, *cursors, &cursor->data.image, nk_white);
  19061. else nk_fill_circle(out, *cursors, cursor->data.color);
  19062. }
  19063. text.padding.x = 0;
  19064. text.padding.y = 0;
  19065. text.background = style->text_background;
  19066. nk_widget_text(out, *label, string, len, &text, NK_TEXT_LEFT, font);
  19067. }
  19068. NK_LIB int
  19069. nk_do_toggle(nk_flags *state,
  19070. struct nk_command_buffer *out, struct nk_rect r,
  19071. int *active, const char *str, int len, enum nk_toggle_type type,
  19072. const struct nk_style_toggle *style, const struct nk_input *in,
  19073. const struct nk_user_font *font)
  19074. {
  19075. int was_active;
  19076. struct nk_rect bounds;
  19077. struct nk_rect select;
  19078. struct nk_rect cursor;
  19079. struct nk_rect label;
  19080. NK_ASSERT(style);
  19081. NK_ASSERT(out);
  19082. NK_ASSERT(font);
  19083. if (!out || !style || !font || !active)
  19084. return 0;
  19085. r.w = NK_MAX(r.w, font->height + 2 * style->padding.x);
  19086. r.h = NK_MAX(r.h, font->height + 2 * style->padding.y);
  19087. /* add additional touch padding for touch screen devices */
  19088. bounds.x = r.x - style->touch_padding.x;
  19089. bounds.y = r.y - style->touch_padding.y;
  19090. bounds.w = r.w + 2 * style->touch_padding.x;
  19091. bounds.h = r.h + 2 * style->touch_padding.y;
  19092. /* calculate the selector space */
  19093. select.w = font->height;
  19094. select.h = select.w;
  19095. select.y = r.y + r.h/2.0f - select.h/2.0f;
  19096. select.x = r.x;
  19097. /* calculate the bounds of the cursor inside the selector */
  19098. cursor.x = select.x + style->padding.x + style->border;
  19099. cursor.y = select.y + style->padding.y + style->border;
  19100. cursor.w = select.w - (2 * style->padding.x + 2 * style->border);
  19101. cursor.h = select.h - (2 * style->padding.y + 2 * style->border);
  19102. /* label behind the selector */
  19103. label.x = select.x + select.w + style->spacing;
  19104. label.y = select.y;
  19105. label.w = NK_MAX(r.x + r.w, label.x) - label.x;
  19106. label.h = select.w;
  19107. /* update selector */
  19108. was_active = *active;
  19109. *active = nk_toggle_behavior(in, bounds, state, *active);
  19110. /* draw selector */
  19111. if (style->draw_begin)
  19112. style->draw_begin(out, style->userdata);
  19113. if (type == NK_TOGGLE_CHECK) {
  19114. nk_draw_checkbox(out, *state, style, *active, &label, &select, &cursor, str, len, font);
  19115. } else {
  19116. nk_draw_option(out, *state, style, *active, &label, &select, &cursor, str, len, font);
  19117. }
  19118. if (style->draw_end)
  19119. style->draw_end(out, style->userdata);
  19120. return (was_active != *active);
  19121. }
  19122. /*----------------------------------------------------------------
  19123. *
  19124. * CHECKBOX
  19125. *
  19126. * --------------------------------------------------------------*/
  19127. NK_API int
  19128. nk_check_text(struct nk_context *ctx, const char *text, int len, int active)
  19129. {
  19130. struct nk_window *win;
  19131. struct nk_panel *layout;
  19132. const struct nk_input *in;
  19133. const struct nk_style *style;
  19134. struct nk_rect bounds;
  19135. enum nk_widget_layout_states state;
  19136. NK_ASSERT(ctx);
  19137. NK_ASSERT(ctx->current);
  19138. NK_ASSERT(ctx->current->layout);
  19139. if (!ctx || !ctx->current || !ctx->current->layout)
  19140. return active;
  19141. win = ctx->current;
  19142. style = &ctx->style;
  19143. layout = win->layout;
  19144. state = nk_widget(&bounds, ctx);
  19145. if (!state) return active;
  19146. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19147. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &active,
  19148. text, len, NK_TOGGLE_CHECK, &style->checkbox, in, style->font);
  19149. return active;
  19150. }
  19151. NK_API unsigned int
  19152. nk_check_flags_text(struct nk_context *ctx, const char *text, int len,
  19153. unsigned int flags, unsigned int value)
  19154. {
  19155. int old_active;
  19156. NK_ASSERT(ctx);
  19157. NK_ASSERT(text);
  19158. if (!ctx || !text) return flags;
  19159. old_active = (int)((flags & value) & value);
  19160. if (nk_check_text(ctx, text, len, old_active))
  19161. flags |= value;
  19162. else flags &= ~value;
  19163. return flags;
  19164. }
  19165. NK_API int
  19166. nk_checkbox_text(struct nk_context *ctx, const char *text, int len, int *active)
  19167. {
  19168. int old_val;
  19169. NK_ASSERT(ctx);
  19170. NK_ASSERT(text);
  19171. NK_ASSERT(active);
  19172. if (!ctx || !text || !active) return 0;
  19173. old_val = *active;
  19174. *active = nk_check_text(ctx, text, len, *active);
  19175. return old_val != *active;
  19176. }
  19177. NK_API int
  19178. nk_checkbox_flags_text(struct nk_context *ctx, const char *text, int len,
  19179. unsigned int *flags, unsigned int value)
  19180. {
  19181. int active;
  19182. NK_ASSERT(ctx);
  19183. NK_ASSERT(text);
  19184. NK_ASSERT(flags);
  19185. if (!ctx || !text || !flags) return 0;
  19186. active = (int)((*flags & value) & value);
  19187. if (nk_checkbox_text(ctx, text, len, &active)) {
  19188. if (active) *flags |= value;
  19189. else *flags &= ~value;
  19190. return 1;
  19191. }
  19192. return 0;
  19193. }
  19194. NK_API int nk_check_label(struct nk_context *ctx, const char *label, int active)
  19195. {
  19196. return nk_check_text(ctx, label, nk_strlen(label), active);
  19197. }
  19198. NK_API unsigned int nk_check_flags_label(struct nk_context *ctx, const char *label,
  19199. unsigned int flags, unsigned int value)
  19200. {
  19201. return nk_check_flags_text(ctx, label, nk_strlen(label), flags, value);
  19202. }
  19203. NK_API int nk_checkbox_label(struct nk_context *ctx, const char *label, int *active)
  19204. {
  19205. return nk_checkbox_text(ctx, label, nk_strlen(label), active);
  19206. }
  19207. NK_API int nk_checkbox_flags_label(struct nk_context *ctx, const char *label,
  19208. unsigned int *flags, unsigned int value)
  19209. {
  19210. return nk_checkbox_flags_text(ctx, label, nk_strlen(label), flags, value);
  19211. }
  19212. /*----------------------------------------------------------------
  19213. *
  19214. * OPTION
  19215. *
  19216. * --------------------------------------------------------------*/
  19217. NK_API int
  19218. nk_option_text(struct nk_context *ctx, const char *text, int len, int is_active)
  19219. {
  19220. struct nk_window *win;
  19221. struct nk_panel *layout;
  19222. const struct nk_input *in;
  19223. const struct nk_style *style;
  19224. struct nk_rect bounds;
  19225. enum nk_widget_layout_states state;
  19226. NK_ASSERT(ctx);
  19227. NK_ASSERT(ctx->current);
  19228. NK_ASSERT(ctx->current->layout);
  19229. if (!ctx || !ctx->current || !ctx->current->layout)
  19230. return is_active;
  19231. win = ctx->current;
  19232. style = &ctx->style;
  19233. layout = win->layout;
  19234. state = nk_widget(&bounds, ctx);
  19235. if (!state) return (int)state;
  19236. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19237. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &is_active,
  19238. text, len, NK_TOGGLE_OPTION, &style->option, in, style->font);
  19239. return is_active;
  19240. }
  19241. NK_API int
  19242. nk_radio_text(struct nk_context *ctx, const char *text, int len, int *active)
  19243. {
  19244. int old_value;
  19245. NK_ASSERT(ctx);
  19246. NK_ASSERT(text);
  19247. NK_ASSERT(active);
  19248. if (!ctx || !text || !active) return 0;
  19249. old_value = *active;
  19250. *active = nk_option_text(ctx, text, len, old_value);
  19251. return old_value != *active;
  19252. }
  19253. NK_API int
  19254. nk_option_label(struct nk_context *ctx, const char *label, int active)
  19255. {
  19256. return nk_option_text(ctx, label, nk_strlen(label), active);
  19257. }
  19258. NK_API int
  19259. nk_radio_label(struct nk_context *ctx, const char *label, int *active)
  19260. {
  19261. return nk_radio_text(ctx, label, nk_strlen(label), active);
  19262. }
  19263. /* ===============================================================
  19264. *
  19265. * SELECTABLE
  19266. *
  19267. * ===============================================================*/
  19268. NK_LIB void
  19269. nk_draw_selectable(struct nk_command_buffer *out,
  19270. nk_flags state, const struct nk_style_selectable *style, int active,
  19271. const struct nk_rect *bounds,
  19272. const struct nk_rect *icon, const struct nk_image *img, enum nk_symbol_type sym,
  19273. const char *string, int len, nk_flags align, const struct nk_user_font *font)
  19274. {
  19275. const struct nk_style_item *background;
  19276. struct nk_text text;
  19277. text.padding = style->padding;
  19278. /* select correct colors/images */
  19279. if (!active) {
  19280. if (state & NK_WIDGET_STATE_ACTIVED) {
  19281. background = &style->pressed;
  19282. text.text = style->text_pressed;
  19283. } else if (state & NK_WIDGET_STATE_HOVER) {
  19284. background = &style->hover;
  19285. text.text = style->text_hover;
  19286. } else {
  19287. background = &style->normal;
  19288. text.text = style->text_normal;
  19289. }
  19290. } else {
  19291. if (state & NK_WIDGET_STATE_ACTIVED) {
  19292. background = &style->pressed_active;
  19293. text.text = style->text_pressed_active;
  19294. } else if (state & NK_WIDGET_STATE_HOVER) {
  19295. background = &style->hover_active;
  19296. text.text = style->text_hover_active;
  19297. } else {
  19298. background = &style->normal_active;
  19299. text.text = style->text_normal_active;
  19300. }
  19301. }
  19302. /* draw selectable background and text */
  19303. if (background->type == NK_STYLE_ITEM_IMAGE) {
  19304. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  19305. text.background = nk_rgba(0,0,0,0);
  19306. } else {
  19307. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  19308. text.background = background->data.color;
  19309. }
  19310. if (icon) {
  19311. if (img) nk_draw_image(out, *icon, img, nk_white);
  19312. else nk_draw_symbol(out, sym, *icon, text.background, text.text, 1, font);
  19313. }
  19314. nk_widget_text(out, *bounds, string, len, &text, align, font);
  19315. }
  19316. NK_LIB int
  19317. nk_do_selectable(nk_flags *state, struct nk_command_buffer *out,
  19318. struct nk_rect bounds, const char *str, int len, nk_flags align, int *value,
  19319. const struct nk_style_selectable *style, const struct nk_input *in,
  19320. const struct nk_user_font *font)
  19321. {
  19322. int old_value;
  19323. struct nk_rect touch;
  19324. NK_ASSERT(state);
  19325. NK_ASSERT(out);
  19326. NK_ASSERT(str);
  19327. NK_ASSERT(len);
  19328. NK_ASSERT(value);
  19329. NK_ASSERT(style);
  19330. NK_ASSERT(font);
  19331. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  19332. old_value = *value;
  19333. /* remove padding */
  19334. touch.x = bounds.x - style->touch_padding.x;
  19335. touch.y = bounds.y - style->touch_padding.y;
  19336. touch.w = bounds.w + style->touch_padding.x * 2;
  19337. touch.h = bounds.h + style->touch_padding.y * 2;
  19338. /* update button */
  19339. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  19340. *value = !(*value);
  19341. /* draw selectable */
  19342. if (style->draw_begin) style->draw_begin(out, style->userdata);
  19343. nk_draw_selectable(out, *state, style, *value, &bounds, 0,0,NK_SYMBOL_NONE, str, len, align, font);
  19344. if (style->draw_end) style->draw_end(out, style->userdata);
  19345. return old_value != *value;
  19346. }
  19347. NK_LIB int
  19348. nk_do_selectable_image(nk_flags *state, struct nk_command_buffer *out,
  19349. struct nk_rect bounds, const char *str, int len, nk_flags align, int *value,
  19350. const struct nk_image *img, const struct nk_style_selectable *style,
  19351. const struct nk_input *in, const struct nk_user_font *font)
  19352. {
  19353. int old_value;
  19354. struct nk_rect touch;
  19355. struct nk_rect icon;
  19356. NK_ASSERT(state);
  19357. NK_ASSERT(out);
  19358. NK_ASSERT(str);
  19359. NK_ASSERT(len);
  19360. NK_ASSERT(value);
  19361. NK_ASSERT(style);
  19362. NK_ASSERT(font);
  19363. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  19364. old_value = *value;
  19365. /* toggle behavior */
  19366. touch.x = bounds.x - style->touch_padding.x;
  19367. touch.y = bounds.y - style->touch_padding.y;
  19368. touch.w = bounds.w + style->touch_padding.x * 2;
  19369. touch.h = bounds.h + style->touch_padding.y * 2;
  19370. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  19371. *value = !(*value);
  19372. icon.y = bounds.y + style->padding.y;
  19373. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  19374. if (align & NK_TEXT_ALIGN_LEFT) {
  19375. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  19376. icon.x = NK_MAX(icon.x, 0);
  19377. } else icon.x = bounds.x + 2 * style->padding.x;
  19378. icon.x += style->image_padding.x;
  19379. icon.y += style->image_padding.y;
  19380. icon.w -= 2 * style->image_padding.x;
  19381. icon.h -= 2 * style->image_padding.y;
  19382. /* draw selectable */
  19383. if (style->draw_begin) style->draw_begin(out, style->userdata);
  19384. nk_draw_selectable(out, *state, style, *value, &bounds, &icon, img, NK_SYMBOL_NONE, str, len, align, font);
  19385. if (style->draw_end) style->draw_end(out, style->userdata);
  19386. return old_value != *value;
  19387. }
  19388. NK_LIB int
  19389. nk_do_selectable_symbol(nk_flags *state, struct nk_command_buffer *out,
  19390. struct nk_rect bounds, const char *str, int len, nk_flags align, int *value,
  19391. enum nk_symbol_type sym, const struct nk_style_selectable *style,
  19392. const struct nk_input *in, const struct nk_user_font *font)
  19393. {
  19394. int old_value;
  19395. struct nk_rect touch;
  19396. struct nk_rect icon;
  19397. NK_ASSERT(state);
  19398. NK_ASSERT(out);
  19399. NK_ASSERT(str);
  19400. NK_ASSERT(len);
  19401. NK_ASSERT(value);
  19402. NK_ASSERT(style);
  19403. NK_ASSERT(font);
  19404. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  19405. old_value = *value;
  19406. /* toggle behavior */
  19407. touch.x = bounds.x - style->touch_padding.x;
  19408. touch.y = bounds.y - style->touch_padding.y;
  19409. touch.w = bounds.w + style->touch_padding.x * 2;
  19410. touch.h = bounds.h + style->touch_padding.y * 2;
  19411. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  19412. *value = !(*value);
  19413. icon.y = bounds.y + style->padding.y;
  19414. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  19415. if (align & NK_TEXT_ALIGN_LEFT) {
  19416. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  19417. icon.x = NK_MAX(icon.x, 0);
  19418. } else icon.x = bounds.x + 2 * style->padding.x;
  19419. icon.x += style->image_padding.x;
  19420. icon.y += style->image_padding.y;
  19421. icon.w -= 2 * style->image_padding.x;
  19422. icon.h -= 2 * style->image_padding.y;
  19423. /* draw selectable */
  19424. if (style->draw_begin) style->draw_begin(out, style->userdata);
  19425. nk_draw_selectable(out, *state, style, *value, &bounds, &icon, 0, sym, str, len, align, font);
  19426. if (style->draw_end) style->draw_end(out, style->userdata);
  19427. return old_value != *value;
  19428. }
  19429. NK_API int
  19430. nk_selectable_text(struct nk_context *ctx, const char *str, int len,
  19431. nk_flags align, int *value)
  19432. {
  19433. struct nk_window *win;
  19434. struct nk_panel *layout;
  19435. const struct nk_input *in;
  19436. const struct nk_style *style;
  19437. enum nk_widget_layout_states state;
  19438. struct nk_rect bounds;
  19439. NK_ASSERT(ctx);
  19440. NK_ASSERT(value);
  19441. NK_ASSERT(ctx->current);
  19442. NK_ASSERT(ctx->current->layout);
  19443. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  19444. return 0;
  19445. win = ctx->current;
  19446. layout = win->layout;
  19447. style = &ctx->style;
  19448. state = nk_widget(&bounds, ctx);
  19449. if (!state) return 0;
  19450. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19451. return nk_do_selectable(&ctx->last_widget_state, &win->buffer, bounds,
  19452. str, len, align, value, &style->selectable, in, style->font);
  19453. }
  19454. NK_API int
  19455. nk_selectable_image_text(struct nk_context *ctx, struct nk_image img,
  19456. const char *str, int len, nk_flags align, int *value)
  19457. {
  19458. struct nk_window *win;
  19459. struct nk_panel *layout;
  19460. const struct nk_input *in;
  19461. const struct nk_style *style;
  19462. enum nk_widget_layout_states state;
  19463. struct nk_rect bounds;
  19464. NK_ASSERT(ctx);
  19465. NK_ASSERT(value);
  19466. NK_ASSERT(ctx->current);
  19467. NK_ASSERT(ctx->current->layout);
  19468. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  19469. return 0;
  19470. win = ctx->current;
  19471. layout = win->layout;
  19472. style = &ctx->style;
  19473. state = nk_widget(&bounds, ctx);
  19474. if (!state) return 0;
  19475. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19476. return nk_do_selectable_image(&ctx->last_widget_state, &win->buffer, bounds,
  19477. str, len, align, value, &img, &style->selectable, in, style->font);
  19478. }
  19479. NK_API int
  19480. nk_selectable_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  19481. const char *str, int len, nk_flags align, int *value)
  19482. {
  19483. struct nk_window *win;
  19484. struct nk_panel *layout;
  19485. const struct nk_input *in;
  19486. const struct nk_style *style;
  19487. enum nk_widget_layout_states state;
  19488. struct nk_rect bounds;
  19489. NK_ASSERT(ctx);
  19490. NK_ASSERT(value);
  19491. NK_ASSERT(ctx->current);
  19492. NK_ASSERT(ctx->current->layout);
  19493. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  19494. return 0;
  19495. win = ctx->current;
  19496. layout = win->layout;
  19497. style = &ctx->style;
  19498. state = nk_widget(&bounds, ctx);
  19499. if (!state) return 0;
  19500. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19501. return nk_do_selectable_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  19502. str, len, align, value, sym, &style->selectable, in, style->font);
  19503. }
  19504. NK_API int
  19505. nk_selectable_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  19506. const char *title, nk_flags align, int *value)
  19507. {
  19508. return nk_selectable_symbol_text(ctx, sym, title, nk_strlen(title), align, value);
  19509. }
  19510. NK_API int nk_select_text(struct nk_context *ctx, const char *str, int len,
  19511. nk_flags align, int value)
  19512. {
  19513. nk_selectable_text(ctx, str, len, align, &value);return value;
  19514. }
  19515. NK_API int nk_selectable_label(struct nk_context *ctx, const char *str, nk_flags align, int *value)
  19516. {
  19517. return nk_selectable_text(ctx, str, nk_strlen(str), align, value);
  19518. }
  19519. NK_API int nk_selectable_image_label(struct nk_context *ctx,struct nk_image img,
  19520. const char *str, nk_flags align, int *value)
  19521. {
  19522. return nk_selectable_image_text(ctx, img, str, nk_strlen(str), align, value);
  19523. }
  19524. NK_API int nk_select_label(struct nk_context *ctx, const char *str, nk_flags align, int value)
  19525. {
  19526. nk_selectable_text(ctx, str, nk_strlen(str), align, &value);return value;
  19527. }
  19528. NK_API int nk_select_image_label(struct nk_context *ctx, struct nk_image img,
  19529. const char *str, nk_flags align, int value)
  19530. {
  19531. nk_selectable_image_text(ctx, img, str, nk_strlen(str), align, &value);return value;
  19532. }
  19533. NK_API int nk_select_image_text(struct nk_context *ctx, struct nk_image img,
  19534. const char *str, int len, nk_flags align, int value)
  19535. {
  19536. nk_selectable_image_text(ctx, img, str, len, align, &value);return value;
  19537. }
  19538. NK_API int
  19539. nk_select_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  19540. const char *title, int title_len, nk_flags align, int value)
  19541. {
  19542. nk_selectable_symbol_text(ctx, sym, title, title_len, align, &value);return value;
  19543. }
  19544. NK_API int
  19545. nk_select_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  19546. const char *title, nk_flags align, int value)
  19547. {
  19548. return nk_select_symbol_text(ctx, sym, title, nk_strlen(title), align, value);
  19549. }
  19550. /* ===============================================================
  19551. *
  19552. * SLIDER
  19553. *
  19554. * ===============================================================*/
  19555. NK_LIB float
  19556. nk_slider_behavior(nk_flags *state, struct nk_rect *logical_cursor,
  19557. struct nk_rect *visual_cursor, struct nk_input *in,
  19558. struct nk_rect bounds, float slider_min, float slider_max, float slider_value,
  19559. float slider_step, float slider_steps)
  19560. {
  19561. int left_mouse_down;
  19562. int left_mouse_click_in_cursor;
  19563. /* check if visual cursor is being dragged */
  19564. nk_widget_state_reset(state);
  19565. left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  19566. left_mouse_click_in_cursor = in && nk_input_has_mouse_click_down_in_rect(in,
  19567. NK_BUTTON_LEFT, *visual_cursor, nk_true);
  19568. if (left_mouse_down && left_mouse_click_in_cursor) {
  19569. float ratio = 0;
  19570. const float d = in->mouse.pos.x - (visual_cursor->x+visual_cursor->w*0.5f);
  19571. const float pxstep = bounds.w / slider_steps;
  19572. /* only update value if the next slider step is reached */
  19573. *state = NK_WIDGET_STATE_ACTIVE;
  19574. if (NK_ABS(d) >= pxstep) {
  19575. const float steps = (float)((int)(NK_ABS(d) / pxstep));
  19576. slider_value += (d > 0) ? (slider_step*steps) : -(slider_step*steps);
  19577. slider_value = NK_CLAMP(slider_min, slider_value, slider_max);
  19578. ratio = (slider_value - slider_min)/slider_step;
  19579. logical_cursor->x = bounds.x + (logical_cursor->w * ratio);
  19580. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = logical_cursor->x;
  19581. }
  19582. }
  19583. /* slider widget state */
  19584. if (nk_input_is_mouse_hovering_rect(in, bounds))
  19585. *state = NK_WIDGET_STATE_HOVERED;
  19586. if (*state & NK_WIDGET_STATE_HOVER &&
  19587. !nk_input_is_mouse_prev_hovering_rect(in, bounds))
  19588. *state |= NK_WIDGET_STATE_ENTERED;
  19589. else if (nk_input_is_mouse_prev_hovering_rect(in, bounds))
  19590. *state |= NK_WIDGET_STATE_LEFT;
  19591. return slider_value;
  19592. }
  19593. NK_LIB void
  19594. nk_draw_slider(struct nk_command_buffer *out, nk_flags state,
  19595. const struct nk_style_slider *style, const struct nk_rect *bounds,
  19596. const struct nk_rect *visual_cursor, float min, float value, float max)
  19597. {
  19598. struct nk_rect fill;
  19599. struct nk_rect bar;
  19600. const struct nk_style_item *background;
  19601. /* select correct slider images/colors */
  19602. struct nk_color bar_color;
  19603. const struct nk_style_item *cursor;
  19604. NK_UNUSED(min);
  19605. NK_UNUSED(max);
  19606. NK_UNUSED(value);
  19607. if (state & NK_WIDGET_STATE_ACTIVED) {
  19608. background = &style->active;
  19609. bar_color = style->bar_active;
  19610. cursor = &style->cursor_active;
  19611. } else if (state & NK_WIDGET_STATE_HOVER) {
  19612. background = &style->hover;
  19613. bar_color = style->bar_hover;
  19614. cursor = &style->cursor_hover;
  19615. } else {
  19616. background = &style->normal;
  19617. bar_color = style->bar_normal;
  19618. cursor = &style->cursor_normal;
  19619. }
  19620. /* calculate slider background bar */
  19621. bar.x = bounds->x;
  19622. bar.y = (visual_cursor->y + visual_cursor->h/2) - bounds->h/12;
  19623. bar.w = bounds->w;
  19624. bar.h = bounds->h/6;
  19625. /* filled background bar style */
  19626. fill.w = (visual_cursor->x + (visual_cursor->w/2.0f)) - bar.x;
  19627. fill.x = bar.x;
  19628. fill.y = bar.y;
  19629. fill.h = bar.h;
  19630. /* draw background */
  19631. if (background->type == NK_STYLE_ITEM_IMAGE) {
  19632. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  19633. } else {
  19634. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  19635. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  19636. }
  19637. /* draw slider bar */
  19638. nk_fill_rect(out, bar, style->rounding, bar_color);
  19639. nk_fill_rect(out, fill, style->rounding, style->bar_filled);
  19640. /* draw cursor */
  19641. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  19642. nk_draw_image(out, *visual_cursor, &cursor->data.image, nk_white);
  19643. else nk_fill_circle(out, *visual_cursor, cursor->data.color);
  19644. }
  19645. NK_LIB float
  19646. nk_do_slider(nk_flags *state,
  19647. struct nk_command_buffer *out, struct nk_rect bounds,
  19648. float min, float val, float max, float step,
  19649. const struct nk_style_slider *style, struct nk_input *in,
  19650. const struct nk_user_font *font)
  19651. {
  19652. float slider_range;
  19653. float slider_min;
  19654. float slider_max;
  19655. float slider_value;
  19656. float slider_steps;
  19657. float cursor_offset;
  19658. struct nk_rect visual_cursor;
  19659. struct nk_rect logical_cursor;
  19660. NK_ASSERT(style);
  19661. NK_ASSERT(out);
  19662. if (!out || !style)
  19663. return 0;
  19664. /* remove padding from slider bounds */
  19665. bounds.x = bounds.x + style->padding.x;
  19666. bounds.y = bounds.y + style->padding.y;
  19667. bounds.h = NK_MAX(bounds.h, 2*style->padding.y);
  19668. bounds.w = NK_MAX(bounds.w, 2*style->padding.x + style->cursor_size.x);
  19669. bounds.w -= 2 * style->padding.x;
  19670. bounds.h -= 2 * style->padding.y;
  19671. /* optional buttons */
  19672. if (style->show_buttons) {
  19673. nk_flags ws;
  19674. struct nk_rect button;
  19675. button.y = bounds.y;
  19676. button.w = bounds.h;
  19677. button.h = bounds.h;
  19678. /* decrement button */
  19679. button.x = bounds.x;
  19680. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol, NK_BUTTON_DEFAULT,
  19681. &style->dec_button, in, font))
  19682. val -= step;
  19683. /* increment button */
  19684. button.x = (bounds.x + bounds.w) - button.w;
  19685. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol, NK_BUTTON_DEFAULT,
  19686. &style->inc_button, in, font))
  19687. val += step;
  19688. bounds.x = bounds.x + button.w + style->spacing.x;
  19689. bounds.w = bounds.w - (2*button.w + 2*style->spacing.x);
  19690. }
  19691. /* remove one cursor size to support visual cursor */
  19692. bounds.x += style->cursor_size.x*0.5f;
  19693. bounds.w -= style->cursor_size.x;
  19694. /* make sure the provided values are correct */
  19695. slider_max = NK_MAX(min, max);
  19696. slider_min = NK_MIN(min, max);
  19697. slider_value = NK_CLAMP(slider_min, val, slider_max);
  19698. slider_range = slider_max - slider_min;
  19699. slider_steps = slider_range / step;
  19700. cursor_offset = (slider_value - slider_min) / step;
  19701. /* calculate cursor
  19702. Basically you have two cursors. One for visual representation and interaction
  19703. and one for updating the actual cursor value. */
  19704. logical_cursor.h = bounds.h;
  19705. logical_cursor.w = bounds.w / slider_steps;
  19706. logical_cursor.x = bounds.x + (logical_cursor.w * cursor_offset);
  19707. logical_cursor.y = bounds.y;
  19708. visual_cursor.h = style->cursor_size.y;
  19709. visual_cursor.w = style->cursor_size.x;
  19710. visual_cursor.y = (bounds.y + bounds.h*0.5f) - visual_cursor.h*0.5f;
  19711. visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
  19712. slider_value = nk_slider_behavior(state, &logical_cursor, &visual_cursor,
  19713. in, bounds, slider_min, slider_max, slider_value, step, slider_steps);
  19714. visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
  19715. /* draw slider */
  19716. if (style->draw_begin) style->draw_begin(out, style->userdata);
  19717. nk_draw_slider(out, *state, style, &bounds, &visual_cursor, slider_min, slider_value, slider_max);
  19718. if (style->draw_end) style->draw_end(out, style->userdata);
  19719. return slider_value;
  19720. }
  19721. NK_API int
  19722. nk_slider_float(struct nk_context *ctx, float min_value, float *value, float max_value,
  19723. float value_step)
  19724. {
  19725. struct nk_window *win;
  19726. struct nk_panel *layout;
  19727. struct nk_input *in;
  19728. const struct nk_style *style;
  19729. int ret = 0;
  19730. float old_value;
  19731. struct nk_rect bounds;
  19732. enum nk_widget_layout_states state;
  19733. NK_ASSERT(ctx);
  19734. NK_ASSERT(ctx->current);
  19735. NK_ASSERT(ctx->current->layout);
  19736. NK_ASSERT(value);
  19737. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  19738. return ret;
  19739. win = ctx->current;
  19740. style = &ctx->style;
  19741. layout = win->layout;
  19742. state = nk_widget(&bounds, ctx);
  19743. if (!state) return ret;
  19744. in = (/*state == NK_WIDGET_ROM || */ layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19745. old_value = *value;
  19746. *value = nk_do_slider(&ctx->last_widget_state, &win->buffer, bounds, min_value,
  19747. old_value, max_value, value_step, &style->slider, in, style->font);
  19748. return (old_value > *value || old_value < *value);
  19749. }
  19750. NK_API float
  19751. nk_slide_float(struct nk_context *ctx, float min, float val, float max, float step)
  19752. {
  19753. nk_slider_float(ctx, min, &val, max, step); return val;
  19754. }
  19755. NK_API int
  19756. nk_slide_int(struct nk_context *ctx, int min, int val, int max, int step)
  19757. {
  19758. float value = (float)val;
  19759. nk_slider_float(ctx, (float)min, &value, (float)max, (float)step);
  19760. return (int)value;
  19761. }
  19762. NK_API int
  19763. nk_slider_int(struct nk_context *ctx, int min, int *val, int max, int step)
  19764. {
  19765. int ret;
  19766. float value = (float)*val;
  19767. ret = nk_slider_float(ctx, (float)min, &value, (float)max, (float)step);
  19768. *val = (int)value;
  19769. return ret;
  19770. }
  19771. /* ===============================================================
  19772. *
  19773. * PROGRESS
  19774. *
  19775. * ===============================================================*/
  19776. NK_LIB nk_size
  19777. nk_progress_behavior(nk_flags *state, struct nk_input *in,
  19778. struct nk_rect r, struct nk_rect cursor, nk_size max, nk_size value, int modifiable)
  19779. {
  19780. int left_mouse_down = 0;
  19781. int left_mouse_click_in_cursor = 0;
  19782. nk_widget_state_reset(state);
  19783. if (!in || !modifiable) return value;
  19784. left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  19785. left_mouse_click_in_cursor = in && nk_input_has_mouse_click_down_in_rect(in,
  19786. NK_BUTTON_LEFT, cursor, nk_true);
  19787. if (nk_input_is_mouse_hovering_rect(in, r))
  19788. *state = NK_WIDGET_STATE_HOVERED;
  19789. if (in && left_mouse_down && left_mouse_click_in_cursor) {
  19790. if (left_mouse_down && left_mouse_click_in_cursor) {
  19791. float ratio = NK_MAX(0, (float)(in->mouse.pos.x - cursor.x)) / (float)cursor.w;
  19792. value = (nk_size)NK_CLAMP(0, (float)max * ratio, (float)max);
  19793. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor.x + cursor.w/2.0f;
  19794. *state |= NK_WIDGET_STATE_ACTIVE;
  19795. }
  19796. }
  19797. /* set progressbar widget state */
  19798. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, r))
  19799. *state |= NK_WIDGET_STATE_ENTERED;
  19800. else if (nk_input_is_mouse_prev_hovering_rect(in, r))
  19801. *state |= NK_WIDGET_STATE_LEFT;
  19802. return value;
  19803. }
  19804. NK_LIB void
  19805. nk_draw_progress(struct nk_command_buffer *out, nk_flags state,
  19806. const struct nk_style_progress *style, const struct nk_rect *bounds,
  19807. const struct nk_rect *scursor, nk_size value, nk_size max)
  19808. {
  19809. const struct nk_style_item *background;
  19810. const struct nk_style_item *cursor;
  19811. NK_UNUSED(max);
  19812. NK_UNUSED(value);
  19813. /* select correct colors/images to draw */
  19814. if (state & NK_WIDGET_STATE_ACTIVED) {
  19815. background = &style->active;
  19816. cursor = &style->cursor_active;
  19817. } else if (state & NK_WIDGET_STATE_HOVER){
  19818. background = &style->hover;
  19819. cursor = &style->cursor_hover;
  19820. } else {
  19821. background = &style->normal;
  19822. cursor = &style->cursor_normal;
  19823. }
  19824. /* draw background */
  19825. if (background->type == NK_STYLE_ITEM_COLOR) {
  19826. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  19827. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  19828. } else nk_draw_image(out, *bounds, &background->data.image, nk_white);
  19829. /* draw cursor */
  19830. if (cursor->type == NK_STYLE_ITEM_COLOR) {
  19831. nk_fill_rect(out, *scursor, style->rounding, cursor->data.color);
  19832. nk_stroke_rect(out, *scursor, style->rounding, style->border, style->border_color);
  19833. } else nk_draw_image(out, *scursor, &cursor->data.image, nk_white);
  19834. }
  19835. NK_LIB nk_size
  19836. nk_do_progress(nk_flags *state,
  19837. struct nk_command_buffer *out, struct nk_rect bounds,
  19838. nk_size value, nk_size max, int modifiable,
  19839. const struct nk_style_progress *style, struct nk_input *in)
  19840. {
  19841. float prog_scale;
  19842. nk_size prog_value;
  19843. struct nk_rect cursor;
  19844. NK_ASSERT(style);
  19845. NK_ASSERT(out);
  19846. if (!out || !style) return 0;
  19847. /* calculate progressbar cursor */
  19848. cursor.w = NK_MAX(bounds.w, 2 * style->padding.x + 2 * style->border);
  19849. cursor.h = NK_MAX(bounds.h, 2 * style->padding.y + 2 * style->border);
  19850. cursor = nk_pad_rect(bounds, nk_vec2(style->padding.x + style->border, style->padding.y + style->border));
  19851. prog_scale = (float)value / (float)max;
  19852. /* update progressbar */
  19853. prog_value = NK_MIN(value, max);
  19854. prog_value = nk_progress_behavior(state, in, bounds, cursor,max, prog_value, modifiable);
  19855. cursor.w = cursor.w * prog_scale;
  19856. /* draw progressbar */
  19857. if (style->draw_begin) style->draw_begin(out, style->userdata);
  19858. nk_draw_progress(out, *state, style, &bounds, &cursor, value, max);
  19859. if (style->draw_end) style->draw_end(out, style->userdata);
  19860. return prog_value;
  19861. }
  19862. NK_API int
  19863. nk_progress(struct nk_context *ctx, nk_size *cur, nk_size max, int is_modifyable)
  19864. {
  19865. struct nk_window *win;
  19866. struct nk_panel *layout;
  19867. const struct nk_style *style;
  19868. struct nk_input *in;
  19869. struct nk_rect bounds;
  19870. enum nk_widget_layout_states state;
  19871. nk_size old_value;
  19872. NK_ASSERT(ctx);
  19873. NK_ASSERT(cur);
  19874. NK_ASSERT(ctx->current);
  19875. NK_ASSERT(ctx->current->layout);
  19876. if (!ctx || !ctx->current || !ctx->current->layout || !cur)
  19877. return 0;
  19878. win = ctx->current;
  19879. style = &ctx->style;
  19880. layout = win->layout;
  19881. state = nk_widget(&bounds, ctx);
  19882. if (!state) return 0;
  19883. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19884. old_value = *cur;
  19885. *cur = nk_do_progress(&ctx->last_widget_state, &win->buffer, bounds,
  19886. *cur, max, is_modifyable, &style->progress, in);
  19887. return (*cur != old_value);
  19888. }
  19889. NK_API nk_size
  19890. nk_prog(struct nk_context *ctx, nk_size cur, nk_size max, int modifyable)
  19891. {
  19892. nk_progress(ctx, &cur, max, modifyable);
  19893. return cur;
  19894. }
  19895. /* ===============================================================
  19896. *
  19897. * SCROLLBAR
  19898. *
  19899. * ===============================================================*/
  19900. NK_LIB float
  19901. nk_scrollbar_behavior(nk_flags *state, struct nk_input *in,
  19902. int has_scrolling, const struct nk_rect *scroll,
  19903. const struct nk_rect *cursor, const struct nk_rect *empty0,
  19904. const struct nk_rect *empty1, float scroll_offset,
  19905. float target, float scroll_step, enum nk_orientation o)
  19906. {
  19907. nk_flags ws = 0;
  19908. int left_mouse_down;
  19909. int left_mouse_clicked;
  19910. int left_mouse_click_in_cursor;
  19911. float scroll_delta;
  19912. nk_widget_state_reset(state);
  19913. if (!in) return scroll_offset;
  19914. left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  19915. left_mouse_clicked = in->mouse.buttons[NK_BUTTON_LEFT].clicked;
  19916. left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
  19917. NK_BUTTON_LEFT, *cursor, nk_true);
  19918. if (nk_input_is_mouse_hovering_rect(in, *scroll))
  19919. *state = NK_WIDGET_STATE_HOVERED;
  19920. scroll_delta = (o == NK_VERTICAL) ? in->mouse.scroll_delta.y: in->mouse.scroll_delta.x;
  19921. if (left_mouse_down && left_mouse_click_in_cursor && !left_mouse_clicked) {
  19922. /* update cursor by mouse dragging */
  19923. float pixel, delta;
  19924. *state = NK_WIDGET_STATE_ACTIVE;
  19925. if (o == NK_VERTICAL) {
  19926. float cursor_y;
  19927. pixel = in->mouse.delta.y;
  19928. delta = (pixel / scroll->h) * target;
  19929. scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->h);
  19930. cursor_y = scroll->y + ((scroll_offset/target) * scroll->h);
  19931. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = cursor_y + cursor->h/2.0f;
  19932. } else {
  19933. float cursor_x;
  19934. pixel = in->mouse.delta.x;
  19935. delta = (pixel / scroll->w) * target;
  19936. scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->w);
  19937. cursor_x = scroll->x + ((scroll_offset/target) * scroll->w);
  19938. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor_x + cursor->w/2.0f;
  19939. }
  19940. } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_UP) && o == NK_VERTICAL && has_scrolling)||
  19941. nk_button_behavior(&ws, *empty0, in, NK_BUTTON_DEFAULT)) {
  19942. /* scroll page up by click on empty space or shortcut */
  19943. if (o == NK_VERTICAL)
  19944. scroll_offset = NK_MAX(0, scroll_offset - scroll->h);
  19945. else scroll_offset = NK_MAX(0, scroll_offset - scroll->w);
  19946. } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_DOWN) && o == NK_VERTICAL && has_scrolling) ||
  19947. nk_button_behavior(&ws, *empty1, in, NK_BUTTON_DEFAULT)) {
  19948. /* scroll page down by click on empty space or shortcut */
  19949. if (o == NK_VERTICAL)
  19950. scroll_offset = NK_MIN(scroll_offset + scroll->h, target - scroll->h);
  19951. else scroll_offset = NK_MIN(scroll_offset + scroll->w, target - scroll->w);
  19952. } else if (has_scrolling) {
  19953. if ((scroll_delta < 0 || (scroll_delta > 0))) {
  19954. /* update cursor by mouse scrolling */
  19955. scroll_offset = scroll_offset + scroll_step * (-scroll_delta);
  19956. if (o == NK_VERTICAL)
  19957. scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->h);
  19958. else scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->w);
  19959. } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_START)) {
  19960. /* update cursor to the beginning */
  19961. if (o == NK_VERTICAL) scroll_offset = 0;
  19962. } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_END)) {
  19963. /* update cursor to the end */
  19964. if (o == NK_VERTICAL) scroll_offset = target - scroll->h;
  19965. }
  19966. }
  19967. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *scroll))
  19968. *state |= NK_WIDGET_STATE_ENTERED;
  19969. else if (nk_input_is_mouse_prev_hovering_rect(in, *scroll))
  19970. *state |= NK_WIDGET_STATE_LEFT;
  19971. return scroll_offset;
  19972. }
  19973. NK_LIB void
  19974. nk_draw_scrollbar(struct nk_command_buffer *out, nk_flags state,
  19975. const struct nk_style_scrollbar *style, const struct nk_rect *bounds,
  19976. const struct nk_rect *scroll)
  19977. {
  19978. const struct nk_style_item *background;
  19979. const struct nk_style_item *cursor;
  19980. /* select correct colors/images to draw */
  19981. if (state & NK_WIDGET_STATE_ACTIVED) {
  19982. background = &style->active;
  19983. cursor = &style->cursor_active;
  19984. } else if (state & NK_WIDGET_STATE_HOVER) {
  19985. background = &style->hover;
  19986. cursor = &style->cursor_hover;
  19987. } else {
  19988. background = &style->normal;
  19989. cursor = &style->cursor_normal;
  19990. }
  19991. /* draw background */
  19992. if (background->type == NK_STYLE_ITEM_COLOR) {
  19993. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  19994. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  19995. } else {
  19996. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  19997. }
  19998. /* draw cursor */
  19999. if (cursor->type == NK_STYLE_ITEM_COLOR) {
  20000. nk_fill_rect(out, *scroll, style->rounding_cursor, cursor->data.color);
  20001. nk_stroke_rect(out, *scroll, style->rounding_cursor, style->border_cursor, style->cursor_border_color);
  20002. } else nk_draw_image(out, *scroll, &cursor->data.image, nk_white);
  20003. }
  20004. NK_LIB float
  20005. nk_do_scrollbarv(nk_flags *state,
  20006. struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
  20007. float offset, float target, float step, float button_pixel_inc,
  20008. const struct nk_style_scrollbar *style, struct nk_input *in,
  20009. const struct nk_user_font *font)
  20010. {
  20011. struct nk_rect empty_north;
  20012. struct nk_rect empty_south;
  20013. struct nk_rect cursor;
  20014. float scroll_step;
  20015. float scroll_offset;
  20016. float scroll_off;
  20017. float scroll_ratio;
  20018. NK_ASSERT(out);
  20019. NK_ASSERT(style);
  20020. NK_ASSERT(state);
  20021. if (!out || !style) return 0;
  20022. scroll.w = NK_MAX(scroll.w, 1);
  20023. scroll.h = NK_MAX(scroll.h, 0);
  20024. if (target <= scroll.h) return 0;
  20025. /* optional scrollbar buttons */
  20026. if (style->show_buttons) {
  20027. nk_flags ws;
  20028. float scroll_h;
  20029. struct nk_rect button;
  20030. button.x = scroll.x;
  20031. button.w = scroll.w;
  20032. button.h = scroll.w;
  20033. scroll_h = NK_MAX(scroll.h - 2 * button.h,0);
  20034. scroll_step = NK_MIN(step, button_pixel_inc);
  20035. /* decrement button */
  20036. button.y = scroll.y;
  20037. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
  20038. NK_BUTTON_REPEATER, &style->dec_button, in, font))
  20039. offset = offset - scroll_step;
  20040. /* increment button */
  20041. button.y = scroll.y + scroll.h - button.h;
  20042. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
  20043. NK_BUTTON_REPEATER, &style->inc_button, in, font))
  20044. offset = offset + scroll_step;
  20045. scroll.y = scroll.y + button.h;
  20046. scroll.h = scroll_h;
  20047. }
  20048. /* calculate scrollbar constants */
  20049. scroll_step = NK_MIN(step, scroll.h);
  20050. scroll_offset = NK_CLAMP(0, offset, target - scroll.h);
  20051. scroll_ratio = scroll.h / target;
  20052. scroll_off = scroll_offset / target;
  20053. /* calculate scrollbar cursor bounds */
  20054. cursor.h = NK_MAX((scroll_ratio * scroll.h) - (2*style->border + 2*style->padding.y), 0);
  20055. cursor.y = scroll.y + (scroll_off * scroll.h) + style->border + style->padding.y;
  20056. cursor.w = scroll.w - (2 * style->border + 2 * style->padding.x);
  20057. cursor.x = scroll.x + style->border + style->padding.x;
  20058. /* calculate empty space around cursor */
  20059. empty_north.x = scroll.x;
  20060. empty_north.y = scroll.y;
  20061. empty_north.w = scroll.w;
  20062. empty_north.h = NK_MAX(cursor.y - scroll.y, 0);
  20063. empty_south.x = scroll.x;
  20064. empty_south.y = cursor.y + cursor.h;
  20065. empty_south.w = scroll.w;
  20066. empty_south.h = NK_MAX((scroll.y + scroll.h) - (cursor.y + cursor.h), 0);
  20067. /* update scrollbar */
  20068. scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
  20069. &empty_north, &empty_south, scroll_offset, target, scroll_step, NK_VERTICAL);
  20070. scroll_off = scroll_offset / target;
  20071. cursor.y = scroll.y + (scroll_off * scroll.h) + style->border_cursor + style->padding.y;
  20072. /* draw scrollbar */
  20073. if (style->draw_begin) style->draw_begin(out, style->userdata);
  20074. nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
  20075. if (style->draw_end) style->draw_end(out, style->userdata);
  20076. return scroll_offset;
  20077. }
  20078. NK_LIB float
  20079. nk_do_scrollbarh(nk_flags *state,
  20080. struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
  20081. float offset, float target, float step, float button_pixel_inc,
  20082. const struct nk_style_scrollbar *style, struct nk_input *in,
  20083. const struct nk_user_font *font)
  20084. {
  20085. struct nk_rect cursor;
  20086. struct nk_rect empty_west;
  20087. struct nk_rect empty_east;
  20088. float scroll_step;
  20089. float scroll_offset;
  20090. float scroll_off;
  20091. float scroll_ratio;
  20092. NK_ASSERT(out);
  20093. NK_ASSERT(style);
  20094. if (!out || !style) return 0;
  20095. /* scrollbar background */
  20096. scroll.h = NK_MAX(scroll.h, 1);
  20097. scroll.w = NK_MAX(scroll.w, 2 * scroll.h);
  20098. if (target <= scroll.w) return 0;
  20099. /* optional scrollbar buttons */
  20100. if (style->show_buttons) {
  20101. nk_flags ws;
  20102. float scroll_w;
  20103. struct nk_rect button;
  20104. button.y = scroll.y;
  20105. button.w = scroll.h;
  20106. button.h = scroll.h;
  20107. scroll_w = scroll.w - 2 * button.w;
  20108. scroll_step = NK_MIN(step, button_pixel_inc);
  20109. /* decrement button */
  20110. button.x = scroll.x;
  20111. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
  20112. NK_BUTTON_REPEATER, &style->dec_button, in, font))
  20113. offset = offset - scroll_step;
  20114. /* increment button */
  20115. button.x = scroll.x + scroll.w - button.w;
  20116. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
  20117. NK_BUTTON_REPEATER, &style->inc_button, in, font))
  20118. offset = offset + scroll_step;
  20119. scroll.x = scroll.x + button.w;
  20120. scroll.w = scroll_w;
  20121. }
  20122. /* calculate scrollbar constants */
  20123. scroll_step = NK_MIN(step, scroll.w);
  20124. scroll_offset = NK_CLAMP(0, offset, target - scroll.w);
  20125. scroll_ratio = scroll.w / target;
  20126. scroll_off = scroll_offset / target;
  20127. /* calculate cursor bounds */
  20128. cursor.w = (scroll_ratio * scroll.w) - (2*style->border + 2*style->padding.x);
  20129. cursor.x = scroll.x + (scroll_off * scroll.w) + style->border + style->padding.x;
  20130. cursor.h = scroll.h - (2 * style->border + 2 * style->padding.y);
  20131. cursor.y = scroll.y + style->border + style->padding.y;
  20132. /* calculate empty space around cursor */
  20133. empty_west.x = scroll.x;
  20134. empty_west.y = scroll.y;
  20135. empty_west.w = cursor.x - scroll.x;
  20136. empty_west.h = scroll.h;
  20137. empty_east.x = cursor.x + cursor.w;
  20138. empty_east.y = scroll.y;
  20139. empty_east.w = (scroll.x + scroll.w) - (cursor.x + cursor.w);
  20140. empty_east.h = scroll.h;
  20141. /* update scrollbar */
  20142. scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
  20143. &empty_west, &empty_east, scroll_offset, target, scroll_step, NK_HORIZONTAL);
  20144. scroll_off = scroll_offset / target;
  20145. cursor.x = scroll.x + (scroll_off * scroll.w);
  20146. /* draw scrollbar */
  20147. if (style->draw_begin) style->draw_begin(out, style->userdata);
  20148. nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
  20149. if (style->draw_end) style->draw_end(out, style->userdata);
  20150. return scroll_offset;
  20151. }
  20152. /* ===============================================================
  20153. *
  20154. * TEXT EDITOR
  20155. *
  20156. * ===============================================================*/
  20157. /* stb_textedit.h - v1.8 - public domain - Sean Barrett */
  20158. struct nk_text_find {
  20159. float x,y; /* position of n'th character */
  20160. float height; /* height of line */
  20161. int first_char, length; /* first char of row, and length */
  20162. int prev_first; /*_ first char of previous row */
  20163. };
  20164. struct nk_text_edit_row {
  20165. float x0,x1;
  20166. /* starting x location, end x location (allows for align=right, etc) */
  20167. float baseline_y_delta;
  20168. /* position of baseline relative to previous row's baseline*/
  20169. float ymin,ymax;
  20170. /* height of row above and below baseline */
  20171. int num_chars;
  20172. };
  20173. /* forward declarations */
  20174. NK_INTERN void nk_textedit_makeundo_delete(struct nk_text_edit*, int, int);
  20175. NK_INTERN void nk_textedit_makeundo_insert(struct nk_text_edit*, int, int);
  20176. NK_INTERN void nk_textedit_makeundo_replace(struct nk_text_edit*, int, int, int);
  20177. #define NK_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end)
  20178. NK_INTERN float
  20179. nk_textedit_get_width(const struct nk_text_edit *edit, int line_start, int char_id,
  20180. const struct nk_user_font *font)
  20181. {
  20182. int len = 0;
  20183. nk_rune unicode = 0;
  20184. const char *str = nk_str_at_const(&edit->string, line_start + char_id, &unicode, &len);
  20185. return font->width(font->userdata, font->height, str, len);
  20186. }
  20187. NK_INTERN void
  20188. nk_textedit_layout_row(struct nk_text_edit_row *r, struct nk_text_edit *edit,
  20189. int line_start_id, float row_height, const struct nk_user_font *font)
  20190. {
  20191. int l;
  20192. int glyphs = 0;
  20193. nk_rune unicode;
  20194. const char *remaining;
  20195. int len = nk_str_len_char(&edit->string);
  20196. const char *end = nk_str_get_const(&edit->string) + len;
  20197. const char *text = nk_str_at_const(&edit->string, line_start_id, &unicode, &l);
  20198. const struct nk_vec2 size = nk_text_calculate_text_bounds(font,
  20199. text, (int)(end - text), row_height, &remaining, 0, &glyphs, NK_STOP_ON_NEW_LINE);
  20200. r->x0 = 0.0f;
  20201. r->x1 = size.x;
  20202. r->baseline_y_delta = size.y;
  20203. r->ymin = 0.0f;
  20204. r->ymax = size.y;
  20205. r->num_chars = glyphs;
  20206. }
  20207. NK_INTERN int
  20208. nk_textedit_locate_coord(struct nk_text_edit *edit, float x, float y,
  20209. const struct nk_user_font *font, float row_height)
  20210. {
  20211. struct nk_text_edit_row r;
  20212. int n = edit->string.len;
  20213. float base_y = 0, prev_x;
  20214. int i=0, k;
  20215. r.x0 = r.x1 = 0;
  20216. r.ymin = r.ymax = 0;
  20217. r.num_chars = 0;
  20218. /* search rows to find one that straddles 'y' */
  20219. while (i < n) {
  20220. nk_textedit_layout_row(&r, edit, i, row_height, font);
  20221. if (r.num_chars <= 0)
  20222. return n;
  20223. if (i==0 && y < base_y + r.ymin)
  20224. return 0;
  20225. if (y < base_y + r.ymax)
  20226. break;
  20227. i += r.num_chars;
  20228. base_y += r.baseline_y_delta;
  20229. }
  20230. /* below all text, return 'after' last character */
  20231. if (i >= n)
  20232. return n;
  20233. /* check if it's before the beginning of the line */
  20234. if (x < r.x0)
  20235. return i;
  20236. /* check if it's before the end of the line */
  20237. if (x < r.x1) {
  20238. /* search characters in row for one that straddles 'x' */
  20239. k = i;
  20240. prev_x = r.x0;
  20241. for (i=0; i < r.num_chars; ++i) {
  20242. float w = nk_textedit_get_width(edit, k, i, font);
  20243. if (x < prev_x+w) {
  20244. if (x < prev_x+w/2)
  20245. return k+i;
  20246. else return k+i+1;
  20247. }
  20248. prev_x += w;
  20249. }
  20250. /* shouldn't happen, but if it does, fall through to end-of-line case */
  20251. }
  20252. /* if the last character is a newline, return that.
  20253. * otherwise return 'after' the last character */
  20254. if (nk_str_rune_at(&edit->string, i+r.num_chars-1) == '\n')
  20255. return i+r.num_chars-1;
  20256. else return i+r.num_chars;
  20257. }
  20258. NK_LIB void
  20259. nk_textedit_click(struct nk_text_edit *state, float x, float y,
  20260. const struct nk_user_font *font, float row_height)
  20261. {
  20262. /* API click: on mouse down, move the cursor to the clicked location,
  20263. * and reset the selection */
  20264. state->cursor = nk_textedit_locate_coord(state, x, y, font, row_height);
  20265. state->select_start = state->cursor;
  20266. state->select_end = state->cursor;
  20267. state->has_preferred_x = 0;
  20268. }
  20269. NK_LIB void
  20270. nk_textedit_drag(struct nk_text_edit *state, float x, float y,
  20271. const struct nk_user_font *font, float row_height)
  20272. {
  20273. /* API drag: on mouse drag, move the cursor and selection endpoint
  20274. * to the clicked location */
  20275. int p = nk_textedit_locate_coord(state, x, y, font, row_height);
  20276. if (state->select_start == state->select_end)
  20277. state->select_start = state->cursor;
  20278. state->cursor = state->select_end = p;
  20279. }
  20280. NK_INTERN void
  20281. nk_textedit_find_charpos(struct nk_text_find *find, struct nk_text_edit *state,
  20282. int n, int single_line, const struct nk_user_font *font, float row_height)
  20283. {
  20284. /* find the x/y location of a character, and remember info about the previous
  20285. * row in case we get a move-up event (for page up, we'll have to rescan) */
  20286. struct nk_text_edit_row r;
  20287. int prev_start = 0;
  20288. int z = state->string.len;
  20289. int i=0, first;
  20290. nk_zero_struct(r);
  20291. if (n == z) {
  20292. /* if it's at the end, then find the last line -- simpler than trying to
  20293. explicitly handle this case in the regular code */
  20294. nk_textedit_layout_row(&r, state, 0, row_height, font);
  20295. if (single_line) {
  20296. find->first_char = 0;
  20297. find->length = z;
  20298. } else {
  20299. while (i < z) {
  20300. prev_start = i;
  20301. i += r.num_chars;
  20302. nk_textedit_layout_row(&r, state, i, row_height, font);
  20303. }
  20304. find->first_char = i;
  20305. find->length = r.num_chars;
  20306. }
  20307. find->x = r.x1;
  20308. find->y = r.ymin;
  20309. find->height = r.ymax - r.ymin;
  20310. find->prev_first = prev_start;
  20311. return;
  20312. }
  20313. /* search rows to find the one that straddles character n */
  20314. find->y = 0;
  20315. for(;;) {
  20316. nk_textedit_layout_row(&r, state, i, row_height, font);
  20317. if (n < i + r.num_chars) break;
  20318. prev_start = i;
  20319. i += r.num_chars;
  20320. find->y += r.baseline_y_delta;
  20321. }
  20322. find->first_char = first = i;
  20323. find->length = r.num_chars;
  20324. find->height = r.ymax - r.ymin;
  20325. find->prev_first = prev_start;
  20326. /* now scan to find xpos */
  20327. find->x = r.x0;
  20328. for (i=0; first+i < n; ++i)
  20329. find->x += nk_textedit_get_width(state, first, i, font);
  20330. }
  20331. NK_INTERN void
  20332. nk_textedit_clamp(struct nk_text_edit *state)
  20333. {
  20334. /* make the selection/cursor state valid if client altered the string */
  20335. int n = state->string.len;
  20336. if (NK_TEXT_HAS_SELECTION(state)) {
  20337. if (state->select_start > n) state->select_start = n;
  20338. if (state->select_end > n) state->select_end = n;
  20339. /* if clamping forced them to be equal, move the cursor to match */
  20340. if (state->select_start == state->select_end)
  20341. state->cursor = state->select_start;
  20342. }
  20343. if (state->cursor > n) state->cursor = n;
  20344. }
  20345. NK_API void
  20346. nk_textedit_delete(struct nk_text_edit *state, int where, int len)
  20347. {
  20348. /* delete characters while updating undo */
  20349. nk_textedit_makeundo_delete(state, where, len);
  20350. nk_str_delete_runes(&state->string, where, len);
  20351. state->has_preferred_x = 0;
  20352. }
  20353. NK_API void
  20354. nk_textedit_delete_selection(struct nk_text_edit *state)
  20355. {
  20356. /* delete the section */
  20357. nk_textedit_clamp(state);
  20358. if (NK_TEXT_HAS_SELECTION(state)) {
  20359. if (state->select_start < state->select_end) {
  20360. nk_textedit_delete(state, state->select_start,
  20361. state->select_end - state->select_start);
  20362. state->select_end = state->cursor = state->select_start;
  20363. } else {
  20364. nk_textedit_delete(state, state->select_end,
  20365. state->select_start - state->select_end);
  20366. state->select_start = state->cursor = state->select_end;
  20367. }
  20368. state->has_preferred_x = 0;
  20369. }
  20370. }
  20371. NK_INTERN void
  20372. nk_textedit_sortselection(struct nk_text_edit *state)
  20373. {
  20374. /* canonicalize the selection so start <= end */
  20375. if (state->select_end < state->select_start) {
  20376. int temp = state->select_end;
  20377. state->select_end = state->select_start;
  20378. state->select_start = temp;
  20379. }
  20380. }
  20381. NK_INTERN void
  20382. nk_textedit_move_to_first(struct nk_text_edit *state)
  20383. {
  20384. /* move cursor to first character of selection */
  20385. if (NK_TEXT_HAS_SELECTION(state)) {
  20386. nk_textedit_sortselection(state);
  20387. state->cursor = state->select_start;
  20388. state->select_end = state->select_start;
  20389. state->has_preferred_x = 0;
  20390. }
  20391. }
  20392. NK_INTERN void
  20393. nk_textedit_move_to_last(struct nk_text_edit *state)
  20394. {
  20395. /* move cursor to last character of selection */
  20396. if (NK_TEXT_HAS_SELECTION(state)) {
  20397. nk_textedit_sortselection(state);
  20398. nk_textedit_clamp(state);
  20399. state->cursor = state->select_end;
  20400. state->select_start = state->select_end;
  20401. state->has_preferred_x = 0;
  20402. }
  20403. }
  20404. NK_INTERN int
  20405. nk_is_word_boundary( struct nk_text_edit *state, int idx)
  20406. {
  20407. int len;
  20408. nk_rune c;
  20409. if (idx <= 0) return 1;
  20410. if (!nk_str_at_rune(&state->string, idx, &c, &len)) return 1;
  20411. return (c == ' ' || c == '\t' ||c == 0x3000 || c == ',' || c == ';' ||
  20412. c == '(' || c == ')' || c == '{' || c == '}' || c == '[' || c == ']' ||
  20413. c == '|');
  20414. }
  20415. NK_INTERN int
  20416. nk_textedit_move_to_word_previous(struct nk_text_edit *state)
  20417. {
  20418. int c = state->cursor - 1;
  20419. while( c >= 0 && !nk_is_word_boundary(state, c))
  20420. --c;
  20421. if( c < 0 )
  20422. c = 0;
  20423. return c;
  20424. }
  20425. NK_INTERN int
  20426. nk_textedit_move_to_word_next(struct nk_text_edit *state)
  20427. {
  20428. const int len = state->string.len;
  20429. int c = state->cursor+1;
  20430. while( c < len && !nk_is_word_boundary(state, c))
  20431. ++c;
  20432. if( c > len )
  20433. c = len;
  20434. return c;
  20435. }
  20436. NK_INTERN void
  20437. nk_textedit_prep_selection_at_cursor(struct nk_text_edit *state)
  20438. {
  20439. /* update selection and cursor to match each other */
  20440. if (!NK_TEXT_HAS_SELECTION(state))
  20441. state->select_start = state->select_end = state->cursor;
  20442. else state->cursor = state->select_end;
  20443. }
  20444. NK_API int
  20445. nk_textedit_cut(struct nk_text_edit *state)
  20446. {
  20447. /* API cut: delete selection */
  20448. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  20449. return 0;
  20450. if (NK_TEXT_HAS_SELECTION(state)) {
  20451. nk_textedit_delete_selection(state); /* implicitly clamps */
  20452. state->has_preferred_x = 0;
  20453. return 1;
  20454. }
  20455. return 0;
  20456. }
  20457. NK_API int
  20458. nk_textedit_paste(struct nk_text_edit *state, char const *ctext, int len)
  20459. {
  20460. /* API paste: replace existing selection with passed-in text */
  20461. int glyphs;
  20462. const char *text = (const char *) ctext;
  20463. if (state->mode == NK_TEXT_EDIT_MODE_VIEW) return 0;
  20464. /* if there's a selection, the paste should delete it */
  20465. nk_textedit_clamp(state);
  20466. nk_textedit_delete_selection(state);
  20467. /* try to insert the characters */
  20468. glyphs = nk_utf_len(ctext, len);
  20469. if (nk_str_insert_text_char(&state->string, state->cursor, text, len)) {
  20470. nk_textedit_makeundo_insert(state, state->cursor, glyphs);
  20471. state->cursor += len;
  20472. state->has_preferred_x = 0;
  20473. return 1;
  20474. }
  20475. /* remove the undo since we didn't actually insert the characters */
  20476. if (state->undo.undo_point)
  20477. --state->undo.undo_point;
  20478. return 0;
  20479. }
  20480. NK_API void
  20481. nk_textedit_text(struct nk_text_edit *state, const char *text, int total_len)
  20482. {
  20483. nk_rune unicode;
  20484. int glyph_len;
  20485. int text_len = 0;
  20486. NK_ASSERT(state);
  20487. NK_ASSERT(text);
  20488. if (!text || !total_len || state->mode == NK_TEXT_EDIT_MODE_VIEW) return;
  20489. glyph_len = nk_utf_decode(text, &unicode, total_len);
  20490. while ((text_len < total_len) && glyph_len)
  20491. {
  20492. /* don't insert a backward delete, just process the event */
  20493. if (unicode == 127) goto next;
  20494. /* can't add newline in single-line mode */
  20495. if (unicode == '\n' && state->single_line) goto next;
  20496. /* filter incoming text */
  20497. if (state->filter && !state->filter(state, unicode)) goto next;
  20498. if (!NK_TEXT_HAS_SELECTION(state) &&
  20499. state->cursor < state->string.len)
  20500. {
  20501. if (state->mode == NK_TEXT_EDIT_MODE_REPLACE) {
  20502. nk_textedit_makeundo_replace(state, state->cursor, 1, 1);
  20503. nk_str_delete_runes(&state->string, state->cursor, 1);
  20504. }
  20505. if (nk_str_insert_text_utf8(&state->string, state->cursor,
  20506. text+text_len, 1))
  20507. {
  20508. ++state->cursor;
  20509. state->has_preferred_x = 0;
  20510. }
  20511. } else {
  20512. nk_textedit_delete_selection(state); /* implicitly clamps */
  20513. if (nk_str_insert_text_utf8(&state->string, state->cursor,
  20514. text+text_len, 1))
  20515. {
  20516. nk_textedit_makeundo_insert(state, state->cursor, 1);
  20517. ++state->cursor;
  20518. state->has_preferred_x = 0;
  20519. }
  20520. }
  20521. next:
  20522. text_len += glyph_len;
  20523. glyph_len = nk_utf_decode(text + text_len, &unicode, total_len-text_len);
  20524. }
  20525. }
  20526. NK_LIB void
  20527. nk_textedit_key(struct nk_text_edit *state, enum nk_keys key, int shift_mod,
  20528. const struct nk_user_font *font, float row_height)
  20529. {
  20530. retry:
  20531. switch (key)
  20532. {
  20533. case NK_KEY_NONE:
  20534. case NK_KEY_CTRL:
  20535. case NK_KEY_ENTER:
  20536. case NK_KEY_SHIFT:
  20537. case NK_KEY_TAB:
  20538. case NK_KEY_COPY:
  20539. case NK_KEY_CUT:
  20540. case NK_KEY_PASTE:
  20541. case NK_KEY_MAX:
  20542. default: break;
  20543. case NK_KEY_TEXT_UNDO:
  20544. nk_textedit_undo(state);
  20545. state->has_preferred_x = 0;
  20546. break;
  20547. case NK_KEY_TEXT_REDO:
  20548. nk_textedit_redo(state);
  20549. state->has_preferred_x = 0;
  20550. break;
  20551. case NK_KEY_TEXT_SELECT_ALL:
  20552. nk_textedit_select_all(state);
  20553. state->has_preferred_x = 0;
  20554. break;
  20555. case NK_KEY_TEXT_INSERT_MODE:
  20556. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  20557. state->mode = NK_TEXT_EDIT_MODE_INSERT;
  20558. break;
  20559. case NK_KEY_TEXT_REPLACE_MODE:
  20560. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  20561. state->mode = NK_TEXT_EDIT_MODE_REPLACE;
  20562. break;
  20563. case NK_KEY_TEXT_RESET_MODE:
  20564. if (state->mode == NK_TEXT_EDIT_MODE_INSERT ||
  20565. state->mode == NK_TEXT_EDIT_MODE_REPLACE)
  20566. state->mode = NK_TEXT_EDIT_MODE_VIEW;
  20567. break;
  20568. case NK_KEY_LEFT:
  20569. if (shift_mod) {
  20570. nk_textedit_clamp(state);
  20571. nk_textedit_prep_selection_at_cursor(state);
  20572. /* move selection left */
  20573. if (state->select_end > 0)
  20574. --state->select_end;
  20575. state->cursor = state->select_end;
  20576. state->has_preferred_x = 0;
  20577. } else {
  20578. /* if currently there's a selection,
  20579. * move cursor to start of selection */
  20580. if (NK_TEXT_HAS_SELECTION(state))
  20581. nk_textedit_move_to_first(state);
  20582. else if (state->cursor > 0)
  20583. --state->cursor;
  20584. state->has_preferred_x = 0;
  20585. } break;
  20586. case NK_KEY_RIGHT:
  20587. if (shift_mod) {
  20588. nk_textedit_prep_selection_at_cursor(state);
  20589. /* move selection right */
  20590. ++state->select_end;
  20591. nk_textedit_clamp(state);
  20592. state->cursor = state->select_end;
  20593. state->has_preferred_x = 0;
  20594. } else {
  20595. /* if currently there's a selection,
  20596. * move cursor to end of selection */
  20597. if (NK_TEXT_HAS_SELECTION(state))
  20598. nk_textedit_move_to_last(state);
  20599. else ++state->cursor;
  20600. nk_textedit_clamp(state);
  20601. state->has_preferred_x = 0;
  20602. } break;
  20603. case NK_KEY_TEXT_WORD_LEFT:
  20604. if (shift_mod) {
  20605. if( !NK_TEXT_HAS_SELECTION( state ) )
  20606. nk_textedit_prep_selection_at_cursor(state);
  20607. state->cursor = nk_textedit_move_to_word_previous(state);
  20608. state->select_end = state->cursor;
  20609. nk_textedit_clamp(state );
  20610. } else {
  20611. if (NK_TEXT_HAS_SELECTION(state))
  20612. nk_textedit_move_to_first(state);
  20613. else {
  20614. state->cursor = nk_textedit_move_to_word_previous(state);
  20615. nk_textedit_clamp(state );
  20616. }
  20617. } break;
  20618. case NK_KEY_TEXT_WORD_RIGHT:
  20619. if (shift_mod) {
  20620. if( !NK_TEXT_HAS_SELECTION( state ) )
  20621. nk_textedit_prep_selection_at_cursor(state);
  20622. state->cursor = nk_textedit_move_to_word_next(state);
  20623. state->select_end = state->cursor;
  20624. nk_textedit_clamp(state);
  20625. } else {
  20626. if (NK_TEXT_HAS_SELECTION(state))
  20627. nk_textedit_move_to_last(state);
  20628. else {
  20629. state->cursor = nk_textedit_move_to_word_next(state);
  20630. nk_textedit_clamp(state );
  20631. }
  20632. } break;
  20633. case NK_KEY_DOWN: {
  20634. struct nk_text_find find;
  20635. struct nk_text_edit_row row;
  20636. int i, sel = shift_mod;
  20637. if (state->single_line) {
  20638. /* on windows, up&down in single-line behave like left&right */
  20639. key = NK_KEY_RIGHT;
  20640. goto retry;
  20641. }
  20642. if (sel)
  20643. nk_textedit_prep_selection_at_cursor(state);
  20644. else if (NK_TEXT_HAS_SELECTION(state))
  20645. nk_textedit_move_to_last(state);
  20646. /* compute current position of cursor point */
  20647. nk_textedit_clamp(state);
  20648. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  20649. font, row_height);
  20650. /* now find character position down a row */
  20651. if (find.length)
  20652. {
  20653. float x;
  20654. float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
  20655. int start = find.first_char + find.length;
  20656. state->cursor = start;
  20657. nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
  20658. x = row.x0;
  20659. for (i=0; i < row.num_chars && x < row.x1; ++i) {
  20660. float dx = nk_textedit_get_width(state, start, i, font);
  20661. x += dx;
  20662. if (x > goal_x)
  20663. break;
  20664. ++state->cursor;
  20665. }
  20666. nk_textedit_clamp(state);
  20667. state->has_preferred_x = 1;
  20668. state->preferred_x = goal_x;
  20669. if (sel)
  20670. state->select_end = state->cursor;
  20671. }
  20672. } break;
  20673. case NK_KEY_UP: {
  20674. struct nk_text_find find;
  20675. struct nk_text_edit_row row;
  20676. int i, sel = shift_mod;
  20677. if (state->single_line) {
  20678. /* on windows, up&down become left&right */
  20679. key = NK_KEY_LEFT;
  20680. goto retry;
  20681. }
  20682. if (sel)
  20683. nk_textedit_prep_selection_at_cursor(state);
  20684. else if (NK_TEXT_HAS_SELECTION(state))
  20685. nk_textedit_move_to_first(state);
  20686. /* compute current position of cursor point */
  20687. nk_textedit_clamp(state);
  20688. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  20689. font, row_height);
  20690. /* can only go up if there's a previous row */
  20691. if (find.prev_first != find.first_char) {
  20692. /* now find character position up a row */
  20693. float x;
  20694. float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
  20695. state->cursor = find.prev_first;
  20696. nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
  20697. x = row.x0;
  20698. for (i=0; i < row.num_chars && x < row.x1; ++i) {
  20699. float dx = nk_textedit_get_width(state, find.prev_first, i, font);
  20700. x += dx;
  20701. if (x > goal_x)
  20702. break;
  20703. ++state->cursor;
  20704. }
  20705. nk_textedit_clamp(state);
  20706. state->has_preferred_x = 1;
  20707. state->preferred_x = goal_x;
  20708. if (sel) state->select_end = state->cursor;
  20709. }
  20710. } break;
  20711. case NK_KEY_DEL:
  20712. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  20713. break;
  20714. if (NK_TEXT_HAS_SELECTION(state))
  20715. nk_textedit_delete_selection(state);
  20716. else {
  20717. int n = state->string.len;
  20718. if (state->cursor < n)
  20719. nk_textedit_delete(state, state->cursor, 1);
  20720. }
  20721. state->has_preferred_x = 0;
  20722. break;
  20723. case NK_KEY_BACKSPACE:
  20724. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  20725. break;
  20726. if (NK_TEXT_HAS_SELECTION(state))
  20727. nk_textedit_delete_selection(state);
  20728. else {
  20729. nk_textedit_clamp(state);
  20730. if (state->cursor > 0) {
  20731. nk_textedit_delete(state, state->cursor-1, 1);
  20732. --state->cursor;
  20733. }
  20734. }
  20735. state->has_preferred_x = 0;
  20736. break;
  20737. case NK_KEY_TEXT_START:
  20738. if (shift_mod) {
  20739. nk_textedit_prep_selection_at_cursor(state);
  20740. state->cursor = state->select_end = 0;
  20741. state->has_preferred_x = 0;
  20742. } else {
  20743. state->cursor = state->select_start = state->select_end = 0;
  20744. state->has_preferred_x = 0;
  20745. }
  20746. break;
  20747. case NK_KEY_TEXT_END:
  20748. if (shift_mod) {
  20749. nk_textedit_prep_selection_at_cursor(state);
  20750. state->cursor = state->select_end = state->string.len;
  20751. state->has_preferred_x = 0;
  20752. } else {
  20753. state->cursor = state->string.len;
  20754. state->select_start = state->select_end = 0;
  20755. state->has_preferred_x = 0;
  20756. }
  20757. break;
  20758. case NK_KEY_TEXT_LINE_START: {
  20759. if (shift_mod) {
  20760. struct nk_text_find find;
  20761. nk_textedit_clamp(state);
  20762. nk_textedit_prep_selection_at_cursor(state);
  20763. if (state->string.len && state->cursor == state->string.len)
  20764. --state->cursor;
  20765. nk_textedit_find_charpos(&find, state,state->cursor, state->single_line,
  20766. font, row_height);
  20767. state->cursor = state->select_end = find.first_char;
  20768. state->has_preferred_x = 0;
  20769. } else {
  20770. struct nk_text_find find;
  20771. if (state->string.len && state->cursor == state->string.len)
  20772. --state->cursor;
  20773. nk_textedit_clamp(state);
  20774. nk_textedit_move_to_first(state);
  20775. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  20776. font, row_height);
  20777. state->cursor = find.first_char;
  20778. state->has_preferred_x = 0;
  20779. }
  20780. } break;
  20781. case NK_KEY_TEXT_LINE_END: {
  20782. if (shift_mod) {
  20783. struct nk_text_find find;
  20784. nk_textedit_clamp(state);
  20785. nk_textedit_prep_selection_at_cursor(state);
  20786. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  20787. font, row_height);
  20788. state->has_preferred_x = 0;
  20789. state->cursor = find.first_char + find.length;
  20790. if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor-1) == '\n')
  20791. --state->cursor;
  20792. state->select_end = state->cursor;
  20793. } else {
  20794. struct nk_text_find find;
  20795. nk_textedit_clamp(state);
  20796. nk_textedit_move_to_first(state);
  20797. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  20798. font, row_height);
  20799. state->has_preferred_x = 0;
  20800. state->cursor = find.first_char + find.length;
  20801. if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor-1) == '\n')
  20802. --state->cursor;
  20803. }} break;
  20804. }
  20805. }
  20806. NK_INTERN void
  20807. nk_textedit_flush_redo(struct nk_text_undo_state *state)
  20808. {
  20809. state->redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
  20810. state->redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
  20811. }
  20812. NK_INTERN void
  20813. nk_textedit_discard_undo(struct nk_text_undo_state *state)
  20814. {
  20815. /* discard the oldest entry in the undo list */
  20816. if (state->undo_point > 0) {
  20817. /* if the 0th undo state has characters, clean those up */
  20818. if (state->undo_rec[0].char_storage >= 0) {
  20819. int n = state->undo_rec[0].insert_length, i;
  20820. /* delete n characters from all other records */
  20821. state->undo_char_point = (short)(state->undo_char_point - n);
  20822. NK_MEMCPY(state->undo_char, state->undo_char + n,
  20823. (nk_size)state->undo_char_point*sizeof(nk_rune));
  20824. for (i=0; i < state->undo_point; ++i) {
  20825. if (state->undo_rec[i].char_storage >= 0)
  20826. state->undo_rec[i].char_storage = (short)
  20827. (state->undo_rec[i].char_storage - n);
  20828. }
  20829. }
  20830. --state->undo_point;
  20831. NK_MEMCPY(state->undo_rec, state->undo_rec+1,
  20832. (nk_size)((nk_size)state->undo_point * sizeof(state->undo_rec[0])));
  20833. }
  20834. }
  20835. NK_INTERN void
  20836. nk_textedit_discard_redo(struct nk_text_undo_state *state)
  20837. {
  20838. /* discard the oldest entry in the redo list--it's bad if this
  20839. ever happens, but because undo & redo have to store the actual
  20840. characters in different cases, the redo character buffer can
  20841. fill up even though the undo buffer didn't */
  20842. nk_size num;
  20843. int k = NK_TEXTEDIT_UNDOSTATECOUNT-1;
  20844. if (state->redo_point <= k) {
  20845. /* if the k'th undo state has characters, clean those up */
  20846. if (state->undo_rec[k].char_storage >= 0) {
  20847. int n = state->undo_rec[k].insert_length, i;
  20848. /* delete n characters from all other records */
  20849. state->redo_char_point = (short)(state->redo_char_point + n);
  20850. num = (nk_size)(NK_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point);
  20851. NK_MEMCPY(state->undo_char + state->redo_char_point,
  20852. state->undo_char + state->redo_char_point-n, num * sizeof(char));
  20853. for (i = state->redo_point; i < k; ++i) {
  20854. if (state->undo_rec[i].char_storage >= 0) {
  20855. state->undo_rec[i].char_storage = (short)
  20856. (state->undo_rec[i].char_storage + n);
  20857. }
  20858. }
  20859. }
  20860. ++state->redo_point;
  20861. num = (nk_size)(NK_TEXTEDIT_UNDOSTATECOUNT - state->redo_point);
  20862. if (num) NK_MEMCPY(state->undo_rec + state->redo_point-1,
  20863. state->undo_rec + state->redo_point, num * sizeof(state->undo_rec[0]));
  20864. }
  20865. }
  20866. NK_INTERN struct nk_text_undo_record*
  20867. nk_textedit_create_undo_record(struct nk_text_undo_state *state, int numchars)
  20868. {
  20869. /* any time we create a new undo record, we discard redo*/
  20870. nk_textedit_flush_redo(state);
  20871. /* if we have no free records, we have to make room,
  20872. * by sliding the existing records down */
  20873. if (state->undo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  20874. nk_textedit_discard_undo(state);
  20875. /* if the characters to store won't possibly fit in the buffer,
  20876. * we can't undo */
  20877. if (numchars > NK_TEXTEDIT_UNDOCHARCOUNT) {
  20878. state->undo_point = 0;
  20879. state->undo_char_point = 0;
  20880. return 0;
  20881. }
  20882. /* if we don't have enough free characters in the buffer,
  20883. * we have to make room */
  20884. while (state->undo_char_point + numchars > NK_TEXTEDIT_UNDOCHARCOUNT)
  20885. nk_textedit_discard_undo(state);
  20886. return &state->undo_rec[state->undo_point++];
  20887. }
  20888. NK_INTERN nk_rune*
  20889. nk_textedit_createundo(struct nk_text_undo_state *state, int pos,
  20890. int insert_len, int delete_len)
  20891. {
  20892. struct nk_text_undo_record *r = nk_textedit_create_undo_record(state, insert_len);
  20893. if (r == 0)
  20894. return 0;
  20895. r->where = pos;
  20896. r->insert_length = (short) insert_len;
  20897. r->delete_length = (short) delete_len;
  20898. if (insert_len == 0) {
  20899. r->char_storage = -1;
  20900. return 0;
  20901. } else {
  20902. r->char_storage = state->undo_char_point;
  20903. state->undo_char_point = (short)(state->undo_char_point + insert_len);
  20904. return &state->undo_char[r->char_storage];
  20905. }
  20906. }
  20907. NK_API void
  20908. nk_textedit_undo(struct nk_text_edit *state)
  20909. {
  20910. struct nk_text_undo_state *s = &state->undo;
  20911. struct nk_text_undo_record u, *r;
  20912. if (s->undo_point == 0)
  20913. return;
  20914. /* we need to do two things: apply the undo record, and create a redo record */
  20915. u = s->undo_rec[s->undo_point-1];
  20916. r = &s->undo_rec[s->redo_point-1];
  20917. r->char_storage = -1;
  20918. r->insert_length = u.delete_length;
  20919. r->delete_length = u.insert_length;
  20920. r->where = u.where;
  20921. if (u.delete_length)
  20922. {
  20923. /* if the undo record says to delete characters, then the redo record will
  20924. need to re-insert the characters that get deleted, so we need to store
  20925. them.
  20926. there are three cases:
  20927. - there's enough room to store the characters
  20928. - characters stored for *redoing* don't leave room for redo
  20929. - characters stored for *undoing* don't leave room for redo
  20930. if the last is true, we have to bail */
  20931. if (s->undo_char_point + u.delete_length >= NK_TEXTEDIT_UNDOCHARCOUNT) {
  20932. /* the undo records take up too much character space; there's no space
  20933. * to store the redo characters */
  20934. r->insert_length = 0;
  20935. } else {
  20936. int i;
  20937. /* there's definitely room to store the characters eventually */
  20938. while (s->undo_char_point + u.delete_length > s->redo_char_point) {
  20939. /* there's currently not enough room, so discard a redo record */
  20940. nk_textedit_discard_redo(s);
  20941. /* should never happen: */
  20942. if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  20943. return;
  20944. }
  20945. r = &s->undo_rec[s->redo_point-1];
  20946. r->char_storage = (short)(s->redo_char_point - u.delete_length);
  20947. s->redo_char_point = (short)(s->redo_char_point - u.delete_length);
  20948. /* now save the characters */
  20949. for (i=0; i < u.delete_length; ++i)
  20950. s->undo_char[r->char_storage + i] =
  20951. nk_str_rune_at(&state->string, u.where + i);
  20952. }
  20953. /* now we can carry out the deletion */
  20954. nk_str_delete_runes(&state->string, u.where, u.delete_length);
  20955. }
  20956. /* check type of recorded action: */
  20957. if (u.insert_length) {
  20958. /* easy case: was a deletion, so we need to insert n characters */
  20959. nk_str_insert_text_runes(&state->string, u.where,
  20960. &s->undo_char[u.char_storage], u.insert_length);
  20961. s->undo_char_point = (short)(s->undo_char_point - u.insert_length);
  20962. }
  20963. state->cursor = (short)(u.where + u.insert_length);
  20964. s->undo_point--;
  20965. s->redo_point--;
  20966. }
  20967. NK_API void
  20968. nk_textedit_redo(struct nk_text_edit *state)
  20969. {
  20970. struct nk_text_undo_state *s = &state->undo;
  20971. struct nk_text_undo_record *u, r;
  20972. if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  20973. return;
  20974. /* we need to do two things: apply the redo record, and create an undo record */
  20975. u = &s->undo_rec[s->undo_point];
  20976. r = s->undo_rec[s->redo_point];
  20977. /* we KNOW there must be room for the undo record, because the redo record
  20978. was derived from an undo record */
  20979. u->delete_length = r.insert_length;
  20980. u->insert_length = r.delete_length;
  20981. u->where = r.where;
  20982. u->char_storage = -1;
  20983. if (r.delete_length) {
  20984. /* the redo record requires us to delete characters, so the undo record
  20985. needs to store the characters */
  20986. if (s->undo_char_point + u->insert_length > s->redo_char_point) {
  20987. u->insert_length = 0;
  20988. u->delete_length = 0;
  20989. } else {
  20990. int i;
  20991. u->char_storage = s->undo_char_point;
  20992. s->undo_char_point = (short)(s->undo_char_point + u->insert_length);
  20993. /* now save the characters */
  20994. for (i=0; i < u->insert_length; ++i) {
  20995. s->undo_char[u->char_storage + i] =
  20996. nk_str_rune_at(&state->string, u->where + i);
  20997. }
  20998. }
  20999. nk_str_delete_runes(&state->string, r.where, r.delete_length);
  21000. }
  21001. if (r.insert_length) {
  21002. /* easy case: need to insert n characters */
  21003. nk_str_insert_text_runes(&state->string, r.where,
  21004. &s->undo_char[r.char_storage], r.insert_length);
  21005. }
  21006. state->cursor = r.where + r.insert_length;
  21007. s->undo_point++;
  21008. s->redo_point++;
  21009. }
  21010. NK_INTERN void
  21011. nk_textedit_makeundo_insert(struct nk_text_edit *state, int where, int length)
  21012. {
  21013. nk_textedit_createundo(&state->undo, where, 0, length);
  21014. }
  21015. NK_INTERN void
  21016. nk_textedit_makeundo_delete(struct nk_text_edit *state, int where, int length)
  21017. {
  21018. int i;
  21019. nk_rune *p = nk_textedit_createundo(&state->undo, where, length, 0);
  21020. if (p) {
  21021. for (i=0; i < length; ++i)
  21022. p[i] = nk_str_rune_at(&state->string, where+i);
  21023. }
  21024. }
  21025. NK_INTERN void
  21026. nk_textedit_makeundo_replace(struct nk_text_edit *state, int where,
  21027. int old_length, int new_length)
  21028. {
  21029. int i;
  21030. nk_rune *p = nk_textedit_createundo(&state->undo, where, old_length, new_length);
  21031. if (p) {
  21032. for (i=0; i < old_length; ++i)
  21033. p[i] = nk_str_rune_at(&state->string, where+i);
  21034. }
  21035. }
  21036. NK_LIB void
  21037. nk_textedit_clear_state(struct nk_text_edit *state, enum nk_text_edit_type type,
  21038. nk_plugin_filter filter)
  21039. {
  21040. /* reset the state to default */
  21041. state->undo.undo_point = 0;
  21042. state->undo.undo_char_point = 0;
  21043. state->undo.redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
  21044. state->undo.redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
  21045. state->select_end = state->select_start = 0;
  21046. state->cursor = 0;
  21047. state->has_preferred_x = 0;
  21048. state->preferred_x = 0;
  21049. state->cursor_at_end_of_line = 0;
  21050. state->initialized = 1;
  21051. state->single_line = (unsigned char)(type == NK_TEXT_EDIT_SINGLE_LINE);
  21052. state->mode = NK_TEXT_EDIT_MODE_VIEW;
  21053. state->filter = filter;
  21054. state->scrollbar = nk_vec2(0,0);
  21055. }
  21056. NK_API void
  21057. nk_textedit_init_fixed(struct nk_text_edit *state, void *memory, nk_size size)
  21058. {
  21059. NK_ASSERT(state);
  21060. NK_ASSERT(memory);
  21061. if (!state || !memory || !size) return;
  21062. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  21063. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  21064. nk_str_init_fixed(&state->string, memory, size);
  21065. }
  21066. NK_API void
  21067. nk_textedit_init(struct nk_text_edit *state, struct nk_allocator *alloc, nk_size size)
  21068. {
  21069. NK_ASSERT(state);
  21070. NK_ASSERT(alloc);
  21071. if (!state || !alloc) return;
  21072. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  21073. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  21074. nk_str_init(&state->string, alloc, size);
  21075. }
  21076. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  21077. NK_API void
  21078. nk_textedit_init_default(struct nk_text_edit *state)
  21079. {
  21080. NK_ASSERT(state);
  21081. if (!state) return;
  21082. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  21083. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  21084. nk_str_init_default(&state->string);
  21085. }
  21086. #endif
  21087. NK_API void
  21088. nk_textedit_select_all(struct nk_text_edit *state)
  21089. {
  21090. NK_ASSERT(state);
  21091. state->select_start = 0;
  21092. state->select_end = state->string.len;
  21093. }
  21094. NK_API void
  21095. nk_textedit_free(struct nk_text_edit *state)
  21096. {
  21097. NK_ASSERT(state);
  21098. if (!state) return;
  21099. nk_str_free(&state->string);
  21100. }
  21101. /* ===============================================================
  21102. *
  21103. * FILTER
  21104. *
  21105. * ===============================================================*/
  21106. NK_API int
  21107. nk_filter_default(const struct nk_text_edit *box, nk_rune unicode)
  21108. {
  21109. NK_UNUSED(unicode);
  21110. NK_UNUSED(box);
  21111. return nk_true;
  21112. }
  21113. NK_API int
  21114. nk_filter_ascii(const struct nk_text_edit *box, nk_rune unicode)
  21115. {
  21116. NK_UNUSED(box);
  21117. if (unicode > 128) return nk_false;
  21118. else return nk_true;
  21119. }
  21120. NK_API int
  21121. nk_filter_float(const struct nk_text_edit *box, nk_rune unicode)
  21122. {
  21123. NK_UNUSED(box);
  21124. if ((unicode < '0' || unicode > '9') && unicode != '.' && unicode != '-')
  21125. return nk_false;
  21126. else return nk_true;
  21127. }
  21128. NK_API int
  21129. nk_filter_decimal(const struct nk_text_edit *box, nk_rune unicode)
  21130. {
  21131. NK_UNUSED(box);
  21132. if ((unicode < '0' || unicode > '9') && unicode != '-')
  21133. return nk_false;
  21134. else return nk_true;
  21135. }
  21136. NK_API int
  21137. nk_filter_hex(const struct nk_text_edit *box, nk_rune unicode)
  21138. {
  21139. NK_UNUSED(box);
  21140. if ((unicode < '0' || unicode > '9') &&
  21141. (unicode < 'a' || unicode > 'f') &&
  21142. (unicode < 'A' || unicode > 'F'))
  21143. return nk_false;
  21144. else return nk_true;
  21145. }
  21146. NK_API int
  21147. nk_filter_oct(const struct nk_text_edit *box, nk_rune unicode)
  21148. {
  21149. NK_UNUSED(box);
  21150. if (unicode < '0' || unicode > '7')
  21151. return nk_false;
  21152. else return nk_true;
  21153. }
  21154. NK_API int
  21155. nk_filter_binary(const struct nk_text_edit *box, nk_rune unicode)
  21156. {
  21157. NK_UNUSED(box);
  21158. if (unicode != '0' && unicode != '1')
  21159. return nk_false;
  21160. else return nk_true;
  21161. }
  21162. /* ===============================================================
  21163. *
  21164. * EDIT
  21165. *
  21166. * ===============================================================*/
  21167. NK_LIB void
  21168. nk_edit_draw_text(struct nk_command_buffer *out,
  21169. const struct nk_style_edit *style, float pos_x, float pos_y,
  21170. float x_offset, const char *text, int byte_len, float row_height,
  21171. const struct nk_user_font *font, struct nk_color background,
  21172. struct nk_color foreground, int is_selected)
  21173. {
  21174. NK_ASSERT(out);
  21175. NK_ASSERT(font);
  21176. NK_ASSERT(style);
  21177. if (!text || !byte_len || !out || !style) return;
  21178. {int glyph_len = 0;
  21179. nk_rune unicode = 0;
  21180. int text_len = 0;
  21181. float line_width = 0;
  21182. float glyph_width;
  21183. const char *line = text;
  21184. float line_offset = 0;
  21185. int line_count = 0;
  21186. struct nk_text txt;
  21187. txt.padding = nk_vec2(0,0);
  21188. txt.background = background;
  21189. txt.text = foreground;
  21190. glyph_len = nk_utf_decode(text+text_len, &unicode, byte_len-text_len);
  21191. if (!glyph_len) return;
  21192. while ((text_len < byte_len) && glyph_len)
  21193. {
  21194. if (unicode == '\n') {
  21195. /* new line separator so draw previous line */
  21196. struct nk_rect label;
  21197. label.y = pos_y + line_offset;
  21198. label.h = row_height;
  21199. label.w = line_width;
  21200. label.x = pos_x;
  21201. if (!line_count)
  21202. label.x += x_offset;
  21203. if (is_selected) /* selection needs to draw different background color */
  21204. nk_fill_rect(out, label, 0, background);
  21205. nk_widget_text(out, label, line, (int)((text + text_len) - line),
  21206. &txt, NK_TEXT_CENTERED, font);
  21207. text_len++;
  21208. line_count++;
  21209. line_width = 0;
  21210. line = text + text_len;
  21211. line_offset += row_height;
  21212. glyph_len = nk_utf_decode(text + text_len, &unicode, (int)(byte_len-text_len));
  21213. continue;
  21214. }
  21215. if (unicode == '\r') {
  21216. text_len++;
  21217. glyph_len = nk_utf_decode(text + text_len, &unicode, byte_len-text_len);
  21218. continue;
  21219. }
  21220. glyph_width = font->width(font->userdata, font->height, text+text_len, glyph_len);
  21221. line_width += (float)glyph_width;
  21222. text_len += glyph_len;
  21223. glyph_len = nk_utf_decode(text + text_len, &unicode, byte_len-text_len);
  21224. continue;
  21225. }
  21226. if (line_width > 0) {
  21227. /* draw last line */
  21228. struct nk_rect label;
  21229. label.y = pos_y + line_offset;
  21230. label.h = row_height;
  21231. label.w = line_width;
  21232. label.x = pos_x;
  21233. if (!line_count)
  21234. label.x += x_offset;
  21235. if (is_selected)
  21236. nk_fill_rect(out, label, 0, background);
  21237. nk_widget_text(out, label, line, (int)((text + text_len) - line),
  21238. &txt, NK_TEXT_LEFT, font);
  21239. }}
  21240. }
  21241. NK_LIB nk_flags
  21242. nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
  21243. struct nk_rect bounds, nk_flags flags, nk_plugin_filter filter,
  21244. struct nk_text_edit *edit, const struct nk_style_edit *style,
  21245. struct nk_input *in, const struct nk_user_font *font)
  21246. {
  21247. struct nk_rect area;
  21248. nk_flags ret = 0;
  21249. float row_height;
  21250. char prev_state = 0;
  21251. char is_hovered = 0;
  21252. char select_all = 0;
  21253. char cursor_follow = 0;
  21254. struct nk_rect old_clip;
  21255. struct nk_rect clip;
  21256. NK_ASSERT(state);
  21257. NK_ASSERT(out);
  21258. NK_ASSERT(style);
  21259. if (!state || !out || !style)
  21260. return ret;
  21261. /* visible text area calculation */
  21262. area.x = bounds.x + style->padding.x + style->border;
  21263. area.y = bounds.y + style->padding.y + style->border;
  21264. area.w = bounds.w - (2.0f * style->padding.x + 2 * style->border);
  21265. area.h = bounds.h - (2.0f * style->padding.y + 2 * style->border);
  21266. if (flags & NK_EDIT_MULTILINE)
  21267. area.w = NK_MAX(0, area.w - style->scrollbar_size.x);
  21268. row_height = (flags & NK_EDIT_MULTILINE)? font->height + style->row_padding: area.h;
  21269. /* calculate clipping rectangle */
  21270. old_clip = out->clip;
  21271. nk_unify(&clip, &old_clip, area.x, area.y, area.x + area.w, area.y + area.h);
  21272. /* update edit state */
  21273. prev_state = (char)edit->active;
  21274. is_hovered = (char)nk_input_is_mouse_hovering_rect(in, bounds);
  21275. if (in && in->mouse.buttons[NK_BUTTON_LEFT].clicked && in->mouse.buttons[NK_BUTTON_LEFT].down) {
  21276. edit->active = NK_INBOX(in->mouse.pos.x, in->mouse.pos.y,
  21277. bounds.x, bounds.y, bounds.w, bounds.h);
  21278. }
  21279. /* (de)activate text editor */
  21280. if (!prev_state && edit->active) {
  21281. const enum nk_text_edit_type type = (flags & NK_EDIT_MULTILINE) ?
  21282. NK_TEXT_EDIT_MULTI_LINE: NK_TEXT_EDIT_SINGLE_LINE;
  21283. nk_textedit_clear_state(edit, type, filter);
  21284. if (flags & NK_EDIT_AUTO_SELECT)
  21285. select_all = nk_true;
  21286. if (flags & NK_EDIT_GOTO_END_ON_ACTIVATE) {
  21287. edit->cursor = edit->string.len;
  21288. in = 0;
  21289. }
  21290. } else if (!edit->active) edit->mode = NK_TEXT_EDIT_MODE_VIEW;
  21291. if (flags & NK_EDIT_READ_ONLY)
  21292. edit->mode = NK_TEXT_EDIT_MODE_VIEW;
  21293. else if (flags & NK_EDIT_ALWAYS_INSERT_MODE)
  21294. edit->mode = NK_TEXT_EDIT_MODE_INSERT;
  21295. ret = (edit->active) ? NK_EDIT_ACTIVE: NK_EDIT_INACTIVE;
  21296. if (prev_state != edit->active)
  21297. ret |= (edit->active) ? NK_EDIT_ACTIVATED: NK_EDIT_DEACTIVATED;
  21298. /* handle user input */
  21299. if (edit->active && in)
  21300. {
  21301. int shift_mod = in->keyboard.keys[NK_KEY_SHIFT].down;
  21302. const float mouse_x = (in->mouse.pos.x - area.x) + edit->scrollbar.x;
  21303. const float mouse_y = (in->mouse.pos.y - area.y) + edit->scrollbar.y;
  21304. /* mouse click handler */
  21305. is_hovered = (char)nk_input_is_mouse_hovering_rect(in, area);
  21306. if (select_all) {
  21307. nk_textedit_select_all(edit);
  21308. } else if (is_hovered && in->mouse.buttons[NK_BUTTON_LEFT].down &&
  21309. in->mouse.buttons[NK_BUTTON_LEFT].clicked) {
  21310. nk_textedit_click(edit, mouse_x, mouse_y, font, row_height);
  21311. } else if (is_hovered && in->mouse.buttons[NK_BUTTON_LEFT].down &&
  21312. (in->mouse.delta.x != 0.0f || in->mouse.delta.y != 0.0f)) {
  21313. nk_textedit_drag(edit, mouse_x, mouse_y, font, row_height);
  21314. cursor_follow = nk_true;
  21315. } else if (is_hovered && in->mouse.buttons[NK_BUTTON_RIGHT].clicked &&
  21316. in->mouse.buttons[NK_BUTTON_RIGHT].down) {
  21317. nk_textedit_key(edit, NK_KEY_TEXT_WORD_LEFT, nk_false, font, row_height);
  21318. nk_textedit_key(edit, NK_KEY_TEXT_WORD_RIGHT, nk_true, font, row_height);
  21319. cursor_follow = nk_true;
  21320. }
  21321. {int i; /* keyboard input */
  21322. int old_mode = edit->mode;
  21323. for (i = 0; i < NK_KEY_MAX; ++i) {
  21324. if (i == NK_KEY_ENTER || i == NK_KEY_TAB) continue; /* special case */
  21325. if (nk_input_is_key_pressed(in, (enum nk_keys)i)) {
  21326. nk_textedit_key(edit, (enum nk_keys)i, shift_mod, font, row_height);
  21327. cursor_follow = nk_true;
  21328. }
  21329. }
  21330. if (old_mode != edit->mode) {
  21331. in->keyboard.text_len = 0;
  21332. }}
  21333. /* text input */
  21334. edit->filter = filter;
  21335. if (in->keyboard.text_len) {
  21336. nk_textedit_text(edit, in->keyboard.text, in->keyboard.text_len);
  21337. cursor_follow = nk_true;
  21338. in->keyboard.text_len = 0;
  21339. }
  21340. /* enter key handler */
  21341. if (nk_input_is_key_pressed(in, NK_KEY_ENTER)) {
  21342. cursor_follow = nk_true;
  21343. if (flags & NK_EDIT_CTRL_ENTER_NEWLINE && shift_mod)
  21344. nk_textedit_text(edit, "\n", 1);
  21345. else if (flags & NK_EDIT_SIG_ENTER)
  21346. ret |= NK_EDIT_COMMITED;
  21347. else nk_textedit_text(edit, "\n", 1);
  21348. }
  21349. /* cut & copy handler */
  21350. {int copy= nk_input_is_key_pressed(in, NK_KEY_COPY);
  21351. int cut = nk_input_is_key_pressed(in, NK_KEY_CUT);
  21352. if ((copy || cut) && (flags & NK_EDIT_CLIPBOARD))
  21353. {
  21354. int glyph_len;
  21355. nk_rune unicode;
  21356. const char *text;
  21357. int b = edit->select_start;
  21358. int e = edit->select_end;
  21359. int begin = NK_MIN(b, e);
  21360. int end = NK_MAX(b, e);
  21361. text = nk_str_at_const(&edit->string, begin, &unicode, &glyph_len);
  21362. if (edit->clip.copy)
  21363. edit->clip.copy(edit->clip.userdata, text, end - begin);
  21364. if (cut && !(flags & NK_EDIT_READ_ONLY)){
  21365. nk_textedit_cut(edit);
  21366. cursor_follow = nk_true;
  21367. }
  21368. }}
  21369. /* paste handler */
  21370. {int paste = nk_input_is_key_pressed(in, NK_KEY_PASTE);
  21371. if (paste && (flags & NK_EDIT_CLIPBOARD) && edit->clip.paste) {
  21372. edit->clip.paste(edit->clip.userdata, edit);
  21373. cursor_follow = nk_true;
  21374. }}
  21375. /* tab handler */
  21376. {int tab = nk_input_is_key_pressed(in, NK_KEY_TAB);
  21377. if (tab && (flags & NK_EDIT_ALLOW_TAB)) {
  21378. nk_textedit_text(edit, " ", 4);
  21379. cursor_follow = nk_true;
  21380. }}
  21381. }
  21382. /* set widget state */
  21383. if (edit->active)
  21384. *state = NK_WIDGET_STATE_ACTIVE;
  21385. else nk_widget_state_reset(state);
  21386. if (is_hovered)
  21387. *state |= NK_WIDGET_STATE_HOVERED;
  21388. /* DRAW EDIT */
  21389. {const char *text = nk_str_get_const(&edit->string);
  21390. int len = nk_str_len_char(&edit->string);
  21391. {/* select background colors/images */
  21392. const struct nk_style_item *background;
  21393. if (*state & NK_WIDGET_STATE_ACTIVED)
  21394. background = &style->active;
  21395. else if (*state & NK_WIDGET_STATE_HOVER)
  21396. background = &style->hover;
  21397. else background = &style->normal;
  21398. /* draw background frame */
  21399. if (background->type == NK_STYLE_ITEM_COLOR) {
  21400. nk_stroke_rect(out, bounds, style->rounding, style->border, style->border_color);
  21401. nk_fill_rect(out, bounds, style->rounding, background->data.color);
  21402. } else nk_draw_image(out, bounds, &background->data.image, nk_white);}
  21403. area.w = NK_MAX(0, area.w - style->cursor_size);
  21404. if (edit->active)
  21405. {
  21406. int total_lines = 1;
  21407. struct nk_vec2 text_size = nk_vec2(0,0);
  21408. /* text pointer positions */
  21409. const char *cursor_ptr = 0;
  21410. const char *select_begin_ptr = 0;
  21411. const char *select_end_ptr = 0;
  21412. /* 2D pixel positions */
  21413. struct nk_vec2 cursor_pos = nk_vec2(0,0);
  21414. struct nk_vec2 selection_offset_start = nk_vec2(0,0);
  21415. struct nk_vec2 selection_offset_end = nk_vec2(0,0);
  21416. int selection_begin = NK_MIN(edit->select_start, edit->select_end);
  21417. int selection_end = NK_MAX(edit->select_start, edit->select_end);
  21418. /* calculate total line count + total space + cursor/selection position */
  21419. float line_width = 0.0f;
  21420. if (text && len)
  21421. {
  21422. /* utf8 encoding */
  21423. float glyph_width;
  21424. int glyph_len = 0;
  21425. nk_rune unicode = 0;
  21426. int text_len = 0;
  21427. int glyphs = 0;
  21428. int row_begin = 0;
  21429. glyph_len = nk_utf_decode(text, &unicode, len);
  21430. glyph_width = font->width(font->userdata, font->height, text, glyph_len);
  21431. line_width = 0;
  21432. /* iterate all lines */
  21433. while ((text_len < len) && glyph_len)
  21434. {
  21435. /* set cursor 2D position and line */
  21436. if (!cursor_ptr && glyphs == edit->cursor)
  21437. {
  21438. int glyph_offset;
  21439. struct nk_vec2 out_offset;
  21440. struct nk_vec2 row_size;
  21441. const char *remaining;
  21442. /* calculate 2d position */
  21443. cursor_pos.y = (float)(total_lines-1) * row_height;
  21444. row_size = nk_text_calculate_text_bounds(font, text+row_begin,
  21445. text_len-row_begin, row_height, &remaining,
  21446. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  21447. cursor_pos.x = row_size.x;
  21448. cursor_ptr = text + text_len;
  21449. }
  21450. /* set start selection 2D position and line */
  21451. if (!select_begin_ptr && edit->select_start != edit->select_end &&
  21452. glyphs == selection_begin)
  21453. {
  21454. int glyph_offset;
  21455. struct nk_vec2 out_offset;
  21456. struct nk_vec2 row_size;
  21457. const char *remaining;
  21458. /* calculate 2d position */
  21459. selection_offset_start.y = (float)(NK_MAX(total_lines-1,0)) * row_height;
  21460. row_size = nk_text_calculate_text_bounds(font, text+row_begin,
  21461. text_len-row_begin, row_height, &remaining,
  21462. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  21463. selection_offset_start.x = row_size.x;
  21464. select_begin_ptr = text + text_len;
  21465. }
  21466. /* set end selection 2D position and line */
  21467. if (!select_end_ptr && edit->select_start != edit->select_end &&
  21468. glyphs == selection_end)
  21469. {
  21470. int glyph_offset;
  21471. struct nk_vec2 out_offset;
  21472. struct nk_vec2 row_size;
  21473. const char *remaining;
  21474. /* calculate 2d position */
  21475. selection_offset_end.y = (float)(total_lines-1) * row_height;
  21476. row_size = nk_text_calculate_text_bounds(font, text+row_begin,
  21477. text_len-row_begin, row_height, &remaining,
  21478. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  21479. selection_offset_end.x = row_size.x;
  21480. select_end_ptr = text + text_len;
  21481. }
  21482. if (unicode == '\n') {
  21483. text_size.x = NK_MAX(text_size.x, line_width);
  21484. total_lines++;
  21485. line_width = 0;
  21486. text_len++;
  21487. glyphs++;
  21488. row_begin = text_len;
  21489. glyph_len = nk_utf_decode(text + text_len, &unicode, len-text_len);
  21490. glyph_width = font->width(font->userdata, font->height, text+text_len, glyph_len);
  21491. continue;
  21492. }
  21493. glyphs++;
  21494. text_len += glyph_len;
  21495. line_width += (float)glyph_width;
  21496. glyph_len = nk_utf_decode(text + text_len, &unicode, len-text_len);
  21497. glyph_width = font->width(font->userdata, font->height,
  21498. text+text_len, glyph_len);
  21499. continue;
  21500. }
  21501. text_size.y = (float)total_lines * row_height;
  21502. /* handle case when cursor is at end of text buffer */
  21503. if (!cursor_ptr && edit->cursor == edit->string.len) {
  21504. cursor_pos.x = line_width;
  21505. cursor_pos.y = text_size.y - row_height;
  21506. }
  21507. }
  21508. {
  21509. /* scrollbar */
  21510. if (cursor_follow)
  21511. {
  21512. /* update scrollbar to follow cursor */
  21513. if (!(flags & NK_EDIT_NO_HORIZONTAL_SCROLL)) {
  21514. /* horizontal scroll */
  21515. const float scroll_increment = area.w * 0.25f;
  21516. if (cursor_pos.x < edit->scrollbar.x)
  21517. edit->scrollbar.x = (float)(int)NK_MAX(0.0f, cursor_pos.x - scroll_increment);
  21518. if (cursor_pos.x >= edit->scrollbar.x + area.w)
  21519. edit->scrollbar.x = (float)(int)NK_MAX(0.0f, edit->scrollbar.x + scroll_increment);
  21520. } else edit->scrollbar.x = 0;
  21521. if (flags & NK_EDIT_MULTILINE) {
  21522. /* vertical scroll */
  21523. if (cursor_pos.y < edit->scrollbar.y)
  21524. edit->scrollbar.y = NK_MAX(0.0f, cursor_pos.y - row_height);
  21525. if (cursor_pos.y >= edit->scrollbar.y + area.h)
  21526. edit->scrollbar.y = edit->scrollbar.y + row_height;
  21527. } else edit->scrollbar.y = 0;
  21528. }
  21529. /* scrollbar widget */
  21530. if (flags & NK_EDIT_MULTILINE)
  21531. {
  21532. nk_flags ws;
  21533. struct nk_rect scroll;
  21534. float scroll_target;
  21535. float scroll_offset;
  21536. float scroll_step;
  21537. float scroll_inc;
  21538. scroll = area;
  21539. scroll.x = (bounds.x + bounds.w - style->border) - style->scrollbar_size.x;
  21540. scroll.w = style->scrollbar_size.x;
  21541. scroll_offset = edit->scrollbar.y;
  21542. scroll_step = scroll.h * 0.10f;
  21543. scroll_inc = scroll.h * 0.01f;
  21544. scroll_target = text_size.y;
  21545. edit->scrollbar.y = nk_do_scrollbarv(&ws, out, scroll, 0,
  21546. scroll_offset, scroll_target, scroll_step, scroll_inc,
  21547. &style->scrollbar, in, font);
  21548. }
  21549. }
  21550. /* draw text */
  21551. {struct nk_color background_color;
  21552. struct nk_color text_color;
  21553. struct nk_color sel_background_color;
  21554. struct nk_color sel_text_color;
  21555. struct nk_color cursor_color;
  21556. struct nk_color cursor_text_color;
  21557. const struct nk_style_item *background;
  21558. nk_push_scissor(out, clip);
  21559. /* select correct colors to draw */
  21560. if (*state & NK_WIDGET_STATE_ACTIVED) {
  21561. background = &style->active;
  21562. text_color = style->text_active;
  21563. sel_text_color = style->selected_text_hover;
  21564. sel_background_color = style->selected_hover;
  21565. cursor_color = style->cursor_hover;
  21566. cursor_text_color = style->cursor_text_hover;
  21567. } else if (*state & NK_WIDGET_STATE_HOVER) {
  21568. background = &style->hover;
  21569. text_color = style->text_hover;
  21570. sel_text_color = style->selected_text_hover;
  21571. sel_background_color = style->selected_hover;
  21572. cursor_text_color = style->cursor_text_hover;
  21573. cursor_color = style->cursor_hover;
  21574. } else {
  21575. background = &style->normal;
  21576. text_color = style->text_normal;
  21577. sel_text_color = style->selected_text_normal;
  21578. sel_background_color = style->selected_normal;
  21579. cursor_color = style->cursor_normal;
  21580. cursor_text_color = style->cursor_text_normal;
  21581. }
  21582. if (background->type == NK_STYLE_ITEM_IMAGE)
  21583. background_color = nk_rgba(0,0,0,0);
  21584. else background_color = background->data.color;
  21585. if (edit->select_start == edit->select_end) {
  21586. /* no selection so just draw the complete text */
  21587. const char *begin = nk_str_get_const(&edit->string);
  21588. int l = nk_str_len_char(&edit->string);
  21589. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  21590. area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
  21591. background_color, text_color, nk_false);
  21592. } else {
  21593. /* edit has selection so draw 1-3 text chunks */
  21594. if (edit->select_start != edit->select_end && selection_begin > 0){
  21595. /* draw unselected text before selection */
  21596. const char *begin = nk_str_get_const(&edit->string);
  21597. NK_ASSERT(select_begin_ptr);
  21598. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  21599. area.y - edit->scrollbar.y, 0, begin, (int)(select_begin_ptr - begin),
  21600. row_height, font, background_color, text_color, nk_false);
  21601. }
  21602. if (edit->select_start != edit->select_end) {
  21603. /* draw selected text */
  21604. NK_ASSERT(select_begin_ptr);
  21605. if (!select_end_ptr) {
  21606. const char *begin = nk_str_get_const(&edit->string);
  21607. select_end_ptr = begin + nk_str_len_char(&edit->string);
  21608. }
  21609. nk_edit_draw_text(out, style,
  21610. area.x - edit->scrollbar.x,
  21611. area.y + selection_offset_start.y - edit->scrollbar.y,
  21612. selection_offset_start.x,
  21613. select_begin_ptr, (int)(select_end_ptr - select_begin_ptr),
  21614. row_height, font, sel_background_color, sel_text_color, nk_true);
  21615. }
  21616. if ((edit->select_start != edit->select_end &&
  21617. selection_end < edit->string.len))
  21618. {
  21619. /* draw unselected text after selected text */
  21620. const char *begin = select_end_ptr;
  21621. const char *end = nk_str_get_const(&edit->string) +
  21622. nk_str_len_char(&edit->string);
  21623. NK_ASSERT(select_end_ptr);
  21624. nk_edit_draw_text(out, style,
  21625. area.x - edit->scrollbar.x,
  21626. area.y + selection_offset_end.y - edit->scrollbar.y,
  21627. selection_offset_end.x,
  21628. begin, (int)(end - begin), row_height, font,
  21629. background_color, text_color, nk_true);
  21630. }
  21631. }
  21632. /* cursor */
  21633. if (edit->select_start == edit->select_end)
  21634. {
  21635. if (edit->cursor >= nk_str_len(&edit->string) ||
  21636. (cursor_ptr && *cursor_ptr == '\n')) {
  21637. /* draw cursor at end of line */
  21638. struct nk_rect cursor;
  21639. cursor.w = style->cursor_size;
  21640. cursor.h = font->height;
  21641. cursor.x = area.x + cursor_pos.x - edit->scrollbar.x;
  21642. cursor.y = area.y + cursor_pos.y + row_height/2.0f - cursor.h/2.0f;
  21643. cursor.y -= edit->scrollbar.y;
  21644. nk_fill_rect(out, cursor, 0, cursor_color);
  21645. } else {
  21646. /* draw cursor inside text */
  21647. int glyph_len;
  21648. struct nk_rect label;
  21649. struct nk_text txt;
  21650. nk_rune unicode;
  21651. NK_ASSERT(cursor_ptr);
  21652. glyph_len = nk_utf_decode(cursor_ptr, &unicode, 4);
  21653. label.x = area.x + cursor_pos.x - edit->scrollbar.x;
  21654. label.y = area.y + cursor_pos.y - edit->scrollbar.y;
  21655. label.w = font->width(font->userdata, font->height, cursor_ptr, glyph_len);
  21656. label.h = row_height;
  21657. txt.padding = nk_vec2(0,0);
  21658. txt.background = cursor_color;;
  21659. txt.text = cursor_text_color;
  21660. nk_fill_rect(out, label, 0, cursor_color);
  21661. nk_widget_text(out, label, cursor_ptr, glyph_len, &txt, NK_TEXT_LEFT, font);
  21662. }
  21663. }}
  21664. } else {
  21665. /* not active so just draw text */
  21666. int l = nk_str_len_char(&edit->string);
  21667. const char *begin = nk_str_get_const(&edit->string);
  21668. const struct nk_style_item *background;
  21669. struct nk_color background_color;
  21670. struct nk_color text_color;
  21671. nk_push_scissor(out, clip);
  21672. if (*state & NK_WIDGET_STATE_ACTIVED) {
  21673. background = &style->active;
  21674. text_color = style->text_active;
  21675. } else if (*state & NK_WIDGET_STATE_HOVER) {
  21676. background = &style->hover;
  21677. text_color = style->text_hover;
  21678. } else {
  21679. background = &style->normal;
  21680. text_color = style->text_normal;
  21681. }
  21682. if (background->type == NK_STYLE_ITEM_IMAGE)
  21683. background_color = nk_rgba(0,0,0,0);
  21684. else background_color = background->data.color;
  21685. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  21686. area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
  21687. background_color, text_color, nk_false);
  21688. }
  21689. nk_push_scissor(out, old_clip);}
  21690. return ret;
  21691. }
  21692. NK_API void
  21693. nk_edit_focus(struct nk_context *ctx, nk_flags flags)
  21694. {
  21695. nk_hash hash;
  21696. struct nk_window *win;
  21697. NK_ASSERT(ctx);
  21698. NK_ASSERT(ctx->current);
  21699. if (!ctx || !ctx->current) return;
  21700. win = ctx->current;
  21701. hash = win->edit.seq;
  21702. win->edit.active = nk_true;
  21703. win->edit.name = hash;
  21704. if (flags & NK_EDIT_ALWAYS_INSERT_MODE)
  21705. win->edit.mode = NK_TEXT_EDIT_MODE_INSERT;
  21706. }
  21707. NK_API void
  21708. nk_edit_unfocus(struct nk_context *ctx)
  21709. {
  21710. struct nk_window *win;
  21711. NK_ASSERT(ctx);
  21712. NK_ASSERT(ctx->current);
  21713. if (!ctx || !ctx->current) return;
  21714. win = ctx->current;
  21715. win->edit.active = nk_false;
  21716. win->edit.name = 0;
  21717. }
  21718. NK_API nk_flags
  21719. nk_edit_string(struct nk_context *ctx, nk_flags flags,
  21720. char *memory, int *len, int max, nk_plugin_filter filter)
  21721. {
  21722. nk_hash hash;
  21723. nk_flags state;
  21724. struct nk_text_edit *edit;
  21725. struct nk_window *win;
  21726. NK_ASSERT(ctx);
  21727. NK_ASSERT(memory);
  21728. NK_ASSERT(len);
  21729. if (!ctx || !memory || !len)
  21730. return 0;
  21731. filter = (!filter) ? nk_filter_default: filter;
  21732. win = ctx->current;
  21733. hash = win->edit.seq;
  21734. edit = &ctx->text_edit;
  21735. nk_textedit_clear_state(&ctx->text_edit, (flags & NK_EDIT_MULTILINE)?
  21736. NK_TEXT_EDIT_MULTI_LINE: NK_TEXT_EDIT_SINGLE_LINE, filter);
  21737. if (win->edit.active && hash == win->edit.name) {
  21738. if (flags & NK_EDIT_NO_CURSOR)
  21739. edit->cursor = nk_utf_len(memory, *len);
  21740. else edit->cursor = win->edit.cursor;
  21741. if (!(flags & NK_EDIT_SELECTABLE)) {
  21742. edit->select_start = win->edit.cursor;
  21743. edit->select_end = win->edit.cursor;
  21744. } else {
  21745. edit->select_start = win->edit.sel_start;
  21746. edit->select_end = win->edit.sel_end;
  21747. }
  21748. edit->mode = win->edit.mode;
  21749. edit->scrollbar.x = (float)win->edit.scrollbar.x;
  21750. edit->scrollbar.y = (float)win->edit.scrollbar.y;
  21751. edit->active = nk_true;
  21752. } else edit->active = nk_false;
  21753. max = NK_MAX(1, max);
  21754. *len = NK_MIN(*len, max-1);
  21755. nk_str_init_fixed(&edit->string, memory, (nk_size)max);
  21756. edit->string.buffer.allocated = (nk_size)*len;
  21757. edit->string.len = nk_utf_len(memory, *len);
  21758. state = nk_edit_buffer(ctx, flags, edit, filter);
  21759. *len = (int)edit->string.buffer.allocated;
  21760. if (edit->active) {
  21761. win->edit.cursor = edit->cursor;
  21762. win->edit.sel_start = edit->select_start;
  21763. win->edit.sel_end = edit->select_end;
  21764. win->edit.mode = edit->mode;
  21765. win->edit.scrollbar.x = (nk_uint)edit->scrollbar.x;
  21766. win->edit.scrollbar.y = (nk_uint)edit->scrollbar.y;
  21767. } return state;
  21768. }
  21769. NK_API nk_flags
  21770. nk_edit_buffer(struct nk_context *ctx, nk_flags flags,
  21771. struct nk_text_edit *edit, nk_plugin_filter filter)
  21772. {
  21773. struct nk_window *win;
  21774. struct nk_style *style;
  21775. struct nk_input *in;
  21776. enum nk_widget_layout_states state;
  21777. struct nk_rect bounds;
  21778. nk_flags ret_flags = 0;
  21779. unsigned char prev_state;
  21780. nk_hash hash;
  21781. /* make sure correct values */
  21782. NK_ASSERT(ctx);
  21783. NK_ASSERT(edit);
  21784. NK_ASSERT(ctx->current);
  21785. NK_ASSERT(ctx->current->layout);
  21786. if (!ctx || !ctx->current || !ctx->current->layout)
  21787. return 0;
  21788. win = ctx->current;
  21789. style = &ctx->style;
  21790. state = nk_widget(&bounds, ctx);
  21791. if (!state) return state;
  21792. in = (win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  21793. /* check if edit is currently hot item */
  21794. hash = win->edit.seq++;
  21795. if (win->edit.active && hash == win->edit.name) {
  21796. if (flags & NK_EDIT_NO_CURSOR)
  21797. edit->cursor = edit->string.len;
  21798. if (!(flags & NK_EDIT_SELECTABLE)) {
  21799. edit->select_start = edit->cursor;
  21800. edit->select_end = edit->cursor;
  21801. }
  21802. if (flags & NK_EDIT_CLIPBOARD)
  21803. edit->clip = ctx->clip;
  21804. edit->active = (unsigned char)win->edit.active;
  21805. } else edit->active = nk_false;
  21806. edit->mode = win->edit.mode;
  21807. filter = (!filter) ? nk_filter_default: filter;
  21808. prev_state = (unsigned char)edit->active;
  21809. in = (flags & NK_EDIT_READ_ONLY) ? 0: in;
  21810. ret_flags = nk_do_edit(&ctx->last_widget_state, &win->buffer, bounds, flags,
  21811. filter, edit, &style->edit, in, style->font);
  21812. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  21813. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_TEXT];
  21814. if (edit->active && prev_state != edit->active) {
  21815. /* current edit is now hot */
  21816. win->edit.active = nk_true;
  21817. win->edit.name = hash;
  21818. } else if (prev_state && !edit->active) {
  21819. /* current edit is now cold */
  21820. win->edit.active = nk_false;
  21821. } return ret_flags;
  21822. }
  21823. NK_API nk_flags
  21824. nk_edit_string_zero_terminated(struct nk_context *ctx, nk_flags flags,
  21825. char *buffer, int max, nk_plugin_filter filter)
  21826. {
  21827. nk_flags result;
  21828. int len = nk_strlen(buffer);
  21829. result = nk_edit_string(ctx, flags, buffer, &len, max, filter);
  21830. buffer[NK_MIN(NK_MAX(max-1,0), len)] = '\0';
  21831. return result;
  21832. }
  21833. /* ===============================================================
  21834. *
  21835. * PROPERTY
  21836. *
  21837. * ===============================================================*/
  21838. NK_LIB void
  21839. nk_drag_behavior(nk_flags *state, const struct nk_input *in,
  21840. struct nk_rect drag, struct nk_property_variant *variant,
  21841. float inc_per_pixel)
  21842. {
  21843. int left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  21844. int left_mouse_click_in_cursor = in &&
  21845. nk_input_has_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, drag, nk_true);
  21846. nk_widget_state_reset(state);
  21847. if (nk_input_is_mouse_hovering_rect(in, drag))
  21848. *state = NK_WIDGET_STATE_HOVERED;
  21849. if (left_mouse_down && left_mouse_click_in_cursor) {
  21850. float delta, pixels;
  21851. pixels = in->mouse.delta.x;
  21852. delta = pixels * inc_per_pixel;
  21853. switch (variant->kind) {
  21854. default: break;
  21855. case NK_PROPERTY_INT:
  21856. variant->value.i = variant->value.i + (int)delta;
  21857. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
  21858. break;
  21859. case NK_PROPERTY_FLOAT:
  21860. variant->value.f = variant->value.f + (float)delta;
  21861. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
  21862. break;
  21863. case NK_PROPERTY_DOUBLE:
  21864. variant->value.d = variant->value.d + (double)delta;
  21865. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
  21866. break;
  21867. }
  21868. *state = NK_WIDGET_STATE_ACTIVE;
  21869. }
  21870. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, drag))
  21871. *state |= NK_WIDGET_STATE_ENTERED;
  21872. else if (nk_input_is_mouse_prev_hovering_rect(in, drag))
  21873. *state |= NK_WIDGET_STATE_LEFT;
  21874. }
  21875. NK_LIB void
  21876. nk_property_behavior(nk_flags *ws, const struct nk_input *in,
  21877. struct nk_rect property, struct nk_rect label, struct nk_rect edit,
  21878. struct nk_rect empty, int *state, struct nk_property_variant *variant,
  21879. float inc_per_pixel)
  21880. {
  21881. if (in && *state == NK_PROPERTY_DEFAULT) {
  21882. if (nk_button_behavior(ws, edit, in, NK_BUTTON_DEFAULT))
  21883. *state = NK_PROPERTY_EDIT;
  21884. else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, label, nk_true))
  21885. *state = NK_PROPERTY_DRAG;
  21886. else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, empty, nk_true))
  21887. *state = NK_PROPERTY_DRAG;
  21888. }
  21889. if (*state == NK_PROPERTY_DRAG) {
  21890. nk_drag_behavior(ws, in, property, variant, inc_per_pixel);
  21891. if (!(*ws & NK_WIDGET_STATE_ACTIVED)) *state = NK_PROPERTY_DEFAULT;
  21892. }
  21893. }
  21894. NK_LIB void
  21895. nk_draw_property(struct nk_command_buffer *out, const struct nk_style_property *style,
  21896. const struct nk_rect *bounds, const struct nk_rect *label, nk_flags state,
  21897. const char *name, int len, const struct nk_user_font *font)
  21898. {
  21899. struct nk_text text;
  21900. const struct nk_style_item *background;
  21901. /* select correct background and text color */
  21902. if (state & NK_WIDGET_STATE_ACTIVED) {
  21903. background = &style->active;
  21904. text.text = style->label_active;
  21905. } else if (state & NK_WIDGET_STATE_HOVER) {
  21906. background = &style->hover;
  21907. text.text = style->label_hover;
  21908. } else {
  21909. background = &style->normal;
  21910. text.text = style->label_normal;
  21911. }
  21912. /* draw background */
  21913. if (background->type == NK_STYLE_ITEM_IMAGE) {
  21914. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  21915. text.background = nk_rgba(0,0,0,0);
  21916. } else {
  21917. text.background = background->data.color;
  21918. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  21919. nk_stroke_rect(out, *bounds, style->rounding, style->border, background->data.color);
  21920. }
  21921. /* draw label */
  21922. text.padding = nk_vec2(0,0);
  21923. nk_widget_text(out, *label, name, len, &text, NK_TEXT_CENTERED, font);
  21924. }
  21925. NK_LIB void
  21926. nk_do_property(nk_flags *ws,
  21927. struct nk_command_buffer *out, struct nk_rect property,
  21928. const char *name, struct nk_property_variant *variant,
  21929. float inc_per_pixel, char *buffer, int *len,
  21930. int *state, int *cursor, int *select_begin, int *select_end,
  21931. const struct nk_style_property *style,
  21932. enum nk_property_filter filter, struct nk_input *in,
  21933. const struct nk_user_font *font, struct nk_text_edit *text_edit,
  21934. enum nk_button_behavior behavior)
  21935. {
  21936. const nk_plugin_filter filters[] = {
  21937. nk_filter_decimal,
  21938. nk_filter_float
  21939. };
  21940. int active, old;
  21941. int num_len, name_len;
  21942. char string[NK_MAX_NUMBER_BUFFER];
  21943. float size;
  21944. char *dst = 0;
  21945. int *length;
  21946. struct nk_rect left;
  21947. struct nk_rect right;
  21948. struct nk_rect label;
  21949. struct nk_rect edit;
  21950. struct nk_rect empty;
  21951. /* left decrement button */
  21952. left.h = font->height/2;
  21953. left.w = left.h;
  21954. left.x = property.x + style->border + style->padding.x;
  21955. left.y = property.y + style->border + property.h/2.0f - left.h/2;
  21956. /* text label */
  21957. name_len = nk_strlen(name);
  21958. size = font->width(font->userdata, font->height, name, name_len);
  21959. label.x = left.x + left.w + style->padding.x;
  21960. label.w = (float)size + 2 * style->padding.x;
  21961. label.y = property.y + style->border + style->padding.y;
  21962. label.h = property.h - (2 * style->border + 2 * style->padding.y);
  21963. /* right increment button */
  21964. right.y = left.y;
  21965. right.w = left.w;
  21966. right.h = left.h;
  21967. right.x = property.x + property.w - (right.w + style->padding.x);
  21968. /* edit */
  21969. if (*state == NK_PROPERTY_EDIT) {
  21970. size = font->width(font->userdata, font->height, buffer, *len);
  21971. size += style->edit.cursor_size;
  21972. length = len;
  21973. dst = buffer;
  21974. } else {
  21975. switch (variant->kind) {
  21976. default: break;
  21977. case NK_PROPERTY_INT:
  21978. nk_itoa(string, variant->value.i);
  21979. num_len = nk_strlen(string);
  21980. break;
  21981. case NK_PROPERTY_FLOAT:
  21982. NK_DTOA(string, (double)variant->value.f);
  21983. num_len = nk_string_float_limit(string, NK_MAX_FLOAT_PRECISION);
  21984. break;
  21985. case NK_PROPERTY_DOUBLE:
  21986. NK_DTOA(string, variant->value.d);
  21987. num_len = nk_string_float_limit(string, NK_MAX_FLOAT_PRECISION);
  21988. break;
  21989. }
  21990. size = font->width(font->userdata, font->height, string, num_len);
  21991. dst = string;
  21992. length = &num_len;
  21993. }
  21994. edit.w = (float)size + 2 * style->padding.x;
  21995. edit.w = NK_MIN(edit.w, right.x - (label.x + label.w));
  21996. edit.x = right.x - (edit.w + style->padding.x);
  21997. edit.y = property.y + style->border;
  21998. edit.h = property.h - (2 * style->border);
  21999. /* empty left space activator */
  22000. empty.w = edit.x - (label.x + label.w);
  22001. empty.x = label.x + label.w;
  22002. empty.y = property.y;
  22003. empty.h = property.h;
  22004. /* update property */
  22005. old = (*state == NK_PROPERTY_EDIT);
  22006. nk_property_behavior(ws, in, property, label, edit, empty, state, variant, inc_per_pixel);
  22007. /* draw property */
  22008. if (style->draw_begin) style->draw_begin(out, style->userdata);
  22009. nk_draw_property(out, style, &property, &label, *ws, name, name_len, font);
  22010. if (style->draw_end) style->draw_end(out, style->userdata);
  22011. /* execute right button */
  22012. if (nk_do_button_symbol(ws, out, left, style->sym_left, behavior, &style->dec_button, in, font)) {
  22013. switch (variant->kind) {
  22014. default: break;
  22015. case NK_PROPERTY_INT:
  22016. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i - variant->step.i, variant->max_value.i); break;
  22017. case NK_PROPERTY_FLOAT:
  22018. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f - variant->step.f, variant->max_value.f); break;
  22019. case NK_PROPERTY_DOUBLE:
  22020. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d - variant->step.d, variant->max_value.d); break;
  22021. }
  22022. }
  22023. /* execute left button */
  22024. if (nk_do_button_symbol(ws, out, right, style->sym_right, behavior, &style->inc_button, in, font)) {
  22025. switch (variant->kind) {
  22026. default: break;
  22027. case NK_PROPERTY_INT:
  22028. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i + variant->step.i, variant->max_value.i); break;
  22029. case NK_PROPERTY_FLOAT:
  22030. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f + variant->step.f, variant->max_value.f); break;
  22031. case NK_PROPERTY_DOUBLE:
  22032. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d + variant->step.d, variant->max_value.d); break;
  22033. }
  22034. }
  22035. if (old != NK_PROPERTY_EDIT && (*state == NK_PROPERTY_EDIT)) {
  22036. /* property has been activated so setup buffer */
  22037. NK_MEMCPY(buffer, dst, (nk_size)*length);
  22038. *cursor = nk_utf_len(buffer, *length);
  22039. *len = *length;
  22040. length = len;
  22041. dst = buffer;
  22042. active = 0;
  22043. } else active = (*state == NK_PROPERTY_EDIT);
  22044. /* execute and run text edit field */
  22045. nk_textedit_clear_state(text_edit, NK_TEXT_EDIT_SINGLE_LINE, filters[filter]);
  22046. text_edit->active = (unsigned char)active;
  22047. text_edit->string.len = *length;
  22048. text_edit->cursor = NK_CLAMP(0, *cursor, *length);
  22049. text_edit->select_start = NK_CLAMP(0,*select_begin, *length);
  22050. text_edit->select_end = NK_CLAMP(0,*select_end, *length);
  22051. text_edit->string.buffer.allocated = (nk_size)*length;
  22052. text_edit->string.buffer.memory.size = NK_MAX_NUMBER_BUFFER;
  22053. text_edit->string.buffer.memory.ptr = dst;
  22054. text_edit->string.buffer.size = NK_MAX_NUMBER_BUFFER;
  22055. text_edit->mode = NK_TEXT_EDIT_MODE_INSERT;
  22056. nk_do_edit(ws, out, edit, NK_EDIT_FIELD|NK_EDIT_AUTO_SELECT,
  22057. filters[filter], text_edit, &style->edit, (*state == NK_PROPERTY_EDIT) ? in: 0, font);
  22058. *length = text_edit->string.len;
  22059. *cursor = text_edit->cursor;
  22060. *select_begin = text_edit->select_start;
  22061. *select_end = text_edit->select_end;
  22062. if (text_edit->active && nk_input_is_key_pressed(in, NK_KEY_ENTER))
  22063. text_edit->active = nk_false;
  22064. if (active && !text_edit->active) {
  22065. /* property is now not active so convert edit text to value*/
  22066. *state = NK_PROPERTY_DEFAULT;
  22067. buffer[*len] = '\0';
  22068. switch (variant->kind) {
  22069. default: break;
  22070. case NK_PROPERTY_INT:
  22071. variant->value.i = nk_strtoi(buffer, 0);
  22072. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
  22073. break;
  22074. case NK_PROPERTY_FLOAT:
  22075. nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
  22076. variant->value.f = nk_strtof(buffer, 0);
  22077. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
  22078. break;
  22079. case NK_PROPERTY_DOUBLE:
  22080. nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
  22081. variant->value.d = nk_strtod(buffer, 0);
  22082. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
  22083. break;
  22084. }
  22085. }
  22086. }
  22087. NK_LIB struct nk_property_variant
  22088. nk_property_variant_int(int value, int min_value, int max_value, int step)
  22089. {
  22090. struct nk_property_variant result;
  22091. result.kind = NK_PROPERTY_INT;
  22092. result.value.i = value;
  22093. result.min_value.i = min_value;
  22094. result.max_value.i = max_value;
  22095. result.step.i = step;
  22096. return result;
  22097. }
  22098. NK_LIB struct nk_property_variant
  22099. nk_property_variant_float(float value, float min_value, float max_value, float step)
  22100. {
  22101. struct nk_property_variant result;
  22102. result.kind = NK_PROPERTY_FLOAT;
  22103. result.value.f = value;
  22104. result.min_value.f = min_value;
  22105. result.max_value.f = max_value;
  22106. result.step.f = step;
  22107. return result;
  22108. }
  22109. NK_LIB struct nk_property_variant
  22110. nk_property_variant_double(double value, double min_value, double max_value,
  22111. double step)
  22112. {
  22113. struct nk_property_variant result;
  22114. result.kind = NK_PROPERTY_DOUBLE;
  22115. result.value.d = value;
  22116. result.min_value.d = min_value;
  22117. result.max_value.d = max_value;
  22118. result.step.d = step;
  22119. return result;
  22120. }
  22121. NK_LIB void
  22122. nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant *variant,
  22123. float inc_per_pixel, const enum nk_property_filter filter)
  22124. {
  22125. struct nk_window *win;
  22126. struct nk_panel *layout;
  22127. struct nk_input *in;
  22128. const struct nk_style *style;
  22129. struct nk_rect bounds;
  22130. enum nk_widget_layout_states s;
  22131. int *state = 0;
  22132. nk_hash hash = 0;
  22133. char *buffer = 0;
  22134. int *len = 0;
  22135. int *cursor = 0;
  22136. int *select_begin = 0;
  22137. int *select_end = 0;
  22138. int old_state;
  22139. char dummy_buffer[NK_MAX_NUMBER_BUFFER];
  22140. int dummy_state = NK_PROPERTY_DEFAULT;
  22141. int dummy_length = 0;
  22142. int dummy_cursor = 0;
  22143. int dummy_select_begin = 0;
  22144. int dummy_select_end = 0;
  22145. NK_ASSERT(ctx);
  22146. NK_ASSERT(ctx->current);
  22147. NK_ASSERT(ctx->current->layout);
  22148. if (!ctx || !ctx->current || !ctx->current->layout)
  22149. return;
  22150. win = ctx->current;
  22151. layout = win->layout;
  22152. style = &ctx->style;
  22153. s = nk_widget(&bounds, ctx);
  22154. if (!s) return;
  22155. /* calculate hash from name */
  22156. if (name[0] == '#') {
  22157. hash = nk_murmur_hash(name, (int)nk_strlen(name), win->property.seq++);
  22158. name++; /* special number hash */
  22159. } else hash = nk_murmur_hash(name, (int)nk_strlen(name), 42);
  22160. /* check if property is currently hot item */
  22161. if (win->property.active && hash == win->property.name) {
  22162. buffer = win->property.buffer;
  22163. len = &win->property.length;
  22164. cursor = &win->property.cursor;
  22165. state = &win->property.state;
  22166. select_begin = &win->property.select_start;
  22167. select_end = &win->property.select_end;
  22168. } else {
  22169. buffer = dummy_buffer;
  22170. len = &dummy_length;
  22171. cursor = &dummy_cursor;
  22172. state = &dummy_state;
  22173. select_begin = &dummy_select_begin;
  22174. select_end = &dummy_select_end;
  22175. }
  22176. /* execute property widget */
  22177. old_state = *state;
  22178. ctx->text_edit.clip = ctx->clip;
  22179. in = ((s == NK_WIDGET_ROM && !win->property.active) ||
  22180. layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22181. nk_do_property(&ctx->last_widget_state, &win->buffer, bounds, name,
  22182. variant, inc_per_pixel, buffer, len, state, cursor, select_begin,
  22183. select_end, &style->property, filter, in, style->font, &ctx->text_edit,
  22184. ctx->button_behavior);
  22185. if (in && *state != NK_PROPERTY_DEFAULT && !win->property.active) {
  22186. /* current property is now hot */
  22187. win->property.active = 1;
  22188. NK_MEMCPY(win->property.buffer, buffer, (nk_size)*len);
  22189. win->property.length = *len;
  22190. win->property.cursor = *cursor;
  22191. win->property.state = *state;
  22192. win->property.name = hash;
  22193. win->property.select_start = *select_begin;
  22194. win->property.select_end = *select_end;
  22195. if (*state == NK_PROPERTY_DRAG) {
  22196. ctx->input.mouse.grab = nk_true;
  22197. ctx->input.mouse.grabbed = nk_true;
  22198. }
  22199. }
  22200. /* check if previously active property is now inactive */
  22201. if (*state == NK_PROPERTY_DEFAULT && old_state != NK_PROPERTY_DEFAULT) {
  22202. if (old_state == NK_PROPERTY_DRAG) {
  22203. ctx->input.mouse.grab = nk_false;
  22204. ctx->input.mouse.grabbed = nk_false;
  22205. ctx->input.mouse.ungrab = nk_true;
  22206. }
  22207. win->property.select_start = 0;
  22208. win->property.select_end = 0;
  22209. win->property.active = 0;
  22210. }
  22211. }
  22212. NK_API void
  22213. nk_property_int(struct nk_context *ctx, const char *name,
  22214. int min, int *val, int max, int step, float inc_per_pixel)
  22215. {
  22216. struct nk_property_variant variant;
  22217. NK_ASSERT(ctx);
  22218. NK_ASSERT(name);
  22219. NK_ASSERT(val);
  22220. if (!ctx || !ctx->current || !name || !val) return;
  22221. variant = nk_property_variant_int(*val, min, max, step);
  22222. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
  22223. *val = variant.value.i;
  22224. }
  22225. NK_API void
  22226. nk_property_float(struct nk_context *ctx, const char *name,
  22227. float min, float *val, float max, float step, float inc_per_pixel)
  22228. {
  22229. struct nk_property_variant variant;
  22230. NK_ASSERT(ctx);
  22231. NK_ASSERT(name);
  22232. NK_ASSERT(val);
  22233. if (!ctx || !ctx->current || !name || !val) return;
  22234. variant = nk_property_variant_float(*val, min, max, step);
  22235. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  22236. *val = variant.value.f;
  22237. }
  22238. NK_API void
  22239. nk_property_double(struct nk_context *ctx, const char *name,
  22240. double min, double *val, double max, double step, float inc_per_pixel)
  22241. {
  22242. struct nk_property_variant variant;
  22243. NK_ASSERT(ctx);
  22244. NK_ASSERT(name);
  22245. NK_ASSERT(val);
  22246. if (!ctx || !ctx->current || !name || !val) return;
  22247. variant = nk_property_variant_double(*val, min, max, step);
  22248. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  22249. *val = variant.value.d;
  22250. }
  22251. NK_API int
  22252. nk_propertyi(struct nk_context *ctx, const char *name, int min, int val,
  22253. int max, int step, float inc_per_pixel)
  22254. {
  22255. struct nk_property_variant variant;
  22256. NK_ASSERT(ctx);
  22257. NK_ASSERT(name);
  22258. if (!ctx || !ctx->current || !name) return val;
  22259. variant = nk_property_variant_int(val, min, max, step);
  22260. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
  22261. val = variant.value.i;
  22262. return val;
  22263. }
  22264. NK_API float
  22265. nk_propertyf(struct nk_context *ctx, const char *name, float min,
  22266. float val, float max, float step, float inc_per_pixel)
  22267. {
  22268. struct nk_property_variant variant;
  22269. NK_ASSERT(ctx);
  22270. NK_ASSERT(name);
  22271. if (!ctx || !ctx->current || !name) return val;
  22272. variant = nk_property_variant_float(val, min, max, step);
  22273. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  22274. val = variant.value.f;
  22275. return val;
  22276. }
  22277. NK_API double
  22278. nk_propertyd(struct nk_context *ctx, const char *name, double min,
  22279. double val, double max, double step, float inc_per_pixel)
  22280. {
  22281. struct nk_property_variant variant;
  22282. NK_ASSERT(ctx);
  22283. NK_ASSERT(name);
  22284. if (!ctx || !ctx->current || !name) return val;
  22285. variant = nk_property_variant_double(val, min, max, step);
  22286. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  22287. val = variant.value.d;
  22288. return val;
  22289. }
  22290. /* ==============================================================
  22291. *
  22292. * CHART
  22293. *
  22294. * ===============================================================*/
  22295. NK_API int
  22296. nk_chart_begin_colored(struct nk_context *ctx, enum nk_chart_type type,
  22297. struct nk_color color, struct nk_color highlight,
  22298. int count, float min_value, float max_value)
  22299. {
  22300. struct nk_window *win;
  22301. struct nk_chart *chart;
  22302. const struct nk_style *config;
  22303. const struct nk_style_chart *style;
  22304. const struct nk_style_item *background;
  22305. struct nk_rect bounds = {0, 0, 0, 0};
  22306. NK_ASSERT(ctx);
  22307. NK_ASSERT(ctx->current);
  22308. NK_ASSERT(ctx->current->layout);
  22309. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  22310. if (!nk_widget(&bounds, ctx)) {
  22311. chart = &ctx->current->layout->chart;
  22312. nk_zero(chart, sizeof(*chart));
  22313. return 0;
  22314. }
  22315. win = ctx->current;
  22316. config = &ctx->style;
  22317. chart = &win->layout->chart;
  22318. style = &config->chart;
  22319. /* setup basic generic chart */
  22320. nk_zero(chart, sizeof(*chart));
  22321. chart->x = bounds.x + style->padding.x;
  22322. chart->y = bounds.y + style->padding.y;
  22323. chart->w = bounds.w - 2 * style->padding.x;
  22324. chart->h = bounds.h - 2 * style->padding.y;
  22325. chart->w = NK_MAX(chart->w, 2 * style->padding.x);
  22326. chart->h = NK_MAX(chart->h, 2 * style->padding.y);
  22327. /* add first slot into chart */
  22328. {struct nk_chart_slot *slot = &chart->slots[chart->slot++];
  22329. slot->type = type;
  22330. slot->count = count;
  22331. slot->color = color;
  22332. slot->highlight = highlight;
  22333. slot->min = NK_MIN(min_value, max_value);
  22334. slot->max = NK_MAX(min_value, max_value);
  22335. slot->range = slot->max - slot->min;}
  22336. /* draw chart background */
  22337. background = &style->background;
  22338. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22339. nk_draw_image(&win->buffer, bounds, &background->data.image, nk_white);
  22340. } else {
  22341. nk_fill_rect(&win->buffer, bounds, style->rounding, style->border_color);
  22342. nk_fill_rect(&win->buffer, nk_shrink_rect(bounds, style->border),
  22343. style->rounding, style->background.data.color);
  22344. }
  22345. return 1;
  22346. }
  22347. NK_API int
  22348. nk_chart_begin(struct nk_context *ctx, const enum nk_chart_type type,
  22349. int count, float min_value, float max_value)
  22350. {
  22351. return nk_chart_begin_colored(ctx, type, ctx->style.chart.color,
  22352. ctx->style.chart.selected_color, count, min_value, max_value);
  22353. }
  22354. NK_API void
  22355. nk_chart_add_slot_colored(struct nk_context *ctx, const enum nk_chart_type type,
  22356. struct nk_color color, struct nk_color highlight,
  22357. int count, float min_value, float max_value)
  22358. {
  22359. NK_ASSERT(ctx);
  22360. NK_ASSERT(ctx->current);
  22361. NK_ASSERT(ctx->current->layout);
  22362. NK_ASSERT(ctx->current->layout->chart.slot < NK_CHART_MAX_SLOT);
  22363. if (!ctx || !ctx->current || !ctx->current->layout) return;
  22364. if (ctx->current->layout->chart.slot >= NK_CHART_MAX_SLOT) return;
  22365. /* add another slot into the graph */
  22366. {struct nk_chart *chart = &ctx->current->layout->chart;
  22367. struct nk_chart_slot *slot = &chart->slots[chart->slot++];
  22368. slot->type = type;
  22369. slot->count = count;
  22370. slot->color = color;
  22371. slot->highlight = highlight;
  22372. slot->min = NK_MIN(min_value, max_value);
  22373. slot->max = NK_MAX(min_value, max_value);
  22374. slot->range = slot->max - slot->min;}
  22375. }
  22376. NK_API void
  22377. nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type type,
  22378. int count, float min_value, float max_value)
  22379. {
  22380. nk_chart_add_slot_colored(ctx, type, ctx->style.chart.color,
  22381. ctx->style.chart.selected_color, count, min_value, max_value);
  22382. }
  22383. NK_INTERN nk_flags
  22384. nk_chart_push_line(struct nk_context *ctx, struct nk_window *win,
  22385. struct nk_chart *g, float value, int slot)
  22386. {
  22387. struct nk_panel *layout = win->layout;
  22388. const struct nk_input *i = &ctx->input;
  22389. struct nk_command_buffer *out = &win->buffer;
  22390. nk_flags ret = 0;
  22391. struct nk_vec2 cur;
  22392. struct nk_rect bounds;
  22393. struct nk_color color;
  22394. float step;
  22395. float range;
  22396. float ratio;
  22397. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  22398. step = g->w / (float)g->slots[slot].count;
  22399. range = g->slots[slot].max - g->slots[slot].min;
  22400. ratio = (value - g->slots[slot].min) / range;
  22401. if (g->slots[slot].index == 0) {
  22402. /* first data point does not have a connection */
  22403. g->slots[slot].last.x = g->x;
  22404. g->slots[slot].last.y = (g->y + g->h) - ratio * (float)g->h;
  22405. bounds.x = g->slots[slot].last.x - 2;
  22406. bounds.y = g->slots[slot].last.y - 2;
  22407. bounds.w = bounds.h = 4;
  22408. color = g->slots[slot].color;
  22409. if (!(layout->flags & NK_WINDOW_ROM) &&
  22410. NK_INBOX(i->mouse.pos.x,i->mouse.pos.y, g->slots[slot].last.x-3, g->slots[slot].last.y-3, 6, 6)){
  22411. ret = nk_input_is_mouse_hovering_rect(i, bounds) ? NK_CHART_HOVERING : 0;
  22412. ret |= (i->mouse.buttons[NK_BUTTON_LEFT].down &&
  22413. i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
  22414. color = g->slots[slot].highlight;
  22415. }
  22416. nk_fill_rect(out, bounds, 0, color);
  22417. g->slots[slot].index += 1;
  22418. return ret;
  22419. }
  22420. /* draw a line between the last data point and the new one */
  22421. color = g->slots[slot].color;
  22422. cur.x = g->x + (float)(step * (float)g->slots[slot].index);
  22423. cur.y = (g->y + g->h) - (ratio * (float)g->h);
  22424. nk_stroke_line(out, g->slots[slot].last.x, g->slots[slot].last.y, cur.x, cur.y, 1.0f, color);
  22425. bounds.x = cur.x - 3;
  22426. bounds.y = cur.y - 3;
  22427. bounds.w = bounds.h = 6;
  22428. /* user selection of current data point */
  22429. if (!(layout->flags & NK_WINDOW_ROM)) {
  22430. if (nk_input_is_mouse_hovering_rect(i, bounds)) {
  22431. ret = NK_CHART_HOVERING;
  22432. ret |= (!i->mouse.buttons[NK_BUTTON_LEFT].down &&
  22433. i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
  22434. color = g->slots[slot].highlight;
  22435. }
  22436. }
  22437. nk_fill_rect(out, nk_rect(cur.x - 2, cur.y - 2, 4, 4), 0, color);
  22438. /* save current data point position */
  22439. g->slots[slot].last.x = cur.x;
  22440. g->slots[slot].last.y = cur.y;
  22441. g->slots[slot].index += 1;
  22442. return ret;
  22443. }
  22444. NK_INTERN nk_flags
  22445. nk_chart_push_column(const struct nk_context *ctx, struct nk_window *win,
  22446. struct nk_chart *chart, float value, int slot)
  22447. {
  22448. struct nk_command_buffer *out = &win->buffer;
  22449. const struct nk_input *in = &ctx->input;
  22450. struct nk_panel *layout = win->layout;
  22451. float ratio;
  22452. nk_flags ret = 0;
  22453. struct nk_color color;
  22454. struct nk_rect item = {0,0,0,0};
  22455. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  22456. if (chart->slots[slot].index >= chart->slots[slot].count)
  22457. return nk_false;
  22458. if (chart->slots[slot].count) {
  22459. float padding = (float)(chart->slots[slot].count-1);
  22460. item.w = (chart->w - padding) / (float)(chart->slots[slot].count);
  22461. }
  22462. /* calculate bounds of current bar chart entry */
  22463. color = chart->slots[slot].color;;
  22464. item.h = chart->h * NK_ABS((value/chart->slots[slot].range));
  22465. if (value >= 0) {
  22466. ratio = (value + NK_ABS(chart->slots[slot].min)) / NK_ABS(chart->slots[slot].range);
  22467. item.y = (chart->y + chart->h) - chart->h * ratio;
  22468. } else {
  22469. ratio = (value - chart->slots[slot].max) / chart->slots[slot].range;
  22470. item.y = chart->y + (chart->h * NK_ABS(ratio)) - item.h;
  22471. }
  22472. item.x = chart->x + ((float)chart->slots[slot].index * item.w);
  22473. item.x = item.x + ((float)chart->slots[slot].index);
  22474. /* user chart bar selection */
  22475. if (!(layout->flags & NK_WINDOW_ROM) &&
  22476. NK_INBOX(in->mouse.pos.x,in->mouse.pos.y,item.x,item.y,item.w,item.h)) {
  22477. ret = NK_CHART_HOVERING;
  22478. ret |= (!in->mouse.buttons[NK_BUTTON_LEFT].down &&
  22479. in->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
  22480. color = chart->slots[slot].highlight;
  22481. }
  22482. nk_fill_rect(out, item, 0, color);
  22483. chart->slots[slot].index += 1;
  22484. return ret;
  22485. }
  22486. NK_API nk_flags
  22487. nk_chart_push_slot(struct nk_context *ctx, float value, int slot)
  22488. {
  22489. nk_flags flags;
  22490. struct nk_window *win;
  22491. NK_ASSERT(ctx);
  22492. NK_ASSERT(ctx->current);
  22493. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  22494. NK_ASSERT(slot < ctx->current->layout->chart.slot);
  22495. if (!ctx || !ctx->current || slot >= NK_CHART_MAX_SLOT) return nk_false;
  22496. if (slot >= ctx->current->layout->chart.slot) return nk_false;
  22497. win = ctx->current;
  22498. if (win->layout->chart.slot < slot) return nk_false;
  22499. switch (win->layout->chart.slots[slot].type) {
  22500. case NK_CHART_LINES:
  22501. flags = nk_chart_push_line(ctx, win, &win->layout->chart, value, slot); break;
  22502. case NK_CHART_COLUMN:
  22503. flags = nk_chart_push_column(ctx, win, &win->layout->chart, value, slot); break;
  22504. default:
  22505. case NK_CHART_MAX:
  22506. flags = 0;
  22507. }
  22508. return flags;
  22509. }
  22510. NK_API nk_flags
  22511. nk_chart_push(struct nk_context *ctx, float value)
  22512. {
  22513. return nk_chart_push_slot(ctx, value, 0);
  22514. }
  22515. NK_API void
  22516. nk_chart_end(struct nk_context *ctx)
  22517. {
  22518. struct nk_window *win;
  22519. struct nk_chart *chart;
  22520. NK_ASSERT(ctx);
  22521. NK_ASSERT(ctx->current);
  22522. if (!ctx || !ctx->current)
  22523. return;
  22524. win = ctx->current;
  22525. chart = &win->layout->chart;
  22526. NK_MEMSET(chart, 0, sizeof(*chart));
  22527. return;
  22528. }
  22529. NK_API void
  22530. nk_plot(struct nk_context *ctx, enum nk_chart_type type, const float *values,
  22531. int count, int offset)
  22532. {
  22533. int i = 0;
  22534. float min_value;
  22535. float max_value;
  22536. NK_ASSERT(ctx);
  22537. NK_ASSERT(values);
  22538. if (!ctx || !values || !count) return;
  22539. min_value = values[offset];
  22540. max_value = values[offset];
  22541. for (i = 0; i < count; ++i) {
  22542. min_value = NK_MIN(values[i + offset], min_value);
  22543. max_value = NK_MAX(values[i + offset], max_value);
  22544. }
  22545. if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
  22546. for (i = 0; i < count; ++i)
  22547. nk_chart_push(ctx, values[i + offset]);
  22548. nk_chart_end(ctx);
  22549. }
  22550. }
  22551. NK_API void
  22552. nk_plot_function(struct nk_context *ctx, enum nk_chart_type type, void *userdata,
  22553. float(*value_getter)(void* user, int index), int count, int offset)
  22554. {
  22555. int i = 0;
  22556. float min_value;
  22557. float max_value;
  22558. NK_ASSERT(ctx);
  22559. NK_ASSERT(value_getter);
  22560. if (!ctx || !value_getter || !count) return;
  22561. max_value = min_value = value_getter(userdata, offset);
  22562. for (i = 0; i < count; ++i) {
  22563. float value = value_getter(userdata, i + offset);
  22564. min_value = NK_MIN(value, min_value);
  22565. max_value = NK_MAX(value, max_value);
  22566. }
  22567. if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
  22568. for (i = 0; i < count; ++i)
  22569. nk_chart_push(ctx, value_getter(userdata, i + offset));
  22570. nk_chart_end(ctx);
  22571. }
  22572. }
  22573. /* ==============================================================
  22574. *
  22575. * COLOR PICKER
  22576. *
  22577. * ===============================================================*/
  22578. NK_LIB int
  22579. nk_color_picker_behavior(nk_flags *state,
  22580. const struct nk_rect *bounds, const struct nk_rect *matrix,
  22581. const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
  22582. struct nk_colorf *color, const struct nk_input *in)
  22583. {
  22584. float hsva[4];
  22585. int value_changed = 0;
  22586. int hsv_changed = 0;
  22587. NK_ASSERT(state);
  22588. NK_ASSERT(matrix);
  22589. NK_ASSERT(hue_bar);
  22590. NK_ASSERT(color);
  22591. /* color matrix */
  22592. nk_colorf_hsva_fv(hsva, *color);
  22593. if (nk_button_behavior(state, *matrix, in, NK_BUTTON_REPEATER)) {
  22594. hsva[1] = NK_SATURATE((in->mouse.pos.x - matrix->x) / (matrix->w-1));
  22595. hsva[2] = 1.0f - NK_SATURATE((in->mouse.pos.y - matrix->y) / (matrix->h-1));
  22596. value_changed = hsv_changed = 1;
  22597. }
  22598. /* hue bar */
  22599. if (nk_button_behavior(state, *hue_bar, in, NK_BUTTON_REPEATER)) {
  22600. hsva[0] = NK_SATURATE((in->mouse.pos.y - hue_bar->y) / (hue_bar->h-1));
  22601. value_changed = hsv_changed = 1;
  22602. }
  22603. /* alpha bar */
  22604. if (alpha_bar) {
  22605. if (nk_button_behavior(state, *alpha_bar, in, NK_BUTTON_REPEATER)) {
  22606. hsva[3] = 1.0f - NK_SATURATE((in->mouse.pos.y - alpha_bar->y) / (alpha_bar->h-1));
  22607. value_changed = 1;
  22608. }
  22609. }
  22610. nk_widget_state_reset(state);
  22611. if (hsv_changed) {
  22612. *color = nk_hsva_colorfv(hsva);
  22613. *state = NK_WIDGET_STATE_ACTIVE;
  22614. }
  22615. if (value_changed) {
  22616. color->a = hsva[3];
  22617. *state = NK_WIDGET_STATE_ACTIVE;
  22618. }
  22619. /* set color picker widget state */
  22620. if (nk_input_is_mouse_hovering_rect(in, *bounds))
  22621. *state = NK_WIDGET_STATE_HOVERED;
  22622. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *bounds))
  22623. *state |= NK_WIDGET_STATE_ENTERED;
  22624. else if (nk_input_is_mouse_prev_hovering_rect(in, *bounds))
  22625. *state |= NK_WIDGET_STATE_LEFT;
  22626. return value_changed;
  22627. }
  22628. NK_LIB void
  22629. nk_draw_color_picker(struct nk_command_buffer *o, const struct nk_rect *matrix,
  22630. const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
  22631. struct nk_colorf col)
  22632. {
  22633. NK_STORAGE const struct nk_color black = {0,0,0,255};
  22634. NK_STORAGE const struct nk_color white = {255, 255, 255, 255};
  22635. NK_STORAGE const struct nk_color black_trans = {0,0,0,0};
  22636. const float crosshair_size = 7.0f;
  22637. struct nk_color temp;
  22638. float hsva[4];
  22639. float line_y;
  22640. int i;
  22641. NK_ASSERT(o);
  22642. NK_ASSERT(matrix);
  22643. NK_ASSERT(hue_bar);
  22644. /* draw hue bar */
  22645. nk_colorf_hsva_fv(hsva, col);
  22646. for (i = 0; i < 6; ++i) {
  22647. NK_GLOBAL const struct nk_color hue_colors[] = {
  22648. {255, 0, 0, 255}, {255,255,0,255}, {0,255,0,255}, {0, 255,255,255},
  22649. {0,0,255,255}, {255, 0, 255, 255}, {255, 0, 0, 255}
  22650. };
  22651. nk_fill_rect_multi_color(o,
  22652. nk_rect(hue_bar->x, hue_bar->y + (float)i * (hue_bar->h/6.0f) + 0.5f,
  22653. hue_bar->w, (hue_bar->h/6.0f) + 0.5f), hue_colors[i], hue_colors[i],
  22654. hue_colors[i+1], hue_colors[i+1]);
  22655. }
  22656. line_y = (float)(int)(hue_bar->y + hsva[0] * matrix->h + 0.5f);
  22657. nk_stroke_line(o, hue_bar->x-1, line_y, hue_bar->x + hue_bar->w + 2,
  22658. line_y, 1, nk_rgb(255,255,255));
  22659. /* draw alpha bar */
  22660. if (alpha_bar) {
  22661. float alpha = NK_SATURATE(col.a);
  22662. line_y = (float)(int)(alpha_bar->y + (1.0f - alpha) * matrix->h + 0.5f);
  22663. nk_fill_rect_multi_color(o, *alpha_bar, white, white, black, black);
  22664. nk_stroke_line(o, alpha_bar->x-1, line_y, alpha_bar->x + alpha_bar->w + 2,
  22665. line_y, 1, nk_rgb(255,255,255));
  22666. }
  22667. /* draw color matrix */
  22668. temp = nk_hsv_f(hsva[0], 1.0f, 1.0f);
  22669. nk_fill_rect_multi_color(o, *matrix, white, temp, temp, white);
  22670. nk_fill_rect_multi_color(o, *matrix, black_trans, black_trans, black, black);
  22671. /* draw cross-hair */
  22672. {struct nk_vec2 p; float S = hsva[1]; float V = hsva[2];
  22673. p.x = (float)(int)(matrix->x + S * matrix->w);
  22674. p.y = (float)(int)(matrix->y + (1.0f - V) * matrix->h);
  22675. nk_stroke_line(o, p.x - crosshair_size, p.y, p.x-2, p.y, 1.0f, white);
  22676. nk_stroke_line(o, p.x + crosshair_size + 1, p.y, p.x+3, p.y, 1.0f, white);
  22677. nk_stroke_line(o, p.x, p.y + crosshair_size + 1, p.x, p.y+3, 1.0f, white);
  22678. nk_stroke_line(o, p.x, p.y - crosshair_size, p.x, p.y-2, 1.0f, white);}
  22679. }
  22680. NK_LIB int
  22681. nk_do_color_picker(nk_flags *state,
  22682. struct nk_command_buffer *out, struct nk_colorf *col,
  22683. enum nk_color_format fmt, struct nk_rect bounds,
  22684. struct nk_vec2 padding, const struct nk_input *in,
  22685. const struct nk_user_font *font)
  22686. {
  22687. int ret = 0;
  22688. struct nk_rect matrix;
  22689. struct nk_rect hue_bar;
  22690. struct nk_rect alpha_bar;
  22691. float bar_w;
  22692. NK_ASSERT(out);
  22693. NK_ASSERT(col);
  22694. NK_ASSERT(state);
  22695. NK_ASSERT(font);
  22696. if (!out || !col || !state || !font)
  22697. return ret;
  22698. bar_w = font->height;
  22699. bounds.x += padding.x;
  22700. bounds.y += padding.x;
  22701. bounds.w -= 2 * padding.x;
  22702. bounds.h -= 2 * padding.y;
  22703. matrix.x = bounds.x;
  22704. matrix.y = bounds.y;
  22705. matrix.h = bounds.h;
  22706. matrix.w = bounds.w - (3 * padding.x + 2 * bar_w);
  22707. hue_bar.w = bar_w;
  22708. hue_bar.y = bounds.y;
  22709. hue_bar.h = matrix.h;
  22710. hue_bar.x = matrix.x + matrix.w + padding.x;
  22711. alpha_bar.x = hue_bar.x + hue_bar.w + padding.x;
  22712. alpha_bar.y = bounds.y;
  22713. alpha_bar.w = bar_w;
  22714. alpha_bar.h = matrix.h;
  22715. ret = nk_color_picker_behavior(state, &bounds, &matrix, &hue_bar,
  22716. (fmt == NK_RGBA) ? &alpha_bar:0, col, in);
  22717. nk_draw_color_picker(out, &matrix, &hue_bar, (fmt == NK_RGBA) ? &alpha_bar:0, *col);
  22718. return ret;
  22719. }
  22720. NK_API int
  22721. nk_color_pick(struct nk_context * ctx, struct nk_colorf *color,
  22722. enum nk_color_format fmt)
  22723. {
  22724. struct nk_window *win;
  22725. struct nk_panel *layout;
  22726. const struct nk_style *config;
  22727. const struct nk_input *in;
  22728. enum nk_widget_layout_states state;
  22729. struct nk_rect bounds;
  22730. NK_ASSERT(ctx);
  22731. NK_ASSERT(color);
  22732. NK_ASSERT(ctx->current);
  22733. NK_ASSERT(ctx->current->layout);
  22734. if (!ctx || !ctx->current || !ctx->current->layout || !color)
  22735. return 0;
  22736. win = ctx->current;
  22737. config = &ctx->style;
  22738. layout = win->layout;
  22739. state = nk_widget(&bounds, ctx);
  22740. if (!state) return 0;
  22741. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22742. return nk_do_color_picker(&ctx->last_widget_state, &win->buffer, color, fmt, bounds,
  22743. nk_vec2(0,0), in, config->font);
  22744. }
  22745. NK_API struct nk_colorf
  22746. nk_color_picker(struct nk_context *ctx, struct nk_colorf color,
  22747. enum nk_color_format fmt)
  22748. {
  22749. nk_color_pick(ctx, &color, fmt);
  22750. return color;
  22751. }
  22752. /* ==============================================================
  22753. *
  22754. * COMBO
  22755. *
  22756. * ===============================================================*/
  22757. NK_INTERN int
  22758. nk_combo_begin(struct nk_context *ctx, struct nk_window *win,
  22759. struct nk_vec2 size, int is_clicked, struct nk_rect header)
  22760. {
  22761. struct nk_window *popup;
  22762. int is_open = 0;
  22763. int is_active = 0;
  22764. struct nk_rect body;
  22765. nk_hash hash;
  22766. NK_ASSERT(ctx);
  22767. NK_ASSERT(ctx->current);
  22768. NK_ASSERT(ctx->current->layout);
  22769. if (!ctx || !ctx->current || !ctx->current->layout)
  22770. return 0;
  22771. popup = win->popup.win;
  22772. body.x = header.x;
  22773. body.w = size.x;
  22774. body.y = header.y + header.h-ctx->style.window.combo_border;
  22775. body.h = size.y;
  22776. hash = win->popup.combo_count++;
  22777. is_open = (popup) ? nk_true:nk_false;
  22778. is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_COMBO);
  22779. if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
  22780. (!is_open && !is_active && !is_clicked)) return 0;
  22781. if (!nk_nonblock_begin(ctx, 0, body,
  22782. (is_clicked && is_open)?nk_rect(0,0,0,0):header, NK_PANEL_COMBO)) return 0;
  22783. win->popup.type = NK_PANEL_COMBO;
  22784. win->popup.name = hash;
  22785. return 1;
  22786. }
  22787. NK_API int
  22788. nk_combo_begin_text(struct nk_context *ctx, const char *selected, int len,
  22789. struct nk_vec2 size)
  22790. {
  22791. const struct nk_input *in;
  22792. struct nk_window *win;
  22793. struct nk_style *style;
  22794. enum nk_widget_layout_states s;
  22795. int is_clicked = nk_false;
  22796. struct nk_rect header;
  22797. const struct nk_style_item *background;
  22798. struct nk_text text;
  22799. NK_ASSERT(ctx);
  22800. NK_ASSERT(selected);
  22801. NK_ASSERT(ctx->current);
  22802. NK_ASSERT(ctx->current->layout);
  22803. if (!ctx || !ctx->current || !ctx->current->layout || !selected)
  22804. return 0;
  22805. win = ctx->current;
  22806. style = &ctx->style;
  22807. s = nk_widget(&header, ctx);
  22808. if (s == NK_WIDGET_INVALID)
  22809. 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. text.text = style->combo.label_active;
  22817. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  22818. background = &style->combo.hover;
  22819. text.text = style->combo.label_hover;
  22820. } else {
  22821. background = &style->combo.normal;
  22822. text.text = style->combo.label_normal;
  22823. }
  22824. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22825. text.background = nk_rgba(0,0,0,0);
  22826. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  22827. } else {
  22828. text.background = background->data.color;
  22829. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  22830. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  22831. }
  22832. {
  22833. /* print currently selected text item */
  22834. struct nk_rect label;
  22835. struct nk_rect button;
  22836. struct nk_rect content;
  22837. enum nk_symbol_type sym;
  22838. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22839. sym = style->combo.sym_hover;
  22840. else if (is_clicked)
  22841. sym = style->combo.sym_active;
  22842. else sym = style->combo.sym_normal;
  22843. /* calculate button */
  22844. button.w = header.h - 2 * style->combo.button_padding.y;
  22845. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  22846. button.y = header.y + style->combo.button_padding.y;
  22847. button.h = button.w;
  22848. content.x = button.x + style->combo.button.padding.x;
  22849. content.y = button.y + style->combo.button.padding.y;
  22850. content.w = button.w - 2 * style->combo.button.padding.x;
  22851. content.h = button.h - 2 * style->combo.button.padding.y;
  22852. /* draw selected label */
  22853. text.padding = nk_vec2(0,0);
  22854. label.x = header.x + style->combo.content_padding.x;
  22855. label.y = header.y + style->combo.content_padding.y;
  22856. label.w = button.x - (style->combo.content_padding.x + style->combo.spacing.x) - label.x;;
  22857. label.h = header.h - 2 * style->combo.content_padding.y;
  22858. nk_widget_text(&win->buffer, label, selected, len, &text,
  22859. NK_TEXT_LEFT, ctx->style.font);
  22860. /* draw open/close button */
  22861. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  22862. &ctx->style.combo.button, sym, style->font);
  22863. }
  22864. return nk_combo_begin(ctx, win, size, is_clicked, header);
  22865. }
  22866. NK_API int
  22867. nk_combo_begin_label(struct nk_context *ctx, const char *selected, struct nk_vec2 size)
  22868. {
  22869. return nk_combo_begin_text(ctx, selected, nk_strlen(selected), size);
  22870. }
  22871. NK_API int
  22872. nk_combo_begin_color(struct nk_context *ctx, struct nk_color color, struct nk_vec2 size)
  22873. {
  22874. struct nk_window *win;
  22875. struct nk_style *style;
  22876. const struct nk_input *in;
  22877. struct nk_rect header;
  22878. int is_clicked = nk_false;
  22879. enum nk_widget_layout_states s;
  22880. const struct nk_style_item *background;
  22881. NK_ASSERT(ctx);
  22882. NK_ASSERT(ctx->current);
  22883. NK_ASSERT(ctx->current->layout);
  22884. if (!ctx || !ctx->current || !ctx->current->layout)
  22885. return 0;
  22886. win = ctx->current;
  22887. style = &ctx->style;
  22888. s = nk_widget(&header, ctx);
  22889. if (s == NK_WIDGET_INVALID)
  22890. return 0;
  22891. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  22892. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  22893. is_clicked = nk_true;
  22894. /* draw combo box header background and border */
  22895. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
  22896. background = &style->combo.active;
  22897. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22898. background = &style->combo.hover;
  22899. else background = &style->combo.normal;
  22900. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22901. nk_draw_image(&win->buffer, header, &background->data.image,nk_white);
  22902. } else {
  22903. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  22904. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  22905. }
  22906. {
  22907. struct nk_rect content;
  22908. struct nk_rect button;
  22909. struct nk_rect bounds;
  22910. enum nk_symbol_type sym;
  22911. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22912. sym = style->combo.sym_hover;
  22913. else if (is_clicked)
  22914. sym = style->combo.sym_active;
  22915. else sym = style->combo.sym_normal;
  22916. /* calculate button */
  22917. button.w = header.h - 2 * style->combo.button_padding.y;
  22918. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  22919. button.y = header.y + style->combo.button_padding.y;
  22920. button.h = button.w;
  22921. content.x = button.x + style->combo.button.padding.x;
  22922. content.y = button.y + style->combo.button.padding.y;
  22923. content.w = button.w - 2 * style->combo.button.padding.x;
  22924. content.h = button.h - 2 * style->combo.button.padding.y;
  22925. /* draw color */
  22926. bounds.h = header.h - 4 * style->combo.content_padding.y;
  22927. bounds.y = header.y + 2 * style->combo.content_padding.y;
  22928. bounds.x = header.x + 2 * style->combo.content_padding.x;
  22929. bounds.w = (button.x - (style->combo.content_padding.x + style->combo.spacing.x)) - bounds.x;
  22930. nk_fill_rect(&win->buffer, bounds, 0, color);
  22931. /* draw open/close button */
  22932. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  22933. &ctx->style.combo.button, sym, style->font);
  22934. }
  22935. return nk_combo_begin(ctx, win, size, is_clicked, header);
  22936. }
  22937. NK_API int
  22938. nk_combo_begin_symbol(struct nk_context *ctx, enum nk_symbol_type symbol, struct nk_vec2 size)
  22939. {
  22940. struct nk_window *win;
  22941. struct nk_style *style;
  22942. const struct nk_input *in;
  22943. struct nk_rect header;
  22944. int is_clicked = nk_false;
  22945. enum nk_widget_layout_states s;
  22946. const struct nk_style_item *background;
  22947. struct nk_color sym_background;
  22948. struct nk_color symbol_color;
  22949. NK_ASSERT(ctx);
  22950. NK_ASSERT(ctx->current);
  22951. NK_ASSERT(ctx->current->layout);
  22952. if (!ctx || !ctx->current || !ctx->current->layout)
  22953. return 0;
  22954. win = ctx->current;
  22955. style = &ctx->style;
  22956. s = nk_widget(&header, ctx);
  22957. if (s == NK_WIDGET_INVALID)
  22958. return 0;
  22959. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  22960. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  22961. is_clicked = nk_true;
  22962. /* draw combo box header background and border */
  22963. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  22964. background = &style->combo.active;
  22965. symbol_color = style->combo.symbol_active;
  22966. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  22967. background = &style->combo.hover;
  22968. symbol_color = style->combo.symbol_hover;
  22969. } else {
  22970. background = &style->combo.normal;
  22971. symbol_color = style->combo.symbol_hover;
  22972. }
  22973. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22974. sym_background = nk_rgba(0,0,0,0);
  22975. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  22976. } else {
  22977. sym_background = background->data.color;
  22978. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  22979. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  22980. }
  22981. {
  22982. struct nk_rect bounds = {0,0,0,0};
  22983. struct nk_rect content;
  22984. struct nk_rect button;
  22985. enum nk_symbol_type sym;
  22986. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22987. sym = style->combo.sym_hover;
  22988. else if (is_clicked)
  22989. sym = style->combo.sym_active;
  22990. else sym = style->combo.sym_normal;
  22991. /* calculate button */
  22992. button.w = header.h - 2 * style->combo.button_padding.y;
  22993. button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
  22994. button.y = header.y + style->combo.button_padding.y;
  22995. button.h = button.w;
  22996. content.x = button.x + style->combo.button.padding.x;
  22997. content.y = button.y + style->combo.button.padding.y;
  22998. content.w = button.w - 2 * style->combo.button.padding.x;
  22999. content.h = button.h - 2 * style->combo.button.padding.y;
  23000. /* draw symbol */
  23001. bounds.h = header.h - 2 * style->combo.content_padding.y;
  23002. bounds.y = header.y + style->combo.content_padding.y;
  23003. bounds.x = header.x + style->combo.content_padding.x;
  23004. bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
  23005. nk_draw_symbol(&win->buffer, symbol, bounds, sym_background, symbol_color,
  23006. 1.0f, style->font);
  23007. /* draw open/close button */
  23008. nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
  23009. &ctx->style.combo.button, sym, style->font);
  23010. }
  23011. return nk_combo_begin(ctx, win, size, is_clicked, header);
  23012. }
  23013. NK_API int
  23014. nk_combo_begin_symbol_text(struct nk_context *ctx, const char *selected, int len,
  23015. enum nk_symbol_type symbol, struct nk_vec2 size)
  23016. {
  23017. struct nk_window *win;
  23018. struct nk_style *style;
  23019. struct nk_input *in;
  23020. struct nk_rect header;
  23021. int is_clicked = nk_false;
  23022. enum nk_widget_layout_states s;
  23023. const struct nk_style_item *background;
  23024. struct nk_color symbol_color;
  23025. struct nk_text text;
  23026. NK_ASSERT(ctx);
  23027. NK_ASSERT(ctx->current);
  23028. NK_ASSERT(ctx->current->layout);
  23029. if (!ctx || !ctx->current || !ctx->current->layout)
  23030. return 0;
  23031. win = ctx->current;
  23032. style = &ctx->style;
  23033. s = nk_widget(&header, ctx);
  23034. if (!s) return 0;
  23035. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  23036. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  23037. is_clicked = nk_true;
  23038. /* draw combo box header background and border */
  23039. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  23040. background = &style->combo.active;
  23041. symbol_color = style->combo.symbol_active;
  23042. text.text = style->combo.label_active;
  23043. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  23044. background = &style->combo.hover;
  23045. symbol_color = style->combo.symbol_hover;
  23046. text.text = style->combo.label_hover;
  23047. } else {
  23048. background = &style->combo.normal;
  23049. symbol_color = style->combo.symbol_normal;
  23050. text.text = style->combo.label_normal;
  23051. }
  23052. if (background->type == NK_STYLE_ITEM_IMAGE) {
  23053. text.background = nk_rgba(0,0,0,0);
  23054. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  23055. } else {
  23056. text.background = background->data.color;
  23057. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  23058. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  23059. }
  23060. {
  23061. struct nk_rect content;
  23062. struct nk_rect button;
  23063. struct nk_rect label;
  23064. struct nk_rect image;
  23065. enum nk_symbol_type sym;
  23066. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  23067. sym = style->combo.sym_hover;
  23068. else if (is_clicked)
  23069. sym = style->combo.sym_active;
  23070. else sym = style->combo.sym_normal;
  23071. /* calculate button */
  23072. button.w = header.h - 2 * style->combo.button_padding.y;
  23073. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  23074. button.y = header.y + style->combo.button_padding.y;
  23075. button.h = button.w;
  23076. content.x = button.x + style->combo.button.padding.x;
  23077. content.y = button.y + style->combo.button.padding.y;
  23078. content.w = button.w - 2 * style->combo.button.padding.x;
  23079. content.h = button.h - 2 * style->combo.button.padding.y;
  23080. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  23081. &ctx->style.combo.button, sym, style->font);
  23082. /* draw symbol */
  23083. image.x = header.x + style->combo.content_padding.x;
  23084. image.y = header.y + style->combo.content_padding.y;
  23085. image.h = header.h - 2 * style->combo.content_padding.y;
  23086. image.w = image.h;
  23087. nk_draw_symbol(&win->buffer, symbol, image, text.background, symbol_color,
  23088. 1.0f, style->font);
  23089. /* draw label */
  23090. text.padding = nk_vec2(0,0);
  23091. label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
  23092. label.y = header.y + style->combo.content_padding.y;
  23093. label.w = (button.x - style->combo.content_padding.x) - label.x;
  23094. label.h = header.h - 2 * style->combo.content_padding.y;
  23095. nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
  23096. }
  23097. return nk_combo_begin(ctx, win, size, is_clicked, header);
  23098. }
  23099. NK_API int
  23100. nk_combo_begin_image(struct nk_context *ctx, struct nk_image img, struct nk_vec2 size)
  23101. {
  23102. struct nk_window *win;
  23103. struct nk_style *style;
  23104. const struct nk_input *in;
  23105. struct nk_rect header;
  23106. int is_clicked = nk_false;
  23107. enum nk_widget_layout_states s;
  23108. const struct nk_style_item *background;
  23109. NK_ASSERT(ctx);
  23110. NK_ASSERT(ctx->current);
  23111. NK_ASSERT(ctx->current->layout);
  23112. if (!ctx || !ctx->current || !ctx->current->layout)
  23113. return 0;
  23114. win = ctx->current;
  23115. style = &ctx->style;
  23116. s = nk_widget(&header, ctx);
  23117. if (s == NK_WIDGET_INVALID)
  23118. return 0;
  23119. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  23120. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  23121. is_clicked = nk_true;
  23122. /* draw combo box header background and border */
  23123. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
  23124. background = &style->combo.active;
  23125. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  23126. background = &style->combo.hover;
  23127. else background = &style->combo.normal;
  23128. if (background->type == NK_STYLE_ITEM_IMAGE) {
  23129. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  23130. } else {
  23131. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  23132. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  23133. }
  23134. {
  23135. struct nk_rect bounds = {0,0,0,0};
  23136. struct nk_rect content;
  23137. struct nk_rect button;
  23138. enum nk_symbol_type sym;
  23139. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  23140. sym = style->combo.sym_hover;
  23141. else if (is_clicked)
  23142. sym = style->combo.sym_active;
  23143. else sym = style->combo.sym_normal;
  23144. /* calculate button */
  23145. button.w = header.h - 2 * style->combo.button_padding.y;
  23146. button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
  23147. button.y = header.y + style->combo.button_padding.y;
  23148. button.h = button.w;
  23149. content.x = button.x + style->combo.button.padding.x;
  23150. content.y = button.y + style->combo.button.padding.y;
  23151. content.w = button.w - 2 * style->combo.button.padding.x;
  23152. content.h = button.h - 2 * style->combo.button.padding.y;
  23153. /* draw image */
  23154. bounds.h = header.h - 2 * style->combo.content_padding.y;
  23155. bounds.y = header.y + style->combo.content_padding.y;
  23156. bounds.x = header.x + style->combo.content_padding.x;
  23157. bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
  23158. nk_draw_image(&win->buffer, bounds, &img, nk_white);
  23159. /* draw open/close button */
  23160. nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
  23161. &ctx->style.combo.button, sym, style->font);
  23162. }
  23163. return nk_combo_begin(ctx, win, size, is_clicked, header);
  23164. }
  23165. NK_API int
  23166. nk_combo_begin_image_text(struct nk_context *ctx, const char *selected, int len,
  23167. struct nk_image img, struct nk_vec2 size)
  23168. {
  23169. struct nk_window *win;
  23170. struct nk_style *style;
  23171. struct nk_input *in;
  23172. struct nk_rect header;
  23173. int is_clicked = nk_false;
  23174. enum nk_widget_layout_states s;
  23175. const struct nk_style_item *background;
  23176. struct nk_text text;
  23177. NK_ASSERT(ctx);
  23178. NK_ASSERT(ctx->current);
  23179. NK_ASSERT(ctx->current->layout);
  23180. if (!ctx || !ctx->current || !ctx->current->layout)
  23181. return 0;
  23182. win = ctx->current;
  23183. style = &ctx->style;
  23184. s = nk_widget(&header, ctx);
  23185. if (!s) return 0;
  23186. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  23187. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  23188. is_clicked = nk_true;
  23189. /* draw combo box header background and border */
  23190. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  23191. background = &style->combo.active;
  23192. text.text = style->combo.label_active;
  23193. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  23194. background = &style->combo.hover;
  23195. text.text = style->combo.label_hover;
  23196. } else {
  23197. background = &style->combo.normal;
  23198. text.text = style->combo.label_normal;
  23199. }
  23200. if (background->type == NK_STYLE_ITEM_IMAGE) {
  23201. text.background = nk_rgba(0,0,0,0);
  23202. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  23203. } else {
  23204. text.background = background->data.color;
  23205. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  23206. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  23207. }
  23208. {
  23209. struct nk_rect content;
  23210. struct nk_rect button;
  23211. struct nk_rect label;
  23212. struct nk_rect image;
  23213. enum nk_symbol_type sym;
  23214. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  23215. sym = style->combo.sym_hover;
  23216. else if (is_clicked)
  23217. sym = style->combo.sym_active;
  23218. else sym = style->combo.sym_normal;
  23219. /* calculate button */
  23220. button.w = header.h - 2 * style->combo.button_padding.y;
  23221. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  23222. button.y = header.y + style->combo.button_padding.y;
  23223. button.h = button.w;
  23224. content.x = button.x + style->combo.button.padding.x;
  23225. content.y = button.y + style->combo.button.padding.y;
  23226. content.w = button.w - 2 * style->combo.button.padding.x;
  23227. content.h = button.h - 2 * style->combo.button.padding.y;
  23228. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  23229. &ctx->style.combo.button, sym, style->font);
  23230. /* draw image */
  23231. image.x = header.x + style->combo.content_padding.x;
  23232. image.y = header.y + style->combo.content_padding.y;
  23233. image.h = header.h - 2 * style->combo.content_padding.y;
  23234. image.w = image.h;
  23235. nk_draw_image(&win->buffer, image, &img, nk_white);
  23236. /* draw label */
  23237. text.padding = nk_vec2(0,0);
  23238. label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
  23239. label.y = header.y + style->combo.content_padding.y;
  23240. label.w = (button.x - style->combo.content_padding.x) - label.x;
  23241. label.h = header.h - 2 * style->combo.content_padding.y;
  23242. nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
  23243. }
  23244. return nk_combo_begin(ctx, win, size, is_clicked, header);
  23245. }
  23246. NK_API int
  23247. nk_combo_begin_symbol_label(struct nk_context *ctx,
  23248. const char *selected, enum nk_symbol_type type, struct nk_vec2 size)
  23249. {
  23250. return nk_combo_begin_symbol_text(ctx, selected, nk_strlen(selected), type, size);
  23251. }
  23252. NK_API int
  23253. nk_combo_begin_image_label(struct nk_context *ctx,
  23254. const char *selected, struct nk_image img, struct nk_vec2 size)
  23255. {
  23256. return nk_combo_begin_image_text(ctx, selected, nk_strlen(selected), img, size);
  23257. }
  23258. NK_API int
  23259. nk_combo_item_text(struct nk_context *ctx, const char *text, int len,nk_flags align)
  23260. {
  23261. return nk_contextual_item_text(ctx, text, len, align);
  23262. }
  23263. NK_API int
  23264. nk_combo_item_label(struct nk_context *ctx, const char *label, nk_flags align)
  23265. {
  23266. return nk_contextual_item_label(ctx, label, align);
  23267. }
  23268. NK_API int
  23269. nk_combo_item_image_text(struct nk_context *ctx, struct nk_image img, const char *text,
  23270. int len, nk_flags alignment)
  23271. {
  23272. return nk_contextual_item_image_text(ctx, img, text, len, alignment);
  23273. }
  23274. NK_API int
  23275. nk_combo_item_image_label(struct nk_context *ctx, struct nk_image img,
  23276. const char *text, nk_flags alignment)
  23277. {
  23278. return nk_contextual_item_image_label(ctx, img, text, alignment);
  23279. }
  23280. NK_API int
  23281. nk_combo_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  23282. const char *text, int len, nk_flags alignment)
  23283. {
  23284. return nk_contextual_item_symbol_text(ctx, sym, text, len, alignment);
  23285. }
  23286. NK_API int
  23287. nk_combo_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  23288. const char *label, nk_flags alignment)
  23289. {
  23290. return nk_contextual_item_symbol_label(ctx, sym, label, alignment);
  23291. }
  23292. NK_API void nk_combo_end(struct nk_context *ctx)
  23293. {
  23294. nk_contextual_end(ctx);
  23295. }
  23296. NK_API void nk_combo_close(struct nk_context *ctx)
  23297. {
  23298. nk_contextual_close(ctx);
  23299. }
  23300. NK_API int
  23301. nk_combo(struct nk_context *ctx, const char **items, int count,
  23302. int selected, int item_height, struct nk_vec2 size)
  23303. {
  23304. int i = 0;
  23305. int max_height;
  23306. struct nk_vec2 item_spacing;
  23307. struct nk_vec2 window_padding;
  23308. NK_ASSERT(ctx);
  23309. NK_ASSERT(items);
  23310. NK_ASSERT(ctx->current);
  23311. if (!ctx || !items ||!count)
  23312. return selected;
  23313. item_spacing = ctx->style.window.spacing;
  23314. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  23315. max_height = count * item_height + count * (int)item_spacing.y;
  23316. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  23317. size.y = NK_MIN(size.y, (float)max_height);
  23318. if (nk_combo_begin_label(ctx, items[selected], size)) {
  23319. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  23320. for (i = 0; i < count; ++i) {
  23321. if (nk_combo_item_label(ctx, items[i], NK_TEXT_LEFT))
  23322. selected = i;
  23323. }
  23324. nk_combo_end(ctx);
  23325. }
  23326. return selected;
  23327. }
  23328. NK_API int
  23329. nk_combo_separator(struct nk_context *ctx, const char *items_separated_by_separator,
  23330. int separator, int selected, int count, int item_height, struct nk_vec2 size)
  23331. {
  23332. int i;
  23333. int max_height;
  23334. struct nk_vec2 item_spacing;
  23335. struct nk_vec2 window_padding;
  23336. const char *current_item;
  23337. const char *iter;
  23338. int length = 0;
  23339. NK_ASSERT(ctx);
  23340. NK_ASSERT(items_separated_by_separator);
  23341. if (!ctx || !items_separated_by_separator)
  23342. return selected;
  23343. /* calculate popup window */
  23344. item_spacing = ctx->style.window.spacing;
  23345. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  23346. max_height = count * item_height + count * (int)item_spacing.y;
  23347. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  23348. size.y = NK_MIN(size.y, (float)max_height);
  23349. /* find selected item */
  23350. current_item = items_separated_by_separator;
  23351. for (i = 0; i < count; ++i) {
  23352. iter = current_item;
  23353. while (*iter && *iter != separator) iter++;
  23354. length = (int)(iter - current_item);
  23355. if (i == selected) break;
  23356. current_item = iter + 1;
  23357. }
  23358. if (nk_combo_begin_text(ctx, current_item, length, size)) {
  23359. current_item = items_separated_by_separator;
  23360. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  23361. for (i = 0; i < count; ++i) {
  23362. iter = current_item;
  23363. while (*iter && *iter != separator) iter++;
  23364. length = (int)(iter - current_item);
  23365. if (nk_combo_item_text(ctx, current_item, length, NK_TEXT_LEFT))
  23366. selected = i;
  23367. current_item = current_item + length + 1;
  23368. }
  23369. nk_combo_end(ctx);
  23370. }
  23371. return selected;
  23372. }
  23373. NK_API int
  23374. nk_combo_string(struct nk_context *ctx, const char *items_separated_by_zeros,
  23375. int selected, int count, int item_height, struct nk_vec2 size)
  23376. {
  23377. return nk_combo_separator(ctx, items_separated_by_zeros, '\0', selected, count, item_height, size);
  23378. }
  23379. NK_API int
  23380. nk_combo_callback(struct nk_context *ctx, void(*item_getter)(void*, int, const char**),
  23381. void *userdata, int selected, int count, int item_height, struct nk_vec2 size)
  23382. {
  23383. int i;
  23384. int max_height;
  23385. struct nk_vec2 item_spacing;
  23386. struct nk_vec2 window_padding;
  23387. const char *item;
  23388. NK_ASSERT(ctx);
  23389. NK_ASSERT(item_getter);
  23390. if (!ctx || !item_getter)
  23391. return selected;
  23392. /* calculate popup window */
  23393. item_spacing = ctx->style.window.spacing;
  23394. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  23395. max_height = count * item_height + count * (int)item_spacing.y;
  23396. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  23397. size.y = NK_MIN(size.y, (float)max_height);
  23398. item_getter(userdata, selected, &item);
  23399. if (nk_combo_begin_label(ctx, item, size)) {
  23400. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  23401. for (i = 0; i < count; ++i) {
  23402. item_getter(userdata, i, &item);
  23403. if (nk_combo_item_label(ctx, item, NK_TEXT_LEFT))
  23404. selected = i;
  23405. }
  23406. nk_combo_end(ctx);
  23407. } return selected;
  23408. }
  23409. NK_API void
  23410. nk_combobox(struct nk_context *ctx, const char **items, int count,
  23411. int *selected, int item_height, struct nk_vec2 size)
  23412. {
  23413. *selected = nk_combo(ctx, items, count, *selected, item_height, size);
  23414. }
  23415. NK_API void
  23416. nk_combobox_string(struct nk_context *ctx, const char *items_separated_by_zeros,
  23417. int *selected, int count, int item_height, struct nk_vec2 size)
  23418. {
  23419. *selected = nk_combo_string(ctx, items_separated_by_zeros, *selected, count, item_height, size);
  23420. }
  23421. NK_API void
  23422. nk_combobox_separator(struct nk_context *ctx, const char *items_separated_by_separator,
  23423. int separator,int *selected, int count, int item_height, struct nk_vec2 size)
  23424. {
  23425. *selected = nk_combo_separator(ctx, items_separated_by_separator, separator,
  23426. *selected, count, item_height, size);
  23427. }
  23428. NK_API void
  23429. nk_combobox_callback(struct nk_context *ctx,
  23430. void(*item_getter)(void* data, int id, const char **out_text),
  23431. void *userdata, int *selected, int count, int item_height, struct nk_vec2 size)
  23432. {
  23433. *selected = nk_combo_callback(ctx, item_getter, userdata, *selected, count, item_height, size);
  23434. }
  23435. /* ===============================================================
  23436. *
  23437. * TOOLTIP
  23438. *
  23439. * ===============================================================*/
  23440. NK_API int
  23441. nk_tooltip_begin(struct nk_context *ctx, float width)
  23442. {
  23443. int x,y,w,h;
  23444. struct nk_window *win;
  23445. const struct nk_input *in;
  23446. struct nk_rect bounds;
  23447. int ret;
  23448. NK_ASSERT(ctx);
  23449. NK_ASSERT(ctx->current);
  23450. NK_ASSERT(ctx->current->layout);
  23451. if (!ctx || !ctx->current || !ctx->current->layout)
  23452. return 0;
  23453. /* make sure that no nonblocking popup is currently active */
  23454. win = ctx->current;
  23455. in = &ctx->input;
  23456. if (win->popup.win && (win->popup.type & NK_PANEL_SET_NONBLOCK))
  23457. return 0;
  23458. w = nk_iceilf(width);
  23459. h = nk_iceilf(nk_null_rect.h);
  23460. x = nk_ifloorf(in->mouse.pos.x + 1) - (int)win->layout->clip.x;
  23461. y = nk_ifloorf(in->mouse.pos.y + 1) - (int)win->layout->clip.y;
  23462. bounds.x = (float)x;
  23463. bounds.y = (float)y;
  23464. bounds.w = (float)w;
  23465. bounds.h = (float)h;
  23466. ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
  23467. "__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER, bounds);
  23468. if (ret) win->layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
  23469. win->popup.type = NK_PANEL_TOOLTIP;
  23470. ctx->current->layout->type = NK_PANEL_TOOLTIP;
  23471. return ret;
  23472. }
  23473. NK_API void
  23474. nk_tooltip_end(struct nk_context *ctx)
  23475. {
  23476. NK_ASSERT(ctx);
  23477. NK_ASSERT(ctx->current);
  23478. if (!ctx || !ctx->current) return;
  23479. ctx->current->seq--;
  23480. nk_popup_close(ctx);
  23481. nk_popup_end(ctx);
  23482. }
  23483. NK_API void
  23484. nk_tooltip(struct nk_context *ctx, const char *text)
  23485. {
  23486. const struct nk_style *style;
  23487. struct nk_vec2 padding;
  23488. int text_len;
  23489. float text_width;
  23490. float text_height;
  23491. NK_ASSERT(ctx);
  23492. NK_ASSERT(ctx->current);
  23493. NK_ASSERT(ctx->current->layout);
  23494. NK_ASSERT(text);
  23495. if (!ctx || !ctx->current || !ctx->current->layout || !text)
  23496. return;
  23497. /* fetch configuration data */
  23498. style = &ctx->style;
  23499. padding = style->window.padding;
  23500. /* calculate size of the text and tooltip */
  23501. text_len = nk_strlen(text);
  23502. text_width = style->font->width(style->font->userdata,
  23503. style->font->height, text, text_len);
  23504. text_width += (4 * padding.x);
  23505. text_height = (style->font->height + 2 * padding.y);
  23506. /* execute tooltip and fill with text */
  23507. if (nk_tooltip_begin(ctx, (float)text_width)) {
  23508. nk_layout_row_dynamic(ctx, (float)text_height, 1);
  23509. nk_text(ctx, text, text_len, NK_TEXT_LEFT);
  23510. nk_tooltip_end(ctx);
  23511. }
  23512. }
  23513. #ifdef NK_INCLUDE_STANDARD_VARARGS
  23514. NK_API void
  23515. nk_tooltipf(struct nk_context *ctx, const char *fmt, ...)
  23516. {
  23517. va_list args;
  23518. va_start(args, fmt);
  23519. nk_tooltipfv(ctx, fmt, args);
  23520. va_end(args);
  23521. }
  23522. NK_API void
  23523. nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
  23524. {
  23525. char buf[256];
  23526. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  23527. nk_tooltip(ctx, buf);
  23528. }
  23529. #endif
  23530. #endif /* NK_IMPLEMENTATION */
  23531. /*
  23532. /// ## License
  23533. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~none
  23534. /// ------------------------------------------------------------------------------
  23535. /// This software is available under 2 licenses -- choose whichever you prefer.
  23536. /// ------------------------------------------------------------------------------
  23537. /// ALTERNATIVE A - MIT License
  23538. /// Copyright (c) 2016-2018 Micha Mettke
  23539. /// Permission is hereby granted, free of charge, to any person obtaining a copy of
  23540. /// this software and associated documentation files (the "Software"), to deal in
  23541. /// the Software without restriction, including without limitation the rights to
  23542. /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  23543. /// of the Software, and to permit persons to whom the Software is furnished to do
  23544. /// so, subject to the following conditions:
  23545. /// The above copyright notice and this permission notice shall be included in all
  23546. /// copies or substantial portions of the Software.
  23547. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23548. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23549. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23550. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23551. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23552. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23553. /// SOFTWARE.
  23554. /// ------------------------------------------------------------------------------
  23555. /// ALTERNATIVE B - Public Domain (www.unlicense.org)
  23556. /// This is free and unencumbered software released into the public domain.
  23557. /// Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  23558. /// software, either in source code form or as a compiled binary, for any purpose,
  23559. /// commercial or non-commercial, and by any means.
  23560. /// In jurisdictions that recognize copyright laws, the author or authors of this
  23561. /// software dedicate any and all copyright interest in the software to the public
  23562. /// domain. We make this dedication for the benefit of the public at large and to
  23563. /// the detriment of our heirs and successors. We intend this dedication to be an
  23564. /// overt act of relinquishment in perpetuity of all present and future rights to
  23565. /// this software under copyright law.
  23566. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23567. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23568. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23569. /// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  23570. /// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23571. /// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23572. /// ------------------------------------------------------------------------------
  23573. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23574. /// ## Changelog
  23575. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~none
  23576. /// [date][x.yy.zz]-[description]
  23577. /// -[date]: date on which the change has been pushed
  23578. /// -[x.yy.zz]: Numerical version string representation. Each version number on the right
  23579. /// resets back to zero if version on the left is incremented.
  23580. /// - [x]: Major version with API and library breaking changes
  23581. /// - [yy]: Minor version with non-breaking API and library changes
  23582. /// - [zz]: Bug fix version with no direct changes to API
  23583. ///
  23584. /// - 2019/09/20 (4.01.3) - Fixed a bug wherein combobox cannot be closed by clicking the header
  23585. /// when NK_BUTTON_TRIGGER_ON_RELEASE is defined.
  23586. /// - 2019/09/10 (4.01.2) - Fixed the nk_cos function, which deviated significantly.
  23587. /// - 2019/09/08 (4.01.1) - Fixed a bug wherein re-baking of fonts caused a segmentation
  23588. /// fault due to dst_font->glyph_count not being zeroed on subsequent
  23589. /// bakes of the same set of fonts.
  23590. /// - 2019/06/23 (4.01.0) - Added nk_***_get_scroll and nk_***_set_scroll for groups, windows, and popups.
  23591. /// - 2019/06/12 (4.00.3) - Fix panel background drawing bug.
  23592. /// - 2018/10/31 (4.00.2) - Added NK_KEYSTATE_BASED_INPUT to "fix" state based backends
  23593. /// like GLFW without breaking key repeat behavior on event based.
  23594. /// - 2018/04/01 (4.00.1) - Fixed calling `nk_convert` multiple time per single frame.
  23595. /// - 2018/04/01 (4.00.0) - BREAKING CHANGE: nk_draw_list_clear no longer tries to
  23596. /// clear provided buffers. So make sure to either free
  23597. /// or clear each passed buffer after calling nk_convert.
  23598. /// - 2018/02/23 (3.00.6) - Fixed slider dragging behavior.
  23599. /// - 2018/01/31 (3.00.5) - Fixed overcalculation of cursor data in font baking process.
  23600. /// - 2018/01/31 (3.00.4) - Removed name collision with stb_truetype.
  23601. /// - 2018/01/28 (3.00.3) - Fixed panel window border drawing bug.
  23602. /// - 2018/01/12 (3.00.2) - Added `nk_group_begin_titled` for separed group identifier and title.
  23603. /// - 2018/01/07 (3.00.1) - Started to change documentation style.
  23604. /// - 2018/01/05 (3.00.0) - BREAKING CHANGE: The previous color picker API was broken
  23605. /// because of conversions between float and byte color representation.
  23606. /// Color pickers now use floating point values to represent
  23607. /// HSV values. To get back the old behavior I added some additional
  23608. /// color conversion functions to cast between nk_color and
  23609. /// nk_colorf.
  23610. /// - 2017/12/23 (2.00.7) - Fixed small warning.
  23611. /// - 2017/12/23 (2.00.7) - Fixed `nk_edit_buffer` behavior if activated to allow input.
  23612. /// - 2017/12/23 (2.00.7) - Fixed modifyable progressbar dragging visuals and input behavior.
  23613. /// - 2017/12/04 (2.00.6) - Added formated string tooltip widget.
  23614. /// - 2017/11/18 (2.00.5) - Fixed window becoming hidden with flag `NK_WINDOW_NO_INPUT`.
  23615. /// - 2017/11/15 (2.00.4) - Fixed font merging.
  23616. /// - 2017/11/07 (2.00.3) - Fixed window size and position modifier functions.
  23617. /// - 2017/09/14 (2.00.2) - Fixed `nk_edit_buffer` and `nk_edit_focus` behavior.
  23618. /// - 2017/09/14 (2.00.1) - Fixed window closing behavior.
  23619. /// - 2017/09/14 (2.00.0) - BREAKING CHANGE: Modifing window position and size funtions now
  23620. /// require the name of the window and must happen outside the window
  23621. /// building process (between function call nk_begin and nk_end).
  23622. /// - 2017/09/11 (1.40.9) - Fixed window background flag if background window is declared last.
  23623. /// - 2017/08/27 (1.40.8) - Fixed `nk_item_is_any_active` for hidden windows.
  23624. /// - 2017/08/27 (1.40.7) - Fixed window background flag.
  23625. /// - 2017/07/07 (1.40.6) - Fixed missing clipping rect check for hovering/clicked
  23626. /// query for widgets.
  23627. /// - 2017/07/07 (1.40.5) - Fixed drawing bug for vertex output for lines and stroked
  23628. /// and filled rectangles.
  23629. /// - 2017/07/07 (1.40.4) - Fixed bug in nk_convert trying to add windows that are in
  23630. /// process of being destroyed.
  23631. /// - 2017/07/07 (1.40.3) - Fixed table internal bug caused by storing table size in
  23632. /// window instead of directly in table.
  23633. /// - 2017/06/30 (1.40.2) - Removed unneeded semicolon in C++ NK_ALIGNOF macro.
  23634. /// - 2017/06/30 (1.40.1) - Fixed drawing lines smaller or equal zero.
  23635. /// - 2017/06/08 (1.40.0) - Removed the breaking part of last commit. Auto layout now only
  23636. /// comes in effect if you pass in zero was row height argument.
  23637. /// - 2017/06/08 (1.40.0) - BREAKING CHANGE: while not directly API breaking it will change
  23638. /// how layouting works. From now there will be an internal minimum
  23639. /// row height derived from font height. If you need a row smaller than
  23640. /// that you can directly set it by `nk_layout_set_min_row_height` and
  23641. /// reset the value back by calling `nk_layout_reset_min_row_height.
  23642. /// - 2017/06/08 (1.39.1) - Fixed property text edit handling bug caused by past `nk_widget` fix.
  23643. /// - 2017/06/08 (1.39.0) - Added function to retrieve window space without calling a `nk_layout_xxx` function.
  23644. /// - 2017/06/06 (1.38.5) - Fixed `nk_convert` return flag for command buffer.
  23645. /// - 2017/05/23 (1.38.4) - Fixed activation behavior for widgets partially clipped.
  23646. /// - 2017/05/10 (1.38.3) - Fixed wrong min window size mouse scaling over boundries.
  23647. /// - 2017/05/09 (1.38.2) - Fixed vertical scrollbar drawing with not enough space.
  23648. /// - 2017/05/09 (1.38.1) - Fixed scaler dragging behavior if window size hits minimum size.
  23649. /// - 2017/05/06 (1.38.0) - Added platform double-click support.
  23650. /// - 2017/04/20 (1.37.1) - Fixed key repeat found inside glfw demo backends.
  23651. /// - 2017/04/20 (1.37.0) - Extended properties with selection and clipboard support.
  23652. /// - 2017/04/20 (1.36.2) - Fixed #405 overlapping rows with zero padding and spacing.
  23653. /// - 2017/04/09 (1.36.1) - Fixed #403 with another widget float error.
  23654. /// - 2017/04/09 (1.36.0) - Added window `NK_WINDOW_NO_INPUT` and `NK_WINDOW_NOT_INTERACTIVE` flags.
  23655. /// - 2017/04/09 (1.35.3) - Fixed buffer heap corruption.
  23656. /// - 2017/03/25 (1.35.2) - Fixed popup overlapping for `NK_WINDOW_BACKGROUND` windows.
  23657. /// - 2017/03/25 (1.35.1) - Fixed windows closing behavior.
  23658. /// - 2017/03/18 (1.35.0) - Added horizontal scroll requested in #377.
  23659. /// - 2017/03/18 (1.34.3) - Fixed long window header titles.
  23660. /// - 2017/03/04 (1.34.2) - Fixed text edit filtering.
  23661. /// - 2017/03/04 (1.34.1) - Fixed group closable flag.
  23662. /// - 2017/02/25 (1.34.0) - Added custom draw command for better language binding support.
  23663. /// - 2017/01/24 (1.33.0) - Added programatic way of remove edit focus.
  23664. /// - 2017/01/24 (1.32.3) - Fixed wrong define for basic type definitions for windows.
  23665. /// - 2017/01/21 (1.32.2) - Fixed input capture from hidden or closed windows.
  23666. /// - 2017/01/21 (1.32.1) - Fixed slider behavior and drawing.
  23667. /// - 2017/01/13 (1.32.0) - Added flag to put scaler into the bottom left corner.
  23668. /// - 2017/01/13 (1.31.0) - Added additional row layouting method to combine both
  23669. /// dynamic and static widgets.
  23670. /// - 2016/12/31 (1.30.0) - Extended scrollbar offset from 16-bit to 32-bit.
  23671. /// - 2016/12/31 (1.29.2) - Fixed closing window bug of minimized windows.
  23672. /// - 2016/12/03 (1.29.1) - Fixed wrapped text with no seperator and C89 error.
  23673. /// - 2016/12/03 (1.29.0) - Changed text wrapping to process words not characters.
  23674. /// - 2016/11/22 (1.28.6) - Fixed window minimized closing bug.
  23675. /// - 2016/11/19 (1.28.5) - Fixed abstract combo box closing behavior.
  23676. /// - 2016/11/19 (1.28.4) - Fixed tooltip flickering.
  23677. /// - 2016/11/19 (1.28.3) - Fixed memory leak caused by popup repeated closing.
  23678. /// - 2016/11/18 (1.28.2) - Fixed memory leak caused by popup panel allocation.
  23679. /// - 2016/11/10 (1.28.1) - Fixed some warnings and C++ error.
  23680. /// - 2016/11/10 (1.28.0) - Added additional `nk_button` versions which allows to directly
  23681. /// pass in a style struct to change buttons visual.
  23682. /// - 2016/11/10 (1.27.0) - Added additional `nk_tree` versions to support external state
  23683. /// storage. Just like last the `nk_group` commit the main
  23684. /// advantage is that you optionally can minimize nuklears runtime
  23685. /// memory consumption or handle hash collisions.
  23686. /// - 2016/11/09 (1.26.0) - Added additional `nk_group` version to support external scrollbar
  23687. /// offset storage. Main advantage is that you can externalize
  23688. /// the memory management for the offset. It could also be helpful
  23689. /// if you have a hash collision in `nk_group_begin` but really
  23690. /// want the name. In addition I added `nk_list_view` which allows
  23691. /// to draw big lists inside a group without actually having to
  23692. /// commit the whole list to nuklear (issue #269).
  23693. /// - 2016/10/30 (1.25.1) - Fixed clipping rectangle bug inside `nk_draw_list`.
  23694. /// - 2016/10/29 (1.25.0) - Pulled `nk_panel` memory management into nuklear and out of
  23695. /// the hands of the user. From now on users don't have to care
  23696. /// about panels unless they care about some information. If you
  23697. /// still need the panel just call `nk_window_get_panel`.
  23698. /// - 2016/10/21 (1.24.0) - Changed widget border drawing to stroked rectangle from filled
  23699. /// rectangle for less overdraw and widget background transparency.
  23700. /// - 2016/10/18 (1.23.0) - Added `nk_edit_focus` for manually edit widget focus control.
  23701. /// - 2016/09/29 (1.22.7) - Fixed deduction of basic type in non `<stdint.h>` compilation.
  23702. /// - 2016/09/29 (1.22.6) - Fixed edit widget UTF-8 text cursor drawing bug.
  23703. /// - 2016/09/28 (1.22.5) - Fixed edit widget UTF-8 text appending/inserting/removing.
  23704. /// - 2016/09/28 (1.22.4) - Fixed drawing bug inside edit widgets which offset all text
  23705. /// text in every edit widget if one of them is scrolled.
  23706. /// - 2016/09/28 (1.22.3) - Fixed small bug in edit widgets if not active. The wrong
  23707. /// text length is passed. It should have been in bytes but
  23708. /// was passed as glyphes.
  23709. /// - 2016/09/20 (1.22.2) - Fixed color button size calculation.
  23710. /// - 2016/09/20 (1.22.1) - Fixed some `nk_vsnprintf` behavior bugs and removed `<stdio.h>`
  23711. /// again from `NK_INCLUDE_STANDARD_VARARGS`.
  23712. /// - 2016/09/18 (1.22.0) - C89 does not support vsnprintf only C99 and newer as well
  23713. /// as C++11 and newer. In addition to use vsnprintf you have
  23714. /// to include <stdio.h>. So just defining `NK_INCLUDE_STD_VAR_ARGS`
  23715. /// is not enough. That behavior is now fixed. By default if
  23716. /// both varargs as well as stdio is selected I try to use
  23717. /// vsnprintf if not possible I will revert to vsprintf. If
  23718. /// varargs but not stdio was defined I will use my own function.
  23719. /// - 2016/09/15 (1.21.2) - Fixed panel `close` behavior for deeper panel levels.
  23720. /// - 2016/09/15 (1.21.1) - Fixed C++ errors and wrong argument to `nk_panel_get_xxxx`.
  23721. /// - 2016/09/13 (1.21.0) - !BREAKING! Fixed nonblocking popup behavior in menu, combo,
  23722. /// and contextual which prevented closing in y-direction if
  23723. /// popup did not reach max height.
  23724. /// In addition the height parameter was changed into vec2
  23725. /// for width and height to have more control over the popup size.
  23726. /// - 2016/09/13 (1.20.3) - Cleaned up and extended type selection.
  23727. /// - 2016/09/13 (1.20.2) - Fixed slider behavior hopefully for the last time. This time
  23728. /// all calculation are correct so no more hackery.
  23729. /// - 2016/09/13 (1.20.1) - Internal change to divide window/panel flags into panel flags and types.
  23730. /// Suprisinly spend years in C and still happened to confuse types
  23731. /// with flags. Probably something to take note.
  23732. /// - 2016/09/08 (1.20.0) - Added additional helper function to make it easier to just
  23733. /// take the produced buffers from `nk_convert` and unplug the
  23734. /// iteration process from `nk_context`. So now you can
  23735. /// just use the vertex,element and command buffer + two pointer
  23736. /// inside the command buffer retrieved by calls `nk__draw_begin`
  23737. /// and `nk__draw_end` and macro `nk_draw_foreach_bounded`.
  23738. /// - 2016/09/08 (1.19.0) - Added additional asserts to make sure every `nk_xxx_begin` call
  23739. /// for windows, popups, combobox, menu and contextual is guarded by
  23740. /// `if` condition and does not produce false drawing output.
  23741. /// - 2016/09/08 (1.18.0) - Changed confusing name for `NK_SYMBOL_RECT_FILLED`, `NK_SYMBOL_RECT`
  23742. /// to hopefully easier to understand `NK_SYMBOL_RECT_FILLED` and
  23743. /// `NK_SYMBOL_RECT_OUTLINE`.
  23744. /// - 2016/09/08 (1.17.0) - Changed confusing name for `NK_SYMBOL_CIRLCE_FILLED`, `NK_SYMBOL_CIRCLE`
  23745. /// to hopefully easier to understand `NK_SYMBOL_CIRCLE_FILLED` and
  23746. /// `NK_SYMBOL_CIRCLE_OUTLINE`.
  23747. /// - 2016/09/08 (1.16.0) - Added additional checks to select correct types if `NK_INCLUDE_FIXED_TYPES`
  23748. /// is not defined by supporting the biggest compiler GCC, clang and MSVC.
  23749. /// - 2016/09/07 (1.15.3) - Fixed `NK_INCLUDE_COMMAND_USERDATA` define to not cause an error.
  23750. /// - 2016/09/04 (1.15.2) - Fixed wrong combobox height calculation.
  23751. /// - 2016/09/03 (1.15.1) - Fixed gaps inside combo boxes in OpenGL.
  23752. /// - 2016/09/02 (1.15.0) - Changed nuklear to not have any default vertex layout and
  23753. /// instead made it user provided. The range of types to convert
  23754. /// to is quite limited at the moment, but I would be more than
  23755. /// happy to accept PRs to add additional.
  23756. /// - 2016/08/30 (1.14.2) - Removed unused variables.
  23757. /// - 2016/08/30 (1.14.1) - Fixed C++ build errors.
  23758. /// - 2016/08/30 (1.14.0) - Removed mouse dragging from SDL demo since it does not work correctly.
  23759. /// - 2016/08/30 (1.13.4) - Tweaked some default styling variables.
  23760. /// - 2016/08/30 (1.13.3) - Hopefully fixed drawing bug in slider, in general I would
  23761. /// refrain from using slider with a big number of steps.
  23762. /// - 2016/08/30 (1.13.2) - Fixed close and minimize button which would fire even if the
  23763. /// window was in Read Only Mode.
  23764. /// - 2016/08/30 (1.13.1) - Fixed popup panel padding handling which was previously just
  23765. /// a hack for combo box and menu.
  23766. /// - 2016/08/30 (1.13.0) - Removed `NK_WINDOW_DYNAMIC` flag from public API since
  23767. /// it is bugged and causes issues in window selection.
  23768. /// - 2016/08/30 (1.12.0) - Removed scaler size. The size of the scaler is now
  23769. /// determined by the scrollbar size.
  23770. /// - 2016/08/30 (1.11.2) - Fixed some drawing bugs caused by changes from 1.11.0.
  23771. /// - 2016/08/30 (1.11.1) - Fixed overlapping minimized window selection.
  23772. /// - 2016/08/30 (1.11.0) - Removed some internal complexity and overly complex code
  23773. /// handling panel padding and panel border.
  23774. /// - 2016/08/29 (1.10.0) - Added additional height parameter to `nk_combobox_xxx`.
  23775. /// - 2016/08/29 (1.10.0) - Fixed drawing bug in dynamic popups.
  23776. /// - 2016/08/29 (1.10.0) - Added experimental mouse scrolling to popups, menus and comboboxes.
  23777. /// - 2016/08/26 (1.10.0) - Added window name string prepresentation to account for
  23778. /// hash collisions. Currently limited to `NK_WINDOW_MAX_NAME`
  23779. /// which in term can be redefined if not big enough.
  23780. /// - 2016/08/26 (1.10.0) - Added stacks for temporary style/UI changes in code.
  23781. /// - 2016/08/25 (1.10.0) - Changed `nk_input_is_key_pressed` and 'nk_input_is_key_released'
  23782. /// to account for key press and release happening in one frame.
  23783. /// - 2016/08/25 (1.10.0) - Added additional nk_edit flag to directly jump to the end on activate.
  23784. /// - 2016/08/17 (1.09.6) - Removed invalid check for value zero in `nk_propertyx`.
  23785. /// - 2016/08/16 (1.09.5) - Fixed ROM mode for deeper levels of popup windows parents.
  23786. /// - 2016/08/15 (1.09.4) - Editbox are now still active if enter was pressed with flag
  23787. /// `NK_EDIT_SIG_ENTER`. Main reasoning is to be able to keep
  23788. /// typing after commiting.
  23789. /// - 2016/08/15 (1.09.4) - Removed redundant code.
  23790. /// - 2016/08/15 (1.09.4) - Fixed negative numbers in `nk_strtoi` and remove unused variable.
  23791. /// - 2016/08/15 (1.09.3) - Fixed `NK_WINDOW_BACKGROUND` flag behavior to select a background
  23792. /// window only as selected by hovering and not by clicking.
  23793. /// - 2016/08/14 (1.09.2) - Fixed a bug in font atlas which caused wrong loading
  23794. /// of glyphes for font with multiple ranges.
  23795. /// - 2016/08/12 (1.09.1) - Added additional function to check if window is currently
  23796. /// hidden and therefore not visible.
  23797. /// - 2016/08/12 (1.09.1) - nk_window_is_closed now queries the correct flag `NK_WINDOW_CLOSED`
  23798. /// instead of the old flag `NK_WINDOW_HIDDEN`.
  23799. /// - 2016/08/09 (1.09.0) - Added additional double version to nk_property and changed
  23800. /// the underlying implementation to not cast to float and instead
  23801. /// work directly on the given values.
  23802. /// - 2016/08/09 (1.08.0) - Added additional define to overwrite library internal
  23803. /// floating pointer number to string conversion for additional
  23804. /// precision.
  23805. /// - 2016/08/09 (1.08.0) - Added additional define to overwrite library internal
  23806. /// string to floating point number conversion for additional
  23807. /// precision.
  23808. /// - 2016/08/08 (1.07.2) - Fixed compiling error without define `NK_INCLUDE_FIXED_TYPE`.
  23809. /// - 2016/08/08 (1.07.1) - Fixed possible floating point error inside `nk_widget` leading
  23810. /// to wrong wiget width calculation which results in widgets falsly
  23811. /// becomming tagged as not inside window and cannot be accessed.
  23812. /// - 2016/08/08 (1.07.0) - Nuklear now differentiates between hiding a window (NK_WINDOW_HIDDEN) and
  23813. /// closing a window (NK_WINDOW_CLOSED). A window can be hidden/shown
  23814. /// by using `nk_window_show` and closed by either clicking the close
  23815. /// icon in a window or by calling `nk_window_close`. Only closed
  23816. /// windows get removed at the end of the frame while hidden windows
  23817. /// remain.
  23818. /// - 2016/08/08 (1.06.0) - Added `nk_edit_string_zero_terminated` as a second option to
  23819. /// `nk_edit_string` which takes, edits and outputs a '\0' terminated string.
  23820. /// - 2016/08/08 (1.05.4) - Fixed scrollbar auto hiding behavior.
  23821. /// - 2016/08/08 (1.05.3) - Fixed wrong panel padding selection in `nk_layout_widget_space`.
  23822. /// - 2016/08/07 (1.05.2) - Fixed old bug in dynamic immediate mode layout API, calculating
  23823. /// wrong item spacing and panel width.
  23824. /// - 2016/08/07 (1.05.1) - Hopefully finally fixed combobox popup drawing bug.
  23825. /// - 2016/08/07 (1.05.0) - Split varargs away from `NK_INCLUDE_STANDARD_IO` into own
  23826. /// define `NK_INCLUDE_STANDARD_VARARGS` to allow more fine
  23827. /// grained controlled over library includes.
  23828. /// - 2016/08/06 (1.04.5) - Changed memset calls to `NK_MEMSET`.
  23829. /// - 2016/08/04 (1.04.4) - Fixed fast window scaling behavior.
  23830. /// - 2016/08/04 (1.04.3) - Fixed window scaling, movement bug which appears if you
  23831. /// move/scale a window and another window is behind it.
  23832. /// If you are fast enough then the window behind gets activated
  23833. /// and the operation is blocked. I now require activating
  23834. /// by hovering only if mouse is not pressed.
  23835. /// - 2016/08/04 (1.04.2) - Fixed changing fonts.
  23836. /// - 2016/08/03 (1.04.1) - Fixed `NK_WINDOW_BACKGROUND` behavior.
  23837. /// - 2016/08/03 (1.04.0) - Added color parameter to `nk_draw_image`.
  23838. /// - 2016/08/03 (1.04.0) - Added additional window padding style attributes for
  23839. /// sub windows (combo, menu, ...).
  23840. /// - 2016/08/03 (1.04.0) - Added functions to show/hide software cursor.
  23841. /// - 2016/08/03 (1.04.0) - Added `NK_WINDOW_BACKGROUND` flag to force a window
  23842. /// to be always in the background of the screen.
  23843. /// - 2016/08/03 (1.03.2) - Removed invalid assert macro for NK_RGB color picker.
  23844. /// - 2016/08/01 (1.03.1) - Added helper macros into header include guard.
  23845. /// - 2016/07/29 (1.03.0) - Moved the window/table pool into the header part to
  23846. /// simplify memory management by removing the need to
  23847. /// allocate the pool.
  23848. /// - 2016/07/29 (1.02.0) - Added auto scrollbar hiding window flag which if enabled
  23849. /// will hide the window scrollbar after NK_SCROLLBAR_HIDING_TIMEOUT
  23850. /// seconds without window interaction. To make it work
  23851. /// you have to also set a delta time inside the `nk_context`.
  23852. /// - 2016/07/25 (1.01.1) - Fixed small panel and panel border drawing bugs.
  23853. /// - 2016/07/15 (1.01.0) - Added software cursor to `nk_style` and `nk_context`.
  23854. /// - 2016/07/15 (1.01.0) - Added const correctness to `nk_buffer_push' data argument.
  23855. /// - 2016/07/15 (1.01.0) - Removed internal font baking API and simplified
  23856. /// font atlas memory management by converting pointer
  23857. /// arrays for fonts and font configurations to lists.
  23858. /// - 2016/07/15 (1.00.0) - Changed button API to use context dependend button
  23859. /// behavior instead of passing it for every function call.
  23860. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23861. /// ## Gallery
  23862. /// ![Figure [blue]: Feature overview with blue color styling](https://cloud.githubusercontent.com/assets/8057201/13538240/acd96876-e249-11e5-9547-5ac0b19667a0.png)
  23863. /// ![Figure [red]: Feature overview with red color styling](https://cloud.githubusercontent.com/assets/8057201/13538243/b04acd4c-e249-11e5-8fd2-ad7744a5b446.png)
  23864. /// ![Figure [widgets]: Widget overview](https://cloud.githubusercontent.com/assets/8057201/11282359/3325e3c6-8eff-11e5-86cb-cf02b0596087.png)
  23865. /// ![Figure [blackwhite]: Black and white](https://cloud.githubusercontent.com/assets/8057201/11033668/59ab5d04-86e5-11e5-8091-c56f16411565.png)
  23866. /// ![Figure [filexp]: File explorer](https://cloud.githubusercontent.com/assets/8057201/10718115/02a9ba08-7b6b-11e5-950f-adacdd637739.png)
  23867. /// ![Figure [opengl]: OpenGL Editor](https://cloud.githubusercontent.com/assets/8057201/12779619/2a20d72c-ca69-11e5-95fe-4edecf820d5c.png)
  23868. /// ![Figure [nodedit]: Node Editor](https://cloud.githubusercontent.com/assets/8057201/9976995/e81ac04a-5ef7-11e5-872b-acd54fbeee03.gif)
  23869. /// ![Figure [skinning]: Using skinning in Nuklear](https://cloud.githubusercontent.com/assets/8057201/15991632/76494854-30b8-11e6-9555-a69840d0d50b.png)
  23870. /// ![Figure [bf]: Heavy modified version](https://cloud.githubusercontent.com/assets/8057201/14902576/339926a8-0d9c-11e6-9fee-a8b73af04473.png)
  23871. ///
  23872. /// ## Credits
  23873. /// Developed by Micha Mettke and every direct or indirect github contributor. <br /><br />
  23874. ///
  23875. /// 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 />
  23876. /// Uses [stddoc.c](https://github.com/r-lyeh/stddoc.c) from [email protected] for documentation generation <br /><br />
  23877. /// Embeds ProggyClean.ttf font by Tristan Grimmer (MIT license). <br />
  23878. ///
  23879. /// Big thank you to Omar Cornut (ocornut@github) for his [imgui library](https://github.com/ocornut/imgui) and
  23880. /// giving me the inspiration for this library, Casey Muratori for handmade hero
  23881. /// and his original immediate mode graphical user interface idea and Sean
  23882. /// Barret for his amazing single header libraries which restored my faith
  23883. /// in libraries and brought me to create some of my own. Finally Apoorva Joshi
  23884. /// for his single header file packer.
  23885. */